Skip to content

Commit

Permalink
Consider the derivatives at boundaries in the 2D-table.
Browse files Browse the repository at this point in the history
Updated to current date
  • Loading branch information
HansOlsson committed Jan 31, 2022
1 parent 27bf6ec commit 1607d74
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 14 deletions.
40 changes: 26 additions & 14 deletions Modelica/Resources/C-Sources/ModelicaStandardTables.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
Modelica.Blocks.Tables.CombiTable2Dv
Changelog:
Jan. 31, 2022: by Hans Olsson
Added better support for one-sided derivatives of 2d-tables.
The idea is that when we are computing the derivative at a boundary
in the table we should consider the der-value to choose side.
This is less important for 1d-tables and thus ignored in those cases.
(ticket #3893 )
Nov. 12, 2021: by Thomas Beutlich
Fixed derivatives in CombiTable2D for one-sided extrapolation
by constant continuation (ticket #3894)
Expand Down Expand Up @@ -4217,6 +4225,8 @@ double ModelicaStandardTables_CombiTable2D_getDerValue(void* _tableID, double u1
const double u1Max = TABLE_COL0(nRow - 1);
const double u2Min = TABLE_ROW0(1);
const double u2Max = TABLE_ROW0(nCol - 1);
const double du1 = der_u1 > 0 ? u1*DBL_EPSILON : -u1*DBL_EPSILON;
const double du2 = der_u2 > 0 ? u2*DBL_EPSILON : -u2*DBL_EPSILON;

if (nRow == 2) {
if (nCol > 2) {
Expand All @@ -4238,20 +4248,20 @@ double ModelicaStandardTables_CombiTable2D_getDerValue(void* _tableID, double u1
} while (u2 > u2Max);
}
last2 = findColIndex(&TABLE(0, 1), nCol - 1,
tableID->last2, u2);
tableID->last2, u2 + du2);
tableID->last2 = last2;
}
else if (u2 < u2Min) {
else if (u2 + du2 < u2Min) {
extrapolate2 = LEFT;
last2 = 0;
}
else if (u2 > u2Max) {
else if (u2 + du2 > u2Max) {
extrapolate2 = RIGHT;
last2 = nCol - 3;
}
else {
last2 = findColIndex(&TABLE(0, 1), nCol - 1,
tableID->last2, u2);
tableID->last2, u2 + du2);
tableID->last2 = last2;
}

Expand Down Expand Up @@ -4383,20 +4393,20 @@ double ModelicaStandardTables_CombiTable2D_getDerValue(void* _tableID, double u1
} while (u1 > u1Max);
}
last1 = findRowIndex(&TABLE(1, 0), nRow - 1, nCol,
tableID->last1, u1);
tableID->last1, u1 + du1);
tableID->last1 = last1;
}
else if (u1 < u1Min) {
else if (u1 + du1 < u1Min) {
extrapolate1 = LEFT;
last1 = 0;
}
else if (u1 > u1Max) {
else if (u1 + du1 > u1Max) {
extrapolate1 = RIGHT;
last1 = nRow - 3;
}
else {
last1 = findRowIndex(&TABLE(1, 0), nRow - 1, nCol,
tableID->last1, u1);
tableID->last1, u1 + du1);
tableID->last1 = last1;
}
if (nCol == 2) {
Expand Down Expand Up @@ -4527,20 +4537,20 @@ double ModelicaStandardTables_CombiTable2D_getDerValue(void* _tableID, double u1
} while (u2 > u2Max);
}
last2 = findColIndex(&TABLE(0, 1), nCol - 1,
tableID->last2, u2);
tableID->last2, u2 + du2);
tableID->last2 = last2;
}
else if (u2 < u2Min) {
else if (u2 + du2 < u2Min) {
extrapolate2 = LEFT;
last2 = 0;
}
else if (u2 > u2Max) {
else if (u2 + du2 > u2Max) {
extrapolate2 = RIGHT;
last2 = nCol - 3;
}
else {
last2 = findColIndex(&TABLE(0, 1), nCol - 1,
tableID->last2, u2);
tableID->last2, u2 + du2);
tableID->last2 = last2;
}

Expand Down Expand Up @@ -5138,6 +5148,8 @@ double ModelicaStandardTables_CombiTable2D_getDer2Value(void* _tableID, double u
const double u1Max = TABLE_COL0(nRow - 1);
const double u2Min = TABLE_ROW0(1);
const double u2Max = TABLE_ROW0(nCol - 1);
const double du1 = (der_u1 > 0) || (der_u1==0 && der2_u1>0) ? u1*DBL_EPSILON : -u1*DBL_EPSILON;
const double du2 = (der_u2 > 0) || (der_u2==0 && der2_u2>0) ? u2*DBL_EPSILON : -u2*DBL_EPSILON;

if (nRow == 2) {
if (nCol > 2) {
Expand All @@ -5159,7 +5171,7 @@ double ModelicaStandardTables_CombiTable2D_getDer2Value(void* _tableID, double u
} while (u2 > u2Max);
}
last2 = findColIndex(&TABLE(0, 1), nCol - 1,
tableID->last2, u2);
tableID->last2, u2 + du2);
tableID->last2 = last2;
}
else if (u2 < u2Min) {
Expand All @@ -5172,7 +5184,7 @@ double ModelicaStandardTables_CombiTable2D_getDer2Value(void* _tableID, double u
}
else {
last2 = findColIndex(&TABLE(0, 1), nCol - 1,
tableID->last2, u2);
tableID->last2, u2 + du2);
tableID->last2 = last2;
}

Expand Down
80 changes: 80 additions & 0 deletions ModelicaTest/Tables/CombiTable2Ds.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1125,4 +1125,84 @@ double mydummyfunc(double dummy_in) {
points={{-59,-10},{-52,-10},{-52,4},{-42,4}}, color={0,0,127}));
annotation (experiment(StartTime=0, StopTime=14));
end Test33;

