parser_module Module Reference

Subroutines for equation parsing in case of an analytic trajectory or luminosity mode. More...

Functions/Subroutines

logical function, private is_digit (str_in)
 Function to decide whether a character is a number or not (i.e., 0-9). More...
 
logical function, private is_operator (str_in)
 Function to decide whether a character is a mathematical operator or not. Functions (as e.g., sin) are not defined here! More...
 
logical function, private is_dot_operator (str_in)
 Function to decide whether a character is "*" or "/". More...
 
logical function, private is_line_operator (str_in)
 Function to decide whether a character is a "+" or "-". More...
 
logical function, private is_power_operator (str_in)
 Function to decide whether a character is a "^". More...
 
logical function, private is_separator (str_in)
 Function to decide whether a character can be associated with a number. More...
 
real(r_kind) function, private operation (number_1, number_2, str_in)
 Function to perform a simple mathematical operation. More...
 
real(r_kind) function, private eval_function (number, str_in)
 Function to perform a function operations. More...
 
subroutine, private make_pars_str_consistent
 Subroutine to check the parsing string for consistency and correct it. More...
 
real(r_kind) function, private common_variables (inp_str)
 Function to define constants. More...
 
subroutine, private store_digits_operator
 Store mathematical operators (+-*/) and numbers in arrays. More...
 
subroutine, private get_simple_result
 Evaluate a simple string expression, without brackets and functions. More...
 
subroutine, private evaluate
 Evaluate a complex string expression. More...
 
real(r_kind) function, public parse_string (input_string, var_value)
 Takes a string and evaluates the expression. More...
 
subroutine, public find_start_value (input_string, eq_value, initial_guess, converged, result)
 Finds a value of the variable for a given y-value. More...
 

Variables

character(max_fname_len), private parsing_string
 Current substring to parse. More...
 
character(max_fname_len), private complete_string
 Complete input string. More...
 
integer, private operator_count
 
integer, private number_count
 
character(1), dimension(:), allocatable, private operators
 Array containing the operations of the equation as a character (e.g., ["*","+"]) More...
 
real(r_kind), dimension(:), allocatable, private numbers
 Array containing the numbers of the equation (e.g., [1, 2, 3]) More...
 
real(r_kind), private variable_value
 Input variable, set by parse_string. More...
 
character(1), parameter, private variable_name ='x'
 Name of the variable that is replaced by parse_string. More...
 

Detailed Description

Subroutines for equation parsing in case of an analytic trajectory or luminosity mode.

The subroutine can calculate equations from a string, e.g., "5.1+3d-2*(2-1)*sin(pi)/x". A variable name (in the upper example "x") can be given, which will be filled with a value. Everything in this function is private with the exception of the function "parse_string" which serves as a interface to parse the given string. The return value of this function is a real (i.e., not a string).

Author
Moritz Reichert
Date
20.12.20

Function/Subroutine Documentation

◆ common_variables()

real(r_kind) function, private parser_module::common_variables ( character(len=*), intent(in)  inp_str)
private

Function to define constants.

This function translates names of constants to numbers (real). For example, "pi" is translated to 3.1415. Define new constants (names and values) in this function.

Example

b = common_variables("pi")

After this, will be 3.141592653589793

Note
New variables can be defined here.
Author
Moritz Reichert
Date
20.12.20
Parameters
[in]inp_strInput character, defining the constant
Returns
A value of a given constant

Definition at line 441 of file parser_module.f90.

◆ eval_function()

real(r_kind) function, private parser_module::eval_function ( real(r_kind), intent(in)  number,
character(max_fname_len), intent(in)  str_in 
)
private

Function to perform a function operations.

Depending on the input it will calculate, e.g., the square root (sqrt) or the sinus (sin). To add a function to the functionality of the parser, add the translation in this function here.

Example

b = eval_function(4, "sqrt")

b will be 2.

Note
New parsing functions can be defined here.
Author
Moritz Reichert
Date
20.12.20
Parameters
[in]str_inName of the function
[in]numberinput to the function
Returns
function(number), where function is defined by the input string.

Definition at line 314 of file parser_module.f90.

Here is the call graph for this function:

◆ evaluate()

subroutine, private parser_module::evaluate
private

Evaluate a complex string expression.

Evaluate expressions with brackets ( i.e., "(" and ")" ) and function names (i.e., "sin(5)" ).

Example

parsing_string = "2+sin(pi)-5*(1+1)"
call evaluate()

After this, the array numbers will have -8 on its first entry.

Author
Moritz Reichert
Date
20.12.20

