H Bode
H Bode

Reputation: 165

Variable sampling frequency in OpenModelica

I am referring to this post:

alternative to sample function with varying sampling range

I want to measure an RMS value (or a mean value) with a variable sampling frequency, which i can feed in as an input signal.

The suggested way which nearly suits my need is the following:

model RMS
  constant Real f_max = 2*2*asin(1.0);
  constant Real f = 1+abs(2*asin(time));
  Real signal = sin(time);
  Real rms = if time < f then (if time < 1e-10 then signal else sqrt(i_sq / time)) else sqrt(i_sq_f / f);
  Real i_sq(start=0, fixed=true);
  Real i_sq_f = i_sq - delay(i_sq, f, f_max);
equation
  der(i_sq) = signal^2;
end RMS;

This one throws the error: Component f of variability CONST has binding 1.0 + abs(2.0 * asin(time)) of higher variability VAR.

Is there a way to solve this problem and feed in the frequency as a real input value?

Upvotes: 1

Views: 294

Answers (3)

H Bode
H Bode

Reputation: 165

Found a solution, some problems occurred in he example above. One with the constant Real f, one with the constant real f_max, and on positions where the time difference was needed, the frequency was used instead.

Following is a working block

model RMS
  constant Real f_max = 10000;
Modelica.Blocks.Interfaces.RealInput f annotation(
    Placement(visible = true, transformation(origin = {0, 106}, extent = {{-20, -20}, {20, 20}}, rotation = -90), iconTransformation(origin = {0, 106}, extent = {{-20, -20}, {20, 20}}, rotation = -90))); 
 Modelica.Blocks.Interfaces.RealInput signal annotation(
    Placement(visible = true, transformation(origin = {-108, 0}, extent = {{-20, -20}, {20, 20}}, rotation = 0), iconTransformation(origin = {-108, 0}, extent = {{-20, -20}, {20, 20}}, rotation = 0)));
  Real dt = 1/f;
  Real rms = if time < dt then (if time < 1e-10 then signal else sqrt(i_sq / time)) else sqrt(i_sq_f / dt);
  Real i_sq(start=0, fixed=true);  
  Real testi = delay(i_sq, dt, f_max);
  Real i_sq_f = i_sq - delay(i_sq, dt, f_max);
Modelica.Blocks.Interfaces.RealOutput y annotation(
    Placement(visible = true, transformation(origin = {110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {110, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
  der(i_sq) = signal^2;
  y = rms;
end RMS;

Edit: Your simulation step size must be greater than the time in the first if-condition. Otherwise, this programm still might throw errors (division by 0)

Upvotes: 1

Markus A.
Markus A.

Reputation: 7525

The main problem with the code is, that time is a variable, meaning it changes its value over time. This corresponds to a continuous time expression in the Modelica Language Specification (see Chapter 3.8). A variable can only be assigned to variable with equal or higher variability. Therefore it is not possible to assign something computed from time to constant, as the result will not be constant. Thus, removing the constant in the definition of f will solve the issue.

Replacing constant with input in the definition of f will make the tool expect f to be provided from the outside. Alternatively you can use Modelica.Blocks.Interfaces.RealInput.

Also f_max sounds rather like a parameter than a constant...

Besides that, if time gets greater than 1, there will be an issue with ´asin(time)´. But that is a different story...

Upvotes: 2

Rene Just Nielsen
Rene Just Nielsen

Reputation: 3413

The expression for f is time-varying but f is declared as constant.

Simply replace constant Real f = ... with input Real f = .... Then you can specify f from the "outside" of the model if you like.

Upvotes: 3

Related Questions