Skip to content

Commit

Permalink
Completed (I think) the support for when clauses
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@9527 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
smiz committed Jul 22, 2011
1 parent 523077e commit 9db2da6
Showing 1 changed file with 10 additions and 242 deletions.
252 changes: 10 additions & 242 deletions Compiler/susan_codegen/SimCode/SimCodeAdevs.tpl
Expand Up @@ -138,6 +138,8 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
double timeValue, $P$old$timeValue;
// Are we at an event?
bool atEvent;
// Are we initializing the model?
bool atInit;
// Helping variables for when clauses
bool helpVars[<%vi.numHelpVars%>], helpVars_saved[<%vi.numHelpVars%>];
int numHelpVars() const { return <%vi.numHelpVars%>; }
Expand All @@ -148,6 +150,7 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
void save_vars();
void restore_vars();
void clear_event_flags();
bool initial() const { return atInit; }

protected:
/**
Expand Down Expand Up @@ -234,7 +237,7 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
<<
#include "<%lastIdentOfPath(modelInfo.name)%>.h"
#include "modelica.h"
#include "simulation_runtime.h"
#include "adevs_modelica_runtime.h"
using namespace adevs;

#ifdef extraPolate
Expand All @@ -253,6 +256,10 @@ case SIMCODE(modelInfo = MODELINFO(varInfo = vi as VARINFO(__))) then
// This is used by the residual functions
static <%lastIdentOfPath(modelInfo.name)%>* active_model;

// undefine initial() (orig. defn. in omc's simulation_runtime.h)
#ifdef initial
#undef initial
#endif
// Less, Greater, etc are positive if true, negative if false
#define Adevs_Less(exp1,exp2) ((exp2)-(exp1))
#define Adevs_Greater(exp1,exp2) ((exp1)-(exp2))
Expand Down Expand Up @@ -447,6 +454,7 @@ case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__))) then

void <%lastIdentOfPath(modelInfo.name)%>::init(double* q)
{
atInit = true;
atEvent = false;
timeValue = q[numVars()-1] = 0.0;
clear_event_flags();
Expand All @@ -471,6 +479,7 @@ case SIMCODE(modelInfo = MODELINFO(vars = vars as SIMVARS(__))) then
for (int i = 0; i < numHelpVars(); i++)
helpVars_saved[i] = helpVars[i];
<%(vars.stateVars |> SIMVAR(__) => 'q[<%index%>]=<%cref(name)%>;') ;separator="\n"%>
atInit = false;
}
>>
end makeInit;
Expand Down Expand Up @@ -592,101 +601,6 @@ case FUNCTIONCODE(__) then
"" // Return empty result since result written to files directly
end translateFunctions;


template simulationFile(SimCode simCode, String guid)
"Generates code for main C file for simulation target."
::=
match simCode
case simCode as SIMCODE(__) then
<<
<%simulationFileHeader(simCode)%>
<%externalFunctionIncludes(externalFunctionIncludes)%>
#include "<%fileNamePrefix%>_functions.c"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _OMC_MEASURE_TIME
int measure_time_flag = 1;
#else
int measure_time_flag = 0;
#endif
<%globalData(modelInfo,fileNamePrefix,guid)%>
<%equationInfo(appendLists(appendAllequation(JacobianMatrixes),allEquations))%>
<%functionSetLocalData()%>
<%functionInitializeDataStruc()%>
<%functionCallExternalObjectConstructors(extObjInfo)%>
<%functionDeInitializeDataStruc(extObjInfo)%>
<%functionExtraResiduals(allEquations)%>
<%functionInput(modelInfo)%>
<%functionOutput(modelInfo)%>
<%functionInitSample(sampleConditions)%>
<%functionSampleEquations(sampleEquations)%>
<%functionStoreDelayed(delayedExps)%>
<%functionInitial(initialEquations)%>
<%functionInitialResidual(residualEquations)%>
<%functionBoundParameters(parameterEquations)%>
<%functionODE(odeEquations,(match simulationSettingsOpt case SOME(settings as SIMULATION_SETTINGS(__)) then settings.method else ""))%>
<%functionODE_residual()%>
<%functionAlgebraic(algebraicEquations)%>
<%functionAliasEquation(removedEquations)%>
<%functionDAE(allEquations, whenClauses, helpVarInfo)%>
<%functionOnlyZeroCrossing(zeroCrossings)%>
<%functionCheckForDiscreteChanges(discreteModelVars)%>
<%functionAssertsforCheck(algorithmAndEquationAsserts)%>
<%generateLinearMatrixes(JacobianMatrixes)%>
<%functionlinearmodel(modelInfo)%>
#ifdef __cplusplus
}
#endif
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end simulationFile;

template simulationFileHeader(SimCode simCode)
"Generates header part of simulation file."
::=
match simCode
case SIMCODE(modelInfo=MODELINFO(__), extObjInfo=EXTOBJINFO(__)) then
<<
// Simulation code for <%dotPath(modelInfo.name)%> generated by the OpenModelica Compiler <%getVersionNr()%>.

#include "modelica.h"
#include "assert.h"
#include "string.h"
#include "simulation_runtime.h"

#include "<%fileNamePrefix%>_functions.h"

>>
end simulationFileHeader;


template globalData(ModelInfo modelInfo, String fileNamePrefix, String guid)
"Generates global data in simulation file."
::=
Expand Down Expand Up @@ -1208,62 +1122,6 @@ template functionSampleEquations(list<SimEqSystem> sampleEqns)
>>
end functionSampleEquations;

template functionInitial(list<SimEqSystem> initialEquations)
"Generates function in simulation file."
::=
let &varDecls = buffer "" /*BUFD*/
let eqPart = (initialEquations |> eq as SES_SIMPLE_ASSIGN(__) =>
equation_(eq, contextOther, &varDecls /*BUFD*/)
;separator="\n")
<<
int initial_function()
{
<%varDecls%>
<%eqPart%>
<%initialEquations |> SES_SIMPLE_ASSIGN(__) =>
'if (sim_verbose >= LOG_INIT) { printf("Setting variable start value: %s(start=%f)\n", "<%cref(cref)%>", (<%crefType(cref)%>) <%cref(cref)%>); }'
;separator="\n"%>
return 0;
}
>>
end functionInitial;


