def test_addVectorOfParameterValues(self): ''' Tests the :mod:`buildingspy.simulate.Simulator.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", "dymola", 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() s.deleteLogFiles()
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()
def ReadMat(dirname, matFileName, readVar, pipeList, modellist):#, lifetime, upv_File, co2_rate_file, co2_pricing_file, startYear): # read the MAT_Result File of the Modelica Simulation TA_data = [] for case in os.listdir(dirname): if 'case' in case: matFile = dirname + "/" + case + "/" + matFileName r = Reader(matFile, "dymola") data = {'name': case} for v in readVar: if v[1] == 'max': data.update({v[2]: r.max(v[0])}) elif v[1] == 'min': data.update({v[2]: r.min(v[0])}) elif v[1] == 'integral': data.update({v[2]: r.integral(v[0])}) # if pipe is active, add its pipe length to the total pipe length pipeLength = 0 for pipe in pipeList: data.update({pipe.name: r.max((pipe.name + ".active"))}) if data[pipe.name] == 1: pipeLength += r.max((pipe.name + ".length")) pipeDiameter = r.max((pipe.name + ".diameter")) data.update({'Total_Pipe_Length': pipeLength}) for m in modellist: data.update({m.name: r.max((m.name + ".connected"))}) TA_data.append(data) return TA_data
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_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.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-8) s.setSolver("Radau5ODE") s.setNumberOfIntervals(50) s.setResultFile("myResults") 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()
def ReadMat(dirname, matFileName, resultVar, pipeList, modellist): TA_data = [] for case in os.listdir(dirname): if 'case' in case: matFile = dirname + "/" + case + "/" + matFileName r = Reader(matFile, "dymola") data = {'name': case} for v in resultVar: if v[1] == 'max': data.update({v[0]: r.max(v[0])}) elif v[1] == 'min': data.update({v[0]: r.min(v[0])}) elif v[1] == 'integral': data.update({v[0]: r.integral(v[0])}) pipeLength = 0 for pipe in pipeList: data.update({pipe.name: r.max((pipe.name + ".active"))}) if data[pipe.name] == 1: pipeLength += r.max((pipe.name + ".length")) data.update({'Total_Pipe_Length': pipeLength}) for m in modellist: data.update({m.name: r.max((m.name + ".connected"))}) TA_data.append(data) return TA_data
def test_addMethods(self): ''' Tests the various add methods. ''' import numpy as np from buildingspy.io.outputfile import Reader s = Simulator("MyModelicaLibrary.MyModel", "dymola", 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() s.deleteLogFiles()
def test_addMethods(self): ''' Tests the various add methods. ''' import os import numpy as np from buildingspy.io.outputfile import Reader s = Simulator("MyModelicaLibrary.MyModel", "dymola") 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() s.deleteLogFiles()
def _profile_dymola(result): ''' Run simulation with dymola. The function returns CPU time used for compile and simulation. ''' import datetime import time from buildingspy.simulate.Simulator import Simulator from buildingspy.io.outputfile import Reader model=result['model'] modelName = model.split(".")[-1] worDir = create_working_directory() # Update MODELICAPATH to get the right library version s=Simulator(model, "dymola", outputDirectory=worDir) s.setSolver(result['solver']) s.setStartTime(result['start_time']) s.setStopTime(result['stop_time']) s.setTolerance(result['tolerance']) timeout = result['timeout'] if float(timeout) > 0.01: s.setTimeOut(timeout) tstart_tr = datetime.datetime.now() s.simulate() tend_tr = datetime.datetime.now() # total time tTotTim = (tend_tr-tstart_tr).total_seconds() resultFile = os.path.join(worDir, "{}.mat".format(modelName)) # In case of timeout or error, the output file may not exist if not os.path.exists(resultFile): shutil.rmtree(worDir) return {'tTra': 0, 'tCPU': 0, 'nTimeEvent': 0, 'nStateEvent': 0, 'nStepEvent': 0} r=Reader(resultFile, "dymola") tCPU=r.max("CPUtime") tTra = tTotTim-tCPU nEve=r.max('EventCounter') eveLog = N.zeros(3) searchEve = list() searchEve.append("Number of (model) time events :") searchEve.append("Number of time events :") searchEve.append("Number of step events :") # ------ search and retrieve times from compile log file ------ with open(os.path.join(worDir,'dslog.txt'), "r") as f: for line in f: for index, strLin in enumerate(searchEve): if strLin in line: sect1 = line.split(": ") sect2 = sect1[1].split("\n") eveLog[index] = sect2[0] f.close() shutil.rmtree(worDir) return {'tTra': float(tTra), 'tCPU': float(tCPU), 'nTimeEvent': float(eveLog[0]), 'nStateEvent': float(eveLog[1]), 'nStepEvent': float(eveLog[2])}