static void PrintFinalStats(void *kmem) { long int nni, nfe, nje, nfeD; long int lenrwB, leniwB; int flag; /* Main solver statistics */ flag = KINGetNumNonlinSolvIters(kmem, &nni); check_flag(&flag, "KINGetNumNonlinSolvIters", 1); flag = KINGetNumFuncEvals(kmem, &nfe); check_flag(&flag, "KINGetNumFuncEvals", 1); /* Band linear solver statistics */ flag = KINDlsGetNumJacEvals(kmem, &nje); check_flag(&flag, "KINDlsGetNumJacEvals", 1); flag = KINDlsGetNumFuncEvals(kmem, &nfeD); check_flag(&flag, "KINDlsGetNumFuncEvals", 1); /* Band linear solver workspace size */ flag = KINDlsGetWorkSpace(kmem, &lenrwB, &leniwB); check_flag(&flag, "KINDlsGetWorkSpace", 1); printf("\nFinal Statistics.. \n\n"); printf("nni = %6ld nfe = %6ld \n", nni, nfe); printf("nje = %6ld nfeB = %6ld \n", nje, nfeD); printf("\n"); printf("lenrwB = %6ld leniwB = %6ld \n", lenrwB, leniwB); }
static void PrintFinalStats(void *kmem) { long int nni, nfe, nje, nfeD; int flag; flag = KINGetNumNonlinSolvIters(kmem, &nni); check_flag(&flag, "KINGetNumNonlinSolvIters", 1); flag = KINGetNumFuncEvals(kmem, &nfe); check_flag(&flag, "KINGetNumFuncEvals", 1); flag = KINDlsGetNumJacEvals(kmem, &nje); check_flag(&flag, "KINDlsGetNumJacEvals", 1); flag = KINDlsGetNumFuncEvals(kmem, &nfeD); check_flag(&flag, "KINDlsGetNumFuncEvals", 1); printf("Final Statistics:\n"); printf(" nni = %5ld nfe = %5ld \n", nni, nfe); printf(" nje = %5ld nfeD = %5ld \n", nje, nfeD); }
void FKIN_SOL(realtype *uu, int *globalstrategy, realtype *uscale , realtype *fscale, int *ier) { N_Vector uuvec, uscalevec, fscalevec; *ier = 0; uuvec = uscalevec = fscalevec = NULL; uuvec = F2C_KINSOL_vec; N_VSetArrayPointer(uu, uuvec); uscalevec = NULL; uscalevec = N_VCloneEmpty(F2C_KINSOL_vec); if (uscalevec == NULL) { *ier = -4; /* KIN_MEM_FAIL */ return; } N_VSetArrayPointer(uscale, uscalevec); fscalevec = NULL; fscalevec = N_VCloneEmpty(F2C_KINSOL_vec); if (fscalevec == NULL) { N_VDestroy(uscalevec); *ier = -4; /* KIN_MEM_FAIL */ return; } N_VSetArrayPointer(fscale, fscalevec); /* Call main solver function */ *ier = KINSol(KIN_kinmem, uuvec, *globalstrategy, uscalevec, fscalevec); N_VSetArrayPointer(NULL, uuvec); N_VSetArrayPointer(NULL, uscalevec); N_VDestroy(uscalevec); N_VSetArrayPointer(NULL, fscalevec); N_VDestroy(fscalevec); /* load optional outputs into iout[] and rout[] */ KINGetWorkSpace(KIN_kinmem, &KIN_iout[0], &KIN_iout[1]); /* LENRW & LENIW */ KINGetNumNonlinSolvIters(KIN_kinmem, &KIN_iout[2]); /* NNI */ KINGetNumFuncEvals(KIN_kinmem, &KIN_iout[3]); /* NFE */ KINGetNumBetaCondFails(KIN_kinmem, &KIN_iout[4]); /* NBCF */ KINGetNumBacktrackOps(KIN_kinmem, &KIN_iout[5]); /* NBCKTRK */ KINGetFuncNorm(KIN_kinmem, &KIN_rout[0]); /* FNORM */ KINGetStepLength(KIN_kinmem, &KIN_rout[1]); /* SSTEP */ switch(KIN_ls) { case KIN_LS_DENSE: case KIN_LS_BAND: case KIN_LS_LAPACKDENSE: case KIN_LS_LAPACKBAND: KINDlsGetWorkSpace(KIN_kinmem, &KIN_iout[6], &KIN_iout[7]); /* LRW & LIW */ KINDlsGetLastFlag(KIN_kinmem, (int *) &KIN_iout[8]); /* LSTF */ KINDlsGetNumFuncEvals(KIN_kinmem, &KIN_iout[9]); /* NFE */ KINDlsGetNumJacEvals(KIN_kinmem, &KIN_iout[10]); /* NJE */ case KIN_LS_SPTFQMR: case KIN_LS_SPBCG: case KIN_LS_SPGMR: KINSpilsGetWorkSpace(KIN_kinmem, &KIN_iout[6], &KIN_iout[7]); /* LRW & LIW */ KINSpilsGetLastFlag(KIN_kinmem, (int *) &KIN_iout[8]); /* LSTF */ KINSpilsGetNumFuncEvals(KIN_kinmem, &KIN_iout[9]); /* NFE */ KINSpilsGetNumJtimesEvals(KIN_kinmem, &KIN_iout[10]); /* NJE */ KINSpilsGetNumPrecEvals(KIN_kinmem, &KIN_iout[11]); /* NPE */ KINSpilsGetNumPrecSolves(KIN_kinmem, &KIN_iout[12]); /* NPS */ KINSpilsGetNumLinIters(KIN_kinmem, &KIN_iout[13]); /* NLI */ KINSpilsGetNumConvFails(KIN_kinmem, &KIN_iout[14]); /* NCFL */ break; } return; }
/*! \fn kinsol_initialization * * \param [ref] [data] * \param [in] [initData] * \param [ref] [initialResiduals] * * \author lochel */ int kinsol_initialization(DATA* data, INIT_DATA* initData, int useScaling) { long i, indz; KINSOL_DATA* kdata = NULL; double fnormtol = 1.e-9; /* function tolerance */ double scsteptol = 1.e-9; /* step tolerance */ long int nni, nfe, nje, nfeD; N_Vector z = NULL; N_Vector sVars = NULL; N_Vector sEqns = NULL; N_Vector c = NULL; int glstr = KIN_NONE; /* globalization strategy applied to the Newton method. It must be one of KIN_NONE or KIN_LINESEARCH */ long int mset = 1; /* maximum number of nonlinear iterations without a call to the preconditioner setup function. Pass 0 to indicate the default [10]. */ void *kmem = NULL; int error_code = -1; ASSERT(data->modelData.nInitResiduals == initData->nz, "The number of initial equations are not consistent with the number of unfixed variables. Select a different initialization."); do /* Try it first with KIN_NONE. If that fails, try it with KIN_LINESEARCH. */ { if(mset == 1 && glstr == KIN_NONE) DEBUG_INFO(LOG_INIT, "using exact Newton"); else if(mset == 1) DEBUG_INFO(LOG_INIT, "using exact Newton with line search"); else if(glstr == KIN_NONE) DEBUG_INFO(LOG_INIT, "using modified Newton"); else DEBUG_INFO(LOG_INIT, "using modified Newton with line search"); DEBUG_INFO_AL1(LOG_INIT, "| mset = %10ld", mset); DEBUG_INFO_AL1(LOG_INIT, "| function tolerance = %10.6g", fnormtol); DEBUG_INFO_AL1(LOG_INIT, "| step tolerance = %10.6g", scsteptol); kdata = (KINSOL_DATA*)malloc(sizeof(KINSOL_DATA)); ASSERT(kdata, "out of memory"); kdata->initData = initData; kdata->data = data; z = N_VNew_Serial(3*initData->nz); ASSERT(z, "out of memory"); sVars = N_VNew_Serial(3*initData->nz); ASSERT(sVars, "out of memory"); sEqns = N_VNew_Serial(3*initData->nz); ASSERT(sEqns, "out of memory"); c = N_VNew_Serial(3*initData->nz); ASSERT(c, "out of memory"); /* initial guess */ for(i=0; i<initData->nz; ++i) { NV_Ith_S(z, i) = initData->start[i]; NV_Ith_S(z, initData->nInitResiduals+2*i+0) = NV_Ith_S(z, i) - initData->min[i]; NV_Ith_S(z, initData->nInitResiduals+2*i+1) = NV_Ith_S(z, i) - initData->max[i]; } kdata->useScaling=useScaling; if(useScaling) { computeInitialResidualScalingCoefficients(data, initData); for(i=0; i<initData->nz; ++i) { NV_Ith_S(sVars, i) = 1.0 / (initData->nominal[i] == 0.0 ? 1.0 : initData->nominal[i]); NV_Ith_S(sVars, initData->nInitResiduals+2*i+0) = NV_Ith_S(sVars, i); NV_Ith_S(sVars, initData->nInitResiduals+2*i+1) = NV_Ith_S(sVars, i); NV_Ith_S(sEqns, i) = 1.0 / (initData->residualScalingCoefficients[i] == 0.0 ? 1.0 : initData->residualScalingCoefficients[i]); NV_Ith_S(sEqns, initData->nInitResiduals+2*i+0) = NV_Ith_S(sEqns, i); NV_Ith_S(sEqns, initData->nInitResiduals+2*i+1) = NV_Ith_S(sEqns, i); } } else { N_VConst_Serial(1.0, sVars); /* no scaling */ N_VConst_Serial(1.0, sEqns); /* no scaling */ } for(i=0; i<initData->nz; ++i) { NV_Ith_S(c, i) = 0.0; /* no constraint on z[i] */ NV_Ith_S(c, initData->nInitResiduals+2*i+0) = 1.0; NV_Ith_S(c, initData->nInitResiduals+2*i+1) = -1.0; } kmem = KINCreate(); ASSERT(kmem, "out of memory"); KINSetErrHandlerFn(kmem, kinsol_errorHandler, NULL); KINSetUserData(kmem, kdata); KINSetConstraints(kmem, c); KINSetFuncNormTol(kmem, fnormtol); KINSetScaledStepTol(kmem, scsteptol); KINInit(kmem, kinsol_residuals, z); /* Call KINDense to specify the linear solver */ KINDense(kmem, 3*initData->nz); KINSetMaxSetupCalls(kmem, mset); /*KINSetNumMaxIters(kmem, 2000);*/ globalInitialResiduals = initData->initialResiduals; error_code = KINSol(kmem, /* KINSol memory block */ z, /* initial guess on input; solution vector */ glstr, /* global stragegy choice */ sVars, /* scaling vector, for the variable cc */ sEqns); /* scaling vector for function values fval */ globalInitialResiduals = NULL; KINGetNumNonlinSolvIters(kmem, &nni); KINGetNumFuncEvals(kmem, &nfe); KINDlsGetNumJacEvals(kmem, &nje); KINDlsGetNumFuncEvals(kmem, &nfeD); DEBUG_INFO(LOG_INIT, "final kinsol statistics"); DEBUG_INFO_AL1(LOG_INIT, "| KINGetNumNonlinSolvIters = %5ld", nni); DEBUG_INFO_AL1(LOG_INIT, "| KINGetNumFuncEvals = %5ld", nfe); DEBUG_INFO_AL1(LOG_INIT, "| KINDlsGetNumJacEvals = %5ld", nje); DEBUG_INFO_AL1(LOG_INIT, "| KINDlsGetNumFuncEvals = %5ld", nfeD); /* Free memory */ N_VDestroy_Serial(z); N_VDestroy_Serial(sVars); N_VDestroy_Serial(sEqns); N_VDestroy_Serial(c); KINFree(&kmem); free(kdata); if(error_code < 0) glstr++; /* try next globalization strategy */ }while(error_code < 0 && glstr <= KIN_LINESEARCH); /* debug output */ indz = 0; DEBUG_INFO(LOG_INIT, "solution"); for(i=0; i<data->modelData.nStates; ++i) if(data->modelData.realVarsData[i].attribute.fixed==0) DEBUG_INFO_AL2(LOG_INIT, "| %s = %g", initData->name[indz++], data->localData[0]->realVars[i]); for(i=0; i<data->modelData.nParametersReal; ++i) if(data->modelData.realParameterData[i].attribute.fixed == 0) DEBUG_INFO_AL2(LOG_INIT, "| %s = %g", initData->name[indz++], data->simulationInfo.realParameter[i]); if(error_code < 0) THROW("kinsol failed. see last warning. use [-lv LOG_INIT] for more output."); return 0; }