v0.16.3
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
MoFEM::OpSchurAssembleEndImpl< OP_SCHUR_ASSEMBLE_BASE > Struct Template Reference

Assemble Schur complement (Implementation) More...

Inheritance diagram for MoFEM::OpSchurAssembleEndImpl< OP_SCHUR_ASSEMBLE_BASE >:
[legend]
Collaboration diagram for MoFEM::OpSchurAssembleEndImpl< OP_SCHUR_ASSEMBLE_BASE >:
[legend]

Public Types

using OP = OP_SCHUR_ASSEMBLE_BASE
 

Public Member Functions

 OpSchurAssembleEndImpl (std::vector< std::string > fields_name, std::vector< boost::shared_ptr< Range > > field_ents, SmartPetscObj< AO > schur_ao, SmartPetscObj< Mat > schur_mat, bool sym_schur, bool symm_op)
 Construct a new Op Schur Assemble End object.
 

Protected Member Functions

template<typename I >
MoFEMErrorCode doWorkImpl (int side, EntityType type, EntitiesFieldData::EntData &data)
 

Protected Attributes

std::vector< std::string > fieldsName
 
std::vector< boost::shared_ptr< Range > > fieldEnts
 
SmartPetscObj< AO > schurAO
 
SmartPetscObj< Mat > schurMat
 
bool symSchur
 
MatrixDouble blockMat
 
MatrixDouble invMat
 
MatrixDouble bM
 
MatrixDouble abM
 
MatrixDouble abcM
 

Detailed Description

template<typename OP_SCHUR_ASSEMBLE_BASE>
struct MoFEM::OpSchurAssembleEndImpl< OP_SCHUR_ASSEMBLE_BASE >

Assemble Schur complement (Implementation)

Definition at line 88 of file Schur.cpp.

Member Typedef Documentation

◆ OP

Definition at line 90 of file Schur.cpp.

Constructor & Destructor Documentation

◆ OpSchurAssembleEndImpl()

template<typename OP_SCHUR_ASSEMBLE_BASE >
MoFEM::OpSchurAssembleEndImpl< OP_SCHUR_ASSEMBLE_BASE >::OpSchurAssembleEndImpl ( std::vector< std::string >  fields_name,
std::vector< boost::shared_ptr< Range > >  field_ents,
SmartPetscObj< AO >  schur_ao,
SmartPetscObj< Mat >  schur_mat,
bool  sym_schur,
bool  symm_op 
)

Construct a new Op Schur Assemble End object.

Parameters
fields_namelist of fields
field_entslist of entities on which schur complement is applied (can be empty)
schur_aomap indices to schur matrix indices
schur_matschur matrix
sym_schurtrue if schur is symmetric
symm_optrue if block diagonal is symmetric

Definition at line 855 of file Schur.cpp.

860 : OP(NOSPACE, OP::OPSPACE, symm_op), fieldsName(fields_name),
861 fieldEnts(field_ents), schurAO(schur_ao), schurMat(schur_mat),
862 symSchur(sym_schur) {}
@ NOSPACE
Definition definitions.h:83
SmartPetscObj< AO > schurAO
Definition Schur.cpp:122
OP_SCHUR_ASSEMBLE_BASE OP
Definition Schur.cpp:90
SmartPetscObj< Mat > schurMat
Definition Schur.cpp:123
std::vector< boost::shared_ptr< Range > > fieldEnts
Definition Schur.cpp:121
std::vector< std::string > fieldsName
Definition Schur.cpp:120

Member Function Documentation

◆ doWorkImpl()

template<typename OP_SCHUR_ASSEMBLE_BASE >
template<typename I >
MoFEMErrorCode MoFEM::OpSchurAssembleEndImpl< OP_SCHUR_ASSEMBLE_BASE >::doWorkImpl ( int  side,
EntityType  type,
EntitiesFieldData::EntData &  data 
)
protected

Definition at line 866 of file Schur.cpp.

