Skip to content

Commit

Permalink
- Main.mo: +i flag will now generate code with the correct filename
Browse files Browse the repository at this point in the history
- Added code for scheduling sample() events before runtime.
  DASSL uses this code for a (really) small hint now (not taking too large steps).
  This improves event-heavy sims by around 10%, but can be a lot better if someone who knows the solvers has a look.
- Simulation code now uses const char* instead of char*. This removes a LOT of warnings in the log files.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@5521 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed May 13, 2010
1 parent 6712f35 commit d7a0e85
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 107 deletions.
35 changes: 32 additions & 3 deletions Compiler/DAELow.mo
Expand Up @@ -13368,6 +13368,7 @@ public function calculateSizes "function: calculateSizes
output Integer outny "number of alg. vars";
output Integer outnp "number of parameters";
output Integer outng " number of zerocrossings";
output Integer outng_sample " number of zerocrossings that are samples";
output Integer outnext " number of external objects";

//nx cannot be strings
Expand All @@ -13378,7 +13379,7 @@ algorithm
matchcontinue (inDAELow)
local
list<Var> varlst,knvarlst,extvarlst;
Value np,ng,nx,ny,nx_1,ny_1,next,ny_string,np_string,ny_1_string;
Value np,ng,nsam,nx,ny,nx_1,ny_1,next,ny_string,np_string,ny_1_string;
String np_str;
Variables vars,knvars,extvars;
list<WhenClause> wc;
Expand All @@ -13394,14 +13395,42 @@ algorithm
knvarlst = varList(knvars);
(np,np_string) = calculateParamSizes(knvarlst);
np_str = intString(np);
ng = listLength(zc);
(ng,nsam) = calculateNumberZeroCrossings(zc,0,0);
(nx,ny,ny_string) = calculateVarSizes(varlst, 0, 0,0);
(nx_1,ny_1,ny_1_string) = calculateVarSizes(knvarlst, nx, ny,ny_string);
then
(nx_1,ny_1,np,ng,next,ny_1_string,np_string);
(nx_1,ny_1,np,ng,nsam,next,ny_1_string,np_string);
end matchcontinue;
end calculateSizes;

protected function calculateNumberZeroCrossings
input list<ZeroCrossing> zcLst;
input Integer zc_index;
input Integer sample_index;
output Integer zc;
output Integer sample;
algorithm
(outCFn) := matchcontinue (zcLst,zc_index,sample_index)
local
list<ZeroCrossing> xs;
case ({},zc_index,sample_index) then (zc_index,sample_index);

case (ZERO_CROSSING(relation_ = DAE.CALL(path = Absyn.IDENT(name = "sample"))) :: xs,zc_index,sample_index)
equation
sample_index = sample_index + 1;
zc_index = zc_index + 1;
(zc,sample) = calculateNumberZeroCrossings(xs,zc_index,sample_index);
then (zc,sample);

case (ZERO_CROSSING(relation_ = DAE.RELATION(operator = _), occurEquLst = _) :: xs,zc_index,sample_index)
equation
zc_index = zc_index + 1;
(zc,sample) = calculateNumberZeroCrossings(xs,zc_index,sample_index);
then (zc,sample);

end matchcontinue;
end calculateNumberZeroCrossings;

protected function calculateParamSizes "function: calculateParamSizes
author: PA

Expand Down
13 changes: 7 additions & 6 deletions Compiler/Main.mo
Expand Up @@ -564,7 +564,7 @@ algorithm
Debug.fcall("execstat",print, "*** Main -> To instantiate at time: " +& realString(clock()) +& "\n" );

// Instantiate the program.
(cache, env, d_1, scode) = instantiate(p);
(cache, env, d_1, scode, cname) = instantiate(p);

Debug.fcall("execstat",print, "*** Main -> done instantiation at time: " +& realString(clock()) +& "\n" );
Debug.fprint("beforefixmodout", "Explicit part:\n");
Expand All @@ -589,7 +589,7 @@ algorithm

// Transform if equations to if expression before going into code generation.
d = DAEUtil.transformIfEqToExpr(d,false);
cname = Absyn.lastClassname(p);

