Finite Element Domain Decomposition Library
FEDDLib
Loading...
Searching...
No Matches
FEDDUtils.hpp
1#ifndef FEDDUTILS_hpp
2#define FEDDUTILS_hpp
3
4#include "feddlib/core/FEDDCore.hpp"
5
6
7#ifndef FEDD_TIMER_START
8#define FEDD_TIMER_START(A,S) Teuchos::RCP<Teuchos::TimeMonitor> A = Teuchos::rcp(new Teuchos::TimeMonitor(*Teuchos::TimeMonitor::getNewTimer(std::string("FEDD") + std::string(S))));
9#endif
10
11#ifndef FEDD_TIMER_STOP
12#define FEDD_TIMER_STOP(A) A.reset();
13#endif
14
15
16namespace FEDD{
17
18template<class ForwardIt, class GO>
19ForwardIt uniqueWithCombines(ForwardIt first, ForwardIt last, std::vector<std::vector<GO> >& combines)
20{
21
22 if (first == last)
23 return last;
24
25 ForwardIt firstForDistance = first;
26 ForwardIt result = first;
27 combines[ distance( firstForDistance, result ) ].push_back( (GO) distance( firstForDistance, first ) );
28 while (++first != last) {
29 if (!(*result == *first) && ++result != first) {
30 *result = std::move(*first);
31 // also add the element which is the final unique element (the first in the sorted list)
32 combines[ distance( firstForDistance, result ) ].push_back( (GO) distance( firstForDistance, first ) );
33 }
34 else{
35 combines[ distance( firstForDistance, result ) ].push_back( (GO) distance( firstForDistance, first ) );
36 }
37 }
38 return ++result;
39};
40
41template <typename T>
42std::vector<T> sort_from_ref(
43 std::vector<T> const& in,
44 std::vector<int> const& reference
45 ) {
46 std::vector<T> ret(in.size());
47
48 int const size = in.size();
49 for (int i = 0; i < size; ++i)
50 ret[i] = in[reference[i]];
51
52 return ret;
53};
54
55template <typename T>
56std::vector<T> sort_from_ref(
57 std::vector<T> const& in,
58 std::vector<long long> const& reference
59 ) {
60 std::vector<T> ret(in.size());
61
62 int const size = in.size();
63 for (long long i = 0; i < size; ++i)
64 ret[i] = in[reference[i]];
65
66 return ret;
67};
68
69
70template <typename T>
71void sort2byFirst( std::vector<std::vector<T> >& in, std::vector<T>& in2 )
72{
73
74 std::vector<int> index(in.size(), 0);
75 for (int i = 0 ; i != index.size() ; i++)
76 index[i] = i;
77
78 std::sort(index.begin(), index.end(),
79 [&](const int& a, const int& b) {
80 return in[a] < in[b];
81 }
82 );
83 in = sort_from_ref( in, index );
84 in2 = sort_from_ref( in2, index );
85
86}
87
88template <typename T, class GO>
89void make_unique( std::vector<std::vector<T> >& in, vec2D_GO_Type& combinedElements, std::vector<GO>& globaIDs )
90{
91 {
92 std::vector<int> index(in.size(), 0);
93 for (int i = 0 ; i != index.size() ; i++)
94 index[i] = i;
95
96 std::sort(index.begin(), index.end(),
97 [&](const int& a, const int& b) {
98 return in[a] < in[b];
99 }
100 );
101 in = sort_from_ref( in, index );
102 globaIDs = sort_from_ref( globaIDs, index );
103 }
104 {
105 std::vector<int> index(in.size(), 0);
106 for (int i = 0 ; i != index.size() ; i++)
107 index[i] = i;
108
109 combinedElements.resize( in.size() );
110
111 auto it = uniqueWithCombines( in.begin(), in.end(), combinedElements );
112
113 in.resize( distance( in.begin(), it ) );
114 combinedElements.resize( in.size() );
115 }
116};
117
118template <typename T>
119void make_unique( std::vector<std::vector<T> >& in )
120{
121 {
122 std::vector<int> index(in.size(), 0);
123 for (int i = 0 ; i != index.size() ; i++)
124 index[i] = i;
125
126 std::sort(index.begin(), index.end(),
127 [&](const int& a, const int& b) {
128 return in[a] < in[b];
129 }
130 );
131 in = sort_from_ref( in, index );
132 }
133 {
134
135 auto it = std::unique( in.begin(), in.end() );
136
137 in.resize( distance( in.begin(), it ) );
138 }
139};
140
141template <typename T>
142void make_unique( std::vector<std::vector<T> >& in, vec2D_GO_Type& combinedElements )
143{
144 {
145 std::vector<int> index(in.size(), 0);
146 for (int i = 0 ; i != index.size() ; i++)
147 index[i] = i;
148
149 std::sort(index.begin(), index.end(),
150 [&](const int& a, const int& b) {
151 return in[a] < in[b];
152 }
153 );
154 in = sort_from_ref( in, index );
155
156 }
157 {
158 std::vector<int> index(in.size(), 0);
159 for (int i = 0 ; i != index.size() ; i++)
160 index[i] = i;
161
162 combinedElements.resize( in.size() );
163
164 auto it = uniqueWithCombines( in.begin(), in.end(), combinedElements );
165
166 in.resize( distance( in.begin(), it ) );
167
168 combinedElements.resize( in.size() );
169 }
170};
171
172template <typename T>
173std::vector<T> operator+(const std::vector<T>& a, const std::vector<T>& b)
174{
175 assert(a.size() == b.size());
176
177 std::vector<T> result;
178 result.reserve(a.size());
179
180 std::transform(a.begin(), a.end(), b.begin(),
181 std::back_inserter(result), std::plus<T>());
182 return result;
183};
184
185template <typename T>
186std::vector<T> operator-(const std::vector<T>& a, const std::vector<T>& b)
187{
188 assert(a.size() == b.size());
189
190 std::vector<T> result;
191 result.reserve(a.size());
192
193 std::transform(a.begin(), a.end(), b.begin(),
194 std::back_inserter(result), std::minus<T>());
195 return result;
196};
197
198template <typename T>
199void make_unique( std::vector<T>& in )
200{
201 std::sort( in.begin(), in.end() );
202 auto it = unique( in.begin(), in.end() );
203 in.erase( it, in.end() );
204};
205
206
207}
208#endif
Adaptive Mesh Refinement.
Definition AdaptiveMeshRefinement.cpp:5