Skip to content

Commit

Permalink
Merge pull request #9 from jacobwilliams/8-real-kinds
Browse files Browse the repository at this point in the history
added support for specifying the real kind via preprocessor flag
  • Loading branch information
jacobwilliams authored Feb 6, 2022
2 parents 795c896 + d0c9517 commit d1b4528
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: CI
on: [push]
on: [push, pull_request]
jobs:

Build:
Expand Down
22 changes: 18 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@ A modern Fortran library for finding the roots of continuous scalar functions of
A `fmp.toml` file is provided for compiling roots-fortran with the [Fortran Package Manager](https://github.com/fortran-lang/fpm). For example, to build:

```
fpm build --profile release
fpm build --profile release
```

And to run the unit tests:
By default, the library is built with double precision (`real64`) real values. Explicitly specifying the real kind can be done using the following processor flags:

Preprocessor flag | Kind | Number of bytes
----------------- | ----- | ---------------
`REAL32` | `real(kind=real32)` | 4
`REAL64` | `real(kind=real64)` | 8
`REAL128` | `real(kind=real128)` | 16

For example, to build a single precision version of the library, use:

```
fpm build --profile release --flag "-DREAL32"
```

To run the unit tests:

```
fpm test
fpm test
```

To use `roots-fortran` within your fpm project, add the following to your `fpm.toml` file:
Expand All @@ -36,7 +50,7 @@ or, to use a specific version:
roots-fortran = { git="https://github.com/jacobwilliams/roots-fortran.git", tag = "1.0.0" }
```

To generate the documentation using [ford](https://github.com/Fortran-FOSS-Programmers/ford), run: ```ford roots-fortran.fobis```
To generate the documentation using [ford](https://github.com/Fortran-FOSS-Programmers/ford), run: ```ford roots-fortran.md```

## Usage

Expand Down
2 changes: 1 addition & 1 deletion fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ auto-examples = false
auto-tests = true

[dev-dependencies]
pyplot-fortran = { git = "https://github.com/jacobwilliams/pyplot-fortran", tag = "3.1.0" }
pyplot-fortran = { git = "https://github.com/jacobwilliams/pyplot-fortran", tag = "3.2.0" }

[library]
source-dir = "src"
Expand Down
24 changes: 23 additions & 1 deletion src/root_module.f90 → src/root_module.F90
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
!
!### Author
! * Jacob Williams
!
!@note The default real kind (`wp`) can be
! changed using optional preprocessor flags.
! This library was built with real kind:
#ifdef REAL32
! `real(kind=real32)` [4 bytes]
#elif REAL64
! `real(kind=real64)` [8 bytes]
#elif REAL128
! `real(kind=real128)` [16 bytes]
#else
! `real(kind=real64)` [8 bytes]
#endif

module root_module

Expand All @@ -16,7 +29,16 @@ module root_module

private

integer,parameter,public :: root_module_rk = real64 !! real kind used by this module
#ifdef REAL32
integer,parameter,public :: root_module_rk = real32 !! real kind used by this module [4 bytes]
#elif REAL64
integer,parameter,public :: root_module_rk = real64 !! real kind used by this module [8 bytes]
#elif REAL128
integer,parameter,public :: root_module_rk = real128 !! real kind used by this module [16 bytes]
#else
integer,parameter,public :: root_module_rk = real64 !! real kind used by this module [8 bytes]
#endif

integer,parameter :: wp = root_module_rk !! local copy of `root_module_rk` with a shorter name
integer,parameter :: name_len = 32 !! max length of the method names

Expand Down
5 changes: 4 additions & 1 deletion test/root_tests.f90
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,27 @@ subroutine test()
integer :: irepeat !! test repeat counter
integer :: i
real(wp) :: atol, rtol, ftol
real(wp) :: tol_for_check !! for pass/fail check

integer,parameter :: n_repeat = 1 !! number of times to repeat each test for timing purposes
real(wp),parameter :: tol_for_check = 1.0e-7_wp !! for pass/fail check
integer,parameter :: maxiter = 1000 !! maximum number of iterations

select case (wp)
case(real32)
atol = 1.0e-5_wp
rtol = 1.0e-5_wp
ftol = 1.0e-5_wp
tol_for_check = 1.0e-4_wp
case(real64)
atol = 1.0e-15_wp
rtol = 1.0e-13_wp
ftol = 1.0e-15_wp
tol_for_check = 1.0e-7_wp
case(real128)
atol = 1.0e-25_wp
rtol = 1.0e-23_wp
ftol = 1.0e-25_wp
tol_for_check = 1.0e-16_wp
case default
error stop 'unknown real kind'
end select
Expand Down
6 changes: 3 additions & 3 deletions test/root_tests_oo.f90
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ program root_tests_oo
integer :: iflag
character(len=1000) :: line

real(wp),parameter :: ftol = 1.0e-15_wp
real(wp),parameter :: rtol = 1.0e-13_wp
real(wp),parameter :: atol = 1.0e-16_wp
real(wp),parameter :: ftol = epsilon(1.0_wp) * 10 !1.0e-15_wp
real(wp),parameter :: rtol = epsilon(1.0_wp) * 100 !1.0e-13_wp
real(wp),parameter :: atol = epsilon(1.0_wp) * 1 !1.0e-16_wp
integer,parameter :: maxiter = 1000

character(len=*),parameter :: fmt = '(A25, 1X,A25, 1X,A25, 1X,A5,1X,A5)' !! format for header
Expand Down

0 comments on commit d1b4528

Please sign in to comment.