Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert all unit tests to pytest #722

Merged
merged 15 commits into from
Dec 10, 2024
Prev Previous commit
Next Next commit
Update test_output_grid.py
  • Loading branch information
t-sommer committed Dec 10, 2024
commit f3e1aa8cf2747c5c73ee3615389cb181074122b6
126 changes: 59 additions & 67 deletions tests/test_output_grid.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,85 @@
import unittest
import pytest
import numpy as np
import sys
import os
from fmpy import simulate_fmu
from fmpy.util import download_test_file, download_file


class OutputGridTest(unittest.TestCase):
def test_step_size_cs():

@classmethod
def setUpClass(cls):
print("Python:", sys.version)
url = 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/cs/win64/Test-FMUs/0.0.2/Dahlquist/Dahlquist.fmu'
sha256 = '6df6ab64705615dfa1217123a103c23384a081763a6f71726ba7943503da8fc0'

def test_step_size_cs(self):
filename = download_file(url, checksum=sha256)

url = 'https://github.com/modelica/fmi-cross-check/raw/master/fmus/2.0/cs/win64/Test-FMUs/0.0.2/Dahlquist/Dahlquist.fmu'
sha256 = '6df6ab64705615dfa1217123a103c23384a081763a6f71726ba7943503da8fc0'
h = 0.02

filename = download_file(url, checksum=sha256)
result = simulate_fmu(filename, output_interval=h, stop_time=10)

h = 0.02
time = result['time']

result = simulate_fmu(filename, output_interval=h, stop_time=10)
grid = np.array(range(501)) * h

time = result['time']
assert np.all(time == grid)

grid = np.array(range(501)) * h
def test_step_size_me():

self.assertTrue(np.all(time == grid))
# download the FMU and input file
for filename in ['CoupledClutches.fmu', 'CoupledClutches_in.csv']:
download_test_file('2.0', 'me', 'MapleSim', '2016.2', 'CoupledClutches', filename)

def test_step_size_me(self):
# load the input
input = np.genfromtxt('CoupledClutches_in.csv', delimiter=',', names=True)

# download the FMU and input file
for filename in ['CoupledClutches.fmu', 'CoupledClutches_in.csv']:
download_test_file('2.0', 'me', 'MapleSim', '2016.2', 'CoupledClutches', filename)
assert np.sum(input['time'] == 0.9) > 1, "Input event expected at t=0.9"

# load the input
input = np.genfromtxt('CoupledClutches_in.csv', delimiter=',', names=True)
start_time = 0.0
stop_time = 1.5
step_size = 1e-2
output_interval = 2e-2
T2 = 0.5

self.assertTrue(np.sum(input['time'] == 0.9) > 1, msg="Input event expected at t=0.9")
# common arguments
kwargs = {
'filename': 'CoupledClutches.fmu',
'start_time': start_time,
'stop_time': stop_time,
'fmi_type': 'ModelExchange',
'step_size': step_size,
'output_interval': output_interval,
'input': input,
'start_values': {'CoupledClutches1_T2': T2}
}

start_time = 0.0
stop_time = 1.5
step_size = 1e-2
output_interval = 2e-2
T2 = 0.5
# fixed step w/o events
result = simulate_fmu(solver='Euler', record_events=False, **kwargs)

# common arguments
kwargs = {
'filename': 'CoupledClutches.fmu',
'start_time': start_time,
'stop_time': stop_time,
'fmi_type': 'ModelExchange',
'step_size': step_size,
'output_interval': output_interval,
'input': input,
'start_values': {'CoupledClutches1_T2': T2}
}
time = result['time']
assert time[0] == pytest.approx(start_time), "First sample time must be equal to start_time"
assert time[-1] == pytest.approx(stop_time), "Last sample time must be equal to stop_time"
assert np.all(np.isclose(np.diff(time), output_interval)), "Output intervals must be regular"

# fixed step w/o events
result = simulate_fmu(solver='Euler', record_events=False, **kwargs)
# fixed step w/ events
result = simulate_fmu(solver='Euler', record_events=True, **kwargs)

time = result['time']
self.assertAlmostEqual(time[0], start_time, msg="First sample time must be equal to start_time")
self.assertAlmostEqual(time[-1], stop_time, msg="Last sample time must be equal to stop_time")
self.assertTrue(np.all(np.isclose(np.diff(time), output_interval)), msg="Output intervals must be regular")
time = result['time']
assert time[0] == pytest.approx(start_time), "First sample time must be equal to start_time"
assert time[-1] == pytest.approx(stop_time), "Last sample time must be equal to stop_time"

# fixed step w/ events
result = simulate_fmu(solver='Euler', record_events=True, **kwargs)
# variable step w/o events
result = simulate_fmu(solver='CVode', record_events=False, **kwargs)

time = result['time']
self.assertAlmostEqual(time[0], start_time, msg="First sample time must be equal to start_time")
self.assertAlmostEqual(time[-1], stop_time, msg="Last sample time must be equal to stop_time")
time = result['time']
assert time[0] == pytest.approx(start_time), "First sample time must be equal to start_time"
assert time[-1] == pytest.approx(stop_time), "Last sample time must be equal to stop_time"
steps = np.diff(time)
steps = steps[steps > 1e-13] # remove events
assert np.all(np.isclose(steps, output_interval)), "Output intervals must be regular"

# variable step w/o events
result = simulate_fmu(solver='CVode', record_events=False, **kwargs)
# variable step w/ events
result = simulate_fmu(solver='CVode', record_events=True, **kwargs)

time = result['time']
self.assertAlmostEqual(time[0], start_time, msg="First sample time must be equal to start_time")
self.assertAlmostEqual(time[-1], stop_time, msg="Last sample time must be equal to stop_time")
steps = np.diff(time)
steps = steps[steps > 1e-13] # remove events
self.assertTrue(np.all(np.isclose(steps, output_interval)), msg="Output intervals must be regular")

# variable step w/ events
result = simulate_fmu(solver='CVode', record_events=True, **kwargs)

time = result['time']
self.assertAlmostEqual(time[0], start_time, msg="First sample time must be equal to start_time")
self.assertAlmostEqual(time[-1], stop_time, msg="Last sample time must be equal to stop_time")
self.assertTrue(np.sum(time == 0.9) > 1, msg="Input event expected at t=0.9")
self.assertTrue(np.sum(np.isclose(time, T2)) > 1, msg="Time event expected at t=T2")
time = result['time']
assert time[0] == pytest.approx(start_time), "First sample time must be equal to start_time"
assert time[-1] == pytest.approx(stop_time), "Last sample time must be equal to stop_time"
assert np.sum(time == 0.9) > 1, "Input event expected at t=0.9"
assert np.sum(np.isclose(time, T2)) > 1, "Time event expected at t=T2"