https://mooseframework.inl.gov
Loading...
Searching...
No Matches
FindContactPoint.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10// Moose
11#include "DenseMatrix.h"
12#include "FindContactPoint.h"
13#include "LineSegment.h"
14#include "PenetrationInfo.h"
15
16// libMesh
17#include "libmesh/fe_base.h"
18#include "libmesh/boundary_info.h"
19#include "libmesh/elem.h"
20#include "libmesh/plane.h"
21#include "libmesh/fe_interface.h"
22#include "libmesh/dense_vector.h"
23#include "libmesh/fe_base.h"
24#include "libmesh/vector_value.h"
25
26// C++
27#include <cstring> // for "Jacobian" exception test
28
29namespace Moose
30{
31
50void
52 FEBase * fe_elem,
53 FEBase * fe_side,
54 FEType & /* fe_side_type */,
55 const libMesh::Point & secondary_point,
56 bool start_with_centroid,
57 const Real tangential_tolerance,
58 bool & contact_point_on_side,
59 bool & search_succeeded)
60{
61 // Default to true and we'll switch on failures
62 search_succeeded = true;
63
64 const Elem * primary_elem = p_info._elem;
65
66 const Elem * side = p_info._side;
67
68 // Contact problem dimension; exceeds the element dimension for a primary surface element
69 unsigned int dim = side->dim() + 1;
70
71 const std::vector<libMesh::Point> & phys_point = fe_side->get_xyz();
72
73 const std::vector<RealGradient> & dxyz_dxi = fe_side->get_dxyzdxi();
74 const std::vector<RealGradient> & d2xyz_dxi2 = fe_side->get_d2xyzdxi2();
75 const std::vector<RealGradient> & d2xyz_dxieta = fe_side->get_d2xyzdxideta();
76
77 const std::vector<RealGradient> & dxyz_deta = fe_side->get_dxyzdeta();
78 const std::vector<RealGradient> & d2xyz_deta2 = fe_side->get_d2xyzdeta2();
79 const std::vector<RealGradient> & d2xyz_detaxi = fe_side->get_d2xyzdxideta();
80
81 if (dim == 1)
82 {
83 const Node * nearest_node = side->node_ptr(0);
84 p_info._closest_point = *nearest_node;
85 p_info._closest_point_ref =
86 primary_elem->master_point(primary_elem->get_node_index(nearest_node));
87 std::vector<libMesh::Point> elem_points = {p_info._closest_point_ref};
88
89 const std::vector<RealGradient> & elem_dxyz_dxi = fe_elem->get_dxyzdxi();
90
91 fe_elem->reinit(primary_elem, &elem_points);
92 fe_side->reinit(side, &elem_points);
93
94 p_info._normal = elem_dxyz_dxi[0];
95 if (nearest_node->id() == primary_elem->node_id(0))
96 p_info._normal *= -1.0;
97 p_info._normal /= p_info._normal.norm();
98
99 libMesh::Point from_secondary_to_closest = p_info._closest_point - secondary_point;
100 p_info._distance = from_secondary_to_closest * p_info._normal;
101 libMesh::Point tangential = from_secondary_to_closest - p_info._distance * p_info._normal;
102 p_info._tangential_distance = tangential.norm();
103 p_info._dxyzdxi = dxyz_dxi;
104 p_info._dxyzdeta = dxyz_deta;
105 p_info._d2xyzdxideta = d2xyz_dxieta;
106 p_info._side_phi = fe_side->get_phi();
107 p_info._side_grad_phi = fe_side->get_dphi();
108 contact_point_on_side = true;
109 return;
110 }
111
112 libMesh::Point ref_point;
113
114 if (start_with_centroid)
115 ref_point = FEMap::inverse_map(dim - 1, side, side->vertex_average(), TOLERANCE, false);
116 else
117 ref_point = p_info._closest_point_ref;
118
119 std::vector<libMesh::Point> points = {ref_point};
120 fe_side->reinit(side, &points);
121 RealGradient d = secondary_point - phys_point[0];
122
123 Real update_size = std::numeric_limits<Real>::max();
124
125 // Least squares
126 for (unsigned int it = 0; it < 3 && update_size > TOLERANCE * 1e3; ++it)
127 {
128 DenseMatrix<Real> jac(dim - 1, dim - 1);
129 jac(0, 0) = -(dxyz_dxi[0] * dxyz_dxi[0]);
130
131 if (dim - 1 == 2)
132 {
133 jac(1, 0) = -(dxyz_dxi[0] * dxyz_deta[0]);
134 jac(0, 1) = -(dxyz_deta[0] * dxyz_dxi[0]);
135 jac(1, 1) = -(dxyz_deta[0] * dxyz_deta[0]);
136 }
137
138 DenseVector<Real> rhs(dim - 1);
139 rhs(0) = dxyz_dxi[0] * d;
140
141 if (dim - 1 == 2)
142 rhs(1) = dxyz_deta[0] * d;
143
144 DenseVector<Real> update(dim - 1);
145 jac.lu_solve(rhs, update);
146
147 ref_point(0) -= update(0);
148
149 if (dim - 1 == 2)
150 ref_point(1) -= update(1);
151
152 points[0] = ref_point;
153 fe_side->reinit(side, &points);
154 d = secondary_point - phys_point[0];
155
156 update_size = update.l2_norm();
157 }
158
159 update_size = std::numeric_limits<Real>::max();
160
161 unsigned nit = 0;
162
163 // Newton Loop
164 const auto max_newton_its = 25;
165 const auto tolerance_newton = 1e3 * TOLERANCE * TOLERANCE;
166 for (; nit < max_newton_its && update_size > tolerance_newton; nit++)
167 {
168 d = secondary_point - phys_point[0];
169
170 DenseMatrix<Real> jac(dim - 1, dim - 1);
171 jac(0, 0) = (d2xyz_dxi2[0] * d) - (dxyz_dxi[0] * dxyz_dxi[0]);
172
173 if (dim - 1 == 2)
174 {
175 jac(1, 0) = (d2xyz_dxieta[0] * d) - (dxyz_dxi[0] * dxyz_deta[0]);
176
177 jac(0, 1) = (d2xyz_detaxi[0] * d) - (dxyz_deta[0] * dxyz_dxi[0]);
178 jac(1, 1) = (d2xyz_deta2[0] * d) - (dxyz_deta[0] * dxyz_deta[0]);
179 }
180
181 DenseVector<Real> rhs(dim - 1);
182 rhs(0) = -dxyz_dxi[0] * d;
183
184 if (dim - 1 == 2)
185 rhs(1) = -dxyz_deta[0] * d;
186
187 DenseVector<Real> update(dim - 1);
188 jac.lu_solve(rhs, update);
189
190 // Improvised line search in case the update is too large and gets out of the element so bad
191 // that we cannot reinit at the new point
192 Real mult = 1;
193 while (true)
194 {
195 try
196 {
197 ref_point(0) += mult * update(0);
198
199 if (dim - 1 == 2)
200 ref_point(1) += mult * update(1);
201
202 points[0] = ref_point;
203 fe_side->reinit(side, &points);
204 d = secondary_point - phys_point[0];
205
206 // we don't multiply by 'mult' because it is used for convergence
207 update_size = update.l2_norm();
208 break;
209 }
210 // libMesh might throw here if we hit a zero/negative Jacobian
211 catch (std::exception & e)
212 {
213 // Make sure this *is* just a bad mapping Jacobian
214 if (!strstr(e.what(), "Jacobian") && !strstr(e.what(), "det != 0"))
215 throw;
216
217 ref_point(0) -= mult * update(0);
218 if (dim - 1 == 2)
219 ref_point(1) -= mult * update(1);
220
221 mult *= 0.5;
222 if (mult < 1e-6)
223 {
224#ifndef NDEBUG
225 mooseWarning("We could not solve for the contact point.", e.what());
226#endif
227 update_size = update.l2_norm();
228 d = (secondary_point - phys_point[0]) * mult;
229 break;
230 }
231 }
232 }
233 // We failed the line search, make sure to trigger the error
234 if (mult < 1e-6)
235 {
236 nit = max_newton_its;
237 update_size = 1;
238 break;
239 }
240 }
241
242 if (nit == max_newton_its && update_size > tolerance_newton)
243 {
244 search_succeeded = false;
245#ifndef NDEBUG
246 const auto initial_point =
247 start_with_centroid ? side->vertex_average() : ref_point = p_info._closest_point_ref;
248 Moose::err << "Warning! Newton solve for contact point failed to converge!\nLast update "
249 "distance was: "
250 << update_size << "\nInitial point guess: " << initial_point
251 << "\nLast considered point: " << phys_point[0]
252 << "\nThis potential contact pair (face, point) will be discarded." << std::endl;
253#endif
254 return;
255 }
256
257 p_info._closest_point_ref = ref_point;
258 p_info._closest_point = phys_point[0];
259 p_info._distance = d.norm();
260
261 if (dim - 1 == 2)
262 {
263 p_info._normal = dxyz_dxi[0].cross(dxyz_deta[0]);
264 if (!MooseUtils::absoluteFuzzyEqual(p_info._normal.norm(), 0))
265 p_info._normal /= p_info._normal.norm();
266 }
267 else
268 {
269 p_info._normal = dxyz_dxi[0].cross(outOfPlaneNormal(*primary_elem));
270 if (std::fabs(p_info._normal.norm()) > 1e-15)
271 p_info._normal /= p_info._normal.norm();
272 }
273
274 // If the point has not penetrated the face, make the distance negative
275 const Real dot(d * p_info._normal);
276 if (dot > 0.0)
277 p_info._distance = -p_info._distance;
278
279 contact_point_on_side = side->on_reference_element(ref_point);
280
281 p_info._tangential_distance = 0.0;
282
283 if (!contact_point_on_side)
284 {
285 p_info._closest_point_on_face_ref = ref_point;
287
288 points[0] = p_info._closest_point_on_face_ref;
289 fe_side->reinit(side, &points);
290 libMesh::Point closest_point_on_face(phys_point[0]);
291
292 RealGradient off_face = closest_point_on_face - p_info._closest_point;
293 Real tangential_distance = off_face.norm();
294 p_info._tangential_distance = tangential_distance;
295 if (tangential_distance <= tangential_tolerance)
296 {
297 contact_point_on_side = true;
298 }
299 }
300
301 const std::vector<std::vector<Real>> & phi = fe_side->get_phi();
302 const std::vector<std::vector<RealGradient>> & grad_phi = fe_side->get_dphi();
303
304 points[0] = p_info._closest_point_ref;
305 fe_side->reinit(side, &points);
306
307 p_info._side_phi = phi;
308 p_info._side_grad_phi = grad_phi;
309 p_info._dxyzdxi = dxyz_dxi;
310 p_info._dxyzdeta = dxyz_deta;
311 p_info._d2xyzdxideta = d2xyz_dxieta;
312}
313
315outOfPlaneNormal(const Elem & primary_elem)
316{
317 if (primary_elem.dim() == 1)
318 return libMesh::Point(0, 0, 1);
319
320 const libMesh::Point in_plane_vector1 = primary_elem.point(1) - primary_elem.point(0);
321 const libMesh::Point in_plane_vector2 = primary_elem.point(2) - primary_elem.point(0);
322 return in_plane_vector1.cross(in_plane_vector2).unit();
323}
324
325void
327 const Elem * side,
328 std::vector<const Node *> & off_edge_nodes)
329{
330 const ElemType t(side->type());
331 off_edge_nodes.clear();
332 Real & xi = p(0);
333 Real & eta = p(1);
334
335 switch (t)
336 {
337 case EDGE2:
338 case EDGE3:
339 case EDGE4:
340 {
341 // The reference 1D element is [-1,1].
342 if (xi < -1.0)
343 {
344 xi = -1.0;
345 off_edge_nodes.push_back(side->node_ptr(0));
346 }
347 else if (xi > 1.0)
348 {
349 xi = 1.0;
350 off_edge_nodes.push_back(side->node_ptr(1));
351 }
352 break;
353 }
354
355 case TRI3:
356 case TRI6:
357 case TRI7:
358 {
359 // The reference triangle is isosceles
360 // and is bound by xi=0, eta=0, and xi+eta=1.
361
362 if (xi <= 0.0 && eta <= 0.0)
363 {
364 xi = 0.0;
365 eta = 0.0;
366 off_edge_nodes.push_back(side->node_ptr(0));
367 }
368 else if (xi > 0.0 && xi < 1.0 && eta < 0.0)
369 {
370 eta = 0.0;
371 off_edge_nodes.push_back(side->node_ptr(0));
372 off_edge_nodes.push_back(side->node_ptr(1));
373 }
374 else if (eta > 0.0 && eta < 1.0 && xi < 0.0)
375 {
376 xi = 0.0;
377 off_edge_nodes.push_back(side->node_ptr(2));
378 off_edge_nodes.push_back(side->node_ptr(0));
379 }
380 else if (xi >= 1.0 && (eta - xi) <= -1.0)
381 {
382 xi = 1.0;
383 eta = 0.0;
384 off_edge_nodes.push_back(side->node_ptr(1));
385 }
386 else if (eta >= 1.0 && (eta - xi) >= 1.0)
387 {
388 xi = 0.0;
389 eta = 1.0;
390 off_edge_nodes.push_back(side->node_ptr(2));
391 }
392 else if ((xi + eta) > 1.0)
393 {
394 Real delta = (xi + eta - 1.0) / 2.0;
395 xi -= delta;
396 eta -= delta;
397 off_edge_nodes.push_back(side->node_ptr(1));
398 off_edge_nodes.push_back(side->node_ptr(2));
399 }
400 break;
401 }
402
403 case QUAD4:
404 case QUAD8:
405 case QUAD9:
406 {
407 // The reference quadrilateral element is [-1,1]^2.
408 if (xi < -1.0)
409 {
410 xi = -1.0;
411 if (eta < -1.0)
412 {
413 eta = -1.0;
414 off_edge_nodes.push_back(side->node_ptr(0));
415 }
416 else if (eta > 1.0)
417 {
418 eta = 1.0;
419 off_edge_nodes.push_back(side->node_ptr(3));
420 }
421 else
422 {
423 off_edge_nodes.push_back(side->node_ptr(3));
424 off_edge_nodes.push_back(side->node_ptr(0));
425 }
426 }
427 else if (xi > 1.0)
428 {
429 xi = 1.0;
430 if (eta < -1.0)
431 {
432 eta = -1.0;
433 off_edge_nodes.push_back(side->node_ptr(1));
434 }
435 else if (eta > 1.0)
436 {
437 eta = 1.0;
438 off_edge_nodes.push_back(side->node_ptr(2));
439 }
440 else
441 {
442 off_edge_nodes.push_back(side->node_ptr(1));
443 off_edge_nodes.push_back(side->node_ptr(2));
444 }
445 }
446 else
447 {
448 if (eta < -1.0)
449 {
450 eta = -1.0;
451 off_edge_nodes.push_back(side->node_ptr(0));
452 off_edge_nodes.push_back(side->node_ptr(1));
453 }
454 else if (eta > 1.0)
455 {
456 eta = 1.0;
457 off_edge_nodes.push_back(side->node_ptr(2));
458 off_edge_nodes.push_back(side->node_ptr(3));
459 }
460 }
461 break;
462 }
463
464 default:
465 {
466 mooseError("Unsupported face type: ", t);
467 break;
468 }
469 }
470}
471
472} // namespace Moose
void mooseWarning(Args &&... args)
Emit a warning message with the given stringified, concatenated args.
Definition MooseError.h:345
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
Point eta
Definition MortarUtils.C:60
Point xi
Definition MortarUtils.C:59
Data structure used to hold penetration information.
std::vector< const Node * > _off_edge_nodes
const Elem * _side
std::vector< RealGradient > _d2xyzdxideta
Point _closest_point_on_face_ref
std::vector< RealGradient > _dxyzdeta
std::vector< RealGradient > _dxyzdxi
std::vector< std::vector< Real > > _side_phi
const Elem * _elem
std::vector< std::vector< RealGradient > > _side_grad_phi
RealVectorValue _normal
TypeVector< typename CompareTypes< Real, T2 >::supertype > cross(const TypeVector< T2 > &v) const
auto norm() const
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
void findContactPoint(PenetrationInfo &p_info, libMesh::FEBase *fe_elem, libMesh::FEBase *fe_side, libMesh::FEType &fe_side_type, const libMesh::Point &secondary_point, bool start_with_centroid, const Real tangential_tolerance, bool &contact_point_on_side, bool &search_succeeded)
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition Moose.h:175
void restrictPointToFace(libMesh::Point &p, const libMesh::Elem *side, std::vector< const libMesh::Node * > &off_edge_nodes)
libMesh::Point outOfPlaneNormal(const libMesh::Elem &primary_elem)