Nektar++
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | List of all members
Nektar::SpatialDomains::GeometryLocator1D Class Referencefinal

Inverse mapping for a segment. More...

#include <GeometryLocator.h>

Inheritance diagram for Nektar::SpatialDomains::GeometryLocator1D:
[legend]

Public Member Functions

 GeometryLocator (Geometry *geom)
 
- Public Member Functions inherited from Nektar::SpatialDomains::GeometryLocator
 GeometryLocator (Geometry *geom)
 
virtual ~GeometryLocator ()=default
 
void Build ()
 Populate the cache. Called by Create() once the concrete type exists, since it dispatches virtually.
 
Geometry * GetGeometry () const
 
std::array< NekDouble, 6 > GetBoundingBox ()
 Generates the bounding box for the element.
 
void ClearBoundingBox ()
 
bool ContainsPoint (const Array< OneD, const NekDouble > &gloCoord, NekDouble tol=0.0)
 Determine whether the element contains a Cartesian coordinate.
 
bool ContainsPoint (const Array< OneD, const NekDouble > &gloCoord, Array< OneD, NekDouble > &locCoord, NekDouble tol)
 Determine whether the element contains a Cartesian coordinate.
 
bool ContainsPoint (const Array< OneD, const NekDouble > &gloCoord, Array< OneD, NekDouble > &locCoord, NekDouble tol, NekDouble &dist)
 
NekDouble GetLocCoords (const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords)
 Determine the local collapsed coordinates corresponding to a given Cartesian coordinate.
 
NekDouble FindDistance (const Array< OneD, const NekDouble > &xs, Array< OneD, NekDouble > &xi)
 
int PreliminaryCheck (const Array< OneD, const NekDouble > &gloCoord, NekDouble &edgeDist)
 A fast and robust check if a given global coord is outside of a deformed element. For regular elements, this check is unnecessary.
 
bool MinMaxCheck (const Array< OneD, const NekDouble > &gloCoord)
 Check if given global coord is within the BoundingBox of the element.
 

Protected Member Functions

NekDouble v_GetLocCoords (const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords) override
 
NekDouble v_FindDistance (const Array< OneD, const NekDouble > &xs, Array< OneD, NekDouble > &xi) override
 
- Protected Member Functions inherited from Nektar::SpatialDomains::GeometryLocator
virtual int v_AllLeftCheck (const Array< OneD, const NekDouble > &gloCoord, NekDouble &edgeDist)
 
virtual void v_CalculateInverseIsoParam ()
 
virtual void v_PreSolveStraightEdge ()
 

Additional Inherited Members

- Static Public Member Functions inherited from Nektar::SpatialDomains::GeometryLocator
static std::unique_ptr< GeometryLocator > Create (Geometry *geom)
 Build the locator appropriate to geom's dimension, with its cache already populated.
 
static bool ClampLocCoords (Array< OneD, NekDouble > &locCoord, int dim, NekDouble tol=std::numeric_limits< NekDouble >::epsilon())
 Clamp local coordinates into the reference element \([-1-tol,1+tol]^{dim}\), returning whether anything was clamped. Depends only on the reference element, so it needs no locator.
 
- Protected Attributes inherited from Nektar::SpatialDomains::GeometryLocator
Geometry * m_geom
 The geometry being searched. Not owned.
 
IsoParam m_iso
 The shape's isoparametric coefficients. The straight-edged 2D solve rewrites them into a factored form, so the locator keeps its own copy rather than sharing the geometry's.
 
int m_straightEdge = 0
 Non-zero if every \(\chi\) basis is linear. Bits 1 and 2 are used by the 2D straight-edged solve to record which axes were swapped.
 
std::array< std::array< NekDouble, kMaxGeomDim >, kMaxGeomDim > m_invIsoParam {}
 Inverse of the linear part of m_iso, for regular elements.
 
bool m_newtonConverged = false
 Whether the Newton iteration of the last GetLocCoords call converged, i.e. whether its local coordinates map to the point. Set only by elements located by Newton iteration; read by ContainsPoint.
 
