| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
#ifndef DUNE_ISTL_MULTITYPEBLOCKVECTOR_HH |
| 6 |
#define DUNE_ISTL_MULTITYPEBLOCKVECTOR_HH |
| 7 |
|
| 8 |
#include <cmath> |
| 9 |
#include <iostream> |
| 10 |
#include <tuple> |
| 11 |
|
| 12 |
#include <dune/common/dotproduct.hh> |
| 13 |
#include <dune/common/ftraits.hh> |
| 14 |
#include <dune/common/hybridutilities.hh> |
| 15 |
#include <dune/common/typetraits.hh> |
| 16 |
#include <dune/common/std/type_traits.hh> |
| 17 |
|
| 18 |
#include "istlexception.hh" |
| 19 |
|
| 20 |
// forward declaration |
| 21 |
namespace Dune { |
| 22 |
template < typename... Args > |
| 23 |
class MultiTypeBlockVector; |
| 24 |
} |
| 25 |
|
| 26 |
#include "gsetc.hh" |
| 27 |
|
| 28 |
namespace Dune { |
| 29 |
|
| 30 |
/** |
| 31 |
@addtogroup ISTL_SPMV |
| 32 |
@{ |
| 33 |
*/ |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
/** @addtogroup DenseMatVec |
| 39 |
@{ |
| 40 |
*/ |
| 41 |
template <typename... Args> |
| 42 |
struct FieldTraits< MultiTypeBlockVector<Args...> > |
| 43 |
{ |
| 44 |
using field_type = typename MultiTypeBlockVector<Args...>::field_type; |
| 45 |
using real_type = typename MultiTypeBlockVector<Args...>::real_type; |
| 46 |
}; |
| 47 |
/** |
| 48 |
@} |
| 49 |
*/ |
| 50 |
|
| 51 |
/** |
| 52 |
@brief A Vector class to support different block types |
| 53 |
|
| 54 |
This vector class combines elements of different types known at compile-time. |
| 55 |
*/ |
| 56 |
template < typename... Args > |
| 57 |
class MultiTypeBlockVector |
| 58 |
: public std::tuple<Args...> |
| 59 |
{ |
| 60 |
/** \brief Helper type */ |
| 61 |
typedef std::tuple<Args...> TupleType; |
| 62 |
public: |
| 63 |
|
| 64 |
/** \brief Type used for vector sizes */ |
| 65 |
using size_type = std::size_t; |
| 66 |
|
| 67 |
/** |
| 68 |
* \brief Get the constructors from tuple |
| 69 |
*/ |
| 70 |
using TupleType::TupleType; |
| 71 |
|
| 72 |
/** |
| 73 |
* own class' type |
| 74 |
*/ |
| 75 |
typedef MultiTypeBlockVector<Args...> type; |
| 76 |
|
| 77 |
/** \brief The type used for scalars |
| 78 |
* |
| 79 |
* Use the `std::common_type_t` of the `Args`' field_type and use `nonesuch` if no implementation of |
| 80 |
* `std::common_type` is provided for the given `field_type` arguments. |
| 81 |
*/ |
| 82 |
using field_type = Std::detected_t<std::common_type_t, typename FieldTraits< std::decay_t<Args> >::field_type...>; |
| 83 |
|
| 84 |
/** \brief The type used for real values |
| 85 |
* |
| 86 |
* Use the `std::common_type_t` of the `Args`' real_type and use `nonesuch` if no implementation of |
| 87 |
* `std::common_type` is provided for the given `real_type` arguments. |
| 88 |
*/ |
| 89 |
using real_type = Std::detected_t<std::common_type_t, typename FieldTraits< std::decay_t<Args> >::real_type...>; |
| 90 |
|
| 91 |
// make sure that we have an std::common_type: using an assertion produces a more readable error message |
| 92 |
// than a compiler template instantiation error |
| 93 |
static_assert ( sizeof...(Args) == 0 or |
| 94 |
not (std::is_same_v<field_type, Std::nonesuch> or std::is_same_v<real_type, Std::nonesuch>), |
| 95 |
"No std::common_type implemented for the given field_type/real_type of the Args. Please provide an implementation and check that a FieldTraits class is present for your type."); |
| 96 |
|
| 97 |
|
| 98 |
/** \brief Return the number of non-zero vector entries |
| 99 |
* |
| 100 |
* As this is a dense vector data structure, all entries are non-zero, |
| 101 |
* and hence 'size' returns the same number as 'N'. |
| 102 |
*/ |
| 103 |
static constexpr size_type size() |
| 104 |
{ |
| 105 |
return sizeof...(Args); |
| 106 |
} |
| 107 |
|
| 108 |
/** \brief Number of elements |
| 109 |
*/ |
| 110 |
static constexpr size_type N() |
| 111 |
{ |
| 112 |
return sizeof...(Args); |
| 113 |
} |
| 114 |
|
| 115 |
/** \brief Number of scalar elements */ |
| 116 |
size_type dim() const |
| 117 |
{ |
| 118 |
size_type result = 0; |
| 119 |
Hybrid::forEach(std::make_index_sequence<N()>{}, |
| 120 |
[&](auto i){result += std::get<i>(*this).dim();}); |
| 121 |
|
| 122 |
return result; |
| 123 |
} |
| 124 |
|
| 125 |
/** \brief Random-access operator |
| 126 |
* |
| 127 |
* This method mimics the behavior of normal vector access with square brackets like, e.g., v[5] = 1. |
| 128 |
* The problem is that the return type is different for each value of the argument in the brackets. |
| 129 |
* Therefore we implement a trick using std::integral_constant. To access the first entry of |
| 130 |
* a MultiTypeBlockVector named v write |
| 131 |
* \code |
| 132 |
* MultiTypeBlockVector<A,B,C,D> v; |
| 133 |
* std::integral_constant<std::size_t,0> _0; |
| 134 |
* v[_0] = ... |
| 135 |
* \endcode |
| 136 |
* The name '_0' used here as a static replacement of the integer number zero is arbitrary. |
| 137 |
* Any other variable name can be used. If you don't like the separate variable, you can writee |
| 138 |
* \code |
| 139 |
* MultiTypeBlockVector<A,B,C,D> v; |
| 140 |
* v[std::integral_constant<std::size_t,0>()] = ... |
| 141 |
* \endcode |
| 142 |
*/ |
| 143 |
template< size_type index > |
| 144 |
typename std::tuple_element<index,TupleType>::type& |
| 145 |
operator[] ([[maybe_unused]] const std::integral_constant< size_type, index > indexVariable) |
| 146 |
{ |
| 147 |
return std::get<index>(*this); |
| 148 |
} |
| 149 |
|
| 150 |
/** \brief Const random-access operator |
| 151 |
* |
| 152 |
* This is the const version of the random-access operator. See the non-const version for a full |
| 153 |
* explanation of how to use it. |
| 154 |
*/ |
| 155 |
template< size_type index > |
| 156 |
const typename std::tuple_element<index,TupleType>::type& |
| 157 |
operator[] ([[maybe_unused]] const std::integral_constant< size_type, index > indexVariable) const |
| 158 |
{ |
| 159 |
return std::get<index>(*this); |
| 160 |
} |
| 161 |
|
| 162 |
/** \brief Assignment operator |
| 163 |
*/ |
| 164 |
template<typename T> |
| 165 |
void operator= (const T& newval) { |
| 166 |
Dune::Hybrid::forEach(*this, [&](auto&& entry) { |
| 167 |
entry = newval; |
| 168 |
}); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* operator for MultiTypeBlockVector += MultiTypeBlockVector operations |
| 173 |
*/ |
| 174 |
void operator+= (const type& newv) { |
| 175 |
using namespace Dune::Hybrid; |
| 176 |
forEach(integralRange(Hybrid::size(*this)), [&](auto&& i) { |
| 177 |
(*this)[i] += newv[i]; |
| 178 |
}); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* operator for MultiTypeBlockVector -= MultiTypeBlockVector operations |
| 183 |
*/ |
| 184 |
void operator-= (const type& newv) { |
| 185 |
using namespace Dune::Hybrid; |
| 186 |
forEach(integralRange(Hybrid::size(*this)), [&](auto&& i) { |
| 187 |
(*this)[i] -= newv[i]; |
| 188 |
}); |
| 189 |
} |
| 190 |
|
| 191 |
/** \brief Multiplication with a scalar */ |
| 192 |
template<class T, |
| 193 |
std::enable_if_t< IsNumber<T>::value, int> = 0> |
| 194 |
void operator*= (const T& w) { |
| 195 |
Hybrid::forEach(*this, [&](auto&& entry) { |
| 196 |
entry *= w; |
| 197 |
}); |
| 198 |
} |
| 199 |
|
| 200 |
/** \brief Division by a scalar */ |
| 201 |
template<class T, |
| 202 |
std::enable_if_t< IsNumber<T>::value, int> = 0> |
| 203 |
void operator/= (const T& w) { |
| 204 |
Hybrid::forEach(*this, [&](auto&& entry) { |
| 205 |
entry /= w; |
| 206 |
}); |
| 207 |
} |
| 208 |
|
| 209 |
field_type operator* (const type& newv) const { |
| 210 |
using namespace Dune::Hybrid; |
| 211 |
return accumulate(integralRange(Hybrid::size(*this)), field_type(0), [&](auto&& a, auto&& i) { |
| 212 |
return a + (*this)[i]*newv[i]; |
| 213 |
}); |
| 214 |
} |
| 215 |
|
| 216 |
field_type dot (const type& newv) const { |
| 217 |
using namespace Dune::Hybrid; |
| 218 |
return accumulate(integralRange(Hybrid::size(*this)), field_type(0), [&](auto&& a, auto&& i) { |
| 219 |
return a + (*this)[i].dot(newv[i]); |
| 220 |
}); |
| 221 |
} |
| 222 |
|
| 223 |
/** \brief Compute the 1-norm |
| 224 |
*/ |
| 225 |
auto one_norm() const { |
| 226 |
using namespace Dune::Hybrid; |
| 227 |
return accumulate(*this, real_type(0), [&](auto&& a, auto&& entry) { |
| 228 |
return a + entry.one_norm(); |
| 229 |
}); |
| 230 |
} |
| 231 |
|
| 232 |
/** \brief Compute the simplified 1-norm (uses 1-norm also for complex values) |
| 233 |
*/ |
| 234 |
auto one_norm_real() const { |
| 235 |
using namespace Dune::Hybrid; |
| 236 |
return accumulate(*this, real_type(0), [&](auto&& a, auto&& entry) { |
| 237 |
return a + entry.one_norm_real(); |
| 238 |
}); |
| 239 |
} |
| 240 |
|
| 241 |
/** \brief Compute the squared Euclidean norm |
| 242 |
*/ |
| 243 |
real_type two_norm2() const { |
| 244 |
using namespace Dune::Hybrid; |
| 245 |
return accumulate(*this, real_type(0), [&](auto&& a, auto&& entry) { |
| 246 |
return a + entry.two_norm2(); |
| 247 |
}); |
| 248 |
} |
| 249 |
|
| 250 |
/** \brief Compute the Euclidean norm |
| 251 |
*/ |
| 252 |
real_type two_norm() const {return sqrt(this->two_norm2());} |
| 253 |
|
| 254 |
/** \brief Compute the maximum norm |
| 255 |
*/ |
| 256 |
real_type infinity_norm() const |
| 257 |
{ |
| 258 |
using namespace Dune::Hybrid; |
| 259 |
using std::max; |
| 260 |
|
| 261 |
real_type result = 0.0; |
| 262 |
// Compute max norm tracking appearing nan values |
| 263 |
// if the field type supports nan. |
| 264 |
if constexpr (HasNaN<field_type>()) { |
| 265 |
// This variable will preserve any nan value |
| 266 |
real_type nanTracker = 1.0; |
| 267 |
using namespace Dune::Hybrid; // needed for icc, see issue #31 |
| 268 |
forEach(*this, [&](auto&& entry) { |
| 269 |
real_type entryNorm = entry.infinity_norm(); |
| 270 |
result = max(entryNorm, result); |
| 271 |
nanTracker += entryNorm; |
| 272 |
}); |
| 273 |
// Incorporate possible nan value into result |
| 274 |
result *= (nanTracker / nanTracker); |
| 275 |
} else { |
| 276 |
using namespace Dune::Hybrid; // needed for icc, see issue #31 |
| 277 |
forEach(*this, [&](auto&& entry) { |
| 278 |
result = max(entry.infinity_norm(), result); |
| 279 |
}); |
| 280 |
} |
| 281 |
return result; |
| 282 |
} |
| 283 |
|
| 284 |
/** \brief Compute the simplified maximum norm (uses 1-norm for complex values) |
| 285 |
*/ |
| 286 |
real_type infinity_norm_real() const |
| 287 |
{ |
| 288 |
using namespace Dune::Hybrid; |
| 289 |
using std::max; |
| 290 |
|
| 291 |
real_type result = 0.0; |
| 292 |
// Compute max norm tracking appearing nan values |
| 293 |
// if the field type supports nan. |
| 294 |
if constexpr (HasNaN<field_type>()) { |
| 295 |
// This variable will preserve any nan value |
| 296 |
real_type nanTracker = 1.0; |
| 297 |
using namespace Dune::Hybrid; // needed for icc, see issue #31 |
| 298 |
forEach(*this, [&](auto&& entry) { |
| 299 |
real_type entryNorm = entry.infinity_norm_real(); |
| 300 |
result = max(entryNorm, result); |
| 301 |
nanTracker += entryNorm; |
| 302 |
}); |
| 303 |
// Incorporate possible nan value into result |
| 304 |
result *= (nanTracker / nanTracker); |
| 305 |
} else { |
| 306 |
using namespace Dune::Hybrid; // needed for icc, see issue #31 |
| 307 |
forEach(*this, [&](auto&& entry) { |
| 308 |
result = max(entry.infinity_norm_real(), result); |
| 309 |
}); |
| 310 |
} |
| 311 |
return result; |
| 312 |
} |
| 313 |
|
| 314 |
/** \brief Axpy operation on this vector (*this += a * y) |
| 315 |
* |
| 316 |
* \tparam Ta Type of the scalar 'a' |
| 317 |
*/ |
| 318 |
template<typename Ta> |
| 319 |
void axpy (const Ta& a, const type& y) { |
| 320 |
using namespace Dune::Hybrid; |
| 321 |
forEach(integralRange(Hybrid::size(*this)), [&](auto&& i) { |
| 322 |
(*this)[i].axpy(a, y[i]); |
| 323 |
}); |
| 324 |
} |
| 325 |
|
| 326 |
}; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
/** \brief Send MultiTypeBlockVector to an outstream |
| 331 |
*/ |
| 332 |
template <typename... Args> |
| 333 |
std::ostream& operator<< (std::ostream& s, const MultiTypeBlockVector<Args...>& v) { |
| 334 |
using namespace Dune::Hybrid; |
| 335 |
forEach(integralRange(Dune::Hybrid::size(v)), [&](auto&& i) { |
| 336 |
s << "\t(" << i << "):\n" << v[i] << "\n"; |
| 337 |
}); |
| 338 |
return s; |
| 339 |
} |
| 340 |
|
| 341 |
} // end namespace Dune |
| 342 |
|
| 343 |
namespace std |
| 344 |
{ |
| 345 |
/** \brief Make std::tuple_element work for MultiTypeBlockVector |
| 346 |
* |
| 347 |
* It derives from std::tuple after all. |
| 348 |
*/ |
| 349 |
template <size_t i, typename... Args> |
| 350 |
struct tuple_element<i,Dune::MultiTypeBlockVector<Args...> > |
| 351 |
{ |
| 352 |
using type = typename std::tuple_element<i, std::tuple<Args...> >::type; |
| 353 |
}; |
| 354 |
|
| 355 |
/** \brief Make std::tuple_size work for MultiTypeBlockVector |
| 356 |
* |
| 357 |
* It derives from std::tuple after all. |
| 358 |
*/ |
| 359 |
template <typename... Args> |
| 360 |
struct tuple_size<Dune::MultiTypeBlockVector<Args...> > |
| 361 |
: std::integral_constant<std::size_t, sizeof...(Args)> |
| 362 |
{}; |
| 363 |
} |
| 364 |
|
| 365 |
#endif |