if expression works in equation, not in algorithm

8 posts / 0 new
Last post
roel
Offline
Joined: 2010-02-23
if expression works in equation, not in algorithm

Dear,

I'm trying to get a model running, and I get very unclear error messages. After some searching, it seems that the cause is an algorithm. I did some tests and could reduce the problem to this.

This code is working (JMU compiler)

model Tester_Algo
Real x;

equation
x =if time > 100 then 666 else 123;

end Tester_Algo;

but this code is NOT working (JMU compiler):

model Tester_Algo
Real x;

algorithm
x :=if time > 100 then 666 else 123;

end Tester_Algo;

Is this normal JModelica behaviour? I can't find any limitations on algorithms in the 1.6 manual.
Thanks for your help,
Roel

jmattsson
Offline
Joined: 2009-10-18
Compiler bug

Hi Roel,

I think that this is caused by a (previously unknown) bug in the handling of algorithms so that time can not be used there. As a workaround util the bug is fixed you could do:

model Tester_Algo
    Real x;
    Real t = time;
algorithm
    x := if t > 100 then 666 else 123;

Generally you should use equations instead of algoritms when you can, since equations are both better supported and more efficient.

I'll keep you posted on the progress of this bug.

Jesper

roel
Offline
Joined: 2010-02-23
Hi Jesper, Thanks for your

Hi Jesper,
Thanks for your reply.  I'm glad to help you discover new bugs :-)
However, your workaround does not work. I see two things:

  1. With FMU compiler, I get the following error:

 

------------------------------------------------------------

Traceback (most recent call last):

File "<ipython console>", line 1, in <module>

File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\startup.py", line 128, in runfile

execfile(filename, glbs)

File "C:\Workspace\Modelica\BuildingSimulation\JMScript.py", line 26, in <module>

fmu_name=compile_fmu(model_name, mo_file)

File "C:\JModelica.org-1.6\Python\jmodelica\fmi.py", line 130, in compile_fmu

comp.compile_FMU(class_name, file_name, target, compile_to)

File "C:\JModelica.org-1.6\Python\jmodelica\compiler.py", line 520, in compile_FMU

self._handle_exception(ex)

File "C:\JModelica.org-1.6\Python\jmodelica\compiler.py", line 764, in _handle_exception

raise JError(ex.stacktrace().encode('utf-8'))

JError: java.lang.ClassCastException: org.jmodelica.modelica.compiler.FFunctionCallEquation cannot be cast to org.jmodelica.modelica.compiler.FEquation

at org.jmodelica.modelica.compiler.MixedEquationBlock.genBlockResidualFunction_C(MixedEquationBlock.java:212)

at org.jmodelica.modelica.compiler.CGenerator$DAETag_C_dae_blocks_residual_functions.generate(CGenerator.java:397)

at org.jmodelica.modelica.compiler.AbstractGenerator.generate(AbstractGenerator.java:221)

at org.jmodelica.modelica.compiler.AbstractGenerator.generate(AbstractGenerator.java:163)

at org.jmodelica.modelica.compiler.ModelicaCompiler.generateCode(ModelicaCompiler.java:1671)

at org.jmodelica.modelica.compiler.ModelicaCompiler.compileModel(ModelicaCompiler.java:1242)

at org.jmodelica.modelica.compiler.ModelicaCompiler.compileUnit(ModelicaCompiler.java:1039)

at org.jmodelica.modelica.compiler.ModelicaCompiler.compileFMU(ModelicaCompiler.java:987)
 
 
 
 
    2.  With JMU compiler, the model compiles, but I get the following error when I simulate it:
 

JVM already running

WARNING:root:Could not find CasADi package, aborting.

WARNING:root:Could not find CasADi package, aborting.

Simulating with JMU

******************************************************************************

This program contains Ipopt, a library for large-scale nonlinear optimization.

Ipopt is released as open source code under the Eclipse Public License (EPL).

For more information visit http://projects.coin-or.org/Ipopt

******************************************************************************

NOTE: You are using Ipopt by default with the MUMPS linear solver.

Other linear solvers might be more efficient (see Ipopt documentation).

This is Ipopt version 3.10.0, running with linear solver mumps.

Number of nonzeros in equality constraint Jacobian...: 2

Number of nonzeros in inequality constraint Jacobian.: 0

Number of nonzeros in Lagrangian Hessian.............: 0

Total number of variables............................: 2

