v0.14.0
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 
9 namespace 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 
21 PetscErrorCode 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 cache_ptr = boost::make_shared<CacheTuple>();
31  cache_ptr);
32 
33  auto set = [&](auto &fe) {
34  fe.ksp = ksp;
35  fe.ksp_ctx = KspMethod::CTX_SETFUNCTION;
36  fe.data_ctx = PetscData::CtxSetF;
37  fe.ksp_f = f;
38  fe.cacheWeakPtr = cache_ptr;
39  };
40 
41  auto unset = [&](auto &fe) {
42  fe.ksp_ctx = KspMethod::CTX_KSPNONE;
43  fe.data_ctx = PetscData::CtxSetNone;
44  };
45 
46  // pre-process
47  for (auto &bit : ksp_ctx->preProcess_Rhs) {
48  bit->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
49  set(*bit);
51  *bit);
52  unset(*bit);
53  ksp_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
54  }
55 
56  // operators
57  for (auto &lit : ksp_ctx->loops_to_do_Rhs) {
58  lit.second->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
59  set(*lit.second);
60  CHKERR ksp_ctx->mField.loop_finite_elements(ksp_ctx->problemName, lit.first,
61  *(lit.second), nullptr,
62  ksp_ctx->bH, cache_ptr);
63  unset(*lit.second);
64  ksp_ctx->vecAssembleSwitch = boost::move(lit.second->vecAssembleSwitch);
65  }
66 
67  // post-process
68  for (auto &bit : ksp_ctx->postProcess_Rhs) {
69  bit->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
70  set(*bit);
72  ksp_ctx->problemName, *bit);
73  unset(*bit);
74  ksp_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
75  }
76 
77  if (*ksp_ctx->vecAssembleSwitch) {
78  CHKERR VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE);
79  CHKERR VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE);
80  CHKERR VecAssemblyBegin(f);
81  CHKERR VecAssemblyEnd(f);
82  }
83  PetscLogEventEnd(ksp_ctx->MOFEM_EVENT_KspRhs, 0, 0, 0, 0);
85 }
86 PetscErrorCode KspMat(KSP ksp, Mat A, Mat B, void *ctx) {
87  // PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
89  KspCtx *ksp_ctx = static_cast<KspCtx *>(ctx);
90  PetscLogEventBegin(ksp_ctx->MOFEM_EVENT_KspMat, 0, 0, 0, 0);
91 
92  ksp_ctx->matAssembleSwitch = boost::movelib::make_unique<bool>(true);
93  auto cache_ptr = boost::make_shared<CacheTuple>();
95  cache_ptr);
96 
97  auto set = [&](auto &fe) {
98  fe.ksp = ksp;
99  fe.ksp_A = A;
100  fe.ksp_B = B;
101  fe.ksp_ctx = KspMethod::CTX_OPERATORS;
102  fe.data_ctx = PetscData::CtxSetA | PetscData::CtxSetB;
103  fe.cacheWeakPtr = cache_ptr;
104  };
105 
106  auto unset = [&](auto &fe) {
107  fe.ksp_ctx = KspMethod::CTX_KSPNONE;
108  fe.data_ctx = PetscData::CtxSetNone;
109  };
110 
111  auto ent_data_cache = boost::make_shared<std::vector<EntityCacheDofs>>();
112  auto ent_row_cache =
113  boost::make_shared<std::vector<EntityCacheNumeredDofs>>();
114  auto ent_col_cache =
115  boost::make_shared<std::vector<EntityCacheNumeredDofs>>();
116 
117  // pre-procsess
118  for (auto &bit : ksp_ctx->preProcess_Mat) {
119  bit->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
120  set(*bit);
122  *bit);
123  unset(*bit);
124  ksp_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
125  }
126 
127  // operators
128  for (auto &lit : ksp_ctx->loops_to_do_Mat) {
129  lit.second->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
130  set(*lit.second);
131  CHKERR ksp_ctx->mField.loop_finite_elements(ksp_ctx->problemName, lit.first,
132  *(lit.second), nullptr,
133  ksp_ctx->bH, cache_ptr);
134  unset(*lit.second);
135  ksp_ctx->matAssembleSwitch = boost::move(lit.second->matAssembleSwitch);
136  }
137 
138  // post-process
139  for (auto &bit : ksp_ctx->postProcess_Mat) {
140  bit->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
141  set(*bit);
143  ksp_ctx->problemName, *bit);
144  unset(*bit);
145  ksp_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
146  }
147 
148  if (ksp_ctx->matAssembleSwitch) {
149  CHKERR MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);
150  CHKERR MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);
151  }
152  PetscLogEventEnd(ksp_ctx->MOFEM_EVENT_KspMat, 0, 0, 0, 0);
154 }
155 
156 } // namespace MoFEM
MoFEM::KspRhs
PetscErrorCode KspRhs(KSP ksp, Vec f, void *ctx)
Run over elements in the lists.
Definition: KspCtx.cpp:21
MoFEM::KspMethod::CTX_KSPNONE
@ CTX_KSPNONE
Definition: LoopMethods.hpp:73
MoFEM::CoreInterface::problem_basic_method_postProcess
virtual MoFEMErrorCode problem_basic_method_postProcess(const Problem *problem_ptr, BasicMethod &method, int verb=DEFAULT_VERBOSITY)=0
Set data for BasicMethod.
MoFEM::CoreInterface::loop_finite_elements
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.
MoFEM::KspCtx::loops_to_do_Rhs
FEMethodsSequence loops_to_do_Rhs
Definition: KspCtx.hpp:27
MoFEM::PetscData::CtxSetB
static constexpr Switches CtxSetB
Definition: LoopMethods.hpp:38
MoFEM::PetscData::CtxSetA
static constexpr Switches CtxSetA
Definition: LoopMethods.hpp:37
MoFEM::KspCtx::problemName
std::string problemName
Problem name.
Definition: KspCtx.hpp:18
MoFEM::Exceptions::MoFEMErrorCode
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
MoFEM::KspCtx::bH
MoFEMTypes bH
If set to MF_EXIST check if element exist.
Definition: KspCtx.hpp:19
MoFEM::KspCtx::mField
MoFEM::Interface & mField
Definition: KspCtx.hpp:15
A
constexpr AssemblyType A
Definition: operators_tests.cpp:30
MoFEM::KspCtx::postProcess_Rhs
BasicMethodsSequence postProcess_Rhs
Definition: KspCtx.hpp:35
MoFEM::KspMethod::CTX_SETFUNCTION
@ CTX_SETFUNCTION
Definition: LoopMethods.hpp:73
MoFEM::CoreInterface::problem_basic_method_preProcess
virtual MoFEMErrorCode problem_basic_method_preProcess(const Problem *problem_ptr, BasicMethod &method, int verb=DEFAULT_VERBOSITY)=0
Set data for BasicMethod.
CHKERR
#define CHKERR
Inline error check.
Definition: definitions.h:548
MoFEM::KspCtx::loops_to_do_Mat
FEMethodsSequence loops_to_do_Mat
Definition: KspCtx.hpp:25
MoFEM::KspCtx::clearLoops
MoFEMErrorCode clearLoops()
Clear loops.
Definition: KspCtx.cpp:11
MoFEM
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:10
MoFEM::KspCtx::preProcess_Mat
BasicMethodsSequence preProcess_Mat
Definition: KspCtx.hpp:29
MoFEM::KspCtx::matAssembleSwitch
boost::movelib::unique_ptr< bool > matAssembleSwitch
Definition: KspCtx.hpp:133
bit
auto bit
set bit
Definition: hanging_node_approx.cpp:75
MoFEM::KspCtx::postProcess_Mat
BasicMethodsSequence postProcess_Mat
Definition: KspCtx.hpp:31
MoFEM::CoreInterface::cache_problem_entities
virtual MoFEMErrorCode cache_problem_entities(const std::string prb_name, CacheTupleWeakPtr cache_ptr)=0
Cache variables.
MoFEM::PetscData::CtxSetNone
static constexpr Switches CtxSetNone
Definition: LoopMethods.hpp:35
MoFEM::KspCtx::MOFEM_EVENT_KspRhs
PetscLogEvent MOFEM_EVENT_KspRhs
Definition: KspCtx.hpp:129
MoFEM::KspCtx::MOFEM_EVENT_KspMat
PetscLogEvent MOFEM_EVENT_KspMat
Definition: KspCtx.hpp:130
HenckyOps::f
auto f
Definition: HenckyOps.hpp:15
MoFEM::KspMat
PetscErrorCode KspMat(KSP ksp, Mat A, Mat B, void *ctx)
Run over elements in the list.
Definition: KspCtx.cpp:86
EigenMatrix::Vec
const FTensor::Tensor2< T, Dim, Dim > Vec
Definition: MatrixFunction.hpp:66
MoFEM::KspCtx
Interface for linear (KSP) solver.
Definition: KspCtx.hpp:13
MoFEM::KspMethod::CTX_OPERATORS
@ CTX_OPERATORS
Definition: LoopMethods.hpp:73
MoFEM::PetscData::CtxSetF
static constexpr Switches CtxSetF
Definition: LoopMethods.hpp:36
MoFEM::KspCtx::vecAssembleSwitch
boost::movelib::unique_ptr< bool > vecAssembleSwitch
Definition: KspCtx.hpp:132
MoFEMFunctionReturn
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:429
MoFEM::KspCtx::preProcess_Rhs
BasicMethodsSequence preProcess_Rhs
Definition: KspCtx.hpp:33
MoFEMFunctionBegin
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:359