v0.13.1
SnesCtx.cpp
Go to the documentation of this file.
1
2
3namespace MoFEM {
4
14}
15
18 loops_to_do_Mat.clear();
19 loops_to_do_Rhs.clear();
20 preProcess_Mat.clear();
21 postProcess_Mat.clear();
22 preProcess_Rhs.clear();
23 postProcess_Rhs.clear();
25}
26
27PetscErrorCode SnesRhs(SNES snes, Vec x, Vec f, void *ctx) {
28 SnesCtx *snes_ctx = (SnesCtx *)ctx;
29 // PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
31 PetscLogEventBegin(snes_ctx->MOFEM_EVENT_SnesRhs, 0, 0, 0, 0);
32 CHKERR VecGhostUpdateBegin(x, INSERT_VALUES, SCATTER_FORWARD);
33 CHKERR VecGhostUpdateEnd(x, INSERT_VALUES, SCATTER_FORWARD);
34 if (snes_ctx->vErify) {
35 // Verify finite elements, check for not a number
36 CHKERR VecAssemblyBegin(f);
37 CHKERR VecAssemblyEnd(f);
38 MPI_Comm comm = PetscObjectComm((PetscObject)f);
39 PetscSynchronizedPrintf(comm, "SNES Verify x\n");
40 const Problem *prb_ptr;
41 CHKERR snes_ctx->mField.get_problem(snes_ctx->problemName, &prb_ptr);
42 CHKERR snes_ctx->mField.getInterface<Tools>()->checkVectorForNotANumber(
43 prb_ptr, COL, x);
44 }
45 CHKERR snes_ctx->mField.getInterface<VecManager>()->setLocalGhostVector(
46 snes_ctx->problemName, COL, x, INSERT_VALUES, SCATTER_REVERSE);
47
48 auto zero_ghost_vec = [](Vec g) {
50 Vec l;
51 CHKERR VecGhostGetLocalForm(g, &l);
52 double *a;
53 CHKERR VecGetArray(l, &a);
54 int s;
55 CHKERR VecGetLocalSize(l, &s);
56 for (int i = 0; i != s; ++i)
57 a[i] = 0;
58 CHKERR VecRestoreArray(l, &a);
59 CHKERR VecGhostRestoreLocalForm(g, &l);
61 };
62 CHKERR zero_ghost_vec(f);
63
64 snes_ctx->vecAssembleSwitch = boost::movelib::make_unique<bool>(true);
65
66 auto set = [&](auto &fe) {
67 fe.snes = snes;
68 fe.snes_x = x;
69 fe.snes_f = f;
71 fe.ksp_ctx = KspMethod::CTX_SETFUNCTION;
73 };
74
75 auto unset = [&](auto &fe) {
76 fe.snes_ctx = SnesMethod::CTX_SNESNONE;
77 fe.ksp_ctx = KspMethod::CTX_KSPNONE;
78 fe.data_ctx = PetscData::CtxSetNone;
79 };
80
81 for (auto &bit : snes_ctx->preProcess_Rhs) {
82 bit->vecAssembleSwitch = boost::move(snes_ctx->vecAssembleSwitch);
83 set(*bit);
85 snes_ctx->problemName, *bit);
86 unset(*bit);
87 snes_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
88 }
89
90 auto cache_ptr = boost::make_shared<CacheTuple>();
92 cache_ptr);
93
94 for (auto &lit : snes_ctx->loops_to_do_Rhs) {
95 lit.second->vecAssembleSwitch = boost::move(snes_ctx->vecAssembleSwitch);
96 set(*lit.second);
98 snes_ctx->problemName, lit.first, *lit.second, nullptr, snes_ctx->bH,
99 cache_ptr);
100 unset(*lit.second);
101 if (snes_ctx->vErify) {
102 // Verify finite elements, check for not a number
103 CHKERR VecAssemblyBegin(f);
104 CHKERR VecAssemblyEnd(f);
105 MPI_Comm comm = PetscObjectComm((PetscObject)f);
106 PetscSynchronizedPrintf(comm, "SNES Verify f FE < %s >\n",
107 lit.first.c_str());
108 const Problem *prb_ptr;
109 CHKERR snes_ctx->mField.get_problem(snes_ctx->problemName, &prb_ptr);
110 CHKERR snes_ctx->mField.getInterface<Tools>()->checkVectorForNotANumber(
111 prb_ptr, ROW, f);
112 }
113
114 snes_ctx->vecAssembleSwitch = boost::move(lit.second->vecAssembleSwitch);
115 }
116
117 for (auto &bit : snes_ctx->postProcess_Rhs) {
118 bit->vecAssembleSwitch = boost::move(snes_ctx->vecAssembleSwitch);
119 set(*bit);
121 snes_ctx->problemName, *bit);
122 unset(*bit);
123 snes_ctx->vecAssembleSwitch = boost::move(bit->vecAssembleSwitch);
124 }
125
126 if (snes_ctx->vecAssembleSwitch) {
127 CHKERR VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE);
128 CHKERR VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE);
129 CHKERR VecAssemblyBegin(f);
130 CHKERR VecAssemblyEnd(f);
131 }
132 PetscLogEventEnd(snes_ctx->MOFEM_EVENT_SnesRhs, 0, 0, 0, 0);
134}
135
136PetscErrorCode SnesMat(SNES snes, Vec x, Mat A, Mat B, void *ctx) {
137 SnesCtx *snes_ctx = (SnesCtx *)ctx;
138 // PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
140 PetscLogEventBegin(snes_ctx->MOFEM_EVENT_SnesMat, 0, 0, 0, 0);
141 if (snes_ctx->zeroPreCondMatrixB)
142 CHKERR MatZeroEntries(B);
143
144 snes_ctx->matAssembleSwitch = boost::movelib::make_unique<bool>(true);
145
146 auto set = [&](auto &fe) {
147 fe.snes = snes;
148 fe.snes_x = x;
149 fe.snes_A = A;
150 fe.snes_B = B;
152 fe.ksp_ctx = KspMethod::CTX_OPERATORS;
154 };
155
156 auto unset = [&](auto &fe) {
157 fe.snes_ctx = SnesMethod::CTX_SNESNONE;
158 fe.ksp_ctx = KspMethod::CTX_KSPNONE;
159 fe.data_ctx = PetscData::CtxSetNone;
160 };
161
162
163 CHKERR VecGhostUpdateBegin(x, INSERT_VALUES, SCATTER_FORWARD);
164 CHKERR VecGhostUpdateEnd(x, INSERT_VALUES, SCATTER_FORWARD);
165 CHKERR snes_ctx->mField.getInterface<VecManager>()->setLocalGhostVector(
166 snes_ctx->problemName, COL, x, INSERT_VALUES, SCATTER_REVERSE);
167 for (auto &bit : snes_ctx->preProcess_Mat) {
168 bit->matAssembleSwitch = boost::move(snes_ctx->matAssembleSwitch);
169 set(*bit);
171 snes_ctx->problemName, *bit);
172 unset(*bit);
173 snes_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
174 }
175
176 auto cache_ptr = boost::make_shared<CacheTuple>();
178 cache_ptr);
179
180 for (auto &lit : snes_ctx->loops_to_do_Mat) {
181 lit.second->matAssembleSwitch = boost::move(snes_ctx->matAssembleSwitch);
182 set(*lit.second);
184 snes_ctx->problemName, lit.first, *(lit.second), nullptr, snes_ctx->bH,
185 cache_ptr);
186 unset(*lit.second);
187 snes_ctx->matAssembleSwitch = boost::move(lit.second->matAssembleSwitch);
188 }
189
190 for (auto &bit : snes_ctx->postProcess_Mat) {
191 bit->matAssembleSwitch = boost::move(snes_ctx->matAssembleSwitch);
192 set(*bit);
194 snes_ctx->problemName, *bit);
195 unset(*bit);
196 snes_ctx->matAssembleSwitch = boost::move(bit->matAssembleSwitch);
197 }
198
199 if (*snes_ctx->matAssembleSwitch) {
200 CHKERR MatAssemblyBegin(B, snes_ctx->typeOfAssembly);
201 CHKERR MatAssemblyEnd(B, snes_ctx->typeOfAssembly);
202 }
203 PetscLogEventEnd(snes_ctx->MOFEM_EVENT_SnesMat, 0, 0, 0, 0);
205}
206
207MoFEMErrorCode SnesMoFEMSetAssemblyType(SNES snes, MatAssemblyType type) {
208 SnesCtx *snes_ctx;
209 // PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
211 CHKERR SNESGetApplicationContext(snes, &snes_ctx);
212 snes_ctx->typeOfAssembly = type;
214}
215
217 SnesCtx *snes_ctx;
219 CHKERR SNESGetApplicationContext(snes, &snes_ctx);
220 snes_ctx->bH = bh;
222}
223
224MoFEMErrorCode MoFEMSNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm,
225 SnesCtx *snes_ctx) {
227 auto &m_field = snes_ctx->mField;
228 auto problem_ptr = m_field.get_problem(snes_ctx->problemName);
229 auto fields_ptr = m_field.get_fields();
230 auto dofs = problem_ptr->numeredRowDofsPtr;
231
232 std::vector<double> lnorms(fields_ptr->size(), 0),
233 norms(fields_ptr->size(), 0);
234
235 Vec res;
236 CHKERR SNESGetFunction(snes, &res, NULL, NULL);
237
238 const double *r;
239 CHKERR VecGetArrayRead(res, &r);
240 {
241 int f = 0;
242 for (auto fi : *fields_ptr) {
243 const auto lo_uid = FieldEntity::getLoBitNumberUId(fi->bitNumber);
244 const auto hi_uid = FieldEntity::getHiBitNumberUId(fi->bitNumber);
245 const auto hi = dofs->get<Unique_mi_tag>().upper_bound(hi_uid);
246 for (auto lo = dofs->get<Unique_mi_tag>().lower_bound(lo_uid); lo != hi;
247 ++lo) {
248 const DofIdx loc_idx = (*lo)->getPetscLocalDofIdx();
249 if (loc_idx >= 0 && loc_idx < problem_ptr->nbLocDofsRow) {
250 lnorms[f] += PetscRealPart(PetscSqr(r[loc_idx]));
251 }
252 }
253 ++f;
254 }
255 }
256 CHKERR VecRestoreArrayRead(res, &r);
257
258 MPIU_Allreduce(&*lnorms.begin(), &*norms.begin(), lnorms.size(), MPIU_REAL,
259 MPIU_SUM, PetscObjectComm((PetscObject)snes));
260
261 std::stringstream s;
262 int tl;
263 CHKERR PetscObjectGetTabLevel((PetscObject)snes, &tl);
264 for (auto t = 0; t != tl; ++t)
265 s << " ";
266 s << its << " Function norm " << boost::format("%14.12e") % (double)fgnorm
267 << " [";
268 {
269 int f = 0;
270 for (auto fi : *fields_ptr) {
271 if (f > 0)
272 s << ", ";
273 s << boost::format("%14.12e") % (double)PetscSqrtReal(norms[f]);
274 ++f;
275 }
276 s << "]";
277 }
278
279 MOFEM_LOG("SNES_WORLD", Sev::inform) << s.str();
280
282}
283
284} // namespace MoFEM
constexpr double a
@ COL
Definition: definitions.h:123
@ ROW
Definition: definitions.h:123
MoFEMTypes
Those types control how functions respond on arguments, f.e. error handling.
Definition: definitions.h:97
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:447
#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
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:440
virtual const Problem * get_problem(const std::string problem_name) const =0
Get the problem object.
#define MOFEM_LOG(channel, severity)
Log.
Definition: LogManager.hpp:301
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
FTensor::Index< 'i', SPACE_DIM > i
FTensor::Index< 'l', 3 > l
const FTensor::Tensor2< T, Dim, Dim > Vec
auto f
Definition: HenckyOps.hpp:5
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:56
int DofIdx
Index of DOF.
Definition: Types.hpp:18
implementation of Data Operators for Forces and Sources
Definition: MoFEM.hpp:24
PetscErrorCode SnesMat(SNES snes, Vec x, Mat A, Mat B, void *ctx)
This is MoFEM implementation for the left hand side (tangent matrix) evaluation in SNES solver.
Definition: SnesCtx.cpp:136
MoFEMErrorCode MoFEMSNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, SnesCtx *snes_ctx)
Sens monitor printing residual field by field.
Definition: SnesCtx.cpp:224
PetscErrorCode SnesRhs(SNES snes, Vec x, Vec f, void *ctx)
This is MoFEM implementation for the right hand side (residual vector) evaluation in SNES solver.
Definition: SnesCtx.cpp:27
MoFEMErrorCode SnesMoFEMSetAssemblyType(SNES snes, MatAssemblyType type)
Set assembly type at the end of SnesMat.
Definition: SnesCtx.cpp:207
MoFEMErrorCode SnesMoFEMSetBehavior(SNES snes, MoFEMTypes bh)
Set behavior if finite element in sequence does not exist.
Definition: SnesCtx.cpp:216
const double r
rate factor
double A
constexpr double t
plate stiffness
Definition: plate.cpp:59
constexpr double g
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.
static UId getHiBitNumberUId(const FieldBitNumber bit_number)
static UId getLoBitNumberUId(const FieldBitNumber bit_number)
static constexpr Switches CtxSetA
Definition: LoopMethods.hpp:37
static constexpr Switches CtxSetX
Definition: LoopMethods.hpp:39
static constexpr Switches CtxSetNone
Definition: LoopMethods.hpp:35
static constexpr Switches CtxSetF
Definition: LoopMethods.hpp:36
static constexpr Switches CtxSetB
Definition: LoopMethods.hpp:38
keeps basic data about problem
boost::shared_ptr< NumeredDofEntity_multiIndex > numeredRowDofsPtr
store DOFs on rows for this problem
Interface for nonlinear (SNES) solver.
Definition: SnesCtx.hpp:15
PetscLogEvent MOFEM_EVENT_SnesMat
Log events to assemble tangent matrix.
Definition: SnesCtx.hpp:131
BasicMethodsSequence preProcess_Rhs
Sequence of methods run before residual is assembled.
Definition: SnesCtx.hpp:41
BasicMethodsSequence postProcess_Mat
Definition: SnesCtx.hpp:38
MoFEMTypes bH
If set to MF_EXIST check if element exist, default MF_EXIST.
Definition: SnesCtx.hpp:22
BasicMethodsSequence postProcess_Rhs
Sequence of methods run after residual is assembled.
Definition: SnesCtx.hpp:43
MoFEMErrorCode copyLoops(const SnesCtx &snes_ctx)
Copy sequences from other SNES contex.
Definition: SnesCtx.cpp:5
MatAssemblyType typeOfAssembly
type of assembly at the end
Definition: SnesCtx.hpp:25
bool zeroPreCondMatrixB
Definition: SnesCtx.hpp:23
MoFEMErrorCode clearLoops()
Clear loops.
Definition: SnesCtx.cpp:16
BasicMethodsSequence preProcess_Mat
Definition: SnesCtx.hpp:36
bool vErify
If true verify vector.
Definition: SnesCtx.hpp:26
FEMethodsSequence loops_to_do_Mat
Definition: SnesCtx.hpp:32
std::string problemName
problem name
Definition: SnesCtx.hpp:20
FEMethodsSequence loops_to_do_Rhs
Definition: SnesCtx.hpp:34
boost::movelib::unique_ptr< bool > matAssembleSwitch
Definition: SnesCtx.hpp:129
MoFEM::Interface & mField
database Interface
Definition: SnesCtx.hpp:17
boost::movelib::unique_ptr< bool > vecAssembleSwitch
Definition: SnesCtx.hpp:128
PetscLogEvent MOFEM_EVENT_SnesRhs
Log events to assemble residual.
Definition: SnesCtx.hpp:130
Auxiliary tools.
Definition: Tools.hpp:19
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface refernce to pointer of interface.
Vector manager is used to create vectors \mofem_vectors.
Definition: VecManager.hpp:23