1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_FUNCTIONS_FUNCTIONSPACEBASES_HIERARCHICVECTORWRAPPER_HH
#define DUNE_FUNCTIONS_FUNCTIONSPACEBASES_HIERARCHICVECTORWRAPPER_HH
#include <dune/common/concept.hh>
#include <dune/common/hybridutilities.hh>
#include <dune/typetree/utility.hh>
#include <dune/functions/common/indexaccess.hh>
#include <dune/functions/common/utility.hh>
#include <dune/functions/common/type_traits.hh>
#include <dune/functions/functionspacebases/concepts.hh>
namespace Dune {
namespace Functions {
namespace Imp {
// Construct default coefficent type from vector and multiindex type
// This requires that MultiIndex has a static size. Otherwise the
// vector type itself is returned.
template<class V, class MultiIndex>
struct CoefficientType
{
template<class E, std::size_t size>
struct DefaultCoefficientTypeHelper
{
using E0 = decltype(std::declval<E>()[Dune::TypeTree::Indices::_0]);
using type = typename DefaultCoefficientTypeHelper<E0, size-1>::type;
};
template<class E>
struct DefaultCoefficientTypeHelper<E, 0>
{
using type = E;
};
template<class MI,
typename std::enable_if<HasStaticSize<MI>::value, int>::type = 0>
static constexpr std::size_t getStaticSizeOrZero()
{
return StaticSize<MI>::value;
}
template<class MI,
typename std::enable_if<not HasStaticSize<MI>::value, int>::type = 0>
static constexpr std::size_t getStaticSizeOrZero()
{
return 0;
}
using type = typename DefaultCoefficientTypeHelper<V, getStaticSizeOrZero<MultiIndex>()>::type;
};
} // namespace Imp
/**
* \brief A wrapper providing multiindex access to vector entries
*
* The coefficient type should be a type such that the coefficients
* entries for each global basis function can be cast to this type.
* This is necessary because the wrapper cannot determine this type
* automatically for multi-type containers and non-uniform indices.
* The reason for this is, that the multi-index type will then be
* dynamically sized such that the index depth cannot statically
* be determined from the multi-indices. However, the compiler needs
* a fixed termination criterion for instantiation of recursive
* functions.
*
* If no coefficient type is given, the wrapper tries to determine
* the coefficient type on its own assuming that the multi-indices
* have fixed size.
*
* \tparam V Type of the raw wrapper vector
* \tparam CO Coefficient type
*/
template<class V, class CO=void>
class HierarchicVectorWrapper
{
template<class MultiIndex>
using Coefficient = typename std::conditional< std::is_same<void,CO>::value and HasStaticSize<MultiIndex>::value,
typename Imp::CoefficientType<V, MultiIndex>::type,
CO
>::type;
using size_type = std::size_t;
template<class C, class SizeProvider,
typename std::enable_if< not models<Concept::HasResize, C>(), int>::type = 0,
typename std::enable_if< not models<Concept::HasSizeMethod, C>(), int>::type = 0>
static void resizeHelper(C& c, const SizeProvider& sizeProvider, typename SizeProvider::SizePrefix prefix)
{
auto size = sizeProvider.size(prefix);
if (size != 0)
DUNE_THROW(RangeError, "Can't resize scalar vector entry v[" << prefix << "] to size(" << prefix << ")=" << size);
}
struct StaticResizeHelper
{
template<class I, class C, class SizeProvider>
static void apply(I&& i, C& c, const SizeProvider& sizeProvider, typename SizeProvider::SizePrefix prefix)
{
prefix.back() = i;
resizeHelper(c[i], sizeProvider, prefix);
}
};
template<class C, class SizeProvider,
typename std::enable_if< not models<Concept::HasResize, C>(), int>::type = 0,
typename std::enable_if< models<Concept::HasSizeMethod, C>(), int>::type = 0>
static void resizeHelper(C& c, const SizeProvider& sizeProvider, typename SizeProvider::SizePrefix prefix)
{
auto size = sizeProvider.size(prefix);
if (size == 0)
return;
if (c.size() != size)
DUNE_THROW(RangeError, "Can't resize statically sized vector entry v[" << prefix << "] of size " << c.size() << " to size(" << prefix << ")=" << size);
using namespace Dune::Hybrid;
prefix.push_back(0);
forEach(integralRange(Hybrid::size(c)), [&](auto&& i) {
StaticResizeHelper::apply(i, c, sizeProvider, prefix);
});
}
template<class C, class SizeProvider,
typename std::enable_if< models<Concept::HasResize, C>(), int>::type = 0>
static void resizeHelper(C& c, const SizeProvider& sizeProvider, typename SizeProvider::SizePrefix prefix)
{
auto size = sizeProvider.size(prefix);
if (size==0)
{
if (c.size()==0)
DUNE_THROW(RangeError, "Can't resize dynamically sized vector entry v[" << prefix << "]. Its size is 0 but the target size is unknown due to size(" << prefix << ")=0.");
else
return;
}
c.resize(size);
prefix.push_back(0);
for(std::size_t i=0; i<size; ++i)
{
prefix.back() = i;
resizeHelper(c[i], sizeProvider, prefix);
}
}
public:
using Vector = V;
template<class MultiIndex>
using Entry = Coefficient<MultiIndex>;
HierarchicVectorWrapper(Vector& vector) :
vector_(&vector)
{}
template<class SizeProvider>
void resize(const SizeProvider& sizeProvider)
{
typename SizeProvider::SizePrefix prefix;
prefix.resize(0);
resizeHelper(*vector_, sizeProvider, prefix);
}
template<class MultiIndex>
const Entry<MultiIndex>& operator[](const MultiIndex& index) const
{
return hybridMultiIndexAccess<const Entry<MultiIndex>&>(*vector_, index);
}
template<class MultiIndex>
Entry<MultiIndex>& operator[](const MultiIndex& index)
{
return hybridMultiIndexAccess<Entry<MultiIndex>&>(*vector_, index);
}
template<class MultiIndex>
const Entry<MultiIndex>& operator()(const MultiIndex& index) const
{
return (*this)[index];
}
template<class MultiIndex>
Entry<MultiIndex>& operator()(const MultiIndex& index)
{
return (*this)[index];
}
const Vector& vector() const
{
return *vector_;
}
Vector& vector()
{
return *vector_;
}
private:
Vector* vector_;
};
template<class V>
HierarchicVectorWrapper< V > hierarchicVector(V& v)
{
return HierarchicVectorWrapper<V>(v);
}
template<class MultiIndex, class V,
typename std::enable_if< models<Concept::HasIndexAccess, V, MultiIndex>(), int>::type = 0>
V& makeHierarchicVectorForMultiIndex(V& v)
{
return v;
}
template<class MultiIndex, class V,
typename std::enable_if< not models<Concept::HasIndexAccess, V, MultiIndex>(), int>::type = 0>
HierarchicVectorWrapper< V > makeHierarchicVectorForMultiIndex(V& v)
{
return HierarchicVectorWrapper<V>(v);
}
} // namespace Dune::Functions
} // namespace Dune
#endif // DUNE_FUNCTIONS_FUNCTIONSPACEBASES_HIERARCHICVECTORWRAPPER_HH
|