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 
63 struct TriFaceHash : std::unary_function<TriFaceIDs, std::size_t>
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"),
100  ProcessDisplacement::create,
101  "Deform a mesh given an input field defining displacement");
102 
103 ProcessDisplacement::ProcessDisplacement(FieldSharedPtr f)
105 {
106  m_config["to"] =
107  ConfigOption(false, "", "Name of file containing high order boundary");
108 
109  m_config["usevertexids"] = ConfigOption(
110  true, "0", "Use vertex IDs instead of face IDs for matching");
111 }
112 
114 {
115 }
116 
117 void ProcessDisplacement::Process(po::variables_map &vm)
118 {
120  ASSERTL0( !boost::iequals(m_config["bnd"].as<string>(), "All"),
121  "ProcessDisplacement needs bnd parameter with a single id.");
122 
123  string toFile = m_config["to"].as<string>();
124 
125  if (toFile == "")
126  {
127  cout << "ProcessDisplacement: you must provide a file" << endl;
128  return;
129  }
130 
131  bool useVertexIds = m_config["usevertexids"].as<bool>();
132 
133  vector<string> files;
134  files.push_back(toFile);
139 
140  // Try to find boundary condition expansion.
141  int bndCondId = m_config["bnd"].as<int>();
142 
143  // FIXME: We should be storing boundary condition IDs
144  // somewhere...
145  if (bndGraph->GetMeshDimension() == 1)
146  {
147  m_f->m_exp.push_back(m_f->AppendExpList(0, "v"));
148  m_f->m_variables.push_back("v");
149 
150  MultiRegions::ExpListSharedPtr bndCondExpU =
151  m_f->m_exp[0]->GetBndCondExpansions()[bndCondId];
152  MultiRegions::ExpListSharedPtr bndCondExpV =
153  m_f->m_exp[1]->GetBndCondExpansions()[bndCondId];
154 
155  map<int, int> bndCondIds;
156  for (int i = 0; i < bndCondExpU->GetExpSize(); ++i)
157  {
158  bndCondIds[bndCondExpU->GetExp(i)->GetGeom()->GetGlobalID()] = i;
159  }
160 
161  const SpatialDomains::SegGeomMap &tmp = bndGraph->GetAllSegGeoms();
162 
163  for (auto &sIt : tmp)
164  {
165  auto mIt = bndCondIds.find(sIt.first);
166 
167  if (mIt == bndCondIds.end())
168  {
169  cout << "Warning: couldn't find element " << sIt.first << endl;
170  continue;
171  }
172 
173  int e = mIt->second;
174 
176  std::dynamic_pointer_cast<SpatialDomains::SegGeom>(
177  bndCondExpU->GetExp(e)->GetGeom());
178 
179  SpatialDomains::SegGeomSharedPtr to = sIt.second;
180 
181  // Create temporary SegExp
184  bndCondExpU->GetExp(e)->GetBasis(0)->GetBasisKey(), to);
185 
186  const int offset = bndCondExpU->GetPhys_Offset(e);
187  const int nq = toSeg->GetTotPoints();
188 
189  Array<OneD, NekDouble> xL(nq), xC(nq), yL(nq), yC(nq), tmp;
190 
191  bndCondExpU->GetExp(e)->GetCoords(xC, yC);
192  toSeg->GetCoords(xL, yL);
193 
194  Vmath::Vsub(nq, xL, 1, xC, 1,
195  tmp = bndCondExpU->UpdatePhys() + offset, 1);
196  Vmath::Vsub(nq, yL, 1, yC, 1,
197  tmp = bndCondExpV->UpdatePhys() + offset, 1);
198  }
199 
200  // bndconstrained?
201  bndCondExpU->FwdTrans_BndConstrained(bndCondExpU->GetPhys(),
202  bndCondExpU->UpdateCoeffs());
203  bndCondExpV->FwdTrans_BndConstrained(bndCondExpV->GetPhys(),
204  bndCondExpV->UpdateCoeffs());
205  }
206  else if (bndGraph->GetMeshDimension() == 2)
207  {
208  m_f->m_exp.push_back(m_f->AppendExpList(0, "v"));
209  m_f->m_exp.push_back(m_f->AppendExpList(0, "w"));
210  m_f->m_variables.push_back("v");
211  m_f->m_variables.push_back("w");
212 
213  MultiRegions::ExpListSharedPtr bndCondExpU =
214  m_f->m_exp[0]->GetBndCondExpansions()[bndCondId];
215  MultiRegions::ExpListSharedPtr bndCondExpV =
216  m_f->m_exp[1]->GetBndCondExpansions()[bndCondId];
217  MultiRegions::ExpListSharedPtr bndCondExpW =
218  m_f->m_exp[2]->GetBndCondExpansions()[bndCondId];
219 
220  map<int, int> bndCondIds;
221  for (int i = 0; i < bndCondExpU->GetExpSize(); ++i)
222  {
223  bndCondIds[bndCondExpU->GetExp(i)->GetGeom()->GetGlobalID()] = i;
224  }
225 
226  TriFaceMap vertexFaceMap;
227 
228  if (useVertexIds)
229  {
230  for (int i = 0; i < bndCondExpU->GetExpSize(); ++i)
231  {
233  std::dynamic_pointer_cast<SpatialDomains::TriGeom>(
234  bndCondExpU->GetExp(i)->GetGeom());
235 
236  TriFaceIDs t(from->GetVid(0), from->GetVid(1), from->GetVid(2));
237  vertexFaceMap[t] = i;
238  }
239  }
240 
241  const SpatialDomains::TriGeomMap &tmp = bndGraph->GetAllTriGeoms();
242 
243  for (auto &sIt : tmp)
244  {
245  int e;
246 
247  if (useVertexIds)
248  {
249  TriFaceIDs t(sIt.second->GetVid(0), sIt.second->GetVid(1),
250  sIt.second->GetVid(2));
251 
252  auto tIt = vertexFaceMap.find(t);
253  e = tIt == vertexFaceMap.end() ? -1 : tIt->second;
254  }
255  else
256  {
257  auto mIt = bndCondIds.find(sIt.first);
258  e = mIt == bndCondIds.end() ? -1 : mIt->second;
259  }
260 
261  if (e == -1)
262  {
263  cout << "Warning: couldn't find element " << sIt.first << endl;
264  continue;
265  }
266 
268  std::dynamic_pointer_cast<SpatialDomains::TriGeom>(
269  bndCondExpU->GetExp(e)->GetGeom());
270 
271  SpatialDomains::TriGeomSharedPtr to = sIt.second;
272 
273  // Create temporary SegExp
276  bndCondExpU->GetExp(e)->GetBasis(0)->GetBasisKey(),
277  bndCondExpV->GetExp(e)->GetBasis(1)->GetBasisKey(), to);
278 
279  const int offset = bndCondExpU->GetPhys_Offset(e);
280  const int nq = toSeg->GetTotPoints();
281 
282  Array<OneD, NekDouble> xL(nq), xC(nq), yL(nq), yC(nq), tmp;
283  Array<OneD, NekDouble> zL(nq), zC(nq);
284 
285  bndCondExpU->GetExp(e)->GetCoords(xC, yC, zC);
286  toSeg->GetCoords(xL, yL, zL);
287 
288  Vmath::Vsub(nq, xL, 1, xC, 1,
289  tmp = bndCondExpU->UpdatePhys() + offset, 1);
290  Vmath::Vsub(nq, yL, 1, yC, 1,
291  tmp = bndCondExpV->UpdatePhys() + offset, 1);
292  Vmath::Vsub(nq, zL, 1, zC, 1,
293  tmp = bndCondExpW->UpdatePhys() + offset, 1);
294  }
295 
296  // bndconstrained?
297  bndCondExpU->FwdTrans_BndConstrained(bndCondExpU->GetPhys(),
298  bndCondExpU->UpdateCoeffs());
299  bndCondExpV->FwdTrans_BndConstrained(bndCondExpV->GetPhys(),
300  bndCondExpV->UpdateCoeffs());
301  bndCondExpW->FwdTrans_BndConstrained(bndCondExpW->GetPhys(),
302  bndCondExpW->UpdateCoeffs());
303  }
304 }
305 }
306 }
virtual void Process(po::variables_map &vm)
Write mesh to output file.
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:163
std::unordered_map< TriFaceIDs, int, TriFaceHash > TriFaceMap
std::map< std::string, ConfigOption > m_config
List of configuration values.
Represents a command-line configuration option.
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
virtual void Process(po::variables_map &vm)
STL namespace.
void hash_combine(std::size_t &seed)
Definition: HashUtils.hpp:46
This processing module sets up for the boundary field to be extracted.
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:762
std::map< int, TriGeomSharedPtr > TriGeomMap
Definition: TriGeom.h:59
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
std::shared_ptr< TriGeom > TriGeomSharedPtr
Definition: TriGeom.h:58
std::pair< ModuleType, std::string > ModuleKey
std::size_t operator()(TriFaceIDs const &p) const
std::map< int, SegGeomSharedPtr > SegGeomMap
Definition: SegGeom.h:52
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
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:346
std::shared_ptr< SegExp > SegExpSharedPtr
Definition: SegExp.h:266
std::shared_ptr< TriExp > TriExpSharedPtr
Definition: TriExp.h:302
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
std::shared_ptr< SegGeom > SegGeomSharedPtr
Definition: Geometry2D.h:62
bool operator==(const VertexSharedPtr &v1, const VertexSharedPtr &v2)
Define comparison operator for the vertex struct.
Definition: VtkToFld.cpp:55
std::shared_ptr< SessionReader > SessionReaderSharedPtr
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, DomainRangeShPtr rng=NullDomainRangeShPtr, bool fillGraph=true)
Definition: MeshGraph.cpp:113
ModuleFactory & GetModuleFactory()
FieldSharedPtr m_f
Field object.