bool m_hasBoundingBox = false
 Element bounding box as (xmin, ymin, zmin, xmax, ymax, zmax).
 
std::array< NekDouble, 6 > m_boundingBox {}
 
bool m_edgeNormalBuilt = false
 2D only: inward edge normals used to reject points outside straight-sided elements. A curved edge gets no normal, so m_hasEdgeNormal records which entries are usable.
 
std::array< bool, kMaxGeomVerts > m_hasEdgeNormal {}
 
std::array< std::array< NekDouble, 2 >, kMaxGeomVerts > m_edgeNormal {}
 

Detailed Description

Inverse mapping for a segment.

Definition at line 199 of file GeometryLocator.h.

Member Function Documentation

◆ GeometryLocator()

Nektar::SpatialDomains::GeometryLocator::GeometryLocator ( Geometry *  geom)
explicit

Definition at line 105 of file GeometryLocator.cpp.

50 : m_geom(geom)
51{
52 ASSERTL0(m_geom != nullptr, "A locator needs a geometry to search.");
53}
#define ASSERTL0(condition, msg)
Geometry * m_geom
The geometry being searched. Not owned.

◆ v_FindDistance()

NekDouble Nektar::SpatialDomains::GeometryLocator1D::v_FindDistance ( const Array< OneD, const NekDouble > &  xs,
Array< OneD, NekDouble > &  xi 
)
overrideprotectedvirtual

Reimplemented from Nektar::SpatialDomains::GeometryLocator.

Definition at line 514 of file GeometryLocator.cpp.

