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

Commit

Permalink
[OMSI] Preparing data structure for code generation in SimCode
Browse files Browse the repository at this point in the history
  - Added new FMU types for simCodeTarget omsic or omsicpp
  - Added new uniontypes and recors:
    * OMSIData
    * OMSIFunction
    * SES_ALGEBRAIC_SYSTEM
    * DerivativeMatrix
  - Added hashTable for each OMSIFunction for local indices
  - Added helper functions to prepare OMSIData
    * generate equations, algebraic loops
    * dump and print functions for some OMSI data strucutres
    * helper functions to get simvar or local index for a cref in OMSIFunction

Co-authored-by: niklwors <niiklas.worschech@boschrexroth.de>
Co-authored-by: wibraun <wbraun@fh-bielefeld.de>

Belonging to [master]:
  - #2937
  - OpenModelica/OpenModelica-testsuite#1123
  • Loading branch information
AnHeuermann authored and OpenModelica-Hudson committed Feb 19, 2019
1 parent 91743ce commit af50f26
Show file tree
Hide file tree
Showing 12 changed files with 1,284 additions and 191 deletions.
8 changes: 5 additions & 3 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -279,10 +279,12 @@ uniontype VarKind "variable kind"
record OPT_LOOP_INPUT
.DAE.ComponentRef replaceExp;
end OPT_LOOP_INPUT;
record ALG_STATE end ALG_STATE; // algebraic state used by inline solver
record ALG_STATE_OLD end ALG_STATE_OLD; // algebraic state old value used by inline solver
record ALG_STATE end ALG_STATE; // algebraic state used by inline solver
record ALG_STATE_OLD end ALG_STATE_OLD; // algebraic state old value used by inline solver
record DAE_RESIDUAL_VAR end DAE_RESIDUAL_VAR; // variable kind used for DAEmode
record DAE_AUX_VAR end DAE_AUX_VAR; // auxiliary variable used for DAEmode
record DAE_AUX_VAR end DAE_AUX_VAR; // auxiliary variable used for DAEmode
record LOOP_ITERATION end LOOP_ITERATION; // used in SIMCODE, iteration variables in algebraic loops
record LOOP_SOLVED end LOOP_SOLVED; // used in SIMCODE, inner variables of a torn algebraic loop
end VarKind;

public uniontype TearingSelect
Expand Down
4 changes: 3 additions & 1 deletion Compiler/BackEnd/BackendDump.mo
Expand Up @@ -2483,7 +2483,9 @@ algorithm
case BackendDAE.ALG_STATE() then "ALG_STATE";
case BackendDAE.ALG_STATE_OLD() then "ALG_STATE_OLD";
case BackendDAE.DAE_RESIDUAL_VAR() then "DAE_RESIDUAL_VAR";
case BackendDAE.DAE_AUX_VAR() then "DAE_AUX_VAR";
case BackendDAE.LOOP_ITERATION() then "LOOP_ITERATION";
case BackendDAE.LOOP_SOLVED() then "LOOP_SOLVED";
else then "ERROR: BackendDump.kindString varKind not implemented";
end match;
end kindString;

Expand Down
2 changes: 2 additions & 0 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -659,6 +659,8 @@ algorithm
case BackendDAE.VAR(varKind=BackendDAE.OPT_TGRID()) then true;
case BackendDAE.VAR(varKind=BackendDAE.OPT_LOOP_INPUT()) then true;
case BackendDAE.VAR(varKind=BackendDAE.ALG_STATE()) then true;
case BackendDAE.VAR(varKind=BackendDAE.LOOP_ITERATION()) then true;
case BackendDAE.VAR(varKind=BackendDAE.LOOP_SOLVED()) then true;
else false;
end match;
end isNonStateVar;
Expand Down
3 changes: 2 additions & 1 deletion Compiler/BackEnd/HpcOmScheduler.mo
Expand Up @@ -51,6 +51,7 @@ import DAE;
import Error;
import Expression;
import Flags;
import HashTableCrefSimVar;
import HpcOmSchedulerExt;
import HpcOmSimCodeMain;
import List;
Expand Down Expand Up @@ -3887,7 +3888,7 @@ algorithm
simVarIdx2 := simVarIdx + numVars;

