Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 71c219d

Browse files
Willi BraunOpenModelica-Hudson
authored andcommitted
[DAEmode] several minor improvments and fixes for events
- fixes the when equations generation - fixes the event handling at startTime - minor improvments Belonging to [master]: - #2259 - OpenModelica/OpenModelica-testsuite#871
1 parent fbf28bf commit 71c219d

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

Compiler/SimCode/SimCodeMain.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ algorithm
11611161
daeVars := BackendVariable.listVar(varsLst);
11621162
daeEqns := BackendEquation.listEquation(eqnsLst);
11631163
// create SimCode residual equation
1164-
(daeEquations, uniqueEqIndex, tempVars) := SimCodeUtil.createEquationsfromList(listReverse(eqnsLst), varsLst, false, uniqueEqIndex, tempVars, inBackendDAE.shared.info);
1164+
(daeEquations, uniqueEqIndex, tempVars) := SimCodeUtil.createEquationsfromList(listReverse(eqnsLst), varsLst, uniqueEqIndex, tempVars, inBackendDAE.shared.info, true, true);
11651165

11661166
(uniqueEqIndex, removedEquations) := BackendEquation.traverseEquationArray(BackendDAEUtil.collapseRemovedEqs(inBackendDAE), SimCodeUtil.traversedlowEqToSimEqSystem, (uniqueEqIndex, {}));
11671167
daeEquations := listAppend(daeEquations, removedEquations);

Compiler/SimCode/SimCodeUtil.mo

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5604,7 +5604,7 @@ algorithm
56045604
case (BackendDAE.IF_EQUATION(conditions=conditions, eqnstrue=eqnsLst, eqnsfalse=elseqns, source=source_), _,
56055605
BackendDAE.SHARED(info = ei), _, _, _) equation
56065606
(ifbranches, uniqueEqIndex, tempvars) = createEquationsIfBranch(conditions, eqnsLst, inVars, shared, genDiscrete, iuniqueEqIndex, itempvars);
5607-
(equations_, uniqueEqIndex, tempvars) = createEquationsfromList(elseqns, inVars, genDiscrete, uniqueEqIndex, tempvars, ei);
5607+
(equations_, uniqueEqIndex, tempvars) = createEquationsfromList(elseqns, inVars, uniqueEqIndex, tempvars, ei, genDiscrete);
56085608
then ({SimCode.SES_IFEQUATION(uniqueEqIndex, ifbranches, equations_, source_)}, uniqueEqIndex+1, tempvars);
56095609

56105610
else equation
@@ -5643,7 +5643,7 @@ algorithm
56435643

56445644
case (condition::conditionList, eqns::eqnsLst, _,
56455645
BackendDAE.SHARED(info = ei), _, _, _) equation
5646-
(equations_, uniqueEqIndex, tempvars) = createEquationsfromList(eqns, inVars, genDiscrete, iuniqueEqIndex, itempvars, ei);
5646+
(equations_, uniqueEqIndex, tempvars) = createEquationsfromList(eqns, inVars, iuniqueEqIndex, itempvars, ei, genDiscrete);
56475647
ifbranch = ((condition, equations_));
56485648
(ifbranches, uniqueEqIndex, tempvars) = createEquationsIfBranch(conditionList, eqnsLst, inVars, shared, genDiscrete, uniqueEqIndex, tempvars);
56495649
ifbranches = ifbranch::ifbranches;
@@ -5658,10 +5658,13 @@ end createEquationsIfBranch;
56585658
public function createEquationsfromList
56595659
input list<BackendDAE.Equation> inEquations;
56605660
input list<BackendDAE.Var> inVars;
5661-
input Boolean genDiscrete;
56625661
input Integer iuniqueEqIndex;
56635662
input list<SimCodeVar.SimVar> itempvars;
56645663
input BackendDAE.ExtraInfo iextra;
5664+
input Boolean genDiscrete = false;
5665+
input Boolean includeWhen = false;
5666+
input Boolean skipDiscInZc = false;
5667+
input Boolean skipDiscInAlgorithm = false;
56655668
output list<SimCode.SimEqSystem> equations_;
56665669
output Integer ouniqueEqIndex;
56675670
output list<SimCodeVar.SimVar> otempvars;
@@ -5689,7 +5692,7 @@ algorithm
56895692
(BackendDAE.DAE({syst as BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(comps=comps))}, shared)) =
56905693
BackendDAEUtil.transformBackendDAE( subsystem_dae, SOME((BackendDAE.NO_INDEX_REDUCTION(),
56915694
BackendDAE.ALLOW_UNDERCONSTRAINED())), NONE(), NONE() );
5692-
(equations_, _, uniqueEqIndex, tempvars) = createEquations(false, false, genDiscrete, false, syst, shared, comps, iuniqueEqIndex, itempvars);
5695+
(equations_, _, uniqueEqIndex, tempvars) = createEquations(includeWhen, skipDiscInZc, genDiscrete, skipDiscInAlgorithm, syst, shared, comps, iuniqueEqIndex, itempvars);
56935696
then (equations_, uniqueEqIndex, tempvars);
56945697

