Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ProcessQualityMetric.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessQualityMetric.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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Compute quality metric of Roca et al.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <string>
37 #include <iostream>
38 using namespace std;
39 
40 #include "ProcessQualityMetric.h"
41 
45 #include <StdRegions/StdTriExp.h>
46 #include <StdRegions/StdQuadExp.h>
47 #include <StdRegions/StdTetExp.h>
48 #include <StdRegions/StdPrismExp.h>
49 
50 namespace Nektar
51 {
52 namespace Utilities
53 {
54 
55 ModuleKey ProcessQualityMetric::className =
57  ModuleKey(eProcessModule, "qualitymetric"),
58  ProcessQualityMetric::create,
59  "add quality metric to field.");
60 
61 ProcessQualityMetric::ProcessQualityMetric(FieldSharedPtr f) :
62  ProcessModule(f)
63 {
64 
65 }
66 
68 {
69 }
70 
71 void ProcessQualityMetric::Process(po::variables_map &vm)
72 {
73  if (m_f->m_verbose)
74  {
75  cout << "ProcessQualityMetric: Process Jacobian fld" << endl;
76  }
77 
78  Array<OneD, NekDouble> &phys = m_f->m_exp[0]->UpdatePhys();
79  Array<OneD, NekDouble> &coeffs = m_f->m_exp[0]->UpdateCoeffs();
80 
81  for(int i =0; i < m_f->m_exp[0]->GetExpSize(); ++i)
82  {
83  // copy Jacobian into field
84  LocalRegions::ExpansionSharedPtr Elmt = m_f->m_exp[0]->GetExp(i);
85  int offset = m_f->m_exp[0]->GetPhys_Offset(i);
86  Array<OneD, NekDouble> q = GetQ(Elmt);
87  Array<OneD, NekDouble> out = phys + offset;
88 
89  ASSERTL0(q.num_elements() == Elmt->GetTotPoints(), "number of points mismatch");
90  Vmath::Vcopy(q.num_elements(), q, 1, out, 1);
91  }
92 
93  m_f->m_exp[0]->FwdTrans_IterPerExp(phys, coeffs);
94 
95  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
96  = m_f->m_exp[0]->GetFieldDefinitions();
97  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
98 
99  for (int i = 0; i < FieldDef.size(); ++i)
100  {
101  FieldDef[i]->m_fields.push_back("QualityMetric");
102  m_f->m_exp[0]->AppendFieldData(FieldDef[i], FieldData[i]);
103  }
104 
105  m_f->m_fielddef = FieldDef;
106  m_f->m_data = FieldData;
107 }
108 
111 {
112  vector<DNekMat> ret;
113 
114  if(geom->GetShapeType() == LibUtilities::eQuadrilateral)
115  {
116  vector<Array<OneD, NekDouble> > xy;
117  for(int i = 0; i < geom->GetNumVerts(); i++)
118  {
119  Array<OneD, NekDouble> loc(2);
120  SpatialDomains::PointGeomSharedPtr p = geom->GetVertex(i);
121  p->GetCoords(loc);
122  xy.push_back(loc);
123  }
124 
126  Array<OneD, NekDouble> u = b[0]->GetZ();
127  Array<OneD, NekDouble> v = b[1]->GetZ();
128 
129  for(int j = 0; j < b[1]->GetNumPoints(); j++)
130  {
131  for(int i = 0; i < b[0]->GetNumPoints(); i++)
132  {
133  NekDouble a1 = 0.5*(1.0-u[i]), a2 = 0.5*(1.0+u[i]);
134  NekDouble b1 = 0.5*(1.0-v[j]), b2 = 0.5*(1.0+v[j]);
135  DNekMat dxdz(2,2,1.0,eFULL);
136 
137  dxdz(0,0) = 0.5*(-b1*xy[0][0] + b1*xy[1][0] + b2*xy[2][0] - b2*xy[3][0]);
138  dxdz(1,0) = 0.5*(-b1*xy[0][1] + b1*xy[1][1] + b2*xy[2][1] - b2*xy[3][1]);
139 
140  dxdz(0,1) = 0.5*(-a1*xy[0][0] - a2*xy[1][0] + a2*xy[2][0] + a1*xy[3][0]);
141  dxdz(1,1) = 0.5*(-a1*xy[0][1] - a2*xy[1][1] + a2*xy[2][1] + a1*xy[3][1]);
142 
143  dxdz.Invert();
144  ret.push_back(dxdz);
145  }
146  }
147  }
148  else if(geom->GetShapeType() == LibUtilities::eTriangle)
149  {
150  vector<Array<OneD, NekDouble> > xy;
151  for(int i = 0; i < geom->GetNumVerts(); i++)
152  {
153  Array<OneD, NekDouble> loc(2);
154  SpatialDomains::PointGeomSharedPtr p = geom->GetVertex(i);
155  p->GetCoords(loc);
156  xy.push_back(loc);
157  }
158 
160  Array<OneD, NekDouble> u = b[0]->GetZ();
161  Array<OneD, NekDouble> v = b[1]->GetZ();
162 
163  for(int i = 0; i < b[0]->GetNumPoints(); i++)
164  {
165  for(int j = 0; j < b[1]->GetNumPoints(); j++)
166  {
167  DNekMat dxdz(2,2,1.0,eFULL);
168  dxdz(0,0) = -xy[0][0]/2.0 + xy[1][0]/2.0;
169 
170  dxdz(0,1) = -xy[0][0]/2.0 + xy[2][0]/2.0;
171 
172  dxdz(1,0) = -xy[0][1]/2.0 + xy[1][1]/2.0;
173 
174  dxdz(1,1) = -xy[0][1]/2.0 + xy[2][1]/2.0;
175 
176  dxdz.Invert();
177  ret.push_back(dxdz);
178  }
179  }
180  }
181  else if(geom->GetShapeType() == LibUtilities::eTetrahedron)
182  {
183  vector<Array<OneD, NekDouble> > xyz;
184  for(int i = 0; i < geom->GetNumVerts(); i++)
185  {
186  Array<OneD, NekDouble> loc(3);
187  SpatialDomains::PointGeomSharedPtr p = geom->GetVertex(i);
188  p->GetCoords(loc);
189  xyz.push_back(loc);
190  }
191 
193  Array<OneD, NekDouble> u = b[0]->GetZ();
194  Array<OneD, NekDouble> v = b[1]->GetZ();
195  Array<OneD, NekDouble> z = b[2]->GetZ();
196 
197  for(int i = 0; i < b[0]->GetNumPoints(); i++)
198  {
199  for(int j = 0; j < b[1]->GetNumPoints(); j++)
200  {
201  for(int k = 0; k < b[2]->GetNumPoints(); k++)
202  {
203  DNekMat dxdz(3,3,1.0,eFULL);
204  dxdz(0,0) = -xyz[0][0]/2.0 + xyz[1][0]/2.0;
205 
206  dxdz(0,1) = -xyz[0][0]/2.0 + xyz[2][0]/2.0;
207 
208  dxdz(0,2) = -xyz[0][0]/2.0 + xyz[3][0]/2.0;
209 
210 
211  dxdz(1,0) = -xyz[0][1]/2.0 + xyz[1][1]/2.0;
212 
213  dxdz(1,1) = -xyz[0][1]/2.0 + xyz[2][1]/2.0;
214 
215  dxdz(1,2) = -xyz[0][1]/2.0 + xyz[3][1]/2.0;
216 
217 
218  dxdz(2,0) = -xyz[0][2]/2.0 + xyz[1][2]/2.0;
219 
220  dxdz(2,1) = -xyz[0][2]/2.0 + xyz[2][2]/2.0;
221 
222  dxdz(2,2) = -xyz[0][2]/2.0 + xyz[3][2]/2.0;
223 
224  dxdz.Invert();
225  ret.push_back(dxdz);
226  }
227  }
228  }
229  }
230  else if(geom->GetShapeType() == LibUtilities::ePrism)
231  {
232  vector<Array<OneD, NekDouble> > xyz;
233  for(int i = 0; i < geom->GetNumVerts(); i++)
234  {
235  Array<OneD, NekDouble> loc(3);
236  SpatialDomains::PointGeomSharedPtr p = geom->GetVertex(i);
237  p->GetCoords(loc);
238  xyz.push_back(loc);
239  }
240 
242  Array<OneD, NekDouble> eta1 = b[0]->GetZ();
243  Array<OneD, NekDouble> eta2 = b[1]->GetZ();
244  Array<OneD, NekDouble> eta3 = b[2]->GetZ();
245 
246  for(int k = 0; k < b[2]->GetNumPoints(); k++)
247  {
248  for(int j = 0; j < b[1]->GetNumPoints(); j++)
249  {
250  for(int i = 0; i < b[0]->GetNumPoints(); i++)
251  {
252  NekDouble xi1 = 0.5*(1+eta1[i])*(1-eta3[k])-1.0;
253  NekDouble a2 = 0.5*(1+xi1);
254  NekDouble b1 = 0.5*(1-eta2[j]), b2 = 0.5*(1+eta2[j]);
255  NekDouble c1 = 0.5*(1-eta3[k]), c2 = 0.5*(1+eta3[k]);
256 
257  DNekMat dxdz(3,3,1.0,eFULL);
258 
259  dxdz(0,0) = 0.5*(-b1*xyz[0][0] + b1*xyz[1][0] + b2*xyz[2][0] - b2*xyz[3][0]);
260  dxdz(1,0) = 0.5*(-b1*xyz[0][1] + b1*xyz[1][1] + b2*xyz[2][1] - b2*xyz[3][1]);
261  dxdz(2,0) = 0.5*(-b1*xyz[0][2] + b1*xyz[1][2] + b2*xyz[2][2] - b2*xyz[3][2]);
262 
263  dxdz(0,1) = 0.5*((a2-c1)*xyz[0][0] - a2*xyz[1][0] + a2*xyz[2][0] + (c1-a2)*xyz[3][0] - c2*xyz[4][0] + c2*xyz[5][0]);
264  dxdz(1,1) = 0.5*((a2-c1)*xyz[0][1] - a2*xyz[1][1] + a2*xyz[2][1] + (c1-a2)*xyz[3][1] - c2*xyz[4][1] + c2*xyz[5][1]);
265  dxdz(2,1) = 0.5*((a2-c1)*xyz[0][2] - a2*xyz[1][2] + a2*xyz[2][2] + (c1-a2)*xyz[3][2] - c2*xyz[4][2] + c2*xyz[5][2]);
266 
267  dxdz(0,2) = 0.5*(-b1*xyz[0][0] - b2*xyz[3][0] + b1*xyz[4][0] + b2*xyz[5][0]);
268  dxdz(1,2) = 0.5*(-b1*xyz[0][1] - b2*xyz[3][1] + b1*xyz[4][1] + b2*xyz[5][1]);
269  dxdz(2,2) = 0.5*(-b1*xyz[0][2] - b2*xyz[3][2] + b1*xyz[4][2] + b2*xyz[5][2]);
270 
271  dxdz.Invert();
272  ret.push_back(dxdz);
273  }
274  }
275  }
276  }
277  else
278  {
279  ASSERTL0(false,"not coded");
280  }
281 
282 
283 
284  return ret;
285 }
286 
288 {
289  SpatialDomains::GeometrySharedPtr geom = e->GetGeom();
290  StdRegions::StdExpansionSharedPtr chi = e->GetGeom()->GetXmap();
291  LibUtilities::PointsKeyVector p = chi->GetPointsKeys();
292  LibUtilities::PointsKeyVector pElem= e->GetPointsKeys();
293  SpatialDomains::GeomFactorsSharedPtr gfac = geom->GetGeomFactors();
294  const int expDim = chi->GetNumBases();
295  int nElemPts = 1;
296 
297  vector<LibUtilities::BasisKey> basisKeys;
298  bool needsInterp = false;
299 
300  for (int i = 0; i < expDim; ++i)
301  {
302  nElemPts *= pElem[i].GetNumPoints();
303  needsInterp =
304  needsInterp || pElem[i].GetNumPoints() < p[i].GetNumPoints() -1;
305  }
306 
307  if (needsInterp)
308  {
309  stringstream err;
310  err << "Interpolating from higher order geometry to lower order in "
311  << "element " << geom->GetGlobalID();
312  NEKERROR(ErrorUtil::ewarning, err.str());
313  }
314 
315  for (int i = 0; i < expDim; ++i)
316  {
317  basisKeys.push_back(
318  needsInterp ? chi->GetBasis(i)->GetBasisKey() :
319  LibUtilities::BasisKey(chi->GetBasisType(i),
320  chi->GetBasisNumModes(i),
321  pElem[i]));
322  }
323 
325  switch(chi->DetShapeType())
326  {
329  basisKeys[0], basisKeys[1]);
330  break;
333  basisKeys[0], basisKeys[1]);
334  break;
337  basisKeys[0], basisKeys[1], basisKeys[2]);
338  break;
341  basisKeys[0], basisKeys[1], basisKeys[2]);
342  break;
343  default:
344  ASSERTL0(false, "nope");
345  }
346 
347  SpatialDomains::DerivStorage deriv = gfac->GetDeriv(pElem);
348 
349  const int pts = deriv[0][0].num_elements();
350  const int nq = chiMod->GetTotPoints();
351 
352  ASSERTL0(pts == nq, "what");
353 
354  vector<DNekMat> i2rm = MappingIdealToRef(geom, chiMod);
355  Array<OneD, NekDouble> eta(nq);
356 
357  for (int k = 0; k < pts; ++k)
358  {
359  DNekMat jac (expDim, expDim, 0.0, eFULL);
360  DNekMat jacIdeal(expDim, expDim, 0.0, eFULL);
361 
362  for (int i = 0; i < expDim; ++i)
363  {
364  for (int j = 0; j < expDim; ++j)
365  {
366  jac(j,i) = deriv[i][j][k];
367  }
368  }
369 
370  jacIdeal = jac * i2rm[k];
371  NekDouble jacDet;
372 
373  if(expDim == 2)
374  {
375  jacDet = jacIdeal(0,0) * jacIdeal(1,1) - jacIdeal(0,1)*jacIdeal(1,0);
376  }
377  else if(expDim == 3)
378  {
379  jacDet = jacIdeal(0,0) * (jacIdeal(1,1)*jacIdeal(2,2) - jacIdeal(2,1)*jacIdeal(1,2)) -
380  jacIdeal(0,1) * (jacIdeal(1,0)*jacIdeal(2,2) - jacIdeal(2,0)*jacIdeal(1,2)) +
381  jacIdeal(0,2) * (jacIdeal(1,0)*jacIdeal(2,1) - jacIdeal(2,0)*jacIdeal(1,1));
382  }
383  else
384  {
385  ASSERTL0(false,"silly exp dim");
386  }
387 
388  NekDouble frob = 0.0;
389 
390  for (int i = 0; i < expDim; ++i)
391  {
392  for (int j = 0; j < expDim; ++j)
393  {
394  frob += jacIdeal(i,j) * jacIdeal(i,j);
395  }
396  }
397 
398  NekDouble sigma = 0.5*(jacDet + sqrt(jacDet*jacDet));
399  eta[k] = expDim * pow(sigma, 2.0/expDim) / frob;
400  }
401 
402  // Project onto output stuff
403  if (needsInterp && pts != 1)
404  {
405  Array<OneD, NekDouble> tmp(nElemPts);
406 
407  if (expDim == 2)
408  {
409  LibUtilities::Interp2D(p[0], p[1], eta, pElem[0], pElem[1], tmp);
410  }
411  else if(expDim == 3)
412  {
413  LibUtilities::Interp3D(p[0], p[1], p[2], eta, pElem[0], pElem[1],
414  pElem[2], tmp);
415  }
416  else
417  {
418  ASSERTL0(false,"mesh dim makes no sense");
419  }
420 
421  eta = tmp;
422  }
423 
424  if (pts == 1)
425  {
426  Vmath::Fill(nq-1, eta[0], &eta[1], 1);
427  }
428 
429  return eta;
430 }
431 
432 }
433 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:158
std::vector< PointsKey > PointsKeyVector
Definition: Points.h:220
pair< ModuleType, string > ModuleKey
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
void Fill(int n, const T alpha, T *x, const int incx)
Fill a vector with a constant value.
Definition: Vmath.cpp:46
virtual void Process()=0
STL namespace.
FieldSharedPtr m_f
Field object.
void Interp2D(const BasisKey &fbasis0, const BasisKey &fbasis1, const Array< OneD, const NekDouble > &from, const BasisKey &tbasis0, const BasisKey &tbasis1, Array< OneD, NekDouble > &to)
this function interpolates a 2D function evaluated at the quadrature points of the 2D basis...
Definition: Interp.cpp:116
double NekDouble
boost::shared_ptr< Expansion > ExpansionSharedPtr
Definition: Expansion.h:68
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:695
vector< DNekMat > MappingIdealToRef(SpatialDomains::GeometrySharedPtr geom, StdRegions::StdExpansionSharedPtr chi)
void Interp3D(const BasisKey &fbasis0, const BasisKey &fbasis1, const BasisKey &fbasis2, const Array< OneD, const NekDouble > &from, const BasisKey &tbasis0, const BasisKey &tbasis1, const BasisKey &tbasis2, Array< OneD, NekDouble > &to)
this function interpolates a 3D function evaluated at the quadrature points of the 3D basis...
Definition: Interp.cpp:186
boost::shared_ptr< GeomFactors > GeomFactorsSharedPtr
Pointer to a GeomFactors object.
Definition: GeomFactors.h:62
Array< OneD, NekDouble > GetQ(LocalRegions::ExpansionSharedPtr e)
boost::shared_ptr< StdExpansion > StdExpansionSharedPtr
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1047
boost::shared_ptr< Geometry > GeometrySharedPtr
Definition: Geometry.h:53
Describes the specification for a Basis.
Definition: Basis.h:50
boost::shared_ptr< PointGeom > PointGeomSharedPtr
Definition: Geometry.h:60
ModuleFactory & GetModuleFactory()
Abstract base class for processing modules.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215