template functionInitialResidual(list<SimEqSystem> residualEquations)
"Generates function in simulation file."
::=
let &varDecls = buffer "" /*BUFD*/
let body = (residualEquations |> SES_RESIDUAL(__) =>
match exp
case DAE.SCONST(__) then
'localData->initialResiduals[i++] = 0;'
else
let &preExp = buffer "" /*BUFD*/
let expPart = daeExp(exp, contextOther, &preExp /*BUFC*/,
&varDecls /*BUFD*/)
'<%preExp%>localData->initialResiduals[i++] = <%expPart%>;
if (sim_verbose == LOG_RES_INIT) { printf(" Residual[%d] : <%ExpressionDump.printExpStr(exp)%> = %f\n",i,localData->initialResiduals[i-1]); }'
;separator="\n")
<<
int initial_residual()
{
int i = 0;
state mem_state;
<%varDecls%>
mem_state = get_memory_state();
<%body%>
restore_memory_state(mem_state);
return 0;
}
>>
end functionInitialResidual;


template functionExtraResiduals(list<SimEqSystem> allEquations)
"Generates functions in simulation file."
::=
Expand Down Expand Up @@ -1631,25 +1489,6 @@ template functionOnlyZeroCrossing(list<ZeroCrossing> zeroCrossings)
>>
end functionOnlyZeroCrossing;

template functionCheckForDiscreteChanges(list<ComponentRef> discreteModelVars)
"Generates function in simulation file."
::=

let changediscreteVars = (discreteModelVars |> var => match var case CREF_QUAL(__) case CREF_IDENT(__) then
'if (<%cref(var)%> != $P$PRE<%cref(var)%>) { if (sim_verbose >= LOG_EVENTS) { printf("Discrete Var <%crefStr(var)%> : <%crefToPrintfArg(var)%> to <%crefToPrintfArg(var)%>\n", $P$PRE<%cref(var)%>, <%cref(var)%>); } needToIterate=1; }'
;separator="\n")
<<
int checkForDiscreteChanges()
{
int needToIterate = 0;
<%changediscreteVars%>
return needToIterate;
}
>>
end functionCheckForDiscreteChanges;

template crefToPrintfArg(ComponentRef cr)
::=
match crefType(cr)
Expand Down Expand Up @@ -2339,74 +2178,6 @@ template recordsFile(String filePrefix, list<RecordDeclaration> recordDecls)
/* adpro: leave a newline at the end of file to get rid of warnings! */
end recordsFile;

