FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
data_handler.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
9#include <kernel/backend.hpp>
10#include <kernel/voxel_assembly/voxel_assembly_common.hpp>
11#include <kernel/voxel_assembly/helper/cell_to_dof_helper.hpp>
12#include <kernel/util/tiny_algebra.hpp>
13#include <kernel/adjacency/coloring_data_handler.hpp>
14#include <kernel/util/time_stamp.hpp>
15#include <kernel/util/stop_watch.hpp>
16
17#ifdef FEAT_HAVE_CUDA
18#include <kernel/util/cuda_util.hpp>
19#endif
20
21#include <vector>
22
23namespace FEAT
24{
25 namespace VoxelAssembly
26 {
27 //Warning: for now only works with Q2standard space... we should probably use this class as specialzation for lagrange based spaces...
40 template<typename Space_, typename DT_, typename IT_>
42 {
43 public:
44 typedef Space_ SpaceType;
45 typedef DT_ DataType;
46 typedef IT_ IndexType;
47
48 static constexpr int dim = SpaceType::world_dim;
49 protected:
56
61
62 public:
65
66 public:
67 double time_off_load_mesh, time_off_load_color;
68 double time_init_mesh, time_init_color;
69
70 public:
71 inline AssemblyMappingData<DataType, IndexType> get_assembly_field() const
72 {
74 }
75
76 protected:
77 //Container has to be sortable and a and b have to be sorted beforhand....
78 template<typename ITX_>
79 bool _contains_common_element(const ITX_* a, const ITX_* ae, const ITX_* b, const ITX_* be) const
80 {
81 std::vector<ITX_> intersection;
82
83 std::set<ITX_> aa { a, ae };
84 std::set<ITX_> bb { b, be };
85
86 std::set_intersection(aa.begin(), aa.end(),
87 bb.begin(), bb.end(),
88 std::inserter(intersection, intersection.end()));
89
90 return !intersection.empty();
91 }
92
93 bool _test_coloring() const
94 {
95 //const auto& _coloring_maps = coloring_data.get_coloring_maps();
96 const Memory::TypedView<int> coloring_view = coloring_data.color_map_view(Memory::Location::main, Memory::Access::read);
97 const Memory::TypedView<IndexType> cell_to_dof_view(_cell_to_dof.view(Memory::Location::main, Memory::Access::read));
98 const auto& _coloring_map_offsets = coloring_data.get_color_offsets();
99 const auto& _coloring_map_sizes = coloring_data.get_color_sizes();
100 for(std::size_t i = 0; i < _coloring_map_sizes.size(); ++i)
101 {
102 for(std::size_t l = 0; l < _coloring_map_sizes.at(i); ++l)
103 {
104 //int cell_a = _coloring_maps[std::size_t(i)][std::size_t(l)];
105 int cell_a = coloring_view[_coloring_map_offsets[i] + l];
106 const IndexType* a_b = &cell_to_dof_view[std::size_t(cell_a*SpaceType::DofMappingType::dof_count)];
107 const IndexType* a_e = a_b + SpaceType::DofMappingType::dof_count;
108 for(std::size_t j = l+1; j < _coloring_map_sizes[i]; ++j)
109 {
110 //int cell_b = _coloring_maps[std::size_t(i)][std::size_t(j)];
111 int cell_b = coloring_view[_coloring_map_offsets[i] + j];
112 const IndexType* b_b = &cell_to_dof_view[std::size_t(cell_b*SpaceType::DofMappingType::dof_count)];
113 const IndexType* b_e = b_b + SpaceType::DofMappingType::dof_count;
114
115 if(_contains_common_element(a_b, a_e, b_b, b_e))
116 {
117 std::cout << "Cell 1: ";
118 for(int r = 0; r < SpaceType::DofMappingType::dof_count; ++r)
119 {
120 std::cout << *(a_b + r) << " ";
121 }
122 std::cout << "\nCell 2: ";
123 for(int r = 0; r < SpaceType::DofMappingType::dof_count; ++r)
124 {
125 std::cout << *(b_b + r) << " ";
126 }
127 std::cout << "\n";
128 XABORTM("Intersection in color " + stringify(i) + " between cells " + stringify(cell_a) + " " + stringify(cell_b));
129 }
130 }
131 }
132
133 for(std::size_t j = i+1; j < _coloring_map_sizes.size(); ++j)
134 {
135 //if(_contains_common_element(
136 // _coloring_maps.at(std::size_t(i)),
137 // _coloring_maps.at(std::size_t(i))+_coloring_map_sizes.at(std::size_t(i)),
138 // _coloring_maps.at(std::size_t(j)),
139 // _coloring_maps.at(std::size_t(j))+_coloring_map_sizes.at(std::size_t(j))))
140 const int* cvi = &coloring_view.get_r()[_coloring_map_offsets[i]];
141 const int* cvj = &coloring_view.get_r()[_coloring_map_offsets[j]];
142 if(_contains_common_element(
143 cvi, cvi + _coloring_map_sizes.at(i),
144 cvj, cvj + _coloring_map_sizes.at(j)))
145 {
146 std::cout << "Colors contain common element!\n";
147 XABORTM("I think you have misunderstood colors.");
148 }
149 }
150 }
151
152 return true;
153
154 }
155
156 void _fill_color(const std::vector<int>& coloring, int hint)
157 {
158 TimeStamp stamp1;
159 coloring_data.fill_color(coloring, hint);
160 #ifdef DEBUG
161 _test_coloring();
162 #endif
163 TimeStamp stamp2;
164 time_init_color = stamp2.elapsed(stamp1);
165 }
166
167 void _fill_color(const Adjacency::Coloring& coloring, int hint)
168 {
169 TimeStamp stamp1;
170 coloring_data.fill_color(coloring, hint);
171 #ifdef DEBUG
172 _test_coloring();
173 #endif
174 TimeStamp stamp2;
175 time_init_color = stamp2.elapsed(stamp1);
176 }
177
178 public:
179 explicit LagrangeDataHandler() = default;
180
181 template<typename ColoringType_>
182 explicit LagrangeDataHandler(const SpaceType& space, const ColoringType_& coloring, int hint = -1) :
183 _cell_to_dof(),
186 _nodes(),
187 _nodes_size(Index(0)),
188 time_off_load_mesh(0.),
189 time_off_load_color(0.),
190 time_init_mesh(0.),
191 time_init_color(0.)
192 {
193 TimeStamp stamp1;
194 if constexpr(std::is_same<ColoringType_, Adjacency::Coloring>::value)
195 {
196 ASSERTM(space.get_mesh().get_num_entities(dim) == coloring.get_num_nodes(), "Coloring and space do not fit!");
197 }
198 else
199 {
200 ASSERTM(space.get_mesh().get_num_entities(dim) == coloring.size(), "Coloring and space do not fit!");
201 }
202 _nodes_size = space.get_mesh().get_vertex_set().get_num_vertices();
203 _nodes = Memory::Arbiter(sizeof(Tiny::Vector<DataType, dim>) * _nodes_size);
204 // copy the internal nodes array
205 const auto* vertex_begin = (const typename SpaceType::MeshType::VertexSetType::CoordType*)space.get_mesh().get_vertex_set().begin();
206 std::transform(vertex_begin, vertex_begin + _nodes_size*Index(dim),
207 _nodes.template typed_view<DataType>(Memory::Location::main, Memory::Access::write).get_w(),
208 [](const auto& a) ->DataType {return DataType(a);});
209
210 // define our _cell_to_dof
211 _cell_to_dof_size = space.get_mesh().get_num_entities(dim) * SpaceType::DofMappingType::dof_count;
212 _cell_to_dof = Memory::Arbiter(sizeof(IndexType) * std::size_t(_cell_to_dof_size));
213 _cell_to_dof_sorter = Memory::Arbiter(sizeof(IndexType) * std::size_t(_cell_to_dof_size));
214 // for this iterate through our target_sets and parse them in
215 {
216 Memory::TypedView<IndexType> ctdv(_cell_to_dof.view(Memory::Location::main, Memory::Access::write));
217 Memory::TypedView<IndexType> ctdsv(_cell_to_dof_sorter.view(Memory::Location::main, Memory::Access::write));
218 VoxelAssembly::fill_cell_to_dof(ctdv.get_w(), space);
219 VoxelAssembly::fill_sorter(ctdsv.get_w(), ctdv.get_r(), space);
220 }
221 TimeStamp stamp2;
222 time_init_mesh = stamp2.elapsed(stamp1);
223
224 // for(int cell = 0; cell < space.get_mesh().get_num_elements(); ++cell)
225 // {
226 // std::cout << "For cell " << cell << ": \n";
227 // IndexType* ctd = _cell_to_dof.data() + cell * SpaceType::DofMappingType::dof_count;
228 // for(int i = 0; i < SpaceType::DofMappingType::dof_count; ++i)
229 // {
230 // std::cout << ctd[i] << " ";
231 // }
232 // std::cout << "\n";
233 // }
234
235 _fill_color(coloring, hint);
236
237 }
238
239 LagrangeDataHandler(const LagrangeDataHandler&) = delete;
240
241 LagrangeDataHandler& operator=(const LagrangeDataHandler&) = delete;
242
243 LagrangeDataHandler(LagrangeDataHandler&& other) noexcept :
244 _cell_to_dof(std::move(other._cell_to_dof)),
245 _cell_to_dof_sorter(std::move(other._cell_to_dof_sorter)),
246 _cell_to_dof_size(other._cell_to_dof_size),
247 _nodes(std::move(other._nodes)),
248 _nodes_size(other._nodes_size),
249 coloring_data(std::move(other.coloring_data)),
250 time_off_load_mesh(other.time_off_load_mesh),
251 time_off_load_color(other.time_off_load_color),
252 time_init_mesh(other.time_init_mesh),
253 time_init_color(other.time_init_color)
254 {
255 }
256
257 LagrangeDataHandler& operator=(LagrangeDataHandler&& other) noexcept
258 {
259 if(this == &other)
260 return *this;
261 time_off_load_mesh = other.time_off_load_mesh;
262 time_off_load_color = other.time_off_load_color;
263 time_init_mesh = other.time_init_mesh;
264 time_init_color = other.time_init_color;
265 coloring_data = std::move(other.coloring_data);
266 _cell_to_dof = std::move(other._cell_to_dof);
267 _cell_to_dof_sorter = std::move(other._cell_to_dof_sorter);
268 _cell_to_dof_size = other._cell_to_dof_size;
269 _nodes = std::move(other._nodes);
270 _nodes_size = other._nodes_size;
271 return *this;
272 }
273
274 ~LagrangeDataHandler() = default;
275 }; //class GPUDataHandler
276 }
277}
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define ASSERTM(expr, msg)
Debug-Assertion macro definition with custom message.
Definition: assertion.hpp:230
FEAT Kernel base header.
Datahandler for inverse coloring data.
void fill_color(const std::vector< int > &coloring, int hint=-1)
Fill in the coloring array.
Memory arbiter class.
View view(Location mem_loc, Access access)
Maps a raw view to a memory block.
Data handler for Lagrange based FE spaces.
Index _cell_to_dof_size
Size of cell to dof array.
Memory::Arbiter _cell_to_dof_sorter
Array mapping each _cell_to_dof entry to it locally sorted position.
Index _nodes_size
Size of node array.
Adjacency::ColoringDataHandler coloring_data
Datahandler for the coloring data.
Memory::Arbiter _cell_to_dof
Array mapping each cell index to ALL its local dofs in correct numbering.
Memory::Arbiter _nodes
Array of node coordinates.
@ other
generic/other permutation strategy
@ read
read-only access
@ write
write-only access (see attention note above)
void fill_cell_to_dof(IT_ *ctd, const Space_ &space, IT_ offset=0)
Computes a sequentialized dof mapping for the given space.
FEAT namespace.
Definition: adjactor.hpp:12
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
std::uint64_t Index
Index data type.
A data field for all necessary values that define the dof mapping for assembly.