opencmp.config_functions package

Submodules

opencmp.config_functions.base_config_functions module

class opencmp.config_functions.base_config_functions.ConfigFunctions(config_rel_path, import_dir, mesh, t_param, new_variables=[{}])[source]

Bases: object

Class to hold any functions from the config files.

_find_rel_path_for_file(file_name)[source]

Function to check if a file exists, returning a relative path to it.

Parameters

file_name (str) – The name of the file.

Return type

str

Returns

The path to the file, relative to the run directory.

re_parse(param_dict, re_parse_dict, t_param, updated_variables, mesh)[source]

Iterates through a parameter dictionary and re-parses any expressions containing model variables to use the updated values of those variables.

Parameters
  • param_dict (Dict[str, Union[str, float, CoefficientFunction, GridFunction, list]]) – The parameter dictionary to update.

  • re_parse_dict (Dict[str, str]) – Dictionary containing only the parameters that need to be re-parsed and their string expressions.

  • t_param (Optional[List[Parameter], None]) – List of parameters representing the current time and previous time steps. If None, the re-parsed values have no possible time dependence and one single value is returned instead of a list of values corresponding to the re-parsed value at each time step.

  • mesh (Mesh) – Mesh used by the model

  • updated_variables (List[Dict[str, Union[int, str, float, CoefficientFunction, GridFunction]]]) – List of dictionaries containing any new model variables and their values at each time step used in the time discretization scheme.

Return type

Dict

Returns

The updated parameter dictionary.

opencmp.config_functions.boundary_conditions module

class opencmp.config_functions.boundary_conditions.BCFunctions(config_rel_path, import_dir, mesh, t_param=[<ngsolve.fem.Parameter object>], new_variables=[{}])[source]

Bases: opencmp.config_functions.base_config_functions.ConfigFunctions

Class to hold the boundary condition functions.

load_bc_gridfunctions(bc_dict, fes, model_components)[source]

Function to load any saved gridfunctions that should be used to specify BCs.

Parameters
  • bc_dict (Dict) – Dictionary specifying the BCs and their mesh markers for each variable.

  • fes (FESpace) – The finite element space of the model.

  • model_components (Dict[str, Optional[int, None]]) – Maps between variable names and their component in the finite element space.

Return type

Dict

Returns

The input dict now, where appropriate, holding gridfunctions in place of strings holding the paths to those gridfunctions.

set_boundary_conditions(BC)[source]

Function to load the BC dictionary and to generate the list of Dirichlet BC markers for use when constructing the finite element space.

Parameters

BC (Union[List[str], Dict[str, str]]) – The model’s BC list (contains information about which types of BC are allowed).

Returns

  • BC_full: The full BC dictionary.

  • dirichlet_names: String containing all markers for Dirichlet BCs in NGSolve’s required format.

Return type

Tuple[Dict, Dict]

set_dirichlet_boundary_conditions(bc_dict, mesh, gfu, model_components)[source]

Function to load the strongly imposed Dirichlet BCs in order to apply them to the solution gridfunction.

Parameters
  • bc_dict (Dict[str, Dict[str, Dict[str, Union[List[CoefficientFunction], List[list], List[float], List[GridFunction]]]]]) – Dictionary specifying the BCs and their mesh markers for each variable. The BCs are all given as floats, gridfunctions, or coefficientfunctions.

  • mesh (Mesh) – The model’s mesh.

  • gfu (GridFunction) – A gridfunction constructed on the model’s finite element space.

  • model_components (Dict) – The model’s model_components dictionary.

Return type

Dict[str, List[CoefficientFunction]]

Returns

Dictionary of coefficientfunctions used to set the strongly imposed Dirichlet BCs. Each dictionary entry holds a list, which gives the coefficientfunction for each time step in the time discretization scheme.

update_boundary_conditions(t_param, updated_variables, mesh)[source]

Function to update the boundary conditions with new values of the model_variables.

Parameters
  • t_param (List[Parameter]) – List of parameters representing the current time and previous time steps.

  • updated_variables (List[Dict[str, Union[float, CoefficientFunction, GridFunction]]]) – List of dictionaries containing any new model variables and their values at each time step used in the time discretization scheme.

  • mesh (Mesh) – Mesh used by the model

Return type

None

opencmp.config_functions.controller_functions module

class opencmp.config_functions.controller_functions.ControllerFunctions(config_rel_path, import_dir, mesh, t_param, new_variables=[{}])[source]

