Creates and parses an input section to define a generic function.
More...
|
| subroutine, public | create_function_section (gen_section, section_name, func_name, description, repeats, do_deriv, input_variables) |
| | Create the input section for a generic function.
|
| subroutine, public | parse_function_section (gen_section, func_name, xfunction, parameters, values, var_values, i_rep_sec, input_variables, dx, error_limit) |
| | Reads from the input structure all information for generic functions.
|
Creates and parses an input section to define a generic function.
- Note
- This module handles the input definition and retrieval, while the task of actually parsing and evaluating a function is handled by common/fparser module. The general workflow is:
- In the module that creates input sections, use the subroutine to add the subsection &GENERIC_FUNCTION with keywords FUNCTION, PARAMETERS, UNITS, VALUES and (optionally) VARIABLES. If a list of symbols has been specified (like input_variables=["X", "Y", "Z"]) then they will be reserved as the variables and the VARIABLES keyword will not appear. If no such list is given then the VARIABLES keyword requesting input of symbol(s) for the variable(s) will appear, with no restriction on the number of symbols. If in addition to the function itself, the numerical derivatives are also needed, set do_deriv=.TRUE. which will create two additional keywords, DX and ERROR_LIMIT. NULLIFY(subsection) CALL create_function_section(subsection, "GENERIC_FUNCTION", "FUNCTION", & "This subsection defines a generic function.") ! Add extra keywords here if needed ! NULLIFY (keyword) ! CALL keyword_create(keyword, LOCATION, ...) ! CALL section_add_keyword(subsection, keyword) ! CALL keyword_release(keyword) ! ... ! Done adding extra keywords CALL section_add_subsection(section, subsection) CALL section_release(subsection)
- Define a type that stores information about the function. TYPE my_func_type CHARACTER(LEN=default_path_length) :: function = "" CHARACTER(LEN=default_string_length), DIMENSION(:), ALLOCATABLE :: parameters REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: values ! To evaluate numerical derivatives also store DX and ERROR_LIMIT REAL(KIND=dp) :: dx = 0.0_dp, lerr = 0.0_dp END TYPE my_func_type
- In the module that parses input, use the subroutine to get and save the definitions. The usage of input_variables should be consistent with above. Note that the variables and the parameters are all saved in my_funcparameters and my_funcvalues; with n_var variables and n_par parameters, both my_funcparameters and my_funcvalues will have a dimension of (n_var + n_par), variables first. To evaluate numerical derivatives, also pass dx=my_funcdx and error_limit=my_funclerr. TYPE(my_func_type), POINTER :: my_func NULLIFY (my_func) CALL parse_function_section(gen_section, "FUNCTION", my_funcfunction, & my_funcparameters, my_funcvalues)
- Use the subroutines from the fparser module to evaluate the function or its derivative. CALL initf(1) CALL parsef(1, TRIM(my_funcfunction), my_funcparameters) ! Substitute new values for the variables if needed ! my_funcvalues(1) = ... ! Done substituting result = evalf(1, my_funcvalues) CPASSERT(EvalErrType <= 0) ! To evaluate numerical derivative with respect to the k-th element of my_funcvalues: ! derivative = evalfd(1, k, my_funcvalues, my_funcdx, err) ! IF (ABS(err) > my_funclerr) THEN ! CALL cp_warn(LOCATION, & ! "Error in computing numerical derivatives larger than allowed") ! END IF ! Done evaluating derivative CALL finalizef()
- History
- 10.2026 created
- Author
- Zilong He
◆ create_function_section()
| subroutine, public generic_function_input::create_function_section |
( |
type(section_type), pointer | gen_section, |
|
|
character(len=*), intent(in) | section_name, |
|
|
character(len=*), intent(in) | func_name, |
|
|
character(len=*), intent(in) | description, |
|
|
logical, intent(in), optional | repeats, |
|
|
logical, intent(in), optional | do_deriv, |
|
|
character(len=*), dimension(:), intent(in), optional | input_variables ) |
Create the input section for a generic function.
- Parameters
-
| gen_section | the section to be created for the generic function |
| section_name | the name of the subsection for the generic function |
| func_name | the name of the keyword for the function |
| description | a custom description for the subsection |
| repeats | whether the section can be repeated |
| do_deriv | whether keywords for evaluating numerical derivative are needed |
| input_variables | a list of symbols that will be parsed as variables |
- History
- 10.2026 created from scratch matching argument names with parse_function_section
Definition at line 128 of file generic_function_input.F.
◆ parse_function_section()
| subroutine, public generic_function_input::parse_function_section |
( |
type(section_vals_type), pointer | gen_section, |
|
|
character(len=*), intent(in) | func_name, |
|
|
character(len=default_path_length), intent(out) | xfunction, |
|
|
character(len=default_string_length), dimension(:), intent(out), allocatable | parameters, |
|
|
real(kind=dp), dimension(:), intent(out), allocatable | values, |
|
|
real(kind=dp), dimension(:), intent(in), optional | var_values, |
|
|
integer, intent(in), optional | i_rep_sec, |
|
|
character(len=*), dimension(:), intent(in), optional | input_variables, |
|
|
real(kind=dp), intent(out), optional | dx, |
|
|
real(kind=dp), intent(out), optional | error_limit ) |
Reads from the input structure all information for generic functions.
- Parameters
-
| gen_section | the name of the section for the generic function |
| func_name | the name of the keyword for the function |
| xfunction | the function |
| parameters | the list of symbols for the parameters in the function |
| values | the list of values for the parameters in the function |
| var_values | the values of the variables in the function |
| i_rep_sec | the index of the section if it may be repeated |
| input_variables | the list of symbols for the variables in the function |
| dx | the dx for evaluating numerical derivatives of the function |
| error_limit | the error limit for evaluating numerical derivatives of the function |
- History
- 10.2026 created by migrating the get_generic_info in force_fields_util.F to here
Definition at line 260 of file generic_function_input.F.