int main(int argc, char* argv[]) { int err; int comm_size; int comm_rank; MPI_Info info; int fn; int B; char basename[100]; int cell_dim = 3; int phys_dim = 3; err = MPI_Init(&argc,&argv); if(err!=MPI_SUCCESS) cgp_doError; err = MPI_Comm_size(MPI_COMM_WORLD, &comm_size); if(err!=MPI_SUCCESS) cgp_doError; err = MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank); if(err!=MPI_SUCCESS) cgp_doError; err = MPI_Info_create(&(info)); if(err!=MPI_SUCCESS) cgp_doError; if (cgp_open("test_base.cgns", CG_MODE_WRITE, &fn) || cg_base_write(fn, "Base 1", cell_dim, phys_dim, &B) || cgp_close(fn)) cgp_error_exit(); if (cgp_open("test_base.cgns", CG_MODE_READ, &fn) || cg_base_read(fn, B, basename, &cell_dim, &phys_dim) || cgp_close(fn)) cgp_error_exit(); err = MPI_Finalize(); if(err!=MPI_SUCCESS) cgp_doError; return err; }
Base_t file::readBase( int ibase, string& name, int& celldim, int& physdim ) const { cgnsstring cname; int ier = cg_base_read( _fileindex, ++ibase, cname, &celldim, &physdim ); check_error( "file::readBase", "cg_base_read", ier ); name = cname; return Base_t( *this, ibase, physdim ); }
int main (int argc, char *argv[]) { int i, n, ib, nb, nz, nv, celldim, phydim; int nn, type, *elems = 0, idata[5]; cgsize_t ne; char *p, basename[33], title[65]; float value, *var; SOLUTION *sol; FILE *fp; if (argc < 2) print_usage (usgmsg, NULL); ib = 0; basename[0] = 0; while ((n = getargs (argc, argv, options)) > 0) { switch (n) { case 'a': ascii = 1; break; case 'b': ib = atoi (argarg); break; case 'B': strncpy (basename, argarg, 32); basename[32] = 0; break; case 'w': weighting = 1; break; case 'S': usesol = atoi (argarg); break; } } if (argind > argc - 2) print_usage (usgmsg, "CGNSfile and/or Tecplotfile not given"); if (!file_exists (argv[argind])) FATAL (NULL, "CGNSfile does not exist or is not a file"); /* open CGNS file */ printf ("reading CGNS file from %s\n", argv[argind]); nb = open_cgns (argv[argind], 1); if (!nb) FATAL (NULL, "no bases found in CGNS file"); if (*basename && 0 == (ib = find_base (basename))) FATAL (NULL, "specified base not found"); if (ib > nb) FATAL (NULL, "base index out of range"); cgnsbase = ib ? ib : 1; if (cg_base_read (cgnsfn, cgnsbase, basename, &celldim, &phydim)) FATAL (NULL, NULL); if (celldim != 3 || phydim != 3) FATAL (NULL, "cell and physical dimension must be 3"); printf (" using base %d - %s\n", cgnsbase, basename); if (NULL == (p = strrchr (argv[argind], '/')) && NULL == (p = strrchr (argv[argind], '\\'))) strncpy (title, argv[argind], sizeof(title)); else strncpy (title, ++p, sizeof(title)); title[sizeof(title)-1] = 0; if ((p = strrchr (title, '.')) != NULL) *p = 0; read_zones (); if (!nZones) FATAL (NULL, "no zones in the CGNS file"); /* verify dimensions fit in an integer */ for (nz = 0; nz < nZones; nz++) { if (Zones[nz].nverts > CG_MAX_INT32) FATAL(NULL, "zone size too large to write with integers"); if (Zones[nz].type == CGNS_ENUMV(Unstructured)) { count_elements (nz, &ne, &type); if (ne > CG_MAX_INT32) FATAL(NULL, "too many elements to write with integers"); } } nv = 3 + check_solution (); /* open Tecplot file */ printf ("writing %s Tecplot data to <%s>\n", ascii ? "ASCII" : "binary", argv[++argind]); if (NULL == (fp = fopen (argv[argind], ascii ? "w+" : "w+b"))) FATAL (NULL, "couldn't open Tecplot output file"); /* write file header */ if (ascii) fprintf (fp, "TITLE = \"%s\"\n", title); else { fwrite ("#!TDV75 ", 1, 8, fp); i = 1; write_ints (fp, 1, &i); write_string (fp, title); } /* write variables */ if (ascii) { fprintf (fp, "VARIABLES = \"X\", \"Y\", \"Z\""); if (usesol) { sol = Zones->sols; for (n = 0; n < sol->nflds; n++) fprintf (fp, ",\n\"%s\"", sol->flds[n].name); } } else { write_ints (fp, 1, &nv); write_string (fp, "X"); write_string (fp, "Y"); write_string (fp, "Z"); if (usesol) { sol = Zones->sols; for (n = 0; n < sol->nflds; n++) write_string (fp, sol->flds[n].name); } } /* write zones */ if (!ascii) { for (nz = 0; nz < nZones; nz++) { if (Zones[nz].type == CGNS_ENUMV(Structured)) { idata[0] = 0; /* BLOCK */ idata[1] = -1; /* color not specified */ idata[2] = (int)Zones[nz].dim[0]; idata[3] = (int)Zones[nz].dim[1]; idata[4] = (int)Zones[nz].dim[2]; } else { count_elements (nz, &ne, &type); idata[0] = 2; /* FEBLOCK */ idata[1] = -1; /* color not specified */ idata[2] = (int)Zones[nz].dim[0]; idata[3] = (int)ne; idata[4] = type; } value = 299.0; write_floats (fp, 1, &value); write_string (fp, Zones[nz].name); write_ints (fp, 5, idata); } value = 357.0; write_floats (fp, 1, &value); } for (nz = 0; nz < nZones; nz++) { printf (" zone %d...", nz+1); fflush (stdout); read_zone_grid (nz+1); ne = 0; type = 2; nn = (int)Zones[nz].nverts; var = (float *) malloc (nn * sizeof(float)); if (NULL == var) FATAL (NULL, "malloc failed for temp float array"); if (Zones[nz].type == CGNS_ENUMV(Unstructured)) elems = volume_elements (nz, &ne, &type); if (ascii) { if (Zones[nz].type == CGNS_ENUMV(Structured)) fprintf (fp, "\nZONE T=\"%s\", I=%d, J=%d, K=%d, F=BLOCK\n", Zones[nz].name, (int)Zones[nz].dim[0], (int)Zones[nz].dim[1], (int)Zones[nz].dim[2]); else fprintf (fp, "\nZONE T=\"%s\", N=%d, E=%d, F=FEBLOCK, ET=%s\n", Zones[nz].name, nn, (int)ne, type == 2 ? "TETRAHEDRON" : "BRICK"); } else { value = 299.0; write_floats (fp, 1, &value); i = 0; write_ints (fp, 1, &i); i = 1; for (n = 0; n < nv; n++) write_ints (fp, 1, &i); } for (n = 0; n < nn; n++) var[n] = (float)Zones[nz].verts[n].x; write_floats (fp, nn, var); for (n = 0; n < nn; n++) var[n] = (float)Zones[nz].verts[n].y; write_floats (fp, nn, var); for (n = 0; n < nn; n++) var[n] = (float)Zones[nz].verts[n].z; write_floats (fp, nn, var); if (usesol) { read_solution_field (nz+1, usesol, 0); sol = &Zones[nz].sols[usesol-1]; if (sol->location != CGNS_ENUMV(Vertex)) cell_vertex_solution (nz+1, usesol, weighting); for (nv = 0; nv < sol->nflds; nv++) { for (n = 0; n < nn; n++) var[n] = (float)sol->flds[nv].data[n]; write_floats (fp, nn, var); } } free (var); if (Zones[nz].type == CGNS_ENUMV(Unstructured)) { if (!ascii) { i = 0; write_ints (fp, 1, &i); } nn = 1 << type; for (i = 0, n = 0; n < ne; n++, i += nn) write_ints (fp, nn, &elems[i]); free (elems); } puts ("done"); } fclose (fp); cg_close (cgnsfn); return 0; }
unsigned short GetnDim(string val_mesh_filename, unsigned short val_format) { string text_line, Marker_Tag; ifstream mesh_file; short nDim = 3; unsigned short iLine, nLine = 10; char cstr[200]; string::size_type position; /*--- Open grid file ---*/ strcpy (cstr, val_mesh_filename.c_str()); mesh_file.open(cstr, ios::in); switch (val_format) { case SU2: /*--- Read SU2 mesh file ---*/ for (iLine = 0; iLine < nLine ; iLine++) { getline (mesh_file, text_line); /*--- Search for the "NDIM" keyword to see if there are multiple Zones ---*/ position = text_line.find ("NDIME=",0); if (position != string::npos) { text_line.erase (0,6); nDim = atoi(text_line.c_str()); } } break; case CGNS: #ifdef HAVE_CGNS /*--- Local variables which are needed when calling the CGNS mid-level API. ---*/ int fn, nbases = 0, nzones = 0, file_type; int cell_dim = 0, phys_dim = 0; char basename[CGNS_STRING_SIZE]; /*--- Check whether the supplied file is truly a CGNS file. ---*/ if ( cg_is_cgns(val_mesh_filename.c_str(), &file_type) != CG_OK ) { printf( "\n\n !!! Error !!!\n" ); printf( " %s is not a CGNS file.\n", val_mesh_filename.c_str()); printf( " Now exiting...\n\n"); exit(EXIT_FAILURE); } /*--- Open the CGNS file for reading. The value of fn returned is the specific index number for this file and will be repeatedly used in the function calls. ---*/ if (cg_open(val_mesh_filename.c_str(), CG_MODE_READ, &fn)) cg_error_exit(); /*--- Get the number of databases. This is the highest node in the CGNS heirarchy. ---*/ if (cg_nbases(fn, &nbases)) cg_error_exit(); /*--- Check if there is more than one database. Throw an error if there is because this reader can currently only handle one database. ---*/ if ( nbases > 1 ) { printf("\n\n !!! Error !!!\n" ); printf("CGNS reader currently incapable of handling more than 1 database."); printf("Now exiting...\n\n"); exit(EXIT_FAILURE); } /*--- Read the databases. Note that the indexing starts at 1. ---*/ for ( int i = 1; i <= nbases; i++ ) { if (cg_base_read(fn, i, basename, &cell_dim, &phys_dim)) cg_error_exit(); /*--- Get the number of zones for this base. ---*/ if (cg_nzones(fn, i, &nzones)) cg_error_exit(); } /*--- Set the problem dimension as read from the CGNS file ---*/ nDim = cell_dim; #endif break; } mesh_file.close(); return (unsigned short) nDim; }
/*---------- PostProcesssing -------------------------------------- * Post-Analysis of CFD Data * Developed only for CFD-Tutor with single base - single zone 2D * -------------------------------------------------------------*/ int PostProcessing(const char *file) { int visual; int nbases, ncoords, celldim, phydim; char basename[33]; ZONE *z; int i, j, nz; SOLUTION *s; /* Checks for existance of file */ if (!file_exists(file)) FATAL(NULL, "File does not exist"); /* Read CGNS file */ printf("Reading CGNS file from %s\n", file); fflush(stdout); /* Open CGNS File */ nbases = open_cgns(file, 0); if (!nbases) FATAL(NULL, "No bases in CGNS file"); cg_version(cgnsfn, &Version); printf("File version = %lf\n", Version); printf("No of Bases = %d\n", nbases); if (nbases == 1) cgnsbase = 1; else { printf("Give base No to Post-Processed : "); scanf("%d", &cgnsbase); if (cgnsbase < 1 || cgnsbase > nbases) FATAL(NULL, "Invailed base index"); } if (cg_base_read(cgnsfn, cgnsbase, basename, &celldim, &phydim) || cg_nzones(cgnsfn, cgnsbase, &nZones)) FATAL(NULL, NULL); if (celldim != 2 || phydim != 2) FATAL(NULL, "Not A 2D Grid"); if (nZones > 1) FATAL(NULL, "Not A Single Zone"); printf("Base Number = %d\n", cgnsbase); printf("Base Name = %s\n", basename); printf("Cell Dimension = %d\n", celldim); printf("Physical Dimension = %d\n", phydim); read_cgns(); printf("No of Zones = %d\n", nZones); /* Print Zone Information */ for (z = Zones, nz = 1; nz <= nZones; nz++, z++) { printf("Zone No = %d\n", z->id); printf("Zone Name = %s\n", z->name); if (cg_ncoords(cgnsfn, cgnsbase, nz, &ncoords)) FATAL("Post-Processing ncoords", NULL); if (ncoords != 2) FATAL(NULL, "No 3D Support Now"); print_ZoneType(z->type); switch (z->type) { case 2: /* For Structured Grid */ printf("Dimensions = %d x %d x %d\n", z->dim[0], z->dim[1], z->dim[2]); break; case 3: /* For Unstructured Grid */ printf("No of Nodes = %d\n", z->dim[0]); printf("No of Cells = %d\n", z->dim[1]); break; default: /* Unknown Type Grids */ FATAL(NULL, "Unknown Grid Type"); } } /* Print Available Solution Fields */ for (z = Zones, nz = 1; nz <= nZones; nz++, z++) { if (z->nsols) { printf("No of Solutions Nodes = %d\n", z->nsols); for (s = z->sols, i = 1; i <= z->nsols; i++, s++) { printf("Solution Node = %d\n", i); if (s->nflds == 0) FATAL(NULL, "No Solution Fields: Exiting"); printf("\tNo of Fields = %d\n", s->nflds); for (j = 0; j < s->nflds; j++) { printf("\tField = %s\n", s->flds[j].name); } } } else FATAL(NULL, "No Solution Node Available: Exiting"); } /* Initialize Data Structure Only Once */ for (z = Zones, nz = 1; nz <= nZones; nz++, z++) InitializeGrid_2D(z); /* Do Post-Processing Operation Below */ /*****************/ /* Post Analysis Function Calls */ for (z = Zones, nz = 1; nz <= nZones; nz++, z++) { InitializeSolutionCGNS_2D(z); InitializeSolution_2D(); GetSolutionList_2D(); /* Visual Mode Option */ printf("Goto Visual Mode (0/1):"); scanf("%d", &visual); if (visual == 1) Graphics_2D(); else { GetFluidProperties(); GetReferenceQuantities(); PostAnalysis_2D(); } } /*****************/ /* Do Post-Processing Operation Above */ /* Close cgns Interface */ if (cg_close(cgnsfn)) FATAL(NULL, NULL); return 0; }
/*@ DMPlexCreateCGNS - Create a DMPlex mesh from a CGNS file ID. Collective on comm Input Parameters: + comm - The MPI communicator . cgid - The CG id associated with a file and obtained using cg_open - interpolate - Create faces and edges in the mesh Output Parameter: . dm - The DM object representing the mesh Note: http://www.grc.nasa.gov/WWW/cgns/CGNS_docs_current/index.html Level: beginner .keywords: mesh,CGNS .seealso: DMPlexCreate(), DMPlexCreateExodus() @*/ PetscErrorCode DMPlexCreateCGNS(MPI_Comm comm, PetscInt cgid, PetscBool interpolate, DM *dm) { #if defined(PETSC_HAVE_CGNS) PetscMPIInt num_proc, rank; PetscSection coordSection; Vec coordinates; PetscScalar *coords; PetscInt *cellStart, *vertStart; PetscInt coordSize, v; PetscErrorCode ierr; /* Read from file */ char basename[CGIO_MAX_NAME_LENGTH+1]; char buffer[CGIO_MAX_NAME_LENGTH+1]; int dim = 0, physDim = 0, numVertices = 0, numCells = 0; int nzones = 0; #endif PetscFunctionBegin; #if defined(PETSC_HAVE_CGNS) ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); ierr = MPI_Comm_size(comm, &num_proc);CHKERRQ(ierr); ierr = DMCreate(comm, dm);CHKERRQ(ierr); ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr); /* Open CGNS II file and read basic informations on rank 0, then broadcast to all processors */ if (!rank) { int nbases, z; ierr = cg_nbases(cgid, &nbases);CHKERRQ(ierr); if (nbases > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single base, not %d\n",nbases); ierr = cg_base_read(cgid, 1, basename, &dim, &physDim);CHKERRQ(ierr); ierr = cg_nzones(cgid, 1, &nzones);CHKERRQ(ierr); ierr = PetscCalloc2(nzones+1, &cellStart, nzones+1, &vertStart);CHKERRQ(ierr); for (z = 1; z <= nzones; ++z) { cgsize_t sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */ ierr = cg_zone_read(cgid, 1, z, buffer, sizes);CHKERRQ(ierr); numVertices += sizes[0]; numCells += sizes[1]; cellStart[z] += sizes[1] + cellStart[z-1]; vertStart[z] += sizes[0] + vertStart[z-1]; } for (z = 1; z <= nzones; ++z) { vertStart[z] += numCells; } } ierr = MPI_Bcast(basename, CGIO_MAX_NAME_LENGTH+1, MPI_CHAR, 0, comm);CHKERRQ(ierr); ierr = MPI_Bcast(&dim, 1, MPI_INT, 0, comm);CHKERRQ(ierr); ierr = MPI_Bcast(&nzones, 1, MPI_INT, 0, comm);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) *dm, basename);CHKERRQ(ierr); ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr); ierr = DMPlexSetChart(*dm, 0, numCells+numVertices);CHKERRQ(ierr); /* Read zone information */ if (!rank) { int z, c, c_loc, v, v_loc; /* Read the cell set connectivity table and build mesh topology CGNS standard requires that cells in a zone be numbered sequentially and be pairwise disjoint. */ /* First set sizes */ for (z = 1, c = 0; z <= nzones; ++z) { ZoneType_t zonetype; int nsections; ElementType_t cellType; cgsize_t start, end; int nbndry, parentFlag; PetscInt numCorners; ierr = cg_zone_type(cgid, 1, z, &zonetype);CHKERRQ(ierr); if (zonetype == Structured) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Can only handle Unstructured zones for CGNS"); ierr = cg_nsections(cgid, 1, z, &nsections);CHKERRQ(ierr); if (nsections > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single section, not %d\n",nsections); ierr = cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);CHKERRQ(ierr); /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */ if (cellType == MIXED) { cgsize_t elementDataSize, *elements; PetscInt off; ierr = cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);CHKERRQ(ierr); ierr = PetscMalloc1(elementDataSize, &elements);CHKERRQ(ierr); ierr = cg_elements_read(cgid, 1, z, 1, elements, NULL);CHKERRQ(ierr); for (c_loc = start, off = 0; c_loc <= end; ++c_loc, ++c) { switch (elements[off]) { case TRI_3: numCorners = 3;break; case QUAD_4: numCorners = 4;break; case TETRA_4: numCorners = 4;break; case HEXA_8: numCorners = 8;break; default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[off]); } ierr = DMPlexSetConeSize(*dm, c, numCorners);CHKERRQ(ierr); off += numCorners+1; } ierr = PetscFree(elements);CHKERRQ(ierr); } else { switch (cellType) { case TRI_3: numCorners = 3;break; case QUAD_4: numCorners = 4;break; case TETRA_4: numCorners = 4;break; case HEXA_8: numCorners = 8;break; default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType); } for (c_loc = start; c_loc <= end; ++c_loc, ++c) { ierr = DMPlexSetConeSize(*dm, c, numCorners);CHKERRQ(ierr); } } } ierr = DMSetUp(*dm);CHKERRQ(ierr); for (z = 1, c = 0; z <= nzones; ++z) { ElementType_t cellType; cgsize_t *elements, elementDataSize, start, end; int nbndry, parentFlag; PetscInt *cone, numc, numCorners, maxCorners = 27; ierr = cg_section_read(cgid, 1, z, 1, buffer, &cellType, &start, &end, &nbndry, &parentFlag);CHKERRQ(ierr); numc = end - start; /* This alone is reason enough to bludgeon every single CGNDS developer, this must be what they describe as the "idiocy of crowds" */ ierr = cg_ElementDataSize(cgid, 1, z, 1, &elementDataSize);CHKERRQ(ierr); ierr = PetscMalloc2(elementDataSize,&elements,maxCorners,&cone);CHKERRQ(ierr); ierr = cg_elements_read(cgid, 1, z, 1, elements, NULL);CHKERRQ(ierr); if (cellType == MIXED) { /* CGNS uses Fortran-based indexing, sieve uses C-style and numbers cell first then vertices. */ for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) { switch (elements[v]) { case TRI_3: numCorners = 3;break; case QUAD_4: numCorners = 4;break; case TETRA_4: numCorners = 4;break; case HEXA_8: numCorners = 8;break; default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) elements[v]); } ++v; for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) { cone[v_loc] = elements[v]+numCells-1; } /* Tetrahedra are inverted */ if (elements[v] == TETRA_4) { PetscInt tmp = cone[0]; cone[0] = cone[1]; cone[1] = tmp; } /* Hexahedra are inverted */ if (elements[v] == HEXA_8) { PetscInt tmp = cone[5]; cone[5] = cone[7]; cone[7] = tmp; } ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr); ierr = DMPlexSetLabelValue(*dm, "zone", c, z);CHKERRQ(ierr); } } else { switch (cellType) { case TRI_3: numCorners = 3;break; case QUAD_4: numCorners = 4;break; case TETRA_4: numCorners = 4;break; case HEXA_8: numCorners = 8;break; default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid cell type %d", (int) cellType); } /* CGNS uses Fortran-based indexing, sieve uses C-style and numbers cell first then vertices. */ for (c_loc = 0, v = 0; c_loc <= numc; ++c_loc, ++c) { for (v_loc = 0; v_loc < numCorners; ++v_loc, ++v) { cone[v_loc] = elements[v]+numCells-1; } /* Tetrahedra are inverted */ if (cellType == TETRA_4) { PetscInt tmp = cone[0]; cone[0] = cone[1]; cone[1] = tmp; } /* Hexahedra are inverted, and they give the top first */ if (cellType == HEXA_8) { PetscInt tmp = cone[5]; cone[5] = cone[7]; cone[7] = tmp; } ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr); ierr = DMPlexSetLabelValue(*dm, "zone", c, z);CHKERRQ(ierr); } } ierr = PetscFree2(elements,cone);CHKERRQ(ierr); } } ierr = DMPlexSymmetrize(*dm);CHKERRQ(ierr); ierr = DMPlexStratify(*dm);CHKERRQ(ierr); if (interpolate) { DM idm = NULL; ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr); /* Maintain zone label */ { DMLabel label; ierr = DMPlexRemoveLabel(*dm, "zone", &label);CHKERRQ(ierr); if (label) {ierr = DMPlexAddLabel(idm, label);CHKERRQ(ierr);} } ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = idm; } /* Read coordinates */ ierr = DMGetCoordinateSection(*dm, &coordSection);CHKERRQ(ierr); ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr); ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr); ierr = PetscSectionSetChart(coordSection, numCells, numCells + numVertices);CHKERRQ(ierr); for (v = numCells; v < numCells+numVertices; ++v) { ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr); ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr); } ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr); ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr); ierr = VecCreate(comm, &coordinates);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr); ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr); ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr); ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); if (!rank) { PetscInt off = 0; float *x[3]; int z, d; ierr = PetscMalloc3(numVertices,&x[0],numVertices,&x[1],numVertices,&x[2]);CHKERRQ(ierr); for (z = 1; z <= nzones; ++z) { DataType_t datatype; cgsize_t sizes[3]; /* Number of vertices, number of cells, number of boundary vertices */ cgsize_t range_min[3] = {1, 1, 1}; cgsize_t range_max[3] = {1, 1, 1}; int ngrids, ncoords; ierr = cg_zone_read(cgid, 1, z, buffer, sizes);CHKERRQ(ierr); range_max[0] = sizes[0]; ierr = cg_ngrids(cgid, 1, z, &ngrids);CHKERRQ(ierr); if (ngrids > 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a single grid, not %d\n",ngrids); ierr = cg_ncoords(cgid, 1, z, &ncoords);CHKERRQ(ierr); if (ncoords != dim) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CGNS file must have a coordinate array for each dimension, not %d\n",ncoords); for (d = 0; d < dim; ++d) { ierr = cg_coord_info(cgid, 1, z, 1+d, &datatype, buffer);CHKERRQ(ierr); ierr = cg_coord_read(cgid, 1, z, buffer, RealSingle, range_min, range_max, x[d]);CHKERRQ(ierr); } if (dim > 0) { for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+0] = x[0][v]; } if (dim > 1) { for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+1] = x[1][v]; } if (dim > 2) { for (v = 0; v < sizes[0]; ++v) coords[(v+off)*dim+2] = x[2][v]; } off += sizes[0]; } ierr = PetscFree3(x[0],x[1],x[2]);CHKERRQ(ierr); } ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); ierr = DMSetCoordinatesLocal(*dm, coordinates);CHKERRQ(ierr); ierr = VecDestroy(&coordinates);CHKERRQ(ierr); /* Read boundary conditions */ if (!rank) { DMLabel label; BCType_t bctype; DataType_t datatype; PointSetType_t pointtype; cgsize_t *points; PetscReal *normals; int normal[3]; char *bcname = buffer; cgsize_t npoints, nnormals; int z, nbc, bc, c, ndatasets; for (z = 1; z <= nzones; ++z) { ierr = cg_nbocos(cgid, 1, z, &nbc);CHKERRQ(ierr); for (bc = 1; bc <= nbc; ++bc) { ierr = cg_boco_info(cgid, 1, z, bc, bcname, &bctype, &pointtype, &npoints, normal, &nnormals, &datatype, &ndatasets);CHKERRQ(ierr); ierr = DMPlexCreateLabel(*dm, bcname);CHKERRQ(ierr); ierr = DMPlexGetLabel(*dm, bcname, &label);CHKERRQ(ierr); ierr = PetscMalloc2(npoints, &points, nnormals, &normals);CHKERRQ(ierr); ierr = cg_boco_read(cgid, 1, z, bc, points, (void *) normals);CHKERRQ(ierr); if (pointtype == ElementRange) { /* Range of cells: assuming half-open interval since the documentation sucks */ for (c = points[0]; c < points[1]; ++c) { ierr = DMLabelSetValue(label, c - cellStart[z-1], 1);CHKERRQ(ierr); } } else if (pointtype == ElementList) { /* List of cells */ for (c = 0; c < npoints; ++c) { ierr = DMLabelSetValue(label, points[c] - cellStart[z-1], 1);CHKERRQ(ierr); } } else if (pointtype == PointRange) { GridLocation_t gridloc; /* List of points: Oh please, someone get the CGNS developers away from a computer. This is unconscionable. */ ierr = cg_goto(cgid, 1, "Zone_t", z, "BC_t", bc, "end");CHKERRQ(ierr); ierr = cg_gridlocation_read(&gridloc);CHKERRQ(ierr); /* Range of points: assuming half-open interval since the documentation sucks */ for (c = points[0]; c < points[1]; ++c) { if (gridloc == Vertex) {ierr = DMLabelSetValue(label, c - vertStart[z-1], 1);CHKERRQ(ierr);} else {ierr = DMLabelSetValue(label, c - cellStart[z-1], 1);CHKERRQ(ierr);} } } else if (pointtype == PointList) { GridLocation_t gridloc; /* List of points: Oh please, someone get the CGNS developers away from a computer. This is unconscionable. */ ierr = cg_goto(cgid, 1, "Zone_t", z, "BC_t", bc, "end"); ierr = cg_gridlocation_read(&gridloc); for (c = 0; c < npoints; ++c) { if (gridloc == Vertex) {ierr = DMLabelSetValue(label, points[c] - vertStart[z-1], 1);CHKERRQ(ierr);} else {ierr = DMLabelSetValue(label, points[c] - cellStart[z-1], 1);CHKERRQ(ierr);} } } else SETERRQ1(comm, PETSC_ERR_SUP, "Unsupported point set type %d", (int) pointtype); ierr = PetscFree2(points, normals);CHKERRQ(ierr); } } ierr = PetscFree2(cellStart, vertStart);CHKERRQ(ierr); } #else SETERRQ(comm, PETSC_ERR_SUP, "This method requires CGNS support. Reconfigure using --with-cgns-dir"); #endif PetscFunctionReturn(0); }
int main (int argc, char *argv[]) { int n, ib, nb, is, nz, celldim, phydim; cgsize_t imax; char basename[33]; if (argc < 2) print_usage (usgmsg, NULL); ib = 0; basename[0] = 0; while ((n = getargs (argc, argv, options)) > 0) { switch (n) { case 's': mblock = 0; break; case 'p': whole = 0; break; case 'n': use_iblank = 0; break; case 'f': case 'u': format = n; break; case 'd': use_double = 1; break; case 'b': ib = atoi (argarg); break; case 'B': strncpy (basename, argarg, 32); basename[32] = 0; break; case 'g': gamma = atof (argarg); if (gamma <= 1.0) FATAL (NULL, "invalid value for gamma"); break; case 'w': weighting = 1; break; case 'S': usesol = atoi (argarg); break; } } if (argind > argc - 2) print_usage (usgmsg, "CGNSfile and/or XYZfile not given"); if (!file_exists (argv[argind])) FATAL (NULL, "CGNSfile does not exist or is not a file"); /* open CGNS file */ printf ("reading CGNS file from %s\n", argv[argind]); nb = open_cgns (argv[argind], 1); if (!nb) FATAL (NULL, "no bases found in CGNS file"); if (*basename && 0 == (ib = find_base (basename))) FATAL (NULL, "specified base not found"); if (ib > nb) FATAL (NULL, "base index out of range"); cgnsbase = ib ? ib : 1; if (cg_base_read (cgnsfn, cgnsbase, basename, &celldim, &phydim)) FATAL (NULL, NULL); if (celldim != 3 || phydim != 3) FATAL (NULL, "cell and/or physical dimension must be 3"); printf (" using base %d - %s\n", cgnsbase, basename); read_zones (); for (nz = 0; nz < nZones; nz++) { if (Zones[nz].type == CGNS_ENUMV(Structured)) { /* verify we can write out using ints */ for (n = 0; n < 3; n++) { if (Zones[nz].dim[n] > CG_MAX_INT32) FATAL(NULL, "zone dimensions too large for integer"); } if (whole) { if (Zones[nz].nverts > CG_MAX_INT32) FATAL(NULL, "zone too large to write as whole using an integer"); } else { if (Zones[nz].dim[0]*Zones[nz].dim[1] > CG_MAX_INT32) FATAL(NULL, "zone too large to write using an integer"); } nblocks++; } } if (!nblocks) FATAL (NULL, "no structured zones found"); /* read the nodes */ printf ("reading %d zones\n", nblocks); ib = is = 0; imax = 0; for (nz = 0; nz < nZones; nz++) { if (Zones[nz].type == CGNS_ENUMV(Structured)) { printf (" zone %d - %s ... ", nz+1, Zones[nz].name); fflush (stdout); read_zone_grid (nz+1); ib += read_zone_interface (nz+1); is += check_solution (nz); if (imax < Zones[nz].nverts) imax = Zones[nz].nverts; puts ("done"); } } if (!ib) use_iblank = 0; if (use_iblank) { iblank = (int *) malloc ((size_t)imax * sizeof(int)); if (NULL == iblank) FATAL (NULL, "malloc failed for iblank array"); } /* write Plot3d XYZ file */ if (format == 'f') write_xyz_formatted (argv[++argind]); else if (format == 'u') write_xyz_unformatted (argv[++argind]); else write_xyz_binary (argv[++argind]); if (use_iblank) free (iblank); /* write solution file */ if (++argind < argc) { if (is != nblocks) { fprintf (stderr, "solution file is not being written since not\n"); fprintf (stderr, "all the blocks contain a complete solution\n"); cg_close (cgnsfn); exit (1); } for (n = 0; n < 5; n++) { q[n] = (double *) malloc ((size_t)imax * sizeof(double)); if (NULL == q[n]) FATAL (NULL, "malloc failed for solution working array"); } get_reference (); if (format == 'f') write_q_formatted (argv[argind]); else if (format == 'u') write_q_unformatted (argv[argind]); else write_q_binary (argv[argind]); } cg_close (cgnsfn); return 0; }
int main (int argc, char *argv[]) { int n, ib = 0, nb, flags = 0, has_q = 0; BINARYIO *bf; static char basename[33] = "Base"; if (argc < 3) print_usage (usgmsg, NULL); /* get options */ while ((n = getargs (argc, argv, options)) > 0) { switch (n) { case 'f': flags &= ~OPEN_FORTRAN; flags |= OPEN_ASCII; break; case 'u': flags &= ~OPEN_ASCII; flags |= OPEN_FORTRAN; break; case 's': mblock = 0; break; case 'p': whole = 0; break; case 'i': use_iblank = 1; /* fall through */ case 'n': has_iblank = 1; break; case 'd': is_double = 1; break; case 'M': flags &= ~MACH_UNKNOWN; flags |= get_machine (argarg); break; case 'b': ib = atoi (argarg); break; case 'B': strncpy (basename, argarg, 32); basename[32] = 0; break; case 'g': gamma = atof (argarg); if (gamma <= 1.0) FATAL (NULL, "invalid value for gamma"); break; case 'c': convert = 1; break; } } if (argind > argc - 2) print_usage (usgmsg, "XYZfile and/or CGNSfile not given"); /* read Plot3d file */ printf ("reading PLOT3D grid file %s\n", argv[argind]); printf (" as %s-block %s", mblock ? "multi" : "single", flags == OPEN_ASCII ? "ASCII" : (flags == OPEN_FORTRAN ? "FORTRAN unformatted" : "binary")); if (has_iblank) printf (" with iblank array"); putchar ('\n'); if (!file_exists (argv[argind])) FATAL (NULL, "XYZ file does not exist or is not a file"); if (NULL == (bf = bf_open (argv[argind], flags | OPEN_READ))) { fprintf (stderr, "can't open <%s> for reading", argv[argind]); exit (1); } read_xyz (bf); bf_close (bf); if (use_iblank) build_interfaces (); /* read solution file if given */ if (++argind < argc-1) { printf ("\nreading PLOT3D solution file %s\n", argv[argind]); if (!file_exists (argv[argind])) FATAL (NULL, "Solution file does not exist or is not a file"); if (NULL == (bf = bf_open (argv[argind], flags | OPEN_READ))) { fprintf (stderr, "can't open <%s> for reading", argv[argind]); exit (1); } read_q (bf); bf_close (bf); argind++; has_q = 1; } /* open CGNS file */ printf ("\nwriting CGNS file to %s\n", argv[argind]); nb = open_cgns (argv[argind], 0); if (ib) { if (ib > nb) FATAL (NULL, "specified base index out of range"); if (cg_base_read (cgnsfn, ib, basename, &n, &n)) FATAL (NULL, NULL); } if (cg_base_write (cgnsfn, basename, 3, 3, &cgnsbase) || cg_goto (cgnsfn, cgnsbase, "end") || cg_dataclass_write (CGNS_ENUMV(NormalizedByUnknownDimensional))) FATAL (NULL, NULL); printf (" output to base %d - %s\n", cgnsbase, basename); write_zones (); for (n = 1; n <= nZones; n++) { printf ("writing zone %d ... grid", n); fflush (stdout); write_zone_grid (n); write_zone_interface (n); if (has_q) { printf (", solution"); fflush (stdout); write_zone_solution (n, 1); write_solution_field (n, 1, 0); } puts (" done"); } if (has_q) write_reference (); cg_close (cgnsfn); return 0; }