How to determine value from previous time step during simulation in Modelica? - 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.

Related

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

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

How to make a parameter only count once in Anylogic model during simulation?

I am having a hard time trying to solve a very easy issue I can imagine, but I just can't see it.
Namely, I am building a dynamic simulation model which calculates accumulated costs and benefits.
However, I have introduced a 5 year time span for the model but there are certain costs and benefits which occur only once (in the first year for example). But currently, the model is using these parameters in each simulation for every year. How can model it such that these values are only taken into the simulation once?
Surely there is some kind of formula which could help me with this. The AnyLogic support page did not help me either.
For applying values that change over time, you should use a "Dynamic Variable" object and set it to 0 after one year as follows (assuming "999" is replaced yb whatever value you want):

scipy.integrate.odeint time dependend stepsize

I have the following problem:
I have to use an ode-solver to solve a chemical reaction equation. The rate constants are functions of time and can suddenly change (puls from electric discharge).
One way to solve this is to keep the stepsize very small hmax < dt. This results in a high comp. affort --> time consuming. My question is: Is there an efficient way to make this work? I thought about to def hmax(puls_ON) with plus_ON=True within the puls and plus_ON=False between. However, since dt is increasing in time, it may dose not even recognize the puls, because the time interval is growing hmax=hmax(t).
A time-grid would be the best option I thin, but I don't think this is possible with odeint?
Or is it possible to somehow force the solver to integrate at a specific point in time (e.g. t0 ->(hmax=False)->tpuls_1_start->(hmax=dt)->tpuls_1_end->(hmax=False)->puls_2_start.....)?
thx
There is an optional parameter tcrit for the odeint that you could try:
Vector of critical points (e.g. singularities) where integration care should be taken.
I don't know what it actually does but it may help to not simply step over the pulse.
If that does not work you can of course manually split your integration into different intervals. Integrate until your tpuls_1_start. Then restart the integration using the results from the previous one as initial values.

How I could make a temperature sweep in comsol?

I make a structure using Comsol then I want to make this structure subjected to a temperature variation ( T(begain)=25C then a temperature ramp (100 C/min) till T=250C and it lasts for 30 min then another temperature ramp (-100 C/min) till T=25C ).How could I make these temperature sweep?
You can define a function (e.g foo) that follows exactly your desired temperature with time profile. Then in the place where you specify your temperature (whether it is a boundary condition or domain condition) you insert foo(t), t being COMSOL's exclusive variable name for time.
You can do that for other variables too, space for instance. The easiest way to define foo is through the 1D interpolation option. Unfortunately, I do not currently have a COMSOL license to check it but I think you can simply enter the time and temperature values in the 1D interpolation table, choose a name and the interpolant style and just use it in the later part of the program.
I'am simulating magnetic fields in time domain with moving coils. Time dependent solver is needed for the movement and for temperature ramps as well. I think that you can use something like this, T=T_start+rate_of_change*t. The t variable is available with the time dependent solver and you can simply write the equation I mentioned. However, I think that you need to use time dependent solver three times, one for ramp up second for the constant temperature and third for the ramp down. Set the times for time dependent solvers so that you can made the desired temperatures.
First t=0s->(225/100*60)135s
second t=135s->(135+30*60)1935s
and last one t=1935s->(1935+135)2070s
You might also need to use compile solutions steps as well to add these three solutions together. I can try to do this tomorrow and check it.
Hope that this helped a bit

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