10PetscErrorCode zeroTaoOutputVector(Vec
v) {
12 Vec local = PETSC_NULLPTR;
13 CHKERR VecGhostGetLocalForm(
v, &local);
15 CHKERR VecZeroEntries(local);
16 CHKERR VecGhostRestoreLocalForm(
v, &local);
23PetscErrorCode assembleTaoOutputVector(Vec
v) {
25 Vec local = PETSC_NULLPTR;
26 CHKERR VecGhostGetLocalForm(
v, &local);
27 const bool has_local_form = local != PETSC_NULLPTR;
29 CHKERR VecGhostRestoreLocalForm(
v, &local);
31 CHKERR VecGhostUpdateBegin(
v, ADD_VALUES, SCATTER_REVERSE);
32 CHKERR VecGhostUpdateEnd(
v, ADD_VALUES, SCATTER_REVERSE);
98 PetscLogEventBegin(log_event, 0, 0, 0, 0);
100 CHKERR VecGhostUpdateBegin(x, INSERT_VALUES, SCATTER_FORWARD);
101 CHKERR VecGhostUpdateEnd(x, INSERT_VALUES, SCATTER_FORWARD);
107 auto cache_ptr = boost::make_shared<CacheTuple>();
110 auto set = [&](
auto &fe) {
116 fe.tao_ctx = evaluation_context;
118 fe.cacheWeakPtr = cache_ptr;
121 auto unset = [&](
auto &fe) {
128 for (
auto &method : pre_process) {
136 for (
auto &loop : loops) {
145 for (
auto &method : post_process) {
154 CHKERR assembleTaoOutputVector(
c);
156 PetscLogEventEnd(log_event, 0, 0, 0, 0);
165 PetscLogEventBegin(log_event, 0, 0, 0, 0);
167 CHKERR VecGhostUpdateBegin(x, INSERT_VALUES, SCATTER_FORWARD);
168 CHKERR VecGhostUpdateEnd(x, INSERT_VALUES, SCATTER_FORWARD);
173 CHKERR MatZeroEntries(Jpre);
177 auto cache_ptr = boost::make_shared<CacheTuple>();
180 auto set = [&](
auto &fe) {
187 fe.tao_ctx = evaluation_context;
189 fe.cacheWeakPtr = cache_ptr;
192 auto unset = [&](
auto &fe) {
199 for (
auto &method : pre_process) {
207 for (
auto &loop : loops) {
216 for (
auto &method : post_process) {
225 CHKERR MatAssemblyBegin(Jpre, MAT_FINAL_ASSEMBLY);
226 CHKERR MatAssemblyEnd(Jpre, MAT_FINAL_ASSEMBLY);
228 CHKERR MatAssemblyBegin(
J, MAT_FINAL_ASSEMBLY);
229 CHKERR MatAssemblyEnd(
J, MAT_FINAL_ASSEMBLY);
233 PetscLogEventEnd(log_event, 0, 0, 0, 0);
238 auto *tao_ctx =
static_cast<TaoCtx *
>(ctx);
240 if (tao_ctx->objectiveHook) {
244 PetscLogEventBegin(tao_ctx->MOFEM_EVENT_TaoObjective, 0, 0, 0, 0);
245 CHKERR VecGhostUpdateBegin(x, INSERT_VALUES, SCATTER_FORWARD);
246 CHKERR VecGhostUpdateEnd(x, INSERT_VALUES, SCATTER_FORWARD);
248 tao_ctx->problemName,
COL, x, INSERT_VALUES, SCATTER_REVERSE);
250 auto cache_ptr = boost::make_shared<CacheTuple>();
251 CHKERR tao_ctx->mField.cache_problem_entities(tao_ctx->problemName,
254 auto set = [&](
auto &fe) {
259 fe.cacheWeakPtr = cache_ptr;
262 auto unset = [&](
auto &fe) {
267 for (
auto &
bit : tao_ctx->preObjective) {
268 bit->vecAssembleSwitch = boost::move(tao_ctx->vecAssembleSwitch);
270 CHKERR tao_ctx->mField.problem_basic_method_preProcess(tao_ctx->problemName,
275 for (
auto &lit : tao_ctx->loopsObjective) {
277 CHKERR tao_ctx->mField.loop_finite_elements(tao_ctx->problemName, lit.first,
283 for (
auto &
bit : tao_ctx->postObjective) {
284 bit->vecAssembleSwitch = boost::move(tao_ctx->vecAssembleSwitch);
286 CHKERR tao_ctx->mField.problem_basic_method_postProcess(
287 tao_ctx->problemName, *
bit);
289 tao_ctx->vecAssembleSwitch = boost::move(
bit->vecAssembleSwitch);
292 PetscLogEventEnd(tao_ctx->MOFEM_EVENT_TaoObjective, 0, 0, 0, 0);
297 auto *tao_ctx =
static_cast<TaoCtx *
>(ctx);
299 if (tao_ctx->gradientHook) {
303 PetscLogEventBegin(tao_ctx->MOFEM_EVENT_TaoGradient, 0, 0, 0, 0);
304 CHKERR VecGhostUpdateBegin(x, INSERT_VALUES, SCATTER_FORWARD);
305 CHKERR VecGhostUpdateEnd(x, INSERT_VALUES, SCATTER_FORWARD);
307 tao_ctx->problemName,
COL, x, INSERT_VALUES, SCATTER_REVERSE);
309 auto zero_ghost_vec = [](Vec
g) {
317 for (
int i = 0;
i != s; ++
i)
320 CHKERR VecGhostRestoreLocalForm(
g, &
l);
325 tao_ctx->vecAssembleSwitch = boost::movelib::make_unique<bool>(
true);
326 auto cache_ptr = boost::make_shared<CacheTuple>();
327 CHKERR tao_ctx->mField.cache_problem_entities(tao_ctx->problemName,
330 auto set = [&](
auto &fe) {
338 fe.cacheWeakPtr = cache_ptr;
341 auto unset = [&](
auto &fe) {
348 for (
auto &
bit : tao_ctx->preGradient) {
349 bit->vecAssembleSwitch = boost::move(tao_ctx->vecAssembleSwitch);
351 CHKERR tao_ctx->mField.problem_basic_method_preProcess(tao_ctx->problemName,
354 tao_ctx->vecAssembleSwitch = boost::move(
bit->vecAssembleSwitch);
357 for (
auto &lit : tao_ctx->loopsGradient) {
358 lit.second->vecAssembleSwitch = boost::move(tao_ctx->vecAssembleSwitch);
360 CHKERR tao_ctx->mField.loop_finite_elements(tao_ctx->problemName, lit.first,
364 tao_ctx->vecAssembleSwitch = boost::move(lit.second->vecAssembleSwitch);
367 for (
auto &
bit : tao_ctx->postGradient) {
368 bit->vecAssembleSwitch = boost::move(tao_ctx->vecAssembleSwitch);
370 CHKERR tao_ctx->mField.problem_basic_method_postProcess(
371 tao_ctx->problemName, *
bit);
373 tao_ctx->vecAssembleSwitch = boost::move(
bit->vecAssembleSwitch);
376 if (*(tao_ctx->vecAssembleSwitch)) {
377 CHKERR VecGhostUpdateBegin(f, ADD_VALUES, SCATTER_REVERSE);
378 CHKERR VecGhostUpdateEnd(f, ADD_VALUES, SCATTER_REVERSE);
379 CHKERR VecAssemblyBegin(f);
382 PetscLogEventEnd(tao_ctx->MOFEM_EVENT_TaoGradient, 0, 0, 0, 0);
405 auto cache_ptr = boost::make_shared<CacheTuple>();
409 auto set = [&](
auto &fe) {
417 fe.cacheWeakPtr = cache_ptr;
420 auto unset = [&](
auto &fe) {
427 CHKERR VecGhostUpdateBegin(x, INSERT_VALUES, SCATTER_FORWARD);
428 CHKERR VecGhostUpdateEnd(x, INSERT_VALUES, SCATTER_FORWARD);
445 *(lit.second),
nullptr,
461 CHKERR MatAssemblyBegin(Hpre, MAT_FINAL_ASSEMBLY);
462 CHKERR MatAssemblyEnd(Hpre, MAT_FINAL_ASSEMBLY);
470 auto *tao_ctx =
static_cast<TaoCtx *
>(ctx);
472 tao, x,
c, tao_ctx->loopsEqualityConstraint,
473 tao_ctx->preEqualityConstraint, tao_ctx->postEqualityConstraint,
475 tao_ctx->MOFEM_EVENT_TaoEqualityConstraint);
481 auto *tao_ctx =
static_cast<TaoCtx *
>(ctx);
483 tao, x,
c, tao_ctx->loopsInequalityConstraint,
484 tao_ctx->preInequalityConstraint, tao_ctx->postInequalityConstraint,
486 tao_ctx->MOFEM_EVENT_TaoInequalityConstraint);
493 auto *tao_ctx =
static_cast<TaoCtx *
>(ctx);
495 tao, x,
J, Jpre, tao_ctx->loopsEqualityJacobian,
496 tao_ctx->preEqualityJacobian, tao_ctx->postEqualityJacobian,
498 tao_ctx->MOFEM_EVENT_TaoEqualityJacobian);
505 auto *tao_ctx =
static_cast<TaoCtx *
>(ctx);
507 tao, x,
J, Jpre, tao_ctx->loopsInequalityJacobian,
508 tao_ctx->preInequalityJacobian, tao_ctx->postInequalityJacobian,
510 tao_ctx->MOFEM_EVENT_TaoInequalityJacobian);
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
virtual MoFEMErrorCode problem_basic_method_postProcess(const Problem *problem_ptr, BasicMethod &method, int verb=DEFAULT_VERBOSITY)=0
Set data for BasicMethod.
virtual MoFEMErrorCode loop_finite_elements(const std::string problem_name, const std::string &fe_name, FEMethod &method, boost::shared_ptr< NumeredEntFiniteElement_multiIndex > fe_ptr=nullptr, MoFEMTypes bh=MF_EXIST, CacheTupleWeakPtr cache_ptr=CacheTupleSharedPtr(), int verb=DEFAULT_VERBOSITY)=0
Make a loop over finite elements.
FTensor::Index< 'i', SPACE_DIM > i
const double c
speed of light (cm/ns)
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'J', DIM1 > J
FTensor::Index< 'l', 3 > l
const FTensor::Tensor2< T, Dim, Dim > Vec
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
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.
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.
PetscErrorCode TaoSetHessian(Tao tao, Vec x, Mat H, Mat Hpre, void *ctx)
Sets the Hessian matrix for a TAO optimization context.
virtual MoFEMErrorCode cache_problem_entities(const std::string prb_name, CacheTupleWeakPtr cache_ptr)=0
Cache variables.
virtual MoFEMErrorCode problem_basic_method_preProcess(const Problem *problem_ptr, BasicMethod &method, int verb=DEFAULT_VERBOSITY)=0
Set data for BasicMethod.
@ CTX_KSPNONE
No specific KSP context.
@ CTX_SETFUNCTION
Setting up the linear system function.
@ CTX_OPERATORS
Setting up linear operators.
static constexpr Switches CtxSetA
Jacobian matrix switch.
static constexpr Switches CtxSetX
Solution vector switch.
static constexpr Switches CtxSetNone
No data switch.
static constexpr Switches CtxSetF
Residual vector switch.
static constexpr Switches CtxSetB
Preconditioner matrix switch.
@ CTX_SNESSETJACOBIAN
Setting up Jacobian matrix computation.
@ CTX_SNESSETFUNCTION
Setting up nonlinear function evaluation.
@ CTX_SNESNONE
No specific SNES context.
Interface for TAO solvers.
MoFEMErrorCode copyLoops(const TaoCtx &tao_ctx)
Copy sequences from another TaoCtx.
FEMethodsSequence loopsInequalityJacobian
boost::movelib::unique_ptr< bool > vecAssembleSwitch
MoFEM::FEMethodsSequence FEMethodsSequence
MoFEM::Interface & mField
database Interface
BasicMethodsSequence postInequalityConstraint
PetscErrorCode evaluateConstraint(Tao tao, Vec x, Vec c, FEMethodsSequence &loops, BasicMethodsSequence &pre_process, BasicMethodsSequence &post_process, TaoMethod::TAOContext evaluation_context, PetscLogEvent log_event)
BasicMethodsSequence preEqualityJacobian
BasicMethodsSequence postObjective
Sequence of methods run after objective is evaluated.
BasicMethodsSequence preEqualityConstraint
std::string problemName
problem name
BasicMethodsSequence preInequalityConstraint
BasicMethodsSequence postEqualityConstraint
BasicMethodsSequence postGradient
Sequence of methods run after gradient is assembled.
PetscLogEvent MOFEM_EVENT_TaoHessian
Log event for TAO Hessian.
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)
boost::movelib::unique_ptr< bool > matAssembleSwitch
FEMethodsSequence loopsInequalityConstraint
BasicMethodsSequence preObjective
Sequence of methods run before objective is evaluated.
FEMethodsSequence loopsObjective
BasicMethodsSequence preGradient
Sequence of methods run before gradient is assembled.
GradientHook gradientHook
BasicMethodsSequence postEqualityJacobian
FEMethodsSequence loopsEqualityConstraint
ObjectiveAndGradientHook objectiveAndGradientHook
MoFEMErrorCode clearLoops()
Clear loops.
BasicMethodsSequence preInequalityJacobian
BasicMethodsSequence postHessian
Sequence of methods run after Hessian is assembled.
ObjectiveHook objectiveHook
BasicMethodsSequence preHessian
Sequence of methods run before Hessian is assembled.
FEMethodsSequence loopsEqualityJacobian
BasicMethodsSequence postInequalityJacobian
FEMethodsSequence loopsGradient
MoFEM::BasicMethodsSequence BasicMethodsSequence
FEMethodsSequence loopsHessian
TAOContext
Context enumeration for TAO solver phases.
@ CTX_TAO_EQUALITY_CONSTRAINT
Evaluating equality constraints.
@ CTX_TAO_EQUALITY_JACOBIAN
Assembling equality Jacobian.
@ CTX_TAO_HESSIAN
Assembling Hessian matrix.
@ CTX_TAO_NONE
No specific TAO context.
@ CTX_TAO_INEQUALITY_JACOBIAN
Assembling inequality Jacobian.
@ CTX_TAO_INEQUALITY_CONSTRAINT
Evaluating inequality constraints.
@ CTX_TAO_GRADIENT
Computing gradient vector.
@ CTX_TAO_OBJECTIVE
Evaluating objective function.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.
Vector manager is used to create vectors \mofem_vectors.