v0.16.3
Loading...
Searching...
No Matches
OperatorsTester.hpp
Go to the documentation of this file.
1/**
2 * @file OperatorsTester.hpp
3 * @brief Used to calculate derivatives and testing tangent matrix, i.e. hessian.
4 * @version 0.13.2
5 * @date 2022-12-04
6 *
7 * @copyright Copyright (c) 2022
8 *
9 */
10
11#ifndef __OPERATORS_TESTER_HPP__
12#define __OPERATORS_TESTER_HPP__
13
14namespace MoFEM {
15
16/**
17 * @brief Calculate directional derivative of the right hand side and compare it
18 * with tangent matrix derivative.
19 *
20 */
22
23 MoFEMErrorCode query_interface(boost::typeindex::type_index type_index,
24 UnknownInterface **iface) const;
25
26 OperatorsTester(const MoFEM::Core &core);
27 virtual ~OperatorsTester() = default;
28
29 using RandomFieldData = std::pair<std::string, std::array<double, 2>>;
31 std::function<MoFEMErrorCode(Vec, PetscReal &)>;
32 using VectorFunction = std::function<MoFEMErrorCode(Vec, Vec)>;
33
34 /**
35 * @brief Generate random fields
36 *
37 * Example: generate random vector for DM (problem) from simple interface,
38 where FIELD random values of DOFs are in range from -1 to 1, and FIELD2
39 random values are in range from 0 to 1.
40 * @code {.cpp}
41 * auto x = opt->setRandomFields(simple->getDM(),
42 {{"FIELD1", {-1, 1}}, {"FIELD2", {0,1}}});
43 * @endcode
44 *
45 *
46 * TODO: Set random field to specific entities, and potentially order for
47 testing proposes to dissect error in tangent matrix.
48 *
49 * @param dm
50 * @param random_fields look at definition @ref RandomFieldData
51 * @return SmartPetscObj<Vec> smart vector
52 */
54 std::vector<RandomFieldData> random_fields,
55 boost::shared_ptr<Range> ents = nullptr,
57
58 /**
59 * @brief Value of a test direction for one DOF
60 *
61 * The DOF gives the entity, field, coefficient index and order; xyz is the
62 * centroid of the entity's corner vertices (zero for meshsets).
63 */
64 using DirectionFunction = std::function<double(
65 const NumeredDofEntity &dof, const std::array<double, 3> &xyz)>;
66
67 /**
68 * @brief Set a direction vector from a function of the DOF and position
69 *
70 * Unlike setRandomFields, which draws values in local DOF order, the value
71 * of each DOF depends only on the function, the DOF and the mesh. The
72 * vector is therefore the same for any number of ranks and any DOF
73 * numbering, so derivative checks on one and several ranks test the same
74 * direction. DOFs of other fields, or outside ents when given, are zero.
75 *
76 * @code {.cpp}
77 * auto d = opt->setDirectionFromFunction(
78 * dm, {"FIELD"}, [](const NumeredDofEntity &dof, const auto &xyz) {
79 * return std::sin(xyz[0] + dof.getDofCoeffIdx());
80 * });
81 * @endcode
82 *
83 * @param dm
84 * @param fields fields set by the function
85 * @param function value for each DOF, see @ref DirectionFunction
86 * @param ents restrict the direction to these entities, null for all
87 * @param r row or column layout
88 * @return SmartPetscObj<Vec> vector with ghost values updated
89 */
92 const std::vector<std::string> &fields,
93 DirectionFunction function,
94 boost::shared_ptr<Range> ents = nullptr,
96
97 /**
98 * @brief Assemble the right hand side vector
99 *
100 * @param dm
101 * @param fe_name // fe name
102 * @param pipeline // pipeline, i.e. fe instance
103 * @param x // problem (dm) vector
104 * @param delta_x // vector for x rate, can be null, i.e, SmartPetscObj<Vec>()
105 * @param delta2_x // vector for x second rate, i.e. acceleration
106 * @param time // time
107 * @param delta_t // time increment
108 * @param cache_ptr // finite element data cache, can be null
109 * @return SmartPetscObj<Vec>
110 */
111 SmartPetscObj<Vec> assembleVec(SmartPetscObj<DM> dm, std::string fe_name,
112 boost::shared_ptr<FEMethod> pipeline,
114 SmartPetscObj<Vec> delta_x,
115 SmartPetscObj<Vec> delta2_x, double time,
116 double delta_t, CacheTupleWeakPtr cache_ptr);
117
118 /**
119 * @brief Assemble the left hand side vector
120 *
121 * @param dm
122 * @param fe_name // fe name
123 * @param pipeline // pipeline, i.e. fe instance
124 * @param x // problem (dm) vector
125 * @param delta_x // vector for x rate, can be null, i.e, SmartPetscObj<Vec>()
126 * @param delta2_x // vector for x second rate, i.e. acceleration
127 * @param time // time
128 * @param delta_t // time increment
129 * @param cache_ptr // finite element data cache, can be null
130 * @return SmartPetscObj<Vec>
131 */
132 SmartPetscObj<Mat> assembleMat(SmartPetscObj<DM> dm, std::string fe_name,
133 boost::shared_ptr<FEMethod> pipeline,
135 SmartPetscObj<Vec> delta_x,
136 SmartPetscObj<Vec> delta2_x, double time,
137 double delta_t, CacheTupleWeakPtr cache_ptr);
138
139 /**
140 * @brief Calculate directional directive using finite difference
141 *
142 * @param dm
143 * @param fe_name
144 * @param pipeline
145 * @param x
146 * @param delta_x
147 * @param delta2_x
148 * @param diff_x // direction of derivative
149 * @param time
150 * @param delta_t
151 * @param eps
152 * @param cache_ptr
153 * @return SmartPetscObj<Vec>
154 */
156 SmartPetscObj<DM> dm, std::string fe_name,
157 boost::shared_ptr<FEMethod> pipeline, SmartPetscObj<Vec> x,
158 SmartPetscObj<Vec> delta_x, SmartPetscObj<Vec> delta2_x,
159 SmartPetscObj<Vec> diff_x, double time, double delta_t, double eps,
161
162 /**
163 * @brief Check consistency between directional derivative with matrix
164 *
165 * @param dm
166 * @param fe_name
167 * @param pipeline_rhs
168 * @param pipeline_lhs
169 * @param x
170 * @param delta_x
171 * @param delta2_x
172 * @param diff_x
173 * @param time
174 * @param delta_t
175 * @param eps
176 * @param cache_ptr
177 * @return SmartPetscObj<Vec>
178 */
180 SmartPetscObj<DM> dm, std::string fe_name,
181 boost::shared_ptr<FEMethod> pipeline_rhs,
182 boost::shared_ptr<FEMethod> pipeline_lhs, SmartPetscObj<Vec> x,
183 SmartPetscObj<Vec> delta_x, SmartPetscObj<Vec> delta2_x,
184 SmartPetscObj<Vec> diff_x, double time, double delta_t, double eps,
186
187 /** Check a scalar function derivative against gradient dot direction. */
189 Vec x, Vec direction, Vec gradient, double eps,
190 const ScalarFunction &evaluate, PetscReal &finite_difference,
191 PetscReal &assembled_derivative);
192
193 /**
194 * Check a vector function derivative against Jacobian times direction.
195 * The returned error is written into a vector with the function layout.
196 */
198 Vec x, Vec direction, Vec function_layout, Mat jacobian, double eps,
199 const VectorFunction &evaluate, Vec error);
200
201private:
203
204 /**
205 * @brief Set vectors, x, x_t, and x_tt to finite element instance
206 *
207 * Finite element instance is a pipeline. x_t and x_tt are evaluated for given
208 * delta_x, delta2_x and delta_t.
209 *
210 * @param pipeline
211 * @param x
212 * @param delta_x
213 * @param delta2_x
214 * @param delta_t
215 * @return std::pair<SmartPetscObj<Vec>, SmartPetscObj<Vec>>
216 */
217 std::pair<SmartPetscObj<Vec>, SmartPetscObj<Vec>>
218 setPipelineX(boost::shared_ptr<FEMethod> pipeline, SmartPetscObj<Vec> x,
219 SmartPetscObj<Vec> delta_x, SmartPetscObj<Vec> delta2_x,
220 double delta_t);
221};
222
223} // namespace MoFEM
224
225#endif //__OPERATORS_TESTER_HPP__
SmartPetscObj< Vec > gradient
SmartPetscObj< Mat > jacobian
static const double eps
RowColData
RowColData.
@ COL
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
boost::shared_ptr< CacheTuple > CacheTupleSharedPtr
boost::weak_ptr< CacheTuple > CacheTupleWeakPtr
Core (interface) class.
Definition Core.hpp:83
keeps information about indexed dofs for the problem
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.
std::pair< std::string, std::array< double, 2 > > RandomFieldData
MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const
MoFEMErrorCode checkScalarCentralFiniteDifference(Vec x, Vec direction, Vec gradient, double eps, const ScalarFunction &evaluate, PetscReal &finite_difference, PetscReal &assembled_derivative)
virtual ~OperatorsTester()=default
std::function< MoFEMErrorCode(Vec, PetscReal &)> ScalarFunction
std::function< double(const NumeredDofEntity &dof, const std::array< double, 3 > &xyz)> DirectionFunction
Value of a test direction for one DOF.
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 > setDirectionFromFunction(SmartPetscObj< DM > dm, const std::vector< std::string > &fields, DirectionFunction function, boost::shared_ptr< Range > ents=nullptr, RowColData r=RowColData::COL)
Set a direction vector from a function of the DOF and position.
std::function< MoFEMErrorCode(Vec, Vec)> VectorFunction
MoFEMErrorCode checkVectorCentralFiniteDifference(Vec x, Vec direction, Vec function_layout, Mat jacobian, double eps, const VectorFunction &evaluate, Vec error)
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.
intrusive_ptr for managing petsc objects
base class for all interface classes