v0.13.1
KspCtx.cpp
Go to the documentation of this file.
1
2
3// #if PETSC_VERSION_GE(3,6,0)
4// #include <petsc/private/kspimpl.h>
5// #else
6// #include <petsc-private/kspimpl.h>
7// #endif
8
9namespace MoFEM {
10
13 loops_to_do_Mat.clear();
14 loops_to_do_Rhs.clear();
15 preProcess_Mat.clear();
16 postProcess_Mat.clear();
17 preProcess_Rhs.clear();
19}
20
21PetscErrorCode KspRhs(KSP ksp, Vec f, void *ctx) {
22 // PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
24 KspCtx *ksp_ctx = static_cast<KspCtx *>(ctx);
25 PetscLogEventBegin(ksp_ctx->MOFEM_EVENT_KspRhs, 0, 0, 0, 0);
26
27 ksp_ctx->vecAssembleSwitch = boost::movelib::make_unique<bool>(true);
28
29 auto set = [&](auto &fe) {
30 fe.ksp = ksp;
31 fe.ksp_ctx = KspMethod::CTX_SETFUNCTION;
32 fe.data_ctx = PetscData::CtxSetF;
33 fe.ksp_f = f;
34 };
35
36 auto unset = [&](auto &fe) {
37 fe.ksp_ctx = KspMethod::CTX_KSPNONE;
38 fe.data_ctx = PetscData::CtxSetNone;
39 };
40
41 // pre-process
42 for (auto &bit : ksp_ctx->preProcess_Rhs) {
43 bit->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
44 set(*bit);
46 *bit);
47 unset(*bit);
48 ksp_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
49 }
50
51 auto cache_ptr = boost::make_shared<CacheTuple>();
53 cache_ptr);
54
55 // operators
56 for (auto &lit : ksp_ctx->loops_to_do_Rhs) {
57 lit.second->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
58 set(*lit.second);
59 CHKERR ksp_ctx->mField.loop_finite_elements(ksp_ctx->problemName, lit.first,
60 *(lit.second), nullptr,
61 ksp_ctx->bH, cache_ptr);
62 unset(*lit.second);
63 ksp_ctx->vecAssembleSwitch = boost::move(lit.second->vecAssembleSwitch);
64 }
65
66 // post-process
67 for (auto &bit : ksp_ctx->postProcess_Rhs) {
68 bit->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
69 set(*bit);
71 ksp_ctx->problemName, *bit);
72 unset(*bit);
73 ksp_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
74 }
75
76 if (*ksp_ctx->vecAssembleSwitch) {
77 CHKERR VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE);
78 CHKERR VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE);
79 CHKERR VecAssemblyBegin(f);
80 CHKERR VecAssemblyEnd(f);
81 }
82 PetscLogEventEnd(ksp_ctx->MOFEM_EVENT_KspRhs, 0, 0, 0, 0);
84}
85PetscErrorCode KspMat(KSP ksp, Mat A, Mat B, void *ctx) {
86 // PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
88 KspCtx *ksp_ctx = static_cast<KspCtx *>(ctx);
89 PetscLogEventBegin(ksp_ctx->MOFEM_EVENT_KspMat, 0, 0, 0, 0);
90
91 ksp_ctx->matAssembleSwitch = boost::movelib::make_unique<bool>(true);
92
93 auto set = [&](auto &fe) {
94 fe.ksp = ksp;
95 fe.ksp_A = A;
96 fe.ksp_B = B;
97 fe.ksp_ctx = KspMethod::CTX_OPERATORS;
99 };
100
101 auto unset = [&](auto &fe) {
102 fe.ksp_ctx = KspMethod::CTX_KSPNONE;
103 fe.data_ctx = PetscData::CtxSetNone;
104 };
105
106 auto ent_data_cache = boost::make_shared<std::vector<EntityCacheDofs>>();
107 auto ent_row_cache =
108 boost::make_shared<std::vector<EntityCacheNumeredDofs>>();
109 auto ent_col_cache =
110 boost::make_shared<std::vector<EntityCacheNumeredDofs>>();
111
112 // pre-procsess
113 for (auto &bit : ksp_ctx->preProcess_Mat) {
114 bit->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
115 set(*bit);
117 *bit);
118 unset(*bit);
119 ksp_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
120 }
121
122 auto cache_ptr = boost::make_shared<CacheTuple>();
124 cache_ptr);
125
126 // operators
127 for (auto &lit : ksp_ctx->loops_to_do_Mat) {
128 lit.second->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
129 set(*lit.second);
130 CHKERR ksp_ctx->mField.loop_finite_elements(ksp_ctx->problemName, lit.first,
131 *(lit.second), nullptr,
132 ksp_ctx->bH, cache_ptr);
133 unset(*lit.second);
134 ksp_ctx->matAssembleSwitch = boost::move(lit.second->matAssembleSwitch);
135 }
136
137 // post-process
138 for (auto &bit : ksp_ctx->postProcess_Mat) {
139 bit->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
140 set(*bit);
142 ksp_ctx->problemName, *bit);
143 unset(*bit);
144 ksp_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
145 }
146
147 if (ksp_ctx->matAssembleSwitch) {
148 CHKERR MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);
149 CHKERR MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);
150 }
151 PetscLogEventEnd(ksp_ctx->MOFEM_EVENT_KspMat, 0, 0, 0, 0);
153}
154
155} // namespace MoFEM
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:346
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:416
#define CHKERR
Inline error check.
Definition: definitions.h:535
virtual MoFEMErrorCode problem_basic_method_postProcess(const Problem *problem_ptr, BasicMethod &method, int verb=DEFAULT_VERBOSITY)=0
Set data for BasicMethod.
virtual MoFEMErrorCode loop_finite_elements(const std::string &problem_name, const std::string &fe_name, FEMethod &method, boost::shared_ptr< NumeredEntFiniteElement_multiIndex > fe_ptr=nullptr, MoFEMTypes bh=MF_EXIST, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr(), int verb=DEFAULT_VERBOSITY)=0
Make a loop over finite elements.
auto bit
set bit
const FTensor::Tensor2< T, Dim, Dim > Vec
auto f
Definition: HenckyOps.hpp:5
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
implementation of Data Operators for Forces and Sources
Definition: MoFEM.hpp:24
PetscErrorCode KspRhs(KSP ksp, Vec f, void *ctx)
Run over elements in the lists.
Definition: KspCtx.cpp:21
PetscErrorCode KspMat(KSP ksp, Mat A, Mat B, void *ctx)
Run over elenents in the list.
Definition: KspCtx.cpp:85
double A
virtual MoFEMErrorCode cache_problem_entities(const std::string prb_name, CacheTupleWeakPtr cache_ptr)=0
Cache variables.
virtual MoFEMErrorCode problem_basic_method_preProcess(const Problem *problem_ptr, BasicMethod &method, int verb=DEFAULT_VERBOSITY)=0
Set data for BasicMethod.
Interface for linear (KSP) solver.
Definition: KspCtx.hpp:15
FEMethodsSequence loops_to_do_Rhs
Definition: KspCtx.hpp:29
PetscLogEvent MOFEM_EVENT_KspRhs
Definition: KspCtx.hpp:101
BasicMethodsSequence preProcess_Rhs
Definition: KspCtx.hpp:35
BasicMethodsSequence postProcess_Rhs
Definition: KspCtx.hpp:37
BasicMethodsSequence postProcess_Mat
Definition: KspCtx.hpp:33
boost::movelib::unique_ptr< bool > matAssembleSwitch
Definition: KspCtx.hpp:105
PetscLogEvent MOFEM_EVENT_KspMat
Definition: KspCtx.hpp:102
MoFEMErrorCode clearLoops()
Clear loops.
Definition: KspCtx.cpp:11
BasicMethodsSequence preProcess_Mat
Definition: KspCtx.hpp:31
MoFEM::Interface & mField
Definition: KspCtx.hpp:17
MoFEMTypes bH
If set to MF_EXIST check if element exist.
Definition: KspCtx.hpp:21
std::string problemName
Problem name.
Definition: KspCtx.hpp:20
boost::movelib::unique_ptr< bool > vecAssembleSwitch
Definition: KspCtx.hpp:104
FEMethodsSequence loops_to_do_Mat
Definition: KspCtx.hpp:27
static constexpr Switches CtxSetA
Definition: LoopMethods.hpp:37
static constexpr Switches CtxSetNone
Definition: LoopMethods.hpp:35
static constexpr Switches CtxSetF
Definition: LoopMethods.hpp:36
static constexpr Switches CtxSetB
Definition: LoopMethods.hpp:38