Nektar++
|
Public Member Functions | |
def | __init__ (self, model) |
def | finalize (self, error_handler, pre_units_check_hook=None, check_units=True) |
def | create_new_component (self, cname) |
def | connect_variables (self, source, target) |
def | remove_connection (self, var1, var2) |
def | remove_connections (self, var) |
def | add_variable (self, comp, vname, units, **kwargs) |
def | add_units (self, units) |
def | add_expr_to_comp (self, comp, expr) |
def | remove_expr (self, expr) |
def | remove_definition (self, var, keep_initial_value=False) |
def | del_attr (self, elt, localName, ns=None) |
def | set_units_converter (self, converter) |
def | get_units_converter (self) |
Public Attributes | |
model | |
connections_made | |
Private Member Functions | |
def | _clear_model_caches (self) |
def | _make_connection (self, src_var, target_comp, target_vname) |
def | _find_connection_element (self, var1, var2) |
def | _create_connection_element (self, var1, var2) |
def | _update_connections (self, oldVar, newVar) |
def | _find_common_tail (self, l1, l2) |
def | _parent_path (self, comp) |
def | _process_operator (self, expr, operator, func, *args, **kwargs) |
def | _find_or_create_variable (self, cname, vname, source) |
def | _get_units_object (self, units) |
def | _uniquify_var_name (self, varname, comp) |
def | _uniquify_name (self, name, callable) |
def | _convert_initial_value (self, var, units, do_conversion=True) |
Private Attributes | |
_units_converter | |
Base class supporting common model modification functionality. This class contains the logic to deal with adding/deleting variables, components, equations, etc. and connecting things up. It also handles re-analysing the model when modifications have been completed to ensure that PyCml's internal data structures are up-to-date. Instances should be created with the model to modify as a single parameter. Once all modifications have been completed, the finalize method must be called to ensure later processing of the model (e.g. code generation) will succeed.
Definition at line 47 of file processors.py.
def CellMLToNektar.processors.ModelModifier.__init__ | ( | self, | |
model | |||
) |
Constructor.
Reimplemented in CellMLToNektar.processors.InterfaceGenerator, and CellMLToNektar.processors.UnitsConverter.
Definition at line 58 of file processors.py.
|
private |
Clear cached links in the model, since we'll need to recompute many of them once we've finished modifying it. Also clears dependency information.
Definition at line 100 of file processors.py.
References CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
Referenced by CellMLToNektar.processors.ModelModifier.finalize().
|
private |
Convert any initial value of the given variable into the given units. If there is no initial value, returns None. If there is no units converter, leaves the initial_value unchanged.
Definition at line 513 of file processors.py.
References CellMLToNektar.processors.ModelModifier._units_converter, and CellMLToNektar.pycml.equals().
Referenced by CellMLToNektar.processors.InterfaceGenerator.add_input().
|
private |
Create a connection element connecting the given variables and add to the model. If there's already a connection element for the relevant pair of components, we just add another map_variables element to that.
Definition at line 249 of file processors.py.
References CellMLToNektar.processors.ModelModifier._find_connection_element(), CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
Referenced by CellMLToNektar.processors.ModelModifier._make_connection().
|
private |
Find the first element at which both lists are identical from then on.
Definition at line 327 of file processors.py.
Referenced by CellMLToNektar.processors.ModelModifier.connect_variables().
|
private |
Find any connection element containing a connection of the given variables. Returns a pair, the first element of which is either the element or None, and the second of which is a boolean indicating whether the variables need to be swapped in order to match the order of the components in the connection.
Definition at line 228 of file processors.py.
References CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
Referenced by CellMLToNektar.processors.ModelModifier._create_connection_element(), and CellMLToNektar.processors.ModelModifier.remove_connection().
|
private |
Find a given variable in the model, creating it if necessary. We look for a variable in the component named cname with the same name as the source. If it doesn't exist, a variable named vname will be created in that component (unless it already exists). The variable will become a mapped variable with the given source. Hence if it is created it will have the same units.
Definition at line 359 of file processors.py.
References CellMLToNektar.processors.ModelModifier.add_variable(), CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
Referenced by CellMLToNektar.processors.ModelModifier._make_connection().
|
private |
Helper function to convert a units specification into a cellml_units object. The input can be a cellml_units object, in which case we just return it. However, it can also be a serialised CellML units definition, in which case it will be parsed to create the object.
Definition at line 394 of file processors.py.
References CellMLToNektar.pycml.amara_parse_cellml().
Referenced by CellMLToNektar.processors.InterfaceGenerator.add_input(), CellMLToNektar.processors.InterfaceGenerator.add_output(), and CellMLToNektar.processors.InterfaceGenerator.add_output_function().
|
private |
Make a connection from a source variable to a given component and suggested local name. Note that in the case that both variables already exist and are connected, the existing connection is allowed to flow in either direction.
Definition at line 180 of file processors.py.
References CellMLToNektar.processors.ModelModifier._create_connection_element(), CellMLToNektar.processors.ModelModifier._find_or_create_variable(), CellMLToNektar.processors.ModelModifier._make_connection(), and CellMLToNektar.processors.ModelModifier.connections_made.
Referenced by CellMLToNektar.processors.ModelModifier._make_connection(), and CellMLToNektar.processors.ModelModifier.connect_variables().
|
private |
Return a path of components from that given to the encapsulation root. The root is specified by None, since we're really dealing with a forest, not a tree.
Definition at line 340 of file processors.py.
Referenced by CellMLToNektar.processors.ModelModifier.connect_variables().
|
private |
Apply func to any application of the given operator within the given tree.
Definition at line 352 of file processors.py.
References CellMLToNektar.processors.ModelModifier._process_operator(), CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
Referenced by CellMLToNektar.processors.ModelModifier._process_operator(), and CellMLToNektar.processors.InterfaceGenerator._transform_derivatives_on_rhs().
|
private |
Ensure the given name is unique within a particular context. The context is determined by the given function: it will be passed candidate names to test for existence, and is expected to throw iff the name is not already used. Underscores will be appended to the given name until callable throws, and the resulting unique name returned.
Definition at line 488 of file processors.py.
Referenced by CellMLToNektar.processors.ModelModifier._uniquify_var_name(), and CellMLToNektar.processors.ModelModifier.add_units().
|
private |
Ensure varname is unique within the given component. Underscores will be appended to the name until it is unique. The unique name will be returned.
Definition at line 481 of file processors.py.
References CellMLToNektar.processors.ModelModifier._uniquify_name().
Referenced by CellMLToNektar.processors.InterfaceGenerator._split_ode().
|
private |
Change all variables connected to oldVar to be mapped to newVar instead.
Definition at line 314 of file processors.py.
References CellMLToNektar.processors.ModelModifier.connect_variables(), CellMLToNektar.processors.ModelModifier.del_attr(), CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), CellMLToNektar.translators.CellMLTranslator.model, and CellMLToNektar.processors.ModelModifier.remove_connections().
Referenced by CellMLToNektar.processors.InterfaceGenerator.add_input().
def CellMLToNektar.processors.ModelModifier.add_expr_to_comp | ( | self, | |
comp, | |||
expr | |||
) |
Add an expression to the mathematics in the given component. comp may be a cellml_component instance or a component name.
Definition at line 434 of file processors.py.
References CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
Referenced by CellMLToNektar.processors.InterfaceGenerator._split_ode(), CellMLToNektar.processors.InterfaceGenerator.add_output_function(), CellMLToNektar.processors.UnitsConverter.convert_constant(), CellMLToNektar.processors.UnitsConverter.convert_mapping(), and CellMLToNektar.processors.InterfaceGenerator.make_var_computed_constant().
def CellMLToNektar.processors.ModelModifier.add_units | ( | self, | |
units | |||
) |
Add a units definition to the model, if it doesn't already exist. If the definition isn't in the model, at whole-model level, it will be added. If the same definition is already available, however, that definition should be used by preference. Will return the actual units object to use.
Definition at line 409 of file processors.py.
References CellMLToNektar.processors.ModelModifier._uniquify_name(), CellMLToNektar.pycml.cellml_model.add_units(), CellMLToNektar.pycml.cellml_component.add_units(), CellMLToNektar.processors.ModelModifier.add_units(), CellMLToNektar.pycml.get_units_by_name(), CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
Referenced by CellMLToNektar.processors.ModelModifier.add_units(), CellMLToNektar.processors.ModelModifier.add_variable(), and CellMLToNektar.processors.UnitsConverter.convert_constant().
def CellMLToNektar.processors.ModelModifier.add_variable | ( | self, | |
comp, | |||
vname, | |||
units, | |||
** | kwargs | ||
) |
Add a new variable to the given component. Remaining arguments are as for cellml_variable.create_new. Returns the new variable object.
Definition at line 381 of file processors.py.
References CellMLToNektar.pycml.cellml_model.add_units(), CellMLToNektar.pycml.cellml_component.add_units(), CellMLToNektar.processors.ModelModifier.add_units(), CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
Referenced by CellMLToNektar.processors.ModelModifier._find_or_create_variable(), CellMLToNektar.processors.InterfaceGenerator._split_ode(), CellMLToNektar.processors.InterfaceGenerator.add_input(), CellMLToNektar.processors.InterfaceGenerator.add_output(), CellMLToNektar.processors.InterfaceGenerator.add_output_function(), and CellMLToNektar.processors.UnitsConverter.convert_mapping().
def CellMLToNektar.processors.ModelModifier.connect_variables | ( | self, | |
source, | |||
target | |||
) |
Create a connection between the given source and target variables. The variables are both specified either by a pair (cname,vname), or as cellml_variable objects. The source variable must exist within the model, whereas the target might not, in which case it will be created. Note that in the case that both source and target exist, it might NOT be the case that target obtains its value from source. They might already be connected, and source obtains its value from target. Or they might both obtain their value from a common source. If the variable names are not identical, any variables created will have the same name as the target, if possible. If there's an existing variable with that name, not connected to the source, then underscores will be appended to the name to avoid conflicts. Note that we do check for variables in intermediate components that have the same name as the source and are connected to it, to avoid adding unnecessary variables. Returns the target variable object.
Definition at line 132 of file processors.py.
References CellMLToNektar.processors.ModelModifier._find_common_tail(), CellMLToNektar.processors.ModelModifier._make_connection(), CellMLToNektar.processors.ModelModifier._parent_path(), CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
Referenced by CellMLToNektar.processors.InterfaceGenerator._transform_derivative_on_rhs(), CellMLToNektar.processors.ModelModifier._update_connections(), CellMLToNektar.processors.InterfaceGenerator.add_output(), and CellMLToNektar.processors.UnitsConverter.modify_rhs().
def CellMLToNektar.processors.ModelModifier.create_new_component | ( | self, | |
cname | |||
) |
Create a new component in the model, ensuring the name is unique. If a component with name cname already exists, underscores will be added to the component name to make it unique.
Definition at line 114 of file processors.py.
References CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
Referenced by CellMLToNektar.processors.InterfaceGenerator.get_interface_component().
def CellMLToNektar.processors.ModelModifier.del_attr | ( | self, | |
elt, | |||
localName, | |||
ns = None |
|||
) |
Delete an XML attribute from an element, if it exists.
Definition at line 474 of file processors.py.
Referenced by CellMLToNektar.processors.ModelModifier._update_connections(), CellMLToNektar.processors.InterfaceGenerator.add_input(), CellMLToNektar.processors.InterfaceGenerator.add_output(), and CellMLToNektar.processors.ModelModifier.remove_definition().
def CellMLToNektar.processors.ModelModifier.finalize | ( | self, | |
error_handler, | |||
pre_units_check_hook = None , |
|||
check_units = True |
|||
) |
Re-do the model validation steps needed for further processing of the model. Checks connections, etc. and builds up the dependency graph again, then performs a topological sort. If any errors are found during re-validation, the error_handler will be called with the list. Warnings are ignored. TODO: figure out how to determine how much of this is actually needed - InterfaceGenerator can probably get away with less work.
Reimplemented in CellMLToNektar.processors.InterfaceGenerator.
Definition at line 64 of file processors.py.
References CellMLToNektar.processors.ModelModifier._clear_model_caches(), CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
def CellMLToNektar.processors.ModelModifier.get_units_converter | ( | self | ) |
Get the units converter object, if any has been set.
Definition at line 507 of file processors.py.
References CellMLToNektar.processors.ModelModifier._units_converter.
def CellMLToNektar.processors.ModelModifier.remove_connection | ( | self, | |
var1, | |||
var2 | |||
) |
Remove a connection between two variables. Removes the relevant map_variables element. If this results in an empty connection element, removes that as well.
Definition at line 271 of file processors.py.
References CellMLToNektar.processors.ModelModifier._find_connection_element().
def CellMLToNektar.processors.ModelModifier.remove_connections | ( | self, | |
var | |||
) |
Remove all connection elements for the given variable. Removes each relevant map_variables element. If this results in an empty connection element, removes that as well.
Definition at line 291 of file processors.py.
References CellMLToNektar.processors.ModelModifier.model, CellMLToNektar.pycml.cellml_variable.model(), CellMLToNektar.pycml.mathml.model(), and CellMLToNektar.translators.CellMLTranslator.model.
Referenced by CellMLToNektar.processors.ModelModifier._update_connections().
def CellMLToNektar.processors.ModelModifier.remove_definition | ( | self, | |
var, | |||
keep_initial_value = False |
|||
) |
Remove any existing definition (as an equation) of the given variable. If keep_initial_value is False, then also remove any initial_value attribute. If the variable is Mapped, throw a ModelModificationError.
Definition at line 456 of file processors.py.
References CellMLToNektar.processors.ModelModifier.del_attr(), and CellMLToNektar.processors.ModelModifier.remove_expr().
Referenced by CellMLToNektar.processors.InterfaceGenerator.make_var_computed_constant(), and CellMLToNektar.processors.InterfaceGenerator.make_var_constant().
def CellMLToNektar.processors.ModelModifier.remove_expr | ( | self, | |
expr | |||
) |
Remove an expression (ODE or assignment) from its parent.
Definition at line 448 of file processors.py.
Referenced by CellMLToNektar.processors.InterfaceGenerator._split_ode(), CellMLToNektar.processors.UnitsConverter.convert_constant(), and CellMLToNektar.processors.ModelModifier.remove_definition().
def CellMLToNektar.processors.ModelModifier.set_units_converter | ( | self, | |
converter | |||
) |
Set the object used to units-convert variable initial values.
Definition at line 503 of file processors.py.
References CellMLToNektar.processors.ModelModifier._units_converter.
|
private |
Definition at line 61 of file processors.py.
Referenced by CellMLToNektar.processors.ModelModifier._convert_initial_value(), CellMLToNektar.processors.ModelModifier.get_units_converter(), and CellMLToNektar.processors.ModelModifier.set_units_converter().
CellMLToNektar.processors.ModelModifier.connections_made |
Definition at line 62 of file processors.py.
Referenced by CellMLToNektar.processors.ModelModifier._make_connection().
CellMLToNektar.processors.ModelModifier.model |
Definition at line 60 of file processors.py.
Referenced by CellMLToNektar.processors.InterfaceGenerator._add_all_odes_to_interface(), CellMLToNektar.processors.ModelModifier._clear_model_caches(), CellMLToNektar.processors.ModelModifier._create_connection_element(), CellMLToNektar.processors.ModelModifier._find_connection_element(), CellMLToNektar.processors.ModelModifier._find_or_create_variable(), CellMLToNektar.processors.ModelModifier._process_operator(), CellMLToNektar.pycml.reduce_commutative_nary._reduce(), CellMLToNektar.pycml.mathml_power._reduce(), CellMLToNektar.pycml.mathml_piecewise._set_in_units(), CellMLToNektar.pycml.cellml_variable._set_source_variable(), CellMLToNektar.processors.InterfaceGenerator._split_ode(), CellMLToNektar.processors.InterfaceGenerator._transform_derivatives_on_rhs(), CellMLToNektar.processors.ModelModifier._update_connections(), CellMLToNektar.processors.UnitsConverter.add_all_conversions(), CellMLToNektar.processors.UnitsConverter.add_conversions_for_component(), CellMLToNektar.processors.ModelModifier.add_expr_to_comp(), CellMLToNektar.processors.InterfaceGenerator.add_output_function(), CellMLToNektar.pycml.cellml_variable.add_rdf_annotation(), CellMLToNektar.processors.ModelModifier.add_units(), CellMLToNektar.processors.ModelModifier.add_variable(), CellMLToNektar.translators.CellMLTranslator.calculate_extended_dependencies(), CellMLToNektar.pycml.mathml_apply.classify_variables(), CellMLToNektar.CellMLToNektarTranslator.CellMLToNektarTranslator.code_name(), CellMLToNektar.processors.ModelModifier.connect_variables(), CellMLToNektar.processors.UnitsConverter.convert_assignments(), CellMLToNektar.processors.UnitsConverter.convert_connections(), CellMLToNektar.processors.UnitsConverter.convert_constant(), CellMLToNektar.processors.UnitsConverter.convert_mapping(), CellMLToNektar.processors.ModelModifier.create_new_component(), CellMLToNektar.processors.ModelModifier.finalize(), CellMLToNektar.pycml.mathml.get_component(), CellMLToNektar.processors.InterfaceGenerator.get_interface_component(), CellMLToNektar.pycml.cellml_variable.get_rdf_annotation(), CellMLToNektar.pycml.cellml_variable.get_rdf_annotations(), CellMLToNektar.pycml.mathml_apply.get_units(), CellMLToNektar.pycml.mathml_piecewise.get_units(), CellMLToNektar.pycml.cellml_variable.is_statically_const(), CellMLToNektar.processors.InterfaceGenerator.make_var_computed_constant(), CellMLToNektar.CellMLToNektarTranslator.CellMLToNektarTranslator.output_backward_euler_mathematics(), CellMLToNektar.CellMLToNektarTranslator.CellMLToNektarTranslator.output_constructor(), CellMLToNektar.CellMLToNektarTranslator.CellMLToNektarTranslator.output_default_stimulus(), CellMLToNektar.translators.CellMLTranslator.output_equations(), CellMLToNektar.CellMLToNektarTranslator.CellMLToNektarTranslator.output_equations(), CellMLToNektar.CellMLToNektarTranslator.CellMLToNektarTranslator.output_get_i_ionic(), CellMLToNektar.CellMLToNektarTranslator.CellMLToNektarTranslator.output_includes(), CellMLToNektar.translators.CellMLTranslator.output_mathematics(), CellMLToNektar.CellMLToNektarTranslator.CellMLToNektarTranslator.output_model_attributes(), CellMLToNektar.CellMLToNektarTranslator.CellMLToNektarTranslator.output_top_boilerplate(), CellMLToNektar.translators.CellMLTranslator.output_top_boilerplate(), CellMLToNektar.CellMLToNektarTranslator.CellMLToNektarTranslator.output_verify_state_variables(), CellMLToNektar.processors.ModelModifier.remove_connections(), CellMLToNektar.pycml.cellml_variable.remove_rdf_annotations(), CellMLToNektar.translators.CellMLTranslator.var_display_name(), and CellMLToNektar.translators.CellMLTranslator.varobj().