56955698
else equation
@@ -5963,7 +5966,7 @@ algorithm
59635966
eqnLst = List.threadMap2(expl, expl1, BackendEquation.generateEquation, source, eqKind);
59645967

59655968
// generate SimCode equations therefore
5966-
(eqSystlst, uniqueEqIndex, tempvars) = createEquationsfromList(eqnLst, inVars, genDiscrete, iuniqueEqIndex, itempvars, iextra);
5969+
(eqSystlst, uniqueEqIndex, tempvars) = createEquationsfromList(eqnLst, inVars, iuniqueEqIndex, itempvars, iextra, genDiscrete);
59675970
then
59685971
(eqSystlst, uniqueEqIndex, tempvars);
59695972

@@ -5983,7 +5986,7 @@ algorithm
59835986
eqnLst = List.threadMap2(expl, expl1, BackendEquation.generateEquation, source, eqKind);
59845987

59855988
// generate SimCode equations therefore
5986-
(eqSystlst, uniqueEqIndex,_) = createEquationsfromList(eqnLst, inVars, genDiscrete, iuniqueEqIndex, itempvars, iextra);
5989+
(eqSystlst, uniqueEqIndex,_) = createEquationsfromList(eqnLst, inVars, iuniqueEqIndex, itempvars, iextra, genDiscrete);
59875990
then
59885991
(eqSystlst, uniqueEqIndex, itempvars);
59895992

Compiler/Template/CodegenC.tpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,6 @@ template simulationFile_dae(SimCode simCode)
897897
<<
898898
/* DAE residuals */
899899
<%simulationFileHeader(fileNamePrefix)%>
900-
#include "<%fileNamePrefix%>_16dae.h"
901900

902901
#ifdef __cplusplus
903902
extern "C" {

SimulationRuntime/c/simulation/solver/ida_solver.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,13 @@ ida_event_update(DATA* data, threadData_t *threadData)
723723

724724
/* get initial step to provide a direction of the solution */
725725
IDAGetActualInitStep(idaData->ida_mem, &init_h);
726+
/* provide a feasible step-size if it's too small */
727+
if (init_h < DBL_EPSILON){
728+
init_h = DBL_EPSILON;
729+
IDASetInitStep(idaData->ida_mem, init_h);
730+
infoStreamPrint(LOG_SOLVER, 0, "##IDA## corrected step-size at %.15g", init_h);
731+
}
732+
726733
/* increase limits of the non-linear solver */
727734
IDASetMaxNumStepsIC(idaData->ida_mem, 2*data->modelData->nStates*10);
728735
IDASetMaxNumJacsIC(idaData->ida_mem, 2*data->modelData->nStates*10);
@@ -738,7 +745,7 @@ ida_event_update(DATA* data, threadData_t *threadData)
738745
if (checkIDAflag(flag)){
739746
infoStreamPrint(LOG_SOLVER, 0, "##IDA## first event iteration failed. Start next try without line search!");
740747
IDASetLineSearchOffIC(idaData->ida_mem, TRUE);
741-
flag = IDACalcIC(idaData->ida_mem, IDA_YA_YDP_INIT, data->localData[0]->timeValue+1);
748+
flag = IDACalcIC(idaData->ida_mem, IDA_YA_YDP_INIT, data->localData[0]->timeValue+data->simulationInfo->tolerance);
742749
IDAGetNumNonlinSolvIters(idaData->ida_mem, &nonLinIters);
743750
infoStreamPrint(LOG_SOLVER, 0, "##IDA## IDACalcIC run status %d.\nIterations : %ld\n", flag, nonLinIters);
744751
if (checkIDAflag(flag)){
@@ -757,6 +764,9 @@ ida_event_update(DATA* data, threadData_t *threadData)
757764
// and also algebraic vars
758765
data->simulationInfo->daeModeData->setAlgebraicDAEVars(data, threadData, idaData->states + data->modelData->nStates);
759766
memcpy(data->localData[0]->realVars + data->modelData->nStates, idaData->statesDer, sizeof(double)*data->modelData->nStates);
767+
768+
/* reset initial step size again to default */
769+
IDASetInitStep(idaData->ida_mem, 0.0);
760770
}
761771
}
762772
else{
@@ -890,7 +900,7 @@ ida_solver_step(DATA* data, threadData_t *threadData, SOLVER_INFO* solverInfo)
890900
/* Calculate steps until TOUT is reached */
891901
if (idaData->internalSteps)
892902
{
893-
/* If dasslsteps is selected, the dassl run to stopTime or next sample event */
903+
/* If internalSteps are selected, let IDA run to stopTime or next sample event */
894904
if (data->simulationInfo->nextSampleEvent < data->simulationInfo->stopTime)
895905
{
896906
tout = data->simulationInfo->nextSampleEvent;

0 commit comments

Comments
 (0)