Skip to content

Commit

Permalink
Improve model_info.json
Browse files Browse the repository at this point in the history
Add tags: torn, jacobian, system, tornsystem
Add attribute 'unknowns'
  • Loading branch information
ptaeuber authored and OpenModelica-Hudson committed Jul 11, 2017
1 parent 64eefbb commit 90441d8
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 60 deletions.
210 changes: 152 additions & 58 deletions Compiler/SimCode/SerializeModelInfo.mo
Expand Up @@ -427,6 +427,7 @@ function serializeEquation
input Boolean withOperations;
input Integer parent = 0 "No parent";
input Boolean first = false;
input Integer assign_type = 0 "0: normal equation, 1: torn equation, 2: jacobian equation";
output Boolean success;
algorithm
if not first then
Expand Down Expand Up @@ -469,7 +470,13 @@ algorithm
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);
File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
if (assign_type==1) then
File.write(file, "\",\"tag\":\"torn\",\"defines\":[\"");
elseif (assign_type==2) then
File.write(file, "\",\"tag\":\"jacobian\",\"defines\":[\"");
else
File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
end if;
writeCref(file,eq.cref,escape=JSON);
File.write(file, "\"],\"uses\":[");
serializeUses(file,Expression.extractUniqueCrefsFromExp(eq.exp));
Expand All @@ -489,7 +496,13 @@ algorithm
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);
File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
if (assign_type==1) then
File.write(file, "\",\"tag\":\"torn\",\"defines\":[\"");
elseif (assign_type==2) then
File.write(file, "\",\"tag\":\"jacobian\",\"defines\":[\"");
else
File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
end if;
writeCref(file,eq.cref,escape=JSON);
File.write(file, "\"],\"uses\":[");
serializeUses(file,Expression.extractUniqueCrefsFromExp(eq.exp));
Expand All @@ -509,7 +522,13 @@ algorithm
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);
File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
if (assign_type==1) then
File.write(file, "\",\"tag\":\"torn\",\"defines\":[\"");
elseif (assign_type==2) then
File.write(file, "\",\"tag\":\"jacobian\",\"defines\":[\"");
else
File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
end if;
writeCref(file,Expression.expCref(eq.lhs),escape=JSON);
File.write(file, "\"],\"uses\":[");
serializeUses(file,Expression.extractUniqueCrefsFromExp(eq.exp));
Expand All @@ -525,16 +544,25 @@ algorithm
i = listLength(lSystem.beqs);
j = listLength(lSystem.simJac);

eqs = SimCodeUtil.sortEqSystems(lSystem.residual);
if not listEmpty(eqs) then
serializeEquation(file,listHead(eqs),section,withOperations,parent=lSystem.index,first=true,assign_type=if lSystem.tornSystem then 1 else 0);
min(serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=if lSystem.tornSystem then 1 else 0) for e in List.rest(eqs));
end if;

jeqs = match lSystem.jacobianMatrix
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then jeqs;
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then SimCodeUtil.sortEqSystems(jeqs);
else {};
end match;
eqs = SimCodeUtil.sortEqSystems(listAppend(lSystem.residual,jeqs));
if listEmpty(eqs) then
if not listEmpty(jeqs) then
File.write(file, ",");
serializeEquation(file,listHead(jeqs),section,withOperations,parent=lSystem.index,first=true,assign_type=2);
min(serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=2) for e in List.rest(jeqs));
end if;

if listEmpty(eqs) and listEmpty(jeqs) then
File.write(file, "\n{\"eqIndex\":");
else
serializeEquation(file,listHead(eqs),section,withOperations,parent=lSystem.index,first=true);
min(serializeEquation(file,e,section,withOperations,parent=lSystem.index) for e in listRest(eqs));
File.write(file, ",\n{\"eqIndex\":");
end if;
File.writeInt(file, lSystem.index);
Expand All @@ -544,8 +572,15 @@ algorithm
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);

// Ax=b
File.write(file, "\",\"tag\":\"container\",\"display\":\"linear\",\"defines\":[");
if lSystem.tornSystem then
File.write(file, "\",\"tag\":\"tornsystem\"");
else
File.write(file, "\",\"tag\":\"system\"");
end if;

File.write(file, ",\"display\":\"linear\",\"unknowns\":" + intString(lSystem.nUnknowns) + ",\"defines\":[");
serializeUses(file,list(match v case SimCodeVar.SIMVAR() then v.name; end match
for v in lSystem.vars));
File.write(file, "],\"equation\":[{\"size\":");
Expand All @@ -567,16 +602,25 @@ algorithm
i = listLength(lSystem.beqs);
j = listLength(lSystem.simJac);

