def test_replace_model(self): from libcellml import Component, Importer, Model, Parser parser = Parser() model = parser.parseModel( file_contents('importer/generic_no_source.cellml')) importer = Importer() wrongSourceModel = Model('wrong') rightSourceModel = Model('right') rightSourceModel.addComponent(Component('a')) rightSourceModel.addComponent(Component('b')) rightSourceModel.addComponent(Component('c')) rightSourceModel.addComponent(Component('d')) self.assertTrue( importer.addModel(wrongSourceModel, 'i_dont_exist.cellml')) self.assertFalse( importer.replaceModel(rightSourceModel, 'not_in_library')) self.assertTrue( importer.replaceModel(rightSourceModel, 'i_dont_exist.cellml')) importer.resolveImports(model, '') self.assertEqual(0, importer.issueCount()) self.assertFalse(model.hasUnresolvedImports())
def test_clean(self): from libcellml import Component, Model, Units, Variable, Printer model = Model() c1 = Component('c1') c2 = Component() c2.setId('c2') c3 = Component() c4 = Component() u1 = Units('used') u2 = Units() u2.setId('u2') u3 = Units() v = Variable('x') model.addComponent(c1) model.addComponent(c2) model.addComponent(c3) model.addComponent(c4) model.addUnits(u1) model.addUnits(u2) model.addUnits(u3) c3.addVariable(v) self.assertEqual(4, model.componentCount()) self.assertEqual(3, model.unitsCount()) # Call the Model.clean() function to remove the empty items (c4 and u3). model.clean() self.assertEqual(3, model.componentCount()) self.assertEqual(2, model.unitsCount())
def test_auto_ids_group(self): from libcellml import Annotator, Component, Model from libcellml.enums import CellmlElementType_COMPONENT annotator = Annotator() model = Model() component1 = Component("c1") component2 = Component("c2") component3 = Component("c3") model.addComponent(component1) model.addComponent(component2) component2.addComponent(component3) annotator.setModel(model) self.assertEqual("", model.id()) self.assertEqual("", component1.id()) self.assertEqual("", component2.id()) self.assertEqual("", component3.id()) annotator.assignIds(CellmlElementType_COMPONENT) self.assertEqual("", model.id()) self.assertEqual("b4da55", component1.id()) self.assertEqual("b4da56", component2.id()) self.assertEqual("b4da57", component3.id())
def test_replace_model(self): from libcellml import Component, Importer, Model, Parser parser = Parser() model = parser.parseModel( file_contents("importer/generic_no_source.cellml")) importer = Importer() wrongSourceModel = Model("wrong") rightSourceModel = Model("right") rightSourceModel.addComponent(Component("a")) rightSourceModel.addComponent(Component("b")) rightSourceModel.addComponent(Component("c")) rightSourceModel.addComponent(Component("d")) self.assertTrue( importer.addModel(wrongSourceModel, "i_dont_exist.cellml")) self.assertFalse( importer.replaceModel(rightSourceModel, "not_in_library")) self.assertTrue( importer.replaceModel(rightSourceModel, "i_dont_exist.cellml")) importer.resolveImports(model, "") self.assertEqual(0, importer.issueCount()) self.assertFalse(model.hasUnresolvedImports())
def test_add_component(self): from libcellml import Model, Component m = Model() c = Component() self.assertEqual(0, m.componentCount()) m.addComponent(c) self.assertTrue(m.containsComponent(c)) self.assertEqual(1, m.componentCount())
def test_parent(self): from libcellml import Model, Component x = Model("model") self.assertIsNone(x.parent()) c = Component() self.assertIsNone(c.parent()) x.addComponent(c) self.assertEqual("model", c.parent().name()) self.assertEqual(1, x.componentCount())
def test_has_unresolved_imports(self): from libcellml import Model, Component, ImportSource # bool hasUnresolvedImports(); m = Model() self.assertFalse(m.hasUnresolvedImports()) c = Component() m.addComponent(c) self.assertFalse(m.hasUnresolvedImports()) c.setImportSource(ImportSource()) self.assertTrue(m.hasUnresolvedImports())
def test_assign_id(self): from libcellml import Annotator, Component, Model, Units from libcellml import Unit, CellmlElementType annotator = Annotator() model = Model() component1 = Component("c1") component2 = Component("c2") component3 = Component("c3") component3.setId("id3") units = Units("u1") units.addUnit("volt") model.addComponent(component1) model.addComponent(component2) component2.addComponent(component3) model.addUnits(units) annotator.setModel(model) self.assertEqual("", component1.id()) self.assertEqual("", component2.id()) self.assertEqual("", units.unitId(0)) annotator.assignId(component1) self.assertEqual("b4da55", component1.id()) self.assertEqual("", component2.id()) self.assertEqual("", units.unitId(0)) annotator.assignId(Unit(units, 0)) self.assertEqual("b4da55", component1.id()) self.assertEqual("", component2.id()) self.assertEqual("b4da56", units.unitId(0)) self.assertEqual("", annotator.assignId(None, CellmlElementType.UNDEFINED)) item = annotator.item("id3") annotator.assignId(item) self.assertEqual("b4da57", component3.id()) # For coverage only. annotator._assignId(component2)
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_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())
def test_remove_component(self): from libcellml import Model, Component m = Model() c1 = Component() c2 = Component() self.assertEqual(0, m.componentCount()) m.addComponent(c1) m.addComponent(c2) self.assertEqual(2, m.componentCount()) m.removeAllComponents() self.assertEqual(0, m.componentCount()) m.addComponent(c1) m.addComponent(c2) self.assertEqual(2, m.componentCount()) m.removeComponent(c2) self.assertEqual(1, m.componentCount())
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_add_model(self): from libcellml import Component, Importer, Model, Parser parser = Parser() importer = Importer() model = parser.parseModel( file_contents('importer/generic_no_source.cellml')) sourceModel = Model('source') sourceModel.addComponent(Component('a')) sourceModel.addComponent(Component('b')) sourceModel.addComponent(Component('c')) sourceModel.addComponent(Component('d')) # Add a model manually to the library, including the URL that it will replace in future imports. self.assertTrue(importer.addModel(sourceModel, 'i_dont_exist.cellml')) self.assertFalse(importer.addModel(sourceModel, 'i_dont_exist.cellml')) importer.resolveImports(model, '') self.assertEqual(0, importer.issueCount()) self.assertFalse(model.hasUnresolvedImports())
def test_create_destroy(self): from libcellml import AnalyserExternalVariable from libcellml import Component from libcellml import Model from libcellml import Variable m = Model('model') c = Component('component') v = Variable('test') self.assertTrue(m.addComponent(c)) self.assertTrue(c.addVariable(v)) aev = AnalyserExternalVariable(v) self.assertEqual(v.name(), aev.variable().name()) d0 = Variable('d0') d1 = Variable('d1') d2 = Variable('d2') d3 = Variable('d3') c.addVariable(d0) c.addVariable(d1) c.addVariable(d2) c.addVariable(d3) self.assertTrue(aev.addDependency(d0)) self.assertTrue(aev.addDependency(d1)) self.assertTrue(aev.addDependency(d2)) self.assertTrue(aev.addDependency(d3)) self.assertEqual(4, aev.dependencyCount()) self.assertIsNotNone(aev.dependency(0)) self.assertIsNotNone(aev.dependency(m, c.name(), d1.name())) self.assertIsNotNone(aev.dependencies()) self.assertTrue(aev.containsDependency(d0)) self.assertTrue(aev.containsDependency(m, c.name(), d1.name())) self.assertTrue(aev.removeDependency(1)) self.assertTrue(aev.removeDependency(d0)) self.assertTrue(aev.removeDependency(m, c.name(), d2.name())) aev.removeAllDependencies()
print('------------------------------------------------------------') print(' STEP 1: Define the model setup ') print('------------------------------------------------------------') # 1.a # Create a Model and name it appropriately. model = Model('PotassiumChannelModel') # 1.b # Create a wrapping component and name it 'potassiumChannel'. k_channel = Component('potassiumChannel') # 1.c # Add the component to the model. model.addComponent(k_channel) # end 1 print('------------------------------------------------------------') print(' STEP 2: Define the potassium channel equations component ') print('------------------------------------------------------------') # 2.a # Create a Component instance for the equations and name it 'potassiumChannelEquations'. # Add it to the wrapper component you created above. k_channel_equations = Component('potassiumChannelEquations') k_channel.addComponent(k_channel_equations) # end 2.a # The mathematics of a component is specified as a MathML 2 string (NB: higher versions
# 0 Setup stuff that is used throughout validator = Validator() model = Model() model.setName("Tutorial7_SodiumChannelModel") math_header = '<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:cellml="http://www.cellml.org/cellml/2.0#">' math_footer = '</math>' print("-----------------------------------------------") print(" STEP 1: Creating the sodium channel") print("-----------------------------------------------") # 1.a Create the component instance, name it, and add to the model sodium_channel = Component() sodium_channel.setName("sodiumChannel") model.addComponent(sodium_channel) # 1.b Add the MathML representing the governing equations if True: equation1 = \ '<apply>\ <eq/>\ <ci>E_Na</ci>\ <apply>\ <times/>\ <ci>RTF</ci>\ <apply>\ <log/>\ <apply>\ <divide/>\ <ci>Nao</ci>\
print("-----------------------------------------------------") print(" TUTORIAL 3: CREATE A MODEL USING THE API ") print("-----------------------------------------------------") # --------------------------------------------------------------------------- # STEP 1: Create the model instance # # 1.a Allocate the ModelPtr model = Model() model.setName("Tutorial3_model") # 1.b Create a component to use as an integrator, set its attributes and # add it to the model component = Component() component.setName("component") model.addComponent(component) # Checking that it worked print_model_to_terminal(model) # 1.c,d Create the MathML2 string representing the governing equation. The # header and footer are below already. math_header = '<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:cellml="http://www.cellml.org/cellml/2.0#">' math_footer = '</math>' equation = "<apply><eq/>\ <apply><diff/>\ <bvar>\ <ci>t</ci>\ </bvar>\ <ci>x</ci>\
model.setName('GateModel') # 1.c # We'll create a wrapper component whose only job is to encapsulate the other components. # This makes is a lot easier for this model to be reused, as the connections between # components internal to this one won't need to be re-established. # Note that the constructor for all named CellML entities is overloaded, so # you can pass it the name string at the time of creation. # Create a component named 'gate'. gate = Component('gate') # 1.d Finally we need to add the component to the model. This sets it at the top-level of # the components' encapsulation hierarchy. All other components need to be added # to this component, rather than the model. # Add the component to the model using the Model::addComponent() function. model.addComponent(gate) # 1.e # Print the model to the terminal using the print_model helper function and # check it is what you'd expect. print_model(model) # end 1 print('----------------------------------------------------------') print(' STEP 2: Create the gateEquations component ') print('----------------------------------------------------------') # 2.a # Create a gateEquations component and name it 'gateEquations'. gateEquations = Component('gateEquations')
print("-----------------------------------------------") # 1.a Read the model provided for you in the "Tutorial8_MembraneModel.cellml" # file in the resources folder. read_file1 = open("../resources/tutorial8_MembraneModel.cellml", "r") # 1.b Create a temporary model for the membrane membrane_model = parser.parseModel(read_file1.read()) membrane_model.setName("membraneModel") # 1.b Extract the membrane component from the parsed model and add it # to the combined model. Note that the membrane component's parent # must be removed before adding it to the model. membrane = membrane_model.component("membrane") membrane.removeParent() model.addComponent(membrane) # 1.c Validate the combined model. We expect to see errors from: # - missing units, as we have only added the component so far validator.validateModel(model) print_errors_to_terminal(validator) # 1.d Import the units from the membraneModel into the combined model for u in range(0, membrane_model.unitsCount()): model.addUnits(membrane_model.units(u)) # 1.e No errors expected this time :) validator.validateModel(model) print_errors_to_terminal(validator) print("\n-----------------------------------------------")