Skip to content
Snippets Groups Projects
Commit 400a1cb9 authored by Sebastian Eibl's avatar Sebastian Eibl
Browse files

add grid classes to grid_generator to allow range based for loops

parent f826c7fb
Branches
Tags
No related merge requests found
......@@ -76,5 +76,39 @@ private:
bool ended_;
};
/// Convenience class to enable range based for loops over grid points.
/// Usage:
/// \code for (const auto& pt : HCPGrid(...) ) \endcode
class HCPGrid
{
public:
using iterator = HCPIterator;
using value_type = iterator::value_type;
/**
* @param domain volume were lattice points will be returned
* @param pointOfReference point somewhere in the world which fixes the lattice
* @param spacing spacing between grid points in x direction
*/
HCPGrid(const AABB& domain, const Vector3<real_t>& pointOfReference, const real_t spacing)
: domain_(domain)
, pointOfReference_(pointOfReference)
, spacing_(spacing)
{}
HCPIterator begin() {return HCPIterator(domain_, pointOfReference_, spacing_);}
HCPIterator begin() const {return HCPIterator(domain_, pointOfReference_, spacing_);}
HCPIterator cbegin() const {return HCPIterator(domain_, pointOfReference_, spacing_);}
HCPIterator end() {return HCPIterator();}
HCPIterator end() const {return HCPIterator();}
HCPIterator cend() const {return HCPIterator();}
private:
AABB domain_;
Vector3<real_t> pointOfReference_;
real_t spacing_;
};
} // namespace grid_generator
} // namespace walberla
......@@ -30,7 +30,7 @@
namespace walberla {
namespace grid_generator {
SCIterator::SCIterator(const AABB& domain, const Vector3<real_t>& pointOfReference, const real_t spacing)
SCIterator::SCIterator(const AABB& domain, const Vector3<real_t>& pointOfReference, const real_t& spacing)
: i_(0)
, j_(0)
, k_(0)
......
......@@ -55,7 +55,7 @@ public:
* @param pointOfReference point somewhere in the world which fixes the lattice
* @param spacing spacing between grid points in x, y and z direction
*/
SCIterator(const AABB& domain, const Vector3<real_t>& pointOfReference, const real_t spacing);
SCIterator(const AABB& domain, const Vector3<real_t>& pointOfReference, const real_t& spacing);
/**
* @brief begin iterator
* @param domain volume were lattice points will be returned
......@@ -77,9 +77,9 @@ public:
static inline real_t getUnitCellX(const real_t spacing) { return spacing; }
static inline real_t getUnitCellY(const real_t spacing) { return spacing; }
static inline real_t getUnitCellZ(const real_t spacing) { return spacing; }
static inline real_t getUnitCellX(const Vector3<real_t> spacing) { return spacing[0]; }
static inline real_t getUnitCellY(const Vector3<real_t> spacing) { return spacing[1]; }
static inline real_t getUnitCellZ(const Vector3<real_t> spacing) { return spacing[2]; }
static inline real_t getUnitCellX(const Vector3<real_t>& spacing) { return spacing[0]; }
static inline real_t getUnitCellY(const Vector3<real_t>& spacing) { return spacing[1]; }
static inline real_t getUnitCellZ(const Vector3<real_t>& spacing) { return spacing[2]; }
private:
void updatePoint();
......@@ -99,5 +99,49 @@ private:
bool ended_;
};
/// Convenience class to enable range based for loops over grid points.
/// Usage:
/// \code for (const auto& pt : SCGrid(...) ) \endcode
class SCGrid
{
public:
using iterator = SCIterator;
using value_type = iterator::value_type;
/**
* @param domain volume were lattice points will be returned
* @param pointOfReference point somewhere in the world which fixes the lattice
* @param spacing spacing between grid points in x, y and z direction
*/
SCGrid(const AABB& domain, const Vector3<real_t>& pointOfReference, const real_t spacing)
: domain_(domain)
, pointOfReference_(pointOfReference)
, spacing_(spacing, spacing, spacing)
{}
/**
* @brief begin iterator
* @param domain volume were lattice points will be returned
* @param pointOfReference point somewhere in the world which fixes the lattice
* @param spacing spacing between grid points in x, y and z direction
*/
SCGrid(const AABB& domain, const Vector3<real_t>& pointOfReference, const Vector3<real_t>& spacing)
: domain_(domain)
, pointOfReference_(pointOfReference)
, spacing_(spacing)
{}
SCIterator begin() {return SCIterator(domain_, pointOfReference_, spacing_);}
SCIterator begin() const {return SCIterator(domain_, pointOfReference_, spacing_);}
SCIterator cbegin() const {return SCIterator(domain_, pointOfReference_, spacing_);}
SCIterator end() {return SCIterator();}
SCIterator end() const {return SCIterator();}
SCIterator cend() const {return SCIterator();}
private:
AABB domain_;
Vector3<real_t> pointOfReference_;
Vector3<real_t> spacing_;
};
} // namespace grid_generator
} // namespace walberla
......@@ -90,6 +90,23 @@ void referencePointTest()
WALBERLA_CHECK_EQUAL( higherIt, endIt);
}
template <class Grid>
void rangeBasedTest()
{
math::AABB domain(0,0,0,10,10,10);
real_t spacing = real_c(1);
auto dx = Vector3<real_t>( Grid::iterator::getUnitCellX(spacing), Grid::iterator::getUnitCellY(spacing), Grid::iterator::getUnitCellZ(spacing) );
auto lowerIt = typename Grid::iterator(domain, Vector3<real_t>(5,5,5) - dx * 30, spacing);
auto endIt = typename Grid::iterator();
for ( const auto& pt : Grid(domain, Vector3<real_t>(5,5,5) - dx * 30, spacing) )
{
WALBERLA_CHECK( lowerIt != endIt );
WALBERLA_CHECK_FLOAT_EQUAL( *lowerIt, pt);
++lowerIt;
}
WALBERLA_CHECK( lowerIt == endIt );
}
int main( int argc, char** argv )
{
walberla::Environment env(argc, argv);
......@@ -139,5 +156,8 @@ int main( int argc, char** argv )
referencePointTest<SCIterator>();
referencePointTest<HCPIterator>();
rangeBasedTest<SCGrid>();
rangeBasedTest<HCPGrid>();
return EXIT_SUCCESS;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment