Skip to content

Commit

Permalink
fix for 3d array in cpp runtime, added stop method to solver interface,
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23565 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
niklwors committed Nov 26, 2014
1 parent e03dabe commit d0f2d56
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 49 deletions.
25 changes: 17 additions & 8 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -2924,7 +2924,7 @@ case efn as EXTERNAL_FUNCTION(extArgs=extArgs) then

else
//(List.restOrEmpty(outVars) |> var hasindex i1 fromindex 1 => varOutputTuple(fn, var,i1, &varDecls1, &outVarInits1, &outVarCopy1, &outVarAssign1, simCode, useFlatArrayNotation)
(outVars |> var hasindex i1 fromindex 0 => varOutputTuple(fn, var,i1, &varDecls1, &outVarInits1, &outVarCopy1, &outVarAssign1, simCode, useFlatArrayNotation)
(outVars |> var hasindex i1 fromindex 0 => varOutputTuple(fn, var,i1, &varDecls, &outVarInits, &outVarCopy1, &outVarAssign1, simCode, useFlatArrayNotation)
;separator="\n"; empty /* increase the counter! */
)
end match
Expand Down Expand Up @@ -3367,18 +3367,20 @@ match var
/* The storage size of arrays is known at call time, so they can be allocated
* before set_memory_state. Strings are not known, so we copy them, etc...
*/
case var as VARIABLE(ty = T_STRING(__)) then
/*
case var as VARIABLE(ty = T_STRING(__)) then
if not acceptMetaModelicaGrammar() then
// We need to strdup() all strings, then allocate them on the memory pool again, then free the temporary string
let strVar = tempDecl("string", &varDecls)
let &varAssign +=
<<
/*_<%fname%>*/ output = <%strVar%>;
output = <%strVar%>;
>>
""
else
let &varAssign += '/*_<%fname%>*/ output= <%contextCref(var.name,contextFunction,simCode,useFlatArrayNotation)%>;<%\n%>'
let &varAssign += output= <%contextCref(var.name,contextFunction,simCode,useFlatArrayNotation)%>;<%\n%>'
""
*/
case var as VARIABLE(__) then
let marker = '<%contextCref(var.name,contextFunction,simCode,useFlatArrayNotation)%>'
let &varInits += '/* varOutputTuple varInits(<%marker%>) */ <%\n%>'
Expand Down Expand Up @@ -9805,15 +9807,22 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
let &preExp += '<%retVar%> = <%daeExpCallBuiltinPrefix(attr.builtin)%><%funName%>(<%argStr%>);<%\n%>'
if attr.builtin then '<%retVar%>' else '<%retVar%>.<%retType%>_1'

case CALL(path=IDENT(name="sinh"),
case CALL(path=IDENT(name="sinh"),
expLst={e1},attr=attr as CALL_ATTR(__)) then
let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode,useFlatArrayNotation)%>' ;separator=", ")
let funName = '<%underscorePath(path)%>'
let retType = 'double'
let retVar = tempDecl(retType, &varDecls /*BUFD*/)
let &preExp += '<%retVar%> = <%daeExpCallBuiltinPrefix(attr.builtin)%><%funName%>(<%argStr%>);<%\n%>'
if attr.builtin then '<%retVar%>' else '<%retVar%>.<%retType%>_1'
case CALL(path=IDENT(name="asin"),
expLst={e1},attr=attr as CALL_ATTR(__)) then
let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode,useFlatArrayNotation)%>' ;separator=", ")
let funName = '<%underscorePath(path)%>'
let retType = 'double'
let retVar = tempDecl(retType, &varDecls /*BUFD*/)
let &preExp += '<%retVar%> = <%daeExpCallBuiltinPrefix(attr.builtin)%><%funName%>(<%argStr%>);<%\n%>'
if attr.builtin then '<%retVar%>' else '<%retVar%>.<%retType%>_1'

case CALL(path=IDENT(name="cos"),
expLst={e1},attr=attr as CALL_ATTR(__)) then
let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode,useFlatArrayNotation)%>' ;separator=", ")
Expand Down Expand Up @@ -11304,7 +11313,7 @@ case ecr as CREF(componentRef=cr as CREF_QUAL(__)) then
else
let arrName = contextCref(crefStripSubs(cr), context, simCode,useFlatArrayNotation)
<<
<%arrName%>(<%threadDimSubList(crefDims(cr),crefSubs(cr),context,&preExp,&varDecls,simCode, useFlatArrayNotation)%>)
<%arrName%>(<%threadDimSubList(crefDims(cr),crefSubs(cr),context,&preExp,&varDecls,simCode, useFlatArrayNotation)%>)/*testindex2*/
>>

