def main():
    """ Main method that configures and runs all simulations
    """
    import shutil

    # Build list of cases to run
    li = []
    # First model, from Modelica Buildings Library, v7.0.0
    model = 'Buildings.Controls.Continuous.Examples.PIDHysteresis'
    s = Simulator(model, 'case1')
    s.addParameters({'con.eOn': 0.1})
    li.append(s)
    # second model
    s = Simulator(model, 'case2')
    s.addParameters({'con.eOn': 1})
    li.append(s)

    # Run all cases in parallel
    po = Pool()
    po.map(simulateCase, li)
    po.close()
    po.join()

    # Clean up
    shutil.rmtree('case1')
    shutil.rmtree('case2')
    def test_setBooleanParameterValues(self):
        """
        Tests the :mod:`buildingspy.simulate.Dymola.addParameters`
        function for boolean parameters.
        """

        from buildingspy.io.outputfile import Reader
        # Delete output file
        resultFile = os.path.join("BooleanParameters.mat")

        if os.path.exists(resultFile):
            os.remove(resultFile)

        s = Simulator("MyModelicaLibrary.Examples.BooleanParameters",
                      packagePath=self._packagePath)
        s.addParameters({'p1': True})
        s.addParameters({'p2': False})
        s.simulate()

        r = Reader(resultFile, "dymola")

        (_, p) = r.values('p1')
        self.assertEqual(p[0], 1.0)
        (_, p) = r.values('p2')
        self.assertEqual(p[0], 0.0)
        # Delete output files
        s.deleteOutputFiles()
    def test_raisesAssertionIfWrongDataType(self):
        """
        Tests the :mod:`buildingspy.simulate.Dymola.simulate`
        function to make sure it raises an assertion if a model fails to translate.
        """
        model = "MyModelicaLibrary.Examples.BooleanParameters"

        s = Simulator(model, packagePath=self._packagePath)
        s.addParameters(
            {'p1':
             123})  # p1 is a boolean parameter. This will fail the model.
        with self.assertRaises(Exception):
            s.simulate()
Exemple #4
0
 def test_addGetParameters(self):
     """
     Tests the :mod:`buildingspy.simulate.Dymola.addParameters`
     and the :mod:`buildingspy.simulate.Dymola.getParameters`
     functions.
     """
     s = Simulator("myPackage.myModel", packagePath=self._packagePath)
     # Make sure values are added correctly
     s.addParameters({'PID.k': 1.0, 'valve.m_flow_nominal': 0.1})
     self.assertEqual(sorted(s.getParameters()), [('PID.k', 1.0), ('valve.m_flow_nominal', 0.1)])
     # Add one more parameter
     s.addParameters({'PID.t': 10.0})
     self.assertEqual(sorted(s.getParameters()), [
                      ('PID.k', 1.0), ('PID.t', 10.0), ('valve.m_flow_nominal', 0.1)])
     # Arguments must be a dictionary
     self.assertRaises(ValueError, s.addParameters, ["aaa", "bbb"])
    def test_addVectorOfParameterValues(self):
        """
        Tests the :mod:`buildingspy.simulate.Dymola.addParameters`
        function for the situation where values for a parameter that is
        a vector is added.
        """
        import numpy as np
        from buildingspy.io.outputfile import Reader
        # Delete output file
        resultFile = os.path.join("Constants.mat")
        if os.path.exists(resultFile):
            os.remove(resultFile)

        s = Simulator("MyModelicaLibrary.Examples.Constants",
                      packagePath=self._packagePath)
        s.addParameters({'const1.k': [2, 3]})
        s.addParameters({'const2.k': [[1.1, 1.2], [2.1, 2.2], [3.1, 3.2]]})
        s.addParameters({'const3.k': 0})
        s.simulate()

        r = Reader(resultFile, "dymola")

        np.testing.assert_allclose(2, r.max('const1[1].y'))
        np.testing.assert_allclose(3, r.max('const1[2].y'))

        np.testing.assert_allclose(1.1, r.max('const2[1, 1].y'))
        np.testing.assert_allclose(1.2, r.max('const2[1, 2].y'))
        np.testing.assert_allclose(2.1, r.max('const2[2, 1].y'))
        np.testing.assert_allclose(2.2, r.max('const2[2, 2].y'))
        np.testing.assert_allclose(3.1, r.max('const2[3, 1].y'))
        np.testing.assert_allclose(3.2, r.max('const2[3, 2].y'))

        np.testing.assert_allclose(0, r.max('const3.y'))
        # Delete output files
        s.deleteOutputFiles()