516{
517 GeomType Gtype = m_geom->CalcGeomType();
518 if (Gtype == eRegular)
519 {
520 xiOut = Array<OneD, NekDouble>(1, 0.0);
521
522 GetLocCoords(xs, xiOut);
524
525 Array<OneD, NekDouble> gloCoord(m_geom->GetCoordim());
526 NekDouble tmp = 0;
527 for (int i = 0; i < m_geom->GetCoordim(); ++i)
528 {
529 gloCoord[i] = m_geom->GetCoord(i, xiOut);
530 tmp += (xs[i] - gloCoord[i]) * (xs[i] - gloCoord[i]);
531 }
532
533 return sqrt(tmp);
534 }
535 // If deformed edge then the inverse mapping is non-linear so need to
536 // numerically solve for the local coordinate
537 else if (Gtype == eDeformed)
538 {
539 Array<OneD, NekDouble> xi(1, 0.0);
540
541 // Armijo constants:
542 // https://en.wikipedia.org/wiki/Backtracking_line_search
543 const NekDouble c1 = 1e-4, c2 = 0.9;
544
545 int dim = m_geom->GetCoordim();
546 int nq = m_geom->GetXmap()->GetTotPoints();
547
548 Array<OneD, Array<OneD, NekDouble>> x(dim), xder(dim), xder2(dim);
549 // Get x,y,z phys values from coefficients
550 for (int i = 0; i < dim; ++i)
551 {
552 x[i] = Array<OneD, NekDouble>(nq);
553 xder[i] = Array<OneD, NekDouble>(nq);
554 xder2[i] = Array<OneD, NekDouble>(nq);
555
556 m_geom->GetXmap()->BwdTrans(m_geom->GetCoeffs(i), x[i]);
557 }
558
559 NekDouble fx_prev = std::numeric_limits<NekDouble>::max();
560
561 // Minimisation loop (Quasi-newton method)
562 for (int i = 0; i < NekConstants::kNewtonIterations; ++i)
563 {
564 // Compute the objective function, f(x_k) and its derivatives
565 Array<OneD, NekDouble> xc(dim);
566 Array<OneD, std::array<NekDouble, 3>> xc_der(dim);
567 Array<OneD, std::array<NekDouble, 6>> xc_der2(dim);
568 NekDouble fx = 0, fxp = 0, fxp2 = 0, xcDiff = 0;
569 for (int j = 0; j < dim; ++j)
570 {
571 xc[j] = m_geom->GetXmap()->PhysEvaluate(xi, x[j], xc_der[j],
572 xc_der2[j]);
573
574 xcDiff = xc[j] - xs[j];
575 // Objective function is the distance to the search point
576 fx += xcDiff * xcDiff;
577 fxp += xc_der[j][0] * xcDiff;
578 fxp2 += xc_der2[j][0] * xcDiff + xc_der[j][0] * xc_der[j][0];
579 }
580
581 fxp *= 2;
582 fxp2 *= 2;
583
584 // Check for convergence
585 if (std::abs(fx - fx_prev) < 1e-12)
586 {
587 fx_prev = fx;
588 break;
589 }
590 else
591 {
592 fx_prev = fx;
593 }
594
595 NekDouble gamma = 1.0;
596 bool conv = false;
597
598 // Search direction: Newton's method
599 NekDouble pk = -fxp / fxp2;
600
601 // Perform backtracking line search
602 while (gamma > 1e-10)
603 {
604 Array<OneD, NekDouble> xi_pk(1);
605 xi_pk[0] = xi[0] + pk * gamma;
606
607 if (xi_pk[0] < -1.0 || xi_pk[0] > 1.0)
608 {
609 gamma /= 2.0;
610 continue;
611 }
612
613 Array<OneD, NekDouble> xc_pk(dim);
614 Array<OneD, std::array<NekDouble, 3>> xc_der_pk(dim);
615 NekDouble fx_pk = 0, fxp_pk = 0, xc_pkDiff = 0;
616 for (int j = 0; j < dim; ++j)
617 {
618 xc_pk[j] = m_geom->GetXmap()->PhysEvaluate(xi_pk, x[j],
619 xc_der_pk[j]);
620
621 xc_pkDiff = xc_pk[j] - xs[j];
622 fx_pk += xc_pkDiff * xc_pkDiff;
623 fxp_pk += xc_der_pk[j][0] * xc_pkDiff;
624 }
625
626 fxp_pk *= 2;
627
628 // Check Wolfe conditions using Armijo constants
629 // https://en.wikipedia.org/wiki/Wolfe_conditions
630 if ((fx_pk - (fx + c1 * gamma * pk * fxp)) <
631 std::numeric_limits<NekDouble>::epsilon() &&
632 (-pk * fxp_pk + c2 * pk * fxp) <
633 std::numeric_limits<NekDouble>::epsilon())
634 {
635 conv = true;
636 break;
637 }
638
639 gamma /= 2.0;
640 }
641
642 if (!conv)
643 {
644 break;
645 }
646
647 xi[0] += gamma * pk;
648 }
649
650 xiOut = xi;
651 return sqrt(fx_prev);
652 }
653 else
654 {
655 NEKERROR(ErrorUtil::efatal, "Geometry type unknown");
656 }
657
658 return -1.0;
659}
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
NekDouble GetCoord(const int i, const Array< OneD, const NekDouble > &Lcoord)
Evaluate the mapping: physical coordinate in direction i at the local collapsed coordinate Lcoord.
Definition Geometry.h:615
int GetShapeDim() const
Dimension of the shape itself, which may be less than GetCoordim().
Definition Geometry.h:557
GeomType CalcGeomType()
Classify whether this shape is regular or deformed.
Definition Geometry.cpp:151
int GetCoordim() const
Dimension of the space this object is embedded in.
Definition Geometry.h:447
StdRegions::StdExpansion * GetXmap() const
The expansion holding the mapping from the standard element to this one. Shared between geometries a...
Definition Geometry.h:571
const Array< OneD, const NekDouble > & GetCoeffs(const int i)
The coefficients of in coordinate direction i, filling them first if that has not happened yet.
Definition Geometry.h:576
NekDouble GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords)
Determine the local collapsed coordinates corresponding to a given Cartesian coordinate.
static bool ClampLocCoords(Array< OneD, NekDouble > &locCoord, int dim, NekDouble tol=std::numeric_limits< NekDouble >::epsilon())
Clamp local coordinates into the reference element , returning whether anything was clamped....
int GetTotPoints() const
This function returns the total number of quadrature points used in the element.
NekDouble PhysEvaluate(const Array< OneD, const NekDouble > &coords, const Array< OneD, const NekDouble > &physvals)
This function evaluates the expansion at a single (arbitrary) point of the domain.
void BwdTrans(const Array< OneD, const NekDouble > &inarray, Array< OneD, NekDouble > &outarray)
This function performs the Backward transformation from coefficient space to physical space.
static const unsigned int kNewtonIterations
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.
scalarT< T > sqrt(scalarT< T > in)
Definition scalar.hpp:289

