Ejemplo n.º 1
0
    def test_variable(self):
        from libcellml import Component, Variable

        # VariablePtr variable(size_t index)
        c = Component()
        v = Variable()
        name = 'green'
        v.setName(name)
        self.assertIsNone(c.variable(0))
        self.assertIsNone(c.variable(1))
        self.assertIsNone(c.variable(-1))
        c.addVariable(v)
        self.assertIsNone(c.variable(1))
        self.assertIsNone(c.variable(-1))
        self.assertIsNotNone(c.variable(0))
        self.assertEqual(c.variable(0).name(), name)
        del [c, v, name]

        # VariablePtr variable(const std::string &name)
        c = Component()
        v = Variable()
        name = 'green'
        v.setName(name)
        self.assertIsNone(c.variable(name))
        c.addVariable(v)
        self.assertIsNone(c.variable('red'))
        self.assertIsNotNone(c.variable(name))
        self.assertEqual(c.variable(name).name(), name)
Ejemplo n.º 2
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())
Ejemplo n.º 3
0
    def test_variable_count(self):
        from libcellml import Component, Variable

        # size_t variableCount()
        c = Component()
        self.assertEqual(c.variableCount(), 0)
        c.addVariable(Variable())
        self.assertEqual(c.variableCount(), 1)
        c.addVariable(Variable())
        self.assertEqual(c.variableCount(), 2)
        c.removeVariable('')
        self.assertEqual(c.variableCount(), 1)
        c.removeVariable('')
        self.assertEqual(c.variableCount(), 0)
Ejemplo n.º 4
0
    def test_remove_all_variables(self):
        from libcellml import Component, Variable

        # void removeAllVariables()
        c = Component()
        v1 = Variable()
        v2 = Variable()
        c.addVariable(v1)
        c.addVariable(v2)
        self.assertTrue(c.hasVariable(v1))
        self.assertTrue(c.hasVariable(v2))
        c.removeAllVariables()
        self.assertFalse(c.hasVariable(v1))
        self.assertFalse(c.hasVariable(v2))
Ejemplo n.º 5
0
    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()
Ejemplo n.º 6
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())
Ejemplo n.º 7
0
    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())
Ejemplo n.º 8
0
    def test_has_variable(self):
        from libcellml import Component, Variable

        # bool hasVariable(const VariablePtr &variable)
        c = Component()
        v = Variable("second")
        self.assertFalse(c.hasVariable(v))
        c.addVariable(v)
        self.assertTrue(c.hasVariable(v))
        self.assertFalse(c.hasVariable(Variable()))
        del [c, v]

        # bool hasVariable(const std::string &name)
        c = Component()
        self.assertFalse(c.hasVariable(''))
        v1 = Variable()
        c.addVariable(v1)
        self.assertFalse(c.hasVariable('blue'))
        self.assertTrue(c.hasVariable(''))
        name = 'yellow'
        v2 = Variable()
        v2.setName(name)
        v1.setName('orange')
        c.addVariable(v2)
        self.assertTrue(c.hasVariable(name))

        vTaken = c.takeVariable(0)
        self.assertEqual('orange', vTaken.name())
        self.assertTrue(c.variableCount() == 1)
        del [c, v1, v2, vTaken, name]
Ejemplo n.º 9
0
    def test_has_variable(self):
        from libcellml import Component, Variable

        # bool hasVariable(const VariablePtr &variable)
        c = Component()
        v = Variable()
        self.assertFalse(c.hasVariable(v))
        c.addVariable(v)
        self.assertTrue(c.hasVariable(v))
        self.assertFalse(c.hasVariable(Variable()))
        del(c, v)

        # bool hasVariable(const std::string &name)
        c = Component()
        self.assertFalse(c.hasVariable(''))
        v1 = Variable()
        c.addVariable(v1)
        self.assertFalse(c.hasVariable('blue'))
        self.assertTrue(c.hasVariable(''))
        name = 'yellow'
        v2 = Variable()
        v2.setName(name)
        c.addVariable(v2)
        self.assertTrue(c.hasVariable(name))
        del(c, v1, v2, name)
Ejemplo n.º 10
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())
Ejemplo n.º 11
0
    def test_remove_variable(self):
        from libcellml import Component, Variable

        # bool removeVariable(size_t index)
        c = Component()
        self.assertFalse(c.removeVariable(0))
        self.assertFalse(c.removeVariable(-1))
        self.assertFalse(c.removeVariable(1))
        c.addVariable(Variable())
        self.assertFalse(c.removeVariable(-1))
        self.assertFalse(c.removeVariable(1))
        self.assertTrue(c.removeVariable(0))
        self.assertFalse(c.removeVariable(0))
        del c

        # bool removeVariable(const std::string &name)
        c = Component()
        self.assertFalse(c.removeVariable(''))
        v1 = Variable()
        c.addVariable(v1)
        self.assertTrue(c.removeVariable(''))
        self.assertFalse(c.removeVariable(''))
        name = 'blue'
        v1.setName(name)
        self.assertFalse(c.removeVariable(name))
        c.addVariable(v1)
        self.assertTrue(c.removeVariable(name))
        self.assertFalse(c.removeVariable(name))
        del [c, v1, name]

        # bool removeVariable(const VariablePtr &variable)
        c = Component()
        v1 = Variable("second")
        v2 = Variable("meter")
        self.assertFalse(c.removeVariable(v1))
        c.addVariable(v1)
        self.assertFalse(c.removeVariable(v2))
        self.assertTrue(c.removeVariable(v1))
        self.assertFalse(c.removeVariable(v1))
                </apply>"

    #   1.e   Include the MathML strings in the component
    component.setMath(math_header)
    component.appendMath(equation)
    component.appendMath(math_footer)

    #  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)
