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