- Index
- » Programming
- » Modelica Language
- » The logic behind der() operator
The logic behind der() operator
The logic behind der() operator
Hi,
I am trying to understand how Modelica solver calculates the value of 'der(X)' where X is a variable. Minimal example:
Real X1(start=1);
Real X2(start=0);
Real X3(start=0);
equation
der(y1) = -0.04*y1 + 1e4*y2 * y3;
der(y2) = 0.04 * y1 - 1e4 * y2 * y3 - 3e7 * y2 ^ 2;
der(y3) = 3e7 * y2 ^ 2;
When I print out the results after simulating it with -noEquidistantGrid flag and manually calculate the dy/dt :
dydt = (y_next-y)/dt
where dt, y_next and y are delta time, y at t+Δt and y at t (calculated manually after printed out the result), the value does not match with the reported der(y) from the Modelica simulation result.
Anybody knows how does Modelica calculates the derivative of y w.r.t time? Or how to calculate the dydt manually from the Modelica result file?
Thanks a lot!
Re: The logic behind der() operator
As far as I remember if you are using dassl, it internally has some polynomial expression for what the derivative is estimated to be at each point. You won't be able to reconstruct the actual values from the derivatives alone.
If you change solver to Euler you should have better luck.
- sjoelund.se
- 1700 Posts
Re: The logic behind der() operator
As far as I remember if you are using dassl, it internally has some polynomial expression for what the derivative is estimated to be at each point. You won't be able to reconstruct the actual values from the derivatives alone.
If you change solver to Euler you should have better luck.
- sjoelund.se
- 1700 Posts
Re: The logic behind der() operator
Thanks for replying to my question.
So if using DASSL, the value of der(y) reported by Modelica is the only way to grab the derivative value, is it not?
Actually, I have some other questions, which related to der() operator and how the DASSL solver solves the problem. I am trying to replace a physics of a model with a surrogate model SM which is constructed/trained to predict der(x) given time (dtype=double) outside Modelica and is called via an external C-function.
Minimal example:
Real x(start=0);
equation
//real equation: der(x) = 3 * time^2 + 2 * time + 3;
/*Surrogate Call*/
der(x) = SM(time);
What I found is that the solver takes twice the amount of time-stepping (and slow as well) when using the surrogate model compared to the real function. Do you know the reason behind this? Do you think this approach (replacing the derivative with a surrogate model) is feasible to be implemented in Modelica with DASSL solver?
Thanks a lot.
Re: The logic behind der() operator
If you have a function call like that, I assume you don't have a derivative or inline annotation. If that's the case, a numerical Jacobian will be calculated instead of a symbolic one. This will be somewhat slower as you experience.
- sjoelund.se
- 1700 Posts
Re: The logic behind der() operator
Hi Sjoelund,
Thanks for your prompt response. You're right that I have neither derivative nor inline annotation in the function definition. I was struggling to understand how (numerically) to implement these two annotations in the problem context that I am having.
If it is not too troubling you, would you mind giving me any pointer to where I should read more about the implementation of those annotations, or probably where a minimal example can be found? I read the Modelica specification handbook, but I don't get how the "derivative" and "inline" annotation can be used in my problem.
From what I understood, derivative annotation is useful to guide the solver to read another function that computes the derivative of the function being annotated. Given the fact that the function call (surrogate model/SM) is used to predict the der(y), how can I use the derivative annotation in the SM definition, since SM it self is used to calculate the derivative of y w.r.t. to time (dydt) and SM function call does not have any derivative?
The same about the inline function, how to implement it in the context that I am having?
Thanks a lot for your time
Cheers,
Re: The logic behind der() operator
https://mbe.modelica.university/behavio … unc_annos/
Regarding the derivative, dassl will probably want the derivative of der(y) to create its polynomials.
For inlining, OpenModelica will not inline complicated functions even if you add the annotation.
- sjoelund.se
- 1700 Posts
Re: The logic behind der() operator
Oh I see, so since dassl needs the derivative of a function to create polynomials for der(y), it makes the simulation slow. The surrogate model that I am trying to use is a neural-network, which does not have derivative w.r.t time, while the example from modelica by example website, the function definition is a polynomial which, can be derived w.r.t time. So is it always the case that dassl wants the derivative of a function call to be able to calculate the derivative of a state?
Regarding inlining, what is the limit of complexity of a function that is still able to be inlined? Is an external C function inline-able? What will be your suggestion to overcome this slow-simulation speed problem? Should I use polynomial-based surrogate model instead of neural-net?
Thanks for your prompt response!
Cheers
Re: The logic behind der() operator
Hi,
I saw your answer and I think you can help me about the integation methods in modelica.
Actually, I try to translate a Mtlab code to modelica,
so my question how I can translate this instruction from matlab to openmodelica.
Code:
[t_7,T_7]=ode45('Modello3',tspan,T0_7)
can you help me please?
Re: The logic behind der() operator
Hi,
I saw your answer and I think you can help me about the integation methods in modelica.
Actually, I try to translate a Mtlab code to modelica,
so my question how I can translate this instruction from matlab to openmodelica.
Code:
[t_7,T_7]=ode45('Modello3',tspan,T0_7)
can you help me please?
Re: The logic behind der() operator
Hi,
I saw your answer and I think you can help me about the integation methods in modelica.
Actually, I try to translate a Mtlab code to modelica,
so my question how I can translate this instruction from matlab to openmodelica.
Code:
[t_7,T_7]=ode45('Modello3',tspan,T0_7)
can you help me please?
Re: The logic behind der() operator
The closest match would be adding a solver for a discrete (clocked) system: https://specification.modelica.org/main … er-methods
- sjoelund.se
- 1700 Posts
- Index
- » Programming
- » Modelica Language
- » The logic behind der() operator