eqs = SimCodeUtil.sortEqSystems(lSystem.residual);
if not listEmpty(eqs) then
serializeEquation(file,listHead(eqs),section,withOperations,parent=lSystem.index,first=true,assign_type=if lSystem.tornSystem then 1 else 0);
min(serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=if lSystem.tornSystem then 1 else 0) for e in List.rest(eqs));
end if;

jeqs = match lSystem.jacobianMatrix
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then jeqs;
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then SimCodeUtil.sortEqSystems(jeqs);
else {};
end match;
eqs = SimCodeUtil.sortEqSystems(listAppend(lSystem.residual,jeqs));
if listEmpty(eqs) then
if not listEmpty(jeqs) then
File.write(file, ",");
serializeEquation(file,listHead(jeqs),section,withOperations,parent=lSystem.index,first=true,assign_type=2);
min(serializeEquation(file,e,section,withOperations,parent=lSystem.index,assign_type=2) for e in List.rest(jeqs));
end if;

if listEmpty(eqs) and listEmpty(jeqs) then
File.write(file, "\n{\"eqIndex\":");
else
serializeEquation(file,listHead(eqs),section,withOperations,parent=lSystem.index,first=true);
min(serializeEquation(file,e,section,withOperations,parent=lSystem.index) for e in listRest(eqs));
File.write(file, ",\n{\"eqIndex\":");
end if;
File.writeInt(file, lSystem.index);
Expand All @@ -586,8 +630,15 @@ algorithm
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);

// Ax=b
File.write(file, "\",\"tag\":\"container\",\"display\":\"linear\",\"defines\":[");
if lSystem.tornSystem then
File.write(file, "\",\"tag\":\"tornsystem\"");
else
File.write(file, "\",\"tag\":\"system\"");
end if;

File.write(file, ",\"display\":\"linear\",\"unknowns\":" + intString(lSystem.nUnknowns) + ",\"defines\":[");
serializeUses(file,list(match v case SimCodeVar.SIMVAR() then v.name; end match
for v in lSystem.vars));
File.write(file, "],\"equation\":[{\"size\":");
Expand All @@ -606,16 +657,25 @@ algorithm
i = listLength(atL.beqs);
j = listLength(atL.simJac);

eqs = SimCodeUtil.sortEqSystems(atL.residual);
if not listEmpty(eqs) then
serializeEquation(file,listHead(eqs),section,withOperations,parent=atL.index,first=true,assign_type=if atL.tornSystem then 1 else 0);
min(serializeEquation(file,e,section,withOperations,parent=atL.index,assign_type=if atL.tornSystem then 1 else 0) for e in List.rest(eqs));
end if;

jeqs = match atL.jacobianMatrix
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then jeqs;
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then SimCodeUtil.sortEqSystems(jeqs);
else {};
end match;
eqs = SimCodeUtil.sortEqSystems(listAppend(atL.residual,jeqs));
if listEmpty(eqs) then
if not listEmpty(jeqs) then
File.write(file, ",");
serializeEquation(file,listHead(jeqs),section,withOperations,parent=atL.index,first=true,assign_type=2);
min(serializeEquation(file,e,section,withOperations,parent=atL.index,assign_type=2) for e in List.rest(jeqs));
end if;

if listEmpty(eqs) and listEmpty(jeqs) then
File.write(file, "\n{\"eqIndex\":");
else
serializeEquation(file,listHead(eqs),section,withOperations,parent=atL.index,first=true);
min(serializeEquation(file,e,section,withOperations,parent=atL.index) for e in listRest(eqs));
File.write(file, ",\n{\"eqIndex\":");
end if;
File.writeInt(file, atL.index);
Expand All @@ -625,8 +685,15 @@ algorithm
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);

// Ax=b
File.write(file, "\",\"tag\":\"container\",\"display\":\"linear\",\"defines\":[");
if atL.tornSystem then
File.write(file, "\",\"tag\":\"tornsystem\"");
else
File.write(file, "\",\"tag\":\"system\"");
end if;

