Registry Mirror
Skip to content

Conversation

@hubertus65

Copy link
Copy Markdown
Member

Modelica.Math.Nonlinear.quadratureNewtonCotes, new in MSL 4.2, evaluates its function argument in an array constructor with an iterator:

y:={f(x[k]) for k in 1:n + 1};

OpenModelica 1.27.1 generates C code for this that does not compile:

..._functions.c:27:3: error: use of undeclared identifier 'metatype_array'
..._functions.c:112:5: error: call to undeclared function 'simple_alloc_1d_metatype_array'
...
10 errors generated.

As a result, no model that uses the function builds in OpenModelica. That includes the MSL's own example Modelica.Math.Nonlinear.Examples.MovingIntegral.

This PR evaluates the integrand in a for-loop instead:

for k in 1:n + 1 loop
  y[k]:=f(x[k]);
end for;

Verification

Model Tool Before After
Math.Nonlinear.Examples.MovingIntegral OpenModelica 1.27.1 C compile error simulates; psi1…psi6 = 0.65450 agree with the analytical psi0 (Boole's rule psi4 within 2e-5)
ModelicaTest Math.TestQuadrature (all degrees d = 1…6, exact for polynomials) OpenModelica 1.27.1 C compile error all assertions pass
Copies of the function, integrand 3u² + sin(u), d = 1 and 4 OCT (Modelon Impact) simulates simulates; identical results to before

The OCT rows used local copies of the function, because Impact ships MSL 4.1.0, which does not contain it yet.

🤖 Generated with Claude Code

https://claude.ai/code/session_01UmNZkRw6HV9CeBvNk36AzT

Math.Nonlinear.quadratureNewtonCotes (new in 4.2) evaluates its function
argument in an array constructor with an iterator:

  y:={f(x[k]) for k in 1:n + 1};

OpenModelica 1.27.1 generates C code that does not compile for this
(undeclared metatype_array), so neither the example
Math.Nonlinear.Examples.MovingIntegral nor any other model using the
function builds. Evaluate the integrand in a for-loop instead.

With this change, MovingIntegral simulates in OpenModelica and all six
quadrature orders agree with the analytical flux linkage. OCT (Modelon
Impact) gives identical results for both forms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UmNZkRw6HV9CeBvNk36AzT
@AHaumer

AHaumer commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

@hubertus65 I'd recommend to validate results you got from an AI manually with Natural Intelligence.
This is legal Modelica. Vectorized version are more perrformant than for-loops. It compiles without any problems with Dymola 2026x Refresh 1, Open Modelica v1.28.0-dev.1586 and Wolfram System Modeler 15.0.0 on Windows 11 Pro 26H2. So there's a chance that this code is not problematic.
If you think this code doesn't compile with OM, you should consider a bug report at OpenModelica.

@AHaumer
AHaumer requested review from AHaumer, HansOlsson, casella and christiankral and removed request for christiankral November 1, 2026 10:26
@AHaumer AHaumer added L: Math Issue addresses Modelica.Math worksforme Clarified issue labels Nov 1, 2026
@AHaumer
AHaumer removed the request for review from christiankral November 1, 2026 10:28
@hubertus65

Copy link
Copy Markdown
Member Author

Same argument again. The question is what we want to achieve with the Standard Library: make it work for as many tools as possible, or make it a gate by using as many features of the Modelica language as possible. FWIW: I also filed a bug report for Modelon Impact. That doesn't mean that a rewrite that works on more tools is isn't useful. I also have made similar changes that make models work in OpenModelica. And regarding speed: there is no measurable difference.

The real industry value of Modelica is the cross- tool compatibility of the libraries. Without it, the Dyad language and tool would be just as good. In Modelica we need to do the best to make use of that advantage.

@AHaumer

AHaumer commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

@hubertus65 You didn't even read my comment.
You complain "no model that uses the function builds in OpenModelica".
BTW, is this your observation or Claude's?
I tested with 3 tools, one of the OM, and it compiles with all of them.

Furthermore:
You don't even notice what we are doing in MAP-Lib.
We try to reduce the number of open PRs and get the CI (with at least 3 tools!) working.
It is easier to get CI on track with a smaller number of open PRs.
After that it is easier taking care of PRs since they get tested automatically.
Instead of supporting that, you flood us with PRs - I found 1 of 6 being a bug.

@HansOlsson

HansOlsson commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Same argument again. The question is what we want to achieve with the Standard Library: make it work for as many tools as possible, or make it a gate by using as many features of the Modelica language as possible. FWIW: I also filed a bug report for Modelon Impact. That doesn't mean that a rewrite that works on more tools is isn't useful. I also have made similar changes that make models work in OpenModelica. And regarding speed: there is no measurable difference.

The real industry value of Modelica is the cross- tool compatibility of the libraries. Without it, the Dyad language and tool would be just as good. In Modelica we need to do the best to make use of that advantage.

On the other hand, there is also the argument that Modelica must keep developing, and focusing on the common denominator of all tools (in MSL) is likely to stop that and also progress down-stream to other libraries

As far as I know the strategy we have used so far:

  • Have a bit of delay between adding a feature in the language and using it in MSL
  • If a PR triggers a bug in some tool for new code, and it is possible to rewrite to avoid it without impacting the use: do that. If the rewrite is odd see this a temporary work-around and revert with a bit of delay
  • Use Modelica language features naturally in MSL, don't use MSL as a gate to check all features (there's a separate repo for that)

@hubertus65

Copy link
Copy Markdown
Member Author

@HansOlsson, I agree with your principles we have applied, and did this exactly in accordance with that. I agree to those principles. FWIW: I have always tested these PR's with released versions OpenModelica, Dymola and Modelon Impact, (with the exception of Dymola lately because my normal laptop is away for repairs)

@HansOlsson

Copy link
Copy Markdown
Contributor

Thinking more, to me the main benefit of y:={f(x[k]) for k in 1:n + 1}; isn't performance, but readability.
By construction you know that it creates the entire y-vector, whereas you for the proposed loop must check that the loop-indices matches.

(You can, of course, use implicit loop-indices, but we generally don't use that in MSL - and some might argue that it is too magic.)

@casella casella left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more, to me the main benefit of y:={f(x[k]) for k in 1:n + 1}; isn't performance, but readability.

I agree with that. @AHaumer an iterator is an iterator, one never knows what kind of tricks the backend can apply behind the scene. In fact, I think the generated code could eventually be exactly the same with both formulations.

As in the previous case, I think it's best if OpenModelica is improved to also handle this case, which is perfectly legal Modelica. Please open a ticket about that on the OpenModelica issue tracker.

@AHaumer

AHaumer commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

@casella I checked with Dymola, OpenModelica and Wolfram System Modeler.
The model compiles woth all three without problems.
So: No ticket at OM, but closing this PR with "works for me"

@AHaumer AHaumer closed this Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

L: Math Issue addresses Modelica.Math worksforme Clarified issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants