Skip to content

Commit

Permalink
added restoreOldValues and restoreNewValues to Algoop of cpp runtime …
Browse files Browse the repository at this point in the history
…to restore all alglopvalues during output routine
  • Loading branch information
niklwors authored and OpenModelica-Hudson committed Aug 24, 2015
1 parent 8c86132 commit 879063b
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 25 deletions.
2 changes: 1 addition & 1 deletion 3rdParty
Submodule 3rdParty updated 237 files
141 changes: 140 additions & 1 deletion Compiler/Template/CodegenCpp.tpl
Expand Up @@ -3566,9 +3566,11 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
<%isConsistent(simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)%>

<%generateStepCompleted(listAppend(allEquations,initialEquations),simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>

<%generateStepStarted(listAppend(allEquations,initialEquations),simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace,useFlatArrayNotation)%>

<%generateRestoreOldValues(listAppend(allEquations,initialEquations),simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>
<%generateRestoreNewValues(listAppend(allEquations,initialEquations),simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>

<%generatehandleTimeEvent(timeEvents, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, boolNot(stringEq(getConfigString(PROFILING_LEVEL),"none")))%>
<%generateDimTimeEvent(listAppend(allEquations,initialEquations),simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)%>
<%generateTimeEvent(timeEvents, simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, true)%>
Expand Down Expand Up @@ -7296,6 +7298,7 @@ int <%modelname%>Algloop<%ls.index%>::getDimReal() const
return(AlgLoopDefaultImplementation::getDimReal());
};


/// Provide number (dimension) of residuals according to data type
int <%modelname%>Algloop<%ls.index%>::getDimRHS() const
{
Expand Down Expand Up @@ -7340,6 +7343,7 @@ int <%modelname%>Algloop<%nls.index%>::getDimReal() const
return(AlgLoopDefaultImplementation::getDimReal());
};


/// Provide number (dimension) of residuals according to data type
int <%modelname%>Algloop<%nls.index%>::getDimRHS() const
{
Expand Down Expand Up @@ -7452,6 +7456,8 @@ case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__))) then
// System is able to provide the Jacobian symbolically
virtual bool provideSymbolicJacobian();

virtual void restoreOldValues();
virtual void restoreNewValues();
virtual bool stepCompleted(double time);
virtual bool stepStarted(double time);

