Skip to content

Commit

Permalink
Added assert condition block (modelica#3985)
Browse files Browse the repository at this point in the history
* Added assert condition block

- Block is useful for applying asserts via graphical layer (rather than via equations)


Co-authored-by: Henrik Tidefelt <henrikt@wolfram.com>
Co-authored-by: tobolar <jakub.tobolar@dlr.de>
Co-authored-by: Hans Olsson <HansOlsson@users.noreply.github.com>
  • Loading branch information
4 people committed Aug 26, 2022
1 parent 1c9bcb3 commit 02657f5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
35 changes: 35 additions & 0 deletions Modelica/Blocks/Logical.mo
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,40 @@ The output <code>Q</code> is set by the input <code>S</code>, is reset by the in
</html>"));
end RSFlipFlop;

block AssertCondition "Assert that input u is true"
extends Modelica.Blocks.Interfaces.partialBooleanSI;
parameter String message = "Assert block failed"
"Assertion message to be displayed if the input u is false; please adapt";
parameter AssertionLevel assertionLevel=AssertionLevel.error
"Assertion level";
equation
assert(u,message,assertionLevel);
annotation (
Icon(graphics={
Text(
extent={{-90,40},{90,-40}},
textString="assert")}),
Documentation(
info="<html>
<p>
This block asserts that the Boolean input&nbsp;<strong>u</strong> is <strong>true</strong>.
If&nbsp;<strong>u</strong> is <strong>false</strong>, the simulation will
either abort with an error (if <strong>assertionLevel</strong> is set to
<strong>error</strong>), or issue a&nbsp;warning and continue (if
<strong>assertionLevel</strong> is set to <strong>warning</strong>).
The <strong>message</strong> will be reported in either case.
For further information, refer to the documentation of
<a href=\"modelica://ModelicaReference.Operators.'assert()'\">ModelicaReference.Operators.'assert()'</a>
&ndash; an operator which this block is based on.
</p>
<p>
Typically, this block is used by connecting a&nbsp;(possibly) <strong>time
varying</strong> outside condition and the input&nbsp;<strong>u</strong>
to abort the simulation if the condition fails.
</p>
</html>"));
end AssertCondition;

block TerminateSimulation "Terminate simulation if condition is fulfilled"

Modelica.Blocks.Interfaces.BooleanOutput condition=false
Expand Down Expand Up @@ -973,6 +1007,7 @@ parameter \"terminationText\".

</html>"));
end TerminateSimulation;

annotation (Documentation(info="<html>
<p>
This package provides blocks with Boolean input and output signals
Expand Down
20 changes: 13 additions & 7 deletions ModelicaTest/Blocks.mo
Original file line number Diff line number Diff line change
Expand Up @@ -952,14 +952,19 @@ if homotopy is active, the solution accepted by the assert statement (x = 100) i
Modelica.Blocks.Logical.Hysteresis hysteresis(uLow=-0.5, uHigh=0.5)
annotation (Placement(transformation(extent={{-60,40},{-40,60}})));
Modelica.Blocks.Logical.OnOffController onOffController(bandwidth=0.5)
annotation (Placement(transformation(extent={{80,-40},{100,-20}})));
annotation (Placement(transformation(extent={{100,-140},{120,-120}})));
Modelica.Blocks.Logical.TriggeredTrapezoid triggeredTrapezoid(rising=1,
falling=0.5)
annotation (Placement(transformation(extent={{106,60},{126,80}})));
Modelica.Blocks.Logical.Timer timer
annotation (Placement(transformation(extent={{100,20},{120,40}})));
Modelica.Blocks.Logical.RSFlipFlop rSFlipFlop
annotation (Placement(transformation(extent={{140,120},{160,140}})));
Modelica.Blocks.Logical.AssertCondition assertCondition(assertionLevel=AssertionLevel.warning) annotation (Placement(transformation(extent={{100,-20},{120,0}})));
Modelica.Blocks.Sources.BooleanPulse booleanPulse(
width=50,
period=0.5,
startTime=0) annotation (Placement(transformation(extent={{60,-20},{80,0}})));
equation
connect(table1.y, and1.u1) annotation (Line(
points={{-79,110},{-62,110}}, color={255,0,255}));
Expand Down Expand Up @@ -1034,16 +1039,16 @@ if homotopy is active, the solution accepted by the assert statement (x = 100) i
connect(switch1.u1, lessEqualThreshold.u) annotation (Line(
points={{78,-62},{52,-62},{52,-112},{-72,-112},{-72,-94},{-62,-94}}, color={0,0,127}));
connect(switch1.u3, greaterEqual.u2) annotation (Line(
points={{78,-78},{66,-78},{66,-106},{106,-106},{106,-154},{-32,-154},{
-32,-138},{-22,-138}}, color={0,0,127}));
points={{78,-78},{66,-78},{66,-106},{88,-106},{88,-154},{-32,-154},{-32,-138},{-22,-138}},
color={0,0,127}));
connect(sine.y, hysteresis.u) annotation (Line(
points={{-79,-10},{-72,-10},{-72,50},{-62,50}}, color={0,0,127}));
connect(onOffController.reference, lessEqualThreshold.u) annotation (Line(
points={{78,-24},{66,-24},{66,-24},{52,-24},{52,-112},{-72,-112},{-72,
-94},{-62,-94}}, color={0,0,127}));
points={{98,-124},{94,-124},{94,-112},{-72,-112},{-72,-94},{-62,-94}},
color={0,0,127}));
connect(onOffController.u, greaterEqual.u2) annotation (Line(
points={{78,-36},{66,-36},{66,-106},{106,-106},{106,-154},{-32,-154},{
-32,-138},{-22,-138}}, color={0,0,127}));
points={{98,-136},{88,-136},{88,-154},{-32,-154},{-32,-138},{-22,-138}},
color={0,0,127}));
connect(table2.y, triggeredTrapezoid.u) annotation (Line(
points={{-79,70},{104,70}}, color={255,0,255}));
connect(timer.u, logicalSwitch.u3) annotation (Line(
Expand All @@ -1052,6 +1057,7 @@ if homotopy is active, the solution accepted by the assert statement (x = 100) i
points={{81,150},{128,150},{128,136},{138,136}}, color={255,0,255}));
connect(pre1.y, rSFlipFlop.R) annotation (Line(
points={{81,110},{130,110},{130,124},{138,124}}, color={255,0,255}));
connect(booleanPulse.y, assertCondition.u) annotation (Line(points={{81,-10},{98,-10}}, color={255,0,255}));
annotation (experiment(StopTime=10), Diagram(coordinateSystem(
preserveAspectRatio=false,extent={{-100,-180},{180,180}})));
end Logical;
Expand Down

0 comments on commit 02657f5

Please sign in to comment.