// @HEADER // ***************************************************************************** // Piro: Strategy package for embedded analysis capabilitites // // Copyright 2010 NTESS and the Piro contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef PIRO_THYRAPRODUCTME_ROL_DYNAMICCONSTRAINT_HPP #define PIRO_THYRAPRODUCTME_ROL_DYNAMICCONSTRAINT_HPP #include "Thyra_ModelEvaluator.hpp" #include "Tempus_Integrator.hpp" #include "Tempus_StepperOptimizationInterface.hpp" #include "ROL_DynamicConstraint.hpp" #include "ROL_Ptr.hpp" #include "ROL_Vector.hpp" #include "ROL_ThyraVector.hpp" #include "Piro_TempusIntegrator.hpp" namespace Piro{ template class ThyraProductME_ROL_DynamicConstraint : public ROL::DynamicConstraint { private: Teuchos::RCP> integrator_; int num_responses_; Teuchos::ParameterList& optParams_; Teuchos::RCP out_; Teuchos::EVerbosityLevel verbosityLevel_; Teuchos::RCP> observer_; // Forward and (optional) adjoint model evaluator. ROL::Ptr> model_; Teuchos::RCP> modelAdjoint_; // Forward and (optional) adjoint steppers. ROL::Ptr> stepper_; ROL::Ptr> stepperAdjoint_; // Forward linear operators. ROL::Ptr> Ju_; ROL::Ptr> Ju_op_; ROL::Ptr> Jz_op_; // (Optional) Adjoint linear operators. ROL::Ptr> adjointJu_; ROL::Ptr> adjointJu_op_; // Flag indicating the use of the adjoint evaluator. bool usingAdjoint_; using SMT=Teuchos::ScalarTraits::ScalarMag>; public: ThyraProductME_ROL_DynamicConstraint(const Teuchos::RCP> & integrator, Teuchos::ParameterList& piroParams, Teuchos::EVerbosityLevel verbLevel= Teuchos::VERB_HIGH, Teuchos::RCP> observer = Teuchos::null); // The convention is that the adjoint model provides the application of // the adjoint Jacobian and its inverse. All other operations are // provided by the forward model. ThyraProductME_ROL_DynamicConstraint(const Teuchos::RCP> & forward_integrator, const Teuchos::RCP> & adjoint_integrator, const Teuchos::RCP> & modelAdjoin, Teuchos::ParameterList& piroParams, Teuchos::EVerbosityLevel verbLevel= Teuchos::VERB_HIGH, Teuchos::RCP> observer = Teuchos::null); virtual ~ThyraProductME_ROL_DynamicConstraint() {} void setNumResponses(int num_responses) { num_responses_ = num_responses; } void value(ROL::Vector &c, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const; void applyJacobian_uo(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const; void applyJacobian_un(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const; void applyJacobian_z(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const; void applyAdjointJacobian_uo(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const; void applyAdjointJacobian_un(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const; void applyAdjointJacobian_z(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const; void applyInverseJacobian_un(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const; void applyInverseAdjointJacobian_un(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const; }; //---------------------------------------------------------------------------- // Constructor with a single model. template ThyraProductME_ROL_DynamicConstraint::ThyraProductME_ROL_DynamicConstraint( const Teuchos::RCP>& integrator, Teuchos::ParameterList& piroParams, Teuchos::EVerbosityLevel verbLevel, Teuchos::RCP> observer) : integrator_(integrator), optParams_(piroParams.sublist("Optimization Status")), out_(Teuchos::VerboseObjectBase::getDefaultOStream()), verbosityLevel_(verbLevel), observer_(observer) { model_ = integrator->getStepper()->getModel(); stepper_ = ROL::dynamicPtrCast>(integrator->getStepper()); Ju_ = model_->create_W(); Ju_op_ = model_->create_W_op(); Jz_op_ = model_->create_DfDp_op(0); // Set all other member variables to null. modelAdjoint_ = integrator->getAdjointModel(); stepperAdjoint_ = ROL::dynamicPtrCast>(integrator->getStepper()); adjointJu_ = modelAdjoint_->create_W(); adjointJu_op_ = modelAdjoint_->create_W_op(); // Set adjoint flag to false. usingAdjoint_ = false; num_responses_ = -1; } //---------------------------------------------------------------------------- // Constructor with forward and adjoint models. template ThyraProductME_ROL_DynamicConstraint::ThyraProductME_ROL_DynamicConstraint( const Teuchos::RCP>& forward_integrator, const Teuchos::RCP>& adjoint_integrator, const Teuchos::RCP>& modelAdjoint, Teuchos::ParameterList& piroParams, Teuchos::EVerbosityLevel verbLevel, Teuchos::RCP> observer) : optParams_(piroParams.sublist("Optimization Status")), out_(Teuchos::VerboseObjectBase::getDefaultOStream()), verbosityLevel_(verbLevel), observer_(observer) { model_ = forward_integrator->getStepper()->getModel(); modelAdjoint_ = modelAdjoint; stepper_ = ROL::dynamicPtrCast>(forward_integrator->getStepper()); stepperAdjoint_ = ROL::dynamicPtrCast>(adjoint_integrator->getStepper()); Ju_ = model_->create_W(); Ju_op_ = model_->create_W_op(); Jz_op_ = model_->create_DfDp_op(0); adjointJu_ = modelAdjoint_->create_W(); adjointJu_op_ = modelAdjoint_->create_W_op(); // Set adjoint flag to true. usingAdjoint_ = true; } //---------------------------------------------------------------------------- // Value template void ThyraProductME_ROL_DynamicConstraint::value(ROL::Vector &c, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::ThyraVector& rtv_c = dynamic_cast& >(c); const ROL::ThyraVector& rtv_uold = dynamic_cast& >(uold); const ROL::ThyraVector& rtv_unew = dynamic_cast& >(unew); const ROL::ThyraVector& rtv_z = dynamic_cast& >(z); Teuchos::Array>> x(2); Teuchos::Array t(2); x[0] = rtv_unew.getVector(); t[0] = ts.t[1]; // note: x[0] is the NEW state x[1] = rtv_uold.getVector(); t[1] = ts.t[0]; // note: x[1] is the OLD state stepper_->computeStepResidual(*(rtv_c.getVector()), x, t, *(rtv_z.getVector()), 0); } // value //---------------------------------------------------------------------------- // Partial Jacobians template void ThyraProductME_ROL_DynamicConstraint::applyJacobian_uo(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::ThyraVector& rtv_jv = dynamic_cast& >(jv); const ROL::ThyraVector& rtv_v = dynamic_cast& >(v); const ROL::ThyraVector& rtv_uold = dynamic_cast& >(uold); const ROL::ThyraVector& rtv_unew = dynamic_cast& >(unew); const ROL::ThyraVector& rtv_z = dynamic_cast& >(z); TEUCHOS_TEST_FOR_EXCEPTION( SMT::isnaninf(rtv_v.getVector()->norm_2()), std::logic_error, "ThyraProductME_ROL_DynamicConstraint::applyJacobian_uo, the norm of rtv_v is not a number." << std::endl); Teuchos::Array>> x(2); Teuchos::Array t(2); x[0] = rtv_unew.getVector(); t[0] = ts.t[1]; // note: x[0] is the NEW state x[1] = rtv_uold.getVector(); t[1] = ts.t[0]; // note: x[1] is the OLD state int deriv_index = 1; // 1 = old state stepper_->computeStepJacobian(*Ju_op_, x, t, *(rtv_z.getVector()), 0, deriv_index); Ju_op_->apply(Thyra::NOTRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), 1.0, 0.0); } // applyJacobian_uo template void ThyraProductME_ROL_DynamicConstraint::applyJacobian_un(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::ThyraVector& rtv_jv = dynamic_cast& >(jv); const ROL::ThyraVector& rtv_v = dynamic_cast& >(v); const ROL::ThyraVector& rtv_uold = dynamic_cast& >(uold); const ROL::ThyraVector& rtv_unew = dynamic_cast& >(unew); const ROL::ThyraVector& rtv_z = dynamic_cast& >(z); TEUCHOS_TEST_FOR_EXCEPTION( SMT::isnaninf(rtv_v.getVector()->norm_2()), std::logic_error, "ThyraProductME_ROL_DynamicConstraint::applyJacobian_un, the norm of rtv_v is not a number." << std::endl); Teuchos::Array>> x(2); Teuchos::Array t(2); x[0] = rtv_unew.getVector(); t[0] = ts.t[1]; // note: x[0] is the NEW state x[1] = rtv_uold.getVector(); t[1] = ts.t[0]; // note: x[1] is the OLD state int deriv_index = 0; // 0 = new state stepper_->computeStepJacobian(*Ju_op_, x, t, *(rtv_z.getVector()), 0, deriv_index); Ju_op_->apply(Thyra::NOTRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), 1.0, 0.0); } // applyJacobian_un template void ThyraProductME_ROL_DynamicConstraint::applyJacobian_z(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::ThyraVector& rtv_jv = dynamic_cast& >(jv); const ROL::ThyraVector& rtv_v = dynamic_cast& >(v); const ROL::ThyraVector& rtv_uold = dynamic_cast& >(uold); const ROL::ThyraVector& rtv_unew = dynamic_cast& >(unew); const ROL::ThyraVector& rtv_z = dynamic_cast& >(z); TEUCHOS_TEST_FOR_EXCEPTION( SMT::isnaninf(rtv_v.getVector()->norm_2()), std::logic_error, "ThyraProductME_ROL_DynamicConstraint::applyJacobian_z, the norm of rtv_v is not a number." << std::endl); Teuchos::Array>> x(2); Teuchos::Array t(2); x[0] = rtv_unew.getVector(); t[0] = ts.t[1]; // note: x[0] is the NEW state x[1] = rtv_uold.getVector(); t[1] = ts.t[0]; // note: x[1] is the OLD state stepper_->computeStepParamDeriv(*Jz_op_, x, t, *(rtv_z.getVector()), 0); rtv_jv.getVector()->assign(0.0); Jz_op_->apply(Thyra::NOTRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), 1.0, 0.0); } // applyJacobian_z //---------------------------------------------------------------------------- // Adjoint partial Jacobians template void ThyraProductME_ROL_DynamicConstraint::applyAdjointJacobian_uo(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::ThyraVector& rtv_jv = dynamic_cast& >(jv); const ROL::ThyraVector& rtv_v = dynamic_cast& >(v); const ROL::ThyraVector& rtv_uold = dynamic_cast& >(uold); const ROL::ThyraVector& rtv_unew = dynamic_cast& >(unew); const ROL::ThyraVector& rtv_z = dynamic_cast& >(z); TEUCHOS_TEST_FOR_EXCEPTION( SMT::isnaninf(rtv_v.getVector()->norm_2()), std::logic_error, "ThyraProductME_ROL_DynamicConstraint::applyAdjointJacobian_uo, the norm of rtv_v is not a number." << std::endl); Teuchos::Array>> x(2); Teuchos::Array t(2); x[0] = rtv_unew.getVector(); t[0] = ts.t[1]; // note: x[0] is the NEW state x[1] = rtv_uold.getVector(); t[1] = ts.t[0]; // note: x[1] is the OLD state if (!usingAdjoint_) { int deriv_index = 1; // 1 = old state stepper_->computeStepJacobian(*Ju_op_, x, t, *(rtv_z.getVector()), 0, deriv_index); Ju_op_->apply(Thyra::TRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), 1.0, 0.0); } else { int deriv_index = 1; // 1 = old state stepperAdjoint_->computeStepJacobian(*adjointJu_op_, x, t, *(rtv_z.getVector()), 0, deriv_index); rtv_jv.getVector()->assign(0.0); //Ju_op_->apply(Thyra::TRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), 1.0, 0.0); adjointJu_op_->apply(Thyra::NOTRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), 1.0, 0.0); } } // applyAdjointJacobian_uo template void ThyraProductME_ROL_DynamicConstraint::applyAdjointJacobian_un(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::ThyraVector& rtv_jv = dynamic_cast& >(jv); const ROL::ThyraVector& rtv_v = dynamic_cast& >(v); const ROL::ThyraVector& rtv_uold = dynamic_cast& >(uold); const ROL::ThyraVector& rtv_unew = dynamic_cast& >(unew); const ROL::ThyraVector& rtv_z = dynamic_cast& >(z); TEUCHOS_TEST_FOR_EXCEPTION( SMT::isnaninf(rtv_v.getVector()->norm_2()), std::logic_error, "ThyraProductME_ROL_DynamicConstraint::applyAdjointJacobian_un, the norm of rtv_v is not a number." << std::endl); Teuchos::Array>> x(2); Teuchos::Array t(2); x[0] = rtv_unew.getVector(); t[0] = ts.t[1]; // note: x[0] is the NEW state x[1] = rtv_uold.getVector(); t[1] = ts.t[0]; // note: x[1] is the OLD state if (!usingAdjoint_) { int deriv_index = 0; // 0 = new state stepper_->computeStepJacobian(*Ju_op_, x, t, *(rtv_z.getVector()), 0, deriv_index); Ju_op_->apply(Thyra::TRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), 1.0, 0.0); } else { int deriv_index = 0; // 0 = new state stepperAdjoint_->computeStepJacobian(*adjointJu_op_, x, t, *(rtv_z.getVector()), 0, deriv_index); rtv_jv.getVector()->assign(0.0); adjointJu_op_->apply(Thyra::NOTRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), 1.0, 0.0); } } // applyAdjointJacobian_un template void ThyraProductME_ROL_DynamicConstraint::applyAdjointJacobian_z(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::ThyraVector& rtv_jv = dynamic_cast& >(jv); const ROL::ThyraVector& rtv_v = dynamic_cast& >(v); const ROL::ThyraVector& rtv_uold = dynamic_cast& >(uold); const ROL::ThyraVector& rtv_unew = dynamic_cast& >(unew); const ROL::ThyraVector& rtv_z = dynamic_cast& >(z); TEUCHOS_TEST_FOR_EXCEPTION( SMT::isnaninf(rtv_v.getVector()->norm_2()), std::logic_error, "ThyraProductME_ROL_DynamicConstraint::applyAdjointJacobian_z, the norm of rtv_v is not a number." << std::endl); Teuchos::Array>> x(2); Teuchos::Array t(2); x[0] = rtv_unew.getVector(); t[0] = ts.t[1]; // note: x[0] is the NEW state x[1] = rtv_uold.getVector(); t[1] = ts.t[0]; // note: x[1] is the OLD state stepper_->computeStepParamDeriv(*Jz_op_, x, t, *(rtv_z.getVector()), 0); Jz_op_->apply(Thyra::TRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), 1.0, 0.0); } // applyAdjointJacobian_z //---------------------------------------------------------------------------- // Inverses template void ThyraProductME_ROL_DynamicConstraint::applyInverseJacobian_un(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::ThyraVector& rtv_jv = dynamic_cast& >(jv); const ROL::ThyraVector& rtv_v = dynamic_cast& >(v); const ROL::ThyraVector& rtv_uold = dynamic_cast& >(uold); const ROL::ThyraVector& rtv_unew = dynamic_cast& >(unew); const ROL::ThyraVector& rtv_z = dynamic_cast& >(z); TEUCHOS_TEST_FOR_EXCEPTION( SMT::isnaninf(rtv_v.getVector()->norm_2()), std::logic_error, "ThyraProductME_ROL_DynamicConstraint::applyInverseJacobian_un, the norm of rtv_v is not a number." << std::endl); Teuchos::Array>> x(2); Teuchos::Array t(2); x[0] = rtv_unew.getVector(); t[0] = ts.t[1]; // note: x[0] is the NEW state x[1] = rtv_uold.getVector(); t[1] = ts.t[0]; // note: x[1] is the OLD state const Teuchos::Ptr> solveCriteria = Teuchos::null; stepper_->computeStepSolver(*Ju_, x, t, *(rtv_z.getVector()), 0); rtv_jv.getVector()->assign(0.0); Ju_->solve(Thyra::NOTRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), solveCriteria); } // applyInverseJacobian_un template void ThyraProductME_ROL_DynamicConstraint::applyInverseAdjointJacobian_un(ROL::Vector &jv, const ROL::Vector &v, const ROL::Vector &uold, const ROL::Vector &unew, const ROL::Vector &z, const ROL::TimeStamp &ts) const { ROL::ThyraVector& rtv_jv = dynamic_cast& >(jv); const ROL::ThyraVector& rtv_v = dynamic_cast& >(v); const ROL::ThyraVector& rtv_uold = dynamic_cast& >(uold); const ROL::ThyraVector& rtv_unew = dynamic_cast& >(unew); const ROL::ThyraVector& rtv_z = dynamic_cast& >(z); TEUCHOS_TEST_FOR_EXCEPTION( SMT::isnaninf(rtv_v.getVector()->norm_2()), std::logic_error, "ThyraProductME_ROL_DynamicConstraint::applyInverseAdjointJacobian_un, the norm of rtv_v is not a number." << std::endl); Teuchos::Array>> x(2); Teuchos::Array t(2); x[0] = rtv_unew.getVector(); t[0] = ts.t[1]; // note: x[0] is the NEW state x[1] = rtv_uold.getVector(); t[1] = ts.t[0]; // note: x[1] is the OLD state const Teuchos::Ptr> solveCriteria = Teuchos::null; if (!usingAdjoint_) { stepper_->computeStepSolver(*Ju_, x, t, *(rtv_z.getVector()), 0); Ju_->solve(Thyra::TRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), solveCriteria); } else { stepperAdjoint_->computeStepSolver(*adjointJu_, x, t, *(rtv_z.getVector()), 0); rtv_jv.getVector()->assign(0.0); adjointJu_->solve(Thyra::NOTRANS, *(rtv_v.getVector()), rtv_jv.getVector().ptr(), solveCriteria); } } // applyInverseAdjointJacobian_un } // namespace Piro #endif