示例#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)
示例#2
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]
示例#3
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)
示例#4
0
    def test_variable(self):
        from libcellml import Issue, Variable

        e = Issue()
        self.assertIsNone(e.variable())
        name = 'var'
        v = Variable()
        v.setName(name)
        e.setVariable(v)
        self.assertIsInstance(e.variable(), Variable)
        self.assertEqual(e.variable().name(), name)
示例#5
0
    def test_get_variable(self):
        from libcellml import Error, Variable

        # VariablePtr getVariable()
        e = Error()
        self.assertIsNone(e.getVariable())
        name = 'var'
        v = Variable()
        v.setName(name)
        e.setVariable(v)
        self.assertIsInstance(e.getVariable(), Variable)
        self.assertEqual(e.getVariable().getName(), name)
示例#6
0
    def test_set_get_test_variable(self):
        from libcellml import Reset
        from libcellml import Variable

        r = Reset()
        v = Variable()
        v.setName("glucose")

        self.assertEqual(None, r.testVariable())

        r.setTestVariable(v)

        self.assertEqual("glucose", r.testVariable().name())
示例#7
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))
示例#8
0
    def test_minimum_interface_type(self):
        from libcellml import Variable

        v_public = Variable()
        v_public.setName("v_public")
        v_public.setInterfaceType("public")

        v_private = Variable()
        v_public.setName("v_private")
        v_private.setInterfaceType("private")

        v_public_and_private = Variable()
        v_public_and_private.setName("v_public_and_private")
        v_public_and_private.setInterfaceType("public_and_private")

        v_none = Variable()
        v_none.setName("v_none")
        v_none.setInterfaceType("none")

        v_empty = Variable()
        v_empty.setName("v_empty")

        # Stored public_and_private meets all requirements.
        self.assertTrue(
            v_public_and_private.permitsInterfaceType(
                Variable.InterfaceType.NONE))
        self.assertTrue(
            v_public_and_private.permitsInterfaceType(
                Variable.InterfaceType.PRIVATE))
        self.assertTrue(
            v_public_and_private.permitsInterfaceType(
                Variable.InterfaceType.PUBLIC))
        self.assertTrue(
            v_public_and_private.permitsInterfaceType(
                Variable.InterfaceType.PUBLIC_AND_PRIVATE))

        # Stored private meets private and none requirements.
        self.assertTrue(
            v_private.permitsInterfaceType(Variable.InterfaceType.NONE))
        self.assertTrue(
            v_private.permitsInterfaceType(Variable.InterfaceType.PRIVATE))
        self.assertFalse(
            v_private.permitsInterfaceType(Variable.InterfaceType.PUBLIC))
        self.assertFalse(
            v_private.permitsInterfaceType(
                Variable.InterfaceType.PUBLIC_AND_PRIVATE))

        # Stored public meets public and none requirements.
        self.assertTrue(
            v_public.permitsInterfaceType(Variable.InterfaceType.NONE))
        self.assertFalse(
            v_public.permitsInterfaceType(Variable.InterfaceType.PRIVATE))
        self.assertTrue(
            v_public.permitsInterfaceType(Variable.InterfaceType.PUBLIC))
        self.assertFalse(
            v_public.permitsInterfaceType(
                Variable.InterfaceType.PUBLIC_AND_PRIVATE))

        # Stored none meets none requirements.
        self.assertTrue(
            v_none.permitsInterfaceType(Variable.InterfaceType.NONE))
        self.assertFalse(
            v_none.permitsInterfaceType(Variable.InterfaceType.PRIVATE))
        self.assertFalse(
            v_none.permitsInterfaceType(Variable.InterfaceType.PUBLIC))
        self.assertFalse(
            v_none.permitsInterfaceType(
                Variable.InterfaceType.PUBLIC_AND_PRIVATE))

        # Stored empty meets none requirements.
        self.assertTrue(
            v_empty.permitsInterfaceType(Variable.InterfaceType.NONE))
        self.assertFalse(
            v_empty.permitsInterfaceType(Variable.InterfaceType.PRIVATE))
        self.assertFalse(
            v_empty.permitsInterfaceType(Variable.InterfaceType.PUBLIC))
        self.assertFalse(
            v_empty.permitsInterfaceType(
                Variable.InterfaceType.PUBLIC_AND_PRIVATE))
示例#9
0
                        <ci>V</ci>\
                        <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")
                    </apply>\
                </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)
    print("\n-----------------------------------------------")
    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")
    #  2.c:  Call the validator and print the messages to the terminal.
    #        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")