// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file pde_navier-stokes.hpp \brief Implements the local PDE interface for the Navier-Stokes control problem. */ #ifndef STEADYPDE_DYNAMIC_NAVIERSTOKES_HPP #define STEADYPDE_DYNAMIC_NAVIERSTOKES_HPP #include "pde_navier-stokes.hpp" template class SteadyPDE_NavierStokes : public PDE { private: // Cell node information std::vector>> bdryCellLocIds_; std::vector>>> bdryCellNodes_; // Coordinates of degrees freedom on boundary cells. // Indexing: [sideset number][local side id](cell number, value at dof) std::vector>>> bdryCellDofValues_; // Finite element definition ROL::Ptr> feVel_; ROL::Ptr> fePrs_; // Local degrees of freedom on boundary, for each side of the reference cell (first index). std::vector> fvidx_; std::vector> fpidx_; // Field pattern, offsets, etc. ROL::Ptr> fieldHelper_; ROL::Ptr> pde_; Real cx_, cy_; // Cylinder center bool useParabolicInflow_; bool useNonPenetratingWalls_; public: SteadyPDE_NavierStokes(Teuchos::ParameterList &parlist) { pde_ = ROL::makePtr>(parlist); useParabolicInflow_ = parlist.sublist("Problem").get("Use Parabolic Inflow", true); useNonPenetratingWalls_ = parlist.sublist("Problem").get("Use Non-Penetrating Walls", false); cx_ = static_cast(-3); cy_ = static_cast(0); } void residual(ROL::Ptr> & res, const ROL::Ptr> & u_coeff, const ROL::Ptr> & z_coeff = ROL::nullPtr, const ROL::Ptr> & z_param = ROL::nullPtr) { int d = feVel_->gradN()->dimension(3); pde_->residual(res, u_coeff, z_coeff, z_param); std::vector>> U, R; fieldHelper_->splitFieldCoeff(U, u_coeff); fieldHelper_->splitFieldCoeff(R, res); // Apply boundary conditions int numSideSets = bdryCellLocIds_.size(); Real bv(0); if (numSideSets > 0) { // APPLY DIRICHLET CONDITIONS for (int i = 0; i < numSideSets; ++i) { // Apply dirichlet conditions on inflow, top and bottom wall if (i==0 || i==2) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numBdryDofs = fvidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numBdryDofs; ++l) { if (useNonPenetratingWalls_) { (*R[1])(cidx,fvidx_[j][l]) = (*U[1])(cidx,fvidx_[j][l]); } else { for (int m=0; m < d; ++m) { bv = (*bdryCellDofValues_[i][j])(k,fvidx_[j][l],m); (*R[m])(cidx,fvidx_[j][l]) = (*U[m])(cidx,fvidx_[j][l]) - bv; } } } } } } // Apply dirichlet conditions on inflow, top and bottom wall if (i==3) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numBdryDofs = fvidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numBdryDofs; ++l) { for (int m=0; m < d; ++m) { bv = (*bdryCellDofValues_[i][j])(k,fvidx_[j][l],m); (*R[m])(cidx,fvidx_[j][l]) = (*U[m])(cidx,fvidx_[j][l]) - bv; } } } } } // Apply Dirichlet control on cylinder if (i==4) { Real omega(0); int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numBdryDofs = fvidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numBdryDofs; ++l) { for (int m=0; m < d; ++m) { (*R[m])(cidx,fvidx_[j][l]) = (*U[m])(cidx,fvidx_[j][l]); } } } } } } } // Combine the residuals. fieldHelper_->combineFieldCoeff(res, R); } 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) { const Real zero(0), one(1); int fv = feVel_->gradN()->dimension(1); int fp = fePrs_->gradN()->dimension(1); int d = feVel_->gradN()->dimension(3); pde_->Jacobian_1(jac, u_coeff, z_coeff, z_param); std::vector>>> J; fieldHelper_->splitFieldCoeff(J, jac); // APPLY BOUNDARY CONDITIONS int numSideSets = bdryCellLocIds_.size(); if (numSideSets > 0) { for (int i = 0; i < numSideSets; ++i) { if (i==0 || i==2) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numBdryDofs = fvidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numBdryDofs; ++l) { for (int m=0; m < fv; ++m) { if (useNonPenetratingWalls_) { for (int p=0; p < d; ++p) { (*J[1][p])(cidx,fvidx_[j][l],m) = zero; } (*J[1][1])(cidx,fvidx_[j][l],fvidx_[j][l]) = one; } else { for (int n=0; n < d; ++n) { for (int p=0; p < d; ++p) { (*J[n][p])(cidx,fvidx_[j][l],m) = zero; } (*J[n][n])(cidx,fvidx_[j][l],fvidx_[j][l]) = one; } } } for (int m=0; m < fp; ++m) { if (useNonPenetratingWalls_) { (*J[1][2])(cidx,fvidx_[j][l],m) = zero; } else { for (int n=0; n < d; ++n) { (*J[n][2])(cidx,fvidx_[j][l],m) = zero; } } } } } } } if (i==3 || i==4) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numBdryDofs = fvidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numBdryDofs; ++l) { for (int m=0; m < fv; ++m) { for (int n=0; n < d; ++n) { for (int p=0; p < d; ++p) { (*J[n][p])(cidx,fvidx_[j][l],m) = zero; } (*J[n][n])(cidx,fvidx_[j][l],fvidx_[j][l]) = one; } } for (int m=0; m < fp; ++m) { for (int n=0; n < d; ++n) { (*J[n][2])(cidx,fvidx_[j][l],m) = zero; } } } } } } } } // Combine the Jacobians. fieldHelper_->combineFieldCoeff(jac, J); } 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) { throw Exception::Zero(">>> (SteadyPDE_NavierStokes::Jacobian_2): Jacobian is zero."); } void Jacobian_3(std::vector>> & jac, const ROL::Ptr> & u_coeff, const ROL::Ptr> & z_coeff = ROL::nullPtr, const ROL::Ptr> & z_param = ROL::nullPtr) { throw Exception::Zero(">>> (SteadyPDE_NavierStokes::Jacobian_3): 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) { const Real zero(0); int d = feVel_->gradN()->dimension(3); std::vector>> L; ROL::Ptr> l0_coeff; fieldHelper_->splitFieldCoeff(L, l_coeff); // Apply Dirichlet conditions int numSideSets = bdryCellLocIds_.size(); if (numSideSets > 0) { for (int i = 0; i < numSideSets; ++i) { if (i==0 || i==2) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numBdryDofs = fvidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numBdryDofs; ++l) { if (useNonPenetratingWalls_) { (*L[1])(cidx,fvidx_[j][l]) = zero; } else { for (int m=0; m < d; ++m) { (*L[m])(cidx,fvidx_[j][l]) = zero; } } } } } } if (i==3 || i==4) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numBdryDofs = fvidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numBdryDofs; ++l) { for (int m=0; m < d; ++m) { (*L[m])(cidx,fvidx_[j][l]) = zero; } } } } } } } fieldHelper_->combineFieldCoeff(l0_coeff, L); pde_->Hessian_11(hess, l0_coeff, u_coeff, z_coeff, z_param); } 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(">>> (SteadyPDE_NavierStokes::Hessian_12): Hessian is zero."); } void Hessian_13(std::vector>> & 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(">>> (SteadyPDE_NavierStokes::Hessian_13): 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(">>> (SteadyPDE_NavierStokes::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(">>> (SteadyPDE_NavierStokes::Hessian_22): Hessian is zero."); } void Hessian_23(std::vector>> & 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(">>> (SteadyPDE_NavierStokes::Hessian_23): Hessian is zero."); } void Hessian_31(std::vector>> & 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(">>> (SteadyPDE_NavierStokes::Hessian_31): Hessian is zero."); } void Hessian_32(std::vector>> & 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(">>> (SteadyPDE_NavierStokes::Hessian_32): Hessian is zero."); } void Hessian_33(std::vector>>> & 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(">>> (SteadyPDE_NavierStokes::Hessian_33): Hessian is zero."); } void RieszMap_1(ROL::Ptr> & riesz) { pde_->RieszMap_1(riesz); } void RieszMap_2(ROL::Ptr> & riesz) { pde_->RieszMap_2(riesz); } std::vector>>> getFields() { return pde_->getFields(); } void setCellNodes(const ROL::Ptr> &volCellNodes, const std::vector>>> &bdryCellNodes, const std::vector>> &bdryCellLocIds) { pde_->setCellNodes(volCellNodes,bdryCellNodes,bdryCellLocIds); bdryCellLocIds_ = bdryCellLocIds; bdryCellNodes_ = bdryCellNodes; feVel_ = pde_->getVelocityFE(); fePrs_ = pde_->getPressureFE(); fvidx_ = feVel_->getBoundaryDofs(); fpidx_ = fePrs_->getBoundaryDofs(); computeDirichlet(); } void setFieldPattern(const std::vector> & fieldPattern) { pde_->setFieldPattern(fieldPattern); fieldHelper_ = pde_->getFieldHelper(); } const ROL::Ptr> getVelocityFE(void) const { return feVel_; } const ROL::Ptr> getPressureFE(void) const { return fePrs_; } const std::vector>> getVelocityBdryFE(void) const { return pde_->getVelocityBdryFE(); } const std::vector> getBdryCellLocIds(const int sideset = -1) const { return pde_->getBdryCellLocIds(sideset); } const ROL::Ptr> getFieldHelper(void) const { return fieldHelper_; } private: Real DirichletFunc(const std::vector & coords, int sideset, int locSideId, int dir) const { const Real one(1); const Real x = coords[0], y = coords[1]; Real val(0); if ((sideset!=4) && (dir==0)) { val = (useParabolicInflow_ ? (one + y) * (one - y) : one); } if (sideset==4) { Real tx = y-cy_, ty = cx_-x; val = (dir==0 ? tx : ty); } return val; } void computeDirichlet(void) { // Compute Dirichlet values at DOFs. int d = feVel_->gradN()->dimension(3); int numSidesets = bdryCellLocIds_.size(); bdryCellDofValues_.resize(numSidesets); for (int i=0; igetFields())[0]->getCardinality(); bdryCellDofValues_[i][j] = ROL::makePtr>(c, f, d); ROL::Ptr > coords = ROL::makePtr>(c, f, d); if (c > 0) { feVel_->computeDofCoords(coords, bdryCellNodes_[i][j]); } for (int k=0; k dofpoint(d); for (int m=0; m