Exemple #1
0
void MFFreeNKMatrix(MFNKMatrix thisMatrix,MFErrorHandler e)
 {
  static char RoutineName[]={"MFFreeNKMatrix"};
  int i;

#ifdef MFNOCONFIDENCE
  if(thisMatrix==NULL)
   {
    sprintf(MFNKMatrixErrorMsg,"Pointer to Matrix (argument 1) is NULL");
    MFSetError(e,4,RoutineName,MFNKMatrixErrorMsg,__LINE__,__FILE__);
    return;
   }
#endif

  thisMatrix->nRefs--;

  if(thisMatrix->nRefs<1)
   {
    if(thisMatrix->data!=NULL)free(thisMatrix->data);
    if(thisMatrix->cols!=NULL)
     {
      for(i=0;i<thisMatrix->k;i++)MFFreeNVector(thisMatrix->cols[i],e);
      free(thisMatrix->cols);
     }
    free(thisMatrix);
   }
  return;
 }
Exemple #2
0
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;
 }
Exemple #3
0
void MFMSetColumn(MFNKMatrix thisMatrix,int j, MFNVector u,MFErrorHandler e)
 {
  static char RoutineName[]={"MFMSetColumn"};
  int i;
  MFNVector c;

#ifdef MFNOCONFIDENCE
  if(j<0 || j >= thisMatrix->k)
   {
    sprintf(MFNKMatrixErrorMsg,"Column %d must be between 0 and %d",j,thisMatrix->k);
    MFSetError(e,12,RoutineName,MFNKMatrixErrorMsg,__LINE__,__FILE__);
    MFErrorHandlerOutOfMemory(e);
    free(thisMatrix);
    return;
   }
#endif

  switch(thisMatrix->type)
   {
    case DENSE:
     for(i=0;i<thisMatrix->n;i++)
      thisMatrix->data[i+thisMatrix->n*j]=MFNV_C(u,i,e);
     break;
    case VECTOR:
      MFRefNVector(u,e);
      MFFreeNVector(thisMatrix->cols[j],e);
      thisMatrix->cols[j]=u;
     break;
    default:
     for(i=0;i<thisMatrix->n;i++)
      MFNVSetC(thisMatrix->cols[j],i,MFNV_C(u,i,e),e);
   }

  return;
 }
Exemple #4
0
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;
 }
Exemple #5
0
void MFChartClean(MFChart thisChart, MFErrorHandler e)
 {
  if(!(thisChart->paged))return;

  if(thisChart->u!=NULL){MFFreeNVector(thisChart->u,e);thisChart->u=NULL;}
  if(thisChart->Phi!=NULL){MFFreeNKMatrix(thisChart->Phi,e);thisChart->Phi=NULL;}

  return;
 }
Exemple #6
0
void MFChartProjectIntoTangentSpace(MFChart chart,MFNVector u,MFKVector s, MFErrorHandler e)
 {
  static char RoutineName[]={"MFChartProjectIntoTangentSpace"};
  MFNVector v;

#ifdef MFNOCONFIDENCE
  if(chart==NULL)
   {
    sprintf(MFChartErrorMsg,"Chart (argument 1) is NULL");
    MFSetError(e,4,RoutineName,MFChartErrorMsg,__LINE__,__FILE__);
    return;
   }

  if(u==NULL)
   {
    sprintf(MFChartErrorMsg,"u (argument 2) is NULL");
    MFSetError(e,8,RoutineName,MFChartErrorMsg,__LINE__,__FILE__);
    return;
   }

  if(s==NULL)
   {
    sprintf(MFChartErrorMsg,"s (argument 3) is NULL");
    MFSetError(e,8,RoutineName,MFChartErrorMsg,__LINE__,__FILE__);
    return;
   }

  if(chart->n==0)
   {
    sprintf(MFChartErrorMsg,"Chart has zero dimension");
    MFSetError(e,8,RoutineName,MFChartErrorMsg,__LINE__,__FILE__);
    return;
   }
#endif

  if(chart->paged)MFChartPageIn(chart,chart->fid,e);

#ifdef MFNOCONFIDENCE
  if(chart->u==NULL)
   {
    sprintf(MFChartErrorMsg,"Chart center is NULL");
    MFSetError(e,8,RoutineName,MFChartErrorMsg,__LINE__,__FILE__);
    return;
   }
#endif

  v=MFCloneNVector(u,e);

  MFNSpaceDirection(MFIMFNSpace(chart->M,e),chart->u,u,v,e);
  MFMVMulT(MFIMFNSpace(chart->M,e),chart->Phi,v,s,e);
  MFFreeNVector(v,e);

  return;
 }
int main(int argc, char *argv[])
 {
  MFImplicitMF M;
  int i;
  int n,k;
  MFNRegion Omega;
  MFAtlas S;
  MFNVector u0;
  FILE *fid;
  double R=.5;
  MFContinuationMethod H;
  MFErrorHandler e;

  e=MFCreateErrorHandler();

  k=4;
  M=MFIMFCreateNSpaceWithRadius(k,.1,e);
  n=MFIMF_N(M,e);
  Omega=MFNRegionCreateHyperCube(n,R,e);
  printf("Interior of Four Cube, volume=%lf\n",2*R*2*R*2*R*2*R);

  u0=MFIMFVectorFactory(M,e);
  for(i=0;i<k;i++)
    MFNVSetC(u0,i, 0.,e);

  H=MFCreateMultifariosMethod(e);
  MFMultifarioSetRealParameter(H,"epsilon",.1,e);
  MFMultifarioSetIntegerParameter(H,"maxCharts",-1,e);
  MFMultifarioSetIntegerParameter(H,"verbose",1,e);
  MFMultifarioSetIntegerParameter(H,"page",1,e);
  MFMultifarioSetFilename(H,"FourSpace",e);


  S=MFComputeAtlas(H,M,Omega,u0,e);

/*fid=fopen("FourSpace.atlas","w");
  MFWriteAtlas(fid,S,e);
  fclose(fid);
  printf("Done writing Atlas\n");fflush(stdout);*/

  MFFreeAtlas(S,e);
  MFFreeContinuationMethod(H,e);
  MFFreeImplicitMF(M,e);
  MFFreeNRegion(Omega,e);
  MFFreeNVector(u0,e);

  MFFreeErrorHandler(e);

  return 0;
 }
Exemple #8
0
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 MFFreeModifiedPolytope(MFModifiedPolytope P, MFErrorHandler e)
 {
  static char RoutineName[]={"MFFreeModifiedPolytope"};
  int i;


#ifdef MFNOCONFIDENCE
  if(P==NULL)
   {
    sprintf(MFAtlasErrorMsg,"Polytope (argument 1) is NULL");
    MFSetError(e,4,RoutineName,MFAtlasErrorMsg,__LINE__,__FILE__);
    return;
   }
#endif
  if(P->v!=NULL)
   {
    free(P->v);
    P->v=NULL;
   }
  if(P->nIndices!=NULL){free(P->nIndices);P->nIndices=NULL;}
  if(P->mIndices!=NULL){free(P->mIndices);P->mIndices=NULL;}

  if(P->mark!=NULL){free(P->mark);P->mark=NULL;}

  for(i=0;i<P->n;i++)
   {
    if(P->indices[i]!=NULL){free(P->indices[i]);P->indices[i]=NULL;}
   }
  if(P->indices!=NULL){free(P->indices);P->indices=NULL;}
  if(P->face!=NULL){free(P->face);P->face=NULL;}
  if(P->nFaceV!=NULL){free(P->nFaceV);P->nFaceV=NULL;}

  if(P->faceN!=NULL)
   {
    for(i=0;i<P->mFaces;i++) /* was nFaces */
     {
      if(P->faceN[i]!=NULL)MFFreeNVector(P->faceN[i],e);
      P->faceN[i]=NULL;
     }
    free(P->faceN);P->faceN=NULL;
   }
  if(P->faceO!=NULL){free(P->faceO);P->faceO=NULL;}
  free(P);

  return;
 }
int main(int argc, char *argv[])
 {
  MFImplicitMF M;
  int n;
  MFNRegion Omega;
  MFAtlas S;
  MFNVector u0;
  FILE *fid;
  MFContinuationMethod H;
  MFErrorHandler e;

  e=MFCreateErrorHandler();

  M=MFIMFCreatePlane(e);
  n=MFIMF_N(M,e);
  Omega=MFNRegionCreateRectangle(-1.,-1.,1.,1.,e);

  u0=MFIMFVectorFactory(M,e);
  MFNVSetC(u0,0, 0.,e);
  MFNVSetC(u0,1, 0.,e);

  H=MFCreateMultifariosMethod(e);
  MFMultifarioSetRealParameter(H,"epsilon",.1,e);
  MFMultifarioSetIntegerParameter(H,"maxCharts",-1,e);
  MFMultifarioSetIntegerParameter(H,"verbose",1,e);
  MFMultifarioSetIntegerParameter(H,"page",1,e);
  MFMultifarioSetIntegerParameter(H,"dumpToPlotFile",1,e);
  MFMultifarioSetIntegerParameter(H,"dumpToCenterFile",0,e);
  MFMultifarioSetFilename(H,"Plane",e);

  S=MFComputeAtlas(H,M,Omega,u0,e);

  MFCloseAtlas(H,S,e);
  printf("Done computing Atlas\n");fflush(stdout);

  MFFreeAtlas(S,e);
  MFFreeImplicitMF(M,e);
  MFFreeNRegion(Omega,e);
  MFFreeNVector(u0,e);
  MFFreeContinuationMethod(H,e);

  MFFreeErrorHandler(e);

  return 0;
 }
Exemple #11
0
void MFChartPointInTangentSpace(MFChart chart,MFKVector s,MFNVector u, MFErrorHandler e)
 {
  static char RoutineName[]={"MFChartPointInTangentSpace"};
  MFNVector v;
  int verbose=0;

#ifdef MFALLOWVERBOSE
  if(verbose){printf("%s\n",RoutineName);fflush(stdout);}
#endif

#ifdef MFNOCONFIDENCE
  if(chart==NULL)
   {
    sprintf(MFChartErrorMsg,"Pointer to Chart (argument 1) is NULL");
    MFSetError(e,4,RoutineName,MFChartErrorMsg,__LINE__,__FILE__);
    return;
   }
#endif

  if(chart->paged)MFChartPageIn(chart,chart->fid,e);

  v=MFCloneNVector(u,e);

  MFMVMul(MFIMFNSpace(chart->M,e),chart->Phi,s,v,e);
  MFNSpaceAdd(MFIMFNSpace(chart->M,e),v,chart->u,u,e);
#ifdef MFALLOWVERBOSE
  if(verbose)
   {
    MFPrintNVector(stdout,chart->u,e);
    printf("+");
    MFPrintNKMatrix(stdout,chart->Phi,e);
    MFPrintKVector(stdout,s,e);
    printf("=");
    MFPrintNVector(stdout,u,e);
    printf("\n");
    fflush(stdout);
   }
#endif

  MFFreeNVector(v,e);

  return;
 }
Exemple #12
0
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;
 }
Exemple #13
0
void MFFreeChart(MFChart chart, MFErrorHandler e)
 {
  static char RoutineName[]={"MFFreeChart"};

#ifdef MFNOCONFIDENCE
  if(chart==NULL)
   {
    sprintf(MFChartErrorMsg,"Pointer to Chart (argument 1) is NULL");
    MFSetError(e,4,RoutineName,MFChartErrorMsg,__LINE__,__FILE__);
    return;
   }
#endif
  if(chart==NULL)return;

  chart->nRefs--;

  if(chart->nRefs>0)return;
  if(chart==(MFChart)0x0087dd58){printf("%s 0x%8.8x, center 0x%8.8x, nRefs=%d\n",RoutineName,chart,chart->u,chart->nRefs);fflush(stdout);}

  if(chart==(MFChart)0x0087dd58){printf("  free chart->M 0x%8.8x\n",chart->M);fflush(stdout);}
  if(chart->M!=NULL){MFFreeImplicitMF(chart->M,e);chart->M=NULL;};
  if(chart==(MFChart)0x0087dd58){printf("  free chart->u 0x%8.8x\n",chart->u);fflush(stdout);}
  if(chart->u!=NULL){MFFreeNVector(chart->u,e);chart->u=NULL;};
  if(chart==(MFChart)0x0087dd58){printf("  free chart->P 0x%8.8x\n",chart->P);fflush(stdout);}
  if(chart->P!=NULL)
   {
    MFFreePolytope(chart->P,e);
    chart->P=NULL;
   }
  if(chart==(MFChart)0x0087dd58){printf("  free chart->Phi 0x%8.8x\n",chart->Phi);fflush(stdout);}
  if(chart->Phi!=NULL){MFFreeNKMatrix(chart->Phi,e);chart->Phi=NULL;};
  if(chart==(MFChart)0x0087dd58){printf("  free chart 0x%8.8x\n",chart);fflush(stdout);}
  free(chart);
  if(chart==(MFChart)0x0087dd58){printf("  done");fflush(stdout);}

  return;
 }