File.write(file, ",\"display\":\"linear\",\"unknowns\":" + intString(atL.nUnknowns) + ",\"defines\":[");
serializeUses(file,list(match v case SimCodeVar.SIMVAR() then v.name; end match
for v in atL.vars));
File.write(file, "],\"equation\":[{\"size\":");
Expand All @@ -650,8 +717,7 @@ algorithm
File.writeInt(file, parent);
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);
File.write(file, "\",\"tag\":\"algorithm\",\"defines\":[\"");
File.write(file, section + "\",\"tag\":\"algorithm\",\"defines\":[\"");
writeCref(file, Expression.expCref(stmt.exp1),escape=JSON);
File.write(file, "\"],\"uses\":[");
serializeUses(file,Expression.extractUniqueCrefsFromExp(stmt.exp));
Expand All @@ -670,8 +736,7 @@ algorithm
File.writeInt(file, parent);
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);
File.write(file, "\",\"tag\":\"algorithm\",\"equation\":[");
File.write(file, section + "\",\"tag\":\"algorithm\",\"equation\":[");
serializeList(file,eq.statements,serializeStatement);
File.write(file, "],\"source\":");
serializeSource(file,Algorithm.getStatementSource(stmt),withOperations);
Expand All @@ -686,8 +751,7 @@ algorithm
File.writeInt(file, parent);
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);
File.write(file, "\",\"tag\":\"algorithm\",\"equation\":[");
File.write(file, section + "\",\"tag\":\"algorithm\",\"equation\":[");
serializeList(file,eq.statements,serializeStatement);
File.write(file, "],\"source\":");
serializeSource(file,Algorithm.getStatementSource(stmt),withOperations);
Expand All @@ -696,28 +760,36 @@ algorithm

// no dynamic tearing
case SimCode.SES_NONLINEAR(nlSystem = nlSystem as SimCode.NONLINEARSYSTEM(), alternativeTearing = NONE()) equation
eqs = SimCodeUtil.sortEqSystems(nlSystem.eqs);
serializeEquation(file,listHead(eqs),section,withOperations,parent=nlSystem.index,first=true,assign_type=if nlSystem.tornSystem then 1 else 0);
min(serializeEquation(file,e,section,withOperations,parent=nlSystem.index,assign_type=if nlSystem.tornSystem then 1 else 0) for e in List.rest(eqs));

jeqs = match nlSystem.jacobianMatrix
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then jeqs;
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then SimCodeUtil.sortEqSystems(jeqs);
else {};
end match;
eqs = SimCodeUtil.sortEqSystems(listAppend(nlSystem.eqs,jeqs));
if listEmpty(eqs) then
File.write(file, "\n{\"eqIndex\":");
else
serializeEquation(file,listHead(eqs),section,withOperations,parent=nlSystem.index,first=true);
min(serializeEquation(file,e,section,withOperations,parent=nlSystem.index) for e in listRest(eqs));
File.write(file, ",\n{\"eqIndex\":");
if not listEmpty(jeqs) then
File.write(file, ",");
serializeEquation(file,listHead(jeqs),section,withOperations,parent=nlSystem.index,first=true,assign_type=2);
min(serializeEquation(file,e,section,withOperations,parent=nlSystem.index,assign_type=2) for e in List.rest(jeqs));
end if;

File.write(file, ",\n{\"eqIndex\":");
File.writeInt(file, nlSystem.index);
if parent <> 0 then
File.write(file, ",\"parent\":");
File.writeInt(file, parent);
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);
File.write(file, "\",\"tag\":\"container\",\"display\":\"non-linear\"");
File.write(file, ",\"defines\":[");

if nlSystem.tornSystem then
File.write(file, "\",\"tag\":\"tornsystem\"");
else
File.write(file, "\",\"tag\":\"system\"");
end if;

File.write(file, ",\"display\":\"non-linear\",\"unknowns\":" + intString(nlSystem.nUnknowns) + ",\"defines\":[");
serializeUses(file,nlSystem.crefs);
File.write(file, "],\"equation\":[[");
serializeList(file,eqs,serializeEquationIndex);
Expand All @@ -729,28 +801,36 @@ algorithm
// dynamic tearing
case SimCode.SES_NONLINEAR(nlSystem = nlSystem as SimCode.NONLINEARSYSTEM(), alternativeTearing = SOME(atNL as SimCode.NONLINEARSYSTEM())) equation
// for strict tearing set
eqs = SimCodeUtil.sortEqSystems(nlSystem.eqs);
serializeEquation(file,listHead(eqs),section,withOperations,parent=nlSystem.index,first=true,assign_type=if nlSystem.tornSystem then 1 else 0);
min(serializeEquation(file,e,section,withOperations,parent=nlSystem.index,assign_type=if nlSystem.tornSystem then 1 else 0) for e in List.rest(eqs));

