Skip to content

Commit

Permalink
Merge pull request #34 from jacobwilliams/33-real-kinds
Browse files Browse the repository at this point in the history
added support for specifying real kind via preprocessor flag
  • Loading branch information
jacobwilliams authored Feb 6, 2022
2 parents f6b08a8 + aacc0a6 commit 8b3b72f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 188 deletions.
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,49 @@ Currently, this module can be used to generate simple plots from Fortran. Event
The way it works is simply to generate a Python script with the plotting code, which
is then executed from the command line using the Fortran ```execute_command_line``` function.

The module requires a modern Fortran compiler (it uses various Fortran 2003/2008 features such as deferred-length strings). It should work fine with the latest gfortran or ifort compilers. A simple script ```build.sh``` is provided for building the library and test program (requires gfortran and [FoBiS](https://github.com/szaghi/FoBiS)). It will also build the HTML documentation if [FORD](https://github.com/Fortran-FOSS-Programmers/ford) is installed. A `fpm.toml` file is also provided for use with the [Fortran Package Manager](https://github.com/fortran-lang/fpm).
### Compiling

The module requires a modern Fortran compiler (it uses various Fortran 2003/2008 features such as deferred-length strings). It should work fine with the latest gfortran or ifort compilers.

A `fmp.toml` file is provided for compiling pyplot-fortran with the [Fortran Package Manager](https://github.com/fortran-lang/fpm). For example, to build:

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

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
```

To use `pyplot-fortran` within your fpm project, add the following to your `fpm.toml` file:
```toml
[dependencies]
pyplot-fortran = { git="https://github.com/jacobwilliams/pyplot-fortran.git" }
```

or, to use a specific version:
```toml
[dependencies]
pyplot-fortran = { git="https://github.com/jacobwilliams/pyplot-fortran.git", tag = "3.1.0" }
```

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

### Supported plot types

Expand All @@ -30,7 +72,7 @@ The module requires a modern Fortran compiler (it uses various Fortran 2003/2008

The following example generates a plot of the sine function:

```Fortran
```fortran
program test
use,intrinsic :: iso_fortran_env, only: wp => real64
Expand All @@ -57,7 +99,7 @@ The following example generates a plot of the sine function:

### Documentation

* The API documentation for the current ```master``` branch can be found [here](https://jacobwilliams.github.io/pyplot-fortran/). This is generated by processing the source files with [FORD](https://github.com/Fortran-FOSS-Programmers/ford). Note that the build script will also generate these files automatically in the ```doc``` folder, assuming you have FORD installed.
* The API documentation for the current ```master``` branch can be found [here](https://jacobwilliams.github.io/pyplot-fortran/). This is generated by processing the source files with [FORD](https://github.com/Fortran-FOSS-Programmers/ford).

### See also

Expand Down
37 changes: 0 additions & 37 deletions build.sh

This file was deleted.

144 changes: 0 additions & 144 deletions pyplot.fobis

This file was deleted.

27 changes: 25 additions & 2 deletions src/pyplot_module.f90 → src/pyplot_module.F90
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,39 @@
!
!# See also
! * Inspired by: [EasyPlot](https://pypi.python.org/pypi/EasyPlot)
!
!@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 pyplot_module

use, intrinsic :: iso_fortran_env, only : real64, error_unit
use, intrinsic :: iso_fortran_env

implicit none

private

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

integer,parameter :: wp = pyplot_wp !! local copy of `pyplot_wp` with a shorter name

character(len=*), parameter :: tmp_file = 'pyplot_module_temp_1234567890.py' !! Default name of the temporary file
!! (this can also be user-specified).
Expand Down
3 changes: 1 addition & 2 deletions tests/test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

program test

use, intrinsic :: iso_fortran_env, only : wp => real64
use pyplot_module, only : pyplot
use pyplot_module, only : pyplot, wp => pyplot_wp

implicit none

Expand Down

0 comments on commit 8b3b72f

Please sign in to comment.