References Nektar::StdRegions::StdExpansion::BwdTrans(), Nektar::SpatialDomains::Geometry::CalcGeomType(), Nektar::SpatialDomains::GeometryLocator::ClampLocCoords(), Nektar::SpatialDomains::eDeformed, Nektar::ErrorUtil::efatal, Nektar::SpatialDomains::eRegular, Nektar::SpatialDomains::Geometry::GetCoeffs(), Nektar::SpatialDomains::Geometry::GetCoord(), Nektar::SpatialDomains::Geometry::GetCoordim(), Nektar::SpatialDomains::GeometryLocator::GetLocCoords(), Nektar::SpatialDomains::Geometry::GetShapeDim(), Nektar::StdRegions::StdExpansion::GetTotPoints(), Nektar::SpatialDomains::Geometry::GetXmap(), Nektar::NekConstants::kNewtonIterations, Nektar::SpatialDomains::GeometryLocator::m_geom, NEKERROR, Nektar::StdRegions::StdExpansion::PhysEvaluate(), and tinysimd::sqrt().

◆ v_GetLocCoords()

NekDouble Nektar::SpatialDomains::GeometryLocator1D::v_GetLocCoords ( const Array< OneD, const NekDouble > &  coords,
Array< OneD, NekDouble > &  Lcoords 
)
overrideprotectedvirtual

Reimplemented from Nektar::SpatialDomains::GeometryLocator.

Definition at line 473 of file GeometryLocator.cpp.

475{
476 NekDouble dist = std::numeric_limits<double>::max();
477 m_geom->FillGeom();
478
479 // calculate local coordinate for coord
480 if (m_geom->CalcGeomType() == eRegular)
481 {
482 NekDouble len = 0.0;
483 NekDouble xi = 0.0;
484
485 const int npts = m_geom->GetXmap()->GetTotPoints();
486 Array<OneD, NekDouble> pts(npts);
487
488 for (int i = 0; i < m_geom->GetCoordim(); ++i)
489 {
490 m_geom->GetXmap()->BwdTrans(m_geom->GetCoeffs(i), pts);
491 len += (pts[npts - 1] - pts[0]) * (pts[npts - 1] - pts[0]);
492 xi += (coords[i] - pts[0]) * (pts[npts - 1] - pts[0]);
493 }
494 xi = xi / len;
495 if (xi < 0.)
496 {
497 dist = -xi * sqrt(len);
498 }
499 else if (xi > 1.)
500 {
501 dist = (xi - 1.) * sqrt(len);
502 }
503
504 Lcoords[0] = 2. * xi - 1.0;
505 }
506 else
507 {
509 "inverse mapping must be set up to use this call");
510 }
511 return dist;
512}
void FillGeom()
Gather the coefficients of from this object's boundary, unless that has already been done.
Definition Geometry.h:591

References Nektar::StdRegions::StdExpansion::BwdTrans(), Nektar::SpatialDomains::Geometry::CalcGeomType(), Nektar::ErrorUtil::efatal, Nektar::SpatialDomains::eRegular, Nektar::SpatialDomains::Geometry::FillGeom(), Nektar::SpatialDomains::Geometry::GetCoeffs(), Nektar::SpatialDomains::Geometry::GetCoordim(), Nektar::StdRegions::StdExpansion::GetTotPoints(), Nektar::SpatialDomains::Geometry::GetXmap(), Nektar::SpatialDomains::GeometryLocator::m_geom, NEKERROR, and tinysimd::sqrt().