//update hashtable, create replacement rules and build new simEqSystems
ht := List.fold(simVarDupl,SimCodeUtil.addSimVarToHashTable,ht);
ht := List.fold(simVarDupl,HashTableCrefSimVar.addSimVarToHashTable,ht);
repl := BackendVarTransform.addReplacements(replIn,crefs,crefsDuplExp,NONE());
//BackendVarTransform.dumpReplacements(repl);
simEqSysts := List.map1(simEqSysIdcs,SimCodeUtil.getSimEqSysForIndex,List.flatten(odes));
Expand Down
53 changes: 43 additions & 10 deletions Compiler/SimCode/SerializeInitXML.mo
Expand Up @@ -73,18 +73,32 @@ protected
VarInfo vi;
SimulationSettings s;
File.File file = File.File();
String FMUType;
algorithm
try
File.open(file, simCode.fileNamePrefix + "_init.xml", File.Mode.Write);
_ := match Config.simCodeTarget()
case "omsic" algorithm
File.open(file, simCode.fullPathPrefix+"/"+simCode.fileNamePrefix + "_init.xml", File.Mode.Write);
then();
else algorithm
File.open(file, simCode.fileNamePrefix + "_init.xml", File.Mode.Write);
then();
end match;
makefileParams := simCode.makefileParams;
modelInfo := simCode.modelInfo;
vi := modelInfo.varInfo;
SOME(s) := simCode.simulationSettingsOpt;
FMUType := match Config.simCodeTarget()
case "omsic" then "2.0";
case "omsicpp" then "2.0";
else "1.0";
end match;


File.write(file, "<?xml version = \"1.0\" encoding=\"UTF-8\"?>\n\n");
File.write(file, "<!-- description of the model interface using an extention of the FMI standard -->\n");
File.write(file, "<fmiModelDescription\n");
File.write(file, " fmiVersion = \"1.0\"\n\n");
File.write(file, " fmiVersion = \""+FMUType+"\"\n\n");

File.write(file, " modelName = \"");
Dump.writePath(file, modelInfo.name, initialDot=false);
Expand Down Expand Up @@ -251,8 +265,16 @@ function modelVariables "Generates code for ModelVariables file for FMU target."
input ModelInfo modelInfo;
protected
SimCodeVar.SimVars vars;
Integer vr=1000, ix=0;
Integer vr, ix=0;
algorithm

// set starting index
vr := match Config.simCodeTarget()
case "omsic" then 0;
case "omsicpp" then 0;
else 1000;
end match;

vars := modelInfo.vars;

vr := scalarVariables(file, vars.stateVars, "rSta", vr);
Expand Down Expand Up @@ -358,7 +380,7 @@ algorithm
end if;

File.write(file, " alias = ");
getAliasVar(file, simVar.aliasvar);
getAliasVar(file, simVar);
File.write(file, "\n");

File.write(file, " classIndex = \"");
Expand Down Expand Up @@ -552,13 +574,24 @@ end getVariablity;

function getAliasVar "Returns the alias Attribute of ScalarVariable."
input File.File file;
input AliasVariable aliasvar;
input SimCodeVar.SimVar simVar;
algorithm
_ := match aliasvar
case AliasVariable.ALIAS()
algorithm File.write(file, "\"alias\" aliasVariable=\""); CR.writeCref(file, aliasvar.varName, XML); File.write(file, "\""); then ();
case AliasVariable.NEGATEDALIAS()
algorithm File.write(file, "\"negatedAlias\" aliasVariable=\""); CR.writeCref(file, aliasvar.varName, XML); File.write(file, "\""); then ();
_ := match simVar
local SimCodeVar.AliasVariable aliasvar;
case SimCodeVar.SIMVAR(aliasvar = aliasvar as AliasVariable.ALIAS())
algorithm
File.write(file, "\"alias\" aliasVariable=\"");
CR.writeCref(file, aliasvar.varName, XML);
File.write(file, "\" aliasVariableId=\"");
File.write(file, SimCodeUtil.getValueReference(simVar, SimCodeUtil.getSimCode(), true)+"\"");
then ();
case SimCodeVar.SIMVAR(aliasvar = aliasvar as AliasVariable.NEGATEDALIAS())
algorithm
File.write(file, "\"negatedAlias\" aliasVariable=\"");
CR.writeCref(file, aliasvar.varName, XML);
File.write(file, "\" aliasVariableId=\"");
File.write(file, SimCodeUtil.getValueReference(simVar, SimCodeUtil.getSimCode(), true)+"\"");
then ();
else
algorithm File.write(file, "\"noAlias\""); then ();
end match;
Expand Down
7 changes: 6 additions & 1 deletion Compiler/SimCode/SerializeModelInfo.mo
Expand Up @@ -46,6 +46,7 @@ import SimCode;

