Skip to content

Commit

Permalink
adding modelica test model for testing modelica model code (Modeling.…
Browse files Browse the repository at this point in the history
…py) #68
  • Loading branch information
Nadja-R committed Nov 15, 2019
1 parent 72490d1 commit aacddd0
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 72 deletions.
37 changes: 37 additions & 0 deletions pyDMPC/tests/Testing/Subsystem/ModelicaTest.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
within Testing.Subsystem;
model ModelicaTest
Modelica.Blocks.Sources.Constant Test(k=20)
annotation (Placement(transformation(extent={{-32,-42},{42,32}})));
Modelica.Blocks.Sources.CombiTimeTable variation(
tableOnFile=true,
extrapolation=Modelica.Blocks.Types.Extrapolation.HoldLastPoint,
tableName="tab1",
columns=2:3,
fileName=ModelicaServices.ExternalReferences.loadResource(
"modelica://Testing/Subsystem/variation.txt"))
"Table with control input"
annotation (
Placement(transformation(
extent={{-10,-10},{10,10}},
rotation=0,
origin={-82,74})));
Modelica.Blocks.Sources.CombiTimeTable decisionVariables(
tableOnFile=true,
extrapolation=Modelica.Blocks.Types.Extrapolation.HoldLastPoint,
tableName="tab1",
smoothness=Modelica.Blocks.Types.Smoothness.ConstantSegments,
columns={2},
fileName=ModelicaServices.ExternalReferences.loadResource(
"modelica://Testing/Subsystem/decisionVariables.txt"))
"Table with decision variables" annotation (Placement(
transformation(
extent={{-10,-10},{10,10}},
rotation=0,
origin={-44,74})));
annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
coordinateSystem(preserveAspectRatio=false)),
experiment(
StopTime=3600,
Interval=10,
Tolerance=0.001));
end ModelicaTest;
3 changes: 3 additions & 0 deletions pyDMPC/tests/Testing/Subsystem/package.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
within Testing;
package Subsystem
end Subsystem;
4 changes: 4 additions & 0 deletions pyDMPC/tests/Testing/package.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
within ;
package Testing
annotation (uses(Modelica(version="3.2.3")));
end Testing;
140 changes: 68 additions & 72 deletions pyDMPC/tests/test_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TestStatesClass(unittest.TestCase):

def test_states(self):

print(Init.sys_id[-1])
for i in Init.sys_id:
with self.subTest(i=i):

Expand All @@ -31,6 +32,8 @@ def test_states(self):
self.assertIsInstance(self.model.outputs, list)
self.assertIsInstance(self.model.state_vars, list)
self.assertIsInstance(self.model.commands, list)

#print('States Class Test successful')

class TestTimesClass (unittest.TestCase):

Expand All @@ -46,10 +49,12 @@ def test_times(self):
self.assertEqual(Init.incr[i], self.model.incr)
self.assertEqual(Init.opt_time[i], self.model.opt_time)
self.assertEqual(Init.samp_time[i], self.model.samp_time)

#print('Times Class Test successful')

class TestPathsClass (unittest.TestCase):

def test_times(self):
def test_paths(self):

for i in Init.sys_id:
with self.subTest(i=i):
Expand All @@ -60,6 +65,8 @@ def test_times(self):
self.assertEqual(Init.res_path[i] + "\\" + Init.name[i], self.model.res_path)
self.assertEqual(Init.dym_path[i], self.model.dym_path)
self.assertEqual(Init.mod_path[i], self.model.mod_path)

#print('Paths Class Test successful')

class TestModelClass (unittest.TestCase):

Expand All @@ -74,6 +81,8 @@ def test_model(self):
self.assertIsInstance(self.model.states, Modeling.States)
self.assertIsInstance(self.model.times, Modeling.Times)
self.assertIsInstance(self.model.paths, Modeling.Paths)

#print('Model Class Test successful')

class TestModifsClass (unittest.TestCase):

Expand All @@ -85,8 +94,10 @@ def test_modifs (self):
self.modifs = Modeling.Modifs(i)

self.assertEqual(self.modifs.factors, Init.factors[i])

#print('Modifs Class Test successful')


class TestModelicaModClass (unittest.TestCase):

def test_Init(self):
Expand All @@ -99,87 +110,57 @@ def test_Init(self):
self.assertEqual(Init.model_type[i], self.model.model_type)
self.assertIsInstance(self.model.states, Modeling.States)
self.assertIsInstance(self.model.times, Modeling.Times)
self.assertIsInstance(self.model.paths, Modeling.Paths)

self.assertIsInstance(self.model.paths, Modeling.Paths)

#print('Modelica Init Function Test successful')

def test_makeDymola(self):

