// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef OPFACTORY_BINARY_STEFAN_BOLTZMANN_HPP #define OPFACTORY_BINARY_STEFAN_BOLTZMANN_HPP #include "ROL_Bounds.hpp" #include "ROL_Reduced_Objective_SimOpt.hpp" #include "ROL_PEBBL_IntegerProblemFactory.hpp" #include "../../TOOLS/pdeconstraint.hpp" #include "../../TOOLS/pdeobjective.hpp" #include "../../TOOLS/pdevector.hpp" //#include "../../TOOLS/meshmanager.hpp" #include "../../TOOLS/meshreader.hpp" #include "pde_stefan_boltzmann.hpp" #include "obj_stefan_boltzmann.hpp" #include "con_stefan_boltzmann.hpp" template class BinaryStefanBoltzmannFactory : public ROL::PEBBL::IntegerProblemFactory { private: mutable ROL::ParameterList pl_; ROL::Ptr> comm_; ROL::Ptr os_; ROL::Ptr> mesh_; ROL::Ptr> pde_; ROL::Ptr> qoi_; ROL::Ptr> assembler_; ROL::Ptr> obj_; ROL::Ptr> con_; ROL::Ptr> u_, p_, z_; ROL::Ptr> bnd_; ROL::Ptr> budget_con_; ROL::Ptr> budget_mul_; ROL::Ptr> budget_bnd_; bool useParam_, useBudget_; public: BinaryStefanBoltzmannFactory(ROL::ParameterList &pl, const ROL::Ptr &os) : pl_(pl), os_(os) { #ifndef HAVE_MPI comm_ = ROL::makePtr>(); initialize(); #endif } #ifdef HAVE_MPI void setCommunicator(const ROL::Ptr &comm) { comm_ = ROL::makePtr>(*comm); initialize(); } #endif // BinaryStefanBoltzmannFactory(ROL::ParameterList &pl, // const ROL::Ptr> &comm, // const ROL::Ptr &os) // : pl_(pl), comm_(comm), os_(os) { // // Create PDE Constraint // int probDim = pl_.sublist("Problem").get("Problem Dimension",2); // int nProcs = comm_->getSize(); // TEUCHOS_TEST_FOR_EXCEPTION(probDim<2||probDim>3, std::invalid_argument, // ">>> PDE-OPT/binary/stefan-boltzmann/example_01.cpp: Problem dim is not 2 or 3!"); // //if (probDim == 2) mesh_ = ROL::makePtr>(pl_); // //else if (probDim == 3) // mesh_ = ROL::makePtr>(pl_,nProcs); // pde_ = ROL::makePtr>(pl_); // con_ = ROL::makePtr>(pde_,mesh_,comm_,pl_,*os_); // con_->setSolveParameters(pl_); // assembler_ = ROL::dynamicPtrCast>(con_)->getAssembler(); // // Create template vectors // u_ = ROL::makePtr>(assembler_->createStateVector(),pde_,assembler_,pl_); // p_ = ROL::makePtr>(assembler_->createStateVector(),pde_,assembler_,pl_); // useParam_ = pl.sublist("Problem").get("Use Parametric Control", true); // bool init = pl_.sublist("Problem").get("Input Control",false); // std::string inCtrlName = pl_.sublist("Problem").get("Input Control Name","control.txt"); // if (useParam_) { // int nx = pl.sublist("Problem").get("Number X Control Patches", 4); // int ny = pl.sublist("Problem").get("Number Y Control Patches", 4); // z_ = ROL::makePtr>(nx*ny); // if (init) { // ROL::Ptr> zdata = ROL::dynamicPtrCast>(z_)->getVector(); // std::ifstream file; // file.open(inCtrlName); // for (int i = 0; i < ny; ++i) { // for (int j = 0; j < nx; ++j) { // file >> (*zdata)[j+i*nx]; // } // } // file.close(); // } // } // else { // z_ = ROL::makePtr>(assembler_->createControlVector(),pde_,assembler_,pl_); // if (init) { // ROL::Ptr> zdata = ROL::dynamicPtrCast>(z_)->getVector(); // assembler_->inputTpetraVector(zdata, inCtrlName); // } // } // if (!init) z_->setScalar(static_cast(1)); // u_->setScalar(static_cast(1)); // // Create objective function // qoi_ = ROL::makePtr>(pde_->getVolFE(),pl_); // obj_ = ROL::makePtr>(qoi_,assembler_); // // Create bound constraint // ROL::Ptr> zlop = z_->clone(), zhip = z_->clone(); // zlop->setScalar(static_cast(0)); // zhip->setScalar(static_cast(1)); // bnd_ = ROL::makePtr>(zlop,zhip); // // Create budget constraint // useBudget_ = pl.sublist("Problem").get("Use Budget Constraint",true); // if (useParam_ && useBudget_) { // budget_con_ = ROL::makePtr>(pl); // budget_mul_ = budget_con_->createMultiplier(); // budget_bnd_ = budget_con_->createBounds(); // } // } ROL::Ptr> build(void) { ROL::Ptr> z = z_->clone(); z->set(*z_); ROL::Ptr> obj = ROL::makePtr>(obj_, con_, u_, z_, p_, true, false); ROL::Ptr> problem = ROL::makePtr>(obj,z); problem->addBoundConstraint(bnd_); if (useParam_ && useBudget_) { problem->addLinearConstraint("Budget",budget_con_,budget_mul_,budget_bnd_); problem->setProjectionAlgorithm(pl_); } return problem; } ROL::Ptr> buildSolutionVector(void) { ROL::Ptr> z = z_->clone(); z->set(*z_); return z; } void getState(ROL::Ptr> &u, const ROL::Ptr> &z) const { Real tol(std::sqrt(ROL::ROL_EPSILON())); u = u_->clone(); ROL::Ptr> r = u->dual().clone(); if (useParam_ && useBudget_) con_->solve(*r,*u,*ROL::dynamicPtrCast>(z)->get(0),tol); else con_->solve(*r,*u,*z,tol); } ROL::Ptr> getAssembler(void) const { return assembler_; } void print(std::ostream &stream = std::cout) { assembler_->printMeshData(stream); } void printTpetraVector(const ROL::Ptr> &x, std::string name) const { assembler_->outputTpetraVector(ROL::dynamicPtrCast>(x)->getVector(),name); } void printControl(const ROL::Ptr> &x, std::string name) const { if (useParam_) { ROL::Ptr> data; if (useBudget_) data = ROL::dynamicPtrCast>(ROL::dynamicPtrCast>(x)->get(0))->getVector(); else data = ROL::dynamicPtrCast>(x)->getVector(); int dim = data->size(); std::ofstream file; file.open(name); for (int i = 0; i < dim; ++i) { file << std::scientific << std::setprecision(16); file << (*data)[i] << std::endl; } file.close(); } else { printTpetraVector(x,name); } } void check(std::ostream &stream = std::cout) { ROL::Ptr> z1 = z_->clone(); z1->randomize(); ROL::Ptr> z2 = z_->clone(); z2->randomize(); ROL::Ptr> u1 = u_->clone(); u1->randomize(500.0,2000.0); ROL::Ptr> u2 = u_->clone(); u2->randomize(); ROL::Ptr> p = p_->clone(); p->randomize(); ROL::Ptr> du = u_->dual().clone(); du->randomize(); ROL::Ptr> dz = z_->dual().clone(); dz->randomize(); con_->checkSolve(*u1,*z1,*du,true,stream); con_->checkAdjointConsistencyJacobian_1(*p,*u2,*u1,*z1,true,stream); con_->checkAdjointConsistencyJacobian_2(*p,*z2,*u1,*z1,true,stream); con_->checkInverseJacobian_1(*du,*u2,*u1,*z1,true,stream); con_->checkInverseAdjointJacobian_1(*p,*du,*u1,*z1,true,stream); con_->checkApplyJacobian_1(*u1,*z1,*u2,*du,true,stream); con_->checkApplyJacobian_2(*u1,*z1,*z2,*du,true,stream); con_->checkApplyAdjointHessian_11(*u1,*z1,*p,*u2,*du,true,stream); con_->checkApplyAdjointHessian_21(*u1,*z1,*p,*z2,*du,true,stream); con_->checkApplyAdjointHessian_12(*u1,*z1,*p,*u2,*dz,true,stream); con_->checkApplyAdjointHessian_22(*u1,*z1,*p,*z2,*dz,true,stream); obj_->checkGradient_1(*u1,*z1,*u2,true,stream); obj_->checkGradient_2(*u1,*z1,*z2,true,stream); obj_->checkHessVec_11(*u1,*z1,*u2,true,stream); obj_->checkHessVec_21(*u1,*z1,*u2,true,stream); obj_->checkHessVec_12(*u1,*z1,*z2,true,stream); obj_->checkHessVec_22(*u1,*z1,*z2,true,stream); } private: void initialize(void) { // Create PDE Constraint int probDim = pl_.sublist("Problem").get("Problem Dimension",2); int nProcs = comm_->getSize(); TEUCHOS_TEST_FOR_EXCEPTION(probDim<2||probDim>3, std::invalid_argument, ">>> PDE-OPT/binary/stefan-boltzmann/example_01.cpp: Problem dim is not 2 or 3!"); //if (probDim == 2) mesh_ = ROL::makePtr>(pl_); //else if (probDim == 3) mesh_ = ROL::makePtr>(pl_,nProcs); pde_ = ROL::makePtr>(pl_); con_ = ROL::makePtr>(pde_,mesh_,comm_,pl_,*os_); con_->setSolveParameters(pl_); assembler_ = ROL::dynamicPtrCast>(con_)->getAssembler(); // Create template vectors u_ = ROL::makePtr>(assembler_->createStateVector(),pde_,assembler_,pl_); p_ = ROL::makePtr>(assembler_->createStateVector(),pde_,assembler_,pl_); useParam_ = pl_.sublist("Problem").get("Use Parametric Control", true); bool init = pl_.sublist("Problem").get("Input Control",false); std::string inCtrlName = pl_.sublist("Problem").get("Input Control Name","control.txt"); if (useParam_) { int nx = pl_.sublist("Problem").get("Number X Control Patches", 4); int ny = pl_.sublist("Problem").get("Number Y Control Patches", 4); z_ = ROL::makePtr>(nx*ny); if (init) { ROL::Ptr> zdata = ROL::dynamicPtrCast>(z_)->getVector(); std::ifstream file; file.open(inCtrlName); for (int i = 0; i < ny; ++i) { for (int j = 0; j < nx; ++j) { file >> (*zdata)[j+i*nx]; } } file.close(); } } else { z_ = ROL::makePtr>(assembler_->createControlVector(),pde_,assembler_,pl_); if (init) { ROL::Ptr> zdata = ROL::dynamicPtrCast>(z_)->getVector(); assembler_->inputTpetraVector(zdata, inCtrlName); } } if (!init) z_->setScalar(static_cast(1)); u_->setScalar(static_cast(1)); // Create objective function qoi_ = ROL::makePtr>(pde_->getVolFE(),pl_); obj_ = ROL::makePtr>(qoi_,assembler_); // Create bound constraint ROL::Ptr> zlop = z_->clone(), zhip = z_->clone(); zlop->setScalar(static_cast(0)); zhip->setScalar(static_cast(1)); bnd_ = ROL::makePtr>(zlop,zhip); // Create budget constraint useBudget_ = pl_.sublist("Problem").get("Use Budget Constraint",true); if (useParam_ && useBudget_) { budget_con_ = ROL::makePtr>(pl_); budget_mul_ = budget_con_->createMultiplier(); budget_bnd_ = budget_con_->createBounds(); } } }; #endif