v0.16.3
Loading...
Searching...
No Matches
Functions | Variables
rebuild_problem_hybrid.cpp File Reference
#include <MoFEM.hpp>
#include <unordered_map>

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Variables

static char help []
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 293 of file rebuild_problem_hybrid.cpp.

293 {
294
295 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
296
297 try {
298
299 moab::Core mb_instance;
300 moab::Interface &moab = mb_instance;
301 MoFEM::Core core(moab);
302 MoFEM::Interface &m_field = core;
304 auto *simple = m_field.getInterface<Simple>();
305 CHKERR DMRegister_MoFEM("DMMOFEM");
306
307 PetscInt nb_levels = 2, order = 2;
308 PetscBool is_partitioned = PETSC_FALSE, incremental = PETSC_TRUE;
309 PetscBool rebuild_structures = PETSC_FALSE;
310 PetscInt local_from = -1;
311 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-nb_levels", &nb_levels,
312 PETSC_NULLPTR);
313 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-order", &order,
314 PETSC_NULLPTR);
315 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-dm_is_partitioned",
316 &is_partitioned, PETSC_NULLPTR);
317 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-incremental", &incremental,
318 PETSC_NULLPTR);
319 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-rebuild_structures",
320 &rebuild_structures, PETSC_NULLPTR);
321 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-local_from", &local_from,
322 PETSC_NULLPTR);
323 if (order < 1)
324 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA, "-order must be >= 1");
325 const int u_order = order - 1;
326 const int hybrid_order = order - 1;
327
328 CHKERR simple->getOptions();
329 simple->getAddBoundaryFE() = false;
330 CHKERR simple->loadFile();
331 if (simple->getDim() != 3)
332 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_IMPLEMENTED,
333 "Tetrahedral meshes only");
334
335 const auto base = DEMKOWICZ_JACOBI_BASE;
336 CHKERR simple->addDomainBrokenField("BROKEN", HDIV, base, 1);
337 CHKERR simple->addDomainField("U", L2, base, 1);
338 CHKERR simple->addSkeletonField("HYBRID", L2, base, 1);
339 CHKERR simple->setFieldOrder("BROKEN", order);
340 CHKERR simple->setFieldOrder("U", u_order);
341 CHKERR simple->setFieldOrder("HYBRID", hybrid_order);
342
343 CHKERR simple->defineFiniteElements();
344 // Simple's skeleton adjacency also reaches the DOFs of the parent cells
345 // of the bridge cells (for hanging nodes); each level here is a complete
346 // mesh, so the skeleton element sees its two bridge cells only.
347 simple->getBitAdjParent() = BitRefLevel();
348 CHKERR simple->setSkeletonAdjacency(-1, simple->getSkeletonFEName());
349 CHKERR simple->defineProblem(is_partitioned);
350
351 MOFEM_LOG_CHANNEL("WORLD");
352 MOFEM_LOG_TAG("WORLD", "RebuildHybrid");
353 MOFEM_LOG("WORLD", Sev::inform)
354 << "Levels " << nb_levels << ", BROKEN order " << order
355 << ", U and HYBRID order " << u_order << ", "
356 << (is_partitioned ? "distributed" : "replicated") << " builder, "
357 << (incremental ? "incremental" : "full")
358 << " field/element registration"
359 << (local_from >= 0 ? ", local refinement from level " +
360 std::to_string(local_from)
361 : std::string())
362 << (rebuild_structures ? ", block and nested structures rebuilt"
363 : "");
364
365 // DOFs per entity for the bases used.
366 const int dofs_broken_per_tet = 4 * NBFACETRI_DEMKOWICZ_HDIV(order) +
368 const int dofs_u_per_tet = NBVOLUMETET_L2(u_order);
369 const int dofs_hybrid_per_face = NBFACETRI_L2(hybrid_order);
370
371 auto zero = TestingInterface::Tolerance{0, 0};
372 auto count_tets = [&](int l) {
373 Range tets;
376 bit(l), allBits(), 3, tets),
377 "count tets");
378 return static_cast<int>(tets.size());
379 };
380
381 // elements and field pairs of the block matrix
382 const SchurFEOpsFEandFields block_ops = {
383 {simple->getDomainFEName(),
384 {{"BROKEN", "BROKEN"},
385 {"U", "U"},
386 {"BROKEN", "U"},
387 {"U", "BROKEN"}}},
388 {simple->getSkeletonFEName(),
389 {{"BROKEN", "HYBRID"}, {"HYBRID", "BROKEN"}}}};
390 // structures kept from level to level with -rebuild_structures
391 boost::shared_ptr<BlockStructure> kept_block;
392 boost::shared_ptr<NestSchurData> kept_nested;
393
394 struct Level {
395 SmartPetscObj<DM> schurDM, blockDM;
396 SmartPetscObj<Mat> blockMat, nestedMat, hybridMat;
397 boost::shared_ptr<BlockStructure> blockData;
398 boost::shared_ptr<NestSchurData> nestedData;
399 Timings t;
400 int tets = 0, skeletonFaces = 0, rows = 0, schurRows = 0, blockRows = 0;
401 double hybridNnz = 0;
402 };
403 std::vector<Level> levels(nb_levels + 1);
404
405 for (int l = 0; l <= nb_levels; ++l) {
406 const auto tag = "level " + std::to_string(l);
407 auto &lv = levels[l];
408
409 CHKERR testing->addTest(10 * l + 1, tag + " refine and register", [&, l,
410 tag]() {
412 const bool local = l > 0 && local_from >= 0 && l >= local_from;
413 int refined = 0;
414 if (l > 0)
415 CHKERR refineLevel(m_field, *simple, l, local, refined);
416 CHKERR setSkeleton(m_field, *simple, l, lv.skeletonFaces);
417 lv.tets = count_tets(l);
418 simple->getBitRefLevel() = bit(l);
419 CHKERR time(
420 [&]() {
421 return registerLevel(m_field, *simple, l, order, incremental);
422 },
423 lv.t.registration);
424 if (l > 0 && !local)
425 CHKERR testing->checkValue<TestType::SCALAR>(
426 tag + " tets are eight per parent", lv.tets,
427 8. * levels[l - 1].tets, zero);
428 if (local) {
429 MOFEM_LOG_CHANNEL("WORLD");
430 MOFEM_LOG_TAG("WORLD", "RebuildHybrid");
431 MOFEM_LOG("WORLD", Sev::inform)
432 << "Level " << l << " local refinement of " << refined << " of "
433 << levels[l - 1].tets << " tets";
434 CHKERR testing->checkValue<TestType::SCALAR>(
435 tag + " local refinement leaves tets unchanged",
436 refined < levels[l - 1].tets ? 1 : 0, 1, zero);
437 }
439 });
440
441 CHKERR testing->addTest(10 * l + 2, tag + " problem", [&, l, tag]() {
443 // Adjacencies on the level, the problem selector set to the level
444 // and DMSetUp of the parent; previous levels' sub-problems are gone.
445 CHKERR time([&]() { return simple->reSetUp(true); }, lv.t.problem);
446 lv.rows = problemRows(m_field, simple->getDM());
447 const double expected =
448 double(lv.tets) * (dofs_broken_per_tet + dofs_u_per_tet) +
449 double(lv.skeletonFaces) * dofs_hybrid_per_face;
450 CHKERR testing->checkValue<TestType::SCALAR>(
451 tag + " parent dofs = BROKEN + U + HYBRID", lv.rows, expected,
452 zero);
454 });
455
456 CHKERR testing->addTest(10 * l + 3, tag + " sub-problems", [&, l,
457 tag]() {
459 CHKERR time(
460 [&]() {
461 return subProblem(m_field, *simple, "SCHUR", {"HYBRID"},
462 lv.schurDM);
463 },
464 lv.t.schurSub);
465 CHKERR time(
466 [&]() {
467 return subProblem(m_field, *simple, "BLOCK", {"BROKEN", "U"},
468 lv.blockDM);
469 },
470 lv.t.blockSub);
471 lv.schurRows = problemRows(m_field, lv.schurDM);
472 lv.blockRows = problemRows(m_field, lv.blockDM);
473 CHKERR testing->checkValue<TestType::SCALAR>(
474 tag + " SCHUR dofs = HYBRID", lv.schurRows,
475 double(lv.skeletonFaces) * dofs_hybrid_per_face, zero);
476 CHKERR testing->checkValue<TestType::SCALAR>(
477 tag + " BLOCK + SCHUR dofs = parent", lv.blockRows + lv.schurRows,
478 lv.rows, zero);
480 });
481
482 CHKERR testing->addTest(10 * l + 4, tag + " block and nested Schur "
483 "matrices",
484 [&, l, tag]() {
486 const bool rebuild = rebuild_structures && kept_block && kept_nested;
487 CHKERR time(
488 [&]() {
490 if (rebuild) {
491 CHKERR rebuildBlockMatStructure(simple->getDM(), block_ops,
492 kept_block);
493 lv.blockData = kept_block;
494 } else {
495 lv.blockData =
496 createBlockMatStructure(simple->getDM(), block_ops);
497 }
498 auto block = createBlockMat(simple->getDM(), lv.blockData);
499 lv.blockMat = block.first;
501 },
502 lv.t.blockMat);
503 CHKERR time(
504 [&]() {
506 if (rebuild) {
508 {lv.schurDM, lv.blockDM}, kept_nested);
509 lv.nestedData = kept_nested;
510 } else {
511 lv.nestedData = createSchurNestedMatrixStruture(
512 {lv.schurDM, lv.blockDM}, lv.blockData, {"BROKEN", "U"},
513 {nullptr, nullptr}, true);
514 }
515 auto nested = createSchurNestedMatrix(lv.nestedData);
516 lv.nestedMat = nested.first;
518 },
519 lv.t.nestedSchur);
520 if (rebuild_structures) {
521 kept_block = lv.blockData;
522 kept_nested = lv.nestedData;
523 }
524 if (rebuild) {
525 // Oracle: new structures for the same DMs, made from scratch.
526 boost::shared_ptr<BlockStructure> fresh_block;
527 boost::shared_ptr<NestSchurData> fresh_nested;
528 double fresh_block_seconds, fresh_nested_seconds;
529 CHKERR time(
530 [&]() {
532 fresh_block =
533 createBlockMatStructure(simple->getDM(), block_ops);
535 },
536 fresh_block_seconds);
537 CHKERR time(
538 [&]() {
540 fresh_nested = createSchurNestedMatrixStruture(
541 {lv.schurDM, lv.blockDM}, fresh_block, {"BROKEN", "U"},
542 {nullptr, nullptr}, true);
544 },
545 fresh_nested_seconds);
546 MOFEM_LOG_CHANNEL("WORLD");
547 MOFEM_LOG_TAG("WORLD", "RebuildHybrid");
548 MOFEM_LOG("WORLD", Sev::inform)
549 << "Level " << l << " structures [s]: rebuilt block "
550 << lv.t.blockMat << ", rebuilt nested " << lv.t.nestedSchur
551 << "; new block " << fresh_block_seconds << ", new nested "
552 << fresh_nested_seconds;
553 auto compare = [&](const std::string &what, auto rebuilt,
554 auto fresh) {
556 const auto r = schurBlockStructureDigest(rebuilt);
557 const auto f = schurBlockStructureDigest(fresh);
558 CHKERR testing->checkValue<TestType::SCALAR>(
559 tag + " rebuilt " + what + " blocks", r.nbBlocks, f.nbBlocks,
560 zero);
561 CHKERR testing->checkValue<TestType::SCALAR>(
562 tag + " rebuilt " + what + " A00 elements", r.nbElems,
563 f.nbElems, zero);
564 CHKERR testing->checkValue<TestType::SCALAR>(
565 tag + " rebuilt " + what + " block index equal",
566 r.indexHash == f.indexHash ? 0 : 1, 0, zero);
567 CHKERR testing->checkValue<TestType::SCALAR>(
568 tag + " rebuilt " + what + " blocks equal",
569 r.blocksHash == f.blocksHash ? 0 : 1, 0, zero);
570 CHKERR testing->checkValue<TestType::SCALAR>(
571 tag + " rebuilt " + what + " A00 elements equal",
572 r.elemsHash == f.elemsHash ? 0 : 1, 0, zero);
574 };
575 CHKERR compare("block matrix", lv.blockData, fresh_block);
576 const auto &rebuilt_parts = std::get<1>(*lv.nestedData);
577 const auto &fresh_parts = std::get<1>(*fresh_nested);
578 for (int q = 0; q != 4; ++q)
579 CHKERR compare("nested quadrant " + std::to_string(q),
580 rebuilt_parts[q], fresh_parts[q]);
581 }
582 PetscInt m, n;
583 CHKERR MatGetSize(lv.blockMat, &m, &n);
584 CHKERR testing->checkValue<TestType::SCALAR>(
585 tag + " block matrix rows = parent", m, lv.rows, zero);
586 CHKERR MatGetSize(lv.nestedMat, &m, &n);
587 CHKERR testing->checkValue<TestType::SCALAR>(
588 tag + " nested matrix rows = parent", m, lv.rows, zero);
590 });
591
592 CHKERR testing->addTest(10 * l + 5, tag + " hybrid L2 matrix", [&, l,
593 tag]() {
595 CHKERR time(
596 [&]() {
598 lv.hybridMat = createDMHybridisedL2Matrix(lv.schurDM);
600 },
601 lv.t.hybridMat);
602 PetscInt m, n;
603 CHKERR MatGetSize(lv.hybridMat, &m, &n);
604 CHKERR testing->checkValue<TestType::SCALAR>(
605 tag + " hybrid matrix rows = SCHUR dofs", m, lv.schurRows, zero);
606 CHKERR testing->checkValue<TestType::SCALAR>(
607 tag + " hybrid matrix columns = SCHUR dofs", n, lv.schurRows, zero);
608 CHKERR checkHybridMatrix(m_field, lv.schurDM, l, lv.hybridMat, *testing,
609 tag, lv.hybridNnz);
610
611 MOFEM_LOG_CHANNEL("WORLD");
612 MOFEM_LOG_TAG("WORLD", "RebuildHybrid");
613 MOFEM_LOG("WORLD", Sev::inform)
614 << "Level " << l << ": tets " << lv.tets << ", skeleton faces "
615 << lv.skeletonFaces << ", dofs " << lv.rows << " (SCHUR "
616 << lv.schurRows << ", BLOCK " << lv.blockRows
617 << "), hybrid non-zeros " << lv.hybridNnz;
618 MOFEM_LOG("WORLD", Sev::inform)
619 << "Level " << l << " times [s]: registration "
620 << lv.t.registration << ", problem " << lv.t.problem
621 << ", sub SCHUR " << lv.t.schurSub << ", sub BLOCK "
622 << lv.t.blockSub << ", block matrix " << lv.t.blockMat
623 << ", nested Schur " << lv.t.nestedSchur << ", hybrid matrix "
624 << lv.t.hybridMat;
625
626 // Release this level's algebra before the next level is built.
627 lv.hybridMat.reset();
628 lv.nestedMat.reset();
629 lv.nestedData.reset();
630 lv.blockMat.reset();
631 lv.blockData.reset();
632 // with -rebuild_structures the structures stay for the next level;
633 // its nested shell matrices are replaced by the rebuild
634 lv.blockDM.reset();
635 lv.schurDM.reset();
637 });
638 }
639
640 CHKERR testing->runTests();
641 }
643
645
646 return 0;
647}
WorkMesh base
Vec t
Removal: the ring edge.
Tag tag
SmartPetscObj< IS > rows
void simple(double P1[], double P2[], double P3[], double c[], const int N)
Definition acoustic.cpp:69
#define CATCH_ERRORS
Catch errors.
@ DEMKOWICZ_JACOBI_BASE
Definition definitions.h:66
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
@ L2
field with C-1 continuity
Definition definitions.h:88
@ HDIV
field with continuous normal traction
Definition definitions.h:87
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_INVALID_DATA
Definition definitions.h:36
@ MOFEM_NOT_IMPLEMENTED
Definition definitions.h:32
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
constexpr int order
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition DMMoFEM.cpp:43
MoFEMErrorCode getEntitiesByDimAndRefLevel(const BitRefLevel bit, const BitRefLevel mask, const int dim, const EntityHandle meshset, int verb=0) const
add all ents from ref level given by bit to meshset
#define MOFEM_LOG(channel, severity)
Log.
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
#define NBFACETRI_L2(P)
Number of base functions on triangle for L2 space.
#define NBVOLUMETET_DEMKOWICZ_HDIV(P)
#define NBFACETRI_DEMKOWICZ_HDIV(P)
#define NBVOLUMETET_L2(P)
Number of base functions on tetrahedron for L2 space.
auto bit
set bit
PetscBool testing
constexpr int nb_levels
Definition level_set.cpp:58
FTensor::Index< 'l', 3 > l
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
Definition Types.hpp:40
SchurBlockStructureDigest schurBlockStructureDigest(boost::shared_ptr< BlockStructure > data_ptr)
Definition Schur.cpp:4821
PetscErrorCode PetscOptionsGetInt(PetscOptions *, const char pre[], const char name[], PetscInt *ivalue, PetscBool *set)
std::pair< SmartPetscObj< Mat >, boost::shared_ptr< NestSchurData > > createSchurNestedMatrix(boost::shared_ptr< NestSchurData > schur_net_data_ptr)
Create a Mat Diag Blocks object.
Definition Schur.cpp:4859
PetscErrorCode PetscOptionsGetBool(PetscOptions *, const char pre[], const char name[], PetscBool *bval, PetscBool *set)
auto createDMHybridisedL2Matrix(DM dm)
Get smart hybridised L2 matrix from DM.
Definition DMMoFEM.hpp:1249
boost::shared_ptr< BlockStructure > createBlockMatStructure(DM dm, SchurFEOpsFEandFields schur_fe_op_vec)
Create block matrix structure.
Definition Schur.cpp:2819
boost::shared_ptr< NestSchurData > createSchurNestedMatrixStruture(std::pair< SmartPetscObj< DM >, SmartPetscObj< DM > > dms, boost::shared_ptr< BlockStructure > block_mat_data_ptr, std::vector< std::string > fields_names, std::vector< boost::shared_ptr< Range > > field_ents, bool add_preconditioner_block)
Create structure of nested matrix [A11 A10; A01 A00].
Definition Schur.cpp:4760
MoFEMErrorCode rebuildSchurNestedMatrixStruture(std::pair< SmartPetscObj< DM >, SmartPetscObj< DM > > dms, boost::shared_ptr< NestSchurData > nest_data_ptr)
Rebuild a nested Schur structure after rebuildBlockMatStructure.
Definition Schur.cpp:4799
MoFEMErrorCode rebuildBlockMatStructure(DM dm, SchurFEOpsFEandFields schur_fe_op_vec, boost::shared_ptr< BlockStructure > data_ptr)
Rebuild a block matrix structure after its problem changed.
Definition Schur.cpp:2833
std::vector< std::pair< std::string, std::vector< SchurFieldPair > > > SchurFEOpsFEandFields
Definition Schur.hpp:83
SchurShellMatData createBlockMat(DM dm, boost::shared_ptr< BlockStructure > data)
Create a Schur Mat object.
Definition Schur.cpp:3615
int r
Definition sdf.py:205
static char help[]
double q
FTensor::Index< 'm', 3 > m
Managing BitRefLevels.
Core (interface) class.
Definition Core.hpp:83
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:68
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:123
Deprecated interface functions.
Simple interface for fast problem set-up.
Definition Simple.hpp:27
intrusive_ptr for managing petsc objects
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.

Variable Documentation

◆ help

char help[]
static
Initial value:
= "Hybridised broken H(div) problem on refined levels: "
"problem, sub-problem and matrix timings.\n"
"-file_name <mesh> -nb_levels <n> -order <p> "
"[-incremental true|false] [-local_from <l>] "
"[-rebuild_structures]\n\n"

Definition at line 43 of file rebuild_problem_hybrid.cpp.