15 : cOre(const_cast<
MoFEM::
Core &>(core)), dEbug(false) {
17 auto core_log = logging::core::get();
35 MOFEM_LOG(
"VECWORLD", Sev::noisy) <<
"Vector manager created";
44 DofIdx nb_local_dofs, nb_ghost_dofs;
57 CHKERR ::VecCreateSeq(PETSC_COMM_SELF, nb_local_dofs + nb_ghost_dofs, V);
58 CHKERR ::VecSetOption(
59 *V, VEC_IGNORE_NEGATIVE_INDICES,
70 DofIdx nb_dofs, nb_local_dofs, nb_ghost_dofs;
91 auto miit = dofs->lower_bound(nb_local_dofs);
92 auto hi_miit = dofs->upper_bound(nb_local_dofs + nb_ghost_dofs);
93 int count = std::distance(miit, hi_miit);
94 if (count != nb_ghost_dofs) {
97 std::vector<DofIdx> ghost_idx;
98 ghost_idx.reserve(count);
99 for (; miit != hi_miit; ++miit) {
100 if ((*miit)->getActive()) {
101 ghost_idx.emplace_back((*miit)->petscGloablDofIdx);
104 if (PetscUnlikely(ghost_idx.back() > nb_dofs || ghost_idx.back() < 0)) {
108 MOFEM_LOG(
"VECSELF", Sev::error) <<
"Problem with DOF " << **miit;
114 CHKERR ::VecCreateGhost(m_field.
get_comm(), nb_local_dofs, nb_dofs,
115 ghost_idx.size(), &*ghost_idx.begin(), V);
116 CHKERR ::VecSetOption(
117 *V, VEC_IGNORE_NEGATIVE_INDICES,
127 v_ptr.reset(vec,
false);
132 Vec xin,
const std::string x_problem,
const std::string x_field_name,
134 const std::string y_field_name,
RowColData y_rc, VecScatter *newctx)
const {
137 std::vector<int> idx(0), idy(0);
139 ->isCreateFromProblemFieldToOtherProblemField(
140 x_problem, x_field_name, x_rc, y_problem, y_field_name, y_rc, idx,
143 CHKERR ISCreateGeneral(PETSC_COMM_WORLD, idx.size(), &idx[0],
144 PETSC_USE_POINTER, &ix);
145 CHKERR ISCreateGeneral(PETSC_COMM_WORLD, idy.size(), &idy[0],
146 PETSC_USE_POINTER, &iy);
147 CHKERR ::VecScatterCreate(xin, ix, yin, iy, newctx);
155 const std::string y_problem,
RowColData y_rc, VecScatter *newctx)
const {
158 std::vector<int> idx(0), idy(0);
160 x_problem, x_rc, y_problem, y_rc, idx, idy);
162 CHKERR ISCreateGeneral(PETSC_COMM_WORLD, idx.size(), &idx[0],
163 PETSC_USE_POINTER, &ix);
164 CHKERR ISCreateGeneral(PETSC_COMM_WORLD, idy.size(), &idy[0],
165 PETSC_USE_POINTER, &iy);
167 ISView(ix, PETSC_VIEWER_STDOUT_WORLD);
168 ISView(iy, PETSC_VIEWER_STDOUT_WORLD);
170 CHKERR ::VecScatterCreate(xin, ix, yin, iy, newctx);
178 const std::string x_field_name,
RowColData x_rc,
179 Vec yin,
const std::string y_problem,
180 const std::string y_field_name,
RowColData y_rc,
185 y_field_name, y_rc, &newctx);
205 ScatterMode scatter_mode)
const {
208 DofIdx nb_local_dofs, nb_ghost_dofs;
226 CHKERR VecGhostGetLocalForm(V, &Vlocal);
228 CHKERR VecGetLocalSize(V, &size);
229 if (size != nb_local_dofs) {
231 "data inconsistency: check ghost vector, problem <%s> with nb. of "
232 "local nodes %d != %d",
233 problem_ptr->
getName().c_str(), size, nb_local_dofs);
235 CHKERR VecGetLocalSize(Vlocal, &size);
236 if (size != nb_local_dofs + nb_ghost_dofs) {
238 "data inconsistency: check ghost vector, problem with nb. of "
239 "ghost nodes %d != ",
240 size, nb_local_dofs + nb_ghost_dofs);
242 auto miit = dofs->lower_bound(0);
243 auto hi_miit = dofs->upper_bound(nb_local_dofs + nb_ghost_dofs);
245 switch (scatter_mode) {
246 case SCATTER_FORWARD: {
248 CHKERR VecGetArray(Vlocal, &array);
251 for (; miit != hi_miit; ++miit, ++ii)
252 array[ii] = (*miit)->getFieldData();
255 for (; miit != hi_miit; ++miit, ++ii)
256 array[ii] += (*miit)->getFieldData();
261 CHKERR VecRestoreArray(Vlocal, &array);
263 case SCATTER_REVERSE: {
264 const PetscScalar *array;
265 CHKERR VecGetArrayRead(Vlocal, &array);
268 for (; miit != hi_miit; ++miit, ++ii) {
269 (*miit)->getFieldData() = array[ii];
273 for (; miit != hi_miit; ++miit, ++ii)
274 (*miit)->getFieldData() += array[ii];
279 CHKERR VecRestoreArrayRead(Vlocal, &array);
284 CHKERR VecGhostRestoreLocalForm(V, &Vlocal);
291 ScatterMode scatter_mode)
const {
302 Vec V, InsertMode mode,
303 ScatterMode scatter_mode)
const {
307 DofsByGlobalIdx *dofs;
312 dofs =
const_cast<DofsByGlobalIdx *
>(
317 dofs =
const_cast<DofsByGlobalIdx *
>(
322 "Function works only for ROWs and COLs");
324 DofsByGlobalIdx::iterator miit = dofs->lower_bound(0);
325 DofsByGlobalIdx::iterator hi_miit = dofs->upper_bound(nb_dofs);
326 auto comm = PetscObjectComm((PetscObject)V);
327 switch (scatter_mode) {
328 case SCATTER_REVERSE: {
331 CHKERR VecScatterCreateToAll(V, &ctx, &V_glob);
332 CHKERR VecScatterBegin(ctx, V, V_glob, INSERT_VALUES, SCATTER_FORWARD);
333 CHKERR VecScatterEnd(ctx, V, V_glob, INSERT_VALUES, SCATTER_FORWARD);
335 CHKERR VecGetSize(V_glob, &size);
336 if (size != nb_dofs) {
338 "Size of vector is inconsistent with size of problem. You could "
339 "use wrong vector with wrong problem, or you created vector "
340 "before you remove DOFs from problem.");
343 CHKERR VecGetArray(V_glob, &array);
346 for (; miit != hi_miit; miit++)
347 (*miit)->getFieldData() = array[(*miit)->getPetscGlobalDofIdx()];
350 for (; miit != hi_miit; miit++)
351 (*miit)->getFieldData() += array[(*miit)->getPetscGlobalDofIdx()];
356 CHKERR VecRestoreArray(V_glob, &array);
357 CHKERR VecScatterDestroy(&ctx);
358 CHKERR VecDestroy(&V_glob);
370 ScatterMode scatter_mode)
const {
380 template <
typename A0,
typename A1,
typename A2,
typename A3,
typename A4>
383 const std::string cpy_field_name) {
385 for (; miit != hi_miit; miit++) {
386 if (miit->get()->getHasLocalIndex()) {
389 cpy_bit_number, (*miit)->getEnt()));
390 auto diiiit = dofs_ptr->template get<Unique_mi_tag>().find(uid);
391 if (diiiit == dofs_ptr->template get<Unique_mi_tag>().end()) {
396 <<
"Problem finding DOFs in the copy field";
397 MOFEM_LOG(
"VECSELF", Sev::error) <<
"Field DOF: " << (**miit);
399 <<
"Copy field name: " << cpy_field_name;
401 "Automatic creation of entity and dof not implemented");
403 if constexpr (MODE == INSERT_VALUES)
404 (*diiiit)->getFieldData() = array[(*miit)->getPetscLocalDofIdx()];
405 else if constexpr (MODE == ADD_VALUES)
406 (*diiiit)->getFieldData() += array[(*miit)->getPetscLocalDofIdx()];
415 const std::string cpy_field_name,
RowColData rc,
Vec V, InsertMode mode,
416 ScatterMode scatter_mode)
const {
426 dofs =
const_cast<DofsByUId *
>(
430 dofs =
const_cast<DofsByUId *
>(
440 "cpy field < %s > not found, (top tip: check spelling)",
441 cpy_field_name.c_str());
442 const auto cpy_bit_number = (*cpy_fit)->getBitNumber();
444 auto miit = dofs->lower_bound(
446 if (miit == dofs->end()) {
448 "cpy field < %s > not found, (top tip: check spelling)",
451 auto hi_miit = dofs->upper_bound(
454 if ((*miit)->getSpace() != (*cpy_fit)->getSpace()) {
456 "fields have to have same space (%s) %s != (%s) %s",
460 if ((*miit)->getNbOfCoeffs() != (*cpy_fit)->getNbOfCoeffs()) {
462 "fields have to have same rank");
465 switch (scatter_mode) {
466 case SCATTER_REVERSE: {
469 CHKERR VecGetArray(V, &array);
470 if (mode == INSERT_VALUES)
472 dofs_ptr, array, miit, hi_miit, cpy_bit_number, cpy_field_name);
473 else if (mode == ADD_VALUES)
475 dofs_ptr, array, miit, hi_miit, cpy_bit_number, cpy_field_name);
478 "Operation mode not implemented");
479 CHKERR VecRestoreArray(V, &array);
481 case SCATTER_FORWARD: {
482 for (; miit != hi_miit; miit++) {
483 if (!miit->get()->getHasLocalIndex())
487 cpy_bit_number, (*miit)->getEnt()));
491 "no data to fill the vector (top tip: you want scatter forward "
492 "or scatter reverse?)");
494 CHKERR VecSetValue(V, (*miit)->getPetscGlobalDofIdx(),
495 (*diiiit)->getFieldData(), mode);
497 CHKERR VecAssemblyBegin(V);
507 const std::string name,
const std::string
field_name,
508 const std::string cpy_field_name,
RowColData rc,
Vec V, InsertMode mode,
509 ScatterMode scatter_mode)
const {
515 V, mode, scatter_mode);
520 template <
typename A0,
typename A1,
typename A2,
typename A3,
typename A4>
522 A4 &cpy_bit_number) {
524 for (; miit != hi_miit; miit++) {
527 cpy_bit_number, (*miit)->getEnt()));
528 auto diiiit = dofs_ptr->template get<Unique_mi_tag>().find(uid);
530 if (diiiit == dofs_ptr->template get<Unique_mi_tag>().end()) {
532 "Automatic creation of entity and dof not implemented");
535 if constexpr (MODE == INSERT_VALUES)
536 (*diiiit)->getFieldData() = array[(*miit)->getPetscGlobalDofIdx()];
537 else if constexpr (MODE == ADD_VALUES)
538 (*diiiit)->getFieldData() += array[(*miit)->getPetscGlobalDofIdx()];
546 const std::string cpy_field_name,
RowColData rc,
Vec V, InsertMode mode,
547 ScatterMode scatter_mode)
const {
571 "cpy field < %s > not found, (top tip: check spelling)",
572 cpy_field_name.c_str());
574 const auto cpy_bit_number = (*cpy_fit)->getBitNumber();
576 auto miit = dofs->lower_bound(
578 if (miit == dofs->end()) {
580 "problem field < %s > not found, (top tip: check spelling)",
583 auto hi_miit = dofs->upper_bound(
585 if ((*miit)->getSpace() != (*cpy_fit)->getSpace()) {
587 "fields have to have same space");
589 if ((*miit)->getNbOfCoeffs() != (*cpy_fit)->getNbOfCoeffs()) {
591 "fields have to have same rank");
593 switch (scatter_mode) {
594 case SCATTER_REVERSE: {
597 CHKERR VecScatterCreateToAll(V, &ctx, &V_glob);
598 CHKERR VecScatterBegin(ctx, V, V_glob, INSERT_VALUES, SCATTER_FORWARD);
599 CHKERR VecScatterEnd(ctx, V, V_glob, INSERT_VALUES, SCATTER_FORWARD);
601 CHKERR VecGetSize(V_glob, &size);
605 "data inconsistency: nb. of dofs and declared nb. dofs in database");
607 CHKERR VecGetArray(V_glob, &array);
609 if (mode == INSERT_VALUES)
611 dofs_ptr, array, miit, hi_miit, cpy_bit_number);
612 else if (mode == ADD_VALUES)
614 hi_miit, cpy_bit_number);
617 "Operation mode not implemented");
619 CHKERR VecRestoreArray(V_glob, &array);
620 CHKERR VecDestroy(&V_glob);
621 CHKERR VecScatterDestroy(&ctx);
623 case SCATTER_FORWARD: {
624 for (; miit != hi_miit; miit++) {
627 cpy_bit_number, (*miit)->getEnt()));
631 "No data to fill the vector (top tip: you want scatter forward "
632 "or scatter reverse?)");
634 CHKERR VecSetValue(V, (*miit)->getPetscGlobalDofIdx(),
635 (*diiiit)->getFieldData(), mode);
637 CHKERR VecAssemblyBegin(V);
647 const std::string name,
const std::string
field_name,
648 const std::string cpy_field_name,
RowColData rc,
Vec V, InsertMode mode,
649 ScatterMode scatter_mode)
const {
655 V, mode, scatter_mode);