AJodeiri
AJodeiri

Reputation: 314

Mean of a time variable in Modelica

I have a simple model as follows and I intend to calculate the mean of x derivative wrt time(der(x)).

model Average
  Real x;
initial equation 
  x = 2.0;
equation 
  der(x) = x + 5;
  annotation (experiment(StopTime=10, __Dymola_Algorithm="Dassl"));
end Average;

The point is that it is a simplified code of an original one in which "x" is given by a CombiTimeTable. I need to perform several simulations (by several text files as inputs to the CombiTimeTable) that they do not have the same number of rows (different durations). In other words, StopTime is different from case to case e.g. here the StopTime is 10s for this specific case. Is there any way to use a general equation to calculate the mean of der(x). I would appreciate if someone could help.

Upvotes: 2

Views: 444

Answers (2)

marco
marco

Reputation: 6655

Based on the answer of sjoelund.se, I created a model which computes the mean value of der(x) via 2 methods:

  • using the block Modelica.Blocks.Math.ContinuousMean from the MSL (instead of Mean, as it does not sample)
  • computing the mean with (x2-x1)/(time2-time1) when the simulation terminates
model Average
  import Modelica.Constants.pi;
  import Modelica.Utilities.Streams.print;

  Real x;
  Real time1, time2, x1, x2;
  Modelica.Blocks.Math.ContinuousMean continuousMean;

equation 

  der(x) = sqrt(1+cos(2*pi*2*time))+sin(time)^2;

  // Method 1: Continuous mean from MSL
  continuousMean.u = der(x);

  when terminal() then
    print("Continuous mean: "+String( continuousMean.y));
  end when;

  // Method 2: Mean at end time
  when initial() then
    x1 = x;
    time1 = time;
  end when;

  when terminal() then
    x2 = x;
    time2 = time;
    print("Computed mean: "+String( (x2-x1)/(time2-time1)));
  end when;

  annotation (experiment(StopTime=1.0, __Dymola_Algorithm="Dassl"));
end Average;

Both values are printed at simulation end. They give a similar value as the mean post processing command which you can apply in Dymola on plotted signals:

simulation output and plot of mean of der(x)

Another possibility is to call the mean signal operator in Dymola after the simulation has ended (maybe in a script which runs your simulation):

DymolaCommands.Plot.signalOperatorValue("der(x)", SignalOperator.ArithmeticMean, 0, 1);

Upvotes: 3

sjoelund.se
sjoelund.se

Reputation: 3523

Couldn't you just calculate the mean as (x2-x1)/(time2-time1), given that you want the mean of der(X)?

Use Modelica.Blocks.Math.Mean if you want a numerical solution instead of an analytic. Note that it samples the signal so there will be some numerical errors due to that. In order to improve the accuracy, you need to reduce the period of sampling but doing so increases the time it takes to simulate the system. If you do not need to use the mean during simulation it's also possible to use post-processing of the result-file.

Upvotes: 3

Related Questions