Expand Down Expand Up @@ -9815,6 +9821,139 @@ let store_delay_expr = functionStoreDelay(delayedExps, simCode ,&extraFuncs ,&ex

end generateStepCompleted;



template generateRestoreOldValues(list<SimEqSystem> allEquations,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
::=
let &varDecls = buffer "" /*BUFD*/
let algloopsolver = generateRestoreOldValues2(allEquations,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)
match simCode
case SIMCODE(modelInfo = MODELINFO(__))
then
<<
void <%lastIdentOfPath(modelInfo.name)%>::restoreOldValues()
{
<%algloopsolver%>
}
>>

end generateRestoreOldValues;


template generateRestoreOldValues2(list<SimEqSystem> allEquations,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
::=
let &varDecls = buffer "" /*BUFD*/
let algloopsolver = (allEquations |> eqs => (eqs |> eq =>
generateRestoreOldValues3(eq, contextOther, &varDecls /*BUFC*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace) ;separator="\n")
;separator="\n")

<<
<%algloopsolver%>
>>

end generateRestoreOldValues2;


template generateRestoreOldValues3(SimEqSystem eq, Context context, Text &varDecls, SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
"Generates an equation.
This template should not be used for a SES_RESIDUAL.
Residual equations are handled differently."
::=
match eq
case SES_LINEAR(lSystem = ls as LINEARSYSTEM(__))
then
let num = ls.index
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
_algLoopSolver<%num%>->restoreOldValues();
>>
end match
case e as SES_NONLINEAR(nlSystem = nls as NONLINEARSYSTEM(__))
then
let num = nls.index
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
_algLoopSolver<%num%>->restoreOldValues();
>>
end match
case e as SES_MIXED(cont = eq_sys)
then
<<
<%generateRestoreOldValues3(eq_sys,context,varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)%>
>>
else
""
end generateRestoreOldValues3;


template generateRestoreNewValues(list<SimEqSystem> allEquations,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
::=
let &varDecls = buffer "" /*BUFD*/
let algloopsolver = generateRestoreOldValues2(allEquations,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)
match simCode
case SIMCODE(modelInfo = MODELINFO(__))
then
<<
void <%lastIdentOfPath(modelInfo.name)%>::restoreNewValues()
{
<%algloopsolver%>
}
>>

end generateRestoreNewValues;


template generateRestoreNewValues2(list<SimEqSystem> allEquations,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
::=
let &varDecls = buffer "" /*BUFD*/
let algloopsolver = (allEquations |> eqs => (eqs |> eq =>
generateRestoreNewValues3(eq, contextOther, &varDecls /*BUFC*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace) ;separator="\n")
;separator="\n")

<<
<%algloopsolver%>
>>

end generateRestoreNewValues2;


template generateRestoreNewValues3(SimEqSystem eq, Context context, Text &varDecls, SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace)
"Generates an equation.
This template should not be used for a SES_RESIDUAL.
Residual equations are handled differently."
::=
match eq
case SES_LINEAR(lSystem = ls as LINEARSYSTEM(__))
then
let num = ls.index
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
_algLoopSolver<%num%>->restoreNewValues();
>>
end match
case e as SES_NONLINEAR(nlSystem = nls as NONLINEARSYSTEM(__))
then
let num = nls.index
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
_algLoopSolver<%num%>->restoreNewValues();
>>
end match
case e as SES_MIXED(cont = eq_sys)
then
<<
<%generateRestoreNewValues3(eq_sys,context,varDecls,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace)%>
>>
else
""
end generateRestoreNewValues3;



template generateStepStarted(list<SimEqSystem> allEquations,SimCode simCode ,Text& extraFuncs,Text& extraFuncsDecl,Text extraFuncsNamespace, Boolean useFlatArrayNotation)
::=
let &varDecls = buffer "" /*BUFD*/
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/cpp/Include/Core/Solver/IAlgLoopSolver.h
Expand Up @@ -42,5 +42,7 @@ class IAlgLoopSolver
/// Returns the status of iteration
virtual ITERATIONSTATUS getIterationStatus() = 0;
virtual void stepCompleted(double time) = 0;
virtual void restoreOldValues() = 0;
virtual void restoreNewValues() = 0;
};
/** @} */ // end of coreSolver
5 changes: 4 additions & 1 deletion SimulationRuntime/cpp/Include/Core/System/IContinuous.h
Expand Up @@ -87,7 +87,10 @@ class IContinuous

/// Provide the right hand side
virtual void setRHS(const double* f) = 0;

///Restores all algloop variables for a output step
virtual void restoreOldValues() = 0;
///Restores all algloop variables for last output step
virtual void restoreNewValues() = 0;
/// Update transfer behavior of the system of equations according to command given by solver

virtual bool evaluateAll(const UPDATETYPE command = UNDEF_UPDATE) = 0; // vxworks
Expand Down
7 changes: 5 additions & 2 deletions SimulationRuntime/cpp/Include/Solver/Hybrj/Hybrj.h
Expand Up @@ -33,7 +33,8 @@ class Hybrj : public IAlgLoopSolver
/// Returns the status of iteration
virtual ITERATIONSTATUS getIterationStatus();
virtual void stepCompleted(double time);

virtual void restoreOldValues();
virtual void restoreNewValues();

private:
/// Encapsulation of determination of residuals to given unknowns
Expand Down Expand Up @@ -75,7 +76,9 @@ class Hybrj : public IAlgLoopSolver
*_x_nom, //Nominal value of unknown variables
*_x_ex, //extraplated unknown varibales
*_x_scale, //current scale factor of unknown varibales
*_zeroVec,
*_zeroVec,
*_x_old,
*_x_new,
_t0, //old time
_t1, //old time
_t2; //old time
Expand Down
8 changes: 6 additions & 2 deletions SimulationRuntime/cpp/Include/Solver/Kinsol/Kinsol.h
Expand Up @@ -24,7 +24,10 @@ class Kinsol : public IAlgLoopSolver
/// Returns the status of iteration
virtual ITERATIONSTATUS getIterationStatus();
virtual void stepCompleted(double time);
virtual void restoreOldValues();
virtual void restoreNewValues();
int kin_f(N_Vector y, N_Vector fval, void *user_data);

/*will be used with new sundials version
int kin_JacSparse(N_Vector u, N_Vector fu,SlsMat J, void *user_data,N_Vector tmp1, N_Vector tmp2);
int kin_JacDense(long int N, N_Vector u, N_Vector fu,DlsMat J, void *user_data,N_Vector tmp1, N_Vector tmp2);
Expand Down Expand Up @@ -70,8 +73,9 @@ class Kinsol : public IAlgLoopSolver
*_fHelp, ///< Temp - Auxillary variables
*_zeroVec,
*_currentIterate,
*_scale;

*_scale,
*_y_old,
*_y_new;
double
_fnormtol,
_scsteptol;
Expand Down
5 changes: 4 additions & 1 deletion SimulationRuntime/cpp/Include/Solver/Newton/Newton.h
Expand Up @@ -62,7 +62,8 @@ class Newton : public IAlgLoopSolver
/// Returns the status of iteration
virtual ITERATIONSTATUS getIterationStatus();
virtual void stepCompleted(double time);

virtual void restoreOldValues();
virtual void restoreNewValues();
private:
/// Encapsulation of determination of residuals to given unknowns
void calcFunction(const double* y, double* residual);
Expand Down Expand Up @@ -94,6 +95,8 @@ class Newton : public IAlgLoopSolver
*_yHelp, ///< Temp - Auxillary variables
*_fHelp, ///< Temp - Auxillary variables
*_jac, ///< Temp - Jacobian
*_y_old,
*_y_new,
* _zeroVec;
long int *_iHelp;
};
Expand Down
7 changes: 5 additions & 2 deletions SimulationRuntime/cpp/Include/Solver/UmfPack/UmfPack.h
Expand Up @@ -20,14 +20,17 @@ class UmfPack : public IAlgLoopSolver
/// Returns the status of iteration
virtual ITERATIONSTATUS getIterationStatus();
virtual void stepCompleted(double time);

virtual void restoreOldValues();
virtual void restoreNewValues();
private:
ITERATIONSTATUS _iterationStatus;
ILinSolverSettings *_umfpackSettings;
IAlgLoop *_algLoop;

double * _jacd;
double * _rhs;
double * _x;
double * _x,
*_x_old,
*_x_new;
bool _firstuse;
};
24 changes: 14 additions & 10 deletions SimulationRuntime/cpp/Solver/CVode/CVode.cpp
Expand Up @@ -468,6 +468,10 @@ void Cvode::CVodeCore()
if (_idid != CV_SUCCESS)
throw ModelicaSimulationError(SOLVER,"CVodeGetLastStep failed. The cvode mem pointer is NULL");

//set completed step to system and check if terminate was called
if(_continuous_system->stepCompleted(_tCurrent))
_solverStatus = DONE;

//Check if there was at least one output-point within the last solver interval
// -> Write output if true
if (writeOutput)
Expand All @@ -483,9 +487,7 @@ void Cvode::CVodeCore()
}
#endif

//set completed step to system and check if terminate was called
if(_continuous_system->stepCompleted(_tCurrent))
_solverStatus = DONE;


#ifdef RUNTIME_PROFILING
if(MeasureTime::getInstance() != NULL)
Expand Down Expand Up @@ -626,16 +628,18 @@ void Cvode::writeCVodeOutput(const double &time, const double &h, const int &stp
if (_cvodesettings->getDenseOutput())
{
_bWritten = false;
double *oldValues = NULL;
/* double *oldValues = NULL;*/

//We have to find all output-points within the last solver step
while (_tLastWrite + dynamic_cast<ISolverSettings*>(_cvodesettings)->getGlobalSettings()->gethOutput() <= time)
{
if (!_bWritten)
{
//Rescue the calculated derivatives
oldValues = new double[_continuous_system->getDimRHS()];
_continuous_system->getRHS(oldValues);
_continuous_system->restoreOldValues();
////Rescue the calculated derivatives
// oldValues = new double[_continuous_system->getDimRHS()];
// _continuous_system->getRHS(oldValues);

}
_bWritten = true;
_tLastWrite = _tLastWrite + dynamic_cast<ISolverSettings*>(_cvodesettings)->getGlobalSettings()->gethOutput();
Expand All @@ -650,9 +654,9 @@ void Cvode::writeCVodeOutput(const double &time, const double &h, const int &stp
{
_time_system->setTime(time);
_continuous_system->setContinuousStates(_z);
_continuous_system->setRHS(oldValues);
delete[] oldValues;
//_continuous_system->evaluateAll(IContinuous::CONTINUOUS);
_continuous_system->restoreNewValues();
/* _continuous_system->setRHS(oldValues);
delete[] oldValues;*/
}
else if (time == _tEnd && _tLastWrite != time)
{
Expand Down
10 changes: 10 additions & 0 deletions SimulationRuntime/cpp/Solver/Hybrj/Hybrj.cpp
Expand Up @@ -475,4 +475,14 @@ void Hybrj::fcn(const int *n, const double *x, double *fvec, double *fjac, const
}

}
void Hybrj::restoreOldValues()
{

}

void Hybrj::restoreNewValues()
{

}

/** @} */ // end of solverHybrj

0 comments on commit 879063b

Please sign in to comment.