Esempio n. 1
0
    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())
Esempio n. 2
0
    def test_units(self):
        from libcellml import Model, Units

        # UnitsPtr units(size_t index)
        name = 'naaame'
        m = Model()
        u = Units()
        u.setName(name)
        self.assertIsNone(m.units(0))
        self.assertIsNone(m.units(1))
        self.assertIsNone(m.units(-1))
        m.addUnits(u)
        self.assertIsNone(m.units(1))
        self.assertIsNone(m.units(-1))
        self.assertIsNotNone(m.units(0))
        self.assertEqual(m.units(0).name(), name)
        del [m, u, name]

        # UnitsPtr units(const std::string &name)
        name = 'kermit'
        m = Model()
        u = Units()
        u.setName(name)
        self.assertIsNone(m.units(name))
        m.addUnits(u)
        self.assertIsNotNone(m.units(name))
        self.assertEqual(m.units(name).name(), name)
        del [m, u, name]
Esempio n. 3
0
    def test_imports(self):
        from libcellml import Model, ImportSource, Units

        m = Model()
        u = Units()

        u.setImportSource(ImportSource())

        m.addUnits(u)
        self.assertTrue(m.hasImports())

        self.assertEqual(1, m.importSourceCount())

        i = ImportSource()
        i.setUrl('actual_url')

        m.addImportSource(i)

        self.assertEqual(2, m.importSourceCount())

        i1 = m.importSource(0)

        self.assertTrue(m.hasImportSource(i))

        m.removeImportSource(0)

        self.assertEqual(1, m.importSourceCount())

        m.removeAllImportSources()

        self.assertEqual(0, m.importSourceCount())
Esempio n. 4
0
    def test_add_units(self):
        from libcellml import Model, Units

        # void addUnits(const UnitsPtr &units)
        m = Model()
        u = Units()
        m.addUnits(u)
Esempio n. 5
0
    def test_remove_all_units(self):
        from libcellml import Model, Units

        # void removeAllUnits()
        m = Model()
        u1 = Units()
        u2 = Units()
        m.addUnits(u1)
        m.addUnits(u2)
        m.removeAllUnits()
        self.assertFalse(m.removeUnits(u1))
        self.assertFalse(m.removeUnits(u2))
        del [m, u1, u2]
Esempio n. 6
0
    def test_units_count(self):
        from libcellml import Model, Units

        # size_t unitsCount()
        m = Model()
        self.assertEqual(m.unitsCount(), 0)
        m.addUnits(Units())
        self.assertEqual(m.unitsCount(), 1)
        m.addUnits(Units())
        self.assertEqual(m.unitsCount(), 2)
        m.removeAllUnits()
        self.assertEqual(m.unitsCount(), 0)
        del m
Esempio n. 7
0
    def test_take_units(self):
        from libcellml import Model, Units

        # UnitsPtr takeUnits(size_t index)
        name = 'piggy'
        m = Model()
        u = Units()
        u.setName(name)
        self.assertIsNone(m.takeUnits(0))
        self.assertIsNone(m.takeUnits(-1))
        self.assertIsNone(m.takeUnits(1))
        m.addUnits(u)
        self.assertIsNone(m.takeUnits(-1))
        self.assertIsNone(m.takeUnits(1))
        self.assertIsNotNone(m.takeUnits(0))
        self.assertIsNone(m.takeUnits(0))
        m.addUnits(Units())
        m.addUnits(u)
        self.assertEqual(m.takeUnits(1).name(), name)
        del [m, u]

        # UnitsPtr takeUnits(const std::string &name)
        name = 'aloha'
        m = Model()
        u = Units()
        u.setName(name)
        self.assertIsNone(m.takeUnits(name))
        m.addUnits(u)
        self.assertEqual(m.takeUnits(name).name(), name)
        self.assertIsNone(m.takeUnits(name))
        del [m, u, name]
