def test_timeout(self, timeout=3):
     model = 'MyModelicaLibrary.MyModelTimeOut'
     s = Simulator(model, packagePath=self._packagePath)
     s._deleteTemporaryDirectory = False
     outDir = os.path.abspath(s.getOutputDirectory())
     s.setTimeOut(timeout)
     with self.assertRaises(TimeoutError):
         s.simulate()
     with open(os.path.join(outDir, s._reporter._logFil)) as fh:
         log = fh.read()
     self.assertTrue('Terminating simulation' in log
                     and 'Process timeout' in log)
     s.setTimeOut(-1)
     s.simulate()
     with open(os.path.join(outDir, 'dslog.txt')) as fh:
         log = fh.read()
     self.assertTrue('Integration terminated successfully' in log)
    def test_addMethods(self):
        """
        Tests the various add methods.
        """
        import numpy as np

        from buildingspy.io.outputfile import Reader

        s = Simulator("MyModelicaLibrary.MyModel",
                      packagePath=self._packagePath)
        s.addPreProcessingStatement("Advanced.StoreProtectedVariables:= true;")
        s.addPostProcessingStatement(
            "Advanced.StoreProtectedVariables:= false;")
        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.exitSimulator(True)
        # s.deleteOutputFiles()
        s.showGUI(False)
        #        s.printModelAndTime()
        s.showProgressBar(False)
        s.simulate()
        # 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.725, r.mean('source.y'))
        np.testing.assert_allclose(0.725 * 6, r.integral('source.y'))
        np.testing.assert_allclose(-0.1, r.min('source.y'))
        # Delete output files
        s.deleteOutputFiles()