Skip to content

Conversation

@SengerM
Copy link

@SengerM SengerM commented Aug 9, 2025

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.

@casella casella requested a review from adeas31 August 10, 2025 07:06
@adeas31
Copy link
Member

adeas31 commented Aug 22, 2025

test_passing is still not used in testing.

Probably its easier to directly add the example in the tests and check against the expected output.

syntron added a commit to syntron/OMPython that referenced this pull request Nov 4, 2025
syntron added a commit to syntron/OMPython that referenced this pull request Nov 5, 2025
'from ex' links the original exception but this is not always shown

see also OpenModelica#328
syntron added a commit to syntron/OMPython that referenced this pull request Nov 5, 2025
'from ex' links the original exception but this is not always shown

see also OpenModelica#328
syntron added a commit to syntron/OMPython that referenced this pull request Nov 6, 2025
'from ex' links the original exception but this is not always shown

see also OpenModelica#328
syntron added a commit to syntron/OMPython that referenced this pull request Nov 13, 2025
'from ex' links the original exception but this is not always shown

see also OpenModelica#328
syntron added a commit to syntron/OMPython that referenced this pull request Nov 14, 2025
'from ex' links the original exception but this is not always shown

see also OpenModelica#328
adeas31 added a commit that referenced this pull request Nov 17, 2025
'from ex' links the original exception but this is not always shown

see also #328

Co-authored-by: Adeel Asghar <adeel.asghar@liu.se>
@adeas31
Copy link
Member

adeas31 commented Nov 17, 2025

I wonder if this is already covered by #278 and #370?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants