Skip to content

Commit

Permalink
- Revamped +simCodeTarget=Dump. It now create $MODEL_dump.xml. Which,…
Browse files Browse the repository at this point in the history
… together with OPENMODELICAHOME/share/scripts/simcodedump.xsl produces a pretty html report. Will be improved further.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14444 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Dec 18, 2012
1 parent 090dabf commit 178a454
Show file tree
Hide file tree
Showing 6 changed files with 431 additions and 95 deletions.
17 changes: 13 additions & 4 deletions Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -6383,11 +6383,20 @@ public function addSymbolicTransformationSolve
input DAE.Exp exp;
input list<DAE.Statement> asserts;
output DAE.ElementSource outSource;
protected
list<DAE.Exp> assertExps;
algorithm
assertExps := List.map(asserts,Algorithm.getAssertCond);
outSource := condAddSymbolicTransformation(add,source,DAE.SOLVE(cr,exp1,exp2,exp,assertExps));
outSource := match (add,source,cr,exp1,exp2,exp,asserts)
local
list<DAE.Exp> assertExps;
DAE.SymbolicOperation op,op1,op2;
case (false,_,_,_,_,_,_) then source;
case (_,_,_,_,_,_,_)
equation
assertExps = List.map(asserts,Algorithm.getAssertCond);
op1 = DAE.SOLVE(cr,exp1,exp2,exp,assertExps);
op2 = DAE.SOLVED(cr,exp2) "If it was already on solved form";
op = Util.if_(Expression.expEqual(exp2,exp),op2,op1);
then addSymbolicTransformation(source,op);
end match;
end addSymbolicTransformationSolve;

public function getSymbolicTransformations
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/AbsynDumpTpl.mo
Expand Up @@ -4,7 +4,7 @@ encapsulated package AbsynDumpTpl
package: AbsynDumpTpl
description: Generated by Susan.
$Id$
$Id: TplCodegen.mo 9216 2011-05-31 10:52:47Z sjoelund.se $
"

public import Tpl;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/NFInstDumpTpl.mo
Expand Up @@ -4,7 +4,7 @@ encapsulated package NFInstDumpTpl
package: NFInstDumpTpl
description: Generated by Susan.
$Id$
$Id: TplCodegen.mo 9216 2011-05-31 10:52:47Z sjoelund.se $
"

public import Tpl;
Expand Down
255 changes: 166 additions & 89 deletions Compiler/Template/SimCodeDump.tpl
Expand Up @@ -7,28 +7,39 @@ template dumpSimCode(SimCode code)
::=
match code
case sc as SIMCODE(modelInfo=mi as MODELINFO(vars=vars as SIMVARS(__))) then
<<
SimCode: <%dotPath(mi.name)%>
<%dumpVars(vars.stateVars)%>
<%dumpVars(vars.derivativeVars)%>
<%dumpVars(vars.algVars)%>
<%dumpVars(vars.intAlgVars)%>
<%dumpVars(vars.boolAlgVars)%>
<%dumpVars(vars.inputVars)%>
<%dumpVars(vars.outputVars)%>
<%dumpVars(vars.aliasVars)%>
<%dumpVars(vars.intAliasVars)%>
<%dumpVars(vars.boolAliasVars)%>
<%dumpVars(vars.paramVars)%>
<%dumpVars(vars.intParamVars)%>
<%dumpVars(vars.boolParamVars)%>
<%dumpVars(vars.stringAlgVars)%>
<%dumpVars(vars.stringParamVars)%>
<%dumpVars(vars.stringAliasVars)%>
<%dumpVars(vars.extObjVars)%>
<%dumpVars(vars.constVars)%>
<%dumpEqs(sc.allEquations)%>
let name = dotPath(mi.name)
let res = <<
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="application/xml" href="simcodedump.xsl"?>
<simcodedump model="<%name%>">
<variables>
<%dumpVars(vars.stateVars)%>
<%dumpVars(vars.derivativeVars)%>
<%dumpVars(vars.algVars)%>
<%dumpVars(vars.intAlgVars)%>
<%dumpVars(vars.boolAlgVars)%>
<%dumpVars(vars.inputVars)%>
<%dumpVars(vars.outputVars)%>
<%dumpVars(vars.aliasVars)%>
<%dumpVars(vars.intAliasVars)%>
<%dumpVars(vars.boolAliasVars)%>
<%dumpVars(vars.paramVars)%>
<%dumpVars(vars.intParamVars)%>
<%dumpVars(vars.boolParamVars)%>
<%dumpVars(vars.stringAlgVars)%>
<%dumpVars(vars.stringParamVars)%>
<%dumpVars(vars.stringAliasVars)%>
<%dumpVars(vars.extObjVars)%>
<%dumpVars(vars.constVars)%>
</variables>
<equations>
<%/* dumpEqs(listAppend(listAppend(sc.initialEquations,sc.parameterEquations),sc.allEquations)) */
dumpEqs(sc.allEquations)%>
</equations>
</simcodedump><%\n%>
>>
let() = textFile(res,'<%name%>_dump.xml')
'Result dumped to <%name%>_dump.xml'
end dumpSimCode;

