Skip to content

Commit

Permalink
added double vector with residuals in the DATA struc
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2355 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
x02lucpo committed May 5, 2006
1 parent 4fb215d commit 55c6fd6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
18 changes: 14 additions & 4 deletions Compiler/SimCodegen.mo
Expand Up @@ -399,6 +399,12 @@ algorithm
" returnData->inputVars = 0;\n",
" }\n",
"\n",
" if(flags & INITIALRESIDUALS && returnData->nInitialResiduals){\n",
" returnData->initialResiduals = (double*) malloc(sizeof(double)*returnData->nInitialResiduals);\n",
" }else{\n",
" returnData->initialResiduals = 0;\n",
" }\n",
"\n",
" if(flags & INITFIXED){\n",
" returnData->initFixed = init_fixed;\n",
" }else{\n",
Expand Down Expand Up @@ -517,6 +523,10 @@ algorithm
" free(data->outputVars);\n",
" data->outputVars = 0;\n",
" }\n",
" if(flags & INITIALRESIDUALS && data->initialResiduals){\n",
" free(data->initialResiduals);\n",
" data->initialResiduals = 0;\n",
" }\n",
"\n",
"}\n"
}
Expand Down Expand Up @@ -1868,7 +1878,7 @@ algorithm
case ((dae as DAELow.DAELOW(orderedVars = vars,knownVars = knvars,orderedEqs = eqns,removedEqs = se,initialEqs = ie,arrayEqs = ae,algorithms = al,eventInfo = ev))) /* code n res */
equation
init_func_1 = Codegen.cMakeFunction("int", "initial_residual", {},
{"double *res"});
{});
vars_lst = DAELow.varList(vars);
knvars_lst = DAELow.varList(knvars);
eqns_lst = DAELow.equationList(eqns);
Expand Down Expand Up @@ -1922,7 +1932,7 @@ algorithm
case ((DAELow.RESIDUAL_EQUATION(exp = e) :: es),cg_id)
equation
(cfunc,var,cg_id) = Codegen.generateExpression(e, cg_id, Codegen.SIMULATION());
assign = Util.stringAppendList({"res[i++] = ",var,";"});
assign = Util.stringAppendList({"localData->initialResiduals[i++] = ",var,";"});
cfunc = Codegen.cAddStatements(cfunc, {assign});
(cfunc2,cg_id) = generateInitialResidualEqn(es, cg_id);
cfn = Codegen.cMergeFns({cfunc,cfunc2});
Expand Down Expand Up @@ -3626,7 +3636,7 @@ algorithm
indx_str = intString(indx);
indx_1 = indx + 1;
(cfunc,cg_id_2) = generateOdeSystem2NonlinearResiduals2(rest, indx_1, repl, cg_id_1);
stmt = Util.stringAppendList({TAB,"res[",indx_str,"] = ",var,";"});
stmt = Util.stringAppendList({TAB,"localData->initialResiduals[",indx_str,"] = ",var,";"});
exp_func_1 = Codegen.cAddStatements(exp_func, {stmt});
cfunc_1 = Codegen.cMergeFns({exp_func_1,cfunc});
then
Expand All @@ -3639,7 +3649,7 @@ algorithm
indx_str = intString(indx);
indx_1 = indx + 1;
(cfunc,cg_id_2) = generateOdeSystem2NonlinearResiduals2(rest, indx_1, repl, cg_id_1);
stmt = Util.stringAppendList({TAB,"res[",indx_str,"] = ",var,";"});
stmt = Util.stringAppendList({TAB,"localData->initialResiduals[",indx_str,"] = ",var,";"});
exp_func_1 = Codegen.cAddStatements(exp_func, {stmt});
cfunc_1 = Codegen.cMergeFns({exp_func_1,cfunc});
then
Expand Down
6 changes: 2 additions & 4 deletions c_runtime/simulation_runtime.cpp
Expand Up @@ -52,7 +52,6 @@ long* zeroCrossingEnabled;
// this is the globalData that is used in all the functions
DATA *globalData = 0;

double *globalInitialResidual;


#define MAXORD 5
Expand Down Expand Up @@ -301,10 +300,10 @@ void leastSquare(long *nz, double *z, double *funcValue)
if (globalData->initFixed[indAct++]==1)
globalData->algebraics [ind] = static_y[indy++];

initial_residual(globalInitialResidual);
initial_residual();

for (ind=0, *funcValue=0; ind<globalData->nInitialResiduals; ind++)
*funcValue += globalInitialResidual[ind]*globalInitialResidual[ind];
*funcValue += globalData->initialResiduals[ind]*globalData->initialResiduals[ind];
}

/** function reportResidualValue
Expand Down Expand Up @@ -500,7 +499,6 @@ int dassl_main( int argc, char**argv)
long ipar = 0;
int i;

globalInitialResidual = new double[globalData->nInitialResiduals];

long liw = 20+globalData->nStates;
long lrw = 50+(MAXORD+4)*globalData->nStates+
Expand Down
5 changes: 3 additions & 2 deletions c_runtime/simulation_runtime.h
Expand Up @@ -143,6 +143,7 @@ typedef enum {
HELPVARS = 0x00000004,
ALGEBRAICS = 0x00000008,
PARAMETERS = 0x00000010,
INITIALRESIDUALS = 0x00000020,
INPUTVARS = 0x00000040,
OUTPUTVARS = 0x00000080,
INITFIXED = 0x00000100,
Expand Down Expand Up @@ -181,6 +182,7 @@ typedef struct sim_DATA {
double* inputVars; //in_y INPUTVARS
double* outputVars; //out_y OUTPUTVARS
double* helpVars;
double* initialResiduals;
char* initFixed;
/* nStatesDerivatives == states */
long nStates,nAlgebraic,nParameters;
Expand Down Expand Up @@ -208,7 +210,6 @@ typedef struct sim_DATA {
double timeValue; //the time for the simulation
//used in some generated function
// this is not changed by initializeDataStruc
// it cannot be time because of the generated: #define time localData->timeValue
} DATA;


Expand Down Expand Up @@ -277,7 +278,7 @@ int bound_parameters();

// function for calculate residual values for the initial equations
// and fixed start attibutes
int initial_residual(double *res);
int initial_residual();

int initialize(const std::string*method);

Expand Down

0 comments on commit 55c6fd6

Please sign in to comment.