9static char help[] =
"Test TaoCtx constraint callbacks.\n\n";
11int main(
int argc,
char *argv[]) {
35 CHKERR TaoCreate(PETSC_COMM_WORLD, &tao);
36 CHKERR TaoSetType(tao, TAOALMM);
37 CHKERR TaoSetSolution(tao, x);
41 auto check_vector_pipeline = [&](
auto context,
const double value,
42 auto &pre_process,
auto &post_process) {
43 auto pre = boost::make_shared<BasicMethod>();
44 auto post = boost::make_shared<BasicMethod>();
45 auto *pre_ptr = pre.get();
46 auto *post_ptr = post.get();
48 pre->preProcessHook = [pre_ptr, context, value,
c]() {
50 if (pre_ptr->tao_ctx != context || pre_ptr->tao_c !=
c)
52 "Incorrect TAO constraint context");
53 CHKERR VecSet(pre_ptr->tao_c, value);
56 post->postProcessHook = [post_ptr, context,
c]() {
58 if (post_ptr->tao_ctx != context || post_ptr->tao_c !=
c)
60 "Incorrect TAO constraint context");
64 pre_process.push_back(pre);
65 post_process.push_back(post);
68 auto check_jacobian_pipeline = [&](
auto context,
const double value,
69 auto &pre_process,
auto &post_process) {
70 auto pre = boost::make_shared<BasicMethod>();
71 auto post = boost::make_shared<BasicMethod>();
72 auto *pre_ptr = pre.get();
73 auto *post_ptr = post.get();
75 pre->preProcessHook = [pre_ptr, context, value,
J]() {
77 if (pre_ptr->tao_ctx != context || pre_ptr->tao_J !=
J ||
78 pre_ptr->tao_Jpre !=
J)
80 "Incorrect TAO constraint Jacobian context");
81 CHKERR MatShift(pre_ptr->tao_Jpre, value);
84 post->postProcessHook = [post_ptr, context,
J]() {
86 if (post_ptr->tao_ctx != context || post_ptr->tao_J !=
J ||
87 post_ptr->tao_Jpre !=
J)
89 "Incorrect TAO constraint Jacobian context");
93 pre_process.push_back(pre);
94 post_process.push_back(post);
110 CHKERR TaoSetEqualityConstraintsRoutine(
112 CHKERR TaoSetInequalityConstraintsRoutine(
114 CHKERR TaoSetJacobianEqualityRoutine(
116 CHKERR TaoSetJacobianInequalityRoutine(
119 auto check_vector = [&](
const double expected) {
123 CHKERR VecNorm(
c, NORM_INFINITY, &norm);
124 if (norm > 10 * PETSC_MACHINE_EPSILON)
126 "Incorrect TAO constraint vector");
130 auto check_jacobian = [&](
const double expected) {
136 if (norm > 10 * PETSC_MACHINE_EPSILON)
138 "Incorrect TAO constraint Jacobian");
142 CHKERR TaoComputeEqualityConstraints(tao, x,
c);
144 CHKERR TaoComputeInequalityConstraints(tao, x,
c);
146 CHKERR TaoComputeJacobianEquality(tao, x,
J,
J);
147 CHKERR check_jacobian(4.0);
148 CHKERR TaoComputeJacobianInequality(tao, x,
J,
J);
149 CHKERR check_jacobian(5.0);
158 "TAO constraint pipelines were not copied");
165 "TAO constraint pipelines were not cleared");
171 simple->getProblemName(),
COL, x, INSERT_VALUES, SCATTER_REVERSE);
177 PetscInt objective_calls = 0, gradient_calls = 0;
182 "Incorrect TAO objective-hook controls");
190 CHKERR VecEqual(
g, expected_gradient, &equal);
191 if (trial != x || !equal)
193 "TAO gradient hook did not receive the cached gradient");
199 CHKERR ::TaoSetObjectiveAndGradient(
202 CHKERR TaoComputeObjective(tao, x, &objective);
203 if (objective != 3.0 || objective_calls != 1 || gradient_calls != 0)
205 "Objective-only wrapper did not dispatch exclusively to its hook");
207 if (objective_calls != 1 || gradient_calls != 1)
209 "Gradient-only wrapper did not dispatch exclusively to its hook");
210 CHKERR TaoComputeObjectiveAndGradient(tao, x, &objective,
gradient);
213 if (objective != 3.0 || objective_calls != 2 || gradient_calls != 2 ||
216 "Combined wrapper did not fall back to the separate hooks");
219 simple->getProblemName(),
COL, mesh_controls, INSERT_VALUES,
222 CHKERR VecNorm(mesh_controls, NORM_INFINITY, &mesh_norm);
225 "TAO evaluation hooks implicitly scattered trial controls");
SmartPetscObj< Vec > diagonal
SmartPetscObj< Vec > gradient
void simple(double P1[], double P2[], double P3[], double c[], const int N)
#define CATCH_ERRORS
Catch errors.
@ AINSWORTH_LEGENDRE_BASE
Ainsworth Cole (Legendre) approx. base .
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_DATA_INCONSISTENCY
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
PetscErrorCode DMRegister_MoFEM(const char sname[])
Register MoFEM problem.
auto createDMVector(DM dm, RowColData rc=RowColData::COL)
Get smart vector from DM.
auto createDMMatrix(DM dm)
Get smart matrix from DM.
const double c
speed of light (cm/ns)
FTensor::Index< 'J', DIM1 > J
implementation of Data Operators for Forces and Sources
PetscErrorCode TaoSetObjective(Tao tao, Vec x, PetscReal *f, void *ctx)
Sets the objective function value for a TAO optimization context.
PetscErrorCode TaoSetJacobianInequality(Tao tao, Vec x, Mat J, Mat Jpre, void *ctx)
Assemble the inequality constraint Jacobian.
PetscErrorCode TaoSetEqualityConstraints(Tao tao, Vec x, Vec c, void *ctx)
Evaluate equality constraints for a TAO optimization context.
SmartPetscObj< Vec > vectorDuplicate(Vec vec)
Create duplicate vector of smart vector.
PetscErrorCode TaoSetInequalityConstraints(Tao tao, Vec x, Vec c, void *ctx)
Evaluate inequality constraints for a TAO optimization context.
PetscErrorCode TaoSetJacobianEquality(Tao tao, Vec x, Mat J, Mat Jpre, void *ctx)
Assemble the equality constraint Jacobian.
PetscErrorCode TaoSetObjectiveAndGradient(Tao tao, Vec x, PetscReal *f, Vec g, void *ctx)
Sets the objective function value and gradient for a TAO optimization solver.
PetscErrorCode TaoSetGradient(Tao tao, Vec x, Vec f, void *ctx)
Sets the gradient vector for a TAO optimization context.
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Deprecated interface functions.
Simple interface for fast problem set-up.
MoFEMErrorCode getOptions()
get options
Interface for TAO solvers.
MoFEMErrorCode copyLoops(const TaoCtx &tao_ctx)
Copy sequences from another TaoCtx.
BasicMethodsSequence & getPreProcEqualityConstraint()
BasicMethodsSequence & getPreProcEqualityJacobian()
BasicMethodsSequence & getPostProcInequalityConstraint()
ObjectiveHook & getObjectiveHook()
Get an optional objective evaluator.
BasicMethodsSequence & getPostProcInequalityJacobian()
BasicMethodsSequence & getPreProcInequalityJacobian()
MoFEMErrorCode clearLoops()
Clear loops.
GradientHook & getGradientHook()
Get an optional gradient evaluator.
BasicMethodsSequence & getPostProcEqualityConstraint()
BasicMethodsSequence & getPostProcEqualityJacobian()
BasicMethodsSequence & getPreProcInequalityConstraint()
@ CTX_TAO_EQUALITY_CONSTRAINT
Evaluating equality constraints.
@ CTX_TAO_EQUALITY_JACOBIAN
Assembling equality Jacobian.
@ CTX_TAO_INEQUALITY_JACOBIAN
Assembling inequality Jacobian.
@ CTX_TAO_INEQUALITY_CONSTRAINT
Evaluating inequality constraints.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
Vector manager is used to create vectors \mofem_vectors.