Skip to content

Commit

Permalink
added time events in cpp template for sample function
Browse files Browse the repository at this point in the history
BouncingBall test use CVode solver now

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17340 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
niklwors committed Sep 24, 2013
1 parent 6bc93eb commit e5958c6
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 16 deletions.
60 changes: 48 additions & 12 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -242,6 +242,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
//Number of equations
<%dimension1(simCode)%>
_dimZeroFunc= <%zerocrosslength(simCode)%>;
_dimTimeEvent = <%timeeventlength(simCode)%>;
//Number of residues
<%if Flags.isSet(Flags.WRITE_TO_BUFFER) then
<<
Expand Down Expand Up @@ -277,9 +278,9 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
<%setConditions(simCode)%>
<%giveConditions(simCode)%>
<%generateStepCompleted(listAppend(allEquations,initialEquations),simCode)%>
<%generatehandleTimeEvent(listAppend(allEquations,initialEquations),simCode)%>
<%generatehandleTimeEvent(sampleLookup,simCode)%>
<%generateDimTimeEvent(listAppend(allEquations,initialEquations),simCode)%>
<%generateTimeEvent(listAppend(allEquations,initialEquations),simCode)%>
<%generateTimeEvent(sampleLookup,simCode)%>
<%isODE(simCode)%>
Expand Down Expand Up @@ -4225,16 +4226,26 @@ then
end generateStepCompleted;


template generatehandleTimeEvent(list<SimEqSystem> allEquations,SimCode simCode)
template generatehandleTimeEvent(BackendDAE.SampleLookup sampleLookup,SimCode simCode)
::=

match simCode
case SIMCODE(modelInfo = MODELINFO(__))
then
<<
void <%lastIdentOfPath(modelInfo.name)%>::handleTimeEvent(int* time_events)
{
for(int i = 0;i<_dimTimeEvent;i++)
{
if(time_events[i]!=_time_event_counter[i])
_time_conditions[i] = true;
else
_time_conditions[i] = false;
}
memcpy(_time_event_counter,time_events,(int)_dimTimeEvent*sizeof(int));
}
>>

Expand All @@ -4248,23 +4259,35 @@ then
<<
int <%lastIdentOfPath(modelInfo.name)%>::getDimTimeEvent() const
{
return 0;
return _dimTimeEvent;
}
>>

end generateDimTimeEvent;


template generateTimeEvent(list<SimEqSystem> allEquations,SimCode simCode)
template generateTimeEvent(BackendDAE.SampleLookup sampleLookup,SimCode simCode)
::=

