Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 0f8a7d1

Browse files
niklworsOpenModelica-Hudson
authored andcommitted
added assert for sqrt function calls
fix typo Belonging to [master]: - #2154
1 parent c97c5bb commit 0f8a7d1

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

Compiler/Template/CodegenCppCommon.tpl

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,10 +1847,21 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
18471847
case CALL(path=IDENT(name="sqrt"),
18481848
expLst={e1},attr=attr as CALL_ATTR(__)) then
18491849
let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' ;separator=", ")
1850-
let typeStr = expTypeShort(attr.ty )
1851-
let retVar = tempDecl(typeStr, &varDecls /*BUFD*/)
1852-
let &preExp += '<%retVar%> = std::sqrt(<%argStr%>);<%\n%>'
1853-
'<%retVar%>'
1850+
(if isPositiveOrZero(e1) then
1851+
let typeStr = expTypeShort(attr.ty )
1852+
let retVar = tempDecl(typeStr, &varDecls /*BUFD*/)
1853+
let &preExp += '<%retVar%> = std::sqrt(<%argStr%>);<%\n%>'
1854+
1855+
'<%retVar%>'
1856+
else
1857+
let tmp = tempDecl(expTypeFromExpModelica(e1), &varDecls)
1858+
let cstr = ExpressionDumpTpl.dumpExp(e1,"\"")
1859+
let &preExp +=
1860+
<<
1861+
<%tmp%> = <%argStr%>;
1862+
<%assertCommonVar('<%tmp%> >= 0.0', '"Model error: Argument of sqrt(<%Util.escapeModelicaStringToCString(cstr)%>) should be >= 0"', context, &varDecls, dummyInfo)%>
1863+
>>
1864+
'sqrt(<%tmp%>)')
18541865

18551866
// built-in mathematical functions
18561867
case CALL(path=IDENT(name="sin"), expLst={e1})
@@ -2974,6 +2985,39 @@ template functionClosure(String funcName, String closureArgs, Type t, Type t_ori
29742985
'<%closureName%>(<%functionsObject%><%closureArgs%>)'
29752986
end functionClosure;
29762987

2988+
2989+
template assertCommonVar(Text condVar, Text msgVar, Context context, Text &varDecls, builtin.SourceInfo info)
2990+
::=
2991+
match context
2992+
case FUNCTION_CONTEXT(__) then
2993+
<<
2994+
if(!(<%condVar%>))
2995+
{
2996+
2997+
throw ModelicaSimulationError(MODEL_EQ_SYSTEM, <%msgVar%>);
2998+
}
2999+
>>
3000+
// OpenCL doesn't have support for variadic args. So message should be just a single string.
3001+
case PARALLEL_FUNCTION_CONTEXT(__) then
3002+
<<
3003+
if(!(<%condVar%>))
3004+
{
3005+
3006+
throw ModelicaSimulationError(MODEL_EQ_SYSTEM, "Common assertion failed");
3007+
3008+
}
3009+
>>
3010+
else
3011+
<<
3012+
if(!(<%condVar%>))
3013+
{
3014+
//string error_msg = "The following assertion has been violated %sat time %f", initial() ? "during initialization " : "", data->localData[0]->timeValue);
3015+
throw ModelicaSimulationError(MODEL_EQ_SYSTEM,<%msgVar%>);
3016+
}
3017+
>>
3018+
end assertCommonVar;
3019+
3020+
29773021
template expTypeUnboxed(Type t)
29783022
"Returns the actual type in the box"
29793023
::=

0 commit comments

Comments
 (0)