Skip to content

Commit

Permalink
- fixes for testsuite dassl2 and modelicaML
Browse files Browse the repository at this point in the history
- rewrote assert and terminate process for solver 
  - added function checkForAsserts()
  - added asserts for sqrt(<0)
changes in detail:
   - boolean_array.* -- added function for print modelica boolean
   - options.cpp -- fix bug for using options in simulation programm
   - simulations_events.*  -- rewrote searching process
   - simulations_runtime.cpp -- added flag for solver switch  (-s <solvername>)
   - solver_main.cpp -- adjustments to other changes
   - BackendDAEUtil.mo -- check RELATION for discrete Variables
   - SimCode.mo -- added searching for sqrt-asserts, some other adjustments
   - testsuite -- adjustments

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7911 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Feb 14, 2011
1 parent c7c474f commit ceb9d8a
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 134 deletions.
13 changes: 8 additions & 5 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -1407,9 +1407,9 @@ algorithm
BackendDAE.Variables vars,knvars;
DAE.ComponentRef cr;
BackendDAE.VarKind kind;
DAE.Exp e;
DAE.Exp e,e1,e2;
Option<Boolean> blst;
Boolean b;
Boolean b,b1,b2;
Boolean res;
BackendDAE.Var backendVar;

Expand Down Expand Up @@ -1461,9 +1461,12 @@ algorithm
then
((e,false,(vars,knvars,SOME(b))));

case (((e as DAE.RELATION(exp1 = _),(vars,knvars,blst))))
case (((e as DAE.RELATION(exp1 = e1, exp2 = e2),(vars,knvars,blst))))
equation
b = Util.getOptionOrDefault(blst,true);
b1 = isDiscreteExp(e1,vars,knvars);
b2 = isDiscreteExp(e2,vars,knvars);
b = Util.boolOrList({b1,b2});
b = Util.getOptionOrDefault(blst,b);
then ((e,false,(vars,knvars,SOME(b))));
case (((e as DAE.CALL(path = Absyn.IDENT(name = "pre")),(vars,knvars,blst))))
equation
Expand Down Expand Up @@ -5058,7 +5061,7 @@ algorithm
end match;
end traverseBackendDAEExpsSubscript;

protected function traverseBackendDAEExpsEqns "function: traverseBackendDAEExpsEqns
public function traverseBackendDAEExpsEqns "function: traverseBackendDAEExpsEqns
author: Frenkel TUD

Helper for traverseBackendDAEExpsEqns
Expand Down
52 changes: 48 additions & 4 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -1973,7 +1973,7 @@ algorithm
parameterEquations = createParameterEquations(dlow2);
removedEquations = createRemovedEquations(dlow2);
algorithmAndEquationAsserts = createAlgorithmAndEquationAsserts(dlow2);
discreteModelVars = extractDiscreteModelVars(dlow2, mt);
discreteModelVars = extractDiscreteModelVars(dlow2, mt);
discreteModelVars2 = extractDiscreteModelVars2(dlow2, mt);
makefileParams = createMakefileParams(libs);
(delayedExps,maxDelayedExpIndex) = extractDelayedExpressions(dlow2);
Expand Down Expand Up @@ -2850,15 +2850,59 @@ algorithm
algorithmAndEquationAsserts := match (dlow)
local
array<Algorithm.Algorithm> algs;
list<Algorithm.Statement> res;
list<Algorithm.Statement> res,res1;
BackendDAE.EquationArray eqns;

case(BackendDAE.DAE(algorithms=algs))
case(BackendDAE.DAE(algorithms=algs,orderedEqs=eqns))
equation
res = createAlgorithmAndEquationAssertsFromAlgs(arrayList(algs));
res1 = BackendDAEUtil.traverseBackendDAEExpsEqns(eqns,findSqrtCallsforAsserts,{});
res = listAppend(res,res1);
then res;
end match;
end createAlgorithmAndEquationAsserts;

protected function findSqrtCallsforAsserts
input tuple<DAE.Exp, list<Algorithm.Statement>> inTpl;
output tuple<DAE.Exp, list<Algorithm.Statement>> outTpl;
algorithm
outTpl :=
matchcontinue inTpl
local
DAE.Exp exp;
list<Algorithm.Statement> inasserts;
case ((exp,inasserts))
equation
((_,inasserts)) = Expression.traverseExp(exp,findSqrtCallsforAssertsExps,inasserts);
then
((exp,inasserts));
case inTpl then inTpl;
end matchcontinue;
end findSqrtCallsforAsserts;

protected function findSqrtCallsforAssertsExps
input tuple<DAE.Exp, list<Algorithm.Statement>> inTuple;
output tuple<DAE.Exp, list<Algorithm.Statement>> outTuple;
algorithm
outTuple := matchcontinue(inTuple)
local
DAE.Exp e,e1;
String estr;
Algorithm.Statement addAssert;
list<Algorithm.Statement> inasserts;

// special case for time, it is never part of the equation system
case (((e as DAE.CALL(tuple_=false, builtin=true,path=Absyn.IDENT(name="sqrt"), expLst={e1})),inasserts))
equation
estr = "Model error: Argument of sqrt should be >= 0";
addAssert = DAE.STMT_ASSERT(DAE.RELATION(e1,DAE.GREATEREQ(DAE.ET_REAL()),DAE.RCONST(0.0),-1,NONE()),DAE.SCONST(estr),DAE.emptyElementSource);
then ((e, addAssert::inasserts));

