Skip to content

Commit f617d0c

Browse files
committed
Adapt tolerance for zero crossings to output step size in Cpp runtime
This appears needed for some electrical examples that use very short time horizons.
1 parent 5f1d838 commit f617d0c

14 files changed

+69
-32
lines changed

OMCompiler/Compiler/Template/CodegenCppOld.tpl

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7113,10 +7113,15 @@ template DefaultImplementationCode(SimCode simCode, Text& extraFuncs, Text& extr
71137113
}
71147114

71157115
// Set current integration time
7116-
void <%lastIdentOfPath(modelInfo.name)%>::setTime(const double& t)
7116+
void <%lastIdentOfPath(modelInfo.name)%>::setTime(double t)
71177117
{
71187118
SystemDefaultImplementation::setTime(t);
71197119
}
7120+
// Set tolerance for zero crossings
7121+
void <%lastIdentOfPath(modelInfo.name)%>::setZeroTol(double dt)
7122+
{
7123+
SystemDefaultImplementation::setZeroTol(dt);
7124+
}
71207125
// Computes the conditions of time event samplers for the current time
71217126
double <%lastIdentOfPath(modelInfo.name)%>::computeNextTimeEvents(double currTime)
71227127
{
@@ -7609,7 +7614,10 @@ case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__))) then
76097614
virtual void initTimeEventData();
76107615

76117616
/// Set current integration time
7612-
virtual void setTime(const double& time);
7617+
virtual void setTime(double time);
7618+
7619+
/// Set tolerance for zero crossings
7620+
virtual void setZeroTol(double dt);
76137621

