def test_id(self): from libcellml import Variable from libcellml.variable import Variable_setEquivalenceMappingId, Variable_setEquivalenceConnectionId, \ Variable_equivalenceMappingId, Variable_equivalenceConnectionId from libcellml.variable import Variable_removeEquivalenceMappingId, Variable_removeEquivalenceConnectionId v1 = Variable("v1") v2 = Variable("v2") Variable.addEquivalence(v1, v2) self.assertEqual("", Variable.equivalenceMappingId(v1, v2)) Variable.setEquivalenceMappingId(v1, v2, "mapping_id") self.assertEqual("mapping_id", Variable.equivalenceMappingId(v1, v2)) Variable.removeEquivalenceMappingId(v1, v2) self.assertEqual("", Variable.equivalenceMappingId(v1, v2)) Variable_setEquivalenceMappingId(v1, v2, "other_mapping_id") self.assertEqual("other_mapping_id", Variable_equivalenceMappingId(v1, v2)) Variable_removeEquivalenceMappingId(v1, v2) self.assertEqual("", Variable_equivalenceMappingId(v1, v2)) self.assertEqual("", Variable.equivalenceConnectionId(v1, v2)) Variable.setEquivalenceConnectionId(v1, v2, "connection_id") self.assertEqual("connection_id", Variable.equivalenceConnectionId(v1, v2)) Variable.removeEquivalenceConnectionId(v1, v2) self.assertEqual("", Variable.equivalenceConnectionId(v1, v2)) Variable_setEquivalenceConnectionId(v1, v2, "other_connection_id") self.assertEqual("other_connection_id", Variable_equivalenceConnectionId(v1, v2)) Variable_removeEquivalenceConnectionId(v1, v2) self.assertEqual("", Variable_equivalenceConnectionId(v1, v2))
def test_has_equivalent_variable(self): from libcellml import Variable # bool hasEquivalentVariable(const VariablePtr &equivalentVariable) v1 = Variable() v2 = Variable() v3 = Variable() self.assertFalse(v1.hasEquivalentVariable(v1, True)) self.assertFalse(v1.hasEquivalentVariable(v2, True)) self.assertFalse(v1.hasEquivalentVariable(v3, True)) self.assertFalse(v2.hasEquivalentVariable(v1, True)) self.assertFalse(v2.hasEquivalentVariable(v2, True)) self.assertFalse(v2.hasEquivalentVariable(v3, True)) self.assertFalse(v3.hasEquivalentVariable(v1, True)) self.assertFalse(v3.hasEquivalentVariable(v2, True)) self.assertFalse(v3.hasEquivalentVariable(v3, True)) Variable.addEquivalence(v1, v2) Variable.addEquivalence(v2, v3) self.assertFalse(v1.hasEquivalentVariable(v1, True)) self.assertTrue(v1.hasEquivalentVariable(v2, True)) self.assertTrue(v1.hasEquivalentVariable(v3, True)) self.assertTrue(v2.hasEquivalentVariable(v1, True)) self.assertFalse(v2.hasEquivalentVariable(v2, True)) self.assertTrue(v2.hasEquivalentVariable(v3, True)) self.assertTrue(v3.hasEquivalentVariable(v1, True)) self.assertTrue(v3.hasEquivalentVariable(v2, True)) self.assertFalse(v3.hasEquivalentVariable(v3, True))
def test_add_equivalence(self): from libcellml import Variable # static void addEquivalence(const VariablePtr &variable1, # const VariablePtr &variable2) v1 = Variable() v2 = Variable() Variable.addEquivalence(v1, v2)
def test_remove_all_equivalences(self): from libcellml import Variable # void removeAllEquivalences() v1 = Variable() v2 = Variable() v3 = Variable() Variable.addEquivalence(v1, v2) Variable.addEquivalence(v1, v3) v2.removeAllEquivalences() self.assertFalse(Variable.removeEquivalence(v1, v2)) self.assertTrue(Variable.removeEquivalence(v1, v3))
def test_equivalent_variable_count(self): from libcellml import Variable # size_t equivalentVariableCount() v1 = Variable() v2 = Variable() v3 = Variable() self.assertEqual(v1.equivalentVariableCount(), 0) self.assertEqual(v2.equivalentVariableCount(), 0) self.assertEqual(v3.equivalentVariableCount(), 0) Variable.addEquivalence(v1, v2) Variable.addEquivalence(v2, v3) self.assertEqual(v1.equivalentVariableCount(), 1) self.assertEqual(v2.equivalentVariableCount(), 2) self.assertEqual(v3.equivalentVariableCount(), 1)
def test_remove_equivalence(self): from libcellml import Variable # static bool removeEquivalence(const VariablePtr &variable1, # const VariablePtr &variable2) v1 = Variable() v2 = Variable() v3 = Variable() self.assertFalse(Variable.removeEquivalence(v1, v2)) Variable.addEquivalence(v1, v2) self.assertFalse(Variable.removeEquivalence(v1, v3)) self.assertFalse(Variable.removeEquivalence(v3, v2)) self.assertTrue(Variable.removeEquivalence(v1, v2)) self.assertFalse(Variable.removeEquivalence(v1, v2)) Variable.addEquivalence(v1, v2) self.assertTrue(Variable.removeEquivalence(v2, v1))
def test_equivalent_variable(self): from libcellml import Variable # VariablePtr equivalentVariable(size_t index) v1 = Variable() v2 = Variable() v3 = Variable() self.assertIsNone(v1.equivalentVariable(0)) self.assertIsNone(v1.equivalentVariable(1)) self.assertIsNone(v1.equivalentVariable(-1)) Variable.addEquivalence(v1, v2) Variable.addEquivalence(v1, v3) self.assertIsNone(v1.equivalentVariable(2)) self.assertIsNone(v1.equivalentVariable(-1)) self.assertIsNotNone(v1.equivalentVariable(0)) self.assertIsNotNone(v1.equivalentVariable(1))
def test_equivalence(self): from libcellml import Variable from libcellml.variable import Variable_addEquivalence, Variable_removeEquivalence v1 = Variable() v2 = Variable() self.assertFalse(v1.hasEquivalentVariable(v2)) Variable.addEquivalence(v1, v2) self.assertTrue(v1.hasEquivalentVariable(v2)) Variable.removeEquivalence(v1, v2) self.assertFalse(v1.hasEquivalentVariable(v2)) Variable_addEquivalence(v1, v2) self.assertTrue(v1.hasEquivalentVariable(v2)) Variable_removeEquivalence(v1, v2) self.assertFalse(v1.hasEquivalentVariable(v2))
def test_variable_interfaces(self): from libcellml import Component, Model, Variable m = Model() c1 = Component("c1") c2 = Component("c2") v1 = Variable("v1") v2 = Variable("v2") c1.addVariable(v1) c2.addVariable(v2) m.addComponent(c1) m.addComponent(c2) Variable.addEquivalence(v1, v2) m.fixVariableInterfaces() self.assertEqual("public", v1.interfaceType()) self.assertEqual("public", v2.interfaceType())
n_gate_parameters = Component('nGateParameters') n_gate.addComponent(n_gate_parameters) n_gate_parameters.addVariable(n_gate_equations.variable('n').clone()) # 7.c # In order for other encapsulating components to access these variables, they also need to have # intermediate variables in the n_gate or potassium channel components too. This is only true # of variables that you want to be available to the outside. In this example, we need to add # the variable 'n' to the n_gate in order that its parent (the potassium channel equations) can # access it. n_gate.addVariable(n_gate_equations.variable('n').clone()) # 7.d # Create variable connections between these variables and their counterparts in the equations # components. Validate, expecting errors related to missing or incorrect interface types. Variable.addEquivalence(k_channel_parameters.variable('E_K'), k_channel_equations.variable('E_K')) Variable.addEquivalence(k_channel_parameters.variable('g_K'), k_channel_equations.variable('g_K')) Variable.addEquivalence(n_gate.variable('n'), n_gate_equations.variable('n')) validator.validateModel(model) print_issues(validator) # 7.e # Set the required interface types as listed by the validator. This can be done individually using the # Variable.setInterfaceType() function, or automatically using the Model.fixVariableInterfaces() # function. Validate again, expecting no validation errors. model.fixVariableInterfaces() validator.validateModel(model) print_issues(validator)
t.setName("t") t.setUnits("ms") environment.addVariable(t) # 4.c Add the new component to the model and validate model.addComponent(environment) validator.validateModel(model) print_errors_to_terminal(validator) print("-----------------------------------------------") print(" STEP 5: Connecting variables and components") print("-----------------------------------------------") # 5.a Connecting the equivalent variables between all the components Variable.addEquivalence(environment.variable("t"), sodium_channel.variable("t")) Variable.addEquivalence(sodium_channel.variable("t"), mGate.variable("t")) Variable.addEquivalence(sodium_channel.variable("t"), hGate.variable("t")) environment.variable("t").setInterfaceType("public") sodium_channel.variable("t").setInterfaceType("public_and_private") mGate.variable("t").setInterfaceType("public") hGate.variable("t").setInterfaceType("public") Variable.addEquivalence(environment.variable("V"), sodium_channel.variable("V")) Variable.addEquivalence(sodium_channel.variable("V"), mGate.variable("V")) Variable.addEquivalence(sodium_channel.variable("V"), hGate.variable("V")) environment.variable("V").setInterfaceType("public") sodium_channel.variable("V").setInterfaceType("public_and_private") mGate.variable("V").setInterfaceType("public") hGate.variable("V").setInterfaceType("public")
gateParameters.addVariable(X) alpha = Variable('alpha') alpha.setUnits(per_ms) alpha.setInitialValue(0.1) gateParameters.addVariable(alpha) beta = Variable('beta') beta.setUnits(per_ms) beta.setInitialValue(0.5) gateParameters.addVariable(beta) # 6.e # Specify a variable equivalence between the gateEquations variables and the parameter variables. # Validate the model again, expecting errors related to the variable interface types. Variable.addEquivalence(gateEquations.variable('X'), gateParameters.variable('X')) Variable.addEquivalence(gateEquations.variable('alpha_X'), gateParameters.variable('alpha')) Variable.addEquivalence(gateEquations.variable('beta_X'), gateParameters.variable('beta')) validator.validateModel(model) print_issues(validator) # 6.f # Set the variable interface type according to the recommendation from the validator. # This can either be done individually using the Variable::setInterfaceType() function, or # en masse for all the model's interfaces using the Model::fixVariableInterfaces() function. # Validate and analyse again, expecting no errors. model.fixVariableInterfaces()
# to one another yet. # Issue [0] is an ERROR: # description: Variable 't' in component 'importedGateM' and variable 't' in component # 'importedGateH' cannot both be the variable of integration. # stored item type: VARIABLE # 6.e # Create any necessary variable equivalences so that these two variables are connected. You # can refer to your printout of the model's structure to help if need be, and remember that only # variables in a sibling or parent/child relationship can be connected. # Useful function: Variable.addEquivalence(v1, v2) will create an equivalence between the # variables v1 and v2. Variable.addEquivalence( model.component('importedGateM', True).variable('t'), model.component('mGateEquations', True).variable('t')) Variable.addEquivalence( model.component('mGate', True).variable('t'), model.component('mGateEquations', True).variable('t')) # 6.f Re-flatten and re-analyse the model and print the issues to the terminal. analyser.analyseModel(importer.flattenModel(model)) print_issues(analyser) # end 6.f # Now we see the importance of checking iteratively for issues in the analyser class! # The nature of this class means that frequently it is unable to continue processing # when an issue is encountered. It's not unusual to fix one issue only to find twenty more! # Two of the errors reported deal with non-initialised variables. Looking at the model # printout we can see that this is because the integrated variable X (in both the imported
t = Variable() t.setName("t") t.setUnits("ms") environment.addVariable(t) # 5.c Add the new component to the model and validate validator.validateModel(model) print_errors_to_terminal(validator) print("\n-----------------------------------------------") print(" STEP 6: Connect the equivalent variables") print("-----------------------------------------------") # 6.a Connecting the membrane to its sibling environment, and the channels to their # parent membrane component. Variable.addEquivalence(membrane.variable("t"), sodium_channel.variable("t")) Variable.addEquivalence(membrane.variable("t"), potassium_channel.variable("t")) Variable.addEquivalence(environment.variable("t"), membrane.variable("t")) Variable.addEquivalence(membrane.variable("V"), sodium_channel.variable("V")) Variable.addEquivalence(membrane.variable("V"), potassium_channel.variable("V")) Variable.addEquivalence(membrane.variable("V"), leakage_current.variable("V")) Variable.addEquivalence(environment.variable("V"), membrane.variable("V")) # 6.b Setting the interface types for those which haven't been inherited already environment.variable("t").setInterfaceType("public") membrane.variable("t").setInterfaceType("public_and_private") environment.variable("V").setInterfaceType("public") membrane.variable("V").setInterfaceType("public_and_private") validator.validateModel(model) print_errors_to_terminal(validator)