Definition at line 713 of file parser_module.f90.

Here is the call graph for this function:

◆ find_start_value()

subroutine, public parser_module::find_start_value ( character(300), intent(in)  input_string,
real(r_kind), intent(in)  eq_value,
real(r_kind), intent(in)  initial_guess,
logical, intent(out)  converged,
real(r_kind), intent(out)  result 
)

Finds a value of the variable for a given y-value.

This function solves an equation, i.e., finds the value of variable "x" for expressions as, for example, "3*x+2+x^2 = 10" returns 1.701562. This works with a newton raphson to find the root of f(x) - 10.

Example

input_string = "3*x+2+x^2"
eq_value = 10
initial_guess = 0.0
converged = .false.
result = 0.0
call find_start_value(input_string,eq_value,initial_guess,converged,result)

After this, result will be 1.701562 and converged will be .True.

Author
Moritz Reichert
Date
20.12.20
Parameters
[in]input_stringString to parse
[in]eq_valueValue of the equation
[in]initial_guessInitial guess for the variable
[out]convergedFlag that indicates a success
[out]resultTime at which the expression is equal to eq_value.

Definition at line 855 of file parser_module.f90.

Here is the call graph for this function:

◆ get_simple_result()

subroutine, private parser_module::get_simple_result
private

Evaluate a simple string expression, without brackets and functions.

This subroutine is able to evaluate simply expressions without brackets or functions. Only +- * / and ^ is allowed here. In addition a variable name or constant names may be given. For example "2+3-1" will store the result (4) in the first entry of the "numbers" array.

Example

parsing_string = "0-2-3^2"
call get_simple_result()

After this, the array numbers will have -11 on its first entry.

Author
Moritz Reichert
Date
20.12.20

Definition at line 594 of file parser_module.f90.

Here is the call graph for this function:

◆ is_digit()

logical function, private parser_module::is_digit ( character(1), intent(in)  str_in)
private

Function to decide whether a character is a number or not (i.e., 0-9).

Example

b = is_digit("5")
c = is_digit("a")

b will be .True. and c will be .False.

Author
Moritz Reichert
Date
20.12.20
Parameters
[in]str_inInput character
Returns
True in case of a digit and False in case of another character

Definition at line 73 of file parser_module.f90.

◆ is_dot_operator()

logical function, private parser_module::is_dot_operator ( character(1), intent(in)  str_in)
private

Function to decide whether a character is "*" or "/".

Example

b = is_dot_operator("+")
c = is_dot_operator("*")

b will be .False. and c will be .True.

See also
is_line_operator, is_power_operator, is_separator, is_operator
Author
Moritz Reichert
Date
20.12.20
Parameters
[in]str_inInput character
Returns
True in case of a * or / input string and False otherwise

Definition at line 141 of file parser_module.f90.

◆ is_line_operator()

logical function, private parser_module::is_line_operator ( character(1), intent(in)  str_in)
private

Function to decide whether a character is a "+" or "-".

Example

b = is_line_operator("+")
c = is_line_operator("*")

b will be .True. and c will be .False.

See also
is_dot_operator, is_power_operator, is_separator, is_operator
Author
Moritz Reichert
Date
20.12.20
Parameters
[in]str_inInput character
Returns
True in case of a + or - input string and False otherwise

Definition at line 172 of file parser_module.f90.

◆ is_operator()

logical function, private parser_module::is_operator ( character(1), intent(in)  str_in)
private

Function to decide whether a character is a mathematical operator or not. Functions (as e.g., sin) are not defined here!

Example

b = is_operator("+")
c = is_operator("5")

b will be .True. and c will be .False.

See also
is_line_operator, is_power_operator, is_separator, is_dot_operator
Author
Moritz Reichert
Date
20.12.20
Parameters
[in]str_inInput character
Returns
True in case of an operator (+,-,*,/,^) and False otherwise

Definition at line 104 of file parser_module.f90.

◆ is_power_operator()

logical function, private parser_module::is_power_operator ( character(1), intent(in)  str_in)
private

Function to decide whether a character is a "^".

Example

b = is_power_operator("^")
c = is_power_operator("-")

b will be .True. and c will be .False.

See also
is_dot_operator, is_line_operator, is_separator, is_operator
Author
Moritz Reichert
Date
20.12.20
Parameters
[in]str_inInput character
Returns
True in case of a ^ input string and False otherwise

Definition at line 203 of file parser_module.f90.

◆ is_separator()

logical function, private parser_module::is_separator ( character(1), intent(in)  str_in)
private

Function to decide whether a character can be associated with a number.

