Skip to content

Commit

Permalink
- start with implementation of FMU
Browse files Browse the repository at this point in the history
- Exp.mo
  - add Function crefToStr and use it for crefStr and crefModelicaStr

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@6371 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Oct 13, 2010
1 parent 97beedd commit e77642a
Show file tree
Hide file tree
Showing 8 changed files with 1,760 additions and 1,540 deletions.
54 changes: 54 additions & 0 deletions Compiler/CevalScript.mo
Expand Up @@ -354,6 +354,22 @@ algorithm
then
(cache,ret_val,st_1);

case (cache,env,
DAE.CALL(
path = Absyn.IDENT(name = "translateModelFMU"),
expLst = {DAE.CODE(Absyn.C_TYPENAME(className),DAE.ET_OTHER()),filenameprefix}),
(st as Interactive.SYMBOLTABLE(
ast = p,
explodedAst = sp,
instClsLst = ic,
lstVarVal = iv,
compiledFunctions = cf)),msg)
equation
(cache,fileNamePrefix_s) = extractFilePrefix(cache,env, filenameprefix, st, msg);
(cache,ret_val,st_1,_,_,_,_) = translateModelFMU(cache,env, className, st, fileNamePrefix_s,true, NONE);
then
(cache,ret_val,st_1);

case (cache,env,
DAE.CALL(
path = Absyn.IDENT(name = "exportDAEtoMatlab"),
Expand Down Expand Up @@ -2408,6 +2424,44 @@ algorithm
end matchcontinue;
end translateModel;

protected function translateModelFMU "function translateModelFMU
author: Frenkel TUD
translates a model into cpp code and writes also a makefile"
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.Path className "path for the model";
input Interactive.InteractiveSymbolTable inInteractiveSymbolTable;
input String inFileNamePrefix;
input Boolean addDummy "if true, add a dummy state";
input Option<SimCode.SimulationSettings> inSimSettingsOpt;
output Env.Cache outCache;
output Values.Value outValue;
output Interactive.InteractiveSymbolTable outInteractiveSymbolTable;
output DAELow.DAELow outDAELow;
output list<String> outStringLst;
output String outFileDir;
output list<tuple<String,Values.Value>> resultValues;
algorithm
(outCache,outValue,outInteractiveSymbolTable,outDAELow,outStringLst,outFileDir,resultValues):=
matchcontinue (inCache,inEnv,className,inInteractiveSymbolTable,inFileNamePrefix,addDummy,inSimSettingsOpt)
local
Env.Cache cache;
list<Env.Frame> env;
DAELow.DAELow indexed_dlow;
Interactive.InteractiveSymbolTable st;
list<String> libs;
Ceval.Msg msg;
Values.Value outValMsg;
DAE.Exp fileprefix;
String file_dir, fileNamePrefix;
case (cache,env,className,st,fileNamePrefix,addDummy,inSimSettingsOpt) /* mo file directory */
equation
(cache, outValMsg, st, indexed_dlow, libs, file_dir, resultValues) =
SimCode.translateModelFMU(cache,env,className,st,fileNamePrefix,addDummy,inSimSettingsOpt);
then
(cache,outValMsg,st,indexed_dlow,libs,file_dir,resultValues);
end matchcontinue;
end translateModelFMU;

public function translateGraphics "function: translates the graphical annotations from old to new version"
input Env.Cache inCache;
Expand Down
50 changes: 27 additions & 23 deletions Compiler/Exp.mo
Expand Up @@ -309,26 +309,43 @@ algorithm
greaterThan := System.strcmp(printComponentRefStr(cr1),printComponentRefStr(cr2)) > 0;
end crefSortFunc;

public function crefStr
public function crefToStr
"function: crefStr
This function simply converts a ComponentRef to a String."
input ComponentRef inComponentRef;
This function converts a ComponentRef to a String.
It is a tail recursive implementation, because of that it
neads inPreString. Use inNameSeperator to define the
Separator inbetween and between the namespace names and the name"
input String inPreString;
input ComponentRef inComponentRef "The ComponentReference";
input String inNameSeparator "The Separator between the Names";
output String outString;
algorithm
outString:=
matchcontinue (inComponentRef)
matchcontinue (inPreString,inComponentRef,inNameSeparator)
local
Ident s,ns,s1,ss;
ComponentRef n;
case (DAE.CREF_IDENT(ident = s)) then s;
case (DAE.CREF_QUAL(ident = s,componentRef = n))
case (inPreString,DAE.CREF_IDENT(ident = s),_)
equation
ss = stringAppend(inPreString, s);
then ss;
case (inPreString,DAE.CREF_QUAL(ident = s,componentRef = n),inNameSeparator)
equation
ns = crefStr(n);
s1 = stringAppend(s, ".");
ss = stringAppend(s1, ns);
s1 = stringAppend(inPreString, s);
ns = stringAppend(s1, inNameSeparator);
ss = crefToStr(ns,n,inNameSeparator);
then
ss;
end matchcontinue;
end crefToStr;

public function crefStr
"function: crefStr
This function simply converts a ComponentRef to a String."
input ComponentRef inComponentRef;
output String outString;
algorithm
outString:= crefToStr("",inComponentRef,".");
end crefStr;

public function crefModelicaStr
Expand All @@ -337,20 +354,7 @@ public function crefModelicaStr
input ComponentRef inComponentRef;
output String outString;
algorithm
outString:=
matchcontinue (inComponentRef)
local
Ident s,ns,s1,ss;
ComponentRef n;
case (DAE.CREF_IDENT(ident = s)) then s;
case (DAE.CREF_QUAL(ident = s,componentRef = n))
equation
ns = crefModelicaStr(n);
s1 = stringAppend(s, "_");
ss = stringAppend(s1, ns);
then
ss;
end matchcontinue;
outString:= crefToStr("",inComponentRef,"_");
end crefModelicaStr;

public function crefLastIdent
Expand Down
1 change: 1 addition & 0 deletions Compiler/Makefile.common
Expand Up @@ -24,6 +24,7 @@ $(srcdir)/modpar/libmodpar.a
SUSANMO= \
SimCodeC.mo \
SimCodeCSharp.mo \
SimCodeFMU.mo \
TplCodegen.mo

SRCMO= \
Expand Down
128 changes: 128 additions & 0 deletions Compiler/SimCode.mo
Expand Up @@ -73,6 +73,7 @@ protected import SCodeUtil;
protected import ClassInf;
protected import SimCodeC;
protected import SimCodeCSharp;
protected import SimCodeFMU;
protected import Util;
protected import Debug;
protected import Error;
Expand Down Expand Up @@ -652,6 +653,133 @@ algorithm
end createSimulationSettings;


public function generateModelCodeFMU
"Generates code for a model by creating a SimCode structure and calling the
template-based code generator on it."
input SCode.Program program;
input DAE.DAElist dae;
input DAELow.DAELow indexedDAELow;
input Absyn.Path className;
input String filenamePrefix;
input String fileDir;
input Integer[:] equationIndices;
input Integer[:] variableIndices;
input DAELow.IncidenceMatrix incidenceMatrix;
input DAELow.IncidenceMatrix incidenceMatrixT;
input list<list<Integer>> strongComponents;
input Option<SimulationSettings> simSettingsOpt;
output DAELow.DAELow outIndexedDAELow;
output list<String> libs;
output Real timeSimCode;
output Real timeTemplates;

list<String> includes;
list<Function> functions;
DAE.DAElist dae2;
String filename, funcfilename;
SimCode simCode;
Real timeSimCode, timeTemplates;
algorithm
System.realtimeTick(CevalScript.RT_CLOCK_BUILD_MODEL);
(libs, includes, functions, outIndexedDAELow, dae2) :=
createFunctions(program, dae, indexedDAELow, className);
simCode := createSimCode(dae2, outIndexedDAELow, equationIndices,
variableIndices, incidenceMatrix, incidenceMatrixT, strongComponents,
className, filenamePrefix, fileDir, functions, includes, libs, simSettingsOpt);
timeSimCode := System.realtimeTock(CevalScript.RT_CLOCK_BUILD_MODEL);

System.realtimeTick(CevalScript.RT_CLOCK_BUILD_MODEL);
Tpl.tplNoret(SimCodeC.translateModel, simCode);
Tpl.tplNoret(SimCodeFMU.translateModel, simCode);
timeTemplates := System.realtimeTock(CevalScript.RT_CLOCK_BUILD_MODEL);
end generateModelCodeFMU;

public function translateModelFMU
"Entry point to translate a Modelica model for FMU export.
Called from other places in the compiler."
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.Path className "path for the model";
input Interactive.InteractiveSymbolTable inInteractiveSymbolTable;
input String inFileNamePrefix;
input Boolean addDummy "if true, add a dummy state";
input Option<SimulationSettings> inSimSettingsOpt;
output Env.Cache outCache;
output Values.Value outValue;
output Interactive.InteractiveSymbolTable outInteractiveSymbolTable;
output DAELow.DAELow outDAELow;
output list<String> outStringLst;
output String outFileDir;
output list<tuple<String,Values.Value>> resultValues;
algorithm
(outCache,outValue,outInteractiveSymbolTable,outDAELow,outStringLst,outFileDir,resultValues):=
matchcontinue (inCache,inEnv,className,inInteractiveSymbolTable,inFileNamePrefix,addDummy, inSimSettingsOpt)
local
String filenameprefix,file_dir;
list<SCode.Class> p_1;
DAE.DAElist dae;
list<Env.Frame> env;
DAELow.DAELow dlow,dlow_1,indexed_dlow,indexed_dlow_1;
list<Integer>[:] m,mT;
Integer[:] ass1,ass2;
list<list<Integer>> comps;
Absyn.ComponentRef a_cref;
list<String> libs;
Interactive.InteractiveSymbolTable st;
Absyn.Program p,ptot;
Ceval.Msg msg;
//Exp.Exp fileprefix;
Env.Cache cache;
DAE.FunctionTree funcs;
Real timeSimCode, timeTemplates, timeBackend, timeFrontend;
case (cache,env,className,(st as Interactive.SYMBOLTABLE(ast = p)),filenameprefix,addDummy, inSimSettingsOpt)
equation
/* calculate stuff that we need to create SimCode data structure */
System.realtimeTick(CevalScript.RT_CLOCK_BUILD_MODEL);
//(cache,Values.STRING(filenameprefix),SOME(_)) = Ceval.ceval(cache,env, fileprefix, true, SOME(st), NONE, msg);
ptot = Dependency.getTotalProgram(className,p);
p_1 = SCodeUtil.translateAbsyn2SCode(ptot);
(cache,env,_,dae) = Inst.instantiateClass(cache,InnerOuter.emptyInstHierarchy,p_1,className);
timeFrontend = System.realtimeTock(CevalScript.RT_CLOCK_BUILD_MODEL);
System.realtimeTick(CevalScript.RT_CLOCK_BUILD_MODEL);
dae = DAEUtil.transformationsBeforeBackend(dae);
dlow = DAELow.lower(dae, addDummy, true);
Debug.fprint("bltdump", "Lowered DAE:\n");
Debug.fcall("bltdump", DAELow.dump, dlow);
m = DAELow.incidenceMatrix(dlow);
mT = DAELow.transposeMatrix(m);
funcs = DAEUtil.daeFunctionTree(dae);
(ass1,ass2,dlow_1,m,mT) = DAELow.matchingAlgorithm(dlow, m, mT, (DAELow.INDEX_REDUCTION(),DAELow.EXACT(),DAELow.REMOVE_SIMPLE_EQN()),funcs);
// late Inline
dlow_1 = Inline.inlineCalls(NONE(),SOME(funcs),{DAE.NORM_INLINE(),DAE.AFTER_INDEX_RED_INLINE()},dlow_1);
(comps) = DAELow.strongComponents(m, mT, ass1, ass2);
indexed_dlow = DAELow.translateDae(dlow_1,NONE);
indexed_dlow_1 = DAELow.calculateValues(indexed_dlow);
Debug.fprint("bltdump", "indexed DAE:\n");
Debug.fcall("bltdump", DAELow.dumpIncidenceMatrix, m);
Debug.fcall("bltdump", DAELow.dumpIncidenceMatrixT, mT);
Debug.fcall("bltdump", DAELow.dump, indexed_dlow_1);
Debug.fcall("bltdump", DAELow.dumpMatching, ass1);
Debug.fcall("bltdump", DAELow.dumpComponents, comps);
Debug.fprintln("dynload", "translateModel: Generating simulation code and functions.");
a_cref = Absyn.pathToCref(className);
file_dir = CevalScript.getFileDir(a_cref, p);
timeBackend = System.realtimeTock(CevalScript.RT_CLOCK_BUILD_MODEL);
(indexed_dlow_1, libs, timeSimCode, timeTemplates)
= generateModelCodeFMU(p_1, dae, indexed_dlow_1, className, filenameprefix,
file_dir, ass1, ass2, m, mT, comps,inSimSettingsOpt);
resultValues =
{("timeTemplates",Values.REAL(timeTemplates)),
("timeSimCode", Values.REAL(timeSimCode)),
("timeBackend", Values.REAL(timeBackend)),
("timeFrontend", Values.REAL(timeFrontend))
};
then
(cache,Values.STRING("SimCode: The model has been translated to FMU"),st,indexed_dlow,libs,file_dir, resultValues);
end matchcontinue;
end translateModelFMU;

public function generateModelCode
"Generates code for a model by creating a SimCode structure and calling the
template-based code generator on it."
Expand Down

0 comments on commit e77642a

Please sign in to comment.