Opened 14 years ago

Last modified 7 years ago

#1410 new defect

Algebraic loop is generated instead of a known variable

Reported by: Martin Sjölund Owned by: Martin Sjölund
Priority: high Milestone: Future
Component: Version:
Keywords: Cc: Martin Sjölund, Frenkel, TUD, Willi Braun

Description

The following will be executed 3 times per time step, but can actually be decided statically:
Real x = sin(x)

Basically, if lhs and rhs have the same variables, and the rest of the input is constant, we should be able to solve this before simulation (if it also has some other parameter input, we should be able to solve it during initialization). Is it desirable to solve this in the backend (it could generate a system that is easier to simulate)? Anyway, the residual function generated will be called 3 times per time step and sin is not a terribly cheap operation.

Change History (4)

comment:1 by Martin Sjölund, 14 years ago

Note: This should be easy to solve since we know the derivative.
We could implement a general function for this and use CevalFunction on it, passing the expressions for x and dx by using traverseExp to modify the function itself.

comment:2 by Martin Sjölund, 14 years ago

Creating something like this an interpreting it seems to be quite fast, but generating the C-code might be better (here max 1 var works, it needs to have a known derivative != 0, etc.).

{{{function newton_rhapson_1var

input Real x;
input Real tolerance;
output Real ox;

protected

Real fx,dx;

algorithm

fx := cos(x)-x;
while abs(fx) > tolerance loop

dx := -sin(x);
x := x - fx/dx*0.5;
fx := cos(x)-x;

end while;
ox := x;

end newton_rhapson_1var;

class A

Real x = cos(y);
Real y = sin(x);
x=0.768169156736796
y=0.6948196907307876
Real nx = newton_rhapson_1var(0.01,1e-6);

equation

(nx,ny) = newton_rhapson(0.01,0.01,1e-6);

end A;}}}

I know how to implement this in the bootstrapped compiler (it involves an external C function that is able to call traverseExp+CevalFunction on a stored data-structure (the function) and a fresh table of variables to replace in it...)
Does anyone have a larger non-linear system to play around with?

Version 0, edited 14 years ago by Martin Sjölund (next)

comment:3 by Jens Frenkel, 14 years ago

If the expression is more complex for example

model Sin

  parameter Real p=3;

  Real x=p*cos(x);

end Sin;

it is not possible to calculate the solution at compile time. Hence, the solution is to add this equation to the initial equations. 

Anyway, if it is possible to calculate the solution during runtime we should do it to replace the variable with its value in the hole equation system. 

Last edited 7 years ago by Martin Sjölund (previous) (diff)

comment:4 by Dietmar Winkler, 9 years ago

Cc: Frenkel TUD added; Jens Frenkel removed
Milestone: Future
Note: See TracTickets for help on using tickets.