Skip to content

Commit f802d4d

Browse files
lvanfrettidietmarw
authored andcommitted
Division by zero protection block and function added
1 parent a8c6758 commit f802d4d

10 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
within OpenIPSL.NonElectrical.Functions;
2+
function div0protect "Division avoiding 0 by replacing b with eps if requried"
3+
extends Modelica.Icons.Function;
4+
import eps=Modelica.Constants.small;
5+
6+
// Public variables
7+
input Real a "Dividend";
8+
input Real b "Divisor";
9+
output Real c "Quotient";
10+
11+
algorithm
12+
c := a/(max(b,eps));
13+
14+
annotation (
15+
Documentation(info="<html>
16+
<p>This function computes <code>c=a/b</code>. It avoids divisions by zero by replacing
17+
<code>a/b</code> with <code>a/eps</code> or <code>a/b</code> whichever of the divisors is larger.
18+
</p>
19+
</html>"));
20+
end div0protect;

OpenIPSL/NonElectrical/Functions/package.order

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
displayPower
2+
div0protect
23
ImSE
34
ImSE_exp
45
SE
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
within OpenIPSL.NonElectrical.Nonlinear;
2+
block Div0block "Block that implements division by zero protection"
3+
Modelica.Blocks.Interfaces.RealInput u1
4+
annotation (Placement(transformation(extent={{-140,40},{-100,80}})));
5+
Modelica.Blocks.Interfaces.RealInput u2
6+
annotation (Placement(transformation(extent={{-140,-80},{-100,-40}})));
7+
Modelica.Blocks.Interfaces.RealOutput y
8+
annotation (Placement(transformation(extent={{100,-10},{120,10}})));
9+
equation
10+
y =OpenIPSL.NonElectrical.Functions.div0protect(u1, u2)
11+
annotation (Icon(coordinateSystem(preserveAspectRatio=false)), Diagram(
12+
coordinateSystem(preserveAspectRatio=false)));
13+
annotation (Icon(graphics={ Rectangle(
14+
extent={{-100,-100},{100,100}},
15+
lineColor={0,0,127},
16+
fillColor={255,255,255},
17+
fillPattern=FillPattern.Solid), Text(
18+
extent={{-150,150},{150,110}},
19+
textString="%name",
20+
textColor={0,0,255}),
21+
Line(points={{-100,60},{-60,60},{0,0}}, color={0,0,127}),
22+
Line(points={{-100,-60},{-60,-60},{0,0}}, color={0,0,127}),
23+
Ellipse(lineColor={0,0,127}, extent={{-50,-50},{50,50}},
24+
fillColor={255,255,255},
25+
fillPattern=FillPattern.Solid),
26+
Line(points={{50,0},{100,0}}, color={0,0,127}),
27+
Line(points={{-30,0},{30,0}}),
28+
Ellipse(fillPattern=FillPattern.Solid, extent={{-5,20},{5,30}}),
29+
Ellipse(fillPattern=FillPattern.Solid, extent={{-5,-30},{5,-20}}),
30+
Text(
31+
extent={{-70,94},{80,54}},
32+
textColor={128,128,128},
33+
textString="u1 / u2"),
34+
Text(
35+
extent={{-102,-60},{100,-100}},
36+
textColor={238,46,47},
37+
textString="div0protect")}));
38+
end Div0block;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CeilingBlock
2+
Div0block
23
FEX
34
FrequencyCalc
45
SaturationBlockTan
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
within OpenIPSL.Tests.NonElectrical.Nonlinear;
2+
model Div0Block "Test the block Div0Block"
3+
extends Modelica.Icons.Example;
4+
OpenIPSL.NonElectrical.Nonlinear.Div0block div0block1
5+
annotation (Placement(transformation(extent={{-20,-20},{20,20}})));
6+
Modelica.Blocks.Sources.RealExpression num(y=1.0)
7+
annotation (Placement(transformation(extent={{-60,2},{-40,22}})));
8+
Modelica.Blocks.Sources.RealExpression den(y=0.0)
9+
annotation (Placement(transformation(extent={{-60,-22},{-40,-2}})));
10+
equation
11+
connect(num.y, div0block1.u1)
12+
annotation (Line(points={{-39,12},{-24,12}}, color={0,0,127}));
13+
connect(den.y, div0block1.u2)
14+
annotation (Line(points={{-39,-12},{-24,-12}}, color={0,0,127}));
15+
end Div0Block;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
within OpenIPSL.Tests.NonElectrical;
2+
package Nonlinear "Test examples for nonlinear components"
3+
extends Modelica.Icons.ExamplesPackage;
4+
end Nonlinear;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Div0Block
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
within OpenIPSL.Tests;
2+
package NonElectrical "Test examples for nonelectrical components"
3+
extends Modelica.Icons.ExamplesPackage;
4+
end NonElectrical;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Nonlinear

OpenIPSL/Tests/package.order

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ Sources
1111
ThreePhase
1212
Wind
1313
BaseClasses
14+
NonElectrical

0 commit comments

Comments
 (0)