Reference documentation for deal.II version Git 7026f387cc 2019-10-15 14:19:01 -0400
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
vector_adaptor.h
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2017 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 //---------------------------------------------------------------
15 
16 #ifndef dealii_optimization_rol_vector_adaptor_h
17 #define dealii_optimization_rol_vector_adaptor_h
18 
19 #include <deal.II/base/config.h>
20 
21 #ifdef DEAL_II_TRILINOS_WITH_ROL
22 # include <deal.II/base/exceptions.h>
23 
24 # include <deal.II/lac/vector.h>
25 
26 # include <ROL_Vector.hpp>
27 
28 # include <type_traits>
29 
30 
31 DEAL_II_NAMESPACE_OPEN
32 
38 namespace Rol
39 {
111  template <typename VectorType>
112  class VectorAdaptor : public ROL::Vector<typename VectorType::value_type>
113  {
117  using size_type = typename VectorType::size_type;
118 
122  using value_type = typename VectorType::value_type;
123 
127  using real_type = typename VectorType::real_type;
128 
129  static_assert(std::is_convertible<real_type, value_type>::value,
130  "The real_type of the current VectorType is not "
131  "convertible to the value_type.");
132 
133  private:
138  Teuchos::RCP<VectorType> vector_ptr;
139 
140  public:
144  VectorAdaptor(const Teuchos::RCP<VectorType> &vector_ptr);
145 
150  Teuchos::RCP<VectorType>
151  getVector();
152 
156  Teuchos::RCP<const VectorType>
157  getVector() const;
158 
162  int
163  dimension() const;
164 
173  void
174  set(const ROL::Vector<value_type> &rol_vector);
175 
179  void
180  plus(const ROL::Vector<value_type> &rol_vector);
181 
186  void
187  axpy(const value_type alpha, const ROL::Vector<value_type> &rol_vector);
188 
192  void
193  scale(const value_type alpha);
194 
198  value_type
199  dot(const ROL::Vector<value_type> &rol_vector) const;
200 
210  value_type
211  norm() const;
212 
216  Teuchos::RCP<ROL::Vector<value_type>>
217  clone() const;
218 
224  Teuchos::RCP<ROL::Vector<value_type>>
225  basis(const int i) const;
226 
230  void
231  applyUnary(const ROL::Elementwise::UnaryFunction<value_type> &f);
232 
237  void
238  applyBinary(const ROL::Elementwise::BinaryFunction<value_type> &f,
239  const ROL::Vector<value_type> & rol_vector);
240 
245  value_type
246  reduce(const ROL::Elementwise::ReductionOp<value_type> &r) const;
247 
251  void
252  print(std::ostream &outStream) const;
253  };
254 
255 
256  /*------------------------------member definitions--------------------------*/
257 # ifndef DOXYGEN
258 
259 
260  template <typename VectorType>
262  const Teuchos::RCP<VectorType> &vector_ptr)
263  : vector_ptr(vector_ptr)
264  {}
265 
266 
267 
268  template <typename VectorType>
269  Teuchos::RCP<VectorType>
271  {
272  return vector_ptr;
273  }
274 
275 
276 
277  template <typename VectorType>
278  Teuchos::RCP<const VectorType>
280  {
281  return vector_ptr;
282  }
283 
284 
285 
286  template <typename VectorType>
287  void
288  VectorAdaptor<VectorType>::set(const ROL::Vector<value_type> &rol_vector)
289  {
290  const VectorAdaptor &vector_adaptor =
291  Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
292 
293  (*vector_ptr) = *(vector_adaptor.getVector());
294  }
295 
296 
297 
298  template <typename VectorType>
299  void
300  VectorAdaptor<VectorType>::plus(const ROL::Vector<value_type> &rol_vector)
301  {
302  Assert(this->dimension() == rol_vector.dimension(),
303  ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
304 
305  const VectorAdaptor &vector_adaptor =
306  Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
307 
308  *vector_ptr += *(vector_adaptor.getVector());
309  }
310 
311 
312 
313  template <typename VectorType>
314  void
316  const ROL::Vector<value_type> &rol_vector)
317  {
318  Assert(this->dimension() == rol_vector.dimension(),
319  ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
320 
321  const VectorAdaptor &vector_adaptor =
322  Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
323 
324  vector_ptr->add(alpha, *(vector_adaptor.getVector()));
325  }
326 
327 
328 
329  template <typename VectorType>
330  int
332  {
333  Assert(vector_ptr->size() < std::numeric_limits<int>::max(),
334  ExcMessage("The size of the vector being used is greater than "
335  "largest value of type int."));
336  return static_cast<int>(vector_ptr->size());
337  }
338 
339 
340 
341  template <typename VectorType>
342  void
344  {
345  (*vector_ptr) *= alpha;
346  }
347 
348 
349 
350  template <typename VectorType>
351  typename VectorType::value_type
353  const ROL::Vector<value_type> &rol_vector) const
354  {
355  Assert(this->dimension() == rol_vector.dimension(),
356  ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
357 
358  const VectorAdaptor &vector_adaptor =
359  Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
360 
361  return (*vector_ptr) * (*vector_adaptor.getVector());
362  }
363 
364 
365 
366  template <typename VectorType>
367  typename VectorType::value_type
369  {
370  return vector_ptr->l2_norm();
371  }
372 
373 
374 
375  template <typename VectorType>
376  Teuchos::RCP<ROL::Vector<typename VectorType::value_type>>
378  {
379  Teuchos::RCP<VectorType> vec_ptr = Teuchos::rcp(new VectorType);
380  (*vec_ptr) = (*vector_ptr);
381 
382  return Teuchos::rcp(new VectorAdaptor(vec_ptr));
383  }
384 
385 
386 
387  template <typename VectorType>
388  Teuchos::RCP<ROL::Vector<typename VectorType::value_type>>
389  VectorAdaptor<VectorType>::basis(const int i) const
390  {
391  Teuchos::RCP<VectorType> vec_ptr = Teuchos::rcp(new VectorType);
392 
393  // Zero all the entries in dealii vector.
394  vec_ptr->reinit(*vector_ptr, false);
395 
396  if (vector_ptr->locally_owned_elements().is_element(i))
397  vec_ptr->operator[](i) = 1.;
398 
399  vec_ptr->compress(VectorOperation::insert);
400 
401  Teuchos::RCP<VectorAdaptor> e = Teuchos::rcp(new VectorAdaptor(vec_ptr));
402 
403  return e;
404  }
405 
406 
407 
408  template <typename VectorType>
409  void
411  const ROL::Elementwise::UnaryFunction<value_type> &f)
412  {
413  const typename VectorType::iterator vend = vector_ptr->end();
414 
415  for (typename VectorType::iterator iterator = vector_ptr->begin();
416  iterator != vend;
417  iterator++)
418  *iterator = f.apply(*iterator);
419 
420  vector_ptr->compress(VectorOperation::insert);
421  }
422 
423 
424 
425  template <typename VectorType>
426  void
428  const ROL::Elementwise::BinaryFunction<value_type> &f,
429  const ROL::Vector<value_type> & rol_vector)
430  {
431  Assert(this->dimension() == rol_vector.dimension(),
432  ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
433 
434  const VectorAdaptor &vector_adaptor =
435  Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
436 
437  const VectorType &given_rol_vector = *(vector_adaptor.getVector());
438 
439  const typename VectorType::iterator vend = vector_ptr->end();
440  const typename VectorType::const_iterator rolend = given_rol_vector.end();
441 
442  typename VectorType::const_iterator r_iterator = given_rol_vector.begin();
443  for (typename VectorType::iterator l_iterator = vector_ptr->begin();
444  l_iterator != vend && r_iterator != rolend;
445  l_iterator++, r_iterator++)
446  *l_iterator = f.apply(*l_iterator, *r_iterator);
447 
448  vector_ptr->compress(VectorOperation::insert);
449  }
450 
451 
452 
453  template <typename VectorType>
454  typename VectorType::value_type
456  const ROL::Elementwise::ReductionOp<value_type> &r) const
457  {
458  typename VectorType::value_type result = r.initialValue();
459 
460  const typename VectorType::iterator vend = vector_ptr->end();
461 
462  for (typename VectorType::iterator iterator = vector_ptr->begin();
463  iterator != vend;
464  iterator++)
465  r.reduce(*iterator, result);
466  // Parallel reduction among processes is handled internally.
467 
468  return result;
469  }
470 
471 
472 
473  template <typename VectorType>
474  void
475  VectorAdaptor<VectorType>::print(std::ostream &outStream) const
476  {
477  vector_ptr->print(outStream);
478  }
479 
480 
481 # endif // DOXYGEN
482 
483 
484 } // namespace Rol
485 
486 
487 DEAL_II_NAMESPACE_CLOSE
488 
489 
490 #endif // DEAL_II_TRILINOS_WITH_ROL
491 
492 #endif // dealii_optimization_rol_vector_adaptor_h
void applyBinary(const ROL::Elementwise::BinaryFunction< value_type > &f, const ROL::Vector< value_type > &rol_vector)
VectorAdaptor(const Teuchos::RCP< VectorType > &vector_ptr)
Teuchos::RCP< ROL::Vector< value_type > > clone() const
Teuchos::RCP< VectorType > getVector()
void applyUnary(const ROL::Elementwise::UnaryFunction< value_type > &f)
value_type norm() const
LinearAlgebra::distributed::Vector< Number > Vector
Teuchos::RCP< VectorType > vector_ptr
typename VectorType::value_type value_type
static ::ExceptionBase & ExcMessage(std::string arg1)
void plus(const ROL::Vector< value_type > &rol_vector)
#define Assert(cond, exc)
Definition: exceptions.h:1407
value_type dot(const ROL::Vector< value_type > &rol_vector) const
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void print(std::ostream &outStream) const
int dimension() const
value_type reduce(const ROL::Elementwise::ReductionOp< value_type > &r) const
Teuchos::RCP< ROL::Vector< value_type > > basis(const int i) const
typename VectorType::real_type real_type
typename VectorType::size_type size_type
void axpy(const value_type alpha, const ROL::Vector< value_type > &rol_vector)
void set(const ROL::Vector< value_type > &rol_vector)
void scale(const value_type alpha)