Commits

Lisandro Dalcin committed e97d45e

More work on Add IGA{Load|Save}Vec()

Comments (0)

Files changed (5)

demo/InputOutput.c

   ierr = IGACreateVec(iga,&vec);CHKERRQ(ierr);
   ierr = VecSet(vec,1.0);CHKERRQ(ierr);
   ierr = IGAWriteVec(iga,vec,"igavec.dat");CHKERRQ(ierr);
+  ierr = VecSet(vec,0.0);CHKERRQ(ierr);
   ierr = IGAReadVec (iga,vec,"igavec.dat");CHKERRQ(ierr);
   ierr = VecGetSize(vec,&size);CHKERRQ(ierr);
   ierr = VecSum(vec,&value);CHKERRQ(ierr);
   VecScatter g2l,l2g;
   PetscInt   nwork;
   Vec        vwork[16];
+  VecScatter g2n;
+  Vec        natural;
 
 };
 
 PETSC_EXTERN PetscErrorCode IGAGlobalToLocal(IGA iga,Vec gvec,Vec lvec);
 PETSC_EXTERN PetscErrorCode IGALocalToGlobal(IGA iga,Vec lvec,Vec gvec,InsertMode addv);
 
+PETSC_EXTERN PetscErrorCode IGAGetNaturalVec(IGA iga,Vec *nvec);
+PETSC_EXTERN PetscErrorCode IGANaturalToGlobal(IGA iga,Vec nvec,Vec gvec);
+PETSC_EXTERN PetscErrorCode IGAGlobalToNatural(IGA iga,Vec gvec,Vec nvec);
+
 PETSC_EXTERN PetscErrorCode IGAGetLocalVecArray(IGA iga,Vec gvec,Vec *lvec,const PetscScalar *array[]);
 PETSC_EXTERN PetscErrorCode IGARestoreLocalVecArray(IGA iga,Vec gvec,Vec *lvec,const PetscScalar *array[]);
 
   ierr = ISLocalToGlobalMappingDestroy(&iga->lgmapb);CHKERRQ(ierr);
   ierr = VecScatterDestroy(&iga->g2l);CHKERRQ(ierr);
   ierr = VecScatterDestroy(&iga->l2g);CHKERRQ(ierr);
+  ierr = VecDestroy(&iga->natural);CHKERRQ(ierr);
+  ierr = VecScatterDestroy(&iga->g2n);CHKERRQ(ierr);
   while (iga->nwork > 0)
     {ierr = VecDestroy(&iga->vwork[--iga->nwork]);CHKERRQ(ierr);}
   ierr = DMDestroy(&iga->node_dm);CHKERRQ(ierr);
     ierr = PetscObjectReference((PetscObject)iga->g2l);CHKERRQ(ierr);
     ierr = IGA_Grid_GetScatterL2G(grid,&iga->l2g);CHKERRQ(ierr);
     ierr = PetscObjectReference((PetscObject)iga->l2g);CHKERRQ(ierr);
+    /* build global to natural vector scatter */
+    ierr = IGA_Grid_GetScatterG2N(grid,&iga->g2n);CHKERRQ(ierr);
+    ierr = PetscObjectReference((PetscObject)iga->g2n);CHKERRQ(ierr);
+    ierr = IGA_Grid_GetVecNatural(grid,VECSTANDARD,&iga->natural);CHKERRQ(ierr);
+    ierr = PetscObjectReference((PetscObject)iga->natural);CHKERRQ(ierr);
     /* destroy the grid context */
     ierr = IGA_Grid_Destroy(&grid);CHKERRQ(ierr);
   }
   ierr = PetscViewerFileSetMode(viewer,FILE_MODE_WRITE);CHKERRQ(ierr);
   ierr = PetscViewerFileSetName(viewer,filename);CHKERRQ(ierr);
   ierr = IGASave(iga,viewer);CHKERRQ(ierr);
