Skip to content

Commit

Permalink
Use start values at first clock tick (ticket:3770)
Browse files Browse the repository at this point in the history
This shall only apply to states of solved continuous equations.
We check for the existence of a solver method so far.
  • Loading branch information
rfranke authored and OpenModelica-Hudson committed May 28, 2016
1 parent 9e139b9 commit cde23af
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -246,6 +246,7 @@ uniontype VarKind "variable kind"
record DUMMY_STATE end DUMMY_STATE;
record CLOCKED_STATE
.DAE.ComponentRef previousName "the name of the previous variable";
Boolean isStartFixed "is fixed at first clock tick";
end CLOCKED_STATE;
record DISCRETE end DISCRETE;
record PARAM end PARAM;
Expand Down
4 changes: 3 additions & 1 deletion Compiler/BackEnd/SynchronousFeatures.mo
Expand Up @@ -290,7 +290,9 @@ algorithm
for i in 1:arrayLength(isPrevVarArr) loop
if isPrevVarArr[i] then
var := BackendVariable.setVarFixed(BackendVariable.getVarAt(inSyst.orderedVars, i), true);
var := BackendVariable.setVarKind(var, BackendDAE.CLOCKED_STATE(previousName = ComponentReference.crefPrefixPrevious(var.varName)));
var := BackendVariable.setVarKind(var, BackendDAE.CLOCKED_STATE(
previousName = ComponentReference.crefPrefixPrevious(var.varName),
isStartFixed = isSome(subPartition.clock.solver)));
BackendVariable.setVarAt(inSyst.orderedVars, i, var);
prevVars := var.varName::prevVars;
end if;
Expand Down
8 changes: 8 additions & 0 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -9815,6 +9815,7 @@ case SIMCODE(modelInfo = MODELINFO(__), modelStructure = fmims) then
_clockInterval[<%i%>] = <%interval%> * <%fnom%>.0 / <%fres%>.0;
_clockShift[<%i%>] = <%snom%>.0 / <%sres%>.0;
_clockTime[<%i%>] = _simTime + _clockShift[<%i%>] * _clockInterval[<%i%>];
_clockStart[<%i%>] = true;
<%i%> ++;
>>
; separator="\n")
Expand Down Expand Up @@ -11564,6 +11565,9 @@ template equationSimpleAssign(SimEqSystem eq, Context context,Text &varDecls, Si
match eq
case SES_SIMPLE_ASSIGN(__) then
let &preExp = buffer "" /*BUFD*/
let startFixedExp = match cref2simvar(cref, simCode)
case SIMVAR(varKind = CLOCKED_STATE(isStartFixed = true)) then
"if (_clockStart[clockIndex - 1]) return;"
let expPart = daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)

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

void <%className%>::<%funcName%>(const UPDATETYPE command)
{
if (_simTime > _clockTime[<%idx%>]) {
_clockStart[<%idx%>] = false;
}
<%funcCalls%>
if (_simTime > _clockTime[<%idx%>]) {
_clockInterval[<%idx%>] = _simTime - _clockTime[<%idx%>];
Expand Down
1 change: 1 addition & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -1071,6 +1071,7 @@ package BackendDAE
record DUMMY_STATE end DUMMY_STATE;
record CLOCKED_STATE
DAE.ComponentRef previousName "the name of the previous variable";
Boolean isStartFixed "is fixed at first clock tick";
end CLOCKED_STATE;
record DISCRETE end DISCRETE;
record PARAM end PARAM;
Expand Down
18 changes: 10 additions & 8 deletions SimulationRuntime/cpp/Core/System/SystemDefaultImplementation.cpp
Expand Up @@ -55,7 +55,8 @@ SystemDefaultImplementation::SystemDefaultImplementation(IGlobalSettings *global
, _clockInterval (NULL)
, _clockShift (NULL)
, _clockTime (NULL)
, _clockCondition(NULL)
, _clockCondition (NULL)
, _clockStart (NULL)
, _outputStream(NULL)
, _callType (IContinuous::UNDEF_UPDATE)
, _initial (false)
Expand Down Expand Up @@ -91,7 +92,8 @@ SystemDefaultImplementation::SystemDefaultImplementation(SystemDefaultImplementa
, _clockInterval (NULL)
, _clockShift (NULL)
, _clockTime (NULL)
, _clockCondition(NULL)
, _clockCondition (NULL)
, _clockStart (NULL)
, _outputStream(NULL)
, _callType (IContinuous::UNDEF_UPDATE)
, _initial (false)
Expand Down Expand Up @@ -134,7 +136,8 @@ SystemDefaultImplementation::~SystemDefaultImplementation()
if(_clockInterval) delete [] _clockInterval;
if(_clockShift) delete [] _clockShift;
if(_clockTime) delete [] _clockTime;
if(_clockCondition) delete [] _clockCondition;
if(_clockCondition) delete [] _clockCondition;
if(_clockStart) delete [] _clockStart;
}

void SystemDefaultImplementation::Assert(bool cond,const string& msg)
Expand Down Expand Up @@ -237,18 +240,17 @@ void SystemDefaultImplementation::initialize()
_clockShift = new double [_dimClock];
if (_clockTime) delete [] _clockTime;
_clockTime = new double [_dimClock];
if (_clockCondition) delete [] _clockCondition;
if (_clockCondition) delete [] _clockCondition;
_clockCondition = new bool [_dimClock];
memset(_clockCondition,false,(_dimClock)*sizeof(bool));
memset(_clockCondition,false,(_dimClock)*sizeof(bool));
if (_clockStart) delete [] _clockStart;
_clockStart = new bool [_dimClock];
}
_start_time = 0.0;
_terminal = false;
_terminate = false;
_clockStart = true;

};


/// Set current integration time
void SystemDefaultImplementation::setTime(const double& t)
{
Expand Down
Expand Up @@ -181,8 +181,8 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation
double *_clockInterval; ///< time interval between clock ticks
double *_clockShift; ///< time before first activation
double *_clockTime; ///< time of clock ticks
bool* _clockCondition; ///< clock tick active
bool _clockStart; ///< only active at clock start
bool *_clockCondition; ///< clock tick active
bool *_clockStart; ///< only active at clock start
std::ostream *_outputStream; ///< Output stream for results

IContinuous::UPDATETYPE _callType;
Expand Down

0 comments on commit cde23af

Please sign in to comment.