for i in Init.sys_id:
with self.subTest(i=i):
i = Init.sys_id[-1]

self.model = Modeling.ModelicaMod(i)
self.model = Modeling.ModelicaMod(i)
self.dymola = self.model.make_dymola()

self.assertEqual(self.model.dym_path, Init.glob_dym_path)
self.assertEqual(self.model.lib_paths, Init.glob_lib_paths)
self.assertEqual(self.model.dymola, None)
def testDelDymola(self):
self.assertEqual(self.model.dym_path, Init.glob_dym_path)
self.assertEqual(self.model.lib_paths, Init.glob_lib_paths)
self.assertNotEqual(self.model.dymola, None)

#print('Modelica Make Dymola Function Test successful')

for i in Init.sys_id:
with self.subTest(i=i):
self.translate = self.model.translate()
self.simulate = self.model.simulate()
results = self.model.get_results(Init.output_names[i][0])

self.assertEqual(results[0], 20)

self.model = Modeling.ModelicaMod(i)
self.delete = self.model.del_dymola()

self.assertEqual(self.delete, None) #überprüfen, ob dymola auf None gesetzt wurde
def test_delDymola(self):

def test_translate(self):

for i in Init.sys_id:
with self.subTest(i=i):

self.model = Modeling.ModelicaMod(i)
#for change directory
self.assertEqual(Init.res_path[i] + "\\" + Init.name[i], self.model.paths.res_path)
#for translating Model
self.assertEqual(Init.mod_path[i], self.model.paths.mod_path)

def test_simulate(self):

for i in Init.sys_id:
with self.subTest(i=i):
i = Init.sys_id[-1]

self.model = Modeling.ModelicaMod(i)
self.delete = self.model.del_dymola()

self.model = Modeling.ModelicaMod(i)
self.assertEqual(self.model.dymola, None) #überprüfen, ob dymola auf None gesetzt wurde

self.dymola = self.model.make_dymola()
self.translate = self.model.translate()
self.simulate = self.model.simulate()

#self.assertEqual(self.simulate.command_variables, "decisionVariables.table[1,2]")

def test_sim(self):


import sys
import os
#print('Modelica Delete Dymola Function Test successful')

sys.path.insert(0, os.path.join(Init.glob_dym_path))

from dymola.dymola_interface import DymolaInterface

path = r'N:\Forschung\EBC0332_BMWi_MODI_GA\Students\mba-nre\01-notes\HiWi Nadja\Git\pyDMPC\pyDMPC\tests'
def test_translate(self):

dymola = DymolaInterface()
print(dymola)
check = dymola.openModel(os.path.join(path, 'TestModel.mo'))
print('Opening successful ' + str(check))
dymola.cd(Init.glob_res_path)
check2 = dymola.translateModel('TestModel')
print("Translation successful " + str(check2))
output = dymola.simulateExtendedModel(
problem = 'TestModel',
startTime = 0.0,
stopTime = 3.1536e+07,
outputInterval = 3600,
method = "Dassl",
tolerance = 0.001,
resultFile = Init.glob_res_path + r'\dsres',
finalNames = ['TestConstant.k'],
)
print(output)
i = Init.sys_id[-1]

self.model = Modeling.ModelicaMod(i)
#for change directory
self.assertEqual(Init.res_path[i] + "\\" + Init.name[i], self.model.paths.res_path)
#for translating Model
self.assertEqual(Init.mod_path[i], self.model.paths.mod_path)

#print('Modelica Translate Function Test successful') #doesn't mean that the translation was successful


class TestSciModClass (unittest.TestCase):

def test_SciMod(self):

for i in Init.sys_id:
with self.subTest(i=i):

Expand All @@ -192,19 +173,34 @@ def test_SciMod(self):

self.load = self.model.load_mod()
self.inputs = self.model.write_inputs()

print('SciMod Class Test successful')

#def test_LoadMod(self):
def test_LoadMod(self):

for i in Init.sys_id:
with self.subTest(i=i):

self.model = Modeling.SciMod(i)

self.load = self.model.load_mod()

print('SciMod LoadMod Function Test successful')

#def test_WriteInputs(self):

@unittest.skipUnless('Linear' in Init.model_type, "no Linear model used")
#@unittest.skipUnless('Linear' in Init.model_type, "no Linear model used")
class TestLinModClass (unittest.TestCase):

def test_LinMod(self):
for i in Init.sys_id:
with self.subTest(i=i):

self.model = Modeling.LinMod(i)



self.assertEqual(self.model.modifs.factors, Init.factors[i])

print('LinMod Class Test successful')

if __name__ == '__main__':
unittest.main()

0 comments on commit aacddd0

Please sign in to comment.