Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Problem with function

Problem with function

Hi,
in the model below both H and h should have the same value. starting algorithm by giving value to protected variable Ent gives wrong value (H = 0), but commenting that line 9 gives warning that variable "Ent was used before it was given value" but gives correct result h = H. Can someone look into this?

Code:


model functiontest
  function hv
  input Real Tc;
  input Real T;
  output Real Ent2;
  protected Real temp, Ent;
  algorithm
  temp := 298.15;
  Ent :=0; //if we comment this and run, we'll get correct result
   if T> 298.15 then
    while temp < T loop
      Ent := Ent + (temp) * 1;
      temp := temp + 1;
    end while;
    Ent2 := Ent;
    else
    while temp > T loop
      Ent := Ent + (temp) * 1;
      temp := temp - 1;
    end while;
    Ent2 := -Ent;
    end if;
  end hv;
 
  function hl
  input Real Tc;
  input Real T;
  output Real Ent1;
  algorithm
  if T<Tc then
  Ent1 := hv(Tc, T) + (1-T/Tc)^2;
  else
  Ent1 := hv(Tc, T); //this is true for T = 400 and Tc = 33
  end if;
  end hl;

parameter Real Tc = 33, T = 400;
Real H, h;

equation
H = hv(Tc, T);
h = hl(Tc, T);
end functiontest;

functiontest.mo

Attachments:

Re: Problem with function

I am not really sure what is causing the problem, but maybe its something to do with initialization.
Apart from just commenting the "Ent := 0" line, assigning any non 0 value to Ent causes the code to work (Ent := 0.00001)

Also while running the Debugger it was found that  H gets an initial value H :=0 while h get value h :=functiontest.hl(Tc,T).

Also letting Ent := 0 and putting a when loop works
when(sample(0,1))then
H = hv(Tc, T);
h = hl(Tc, T);
end when;

Not really sure what the problem is though..

Re: Problem with function

The problem was caused by an optimization module, which constantly evaluated the equation H = hv(Tc, T) to 0 and removes the equation. The problem is now fixed (commit 2cd28b6) and the above model should work as expected.

There are 0 guests and 0 other users also viewing this topic