Bases: opencmp.config_functions.base_config_functions.ConfigFunctions

Class to process values for the controller

get_control_variables()[source]

Function to get the control variables in a structured format.

Returns

The control variables as a set of nested lists [[name_1, pos_1, val_1, index_1], […], …]

Return type

~

get_manipulated_variables()[source]

Function to get the manipulated variables in a structured format.

Returns

The manipulated variables as a set of nested lists [[type_1, var_1, loc_1], […], …]

Return type

~

opencmp.config_functions.controller_functions._convert_dict_entries_to_list(_dict)[source]

Function to convert the top level entries in a dictionary into a list.

Parameters

_dict (Dict[str, Any]) – The dictionary to work on

Returns

The dictionary after all top level entries have been converted to lists

Return type

~

opencmp.config_functions.expanded_config_parser module

class opencmp.config_functions.expanded_config_parser.ConfigParser(config_file_path)[source]

Bases: configparser.ConfigParser

A ConfigParser extended to have several useful functions added to it.

_load_param(config_keys, val_type, quiet=False)[source]

Loads a parameter specified in the given config file. Can also loads lists of the specified parameter type.

Parameters
  • config_keys (List[str]) – The keys that specify the location of the parameter in the config file (header, subheader…).

  • val_type (Type[~T]) – The expected parameter type.

  • quiet (bool) – If True suppresses the warning about the default value being used for a parameter, and about not converting a number to a string.

Return type

List[~T]

Returns

The parameter value or a list of the parameter’s values.

get_dict(config_keys, import_dir, mesh=None, t_param=None, quiet=False, all_str=False)[source]

Function to load a one level dictionary of parameters from the config file.

Use instead of get_one_level_dict if the dictionary is denoted by a “->” separator.

Parameters
  • config_keys (List[str]) – The keys needed to access the parameters from the config file.

  • import_dir (str) – The path to the main run directory containing the file from which to import any Python functions.

  • mesh (Optional[Mesh, None]) – Mesh for the model

  • t_param (Optional[List[Parameter], None]) – List of parameters representing the current time and previous time steps. If None, the re-parsed values have no possible time dependence and one single value is returned instead of a list of values corresponding to the re-parsed value at each time step.

  • quiet (bool) – If True suppresses the warning about the default value being used for a parameter.

  • all_str (bool) – If True, don’t bother parsing any values, just load them as strings.

Return type

Dict[str, Any]

Returns

Dictionary of the parsed parameters from the config file.

get_item(config_keys, val_type, quiet=False)[source]

Function to load a parameter from the config file.

Parameters
  • config_keys (List[str]) – The keys needed to access the parameter from the config file.

  • val_type (Type[~T]) – The type that the parameter is supposed to be.

  • quiet (bool) – If True suppresses the warning about the default value being used for a parameter.

Return type

~T

Returns

The parameter from the config file in the type specified.

get_list(config_keys, val_type, quiet=False)[source]

Function to load a list of parameters from the config file.

Parameters
  • config_keys (List[str]) – The keys needed to access the parameters from the config file.

  • val_type (Type[~T]) – The type that each parameter is supposed to be.

  • quiet (bool) – If True suppresses the warning about the default value being used for a parameter.

Return type

List[~T]

Returns

List of the parameters from the config file in the type specified.

get_one_level_dict(config_section, import_dir, mesh, t_param=None, new_variables=[{}], all_str=False)[source]

Function to load parameters from a config file into a single-level dictionary.

The values in the lowest level dictionary are either parsed into Python code or kept as strings if they represent paths to .sol files. All keys are kept as strings.

Ex: model_params_dict = {'density': float,
                         'viscosity': float,
                         'source': CoefficientFunction}
Parameters
  • config_section (str) – The section of the config file to load parameters from.

  • import_dir (str) – The path to the main run directory containing the file from which to import any Python functions.

  • t_param (Optional[List[Parameter], None]) – List of parameters representing the current time and previous time steps. If None, the re-parsed values have no possible time dependence and one single value is returned instead of a list of values corresponding to the re-parsed value at each time step.

  • mesh (Mesh) – Mesh used by the model

  • new_variables (List[Dict[str, Union[int, str, float, CoefficientFunction, GridFunction, None]]]) – List of dictionaries containing any new model variables and their values at each time step used in the time discretization scheme.

  • all_str (bool) – If True, don’t bother parsing any values, just load them as strings.

