v0.9.0
KspCtx.cpp
Go to the documentation of this file.
1 /* This file is part of MoFEM.
2  * MoFEM is free software: you can redistribute it and/or modify it under
3  * the terms of the GNU Lesser General Public License as published by the
4  * Free Software Foundation, either version 3 of the License, or (at your
5  * option) any later version.
6  *
7  * MoFEM is distributed in the hope that it will be useful, but WITHOUT
8  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  * License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with MoFEM. If not, see <http://www.gnu.org/licenses/>. */
14 
15 // #if PETSC_VERSION_GE(3,6,0)
16 // #include <petsc/private/kspimpl.h>
17 // #else
18 // #include <petsc-private/kspimpl.h>
19 // #endif
20 
21 namespace MoFEM {
22 
23 PetscErrorCode KspRhs(KSP ksp, Vec f, void *ctx) {
24  // PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
26  KspCtx *ksp_ctx = static_cast<KspCtx *>(ctx);
27  PetscLogEventBegin(ksp_ctx->MOFEM_EVENT_KspRhs, 0, 0, 0, 0);
28 
29  ksp_ctx->vecAssembleSwitch = boost::movelib::make_unique<bool>(true);
30 
31  for (auto &bit : ksp_ctx->preProcess_Rhs) {
32  bit->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
33  CHKERR bit->setKsp(ksp);
34  bit->ksp_f = f;
35  CHKERR bit->setKspCtx(KspMethod::CTX_SETFUNCTION);
37  *bit);
38  CHKERR bit->setKspCtx(KspMethod::CTX_KSPNONE);
39  ksp_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
40  }
41  for (auto &lit : ksp_ctx->loops_to_do_Rhs) {
42  lit.second->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
43  CHKERR lit.second->setKspCtx(KspMethod::CTX_SETFUNCTION);
44  CHKERR lit.second->setKsp(ksp);
45  lit.second->ksp_f = f;
47  ksp_ctx->problemName, lit.first, *(lit.second), nullptr, ksp_ctx->bH);
48  CHKERR lit.second->setKspCtx(KspMethod::CTX_KSPNONE);
49  ksp_ctx->vecAssembleSwitch = boost::move(lit.second->vecAssembleSwitch);
50  }
51  for (auto &bit : ksp_ctx->postProcess_Rhs) {
52  bit->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
53  CHKERR bit->setKsp(ksp);
54  bit->ksp_f = f;
55  CHKERR bit->setKspCtx(KspMethod::CTX_SETFUNCTION);
57  ksp_ctx->problemName, *bit);
58  CHKERR bit->setKspCtx(KspMethod::CTX_KSPNONE);
59  ksp_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
60  }
61 
62  if (*ksp_ctx->vecAssembleSwitch) {
63  CHKERR VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE);
64  CHKERR VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE);
65  CHKERR VecAssemblyBegin(f);
66  CHKERR VecAssemblyEnd(f);
67  }
68  PetscLogEventEnd(ksp_ctx->MOFEM_EVENT_KspRhs, 0, 0, 0, 0);
70 }
71 PetscErrorCode KspMat(KSP ksp, Mat A, Mat B, void *ctx) {
72  // PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
74  KspCtx *ksp_ctx = static_cast<KspCtx *>(ctx);
75  PetscLogEventBegin(ksp_ctx->MOFEM_EVENT_KspMat, 0, 0, 0, 0);
76 
77  ksp_ctx->matAssembleSwitch = boost::movelib::make_unique<bool>(true);
78 
79  for (auto &bit : ksp_ctx->preProcess_Mat) {
80  bit->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
81  CHKERR bit->setKsp(ksp);
82  bit->ksp_A = A;
83  bit->ksp_B = B;
84  CHKERR bit->setKspCtx(KspMethod::CTX_OPERATORS);
86  *bit);
87  CHKERR bit->setKspCtx(KspMethod::CTX_KSPNONE);
88  ksp_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
89  }
90  for (auto &lit : ksp_ctx->loops_to_do_Mat) {
91  lit.second->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
92  lit.second->ksp_A = A;
93  lit.second->ksp_B = B;
94  CHKERR lit.second->setKsp(ksp);
95  CHKERR lit.second->setKspCtx(KspMethod::CTX_OPERATORS);
97  ksp_ctx->problemName, lit.first, *(lit.second), nullptr, ksp_ctx->bH);
98  CHKERR lit.second->setKspCtx(KspMethod::CTX_KSPNONE);
99  ksp_ctx->matAssembleSwitch = boost::move(lit.second->matAssembleSwitch);
100  }
101  for (auto &bit : ksp_ctx->postProcess_Mat) {
102  bit->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
103  CHKERR bit->setKsp(ksp);
104  bit->ksp_A = A;
105  bit->ksp_B = B;
106  CHKERR bit->setKspCtx(KspMethod::CTX_OPERATORS);
108  ksp_ctx->problemName, *bit);
109  CHKERR bit->setKspCtx(KspMethod::CTX_KSPNONE);
110  ksp_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
111  }
112 
113  if (ksp_ctx->matAssembleSwitch) {
114  CHKERR MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);
115  CHKERR MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);
116  }
117  PetscLogEventEnd(ksp_ctx->MOFEM_EVENT_KspMat, 0, 0, 0, 0);
119 }
120 
121 } // namespace MoFEM
BasicMethodsSequence postProcess_Rhs
Sequence of methods run after residual is assembled.
Definition: KspCtx.hpp:59
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:477
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:21
std::string problemName
Problem name.
Definition: KspCtx.hpp:32
BasicMethodsSequence postProcess_Mat
Definition: KspCtx.hpp:54
PetscErrorCode KspRhs(KSP ksp, Vec f, void *ctx)
Run over elements in the lists.
Definition: KspCtx.cpp:23
MoFEMTypes bH
If set to MF_EXIST check if element exist.
Definition: KspCtx.hpp:33
MoFEM::Interface & mField
Definition: KspCtx.hpp:29
BasicMethodsSequence preProcess_Mat
Definition: KspCtx.hpp:52
PetscLogEvent MOFEM_EVENT_KspRhs
Definition: KspCtx.hpp:116
boost::movelib::unique_ptr< bool > vecAssembleSwitch
Definition: KspCtx.hpp:119
BasicMethodsSequence preProcess_Rhs
Sequence of methods run before residual is assembled.
Definition: KspCtx.hpp:57
PetscErrorCode KspMat(KSP ksp, Mat A, Mat B, void *ctx)
Run over elenents in the list.
Definition: KspCtx.cpp:71
virtual MoFEMErrorCode problem_basic_method_preProcess(const Problem *problem_ptr, BasicMethod &method, int verb=DEFAULT_VERBOSITY)=0
Set data for BasicMethod.
#define CHKERR
Inline error check.
Definition: definitions.h:596
boost::movelib::unique_ptr< bool > matAssembleSwitch
Definition: KspCtx.hpp:120
FEMethodsSequence loops_to_do_Rhs
Definition: KspCtx.hpp:50
Interface for linear (KSP) solver.
Definition: KspCtx.hpp:27
PetscLogEvent MOFEM_EVENT_KspMat
Definition: KspCtx.hpp:117
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:407
virtual MoFEMErrorCode problem_basic_method_postProcess(const Problem *problem_ptr, BasicMethod &method, int verb=DEFAULT_VERBOSITY)=0
Set data for BasicMethodThis function set data about problem, adjacencies and other multi-indices in ...
FEMethodsSequence loops_to_do_Mat
Definition: KspCtx.hpp:48
DEPRECATED MoFEMErrorCode loop_finite_elements(const Problem *problem_ptr, const std::string &fe_name, FEMethod &method, int lower_rank, int upper_rank, MoFEMTypes bh, int verb=DEFAULT_VERBOSITY)