def test_units(self): from libcellml import Variable # std::string units() name = 'testo' v = Variable() self.assertEqual(v.units(), None) v.setUnits(name) self.assertEqual(v.units().name(), name) v.setUnits('') self.assertEqual(v.units().name(), '')
def test_clone(self): from libcellml import Units, Variable v = Variable("sodium") u = Units("kg_per_ml") v.setUnits(u) vCloned = v.clone() self.assertEqual("sodium", vCloned.name()) self.assertEqual("kg_per_ml", vCloned.units().name())
def test_get_units(self): from libcellml import Variable # std::string getUnits() name = 'testo' v = Variable() self.assertEqual(v.getUnits(), '') v.setUnits(name) self.assertEqual(v.getUnits(), name) v.setUnits('') self.assertEqual(v.getUnits(), '') del (v, name)
def test_link_units(self): from libcellml import Component, Model, Units, Variable m = Model() c = Component() v = Variable() u = Units("orange") self.assertFalse(m.hasUnlinkedUnits()) m.addUnits(u) v.setUnits("orange") c.addVariable(v) m.addComponent(c) self.assertTrue(m.hasUnlinkedUnits()) m.linkUnits() self.assertFalse(m.hasUnlinkedUnits())
def test_clone(self): from libcellml import Component, Model, Units, Variable m = Model() c1 = Component("c1") c2 = Component("c2") v = Variable("v1") u = Units("apple") m.addComponent(c1) m.addComponent(c2) c1.addVariable(v) v.setUnits(u) m.addUnits(u) mCloned = m.clone() self.assertEqual(2, mCloned.componentCount()) self.assertEqual(1, mCloned.unitsCount()) self.assertEqual(1, mCloned.component(0).variableCount()) self.assertEqual("apple", mCloned.component(0).variable(0).units().name())
def test_set_units(self): from libcellml import Variable, Units # void setUnits(const std::string &name) v = Variable() v.setUnits('') v.setUnits('Hello') v.setUnits('') del (v) # void setUnits(const UnitsPtr &units) name = 'tiger' u = Units() u.setName(name) v = Variable() v.setUnits(u) self.assertEqual(v.getUnits(), name)
def test_set_units(self): from libcellml import Variable, Units # void setUnits(const std::string &name) v = Variable() v.setUnits('') v.setUnits('Hello') v.setUnits('') del v # void setUnits(const UnitsPtr &units) name = 'tiger' u = Units() u.setName(name) v = Variable() v.setUnits(u) self.assertEqual(name, v.units().name()) v.removeUnits() self.assertIsNone(v.units())
<ci>E_Na</ci>\ </apply>\ </apply>\ </apply>' sodium_channel.setMath(math_header) sodium_channel.appendMath(equation1) sodium_channel.appendMath(equation2) sodium_channel.appendMath(equation3) sodium_channel.appendMath(math_footer) # 1.c Add the variables if True: V = Variable() V.setName("V") V.setUnits("mV") sodium_channel.addVariable(V) t = Variable() t.setName("t") t.setUnits("ms") sodium_channel.addVariable(t) h = Variable() h.setName("h") h.setUnits("dimensionless") sodium_channel.addVariable(h) m = Variable() m.setName("m") m.setUnits("dimensionless")
# 1.f Create a validator and use it to check the model so far validator = Validator() validator.validateModel(model) print_errors_to_terminal(validator) # 1.g Create some variables and add them to the component time = Variable() time.setName("t") component.addVariable(time) distance = Variable() distance.setName("x") component.addVariable(distance) # 1.e Assign units to the variables time.setUnits("millisecond") distance.setUnits("league") # Check it work and print the validation errors to the terminal print("-----------------------------------------------------") print(" Printing the model at Step 1 ") print("-----------------------------------------------------") print_model_to_terminal(model) print("-----------------------------------------------------") print(" Printing the validation errors after Step 1 ") print("-----------------------------------------------------") validator.validateModel(model) print_errors_to_terminal(validator) # ---------------------------------------------------------------------------
# - we need to create external variables in that component # - we need to specify the connections between variables and # - we need to permit external connections on the variables. # 6.c # Create a component which will store the hard-coded values for initialisation. # Name it 'gateParameters', and add it to the top-level gate component as a sibling # of the gateEquations component. gateParameters = Component('gateParameters') gate.addComponent(gateParameters) # 6.d # Create appropriate variables in this component, and set their units. # Use the setInitialValue function to initialise them. X = Variable('X') X.setUnits('dimensionless') X.setInitialValue(0) 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.
print(" STEP 5: Create the environment component") print("-----------------------------------------------") print_encapsulation_structure_to_terminal(model) # 5.a Creating the environment component and adding it to the model environment = Component() environment.setName("environment") model.addComponent(environment) # 5.b Add variables to the environment component. if True: V = Variable() V.setName("V") V.setInitialValue(-85) V.setUnits("mV") environment.addVariable(V) 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("-----------------------------------------------")
# with the addUnit command is multiplied to create the final Units definition. b_units = Units() b_units.setName("per_shark_month") b_units.addUnit("per_month") b_units.addUnit("number_of_sharks", -1) model.addUnits(b_units) d_units = Units() d_units.setName("per_fish_month") d_units.addUnit("per_month") d_units.addUnit("thousands_of_fish", -1) model.addUnits(d_units) # 3.e # Set the units to their respective variables. time.setUnits(month) sharks.setUnits(number_of_sharks) fish.setUnits(thousands_of_fish) a.setUnits(per_month) b.setUnits(b_units) c.setUnits(per_month) d.setUnits(d_units) # 3.f # Call the validator again to check the model. # Expect one error regarding a missing unit in the MathML. validator.validateModel(model) print_issues(validator) # 3.g # Units for constants inside the MathML must be specified at the time.
# Expected errors refer to variables referenced in the maths which # are not (yet) defined in the component, as well as cn element units # which are not defined yet either. validator.validateModel(model) print_errors_to_terminal(validator) print("-----------------------------------------------") print(" STEP 3: Define the variables and their units ") print("-----------------------------------------------") # 3.a,b Declaring the variables, their names, units, and initial conditions # Note that the names given to variables must be the same as that used # within the <ci> blocks in the MathML string we created in step 2.a. t = Variable() t.setName("t") t.setUnits("millisecond") V = Variable() V.setName("V") V.setUnits("millivolt") V.setInitialValue(0.0) alpha_n = Variable() alpha_n.setName("alpha_n") alpha_n.setUnits("per_millisecond") alpha_n.setInitialValue(1.0) beta_n = Variable() beta_n.setName("beta_n") beta_n.setUnits("per_millisecond") beta_n.setInitialValue(2.0)