protected
import Algorithm;
import Config;
import DAEDump;
import Error;
import Expression;
Expand Down Expand Up @@ -75,7 +76,11 @@ algorithm
list<SimCode.SimEqSystem> eqs;
case SimCode.SIMCODE(modelInfo=mi as SimCode.MODELINFO(vars=vars))
equation
fileName = code.fileNamePrefix + "_info.json";
if Config.simCodeTarget() == "omsic" then
fileName = code.fullPathPrefix + System.pathDelimiter() + code.fileNamePrefix + "_info.json";
else
fileName = code.fileNamePrefix + "_info.json";
end if;
File.open(file,fileName,File.Mode.Write);
File.write(file, "{\"format\":\"Transformational debugger info\",\"version\":1,\n\"info\":{\"name\":");
serializePath(file, mi.name);
Expand Down
77 changes: 76 additions & 1 deletion Compiler/SimCode/SimCode.mo
Expand Up @@ -152,6 +152,7 @@ uniontype SimCode
PartitionData partitionData;
Option<DaeModeData> daeModeData;
list<SimEqSystem> inlineEquations;
Option<OMSIData> omsiData "used for OMSI to generate equations code";
end SIMCODE;
end SimCode;

Expand Down Expand Up @@ -292,6 +293,37 @@ uniontype DaeModeData
end DAEMODEDATA;
end DaeModeData;

uniontype OMSIData
"contains data for code generation for OMSI"
record OMSI_DATA
OMSIFunction initialization "contains equations and variables for initialization problem";
OMSIFunction simulation "contains equations and variables for simulation problem";
end OMSI_DATA;
end OMSIData;

uniontype OMSIFunction
"contains equations and variables for initialization or simulation problem"
record OMSI_FUNCTION
list<SimEqSystem> equations "list of single equations and systems of equations";
list<SimCodeVar.SimVar> inputVars "list of simcode variables determining input variables for equation(s)";
list<SimCodeVar.SimVar> outputVars "list of simcode variables determining output variables for equation(s)";
list<SimCodeVar.SimVar> innerVars "list of simcode variables determining inner variables for equation(s), e.g $DER(x)";
Integer nAllVars "number of input, inner and output vars";
SimCodeFunction.Context context "contains crefToSimVar hash table for lookup function in templates";
Integer nAlgebraicSystems "number of linear and non-linear algebraic systems in OMSI_FUNCTION.equations";
end OMSI_FUNCTION;
end OMSIFunction;

public constant
OMSIFunction emptyOMSIFunction = OMSI_FUNCTION(equations = {},
inputVars = {},
outputVars = {},
innerVars = {},
nAllVars = 0,
context = SimCodeFunction.contextOMSI,
nAlgebraicSystems = 0);


uniontype SimEqSystem
"Represents a single equation or a system of equations that must be solved together."
record SES_RESIDUAL
Expand All @@ -303,7 +335,7 @@ uniontype SimEqSystem

record SES_SIMPLE_ASSIGN
Integer index;
DAE.ComponentRef cref;
DAE.ComponentRef cref "left hand side of equation";
DAE.Exp exp;
DAE.ElementSource source;
BackendDAE.EquationAttributes eqAttr;
Expand Down Expand Up @@ -396,8 +428,51 @@ uniontype SimEqSystem
Integer aliasOf;
end SES_ALIAS;

record SES_ALGEBRAIC_SYSTEM
Integer index; // equation index
Integer algSysIndex; // index of algebraic system

Integer dim_n; // dimension of algebraic loop (after tearing)

Boolean partOfMixed;
Boolean tornSystem;
Boolean linearSystem;

