Nektar++
Loading...
Searching...
No Matches
GeometryLocator.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: GeometryLocator.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: Inverse mapping x -> xi for a geometry.
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#include <cmath>
36#include <iomanip>
37
46
48{
49
51{
52 ASSERTL0(m_geom != nullptr, "A locator needs a geometry to search.");
53}
54
55/**
56 * @brief Populate the cache of quantities derived from the geometry.
57 *
58 * Separate from the constructor because it dispatches virtually: the
59 * inverse-coefficient and straight-edged forms differ by dimension.
60 */
62{
64
65 // Classifying the geometry is exactly the test of whether the nonlinear
66 // isoparametric coefficients vanish, so this yields both at once.
68 {
70 }
71 else if (m_straightEdge)
72 {
74 }
75}
76
78{
79 ASSERTL0(geom != nullptr, "A locator needs a geometry to search.");
80
82 switch (geom->GetShapeDim())
83 {
84 case 1:
85 loc = std::make_unique<GeometryLocator1D>(geom);
86 break;
87 case 2:
88 loc = std::make_unique<GeometryLocator2D>(geom);
89 break;
90 case 3:
91 loc = std::make_unique<GeometryLocator3D>(geom);
92 break;
93 default:
95 "No inverse mapping for this shape dimension.");
96 return loc;
97 }
98
99 loc->Build();
100 return loc;
101}
102
103/**
104 * @brief Determine whether the element contains a Cartesian coordinate.
106 * @see GeometryLocator::ContainsPoint
107 */
109 const Array<OneD, const NekDouble> &gloCoord, NekDouble tol)
110{
111 Array<OneD, NekDouble> locCoord(m_geom->GetCoordim(), 0.0);
112 NekDouble dist;
113 return ContainsPoint(gloCoord, locCoord, tol, dist);
114}
115
116/**
117 * @copydoc GeometryLocator::ContainsPoint
118 */
120 const Array<OneD, const NekDouble> &gloCoord,
121 Array<OneD, NekDouble> &locCoord, NekDouble tol)
122{
123 NekDouble dist;
124 return ContainsPoint(gloCoord, locCoord, tol, dist);
125}
126
127/**
128 * @brief Determine the local collapsed coordinates corresponding to a given
129 * Cartesian coordinate.
130 *
131 * For curvilinear and non-affine elements this is a nonlinear optimisation
132 * problem solved by Newton iteration, so it can be expensive.
133 *
134 * @param coords Input Cartesian global coordinates
135 * @param Lcoords Corresponding local coordinates
136 *
137 * @return Distance between the obtained coordinates and the provided ones.
138 */
141{
142 return v_GetLocCoords(coords, Lcoords);
143}
144
150
151/**
152 * @brief Generates the bounding box for the element.
153 *
154 * For regular elements, the vertices are sufficient to define the extent of
155 * the bounding box. For non-regular elements, the extremes of the quadrature
156 * point coordinates are used. A 10% margin is added around this computed
157 * region to account for convex hull elements where the true extent of the
158 * element may extend slightly beyond the quadrature points.
159 */
160std::array<NekDouble, 6> GeometryLocator::GetBoundingBox()
161{
162
164 {
165 return {{m_boundingBox[0], m_boundingBox[1], m_boundingBox[2],
167 }
168 // NekDouble minx, miny, minz, maxx, maxy, maxz;
170
171 // Always get vertexes min/max
172 PointGeom *p = m_geom->GetVertex(0);
173 Array<OneD, NekDouble> x(3, 0.0);
174 p->GetCoords(x[0], x[1], x[2]);
175 for (int j = 0; j < 3; ++j)
176 {
177 min[j] = x[j];
178 max[j] = x[j];
179 }
180 for (int i = 1; i < m_geom->GetNumVerts(); ++i)
181 {
182 p = m_geom->GetVertex(i);
183 p->GetCoords(x[0], x[1], x[2]);
184 for (int j = 0; j < 3; ++j)
185 {
186 min[j] = (x[j] < min[j] ? x[j] : min[j]);
187 max[j] = (x[j] > max[j] ? x[j] : max[j]);
188 }
189 }
190 // If element is deformed loop over quadrature points
192 if (m_geom->CalcGeomType() != eRegular)
193 {
194 marginFactor = 0.1;
195 const int nq = m_geom->GetXmap()->GetTotPoints();
197 for (int j = 0; j < 3; ++j)
198 {
199 xvec[j] = Array<OneD, NekDouble>(nq, 0.0);
200 }
201 for (int j = 0; j < m_geom->GetCoordim(); ++j)
202 {
203 m_geom->GetXmap()->BwdTrans(m_geom->GetCoeffs(j), xvec[j]);
204 }
205 for (int j = 0; j < 3; ++j)
206 {
207 for (int i = 0; i < nq; ++i)
208 {
209 min[j] = (xvec[j][i] < min[j] ? xvec[j][i] : min[j]);
210 max[j] = (xvec[j][i] > max[j] ? xvec[j][i] : max[j]);
211 }
212 }
213 }
214 // Add margin to bounding box, in order to
215 // return the nearest element
216 for (int j = 0; j < 3; ++j)
217 {
218 NekDouble margin =
219 marginFactor * (max[j] - min[j]) + NekConstants::kFindDistanceMin;
220 min[j] -= margin;
221 max[j] += margin;
222 }
223
224 // save bounding box
225 m_hasBoundingBox = true;
226 for (int j = 0; j < 3; ++j)
227 {
228 m_boundingBox[j] = min[j];
229 m_boundingBox[j + 3] = max[j];
230 }
231 // Return bounding box
232 return {{min[0], min[1], min[2], max[0], max[1], max[2]}};
233}
234
239
240/**
241 * @brief A fast and robust check if a given global coord is outside of a
242 * deformed element. For regular elements, this check is unnecessary.
243 *
244 * @param coords Input Cartesian global coordinates
245 * @param edgeDist For a point just outside straight edges of the element
246 * (see v_AllLeftCheck), its distance from them. Otherwise
247 * the largest double.
248 *
249 * @return 1 is inside of the element.
250 * 0 maybe inside
251 * -1 outside of the element
252 */
254 const Array<OneD, const NekDouble> &gloCoord, NekDouble &edgeDist)
255{
256 edgeDist = std::numeric_limits<double>::max();
257 // bounding box check
258 if (!MinMaxCheck(gloCoord))
259 {
260 return -1;
261 }
262
263 // regular element check
264 if (m_geom->CalcGeomType() == eRegular)
265 {
266 return 0;
267 }
268
269 // All left check for straight edges/plane surfaces
270 return v_AllLeftCheck(gloCoord, edgeDist);
271}
272
273/**
274 * @brief Check if given global coord is within the BoundingBox of the element.
275 *
276 * @param coords Input Cartesian global coordinates
277 *
278 * @return True if within distance or False otherwise.
279 */
281{
282 // Validation checks
283 ASSERTL1(gloCoord.size() >= m_geom->GetCoordim(),
284 "Expects number of global coordinates supplied to be greater than "
285 "or equal to the mesh dimension.");
286
287 std::array<NekDouble, 6> minMax = GetBoundingBox();
288 for (int i = 0; i < m_geom->GetCoordim(); ++i)
289 {
290 if ((gloCoord[i] < minMax[i]) || (gloCoord[i] > minMax[i + 3]))
291 {
292 return false;
293 }
294 }
295 return true;
296}
297
298/**
299 * @brief Clamp local coords to be within standard regions [-1, 1]^dim.
300 *
301 * @param Lcoords Corresponding local coordinates
302 */
304 NekDouble tol)
305{
306 // Validation checks
307 ASSERTL1(locCoord.size() >= dim, "Expects local coordinates to be same or "
308 "larger than shape dimension.");
309
310 // If out of range clamp locCoord to be within [-1,1]^dim
311 // since any larger value will be very oscillatory if
312 // called by 'returnNearestElmt' option in
313 // ExpList::GetExpIndex
314 bool clamp = false;
315 for (int i = 0; i < dim; ++i)
316 {
317 if (!std::isfinite(locCoord[i]))
318 {
319 locCoord[i] = 0.;
320 clamp = true;
321 }
322 else if (locCoord[i] < -(1. + tol))
323 {
324 locCoord[i] = -(1. + tol);
325 clamp = true;
326 }
327 else if (locCoord[i] > (1. + tol))
328 {
329 locCoord[i] = 1. + tol;
330 clamp = true;
331 }
332 }
333 return clamp;
334}
335
336/**
337 * @copydoc Geometry::ContainsPoint(
338 * const Array<OneD, const NekDouble> &, Array<OneD, NekDouble> &,
339 * NekDouble, NekDouble&)
340 * dist is assigned value for curved elements
341 */
343 const Array<OneD, const NekDouble> &gloCoord,
344 Array<OneD, NekDouble> &locCoord, NekDouble tol, NekDouble &dist)
345{
346 NekDouble edgeDist;
347 int inside = PreliminaryCheck(gloCoord, edgeDist);
348 if (inside == -1)
349 {
350 dist = edgeDist;
351 if (edgeDist < std::numeric_limits<double>::max())
352 {
353 // Just outside a straight edge: not inside, but the element is
354 // a candidate nearest element. A point on a straight domain
355 // boundary is only on it to round-off, and no other element can
356 // contain it. Return the clamped local coordinates and the
357 // distance, so that GetExpIndex can still pick this element
358 // when no element contains the point.
359 NekDouble locDist = GetLocCoords(gloCoord, locCoord);
361 m_geom->GetXmap()->LocCoordToLocCollapsed(locCoord, eta);
362 ClampLocCoords(eta, m_geom->GetShapeDim(), 0.);
363 m_geom->GetXmap()->LocCollapsedToLocCoord(eta, locCoord);
364 // Only curved elements measure a distance in GetLocCoords
365 if (locDist < std::numeric_limits<double>::max())
366 {
367 dist = std::max(dist, locDist);
368 }
369 }
370 return false;
371 }
372 dist = GetLocCoords(gloCoord, locCoord);
373 if (inside == 1)
374 {
375 dist = 0.;
376 return true;
377 }
378 else
379 {
381 m_geom->GetXmap()->LocCoordToLocCollapsed(locCoord, eta);
382 if (ClampLocCoords(eta, m_geom->GetShapeDim(), tol))
383 {
384 // Return the clamped point, at which dist was measured: the
385 // caller may take it as the nearest point. Unclamped, a point
386 // just beyond a collapsed edge or vertex can have collapsed
387 // coordinates of any size, and the expansion is evaluated there.
388 m_geom->GetXmap()->LocCollapsedToLocCoord(eta, locCoord);
389 // A straight-sided 3D element's Newton iteration reports no
390 // distance to the element, only whether it converged, so it
391 // offers no nearest point. A straight-sided pyramid with a
392 // non-parallelogram base (m_nCoeff = 0) takes the full-map
393 // Newton iteration instead, which does measure the distance.
394 if (m_geom->CalcGeomType() == eRegular ||
395 (m_geom->GetShapeDim() == 3 && m_straightEdge &&
396 m_iso.m_nCoeff > 0))
397 {
398 dist = std::numeric_limits<double>::max();
399 }
400 return false;
401 }
402 // Elements located by Newton iteration: 2D curved, and 3D
403 if (m_geom->GetCoordim() == m_geom->GetShapeDim() &&
405 (m_geom->GetShapeDim() == 3 ||
406 (m_geom->GetShapeDim() == 2 && !m_straightEdge)))
407 {
408 // The point is within tol of the element, so it is inside if the
409 // local coordinates map to it. dist cannot tell: a converged
410 // point just outside keeps the distance from the clamped point,
411 // and one that did not converge keeps its residual (both used
412 // by GetExpIndex to rank the nearest element).
413 return m_newtonConverged;
414 }
415 return m_geom->GetCoordim() != 3 ||
418 dist <= tol;
419 }
420}
421
422/**
423 * @copydoc Geometry::GetLocCoords()
424 */
426 [[maybe_unused]] const Array<OneD, const NekDouble> &coords,
427 [[maybe_unused]] Array<OneD, NekDouble> &Lcoords)
428{
430 "This function is only valid for expansion type geometries");
431 return 0.0;
432}
433
435 [[maybe_unused]] const Array<OneD, const NekDouble> &xs,
436 [[maybe_unused]] Array<OneD, NekDouble> &xi)
437{
439 "This function has not been defined for this geometry");
440 return false;
441}
442
444 [[maybe_unused]] const Array<OneD, const NekDouble> &gloCoord,
445 [[maybe_unused]] NekDouble &edgeDist)
446{
447 return 0;
448}
449
450/**
451 * @copydoc GeometryLocator::v_CalculateInverseIsoParam()
452 *
453 * Nothing to do by default. Build() drives this for every shape, but only
454 * the 2D and 3D inverse maps are expressed through an inverted coefficient
455 * matrix; a segment inverts \f$\chi\f$ in closed form straight from its
456 * quadrature points.
457 *
458 * @see Geometry1D::v_GetLocCoords
459 */
463
464/**
465 * @copydoc GeometryLocator::v_PreSolveStraightEdge()
466 *
467 * Only the quadrilateral has a straight-edged solve to prepare for.
468 */
472
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}
513
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
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
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);
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}
660
663{
664 auto &isoParam = m_iso.m_coeff;
665 int i0 = 0, i1 = 1, j1 = 0, j2 = 1;
666 if (m_straightEdge & 2)
667 {
668 i0 = 1;
669 i1 = 0;
670 }
671 if (m_straightEdge & 4)
672 {
673 j1 = 1;
674 j2 = 0;
675 }
676 NekDouble beta = isoParam[1][2];
677 NekDouble gamma = isoParam[1][3];
678 NekDouble tty =
679 (coords[i1] - gamma * coords[i0] - isoParam[1][0]) * isoParam[1][1];
680 NekDouble denom = 1. / (isoParam[0][2] + isoParam[0][3] * tty);
681 NekDouble epsilon = -isoParam[0][3] * beta * denom;
682 NekDouble h = (isoParam[0][0] + isoParam[0][1] * tty - coords[i0]) * denom;
683 Lcoords[j2] = -h / (0.5 + sqrt(0.25 - epsilon * h));
684 Lcoords[j1] = -beta * Lcoords[j2] + tty;
685}
686
688 const Array<OneD, const NekDouble> &coords,
691 NekDouble &dist)
692{
693 // Maximum iterations for convergence
694 const int MaxIterations = NekConstants::kNewtonIterations;
695 // |x-xp|^2 < EPSILON error tolerance
696 const NekDouble Tol = 1.e-8;
697 // |r,s| > LcoordDIV stop the search
698 const NekDouble LcoordDiv = 15.0;
699
702 m_geom->GenGeomFactors(ptsKeys)->GetJac();
703
704 NekDouble ScaledTol =
705 Vmath::Vsum(Jac.size(), Jac, 1) / ((NekDouble)Jac.size());
706 ScaledTol *= Tol;
707
708 NekDouble xmap, ymap, F1, F2;
709 NekDouble derx_1, derx_2, dery_1, dery_2, jac;
710
711 // save intiial guess for later reference if required.
712 NekDouble init0 = Lcoords[0], init1 = Lcoords[1];
713
714 Array<OneD, NekDouble> DxD1(ptsx.size());
715 Array<OneD, NekDouble> DxD2(ptsx.size());
716 Array<OneD, NekDouble> DyD1(ptsx.size());
717 Array<OneD, NekDouble> DyD2(ptsx.size());
718
719 // Ideally this will be stored in m_geomfactors
720 m_geom->GetXmap()->PhysDeriv(ptsx, DxD1, DxD2);
721 m_geom->GetXmap()->PhysDeriv(ptsy, DyD1, DyD2);
722
723 int cnt = 0;
726
727 F1 = F2 = 2000; // Starting value of Function
728 NekDouble resid = sqrt(F1 * F1 + F2 * F2);
729 m_newtonConverged = false;
730 while (cnt++ < MaxIterations)
731 {
732 // evaluate lagrange interpolant at Lcoords
733 m_geom->GetXmap()->LocCoordToLocCollapsed(Lcoords, eta);
734 I[0] = m_geom->GetXmap()->GetBasis(0)->GetI(eta);
735 I[1] = m_geom->GetXmap()->GetBasis(1)->GetI(eta + 1);
736
737 // calculate the global point `corresponding to Lcoords
738 xmap = m_geom->GetXmap()->PhysEvaluate(I, ptsx);
739 ymap = m_geom->GetXmap()->PhysEvaluate(I, ptsy);
740
741 F1 = coords[0] - xmap;
742 F2 = coords[1] - ymap;
743
744 if (F1 * F1 + F2 * F2 < ScaledTol)
745 {
746 resid = sqrt(F1 * F1 + F2 * F2);
747 m_newtonConverged = true;
748 break;
749 }
750
751 // Interpolate derivative metric at Lcoords
752 derx_1 = m_geom->GetXmap()->PhysEvaluate(I, DxD1);
753 derx_2 = m_geom->GetXmap()->PhysEvaluate(I, DxD2);
754 dery_1 = m_geom->GetXmap()->PhysEvaluate(I, DyD1);
755 dery_2 = m_geom->GetXmap()->PhysEvaluate(I, DyD2);
756
757 jac = dery_2 * derx_1 - dery_1 * derx_2;
758
759 // use analytical inverse of derivitives which are
760 // also similar to those of metric factors.
761 Lcoords[0] =
762 Lcoords[0] +
763 (dery_2 * (coords[0] - xmap) - derx_2 * (coords[1] - ymap)) / jac;
764
765 Lcoords[1] =
766 Lcoords[1] +
767 (-dery_1 * (coords[0] - xmap) + derx_1 * (coords[1] - ymap)) / jac;
768
769 if (!(std::isfinite(Lcoords[0]) && std::isfinite(Lcoords[1])))
770 {
771 dist = 1e16;
772 std::ostringstream ss;
773 ss << "nan or inf found in NewtonIterationForLocCoord in element "
774 << m_geom->GetGlobalID();
775 WARNINGL1(false, ss.str());
776 return;
777 }
778 if (fabs(Lcoords[0]) > LcoordDiv || fabs(Lcoords[1]) > LcoordDiv)
779 {
780 break; // lcoords have diverged so stop iteration
781 }
782 }
783
784 m_geom->GetXmap()->LocCoordToLocCollapsed(Lcoords, eta);
785 if (ClampLocCoords(eta, m_geom->GetShapeDim(), 0.))
786 {
787 I[0] = m_geom->GetXmap()->GetBasis(0)->GetI(eta);
788 I[1] = m_geom->GetXmap()->GetBasis(1)->GetI(eta + 1);
789 // calculate the global point corresponding to Lcoords
790 xmap = m_geom->GetXmap()->PhysEvaluate(I, ptsx);
791 ymap = m_geom->GetXmap()->PhysEvaluate(I, ptsy);
792 F1 = coords[0] - xmap;
793 F2 = coords[1] - ymap;
794 dist = sqrt(F1 * F1 + F2 * F2);
795 }
796 else if (m_newtonConverged)
797 {
798 dist = 0.;
799 }
800 else
801 {
802 // Inside the element but not converged: Lcoords does not map to
803 // coords, so report the residual rather than claiming a match
804 I[0] = m_geom->GetXmap()->GetBasis(0)->GetI(eta);
805 I[1] = m_geom->GetXmap()->GetBasis(1)->GetI(eta + 1);
806 xmap = m_geom->GetXmap()->PhysEvaluate(I, ptsx);
807 ymap = m_geom->GetXmap()->PhysEvaluate(I, ptsy);
808 F1 = coords[0] - xmap;
809 F2 = coords[1] - ymap;
810 dist = std::sqrt(F1 * F1 + F2 * F2);
811 resid = dist;
812 }
813
814 if (cnt >= MaxIterations)
815 {
816 Array<OneD, NekDouble> collCoords(2);
817 m_geom->GetXmap()->LocCoordToLocCollapsed(Lcoords, collCoords);
818
819 // if coordinate is inside element dump error!
820 if ((collCoords[0] >= -1.0 && collCoords[0] <= 1.0) &&
821 (collCoords[1] >= -1.0 && collCoords[1] <= 1.0))
822 {
823 std::ostringstream ss;
824
825 ss << "Reached MaxIterations (" << MaxIterations
826 << ") in Newton iteration ";
827 ss << "Init value (" << std::setprecision(4) << init0 << ","
828 << init1 << ","
829 << ") ";
830 ss << "Fin value (" << Lcoords[0] << "," << Lcoords[1] << ","
831 << ") ";
832 ss << "Resid = " << resid
833 << " Tolerance = " << std::sqrt(ScaledTol);
834
835 WARNINGL1(cnt < MaxIterations, ss.str());
836 }
837 }
838}
839
842{
843 NekDouble dist = std::numeric_limits<double>::max();
844 Array<OneD, NekDouble> tmpcoords(2);
845 tmpcoords[0] = coords[m_iso.m_manifold[0]];
846 tmpcoords[1] = coords[m_iso.m_manifold[1]];
847 GeomType Gtype = m_geom->CalcGeomType();
848 if (Gtype == eRegular)
849 {
850 tmpcoords[0] -= m_iso.m_coeff[0][0];
851 tmpcoords[1] -= m_iso.m_coeff[1][0];
852 Lcoords[0] = m_invIsoParam[0][0] * tmpcoords[0] +
853 m_invIsoParam[0][1] * tmpcoords[1];
854 Lcoords[1] = m_invIsoParam[1][0] * tmpcoords[0] +
855 m_invIsoParam[1][1] * tmpcoords[1];
856 }
857 else if (m_straightEdge)
858 {
859 SolveStraightEdgeQuad(tmpcoords, Lcoords);
860 }
861 else if (Gtype == eDeformed)
862 {
863 m_geom->FillGeom();
864 // Determine nearest point of coords to values in m_geom->GetXmap()
865 int npts = m_geom->GetXmap()->GetTotPoints();
866 Array<OneD, NekDouble> ptsx(npts), ptsy(npts);
867 Array<OneD, NekDouble> tmpx(npts), tmpy(npts);
868
869 // Determine 3D manifold orientation
871 ptsx);
873 ptsy);
874
875 // Start from the nearest quadrature point: from the centre, the
876 // iteration can diverge for points near the corners of strongly
877 // curved elements
878 Vmath::Sadd(npts, -tmpcoords[0], ptsx, 1, tmpx, 1);
879 Vmath::Sadd(npts, -tmpcoords[1], ptsy, 1, tmpy, 1);
880 Vmath::Vmul(npts, tmpx, 1, tmpx, 1, tmpx, 1);
881 Vmath::Vvtvp(npts, tmpy, 1, tmpy, 1, tmpx, 1, tmpx, 1);
882
883 int min_i = Vmath::Imin(npts, tmpx, 1);
884
887 Array<OneD, NekDouble> eta(2, 0.);
888 eta[0] = za[min_i % za.size()];
889 eta[1] = zb[min_i / za.size()];
890 m_geom->GetXmap()->LocCollapsedToLocCoord(eta, Lcoords);
891
892 // Perform newton iteration to find local coordinates
893 NewtonIterationForLocCoord(tmpcoords, ptsx, ptsy, Lcoords, dist);
894 }
895 if (m_geom->GetCoordim() == 3)
896 {
897 Array<OneD, NekDouble> eta(2, 0.), xi(2, 0.);
898 m_geom->GetXmap()->LocCoordToLocCollapsed(Lcoords, eta);
899 ClampLocCoords(eta, m_geom->GetShapeDim(), 0.);
901 int npts = m_geom->GetXmap()->GetTotPoints();
902 Array<OneD, NekDouble> ptsz(npts);
904 ptsz);
905 NekDouble z = m_geom->GetXmap()->PhysEvaluate(xi, ptsz) -
906 coords[m_iso.m_manifold[2]];
907 if (Gtype == eDeformed)
908 {
909 dist = sqrt(z * z + dist * dist);
910 }
911 else
912 {
913 dist = fabs(z);
914 }
915 }
916 return dist;
917}
918
921{
922 GeomType Gtype = m_geom->CalcGeomType();
923 if (Gtype == eRegular)
924 {
925 xiOut = Array<OneD, NekDouble>(2, 0.0);
926
927 GetLocCoords(xs, xiOut);
929
930 Array<OneD, NekDouble> gloCoord(3);
931 gloCoord[0] = m_geom->GetCoord(0, xiOut);
932 gloCoord[1] = m_geom->GetCoord(1, xiOut);
933 gloCoord[2] = m_geom->GetCoord(2, xiOut);
934
935 return sqrt((xs[0] - gloCoord[0]) * (xs[0] - gloCoord[0]) +
936 (xs[1] - gloCoord[1]) * (xs[1] - gloCoord[1]) +
937 (xs[2] - gloCoord[2]) * (xs[2] - gloCoord[2]));
938 }
939 // If deformed edge then the inverse mapping is non-linear so need to
940 // numerically solve for the local coordinate
941 else if (Gtype == eDeformed)
942 {
943 // Choose starting based on closest quad
944 Array<OneD, NekDouble> xi(2, 0.0), eta(2, 0.0);
946
947 // Armijo constants:
948 // https://en.wikipedia.org/wiki/Backtracking_line_search
949 const NekDouble c1 = 1e-4, c2 = 0.9;
950
951 int nq = m_geom->GetXmap()->GetTotPoints();
952
953 Array<OneD, NekDouble> x(nq), y(nq), z(nq);
957
958 Array<OneD, NekDouble> xderxi1(nq, 0.0), yderxi1(nq, 0.0),
959 zderxi1(nq, 0.0), xderxi2(nq, 0.0), yderxi2(nq, 0.0),
960 zderxi2(nq, 0.0), xderxi1xi1(nq, 0.0), yderxi1xi1(nq, 0.0),
961 zderxi1xi1(nq, 0.0), xderxi1xi2(nq, 0.0), yderxi1xi2(nq, 0.0),
962 zderxi1xi2(nq, 0.0), xderxi2xi1(nq, 0.0), yderxi2xi1(nq, 0.0),
963 zderxi2xi1(nq, 0.0), xderxi2xi2(nq, 0.0), yderxi2xi2(nq, 0.0),
964 zderxi2xi2(nq, 0.0);
965
966 // Get first & second derivatives & partial derivatives of x,y,z values
967 std::array<NekDouble, 3> xc_derxi, yc_derxi, zc_derxi;
968
969 m_geom->GetXmap()->PhysDeriv(x, xderxi1, xderxi2);
970 m_geom->GetXmap()->PhysDeriv(y, yderxi1, yderxi2);
971 m_geom->GetXmap()->PhysDeriv(z, zderxi1, zderxi2);
972
973 m_geom->GetXmap()->PhysDeriv(xderxi1, xderxi1xi1, xderxi1xi2);
974 m_geom->GetXmap()->PhysDeriv(yderxi1, yderxi1xi1, yderxi1xi2);
975 m_geom->GetXmap()->PhysDeriv(zderxi1, zderxi1xi1, zderxi1xi2);
976
977 m_geom->GetXmap()->PhysDeriv(yderxi2, yderxi2xi1, yderxi2xi2);
978 m_geom->GetXmap()->PhysDeriv(xderxi2, xderxi2xi1, xderxi2xi2);
979 m_geom->GetXmap()->PhysDeriv(zderxi2, zderxi2xi1, zderxi2xi2);
980
981 // Minimisation loop (Quasi-newton method)
982 NekDouble fx_prev = std::numeric_limits<NekDouble>::max();
983 for (int i = 0; i < NekConstants::kNewtonIterations; ++i)
984 {
985 // Compute the objective function, f(x_k) and its derivatives
986 NekDouble xc = m_geom->GetXmap()->PhysEvaluate(xi, x, xc_derxi);
987 NekDouble yc = m_geom->GetXmap()->PhysEvaluate(xi, y, yc_derxi);
988 NekDouble zc = m_geom->GetXmap()->PhysEvaluate(xi, z, zc_derxi);
989
990 NekDouble xc_derxi1xi1 =
991 m_geom->GetXmap()->PhysEvaluate(xi, xderxi1xi1);
992 NekDouble yc_derxi1xi1 =
993 m_geom->GetXmap()->PhysEvaluate(xi, yderxi1xi1);
994 NekDouble zc_derxi1xi1 =
995 m_geom->GetXmap()->PhysEvaluate(xi, zderxi1xi1);
996
997 NekDouble xc_derxi1xi2 =
998 m_geom->GetXmap()->PhysEvaluate(xi, xderxi1xi2);
999 NekDouble yc_derxi1xi2 =
1000 m_geom->GetXmap()->PhysEvaluate(xi, yderxi1xi2);
1001 NekDouble zc_derxi1xi2 =
1002 m_geom->GetXmap()->PhysEvaluate(xi, zderxi1xi2);
1003
1004 NekDouble xc_derxi2xi2 =
1005 m_geom->GetXmap()->PhysEvaluate(xi, xderxi2xi2);
1006 NekDouble yc_derxi2xi2 =
1007 m_geom->GetXmap()->PhysEvaluate(xi, yderxi2xi2);
1008 NekDouble zc_derxi2xi2 =
1009 m_geom->GetXmap()->PhysEvaluate(xi, zderxi2xi2);
1010
1011 // Objective function is the distance to the search point
1012 NekDouble xdiff = xc - xs[0];
1013 NekDouble ydiff = yc - xs[1];
1014 NekDouble zdiff = zc - xs[2];
1015
1016 NekDouble fx = xdiff * xdiff + ydiff * ydiff + zdiff * zdiff;
1017
1018 NekDouble fx_derxi1 = 2.0 * xdiff * xc_derxi[0] +
1019 2.0 * ydiff * yc_derxi[0] +
1020 2.0 * zdiff * zc_derxi[0];
1021
1022 NekDouble fx_derxi2 = 2.0 * xdiff * xc_derxi[1] +
1023 2.0 * ydiff * yc_derxi[1] +
1024 2.0 * zdiff * zc_derxi[1];
1025
1026 NekDouble fx_derxi1xi1 =
1027 2.0 * xdiff * xc_derxi1xi1 + 2.0 * xc_derxi[0] * xc_derxi[0] +
1028 2.0 * ydiff * yc_derxi1xi1 + 2.0 * yc_derxi[0] * yc_derxi[0] +
1029 2.0 * zdiff * zc_derxi1xi1 + 2.0 * zc_derxi[0] * zc_derxi[0];
1030
1031 NekDouble fx_derxi1xi2 =
1032 2.0 * xdiff * xc_derxi1xi2 + 2.0 * xc_derxi[1] * xc_derxi[0] +
1033 2.0 * ydiff * yc_derxi1xi2 + 2.0 * yc_derxi[1] * yc_derxi[0] +
1034 2.0 * zdiff * zc_derxi1xi2 + 2.0 * zc_derxi[1] * zc_derxi[0];
1035
1036 NekDouble fx_derxi2xi2 =
1037 2.0 * xdiff * xc_derxi2xi2 + 2.0 * xc_derxi[1] * xc_derxi[1] +
1038 2.0 * ydiff * yc_derxi2xi2 + 2.0 * yc_derxi[1] * yc_derxi[1] +
1039 2.0 * zdiff * zc_derxi2xi2 + 2.0 * zc_derxi[1] * zc_derxi[1];
1040
1041 // Jacobian
1042 NekDouble jac[2];
1043 jac[0] = fx_derxi1;
1044 jac[1] = fx_derxi2;
1045
1046 // Inverse of 2x2 hessian
1047 NekDouble hessInv[2][2];
1048
1049 NekDouble det =
1050 1 / (fx_derxi1xi1 * fx_derxi2xi2 - fx_derxi1xi2 * fx_derxi1xi2);
1051 hessInv[0][0] = det * fx_derxi2xi2;
1052 hessInv[0][1] = det * -fx_derxi1xi2;
1053 hessInv[1][0] = det * -fx_derxi1xi2;
1054 hessInv[1][1] = det * fx_derxi1xi1;
1055
1056 // Check for convergence
1057 if (std::abs(fx - fx_prev) < 1e-12)
1058 {
1059 fx_prev = fx;
1060 break;
1061 }
1062 else
1063 {
1064 fx_prev = fx;
1065 }
1066
1067 NekDouble gamma = 1.0;
1068 bool conv = false;
1069
1070 // Search direction: Newton's method
1071 NekDouble pk[2];
1072 pk[0] = -(hessInv[0][0] * jac[0] + hessInv[1][0] * jac[1]);
1073 pk[1] = -(hessInv[0][1] * jac[0] + hessInv[1][1] * jac[1]);
1074
1075 // Backtracking line search
1076 while (gamma > 1e-10)
1077 {
1078 Array<OneD, NekDouble> xi_pk(2);
1079 xi_pk[0] = xi[0] + pk[0] * gamma;
1080 xi_pk[1] = xi[1] + pk[1] * gamma;
1081
1082 Array<OneD, NekDouble> eta_pk(2, 0.0);
1083 m_geom->GetXmap()->LocCoordToLocCollapsed(xi_pk, eta_pk);
1084
1085 if (eta_pk[0] <
1086 (-1 - std::numeric_limits<NekDouble>::epsilon()) ||
1087 eta_pk[0] >
1088 (1 + std::numeric_limits<NekDouble>::epsilon()) ||
1089 eta_pk[1] <
1090 (-1 - std::numeric_limits<NekDouble>::epsilon()) ||
1091 eta_pk[1] > (1 + std::numeric_limits<NekDouble>::epsilon()))
1092 {
1093 gamma /= 2.0;
1094 continue;
1095 }
1096
1097 std::array<NekDouble, 3> xc_pk_derxi, yc_pk_derxi, zc_pk_derxi;
1098
1099 NekDouble xc_pk =
1100 m_geom->GetXmap()->PhysEvaluate(xi_pk, x, xc_pk_derxi);
1101 NekDouble yc_pk =
1102 m_geom->GetXmap()->PhysEvaluate(xi_pk, y, yc_pk_derxi);
1103 NekDouble zc_pk =
1104 m_geom->GetXmap()->PhysEvaluate(xi_pk, z, zc_pk_derxi);
1105
1106 NekDouble xc_pk_diff = xc_pk - xs[0];
1107 NekDouble yc_pk_diff = yc_pk - xs[1];
1108 NekDouble zc_pk_diff = zc_pk - xs[2];
1109
1110 NekDouble fx_pk = xc_pk_diff * xc_pk_diff +
1111 yc_pk_diff * yc_pk_diff +
1112 zc_pk_diff * zc_pk_diff;
1113
1114 NekDouble fx_pk_derxi1 = 2.0 * xc_pk_diff * xc_pk_derxi[0] +
1115 2.0 * yc_pk_diff * yc_pk_derxi[0] +
1116 2.0 * zc_pk_diff * zc_pk_derxi[0];
1117
1118 NekDouble fx_pk_derxi2 = 2.0 * xc_pk_diff * xc_pk_derxi[1] +
1119 2.0 * yc_pk_diff * yc_pk_derxi[1] +
1120 2.0 * zc_pk_diff * zc_pk_derxi[1];
1121
1122 // Check Wolfe conditions using Armijo constants
1123 // https://en.wikipedia.org/wiki/Wolfe_conditions
1124 NekDouble tmp = pk[0] * fx_derxi1 + pk[1] * fx_derxi2;
1125 NekDouble tmp2 = pk[0] * fx_pk_derxi1 + pk[1] * fx_pk_derxi2;
1126 if ((fx_pk - (fx + c1 * gamma * tmp)) <
1127 std::numeric_limits<NekDouble>::epsilon() &&
1128 (-tmp2 - (-c2 * tmp)) <
1129 std::numeric_limits<NekDouble>::epsilon())
1130 {
1131 conv = true;
1132 break;
1133 }
1134
1135 gamma /= 2.0;
1136 }
1137
1138 if (!conv)
1139 {
1140 break;
1141 }
1142
1143 xi[0] += gamma * pk[0];
1144 xi[1] += gamma * pk[1];
1145 }
1146
1147 xiOut = xi;
1148 return sqrt(fx_prev);
1149 }
1150 else
1151 {
1152 NEKERROR(ErrorUtil::efatal, "Geometry type unknown");
1153 }
1154
1155 return -1.0;
1156}
1157
1159{
1160 NekDouble Jac = m_iso.m_coeff[0][1] * m_iso.m_coeff[1][2] -
1161 m_iso.m_coeff[1][1] * m_iso.m_coeff[0][2];
1162 Jac = 1. / Jac;
1163 // a12, -a02, -a11, a01
1164 m_invIsoParam[0][0] = m_iso.m_coeff[1][2] * Jac;
1165 m_invIsoParam[0][1] = -m_iso.m_coeff[0][2] * Jac;
1166 m_invIsoParam[1][0] = -m_iso.m_coeff[1][1] * Jac;
1167 m_invIsoParam[1][1] = m_iso.m_coeff[0][1] * Jac;
1168}
1169
1171 const Array<OneD, const NekDouble> &gloCoord, NekDouble &edgeDist)
1172{
1173 int nc = 1, d0 = m_iso.m_manifold[0], d1 = m_iso.m_manifold[1];
1174 NekDouble nearDist = -1.; // >= 0 once the point is just outside an edge
1175 if (!m_edgeNormalBuilt)
1176 {
1177 m_edgeNormalBuilt = true;
1178 std::array<std::array<NekDouble, 3>, 2> x;
1179 m_geom->GetVertex(0)->GetCoords(x[0][0], x[0][1], x[0][2]);
1180 int i0 = 1, i1 = 0, direction = 1;
1181 for (size_t i = 0; i < m_geom->GetNumVerts(); ++i)
1182 {
1183 i0 ^= 1;
1184 i1 ^= 1;
1185 m_geom->GetVertex((i + 1) % m_geom->GetNumVerts())
1186 ->GetCoords(x[i1][0], x[i1][1], x[i1][2]);
1187 if (m_geom->GetEdge(i)->GetXmap()->GetBasis(0)->GetNumModes() > 2)
1188 {
1189 continue;
1190 }
1191 m_hasEdgeNormal[i] = true;
1192 m_edgeNormal[i][0] = x[i0][d1] - x[i1][d1];
1193 m_edgeNormal[i][1] = x[i1][d0] - x[i0][d0];
1194 }
1195 if (m_geom->GetCoordim() == 3)
1196 {
1197 for (size_t i = 0; i < m_geom->GetNumVerts(); ++i)
1198 {
1199 if (m_hasEdgeNormal[i])
1200 {
1201 m_geom->GetVertex(i)->GetCoords(x[0][0], x[0][1], x[0][2]);
1202 m_geom->GetVertex((i + 2) % m_geom->GetNumVerts())
1203 ->GetCoords(x[1][0], x[1][1], x[1][2]);
1204 if (m_edgeNormal[i][0] * (x[1][d0] - x[0][d0]) <
1205 m_edgeNormal[i][1] * (x[0][d1] - x[1][d1]))
1206 {
1207 direction = -1;
1208 }
1209 break;
1210 }
1211 }
1212 }
1213 if (direction == -1)
1214 {
1215 for (size_t i = 0; i < m_geom->GetNumVerts(); ++i)
1216 {
1217 if (m_hasEdgeNormal[i])
1218 {
1219 m_edgeNormal[i][0] = -m_edgeNormal[i][0];
1220 m_edgeNormal[i][1] = -m_edgeNormal[i][1];
1221 }
1222 }
1223 }
1224 }
1225
1226 std::array<NekDouble, 3> vertex;
1227 for (size_t i = 0; i < m_geom->GetNumVerts(); ++i)
1228 {
1229 int i1 = (i + 1) % m_geom->GetNumVerts();
1230 if (m_geom->GetVertex(i)->GetGlobalID() <
1232 {
1233 m_geom->GetVertex(i)->GetCoords(vertex[0], vertex[1], vertex[2]);
1234 }
1235 else
1236 {
1237 m_geom->GetVertex(i1)->GetCoords(vertex[0], vertex[1], vertex[2]);
1238 }
1239 if (!m_hasEdgeNormal[i])
1240 {
1241 nc = 0; // not sure
1242 continue;
1243 }
1244 // s is the signed distance from the edge times its length L, the
1245 // length of the normal. Both elements sharing the edge use the same
1246 // vertex and exactly opposite normals, so their values of s are
1247 // exactly opposite: the test s < 0 never rejects a point in both, and
1248 // a point off the edge passes it only in the element on its side.
1249 // A point that fails by less than kGeomFactorsTol * L is just
1250 // outside: its distance is returned in edgeDist, so that
1251 // ContainsPoint can still offer the element as the nearest one.
1252 NekDouble n2 = m_edgeNormal[i][0] * m_edgeNormal[i][0] +
1253 m_edgeNormal[i][1] * m_edgeNormal[i][1];
1254 NekDouble s = m_edgeNormal[i][0] * (gloCoord[d0] - vertex[d0]) +
1255 m_edgeNormal[i][1] * (gloCoord[d1] - vertex[d1]);
1256 if (s < -NekConstants::kGeomFactorsTol * n2)
1257 {
1258 return -1; // outside
1259 }
1260 if (s < 0.)
1261 {
1262 nearDist = std::max(nearDist, -s / std::sqrt(n2));
1263 }
1264 }
1265 if (nearDist >= 0.)
1266 {
1267 edgeDist = nearDist;
1268 return -1; // just outside
1269 }
1270 // 3D manifold needs to check the distance
1271 if (m_geom->GetCoordim() == 3)
1272 {
1273 nc = 0;
1274 }
1275 // nc: 1 (side element), 0 (maybe inside), -1 (outside)
1276 return nc;
1277}
1278
1280{
1281 int i0, i1, j1, j2;
1282 if (fabs(m_iso.m_coeff[0][3]) >= fabs(m_iso.m_coeff[1][3]))
1283 {
1284 i0 = 0;
1285 i1 = 1;
1286 }
1287 else
1288 {
1289 i1 = 0;
1290 i0 = 1;
1291 m_straightEdge |= 2;
1292 }
1293 NekDouble gamma = m_iso.m_coeff[i1][3] / m_iso.m_coeff[i0][3];
1294 std::vector<NekDouble> c(3);
1295 for (int i = 0; i < 3; ++i)
1296 {
1297 c[i] = m_iso.m_coeff[i1][i] - gamma * m_iso.m_coeff[i0][i];
1298 }
1299 if (fabs(c[1]) >= fabs(c[2]))
1300 {
1301 j1 = 1;
1302 j2 = 2;
1303 }
1304 else
1305 {
1306 j1 = 2;
1307 j2 = 1;
1308 m_straightEdge |= 4;
1309 }
1310 NekDouble beta = c[j2] / c[j1];
1311 if (i0 == 1)
1312 {
1313 m_iso.m_coeff[0] = m_iso.m_coeff[1];
1314 }
1315 if (j1 == 2)
1316 {
1317 NekDouble temp = m_iso.m_coeff[0][j1];
1318 m_iso.m_coeff[0][j1] = m_iso.m_coeff[0][j2];
1319 m_iso.m_coeff[0][j2] = temp;
1320 }
1321 m_iso.m_coeff[0][2] -= m_iso.m_coeff[0][1] * beta;
1322 m_iso.m_coeff[1][0] = c[0];
1323 m_iso.m_coeff[1][1] = 1. / c[j1];
1324 m_iso.m_coeff[1][2] = beta;
1325 m_iso.m_coeff[1][3] = gamma;
1326}
1327
1330 NekDouble &dist)
1331{
1332 auto &isoParam = m_iso.m_coeff;
1333 const int nIso = m_iso.m_nCoeff;
1334
1335 // maximum iterations for convergence
1336 const int MaxIterations = 51;
1337 // |x-xp|^2 < EPSILON error tolerance
1338 const NekDouble Tol = 1.e-8;
1339 // |r,s| > LcoordDIV stop the search
1340 const NekDouble LcoordDiv = 15.0;
1341
1344 m_geom->GenGeomFactors(ptsKeys)->GetJac();
1345
1346 NekDouble ScaledTol =
1347 Vmath::Vsum(Jac.size(), Jac, 1) / ((NekDouble)Jac.size());
1348 ScaledTol *= Tol * Tol;
1349
1350 NekDouble xmap, ymap, zmap, res;
1351 int cnt = 0;
1352 Array<OneD, NekDouble> var(8, 1.);
1353 Array<OneD, NekDouble> deriv(9);
1355 m_newtonConverged = false;
1356 while (cnt++ < MaxIterations)
1357 {
1358 var[1] = Lcoords[0];
1359 var[2] = Lcoords[1];
1360 var[3] = Lcoords[2];
1361 var[4] = Lcoords[0] * Lcoords[1];
1362 var[5] = Lcoords[1] * Lcoords[2];
1363 var[6] = Lcoords[0] * Lcoords[2];
1364 var[7] = var[4] * Lcoords[2];
1365 // calculate the global point corresponding to Lcoords
1366 xmap = Vmath::Dot(nIso, var.data(), 1, isoParam[0].data(), 1);
1367 ymap = Vmath::Dot(nIso, var.data(), 1, isoParam[1].data(), 1);
1368 zmap = Vmath::Dot(nIso, var.data(), 1, isoParam[2].data(), 1);
1369
1370 tmp[0] = coords[0] - xmap;
1371 tmp[1] = coords[1] - ymap;
1372 tmp[2] = coords[2] - zmap;
1373
1374 res = tmp[0] * tmp[0] + tmp[1] * tmp[1] + tmp[2] * tmp[2];
1375 if (res < ScaledTol)
1376 {
1377 m_newtonConverged = true;
1378 dist = 0.;
1379 return;
1380 }
1381
1382 // Interpolate derivative metric at Lcoords (ddx1, ddx2, ddx3)
1383 deriv[0] = isoParam[0][1] + isoParam[0][4] * Lcoords[1];
1384 deriv[1] = isoParam[0][2] + isoParam[0][4] * Lcoords[0];
1385 deriv[2] = isoParam[0][3];
1386 deriv[3] = isoParam[1][1] + isoParam[1][4] * Lcoords[1];
1387 deriv[4] = isoParam[1][2] + isoParam[1][4] * Lcoords[0];
1388 deriv[5] = isoParam[1][3];
1389 deriv[6] = isoParam[2][1] + isoParam[2][4] * Lcoords[1];
1390 deriv[7] = isoParam[2][2] + isoParam[2][4] * Lcoords[0];
1391 deriv[8] = isoParam[2][3];
1392 if (nIso >= 6)
1393 {
1394 deriv[1] += isoParam[0][5] * Lcoords[2];
1395 deriv[2] += isoParam[0][5] * Lcoords[1];
1396 deriv[4] += isoParam[1][5] * Lcoords[2];
1397 deriv[5] += isoParam[1][5] * Lcoords[1];
1398 deriv[7] += isoParam[2][5] * Lcoords[2];
1399 deriv[8] += isoParam[2][5] * Lcoords[1];
1400 }
1401 if (nIso >= 8)
1402 {
1403 deriv[0] += isoParam[0][6] * Lcoords[2] + isoParam[0][7] * var[5];
1404 deriv[1] += isoParam[0][7] * var[6];
1405 deriv[2] += isoParam[0][6] * Lcoords[0] + isoParam[0][7] * var[4];
1406 deriv[3] += isoParam[1][6] * Lcoords[2] + isoParam[1][7] * var[5];
1407 deriv[4] += isoParam[1][7] * var[6];
1408 deriv[5] += isoParam[1][6] * Lcoords[0] + isoParam[1][7] * var[4];
1409 deriv[6] += isoParam[2][6] * Lcoords[2] + isoParam[2][7] * var[5];
1410 deriv[7] += isoParam[2][7] * var[6];
1411 deriv[8] += isoParam[2][6] * Lcoords[0] + isoParam[2][7] * var[4];
1412 }
1413 DNekMatSharedPtr mat =
1415 Vmath::Vcopy(9, deriv, 1, mat->GetPtr(), 1);
1416 mat->Invert();
1417 Lcoords[0] += Vmath::Dot(3, mat->GetPtr(), 1, tmp, 1);
1418 Lcoords[1] += Vmath::Dot(3, mat->GetPtr() + 3, 1, tmp, 1);
1419 Lcoords[2] += Vmath::Dot(3, mat->GetPtr() + 6, 1, tmp, 1);
1420
1421 if (!(std::isfinite(Lcoords[0]) && std::isfinite(Lcoords[1]) &&
1422 std::isfinite(Lcoords[2])) ||
1423 fabs(Lcoords[0]) > LcoordDiv || fabs(Lcoords[1]) > LcoordDiv ||
1424 fabs(Lcoords[2]) > LcoordDiv)
1425 {
1426 std::ostringstream ss;
1427 ss << "Iteration has diverged in NewtonIterationForLocCoord in "
1428 "element "
1429 << m_geom->GetGlobalID();
1430 WARNINGL1(false, ss.str());
1431 return;
1432 }
1433 }
1434
1435 // Not converged: Lcoords does not map to coords, so report the residual
1436 // rather than claiming a match
1437 var[1] = Lcoords[0];
1438 var[2] = Lcoords[1];
1439 var[3] = Lcoords[2];
1440 var[4] = Lcoords[0] * Lcoords[1];
1441 var[5] = Lcoords[1] * Lcoords[2];
1442 var[6] = Lcoords[0] * Lcoords[2];
1443 var[7] = var[4] * Lcoords[2];
1444 for (int i = 0; i < 3; ++i)
1445 {
1446 tmp[i] =
1447 coords[i] - Vmath::Dot(nIso, var.data(), 1, isoParam[i].data(), 1);
1448 }
1449 dist = std::sqrt(tmp[0] * tmp[0] + tmp[1] * tmp[1] + tmp[2] * tmp[2]);
1450
1451 std::ostringstream ss;
1452
1453 ss << "Reached MaxIterations (" << MaxIterations
1454 << ") in Newton iteration ";
1455
1456 WARNINGL1(cnt < MaxIterations, ss.str());
1457}
1458
1460 const Array<OneD, const NekDouble> &coords,
1461 const Array<OneD, const NekDouble> &ptsx,
1462 const Array<OneD, const NekDouble> &ptsy,
1464 NekDouble &dist)
1465{
1466 // maximum iterations for convergence
1467 const int MaxIterations = NekConstants::kNewtonIterations;
1468 // |x-xp|^2 < EPSILON error tolerance
1469 const NekDouble Tol = 1.e-8;
1470 // |r,s| > LcoordDIV stop the search
1471 const NekDouble LcoordDiv = 15.0;
1472
1475 m_geom->GenGeomFactors(ptsKeys)->GetJac();
1476
1477 NekDouble ScaledTol =
1478 Vmath::Vsum(Jac.size(), Jac, 1) / ((NekDouble)Jac.size());
1479 ScaledTol *= Tol;
1480
1481 NekDouble xmap, ymap, zmap, F1, F2, F3;
1482
1483 NekDouble derx_1, derx_2, derx_3, dery_1, dery_2, dery_3, derz_1, derz_2,
1484 derz_3, jac;
1485
1486 // save intiial guess for later reference if required.
1487 NekDouble init0 = Lcoords[0], init1 = Lcoords[1], init2 = Lcoords[2];
1488
1489 Array<OneD, NekDouble> DxD1(ptsx.size());
1490 Array<OneD, NekDouble> DxD2(ptsx.size());
1491 Array<OneD, NekDouble> DxD3(ptsx.size());
1492 Array<OneD, NekDouble> DyD1(ptsx.size());
1493 Array<OneD, NekDouble> DyD2(ptsx.size());
1494 Array<OneD, NekDouble> DyD3(ptsx.size());
1495 Array<OneD, NekDouble> DzD1(ptsx.size());
1496 Array<OneD, NekDouble> DzD2(ptsx.size());
1497 Array<OneD, NekDouble> DzD3(ptsx.size());
1498
1499 // Ideally this will be stored in m_geomfactors
1500 m_geom->GetXmap()->PhysDeriv(ptsx, DxD1, DxD2, DxD3);
1501 m_geom->GetXmap()->PhysDeriv(ptsy, DyD1, DyD2, DyD3);
1502 m_geom->GetXmap()->PhysDeriv(ptsz, DzD1, DzD2, DzD3);
1503
1504 int cnt = 0;
1507
1508 F1 = F2 = F3 = 2000; // Starting value of Function
1509 NekDouble resid = sqrt(F1 * F1 + F2 * F2 + F3 * F3);
1510 m_newtonConverged = false;
1511 while (cnt++ < MaxIterations)
1512 {
1513 // evaluate lagrange interpolant at Lcoords
1514 m_geom->GetXmap()->LocCoordToLocCollapsed(Lcoords, eta);
1515 I[0] = m_geom->GetXmap()->GetBasis(0)->GetI(eta);
1516 I[1] = m_geom->GetXmap()->GetBasis(1)->GetI(eta + 1);
1517 I[2] = m_geom->GetXmap()->GetBasis(2)->GetI(eta + 2);
1518
1519 // calculate the global point `corresponding to Lcoords
1520 xmap = m_geom->GetXmap()->PhysEvaluate(I, ptsx);
1521 ymap = m_geom->GetXmap()->PhysEvaluate(I, ptsy);
1522 zmap = m_geom->GetXmap()->PhysEvaluate(I, ptsz);
1523
1524 F1 = coords[0] - xmap;
1525 F2 = coords[1] - ymap;
1526 F3 = coords[2] - zmap;
1527
1528 if (F1 * F1 + F2 * F2 + F3 * F3 < ScaledTol)
1529 {
1530 resid = sqrt(F1 * F1 + F2 * F2 + F3 * F3);
1531 m_newtonConverged = true;
1532 break;
1533 }
1534
1535 // Interpolate derivative metric at Lcoords
1536 derx_1 = m_geom->GetXmap()->PhysEvaluate(I, DxD1);
1537 derx_2 = m_geom->GetXmap()->PhysEvaluate(I, DxD2);
1538 derx_3 = m_geom->GetXmap()->PhysEvaluate(I, DxD3);
1539 dery_1 = m_geom->GetXmap()->PhysEvaluate(I, DyD1);
1540 dery_2 = m_geom->GetXmap()->PhysEvaluate(I, DyD2);
1541 dery_3 = m_geom->GetXmap()->PhysEvaluate(I, DyD3);
1542 derz_1 = m_geom->GetXmap()->PhysEvaluate(I, DzD1);
1543 derz_2 = m_geom->GetXmap()->PhysEvaluate(I, DzD2);
1544 derz_3 = m_geom->GetXmap()->PhysEvaluate(I, DzD3);
1545
1546 jac = derx_1 * (dery_2 * derz_3 - dery_3 * derz_2) -
1547 derx_2 * (dery_1 * derz_3 - dery_3 * derz_1) +
1548 derx_3 * (dery_1 * derz_2 - dery_2 * derz_1);
1549
1550 // use analytical inverse of derivitives which are also similar to
1551 // those of metric factors.
1552 Lcoords[0] =
1553 Lcoords[0] +
1554 ((dery_2 * derz_3 - dery_3 * derz_2) * (coords[0] - xmap) -
1555 (derx_2 * derz_3 - derx_3 * derz_2) * (coords[1] - ymap) +
1556 (derx_2 * dery_3 - derx_3 * dery_2) * (coords[2] - zmap)) /
1557 jac;
1558
1559 Lcoords[1] =
1560 Lcoords[1] -
1561 ((dery_1 * derz_3 - dery_3 * derz_1) * (coords[0] - xmap) -
1562 (derx_1 * derz_3 - derx_3 * derz_1) * (coords[1] - ymap) +
1563 (derx_1 * dery_3 - derx_3 * dery_1) * (coords[2] - zmap)) /
1564 jac;
1565
1566 Lcoords[2] =
1567 Lcoords[2] +
1568 ((dery_1 * derz_2 - dery_2 * derz_1) * (coords[0] - xmap) -
1569 (derx_1 * derz_2 - derx_2 * derz_1) * (coords[1] - ymap) +
1570 (derx_1 * dery_2 - derx_2 * dery_1) * (coords[2] - zmap)) /
1571 jac;
1572
1573 if (!(std::isfinite(Lcoords[0]) && std::isfinite(Lcoords[1]) &&
1574 std::isfinite(Lcoords[2])))
1575 {
1576 dist = 1e16;
1577 std::ostringstream ss;
1578 ss << "nan or inf found in NewtonIterationForLocCoord in element "
1579 << m_geom->GetGlobalID();
1580 WARNINGL1(false, ss.str());
1581 return;
1582 }
1583 if (fabs(Lcoords[0]) > LcoordDiv || fabs(Lcoords[1]) > LcoordDiv ||
1584 fabs(Lcoords[2]) > LcoordDiv)
1585 {
1586 break; // lcoords have diverged so stop iteration
1587 }
1588 }
1589
1590 m_geom->GetXmap()->LocCoordToLocCollapsed(Lcoords, eta);
1591 if (ClampLocCoords(eta, m_geom->GetShapeDim(), 0.))
1592 {
1593 I[0] = m_geom->GetXmap()->GetBasis(0)->GetI(eta);
1594 I[1] = m_geom->GetXmap()->GetBasis(1)->GetI(eta + 1);
1595 I[2] = m_geom->GetXmap()->GetBasis(2)->GetI(eta + 2);
1596 // calculate the global point corresponding to Lcoords
1597 xmap = m_geom->GetXmap()->PhysEvaluate(I, ptsx);
1598 ymap = m_geom->GetXmap()->PhysEvaluate(I, ptsy);
1599 zmap = m_geom->GetXmap()->PhysEvaluate(I, ptsz);
1600 F1 = coords[0] - xmap;
1601 F2 = coords[1] - ymap;
1602 F3 = coords[2] - zmap;
1603 dist = sqrt(F1 * F1 + F2 * F2 + F3 * F3);
1604 }
1605 else if (m_newtonConverged)
1606 {
1607 dist = 0.;
1608 }
1609 else
1610 {
1611 // Inside the element but not converged: Lcoords does not map to
1612 // coords, so report the residual rather than claiming a match
1613 I[0] = m_geom->GetXmap()->GetBasis(0)->GetI(eta);
1614 I[1] = m_geom->GetXmap()->GetBasis(1)->GetI(eta + 1);
1615 I[2] = m_geom->GetXmap()->GetBasis(2)->GetI(eta + 2);
1616 xmap = m_geom->GetXmap()->PhysEvaluate(I, ptsx);
1617 ymap = m_geom->GetXmap()->PhysEvaluate(I, ptsy);
1618 zmap = m_geom->GetXmap()->PhysEvaluate(I, ptsz);
1619 F1 = coords[0] - xmap;
1620 F2 = coords[1] - ymap;
1621 F3 = coords[2] - zmap;
1622 dist = std::sqrt(F1 * F1 + F2 * F2 + F3 * F3);
1623 resid = dist;
1624 }
1625
1626 if (cnt >= MaxIterations)
1627 {
1628 Array<OneD, NekDouble> collCoords(3);
1629 m_geom->GetXmap()->LocCoordToLocCollapsed(Lcoords, collCoords);
1630
1631 // if coordinate is inside element dump error!
1632 if ((collCoords[0] >= -1.0 && collCoords[0] <= 1.0) &&
1633 (collCoords[1] >= -1.0 && collCoords[1] <= 1.0) &&
1634 (collCoords[2] >= -1.0 && collCoords[2] <= 1.0))
1635 {
1636 std::ostringstream ss;
1637
1638 ss << "Reached MaxIterations (" << MaxIterations
1639 << ") in Newton iteration ";
1640 ss << "Init value (" << std::setprecision(4) << init0 << ","
1641 << init1 << "," << init2 << ") ";
1642 ss << "Fin value (" << Lcoords[0] << "," << Lcoords[1] << ","
1643 << Lcoords[2] << ") ";
1644 ss << "Resid = " << resid
1645 << " Tolerance = " << std::sqrt(ScaledTol);
1646
1647 WARNINGL1(cnt < MaxIterations, ss.str());
1648 }
1649 }
1650}
1651
1654{
1655 NekDouble dist = std::numeric_limits<double>::max();
1656 Array<OneD, NekDouble> tmpcoords(3);
1657 tmpcoords[0] = coords[0];
1658 tmpcoords[1] = coords[1];
1659 tmpcoords[2] = coords[2];
1660 if (m_geom->CalcGeomType() == eRegular)
1661 {
1662 tmpcoords[0] -= m_iso.m_coeff[0][0];
1663 tmpcoords[1] -= m_iso.m_coeff[1][0];
1664 tmpcoords[2] -= m_iso.m_coeff[2][0];
1665 Lcoords[0] = m_invIsoParam[0][0] * tmpcoords[0] +
1666 m_invIsoParam[0][1] * tmpcoords[1] +
1667 m_invIsoParam[0][2] * tmpcoords[2];
1668 Lcoords[1] = m_invIsoParam[1][0] * tmpcoords[0] +
1669 m_invIsoParam[1][1] * tmpcoords[1] +
1670 m_invIsoParam[1][2] * tmpcoords[2];
1671 Lcoords[2] = m_invIsoParam[2][0] * tmpcoords[0] +
1672 m_invIsoParam[2][1] * tmpcoords[1] +
1673 m_invIsoParam[2][2] * tmpcoords[2];
1674 }
1675 else if (m_straightEdge && m_iso.m_nCoeff > 0)
1676 {
1677 // Straight-sided, and the shape has a polynomial form of its mapping
1678 // to iterate on. A pyramid with a non-parallelogram base has not.
1679 ClampLocCoords(Lcoords, m_geom->GetShapeDim(), 0.);
1680 NewtonIterationForLocCoord(tmpcoords, Lcoords, dist);
1681 }
1682 else
1683 {
1684 m_geom->FillGeom();
1685 // Determine nearest point of coords to values in m_geom->GetXmap()
1686 int npts = m_geom->GetXmap()->GetTotPoints();
1687 Array<OneD, NekDouble> ptsx(npts), ptsy(npts), ptsz(npts);
1688 Array<OneD, NekDouble> tmp1(npts), tmp2(npts);
1689
1690 m_geom->GetXmap()->BwdTrans(m_geom->GetCoeffs(0), ptsx);
1691 m_geom->GetXmap()->BwdTrans(m_geom->GetCoeffs(1), ptsy);
1692 m_geom->GetXmap()->BwdTrans(m_geom->GetCoeffs(2), ptsz);
1693
1697
1698 // guess the first local coords based on nearest point
1699 Vmath::Sadd(npts, -coords[0], ptsx, 1, tmp1, 1);
1700 Vmath::Vmul(npts, tmp1, 1, tmp1, 1, tmp1, 1);
1701 Vmath::Sadd(npts, -coords[1], ptsy, 1, tmp2, 1);
1702 Vmath::Vvtvp(npts, tmp2, 1, tmp2, 1, tmp1, 1, tmp1, 1);
1703 Vmath::Sadd(npts, -coords[2], ptsz, 1, tmp2, 1);
1704 Vmath::Vvtvp(npts, tmp2, 1, tmp2, 1, tmp1, 1, tmp1, 1);
1705
1706 int min_i = Vmath::Imin(npts, tmp1, 1);
1707
1708 // Get Local coordinates
1709 int qa = za.size(), qb = zb.size();
1710
1711 Array<OneD, NekDouble> eta(3, 0.);
1712 eta[2] = zc[min_i / (qa * qb)];
1713 min_i = min_i % (qa * qb);
1714 eta[1] = zb[min_i / qa];
1715 eta[0] = za[min_i % qa];
1716 m_geom->GetXmap()->LocCollapsedToLocCoord(eta, Lcoords);
1717
1718 // Perform newton iteration to find local coordinates
1719 NewtonIterationForLocCoord(coords, ptsx, ptsy, ptsz, Lcoords, dist);
1720 }
1721 return dist;
1722}
1723
1725{
1726 DNekMatSharedPtr mat =
1728 for (int i = 0; i < 3; ++i)
1729 {
1730 for (int j = 0; j < 3; ++j)
1731 {
1732
1733 mat->SetValue(i, j, m_iso.m_coeff[i][j + 1]);
1734 }
1735 }
1736 mat->Invert();
1737 for (int i = 0; i < 3; ++i)
1738 {
1739 for (int j = 0; j < 3; ++j)
1740 {
1741 m_invIsoParam[i][j] = mat->GetValue(i, j);
1742 }
1743 }
1744}
1745
1746} // namespace Nektar::SpatialDomains
#define WARNINGL1(condition, msg)
#define ASSERTL0(condition, msg)
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
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
LibUtilities::ShapeType GetShapeType(void)
The shape this object is, as a LibUtilities::ShapeType.
Definition Geometry.h:460
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 GetGlobalID(void) const
Get the ID of this object.
Definition Geometry.h:465
PointGeom * GetVertex(int i) const
Get vertex i of this object.
Definition Geometry.h:495
int GetCoordim() const
Dimension of the space this object is embedded in.
Definition Geometry.h:447
void FillGeom()
Gather the coefficients of from this object's boundary, unless that has already been done.
Definition Geometry.h:591
int GetNumVerts() const
Get the number of vertices of this object.
Definition Geometry.h:520
bool HasLinearXmap()
True if every basis is linear.
Definition Geometry.cpp:109
Geometry1D * GetEdge(int i) const
Get edge i of this object.
Definition Geometry.h:500
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
GeomFactorsUniquePtr GenGeomFactors(LibUtilities::PointsKeyVector &keyTgt)
Build the #GeomFactors for this object at the quadrature points keyTgt.
Definition Geometry.h:565
NekDouble v_FindDistance(const Array< OneD, const NekDouble > &xs, Array< OneD, NekDouble > &xi) override
NekDouble v_GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords) override
int v_AllLeftCheck(const Array< OneD, const NekDouble > &gloCoord, NekDouble &edgeDist) override
NekDouble v_FindDistance(const Array< OneD, const NekDouble > &xs, Array< OneD, NekDouble > &xi) override
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 &dist)
NekDouble v_GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords) override
void SolveStraightEdgeQuad(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords)
void NewtonIterationForLocCoord(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords, NekDouble &dist)
NekDouble v_GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords) override
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 element...
bool m_edgeNormalBuilt
2D only: inward edge normals used to reject points outside straight-sided elements....
virtual NekDouble v_GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords)
IsoParam m_iso
The shape's isoparametric coefficients. The straight-edged 2D solve rewrites them into a factored for...
NekDouble FindDistance(const Array< OneD, const NekDouble > &xs, Array< OneD, NekDouble > &xi)
Geometry * m_geom
The geometry being searched. Not owned.
std::array< std::array< NekDouble, 2 >, kMaxGeomVerts > m_edgeNormal
static std::unique_ptr< GeometryLocator > Create(Geometry *geom)
Build the locator appropriate to geom's dimension, with its cache already populated.
std::array< NekDouble, 6 > GetBoundingBox()
Generates the bounding box for the element.
bool ContainsPoint(const Array< OneD, const NekDouble > &gloCoord, NekDouble tol=0.0)
Determine whether the element contains a Cartesian coordinate.
NekDouble GetLocCoords(const Array< OneD, const NekDouble > &coords, Array< OneD, NekDouble > &Lcoords)
Determine the local collapsed coordinates corresponding to a given Cartesian coordinate.
bool MinMaxCheck(const Array< OneD, const NekDouble > &gloCoord)
Check if given global coord is within the BoundingBox of the element.
void Build()
Populate the cache. Called by Create() once the concrete type exists, since it dispatches virtually.
std::array< NekDouble, 6 > m_boundingBox
int m_straightEdge
Non-zero if every basis is linear. Bits 1 and 2 are used by the 2D straight-edged solve to record wh...
std::array< std::array< NekDouble, kMaxGeomDim >, kMaxGeomDim > m_invIsoParam
Inverse of the linear part of m_iso, for regular elements.
virtual NekDouble v_FindDistance(const Array< OneD, const NekDouble > &xs, Array< OneD, NekDouble > &xi)
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....
bool m_hasBoundingBox
Element bounding box as (xmin, ymin, zmin, xmax, ymax, zmax).
std::array< bool, kMaxGeomVerts > m_hasEdgeNormal
virtual int v_AllLeftCheck(const Array< OneD, const NekDouble > &gloCoord, NekDouble &edgeDist)
bool m_newtonConverged
Whether the Newton iteration of the last GetLocCoords call converged, i.e. whether its local coordina...
void GetCoords(NekDouble &x, NekDouble &y, NekDouble &z)
Definition PointGeom.cpp:84
const LibUtilities::BasisSharedPtr & GetBasis(int dir) const
This function gets the shared point to basis in the dir direction.
int GetTotPoints() const
This function returns the total number of quadrature points used in the element.
const LibUtilities::PointsKeyVector GetPointsKeys() const
void LocCoordToLocCollapsed(const Array< OneD, const NekDouble > &xi, Array< OneD, NekDouble > &eta)
Convert local cartesian coordinate xi into local collapsed coordinates eta.
const Array< OneD, const NekDouble > & GetPoints(const int dir) const
This function returns a pointer to the array containing the quadrature points in dir direction.
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.
void LocCollapsedToLocCoord(const Array< OneD, const NekDouble > &eta, Array< OneD, NekDouble > &xi)
Convert local collapsed coordinates eta into local cartesian coordinate xi.
void PhysDeriv(const Array< OneD, const NekDouble > &inarray, Array< OneD, NekDouble > &out_d0, Array< OneD, NekDouble > &out_d1=NullNekDouble1DArray, Array< OneD, NekDouble > &out_d2=NullNekDouble1DArray)
std::vector< PointsKey > PointsKeyVector
Definition Points.h:309
@ beta
Gauss Radau pinned at x=-1,.
Definition PointsType.h:59
static const NekDouble kGeomFactorsTol
static const NekDouble kFindDistanceMin
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.
std::unique_ptr< GeometryLocator > GeometryLocatorUniquePtr
std::shared_ptr< DNekMat > DNekMatSharedPtr
void Vmul(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x*y.
Definition Vmath.hpp:74
void Vvtvp(int n, const T *w, const int incw, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
vvtvp (vector times vector plus vector): z = w*x + y
Definition Vmath.hpp:368
T Vsum(int n, const T *x, const int incx)
Subtract return sum(x)
Definition Vmath.hpp:611
T Dot(int n, const T *w, const T *x)
dot product
Definition Vmath.hpp:764
int Imin(int n, const T *x, const int incx)
Return the index of the minimum element in x.
Definition Vmath.hpp:707
void Sadd(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Add vector y = alpha + x.
Definition Vmath.hpp:196
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition Vmath.hpp:828
scalarT< T > max(scalarT< T > lhs, scalarT< T > rhs)
Definition scalar.hpp:304
scalarT< T > min(scalarT< T > lhs, scalarT< T > rhs)
Definition scalar.hpp:299
scalarT< T > sqrt(scalarT< T > in)
Definition scalar.hpp:289
std::array< int, kMaxGeomDim > m_manifold
2D only: coordinate directions spanning the manifold.
int m_nCoeff
Coefficients per coordinate direction. Only the leading m_nCoeff entries of each row are meaningful,...
std::array< std::array< NekDouble, kMaxGeomIsoCoeff >, kMaxGeomDim > m_coeff