Skip to content

Commit de107c2

Browse files
committed
Made assertion messages visible in error messages and added example
1 parent 3e9c55b commit de107c2

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

OMPython/ModelicaSystem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def run(self) -> int:
250250
except subprocess.TimeoutExpired as ex:
251251
raise ModelicaSystemError(f"Timeout running command {repr(cmdl)}") from ex
252252
except subprocess.CalledProcessError as ex:
253-
raise ModelicaSystemError(f"Error running command {repr(cmdl)}") from ex
253+
raise ModelicaSystemError(f"Error running command {repr(cmdl)}\n{ex.stdout}") from ex
254254

255255
return returncode
256256

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from OMPython import ModelicaSystem
2+
from pathlib import Path
3+
4+
def main(file=None):
5+
"""Run a simple parameters validation model."""
6+
model = ModelicaSystem(
7+
str(Path(__file__).parent/'model.mo'), # Path to file where modelica code is.
8+
'ParametersValidation', # Name of model from that file.
9+
)
10+
# Run the model with different parameters values and print the results:
11+
for positive_number in [-1,1,.1]:
12+
for negative_number in [-1,1,-.1]:
13+
model.setParameters(
14+
[
15+
f'positive_number={positive_number}',
16+
f'negative_number={negative_number}',
17+
],
18+
)
19+
print('--------------------------------------', file=file)
20+
print('Simulating with:', file=file)
21+
print(f'\tpositive_number = {positive_number}', file=file)
22+
print(f'\tnegative_number = {negative_number}', file=file)
23+
try:
24+
model.simulate() # Parameters values are validated here.
25+
except Exception as e:
26+
print(e, file=file)
27+
28+
def test_passing()->bool:
29+
"""Function to use for automatic testing. If returns `True` it means this example is working."""
30+
class PrintSilencer:
31+
def write(s:str):
32+
pass
33+
test_passing = False
34+
try:
35+
main(file=PrintSilencer())
36+
test_passing = True
37+
except Exception as e:
38+
test_passing = False
39+
return test_passing
40+
41+
if __name__ == '__main__':
42+
main()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
model ParametersValidation "A dummy model to show how to validate parameters"
2+
parameter Real positive_number = 1;
3+
parameter Real negative_number = -1;
4+
5+
initial algorithm
6+
assert(positive_number>0, "`positive_number` must be > 0");
7+
assert(negative_number<0, "`negative_number` must be < 0");
8+
assert(positive_number-negative_number > 1, "`positive_number - negative_number` must be > 1");
9+
10+
end ParametersValidation;

0 commit comments

Comments
 (0)