How to use old value of variable with new value of variable in closed cycle in Openmodellica - modelica

I want to adjust the mass flow rate of a pump by using similarities laws so if I want to head and flow rate equation. I need to use old value and new value of head so that I can calculate new value of mass flow rate.
Can anybody tell me how can I write this in program in OpenModellica.

Generally Modelica is not designed to access "old values" of a continuous variable, as this is usually not necessary to model physical behavior. For discrete events there is the pre() operator, for clocks previous() but none of them is useful in continuous modeling.
What is common in continuous modeling, is to have relations based on the derivative der() of a variable, but not "old values". Personally I would double-check if you really need an old value or if that is just a form of abstraction chosen due to limitations of other software...
Still you can use the delay() operator as shown below to delay a signal by a fixed or a variable amount of time.
model DelayExample
Real x, y_fix, y_var;
equation
x = time;
y_fix = delay(x, 100e-3);
y_var = delay(x, min(time/2,0.2), 0.2);
end DelayExample;
The result is the following:
If you want to do use graphical modeling, you can use blocks from Modelica.Blocks.Nonlinear, namely
Modelica.Blocks.Nonlinear.FixedDelay
Modelica.Blocks.Nonlinear.PadeDelay
Modelica.Blocks.Nonlinear.VariableDelay

Related

How to make the dynamic model in Dymola agree with the steady-state design result?

Modelica modeling is the first principle modeling, so how to test the model and set an effective benchmark is important, for example, I could design a fluid network as my wish, but when building a dynamic simulation model, I need to know the detailed geometry structure and parameters to set up every piece of my model. Usually, I would build a steady-state model with simple energy and mass conservation laws, then design every piece of equipment based on the corresponding design manual, but when I put every dynamic component together, when simulation till steady-state, the result is different from the steady-state model more or less. So I was wondering if I should modify my workflow to make the dynamic model agree with the steady-state model. Any suggestions are welcome.
#dymola #modelica
To my understanding of the question, your parameter values are fixed and physically known. I would attempt the following approach as a heuristic to identify the (few) component(s) that one needs to carefully investigate in order to understand how they influence or violates the assumed first principles.
This is just as a first trial and it could be subject to further improvement and fine-tuning.
Consider the set of significant set of variables xd(p,t) \in R^n and parameters p. Note that p also includes significant start values. p in R^m includes only the set of additional parameters not available in the steady state model.
Denote the corresponding variables of the steady state model by x_s
Denote a time point where the dynamic model is "numerically" in "semi-" steady-state by t*
Consider the function C(xd(p,t*),xs) = ||D||^2 with D = xd(p,t*) - xs
It could be beneficial to describe C as a vector rather than a single valued function.
Compute the partial derivatives of C w.t. p expressed in terms of dxd/dp, i.e.
dC/dp = d[D^T D]/dp
= d[(x_d-x_s)^T (x_d - x_s)]/dp
= (dx_d/dp)^T D + ...
Consider scaling the above function, i.e. dC/dp * p/C (avoid expected numerical issues via some epsilon-tricks)
Here you get a ranking of most significant parameters which are causing the apparent differences. The hopefully few number of components including these parameters could be the ones causing such violation.
If this still does not help, may be due to expected high correlation among parameters, I would go further and consider a dummy parameter identification problem, out of which a more rigorous ranking of significant model parameters can be obtained.
If the Modelica language had capabilities for expressing dynamic parameter sensitivities, all the above computation can be easily carried out as a single Modelica model (with a slightly modified formulation).
For instance, if we had something like der(x,p) corresponding to dx/dp, one could simply state
dcdp = der(C,p)
An alternative approach is proposed via the DerXP library

A general question about Modelica initialization

How to set values to all the variables that could be possibly used as iteration variables, for example, there is a heat exchanger which includes a few connectors, and each connector includes a few variables, I can't know which variables could be used as iteration variables, when dealing with initialization, do I need to set values to every variable so that no matter which variable is chosen as iteration variable, there is a reasonable value?
Marvel,
I think that you are a bit on the wrong track for finding a solution: setting values to all variables that possibly could become iteration variables is often too many, and will lead to errors and problems. But I think I can give you some useful advice in any case.
Alias variables: there are many alias variable sin Modelica models. You should always try to only select one of them to set start values.
Feedback between start values and iteration variables: most Modelica tools will prefer to select iteration variables that have start values set. Selecting fewer thus can guide the algorithm towards selecting good one. Therefore: don't overdo it.
General advice for selecting iteration variables. For a pure ODE, the states will always be a complete set of start variables, even if sometimes not the best one. For DAE you can start with the following exercise: think of all equations that result from a singular perturbation of the complete physics as differential equations with states. For example, in a heat exchanger, you need to consider the dynamic momentum balance and not the most often used static reduction to an algebraic pressure loss only, i.e. add the mass flow as a state. Similar in chemical reactions: think of it as Kinetics, not equilibrium reactions. That gives you a pretty good starting point, even though often not the best one.
If your troubles don't quite resolve from that, I recommend that you contact us via www.modelon.com: we have advanced ways of dealing with hard initialization and steady state problems in our Modelic tool. :-)
There is also a simplest way to answer your question, working quite well with fluid models.
Giving the fact that you are using a dynamic model, what you need to initialize are the state variables of your system. To know the state variables, either you know the type of model you are wirking with or you can dig through them using options like 'List continuous time states selected' in Dymola (I do not know about other tools), giving you the state variables in the translation log.
In case of fluid models, most of the times those are pressure and energy (enthalpy or temperature). All other variables will be calculated based on them.
For complex (or not) models, this approach show limits, which can sometimes be solved by changing/correcting the structure of the model.
Static models are something else...
Hope this can help :)

