Nektar++
Geometry2D.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: Geometry2D.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: 2D geometry information
32 //
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <iomanip>
37 
39 #include <SpatialDomains/SegGeom.h>
40 
41 #include <iomanip>
42 
43 using namespace std;
44 
45 namespace Nektar
46 {
47 namespace SpatialDomains
48 {
49 
50 Geometry2D::Geometry2D()
51 {
52 }
53 
54 Geometry2D::Geometry2D(const int coordim, CurveSharedPtr curve)
55  : Geometry(coordim), m_curve(curve)
56 {
57  ASSERTL0(m_coordim > 1,
58  "Coordinate dimension should be at least 2 for a 2D geometry");
59 }
60 
62 {
63 }
64 
66  const Array<OneD, const NekDouble> &coords,
67  const Array<OneD, const NekDouble> &ptsx,
68  const Array<OneD, const NekDouble> &ptsy,
69  Array<OneD, NekDouble> &Lcoords,
70  NekDouble &resid)
71 {
72  // Maximum iterations for convergence
73  const int MaxIterations = 51;
74  // |x-xp|^2 < EPSILON error tolerance
75  const NekDouble Tol = 1.e-8;
76  // |r,s| > LcoordDIV stop the search
77  const NekDouble LcoordDiv = 15.0;
78 
80  m_geomFactors->GetJac(m_xmap->GetPointsKeys());
81 
82  NekDouble ScaledTol = Vmath::Vsum(Jac.num_elements(), Jac, 1) /
83  ((NekDouble)Jac.num_elements());
84  ScaledTol *= Tol;
85 
86  NekDouble xmap, ymap, F1, F2;
87  NekDouble derx_1, derx_2, dery_1, dery_2, jac;
88 
89  // save intiial guess for later reference if required.
90  NekDouble init0 = Lcoords[0], init1 = Lcoords[1];
91 
92  Array<OneD, NekDouble> DxD1(ptsx.num_elements());
93  Array<OneD, NekDouble> DxD2(ptsx.num_elements());
94  Array<OneD, NekDouble> DyD1(ptsx.num_elements());
95  Array<OneD, NekDouble> DyD2(ptsx.num_elements());
96 
97  // Ideally this will be stored in m_geomfactors
98  m_xmap->PhysDeriv(ptsx, DxD1, DxD2);
99  m_xmap->PhysDeriv(ptsy, DyD1, DyD2);
100 
101  int cnt = 0;
103  Array<OneD, NekDouble> eta(2);
104 
105  F1 = F2 = 2000; // Starting value of Function
106 
107  while (cnt++ < MaxIterations)
108  {
109  // evaluate lagrange interpolant at Lcoords
110  m_xmap->LocCoordToLocCollapsed(Lcoords, eta);
111  I[0] = m_xmap->GetBasis(0)->GetI(eta);
112  I[1] = m_xmap->GetBasis(1)->GetI(eta + 1);
113 
114  // calculate the global point `corresponding to Lcoords
115  xmap = m_xmap->PhysEvaluate(I, ptsx);
116  ymap = m_xmap->PhysEvaluate(I, ptsy);
117 
118  F1 = coords[0] - xmap;
119  F2 = coords[1] - ymap;
120 
121  if (F1 * F1 + F2 * F2 < ScaledTol)
122  {
123  resid = sqrt(F1 * F1 + F2 * F2);
124  break;
125  }
126 
127  // Interpolate derivative metric at Lcoords
128  derx_1 = m_xmap->PhysEvaluate(I, DxD1);
129  derx_2 = m_xmap->PhysEvaluate(I, DxD2);
130  dery_1 = m_xmap->PhysEvaluate(I, DyD1);
131  dery_2 = m_xmap->PhysEvaluate(I, DyD2);
132 
133  jac = dery_2 * derx_1 - dery_1 * derx_2;
134 
135  // use analytical inverse of derivitives which are
136  // also similar to those of metric factors.
137  Lcoords[0] =
138  Lcoords[0] +
139  (dery_2 * (coords[0] - xmap) - derx_2 * (coords[1] - ymap)) / jac;
140 
141  Lcoords[1] =
142  Lcoords[1] +
143  (-dery_1 * (coords[0] - xmap) + derx_1 * (coords[1] - ymap)) / jac;
144 
145  if (fabs(Lcoords[0]) > LcoordDiv || fabs(Lcoords[1]) > LcoordDiv)
146  {
147  break; // lcoords have diverged so stop iteration
148  }
149  }
150 
151  resid = sqrt(F1 * F1 + F2 * F2);
152 
153  if (cnt >= MaxIterations)
154  {
155  Array<OneD, NekDouble> collCoords(2);
156  m_xmap->LocCoordToLocCollapsed(Lcoords, collCoords);
157 
158  // if coordinate is inside element dump error!
159  if ((collCoords[0] >= -1.0 && collCoords[0] <= 1.0) &&
160  (collCoords[1] >= -1.0 && collCoords[1] <= 1.0))
161  {
162  std::ostringstream ss;
163 
164  ss << "Reached MaxIterations (" << MaxIterations
165  << ") in Newton iteration ";
166  ss << "Init value (" << setprecision(4) << init0 << "," << init1
167  << ","
168  << ") ";
169  ss << "Fin value (" << Lcoords[0] << "," << Lcoords[1] << ","
170  << ") ";
171  ss << "Resid = " << resid << " Tolerance = " << sqrt(ScaledTol);
172 
173  WARNINGL1(cnt < MaxIterations, ss.str());
174  }
175  }
176 }
177 
179 {
180  return m_verts.size();
181 }
182 
184 {
185  return m_edges.size();
186 }
187 
189 {
190  ASSERTL2(i >= 0 && i < m_verts.size(), "Index out of range");
191  return m_verts[i];
192 }
193 
195 {
196  ASSERTL2(i >= 0 && i < m_edges.size(), "Index out of range");
197  return m_edges[i];
198 }
199 
201 {
202  ASSERTL2(i >= 0 && i < m_eorient.size(), "Index out of range");
203  return m_eorient[i];
204 }
205 
207 {
208  return 2;
209 }
210 
211 }
212 }
StdRegions::StdExpansionSharedPtr m_xmap
mapping containing isoparametric transformation.
Definition: Geometry.h:189
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
Base class for shape geometry information.
Definition: Geometry.h:83
GeomFactorsSharedPtr m_geomFactors
Geometric factors.
Definition: Geometry.h:185
virtual int v_GetShapeDim() const
Get the object&#39;s shape dimension.
Definition: Geometry2D.cpp:206
STL namespace.
virtual Geometry1DSharedPtr v_GetEdge(int i) const
Returns edge i of this object.
Definition: Geometry2D.cpp:194
std::vector< StdRegions::Orientation > m_eorient
Definition: Geometry2D.h:80
void NewtonIterationForLocCoord(const Array< OneD, const NekDouble > &coords, const Array< OneD, const NekDouble > &ptsx, const Array< OneD, const NekDouble > &ptsy, Array< OneD, NekDouble > &Lcoords, NekDouble &resid)
Definition: Geometry2D.cpp:65
double NekDouble
std::shared_ptr< PointGeom > PointGeomSharedPtr
Definition: Geometry.h:59
virtual StdRegions::Orientation v_GetEorient(const int i) const
Returns the orientation of edge i with respect to the ordering of edges in the standard element...
Definition: Geometry2D.cpp:200
virtual int v_GetNumVerts() const
Get the number of vertices of this object.
Definition: Geometry2D.cpp:178
#define WARNINGL1(condition, msg)
Definition: ErrorUtil.hpp:251
#define ASSERTL2(condition, msg)
Assert Level 2 – Debugging which is used FULLDEBUG compilation mode. This level assert is designed t...
Definition: ErrorUtil.hpp:274
std::shared_ptr< Curve > CurveSharedPtr
Definition: Curve.hpp:61
std::shared_ptr< Geometry1D > Geometry1DSharedPtr
Definition: Geometry.h:63
virtual PointGeomSharedPtr v_GetVertex(int i) const
Definition: Geometry2D.cpp:188
virtual int v_GetNumEdges() const
Get the number of edges of this object.
Definition: Geometry2D.cpp:183
T Vsum(int n, const T *x, const int incx)
Subtract return sum(x)
Definition: Vmath.cpp:740
int m_coordim
Coordinate dimension of this geometry object.
Definition: Geometry.h:183