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

Example that does not work. What is wrong?

Example that does not work. What is wrong?

Below you find a model from the Modelica 2.1 book of Peter Fritzson. It simulates but the results are not oké.

The variable CtrlValveFlow.inlet.mdot looks correct, but the variable pressureEnthalpySource.outlet.p is zero (0) but should be 1.2e5. What could be wrong?

I do get the warning:
[dck1.mo:2:3-7:20:writable] Warning: Connector dck1.FlowConnector is not balanced: The number of potential variables (2) is not equal to the number of flow variables (1).
Is this the problem?


Code:


package dck1
  connector FlowConnector
    import Modelica.SIunits.*;
    Pressure              P;
    flow MassFlowRate     mdot;
    SpecificEnthalpy      h;
  end FlowConnector;

  model PressureEnthalpySource
    import Modelica.SIunits.*;
    FlowConnector outlet;
    parameter Pressure P_0=1.0e5;
    parameter SpecificEnthalpy h_0=300190;
  equation
    outlet.P = P_0;
    outlet.h = h_0;
  end PressureEnthalpySource;

  model ControlledValveFlow
    import Modelica.SIunits;
    import Modelica.Blocks.Interfaces;
    FlowConnector           inlet, outlet;
    Interfaces.RealInput    u;
    parameter SIunits.Area  A = 1e-4;
    parameter Real          beta = 5.0e-5;
  equation
    // Conservation of mass
    0 = inlet.mdot + outlet.mdot;
    // Conservation of energy
    0 = inlet.mdot*inlet.h + outlet.mdot*outlet.h;
    beta*(A*u)^2*(inlet.P - outlet.P) = inlet.mdot^2;
    assert(inlet.P - outlet.P > 0, "Error: model not valid for backflow");
  end ControlledValveFlow;

  model PressureSink
    FlowConnector inlet;
    parameter Modelica.SIunits.Pressure P_0=1.0e5;
  equation
    inlet.P = P_0;
  end PressureSink;

  model CtrlFlowSystem
    import Modelica.Blocks.Sources;
    PressureEnthalpySource   pressureEnthalpySource(P_0=1.2e5);
    Sources.Step             step(offset=1,startTime=0.5,height=-0.5);
    ControlledValveFlow      ctrlValveFlow;
    PressureSink             pressureSink;
  equation
    connect(pressureEnthalpySource.outlet, ctrlValveFlow.inlet);
    connect(ctrlValveFlow.outlet, pressureSink.inlet);
    connect(step.y, ctrlValveFlow.u);
  end CtrlFlowSystem;
end dck1;

Re: Example that does not work. What is wrong?

What version of OpenModelica are you using? The following gave the correct result in r14428

Code:

loadModel(Modelica);getErrorString();

loadFile("a.mo");getErrorString();
simulate(dck1.CtrlFlowSystem);getErrorString();
val(pressureEnthalpySource.outlet.P,0);getErrorString();

Re: Example that does not work. What is wrong?

I'm using r14545.

I also get a good result when I use OMShell and your script commands. (a.mo probably should be dck1.mo in your script?)

Although when I use the 'outputformat="csv" I do not get the same values in the 'csv' file!

Could you check this.

Re: Example that does not work. What is wrong?

The problem was caused by the following line:

Code:


beta*(A*u)^2*(inlet.P - outlet.P) = inlet.mdot^2;

The solution method choose for inlet.mdot the negative value as the solution. It should have been the positive value, because the inlet.P pressure is higher than the outlet.P pressure.

So i changed the code to:

Code:


sqrt(beta*(A*u)^2*(inlet.P - outlet.P)) = inlet.mdot

Now all looks oké!

Is there a way of coding that can avoid such programmer "mistakes"?

There are 0 guests and 0 other users also viewing this topic
You are here: