22 using namespace MoFEM;
26 #undef PETSC_VERSION_RELEASE
27 #define PETSC_VERSION_RELEASE 1
29 #if PETSC_VERSION_GE(3, 6, 0)
30 #include <petsc/private/petscimpl.h>
32 #include <petsc-private/petscimpl.h>
35 #if PETSC_VERSION_GE(3, 6, 0)
36 #include <petsc/private/dmimpl.h>
39 #include <petsc-private/dmimpl.h>
40 #include <petsc-private/vecimpl.h>
46 ierr = PetscObjectReference((PetscObject)
A);
47 CHKERRABORT(PETSC_COMM_WORLD,
ierr);
48 ierr = PetscObjectReference((PetscObject)
iS);
49 CHKERRABORT(PETSC_COMM_WORLD,
ierr);
53 ierr = MatDestroy(&
A);
54 CHKERRABORT(PETSC_COMM_WORLD,
ierr);
56 CHKERRABORT(PETSC_COMM_WORLD,
ierr);
68 CHKERRABORT(PETSC_COMM_WORLD,
ierr);
69 ierr = VecDestroy(&
X);
70 CHKERRABORT(PETSC_COMM_WORLD,
ierr);
71 ierr = VecDestroy(&
F);
72 CHKERRABORT(PETSC_COMM_WORLD,
ierr);
75 template <InsertMode MODE>
78 MatSORType flag, PetscReal shift,
79 PetscInt its, PetscInt lits,
Vec x);
98 template <InsertMode MODE>
102 CHKERR MatShellGetContext(
a, &void_ctx);
108 CHKERR VecScatterBegin(ctx->
sCat, x, ctx->
X, INSERT_VALUES, SCATTER_REVERSE);
109 CHKERR VecScatterEnd(ctx->
sCat, x, ctx->
X, INSERT_VALUES, SCATTER_REVERSE);
111 CHKERR VecScatterBegin(ctx->
sCat, ctx->
F,
f, MODE, SCATTER_FORWARD);
112 CHKERR VecScatterEnd(ctx->
sCat, ctx->
F,
f, MODE, SCATTER_FORWARD);
118 return sub_mat_mult_generic<INSERT_VALUES>(
a, x,
f);
122 return sub_mat_mult_generic<ADD_VALUES>(
a, x,
f);
126 PetscReal shift, PetscInt its, PetscInt lits,
130 CHKERR MatShellGetContext(mat, &void_ctx);
136 CHKERR VecScatterBegin(ctx->
sCat,
b, ctx->
X, INSERT_VALUES, SCATTER_REVERSE);
137 CHKERR VecScatterEnd(ctx->
sCat,
b, ctx->
X, INSERT_VALUES, SCATTER_REVERSE);
138 CHKERR MatSOR(ctx->
A, ctx->
X,
omega, flag, shift, its, lits, ctx->
F);
139 CHKERR VecScatterBegin(ctx->
sCat, ctx->
F, x, INSERT_VALUES, SCATTER_FORWARD);
140 CHKERR VecScatterEnd(ctx->
sCat, ctx->
F, x, INSERT_VALUES, SCATTER_FORWARD);
151 CHKERRABORT(PETSC_COMM_WORLD,
ierr);
156 for (
unsigned int ii = 0; ii <
coarseningIS.size(); ii++) {
160 for (
unsigned int ii = 0; ii <
kspOperators.size(); ii++) {
181 #define GET_DM_FIELD(DM) \
183 static_cast<DMCtx *>(DM->data)->getInterface<DMMGViaApproxOrdersCtx>(); \
187 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
195 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
200 CHKERR AODestroy(&dm_field->aO);
204 CHKERR PetscObjectReference((PetscObject)ao);
210 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
213 *size = dm_field->coarseningIS.size();
219 bool create_sub_matrix,
221 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
224 dm_field->coarseningIS.push_back(is);
227 CHKERR PetscObjectReference((PetscObject)is);
232 CHKERR ISDuplicate(is, &is2);
234 CHKERR AOApplicationToPetscIS(dm_field->aO, is2);
236 if (create_sub_matrix) {
242 CHKERR PetscObjectGetComm((PetscObject)
A, &comm);
244 &(dm_field->shellMatrixCtxPtr.back()), subA);
245 CHKERR MatShellSetOperation(*subA, MATOP_MULT,
247 CHKERR MatShellSetOperation(*subA, MATOP_MULT_ADD,
249 CHKERR MatShellSetOperation(*subA, MATOP_SOR,
252 #if PETSC_VERSION_GE(3, 8, 0)
253 CHKERR MatCreateSubMatrix(
A, is2, is2, MAT_INITIAL_MATRIX, subA);
255 CHKERR MatGetSubMatrix(
A, is2, is2, MAT_INITIAL_MATRIX, subA);
262 dm_field->kspOperators.push_back(*subA);
263 CHKERR PetscObjectReference((PetscObject)(*subA));
267 PetscInfo(dm,
"Push back IS to DMMGViaApproxOrders\n");
272 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
275 if (dm_field->coarseningIS.back()) {
276 CHKERR ISDestroy(&dm_field->coarseningIS.back());
277 dm_field->coarseningIS.pop_back();
279 if (dm_field->kspOperators.back()) {
280 CHKERR MatDestroy(&dm_field->kspOperators.back());
282 dm_field->kspOperators.pop_back();
283 PetscInfo(dm,
"Pop back IS to DMMGViaApproxOrders\n");
288 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291 CHKERR dm_field->destroyCoarseningIS();
292 PetscInfo(dm,
"Clear DMs data structures\n");
299 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
302 int nb_no_changed = 0;
306 std::vector<IS>::iterator it;
307 it = dm_field->coarseningIS.begin();
309 for (; it != dm_field->coarseningIS.end(); it++, ii++) {
312 CHKERR ISEqual(*it, is_vec[ii], &flg);
315 CHKERR MatDestroy(&dm_field->kspOperators[ii]);
317 CHKERR PetscObjectReference((PetscObject)is_vec[ii]);
318 if (ii < nb_elems - 1) {
321 CHKERR ISDuplicate(is_vec[ii], &is);
322 CHKERR ISCopy(is_vec[ii], is);
323 CHKERR AOApplicationToPetscIS(dm_field->aO, is);
326 #if PETSC_VERSION_GE(3, 8, 0)
327 CHKERR MatCreateSubMatrix(
A, is, is, MAT_INITIAL_MATRIX, &subA);
329 CHKERR MatGetSubMatrix(
A, is, is, MAT_INITIAL_MATRIX, &subA);
331 CHKERR PetscObjectReference((PetscObject)subA);
332 dm_field->kspOperators[ii] = subA;
338 CHKERR PetscObjectReference((PetscObject)
A);
339 dm_field->kspOperators[ii] =
A;
348 if (
static_cast<int>(dm_field->coarseningIS.size()) < nb_elems) {
349 for (; ii < nb_elems - 1; ii++) {
360 for (; ii < static_cast<int>(dm_field->coarseningIS.size()); ii++) {
366 CHKERR PetscObjectGetComm((PetscObject)dm, &comm);
369 "DMMGViaApproxOrders nb_no_changed = %d, nb_replaced = %d, "
370 "nb_added = %d, nb_deleted = %d, size = %d\n",
371 nb_no_changed, nb_replaced, nb_added, nb_deleted,
372 dm_field->coarseningIS.size());
374 PetscInfo(dm,
"Replace IS to DMMGViaApproxOrders\n");
380 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
421 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
426 ((
DMCtx *)(dm->data))->referenceNumber++;
436 PetscInfo1(dm,
"Create DMMGViaApproxOrders reference = %d\n",
437 ((
DMCtx *)(dm->data))->referenceNumber);
443 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
447 int leveldown = dm->leveldown;
449 if (dm_field->kspOperators.empty()) {
453 CHKERR PetscObjectGetComm((PetscObject)dm, &comm);
454 if (dm_field->kspOperators.empty()) {
456 "data inconsistency, operator can not be set");
458 if (
static_cast<int>(dm_field->kspOperators.size()) < leveldown) {
460 "data inconsistency, no IS for that level");
462 *
M = dm_field->kspOperators[dm_field->kspOperators.size() - 1 - leveldown];
463 CHKERR PetscObjectReference((PetscObject)*
M);
467 PetscInfo1(dm,
"Create Matrix DMMGViaApproxOrders leveldown = %d\n",
474 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
477 CHKERR PetscObjectGetComm((PetscObject)dm, &comm);
478 CHKERR DMCreate(comm, dmc);
479 (*dmc)->data = dm->data;
483 CHKERR PetscObjectReference((PetscObject)(*dmc));
484 PetscInfo1(dm,
"Coarsen DMMGViaApproxOrders leveldown = %d\n", dm->leveldown);
490 PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
491 PetscValidHeaderSpecific(dm2, DM_CLASSID, 1);
495 CHKERR PetscObjectGetComm((PetscObject)dm1, &comm);
502 int dm_down_leveldown = dm_down->leveldown;
503 int dm_up_leveldown = dm_up->leveldown;
506 "Create interpolation DMMGViaApproxOrders dm1_leveldown = %d "
507 "dm2_leveldown = %d\n",
508 dm_down_leveldown, dm_up_leveldown);
514 if (
static_cast<int>(dm_field->coarseningIS.size()) < dm_down_leveldown) {
517 is_down = dm_field->coarseningIS[dm_field->coarseningIS.size() - 1 -
520 CHKERR ISGetLocalSize(is_down, &
m);
525 if (
static_cast<int>(dm_field->coarseningIS.size()) < dm_up_leveldown) {
530 ->coarseningIS[dm_field->coarseningIS.size() - 1 - dm_up_leveldown];
532 CHKERR ISGetLocalSize(is_up, &
n);
538 CHKERR MatCreate(comm, mat);
540 CHKERR MatSetType(*mat, MATMPIAIJ);
541 CHKERR MatMPIAIJSetPreallocation(*mat, 1, PETSC_NULL, 0, PETSC_NULL);
544 PetscLayout rmap, cmap;
545 CHKERR MatGetLayouts(*mat, &rmap, &cmap);
546 int rstart, rend, cstart, cend;
547 CHKERR PetscLayoutGetRange(rmap, &rstart, &rend);
548 CHKERR PetscLayoutGetRange(cmap, &cstart, &cend);
556 const int *row_indices_ptr, *col_indices_ptr;
557 CHKERR ISGetIndices(is_down, &row_indices_ptr);
558 CHKERR ISGetIndices(is_up, &col_indices_ptr);
560 map<int, int> idx_map;
561 for (
int ii = 0; ii <
m; ii++) {
562 idx_map[row_indices_ptr[ii]] = rstart + ii;
565 CHKERR MatZeroEntries(*mat);
567 for (
int jj = 0; jj <
n; jj++) {
568 map<int, int>::iterator mit = idx_map.find(col_indices_ptr[jj]);
569 if (mit != idx_map.end()) {
570 CHKERR MatSetValue(*mat, mit->second, cstart + jj, 1, INSERT_VALUES);
574 CHKERR ISRestoreIndices(is_down, &row_indices_ptr);
575 CHKERR ISRestoreIndices(is_up, &col_indices_ptr);
577 CHKERR MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY);
578 CHKERR MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY);
589 PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
591 int leveldown = dm->leveldown;
593 if (dm_field->kspOperators.empty()) {
596 #if PETSC_VERSION_GE(3, 5, 3)
598 dm_field->kspOperators[dm_field->kspOperators.size() - 1 - leveldown],
602 dm_field->kspOperators[dm_field->kspOperators.size() - 1 - leveldown],
607 PetscInfo1(dm,
"Create global vector DMMGViaApproxOrders leveldown = %d\n",
614 ierr = PetscOptionsBegin(PETSC_COMM_WORLD,
"",
615 "MOFEM Multi-Grid (Orders) pre-conditioner",
"none");
618 CHKERR PetscOptionsInt(
"-mofem_mg_levels",
"nb levels of multi-grid solver",
620 CHKERR PetscOptionsInt(
"-mofem_mg_coarse_order",
621 "approximation order of coarse level",
"", 2,
623 CHKERR PetscOptionsInt(
"-mofem_mg_order_at_last_level",
"order at last level",
625 CHKERR PetscOptionsInt(
"-mofem_mg_verbose",
"nb levels of multi-grid solver",
627 PetscBool shell_sub_a =
shellSubA ? PETSC_TRUE : PETSC_FALSE;
628 CHKERR PetscOptionsBool(
"-mofem_mg_shell_a",
"use shell matrix as sub matrix",
629 "", shell_sub_a, &shell_sub_a, NULL);
632 ierr = PetscOptionsEnd();
652 ->getPetscGlobalDofIdx();
658 string problem_name = problem_ptr->
getName();
660 order_at_next_level, is);
677 CHKERR PetscObjectGetComm((PetscObject)
dM, &comm);
680 PetscPrintf(comm,
"set MG levels %u\n",
nbLevels);
683 std::vector<IS> is_vec(
nbLevels + 1);
686 for (
int kk = 0; kk <
nbLevels; kk++) {
690 CHKERR ISGetSize(is_vec[kk], &is_glob_size[kk]);
691 CHKERR ISGetLocalSize(is_vec[kk], &is_loc_size[kk]);
694 PetscSynchronizedPrintf(comm,
695 "Nb. dofs at level [ %d ] global %u local %d\n",
696 kk, is_glob_size[kk], is_loc_size[kk]);
700 if (is_glob_size[kk] == 0) {
705 for (
int kk = 0; kk !=
nbLevels; kk++) {
707 if (kk ==
nbLevels - 1 && use_mat_a) {
728 for (
unsigned int kk = 0; kk < is_vec.size(); kk++) {
733 PetscSynchronizedFlush(comm, PETSC_STDOUT);
742 PetscValidHeaderSpecific(pc, PC_CLASSID, 1);
746 CHKERR PetscObjectGetComm((PetscObject)pc, &comm);
748 PetscPrintf(comm,
"Start PCMGSetUpViaApproxOrders\n");
754 #if PETSC_VERSION_GE(3, 8, 0)
755 CHKERR PCMGSetGalerkin(pc, PC_MG_GALERKIN_NONE);
757 CHKERR PCMGSetGalerkin(pc, PETSC_FALSE);
763 PetscPrintf(comm,
"End PCMGSetUpViaApproxOrders\n");
MoFEMErrorCode DMCreateGlobalVector_MGViaApproxOrders(DM dm, Vec *g)
Create global vector for DMGViaApproxOrders.
MoFEMErrorCode DMMGViaApproxOrdersSetAO(DM dm, AO ao)
Set DM ordering.
MoFEMErrorCode PCMGSetUpViaApproxOrders(PC pc, PCMGSetUpViaApproxOrdersCtx *ctx, int verb)
Function build MG structure.
MoFEMErrorCode sub_mat_mult_generic(Mat a, Vec x, Vec f)
MoFEMErrorCode sub_mat_mult(Mat a, Vec x, Vec f)
MoFEMErrorCode DMCreate_MGViaApproxOrders(DM dm)
Create DM data structure for Multi-Grid via approximation orders.
MoFEMErrorCode sub_mat_sor(Mat mat, Vec b, PetscReal omega, MatSORType flag, PetscReal shift, PetscInt its, PetscInt lits, Vec x)
static MoFEMErrorCode ksp_set_operators(KSP ksp, Mat A, Mat B, void *ctx)
MoFEMErrorCode DMMGViaApproxOrdersGetCtx(DM dm, DMMGViaApproxOrdersCtx **ctx)
MoFEMErrorCode sub_mat_mult_add(Mat a, Vec x, Vec f)
MoFEMErrorCode DMMGViaApproxOrdersGetCoarseningISSize(DM dm, int *size)
Gets size of coarseningIS in internal data struture DMMGViaApproxOrdersCtx.
MoFEMErrorCode DMMGViaApproxOrdersReplaceCoarseningIS(DM dm, IS *is_vec, int nb_elems, Mat A, int verb)
Replace coarsening IS in DM via approximation orders.
MoFEMErrorCode DMCreateInterpolation_MGViaApproxOrders(DM dm1, DM dm2, Mat *mat, Vec *vec)
Create interpolation matrix between data managers dm1 and dm2.
header of multi-grid solver for p- adaptivity
#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 CHKERRG(n)
Check error code of MoFEM/MOAB/PETSc function.
@ 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 MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
PetscErrorCode DMMoFEMGetProblemPtr(DM dm, const MoFEM::Problem **problem_ptr)
Get pointer to problem data structure.
MoFEMErrorCode DMMGViaApproxOrdersPushBackCoarseningIS(DM dm, IS is, Mat A, Mat *subA, bool create_sub_matrix, bool shell_sub_a)
Push back coarsening level to MG via approximation orders.
PetscErrorCode DMCreateMatrix_MoFEM(DM dm, Mat *M)
MoFEMErrorCode DMCreateMatrix_MGViaApproxOrders(DM dm, Mat *M)
Create matrix for Multi-Grid via approximation orders.
MoFEMErrorCode DMCoarsen_MGViaApproxOrders(DM dm, MPI_Comm comm, DM *dmc)
Coarsen DM.
MoFEMErrorCode DMMGViaApproxOrdersPopBackCoarseningIS(DM dm)
Pop is form MG via approximation orders.
PetscErrorCode DMoFEMGetInterfacePtr(DM dm, MoFEM::Interface **m_field_ptr)
Get pointer to MoFEM::Interface.
PetscErrorCode DMCreateGlobalVector_MoFEM(DM dm, Vec *g)
DMShellSetCreateGlobalVector.
MoFEMErrorCode DMRegister_MGViaApproxOrders(const char sname[])
Register DM for Multi-Grid via approximation orders.
MoFEMErrorCode DMMGViaApproxOrdersClearCoarseningIS(DM dm)
Clear approximation orders.
PetscErrorCode DMSetOperators_MoFEM(DM dm)
Set operators for MoFEM dm.
MoFEMErrorCode isCreateProblemOrder(const std::string problem_name, RowColData rc, int min_order, int max_order, IS *is) const
create IS for given order range (collective)
const FTensor::Tensor2< T, Dim, Dim > Vec
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
FTensor::Index< 'm', 3 > m
Structure for DM for multi-grid via approximation orders.
MoFEMErrorCode destroyCoarseningIS()
std::vector< Mat > kspOperators
Get KSP operators.
boost::ptr_vector< PCMGSubMatrixCtx > shellMatrixCtxPtr
Shell sub-matrix context.
virtual ~DMMGViaApproxOrdersCtx()
std::vector< IS > coarseningIS
Coarsening IS.
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, MoFEM::UnknownInterface **iface) const
PETSc Discrete Manager data structure.
Deprecated interface functions.
Section manager is used to create indexes and sections.
keeps basic data about problem
DofIdx getNbLocalDofsRow() const
auto & getNumeredRowDofsPtr() const
get access to numeredRowDofsPtr storing DOFs on rows
base class for all interface classes
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface refernce to pointer of interface.
Set data structures of MG pre-conditioner via approximation orders.
virtual MoFEMErrorCode createIsAtLevel(int kk, IS *is)
Set IS for levels.
virtual MoFEMErrorCode getOptions()
get options from line command
virtual MoFEMErrorCode destroyIsAtLevel(int kk, IS *is)
Destroy IS if internally created.
DM dM
Distributed mesh manager.
int coarseOrder
approximation order of coarse level
int orderAtLastLevel
set maximal evaluated order
int nbLevels
number of multi-grid levels
Mat A
Matrix at fine level.
virtual MoFEMErrorCode buildProlongationOperator(bool use_mat_a, int verb=0)
Set up data structures for MG.
MoFEMErrorCode initData(Vec x)
friend MoFEMErrorCode sub_mat_mult_generic(Mat a, Vec x, Vec f)
friend MoFEMErrorCode sub_mat_sor(Mat mat, Vec b, PetscReal omega, MatSORType flag, PetscReal shift, PetscInt its, PetscInt lits, Vec x)
PCMGSubMatrixCtx_private(Mat a, IS is)
PetscLogEvent MOFEM_EVENT_sor
~PCMGSubMatrixCtx_private()
PetscLogEvent MOFEM_EVENT_mult
virtual ~PCMGSubMatrixCtx()
PCMGSubMatrixCtx(Mat a, IS is)