variables with only lower bounds: 0

variables with lower and upper bounds: 0

variables with only upper bounds: 0

Total number of equality constraints.................: 2

Total number of inequality constraints...............: 0

inequality constraints with only lower bounds: 0

inequality constraints with lower and upper bounds: 0

inequality constraints with only upper bounds: 0

iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls

0 0.0000000e+000 1.23e+002 0.00e+000 0.0 0.00e+000 - 0.00e+000 0.00e+000 0

1 7.5645000e+003 0.00e+000 0.00e+000 -11.0 1.23e+002 - 1.00e+000 1.00e+000h 1

Number of Iterations....: 1

(scaled) (unscaled)

Objective...............: 7.5645000000000000e+003 7.5645000000000000e+003

Dual infeasibility......: 0.0000000000000000e+000 0.0000000000000000e+000

Constraint violation....: 0.0000000000000000e+000 0.0000000000000000e+000

Complementarity.........: 0.0000000000000000e+000 0.0000000000000000e+000

Overall NLP error.......: 0.0000000000000000e+000 0.0000000000000000e+000

Number of objective function evaluations = 2

Number of objective gradient evaluations = 2

Number of equality constraint evaluations = 2

Number of inequality constraint evaluations = 0

Number of equality constraint Jacobian evaluations = 2

Number of inequality constraint Jacobian evaluations = 0

Number of Lagrangian Hessian evaluations = 0

Total CPU secs in IPOPT (w/o function evaluations) = 0.000

Total CPU secs in NLP function evaluations = 0.000

EXIT: Optimal Solution Found.

------------------------------------------------------------

Traceback (most recent call last):

File "<ipython console>", line 1, in <module>

File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\startup.py", line 128, in runfile

execfile(filename, glbs)

File "C:\Workspace\Modelica\BuildingSimulation\JMScript.py", line 48, in <module>

res=mymodel_jmu.simulate(final_time=600, options=opts)

File "C:\JModelica.org-1.6\Python\jmodelica\jmi.py", line 2023, in simulate

options)

File "C:\JModelica.org-1.6\Python\jmodelica\core.py", line 188, in _exec_simulate_algorithm

alg.solve()

File "C:\JModelica.org-1.6\Python\jmodelica\algorithm_drivers.py", line 1117, in solve

self.simulator.simulate(self.final_time, self.ncp)

File "C:\JModelica.org-1.6\Python\assimulo\ode.py", line 268, in simulate

self.__call__(tfinal,ncp)

File "C:\JModelica.org-1.6\Python\assimulo\implicit_ode.py", line 249, in __call__

solution = list(self._integrator(self.t_cur, self.y_cur, self.yd_cur, tfinal,dt))

File "C:\JModelica.org-1.6\Python\assimulo\implicit_ode.py", line 783, in _integrator

return self.Integrator.run(t,tfinal,nt)

File "sundials_core.pyx", line 1349, in lib.sundials_core.IDA_wrap.run (src\lib\sundials_core.c:17858)

File "sundials_core.pyx", line 1374, in lib.sundials_core.IDA_wrap.run (src\lib\sundials_core.c:17149)

IDAError: 'The solver took max internal steps but could not reach tout. At time 100.000000.'
 
 
I would really like this to work with an algorithm.  Actually the code I wanted to get running has a for loop with N equations in the algorithm, each one defining the same x.  That way I want to implement a piecewise constant function to define inputs for optimizations.  It works in Dymola and I think it should work also in JModelica.
Thanks for your help,
Roel

jmattsson
Offline
Joined: 2009-10-18
FMU is fixed

Hi Roel
The FMU problem is due to a bug that was introduced just before christmas. Try updating to the latest trunk version and it should be solved. The JMU issue I don't think I can help you with. Is that when you compile the example you sent, or when you compile your real code?
Johan, can you help with the JMU issue?
 
I'm afraid I don't quite understand youd description of what you want to do, could you post an example of that?
Jesper

roel
Offline
Joined: 2010-02-23
what I want to do

Jesper,
Thanks for reply.  I never used the trunk versions before, I'll try to do get into that :-)
 
The errors occur when i try to simulate the simple example I sent.
 
