Skip to content

Commit

Permalink
Remove the third argument of the DIVISION macro, passing equation ind…
Browse files Browse the repository at this point in the history
…ex instead (for debugging)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18998 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 6, 2014
1 parent 391cfba commit 43d9787
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 85 deletions.
34 changes: 3 additions & 31 deletions Compiler/BackEnd/SimCodeUtil.mo
Expand Up @@ -9990,55 +9990,27 @@ algorithm
false = Expression.isZero(e2);
then ((e, source ));
case( (e as DAE.BINARY(exp1 = e1, operator = DAE.DIV(ty), exp2 = e2), source))
equation
se = generateDivExpErrorMsg(e, e2, source);
then ((DAE.CALL(Absyn.IDENT("DIVISION"), {e1, e2, DAE.SCONST(se)}, DAE.CALL_ATTR(ty, false, true, false, DAE.NO_INLINE(), DAE.NO_TAIL())), source ));
/*
case( (e as DAE.BINARY(exp1 = e1, operator = DAE.DIV_ARR(ty), exp2 = e2), dlowmode as (dlow, _)))
then ((e, dlowmode ));
*/
then ((DAE.CALL(Absyn.IDENT("DIVISION"), {e1, e2}, DAE.CALL_ATTR(ty, false, true, false, DAE.NO_INLINE(), DAE.NO_TAIL())), source ));

case( (e as DAE.BINARY(exp1 = e1, operator = DAE.DIV_ARRAY_SCALAR(ty), exp2 = e2), source))
equation
true = Expression.isConst(e2);
false = Expression.isZero(e2);
then ((e, source ));
case( (e as DAE.BINARY(exp1 = e1, operator = DAE.DIV_ARRAY_SCALAR(ty), exp2 = e2), source))
equation
se = generateDivExpErrorMsg(e, e2, source);
then ((DAE.CALL(Absyn.IDENT("DIVISION_ARRAY_SCALAR"), {e1, e2, DAE.SCONST(se)}, DAE.CALL_ATTR(ty, false, true, false, DAE.NO_INLINE(), DAE.NO_TAIL())), source ));
then ((DAE.CALL(Absyn.IDENT("DIVISION_ARRAY_SCALAR"), {e1, e2}, DAE.CALL_ATTR(ty, false, true, false, DAE.NO_INLINE(), DAE.NO_TAIL())), source ));

case( (e as DAE.BINARY(exp1 = e1, operator = DAE.DIV_SCALAR_ARRAY(ty), exp2 = e2), source))
equation
true = Expression.isConst(e2);
false = Expression.isZero(e2);
then ((e, source ));
case( (e as DAE.BINARY(exp1 = e1, operator = DAE.DIV_SCALAR_ARRAY(ty), exp2 = e2), source))
equation
se = generateDivExpErrorMsg(e, e2, source);
then ((DAE.CALL(Absyn.IDENT("DIVISION_SCALAR_ARRAY"), {e1, e2, DAE.SCONST(se)}, DAE.CALL_ATTR(ty, false, true, false, DAE.NO_INLINE(), DAE.NO_TAIL())), source));
then ((DAE.CALL(Absyn.IDENT("DIVISION_SCALAR_ARRAY"), {e1, e2}, DAE.CALL_ATTR(ty, false, true, false, DAE.NO_INLINE(), DAE.NO_TAIL())), source));
case _ then (inExp);
end matchcontinue;
end traversingDivExpFinder;

protected function generateDivExpErrorMsg "author: Frenkel TUD 2010-02. varOrigCref"
input DAE.Exp inExp;
input DAE.Exp inDivisor;
input DAE.ElementSource source;
output String outString;
protected
String se, se2, s, fileName;
Integer lns;
algorithm
/* Come on... This is just silly. Why not store an Absyn.Info in the OP_DIVISION()? */
se := ExpressionDump.printExp2Str(inExp, "\"", SOME((BackendDump.componentRef_DIVISION_String, 0)), SOME(BackendDump.printCallFunction2StrDIVISION));
se2 := ExpressionDump.printExp2Str(inDivisor, "\"", SOME((BackendDump.componentRef_DIVISION_String, 0)), SOME(BackendDump.printCallFunction2StrDIVISION));
Absyn.INFO(fileName=fileName, lineNumberStart=lns) := DAEUtil.getElementSourceFileInfo(source);
fileName := Util.testsuiteFriendly(fileName);
s := intString(lns);
outString := stringAppendList({se, " because ", se2, " == 0: File: ", fileName, " Line: ", s});
end generateDivExpErrorMsg;

