Nektar++
ProcessLinear.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessLinear.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: linearises mesh.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
36 #include "ProcessLinear.h"
37 
38 using namespace std;
39 
40 namespace Nektar
41 {
42 namespace Utilities
43 {
44 
45 using namespace Nektar::NekMeshUtils;
46 
47 ModuleKey ProcessLinear::className = GetModuleFactory().RegisterCreatorFunction(
48  ModuleKey(eProcessModule, "linearise"),
49  ProcessLinear::create,
50  "Linearises mesh.");
51 
52 ProcessLinear::ProcessLinear(MeshSharedPtr m) : ProcessModule(m)
53 {
54  m_config["all"] =
55  ConfigOption(true, "0", "remove curve nodes for all elements.");
56  m_config["invalid"] =
57  ConfigOption(false, "0", "remove curve nodes if element is invalid.");
58  m_config["prismonly"] =
59  ConfigOption(true, "0", "only acts on prims");
60  m_config["extract"] =
61  ConfigOption(false, "", "dump a mesh of the extracted elements");
62 }
63 
65 {
66 }
67 
69 {
70  if (m_mesh->m_verbose)
71  {
72  cout << "ProcessLinear: Linearising mesh... " << endl;
73  }
74 
75  bool all = m_config["all"].as<bool>();
76  bool invalid = m_config["invalid"].beenSet;
77  NekDouble thr = m_config["invalid"].as<NekDouble>();
78 
79  ASSERTL0(all || invalid,
80  "Must specify an option: all (to remove all curvature) or invalid "
81  "(to remove curvature that makes elements invalid)");
82 
83  if (all)
84  {
85  for (auto &edge : m_mesh->m_edgeSet)
86  {
87  edge->m_edgeNodes.clear();
88  }
89 
90  for (auto &face : m_mesh->m_faceSet)
91  {
92  face->m_faceNodes.clear();
93  }
94 
95  for (int i = 0; i < m_mesh->m_element[m_mesh->m_expDim].size(); i++)
96  {
97  vector<NodeSharedPtr> empty;
98  m_mesh->m_element[m_mesh->m_expDim][i]->SetVolumeNodes(empty);
99  }
100 
101  if (m_mesh->m_verbose)
102  {
103  cerr << "Removed all element curvature" << endl;
104  }
105  }
106  else if (invalid)
107  {
108  map<int,vector<FaceSharedPtr> > eidToFace;
109  map<int,vector<ElementSharedPtr> > eidToElm;
110 
111  vector<ElementSharedPtr> els = m_mesh->m_element[m_mesh->m_expDim];
112  vector<ElementSharedPtr> el = els;
113 
114  for(int i = 0; i < el.size(); i++)
115  {
116  vector<EdgeSharedPtr> e = el[i]->GetEdgeList();
117  for(int j = 0; j < e.size(); j++)
118  {
119  eidToElm[e[j]->m_id].push_back(el[i]);
120  }
121  }
122 
123  if(m_mesh->m_expDim > 2)
124  {
125  for(auto &face : m_mesh->m_faceSet)
126  {
127  vector<EdgeSharedPtr> es = face->m_edgeList;
128  for(int i = 0; i < es.size(); i++)
129  {
130  eidToFace[es[i]->m_id].push_back(face);
131  }
132  }
133  }
134 
135  set<int> neigh;
136  vector<NodeSharedPtr> zeroNodes;
137  std::unordered_set<int> clearedEdges, clearedFaces, clearedElmts;
138 
139  vector<ElementSharedPtr> dumpEls;
140 
141  // Iterate over list of elements of expansion dimension.
142  while(el.size() > 0)
143  {
144  for (int i = 0; i < el.size(); ++i)
145  {
146  if(m_config["prismonly"].beenSet)
147  {
148  if(el[i]->GetConf().m_e != LibUtilities::ePrism)
149  {
150  continue;
151  }
152  }
153 
154  if (Invalid(el[i],thr))//(!gfac->IsValid())
155  {
156  dumpEls.push_back(el[i]);
157  clearedElmts.insert(el[i]->GetId());;
158  el[i]->SetVolumeNodes(zeroNodes);
159 
160  vector<FaceSharedPtr> f = el[i]->GetFaceList();
161  for (int j = 0; j < f.size(); j++)
162  {
163  f[j]->m_faceNodes.clear();
164  clearedFaces.insert(f[j]->m_id);
165  }
166  vector<EdgeSharedPtr> e = el[i]->GetEdgeList();
167  for(int j = 0; j < e.size(); j++)
168  {
169  e[j]->m_edgeNodes.clear();
170  clearedEdges.insert(e[j]->m_id);
171  }
172 
173  if(m_mesh->m_expDim > 2)
174  {
175  for(int j = 0; j < e.size(); j++)
176  {
177  auto it = eidToFace.find(e[j]->m_id);
178  for(int k = 0; k < it->second.size(); k++)
179  {
180  clearedEdges.insert(it->second[k]->m_id);
181  it->second[k]->m_faceNodes.clear();
182  }
183  }
184  }
185 
186  for(int j = 0; j < e.size(); j++)
187  {
188  auto it = eidToElm.find(e[j]->m_id);
189  for(int k = 0; k < it->second.size(); k++)
190  {
191  neigh.insert(it->second[k]->GetId());
192  }
193  }
194  }
195  }
196 
197  el.clear();
198  for(int i = 0; i < els.size(); i++)
199  {
200  auto it1 = neigh.find(els[i]->GetId());
201  auto it2 = clearedElmts.find(els[i]->GetId());
202  if(it1 != neigh.end() && it2 == clearedElmts.end())
203  {
204  el.push_back(els[i]);
205  }
206  }
207  neigh.clear();
208  }
209 
210  if (m_mesh->m_verbose)
211  {
212  cerr << "Removed curvature from " << clearedElmts.size()
213  << " elements (" << clearedEdges.size() << " edges, "
214  << clearedFaces.size() << " faces)" << endl;
215  }
216 
217  if(m_config["extract"].beenSet)
218  {
219  MeshSharedPtr dmp = std::shared_ptr<Mesh>(new Mesh());
220  dmp->m_expDim = 3;
221  dmp->m_spaceDim = 3;
222  dmp->m_nummode = 2;
223 
224  dmp->m_element[3] = dumpEls;
225 
227  ModuleKey(eOutputModule, "xml"), dmp);
228  mod->RegisterConfig("outfile", m_config["extract"].as<string>().c_str());
229  mod->ProcessVertices();
230  mod->ProcessEdges();
231  mod->ProcessFaces();
232  mod->ProcessElements();
233  mod->ProcessComposites();
234  mod->Process();
235  }
236  }
237 }
238 
240 {
241  // Create elemental geometry.
243  el->GetGeom(m_mesh->m_spaceDim);
244 
245  // Generate geometric factors.
247  geom->GetGeomFactors();
248 
249  if(!gfac->IsValid())
250  {
251  return true;
252  }
253 
254  vector<NodeSharedPtr> ns = el->GetVertexList();
255  ElmtConfig c = el->GetConf();
256  c.m_order = 1;
257  c.m_faceNodes = false;
258  c.m_volumeNodes = false;
259  c.m_reorient = false;
260 
262  c.m_e, c, ns, el->GetTagList());
263  SpatialDomains::GeometrySharedPtr geomL = elL->GetGeom(m_mesh->m_spaceDim);
264  SpatialDomains::GeomFactorsSharedPtr gfacL = geomL->GetGeomFactors();
265 
266  LibUtilities::PointsKeyVector p = geom->GetXmap()->GetPointsKeys();
267  SpatialDomains::DerivStorage deriv = gfac->GetDeriv(p);
268  SpatialDomains::DerivStorage derivL = gfacL->GetDeriv(p);
269  const int pts = deriv[0][0].num_elements();
270  Array<OneD,NekDouble> jc(pts);
271  Array<OneD,NekDouble> jcL(pts);
272  for (int k = 0; k < pts; ++k)
273  {
274  DNekMat jac(m_mesh->m_expDim, m_mesh->m_expDim, 0.0, eFULL);
275  DNekMat jacL(m_mesh->m_expDim, m_mesh->m_expDim, 0.0, eFULL);
276 
277  for (int l = 0; l < m_mesh->m_expDim; ++l)
278  {
279  for (int j = 0; j < m_mesh->m_expDim; ++j)
280  {
281  jac(j,l) = deriv[l][j][k];
282  jacL(j,l) = derivL[l][j][k];
283  }
284  }
285 
286  if(m_mesh->m_expDim == 2)
287  {
288  jc[k] = jac(0,0) * jac(1,1) - jac(0,1)*jac(1,0);
289  jcL[k] = jacL(0,0) * jacL(1,1) - jacL(0,1)*jacL(1,0);
290  }
291  else if(m_mesh->m_expDim == 3)
292  {
293  jc[k] = jac(0,0) * (jac(1,1)*jac(2,2) - jac(2,1)*jac(1,2)) -
294  jac(0,1) * (jac(1,0)*jac(2,2) - jac(2,0)*jac(1,2)) +
295  jac(0,2) * (jac(1,0)*jac(2,1) - jac(2,0)*jac(1,1));
296  jcL[k] = jacL(0,0) * (jacL(1,1)*jacL(2,2) - jacL(2,1)*jacL(1,2)) -
297  jacL(0,1) * (jacL(1,0)*jacL(2,2) - jacL(2,0)*jacL(1,2)) +
298  jacL(0,2) * (jacL(1,0)*jacL(2,1) - jacL(2,0)*jacL(1,1));
299  }
300  }
301 
302  Array<OneD, NekDouble> j(pts);
303  Vmath::Vdiv(jc.num_elements(),jc,1,jcL,1,j,1);
304 
305  NekDouble scaledJac = Vmath::Vmin(j.num_elements(),j,1) /
306  Vmath::Vmax(j.num_elements(),j,1);
307 
308  //cout << scaledJac << endl;
309 
310  if(scaledJac < thr)
311  {
312  return true;
313  }
314 
315  return false;
316 }
317 }
318 }
bool m_faceNodes
Denotes whether the element contains face nodes. For 2D elements, if this is true then the element co...
Definition: ElementConfig.h:81
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
Basic information about an element.
Definition: ElementConfig.h:49
std::vector< PointsKey > PointsKeyVector
Definition: Points.h:246
virtual void Process()
Write mesh to output file.
T Vmax(int n, const T *x, const int incx)
Return the maximum element in x – called vmax to avoid conflict with max.
Definition: Vmath.cpp:782
std::shared_ptr< GeomFactors > GeomFactorsSharedPtr
Pointer to a GeomFactors object.
Definition: GeomFactors.h:62
T Vmin(int n, const T *x, const int incx)
Return the minimum element in x - called vmin to avoid conflict with min.
Definition: Vmath.cpp:874
std::shared_ptr< Module > ModuleSharedPtr
STL namespace.
void Vdiv(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x/y.
Definition: Vmath.cpp:244
std::shared_ptr< Mesh > MeshSharedPtr
Shared pointer to a mesh.
Definition: Mesh.h:156
ElementFactory & GetElementFactory()
Definition: Element.cpp:44
unsigned int m_order
Order of the element.
Definition: ElementConfig.h:88
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:144
std::pair< ModuleType, std::string > ModuleKey
bool Invalid(NekMeshUtils::ElementSharedPtr el, NekDouble thr)
bool m_volumeNodes
Denotes whether the element contains volume (i.e. interior) nodes. These are not supported by either ...
Definition: ElementConfig.h:86
std::shared_ptr< Element > ElementSharedPtr
Definition: Edge.h:49
Represents a command-line configuration option.
std::shared_ptr< Geometry > GeometrySharedPtr
Definition: Geometry.h:53
double NekDouble
std::map< std::string, ConfigOption > m_config
List of configuration values.
Abstract base class for processing modules.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
bool m_reorient
Denotes whether the element needs to be re-orientated for a spectral element framework.
Definition: ElementConfig.h:91
std::pair< ModuleType, std::string > ModuleKey
LibUtilities::ShapeType m_e
Element type (e.g. triangle, quad, etc).
Definition: ElementConfig.h:78
ModuleFactory & GetModuleFactory()