str = Print.getString();
silent = RTOpts.silent();
notsilent = boolNot(silent);
Expand Down Expand Up @@ -674,8 +674,9 @@ protected function instantiate
output Env.Env env;
output DAE.DAElist dae;
output list<SCode.Class> scode;
output Absyn.Path cname;
algorithm
(cache, env, dae) := matchcontinue(program)
(cache, env, dae, cname) := matchcontinue(program)
local
Env.Cache c;
Env.Env e;
Expand All @@ -696,7 +697,7 @@ algorithm
InnerOuter.emptyInstHierarchy,
s);
then
(c, Env.emptyEnv(), d, s);
(c, Env.emptyEnv(), d, s, Absyn.lastClassname(program));
case (_)
equation
// If a class to instantiate was given on the command line, instantiate
Expand All @@ -711,7 +712,7 @@ algorithm
s,
class_path);
then
(c, e, d, s);
(c, e, d, s, class_path);
end matchcontinue;
end instantiate;

Expand Down Expand Up @@ -847,7 +848,7 @@ algorithm
TaskGraphExt.dumpMergedGraph("merged_model.viz");
n = RTOpts.noProc();
TaskGraphExt.schedule(n);
(nx,ny,np,_,_,_,_) = DAELow.calculateSizes(indexed_dae_1);
(nx,ny,np,_,_,_,_,_) = DAELow.calculateSizes(indexed_dae_1);
nps = intString(np);
print("=======\nnp =");
print(nps);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/SimCode.mo
Expand Up @@ -3837,7 +3837,7 @@ algorithm
Integer nx, ny, np, ng, next, ny_string, np_string, ng_1;
case (dlow, numOutVars, numInVars, numHelpVars, numResiduals)
equation
(nx, ny, np, ng, next, ny_string, np_string) =
(nx, ny, np, ng, _ /* ng_sam */, next, ny_string, np_string) =
DAELow.calculateSizes(dlow);
ng_1 = filterNg(ng);
then
Expand Down
111 changes: 93 additions & 18 deletions Compiler/SimCodegen.mo
Expand Up @@ -489,25 +489,27 @@ algorithm
outString:=
matchcontinue (class_,loweredDAE,numberOfOutputVariables,numberOfInputVariables,numberOfHelpVariables,numberOfResidulas,fileDir)
local
Integer nx,ny,np,ng,ng_1,no,ni,nh,nres,next,ny_string,np_string;
Integer nx,ny,np,ng,ng_sam,ng_1,ng_sam_1,no,ni,nh,nres,next,ny_string,np_string;
String initDeinitDataStructFunction,class_str,nx_str,ny_str,np_str,ng_str,no_str,ni_str,nh_str;
String nres_str,c_code2_str,c_code3_str,c_code_str,macros_str,global_bufs,str1,str,next_str;
String nystring_str, npstring_str;
String nystring_str, npstring_str, ng_sam_str;
list<String> c_code;
Absyn.Path class_;
DAELow.DAELow dlow;
String trimmedFileDir;
case (class_,dlow,no,ni,nh,nres,fileDir)
equation
(nx,ny,np,ng,next,ny_string,np_string) = DAELow.calculateSizes(dlow);
(nx,ny,np,ng,ng_sam,next,ny_string,np_string) = DAELow.calculateSizes(dlow);
//DAELow.dump(dlow);

