void MFMVMulT(MFNSpace R, MFNKMatrix thisMatrix,MFNVector u,MFKVector s,MFErrorHandler e) { static char RoutineName[]={"MFMVMulT"}; int i,j; double t; MFNVector col; int verbose=0; #ifdef MFALLOWVERBOSE if(verbose){printf("%s, n=%d\n",RoutineName,thisMatrix->n);fflush(stdout);} #endif for(j=0;j<thisMatrix->k;j++) { col=MFMColumn(thisMatrix,j,e); t=MFNSpaceInner(R,col,u,e); #ifdef MFALLOWVERBOSE if(verbose) { printf(" s[%d]=",j);MFPrintNVector(stdout,col,e); printf(".");MFPrintNVector(stdout,u,e); printf("=%lf\n",t); } #endif MFKVSetC(s,j,t,e); MFFreeNVector(col,e); } return; }
double MFScaleLOCA(int n,int k,MFNVector u,MFNKMatrix Phi,void *d, MFErrorHandler err) { LOCAData* data = (LOCAData *)d; if (data->radius < 0.0) { data->radius = 0.0; data->minRadius = 0.0; data->maxRadius = 0.0; std::list<ParamData>::iterator it = data->paramData->begin(); for (int i=0; i<k; i++) { double dpidsj = 0.0; for (int j=0; j<k; j++) { MFNVector tmp = MFMColumn(Phi,j, err); LOCANVectorData* tmp2_data = (LOCANVectorData *) MFNVectorGetData(tmp, err); dpidsj += fabs(tmp2_data->u_ptr->getScalar(i)); MFFreeNVector(tmp, err); } data->radius += it->initialStepSize / dpidsj; data->minRadius += it->minStepSize / dpidsj; data->maxRadius += it->maxStepSize / dpidsj; it++; } data->radius /= (double) k; data->minRadius /= (double) k; data->maxRadius /= (double) k; } else { NOX::StatusTest::StatusType status = data->solver->getStatus(); if (status != NOX::StatusTest::Converged) { data->radius *= 0.7; if (data->radius < data->minRadius) data->globalData->locaErrorCheck->throwError( "MFScaleLOCA()", "Reached minimum radius!"); } else { double numNonlinearSteps = static_cast<double>(data->solver->getNumIterations()); double factor = (data->maxNonlinearIterations - numNonlinearSteps) / (data->maxNonlinearIterations); data->radius *= (1.0 + data->aggressiveness * factor * factor); if (data->radius > data->maxRadius) data->radius = data->maxRadius; } } data->globalData->locaUtils->out() << "radius = " << data->radius << std::endl; return data->radius; }
void MFMVMul(MFNSpace R, MFNKMatrix thisMatrix,MFKVector s,MFNVector u,MFErrorHandler e) { static char RoutineName[]={"MFMVMul"}; int i,j; MFNVector col; MFNVector v; double t; static int verbose=0; #ifdef MFALLOWVERBOSE if(verbose){printf("%s\n",RoutineName);fflush(stdout);} #endif /* Need to use Add and Scale from NSpace */ v=MFCloneNVector(u,e); for(i=0;i<thisMatrix->k;i++) { if(thisMatrix->type==DENSE) col=MFCreateNVectorWithData(thisMatrix->n,thisMatrix->data+i*thisMatrix->n,e); else col=MFMColumn(thisMatrix,i,e); #ifdef MFNOCONFIDENCE if(col==NULL) { sprintf(MFNKMatrixErrorMsg,"col %d of thisMatrix is NULL"); MFSetError(e,12,RoutineName,MFNKMatrixErrorMsg,__LINE__,__FILE__); return; } #endif t=MFKV_C(s,i,e); if(i==0) { MFNSpaceScale(R,t,col,u,e); }else{ MFNSpaceScale(R,t,col,v,e); MFNSpaceAdd(R,u,v,u,e); } MFFreeNVector(col,e); } MFFreeNVector(v,e); return; }
void MFPrintChart(FILE *fid,MFChart chart, MFErrorHandler e) { static char RoutineName[]={"MFPrintChart"}; MFNVector phi; int i; #ifdef MFNOCONFIDENCE if(fid==NULL) { sprintf(MFPrintErrorMsg,"fid (argument 1) is NULL."); MFSetError(e,12,RoutineName,MFPrintErrorMsg,__LINE__,__FILE__); return; } if(chart==NULL) { sprintf(MFPrintErrorMsg,"Chart (argument 2) is NULL."); MFSetError(e,12,RoutineName,MFPrintErrorMsg,__LINE__,__FILE__); return; } #endif fprintf(fid,"Chart center "); MFPrintNVector(fid,MFChartCenter(chart,e),e); fprintf(fid,"\n"); fprintf(fid," R %lf\n",MFChartRadius(chart,e)); fprintf(fid," Basis for TS "); for(i=0;i<MFChartK(chart,e);i++) { if(i>0)fprintf(fid," "); phi=MFMColumn(MFChartTangentSpace(chart,e),i,e); MFPrintNVector(fid,phi,e);fprintf(fid,"\n"); MFFreeNVector(phi,e); } fprintf(fid," Polytope\n");MFPrintPolytope(fid,MFChartPolytope(chart,e),e); fflush(fid); return; }
int MFProjectLOCA(int n,int k,MFNVector vu0,MFNKMatrix mPhi,MFNVector vu, void *d,int *index, MFErrorHandler err) { static int stepNumber = 1; int i; *index = 1; LOCAData* data = (LOCAData *)d; for (i=0; i<k; i++) { MFNVector tmp = MFMColumn(mPhi,i, err); LOCANVectorData* tmp2_data = (LOCANVectorData *) MFNVectorGetData(tmp, err); data->grp->setPredictorTangentDirection(*(tmp2_data->u_ptr), i); MFFreeNVector(tmp, err); } LOCANVectorData* u0_data = (LOCANVectorData *) MFNVectorGetData(vu0, err); data->grp->setPrevX(*(u0_data->u_ptr)); data->grp->setX(*(u0_data->u_ptr)); for (i=0; i<k; i++) { data->grp->setStepSize(0.0, i); } if (data->globalData->locaUtils->isPrintType(NOX::Utils::StepperIteration)) { data->globalData->locaUtils->out() << "\n" << data->globalData->locaUtils->fill(72, '~') << "\n"; data->globalData->locaUtils->out() << "Start of Continuation Step " << stepNumber <<" : " << std::endl; std::list<ParamData>::iterator it = data->paramData->begin(); for (i=0; i<k; i++) { data->globalData->locaUtils->out() << "\tParameter: " << it->name << " = " << data->globalData->locaUtils->sciformat(data->grp->getContinuationParameter(i)) << std::endl; it++; } data->globalData->locaUtils->out() << data->globalData->locaUtils->fill(72, '~') << "\n" << std::endl; } data->grp->preProcessContinuationStep(LOCA::Abstract::Iterator::Successful); data->grp->computeF(); data->solver->reset(data->grp->getX()); NOX::StatusTest::StatusType status = data->solver->solve(); if (status != NOX::StatusTest::Converged) { if (data->globalData->locaUtils->isPrintType(NOX::Utils::StepperIteration)) { data->globalData->locaUtils->out() << std::endl << data->globalData->locaUtils->fill(72, '~') << std::endl; data->globalData->locaUtils->out() << "Continuation Step Number " << stepNumber << " experienced a convergence failure in\n" << "the nonlinear solver after "<< data->solver->getNumIterations() <<" Iterations\n"; data->globalData->locaUtils->out() << "Value of continuation parameters at failed step:" << std::endl; std::list<ParamData>::iterator it = data->paramData->begin(); for (i=0; i<k; i++) { data->globalData->locaUtils->out() << "\tParameter: " << it->name << " = " << data->globalData->locaUtils->sciformat(data->grp->getContinuationParameter(i)) << std::endl; it++; } data->globalData->locaUtils->out() << data->globalData->locaUtils->fill(72, '~') << std::endl; } data->grp->postProcessContinuationStep(LOCA::Abstract::Iterator::Unsuccessful); return 0; } else { LOCANVectorData* u_data = (LOCANVectorData *) MFNVectorGetData(vu, err); *(Teuchos::rcp_dynamic_cast<NOX::Abstract::Group>(data->grp)) = data->solver->getSolutionGroup(); *(u_data->u_ptr) = data->grp->getX(); /* overloaded deep copy */ data->grp->postProcessContinuationStep(LOCA::Abstract::Iterator::Successful); if (data->globalData->locaUtils->isPrintType(NOX::Utils::StepperIteration)) { data->globalData->locaUtils->out() << "\n" << data->globalData->locaUtils->fill(72, '~') << "\n"; data->globalData->locaUtils->out() << "End of Continuation Step " << stepNumber << " : " << std::endl; std::list<ParamData>::iterator it = data->paramData->begin(); for (i=0; i<k; i++) { data->globalData->locaUtils->out() << "\tParameter: " << it->name << " = " << data->globalData->locaUtils->sciformat(data->grp->getContinuationParameter(i)) << std::endl; it++; } data->globalData->locaUtils->out() << "--> Step Converged in " << data->solver->getNumIterations() <<" Nonlinear Solver Iterations!\n"; data->globalData->locaUtils->out() << data->globalData->locaUtils->fill(72, '~') << "\n" << std::endl; } ++stepNumber; return 1; } }
void MFGramSchmidtReplace(MFNSpace space,MFNKMatrix A,MFNVector phi0, MFNVector phi1,MFErrorHandler e) { static char RoutineName[]={"MFGramSchmidtReplace"}; double *a; int i,j,n; int k; MFNVector u; MFNVector v; MFNVector w; double inner; int zero; double *B=NULL; int sign0,sign1; int verbose=0; /* Remove vector phi0 from the basis, and replaces it with the vector phi1 */ if(verbose){printf("%s\n",RoutineName);fflush(stdout);} n=MFNKMatrixN(A,e); k=MFNKMatrixK(A,e); B=(double*)malloc(k*k*sizeof(double)); #ifndef MFNOSAFETYNET if(B==NULL) { sprintf(MFNKMatrixErrorMsg,"Out of memory, trying to allocate %d bytes",k*k*sizeof(double)); MFSetError(e,12,RoutineName,MFNKMatrixErrorMsg,__LINE__,__FILE__); MFErrorHandlerOutOfMemory(e); return; } #endif if(verbose) { printf("Remove vector phi0 from the basis, and replace it with the vector phi1\n");fflush(stdout); printf(" phi0=0x%8.8x ",phi0);MFPrintNVector(stdout,phi0,e);printf("\n");fflush(stdout); printf(" phi1=0x%8.8x ",phi0);MFPrintNVector(stdout,phi1,e);printf("\n");fflush(stdout); } for(i=0;i<k;i++) { u=MFMColumn(A,i,e); for(j=0;j<k;j++) { v=MFMColumn(A,j,e); B[i+k*j]=MFNSpaceInner(space,u,v,e); MFFreeNVector(v,e); } MFFreeNVector(u,e); } sign0=MFAtlasDet(k,B,e)>0; u=MFMColumn(A,0,e); w=MFCloneNVector(u,e); MFFreeNVector(u,e); zero=-1; for(i=-1;i<k;i++) { if(i>-1)u=MFMColumn(A,i,e); else{u=phi0;MFRefNVector(phi0,e);} inner=MFNSpaceInner(space,u,u,e); #ifdef MFALLOWVERBOSE if(verbose){printf("u_%d\n",i);MFPrintNVector(stdout,u,e);printf("\n");fflush(stdout); printf("<u_%d,u_%d>=%lf\n",i,i,inner);fflush(stdout);} #endif if(inner>1.e-7) { inner=1./sqrt(inner); MFNSpaceScale(space,inner,u,u,e); #ifdef MFALLOWVERBOSE if(verbose){printf("normalized u_%d\n",i);MFPrintNVector(stdout,u,e);printf("\n");fflush(stdout);} #endif }else{ zero=i; #ifdef MFALLOWVERBOSE if(verbose){printf("zero vector u_%d\n",i);MFPrintNVector(stdout,u,e);printf("\n");fflush(stdout);} #endif } for(j=i+1;j<k;j++) { v=MFMColumn(A,j,e); #ifdef MFALLOWVERBOSE if(verbose){printf("v=u_%d\n",j);MFPrintNVector(stdout,v,e);printf("\n");fflush(stdout);} #endif inner=MFNSpaceInner(space,u,v,e); #ifdef MFALLOWVERBOSE if(verbose){printf("inner=<u_%d,u_%d>=%lf\n",i,j,inner);fflush(stdout);} #endif MFNSpaceScale(space,inner,u,w,e); #ifdef MFALLOWVERBOSE if(verbose){printf("w=<u_%d,u_%d>u_%d\n",j,i,i);MFPrintNVector(stdout,w,e);printf("\n");fflush(stdout);} #endif MFNVDiff(v,w,v,e); #ifdef MFALLOWVERBOSE if(verbose){printf("v-w=u_%d-<u_%d,u_%d>u_%d\n",j,j,i,i);MFPrintNVector(stdout,v,e);printf("\n");fflush(stdout);} #endif MFFreeNVector(v,e); } MFFreeNVector(u,e); #ifdef MFALLOWVERBOSE if(verbose){printf("\n");fflush(stdout);} #endif } MFFreeNVector(w,e); if(verbose){printf("done orthnormalizing, line %d, routine %s\n",__LINE__,RoutineName);fflush(stdout);} if(verbose){printf("Delete the zero column (move it to the first position) zero=%d, line %d, routine %s\n",zero,__LINE__,RoutineName);fflush(stdout);} if(zero>1) { for(i=zero;i>-1;i--) { u=MFMColumn(A,i-1,e); v=MFMColumn(A,i,e); MFMSetColumn(A,i,u,e); MFFreeNVector(u,e); MFFreeNVector(v,e); } if(verbose){printf("done removing the zero column line %d, routine %s\n",__LINE__,RoutineName);fflush(stdout);} } /* Replace the zero column with phi1 */ u=MFMColumn(A,0,e); MFFreeNVector(u,e); MFMSetColumn(A,0,phi1,e); if(verbose){printf("Gram-Schmidt on new basis line %d, routine %s\n",__LINE__,RoutineName);fflush(stdout);} MFGramSchmidt(space,A,e); if(verbose){printf("done Gram-Schmidt on new basis line %d, routine %s\n",__LINE__,RoutineName);fflush(stdout);} for(i=0;i<k;i++) { u=MFMColumn(A,i,e); for(j=0;j<k;j++) { v=MFMColumn(A,j,e); B[i+k*j]=MFNSpaceInner(space,u,v,e); MFFreeNVector(v,e); } MFFreeNVector(u,e); } sign1=MFAtlasDet(k,B,e)>0; if(verbose){printf("make Det the same line %d, routine %s\n",__LINE__,RoutineName);fflush(stdout);} if(sign0&&!sign1||sign1&&!sign0) { u=MFMColumn(A,0,e); MFNSpaceScale(space,-1.,u,u,e); MFFreeNVector(u,e); } free(B); if(verbose){printf("done %s\n",RoutineName);fflush(stdout);} return; }
void MFNKMProjectTangentForBranchSwitch(MFNSpace space, MFNKMatrix A,MFNVector phi,MFNKMatrix Phi,MFErrorHandler e) { static char RoutineName[]={"MFNKMProjectTangentForBranchSwitch"}; int i,j; int k,m; MFNVector u,w; double *p; double pmax; MFNVector pi,pj; int verbose=0; #ifdef MFALLOWVERBOSE if(verbose){printf("%s\n",RoutineName);fflush(stdout); printf(" A=");MFPrintNKMatrix(stdout,A,e);fflush(stdout); printf(" phi=");MFPrintNVector(stdout,phi,e);printf("\n");fflush(stdout);} #endif k=MFNKMatrixK(A,e); p=(double*)malloc(k*sizeof(double)); #ifndef MFNOSAFETYNET if(p==NULL) { sprintf(MFNKMatrixErrorMsg,"Out of memory, trying to allocate %d bytes",k*sizeof(double)); MFSetError(e,12,RoutineName,MFNKMatrixErrorMsg,__LINE__,__FILE__); MFErrorHandlerOutOfMemory(e); return; } #endif j=0; pmax=0.; for(i=0;i<k;i++) { u=MFMColumn(A,i,e); p[i]=MFNSpaceInner(space,phi,u,e); if(fabs(p[i])>pmax){pmax=fabs(p[i]);j=i;} MFFreeNVector(u,e); #ifdef MFALLOWVERBOSE if(verbose){printf("phi^* A_[%d]=%lf\n",i,p[i]);fflush(stdout);} #endif } #ifdef MFALLOWVERBOSE if(verbose){printf("max of phi^* A_j is for j=%d, =%lf\n",j,pmax);fflush(stdout);} #endif m=0; u=MFMColumn(Phi,m,e); w=MFCloneNVector(u,e); MFNSpaceScale(space,1.,phi,u,e); #ifdef MFALLOWVERBOSE if(verbose){printf("column 0 of Phi is phi ");MFPrintNVector(stdout,u,e);printf("\n");(stdout);} #endif if(Phi->type==DENSE)MFMSetColumn(Phi,m,u,e); MFFreeNVector(u,e); m++; pj=MFMColumn(A,j,e); for(i=0;i<k;i++) { if(i!=j) { pi=MFMColumn(A,i,e); u=MFMColumn(Phi,m,e); MFNSpaceScale(space, p[i],pj,u,e); MFNSpaceScale(space,-p[j],pi,w,e); MFNSpaceAdd(space,u,w,u,e); #ifdef MFALLOWVERBOSE if(verbose){printf("column %d of Phi is %lf A_%d - %lf A_%d ",m,p[i],j,p[j],i);MFPrintNVector(stdout,u,e);printf("\n");(stdout);} #endif if(Phi->type==DENSE)MFMSetColumn(Phi,m,u,e); m++; MFFreeNVector(u,e); MFFreeNVector(pi,e); } } MFFreeNVector(pj,e); free(p); #ifdef MFALLOWVERBOSE if(verbose){printf("Before G-S:\n");MFPrintNKMatrix(stdout,Phi,e);fflush(stdout);} #endif MFGramSchmidt(space,Phi,e); #ifdef MFALLOWVERBOSE if(verbose){printf("After G-S:\n");MFPrintNKMatrix(stdout,Phi,e);fflush(stdout); printf("done %s\n",RoutineName);fflush(stdout);} #endif return; }
void MFGramSchmidt(MFNSpace space,MFNKMatrix A,MFErrorHandler e) { static char RoutineName[]={"MFGramSchmidt"}; double *a; int i,j,n; int k; MFNVector u; MFNVector v; MFNVector w; double inner; int verbose=0; n=MFNKMatrixN(A,e); k=MFNKMatrixK(A,e); switch(A->type) { case DENSE: a=MFNKM_CStar(A,e); MFGramSchmidtNoMat(n,k,a,e); break; default: u=MFMColumn(A,0,e); w=MFCloneNVector(u,e); MFFreeNVector(u,e); #ifdef MFALLOWVERBOSE if(verbose){printf("Gram Schmidt:\n");fflush(stdout);} #endif for(i=0;i<k;i++) { u=MFMColumn(A,i,e); inner=MFNSpaceInner(space,u,u,e); #ifdef MFALLOWVERBOSE if(verbose){printf("u_%d\n",i);MFPrintNVector(stdout,u,e);printf("\n");fflush(stdout); printf("<u_%d,u_%d>=%lf\n",i,i,inner);fflush(stdout);} #endif inner=1./sqrt(inner); MFNSpaceScale(space,inner,u,u,e); #ifdef MFALLOWVERBOSE if(verbose){printf("normalized u_%d\n",i);MFPrintNVector(stdout,u,e);printf("\n");fflush(stdout);} #endif for(j=i+1;j<k;j++) { v=MFMColumn(A,j,e); #ifdef MFALLOWVERBOSE if(verbose){printf("u_%d\n",j);MFPrintNVector(stdout,v,e);printf("\n");fflush(stdout);} #endif inner=MFNSpaceInner(space,u,v,e); #ifdef MFALLOWVERBOSE if(verbose){printf("<u_%d,u_%d>=%lf\n",i,j,inner);fflush(stdout);} #endif MFNSpaceScale(space,inner,u,w,e); #ifdef MFALLOWVERBOSE if(verbose){printf("<u_%d,u_%d>u_%d\n",j,i,i);MFPrintNVector(stdout,w,e);printf("\n");fflush(stdout);} #endif MFNVDiff(v,w,v,e); #ifdef MFALLOWVERBOSE if(verbose){printf("u_%d-<u_%d,u_%d>u_%d\n",j,j,i,i);MFPrintNVector(stdout,v,e);printf("\n");fflush(stdout);} #endif MFFreeNVector(v,e); } MFFreeNVector(u,e); } MFFreeNVector(w,e); } return; }
void MFMMMul(MFNSpace R, MFNKMatrix Phi0,MFNKMatrix Phi1,double *prod,MFErrorHandler e) { static char RoutineName[]={"MFMMMul"}; int i,j; MFNVector col0,col1; static int verbose; /* Phi_0^T Phi_1 */ verbose=0; if(verbose){printf("%s\n",RoutineName);fflush(stdout);} #ifdef MFNOCONFIDENCE if(Phi0->n!=Phi1->n) { sprintf(MFNKMatrixErrorMsg,"Can't do product of incompatible matrices. A.n=%d, B.n=%d",Phi0->n,Phi1->n); MFSetError(e,12,RoutineName,MFNKMatrixErrorMsg,__LINE__,__FILE__); return; } if(Phi0->type!=Phi1->type) { sprintf(MFNKMatrixErrorMsg,"Can't do product of incompatible matrices. A.type=%d, B.type=%d",Phi0->type,Phi1->type); MFSetError(e,12,RoutineName,MFNKMatrixErrorMsg,__LINE__,__FILE__); return; } #endif for(i=0;i<Phi0->k;i++) { if(Phi0->type==DENSE) col0=MFCreateNVectorWithData(Phi0->n,Phi0->data+i*Phi0->n,e); else col0=MFMColumn(Phi0,i,e); #ifdef MFNOCONFIDENCE if(col0==NULL) { sprintf(MFNKMatrixErrorMsg,"col %d of A is NULL",i); MFSetError(e,12,RoutineName,MFNKMatrixErrorMsg,__LINE__,__FILE__); return; } #endif for(j=0;j<Phi1->k;j++) { if(Phi1->type==DENSE) col1=MFCreateNVectorWithData(Phi1->n,Phi1->data+i*Phi1->n,e); else col1=MFMColumn(Phi1,j,e); #ifdef MFNOCONFIDENCE if(col1==NULL) { sprintf(MFNKMatrixErrorMsg,"col %d of B is NULL",j); MFSetError(e,12,RoutineName,MFNKMatrixErrorMsg,__LINE__,__FILE__); return; } #endif prod[i+Phi0->k*j]=MFNSpaceInner(R,col0,col1,e); MFFreeNVector(col1,e); } MFFreeNVector(col0,e); } return; }