Return type

Tuple[Dict, Dict]

Returns

Tuple[Dict, Dict]
  • dict_one: Dictionary containing the config file values.

  • re_parse_dict: Dictionary containing only parameter values that may need to be re-parsed in the future.

get_three_level_dict(import_dir, mesh, t_param=None, new_variables=[{}], ignore=[])[source]

Function to load parameters from a config file into a three-level dictionary.

The values in the lowest level dictionary are either parsed into Python code or kept as strings if they represent paths to .sol files. All other keys and values are kept as strings.

Ex: bc_dict = {'dirichlet': {'u': {marker1: CoefficientFunction,
                                   marker2: CoefficientFunction},
                             'p': {marker3: CoefficientFunction}
                             },
               'neumann':   {'p': {marker4: CoefficientFunction}
                             }
               }
Parameters
  • import_dir (str) – The path to the main run directory containing the file from which to import any Python functions.

  • t_param (Optional[List[Parameter], None]) – List of parameters representing the current time and previous time steps. If None, the re-parsed values have no possible time dependence and one single value is returned instead of a list of values corresponding to the re-parsed value at each time step.

  • new_variables (List[Dict[str, Union[int, str, float, CoefficientFunction, GridFunction, None]]]) – List of dictionaries containing any new model variables and their values at each time step used in the time discretization scheme.

  • ignore – List of section headers to ignore if only part of the config file should be read.

Returns

  • dict_one: Dictionary containing the config file values.

  • re_parse_dict: Dictionary containing only parameter values that may need to be re-parsed in the future.

Return type

Tuple[Dict, Dict]

get_two_level_dict(config_section, import_dir, mesh, t_param=None, new_variables=[{}])[source]

Function to load parameters from a config file into a two-level dictionary.

The values in the lowest level dictionary are either parsed into Python code or kept as strings if they represent paths to .sol files. All other keys and values are kept as strings.

Ex: model_functions_dict = {'source': {'c1': CoefficientFunction,
                                       'c2': CoefficientFunction}
                            }
Parameters
  • config_section (str) – The section of the config file to load parameters from.

  • import_dir (str) – The path to the main run directory containing the file from which to import any Python functions.

  • t_param (Optional[List[Parameter], None]) – List of parameters representing the current time and previous time steps. If None, the re-parsed values have no possible time dependence and one single value is returned instead of a list of values corresponding to the re-parsed value at each time step.

  • new_variables (List[Dict[str, Union[int, str, float, CoefficientFunction, GridFunction, None]]]) – List of dictionaries containing any new model variables and their values at each time step used in the time discretization scheme.

Return type

Tuple[Dict, Dict]

Returns

Tuple[Dict, Dict]
  • dict_one: Dictionary containing the config file values.

  • re_parse_dict: Dictionary containing only parameter values that may need to be re-parsed in the future.

load_param_simple(config_keys, quiet=False)[source]

Loads a parameter specified in the given config file. Keeps the parameter as a string and does not try to split it into a list of values.

Parameters
  • config_keys (List[str]) – The keys that specify the location of the parameter in the config file (header, subheader…).

  • quiet (bool) – If True suppresses the warning about the default value being used for a parameter, and about not converting a number to a string.

Return type

str

Returns

The parameter value.

opencmp.config_functions.initial_conditions module

class opencmp.config_functions.initial_conditions.ICFunctions(config_rel_path, import_dir, mesh, t_param=[<ngsolve.fem.Parameter object>], new_variables=[{}])[source]

Bases: opencmp.config_functions.base_config_functions.ConfigFunctions

Class to hold the initial condition functions.

set_initial_conditions(gfu_IC, mesh, model_name, model_components)[source]

Function to load the initial conditions from their configfile into a gridfunction.

Parameters
  • mesh (Mesh) – The model’s mesh.

  • model_name (str) – Sometimes several models will use the same IC configfile. This identifies which model’s ICs should be loaded.

  • gfu_IC (GridFunction) – A gridfunction to hold the initial conditions.

  • model_components (Dict[str, int]) – Maps between variable names and their component in the model’s finite element space.

Return type

None

update_initial_conditions(t_param, updated_variables, mesh)[source]

Function to update the initial conditions with new values of the model_variables.

