Skip to content

Commit

Permalink
Cleaned up #116
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel committed Jul 15, 2015
1 parent f7b4037 commit af61d76
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
15 changes: 1 addition & 14 deletions SimulationRuntime/c/simulation/solver/newtonIteration.c
Expand Up @@ -87,9 +87,8 @@ int allocateNewtonData(int size, void** voiddata)
DATA_NEWTON* data = (DATA_NEWTON*) malloc(sizeof(DATA_NEWTON));

*voiddata = (void*)data;
assertStreamPrint(NULL, 0 != data, "allocationNewtonData() failed!");
assertStreamPrint(NULL, NULL != data, "allocationNewtonData() failed!");

data->initialized = 0;
data->resScaling = (double*) malloc(size*sizeof(double));
data->fvecScaled = (double*) malloc(size*sizeof(double));

Expand Down Expand Up @@ -118,8 +117,6 @@ int allocateNewtonData(int size, void** voiddata)
data->numberOfIterations = 0;
data->numberOfFunctionEvaluations = 0;


assertStreamPrint(NULL, 0 != *voiddata, "allocationNewtonData() voiddata failed!");
return 0;
}

Expand Down Expand Up @@ -206,7 +203,6 @@ int _omc_newton(int(*f)(int*, double*, double*, void*, int), DATA_NEWTON* solver

solverData->nfev++;


/* save current fvec in f_old*/
memcpy(solverData->f_old, fvec, *n*sizeof(double));

Expand Down Expand Up @@ -293,7 +289,6 @@ int _omc_newton(int(*f)(int*, double*, double*, void*, int), DATA_NEWTON* solver
solverData->nfev++;
}


calculatingErrors(solverData, &delta_x, &delta_x_scaled, &delta_f, &error_f, &scaledError_f, n, x, fvec);

/* updating x */
Expand Down Expand Up @@ -321,7 +316,6 @@ int _omc_newton(int(*f)(int*, double*, double*, void*, int), DATA_NEWTON* solver
messageClose(LOG_NLS_V);
printErrors(delta_x, delta_x_scaled, delta_f, error_f, scaledError_f, eps);
}

}