Exemple #14
0
int MFTangentLOCA(int n,int k,MFNVector vu,MFNKMatrix mPhi,void *d, 
		  MFErrorHandler err)
{
   LOCAData* data = (LOCAData *)d;

   LOCANVectorData* u0_data = (LOCANVectorData *) MFNVectorGetData(vu, err);
   data->grp->setX(*(u0_data->u_ptr));
   data->grp->computePredictor();

   const LOCA::MultiContinuation::ExtendedMultiVector& pred = 
     data->grp->getPredictorTangent();

   for (int i=0; i<k; i++) {
     Teuchos::RCP<LMCEV> t = 
       Teuchos::rcp_dynamic_cast<LMCEV>(pred[i].clone());
     MFNVector tmp =  MFCreateLOCANVectorWithData(t,err);
     MFMSetColumn(mPhi, i, tmp, err);
     MFFreeNVector(tmp, err);
   }

   MFGramSchmidt(data->space,mPhi, err);

   return 1;
}
Exemple #15
0
int MFChartPageOut(MFChart thisChart, FILE *fid, int index, MFErrorHandler e)
 {
  static char RoutineName[]={"MFChartPageOut"};
  static int i,j;
  double *c;
  int verbose=0;
  int rc;

#ifdef MFNOCONFIDENCE
  if(thisChart==NULL)
   {
    sprintf(MFChartErrorMsg,"Chart is NULL");
    MFSetError(e,0,RoutineName,MFChartErrorMsg,__LINE__,__FILE__);
    return 0;
   }
#endif

/* Returns 1 if written, 0 if already paged out */

  if(thisChart->paged)
   {
#ifdef MFALLOWVERBOSE
    if(verbose){printf("Already paged out\n");fflush(stdout);}
#endif
    return 0;
   }

#ifdef MFREALLYPAGE
#ifdef MFALLOWVERBOSE
  if(verbose){fprintf(stderr,"PAGING out chart, fseek to end of file 0x%8.8x\n",fid);fflush(stderr);}
#endif

  rc=fseek(fid,(long)0,SEEK_END);

#ifdef MFALLOWVERBOSE
  if(verbose){fprintf(stderr,"  return code from fseek is %d\n",rc);fprintf(stderr,"ftell= %d\n",ftell(fid));fflush(stderr);}
  if(verbose){fprintf(stderr,"  write to position %d\n",index);fprintf(stderr,"ftell= %d\n",ftell(fid));fflush(stderr);}
#endif

  c=MFNV_CStar(thisChart->u);

#ifdef MFALLOWVERBOSE
  if(verbose){fprintf(stderr,"PAGING   u starts (%lf,%lf,%lf...\n",c[0],c[1],c[2]);fflush(stderr);}
#endif
  thisChart->fid=fid;
  thisChart->paged=1;
  thisChart->indexInPageFile=ftell(fid);
  for(i=0;i<thisChart->n;i++)fprintf(fid,"%lf ",c[i]);
  fprintf(fid,"\n");

  c=MFNKM_CStar(thisChart->Phi,e);  /* BAD What if it's not dense? */

#ifdef MFALLOWVERBOSE
  if(verbose){fprintf(stderr,"PAGING   Phi starts (%lf,%lf,%lf...\n",c[0],c[1],c[2]);fflush(stderr);}
#endif

  for(j=0;j<thisChart->k;j++)
  for(i=0;i<thisChart->n;i++)fprintf(fid,"%lf ",c[i+thisChart->n*j]);
  fprintf(fid,"\n");
#endif

#ifdef MFREALLYTHROWITAWAY
#ifdef MFALLOWVERBOSE
  if(verbose){printf("Discarding contents of chart\n");fflush(stdout);}
#endif

  MFFreeNVector(thisChart->u,e);thisChart->u=NULL;
  MFFreeNKMatrix(thisChart->Phi,e);thisChart->Phi=NULL;
  MFFreePolytope(thisChart->P,e);thisChart->P=NULL;
#endif

  thisChart->paged=1;

  return 1;
 }
/*! \fn MFAtlas IMFComputeUnstableInvariantManifold(IMFFlow F,char *name, MFNVector u0, MFKVector p0, MFNRegion Omega, double eps, double dt, double tmax, int maxInterp, int maxCharts, double R0, MFErrorHandler e);
 * \brief Computes an atlas of charts that cover the unstable manifold of a hyerbolic fixed point.
 *
 * \param L The flow.
 * \param name A name to used for paging files and so forth.
 * \param u0 The fixed point.
 * \param p0 The parameters of the flow for the fixed point.
 * \param Omega A region to bound the computation.
 * \param eps The tolerance on the size of the quadratic terms over a balls. Controls the stepsize.
 * \param dt The initial timestep to use along a fat trajectory.
 * \param tmax The upper limit on trajectory length.
 * \param maxInterp The upper limit on the number of interpolation performed.
 * \param maxCharts The upper limit on the number of charts in the atlas.
 * \param R0 The radius for the initial ball about the fixedpoint that serves as initial surface for the manifold.
 * \param e An MFErrorHandler to handle exceptions and errors.
 * \returns A new Atlas
 */