Parameters
  • t_param (List[Parameter]) – List of parameters representing the current time and previous time steps.

  • updated_variables (List[Dict[str, Union[float, CoefficientFunction, GridFunction]]]) – List of dictionaries containing any new model variables and their values at each time step used in the time discretization scheme.

  • mesh (Mesh) – Mesh used by the model

Return type

None

opencmp.config_functions.load_config module

opencmp.config_functions.load_config.convert_str_to_dict(string, import_dir, t_param, mesh, new_variables=[{}], filetypes=['.vol', '.sol', '.vtk'], all_str=False)[source]

Function to convert a string into a dict. The values of the dict may be parsed into Python code or left as strings.

Parameters
  • string (str) – The string.

  • import_dir (str) – The path to the main run directory containing the file from which to import any Python functions.

  • t_param (Optional[List[Parameter], None]) – List of parameters representing the current time and previous time steps. If None, the parsed values have no possible time dependence and one single value is returned instead of a list of values corresponding to the parsed value at each time step.

  • mesh (Mesh) – Mesh used for the model

  • new_variables (List[Dict[str, Union[int, str, float, CoefficientFunction, GridFunction]]]) – List of dictionaries containing any new model variables and their values at each time step used in the time discretization scheme.

  • filetypes (List[str]) – List of possible filetypes to consider.

  • all_str (bool) – If True, don’t bother parsing any values, just load them as strings.

Returns

  • param_dict: Dictionary of the parameters from the string.

  • re_parse_dict: Dictionary containing only parameter values that may need to be re-parsed in the future.

Return type

Tuple[Dict, Dict]

opencmp.config_functions.load_config.load_coefficientfunction_into_gridfunction(gfu, coef_dict)[source]

Function to load a coefficientfunction(s) into a gridfunction.

The coefficientfunction(s) may have a different dimension than the gridfunction and need to be loaded into a specific component of it.

Parameters
  • coef_dict (Dict[Optional[int, None], CoefficientFunction]) – Dictionary containing the coefficientfunction(s) and which component they belong to (keys).

  • gfu (GridFunction) – The gridfunction to load the values into.

Return type

None

opencmp.config_functions.load_config.parse_str(string, import_dir, t_param, new_variables=[{}], filetypes=['.vol', '.sol', '.vtk'], mesh=None)[source]

Checks if a string appears to be a path to a file and if not parses the string into Python code.

Parameters
  • string (str) – The string.

  • import_dir (str) – The path to the main run directory containing the file from which to import any Python functions.

  • t_param (Optional[List[Parameter], None]) – List of parameters representing the current time and previous time steps. If None, the parsed values have no possible time dependence and one single value is returned instead of a list of values corresponding to the parsed value at each time step.

  • new_variables (List[Dict[str, Union[int, str, float, CoefficientFunction, GridFunction]]]) – List of dictionaries containing any new model variables and their values at each time step used in the time discretization scheme.

  • filetypes (List[str]) – List of possible filetypes to consider.

  • mesh (Optional[Mesh, None]) – Mesh used by the model.

Returns

  • parsed_str: List containing the value of the parsed string at every time, or just the single value of the parsed string if it has no possible time dependence.

  • variable_eval: Whether or not the expression contains any of the new model variables (would need to be re-parsed if their values change).

Return type

Tuple[List[Union[str, float, CoefficientFunction]], Union[str, bool]]

opencmp.config_functions.model_functions module

class opencmp.config_functions.model_functions.ModelFunctions(config_rel_path, import_dir, mesh, t_param=[<ngsolve.fem.Parameter object>], new_variables=[{}])[source]

Bases: opencmp.config_functions.base_config_functions.ConfigFunctions

Class to hold the model functions and parameters.

set_model_functions(fes, model_components)[source]

Function to load saved model functions into gridfunctions.

Parameters
  • fes (FESpace) – The model’s finite element space.

  • model_components (Dict[str, int]) – Maps between variable names and their component in the model’s finite element space.

Return type

None

update_model_functions(t_param, updated_variables, mesh)[source]

Function to update the model parameters/functions with new values of the model_variables.

Parameters
  • t_param (List[Parameter]) – List of parameters representing the current time and previous time steps.

  • updated_variables (List[Dict[str, Union[float, CoefficientFunction, GridFunction]]]) – List of dictionaries containing any new model variables and their values at each time step used in the time discretization scheme.

Return type

None

opencmp.config_functions.parse_arithmetic module

opencmp.config_functions.parse_arithmetic.eval_item(string, import_dir, t_param, new_variables, mesh=None, time_step=None)[source]

