Neko 1.99.7
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
expression Module Reference

Evaluation of mathematical expressions given as strings in the case file. More...

Data Types

type  expression_t
 A compiled mathematical expression. More...
 
type  parser_t
 State of the recursive descent parser. More...
 
type  token_t
 A single token of an expression. More...
 

Functions/Subroutines

subroutine expression_init (this, str)
 Compile an expression.
 
subroutine parser_free (p)
 Release the memory held by the parser state.
 
subroutine expression_free (this)
 Destructor.
 
subroutine expression_eval (this, res, n, x, y, z, t, dt)
 Evaluate the expression in n points.
 
subroutine, public expression_eval_static (str, res, n, x, y, z, usage)
 Compile an expression and evaluate it in a set of points, in a context where there is no time state.
 
subroutine, public expression_check_finite (str, res, n, usage)
 Abort if an expression did not evaluate to a finite value everywhere.
 
subroutine expr_tokenize (p)
 Split the source string into a stream of tokens.
 
subroutine push_token (p, kind, pos)
 Append a token to the token stream.
 
recursive subroutine parse_expr (p)
 Parse an additive expression.
 
recursive subroutine parse_term (p)
 Parse a multiplicative expression.
 
recursive subroutine parse_factor (p)
 Parse a possibly signed factor. Unary minus binds looser than ^, so -x^2 is -(x^2), as in Fortran.
 
recursive subroutine parse_power (p)
 Parse an exponentiation. Right associative, so 2^3^2 is 2^(3^2).
 
recursive subroutine parse_primary (p)
 Parse a literal, a symbol, a function call or a parenthesised expression.
 
subroutine parse_symbol (p, k)
 Resolve a bare identifier into either a variable opcode or a literal.
 
integer function func_op (name, nargs)
 Look up the opcode of a function.
 
subroutine emit (p, op, val)
 Append an opcode, keeping track of how deep the evaluation stack gets.
 
subroutine expr_error (p, msg, pos)
 Abort with a message pointing at the offending part of the expression.
 
pure logical function is_digit (c)
 True if c is a decimal digit.
 
pure logical function is_alpha (c)
 True if c is a letter.
 

Variables

integer, parameter, public neko_expr_len = 256
 Maximum length of an expression string read from the case file.
 
integer, parameter expr_max_ident = 64
 Maximum length of an identifier appearing in an expression.
 
integer, parameter expr_chunk = 1024
 Number of points evaluated per pass over the opcode stream. Chosen so that the evaluation stack stays in cache irrespective of how many points the caller hands over in one go.
 
integer, parameter op_lit = 1
 
integer, parameter op_x = 2
 
integer, parameter op_y = 3
 
integer, parameter op_z = 4
 
integer, parameter op_t = 5
 
integer, parameter op_dt = 6
 
integer, parameter op_unary = 20
 
integer, parameter op_neg = 20
 
integer, parameter op_sin = 21
 
integer, parameter op_cos = 22
 
integer, parameter op_tan = 23
 
integer, parameter op_asin = 24
 
integer, parameter op_acos = 25
 
integer, parameter op_atan = 26
 
integer, parameter op_sinh = 27
 
integer, parameter op_cosh = 28
 
integer, parameter op_tanh = 29
 
integer, parameter op_exp = 30
 
integer, parameter op_log = 31
 
integer, parameter op_log10 = 32
 
integer, parameter op_sqrt = 33
 
integer, parameter op_abs = 34
 
integer, parameter op_erf = 35
 
integer, parameter op_erfc = 36
 
integer, parameter op_step = 37
 
integer, parameter op_ipow = 38
 
integer, parameter op_binary = 60
 
integer, parameter op_add = 60
 
integer, parameter op_sub = 61
 
integer, parameter op_mul = 62
 
integer, parameter op_div = 63
 
integer, parameter op_pow = 64
 
integer, parameter op_atan2 = 65
 
integer, parameter op_min = 66
 
integer, parameter op_max = 67
 
integer, parameter op_mod = 68
 
integer, parameter tk_end = 0
 
integer, parameter tk_num = 1
 
integer, parameter tk_ident = 2
 
integer, parameter tk_plus = 3
 
integer, parameter tk_minus = 4
 
integer, parameter tk_star = 5
 
integer, parameter tk_slash = 6
 
integer, parameter tk_pow = 7
 
