Nektar++
ProcessJac.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessJac.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: Calculate Jacobians of elements.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
36 #include "ProcessJac.h"
37 
38 using namespace std;
39 using namespace Nektar::NekMeshUtils;
40 
41 namespace Nektar
42 {
43 namespace Utilities
44 {
45 
46 ModuleKey ProcessJac::className = GetModuleFactory().RegisterCreatorFunction(
47  ModuleKey(eProcessModule, "jac"),
48  ProcessJac::create,
49  "Process elements based on values of Jacobian.");
50 
51 ProcessJac::ProcessJac(MeshSharedPtr m) : ProcessModule(m)
52 {
53  m_config["extract"] =
54  ConfigOption(false, "0.0", "Extract non-valid elements from mesh.");
55  m_config["list"] = ConfigOption(
56  true, "0", "Print list of elements having negative Jacobian.");
57 }
58 
60 {
61 }
62 
64 {
65  if (m_mesh->m_verbose)
66  {
67  cout << "ProcessJac: Calculating Jacobians... " << endl;
68  }
69 
70  bool extract = m_config["extract"].beenSet;
71  bool printList = m_config["list"].as<bool>();
72  NekDouble thres = m_config["extract"].as<NekDouble>();
73 
74  vector<ElementSharedPtr> el = m_mesh->m_element[m_mesh->m_expDim];
75 
76  if (extract)
77  {
78  m_mesh->m_element[m_mesh->m_expDim].clear();
79  }
80 
81  if (printList)
82  {
83  cout << "Elements with negative Jacobian:" << endl;
84  }
85 
86  int nNeg = 0;
87 
88  Array<OneD, int> bin(20, 0);
89 
90  // Iterate over list of elements of expansion dimension.
91  for (int i = 0; i < el.size(); ++i)
92  {
93  // Create elemental geometry.
95  el[i]->GetGeom(m_mesh->m_spaceDim);
96 
97  // Generate geometric factors.
98  SpatialDomains::GeomFactorsSharedPtr gfac = geom->GetGeomFactors();
99 
100  LibUtilities::PointsKeyVector p = geom->GetXmap()->GetPointsKeys();
101  SpatialDomains::DerivStorage deriv = gfac->GetDeriv(p);
102  const int pts = deriv[0][0].num_elements();
103  Array<OneD,NekDouble> jc(pts);
104  for (int k = 0; k < pts; ++k)
105  {
106  DNekMat jac(m_mesh->m_expDim, m_mesh->m_expDim, 0.0, eFULL);
107 
108  for (int l = 0; l < m_mesh->m_expDim; ++l)
109  {
110  for (int j = 0; j < m_mesh->m_expDim; ++j)
111  {
112  jac(j,l) = deriv[l][j][k];
113  }
114  }
115 
116  if(m_mesh->m_expDim == 2)
117  {
118  jc[k] = jac(0,0) * jac(1,1) - jac(0,1)*jac(1,0);
119  }
120  else if(m_mesh->m_expDim == 3)
121  {
122  jc[k] = jac(0,0) * (jac(1,1)*jac(2,2) - jac(2,1)*jac(1,2)) -
123  jac(0,1) * (jac(1,0)*jac(2,2) - jac(2,0)*jac(1,2)) +
124  jac(0,2) * (jac(1,0)*jac(2,1) - jac(2,0)*jac(1,1));
125  }
126  }
127 
128  NekDouble scaledJac = Vmath::Vmin(jc.num_elements(),jc,1) /
129  Vmath::Vmax(jc.num_elements(),jc,1);
130 
131  bool valid = gfac->IsValid();
132 
133  if (extract && (scaledJac < thres || !valid))
134  {
135  m_mesh->m_element[m_mesh->m_expDim].push_back(el[i]);
136  }
137 
138  // Get the Jacobian and, if it is negative, print a warning
139  // message.
140  if (!valid)
141  {
142  nNeg++;
143 
144  if (printList)
145  {
146  cout << " - " << el[i]->GetId() << " ("
147  << LibUtilities::ShapeTypeMap[el[i]->GetConf().m_e] << ")"
148  << " " << scaledJac
149  << endl;
150  }
151  }
152  }
153 
154  if (extract)
155  {
156  m_mesh->m_element[m_mesh->m_expDim - 1].clear();
157  ProcessVertices();
158  ProcessEdges();
159  ProcessFaces();
160  ProcessElements();
162  }
163 
164  if (printList || m_mesh->m_verbose)
165  {
166  cout << "Total negative Jacobians: " << nNeg << endl;
167  }
168  else if (nNeg > 0)
169  {
170  cout << "WARNING: Detected " << nNeg << " element"
171  << (nNeg == 1 ? "" : "s") << " with negative Jacobian." << endl;
172  }
173 }
174 }
175 }
std::vector< PointsKey > PointsKeyVector
Definition: Points.h:246
virtual void Process()
Write mesh to output file.
Definition: ProcessJac.cpp:63
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
STL namespace.
std::shared_ptr< Mesh > MeshSharedPtr
Shared pointer to a mesh.
Definition: Mesh.h:156
const char *const ShapeTypeMap[]
Definition: ShapeType.hpp:67
std::pair< ModuleType, std::string > ModuleKey
virtual NEKMESHUTILS_EXPORT void ProcessFaces(bool ReprocessFaces=true)
Extract element faces.
virtual NEKMESHUTILS_EXPORT void ProcessElements()
Generate element IDs.
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.
def extract(self, check_equality=False)
Definition: pycml.py:2657
Abstract base class for processing modules.
virtual NEKMESHUTILS_EXPORT void ProcessVertices()
Extract element vertices.
virtual NEKMESHUTILS_EXPORT void ProcessEdges(bool ReprocessEdges=true)
Extract element edges.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
std::pair< ModuleType, std::string > ModuleKey
virtual NEKMESHUTILS_EXPORT void ProcessComposites()
Generate composites.
ModuleFactory & GetModuleFactory()