v0.16.0
Loading...
Searching...
No Matches
OperatorsTester.cpp
Go to the documentation of this file.
1/**
2 * @file OperatorsTester.cpp
3 * @brief
4 * @version 0.13.2
5 * @date 2022-12-04
6 *
7 * @copyright Copyright (c) 2022
8 *
9 */
10
11namespace MoFEM {
12
14 : cOre(const_cast<Core &>(core)) {}
15
17OperatorsTester::query_interface(boost::typeindex::type_index type_index,
18 UnknownInterface **iface) const {
19 *iface = const_cast<OperatorsTester *>(this);
20 return 0;
21}
22
24 SmartPetscObj<DM> dm, std::vector<RandomFieldData> random_fields,
25 boost::shared_ptr<Range> ents_ptr, RowColData r) {
26
27 MoFEM::Interface &m_field = cOre;
28
29 auto prb_ptr = getProblemPtr(dm);
30 std::mt19937 rng(0x4d584431u);
31
32 auto get_random_number = [&rng](auto &range) {
33 std::uniform_real_distribution<double> dist(range[0], range[1]);
34 return dist(rng);
35 };
36
37 auto v = createDMVector(dm, r);
38 double *array;
39 CHKERR VecGetArray(v, &array);
40
41 int size;
42 CHKERR VecGetLocalSize(v, &size);
43
44 MOFEM_LOG_CHANNEL("WORLD");
45
46 for (auto &rf : random_fields) {
47 const auto field_id = m_field.get_field_bit_number(rf.first);
48 MOFEM_TAG_AND_LOG("WORLD", Sev::noisy, "OperatorsTester")
49 << "Set random field " << rf.first << " " << static_cast<int>(field_id);
50
51 auto get_numbered_dofs = [&]() {
52 switch (r) {
53 case RowColData::ROW:
54 return prb_ptr->getNumeredRowDofsPtr();
55 case RowColData::COL:
56 return prb_ptr->getNumeredColDofsPtr();
57 default:
58 throw std::runtime_error("Invalid RowColData");
59 }
60 };
61
62 if (!ents_ptr) {
63 auto lo = get_numbered_dofs()->lower_bound(
65 const auto hi = get_numbered_dofs()->upper_bound(
67 for (; lo != hi; ++lo) {
68 const auto idx = (*lo)->getPetscLocalDofIdx();
69 if (idx >= 0 && idx < size) {
70 array[idx] = get_random_number(rf.second);
71 }
72 }
73 } else {
74 for (auto pit = ents_ptr->const_pair_begin();
75 pit != ents_ptr->const_pair_end(); ++pit) {
76 auto lo = get_numbered_dofs()->get<Unique_mi_tag>().lower_bound(
77 DofEntity::getLoFieldEntityUId(field_id, pit->first));
78 auto hi = get_numbered_dofs()->get<Unique_mi_tag>().upper_bound(
79 DofEntity::getHiFieldEntityUId(field_id, pit->second));
80 for (; lo != hi; ++lo) {
81 const auto idx = (*lo)->getPetscLocalDofIdx();
82 if (idx >= 0 && idx < size) {
83 array[idx] = get_random_number(rf.second);
84 }
85 }
86 }
87 }
88 }
89
90 CHKERR VecRestoreArray(v, &array);
91
92 CHKERR VecGhostUpdateBegin(v, INSERT_VALUES, SCATTER_FORWARD);
93 CHKERR VecGhostUpdateEnd(v, INSERT_VALUES, SCATTER_FORWARD);
94
95 return v;
96};
97
100 boost::shared_ptr<FEMethod> pipeline,
102 SmartPetscObj<Vec> delta2_x, double time,
103 double delta_t, CacheTupleWeakPtr cache_ptr) {
104
105 pipeline->data_ctx = PetscData::CtxSetF | PetscData::CtxSetTime;
106 auto f = createDMVector(dm);
107 pipeline->f = f;
108 pipeline->ts_t = time;
109 pipeline->ts_dt = delta_t;
110
111 pipeline->ksp_ctx = KspMethod::KSPContext::CTX_SETFUNCTION;
114
115 auto [v, a] = setPipelineX(pipeline, x, delta_x, delta2_x, delta_t);
116 std::ignore = a;
117 std::ignore = v;
118
120 DMoFEMMeshToLocalVector(dm, x, INSERT_VALUES, SCATTER_REVERSE),
121 "loc to vec");
122 CHK_THROW_MESSAGE(DMoFEMLoopFiniteElements(dm, fe_name, pipeline, cache_ptr),
123 "Run assemble pipeline");
124 CHK_THROW_MESSAGE(VecAssemblyBegin(f), "Asmb vector");
125 CHK_THROW_MESSAGE(VecAssemblyEnd(f), "Asmb vector");
126
127 CHK_THROW_MESSAGE(VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE),
128 "Update vec");
129 CHK_THROW_MESSAGE(VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE),
130 "Update vec");
131 CHK_THROW_MESSAGE(VecGhostUpdateBegin(f, INSERT_VALUES, SCATTER_FORWARD),
132 "Update vec");
133 CHK_THROW_MESSAGE(VecGhostUpdateEnd(f, INSERT_VALUES, SCATTER_FORWARD),
134 "Update vec");
135
136 auto get_norm = [](auto f) {
137 double fnorm;
138 VecNorm(f, NORM_2, &fnorm);
139 return fnorm;
140 };
141 MOFEM_LOG_CHANNEL("WORLD");
142 MOFEM_TAG_AND_LOG_C("WORLD", Sev::noisy, "OpTester", "Vec norm %3.4e",
143 get_norm(f));
144
145 return f;
146}
147
150 boost::shared_ptr<FEMethod> pipeline,
152 SmartPetscObj<Vec> delta2_x, double time,
153 double delta_t, CacheTupleWeakPtr cache_ptr) {
154 auto m = createDMMatrix(dm);
155
156 pipeline->data_ctx =
158 pipeline->A = m;
159 pipeline->B = m;
160
161 pipeline->ts_t = time;
162
163 pipeline->ts_dt = delta_t;
164 pipeline->ts_a = 1. / delta_t;
165 pipeline->ts_aa = 1. / pow(delta_t, 2);
166
167 pipeline->ksp_ctx = KspMethod::KSPContext::CTX_OPERATORS;
170
171 auto [v, a] = setPipelineX(pipeline, x, delta_x, delta2_x, delta_t);
172 std::ignore = a;
173 std::ignore = v;
174
176 DMoFEMMeshToLocalVector(dm, x, INSERT_VALUES, SCATTER_REVERSE),
177 "loc to vec");
178 CHK_THROW_MESSAGE(DMoFEMLoopFiniteElements(dm, fe_name, pipeline, cache_ptr),
179 "Run assemble pipeline");
180 CHK_THROW_MESSAGE(MatAssemblyBegin(m, MAT_FINAL_ASSEMBLY), "Asmb mat");
181 CHK_THROW_MESSAGE(MatAssemblyEnd(m, MAT_FINAL_ASSEMBLY), "Asmb mat");
182
183 auto get_norm = [](auto m) {
184 double fnorm;
185 MatNorm(m, NORM_1, &fnorm);
186 return fnorm;
187 };
188 MOFEM_LOG_CHANNEL("WORLD");
189 MOFEM_TAG_AND_LOG_C("WORLD", Sev::noisy, "OpTester", "Matrix norm %3.4e",
190 get_norm(m));
191
192 return m;
193}
194
196 SmartPetscObj<DM> dm, std::string fe_name,
197 boost::shared_ptr<FEMethod> pipeline, SmartPetscObj<Vec> x,
198 SmartPetscObj<Vec> delta_x, SmartPetscObj<Vec> delta2_x,
199 SmartPetscObj<Vec> diff_x, double time, double delta_t, double eps,
200 CacheTupleWeakPtr cache_ptr) {
201
202 auto axpy = [&](auto v0, auto diff_v, auto eps) {
204 if (v0.use_count()) {
205 v = vectorDuplicate(v0);
206 CHK_THROW_MESSAGE(VecCopy(v0, v), "Cpy");
207 CHK_THROW_MESSAGE(VecAXPY(v, eps, diff_v), "Add");
208 CHK_THROW_MESSAGE(VecGhostUpdateBegin(v, INSERT_VALUES, SCATTER_FORWARD),
209 "ghost");
210 CHK_THROW_MESSAGE(VecGhostUpdateEnd(v, INSERT_VALUES, SCATTER_FORWARD),
211 "ghost");
212 }
213 return v;
214 };
215
216 auto fm1 = assembleVec(dm, fe_name, pipeline,
217 //
218 axpy(x, diff_x, -eps),
219 //
220 axpy(delta_x, diff_x, -eps),
221 //
222 axpy(delta2_x, diff_x, -eps),
223 //
224 time, delta_t, cache_ptr);
225
226 auto fp1 = assembleVec(dm, fe_name, pipeline,
227 //
228 axpy(x, diff_x, eps),
229 //
230 axpy(delta_x, diff_x, eps),
231 //
232 axpy(delta2_x, diff_x, eps),
233 //
234 time, delta_t, cache_ptr);
235
236 auto calculate_derivative = [eps](auto fm1, auto fp1) {
237 int size;
238 VecGetLocalSize(fp1, &size);
239 VecAXPY(fp1, -1, fm1);
240 double *array;
241 VecGetArray(fp1, &array);
242 for (int j = 0; j != size; ++j) {
243 array[j] /= 2 * eps;
244 }
245 VecRestoreArray(fp1, &array);
246 return fp1;
247 };
248
249 return calculate_derivative(fm1, fp1);
250}
251
253 SmartPetscObj<DM> dm, std::string fe_name,
254 boost::shared_ptr<FEMethod> pipeline_rhs,
255 boost::shared_ptr<FEMethod> pipeline_lhs, SmartPetscObj<Vec> x,
256 SmartPetscObj<Vec> delta_x, SmartPetscObj<Vec> delta2_x,
257 SmartPetscObj<Vec> diff_x, double time, double delta_t, double eps,
258 CacheTupleWeakPtr cache_ptr) {
259
261 dm, fe_name, pipeline_rhs, x, delta_x, delta2_x, diff_x, time, delta_t,
262 eps, cache_ptr);
263
264 auto m = assembleMat(dm, fe_name, pipeline_lhs, x, delta_x, delta2_x, time,
265 delta_t, cache_ptr);
266
267 auto fm = vectorDuplicate(fd_diff);
268 CHK_THROW_MESSAGE(MatMult(m, diff_x, fm), "Mat mult");
269 CHK_THROW_MESSAGE(VecAXPY(fd_diff, -1., fm), "Add");
270
271 return fd_diff;
272}
273
274std::pair<SmartPetscObj<Vec>, SmartPetscObj<Vec>>
275OperatorsTester::setPipelineX(boost::shared_ptr<FEMethod> pipeline,
277 SmartPetscObj<Vec> delta2_x, double delta_t) {
278
279 // Set dofs vector to finite element instance
280 pipeline->x = x;
281 pipeline->data_ctx |= PetscData::CTX_SET_X;
282
283 SmartPetscObj<Vec> x_t, x_tt;
284
285 // Set velocity dofs vector to finite element instance
286
287 if (delta_x.use_count()) {
288 x_t = vectorDuplicate(x);
289 VecCopy(delta_x, x_t);
290 VecScale(x_t, 1. / delta_t);
291 CHK_THROW_MESSAGE(VecGhostUpdateBegin(x_t, INSERT_VALUES, SCATTER_FORWARD),
292 "ghost");
293 CHK_THROW_MESSAGE(VecGhostUpdateEnd(x_t, INSERT_VALUES, SCATTER_FORWARD),
294 "ghost");
295 pipeline->x_t = x_t;
296 pipeline->data_ctx |= PetscData::CTX_SET_X_T;
297 } else {
298 pipeline->x_t = PETSC_NULLPTR;
299 }
300
301 // Set acceleration dofs vector to finite element instance
302
303 if (delta2_x.use_count()) {
304 x_tt = vectorDuplicate(x);
305 VecCopy(delta2_x, x_tt);
306 VecScale(x_tt, 1. / pow(delta_t, 2));
307 CHK_THROW_MESSAGE(VecGhostUpdateBegin(x_tt, INSERT_VALUES, SCATTER_FORWARD),
308 "ghost");
309 CHK_THROW_MESSAGE(VecGhostUpdateEnd(x_tt, INSERT_VALUES, SCATTER_FORWARD),
310 "ghost");
311 pipeline->x_tt = x_tt;
312 pipeline->data_ctx |= PetscData::CTX_SET_X_TT;
313 } else {
314 pipeline->x_tt = PETSC_NULLPTR;
315 }
316
317 return std::make_pair(x_t, x_tt);
318}
319} // namespace MoFEM
#define MOFEM_TAG_AND_LOG_C(channel, severity, tag, format,...)
Tag and log in channel.
#define MOFEM_TAG_AND_LOG(channel, severity, tag)
Tag and log in channel.
constexpr double a
static const double eps
RowColData
RowColData.
@ COL
@ ROW
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
#define CHKERR
Inline error check.
PetscErrorCode DMoFEMMeshToLocalVector(DM dm, Vec l, InsertMode mode, ScatterMode scatter_mode, RowColData rc=RowColData::COL)
set local (or ghosted) vector values on mesh for partition only
Definition DMMoFEM.cpp:514
PetscErrorCode DMoFEMLoopFiniteElements(DM dm, const char fe_name[], MoFEM::FEMethod *method, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr())
Executes FEMethod for finite elements in DM.
Definition DMMoFEM.cpp:576
auto createDMVector(DM dm, RowColData rc=RowColData::COL)
Get smart vector from DM.
Definition DMMoFEM.hpp:1237
auto createDMMatrix(DM dm)
Get smart matrix from DM.
Definition DMMoFEM.hpp:1194
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'j', 3 > j
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
SmartPetscObj< Vec > vectorDuplicate(Vec vec)
Create duplicate vector of smart vector.
auto getProblemPtr(DM dm)
get problem pointer from DM
Definition DMMoFEM.hpp:1182
boost::weak_ptr< CacheTuple > CacheTupleWeakPtr
FTensor::Index< 'm', 3 > m
virtual FieldBitNumber get_field_bit_number(const std::string name) const =0
get field bit number
Core (interface) class.
Definition Core.hpp:83
Deprecated interface functions.
static UId getLoFieldEntityUId(const FieldBitNumber bit, const EntityHandle ent)
static UId getHiFieldEntityUId(const FieldBitNumber bit, const EntityHandle ent)
static UId getHiBitNumberUId(const FieldBitNumber bit_number)
static UId getLoBitNumberUId(const FieldBitNumber bit_number)
@ CTX_SETFUNCTION
Setting up the linear system function.
@ CTX_OPERATORS
Setting up linear operators.
Calculate directional derivative of the right hand side and compare it with tangent matrix derivative...
SmartPetscObj< Mat > assembleMat(SmartPetscObj< DM > dm, std::string fe_name, boost::shared_ptr< FEMethod > pipeline, SmartPetscObj< Vec > x, SmartPetscObj< Vec > delta_x, SmartPetscObj< Vec > delta2_x, double time, double delta_t, CacheTupleWeakPtr cache_ptr)
Assemble the left hand side vector.
SmartPetscObj< Vec > checkCentralFiniteDifference(SmartPetscObj< DM > dm, std::string fe_name, boost::shared_ptr< FEMethod > pipeline_rhs, boost::shared_ptr< FEMethod > pipeline_lhs, SmartPetscObj< Vec > x, SmartPetscObj< Vec > delta_x, SmartPetscObj< Vec > delta2_x, SmartPetscObj< Vec > diff_x, double time, double delta_t, double eps, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr())
Check consistency between directional derivative with matrix.
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
OperatorsTester(const MoFEM::Core &core)
SmartPetscObj< Vec > assembleVec(SmartPetscObj< DM > dm, std::string fe_name, boost::shared_ptr< FEMethod > pipeline, SmartPetscObj< Vec > x, SmartPetscObj< Vec > delta_x, SmartPetscObj< Vec > delta2_x, double time, double delta_t, CacheTupleWeakPtr cache_ptr)
Assemble the right hand side vector.
SmartPetscObj< Vec > setRandomFields(SmartPetscObj< DM > dm, std::vector< RandomFieldData > random_fields, boost::shared_ptr< Range > ents=nullptr, RowColData r=RowColData::COL)
Generate random fields.
std::pair< SmartPetscObj< Vec >, SmartPetscObj< Vec > > setPipelineX(boost::shared_ptr< FEMethod > pipeline, SmartPetscObj< Vec > x, SmartPetscObj< Vec > delta_x, SmartPetscObj< Vec > delta2_x, double delta_t)
Set vectors, x, x_t, and x_tt to finite element instance.
SmartPetscObj< Vec > directionalCentralFiniteDifference(SmartPetscObj< DM > dm, std::string fe_name, boost::shared_ptr< FEMethod > pipeline, SmartPetscObj< Vec > x, SmartPetscObj< Vec > delta_x, SmartPetscObj< Vec > delta2_x, SmartPetscObj< Vec > diff_x, double time, double delta_t, double eps, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr())
Calculate directional directive using finite difference.
static constexpr Switches CtxSetA
Jacobian matrix switch.
static constexpr Switches CtxSetF
Residual vector switch.
@ CTX_SET_X_T
Time derivative X_t is set.
@ CTX_SET_X
Solution vector X is set.
@ CTX_SET_X_TT
Second time derivative X_tt is set.
static constexpr Switches CtxSetB
Preconditioner matrix switch.
static constexpr Switches CtxSetTime
Time value switch.
intrusive_ptr for managing petsc objects
@ CTX_SNESSETJACOBIAN
Setting up Jacobian matrix computation.
@ CTX_SNESSETFUNCTION
Setting up nonlinear function evaluation.
@ CTX_TSSETIFUNCTION
Setting up implicit function.
@ CTX_TSSETIJACOBIAN
Setting up implicit Jacobian.
base class for all interface classes