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.
meshStitcher.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 *******************************************************************************/
29 #include "AuxiliaryFunctions.H"
31 
32 #include "IO/cgnsAnalyzer.H"
33 #include "IO/rocstarCgns.H"
34 #include "Mesh/meshBase.H"
35 
36 meshStitcher::meshStitcher(std::vector<std::string> _cgFileNames, bool surf)
37  : cgFileNames(std::move(_cgFileNames)),
38  stitchedMesh(nullptr),
39  cgObj(nullptr) {
40  if (!cgFileNames.empty()) {
41  if (surf)
42  initSurfCgObj();
43  else
44  initVolCgObj();
45  }
46 }
47 
49  partitions.resize(cgFileNames.size(), nullptr);
50  for (int iCg = 0; iCg < cgFileNames.size(); ++iCg) {
51  partitions[iCg] = std::make_shared<cgnsAnalyzer>(cgFileNames[iCg]);
52  partitions[iCg]->loadGrid(1);
53  // defining partition flags
54  std::vector<double> slnData(partitions[iCg]->getNElement(), iCg);
55  // append partition number data if not already existing
56  bool exists = false;
57  std::vector<std::string> slnNamelist;
58  partitions[iCg]->getSolutionDataNames(slnNamelist);
59  for (const auto &in : slnNamelist)
60  if (in == "partitionOld") {
61  exists = true;
62  break;
63  }
64  if (!exists)
65  partitions[iCg]->appendSolutionData("partitionOld", slnData,
67  partitions[iCg]->getNElement(), 1);
68  // stitching new partitions to partition 0
69  // close partition CGNS file to avoid clutter
70  // MS: writing ghost mesh before closing
71  // std::vector<std::string> secNames;
72  // partitions[iCg]->getSectionNames(secNames);
73  // auto its = std::find(secNames.begin(), secNames.end(), ":T4:virtual");
74  // if (its != secNames.end())
75  // meshBase::Create(partitions[iCg]->getSectionMesh(*its),
76  // "_virtual_" + nemAux::find_name(cgFileNames[iCg]) +
77  // ".vtu")->write();
78  // End of ghost mesh
79  if (iCg) {
80  partitions[0]->stitchMesh(partitions[iCg].get(), true);
81  partitions[iCg]->closeCG();
82  }
83  }
84  std::cout << "Meshes stitched successfully." << std::endl;
85  std::cout << "Exporting stitched mesh to VTK format." << std::endl;
86  std::string newname(cgFileNames[0]);
87  std::size_t pos = newname.find_last_of('/');
88  newname = newname.substr(pos + 1);
89  newname = nemAux::trim_fname(newname, "stitched.vtu");
90  stitchedMesh = meshBase::CreateShared(partitions[0]->getVTKMesh(), newname);
91  std::cout << "Transferring physical quantities to vtk mesh." << std::endl;
92  // figure out what is existing on the stitched grid
93  int outNData, outNDim;
94  std::vector<std::string> slnNameList;
95  std::vector<std::string> appSlnNameList;
96  partitions[0]->getSolutionDataNames(slnNameList);
97  partitions[0]->getAppendedSolutionDataName(appSlnNameList);
98  slnNameList.insert(slnNameList.end(), appSlnNameList.begin(),
99  appSlnNameList.end());
100 
101  // write all data into vtk file
102  for (auto is = slnNameList.begin(); is < slnNameList.end(); is++) {
103  std::vector<double> physData;
104  partitions[0]->getSolutionDataStitched(*is, physData, outNData, outNDim);
105  solution_type_t dt = partitions[0]->getSolutionDataObj(*is)->getDataType();
106  if (dt == solution_type_t::NODAL) {
107  std::cout << "Embedding nodal " << *is << std::endl;
108  stitchedMesh->setPointDataArray((*is).c_str(), physData);
109  } else {
110  // gs field is 'weird' in irocstar files- we don't write it back
111  // if (!(*is).compare("gs"))
112  // continue;
113  std::cout << "Embedding cell-based " << *is << std::endl;
114  stitchedMesh->setCellDataArray((*is).c_str(), physData);
115  }
116  }
117  stitchedMesh->report();
118  // TODO: Expensive IO, refactoring needed
119  stitchedMesh->write();
120 }
121 
123  cgObj = std::make_shared<rocstarCgns>(cgFileNames);
124  // different read for burn files
125  if (cgFileNames[0].find("burn") != std::string::npos)
126  cgObj->setBurnBool(true);
127  cgObj->loadCgSeries();
128  cgObj->stitchGroup();
129  // cgObj->closeCG();
130  std::cout << "Surface mesh stitched successfully." << std::endl;
131  std::cout << "Exporting stitched mesh to VTK format." << std::endl;
132  std::string newname(cgFileNames[0]);
133  std::size_t pos = newname.find_last_of('/');
134  newname = newname.substr(pos + 1);
135  newname = nemAux::trim_fname(newname, "stitched.vtu");
136  stitchedMesh = meshBase::CreateShared(cgObj->getVTKMesh(), newname);
137  std::cout << "Transferring physical quantities to vtk mesh." << std::endl;
138  // figure out what exists on the stitched grid
139  int outNData, outNDim;
140  std::vector<std::string> slnNameList;
141  std::vector<std::string> appSlnNameList;
142  cgObj->getSolutionDataNames(slnNameList);
143  cgObj->getAppendedSolutionDataName(appSlnNameList);
144  slnNameList.insert(slnNameList.end(), appSlnNameList.begin(),
145  appSlnNameList.end());
146  // write all data into vtk file
147  for (auto is = slnNameList.begin(); is < slnNameList.end(); is++) {
148  std::vector<double> physData;
149  cgObj->getSolutionDataStitched(*is, physData, outNData, outNDim);
150  solution_type_t dt = cgObj->getSolutionDataObj(*is)->getDataType();
151  if (dt == solution_type_t::NODAL) {
152  std::cout << "Embedding nodal " << *is << std::endl;
153  stitchedMesh->setPointDataArray((*is).c_str(), physData);
154  } else {
155  // gs field is 'weird' in irocstar files- we don't write it back
156  // if (!(*is).compare("gs"))
157  if (*is == "mdot_old") { continue; }
158  std::cout << "Embedding cell-based " << *is << std::endl;
159  stitchedMesh->setCellDataArray((*is).c_str(), physData);
160  }
161  }
162  stitchedMesh->report();
163  // TODO: expensive IO, need to be refactored
164  stitchedMesh->write();
165 }
166 
167 std::shared_ptr<meshBase> meshStitcher::getStitchedMB() const {
168  if (stitchedMesh)
169  return stitchedMesh;
170  else {
171  std::cerr << "No stitched mesh to return!" << std::endl;
172  exit(1);
173  }
174 }
175 
176 std::shared_ptr<cgnsAnalyzer> meshStitcher::getStitchedCGNS() const {
177  if (!partitions.empty())
178  return partitions[0];
179  else if (cgObj)
180  return cgObj;
181  else {
182  std::cerr << "No stitched mesh to return!" << std::endl;
183  exit(1);
184  }
185 }
const std::vector< std::string > cgFileNames
Definition: meshStitcher.H:65
STL namespace.
void initVolCgObj()
Definition: meshStitcher.C:48
std::shared_ptr< meshBase > getStitchedMB() const
Definition: meshStitcher.C:167
std::string trim_fname(const std::string &name, const std::string &ext)
void initSurfCgObj()
Definition: meshStitcher.C:122
std::shared_ptr< cgnsAnalyzer > getStitchedCGNS() const
Definition: meshStitcher.C:176
solution_type_t
Definition: cgnsAnalyzer.H:58
std::shared_ptr< rocstarCgns > cgObj
Definition: meshStitcher.H:71
std::shared_ptr< meshBase > stitchedMesh
Definition: meshStitcher.H:69
std::vector< std::shared_ptr< cgnsAnalyzer > > partitions
Definition: meshStitcher.H:67
static std::shared_ptr< meshBase > CreateShared(const std::string &fname)
Create shared ptr from fname.
Definition: meshBase.C:171
meshStitcher(std::vector< std::string > cgFileNames, bool surf)
Definition: meshStitcher.C:36