protected function addDivExpErrorMsgtosimJac "helper for addDivExpErrorMsgtoSimEqSystem."
input tuple<Integer, Integer, SimCode.SimEqSystem> inJac;
output tuple<Integer, Integer, SimCode.SimEqSystem> outJac;
Expand Down
35 changes: 17 additions & 18 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -1463,13 +1463,15 @@ template functionSetupLinearSystemsTemp(list<SimEqSystem> allEquations)
<<
void setLinearMatrixA<%index%>(void *inData, void *systemData)
{
const int equationIndexes[2] = {1,<%index%>};
DATA* data = (DATA*) inData;
LINEAR_SYSTEM_DATA* linearSystemData = (LINEAR_SYSTEM_DATA*) systemData;
<%varDecls%>
<%MatrixA%>
}
void setLinearVectorb<%index%>(void *inData, void *systemData)
{
const int equationIndexes[2] = {1,<%index%>};
DATA* data = (DATA*) inData;
LINEAR_SYSTEM_DATA* linearSystemData = (LINEAR_SYSTEM_DATA*) systemData;
<%varDecls2%>
Expand Down Expand Up @@ -1590,6 +1592,7 @@ template functionNonLinearResiduals(list<SimEqSystem> allEquations, String model
void residualFunc<%index%>(void* dataIn, double* xloc, double* res, integer* iflag)
{
DATA* data = (DATA*) dataIn;
const int equationIndexes[2] = {1,<%index%>};
<%varDecls%>
#ifdef _OMC_MEASURE_TIME
SIM_PROF_ADD_NCALL_EQ(modelInfoXmlGetEquation(&data->modelData.modelDataXml,<%index%>).profileBlockIndex,1);
Expand Down Expand Up @@ -1775,6 +1778,7 @@ template functionInitialResidual(list<SimEqSystem> residualEquations, String mod
<%tmp%>
int <%symbolName(modelNamePrefix,"initial_residual")%>(DATA *data, double *initialResiduals)
{
const int *equationIndexes = NULL;
int i = 0;
<%varDecls%>
Expand Down Expand Up @@ -2591,6 +2595,7 @@ template functionODE(list<list<SimEqSystem>> derivativEquations, Text method, Op
*/
int <%symbolName(modelNamePrefix,"functionODE_inline")%>(DATA* data, double stepSize)
{
const int *equationIndexes = NULL;
<%varDecls2%>
data->simulationInfo.discreteCall = 0;
begin_inline();
Expand Down Expand Up @@ -3418,6 +3423,7 @@ template equation_(SimEqSystem eq, Context context, Text &varDecls /*BUFP*/, Tex
*/
void <%symbolName(modelNamePrefix,"eqFunction")%>_<%ix%>(DATA *data)
{
const int equationIndexes[2] = {1,<%ix%>};
<%&varD%>
<%x%>
}
Expand Down Expand Up @@ -8390,37 +8396,30 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
'<%var2%>'

case CALL(path=IDENT(name="DIVISION"),
expLst={e1, e2, DAE.SCONST(string=string)}) then
expLst={e1, e2}) then
let var1 = daeExp(e1, context, &preExp, &varDecls)
let var2 = daeExp(e2, context, &preExp, &varDecls)
let var3 = Util.escapeModelicaStringToCString(string)
'DIVISION(<%var1%>,<%var2%>,"<%var3%>")'
let var3 = Util.escapeModelicaStringToCString(printExpStr(e2))
(match context
case FUNCTION_CONTEXT(__) then
'DIVISION(<%var1%>,<%var2%>,"<%var3%>")'
else
'DIVISION_SIM(<%var1%>,<%var2%>,"<%var3%>",equationIndexes)'
)

case CALL(attr=CALL_ATTR(ty=ty),
path=IDENT(name="DIVISION_ARRAY_SCALAR"),
expLst={e1, e2, e3 as DAE.SCONST(string=string)}) then
expLst={e1, e2}) then
let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then "integer_array"
case T_ARRAY(ty=T_ENUMERATION(__)) then "integer_array"
else "real_array"
let var = tempDecl(type, &varDecls)
let var1 = daeExp(e1, context, &preExp, &varDecls)
let var2 = daeExp(e2, context, &preExp, &varDecls)
let var3 = Util.escapeModelicaStringToCString(string)
let &preExp += 'division_alloc_<%type%>_scalar(&<%var1%>, <%var2%>, &<%var%>, <%var3%>);<%\n%>'
let var3 = Util.escapeModelicaStringToCString(printExpStr(e2))
let &preExp += 'division_alloc_<%type%>_scalar(&<%var1%>, <%var2%>, &<%var%>, "<%var3%>");<%\n%>'
'<%var%>'

case CALL(attr=CALL_ATTR(ty=ty),
path=IDENT(name="DIVISION_ARRAY_SCALAR"),
expLst={e1, e2, e3 as SHARED_LITERAL(__)}) then
let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then "integer_array"
case T_ARRAY(ty=T_ENUMERATION(__)) then "integer_array"
else "real_array"
let var = tempDecl(type, &varDecls)
let var1 = daeExp(e1, context, &preExp, &varDecls)
let var2 = daeExp(e2, context, &preExp, &varDecls)
let var3 = daeExp(e3, context, &preExp, &varDecls)
let &preExp += 'division_alloc_<%type%>_scalar(&<%var1%>, <%var2%>, &<%var%>, <%var3%>);<%\n%>'
'<%var%>'
case exp as CALL(attr=CALL_ATTR(ty=ty), path=IDENT(name="DIVISION_ARRAY_SCALAR")) then
error(sourceInfo(),'Code generation does not support <%printExpStr(exp)%>')

Expand Down
23 changes: 4 additions & 19 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -6191,42 +6191,27 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
expLst={e1, e2}) then
let var1 = daeExp(e1, context, &preExp, &varDecls,simCode)
let var2 = daeExp(e2, context, &preExp, &varDecls,simCode)

'division(<%var1%>,<%var2%>,"<%var1%>/<%var2%> because <%var2%> == 0")'

case CALL(path=IDENT(name="DIVISION"),
expLst={e1, e2, DAE.SCONST(string=string)}) then
let var1 = daeExp(e1, context, &preExp, &varDecls,simCode)
let var2 = daeExp(e2, context, &preExp, &varDecls,simCode)
let var3 = Util.escapeModelicaStringToCString(string)
let var3 = Util.escapeModelicaStringToCString(printExpStr(e2))
'division(<%var1%>,<%var2%>,"<%var3%>")'

case CALL(path=IDENT(name="sign"),
expLst={e1}) then
let var1 = daeExp(e1, context, &preExp, &varDecls,simCode)
'sgn(<%var1%>)'
case CALL(path=IDENT(name="DIVISION"))
then
let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode)%>' ;separator=", ")
// let typeStr = expTypeShort(attr.ty )
let typeStr ="double"
let retVar = tempDecl(typeStr, &varDecls /*BUFD*/)
let &preExp += '<%retVar%> = division(<%argStr%>);<%\n%>'
'<%retVar%>'

case CALL(attr=CALL_ATTR(ty=ty as T_ARRAY(dims=dims)),
path=IDENT(name="DIVISION_ARRAY_SCALAR"),
expLst={e1, e2, e3 as SHARED_LITERAL(__)}) then
expLst={e1, e2}) then
let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then "int"
case T_ARRAY(ty=T_ENUMERATION(__)) then "int"
else "double"

let var = tempDecl('multi_array<<%type%>,<%listLength(dims)%>>', &varDecls /*BUFD*/)
let var1 = daeExp(e1, context, &preExp, &varDecls,simCode)
let var2 = daeExp(e2, context, &preExp, &varDecls,simCode)
let var3 = daeExp(e3, context, &preExp, &varDecls,simCode)
let var3 = Util.escapeModelicaStringToCString(printExpStr(e2))
let &preExp += 'assign_array(<%var%>,divide_array<<%type%>,<%listLength(dims)%>>(<%var1%>, <%var2%>));<%\n%>'
//let &preExp += 'division_alloc_<%type%>_scalar(&<%var1%>, <%var2%>, &<%var%>, <%var3%>);<%\n%>'
//let &preExp += 'division_alloc_<%type%>_scalar(&<%var1%>, <%var2%>, &<%var%>, "<%var3%>");<%\n%>'
'<%var%>'


Expand Down
17 changes: 0 additions & 17 deletions SimulationRuntime/c/simulation/simulation_runtime.cpp
Expand Up @@ -90,21 +90,6 @@ using namespace std;

static int interactiveSimulation = 0; /* This variable signals if an simulation session is interactive or non-interactive (by default) */

/* This variable is used to get the step size value during the simulation. */
double stepSize = 0.0;

double getSimulationStepSize()
{
return stepSize;
}

void printSimulationStepSize(double in_stepSize, double time)
{
fprintf(stderr, "in_stepSize=%f, time=%f\n", in_stepSize, time);
}

/* const char* version = "20110520_1120"; */

#ifndef NO_INTERACTIVE_DEPENDENCY
Socket sim_communication_port;
static int sim_communication_port_open = 0;
Expand Down Expand Up @@ -799,8 +784,6 @@ int initRuntimeAndSimulation(int argc, char**argv, DATA *data)
data->simulationInfo.lsMethod = getlinearSolverMethod(argc, argv);

read_input_xml(&(data->modelData), &(data->simulationInfo));
/* set the global stepsize variable */
stepSize = data->simulationInfo.stepSize;

/* allocate memory for mixed system solvers */
allocatemixedSystem(data);
Expand Down
10 changes: 10 additions & 0 deletions SimulationRuntime/c/util/division.c
Expand Up @@ -36,6 +36,16 @@
#include "division.h"
#include "omc_error.h"

modelica_real division_error_equation_time(modelica_real b, const char *msg, const int *indexes, modelica_real time, modelica_boolean noThrow)
{
if(noThrow){
warningStreamPrintWithEquationIndexes(LOG_UTIL, 0, indexes, "solver will try to handle division by zero at time %.16g: %s", time, msg);
} else {
throwStreamPrintWithEquationIndexes(indexes, "division by zero at time %.16g, divisor: %s", time, msg);
}
return b;
}

modelica_real division_error_time(modelica_real b, const char* division_str, modelica_real time, const char* file, long line, modelica_boolean noThrow)
{
if(noThrow){
Expand Down
4 changes: 4 additions & 0 deletions SimulationRuntime/c/util/division.h
Expand Up @@ -41,8 +41,12 @@
#else
#define DIVISION(a,b,c) (((b) != 0) ? ((a) / (b)) : ((a==0)?a:((a) / division_error_time(b, c, time, __FILE__, __LINE__,data->simulationInfo.noThrowDivZero?1:0))))
#endif

#define DIVISION_SIM(a,b,msg,equation) (((b) != 0) ? ((a) / (b)) : ((a==0)?a:((a) / division_error_equation_time(b, msg, equation, time, data->simulationInfo.noThrowDivZero?1:0))))

#define DIVISIONNOTIME(a,b,c) (((b) != 0) ? ((a) / (b)) : ((a==0)?a:((a) / division_error(b, c, __FILE__, __LINE__))))

modelica_real division_error_equation_time(modelica_real b, const char *division_str, const int *indexes, modelica_real time, modelica_boolean noThrow);
modelica_real division_error_time(modelica_real b, const char* division_str, modelica_real time, const char* file, long line, modelica_boolean noThrow);
modelica_real division_error(modelica_real b, const char* division_str, const char* file, long line);
modelica_real isnan_error(modelica_real b, const char* division_str, const char* file, long line);
Expand Down
10 changes: 10 additions & 0 deletions SimulationRuntime/c/util/omc_error.c
Expand Up @@ -353,3 +353,13 @@ void throwStreamPrint(const char *format, ...)
messageFunction(LOG_TYPE_DEBUG, LOG_ASSERT, 0, logBuffer, 0, NULL);
longjmp(globalJmpbuf, 1);
}

void throwStreamPrintWithEquationIndexes(const int *indexes, const char *format, ...)
{
char logBuffer[SIZE_LOG_BUFFER];
va_list args;
va_start(args, format);
vsnprintf(logBuffer, SIZE_LOG_BUFFER, format, args);
messageFunction(LOG_TYPE_DEBUG, LOG_ASSERT, 0, logBuffer, 0, indexes);
longjmp(globalJmpbuf, 1);
}
1 change: 1 addition & 0 deletions SimulationRuntime/c/util/omc_error.h
Expand Up @@ -157,6 +157,7 @@ extern void warningStreamPrintWithEquationIndexes(int stream, int indentNext, co
extern void errorStreamPrint(int stream, int indentNext, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
extern void assertStreamPrint(int cond, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
extern void throwStreamPrint(const char *format, ...) __attribute__ ((format (printf, 1, 2), noreturn));
extern void throwStreamPrintWithEquationIndexes(const int *indexes, const char *format, ...) __attribute__ ((format (printf, 2, 3), noreturn));

#ifdef USE_DEBUG_OUTPUT
void debugStreamPrint(int stream, int indentNext, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
Expand Down

0 comments on commit 43d9787

Please sign in to comment.