Skip to content

Commit

Permalink
Adjust minimal step size (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel committed Jun 4, 2019
1 parent 43bbd73 commit 4e286fa
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 85 deletions.
Expand Up @@ -937,6 +937,7 @@ int initRuntimeAndSimulation(int argc, char**argv, DATA *data, threadData_t *thr

rt_tick(SIM_TIMER_INIT_XML);
read_input_xml(data->modelData, data->simulationInfo);
data->simulationInfo->minStepSize = 4.0 * DBL_EPSILON * fmax(fabs(data->simulationInfo->startTime),fabs(data->simulationInfo->stopTime));
rt_accumulate(SIM_TIMER_INIT_XML);

/* initialize static data of mixed/linear/non-linear system solvers */
Expand Down
16 changes: 5 additions & 11 deletions OMCompiler/SimulationRuntime/c/simulation/solver/epsilon.h
Expand Up @@ -35,33 +35,27 @@
* used in events.c in function checkForSampleEvent and handleEvents
* to detect sample time.
*/
static
const double SAMPLE_EPS = 1e-14;
static const double SAMPLE_EPS = 1e-14;

static
const double SYNC_EPS = 1e-14;
static const double SYNC_EPS = 1e-14;

/*
* used in dassl.c for function dasrt_step
* to prevent dassl errors, because of too small step size.
*/
static
const double DASSL_STEP_EPS = 1e-13;
static const double DASSL_STEP_EPS = 1e-13;

/*
* used in solver_main.c for function initializeSolverData
* defines the minimal step size
*/
static
const double MINIMAL_STEP_SIZE = 1e-12;
static const double MINIMAL_STEP_SIZE = 1e-12;

/*
* used in model_help.c for function setZCtol
* defines a threshold for relation hysteresis,
* in multiplied by minimum(tolerance, step-size)
*
*/
static
const double TOL_HYSTERESIS_ZEROCROSSINGS = 1e-4;
static const double TOL_HYSTERESIS_ZEROCROSSINGS = 1e-4;

#endif
10 changes: 6 additions & 4 deletions OMCompiler/SimulationRuntime/c/simulation/solver/solver_main.c
Expand Up @@ -699,10 +699,12 @@ int solver_main(DATA* data, threadData_t *threadData, const char* init_initMetho
*/
simInfo->useStopTime = 1;

/* if the given step size is too small redefine it */
if ((simInfo->stepSize < MINIMAL_STEP_SIZE) && (simInfo->stopTime > 0)){
warningStreamPrint(LOG_STDOUT, 0, "The step-size %g is too small. Adjust the step-size to %g.", simInfo->stepSize, MINIMAL_STEP_SIZE);
simInfo->stepSize = MINIMAL_STEP_SIZE;
/* Use minStepSize if stepSize is getting too small, but
* allow stepSize to be zero if startTime == stopTime.
*/
if ((simInfo->stepSize < simInfo->minStepSize) && (simInfo->stopTime > simInfo->startTime)){
warningStreamPrint(LOG_STDOUT, 0, "The step-size %g is too small. Adjust the step-size to %g.", simInfo->stepSize, simInfo->minStepSize);
simInfo->stepSize = simInfo->minStepSize;
simInfo->numSteps = round((simInfo->stopTime - simInfo->startTime)/simInfo->stepSize);
}
#if !defined(OMC_EMCC)
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/SimulationRuntime/c/simulation_data.h
Expand Up @@ -578,6 +578,7 @@ typedef struct SIMULATION_INFO
int useStopTime;
modelica_integer numSteps;
modelica_real stepSize;
modelica_real minStepSize; /* defines the minimal step size */
modelica_real tolerance;
const char *solverMethod;
const char *outputFormat;
Expand Down
Expand Up @@ -2,7 +2,7 @@
// keywords: xml, init, parameters, changeable value
// status: correct
// teardown_command: rm -f testParameters*
//
//

loadString("
model testParameters
Expand Down Expand Up @@ -86,81 +86,81 @@ readFile("testParameters_init.tmp.xml");
// <ModelVariables>
//
// <ScalarVariable
// name = \"x\"
// valueReference = \"1000\"
// variability = \"continuous\" isDiscrete = \"false\"
// causality = \"internal\" isValueChangeable = \"true\"
// alias = \"noAlias\"
// classIndex = \"0\" classType = \"rSta\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"8\" startColumn = \"3\" endLine = \"8\" endColumn = \"9\" fileWritable = \"true\">
// <Real fixed=\"false\" useNominal=\"false\" />
// name = \"x\"
// valueReference = \"1000\"
// variability = \"continuous\" isDiscrete = \"false\"
// causality = \"internal\" isValueChangeable = \"true\"
// alias = \"noAlias\"
// classIndex = \"0\" classType = \"rSta\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"8\" startColumn = \"3\" endLine = \"8\" endColumn = \"9\" fileWritable = \"true\">
// <Real fixed=\"false\" useNominal=\"false\" />
// </ScalarVariable>
// <ScalarVariable
// name = \"der(x)\"
// valueReference = \"1001\"
// variability = \"continuous\" isDiscrete = \"false\"
// causality = \"internal\" isValueChangeable = \"false\"
// alias = \"noAlias\"
// classIndex = \"0\" classType = \"rDer\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"8\" startColumn = \"3\" endLine = \"8\" endColumn = \"9\" fileWritable = \"true\">
// <Real fixed=\"false\" useNominal=\"false\" />
// name = \"der(x)\"
// valueReference = \"1001\"
// variability = \"continuous\" isDiscrete = \"false\"
// causality = \"internal\" isValueChangeable = \"false\"
// alias = \"noAlias\"
// classIndex = \"0\" classType = \"rDer\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"8\" startColumn = \"3\" endLine = \"8\" endColumn = \"9\" fileWritable = \"true\">
// <Real fixed=\"false\" useNominal=\"false\" />
// </ScalarVariable>
// <ScalarVariable
// name = \"a\"
// valueReference = \"1002\"
// variability = \"parameter\" isDiscrete = \"true\"
// causality = \"internal\" isValueChangeable = \"true\"
// alias = \"noAlias\"
// classIndex = \"0\" classType = \"rPar\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"4\" startColumn = \"3\" endLine = \"4\" endColumn = \"31\" fileWritable = \"true\">
// <Real start=\"0.9356160015533859\" fixed=\"true\" useNominal=\"false\" />
// name = \"a\"
// valueReference = \"1002\"
// variability = \"parameter\" isDiscrete = \"true\"
// causality = \"internal\" isValueChangeable = \"true\"
// alias = \"noAlias\"
// classIndex = \"0\" classType = \"rPar\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"4\" startColumn = \"3\" endLine = \"4\" endColumn = \"31\" fileWritable = \"true\">
// <Real start=\"0.9356160015533859\" fixed=\"true\" useNominal=\"false\" />
// </ScalarVariable>
// <ScalarVariable
// name = \"a0\"
// valueReference = \"1003\"
// variability = \"parameter\" isDiscrete = \"true\"
// causality = \"internal\" isValueChangeable = \"true\"
// alias = \"noAlias\"
// classIndex = \"1\" classType = \"rPar\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"3\" startColumn = \"3\" endLine = \"3\" endColumn = \"20\" fileWritable = \"true\">
// <Real fixed=\"true\" useNominal=\"false\" />
// name = \"a0\"
// valueReference = \"1003\"
// variability = \"parameter\" isDiscrete = \"true\"
// causality = \"internal\" isValueChangeable = \"true\"
// alias = \"noAlias\"
// classIndex = \"1\" classType = \"rPar\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"3\" startColumn = \"3\" endLine = \"3\" endColumn = \"20\" fileWritable = \"true\">
// <Real fixed=\"true\" useNominal=\"false\" />
// </ScalarVariable>
// <ScalarVariable
// name = \"b\"
// valueReference = \"1004\"
// variability = \"parameter\" isDiscrete = \"true\"
// causality = \"internal\" isValueChangeable = \"false\"
// alias = \"noAlias\"
// classIndex = \"2\" classType = \"rPar\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"5\" startColumn = \"3\" endLine = \"5\" endColumn = \"30\" fileWritable = \"true\">
// <Real fixed=\"true\" useNominal=\"false\" />
// name = \"b\"
// valueReference = \"1004\"
// variability = \"parameter\" isDiscrete = \"true\"
// causality = \"internal\" isValueChangeable = \"false\"
// alias = \"noAlias\"
// classIndex = \"2\" classType = \"rPar\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"5\" startColumn = \"3\" endLine = \"5\" endColumn = \"30\" fileWritable = \"true\">
// <Real fixed=\"true\" useNominal=\"false\" />
// </ScalarVariable>
// <ScalarVariable
// name = \"c\"
// valueReference = \"1005\"
// variability = \"parameter\" isDiscrete = \"true\"
// causality = \"internal\" isValueChangeable = \"false\"
// alias = \"noAlias\"
// classIndex = \"3\" classType = \"rPar\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"6\" startColumn = \"3\" endLine = \"6\" endColumn = \"29\" fileWritable = \"true\">
// <Real start=\"3.0\" fixed=\"true\" useNominal=\"false\" />
// name = \"c\"
// valueReference = \"1005\"
// variability = \"parameter\" isDiscrete = \"true\"
// causality = \"internal\" isValueChangeable = \"false\"
// alias = \"noAlias\"
// classIndex = \"3\" classType = \"rPar\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"6\" startColumn = \"3\" endLine = \"6\" endColumn = \"29\" fileWritable = \"true\">
// <Real start=\"3.0\" fixed=\"true\" useNominal=\"false\" />
// </ScalarVariable>
// <ScalarVariable
// name = \"d\"
// valueReference = \"1006\"
// variability = \"parameter\" isDiscrete = \"true\"
// causality = \"internal\" isValueChangeable = \"false\"
// alias = \"noAlias\"
// classIndex = \"4\" classType = \"rPar\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"7\" startColumn = \"3\" endLine = \"7\" endColumn = \"32\" fileWritable = \"true\">
// <Real fixed=\"false\" useNominal=\"false\" />
// name = \"d\"
// valueReference = \"1006\"
// variability = \"parameter\" isDiscrete = \"true\"
// causality = \"internal\" isValueChangeable = \"false\"
// alias = \"noAlias\"
// classIndex = \"4\" classType = \"rPar\"
// isProtected = \"false\" hideResult = \"false\"
// fileName = \"&lt;interactive&gt;\" startLine = \"7\" startColumn = \"3\" endLine = \"7\" endColumn = \"32\" fileWritable = \"true\">
// <Real fixed=\"false\" useNominal=\"false\" />
// </ScalarVariable>
//
//
Expand Down
Expand Up @@ -8,7 +8,7 @@

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

modelTestingType := OpenModelicaModelTesting.Kind.SimpleSimulation;
modelTestingType := OpenModelicaModelTesting.Kind.VerifiedSimulation;
modelName := $TypeName(Modelica.Electrical.Spice3.Examples.Inverter);
compareVars :=
{
Expand All @@ -24,15 +24,15 @@ runScript(modelTesting);getErrorString();
// "true
// "
// ""
// OpenModelicaModelTesting.Kind.SimpleSimulation
// OpenModelicaModelTesting.Kind.VerifiedSimulation
// Modelica.Electrical.Spice3.Examples.Inverter
// {"mp.Dinternal","mp.Sinternal","mn.Dinternal","mn.Sinternal"}
// Simulation options: startTime = 0.0, stopTime = 9.999999999999999e-12, numberOfIntervals = 1999, tolerance = 1e-07, method = 'dassl', fileNamePrefix = 'Modelica.Electrical.Spice3.Examples.Inverter', options = '', outputFormat = 'mat', variableFilter = 'time|mp.Dinternal|mp.Sinternal|mn.Dinternal|mn.Sinternal', cflags = '', simflags = ' -abortSlowSimulation -alarm=360 -emit_protected'
// Result file: Modelica.Electrical.Spice3.Examples.Inverter_res.mat
// Messages: stdout | warning | The step-size 5.0025e-15 is too small. Adjust the step-size to 1e-12.
// LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// Messages: LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
//
// Files Equal!
// "true
// "
// ""
Expand Down
3 changes: 1 addition & 2 deletions testsuite/simulation/modelica/functions_eval/Inverter2.mos
Expand Up @@ -23,8 +23,7 @@ getErrorString();
// record SimulationResult
// resultFile = "Inverter2_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 9.999999999999999e-12, numberOfIntervals = 2000, tolerance = 1e-07, method = 'dassl', fileNamePrefix = 'Inverter2', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "stdout | warning | The step-size 5e-15 is too small. Adjust the step-size to 1e-12.
// LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
// end SimulationResult;
Expand Down

0 comments on commit 4e286fa

Please sign in to comment.