v0.16.3
Loading...
Searching...
No Matches
Functions | Variables
rebuild_problem.cpp File Reference
#include <MoFEM.hpp>
#include <map>
#include <boost/functional/hash.hpp>

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 429 of file rebuild_problem.cpp.

429 {
430
431 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
432
433 try {
434
435 moab::Core mb_instance;
436 moab::Interface &moab = mb_instance;
437
438 char mesh_file_name[255];
439 PetscBool flg = PETSC_FALSE;
440 CHKERR PetscOptionsGetString(PETSC_NULLPTR, "", "-file_name",
441 mesh_file_name, 255, &flg);
442 if (flg != PETSC_TRUE)
443 SETERRQ(PETSC_COMM_SELF, MOFEM_NOT_FOUND,
444 "-file_name <mesh> is required");
445 PetscInt nb_levels = 2, order = 3;
446 PetscBool is_partitioned = PETSC_FALSE, skip_matrix = PETSC_FALSE;
447 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-nb_levels", &nb_levels,
448 PETSC_NULLPTR);
449 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-order", &order,
450 PETSC_NULLPTR);
451 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-dm_is_partitioned",
452 &is_partitioned, PETSC_NULLPTR);
453 // Skip the matrix graphs: at large sizes they cost more memory than the
454 // problems themselves and are not part of the build being measured.
455 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-skip_matrix", &skip_matrix,
456 PETSC_NULLPTR);
457 // Register each level's new entities only (default), or rebuild every
458 // field entity and element with the all-entities overloads.
459 PetscBool incremental = PETSC_TRUE;
460 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-incremental", &incremental,
461 PETSC_NULLPTR);
462 // From this level on, refine only the star of one vertex instead of the
463 // whole mesh, so registration and build costs can be compared for a
464 // local change on a mesh of fixed size. Negative: always uniform.
465 PetscInt local_from = -1;
466 CHKERR PetscOptionsGetInt(PETSC_NULLPTR, "", "-local_from", &local_from,
467 PETSC_NULLPTR);
468 // Rebuild the alive problem in place (DMMoFEMRebuildProblem, same name
469 // and DM) instead of building the output under a second name; needs the
470 // distributed builder.
471 PetscBool in_place = PETSC_FALSE;
472 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-in_place", &in_place,
473 PETSC_NULLPTR);
474 if (in_place && !is_partitioned)
475 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
476 "-in_place needs -dm_is_partitioned");
477
478 CHKERR moab.load_file(mesh_file_name, 0, "");
479
480 MoFEM::Core core(moab);
481 MoFEM::Interface &m_field = core;
483 CHKERR DMRegister_MoFEM("DMMOFEM");
484
485 Events ev;
486 CHKERR PetscLogEventRegister("RP_Refine", 0, &ev.refine);
487 CHKERR PetscLogEventRegister("RP_FieldsFEs", 0, &ev.fieldsFes);
488 CHKERR PetscLogEventRegister("RP_Rebuild", 0, &ev.rebuild);
489 CHKERR PetscLogEventRegister("RP_Fresh", 0, &ev.fresh);
490 CHKERR PetscLogEventRegister("RP_Matrix", 0, &ev.matrix);
491
492 MOFEM_LOG_CHANNEL("WORLD");
493 MOFEM_LOG_TAG("WORLD", "RebuildProblem");
494 MOFEM_LOG("WORLD", Sev::inform)
495 << "Levels " << nb_levels << ", order " << order << ", "
496 << (is_partitioned ? "distributed" : "replicated") << " builder, "
497 << (incremental ? "incremental" : "full")
498 << " field/element registration, "
499 << (in_place ? "in-place rebuild" : "recreate path");
500
501 auto count_tets = [&](int l) {
502 Range tets;
505 bit(l), allBits(), 3, tets),
506 "count tets");
507 return tets.size();
508 };
509
510 auto log_level = [&](int l, const std::string &name, const Summary &s,
511 double seconds) {
512 MOFEM_LOG_CHANNEL("WORLD");
513 MOFEM_LOG_TAG("WORLD", "RebuildProblem");
514 MOFEM_LOG("WORLD", Sev::inform)
515 << "Level " << l << " " << name << ": tets " << count_tets(l)
516 << ", elements " << s.elements << ", dofs " << s.globalDofs
517 << " (local " << s.localDofs << ", ghost " << s.ghostDofs
518 << "), build " << seconds << " s";
519 };
520
521 auto zero = TestingInterface::Tolerance{0, 0};
522 State st;
523
524 // Mask of the rebuilt problem on level l. After a uniform refinement the
525 // level's elements carry bit l only, and {l-1, l} selects them; a local
526 // refinement adds bit l to the unrefined elements, which keep their
527 // older bits, so from the first local level on every bit is admitted.
528 auto rebuilt_mask = [&](int l) {
529 return (local_from >= 0 && l >= local_from) ? allBits()
530 : (bit(l - 1) | bit(l));
531 };
532
533 // Fields and the element are declared once; entities join per level.
534 CHKERR m_field.getInterface<BitRefManager>()->setBitRefLevelByDim(
535 0, 3, bit(0));
536 CHKERR m_field.add_field(fieldName, H1, AINSWORTH_LEGENDRE_BASE, 3,
537 MB_TAG_DENSE);
538 CHKERR m_field.add_finite_element(elementName);
539 CHKERR m_field.modify_finite_element_add_field_row(elementName, fieldName);
540 CHKERR m_field.modify_finite_element_add_field_col(elementName, fieldName);
542 fieldName);
543
544 auto require = [&](bool ok, const std::string &what) {
546 if (!ok)
547 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
548 "Stage order broken: %s", what.c_str());
550 };
551
552 // Stage 1 of level 0: register the mesh and build the first problem.
553 CHKERR testing->addTest(1, "level 0 build", [&]() {
555 PetscLogEventBegin(ev.fieldsFes, 0, 0, 0, 0);
556 CHKERR registerLevel(m_field, 0, order, incremental);
557 PetscLogEventEnd(ev.fieldsFes, 0, 0, 0, 0);
558 st.level = 0;
559 st.aliveName = "P0";
560 CHKERR buildProblem(m_field, st.aliveName, bit(0), allBits(),
561 is_partitioned, ev.fresh, st.alive, st.aliveSeconds);
562 CHKERR summarise(m_field, st.aliveName, st.aliveSummary);
563 log_level(0, st.aliveName, st.aliveSummary, st.aliveSeconds);
564 CHKERR testing->checkValue<TestType::SCALAR>(
565 "level 0 has dofs", st.aliveSummary.globalDofs > 0 ? 1 : 0, 1,
566 zero);
568 });
569
570 for (int l = 1; l <= nb_levels; ++l) {
571 const auto tag = "level " + std::to_string(l);
572 const int base = 10 * l;
573
574 CHKERR testing->addTest(base + 1, tag + " refine", [&, l, tag]() {
576 CHKERR require(st.level == l - 1 && st.alive,
577 tag + " refine needs level " + std::to_string(l - 1) +
578 " registered and its problem alive");
579 const bool local = local_from >= 0 && l >= local_from;
580 const auto tets_before = count_tets(l - 1);
581 int refined;
582 PetscLogEventBegin(ev.refine, 0, 0, 0, 0);
583 CHKERR refineLevel(m_field, l, local, refined);
584 PetscLogEventEnd(ev.refine, 0, 0, 0, 0);
585 PetscLogDouble t0, t1;
586 CHKERR PetscTime(&t0);
587 PetscLogEventBegin(ev.fieldsFes, 0, 0, 0, 0);
588 CHKERR registerLevel(m_field, l, order, incremental);
589 PetscLogEventEnd(ev.fieldsFes, 0, 0, 0, 0);
590 CHKERR PetscTime(&t1);
591 st.level = l;
592 int duplicates;
593 CHKERR countDuplicateViewEntries(m_field, duplicates);
594 MOFEM_LOG_CHANNEL("WORLD");
595 MOFEM_LOG_TAG("WORLD", "RebuildProblem");
596 MOFEM_LOG("WORLD", Sev::inform)
597 << "Level " << l << (local ? " local" : " uniform")
598 << " refinement of " << refined
599 << " tets; fields and elements registered in " << t1 - t0
600 << " s, duplicate view entries " << duplicates;
601 // Each refined tetrahedron is replaced by eight.
602 CHKERR testing->checkValue<TestType::SCALAR>(
603 tag + " tets after refinement", count_tets(l),
604 tets_before + 7. * refined, zero);
605 CHKERR testing->checkValue<TestType::SCALAR>(
606 tag + " element views without duplicates", duplicates, 0, zero);
608 });
609
610 // Recreate path: the output problem is built while the source is
611 // alive, the source is checked and then destroyed. In-place path: the
612 // alive problem is rebuilt under its own name and DM, and the survivor
613 // index sets are checked.
614 CHKERR testing->addTest(base + 2, tag + " rebuild", [&, l, tag]() {
616 CHKERR require(st.level == l && st.alive && !st.rebuilt,
617 tag + " rebuild needs the refined level and the "
618 "previous problem alive");
619 if (in_place) {
620 std::map<UId, int> old_global;
621 {
622 const Problem *prb;
623 CHKERR m_field.get_problem(st.aliveName, &prb);
624 for (auto &dof : *prb->getNumeredRowDofsPtr())
625 if (dof->getPetscGlobalDofIdx() >= 0)
626 old_global[dof->getLocalUniqueId()] =
627 dof->getPetscGlobalDofIdx();
628 }
629 IS raw_old, raw_new;
630 PetscLogDouble t0, t1;
631 CHKERR PetscTime(&t0);
632 PetscLogEventBegin(ev.rebuild, 0, 0, 0, 0);
633 CHKERR DMMoFEMRebuildProblem(st.alive, bit(l), rebuilt_mask(l),
634 &raw_old, &raw_new);
635 PetscLogEventEnd(ev.rebuild, 0, 0, 0, 0);
636 CHKERR PetscTime(&t1);
637 SmartPetscObj<IS> is_old(raw_old), is_new(raw_new);
638 st.rebuilt = st.alive;
639 st.alive.reset();
640 st.rebuiltName = st.aliveName;
641 st.rebuiltSeconds = t1 - t0;
642 CHKERR summarise(m_field, st.rebuiltName, st.rebuiltSummary);
643 log_level(l, st.rebuiltName + " in place", st.rebuiltSummary,
644 st.rebuiltSeconds);
645 CHKERR testing->checkValue<TestType::SCALAR>(
646 tag + " problem keeps its name",
647 m_field.check_problem(st.rebuiltName) ? 1 : 0, 1, zero);
648 CHKERR testing->checkValue<TestType::SCALAR>(
649 tag + " staging problem removed",
650 m_field.check_problem(st.rebuiltName + "__REBUILD_STAGE") ? 1
651 : 0,
652 0, zero);
653 CHKERR checkSurvivors(m_field, *testing, tag, st.rebuiltName,
654 old_global, st.aliveSummary.localDofs,
655 st.aliveSummary.globalDofs, is_old, is_new);
657 }
658 st.rebuiltName = "P" + std::to_string(l) + "_rebuild";
659 CHKERR buildProblem(m_field, st.rebuiltName, bit(l), rebuilt_mask(l),
660 is_partitioned, ev.rebuild, st.rebuilt,
661 st.rebuiltSeconds);
662 CHKERR summarise(m_field, st.rebuiltName, st.rebuiltSummary);
663 log_level(l, st.rebuiltName, st.rebuiltSummary, st.rebuiltSeconds);
664
665 Summary source_after;
666 CHKERR summarise(m_field, st.aliveName, source_after);
667 CHKERR testing->checkValue<TestType::SCALAR>(
668 tag + " source dofs unchanged", source_after.globalDofs,
669 st.aliveSummary.globalDofs, zero);
670 CHKERR testing->checkValue<TestType::SCALAR>(
671 tag + " source numbering unchanged",
672 source_after.numbering == st.aliveSummary.numbering ? 0 : 1, 0,
673 zero);
674
675 // Commit: destroying the source DM deletes its problem.
676 st.alive.reset();
677 CHKERR testing->checkValue<TestType::SCALAR>(
678 tag + " source problem deleted",
679 m_field.check_problem(st.aliveName) ? 1 : 0, 0, zero);
681 });
682
683 // Oracle: the same level built fresh, alone.
684 CHKERR testing->addTest(base + 3, tag + " fresh build", [&, l, tag]() {
686 CHKERR require(st.level == l && st.rebuilt && !st.alive && !st.fresh,
687 tag + " fresh build needs the rebuilt problem and "
688 "the source destroyed");
689 st.freshName = "P" + std::to_string(l);
690 CHKERR buildProblem(m_field, st.freshName, bit(l), allBits(),
691 is_partitioned, ev.fresh, st.fresh,
692 st.freshSeconds);
693 CHKERR summarise(m_field, st.freshName, st.freshSummary);
694 log_level(l, st.freshName, st.freshSummary, st.freshSeconds);
696 });
697
698 CHKERR testing->addTest(base + 4, tag + " compare", [&, l, tag]() {
700 CHKERR require(st.level == l && st.rebuilt && st.fresh,
701 tag + " compare needs both problems");
702 const auto &r = st.rebuiltSummary, &f = st.freshSummary;
703 CHKERR testing->checkValue<TestType::SCALAR>(
704 tag + " global dofs", r.globalDofs, f.globalDofs, zero);
705 CHKERR testing->checkValue<TestType::SCALAR>(
706 tag + " local dofs", r.localDofs, f.localDofs, zero);
707 CHKERR testing->checkValue<TestType::SCALAR>(
708 tag + " ghost dofs", r.ghostDofs, f.ghostDofs, zero);
709 CHKERR testing->checkValue<TestType::SCALAR>(
710 tag + " elements", r.elements, f.elements, zero);
711 CHKERR testing->checkValue<TestType::SCALAR>(
712 tag + " numbering hash equal",
713 r.numbering == f.numbering ? 0 : 1, 0, zero);
714 Mismatch mismatch;
715 CHKERR compareProblems(m_field, st.rebuiltName, st.freshName, bit(l),
716 mismatch);
717 CHKERR testing->checkValue<TestType::SCALAR>(
718 tag + " dofs missing from fresh", mismatch.missing, 0, zero);
719 CHKERR testing->checkValue<TestType::SCALAR>(
720 tag + " dofs with different index", mismatch.index, 0, zero);
721 CHKERR testing->checkValue<TestType::SCALAR>(
722 tag + " dofs on source-only parents", mismatch.parents, 0, zero);
724 });
725
726 // Matrix graphs agree; then the fresh oracle goes and the rebuilt
727 // problem is the next source.
728 CHKERR testing->addTest(base + 5, tag + " matrix", [&, l, tag]() {
730 CHKERR require(st.level == l && st.rebuilt && st.fresh,
731 tag + " matrix needs both problems");
732 if (!skip_matrix) {
733 double rows_rebuilt, nnz_rebuilt, rows_fresh, nnz_fresh;
734 double seconds_rebuilt, seconds_fresh;
735 MatrixGraph graph;
736 CHKERR matrixInfo(m_field, st.rebuiltName, ev.matrix, rows_rebuilt,
737 nnz_rebuilt, seconds_rebuilt, graph);
738 CHKERR matrixInfo(m_field, st.freshName, ev.matrix, rows_fresh,
739 nnz_fresh, seconds_fresh, graph, true);
740 MOFEM_LOG_CHANNEL("WORLD");
741 MOFEM_LOG_TAG("WORLD", "RebuildProblem");
742 MOFEM_LOG("WORLD", Sev::inform)
743 << "Level " << l << " matrix rows " << rows_rebuilt
744 << ", non-zeros " << nnz_rebuilt << ", created in "
745 << seconds_rebuilt << " s and " << seconds_fresh << " s";
746 CHKERR testing->checkValue<TestType::SCALAR>(
747 tag + " matrix rows", rows_rebuilt, rows_fresh, zero);
748 CHKERR testing->checkValue<TestType::SCALAR>(
749 tag + " matrix rows equal dofs", rows_rebuilt,
750 st.rebuiltSummary.globalDofs, zero);
751 CHKERR testing->checkValue<TestType::SCALAR>(
752 tag + " matrix non-zeros", nnz_rebuilt, nnz_fresh, zero);
753 CHKERR testing->checkValue<TestType::SCALAR>(
754 tag + " matrix rows with different column indices",
755 graph.differentRows, 0, zero);
756 }
757
758 st.fresh.reset();
759 st.alive = st.rebuilt;
760 st.rebuilt.reset();
761 st.aliveName = st.rebuiltName;
762 st.aliveSummary = st.rebuiltSummary;
763 st.aliveSeconds = st.rebuiltSeconds;
765 });
766 }
767
768 // Stages run in ascending number: level 0, then refine, rebuild, fresh,
769 // compare and matrix for each level.
770 CHKERR testing->runTests();
771 }
773
775
776 return 0;
777}
WorkMesh base
Tag tag
std::string name
#define CATCH_ERRORS
Catch errors.
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
Definition definitions.h:60
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
@ H1
continuous field
Definition definitions.h:85
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_NOT_FOUND
Definition definitions.h:33
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
@ MOFEM_INVALID_DATA
Definition definitions.h:36
#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 DMMoFEMRebuildProblem(DM dm, const MoFEM::BitRefLevel target_bit, const MoFEM::BitRefLevel target_mask, IS *old_row_indices=PETSC_NULLPTR, IS *new_row_indices=PETSC_NULLPTR)
Rebuild the DM's parent problem in place on a new bit ref level.
Definition DMMoFEM.cpp:479
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
Definition DMMoFEM.cpp:43
virtual const Problem * get_problem(const std::string problem_name) const =0
Get the problem object.
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
virtual MoFEMErrorCode add_finite_element(const std::string &fe_name, enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
add finite element
virtual MoFEMErrorCode modify_finite_element_add_field_col(const std::string &fe_name, const std::string name_row)=0
set field col which finite element use
virtual MoFEMErrorCode modify_finite_element_add_field_row(const std::string &fe_name, const std::string name_row)=0
set field row which finite element use
virtual MoFEMErrorCode modify_finite_element_add_field_data(const std::string &fe_name, const std::string name_field)=0
set finite element field data
#define MOFEM_LOG(channel, severity)
Log.
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
virtual bool check_problem(const std::string name)=0
check if problem exist
auto bit
set bit
PetscBool testing
constexpr int nb_levels
Definition level_set.cpp:58
FTensor::Index< 'l', 3 > l
PetscErrorCode PetscOptionsGetInt(PetscOptions *, const char pre[], const char name[], PetscInt *ivalue, PetscBool *set)
PetscErrorCode PetscOptionsGetBool(PetscOptions *, const char pre[], const char name[], PetscBool *bval, PetscBool *set)
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
int r
Definition sdf.py:205
static char help[]
Managing BitRefLevels.
virtual MoFEMErrorCode add_field(const std::string name, const FieldSpace space, const FieldApproximationBase base, const FieldCoefficientsNumber nb_of_coefficients, const TagType tag_type=MB_TAG_SPARSE, const enum MoFEMTypes bh=MF_EXCL, int verb=DEFAULT_VERBOSITY)=0
Add field.
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.
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:
=
"Build a problem on uniformly refined levels while the previous level's "
"problem is alive, and compare with a fresh build.\n"
"-file_name <mesh> -nb_levels <n> -order <p> [-dm_is_partitioned]\n\n"

Definition at line 48 of file rebuild_problem.cpp.