MFAtlas IMFComputeUnstableInvariantManifold(IMFFlow F,char *name, MFNVector u0, MFKVector p0, MFNRegion Omega, double eps, double dt, double tmax, int maxInterp, int maxCharts, double R0, MFErrorHandler e)
{
    static char RoutineName[]= {"IMFComputeUnstableInvariantManifold"};
    IMFExpansion u,a;
    double s0[2],s1[2];
    MFKVector s;
    MFNVector ug,v;
    MFNVector ustar;
    MFNKMatrix Phi0;
    MFNKMatrix Phi;
    MFNKMatrix Psi;
    MFImplicitMF c;
    int i,j;
    IMFExpansion cE,U;
    MFContinuationMethod H;
    MFAtlas A;
    MFAtlas I;
    MFImplicitMF M;
    double R,r;
    char sname[1024];
    FILE *fid;
    MFNKMatrix TS;
    MFNVector ui;
    MFNVector ut;
    double t;
    int chart;
    MFNVector sigma;
    MFKVector zero;
    int n,k;
    int verbose=0;

    Phi=IMFGetBasisForUnstableInvariantSubspace(F,u0,p0,e);
    Psi=IMFGetBasisForOrthogonalComplement(Phi,e);

    n=MFNKMatrixN(Phi,e);
    k=MFNKMatrixK(Phi,e);
    u=IMFCreateExpansion(n,k,e);
    a=IMFCreateExpansion(k,k,e);
    IMFFindExpansionNearFixedPt(u0,p0,Phi,Psi,F,u,a,e);

#ifdef MFALLOWVERBOSE
    if(verbose) {
        printf("done IMFFindExpansionNearFixedPt\n");
        fflush(stdout);
        printf("Expansion at fixed point:\n");
        IMFPrintExpansion(stdout,u,e);
        fflush(stdout);
    }
#endif

    zero=MFCreateKVector(k-1,e);

    R=IMFExpansionR(u,eps,e);
    if(R!=R||R>R0)R=R0;
    r=.5*R;
    /* For U1!    R=R0; r=.05; */
    c=IMFCreateSphereOnExpansion(u,F,p0,eps,R,r,e);

    s =MFCreateKVector(2,e);
    MFKVSetC(s,0,R,e);
    MFKVSetC(s,1,0.,e);

    ug=MFCreateNVector(n,e);
    IMFEvaluateExpansion(u,s,ug,e);

    ut=MFCreateNVector(n+k,e);
    for(i=0; i<n; i++)MFNVSetC(ut,i,MFNV_C(ug,i,e),e);
    for(i=0; i<k; i++)MFNVSetC(ut,n+i,MFKV_C(s,i,e),e);
    MFFreeNVector(ug,e);

    ustar=MFCreateNVector(n+k,e);

    printf("Cover the initial surface\n");
    Phi0=MFIMFTangentSpace(c,ut,e);
    if(MFIMFProject(c,ut,Phi0,ustar,e))
    {

        H=MFCreateMultifariosMethod(e);
        MFMultifarioSetRealParameter(H,"epsilon",.8,e);
        MFMultifarioSetIntegerParameter(H,"maxCharts",-1,e);
        MFMultifarioSetIntegerParameter(H,"verbose",1,e);
        MFMultifarioSetIntegerParameter(H,"page",0,e);
        MFMultifarioSetIntegerParameter(H,"dumpToPlotFile",0,e);
        MFMultifarioSetIntegerParameter(H,"dumpToCenterFile",0,e);
        MFMultifarioSetFilename(H,"SphereOnU0",e);

        A=MFComputeAtlas(H,c,Omega,ustar,e);
        MFFreeNVector(ut,e);
        MFCloseAtlas(H,A,e);
        MFFreeContinuationMethod(H,e);
        printf("Done computing Atlas for initial values\n");
        fflush(stdout);
    } else {
        printf("Projection of initial point failed\n");
        fflush(stdout);
        return NULL;
    }

    M=IMFCreateFlat(n,k,e);
    I=MFCreateAtlas(M,e);
    MFAtlasSetNearRtn(I,IMFIsNear,e);

    H=MFCreateMultifariosMethod(e);
    MFMultifarioSetIntegerParameter(H,"page",0,e);
    MFMultifarioSetIntegerParameter(H,"dumpToPlotFile",1,e);
    MFMultifarioSetFilename(H,name,e);

    TS=IMFExpansionTS(u,e);
    ui=IMFCreateExpansionNVector(u,-100.,u0,-1,1,e);
    MFAtlasAddChartWithAll(I,ui,TS,R,e);
    printf("0) Fixed Point. ");
    MFPrintNVector(stdout,ui,e);
    printf(", R=%lf\n",R);
    fflush(stdout);
    MFFreeNVector(ui,e);
    MFFreeNKMatrix(TS,e);

    s0[0]=-r;
    s1[0]= r;
    s0[1]=-r;
    s1[1]= r;
    for(i=0; i<MFAtlasNumberOfCharts(A,e); i++)
    {
        cE=IMFSphereOnExpansionGetLocal(A,MFAtlasCenterOfChart(A,i,e),e);
        U=IMFInflateExpansionWithFlow(cE,F,p0,e);

        ui=IMFCreateExpansionNVector(U,0.,MFAtlasCenterOfChart(A,i,e),-1,1,e);
        IMFExpansionNVSetChart0(ui,i,e);
        IMFExpansionNVSetS0(ui,zero,e);
        printf("*) Point on c");
        fflush(stdout);
        IMFExtendAtlasAlongFatTraj(I,F,name,ui,p0,dt,0.,tmax,eps,Omega,maxCharts,maxCharts,1.,0,e);
        if(0&&MFAtlasNumberOfCharts(I,e)%1000==0) {
            printf("Paging\n");
            MFAtlasPageOutChartsNotNearBoundary(I,1,0,name,e);
        }
        MFFreeNVector(ui,e);

        IMFFreeExpansion(cE,e);
        IMFFreeExpansion(U,e);
        if(MFAtlasNumberOfCharts(I,e)>maxCharts)
        {
            printf("Maximum number of charts exceeded (%d>%d).\n",MFAtlasNumberOfCharts(I,e),maxCharts,e);
            fflush(stdout);
            goto DoneCover;
        }
    }

    printf("*** Done covering c\n");
    fflush(stdout);
#ifdef DOINTERPOLATIONPT
    sprintf(sname,"%s.intpt",name);
    IMFInterp=fopen(sname,"w");
    IMFNInterp=0;
#endif

    i=0;
    printf("*** Begin interpolation\n");
    fflush(stdout);
    while(i<maxInterp&& (ui=IMFGetInterpolationPoint(I,F,p0,A,tmax,NULL,e))!=NULL && MFAtlasNumberOfCharts(I,e)<maxCharts)
    {
        printf("*** Interpolated Point %d\n",i);
        fflush(stdout);
#ifdef DOINTERPOLATIONPT
        for(j=0; j<3; j++)fprintf(IMFInterp," %lf",IMFExpansionU(IMFExpansionNVGetE(ui,e))[j],e);
        fprintf(IMFInterp,"\n");
        fflush(IMFInterp);
        IMFNInterp++;
#endif
        if(i<maxInterp-1)IMFExtendAtlasAlongFatTraj(I,F,name,ui,p0,dt,IMFExpansionNVGetT(ui,e),tmax,eps,Omega,maxCharts,maxCharts,1.,0,e);
        if(0&&MFAtlasNumberOfCharts(I,e)%1000==0) {
            printf("Paging\n");
            MFAtlasPageOutChartsNotNearBoundary(I,1,0,name,e);
        }
        MFFreeNVector(ui,e);
        i++;
        if(MFAtlasNumberOfCharts(I,e)>maxCharts)
        {
            printf("Maximum number of charts exceeded (%d>%d).\n",MFAtlasNumberOfCharts(I,e),maxCharts);
            fflush(stdout);
            goto DoneCover;
        }
    }

    if(i<maxInterp)
    {
        printf("*** No more interpolation points, total was %d\n",i);
        fflush(stdout);
    }
    else
    {
        printf("*** Max number of interpolation points reached, %d\n",i);
        fflush(stdout);
    }

DoneCover:
    printf("*** Done cover\n");
    fflush(stdout);

    MFFreeImplicitMF(c,e);
    MFFreeKVector(s,e);

    MFCloseAtlas(H,I,e);
    MFFreeContinuationMethod(H,e);

    MFFreeNKMatrix(Phi0,e);

    MFFreeNVector(ustar,e);

    MFFreeNKMatrix(Phi,e);

    MFFreeNKMatrix(Psi,e);

    IMFFreeExpansion(u,e);

    IMFFreeExpansion(a,e);

    MFFreeImplicitMF(M,e);

    MFFreeAtlas(A,e);

#ifdef DOINTERPOLATIONPT
    if(IMFInterp!=NULL)
    {
        fclose(IMFInterp);

        sprintf(sname,"i%s.dx",name);
        IMFInterp=fopen(sname,"w");
        sprintf(sname,"%s.intpt",name);
        if(IMFNInterp>0)
        {
            fprintf(IMFInterp,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file %s.dx\n",sname,IMFNInterp);
        } else {
            fprintf(IMFInterp,"object \"Vertices\" class array type float rank 1 shape 3 items 1 data follows\n");
            fprintf(IMFInterp,"  0. 0. 0.\n");
        }
        fprintf(IMFInterper,"object \"interpolated points\" class field\n");
        fprintf(IMFInterper,"component \"positions\" value \"Vertices\"\n");
        fclose(IMFInterper);
    }
#endif

#ifdef DOINTERPANIM

    if(IMFInterper!=NULL)
    {
        fclose(IMFInterper);
        IMFInterper=fopen("gInterp1.dx","w");
        if(IMFNInterper>0)
        {
            fprintf(IMFInterper,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file Interp1.dx\n",IMFNInterper);
        } else {
            fprintf(IMFInterper,"object \"Vertices\" class array type float rank 1 shape 3 items 1 data follows\n");
            fprintf(IMFInterper,"  0. 0. 0.\n");
        }
        fprintf(IMFInterper,"object \"interpolated points\" class field\n");
        fprintf(IMFInterper,"component \"positions\" value \"Vertices\"\n");
        fclose(IMFInterper);
    }

    if(IMFInterpee!=NULL)
    {
        fclose(IMFInterpee);
        IMFInterpee=fopen("gInterp2.dx","w");
        if(IMFNInterpee>0)
        {
            fprintf(IMFInterpee,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file Interp2.dx\n",IMFNInterpee);
            fprintf(IMFInterpee,"object \"Lines\" class array type int rank 1 shape 2 items %d data follows\n",IMFNInterpee/2);
            for(i=0; i<IMFNInterpee; i+=2)
                fprintf(IMFInterpee,"   %d %d\n",i,i+1);
        } else {
            fprintf(IMFInterpee,"object \"Vertices\" class array type float rank 1 shape 3 items 2 data follows\n");
            fprintf(IMFInterpee,"  0. 0. 0.\n");
            fprintf(IMFInterpee,"  0. 0. 0.001\n");
            fprintf(IMFInterpee,"object \"Lines\" class array type int rank 1 shape 2 items 1 data follows\n");
            fprintf(IMFInterpee,"   %d %d\n",0,1);
        }
        fprintf(IMFInterpee,"attribute \"ref\" string \"positions\"\n");
        fprintf(IMFInterpee,"attribute \"element type\" string \"lines\"\n");

        fprintf(IMFInterpee,"object \"interpolation points\" class field\n");
        fprintf(IMFInterpee,"component \"positions\" value \"Vertices\"\n");
        fprintf(IMFInterpee,"component \"connections\" value \"Lines\"\n");
        fclose(IMFInterpee);
    }

    if(IMFInterpT!=NULL)
    {
        fclose(IMFInterpT);
        IMFInterpT=fopen("gInterp3.dx","w");
        if(IMFNInterpT>0)
        {
            fprintf(IMFInterpT,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file Interp3.dx\n",IMFNInterpT
                   );
            fprintf(IMFInterpT,"object \"Lines\" class array type int rank 1 shape 2 items %d data follows\n",IMFNInterpT-1);
            for(i=0; i<IMFNInterpT-1; i++)
                fprintf(IMFInterpT,"   %d %d\n",i,i+1);
        } else {
            fprintf(IMFInterpT,"object \"Vertices\" class array type float rank 1 shape 3 items 2 data follows\n");
            fprintf(IMFInterpT,"  0. 0. 0.\n");
            fprintf(IMFInterpT,"  0. 0. 0.001\n");
            fprintf(IMFInterpT,"object \"Lines\" class array type int rank 1 shape 2 items 1 data follows\n");
            fprintf(IMFInterpT,"   %d %d\n",0,1);
        }
        fprintf(IMFInterpT,"attribute \"ref\" string \"positions\"\n");
        fprintf(IMFInterpT,"attribute \"element type\" string \"lines\"\n");

        fprintf(IMFInterpT,"object \"interpolation points\" class field\n");
        fprintf(IMFInterpT,"component \"positions\" value \"Vertices\"\n");
        fprintf(IMFInterpT,"component \"connections\" value \"Lines\"\n");
        fclose(IMFInterpT);
    }

    if(IMFCircle!=NULL)
    {
        fclose(IMFCircle);
        IMFCircle=fopen("gIMFCircle.dx","w");
        fprintf(IMFCircle,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file IMFCircle.dx\n",IMFNCircle);
        fprintf(IMFCircle,"object \"Lines\" class array type int rank 1 shape 2 items %d data follows\n",IMFNCircle/2);
        for(i=0; i<IMFNCircle; i+=2)
            fprintf(IMFCircle,"   %d %d\n",i,i+1);
        fprintf(IMFCircle,"attribute \"ref\" string \"positions\"\n");
        fprintf(IMFCircle,"attribute \"element type\" string \"lines\"\n");

        fprintf(IMFCircle,"object \"interpolation points\" class field\n");
        fprintf(IMFCircle,"component \"positions\" value \"Vertices\"\n");
        fprintf(IMFCircle,"component \"connections\" value \"Lines\"\n");
        fclose(IMFCircle);
    }
#endif

#ifdef DOTRAJ
    if(IMFTraj!=NULL)
    {
        printf("dump trajectory file\n");
        fflush(stdout);
        fclose(IMFTraj);
        IMFTraj=fopen("gIMFTraj.dx","w");
        fprintf(IMFTraj,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file IMFTraj.dx\n",IMFNTraj);
        fprintf(IMFTraj,"object \"Lines\" class array type int rank 1 shape 2 items %d data follows\n",IMFNTraj/2);
        for(i=0; i<IMFNTraj; i+=2)
            fprintf(IMFTraj,"   %d %d\n",i,i+1);
        fprintf(IMFTraj,"attribute \"ref\" string \"positions\"\n");
        fprintf(IMFTraj,"attribute \"element type\" string \"lines\"\n");

        fprintf(IMFTraj,"object \"trajectories\" class field\n");
        fprintf(IMFTraj,"component \"positions\" value \"Vertices\"\n");
        fprintf(IMFTraj,"component \"connections\" value \"Lines\"\n");
        fclose(IMFTraj);
    }
#endif

    printf("done %s\n",RoutineName);
    fflush(stdout);

    return I;
}
/*! \fn MFAtlas IMFComputeInvariantManifold(IMFFlow F,MFKVector p0,char *name, MFAtlas c,MFNRegion Omega, double eps, double dt, double tmax, int maxInterp, int maxCharts, double Rmax, MFErrorHandler e);
 * \brief Computes an atlas of charts that cover the image of a manifold under a flow. A streamsurface.
 *
 * \param L The flow.
 * \param name A name to used for paging files and so forth.
 * \param u0 The fixed point.
 * \param p0 The parameters of the flow for the fixed point.
 * \param c The manifold of initial conditions.
 * \param Omega A region to bound the computation.
 * \param eps The tolerance on the size of the quadratic terms over a balls. Controls the stepsize.
 * \param dt The initial timestep to use along a fat trajectory.
 * \param tmax The upper limit on trajectory length.
 * \param maxInterp The upper limit on the number of interpolation performed.
 * \param maxCharts The upper limit on the number of charts in the atlas.
 * \param RMax An upper limit to impose on the radius of the balls along the fat trajectories.
 * \param e An MFErrorHandler to handle exceptions and errors.
 * \returns A new Atlas
 */
MFAtlas IMFComputeInvariantManifold(IMFFlow F,MFKVector p0,char *name, MFAtlas c,MFNRegion Omega, double eps, double dt, double tmax, int maxInterp, int maxCharts, double Rmax,MFErrorHandler e)
{
    static char RoutineName[]= {"IMFComputeInvariantManifold"};
    double s0[2],s1[2];
    double *ddu;
    double *du;
    MFKVector s;
    MFNVector ug,v;
    MFNKMatrix Phi;
    MFNKMatrix Psi;
    int i;
    int ii,jj;
    IMFExpansion cE,U;
    MFContinuationMethod H;
    MFAtlas A;
    MFAtlas I;
    MFImplicitMF M;
    double R,r;
    char sname[1024];
    FILE *fid;
    MFNKMatrix TS;
    MFNVector ui;
    double t;
    int chart;
    MFNVector sigma;
    MFKVector zero;
    double *u0;
    int n,k;
    int verbose=0;

    if(verbose) {
        printf("in %s\n",RoutineName);
        fflush(stdout);
    }

    n=MFAtlasN(c,e);
    k=MFAtlasK(c,e);
    M=IMFCreateFlat(n,k+1,e);
    MFIMFSetProjectForDraw(M,MFIMFGetProjectForDraw(MFAtlasMF(c,e),e),e);
    MFIMFSetProjectForSave(M,MFIMFGetProjectForSave(MFAtlasMF(c,e),e),e);
    MFIMFSetProjectForBB  (M,MFIMFGetProjectForBB  (MFAtlasMF(c,e),e),e);
    MFIMFSetR(M,Rmax/2,e);
    I=MFCreateAtlas(M,e);
    printf("%s, Create Atlas I=0x%8.8x\n",RoutineName,I);
    fflush(stdout);

    H=MFCreateMultifariosMethod(e);
    MFMultifarioSetIntegerParameter(H,"page",0,e);
    MFMultifarioSetIntegerParameter(H,"maxCharts",-1,e);
    MFMultifarioSetIntegerParameter(H,"dumpToPlotFile",1,e);
    MFMultifarioSetFilename(H,name,e);

    u0=(double*)malloc(n*sizeof(double));

#ifndef MFNOSAFETYNET
    if(u0==NULL)
    {
        sprintf(MFNSpaceErrorMsg,"Out of memory trying to allocate %d bytes\n",n*k*sizeof(double));
        MFSetError(e,12,RoutineName,MFNSpaceErrorMsg,__LINE__,__FILE__);
        MFErrorHandlerOutOfMemory(e);
        return NULL;
    }
#endif

    du=(double*)malloc(n*k*sizeof(double));

#ifndef MFNOSAFETYNET
    if(du==NULL)
    {
        sprintf(MFNSpaceErrorMsg,"Out of memory trying to allocate %d bytes\n",n*k*sizeof(double));
        MFSetError(e,12,RoutineName,MFNSpaceErrorMsg,__LINE__,__FILE__);
        MFErrorHandlerOutOfMemory(e);
        return NULL;
    }
#endif

    ddu=(double*)malloc(n*k*k*sizeof(double));

#ifndef MFNOSAFETYNET
    if(ddu==NULL)
    {
        sprintf(MFNSpaceErrorMsg,"Out of memory trying to allocate %d bytes\n",n*sizeof(double));
        MFSetError(e,12,RoutineName,MFNSpaceErrorMsg,__LINE__,__FILE__);
        MFErrorHandlerOutOfMemory(e);
        return NULL;
    }
#endif

    zero=MFCreateKVector(k-1,e);

    s0[0]=-r;
    s1[0]= r;
    s0[1]=-r;
    s1[1]= r;
    for(i=0; i<n*k*k; i++)ddu[i]=0.;
    for(i=0; i<MFAtlasNumberOfCharts(c,e); i++)
    {
        if(verbose) {
            printf("   chart %d on the initial manifold\n",i);
            fflush(stdout);
        }
        if(MFNV_CStar(MFAtlasChartCenter(c,i,e),e)!=NULL)
        {
            for(ii=0; ii<n; ii++)
                u0[ii]=MFNV_CStar(MFAtlasChartCenter(c,i,e),e)[ii];
        } else {
            for(ii=0; ii<n; ii++)
                u0[ii]=MFNV_C(MFAtlasChartCenter(c,i,e),ii,e);
        }
        Phi=MFAtlasChartTangentSpace(c,i,e);

        if(MFNKM_CStar(Phi,e)!=NULL)
        {
            for(ii=0; ii<n*k; ii++)
                du[ii]=MFNKM_CStar(Phi,e)[ii];
        } else {
            for(ii=0; ii<n; ii++)
                for(jj=0; jj<k; jj++)
                    du[ii+n*jj]=MFNKMGetC(Phi,ii,jj,e);
        }

        cE=IMFCreateExpansion(n,k,e);

        /* Assumes it's a straight line  */
        /* Assumes Phi is a dense matrix */


        IMFExpansionSetDerivatives(cE,u0,du,ddu,NULL,e);
        U=IMFInflateExpansionWithFlow(cE,F,p0,e);

        ui=IMFCreateExpansionNVector(U,0.,MFAtlasCenterOfChart(c,i,e),-1,1,e);
        if(0&&i==0) /* Why??? */
        {
            TS=IMFExpansionTS(cE,e);
            R=MFAtlasChartRadius(c,i,e);
            MFAtlasAddChartWithAll(I,ui,TS,R,e);
        }
        IMFExpansionNVSetChart0(ui,i,e);
        IMFExpansionNVSetS0(ui,zero,e);
        printf("*) Point on c\n");
        fflush(stdout);
        MFPrintNVector(stdout,ui,e);
        printf("   Extend along fat traj\n");
        fflush(stdout);
        IMFExtendAtlasAlongFatTraj(I,F,name,ui,p0,dt,0.,tmax,eps,Omega,maxCharts,maxCharts,Rmax,0,e);
        printf("   done fat traj\n");
        fflush(stdout);
        if(0&&MFAtlasNumberOfCharts(I,e)%1000==0)MFAtlasPageOutChartsNotNearBoundary(I,1,0,name,e);
        MFFreeNVector(ui,e);

        IMFFreeExpansion(cE,e);
        IMFFreeExpansion(U,e);
    }

    printf("*** Done covering c\n");
    fflush(stdout);
#ifdef DOINTERPOLATIONPT
    sprintf(sname,"%s.intpt",name);
    IMFInterp=fopen(sname,"w");
    IMFNInterp=0;
#endif

    i=0;
    while(i<maxInterp&& (ui=IMFGetInterpolationPoint(I,F,p0,A,tmax,NULL,e))!=NULL && MFAtlasNumberOfCharts(I,e)<maxCharts)
    {
        printf("*** Interpolated Point %d\n",i);
        fflush(stdout);
#ifdef DOINTERPOLATIONPT
        for(j=0; j<3; j++)fprintf(IMFInterp," %lf",IMFExpansionU(IMFExpansionNVGetE(ui,e))[j],e);
        fprintf(IMFInterp,"\n");
        fflush(IMFInterp);
        IMFNInterp++;
#endif
        IMFExtendAtlasAlongFatTraj(I,F,name,ui,p0,dt,IMFExpansionNVGetT(ui,e),tmax,eps,Omega,maxCharts,maxCharts,Rmax,0,e);
        if(0&&MFAtlasNumberOfCharts(I,e)%1000==0)MFAtlasPageOutChartsNotNearBoundary(I,1,0,name,e);
        MFFreeNVector(ui,e);
        i++;
    }

    if(i<maxInterp)
    {
        printf("*** No more interpolation points, total was %d\n",i);
        fflush(stdout);
    }
    else
    {
        printf("*** Max number of interpolation points reached, %d\n",i);
        fflush(stdout);
    }

    MFFlushAtlas(H,I,e);
    MFCloseAtlas(H,I,e);

    MFFreeContinuationMethod(H,e);
    MFFreeImplicitMF(M,e);

    free(u0);
    free(du);
    free(ddu);

#ifdef DOINTERPOLATIONPT
    if(IMFInterp!=NULL)
    {
        fclose(IMFInterp);

        sprintf(sname,"i%s.dx",name);
        IMFInterp=fopen(sname,"w");
        sprintf(sname,"%s.intpt",name);
        if(IMFNInterp>0)
        {
            fprintf(IMFInterp,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file %s.dx\n",sname,IMFNInterp);
        } else {
            fprintf(IMFInterp,"object \"Vertices\" class array type float rank 1 shape 3 items 1 data follows\n");
            fprintf(IMFInterp,"  0. 0. 0.\n");
        }
        fprintf(IMFInterper,"object \"interpolated points\" class field\n");
        fprintf(IMFInterper,"component \"positions\" value \"Vertices\"\n");
        fclose(IMFInterper);
    }
#endif

#ifdef DOINTERPANIM

    if(IMFInterper!=NULL)
    {
        fclose(IMFInterper);
        IMFInterper=fopen("gInterp1.dx","w");
        if(IMFNInterper>0)
        {
            fprintf(IMFInterper,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file Interp1.dx\n",IMFNInterper);
        } else {
            fprintf(IMFInterper,"object \"Vertices\" class array type float rank 1 shape 3 items 1 data follows\n");
            fprintf(IMFInterper,"  0. 0. 0.\n");
        }
        fprintf(IMFInterper,"object \"interpolated points\" class field\n");
        fprintf(IMFInterper,"component \"positions\" value \"Vertices\"\n");
        fclose(IMFInterper);
    }

    if(IMFInterpee!=NULL)
    {
        fclose(IMFInterpee);
        IMFInterpee=fopen("gInterp2.dx","w");
        if(IMFNInterpee>0)
        {
            fprintf(IMFInterpee,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file Interp2.dx\n",IMFNInterpee);
            fprintf(IMFInterpee,"object \"Lines\" class array type int rank 1 shape 2 items %d data follows\n",IMFNInterpee/2);
            for(i=0; i<IMFNInterpee; i+=2)
                fprintf(IMFInterpee,"   %d %d\n",i,i+1);
        } else {
            fprintf(IMFInterpee,"object \"Vertices\" class array type float rank 1 shape 3 items 2 data follows\n");
            fprintf(IMFInterpee,"  0. 0. 0.\n");
            fprintf(IMFInterpee,"  0. 0. 0.001\n");
            fprintf(IMFInterpee,"object \"Lines\" class array type int rank 1 shape 2 items 1 data follows\n");
            fprintf(IMFInterpee,"   %d %d\n",0,1);
        }
        fprintf(IMFInterpee,"attribute \"ref\" string \"positions\"\n");
        fprintf(IMFInterpee,"attribute \"element type\" string \"lines\"\n");

        fprintf(IMFInterpee,"object \"interpolation points\" class field\n");
        fprintf(IMFInterpee,"component \"positions\" value \"Vertices\"\n");
        fprintf(IMFInterpee,"component \"connections\" value \"Lines\"\n");
        fclose(IMFInterpee);
    }

    if(IMFInterpT!=NULL)
    {
        fclose(IMFInterpT);
        IMFInterpT=fopen("gInterp3.dx","w");
        if(IMFNInterpT>0)
        {
            fprintf(IMFInterpT,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file Interp3.dx\n",IMFNInterpT
                   );
            fprintf(IMFInterpT,"object \"Lines\" class array type int rank 1 shape 2 items %d data follows\n",IMFNInterpT-1);
            for(i=0; i<IMFNInterpT-1; i++)
                fprintf(IMFInterpT,"   %d %d\n",i,i+1);
        } else {
            fprintf(IMFInterpT,"object \"Vertices\" class array type float rank 1 shape 3 items 2 data follows\n");
            fprintf(IMFInterpT,"  0. 0. 0.\n");
            fprintf(IMFInterpT,"  0. 0. 0.001\n");
            fprintf(IMFInterpT,"object \"Lines\" class array type int rank 1 shape 2 items 1 data follows\n");
            fprintf(IMFInterpT,"   %d %d\n",0,1);
        }
        fprintf(IMFInterpT,"attribute \"ref\" string \"positions\"\n");
        fprintf(IMFInterpT,"attribute \"element type\" string \"lines\"\n");

        fprintf(IMFInterpT,"object \"interpolation points\" class field\n");
        fprintf(IMFInterpT,"component \"positions\" value \"Vertices\"\n");
        fprintf(IMFInterpT,"component \"connections\" value \"Lines\"\n");
        fclose(IMFInterpT);
    }

    if(IMFCircle!=NULL)
    {
        fclose(IMFCircle);
        IMFCircle=fopen("gIMFCircle.dx","w");
        fprintf(IMFCircle,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file IMFCircle.dx\n",IMFNCircle);
        fprintf(IMFCircle,"object \"Lines\" class array type int rank 1 shape 2 items %d data follows\n",IMFNCircle/2);
        for(i=0; i<IMFNCircle; i+=2)
            fprintf(IMFCircle,"   %d %d\n",i,i+1);
        fprintf(IMFCircle,"attribute \"ref\" string \"positions\"\n");
        fprintf(IMFCircle,"attribute \"element type\" string \"lines\"\n");

        fprintf(IMFCircle,"object \"interpolation points\" class field\n");
        fprintf(IMFCircle,"component \"positions\" value \"Vertices\"\n");
        fprintf(IMFCircle,"component \"connections\" value \"Lines\"\n");
        fclose(IMFCircle);
    }
#endif

#ifdef DOTRAJ
    if(IMFTraj!=NULL)
    {
        printf("dump trajectory file\n");
        fflush(stdout);
        fclose(IMFTraj);
        IMFTraj=fopen("gIMFTraj.dx","w");
        fprintf(IMFTraj,"object \"Vertices\" class array type float rank 1 shape 3 items %d data file IMFTraj.dx\n",IMFNTraj);
        fprintf(IMFTraj,"object \"Lines\" class array type int rank 1 shape 2 items %d data follows\n",IMFNTraj/2);
        for(i=0; i<IMFNTraj; i+=2)
            fprintf(IMFTraj,"   %d %d\n",i,i+1);
        fprintf(IMFTraj,"attribute \"ref\" string \"positions\"\n");
        fprintf(IMFTraj,"attribute \"element type\" string \"lines\"\n");

        fprintf(IMFTraj,"object \"trajectories\" class field\n");
        fprintf(IMFTraj,"component \"positions\" value \"Vertices\"\n");
        fprintf(IMFTraj,"component \"connections\" value \"Lines\"\n");
        fclose(IMFTraj);
    }
#endif

    printf("done %s\n",RoutineName);
    fflush(stdout);


    return I;
}
int main(int argc,char *argv[])
 {
  MFAUTOTPBVP tpbvp;
  MFNSpace space;
  MFNRegion Omega;
  MFImplicitMF M;
  MFContinuationMethod H;
  MFNVector u0;
  MFNKMatrix Phi0;
  MFAtlas S;
  MFErrorHandler e;

  double epsilon=1.00;
  int through=10;
  doublereal rl0[1]={0.};
  doublereal rl1[1]={20.};
  doublereal a0=0.;
  doublereal a1=100.;
  doublereal ds=0.1;

  integer npar=3;
  integer nicp=1;
  integer icp[1]={0};
  doublereal par[3];

  doublereal thu[2]={1.,1.};
  doublereal thl[3]={1.,1.,1.};

  integer k=1;
  integer jac=1;
  integer ntst=5,ncol=4,ndim=2;
  integer nbc=1,nic=1;

  e=MFCreateErrorHandler();

  tpbvp=MFCreateAUTOTPBVP(k,ndim,f,jac,nbc,a,nic,ic,npar,nicp,icp,ntst,ncol,plt,e);

  space=MFCreateAUTONSpace(tpbvp,thu,thl,e);
  M=MFCreateAUTOBV(tpbvp,space,e);

  MFAUTOBVSetRealParameter(M,"dlmin",0.0001,e);
  MFAUTOBVSetRealParameter(M,"dsmax",2.0,e);
  MFAUTOBVSetRealParameter(M,"ds0",0.1,e);
  MFAUTOBVSetRealParameter(M,"epsl",1.e-4,e);
  MFAUTOBVSetRealParameter(M,"epsu",1.e-4,e);
  MFAUTOBVSetIntegerParameter(M,"iid",0,e);

/*MFAUTOAddUserZero(M,0,1.,e);
  MFAUTOAddUserZero(M,0,3.,e);
  MFAUTODetectLimitPoints(M,e);*/

  Omega=MFNRegionCreateAUTO(space,k,icp,rl0,rl1,a0,a1,e);

  H=MFCreateMultifariosMethod(e);
  MFMultifarioSetRealParameter(H,"minR",0.0001,e);
  MFMultifarioSetRealParameter(H,"maxR",2.0,e);
  MFMultifarioSetRealParameter(H,"epsilon",epsilon,e);
  MFMultifarioSetIntegerParameter(H,"maxCharts",-1,e);
  MFMultifarioSetIntegerParameter(H,"verbose",1,e);
  MFMultifarioSetIntegerParameter(H,"page",1,e);
  MFMultifarioSetIntegerParameter(H,"branchSwitch",1,e);
  MFMultifarioSetIntegerParameter(H,"dumpToPlotFile",1,e);
  MFMultifarioSetIntegerParameter(H,"dumpToCenterFile",0,e);
  MFMultifarioSetFilename(H,"AUTINTMultifario",e);

  par[0]=0.;
  par[1]=0.;
  par[2]=0.;
  MFAUTOGetStartPoint(M,tpbvp,stpnt,par,&u0,&Phi0,e);

  S=MFComputeAtlasWithTangent(H,M,Omega,u0,Phi0,e);
  MFCloseAtlas(H,S,e);

  MFFreeNRegion(Omega,e);
  MFFreeNSpace(space,e);
  MFFreeNVector(u0,e);
  if(Phi0!=(MFNKMatrix)NULL)MFFreeNKMatrix(Phi0,e);
  MFFreeImplicitMF(M,e);
  MFFreeContinuationMethod(H,e);
  MFFreeAtlas(S,e);

  MFFreeErrorHandler(e);

  return 0;
 } 
MFChart IMFStepAlongFatTraj(MFAtlas I, IMFFlow F, MFNVector u0, MFKVector p0, double R0, double dt, double epsilon,MFNRegion Omega, double Rmax, MFErrorHandler e)
 {
  static char RoutineName[]={"IMFStepAlongFatTraj"};

  double t,tout;
  MFNKMatrix TS;
  double R;
  int i,j;
  int p,ni;
  double d;
  int nsteps;
  int neq,n;
  IMFExpansion E;
  MFNVector u;
  MFNVector f,g;
  double *fp;
  MFKVector s;
  double *y0;
  double *y;
  MFListOfCharts L;
  MFBinaryTree BTree;
  MFNSpace space;
  int k,prev;
  IMFFlow Fp;
  int nearChart;
  double dMin;
  double Rf;
  MFChart c;
  int bail;
  int verbose=0;
  double *BBCenter=NULL;
  int BBDimension;

  if(MFAtlasNumberOfCharts(I,e)>0)
   {
    BBDimension=MFIMFProjectToBB(MFAtlasMF(I,e),MFAtlasChartCenter(I,0,e),NULL,e);
    BBCenter=(double*)malloc(BBDimension*sizeof(double));
    printf("%s, BBDimension=%d\n",RoutineName,BBDimension);fflush(stdout);
   }

  E=IMFExpansionNVGetE(u0,e);

#ifdef MFALLOWVERBOSE
  if(verbose){printf("Initial expansion\n");IMFPrintExpansion(stdout,E,e);fflush(stdout);}
#endif

  neq=IMFExpansionDataLn(E,e);
  space=MFIMFNSpace(MFAtlasMF(I,e),e);
  BTree=MFAtlasGetBB(I,e);

  t=0.;
  R=R0;

  n=IMFFlowNU(F,e);
  k=IMFExpansionK(E,e);

  Fp=IMFCreateFatFlow(F,k,e);

  fp=(double*)malloc(n*sizeof(double));

#ifndef MFNOSAFETYNET
  if(fp==NULL)
   {
    sprintf(IMFIntegrateFatErrorMsg,"Out of memory, trying to allocate %d bytes",n*sizeof(double));
    MFSetError(e,12,RoutineName,IMFIntegrateFatErrorMsg,__LINE__,__FILE__);
    MFErrorHandlerOutOfMemory(e);
    if(BBCenter!=NULL)free(BBCenter);
    return 0;
   }
#endif

  f=MFCreateWrappedNVector(n,fp,e);
  g=MFCreateNVector(n,e);
  s=MFCreateKVector(IMFExpansionK(E,e),e);

  nsteps=0;
  u=u0;
  MFRefNVector(u,e);
  y=(double*)malloc(IMFExpansionDataLn(E,e)*sizeof(double));

#ifndef MFNOSAFETYNET
  if(y==NULL)
   {
    sprintf(IMFIntegrateFatErrorMsg,"Out of memory, trying to allocate %d bytes",IMFExpansionDataLn(E,e)*sizeof(double));
    MFSetError(e,12,RoutineName,IMFIntegrateFatErrorMsg,__LINE__,__FILE__);
    MFErrorHandlerOutOfMemory(e);
    if(BBCenter!=NULL)free(BBCenter);
    return 0;
   }
#endif

  while(1)
   {
    E=IMFExpansionNVGetE(u,e);

/* find the next */

    y0=IMFExpansionData(E,e);
    for(i=0;i<IMFExpansionDataLn(E,e);i++)y[i]=y0[i];

    d=0;
    tout=t+R;
    while(t+1.e-7<=tout && d<1.03*R)
     {
      if(!MEHKellerInt(Fp,y,p0,t,tout,2,e)){
       printf("Ending integration, Integrator failed to converge\n");fflush(stdout);goto FreeAndReturn;}
/* was ,5); */
    
      for(i=0;i<n;i++)
       {
        if(y[i]!=y[i]){printf("Ending integration, Integrator returned a NaN\n");fflush(stdout);goto FreeAndReturn;}
       }
      d=0.;for(i=0;i<n;i++)d+=pow(y[i]-y0[i],2);d=sqrt(d);
      t=tout;
      tout=t+.1*R;
     }
    E=IMFCreateExpansion(n,k,e);
    IMFExpansionSetDerivatives(E,y,y+n,y+n*k,NULL,e);

    u0=u;
    u=IMFCreateExpansionNVector(E,t,IMFExpansionNVGetSigma(u0,e),prev,0,e);
    MFFreeNVector(u0,e);
    IMFFreeExpansion(E,e);
    bail=0;

    if(!MFNRegionInterior(Omega,u,e)){printf("Ending integration, point is outside Omega. ");MFPrintNVector(stdout,u,e);printf("\n");fflush(stdout);bail=1;}

    IMFEvaluateFlow(F,u,p0,fp,e);
    d=sqrt(MFNSpaceInner(space,f,f,e));
    if(d<epsilon){printf("Ending integration, |F| (%le<%le) too small (near fixed point)\n",d,epsilon);fflush(stdout);bail=1;}

    E=IMFExpansionNVGetE(u,e);
    TS=IMFExpansionTS(E,e);

    MFNSpaceScale(space,1./d,f,f,e);
    MFMVMulT(space,TS,f,s,e);
    MFMVMul(space,TS,s,g,e);
    d=MFNSpaceDistance(space,f,g,e);
    if(0&&d>epsilon){printf("Ending integration, F too far from tangent space, d=%l2\n",d);fflush(stdout);bail=1;}

    R=IMFExpansionR(E,epsilon,e);
    Rf=IMFFlowR(F,epsilon,u,p0,TS,e);
    if(Rf<R)R=Rf;
    if(R>Rmax||R!=R)R=Rmax;
    c=MFCreateChart(MFAtlasMF(I,e),u,TS,R,e);
    if(0&&R<epsilon){printf("Ending integration, R too small\n");fflush(stdout);bail=1;}

/* find closest point to keep trajectories from getting too close */

    nearChart=-1;
    if(BBCenter!=NULL)
     {
      MFIMFProjectToBB(MFAtlasMF(I,e),MFChartCenter(c,e),BBCenter,e);
      L=MFCreateListOfNearbyCharts(BTree,BBCenter,R,e);
      ni=MFNumberOfIntersectingCharts(L,e);
      nearChart=-1;
      dMin=2*R;
      if(ni==0)dMin=0;
      for(p=0;p<ni;p++)
       {
        j=MFIntersectingChart(L,p,e);
        if(MFChartPaged(MFAtlasChart(I,j,e),e))continue;
        if(!IMFIsNear(I,MFAtlasChart(I,j,e),c,e))continue;
        d=MFNSpaceDistance(space,MFAtlasCenterOfChart(I,j,e),u,e);
        if(MFAtlasChartRadius(I,j,e)&&(nearChart==-1||d<dMin))
         {
          dMin=d;
          nearChart=j;
         }
       }
      printf("   Check that this point is away from others. dMin=%lf\n",dMin);fflush(stdout);
      if(nearChart==-1){printf("    point is OK, continue.\n");fflush(stdout);}
       else            {printf("    point is too close to another.\n");fflush(stdout);}
      MFFreeListOfIntersectingCharts(L,e);
     }else{
      printf("   There are no other points, skip check if point is away from others.\n");fflush(stdout);
      nearChart=-1;
     }

/* Passed checks, add chart */

    prev=-1;
    if((nearChart==-1||IMFExpansionNVGetType(u,e)==1)||IMFExpansionNVGetType(u,e)==2)
     {
      goto FreeAndReturn;
     }else{
      printf("*) Point %d on fat traj. ",nsteps);MFPrintNVector(stdout,u,e);printf(", R=%lf, t=%lf\n",R,t);fflush(stdout);
      printf("    is too near chart %d (distance = %lf)\n",nearChart,dMin);fflush(stdout);
      printf("    is too near chart %d (distance = %lf)\n",nearChart,dMin);fflush(stdout);
      bail=1;
     }

    if(bail){if(prev>-1)MFChartSetSingular(MFAtlasChart(I,prev,e),e);MFFreeChart(c,e);c=NULL;goto FreeAndReturn;}

    MFFreeChart(c,e);
    nsteps++;
   }

FreeAndReturn:
  MFFreeNVector(u,e);
  MFFreeNVector(f,e);
  free(fp);
  IMFFreeFlow(Fp,e);
  MFFreeNVector(g,e);
  MFFreeKVector(s,e);
  free(y);

/*printf("done %s\n",RoutineName);fflush(stdout);*/
  if(BBCenter!=NULL)free(BBCenter);
  return c;
 }
Exemple #20
0
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;
 }
int MEHKellerInt(IMFFlow F,double *y,MFKVector p0,double ti,double tf,int nsteps, MFErrorHandler e)
 {
  static char RoutineName[]={"MEHKellerInt"};
  double t,dt;
  static int meq=-1;
  static double *y0=NULL;
  static double *ym=NULL;
  static double *y1=NULL;
  static double *f=NULL;
  static double *A=NULL;
  static double *b=NULL;
  double fNorm;
  int i,j,itimes;
  double error;
  int verbose=0;
  int neq;
  MFNVector vy;
  MFNVector vym;
  MFNVector vy0;

  neq=IMFFlowNU(F,e);

#ifdef MFALLOWVERBOSE
  if(verbose){printf("%s, neq=%d integrate from t=%lf to %lf\n",RoutineName,neq,ti,tf);fflush(stdout);}
#endif

  if(meq<neq)
   {
    y0=(double*)realloc((void*)y0,neq*sizeof(double));

#ifndef MFNOSAFETYNET
    if(y0==NULL)
     {
      sprintf(IMFIntegrateFatErrorMsg,"Out of memory, trying to allocate %d bytes",neq*sizeof(double));
      MFSetError(e,12,RoutineName,IMFIntegrateFatErrorMsg,__LINE__,__FILE__);
      MFErrorHandlerOutOfMemory(e);
      return 0;
     }
#endif

    ym=(double*)realloc((void*)ym,neq*sizeof(double));

#ifndef MFNOSAFETYNET
    if(ym==NULL)
     {
      sprintf(IMFIntegrateFatErrorMsg,"Out of memory, trying to allocate %d bytes",neq*sizeof(double));
      MFSetError(e,12,RoutineName,IMFIntegrateFatErrorMsg,__LINE__,__FILE__);
      MFErrorHandlerOutOfMemory(e);
      return 0;
     }
#endif

    y1=(double*)realloc((void*)y1,neq*sizeof(double));

#ifndef MFNOSAFETYNET
    if(y1==NULL)
     {
      sprintf(IMFIntegrateFatErrorMsg,"Out of memory, trying to allocate %d bytes",neq*sizeof(double));
      MFSetError(e,12,RoutineName,IMFIntegrateFatErrorMsg,__LINE__,__FILE__);
      MFErrorHandlerOutOfMemory(e);
      return 0;
     }
#endif

    f =(double*)realloc((void*)f ,neq*sizeof(double));

#ifndef MFNOSAFETYNET
    if(f==NULL)
     {
      sprintf(IMFIntegrateFatErrorMsg,"Out of memory, trying to allocate %d bytes",neq*sizeof(double));
      MFSetError(e,12,RoutineName,IMFIntegrateFatErrorMsg,__LINE__,__FILE__);
      MFErrorHandlerOutOfMemory(e);
      return 0;
     }
#endif

    A =(double*)realloc((void*)A ,neq*neq*sizeof(double));

#ifndef MFNOSAFETYNET
    if(A==NULL)
     {
      sprintf(IMFIntegrateFatErrorMsg,"Out of memory, trying to allocate %d bytes",neq*neq*sizeof(double));
      MFSetError(e,12,RoutineName,IMFIntegrateFatErrorMsg,__LINE__,__FILE__);
      MFErrorHandlerOutOfMemory(e);
      return 0;
     }
#endif

    b =(double*)realloc((void*)b ,neq*sizeof(double));

#ifndef MFNOSAFETYNET
    if(b==NULL)
     {
      sprintf(IMFIntegrateFatErrorMsg,"Out of memory, trying to allocate %d bytes",neq*sizeof(double));
      MFSetError(e,12,RoutineName,IMFIntegrateFatErrorMsg,__LINE__,__FILE__);
      MFErrorHandlerOutOfMemory(e);
      return 0;
     }
#endif

    meq=neq;
   }

  vy=MFCreateWrappedNVector(neq,y,e);
  vym=MFCreateWrappedNVector(neq,ym,e);
  vy0=MFCreateWrappedNVector(neq,y0,e);

  IMFEvaluateFlow(F,vy,p0,f,e);
  fNorm=0.;for(i=0;i<neq;i++)fNorm+=f[i]*f[i]; fNorm=sqrt(fNorm);
  if(fNorm>10.)fNorm=10.;


  nsteps=round(10*(tf-ti)*fNorm+2);

  t=ti;
  dt=(tf-ti)/nsteps;

#ifdef MFALLOWVERBOSE
  if(verbose){printf("   nsteps=%d, fNorm=%lf\n",nsteps,fNorm);fflush(stdout);}
#endif

  for(j=0;j<neq;j++)y0[j]=y[j];
  for(i=0;i<nsteps;i++)
   {

#ifdef MFALLOWVERBOSE
    if(verbose){printf("Keller Box for IVP, step %d/%d, t=%lf\n",i,nsteps,t);fflush(stdout);  }
#endif

    IMFEvaluateFlow(F,vy0,p0,f,e);

    for(j=0;j<neq;j++)y1[j]=y0[j]+dt*f[j];

    error=1.;
    itimes=0;
    while(error>1.e-5)
     {
      for(j=0;j<neq;j++)ym[j]=.5*(y0[j]+y1[j]);
      IMFEvaluateFlow(F,vym,p0,f,e);
      IMFEvaluateDerivativeOfFlow(F,vym,p0,A,e);
      for(j=0;j<neq*neq;j++)A[j]=-.5*dt*A[j];
      error=0.;
      for(j=0;j<neq;j++)
       {
        b[j]=-(y1[j]-y0[j]-dt*f[j]);
        error+=b[j]*b[j];
        A[j+neq*j]=A[j+neq*j]+1.;
       }

#ifdef MFALLOWVERBOSE
      if(verbose){printf("Iteration %d, error=%le\n",itimes,error);fflush(stdout);  }
#endif

      MFSolveFull(neq,A,b,e);
      for(j=0;j<neq;j++)y1[j]+=b[j];
      itimes++;
      if(itimes>100)return 0;
     }
    t+=dt;
    for(j=0;j<neq;j++)y0[j]=y1[j];
   }
  for(j=0;j<neq;j++)y[j]=y1[j];

  MFFreeNVector(vy,e);
  MFFreeNVector(vym,e);
  MFFreeNVector(vy0,e);
  return 1;
 }
int main(int argc, char *argv[])
 {
  MFImplicitMF M;
  int n;
  MFNRegion Omega;
  MFAtlas S;
  MFNVector u0,u;
  MFNKMatrix Phi;
  FILE *fid;
  MFContinuationMethod H;
  MFErrorHandler e;

  e=MFCreateErrorHandler();

  M=MFIMFCreateAlgebraicExpression("[x,y,z]",
  "[((sqrt((x-.8)**2+y**2)-.8)**2+z**2 -.5**2)*((sqrt((x+.8)**2+y**2)-.8)**2+z**2 -.5**2)-.05]",e);
  n=MFIMF_N(M,e);
  Omega=MFNRegionCreateHyperCube(n,2.4,e);

/* This bit starts with an approximate point on M, and calls project to get a point on M */

  u=MFIMFVectorFactory(M,e);
  MFNVSetC(u,0, 1.2,e);
  MFNVSetC(u,1, 0.,e);
  MFNVSetC(u,2, 0.,e);
  u0=MFIMFVectorFactory(M,e);
  Phi=MFIMFMatrixFactory(M,e);

  MFNKMSetC(Phi,0,0,0.,e);
  MFNKMSetC(Phi,1,0,1.,e);
  MFNKMSetC(Phi,2,0,0.,e);
  MFNKMSetC(Phi,0,1,0.,e);
  MFNKMSetC(Phi,1,1,0.,e);
  MFNKMSetC(Phi,2,1,1.,e);

  if(!MFIMFProject(M,u,Phi,u0,e))return 8;

  H=MFCreateMultifariosMethod(e);
  MFMultifarioSetRealParameter(H,"epsilon",.01,e);
  MFMultifarioSetIntegerParameter(H,"maxCharts",-1,e);
  MFMultifarioSetIntegerParameter(H,"verbose",1,e);
  MFMultifarioSetIntegerParameter(H,"page",1,e);
  MFMultifarioSetIntegerParameter(H,"dumpToPlotFile",1,e);
  MFMultifarioSetIntegerParameter(H,"dumpToCenterFile",0,e);
  MFMultifarioSetFilename(H,"GenusTwo",e);

  S=MFComputeAtlas(H,M,Omega,u0,e);

  MFCloseAtlas(H,S,e);
  printf("Done computing Atlas\n");fflush(stdout);

  MFFreeAtlas(S,e);
  MFFreeImplicitMF(M,e);
  MFFreeContinuationMethod(H,e);
  MFFreeNRegion(Omega,e);
  MFFreeNVector(u0,e);

  MFFreeErrorHandler(e);

  return(0);
 }
MFAtlas IMFComputeUnstableInvariantManifold2(IMFFlow F,char *name, MFNVector u0,MFKVector p0,MFNRegion Omega, double eps, double dt, double tmax, int maxInterp, int maxCharts, double R0,MFErrorHandler e)
{
    static char RoutineName[]= {"ComputeUnstableInvariantManifold2"};
    IMFExpansion u,a;
    double s0[2],s1[2];
    MFKVector s;
    MFNVector ug,v;
    MFNVector ustar;
    MFNKMatrix Phi0;
    MFNKMatrix Phi;
    MFNKMatrix Psi;
    MFImplicitMF c;
    int i,j,l;
    IMFExpansion cE,U;
    MFContinuationMethod H;
    MFAtlas A;
    MFAtlas I;
    MFImplicitMF M;
    double R,r;
    double Rf,Rmax;
    char sname[1024];
    FILE *fid;
    MFNKMatrix TS;
    MFNVector ui;
    MFNVector ut;
    double t;
    int chart;
    MFNVector sigma;
    int n,k;
    int nSkip;
    int iring,maxRings;
    double epsilon=1.;

    int nCharts;
    int *chartList=NULL;
    int mCharts;
    MFChart *cList=NULL;
    MFChart nchart;
    double dist,d;
    MFKVector zero;

    Rmax=1.;

    Phi=IMFGetBasisForUnstableInvariantSubspace(F,u0,p0,e);
    Psi=IMFGetBasisForOrthogonalComplement(Phi,e);

    n=MFNKMatrixN(Phi,e);
    k=MFNKMatrixK(Phi,e);
    printf("n=%d, k=%d\n",n,k);
    fflush(stdout);
    u=IMFCreateExpansion(n,k,e);
    a=IMFCreateExpansion(k,k,e);
    IMFFindExpansionNearFixedPt(u0,p0,Phi,Psi,F,u,a,e);
    zero=MFCreateKVector(k-1,e);

    R=IMFExpansionR(u,eps,e);
    if(R!=R||R>R0)R=R0;
    r=.5*R;
    c=IMFCreateSphereOnExpansion(u,F,p0,eps,R,r,e);

    s =MFCreateKVector(2,e);
    MFKVSetC(s,0,R,e);
    MFKVSetC(s,1,0.,e);

    ug=MFCreateNVector(n,e);
    IMFEvaluateExpansion(u,s,ug,e);

    ut=MFCreateNVector(n+k,e);
    for(i=0; i<n; i++)MFNVSetC(ut,i,MFNV_C(ug,i,e),e);
    for(i=0; i<k; i++)MFNVSetC(ut,n+i,MFKV_C(s,i,e),e);
    MFFreeNVector(ug,e);

    ustar=MFCreateNVector(n+k,e);

    Phi0=MFIMFTangentSpace(c,ut,e);
    if(MFIMFProject(c,ut,Phi0,ustar,e))
    {

        H=MFCreateMultifariosMethod(e);
        MFMultifarioSetRealParameter(H,"epsilon",.8,e);
        MFMultifarioSetIntegerParameter(H,"maxCharts",-1,e);
        MFMultifarioSetIntegerParameter(H,"verbose",0,e);
        MFMultifarioSetIntegerParameter(H,"page",0,e);
        MFMultifarioSetIntegerParameter(H,"dumpToPlotFile",0,e);
        MFMultifarioSetIntegerParameter(H,"dumpToCenterFile",0,e);
        MFMultifarioSetFilename(H,"SphereOnU0",e);

        A=MFComputeAtlas(H,c,Omega,ustar,e);
        MFFreeNVector(ut,e);
        MFCloseAtlas(H,A,e);
        MFFreeContinuationMethod(H,e);
        printf("Done computing Atlas\n");
        fflush(stdout);
    } else {
        printf("Projection of initial point failed\n");
        fflush(stdout);
        return NULL;
    }

    M=IMFCreateFlat(n,k,e);
    I=MFCreateAtlas(M,e);
    /*MFAtlasSetNearRtn(I,IMFIsNear,e);*/

    H=MFCreateMultifariosMethod(e);
    MFMultifarioSetIntegerParameter(H,"page",0,e);
    MFMultifarioSetIntegerParameter(H,"dumpToPlotFile",1,e);
    MFMultifarioSetFilename(H,name,e);

    TS=IMFExpansionTS(u,e);
    ui=IMFCreateExpansionNVector(u,-100.,u0,-1,1,e);
    chart=MFAtlasAddChartWithAll(I,ui,TS,R,e);
    printf("%d) Fixed Point. ",chart);
    MFPrintNVector(stdout,ui,e);
    printf(", R=%lf\n",R);
    fflush(stdout);
    MFFreeNVector(ui,e);
    MFFreeNKMatrix(TS,e);

    /* --------------------------------------------------------------------- */

    s0[0]=-r;
    s1[0]= r;
    s0[1]=-r;
    s1[1]= r;
    for(i=0; i<MFAtlasNumberOfCharts(A,e); i++)
    {
        cE=IMFSphereOnExpansionGetLocal(A,MFAtlasCenterOfChart(A,i,e),e);
        U=IMFInflateExpansionWithFlow(cE,F,p0,e);

        ui=IMFCreateExpansionNVector(U,0.,MFAtlasCenterOfChart(A,i,e),-1,1,e);
        IMFExpansionNVSetChart0(ui,i,e);
        IMFExpansionNVSetS0(ui,zero,e);

        TS=IMFExpansionTS(U,e);
        R=IMFExpansionR(U,.5*epsilon,e);
        Rf=IMFFlowR(F,.5*epsilon,ui,p0,TS,e);
        if(Rf<R)R=Rf;
        if(R>Rmax||R!=R)R=Rmax;
        chart=MFAtlasAddChartWithAll(I,ui,TS,R,e);
        printf("%d) Point on c ",chart);
        MFPrintNVector(stdout,ui,e);
        printf(", R=%lf\n",R);
        fflush(stdout);

        MFFreeNKMatrix(TS,e);
        MFFreeNVector(ui,e);
        IMFFreeExpansion(cE,e);
        IMFFreeExpansion(U,e);
        if(MFAtlasNumberOfCharts(I,e)>maxCharts)
        {
            printf("Maximum number of charts exceeded (%d>%d).\n",MFAtlasNumberOfCharts(I,e),maxCharts);
            fflush(stdout);
            goto DoneCover;
        }
    }

    printf("*** Done covering c\n");
    fflush(stdout);

    /* --------------------------------------------------------------------- */

    i=0;
    iring=0;
    maxRings=1;
    while(iring<maxRings&&i<maxInterp&&MFAtlasNumberOfCharts(I,e)<maxCharts&&MFAtlasNumberOfChartsWithBoundary(I,e)>0)
    {
        nCharts=MFAtlasNumberOfChartsWithBoundary(I,e);
        chartList=(int*)realloc((void*)chartList,nCharts*sizeof(int));

#ifndef MFNOSAFETYNET
        if(chartList==NULL)
        {
            sprintf(MFNSpaceErrorMsg,"Out of memory trying to allocate %d bytes\n",nCharts*sizeof(int));
            MFSetError(e,12,RoutineName,MFNSpaceErrorMsg,__LINE__,__FILE__);
            MFErrorHandlerOutOfMemory(e);
            return NULL;
        }
#endif

        mCharts=0;
        cList=(MFChart*)realloc((void*)cList,nCharts*sizeof(MFChart));

#ifndef MFNOSAFETYNET
        if(cList==NULL)
        {
            sprintf(MFNSpaceErrorMsg,"Out of memory trying to allocate %d bytes\n",nCharts*sizeof(MFChart));
            MFSetError(e,12,RoutineName,MFNSpaceErrorMsg,__LINE__,__FILE__);
            MFErrorHandlerOutOfMemory(e);
            return NULL;
        }
#endif

        for(j=0; j<nCharts; j++)
            chartList[j]=MFAtlasChartWithBoundary(I,j,e);

        printf("Extend ring %d\n",iring);
        fflush(stdout);
        for(j=0; j<nCharts; j++)
        {
            ui=MFAtlasChartCenter(I,chartList[j],e);
            nSkip=1;
            printf("-------Front point %d/%d\n",j,nCharts);
            fflush(stdout);
            nchart=IMFStepAlongFatTraj(I,F,ui,p0,MFAtlasChartRadius(I,chartList[j],e),dt,eps,Omega,Rmax,e);

            if(nchart!=NULL)
            {
                cList[mCharts]=nchart;
                mCharts++;
            }

            printf("-------\n");
            fflush(stdout);
        }
        iring++;

        for(j=0; j<mCharts; j++)
        {
            for(l=0; l<j; l++)
            {
                d=MFNSpaceDistance(MFIMFNSpace(MFAtlasMF(I,e),e),MFChartCenter(cList[j],e),MFChartCenter(cList[l],e),e)/MFChartRadius(cList[j],e);
                if(l==0||d<dist)dist=d;
            }
            if(1||j==0||dist>1.)
                MFAtlasAddChartWithAll(I,MFChartCenter(cList[j],e),MFChartTangentSpace(cList[j],e),MFChartRadius(cList[j],e),e);
        }

        for(j=0; j<mCharts; j++)MFFreeChart(cList[j],e);

        for(j=0; j<nCharts; j++)
        {
            if(nCharts>1&&!MFChartHasBoundary(MFAtlasChart(I,chartList[j],e),e) )
            {
                if(j!=nCharts-1)chartList[j]=chartList[nCharts-1];
                nCharts--;
                if(j>0)j--;
            }
        }

        while(0&&i<maxInterp&& (ui=IMFGetInterpolationPointOnList(I,F,p0,A,tmax,NULL,nCharts,chartList,e))!=NULL && MFAtlasNumberOfCharts(I,e)<maxCharts)
        {
            printf("*** Interpolated Point %d\n",i);
            fflush(stdout);

            U=IMFExpansionNVGetE(ui,e);
            TS=IMFExpansionTS(U,e);
            R=IMFExpansionR(U,epsilon,e);
            Rf=IMFFlowR(F,epsilon,ui,p0,TS,e);
            if(Rf<R)R=Rf;
            if(R>Rmax||R!=R)R=Rmax;
            MFAtlasAddChartWithAll(I,ui,TS,R,e);

            MFFreeNKMatrix(TS,e);
            IMFFreeExpansion(cE,e);
            IMFFreeExpansion(U,e);
            MFFreeNVector(ui,e);
            if(MFAtlasNumberOfCharts(I,e)>maxCharts)
            {
                printf("Maximum number of charts exceeded (%d>%d).\n",MFAtlasNumberOfCharts(I,e),maxCharts);
                fflush(stdout);
                goto DoneCover;
            }
            i++;

            for(j=0; j<nCharts; j++)
            {
                if(nCharts>1&&!MFChartHasBoundary(MFAtlasChart(I,chartList[j],e),e) )
                {
                    if(j!=nCharts-1)chartList[j]=chartList[nCharts-1];
                    nCharts--;
                    if(j>0)j--;
                }
            }
        }
    }

    if(i<maxInterp)
    {
        printf("*** No more boundary, total was %d interpolated, %d charts\n",i,MFAtlasNumberOfCharts(I,e));
        fflush(stdout);
    }
    else
    {
        printf("*** Max number of interpolation points reached, %d\n",i);
        fflush(stdout);
    }

    /* --------------------------------------------------------------------- */

DoneCover:

    MFFreeImplicitMF(c,e);
    MFFreeKVector(s,e);

    printf("Flushing invariant manifold\n");
    MFCloseAtlas(H,I,e);
    MFFreeContinuationMethod(H,e);

    MFFreeNKMatrix(Phi0,e);

    MFFreeNVector(ustar,e);
    MFFreeNKMatrix(Phi,e);
    MFFreeNKMatrix(Psi,e);

    IMFFreeExpansion(u,e);
    IMFFreeExpansion(a,e);
    MFFreeImplicitMF(M,e);
    MFFreeAtlas(A,e);
    free(chartList);
    free(cList);
    free(zero);

    return I;
}
Exemple #24
0
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;
  }
}
int main(int argc, char *argv[])
 {
  MFImplicitMF M;
  int i,n;
  MFNRegion Omega;
  MFAtlas S;
  MFNVector u0;
  FILE *fid;
  char *vars;
  char *expr;
  char *digits[]={"0","1","2","3","4","5","6","7","8","9"};
  char name[80]="";
  MFContinuationMethod H;
  MFErrorHandler e;

  e=MFCreateErrorHandler();

  n=4;
  if(argc>1)sscanf(argv[1],"%d",&n);
  if(n<1||n>9)
   {
    fprintf(stderr,"%s currently only works for 0<n<10. You supplied n=%d\n",n);
    fflush(stderr);
    return 12;
   }
  vars=(char*)malloc((2+3*n)*sizeof(char));
  strcpy(vars,"[");
  for(i=0;i<n;i++)
   {
    if(i>0)strcat(vars,",");
    strcat(vars,"v");
    strcat(vars,digits[i]);
   }
  strcat(vars,"]");

  expr=(char*)malloc((5+6*n)*sizeof(char));
  strcpy(expr,"[");
  for(i=0;i<n;i++)
   {
    if(i>0)strcat(expr,"+");
    strcat(expr,"v");
    strcat(expr,digits[i]);
    strcat(expr,"**2");
   }
  strcat(expr,"-1.]");

  printf("Variables : %s\n",vars);
  fflush(stdout);
  printf("Expression: %s\n",expr);
  fflush(stdout);

  M=MFIMFCreateAlgebraicExpression(vars,expr,e);
  MFIMFSetR(M,sqrt(3.)/2.1,e);
  Omega=MFNRegionCreateHyperCube(n,3.,e);

  u0=MFIMFVectorFactory(M,e);
  MFNVSetC(u0,0, 1.,e);
  for(i=1;i<n;i++)MFNVSetC(u0,i, 0.,e);

  H=MFCreateMultifariosMethod(e);
  MFMultifarioSetRealParameter(H,"epsilon",1.,e);
  MFMultifarioSetIntegerParameter(H,"maxCharts",-1,e);
  MFMultifarioSetIntegerParameter(H,"verbose",1,e);
  MFMultifarioSetIntegerParameter(H,"page",0,e);
  MFMultifarioSetIntegerParameter(H,"dumpToPlotFile",1,e);
  MFMultifarioSetIntegerParameter(H,"dumpToCenterFile",0,e);
  strcpy(name,"SpherePacking");
  strcat(name,digits[n]);
  MFMultifarioSetFilename(H,name,e);

  S=MFComputeAtlas(H,M,Omega,u0,e);

  MFCloseAtlas(H,S,e);
  printf("Done computing manifold\n");fflush(stdout);

  MFFreeAtlas(S,e);
  MFFreeImplicitMF(M,e);
  MFFreeNRegion(Omega,e);
  MFFreeNVector(u0,e);
  MFFreeContinuationMethod(H,e);
  free(vars);
  free(expr);

  MFFreeErrorHandler(e);

  return 0;
 }
Exemple #26
0
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;
 }
Exemple #27
0
int MFChartPageIn(MFChart thisChart, FILE *fid, MFErrorHandler e)
 {
  static int i,j;
  double *c;
  long pos;
  int verbose=0;
  static char RoutineName[]={"MFChartPageIn"};

#ifdef MFALLOWVERBOSE
  if(verbose)
   {
    printf("%s!!!!!\n",RoutineName);
    fflush(stdout);
   }
#endif

  sprintf(MFChartErrorMsg,"Routine %s is broken, FATAL ERROR",RoutineName);
  MFSetError(e,16,RoutineName,MFChartErrorMsg,__LINE__,__FILE__);
  return -1;

/* Returns 1 if paged in, 0 if not paged out */

  if(!(thisChart->paged))return 0;

#ifdef MFALLOWVERBOSE
  if(verbose){fprintf(stderr,"PAGING IN CHART! %d in file\n",thisChart->indexInPageFile);fflush(stderr);}
#endif

  if(thisChart->u!=NULL && thisChart->Phi!=NULL)
   {
#ifdef MFALLOWVERBOSE
    if(verbose){fprintf(stderr,"PAGING Already in\n");fflush(stderr);}
#endif
    return 1;
   }

  if(thisChart->u!=NULL)MFFreeNVector(thisChart->u,e);
  thisChart->u=MFCreateNVector(thisChart->n,e);

  if(thisChart->Phi!=NULL)MFFreeNKMatrix(thisChart->Phi,e);
  thisChart->Phi=MFCreateNKMatrixWithData(thisChart->n,thisChart->k,NULL,e);

#ifdef MFALLOWVERBOSE
  if(verbose){fprintf(stderr,"PAGING IN CHART! read from file\n");}
#endif

  fseek(fid,thisChart->indexInPageFile,SEEK_SET);

#ifdef MFALLOWVERBOSE
  if(verbose){fprintf(stderr,"ftell= %d\n",ftell(fid));}
#endif

  c=MFNV_CStar(thisChart->u,e);
  for(i=0;i<thisChart->n;i++)fscanf(fid,"%lf ",&(c[i]));
  fscanf(fid,"\n");

#ifdef MFALLOWVERBOSE
  if(verbose){fprintf(stderr,"PAGING   u starts (%lf,%lf,%lf...\n",c[0],c[1],c[2]);fflush(stderr);}
#endif

  c=MFNKM_CStar(thisChart->Phi,e);
  for(j=0;j<thisChart->k;j++)
  for(i=0;i<thisChart->n;i++)fscanf(fid,"%lf ",&(c[i+thisChart->n*j]));
  fscanf(fid,"\n");

#ifdef MFALLOWVERBOSE
  if(verbose){fprintf(stderr,"PAGING   Phi starts (%lf,%lf,%lf...\n",c[0],c[1],c[2]);fflush(stderr);}
#endif

  fseek(fid,0,SEEK_END);

  return 1;
 }
void IMFExtendAtlasAlongFatTraj(MFAtlas I, IMFFlow F, char *name, MFNVector u0, MFKVector p0, double dt, double ti, double tf,double epsilon,MFNRegion Omega, int maxSteps, int maxCharts,double Rmax,int nSkip, MFErrorHandler e)
 {
  static char RoutineName[]={"IMFExtendAtlasAlongFatTraj"};

  double t,tout;
  MFNKMatrix TS;
  double R;
  int i,j;
  int p,ni;
  double d;
  int nsteps;
  int neq,n;
  IMFExpansion E;
  MFNVector u;
  MFNVector f,g;
  double *fp;
  MFKVector s;
  double *y0;
  double *y;
  MFListOfCharts L;
  MFBinaryTree BTree;
  MFNSpace space;
  int k,prev;
  IMFFlow Fp;
  int nearChart;
  double dMin;
  double Rf;
  MFChart c;
  int bail;
  int verbose=0;
  double z[3];
  double *BBCenter=NULL;
  int BBDimension;

  if(MFAtlasNumberOfCharts(I,e)>0)
   {
    BBDimension=MFIMFProjectToBB(MFAtlasMF(I,e),MFAtlasChartCenter(I,0,e),NULL,e);
    BBCenter=(double*)malloc(BBDimension*sizeof(double));
    printf("   BBDimension=%d\n",BBDimension);fflush(stdout);
   }

#ifdef DOTRAJ
  if(IMFTraj==NULL)
   {
    IMFTraj=fopen("IMFTraj.dx","w");
    IMFNTraj=0;
   }
#endif

  E=IMFExpansionNVGetE(u0,e);
#ifdef MFALLOWVERBOSE
  if(verbose){printf("Initial expansion\n");IMFPrintExpansion(stdout,E,e);fflush(stdout);}
#endif
  neq=IMFExpansionDataLn(E,e);
  space=MFIMFNSpace(MFAtlasMF(I,e),e);
  BTree=MFAtlasGetBB(I,e);

/* find closest point to keep trajectories from getting too close */

  if(MFAtlasNumberOfCharts(I,e)>0)
   {
    MFIMFProjectToBB(MFAtlasMF(I,e),u0,BBCenter,e);
    L=MFCreateListOfNearbyCharts(BTree,BBCenter,R,e);
    ni=MFNumberOfIntersectingCharts(L,e);
    nearChart=-1;
    dMin=2*R;
    if(ni==0)dMin=0;
    for(p=0;p<ni;p++)
     {
      j=MFIntersectingChart(L,p,e);
      if(MFChartPaged(MFAtlasChart(I,j,e),e))continue;
      d=MFNSpaceDistance(space,MFAtlasCenterOfChart(I,j,e),u0,e);
      if(d<.5*MFAtlasChartRadius(I,j,e)&&(nearChart==-1||d<dMin))
       {
        dMin=d;
        nearChart=j;
       }
     }
    printf("   Check that initial point is away from others. dMin=%lf\n",dMin);fflush(stdout);
    if(nearChart==-1){printf("    point on c is OK, continue.\n");fflush(stdout);}
     else            {printf("    point on c is too close to another, go on to next.\n");fflush(stdout);}
    MFFreeListOfIntersectingCharts(L,e);
    if(nearChart!=-1){if(BBCenter!=NULL)free(BBCenter);return;}
   }else{
    printf("   There are no other points, skip check if point is away from others.\n");fflush(stdout);
    nearChart=-1;
   }
  if(nearChart!=-1)
   {
    if(BBCenter!=NULL)free(BBCenter);
    return;
   }

  t=ti;

  n=IMFFlowNU(F,e);
  k=IMFExpansionK(E,e);

  printf("CreateFatFlow\n");fflush(stdout);
  Fp=IMFCreateFatFlow(F,k,e);

  fp=(double*)malloc(n*sizeof(double));

#ifndef MFNOSAFETYNET
  if(fp==NULL)
   {
    sprintf(IMFIntegrateFatErrorMsg,"Out of memory, trying to allocate %d bytes",n*sizeof(double));
    MFSetError(e,12,RoutineName,IMFIntegrateFatErrorMsg,__LINE__,__FILE__);
    MFErrorHandlerOutOfMemory(e);
    if(BBCenter!=NULL)free(BBCenter);
    return;
   }
#endif

  f=MFCreateWrappedNVector(n,fp,e);
  g=MFCreateNVector(n,e);
  s=MFCreateKVector(IMFExpansionK(E,e),e);

  nsteps=0;
  u=u0;
  MFRefNVector(u,e);
  while(1)
   {
    printf(" step %d\n",nsteps);fflush(stdout);

    bail=0;
    if(t>tf){printf("Ending integration, final time reached (%lf>%lf)\n",t,tf);fflush(stdout);bail=1;}

    if(nsteps>=maxSteps){printf("Ending integration, too many steps\n");fflush(stdout);goto FreeAndReturn;}

    if(MFAtlasNumberOfCharts(I,e)>=maxCharts){printf("Ending integration, too many charts\n");fflush(stdout);goto FreeAndReturn;}

    if(!MFNRegionInterior(Omega,u,e)){printf("Ending integration, point is outside Omega. ");MFPrintNVector(stdout,u,e);printf("\n");fflush(stdout);bail=1;}

    IMFEvaluateFlow(F,u,p0,fp,e);
    d=sqrt(MFNSpaceInner(space,f,f,e));
    if(d<epsilon){printf("Ending integration, |F| (%le<%le) too small (near fixed point)\n",d,epsilon);fflush(stdout);bail=1;}

    E=IMFExpansionNVGetE(u,e);
    TS=IMFExpansionTS(E,e);

    MFNSpaceScale(space,1./d,f,f,e);
    MFMVMulT(space,TS,f,s,e);
    MFMVMul(space,TS,s,g,e);
    d=MFNSpaceDistance(space,f,g,e);
    if(0&&d>epsilon){printf("Ending integration, F too far from tangent space, d=%l2\n",d);fflush(stdout);bail=1;}

    R=IMFExpansionR(E,epsilon,e);
    Rf=IMFFlowR(F,epsilon,u,p0,TS,e);
    if(Rf<R)R=Rf;
    if(R>Rmax||R!=R)R=Rmax;
    c=MFCreateChart(MFAtlasMF(I,e),u,TS,R,e);
    if(0&&R<epsilon){printf("Ending integration, R too small\n");fflush(stdout);bail=1;}

/* find closest point to keep trajectories from getting too close */

    nearChart=-1;
    if(MFAtlasNumberOfCharts(I,e)>0)
     {
      MFIMFProjectToBB(MFAtlasMF(I,e),MFChartCenter(c,e),BBCenter,e);
      L=MFCreateListOfNearbyCharts(BTree,BBCenter,R,e);
      ni=MFNumberOfIntersectingCharts(L,e);
      nearChart=-1;
      dMin=2*R;
      if(ni==0)dMin=0;
      for(p=0;p<ni;p++)
       {
        j=MFIntersectingChart(L,p,e);
        if(MFChartPaged(MFAtlasChart(I,j,e),e))continue;
        if(!IMFIsNear(I,MFAtlasChart(I,j,e),c,e))continue;
        d=MFNSpaceDistance(space,MFAtlasCenterOfChart(I,j,e),u,e);
        if(d<.5*MFAtlasChartRadius(I,j,e)&&(nearChart==-1||d<dMin))
         {
          dMin=d;
          nearChart=j;
         }
       }
      printf("   Check that this point is away from others. dMin=%lf\n",dMin);fflush(stdout);
      if(nearChart==-1){printf("    point is OK, continue.\n");fflush(stdout);}
       else            {printf("    point is too close to another.\n");fflush(stdout);}
      MFFreeListOfIntersectingCharts(L,e);
      MFFreeChart(c,e);
     }else{
      printf("   There are no other points, skip check if point is away from others.\n");fflush(stdout);
      nearChart=-1;
     }

/* Passed checks, add chart */

/* NV Types: 
           0     normal
           1     fixed point or on c
           2     interpolation point
           3     skipped point, not on manifold - don't plot
*/

    prev=-1;
    if(  ((nearChart==-1||IMFExpansionNVGetType(u,e)==1)||IMFExpansionNVGetType(u,e)==2) && nsteps>=nSkip )
     {
      printf("%d) Point %d on fat traj.",MFAtlasNumberOfCharts(I,e),nsteps);MFPrintNVector(stdout,u,e);printf(", R=%lf, t=%lf\n",R,t);fflush(stdout);
      prev=MFAtlasAddChartWithAll(I,u,TS,R,e);
      if(BTree==NULL)BTree=MFAtlasGetBB(I,e);
      if(BBCenter==NULL)
       {
        BBDimension=MFIMFProjectToBB(MFAtlasMF(I,e),MFAtlasChartCenter(I,0,e),NULL,e);
        BBCenter=(double*)malloc(BBDimension*sizeof(double));
        printf("%s, BBDimension=%d\n",RoutineName,BBDimension);fflush(stdout);
       }
      if(0&&MFAtlasNumberOfCharts(I,e)%1000==0){printf("Paging\n");MFAtlasPageOutChartsNotNearBoundary(I,1,0,name,e);}
      if(prev>0 && (IMFExpansionNVGetType(u,e)==1||IMFExpansionNVGetType(u,e)==2))MFChartSetSingular(MFAtlasChart(I,prev,e),e);
       else if(prev>1)MFChartSetNonSingular(MFAtlasChart(I,prev,e),e);
#ifdef DOTRAJ
         {
          int m;
          m=MFIMFProjectToDraw(MFAtlasMF(I,e),NULL,NULL,e);
          MFIMFProjectToDraw(MFAtlasMF(I,e),u0,z,e);
          for(i=0;i<m;i++)fprintf(IMFTraj," %lf",z[i]);fprintf(IMFTraj,"\n");fflush(IMFTraj);IMFNTraj++;
          MFIMFProjectToDraw(MFAtlasMF(I,e),u,z,e);
          for(i=0;i<m;i++)fprintf(IMFTraj," %lf",z[i]);fprintf(IMFTraj,"\n");fflush(IMFTraj);IMFNTraj++;
         }
#endif

     }else{
      printf("*) Point %d on fat traj. ",nsteps);MFPrintNVector(stdout,u,e);printf(", R=%lf, t=%lf\n",R,t);fflush(stdout);
      if(nsteps<nSkip){printf("    is being skipped, %d<%d\n",nsteps,nSkip);fflush(stdout);}
        else {printf("    is too near chart %d (distance = %lf)\n",nearChart,dMin);fflush(stdout);}
      prev=MFAtlasAddChartToList(I,MFCreateChart(MFAtlasMF(I,e),u,TS,R,e),e);
      t=2*tf+1;
      if(0&&MFAtlasNumberOfCharts(I,e)%1000==0){printf("Paging\n");MFAtlasPageOutChartsNotNearBoundary(I,1,0,name,e);}
      IMFExpansionNVSetType(u,3,e);
      MFChartSetPaged(MFAtlasChart(I,prev,e),e);
     }
    printf("  done adding new point\n");fflush(stdout);

    if(bail){if(prev>-1)MFChartSetSingular(MFAtlasChart(I,prev,e),e);goto FreeAndReturn;}

    nsteps++;

/* Now find the next */

    y0=IMFExpansionData(E,e);
    y=(double*)malloc(IMFExpansionDataLn(E,e)*sizeof(double));

#ifndef MFNOSAFETYNET
    if(y==NULL)
     {
      sprintf(IMFIntegrateFatErrorMsg,"Out of memory, trying to allocate %d bytes",IMFExpansionDataLn(E,e)*sizeof(double));
      MFSetError(e,12,RoutineName,IMFIntegrateFatErrorMsg,__LINE__,__FILE__);
      MFErrorHandlerOutOfMemory(e);
    if(BBCenter!=NULL)free(BBCenter);
      return;
     }
#endif

    for(i=0;i<IMFExpansionDataLn(E,e);i++)y[i]=y0[i];
#ifdef DOTRAJ
    if(0&&nsteps>nSkip)
     {
      int m;
      MFNVector u;
      m=MFIMFProjectToDraw(MFAtlasMF(I,e),NULL,NULL,e);
      u=MFCreateWrappedNVector(IMFExpansionDataLn(E,e),y,e);
      MFIMFProjectToDraw(MFAtlasMF(I,e),u,z,e);
      for(i=0;i<m;i++)fprintf(IMFTraj," %lf",z[i]);fprintf(IMFTraj,"\n");fflush(IMFTraj);IMFNTraj++;
      MFFreeNVector(u,e);
     }
#endif

    d=0;
    tout=t+R;
    while(t+1.e-7<=tout && t<tf && d<1.03*R)
     {
      if(!MEHKellerInt(Fp,y,p0,t,tout,2,e)){
#ifdef DOTRAJ
  if(0){
                int m;
              MFNVector u;
          m=MFIMFProjectToDraw(MFAtlasMF(I,e),NULL,NULL,e);
          u=MFCreateWrappedNVector(IMFExpansionDataLn(E,e),y,e);
          MFIMFProjectToDraw(MFAtlasMF(I,e),u,z,e);
          for(i=0;i<m;i++)fprintf(IMFTraj," %lf",z[i]);fprintf(IMFTraj,"\n");fflush(IMFTraj);IMFNTraj++;
          MFFreeNVector(u,e);
         }
#endif
        printf("Ending integration, Integrator failed to converge\n");fflush(stdout);goto FreeAndReturn;}
/* was ,5); */
    
      for(i=0;i<n;i++)
       {
        if(y[i]!=y[i]){printf("Ending integration, Integrator returned a NaN\n");fflush(stdout);goto FreeAndReturn;}
       }
      d=0.;for(i=0;i<n;i++)d+=pow(y[i]-y0[i],2);d=sqrt(d);
      t=tout;
      tout=t+.1*R;
     }
#ifdef DOTRAJ
     if(0){
      int m;
      MFNVector u;
     m=MFIMFProjectToDraw(MFAtlasMF(I,e),NULL,NULL,e);
    u=MFCreateWrappedNVector(IMFExpansionDataLn(E,e),y,e);
          MFIMFProjectToDraw(MFAtlasMF(I,e),u,z,e);
          for(i=0;i<m;i++)fprintf(IMFTraj," %lf",z[i]);fprintf(IMFTraj,"\n");fflush(IMFTraj);IMFNTraj++;
          MFFreeNVector(u,e);
         }
#endif
    E=IMFCreateExpansion(n,k,e);
    IMFExpansionSetDerivatives(E,y,y+n,y+n*k,NULL,e);
    free(y);

    u0=u;
    u=IMFCreateExpansionNVector(E,t,IMFExpansionNVGetSigma(u0,e),prev,0,e);
    IMFExpansionNVSetS0(u,IMFExpansionNVGetS0(u0,e),e);
    IMFExpansionNVSetChart0(u,IMFExpansionNVGetChart0(u0,e),e);
    MFFreeNVector(u0,e);
    IMFFreeExpansion(E,e);
   }

FreeAndReturn:
  MFFreeNVector(u,e);
  MFFreeNVector(f,e);
  free(fp);
  IMFFreeFlow(Fp,e);
  MFFreeNVector(g,e);
  MFFreeKVector(s,e);

/*printf("done IMFExtendAtlasAlongFatTraj\n");fflush(stdout);*/
  if(BBCenter!=NULL)free(BBCenter);
  return;
 }
Exemple #29
0
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;
 }
Exemple #30
0
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;
 }