Skip to content

Commit cde23af

Browse files
rfrankeOpenModelica-Hudson
authored andcommitted
Use start values at first clock tick (ticket:3770)
This shall only apply to states of solved continuous equations. We check for the existence of a solver method so far.
1 parent 9e139b9 commit cde23af

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

Compiler/BackEnd/BackendDAE.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ uniontype VarKind "variable kind"
246246
record DUMMY_STATE end DUMMY_STATE;
247247
record CLOCKED_STATE
248248
.DAE.ComponentRef previousName "the name of the previous variable";
249+
Boolean isStartFixed "is fixed at first clock tick";
249250
end CLOCKED_STATE;
250251
record DISCRETE end DISCRETE;
251252
record PARAM end PARAM;

Compiler/BackEnd/SynchronousFeatures.mo

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,9 @@ algorithm
290290
for i in 1:arrayLength(isPrevVarArr) loop
291291
if isPrevVarArr[i] then
292292
var := BackendVariable.setVarFixed(BackendVariable.getVarAt(inSyst.orderedVars, i), true);
293-
var := BackendVariable.setVarKind(var, BackendDAE.CLOCKED_STATE(previousName = ComponentReference.crefPrefixPrevious(var.varName)));
293+
var := BackendVariable.setVarKind(var, BackendDAE.CLOCKED_STATE(
294+
previousName = ComponentReference.crefPrefixPrevious(var.varName),
295+
isStartFixed = isSome(subPartition.clock.solver)));
294296
BackendVariable.setVarAt(inSyst.orderedVars, i, var);
295297
prevVars := var.varName::prevVars;
296298
end if;

Compiler/Template/CodegenCpp.tpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9815,6 +9815,7 @@ case SIMCODE(modelInfo = MODELINFO(__), modelStructure = fmims) then
98159815
_clockInterval[<%i%>] = <%interval%> * <%fnom%>.0 / <%fres%>.0;
98169816
_clockShift[<%i%>] = <%snom%>.0 / <%sres%>.0;
98179817
_clockTime[<%i%>] = _simTime + _clockShift[<%i%>] * _clockInterval[<%i%>];
9818+
_clockStart[<%i%>] = true;
98189819
<%i%> ++;
98199820
>>
98209821
; separator="\n")
@@ -11564,6 +11565,9 @@ template equationSimpleAssign(SimEqSystem eq, Context context,Text &varDecls, Si
1156411565
match eq
1156511566
case SES_SIMPLE_ASSIGN(__) then
1156611567
let &preExp = buffer "" /*BUFD*/
11568+
let startFixedExp = match cref2simvar(cref, simCode)
11569+
case SIMVAR(varKind = CLOCKED_STATE(isStartFixed = true)) then
11570+
"if (_clockStart[clockIndex - 1]) return;"
1156711571
let expPart = daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
1156811572

1156911573
match cref
@@ -11585,6 +11589,7 @@ case SES_SIMPLE_ASSIGN(__) then
1158511589
let &assignExp += cref1(cref, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, context, varDecls, stateDerVectorName, useFlatArrayNotation)
1158611590
let &assignExp += if(assignToStartValues) then ',<%expPart%>,<%overwriteOldStartValue%>);' else ' = <%expPart%>;'
1158711591
<<
11592+
<%if not assignToStartValues then '<%startFixedExp%>'%>
1158811593
<%preExp%>
1158911594
<%assignExp%>
1159011595
>>
@@ -12759,6 +12764,9 @@ template clockedPartFunctions(Integer i, list<tuple<SimCodeVar.SimVar, Boolean>>
1275912764

1276012765
void <%className%>::<%funcName%>(const UPDATETYPE command)
1276112766
{
12767+
if (_simTime > _clockTime[<%idx%>]) {
12768+
_clockStart[<%idx%>] = false;
12769+
}
1276212770
<%funcCalls%>
1276312771
if (_simTime > _clockTime[<%idx%>]) {
1276412772
_clockInterval[<%idx%>] = _simTime - _clockTime[<%idx%>];

Compiler/Template/SimCodeTV.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,7 @@ package BackendDAE
10711071
record DUMMY_STATE end DUMMY_STATE;
10721072
record CLOCKED_STATE
10731073
DAE.ComponentRef previousName "the name of the previous variable";
1074+
Boolean isStartFixed "is fixed at first clock tick";
10741075
end CLOCKED_STATE;
10751076
record DISCRETE end DISCRETE;
10761077
record PARAM end PARAM;

SimulationRuntime/cpp/Core/System/SystemDefaultImplementation.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ SystemDefaultImplementation::SystemDefaultImplementation(IGlobalSettings *global
5555
, _clockInterval (NULL)
5656
, _clockShift (NULL)
5757
, _clockTime (NULL)
58-
, _clockCondition(NULL)
58+
, _clockCondition (NULL)
59+
, _clockStart (NULL)
5960
, _outputStream(NULL)
6061
, _callType (IContinuous::UNDEF_UPDATE)
6162
, _initial (false)
@@ -91,7 +92,8 @@ SystemDefaultImplementation::SystemDefaultImplementation(SystemDefaultImplementa
9192
, _clockInterval (NULL)
9293
, _clockShift (NULL)
9394
, _clockTime (NULL)
94-
, _clockCondition(NULL)
95+
, _clockCondition (NULL)
96+
, _clockStart (NULL)
9597
, _outputStream(NULL)
9698
, _callType (IContinuous::UNDEF_UPDATE)
9799
, _initial (false)
@@ -134,7 +136,8 @@ SystemDefaultImplementation::~SystemDefaultImplementation()
134136
if(_clockInterval) delete [] _clockInterval;
135137
if(_clockShift) delete [] _clockShift;
136138
if(_clockTime) delete [] _clockTime;
137-
if(_clockCondition) delete [] _clockCondition;
139+
if(_clockCondition) delete [] _clockCondition;
140+
if(_clockStart) delete [] _clockStart;
138141
}
139142

140143
void SystemDefaultImplementation::Assert(bool cond,const string& msg)
@@ -237,18 +240,17 @@ void SystemDefaultImplementation::initialize()
237240
_clockShift = new double [_dimClock];
238241
if (_clockTime) delete [] _clockTime;
239242
_clockTime = new double [_dimClock];
240-
if (_clockCondition) delete [] _clockCondition;
243+
if (_clockCondition) delete [] _clockCondition;
241244
_clockCondition = new bool [_dimClock];
242-
memset(_clockCondition,false,(_dimClock)*sizeof(bool));
245+
memset(_clockCondition,false,(_dimClock)*sizeof(bool));
246+
if (_clockStart) delete [] _clockStart;
247+
_clockStart = new bool [_dimClock];
243248
}
244249
_start_time = 0.0;
245250
_terminal = false;
246251
_terminate = false;
247-
_clockStart = true;
248-
249252
};
250253

251-
252254
/// Set current integration time
253255
void SystemDefaultImplementation::setTime(const double& t)
254256
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation
181181
double *_clockInterval; ///< time interval between clock ticks
182182
double *_clockShift; ///< time before first activation
183183
double *_clockTime; ///< time of clock ticks
184-
bool* _clockCondition; ///< clock tick active
185-
bool _clockStart; ///< only active at clock start
184+
bool *_clockCondition; ///< clock tick active
185+
bool *_clockStart; ///< only active at clock start
186186
std::ostream *_outputStream; ///< Output stream for results
187187

188188
IContinuous::UPDATETYPE _callType;

0 commit comments

Comments
 (0)