Skip to content

Commit

Permalink
Added support for sample to adevs
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14177 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
smiz committed Dec 1, 2012
1 parent 67c873e commit c5d9436
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
48 changes: 43 additions & 5 deletions Compiler/Template/CodegenAdevs.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
* These methods may be overridden by any derived class.
*/
virtual void extra_state_event_funcs(double* z){}
double time_event_func(const double* q) { return DBL_MAX; }
double time_event_func(const double* q);
void internal_event(double* q, const bool* state_event);
void external_event(double* q, double e,
const adevs::Bag<OMC_ADEVS_IO_TYPE>& xb){}
Expand All @@ -121,6 +121,7 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
* These methods are used to access variables and
* parameters in the modelica model by name.
*/
double getEventEpsilon() const { return epsilon; }
<%makeGetAccessors(modelInfo)%>

/// These methods are for solving non-linear algebraic eqns
Expand Down Expand Up @@ -158,6 +159,10 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then

void calc_vars(const double* q = NULL, bool doReinit = false);

AdevsSampleData** samples;
int numTimeEvents() { return <%vi.numTimeEvents%>; }
bool sample(double tStart, double tInterval, int index);

protected:
/**
* Calculate the values of the state and algebraic variables.
Expand Down Expand Up @@ -267,7 +272,8 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
epsilon(eventHys),
helpVars(NULL),
helpVars_saved(NULL),
zc(NULL)
zc(NULL),
samples(NULL)
{
timeValue = 0.0;
if (numHelpVars() > 0)
Expand All @@ -277,13 +283,25 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
}
if (numZeroCrossings() > 0)
zc = new int[numZeroCrossings()];
if (numTimeEvents() > 0)
{
samples = new AdevsSampleData*[numZeroCrossings()];
for (int i = 0; i < numTimeEvents(); i++)
samples[i] = NULL;
}
}

<%lastIdentOfPath(modelInfo.name)%>::~<%lastIdentOfPath(modelInfo.name)%>()
{
if (helpVars != NULL) delete [] helpVars;
if (helpVars_saved != NULL) delete [] helpVars_saved;
if (zc != NULL) delete [] zc;
if (samples != NULL)
{
for (int i = 0; i < numTimeEvents(); i++)
if (samples[i] != NULL) delete samples[i];
delete [] samples;
}
}

<%makeExtraResiduals(allEquations,lastIdentOfPath(modelInfo.name))%>
Expand All @@ -302,7 +320,7 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then

<%makeDerFuncCalculator(simCode)%>

<%makeStateEventFunc(simCode)%>
<%makeEventFunc(simCode)%>

>>
end simulationCppFile;
Expand Down Expand Up @@ -404,7 +422,7 @@ case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__))) then
>>
end makeExtraFunctionsAndRecords;

template makeStateEventFunc(SimCode simCode)
template makeEventFunc(SimCode simCode)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__))) then
Expand Down Expand Up @@ -440,21 +458,41 @@ case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__))) then
restore_vars();
}

bool <%lastIdentOfPath(modelInfo.name)%>::sample(double tStart, double tInterval, int index)
{
if (samples[index] == NULL)
samples[index] = new AdevsSampleData(tStart,tInterval);
return samples[index]->atEvent(timeValue,epsilon);
}

double <%lastIdentOfPath(modelInfo.name)%>::time_event_func(const double* q)
{
double ttgMin = adevs_inf<double>();
for (int i = 0; i < numTimeEvents(); i++)
{
double ttg = samples[i]->timeToEvent(timeValue);
if (ttg < ttgMin) ttgMin = ttg;
}
return ttgMin;
}

void <%lastIdentOfPath(modelInfo.name)%>::internal_event(double* q, const bool* state_event)
{
atEvent = true;
// Switch the state event variables
for (int i = 0; i < numZeroCrossings(); i++)
if (state_event[i]) zc[i] = !zc[i];
calc_vars(q);
for (int i = 0; i < numTimeEvents(); i++)
samples[i]->update(timeValue,epsilon);
save_vars(); // save the new state of the model
// Reinitialize state variables that need to be reinitialized
<%(vars.stateVars |> SIMVAR(__) => 'q[<%index%>]=<%cref(name)%>;') ;separator="\n"%>
atEvent = false;
}

>>
end makeStateEventFunc;
end makeEventFunc;

template zeroCrossingEqns(list<ZeroCrossing> zeroCrossings)
"Generates function in simulation file."
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/NFInstDumpTpl.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ encapsulated package NFInstDumpTpl
package: NFInstDumpTpl
description: Generated by Susan.
$Id: TplCodegen.mo 9216 2011-05-31 10:52:47Z sjoelund.se $
$Id$
"

public import Tpl;
Expand Down

0 comments on commit c5d9436

Please sign in to comment.