Skip to content

Commit

Permalink
- continue dynamic state selection
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14508 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Dec 21, 2012
1 parent 4b50aa8 commit 182dbae
Show file tree
Hide file tree
Showing 19 changed files with 217 additions and 108 deletions.
3 changes: 2 additions & 1 deletion Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -498,9 +498,10 @@ public
uniontype StateSet
record STATESET
Integer rang;
.DAE.ComponentRef state;
.DAE.ComponentRef crA "set.x=A*states";
list< Var> varA;
list< Var> states;
list< Var> statescandidates;
list< Var> ovars;
list< Equation> eqns;
list< Equation> oeqns;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDump.mo
Expand Up @@ -269,7 +269,7 @@ protected
list<BackendDAE.Var> states;
list<DAE.ComponentRef> crstates;
algorithm
BackendDAE.STATESET(rang=rang,states=states) := statSet;
BackendDAE.STATESET(rang=rang,statescandidates=states) := statSet;
crstates := List.map(states,BackendVariable.varCref);
print("StateSet: select " +& intString(rang) +& " from\n");
debuglst((crstates,ComponentReference.printComponentRefStr,"\n","\n"));
Expand Down
7 changes: 4 additions & 3 deletions Compiler/BackEnd/IndexReduction.mo
Expand Up @@ -3839,7 +3839,7 @@ algorithm
Debug.fcall(Flags.BLT_DUMP, BackendDump.debuglst,((dstates,BackendDAETransform.dumpStates,"\n","\n")));

// generate Set Vars
(crset,setVars,crA,aVars,tp,crJ,varJ) = getSetVars(iSetIndex,rang,size);
(set,crset,setVars,crA,aVars,tp,crJ,varJ) = getSetVars(iSetIndex,rang,size);
// add set states
v = BackendVariable.addVars(setVars,v);

Expand Down Expand Up @@ -3880,7 +3880,7 @@ algorithm
varlst = List.map1r(List.map(states,Util.tuple22),BackendVariable.getVarAt,vars);
ovarlst = List.map1r(List.map(dstates,Util.tuple22),BackendVariable.getVarAt,vars);

syst = BackendDAE.EQSYSTEM(v,eqnsarr,om,omT,matching,BackendDAE.STATESET(rang,crA,aVars,varlst,ovarlst,eqnlst,oeqnlst,crJ,varJ)::stateSets);
syst = BackendDAE.EQSYSTEM(v,eqnsarr,om,omT,matching,BackendDAE.STATESET(rang,set,crA,aVars,varlst,ovarlst,eqnlst,oeqnlst,crJ,varJ)::stateSets);

// add dummy states
dstates1 = listAppend(states,dstates);
Expand Down Expand Up @@ -6081,6 +6081,7 @@ protected function getSetVars
input Integer index;
input Integer setsize;
input Integer nStates;
output DAE.ComponentRef crstates;
output list<DAE.ComponentRef> crset;
output list<BackendDAE.Var> oSetVars;
output DAE.ComponentRef ocrA;
Expand All @@ -6089,7 +6090,7 @@ protected function getSetVars
output DAE.ComponentRef ocrJ;
output list<BackendDAE.Var> oJVars;
protected
DAE.ComponentRef set,crstates;
DAE.ComponentRef set;
DAE.Type tp;
list<Integer> range;
algorithm
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/RemoveSimpleEquations.mo
Expand Up @@ -1498,7 +1498,7 @@ algorithm
case(_,_,false,_,_,SOME((_,w2)),_,_)
equation
w1 = BackendVariable.calcAliasKey(var);
tpl = Util.if_(intGt(w1,w2),SOME((i,w1)),iRmax);
tpl = Util.if_(intLt(w1,w2),SOME((i,w1)),iRmax);
then
(tpl,iSmax,iUnremovable);
end match;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/ResidualCmp.mo
Expand Up @@ -180,7 +180,7 @@ algorithm
(daevars,_,allEquations,allInitEquations) = generateEquationscollectVars(elementLst,{},1,{},{});
// generate variable definitions
simvars = SimCode.SIMVARS({},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{});
varinfo = SimCode.VARINFO(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,NONE(),NONE());
varinfo = SimCode.VARINFO(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,NONE(),NONE());