case inTuple then inTuple;
end matchcontinue;
end findSqrtCallsforAssertsExps;


protected function createAlgorithmAndEquationAssertsFromAlgs
input list<Algorithm.Algorithm> algs;
output list<Algorithm.Statement> algorithmAndEquationAsserts;
Expand Down Expand Up @@ -2977,7 +3021,7 @@ algorithm
// replace var with cref
vLst1 = BackendEquation.traverseBackendDAEEqns(e,traversingisVarDiscreteCrefFinder2,{});
vLst2 = BackendVariable.traverseBackendDAEVars(v,traversingisVarDiscreteCrefFinder,{});
vLst2 = Util.listUnionOnTrue(vLst1, vLst2, ComponentReference.crefEqual);
vLst2 = Util.listUnionComp(vLst1, vLst2, ComponentReference.crefEqual);
then vLst2;
end match;
end extractDiscreteModelVars2;
Expand Down
41 changes: 37 additions & 4 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -158,6 +158,8 @@ case SIMCODE(__) then

<%functionCheckForDiscreteChanges(discreteModelVars2)%>

<%functionAssertsforCheck(algorithmAndEquationAsserts)%>

<%generateLinearMatrixes(JacobianMatrixes)%>

<%functionlinearmodel(modelInfo)%>
Expand Down Expand Up @@ -1794,21 +1796,53 @@ end functionOnlyZeroCrossing;
template functionCheckForDiscreteChanges(list<ComponentRef> discreteModelVars)
"Generates function in simulation file."
::=

let changediscreteVars = (discreteModelVars |> var => match var case CREF_QUAL(__) case CREF_IDENT(__) then
'if (change(<%cref(var)%>)) { if (sim_verbose) { cout << "Discrete Var <%crefStr(var)%> : " << (<%crefType(var)%>) pre(<%cref(var)%>) << " to " << (<%crefType(var)%>) <%cref(var)%> << endl;} needToIterate=1; }'
;separator="\n")
<<
int checkForDiscreteChanges()
{
int needToIterate = 0;
<%discreteModelVars |> var as CREF_IDENT(__) =>
'if (change(<%cref(var)%>)) { if (sim_verbose) { cout << "Discrete Var <%crefStr(var)%> : " << (<%extType(identType)%>) pre(<%cref(var)%>) << " to " << (<%extType(identType)%>) <%cref(var)%> << endl;} needToIterate=1; }'
;separator="\n"%>
<%changediscreteVars%>
return needToIterate;
}
>>
// if (sim_verbose) { cout << "Discrete Var <%crefStr(var)%> : " << (double) pre(<%cref(var)%>) << " to " << (double) <%cref(var)%> << endl;}
end functionCheckForDiscreteChanges;

template crefType(ComponentRef cr)
"Like cref but with cast if type is integer."
::=
match cr
case CREF_IDENT(__) then '<%extType(identType)%>'
case CREF_QUAL(__) then '<%crefType(componentRef)%>'
else "crefType:ERROR"
end crefType;


template functionAssertsforCheck(list<DAE.Statement> algorithmAndEquationAsserts)
"Generates function in simulation file."
::=
let &varDecls = buffer "" /*BUFD*/
let algAndEqAssertsPart = (algorithmAndEquationAsserts |> stmt =>
algStatement(stmt, contextSimulation2Discrete, &varDecls /*BUFD*/)
;separator="\n")
<<
/* for continuous time variables */
int checkForAsserts()
{
<%varDecls%>
<%algAndEqAssertsPart%>
return 0;
}
>>
end functionAssertsforCheck;

template functionJac(list<SimEqSystem> JacEquations, list<SimVar> JacVars, String MatrixName)
"Generates function in simulation file."
::=
Expand Down Expand Up @@ -4593,7 +4627,6 @@ template scalarLhsCref(Exp ecr, Context context, Text &preExp, Text &varDecls)
"ONLY_IDENT_OR_QUAL_CREF_SUPPORTED_SLHS"
end scalarLhsCref;


template rhsCref(ComponentRef cr, ExpType ty)
"Like cref but with cast if type is integer."
::=
Expand Down
5 changes: 5 additions & 0 deletions c_runtime/boolean_array.c
Expand Up @@ -235,6 +235,11 @@ void print_boolean_array(boolean_array_t *source)
}
}

char print_boolean(m_boolean value)
{
return value ? 'T' : 'F';
}

void put_boolean_element(m_boolean value, int i1, boolean_array_t *dest)
{
/* Assert that dest has correct dimension */
Expand Down
1 change: 1 addition & 0 deletions c_runtime/boolean_array.h
Expand Up @@ -91,6 +91,7 @@ void put_boolean_matrix_element(m_boolean value, int r, int c, boolean_array_t*

void print_boolean_matrix(boolean_array_t* source);
void print_boolean_array(boolean_array_t* source);
char print_boolean(m_boolean value);
/*
a[1:3] := b;
Expand Down
2 changes: 1 addition & 1 deletion c_runtime/options.cpp
Expand Up @@ -60,7 +60,7 @@ const string* getFlagValue(const char *option, int argc, char **argv)
for (int i=0; i<argc;i++) {
string tmpStr=string(argv[i]);
if (("-"+string(option))==string(argv[i])) {
if (argc >= i+1) {
if (argc > i+1) {
return new string(argv[i+1]);
}
}
Expand Down

0 comments on commit ceb9d8a

Please sign in to comment.