/*! \fn printStateSelectionInfo * * function prints actually information about current state selection * * \param [in] [data] * \param [in] [set] * * \author wbraun */ void printStateSelectionInfo(DATA *data, STATE_SET_DATA *set) { long k, l; infoStreamPrint(LOG_DSS, 1, "Select %ld states from %ld candidates.", set->nStates, set->nCandidates); for(k=0; k < set->nCandidates; k++) { infoStreamPrint(LOG_DSS, 0, "[%ld] cadidate %s", k+1, set->statescandidates[k]->name); } messageClose(LOG_DSS); infoStreamPrint(LOG_DSS, 1, "Selected states"); { unsigned int aid = set->A->id - data->modelData->integerVarsData[0].info.id; modelica_integer *Adump = &(data->localData[0]->integerVars[aid]); for(k=0; k < set->nStates; k++) { for(l=0; l < set->nCandidates; l++) { if (Adump[k*set->nCandidates+l] == 1) { infoStreamPrint(LOG_DSS, 0, "[%ld] %s", k+1, set->statescandidates[k]->name); } } } } messageClose(LOG_DSS); }
/*! \fn int initializeMixedSystems(DATA *data) * * This function allocates memory for all mixed systems. * * \param [ref] [data] */ int initializeMixedSystems(DATA *data, threadData_t *threadData) { int i; int size; MIXED_SYSTEM_DATA *system = data->simulationInfo->mixedSystemData; infoStreamPrint(LOG_NLS, 1, "initialize mixed system solvers"); infoStreamPrint(LOG_NLS, 0, "%ld mixed systems", data->modelData->nMixedSystems); for(i=0; i<data->modelData->nMixedSystems; ++i) { size = system[i].size; system[i].iterationVarsPtr = (modelica_boolean**) malloc(size*sizeof(modelica_boolean*)); system[i].iterationPreVarsPtr = (modelica_boolean**) malloc(size*sizeof(modelica_boolean*)); /* allocate solver data */ switch(data->simulationInfo->mixedMethod) { case MIXED_SEARCH: allocateMixedSearchData(size, &system[i].solverData); break; default: throwStreamPrint(threadData, "unrecognized mixed solver"); } } messageClose(LOG_NLS); return 0; }
/*! * helper for initial_guess_optimizer (pick up clfag option) * author: Vitalij Ruge **/ static int initial_guess_ipopt_cflag(OptData *optData, char* cflags) { if(!strcmp(cflags,"const") || !strcmp(cflags,"CONST")) { int i, j; const int nsi = optData->dim.nsi; const int np = optData->dim.np; const int nu = optData->dim.nu; const int nReal = optData->dim.nReal; for(i = 0; i< nu; ++i ) optData->data->simulationInfo->inputVars[i] = optData->bounds.u0[i]; for(i = 0; i < nsi; ++i){ for(j = 0; j < np; ++j){ memcpy(optData->v[i][j], optData->v0, nReal*sizeof(modelica_real)); } } infoStreamPrint(LOG_IPOPT, 0, "Using const trajectory as initial guess."); return 0; }else if(!strcmp(cflags,"sim") || !strcmp(cflags,"SIM")){ infoStreamPrint(LOG_IPOPT, 0, "Using simulation as initial guess."); return 1; }else if(!strcmp(cflags,"file") || !strcmp(cflags,"FILE")){ infoStreamPrint(LOG_STDOUT, 0, "Using values from file as initial guess."); return 2; } warningStreamPrint(LOG_STDOUT, 0, "not support ipopt_init=%s", cflags); return 1; }
int MultFive_updateBoundVariableAttributes(DATA *data) { /* min ******************************************************** */ infoStreamPrint(LOG_INIT, 1, "updating min-values"); if (ACTIVE_STREAM(LOG_INIT)) messageClose(LOG_INIT); /* max ******************************************************** */ infoStreamPrint(LOG_INIT, 1, "updating max-values"); if (ACTIVE_STREAM(LOG_INIT)) messageClose(LOG_INIT); /* nominal **************************************************** */ infoStreamPrint(LOG_INIT, 1, "updating nominal-values"); if (ACTIVE_STREAM(LOG_INIT)) messageClose(LOG_INIT); /* start ****************************************************** */ infoStreamPrint(LOG_INIT, 1, "updating start-values"); if (ACTIVE_STREAM(LOG_INIT)) messageClose(LOG_INIT); return 0; }
void debugMatrixDoubleLS(int logName, char* matrixName, double* matrix, int n, int m) { if(ACTIVE_STREAM(logName)) { int i, j; int sparsity = 0; char *buffer = (char*)malloc(sizeof(char)*m*18); infoStreamPrint(logName, 1, "%s [%dx%d-dim]", matrixName, n, m); for(i=0; i<n;i++) { buffer[0] = 0; for(j=0; j<m; j++) { if (sparsity) { if (fabs(matrix[i + j*(m-1)])<1e-12) sprintf(buffer, "%s 0", buffer); else sprintf(buffer, "%s *", buffer); } else { sprintf(buffer, "%s%12.4g ", buffer, matrix[i + j*(m-1)]); } } infoStreamPrint(logName, 0, "%s", buffer); } messageClose(logName); free(buffer); } }
/*! \fn printVector * * \param [in] [vector] * \param [in] [size] * \param [in] [logLevel] * \param [in] [name] * * \author wbraun */ static void printVector(const double *vector, const integer *size, const int logLevel, const char *name) { int i; if (!ACTIVE_STREAM(logLevel)) return; infoStreamPrint(logLevel, 1, "%s", name); for(i=0; i<*size; i++) infoStreamPrint(logLevel, 0, "[%2d] %20.12g", i, vector[i]); messageClose(logLevel); }
/*! \fn damping_heuristic2 * * second (default) damping heuristic: * x_increment will be multiplied by 3/4 until the Euclidean norm of the residual function * is smaller than the Euclidean norm of the current point * * treshold for damping = 0.0001 * compiler flag: -newton = damped2 */ void damping_heuristic2(double damping_parameter, double* x, int(*f)(int*, double*, double*, void*, int), double current_fvec_enorm, int* n, double* fvec, int* k, DATA_NEWTON* solverData, void* userdata) { int i,j=0; double enorm_new, treshold = 1e-4, lambda=1; /* calculate new function values */ (*f)(n, solverData->x_new, fvec, userdata, 1); solverData->nfev++; enorm_new=enorm_(n,fvec); if (enorm_new >= current_fvec_enorm) infoStreamPrint(LOG_NLS_V, 1, "StartDamping: "); while (enorm_new >= current_fvec_enorm) { j++; lambda*=damping_parameter; infoStreamPrint(LOG_NLS_V, 0, "lambda = %e, k = %d", lambda, *k); for (i=0; i<*n; i++) solverData->x_new[i]=x[i]-lambda*solverData->x_increment[i]; /* calculate new function values */ (*f)(n, solverData->x_new, fvec, userdata, 1); solverData->nfev++; enorm_new=enorm_(n,fvec); if (lambda <= treshold) { warningStreamPrint(LOG_NLS_V, 0, "Warning: lambda reached a threshold."); /* if damping is without success, trying full newton step; after 5 full newton steps try a very little step */ if (*k >= 5) for (i=0; i<*n; i++) solverData->x_new[i]=x[i]-lambda*solverData->x_increment[i]; else for (i=0; i<*n; i++) solverData->x_new[i]=x[i]-solverData->x_increment[i]; /* calculate new function values */ (*f)(n, solverData->x_new, fvec, userdata, 1); solverData->nfev++; (*k)++; break; } } messageClose(LOG_NLS_V); }
/* \fn printVector(int logLevel, double* y, DATA* data, double time) * * \param [in] [logLevel] * \param [in] [name] * \param [in] [vec] * \param [in] [size] * \param [in] [time] * * This function outputs a vector of size * */ int printVector(int logLevel, const char* name, double* vec, int n, double time) { int i; infoStreamPrint(logLevel, 1, "%s at time=%g", name, time); for(i=0; i<n; ++i) { infoStreamPrint(logLevel, 0, "%d. %g", i+1, vec[i]); } messageClose(logLevel); return 0; }
/* \fn printCurrentStatesVector(int logLevel, double* y, DATA* data, double time) * * \param [in] [logLevel] * \param [in] [states] * \param [in] [data] * \param [in] [time] * * This function outputs states vector. * */ int printCurrentStatesVector(int logLevel, double* states, DATA* data, double time) { int i; infoStreamPrint(logLevel, 1, "states at time=%g", time); for(i=0;i<data->modelData->nStates;++i) { infoStreamPrint(logLevel, 0, "%d. %s = %g", i+1, data->modelData->realVarsData[i].info.name, states[i]); } messageClose(logLevel); return 0; }
void* embedded_server_load_functions(const char *server_name) { void *dll, *funcInit, *funcWaitForStep, *funcDeinit, *funcUpdate; if (NULL==server_name || 0==strcmp("none", server_name)) { return NULL; } if (0==strcmp("opc-ua", server_name)) { server_name = "libomopcua" DLL_EXT; } else if (0==strcmp("opc-da", server_name)) { #if defined(UPC_DA) server_name = "libomopcda" DLL_EXT; #else errorStreamPrint(LOG_DEBUG, 0, "OPC DA interface is not available on this platform (requires WIN32)"); MMC_THROW(); #endif } infoStreamPrint(LOG_DEBUG, 0, "Try to load embedded server %s", server_name); dll = dlopen(server_name, RTLD_LAZY); if (dll == NULL) { errorStreamPrint(LOG_DEBUG, 0, "Failed to load shared object %s: %s\n", server_name, dlerror()); MMC_THROW(); } funcInit = dlsym(dll, "omc_embedded_server_init"); if (!funcInit) { errorStreamPrint(LOG_DEBUG, 0, "Failed to load function omc_embedded_server_init: %s\n", dlerror()); MMC_THROW(); } funcWaitForStep = dlsym(dll, "omc_wait_for_step"); if (!funcWaitForStep) { errorStreamPrint(LOG_DEBUG, 0, "Failed to load function omc_wait_for_step: %s\n", dlerror()); MMC_THROW(); } funcDeinit = dlsym(dll, "omc_embedded_server_deinit"); if (!funcDeinit) { errorStreamPrint(LOG_DEBUG, 0, "Failed to load function omc_embedded_server_deinit: %s\n", dlerror()); MMC_THROW(); } funcUpdate = dlsym(dll, "omc_embedded_server_update"); if (!funcUpdate) { errorStreamPrint(LOG_DEBUG, 0, "Failed to load function omc_embedded_server_update: %s\n", dlerror()); MMC_THROW(); } embedded_server_init = funcInit; wait_for_step = funcWaitForStep; embedded_server_deinit = funcDeinit; embedded_server_update = funcUpdate; infoStreamPrint(LOG_DEBUG, 0, "Loaded embedded server"); return dll; }
/*! \fn printSparseStructure * * prints sparse structure of jacobian A * * \param [in] [data] * \param [in] [stream] * * \author lochel */ void printSparseStructure(DATA *data, int stream) { const int index = data->callback->INDEX_JAC_A; unsigned int row, col, i, j; /* Will crash with a static size array */ char *buffer = NULL; if (!ACTIVE_STREAM(stream)) return; buffer = (char*)omc_alloc_interface.malloc(sizeof(char)* 2*data->simulationInfo.analyticJacobians[index].sizeCols + 4); infoStreamPrint(stream, 1, "sparse structure of jacobian A [size: %ux%u]", data->simulationInfo.analyticJacobians[index].sizeRows, data->simulationInfo.analyticJacobians[index].sizeCols); infoStreamPrint(stream, 0, "%u nonzero elements", data->simulationInfo.analyticJacobians[index].sparsePattern.numberOfNoneZeros); /* sprintf(buffer, ""); for(row=0; row < data->simulationInfo.analyticJacobians[index].sizeRows; row++) sprintf(buffer, "%s%u ", buffer, data->simulationInfo.analyticJacobians[index].sparsePattern.leadindex[row]); infoStreamPrint(stream, 0, "leadindex: %s", buffer); sprintf(buffer, ""); for(i=0; i < data->simulationInfo.analyticJacobians[index].sparsePattern.numberOfNoneZeros; i++) sprintf(buffer, "%s%u ", buffer, data->simulationInfo.analyticJacobians[index].sparsePattern.index[i]); infoStreamPrint(stream, 0, "index: %s", buffer); */ infoStreamPrint(stream, 1, "transposed sparse structure (rows: states)"); i=0; for(row=0; row < data->simulationInfo.analyticJacobians[index].sizeRows; row++) { j=0; for(col=0; i < data->simulationInfo.analyticJacobians[index].sparsePattern.leadindex[row]; col++) { if(data->simulationInfo.analyticJacobians[index].sparsePattern.index[i] == col) { buffer[j++] = '*'; ++i; } else { buffer[j++] = ' '; } buffer[j++] = ' '; } buffer[j] = '\0'; infoStreamPrint(stream, 0, "%s", buffer); } messageClose(stream); messageClose(stream); }
static void printMatrixCSR(int* Ap, int* Ai, double* Ax, int n) { int i, j, k; char *buffer = (char*)malloc(sizeof(char)*n*15); k = 0; for (i = 0; i < n; i++) { buffer[0] = 0; for (j = 0; j < n; j++) { if ((k < Ap[i + 1]) && (Ai[k] == j)) { sprintf(buffer, "%s %5.2g ", buffer, Ax[k]); k++; } else { sprintf(buffer, "%s %5.2g ", buffer, 0.0); } } infoStreamPrint(LOG_LS_V, 0, "%s", buffer); } free(buffer); }
static void printMatrixCSC(int* Ap, int* Ai, double* Ax, int n) { int i, j, k, l; char **buffer = (char**)malloc(sizeof(char*)*n); for (l=0; l<n; l++) { buffer[l] = (char*)malloc(sizeof(char)*n*20); buffer[l][0] = 0; } k = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if ((k < Ap[i + 1]) && (Ai[k] == j)) { sprintf(buffer[j], "%s %5g ", buffer[j], Ax[k]); k++; } else { sprintf(buffer[j], "%s %5g ", buffer[j], 0.0); } } } for (l = 0; l < n; l++) { infoStreamPrint(LOG_LS_V, 0, "%s", buffer[l]); free(buffer[l]); } free(buffer); }
static int simulationUpdate(DATA* data, threadData_t *threadData, SOLVER_INFO* solverInfo) { prefixedName_updateContinuousSystem(data, threadData); if (solverInfo->solverMethod == S_SYM_IMP_EULER) data->callback->symEulerUpdate(data, solverInfo->solverStepSize); saveZeroCrossings(data, threadData); messageClose(LOG_SOLVER); /***** Event handling *****/ if (measure_time_flag) rt_tick(SIM_TIMER_EVENT); int syncRet = handleTimers(data, threadData, solverInfo); int syncRet1; do { int eventType = checkEvents(data, threadData, solverInfo->eventLst, &(solverInfo->currentTime), solverInfo); if(eventType > 0 || syncRet == 2) /* event */ { threadData->currentErrorStage = ERROR_EVENTHANDLING; infoStreamPrint(LOG_EVENTS, 1, "%s event at time=%.12g", eventType == 1 ? "time" : "state", solverInfo->currentTime); /* prevent emit if noEventEmit flag is used */ if (!(omc_flag[FLAG_NOEVENTEMIT])) /* output left limit */ sim_result.emit(&sim_result, data, threadData); handleEvents(data, threadData, solverInfo->eventLst, &(solverInfo->currentTime), solverInfo); messageClose(LOG_EVENTS); threadData->currentErrorStage = ERROR_SIMULATION; solverInfo->didEventStep = 1; overwriteOldSimulationData(data); } else /* no event */ { solverInfo->laststep = solverInfo->currentTime; solverInfo->didEventStep = 0; } if (measure_time_flag) rt_accumulate(SIM_TIMER_EVENT); /***** End event handling *****/ /***** check state selection *****/ if (stateSelection(data, threadData, 1, 1)) { /* if new set is calculated reinit the solver */ solverInfo->didEventStep = 1; overwriteOldSimulationData(data); } /* Check for warning of variables out of range assert(min<x || x>xmax, ...)*/ data->callback->checkForAsserts(data, threadData); storePreValues(data); storeOldValues(data); syncRet1 = handleTimers(data, threadData, solverInfo); syncRet = syncRet1 == 0 ? syncRet : syncRet1; } while (syncRet1); return syncRet; }
/*! \fn freeNonlinearSystems * * This function frees memory of nonlinear systems. * * \param [ref] [data] */ int freeNonlinearSystems(DATA *data, threadData_t *threadData) { TRACE_PUSH int i; NONLINEAR_SYSTEM_DATA* nonlinsys = data->simulationInfo.nonlinearSystemData; struct csvStats* stats; infoStreamPrint(LOG_NLS, 1, "free non-linear system solvers"); for(i=0; i<data->modelData.nNonLinearSystems; ++i) { free(nonlinsys[i].nlsx); free(nonlinsys[i].nlsxExtrapolation); free(nonlinsys[i].nlsxOld); free(nonlinsys[i].nominal); free(nonlinsys[i].min); free(nonlinsys[i].max); #if !defined(OMC_MINIMAL_RUNTIME) if (data->simulationInfo.nlsCsvInfomation) { stats = nonlinsys[i].csvData; omc_write_csv_free(stats->callStats); omc_write_csv_free(stats->iterStats); } #endif /* free solver data */ switch(data->simulationInfo.nlsMethod) { #if !defined(OMC_MINIMAL_RUNTIME) case NLS_HYBRID: freeHybrdData(&nonlinsys[i].solverData); break; case NLS_KINSOL: nls_kinsol_free(&nonlinsys[i]); break; case NLS_NEWTON: freeNewtonData(&nonlinsys[i].solverData); break; #endif case NLS_HOMOTOPY: freeHomotopyData(&nonlinsys[i].solverData); break; #if !defined(OMC_MINIMAL_RUNTIME) case NLS_MIXED: freeHomotopyData(&((struct dataNewtonAndHybrid*) nonlinsys[i].solverData)->newtonData); freeHybrdData(&((struct dataNewtonAndHybrid*) nonlinsys[i].solverData)->hybridData); break; #endif default: throwStreamPrint(threadData, "unrecognized nonlinear solver"); } free(nonlinsys[i].solverData); } messageClose(LOG_NLS); TRACE_POP return 0; }
void printMatrixCSC(int* Ap, int* Ai, double* Ax, int n) { int i, j, k, l; char buffer[400][4096] = {0}; k = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if ((k < Ap[i + 1]) && (Ai[k] == j)) { sprintf(buffer[j], "%s %5g ", buffer[j], Ax[k]); k++; } else { sprintf(buffer[j], "%s %5g ", buffer[j], 0.0); } } } for (l = 0; l < n; l++) { infoStreamPrint(LOG_LS_V, 0, "%s", buffer[l]); } }
/*! \fn getAnalyticalJacobian * * function calculates a jacobian matrix by * numerical method finite differences * * \param [ref] [data] * \param [out] [jac] * * \author wbraun * */ static int getNumericalJacobian(struct dataAndSys* dataAndSysNum, double* jac, const double* x, double* f) { struct dataAndSys *dataSys = (struct dataAndSys*) dataAndSysNum; NONLINEAR_SYSTEM_DATA* systemData = &(dataSys->data->simulationInfo->nonlinearSystemData[dataSys->sysNumber]); DATA_HYBRD* solverData = (DATA_HYBRD*)(systemData->solverData); double delta_h = sqrt(solverData->epsfcn); double delta_hh, delta_hhh, deltaInv; integer iflag = 1; int i, j, l; memcpy(solverData->xSave, x, solverData->n*sizeof(double)); for(i = 0; i < solverData->n ; ++i) { delta_hhh = solverData->epsfcn * f[i]; delta_hh = fmax(delta_h * fmax(fabs(x[i]), fabs(delta_hhh)), delta_h); delta_hh = ((f[i] >= 0) ? delta_hh : -delta_hh); delta_hh = x[i] + delta_hh - x[i]; deltaInv = 1. / delta_hh; solverData->xSave[i] = x[i] + delta_hh; infoStreamPrint(LOG_NLS_JAC, 0, "%d. %s = %f (delta_hh = %f)", i+1, modelInfoGetEquation(&dataSys->data->modelData->modelDataXml,systemData->equationIndex).vars[i], solverData->xSave[i], delta_hh); wrapper_fvec_hybrj(&solverData->n, (const double*) solverData->xSave, solverData->fvecSave, solverData->fjacobian, &solverData->ldfjac, &iflag, dataSys); for(j = 0; j < solverData->n; ++j) { l = i*solverData->n+j; solverData->fjacobian[l] = jac[l] = (solverData->fvecSave[j] - f[j]) * deltaInv; } solverData->xSave[i] = x[i]; } return 0; }
/*! \fn setAMatrix * * ??? desc ??? * * \param [ref] [newEnable] * \param [ref] [nCandidates] * \param [ref] [nStates] * \param [ref] [Ainfo] * \param [ref] [states] * \param [ref] [statecandidates] * \param [ref] [data] */ static void setAMatrix(modelica_integer* newEnable, modelica_integer nCandidates, modelica_integer nStates, VAR_INFO* Ainfo, VAR_INFO** states, VAR_INFO** statecandidates, DATA *data) { TRACE_PUSH modelica_integer col; modelica_integer row=0; /* clear old values */ unsigned int aid = Ainfo->id - data->modelData->integerVarsData[0].info.id; modelica_integer *A = &(data->localData[0]->integerVars[aid]); memset(A, 0, nCandidates*nStates*sizeof(modelica_integer)); for(col=0; col<nCandidates; col++) { if(newEnable[col]==2) { unsigned int firstrealid = data->modelData->realVarsData[0].info.id; unsigned int id = statecandidates[col]->id-firstrealid; unsigned int sid = states[row]->id-firstrealid; infoStreamPrint(LOG_DSS, 0, "select %s", statecandidates[col]->name); /* set A[row, col] */ A[row*nCandidates + col] = 1; /* reinit state */ data->localData[0]->realVars[sid] = data->localData[0]->realVars[id]; row++; } } TRACE_POP }
/*! \fn freeMixedSystems * * Thi function frees memory of mixed systems. * * \param [ref] [data] */ int freeMixedSystems(DATA *data, threadData_t *threadData) { int i; MIXED_SYSTEM_DATA* system = data->simulationInfo->mixedSystemData; infoStreamPrint(LOG_NLS, 1, "free mixed system solvers"); for(i=0;i<data->modelData->nMixedSystems;++i) { free(system[i].iterationVarsPtr); free(system[i].iterationPreVarsPtr); /* allocate solver data */ switch(data->simulationInfo->mixedMethod) { case MIXED_SEARCH: freeMixedSearchData(&system[i].solverData); break; default: throwStreamPrint(threadData, "unrecognized mixed solver"); } free(system[i].solverData); } messageClose(LOG_NLS); return 0; }
void debugIntLS(int logName, char* message, int value) { if(ACTIVE_STREAM(logName)) { infoStreamPrint(logName, 1, "%s %d", message, value); messageClose(logName); } }
void debugStringLS(int logName, char* message) { if(ACTIVE_STREAM(logName)) { infoStreamPrint(logName, 1, "%s", message); messageClose(logName); } }
/*! \fn printStatus * * \param [in] [solverData] * \param [in] [nfunc_evals] * \param [in] [xerror] * \param [in] [xerror_scaled] * \param [in] [logLevel] * * \author wbraun */ static void printStatus(DATA *data, DATA_HYBRD *solverData, int eqSystemNumber, const int *nfunc_evals, const double *xerror, const double *xerror_scaled, const int logLevel) { long i; if (!ACTIVE_STREAM(logLevel)) return; infoStreamPrint(logLevel, 1, "nls status"); infoStreamPrint(logLevel, 1, "variables"); for(i=0; i<solverData->n; i++) infoStreamPrint(logLevel, 0, "[%ld] %s = %.20e\n - scaling factor internal = %.16e\n" " - scaling factor external = %.16e", i+1, modelInfoGetEquation(&data->modelData->modelDataXml,eqSystemNumber).vars[i], solverData->x[i], solverData->diag[i], solverData->xScalefactors[i]); messageClose(logLevel); infoStreamPrint(logLevel, 1, "functions"); for(i=0; i<solverData->n; i++) infoStreamPrint(logLevel, 0, "res[%ld] = %.20e [scaling factor = %.16e]", i+1, solverData->fvec[i], solverData->resScaling[i]); messageClose(logLevel); infoStreamPrint(logLevel, 1, "statistics"); infoStreamPrint(logLevel, 0, "nfunc = %d\nerror = %.20e\nerror_scaled = %.20e", *nfunc_evals, *xerror, *xerror_scaled); messageClose(logLevel); messageClose(logLevel); }
/* * function calculates a jacobian matrix */ static int nlsDenseJac(long int N, N_Vector vecX, N_Vector vecFX, DlsMat Jac, void *userData, N_Vector tmp1, N_Vector tmp2) { NLS_KINSOL_USERDATA *kinsolUserData = (NLS_KINSOL_USERDATA*) userData; DATA* data = kinsolUserData->data; threadData_t *threadData = kinsolUserData->threadData; int sysNumber = kinsolUserData->sysNumber; NONLINEAR_SYSTEM_DATA *nlsData = &(data->simulationInfo->nonlinearSystemData[sysNumber]); NLS_KINSOL_DATA* kinsolData = (NLS_KINSOL_DATA*) nlsData->solverData; /* prepare variables */ double *x = N_VGetArrayPointer(vecX); double *fx = N_VGetArrayPointer(vecFX); double *xScaling = NV_DATA_S(kinsolData->xScale); double *fRes = NV_DATA_S(kinsolData->fRes); double xsave, xscale, sign; double delta_hh; const double delta_h = sqrt(DBL_EPSILON*2e1); long int i,j; /* performance measurement */ rt_ext_tp_tick(&nlsData->jacobianTimeClock); for(i = 0; i < N; i++) { xsave = x[i]; delta_hh = delta_h * (fabs(xsave) + 1.0); if ((xsave + delta_hh >= nlsData->max[i])) delta_hh *= -1; x[i] += delta_hh; /* Calculate difference quotient */ nlsKinsolResiduals(vecX, kinsolData->fRes, userData); /* Calculate scaled difference quotient */ delta_hh = 1. / delta_hh; for(j = 0; j < N; j++) { DENSE_ELEM(Jac, j, i) = (fRes[j] - fx[j]) * delta_hh; } x[i] = xsave; } /* debug */ if (ACTIVE_STREAM(LOG_NLS_JAC)){ infoStreamPrint(LOG_NLS_JAC, 0, "##KINSOL## omc dense matrix."); PrintMat(Jac); } /* performance measurement and statistics */ nlsData->jacobianTime += rt_ext_tp_tock(&(nlsData->jacobianTimeClock)); nlsData->numberOfJEval++; return 0; }
int MultFive_checkForDiscreteChanges(DATA *data) { int needToIterate = 0; infoStreamPrint(LOG_EVENTS_V, 1, "check for discrete changes"); if (ACTIVE_STREAM(LOG_EVENTS_V)) messageClose(LOG_EVENTS_V); return needToIterate; }
void printLisMatrixCSR(LIS_MATRIX A, int n) { char buffer[16384]; int i, j; /* A matrix */ infoStreamPrint(LOG_LS_V, 1, "A matrix [%dx%d] nnz = %d ", n, n, A->nnz); for(i=0; i<n; i++) { buffer[0] = 0; for(j=A->ptr[i]; j<A->ptr[i+1]; j++){ sprintf(buffer, "%s(%d,%d,%g) ", buffer, i, A->index[j], A->value[j]); } infoStreamPrint(LOG_LS_V, 0, "%s", buffer); } messageClose(LOG_LS_V); }
static void checkSimulationTerminated(DATA* data, SOLVER_INFO* solverInfo) { if(terminationTerminate) { printInfo(stdout, TermInfo); fputc('\n', stdout); infoStreamPrint(LOG_STDOUT, 0, "Simulation call terminate() at time %f\nMessage : %s", data->localData[0]->timeValue, TermMsg); data->simulationInfo.stopTime = solverInfo->currentTime; } }
static int simulationStep(DATA* data, threadData_t *threadData, SOLVER_INFO* solverInfo) { SIMULATION_INFO *simInfo = &(data->simulationInfo); infoStreamPrint(LOG_SOLVER, 1, "call solver from %g to %g (stepSize: %.15g)", solverInfo->currentTime, solverInfo->currentTime + solverInfo->currentStepSize, solverInfo->currentStepSize); if(0 != strcmp("ia", data->simulationInfo.outputFormat)) { communicateStatus("Running", (solverInfo->currentTime - simInfo->startTime)/(simInfo->stopTime - simInfo->startTime)); } return solver_main_step(data, threadData, solverInfo); }
static int nlsKinsolConfigPrint(NLS_KINSOL_DATA *kinsolData, NONLINEAR_SYSTEM_DATA *nlsData) { int retValue; double fNorm; double *xStart = NV_DATA_S(kinsolData->initialGuess); double *xScaling = NV_DATA_S(kinsolData->xScale); DATA* data = kinsolData->userData.data; int eqSystemNumber = nlsData->equationIndex; infoStreamPrint(LOG_NLS_V, 1, "Kinsol Configuration"); _omc_printVectorWithEquationInfo(_omc_createVector(kinsolData->size, xStart), "Initial guess values", LOG_NLS_V, modelInfoGetEquation(&data->modelData->modelDataXml,eqSystemNumber)); _omc_printVectorWithEquationInfo(_omc_createVector(kinsolData->size, xScaling), "xScaling", LOG_NLS_V, modelInfoGetEquation(&data->modelData->modelDataXml,eqSystemNumber)); infoStreamPrint(LOG_NLS_V, 0, "KINSOL F tolerance: %g", (*(KINMem)kinsolData->kinsolMemory).kin_fnormtol); infoStreamPrint(LOG_NLS_V, 0, "KINSOL minimal step size %g", (*(KINMem)kinsolData->kinsolMemory).kin_scsteptol); infoStreamPrint(LOG_NLS_V, 0, "KINSOL max iterations %d", 20*kinsolData->size); infoStreamPrint(LOG_NLS_V, 0, "KINSOL strategy %d", kinsolData->kinsolStrategy); infoStreamPrint(LOG_NLS_V, 0, "KINSOL current retry %d", kinsolData->retries); messageClose(LOG_NLS_V); return retValue; }
void debugVectorDoubleLS(int logName, char* vectorName, double* vector, int n) { if(ACTIVE_STREAM(logName)) { int i; char buffer[4096]; infoStreamPrint(logName, 1, "%s [%d-dim]", vectorName, n); buffer[0] = 0; for(i=0; i<n; i++) { if (vector[i]<-1e+300) sprintf(buffer, "%s -INF ", buffer); else if (vector[i]>1e+300) sprintf(buffer, "%s +INF ", buffer); else sprintf(buffer, "%s%16.8g ", buffer, vector[i]); } infoStreamPrint(logName, 0, "%s", buffer); messageClose(logName); } }
static void nlsKinsolJacSumSparse(SlsMat mat) { int i,j; double sum; for(i=0; i<mat->N; ++i){ sum = 0; for(j=mat->colptrs[i]; j<mat->colptrs[i+1];++j){ sum += fabs(mat->data[j]); } infoStreamPrint(LOG_NLS_JAC, 0, "row %d jac sum = %g", i, sum); } }