modelInfo = SimCode.MODELINFO(inClassName,fileDir,varinfo,simvars,functions,{});
extObjInfo = SimCode.EXTOBJINFO({},{});
Expand Down
11 changes: 8 additions & 3 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -194,7 +194,8 @@ uniontype VarInfo
Integer numStringParamVars;
Integer numStringAliasVars;
Integer numEquations;
Integer numNonLinearResFunctions;
Integer numNonLinearResFunctions;
Integer numStateSets;
Option <Integer> dimODE1stOrder;
Option <Integer> dimODE2ndOrder;
end VARINFO;
Expand Down Expand Up @@ -450,10 +451,14 @@ uniontype SimEqSystem
end SimEqSystem;

uniontype StateSet
record SES_STATESET
record SES_STATESET
Integer index;
Integer nCandidates;
Integer nStates;
DAE.ComponentRef states;
list<DAE.ComponentRef> statescandidates;
DAE.ComponentRef crA;
Option<JacobianMatrix> jacobianMatrix;
JacobianMatrix jacobianMatrix;
end SES_STATESET;
end StateSet;

Expand Down
131 changes: 61 additions & 70 deletions Compiler/BackEnd/SimCodeUtil.mo

Large diffs are not rendered by default.

57 changes: 54 additions & 3 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -156,6 +156,8 @@ template simulationFile(SimCode simCode, String guid)
<%functionExtraResiduals(allEquations)%>
<%functionInitialNonLinearSystems(initialEquations,parameterEquations,allEquations)%>
<%functionInitialStateSets(stateSets)%>
<%functionInput(modelInfo)%>
Expand Down Expand Up @@ -279,6 +281,7 @@ template populateModelInfo(ModelInfo modelInfo, String fileNamePrefix, String gu
data->modelData.nFunctions = <%listLength(functions)%>;
data->modelData.nEquations = <%varInfo.numEquations%>;
data->modelData.nNonLinearSystems = <%varInfo.numNonLinearResFunctions%>;
data->modelData.nStateSets = <%varInfo.numStateSets%>;

data->modelData.nDelayExpressions = <%match delayed case DELAYED_EXPRESSIONS(__) then maxDelayedIndex%>;

Expand Down Expand Up @@ -542,9 +545,15 @@ template variableDefinitionsJacobians(list<JacobianMatrix> JacobianMatrixes) "te
let varsDef = variableDefinitionsJacobians2(index0, jacColumn, seedVars, name)
let sparseDef = defineSparseIndexes(diffVars, diffedVars, name)
<<
const int INDEX_JAC_<%name%> = <%index0%>;
int functionJac<%name%>_column(void* data);
int initialAnalyticJacobian<%name%>(void* data);
#if defined(_MSC_VER)
extern "C" {
#endif
const int INDEX_JAC_<%name%> = <%index0%>;
int functionJac<%name%>_column(void* data);
int initialAnalyticJacobian<%name%>(void* data);
#if defined(_MSC_VER)
}
#endif
<%varsDef%>
<%sparseDef%>
>>
Expand Down Expand Up @@ -923,6 +932,48 @@ template functionExtraResiduals(list<SimEqSystem> allEquations)
;separator="\n\n")
end functionExtraResiduals;

// =============================================================================
//
// section for State Sets
//
// =============================================================================

template functionInitialStateSets(list<StateSet> stateSets)
"Generates functions in simulation file to initialize the stateset data."
::=
let body = (stateSets |> set hasindex i1 fromindex 0 => (match set
case set as SES_STATESET(__) then
let generatedJac = 'functionJacStateSetJac<%set.index%>_column'
let initialJac = 'initialAnalyticJacobianStateSetJac<%set.index%>'
let jacIndex = 'INDEX_JAC_StateSetJac<%set.index%>'
let statescandidatesvars = (statescandidates |> cstate hasindex i2 fromindex 0 => 'statesetData[<%i1%>].statescandidates[<%i2%>] = <%cref(cstate)%>__varInfo.id;' ;separator="\n")
<<
statesetData[<%i1%>].nCandidates = <%nCandidates%>;
statesetData[<%i1%>].nStates = <%nStates%>;
statesetData[<%i1%>].nDummyStates = <%nCandidates%>-<%nStates%>;
statesetData[<%i1%>].states = &<%cref(states)%>;
statesetData[<%i1%>].statescandidates = (unsigned int*) calloc(<%nCandidates%>,sizeof(unsigned int));
<%statescandidatesvars%>
statesetData[<%i1%>].A = &<%cref(crA)%>;
statesetData[<%i1%>].rowPivot = (modelica_integer*) calloc(<%nCandidates%>-<%nStates%>,sizeof(modelica_integer));
statesetData[<%i1%>].colPivot = (modelica_integer*) calloc(<%nCandidates%>,sizeof(modelica_integer));
statesetData[<%i1%>].J = (modelica_real*) calloc(<%nCandidates%>*<%nStates%>,sizeof(modelica_real));
statesetData[<%i1%>].analyticalJacobianColumn = <%generatedJac%>;
statesetData[<%i1%>].initialAnalyticalJacobian = <%initialJac%>;
statesetData[<%i1%>].jacobianIndex = <%jacIndex%>;

>>
)
;separator="\n\n")
<<
/* funtion initialize state sets */
void initializeStateSets(STATE_SET_DATA* statesetData, DATA *data)
{
<%body%>
}
>>
end functionInitialStateSets;

