Commit 8048a61e authored by Jesse VanderWees's avatar Jesse VanderWees 🐘

get all simulation tests working

parent b0078645
......@@ -99,6 +99,10 @@ class SimulationProblem:
self.__python_types = AliasDict(self.alias_relation)
for v in itertools.chain(self.__pymola_model.states, self.__pymola_model.alg_states, self.__pymola_model.inputs):
sym_name = v.symbol.name()
# Store the types in an AliasDict
self.__python_types[sym_name] = v.python_type
# If the nominal is 0.0 or 1.0 or -1.0, ignore: get_variable_nominal returns a default of 1.0
# TODO: handle nominal vectors (update() will need to load them)
if ca.MX(v.nominal).is_zero() or ca.MX(v.nominal - 1).is_zero() or ca.MX(v.nominal + 1).is_zero():
......@@ -111,9 +115,6 @@ class SimulationProblem:
logger.debug("SimulationProblem: Setting nominal value for variable {} to {}".format(
sym_name, self.__nominals[sym_name]))
# Store the types in an AliasDict
self.__python_types[sym_name] = v.python_type
# Initialize DAE and initial residuals
variable_lists = ['states', 'der_states', 'alg_states', 'inputs', 'constants', 'parameters']
function_arguments = [self.__pymola_model.time] + \
......@@ -503,21 +504,21 @@ class SimulationProblem:
def get_var_count(self):
"""
Return the number of variables (internal FMU and user declared).
Return the number of variables in the model.
:returns: The number of variables supported by the FMU.
:returns: The number of variables in the model.
"""
return len(self.get_model_variables())
return len(self.get_variables())
def get_var_name(self, i):
"""
Returns the name of a variable.
:param i: Index in ordered dictionary returned by FMU-method get_model_variables.
:param i: Index in ordered dictionary returned by method get_variables.
:returns: The name of the variable.
"""
return self.get_variables().items()[i][0]
return list(self.get_variables())[i]
def get_var_type(self, name):
"""
......@@ -529,7 +530,7 @@ class SimulationProblem:
:raises: KeyError
"""
return self.__python_types(name)
return self.__python_types[name]
def get_var_rank(self, name):
"""
......@@ -547,7 +548,7 @@ class SimulationProblem:
"""
Return all variables (both internal and user defined)
:returns: A list of all variables supported by the model.
:returns: An ordered dictionary of all variables supported by the model.
"""
return self.__sym_dict
......@@ -646,6 +647,12 @@ class SimulationProblem:
"""
return self.__nominals.get(variable, 1.0)
def timeseries_at(self, variable, t):
"""
Get value of timeseries variable at time t: should be overriden by pi or csv mixin
"""
raise NotImplementedError
@cached
def initial_state(self) -> AliasDict:
"""
......@@ -661,6 +668,8 @@ class SimulationProblem:
initial_state_dict[variable] = self.timeseries_at(variable, t0)
except KeyError:
pass
except NotImplementedError:
pass
else:
if logger.getEffectiveLevel() == logging.DEBUG:
logger.debug("Read intial state for {}".format(variable))
......
......@@ -13,7 +13,8 @@ model TestModel
output Real z;
input Real x_delayed;
//TODO: Implement delayed variables and tests for them
//input Real x_delayed;
output Real switched;
......
......@@ -105,4 +105,39 @@
<ns0:event date="2013-05-20" flag="0" time="18:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="19:00:00" value="1.0" />
</ns0:series>
<ns0:series>
<ns0:header>
<ns0:type>instantaneous</ns0:type>
<ns0:locationId>Controls</ns0:locationId>
<ns0:parameterId>U</ns0:parameterId>
<ns0:timeStep multiplier="3600" unit="second" />
<ns0:startDate date="2013-05-19" time="22:00:00" />
<ns0:forecastDate date="2013-05-19" time="22:00:00" />
<ns0:endDate date="2013-05-20" time="19:00:00" />
<ns0:missVal>-999.0</ns0:missVal>
<ns0:units>m3/s</ns0:units>
</ns0:header>
<ns0:event date="2013-05-19" flag="0" time="22:00:00" value="1.0" />
<ns0:event date="2013-05-19" flag="0" time="23:00:00" value="2.0" />
<ns0:event date="2013-05-20" flag="0" time="00:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="01:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="02:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="03:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="04:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="05:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="06:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="07:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="08:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="09:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="10:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="11:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="12:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="13:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="14:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="15:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="16:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="17:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="18:00:00" value="1.0" />
<ns0:event date="2013-05-20" flag="0" time="19:00:00" value="1.0" />
</ns0:series>
</ns0:TimeSeries>
\ No newline at end of file
......@@ -22,32 +22,27 @@ class TestSimulation(TestCase):
def test_get_variables(self):
all_variables = self.problem.get_variables()
self.assertIsInstance(all_variables, collections.OrderedDict)
variables = []
for var in all_variables.items():
varname = var[0]
# method returns all variables, including internal FMUvariables, starting with '_'
if not re.match('_', varname):
variables.append(varname)
self.assertEqual(variables, ['alias', 'constant_input', 'constant_output', 'k', 'switched', 'u', 'u_out', 'w', 'der(w)', 'x', 'der(x)', 'x_delayed', 'x_start', 'y', 'z'] )
nvar = self.problem.get_var_count()
self.assertEqual(len(all_variables), nvar)
self.assertEqual(self.problem.get_parameter_variables().keys(), ['x_start'])
self.assertEqual(self.problem.get_input_variables().keys(), ['constant_input', 'u', 'x_delayed'])
self.assertEqual(self.problem.get_output_variables().keys(), ['constant_output', 'switched', 'u_out', 'y', 'z'])
self.assertEqual(set(all_variables),
{'time', 'constant_input', 'k', 'switched', 'u', 'u_out',
'w', 'der(w)', 'x', 'der(x)', 'x_start', 'y', 'z'})
self.assertEqual(set(self.problem.get_parameter_variables()),
{'x_start', 'k'})
self.assertEqual(set(self.problem.get_input_variables()),
{'constant_input', 'u'})
self.assertEqual(set(self.problem.get_output_variables()),
{'constant_output', 'switched', 'u_out', 'y', 'z'})
def test_get_set_var(self):
val = self.problem.get_var('switched')
self.assertEqual(val, 0.0)
self.assertTrue(np.isnan(val))
self.problem.set_var('switched', 10.0)
val_reset = self.problem.get_var('switched')
self.assertNotEqual(val_reset, val)
def test_get_var_name_and_type(self):
type = self.problem.get_var_type('switched')
self.assertEqual(type, 'float')
t = self.problem.get_var_type('switched')
self.assertTrue(t == float)
all_variables = self.problem.get_variables()
idx = 0
for var in all_variables.items():
......@@ -65,6 +60,9 @@ class TestSimulation(TestCase):
stop = 10.0
dt = 1.0
self.problem.setup_experiment(start, stop, dt)
self.problem.set_var('x_start', 0.0)
self.problem.set_var('constant_input', 0.0)
self.problem.set_var('u', 0.0)
self.problem.initialize()
val = self.problem.get_start_time()
self.assertAlmostEqual(self.problem.get_start_time(), start, 1e-6)
......@@ -76,19 +74,6 @@ class TestSimulation(TestCase):
curtime = self.problem.get_current_time()
self.assertAlmostEqual(curtime, stop, 1e-6)
def test_basic_run(self):
# run FMU model
start = 0.0
stop = 10.0
dt = 1.0
self.problem.setup_experiment(start, stop, dt)
self.problem.initialize()
curtime = self.problem.get_current_time()
while curtime < stop:
status = self.problem.update(dt)
curtime = self.problem.get_current_time()
self.problem.finalize()
def test_set_input(self):
# run FMU model
expected_values = [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0]
......@@ -96,6 +81,8 @@ class TestSimulation(TestCase):
dt = 0.1
self.problem.setup_experiment(0.0, stop, dt)
self.problem.set_var('x_start', 0.25)
self.problem.set_var('constant_input', 0.0)
self.problem.set_var('u', 0.0)
self.problem.initialize()
i = 0
while i < int(stop/dt):
......@@ -104,7 +91,6 @@ class TestSimulation(TestCase):
val = self.problem.get_var('switched')
self.assertEqual(val, expected_values[i])
i += 1
self.problem.finalize()
def test_set_input2(self):
# run FMU model
......@@ -113,6 +99,8 @@ class TestSimulation(TestCase):
dt = 0.1
self.problem.setup_experiment(0.0, stop, dt)
self.problem.set_var('x_start', 0.25)
self.problem.set_var('constant_input', 0.0)
self.problem.set_var('u', 0.0)
self.problem.initialize()
i = 0
while i < int(stop/dt):
......@@ -123,4 +111,3 @@ class TestSimulation(TestCase):
val = self.problem.get_var('switched')
self.assertEqual(val, expected_values[i])
i += 1
self.problem.finalize()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment