OMPython icon indicating copy to clipboard operation
OMPython copied to clipboard

Assertion messages in error messages

Open SengerM opened this issue 6 months ago • 2 comments

Purpose

Make assertion messages defined in Modelica models visible in Python error messages.

Approach

The current implementation completely hides away assertion messages, which makes it very hard to debug when something is not working. This simple change shows the user what the assertion error was (see below), though I recognize it is not the most beautiful so it might be improved.

Additionally, I added a simple example to illustrate how to use assertions in Modelica. This is the output that the user gets when the example is run:

$ python main.py 
--------------------------------------
Simulating with:
	positive_number = -1
	negative_number = -1
Error running command ['/tmp/tmpgz5871j2/ParametersValidation', '-r=/tmp/tmpgz5871j2/ParametersValidation_res.mat', '-overrideFile=/tmp/tmpgz5871j2/ParametersValidation_override.txt']
LOG_ASSERT        | error   | [/home/lionelmessi/code/OMPython/examples/parameters_validation/model.mo:6:3-6:61:writable]
|                 | |       | The following assertion has been violated during initialization at time 0.000000
|                 | |       | ((positive_number > 0.0)) --> "`positive_number` must be > 0"
LOG_ASSERT        | info    | simulation terminated by an assertion at initialization

--------------------------------------
Simulating with:
	positive_number = -1
	negative_number = 1
Error running command ['/tmp/tmpgz5871j2/ParametersValidation', '-r=/tmp/tmpgz5871j2/ParametersValidation_res.mat', '-overrideFile=/tmp/tmpgz5871j2/ParametersValidation_override.txt']
LOG_ASSERT        | error   | [/home/lionelmessi/code/OMPython/examples/parameters_validation/model.mo:6:3-6:61:writable]
|                 | |       | The following assertion has been violated during initialization at time 0.000000
|                 | |       | ((positive_number > 0.0)) --> "`positive_number` must be > 0"
LOG_ASSERT        | info    | simulation terminated by an assertion at initialization

--------------------------------------
Simulating with:
	positive_number = -1
	negative_number = -0.1
Error running command ['/tmp/tmpgz5871j2/ParametersValidation', '-r=/tmp/tmpgz5871j2/ParametersValidation_res.mat', '-overrideFile=/tmp/tmpgz5871j2/ParametersValidation_override.txt']
LOG_ASSERT        | error   | [/home/lionelmessi/code/OMPython/examples/parameters_validation/model.mo:6:3-6:61:writable]
|                 | |       | The following assertion has been violated during initialization at time 0.000000
|                 | |       | ((positive_number > 0.0)) --> "`positive_number` must be > 0"
LOG_ASSERT        | info    | simulation terminated by an assertion at initialization

--------------------------------------
Simulating with:
	positive_number = 1
	negative_number = -1
--------------------------------------
Simulating with:
	positive_number = 1
	negative_number = 1
Error running command ['/tmp/tmpgz5871j2/ParametersValidation', '-r=/tmp/tmpgz5871j2/ParametersValidation_res.mat', '-overrideFile=/tmp/tmpgz5871j2/ParametersValidation_override.txt']
LOG_ASSERT        | error   | [/home/lionelmessi/code/OMPython/examples/parameters_validation/model.mo:7:3-7:61:writable]
|                 | |       | The following assertion has been violated during initialization at time 0.000000
|                 | |       | ((negative_number < 0.0)) --> "`negative_number` must be < 0"
LOG_ASSERT        | info    | simulation terminated by an assertion at initialization

--------------------------------------
Simulating with:
	positive_number = 1
	negative_number = -0.1
--------------------------------------
Simulating with:
	positive_number = 0.1
	negative_number = -1
--------------------------------------
Simulating with:
	positive_number = 0.1
	negative_number = 1
Error running command ['/tmp/tmpgz5871j2/ParametersValidation', '-r=/tmp/tmpgz5871j2/ParametersValidation_res.mat', '-overrideFile=/tmp/tmpgz5871j2/ParametersValidation_override.txt']
LOG_ASSERT        | error   | [/home/lionelmessi/code/OMPython/examples/parameters_validation/model.mo:7:3-7:61:writable]
|                 | |       | The following assertion has been violated during initialization at time 0.000000
|                 | |       | ((negative_number < 0.0)) --> "`negative_number` must be < 0"
LOG_ASSERT        | info    | simulation terminated by an assertion at initialization

--------------------------------------
Simulating with:
	positive_number = 0.1
	negative_number = -0.1
Error running command ['/tmp/tmpgz5871j2/ParametersValidation', '-r=/tmp/tmpgz5871j2/ParametersValidation_res.mat', '-overrideFile=/tmp/tmpgz5871j2/ParametersValidation_override.txt']
LOG_ASSERT        | error   | [/home/lionelmessi/code/OMPython/examples/parameters_validation/model.mo:8:3-8:97:writable]
|                 | |       | The following assertion has been violated during initialization at time 0.000000
|                 | |       | ((positive_number - negative_number > 1.0)) --> "`positive_number - negative_number` must be > 1"
LOG_ASSERT        | info    | simulation terminated by an assertion at initialization

Finally, a prototype of test_passing function that could be made a standard with other examples to be used, as suggested by @syntron in #327 (here exactly), for automatic testing.

SengerM avatar Aug 09 '25 08:08 SengerM