76147622
// System is able to provide the Jacobian symbolically
76157623
virtual bool provideSymbolicJacobian();
@@ -12322,30 +12330,30 @@ template giveZeroFunc3(Integer index1, Exp relation, Text &varDecls /*BUFP*/,Tex
1232212330
case LESS(__) then
1232312331
<<
1232412332
if(_conditions[<%zerocrossingIndex%>])
12325-
f[<%index1%>]=(<%e1%> - 1e-9 - <%e2%>);
12333+
f[<%index1%>]=(<%e1%> - _zeroTol - <%e2%>);
1232612334
else
12327-
f[<%index1%>]=(<%e2%> - <%e1%> - 1e-9);
12335+
f[<%index1%>]=(<%e2%> - <%e1%> - _zeroTol);
1232812336
>>
1232912337
case LESSEQ(__) then
1233012338
<<
1233112339
if(_conditions[<%zerocrossingIndex%>])
12332-
f[<%index1%>] = (<%e1%> - 1e-9 - <%e2%>);
12340+
f[<%index1%>] = (<%e1%> - _zeroTol - <%e2%>);
1233312341
else
12334-
f[<%index1%>] = (<%e2%> - <%e1%> - 1e-9);
12342+
f[<%index1%>] = (<%e2%> - <%e1%> - _zeroTol);
1233512343
>>
1233612344
case GREATER(__) then
1233712345
<<
1233812346
if(_conditions[<%zerocrossingIndex%>])
12339-
f[<%index1%>] = (<%e2%> - <%e1%> - 1e-9);
12347+
f[<%index1%>] = (<%e2%> - <%e1%> - _zeroTol);
1234012348
else
12341-
f[<%index1%>] = (<%e1%> - 1e-9 - <%e2%>);
12349+
f[<%index1%>] = (<%e1%> - _zeroTol - <%e2%>);
1234212350
>>
1234312351
case GREATEREQ(__) then
1234412352
<<
1234512353
if(_conditions[<%zerocrossingIndex%>])
12346-
f[<%index1%>] = (<%e2%> - <%e1%> - 1e-9);
12354+
f[<%index1%>] = (<%e2%> - <%e1%> - _zeroTol);
1234712355
else
12348-
f[<%index1%>] = (<%e1%> - 1e-9 - <%e2%>);
12356+
f[<%index1%>] = (<%e1%> - _zeroTol - <%e2%>);
1234912357
>>
1235012358
else
1235112359
<<

OMCompiler/SimulationRuntime/cpp/Core/System/SystemDefaultImplementation.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ SystemDefaultImplementation::SystemDefaultImplementation(IGlobalSettings *global
4242
, __daeResidual (NULL)
4343
, _conditions (NULL)
4444
, _time_conditions(NULL)
45+
, _zeroTol (1e-6)
4546
, _dimContinuousStates(0)
4647
, _dimRHS (0)
4748
, _dimReal (0)
@@ -84,6 +85,7 @@ SystemDefaultImplementation::SystemDefaultImplementation(SystemDefaultImplementa
8485
, __daeResidual (NULL)
8586
, _conditions (NULL)
8687
, _time_conditions(NULL)
88+
, _zeroTol (1e-6)
8789
, _dimContinuousStates(0)
8890
, _dimRHS (0)
8991
, _dimReal (0)
@@ -281,17 +283,29 @@ void SystemDefaultImplementation::initialize()
281283
};
282284

283285
/// Set current integration time
284-
void SystemDefaultImplementation::setTime(const double& t)
286+
void SystemDefaultImplementation::setTime(double t)
285287
{
286288
_simTime = t;
287-
};
289+
}
288290

289291
// Get current integration time
290292
double SystemDefaultImplementation::getTime()
291293
{
292294
return _simTime;
293295
}
294296

297+
/// Set tolerance for zero crossings
298+
void SystemDefaultImplementation::setZeroTol(double dt)
299+
{
300+
_zeroTol = dt;
301+
}
302+
303+
// Get tolerance for zero crossings
304+
double SystemDefaultImplementation::getZeroTol()
305+
{
306+
return _zeroTol;
307+
}
308+
295309
/// Set status of independent variables
296310
void SystemDefaultImplementation::setFreeVariablesLock(bool freeVariablesLock)
297311
{

OMCompiler/SimulationRuntime/cpp/Include/Core/System/IEvent.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class IEvent
2929
virtual int getDimClock() = 0;
3030
/// Provides current values of root/zero functions
3131
virtual void getZeroFunc(double* f) = 0;
32-
32+
/// Set tolerance for zero crossings
33+
virtual void setZeroTol(double dt) = 0;
34+
/// Set and get conditions
3335
virtual void setConditions(bool* c) = 0;
3436
virtual void getConditions(bool* c) = 0;
3537
virtual void getClockConditions(bool* c) = 0;

OMCompiler/SimulationRuntime/cpp/Include/Core/System/ITime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ class ITime
1717
virtual void resetTimeConditions() = 0;
1818

1919
/// Set current integration time
20-
virtual void setTime(const double& time) = 0;
20+
virtual void setTime(double time) = 0;
2121
};
2222
/** @} */ // end of coreSystem

OMCompiler/SimulationRuntime/cpp/Include/Core/System/SystemDefaultImplementation.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation
121121
/// (Re-) initialize the system of equations
122122
void initialize();
123123
/// Set current integration time
124-
void setTime(const double& t);
124+
void setTime(double t);
125125
double getTime();
126+
/// Set tolerance for zero crossings
127+
void setZeroTol(double dt);
128+
double getZeroTol();
126129

127130
/// Set modification status of independent variables
128131
// (exploited by linear equation systems in Jacobians)
@@ -170,8 +173,8 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation
170173
shared_ptr<ISimObjects> _simObjects;
171174

172175
double
173-
_simTime; ///< current simulation time (given by the solver)
174-
176+
_simTime, ///< current simulation time (given by the solver)
177+
_zeroTol; ///< tolerance for zero crossings (given by the solver)
175178

176179
bool
177180
* _conditions, ///< External conditions changed by the solver

OMCompiler/SimulationRuntime/cpp/Solver/DASSL/DASSL.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ void DASSL::initialize()
279279
_info[7] = 1;
280280
_rwork[2] = _settings->gethInit();
281281

282+
// Adapt tolerances for zero crossings and end time to output interval
283+
_event_system->setZeroTol(1e-6 * _settings->getGlobalSettings()->gethOutput());
284+
_settings->setEndTimeTol(1e-6 * _settings->getGlobalSettings()->gethOutput());
285+
282286
LOGGER_WRITE_END(LC_SOLVER, LL_DEBUG);
283287
}
284288

