FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
voxel_assembly_common.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/shape.hpp>
10#include <kernel/geometry/conformal_mesh.hpp>
11#include <kernel/trafo/standard/mapping.hpp>
12#include <kernel/space/lagrange2/element.hpp>
13#include <kernel/util/memory_arbiter.hpp>
14
15#ifdef FEAT_HAVE_OMP
16#include "omp.h"
17#endif
18
19#ifdef __CUDACC__
20#include <cooperative_groups.h>
21#include <cooperative_groups/memcpy_async.h>
22#include <cooperative_groups/reduce.h>
23
24namespace cg = cooperative_groups;
25#endif
26
27namespace FEAT
28{
40 namespace VoxelAssembly
41 {
46
48 template<int dim_>
50
51
58 template<typename DT_, typename IT_>
60 {
62 //const IT_* cell_to_dof;
65 //const IT_* cell_to_dof_sorter;
70 //const void* nodes;
74 };
75
81 template<typename DT_>
83 {
85 const void* cub_pt;
87 const DT_* cub_wg;
90 };
91
109 template<typename DT_, typename PointType_>
111 {
112 private:
118 int _num_cubs = 0;
119
120 public:
121 DeviceCubature() = default;
122 // Memory::Arbiter is move-only (copy deleted, move + RAII defaulted); inherit that behavior
123 DeviceCubature(const DeviceCubature&) = delete;
124 DeviceCubature& operator=(const DeviceCubature&) = delete;
125 DeviceCubature(DeviceCubature&&) = default;
126 DeviceCubature& operator=(DeviceCubature&&) = default;
127 ~DeviceCubature() = default;
128
144 void update(const PointType_* pts, const DT_* wgs, int num_cubs)
145 {
146 if(num_cubs == _num_cubs)
147 return;
148 _points = Memory::Arbiter(std::size_t(num_cubs) * sizeof(PointType_));
149 _weights = Memory::Arbiter(std::size_t(num_cubs) * sizeof(DT_));
150 // synchronous host memcpy into the arbiters' own main memory; sets valid location to main
151 _points.copy(reinterpret_cast<const void*>(pts), Memory::Location::main, Memory::Location::main);
152 _weights.copy(reinterpret_cast<const void*>(wgs), Memory::Location::main, Memory::Location::main);
153 _num_cubs = num_cubs;
154 }
155
166 {
167 const void* pts = _points.template typed_view<PointType_>(Memory::Location::cuda, Memory::Access::read).get_r();
168 const DT_* wgs = _weights.template typed_view<DT_>(Memory::Location::cuda, Memory::Access::read).get_r();
169 return AssemblyCubatureData<DT_>{pts, wgs, _num_cubs};
170 }
171 }; // class DeviceCubature
172
179 template<typename DT_, typename IT_>
181 {
182 DT_* data;
183 const IT_* row_ptr;
184 const IT_* col_idx;
185 Index num_rows;
186 Index num_cols;
187 };
188
194 template<typename DT_>
196 {
197 DT_ nu;
198 DT_ theta;
199 DT_ beta;
200 DT_ frechet_beta;
201 DT_ sd_delta;
202 DT_ sd_nu;
203 DT_ sd_v_norm;
204 bool deformation;
205 };
206
207 template<typename SpaceHelp_>
209 {
210 typedef SpaceHelp_ SpaceHelp;
211 typedef typename SpaceHelp::SpaceType SpaceType;
212 typedef typename SpaceHelp::DataType DataType;
213 typedef typename SpaceHelp::IndexType IndexType;
214 static constexpr int dim = SpaceHelp::dim;
215 static constexpr int num_loc_dofs = SpaceType::DofMappingType::dof_count;
216 static constexpr int num_loc_verts = SpaceHelp::num_verts;
217 DataType local_coeffs[dim*num_loc_verts];
218 DataType local_conv_dofs[dim*num_loc_dofs];
219 IndexType local_dofs[num_loc_dofs];
220 // these can be defined in shared memory
221 DataType tol_eps;
222 bool need_streamline;
223 bool need_convection;
224 };
225
226 template<typename SpaceHelp_>
228 {
229 typedef SpaceHelp_ SpaceHelp;
230 typedef typename SpaceHelp::SpaceType SpaceType;
231 typedef typename SpaceHelp::DataType DataType;
232 typedef typename SpaceHelp::IndexType IndexType;
233 static constexpr int dim = SpaceHelp::dim;
234 static constexpr int num_loc_dofs = SpaceType::DofMappingType::dof_count;
235 static constexpr int num_loc_verts = SpaceHelp::num_verts;
236 DataType local_coeffs[dim*num_loc_verts];
237 DataType local_conv_dofs[dim*num_loc_dofs];
238 DataType local_prim_dofs[dim*num_loc_dofs];
239 IndexType local_dofs[num_loc_dofs];
240 // these can be defined in shared memory
241 bool need_convection;
242 };
243
244 #ifdef __CUDACC__
245 template<typename DT_, int dim_>
246 CUDA_DEVICE inline DT_ dot(const Tiny::Vector<DT_, dim_>& grad, const DT_* conv, DT_ alpha = DT_(1))
247 {
248 DT_ lv(DT_(0));
249 for(int k = 0; k < dim_; ++k)
250 {
251 lv += alpha * grad[k] * conv[k];
252 }
253 return lv;
254 }
255
256 template<typename DT_, typename TG_>
257 CUDA_DEVICE inline void coalesced_format(const TG_& thread_group, DT_* target, int size)
258 {
259 for(int i = thread_group.thread_rank(); i < size; i += thread_group.num_threads())
260 {
261 target[i] = DT_(0);
262 }
263 }
264
265 template<typename DT_, typename TG_, int dim_>
266 CUDA_DEVICE inline void grouped_special_dot(const TG_& tg, DT_& loc_val, const DT_ phi, const DT_* conv, DT_ alpha = DT_(1))
267 {
268 auto coal_g = cg::coalesced_threads();
269 DT_ lv(DT_(0));
270 for(int k = 0; k < dim_; ++k)
271 {
272 lv += alpha * phi * conv[k];
273 }
274 lv = cg::reduce(coal_g, lv, cg::plus<DT_>());
275 cg::invoke_one(coal_g, [&](){atomicAdd(&loc_val, lv);});
276 }
277
278 template<typename DT_, typename TG_, int dim_>
279 CUDA_DEVICE inline void grouped_dot(const TG_& tg, DT_& loc_val, const Tiny::Vector<DT_, dim_>& grad, const DT_* conv, DT_ alpha = DT_(1))
280 {
281 auto coal_g = cg::coalesced_threads();
282 DT_ lv(DT_(0));
283 for(int k = 0; k < dim_; ++k)
284 {
285 lv += alpha * grad[k] * conv[k];
286 }
287 lv = cg::reduce(coal_g, lv, cg::plus<DT_>());
288 cg::invoke_one(coal_g, [&](){atomicAdd(&loc_val, lv);});
289 }
290
291 template<typename DT_, typename TG_, int dim_>
292 CUDA_DEVICE inline void grouped_axpy(const TG_& tg, DT_* loc_v, const DT_ phi, const DT_* conv, DT_ alpha = DT_(1))
293 {
294 auto coal_g = cg::coalesced_threads();
295 for(int k = 0; k < dim_; ++k)
296 {
297 //thos could also be done a bit nicer by using async reduce versions...
298 DT_ lv(DT_(0));
299 lv = cg::reduce(coal_g, alpha * phi * conv[k], cg::plus<DT_>());
300 cg::invoke_one(coal_g, [&](){atomicAdd(loc_v+k, lv);});
301 // coal_g.snyc();
302 }
303 }
304
306 template<typename DT_, typename TG_, int dim_>
307 CUDA_DEVICE inline void grouped_add_outer_product(const TG_& tg, DT_* loc_grad_v, const DT_* conv, const Tiny::Vector<DT_, dim_>& grad, DT_ alpha = DT_(1))
308 {
309 auto coal_g = cg::coalesced_threads();
310 for(int i(0); i < dim_; ++i)
311 {
312 for(int j(0); j < dim_; ++j)
313 {
314 DT_ lv = cg::reduce(coal_g, alpha * conv[i] * grad[j], cg::plus<DT_>());
315 cg::invoke_one(coal_g, [&](){atomicAdd(&loc_grad_v[i*dim_ + j], lv);});
316 }
317 }
318 }
319
320 template<typename DT_>
321 CUDA_DEVICE DT_* get_aligned_address(void* ptr)
322 {
323 // alignment is always a power of 2, so can use the following bit magic
324 // first add the (alignment-1) of the datatype, which leads to the next aligned
325 // address modulo bits that fall into the alignment.
326 // The simply cut off everything from the address larger then our aligned address by using
327 // AND with the negative alignment, which is simply all bits flipped of (alignment-1)
328 constexpr std::uint64_t align = alignof(DT_);
329 std::uint64_t ptr_a = reinterpret_cast<std::uint64_t>(ptr);
330 return (DT_*)((ptr_a + align - 1u)&(-align));
331 }
332
333 #endif
334
340 template<typename DT_>
342 {
343 DT_ frechet_material;
344 DT_ reg_eps;
345 DT_ mu_0;
346 DT_ exp;
347 DT_ lambda;
348 DT_ a_T;
349 DT_ yasuda_a;
350 DT_ mu_inf;
351 bool need_frechet_material;
352 };
353
358 {
359 carreau = 0,
360 carreauYasuda = 1
361 };
362
363 namespace Intern
364 {
365 template<typename DT_, MaterialType mat_type>
367
368 template<typename DT_, MaterialType mat_type>
370
371 template<typename DT_>
372 struct ViscoFunctorNew<DT_, MaterialType::carreau>
373 {
374 DT_ mu_0, exp, lambda;
375
376 CUDA_HOST_DEVICE DT_ operator()(DT_ gamma_dot, DT_ a_T) const
377 {
378 #ifdef __CUDACC__
379 return a_T * mu_0 * CudaMath::cuda_pow(DT_(1) + lambda * a_T * gamma_dot, - exp);
380 #else
381 return a_T * mu_0 * Math::pow(DT_(1) + lambda * a_T * gamma_dot, - exp);
382 #endif
383 }
384 };
385
386 template<typename DT_>
387 struct ViscoFunctor<DT_, MaterialType::carreau>
388 {
389 DT_ mu_0, exp, lambda, a_T;
390
391 CUDA_HOST_DEVICE DT_ operator()(DT_ gamma_dot) const
392 {
393 #ifdef __CUDACC__
394 return a_T * mu_0 * CudaMath::cuda_pow(DT_(1) + lambda * a_T * gamma_dot, - exp);
395 #else
396 return a_T * mu_0 * Math::pow(DT_(1) + lambda * a_T * gamma_dot, - exp);
397 #endif
398 }
399 };
400
401 template<typename DT_>
402 struct ViscoFunctorNew<DT_, MaterialType::carreauYasuda>
403 {
404 DT_ mu_0, exp, lambda, yasuda_a, mu_inf;
405
406 CUDA_HOST_DEVICE DT_ operator()(DT_ gamma_dot, DT_ a_T) const
407 {
408 #ifdef __CUDACC__
409 return a_T * mu_inf + a_T * (mu_0 - mu_inf) *
410 CudaMath::cuda_pow(DT_(1) + CudaMath::cuda_pow(lambda * a_T * gamma_dot, yasuda_a), (exp - 1) / yasuda_a);
411 #else
412 return a_T * mu_inf + a_T * (mu_0 - mu_inf) *
413 Math::pow(DT_(1) + Math::pow(lambda * a_T * gamma_dot, yasuda_a), (exp - 1) / yasuda_a);
414 #endif
415 }
416 };
417
418 template<typename DT_>
419 struct ViscoFunctor<DT_, MaterialType::carreauYasuda>
420 {
421 DT_ mu_0, exp, lambda, a_T, yasuda_a, mu_inf;
422
423 CUDA_HOST_DEVICE DT_ operator()(DT_ gamma_dot) const
424 {
425 #ifdef __CUDACC__
426 return a_T * mu_inf + a_T * (mu_0 - mu_inf) *
427 CudaMath::cuda_pow(DT_(1) + CudaMath::cuda_pow(lambda * a_T * gamma_dot, yasuda_a), (exp - DT_(1)) / yasuda_a);
428 #else
429 return a_T * mu_inf + a_T * (mu_0 - mu_inf) *
430 Math::pow(DT_(1) + Math::pow(lambda * a_T * gamma_dot, yasuda_a), (exp - DT_(1)) / yasuda_a);
431 #endif
432 }
433 };
434
435 template<typename DT_, MaterialType mat_type>
437
438 template<typename DT_, MaterialType mat_type>
440
441 template<typename DT_>
442 struct ViscoDFunctorNew<DT_, MaterialType::carreau>
443 {
444 DT_ mu_0, exp, lambda;
445
446 CUDA_HOST_DEVICE DT_ operator()(DT_ gamma_dot, DT_ a_T) const
447 {
448 #ifdef __CUDACC__
449 return - a_T * a_T * mu_0 * lambda * exp *
450 CudaMath::cuda_pow(DT_(1) + lambda * a_T * gamma_dot, - exp - DT_(1));
451 #else
452 return - a_T * a_T * mu_0 * lambda * exp *
453 Math::pow(DT_(1) + lambda * a_T * gamma_dot, - exp - DT_(1));
454 #endif
455 }
456 };
457
458 template<typename DT_>
459 struct ViscoDFunctor<DT_, MaterialType::carreau>
460 {
461 DT_ mu_0, exp, lambda, a_T;
462
463 CUDA_HOST_DEVICE DT_ operator()(DT_ gamma_dot) const
464 {
465 #ifdef __CUDACC__
466 return - a_T * a_T * mu_0 * lambda * exp *
467 CudaMath::cuda_pow(DT_(1) + lambda * a_T * gamma_dot, - exp - DT_(1));
468 #else
469 return - a_T * a_T * mu_0 * lambda * exp *
470 Math::pow(DT_(1) + lambda * a_T * gamma_dot, - exp - DT_(1));
471 #endif
472 }
473 };
474
475 template<typename DT_>
476 struct ViscoDFunctorNew<DT_, MaterialType::carreauYasuda>
477 {
478 DT_ mu_0, exp, lambda, yasuda_a, mu_inf;
479
480 CUDA_HOST_DEVICE DT_ operator()(DT_ gamma_dot, DT_ a_T) const
481 {
482 #ifdef __CUDACC__
483 return a_T * (mu_0 - mu_inf) * ((exp-DT_(1.0)) / yasuda_a) *
484 CudaMath::cuda_pow( DT_(1.0) + CudaMath::cuda_pow(lambda * gamma_dot, yasuda_a), ((exp - DT_(1.0) - yasuda_a) / yasuda_a)) *
485 yasuda_a * lambda * a_T * CudaMath::cuda_pow(lambda * gamma_dot * a_T, yasuda_a-DT_(1.0));
486 #else
487 return a_T * (mu_0 - mu_inf) * ((exp-DT_(1.0)) / yasuda_a) *
488 Math::pow( DT_(1.0) + Math::pow(lambda * gamma_dot, yasuda_a), ((exp - DT_(1.0) - yasuda_a) / yasuda_a)) *
489 yasuda_a * lambda * a_T * Math::pow(lambda * gamma_dot * a_T, yasuda_a-DT_(1.0));
490 #endif
491 }
492 };
493
494 template<typename DT_>
495 struct ViscoDFunctor<DT_, MaterialType::carreauYasuda>
496 {
497 DT_ mu_0, exp, lambda, a_T, yasuda_a, mu_inf;
498
499 CUDA_HOST_DEVICE DT_ operator()(DT_ gamma_dot) const
500 {
501 #ifdef __CUDACC__
502 return a_T * (mu_0 - mu_inf) * ((exp-DT_(1.0)) / yasuda_a) *
503 CudaMath::cuda_pow( DT_(1.0) + CudaMath::cuda_pow(lambda * gamma_dot, yasuda_a), ((exp - DT_(1.0) - yasuda_a) / yasuda_a)) *
504 yasuda_a * lambda * a_T * CudaMath::cuda_pow(lambda * gamma_dot * a_T, yasuda_a-DT_(1.0));
505 #else
506 return a_T * (mu_0 - mu_inf) * ((exp-DT_(1.0)) / yasuda_a) *
507 Math::pow( DT_(1.0) + Math::pow(lambda * gamma_dot, yasuda_a), ((exp - DT_(1.0) - yasuda_a) / yasuda_a)) *
508 yasuda_a * lambda * a_T * Math::pow(lambda * gamma_dot * a_T, yasuda_a-DT_(1.0));
509 #endif
510 }
511 };
512 }
513 }
514}
FEAT Kernel base header.
Memory arbiter class.
void copy(const Arbiter &source, Location mem_loc=Location::none)
Copies of the contents of one memory block into another one.
Standard Lagrange-2 Finite-Element space class template.
Definition: element.hpp:39
Tiny Vector class template.
Owns the device-resident cubature (points + weights) of a voxel assembler.
Memory::Arbiter _points
device/host buffer for the cubature points
int _num_cubs
number of cubature points currently cached (0 = nothing uploaded yet)
AssemblyCubatureData< DT_ > device_data() const
Returns the device-side view of the cached cubature.
void update(const PointType_ *pts, const DT_ *wgs, int num_cubs)
Uploads the cubature into the cached host buffers if it changed.
Memory::Arbiter _weights
device/host buffer for the cubature weights
T_ pow(T_ x, T_ y)
Returns x raised to the power of y.
Definition: math.hpp:643
@ read
read-only access
Space::Lagrange2::Element< Trafo::Standard::Mapping< Geometry::ConformalMesh< Shape::Hypercube< 3 > > > > Q2StandardHexa
Q2 hexadron space.
Space::Lagrange2::Element< Trafo::Standard::Mapping< Geometry::ConformalMesh< Shape::Hypercube< 2 > > > > Q2StandardQuad
Q2 quadliteral space.
MaterialType
Enum for different material types.
FEAT namespace.
Definition: adjactor.hpp:12
@ grad
specifies whether the space should supply basis function gradients
std::uint64_t Index
Index data type.
const void * cub_pt
The cubature point data array.
A data field for all necessary values that define the dof mapping for assembly.
const Memory::Arbiter & cell_to_dof
The cell to dof, where cell_to_dof[i],..., cell_to_dof[i+cell_dofs-1] are the dofs of one cell.
const Memory::Arbiter & nodes
An array of the nodes fitting to the cell_to_dof mapping.
const Memory::Arbiter & cell_to_dof_sorter
Array of sortingindices of cell_to_dof.
Data for material burgers assembler.