NEMoSys  0.63.0
A modular, extensible resource with robust automated mesh generation, mesh quality analysis, adaptive mesh refinement, and data transfer between arbitrary meshes.
GmshToExoConversionDriver.C
Go to the documentation of this file.
1 /*******************************************************************************
2 * Promesh *
3 * Copyright (C) 2022, IllinoisRocstar LLC. All rights reserved. *
4 * *
5 * Promesh is the property of IllinoisRocstar LLC. *
6 * *
7 * IllinoisRocstar LLC *
8 * Champaign, IL *
9 * www.illinoisrocstar.com *
10 * promesh@illinoisrocstar.com *
11 *******************************************************************************/
12 /*******************************************************************************
13 * This file is part of Promesh *
14 * *
15 * This version of Promesh is free software: you can redistribute it and/or *
16 * modify it under the terms of the GNU Lesser General Public License as *
17 * published by the Free Software Foundation, either version 3 of the License, *
18 * or (at your option) any later version. *
19 * *
20 * Promesh is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more *
23 * details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with this program. If not, see <https://www.gnu.org/licenses/>. *
27 * *
28 *******************************************************************************/
30 
31 #include <vtkCell.h>
32 #include "AuxiliaryFunctions.H"
33 #include "Mesh/exoMesh.H"
34 #include "MeshOperation/meshSrch.H"
35 
36 // helper functions
37 
38 namespace NEM {
39 namespace DRV {
40 
42  : taskFile(std::move(taskFile)) {}
43 
45  : meshFileName(std::move(meshFile)) {}
46 
48  std::vector<MeshData> meshData,
49  bool needsPostProc)
50  : numMeshes(numMeshes),
51  meshData(std::move(meshData)),
52  needsPostProc(needsPostProc) {}
53 
55  : file_(std::move(file)), opts_(std::move(opts)) {}
56 
58  : GmshToExoConversionDriver(Files{{}}, Opts{{}, {}, {}}) {}
59 
61  const {
62  return file_;
63 }
64 
66  this->file_ = std::move(file);
67 }
68 
70  const {
71  return opts_;
72 }
73 
75  this->opts_ = std::move(opts);
76 }
77 
79  std::cout << "Converting to EXODUS II..." << std::endl;
80 
81  // sanity check
82  if (this->opts_.numMeshes == 0) {
83  std::cerr << "Error: At least one mesh should be provided!\n";
84  exit(-1);
85  }
86 
87  // starting conversion operation
88  auto em = new NEM::MSH::EXOMesh::exoMesh(this->file_.outputFile);
89 
90  // reading meshes
91  int ieb = 0; // Element Block counter.
92  int ins = 0; // Node Set counter.
93  int iss = 0; // Side Set counter.
94  int ndeIdOffset = 0; // for element blocks and node sets
95  int elmIdOffset = 0; // for side sets
96  for (int iMsh = 0; iMsh < this->opts_.numMeshes; iMsh++) {
97  const auto &meshData = this->opts_.meshData[iMsh];
98  // reading input mesh
99  std::string fileExt = nemAux::find_ext(meshData.meshFileName);
100  if (fileExt == ".g" || fileExt == ".e" || fileExt == ".exo" ||
101  fileExt == ".exodus") {
102  // if already exodus format, stitch and continue.
104  em2.read(meshData.meshFileName);
105 
106  // Allow changing element block names using key-value.
107  for (const auto &kv : meshData.elmBlkNames)
108  em2.setBlockName(std::stoi(kv.first) - 1, kv.second);
109 
110  // Allow defining a global node set containing all nodes.
111  if (meshData.addGlobalNodeSet) {
113  ns.id = ++ins;
114  ns.nNde = em2.getNumberOfNodes();
115  ns.name = meshData.addGlobalNodeSet.value();
116  ns.ndeIdOffset = ndeIdOffset;
117  for (int iNde = 0; iNde < ns.nNde; ++iNde)
118  ns.ndeIds.emplace_back(iNde + 1);
119  em2.addNdeSet(ns);
120  }
121 
122  em->stitch(em2);
123  ieb += em2.getNumberOfElementBlocks();
124  ins += em2.getNumberOfNodeSets();
125  iss += em2.getNumberOfSideSets();
126  ndeIdOffset += em2.getNumberOfNodes();
127  elmIdOffset += em2.getNumberOfElements();
128  continue;
129  }
130 
131  // reading input mesh
132  meshBase *mb = meshBase::Create(meshData.meshFileName);
133  // mb->write("exo_inp_mesh_"+std::to_string(iMsh)+".vtu");
134 
135  // adding information to exodusII object
136  int ndeIdOffset_local = 0;
137  int elmIdOffset_local = 0;
138 
140  mb, em, ndeIdOffset, elmIdOffset, ins, ieb, iss, meshData.meshName,
141  meshData.usePhys, ndeIdOffset_local, elmIdOffset_local,
142  meshData.makeFreeSurfSS, meshData.splitTopBotSS, meshData.sideSetNames);
143 
144  // offsetting starting ids for next file
145  ndeIdOffset += ndeIdOffset_local;
146  elmIdOffset += elmIdOffset_local;
147  // clean up
148  delete mb;
149  }
150 
151  // writing the file
152  em->write();
153  em->report();
154 
155  // performing post-processing tasks
156  if (this->opts_.needsPostProc) {
157  for (int iTsk = 0; iTsk < this->opts_.numTasks; iTsk++) {
158  const auto &ppFName = this->opts_.tasks[iTsk].taskFile;
159  std::cout << "Reading Post Processing JSON file " << iTsk << std::endl;
160  std::ifstream inputStream(ppFName);
161  if (!inputStream.good() || nemAux::find_ext(ppFName) != ".json") {
162  std::cerr << "Error opening file " << ppFName << std::endl;
163  exit(1);
164  }
165  if (nemAux::find_ext(ppFName) != ".json") {
166  std::cerr << "Input File must be in .json format" << std::endl;
167  exit(1);
168  }
169  jsoncons::json ppJson;
170  inputStream >> ppJson;
171  ConversionDriver::procExo(ppJson, this->file_.outputFile, em);
172  }
173 
174  // writing augmented exo file
175  std::cout << "writing exodus file" << std::endl;
176  em->write();
177  em->report();
178  }
179 
180  // clean up
181  delete em;
182 }
183 
184 } // namespace DRV
185 } // namespace NEM
std::string outputFile
Definition: NemDriver.H:78
A complete I/O class for EXODUS II file format.
Definition: exoMesh.H:172
void setBlockName(int idx, const std::string &name)
Sets/changes block name.
Definition: exoMesh.H:421
A brief description of meshBase.
Definition: meshBase.H:64
int getNumberOfNodeSets() const
Returns total number of node sets.
Definition: exoMesh.H:212
STL namespace.
static void procExo(const jsoncons::json &ppJson, const std::string &fname, NEM::MSH::EXOMesh::exoMesh *em)
static meshBase * Create(const std::string &fname)
Construct vtkMesh from filename.
Definition: meshBase.C:78
std::string find_ext(const std::string &fname)
int getNumberOfSideSets() const
Returns total number of side sets.
Definition: exoMesh.H:217
int getNumberOfNodes() const
Returns total number of nodes.
Definition: exoMesh.H:197
void execute() const override
Run the workflow represented by the driver.
virtual void write() const
write the mesh to file named after the private var &#39;filename&#39;.
Definition: meshBase.H:598
void addNdeSet(const ndeSetType &ns)
Add node set to the database.
Definition: exoMesh.H:512
Stores node set information.
Definition: exoMesh.H:71
std::vector< int > ndeIds
Definition: exoMesh.H:75
int getNumberOfElements() const
Returns total number of elements.
Definition: exoMesh.H:202
void read(const std::string &ifname=std::string())
Resets the class and reads from file name provided.
Definition: exoMesh.C:1003
int getNumberOfElementBlocks() const
Returns total number of element blocks.
Definition: exoMesh.H:207
static void genExo(std::vector< meshBase *> meshes, const std::string &fname)