Nektar++
Loading...
Searching...
No Matches
SegGeom.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: SegGeom.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:
32//
33//
34////////////////////////////////////////////////////////////////////////////////
35
39
40#include <LibUtilities/Foundations/ManagerAccess.h> // for PointsManager, etc
43
44namespace Nektar
45{
46// Forward declarations for allocation pools that are defined within
47// MeshGraph.cpp compilation unit.
48template <>
49PoolAllocator<SpatialDomains::PointGeom>
51template <>
54} // namespace Nektar
55
57{
58
68
69SegGeom::SegGeom(int id, int coordim, std::array<PointGeom *, kNverts> vertex,
70 Curve *curve)
71 : Geometry1D(coordim)
72{
74 m_globalID = id;
76 m_curve = curve;
77 m_verts = vertex;
78}
79
81{
82 // From Geometry class
84
85 // info from EdgeComponent class
87 m_xmap = in.m_xmap;
88 SetUpCoeffs(m_xmap->GetNcoeffs());
89
90 // info from SegGeom class
92 m_verts[0] = in.m_verts[0];
93 m_verts[1] = in.m_verts[1];
94
95 m_state = in.m_state;
96}
97
116
117/**
118 * \brief Generate a one dimensional space segment geometry where the vert[0]
119 * has the same x value and vert[1] is set to vert[0] plus the length of the
120 * original segment
121 **/
123{
125
126 // info about numbering
127 returnval->m_globalID = m_globalID;
128
129 // geometric information.
130 returnval->m_coordim = 1;
131 NekDouble x0 = (*m_verts[0])[0];
133 1, m_verts[0]->GetGlobalID(), x0, 0.0, 0.0);
134 vert0->SetGlobalID(vert0->GetGlobalID());
135 returnval->m_verts[0] = vert0.get();
136 holder.m_pointVec.push_back(std::move(vert0));
137
138 // Get information to calculate length.
140 m_xmap->GetBase();
142 v.push_back(base[0]->GetPointsKey());
143
144 const Array<OneD, const NekDouble> jac = v_GenGeomFactors(v)->GetJac();
145
146 NekDouble len = 0.0;
147 if (jac.size() == 1)
148 {
149 len = jac[0] * 2.0;
150 }
151 else
152 {
153 Array<OneD, const NekDouble> w0 = base[0]->GetW();
154 len = 0.0;
155
156 for (int i = 0; i < jac.size(); ++i)
157 {
158 len += jac[i] * w0[i];
159 }
160 }
161 // Set up second vertex.
163 1, m_verts[1]->GetGlobalID(), x0 + len, 0.0, 0.0);
164 vert1->SetGlobalID(vert1->GetGlobalID());
165
166 returnval->m_verts[1] = vert1.get();
167 holder.m_pointVec.push_back(std::move(vert1));
168
169 // at present just use previous m_xmap[0];
170 returnval->m_xmap = m_xmap;
171 returnval->SetUpCoeffs(m_xmap->GetNcoeffs());
172 returnval->m_state = eNotFilled;
173
174 return returnval;
175}
176
181
183 const Array<OneD, const NekDouble> &Lcoord)
184{
185 if (m_state != ePtsFilled)
186 {
187 NEKERROR(ErrorUtil::ewarning, "Geometry is not in physical space");
188 }
189
190 Array<OneD, NekDouble> tmp(m_xmap->GetTotPoints());
191 m_xmap->BwdTrans(m_coeffs[i], tmp);
192
193 return m_xmap->PhysEvaluate(Lcoord, tmp);
194}
195
196/**
197 * @brief Get the orientation of @p edge1.
198 *
199 * If @p edge1 is connected to @p edge2 in the same direction as the points
200 * comprising @p edge1 then it is forward, otherwise it is backward.
201 *
202 * For example, assume @p edge1 is comprised of points 1 and 2, and @p edge2 is
203 * comprised of points 2 and 3, then @p edge1 is forward.
204 *
205 * If @p edge1 is comprised of points 2 and 1 and @p edge2 is comprised of
206 * points 3 and 2, then @p edge1 is backward.
207 *
208 * Since both edges are passed, it does not need any information from the
209 * EdgeComponent instance.
210 */
212 const SegGeom &edge2)
213{
215
216 if ((*edge1.GetVertex(0) == *edge2.GetVertex(0)) ||
217 (*edge1.GetVertex(0) == *edge2.GetVertex(1)))
218 {
219 // Backward direction. Vertex 0 is connected to edge 2.
220 returnval = StdRegions::eBackwards;
221 }
222 else if ((*edge1.GetVertex(1) != *edge2.GetVertex(0)) &&
223 (*edge1.GetVertex(1) != *edge2.GetVertex(1)))
224 {
225 // Not forward either, then we have a problem.
226 std::ostringstream errstrm;
227 errstrm << "Connected edges do not share a vertex. Edges ";
228 errstrm << edge1.GetGlobalID() << ", " << edge2.GetGlobalID();
229 NEKERROR(ErrorUtil::efatal, errstrm.str());
230 }
231
232 return returnval;
233}
234
236{
237 if (!m_setupState)
238 {
240 }
242
244
245 if (m_xmap->GetBasisNumModes(0) != 2)
246 {
247 gType = eDeformed;
248 }
249
250 return gType;
251}
252
260
262{
263 if (m_state != ePtsFilled)
264 {
265 int i;
266
267 if (m_coordim > 0 && m_curve)
268 {
269 int npts = m_curve->m_points.size();
270 LibUtilities::PointsKey pkey(npts + 1,
272 Array<OneD, NekDouble> tmp(npts);
273
274 if (m_verts[0]->dist(*(m_curve->m_points[0])) >
276 {
277 std::string err =
278 "Vertex 0 is separated from first point by more than ";
279 std::stringstream strstrm;
280 strstrm << NekConstants::kVertexTheSameDouble << " in edge "
281 << m_globalID;
282 err += strstrm.str();
283 NEKERROR(ErrorUtil::ewarning, err.c_str());
284 }
285
286 if (m_verts[1]->dist(*(m_curve->m_points[npts - 1])) >
288 {
289 std::string err =
290 "Vertex 1 is separated from last point by more than ";
291 std::stringstream strstrm;
292 strstrm << NekConstants::kVertexTheSameDouble << " in edge "
293 << m_globalID;
294 err += strstrm.str();
295 NEKERROR(ErrorUtil::ewarning, err.c_str());
296 }
297
300 LibUtilities::PointsManager()[fkey]->GetI(pkey);
301 NekVector<NekDouble> out(npts + 1);
302
303 for (int i = 0; i < m_coordim; ++i)
304 {
305 // Load up coordinate values into tmp
306 for (int j = 0; j < npts; ++j)
307 {
308 tmp[j] = (m_curve->m_points[j]->GetPtr())[i];
309 }
310
311 // Interpolate to GLL points
312 NekVector<NekDouble> in(npts, tmp, eWrapper);
313 out = (*I0) * in;
314
315 m_xmap->FwdTrans(out.GetPtr(), m_coeffs[i]);
316 }
317 }
318
319 for (i = 0; i < m_coordim; ++i)
320 {
321 m_coeffs[i][0] = (*m_verts[0])[i];
322 m_coeffs[i][1] = (*m_verts[1])[i];
323 }
324
326 }
327}
328
329void SegGeom::v_Reset(CurveMap &curvedEdges, CurveMap &curvedFaces)
330{
331 Geometry::v_Reset(curvedEdges, curvedFaces);
332 CurveMap::iterator it = curvedEdges.find(m_globalID);
333
334 if (it != curvedEdges.end())
335 {
336 m_curve = it->second.get();
337 }
338
339 SetUpXmap();
340 SetUpCoeffs(m_xmap->GetNcoeffs());
341}
342
344{
345 if (!m_setupState)
346 {
347 SetUpXmap();
348 SetUpCoeffs(m_xmap->GetNcoeffs());
349 m_setupState = true;
350 }
351}
352
354{
355 PointGeom *returnval = nullptr;
356
357 if (i >= 0 && i < kNverts)
358 {
359 returnval = m_verts[i];
360 }
361
362 return returnval;
363}
364
366{
367 return kNverts;
368}
369
372{
373 GeomType Gtype = CalcGeomType();
374 if (Gtype == eRegular)
375 {
376 xiOut = Array<OneD, NekDouble>(1, 0.0);
377
378 GetLocCoords(xs, xiOut);
379 ClampLocCoords(xiOut);
380
382 NekDouble tmp = 0;
383 for (int i = 0; i < m_coordim; ++i)
384 {
385 gloCoord[i] = GetCoord(i, xiOut);
386 tmp += (xs[i] - gloCoord[i]) * (xs[i] - gloCoord[i]);
387 }
388
389 return sqrt(tmp);
390 }
391 // If deformed edge then the inverse mapping is non-linear so need to
392 // numerically solve for the local coordinate
393 else if (Gtype == eDeformed)
394 {
395 Array<OneD, NekDouble> xi(1, 0.0);
396
397 // Armijo constants:
398 // https://en.wikipedia.org/wiki/Backtracking_line_search
399 const NekDouble c1 = 1e-4, c2 = 0.9;
400
401 int dim = GetCoordim();
402 int nq = m_xmap->GetTotPoints();
403
404 Array<OneD, Array<OneD, NekDouble>> x(dim), xder(dim), xder2(dim);
405 // Get x,y,z phys values from coefficients
406 for (int i = 0; i < dim; ++i)
407 {
408 x[i] = Array<OneD, NekDouble>(nq);
409 xder[i] = Array<OneD, NekDouble>(nq);
410 xder2[i] = Array<OneD, NekDouble>(nq);
411
412 m_xmap->BwdTrans(m_coeffs[i], x[i]);
413 }
414
415 NekDouble fx_prev = std::numeric_limits<NekDouble>::max();
416
417 // Minimisation loop (Quasi-newton method)
418 for (int i = 0; i < NekConstants::kNewtonIterations; ++i)
419 {
420 // Compute the objective function, f(x_k) and its derivatives
424 NekDouble fx = 0, fxp = 0, fxp2 = 0, xcDiff = 0;
425 for (int j = 0; j < dim; ++j)
426 {
427 xc[j] = m_xmap->PhysEvaluate(xi, x[j], xc_der[j], xc_der2[j]);
428
429 xcDiff = xc[j] - xs[j];
430 // Objective function is the distance to the search point
431 fx += xcDiff * xcDiff;
432 fxp += xc_der[j][0] * xcDiff;
433 fxp2 += xc_der2[j][0] * xcDiff + xc_der[j][0] * xc_der[j][0];
434 }
435
436 fxp *= 2;
437 fxp2 *= 2;
438
439 // Check for convergence
440 if (std::abs(fx - fx_prev) < 1e-12)
441 {
442 fx_prev = fx;
443 break;
444 }
445 else
446 {
447 fx_prev = fx;
448 }
449
450 NekDouble gamma = 1.0;
451 bool conv = false;
452
453 // Search direction: Newton's method
454 NekDouble pk = -fxp / fxp2;
455
456 // Perform backtracking line search
457 while (gamma > 1e-10)
458 {
459 Array<OneD, NekDouble> xi_pk(1);
460 xi_pk[0] = xi[0] + pk * gamma;
461
462 if (xi_pk[0] < -1.0 || xi_pk[0] > 1.0)
463 {
464 gamma /= 2.0;
465 continue;
466 }
467
468 Array<OneD, NekDouble> xc_pk(dim);
470 NekDouble fx_pk = 0, fxp_pk = 0, xc_pkDiff = 0;
471 for (int j = 0; j < dim; ++j)
472 {
473 xc_pk[j] = m_xmap->PhysEvaluate(xi_pk, x[j], xc_der_pk[j]);
474
475 xc_pkDiff = xc_pk[j] - xs[j];
476 fx_pk += xc_pkDiff * xc_pkDiff;
477 fxp_pk += xc_der_pk[j][0] * xc_pkDiff;
478 }
479
480 fxp_pk *= 2;
481
482 // Check Wolfe conditions using Armijo constants
483 // https://en.wikipedia.org/wiki/Wolfe_conditions
484 if ((fx_pk - (fx + c1 * gamma * pk * fxp)) <
485 std::numeric_limits<NekDouble>::epsilon() &&
486 (-pk * fxp_pk + c2 * pk * fxp) <
487 std::numeric_limits<NekDouble>::epsilon())
488 {
489 conv = true;
490 break;
491 }
492
493 gamma /= 2.0;
494 }
495
496 if (!conv)
497 {
498 break;
499 }
500
501 xi[0] += gamma * pk;
502 }
503
504 xiOut = xi;
505 return sqrt(fx_prev);
506 }
507 else
508 {
509 NEKERROR(ErrorUtil::efatal, "Geometry type unknown");
510 }
511
512 return -1.0;
513}
514
515} // namespace Nektar::SpatialDomains
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Describes the specification for a Basis.
Definition Basis.h:45
Defines a specification for a set of points.
Definition Points.h:50
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
Array< OneD, DataType > & GetPtr()
Generic object pool allocator/deallocator.
static std::unique_ptr< DataType, UniquePtrDeleter > AllocateUniquePtr(const Args &...args)
std::vector< PointGeomUniquePtr > m_pointVec
Definition SegGeom.h:55
1D geometry information
Definition Geometry1D.h:49
NekDouble GetCoord(const int i, const Array< OneD, const NekDouble > &Lcoord)
Given local collapsed coordinate Lcoord, return the value of physical coordinate in direction i.
Definition Geometry.h:559
bool m_setupState
Wether or not the setup routines have been run.
Definition Geometry.h:190
GeomState m_state
Enumeration to dictate whether coefficients are filled.
Definition Geometry.h:188
void SetUpCoeffs(const int nCoeffs)
Initialise the Geometry::m_coeffs array.
Definition Geometry.h:694
NekDouble GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords)
Determine the local collapsed coordinates that correspond to a given Cartesian coordinate for this ge...
Definition Geometry.h:549
virtual void v_Reset(CurveMap &curvedEdges, CurveMap &curvedFaces)
Reset this geometry object: unset the current state, zero Geometry::m_coeffs and remove allocated Geo...
Definition Geometry.cpp:366
bool ClampLocCoords(Array< OneD, NekDouble > &locCoord, NekDouble tol=std::numeric_limits< NekDouble >::epsilon())
Clamp local coords to be within standard regions [-1, 1]^dim.
Definition Geometry.cpp:525
int GetGlobalID(void) const
Get the ID of this object.
Definition Geometry.h:314
PointGeom * GetVertex(int i) const
Returns vertex i of this object.
Definition Geometry.h:353
int GetCoordim() const
Return the coordinate dimension of this object (i.e. the dimension of the space in which this object ...
Definition Geometry.h:277
std::vector< Array< OneD, NekDouble > > m_coeffs
Array containing expansion coefficients of m_xmap.
Definition Geometry.h:196
LibUtilities::ShapeType m_shapeType
Type of shape.
Definition Geometry.h:192
StdRegions::StdExpansionSharedPtr m_xmap
mapping containing isoparametric transformation.
Definition Geometry.h:186
int m_coordim
Coordinate dimension of this geometry object.
Definition Geometry.h:184
GeomType v_CalcGeomType() override
Definition SegGeom.cpp:235
PointGeom * v_GetVertex(const int i) const override
Returns vertex i of this object.
Definition SegGeom.cpp:353
std::array< SpatialDomains::PointGeom *, kNverts > m_verts
Definition SegGeom.h:90
int v_GetNumVerts() const override
Get the number of vertices of this object.
Definition SegGeom.cpp:365
virtual LibUtilities::ShapeType v_GetShapeType() const
Definition SegGeom.cpp:177
void v_FillGeom() override
Populate the coordinate mapping Geometry::m_coeffs information from any children geometry elements.
Definition SegGeom.cpp:261
GeomFactorsUniquePtr v_GenGeomFactors(LibUtilities::PointsKeyVector &keyTgt) override
Used by Expansion to generate associated GeomFactors.
Definition SegGeom.cpp:253
Curve * m_curve
Boolean indicating whether object owns the data.
Definition SegGeom.h:109
static StdRegions::Orientation GetEdgeOrientation(const SegGeom &edge1, const SegGeom &edge2)
Get the orientation of edge1.
Definition SegGeom.cpp:211
NekDouble v_FindDistance(const Array< OneD, const NekDouble > &xs, Array< OneD, NekDouble > &xi) override
Definition SegGeom.cpp:370
static const int kNverts
Definition SegGeom.h:62
NekDouble v_GetCoord(const int i, const Array< OneD, const NekDouble > &Lcoord) override
Given local collapsed coordinate Lcoord, return the value of physical coordinate in direction i.
Definition SegGeom.cpp:182
SegGeomUniquePtr GenerateOneSpaceDimGeom(EntityHolder1D &holder)
Generate a one dimensional space segment geometry where the vert[0] has the same x value and vert[1] ...
Definition SegGeom.cpp:122
void v_Reset(CurveMap &curvedEdges, CurveMap &curvedFaces) override
Reset this geometry object: unset the current state, zero Geometry::m_coeffs and remove allocated Geo...
Definition SegGeom.cpp:329
A simple factory for Xmap objects that is based on the element type, the basis and quadrature selecti...
PointsManagerT & PointsManager(void)
std::vector< PointsKey > PointsKeyVector
Definition Points.h:313
@ eGaussLobattoLegendre
1D Gauss-Lobatto-Legendre quadrature points
Definition PointsType.h:51
@ eModified_A
Principle Modified Functions .
Definition BasisType.h:48
static const NekDouble kVertexTheSameDouble
static const unsigned int kNewtonIterations
XmapFactory< StdRegions::StdSegExp, 1 > & GetStdSegFactory()
Definition SegGeom.cpp:59
unique_ptr_objpool< GeomFactors > GeomFactorsUniquePtr
Definition Geometry.h:62
std::map< int, CurveUniquePtr > CurveMap
Definition Geometry.h:71
GeomType
Indicates the type of element geometry.
@ eRegular
Geometry is straight-sided with constant geometric factors.
@ eDeformed
Geometry is curved or has non-constant factors.
@ eNotFilled
Geometric information has not been generated.
@ ePtsFilled
Geometric information has been generated.
unique_ptr_objpool< SegGeom > SegGeomUniquePtr
Definition MeshGraph.h:102
unique_ptr_objpool< PointGeom > PointGeomUniquePtr
Definition MeshGraph.h:99
boost::fast_pool_allocator< DataType, boost::default_user_allocator_new_delete, boost::details::pool::null_mutex > PoolAllocator
std::shared_ptr< DNekMat > DNekMatSharedPtr
scalarT< T > sqrt(scalarT< T > in)
Definition scalar.hpp:290
LibUtilities::PointsType m_ptype
Points distribution of this curve.
Definition Curve.hpp:57
std::vector< PointGeom * > m_points
Points along the curve.
Definition Curve.hpp:53