Reference documentation for deal.II version 9.2.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
vector_adaptor.h
Go to the documentation of this file.
1 //-----------------------------------------------------------
2 //
3 // Copyright (C) 2017 - 2020 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 
32 
38 namespace Rol
39 {
111  template <typename VectorType>
112  class VectorAdaptor : public ROL::Vector<typename VectorType::value_type>
113  {
118 
122  using value_type = typename VectorType::value_type;
123 
127  using real_type = typename VectorType::real_type;
128 
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  if (vec_ptr->has_ghost_elements())
400  {
401  vec_ptr->update_ghost_values();
402  }
403  else
404  {
405  vec_ptr->compress(VectorOperation::insert);
406  }
407 
408  Teuchos::RCP<VectorAdaptor> e = Teuchos::rcp(new VectorAdaptor(vec_ptr));
409 
410  return e;
411  }
412 
413 
414 
415  template <typename VectorType>
416  void
418  const ROL::Elementwise::UnaryFunction<value_type> &f)
419  {
420  const typename VectorType::iterator vend = vector_ptr->end();
421 
422  for (typename VectorType::iterator iterator = vector_ptr->begin();
423  iterator != vend;
424  iterator++)
425  *iterator = f.apply(*iterator);
426 
427  if (vector_ptr->has_ghost_elements())
428  {
429  vector_ptr->update_ghost_values();
430  }
431  else
432  {
433  vector_ptr->compress(VectorOperation::insert);
434  }
435  }
436 
437 
438 
439  template <typename VectorType>
440  void
442  const ROL::Elementwise::BinaryFunction<value_type> &f,
443  const ROL::Vector<value_type> & rol_vector)
444  {
445  Assert(this->dimension() == rol_vector.dimension(),
446  ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
447 
448  const VectorAdaptor &vector_adaptor =
449  Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
450 
451  const VectorType &given_rol_vector = *(vector_adaptor.getVector());
452 
453  const typename VectorType::iterator vend = vector_ptr->end();
454  const typename VectorType::const_iterator rolend = given_rol_vector.end();
455 
456  typename VectorType::const_iterator r_iterator = given_rol_vector.begin();
457  for (typename VectorType::iterator l_iterator = vector_ptr->begin();
458  l_iterator != vend && r_iterator != rolend;
459  l_iterator++, r_iterator++)
460  *l_iterator = f.apply(*l_iterator, *r_iterator);
461 
462  if (vector_ptr->has_ghost_elements())
463  {
464  vector_ptr->update_ghost_values();
465  }
466  else
467  {
468  vector_ptr->compress(VectorOperation::insert);
469  }
470  }
471 
472 
473 
474  template <typename VectorType>
475  typename VectorType::value_type
477  const ROL::Elementwise::ReductionOp<value_type> &r) const
478  {
479  typename VectorType::value_type result = r.initialValue();
480 
481  const typename VectorType::iterator vend = vector_ptr->end();
482 
483  for (typename VectorType::iterator iterator = vector_ptr->begin();
484  iterator != vend;
485  iterator++)
486  r.reduce(*iterator, result);
487  // Parallel reduction among processes is handled internally.
488 
489  return result;
490  }
491 
492 
493 
494  template <typename VectorType>
495  void
496  VectorAdaptor<VectorType>::print(std::ostream &outStream) const
497  {
498  vector_ptr->print(outStream);
499  }
500 
501 
502 # endif // DOXYGEN
503 
504 
505 } // namespace Rol
506 
507 
509 
510 
511 #endif // DEAL_II_TRILINOS_WITH_ROL
512 
513 #endif // dealii_optimization_rol_vector_adaptor_h
Rol::VectorAdaptor
Definition: vector_adaptor.h:112
LinearAlgebraDealII::Vector
Vector< double > Vector
Definition: generic_linear_algebra.h:43
Rol::VectorAdaptor::VectorAdaptor
VectorAdaptor(const Teuchos::RCP< VectorType > &vector_ptr)
Rol::VectorAdaptor::print
void print(std::ostream &outStream) const
VectorOperation::insert
@ insert
Definition: vector_operation.h:49
Rol::VectorAdaptor::real_type
typename VectorType::real_type real_type
Definition: vector_adaptor.h:127
Rol::VectorAdaptor::size_type
typename VectorType::size_type size_type
Definition: vector_adaptor.h:117
Rol
Definition: vector_adaptor.h:38
VectorType
Physics::Elasticity::Kinematics::e
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
Rol::VectorAdaptor::norm
value_type norm() const
Rol::VectorAdaptor::set
void set(const ROL::Vector< value_type > &rol_vector)
Rol::VectorAdaptor::getVector
Teuchos::RCP< VectorType > getVector()
Rol::VectorAdaptor::dot
value_type dot(const ROL::Vector< value_type > &rol_vector) const
StandardExceptions::ExcMessage
static ::ExceptionBase & ExcMessage(std::string arg1)
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
Rol::VectorAdaptor::basis
Teuchos::RCP< ROL::Vector< value_type > > basis(const int i) const
Rol::VectorAdaptor::reduce
value_type reduce(const ROL::Elementwise::ReductionOp< value_type > &r) const
Rol::VectorAdaptor::applyBinary
void applyBinary(const ROL::Elementwise::BinaryFunction< value_type > &f, const ROL::Vector< value_type > &rol_vector)
exceptions.h
value
static const bool value
Definition: dof_tools_constraints.cc:433
LinearAlgebra::CUDAWrappers::kernel::size_type
types::global_dof_index size_type
Definition: cuda_kernels.h:45
Rol::VectorAdaptor::value_type
typename VectorType::value_type value_type
Definition: vector_adaptor.h:122
Assert
#define Assert(cond, exc)
Definition: exceptions.h:1419
Rol::VectorAdaptor::vector_ptr
Teuchos::RCP< VectorType > vector_ptr
Definition: vector_adaptor.h:131
vector.h
Rol::VectorAdaptor::plus
void plus(const ROL::Vector< value_type > &rol_vector)
Rol::VectorAdaptor::clone
Teuchos::RCP< ROL::Vector< value_type > > clone() const
StandardExceptions::ExcDimensionMismatch
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
config.h
Rol::VectorAdaptor::dimension
int dimension() const
Rol::VectorAdaptor::applyUnary
void applyUnary(const ROL::Elementwise::UnaryFunction< value_type > &f)
Rol::VectorAdaptor::scale
void scale(const value_type alpha)
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
Rol::VectorAdaptor::axpy
void axpy(const value_type alpha, const ROL::Vector< value_type > &rol_vector)
value_type
Utilities::MPI::max
T max(const T &t, const MPI_Comm &mpi_communicator)