template simulationFunctionsHeaderFile(String filePrefix, list<Function> functions, list<RecordDeclaration> recordDecls)
"Generates the content of the C file for functions in the simulation case."
::=
<<
#ifndef <%stringReplace(filePrefix,".","_")%>__H
#define <%stringReplace(filePrefix,".","_")%>__H
<%commonHeader()%>
#include "simulation_runtime.h"
#ifdef __cplusplus
extern "C" {
#endif
<%recordDecls |> rd => recordDeclarationHeader(rd) ;separator="\n"%>
<%functionHeaders(functions)%>
#ifdef __cplusplus
}
#endif
#endif

<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end simulationFunctionsHeaderFile;


template simulationMakefile(SimCode simCode)
"Generates the contents of the makefile for the simulation case."
::=
match simCode
case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simulationSettingsOpt = sopt) then
let dirExtra = if modelInfo.directory then '-L"<%modelInfo.directory%>"' //else ""
let libsStr = (makefileParams.libs |> lib => lib ;separator=" ")
let libsPos1 = if not dirExtra then libsStr //else ""
let libsPos2 = if dirExtra then libsStr // else ""
let extraCflags = match sopt case SOME(s as SIMULATION_SETTINGS(__)) then
'<%if s.measureTime then "-D_OMC_MEASURE_TIME "%> <%match s.method
case "inline-euler" then "-D_OMC_INLINE_EULER"
case "inline-rungekutta" then "-D_OMC_INLINE_RK"%>'
<<
# Makefile generated by OpenModelica

# Simulations use -O3 by default
SIM_OR_DYNLOAD_OPT_LEVEL=-O3
CC=<%makefileParams.ccompiler%>
CXX=<%makefileParams.cxxcompiler%>
LINK=<%makefileParams.linker%>
EXEEXT=<%makefileParams.exeext%>
DLLEXT=<%makefileParams.dllext%>
CFLAGS_BASED_ON_INIT_FILE=<%extraCflags%>
CFLAGS=$(CFLAGS_BASED_ON_INIT_FILE) <%makefileParams.cflags%> <%match sopt case SOME(s as SIMULATION_SETTINGS(__)) then s.cflags /* From the simulate() command */%>
CPPFLAGS=-I"<%makefileParams.omhome%>/include/omc" -I. <%dirExtra%> <%makefileParams.includes ; separator=" "%>
LDFLAGS=-L"<%makefileParams.omhome%>/lib/omc" <%makefileParams.ldflags%>
SENDDATALIBS=<%makefileParams.senddatalibs%>
PERL=perl
MAINFILE=<%fileNamePrefix%><% if acceptMetaModelicaGrammar() then ".conv"%>.c
MAINOBJ=<%fileNamePrefix%><% if acceptMetaModelicaGrammar() then ".conv"%>.o

.PHONY: clean <%fileNamePrefix%>
<%fileNamePrefix%>: clean $(MAINOBJ) <%fileNamePrefix%>_records.o
<%\t%> $(CXX) -I. -o <%fileNamePrefix%>$(EXEEXT) $(MAINOBJ) <%fileNamePrefix%>_records.o $(CPPFLAGS) <%dirExtra%> <%libsPos1%> <%libsPos2%> -lsim -linteractive $(CFLAGS) $(SENDDATALIBS) $(LDFLAGS) <%match System.os() case "OSX" then "-lf2c" else "-Wl,-Bstatic -lf2c -Wl,-Bdynamic"%>
<%fileNamePrefix%>.conv.c: <%fileNamePrefix%>.c
<%\t%> $(PERL) <%makefileParams.omhome%>/share/omc/scripts/convert_lines.pl $< $@.tmp
<%\t%> @mv $@.tmp $@
$(MAINOBJ): $(MAINFILE) <%fileNamePrefix%>_functions.c <%fileNamePrefix%>_functions.h
clean:
<%\t%> @rm -f <%fileNamePrefix%>_records.o $(MAINOBJ)
>>
end simulationMakefile;

template xsdateTime(DateTime dt)
"YYYY-MM-DDThh:mm:ssZ"
::=
Expand Down Expand Up @@ -4567,9 +4338,6 @@ template algStmtReinit(DAE.Statement stmt, Context context, Text &varDecls /*BUF
let expPart1 = daeExp(var, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
let expPart2 = daeExp(value, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
<<
if (sim_verbose >= LOG_EVENTS) {
printf("reinit <%expPart1%> = %f\n", <%expPart1%>);
}
$P$old<%expPart1%> = <%expPart1%>;
<%preExp%>
<%expPart1%> = <%expPart2%>;
Expand Down

0 comments on commit 9db2da6

Please sign in to comment.