Example #1
0
    def test_model(self):
        from libcellml import ImportSource, Model

        model = Model()
        model.setName('bert')
        x = ImportSource()
        self.assertIsNone(x.model())
        x.setModel(model)
        self.assertEqual(x.model().name(), model.name())
        x.setModel(None)
        self.assertIsNone(x.model())
Example #2
0
    def test_create_destroy(self):
        import libcellml
        from libcellml import Model

        x = Model()
        del x

        y = Model('bob')
        self.assertEqual('bob', y.name())

        z = libcellml.Model()
        del z
Example #3
0
                        <apply><lt/><ci>t</ci><cn cellml:units="ms">5</cn></apply>\
                    </piece>\
                        <piece>\
                        <cn cellml:units="mV">-85</cn>\
                        <apply><gt/><ci>t</ci><cn cellml:units="ms">15</cn></apply>\
                    </piece>\
                    <otherwise>\
                        <cn cellml:units="mV">-20</cn>\
                    </otherwise>\
                </piecewise>\
            </apply>'

        environment.setMath(math_header)
        environment.appendMath(voltageClampMaths)
        environment.appendMath(math_footer)

    #  6.b Validate the final model
    validator.validateModel(model)
    print_errors_to_terminal(validator)

    print("-----------------------------------------------")
    print("    STEP 7: Serialise and print the model ")
    print("-----------------------------------------------")

    printer = Printer()
    serialised_model = printer.printModel(model)
    out_file = "tutorial7_SodiumChannelModel.cellml"
    write_file = open(out_file, "w")
    write_file.write(serialised_model)
    print("The {m} has been printed to {n}".format(m=model.name(), n=out_file))
    generator.processModel(model)
    print_errors_to_terminal(generator)

    #  3.c Check that the generator has the settings which we expect:
    #      - the number of variables
    #      - the language of output
    #      - the variable of integration
    #      - the type of model
    print("-----------------------------------------------------")
    print("       Investigating the Generator settings          ")
    print("-----------------------------------------------------")

    print("Number of variables = {}".format(generator.variableCount()))
    print("Variable of integration = {}".format(generator.voi().name()))
    print("Number of states = {}".format(generator.stateCount()))
    # print("Model type = " + get_model_type_from_enum(generator.modelType()))

    #  3.d Change the Generator profile to be Python instead of the default C
    profile = GeneratorProfile(GeneratorProfile.Profile.PYTHON)
    generator.setProfile(profile)

    #  3.e Create the implementation code and print to a Python file

    implementation_code = generator.implementationCode()
    write_file = open("tutorial3_generated.py", "w")
    write_file.write(implementation_code)
    print("The {} has been printed to tutorial3_generated.py".format(
        model.name()))

    #  3.e Go have a cuppa, you're done!
                    <apply><lt/><ci>t</ci><cn cellml:units="ms">1</cn></apply>\
                </piece>\
                    <piece>\
                    <cn cellml:units="microA_per_cm2">0</cn>\
                    <apply><gt/><ci>t</ci><cn cellml:units="ms">1.2</cn></apply>\
                </piece>\
                <otherwise>\
                    <cn cellml:units="microA_per_cm2">100</cn>\
                </otherwise>\
            </piecewise>\
        </apply>'

    #  7.b Add before the closing </math> tag in the membrane component and print to check it
    membrane_math = membrane.math()
    membrane_math = insert_into_mathml_string(membrane_math, stimulusEquation)
    membrane.setMath(membrane_math)
    print(membrane_math)

    validator.validateModel(model)
    print_errors_to_terminal(validator)

    print("\n-----------------------------------------------")
    print("   STEP 8: Output the final model")
    print("-----------------------------------------------")

    printer = Printer()
    serialised_model = printer.printModel(model)
    write_file = open("tutorial8_HodgkinHuxleyModel.cellml", "w")
    write_file.write(serialised_model)
    print("The {} has been printed to tutorial8_HodgkinHuxleyModel.cellml".format(model.name()))