// =============================================================================
// section for initialization
//
Expand Down
13 changes: 9 additions & 4 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -314,10 +314,14 @@ package SimCode
end SimEqSystem;

uniontype StateSet
record SES_STATESET
record SES_STATESET
Integer index;
Integer nCandidates;
Integer nStates;
DAE.ComponentRef states;
list<DAE.ComponentRef> statescandidates;
DAE.ComponentRef crA;
Option<JacobianMatrix> jacobianMatrix;
JacobianMatrix jacobianMatrix;
end SES_STATESET;
end StateSet;

Expand Down Expand Up @@ -376,9 +380,10 @@ package SimCode
Integer numStringParamVars;
Integer numStringAliasVars;
Integer numEquations;
Integer numNonLinearResFunctions;
Integer numNonLinearResFunctions;
Integer numStateSets;
Option <Integer> dimODE1stOrder;
Option <Integer> dimODE2ndOrder;
Option <Integer> dimODE2ndOrder;
end VARINFO;
end VarInfo;

Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/c/Makefile.common
Expand Up @@ -35,6 +35,7 @@ RUNTIME_HEADERS = ./simulation/libf2c/f2c.h \
./simulation/solver/model_help.h \
./simulation/solver/nonlinearSystem.h \
./simulation/solver/simulation_inline_solver.h \
./simulation/solver/stateset.h \
./simulation/results/simulation_result.h \
./util/base_array.h \
./util/boolean_array.h \
Expand Down
3 changes: 1 addition & 2 deletions SimulationRuntime/c/math-support/CMakeLists.txt
Expand Up @@ -5,8 +5,7 @@
SET(math_support_sources bigden.c biglag.c dgesv_aux.c dogleg.c dpmpar.c
daux.c ddassl.c ddasrt.c dlamch.c dlinpk.c
enorm.c fdjac1.c hybrd.c hybrd1.c hybrj.c lsame.c
# nelmead.c newuoa.c newuob.c pivot.c qform.c qrfac.c r1mpyq.c
nelmead.c newuoa.c newuob.c qform.c qrfac.c r1mpyq.c
nelmead.c newuoa.c newuob.c pivot.c qform.c qrfac.c r1mpyq.c
r1updt.c trsapp.c update.c)

SET(math_support_headers blaswrap.h matrix.h)
Expand Down
27 changes: 16 additions & 11 deletions SimulationRuntime/c/math-support/pivot.c
@@ -1,6 +1,9 @@
/* pivot.c
provides a simple function to do a full pivot search while doing an LU factorization
*/

#include "modelica.h"

#include <math.h>
#include <assert.h>
//#include "matrix.h"
Expand All @@ -18,8 +21,10 @@
/* Matrixes using column major order (as in Fortran) */
/* colInd, rowInd, n_rows is added implicitly, makes code easier to read but may be considered bad programming style! */
#define set_pivot_matrix_elt(A,r,c,value) set_matrix_elt(A,rowInd[r],colInd[c],n_rows,value)
//#define set_pivot_matrix_elt(A,r,c,value) set_matrix_elt(A,colInd[c],rowInd[r],n_cols,value)
#define get_pivot_matrix_elt(A,r,c) get_matrix_elt(A,rowInd[r],colInd[c],n_rows)
#define swap(a,b) { int _swap=a; a=b; b=_swap; }
//#define get_pivot_matrix_elt(A,r,c) get_matrix_elt(A,colInd[c],rowInd[r],n_cols)
#define swap(a,b) { modelica_integer _swap=a; a=b; b=_swap; }