How could I redefine or change the value of a predefined parameter in Dymola during the simulation?

I am building model in Dymola. I have defined the mass of this model as a parameter, because it would be transfered into other moduls and called in them. But the mass should be changing during the simulation in different time intervals. For example, during the first 100 seconds the mass should remain 500kg, and during 100 to 200 sec, a passenger is going to get in, so that a new mass should be calculated including the mass of the passenger. But it has been showed, that "The problem is structurally singular", because to the parameter values have been twice assigned. Could someone give some tips to solve this problem? Thanks a lot.
If you define the mass of your component as an input rather than a parameter then you can change it during simulation by assigning e.g. the output from a TimeTable to it. For example
model Component
input Modelica.SIunits.Mass mass "Passenger dependent mass";
equation
...
end Component;
model systemModel
TimeTable timeTable;
Component component(mass=timeTable.y);
OtherComponent otherComponent(mass=component.mass);
equation
...
end systemModel;
Note that the other components using the mass must also have their internal mass 'parameters' defined as input to allow higher variability than parameters.
Best regards
Rene Just Nielsen
Modelica parameters are defined by the fact, that they don't change over time. Therefore you would need to stop the simulation, change the parameter and restart the simulation (see another question). Given you description I would rather not use this possibility, as it seems your variable is designed to change over time.
A better alternative seems to be defining the mass as a variable. If this is done, you can:
Transfer this variable from one model to the others using interfaces. This could be a bit tedious depending on the amount of classes using the variable.
Use inner/outer (basically global variables) is a feasible concept for this use-case. This concept is used in the MultiBody libraries world model.
With both solutions you will have to modify the original mass model, as m would then have to be a variable instead of a mass.

How to determine value from previous time step during simulation in Modelica?

How can I determine value from previous time step during simulation in Modelica?
I have equation Q=m*c*(Ts2-Ts1-Tr) I need to extract value of Ts2 and Ts1 from it.
Ts2 - is the value from time step 2
Ts1 - is value from previous time step
Ts is input signal and it has variations during the time. Each step
it has different value. In my case time step is 1s. Other values are
fixed.
Can I set in equation variable time?
For example:
Ts2 (start=time);
Ts (start=time-1);
Or it should be input inside this model?
regards Tymofii
This was addressed in a similar question already.
The key point is that equations describing physical behavior cannot refer to time steps. This is because there is no "timestep" in nature or the laws of physics and so the response of a system cannot depend on it.
You don't really explain why you need to do what you are doing. Are you trying to extract simulation results? Are you trying to correlate to experimental data? Or, are you just trying to solve a differential equation?
It isn't clear what you want to do. Please elaborate and we can probably give you some guidance on how to proceed in Modelica.
Update
Using values from a "previous interval" is fine. For example, if you wanted to sample your solution at regular intervals, express a "z transform" or implement a Kalman Filter in Modelica, you could do each of those very easily (for example, see the 'sample' keyword here). In other words, it is possible to store as many previous values as you would like.
What you cannot do is use the timestep of the continuous solver in expressing how your system behaves. The intervals you reference must be independent from any intervals that the solver is using.

How to use the value of the variable in the previous interval as an input to the equation....?

Is it possible to use the previous value of the time varying variable
for eg:
Suppose I have pipe whose inlet temperature is 298K with a specified uniform mass flow(m_flow), now suppose i am heating the pipe using a heater of 100 watts.
The outlet temperature will be attain a higher temperature of suppose 302K, now if i have to use this outlet temperature as my inlet temperature (in the sense i am recircuilating the water), how would i be doing it?
is it possible to update the value of the inlet temperature based on the outlet temperature at the previous timestep? so that for the next iteration the inlet temperature will be the same as the oulet temperature in the previous iteration (in other words the fluid would be recirculating).
Thanks
You cannot access the value in the previous time step. The closest you can get in Modelica is using delay(exp,T) to get the value T units of time ago.
The timestep does not enter into it at all. A model that uses information about timestep is just wrong. Nature doesn't know or care about integration time steps, the model should reflect that.
It seems to me what you want to capture is transport delay. Transport delay is the delay introduced by the time it takes for molecules, electrons, etc. through the system. So presumably what you wish to model is the time it takes the inlet fluid to reach the exit. Again, this has nothing to do with the integration timestep but rather the velocity of the fluid and the distance it must travel. Once you know how long that takes (by either a priori knowledge of the system of by looking at the simulation results themselves), you can follow Marco's suggestion of using the delay operator.
In order to setup a proper model for the system you described I suggest you to look at the example :
Modelica.Thermal.FluidHeatFlow.Examples.IndirectCooling
of the modelica standard library ver. 3.2. Instead of one pipe you can put an ambient or control volume component to better suit you needs. Moreover using continous and differentiable equations (the delay function is not) you will benefit from some of the advantages of the Modelica code, e.g. you will be able to reuse your models in a much wider range of cases, solve inverse problems, solve initial value problems, ...
I hope this helps,
Marco