Debian Codegraph @

include/deal.II/base/template_constraints.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2003 - 2025 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_template_constraints_h
16 #define dealii_template_constraints_h
17
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/complex_overloads.h>
22 #include <deal.II/base/mpi_stub.h>
23 #include <deal.II/base/std_cxx20/type_traits.h>
24
25 #include <complex>
26 #include <type_traits>
27 #include <utility>
28
29
30 DEAL_II_NAMESPACE_OPEN
31
32 // Detection idiom adapted from Version 2 of the C++ Extensions for Library
33 // Fundamentals, ISO/IEC TS 19568:2017
34 namespace internal
35 {
36 /**
37 * A namespace used to declare the machinery for detecting whether a specific
38 * class supports an operation. This approach simulates C++20-style
39 * concepts with language standards before C++20.
40 */
41 namespace SupportsOperation
42 {
43 template <class...>
44 using void_t = void;
45
46 /**
47 * The primary template class used in detecting operations. If the
48 * compiler does not choose the specialization, then the fall-back
49 * case is this general template, which then declares member variables
50 * and types according to the failed detection.
51 */
52 template <class Default,
53 class AlwaysVoid,
54 template <class...>
55 class Op,
56 class... Args>
57 struct detector
58 {
59 using value_t = std::false_type;
60 using type = Default;
61 };
62
63 /**
64 * A specialization of the general template.
65 *
66 * The trick this class uses is that, just like the general template,
67 * its second argument is always `void`, but here it is written as
68 * `void_t<Op<Args...>>` and consequently the compiler will only select this
69 * specialization if `Op<Args...>` is in fact a valid type. This means that
70 * the operation we seek to understand is indeed supported.
71 *
72 * This specialization then declares member variables and types according
73 * to the successful detection.
74 */
75 template <class Default, template <class...> class Op, class... Args>
76 struct detector<Default, void_t<Op<Args...>>, Op, Args...>
77 {
78 using value_t = std::true_type;
79 using type = Op<Args...>;
80 };
81
82
83 /**
84 * A base class for the `nonesuch` type to inherit from so it is not an
85 * aggregate.
86 */
87 struct nonesuch_base
88 {};
89
90 /**
91 * A type that can not be used in any reasonable way and consequently
92 * can be used to indicate a failed detection in template metaprogramming.
93 */
94 struct nonesuch : private nonesuch_base
95 {
96 ~nonesuch() = delete;
97 nonesuch(const nonesuch &) = delete;
98 void
99 operator=(const nonesuch &) = delete;
100 };
101
102 template <class Default, template <class...> class Op, class... Args>
103 using detected_or = detector<Default, void, Op, Args...>;
104
105 template <template <class...> class Op, class... Args>
106 using is_detected = typename detected_or<nonesuch, Op, Args...>::value_t;
107
108 template <template <class...> class Op, class... Args>
109 using detected_t = typename detected_or<nonesuch, Op, Args...>::type;
110
111 template <class Default, template <class...> class Op, class... Args>
112 using detected_or_t = typename detected_or<Default, Op, Args...>::type;
113
114 template <class Expected, template <class...> class Op, class... Args>
115 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
116
117 template <class To, template <class...> class Op, class... Args>
118 using is_detected_convertible =
119 std::is_convertible<detected_t<Op, Args...>, To>;
120 } // namespace SupportsOperation
121
122
123 /**
124 * A `constexpr` variable that describes whether or not `Op<Args...>` is a
125 * valid expression.
126 *
127 * The way this is used is to define an `Op` operation template that
128 * describes the operation we want to perform, and `Args` is a template
129 * pack that describes the arguments to the operation. This variable
130 * then states whether the operation, with these arguments, leads to
131 * a valid C++ expression.
132 *
133 * An example is if one wanted to find out whether a type `T` has
134 * a `get_mpi_communicator()` member function. In that case, one would write
135 * the operation as
136 * @code
137 * template <typename T>
138 * using get_mpi_communicator_op
139 * = decltype(std::declval<T>().get_mpi_communicator());
140 * @endcode
141 * and could define a variable like
142 * @code
143 * template <typename T>
144 * constexpr bool has_get_mpi_communicator =
145 * is_supported_operation<get_mpi_communicator_op, T>;
146 * @endcode
147 *
148 * The trick used here is that `get_mpi_communicator_op` is a general
149 * template, but when used with a type that does *not* have a
150 * `get_mpi_communicator()` member variable, the `decltype(...)` operation
151 * will fail because its argument does not represent a valid expression for
152 * such a type. In other words, for such types `T` that do not have such a
153 * member function, the general template `get_mpi_communicator_op` represents
154 * a valid declaration, but the instantiation `get_mpi_communicator_op<T>`
155 * is not, and the variable declared here detects and reports this.
156 */
157 template <template <class...> class Op, class... Args>
158 constexpr bool is_supported_operation =
159 SupportsOperation::is_detected<Op, Args...>::value;
160 } // namespace internal
161
162
163
164 namespace internal
165 {
166 namespace TemplateConstraints
167 {
168 /**
169 * A helper class whose `value` member is true or false depending on
170 * whether all of the given boolean template arguments are `true`.
171 * The class works by comparing the list of boolean values
172 * `true, Values...` with the list `Values..., true` (i.e., with
173 * its rotated self). The two are only the same if `Values...` is
174 * a list of only `true` values.
175 */
176 template <bool... Values>
177 struct all_true
178 {
179 static constexpr bool value = (Values && ...);
180 };
181
182
183 /**
184 * A class whose `value` member is set to `true` if any of the
185 * boolean template arguments are true.
186 */
187 template <bool... Values>
188 struct any_true
189 {
190 static constexpr bool value = (Values || ...);
191 };
192 } // namespace TemplateConstraints
193 } // namespace internal
194
195 /**
196 * This struct is a generalization of std::is_base_of<Base, Derived>
197 * to template parameter packs and tests if all of the Derived...
198 * classes have Base as base class or are Base itself. The result
199 * is stored in the member variable value.
200 */
201 template <class Base, class... Derived>
202 struct is_base_of_all
203 {
204 static constexpr bool value = internal::TemplateConstraints::all_true<
205 std::is_base_of_v<Base, Derived>...>::value;
206 };
207
208
209
210 /**
211 * This struct is a generalization of std::is_same to template
212 * parameter packs and tests if all of the types in the `Types...`
213 * parameter pack are equal to the `Type` given as first template
214 * argument. The result is stored in the member variable `value`.
215 */
216 template <typename Type, class... Types>
217 struct all_same_as
218 {
219 static constexpr bool value = internal::TemplateConstraints::all_true<
220 std::is_same_v<Type, Types>...>::value;
221 };
222
223
224
225 /**
226 * This struct is a generalization of std::is_same to template
227 * parameter packs and tests if any of the types in the `Types...`
228 * parameter pack are equal to the `Type` given as first template
229 * argument. The result is stored in the member variable `value`.
230 */
231 template <typename Type, class... Types>
232 struct is_same_as_any_of
233 {
234 static constexpr bool value = internal::TemplateConstraints::any_true<
235 std::is_same_v<Type, Types>...>::value;
236 };
237
238
239
240 /*
241 * A generalization of `std::enable_if` that only works if
242 * <i>all</i> of the given boolean template parameters are
243 * true. See [here](https://en.cppreference.com/w/cpp/types/enable_if)
244 * for what `std::enable_if` does.
245 *
246 * @note
247 * In contrast to `std::enable_if`, this template has no additional
248 * template type (which for `std::enable_if` is defaulted to `void`).
249 * As a consequence, this structure cannot be used for anything other
250 * than enabling or disabling a template declaration; in particular
251 * it cannot be used to set the return type of a function as one might
252 * do with something like
253 * @code
254 * template <typename T>
255 * typename std::enable_if<std::is_floating_point_v<T>, T>::type
256 * abs (const T t);
257 * @endcode
258 * which declares a function template `abs()` that can only be
259 * instantiated if `T` is a floating point type; this function then
260 * returns an object of type `T` as indicated by the last argument to
261 * `std::enable_if`. The reason `enable_if_all` does not allow providing
262 * this additional type is that variadic templates (here, the list of
263 * `bool` arguments) must be the last template argument.
264 */
265 template <bool... Values>
266 struct enable_if_all
267 : std::enable_if<internal::TemplateConstraints::all_true<Values...>::value>
268 {};
269
270
271
272 /*
273 * A generalization of `std::enable_if_t` that only works if
274 * <i>all</i> of the given boolean template parameters are
275 * true. See [here](https://en.cppreference.com/w/cpp/types/enable_if)
276 * for what `std::enable_if_t` does.
277 *
278 * @note
279 * In contrast to `std::enable_if_t`, this template has no additional
280 * template type (which for `std::enable_if` is defaulted to `void`).
281 * As a consequence, this structure cannot be used for anything other
282 * than enabling or disabling a template declaration; in particular
283 * it cannot be used to set the return type of a function as one might
284 * do with something like
285 * @code
286 * template <typename T>
287 * std::enable_if_t<std::is_floating_point_v<T>, T>
288 * abs (const T t);
289 * @endcode
290 * which declares a function template `abs()` that can only be
291 * instantiated if `T` is a floating point type; this function then
292 * returns an object of type `T` as indicated by the last argument to
293 * `std::enable_if`. The reason `enable_if_all` does not allow providing
294 * this additional type is that variadic templates (here, the list of
295 * `bool` arguments) must be the last template argument.
296 */
297 template <bool... Values>
298 using enable_if_all_t = typename enable_if_all<Values...>::type;
299
300
301 /**
302 * A type trait that checks to see if a class behaves as an iterable container
303 * that has a beginning and an end. This implies that the class either defines
304 * the `begin()` and `end()` functions, or is a C-style array.
305 */
306 template <typename T>
307 using begin_and_end_t =
308 decltype(std::begin(std::declval<T>()), std::end(std::declval<T>()));
309
310 template <typename T>
311 constexpr bool has_begin_and_end =
312 internal::is_supported_operation<begin_and_end_t, T>;
313
314
315 /**
316 * A `using` declaration to make the
317 * [std::identity_type](https://en.cppreference.com/w/cpp/types/type_identity)
318 * class available under the name that deal.II has used for a long time.
319 *
320 * @deprecated Use `std_cxx20::type_identity` instead.
321 */
322 template <typename T>
323 using identity DEAL_II_DEPRECATED = std_cxx20::type_identity<T>;
324
325
326 /**
327 * A class that always returns a given value.
328 * This is needed as a workaround for lambdas used as default parameters
329 * some compilers struggle to deal with.
330 */
331 template <typename ArgType, typename ValueType>
332 struct always_return
333 {
334 ValueType value;
335 ValueType
336 operator()(const ArgType &)
337 {
338 return value;
339 }
340 };
341
342
343
344 /**
345 * A class to perform comparisons of arbitrary pointers for equality. In some
346 * circumstances, one would like to make sure that two arguments to a function
347 * are not the same object. One would, in this case, make sure that their
348 * addresses are not the same. However, sometimes the types of these two
349 * arguments may be template types, and they may be the same type or not. In
350 * this case, a simple comparison as in <tt>&object1 != &object2</tt> does
351 * only work if the types of the two objects are equal, but the compiler will
352 * barf if they are not. However, in the latter case, since the types of the
353 * two objects are different, we can be sure that the two objects cannot be
354 * the same.
355 *
356 * This class implements a comparison function that always returns @p false if
357 * the types of its two arguments are different, and returns <tt>p1 == p2</tt>
358 * otherwise.
359 */
360 struct PointerComparison
361 {
362 /**
363 * Comparison function for pointers of the same type. Returns @p true if the
364 * two pointers are equal.
365 */
366 template <typename T>
367 static bool
368 equal(const T *p1, const T *p2)
369 {
370 return (p1 == p2);
371 }
372
373
374 /**
375 * Comparison function for pointers of different types. The C++ language
376 * does not allow comparing these pointers using <tt>operator==</tt>.
377 * However, since the two pointers have different types, we know that they
378 * can't be the same, so we always return @p false.
379 */
380 template <typename T, typename U>
381 static bool
382 equal(const T *, const U *)
383 {
384 return false;
385 }
386 };
387
388
389
390 namespace internal
391 {
392 /**
393 * A struct that implements the default product type resulting from the
394 * multiplication of two types.
395 *
396 * @note Care should be taken when @p T or @p U have qualifiers (@p const or
397 * @p volatile) or are @p lvalue or @p rvalue references! It is recommended
398 * that specialization of this class is only made for unqualified (fully
399 * stripped) types and that the ProductType class be used to determine the
400 * result of operating with (potentially) qualified types.
401 */
402 template <typename T, typename U>
403 struct ProductTypeImpl
404 {
405 using type = decltype(std::declval<T>() * std::declval<U>());
406 };
407
408 } // namespace internal
409
410
411
412 /**
413 * A class with a local alias that represents the type that results from the
414 * product of two variables of type @p T and @p U. In other words, we would
415 * like to infer the type of the <code>product</code> variable in code like
416 * this:
417 * @code
418 * T t;
419 * U u;
420 * auto product = t*u;
421 * @endcode
422 * The local alias of this structure represents the type the variable
423 * <code>product</code> would have.
424 *
425 *
426 * <h3>Where is this useful</h3>
427 *
428 * The purpose of this class is principally to represent the type one needs to
429 * use to represent the values or gradients of finite element fields at
430 * quadrature points. For example, assume you are storing the values $U_j$ of
431 * unknowns in a Vector<float>, then evaluating $u_h(x_q) = \sum_j U_j
432 * \varphi_j(x_q)$ at quadrature points results in values $u_h(x_q)$ that need
433 * to be stored as @p double variables because the $U_j$ are @p float values
434 * and the $\varphi_j(x_q)$ are computed as @p double values, and the product
435 * are then @p double values. On the other hand, if you store your unknowns
436 * $U_j$ as <code>std::complex@<double@></code> values and you try to evaluate
437 * $\nabla u_h(x_q) = \sum_j U_j \nabla\varphi_j(x_q)$ at quadrature points,
438 * then the gradients $\nabla u_h(x_q)$ need to be stored as objects of type
439 * <code>Tensor@<1,dim,std::complex@<double@>@></code> because that's what you
440 * get when you multiply a complex number by a <code>Tensor@<1,dim@></code>
441 * (the type used to represent the gradient of shape functions of scalar
442 * finite elements).
443 *
444 * Likewise, if you are using a vector valued element (with dim components)
445 * and the $U_j$ are stored as @p double variables, then $u_h(x_q) = \sum_j
446 * U_j \varphi_j(x_q)$ needs to have type <code>Tensor@<1,dim@></code>
447 * (because the shape functions have type <code>Tensor@<1,dim@></code>).
448 * Finally, if you store the $U_j$ as objects of type
449 * <code>std::complex@<double@></code> and you have a vector valued element,
450 * then the gradients $\nabla u_h(x_q) = \sum_j U_j \nabla\varphi_j(x_q)$ will
451 * result in objects of type <code>Tensor@<2,dim,std::complex@<double@>
452 * @></code>.
453 *
454 * In all of these cases, this type is used to identify which type needs to be
455 * used for the result of computing the product of unknowns and the values,
456 * gradients, or other properties of shape functions.
457 */
458 template <typename T, typename U>
459 struct ProductType
460 {
461 using type =
462 typename internal::ProductTypeImpl<std::decay_t<T>, std::decay_t<U>>::type;
463 };
464
465 namespace internal
466 {
467 // Annoyingly, there is no std::complex<T>::operator*(U) for scalars U
468 // other than T (not even in C++11, or C++14). We provide our own overloads
469 // in base/complex_overloads.h, but in order for them to work, we have to
470 // manually specify all products we want to allow:
471
472 template <typename T>
473 struct ProductTypeImpl<std::complex<T>, std::complex<T>>
474 {
475 using type = std::complex<T>;
476 };
477
478 template <typename T, typename U>
479 struct ProductTypeImpl<std::complex<T>, std::complex<U>>
480 {
481 using type = std::complex<typename ProductType<T, U>::type>;
482 };
483
484 template <typename U>
485 struct ProductTypeImpl<double, std::complex<U>>
486 {
487 using type = std::complex<typename ProductType<double, U>::type>;
488 };
489
490 template <typename T>
491 struct ProductTypeImpl<std::complex<T>, double>
492 {
493 using type = std::complex<typename ProductType<T, double>::type>;
494 };
495
496 template <typename U>
497 struct ProductTypeImpl<float, std::complex<U>>
498 {
499 using type = std::complex<typename ProductType<float, U>::type>;
500 };
501
502 template <typename T>
503 struct ProductTypeImpl<std::complex<T>, float>
504 {
505 using type = std::complex<typename ProductType<T, float>::type>;
506 };
507
508 } // namespace internal
509
510
511
512 /**
513 * This class provides a local alias @p type that is equal to the template
514 * argument but only if the template argument corresponds to a scalar type
515 * (i.e., one of the floating point types, signed or unsigned integer, or a
516 * complex number). If the template type @p T is not a scalar, then no class
517 * <code>EnableIfScalar@<T@></code> is declared and, consequently, no local
518 * alias is available.
519 *
520 * The purpose of the class is to disable certain template functions if one of
521 * the arguments is not a scalar number. By way of (nonsensical) example,
522 * consider the following function:
523 * @code
524 * template <typename T>
525 * T multiply (const T t1, const T t2)
526 * {
527 * return t1*t2;
528 * }
529 * @endcode
530 * This function can be called with any two arguments of the same type @p T.
531 * This includes arguments for which this clearly makes no sense.
532 * Consequently, one may want to restrict the function to only scalars, and
533 * this can be written as
534 * @code
535 * template <typename T>
536 * typename EnableIfScalar<T>::type
537 * multiply (const T t1, const T t2)
538 * {
539 * return t1*t2;
540 * }
541 * @endcode
542 * At a place where you call the function, the compiler will deduce the type
543 * @p T from the arguments. For example, in
544 * @code
545 * multiply(1.234, 2.345);
546 * @endcode
547 * it will deduce @p T to be @p double, and because
548 * <code>EnableIfScalar@<double@>::%type</code> equals @p double, the compiler
549 * will instantiate a function <code>double multiply(const double, const
550 * double)</code> from the template above. On the other hand, in a context
551 * like
552 * @code
553 * std::vector<char> v1, v2;
554 * multiply(v1, v2);
555 * @endcode
556 * the compiler will deduce @p T to be <code>std::vector@<char@></code> but
557 * because <code>EnableIfScalar@<std::vector@<char@>@>::%type</code> does not
558 * exist the compiler does not consider the template for instantiation. This
559 * technique is called "Substitution Failure is not an Error (SFINAE)". It
560 * makes sure that the template function can not even be called, rather than
561 * leading to a later error about the fact that the operation
562 * <code>t1*t2</code> is not defined (or may lead to some nonsensical result).
563 * It also allows the declaration of overloads of a function such as @p
564 * multiply for different types of arguments, without resulting in ambiguous
565 * call errors by the compiler.
566 */
567 template <typename T>
568 struct EnableIfScalar;
569
570
571 template <>
572 struct EnableIfScalar<double>
573 {
574 using type = double;
575 };
576
577 template <>
578 struct EnableIfScalar<float>
579 {
580 using type = float;
581 };
582
583 template <>
584 struct EnableIfScalar<long double>
585 {
586 using type = long double;
587 };
588
589 template <>
590 struct EnableIfScalar<int>
591 {
592 using type = int;
593 };
594
595 template <>
596 struct EnableIfScalar<unsigned int>
597 {
598 using type = unsigned int;
599 };
600
601 template <typename T>
602 struct EnableIfScalar<std::complex<T>>
603 {
604 using type = std::complex<T>;
605 };
606
607
608 // Forward declarations of vector types
609 template <typename Number>
610 class Vector;
611
612 template <typename Number>
613 class BlockVector;
614
615 namespace LinearAlgebra
616 {
617 template <typename Number>
618 class Vector;
619
620 template <typename Number>
621 class BlockVector;
622
623 namespace distributed
624 {
625 template <typename Number, typename MemorySpace>
626 class Vector;
627
628 template <typename Number, typename MemorySpace>
629 class BlockVector;
630 } // namespace distributed
631 } // namespace LinearAlgebra
632
633 #ifdef DEAL_II_WITH_PETSC
634 namespace PETScWrappers
635 {
636 class VectorBase;
637 class Vector;
638 class BlockVector;
639
640 namespace MPI
641 {
642 class Vector;
643 class BlockVector;
644
645 class SparseMatrix;
646 class BlockSparseMatrix;
647 } // namespace MPI
648 } // namespace PETScWrappers
649 #endif
650
651 #ifdef DEAL_II_WITH_TRILINOS
652 namespace TrilinosWrappers
653 {
654 namespace MPI
655 {
656 class Vector;
657 class BlockVector;
658 } // namespace MPI
659 } // namespace TrilinosWrappers
660
661 namespace LinearAlgebra
662 {
663 namespace EpetraWrappers
664 {
665 class Vector;
666 }
667
668 # ifdef DEAL_II_TRILINOS_WITH_TPETRA
669 namespace TpetraWrappers
670 {
671 template <typename Number, typename MemorySpace>
672 class Vector;
673
674 template <typename Number, typename MemorySpace>
675 class BlockVector;
676
677 template <typename Number, typename MemorySpace>
678 class SparseMatrix;
679
680 template <typename Number, typename MemorySpace>
681 class BlockSparseMatrix;
682 } // namespace TpetraWrappers
683 # endif
684 } // namespace LinearAlgebra
685 #endif
686
687
688
689 /**
690 * A namespace that is used to declare concepts used in C++20-style
691 * `requires` clauses.
692 */
693 namespace concepts
694 {
695 #if defined(DEAL_II_HAVE_CXX20) || defined(DOXYGEN)
696 /**
697 * A concept that identifies whether a template argument `C`
698 * represents a [contiguous
699 * container](https://en.cppreference.com/w/cpp/named_req/ContiguousContainer).
700 * A contiguous container is a container object (such as `std::vector`,
701 * `std::array`, or `boost::container::small_vector` that stores its elements
702 * in one contiguous array in which we access all elements via a pointer to
703 * the first element plus an offset. In contrast, linked lists, maps, and
704 * similar objects are typically not stored as contiguous containers.
705 */
706 template <typename C>
707 concept is_contiguous_container = requires(C &c) {
708 {
709 std::data(c)
710 };
711 {
712 std::size(c)
713 };
714 };
715
716
717 /**
718 * A concept that tests that a combination of `dim` and `spacedim`
719 * template arguments is valid. Specifically, we must have that
720 * - `dim>=1`
721 * - `spacedim<=3`
722 * - `dim<=spacedim`.
723 * These are the kinds of requirements that are imposed, for
724 * example, on class Triangulation.
725 */
726 template <int dim, int spacedim>
727 concept is_valid_dim_spacedim =
728 (dim >= 1 && spacedim <= 3 && dim <= spacedim);
729
730 namespace internal
731 {
732 /**
733 * A template variable that returns whether the template argument is
734 * a valid deal.II vector type. Its general definition is `false`, with
735 * specializations dealing with actual vector types for which the
736 * predicate is `true`.
737 */
738 template <typename T>
739 inline constexpr bool is_dealii_vector_type = false;
740
741 template <typename Number>
742 inline constexpr bool is_dealii_vector_type<dealii::Vector<Number>> = true;
743
744 template <typename Number>
745 inline constexpr bool is_dealii_vector_type<dealii::BlockVector<Number>> =
746 true;
747
748 template <typename Number>
749 inline constexpr bool
750 is_dealii_vector_type<dealii::LinearAlgebra::BlockVector<Number>> = true;
751
752 template <typename Number, typename MemorySpace>
753 inline constexpr bool is_dealii_vector_type<
754 dealii::LinearAlgebra::distributed::Vector<Number, MemorySpace>> = true;
755
756 template <typename Number, typename MemorySpace>
757 inline constexpr bool is_dealii_vector_type<
758 dealii::LinearAlgebra::distributed::BlockVector<Number, MemorySpace>> =
759 true;
760
761 # ifdef DEAL_II_WITH_PETSC
762 template <>
763 inline constexpr bool is_dealii_vector_type<dealii::PETScWrappers::Vector> =
764 true;
765
766 template <>
767 inline constexpr bool
768 is_dealii_vector_type<dealii::PETScWrappers::BlockVector> = true;
769
770 template <>
771 inline constexpr bool
772 is_dealii_vector_type<dealii::PETScWrappers::MPI::Vector> = true;
773
774 template <>
775 inline constexpr bool
776 is_dealii_vector_type<dealii::PETScWrappers::MPI::BlockVector> = true;
777 # endif
778
779 # ifdef DEAL_II_WITH_TRILINOS
780 template <>
781 inline constexpr bool
782 is_dealii_vector_type<dealii::TrilinosWrappers::MPI::Vector> = true;
783
784 template <>
785 inline constexpr bool
786 is_dealii_vector_type<dealii::TrilinosWrappers::MPI::BlockVector> = true;
787
788 template <>
789 inline constexpr bool
790 is_dealii_vector_type<dealii::LinearAlgebra::EpetraWrappers::Vector> =
791 true;
792
793 # ifdef DEAL_II_TRILINOS_WITH_TPETRA
794 template <typename Number, typename MemorySpace>
795 inline constexpr bool is_dealii_vector_type<
796 dealii::LinearAlgebra::TpetraWrappers::Vector<Number, MemorySpace>> =
797 true;
798
799 template <typename Number, typename MemorySpace>
800 inline constexpr bool is_dealii_vector_type<
801 dealii::LinearAlgebra::TpetraWrappers::BlockVector<Number, MemorySpace>> =
802 true;
803 # endif
804 # endif
805
806
807 /**
808 * A template variable that returns whether the template argument is
809 * a valid deal.II vector type that is internally built on PETSc
810 * functionality. Its general definition is `false`, with
811 * specializations dealing with actual vector types for which the
812 * predicate is `true`.
813 */
814 template <typename T>
815 inline constexpr bool is_dealii_petsc_vector_type = false;
816
817 # ifdef DEAL_II_WITH_PETSC
818 template <>
819 inline constexpr bool
820 is_dealii_petsc_vector_type<dealii::PETScWrappers::VectorBase> = true;
821
822 template <>
823 inline constexpr bool
824 is_dealii_petsc_vector_type<dealii::PETScWrappers::Vector> = true;
825
826 template <>
827 inline constexpr bool
828 is_dealii_petsc_vector_type<dealii::PETScWrappers::BlockVector> = true;
829
830 template <>
831 inline constexpr bool
832 is_dealii_petsc_vector_type<dealii::PETScWrappers::MPI::Vector> = true;
833
834 template <>
835 inline constexpr bool
836 is_dealii_petsc_vector_type<dealii::PETScWrappers::MPI::BlockVector> =
837 true;
838 # endif
839
840
841 /**
842 * A template variable that returns whether the template argument is
843 * a valid deal.II matrix type that is internally built on PETSc
844 * functionality. Its general definition is `false`, with
845 * specializations dealing with actual matrix types for which the
846 * predicate is `true`.
847 */
848 template <typename T>
849 inline constexpr bool is_dealii_petsc_matrix_type = false;
850
851 # ifdef DEAL_II_WITH_PETSC
852 template <>
853 inline constexpr bool
854 is_dealii_petsc_matrix_type<dealii::PETScWrappers::MPI::SparseMatrix> =
855 true;
856
857 template <>
858 inline constexpr bool is_dealii_petsc_matrix_type<
859 dealii::PETScWrappers::MPI::BlockSparseMatrix> = true;
860 # endif
861 } // namespace internal
862
863
864 /**
865 * A concept that tests whether a given template argument is a deal.II
866 * vector type. This concept is used in many places, such as for the
867 * functions in namespace VectorTools, where functions take a vector
868 * as argument, but the type of the vector is a template argument. The
869 * concept ensures that these functions cannot be called with an `int`
870 * argument, for example, for which the declaration without the concept
871 * would be perfectly valid but for which one would later get a linker
872 * error because the function is only instantiated for deal.II vector
873 * types.
874 */
875 template <typename VectorType>
876 concept is_dealii_vector_type =
877 internal::is_dealii_vector_type<std::remove_cv_t<VectorType>>;
878
879 /**
880 * A concept that tests whether a given template argument is a deal.II
881 * vector type into which one can write. It is defined by asking
882 * whether the is_dealii_vector_type concept is satisfied, and
883 * that the template argument is not a `const`-qualified type. For
884 * example, `is_writable_dealii_vector_type<dealii::Vector>` is true,
885 * whereas `is_writable_dealii_vector_type<const dealii::Vector>` is
886 * not.
887 */
888 template <typename VectorType>
889 concept is_writable_dealii_vector_type =
890 is_dealii_vector_type<VectorType> && (std::is_const_v<VectorType> == false);
891
892 /**
893 * A concept that tests whether a given template argument is a deal.II
894 * vector type that internally builds on PETSc functionality. This
895 * concept is used to constrain some classes that implement advanced
896 * functionality based on PETSc and that requires that the vector
897 * it works on are PETSc vectors. This includes, for example, the
898 * time stepping and nonlinear solver classes in namespace PETScWrappers.
899 */
900 template <typename VectorType>
901 concept is_dealii_petsc_vector_type =
902 internal::is_dealii_petsc_vector_type<VectorType>;
903
904 /**
905 * A concept that tests whether a given template argument is a deal.II
906 * matrix type that internally builds on PETSc functionality. This
907 * concept is used to constrain some classes that implement advanced
908 * functionality based on PETSc and that requires that the matrix
909 * it works on are PETSc matrices. This includes, for example, the
910 * time stepping and nonlinear solver classes in namespace PETScWrappers.
911 */
912 template <typename VectorType>
913 concept is_dealii_petsc_matrix_type =
914 internal::is_dealii_petsc_matrix_type<VectorType>;
915 #endif
916 } // namespace concepts
917
918
919 template <int dim, int spacedim>
920 DEAL_II_CXX20_REQUIRES((concepts::is_valid_dim_spacedim<dim, spacedim>))
921 class Triangulation;
922
923 template <int dim, int spacedim>
924 DEAL_II_CXX20_REQUIRES((concepts::is_valid_dim_spacedim<dim, spacedim>))
925 class DoFHandler;
926
927 namespace parallel
928 {
929 namespace distributed
930 {
931 template <int dim, int spacedim>
932 DEAL_II_CXX20_REQUIRES((concepts::is_valid_dim_spacedim<dim, spacedim>))
933 class Triangulation;
934 }
935 namespace shared
936 {
937 template <int dim, int spacedim>
938 DEAL_II_CXX20_REQUIRES((concepts::is_valid_dim_spacedim<dim, spacedim>))
939 class Triangulation;
940 }
941 namespace fullydistributed
942 {
943 template <int dim, int spacedim>
944 DEAL_II_CXX20_REQUIRES((concepts::is_valid_dim_spacedim<dim, spacedim>))
945 class Triangulation;
946 }
947 } // namespace parallel
948
949 namespace concepts
950 {
951 #if defined(DEAL_II_HAVE_CXX20) || defined(DOXYGEN)
952 namespace internal
953 {
954 template <typename T>
955 inline constexpr bool is_triangulation_or_dof_handler = false;
956
957 template <int dim, int spacedim>
958 inline constexpr bool
959 is_triangulation_or_dof_handler<Triangulation<dim, spacedim>> = true;
960
961 template <int dim, int spacedim>
962 inline constexpr bool is_triangulation_or_dof_handler<
963 parallel::distributed::Triangulation<dim, spacedim>> = true;
964
965 template <int dim, int spacedim>
966 inline constexpr bool is_triangulation_or_dof_handler<
967 parallel::shared::Triangulation<dim, spacedim>> = true;
968
969 template <int dim, int spacedim>
970 inline constexpr bool is_triangulation_or_dof_handler<
971 parallel::fullydistributed::Triangulation<dim, spacedim>> = true;
972
973 template <int dim, int spacedim>
974 inline constexpr bool
975 is_triangulation_or_dof_handler<DoFHandler<dim, spacedim>> = true;
976 } // namespace internal
977
978
979 /**
980 * A concept that is used to check whether the `MeshType` template
981 * type used in many functions in namespace GridTools and
982 * VectorTools is in fact a "mesh" in the sense expected by these
983 * functions. Specifically, this means that the type is either a
984 * Triangulation or a DoFHandler type.
985 */
986 template <typename MeshType>
987 concept is_triangulation_or_dof_handler =
988 internal::is_triangulation_or_dof_handler<MeshType>;
989
990 /**
991 * A concept that tests whether a class `VectorType` has the required
992 * interface to serve as a vector in vector-space operations -- principally
993 * what is required to run iterative solvers: things such as norms, dot
994 * products, etc.
995 */
996 template <typename VectorType>
997 concept is_vector_space_vector = requires(VectorType U,
998 VectorType V,
999 VectorType W,
1000 typename VectorType::value_type a,
1001 typename VectorType::value_type b,
1002 typename VectorType::value_type s) {
1003 // Check local type requirements:
1004 typename VectorType::value_type;
1005 typename VectorType::size_type;
1006 typename VectorType::real_type;
1007
1008 // Check some assignment and reinit operations;
1009 U.reinit(V);
1010 U.reinit(V, /* omit_zeroing_entries= */ true);
1011
1012 U = V;
1013 U = a; // assignment of scalar
1014
1015 U.equ(a, V);
1016
1017 // Check scaling operations
1018 U *= a;
1019 U /= a;
1020
1021 U.scale(V);
1022
1023 // Vector additions:
1024 U += V;
1025 U -= V;
1026
1027 U.add(a);
1028 U.add(a, V);
1029 U.add(a, V, b, W);
1030
1031 U.sadd(s, a, V);
1032
1033 // Norms and similar stuff:
1034 {
1035 U.mean_value()
1036 } -> std::convertible_to<typename VectorType::value_type>;
1037
1038 {
1039 U.l1_norm()
1040 } -> std::convertible_to<typename VectorType::real_type>;
1041
1042 {
1043 U.l2_norm()
1044 } -> std::convertible_to<typename VectorType::real_type>;
1045
1046 {
1047 U.linfty_norm()
1048 } -> std::convertible_to<typename VectorType::real_type>;
1049
1050 // Dot products:
1051 {
1052 U *V
1053 } -> std::convertible_to<typename VectorType::value_type>;
1054
1055 {
1056 U.add_and_dot(a, V, W)
1057 } -> std::convertible_to<typename VectorType::value_type>;
1058
1059 // Some queries:
1060 {
1061 U.size()
1062 } -> std::convertible_to<typename VectorType::size_type>;
1063
1064 {
1065 U.all_zero()
1066 } -> std::same_as<bool>;
1067
1068 {
1069 U.get_mpi_communicator()
1070 } -> std::same_as<MPI_Comm>;
1071 };
1072
1073
1074 /**
1075 * A concept that tests whether objects of type `MatrixType` can act
1076 * as linear operators on `VectorType`. In practice, that means that
1077 * `MatrixType` must have a `vmult()` member function that can take
1078 * a `VectorType` object as input and produce another `VectorType`
1079 * as output (both objects being taken as arguments to the `vmult()`
1080 * function).
1081 */
1082 template <typename MatrixType, typename VectorType>
1083 concept is_linear_operator_on =
1084 requires(const MatrixType &A, VectorType &dst, const VectorType &src) {
1085 A.vmult(dst, src);
1086 };
1087
1088
1089 /**
1090 * A concept that tests whether objects of type `MatrixType` can act
1091 * as the transposes of linear operators on `VectorType`. In practice, that
1092 * means that `MatrixType` must have a `Tvmult()` member function that can
1093 * take a `VectorType` object as input and produce another `VectorType`
1094 * as output (both objects being taken as arguments to the `vmult()`
1095 * function).
1096 */
1097 template <typename MatrixType, typename VectorType>
1098 concept is_transpose_linear_operator_on =
1099 requires(const MatrixType &A, VectorType &dst, const VectorType &src) {
1100 A.Tvmult(dst, src);
1101 };
1102
1103 #endif
1104
1105 } // namespace concepts
1106
1107
1108
1109 DEAL_II_NAMESPACE_CLOSE
1110
1111 #endif