Skip to content

Commit

Permalink
Fix for division during internalization in cpp runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
niklwors authored and OpenModelica-Hudson committed Nov 4, 2016
1 parent 96f1677 commit 7359ea2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
16 changes: 13 additions & 3 deletions Compiler/Template/CodegenCppCommon.tpl
Expand Up @@ -1738,12 +1738,22 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
else
'_terminal'

case CALL(path=IDENT(name="DIVISION"),
expLst={e1, e2}) then
case CALL(path=IDENT(name="DIVISION"), expLst={e1, e2}) then
let var1 = daeExp(e1, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let var2 = daeExp(e2, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)
let var3 = Util.escapeModelicaStringToCString(ExpressionDumpTpl.dumpExp(e2,"\""))
'division(<%var1%>,<%var2%>,"<%var3%>")'
match context
case ALGLOOP_CONTEXT(genInitialisation = false)
then
<<
division(<%var1%>,<%var2%>,!_system->_initial,"<%var3%>")
>>
else
<<
division(<%var1%>,<%var2%>,!_initial,"<%var3%>")
>>
end match


case CALL(path=IDENT(name="sign"),
expLst={e1}) then
Expand Down
14 changes: 10 additions & 4 deletions SimulationRuntime/cpp/Core/Math/Functions.cpp
Expand Up @@ -9,7 +9,7 @@

#include <Core/Utils/numeric/bindings/ublas.hpp>
#include <Core/Utils/numeric/utils.h>

//#include <Core/Utils/extension/logger.hpp>
namespace bindings = boost::numeric::bindings;

/* Matrixes using column major order (as in Fortran) */
Expand All @@ -33,14 +33,20 @@ namespace bindings = boost::numeric::bindings;
#define min(a,b) ((a > b) ? (b) : (a))
#endif

double division (const double &a,const double &b, const char* text)
double division (const double &a,const double &b, bool throwEx,const char* text)
{
if(b != 0)
return a/b ;
else
{
std::string error_msg = "Division by zero: ";
throw ModelicaSimulationError(UTILITY,error_msg+string(text));
std::string error_msg = "Division by zero: ";
if(throwEx)
throw ModelicaSimulationError(UTILITY,error_msg+string(text));
else
{
// LOGGER_WRITE("Division: Solver will try to handle division by zero for" + string(text), LC_INIT, LL_DEBUG);
return a;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/cpp/Include/Core/Math/Functions.h
Expand Up @@ -38,7 +38,7 @@ inline static int sgn (const double &c)
}

/// Definition of Signum function
double BOOST_EXTENSION_EXPORT_DECL division (const double &a,const double &b, const char * text);
double BOOST_EXTENSION_EXPORT_DECL division (const double &a,const double &b, bool throwEx,const char * text);


void BOOST_EXTENSION_EXPORT_DECL getSparseMatrixData(sparsematrix_t& A, double** px);
Expand Down

0 comments on commit 7359ea2

Please sign in to comment.