deal.II version GIT relicensing-2291-g668cd86249 2024-12-24 11:30:00+00:00
\(\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\}}\)
Loading...
Searching...
No Matches
vector_adaptor.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2017 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_optimization_rol_vector_adaptor_h
16#define dealii_optimization_rol_vector_adaptor_h
17
18#include <deal.II/base/config.h>
19
20#ifdef DEAL_II_TRILINOS_WITH_ROL
22
23# include <deal.II/lac/vector.h>
24
25# include <ROL_Vector.hpp>
26
27# include <limits>
28# include <type_traits>
29
30
32
38namespace Rol
39{
109 template <typename VectorType>
110 class VectorAdaptor : public ROL::Vector<typename VectorType::value_type>
111 {
115 using size_type = typename VectorType::size_type;
116
120 using value_type = typename VectorType::value_type;
121
125 using real_type = typename VectorType::real_type;
126
127 static_assert(std::is_convertible_v<real_type, value_type>,
128 "The real_type of the current VectorType is not "
129 "convertible to the value_type.");
130
131 private:
136 Teuchos::RCP<VectorType> vector_ptr;
137
138 public:
142 VectorAdaptor(const Teuchos::RCP<VectorType> &vector_ptr);
143
148 Teuchos::RCP<VectorType>
150
154 Teuchos::RCP<const VectorType>
155 getVector() const;
156
160 int
161 dimension() const;
162
171 void
172 set(const ROL::Vector<value_type> &rol_vector);
173
177 void
178 plus(const ROL::Vector<value_type> &rol_vector);
179
184 void
185 axpy(const value_type alpha, const ROL::Vector<value_type> &rol_vector);
186
190 void
191 scale(const value_type alpha);
192
197 dot(const ROL::Vector<value_type> &rol_vector) const;
198
209 norm() const;
210
214 Teuchos::RCP<ROL::Vector<value_type>>
215 clone() const;
216
222 Teuchos::RCP<ROL::Vector<value_type>>
223 basis(const int i) const;
224
228 void
229 applyUnary(const ROL::Elementwise::UnaryFunction<value_type> &f);
230
235 void
236 applyBinary(const ROL::Elementwise::BinaryFunction<value_type> &f,
237 const ROL::Vector<value_type> &rol_vector);
238
244 reduce(const ROL::Elementwise::ReductionOp<value_type> &r) const;
245
249 void
250 print(std::ostream &outStream) const;
251 };
252
253
254 /*------------------------------member definitions--------------------------*/
255# ifndef DOXYGEN
256
257
258 template <typename VectorType>
260 const Teuchos::RCP<VectorType> &vector_ptr)
261 : vector_ptr(vector_ptr)
262 {}
263
264
265
266 template <typename VectorType>
267 Teuchos::RCP<VectorType>
269 {
270 return vector_ptr;
271 }
272
273
274
275 template <typename VectorType>
276 Teuchos::RCP<const VectorType>
278 {
279 return vector_ptr;
280 }
281
282
283
284 template <typename VectorType>
285 void
286 VectorAdaptor<VectorType>::set(const ROL::Vector<value_type> &rol_vector)
287 {
288 const VectorAdaptor &vector_adaptor =
289 Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
290
291 (*vector_ptr) = *(vector_adaptor.getVector());
292 }
293
294
295
296 template <typename VectorType>
297 void
298 VectorAdaptor<VectorType>::plus(const ROL::Vector<value_type> &rol_vector)
299 {
300 Assert(this->dimension() == rol_vector.dimension(),
301 ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
302
303 const VectorAdaptor &vector_adaptor =
304 Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
305
306 *vector_ptr += *(vector_adaptor.getVector());
307 }
308
309
310
311 template <typename VectorType>
312 void
313 VectorAdaptor<VectorType>::axpy(const value_type alpha,
314 const ROL::Vector<value_type> &rol_vector)
315 {
316 Assert(this->dimension() == rol_vector.dimension(),
317 ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
318
319 const VectorAdaptor &vector_adaptor =
320 Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
321
322 vector_ptr->add(alpha, *(vector_adaptor.getVector()));
323 }
324
325
326
327 template <typename VectorType>
328 int
330 {
331 Assert(vector_ptr->size() < std::numeric_limits<int>::max(),
332 ExcMessage("The size of the vector being used is greater than "
333 "largest value of type int."));
334 return static_cast<int>(vector_ptr->size());
335 }
336
337
338
339 template <typename VectorType>
340 void
341 VectorAdaptor<VectorType>::scale(const value_type alpha)
342 {
343 (*vector_ptr) *= alpha;
344 }
345
346
347
348 template <typename VectorType>
349 typename VectorType::value_type
351 const ROL::Vector<value_type> &rol_vector) const
352 {
353 Assert(this->dimension() == rol_vector.dimension(),
354 ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
355
356 const VectorAdaptor &vector_adaptor =
357 Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
358
359 return (*vector_ptr) * (*vector_adaptor.getVector());
360 }
361
362
363
364 template <typename VectorType>
365 typename VectorType::value_type
367 {
368 return vector_ptr->l2_norm();
369 }
370
371
372
373 template <typename VectorType>
374 Teuchos::RCP<ROL::Vector<typename VectorType::value_type>>
376 {
377 Teuchos::RCP<VectorType> vec_ptr = Teuchos::rcp(new VectorType);
378 (*vec_ptr) = (*vector_ptr);
379
380 return Teuchos::rcp(new VectorAdaptor(vec_ptr));
381 }
382
383
384
385 template <typename VectorType>
386 Teuchos::RCP<ROL::Vector<typename VectorType::value_type>>
387 VectorAdaptor<VectorType>::basis(const int i) const
388 {
389 Teuchos::RCP<VectorType> vec_ptr = Teuchos::rcp(new VectorType);
390
391 // Zero all the entries in dealii vector.
392 vec_ptr->reinit(*vector_ptr, false);
393
394 if (vector_ptr->locally_owned_elements().is_element(i))
395 vec_ptr->operator[](i) = 1.;
396
397 if (vec_ptr->has_ghost_elements())
398 {
399 vec_ptr->update_ghost_values();
400 }
401 else
402 {
403 vec_ptr->compress(VectorOperation::insert);
404 }
405
406 Teuchos::RCP<VectorAdaptor> e = Teuchos::rcp(new VectorAdaptor(vec_ptr));
407
408 return e;
409 }
410
411
412
413 template <typename VectorType>
414 void
416 const ROL::Elementwise::UnaryFunction<value_type> &f)
417 {
418 const typename VectorType::iterator vend = vector_ptr->end();
419
420 for (typename VectorType::iterator iterator = vector_ptr->begin();
421 iterator != vend;
422 iterator++)
423 *iterator = f.apply(*iterator);
424
425 if (vector_ptr->has_ghost_elements())
426 {
427 vector_ptr->update_ghost_values();
428 }
429 else
430 {
431 vector_ptr->compress(VectorOperation::insert);
432 }
433 }
434
435
436
437 template <typename VectorType>
438 void
440 const ROL::Elementwise::BinaryFunction<value_type> &f,
441 const ROL::Vector<value_type> &rol_vector)
442 {
443 Assert(this->dimension() == rol_vector.dimension(),
444 ExcDimensionMismatch(this->dimension(), rol_vector.dimension()));
445
446 const VectorAdaptor &vector_adaptor =
447 Teuchos::dyn_cast<const VectorAdaptor>(rol_vector);
448
449 const VectorType &given_rol_vector = *(vector_adaptor.getVector());
450
451 const typename VectorType::iterator vend = vector_ptr->end();
452 const typename VectorType::const_iterator rolend = given_rol_vector.end();
453
454 typename VectorType::const_iterator r_iterator = given_rol_vector.begin();
455 for (typename VectorType::iterator l_iterator = vector_ptr->begin();
456 l_iterator != vend && r_iterator != rolend;
457 l_iterator++, r_iterator++)
458 *l_iterator = f.apply(*l_iterator, *r_iterator);
459
460 if (vector_ptr->has_ghost_elements())
461 {
462 vector_ptr->update_ghost_values();
463 }
464 else
465 {
466 vector_ptr->compress(VectorOperation::insert);
467 }
468 }
469
470
471
472 template <typename VectorType>
473 typename VectorType::value_type
475 const ROL::Elementwise::ReductionOp<value_type> &r) const
476 {
477 typename VectorType::value_type result = r.initialValue();
478
479 const typename VectorType::iterator vend = vector_ptr->end();
480
481 for (typename VectorType::iterator iterator = vector_ptr->begin();
482 iterator != vend;
483 iterator++)
484 r.reduce(*iterator, result);
485 // Parallel reduction among processes is handled internally.
486
487 return result;
488 }
489
490
491
492 template <typename VectorType>
493 void
494 VectorAdaptor<VectorType>::print(std::ostream &outStream) const
495 {
496 vector_ptr->print(outStream);
497 }
498
499
500# endif // DOXYGEN
501
502
503} // namespace Rol
504
505
507
508
509#endif // DEAL_II_TRILINOS_WITH_ROL
510
511#endif // dealii_optimization_rol_vector_adaptor_h
void applyBinary(const ROL::Elementwise::BinaryFunction< value_type > &f, const ROL::Vector< value_type > &rol_vector)
Teuchos::RCP< ROL::Vector< value_type > > clone() const
void scale(const value_type alpha)
value_type dot(const ROL::Vector< value_type > &rol_vector) const
void applyUnary(const ROL::Elementwise::UnaryFunction< value_type > &f)
Teuchos::RCP< VectorType > vector_ptr
Teuchos::RCP< VectorType > getVector()
int dimension() const
typename VectorType::value_type value_type
VectorAdaptor(const Teuchos::RCP< VectorType > &vector_ptr)
typename VectorType::real_type real_type
void print(std::ostream &outStream) const
typename VectorType::size_type size_type
void set(const ROL::Vector< value_type > &rol_vector)
Teuchos::RCP< const VectorType > getVector() const
void plus(const ROL::Vector< value_type > &rol_vector)
Teuchos::RCP< ROL::Vector< value_type > > basis(const int i) const
void axpy(const value_type alpha, const ROL::Vector< value_type > &rol_vector)
value_type reduce(const ROL::Elementwise::ReductionOp< value_type > &r) const
value_type norm() const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
#define Assert(cond, exc)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
Tpetra::Vector< Number, LO, GO, NodeType< MemorySpace > > VectorType
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)