case ecr as CREF(componentRef=CREF_QUAL(__)) then
Expand Down Expand Up @@ -11341,7 +11350,7 @@ template threadDimSubList(list<Dimension> dims, list<Subscript> subs, Context co
case DIM_BOOLEAN(__) then '*2'
case DIM_ENUM(__) then '*<%size%>'
else error(sourceInfo(),"Non-constant dimension in simulation context")
%>)<%match subrest case {} then "" else '+<%threadDimSubList(dimrest, subrest, context, &preExp, &varDecls, simCode, useFlatArrayNotation)%>'%>'
%>)<%match subrest case {} then "" else ',<%threadDimSubList(dimrest, subrest, context, &preExp, &varDecls, simCode, useFlatArrayNotation)%>'%>'
else error(sourceInfo(),"Less subscripts that dimensions in indexing cref? That's odd!")
else error(sourceInfo(),"Non-index subscript in indexing cref? That's odd!")
end threadDimSubList;
Expand Down
10 changes: 6 additions & 4 deletions SimulationRuntime/cpp/Core/SimController/SimController.cpp
Expand Up @@ -129,7 +129,7 @@ void SimController::Start(boost::shared_ptr<IMixedSystem> mixedsystem, SimSettin
{
try
{
boost::shared_ptr<SimManager> simMgr;


IGlobalSettings* global_settings = _config->getGlobalSettings();

Expand All @@ -145,17 +145,17 @@ void SimController::Start(boost::shared_ptr<IMixedSystem> mixedsystem, SimSettin
global_settings->setAlarmTime(simsettings.timeOut);
global_settings->setOutputPointType(simsettings.outputPointType);

simMgr = boost::shared_ptr<SimManager>(new SimManager(mixedsystem, _config.get()));
_simMgr = boost::shared_ptr<SimManager>(new SimManager(mixedsystem, _config.get()));

ISolverSettings* solver_settings = _config->getSolverSettings();
solver_settings->setLowerLimit(simsettings.lower_limit);
solver_settings->sethInit(simsettings.lower_limit);
solver_settings->setUpperLimit(simsettings.upper_limit);
solver_settings->setRTol(simsettings.tolerance);
solver_settings->setATol(simsettings.tolerance);
simMgr->initialize();
_simMgr->initialize();

simMgr->runSimulation();
_simMgr->runSimulation();

/* if(boost::shared_ptr<IMixedSystem> history_system = mixedsystem.lock())
{
Expand Down Expand Up @@ -190,4 +190,6 @@ void SimController::Start(boost::shared_ptr<IMixedSystem> mixedsystem, SimSettin

void SimController::Stop()
{
if(_simMgr)
_simMgr->stopSimulation();
}
6 changes: 6 additions & 0 deletions SimulationRuntime/cpp/Core/SimController/SimManager.cpp
Expand Up @@ -253,6 +253,12 @@ void SimManager::runSimulation()
throw;
}
}

void SimManager::stopSimulation()
{
if(_solver)
_solver->stop();
}
void SimManager::writeProperties()
{
if(_config->getGlobalSettings()->getLogType() != STATS)
Expand Down
5 changes: 4 additions & 1 deletion SimulationRuntime/cpp/Core/Solver/SimulationMonitor.cpp
Expand Up @@ -22,7 +22,10 @@ void SimulationMonitor::setTimeOut(unsigned int time_out)
{
/*_time_out = nanosecond_type(time_out* 1000000000LL);*/
}

void SimulationMonitor::stop()
{
_interrupt =true;
}
void SimulationMonitor::checkTimeout()
{
/* cpu_times elapsed_times(_timer.elapsed());
Expand Down
38 changes: 14 additions & 24 deletions SimulationRuntime/cpp/Include/Core/Math/Array.h
Expand Up @@ -581,21 +581,21 @@ template<typename T ,std::size_t size1, std::size_t size2, std::size_t size3> cl
}
virtual T& operator()(vector<size_t> idx)
{
return _real_array[size3 * size2 * (idx[0] - 1) + size2 * (idx[1] - 1) + (idx[2] - 1)];
return _real_array[ size2 * (idx[0] - 1) +size3 * (idx[1] - 1) + (idx[2] - 1)];
};
inline virtual T& operator()(unsigned int i, unsigned int j, unsigned int k)
{
return _real_array[size3 * size2 * (i - 1) + size2 * (j - 1) + (k - 1)];
return _real_array[size2 * (i - 1) + size3 * (j - 1) + (k - 1)];
}

virtual unsigned int getNumElems()
{
return size1 + size2 + size3;
return size1 * size2 * size3;
}

virtual unsigned int getNumDims()
{
return 2;
return 3;
}

virtual void setDims(std::vector<size_t> v) { }
Expand Down Expand Up @@ -1197,7 +1197,7 @@ template<typename T> class DynArrayDim3 : public BaseArray<T>
_multi_array.assign(data_otherarray,data_otherarray+v[0]*v[1]*v[3]);
}
~DynArrayDim3(){}

/*
void assign(DynArrayDim3<T> otherArray)
{
std::vector<size_t> v = otherArray.getDims();
Expand All @@ -1206,25 +1206,18 @@ template<typename T> class DynArrayDim3 : public BaseArray<T>
T* data = otherArray._multi_array.data();
_multi_array.assign(data, data + v[0]*v[1]*v[2]);
}

*/
virtual void assign(const T* data)
{
_multi_array.assign(data, data + _multi_array.num_elements() );
}
virtual void assign(const BaseArray<T>& otherArray)
{
std::vector<size_t> v = otherArray.getDims();
_multi_array.resize(v);
_multi_array.reindex(1);
const T* data = otherArray._multi_array.data();
_multi_array.assign(data, data + otherArray._multi_array.num_elements());
/*for (int i = 1; i <= v[0]; i++)
{
for (int j = 1; j <= v[1]; i++)
{
for (int k = 1; k <= v[1]; i++)
{
_multi_array[i][j][k] = otherArray(i,j,k);
}
}
}
*/
const T* data = otherArray.getData();
_multi_array.assign(data, data + v[0]*v[1]*v[2]);
}
DynArrayDim3<T>& operator=(const DynArrayDim3<T>& rhs)
{
Expand All @@ -1238,10 +1231,7 @@ template<typename T> class DynArrayDim3 : public BaseArray<T>
}
return *this;
}
virtual void assign(const T& data)
{
_multi_array.assign(data, data + _multi_array.num_elements() );
}


void setDims(unsigned int size1, unsigned int size2, unsigned int size3)
{
Expand Down
Expand Up @@ -32,6 +32,7 @@ class SimManager
SimManager(boost::shared_ptr<IMixedSystem> system, Configuration* _config);
~SimManager();
void runSimulation();
void stopSimulation();
void initialize();

// for real-time usage (VxWorks and BODAS)
Expand Down
5 changes: 3 additions & 2 deletions SimulationRuntime/cpp/Include/Core/Solver/ISolver.h
Expand Up @@ -62,8 +62,9 @@ class ISolver

/// Provides the status of the solver after returning
virtual const SOLVERSTATUS getSolverStatus() = 0;
//virtual void setTimeOut(unsigned int time_out) = 0;

virtual void setTimeOut(unsigned int time_out) = 0;
virtual void stop() = 0;

/// Write out statistical information (statistical information of last simulation, e.g. time, number of steps, etc.)
virtual void writeSimulationInfo() = 0;

Expand Down
Expand Up @@ -11,6 +11,7 @@ class BOOST_EXTENSION_MONITOR_DECL SimulationMonitor
~SimulationMonitor();
void initialize();
void setTimeOut(unsigned int time_out);
void stop();
void checkTimeout();

protected:
Expand Down
5 changes: 3 additions & 2 deletions SimulationRuntime/cpp/Include/Solver/CVode/CVode.h
Expand Up @@ -121,7 +121,8 @@ class Cvode

//// Ausgabe von statistischen Informationen (wird vom SimManager nach Abschluß der Simulation aufgerufen)
virtual void writeSimulationInfo();

virtual void setTimeOut(unsigned int time_out);
virtual void stop();

virtual const int reportErrorMessage(std::ostream& messageStream);
virtual bool stateSelection();
Expand All @@ -142,7 +143,7 @@ class Cvode
// Nulltellenfunktion
void giveZeroVal(const double &t,const double *y,double *zeroValue);
void writeCVodeOutput(const double &time,const double &h,const int &stp);

bool isInterrupted();
// Callback der Nullstellenfunktion
static int CV_ZerofCallback(double t, N_Vector y, double *zeroval, void *user_data);

Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/cpp/Include/Solver/Euler/Euler.h
Expand Up @@ -51,6 +51,7 @@ class Euler : public ISolver, public SolverDefaultImplementation
/// Write out statistical information (statistical information of last simulation, e.g. time, number of steps, etc.)
virtual void writeSimulationInfo();
virtual void setTimeOut(unsigned int time_out);
virtual void stop();
/// Indicates whether a solver error occurred during integration, returns type of error and provides error message
virtual const int reportErrorMessage(ostream& messageStream);
virtual bool stateSelection();
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/cpp/Include/Solver/Peer/Peer.h
Expand Up @@ -121,6 +121,8 @@ class Peer

virtual const int reportErrorMessage(std::ostream& messageStream);
virtual bool stateSelection();
virtual void setTimeOut(unsigned int time_out);
virtual void stop();
private:

// Solveraufruf
Expand Down
30 changes: 24 additions & 6 deletions SimulationRuntime/cpp/Solver/CVode/CVode.cpp
Expand Up @@ -300,7 +300,7 @@ void Cvode::solve(const SOLVERCALL action)
// Solver soll fortfahren
_solverStatus = ISolver::CONTINUE;

while (_solverStatus & ISolver::CONTINUE)
while (_solverStatus & ISolver::CONTINUE && !_interrupt )
{
// Zuvor wurde initialize aufgerufen und hat funktioniert => RESET IDID
if (_idid == 5000)
Expand Down Expand Up @@ -340,7 +340,18 @@ void Cvode::solve(const SOLVERCALL action)
throw std::invalid_argument("CVode::solve()");
}
}

bool Cvode::isInterrupted()
{
if(_interrupt)
{
_solverStatus = DONE;
return true;
}
else
{
return false;
}
}
void Cvode::CVodeCore()
{
_idid = CVodeReInit(_cvodeMem, _tCurrent, _CV_y);
Expand All @@ -352,7 +363,7 @@ void Cvode::CVodeCore()
bool writeEventOutput = (_settings->getGlobalSettings()->getOutputPointType() == ALL);
bool writeOutput = !(_settings->getGlobalSettings()->getOutputPointType() == EMPTY2);

while (_solverStatus & ISolver::CONTINUE)
while (_solverStatus & ISolver::CONTINUE && !_interrupt )
{
_cv_rt = CVode(_cvodeMem, _tEnd, _CV_y, &_tCurrent, CV_ONE_STEP);

Expand Down Expand Up @@ -388,7 +399,7 @@ void Cvode::CVodeCore()
}

// A root was found
if ((_cv_rt == CV_ROOT_RETURN))
if ((_cv_rt == CV_ROOT_RETURN) && !isInterrupted())
{
// CVode is setting _tCurrent to the time where the first event occurred
double _abs = fabs(_tLastEvent - _tCurrent);
Expand Down Expand Up @@ -445,7 +456,7 @@ void Cvode::CVodeCore()
}
}

if (_zeroFound || state_selection)
if ((_zeroFound || state_selection)&& !isInterrupted())
{
// Write the values of (P2)
if (writeEventOutput)
Expand Down Expand Up @@ -480,7 +491,14 @@ void Cvode::CVodeCore()
}
}
}

void Cvode::setTimeOut(unsigned int time_out)
{
SimulationMonitor::setTimeOut(time_out);
}
void Cvode::stop()
{
SimulationMonitor::stop();
}
void Cvode::writeCVodeOutput(const double &time, const double &h, const int &stp)
{
if (stp > 0)
Expand Down
5 changes: 4 additions & 1 deletion SimulationRuntime/cpp/Solver/Euler/Euler.cpp
Expand Up @@ -131,7 +131,10 @@ void Euler::initialize()
{
SimulationMonitor::setTimeOut(time_out);
}

void Euler::stop()
{
SimulationMonitor::stop();
}

/// Set start t for numerical solution
void Euler::setStartTime(const double& t)
Expand Down
9 changes: 8 additions & 1 deletion SimulationRuntime/cpp/Solver/Peer/Peer.cpp
Expand Up @@ -803,7 +803,14 @@ void Peer::writeSimulationInfo()
// << "Solver-Status: " << _idid << "\n\n";
*/
}

void Peer::setTimeOut(unsigned int time_out)
{
SimulationMonitor::setTimeOut(time_out);
}
void Peer::stop()
{
SimulationMonitor::stop();
}
int Peer::check_flag(void *flagvalue, const char *funcname, int opt)
{
/*
Expand Down

0 comments on commit d0f2d56

Please sign in to comment.