Skip to content

--generateSymbolicLinearization causes error for some models #274

Closed

Description

@ondras12345

Description

Current implementation of ModelicaSystem sets --generateSymbolicLinearization in __init__, regardless of whether the user actually wants to perform linearization. This causes an error for some models, e.g. this one:

model.mo:

model MyModel
  Modelica.Clocked.RealSignals.Sampler.SampleClocked sample1 annotation(
    Placement(transformation(origin = {-40, 40}, extent = {{-6, -6}, {6, 6}})));
  Modelica.Clocked.RealSignals.Sampler.Hold hold1 annotation(
    Placement(transformation(origin = {20, 40}, extent = {{-6, -6}, {6, 6}})));
  Modelica.Clocked.ClockSignals.Clocks.PeriodicRealClock periodicClock1(period = 0.1)  annotation(
    Placement(transformation(origin = {-80, 0}, extent = {{-6, -6}, {6, 6}})));
  Modelica.Blocks.Interfaces.RealInput u annotation(
    Placement(transformation(origin = {-86, 40}, extent = {{-20, -20}, {20, 20}}), iconTransformation(origin = {-86, 30}, extent = {{-20, -20}, {20, 20}})));
  Modelica.Clocked.RealSignals.Periodic.TransferFunction transferFunction(b = {1}, a = {1, -1})  annotation(
    Placement(transformation(origin = {-12, 40}, extent = {{-10, -10}, {10, 10}})));
  Modelica.Blocks.Interfaces.RealOutput y annotation(
    Placement(transformation(origin = {60, 40}, extent = {{-10, -10}, {10, 10}}), iconTransformation(origin = {60, 40}, extent = {{-10, -10}, {10, 10}})));
equation
  connect(periodicClock1.y, sample1.clock) annotation(
    Line(points = {{-74, 0}, {-40, 0}, {-40, 32}}, color = {175, 175, 175}));
  connect(u, sample1.u) annotation(
    Line(points = {{-86, 40}, {-48, 40}}, color = {0, 0, 127}));
  connect(sample1.y, transferFunction.u) annotation(
    Line(points = {{-34, 40}, {-24, 40}}, color = {0, 0, 127}));
  connect(transferFunction.y, hold1.u) annotation(
    Line(points = {{0, 40}, {12, 40}}, color = {0, 0, 127}));
  connect(y, hold1.y) annotation(
    Line(points = {{60, 40}, {26, 40}}, color = {0, 0, 127}));

annotation(
    uses(Modelica(version = "4.0.0")));
end MyModel;

with demo.py:

#!/usr/bin/env python3
from OMPython import ModelicaSystem

# note the raiseerrors=True
mod = ModelicaSystem("model.mo", "MyModel", ["Modelica"], raiseerrors=True)
# this should be enough to demonstrate the error

Running demo.py results in the following exception:

Traceback (most recent call last):
  File "/home/ondra/tmp/lin-demo/./demo.py", line 4, in <module>
    mod = ModelicaSystem("model.mo", "MyModel", ["Modelica"], raiseerrors=True)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ondra/source/repos/OMPython/OMPython/ModelicaSystem.py", line 228, in __init__
    self.buildModel(variableFilter)
  File "/home/ondra/source/repos/OMPython/OMPython/ModelicaSystem.py", line 352, in buildModel
    self._check_error()
  File "/home/ondra/source/repos/OMPython/OMPython/ModelicaSystem.py", line 332, in _check_error
    self._raise_error(errstr=errstr)
  File "/home/ondra/source/repos/OMPython/OMPython/ModelicaSystem.py", line 336, in _raise_error
    raise ModelicaSystemError(f"OM error: {errstr}")
OMPython.ModelicaSystem.ModelicaSystemError: OM error: [/var/lib/jenkins/ws/LINUX_BUILDS/tmp.build/openmodelica-1.25.0/OMCompiler/Compiler/BackEnd/Differentiate.mo:268:5-268:157:writable] Error: Derivative of expression "transferFunction.x1 = (transferFunction.u - transferFunction.a[2] * previous(transferFunction.x[1])) / transferFunction.a[1]" w.r.t. "dummyVarD" is non-existent.
Error: Internal error SymbolicJacobian.deriveAll failed
[/var/lib/jenkins/ws/LINUX_BUILDS/tmp.build/openmodelica-1.25.0/OMCompiler/Compiler/BackEnd/SymbolicJacobian.mo:2474:7-2474:74:writable] Error: Internal error SymbolicJacobian.generateSymbolicJacobian failed
[/var/lib/jenkins/ws/LINUX_BUILDS/tmp.build/openmodelica-1.25.0/OMCompiler/Compiler/BackEnd/SymbolicJacobian.mo:2265:9-2265:79:writable] Error: Internal error function createJacobian failed

Ideally, it should be possible to simulate this model while keeping raiseerrors=True.

Version and OS

  • Python Version: 3.12.3
  • OMPython Version: 0b63a2b
  • OpenModelica Version: 1.25.0
  • OS: Ubuntu 24.04.2 LTS

Activity

  1. added this to the 4.0.0 milestone on Jun 17, 2025
  2. arun3688 commented on Jun 17, 2025

    @arun3688
    Collaborator

    @ondras12345 that is true, current ModelicaSystem sets --generateSymbolicLinearization, it is done to improve the performance of linearization to avoid recompilation, eventhough user does not want to perform linearization, but then there are cases where users wants to perform the linearization in some kind of simulation loop and that takes too much time to compile the model. So one solution should would be to set the --generateSymbolicLinearization using some args in ModelicaSystem( linearization = true), by default the argument can be linearization = false , but in general it should work may be it is a bug in the backend
    @adeas31 what do you think

  3. syntron commented on Aug 23, 2025

    @syntron
    Contributor

    It would be possible to link this with the option commandLineOptions - if it is set to None default values are used (currently: --linearizationDumpLanguage=python and --generateSymbolicLinearization), else the user has to define the options he would like to use ...

    In that case, I propose to change it from str to a list of options ...

  4. syntron commented on Sep 19, 2025

    @syntron
    Contributor

    @ondras12345 could you test using PR #349? it should work if you set commandLineOptions=[]

  5. adeas31 commented on Sep 19, 2025

    @adeas31
    Member

    #349 is now merged.

  6. ondras12345 commented on Sep 19, 2025

    @ondras12345
    ContributorAuthor

    No more errors with commandLineOptions=[]. Thanks!

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions