OpenModelica 抱怨不能为负的负值
OpenModelica complains about a negative value which can't be negative
遵循 this question I have modified the energy based controller which I have described here 以避免 sqrt
中出现负值:
model Model
//constants
parameter Real m = 1;
parameter Real k = 2;
parameter Real Fmax = 3;
parameter Real x0 = 1;
parameter Real x1 = 2;
parameter Real t1 = 5;
parameter Real v0 = -2;
//variables
Real x, v, a, xy, F, vm, K;
initial equation
x = x0;
v = v0;
equation
v = der(x);
a = der(v);
m * a + k * x = F;
algorithm
if time < t1 then
xy := x0;
else
xy := x1;
end if;
K := Fmax * abs(xy - x) + k * (xy^2 - x^2) / 2;
if abs(xy - x) < 1e-6 then
F := k * x;
else
if K > 0 then
vm := sign(xy - x) * sqrt(2 * K / m);
F := Fmax * sign(vm - v);
else
F := Fmax * sign(x - xy);
end if;
end if;
annotation(
experiment(StartTime = 0, StopTime = 20, Tolerance = 1e-06, Interval = 0.001),
__OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "euler"));
end Model;
但是,它一直给我错误:
The following assertion has been violated at time 7.170000
Model error: Argument of sqrt(K / m) was -1.77973e-005 should be >= 0
Integrator attempt to handle a problem with a called assert.
The following assertion has been violated at time 7.169500
Model error: Argument of sqrt(K / m) was -6.5459e-006 should be >= 0
model terminate | Simulation terminated by an assert at the time: 7.1695
STATISTICS
Simulation process failed. Exited with code -1.
如果您能帮助我了解问题所在以及如何解决,我将不胜感激。
您创建的代码执行事件本地化以查明 if 语句中的条件何时变为真 and/or 假。在此搜索过程中,平方根中的表达式可能会变为负数,尽管您使用 if 语句 'avoided' 它。
尝试阅读 this 并应用那里提供的解决方案。剧透:它基本上归结为为你的布尔条件添加一个 noEvent()
语句......
遵循 this question I have modified the energy based controller which I have described here 以避免 sqrt
中出现负值:
model Model
//constants
parameter Real m = 1;
parameter Real k = 2;
parameter Real Fmax = 3;
parameter Real x0 = 1;
parameter Real x1 = 2;
parameter Real t1 = 5;
parameter Real v0 = -2;
//variables
Real x, v, a, xy, F, vm, K;
initial equation
x = x0;
v = v0;
equation
v = der(x);
a = der(v);
m * a + k * x = F;
algorithm
if time < t1 then
xy := x0;
else
xy := x1;
end if;
K := Fmax * abs(xy - x) + k * (xy^2 - x^2) / 2;
if abs(xy - x) < 1e-6 then
F := k * x;
else
if K > 0 then
vm := sign(xy - x) * sqrt(2 * K / m);
F := Fmax * sign(vm - v);
else
F := Fmax * sign(x - xy);
end if;
end if;
annotation(
experiment(StartTime = 0, StopTime = 20, Tolerance = 1e-06, Interval = 0.001),
__OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "euler"));
end Model;
但是,它一直给我错误:
The following assertion has been violated at time 7.170000
Model error: Argument of sqrt(K / m) was -1.77973e-005 should be >= 0
Integrator attempt to handle a problem with a called assert.
The following assertion has been violated at time 7.169500
Model error: Argument of sqrt(K / m) was -6.5459e-006 should be >= 0
model terminate | Simulation terminated by an assert at the time: 7.1695
STATISTICS
Simulation process failed. Exited with code -1.
如果您能帮助我了解问题所在以及如何解决,我将不胜感激。
您创建的代码执行事件本地化以查明 if 语句中的条件何时变为真 and/or 假。在此搜索过程中,平方根中的表达式可能会变为负数,尽管您使用 if 语句 'avoided' 它。
尝试阅读 this 并应用那里提供的解决方案。剧透:它基本上归结为为你的布尔条件添加一个 noEvent()
语句......