jeqs = match nlSystem.jacobianMatrix
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then jeqs;
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then SimCodeUtil.sortEqSystems(jeqs);
else {};
end match;
eqs = SimCodeUtil.sortEqSystems(listAppend(nlSystem.eqs,jeqs));
if listEmpty(eqs) then
File.write(file, "\n{\"eqIndex\":");
else
serializeEquation(file,listHead(eqs),section,withOperations,parent=nlSystem.index,first=true);
min(serializeEquation(file,e,section,withOperations,parent=nlSystem.index) for e in listRest(eqs));
File.write(file, ",\n{\"eqIndex\":");
if not listEmpty(jeqs) then
File.write(file, ",");
serializeEquation(file,listHead(jeqs),section,withOperations,parent=nlSystem.index,first=true,assign_type=2);
min(serializeEquation(file,e,section,withOperations,parent=nlSystem.index,assign_type=2) for e in List.rest(jeqs));
end if;

File.write(file, ",\n{\"eqIndex\":");
File.writeInt(file, nlSystem.index);
if parent <> 0 then
File.write(file, ",\"parent\":");
File.writeInt(file, parent);
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);
File.write(file, "\",\"tag\":\"container\",\"display\":\"non-linear\"");
File.write(file, ",\"defines\":[");

if nlSystem.tornSystem then
File.write(file, "\",\"tag\":\"tornsystem\"");
else
File.write(file, "\",\"tag\":\"system\"");
end if;

File.write(file, ",\"display\":\"non-linear\",\"unknowns\":" + intString(nlSystem.nUnknowns) + ",\"defines\":[");
serializeUses(file,nlSystem.crefs);
File.write(file, "],\"equation\":[[");
serializeList(file,eqs,serializeEquationIndex);
Expand All @@ -759,28 +839,36 @@ algorithm
File.write(file, "]]},");

// for casual tearing set
eqs = SimCodeUtil.sortEqSystems(atNL.eqs);
serializeEquation(file,listHead(eqs),section,withOperations,parent=atNL.index,first=true,assign_type=if atNL.tornSystem then 1 else 0);
min(serializeEquation(file,e,section,withOperations,parent=atNL.index,assign_type=if atNL.tornSystem then 1 else 0) for e in List.rest(eqs));

jeqs = match atNL.jacobianMatrix
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then jeqs;
case SOME(SimCode.JAC_MATRIX(columns={SimCode.JAC_COLUMN(columnEqns=jeqs)})) then SimCodeUtil.sortEqSystems(jeqs);
else {};
end match;
eqs = SimCodeUtil.sortEqSystems(listAppend(atNL.eqs,jeqs));
if listEmpty(eqs) then
File.write(file, "\n{\"eqIndex\":");
else
serializeEquation(file,listHead(eqs),section,withOperations,parent=atNL.index,first=true);
min(serializeEquation(file,e,section,withOperations,parent=atNL.index) for e in listRest(eqs));
File.write(file, ",\n{\"eqIndex\":");
if not listEmpty(jeqs) then
File.write(file, ",");
serializeEquation(file,listHead(jeqs),section,withOperations,parent=atNL.index,first=true,assign_type=2);
min(serializeEquation(file,e,section,withOperations,parent=atNL.index,assign_type=2) for e in List.rest(jeqs));
end if;

File.write(file, ",\n{\"eqIndex\":");
File.writeInt(file, atNL.index);
if parent <> 0 then
File.write(file, ",\"parent\":");
File.writeInt(file, parent);
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);
File.write(file, "\",\"tag\":\"container\",\"display\":\"non-linear\"");
File.write(file, ",\"defines\":[");

if atNL.tornSystem then
File.write(file, "\",\"tag\":\"tornsystem\"");
else
File.write(file, "\",\"tag\":\"system\"");
end if;

File.write(file, ",\"display\":\"non-linear\",\"unknowns\":" + intString(atNL.nUnknowns) + ",\"defines\":[");
serializeUses(file,atNL.crefs);
File.write(file, "],\"equation\":[[");
serializeList(file,eqs,serializeEquationIndex);
Expand Down Expand Up @@ -914,7 +1002,13 @@ algorithm
end if;
File.write(file, ",\"section\":\"");
File.write(file, section);
File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
if (assign_type==1) then
File.write(file, "\",\"tag\":\"torn\",\"defines\":[\"");
elseif (assign_type==2) then
File.write(file, "\",\"tag\":\"jacobian\",\"defines\":[\"");
else
File.write(file, "\",\"tag\":\"assign\",\"defines\":[\"");
end if;
writeCref(file,eq.cref,escape=JSON);
File.write(file, "\"],\"uses\":[");
serializeUses(file,Expression.extractUniqueCrefsFromExp(eq.exp));
Expand Down

0 comments on commit 90441d8

Please sign in to comment.