Allowed characters are ".", "d", and "e" (for scientific format of a number).

Example

b = is_separator(",")
c = is_separator("-")

b will be .True. and c will be .False.

See also
is_dot_operator, is_line_operator, is_separator, is_operator, is_power_operator
Author
Moritz Reichert
Date
20.12.20
Parameters
[in]str_inInput character
Returns
True in case of a ",", "d", and "e" input string and False otherwise

Definition at line 235 of file parser_module.f90.

◆ make_pars_str_consistent()

subroutine, private parser_module::make_pars_str_consistent
private

Subroutine to check the parsing string for consistency and correct it.

This subroutine will convert all characters to lower case characters (e.g., 4E-1 -> 4e-1) and will add a leading "0" in case the string starts with a mathematical operator (i.e., + or -, e.g., "-3+5" -> "0-3+5"). Furthermore, it checks for "+-" (and "++", "--" and all combinations) and will correct it to "-". Additionally "**" is corrected to "^", in this way the power can be written by both (**, and ^).

Example

parsing_string = "-2+-3**2"
call make_pars_str_consistent()

After this, parsing_string will be "0-2-3^2"

Returns
A corrected string that can be parsed by other functions
Author
Moritz Reichert
Date
20.12.20

Definition at line 372 of file parser_module.f90.

◆ operation()

real(r_kind) function, private parser_module::operation ( real(r_kind number_1,
real(r_kind number_2,
character(1), intent(in)  str_in 
)
private

Function to perform a simple mathematical operation.

Depending on the input character, the function performs "+". "-", "*", "/", or "^". In case of a different character, an error is raised.

Example

b = operation(10.3, 2.7, "+")

b will be 13.

Author
Moritz Reichert
Date
20.12.20
Parameters
[in]str_inInput character (containing "+","-","*","/","^".)
Returns
Result of a certain operation of number_1 and number_2, determined by the input string.
Parameters
number_2Input numbers on which the operation is performed

Definition at line 269 of file parser_module.f90.

Here is the call graph for this function:

◆ parse_string()

real(r_kind) function, public parser_module::parse_string ( character(300), intent(in)  input_string,
real(r_kind), intent(in)  var_value 
)

Takes a string and evaluates the expression.

This function is accessible to the outside of the module. It serves as interface between other modules and this one. Also replaces variable_name with variable_value.

Example

b = parse_string("-8+3*(2-1)-x",10)

After this, b will be -15.

Author
Moritz Reichert
Date
20.12.20
Parameters
[in]input_stringString to parse
[in]var_valueValue of the variable that will be stored in variable_value
Returns
Value of the evaluated expression

Definition at line 806 of file parser_module.f90.

Here is the call graph for this function:

◆ store_digits_operator()

subroutine, private parser_module::store_digits_operator
private

Store mathematical operators (+-*/) and numbers in arrays.

The result of this subroutine are two arrays, one containing all operators (e.g., for "3+2*2+1-1": ['+','*','+','-']) and one array containing all numbers (as real type, [3,2,2,1,1]).

Example

parsing_string = "0-2-3^2"
call store_digits_operator()

After this, the array operators will contain ("-","-","^"), and the array numbers will contain (0, 2, 3, 2).

Author
Moritz Reichert
Date
20.12.20

Definition at line 480 of file parser_module.f90.

Here is the call graph for this function:

Variable Documentation

◆ complete_string

character(max_fname_len), private parser_module::complete_string
private

Complete input string.

Definition at line 29 of file parser_module.f90.

◆ number_count

integer, private parser_module::number_count
private

Definition at line 32 of file parser_module.f90.

◆ numbers

real(r_kind), dimension(:), allocatable, private parser_module::numbers
private

Array containing the numbers of the equation (e.g., [1, 2, 3])

Definition at line 34 of file parser_module.f90.

◆ operator_count

integer, private parser_module::operator_count
private

Definition at line 32 of file parser_module.f90.

◆ operators

character(1), dimension(:), allocatable, private parser_module::operators
private

Array containing the operations of the equation as a character (e.g., ["*","+"])

Definition at line 33 of file parser_module.f90.

◆ parsing_string

character(max_fname_len), private parser_module::parsing_string
private

Current substring to parse.

Definition at line 28 of file parser_module.f90.

◆ variable_name

character(1), parameter, private parser_module::variable_name ='x'
private

Name of the variable that is replaced by parse_string.

Definition at line 38 of file parser_module.f90.

◆ variable_value

real(r_kind), private parser_module::variable_value
private

Input variable, set by parse_string.

Definition at line 37 of file parser_module.f90.