#ifndef min
#define min(a,b) ((a > b) ? (b) : (a))
Expand All @@ -28,15 +33,15 @@
/*
find the maximum element below (and including) line/row start
*/
void maxsearch( double *A, int start, int n_rows, int n_cols, int *rowInd, int *colInd, int *maxrow, int *maxcol, double *maxabsval)
void maxsearch( double *A, modelica_integer start, modelica_integer n_rows, modelica_integer n_cols, modelica_integer *rowInd, modelica_integer *colInd, modelica_integer *maxrow, modelica_integer *maxcol, double *maxabsval)
{
// temporary variables
int row;
int col;
modelica_integer row;
modelica_integer col;

// Initialization
int mrow = -1;
int mcol = -1;
modelica_integer mrow = -1;
modelica_integer mcol = -1;
double mabsval = 0.0;

// go through all rows and columns
Expand Down Expand Up @@ -73,16 +78,16 @@ rowInd and colInd are vectors of length nrwos and n_cols respectively.
They hold the old (and new) pivoting information, such that
A_pivoted[i,j] = A[rowInd[i], colInd[j]]
*/
void pivot( double *A, int n_rows, int n_cols, int *rowInd, int *colInd )
void pivot( double *A, modelica_integer n_rows, modelica_integer n_cols, modelica_integer *rowInd, modelica_integer *colInd )
{
// parameter, determines how much larger an element should be before rows and columns are interchanged
const double fac = 2.0;

// temporary variables
int row;
int i,j;
int maxrow;
int maxcol;
modelica_integer row;
modelica_integer i,j;
modelica_integer maxrow;
modelica_integer maxcol;
double maxabsval;
double pivot;

Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/simulation_runtime.cpp
Expand Up @@ -361,7 +361,7 @@ int startNonInteractiveSimulation(int argc, char**argv, DATA* data)
setupDataStruc2(data);

/* calc numStep */
data->simulationInfo.numSteps = (data->simulationInfo.stopTime - data->simulationInfo.startTime)/data->simulationInfo.stepSize;
data->simulationInfo.numSteps = static_cast<modelica_integer>((data->simulationInfo.stopTime - data->simulationInfo.startTime)/data->simulationInfo.stepSize);

if(measure_time_flag)
{
Expand Down
6 changes: 4 additions & 2 deletions SimulationRuntime/c/simulation/solver/CMakeLists.txt
Expand Up @@ -11,7 +11,8 @@ SET(solver_sources dassl.c
nonlinearSystem.c
nonlinearSolverHybrd.c
kinsolSolver.c
solver_main.c)
solver_main.c
stateset.c)

SET(solver_headers dassl.h
delay.h
Expand All @@ -20,7 +21,8 @@ SET(solver_headers dassl.h
nonlinearSolverHybrd.h
kinsolSolver.h
nonlinearSystem.h
solver_main.h)
solver_main.h
stateset.h)

# Library util
ADD_LIBRARY(solver ${solver_sources} ${solver_headers})
Expand Down
8 changes: 6 additions & 2 deletions SimulationRuntime/c/simulation/solver/model_help.c
Expand Up @@ -163,7 +163,7 @@ void printAllVars(DATA *data, int ringSegment, int stream)
long i;
MODEL_DATA *mData = &(data->modelData);
SIMULATION_INFO *sInfo = &(data->simulationInfo);

INFO2(stream, "Print values for buffer segment %d regarding point in time : %e", ringSegment, data->localData[ringSegment]->timeValue);
INDENT(stream);

Expand Down Expand Up @@ -720,6 +720,10 @@ void initializeDataStruc(DATA *data)
data->simulationInfo.nonlinearSystemData = (NONLINEAR_SYSTEM_DATA*) malloc(data->modelData.nNonLinearSystems*sizeof(NONLINEAR_SYSTEM_DATA));
initialNonLinearSystem(data->simulationInfo.nonlinearSystemData);

/* buffer for state sets */
data->simulationInfo.stateSetData = (STATE_SET_DATA*) malloc(data->modelData.nStateSets*sizeof(STATE_SET_DATA));
initializeStateSets(data->simulationInfo.stateSetData, data);

/* buffer for analytical jacobains */
data->simulationInfo.analyticJacobians = (ANALYTIC_JACOBIAN*) malloc(data->modelData.nJacobians*sizeof(ANALYTIC_JACOBIAN));

Expand Down Expand Up @@ -1057,7 +1061,7 @@ modelica_real _event_div_real(modelica_real x1, modelica_real x2, modelica_integ
};
#else
return trunc(value1/value2);
#endif
#endif
}

/*! \fn deInitializeDataStruc
Expand Down

0 comments on commit 182dbae

Please sign in to comment.