Skip to content

Commit 2a6fd9e

Browse files
committed
First commit.
0 parents  commit 2a6fd9e

File tree

8 files changed

+2368
-0
lines changed

8 files changed

+2368
-0
lines changed

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Compiled Object files
2+
*.slo
3+
*.lo
4+
*.o
5+
*.obj
6+
7+
# Precompiled Headers
8+
*.gch
9+
*.pch
10+
11+
# Compiled Dynamic libraries
12+
*.so
13+
*.dylib
14+
*.dll
15+
16+
# Fortran module files
17+
*.mod
18+
19+
# Compiled Static libraries
20+
*.lai
21+
*.la
22+
*.a
23+
*.lib
24+
25+
# Executables
26+
*.exe
27+
*.out
28+
*.app
29+
30+
# Directories
31+
build
32+
doc
33+
lib
34+
bin

LICENSE

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Copyright (c) 2000-2008, Roland Schmehl.
2+
Copyright (c) 2017, Jacob Williams.
3+
4+
All rights reserved.
5+
6+
* Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are
8+
met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
13+
* Redistributions in binary form must reproduce the above copyright
14+
notice, this list of conditions and the following disclaimer in the
15+
documentation and/or other materials provided with the distribution.
16+
17+
* Neither the name of the copyright holder nor the names of its
18+
contributors may be used to endorse or promote products derived from
19+
this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
### Description
2+
3+
This function parser module is intended for applications where a set of
4+
mathematical fortran-style expressions is specified at runtime and is
5+
then evaluated for a large number of variable values. This is done by
6+
compiling the set of function strings into byte code, which is
7+
interpreted efficiently for the various variable values.
8+
9+
### Basic usage
10+
11+
#### Module Import
12+
13+
In all program units where you want to use the function parser
14+
you must import the module by:
15+
16+
```fortran
17+
use function_parser
18+
```
19+
20+
This command imports only 3 public types: `fparser`, `fparser_array`, and
21+
`list_of_errors`, which are explained in the following. The remainder of the
22+
module is hidden to the calling program.
23+
24+
#### Function parsing
25+
26+
A single function string `funcstr` can be parsed (checked and compiled) into
27+
bytecode by calling the `fparser` class method subroutine `parse`:
28+
29+
```fortran
30+
call me%parse(funcstr, var, case_sensitive, error_msg)
31+
```
32+
33+
The variable names as they appear in the string `funcstr` have to be passed
34+
in the one-dimensional string array `var` (zero size of `var` is acceptable).
35+
The number of variables is implicitly passed by the dimension of this array.
36+
For some notes on the syntax of the function string see below.
37+
38+
To parse an array of function strings, you can use the `fparser_array` class
39+
method `parse` in a similar manner.
40+
41+
#### Function evaluation
42+
43+
The function value is evaluated for a specific set of variable values
44+
by calling the `fparser` class method subroutine `evaluate`:
45+
46+
```fortran
47+
call me%evaluate(val, res, error_msg)
48+
```
49+
50+
The variable values are passed in the one-dimensional array `val` which must
51+
have the same dimension as array `var`.
52+
53+
To evaluate an array of function strings, you can use the `fparser_array` class
54+
method `evaluate` in a similar manner.
55+
56+
#### Cleanup
57+
58+
To free the memory and destroy a variable of type `fparser` or `fparser_array`,
59+
use the `destroy` method:
60+
61+
```fortran
62+
call me%destroy()
63+
```
64+
65+
### Error handling
66+
67+
Errors are reported using the `error_msg` output of the `parse` and `evaluate`
68+
methods. This is variable of type `list_of_errors`, which is also exported
69+
by the `function_parser` module.
70+
71+
* An error in the function parsing step leads to a detailed error message
72+
(Type and position of error) returned in the `error_msg` output of the
73+
`parse` method.
74+
75+
* An error during function evaluation returns a function value of 0.0 and
76+
an error message returned in the `error_msg` output of the
77+
`evaluate` method.
78+
79+
To check if a `list_of_errors` class contains a message, use the
80+
`has_errors` method.
81+
The `print` method can also be used to print the error messages.
82+
83+
### Function string syntax
84+
85+
Although they have to be passed as array elements of the same declared
86+
length (Fortran restriction), the variable names can be of arbitrary
87+
actual length for the parser. Parsing for variables is case sensitive.
88+
89+
The syntax of the function string is similar to the Fortran convention.
90+
Mathematical Operators recognized are `+,` `-,` `*,` `/,` `**` or alternatively `^,`
91+
whereas symbols for brackets must be `()`.
92+
93+
The function parser recognizes the (single argument) Fortran intrinsic
94+
functions `abs`, `exp`, `log10`, `log`, `sqrt`, `sinh`, `cosh`, `tanh`,
95+
`sin`, `cos`, `tan`, `asin`, `acos`, `atan`. Parsing for intrinsic
96+
functions is case INsensitive.
97+
98+
Operations are evaluated in the correct order:
99+
100+
* `() ` expressions in brackets first
101+
* `-A ` unary minus (or plus)
102+
* `A**B A^B` exponentiation (`A` raised to the power `B`)
103+
* `A*B A/B` multiplication and division
104+
* `A+B A-B` addition and subtraction
105+
106+
The function string can contain integer or real constants. To be recognized
107+
as explicit constants these must conform to the format
108+
109+
`[+|-][nnn][.nnn][e|E|d|D[+|-]nnn]`
110+
111+
where `nnn` means any number of digits. The mantissa must contain at least
112+
one digit before or following an optional decimal point. Valid exponent
113+
identifiers are 'e', 'E', 'd' or 'D'. If they appear they must be followed
114+
by a valid exponent!
115+
116+
### Notes
117+
118+
* The precision of real numbers can be adapted to the calling program by
119+
adjusting the KIND parameter `wp` in the module.
120+
121+
### Credits
122+
123+
* This code is based on Fortran 95 function parser v1.1 by Roland Schmehl
124+
<[email protected]>. The source code is available
125+
from [here](http://fparser.sourceforge.net).
126+
* The function parser concept is based on a C++ class library written by
127+
Juha Nieminen <[email protected]> available from [here](http://warp.povusers.org/FunctionParser/).
128+
* The code has been updated to Fortran 2008 by Jacob Williams. Development
129+
continues on [GitHub](https://github.com/jacobwilliams/fortran_function_parser).

build.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
#
4+
# Simple build script.
5+
#
6+
# Requires: FoBiS and Ford
7+
#
8+
9+
MODCODE='function_parser.f90' # module file name
10+
LIBOUT='libfparser.a' # name of library
11+
DOCDIR='./doc/' # build directory for documentation
12+
SRCDIR='./src/' # library source directory
13+
TESTSRCDIR='./src/tests/' # unit test source directory
14+
BINDIR='./bin/' # build directory for unit tests
15+
LIBDIR='./lib/' # build directory for library
16+
FORDMD='fortran_function_parser.md' # FORD config file name
17+
18+
#compiler flags:
19+
20+
FCOMPILER='gnu' #Set compiler to gfortran
21+
FCOMPILERFLAGS='-c -O2 -std=f2008'
22+
23+
#build using FoBiS:
24+
25+
if hash FoBiS.py 2>/dev/null; then
26+
27+
echo "Building library..."
28+
29+
FoBiS.py build -compiler ${FCOMPILER} -cflags "${FCOMPILERFLAGS}" -dbld ${LIBDIR} -s ${SRCDIR} -dmod ./ -dobj ./ -t ${MODCODE} -o ${LIBOUT} -mklib static -colors
30+
31+
echo "Building test programs..."
32+
33+
FoBiS.py build -compiler ${FCOMPILER} -cflags "${FCOMPILERFLAGS}" -dbld ${BINDIR} -s ${TESTSRCDIR} -dmod ./ -dobj ./ -colors -libs ${LIBDIR}${LIBOUT} --include ${LIBDIR}
34+
35+
else
36+
echo "FoBiS.py not found! Cannot build library. Install using: sudo pip install FoBiS.py"
37+
fi
38+
39+
# build the documentation using FORD:
40+
41+
if hash ford 2>/dev/null; then
42+
43+
echo "Building documentation..."
44+
45+
ford ${FORDMD}
46+
47+
else
48+
echo "Ford not found! Cannot build documentation. Install using: sudo pip install ford"
49+
fi

fortran_function_parser.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
project: fortran_function_parser
2+
project_dir: ./src
3+
output_dir: ./doc
4+
project_github: https://github.com/jacobwilliams/fortran_function_parser
5+
summary: Modern Fortran Function Parser
6+
author: Jacob Williams
7+
github: https://github.com/jacobwilliams
8+
predocmark_alt: >
9+
predocmark: <
10+
docmark_alt:
11+
docmark: !
12+
display: public
13+
protected
14+
source: true
15+
graph: true
16+
extra_mods: iso_fortran_env:https://gcc.gnu.org/onlinedocs/gfortran/ISO_005fFORTRAN_005fENV.html
17+
18+
{!README.md!}

0 commit comments

Comments
 (0)