Actual source code: meshlagrit.c
1: #include <petscmesh_formats.hh> /*I "petscmesh.h" I*/
3: #if 0
5: void FlipCellOrientation(pylith::int_array * const cells, const int numCells, const int numCorners, const int meshDim) {
6: if (3 == meshDim && 4 == numCorners) {
7: for(int iCell = 0; iCell < numCells; ++iCell) {
8: const int i1 = iCell*numCorners+1;
9: const int i2 = iCell*numCorners+2;
10: const int tmp = (*cells)[i1];
11: (*cells)[i1] = (*cells)[i2];
12: (*cells)[i2] = tmp;
13: }
14: }
15: }
16: //Obj<PETSC_MESH_TYPE> m = ALE::LaGriT::Builder::readMesh(PETSC_COMM_WORLD, 3, options->baseFilename, options->interpolate, options->debug);'
17: Obj<PETSC_MESH_TYPE> m = new PETSC_MESH_TYPE(comm, options->dim, options->debug);
18: Obj<PETSC_MESH_TYPE::sieve_type> sieve = new PETSC_MESH_TYPE::sieve_type(comm, options->debug);
19: bool flipEndian = false;
20: int dim;
21: pylith::int_array cells;
22: pylith::double_array coordinates;
23: pylith::int_array materialIds;
24: int numCells = 0, numVertices = 0, numCorners = 0;
26: if (!m->commRank()) {
27: if (pylith::meshio::GMVFile::isAscii(options->baseFilename)) {
28: pylith::meshio::GMVFileAscii filein(options->baseFilename);
29: filein.read(&coordinates, &cells, &materialIds, &dim, &dim, &numVertices, &numCells, &numCorners);
30: if (options->interpolate) {
31: FlipCellOrientation(&cells, numCells, numCorners, dim);
32: }
33: } else {
34: pylith::meshio::GMVFileBinary filein(options->baseFilename, flipEndian);
35: filein.read(&coordinates, &cells, &materialIds, &dim, &dim, &numVertices, &numCells, &numCorners);
36: if (!options->interpolate) {
37: FlipCellOrientation(&cells, numCells, numCorners, dim);
38: }
39: } // if/else
40: }
41: ALE::SieveBuilder<PETSC_MESH_TYPE>::buildTopology(sieve, dim, numCells, const_cast<int*>(&cells[0]), numVertices, options->interpolate, numCorners, -1, m->getArrowSection("orientation"));
42: m->setSieve(sieve);
43: m->stratify();
44: ALE::SieveBuilder<PETSC_MESH_TYPE>::buildCoordinates(m, dim, const_cast<double*>(&coordinates[0]));
46: MeshCreate(comm, &mesh);
47: MeshSetMesh(mesh, m);
48: MeshIDBoundary(mesh);
49: #endif
51: namespace ALE {
52: namespace LaGriT {
53: void Builder::readInpFile(MPI_Comm comm, const std::string& filename, const int dim, const int numCorners, int& numElements, int *vertices[], int& numVertices, double *coordinates[]) {
54: PetscViewer viewer;
55: FILE *f;
56: PetscReal *coords;
57: PetscInt *verts;
58: PetscInt commRank;
59: char buf[2048];
62: MPI_Comm_rank(comm, &commRank);
63: if (commRank != 0) return;
64: PetscViewerCreate(PETSC_COMM_SELF, &viewer);
65: PetscViewerSetType(viewer, PETSC_VIEWER_ASCII);
66: PetscViewerFileSetMode(viewer, FILE_MODE_READ);
67: PetscViewerFileSetName(viewer, filename.c_str());
68: PetscViewerASCIIGetPointer(viewer, &f);
69: // Read header
70: if (fgets(buf, 2048, f) == NULL) throw ALE::Exception("File ended prematurely");
71: // Number of vertices
72: const char *x = strtok(buf, " ");
73: numVertices = atoi(x);
74: // Number of elements
75: x = strtok(NULL, " ");
76: numElements = atoi(x);
77: // Element type
78: x = strtok(NULL, " ");
79: // ???
80: x = strtok(NULL, " ");
81: // ???
82: x = strtok(NULL, " ");
83: PetscMalloc(numVertices*dim * sizeof(PetscReal), &coords);
84: for(PetscInt i = 0; i < numVertices; ++i) {
85: if (fgets(buf, 2048, f) == NULL) throw ALE::Exception("File ended prematurely");
86: x = strtok(buf, " ");
87: // Ignore vertex number
88: x = strtok(NULL, " ");
89: for(int c = 0; c < dim; c++) {
90: coords[i*dim+c] = atof(x);
91: x = strtok(NULL, " ");
92: }
93: }
94: *coordinates = coords;
95: PetscMalloc(numElements*numCorners * sizeof(PetscInt), &verts);
96: for(PetscInt i = 0; i < numElements; ++i) {
97: if (fgets(buf, 2048, f) == NULL) throw ALE::Exception("File ended prematurely");
98: x = strtok(buf, " ");
99: // Ignore element number
100: x = strtok(NULL, " ");
101: // Ignore ??? (material)
102: x = strtok(NULL, " ");
103: // Ignore element type
104: x = strtok(NULL, " ");
105: for(int c = 0; c < numCorners; c++) {
106: verts[i*numCorners+c] = atoi(x) - 1;
107: x = strtok(NULL, " ");
108: }
109: }
110: *vertices = verts;
111: PetscViewerDestroy(viewer);
112: };
113: #ifdef PETSC_OPT_SIEVE
114: Obj<Builder::Mesh> Builder::readMesh(MPI_Comm comm, const int dim, const std::string& filename, const bool interpolate = false, const int debug = 0) {
115: throw ALE::Exception("Not implemented for optimized sieves");
116: };
117: void Builder::readFault(Obj<Builder::Mesh> mesh, const std::string& filename) {
118: throw ALE::Exception("Not implemented for optimized sieves");
119: };
120: #else
121: Obj<Builder::Mesh> Builder::readMesh(MPI_Comm comm, const int dim, const std::string& filename, const bool interpolate = false, const int debug = 0) {
122: Obj<Mesh> mesh = new Mesh(comm, dim, debug);
123: Obj<sieve_type> sieve = new sieve_type(comm, debug);
124: int *cells;
125: double *coordinates;
126: int numCells = 0, numVertices = 0, numCorners = dim+1;
128: Builder::readInpFile(comm, filename, dim, numCorners, numCells, &cells, numVertices, &coordinates);
129: ALE::SieveBuilder<Mesh>::buildTopology(sieve, dim, numCells, cells, numVertices, interpolate, numCorners);
130: mesh->setSieve(sieve);
131: mesh->stratify();
132: ALE::SieveBuilder<Mesh>::buildCoordinates(mesh, dim, coordinates);
133: return mesh;
134: };
135: void Builder::readFault(Obj<Builder::Mesh> mesh, const std::string& filename) {
136: const int numCells = mesh->heightStratum(0)->size();
137: PetscViewer viewer;
138: FILE *f;
139: char buf[2048];
140: PetscInt numPsets;
143: if (mesh->commRank() != 0) return;
144: PetscViewerCreate(PETSC_COMM_SELF, &viewer);
145: PetscViewerSetType(viewer, PETSC_VIEWER_ASCII);
146: PetscViewerFileSetMode(viewer, FILE_MODE_READ);
147: PetscViewerFileSetName(viewer, filename.c_str());
148: PetscViewerASCIIGetPointer(viewer, &f);
149: // Read header
150: if (fgets(buf, 2048, f) == NULL) throw ALE::Exception("File ended prematurely");
151: // Check file type
152: const char *x = strtok(buf, " ");
153: std::string fileType("pset");
154: if (fileType != x) throw ALE::Exception("Invalid file type");
155: // Ignore set type
156: x = strtok(NULL, " ");
157: // Number of psets
158: x = strtok(NULL, " ");
159: numPsets = atoi(x);
160: for(PetscInt p = 0; p < numPsets; ++p) {
161: if (fgets(buf, 2048, f) == NULL) throw ALE::Exception("File ended prematurely");
162: // Read name
163: x = strtok(buf, " ");
164: const Obj<Mesh::int_section_type>& fault = mesh->getIntSection(x);
165: // Vertices per line
166: x = strtok(NULL, " ");
167: const PetscInt vertsPerLine = atoi(x);
168: // Total vertices
169: x = strtok(NULL, " ");
170: const PetscInt totalVerts = atoi(x);
172: for(PetscInt v = 0; v < totalVerts; ++v) {
173: if (v%vertsPerLine == 0) {
174: if (fgets(buf, 2048, f) == NULL) throw ALE::Exception("File ended prematurely");
175: x = strtok(buf, " ");
176: } else {
177: x = strtok(NULL, " ");
178: }
179: const int vv = atoi(x) + numCells - 1;
181: fault->setFiberDimension(vv, 1);
182: }
183: fault->allocatePoint();
184: }
185: PetscViewerDestroy(viewer);
186: };
187: #endif
188: }
189: }