Esempio n. 8
0
    def test_has_units(self):
        from libcellml import Model, Units

        # bool hasUnits(const std::string &name)
        name = 'loud'
        m = Model()
        u = Units()
        u.setName(name)
        m.addUnits(u)
        self.assertFalse(m.hasUnits('hi'))
        self.assertTrue(m.hasUnits(name))

        # bool hasUnits(const UnitsPtr &units)
        self.assertTrue(m.hasUnits(u))
        v = Units()
        self.assertFalse(m.hasUnits(v))
Esempio n. 9
0
    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)
Esempio n. 10
0
    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())
Esempio n. 11
0
    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())
Esempio n. 12
0
    def test_replace_units(self):
        from libcellml import Model, Units

        # bool replaceUnits(size_t index, const UnitsPtr &units)
        m = Model()
        u1 = Units()
        u1.setName('a')
        m.addUnits(u1)
        u2 = Units()
        u2.setName('b')
        self.assertTrue(m.replaceUnits(0, u2))
        self.assertFalse(m.replaceUnits(1, u1))
        self.assertFalse(m.replaceUnits(-1, u1))
        self.assertEqual(m.getUnits(0).getName(), 'b')
        del(m, u1, u2)

        # bool replaceUnits(const std::string &name, const UnitsPtr &units)
        m = Model()
        a = Units()
        a.setName('a')
        m.addUnits(a)
        b = Units()
        b.setName('b')
        self.assertFalse(m.replaceUnits('b', a))
        self.assertTrue(m.replaceUnits('a', b))
        self.assertTrue(m.replaceUnits('b', a))
        self.assertFalse(m.replaceUnits('b', a))
        del(m, a, b)

        # bool replaceUnits(const UnitsPtr &oldUnits, const UnitsPtr &newUnits)
        m = Model()
        a = Units()
        m.addUnits(a)
        b = Units()
        self.assertFalse(m.replaceUnits(b, a))
        self.assertTrue(m.replaceUnits(a, b))
        self.assertTrue(m.replaceUnits(b, a))
        self.assertFalse(m.replaceUnits(b, a))
        del(m, a, b)