+  ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
   PetscFunctionReturn(0);
 }
 #define __FUNCT__ "IGALoadVec"
 PetscErrorCode IGALoadVec(IGA iga,Vec vec,PetscViewer viewer)
 {
-  IGA_Grid       grid;
-  Vec            nvec,gvec,lvec;
-  VecScatter     g2n,g2l;
+  Vec            nvec;
   PetscErrorCode ierr;
   PetscFunctionBegin;
   PetscValidHeaderSpecific(iga,IGA_CLASSID,1);
   PetscCheckSameComm(iga,1,vec,2);
   PetscCheckSameComm(iga,1,viewer,3);
   IGACheckSetUp(iga,1);
+
   if (!IGA_AnyPeriodic(iga)) {
-    ierr = VecLoad(vec,viewer);CHKERRQ(ierr);
-    PetscFunctionReturn(0);
+    ierr = IGAGetNaturalVec(iga,&nvec);
+    ierr = VecLoad(nvec,viewer);CHKERRQ(ierr);
+    ierr = IGANaturalToGlobal(iga,nvec,vec);
+  } else {
+    IGA_Grid   grid;
+    Vec        gvec,lvec;
+    VecScatter g2n,g2l;
+    ierr = IGA_GetGridIO_Vec(iga,&grid);CHKERRQ(ierr);
+    ierr = IGA_Grid_GetVecNatural(grid,VECSTANDARD,&nvec);CHKERRQ(ierr);
+    ierr = IGA_Grid_GetVecGlobal (grid,VECSTANDARD,&gvec);CHKERRQ(ierr);
+    ierr = IGA_Grid_GetVecLocal  (grid,VECSTANDARD,&lvec);CHKERRQ(ierr);
+    ierr = IGA_Grid_GetScatterG2N(grid,&g2n);CHKERRQ(ierr);
+    ierr = IGA_Grid_GetScatterG2L(grid,&g2l);CHKERRQ(ierr);
+    ierr = VecLoad(nvec,viewer);CHKERRQ(ierr);
+    ierr = VecScatterBegin(g2n,nvec,gvec,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
+    ierr = VecScatterEnd  (g2n,nvec,gvec,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
+    ierr = VecScatterBegin(g2l,gvec,lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
+    ierr = VecScatterEnd  (g2l,gvec,lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
+    ierr = IGALocalToGlobal(iga,lvec,vec,INSERT_VALUES);CHKERRQ(ierr);
   }
-
-  ierr = IGA_GetGridIO_Vec(iga,&grid);CHKERRQ(ierr);
-  ierr = IGA_Grid_GetVecNatural(grid,VECSTANDARD,&nvec);CHKERRQ(ierr);
-  ierr = IGA_Grid_GetVecGlobal (grid,VECSTANDARD,&gvec);CHKERRQ(ierr);
-  ierr = IGA_Grid_GetVecLocal  (grid,VECSTANDARD,&lvec);CHKERRQ(ierr);
-  ierr = IGA_Grid_GetScatterG2N(grid,&g2n);CHKERRQ(ierr);
-  ierr = IGA_Grid_GetScatterG2L(grid,&g2l);CHKERRQ(ierr);
-
-  ierr = VecLoad(nvec,viewer);CHKERRQ(ierr);
-  ierr = VecScatterBegin(g2n,nvec,gvec,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
-  ierr = VecScatterEnd  (g2n,nvec,gvec,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
-  ierr = VecScatterBegin(g2l,gvec,lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
-  ierr = VecScatterEnd  (g2l,gvec,lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
-
-  ierr = IGALocalToGlobal(iga,lvec,vec,INSERT_VALUES);CHKERRQ(ierr);
-
   PetscFunctionReturn(0);
 }
 
 #define __FUNCT__ "IGASaveVec"
 PetscErrorCode IGASaveVec(IGA iga,Vec vec,PetscViewer viewer)
 {
-  IGA_Grid       grid;
-  Vec            nvec,gvec,lvec;
-  VecScatter     g2n,l2g;
+  Vec            nvec;
   PetscErrorCode ierr;
   PetscFunctionBegin;
   PetscValidHeaderSpecific(iga,IGA_CLASSID,1);
   PetscCheckSameComm(iga,1,vec,2);
   PetscCheckSameComm(iga,1,viewer,3);
   IGACheckSetUp(iga,1);
+
   if (!IGA_AnyPeriodic(iga)) {
-    ierr = VecView(vec,viewer);CHKERRQ(ierr);
-    PetscFunctionReturn(0);
+    ierr = IGAGetNaturalVec(iga,&nvec);
+    ierr = IGAGlobalToNatural(iga,vec,nvec);
+  } else {
+    IGA_Grid   grid;
+    Vec        gvec,lvec;
+    VecScatter g2n,l2g;
+    ierr = IGA_GetGridIO_Vec(iga,&grid);CHKERRQ(ierr);
+    ierr = IGA_Grid_GetVecLocal  (grid,VECSTANDARD,&lvec);CHKERRQ(ierr);
+    ierr = IGA_Grid_GetVecGlobal (grid,VECSTANDARD,&gvec);CHKERRQ(ierr);
+    ierr = IGA_Grid_GetVecNatural(grid,VECSTANDARD,&nvec);CHKERRQ(ierr);
+    ierr = IGA_Grid_GetScatterL2G(grid,&l2g);CHKERRQ(ierr);
+    ierr = IGA_Grid_GetScatterG2N(grid,&g2n);CHKERRQ(ierr);
+    ierr = IGAGlobalToLocal(iga,vec,lvec);CHKERRQ(ierr);
+    ierr = VecScatterBegin(l2g,lvec,gvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
+    ierr = VecScatterEnd  (l2g,lvec,gvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
+    ierr = VecScatterBegin(g2n,gvec,nvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
+    ierr = VecScatterEnd  (g2n,gvec,nvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
   }
-
-  ierr = IGA_GetGridIO_Vec(iga,&grid);CHKERRQ(ierr);
-  ierr = IGA_Grid_GetVecNatural(grid,VECSTANDARD,&nvec);CHKERRQ(ierr);
-  ierr = IGA_Grid_GetVecGlobal (grid,VECSTANDARD,&gvec);CHKERRQ(ierr);
-  ierr = IGA_Grid_GetVecLocal  (grid,VECSTANDARD,&lvec);CHKERRQ(ierr);
-  ierr = IGA_Grid_GetScatterG2N(grid,&g2n);CHKERRQ(ierr);
-  ierr = IGA_Grid_GetScatterL2G(grid,&l2g);CHKERRQ(ierr);
-
-  ierr = IGAGlobalToLocal(iga,vec,lvec);CHKERRQ(ierr);
-
-  ierr = VecScatterBegin(l2g,lvec,gvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
-  ierr = VecScatterEnd  (l2g,lvec,gvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
-  ierr = VecScatterBegin(g2n,gvec,nvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
-  ierr = VecScatterEnd  (g2n,gvec,nvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
-
   ierr = PetscObjectSetName((PetscObject)nvec,((PetscObject)vec)->name);CHKERRQ(ierr);
   ierr = VecView(nvec,viewer);CHKERRQ(ierr);
 
   ierr = PetscViewerFileSetMode(viewer,FILE_MODE_WRITE);CHKERRQ(ierr);
   ierr = PetscViewerFileSetName(viewer,filename);CHKERRQ(ierr);
   ierr = IGASaveVec(iga,vec,viewer);CHKERRQ(ierr);
+  ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
 
   PetscFunctionReturn(0);
   PetscValidHeaderSpecific(*lvec,VEC_CLASSID,2);
   IGACheckSetUp(iga,1);
   if (iga->nwork < (PetscInt)(sizeof(iga->vwork)/sizeof(Vec))) {
-    iga->vwork[iga->nwork++] = *lvec;
+    iga->vwork[iga->nwork++] = *lvec; *lvec = 0;
   } else {
     ierr = VecDestroy(lvec);CHKERRQ(ierr);
   }
   ierr = IGARestoreLocalVec(iga,lvec);CHKERRQ(ierr);
   PetscFunctionReturn(0);
 }
+
+#undef  __FUNCT__
+#define __FUNCT__ "IGAGetNaturalVec"
+PetscErrorCode IGAGetNaturalVec(IGA iga,Vec *nvec)
+{
+  PetscFunctionBegin;
+  PetscValidHeaderSpecific(iga,IGA_CLASSID,1);
+  PetscValidPointer(nvec,2);
+  IGACheckSetUp(iga,1);
+  *nvec = iga->natural;
+  PetscFunctionReturn(0);
+}
+
+#undef  __FUNCT__
+#define __FUNCT__ "IGANaturalToGlobal"
+PetscErrorCode IGANaturalToGlobal(IGA iga,Vec nvec,Vec gvec)
+{
+  PetscErrorCode ierr;
+  PetscFunctionBegin;
+  PetscValidHeaderSpecific(iga,IGA_CLASSID,1);
+  PetscValidHeaderSpecific(nvec,VEC_CLASSID,2);
+  PetscValidHeaderSpecific(gvec,VEC_CLASSID,3);
+  ierr = VecScatterBegin(iga->g2n,nvec,gvec,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
+  ierr = VecScatterEnd  (iga->g2n,nvec,gvec,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
+  PetscFunctionReturn(0);
+}
+
+#undef  __FUNCT__
+#define __FUNCT__ "IGAGlobalToNatural"
+PetscErrorCode IGAGlobalToNatural(IGA iga,Vec gvec,Vec nvec)
+{
+  PetscErrorCode ierr;
+  PetscFunctionBegin;
+  PetscValidHeaderSpecific(iga,IGA_CLASSID,1);
+  PetscValidHeaderSpecific(gvec,VEC_CLASSID,2);
+  PetscValidHeaderSpecific(nvec,VEC_CLASSID,3);
+  ierr = VecScatterBegin(iga->g2n,gvec,nvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
+  ierr = VecScatterEnd  (iga->g2n,gvec,nvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
+  PetscFunctionReturn(0);
+}