SystemVector.hpp 4.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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
{
Praetorius, Simon's avatar
cleanup    
Praetorius, Simon committed
19
20
21
22
23
24
25
26
    /// \brief Container that repesents multiple data-Vectors of different value types.
    /** 
     *  Represents a \ref Dune::MultiTypeBlockVector + a tuple of corresponding 
     *  feSpaces. This I'th \ref Dune::BlockVector compbined with the I'th FeSpace 
     *  builds a \ref DOFVector  and can be return by the \ref operator[].
     * 
     *  By default, the ValueTypes are all \ref Dune::FieldVector of 1 double entry.
     **/
27
28
29
30
31
32
33
34
35
36
37
38
    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>;
	
Praetorius, Simon's avatar
cleanup    
Praetorius, Simon committed
39
	AMDIS_STATIC_ASSERT( std::tuple_size<FeSpaces>::value > 0 );
40
41
42
43
44
45
46
47
	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)
Praetorius, Simon's avatar
cleanup    
Praetorius, Simon committed
48
49
	  , vector(new MultiVector)
	  , allocated(true)
50
51
52
	{
	    compress();
	}
Praetorius, Simon's avatar
cleanup    
Praetorius, Simon committed
53
54
55
56
57
58
	
	~SystemVector()
	{
	    if (allocated)
		delete vector;
	}
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

	/// 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:
Praetorius, Simon's avatar
cleanup    
Praetorius, Simon committed
141
142
143
	FeSpaces const& feSpaces;		///< a tuple of feSpaces
	
	std::vector<std::string> names;		///< a vector of component names
144
	
Praetorius, Simon's avatar
cleanup    
Praetorius, Simon committed
145
146
	MultiVector* vector;			///< a tuple of data-vectors
	bool allicated = false;
147
148
149
    };
    
#ifndef AMDIS_NO_EXTERN_SYSTEMVECTOR
Praetorius, Simon's avatar
cleanup    
Praetorius, Simon committed
150
151
    // explicit instantiation in SystemVector.cpp
    extern template class SystemVector<typename ProblemParametersBase<2>::FeSpaces>;
152
153
154
#endif

} // end namespace AMDiS