v0.10.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  auto set = [&](auto &fe) {
32  fe.ksp = ksp;
33  fe.ksp_ctx = KspMethod::CTX_SETFUNCTION;
34  fe.data_ctx = PetscData::CtxSetF;
35  fe.ksp_f = f;
36  };
37 
38  auto unset = [&](auto &fe) {
39  fe.ksp_ctx = KspMethod::CTX_KSPNONE;
40  fe.data_ctx = PetscData::CtxSetNone;
41  };
42 
43  auto cache_ptr = boost::make_shared<CacheTuple>();
45  cache_ptr);
46 
47  // pre-process
48  for (auto &bit : ksp_ctx->preProcess_Rhs) {
49  bit->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
50  set(*bit);
52  *bit);
53  unset(*bit);
54  ksp_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
55  }
56 
57  // operators
58  for (auto &lit : ksp_ctx->loops_to_do_Rhs) {
59  lit.second->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
60  set(*lit.second);
61  CHKERR ksp_ctx->mField.loop_finite_elements(ksp_ctx->problemName, lit.first,
62  *(lit.second), nullptr,
63  ksp_ctx->bH, cache_ptr);
64  unset(*lit.second);
65  ksp_ctx->vecAssembleSwitch = boost::move(lit.second->vecAssembleSwitch);
66  }
67 
68  // post-process
69  for (auto &bit : ksp_ctx->postProcess_Rhs) {
70  bit->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
71  set(*bit);
73  ksp_ctx->problemName, *bit);
74  unset(*bit);
75  ksp_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
76  }
77 
78  if (*ksp_ctx->vecAssembleSwitch) {
79  CHKERR VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE);
80  CHKERR VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE);
81  CHKERR VecAssemblyBegin(f);
82  CHKERR VecAssemblyEnd(f);
83  }
84  PetscLogEventEnd(ksp_ctx->MOFEM_EVENT_KspRhs, 0, 0, 0, 0);
86 }
87 PetscErrorCode KspMat(KSP ksp, Mat A, Mat B, void *ctx) {
88  // PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
90  KspCtx *ksp_ctx = static_cast<KspCtx *>(ctx);
91  PetscLogEventBegin(ksp_ctx->MOFEM_EVENT_KspMat, 0, 0, 0, 0);
92 
93  ksp_ctx->matAssembleSwitch = boost::movelib::make_unique<bool>(true);
94 
95  auto set = [&](auto &fe) {
96  fe.ksp = ksp;
97  fe.ksp_A = A;
98  fe.ksp_B = B;
99  fe.ksp_ctx = KspMethod::CTX_OPERATORS;
100  fe.data_ctx = PetscData::CtxSetA | PetscData::CtxSetB;
101  };
102 
103  auto unset = [&](auto &fe) {
104  fe.ksp_ctx = KspMethod::CTX_KSPNONE;
105  fe.data_ctx = PetscData::CtxSetNone;
106  };
107 
108  auto ent_data_cache = boost::make_shared<std::vector<EntityCacheDofs>>();
109  auto ent_row_cache =
110  boost::make_shared<std::vector<EntityCacheNumeredDofs>>();
111  auto ent_col_cache =
112  boost::make_shared<std::vector<EntityCacheNumeredDofs>>();
113 
114  // pre-procsess
115  for (auto &bit : ksp_ctx->preProcess_Mat) {
116  bit->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
117  set(*bit);
119  *bit);
120  unset(*bit);
121  ksp_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
122  }
123 
124  auto cache_ptr = boost::make_shared<CacheTuple>();
126  cache_ptr);
127 
128  // operators
129  for (auto &lit : ksp_ctx->loops_to_do_Mat) {
130  lit.second->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
131  set(*lit.second);
132  CHKERR ksp_ctx->mField.loop_finite_elements(ksp_ctx->problemName, lit.first,
133  *(lit.second), nullptr,
134  ksp_ctx->bH, cache_ptr);
135  unset(*lit.second);
136  ksp_ctx->matAssembleSwitch = boost::move(lit.second->matAssembleSwitch);
137  }
138 
139  // post-process
140  for (auto &bit : ksp_ctx->postProcess_Mat) {
141  bit->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
142  set(*bit);
144  ksp_ctx->problemName, *bit);
145  unset(*bit);
146  ksp_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
147  }
148 
149  if (ksp_ctx->matAssembleSwitch) {
150  CHKERR MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);
151  CHKERR MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);
152  }
153  PetscLogEventEnd(ksp_ctx->MOFEM_EVENT_KspMat, 0, 0, 0, 0);
155 }
156 
157 } // namespace MoFEM
BasicMethodsSequence postProcess_Rhs
Definition: KspCtx.hpp:49
virtual MoFEMErrorCode cache_problem_entities(const std::string prb_name, CacheTupleWeakPtr cache_ptr)=0
Cache variables.
static constexpr Switches CtxSetB
Definition: LoopMethods.hpp:64
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:485
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:45
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:43
PetscLogEvent MOFEM_EVENT_KspRhs
Definition: KspCtx.hpp:106
boost::movelib::unique_ptr< bool > vecAssembleSwitch
Definition: KspCtx.hpp:109
static constexpr Switches CtxSetF
Definition: LoopMethods.hpp:62
BasicMethodsSequence preProcess_Rhs
Definition: KspCtx.hpp:47
PetscErrorCode KspMat(KSP ksp, Mat A, Mat B, void *ctx)
Run over elenents in the list.
Definition: KspCtx.cpp:87
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:604
boost::movelib::unique_ptr< bool > matAssembleSwitch
Definition: KspCtx.hpp:110
static constexpr Switches CtxSetA
Definition: LoopMethods.hpp:63
FEMethodsSequence loops_to_do_Rhs
Definition: KspCtx.hpp:41
Interface for linear (KSP) solver.
Definition: KspCtx.hpp:27
PetscLogEvent MOFEM_EVENT_KspMat
Definition: KspCtx.hpp:107
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:415
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:39
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)
static constexpr Switches CtxSetNone
Definition: LoopMethods.hpp:61