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

Commit 77407e8

Browse files
Willi BraunOpenModelica-Hudson
authored andcommitted
[DAEmode] Avoid IDA for models without states
- when the model has no states and no residual equations the general simulation algorithm with event handling is used. - This fixes models like Modelica.Blocks.Examples.RealNetwork1 for the daeMode=new. Belonging to [master]: - #2295
1 parent 1769e70 commit 77407e8

File tree

7 files changed

+124
-12
lines changed

7 files changed

+124
-12
lines changed

SimulationRuntime/c/Makefile.common

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ RUNTIMESIMSOLVER_HEADERS = ./simulation/solver/delay.h \
7575
./simulation/solver/events.h \
7676
./simulation/solver/synchronous.h \
7777
./simulation/solver/external_input.h\
78-
./simulation/solver/solver_main.h
78+
./simulation/solver/solver_main.h \
79+
./simulation/solver/dae_mode.h
7980

8081
RUNTIMEMETA_HEADERS = ./meta/meta_modelica_builtin_boxptr.h \
8182
./meta/meta_modelica_builtin_boxvar.h \

SimulationRuntime/c/Makefile.objs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ else
6767
SOLVER_OBJS_MINIMAL=$(SOLVER_OBJS_FMU)
6868
endif
6969
ifeq ($(OMC_MINIMAL_RUNTIME),)
70-
SOLVER_OBJS=$(SOLVER_OBJS_MINIMAL) kinsolSolver$(OBJ_EXT) linearSolverKlu$(OBJ_EXT) linearSolverLis$(OBJ_EXT) linearSolverUmfpack$(OBJ_EXT) dassl$(OBJ_EXT) radau$(OBJ_EXT) sym_solver_ssc$(OBJ_EXT) nonlinearSolverNewton$(OBJ_EXT) newtonIteration$(OBJ_EXT) ida_solver$(OBJ_EXT) irksco$(OBJ_EXT)
70+
SOLVER_OBJS=$(SOLVER_OBJS_MINIMAL) kinsolSolver$(OBJ_EXT) linearSolverKlu$(OBJ_EXT) linearSolverLis$(OBJ_EXT) linearSolverUmfpack$(OBJ_EXT) dassl$(OBJ_EXT) radau$(OBJ_EXT) sym_solver_ssc$(OBJ_EXT) nonlinearSolverNewton$(OBJ_EXT) newtonIteration$(OBJ_EXT) ida_solver$(OBJ_EXT) irksco$(OBJ_EXT) dae_mode$(OBJ_EXT)
7171
else
7272
SOLVER_OBJS=$(SOLVER_OBJS_MINIMAL)
7373
endif
74-
SOLVER_HFILES = dassl.h delay.h epsilon.h events.h external_input.h fmi_events.h ida_solver.h linearSystem.h mixedSystem.h model_help.h nonlinearSystem.h nonlinearValuesList.h radau.h sym_solver_ssc.h solver_main.h stateset.h
74+
SOLVER_HFILES = dassl.h dae_mode.h delay.h epsilon.h events.h external_input.h fmi_events.h ida_solver.h linearSystem.h mixedSystem.h model_help.h nonlinearSystem.h nonlinearValuesList.h radau.h sym_solver_ssc.h solver_main.h stateset.h
7575

7676
INITIALIZATION_OBJS = initialization$(OBJ_EXT)
7777
INITIALIZATION_HFILES = initialization.h

SimulationRuntime/c/simulation/simulation_runtime.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#include "util/rtclock.h"
8585
#include "omc_config.h"
8686
#include "simulation/solver/initialization/initialization.h"
87+
#include "simulation/solver/dae_mode.h"
8788