model OneSidedDerivative2D "Test of one sided derivatives in 2D-tables"
// We are diagonally sliding through the table
extends Modelica.Icons.Example;
parameter Real M0[:,:]=[0,-10,1,2,10; -10,2,2,3,4; 1,2,2,3,3; 2,3,3,4,4; 10,3,
3,4,4];
parameter Real M[:,:]=[M0[1:1,1:1],M0[1:1,3:end-1];M0[3:end-1,1],M0[3:end-1,3:end-1]];
Modelica.Blocks.Sources.Ramp ramp(
height=1,
duration=1,
offset=1)
annotation (Placement(transformation(extent={{-76,12},{-56,32}})));
Modelica.Blocks.Sources.Ramp ramp1(
height=-1,
duration=1,
offset=2)
annotation (Placement(transformation(extent={{-80,-36},{-60,-16}})));
Modelica.Blocks.Tables.CombiTable2Ds combiTable2Ds1(
tableOnFile=false,
table=M,
smoothness=Modelica.Blocks.Types.Smoothness.LinearSegments,
extrapolation=Modelica.Blocks.Types.Extrapolation.HoldLastPoint)
annotation (Placement(transformation(extent={{40,20},{60,40}})));
Modelica.Blocks.Continuous.Der der1
annotation (Placement(transformation(extent={{80,20},{100,40}})));
Modelica.Blocks.Tables.CombiTable2Ds combiTable2Ds2(
tableOnFile=false,
table=M0,
smoothness=Modelica.Blocks.Types.Smoothness.LinearSegments,
extrapolation=Modelica.Blocks.Types.Extrapolation.LastTwoPoints)
annotation (Placement(transformation(extent={{40,60},{60,80}})));
Modelica.Blocks.Continuous.Der der2
annotation (Placement(transformation(extent={{80,60},{100,80}})));
Modelica.Blocks.Tables.CombiTable2Ds combiTable2Ds3(
tableOnFile=false,
table=M,
smoothness=Modelica.Blocks.Types.Smoothness.LinearSegments,
extrapolation=Modelica.Blocks.Types.Extrapolation.HoldLastPoint)
annotation (Placement(transformation(extent={{40,-80},{60,-60}})));
Modelica.Blocks.Continuous.Der der3
annotation (Placement(transformation(extent={{80,-80},{100,-60}})));
Modelica.Blocks.Tables.CombiTable2Ds combiTable2Ds4(
tableOnFile=false,
table=M0,
smoothness=Modelica.Blocks.Types.Smoothness.LinearSegments,
extrapolation=Modelica.Blocks.Types.Extrapolation.LastTwoPoints)
annotation (Placement(transformation(extent={{40,-40},{60,-20}})));
Modelica.Blocks.Continuous.Der der4
annotation (Placement(transformation(extent={{80,-40},{100,-20}})));
equation
connect(combiTable2Ds1.y, der1.u)
annotation (Line(points={{61,30},{78,30}}, color={0,0,127}));
connect(combiTable2Ds2.y, der2.u)
annotation (Line(points={{61,70},{78,70}}, color={0,0,127}));
connect(ramp.y, combiTable2Ds2.u1) annotation (Line(points={{-55,22},{14,22},
{14,76},{38,76}}, color={0,0,127}));
connect(ramp.y, combiTable2Ds1.u1) annotation (Line(points={{-55,22},{14,22},
{14,36},{38,36}}, color={0,0,127}));
connect(ramp1.y, combiTable2Ds2.u2) annotation (Line(points={{-59,-26},{20,
-26},{20,64},{38,64}}, color={0,0,127}));
connect(ramp1.y, combiTable2Ds1.u2) annotation (Line(points={{-59,-26},{20,
-26},{20,24},{38,24}}, color={0,0,127}));
connect(combiTable2Ds3.y,der3. u)
annotation (Line(points={{61,-70},{78,-70}}, color={0,0,127}));
connect(combiTable2Ds4.y, der4.u)
annotation (Line(points={{61,-30},{78,-30}}, color={0,0,127}));
connect(ramp.y, combiTable2Ds4.u2) annotation (Line(points={{-55,22},{14,22},
{14,-36},{38,-36}}, color={0,0,127}));
connect(combiTable2Ds3.u2, combiTable2Ds4.u2) annotation (Line(points={{38,
-76},{14,-76},{14,-36},{38,-36}}, color={0,0,127}));
connect(combiTable2Ds4.u1, combiTable2Ds2.u2) annotation (Line(points={{38,
-24},{30,-24},{30,-22},{20,-22},{20,64},{38,64}}, color={0,0,127}));
connect(combiTable2Ds3.u1, combiTable2Ds2.u2) annotation (Line(points={{38,
-64},{20,-64},{20,64},{38,64}}, color={0,0,127}));
annotation (
experiment(
StartTime=-1,
StopTime=4));
end OneSidedDerivative2D;

end CombiTable2Ds;

0 comments on commit 1607d74

Please sign in to comment.