21 MooseEnum tao_solver_enum(
"taontr taobntr taobncg taonls taobnls taobqnktr taontl taobntl "
22 "taonm taobqnls taoowlqn taogpcg taobmrm taoalmm");
24 "tao_solver", tao_solver_enum,
"Tao solver to use for optimization.");
34 "solve_on", exec_enum,
"List of flags indicating when inner system solve should occur.");
36 "output_optimization_iterations",
38 "Use the time step as the current iteration for outputting optimization history.");
44 _my_comm(MPI_COMM_SELF),
46 _verbose(getParam<bool>(
"verbose")),
47 _output_opt_iters(getParam<bool>(
"output_optimization_iterations")),
49 _parameters(
std::make_unique<
libMesh::PetscVector<Number>>(_my_comm))
52 mooseError(
"OptimizeSolve does not currently support threaded execution");
56 "moose", 27225,
"Outputting for transient executioners has not been implemented.");
62 TIME_SECTION(
"optimizeSolve", 1,
"Optimization Solve");
68 mooseError(
"No OptimizationReporter object found.");
90#if PETSC_RELEASE_LESS_THAN(3, 21, 0)
91 LibmeshPetscCallQ(TaoSetMonitor(
_tao,
monitor,
this,
nullptr));
93 LibmeshPetscCallQ(TaoMonitorSet(
_tao,
monitor,
this,
nullptr));
99 LibmeshPetscCallQ(TaoSetType(
_tao, TAONTR));
102 LibmeshPetscCallQ(TaoSetType(
_tao, TAOBNTR));
105 LibmeshPetscCallQ(TaoSetType(
_tao, TAOBNCG));
108 LibmeshPetscCallQ(TaoSetType(
_tao, TAONLS));
111 LibmeshPetscCallQ(TaoSetType(
_tao, TAOBNLS));
114 LibmeshPetscCallQ(TaoSetType(
_tao, TAOBQNKTR));
117 LibmeshPetscCallQ(TaoSetType(
_tao, TAONTL));
120 LibmeshPetscCallQ(TaoSetType(
_tao, TAOBNTL));
123 LibmeshPetscCallQ(TaoSetType(
_tao, TAONM));
127 LibmeshPetscCallQ(TaoSetType(
_tao, TAOBQNLS));
130 LibmeshPetscCallQ(TaoSetType(
_tao, TAOOWLQN));
133 LibmeshPetscCallQ(TaoSetType(
_tao, TAOGPCG));
136 LibmeshPetscCallQ(TaoSetType(
_tao, TAOBMRM));
139#if !PETSC_VERSION_LESS_THAN(3, 15, 0)
140 LibmeshPetscCallQ(TaoSetType(
_tao, TAOALMM));
143#if PETSC_RELEASE_GREATER_EQUALS(3, 21, 0)
144 LibmeshPetscCallQ(TaoMonitorCancel(
_tao));
146 LibmeshPetscCallQ(TaoCancelMonitors(
_tao));
148 LibmeshPetscCallQ(PetscOptionsSetValue(NULL,
"-tao_cmonitor", NULL));
151 mooseError(
"ALMM is only compatible with PETSc versions above 3.14. ");
159#if !PETSC_VERSION_LESS_THAN(3, 17, 0)
164#if !PETSC_VERSION_LESS_THAN(3, 17, 0)
175#if !PETSC_VERSION_LESS_THAN(3, 17, 0)
182#if !PETSC_VERSION_LESS_THAN(3, 17, 0)
189 LibmeshPetscCallQ(TaoSetFromOptions(
_tao));
208 LibmeshPetscCallQ(TaoSolve(
_tao));
211 if (getParam<bool>(
"verbose"))
212 LibmeshPetscCallQ(TaoView(
_tao, PETSC_VIEWER_STDOUT_WORLD));
214 LibmeshPetscCallQ(TaoDestroy(&
_tao));
216 LibmeshPetscCallQ(MatDestroy(&
_hessian));
221 PetscFunctionReturn(PETSC_SUCCESS);
226 std::vector<double> & gnorm,
227 std::vector<int> & obj_iters,
228 std::vector<double> & cnorm,
229 std::vector<int> & grad_iters,
230 std::vector<double> & xdiff,
231 std::vector<int> & hess_iters,
232 std::vector<double> &
f,
233 std::vector<int> & tot_solves)
const
236 tot_iters.resize(num);
237 obj_iters.resize(num);
238 grad_iters.resize(num);
239 hess_iters.resize(num);
240 tot_solves.resize(num);
246 for (
const auto i : make_range(num))
284 if (
auto steady =
dynamic_cast<Steady *
>(sub_app->getExecutioner(0)))
285 steady->setIterationNumberOutput((
unsigned int)its);
298 _console <<
"TAO SOLVER: iteration=" << its <<
"\tf=" <<
f <<
"\tgnorm=" << gnorm
299 <<
"\tcnorm=" << cnorm <<
"\txdiff=" << xdiff << std::endl;
305 TaoConvergedReason reason;
307 PetscReal
f, gnorm, cnorm, xdiff;
310 LibmeshPetscCallQ(TaoGetSolutionStatus(tao, &its, &
f, &gnorm, &cnorm, &xdiff, &reason));
315 PetscFunctionReturn(PETSC_SUCCESS);
325 solver->_parameters->swap(param);
327 (*objective) = solver->objectiveFunction();
328 solver->_parameters->swap(param);
329 PetscFunctionReturn(PETSC_SUCCESS);
334 Tao , Vec
x, Real * objective, Vec gradient,
void * ctx)
340 solver->_parameters->swap(param);
342 (*objective) = solver->objectiveFunction();
344 solver->gradientFunction(grad);
345 solver->_parameters->swap(param);
346 PetscFunctionReturn(PETSC_SUCCESS);
351 Tao , Vec , Mat , Mat ,
void * ctx)
356 LibmeshPetscCallQ(MatShellSetOperation(
358 PetscFunctionReturn(PETSC_SUCCESS);
367 LibmeshPetscCallQ(MatShellGetContext(
H, &ctx));
372 return solver->applyHessian(sbar, Hsbar);
381 LibmeshPetscCallQ(solver->variableBounds(tao));
382 PetscFunctionReturn(PETSC_SUCCESS);
388 TIME_SECTION(
"objectiveFunction", 2,
"Objective forward solve");
411 TIME_SECTION(
"gradientFunction", 2,
"Gradient adjoint solve");
430 TIME_SECTION(
"applyHessian", 2,
"Hessian forward/adjoint solve");
436 mooseError(
"Hessian based optimization algorithms require a sub-app with:\n"
437 " execute_on = HOMOGENEOUS_FORWARD");
444 mooseError(
"Homogeneous forward solve multiapp failed!");
461 PetscFunctionReturn(PETSC_SUCCESS);
474 for (
const auto i : make_range(sz))
480 LibmeshPetscCallQ(TaoSetVariableBounds(tao, xl.
vec(), xu.
vec()));
481 PetscFunctionReturn(PETSC_SUCCESS);
494 PetscFunctionReturn(PETSC_SUCCESS);
499 Tao , Vec , Mat gradient_e, Mat ,
void * ctx)
509 PetscFunctionReturn(PETSC_SUCCESS);
522 PetscFunctionReturn(PETSC_SUCCESS);
527 Tao , Vec , Mat gradient_i, Mat ,
void * ctx)
537 PetscFunctionReturn(PETSC_SUCCESS);
550 LibmeshPetscCallQ(VecSetFromOptions(
_ce));
551 LibmeshPetscCallQ(VecSetUp(
_ce));
555 LibmeshPetscCallQ(MatSetSizes(
564 LibmeshPetscCallQ(TaoSetJacobianEqualityRoutine(
574 LibmeshPetscCallQ(VecSetFromOptions(
_ci));
575 LibmeshPetscCallQ(VecSetUp(
_ci));
592 LibmeshPetscCallQ(TaoSetJacobianInequalityRoutine(
595 PetscFunctionReturn(PETSC_SUCCESS);
604 LibmeshPetscCallQ(VecDestroy(&
_ce));
609 LibmeshPetscCallQ(VecDestroy(&
_ci));
613 PetscFunctionReturn(PETSC_SUCCESS);
Real f(Real x)
Test function for Brents method.
const std::vector< double > x
const ExecFlagType EXEC_NONE
void ErrorVector unsigned int
const ConsoleStream _console
FEProblemBase & feProblem()
T & getUserObject(const std::string &name, unsigned int tid=0) const
ExecuteMooseObjectWarehouse< MultiApp > & getMultiAppWarehouse()
bool hasMultiApps() const
SolverParams & solverParams(unsigned int solver_sys_num=0)
bool hasUserObject(const std::string &name) const
void restoreMultiApps(ExecFlagType type, bool force=false)
bool execMultiApps(ExecFlagType type, bool auto_advance=true)
void backupMultiApps(ExecFlagType type)
virtual void execute(const ExecFlagType &exec_type)
Moose::PetscSupport::PetscOptions & getPetscOptions()
virtual int & timeStep() const
virtual bool isTransient() const override
virtual void outputStep(ExecFlagType type)
Executioner * getExecutioner() const
void mooseDocumentedError(const std::string &repo_name, const unsigned int issue_num, Args &&... args) const
void mooseError(Args &&... args) const
const std::vector< std::shared_ptr< T > > & getObjects(THREAD_ID tid=0) const
bool isValueSet(const std::string &value) const
Base class for optimization objects, implements routines for calculating misfit.
void setInitialCondition(libMesh::PetscVector< Number > ¶m)
Function to initialize petsc vectors from vpp data.
virtual void computeGradient(libMesh::PetscVector< Number > &gradient) const
Function to compute gradient.
virtual Real computeObjective()=0
Function to compute objective.
virtual void computeEqualityGradient(libMesh::PetscMatrix< Number > &gradient) const
Function to compute the gradient of the equality constraints/ This is the last call of the equality c...
Real getLowerBound(dof_id_type i) const
dof_id_type getNumEqCons() const
Function to get the total number of equalities.
virtual void computeInequalityConstraints(libMesh::PetscVector< Number > &ineqs_constraints) const
Function to compute the inequality constraints.
virtual void updateParameters(const libMesh::PetscVector< Number > &x)
Function to set parameters.
Real getUpperBound(dof_id_type i) const
Upper and lower bounds for each parameter being controlled.
virtual dof_id_type getNumParams() const
Function to get the total number of parameters.
virtual void computeEqualityConstraints(libMesh::PetscVector< Number > &eqs_constraints) const
Function to compute the equality constraints.
virtual void computeInequalityGradient(libMesh::PetscMatrix< Number > &gradient) const
Function to compute the gradient of the inequality constraints/ This is the last call of the inequali...
dof_id_type getNumInEqCons() const
Function to get the total number of inequalities.
solveObject to interface with Petsc Tao
static PetscErrorCode objectiveFunctionWrapper(Tao tao, Vec x, Real *objective, void *ctx)
static PetscErrorCode equalityFunctionWrapper(Tao tao, Vec x, Vec ce, void *ctx)
Mat _gradient_e
Equality constraint gradient.
std::vector< double > _f_vec
objective value per iteration
dof_id_type _ndof
Number of parameters being optimized.
std::vector< int > _grad_iterate_vec
gradient solves per iteration
virtual Real objectiveFunction()
Objective routine.
virtual bool solve() override
Moose::PetscSupport::PetscOptions _petsc_options
const libMesh::Parallel::Communicator _my_comm
Communicator used for operations.
std::vector< int > _obj_iterate_vec
number of objective solves per iteration
static PetscErrorCode monitor(Tao tao, void *ctx)
void getTaoSolutionStatus(std::vector< int > &tot_iters, std::vector< double > &gnorm, std::vector< int > &obj_iters, std::vector< double > &cnorm, std::vector< int > &grad_iters, std::vector< double > &xdiff, std::vector< int > &hess_iters, std::vector< double > &f, std::vector< int > &tot_solves) const
Record tao TaoGetSolutionStatus data for output by a reporter.
Mat _gradient_i
Inequality constraint gradient.
Vec _ce
Equality constraint vector.
std::vector< int > _hess_iterate_vec
Hessian solves per iteration.
virtual PetscErrorCode variableBounds(Tao tao)
Bounds routine.
std::vector< double > _xdiff_vec
step length per iteration
virtual void gradientFunction(libMesh::PetscVector< Number > &gradient)
Gradient routine.
std::unique_ptr< libMesh::PetscVector< Number > > _parameters
Parameters (solution) given to TAO.
void setTaoSolutionStatus(double f, int its, double gnorm, double cnorm, double xdiff)
output optimization iteration solve data
const ExecFlagEnum & _solve_on
List of execute flags for when to solve the system.
OptimizeSolve(Executioner &ex)
static PetscErrorCode inequalityGradientFunctionWrapper(Tao tao, Vec x, Mat gradient_i, Mat gradient_ipre, void *ctx)
TaoSolverEnum
Enum of tao solver types.
@ BOUNDED_NEWTON_TRUST_LINE
@ BOUNDED_QUASI_NEWTON_LINE_SEARCH
@ AUGMENTED_LAGRANGIAN_MULTIPLIER_METHOD
@ BOUNDED_NEWTON_TRUST_REGION
@ BOUNDED_CONJUGATE_GRADIENT
@ BOUNDED_QUASI_NEWTON_TRUST_REGION
@ GRADIENT_PROJECTION_CONJUGATE_GRADIENT
@ BOUNDED_NEWTON_LINE_SEARCH
std::vector< int > _total_iterate_vec
total solves per iteration
bool _verbose
control optimization executioner output
static PetscErrorCode applyHessianWrapper(Mat H, Vec s, Vec Hs)
std::vector< double > _cnorm_vec
infeasibility norm per iteration
virtual PetscErrorCode applyHessian(libMesh::PetscVector< Number > &s, libMesh::PetscVector< Number > &Hs)
Hessian application routine.
static PetscErrorCode equalityGradientFunctionWrapper(Tao tao, Vec x, Mat gradient_e, Mat gradient_epre, void *ctx)
static PetscErrorCode inequalityFunctionWrapper(Tao tao, Vec x, Vec ci, void *ctx)
static PetscErrorCode variableBoundsWrapper(Tao, Vec xl, Vec xu, void *ctx)
Mat _hessian
Hessian (matrix) - usually a matrix-free representation.
enum OptimizeSolve::TaoSolverEnum _tao_solver_enum
bool _output_opt_iters
Use time step as the iteration counter for purposes of outputting.
PetscErrorCode taoALCreate()
Used for creating petsc structures when using the ALMM algorithm.
std::vector< int > _function_solve_vec
total solves per iteration
std::vector< double > _gnorm_vec
gradient norm per iteration
static PetscErrorCode hessianFunctionWrapper(Tao tao, Vec x, Mat hessian, Mat pc, void *ctx)
OptimizationReporterBase * _obj_function
objective function defining objective, gradient, and hessian
static InputParameters validParams()
static PetscErrorCode objectiveAndGradientFunctionWrapper(Tao tao, Vec x, Real *objective, Vec gradient, void *ctx)
Tao _tao
Tao optimization object.
PetscErrorCode taoSolve()
Here is where we call tao and solve.
Vec _ci
Inequality constraint vector.
PetscErrorCode taoALDestroy()
Used for destroying petsc structures when using the ALMM algorithm.
SolverParams _solver_params
SolveObject * _inner_solve
THREAD_ID numThreads() const
virtual void set(const numeric_index_type i, const T value) override
void petscSetOptions(const PetscOptions &po, const SolverParams &solver_params, FEProblemBase *const problem=nullptr)
const ExecFlagType EXEC_ADJOINT
const ExecFlagType EXEC_FORWARD
const ExecFlagType EXEC_HOMOGENEOUS_FORWARD
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...