v0.9.1
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  // pre-process
44  for (auto &bit : ksp_ctx->preProcess_Rhs) {
45  bit->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
46  set(*bit);
48  *bit);
49  unset(*bit);
50  ksp_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
51  }
52 
53  // operators
54  for (auto &lit : ksp_ctx->loops_to_do_Rhs) {
55  lit.second->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
56  set(*lit.second);
58  ksp_ctx->problemName, lit.first, *(lit.second), nullptr, ksp_ctx->bH);
59  unset(*lit.second);
60  ksp_ctx->vecAssembleSwitch = boost::move(lit.second->vecAssembleSwitch);
61  }
62 
63  // post-process
64  for (auto &bit : ksp_ctx->postProcess_Rhs) {
65  bit->vecAssembleSwitch = boost::move(ksp_ctx->vecAssembleSwitch);
66  set(*bit);
68  ksp_ctx->problemName, *bit);
69  unset(*bit);
70  ksp_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
71  }
72 
73  if (*ksp_ctx->vecAssembleSwitch) {
74  CHKERR VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE);
75  CHKERR VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE);
76  CHKERR VecAssemblyBegin(f);
77  CHKERR VecAssemblyEnd(f);
78  }
79  PetscLogEventEnd(ksp_ctx->MOFEM_EVENT_KspRhs, 0, 0, 0, 0);
81 }
82 PetscErrorCode KspMat(KSP ksp, Mat A, Mat B, void *ctx) {
83  // PetscValidHeaderSpecific(ksp,KSP_CLASSID,1);
85  KspCtx *ksp_ctx = static_cast<KspCtx *>(ctx);
86  PetscLogEventBegin(ksp_ctx->MOFEM_EVENT_KspMat, 0, 0, 0, 0);
87 
88  ksp_ctx->matAssembleSwitch = boost::movelib::make_unique<bool>(true);
89 
90  auto set = [&](auto &fe) {
91  fe.ksp = ksp;
92  fe.ksp_A = A;
93  fe.ksp_B = B;
94  fe.ksp_ctx = KspMethod::CTX_OPERATORS;
95  fe.data_ctx = PetscData::CtxSetA | PetscData::CtxSetB;
96  };
97 
98  auto unset = [&](auto &fe) {
99  fe.ksp_ctx = KspMethod::CTX_KSPNONE;
100  fe.data_ctx = PetscData::CtxSetNone;
101  };
102 
103  // pre-procsess
104  for (auto &bit : ksp_ctx->preProcess_Mat) {
105  bit->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
106  set(*bit);
108  *bit);
109  unset(*bit);
110  ksp_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
111  }
112 
113  // operators
114  for (auto &lit : ksp_ctx->loops_to_do_Mat) {
115  lit.second->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
116  set(*lit.second);
118  ksp_ctx->problemName, lit.first, *(lit.second), nullptr, ksp_ctx->bH);
119  unset(*lit.second);
120  ksp_ctx->matAssembleSwitch = boost::move(lit.second->matAssembleSwitch);
121  }
122 
123  // post-process
124  for (auto &bit : ksp_ctx->postProcess_Mat) {
125  bit->matAssembleSwitch = boost::move(ksp_ctx->matAssembleSwitch);
126  set(*bit);
128  ksp_ctx->problemName, *bit);
129  unset(*bit);
130  ksp_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
131  }
132 
133  if (ksp_ctx->matAssembleSwitch) {
134  CHKERR MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);
135  CHKERR MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);
136  }
137  PetscLogEventEnd(ksp_ctx->MOFEM_EVENT_KspMat, 0, 0, 0, 0);
139 }
140 
141 } // namespace MoFEM
BasicMethodsSequence postProcess_Rhs
Sequence of methods run after residual is assembled.
Definition: KspCtx.hpp:59
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:482
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
static constexpr Switches CtxSetF
Definition: LoopMethods.hpp:62
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:82
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:601
boost::movelib::unique_ptr< bool > matAssembleSwitch
Definition: KspCtx.hpp:120
static constexpr Switches CtxSetA
Definition: LoopMethods.hpp:63
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:412
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)
static constexpr Switches CtxSetNone
Definition: LoopMethods.hpp:61