I'll try to explain why I need it.  It's closely related to another post on the forum (http://www.jmodelica.org/4436 - I'm still hoping to get more answers to that issue :-o).  The point is that in the optimisations we like to do, be it parameter estimations or optimal control, we have weather files and sometimes also user behaviour, ... as input data. 
 
When I look at the parameter estimation example in the tutorial (chapter 8.6), I see how the inputs u1 and u2 to the tanks are defined: there's a big difference between the simulation, where you can indeed pass an array as input object (this is fine) and optimization, where this is not possible. The solution developed for the optimization is very unsatisfactory for me: the inputs u1 and u2 are hard coded in a modelica model (PRBS1 and PRBS2).  For u1, this model looks like this:

model PRBS1

    Modelica.Blocks.Interfaces.RealOutput y;

    parameter Integer N = 10;

    parameter Real ts[N] = {  0. ,   3.3,   9.3,  15.3,  24.3,  36.3,  39.3,  42.3,  54.3,  57.3};

    parameter Real ys[N] = { 5.,  6.,  5.,  6.,  5.,  6.,  5.,  6.,  5.,  6.};

   equation

    y = noEvent(if time <= ts[2] then ys[1] else

        if time <= ts[3] then ys[2] else

        if time <= ts[4] then ys[3] else

        if time <= ts[5] then ys[4] else

        if time <= ts[6] then ys[5] else

        if time <= ts[7] then ys[6] else

        if time <= ts[8] then ys[7] else

        if time <= ts[9] then ys[8] else

        if time <= ts[10] then ys[9] else ys[10]);

  end PRBS1;

 

I also understand that it's possible to set each value in ts and ys in a JMU model with the .set() method.  So I was looking at a way to rewrite the equation block of the code above in order to make it work for any input array.  Interpolation is not necessary at this stage, I can live with piecewise constant inputs. 

 

The code I developed (works in Dymola) looks like this:

 
block PiecewiseConstant2 

  "Generate a signal for JModelica optimizations"
   parameter Integer n=6;
   parameter Real[n] times = {0,100,200,500,520,1000};
   parameter Real[n] signal = {0,1,3,0,2,0};
 
protected 
   Real y_pre;
public 
  Modelica.Blocks.Interfaces.RealOutput y(start=signal[1]);
   
algorithm 
  for i in 1:n-1 loop
    y :=  noEvent(if time >= times[i] and time < times[i+1]  then signal[i] else y_pre);
    y_pre :=  y;
  end for;
 
end PiecewiseConstant2;

 

If this model would work with the JMU compiler, I can set all the values for time and signal with whatever array I want, and I can do optimisations taking into account input variables.  I hope I'm not overlooking something.

 

Do you think this could work and could you check why the JMU compiler cannot handle this?

Thanks on beforehand,

Roel

 

 

 

 

roel
Offline
Joined: 2010-02-23
forum bug

I discovered an strange bug in the JModelica forum: it seems that the last reply of Jesper didn't make it to the forum, although I got a mail with the first lines of his message. 
 
Here's the mail I got:

roel,

Your are receiving this email because you have chosen to receive email  
notifications from JModelica.org (http://www.jmodelica.org). To change this,  
click the link below to go to your personal settings and disable email  
notifications:
http://www.jmodelica.org/user/86/watcher/settings

------------------------------------------

jmattsson wrote a new comment about 'if expression works in equation, not in  
algorithm':
http://www.jmodelica.org/4796

Comment excerpt:
"The only problem I see with that is if you want to be able to change the  
value of n after compilation - that will not work. Otherwise the workaround I  
suggested should work (well, that and setting a s..."

To read the rest, view the entire comment by clicking the link below:
http://www.jmodelica.org/4796#comment-2090

Click the link below to reply to this comment:
http://www.jmodelica.org/comment/reply/4796/2090

------------------------------------------

To stop watching this post, click the link below:
http://www.jmodelica.org/user/86/watcher/toggle/4796

If you believe you have received this message in error, please contact the  
site administrator at the following email address:
no-reply@jmodelica.org

Sincerely,

The staff at JModelica.org -
 
 
I cannot find the rest of this message on the forum!!
roel

roel
Offline
Joined: 2010-02-23
post again

Jesper,
(see previous message)
Can you post your answer again?
 
thanks,
Roel

jmattsson
Offline
Joined: 2009-10-18
Erroneous post

Hi Roel
 
I'm sorry, I forgot to reply about the missing post.
 
The reason the post wasn't there was that I posted an answer, then decided that it was a bad answer, and deleted it.
Sorry about the confusion.
 
Jesper

Login or register to post comments