// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file pde_stokes.hpp \brief Implements the local PDE interface for the Stokes control problem. */ #ifndef PDE_STOKES_HPP #define PDE_STOKES_HPP #include "../TOOLS/pde.hpp" #include "../TOOLS/fe.hpp" #include "../TOOLS/fieldhelper.hpp" #include "Intrepid_HGRAD_QUAD_C1_FEM.hpp" #include "Intrepid_HGRAD_QUAD_C2_FEM.hpp" #include "Intrepid_DefaultCubatureFactory.hpp" #include "Intrepid_FunctionSpaceTools.hpp" #include "Intrepid_CellTools.hpp" #include "ROL_Ptr.hpp" template class PDE_Stokes : public PDE { private: // Finite element basis information ROL::Ptr > > basisPtrVel_; ROL::Ptr > > basisPtrPrs_; std::vector > > > basisPtrs_; // Cell cubature information ROL::Ptr > cellCub_; ROL::Ptr > bdryCub_; // Cell node information ROL::Ptr > volCellNodes_; std::vector > > > bdryCellNodes_; std::vector > > bdryCellLocIds_; // Finite element definition ROL::Ptr > feVel_; ROL::Ptr > fePrs_; std::vector > > > feVelBdry_; std::vector > > > fePrsBdry_; // Local degrees of freedom on boundary, for each side of the reference cell (first index). std::vector > fvidx_; std::vector > fpidx_; // Coordinates of degrees freedom on boundary cells. // Indexing: [sideset number][local side id](cell number, value at dof) std::vector > > > bdryCellVDofValues_; // Field pattern, offsets, etc. std::vector > fieldPattern_; // local Field/DOF pattern; set from DOF manager int numFields_; // number of fields (equations in the PDE) int numDofs_; // total number of degrees of freedom for all (local) fields std::vector offset_; // for each field, a counting offset std::vector numFieldDofs_; // for each field, number of degrees of freedom // Problem parameters. Real Re_; bool pinPressure_, dirType_; ROL::Ptr > fieldHelper_; Real velocityDirichletFunc(const std::vector & coords, int sideset, int locSideId, int dir) const { Real val(0); if (dirType_ == 0) { if ((sideset==3) && (dir==0)) { val = static_cast(1); } } else if (dirType_ == 1) { Real one(1); if ((sideset==1) && (dir==0)) { val = coords[1]*(one-coords[1]); } else if ((sideset==2) && (dir==0)) { val = coords[1]*(one-coords[1]); } } return val; } void computeDirichlet(void) { // Compute Dirichlet values at DOFs. int d = basisPtrVel_->getBaseCellTopology().getDimension(); int fv = basisPtrVel_->getCardinality(); int numSidesets = bdryCellLocIds_.size(); bdryCellVDofValues_.resize(numSidesets); for (int i=0; i>(c, fv, d); ROL::Ptr > Vcoords, Tcoords; Vcoords = ROL::makePtr>(c, fv, d); if (c > 0) { feVel_->computeDofCoords(Vcoords, bdryCellNodes_[i][j]); } for (int k=0; k dofpoint(d); //std::cout << "Sideset " << i << " LocalSide " << j << " Cell " << k << " Field " << l << " Coord "; for (int m=0; m >>(); basisPtrPrs_ = ROL::makePtr >>(); // Volume quadrature rules. shards::CellTopology cellType = basisPtrVel_->getBaseCellTopology(); // get the cell type from any basis Intrepid::DefaultCubatureFactory cubFactory; // create cubature factory int cubDegree = parlist.sublist("Problem").get("Cubature Degree", 4); // set cubature degree, e.g., 4 cellCub_ = cubFactory.create(cellType, cubDegree); // create default cubature // Boundary quadrature rules. int d = cellType.getDimension(); shards::CellTopology bdryCellType = cellType.getCellTopologyData(d-1, 0); int bdryCubDegree = parlist.sublist("Problem").get("Boundary Cubature Degree",4); // set cubature degree, e.g., 4 bdryCub_ = cubFactory.create(bdryCellType, bdryCubDegree); // Fill finite element basis container basisPtrs_.clear(); for (int i = 0; i < d; ++i) { basisPtrs_.push_back(basisPtrVel_); // Velocity } basisPtrs_.push_back(basisPtrPrs_); // Pressure // Reynold's Number Re_ = parlist.sublist("Problem").get("Reynolds Number",100.0); // Pin pressure pinPressure_ = parlist.sublist("Problem").get("Pin Pressure",true); // Dirichlet Type dirType_ = parlist.sublist("Problem").get("Dirichlet Type",0); numDofs_ = 0; numFields_ = basisPtrs_.size(); offset_.resize(numFields_); numFieldDofs_.resize(numFields_); for (int i=0; igetCardinality(); } numFieldDofs_[i] = basisPtrs_[i]->getCardinality(); numDofs_ += numFieldDofs_[i]; } } void residual(ROL::Ptr > & res, const ROL::Ptr > & u_coeff, const ROL::Ptr > & z_coeff = ROL::nullPtr, const ROL::Ptr > & z_param = ROL::nullPtr) { // Retrieve dimensions. int c = u_coeff->dimension(0); int p = cellCub_->getNumPoints(); int fv = basisPtrVel_->getCardinality(); int fp = basisPtrPrs_->getCardinality(); int d = cellCub_->getDimension(); // Initialize residuals. std::vector > > R(d+1); for (int i = 0; i < d; ++i) { R[i] = ROL::makePtr>(c, fv); } R[d] = ROL::makePtr>(c, fp); // Split u_coeff into components. std::vector > > U; fieldHelper_->splitFieldCoeff(U, u_coeff); // Evaluate/interpolate finite element fields on cells. ROL::Ptr > valPres_eval; valPres_eval = ROL::makePtr>(c, p); fePrs_->evaluateValue(valPres_eval, U[d]); // Evaluate/interpolate gradient of finite element fields on cells. std::vector > > gradVel_vec(d); for (int i = 0; i < d; ++i) { gradVel_vec[i] = ROL::makePtr>(c, p, d); feVel_->evaluateGradient(gradVel_vec[i], U[i]); } // Assemble the velocity vector and its divergence. ROL::Ptr > divVel_eval; divVel_eval = ROL::makePtr>(c, p); for (int i = 0; i < c; ++i) { for (int j = 0; j < p; ++j) { (*divVel_eval)(i,j) = static_cast(0); for (int k = 0; k < d; ++k) { (*divVel_eval)(i,j) += (*gradVel_vec[k])(i,j,k); } } } // Scale pressure and velocity gradient const Real zero(0), one(1); for (int i = 0; i < d; ++i) { // Multiply velocity gradients with viscosity. Intrepid::RealSpaceTools::scale(*gradVel_vec[i], one/Re_); } // Negative pressure Intrepid::RealSpaceTools::scale(*valPres_eval, -one); /**************************************************************************/ /*** EVALUATE WEAK FORM OF RESIDUAL ***************************************/ /**************************************************************************/ // Velocity equation. for (int i = 0; i < d; ++i) { Intrepid::FunctionSpaceTools::integrate(*R[i], *gradVel_vec[i], // nu gradUX *(feVel_->gradNdetJ()), // gradPhi Intrepid::COMP_CPP, false); Intrepid::FunctionSpaceTools::integrate(*R[i], *valPres_eval, // p *(feVel_->DNDdetJ(i)), // dPhi/dx Intrepid::COMP_CPP, true); } // Pressure equation. Intrepid::FunctionSpaceTools::integrate(*R[d], *divVel_eval, // divU *(fePrs_->NdetJ()), // Phi Intrepid::COMP_CPP, false); Intrepid::RealSpaceTools::scale(*R[d], -one); /**************************************************************************/ /*** APPLY BOUNDARY CONDITIONS ********************************************/ /**************************************************************************/ // --> No slip boundaries: i=0,1,3 // --> Lid boundaries: i=2 // --> Pressure pin: i=4 int numSideSets = bdryCellLocIds_.size(); if (numSideSets > 0) { for (int i = 0; i < numSideSets; ++i) { // Velocity Boundary Conditions if (i!=4) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numVBdryDofs = fvidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numVBdryDofs; ++l) { for (int m = 0; m < d; ++m) { (*R[m])(cidx,fvidx_[j][l]) = (*U[m])(cidx,fvidx_[j][l]) - (*bdryCellVDofValues_[i][j])(k,fvidx_[j][l],m); } } } } } // Pressure pinning if (i==4 && pinPressure_) { Real val = (dirType_ == 1) ? one/Re_ : zero; int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); for (int k = 0; k < numCellsSide; ++k) { int l = 0, cidx = bdryCellLocIds_[i][j][k]; (*R[d])(cidx,fpidx_[j][l]) = (*U[d])(cidx,fpidx_[j][l]) - val; } } } } } // 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) { // Retrieve dimensions. int c = u_coeff->dimension(0); int fv = basisPtrVel_->getCardinality(); int fp = basisPtrPrs_->getCardinality(); int d = cellCub_->getDimension(); // Initialize jacobians. std::vector > > > J(d+1); for (int i = 0; i < d+1; ++i) { J[i].resize(d+1); } for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { J[i][j] = ROL::makePtr>(c, fv, fv); } J[d][i] = ROL::makePtr>(c, fp, fv); J[i][d] = ROL::makePtr>(c, fv, fp); } J[d][d] = ROL::makePtr>(c, fp, fp); // Multiply velocity gradients with viscosity. const Real zero(0), one(1); Intrepid::FieldContainer nuGradPhi_eval(*feVel_->gradN()); Intrepid::RealSpaceTools::scale(nuGradPhi_eval, one/Re_); // Negative pressure basis. Intrepid::FieldContainer negPrsPhi(*fePrs_->N()); Intrepid::RealSpaceTools::scale(negPrsPhi, -one); /*** Evaluate weak form of the Jacobian. ***/ for (int i = 0; i < d; ++i) { // Velocity components Intrepid::FunctionSpaceTools::integrate(*J[i][i], nuGradPhi_eval, // nu gradPhi *feVel_->gradNdetJ(), // gradPhi Intrepid::COMP_CPP, true); // Pressure components Intrepid::FunctionSpaceTools::integrate(*J[i][d], *feVel_->DNDdetJ(i), // dPhi/dx negPrsPhi, // -Phi Intrepid::COMP_CPP, false); Intrepid::FunctionSpaceTools::integrate(*J[d][i], *fePrs_->NdetJ(), // Phi *feVel_->DND(i), // dPhi/dx Intrepid::COMP_CPP, false); Intrepid::RealSpaceTools::scale(*J[d][i], -one); } // APPLY BOUNDARY CONDITIONS int numSideSets = bdryCellLocIds_.size(); if (numSideSets > 0) { // DIRICHLET CONDITIONS for (int i = 0; i < numSideSets; ++i) { // Velocity Boundary Conditions if (i!=4) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); int numVBdryDofs = fvidx_[j].size(); for (int k = 0; k < numCellsSide; ++k) { int cidx = bdryCellLocIds_[i][j][k]; for (int l = 0; l < numVBdryDofs; ++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 < d; ++m) { for (int n = 0; n < fp; ++n) { (*J[m][d])(cidx,fvidx_[j][l],n) = zero; } } } } } } // Pressure pinning if (i==4 && pinPressure_) { int numLocalSideIds = bdryCellLocIds_[i].size(); for (int j = 0; j < numLocalSideIds; ++j) { int numCellsSide = bdryCellLocIds_[i][j].size(); for (int k = 0; k < numCellsSide; ++k) { int l = 0, cidx = bdryCellLocIds_[i][j][k]; for (int m = 0; m < fv; ++m) { for (int n = 0; n < d; ++n) { (*J[d][n])(cidx,fpidx_[j][l],m) = zero; } } for (int m = 0; m < fp; ++m) { (*J[d][d])(cidx,fpidx_[j][l],m) = zero; } (*J[d][d])(cidx,fpidx_[j][l],fpidx_[j][l]) = one; } } } } } // 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) { // Retrieve dimensions. int c = u_coeff->dimension(0); int fv = basisPtrVel_->getCardinality(); int fp = basisPtrPrs_->getCardinality(); int d = cellCub_->getDimension(); // Initialize jacobians. std::vector > > > J(d+1); for (int i = 0; i < d+1; ++i) { J[i].resize(d+1); } for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { J[i][j] = ROL::makePtr>(c, fv, fv); } J[d][i] = ROL::makePtr>(c, fp, fv); J[i][d] = ROL::makePtr>(c, fv, fp); } J[d][d] = ROL::makePtr>(c, fp, fp); // Combine the jacobians. fieldHelper_->combineFieldCoeff(jac, J); } 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_Stokes::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_Stokes::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_Stokes::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_Stokes::Hessian_22): Hessian is zero."); } void RieszMap_1(ROL::Ptr > & riesz) { // Retrieve dimensions. int c = feVel_->N()->dimension(0); int fv = basisPtrVel_->getCardinality(); int fp = basisPtrPrs_->getCardinality(); int d = cellCub_->getDimension(); // Initialize jacobians. std::vector > > > J(d+1); for (int i = 0; i < d+1; ++i) { J[i].resize(d+1); } for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { J[i][j] = ROL::makePtr>(c, fv, fv); } J[d][i] = ROL::makePtr>(c, fp, fv); J[i][d] = ROL::makePtr>(c, fv, fp); } J[d][d] = ROL::makePtr>(c, fp, fp); for (int i = 0; i < d; ++i) { *(J[i][i]) = *(feVel_->stiffMat()); Intrepid::RealSpaceTools::add(*(J[i][i]),*(feVel_->massMat())); } *(J[d][d]) = *(fePrs_->massMat()); // Combine the jacobians. fieldHelper_->combineFieldCoeff(riesz, J); } void RieszMap_2(ROL::Ptr > & riesz) { // Retrieve dimensions. int c = feVel_->N()->dimension(0); int fv = basisPtrVel_->getCardinality(); int fp = basisPtrPrs_->getCardinality(); int d = cellCub_->getDimension(); // Initialize jacobians. std::vector > > > J(d+1); for (int i = 0; i < d+1; ++i) { J[i].resize(d+1); } for (int i = 0; i < d; ++i) { for (int j = 0; j < d; ++j) { J[i][j] = ROL::makePtr>(c, fv, fv); } J[d][i] = ROL::makePtr>(c, fp, fv); J[i][d] = ROL::makePtr>(c, fv, fp); } J[d][d] = ROL::makePtr>(c, fp, fp); for (int i = 0; i < d; ++i) { *(J[i][i]) = *(feVel_->massMat()); } *(J[d][d]) = *(fePrs_->massMat()); // Combine the jacobians. fieldHelper_->combineFieldCoeff(riesz, J); } 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. feVel_ = ROL::makePtr>(volCellNodes_,basisPtrVel_,cellCub_); fePrs_ = ROL::makePtr>(volCellNodes_,basisPtrPrs_,cellCub_); // Get boundary degrees of freedom. fvidx_ = feVel_->getBoundaryDofs(); fpidx_ = fePrs_->getBoundaryDofs(); // Construct boundary FEs const int numSideSets = bdryCellNodes.size(); feVelBdry_.resize(numSideSets); fePrsBdry_.resize(numSideSets); for (int i = 0; i < numSideSets; ++i) { int numLocSides = bdryCellNodes[i].size(); feVelBdry_[i].resize(numLocSides); fePrsBdry_[i].resize(numLocSides); for (int j = 0; j < numLocSides; ++j) { if (bdryCellNodes[i][j] != ROL::nullPtr) { feVelBdry_[i][j] = ROL::makePtr>(bdryCellNodes[i][j],basisPtrVel_,bdryCub_,j); fePrsBdry_[i][j] = ROL::makePtr>(bdryCellNodes[i][j],basisPtrPrs_,bdryCub_,j); } } } computeDirichlet(); } void setFieldPattern(const std::vector > & fieldPattern) { fieldPattern_ = fieldPattern; fieldHelper_ = ROL::makePtr>(numFields_, numDofs_, numFieldDofs_, fieldPattern_); } const ROL::Ptr > getVelocityFE(void) const { return feVel_; } const ROL::Ptr > getPressureFE(void) const { return fePrs_; } const std::vector > > > getVelocityBdryFE(void) const { return feVelBdry_; } const std::vector > > > getPressureBdryFE(void) const { return fePrsBdry_; } const std::vector > > getBdryCellLocIds(void) const { return bdryCellLocIds_; } const ROL::Ptr > getFieldHelper(void) const { return fieldHelper_; } }; // PDE_Stokes #endif