867 {
868
870
871#ifndef NDEBUG
872 PetscLogEventBegin(SchurEvents::MOFEM_EVENT_opSchurAssembleEnd, 0, 0, 0, 0);
873#endif
874
875#ifndef NDEBUG
876 if constexpr (debug_schur)
877 MOFEM_LOG("SELF", Sev::noisy) << "Schur assemble begin -> end";
878#endif
879
880 auto get_a00_uids = [&]() {
881 auto get_field_bit = [&](auto &name) {
882 return OP::getPtrFE()->mField.get_field_bit_number(name);
883 };
884
885 std::vector<std::pair<UId, UId>> a00_uids;
886 a00_uids.reserve(fieldsName.size());
887 for (auto ss = 0; ss != fieldsName.size(); ++ss) {
888 auto field_bit = get_field_bit(fieldsName[ss]);
889 auto row_ents = fieldEnts[ss];
890 if (row_ents) {
891 for (auto p = row_ents->pair_begin(); p != row_ents->pair_end(); ++p) {
892 auto lo_uid =
893 FieldEntity::getLoLocalEntityBitNumber(field_bit, p->first);
894 auto hi_uid =
895 FieldEntity::getHiLocalEntityBitNumber(field_bit, p->second);
896 a00_uids.push_back(std::make_pair(lo_uid, hi_uid));
897 }
898 } else {
900 field_bit, get_id_for_min_type<MBVERTEX>());
902 field_bit, get_id_for_max_type<MBENTITYSET>());
903 a00_uids.push_back(std::make_pair(lo_uid, hi_uid));
904 }
905 }
906 return a00_uids;
907 };
908
909#ifndef NDEBUG
910 auto get_field_name = [&](auto uid) {
911 return OP::getPtrFE()->mField.get_field_name(field_bit_from_bit_number(
913 };
914 auto list_storage = [&](auto &storage) {
916 int i = 0;
917 for (auto &p : storage) {
918 MOFEM_LOG("SELF", Sev::noisy)
919 << "List schur storage: " << i << " " << p->iDX << ": "
920 << get_field_name(p->uidRow) << " " << get_field_name(p->uidCol)
921 << " : " << p->getMat().size1() << " " << p->getMat().size2();
922 ++i;
923 }
925 };
926#endif // NDEBUG
927
928 auto assemble_dense_blocks = [&]() {
929 using matrix_range = ublas::matrix_range<MatrixDouble>;
930 using range = ublas::range;
932 auto &storage = SchurElemMats::schurL2Storage;
933
934 auto assemble_schur = [this](auto &m, auto &uid_row, auto &uid_col,
935 auto *row_ind_ptr, auto *col_ind_ptr) {
937
938 if (schurMat) {
939
940 if (auto ierr = this->assembleSchurMat(
941
942 schurMat, uid_row, *row_ind_ptr, uid_col, *col_ind_ptr, m,
943 ADD_VALUES
944
945 )) {
946#ifndef NDEBUG
947 auto field_ents = OP::getPtrFE()->mField.get_field_ents();
948 auto row_ent_it = field_ents->find(uid_row);
949 auto col_ent_it = field_ents->find(uid_col);
950 MOFEM_LOG_CHANNEL("SELF");
951 if (row_ent_it != field_ents->end())
952 MOFEM_LOG("SELF", Sev::error)
953 << "Assemble row entity: " << (*row_ent_it)->getName() << " "
954 << (*col_ent_it)->getEntTypeName() << " side "
955 << (*row_ent_it)->getSideNumber();
956 if (col_ent_it != field_ents->end())
957 MOFEM_LOG("SELF", Sev::error)
958 << "Assemble col entity: " << (*col_ent_it)->getName() << " "
959 << (*col_ent_it)->getEntTypeName() << " side "
960 << (*col_ent_it)->getSideNumber();
961#endif // NDEBUG
962 CHK_THROW_MESSAGE(ierr, "MatSetValues");
963 }
964 }
965
967 };
968
969 auto a00_uids = get_a00_uids();
970
971 auto get_block_indexing = [&](auto &a00_uids) {
972 // iterate over a00 uids and find blocks
973 std::vector<const SchurElemMats *> block_list;
974 block_list.reserve(storage.size());
975 for (auto &rp_uid : a00_uids) {
976 auto [rlo_uid, rhi_uid] = rp_uid;
977 for (auto &cp_uid : a00_uids) {
978 auto [clo_uid, chi_uid] = cp_uid;
979
980 auto it =
981 storage.template get<SchurElemMats::uid_mi_tag>().lower_bound(
982 boost::make_tuple(rlo_uid, clo_uid));
983 auto hi_it =
984 storage.template get<SchurElemMats::uid_mi_tag>().upper_bound(
985 boost::make_tuple(rhi_uid, chi_uid));
986
987 for (; it != hi_it; ++it) {
988 if ((*it)->uidRow >= rlo_uid && (*it)->uidRow < rhi_uid &&
989 (*it)->uidCol >= clo_uid && (*it)->uidCol < chi_uid) {
990 block_list.push_back(*it);
991 }
992 }
993 }
994 }
995
996 // create block indexes map for blockMat
997 std::map<UId, std::pair<size_t, const VectorInt *>>
998 block_indexing; // uid block map
999 for (auto d : block_list) {
1000 if (block_indexing.find(d->uidRow) == block_indexing.end()) {
1001 block_indexing[d->uidRow] =
1002 std::make_pair(d->getRowInd().size(), &(d->getRowInd()));
1003 }
1004 }
1005
1006 // set indexes to block
1007 int mat_block_size = 0; // size of block matrix
1008 for (auto &p_uid : a00_uids) {
1009 auto [lo_uid, hi_uid] = p_uid;
1010 auto lo = block_indexing.lower_bound(lo_uid);
1011 auto up = block_indexing.upper_bound(hi_uid);
1012 for (; lo != up; ++lo) {
1013 lo->second.first = mat_block_size;
1014 mat_block_size += lo->second.second->size();
1015 }
1016 }
1017
1018 return std::make_tuple(block_list, block_indexing, mat_block_size);
1019 };
1020
1021 auto get_schur_block_list = [&](auto &block_indexing) {
1022 std::vector<const SchurElemMats *> block_list;
1023 block_list.reserve(storage.size());
1024 for (auto &s : storage) {
1025 if (block_indexing.find(s->uidRow) == block_indexing.end() &&
1026 block_indexing.find(s->uidCol) == block_indexing.end()) {
1027 block_list.push_back(s);
1028 }
1029 }
1030 return block_list;
1031 };
1032
1033 auto bi = get_block_indexing(a00_uids);
1034 auto &[block_list, block_indexing, block_mat_size] = bi;
1035
1036 if (block_mat_size == 0) {
1037
1038 if (schurAO) {
1039 for (auto &s : storage) {
1040 CHKERR AOApplicationToPetsc(schurAO, s->getRowInd().size(),
1041 &*s->getRowInd().begin());
1042 CHKERR AOApplicationToPetsc(schurAO, s->getColInd().size(),
1043 &*s->getColInd().begin());
1044 }
1045 }
1046
1047 for (auto &s : storage) {
1048 auto &m = s->getMat();
1049 CHKERR assemble_schur(m, s->uidRow, s->uidCol, &(s->getRowInd()),
1050 &(s->getColInd()));
1051 }
1053 }
1054
1055 blockMat.resize(block_mat_size, block_mat_size, false);
1056 blockMat.clear();
1057
1058 auto get_range = [](auto &bi) {
1059 return range(bi.first, bi.first + bi.second->size());
1060 };
1061
1062 for (auto &s : block_list) {
1063 auto &m = s->getMat();
1064#ifndef NDEBUG
1065 if (block_indexing.find(s->uidRow) == block_indexing.end())
1066 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "Wrong rlo_uid");
1067 if (block_indexing.find(s->uidCol) == block_indexing.end())
1068 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY, "Wrong clo_uid");
1069#endif // NDEBUG
1070
1071 auto &rbi = block_indexing.at(s->uidRow);
1072 auto &cbi = block_indexing.at(s->uidCol);
1073
1074 auto sub_mat = matrix_range(blockMat, get_range(rbi), get_range(cbi));
1075 sub_mat = m;
1076 }
1077
1078 auto get_zeroed_indices = [&](auto extractor_uid, auto extractor_ind) {
1079 auto &[block_list, block_indexing, block_mat_size] = bi;
1080 std::vector<int> zeroed_indices;
1081 zeroed_indices.reserve(block_mat_size);
1082 for (auto &s : block_list) {
1083 auto &bi = block_indexing.at(extractor_uid(s));
1084 auto &ind = extractor_ind(s);
1085 for (auto it = ind.begin(); it != ind.end(); ++it) {
1086 if (*it < 0) {
1087 auto idx = bi.first + std::distance(ind.begin(), it);
1088 zeroed_indices.push_back(idx);
1089 }
1090 }
1091 }
1092 std::sort(zeroed_indices.begin(), zeroed_indices.end());
1093 auto it = std::unique(zeroed_indices.begin(), zeroed_indices.end());
1094 zeroed_indices.resize(std::distance(zeroed_indices.begin(), it));
1095 return zeroed_indices;
1096 };
1097 auto zero_rows = get_zeroed_indices(
1098 [](auto &s) { return s->uidRow; },
1099 [](auto &s) -> VectorInt & { return s->getRowInd(); });
1100 auto zero_cols = get_zeroed_indices(
1101 [](auto &s) { return s->uidCol; },
1102 [](auto &s) -> VectorInt & { return s->getColInd(); });
1103
1104 for (auto i : zero_rows) {
1105 for (auto j = 0; j != blockMat.size2(); ++j) {
1106 blockMat(i, j) = 0;
1107 }
1108 }
1109 for (auto j : zero_cols) {
1110 for (auto i = 0; i != blockMat.size1(); ++i) {
1111 blockMat(i, j) = 0;
1112 }
1113 }
1114 for (auto i : zero_rows) {
1115 blockMat(i, i) = 1;
1116 }
1117
1118 CHKERR I::invertMat(blockMat, invMat);
1119
1120 // clear storage and block list from a00 blocks, no more needed
1121 for (auto &s : block_list) {
1122 auto it = storage.template get<SchurElemMats::uid_mi_tag>().find(
1123 boost::make_tuple(s->uidRow, s->uidCol));
1124 storage.template get<SchurElemMats::uid_mi_tag>().erase(it);
1125 }
1126 block_list.clear();
1127
1128 if (schurAO) {
1129 for (auto &s : storage) {
1130 if (block_indexing.find(s->uidRow) == block_indexing.end()) {
1131 CHKERR AOApplicationToPetsc(schurAO, s->getRowInd().size(),
1132 &*s->getRowInd().begin());
1133 }
1134 if (block_indexing.find(s->uidCol) == block_indexing.end()) {
1135 CHKERR AOApplicationToPetsc(schurAO, s->getColInd().size(),
1136 &*s->getColInd().begin());
1137 }
1138 }
1139 }
1140
1141 auto schur_block_list = get_schur_block_list(block_indexing);
1142 for (auto &s : schur_block_list) {
1143 auto &m = s->getMat();
1144 CHKERR assemble_schur(m, s->uidRow, s->uidCol, &(s->getRowInd()),
1145 &(s->getColInd()));
1146 }
1147 for (auto &s : schur_block_list) {
1148 auto it = storage.template get<SchurElemMats::uid_mi_tag>().find(
1149 boost::make_tuple(s->uidRow, s->uidCol));
1150 storage.template get<SchurElemMats::uid_mi_tag>().erase(it);
1151 }
1152 schur_block_list.clear();
1153
1154 for (
1155
1156 auto rp_uid_it = a00_uids.begin(); rp_uid_it != a00_uids.end();
1157 ++rp_uid_it
1158
1159 ) {
1160 auto [rlo_uid, rhi_uid] = *rp_uid_it;
1161 for (auto rm = block_indexing.lower_bound(rlo_uid);
1162 rm != block_indexing.upper_bound(rhi_uid); ++rm) {
1163 auto &rbi = rm->second;
1164
1165 auto a_lo_tmp =
1166 storage.template get<SchurElemMats::col_mi_tag>().lower_bound(
1167 rm->first);
1168 auto a_hi =
1169 storage.template get<SchurElemMats::col_mi_tag>().upper_bound(
1170 rm->first);
1171
1172 for (
1173
1174 auto cp_uid_it = (symSchur) ? rp_uid_it : a00_uids.begin();
1175 cp_uid_it != a00_uids.end(); ++cp_uid_it
1176
1177 ) {
1178 auto [clo_uid, chi_uid] = *cp_uid_it;
1179 for (auto cm = block_indexing.lower_bound(clo_uid);
1180 cm != block_indexing.upper_bound(chi_uid); ++cm) {
1181 auto &cbi = cm->second;
1182
1183 auto c_lo_tmp =
1184 storage.template get<SchurElemMats::uid_mi_tag>().lower_bound(
1185 boost::make_tuple(cm->first, 0));
1186 auto c_hi =
1187 storage.template get<SchurElemMats::uid_mi_tag>().upper_bound(
1188 boost::make_tuple(cm->first, FieldEntity::getHiBitNumberUId(
1189 BITFIELDID_SIZE - 1)));
1190
1191 auto sub_inv_mat =
1192 matrix_range(invMat, get_range(rbi), get_range(cbi));
1193 bM.resize(sub_inv_mat.size1(), sub_inv_mat.size2());
1194 noalias(bM) = sub_inv_mat;
1195
1196 for (auto a_lo = a_lo_tmp; a_lo != a_hi; ++a_lo) {
1197#ifndef NDEBUG
1198 if (block_indexing.find((*a_lo)->uidRow) != block_indexing.end())
1199 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1200 "Wrong a_lo->uidRow");
1201#endif
1202
1203 auto &a = (*a_lo)->getMat();
1204 abM.resize(a.size1(), bM.size2(), false);
1205 abM.clear();
1206 cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
1207
1208 a.size1(), bM.size2(), a.size2(), 1.,
1209
1210 &*a.data().begin(), a.size2(),
1211
1212 &*bM.data().begin(), bM.size2(), 0.,
1213
1214 &*abM.data().begin(), abM.size2());
1215
1216 for (auto c_lo = c_lo_tmp; c_lo != c_hi; ++c_lo) {
1217#ifndef NDEBUG
1218 if (block_indexing.find((*c_lo)->uidCol) !=
1219 block_indexing.end())
1220 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
1221 "Wrong a_lo->uidRow");
1222#endif
1223
1224 auto &c = (*c_lo)->getMat();
1225
1226 abcM.resize(abM.size1(), c.size2(), false);
1227 abcM.clear();
1228
1229 cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
1230 abM.size1(), c.size2(), abM.size2(), -1.,
1231
1232 &*abM.data().begin(), abM.size2(),
1233
1234 &*c.data().begin(), c.size2(), 0.,
1235
1236 &*abcM.data().begin(), abcM.size2());
1237
1238 CHKERR assemble_schur(abcM, (*a_lo)->uidRow, (*c_lo)->uidCol,
1239 &((*a_lo)->getRowInd()),
1240 &((*c_lo)->getColInd()));
1241
1242 if (symSchur && rp_uid_it != cp_uid_it) {
1243 abcM = trans(abcM);
1244 CHKERR assemble_schur(abcM, (*c_lo)->uidCol, (*a_lo)->uidRow,
1245 &((*c_lo)->getColInd()),
1246 &((*a_lo)->getRowInd()));
1247 }
1248 }
1249 }
1250 }
1251 }
1252 }
1253 }
1254
1256 };
1257
1258 // Assemble Schur complements
1259 CHKERR assemble_dense_blocks();
1260
1261#ifndef NDEBUG
1262 if constexpr (debug_schur)
1263 MOFEM_LOG("SELF", Sev::noisy) << "Schur assemble done";
1264#endif
1265
1266#ifndef NDEBUG
1267 PetscLogEventEnd(SchurEvents::MOFEM_EVENT_opSchurAssembleEnd, 0, 0, 0, 0);
1268#endif
1269
1271}
It it
Definition Schur.cpp:2568
std::string name
constexpr double a
#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()
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define BITFIELDID_SIZE
max number of fields
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#define MOFEM_LOG(channel, severity)
Log.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
FTensor::Index< 'i', SPACE_DIM > i
const double c
speed of light (cm/ns)
FTensor::Index< 'j', 3 > j
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
UBlasVector< int > VectorInt
Definition Types.hpp:67
constexpr bool debug_schur
Definition Schur.cpp:12
auto field_bit_from_bit_number(const int bit_number)
get field bit id from bit number
FTensor::Index< 'm', 3 > m
static UId getHiBitNumberUId(const FieldBitNumber bit_number)
static auto getFieldBitNumberFromUniqueId(const UId uid)
Get the Field Bit Number From Unique Id.
static UId getLoLocalEntityBitNumber(const char bit_number, const EntityHandle ent)
static UId getHiLocalEntityBitNumber(const char bit_number, const EntityHandle ent)
static SchurElemStorage schurL2Storage
Definition Schur.cpp:218
static PetscLogEvent MOFEM_EVENT_opSchurAssembleEnd
Definition Schur.hpp:28

Member Data Documentation

◆ abcM

Definition at line 128 of file Schur.cpp.

◆ abM

Definition at line 128 of file Schur.cpp.

◆ blockMat

Definition at line 126 of file Schur.cpp.

◆ bM

Definition at line 128 of file Schur.cpp.

◆ fieldEnts

template<typename OP_SCHUR_ASSEMBLE_BASE >
std::vector<boost::shared_ptr<Range> > MoFEM::OpSchurAssembleEndImpl< OP_SCHUR_ASSEMBLE_BASE >::fieldEnts
protected

Definition at line 121 of file Schur.cpp.

◆ fieldsName

template<typename OP_SCHUR_ASSEMBLE_BASE >
std::vector<std::string> MoFEM::OpSchurAssembleEndImpl< OP_SCHUR_ASSEMBLE_BASE >::fieldsName
protected

Definition at line 120 of file Schur.cpp.

◆ invMat

Definition at line 127 of file Schur.cpp.

◆ schurAO

Definition at line 122 of file Schur.cpp.

◆ schurMat

Definition at line 123 of file Schur.cpp.

◆ symSchur

Definition at line 124 of file Schur.cpp.


The documentation for this struct was generated from the following file: