39#include <unordered_map>
43static char help[] =
"Hybridised broken H(div) problem on refined levels: "
44 "problem, sub-problem and matrix timings.\n"
45 "-file_name <mesh> -nb_levels <n> -order <p> "
46 "[-incremental true|false] [-local_from <l>] "
47 "[-rebuild_structures]\n\n";
56 double registration = 0, problem = 0, schurSub = 0, blockSub = 0,
57 blockMat = 0, nestedSchur = 0, hybridMat = 0;
62 PetscLogDouble t0, t1;
76 bool local,
int &refined) {
81 CHKERR bit_mng->getEntitiesByDimAndRefLevel(
bit(
l - 1), allBits(), 3,
83 Range tets = level_tets, edges;
86 CHKERR moab.get_connectivity(level_tets, verts,
true);
87 const EntityHandle vertex = verts.front();
89 CHKERR moab.get_adjacencies(&vertex, 1, 3,
false, star);
90 star = intersect(star, level_tets);
91 CHKERR moab.get_adjacencies(star, 1,
true, edges, moab::Interface::UNION);
93 CHKERR moab.get_adjacencies(edges, 3,
false, touched,
94 moab::Interface::UNION);
95 tets = intersect(touched, level_tets);
97 CHKERR moab.get_adjacencies(tets, 1,
true, edges, moab::Interface::UNION);
99 refined = tets.size();
104 const Range unchanged = subtract(level_tets, tets);
106 CHKERR moab.get_connectivity(unchanged, adj,
true);
107 for (
auto d : {1, 2})
108 CHKERR moab.get_adjacencies(unchanged,
d,
false, adj,
109 moab::Interface::UNION);
116 if (
simple.getMeshset() != 0)
117 CHKERR bit_mng->updateMeshsetByEntitiesChildren(
124 int &nb_skeleton_faces) {
129 CHKERR bit_mng->getEntitiesByDimAndRefLevel(
bit(
l), allBits(), 3, tets);
130 CHKERR moab.get_adjacencies(tets, 2,
true,
faces, moab::Interface::UNION);
132 Skinner skinner(&moab);
133 CHKERR skinner.find_skin(0, tets,
false,
skin);
135 const EntityHandle meshset =
simple.getSkeletonMeshSet();
136 CHKERR moab.clear_meshset(&meshset, 1);
137 CHKERR moab.add_entities(meshset, skeleton);
138 nb_skeleton_faces = skeleton.size();
144 int order,
bool incremental) {
155 bit(
l), allBits(), 3, tets);
158 tets = subtract(tets, registered);
166 for (
const auto &
field : {
"BROKEN",
"U"}) {
168 CHKERR comm_ptr->synchroniseFieldEntities(
field, 0);
173 CHKERR comm_ptr->synchroniseFieldEntities(
"HYBRID", 0);
175 Range new_ents = tets;
176 new_ents.merge(
faces);
180 simple.getDomainFEName());
182 simple.getSkeletonFEName());
191 const std::string &
tag,
double &nonzeros) {
195 std::unordered_map<EntityHandle, std::vector<PetscInt>> face_dofs;
197 if (dof->getPetscGlobalDofIdx() >= 0)
198 face_dofs[dof->getEnt()].push_back(dof->getPetscGlobalDofIdx());
202 std::unordered_map<EntityHandle, std::vector<PetscInt>> neighbors;
205 bit(
l), allBits(), 3, tets);
206 for (
auto tet : tets) {
209 std::vector<PetscInt> columns;
210 for (
auto face :
faces) {
211 auto it = face_dofs.find(face);
212 if (
it != face_dofs.end())
213 columns.insert(columns.end(),
it->second.begin(),
it->second.end());
215 for (
auto face :
faces)
216 if (face_dofs.count(face)) {
217 auto &adj = neighbors[face];
218 adj.insert(adj.end(), columns.begin(), columns.end());
222 PetscInt first_row, last_row;
223 CHKERR MatGetOwnershipRange(matrix, &first_row, &last_row);
225 CHKERR MatGetInfo(matrix, MAT_GLOBAL_SUM, &before);
226 CHKERR MatSetOption(matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
227 double local_nnz = 0;
228 for (
auto &entry : neighbors) {
229 auto &columns = entry.second;
230 std::sort(columns.begin(), columns.end());
231 columns.erase(std::unique(columns.begin(), columns.end()), columns.end());
232 const PetscInt nb_cols = columns.size();
233 const std::vector<PetscScalar>
values(nb_cols, 1.);
234 for (
auto row : face_dofs.at(entry.first))
235 if (
row >= first_row &&
row < last_row) {
236 local_nnz += nb_cols;
238 values.data(), INSERT_VALUES);
242 CHKERR MPI_Allreduce(&local_nnz, &expected_nnz, 1, MPI_DOUBLE, MPI_SUM,
246 tag +
" hybrid allocation equals topology non-zeros", before.nz_allocated,
248 CHKERR MatAssemblyBegin(matrix, MAT_FINAL_ASSEMBLY);
249 CHKERR MatAssemblyEnd(matrix, MAT_FINAL_ASSEMBLY);
251 CHKERR MatGetInfo(matrix, MAT_GLOBAL_SUM, &after);
252 nonzeros = after.nz_used;
254 tag +
" hybrid filled non-zeros equal topology", nonzeros, expected_nnz,
257 tag +
" hybrid fill without reallocations", after.mallocs, before.mallocs,
260 PetscInt missing_row;
261 CHKERR MatMissingDiagonal(matrix, &missing, &missing_row);
263 tag +
" hybrid diagonal exists in every row", missing ? 1 : 0, 0, zero);
268 const std::string &
name,
269 const std::vector<std::string> &fields,
277 for (
auto &
field : fields) {
293int main(
int argc,
char *argv[]) {
299 moab::Core mb_instance;
300 moab::Interface &moab = mb_instance;
308 PetscBool is_partitioned = PETSC_FALSE, incremental = PETSC_TRUE;
309 PetscBool rebuild_structures = PETSC_FALSE;
310 PetscInt local_from = -1;
316 &is_partitioned, PETSC_NULLPTR);
320 &rebuild_structures, PETSC_NULLPTR);
325 const int u_order =
order - 1;
326 const int hybrid_order =
order - 1;
329 simple->getAddBoundaryFE() =
false;
331 if (
simple->getDim() != 3)
333 "Tetrahedral meshes only");
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)
362 << (rebuild_structures ?
", block and nested structures rebuilt"
369 const int dofs_hybrid_per_face =
NBFACETRI_L2(hybrid_order);
372 auto count_tets = [&](
int l) {
376 bit(
l), allBits(), 3, tets),
378 return static_cast<int>(tets.size());
383 {
simple->getDomainFEName(),
384 {{
"BROKEN",
"BROKEN"},
388 {
simple->getSkeletonFEName(),
389 {{
"BROKEN",
"HYBRID"}, {
"HYBRID",
"BROKEN"}}}};
391 boost::shared_ptr<BlockStructure> kept_block;
392 boost::shared_ptr<NestSchurData> kept_nested;
397 boost::shared_ptr<BlockStructure> blockData;
398 boost::shared_ptr<NestSchurData> nestedData;
400 int tets = 0, skeletonFaces = 0,
rows = 0, schurRows = 0, blockRows = 0;
401 double hybridNnz = 0;
403 std::vector<Level> levels(
nb_levels + 1);
406 const auto tag =
"level " + std::to_string(
l);
407 auto &lv = levels[
l];
412 const bool local =
l > 0 && local_from >= 0 &&
l >= local_from;
417 lv.tets = count_tets(
l);
421 return registerLevel(m_field, *
simple,
l,
order, incremental);
426 tag +
" tets are eight per parent", lv.tets,
427 8. * levels[
l - 1].tets, zero);
432 <<
"Level " <<
l <<
" local refinement of " << refined <<
" of "
433 << levels[
l - 1].tets <<
" tets";
435 tag +
" local refinement leaves tets unchanged",
436 refined < levels[
l - 1].tets ? 1 : 0, 1, zero);
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;
451 tag +
" parent dofs = BROKEN + U + HYBRID", lv.rows, expected,
461 return subProblem(m_field, *
simple,
"SCHUR", {
"HYBRID"},
467 return subProblem(m_field, *
simple,
"BLOCK", {
"BROKEN",
"U"},
471 lv.schurRows = problemRows(m_field, lv.schurDM);
472 lv.blockRows = problemRows(m_field, lv.blockDM);
474 tag +
" SCHUR dofs = HYBRID", lv.schurRows,
475 double(lv.skeletonFaces) * dofs_hybrid_per_face, zero);
477 tag +
" BLOCK + SCHUR dofs = parent", lv.blockRows + lv.schurRows,
486 const bool rebuild = rebuild_structures && kept_block && kept_nested;
493 lv.blockData = kept_block;
499 lv.blockMat = block.first;
508 {lv.schurDM, lv.blockDM}, kept_nested);
509 lv.nestedData = kept_nested;
512 {lv.schurDM, lv.blockDM}, lv.blockData, {
"BROKEN",
"U"},
513 {
nullptr,
nullptr},
true);
516 lv.nestedMat = nested.first;
520 if (rebuild_structures) {
521 kept_block = lv.blockData;
522 kept_nested = lv.nestedData;
526 boost::shared_ptr<BlockStructure> fresh_block;
527 boost::shared_ptr<NestSchurData> fresh_nested;
528 double fresh_block_seconds, fresh_nested_seconds;
536 fresh_block_seconds);
541 {lv.schurDM, lv.blockDM}, fresh_block, {
"BROKEN",
"U"},
542 {
nullptr,
nullptr},
true);
545 fresh_nested_seconds);
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,
559 tag +
" rebuilt " + what +
" blocks", r.nbBlocks,
f.nbBlocks,
562 tag +
" rebuilt " + what +
" A00 elements", r.nbElems,
565 tag +
" rebuilt " + what +
" block index equal",
566 r.indexHash ==
f.indexHash ? 0 : 1, 0, zero);
568 tag +
" rebuilt " + what +
" blocks equal",
569 r.blocksHash ==
f.blocksHash ? 0 : 1, 0, zero);
571 tag +
" rebuilt " + what +
" A00 elements equal",
572 r.elemsHash ==
f.elemsHash ? 0 : 1, 0, zero);
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]);
583 CHKERR MatGetSize(lv.blockMat, &
m, &
n);
585 tag +
" block matrix rows = parent",
m, lv.rows, zero);
586 CHKERR MatGetSize(lv.nestedMat, &
m, &
n);
588 tag +
" nested matrix rows = parent",
m, lv.rows, zero);
603 CHKERR MatGetSize(lv.hybridMat, &
m, &
n);
605 tag +
" hybrid matrix rows = SCHUR dofs",
m, lv.schurRows, zero);
607 tag +
" hybrid matrix columns = SCHUR dofs",
n, lv.schurRows, zero);
608 CHKERR checkHybridMatrix(m_field, lv.schurDM,
l, lv.hybridMat, *
testing,
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;
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 "
627 lv.hybridMat.reset();
628 lv.nestedMat.reset();
629 lv.nestedData.reset();
631 lv.blockData.reset();
std::vector< std::array< EntityHandle, 3 > > faces
Sorted keys.
Vec t
Removal: the ring edge.
std::vector< FieldBitNumber > row
SmartPetscObj< Vec > values
void simple(double P1[], double P2[], double P3[], double c[], const int N)
#define CATCH_ERRORS
Catch errors.
#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()
@ L2
field with C-1 continuity
@ HDIV
field with continuous normal traction
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
PetscErrorCode DMMoFEMCreateSubDM(DM subdm, DM dm, const char problem_name[])
Must be called by user to set Sub DM MoFEM data structures.
PetscErrorCode DMMoFEMAddElement(DM dm, std::string fe_name)
add element to dm
PetscErrorCode DMMoFEMSetSquareProblem(DM dm, PetscBool square_problem)
set squared problem
PetscErrorCode DMMoFEMGetProblemPtr(DM dm, const MoFEM::Problem **problem_ptr)
Get pointer to problem data structure.
PetscErrorCode DMMoFEMAddSubFieldRow(DM dm, const char field_name[])
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
PetscErrorCode DMMoFEMAddSubFieldCol(DM dm, const char field_name[])
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
MoFEMErrorCode addBitRefLevel(const Range &ents, const BitRefLevel bit, int verb=QUIET) const
Add bit ref level to ref entity.
virtual MoFEMErrorCode add_ents_to_finite_element_by_dim(const EntityHandle entities, const int dim, const std::string name, const bool recursive=true)=0
add entities to finite element
virtual MoFEMErrorCode build_finite_elements(int verb=DEFAULT_VERBOSITY)=0
Build finite elements.
virtual MoFEMErrorCode build_fields(int verb=DEFAULT_VERBOSITY)=0
virtual MoFEMErrorCode add_ents_to_field_by_dim(const Range &ents, const int dim, const std::string &name, int verb=DEFAULT_VERBOSITY)=0
Add entities to field meshset.
virtual MoFEMErrorCode set_field_order(const EntityHandle meshset, const EntityType type, const std::string &name, const ApproximationOrder order, int verb=DEFAULT_VERBOSITY)=0
Set order approximation of the entities in the field.
#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.
FTensor::Index< 'l', 3 > l
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
std::bitset< BITREFLEVEL_SIZE > BitRefLevel
Bit structure attached to each entity identifying to what mesh entity is attached.
implementation of Data Operators for Forces and Sources
SchurBlockStructureDigest schurBlockStructureDigest(boost::shared_ptr< BlockStructure > data_ptr)
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.
PetscErrorCode PetscOptionsGetBool(PetscOptions *, const char pre[], const char name[], PetscBool *bval, PetscBool *set)
auto createDMHybridisedL2Matrix(DM dm)
Get smart hybridised L2 matrix from DM.
boost::shared_ptr< BlockStructure > createBlockMatStructure(DM dm, SchurFEOpsFEandFields schur_fe_op_vec)
Create block matrix structure.
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].
MoFEMErrorCode MatSetValues(Mat M, const EntitiesFieldData::EntData &row_data, const EntitiesFieldData::EntData &col_data, const double *ptr, InsertMode iora)
Assemble PETSc matrix.
MoFEMErrorCode rebuildSchurNestedMatrixStruture(std::pair< SmartPetscObj< DM >, SmartPetscObj< DM > > dms, boost::shared_ptr< NestSchurData > nest_data_ptr)
Rebuild a nested Schur structure after rebuildBlockMatStructure.
MoFEMErrorCode rebuildBlockMatStructure(DM dm, SchurFEOpsFEandFields schur_fe_op_vec, boost::shared_ptr< BlockStructure > data_ptr)
Rebuild a block matrix structure after its problem changed.
std::vector< std::pair< std::string, std::vector< SchurFieldPair > > > SchurFEOpsFEandFields
auto createDM(MPI_Comm comm, const std::string dm_type_name)
Creates smart DM object.
SchurShellMatData createBlockMat(DM dm, boost::shared_ptr< BlockStructure > data)
Create a Schur Mat object.
FTensor::Index< 'm', 3 > m
virtual moab::Interface & get_moab()=0
virtual EntityHandle get_field_meshset(const std::string name) const =0
get field meshset
virtual MPI_Comm & get_comm() const =0
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Deprecated interface functions.
Mesh refinement interface.
MoFEMErrorCode addVerticesInTheMiddleOfEdges(const EntityHandle meshset, const BitRefLevel &bit, const bool recursive=false, int verb=QUIET, EntityHandle start_v=0)
make vertices in the middle of edges in meshset and add them to refinement levels defined by bit
keeps basic data about problem
DofIdx getNbDofsRow() const
auto & getNumeredColDofsPtr() const
get access to numeredColDofsPtr storing DOFs on cols
Simple interface for fast problem set-up.
intrusive_ptr for managing petsc objects
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.