Ejemplo n.º 13
0
    def test_add_variable(self):
        from libcellml import Component, Variable

        c = Component()
        v = Variable()
        c.addVariable(v)
Ejemplo n.º 14
0
                    </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")
        sodium_channel.addVariable(m)
    #  end 3

    print('----------------------------------------------------------')
    print('   STEP 4: Add the variables                              ')
    print('----------------------------------------------------------')

    #  The issues reported by the validator are related to the MathML string that we entered
    #  in Step 2 requiring variables which don't yet exist. These must be created, named,
    #  and added to their parent component.

    #  4.a
    #      Create items for the missing variables and add them to the gateEquations component.
    #      You will need to be sure to give them names which match exactly those reported by the
    #      validator, or are present in the MathML string.
    gateEquations.addVariable(Variable('t'))
    gateEquations.addVariable(Variable('alpha_X'))
    gateEquations.addVariable(Variable('beta_X'))
    gateEquations.addVariable(Variable('X'))

    #  4.b
    #      Validate again, and expect errors relating to missing units.
    #      Note that you can use the helper function print_issues(validator) to print your
    #      issues to the screen instead of repeating the code from 3.b.
    validator.validateModel(model)
    print_issues(validator)

    #  end 4

    print('----------------------------------------------------------')
    print('   STEP 5: Add the units                                  ')
    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("-----------------------------------------------")

    #  6.a Connecting the membrane to its sibling environment, and the channels to their
    i_K.setUnits("microA_per_cm2")
    # Note that no initial value is needed for this variable as its value is defined by equation2

    g_K = Variable()
    g_K.setName("g_K")
    g_K.setUnits("milliS_per_cm2")
    g_K.setInitialValue(36.0)

    gamma = Variable()
    gamma.setName("gamma")
    gamma.setUnits("dimensionless")
    gamma.setInitialValue(4.0)

    #  3.c Adding the variables to the component.  Note that Variables are
    #      added by their pointer (cf. their name)
    component.addVariable(t)
    component.addVariable(V)
    component.addVariable(E_K)
    component.addVariable(gamma)
    component.addVariable(i_K)
    component.addVariable(g_K)
    component.addVariable(alpha_n)
    component.addVariable(beta_n)
    component.addVariable(n)

    #  3.d Call the validator and print the messages to the terminal.
    #      Expected errors refer to units referred to by these variables, but
    #      which don't (yet) exist in the model.
    validator.validateModel(model)
    print_errors_to_terminal(validator)
    #  We can use these issues as we need to.  The simplest way is to print the descriptions
    #  to the terminal.

    #  2.d
    #      Retrieve the number of issues encountered using the validator.issueCount() function,
    #      then retrieve the issue items from the validator using their index and the validator.issue(index)
    #      function.
    print('The validator has found {} issues.'.format(validator.issueCount()))
    for i in range(0, validator.issueCount()):
        print(validator.issue(i).description())
    print()

    #  2.e 
    #      Create the variables needed and add them to the potassium channel component.
    #      Revalidate and expect errors related to variables without units.
    k_channel_equations.addVariable(Variable('E_K'))
    k_channel_equations.addVariable(Variable('i_K'))
    k_channel_equations.addVariable(Variable('g_K'))
    k_channel_equations.addVariable(Variable('V'))
    k_channel_equations.addVariable(Variable('t'))
    k_channel_equations.addVariable(Variable('n'))

    validator.validateModel(model)
    print_issues(validator)

    #  2.f 
    #      Create the missing Units items and add them to the model. These are:
    #      - milli-volts
    #      - milli-seconds
    #      - milli-moles
    #      - micro-Amperes per square centimetre
Ejemplo n.º 19
0
    print("   Step 2: Create the variables                              ")
    print("-------------------------------------------------------------")

    #  2.a
    #      Create the variables listed by the validator: d, a, b, c, time, y_s, y_f.
    sharks = Variable("y_s")
    fish = Variable("y_f")
    time = Variable("time")
    a = Variable("a")
    b = Variable("b")
    c = Variable("c")
    d = Variable("d")

    #  2.b
    #      Add the variables into the component.
    component.addVariable(sharks)
    component.addVariable(fish)
    component.addVariable(time)
    component.addVariable(a)
    component.addVariable(b)
    component.addVariable(c)
    component.addVariable(d)

    #  2.c
    #      Call the validator again to check the model.
    validator.validateModel(model)
    print_issues(validator)

    #  end 2

    print("-------------------------------------------------------------")