Modelica: calculates the minimum / maximum of a continuous variable over time

As stated above: I want to calculate the minimum (and / or maximum) of a continuous variable over time. Here is a minimal example to demonstrate:

model MinMaxTest
  Real u;
  Real u_min(start = 10);
  Real u_max(start = -10);
equation
  u = sin(time / 180 * Modelica.Constants.pi);
  u_min = min(u, u_min);
  u_max = max(u, u_max);
  annotation(experiment(StartTime = 0, StopTime = 360, Tolerance = 1e-06, Interval = 1));
end MinMaxTest;

u- an arbitrary continuous variable (for demonstration purposes - a simple sinus wave). u_min/ u_maxis minimum / maximum over time.

Obviously, the expected result u_min=-1and u_max=1. Unfortunately, the simulation crashes with the "Singular Matrix!" error. Can someone guide me how to avoid this?


EDIT 1

I am using OpenModelica 1.15 (was 1.9.2)

EDIT 2

Since I'm completely new to Modelica, I try my best to understand the differences between the following approaches:

  • u_min = if noEvent(u < u_min) then u else pre(u_min);
  • if noEvent(u < u_min) then u_min = u; else u_min = pre(u_min); end if;
  • u_min = if noEvent(u < u_min) then u else u_min;
  • u_min = if u < u_min then u else pre(u_min);
  • u_min = if u < u_min then u else u_min;
  • when u < u_min then u_min = u; end when;
  • u_min + T*der(u_min) = if u <= u_min then u else u_min;

1 2 .

3 , " " " ", ?

4 , u_min u?! ?

5 3 4.

6 Sorry - Support for Discrete Equation Systems is not yet implemented

7 , , , T .

Modelica, 1-5 , . noEvent . , . 4, ? pre , , , , , 7 ? when , , 6. , , ).

EDIT3

  1. u_min = smooth(0, if u < u_min then u else pre(u_min));

, .

+4
3

Dymola 2016, , . Modelica , .

u_min = min(u, u_min);

, , . Modelica , .

, .

Statistics

Original Model
Number of components: 1
Variables: 3
Unknowns: 3 (3 scalars)
Equations: 3
Nontrivial: 3

Translated Model
Time-varying variables: 3 scalars
Number of mixed real/discrete systems of equations: 0
Sizes of linear systems of equations: { }
Sizes after manipulation of the linear systems: { }
Sizes of nonlinear systems of equations: {1, 1}
Sizes after manipulation of the nonlinear systems: {1, 1}
Number of numerical Jacobians: 0

, : u_min u_max.

:

model Test
  Real x;
  Real y;
  Real u_min;
  Real u_max;
  parameter Real T = 1e-4;
equation 
  x = sin(time) + 0.1*time;
  y = sin(time) - 0.1*time;
  u_min + T*der(u_min) = if y <= u_min then y else u_min;
  u_max + T*der(u_max) = if x >= u_max then x else u_max;

end Test;

u_min u_max , x y, . , x , u_max, u_max "" , .

, , .

+3

, , , u_min = min(u,u_min). u_min u u_min u_min, u, , . , , :

  u_min = min(u, delay(u_min,0));
  u_max = max(u, delay(u_max,0));

:

  • u_min = if noEvent(u < u_min) then u else pre(u_min);
  • if noEvent(u < u_min) then u_min = u; else u_min = pre(u_min); end if;

, . , - , u_min u pre(u_min), .

  1. u_min = if noEvent(u < u_min) then u else u_min;

, min() , u_min u u_min, .

  1. u_min = u < u_min, u else pre (u_min);

noEvent() if-, u < u_min, u_min = u .

  1. u_min = if u < u_min then u else u_min;

, 3 4.

  1. when u < u_min the u_min = u; end when;

u_min u_min u.

  1. u_min + T*der(u_min) = if u <= u_min then u else u_min;

u_min - , u_min , der (u_min), u_min.

+2

, , , OpenModelica, :

u_min = min(u, pre(u_min));
u_max = max(u, pre(u_max));

, , " !". , u_max :

Real u_max(start = 0);

Then "The Matrix is ​​the only one!" leaves.
I don't know why, but it seems to do the job, and I would suggest simpler than the other options that you indicated.

+1
source

All Articles