168 const bool square_matrix,
171 auto dofs_field_ptr = m_field.
get_dofs();
181 SETERRQ(PETSC_COMM_SELF, 1,
"problem <%s> refinement level not set",
182 problem_ptr->
getName().c_str());
192 using DofPtrVector = std::vector<boost::shared_ptr<DofEntity>>;
193 DofPtrVector dofs_rows, dofs_cols;
194 auto make_rows_and_cols_view = [&](
auto &dofs_rows,
auto &dofs_cols) {
196 for (
auto it = ents_field_ptr->begin();
it != ents_field_ptr->end(); ++
it) {
198 const auto uid = (*it)->getLocalUniqueId();
200 auto r = adjacencies_ptr->get<
Unique_mi_tag>().equal_range(uid);
201 for (
auto lo = r.first; lo != r.second; ++lo) {
203 if ((lo->getBitFEId() & problem_ptr->
getBitFEId()).any()) {
204 std::array<bool, 2> row_col = {
false,
false};
208 const BitRefLevel &fe_bit = lo->entFePtr->getBitRefLevel();
211 if ((fe_bit & prb_mask) != fe_bit)
213 if ((fe_bit & prb_bit).none())
216 auto add_to_view = [&](
auto &nb_dofs,
auto &view,
auto rc) {
217 auto dit = nb_dofs->lower_bound(uid);
218 decltype(dit) hi_dit;
219 if (dit != nb_dofs->end()) {
220 hi_dit = nb_dofs->upper_bound(
222 view.insert(view.end(), dit, hi_dit);
227 if ((lo->entFePtr->getBitFieldIdRow() & (*it)->getId()).any())
228 add_to_view(dofs_field_ptr, dofs_rows,
ROW);
231 if ((lo->entFePtr->getBitFieldIdCol() & (*it)->getId()).any())
232 add_to_view(dofs_field_ptr, dofs_cols,
COL);
234 if (square_matrix && row_col[
ROW])
236 else if (row_col[
ROW] && row_col[
COL])
241 auto by_uid = [](
const auto &
a,
const auto &b) {
242 return a->getLocalUniqueId() < b->getLocalUniqueId();
244 for (
auto *view : {&dofs_rows, &dofs_cols}) {
245 if (!std::is_sorted(view->begin(), view->end(), by_uid))
246 std::sort(view->begin(), view->end(), by_uid);
251 CHKERR make_rows_and_cols_view(dofs_rows, dofs_cols);
262 for (
auto &dof : dofs_rows)
263 if (dof->getActive())
265 boost::shared_ptr<std::vector<NumeredDofEntity>> dofs_array =
266 boost::shared_ptr<std::vector<NumeredDofEntity>>(
267 new std::vector<NumeredDofEntity>());
269 dofs_array->reserve(count_dofs);
276 for (
auto miit = dofs_rows.begin(); miit != dofs_rows.end(); miit++) {
277 if ((*miit)->getActive()) {
278 dofs_array->emplace_back(*miit);
279 auto &dof = dofs_array->back();
280 dof.petscGloablDofIdx = (problem_ptr->
nbDofsRow)++;
283 dof.petscLocalDofIdx = dof.petscGloablDofIdx;
291 if (!square_matrix) {
299 for (
auto &dof : dofs_cols)
300 if (dof->getActive())
303 boost::shared_ptr<std::vector<NumeredDofEntity>> dofs_array =
304 boost::shared_ptr<std::vector<NumeredDofEntity>>(
305 new std::vector<NumeredDofEntity>());
307 dofs_array->reserve(count_dofs);
308 for (
auto miit = dofs_cols.begin(); miit != dofs_cols.end(); miit++) {
309 if (!(*miit)->getActive()) {
312 dofs_array->emplace_back(*miit);
313 auto &dof = dofs_array->back();
314 dof.petscGloablDofIdx = problem_ptr->
nbDofsCol++;
317 dof.petscLocalDofIdx = dof.petscGloablDofIdx;
331 << problem_ptr->
getName() <<
" Nb. local dofs "
339 <<
"FEs row dofs " << *problem_ptr <<
" Nb. row dof "
345 <<
"FEs col dofs " << *problem_ptr <<
" Nb. col dof "
489 Problem *problem_ptr,
const bool square_matrix,
int verb) {
493 auto dofs_field_ptr = m_field.
get_dofs();
499 auto *pcomm = ParallelComm::get_pcomm(
509 "problem <%s> refinement level not set",
510 problem_ptr->
getName().c_str());
518 boost::shared_ptr<NumeredDofEntity_multiIndex>(
529 using DofPtrVector = std::vector<boost::shared_ptr<DofEntity>>;
530 DofPtrVector dofs_rows, dofs_cols;
536 auto make_rows_and_cols_view = [&](
auto &dofs_rows,
auto &dofs_cols) {
540 for (
auto it = ents_field_ptr->begin();
it != ents_field_ptr->end();
543 const auto uid = (*it)->getLocalUniqueId();
545 auto r = adjacencies_ptr->get<
Unique_mi_tag>().equal_range(uid);
546 for (
auto lo = r.first; lo != r.second; ++lo) {
548 if ((lo->getBitFEId() & problem_ptr->
getBitFEId()).any()) {
549 std::array<bool, 2> row_col = {
false,
false};
551 const BitRefLevel &fe_bit = lo->entFePtr->getBitRefLevel();
554 if ((fe_bit & prb_bit).any() && (fe_bit & prb_mask) == fe_bit) {
556 auto add_to_view = [&](
auto dofs,
auto &view,
auto rc) {
557 auto dit = dofs->lower_bound(uid);
558 decltype(dit) hi_dit;
559 if (dit != dofs->end()) {
560 hi_dit = dofs->upper_bound(
562 view.insert(view.end(), dit, hi_dit);
567 if ((lo->entFePtr->getBitFieldIdRow() & (*it)->getId()).any())
568 add_to_view(dofs_field_ptr, dofs_rows,
ROW);
571 if ((lo->entFePtr->getBitFieldIdCol() & (*it)->getId()).any())
572 add_to_view(dofs_field_ptr, dofs_cols,
COL);
574 if (square_matrix && row_col[
ROW])
576 else if (row_col[
ROW] && row_col[
COL])
585 CHKERR make_rows_and_cols_view(dofs_rows, dofs_cols);
593 for (
auto fit = fe_ptr->begin(); fit != fe_ptr->end(); fit++) {
594 if ((fit->get()->getId() & problem_ptr->
getBitFEId()).any()) {
595 fields_ids_row |= fit->get()->getBitFieldIdRow();
596 fields_ids_col |= fit->get()->getBitFieldIdCol();
600 for (
auto fit = fields_ptr->begin(); fit != fields_ptr->end(); fit++) {
601 if ((fit->get()->getId() & (fields_ids_row | fields_ids_col)).any()) {
605 auto hi_dit = dofs_field_ptr->get<
Unique_mi_tag>().upper_bound(
608 for (; dit != hi_dit; dit++) {
610 const int owner_proc = dit->get()->getOwnerProc(pcomm);
612 const unsigned char pstatus = dit->get()->getPStatus(pcomm);
618 const auto &dof_bit = (*dit)->getBitRefLevel();
620 if ((dof_bit & prb_bit).any() && (dof_bit & prb_mask) == dof_bit) {
622 if ((fit->get()->getId() & fields_ids_row).any()) {
623 dofs_rows.push_back(*dit);
625 if (!square_matrix) {
626 if ((fit->get()->getId() & fields_ids_col).any()) {
627 dofs_cols.push_back(*dit);
639 BitFieldId *fields_ids[2] = {&fields_ids_row, &fields_ids_col};
640 for (FiniteElement_multiIndex::iterator fit = fe_ptr->begin();
641 fit != fe_ptr->end(); fit++) {
642 if ((fit->get()->getId() & problem_ptr->
getBitFEId()).any()) {
643 fields_ids_row |= fit->get()->getBitFieldIdRow();
644 fields_ids_col |= fit->get()->getBitFieldIdCol();
648 DofPtrVector *dofs_ptr[] = {&dofs_rows, &dofs_cols};
649 for (
int ss = 0; ss != ((square_matrix) ? 1 : 2); ++ss) {
650 Range ents_to_synchronise;
651 for (
auto &dof : *dofs_ptr[ss]) {
652 if (!dof->getActive() || dof->getEntDofIdx() != 0)
654 ents_to_synchronise.insert(dof->getEnt());
656 Range tmp_ents = ents_to_synchronise;
658 ents_to_synchronise,
nullptr, verb);
659 ents_to_synchronise = subtract(ents_to_synchronise, tmp_ents);
660 for (
auto fit = fields_ptr->begin(); fit != fields_ptr->end(); fit++) {
661 if ((fit->get()->getId() & *fields_ids[ss]).any()) {
662 const auto bit_number = (*fit)->getBitNumber();
663 for (Range::pair_iterator pit = ents_to_synchronise.pair_begin();
664 pit != ents_to_synchronise.pair_end(); ++pit) {
665 const auto f = pit->first;
666 const auto s = pit->second;
672 auto dit = dofs_field_ptr->get<
Unique_mi_tag>().lower_bound(lo_uid);
676 dofs_ptr[ss]->insert(dofs_ptr[ss]->
end(), dit, hi_dit);
686 auto finalise_view = [](DofPtrVector &view) {
687 auto by_uid = [](
const auto &
a,
const auto &b) {
688 return a->getLocalUniqueId() < b->getLocalUniqueId();
690 if (!std::is_sorted(view.begin(), view.end(), by_uid))
691 std::sort(view.begin(), view.end(), by_uid);
692 view.erase(std::unique(view.begin(), view.end(),
693 [](
const auto &
a,
const auto &b) {
694 return a->getLocalUniqueId() ==
695 b->getLocalUniqueId();
699 finalise_view(dofs_rows);
700 finalise_view(dofs_cols);
703 DofPtrVector *dofs_ptr[] = {&dofs_rows, &dofs_cols};
704 boost::shared_ptr<NumeredDofEntity_multiIndex> numered_dofs_ptr[] = {
711 for (
int ss = 0; ss < 2; ss++) {
712 *(nbdof_ptr[ss]) = 0;
713 *(local_nbdof_ptr[ss]) = 0;
714 *(ghost_nbdof_ptr[ss]) = 0;
719 int nb_local_dofs[] = {0, 0};
720 for (
int ss = 0; ss < loop_size; ss++) {
721 for (
auto &dof : *dofs_ptr[ss]) {
722 if (!dof->getActive())
730 int start_ranges[2], end_ranges[2];
731 for (
int ss = 0; ss != loop_size; ss++) {
734 CHKERR PetscLayoutSetBlockSize(layout, 1);
735 CHKERR PetscLayoutSetLocalSize(layout, nb_local_dofs[ss]);
736 CHKERR PetscLayoutSetUp(layout);
737 CHKERR PetscLayoutGetSize(layout, &*nbdof_ptr[ss]);
738 CHKERR PetscLayoutGetRange(layout, &start_ranges[ss], &end_ranges[ss]);
739 CHKERR PetscLayoutDestroy(&layout);
742 nbdof_ptr[1] = nbdof_ptr[0];
743 nb_local_dofs[1] = nb_local_dofs[0];
744 start_ranges[1] = start_ranges[0];
745 end_ranges[1] = end_ranges[0];
749 const size_t idx_data_type_size =
sizeof(
IdxDataType);
750 const size_t data_block_size = idx_data_type_size /
sizeof(int);
755 std::vector<std::vector<IdxDataType>> ids_data_packed_rows(
761 for (
int ss = 0; ss != loop_size; ++ss) {
763 boost::shared_ptr<std::vector<NumeredDofEntity>> dofs_array =
764 boost::shared_ptr<std::vector<NumeredDofEntity>>(
765 new std::vector<NumeredDofEntity>());
766 int nb_dofs_to_add = 0;
767 for (
auto &dof : *dofs_ptr[ss])
768 if (dof->getActive())
770 dofs_array->reserve(nb_dofs_to_add);
777 int &local_idx = *local_nbdof_ptr[ss];
778 for (
auto miit = dofs_ptr[ss]->begin(); miit != dofs_ptr[ss]->end();
782 if (!(miit->get()->getActive()))
785 dofs_array->emplace_back(*miit);
787 int owner_proc = dofs_array->back().getOwnerProc(pcomm);
788 if (owner_proc < 0) {
790 "data inconsistency");
794 dofs_array->back().pArt = owner_proc;
795 dofs_array->back().petscGloablDofIdx = -1;
796 dofs_array->back().petscLocalDofIdx = -1;
800 int glob_idx = start_ranges[ss] + local_idx;
801 dofs_array->back().pArt = owner_proc;
802 dofs_array->back().petscGloablDofIdx = glob_idx;
803 dofs_array->back().petscLocalDofIdx = local_idx;
806 unsigned char pstatus = dofs_array->back().getPStatus(pcomm);
810 const UId global_uid = dofs_array->back().getGlobalUniqueId();
813 proc < MAX_SHARING_PROCS &&
814 -1 != dofs_array->back().getSharingProcsPtr(pcomm)[proc];
818 ids_data_packed_rows[dofs_array->back()
819 .getSharingProcsPtr(pcomm)[proc]]
820 .emplace_back(global_uid, glob_idx);
822 ids_data_packed_cols[dofs_array->back()
823 .getSharingProcsPtr(pcomm)[proc]]
824 .emplace_back(global_uid, glob_idx);
826 if (!(pstatus & PSTATUS_MULTISHARED)) {
838 local_nbdof_ptr[1] = local_nbdof_ptr[0];
841 int nsends_rows = 0, nsends_cols = 0;
848 lengths_rows[proc] = ids_data_packed_rows[proc].size() * data_block_size;
849 lengths_cols[proc] = ids_data_packed_cols[proc].size() * data_block_size;
850 if (!ids_data_packed_rows[proc].
empty())
852 if (!ids_data_packed_cols[proc].
empty())
874 CHKERR PetscGatherNumberOfMessages(
comm, NULL, &lengths_rows[0],
880 CHKERR PetscGatherMessageLengths(
comm, nsends_rows, nrecvs_rows,
881 &lengths_rows[0], &onodes_rows,
893 MPI_Request *r_waits_row;
898 CHKERR PetscPostIrecvInt(
comm, tag_row, nrecvs_rows, onodes_rows,
899 olengths_rows, &rbuf_row, &r_waits_row);
900 CHKERR PetscFree(onodes_rows);
902 MPI_Request *s_waits_row;
903 CHKERR PetscMalloc1(nsends_rows, &s_waits_row);
906 for (
int proc = 0, kk = 0; proc < m_field.
get_comm_size(); proc++) {
907 if (!lengths_rows[proc])
909 CHKERR MPI_Isend(&(ids_data_packed_rows[proc])[0],
912 tag_row,
comm, s_waits_row + kk);
923 CHKERR PetscFree(r_waits_row);
924 CHKERR PetscFree(s_waits_row);
928 int nrecvs_cols = nrecvs_rows;
929 int *olengths_cols = olengths_rows;
930 PetscInt **rbuf_col = rbuf_row;
931 if (!square_matrix) {
934 CHKERR PetscGatherNumberOfMessages(
comm, NULL, &lengths_cols[0],
940 CHKERR PetscGatherMessageLengths(
comm, nsends_cols, nrecvs_cols,
941 &lengths_cols[0], &onodes_cols,
950 MPI_Request *r_waits_col;
951 CHKERR PetscPostIrecvInt(
comm, tag_col, nrecvs_cols, onodes_cols,
952 olengths_cols, &rbuf_col, &r_waits_col);
953 CHKERR PetscFree(onodes_cols);
955 MPI_Request *s_waits_col;
956 CHKERR PetscMalloc1(nsends_cols, &s_waits_col);
959 for (
int proc = 0, kk = 0; proc < m_field.
get_comm_size(); proc++) {
960 if (!lengths_cols[proc])
962 CHKERR MPI_Isend(&(ids_data_packed_cols[proc])[0],
965 tag_col,
comm, s_waits_col + kk);
976 CHKERR PetscFree(r_waits_col);
977 CHKERR PetscFree(s_waits_col);
985 using GlobalDof = std::pair<UId, boost::shared_ptr<DofEntity>>;
986 std::vector<GlobalDof> dofs_glob_uid_view;
987 if (nrecvs_rows > 0 || nrecvs_cols > 0) {
988 dofs_glob_uid_view.reserve(m_field.
get_dofs()->size());
991 for (
const auto &dof : *m_field.
get_dofs()) {
992 const auto &field_ptr = dof->getFieldEntityPtr();
993 if (field_ptr.get() != last_field_ptr) {
995 last_field_ptr = field_ptr.get();
997 dofs_glob_uid_view.emplace_back(
1002 std::sort(dofs_glob_uid_view.begin(), dofs_glob_uid_view.end(),
1003 [](
const GlobalDof &
a,
const GlobalDof &b) {
1004 return a.first == b.first
1005 ? a.second->getLocalUniqueId() <
1006 b.second->getLocalUniqueId()
1007 : a.first < b.first;
1012 for (
int ss = 0; ss != loop_size; ++ss) {
1018 nrecvs = nrecvs_rows;
1019 olengths = olengths_rows;
1020 data_procs = rbuf_row;
1022 nrecvs = nrecvs_cols;
1023 olengths = olengths_cols;
1024 data_procs = rbuf_col;
1028 for (
int kk = 0; kk != nrecvs; ++kk) {
1029 int len = olengths[kk];
1030 int *data_from_proc = data_procs[kk];
1031 for (
int dd = 0; dd < len; dd += data_block_size) {
1033 auto ddit = std::lower_bound(
1034 dofs_glob_uid_view.begin(), dofs_glob_uid_view.end(), uid,
1035 [](
const GlobalDof &record,
const UId &
key) {
1036 return record.first < key;
1038 if (ddit != dofs_glob_uid_view.end() && ddit->first != uid)
1039 ddit = dofs_glob_uid_view.end();
1043 PetscUnlikely(ddit == dofs_glob_uid_view.end())) {
1048 <<
"DOF is shared or multishared between processors. For example "
1049 "if order of field on given entity is set in inconsistently, "
1050 "has different value on two processor, error such as this is "
1053 MOFEM_LOG(
"SELF", Sev::error) <<
"UId " << uid <<
" is not found";
1055 <<
"Problematic UId owner proc is " << owner_proc;
1058 <<
"Problematic UId entity owning processor handle is "
1059 << uid_handle <<
" entity type "
1061 const auto uid_bit_number =
1064 <<
"Problematic UId field is "
1069 field_view.insert(ents_field_ptr->begin(), ents_field_ptr->end());
1071 owner_proc, uid_bit_number, uid_handle));
1072 if (fe_it == field_view.end()) {
1074 <<
"Also, no field entity in database for given global UId";
1076 MOFEM_LOG(
"SELF", Sev::error) <<
"Field entity in databse exist "
1077 "(but have no DOF wih give UId";
1078 MOFEM_LOG(
"SELF", Sev::error) << **fe_it;
1081 auto error_file_name =
1082 "error_with_missing_entity_" +
1086 <<
"Look to file < " << error_file_name
1087 <<
" > it contains entity with missing DOF.";
1090 const auto local_fe_ent = (*fe_it)->getEnt();
1091 CHKERR m_field.
get_moab().add_entities(*tmp_msh, &local_fe_ent, 1);
1092 CHKERR m_field.
get_moab().write_file(error_file_name.c_str(),
"VTK",
1093 "", tmp_msh->get_ptr(), 1);
1098 "DOF with global UId not found (Compile code in Debug to "
1099 "learn more about problem");
1102 if (PetscUnlikely(ddit == dofs_glob_uid_view.end())) {
1106 auto dit = numered_dofs_ptr[ss]->find(ddit->second->getLocalUniqueId());
1108 if (dit != numered_dofs_ptr[ss]->
end()) {
1112 if (PetscUnlikely(global_idx < 0))
1114 "received negative dof");
1118 const bool success = numered_dofs_ptr[ss]->modify(
1120 (*dit)->getPart(), global_idx,
1121 (*dit)->getPetscLocalDofIdx()));
1122 if (PetscUnlikely(!success))
1124 "modification unsuccessful");
1128 if (PetscUnlikely(ddit->second->getPStatus() == 0)) {
1135 std::ostringstream zz;
1136 zz << *ddit->second << std::endl;
1138 "data inconsistency, dofs is not shared, but received "
1148 if (square_matrix) {
1153 CHKERR PetscFree(olengths_rows);
1154 CHKERR PetscFree(rbuf_row[0]);
1155 CHKERR PetscFree(rbuf_row);
1156 if (!square_matrix) {
1157 CHKERR PetscFree(olengths_cols);
1158 CHKERR PetscFree(rbuf_col[0]);
1159 CHKERR PetscFree(rbuf_col);
1162 if (square_matrix) {
1163 if (numered_dofs_ptr[0]->
size() != numered_dofs_ptr[1]->
size()) {
1165 "data inconsistency for square_matrix %ld!=%ld",
1166 numered_dofs_ptr[0]->
size(), numered_dofs_ptr[1]->
size());
1170 "data inconsistency for square_matrix");
1183 const std::string out_name,
1185 const std::vector<std::string> &fields_row,
1186 const std::vector<std::string> &fields_col,
1188 const std::string main_problem,
const bool square_matrix,
1190 const map<std::string, boost::shared_ptr<Range>> *entityMapRow,
1191 const map<std::string, boost::shared_ptr<Range>> *entityMapCol,
1201 using ProblemByName =
decltype(problems_ptr->get<
Problem_mi_tag>());
1202 auto &problems_by_name =
1203 const_cast<ProblemByName &
>(problems_ptr->get<
Problem_mi_tag>());
1206 auto out_problem_it = problems_by_name.find(out_name);
1207 if (out_problem_it == problems_by_name.end()) {
1209 "subproblem with name < %s > not defined (top tip check spelling)",
1214 auto main_problem_it = problems_by_name.find(main_problem);
1215 if (main_problem_it == problems_by_name.end()) {
1217 "problem of subproblem with name < %s > not defined (top tip "
1219 main_problem.c_str());
1223 boost::shared_ptr<NumeredDofEntity_multiIndex> out_problem_dofs[] = {
1224 out_problem_it->numeredRowDofsPtr, out_problem_it->numeredColDofsPtr};
1226 boost::shared_ptr<NumeredDofEntity_multiIndex> main_problem_dofs[] = {
1227 main_problem_it->numeredRowDofsPtr, main_problem_it->numeredColDofsPtr};
1229 int *nb_local_dofs[] = {&out_problem_it->nbLocDofsRow,
1230 &out_problem_it->nbLocDofsCol};
1232 int *nb_dofs[] = {&out_problem_it->nbDofsRow, &out_problem_it->nbDofsCol};
1236 out_problem_it->nbGhostDofsRow = 0;
1237 out_problem_it->nbGhostDofsCol = 0;
1241 std::vector<std::string> fields[] = {fields_row, fields_col};
1242 const map<std::string, boost::shared_ptr<Range>> *entityMap[] = {
1243 entityMapRow, entityMapCol};
1246 out_problem_it->subProblemData =
1247 boost::make_shared<Problem::SubProblemData>();
1250 for (
int ss = 0; ss != (square_matrix ? 1 : 2); ++ss) {
1253 (*nb_local_dofs[ss]) = 0;
1256 out_problem_dofs[ss]->clear();
1259 out_problem_it->numeredFiniteElementsPtr->clear();
1265 std::vector<Record> records;
1266 std::vector<boost::shared_ptr<std::vector<NumeredDofEntity>>> sequences;
1267 auto fields_by_bit = fields[ss];
1268 std::sort(fields_by_bit.begin(), fields_by_bit.end(),
1269 [&](
const std::string &
a,
const std::string &b) {
1270 return m_field.get_field_bit_number(a) <
1271 m_field.get_field_bit_number(b);
1275 for (
auto field : fields_by_bit) {
1281 boost::shared_ptr<std::vector<NumeredDofEntity>> dofs_array =
1282 boost::make_shared<std::vector<NumeredDofEntity>>();
1283 sequences.push_back(dofs_array);
1286 out_problem_it->getRowDofsSequence()->emplace_back(dofs_array);
1288 out_problem_it->getColDofsSequence()->emplace_back(dofs_array);
1293 auto add_dit_to_dofs_array = [&](
auto &dit) {
1294 if (dit->get()->getPetscGlobalDofIdx() >= 0)
1295 dofs_array->emplace_back(
1296 dit->get()->getDofEntityPtr(), dit->get()->getPetscGlobalDofIdx(),
1297 dit->get()->getPetscLocalDofIdx(), dit->get()->getPart());
1300 auto get_dafult_dof_range = [&]() {
1301 auto dit = main_problem_dofs[ss]->get<
Unique_mi_tag>().lower_bound(
1303 auto hi_dit = main_problem_dofs[ss]->get<
Unique_mi_tag>().upper_bound(
1305 return std::make_pair(dit, hi_dit);
1308 auto check = [&](
auto &
field) -> boost::shared_ptr<Range> {
1309 if (entityMap[ss]) {
1310 auto mit = entityMap[ss]->find(
field);
1311 if (mit != entityMap[ss]->
end()) {
1321 if (
auto r_ptr = check(
field)) {
1322 for (
auto p = r_ptr->pair_begin(); p != r_ptr->pair_end(); ++p) {
1323 const auto lo_ent = p->first;
1324 const auto hi_ent = p->second;
1325 auto dit = main_problem_dofs[ss]->get<
Unique_mi_tag>().lower_bound(
1327 auto hi_dit = main_problem_dofs[ss]->get<
Unique_mi_tag>().upper_bound(
1329 dofs_array->reserve(std::distance(dit, hi_dit));
1330 for (; dit != hi_dit; dit++) {
1331 add_dit_to_dofs_array(dit);
1335 auto [dit, hi_dit] = get_dafult_dof_range();
1336 dofs_array->reserve(std::distance(dit, hi_dit));
1337 for (; dit != hi_dit; dit++)
1338 add_dit_to_dofs_array(dit);
1342 records.reserve(records.size() + dofs_array->size());
1343 for (
auto &
v : *dofs_array)
1344 records.push_back({&
v});
1350 std::vector<Record *> by_main_index;
1351 by_main_index.reserve(records.size());
1352 for (
auto &r : records)
1353 by_main_index.push_back(&r);
1354 std::sort(by_main_index.begin(), by_main_index.end(),
1355 [](
const Record *
a,
const Record *b) {
1356 return a->dof->getPetscGlobalDofIdx() <
1357 b->dof->getPetscGlobalDofIdx();
1360 for (
auto *r : by_main_index)
1361 r->dof->petscLocalDofIdx =
1362 (r->dof->getPart() == rank) ? (*nb_local_dofs[ss])++ : -1;
1367 std::vector<int> main_indices;
1368 main_indices.reserve(*nb_local_dofs[ss]);
1369 for (
auto *r : by_main_index)
1370 if (r->dof->petscLocalDofIdx >= 0)
1371 main_indices.push_back(r->dof->getPetscGlobalDofIdx());
1372 const int nb = main_indices.size();
1375 PETSC_USE_POINTER, &is);
1377 CHKERR AOCreateMappingIS(is, PETSC_NULLPTR, &ao);
1380 CHKERR ISDuplicate(is, &is_dup);
1385 CHKERR ISDuplicate(is, &is_dup);
1391 CHKERR AOApplicationToPetscIS(ao, is);
1392 CHKERR ISGetSize(is, nb_dofs[ss]);
1395 auto it = main_indices.begin();
1396 for (
auto *r : by_main_index)
1397 if (r->dof->petscLocalDofIdx >= 0) {
1398 r->dof->petscGloablDofIdx = *
it;
1405 std::vector<int> main_indices_non_local;
1406 main_indices_non_local.reserve(records.size() -
nb);
1407 for (
auto *r : by_main_index)
1408 if (r->dof->petscLocalDofIdx < 0)
1409 main_indices_non_local.push_back(r->dof->getPetscGlobalDofIdx());
1412 CHKERR ISCreateGeneral(m_field.
get_comm(), main_indices_non_local.size(),
1413 main_indices_non_local.data(), PETSC_USE_POINTER,
1415 CHKERR AOApplicationToPetscIS(ao, is_non_local);
1416 CHKERR ISDestroy(&is_non_local);
1417 auto it = main_indices_non_local.begin();
1418 for (
auto *r : by_main_index)
1419 if (r->dof->petscLocalDofIdx < 0)
1420 r->dof->petscGloablDofIdx = *
it++;
1425 for (
auto &sequence_ptr : sequences)
1429 if (square_matrix) {
1430 out_problem_it->numeredColDofsPtr = out_problem_it->numeredRowDofsPtr;
1431 out_problem_it->nbLocDofsCol = out_problem_it->nbLocDofsRow;
1432 out_problem_it->nbDofsCol = out_problem_it->nbDofsRow;
1433 out_problem_it->getSubData()->colIs = out_problem_it->getSubData()->rowIs;
1434 out_problem_it->getSubData()->colMap = out_problem_it->getSubData()->rowMap;
1437 CHKERR printPartitionedProblem(&*out_problem_it, verb);
1438 CHKERR debugPartitionedProblem(&*out_problem_it, verb);
1444 const std::string out_name,
const std::vector<std::string> add_row_problems,
1445 const std::vector<std::string> add_col_problems,
const bool square_matrix,
1447 if (!(
cOre.getBuildMoFEM() & Core::BUILD_FIELD))
1449 if (!(
cOre.getBuildMoFEM() & Core::BUILD_FE))
1451 if (!(
cOre.getBuildMoFEM() & Core::BUILD_ADJ))
1459 typedef Problem_multiIndex::index<Problem_mi_tag>::type ProblemByName;
1460 ProblemByName &problems_by_name =
1461 const_cast<ProblemByName &
>(problems_ptr->get<
Problem_mi_tag>());
1464 ProblemByName::iterator out_problem_it = problems_by_name.find(out_name);
1465 if (out_problem_it == problems_by_name.end()) {
1467 "problem with name < %s > not defined (top tip check spelling)",
1471 out_problem_it->composedProblemsData =
1472 boost::make_shared<ComposedProblemsData>();
1473 boost::shared_ptr<ComposedProblemsData> cmp_prb_data =
1474 out_problem_it->getComposedProblemsData();
1476 const std::vector<std::string> *add_prb[] = {&add_row_problems,
1478 std::vector<const Problem *> *add_prb_ptr[] = {&cmp_prb_data->rowProblemsAdd,
1479 &cmp_prb_data->colProblemsAdd};
1480 std::vector<SmartPetscObj<IS>> *add_prb_is[] = {&cmp_prb_data->rowIs,
1481 &cmp_prb_data->colIs};
1484 int *nb_local_dofs[] = {&out_problem_it->nbLocDofsRow,
1485 &out_problem_it->nbLocDofsCol};
1487 int *nb_dofs[] = {&out_problem_it->nbDofsRow, &out_problem_it->nbDofsCol};
1491 out_problem_it->nbDofsRow = 0;
1492 out_problem_it->nbDofsCol = 0;
1493 out_problem_it->nbLocDofsRow = 0;
1494 out_problem_it->nbLocDofsCol = 0;
1495 out_problem_it->nbGhostDofsRow = 0;
1496 out_problem_it->nbGhostDofsCol = 0;
1498 int nb_dofs_reserve[] = {0, 0};
1501 for (
int ss = 0; ss != ((square_matrix) ? 1 : 2); ss++) {
1502 add_prb_ptr[ss]->reserve(add_prb[ss]->
size());
1503 add_prb_is[ss]->reserve(add_prb[ss]->
size());
1504 for (std::vector<std::string>::const_iterator vit = add_prb[ss]->begin();
1505 vit != add_prb[ss]->end(); vit++) {
1506 ProblemByName::iterator prb_it = problems_by_name.find(*vit);
1507 if (prb_it == problems_by_name.end()) {
1510 "problem with name < %s > not defined (top tip check spelling)",
1513 add_prb_ptr[ss]->push_back(&*prb_it);
1517 *nb_dofs[ss] += add_prb_ptr[ss]->back()->getNbDofsRow();
1518 *nb_local_dofs[ss] += add_prb_ptr[ss]->back()->getNbLocalDofsRow();
1519 nb_dofs_reserve[ss] +=
1520 add_prb_ptr[ss]->back()->numeredRowDofsPtr->size();
1523 *nb_dofs[ss] += add_prb_ptr[ss]->back()->getNbDofsCol();
1524 *nb_local_dofs[ss] += add_prb_ptr[ss]->back()->getNbLocalDofsCol();
1525 nb_dofs_reserve[ss] +=
1526 add_prb_ptr[ss]->back()->numeredColDofsPtr->size();
1531 if (square_matrix) {
1532 add_prb_ptr[1]->reserve(add_prb_ptr[0]->
size());
1533 add_prb_is[1]->reserve(add_prb_ptr[0]->
size());
1534 out_problem_it->numeredColDofsPtr = out_problem_it->numeredRowDofsPtr;
1535 *nb_dofs[1] = *nb_dofs[0];
1536 *nb_local_dofs[1] = *nb_local_dofs[0];
1540 boost::shared_ptr<std::vector<NumeredDofEntity>> dofs_array[2];
1542 for (
int ss = 0; ss != ((square_matrix) ? 1 : 2); ss++) {
1543 dofs_array[ss] = boost::make_shared<std::vector<NumeredDofEntity>>();
1544 dofs_array[ss]->reserve(nb_dofs_reserve[ss]);
1546 out_problem_it->getRowDofsSequence()->emplace_back(dofs_array[ss]);
1548 out_problem_it->getColDofsSequence()->emplace_back(dofs_array[ss]);
1552 for (
int ss = 0; ss != ((square_matrix) ? 1 : 2); ss++) {
1553 NumeredDofEntity_multiIndex::index<PetscGlobalIdx_mi_tag>::type::iterator
1558 for (
unsigned int pp = 0; pp != add_prb_ptr[ss]->size(); pp++) {
1559 PetscInt *dofs_out_idx_ptr;
1560 int nb_local_dofs = (*add_prb_ptr[ss])[pp]->getNbLocalDofsRow();
1561 CHKERR PetscMalloc(nb_local_dofs *
sizeof(
int), &dofs_out_idx_ptr);
1563 dit = (*add_prb_ptr[ss])[pp]
1566 hi_dit = (*add_prb_ptr[ss])[pp]
1570 dit = (*add_prb_ptr[ss])[pp]
1573 hi_dit = (*add_prb_ptr[ss])[pp]
1578 for (; dit != hi_dit; dit++) {
1579 const BitRefLevel &prb_bit = out_problem_it->getBitRefLevel();
1580 const BitRefLevel &prb_mask = out_problem_it->getBitRefLevelMask();
1581 const BitRefLevel &dof_bit = dit->get()->getBitRefLevel();
1582 if ((dof_bit & prb_bit).none() || ((dof_bit & prb_mask) != dof_bit))
1585 const int part = dit->get()->getPart();
1586 const int glob_idx = shift_glob + dit->get()->getPetscGlobalDofIdx();
1588 (part == rank) ? (shift_loc + dit->get()->getPetscLocalDofIdx())
1590 dofs_array[ss]->emplace_back(dit->get()->getDofEntityPtr(), glob_idx,
1593 dofs_out_idx_ptr[is_nb++] = glob_idx;
1596 if (is_nb > nb_local_dofs) {
1598 "Data inconsistency");
1601 CHKERR ISCreateGeneral(m_field.
get_comm(), is_nb, dofs_out_idx_ptr,
1602 PETSC_OWN_POINTER, &is);
1604 (*add_prb_is[ss]).push_back(smart_is);
1606 shift_glob += (*add_prb_ptr[ss])[pp]->getNbDofsRow();
1607 shift_loc += (*add_prb_ptr[ss])[pp]->getNbLocalDofsRow();
1609 shift_glob += (*add_prb_ptr[ss])[pp]->getNbDofsCol();
1610 shift_loc += (*add_prb_ptr[ss])[pp]->getNbLocalDofsCol();
1612 if (square_matrix) {
1613 (*add_prb_ptr[1]).push_back((*add_prb_ptr[0])[pp]);
1614 (*add_prb_is[1]).push_back(smart_is);
1619 if ((*add_prb_is[1]).size() != (*add_prb_is[0]).
size()) {
1624 for (
int ss = 0; ss != ((square_matrix) ? 1 : 2); ss++) {
1625 auto &dofs = ss == 0 ? *out_problem_it->numeredRowDofsPtr
1626 : *out_problem_it->numeredColDofsPtr;
1633 *nb_local_dofs[0] = 0;
1634 *nb_local_dofs[1] = 0;
1635 for (
int ss = 0; ss != ((square_matrix) ? 1 : 2); ss++) {
1637 boost::shared_ptr<NumeredDofEntity_multiIndex> dofs_ptr;
1639 dofs_ptr = out_problem_it->numeredRowDofsPtr;
1641 dofs_ptr = out_problem_it->numeredColDofsPtr;
1643 NumeredDofEntityByUId::iterator dit, hi_dit;
1646 std::vector<int> idx;
1647 idx.reserve(std::distance(dit, hi_dit));
1649 for (; dit != hi_dit; dit++) {
1650 if (dit->get()->getPart() == (
unsigned int)m_field.
get_comm_rank()) {
1655 "modification unsuccessful");
1657 idx.push_back(dit->get()->getPetscGlobalDofIdx());
1659 if (dit->get()->getPetscLocalDofIdx() != -1) {
1661 "local index should be negative");
1665 if (square_matrix) {
1666 *nb_local_dofs[1] = *nb_local_dofs[0];
1671 CHKERR ISCreateGeneral(m_field.
get_comm(), idx.size(), &*idx.begin(),
1672 PETSC_USE_POINTER, &is);
1673 CHKERR ISGetSize(is, nb_dofs[ss]);
1674 if (square_matrix) {
1675 *nb_dofs[1] = *nb_dofs[0];
1679 CHKERR AOCreateMappingIS(is, PETSC_NULLPTR, &ao);
1680 for (
unsigned int pp = 0; pp != (*add_prb_is[ss]).
size(); pp++)
1681 CHKERR AOApplicationToPetscIS(ao, (*add_prb_is[ss])[pp]);
1685 std::vector<int> idx_new;
1686 idx_new.reserve(dofs_ptr->size());
1687 for (NumeredDofEntityByUId::iterator dit =
1690 idx_new.push_back(dit->get()->getPetscGlobalDofIdx());
1695 &*idx_new.begin(), PETSC_USE_POINTER, &is_new);
1696 CHKERR AOApplicationToPetscIS(ao, is_new);
1698 std::vector<int>::iterator vit = idx_new.begin();
1699 for (NumeredDofEntityByUId::iterator dit =
1704 dit->get()->getPart(), *(vit++)));
1707 "modification unsuccessful");
1710 CHKERR ISDestroy(&is_new);
1716 CHKERR printPartitionedProblem(&*out_problem_it, verb);
1717 CHKERR debugPartitionedProblem(&*out_problem_it, verb);
1720 cOre.getBuildMoFEM() |= Core::BUILD_PROBLEM;
1721 cOre.getBuildMoFEM() |= Core::PARTITION_PROBLEM;
1733 if (!(
cOre.getBuildMoFEM() & Core::BUILD_FIELD))
1735 if (!(
cOre.getBuildMoFEM() & Core::BUILD_FE))
1737 if (!(
cOre.getBuildMoFEM() & Core::BUILD_ADJ))
1739 if (!(
cOre.getBuildMoFEM() & Core::BUILD_PROBLEM))
1741 MOFEM_LOG(
"WORLD", Sev::verbose) <<
"Simple partition problem " <<
name;
1744 typedef Problem_multiIndex::index<Problem_mi_tag>::type ProblemByName;
1745 ProblemByName &problems_set =
1746 const_cast<ProblemByName &
>(problems_ptr->get<
Problem_mi_tag>());
1747 ProblemByName::iterator p_miit = problems_set.find(
name);
1748 if (p_miit == problems_set.end()) {
1749 SETERRQ(PETSC_COMM_SELF, 1,
1750 "problem < %s > is not found (top tip: check spelling)",
1758 NumeredDofEntitysByIdx;
1759 NumeredDofEntitysByIdx &dofs_row_by_idx =
1761 NumeredDofEntitysByIdx &dofs_col_by_idx =
1763 NumeredDofEntitysByIdx::iterator miit_row, hi_miit_row;
1764 NumeredDofEntitysByIdx::iterator miit_col, hi_miit_col;
1765 DofIdx &nb_row_local_dofs = p_miit->nbLocDofsRow;
1766 DofIdx &nb_row_ghost_dofs = p_miit->nbGhostDofsRow;
1767 nb_row_local_dofs = 0;
1768 nb_row_ghost_dofs = 0;
1769 DofIdx &nb_col_local_dofs = p_miit->nbLocDofsCol;
1770 DofIdx &nb_col_ghost_dofs = p_miit->nbGhostDofsCol;
1771 nb_col_local_dofs = 0;
1772 nb_col_ghost_dofs = 0;
1774 bool square_matrix =
false;
1775 if (p_miit->numeredRowDofsPtr == p_miit->numeredColDofsPtr) {
1776 square_matrix =
true;
1780 PetscLayout layout_row;
1781 const int *ranges_row;
1783 DofIdx nb_dofs_row = dofs_row_by_idx.size();
1785 CHKERR PetscLayoutSetBlockSize(layout_row, 1);
1786 CHKERR PetscLayoutSetSize(layout_row, nb_dofs_row);
1787 CHKERR PetscLayoutSetUp(layout_row);
1788 CHKERR PetscLayoutGetRanges(layout_row, &ranges_row);
1790 PetscLayout layout_col;
1791 const int *ranges_col;
1792 if (!square_matrix) {
1793 DofIdx nb_dofs_col = dofs_col_by_idx.size();
1795 CHKERR PetscLayoutSetBlockSize(layout_col, 1);
1796 CHKERR PetscLayoutSetSize(layout_col, nb_dofs_col);
1797 CHKERR PetscLayoutSetUp(layout_col);
1798 CHKERR PetscLayoutGetRanges(layout_col, &ranges_col);
1800 for (
unsigned int part = 0; part < (
unsigned int)m_field.
get_comm_size();
1802 miit_row = dofs_row_by_idx.lower_bound(ranges_row[part]);
1803 hi_miit_row = dofs_row_by_idx.lower_bound(ranges_row[part + 1]);
1804 if (std::distance(miit_row, hi_miit_row) !=
1805 ranges_row[part + 1] - ranges_row[part]) {
1807 PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ,
1808 "data inconsistency, std::distance(miit_row,hi_miit_row) != rend - "
1809 "rstart (%ld != %d - %d = %d) ",
1810 std::distance(miit_row, hi_miit_row), ranges_row[part + 1],
1811 ranges_row[part], ranges_row[part + 1] - ranges_row[part]);
1814 for (; miit_row != hi_miit_row; miit_row++) {
1815 bool success = dofs_row_by_idx.modify(
1817 part, (*miit_row)->getPetscGlobalDofIdx()));
1820 "modification unsuccessful");
1822 success = dofs_row_by_idx.modify(
1826 "modification unsuccessful");
1829 if (!square_matrix) {
1830 miit_col = dofs_col_by_idx.lower_bound(ranges_col[part]);
1831 hi_miit_col = dofs_col_by_idx.lower_bound(ranges_col[part + 1]);
1832 if (std::distance(miit_col, hi_miit_col) !=
1833 ranges_col[part + 1] - ranges_col[part]) {
1834 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ,
1835 "data inconsistency, std::distance(miit_col,hi_miit_col) != "
1837 "rstart (%ld != %d - %d = %d) ",
1838 std::distance(miit_col, hi_miit_col), ranges_col[part + 1],
1839 ranges_col[part], ranges_col[part + 1] - ranges_col[part]);
1842 for (; miit_col != hi_miit_col; miit_col++) {
1843 bool success = dofs_col_by_idx.modify(
1845 part, (*miit_col)->getPetscGlobalDofIdx()));
1848 "modification unsuccessful");
1850 success = dofs_col_by_idx.modify(
1854 "modification unsuccessful");
1859 CHKERR PetscLayoutDestroy(&layout_row);
1860 if (!square_matrix) {
1861 CHKERR PetscLayoutDestroy(&layout_col);
1863 if (square_matrix) {
1864 nb_col_local_dofs = nb_row_local_dofs;
1865 nb_col_ghost_dofs = nb_row_ghost_dofs;
1867 CHKERR printPartitionedProblem(&*p_miit, verb);
1868 cOre.getBuildMoFEM() |= Core::PARTITION_PROBLEM;
1878 MOFEM_LOG(
"WORLD", Sev::noisy) <<
"Partition problem " <<
name;
1883 using NumeredDofEntitysByIdx =
1884 NumeredDofEntity_multiIndex::index<Unique_mi_tag>::type;
1885 using ProblemsByName = Problem_multiIndex::index<Problem_mi_tag>::type;
1888 auto &problems_set =
1889 const_cast<ProblemsByName &
>(problems_ptr->get<
Problem_mi_tag>());
1890 auto p_miit = problems_set.find(
name);
1891 if (p_miit == problems_set.end()) {
1893 "problem with name %s not defined (top tip check spelling)",
1896 int nb_dofs_row = p_miit->getNbDofsRow();
1900 if (!(
cOre.getBuildMoFEM() & (1 << 0)))
1902 if (!(
cOre.getBuildMoFEM() & (1 << 1)))
1904 if (!(
cOre.getBuildMoFEM() & (1 << 2)))
1906 "entFEAdjacencies not build");
1907 if (!(
cOre.getBuildMoFEM() & (1 << 3)))
1912 ->createMPIAdjWithArrays<PetscGlobalIdx_mi_tag>(
name, &Adj, verb);
1917 MatView(Adj, PETSC_VIEWER_STDOUT_WORLD);
1920 MatPartitioning part;
1923 CHKERR MatPartitioningSetAdjacency(part, Adj);
1924 CHKERR MatPartitioningSetFromOptions(part);
1926 CHKERR MatPartitioningApply(part, &is);
1928 ISView(is, PETSC_VIEWER_STDOUT_WORLD);
1931 IS is_gather, is_num, is_gather_num;
1932 CHKERR ISAllGather(is, &is_gather);
1933 CHKERR ISPartitioningToNumbering(is, &is_num);
1934 CHKERR ISAllGather(is_num, &is_gather_num);
1935 const int *part_number, *petsc_idx;
1936 CHKERR ISGetIndices(is_gather, &part_number);
1937 CHKERR ISGetIndices(is_gather_num, &petsc_idx);
1938 int size_is_num, size_is_gather;
1939 CHKERR ISGetSize(is_gather, &size_is_gather);
1940 if (size_is_gather != (
int)nb_dofs_row)
1941 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ,
1942 "data inconsistency %d != %d", size_is_gather, nb_dofs_row);
1944 CHKERR ISGetSize(is_num, &size_is_num);
1945 if (size_is_num != (
int)nb_dofs_row)
1946 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ,
1947 "data inconsistency %d != %d", size_is_num, nb_dofs_row);
1949 bool square_matrix =
false;
1950 if (p_miit->numeredRowDofsPtr == p_miit->numeredColDofsPtr)
1951 square_matrix =
true;
1978 auto number_dofs = [&](
auto &dofs_idx,
auto &counter) {
1980 for (
auto miit_dofs_row = dofs_idx.begin();
1981 miit_dofs_row != dofs_idx.end(); miit_dofs_row++) {
1982 const int idx = (*miit_dofs_row)->getPetscGlobalDofIdx();
1983 const int part = part_number[idx];
1985 const bool success = dofs_idx.modify(
1987 part, petsc_idx[idx], counter++));
1990 "modification unsuccessful");
1993 const bool success = dofs_idx.modify(
1995 part, petsc_idx[idx]));
1998 "modification unsuccessful");
2006 auto &dofs_row_by_idx_no_const =
const_cast<NumeredDofEntitysByIdx &
>(
2008 int &nb_row_local_dofs = p_miit->nbLocDofsRow;
2009 int &nb_row_ghost_dofs = p_miit->nbGhostDofsRow;
2010 nb_row_local_dofs = 0;
2011 nb_row_ghost_dofs = 0;
2013 CHKERR number_dofs(dofs_row_by_idx_no_const, nb_row_local_dofs);
2015 int &nb_col_local_dofs = p_miit->nbLocDofsCol;
2016 int &nb_col_ghost_dofs = p_miit->nbGhostDofsCol;
2017 if (square_matrix) {
2018 nb_col_local_dofs = nb_row_local_dofs;
2019 nb_col_ghost_dofs = nb_row_ghost_dofs;
2021 NumeredDofEntitysByIdx &dofs_col_by_idx_no_const =
2022 const_cast<NumeredDofEntitysByIdx &
>(
2024 nb_col_local_dofs = 0;
2025 nb_col_ghost_dofs = 0;
2026 CHKERR number_dofs(dofs_col_by_idx_no_const, nb_col_local_dofs);
2029 CHKERR ISRestoreIndices(is_gather, &part_number);
2030 CHKERR ISRestoreIndices(is_gather_num, &petsc_idx);
2031 CHKERR ISDestroy(&is_num);
2032 CHKERR ISDestroy(&is_gather_num);
2033 CHKERR ISDestroy(&is_gather);
2035 CHKERR MatPartitioningDestroy(&part);
2037 CHKERR printPartitionedProblem(&*p_miit, verb);
2040 auto number_dofs = [&](
auto &dof_idx,
auto &counter) {
2042 for (
auto miit_dofs_row = dof_idx.begin(); miit_dofs_row != dof_idx.end();
2046 const auto &dof = **miit_dofs_row;
2047 if (dof.getPart() != 0 || dof.getPetscGlobalDofIdx() != counter ||
2048 dof.getPetscLocalDofIdx() != counter) {
2049 const bool success = dof_idx.modify(
2054 "modification unsuccessful");
2062 auto &dofs_row_by_idx_no_const =
const_cast<NumeredDofEntitysByIdx &
>(
2064 int &nb_row_local_dofs = p_miit->nbLocDofsRow;
2065 int &nb_row_ghost_dofs = p_miit->nbGhostDofsRow;
2066 nb_row_local_dofs = 0;
2067 nb_row_ghost_dofs = 0;
2069 CHKERR number_dofs(dofs_row_by_idx_no_const, nb_row_local_dofs);
2071 bool square_matrix =
false;
2072 if (p_miit->numeredRowDofsPtr == p_miit->numeredColDofsPtr)
2073 square_matrix =
true;
2075 int &nb_col_local_dofs = p_miit->nbLocDofsCol;
2076 int &nb_col_ghost_dofs = p_miit->nbGhostDofsCol;
2077 if (square_matrix) {
2078 nb_col_local_dofs = nb_row_local_dofs;
2079 nb_col_ghost_dofs = nb_row_ghost_dofs;
2081 NumeredDofEntitysByIdx &dofs_col_by_idx_no_const =
2082 const_cast<NumeredDofEntitysByIdx &
>(
2084 nb_col_local_dofs = 0;
2085 nb_col_ghost_dofs = 0;
2086 CHKERR number_dofs(dofs_col_by_idx_no_const, nb_col_local_dofs);
2090 cOre.getBuildMoFEM() |= 1 << 4;
2095 const std::string
name,
const std::string problem_for_rows,
bool copy_rows,
2096 const std::string problem_for_cols,
bool copy_cols,
int verb) {
2101 if (!(
cOre.getBuildMoFEM() & Core::BUILD_PROBLEM))
2104 typedef Problem_multiIndex::index<Problem_mi_tag>::type ProblemByName;
2107 ProblemByName &problems_by_name =
2108 const_cast<ProblemByName &
>(problems_ptr->get<
Problem_mi_tag>());
2109 ProblemByName::iterator p_miit = problems_by_name.find(
name);
2110 if (p_miit == problems_by_name.end()) {
2112 "problem with name < %s > not defined (top tip check spelling)",
2117 << p_miit->getName() <<
" from rows of " << problem_for_rows
2118 <<
" and columns of " << problem_for_cols;
2121 ProblemByName::iterator p_miit_row = problems_by_name.find(problem_for_rows);
2122 if (p_miit_row == problems_by_name.end()) {
2124 "problem with name < %s > not defined (top tip check spelling)",
2125 problem_for_rows.c_str());
2127 const boost::shared_ptr<NumeredDofEntity_multiIndex> dofs_row =
2128 p_miit_row->numeredRowDofsPtr;
2131 ProblemByName::iterator p_miit_col = problems_by_name.find(problem_for_cols);
2132 if (p_miit_col == problems_by_name.end()) {
2134 "problem with name < %s > not defined (top tip check spelling)",
2135 problem_for_cols.c_str());
2137 boost::shared_ptr<NumeredDofEntity_multiIndex> dofs_col =
2138 p_miit_col->numeredColDofsPtr;
2140 bool copy[] = {copy_rows, copy_cols};
2141 boost::shared_ptr<NumeredDofEntity_multiIndex> composed_dofs[] = {
2142 p_miit->numeredRowDofsPtr, p_miit->numeredColDofsPtr};
2144 int *nb_local_dofs[] = {&p_miit->nbLocDofsRow, &p_miit->nbLocDofsCol};
2145 int *nb_dofs[] = {&p_miit->nbDofsRow, &p_miit->nbDofsCol};
2146 boost::shared_ptr<NumeredDofEntity_multiIndex> copied_dofs[] = {dofs_row,
2149 for (
int ss = 0; ss < 2; ss++) {
2152 *nb_local_dofs[ss] = 0;
2156 std::vector<int> is_local, is_new;
2160 for (NumeredDofEntity_multiIndex::iterator dit =
2161 composed_dofs[ss]->begin();
2162 dit != composed_dofs[ss]->end(); dit++) {
2163 NumeredDofEntityByUId::iterator diit =
2164 dofs_by_uid.find((*dit)->getLocalUniqueId());
2165 if (diit == dofs_by_uid.end()) {
2168 "data inconsistency, could not find dof in composite problem");
2170 int part_number = (*diit)->getPart();
2171 int petsc_global_dof = (*diit)->getPetscGlobalDofIdx();
2173 success = composed_dofs[ss]->modify(
2178 "modification unsuccessful");
2180 if ((*dit)->getPart() == (
unsigned int)m_field.
get_comm_rank()) {
2181 success = composed_dofs[ss]->modify(
2185 "modification unsuccessful");
2187 is_local.push_back(petsc_global_dof);
2192 CHKERR AOCreateMapping(m_field.
get_comm(), is_local.size(), &is_local[0],
2197 for (NumeredDofEntity_multiIndex::iterator dit =
2198 composed_dofs[ss]->begin();
2199 dit != composed_dofs[ss]->end(); dit++) {
2200 is_local.push_back((*dit)->getPetscGlobalDofIdx());
2202 CHKERR AOPetscToApplication(ao, is_local.size(), &is_local[0]);
2204 for (NumeredDofEntity_multiIndex::iterator dit =
2205 composed_dofs[ss]->begin();
2206 dit != composed_dofs[ss]->end(); dit++) {
2207 int part_number = (*dit)->getPart();
2208 int petsc_global_dof = is_local[idx2++];
2210 success = composed_dofs[ss]->modify(
2215 "modification unsuccessful");
2223 for (NumeredDofEntity_multiIndex::iterator dit = copied_dofs[ss]->begin();
2224 dit != copied_dofs[ss]->end(); dit++) {
2225 std::pair<NumeredDofEntity_multiIndex::iterator, bool> p;
2226 p = composed_dofs[ss]->insert(boost::shared_ptr<NumeredDofEntity>(
2231 int part_number = (*dit)->getPart();
2232 int petsc_global_dof = (*dit)->getPetscGlobalDofIdx();
2234 const bool success = composed_dofs[ss]->modify(
2236 part_number, petsc_global_dof,
2237 (*nb_local_dofs[ss])++));
2240 "modification unsuccessful");
2243 const bool success = composed_dofs[ss]->modify(
2245 part_number, petsc_global_dof));
2248 "modification unsuccessful");
2255 CHKERR printPartitionedProblem(&*p_miit, verb);
2256 CHKERR debugPartitionedProblem(&*p_miit, verb);
2365 bool part_from_moab,
2367 int hi_proc,
int verb) {
2373 if (!(
cOre.getBuildMoFEM() & Core::BUILD_FIELD))
2376 if (!(
cOre.getBuildMoFEM() & Core::BUILD_FE))
2379 if (!(
cOre.getBuildMoFEM() & Core::BUILD_ADJ))
2381 "adjacencies not build");
2383 if (!(
cOre.getBuildMoFEM() & Core::BUILD_PROBLEM))
2386 if (!(
cOre.getBuildMoFEM() & Core::PARTITION_PROBLEM))
2388 "problem not partitioned");
2396 typedef Problem_multiIndex::index<Problem_mi_tag>::type ProblemByName;
2398 const_cast<ProblemByName &
>(problems_ptr->get<
Problem_mi_tag>());
2399 ProblemByName::iterator p_miit = problems.find(
name);
2400 if (p_miit == problems.end()) {
2402 "problem < %s > not found (top tip: check spelling)",
2408 *p_miit->numeredFiniteElementsPtr;
2411 problem_finite_elements.clear();
2415 bool do_cols_prob =
true;
2416 if (p_miit->numeredRowDofsPtr == p_miit->numeredColDofsPtr) {
2417 do_cols_prob =
false;
2420 auto get_good_elems = [&]() {
2421 auto good_elems = std::vector<
decltype(fe_ent_ptr->begin())>();
2422 good_elems.reserve(fe_ent_ptr->size());
2424 const auto prb_bit = p_miit->getBitRefLevel();
2425 const auto prb_mask = p_miit->getBitRefLevelMask();
2429 for (
auto efit = fe_ent_ptr->begin(); efit != fe_ent_ptr->end(); ++efit) {
2432 if (((*efit)->getId() & p_miit->getBitFEId()).any()) {
2434 const auto &fe_bit = (*efit)->getBitRefLevel();
2437 if ((fe_bit & prb_mask) == fe_bit && (fe_bit & prb_bit).any())
2438 good_elems.emplace_back(efit);
2445 auto good_elems = get_good_elems();
2447 auto numbered_good_elems_ptr =
2448 boost::make_shared<std::vector<NumeredEntFiniteElement>>();
2449 numbered_good_elems_ptr->reserve(good_elems.size());
2450 for (
auto &efit : good_elems)
2453 if (!do_cols_prob) {
2454 for (
auto &fe : *numbered_good_elems_ptr) {
2455 if (fe.sPtr->getRowFieldEntsPtr() == fe.sPtr->getColFieldEntsPtr()) {
2456 fe.getColFieldEntsPtr() = fe.getRowFieldEntsPtr();
2461 if (part_from_moab) {
2462 for (
auto &fe : *numbered_good_elems_ptr) {
2464 if (m_field.
get_comm_size() == 1 || fe.getEntType() == MBENTITYSET) {
2468 int proc = fe.getPartProc();
2469 if (proc == -1 && fe.getEntType() == MBVERTEX)
2470 proc = fe.getOwnerProc();
2478 if (!part_from_moab) {
2479 for (
auto &fe : *numbered_good_elems_ptr) {
2480 if (fe.getEntType() == MBENTITYSET || m_field.
get_comm_size() == 1) {
2484 CHKERR fe.sPtr->getRowDofView(*(p_miit->numeredRowDofsPtr), rows_view);
2486 for (
auto &dof_ptr : rows_view)
2487 parts[dof_ptr->pArt]++;
2488 std::vector<int>::iterator pos =
2489 max_element(parts.begin(), parts.end());
2490 const auto max_part = std::distance(parts.begin(), pos);
2496 for (
auto &fe : *numbered_good_elems_ptr) {
2498 auto check_fields_and_dofs = [&]() {
2499 if (!part_from_moab) {
2500 if (fe.getBitFieldIdRow().none() && m_field.
get_comm_size() == 0) {
2502 <<
"At least one field has to be added to element row to "
2503 "determine partition of finite element. Check element " +
2504 boost::lexical_cast<std::string>(fe.getName());
2511 if (check_fields_and_dofs()) {
2513 auto p = problem_finite_elements.insert(
2514 boost::shared_ptr<NumeredEntFiniteElement>(numbered_good_elems_ptr,
2522 auto elements_on_rank =
2523 problem_finite_elements.get<
Part_mi_tag>().equal_range(
2526 << p_miit->getName() <<
" nb. elems "
2527 << std::distance(elements_on_rank.first, elements_on_rank.second);
2529 for (
auto &fe : *fe_ptr) {
2535 <<
"Element " << fe->getName() <<
" nb. elems "
2536 << std::distance(e_range.first, e_range.second);
2542 cOre.getBuildMoFEM() |= Core::PARTITION_FE;
2552 if (!(
cOre.getBuildMoFEM() & Core::PARTITION_PROBLEM))
2554 "partition of problem not build");
2555 if (!(
cOre.getBuildMoFEM() & Core::PARTITION_FE))
2557 "partitions finite elements not build");
2566 int &nb_row_ghost_dofs = p_miit->nbGhostDofsRow;
2567 int &nb_col_ghost_dofs = p_miit->nbGhostDofsCol;
2568 nb_row_ghost_dofs = 0;
2569 nb_col_ghost_dofs = 0;
2579 p_miit->numeredFiniteElementsPtr->get<
Part_mi_tag>().equal_range(
2585 using Vec = std::vector<boost::shared_ptr<NumeredDofEntity>>;
2586 using It = Vec::iterator;
2587 It operator()(Vec &dofs_view, It &hint,
2588 boost::shared_ptr<NumeredDofEntity> &&dof) {
2589 dofs_view.emplace_back(dof);
2590 return dofs_view.end();
2595 std::vector<boost::shared_ptr<NumeredDofEntity>> fe_vec_view;
2596 auto hint_r = ghost_idx_row_view.begin();
2597 for (
auto fe_ptr = fe_range.first; fe_ptr != fe_range.second; ++fe_ptr) {
2599 fe_vec_view.clear();
2600 CHKERR EntFiniteElement::getDofView((*fe_ptr)->getRowFieldEnts(),
2601 *(p_miit->getNumeredRowDofsPtr()),
2602 fe_vec_view, Inserter());
2604 for (
auto &dof_ptr : fe_vec_view) {
2605 if (dof_ptr->getPart() != (
unsigned int)m_field.
get_comm_rank()) {
2606 hint_r = ghost_idx_row_view.emplace_hint(hint_r, dof_ptr);
2613 if (p_miit->numeredColDofsPtr == p_miit->numeredRowDofsPtr) {
2615 auto hint_c = ghost_idx_col_view.begin();
2616 for (
auto fe_ptr = fe_range.first; fe_ptr != fe_range.second; ++fe_ptr) {
2618 fe_vec_view.clear();
2619 CHKERR EntFiniteElement::getDofView((*fe_ptr)->getColFieldEnts(),
2620 *(p_miit->getNumeredColDofsPtr()),
2621 fe_vec_view, Inserter());
2623 for (
auto &dof_ptr : fe_vec_view) {
2624 if (dof_ptr->getPart() != (
unsigned int)m_field.
get_comm_rank()) {
2625 hint_c = ghost_idx_col_view.emplace_hint(hint_c, dof_ptr);
2632 int *nb_ghost_dofs[2] = {&nb_row_ghost_dofs, &nb_col_ghost_dofs};
2633 int nb_local_dofs[2] = {p_miit->nbLocDofsRow, p_miit->nbLocDofsCol};
2636 &ghost_idx_row_view, &ghost_idx_col_view};
2642 if (p_miit->numeredColDofsPtr == p_miit->numeredRowDofsPtr) {
2647 for (
int ss = 0; ss != loop_size; ++ss) {
2648 for (
auto &gid : *ghost_idx_view[ss]) {
2649 NumeredDofEntityByUId::iterator dof =
2650 dof_by_uid_no_const[ss]->find(gid->getLocalUniqueId());
2651 if (PetscUnlikely((*dof)->petscLocalDofIdx != (
DofIdx)-1))
2653 "inconsistent data, ghost dof already set");
2654 bool success = dof_by_uid_no_const[ss]->modify(
2656 if (PetscUnlikely(!success))
2658 "modification unsuccessful");
2659 (*nb_ghost_dofs[ss])++;
2662 if (loop_size == 1) {
2663 (*nb_ghost_dofs[1]) = (*nb_ghost_dofs[0]);
2669 <<
" FEs ghost dofs on problem " << p_miit->getName()
2670 <<
" Nb. ghost dof " << p_miit->getNbGhostDofsRow() <<
" by "
2671 << p_miit->getNbGhostDofsCol() <<
" Nb. local dof "
2672 << p_miit->getNbLocalDofsRow() <<
" by " << p_miit->getNbLocalDofsCol();
2677 cOre.getBuildMoFEM() |= Core::PARTITION_GHOST_DOFS;
2924 const std::string problem_name,
RowColData rc,
2925 std::vector<boost::weak_ptr<NumeredDofEntity>> &vec_dof_view,
int verb,
2957 if (numered_dofs[rc]) {
2961 "Number of DOFs in multi-index %zu and to delete %zu\n",
2962 numered_dofs[rc]->
size(), vec_dof_view.size());
2965 for (
auto weak_dit : vec_dof_view)
2966 if (
auto dit = weak_dit.lock()) {
2967 numered_dofs[rc]->erase(dit->getLocalUniqueId());
2972 "Number of DOFs in multi-index after delete %zu\n",
2973 numered_dofs[rc]->
size());
2976 int nb_local_dofs = 0;
2977 int nb_ghost_dofs = 0;
2978 for (
auto dit = numered_dofs[rc]->get<PetscLocalIdx_mi_tag>().begin();
2980 if ((*dit)->getPetscLocalDofIdx() >= 0 &&
2981 (*dit)->getPetscLocalDofIdx() < *(local_nbdof_ptr[rc]))
2983 else if ((*dit)->getPetscLocalDofIdx() >= *(local_nbdof_ptr[rc]))
2987 "Impossible case. You could run problem on no distributed "
2988 "mesh. That is not implemented. Dof local index is %d",
2989 (*dit)->getPetscLocalDofIdx());
2993 auto get_indices_by_tag = [&](
auto tag,
auto &indices,
bool only_local) {
2994 const int nb_dofs = numered_dofs[rc]->size();
2996 indices.reserve(nb_dofs);
2997 for (
auto dit = numered_dofs[rc]->get<
decltype(
tag)>().begin();
2998 dit != numered_dofs[rc]->get<
decltype(
tag)>().end(); ++dit) {
3001 if ((*dit)->getPetscLocalDofIdx() < 0 ||
3002 (*dit)->getPetscLocalDofIdx() >= *(local_nbdof_ptr[rc])) {
3007 indices.push_back(
decltype(
tag)::get_index(dit));
3011 auto get_indices_by_uid = [&](
auto tag,
auto &indices) {
3012 const int nb_dofs = numered_dofs[rc]->size();
3014 indices.reserve(nb_dofs);
3015 for (
auto dit = numered_dofs[rc]->begin(); dit != numered_dofs[rc]->end();
3017 indices.push_back(
decltype(
tag)::get_index(dit));
3020 auto get_sub_ao = [&](
auto sub_data) {
3022 return sub_data->getSmartRowMap();
3024 return sub_data->getSmartColMap();
3028 auto set_sub_is_and_ao = [&rc, &prb_ptr](
auto sub_data,
auto is,
auto ao) {
3030 sub_data->rowIs = is;
3031 sub_data->rowMap = ao;
3033 sub_data->colIs = is;
3034 sub_data->colMap = ao;
3037 sub_data->colIs = is;
3038 sub_data->colMap = ao;
3042 auto apply_symmetry = [&rc, &prb_ptr](
auto sub_data) {
3045 sub_data->colIs = sub_data->getSmartRowIs();
3046 sub_data->colMap = sub_data->getSmartRowMap();
3051 auto concatenate_dofs = [&](
auto tag,
auto &indices,
3052 const auto local_only) {
3054 get_indices_by_tag(
tag, indices, local_only);
3056 auto indices_ptr = [](
auto &indices) {
3057 return indices.empty() ? nullptr : indices.data();
3065 indices_ptr(indices), PETSC_NULLPTR);
3068 indices_ptr(indices), PETSC_NULLPTR);
3075 indices_ptr(indices), PETSC_COPY_VALUES);
3078 auto sub_ao = get_sub_ao(sub_data);
3080 CHKERR AOPetscToApplicationIS(sub_ao, sub_is);
3086 set_sub_is_and_ao(sub_data, sub_is, sub_ao);
3087 apply_symmetry(sub_data);
3090 prb_ptr->
getSubData() = boost::make_shared<Problem::SubProblemData>();
3092 indices_ptr(indices), PETSC_COPY_VALUES);
3094 set_sub_is_and_ao(prb_ptr->
getSubData(), sub_is, ao);
3099 get_indices_by_uid(
tag, indices);
3100 CHKERR AOApplicationToPetsc(ao, indices.size(), indices_ptr(indices));
3106 auto set_concatenated_indices = [&]() {
3107 std::vector<int> global_indices;
3108 std::vector<int> local_indices;
3112 auto gi = global_indices.begin();
3113 auto li = local_indices.begin();
3114 for (
auto dit = numered_dofs[rc]->begin(); dit != numered_dofs[rc]->end();
3117 (*dit)->getPart(), *gi, *li);
3118 bool success = numered_dofs[rc]->modify(dit, mod);
3121 "can not set negative indices");
3127 CHKERR set_concatenated_indices();
3129 MPI_Allreduce(&nb_local_dofs, nbdof_ptr[rc], 1, MPI_INT, MPI_SUM,
3131 *(local_nbdof_ptr[rc]) = nb_local_dofs;
3132 *(ghost_nbdof_ptr[rc]) = nb_ghost_dofs;
3135 for (
auto dof : (*numered_dofs[rc])) {
3136 if (dof->getPetscGlobalDofIdx() < 0) {
3138 "Negative global idx");
3140 if (dof->getPetscLocalDofIdx() < 0) {
3142 "Negative local idx");
3148 *(nbdof_ptr[1]) = *(nbdof_ptr[0]);
3149 *(local_nbdof_ptr[1]) = *(local_nbdof_ptr[0]);
3150 *(ghost_nbdof_ptr[1]) = *(ghost_nbdof_ptr[0]);
3155 "WORLD", Sev::inform,
3156 "Removed DOFs from problem %s dofs [%d / %d (before %d / %d) global]",
3158 prb_ptr->
getNbDofsCol(), nb_init_row_dofs, nb_init_col_dofs);
3160 "Removed DOFs from problem %s dofs [ %d / %d "
3161 "(before %d / %d) local, %d / %d (before %d / %d)]",
3166 nb_init_ghost_col_dofs);
3174 const std::string problem_name,
const std::string
field_name,
3175 const Range ents,
const int lo_coeff,
const int hi_coeff,
3176 const int lo_order,
const int hi_order,
int verb,
const bool debug) {
3200 for (
int s = 0;
s != 2; ++
s)
3201 if (numered_dofs[
s]) {
3203 using NumeredDofEntity_it_view_multiIndex = multi_index_container<
3205 NumeredDofEntity_multiIndex::iterator, indexed_by<sequenced<>>
3210 NumeredDofEntity_it_view_multiIndex dofs_it_view;
3213 for (
auto pit = ents.const_pair_begin(); pit != ents.const_pair_end();
3216 DofEntity::getLoFieldEntityUId(bit_number, pit->first));
3218 DofEntity::getHiFieldEntityUId(bit_number, pit->second));
3220 for (; lo != hi; ++lo)
3221 if ((*lo)->getDofCoeffIdx() >= lo_coeff &&
3222 (*lo)->getDofCoeffIdx() <= hi_coeff &&
3223 (*lo)->getDofOrder() >= lo_order &&
3224 (*lo)->getDofOrder() <= hi_order)
3225 dofs_it_view.emplace_back(numered_dofs[
s]->project<0>(lo));
3229 for (
auto &dof : dofs_it_view)
3235 std::vector<boost::weak_ptr<NumeredDofEntity>> dofs_weak_view;
3236 dofs_weak_view.reserve(dofs_it_view.size());
3237 for (
auto dit : dofs_it_view)
3238 dofs_weak_view.push_back(*dit);
3242 "Number of DOFs in multi-index %zu and to delete %zu\n",
3243 numered_dofs[
s]->
size(), dofs_it_view.size());
3246 for (
auto weak_dit : dofs_weak_view)
3247 if (
auto dit = weak_dit.lock()) {
3248 numered_dofs[
s]->erase(dit->getLocalUniqueId());
3253 "Number of DOFs in multi-index after delete %zu\n",
3254 numered_dofs[
s]->
size());
3257 int nb_local_dofs = 0;
3258 int nb_ghost_dofs = 0;
3259 for (
auto dit = numered_dofs[
s]->get<PetscLocalIdx_mi_tag>().begin();
3261 if ((*dit)->getPetscLocalDofIdx() >= 0 &&
3262 (*dit)->getPetscLocalDofIdx() < *(local_nbdof_ptr[
s]))
3264 else if ((*dit)->getPetscLocalDofIdx() >= *(local_nbdof_ptr[
s]))
3268 "Impossible case. You could run problem on no distributed "
3269 "mesh. That is not implemented. Dof local index is %d",
3270 (*dit)->getPetscLocalDofIdx());
3274 auto get_indices_by_tag = [&](
auto tag,
auto &indices,
bool only_local) {
3275 const int nb_dofs = numered_dofs[
s]->size();
3277 indices.reserve(nb_dofs);
3278 for (
auto dit = numered_dofs[
s]->get<
decltype(
tag)>().begin();
3279 dit != numered_dofs[
s]->get<
decltype(
tag)>().end(); ++dit) {
3282 if ((*dit)->getPetscLocalDofIdx() < 0 ||
3283 (*dit)->getPetscLocalDofIdx() >= *(local_nbdof_ptr[
s])) {
3288 indices.push_back(
decltype(
tag)::get_index(dit));
3292 auto get_indices_by_uid = [&](
auto tag,
auto &indices) {
3293 const int nb_dofs = numered_dofs[
s]->size();
3295 indices.reserve(nb_dofs);
3296 for (
auto dit = numered_dofs[
s]->begin(); dit != numered_dofs[
s]->end();
3298 indices.push_back(
decltype(
tag)::get_index(dit));
3301 auto get_sub_ao = [&](
auto sub_data) {
3303 return sub_data->getSmartRowMap();
3305 return sub_data->getSmartColMap();
3309 auto set_sub_is_and_ao = [&
s, &prb_ptr](
auto sub_data,
auto is,
auto ao) {
3311 sub_data->rowIs = is;
3312 sub_data->rowMap = ao;
3314 sub_data->colIs = is;
3315 sub_data->colMap = ao;
3318 sub_data->colIs = is;
3319 sub_data->colMap = ao;
3323 auto apply_symmetry = [&
s, &prb_ptr](
auto sub_data) {
3326 sub_data->colIs = sub_data->getSmartRowIs();
3327 sub_data->colMap = sub_data->getSmartRowMap();
3332 auto concatenate_dofs = [&](
auto tag,
auto &indices,
3333 const auto local_only) {
3335 get_indices_by_tag(
tag, indices, local_only);
3337 auto indices_ptr = [](
auto &indices) {
3338 return indices.empty() ? nullptr : indices.data();
3346 indices_ptr(indices), PETSC_NULLPTR);
3349 indices_ptr(indices), PETSC_NULLPTR);
3357 indices_ptr(indices), PETSC_COPY_VALUES);
3360 auto sub_ao = get_sub_ao(sub_data);
3362 CHKERR AOPetscToApplicationIS(sub_ao, sub_is);
3368 set_sub_is_and_ao(sub_data, sub_is, sub_ao);
3369 apply_symmetry(sub_data);
3373 boost::make_shared<Problem::SubProblemData>();
3376 indices_ptr(indices), PETSC_COPY_VALUES);
3378 set_sub_is_and_ao(prb_ptr->
getSubData(), sub_is, ao);
3383 get_indices_by_uid(
tag, indices);
3384 CHKERR AOApplicationToPetsc(ao, indices.size(), indices_ptr(indices));
3390 auto set_concatenated_indices = [&]() {
3391 std::vector<int> global_indices;
3392 std::vector<int> local_indices;
3396 auto gi = global_indices.begin();
3397 auto li = local_indices.begin();
3398 for (
auto dit = numered_dofs[
s]->begin(); dit != numered_dofs[
s]->end();
3401 (*dit)->getPart(), *gi, *li);
3402 bool success = numered_dofs[
s]->modify(dit, mod);
3405 "can not set negative indices");
3411 CHKERR set_concatenated_indices();
3413 MPI_Allreduce(&nb_local_dofs, nbdof_ptr[
s], 1, MPI_INT, MPI_SUM,
3415 *(local_nbdof_ptr[
s]) = nb_local_dofs;
3416 *(ghost_nbdof_ptr[
s]) = nb_ghost_dofs;
3419 for (
auto dof : (*numered_dofs[
s])) {
3420 if (dof->getPetscGlobalDofIdx() < 0) {
3422 "Negative global idx");
3424 if (dof->getPetscLocalDofIdx() < 0) {
3426 "Negative local idx");
3432 *(nbdof_ptr[1]) = *(nbdof_ptr[0]);
3433 *(local_nbdof_ptr[1]) = *(local_nbdof_ptr[0]);
3434 *(ghost_nbdof_ptr[1]) = *(ghost_nbdof_ptr[0]);
3439 "WORLD", Sev::inform,
3440 "Removed DOFs from problem %s dofs [%d / %d (before %d / %d) global]",
3442 prb_ptr->
getNbDofsCol(), nb_init_row_dofs, nb_init_col_dofs);
3444 "Removed DOFs from problem %s dofs [ %d / %d "
3445 "(before %d / %d) local, %d / %d (before %d / %d)]",
3450 nb_init_ghost_col_dofs);
3458 const std::string problem_name,
const std::string
field_name,
3459 const Range ents,
const int lo_coeff,
const int hi_coeff,
3460 const int lo_order,
const int hi_order,
int verb,
const bool debug) {
3484 const std::array<int, 2> nb_init_dofs = {nb_init_row_dofs, nb_init_col_dofs};
3486 for (
int s = 0;
s != 2; ++
s)
3487 if (numered_dofs[
s]) {
3489 typedef multi_index_container<
3491 NumeredDofEntity_multiIndex::iterator, indexed_by<sequenced<>>
3494 NumeredDofEntity_it_view_multiIndex;
3497 NumeredDofEntity_it_view_multiIndex dofs_it_view;
3500 for (
auto pit = ents.const_pair_begin(); pit != ents.const_pair_end();
3503 DofEntity::getLoFieldEntityUId(bit_number, pit->first));
3505 DofEntity::getHiFieldEntityUId(bit_number, pit->second));
3507 for (; lo != hi; ++lo)
3508 if ((*lo)->getDofCoeffIdx() >= lo_coeff &&
3509 (*lo)->getDofCoeffIdx() <= hi_coeff &&
3510 (*lo)->getDofOrder() >= lo_order &&
3511 (*lo)->getDofOrder() <= hi_order)
3512 dofs_it_view.emplace_back(numered_dofs[
s]->project<0>(lo));
3516 for (
auto &dof : dofs_it_view)
3522 for (
auto dit : dofs_it_view) {
3523 bool success = numered_dofs[
s]->modify(dit, mod);
3526 "can not set negative indices");
3530 std::vector<boost::weak_ptr<NumeredDofEntity>> dosf_weak_view;
3531 dosf_weak_view.reserve(dofs_it_view.size());
3532 for (
auto dit : dofs_it_view)
3533 dosf_weak_view.push_back(*dit);
3537 "Number of DOFs in multi-index %zu and to delete %zu\n",
3538 numered_dofs[
s]->
size(), dofs_it_view.size());
3541 for (
auto weak_dit : dosf_weak_view)
3542 if (
auto dit = weak_dit.lock()) {
3543 numered_dofs[
s]->erase(dit->getLocalUniqueId());
3548 "Number of DOFs in multi-index after delete %zu\n",
3549 numered_dofs[
s]->
size());
3552 int nb_global_dof = 0;
3553 int nb_local_dofs = 0;
3554 int nb_ghost_dofs = 0;
3556 for (
auto dit = numered_dofs[
s]->begin(); dit != numered_dofs[
s]->end();
3559 if ((*dit)->getPetscGlobalDofIdx() >= 0) {
3561 if ((*dit)->getPetscLocalDofIdx() >= 0 &&
3562 (*dit)->getPetscLocalDofIdx() < *(local_nbdof_ptr[
s]))
3564 else if ((*dit)->getPetscLocalDofIdx() >= *(local_nbdof_ptr[
s]))
3572 MPI_Allreduce(&nb_local_dofs, nbdof_ptr[
s], 1, MPI_INT, MPI_SUM,
3574 if (*(nbdof_ptr[
s]) != nb_global_dof)
3576 "Number of local DOFs do not add up %d != %d",
3577 *(nbdof_ptr[
s]), nb_global_dof);
3580 *(nbdof_ptr[
s]) = nb_global_dof;
3581 *(local_nbdof_ptr[
s]) = nb_local_dofs;
3582 *(ghost_nbdof_ptr[
s]) = nb_ghost_dofs;
3587 auto get_indices_by_tag = [&](
auto tag) {
3588 std::vector<int> indices;
3589 indices.resize(nb_init_dofs[
s], -1);
3591 numered_dofs[
s]->get<PetscGlobalIdx_mi_tag>().lower_bound(0);
3594 indices[(*dit)->getPetscGlobalDofIdx()] =
3595 decltype(
tag)::get_index(dit);
3600 auto renumber = [&](
auto tag,
auto &indices) {
3603 for (
auto dit = numered_dofs[
s]->get<
decltype(
tag)>().lower_bound(0);
3604 dit != numered_dofs[
s]->get<
decltype(
tag)>().end(); ++dit) {
3605 indices[(*dit)->getPetscGlobalDofIdx()] = idx++;
3610 auto get_sub_ao = [&](
auto sub_data) {
3612 return sub_data->getSmartRowMap();
3614 return sub_data->getSmartColMap();
3618 auto set_sub_is_and_ao = [&
s, &prb_ptr](
auto sub_data,
auto is,
auto ao) {
3620 sub_data->rowIs = is;
3621 sub_data->rowMap = ao;
3623 sub_data->colIs = is;
3624 sub_data->colMap = ao;
3627 sub_data->colIs = is;
3628 sub_data->colMap = ao;
3632 auto apply_symmetry = [&
s, &prb_ptr](
auto sub_data) {
3635 sub_data->colIs = sub_data->getSmartRowIs();
3636 sub_data->colMap = sub_data->getSmartRowMap();
3641 auto set_sub_data = [&](
auto &indices) {
3643 auto indices_ptr = [](
auto &indices) {
3644 return indices.empty() ? nullptr : indices.data();
3649 indices_ptr(indices), PETSC_COPY_VALUES);
3652 auto sub_ao = get_sub_ao(sub_data);
3654 CHKERR AOPetscToApplicationIS(sub_ao, sub_is);
3657 set_sub_is_and_ao(sub_data, sub_is, sub_ao);
3658 apply_symmetry(sub_data);
3660 prb_ptr->
getSubData() = boost::make_shared<Problem::SubProblemData>();
3662 indices_ptr(indices), PETSC_COPY_VALUES);
3665 set_sub_is_and_ao(prb_ptr->
getSubData(), sub_is, sub_ao);
3673 CHKERR set_sub_data(global_indices);
3677 for (
auto dit = numered_dofs[
s]->begin(); dit != numered_dofs[
s]->end();
3679 const auto idx = (*dit)->getPetscGlobalDofIdx();
3682 (*dit)->getPart(), global_indices[idx], local_indices[idx]);
3683 bool success = numered_dofs[
s]->modify(dit, mod);
3686 "can not set negative indices");
3689 (*dit)->getPart(), -1, -1);
3690 bool success = numered_dofs[
s]->modify(dit, mod);
3693 "can not set negative indices");
3698 for (
auto dof : (*numered_dofs[
s])) {
3699 if (dof->getPetscGlobalDofIdx() >= *(nbdof_ptr[
s])) {
3701 "Global idx out of range");
3708 *(nbdof_ptr[1]) = *(nbdof_ptr[0]);
3709 *(local_nbdof_ptr[1]) = *(local_nbdof_ptr[0]);
3710 *(ghost_nbdof_ptr[1]) = *(ghost_nbdof_ptr[0]);
3718 "WORLD", Sev::inform,
3719 "Removed DOFs from problem %s dofs [%d / %d (before %d / %d) global]",
3721 prb_ptr->
getNbDofsCol(), nb_init_row_dofs, nb_init_col_dofs);
3723 "Removed DOFs from problem %s dofs [ %d / %d "
3724 "(before %d / %d) local, %d / %d (before %d / %d)]",
3729 nb_init_ghost_col_dofs);