4#ifndef FUN3_HYBRID_PROBLEM_HPP
5#define FUN3_HYBRID_PROBLEM_HPP
6#include <unordered_map>
12 const Range &tets, Mat matrix,
14 const std::string &
tag,
19 std::unordered_map<EntityHandle, std::vector<PetscInt>> face_dofs;
21 if (dof->getPetscGlobalDofIdx() >= 0)
22 face_dofs[dof->getEnt()].push_back(dof->getPetscGlobalDofIdx());
26 std::unordered_map<EntityHandle, std::vector<PetscInt>> neighbors;
27 for (
auto tet : tets) {
30 std::vector<PetscInt> columns;
31 for (
auto face :
faces) {
32 auto it = face_dofs.find(face);
33 if (
it != face_dofs.end())
34 columns.insert(columns.end(),
it->second.begin(),
it->second.end());
36 for (
auto face :
faces)
37 if (face_dofs.count(face)) {
38 auto &adj = neighbors[face];
39 adj.insert(adj.end(), columns.begin(), columns.end());
43 PetscInt first_row, last_row;
44 CHKERR MatGetOwnershipRange(matrix, &first_row, &last_row);
46 CHKERR MatGetInfo(matrix, MAT_GLOBAL_SUM, &before);
47 CHKERR MatSetOption(matrix, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
48 double local_nnz = 0, root_nnz = 0;
49 for (
auto &entry : neighbors) {
50 auto &columns = entry.second;
51 std::sort(columns.begin(), columns.end());
52 columns.erase(std::unique(columns.begin(), columns.end()), columns.end());
53 const PetscInt nb_cols = columns.size();
56 static_cast<double>(nb_cols) * face_dofs.at(entry.first).size();
57 const std::vector<PetscScalar>
values(nb_cols, 1.);
58 for (
auto row : face_dofs.at(entry.first))
59 if (
row >= first_row &&
row < last_row) {
62 values.data(), INSERT_VALUES);
66 CHKERR MPI_Allreduce(&local_nnz, &expected_nnz, 1, MPI_DOUBLE, MPI_SUM,
71 tag +
" distributed graph equals complete root graph", expected_nnz,
76 tag +
" hybrid allocation covers topology non-zeros",
77 before.nz_allocated >= expected_nnz ? 0 : 1, 0, zero);
80 tag +
" serial hybrid allocation is exact", before.nz_allocated,
82 CHKERR MatAssemblyBegin(matrix, MAT_FINAL_ASSEMBLY);
83 CHKERR MatAssemblyEnd(matrix, MAT_FINAL_ASSEMBLY);
85 CHKERR MatGetInfo(matrix, MAT_GLOBAL_SUM, &after);
86 nonzeros = after.nz_used;
88 tag +
" hybrid filled non-zeros equal topology", nonzeros, expected_nnz,
91 tag +
" hybrid fill without reallocations", after.mallocs, before.mallocs,
95 CHKERR MatMissingDiagonal(matrix, &missing, &missing_row);
97 tag +
" hybrid diagonal exists in every row", missing ? 1 : 0, 0, zero);
111 std::array<PetscObjectId, 3>
dmIds{};
135 CHKERR DMClearGlobalVectors(dm);
136 CHKERR DMClearLocalVectors(dm);
137 CHKERR DMClearNamedGlobalVectors(dm);
138 CHKERR DMClearNamedLocalVectors(dm);
152 for (
const auto &
name : {
"SCHUR",
"BLOCK"})
159 parent_dm_ptr.reset();
167 const std::vector<std::string> &fields,
170 const bool update =
bool(dm);
178 for (
const auto &
field : fields) {
204 {{
"BROKEN",
"BROKEN"}, {
"U",
"U"}, {
"BROKEN",
"U"}, {
"U",
"BROKEN"}}},
206 {{
"BROKEN",
"HYBRID"}, {
"HYBRID",
"BROKEN"}}}};
212 bool refresh_registration) {
244 if (!update || refresh_registration) {
245 Range new_field_entities;
249 for (
const auto &
name : {
"BROKEN",
"U",
"HYBRID"}) {
253 const auto &keep = std::string(
name) ==
"HYBRID" ? skeleton : entities;
254 const auto removed = subtract(registered, keep);
257 registered = intersect(registered, keep);
258 const int dim = std::string(
name) ==
"HYBRID" ? 2 : 3;
259 const auto added = subtract(keep.subset_by_dimension(dim), registered);
261 CHKERR comm_ptr->synchroniseFieldEntities(
name, 0);
267 const auto new_entities =
268 update ? subtract(current, registered) : current;
269 if (!new_entities.empty())
273 new_field_entities.merge(new_entities);
275 for (
const auto &[
name, keep] :
276 std::vector<std::pair<std::string, Range>>{
284 const auto removed = subtract(registered, keep);
304 problems_ptr->buildProblemFromFields = PETSC_TRUE;
307 problems_ptr->buildProblemFromFields = from_fields;
329 {
nullptr,
nullptr},
true);
337 int order,
const std::string &
tag) {
341 "Build the hybrid problem before validating it");
343 auto fresh_block_ptr =
347 {
nullptr,
nullptr},
true);
348 auto compare = [&](
const std::string &what,
auto rebuilt_ptr,
353 if (r.nbBlocks !=
f.nbBlocks || r.nbElems !=
f.nbElems ||
354 r.indexHash !=
f.indexHash || r.blocksHash !=
f.blocksHash ||
355 r.elemsHash !=
f.elemsHash)
357 << what <<
" digest: blocks " << r.nbBlocks <<
"/" <<
f.nbBlocks
358 <<
", elements " << r.nbElems <<
"/" <<
f.nbElems <<
", index "
359 << r.indexHash <<
"/" <<
f.indexHash <<
", layout "
360 << r.blocksHash <<
"/" <<
f.blocksHash <<
", element layout "
361 << r.elemsHash <<
"/" <<
f.elemsHash;
364 tag +
" rebuilt " + what +
" equals fresh structure",
365 r.nbBlocks ==
f.nbBlocks && r.nbElems ==
f.nbElems &&
366 r.indexHash ==
f.indexHash &&
367 r.blocksHash ==
f.blocksHash &&
368 r.elemsHash ==
f.elemsHash
375 for (
int q = 0;
q != 4; ++
q)
376 CHKERR compare(
"quadrant " + std::to_string(
q),
378 std::get<1>(*fresh_nested_ptr)[
q]);
386 CHKERR PetscObjectGetId(
reinterpret_cast<PetscObject
>(dm), &
id);
387 if (!
dmIds[dm_index])
388 dmIds[dm_index] = id;
389 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
390 tag +
" DM " + std::to_string(dm_index) +
" identity retained",
391 id ==
dmIds[dm_index] ? 0 : 1, 0, zero);
398 for (
const auto &
name : {
"BROKEN",
"U",
"HYBRID"}) {
400 int identity_error = 0;
402 auto it = dofs.find(old_ptr->getLocalUniqueId());
403 if (
it != dofs.end() &&
404 (*it)->getFieldEntityPtr() == old_ptr->getFieldEntityPtr())
405 identity_error = *
it == old_ptr ? 0 : 1;
407 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
408 tag +
" surviving " +
name +
" DOF identity retained", identity_error,
412 old_ptr =
it != dofs.end() && (*it)->getBitNumber() == bit_number
416 int counts[2] = {
static_cast<int>(entities.num_of_type(MBTET)),
417 static_cast<int>(skeleton.size())};
419 const double broken =
423 const double parent_rows = block_rows + schur_rows;
424 for (
const auto &[dm, expected] :
425 std::vector<std::pair<DM, double>>{{
simplePtr->
getDM(), parent_rows},
430 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
431 tag +
" DM DOFs match complete root topology",
434 PetscInt
n, first, last;
436 CHKERR VecGetOwnershipRange(vector, &first, &last);
437 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
438 tag +
" DM vector global size",
n, expected, zero);
439 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
440 tag +
" DM vector local size", last - first,
443 for (
const auto &[matrix, expected] :
444 std::vector<std::pair<Mat, double>>{{
blockMatPtr, parent_rows},
449 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
tag +
" matrix rows",
m,
451 CHKERR testing_ptr->checkValue<TestType::SCALAR>(
tag +
" matrix columns",
457 *testing_ptr,
tag, nnz);
458 summary = {
static_cast<double>(
order), parent_rows, block_rows, schur_rows,
461 <<
"Hybrid rebuilt " <<
tag
463 <<
": parent " << parent_rows <<
", BLOCK " << block_rows <<
", SCHUR "
464 << schur_rows <<
", nonzeros " << nnz;
std::vector< std::array< EntityHandle, 3 > > faces
Sorted keys.
std::vector< int > removed
std::vector< FieldBitNumber > row
SmartPetscObj< Vec > values
#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 ...
@ MOFEM_DATA_INCONSISTENCY
#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 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.
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[])
auto createDMVector(DM dm, RowColData rc=RowColData::COL)
Get smart vector from DM.
PetscErrorCode DMMoFEMAddSubFieldCol(DM dm, const char field_name[])
PetscErrorCode DMSubDMSetUp_MoFEM(DM subdm)
virtual const DofEntity_multiIndex * get_dofs() const =0
Get the dofs object.
virtual EntityHandle get_finite_element_meshset(const std::string name) const =0
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.
virtual MoFEMErrorCode remove_ents_from_field(const std::string name, const EntityHandle meshset, const EntityType type, int verb=DEFAULT_VERBOSITY)=0
remove entities from field
#define MOFEM_LOG(channel, severity)
Log.
virtual MoFEMErrorCode modify_problem_mask_ref_level_set_bit(const std::string &name_problem, const BitRefLevel &bit)=0
set dof mask ref level for problem
virtual MoFEMErrorCode delete_problem(const std::string name)=0
Delete problem.
virtual MoFEMErrorCode modify_problem_ref_level_set_bit(const std::string &name_problem, const BitRefLevel &bit)=0
set ref level for problem
virtual bool check_problem(const std::string name)=0
check if problem exist
#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.
MoFEMErrorCode check_hybrid_matrix(MoFEM::Interface &m_field, DM dm, const Range &tets, Mat matrix, TestingInterface &testing, const std::string &tag, double &nonzeros)
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
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 DMMoFEMSetDestroyProblem(DM dm, PetscBool destroy_problem)
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)
PetscErrorCode DMMoFEMClearDMCtx(DM dm)
Clear solver and Schur data owned by a MoFEM DM context.
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 TestingInterface::checkValue< TestType::SCALAR >(const std::string &check_name, PetscReal actual, PetscReal expected, Tolerance tolerance, Sev error_severity, Sev success_severity) const
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
Retain the parent/sub-DMs, field DOFs and block storage across mesh edits.
MoFEMErrorCode buildSubProblem(const char *name, const std::vector< std::string > &fields, SmartPetscObj< DM > &dm)
Rebuild subproblem numbering while keeping its DM and field selection.
MoFEM::Interface & mField
boost::shared_ptr< NestSchurData > nestedDataPtr
MoFEMErrorCode build(const Range &entities, const Range &skeleton, const BitRefLevel &active_bit, int order, bool refresh_registration)
Construct the DMs and matrices without running validation or test fills.
std::array< double, 5 > summary
std::array< PetscObjectId, 3 > dmIds
HybridProblem(const HybridProblem &)=delete
HybridProblem(MoFEM::Interface &m_field)
std::array< boost::shared_ptr< DofEntity >, 3 > retainedDofPtrs
PetscBool checkRebuiltStructures
SmartPetscObj< Mat > blockMatPtr
SmartPetscObj< DM > blockDMPtr
HybridProblem & operator=(const HybridProblem &)=delete
MoFEMErrorCode release()
Final teardown, after all retained storage stops referencing sub-DMs.
MoFEMErrorCode prepareUpdate()
Release numeric objects and cached vectors before topology edits.
SmartPetscObj< Mat > nestedMatPtr
boost::shared_ptr< BlockStructure > blockDataPtr
PetscBool rebuildStructures
MoFEMErrorCode validate(const Range &entities, const Range &skeleton, int order, const std::string &tag)
Check the completed algebra; graph validation fills the hybrid matrix.
SchurFEOpsFEandFields getBlockOperations() const
Couplings used by both construction and the optional fresh-build oracle.
SmartPetscObj< DM > schurDMPtr
SmartPetscObj< Mat > hybridMatPtr
virtual int get_comm_size() const =0
virtual FieldBitNumber get_field_bit_number(const std::string name) const =0
get field bit number
virtual moab::Interface & get_moab()=0
virtual EntityHandle get_field_meshset(const std::string name) const =0
get field meshset
virtual bool check_finite_element(const std::string &name) const =0
Check if finite element is in database.
virtual MoFEMErrorCode build_adjacencies(const Range &ents, int verb=DEFAULT_VERBOSITY)=0
build adjacencies
virtual MPI_Comm & get_comm() const =0
virtual MoFEMErrorCode remove_ents_from_finite_element(const std::string name, const EntityHandle meshset, const EntityType type, int verb=DEFAULT_VERBOSITY)=0
remove entities from given refinement level to finite element database
virtual int get_comm_rank() const =0
Deprecated interface functions.
static UId getLoBitNumberUId(const FieldBitNumber bit_number)
keeps basic data about problem
DofIdx getNbDofsRow() const
BitRefLevel getBitRefLevel() const
BitRefLevel getBitRefLevelMask() const
DofIdx nbLocDofsRow
Local number of DOFs in row.
auto & getNumeredColDofsPtr() const
get access to numeredColDofsPtr storing DOFs on cols
Problem manager is used to build and partition problems.
PetscBool buildProblemFromFields
Simple interface for fast problem set-up.
MoFEMErrorCode buildProblem()
Build problem.
MoFEMErrorCode addDomainField(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_ZERO, int verb=-1)
Add field on domain.
bool & getAddBoundaryFE()
Get the addBoundaryFE flag.
MoFEMErrorCode defineFiniteElements()
Define finite elements.
EntityHandle & getMeshset()
Get the MeshSet object.
auto & getBitAdjParent()
Get bit ref level for parent.
MoFEMErrorCode buildFiniteElements()
Build finite elements.
const std::string getSkeletonFEName() const
Get the Skeleton FE Name.
EntityHandle & getSkeletonMeshSet()
Get the SkeletonMeshSet object.
void setDim(int dim)
Set the problem dimension.
MoFEMErrorCode getDM(DM *dm)
Get DM.
auto & getBitAdjEnt()
Get bit ref level for entities.
MoFEMErrorCode setFieldOrder(const std::string field_name, const int order, const Range *ents=NULL)
Set field order.
MoFEMErrorCode defineProblem(const PetscBool is_partitioned=PETSC_TRUE)
define problem
BitRefLevel & getBitRefLevelMask()
Get the BitRefLevelMask.
const std::string getDomainFEName() const
Get the Domain FE Name.
MoFEMErrorCode setSkeletonAdjacency(int dim=-1, std::string fe_name="")
Set the skeleton adjacency object.
MoFEMErrorCode deleteDM()
Delete dm.
BitRefLevel & getBitRefLevel()
Get the BitRefLevel.
MoFEMErrorCode deleteFiniteElements()
Delete finite elements.
MoFEMErrorCode addDomainBrokenField(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_ZERO, int verb=-1)
Add broken field on domain.
MoFEMErrorCode addSkeletonField(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_ZERO, int verb=-1)
Add field on skeleton.
intrusive_ptr for managing petsc objects
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.