integer, parameter tk_lpar = 8
 
integer, parameter tk_rpar = 9
 
integer, parameter tk_comma = 10
 

Detailed Description

An expression_t is compiled once, from a string such as "6*U_b*y*(H - y)/H^2", into a stream of stack machine opcodes. It can then be evaluated over an arbitrary set of points, with x, y and z bound to the point coordinates and t, dt bound to the time state.

Any identifier which is not a coordinate, a time variable or the constant pi is looked up in the neko_const_registry when the expression is compiled, and folded into a literal. This is the same mechanism as the one used by json_get_or_lookup, so constants declared under case.constants in the case file can be used directly in the expressions.

Function/Subroutine Documentation

◆ emit()

subroutine expression::emit ( type(parser_t), intent(inout p,
integer, intent(in op,
real(kind=rp), intent(in), optional  val 
)
private
Parameters
[in,out]pThe parser state.
[in]opThe opcode to append.
[in]valThe literal operand, only meaningful for OP_LIT.

Definition at line 943 of file expression.f90.

Here is the caller graph for this function:

◆ expr_error()

subroutine expression::expr_error ( type(parser_t), intent(in p,
character(len=*), intent(in msg,
integer, intent(in pos 
)
private
Parameters
[in]pThe parser state.
[in]msgWhat is wrong with the expression.
[in]posPosition in the source string to point at.

Definition at line 973 of file expression.f90.

Here is the caller graph for this function:

◆ expr_tokenize()

subroutine expression::expr_tokenize ( type(parser_t), intent(inout p)
private
Parameters
[in,out]pThe parser state, whose src holds the expression.

Definition at line 514 of file expression.f90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expression_check_finite()

subroutine, public expression::expression_check_finite ( character(len=*), intent(in str,
real(kind=rp), dimension(n), intent(in res,
integer, intent(in n,
character(len=*), intent(in usage 
)

Split out of expression_eval_static so that the boundary conditions, which compile and evaluate their expressions themselves, can apply the same check. A non-finite value typically means a division by zero or the square root of a negative number somewhere.

Parameters
[in]strThe expression, used in the error message.
[in]resThe values to check.
[in]nThe number of values.
[in]usageWhat the expression configures, used in error messages.

Definition at line 492 of file expression.f90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expression_eval()

subroutine expression::expression_eval ( class(expression_t), intent(inout this,
real(kind=rp), dimension(n), intent(inout res,
integer, intent(in n,
real(kind=rp), dimension(n), intent(in x,
real(kind=rp), dimension(n), intent(in y,
real(kind=rp), dimension(n), intent(in z,
real(kind=rp), intent(in), optional  t,
real(kind=rp), intent(in), optional  dt 
)
private

Not thread safe. The evaluation stack is a component of the expression, so two concurrent calls on the same object corrupt each other's intermediate values. A caller inside an OpenMP parallel region must evaluate on one thread only.

Parameters
[in,out]resThe result, one value per point.
[in]nThe number of points.
[in]xThe x-coordinates of the points.
[in]yThe y-coordinates of the points.
[in]zThe z-coordinates of the points.
[in]tThe current time, required if the expression uses t.
[in]dtThe current timestep, required if the expression uses dt.

Definition at line 226 of file expression.f90.

◆ expression_eval_static()

subroutine, public expression::expression_eval_static ( character(len=*), intent(in str,
real(kind=rp), dimension(n), intent(inout res,
integer, intent(in n,
real(kind=rp), dimension(n), intent(in x,
real(kind=rp), dimension(n), intent(in y,
real(kind=rp), dimension(n), intent(in z,
character(len=*), intent(in usage 
)

Convenience routine for initial conditions and other setup-time uses of an expression. The expression is rejected if it is empty, if it depends on time, or if it does not evaluate to a finite value in every point, the latter typically meaning a division by zero or the square root of a negative number somewhere in the domain.

Parameters
[in]strThe expression.
[in,out]resThe result, one value per point.
[in]nThe number of points.
[in]xThe x-coordinates of the points.
[in]yThe y-coordinates of the points.
[in]zThe z-coordinates of the points.
[in]usageWhat the expression configures, used in error messages.

Definition at line 455 of file expression.f90.

Here is the call graph for this function:

◆ expression_free()

subroutine expression::expression_free ( class(expression_t), intent(inout this)
private

Definition at line 201 of file expression.f90.

◆ expression_init()

subroutine expression::expression_init ( class(expression_t), intent(inout this,
character(len=*), intent(in str 
)
Parameters
[in]strThe expression, e.g. "6*U_b*y*(H - y)/H^2".

Definition at line 146 of file expression.f90.

Here is the call graph for this function:

◆ func_op()

integer function expression::func_op ( character(len=*), intent(in name,
integer, intent(in nargs 
)
private
Parameters
[in]nameThe name of the function.
[in]nargsThe number of arguments it was called with.
Returns
The opcode, -1 if the name is unknown and -2 if the name is known but the number of arguments is wrong.

Definition at line 877 of file expression.f90.

Here is the caller graph for this function:

◆ is_alpha()

pure logical function expression::is_alpha ( character(len=1), intent(in c)
private
Parameters
[in]cThe character to test.

Definition at line 997 of file expression.f90.

Here is the caller graph for this function:

◆ is_digit()

pure logical function expression::is_digit ( character(len=1), intent(in c)
private
Parameters
[in]cThe character to test.

Definition at line 987 of file expression.f90.

Here is the caller graph for this function:

◆ parse_expr()

recursive subroutine expression::parse_expr ( type(parser_t), intent(inout p)
private
Parameters
[in,out]pThe parser state.

Definition at line 665 of file expression.f90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_factor()

recursive subroutine expression::parse_factor ( type(parser_t), intent(inout p)
private
Parameters
[in,out]pThe parser state.

Definition at line 710 of file expression.f90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_power()

recursive subroutine expression::parse_power ( type(parser_t), intent(inout p)
private
Parameters
[in,out]pThe parser state.

Definition at line 736 of file expression.f90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_primary()

recursive subroutine expression::parse_primary ( type(parser_t), intent(inout p)
private
Parameters
[in,out]pThe parser state.

Definition at line 769 of file expression.f90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_symbol()

subroutine expression::parse_symbol ( type(parser_t), intent(inout p,
integer, intent(in k 
)
private

Coordinates and time variables take precedence over anything else. Everything that is left is looked up in the neko_const_registry and folded into a literal, so the value it had when the expression was compiled is the value that is used.

Parameters
[in,out]pThe parser state.
[in]kIndex of the identifier token to resolve.

Definition at line 830 of file expression.f90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_term()

recursive subroutine expression::parse_term ( type(parser_t), intent(inout p)
private
Parameters
[in,out]pThe parser state.

Definition at line 687 of file expression.f90.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parser_free()

subroutine expression::parser_free ( type(parser_t), intent(inout p)
private
Parameters
[in,out]pThe parser state.

Definition at line 183 of file expression.f90.

Here is the caller graph for this function:

◆ push_token()

subroutine expression::push_token ( type(parser_t), intent(inout p,
integer, intent(in kind,
integer, intent(in pos 
)
private
Parameters
[in,out]pThe parser state.
[in]kindThe kind of the token, one of the TK_ constants.
[in]posPosition of the token in the source string.

Definition at line 642 of file expression.f90.

Here is the caller graph for this function:

Variable Documentation

◆ expr_chunk

integer, parameter expression::expr_chunk = 1024
private

Definition at line 63 of file expression.f90.

◆ expr_max_ident

integer, parameter expression::expr_max_ident = 64
private

Definition at line 58 of file expression.f90.

◆ neko_expr_len

integer, parameter, public expression::neko_expr_len = 256

Definition at line 55 of file expression.f90.

◆ op_abs

integer, parameter expression::op_abs = 34
private

Definition at line 74 of file expression.f90.

◆ op_acos

integer, parameter expression::op_acos = 25
private

Definition at line 74 of file expression.f90.

◆ op_add

integer, parameter expression::op_add = 60
private

Definition at line 81 of file expression.f90.

◆ op_asin

integer, parameter expression::op_asin = 24
private

Definition at line 74 of file expression.f90.

◆ op_atan

integer, parameter expression::op_atan = 26
private

Definition at line 74 of file expression.f90.

◆ op_atan2

integer, parameter expression::op_atan2 = 65
private

Definition at line 81 of file expression.f90.

◆ op_binary

integer, parameter expression::op_binary = 60
private

Definition at line 80 of file expression.f90.

◆ op_cos

integer, parameter expression::op_cos = 22
private

Definition at line 74 of file expression.f90.

◆ op_cosh

integer, parameter expression::op_cosh = 28
private

Definition at line 74 of file expression.f90.

◆ op_div

integer, parameter expression::op_div = 63
private

Definition at line 81 of file expression.f90.

◆ op_dt

integer, parameter expression::op_dt = 6
private

Definition at line 70 of file expression.f90.

◆ op_erf

integer, parameter expression::op_erf = 35
private

Definition at line 74 of file expression.f90.

◆ op_erfc

integer, parameter expression::op_erfc = 36
private

Definition at line 74 of file expression.f90.

◆ op_exp

integer, parameter expression::op_exp = 30
private

Definition at line 74 of file expression.f90.

◆ op_ipow

integer, parameter expression::op_ipow = 38
private

Definition at line 74 of file expression.f90.

◆ op_lit

integer, parameter expression::op_lit = 1
private

Definition at line 70 of file expression.f90.

◆ op_log

integer, parameter expression::op_log = 31
private

Definition at line 74 of file expression.f90.

◆ op_log10

integer, parameter expression::op_log10 = 32
private

Definition at line 74 of file expression.f90.

◆ op_max

integer, parameter expression::op_max = 67
private

Definition at line 81 of file expression.f90.

◆ op_min

integer, parameter expression::op_min = 66
private

Definition at line 81 of file expression.f90.

◆ op_mod

integer, parameter expression::op_mod = 68
private

Definition at line 81 of file expression.f90.

◆ op_mul

integer, parameter expression::op_mul = 62
private

Definition at line 81 of file expression.f90.

◆ op_neg

integer, parameter expression::op_neg = 20
private

Definition at line 74 of file expression.f90.

◆ op_pow

integer, parameter expression::op_pow = 64
private

Definition at line 81 of file expression.f90.

◆ op_sin

integer, parameter expression::op_sin = 21
private

Definition at line 74 of file expression.f90.

◆ op_sinh

integer, parameter expression::op_sinh = 27
private

Definition at line 74 of file expression.f90.

◆ op_sqrt

integer, parameter expression::op_sqrt = 33
private

Definition at line 74 of file expression.f90.

◆ op_step

integer, parameter expression::op_step = 37
private

Definition at line 74 of file expression.f90.

◆ op_sub

integer, parameter expression::op_sub = 61
private

Definition at line 81 of file expression.f90.

◆ op_t

integer, parameter expression::op_t = 5
private

Definition at line 70 of file expression.f90.

◆ op_tan

integer, parameter expression::op_tan = 23
private

Definition at line 74 of file expression.f90.

◆ op_tanh

integer, parameter expression::op_tanh = 29
private

Definition at line 74 of file expression.f90.

◆ op_unary

integer, parameter expression::op_unary = 20
private

Definition at line 73 of file expression.f90.

◆ op_x

integer, parameter expression::op_x = 2
private

Definition at line 70 of file expression.f90.

◆ op_y

integer, parameter expression::op_y = 3
private

Definition at line 70 of file expression.f90.

◆ op_z

integer, parameter expression::op_z = 4
private

Definition at line 70 of file expression.f90.

◆ tk_comma

integer, parameter expression::tk_comma = 10
private

Definition at line 87 of file expression.f90.

◆ tk_end

integer, parameter expression::tk_end = 0
private

Definition at line 87 of file expression.f90.

◆ tk_ident

integer, parameter expression::tk_ident = 2
private

Definition at line 87 of file expression.f90.

◆ tk_lpar

integer, parameter expression::tk_lpar = 8
private

Definition at line 87 of file expression.f90.

◆ tk_minus

integer, parameter expression::tk_minus = 4
private

Definition at line 87 of file expression.f90.

◆ tk_num

integer, parameter expression::tk_num = 1
private

Definition at line 87 of file expression.f90.

◆ tk_plus

integer, parameter expression::tk_plus = 3
private

Definition at line 87 of file expression.f90.

◆ tk_pow

integer, parameter expression::tk_pow = 7
private

Definition at line 87 of file expression.f90.

◆ tk_rpar

integer, parameter expression::tk_rpar = 9
private

Definition at line 87 of file expression.f90.

◆ tk_slash

integer, parameter expression::tk_slash = 6
private

Definition at line 87 of file expression.f90.

◆ tk_star

integer, parameter expression::tk_star = 5
private

Definition at line 87 of file expression.f90.