testsuite/openmodelica/cppruntime/outputFormat/Modelica.Blocks.Examples.BooleanNetwork1_csv.mos

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ runScript(modelTesting);getErrorString();
3030
// OpenModelicaModelTesting.Kind.VerifiedSimulation
3131
// Modelica.Blocks.Examples.BooleanNetwork1
3232
// {"showValue4.showActive","showValue5.showActive","showValue6.showActive"}
33-
// Simulation options: startTime = 0.0, stopTime = 10.0, numberOfIntervals = 500, tolerance = 1e-6, method = 'dassl', fileNamePrefix = 'Modelica.Blocks.Examples.BooleanNetwork1', options = '', outputFormat = 'csv', variableFilter = '.*', cflags = '', simflags = ''
33+
// Simulation options: startTime = 0.0, stopTime = 10.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Modelica.Blocks.Examples.BooleanNetwork1', options = '', outputFormat = 'csv', variableFilter = 'time|showValue4.showActive|showValue5.showActive|showValue6.showActive', cflags = '', simflags = ' -emit_protected'
3434
// Result file: Modelica.Blocks.Examples.BooleanNetwork1_res.csv
3535
// Files Equal!
3636
// "true

testsuite/openmodelica/cppruntime/outputFormat/Modelica.Blocks.Examples.BooleanNetwork1_mat.mos

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ runScript(modelTesting);getErrorString();
3030
// OpenModelicaModelTesting.Kind.VerifiedSimulation
3131
// Modelica.Blocks.Examples.BooleanNetwork1
3232
// {"showValue4.showActive","showValue5.showActive","showValue6.showActive"}
33-
// Simulation options: startTime = 0.0, stopTime = 10.0, numberOfIntervals = 500, tolerance = 1e-6, method = 'dassl', fileNamePrefix = 'Modelica.Blocks.Examples.BooleanNetwork1', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''
33+
// Simulation options: startTime = 0.0, stopTime = 10.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Modelica.Blocks.Examples.BooleanNetwork1', options = '', outputFormat = 'mat', variableFilter = 'time|showValue4.showActive|showValue5.showActive|showValue6.showActive', cflags = '', simflags = ' -emit_protected'
3434
// Result file: Modelica.Blocks.Examples.BooleanNetwork1_res.mat
3535
// Files Equal!
3636
// "true

testsuite/openmodelica/cppruntime/outputFormat/Modelica.Blocks.Examples.BooleanNetwork1_none.mos

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ end if;
3131
// messages = ""
3232
// end SimulationResult;
3333
// ""
34-
//
34+
//
3535
// endResult

testsuite/simulation/libraries/msl32_cpp/Modelica.Electrical.Analog.Examples.NandGate.mos

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
runScript("../common/ModelTestingDefaults.mos"); getErrorString();
1111

12-
modelTestingType := OpenModelicaModelTesting.Kind.SimpleSimulation;
12+
modelTestingType := OpenModelicaModelTesting.Kind.VerifiedSimulation;
1313
modelName := $TypeName(Modelica.Electrical.Analog.Examples.NandGate);
1414
compareVars :=
1515
{
@@ -25,12 +25,13 @@ runScript(modelTesting);getErrorString();
2525
// "true
2626
// "
2727
// ""
28-
// OpenModelicaModelTesting.Kind.SimpleSimulation
28+
// OpenModelicaModelTesting.Kind.VerifiedSimulation
2929
// Modelica.Electrical.Analog.Examples.NandGate
3030
// {"Nand.C4.v","Nand.C7.v"}
3131
// OpenModelicaModelTesting.SimulationRuntime.Cpp
3232
// Simulation options: startTime = 0.0, stopTime = 1e-07, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'Modelica.Electrical.Analog.Examples.NandGate', options = '', outputFormat = 'mat', variableFilter = 'time|Nand.C4.v|Nand.C7.v', cflags = '', simflags = ' -emit_protected'
3333
// Result file: Modelica.Electrical.Analog.Examples.NandGate_res.mat
34+
// Files Equal!
3435
// "true
3536
// "
3637
// ""

0 commit comments

Comments
 (0)