solverData->numberOfIterations += l;
Expand Down Expand Up @@ -364,7 +358,6 @@ int solveLinearSystem(int* n, int* iwork, double* fvec, double *fjac, DATA_NEWTO
int i, nrsh=1, lapackinfo;
char trans = 'N';


/* if no factorization is given, calculate it */
if (solverData->factorization == 0)
{
Expand Down Expand Up @@ -433,7 +426,6 @@ void calculatingErrors(DATA_NEWTON* solverData, double* delta_x, double* delta_x
for (i=0; i<*n; i++)
solverData->fvecScaled[i]=fvec[i]/solverData->resScaling[i];
*scaledError_f = enorm_(n,solverData->fvecScaled);

}

/*! \fn calculatingErrors
Expand Down Expand Up @@ -523,7 +515,6 @@ void damping_heuristic(double* x, int(*f)(int*, double*, double*, void*, int),

*lambda = 1;


messageClose(LOG_NLS_V);
}

Expand Down Expand Up @@ -664,7 +655,6 @@ void LineSearch(double* x, int(*f)(int*, double*, double*, void*, int),

for (i=0; i<*n; i++)
solverData->x_new[i]=x[i]-lambda_minimum*solverData->x_increment[i];

}

/*! \fn Backtracking
Expand Down Expand Up @@ -701,10 +691,8 @@ void Backtracking(double* x, int(*f)(int*, double*, double*, void*, int),
/* Backtracking only if full newton step is useless */
if (enorm_new >= current_fvec_enorm)
{

infoStreamPrint(LOG_NLS_V, 0, "Start Backtracking\n enorm_new= %f \t current_fvec_enorm=%f",enorm_new, current_fvec_enorm);


/* h(x) = 1/2 * ||f(x)|| ^2
* g(lambda) = h(x_old + lambda * x_increment)
* find minimum of g with golden ratio method
Expand Down Expand Up @@ -765,7 +753,6 @@ void Backtracking(double* x, int(*f)(int*, double*, double*, void*, int),
}



lambda = (a+b)/2;


Expand Down
1 change: 0 additions & 1 deletion SimulationRuntime/c/simulation/solver/newtonIteration.h
Expand Up @@ -43,7 +43,6 @@ extern "C" {

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

Expand Down
Expand Up @@ -92,7 +92,7 @@ int getAnalyticalJacobianNewton(DATA* data, double* jac, int sysNumber)
if(data->simulationInfo.analyticJacobians[index].sparsePattern.colorCols[ii]-1 == i)
data->simulationInfo.analyticJacobians[index].seedVars[ii] = 1;

((systemData->analyticalJacobianColumn))(data);
systemData->analyticalJacobianColumn(data);

for(j = 0; j < data->simulationInfo.analyticJacobians[index].sizeCols; j++)
{
Expand Down
10 changes: 5 additions & 5 deletions SimulationRuntime/c/simulation/solver/nonlinearSolverNewton.h
Expand Up @@ -40,11 +40,11 @@
extern "C" {
#endif

typedef struct{
void* data;
int sysNumber;
}DATA_USER;

typedef struct
{
void* data;
int sysNumber;
}DATA_USER;

int solveNewton(DATA *data, int sysNumber);

Expand Down
38 changes: 25 additions & 13 deletions SimulationRuntime/c/simulation/solver/nonlinearSystem.c
Expand Up @@ -50,7 +50,8 @@

int check_nonlinear_solution(DATA *data, int printFailingSystems, int sysNumber);

struct dataNewtonAndHybrid {
struct dataNewtonAndHybrid
{
void* newtonData;
void* hybridData;
};
Expand All @@ -62,8 +63,8 @@ struct dataNewtonAndHybrid {
* \param [ref] [data]
* \param [ref] [systemData]
*/
int initializeNLScsvData(DATA* data, NONLINEAR_SYSTEM_DATA* systemData){

int initializeNLScsvData(DATA* data, NONLINEAR_SYSTEM_DATA* systemData)
{
struct csvStats* stats = (struct csvStats*) malloc(sizeof(struct csvStats));
char buffer[100];
sprintf(buffer, "%s_NLS%dStatsCall.csv", data->modelData.modelFilePrefix, (int)systemData->equationIndex);
Expand All @@ -84,7 +85,8 @@ int initializeNLScsvData(DATA* data, NONLINEAR_SYSTEM_DATA* systemData){
* \param [ref] [data]
* \param [ref] [systemData]
*/
int print_csvLineCallStatsHeader(OMC_WRITE_CSV* csvData){
int print_csvLineCallStatsHeader(OMC_WRITE_CSV* csvData)
{
unsigned char buffer[1024] = "";

/* number of call */
Expand Down Expand Up @@ -182,7 +184,8 @@ int print_csvLineCallStats(OMC_WRITE_CSV* csvData, int num, double time,
* \param [ref] [data]
* \param [ref] [systemData]
*/
int print_csvLineIterStatsHeader(DATA* data, NONLINEAR_SYSTEM_DATA* systemData, OMC_WRITE_CSV* csvData){
int print_csvLineIterStatsHeader(DATA* data, NONLINEAR_SYSTEM_DATA* systemData, OMC_WRITE_CSV* csvData)
{
unsigned char buffer[1024];
int j;
int size = modelInfoGetEquation(&data->modelData.modelDataXml, systemData->equationIndex).numVar;
Expand Down Expand Up @@ -336,7 +339,8 @@ int initializeNonlinearSystems(DATA *data)
assertStreamPrint(data->threadData, 0 != nonlinsys[i].residualFunc, "residual function pointer is invalid" );

/* check if analytical jacobian is created */
if(nonlinsys[i].jacobianIndex != -1){
if(nonlinsys[i].jacobianIndex != -1)
{
assertStreamPrint(data->threadData, 0 != nonlinsys[i].analyticalJacobianColumn, "jacobian function pointer is invalid" );
if(nonlinsys[i].initialAnalyticalJacobian(data))
{
Expand All @@ -356,10 +360,14 @@ int initializeNonlinearSystems(DATA *data)
nonlinsys[i].initializeStaticNLSData(data, &nonlinsys[i]);

/* csv data call stats*/
if (data->simulationInfo.nlsCsvInfomation){
if (initializeNLScsvData(data, &nonlinsys[i])){
if (data->simulationInfo.nlsCsvInfomation)
{
if (initializeNLScsvData(data, &nonlinsys[i]))
{
throwStreamPrint(data->threadData, "csvData initialization failed");
}else{
}
else
{
print_csvLineCallStatsHeader(((struct csvStats*) nonlinsys[i].csvData)->callStats);
print_csvLineIterStatsHeader(data, &nonlinsys[i], ((struct csvStats*) nonlinsys[i].csvData)->iterStats);
}
Expand Down Expand Up @@ -455,7 +463,8 @@ int freeNonlinearSystems(DATA *data)
free(nonlinsys[i].min);
free(nonlinsys[i].max);

if (data->simulationInfo.nlsCsvInfomation){
if (data->simulationInfo.nlsCsvInfomation)
{
stats = nonlinsys[i].csvData;
omc_write_csv_free(stats->callStats);
omc_write_csv_free(stats->iterStats);
Expand Down Expand Up @@ -539,7 +548,8 @@ int solve_nonlinear_system(DATA *data, int sysNumber)

rt_ext_tp_tick(&nonlinsys->totalTimeClock);

if(data->simulationInfo.discreteCall){
if(data->simulationInfo.discreteCall)
{
double *fvec = malloc(sizeof(double)*nonlinsys->size);
int success = 0;

Expand All @@ -557,7 +567,8 @@ int solve_nonlinear_system(DATA *data, int sysNumber)
/*catch */
MMC_CATCH_INTERNAL(simulationJumpBuffer)
#endif
if (!success) {
if (!success)
{
warningStreamPrint(LOG_STDOUT, 0, "Non-Linear Solver try to handle a problem with a called assert.");
}

Expand Down Expand Up @@ -632,7 +643,8 @@ int solve_nonlinear_system(DATA *data, int sysNumber)
nonlinsys->totalTime += rt_ext_tp_tock(&(nonlinsys->totalTimeClock));
nonlinsys->numberOfCall++;

if (data->simulationInfo.nlsCsvInfomation){
if (data->simulationInfo.nlsCsvInfomation)
{
print_csvLineCallStats(((struct csvStats*) nonlinsys->csvData)->callStats,
nonlinsys->numberOfCall,
data->localData[0]->timeValue,
Expand Down

0 comments on commit af61d76

Please sign in to comment.