| 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 |
#include <dune/istl/bvector.hh> |
| 6 |
#include <dune/istl/vbvector.hh> |
| 7 |
#include <dune/common/fvector.hh> |
| 8 |
#include <dune/common/classname.hh> |
| 9 |
#include <dune/common/std/type_traits.hh> |
| 10 |
#include <dune/common/test/testsuite.hh> |
| 11 |
#include <dune/common/scalarvectorview.hh> |
| 12 |
|
| 13 |
template<typename T> |
| 14 |
struct Sign |
| 15 |
{ |
| 16 |
static T complexSign() |
| 17 |
{ return T(1.0); } |
| 18 |
static T sqrtComplexSign() |
| 19 |
{ return T(1.0); } |
| 20 |
}; |
| 21 |
|
| 22 |
template<typename T> |
| 23 |
struct Sign< std::complex<T> > |
| 24 |
{ |
| 25 |
static std::complex<T> complexSign() |
| 26 |
{ return std::complex<T>(-1.0); } |
| 27 |
static std::complex<T> sqrtComplexSign() |
| 28 |
{ return std::complex<T>(0.0, 1.0); } |
| 29 |
}; |
| 30 |
|
| 31 |
// scalar ordering doesn't work for complex numbers |
| 32 |
template <class RealBlockVector, class ComplexBlockVector> |
| 33 |
Dune::TestSuite DotProductTest(const size_t numBlocks,const size_t blockSizeOrCapacity) { |
| 34 |
Dune::TestSuite t; |
| 35 |
|
| 36 |
typedef typename RealBlockVector::field_type rt; |
| 37 |
typedef typename ComplexBlockVector::field_type ct; |
| 38 |
const rt myEps((rt)1e-6); |
| 39 |
|
| 40 |
static_assert(std::is_same< typename Dune::FieldTraits<rt>::real_type, rt>::value, |
| 41 |
"DotProductTest requires real data type for first block vector!"); |
| 42 |
|
| 43 |
const ct complexSign = Sign<ct>::complexSign(); |
| 44 |
const ct I = Sign<ct>::sqrtComplexSign(); |
| 45 |
|
| 46 |
typedef typename RealBlockVector::size_type size_type; |
| 47 |
|
| 48 |
// empty vectors |
| 49 |
RealBlockVector one(numBlocks,blockSizeOrCapacity); |
| 50 |
ComplexBlockVector iVec(numBlocks,blockSizeOrCapacity); |
| 51 |
|
| 52 |
using RealBlockType = typename RealBlockVector::block_type; |
| 53 |
|
| 54 |
const size_type blockSize = Dune::Impl::asVector(one[0]).size(); |
| 55 |
|
| 56 |
t.require(numBlocks==one.N()); |
| 57 |
t.require(numBlocks==iVec.N()); |
| 58 |
const size_type length = numBlocks * blockSize; // requires inner block size of VariableBlockVector to be 1! |
| 59 |
|
| 60 |
ct ctlength = ct(length); |
| 61 |
|
| 62 |
std::cout << __func__ << "\t \t ( " << Dune::className(one) << " and \n \t \t \t " << Dune::className(iVec) << " )" << std::endl << std::endl; |
| 63 |
|
| 64 |
// initialize vectors with data |
| 65 |
for(size_type i=0; i < numBlocks; ++i) { |
| 66 |
one[i] = 1.; |
| 67 |
iVec[i] = I; |
| 68 |
} |
| 69 |
|
| 70 |
ct result = ct(); |
| 71 |
|
| 72 |
// blockwise dot tests |
| 73 |
if constexpr (!Dune::IsNumber<RealBlockType>{}){ |
| 74 |
result = ct(); |
| 75 |
for(size_type i=0; i < numBlocks; ++i) { |
| 76 |
result += dot(one[i],one[i]) + (one[i]).dot(one[i]); |
| 77 |
} |
| 78 |
|
| 79 |
t.check(std::abs(result-ct(2)*ctlength)<= myEps); |
| 80 |
|
| 81 |
result = ct(); |
| 82 |
for(size_type i=0; i < numBlocks; ++i) { |
| 83 |
result += dot(iVec[i],iVec[i])+ (iVec[i]).dot(iVec[i]); |
| 84 |
} |
| 85 |
|
| 86 |
t.check(std::abs(result-ct(2)*ctlength)<= myEps); |
| 87 |
|
| 88 |
// blockwise dotT / operator * tests |
| 89 |
result = ct(); |
| 90 |
for(size_type i=0; i < numBlocks; ++i) { |
| 91 |
result += dotT(one[i],one[i]) + one[i]*one[i]; |
| 92 |
} |
| 93 |
|
| 94 |
t.check(std::abs(result-ct(2)*ctlength)<= myEps); |
| 95 |
|
| 96 |
result = ct(); |
| 97 |
for(size_type i=0; i < numBlocks; ++i) { |
| 98 |
result += dotT(iVec[i],iVec[i]) + iVec[i]*iVec[i]; |
| 99 |
} |
| 100 |
|
| 101 |
t.check(std::abs(result-complexSign*ct(2)*ctlength)<= myEps); |
| 102 |
} |
| 103 |
|
| 104 |
// global operator * tests |
| 105 |
result = one*one + dotT(one,one); |
| 106 |
|
| 107 |
t.check(std::abs(result-ct(2)*ctlength)<= myEps); |
| 108 |
|
| 109 |
result = iVec*iVec + dotT(iVec,iVec); |
| 110 |
|
| 111 |
t.check(std::abs(result-complexSign*ct(2)*ctlength)<= myEps); |
| 112 |
|
| 113 |
// global operator dot(,) tests |
| 114 |
result = one.dot(one) + dot(one,one); |
| 115 |
|
| 116 |
t.check(std::abs(result-ct(2)*ctlength)<= myEps); |
| 117 |
result = iVec.dot(iVec) + dot(iVec,iVec); |
| 118 |
|
| 119 |
t.check(std::abs(result-ct(2)*ctlength)<= myEps); |
| 120 |
|
| 121 |
// mixed global dotT tests |
| 122 |
result = iVec*one + one*iVec + dotT(one,iVec) + dotT(iVec,one); |
| 123 |
|
| 124 |
t.check(std::abs(result-ct(4)*ctlength*I)<= myEps); |
| 125 |
|
| 126 |
// mixed global dot tests |
| 127 |
result = iVec.dot(one) + dot(iVec,one); |
| 128 |
|
| 129 |
t.check(std::abs(result-ct(2)*complexSign*ctlength*I)<= myEps); |
| 130 |
result = one.dot(iVec) + dot(one,iVec); |
| 131 |
|
| 132 |
t.check(std::abs(result-ct(2)*ctlength*I)<= myEps); |
| 133 |
|
| 134 |
return t; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
int main() |
| 139 |
{ |
| 140 |
Dune::TestSuite t; |
| 141 |
const size_t BlockSize = 5; |
| 142 |
const size_t numBlocks = 10; |
| 143 |
const size_t capacity = BlockSize * numBlocks * 2; // use capacity here, that we can use the a constructor taking two integers for both BlockVector and VariableBlockVector |
| 144 |
|
| 145 |
t.subTest(DotProductTest<Dune::BlockVector<Dune::FieldVector<int,BlockSize> >, Dune::BlockVector<Dune::FieldVector<int,BlockSize> > > (numBlocks,capacity)); |
| 146 |
t.subTest(DotProductTest<Dune::VariableBlockVector<Dune::FieldVector<int,1> >, Dune::VariableBlockVector<Dune::FieldVector<int,1> > > (numBlocks,1)); |
| 147 |
|
| 148 |
t.subTest(DotProductTest<Dune::BlockVector<Dune::FieldVector<float,BlockSize> >, Dune::BlockVector<Dune::FieldVector<std::complex<float>,BlockSize> > > (numBlocks,capacity)); |
| 149 |
t.subTest(DotProductTest<Dune::VariableBlockVector<Dune::FieldVector<float,1> >, Dune::VariableBlockVector<Dune::FieldVector<std::complex<float>,1> > > (numBlocks,BlockSize)); |
| 150 |
|
| 151 |
t.subTest(DotProductTest<Dune::BlockVector<Dune::FieldVector<float,BlockSize> >, Dune::BlockVector<Dune::FieldVector<float,BlockSize> > > (numBlocks,capacity)); |
| 152 |
t.subTest(DotProductTest<Dune::VariableBlockVector<Dune::FieldVector<float,1> >, Dune::VariableBlockVector<Dune::FieldVector<float,1> > > (numBlocks,1)); |
| 153 |
|
| 154 |
t.subTest(DotProductTest<Dune::BlockVector<Dune::FieldVector<double,BlockSize> >, Dune::BlockVector<Dune::FieldVector<std::complex<double>,BlockSize> > > (numBlocks,capacity)); |
| 155 |
t.subTest(DotProductTest<Dune::VariableBlockVector<Dune::FieldVector<double,1> >, Dune::VariableBlockVector<Dune::FieldVector<std::complex<double>,1> > > (numBlocks,BlockSize)); |
| 156 |
|
| 157 |
t.subTest(DotProductTest<Dune::BlockVector<Dune::FieldVector<double,BlockSize> >, Dune::BlockVector<Dune::FieldVector<double,BlockSize> > > (numBlocks,capacity)); |
| 158 |
t.subTest(DotProductTest<Dune::VariableBlockVector<Dune::FieldVector<double,1> >, Dune::VariableBlockVector<Dune::FieldVector<double,1> > > (numBlocks,BlockSize)); |
| 159 |
|
| 160 |
t.subTest(DotProductTest<Dune::BlockVector<double>, Dune::BlockVector<double> > (numBlocks,capacity)); |
| 161 |
|
| 162 |
return t.exit(); |
| 163 |
} |