Nektar++
ProcessDisplacement.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessDisplacement.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Deforms a mesh given input field defining displacement.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #include <iostream>
36 #include <string>
37 using namespace std;
38 
39 #include "ProcessDisplacement.h"
40 
43 #include <LocalRegions/SegExp.h>
44 #include <LocalRegions/TriExp.h>
45 #include <StdRegions/StdQuadExp.h>
46 #include <StdRegions/StdSegExp.h>
47 #include <StdRegions/StdTriExp.h>
48 
49 namespace Nektar
50 {
51 namespace FieldUtils
52 {
53 struct TriFaceIDs
54 {
55  TriFaceIDs(int a, int b, int c) : a(a), b(b), c(c)
56  {
57  }
58  int a;
59  int b;
60  int c;
61 };
62 
64 {
65  std::size_t operator()(TriFaceIDs const &p) const
66  {
67  std::vector<int> ids(3);
68 
69  ids[0] = p.a;
70  ids[1] = p.b;
71  ids[2] = p.c;
72 
73  std::sort(ids.begin(), ids.end());
74  return hash_combine(ids[0], ids[1], ids[2]);
75  }
76 };
77 
78 bool operator==(TriFaceIDs const &p1, TriFaceIDs const &p2)
79 {
80  std::vector<int> ids1(3), ids2(3);
81 
82  ids1[0] = p1.a;
83  ids1[1] = p1.b;
84  ids1[2] = p1.c;
85  ids2[0] = p2.a;
86  ids2[1] = p2.b;
87  ids2[2] = p2.c;
88 
89  std::sort(ids1.begin(), ids1.end());
90  std::sort(ids2.begin(), ids2.end());
91 
92  return ids1[0] == ids2[0] && ids1[1] == ids2[1] && ids1[2] == ids2[2];
93 }
94 
95 typedef std::unordered_map<TriFaceIDs, int, TriFaceHash> TriFaceMap;
96 
97 ModuleKey ProcessDisplacement::className =
99  ModuleKey(eProcessModule, "displacement"), ProcessDisplacement::create,
100  "Deform a mesh given an input field defining displacement");
101 
102 ProcessDisplacement::ProcessDisplacement(FieldSharedPtr f)
104 {
105  m_config["to"] =
106  ConfigOption(false, "", "Name of file containing high order boundary");
107 
108  m_config["usevertexids"] = ConfigOption(
109  true, "0", "Use vertex IDs instead of face IDs for matching");
110 }
111 
113 {
114 }
115 
116 void ProcessDisplacement::v_Process(po::variables_map &vm)
117 {
119  ASSERTL0(!boost::iequals(m_config["bnd"].as<string>(), "All"),
120  "ProcessDisplacement needs bnd parameter with a single id.");
121 
122  string toFile = m_config["to"].as<string>();
123 
124  if (toFile == "")
125  {
126  cout << "ProcessDisplacement: you must provide a file" << endl;
127  return;
128  }
129 
130  bool useVertexIds = m_config["usevertexids"].as<bool>();
131 
132  vector<string> files;
133  files.push_back(toFile);
138 
139  // Try to find boundary condition expansion.
140  int bndCondId = m_config["bnd"].as<int>();
141 
142  // FIXME: We should be storing boundary condition IDs
143  // somewhere...
144  if (bndGraph->GetMeshDimension() == 1)
145  {
146  m_f->m_exp.push_back(m_f->AppendExpList(0, "v"));
147  m_f->m_variables.push_back("v");
148 
149  MultiRegions::ExpListSharedPtr bndCondExpU =
150  m_f->m_exp[0]->GetBndCondExpansions()[bndCondId];
151  MultiRegions::ExpListSharedPtr bndCondExpV =
152  m_f->m_exp[1]->GetBndCondExpansions()[bndCondId];
153 
154  map<int, int> bndCondIds;
155  for (int i = 0; i < bndCondExpU->GetExpSize(); ++i)
156  {
157  bndCondIds[bndCondExpU->GetExp(i)->GetGeom()->GetGlobalID()] = i;
158  }
159 
160  const SpatialDomains::SegGeomMap &tmp = bndGraph->GetAllSegGeoms();
161 
162  for (auto &sIt : tmp)
163  {
164  auto mIt = bndCondIds.find(sIt.first);
165 
166  if (mIt == bndCondIds.end())
167  {
168  cout << "Warning: couldn't find element " << sIt.first << endl;
169  continue;
170  }
171 
172  int e = mIt->second;
173 
175  std::dynamic_pointer_cast<SpatialDomains::SegGeom>(
176  bndCondExpU->GetExp(e)->GetGeom());
177 
178  SpatialDomains::SegGeomSharedPtr to = sIt.second;
179 
180  // Create temporary SegExp
183  bndCondExpU->GetExp(e)->GetBasis(0)->GetBasisKey(), to);
184 
185  const int offset = bndCondExpU->GetPhys_Offset(e);
186  const int nq = toSeg->GetTotPoints();
187 
188  Array<OneD, NekDouble> xL(nq), xC(nq), yL(nq), yC(nq), tmp;
189 
190  bndCondExpU->GetExp(e)->GetCoords(xC, yC);
191  toSeg->GetCoords(xL, yL);
192 
193  Vmath::Vsub(nq, xL, 1, xC, 1,
194  tmp = bndCondExpU->UpdatePhys() + offset, 1);
195  Vmath::Vsub(nq, yL, 1, yC, 1,
196  tmp = bndCondExpV->UpdatePhys() + offset, 1);
197  }
198 
199  // bndconstrained?
200  bndCondExpU->FwdTransBndConstrained(bndCondExpU->GetPhys(),
201  bndCondExpU->UpdateCoeffs());
202  bndCondExpV->FwdTransBndConstrained(bndCondExpV->GetPhys(),
203  bndCondExpV->UpdateCoeffs());
204  }
205  else if (bndGraph->GetMeshDimension() == 2)
206  {
207  m_f->m_exp.push_back(m_f->AppendExpList(0, "v"));
208  m_f->m_exp.push_back(m_f->AppendExpList(0, "w"));
209  m_f->m_variables.push_back("v");
210  m_f->m_variables.push_back("w");
211 
212  MultiRegions::ExpListSharedPtr bndCondExpU =
213  m_f->m_exp[0]->GetBndCondExpansions()[bndCondId];
214  MultiRegions::ExpListSharedPtr bndCondExpV =
215  m_f->m_exp[1]->GetBndCondExpansions()[bndCondId];
216  MultiRegions::ExpListSharedPtr bndCondExpW =
217  m_f->m_exp[2]->GetBndCondExpansions()[bndCondId];
218 
219  map<int, int> bndCondIds;
220  for (int i = 0; i < bndCondExpU->GetExpSize(); ++i)
221  {
222  bndCondIds[bndCondExpU->GetExp(i)->GetGeom()->GetGlobalID()] = i;
223  }
224 
225  TriFaceMap vertexFaceMap;
226 
227  if (useVertexIds)
228  {
229  for (int i = 0; i < bndCondExpU->GetExpSize(); ++i)
230  {
232  std::dynamic_pointer_cast<SpatialDomains::TriGeom>(
233  bndCondExpU->GetExp(i)->GetGeom());
234 
235  TriFaceIDs t(from->GetVid(0), from->GetVid(1), from->GetVid(2));
236  vertexFaceMap[t] = i;
237  }
238  }
239 
240  const SpatialDomains::TriGeomMap &tmp = bndGraph->GetAllTriGeoms();
241 
242  for (auto &sIt : tmp)
243  {
244  int e;
245 
246  if (useVertexIds)
247  {
248  TriFaceIDs t(sIt.second->GetVid(0), sIt.second->GetVid(1),
249  sIt.second->GetVid(2));
250 
251  auto tIt = vertexFaceMap.find(t);
252  e = tIt == vertexFaceMap.end() ? -1 : tIt->second;
253  }
254  else
255  {
256  auto mIt = bndCondIds.find(sIt.first);
257  e = mIt == bndCondIds.end() ? -1 : mIt->second;
258  }
259 
260  if (e == -1)
261  {
262  cout << "Warning: couldn't find element " << sIt.first << endl;
263  continue;
264  }
265 
267  std::dynamic_pointer_cast<SpatialDomains::TriGeom>(
268  bndCondExpU->GetExp(e)->GetGeom());
269 
270  SpatialDomains::TriGeomSharedPtr to = sIt.second;
271 
272  // Create temporary SegExp
275  bndCondExpU->GetExp(e)->GetBasis(0)->GetBasisKey(),
276  bndCondExpV->GetExp(e)->GetBasis(1)->GetBasisKey(), to);
277 
278  const int offset = bndCondExpU->GetPhys_Offset(e);
279  const int nq = toSeg->GetTotPoints();
280 
281  Array<OneD, NekDouble> xL(nq), xC(nq), yL(nq), yC(nq), tmp;
282  Array<OneD, NekDouble> zL(nq), zC(nq);
283 
284  bndCondExpU->GetExp(e)->GetCoords(xC, yC, zC);
285  toSeg->GetCoords(xL, yL, zL);
286 
287  Vmath::Vsub(nq, xL, 1, xC, 1,
288  tmp = bndCondExpU->UpdatePhys() + offset, 1);
289  Vmath::Vsub(nq, yL, 1, yC, 1,
290  tmp = bndCondExpV->UpdatePhys() + offset, 1);
291  Vmath::Vsub(nq, zL, 1, zC, 1,
292  tmp = bndCondExpW->UpdatePhys() + offset, 1);
293  }
294 
295  // bndconstrained?
296  bndCondExpU->FwdTransBndConstrained(bndCondExpU->GetPhys(),
297  bndCondExpU->UpdateCoeffs());
298  bndCondExpV->FwdTransBndConstrained(bndCondExpV->GetPhys(),
299  bndCondExpV->UpdateCoeffs());
300  bndCondExpW->FwdTransBndConstrained(bndCondExpW->GetPhys(),
301  bndCondExpW->UpdateCoeffs());
302  }
303 }
304 } // namespace FieldUtils
305 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
bool operator==(const VertexSharedPtr &v1, const VertexSharedPtr &v2)
Define comparison operator for the vertex struct.
Definition: VtkToFld.cpp:95
FieldSharedPtr m_f
Field object.
Definition: Module.h:234
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:263
This processing module sets up for the boundary field to be extracted.
virtual void v_Process(po::variables_map &vm) override
virtual void v_Process(po::variables_map &vm) override
Write mesh to output file.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:198
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true)
Definition: MeshGraph.cpp:111
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:991
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:317
std::unordered_map< TriFaceIDs, int, TriFaceHash > TriFaceMap
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:49
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< SegExp > SegExpSharedPtr
Definition: SegExp.h:251
std::shared_ptr< TriExp > TriExpSharedPtr
Definition: TriExp.h:253
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
std::map< int, TriGeomSharedPtr > TriGeomMap
Definition: TriGeom.h:59
std::map< int, SegGeomSharedPtr > SegGeomMap
Definition: SegGeom.h:52
std::shared_ptr< SegGeom > SegGeomSharedPtr
Definition: Geometry2D.h:62
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:172
std::shared_ptr< TriGeom > TriGeomSharedPtr
Definition: TriGeom.h:58
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
void hash_combine(std::size_t &seed)
Definition: HashUtils.hpp:46
void Vsub(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Subtract vector z = x-y.
Definition: Vmath.cpp:419
Represents a command-line configuration option.
Definition: Module.h:131
std::size_t operator()(TriFaceIDs const &p) const