Skip to content

Commit

Permalink
make Modelica.Fluid.Examples.PumpingSystem working again
Browse files Browse the repository at this point in the history
 - by adding hybrid solver to the homotopy solver


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25017 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Mar 10, 2015
1 parent 950d9f4 commit 918039e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 46 deletions.
34 changes: 31 additions & 3 deletions SimulationRuntime/c/simulation/solver/nonlinearSolverHomotopy.c
Expand Up @@ -44,6 +44,7 @@

#include "nonlinearSystem.h"
#include "nonlinearSolverHomotopy.h"
#include "nonlinearSolverHybrd.h"

/*! \typedef DATA_HOMOTOPY
* define memory structure for nonlinear system solver
Expand Down Expand Up @@ -127,6 +128,8 @@ typedef struct DATA_HOMOTOPY
double timeValue;
int mixedSystem;

void* dataHybrid;

} DATA_HOMOTOPY;

/*! \fn allocateHomotopyData
Expand Down Expand Up @@ -194,6 +197,8 @@ int allocateHomotopyData(int size, void** voiddata)
data->indRow =(int*) calloc(size,sizeof(int));
data->indCol =(int*) calloc(size+1,sizeof(int));

allocateHybrdData(size, &data->dataHybrid);

assertStreamPrint(NULL, 0 != *voiddata, "allocationHomotopyData() voiddata failed!");
return 0;
}
Expand Down Expand Up @@ -245,6 +250,8 @@ int freeHomotopyData(void **voiddata)
free(data->indRow);
free(data->indCol);

freeHybrdData(&data->dataHybrid);

return 0;
}

Expand Down Expand Up @@ -1637,6 +1644,7 @@ int solveHomotopy(DATA *data, int sysNumber)
{
NONLINEAR_SYSTEM_DATA* systemData = &(data->simulationInfo.nonlinearSystemData[sysNumber]);
DATA_HOMOTOPY* solverData = (DATA_HOMOTOPY*)(systemData->solverData);
DATA_HYBRD* solverDataHybrid;
threadData_t *threadData = data->threadData;

/*
Expand Down Expand Up @@ -1701,9 +1709,10 @@ int solveHomotopy(DATA *data, int sysNumber)
debugVectorDouble(LOG_NLS_V,"System extrapolation", solverData->xStart, solverData->n);
}
vecCopy(solverData->n, solverData->xStart, solverData->x0);
/* Use actual working point for scaling */
for (i=0;i<solverData->n;i++)
/* Use actual working point for scaling */
for (i=0;i<solverData->n;i++){
solverData->xScaling[i] = fmax(systemData->nominal[i],fabs(solverData->x0[i]));
}
solverData->xScaling[solverData->n] = 1.0;

debugVectorDouble(LOG_NLS_V,"Nominal values", systemData->nominal, solverData->n);
Expand Down Expand Up @@ -1798,7 +1807,26 @@ int solveHomotopy(DATA *data, int sysNumber)
giveUp = 1;

solverData->info = 0;
if (!skipNewton) newtonAlgorithm(solverData, solverData->x);
/*if (!skipNewton) newtonAlgorithm(solverData, solverData->x); */
if (!skipNewton){

/* set x vector */
if(data->simulationInfo.discreteCall)
memcpy(systemData->nlsx, solverData->x, solverData->n*(sizeof(double)));
else
memcpy(systemData->nlsxExtrapolation, solverData->x, solverData->n*(sizeof(double)));

newtonAlgorithm(solverData, solverData->x);
if (solverData->info == -1){
solverDataHybrid = (DATA_HYBRD*)(solverData->dataHybrid);
systemData->solverData = solverDataHybrid;

solverData->info = solveHybrd(data, sysNumber);

memcpy(solverData->x, systemData->nlsx, solverData->n*(sizeof(double)));
systemData->solverData = solverData;
}
}

/* solution found */
if(solverData->info == 1)
Expand Down
43 changes: 0 additions & 43 deletions SimulationRuntime/c/simulation/solver/nonlinearSolverHybrd.c
Expand Up @@ -50,49 +50,6 @@ extern "C" {
#include "nonlinearSolverHybrd.h"
extern double enorm_(integer *n, double *x);

typedef struct DATA_HYBRD
{
int initialized; /* 1 = initialized, else = 0*/
double* resScaling;
int useXScaling;
double* xScalefactors;
double* fvecScaled;

integer n;
double* x;
double* xSave;
double* xScaled;
double* fvec;
double* fvecSave;
double xtol;
integer maxfev;
int ml;
int mu;
double epsfcn;
double* diag;
double* diagres;
integer mode;
double factor;
integer nprint;
integer info;
integer nfev;
integer njev;
double* fjac;
double* fjacobian;
integer ldfjac;
double* r__;
integer lr;
double* qtf;
double* wa1;
double* wa2;
double* wa3;
double* wa4;

unsigned int numberOfIterations; /* over the whole simulation time */
unsigned int numberOfFunctionEvaluations; /* over the whole simulation time */

} DATA_HYBRD;

struct dataAndSys {
DATA* data;
int sysNumber;
Expand Down
44 changes: 44 additions & 0 deletions SimulationRuntime/c/simulation/solver/nonlinearSolverHybrd.h
Expand Up @@ -55,6 +55,50 @@ extern int allocateHybrdData(int size, void **data);
extern int freeHybrdData(void **data);
extern int solveHybrd(DATA *data, int sysNumber);


typedef struct DATA_HYBRD
{
int initialized; /* 1 = initialized, else = 0*/
double* resScaling;
int useXScaling;
double* xScalefactors;
double* fvecScaled;

integer n;
double* x;
double* xSave;
double* xScaled;
double* fvec;
double* fvecSave;
double xtol;
integer maxfev;
int ml;
int mu;
double epsfcn;
double* diag;
double* diagres;
integer mode;
double factor;
integer nprint;
integer info;
integer nfev;
integer njev;
double* fjac;
double* fjacobian;
integer ldfjac;
double* r__;
integer lr;
double* qtf;
double* wa1;
double* wa2;
double* wa3;
double* wa4;

unsigned int numberOfIterations; /* over the whole simulation time */
unsigned int numberOfFunctionEvaluations; /* over the whole simulation time */

} DATA_HYBRD;

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 918039e

Please sign in to comment.