template dumpVarsShort(list<SimVar> vars)
Expand All @@ -49,73 +60,110 @@ template dumpVars(list<SimVar> vars)
::=
vars |> v as SIMVAR(__) =>
<<
<%crefStr(v.name)%> <%v.comment%> <%dumpAlias(v.aliasvar)%>
<variable name="<%crefStr(v.name)%>" comment="<%escapeModelicaStringToXmlString(v.comment)%>">
<%dumpAlias(v.aliasvar)%>
<%dumpElementSource(v.source)%>
<%\n%>
</variable><%\n%>
>>
end dumpVars;

template dumpAlias(AliasVariable alias)
::=
match alias
case ALIAS(__) then 'alias of <%crefStr(varName)%>'
case NEGATEDALIAS(__) then 'alias of -<%crefStr(varName)%>'
case ALIAS(__) then '<alias><%crefStr(varName)%></alias>'
case NEGATEDALIAS(__) then ' <alias negated="true"><%crefStr(varName)%></alias>'
end dumpAlias;

template dumpEqs(list<SimEqSystem> eqs)
::= eqs |> eq hasindex i0 =>
match eq
<<
<%match eq
case SES_RESIDUAL(__)
case SES_SIMPLE_ASSIGN(__)
case SES_ARRAY_CALL_ASSIGN(__)
case SES_ALGORITHM(__)
case SES_LINEAR(__)
case SES_NONLINEAR(__)
case SES_MIXED(__)
case SES_WHEN(__) then '<equation index="<%index%>">'
else error(sourceInfo(), "dumpEqs: Unknown equation")
%>
<%match eq
case e as SES_RESIDUAL(__) then
<<
residual: <%printExpStr(e.exp)%>;
<residual><%printExpStrEscaped(e.exp)%></residual>
<%dumpElementSource(e.source)%><%\n%>
>>
case e as SES_SIMPLE_ASSIGN(__) then
<<
eq <%index%>: <%crefStr(e.cref)%> = <%printExpStr(e.exp)%>;
<assign>
<lhs><%crefStr(e.cref)%></lhs>
<rhs><%printExpStrEscaped(e.exp)%></rhs>
</assign>
<%dumpElementSource(e.source)%><%\n%>
>>
case e as SES_ARRAY_CALL_ASSIGN(__) then
<<
array assign: <%crefStr(e.componentRef)%> = <%printExpStr(e.exp)%>;
<assign type="array">
<lhs><%crefStr(e.componentRef)%></lhs>
<rhs><%printExpStrEscaped(e.exp)%></rhs>
</assign>
<%dumpElementSource(e.source)%><%\n%>
>>
case e as SES_ALGORITHM(statements={}) then 'empty algorithm<%\n%>'
case e as SES_ALGORITHM(__)
then (e.statements |> stmt =>
<<
statement: <%ppStmtStr(stmt,2)%>
<statement>
<%escapeModelicaStringToXmlString(ppStmtStr(stmt,2))%>
</statement>
<%dumpElementSource(getStatementSource(stmt))%><%\n%>
>>
)
case e as SES_LINEAR(__) then
<<
linear: <%e.vars |> var => "var" ; separator = "," %>
<%beqs |> exp => printExpStr(exp) ; separator = "," %><%\n%>
<%simJac |> (i1,i2,eq) => '<%i1%>,<%i2%>: <%dumpEqs(fill(eq,1))%>' ; separator = "\n" %><%\n%>
<linear>
<%e.vars |> SIMVAR(name=cr) => '<var><%crefStr(cr)%></var>' ; separator = "\n" %>
<row>
<%beqs |> exp => '<cell><%printExpStrEscaped(exp)%></cell>' ; separator = "\n" %><%\n%>
</row>
<matrix>
<%simJac |> (i1,i2,eq) => '<cell row="<%i1%>" col="<%i2%>"><%dumpEqs(fill(eq,1))%></cell>' ; separator = "\n" %><%\n%>
</matrix>
</linear>
>>
case e as SES_NONLINEAR(__) then
<<
nonlinear: <%e.crefs |> cr => crefStr(cr) ; separator = "," %>
<nonlinear indexNonlinear="<%indexNonLinear%>">
<%e.crefs |> cr => '<var><%crefStr(cr)%></var>' ; separator = "\n" %>
<%dumpEqs(e.eqs)%><%\n%>
</nonlinear>
>>
case e as SES_MIXED(__) then
<<
mixed system:
continuous part:
<mixed>
<continuous>
<%dumpEqs(fill(e.cont,1))%>
discrete vars:
<%e.discVars |> var => "var" ; separator = ","%>
discrete parts:
<%dumpEqs(e.discEqs)%><%\n%>
</continuous>
<%e.discVars |> SIMVAR(name=cr) => '<var><%crefStr(cr)%></var>' ; separator = ","%>
<discrete>
<%dumpEqs(e.discEqs)%>
</discrete>
</mixed>
>>
case e as SES_WHEN(__) then
<<
when (#<%i0%>): <%conditions |> (cond,_) => printExpStr(cond) ; separator="," %>
<%crefStr(e.left)%> = <%printExpStr(e.right)%>
<%dumpElementSource(e.source)%><%\n%>
<when>
<%conditions |> (cond,_) => '<cond><%printExpStrEscaped(cond)%></cond>' ; separator="\n" %>
<lhs><%crefStr(e.left)%></lhs>
<rhs><%printExpStrEscaped(e.right)%></rhs>
</when>
<%dumpElementSource(e.source)%>
>>
else "UNKNOWN"
else error(sourceInfo(),"dumpEqs: Unknown equation")
%>
</equation><%\n%>
>>
end dumpEqs;

template dumpWithin(Within w)
Expand All @@ -128,47 +176,55 @@ end dumpWithin;
template dumpElementSource(ElementSource source)
::=
match source
case s as SOURCE(__) then
case s as SOURCE(info=info as INFO(__)) then
<<
<%infoStr(s.info)%>
partOfLst: <%s.partOfLst |> w => dumpWithin(w)%>
instanceOptLst: <%s.instanceOptLst |> SOME(cr) => crefStr(cr)%>
connectEquationOptLst: <%s.connectEquationOptLst |> p => "w"%>
typeLst: <%s.typeLst |> p => dotPath(p) ; separator = "," %>
operations (<%listLength(s.operations)%>): <%s.operations |> op => dumpOperation(op,s.info) %>
<source>
<info file="<%info.fileName%>" lineStart="<%info.lineNumberStart%>" lineEnd="<%info.lineNumberEnd%>" colStart="<%info.columnNumberStart%>" colEnd="<%info.columnNumberEnd%>"/>
<%s.partOfLst |> w => '<part-of><%dumpWithin(w)%></part-of>' %>
<%s.instanceOptLst |> SOME(cr) => '<instance><%crefStr(cr)%></instance>' %>
<%s.connectEquationOptLst |> p => "<connect-equation />"%>
<%s.typeLst |> p => '<type><%dotPath(p)%></type>' ; separator = "\n" %>
</source>
<operations>
<%s.operations |> op => dumpOperation(op,s.info) ; separator="\n" %>
</operations>
>>
end dumpElementSource;

template dumpOperation(SymbolicOperation op, Info info)
::=
match op
case SIMPLIFY(__) then
<<<%\n%>
simplify:
<%printExpStr(before)%>
=>
<%printExpStr(after)%>
<<
<simplify>
<before><%printExpStrEscaped(before)%></before>
<after><%printExpStrEscaped(after)%></after>
</simplify>
>>
case SUBSTITUTION(__) then
<<<%\n%>
subst:
<%printExpStr(source)%>
<%listReverse(substitutions) |> target =>
<<
=>
<%printExpStr(target)%>
>> ; separator = "\n" %>
<<
<substitution>
<before><%printExpStrEscaped(source)%></before>
<%listReverse(substitutions) |> target => '<exp><%printExpStrEscaped(target)%></exp>' ; separator="\n" %>
</substitution>
>>
case op as OP_INLINE(__) then
<<<%\n%>
inline:
<%printExpStr(op.before)%>
=>
<%printExpStr(op.after)%>
<<
<inline>
<before><%printExpStrEscaped(op.before)%></before>
<after><%printExpStrEscaped(op.after)%></after>
</inline>
>>
case op as SOLVED(__) then
<<
<solved>
<lhs><%crefStr(op.cr)%></lhs>
<rhs><%printExpStrEscaped(op.exp)%></rhs>
</solved>
>>
case op as SOLVED(__) then '<%\n%> simple equation: <%crefStr(op.cr)%> = <%printExpStr(op.exp)%>'
case op as LINEAR_SOLVED(__) then
<<<%\n%>
<<
<linear-solved>
simple equation from linear system:
[<%vars |> v => crefStr(v) ; separator = " ; "%>] = [<%result |> r => r ; separator = " ; "%>]
[
Expand All @@ -178,34 +234,55 @@ template dumpOperation(SymbolicOperation op, Info info)
X
=
[<%rhs |> r => r ; separator = " ; "%>]
</linear-solved>
>>
case op as SOLVE(__) then
<<<%\n%>
solve:
<%printExpStr(op.exp1)%> = <%printExpStr(op.exp2)%>
=>
<%crefStr(op.cr)%> = <%printExpStr(op.res)%>
added assertions:
<%op.assertConds |> cond => printExpStr(cond); separator="\n"%>
<<
<solve>
<old>
<lhs><%printExpStrEscaped(op.exp1)%></lhs>
<rhs><%printExpStrEscaped(op.exp2)%></rhs>
</old>
<new>
<lhs><%crefStr(op.cr)%></lhs>
<rhs><%printExpStrEscaped(op.res)%></rhs>
</new>
<assertions>
<%op.assertConds |> cond => '<assertion><%printExpStrEscaped(cond)%></assertion>'; separator="\n"%>
</assertions>
</solve>
>>
case op as OP_DERIVE(__) then
<<<%\n%>
derive:
d/d<%crefStr(op.cr)%> <%printExpStr(op.before)%>
=>
<%printExpStr(op.after)%>
<<
<derivative>
<exp><%printExpStrEscaped(op.before)%></exp>
<with-respect-to><%crefStr(op.cr)%></with-respect-to>
<result><%printExpStrEscaped(op.after)%></result>
</derivative>
>>
case OP_RESIDUAL(__) then
<<<%\n%>
residual:
<%printExpStr(e1)%> = <%printExpStr(e2)%>
=>
0.0 = <%printExpStr(e)%>
<<
<op-residual>
<lhs><%printExpStrEscaped(e1)%></lhs>
<rhs><%printExpStrEscaped(e2)%></rhs>
<result><%printExpStrEscaped(e)%></result>
</op-residual>
>>
case op as NEW_DUMMY_DER(__) then
<<
<dummyderivative>
<chosen><%crefStr(op.chosen)%></chosen>
<%op.candidates |> cr => '<candidate><%crefStr(cr)%></candidate>' ; separator = "\n"%>'
</dummyderivative>
>>
case op as NEW_DUMMY_DER(__) then '<%\n%> dummy derivative: <%crefStr(op.chosen)%> from candidates: <%op.candidates |> cr => crefStr(cr) ; separator = ","%>'
else Tpl.addSourceTemplateError("Unknown operation",info)
end dumpOperation;

template printExpStrEscaped(Exp exp)
::=
escapeModelicaStringToXmlString(printExpStr(exp))
end printExpStrEscaped;

end SimCodeDump;

// vim: filetype=susan sw=2 sts=2

0 comments on commit 178a454

Please sign in to comment.