SystemVector.hpp 3.57 KB
Newer Older
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
#pragma once


#include <vector>
#include <string>
#include <memory>
#include <tuple>

#include <dune/istl/multitypeblockvector.hh>

#include "Basic.hpp"
#include "DOFVector.hpp"
#include "Log.hpp"
#include "Loops.hpp"
#include "ProblemStatBase.hpp"

namespace AMDiS
{
    template <class FeSpaces, 
	      class ValueTypes = Repeat_t<std::tuple_size<FeSpaces>::value, Dune::FieldVector<double,1>> >
    class SystemVector
    {
    public:
	template <class T> using SimpleBlockVector = Dune::BlockVector<T, std::allocator<T>>;
	
	using BaseVectors = MakeTuple_t<SimpleBlockVector, ValueTypes>;
	using MultiVector = ExpandTuple_t<Dune::MultiTypeBlockVector, BaseVectors>;
	
	using DOFVectors  = MakeTuple2_t<DOFVector, FeSpaces, ValueTypes>;
	
	AMDIS_STATIC_ASSERT( (std::tuple_size<FeSpaces>::value > 0) );
	AMDIS_STATIC_ASSERT( std::tuple_size<FeSpaces>::value == std::tuple_size<ValueTypes>::value );
	AMDIS_STATIC_ASSERT( std::is_same< typename std::tuple_element_t<0, DOFVectors>::BaseVector,
					      std::tuple_element_t<0, BaseVectors> >::value );
	
	/// Constructor that constructs new base-vectors
	SystemVector(FeSpaces const& feSpaces, std::vector<std::string> const& names)
	  : feSpaces(feSpaces)
	  , names(names)
	  , vector(std::make_shared<MultiVector>())
	{
	    compress();
	}

	/// Return the number of vector entries
	static constexpr size_t size()
	{
	    return std::tuple_size<FeSpaces>::value;
	}

	/// Return number of elements
	int count() const
	{
	    return int(size());
	}
	
	/// Return a shared pointer to the i'th underlaying base vector
	template <size_t I>
	decltype(auto) getVector(const index_<I> = index_<I>())
	{
	    return std::get<I>(*vector);
	}
	
	/// Return a shared pointer to the i'th underlaying base vector
	template <size_t I>
	decltype(auto) getVector(const index_<I> = index_<I>()) const
	{
	    return std::get<I>(*vector);
	}
	
	/// Return the underlying multi vector
	MultiVector const& getVector() const
	{
	    return *vector;
	}
	
	/// Return the I'th finite element space
	template <size_t I = 0>
	decltype(auto) getFeSpace(const index_<I> = index_<I>()) const
	{
	    return std::get<I>(feSpaces);
	}
	
	/// Return the name of the i'th DOFVector
	std::string getName(size_t i) const
	{
	    return names[i];
	}
	
	/// Create a DOFVector with references to the unerlaying data
	template <size_t I>
	std::tuple_element_t<I, DOFVectors>
	operator[](const index_<I> _i)
	{
	    AMDIS_TEST_EXIT_DBG( _i < size() , 
		"index " << _i << " out of range [" << 0 << "," << size() << ")" );
	    return {getFeSpace(_i), getName(_i), getVector(_i)};
	}
	
	/// Create a DOFVector with references to the unerlaying data
	template <size_t I>
	std::tuple_element_t<I, DOFVectors> const
	operator[](const index_<I> _i) const
	{
	    AMDIS_TEST_EXIT_DBG( _i < size() , 
		"index " << _i << " out of range [" << 0 << "," << size() << ")" );
	    return {getFeSpace(_i), getName(_i), getVector(_i)};
	}
	
	/// Resize the I'th \ref vector to the sizes of the I'th \ref feSpaces.
	template <size_t I = 0>
	void compress(const index_<I> = index_<I>())
	{
	    std::get<I>(*vector).resize( std::get<I>(feSpaces).size() );
	}
	
	/// Resize the \ref vectors to the sizes of the \ref feSpaces.
	void compress()
	{
	    For<0, size()>::loop([this](const auto I) {
		std::get<I>(*vector).resize( std::get<I>(feSpaces).size() );
	    });
	}
	
    private:
	FeSpaces const& feSpaces;
	std::vector<std::string> names;
	
	std::shared_ptr<MultiVector> vector;
    };
    
#ifndef AMDIS_NO_EXTERN_SYSTEMVECTOR
  extern template class SystemVector<typename ProblemParametersBase<2>::FeSpaces>;
#endif

} // end namespace AMDiS