Parse a string containing a single expression (not a list of expressions).

Parameters
  • string (str) – The string of interest.

  • import_dir (str) – The path to the main run directory containing the file from which to import any Python functions.

  • t_param (Optional[List[Parameter], None]) – Parameter representing the current time.

  • new_variables (List[Dict[str, Any]]) – A dictionary of any new model variables and their values.

  • mesh (Optional[Mesh, None]) – Mesh used by the model.

  • time_step (Optional[int, None]) – The time step that the string is being evaluated for.

Returns

  • val: The Python code.

  • variable_eval: Whether or not the expression contains any of the new model variables (would need to be re-parsed if their values change). If variable_eval is True, the original string expression is returned in its place. If the expression involved importing a Python function that Python function is returned in place of variable_eval.

Return type

Tuple[Union[str, float, CoefficientFunction], Union[str, bool, callable]]

opencmp.config_functions.parse_arithmetic.eval_python(string, import_dir, mesh=None, new_variables={}, t_param=None, time_step=None)[source]

Parses a string into Python code.

Parameters
  • string (str) – The string of interest.

  • import_dir (str) – The path to the main run directory containing the file from which to import any Python functions.

  • mesh (Optional[Mesh, None]) – Mesh used by the model.

  • t_param (Optional[List[Parameter], None]) – Parameter representing the current time.

  • new_variables (List[Dict[str, Any]]) – A dictionary of any new model variables and their values.

  • time_step (Optional[int, None]) – The time step that the string is being evaluated for.

Returns

  • val: The Python code.

  • variable_eval: Whether or not the expression contains any of the new model variables (would need to be re-parsed if their values change). If variable_eval is True, the original string expression is returned in its place. If the expression involved importing a Python function that Python function is returned in place of variable_eval.

Return type

Tuple[Union[str, float, CoefficientFunction], Union[str, bool, callable]]

opencmp.config_functions.parse_arithmetic.evaluate_arith_stack(stack, import_dir, t_param, new_variables, mesh=None, time_step=None)[source]

Function to turn a list of strings corresponding to arithmetic operations into those operations as Python code.

Parameters
  • stack (List[Union[str, Tuple[str, int]]]) – The list of strings.

  • import_dir (str) – The path to the main run directory containing the file from which to import any Python functions.

  • t_param (Optional[List[Parameter], None]) – Parameter representing the current time.

  • new_variables (List[Dict[str, Any]]) – A dictionary of any new model variables and their values.

  • mesh (Optional[Mesh, None]) – The mesh used by the model.

  • time_step (Optional[int, None]) – The time step that the string is being evaluated for.

Returns

  • val: The Python code.

  • variable_eval: Whether or not the expression contains any of the new model variables (would need to be re-parsed if their values change). If the expression involves importing a Python function return that Python function instead.

Return type

Tuple[Union[str, float, CoefficientFunction], Union[bool, callable]]

opencmp.config_functions.parse_arithmetic.parse_to_arith(expr_stack)[source]

Creates a parser to turn a string into a series of arithmetic operations.

Parameters

expr_stack (List[Union[str, Tuple[str, int]]]) – Empty list to parse the string into.

Return type

Any

Returns

The parser.

opencmp.config_functions.reference_solutions module

class opencmp.config_functions.reference_solutions.RefSolFunctions(config_rel_path, import_dir, mesh, t_param=[<ngsolve.fem.Parameter object>], new_variables=[{}])[source]

Bases: opencmp.config_functions.base_config_functions.ConfigFunctions

Class to hold the reference solutions.

set_ref_solution(fes, model_components)[source]

Function to load the reference solutions from their configfile into a dictionary, including loading any saved gridfunctions.

Parameters
  • fes (FESpace) – The finite element space of the model.

  • model_components (Dict[str, int]) – Maps between variable names and their component in the finite element space.

Returns

Dictionary of reference solutions.

update_ref_solutions(t_param, updated_variables, mesh)[source]

Function to update the reference solutions with new values of the model_variables.

Parameters
  • t_param (List[Parameter]) – List of parameters representing the current time and previous time steps.

  • updated_variables (List[Dict[str, Union[float, CoefficientFunction, GridFunction]]]) – List of dictionaries containing any new model variables and their values at each time step used in the time discretization scheme.

  • mesh (Mesh) – Mesh used by the model

Return type

None

Module contents