1
$\begingroup$

I have questions about the use of "derivative" annotation I wanted to ask you. Lets put a sample.

The equation is this:

z_level = LevelZ(time);

where LevelZ(time) is an external C function, and there are also LevelZ_der(time) and LevelZ_der2(time) that are the first and second derivative.

It the code is like this, it works well...

function LevelZ_der2 
    input Real t; 
    input Real dt; 
    input Real ddt; 
    output Real res; 
external 
    res = LevelZ_der2(t); 
annotation(Library="...", Include="...");  
end LevelZ_der2; 

function LevelZ_der 
    input Real t 
    input Real dt 
    output Real res; 
external 
    res = LevelZ_der(t); 
annotation(derivative=LevelZ_der2, 
           Library="...", Include="...");  
end LevelZ_der; 

function LevelZ 
    input Real t 
    output Real res; 
external 
    res = LevelZ(t) 
annotation(derivative=LevelZ_der, 
           derivative(order=2)=LevelZ_der2, 
           Library="...", Include="...");  
end LevelZ;

... although these errors appear:

[C:/Users/Model/Model.mo:52:2-52:28:writable] Warning: Unused input variable dt in function .LevelZ_der2. 
[C:/Users/Model/Model.mo:52:2-52:28:writable] Warning: Unused input variable ddt in function .LevelZ_der2. 
[C:/Users/Model/Model.mo:52:2-52:28:writable] Warning: Unused input variable dt in function .LevelZ_der. 

Error: Derivative of expression 
Differentiate.differentiateExpSolve failed for z_level - LevelZ(time) 
is non-existent.

However if the code is like this, OpenModelica simulation ignores the derivatives...

function LevelZ_der2 
    input Real t; 
    output Real res; 
external 
    res = LevelZ_der2(t); 
annotation(Library="...", Include="...");  
end LevelZ_der2; 

function LevelZ_der 
    input Real t 
    output Real res; 
external 
    res = LevelZ_der(t); 
annotation(derivative(zeroDerivative=t)=LevelZ_der2, 
           Library="...", Include="...");  
end LevelZ_der; 

function LevelZ 
    input Real t 
    output Real res; 
external 
    res = LevelZ(t) 
annotation(derivative(zeroDerivative=t)=LevelZ_der,  
           derivative(order=2)=LevelZ_der2, 
           Library="...", Include="...");  
end LevelZ;

... and this error appear:

Error: Derivative of expression 
Differentiate.differentiateExpSolve failed for z_level - LevelZ(time) 
is non-existent.

What is the right way to do it and why?

Best regards Koldo

$\endgroup$
2
$\begingroup$

Remove derivative(order=2)=LevelZ_der2, from the derivative-annotation of LevelZ and replace the derivative annotation of LevelZ_der with derivative(order=2)=LevelZ_der2.

See Section 12.7.1 "Using the Derivative Annotation" of the Modelica specification Version 3.3.

It is a problem of the tool if the external functions are not accepted after this correction.

$\endgroup$
  • $\begingroup$ Thank you Tobias. Equations work properly with your advice. However error and warning messages remain. $\endgroup$ – Koldo Nov 28 '14 at 21:56
  • $\begingroup$ If you did not make any other error then the error and warning messages are a problem of the tool -- and maybe, you should report it. $\endgroup$ – Tobias Nov 28 '14 at 22:30
  • $\begingroup$ I have reported it in the Forum but I have not received any answer. However I will report it to the bug tracker. $\endgroup$ – Koldo Nov 30 '14 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.