8889
#ifdef _OMC_QSS_LIB
8990
#include "solver_qss/solver_qss.h"
@@ -644,8 +645,19 @@ static int callSolver(DATA* simData, threadData_t *threadData, string init_initM
644645
/* if no states are present, then we can
645646
* use euler method, since it does nothing.
646647
*/
647-
if (simData->modelData->nStates < 1 && solverID != S_OPTIMIZATION && solverID != S_SYM_SOLVER && !compiledInDAEMode) {
648+
if ( (simData->modelData->nStates < 1 &&
649+
solverID != S_OPTIMIZATION &&
650+
solverID != S_SYM_SOLVER) ||
651+
(compiledInDAEMode && (simData->simulationInfo->daeModeData->nResidualVars +
652+
simData->simulationInfo->daeModeData->nAlgebraicDAEVars < 1))
653+
)
654+
{
648655
solverID = S_EULER;
656+
if (compiledInDAEMode == 3)
657+
{
658+
simData->callback->functionDAE = evaluateDAEResiduals_wrapperEventUpdate;
659+
simData->callback->function_ZeroCrossingsEquations = simData->simulationInfo->daeModeData->evaluateDAEResiduals;
660+
}
649661
}
650662

651663
if(S_UNKNOWN == solverID) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-2018, Open Source Modelica Consortium (OSMC),
5+
* c/o Linköpings universitet, Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
7+
*
8+
* All rights reserved.
9+
*
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
11+
* GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12+
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13+
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14+
* ACCORDING TO RECIPIENTS CHOICE.
15+
*
16+
* The OpenModelica software and the OSMC (Open Source Modelica Consortium)
17+
* Public License (OSMC-PL) are obtained from OSMC, either from the above
18+
* address, from the URLs: http://www.openmodelica.org or
19+
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
20+
* distribution. GNU version 3 is obtained from:
21+
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
22+
* http://www.opensource.org/licenses/BSD-3-Clause.
23+
*
24+
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
25+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
26+
* EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
27+
* CONDITIONS OF OSMC-PL.
28+
*
29+
*/
30+
31+
#include "dae_mode.h"
32+
33+
#ifdef __cplusplus
34+
extern "C" {
35+
#endif
36+
37+
/*! \fn void evaluateDAEResiduals_wrapperEventUpdate
38+
*
39+
* wrapper function of the main evaluation function for DAE mode
40+
*/
41+
int evaluateDAEResiduals_wrapperEventUpdate(DATA* data, threadData_t* threadData)
42+
{
43+
int retVal;
44+
45+
data->simulationInfo->discreteCall = 1;
46+
retVal = data->simulationInfo->daeModeData->evaluateDAEResiduals(data, threadData);
47+
data->simulationInfo->discreteCall = 0;
48+
49+
return retVal;
50+
}
51+
52+
#ifdef __cplusplus
53+
}
54+
#endif
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-2018, Open Source Modelica Consortium (OSMC),
5+
* c/o Linköpings universitet, Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
7+
*
8+
* All rights reserved.
9+
*
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
11+
* GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12+
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13+
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14+
* ACCORDING TO RECIPIENTS CHOICE.
15+
*
16+
* The OpenModelica software and the OSMC (Open Source Modelica Consortium)
17+
* Public License (OSMC-PL) are obtained from OSMC, either from the above
18+
* address, from the URLs: http://www.openmodelica.org or
19+
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
20+
* distribution. GNU version 3 is obtained from:
21+
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
22+
* http://www.opensource.org/licenses/BSD-3-Clause.
23+
*
24+
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
25+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
26+
* EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
27+
* CONDITIONS OF OSMC-PL.
28+
*
29+
*/
30+
31+
#ifndef DAE_MODE_H
32+
#define DAE_MODE_H
33+
34+
#include "simulation_data.h"
35+
36+
#ifdef __cplusplus
37+
extern "C" {
38+
#endif
39+
40+
int evaluateDAEResiduals_wrapperEventUpdate(DATA* data, threadData_t* threadData);
41+
42+
#ifdef __cplusplus
43+
}
44+
#endif
45+
46+
#endif

SimulationRuntime/c/simulation/solver/ida_solver.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "simulation/solver/omc_math.h"
5353
#include "simulation/solver/ida_solver.h"
5454
#include "simulation/solver/dassl.h"
55+
#include "simulation/solver/dae_mode.h"
5556

5657
#ifdef WITH_SUNDIALS
5758

@@ -703,17 +704,14 @@ ida_event_update(DATA* data, threadData_t *threadData)
703704
if (initializedSolver){
704705

705706
data->simulationInfo->needToIterate = 0;
706-
/* get new values from data -> TODO: update all discrete */
707-
data->simulationInfo->discreteCall = 1;
708707

709708
memcpy(idaData->states, data->localData[0]->realVars, sizeof(double)*data->modelData->nStates);
710709
data->simulationInfo->daeModeData->getAlgebraicDAEVars(data, threadData, idaData->states + data->modelData->nStates);
711710
memcpy(idaData->statesDer, data->localData[0]->realVars + data->modelData->nStates, sizeof(double)*data->modelData->nStates);
712711

713712
/* update inner algebraic get new values from data */
714-
idaData->residualFunction(data->localData[0]->timeValue, idaData->y, idaData->yp, idaData->newdelta, idaData);
713+
evaluateDAEResiduals_wrapperEventUpdate(data, threadData);
715714
data->simulationInfo->daeModeData->getAlgebraicDAEVars(data, threadData, idaData->states + data->modelData->nStates);
716-
data->simulationInfo->discreteCall = 0;
717715

718716
infoStreamPrint(LOG_SOLVER, 0, "##IDA## do event update at %.15g", data->localData[0]->timeValue);
719717
flag = IDAReInit(idaData->ida_mem,
@@ -756,9 +754,7 @@ ida_event_update(DATA* data, threadData_t *threadData)
756754
IDAGetConsistentIC(idaData->ida_mem, idaData->y, idaData->yp);
757755

758756
/* update inner algebraic variables */
759-
data->simulationInfo->discreteCall = 1;
760-
idaData->residualFunction(data->localData[0]->timeValue, idaData->y, idaData->yp, idaData->newdelta, idaData);
761-
data->simulationInfo->discreteCall = 0;
757+
evaluateDAEResiduals_wrapperEventUpdate(data, threadData);
762758

763759
memcpy(data->localData[0]->realVars, idaData->states, sizeof(double)*data->modelData->nStates);
764760
// and also algebraic vars
@@ -1034,7 +1030,6 @@ ida_solver_step(DATA* data, threadData_t *threadData, SOLVER_INFO* solverInfo)
10341030
data->simulationInfo->daeModeData->setAlgebraicDAEVars(data, threadData, idaData->states + data->modelData->nStates);
10351031
memcpy(data->localData[0]->realVars + data->modelData->nStates, idaData->statesDer, sizeof(double)*data->modelData->nStates);
10361032
sData->timeValue = solverInfo->currentTime;
1037-
idaData->residualFunction(sData->timeValue, idaData->y , idaData->yp, idaData->newdelta, idaData);
10381033
}
10391034

10401035
/* sensitivity mode */

SimulationRuntime/c/simulation/solver/perform_simulation.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ static void prefixedName_updateContinuousSystem(DATA *data, threadData_t *thread
7676
{
7777
data->callback->functionAlgebraics(data, threadData);
7878
}
79+
else if (compiledInDAEMode == 3)
80+
{
81+
data->simulationInfo->daeModeData->evaluateDAEResiduals(data, threadData);
82+
}
7983
}
8084
else /* ode mode */
8185
{

0 commit comments

Comments
 (0)