867 {
868
870
871#ifndef NDEBUG
873#endif
874
875#ifndef NDEBUG
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;
887 for (
auto ss = 0; ss !=
fieldsName.size(); ++ss) {
888 auto field_bit = get_field_bit(
fieldsName[ss]);
890 if (row_ents) {
891 for (auto p = row_ents->pair_begin(); p != row_ents->pair_end(); ++p) {
892 auto lo_uid =
894 auto hi_uid =
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) {
913 };
914 auto list_storage = [&](auto &storage) {
917 for (auto &p : storage) {
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();
923 }
925 };
926#endif
927
928 auto assemble_dense_blocks = [&]() {
929 using matrix_range = ublas::matrix_range<MatrixDouble>;
930 using range = ublas::range;
933
934 auto assemble_schur = [
this](
auto &
m,
auto &uid_row,
auto &uid_col,
935 auto *row_ind_ptr, auto *col_ind_ptr) {
937
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);
951 if (row_ent_it != field_ents->end())
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())
958 << "Assemble col entity: " << (*col_ent_it)->getName() << " "
959 << (*col_ent_it)->getEntTypeName() << " side "
960 << (*col_ent_it)->getSideNumber();
961#endif
963 }
964 }
965
967 };
968
969 auto a00_uids = get_a00_uids();
970
971 auto get_block_indexing = [&](auto &a00_uids) {
972
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
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
997 std::map<UId, std::pair<size_t, const VectorInt *>>
998 block_indexing;
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
1007 int mat_block_size = 0;
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
1039 for (
auto &
s : storage) {
1041 &*
s->getRowInd().begin());
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()),
1051 }
1053 }
1054
1055 blockMat.resize(block_mat_size, block_mat_size,
false);
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())
1067 if (block_indexing.find(
s->uidCol) == block_indexing.end())
1069#endif
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));
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);
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) {
1107 }
1108 }
1109 for (
auto j : zero_cols) {
1112 }
1113 }
1114 for (
auto i : zero_rows) {
1116 }
1117
1119
1120
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
1129 for (
auto &
s : storage) {
1130 if (block_indexing.find(
s->uidRow) == block_indexing.end()) {
1132 &*
s->getRowInd().begin());
1133 }
1134 if (block_indexing.find(
s->uidCol) == block_indexing.end()) {
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()),
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(
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())
1200 "Wrong a_lo->uidRow");
1201#endif
1202
1203 auto &
a = (*a_lo)->getMat();
1204 abM.resize(
a.size1(),
bM.size2(),
false);
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())
1221 "Wrong a_lo->uidRow");
1222#endif
1223
1224 auto &
c = (*c_lo)->getMat();
1225
1226 abcM.resize(
abM.size1(),
c.size2(),
false);
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) {
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
1259 CHKERR assemble_dense_blocks();
1260
1261#ifndef NDEBUG
1263 MOFEM_LOG(
"SELF", Sev::noisy) <<
"Schur assemble done";
1264#endif
1265
1266#ifndef NDEBUG
1268#endif
1269
1271}
#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
#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
constexpr bool debug_schur
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
static PetscLogEvent MOFEM_EVENT_opSchurAssembleEnd