v0.8.23
TsCtx.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/tsimpl.h>
17 // #else
18 // #include <petsc-private/tsimpl.h>
19 // #endif
20 
21 namespace MoFEM {
22 
23 PetscErrorCode TsSetIFunction(TS ts, PetscReal t, Vec u, Vec u_t, Vec F,
24  void *ctx) {
25  // PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27  TsCtx *ts_ctx = (TsCtx *)ctx;
28  PetscLogEventBegin(ts_ctx->MOFEM_EVENT_TsCtxIFunction, 0, 0, 0, 0);
29  CHKERR VecGhostUpdateBegin(u, INSERT_VALUES, SCATTER_FORWARD);
30  CHKERR VecGhostUpdateEnd(u, INSERT_VALUES, SCATTER_FORWARD);
31  CHKERR VecGhostUpdateBegin(u_t, INSERT_VALUES, SCATTER_FORWARD);
32  CHKERR VecGhostUpdateEnd(u_t, INSERT_VALUES, SCATTER_FORWARD);
33  CHKERR ts_ctx->mField.getInterface<VecManager>()->setLocalGhostVector(
34  ts_ctx->problemName, COL, u, INSERT_VALUES, SCATTER_REVERSE);
35 
36  auto zero_ghost_vec = [](Vec g) {
38  Vec l;
39  CHKERR VecGhostGetLocalForm(g, &l);
40  double *a;
41  CHKERR VecGetArray(l, &a);
42  int s;
43  CHKERR VecGetLocalSize(l, &s);
44  for (int i = 0; i != s; ++i)
45  a[i] = 0;
46  CHKERR VecRestoreArray(l, &a);
47  CHKERR VecGhostRestoreLocalForm(g, &l);
49  };
50  CHKERR zero_ghost_vec(F);
51 
52  int step;
53  CHKERR TSGetTimeStepNumber(ts, &step);
54  // preprocess
55  for (auto &bit : ts_ctx->preProcess_IFunction) {
56  bit->ts_u = u;
57  bit->ts_u_t = u_t;
58  bit->ts_F = F;
59  bit->ts_t = t;
60  bit->ts_step = step;
61  CHKERR bit->setTsCtx(TSMethod::CTX_TSSETIFUNCTION);
62  CHKERR bit->setTs(ts);
64  *bit);
65  CHKERR bit->setTsCtx(TSMethod::CTX_TSNONE);
66  }
67 
68  // fe loops
69  for (auto &lit : ts_ctx->loops_to_do_IFunction) {
70  lit.second->ts_u = u;
71  lit.second->ts_u_t = u_t;
72  lit.second->ts_F = F;
73  lit.second->ts_t = t;
74  lit.second->ts_step = step;
75  CHKERR lit.second->setTsCtx(TSMethod::CTX_TSSETIFUNCTION);
76  CHKERR lit.second->setTs(ts);
78  ts_ctx->problemName, lit.first, *(lit.second), nullptr, ts_ctx->bH);
79  CHKERR lit.second->setTsCtx(TSMethod::CTX_TSNONE);
80  }
81 
82  // post process
83  for (auto &bit : ts_ctx->postProcess_IFunction) {
84  bit->ts_u = u;
85  bit->ts_u_t = u_t;
86  bit->ts_F = F;
87  bit->ts_t = t;
88  bit->ts_step = step;
89  CHKERR bit->setTsCtx(TSMethod::CTX_TSSETIFUNCTION);
90  CHKERR bit->setTs(ts);
92  *bit);
93  CHKERR bit->setTsCtx(TSMethod::CTX_TSNONE);
94  }
95 
96  CHKERR VecGhostUpdateBegin(F, ADD_VALUES, SCATTER_REVERSE);
97  CHKERR VecGhostUpdateEnd(F, ADD_VALUES, SCATTER_REVERSE);
98  CHKERR VecAssemblyBegin(F);
99  CHKERR VecAssemblyEnd(F);
100  PetscLogEventEnd(ts_ctx->MOFEM_EVENT_TsCtxIFunction, 0, 0, 0, 0);
102 }
103 PetscErrorCode TsSetIJacobian(TS ts, PetscReal t, Vec u, Vec u_t, PetscReal a,
104  Mat A, Mat B, void *ctx) {
105  // PetscValidHeaderSpecific(ts,TS_CLASSID,1);
107  TsCtx *ts_ctx = (TsCtx *)ctx;
108  PetscLogEventBegin(ts_ctx->MOFEM_EVENT_TsCtxIFunction, 0, 0, 0, 0);
109  CHKERR VecGhostUpdateBegin(u, INSERT_VALUES, SCATTER_FORWARD);
110  CHKERR VecGhostUpdateEnd(u, INSERT_VALUES, SCATTER_FORWARD);
111  CHKERR VecGhostUpdateBegin(u_t, INSERT_VALUES, SCATTER_FORWARD);
112  CHKERR VecGhostUpdateEnd(u_t, INSERT_VALUES, SCATTER_FORWARD);
113  CHKERR ts_ctx->mField.getInterface<VecManager>()->setLocalGhostVector(
114  ts_ctx->problemName, COL, u, INSERT_VALUES, SCATTER_REVERSE);
115  if (ts_ctx->zeroMatrix) {
116  CHKERR MatZeroEntries(B);
117  }
118  int step;
119  CHKERR TSGetTimeStepNumber(ts, &step);
120 
121  // preproces
122  for (auto &bit : ts_ctx->preProcess_IJacobian) {
123  bit->ts_u = u;
124  bit->ts_u_t = u_t;
125  bit->ts_A = A;
126  bit->ts_B = B;
127  bit->ts_t = t;
128  bit->ts_a = a;
129  bit->ts_step = step;
130  CHKERR bit->setTsCtx(TSMethod::CTX_TSSETIJACOBIAN);
131  CHKERR bit->setTs(ts);
133  *bit);
134  CHKERR bit->setTsCtx(TSMethod::CTX_TSNONE);
135  }
136 
137  for (auto &lit : ts_ctx->loops_to_do_IJacobian) {
138  lit.second->ts_u = u;
139  lit.second->ts_u_t = u_t;
140  lit.second->ts_A = A;
141  lit.second->ts_B = B;
142  lit.second->ts_t = t;
143  lit.second->ts_a = a;
144  lit.second->ts_step = step;
145  CHKERR lit.second->setTsCtx(TSMethod::CTX_TSSETIJACOBIAN);
146  CHKERR lit.second->setTs(ts);
148  ts_ctx->problemName, lit.first, *(lit.second), nullptr, ts_ctx->bH);
149  CHKERR lit.second->setTsCtx(TSMethod::CTX_TSNONE);
150  }
151 
152  // post process
153  for (auto &bit : ts_ctx->postProcess_IJacobian) {
154  bit->ts_u = u;
155  bit->ts_u_t = u_t;
156  bit->ts_A = A;
157  bit->ts_B = B;
158  bit->ts_t = t;
159  bit->ts_a = a;
160  bit->ts_step = step;
161  CHKERR bit->setTsCtx(TSMethod::CTX_TSSETIJACOBIAN);
162  CHKERR bit->setTs(ts);
164  *bit);
165  CHKERR bit->setTsCtx(TSMethod::CTX_TSNONE);
166  }
167 
168  if (ts_ctx->zeroMatrix) {
169  CHKERR MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);
170  CHKERR MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);
171  }
172  PetscLogEventEnd(ts_ctx->MOFEM_EVENT_TsCtxIFunction, 0, 0, 0, 0);
174 }
175 PetscErrorCode TsMonitorSet(TS ts, PetscInt step, PetscReal t, Vec u,
176  void *ctx) {
177  // PetscValidHeaderSpecific(ts,TS_CLASSID,1);
179  TsCtx *ts_ctx = (TsCtx *)ctx;
180  PetscLogEventBegin(ts_ctx->MOFEM_EVENT_TsCtxRHSFunction, 0, 0, 0, 0);
181  CHKERR VecGhostUpdateBegin(u, INSERT_VALUES, SCATTER_FORWARD);
182  CHKERR VecGhostUpdateEnd(u, INSERT_VALUES, SCATTER_FORWARD);
183  CHKERR ts_ctx->mField.getInterface<VecManager>()->setLocalGhostVector(
184  ts_ctx->problemName, COL, u, INSERT_VALUES, SCATTER_REVERSE);
185 
186  // preproces
187  for (auto &bit : ts_ctx->preProcess_Monitor) {
188  bit->ts_u = u;
189  bit->ts_t = t;
190  bit->ts_step = step;
191  bit->ts_F = PETSC_NULL;
192  CHKERR bit->setTsCtx(TSMethod::CTX_TSSETIJACOBIAN);
193  CHKERR bit->setTs(ts);
195  *bit);
196  CHKERR bit->setTsCtx(TSMethod::CTX_TSNONE);
197  }
198 
199  for (auto &lit : ts_ctx->loops_to_do_Monitor) {
200  lit.second->ts_u = u;
201  lit.second->ts_t = t;
202  lit.second->ts_step = step;
203  lit.second->ts_F = PETSC_NULL;
204  CHKERR lit.second->setTsCtx(TSMethod::CTX_TSTSMONITORSET);
205  CHKERR lit.second->setTs(ts);
207  ts_ctx->problemName, lit.first, *(lit.second), nullptr, ts_ctx->bH);
208  CHKERR lit.second->setTsCtx(TSMethod::CTX_TSNONE);
209  }
210 
211  // post process
212  for (auto &bit : ts_ctx->postProcess_Monitor) {
213  bit->ts_u = u;
214  bit->ts_t = t;
215  bit->ts_step = step;
216  bit->ts_F = PETSC_NULL;
217  CHKERR bit->setTsCtx(TSMethod::CTX_TSSETIJACOBIAN);
218  CHKERR bit->setTs(ts);
220  *bit);
221  CHKERR bit->setTsCtx(TSMethod::CTX_TSNONE);
222  }
223  PetscLogEventEnd(ts_ctx->MOFEM_EVENT_TsCtxRHSFunction, 0, 0, 0, 0);
225 }
226 
227 } // namespace MoFEM
FEMethodsSequence loops_to_do_Monitor
Definition: TsCtx.hpp:50
PetscLogEvent MOFEM_EVENT_TsCtxIFunction
Definition: TsCtx.hpp:60
bool zeroMatrix
Definition: TsCtx.hpp:64
Interface for Time Stepping (TS) solver.
Definition: TsCtx.hpp:27
MoFEMTypes bH
If set to MF_EXIST check if element exist.
Definition: TsCtx.hpp:33
BasicMethodsSequence preProcess_IJacobian
Definition: TsCtx.hpp:51
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:476
PetscErrorCode TsMonitorSet(TS ts, PetscInt step, PetscReal t, Vec u, void *ctx)
Set monitor for TS solver.
Definition: TsCtx.cpp:175
std::string problemName
Definition: TsCtx.hpp:32
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:21
PetscLogEvent MOFEM_EVENT_TsCtxRHSFunction
Definition: TsCtx.hpp:58
MoFEMErrorCode getInterface(const MOFEMuuid &uuid, IFACE *&iface) const
Get interface by uuid and return reference to pointer of interface.
PetscErrorCode TsSetIFunction(TS ts, PetscReal t, Vec u, Vec u_t, Vec F, void *ctx)
Set IFunction for TS solver.
Definition: TsCtx.cpp:23
BasicMethodsSequence postProcess_IFunction
Definition: TsCtx.hpp:54
BasicMethodsSequence preProcess_IFunction
Definition: TsCtx.hpp:53
BasicMethodsSequence postProcess_IJacobian
Definition: TsCtx.hpp:52
Vector manager is used to create vectors \mofem_vectors.
Definition: VecManager.hpp:35
FEMethodsSequence loops_to_do_IJacobian
Definition: TsCtx.hpp:48
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:595
FEMethodsSequence loops_to_do_IFunction
Definition: TsCtx.hpp:49
MoFEM::Interface & mField
Definition: TsCtx.hpp:29
BasicMethodsSequence preProcess_Monitor
Definition: TsCtx.hpp:55
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:406
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 ...
BasicMethodsSequence postProcess_Monitor
Definition: TsCtx.hpp:56
PetscErrorCode TsSetIJacobian(TS ts, PetscReal t, Vec u, Vec u_t, PetscReal a, Mat A, Mat B, void *ctx)
Set function evaluating jacobina in TS solver.
Definition: TsCtx.cpp:103
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)