xref: /petsc/src/dm/impls/plex/plexcgns.c (revision c4cbe8f1a17360bb5d0a076fa94f805a50b9f265)
1e1b39ce3SMatthew G. Knepley #define PETSCDM_DLL
2af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>    /*I   "petscdmplex.h"   I*/
3e1b39ce3SMatthew G. Knepley 
41b7ec9c1SMatthew G. Knepley #undef I /* Very old CGNS stupidly uses I as a variable, which fails when using complex. Curse you idiot package managers */
5e1b39ce3SMatthew G. Knepley #if defined(PETSC_HAVE_CGNS)
6e1b39ce3SMatthew G. Knepley #include <cgnslib.h>
7e1b39ce3SMatthew G. Knepley #include <cgns_io.h>
8e1b39ce3SMatthew G. Knepley #endif
9*c4cbe8f1SToby Isaac #if !defined(CGNS_ENUMT)
10*c4cbe8f1SToby Isaac #define CGNS_ENUMT(a) a
11*c4cbe8f1SToby Isaac #endif
12*c4cbe8f1SToby Isaac #if !defined(CGNS_ENUMV)
13*c4cbe8f1SToby Isaac #define CGNS_ENUMV(a) a
14*c4cbe8f1SToby Isaac #endif
15e1b39ce3SMatthew G. Knepley 
1644cd5272SMichael Lange /*@C
1744cd5272SMichael Lange   DMPlexCreateCGNS - Create a DMPlex mesh from a CGNS file.
1844cd5272SMichael Lange 
1944cd5272SMichael Lange   Collective on comm
2044cd5272SMichael Lange 
2144cd5272SMichael Lange   Input Parameters:
2244cd5272SMichael Lange + comm  - The MPI communicator
2344cd5272SMichael Lange . filename - The name of the CGNS file
2444cd5272SMichael Lange - interpolate - Create faces and edges in the mesh
2544cd5272SMichael Lange 
2644cd5272SMichael Lange   Output Parameter:
2744cd5272SMichael Lange . dm  - The DM object representing the mesh
2844cd5272SMichael Lange 
2944cd5272SMichael Lange   Note: http://www.grc.nasa.gov/WWW/cgns/CGNS_docs_current/index.html
3044cd5272SMichael Lange 
3144cd5272SMichael Lange   Level: beginner
3244cd5272SMichael Lange 
3344cd5272SMichael Lange .keywords: mesh,CGNS
3444cd5272SMichael Lange .seealso: DMPlexCreate(), DMPlexCreateCGNS(), DMPlexCreateExodus()
3544cd5272SMichael Lange @*/
3644cd5272SMichael Lange PetscErrorCode DMPlexCreateCGNSFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
3744cd5272SMichael Lange {
3844cd5272SMichael Lange   PetscMPIInt    rank;
3944cd5272SMichael Lange   PetscErrorCode ierr;
4044cd5272SMichael Lange #if defined(PETSC_HAVE_CGNS)
4144cd5272SMichael Lange   int cgid = -1;
4244cd5272SMichael Lange #endif
4344cd5272SMichael Lange 
4444cd5272SMichael Lange   PetscFunctionBegin;
4544cd5272SMichael Lange   PetscValidCharPointer(filename, 2);
4644cd5272SMichael Lange   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4744cd5272SMichael Lange #if defined(PETSC_HAVE_CGNS)
4844cd5272SMichael Lange   if (!rank) {
4944cd5272SMichael Lange     ierr = cg_open(filename, CG_MODE_READ, &cgid);CHKERRQ(ierr);
5044cd5272SMichael Lange     if (cgid <= 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "cg_open(\"%s\",...) did not return a valid file ID", filename);
5144cd5272SMichael Lange   }
5244cd5272SMichael Lange   ierr = DMPlexCreateCGNS(comm, cgid, interpolate, dm);CHKERRQ(ierr);
5344cd5272SMichael Lange   if (!rank) {ierr = cg_close(cgid);CHKERRQ(ierr);}
5444cd5272SMichael Lange #else
5544cd5272SMichael Lange   SETERRQ(comm, PETSC_ERR_SUP, "Loading meshes requires CGNS support. Reconfigure using --with-cgns-dir");
5644cd5272SMichael Lange #endif
5744cd5272SMichael Lange   PetscFunctionReturn(0);
5844cd5272SMichael Lange }
5944cd5272SMichael Lange 
60e1b39ce3SMatthew G. Knepley /*@
6144cd5272SMichael Lange   DMPlexCreateCGNS - Create a DMPlex mesh from a CGNS file ID.
62e1b39ce3SMatthew G. Knepley 
63e1b39ce3SMatthew G. Knepley   Collective on comm
64e1b39ce3SMatthew G. Knepley 
65e1b39ce3SMatthew G. Knepley   Input Parameters:
66e1b39ce3SMatthew G. Knepley + comm  - The MPI communicator
67e1b39ce3SMatthew G. Knepley . cgid - The CG id associated with a file and obtained using cg_open
68e1b39ce3SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
69e1b39ce3SMatthew G. Knepley 
70e1b39ce3SMatthew G. Knepley   Output Parameter:
71e1b39ce3SMatthew G. Knepley . dm  - The DM object representing the mesh
72e1b39ce3SMatthew G. Knepley 
73e1b39ce3SMatthew G. Knepley   Note: http://www.grc.nasa.gov/WWW/cgns/CGNS_docs_current/index.html
74e1b39ce3SMatthew G. Knepley 
75e1b39ce3SMatthew G. Knepley   Level: beginner
76e1b39ce3SMatthew G. Knepley 
77e1b39ce3SMatthew G. Knepley .keywords: mesh,CGNS
78e1b39ce3SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexCreateExodus()
79e1b39ce3SMatthew G. Knepley @*/
80e1b39ce3SMatthew G. Knepley PetscErrorCode DMPlexCreateCGNS(MPI_Comm comm, PetscInt cgid, PetscBool interpolate, DM *dm)
81e1b39ce3SMatthew G. Knepley {
82e1b39ce3SMatthew G. Knepley #if defined(PETSC_HAVE_CGNS)
83e1b39ce3SMatthew G. Knepley   PetscMPIInt    num_proc, rank;
84e1b39ce3SMatthew G. Knepley   PetscSection   coordSection;
85e1b39ce3SMatthew G. Knepley   Vec            coordinates;
86e1b39ce3SMatthew G. Knepley   PetscScalar   *coords;
87f8716870SMatthew G. Knepley   PetscInt      *cellStart, *vertStart;
88e1b39ce3SMatthew G. Knepley   PetscInt       coordSize, v;
89e1b39ce3SMatthew G. Knepley   PetscErrorCode ierr;
90e1b39ce3SMatthew G. Knepley   /* Read from file */
91e1b39ce3SMatthew G. Knepley   char basename[CGIO_MAX_NAME_LENGTH+1];
92e1b39ce3SMatthew G. Knepley   char buffer[CGIO_MAX_NAME_LENGTH+1];
93e1b39ce3SMatthew G. Knepley   int  dim    = 0, physDim = 0, numVertices = 0, numCells = 0;
94e1b39ce3SMatthew G. Knepley   int  nzones = 0;
95e1b39ce3SMatthew G. Knepley #endif
96e1b39ce3SMatthew G. Knepley 
97e1b39ce3SMatthew G. Knepley   PetscFunctionBegin;
98e1b39ce3SMatthew G. Knepley #if defined(PETSC_HAVE_CGNS)
99e1b39ce3SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
100e1b39ce3SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &num_proc);CHKERRQ(ierr);
101e1b39ce3SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
102e1b39ce3SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
103e1b39ce3SMatthew G. Knepley   /* Open CGNS II file and read basic informations on rank 0, then broadcast to all processors */
104e1b39ce3SMatthew G. Knepley   if (!rank) {
105e1b39ce3SMatthew G. Knepley     int nbases, z;
106e1b39ce3SMatthew G. Knepley 
107e1b39ce3SMatthew G. Knepley     ierr = cg_nbases(cgid, &nbases);CHKERRQ(ierr);
108e1b39ce3SMatthew G. Knepley     if (nbases > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single base, not %d\n",nbases);
109e1b39ce3SMatthew G. Knepley     ierr = cg_base_read(cgid, 1, basename, &dim, &physDim);CHKERRQ(ierr);
110e1b39ce3SMatthew G. Knepley     ierr = cg_nzones(cgid, 1, &nzones);CHKERRQ(ierr);
111f8716870SMatthew G. Knepley     ierr = PetscCalloc2(nzones+1, &cellStart, nzones+1, &vertStart);CHKERRQ(ierr);
112e1b39ce3SMatthew G. Knepley     for (z = 1; z <= nzones; ++z) {
113e1b39ce3SMatthew G. Knepley       cgsize_t sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */
114e1b39ce3SMatthew G. Knepley 
115e1b39ce3SMatthew G. Knepley       ierr = cg_zone_read(cgid, 1, z, buffer, sizes);CHKERRQ(ierr);
116e1b39ce3SMatthew G. Knepley       numVertices += sizes[0];
117e1b39ce3SMatthew G. Knepley       numCells    += sizes[1];
118f8716870SMatthew G. Knepley       cellStart[z] += sizes[1] + cellStart[z-1];
119f8716870SMatthew G. Knepley       vertStart[z] += sizes[0] + vertStart[z-1];
120f8716870SMatthew G. Knepley     }
121f8716870SMatthew G. Knepley     for (z = 1; z <= nzones; ++z) {
122f8716870SMatthew G. Knepley       vertStart[z] += numCells;
123e1b39ce3SMatthew G. Knepley     }
124e1b39ce3SMatthew G. Knepley   }
125e1b39ce3SMatthew G. Knepley   ierr = MPI_Bcast(basename, CGIO_MAX_NAME_LENGTH+1, MPI_CHAR, 0, comm);CHKERRQ(ierr);
126e1b39ce3SMatthew G. Knepley   ierr = MPI_Bcast(&dim, 1, MPI_INT, 0, comm);CHKERRQ(ierr);
127e1b39ce3SMatthew G. Knepley   ierr = MPI_Bcast(&nzones, 1, MPI_INT, 0, comm);CHKERRQ(ierr);
128e1b39ce3SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *dm, basename);CHKERRQ(ierr);
129c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
130e1b39ce3SMatthew G. Knepley   ierr = DMPlexSetChart(*dm, 0, numCells+numVertices);CHKERRQ(ierr);
131e1b39ce3SMatthew G. Knepley 
132e1b39ce3SMatthew G. Knepley   /* Read zone information */
133e1b39ce3SMatthew G. Knepley   if (!rank) {
134e1b39ce3SMatthew G. Knepley     int z, c, c_loc, v, v_loc;
135e1b39ce3SMatthew G. Knepley 
136e1b39ce3SMatthew G. Knepley     /* Read the cell set connectivity table and build mesh topology
137e1b39ce3SMatthew G. Knepley        CGNS standard requires that cells in a zone be numbered sequentially and be pairwise disjoint. */
138e1b39ce3SMatthew G. Knepley     /* First set sizes */
139e1b39ce3SMatthew G. Knepley     for (z = 1, c = 0; z <= nzones; ++z) {
140*c4cbe8f1SToby Isaac       CGNS_ENUMT( ZoneType_t )    zonetype;
141e1b39ce3SMatthew G. Knepley       int                         nsections;
142*c4cbe8f1SToby Isaac       CGNS_ENUMT( ElementType_t ) cellType;
143e1b39ce3SMatthew G. Knepley       cgsize_t                    start, end;
144e1b39ce3SMatthew G. Knepley       int                         nbndry, parentFlag;
145e1b39ce3SMatthew G. Knepley       PetscInt                    numCorners;
146e1b39ce3SMatthew G. Knepley 
147e1b39ce3SMatthew G. Knepley       ierr = cg_zone_type(cgid, 1, z, &zonetype);CHKERRQ(ierr);
148*c4cbe8f1SToby Isaac       if (zonetype == CGNS_ENUMV( Structured )) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Can only handle Unstructured zones for CGNS");
149e1b39ce3SMatthew G. Knepley       ierr = cg_nsections(cgid, 1, z, &nsections);CHKERRQ(ierr);
150e1b39ce3SMatthew G. Knepley       if (nsections > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single section, not %d\n",nsections);
151e1b39ce3SMatthew G. Knepley       ierr = cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);CHKERRQ(ierr);
152e1b39ce3SMatthew G. Knepley       /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */
153*c4cbe8f1SToby Isaac       if (cellType == CGNS_ENUMV( MIXED )) {
154e1b39ce3SMatthew G. Knepley         cgsize_t elementDataSize, *elements;
155e1b39ce3SMatthew G. Knepley         PetscInt off;
156e1b39ce3SMatthew G. Knepley 
157e1b39ce3SMatthew G. Knepley         ierr = cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);CHKERRQ(ierr);
158785e854fSJed Brown         ierr = PetscMalloc1(elementDataSize, &elements);CHKERRQ(ierr);
159e1b39ce3SMatthew G. Knepley         ierr = cg_elements_read(cgid, 1, z, 1, elements, NULL);CHKERRQ(ierr);
16059dfc837SStefan Lemvig Glimberg         for (c_loc = start, off = 0; c_loc <= end; ++c_loc, ++c) {
1614c9a562dSMatthew G. Knepley           switch (elements[off]) {
162*c4cbe8f1SToby Isaac           case CGNS_ENUMV( TRI_3 ):   numCorners = 3;break;
163*c4cbe8f1SToby Isaac           case CGNS_ENUMV( QUAD_4 ):  numCorners = 4;break;
164*c4cbe8f1SToby Isaac           case CGNS_ENUMV( TETRA_4 ): numCorners = 4;break;
165*c4cbe8f1SToby Isaac           case CGNS_ENUMV( HEXA_8 ):  numCorners = 8;break;
1664c9a562dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[off]);
167e1b39ce3SMatthew G. Knepley           }
168e1b39ce3SMatthew G. Knepley           ierr = DMPlexSetConeSize(*dm, c, numCorners);CHKERRQ(ierr);
169e1b39ce3SMatthew G. Knepley           off += numCorners+1;
170e1b39ce3SMatthew G. Knepley         }
171e1b39ce3SMatthew G. Knepley         ierr = PetscFree(elements);CHKERRQ(ierr);
172e1b39ce3SMatthew G. Knepley       } else {
173e1b39ce3SMatthew G. Knepley         switch (cellType) {
174*c4cbe8f1SToby Isaac         case CGNS_ENUMV( TRI_3 ):   numCorners = 3;break;
175*c4cbe8f1SToby Isaac         case CGNS_ENUMV( QUAD_4 ):  numCorners = 4;break;
176*c4cbe8f1SToby Isaac         case CGNS_ENUMV( TETRA_4 ): numCorners = 4;break;
177*c4cbe8f1SToby Isaac         case CGNS_ENUMV( HEXA_8 ):  numCorners = 8;break;
178e1b39ce3SMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType);
179e1b39ce3SMatthew G. Knepley         }
18059dfc837SStefan Lemvig Glimberg         for (c_loc = start; c_loc <= end; ++c_loc, ++c) {
181e1b39ce3SMatthew G. Knepley           ierr = DMPlexSetConeSize(*dm, c, numCorners);CHKERRQ(ierr);
182e1b39ce3SMatthew G. Knepley         }
183e1b39ce3SMatthew G. Knepley       }
184e1b39ce3SMatthew G. Knepley     }
185e1b39ce3SMatthew G. Knepley     ierr = DMSetUp(*dm);CHKERRQ(ierr);
186e1b39ce3SMatthew G. Knepley     for (z = 1, c = 0; z <= nzones; ++z) {
187*c4cbe8f1SToby Isaac       CGNS_ENUMT( ElementType_t ) cellType;
188e1b39ce3SMatthew G. Knepley       cgsize_t                    *elements, elementDataSize, start, end;
189e1b39ce3SMatthew G. Knepley       int                          nbndry, parentFlag;
190e1b39ce3SMatthew G. Knepley       PetscInt                    *cone, numc, numCorners, maxCorners = 27;
191e1b39ce3SMatthew G. Knepley 
192e1b39ce3SMatthew G. Knepley       ierr = cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);CHKERRQ(ierr);
193e1b39ce3SMatthew G. Knepley       numc = end - start;
194e1b39ce3SMatthew G. Knepley       /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */
195e1b39ce3SMatthew G. Knepley       ierr = cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);CHKERRQ(ierr);
196dcca6d9dSJed Brown       ierr = PetscMalloc2(elementDataSize,&elements,maxCorners,&cone);CHKERRQ(ierr);
197e1b39ce3SMatthew G. Knepley       ierr = cg_elements_read(cgid, 1, z, 1, elements, NULL);CHKERRQ(ierr);
198*c4cbe8f1SToby Isaac       if (cellType == CGNS_ENUMV( MIXED )) {
199e1b39ce3SMatthew G. Knepley         /* CGNS uses Fortran-based indexing, sieve uses C-style and numbers cell first then vertices. */
20059dfc837SStefan Lemvig Glimberg         for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) {
2014c9a562dSMatthew G. Knepley           switch (elements[v]) {
202*c4cbe8f1SToby Isaac           case CGNS_ENUMV( TRI_3 ):   numCorners = 3;break;
203*c4cbe8f1SToby Isaac           case CGNS_ENUMV( QUAD_4 ):  numCorners = 4;break;
204*c4cbe8f1SToby Isaac           case CGNS_ENUMV( TETRA_4 ): numCorners = 4;break;
205*c4cbe8f1SToby Isaac           case CGNS_ENUMV( HEXA_8 ):  numCorners = 8;break;
2064c9a562dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[v]);
207e1b39ce3SMatthew G. Knepley           }
2084c9a562dSMatthew G. Knepley           ++v;
209e1b39ce3SMatthew G. Knepley           for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) {
210e1b39ce3SMatthew G. Knepley             cone[v_loc] = elements[v]+numCells-1;
211e1b39ce3SMatthew G. Knepley           }
212e1b39ce3SMatthew G. Knepley           /* Tetrahedra are inverted */
213*c4cbe8f1SToby Isaac           if (elements[v] == CGNS_ENUMV( TETRA_4 )) {
214e1b39ce3SMatthew G. Knepley             PetscInt tmp = cone[0];
215e1b39ce3SMatthew G. Knepley             cone[0] = cone[1];
216e1b39ce3SMatthew G. Knepley             cone[1] = tmp;
217e1b39ce3SMatthew G. Knepley           }
218e1b39ce3SMatthew G. Knepley           /* Hexahedra are inverted */
219*c4cbe8f1SToby Isaac           if (elements[v] == CGNS_ENUMV( HEXA_8 )) {
220f8716870SMatthew G. Knepley             PetscInt tmp = cone[5];
221f8716870SMatthew G. Knepley             cone[5] = cone[7];
222f8716870SMatthew G. Knepley             cone[7] = tmp;
223e1b39ce3SMatthew G. Knepley           }
224e1b39ce3SMatthew G. Knepley           ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr);
225c58f1c22SToby Isaac           ierr = DMSetLabelValue(*dm, "zone", c, z);CHKERRQ(ierr);
226e1b39ce3SMatthew G. Knepley         }
227e1b39ce3SMatthew G. Knepley       } else {
228e1b39ce3SMatthew G. Knepley         switch (cellType) {
229*c4cbe8f1SToby Isaac         case CGNS_ENUMV( TRI_3 ):   numCorners = 3;break;
230*c4cbe8f1SToby Isaac         case CGNS_ENUMV( QUAD_4 ):  numCorners = 4;break;
231*c4cbe8f1SToby Isaac         case CGNS_ENUMV( TETRA_4 ): numCorners = 4;break;
232*c4cbe8f1SToby Isaac         case CGNS_ENUMV( HEXA_8 ):  numCorners = 8;break;
233e1b39ce3SMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType);
234e1b39ce3SMatthew G. Knepley         }
235e1b39ce3SMatthew G. Knepley 
236e1b39ce3SMatthew G. Knepley         /* CGNS uses Fortran-based indexing, sieve uses C-style and numbers cell first then vertices. */
23759dfc837SStefan Lemvig Glimberg         for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) {
238e1b39ce3SMatthew G. Knepley           for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) {
239e1b39ce3SMatthew G. Knepley             cone[v_loc] = elements[v]+numCells-1;
240e1b39ce3SMatthew G. Knepley           }
241e1b39ce3SMatthew G. Knepley           /* Tetrahedra are inverted */
242*c4cbe8f1SToby Isaac           if (cellType == CGNS_ENUMV( TETRA_4 )) {
243e1b39ce3SMatthew G. Knepley             PetscInt tmp = cone[0];
244e1b39ce3SMatthew G. Knepley             cone[0] = cone[1];
245e1b39ce3SMatthew G. Knepley             cone[1] = tmp;
246e1b39ce3SMatthew G. Knepley           }
247f8716870SMatthew G. Knepley           /* Hexahedra are inverted, and they give the top first */
248*c4cbe8f1SToby Isaac           if (cellType == CGNS_ENUMV( HEXA_8 )) {
249f8716870SMatthew G. Knepley             PetscInt tmp = cone[5];
250f8716870SMatthew G. Knepley             cone[5] = cone[7];
251f8716870SMatthew G. Knepley             cone[7] = tmp;
252e1b39ce3SMatthew G. Knepley           }
253e1b39ce3SMatthew G. Knepley           ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr);
254c58f1c22SToby Isaac           ierr = DMSetLabelValue(*dm, "zone", c, z);CHKERRQ(ierr);
255e1b39ce3SMatthew G. Knepley         }
256e1b39ce3SMatthew G. Knepley       }
257e1b39ce3SMatthew G. Knepley       ierr = PetscFree2(elements,cone);CHKERRQ(ierr);
258e1b39ce3SMatthew G. Knepley     }
259e1b39ce3SMatthew G. Knepley   }
260e1b39ce3SMatthew G. Knepley   ierr = DMPlexSymmetrize(*dm);CHKERRQ(ierr);
261e1b39ce3SMatthew G. Knepley   ierr = DMPlexStratify(*dm);CHKERRQ(ierr);
262e1b39ce3SMatthew G. Knepley   if (interpolate) {
263e56d480eSMatthew G. Knepley     DM idm = NULL;
264e1b39ce3SMatthew G. Knepley 
265e1b39ce3SMatthew G. Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
266e1b39ce3SMatthew G. Knepley     /* Maintain zone label */
267e1b39ce3SMatthew G. Knepley     {
268e1b39ce3SMatthew G. Knepley       DMLabel label;
269e1b39ce3SMatthew G. Knepley 
270c58f1c22SToby Isaac       ierr = DMRemoveLabel(*dm, "zone", &label);CHKERRQ(ierr);
271c58f1c22SToby Isaac       if (label) {ierr = DMAddLabel(idm, label);CHKERRQ(ierr);}
272e1b39ce3SMatthew G. Knepley     }
273e1b39ce3SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
274e1b39ce3SMatthew G. Knepley     *dm  = idm;
275e1b39ce3SMatthew G. Knepley   }
276e1b39ce3SMatthew G. Knepley 
277e1b39ce3SMatthew G. Knepley   /* Read coordinates */
27865f90189SJed Brown   ierr = DMGetCoordinateSection(*dm, &coordSection);CHKERRQ(ierr);
279e1b39ce3SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
280e1b39ce3SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
281e1b39ce3SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, numCells, numCells + numVertices);CHKERRQ(ierr);
282e1b39ce3SMatthew G. Knepley   for (v = numCells; v < numCells+numVertices; ++v) {
283e1b39ce3SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
284e1b39ce3SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
285e1b39ce3SMatthew G. Knepley   }
286e1b39ce3SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
287e1b39ce3SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
2888b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
289e1b39ce3SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
290e1b39ce3SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
2912eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
292e1b39ce3SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
293e1b39ce3SMatthew G. Knepley   if (!rank) {
294e1b39ce3SMatthew G. Knepley     PetscInt off = 0;
295e1b39ce3SMatthew G. Knepley     float   *x[3];
29659dfc837SStefan Lemvig Glimberg     int      z, d;
297e1b39ce3SMatthew G. Knepley 
298dcca6d9dSJed Brown     ierr = PetscMalloc3(numVertices,&x[0],numVertices,&x[1],numVertices,&x[2]);CHKERRQ(ierr);
29959dfc837SStefan Lemvig Glimberg     for (z = 1; z <= nzones; ++z) {
300*c4cbe8f1SToby Isaac       CGNS_ENUMT( DataType_t ) datatype;
301e1b39ce3SMatthew G. Knepley       cgsize_t                 sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */
302e1b39ce3SMatthew G. Knepley       cgsize_t                 range_min[3] = {1, 1, 1};
303e1b39ce3SMatthew G. Knepley       cgsize_t                 range_max[3] = {1, 1, 1};
304e1b39ce3SMatthew G. Knepley       int                      ngrids, ncoords;
305e1b39ce3SMatthew G. Knepley 
306e1b39ce3SMatthew G. Knepley 
307e1b39ce3SMatthew G. Knepley       ierr = cg_zone_read(cgid, 1, z, buffer, sizes);CHKERRQ(ierr);
308e1b39ce3SMatthew G. Knepley       range_max[0] = sizes[0];
309e1b39ce3SMatthew G. Knepley       ierr = cg_ngrids(cgid, 1, z, &ngrids);CHKERRQ(ierr);
310e1b39ce3SMatthew G. Knepley       if (ngrids > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single grid, not %d\n",ngrids);
311e1b39ce3SMatthew G. Knepley       ierr = cg_ncoords(cgid, 1, z, &ncoords);CHKERRQ(ierr);
312e1b39ce3SMatthew G. Knepley       if (ncoords != dim) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a coordinate array for each dimension, not %d\n",ncoords);
313e1b39ce3SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
31459dfc837SStefan Lemvig Glimberg         ierr = cg_coord_info(cgid, 1, z, 1+d, &datatype, buffer);CHKERRQ(ierr);
315*c4cbe8f1SToby Isaac         ierr = cg_coord_read(cgid, 1, z, buffer, CGNS_ENUMV( RealSingle ), range_min, range_max, x[d]);CHKERRQ(ierr);
316e1b39ce3SMatthew G. Knepley       }
317e1b39ce3SMatthew G. Knepley       if (dim > 0) {
318e1b39ce3SMatthew G. Knepley         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+0] = x[0][v];
319e1b39ce3SMatthew G. Knepley       }
320e1b39ce3SMatthew G. Knepley       if (dim > 1) {
321e1b39ce3SMatthew G. Knepley         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+1] = x[1][v];
322e1b39ce3SMatthew G. Knepley       }
323e1b39ce3SMatthew G. Knepley       if (dim > 2) {
324e1b39ce3SMatthew G. Knepley         for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+2] = x[2][v];
325e1b39ce3SMatthew G. Knepley       }
326e1b39ce3SMatthew G. Knepley       off += sizes[0];
327e1b39ce3SMatthew G. Knepley     }
328e1b39ce3SMatthew G. Knepley     ierr = PetscFree3(x[0],x[1],x[2]);CHKERRQ(ierr);
329e1b39ce3SMatthew G. Knepley   }
330e1b39ce3SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
331e1b39ce3SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(*dm, coordinates);CHKERRQ(ierr);
332e1b39ce3SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
333f8716870SMatthew G. Knepley   /* Read boundary conditions */
334f8716870SMatthew G. Knepley   if (!rank) {
335f8716870SMatthew G. Knepley     DMLabel                       label;
336*c4cbe8f1SToby Isaac     CGNS_ENUMT( BCType_t )        bctype;
337*c4cbe8f1SToby Isaac     CGNS_ENUMT( DataType_t )      datatype;
338*c4cbe8f1SToby Isaac     CGNS_ENUMT( PointSetType_t )  pointtype;
339f8716870SMatthew G. Knepley     cgsize_t                     *points;
340f8716870SMatthew G. Knepley     PetscReal                    *normals;
341f8716870SMatthew G. Knepley     int                           normal[3];
342f8716870SMatthew G. Knepley     char                         *bcname = buffer;
343f8716870SMatthew G. Knepley     cgsize_t                      npoints, nnormals;
344f8716870SMatthew G. Knepley     int                           z, nbc, bc, c, ndatasets;
345f8716870SMatthew G. Knepley 
346f8716870SMatthew G. Knepley     for (z = 1; z <= nzones; ++z) {
347f8716870SMatthew G. Knepley       ierr = cg_nbocos(cgid, 1, z, &nbc);CHKERRQ(ierr);
348f8716870SMatthew G. Knepley       for (bc = 1; bc <= nbc; ++bc) {
349f8716870SMatthew G. Knepley         ierr = cg_boco_info(cgid, 1, z, bc, bcname, &bctype, &pointtype, &npoints, normal, &nnormals, &datatype, &ndatasets);CHKERRQ(ierr);
350c58f1c22SToby Isaac         ierr = DMCreateLabel(*dm, bcname);CHKERRQ(ierr);
351c58f1c22SToby Isaac         ierr = DMGetLabel(*dm, bcname, &label);CHKERRQ(ierr);
352f8716870SMatthew G. Knepley         ierr = PetscMalloc2(npoints, &points, nnormals, &normals);CHKERRQ(ierr);
353f8716870SMatthew G. Knepley         ierr = cg_boco_read(cgid, 1, z, bc, points, (void *) normals);CHKERRQ(ierr);
354*c4cbe8f1SToby Isaac         if (pointtype == CGNS_ENUMV( ElementRange )) {
355f8716870SMatthew G. Knepley           /* Range of cells: assuming half-open interval since the documentation sucks */
356f8716870SMatthew G. Knepley           for (c = points[0]; c < points[1]; ++c) {
357f8716870SMatthew G. Knepley             ierr = DMLabelSetValue(label, c - cellStart[z-1], 1);CHKERRQ(ierr);
358f8716870SMatthew G. Knepley           }
359*c4cbe8f1SToby Isaac         } else if (pointtype == CGNS_ENUMV( ElementList )) {
360f8716870SMatthew G. Knepley           /* List of cells */
361f8716870SMatthew G. Knepley           for (c = 0; c < npoints; ++c) {
362f8716870SMatthew G. Knepley             ierr = DMLabelSetValue(label, points[c] - cellStart[z-1], 1);CHKERRQ(ierr);
363f8716870SMatthew G. Knepley           }
364*c4cbe8f1SToby Isaac         } else if (pointtype == CGNS_ENUMV( PointRange )) {
365*c4cbe8f1SToby Isaac           CGNS_ENUMT( GridLocation_t ) gridloc;
366f8716870SMatthew G. Knepley 
367f8716870SMatthew G. Knepley           /* List of points: Oh please, someone get the CGNS developers away from a computer. This is unconscionable. */
368f8716870SMatthew G. Knepley           ierr = cg_goto(cgid, 1, "Zone_t", z, "BC_t", bc, "end");CHKERRQ(ierr);
369f8716870SMatthew G. Knepley           ierr = cg_gridlocation_read(&gridloc);CHKERRQ(ierr);
370f8716870SMatthew G. Knepley           /* Range of points: assuming half-open interval since the documentation sucks */
371f8716870SMatthew G. Knepley           for (c = points[0]; c < points[1]; ++c) {
372*c4cbe8f1SToby Isaac             if (gridloc == CGNS_ENUMV( Vertex )) {ierr = DMLabelSetValue(label, c - vertStart[z-1], 1);CHKERRQ(ierr);}
373f8716870SMatthew G. Knepley             else                                 {ierr = DMLabelSetValue(label, c - cellStart[z-1], 1);CHKERRQ(ierr);}
374f8716870SMatthew G. Knepley           }
375*c4cbe8f1SToby Isaac         } else if (pointtype == CGNS_ENUMV( PointList )) {
376*c4cbe8f1SToby Isaac           CGNS_ENUMT( GridLocation_t ) gridloc;
377f8716870SMatthew G. Knepley 
378f8716870SMatthew G. Knepley           /* List of points: Oh please, someone get the CGNS developers away from a computer. This is unconscionable. */
379f8716870SMatthew G. Knepley           ierr = cg_goto(cgid, 1, "Zone_t", z, "BC_t", bc, "end");
380f8716870SMatthew G. Knepley           ierr = cg_gridlocation_read(&gridloc);
381f8716870SMatthew G. Knepley           for (c = 0; c < npoints; ++c) {
382*c4cbe8f1SToby Isaac             if (gridloc == CGNS_ENUMV( Vertex )) {ierr = DMLabelSetValue(label, points[c] - vertStart[z-1], 1);CHKERRQ(ierr);}
383f8716870SMatthew G. Knepley             else                   {ierr = DMLabelSetValue(label, points[c] - cellStart[z-1], 1);CHKERRQ(ierr);}
384f8716870SMatthew G. Knepley           }
385f8716870SMatthew G. Knepley         } else SETERRQ1(comm, PETSC_ERR_SUP, "Unsupported point set type %d", (int) pointtype);
386f8716870SMatthew G. Knepley         ierr = PetscFree2(points, normals);CHKERRQ(ierr);
387f8716870SMatthew G. Knepley       }
388f8716870SMatthew G. Knepley     }
389f8716870SMatthew G. Knepley     ierr = PetscFree2(cellStart, vertStart);CHKERRQ(ierr);
390f8716870SMatthew G. Knepley   }
391e1b39ce3SMatthew G. Knepley #else
392e1b39ce3SMatthew G. Knepley   SETERRQ(comm, PETSC_ERR_SUP, "This method requires CGNS support. Reconfigure using --with-cgns-dir");
393e1b39ce3SMatthew G. Knepley #endif
394e1b39ce3SMatthew G. Knepley   PetscFunctionReturn(0);
395e1b39ce3SMatthew G. Knepley }
396