// residual.inputVars = dependentVars
// residual.innerVars = otherTearingVars
// residual.outputVars = iterationsVars
OMSIFunction residual; // linear: A*x-b = res
// non-linear: f(x) = res

Option<DerivativeMatrix> matrix; // linear => A
// non-linear => f'(x)

list<Integer> zeroCrossingConditions;

list<DAE.ElementSource> sources;
BackendDAE.EquationAttributes eqAttr;
end SES_ALGEBRAIC_SYSTEM;

end SimEqSystem;


public
uniontype DerivativeMatrix
"represents directional derivatives with sparsity and coloring"
record DERIVATIVE_MATRIX
list<OMSIFunction> columns; // column(s) equations and variables
// inputVars: seedVars
// innerVars: inner column vars
// outputVars: result vars of the column

String matrixName; // unique matrix name
SparsityPattern sparsity;
SparsityPattern sparsityT;
list<list<Integer>> coloredCols;
Integer maxColorCols;
end DERIVATIVE_MATRIX;
end DerivativeMatrix;

public
uniontype LinearSystem
record LINEARSYSTEM
Expand Down
6 changes: 5 additions & 1 deletion Compiler/SimCode/SimCodeFunction.mo
Expand Up @@ -36,8 +36,8 @@ encapsulated package SimCodeFunction
public
import Absyn;
import DAE;
import HashTableStringToPath;
import HashTableCrefSimVar;
import HashTableStringToPath;
import Tpl;

uniontype FunctionCode
Expand Down Expand Up @@ -228,6 +228,9 @@ uniontype Context
record DAE_MODE_CONTEXT
end DAE_MODE_CONTEXT;

record OMSI_CONTEXT
Option<HashTableCrefSimVar.HashTable> hashTable; // used to get local SimVars and corresponding value references
end OMSI_CONTEXT;
end Context;

public constant Context contextSimulationNonDiscrete = SIMULATION_CONTEXT(false);
Expand All @@ -243,6 +246,7 @@ public constant Context contextZeroCross = ZEROCROSSINGS_CONTEXT();
public constant Context contextOptimization = OPTIMIZATION_CONTEXT();
public constant Context contextFMI = FMI_CONTEXT();
public constant Context contextDAEmode = DAE_MODE_CONTEXT();
public constant Context contextOMSI = OMSI_CONTEXT(NONE());

constant list<DAE.Exp> listExpLength1 = {DAE.ICONST(0)} "For CodegenC.tpl";
constant list<Variable> boxedRecordOutVars = VARIABLE(DAE.CREF_IDENT("",DAE.T_COMPLEX_DEFAULT_RECORD,{}),DAE.T_COMPLEX_DEFAULT_RECORD,NONE(),{},DAE.NON_PARALLEL(),DAE.VARIABLE())::{} "For CodegenC.tpl";
Expand Down
21 changes: 3 additions & 18 deletions Compiler/SimCode/SimCodeFunctionUtil.mo
Expand Up @@ -2051,7 +2051,6 @@ algorithm
(strs,names) := matchcontinue exp
local
String str;
list<String> strs1, strs2, names1, names2;

// seems lapack can show on Lapack form or lapack (different case) (MLS revision 6155)
// Lapack on MinGW/Windows is linked against f2c
Expand Down Expand Up @@ -2079,23 +2078,9 @@ algorithm

// If the string starts with a -, it's probably -l or -L gcc flags
case Absyn.STRING(str)
algorithm
if str=="ModelicaStandardTables" then
(strs1,names1) := getLibraryStringInMSVCFormat(Absyn.STRING("ModelicaIO"));
(strs2,names2) := getLibraryStringInMSVCFormat(Absyn.STRING("ModelicaMatIO"));
strs := listAppend(strs1, strs2);
names := listAppend(names1, names2);
else
strs := {};
names := {};
end if;
if System.regularFileExists(str) or "-" == stringGetStringChar(str, 1) then
strs := str::strs;
else
strs := (str + ".lib")::strs;
names := str::names;
end if;
then (strs,names);
equation
true = "-" == stringGetStringChar(str, 1);
then ({str},{});

case Absyn.STRING(str)
equation
Expand Down

0 comments on commit af50f26

Please sign in to comment.