| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Rapid Optimization Library (ROL) Package |
| 4 |
// |
| 5 |
// Copyright 2014 NTESS and the ROL contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef ROL_FRACTIONALOPERATOR_H |
| 11 |
#define ROL_FRACTIONALOPERATOR_H |
| 12 |
|
| 13 |
#include "ROL_LinearOperator.hpp" |
| 14 |
#include "../../TOOLS/assembler.hpp" |
| 15 |
|
| 16 |
// Do not instantiate the template in this translation unit. |
| 17 |
extern template class Assembler<double>; |
| 18 |
|
| 19 |
template <class Real> |
| 20 |
class FractionalOperator : public ROL::LinearOperator<Real> { |
| 21 |
private: |
| 22 |
typedef Tpetra::Map<>::global_ordinal_type GO; |
| 23 |
|
| 24 |
ROL::Ptr<Tpetra::CrsMatrix<> > Klocal_, Mlocal_; |
| 25 |
ROL::Ptr<Tpetra::CrsMatrix<> > Kcylinder_, Mcylinder_; |
| 26 |
ROL::Ptr<Tpetra::CrsMatrix<> > KcylinderT_, McylinderT_; |
| 27 |
|
| 28 |
void transposeMats(void) { |
| 29 |
Tpetra::RowMatrixTransposer<> transposerK(Kcylinder_); |
| 30 |
KcylinderT_ = transposerK.createTranspose(); |
| 31 |
Tpetra::RowMatrixTransposer<> transposerM(Mcylinder_); |
| 32 |
McylinderT_ = transposerM.createTranspose(); |
| 33 |
} |
| 34 |
|
| 35 |
bool transpose_; |
| 36 |
|
| 37 |
public: |
| 38 |
FractionalOperator(const ROL::Ptr<PDE<Real> > &pde_local, |
| 39 |
const ROL::Ptr<MeshManager<Real> > &mesh_local, |
| 40 |
const ROL::Ptr<PDE<Real> > &pde_cylinder, |
| 41 |
const ROL::Ptr<MeshManager<Real> > &mesh_cylinder, |
| 42 |
const ROL::Ptr<const Teuchos::Comm<int> > &comm, |
| 43 |
Teuchos::ParameterList &parlist, |
| 44 |
std::ostream &outStream = std::cout) |
| 45 |
: transpose_(false) { |
| 46 |
ROL::Ptr<Tpetra::MultiVector<> > uvec; |
| 47 |
ROL::Ptr<Tpetra::MultiVector<> > zvec; |
| 48 |
// Assemble local components |
| 49 |
ROL::Ptr<Assembler<Real> > assembler_local |
| 50 |
= ROL::makePtr<Assembler<Real>>(pde_local->getFields(),mesh_local,comm,parlist,outStream); |
| 51 |
assembler_local->setCellNodes(*pde_local); |
| 52 |
uvec = assembler_local->createStateVector(); |
| 53 |
zvec = assembler_local->createControlVector(); |
| 54 |
assembler_local->assemblePDEJacobian1(Klocal_,pde_local,uvec,zvec); |
| 55 |
assembler_local->assemblePDERieszMap1(Mlocal_,pde_local); |
| 56 |
// Assemble cylinder components |
| 57 |
ROL::Ptr<Assembler<Real> > assembler_cylinder |
| 58 |
= ROL::makePtr<Assembler<Real>>(pde_cylinder->getFields(),mesh_cylinder,comm,parlist,outStream); |
| 59 |
assembler_cylinder->setCellNodes(*pde_cylinder); |
| 60 |
uvec = assembler_cylinder->createStateVector(); |
| 61 |
zvec = assembler_cylinder->createControlVector(); |
| 62 |
assembler_cylinder->assemblePDEJacobian1(Kcylinder_,pde_cylinder,uvec,zvec); |
| 63 |
assembler_cylinder->assemblePDERieszMap1(Mcylinder_,pde_cylinder); |
| 64 |
// Transpose cylinder components |
| 65 |
transposeMats(); |
| 66 |
} |
| 67 |
|
| 68 |
FractionalOperator(const ROL::Ptr<Tpetra::CrsMatrix<> > &Klocal, |
| 69 |
const ROL::Ptr<Tpetra::CrsMatrix<> > &Mlocal, |
| 70 |
const ROL::Ptr<Tpetra::CrsMatrix<> > &Kcylinder, |
| 71 |
const ROL::Ptr<Tpetra::CrsMatrix<> > &Mcylinder) |
| 72 |
: Klocal_(Klocal), Mlocal_(Mlocal), Kcylinder_(Kcylinder), Mcylinder_(Mcylinder), |
| 73 |
transpose_(false) { |
| 74 |
// Transpose cylinder components |
| 75 |
transposeMats(); |
| 76 |
} |
| 77 |
|
| 78 |
void setTranspose(const bool trans = true) { |
| 79 |
transpose_ = trans; |
| 80 |
} |
| 81 |
|
| 82 |
void apply( ROL::Vector<Real> &Hv, const ROL::Vector<Real> &v, Real &tol ) const { |
| 83 |
ROL::Ptr<Tpetra::MultiVector<> > Hvf = getField(Hv); |
| 84 |
ROL::Ptr<const Tpetra::MultiVector<> > vf = getConstField(v); |
| 85 |
|
| 86 |
if ( !transpose_ ) { |
| 87 |
size_t numRowEntries(0); |
| 88 |
typename Tpetra::CrsMatrix<>::nonconst_global_inds_host_view_type indices; |
| 89 |
typename Tpetra::CrsMatrix<>::nonconst_values_host_view_type values; |
| 90 |
Teuchos::Array<size_t> col(1), row(1); |
| 91 |
for (size_t r = 0; r < Mcylinder_->getGlobalNumRows(); ++r) { |
| 92 |
row[0] = r; |
| 93 |
numRowEntries = Mcylinder_->getNumEntriesInGlobalRow(r); |
| 94 |
Kokkos::resize(indices,numRowEntries); |
| 95 |
Kokkos::resize(values,numRowEntries); |
| 96 |
Mcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); |
| 97 |
for (size_t c = 0; c < indices.size(); ++c) { |
| 98 |
col[0] = static_cast<size_t>(indices[c]); |
| 99 |
Klocal_->apply(*(vf->subView(col())),*(Hvf->subViewNonConst(row())),Teuchos::NO_TRANS,values[c],static_cast<Real>(0)); |
| 100 |
//Klocal_->apply(*(vf->getVector(indices[c])),*(Hvf->getVectorNonConst(r)),Teuchos::NO_TRANS,values[c],static_cast<Real>(0)); |
| 101 |
} |
| 102 |
} |
| 103 |
for (size_t r = 0; r < Kcylinder_->getGlobalNumRows(); ++r) { |
| 104 |
row[0] = r; |
| 105 |
numRowEntries = Kcylinder_->getNumEntriesInGlobalRow(r); |
| 106 |
Kokkos::resize(indices,numRowEntries); |
| 107 |
Kokkos::resize(values,numRowEntries); |
| 108 |
Kcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); |
| 109 |
for (size_t c = 0; c < indices.size(); ++c) { |
| 110 |
col[0] = static_cast<size_t>(indices[c]); |
| 111 |
Mlocal_->apply(*(vf->subView(col())),*(Hvf->subViewNonConst(row())),Teuchos::NO_TRANS,values[c],static_cast<Real>(1)); |
| 112 |
//Mlocal_->apply(*(vf->getVector(indices[c])),*(Hvf->getVectorNonConst(r)),Teuchos::NO_TRANS,values[c],static_cast<Real>(1)); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
private: // Vector accessor functions |
| 119 |
|
| 120 |
ROL::Ptr<const Tpetra::MultiVector<> > getConstField(const ROL::Vector<Real> &x) const { |
| 121 |
return dynamic_cast<const ROL::TpetraMultiVector<Real>&>(x).getVector(); |
| 122 |
} |
| 123 |
|
| 124 |
ROL::Ptr<Tpetra::MultiVector<> > getField(ROL::Vector<Real> &x) const { |
| 125 |
return dynamic_cast<ROL::TpetraMultiVector<Real>&>(x).getVector(); |
| 126 |
} |
| 127 |
}; |
| 128 |
|
| 129 |
template <class Real> |
| 130 |
class FractionalPreconditioner : public ROL::LinearOperator<Real> { |
| 131 |
private: |
| 132 |
typedef Tpetra::Map<>::global_ordinal_type GO; |
| 133 |
|
| 134 |
ROL::Ptr<Tpetra::CrsMatrix<> > Klocal_, Mlocal_; |
| 135 |
ROL::Ptr<Tpetra::CrsMatrix<> > Kcylinder_, Mcylinder_; |
| 136 |
mutable ROL::Ptr<Tpetra::CrsMatrix<> > M_; |
| 137 |
mutable ROL::Ptr<Solver<Real> > solver_; |
| 138 |
|
| 139 |
mutable Teuchos::ParameterList parlist_; |
| 140 |
|
| 141 |
bool transpose_; |
| 142 |
|
| 143 |
public: |
| 144 |
FractionalPreconditioner(const ROL::Ptr<PDE<Real> > &pde_local, |
| 145 |
const ROL::Ptr<MeshManager<Real> > &mesh_local, |
| 146 |
const ROL::Ptr<PDE<Real> > &pde_cylinder, |
| 147 |
const ROL::Ptr<MeshManager<Real> > &mesh_cylinder, |
| 148 |
const ROL::Ptr<const Teuchos::Comm<int> > &comm, |
| 149 |
Teuchos::ParameterList &parlist, |
| 150 |
std::ostream &outStream = std::cout) |
| 151 |
: parlist_(parlist), transpose_(false) { |
| 152 |
ROL::Ptr<Tpetra::MultiVector<> > uvec; |
| 153 |
ROL::Ptr<Tpetra::MultiVector<> > zvec; |
| 154 |
// Assemble local components |
| 155 |
ROL::Ptr<Assembler<Real> > assembler_local |
| 156 |
= ROL::makePtr<Assembler<Real>>(pde_local->getFields(),mesh_local,comm,parlist,outStream); |
| 157 |
assembler_local->setCellNodes(*pde_local); |
| 158 |
uvec = assembler_local->createStateVector(); |
| 159 |
zvec = assembler_local->createControlVector(); |
| 160 |
assembler_local->assemblePDEJacobian1(Klocal_,pde_local,uvec,zvec); |
| 161 |
assembler_local->assemblePDERieszMap1(Mlocal_,pde_local); |
| 162 |
// Assemble cylinder components |
| 163 |
ROL::Ptr<Assembler<Real> > assembler_cylinder |
| 164 |
= ROL::makePtr<Assembler<Real>>(pde_cylinder->getFields(),mesh_cylinder,comm,parlist,outStream); |
| 165 |
assembler_cylinder->setCellNodes(*pde_cylinder); |
| 166 |
uvec = assembler_cylinder->createStateVector(); |
| 167 |
zvec = assembler_cylinder->createControlVector(); |
| 168 |
assembler_cylinder->assemblePDEJacobian1(Kcylinder_,pde_cylinder,uvec,zvec); |
| 169 |
assembler_cylinder->assemblePDERieszMap1(Mcylinder_,pde_cylinder); |
| 170 |
// Create linear solver object |
| 171 |
solver_ = ROL::makePtr<Solver<Real>>(parlist); |
| 172 |
} |
| 173 |
|
| 174 |
FractionalPreconditioner(const ROL::Ptr<Tpetra::CrsMatrix<> > &Klocal, |
| 175 |
const ROL::Ptr<Tpetra::CrsMatrix<> > &Mlocal, |
| 176 |
const ROL::Ptr<Tpetra::CrsMatrix<> > &Kcylinder, |
| 177 |
const ROL::Ptr<Tpetra::CrsMatrix<> > &Mcylinder, |
| 178 |
Teuchos::ParameterList &parlist) |
| 179 |
: Klocal_(Klocal), Mlocal_(Mlocal), Kcylinder_(Kcylinder), Mcylinder_(Mcylinder), |
| 180 |
parlist_(parlist), transpose_(false) { |
| 181 |
// Create linear solver object |
| 182 |
solver_ = ROL::makePtr<Solver<Real>>(parlist); |
| 183 |
} |
| 184 |
|
| 185 |
void setTranspose(const bool trans = true) { |
| 186 |
transpose_ = trans; |
| 187 |
} |
| 188 |
|
| 189 |
void apply( ROL::Vector<Real> &Hv, const ROL::Vector<Real> &v, Real &tol ) const { |
| 190 |
ROL::Ptr<Tpetra::MultiVector<> > Hvf = getField(Hv); |
| 191 |
ROL::Ptr<const Tpetra::MultiVector<> > vf = getConstField(v); |
| 192 |
|
| 193 |
size_t numRowEntries(0); |
| 194 |
Real massVal(0), stiffVal(0); |
| 195 |
typename Tpetra::CrsMatrix<>::nonconst_global_inds_host_view_type indices; |
| 196 |
typename Tpetra::CrsMatrix<>::nonconst_values_host_view_type values; |
| 197 |
Teuchos::Array<size_t> row(1); |
| 198 |
for (size_t r = 0; r < Mcylinder_->getGlobalNumRows(); ++r) { |
| 199 |
row[0] = r; |
| 200 |
numRowEntries = Mcylinder_->getNumEntriesInGlobalRow(r); |
| 201 |
Kokkos::resize(indices,numRowEntries); |
| 202 |
Kokkos::resize(values,numRowEntries); |
| 203 |
Mcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); |
| 204 |
massVal = static_cast<Real>(0); |
| 205 |
for (size_t c = 0; c < indices.size(); ++c) { |
| 206 |
if ( indices[c] == static_cast<int>(r) ) { |
| 207 |
massVal = values[c]; |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
numRowEntries = Kcylinder_->getNumEntriesInGlobalRow(r); |
| 212 |
Kokkos::resize(indices,numRowEntries); |
| 213 |
Kokkos::resize(values,numRowEntries); |
| 214 |
Kcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); |
| 215 |
stiffVal = static_cast<Real>(0); |
| 216 |
for (size_t c = 0; c < indices.size(); ++c) { |
| 217 |
if ( indices[c] == static_cast<int>(r) ) { |
| 218 |
stiffVal = values[c]; |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
M_ = Tpetra::MatrixMatrix::add(massVal, transpose_, *Klocal_, stiffVal, transpose_, *Mlocal_); |
| 223 |
M_->apply(*(vf->subView(row())),*(Hvf->subViewNonConst(row()))); |
| 224 |
//M_->apply(*(vf->getVector(r)),*(Hvf->getVectorNonConst(r))); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
void applyInverse( ROL::Vector<Real> &Hv, const ROL::Vector<Real> &v, Real &tol ) const { |
| 229 |
ROL::Ptr<Tpetra::MultiVector<> > Hvf = getField(Hv); |
| 230 |
ROL::Ptr<const Tpetra::MultiVector<> > vf = getConstField(v); |
| 231 |
|
| 232 |
size_t numRowEntries(0); |
| 233 |
Real massVal(0), stiffVal(0); |
| 234 |
typename Tpetra::CrsMatrix<>::nonconst_global_inds_host_view_type indices; |
| 235 |
typename Tpetra::CrsMatrix<>::nonconst_values_host_view_type values; |
| 236 |
Teuchos::Array<size_t> row(1); |
| 237 |
for (size_t r = 0; r < Mcylinder_->getGlobalNumRows(); ++r) { |
| 238 |
row[0] = r; |
| 239 |
numRowEntries = Mcylinder_->getNumEntriesInGlobalRow(r); |
| 240 |
Kokkos::resize(indices,numRowEntries); |
| 241 |
Kokkos::resize(values,numRowEntries); |
| 242 |
Mcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); |
| 243 |
massVal = static_cast<Real>(0); |
| 244 |
for (size_t c = 0; c < indices.size(); ++c) { |
| 245 |
if ( indices[c] == static_cast<int>(r) ) { |
| 246 |
massVal = values[c]; |
| 247 |
break; |
| 248 |
} |
| 249 |
} |
| 250 |
numRowEntries = Kcylinder_->getNumEntriesInGlobalRow(r); |
| 251 |
Kokkos::resize(indices,numRowEntries); |
| 252 |
Kokkos::resize(values,numRowEntries); |
| 253 |
Kcylinder_->getGlobalRowCopy(r,indices,values,numRowEntries); |
| 254 |
stiffVal = static_cast<Real>(0); |
| 255 |
for (size_t c = 0; c < indices.size(); ++c) { |
| 256 |
if ( indices[c] == static_cast<int>(r) ) { |
| 257 |
stiffVal = values[c]; |
| 258 |
break; |
| 259 |
} |
| 260 |
} |
| 261 |
M_ = Tpetra::MatrixMatrix::add(massVal, false, *Klocal_, stiffVal, false, *Mlocal_); |
| 262 |
solver_ = ROL::makePtr<Solver<Real>>(parlist_); |
| 263 |
solver_->setA(M_); |
| 264 |
solver_->solve(Hvf->subViewNonConst(row()),vf->subView(row()),transpose_); |
| 265 |
//solver_->solve(Hvf->getVectorNonConst(r),vf->getVector(r),transpose_); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
private: // Vector accessor functions |
| 270 |
|
| 271 |
ROL::Ptr<const Tpetra::MultiVector<> > getConstField(const ROL::Vector<Real> &x) const { |
| 272 |
return dynamic_cast<const ROL::TpetraMultiVector<Real>&>(x).getVector(); |
| 273 |
} |
| 274 |
|
| 275 |
ROL::Ptr<Tpetra::MultiVector<> > getField(ROL::Vector<Real> &x) const { |
| 276 |
return dynamic_cast<ROL::TpetraMultiVector<Real>&>(x).getVector(); |
| 277 |
} |
| 278 |
}; |
| 279 |
|
| 280 |
template <class Real> |
| 281 |
class FractionalControlOperator : public ROL::LinearOperator<Real> { |
| 282 |
private: |
| 283 |
const ROL::Ptr<Tpetra::CrsMatrix<> > Blocal_; |
| 284 |
const int numCylinder_; |
| 285 |
|
| 286 |
bool transpose_; |
| 287 |
|
| 288 |
public: |
| 289 |
FractionalControlOperator(const ROL::Ptr<Tpetra::CrsMatrix<> > &Blocal, |
| 290 |
const int numCylinder) |
| 291 |
: Blocal_(Blocal), numCylinder_(numCylinder), transpose_(false) {} |
| 292 |
|
| 293 |
void setTranspose(const bool trans = true) { |
| 294 |
transpose_ = trans; |
| 295 |
} |
| 296 |
|
| 297 |
void apply( ROL::Vector<Real> &Hv, const ROL::Vector<Real> &v, Real &tol ) const { |
| 298 |
ROL::Ptr<Tpetra::MultiVector<> > Hvf = getField(Hv); |
| 299 |
ROL::Ptr<const Tpetra::MultiVector<> > vf = getConstField(v); |
| 300 |
|
| 301 |
Teuchos::Array<size_t> col(1,0); |
| 302 |
if ( !transpose_ ) { |
| 303 |
Hv.zero(); |
| 304 |
Blocal_->apply(*vf, *(Hvf->subViewNonConst(col()))); |
| 305 |
//Blocal_->apply(*vf, *(Hvf->getVectorNonConst(0))); |
| 306 |
} |
| 307 |
else { |
| 308 |
Blocal_->apply(*(vf->subView(col())), *Hvf, Teuchos::TRANS); |
| 309 |
//Blocal_->apply(*(vf->getVector(0)), *Hvf, Teuchos::TRANS); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
private: // Vector accessor functions |
| 314 |
|
| 315 |
ROL::Ptr<const Tpetra::MultiVector<> > getConstField(const ROL::Vector<Real> &x) const { |
| 316 |
return dynamic_cast<const ROL::TpetraMultiVector<Real>&>(x).getVector(); |
| 317 |
} |
| 318 |
|
| 319 |
ROL::Ptr<Tpetra::MultiVector<> > getField(ROL::Vector<Real> &x) const { |
| 320 |
return dynamic_cast<ROL::TpetraMultiVector<Real>&>(x).getVector(); |
| 321 |
} |
| 322 |
}; |
| 323 |
|
| 324 |
#endif |