LinearizedCompareFunctor.h 2.36 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
//======================================================================================================================
//
//  This file is part of waLBerla. waLBerla is free software: you can
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
//  for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file LinearizedCompareFunctor.h
//! \author Sebastian Eibl <sebastian.eibl@fau.de>
//
//======================================================================================================================

#pragma once

#include <core/math/AABB.h>
#include <core/math/Vector3.h>

#include <mesa_pd/data/DataTypes.h>
#include <mesa_pd/data/ParticleStorage.h>

#include <vector>

namespace walberla {
namespace mesa_pd {
namespace sorting {

/**
 * Compare functor which sorts particles along the x, y and z axes.
 */
class LinearizedCompareFunctor
{
public:
   /**
    * Subdivides a domain into cells and arranges the cells in a linearized fashion.
    * The position within the linearization gives the particles their weight.
    * \param domain domain which should be partitioned into cells
    * \param cells number of cells in every direction.
    */
   LinearizedCompareFunctor(const math::AABB& domain, const Vector3<uint_t> cells);
   bool operator()(const data::Particle bd1, const data::Particle bd2) const;
   double getWeight(const data::Particle p1) const;
private:
   uint_t discretize(const Vec3& pos) const;
   void initializeLookup();

   math::AABB  domain_;
   const Vector3<uint_t> cells_;
   Vec3 inverse_dx;
   std::vector<uint_t> hilbertLookup_;
};

inline
double LinearizedCompareFunctor::getWeight(const data::Particle p1) const
{
   const auto hash = discretize(p1.getPosition() - domain_.minCorner());
   WALBERLA_ASSERT_LESS( hash, cells_[0]*cells_[1]*cells_[2]);

   return double_c(hash);
}

} //namespace sorting
} //namespace mesa_pd
} //namespace walberla