Reference documentation for deal.II version 9.1.0-pre
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 
33 
34 using namespace dealii;
35 
36 
42 namespace Rol
43 {
114  template <typename VectorType>
115  class VectorAdaptor : public ROL::Vector<typename VectorType::value_type>
116  {
120  using size_type = typename VectorType::size_type;
121 
125  using value_type = typename VectorType::value_type;
126 
130  using real_type = typename VectorType::real_type;
131 
132  static_assert(std::is_convertible<real_type, value_type>::value,
133  "The real_type of the current VectorType is not "
134  "convertible to the value_type.");
135 
136  private:
141  Teuchos::RCP<VectorType> vector_ptr;
142 
143  public:
147  VectorAdaptor(const Teuchos::RCP<VectorType> &vector_ptr);
148 
153  Teuchos::RCP<VectorType>
154  getVector();
155 
159  Teuchos::RCP<const VectorType>
160  getVector() const;
161 
165  int
166  dimension() const;
167 
176  void
177  set(const ROL::Vector<value_type> &rol_vector);
178 
182  void
183  plus(const ROL::Vector<value_type> &rol_vector);
184 
189  void
190  axpy(const value_type alpha, const ROL::Vector<value_type> &rol_vector);
191 
195  void
196  scale(const value_type alpha);
197 
201  value_type
202  dot(const ROL::Vector<value_type> &rol_vector) const;
203 
213  value_type
214  norm() const;
215 
219  Teuchos::RCP<ROL::Vector<value_type>>
220  clone() const;
221 
227  Teuchos::RCP<ROL::Vector<value_type>>
228  basis(const int i) const;
229 
233  void
234  applyUnary(const ROL::Elementwise::UnaryFunction<value_type> &f);
235 
240  void
241  applyBinary(const ROL::Elementwise::BinaryFunction<value_type> &f,
242  const ROL::Vector<value_type> & rol_vector);
243 
248  value_type
249  reduce(const ROL::Elementwise::ReductionOp<value_type> &r) const;
250 
254  void
255  print(std::ostream &outStream) const;
256  };
257 
258 
259  /*------------------------------member definitions--------------------------*/
260 # ifndef DOXYGEN
261 
262 
263  template <typename VectorType>
265  const Teuchos::RCP<VectorType> &vector_ptr)
266  : vector_ptr(vector_ptr)
267  {}
268 
269 
270 
271  template <typename VectorType>
272  Teuchos::RCP<VectorType>
274  {
275  return vector_ptr;
276  }
277 
278 
279 
280  template <typename VectorType>
281  Teuchos::RCP<const VectorType>
283  {
284  return vector_ptr;
285  }
286 
287 
288 
289  template <typename VectorType>
290  void
291  VectorAdaptor<VectorType>::set(const ROL::Vector<value_type> &rol_vector)
292  {
293  const VectorAdaptor &vector_adaptor =
294  Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
295 
296  (*vector_ptr) = *(vector_adaptor.getVector());
297  }
298 
299 
300 
301  template <typename VectorType>
302  void
303  VectorAdaptor<VectorType>::plus(const ROL::Vector<value_type> &rol_vector)
304  {
305  Assert(this->dimension() == rol_vector.dimension(),
306  ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
307 
308  const VectorAdaptor &vector_adaptor =
309  Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
310 
311  *vector_ptr += *(vector_adaptor.getVector());
312  }
313 
314 
315 
316  template <typename VectorType>
317  void
319  const ROL::Vector<value_type> &rol_vector)
320  {
321  Assert(this->dimension() == rol_vector.dimension(),
322  ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
323 
324  const VectorAdaptor &vector_adaptor =
325  Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
326 
327  vector_ptr->add(alpha, *(vector_adaptor.getVector()));
328  }
329 
330 
331 
332  template <typename VectorType>
333  int
335  {
336  Assert(vector_ptr->size() < std::numeric_limits<int>::max(),
337  ExcMessage("The size of the vector being used is greater than "
338  "largest value of type int."));
339  return static_cast<int>(vector_ptr->size());
340  }
341 
342 
343 
344  template <typename VectorType>
345  void
347  {
348  (*vector_ptr) *= alpha;
349  }
350 
351 
352 
353  template <typename VectorType>
354  typename VectorType::value_type
356  const ROL::Vector<value_type> &rol_vector) const
357  {
358  Assert(this->dimension() == rol_vector.dimension(),
359  ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
360 
361  const VectorAdaptor &vector_adaptor =
362  Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
363 
364  return (*vector_ptr) * (*vector_adaptor.getVector());
365  }
366 
367 
368 
369  template <typename VectorType>
370  typename VectorType::value_type
372  {
373  return vector_ptr->l2_norm();
374  }
375 
376 
377 
378  template <typename VectorType>
379  Teuchos::RCP<ROL::Vector<typename VectorType::value_type>>
381  {
382  Teuchos::RCP<VectorType> vec_ptr = Teuchos::rcp(new VectorType);
383  (*vec_ptr) = (*vector_ptr);
384 
385  return Teuchos::rcp(new VectorAdaptor(vec_ptr));
386  }
387 
388 
389 
390  template <typename VectorType>
391  Teuchos::RCP<ROL::Vector<typename VectorType::value_type>>
392  VectorAdaptor<VectorType>::basis(const int i) const
393  {
394  Teuchos::RCP<VectorType> vec_ptr = Teuchos::rcp(new VectorType);
395 
396  // Zero all the entries in dealii vector.
397  vec_ptr->reinit(*vector_ptr, false);
398 
399  if (vector_ptr->locally_owned_elements().is_element(i))
400  vec_ptr->operator[](i) = 1.;
401 
402  vec_ptr->compress(VectorOperation::insert);
403 
404  Teuchos::RCP<VectorAdaptor> e = Teuchos::rcp(new VectorAdaptor(vec_ptr));
405 
406  return e;
407  }
408 
409 
410 
411  template <typename VectorType>
412  void
414  const ROL::Elementwise::UnaryFunction<value_type> &f)
415  {
416  const typename VectorType::iterator vend = vector_ptr->end();
417 
418  for (typename VectorType::iterator iterator = vector_ptr->begin();
419  iterator != vend;
420  iterator++)
421  *iterator = f.apply(*iterator);
422 
423  vector_ptr->compress(VectorOperation::insert);
424  }
425 
426 
427 
428  template <typename VectorType>
429  void
431  const ROL::Elementwise::BinaryFunction<value_type> &f,
432  const ROL::Vector<value_type> & rol_vector)
433  {
434  Assert(this->dimension() == rol_vector.dimension(),
435  ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
436 
437  const VectorAdaptor &vector_adaptor =
438  Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
439 
440  const VectorType &given_rol_vector = *(vector_adaptor.getVector());
441 
442  const typename VectorType::iterator vend = vector_ptr->end();
443  const typename VectorType::const_iterator rolend = given_rol_vector.end();
444 
445  typename VectorType::const_iterator r_iterator = given_rol_vector.begin();
446  for (typename VectorType::iterator l_iterator = vector_ptr->begin();
447  l_iterator != vend && r_iterator != rolend;
448  l_iterator++, r_iterator++)
449  *l_iterator = f.apply(*l_iterator, *r_iterator);
450 
451  vector_ptr->compress(VectorOperation::insert);
452  }
453 
454 
455 
456  template <typename VectorType>
457  typename VectorType::value_type
459  const ROL::Elementwise::ReductionOp<value_type> &r) const
460  {
461  typename VectorType::value_type result = r.initialValue();
462 
463  const typename VectorType::iterator vend = vector_ptr->end();
464 
465  for (typename VectorType::iterator iterator = vector_ptr->begin();
466  iterator != vend;
467  iterator++)
468  r.reduce(*iterator, result);
469  // Parallel reduction among processes is handled internally.
470 
471  return result;
472  }
473 
474 
475 
476  template <typename VectorType>
477  void
478  VectorAdaptor<VectorType>::print(std::ostream &outStream) const
479  {
480  vector_ptr->print(outStream);
481  }
482 
483 
484 # endif // DOXYGEN
485 
486 
487 } // namespace Rol
488 
489 
490 DEAL_II_NAMESPACE_CLOSE
491 
492 
493 #endif // DEAL_II_TRILINOS_WITH_ROL
494 
495 #endif // dealii_optimization_rol_vector_adaptor_h
void print(std::ostream &outStream) const
value_type norm() const
void applyBinary(const ROL::Elementwise::BinaryFunction< value_type > &f, const ROL::Vector< value_type > &rol_vector)
Teuchos::RCP< ROL::Vector< value_type > > clone() const
VectorAdaptor(const Teuchos::RCP< VectorType > &vector_ptr)
value_type dot(const ROL::Vector< value_type > &rol_vector) const
Teuchos::RCP< ROL::Vector< value_type > > basis(const int i) const
Teuchos::RCP< VectorType > getVector()
void applyUnary(const ROL::Elementwise::UnaryFunction< value_type > &f)
Teuchos::RCP< VectorType > vector_ptr
typename VectorType::value_type value_type
int dimension() const
static::ExceptionBase & ExcMessage(std::string arg1)
void plus(const ROL::Vector< value_type > &rol_vector)
#define Assert(cond, exc)
Definition: exceptions.h:1227
static::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
value_type reduce(const ROL::Elementwise::ReductionOp< value_type > &r) 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)