match simCode
case SIMCODE(modelInfo = MODELINFO(__))
then
let &varDecls = buffer "" /*BUFD*/
<<
void <%lastIdentOfPath(modelInfo.name)%>::getTimeEvent(time_event_type& time_events)
{
<%match sampleLookup
case BackendDAE.SAMPLE_LOOKUP(__) then
(lookup |> (index, start, interval) =>
let &preExp = buffer "" /*BUFD*/
let e1 = daeExp(start, contextOther, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
let e2 = daeExp(interval, contextOther, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)
<<
<%preExp%>
time_events.push_back(std::make_pair(<%e1%>, <%e2%>));
>>)%>
}
>>

Expand Down Expand Up @@ -5208,7 +5231,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
let &preExp = buffer "" /*BUFD*/
let eStart = daeExp(start, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
let eInterval = daeExp(interval, contextOther, &preExp, &varDecls /*BUFD*/,simCode)
'_conditions[<%intSub(index, 1)%>]'
'_time_conditions[<%intSub(index, 1)%>]'
case CALL(path=IDENT(name="initial") ) then
match context

Expand Down Expand Up @@ -6045,14 +6068,26 @@ end helpvarlength;
template zerocrosslength(SimCode simCode)
::=
match simCode
case SIMCODE(__) then
let size = listLength(zeroCrossings)
case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
let size = listLength(zeroCrossings)
<<
<%size%>
<%intSub(listLength(zeroCrossings), vi.numTimeEvents)%>
>>
end zerocrosslength;


template timeeventlength(SimCode simCode)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then

<<
<%vi.numTimeEvents%>
>>
end timeeventlength;



template DimZeroFunc(SimCode simCode)
::=
match simCode
Expand Down Expand Up @@ -6885,7 +6920,8 @@ template giveZeroFunc3(Integer index1, Exp relation, Text &varDecls /*BUFP*/,Tex
error(sourceInfo(), ' UNKNOWN RELATION for <%index1%>')
end match
case CALL(path=IDENT(name="sample"), expLst={_, start, interval}) then
error(sourceInfo(), ' sample not supported for <%index1%> ')
//error(sourceInfo(), ' sample not supported for <%index1%> ')
'//sample for <%index1%>'
else
error(sourceInfo(), ' UNKNOWN ZERO CROSSING for <%index1%> ')
end match
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/cpp/Core/SimController/Main.cpp
Expand Up @@ -113,7 +113,7 @@ int main(int argc, const char* argv[])


//SimController to start simulation
SimSettings settings = {solver,"Newton",starttime,stoptime,stepsize,0.0001,1,results_file_path.string()};
SimSettings settings = {solver,"Newton",starttime,stoptime,stepsize,1e-12,0.01,results_file_path.string()};
boost::shared_ptr<ISimController> sim_controller = boost::shared_ptr<ISimController>(new SimController(runtime_lib_path,modelica_path));
//create Modelica system
std::pair<boost::weak_ptr<IMixedSystem>,boost::weak_ptr<ISimData> > system = sim_controller->LoadSystem("ModelicaSystem");
Expand Down
3 changes: 1 addition & 2 deletions SimulationRuntime/cpp/Core/SimController/SimManager.cpp
Expand Up @@ -478,9 +478,8 @@ void SimManager::runSingleProcess()
{
if(_writeFinalState)
{
_solverTask = ISolver::SOLVERCALL(_solverTask | ISolver::RECORDCALL);
_solverTask = ISolver::SOLVERCALL(ISolver::RECORDCALL);
_solver->solve(_solverTask);
_solverTask = ISolver::SOLVERCALL(_solverTask ^ ISolver::RECORDCALL);
}
}

Expand Down
15 changes: 15 additions & 0 deletions SimulationRuntime/cpp/Core/System/SystemDefaultImplementation.cpp
Expand Up @@ -10,6 +10,8 @@ SystemDefaultImplementation::SystemDefaultImplementation(IGlobalSettings& global
, __z (NULL)
, __zDot (NULL)
,_conditions(NULL)
,_time_conditions(NULL)
,_time_event_counter(NULL)

{

Expand Down Expand Up @@ -90,6 +92,19 @@ void SystemDefaultImplementation::initialize()
memset(_conditions,false,(_dimZeroFunc)*sizeof(bool));

}
if(_dimTimeEvent > 0)
{
if(_time_conditions) delete [] _time_conditions ;
if(_time_event_counter) delete [] _time_event_counter;
_time_conditions = new bool[_dimTimeEvent];


_time_event_counter = new int[_dimTimeEvent];

memset(_time_conditions,false,(_dimTimeEvent)*sizeof(bool));
memset(_time_event_counter,0,(_dimTimeEvent)*sizeof(int));
}

};


Expand Down
Expand Up @@ -125,7 +125,8 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation
*__z, ///< "Extended state vector", containing all states and algebraic variables of all types
*__zDot; ///< "Extended vector of derivatives", containing all right hand sides of differential and algebraic equations
bool
* _conditions; ///< External conditions changed by the solver
* _conditions, ///< External conditions changed by the solver
* _time_conditions;

int
_dimContinuousStates,
Expand All @@ -138,6 +139,8 @@ class BOOST_EXTENSION_SYSTEM_DECL SystemDefaultImplementation
_dimTimeEvent, ///< Dimension (=Anzahl) Time event (start zeit und frequenz)
_dimAE; ///< Number (dimension) of algebraic equations (e.g. constraints from an algebraic loop)

int
* _time_event_counter;
ostream
*_outputStream; ///< Output stream for results

Expand Down
10 changes: 10 additions & 0 deletions SimulationRuntime/cpp/Solver/CVode/CVode.cpp
Expand Up @@ -207,6 +207,12 @@ void Cvode::solve(const SOLVERCALL action)

}

if(action & RECORDCALL)
{
writeToFile(_accStps, _tCurrent, _h);
return;
}

// Nach einem TimeEvent wird der neue Zustand recorded
if(action & RECALL)
{
Expand Down Expand Up @@ -326,6 +332,10 @@ void Cvode::CVodeCore()
_idid = CVodeReInit(_cvodeMem, _tCurrent, _CV_y);
if(_idid < 0)
throw std::runtime_error("CVode::ReInit()");

// Der Eventzeitpunkt kann auf der Endzeit liegen (Time-Events). In diesem Fall wird der Solver beendet, da CVode sonst eine interne Warnung schmeißt
if(_tCurrent == _tEnd)
_cv_rt = CV_TSTOP_RETURN;
}

// Zähler für die Anzahl der ausgegebenen Schritte erhöhen
Expand Down

0 comments on commit e5958c6

Please sign in to comment.