v0.16.3
Loading...
Searching...
No Matches
TaoCtx.hpp
Go to the documentation of this file.
1/** \file TaoCtx.hpp
2 * \brief Context for PETSc TAO, i.e. optimization solver
3 * \author Anonymous author(s) committing under MIT license
4 */
5
6#ifndef __TAOTX_HPP__
7#define __TAOTX_HPP__
8
9namespace MoFEM {
10
11/** \brief Interface for TAO solvers
12 * \ingroup mofem_petsc_solvers
13 */
14struct TaoCtx {
15
16 MoFEM::Interface &mField; ///< database Interface
17 moab::Interface &moab; ///< moab Interface
18 std::string problemName; ///< problem name
19 bool vErify; ///< If true verify vector
20
25 boost::function<MoFEMErrorCode(Tao tao, Vec x, PetscReal *objective)>;
27 boost::function<MoFEMErrorCode(Tao tao, Vec x, Vec gradient)>;
29 Tao tao, Vec x, PetscReal *objective, Vec gradient)>;
30
32 loopsHessian; ///< Sequence of finite element methods for assembling the
33 ///< Hessian (second derivative) matrix in TAO optimization
35 loopsObjective; ///< Sequence of finite element methods for evaluating the
36 ///< objective function in TAO optimization
38 loopsGradient; ///< Sequence of finite element methods for assembling the
39 ///< gradient (first derivative) vector in TAO optimization
40 FEMethodsSequence loopsEqualityConstraint; ///< Sequence of finite element
41 ///< methods for evaluating
42 ///< equality constraints
43 FEMethodsSequence loopsInequalityConstraint; ///< Sequence of finite element
44 ///< methods for evaluating
45 ///< inequality constraints
46 FEMethodsSequence loopsEqualityJacobian; ///< Sequence of finite element
47 ///< methods for assembling the
48 ///< equality constraint Jacobian
49 FEMethodsSequence loopsInequalityJacobian; ///< Sequence of finite element
50 ///< methods for assembling the
51 ///< inequality constraint Jacobian
52
54 preHessian; ///< Sequence of methods run before Hessian is assembled
56 postHessian; ///< Sequence of methods run after Hessian is assembled
57
59 preObjective; ///< Sequence of methods run before objective is evaluated
61 postObjective; ///< Sequence of methods run after objective is evaluated
62
64 preGradient; ///< Sequence of methods run before gradient is assembled
66 postGradient; ///< Sequence of methods run after gradient is assembled
67
68 BasicMethodsSequence preEqualityConstraint; ///< Sequence of methods run
69 ///< before equality constraints
70 ///< are evaluated
71 BasicMethodsSequence postEqualityConstraint; ///< Sequence of methods run
72 ///< after equality constraints
73 ///< are evaluated
74 BasicMethodsSequence preInequalityConstraint; ///< Sequence of methods run
75 ///< before inequality
76 ///< constraints are evaluated
77 BasicMethodsSequence postInequalityConstraint; ///< Sequence of methods run
78 ///< after inequality
79 ///< constraints are evaluated
80 BasicMethodsSequence preEqualityJacobian; ///< Sequence of methods run before
81 ///< the equality Jacobian is
82 ///< assembled
83 BasicMethodsSequence postEqualityJacobian; ///< Sequence of methods run after
84 ///< the equality Jacobian is
85 ///< assembled
86 BasicMethodsSequence preInequalityJacobian; ///< Sequence of methods run
87 ///< before the inequality
88 ///< Jacobian is assembled
89 BasicMethodsSequence postInequalityJacobian; ///< Sequence of methods run
90 ///< after the inequality
91 ///< Jacobian is assembled
92
93 TaoCtx(Interface &m_field, const std::string &problem_name)
94 : mField(m_field), moab(m_field.get_moab()), problemName(problem_name),
95 vErify(false) {
96 PetscLogEventRegister("LoopTAOHessian", 0, &MOFEM_EVENT_TaoHessian);
97 PetscLogEventRegister("LoopTAOObjective", 0, &MOFEM_EVENT_TaoObjective);
98 PetscLogEventRegister("LoopTAOGradient", 0, &MOFEM_EVENT_TaoGradient);
99 PetscLogEventRegister("LoopTAOEqualityConstraint", 0,
101 PetscLogEventRegister("LoopTAOInequalityConstraint", 0,
103 PetscLogEventRegister("LoopTAOEqualityJacobian", 0,
105 PetscLogEventRegister("LoopTAOInequalityJacobian", 0,
107 if (!LogManager::checkIfChannelExist("TAO_WORLD")) {
108 auto core_log = logging::core::get();
109 core_log->add_sink(
111 LogManager::setLog("TAO_WORLD");
112 MOFEM_LOG_TAG("TAO_WORLD", "TAO");
113 }
114 }
115
116 virtual ~TaoCtx() = default;
117
118 // TAO Hessian (second derivative) matrix assembly loop and pre/post sequences
122
123 // TAO Objective function evaluation loop and pre/post sequences
127
128 // TAO Gradient (first derivative) vector assembly loop and pre/post sequences
132
133 // TAO equality constraint evaluation loop and pre/post sequences
143
144 // TAO inequality constraint evaluation loop and pre/post sequences
154
155 // TAO equality constraint Jacobian loop and pre/post sequences
165
166 // TAO inequality constraint Jacobian loop and pre/post sequences
176
177 /**
178 * @brief Get an optional objective evaluator.
179 *
180 * When set, MoFEM::TaoSetObjective delegates the complete evaluation to this
181 * hook before scattering controls to the mesh or running element loops.
182 * The hook owns state synchronization and objective evaluation.
183 */
185
186 /**
187 * @brief Get an optional gradient evaluator.
188 *
189 * When set, MoFEM::TaoSetGradient delegates the complete evaluation to this
190 * hook before scattering controls to the mesh or running element loops.
191 * The hook owns state synchronization and gradient assembly.
192 */
194
195 /**
196 * @brief Get an optional combined objective-and-gradient evaluator.
197 *
198 * When set, MoFEM::TaoSetObjectiveAndGradient delegates the complete
199 * evaluation to this hook. This supports reduced objectives which require
200 * an application-owned state solve before returning the value and gradient.
201 * Otherwise, the separate objective and gradient callbacks are used.
202 */
206
207 /**
208 * \brief Copy sequences from another TaoCtx
209 *
210 * Evaluator hooks are unchanged.
211 * @param tao_ctx TaoCtx from which sequences are copied
212 * @return error code
213 */
214 MoFEMErrorCode copyLoops(const TaoCtx &tao_ctx);
215
216 /**
217 * @brief Clear loops
218 *
219 * Evaluator hooks are unchanged.
220 *
221 * @return MoFEMErrorCode
222 */
224
225 friend PetscErrorCode TaoSetObjective(Tao tao, Vec x, PetscReal *f,
226 void *ctx);
227 friend PetscErrorCode TaoSetGradient(Tao tao, Vec x, Vec g, void *ctx);
228 friend PetscErrorCode TaoSetObjectiveAndGradient(Tao tao, Vec x, PetscReal *f,
229 Vec g, void *ctx);
230 friend PetscErrorCode TaoSetHessian(Tao tao, Vec x, Mat H, Mat Hpre,
231 void *ctx);
232 friend PetscErrorCode TaoSetEqualityConstraints(Tao tao, Vec x, Vec c,
233 void *ctx);
234 friend PetscErrorCode TaoSetInequalityConstraints(Tao tao, Vec x, Vec c,
235 void *ctx);
236 friend PetscErrorCode TaoSetJacobianEquality(Tao tao, Vec x, Mat J, Mat Jpre,
237 void *ctx);
238 friend PetscErrorCode TaoSetJacobianInequality(Tao tao, Vec x, Mat J,
239 Mat Jpre, void *ctx);
240
241private:
242 PetscErrorCode evaluateConstraint(Tao tao, Vec x, Vec c,
243 FEMethodsSequence &loops,
244 BasicMethodsSequence &pre_process,
245 BasicMethodsSequence &post_process,
246 TaoMethod::TAOContext evaluation_context,
247 PetscLogEvent log_event);
248 PetscErrorCode evaluateConstraintJacobian(
249 Tao tao, Vec x, Mat J, Mat Jpre, FEMethodsSequence &loops,
250 BasicMethodsSequence &pre_process, BasicMethodsSequence &post_process,
251 TaoMethod::TAOContext evaluation_context, PetscLogEvent log_event);
252
256 boost::movelib::unique_ptr<bool> vecAssembleSwitch;
257 boost::movelib::unique_ptr<bool> matAssembleSwitch;
258 PetscLogEvent MOFEM_EVENT_TaoHessian; ///< Log event for TAO Hessian
259 PetscLogEvent MOFEM_EVENT_TaoGradient; ///< Log event for TAO Gradient
260 PetscLogEvent MOFEM_EVENT_TaoObjective; ///< Log event for TAO Objective
265};
266
267/**
268 * @brief Sets the objective function value for a TAO optimization context.
269 *
270 * @param tao The TAO optimization solver context.
271 * @param x The input vector at which to evaluate the objective.
272 * @param f Pointer to store the computed objective value.
273 * @param ctx User-defined context for the objective function.
274 *
275 * @return PetscErrorCode Error code (0 on success).
276 */
277PetscErrorCode TaoSetObjective(Tao tao, Vec x, PetscReal *f, void *ctx);
278
279/**
280 * @brief Sets the gradient vector for a TAO optimization context.
281 *
282 * @param tao The TAO optimization solver context.
283 * @param x The input vector at which to evaluate the gradient.
284 * @param g Pointer to the gradient vector.
285 * @param ctx User-defined context for the gradient function.
286 *
287 * @return PetscErrorCode Error code (0 on success).
288 */
289PetscErrorCode TaoSetGradient(Tao tao, Vec x, Vec g, void *ctx);
290
291/**
292 * @brief Sets the objective function value and gradient for a TAO optimization
293 * solver.
294 *
295 * @param tao The TAO solver context.
296 * @param x Input vector at which to evaluate the objective and gradient.
297 * @param f Pointer to store the computed objective function value.
298 * @param g Vector to store the computed gradient.
299 * @param ctx User-defined context for objective and gradient evaluation.
300 *
301 * @return PetscErrorCode Error code (0 on success).
302 *
303 * @note This function is typically called within a user-defined routine to
304 * provide both the objective value and its gradient for optimization
305 * algorithms.
306 *
307 * @see
308 * https://petsc.org/release/docs/manualpages/Tao/TaoSetObjectiveAndGradient.html
309 */
310PetscErrorCode TaoSetObjectiveAndGradient(Tao tao, Vec x, PetscReal *f, Vec g,
311 void *ctx);
312
313/**
314 * @brief Sets the Hessian matrix for a TAO optimization context.
315 *
316 * @param tao The TAO optimization solver context.
317 * @param x The input vector at which to evaluate the Hessian.
318 * @param H Pointer to the Hessian matrix.
319 * @param Hpre Preconditioner matrix (can be NULL).
320 * @param ctx User-defined context for the Hessian function.
321 *
322 * @return PetscErrorCode Error code (0 on success).
323 */
324PetscErrorCode TaoSetHessian(Tao tao, Vec x, Mat H, Mat Hpre, void *ctx);
325
326/**
327 * @brief Evaluate equality constraints for a TAO optimization context.
328 *
329 * Register this callback with TaoSetEqualityConstraintsRoutine().
330 */
331PetscErrorCode TaoSetEqualityConstraints(Tao tao, Vec x, Vec c, void *ctx);
332
333/**
334 * @brief Evaluate inequality constraints for a TAO optimization context.
335 *
336 * Register this callback with TaoSetInequalityConstraintsRoutine(). PETSc
337 * defines feasible inequality constraints by c(x) >= 0.
338 */
339PetscErrorCode TaoSetInequalityConstraints(Tao tao, Vec x, Vec c, void *ctx);
340
341/**
342 * @brief Assemble the equality constraint Jacobian.
343 *
344 * Register this callback with TaoSetJacobianEqualityRoutine().
345 */
346PetscErrorCode TaoSetJacobianEquality(Tao tao, Vec x, Mat J, Mat Jpre,
347 void *ctx);
348
349/**
350 * @brief Assemble the inequality constraint Jacobian.
351 *
352 * Register this callback with TaoSetJacobianInequalityRoutine().
353 */
354PetscErrorCode TaoSetJacobianInequality(Tao tao, Vec x, Mat J, Mat Jpre,
355 void *ctx);
356
357} // namespace MoFEM
358
359#endif // __TAOTX_HPP__
SmartPetscObj< Vec > gradient
static LoggerType & setLog(const std::string channel)
Set ans resset chanel logger.
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
const double c
speed of light (cm/ns)
FTensor::Index< 'J', DIM1 > J
Definition level_set.cpp:30
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
PetscErrorCode TaoSetObjective(Tao tao, Vec x, PetscReal *f, void *ctx)
Sets the objective function value for a TAO optimization context.
Definition TaoCtx.cpp:237
PetscErrorCode TaoSetJacobianInequality(Tao tao, Vec x, Mat J, Mat Jpre, void *ctx)
Assemble the inequality constraint Jacobian.
Definition TaoCtx.cpp:502
PetscErrorCode TaoSetEqualityConstraints(Tao tao, Vec x, Vec c, void *ctx)
Evaluate equality constraints for a TAO optimization context.
Definition TaoCtx.cpp:468
PetscErrorCode TaoSetInequalityConstraints(Tao tao, Vec x, Vec c, void *ctx)
Evaluate inequality constraints for a TAO optimization context.
Definition TaoCtx.cpp:479
std::deque< BasicMethodPtr > BasicMethodsSequence
Definition AuxPETSc.hpp:58
PetscErrorCode TaoSetJacobianEquality(Tao tao, Vec x, Mat J, Mat Jpre, void *ctx)
Assemble the equality constraint Jacobian.
Definition TaoCtx.cpp:490
std::deque< PairNameFEMethodPtr > FEMethodsSequence
Definition AuxPETSc.hpp:57
PetscErrorCode TaoSetObjectiveAndGradient(Tao tao, Vec x, PetscReal *f, Vec g, void *ctx)
Sets the objective function value and gradient for a TAO optimization solver.
Definition TaoCtx.cpp:386
PetscErrorCode TaoSetGradient(Tao tao, Vec x, Vec f, void *ctx)
Sets the gradient vector for a TAO optimization context.
Definition TaoCtx.cpp:296
PetscErrorCode TaoSetHessian(Tao tao, Vec x, Mat H, Mat Hpre, void *ctx)
Sets the Hessian matrix for a TAO optimization context.
Definition TaoCtx.cpp:399
double H
Hardening.
Definition plastic.cpp:129
constexpr double g
Deprecated interface functions.
static boost::shared_ptr< SinkType > createSink(boost::shared_ptr< std::ostream > stream_ptr, std::string comm_filter)
Create a sink object.
static boost::shared_ptr< std::ostream > getStrmWorld()
Get the strm world object.
static bool checkIfChannelExist(const std::string channel)
Check if channel exist.
Interface for TAO solvers.
Definition TaoCtx.hpp:14
TaoCtx(Interface &m_field, const std::string &problem_name)
Definition TaoCtx.hpp:93
MoFEMErrorCode copyLoops(const TaoCtx &tao_ctx)
Copy sequences from another TaoCtx.
Definition TaoCtx.cpp:41
FEMethodsSequence loopsInequalityJacobian
Definition TaoCtx.hpp:49
boost::movelib::unique_ptr< bool > vecAssembleSwitch
Definition TaoCtx.hpp:256
FEMethodsSequence & getInequalityJacobianLoops()
Definition TaoCtx.hpp:167
MoFEM::FEMethodsSequence FEMethodsSequence
Definition TaoCtx.hpp:22
BasicMethodsSequence & getPreProcEqualityConstraint()
Definition TaoCtx.hpp:137
MoFEM::Interface & mField
database Interface
Definition TaoCtx.hpp:16
moab::Interface & moab
moab Interface
Definition TaoCtx.hpp:17
FEMethodsSequence & getEqualityConstraintLoops()
Definition TaoCtx.hpp:134
virtual ~TaoCtx()=default
BasicMethodsSequence postInequalityConstraint
Definition TaoCtx.hpp:77
FEMethodsSequence & getGradientLoops()
Definition TaoCtx.hpp:129
PetscErrorCode evaluateConstraint(Tao tao, Vec x, Vec c, FEMethodsSequence &loops, BasicMethodsSequence &pre_process, BasicMethodsSequence &post_process, TaoMethod::TAOContext evaluation_context, PetscLogEvent log_event)
Definition TaoCtx.cpp:93
FEMethodsSequence & getInequalityConstraintLoops()
Definition TaoCtx.hpp:145
BasicMethodsSequence preEqualityJacobian
Definition TaoCtx.hpp:80
BasicMethodsSequence postObjective
Sequence of methods run after objective is evaluated.
Definition TaoCtx.hpp:61
BasicMethodsSequence & getPreProcGradient()
Definition TaoCtx.hpp:130
PetscLogEvent MOFEM_EVENT_TaoGradient
Log event for TAO Gradient.
Definition TaoCtx.hpp:259
BasicMethodsSequence preEqualityConstraint
Definition TaoCtx.hpp:68
friend PetscErrorCode TaoSetEqualityConstraints(Tao tao, Vec x, Vec c, void *ctx)
Evaluate equality constraints for a TAO optimization context.
Definition TaoCtx.cpp:468
friend PetscErrorCode TaoSetJacobianEquality(Tao tao, Vec x, Mat J, Mat Jpre, void *ctx)
Assemble the equality constraint Jacobian.
Definition TaoCtx.cpp:490
std::string problemName
problem name
Definition TaoCtx.hpp:18
BasicMethodsSequence & getPostProcGradient()
Definition TaoCtx.hpp:131
BasicMethodsSequence preInequalityConstraint
Definition TaoCtx.hpp:74
PetscLogEvent MOFEM_EVENT_TaoInequalityJacobian
Definition TaoCtx.hpp:264
BasicMethodsSequence & getPreProcEqualityJacobian()
Definition TaoCtx.hpp:159
BasicMethodsSequence & getPostProcObjective()
Definition TaoCtx.hpp:126
BasicMethodsSequence postEqualityConstraint
Definition TaoCtx.hpp:71
BasicMethodsSequence postGradient
Sequence of methods run after gradient is assembled.
Definition TaoCtx.hpp:66
PetscLogEvent MOFEM_EVENT_TaoHessian
Log event for TAO Hessian.
Definition TaoCtx.hpp:258
PetscErrorCode evaluateConstraintJacobian(Tao tao, Vec x, Mat J, Mat Jpre, FEMethodsSequence &loops, BasicMethodsSequence &pre_process, BasicMethodsSequence &post_process, TaoMethod::TAOContext evaluation_context, PetscLogEvent log_event)
Definition TaoCtx.cpp:160
BasicMethodsSequence & getPostProcInequalityConstraint()
Definition TaoCtx.hpp:151
boost::movelib::unique_ptr< bool > matAssembleSwitch
Definition TaoCtx.hpp:257
FEMethodsSequence loopsInequalityConstraint
Definition TaoCtx.hpp:43
FEMethodsSequence & getEqualityJacobianLoops()
Definition TaoCtx.hpp:156
BasicMethodsSequence preObjective
Sequence of methods run before objective is evaluated.
Definition TaoCtx.hpp:59
ObjectiveHook & getObjectiveHook()
Get an optional objective evaluator.
Definition TaoCtx.hpp:184
friend PetscErrorCode TaoSetHessian(Tao tao, Vec x, Mat H, Mat Hpre, void *ctx)
Sets the Hessian matrix for a TAO optimization context.
Definition TaoCtx.cpp:399
FEMethodsSequence loopsObjective
Definition TaoCtx.hpp:35
BasicMethodsSequence preGradient
Sequence of methods run before gradient is assembled.
Definition TaoCtx.hpp:64
GradientHook gradientHook
Definition TaoCtx.hpp:254
BasicMethodsSequence & getPostProcHessian()
Definition TaoCtx.hpp:121
PetscLogEvent MOFEM_EVENT_TaoEqualityJacobian
Definition TaoCtx.hpp:263
BasicMethodsSequence postEqualityJacobian
Definition TaoCtx.hpp:83
BasicMethodsSequence & getPostProcInequalityJacobian()
Definition TaoCtx.hpp:173
PetscLogEvent MOFEM_EVENT_TaoObjective
Log event for TAO Objective.
Definition TaoCtx.hpp:260
FEMethodsSequence loopsEqualityConstraint
Definition TaoCtx.hpp:40
BasicMethodsSequence & getPreProcInequalityJacobian()
Definition TaoCtx.hpp:170
PetscLogEvent MOFEM_EVENT_TaoInequalityConstraint
Definition TaoCtx.hpp:262
FEMethodsSequence & getObjectiveLoops()
Definition TaoCtx.hpp:124
friend PetscErrorCode TaoSetObjectiveAndGradient(Tao tao, Vec x, PetscReal *f, Vec g, void *ctx)
Sets the objective function value and gradient for a TAO optimization solver.
Definition TaoCtx.cpp:386
FEMethodsSequence & getHessianLoops()
Definition TaoCtx.hpp:119
ObjectiveAndGradientHook objectiveAndGradientHook
Definition TaoCtx.hpp:255
MoFEMErrorCode clearLoops()
Clear loops.
Definition TaoCtx.cpp:67
boost::function< MoFEMErrorCode(Tao tao, Vec x, PetscReal *objective)> ObjectiveHook
Definition TaoCtx.hpp:25
GradientHook & getGradientHook()
Get an optional gradient evaluator.
Definition TaoCtx.hpp:193
BasicMethodsSequence preInequalityJacobian
Definition TaoCtx.hpp:86
BasicMethodsSequence & getPostProcEqualityConstraint()
Definition TaoCtx.hpp:140
bool vErify
If true verify vector.
Definition TaoCtx.hpp:19
friend PetscErrorCode TaoSetObjective(Tao tao, Vec x, PetscReal *f, void *ctx)
Sets the objective function value for a TAO optimization context.
Definition TaoCtx.cpp:237
BasicMethodsSequence postHessian
Sequence of methods run after Hessian is assembled.
Definition TaoCtx.hpp:56
ObjectiveHook objectiveHook
Definition TaoCtx.hpp:253
BasicMethodsSequence & getPostProcEqualityJacobian()
Definition TaoCtx.hpp:162
BasicMethodsSequence preHessian
Sequence of methods run before Hessian is assembled.
Definition TaoCtx.hpp:54
friend PetscErrorCode TaoSetGradient(Tao tao, Vec x, Vec g, void *ctx)
Sets the gradient vector for a TAO optimization context.
Definition TaoCtx.cpp:296
FEMethodsSequence loopsEqualityJacobian
Definition TaoCtx.hpp:46
BasicMethodsSequence postInequalityJacobian
Definition TaoCtx.hpp:89
boost::function< MoFEMErrorCode(Tao tao, Vec x, PetscReal *objective, Vec gradient)> ObjectiveAndGradientHook
Definition TaoCtx.hpp:29
PetscLogEvent MOFEM_EVENT_TaoEqualityConstraint
Definition TaoCtx.hpp:261
FEMethodsSequence loopsGradient
Definition TaoCtx.hpp:38
friend PetscErrorCode TaoSetJacobianInequality(Tao tao, Vec x, Mat J, Mat Jpre, void *ctx)
Assemble the inequality constraint Jacobian.
Definition TaoCtx.cpp:502
BasicMethodsSequence & getPreProcObjective()
Definition TaoCtx.hpp:125
BasicMethodsSequence & getPreProcHessian()
Definition TaoCtx.hpp:120
boost::function< MoFEMErrorCode(Tao tao, Vec x, Vec gradient)> GradientHook
Definition TaoCtx.hpp:27
friend PetscErrorCode TaoSetInequalityConstraints(Tao tao, Vec x, Vec c, void *ctx)
Evaluate inequality constraints for a TAO optimization context.
Definition TaoCtx.cpp:479
ObjectiveAndGradientHook & getObjectiveAndGradientHook()
Get an optional combined objective-and-gradient evaluator.
Definition TaoCtx.hpp:203
BasicMethodsSequence & getPreProcInequalityConstraint()
Definition TaoCtx.hpp:148
MoFEM::BasicMethodsSequence BasicMethodsSequence
Definition TaoCtx.hpp:23
FEMethodsSequence loopsHessian
Definition TaoCtx.hpp:32
TAOContext
Context enumeration for TAO solver phases.