ng_1 = filterNg(ng);
ng_sam_1 = filterNg(ng_sam);
class_str = Absyn.pathString(class_);
nx_str = intString(nx);
ny_str = intString(ny);
np_str = intString(np);
ng_str = intString(ng_1);
ng_sam_str = intString(ng_sam_1);
no_str = intString(no);
ni_str = intString(ni);
nh_str = intString(nh);
Expand All @@ -527,10 +529,14 @@ algorithm
// and transform it to a C string: \ replaced by \\
// trimmedFileDir = System.stringReplace(trimmedFileDir, "\\", "\\\\");
str1 = Util.stringAppendList(
{"\n","#define NHELP ",nh_str,"\n","#define NG ",ng_str,"//number of zero crossing",
"\n","#define NX ",nx_str,"\n","#define NY ",ny_str,"\n","#define NP ",
np_str," // number of parameters\n","#define NO ",no_str,
" // number of outputvar on topmodel\n","#define NI ",ni_str," // number of inputvar on topmodel\n",
{"\n","#define NHELP ",nh_str,"\n",
"#define NG ",ng_str,"// number of zero crossing\n",
"#define NG_SAM ",ng_sam_str,"// number of zero crossings that are samples\n",
"#define NX ",nx_str,"\n",
"#define NY ",ny_str,"\n",
"#define NP ",np_str," // number of parameters\n",
"#define NO ",no_str," // number of outputvar on topmodel\n",
"#define NI ",ni_str," // number of inputvar on topmodel\n",
"#define NR ",nres_str," // number of residuals for initialialization function\n",
"#define NEXT ", next_str," // number of external objects\n",
"#define MAXORD 5\n",
Expand All @@ -539,8 +545,8 @@ algorithm
"\n",
global_bufs,
"extern \"C\" { /* adrpo: this is needed for Visual C++ compilation to work! */\n",
" char *model_name=\"",class_str,"\";\n",
" char *model_dir=\"",trimmedFileDir,"\";\n",
" const char *model_name=\"",class_str,"\";\n",
" const char *model_dir=\"",trimmedFileDir,"\";\n",
"}\n",
c_code_str,c_code2_str,"\n",c_code3_str,"\n"});
str = Util.stringAppendList({str1,macros_str,"\n",initDeinitDataStructFunction,"\n"})
Expand Down Expand Up @@ -869,6 +875,7 @@ extObjConstructorsDecl_str,"
returnData->nInputVars = NI;
returnData->nOutputVars = NO;
returnData->nZeroCrossing = NG;
returnData->nRawSamples = NG_SAM;
returnData->nInitialResiduals = NR;
returnData->nHelpVars = NHELP;
returnData->stringVariables.nParameters = NPSTR;
Expand Down Expand Up @@ -1035,6 +1042,13 @@ extObjConstructorsDecl_str,"
} else {
returnData->outputComments = 0;
}\n","
if(flags & RAWSAMPLES && returnData->nRawSamples) {
returnData->rawSampleExps = (sample_raw_time*) malloc(sizeof(sample_raw_time)*returnData->nRawSamples);
assert(returnData->rawSampleExps);
memset(returnData->rawSampleExps,0,sizeof(sample_raw_time)*returnData->nRawSamples);
} else {
returnData->rawSampleExps = 0;
}
if (flags & EXTERNALVARS) {
returnData->extObjs = (void**)malloc(sizeof(void*)*NEXT);
if (!returnData->extObjs) {
Expand Down Expand Up @@ -1789,7 +1803,7 @@ algorithm

get_name_function_ifs_1 = Util.stringAppendList(get_name_function_ifs_1) "generate getName function" ;
get_name_function = Util.stringAppendList(
{"char* getName( double* ",paramInGetNameFunction,")\n",
{"const char* getName( double* ",paramInGetNameFunction,")\n",
"{\n",get_name_function_ifs_1," return \"\";\n}\n\n"});
var_defines_str = Util.stringAppendList(var_defines_1);
then
Expand All @@ -1801,7 +1815,7 @@ end generateVarNamesAndComments;
protected function generateCDeclForStringArray
"function generateCDeclForStringArray
author x02lucpo
generates a static C-array with char <name>{<number>}
generates a static C-array with const char <name>{<number>}
or only a char depending it the int parameters is > 0"
input String inString1;
input String inString2;
Expand All @@ -1816,13 +1830,13 @@ algorithm
case (array_name,_,number_of_strings)
equation
(number_of_strings == 0) = true;
res = Util.stringAppendList({"char* ",array_name,"[1] = {\"\"};\n"});
res = Util.stringAppendList({"const char* ",array_name,"[1] = {\"\"};\n"});
then
res;
case (array_name,array_str,number_of_strings)
equation
number_of_strings_str = intString(number_of_strings);
res = Util.stringAppendList({"char* ",array_name,"[",number_of_strings_str,"]={",array_str,"};\n"});
res = Util.stringAppendList({"const char* ",array_name,"[",number_of_strings_str,"]={",array_str,"};\n"});
then
res;
end matchcontinue;
Expand Down Expand Up @@ -7287,7 +7301,7 @@ algorithm
stop_str = realString(stop);
step_str = realString(step);
tolerance_str = realString(tolerance);
(nx,ny,np,_,_,nystring,npstring) = DAELow.calculateSizes(dlow);
(nx,ny,np,_,_,_,nystring,npstring) = DAELow.calculateSizes(dlow);
nx_str = intString(nx);
ny_str = intString(ny);
np_str = intString(np);
Expand Down Expand Up @@ -7768,7 +7782,7 @@ algorithm
matchcontinue (inString1,inDAElist2,inDAELow3,inIntegerArray4,inIntegerArray5,inIntegerLstLst6,helpVarLst)
local
Codegen.CFunction func_zc,func_handle_zc,cfunc,cfunc0_1,cfunc0,cfunc_1,cfunc_2,func_zc0,func_handle_zc0,func_handle_zc0_1;
Codegen.CFunction func_handle_zc0_2,func_handle_zc0_3,func_zc0_1,func_zc_1,func_handle_zc_1,cfuncHelpvars;
Codegen.CFunction func_handle_zc0_2,func_handle_zc0_3,func_zc0_1,func_zc_1,func_handle_zc_1,cfuncHelpvars,func_sample_init;
Integer cg_id1,cg_id2,cg_id;
list<CFunction> extra_funcs1,extra_funcs2,extra_funcs;
String extra_funcs_str,helpvarUpdateStr,func_str,res,cname;
Expand Down Expand Up @@ -7813,8 +7827,10 @@ algorithm
func_zc0_1 = Codegen.cAddCleanups(func_zc0, {"localData->timeValue = timeBackup;", "return 0;"});
func_zc_1 = Codegen.cMergeFns({func_zc0_1,func_zc});
func_handle_zc_1 = Codegen.cMergeFns({func_handle_zc0_3,func_handle_zc});

func_sample_init = generateSampleInit(zc);

func_str = Codegen.cPrintFunctionsStr({func_zc_1,func_handle_zc_1,cfunc_2});
func_str = Codegen.cPrintFunctionsStr({func_zc_1,func_handle_zc_1,func_sample_init,cfunc_2});
res = Util.stringAppendList({extra_funcs_str,func_str});
then
res;
Expand All @@ -7838,7 +7854,7 @@ protected function generateZeroCrossing2
input Integer[:] inIntegerArray5;
input Integer[:] inIntegerArray6;
input list<list<Integer>> inIntegerLstLst7;
input list<HelpVarInfo> helpVarLst; // not used her anymore
input list<HelpVarInfo> helpVarLst; // not used here anymore
input Integer inInteger9;
input Integer inInteger10;
output CFunction outCFunction1;
Expand Down Expand Up @@ -7909,6 +7925,65 @@ algorithm
end matchcontinue;
end generateZeroCrossing2;

protected function generateSampleInit
"Generates code to schedule sample() events."
input list<DAELow.ZeroCrossing> zcLst;
output CFunction outFn;
protected
list<String> sampleTimes;
algorithm
(sampleTimes) := generateSampleInit2(zcLst, 0, 0);
outFn := Codegen.cMakeFunction("void", "function_sampleInit", {}, {});
outFn := Codegen.cAddStatements(outFn, sampleTimes);
end generateSampleInit;

protected function generateSampleInit2
"Generates code to schedule sample() events."
input list<DAELow.ZeroCrossing> zcLst;
input Integer sample_index;
input Integer zc_index;
output list<String> outStmts;
algorithm
(outCFn) := matchcontinue (zcLst,sample_index,zc_index)
local
DAELow.ZeroCrossing zc;
list<DAELow.ZeroCrossing> xs;
DAE.Exp start,interval;
String sample_index_str, zc_index_str, e1_str, e2_str, zc_str1, zc_str2, zc_str3;
list<String> res;
case ({},sample_index,_) then ({});

case (DAELow.ZERO_CROSSING(relation_ = DAE.CALL(path = Absyn.IDENT(name = "sample"),expLst = {start,interval})) :: xs,sample_index,zc_index)
equation
sample_index_str = intString(sample_index);
sample_index = sample_index + 1;
zc_index_str = intString(zc_index);
zc_index = zc_index + 1;

e1_str = printExpCppStr(start);
e2_str = printExpCppStr(interval);
zc_str1 = Util.stringAppendList({"localData->rawSampleExps[",sample_index_str,"].start = ",e1_str,";"});
zc_str2 = Util.stringAppendList({"localData->rawSampleExps[",sample_index_str,"].interval = ",e2_str,";"});
zc_str3 = Util.stringAppendList({"localData->rawSampleExps[",sample_index_str,"].zc_index = ",zc_index_str,";"});
res = generateSampleInit2(xs,sample_index,zc_index);
then (zc_str1 :: zc_str2 :: zc_str3 :: res);

case (((zc as DAELow.ZERO_CROSSING(relation_ = DAE.RELATION(operator = _), occurEquLst = _)) :: xs),sample_index,zc_index)
equation
zc_index = zc_index + 1;
(res) = generateSampleInit2(xs,sample_index,zc_index);
then (res);

case (((zc as DAELow.ZERO_CROSSING(occurEquLst = _)) :: xs),_,_)
equation
zc_str1 = dumpZeroCrossingStr(zc);
zc_str2 = Util.stringAppendList({"generateSampleInit :",zc_str1,"\n"});
Error.addMessage(Error.INTERNAL_ERROR, {zc_str2});
then fail();

end matchcontinue;
end generateSampleInit2;

protected function generateUpdateDepended
input DAE.DAElist inDAElist;
input DAELow.DAELow inDAELow;
Expand Down Expand Up @@ -8111,7 +8186,7 @@ end getZcMixedSystem;
protected function dumpZeroCrossingStr
"function: dumpZeroCrossingStr
author:
Dumps a ZeroCrossing to a sting. Useful for debugging."
Dumps a ZeroCrossing to a string. Useful for debugging."
input DAELow.ZeroCrossing inZeroCrossing;
output String outString;
algorithm
Expand Down
2 changes: 1 addition & 1 deletion c_runtime/options.cpp
Expand Up @@ -34,7 +34,7 @@

using namespace std;

bool flagSet(char *option, int argc, char** argv)
bool flagSet(const char *option, int argc, char** argv)
{
for (int i=0; i<argc;i++) {
if (("-"+string(option))==string(argv[i])) return true;
Expand Down
2 changes: 1 addition & 1 deletion c_runtime/options.h
Expand Up @@ -36,7 +36,7 @@
#include <algorithm>

/* -f */
bool flagSet(char*, int, char**);
bool flagSet(const char*, int, char**);

/* -f=value */
const std::string * getOption(const char*, int, char **);
Expand Down
2 changes: 1 addition & 1 deletion c_runtime/sendData/humbug.cpp
Expand Up @@ -113,7 +113,7 @@ void enableSendData(int enable)
}

//void initSendData(int variableCount, const char* variableNames);
void initSendData(int variableCount1, int variableCount2, char** statesNames, char** stateDerivativesNames, char** algebraicsNames)
void initSendData(int variableCount1, int variableCount2, const char** statesNames, const char** stateDerivativesNames, const char** algebraicsNames)
{
_errmesg();
return;
Expand Down
2 changes: 1 addition & 1 deletion c_runtime/sendData/humbug.h
Expand Up @@ -77,7 +77,7 @@ void _errmesg();
void setDataPort(int port);
void enableSendData(int enable);
//void initSendData(int variableCount, const char* variableNames);
void initSendData(int variableCount1, int variableCount2, char** statesNames, char** stateDerivativesNames, char** algebraicsNames);
void initSendData(int variableCount1, int variableCount2, const char** statesNames, const char** stateDerivativesNames, const char** algebraicsNames);
void sendPacket(const char* data);
void closeSendData();

Expand Down

0 comments on commit d7a0e85

Please sign in to comment.