Esempio n. 13
0
    def test_remove_units(self):
        from libcellml import Model, Units

        # bool removeUnits(size_t index)
        m = Model()
        u = Units()
        self.assertFalse(m.removeUnits(0))
        self.assertFalse(m.removeUnits(1))
        self.assertFalse(m.removeUnits(-1))
        m.addUnits(u)
        self.assertFalse(m.removeUnits(1))
        self.assertFalse(m.removeUnits(-1))
        self.assertTrue(m.removeUnits(0))
        self.assertFalse(m.removeUnits(0))
        del(m, u)

        # bool removeUnits(const std::string &name)
        name = 'bert'
        m = Model()
        u = Units()
        u.setName(name)
        self.assertFalse(m.removeUnits(name))
        m.addUnits(u)
        self.assertFalse(m.removeUnits('ernie'))
        self.assertTrue(m.removeUnits(name))
        del(m, u, name)

        # bool removeUnits(const UnitsPtr &units)
        m = Model()
        u1 = Units()
        u2 = Units()
        self.assertFalse(m.removeUnits(u1))
        m.addUnits(u1)
        self.assertFalse(m.removeUnits(u2))
        self.assertTrue(m.removeUnits(u1))
        self.assertFalse(m.removeUnits(u1))
        del(m, u1, u2)
    microA_per_cm2 = Units('microA_per_cm2')
    microA_per_cm2.addUnit('ampere', 'micro')
    microA_per_cm2.addUnit('metre', 'centi', -2.0)

    mS_per_cm2 = Units('milliS_per_cm2')
    mS_per_cm2.addUnit('siemens', 'milli')
    mS_per_cm2.addUnit('metre', 'centi', -2.0)

    ms = Units('ms')
    ms.addUnit('second', 'milli')

    mM = Units('mM')
    mM.addUnit('mole', 'milli')

    model.addUnits(ms)
    model.addUnits(mV)
    model.addUnits(mM)
    model.addUnits(microA_per_cm2)
    model.addUnits(mS_per_cm2)

    #  2.g 
    #      Set the units on each of the variables.  
    #      Call the validator again, and expect there to be no errors.
    k_channel_equations.variable('E_K').setUnits(mV)
    k_channel_equations.variable('i_K').setUnits(microA_per_cm2)
    k_channel_equations.variable('g_K').setUnits(mS_per_cm2)
    k_channel_equations.variable('V').setUnits(mV)
    k_channel_equations.variable('t').setUnits(ms)
    k_channel_equations.variable('n').setUnits('dimensionless')
    millisecond.addUnit("second", "milli")
    # "second" is a built-in unit, used inside millisecond with the
    # prefix "milli".  NB this is equivalent to specifying a prefix
    # integer value of -3, corresponding to the power of 10 by
    # which the base is multiplied.

    league = Units()
    league.setName("league")
    league.addUnit("metre", 3, 1.0, 5.556)
    # "metre" is a built-in unit.  A league is equal to 5556m, but here
    # we illustrate the overloaded function by passing a prefix of 3
    # (indicating a factor of 10^3), an exponent of 1, and a
    # multiplier of 5.556.

    #  2.b  Add the units to the model
    model.addUnits(millisecond)
    model.addUnits(league)

    #  2.c  Validate the model again and output the errors
    # validator.validateModel(model)
    # print_errors_to_terminal(validator)

    #  2.d  Change the constant "b" to have a hard-coded value of 2.0 in the MathML
    #       and amend the component's math block.
    equation2 = '<apply><eq/>\
                    <apply><diff/>\
                        <bvar>\
                            <ci>t</ci>\
                        </bvar>\
                        <ci>x</ci>\
                    </apply>\
Esempio n. 16
0
    #  we need from the set of built-in units, we just need to define the
    #  relationship.  NB: Even though units are used by Variables, which sit
    #  'inside' Components, Units sit inside the Model itself.  This helps you to
    #  reuse Units when you have more than one component (more on that in
    #  Tutorial 5)

    #  3.a
    #      Define the relationship between our custom units and the built-in
    #      units. There is a list of built-in units and their definitions
    #      available in section 19.2 of the CellML2 specification.
    #      First we create the "month" units, which will be equivalent to
    #      60*60*24*30 = 2,592,000 seconds.
    month = Units("month")
    month.addUnit("second", 1,
                  2592000)  # Setting a month to be 2592000 seconds.
    model.addUnits(month)

    #  "second" is a built-in unit, used with a multiplier of 2592000.
    #  Note that this could have been written:
    #    month.addUnit("second", "mega", 1, 2.592)
    #    month.addUnit("second", 5, 25.92)

    #  3.b
    #      Create units which represent "per_month", which
    #      is simply the inverse of the "month" unit above.
    per_month = Units()
    per_month.setName("per_month")
    per_month.addUnit("month", -1)
    model.addUnits(per_month)

    #  3.c
    #  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-----------------------------------------------")
    print("      STEP 2: Read the sodium channel")
    print("-----------------------------------------------")

    #  2.a Read the model created in Tutorial 7 representing the sodium channel.
    #      Note that if you didn't do that tutorial you can simply copy the CellML file
    #      from Tutorial7_SodiumChannelModel.cellml in the resources folder.

    read_file2 = open("../resources/tutorial7_SodiumChannelModel.cellml", "r")
    #      - alpha_X and beta_X, rates, have units of *per millisecond*.

    #  5.a
    #      Create the units which will be needed by your variables and add them to the model.
    ms = Units('ms')
    per_ms = Units('per_ms')

    #  5.b
    #      Add Unit items to the units you created to define them.
    ms.addUnit('second', 'milli')
    per_ms.addUnit('second', 'milli', -1)

    #  5.c
    #      Add the Units to the model (not the component) so that other components can make
    #      use of them too.
    model.addUnits(ms)
    model.addUnits(per_ms)

    #  5.d
    #      Use the setUnits function to associate them with the appropriate variables.
    gateEquations.variable('t').setUnits(ms)
    gateEquations.variable('alpha_X').setUnits(per_ms)
    gateEquations.variable('beta_X').setUnits(per_ms)
    gateEquations.variable('X').setUnits('dimensionless')

    #  5.e
    #      Validate again, and expect no errors.
    validator.validateModel(model)
    print_issues(validator)

    #  end 5