v0.16.3
Loading...
Searching...
No Matches
CgalCrackGrowth.cpp
Go to the documentation of this file.
1/** \file CgalCrackGrowth.cpp
2 * \brief Crack growth one step at a time on two bit levels.
3 *
4 * The order of a step is the order of the operations in
5 * doc/development/cgal_crack_extension_algorithm.md: close a gap, else join,
6 * else extend; a closure or an extension is followed by the notch fill,
7 * retirement of the old level and the swap of the levels.
8 */
9#include <MoFEM.hpp>
10#include "CgalCrackGrowth.hpp"
11
12namespace MoFEM {
13
15 EntityHandle crack_set, BitRefLevel source,
16 BitRefLevel output, const Range &constraints,
17 const Settings &settings)
18 : mField(m_field), crackSet(crack_set), sourceBit(source),
19 outputBit(output), constraints(constraints), options(settings) {
21}
22
25 if (!levelChanged)
27 auto &mb = mField.get_moab();
28 auto bits = mField.getInterface<BitRefManager>();
30 CHKERR bits->getEntitiesByTypeAndRefLevel(sourceBit, BitRefLevel().set(),
31 MBTET, cells);
32 Skinner skinner(&mb);
33 CHKERR skinner.find_skin(0, cells, false, skin);
34 skinEdges.clear();
35 CHKERR mb.get_adjacencies(skin, 1, false, skinEdges, moab::Interface::UNION);
36 skinVertices.clear();
37 CHKERR mb.get_connectivity(skinEdges, skinVertices, true);
39 CHKERR bits->getEntitiesByTypeAndRefLevel(sourceBit, BitRefLevel().set(),
40 MBVERTEX, vertices);
41 vertexById.clear();
42 for (auto v : vertices) {
43 int id;
44 CHKERR mb.tag_get_data(mb.globalId_tag(), &v, 1, &id);
45 vertexById[id] = v;
46 }
47 levelChanged = false;
49}
50
53 crack.clear();
54 CHKERR mField.get_moab().get_entities_by_type(crackSet, MBTRI, crack, true);
56}
57
59 Range &front) const {
62 auto &mb = mField.get_moab();
63 Range edges;
64 CHKERR mb.get_adjacencies(crack, 1, false, edges, moab::Interface::UNION);
65 front.clear();
66 for (auto edge : subtract(edges, skinEdges)) {
68 CHKERR mb.get_adjacencies(&edge, 1, 2, false, faces);
69 if (intersect(faces, crack).size() == 1)
70 front.insert(edge);
71 }
73}
74
76 auto &mb = mField.get_moab();
77 const EntityHandle *conn;
78 int count;
79 Pair ids;
80 CHK_MOAB_THROW(mb.get_connectivity(edge, conn, count, true),
81 "edge connectivity");
82 CHK_MOAB_THROW(mb.tag_get_data(mb.globalId_tag(), conn, 2, ids.data()),
83 "edge IDs");
84 std::sort(ids.begin(), ids.end());
85 return ids;
86}
87
89 const Range &front,
90 double &value) const {
93 auto &mb = mField.get_moab();
94 value = M_PI;
95 Range edges, faces;
96 CHKERR mb.get_adjacencies(&node, 1, 1, false, edges);
97 if (intersect(edges, front).size() != 2 ||
98 skinVertices.find(node) != skinVertices.end())
100 CHKERR mb.get_adjacencies(&node, 1, 2, false, faces);
101 std::array<double, 3> x;
102 CHKERR mb.get_coords(&node, 1, x.data());
103 double sector = 0;
104 for (auto face : intersect(faces, crack)) {
105 const EntityHandle *conn;
106 int count;
107 CHKERR mb.get_connectivity(face, conn, count, true);
108 std::array<std::array<double, 3>, 2> d;
109 int k = 0;
110 for (int n = 0; n != 3; ++n)
111 if (conn[n] != node) {
112 CHKERR mb.get_coords(&conn[n], 1, d[k].data());
113 for (int i = 0; i != 3; ++i)
114 d[k][i] -= x[i];
115 ++k;
116 }
117 const std::array<double, 3> c{d[0][1] * d[1][2] - d[0][2] * d[1][1],
118 d[0][2] * d[1][0] - d[0][0] * d[1][2],
119 d[0][0] * d[1][1] - d[0][1] * d[1][0]};
120 sector +=
121 std::atan2(std::sqrt(c[0] * c[0] + c[1] * c[1] + c[2] * c[2]),
122 d[0][0] * d[1][0] + d[0][1] * d[1][1] + d[0][2] * d[1][2]);
123 }
124 value = 2 * M_PI - sector;
126}
127
129 EntityHandle &edge) const {
132 edge = 0;
133 const auto a = vertexById.find(pair[0]), b = vertexById.find(pair[1]);
134 if (a == vertexById.end() || b == vertexById.end())
136 const EntityHandle conn[] = {a->second, b->second};
137 Range candidates;
138 CHKERR mField.get_moab().get_adjacencies(conn, 2, 1, false, candidates,
139 moab::Interface::INTERSECT);
140 candidates = intersect(candidates, front);
141 if (candidates.size() == 1)
142 edge = candidates.front();
144}
145
147CgalCrackGrowth::change(Kind operation, const Pair &pair, EntityHandle edge,
148 const Range &crack, const Direction &direction,
150 bool &changed, Step &step) {
152 changed = false;
153 auto bridge = mField.getInterface<CGALInterface>();
154 auto &crack_extension = bridge->getCrackExtension();
155 auto &mb = mField.get_moab();
156 std::array<double, 3> d;
157 CHKERR direction(edge, crack, d);
160 Range entities, closures;
161 EntityHandle face = 0;
162 if (operation == Kind::Closure)
163 CHKERR crack_extension.closeCrackGap(
165 entities, face, &candidate, &report, &closures);
166 else if (operation == Kind::Join)
167 CHKERR crack_extension.joinCrack(sourceBit, outputBit, crack, constraints,
168 {{edge, d}}, settings, entities, face,
170 else
171 CHKERR crack_extension.extendCrack(sourceBit, outputBit, crack, constraints,
172 {{edge, d}}, settings, entities, face,
174 if (!face) {
175 // Only an empty structural target set is independent of the caller's
176 // direction. Angle-dependent joining failures must be retried when a
177 // mechanics solve changes the requested direction.
178 if (operation == Kind::Join && !settings.joinGapsOnly &&
179 report.candidatesTried == 0)
180 failedJoins.insert(pair);
181 MOFEM_LOG("WORLD", Sev::verbose)
182 << "Edge (" << pair[0] << ", " << pair[1]
183 << ") has no candidate: tried " << report.candidatesTried
184 << ", rejected behind/angle/move " << report.rejectedBehind << '/'
185 << report.rejectedAngle << '/' << report.rejectedMove
186 << ", inversion/quality " << report.rejectedInversion << '/'
187 << report.rejectedQuality << ", bridging/pocket/fixed/topology "
188 << report.rejectedBridging << '/' << report.rejectedPocket << '/'
189 << report.rejectedFixed << '/' << report.rejectedTopology
190 << ", open corner/pinch " << report.rejectedOpenCorner << '/'
191 << report.rejectedPinch;
193 }
194 changed = true;
196 ->updateAllMeshsetsByEntitiesChildren(outputBit);
197 CHKERR mb.add_entities(crackSet, &face, 1);
198 CHKERR mb.add_entities(crackSet, closures);
199 Range added(face, face);
200 added.merge(closures);
201 Range filled;
202 if (options.fill) {
203 Range current;
204 CHKERR crackFaces(current);
205 CHKERR crack_extension.crackFill(outputBit, current, constraints, filled);
206 CHKERR mb.add_entities(crackSet, filled);
207 added.merge(filled);
208 }
209 CHKERR bridge->retireLevel(sourceBit);
210 // Entity deletion retags the shared channel; restore it.
211 MOFEM_LOG_CHANNEL("WORLD");
212 std::swap(sourceBit, outputBit);
213 levelChanged = true;
215 Range edges;
216 CHKERR mb.get_adjacencies(added, 1, false, edges, moab::Interface::UNION);
217 for (auto e : edges)
218 step.addedEdges.push_back(edgeIds(e));
219 step.kind = operation;
220 step.edge = pair;
221 step.candidate = candidate;
222 step.closures = closures.size();
223 step.filled = filled.size();
225}
226
229 if (failedClosures.empty() && failedGapJoins.empty() && failedJoins.empty())
231 auto &mb = mField.get_moab();
232 auto bits = mField.getInterface<BitRefManager>();
234 CHKERR mb.get_connectivity(faces, vertices, true);
235 for (int r = 0; r != 2 * options.extension.ringLimit; ++r) {
236 Range cells;
237 CHKERR mb.get_adjacencies(vertices, 3, false, cells,
238 moab::Interface::UNION);
239 CHKERR bits->filterEntitiesByRefLevel(sourceBit, BitRefLevel().set(),
240 cells);
241 CHKERR mb.get_connectivity(cells, vertices, true);
242 }
243 std::vector<int> ids(vertices.size());
244 CHKERR mb.tag_get_data(mb.globalId_tag(), vertices, ids.data());
245 const std::set<int> near(ids.begin(), ids.end());
247 for (auto it = failed->begin(); it != failed->end();)
248 it = near.count((*it)[0]) || near.count((*it)[1]) ? failed->erase(it)
249 : std::next(it);
251}
252
255 step = Step();
256 if (!options.join)
259 Range crack, front;
261 CHKERR frontEdges(crack, front);
262 std::vector<std::pair<double, Pair>> sharp;
263 for (auto edge : front) {
264 const EntityHandle *conn;
265 int count;
266 CHKERR mField.get_moab().get_connectivity(edge, conn, count, true);
267 double a, b;
268 CHKERR opening(conn[0], crack, front, a);
269 CHKERR opening(conn[1], crack, front, b);
270 sharp.emplace_back(std::min(a, b), edgeIds(edge));
271 }
272 std::sort(sharp.begin(), sharp.end());
273 std::vector<Pair> edges;
274 for (const auto &entry : sharp)
275 edges.push_back(entry.second);
276 CHKERR closeAndJoin(edges, direction, step);
278}
279
280MoFEMErrorCode CgalCrackGrowth::closeAndJoin(const std::vector<Pair> &edges,
281 const Direction &direction,
282 Step &step) {
284 step = Step();
285 if (!options.join)
287 Range crack, front;
289 CHKERR frontEdges(crack, front);
290 std::vector<EntityHandle> handles;
291 std::vector<int> ranks;
292 for (std::size_t i = 0; i != edges.size(); ++i) {
293 EntityHandle edge;
294 CHKERR resolve(edges[i], front, edge);
295 if (edge) {
296 handles.push_back(edge);
297 ranks.push_back(i);
298 } else
299 step.dropped.push_back(i);
300 }
301 if (handles.empty())
303 auto &mb = mField.get_moab();
304 auto &bridge = crackExtension();
305 for (bool gaps_only : {true, false}) {
306 Range existing;
307 std::vector<int> categories;
308 EntityHandle selected = 0;
309 CHKERR bridge.crackJoin(sourceBit, crack, constraints, existing,
310 &categories, handles, &selected, gaps_only);
311 for (std::size_t i = 0; i != handles.size(); ++i) {
312 const auto edge = handles[i];
313 const auto &pair = edges[ranks[i]];
314 double opening_angle = 0;
315 if (gaps_only) {
316 const EntityHandle *conn;
317 int count;
318 CHKERR mb.get_connectivity(edge, conn, count, true);
319 double a, b;
320 CHKERR opening(conn[0], crack, front, a);
321 CHKERR opening(conn[1], crack, front, b);
322 opening_angle = std::min(a, b);
323 }
324 if (edge == selected) {
325 CHKERR mb.add_entities(crackSet, existing);
326 CHKERR forgetClosuresNear(existing);
327 step.kind = gaps_only ? Kind::Closure : Kind::Join;
328 step.joinCategories = std::move(categories);
329 step.edge = pair;
330 step.extended = ranks[i];
331 step.opening = opening_angle;
333 }
335 settings.joinGapsOnly = gaps_only;
336 if (gaps_only && !failedClosures.count(pair)) {
337 bool changed = false;
338 CHKERR change(Kind::Closure, pair, edge, crack, direction, settings,
339 changed, step);
340 if (changed) {
341 step.extended = ranks[i];
342 step.opening = opening_angle;
344 }
345 failedClosures.insert(pair);
346 }
347 auto &failed = gaps_only ? failedGapJoins : failedJoins;
348 if (failed.count(pair))
349 continue;
350 for (double tolerance = settings.angleToleranceDegrees;;) {
351 settings.angleToleranceDegrees = tolerance;
352 bool changed = false;
353 CHKERR change(Kind::Join, pair, edge, crack, direction, settings,
354 changed, step);
355 if (changed) {
356 if (gaps_only)
357 step.kind = Kind::Closure;
358 step.extended = ranks[i];
359 step.opening = opening_angle;
362 }
363 if (gaps_only || failedJoins.count(pair) || !widenTolerance(tolerance))
364 break;
365 }
366 if (gaps_only)
367 failed.insert(pair);
368 }
369 }
371}
372
373MoFEMErrorCode CgalCrackGrowth::advanceOrdered(const std::vector<Pair> &edges,
374 const Direction &direction,
375 Step &step) {
378 CHKERR closeAndJoin(edges, direction, step);
379 if (step.kind != Kind::None)
381 step = Step();
382 Range crack, front;
384 CHKERR frontEdges(crack, front);
385 std::vector<char> dropped(edges.size(), 0);
386 for (auto settings : advancePhases())
387 for (std::size_t i = 0; i != edges.size(); ++i) {
388 EntityHandle edge;
389 CHKERR resolveListed(edges, i, front, dropped, step, edge);
390 if (!edge)
391 continue;
393 settings.angleToleranceDegrees = tolerance;
394 bool changed = false;
395 CHKERR change(Kind::Extension, edges[i], edge, crack, direction,
396 settings, changed, step);
397 if (changed) {
398 step.extended = i;
401 }
403 break;
404 MOFEM_LOG("WORLD", Sev::verbose)
405 << "Ranked edge (" << edges[i][0] << ", " << edges[i][1]
406 << ") retry at direction tolerance " << tolerance << " degrees";
407 }
408 }
410}
411
412MoFEMErrorCode CgalCrackGrowth::extend(const std::vector<Pair> &edges,
413 const Direction &direction, Step &step) {
416 step = Step();
417 Range crack, front;
419 CHKERR frontEdges(crack, front);
420 std::vector<char> dropped(edges.size(), 0);
421 for (auto settings : advancePhases())
423 settings.angleToleranceDegrees = tolerance;
424 for (std::size_t i = 0; i != edges.size(); ++i) {
425 EntityHandle edge;
426 CHKERR resolveListed(edges, i, front, dropped, step, edge);
427 if (!edge)
428 continue;
429 bool changed = false;
430 CHKERR change(Kind::Extension, edges[i], edge, crack, direction,
431 settings, changed, step);
432 if (changed) {
433 step.extended = i;
436 }
437 }
439 break;
440 MOFEM_LOG("WORLD", Sev::verbose)
441 << "No front edge extends; angle tolerance " << tolerance
442 << " degrees";
443 }
445}
446
447std::vector<CGALTools::CrackExtensionSettings>
449 auto flips = options.extension;
450 flips.maxMoveRelative = 0;
451 std::vector<CGALTools::CrackExtensionSettings> phases{flips};
453 // Flips alone already failed for every edge at every tolerance.
454 auto moves = options.extension;
455 moves.skipFlips = true;
456 phases.push_back(moves);
457 }
458 return phases;
459}
460
464 return false;
466 return true;
467}
468
469MoFEMErrorCode CgalCrackGrowth::resolveListed(const std::vector<Pair> &edges,
470 std::size_t i,
471 const Range &front,
472 std::vector<char> &dropped,
473 Step &step, EntityHandle &edge) {
475 edge = 0;
476 if (dropped[i])
478 CHKERR resolve(edges[i], front, edge);
479 if (!edge) {
480 dropped[i] = 1;
481 step.dropped.push_back(i);
482 }
484}
485
486} // namespace MoFEM
Range crackFaces
std::vector< std::array< EntityHandle, 3 > > faces
Sorted keys.
CGALTools::CrackExtensionCandidate candidate
MoFEM::Interface & mField
bool selected
Definition CGALTools.cpp:97
Report & report
FaceSet skin
std::vector< int > source
const Settings & settings
FaceSet crack
std::vector< Face > closures
Faces closing non-convex front corners.
std::vector< int > size
double tolerance
Radians.
std::vector< int > vertices
std::unordered_map< int, NodeConstraint > constraints
std::vector< Cell > cells
Crack growth one step at a time on two bit levels, on top of CGALInterface. Not included by MoFEM....
moab::Interface & mb
It it
Definition Schur.cpp:2568
bool failed
constexpr double a
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define CHK_MOAB_THROW(err, msg)
Check error code of MoAB function and throw MoFEM exception.
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#define MOFEM_LOG(channel, severity)
Log.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
FTensor::Index< 'i', SPACE_DIM > i
const double c
speed of light (cm/ns)
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'k', 3 > k
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition Types.hpp:40
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
Managing BitRefLevels.
virtual CrackExtension & getCrackExtension()=0
double maxMoveRelative
Move bound, fraction of mean edge length at the free node.
int ringLimit
Vertex rings around the front edge that may change.
double angleToleranceDegrees
Accepted angle between candidate face and target plane.
CGALTools::CrackExtensionSettings extension
bool join
Close gaps globally, then join, before advancing.
std::vector< Pair > addedEdges
Closure, extension: edges of new faces.
CGALTools::CrackExtensionCandidate candidate
Closure, extension.
std::vector< int > dropped
Extension: listed edges not on the front.
int extended
Index selected by extend or advanceOrdered.
double toleranceDegrees
Extension: the angle tolerance used.
std::vector< int > joinCategories
Join: the CGALTools::CrackJoinFace category of each face.
double opening
Closure: uncracked angle of the corner, radians.
std::size_t closures
Faces closing corners in the same step.
Pair edge
Front edge of closure, extension or ordered join.
std::size_t filled
Notch faces filled after it.
std::unordered_map< int, EntityHandle > vertexById
bool widenTolerance(double &tolerance) const
MoFEMErrorCode forgetClosuresNear(const Range &faces)
std::set< Pair > failedGapJoins
MoFEMErrorCode join(const Direction &direction, Step &step)
MoFEM::Interface & mField
std::set< Pair > failedJoins
MoFEMErrorCode refreshLevel() const
CgalCrackGrowth(MoFEM::Interface &m_field, EntityHandle crack_set, BitRefLevel source, BitRefLevel output, const Range &constraints, const Settings &settings)
std::set< Pair > failedClosures
MoFEMErrorCode resolveListed(const std::vector< Pair > &edges, std::size_t i, const Range &front, std::vector< char > &dropped, Step &step, EntityHandle &edge)
std::function< MoFEMErrorCode(EntityHandle edge, const Range &crack, std::array< double, 3 > &direction)> Direction
Extension direction of a front edge, given the crack faces.
MoFEMErrorCode extend(const std::vector< Pair > &edges, const Direction &direction, Step &step)
bool levelChanged
Skin and vertex IDs of the current level, refreshed when it changes.
MoFEMErrorCode frontEdges(const Range &crack, Range &front) const
const Settings & settings() const
MoFEMErrorCode crackFaces(Range &crack) const
MoFEMErrorCode resolve(const Pair &pair, const Range &front, EntityHandle &edge) const
Pair edgeIds(EntityHandle edge) const
MoFEMErrorCode opening(EntityHandle node, const Range &crack, const Range &front, double &value) const
std::array< int, 2 > Pair
std::vector< CGALTools::CrackExtensionSettings > advancePhases() const
MoFEMErrorCode change(Kind operation, const Pair &pair, EntityHandle edge, const Range &crack, const Direction &direction, const CGALTools::CrackExtensionSettings &settings, bool &changed, Step &step)
CGALInterface::CrackExtension & crackExtension() const
MoFEMErrorCode closeAndJoin(const std::vector< Pair > &edges, const Direction &direction, Step &step)
MoFEMErrorCode advanceOrdered(const std::vector< Pair > &edges, const Direction &direction, Step &step)
virtual moab::Interface & get_moab()=0
Deprecated interface functions.
Interface for managing meshsets containing materials and boundary conditions.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.