13#ifndef dealii_trilinos_rol_adaptor_h
14#define dealii_trilinos_rol_adaptor_h
18#ifdef DEAL_II_TRILINOS_WITH_ROL
24# include <ROL_Vector.hpp>
28# include <type_traits>
34#ifdef DEAL_II_TRILINOS_WITH_ROL
125 template <
typename VectorType>
126 class ROLAdaptor :
public ROL::Vector<typename VectorType::value_type>
143 static_assert(std::is_convertible_v<real_type, value_type>,
144 "The real_type of the current VectorType is not "
145 "convertible to the value_type.");
204 ROL::Ptr<const VectorType>
224 set(
const ROL::Vector<value_type> &rol_vector)
override;
236 plus(
const ROL::Vector<value_type> &rol_vector)
override;
249 const ROL::Vector<value_type> &rol_vector)
override;
270 dot(
const ROL::Vector<value_type> &rol_vector)
const override;
292 ROL::Ptr<ROL::Vector<value_type>>
299 ROL::Ptr<ROL::Vector<value_type>>
309 applyUnary(
const ROL::Elementwise::UnaryFunction<value_type> &f)
override;
322 applyBinary(
const ROL::Elementwise::BinaryFunction<value_type> &f,
323 const ROL::Vector<value_type> &rol_vector)
override;
333 reduce(
const ROL::Elementwise::ReductionOp<value_type> &r)
const override;
342 print(std::ostream &outStream)
const override;
350 template <
typename VectorType>
352 :
ROLAdaptor(vector_ptr, vector_ptr->locally_owned_elements())
357 template <
typename VectorType>
360 : vector_ptr(vector_ptr)
361 , optimization_space(optimization_space)
366 "Provided IndexSet needs to be a subset of locally owned indices."));
375 std::numeric_limits<int>::max()),
376 ExcMessage(
"The number of elements to optimize is greater than the "
377 "largest value of type int."));
382 template <
typename VectorType>
391 template <
typename VectorType>
392 ROL::Ptr<const VectorType>
400 template <
typename VectorType>
404 Assert(
dynamic_cast<const ROLAdaptor *
>(&other_) !=
nullptr,
406 const ROLAdaptor &other =
dynamic_cast<const ROLAdaptor &
>(other_);
409 (*vector_ptr) = *(other.getVector());
411 optimization_space = other.optimization_space;
412 global_opt_dimension = other.global_opt_dimension;
413 local_opt_start_index = other.local_opt_start_index;
418 template <
typename VectorType>
422 Assert(
dynamic_cast<const ROLAdaptor *
>(&other_) !=
nullptr,
424 const ROLAdaptor &other =
dynamic_cast<const ROLAdaptor &
>(other_);
427 Assert(optimization_space.
size() == vector_ptr->size(),
428 ExcMessage(
"Optimization space is out-of-sync. "
429 "Please create a new wrapper."));
430 Assert(optimization_space == other.optimization_space,
431 ExcMessage(
"Optimization spaces of vectors do not match."));
433 for (
const auto i : optimization_space)
434 (*vector_ptr)[i] += (*other.getVector())[i];
441 template <
typename VectorType>
444 const ROL::Vector<value_type> &other_)
446 Assert(
dynamic_cast<const ROLAdaptor *
>(&other_) !=
nullptr,
448 const ROLAdaptor &other =
dynamic_cast<const ROLAdaptor &
>(other_);
451 Assert(optimization_space.
size() == vector_ptr->size(),
452 ExcMessage(
"Optimization space is out-of-sync. "
453 "Please create a new wrapper."));
454 Assert(optimization_space == other.optimization_space,
455 ExcMessage(
"Optimization spaces of vectors do not match."));
457 for (
const auto i : optimization_space)
458 (*vector_ptr)[i] += alpha * (*other.getVector())[i];
465 template <
typename VectorType>
469 Assert(optimization_space.
size() == vector_ptr->size(),
470 ExcMessage(
"Optimization space is out-of-sync. "
471 "Please create a new wrapper."));
473 return static_cast<int>(global_opt_dimension);
478 template <
typename VectorType>
483 Assert(optimization_space.
size() == vector_ptr->size(),
484 ExcMessage(
"Optimization space is out-of-sync. "
485 "Please create a new wrapper."));
487 for (
const auto i : optimization_space)
488 (*vector_ptr)[i] *= alpha;
495 template <
typename VectorType>
496 typename VectorType::value_type
499 Assert(
dynamic_cast<const ROLAdaptor *
>(&other_) !=
nullptr,
501 const ROLAdaptor &other =
dynamic_cast<const ROLAdaptor &
>(other_);
503 Assert(optimization_space.
size() == vector_ptr->size(),
504 ExcMessage(
"Optimization space is out-of-sync. "
505 "Please create a new wrapper."));
506 Assert(optimization_space == other.optimization_space,
507 ExcMessage(
"Optimization spaces of vectors do not match."));
510 for (
const auto i : optimization_space)
511 dot += (*vector_ptr)[i] * (*other.getVector())[i];
513 return Utilities::MPI::sum<value_type>(dot,
514 vector_ptr->get_mpi_communicator());
519 template <
typename VectorType>
520 typename VectorType::value_type
528 template <
typename VectorType>
529 ROL::Ptr<ROL::Vector<typename VectorType::value_type>>
533 ROL::Ptr<VectorType> clone_ptr = ROL::makePtr<VectorType>();
534 clone_ptr->reinit(*vector_ptr,
false);
536 return ROL::makePtr<ROLAdaptor>(clone_ptr, optimization_space);
542 template <
typename VectorType>
543 ROL::Ptr<ROL::Vector<typename VectorType::value_type>>
546 Assert(optimization_space.
size() == vector_ptr->size(),
547 ExcMessage(
"Optimization space is out-of-sync. "
548 "Please create a new wrapper."));
552 ROL::Ptr<VectorType> basis_ptr = ROL::makePtr<VectorType>();
553 basis_ptr->reinit(*vector_ptr,
false);
557 if ((global_opt_index >= local_opt_start_index) &&
559 local_opt_start_index + optimization_space.
n_elements()))
561 const ::types::global_dof_index local_opt_index =
562 global_opt_index - local_opt_start_index;
573 return ROL::makePtr<ROLAdaptor>(basis_ptr, optimization_space);
579 template <
typename VectorType>
582 const ROL::Elementwise::UnaryFunction<value_type> &f)
585 Assert(optimization_space.
size() == vector_ptr->size(),
586 ExcMessage(
"Optimization space is out-of-sync. "
587 "Please create a new wrapper."));
589 for (
const auto i : optimization_space)
590 (*vector_ptr)[i] = f.
apply((*vector_ptr)[i]);
597 template <
typename VectorType>
600 const ROL::Elementwise::BinaryFunction<value_type> &f,
601 const ROL::Vector<value_type> &other_)
603 Assert(
dynamic_cast<const ROLAdaptor *
>(&other_) !=
nullptr,
605 const ROLAdaptor &other =
dynamic_cast<const ROLAdaptor &
>(other_);
608 Assert(optimization_space.
size() == vector_ptr->size(),
609 ExcMessage(
"Optimization space is out-of-sync. "
610 "Please create a new wrapper."));
611 Assert(optimization_space == other.optimization_space,
612 ExcMessage(
"Optimization spaces of vectors do not match."));
614 for (
const auto i : optimization_space)
615 (*vector_ptr)[i] = f.
apply((*vector_ptr)[i], (*other.getVector())[i]);
622 template <
typename VectorType>
623 typename VectorType::value_type
625 const ROL::Elementwise::ReductionOp<value_type> &r)
const
627 Assert(optimization_space.
size() == vector_ptr->size(),
628 ExcMessage(
"Optimization space is out-of-sync. "
629 "Please create a new wrapper."));
634 for (
const auto i : optimization_space)
635 r.
reduce((*vector_ptr)[i], result);
638 const auto combiner = [&r](
const value_type a,
645 return Utilities::MPI::all_reduce<value_type>(
646 result, vector_ptr->get_mpi_communicator(), combiner);
651 template <
typename VectorType>
655 vector_ptr->print(outStream);
bool is_subset_of(const IndexSet &other) const
size_type n_elements() const
size_type nth_index_in_set(const size_type local_index) const
void applyUnary(const ROL::Elementwise::UnaryFunction< value_type > &f) override
value_type reduce(const ROL::Elementwise::ReductionOp< value_type > &r) const override
IndexSet optimization_space
::types::global_dof_index global_opt_dimension
void set(const ROL::Vector< value_type > &rol_vector) override
ROL::Ptr< const VectorType > getVector() const
ROL::Ptr< VectorType > vector_ptr
value_type dot(const ROL::Vector< value_type > &rol_vector) const override
void axpy(const value_type alpha, const ROL::Vector< value_type > &rol_vector) override
void print(std::ostream &outStream) const override
int dimension() const override
typename VectorType::size_type size_type
void scale(const value_type alpha) override
::types::global_dof_index local_opt_start_index
void plus(const ROL::Vector< value_type > &rol_vector) override
ROL::Ptr< VectorType > getVector()
value_type norm() const override
typename VectorType::real_type real_type
ROL::Ptr< ROL::Vector< value_type > > clone() const override
ROL::Ptr< ROL::Vector< value_type > > basis(const int i) const override
void applyBinary(const ROL::Elementwise::BinaryFunction< value_type > &f, const ROL::Vector< value_type > &rol_vector) override
ROLAdaptor(const ROL::Ptr< VectorType > &vector_ptr, const IndexSet &optimization_space)
typename VectorType::value_type value_type
ROLAdaptor(const ROL::Ptr< VectorType > &vector_ptr)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcGhostsPresent()
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
void apply(const Kokkos::TeamPolicy< MemorySpace::Default::kokkos_space::execution_space >::member_type &team_member, const Kokkos::View< Number *, ShapeDataMemorySpace > shape_data, const ViewTypeIn in, ViewTypeOut out)
std::pair< T, T > partial_and_total_sum(const T &value, const MPI_Comm comm)
T reduce(const T &local_value, const MPI_Comm comm, const std::function< T(const T &, const T &)> &combiner, const unsigned int root_process=0)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
unsigned int global_dof_index