Example #6
0
    def test_type_based_retrieval(self):
        from libcellml import Annotator, Model, Parser

        annotator = Annotator()
        model = Model()
        parser = Parser()

        model_string = file_contents("annotator/unique_ids.cellml")

        model = parser.parseModel(model_string)
        annotator.setModel(model)

        v1v1 = (model.component("component2").variable("variable1"),
                model.component("component2").component("component3").variable(
                    "variable1"))
        v2v2 = (model.component("component2").variable("variable2"),
                model.component("component2").component("component3").variable(
                    "variable2"))

        self.assertEqual(model.name(), annotator.model("model_1").name())
        self.assertEqual(model.name(),
                         annotator.encapsulation("encapsulation_1").name())
        self.assertEqual(
            model.component("component1").name(),
            annotator.component("component_1").name())
        self.assertEqual(
            model.component("component2").name(),
            annotator.component("component_2").name())
        self.assertEqual(
            model.component("component2").name(),
            annotator.component("component_ref_1").name())
        self.assertEqual(
            model.component("component2").component("component3").name(),
            annotator.component("component_3").name())
        self.assertEqual(
            model.component("component2").component("component3").name(),
            annotator.componentRef("component_ref_2").name())
        self.assertEqual(
            model.component("component1").importSource().url(),
            annotator.importSource("import_1").url())
        self.assertEqual(
            model.units("units1").name(),
            annotator.units("units_1").name())
        self.assertEqual(
            model.units("units1").importSource().url(),
            annotator.importSource("import_2").url())
        self.assertEqual(
            model.units("units2").name(),
            annotator.units("units_2").name())
        self.assertEqual(
            model.units("units2").name(),
            annotator.unit("unit_1").units().name())
        self.assertEqual(0, annotator.unit("unit_1").index())

        self.assertEqual(
            model.component("component2").variable("variable1").name(),
            annotator.variable("variable_1").name())

        self.assertEqual(
            model.component("component2").variable("variable2").name(),
            annotator.variable("variable_2").name())
        self.assertEqual(
            model.component("component2").reset(0).variable().name(),
            annotator.reset("reset_1").variable().name())
        self.assertEqual(
            model.component("component2").reset(0).testVariable().name(),
            annotator.reset("reset_1").testVariable().name())
        self.assertEqual(
            model.component("component2").reset(0).testValue(),
            annotator.testValue("test_value_1").testValue())
        self.assertEqual(
            model.component("component2").reset(0).resetValue(),
            annotator.resetValue("reset_value_1").resetValue())

        self.assertEqual(
            model.component("component2").component("component3").variable(
                "variable1").name(),
            annotator.variable("variable_3").name())
        self.assertEqual(
            model.component("component2").component("component3").variable(
                "variable2").name(),
            annotator.variable("variable_4").name())
        self.assertEqual(
            v1v1[0].name(),
            annotator.connection("connection_1").variable1().name())

        self.assertEqual(
            v1v1[1].name(),
            annotator.connection("connection_1").variable2().name())
        self.assertEqual(
            v1v1[0].name(),
            annotator.mapVariables("map_variables_1").variable1().name())
        self.assertEqual(
            v1v1[1].name(),
            annotator.mapVariables("map_variables_1").variable2().name())
        self.assertEqual(
            v2v2[0].name(),
            annotator.mapVariables("map_variables_2").variable1().name())
        self.assertEqual(
            v2v2[1].name(),
            annotator.mapVariables("map_variables_2").variable2().name())

        self.assertIsNone(annotator.model("i_dont_exist"))
        self.assertIsNone(annotator.component("i_dont_exist"))
        self.assertIsNone(annotator.variable("i_dont_exist"))
        self.assertIsNone(annotator.units("i_dont_exist"))
        self.assertIsNone(annotator.unit("i_dont_exist"))
        self.assertIsNone(annotator.reset("i_dont_exist"))
        self.assertIsNone(annotator.resetValue("i_dont_exist"))
        self.assertIsNone(annotator.testValue("i_dont_exist"))
        self.assertIsNone(annotator.componentRef("i_dont_exist"))
        self.assertIsNone(annotator.connection("i_dont_exist"))
        self.assertIsNone(annotator.importSource("i_dont_exist"))