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 Assemble the right hand side vector
60 *
61 * @param dm
62 * @param fe_name // fe name
63 * @param pipeline // pipeline, i.e. fe instance
64 * @param x // problem (dm) vector
65 * @param delta_x // vector for x rate, can be null, i.e, SmartPetscObj<Vec>()
66 * @param delta2_x // vector for x second rate, i.e. acceleration
67 * @param time // time
68 * @param delta_t // time increment
69 * @param cache_ptr // finite element data cache, can be null
70 * @return SmartPetscObj<Vec>
71 */
72 SmartPetscObj<Vec> assembleVec(SmartPetscObj<DM> dm, std::string fe_name,
73 boost::shared_ptr<FEMethod> pipeline,
75 SmartPetscObj<Vec> delta_x,
76 SmartPetscObj<Vec> delta2_x, double time,
77 double delta_t, CacheTupleWeakPtr cache_ptr);
78
79 /**
80 * @brief Assemble the left hand side vector
81 *
82 * @param dm
83 * @param fe_name // fe name
84 * @param pipeline // pipeline, i.e. fe instance
85 * @param x // problem (dm) vector
86 * @param delta_x // vector for x rate, can be null, i.e, SmartPetscObj<Vec>()
87 * @param delta2_x // vector for x second rate, i.e. acceleration
88 * @param time // time
89 * @param delta_t // time increment
90 * @param cache_ptr // finite element data cache, can be null
91 * @return SmartPetscObj<Vec>
92 */
93 SmartPetscObj<Mat> assembleMat(SmartPetscObj<DM> dm, std::string fe_name,
94 boost::shared_ptr<FEMethod> pipeline,
96 SmartPetscObj<Vec> delta_x,
97 SmartPetscObj<Vec> delta2_x, double time,
98 double delta_t, CacheTupleWeakPtr cache_ptr);
99
100 /**
101 * @brief Calculate directional directive using finite difference
102 *
103 * @param dm
104 * @param fe_name
105 * @param pipeline
106 * @param x
107 * @param delta_x
108 * @param delta2_x
109 * @param diff_x // direction of derivative
110 * @param time
111 * @param delta_t
112 * @param eps
113 * @param cache_ptr
114 * @return SmartPetscObj<Vec>
115 */
117 SmartPetscObj<DM> dm, std::string fe_name,
118 boost::shared_ptr<FEMethod> pipeline, SmartPetscObj<Vec> x,
119 SmartPetscObj<Vec> delta_x, SmartPetscObj<Vec> delta2_x,
120 SmartPetscObj<Vec> diff_x, double time, double delta_t, double eps,
122
123 /**
124 * @brief Check consistency between directional derivative with matrix
125 *
126 * @param dm
127 * @param fe_name
128 * @param pipeline_rhs
129 * @param pipeline_lhs
130 * @param x
131 * @param delta_x
132 * @param delta2_x
133 * @param diff_x
134 * @param time
135 * @param delta_t
136 * @param eps
137 * @param cache_ptr
138 * @return SmartPetscObj<Vec>
139 */
141 SmartPetscObj<DM> dm, std::string fe_name,
142 boost::shared_ptr<FEMethod> pipeline_rhs,
143 boost::shared_ptr<FEMethod> pipeline_lhs, SmartPetscObj<Vec> x,
144 SmartPetscObj<Vec> delta_x, SmartPetscObj<Vec> delta2_x,
145 SmartPetscObj<Vec> diff_x, double time, double delta_t, double eps,
147
148 /** Check a scalar function derivative against gradient dot direction. */
150 Vec x, Vec direction, Vec gradient, double eps,
151 const ScalarFunction &evaluate, PetscReal &finite_difference,
152 PetscReal &assembled_derivative);
153
154 /**
155 * Check a vector function derivative against Jacobian times direction.
156 * The returned error is written into a vector with the function layout.
157 */
159 Vec x, Vec direction, Vec function_layout, Mat jacobian, double eps,
160 const VectorFunction &evaluate, Vec error);
161
162private:
164
165 /**
166 * @brief Set vectors, x, x_t, and x_tt to finite element instance
167 *
168 * Finite element instance is a pipeline. x_t and x_tt are evaluated for given
169 * delta_x, delta2_x and delta_t.
170 *
171 * @param pipeline
172 * @param x
173 * @param delta_x
174 * @param delta2_x
175 * @param delta_t
176 * @return std::pair<SmartPetscObj<Vec>, SmartPetscObj<Vec>>
177 */
178 std::pair<SmartPetscObj<Vec>, SmartPetscObj<Vec>>
179 setPipelineX(boost::shared_ptr<FEMethod> pipeline, SmartPetscObj<Vec> x,
180 SmartPetscObj<Vec> delta_x, SmartPetscObj<Vec> delta2_x,
181 double delta_t);
182};
183
184} // namespace MoFEM
185
186#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
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
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.
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