97 std::vector<SpatialDomains::PointGeom *>
nodes;
105 Jitter(
unsigned seed) :
m_rng(seed)
133std::vector<std::vector<NekDouble>> VertexPositions(
139 return {{0.0, 0.0, 0.0}, {1.0, 0.1, 0.0}, {0.2, 1.0, 0.0}};
141 return {{0.0, 0.0, 0.0},
146 BOOST_FAIL(
"unsupported shape");
157 unsigned seed,
bool invert)
162 ret.mesh = std::make_shared<Mesh>();
163 auto &
graph = ret.mesh->m_meshGraph;
164 graph->SetMeshDimension(dim);
165 graph->SetSpaceDimension(dim);
167 std::vector<std::vector<NekDouble>>
verts = VertexPositions(shape);
173 std::swap(verts[1], verts[2]);
175 std::vector<SpatialDomains::PointGeom *> nodeList(
verts.size());
176 for (
size_t i = 0; i <
verts.size(); ++i)
178 nodeList[i] =
graph->CreatePointGeom(dim, i, verts[i][0], verts[i][1],
182 ret.el = CreateElementLite(shape, nodeList,
graph, ret.mesh->m_edgeSet,
183 ret.mesh->m_faceSet);
184 ret.mesh->m_elementTags[dim][ret.el] = {0};
189 ret.nodes = GetCurvedNodes(ret.el);
192 for (
auto *n : ret.
nodes)
194 for (
int d = 0;
d < dim; ++
d)
196 (*n)[
d] += 0.08 * jitter();
207 std::vector<std::vector<NekDouble>>
hess;
210template <
int DIM> Derivatives Unpack(
const std::array<NekDouble, 9> &g)
213 ret.grad.assign(g.begin(), g.begin() + DIM);
214 ret.hess.assign(DIM, std::vector<NekDouble>(DIM, 0.0));
217 for (
int m = 0; m < DIM; ++m)
219 for (
int l = m; l < DIM; ++l, ++ct)
221 ret.hess[m][l] = ret.hess[l][m] = g[ct];
227NekDouble MaxAbs(
const std::vector<NekDouble> &v)
232 ret = std::max(ret, std::fabs(x));
244 const std::string &what)
247 opti->GetFunctional<DIM>(minJac,
true);
248 Derivatives analytic = Unpack<DIM>(opti->GetGrad());
250 std::vector<NekDouble> x0(DIM);
251 for (
int d = 0;
d < DIM; ++
d)
258 auto eval = [&](std::vector<int> steps) {
260 for (
int d = 0;
d < DIM; ++
d)
262 offset[
d] = steps[
d] * h;
265 return opti->GetFunctionalAt<DIM>(offset, dummy);
269 ret.grad.assign(DIM, 0.0);
270 ret.hess.assign(DIM, std::vector<NekDouble>(DIM, 0.0));
272 for (
int i = 0; i < DIM; ++i)
274 std::vector<int> fwd(DIM, 0), bwd(DIM, 0);
277 ret.grad[i] = (eval(fwd) - eval(bwd)) / (2.0 * h);
280 for (
int i = 0; i < DIM; ++i)
282 for (
int j = 0; j < DIM; ++j)
285 for (
int si : {1, -1})
287 for (
int sj : {1, -1})
289 std::vector<int> steps(DIM, 0);
292 sum += si * sj * eval(steps);
295 ret.hess[i][j] =
sum / (4.0 * h * h);
308 Derivatives d1 = difference(h);
309 Derivatives d2 = difference(0.5 * h);
312 fd.grad.assign(DIM, 0.0);
313 fd.hess.assign(DIM, std::vector<NekDouble>(DIM, 0.0));
314 for (
int i = 0; i < DIM; ++i)
316 fd.grad[i] = (4.0 * d2.grad[i] - d1.grad[i]) / 3.0;
317 for (
int j = 0; j < DIM; ++j)
319 fd.hess[i][j] = (4.0 * d2.hess[i][j] - d1.hess[i][j]) / 3.0;
323 const NekDouble gScale = std::max(MaxAbs(fd.grad), 1e-12);
324 for (
int i = 0; i < DIM; ++i)
327 std::fabs(analytic.grad[i] - fd.grad[i]) < 1e-6 * gScale,
328 what <<
": gradient component " << i <<
" is " << analytic.grad[i]
329 <<
", differencing the functional gives " << fd.grad[i]);
333 for (
int i = 0; i < DIM; ++i)
335 hScale = std::max(hScale, MaxAbs(fd.hess[i]));
338 for (
int i = 0; i < DIM; ++i)
340 for (
int j = 0; j < DIM; ++j)
342 BOOST_CHECK_MESSAGE(std::fabs(analytic.hess[i][j] - fd.hess[i][j]) <
344 what <<
": Hessian entry (" << i <<
"," << j
345 <<
") is " << analytic.hess[i][j]
346 <<
", differencing the functional gives "
361 const std::string &optiName, Config config)
363 const int nummode = 4;
364 const int overInt = 2;
367 MakeElement(shape, nummode, 20251005u, config == Config::eInverted);
369 std::map<LibUtilities::ShapeType, DerivUtilSharedPtr> derivUtils =
370 BuildDerivUtil(nummode, overInt);
371 BOOST_REQUIRE(derivUtils.count(shape) == 1);
373 ResidualSharedPtr res = std::make_shared<Residual>();
376 res->worstJac = std::numeric_limits<NekDouble>::max();
378 ElUtilSharedPtr elUtil = std::make_shared<ElUtil>(te.el, derivUtils[shape],
379 res, nummode, overInt);
385 std::set<SpatialDomains::PointGeom *>
verts;
386 for (
int i = 0; i < te.el->GetNumVerts(); ++i)
388 verts.insert(te.el->GetVertex(i));
392 for (
auto *n : te.
nodes)
394 if (
verts.count(n) == 0)
399 BOOST_REQUIRE(node !=
nullptr);
401 if (config == Config::eTangled)
412 res->minJac = std::numeric_limits<NekDouble>::max();
414 elUtil->CalcMinJac();
416 std::vector<ElUtilSharedPtr> els{elUtil};
417 NodeOptiSharedPtr nodeOpti = GetNodeOptiFactory().CreateInstance(
418 DIM * 11, node, els, res, derivUtils, opti, te.mesh->m_meshGraph.get());
420 const char *configName = config == Config::eValid ?
" (valid)"
421 : config == Config::eTangled ?
" (tangled)"
426 if (config == Config::eTangled)
429 nodeOpti->GetFunctional<DIM>(minJac,
false);
430 BOOST_REQUIRE_MESSAGE(minJac < 0.0,
431 what +
": element was meant to be tangled");
434 if (config == Config::eInverted)
436 BOOST_REQUIRE_MESSAGE(
437 elUtil->maps[9] < 0.0,
438 what +
": the ideal mapping was meant to be inverted");
441 CheckDerivatives<DIM>(nodeOpti, node, what);
444const std::vector<std::pair<optiType, std::string>> functionals = {
445 {eLinEl,
"linearelastic"},
446 {eHypEl,
"hyperelastic"},
447 {eRoca,
"distortion"},
454 for (
auto &[opti, name] : functionals)
459 {Config::eValid, Config::eTangled, Config::eInverted})
461 RunCase<2>(shape, opti, name, config);
469 for (
auto &[opti, name] : functionals)
474 {Config::eValid, Config::eTangled, Config::eInverted})
476 RunCase<3>(shape, opti, name, config);
497 const int nummode = 4;
498 const int overInt = 2;
504 TestElement te = MakeElement(shape, nummode, 7u,
false);
506 std::map<LibUtilities::ShapeType, DerivUtilSharedPtr> derivUtils =
507 BuildDerivUtil(nummode, overInt);
509 ResidualSharedPtr res = std::make_shared<Residual>();
510 res->worstJac = std::numeric_limits<NekDouble>::max();
511 res->minJac = std::numeric_limits<NekDouble>::max();
513 ElUtilSharedPtr elUtil = std::make_shared<ElUtil>(
514 te.el, derivUtils[shape], res, nummode, overInt);
520 const NekDouble offset[3] = {0.031, -0.047, 0.013};
522 elUtil->MoveNode(elUtil->NodeId(node), offset, dim);
523 for (
int d = 0; d < dim; ++d)
525 (*node)[d] += offset[d];
528 std::vector<NekDouble> updated = elUtil->deriv;
533 BOOST_REQUIRE_EQUAL(updated.size(), elUtil->deriv.size());
536 for (
size_t i = 0; i < updated.size(); ++i)
538 scale = std::max(scale, std::fabs(elUtil->deriv[i]));
539 worst = std::max(worst, std::fabs(updated[i] - elUtil->deriv[i]));
542 BOOST_CHECK_MESSAGE(worst < 1e-12 * std::max(scale, 1e-12),
544 <<
": the rank-one update left the "
545 <<
"derivatives differing by " << worst);
568 for (
int t = 0; t < 500; ++t)
571 for (
int i = 0; i < 3; ++i)
573 for (
int j = 0; j < 3; ++j)
580 for (
int i = 0; i < 3; ++i)
582 for (
int j = 0; j < 3; ++j)
584 H[i][j] = (i == j) ? 1e-6 : 0.0;
585 for (
int k = 0; k < 3; ++k)
587 H[i][j] += a[i][k] * a[j][k];
592 std::vector<NekDouble> grad3 = {jitter(), jitter(), jitter(),
593 H[0][0], H[0][1], H[0][2],
594 H[1][1], H[1][2], H[2][2]};
597 NewtonDirection<3>(grad3.data(), sk3);
600 for (
int i = 0; i < 3; ++i)
602 normSk = std::max(normSk, std::fabs(sk3[i]));
603 for (
int j = 0; j < 3; ++j)
605 normH = std::max(normH, std::fabs(H[i][j]));
609 for (
int i = 0; i < 3; ++i)
612 for (
int j = 0; j < 3; ++j)
614 Hsk += H[i][j] * sk3[j];
616 BOOST_CHECK_SMALL(Hsk + grad3[i], 1e-9 * normH * normSk);
620 grad3[0] * sk3[0] + grad3[1] * sk3[1] + grad3[2] * sk3[2], 0.0);
623 std::array<NekDouble, 5> grad2 = {grad3[0], grad3[1], H[0][0], H[0][1],
626 NewtonDirection<2>(grad2.data(), sk2);
628 for (
int i = 0; i < 2; ++i)
631 for (
int j = 0; j < 2; ++j)
633 Hsk += H[i][j] * sk2[j];
637 1e-9 * normH * std::max(std::fabs(sk2[0]), std::fabs(sk2[1])));
640 BOOST_CHECK_LT(grad2[0] * sk2[0] + grad2[1] * sk2[1], 0.0);