ROL
ROL_BlockOperator.hpp
Go to the documentation of this file.
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_BLOCKOPERATOR_H
11 #define ROL_BLOCKOPERATOR_H
12 
13 #include "ROL_LinearOperator.hpp"
15 
24 namespace ROL {
25 
26 template<class Real>
27 class BlockOperator : public LinearOperator<Real> {
28 
29  typedef Vector<Real> V; // ROL Vector
30  typedef PartitionedVector<Real> PV; // Partitioned Vector
31  typedef LinearOperator<Real> OP; // Linear Operator
32 
33  typedef std::vector<ROL::Ptr<OP> > OpVec; // Vector (column-stacked matrix) of pointers to operators
34  typedef typename OpVec::size_type uint; // index type
35 
36 private:
37 
38  ROL::Ptr<OpVec> blocks_;
39 
40 public:
42  BlockOperator( const ROL::Ptr<OpVec> &blocks ) : blocks_(blocks) {}
43 
44  virtual void apply( V &Hv, const V &v, Real &tol ) const {
45 
46  // Downcast to Partitioned Vectors
47  PV &Hv_part = dynamic_cast<PV&>(Hv);
48  const PV &v_part = dynamic_cast<const PV&>(v);
49 
50  uint nvec1 = v_part.numVectors();
51  uint nvec2 = Hv_part.numVectors();
52  uint nblks = blocks_->size();
53 
54  ROL_TEST_FOR_EXCEPTION( (nvec1 != nvec2), std::invalid_argument,
55  ">>> ERROR (ROL_BlockOperator, apply): "
56  "Mismatch between input and output number of subvectors.");
57 
58  ROL_TEST_FOR_EXCEPTION( (nblks != nvec1*nvec2 ) , std::invalid_argument,
59  ">>> ERROR (ROL_BlockOperator, apply): "
60  "Block operator dimension mismatch.");
61 
62  for( uint i=0; i<nvec1; ++i ) {
63 
64  ROL::Ptr<V> Hvi = Hv_part.get(i);
65  ROL::Ptr<V> u = Hvi->clone();
66 
67  u->zero();
68 
69  for( uint j=0; j<nvec2; ++j ) {
70  uint k = i+nvec1*j;
71  (*blocks_)[k]->apply(*u,*v_part.get(j),tol);
72  Hvi->plus(*u);
73  }
74  }
75  }
76 
77 
78 }; // class BlockOperator
79 
80 } // namespace ROL
81 
82 #endif // ROL_BLOCKOPERATOR_H
typename PV< Real >::size_type size_type
ROL::Ptr< const Vector< Real > > get(size_type i) const
Defines the linear algebra of vector space on a generic partitioned vector.
std::vector< ROL::Ptr< OP > > OpVec
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
ROL::Ptr< OpVec > blocks_
PartitionedVector< Real > PV
virtual void apply(V &Hv, const V &v, Real &tol) const
Apply linear operator.
Provides the interface to apply a linear operator.
Provides the interface to apply a block operator to a partitioned vector.
LinearOperator< Real > OP
OpVec::size_type uint
BlockOperator(const ROL::Ptr< OpVec > &blocks)