Skip to content

Commit

Permalink
Add FMPy state integration test #62
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Ivar Hatledal committed Feb 17, 2020
1 parent f343412 commit 4d50af3
Showing 1 changed file with 73 additions and 18 deletions.
91 changes: 73 additions & 18 deletions pythonfmu/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"pyfmi", reason="pyfmi is required for testing the produced FMU"
)


DEMO = "pythonslave.py"


Expand Down Expand Up @@ -51,7 +50,7 @@ def test_integration_reset(tmp_path):


@pytest.mark.integration
def test_integration_state(tmp_path):
def test_integration_get_state(tmp_path):
script_file = Path(__file__).parent / DEMO

FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false", canGetAndSetFMUstate="true")
Expand All @@ -69,6 +68,7 @@ def step(model):
t += dt

model = pyfmi.load_fmu(str(fmu))
model.initialize()
step(model)
state = model.get_fmu_state()
assert model.get_real([vr])[0] == pytest.approx(dt, rel=1e-7)
Expand All @@ -82,23 +82,79 @@ def step(model):


@pytest.mark.integration
def test_integration_get(tmp_path):
def test_integration_get_serialize_state(tmp_path):
fmpy = pytest.importorskip(
"fmpy", reason="fmpy is not available for testing the produced FMU"
)

script_file = Path(__file__).parent / DEMO

FmuBuilder.build_FMU(script_file, dest=tmp_path)

fmu = tmp_path / "PythonSlave.fmu"
assert fmu.exists()

model_description = fmpy.read_model_description(fmu)

unzipdir = fmpy.extract(fmu)

model = fmpy.fmi2.FMU2Slave(
guid=model_description.guid,
unzipDirectory=unzipdir,
modelIdentifier=model_description.coSimulation.modelIdentifier,
instanceName='instance1')

vr = 5 # realOut
t = 0.0
dt = 0.1

def step(model):
nonlocal t
model.doStep(t, dt)
t += dt

model.instantiate()
model.setupExperiment()
model.enterInitializationMode()
model.exitInitializationMode()

step(model)
state = model.getFMUstate()
assert model.getReal([vr])[0] == pytest.approx(dt, rel=1e-7)
step(model)
assert model.getReal([vr])[0] == pytest.approx(dt * 2, rel=1e-7)
model.setFMUstate(state)
assert model.getReal([vr])[0] == pytest.approx(dt, rel=1e-7)
step(model)
assert model.getReal([vr])[0] == pytest.approx(dt * 3, rel=1e-7)

serialize_fm_ustate = model.serializeFMUstate(state)
model.freeFMUstate(state)
de_serialize_fm_ustate = model.deSerializeFMUstate(serialize_fm_ustate)
model.setFMUstate(de_serialize_fm_ustate)
assert model.getReal([vr])[0] == pytest.approx(dt, rel=1e-7)

model.freeFMUstate(de_serialize_fm_ustate)
model.terminate()


@pytest.mark.integration
def test_integration_get(tmp_path):
script_file = Path(__file__).parent / DEMO

FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")

fmu = tmp_path / "PythonSlave.fmu"
assert fmu.exists()
model = pyfmi.load_fmu(str(fmu))

to_test = {
"intParam": 42,
"intOut": 23,
"realOut": 3.0,
"booleanVariable": True,
"stringVariable": "Hello World!",
"realIn": 2./3.,
"realIn": 2. / 3.,
"booleanParameter": False,
"stringParameter": "dog"
}
Expand All @@ -107,33 +163,32 @@ def test_integration_get(tmp_path):
for key, value in to_test.items():
var = variables[key]
if var.type == pyfmi.fmi.FMI2_INTEGER:
model_value = model.get_integer([var.value_reference,])[0]
model_value = model.get_integer([var.value_reference, ])[0]
elif var.type == pyfmi.fmi.FMI2_REAL:
model_value = model.get_real([var.value_reference,])[0]
model_value = model.get_real([var.value_reference, ])[0]
elif var.type == pyfmi.fmi.FMI2_BOOLEAN:
model_value = model.get_boolean([var.value_reference,])[0]
model_value = model.get_boolean([var.value_reference, ])[0]
elif var.type == pyfmi.fmi.FMI2_STRING:
model_value = model.get_string([var.value_reference,])[0]
model_value = model.get_string([var.value_reference, ])[0]
else:
pytest.xfail("Unsupported type")

assert model_value == value


@pytest.mark.integration
def test_integration_set(tmp_path):

script_file = Path(__file__).parent / DEMO

FmuBuilder.build_FMU(script_file, dest=tmp_path, needsExecutionTool="false")

fmu = tmp_path / "PythonSlave.fmu"
assert fmu.exists()
model = pyfmi.load_fmu(str(fmu))

to_test = {
"intParam": 20,
"realIn": 1./3.,
"realIn": 1. / 3.,
"booleanParameter": True,
"stringParameter": "cat"
}
Expand All @@ -143,19 +198,19 @@ def test_integration_set(tmp_path):
var = variables[key]
if var.type == pyfmi.fmi.FMI2_INTEGER:
model.set_integer([var.value_reference, ], [value, ])
model_value = model.get_integer([var.value_reference,])[0]
model_value = model.get_integer([var.value_reference, ])[0]
elif var.type == pyfmi.fmi.FMI2_REAL:
model.set_real([var.value_reference, ], [value, ])
model_value = model.get_real([var.value_reference,])[0]
model_value = model.get_real([var.value_reference, ])[0]
elif var.type == pyfmi.fmi.FMI2_BOOLEAN:
model.set_boolean([var.value_reference, ], [value, ])
model_value = model.get_boolean([var.value_reference,])[0]
model_value = model.get_boolean([var.value_reference, ])[0]
elif var.type == pyfmi.fmi.FMI2_STRING:
model.set_string([var.value_reference, ], [value, ])
model_value = model.get_string([var.value_reference,])[0]
model_value = model.get_string([var.value_reference, ])[0]
else:
pytest.xfail("Unsupported type")

assert model_value == value


Expand Down

0 comments on commit 4d50af3

Please sign in to comment.