// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file pde.hpp \brief Implements the local PDE interface for the Poisson control problem. */ #ifndef PDE_POISSON_HPP #define PDE_POISSON_HPP #include "../TOOLS/pde.hpp" #include "../TOOLS/fe.hpp" #include "Intrepid_HGRAD_LINE_Cn_FEM.hpp" #include "Intrepid_HGRAD_QUAD_C1_FEM.hpp" #include "Intrepid_HGRAD_QUAD_C2_FEM.hpp" #include "Intrepid_HGRAD_HEX_C1_FEM.hpp" #include "Intrepid_HGRAD_HEX_C2_FEM.hpp" #include "Intrepid_DefaultCubatureFactory.hpp" #include "Intrepid_FunctionSpaceTools.hpp" #include "Intrepid_CellTools.hpp" #include "ROL_Ptr.hpp" template class PDE_Poisson : public PDE { private: // Finite element basis information ROL::Ptr>> basisPtr_; std::vector>>> basisPtrs_; // Cell cubature information ROL::Ptr> cellCub_; // Cell node information ROL::Ptr> volCellNodes_; std::vector>>> bdryCellNodes_; std::vector>> bdryCellLocIds_; // Finite element definition ROL::Ptr> fe_vol_; // Local degrees of freedom on boundary, for each side of the reference cell (first index). std::vector> fidx_; // Coordinates of degrees freedom on boundary cells. // Indexing: [sideset number][local side id](cell number, value at dof) std::vector>>> bdryCellDofValues_; bool useStateRiesz_; bool useControlRiesz_; Real alpha_; Real DirichletFunc(const std::vector & coords, int sideset, int locSideId) const { return 0; } Real evaluateRHS(const std::vector &x) const { const Real pi(M_PI), eight(8); Real s1(1), s2(1); int dim = x.size(); for (int i=0; i> &rhs) const { // GET DIMENSIONS int c = fe_vol_->gradN()->dimension(0); int p = fe_vol_->gradN()->dimension(2); int d = fe_vol_->gradN()->dimension(3); std::vector pt(d); for (int i = 0; i < c; ++i) { for (int j = 0; j < p; ++j) { for ( int k = 0; k < d; ++k) { pt[k] = (*fe_vol_->cubPts())(i,j,k); } // Compute forcing term f (*rhs)(i,j) = -evaluateRHS(pt); } } } public: PDE_Poisson(Teuchos::ParameterList &parlist) { // Finite element fields. int basisOrder = parlist.sublist("Problem").get("Basis Order",1); int cubDegree = parlist.sublist("Problem").get("Cubature Degree",4); int probDim = parlist.sublist("Problem").get("Problem Dimension",2); if (probDim > 3 || probDim < 1) { TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, ">>> PDE-OPT/poisson/pde_poisson.hpp: Problem dimension is not 1, 2 or 3!"); } if (basisOrder > 2 || basisOrder < 1) { TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, ">>> PDE-OPT/poisson/pde_poisson.hpp: Basis order is not 1 or 2!"); } if (probDim == 1) { basisPtr_ = ROL::makePtr>>(basisOrder,Intrepid::POINTTYPE_EQUISPACED); } else if (probDim == 2) { if (basisOrder == 1) { basisPtr_ = ROL::makePtr>>(); } else if (basisOrder == 2) { basisPtr_ = ROL::makePtr>>(); } } else if (probDim == 3) { if (basisOrder == 1) { basisPtr_ = ROL::makePtr>>(); } else if (basisOrder == 2) { basisPtr_ = ROL::makePtr>>(); } } basisPtrs_.clear(); basisPtrs_.push_back(basisPtr_); // Quadrature rules. shards::CellTopology cellType = basisPtr_->getBaseCellTopology(); Intrepid::DefaultCubatureFactory cubFactory; cellCub_ = cubFactory.create(cellType, cubDegree); // Problem data. useStateRiesz_ = parlist.sublist("Problem").get("Use State Riesz Map", true); // use Riesz map for state variables? useControlRiesz_ = parlist.sublist("Problem").get("Use Control Riesz Map", true); // use Riesz map for control variables? alpha_ = parlist.sublist("Problem").get("Control penalty parameter",1e-2); } void residual(ROL::Ptr> & res, const ROL::Ptr> & u_coeff, const ROL::Ptr> & z_coeff = ROL::nullPtr, const ROL::Ptr> & z_param = ROL::nullPtr) { // GET DIMENSIONS int c = u_coeff->dimension(0); int p = cellCub_->getNumPoints(); int f = basisPtr_->getCardinality(); int d = cellCub_->getDimension(); // INITIALIZE RESIDUAL res = ROL::makePtr>(c, f); // COMPUTE STIFFNESS TERM ROL::Ptr> gradU_eval = ROL::makePtr>(c, p, d); fe_vol_->evaluateGradient(gradU_eval, u_coeff); Intrepid::FunctionSpaceTools::integrate(*res, *gradU_eval, *(fe_vol_->gradNdetJ()), Intrepid::COMP_CPP, false); // COMPUTE RHS ROL::Ptr> rhs = ROL::makePtr>(c, p); computeRHS(rhs); Intrepid::FunctionSpaceTools::integrate(*res, *rhs, *(fe_vol_->NdetJ()), Intrepid::COMP_CPP, true); // ADD CONTROL TERM TO RESIDUAL if ( z_coeff != ROL::nullPtr ) { ROL::Ptr> valZ_eval = ROL::makePtr>(c, p); fe_vol_->evaluateValue(valZ_eval, z_coeff); Intrepid::RealSpaceTools::scale(*valZ_eval,static_cast(-1)); Intrepid::FunctionSpaceTools::integrate(*res, *valZ_eval, *(fe_vol_->NdetJ()), Intrepid::COMP_CPP, true); } // APPLY DIRICHLET CONDITIONS int numSideSets = bdryCellLocIds_.size(); if (numSideSets > 0) { for (int i = 0; i < numSideSets; ++i) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numBdryDofs = fidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numBdryDofs; ++l) { //std::cout << "\n j=" << j << " l=" << l << " " << fidx_[j][l]; (*res)(cidx,fidx_[j][l]) = (*u_coeff)(cidx,fidx_[j][l]) - (*bdryCellDofValues_[i][j])(k,fidx_[j][l]); } } } } } } void Jacobian_1(ROL::Ptr> & jac, const ROL::Ptr> & u_coeff, const ROL::Ptr> & z_coeff = ROL::nullPtr, const ROL::Ptr> & z_param = ROL::nullPtr) { // GET DIMENSIONS int c = u_coeff->dimension(0); int f = basisPtr_->getCardinality(); // INITILAIZE JACOBIAN jac = ROL::makePtr>(c, f, f); // COMPUTE STIFFNESS TERM Intrepid::FunctionSpaceTools::integrate(*jac, *(fe_vol_->gradN()), *(fe_vol_->gradNdetJ()), Intrepid::COMP_CPP, false); // APPLY DIRICHLET CONDITIONS int numSideSets = bdryCellLocIds_.size(); if (numSideSets > 0) { for (int i = 0; i < numSideSets; ++i) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numBdryDofs = fidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numBdryDofs; ++l) { //std::cout << "\n j=" << j << " l=" << l << " " << fidx[j][l]; for (int m = 0; m < f; ++m) { (*jac)(cidx,fidx_[j][l],m) = static_cast(0); } (*jac)(cidx,fidx_[j][l],fidx_[j][l]) = static_cast(1); } } } } } } void Jacobian_2(ROL::Ptr> & jac, const ROL::Ptr> & u_coeff, const ROL::Ptr> & z_coeff = ROL::nullPtr, const ROL::Ptr> & z_param = ROL::nullPtr) { if ( z_coeff != ROL::nullPtr ) { // GET DIMENSIONS int c = u_coeff->dimension(0); int f = basisPtr_->getCardinality(); // INITIALIZE JACOBIAN jac = ROL::makePtr>(c, f, f); // ADD CONTROL TERM Intrepid::FunctionSpaceTools::integrate(*jac, *(fe_vol_->N()), *(fe_vol_->NdetJ()), Intrepid::COMP_CPP, false); Intrepid::RealSpaceTools::scale(*jac,static_cast(-1)); // APPLY DIRICHLET CONDITIONS int numSideSets = bdryCellLocIds_.size(); if (numSideSets > 0) { for (int i = 0; i < numSideSets; ++i) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numBdryDofs = fidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numBdryDofs; ++l) { //std::cout << "\n j=" << j << " l=" << l << " " << fidx[j][l]; for (int m = 0; m < f; ++m) { (*jac)(cidx,fidx_[j][l],m) = static_cast(0); } } } } } } } else { throw Exception::Zero(">>> (PDE_Poisson::Jacobian_2): Jacobian is zero."); } } void Hessian_11(ROL::Ptr> & hess, const ROL::Ptr> & l_coeff, const ROL::Ptr> & u_coeff, const ROL::Ptr> & z_coeff = ROL::nullPtr, const ROL::Ptr> & z_param = ROL::nullPtr) { throw Exception::Zero(">>> (PDE_Poisson::Hessian_11): Hessian is zero."); } void Hessian_12(ROL::Ptr> & hess, const ROL::Ptr> & l_coeff, const ROL::Ptr> & u_coeff, const ROL::Ptr> & z_coeff = ROL::nullPtr, const ROL::Ptr> & z_param = ROL::nullPtr) { throw Exception::Zero(">>> (PDE_Poisson::Hessian_12): Hessian is zero."); } void Hessian_21(ROL::Ptr> & hess, const ROL::Ptr> & l_coeff, const ROL::Ptr> & u_coeff, const ROL::Ptr> & z_coeff = ROL::nullPtr, const ROL::Ptr> & z_param = ROL::nullPtr) { throw Exception::Zero(">>> (PDE_Poisson::Hessian_21): Hessian is zero."); } void Hessian_22(ROL::Ptr> & hess, const ROL::Ptr> & l_coeff, const ROL::Ptr> & u_coeff, const ROL::Ptr> & z_coeff = ROL::nullPtr, const ROL::Ptr> & z_param = ROL::nullPtr) { throw Exception::Zero(">>> (PDE_Poisson::Hessian_22): Hessian is zero."); } void RieszMap_1(ROL::Ptr> & riesz) { // Optionally disable Riesz map ... if (!useStateRiesz_) { throw Exception::NotImplemented(">>> (PDE_Poisson::RieszMap_1): Not implemented."); } // ...otherwise ... int c = fe_vol_->N()->dimension(0); int f = fe_vol_->N()->dimension(1); // INITILAIZE JACOBIAN riesz = ROL::makePtr>(c, f, f); *riesz = *fe_vol_->stiffMat(); Intrepid::RealSpaceTools::add(*riesz,*(fe_vol_->massMat())); } void RieszMap_2(ROL::Ptr> & riesz) { // Optionally disable Riesz map ... if (!useControlRiesz_) { throw Exception::NotImplemented(">>> (PDE_Poisson::RieszMap_2): Not implemented."); } // ...otherwise ... int c = fe_vol_->N()->dimension(0); int f = fe_vol_->N()->dimension(1); // INITILAIZE JACOBIAN riesz = ROL::makePtr>(c, f, f); *riesz = *fe_vol_->massMat(); } std::vector>>> getFields() { return basisPtrs_; } void setCellNodes(const ROL::Ptr> &volCellNodes, const std::vector>>> &bdryCellNodes, const std::vector>> &bdryCellLocIds) { volCellNodes_ = volCellNodes; bdryCellNodes_ = bdryCellNodes; bdryCellLocIds_ = bdryCellLocIds; // Finite element definition. fe_vol_ = ROL::makePtr>(volCellNodes_,basisPtr_,cellCub_); // Set local boundary DOFs. fidx_ = fe_vol_->getBoundaryDofs(); // Compute Dirichlet values at DOFs. int d = basisPtr_->getBaseCellTopology().getDimension(); int numSidesets = bdryCellLocIds_.size(); bdryCellDofValues_.resize(numSidesets); for (int i=0; igetCardinality(); bdryCellDofValues_[i][j] = ROL::makePtr>(c, f); ROL::Ptr> coords = ROL::makePtr>(c, f, d); if (c > 0) { fe_vol_->computeDofCoords(coords, bdryCellNodes_[i][j]); } for (int k=0; k dofpoint(d); for (int m=0; m> getFE(void) const { return fe_vol_; } }; // PDE_Poisson #endif