Skip to content

Commit 1fa780c

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent c664d6d commit 1fa780c

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

modelitool/simulate.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ def simulate(
104104
self.set_param_dict(parameter_dict)
105105

106106
if simulation_options is not None:
107-
if x is not None and 'x' in simulation_options:
107+
if x is not None and "x" in simulation_options:
108108
warnings.warn(
109109
"Boundary file 'x' specified both in simulation_options and as a direct parameter."
110110
" The 'x' provided in simulate() will be used.",
111-
UserWarning
111+
UserWarning,
112112
)
113113

114114
self._set_simulation_options(simulation_options)
@@ -179,20 +179,20 @@ def get_parameters(self):
179179

180180
def _set_simulation_options(self, simulation_options):
181181
standard_options = {
182-
'startTime': simulation_options.get('startTime'),
183-
'stopTime': simulation_options.get('stopTime'),
184-
'stepSize': simulation_options.get('stepSize'),
185-
'tolerance': simulation_options.get('tolerance'),
186-
'solver': simulation_options.get('solver'),
187-
'outputFormat': simulation_options.get('outputFormat')
182+
"startTime": simulation_options.get("startTime"),
183+
"stopTime": simulation_options.get("stopTime"),
184+
"stepSize": simulation_options.get("stepSize"),
185+
"tolerance": simulation_options.get("tolerance"),
186+
"solver": simulation_options.get("solver"),
187+
"outputFormat": simulation_options.get("outputFormat"),
188188
}
189189

190-
options = [f'{k}={v}' for k, v in standard_options.items() if v is not None]
190+
options = [f"{k}={v}" for k, v in standard_options.items() if v is not None]
191191
self.model.setSimulationOptions(options)
192192
self.simulation_options = simulation_options
193193

194-
if 'x' in simulation_options:
195-
self._set_x(simulation_options['x'])
194+
if "x" in simulation_options:
195+
self._set_x(simulation_options["x"])
196196

197197
def _set_x(self, df: pd.DataFrame):
198198
"""Sets the input data for the simulation and updates the corresponding file."""
@@ -254,4 +254,3 @@ def library_contents(library_path):
254254
for file in files:
255255
file_path = os.path.join(root, file)
256256
print(file_path)
257-

tests/test_simulate.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pathlib import Path
2+
23
import pytest
4+
35
import numpy as np
46
import pandas as pd
57

@@ -114,12 +116,19 @@ def test_set_boundaries_df(self):
114116
)
115117

116118
simulation_options_with_x = simulation_options.copy()
117-
simulation_options_with_x['x'] = x_options
119+
simulation_options_with_x["x"] = x_options
118120
res1 = simu.simulate(simulation_options=simulation_options_with_x)
119121
res1 = res1.loc[:, ["Boundaries.y[1]", "Boundaries.y[2]"]]
120122
np.testing.assert_allclose(x_options.to_numpy(), res1.to_numpy())
121-
assert np.all([x_options.index[i] == res1.index[i] for i in range(len(x_options.index))])
122-
assert np.all([x_options.columns[i] == res1.columns[i] for i in range(len(x_options.columns))])
123+
assert np.all(
124+
[x_options.index[i] == res1.index[i] for i in range(len(x_options.index))]
125+
)
126+
assert np.all(
127+
[
128+
x_options.columns[i] == res1.columns[i]
129+
for i in range(len(x_options.columns))
130+
]
131+
)
123132

124133
simu = OMModel(
125134
model_path="TestLib.boundary_test",
@@ -129,18 +138,29 @@ def test_set_boundaries_df(self):
129138
res2 = simu.simulate(simulation_options=simulation_options, x=x_direct)
130139
res2 = res2.loc[:, ["Boundaries.y[1]", "Boundaries.y[2]"]]
131140
np.testing.assert_allclose(x_direct.to_numpy(), res2.to_numpy())
132-
assert np.all([x_direct.index[i] == res2.index[i] for i in range(len(x_direct.index))])
133-
assert np.all([x_direct.columns[i] == res2.columns[i] for i in range(len(x_direct.columns))])
141+
assert np.all(
142+
[x_direct.index[i] == res2.index[i] for i in range(len(x_direct.index))]
143+
)
144+
assert np.all(
145+
[
146+
x_direct.columns[i] == res2.columns[i]
147+
for i in range(len(x_direct.columns))
148+
]
149+
)
134150

135151
simu = OMModel(
136152
model_path="TestLib.boundary_test",
137153
package_path=PACKAGE_DIR / "package.mo",
138154
lmodel=["Modelica"],
139155
)
140-
with pytest.warns(UserWarning,
141-
match="Boundary file 'x' specified both in simulation_options and as a direct parameter"):
142-
res3 = simu.simulate(simulation_options=simulation_options_with_x, x=x_direct)
156+
with pytest.warns(
157+
UserWarning,
158+
match="Boundary file 'x' specified both in simulation_options and as a direct parameter",
159+
):
160+
res3 = simu.simulate(
161+
simulation_options=simulation_options_with_x, x=x_direct
162+
)
143163
res3 = res3.loc[:, ["Boundaries.y[1]", "Boundaries.y[2]"]]
144164
np.testing.assert_allclose(x_direct.to_numpy(), res3.to_numpy())
145165
with pytest.raises(AssertionError):
146-
np.testing.assert_allclose(x_options.to_numpy(), res3.to_numpy())
166+
np.testing.assert_allclose(x_options.to_numpy(), res3.to_numpy())

0 commit comments

Comments
 (0)