|
| 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). |
0 commit comments