def test_translate_simulate(self):
        '''
        Tests the :mod:`buildingspy.simulate.Simulator.translate` and
        the :mod:`buildingspy.simulate.Simulator.simulate_translated`
        method.
        '''
        import numpy as np

        from buildingspy.io.outputfile import Reader

        s = Simulator("MyModelicaLibrary.MyModel",
                      "dymola",
                      packagePath=self._packagePath)
        s.addModelModifier(
            "redeclare Modelica.Blocks.Sources.Step source(offset=-0.1, height=1.1, startTime=0.5)"
        )
        s.setStartTime(-1)
        s.setStopTime(5)
        s.setTimeOut(600)
        s.setTolerance(1e-4)
        s.setSolver("dassl")
        s.setNumberOfIntervals(50)
        s.setResultFile("myResults")
        s.translate()
        s.simulate_translated()

        # Read the result and test their validity
        outDir = s.getOutputDirectory()
        resultFile = os.path.abspath(os.path.join(outDir, "myResults.mat"))
        r = Reader(resultFile, "dymola")
        np.testing.assert_allclose(1.0, r.max('source.y'))
        np.testing.assert_allclose(-0.1, r.min('source.y'))
        np.testing.assert_allclose(0.725, r.mean('source.y'))
        np.testing.assert_allclose(0.725 * 6, r.integral('source.y'))
        # Delete output files
        s.deleteOutputFiles()
        s.deleteLogFiles()

        # Make another simulation, but now the start time is at -2, and the height is 2.1
        s.setStartTime(-2)
        s.addParameters({'source.height': 2.1})
        s.simulate_translated()
        outDir = s.getOutputDirectory()
        resultFile = os.path.abspath(os.path.join(outDir, "myResults.mat"))
        r = Reader(resultFile, "dymola")
        np.testing.assert_allclose(2.0, r.max('source.y'))
        np.testing.assert_allclose(-0.1, r.min('source.y'))
        np.testing.assert_allclose(1.25, r.mean('source.y'))
        np.testing.assert_allclose(7 * 1.25, r.integral('source.y'))

        # clean up translate temporary dir
        s.deleteOutputFiles()
        s.deleteLogFiles()
        s.deleteTranslateDirectory()
    def test_translate_simulate(self):
        """
        Tests the :mod:`buildingspy.simulate.Simulator.translate` and
        the :mod:`buildingspy.simulate.Simulator.simulate_translated`
        method.
        """
        import numpy as np

        from buildingspy.io.outputfile import Reader

        s = Simulator("MyModelicaLibrary.MyModel", "dymola", packagePath=self._packagePath)
        s.addModelModifier(
            "redeclare Modelica.Blocks.Sources.Step source(offset=-0.1, height=1.1, startTime=0.5)")
        s.setStartTime(-1)
        s.setStopTime(5)
        s.setTimeOut(600)
        s.setTolerance(1e-4)
        s.setSolver("dassl")
        s.setNumberOfIntervals(50)
        s.setResultFile("myResults")
        s.translate()
        s.simulate_translated()

        # Read the result and test their validity
        outDir = s.getOutputDirectory()
        resultFile = os.path.abspath(os.path.join(outDir, "myResults.mat"))
        r = Reader(resultFile, "dymola")
        np.testing.assert_allclose(1.0, r.max('source.y'))
        np.testing.assert_allclose(-0.1, r.min('source.y'))
        np.testing.assert_allclose(0.725, r.mean('source.y'))
        np.testing.assert_allclose(0.725 * 6, r.integral('source.y'))
        # Delete output files
        s.deleteOutputFiles()
        s.deleteLogFiles()

        # Make another simulation, but now the start time is at -2, and the height is 2.1
        s.setStartTime(-2)
        s.addParameters({'source.height': 2.1})
        s.simulate_translated()
        outDir = s.getOutputDirectory()
        resultFile = os.path.abspath(os.path.join(outDir, "myResults.mat"))
        r = Reader(resultFile, "dymola")
        np.testing.assert_allclose(2.0, r.max('source.y'))
        np.testing.assert_allclose(-0.1, r.min('source.y'))
        np.testing.assert_allclose(1.25, r.mean('source.y'))
        np.testing.assert_allclose(7 * 1.25, r.integral('source.y'))

        # clean up translate temporary dir
        s.deleteOutputFiles()
        s.deleteLogFiles()
        s.deleteTranslateDirectory()
    def test_translate_simulate_exception_parameter(self):
        """
        Tests the :mod:`buildingspy.simulate.Simulator.translate` and
        the :mod:`buildingspy.simulate.Simulator.simulate_translated`
        method.
        This tests whether an exception is thrown if
        one attempts to change a parameter that is fixed after compilation
        """
        import numpy as np
        from buildingspy.io.outputfile import Reader

        s = Simulator("MyModelicaLibrary.Examples.ParameterEvaluation",
                      "dymola", packagePath=self._packagePath)
        s.translate()
        s.setSolver("dassl")
        desired_value = 0.2
        s.addParameters({'x': desired_value})
        # Simulate the model with new parameter and check in the output file
        # whether the parameter is really set.
        # Dymola 2016 FD01 sets it correctly, but Dymola 2016 does not.
        try:
            s.simulate_translated()
            # No exception. Check results
            actual_res = Reader(os.path.join(".",
                                             'ParameterEvaluation.mat'),
                                'dymola')
            (_, y) = actual_res.values('x')
            (_, n) = actual_res.values('n')
            np.testing.assert_allclose(y[0], desired_value)
            np.testing.assert_allclose(n[0], 5)
        except IOError as e:
            # An IOError was raised. Make sure it is raised by simulate_translated
            print(("Caught IOError with message '{}'".format(e)))
            self.assertRaises(IOError, s.simulate_translated)
        # clean up translate temporary dir
        s.deleteTranslateDirectory()
        # Delete output files
        s.deleteOutputFiles()
        s.deleteLogFiles()
        # This is called to clean up after an exception in simulate_translated().
        s.deleteSimulateDirectory()
    def test_translate_simulate_exception_parameter(self):
        '''
        Tests the :mod:`buildingspy.simulate.Simulator.translate` and
        the :mod:`buildingspy.simulate.Simulator.simulate_translated`
        method.
        This tests whether an exception is thrown if
        one attempts to change a parameter that is fixed after compilation
        '''
        import numpy as np
        from buildingspy.io.outputfile import Reader

        s = Simulator("MyModelicaLibrary.Examples.ParameterEvaluation",
                      "dymola",
                      packagePath=self._packagePath)
        s.translate()
        s.setSolver("dassl")
        desired_value = 0.2
        s.addParameters({'x': desired_value})
        # Simulate the model with new parameter and check in the output file
        # whether the parameter is really set.
        # Dymola 2016 FD01 sets it correctly, but Dymola 2016 does not.
        try:
            s.simulate_translated()
            # No exception. Check results
            actual_res = Reader(os.path.join(".", 'ParameterEvaluation.mat'),
                                'dymola')
            (_, y) = actual_res.values('x')
            (_, n) = actual_res.values('n')
            np.testing.assert_allclose(y[0], desired_value)
            np.testing.assert_allclose(n[0], 5)
        except IOError as e:
            # An IOError was raised. Make sure it is raised by simulate_translated
            print(("Caught IOError with message '{}'".format(e)))
            self.assertRaises(IOError, s.simulate_translated)
        # clean up translate temporary dir
        s.deleteTranslateDirectory()
        # Delete output files
        s.deleteOutputFiles()
        s.deleteLogFiles()
        # This is called to clean up after an exception in simulate_translated().
        s.deleteSimulateDirectory()