// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef ROL_FRACTIONALOPERATOR_H #define ROL_FRACTIONALOPERATOR_H #include "ROL_LinearOperator.hpp" #include "../../TOOLS/assembler.hpp" // Do not instantiate the template in this translation unit. extern template class Assembler; template class FractionalOperator : public ROL::LinearOperator { private: typedef Tpetra::Map<>::global_ordinal_type GO; ROL::Ptr > Klocal_, Mlocal_; ROL::Ptr > Kcylinder_, Mcylinder_; ROL::Ptr > KcylinderT_, McylinderT_; void transposeMats(void) { Tpetra::RowMatrixTransposer<> transposerK(Kcylinder_); KcylinderT_ = transposerK.createTranspose(); Tpetra::RowMatrixTransposer<> transposerM(Mcylinder_); McylinderT_ = transposerM.createTranspose(); } bool transpose_; public: FractionalOperator(const ROL::Ptr > &pde_local, const ROL::Ptr > &mesh_local, const ROL::Ptr > &pde_cylinder, const ROL::Ptr > &mesh_cylinder, const ROL::Ptr > &comm, Teuchos::ParameterList &parlist, std::ostream &outStream = std::cout) : transpose_(false) { ROL::Ptr > uvec; ROL::Ptr > zvec; // Assemble local components ROL::Ptr > assembler_local = ROL::makePtr>(pde_local->getFields(),mesh_local,comm,parlist,outStream); assembler_local->setCellNodes(*pde_local); uvec = assembler_local->createStateVector(); zvec = assembler_local->createControlVector(); assembler_local->assemblePDEJacobian1(Klocal_,pde_local,uvec,zvec); assembler_local->assemblePDERieszMap1(Mlocal_,pde_local); // Assemble cylinder components ROL::Ptr > assembler_cylinder = ROL::makePtr>(pde_cylinder->getFields(),mesh_cylinder,comm,parlist,outStream); assembler_cylinder->setCellNodes(*pde_cylinder); uvec = assembler_cylinder->createStateVector(); zvec = assembler_cylinder->createControlVector(); assembler_cylinder->assemblePDEJacobian1(Kcylinder_,pde_cylinder,uvec,zvec); assembler_cylinder->assemblePDERieszMap1(Mcylinder_,pde_cylinder); // Transpose cylinder components transposeMats(); } FractionalOperator(const ROL::Ptr > &Klocal, const ROL::Ptr > &Mlocal, const ROL::Ptr > &Kcylinder, const ROL::Ptr > &Mcylinder) : Klocal_(Klocal), Mlocal_(Mlocal), Kcylinder_(Kcylinder), Mcylinder_(Mcylinder), transpose_(false) { // Transpose cylinder components transposeMats(); } void setTranspose(const bool trans = true) { transpose_ = trans; } void apply( ROL::Vector &Hv, const ROL::Vector &v, Real &tol ) const { ROL::Ptr > Hvf = getField(Hv); ROL::Ptr > vf = getConstField(v); if ( !transpose_ ) { size_t numRowEntries(0); typename Tpetra::CrsMatrix<>::nonconst_global_inds_host_view_type indices; typename Tpetra::CrsMatrix<>::nonconst_values_host_view_type values; Teuchos::Array col(1), row(1); for (size_t r = 0; r < Mcylinder_->getGlobalNumRows(); ++r) { row[0] = r; numRowEntries = Mcylinder_->getNumEntriesInGlobalRow(r); Kokkos::resize(indices,numRowEntries); Kokkos::resize(values,numRowEntries); Mcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); for (size_t c = 0; c < indices.size(); ++c) { col[0] = static_cast(indices[c]); Klocal_->apply(*(vf->subView(col())),*(Hvf->subViewNonConst(row())),Teuchos::NO_TRANS,values[c],static_cast(0)); //Klocal_->apply(*(vf->getVector(indices[c])),*(Hvf->getVectorNonConst(r)),Teuchos::NO_TRANS,values[c],static_cast(0)); } } for (size_t r = 0; r < Kcylinder_->getGlobalNumRows(); ++r) { row[0] = r; numRowEntries = Kcylinder_->getNumEntriesInGlobalRow(r); Kokkos::resize(indices,numRowEntries); Kokkos::resize(values,numRowEntries); Kcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); for (size_t c = 0; c < indices.size(); ++c) { col[0] = static_cast(indices[c]); Mlocal_->apply(*(vf->subView(col())),*(Hvf->subViewNonConst(row())),Teuchos::NO_TRANS,values[c],static_cast(1)); //Mlocal_->apply(*(vf->getVector(indices[c])),*(Hvf->getVectorNonConst(r)),Teuchos::NO_TRANS,values[c],static_cast(1)); } } } } private: // Vector accessor functions ROL::Ptr > getConstField(const ROL::Vector &x) const { return dynamic_cast&>(x).getVector(); } ROL::Ptr > getField(ROL::Vector &x) const { return dynamic_cast&>(x).getVector(); } }; template class FractionalPreconditioner : public ROL::LinearOperator { private: typedef Tpetra::Map<>::global_ordinal_type GO; ROL::Ptr > Klocal_, Mlocal_; ROL::Ptr > Kcylinder_, Mcylinder_; mutable ROL::Ptr > M_; mutable ROL::Ptr > solver_; mutable Teuchos::ParameterList parlist_; bool transpose_; public: FractionalPreconditioner(const ROL::Ptr > &pde_local, const ROL::Ptr > &mesh_local, const ROL::Ptr > &pde_cylinder, const ROL::Ptr > &mesh_cylinder, const ROL::Ptr > &comm, Teuchos::ParameterList &parlist, std::ostream &outStream = std::cout) : parlist_(parlist), transpose_(false) { ROL::Ptr > uvec; ROL::Ptr > zvec; // Assemble local components ROL::Ptr > assembler_local = ROL::makePtr>(pde_local->getFields(),mesh_local,comm,parlist,outStream); assembler_local->setCellNodes(*pde_local); uvec = assembler_local->createStateVector(); zvec = assembler_local->createControlVector(); assembler_local->assemblePDEJacobian1(Klocal_,pde_local,uvec,zvec); assembler_local->assemblePDERieszMap1(Mlocal_,pde_local); // Assemble cylinder components ROL::Ptr > assembler_cylinder = ROL::makePtr>(pde_cylinder->getFields(),mesh_cylinder,comm,parlist,outStream); assembler_cylinder->setCellNodes(*pde_cylinder); uvec = assembler_cylinder->createStateVector(); zvec = assembler_cylinder->createControlVector(); assembler_cylinder->assemblePDEJacobian1(Kcylinder_,pde_cylinder,uvec,zvec); assembler_cylinder->assemblePDERieszMap1(Mcylinder_,pde_cylinder); // Create linear solver object solver_ = ROL::makePtr>(parlist); } FractionalPreconditioner(const ROL::Ptr > &Klocal, const ROL::Ptr > &Mlocal, const ROL::Ptr > &Kcylinder, const ROL::Ptr > &Mcylinder, Teuchos::ParameterList &parlist) : Klocal_(Klocal), Mlocal_(Mlocal), Kcylinder_(Kcylinder), Mcylinder_(Mcylinder), parlist_(parlist), transpose_(false) { // Create linear solver object solver_ = ROL::makePtr>(parlist); } void setTranspose(const bool trans = true) { transpose_ = trans; } void apply( ROL::Vector &Hv, const ROL::Vector &v, Real &tol ) const { ROL::Ptr > Hvf = getField(Hv); ROL::Ptr > vf = getConstField(v); size_t numRowEntries(0); Real massVal(0), stiffVal(0); typename Tpetra::CrsMatrix<>::nonconst_global_inds_host_view_type indices; typename Tpetra::CrsMatrix<>::nonconst_values_host_view_type values; Teuchos::Array row(1); for (size_t r = 0; r < Mcylinder_->getGlobalNumRows(); ++r) { row[0] = r; numRowEntries = Mcylinder_->getNumEntriesInGlobalRow(r); Kokkos::resize(indices,numRowEntries); Kokkos::resize(values,numRowEntries); Mcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); massVal = static_cast(0); for (size_t c = 0; c < indices.size(); ++c) { if ( indices[c] == static_cast(r) ) { massVal = values[c]; break; } } numRowEntries = Kcylinder_->getNumEntriesInGlobalRow(r); Kokkos::resize(indices,numRowEntries); Kokkos::resize(values,numRowEntries); Kcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); stiffVal = static_cast(0); for (size_t c = 0; c < indices.size(); ++c) { if ( indices[c] == static_cast(r) ) { stiffVal = values[c]; break; } } M_ = Tpetra::MatrixMatrix::add(massVal, transpose_, *Klocal_, stiffVal, transpose_, *Mlocal_); M_->apply(*(vf->subView(row())),*(Hvf->subViewNonConst(row()))); //M_->apply(*(vf->getVector(r)),*(Hvf->getVectorNonConst(r))); } } void applyInverse( ROL::Vector &Hv, const ROL::Vector &v, Real &tol ) const { ROL::Ptr > Hvf = getField(Hv); ROL::Ptr > vf = getConstField(v); size_t numRowEntries(0); Real massVal(0), stiffVal(0); typename Tpetra::CrsMatrix<>::nonconst_global_inds_host_view_type indices; typename Tpetra::CrsMatrix<>::nonconst_values_host_view_type values; Teuchos::Array row(1); for (size_t r = 0; r < Mcylinder_->getGlobalNumRows(); ++r) { row[0] = r; numRowEntries = Mcylinder_->getNumEntriesInGlobalRow(r); Kokkos::resize(indices,numRowEntries); Kokkos::resize(values,numRowEntries); Mcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); massVal = static_cast(0); for (size_t c = 0; c < indices.size(); ++c) { if ( indices[c] == static_cast(r) ) { massVal = values[c]; break; } } numRowEntries = Kcylinder_->getNumEntriesInGlobalRow(r); Kokkos::resize(indices,numRowEntries); Kokkos::resize(values,numRowEntries); Kcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); stiffVal = static_cast(0); for (size_t c = 0; c < indices.size(); ++c) { if ( indices[c] == static_cast(r) ) { stiffVal = values[c]; break; } } M_ = Tpetra::MatrixMatrix::add(massVal, false, *Klocal_, stiffVal, false, *Mlocal_); solver_ = ROL::makePtr>(parlist_); solver_->setA(M_); solver_->solve(Hvf->subViewNonConst(row()),vf->subView(row()),transpose_); //solver_->solve(Hvf->getVectorNonConst(r),vf->getVector(r),transpose_); } } private: // Vector accessor functions ROL::Ptr > getConstField(const ROL::Vector &x) const { return dynamic_cast&>(x).getVector(); } ROL::Ptr > getField(ROL::Vector &x) const { return dynamic_cast&>(x).getVector(); } }; template class FractionalControlOperator : public ROL::LinearOperator { private: const ROL::Ptr > Blocal_; const int numCylinder_; bool transpose_; public: FractionalControlOperator(const ROL::Ptr > &Blocal, const int numCylinder) : Blocal_(Blocal), numCylinder_(numCylinder), transpose_(false) {} void setTranspose(const bool trans = true) { transpose_ = trans; } void apply( ROL::Vector &Hv, const ROL::Vector &v, Real &tol ) const { ROL::Ptr > Hvf = getField(Hv); ROL::Ptr > vf = getConstField(v); Teuchos::Array col(1,0); if ( !transpose_ ) { Hv.zero(); Blocal_->apply(*vf, *(Hvf->subViewNonConst(col()))); //Blocal_->apply(*vf, *(Hvf->getVectorNonConst(0))); } else { Blocal_->apply(*(vf->subView(col())), *Hvf, Teuchos::TRANS); //Blocal_->apply(*(vf->getVector(0)), *Hvf, Teuchos::TRANS); } } private: // Vector accessor functions ROL::Ptr > getConstField(const ROL::Vector &x) const { return dynamic_cast&>(x).getVector(); } ROL::Ptr > getField(ROL::Vector &x) const { return dynamic_cast&>(x).getVector(); } }; #endif