Skip to content

Commit

Permalink
upgrade to C++20
Browse files Browse the repository at this point in the history
better consting to pick correct generate fn

simple Diags in tests - silence hidden overload

test
  • Loading branch information
PhilipDeegan committed Jan 4, 2024
1 parent 618c8f6 commit 0076fc5
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 83 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ include("${PHARE_PROJECT_DIR}/res/cmake/coverage.cmake")
include("${PHARE_PROJECT_DIR}/res/cmake/dep.cmake")
include("${PHARE_PROJECT_DIR}/res/cmake/cppcheck.cmake")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#### post deps config
Expand Down
4 changes: 3 additions & 1 deletion res/cmake/def.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ set (PHARE_FLAGS ${PHARE_FLAGS} )
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set (PHARE_FLAGS ${PHARE_FLAGS} )
else() # !Clang
set (PHARE_FLAGS ${PHARE_FLAGS} --param=min-pagesize=0 )
# --param=min-pagesize=0 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105523
# --param=evrp-mode=legacy https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329
set (PHARE_FLAGS ${PHARE_FLAGS} --param=min-pagesize=0 --param=evrp-mode=legacy )
endif() # clang

set (PHARE_LINK_FLAGS )
Expand Down
2 changes: 1 addition & 1 deletion src/amr/data/particles/particles_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ namespace amr
using Packer = core::ParticlePacker<dim>;

auto getParticles = [&](std::string const name, auto& particles) {
auto const keys_exist = core::generate(
std::array<bool, Packer::n_keys> keys_exist = core::generate(
[&](auto const& key) { return restart_db->keyExists(name + "_" + key); },
Packer::keys());

Expand Down
4 changes: 2 additions & 2 deletions src/core/data/grid/gridlayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ namespace core
/**
* @brief the number of ghost nodes on each side of the mesh for a given centering
*/
NO_DISCARD auto static constexpr nbrGhosts(
NO_DISCARD std::uint32_t static constexpr nbrGhosts(
QtyCentering /*centering*/ = QtyCentering::primal)
{ // Both dual and primal ghosts are the same!
static_assert(nbrDualGhosts_() == nbrPrimalGhosts_());
Expand All @@ -569,7 +569,7 @@ namespace core


template<typename Centering, Centering centering>
NO_DISCARD auto static constexpr nbrGhosts()
NO_DISCARD std::uint32_t static constexpr nbrGhosts()
{
if constexpr (centering == QtyCentering::dual)
return nbrDualGhosts_();
Expand Down
5 changes: 4 additions & 1 deletion src/core/data/particles/particle_packer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ template<std::size_t dim>
class ParticlePacker
{
public:
static constexpr std::size_t n_keys = 5;

ParticlePacker(ParticleArray<dim> const& particles)
: particles_{particles}
{
Expand Down Expand Up @@ -63,7 +65,8 @@ class ParticlePacker
private:
ParticleArray<dim> const& particles_;
std::size_t it_ = 0;
static inline std::array<std::string, 5> keys_{"weight", "charge", "iCell", "delta", "v"};
static inline const std::array<std::string, n_keys> keys_{"weight", "charge", "iCell", "delta",
"v"};
};


Expand Down
7 changes: 4 additions & 3 deletions src/core/utilities/box/box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@ struct Box

Box() = default;

constexpr Box(std::array<Type, dim> _lower, std::array<Type, dim> _upper)
constexpr Box(std::array<Type, dim> const& _lower, std::array<Type, dim> const& _upper)
: lower{_lower}
, upper{_upper}
{
}

template<typename T, std::size_t s>
Box(Point<T, s> _lower, Point<T, s> _upper)
Box(Point<T, s> const& _lower, Point<T, s> const& _upper)
: lower{_lower}
, upper{_upper}
{
}

NO_DISCARD bool operator==(Box const& box) const
template<typename T2>
NO_DISCARD bool operator==(Box<T2, dim> const& box) const
{
return box.lower == lower && box.upper == upper;
}
Expand Down
9 changes: 5 additions & 4 deletions src/core/utilities/point/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ namespace core
}


constexpr Point(std::array<Type, dim> coords)
: r{std::move(coords)}
constexpr Point(std::array<Type, dim> const& coords)
: r{coords}
{
}

Expand All @@ -69,15 +69,16 @@ namespace core
NO_DISCARD auto const& operator[](std::size_t i) const { return r[i]; }


NO_DISCARD bool operator==(Point const& p) const
template<typename T2>
NO_DISCARD bool operator==(Point<T2, dim> const& p) const
{
bool areEqual = true;
for (std::size_t i = 0; i < dim; ++i)
{
static_assert(std::is_integral_v<Type>,
"this function is only valid for integral type of Point");

areEqual &= ((*this)[i] == p[i]);
areEqual &= int_equals((*this)[i], p[i]); // handles signed differences
}
return areEqual;
}
Expand Down
36 changes: 32 additions & 4 deletions src/core/utilities/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ NO_DISCARD auto generate(F&& f, std::size_t count)


template<typename F, typename Container>
NO_DISCARD auto generate(F&& f, Container& container)
NO_DISCARD auto generate(F&& f, Container const& container)
{
using T = typename Container::value_type;
using value_type = std::decay_t<std::result_of_t<F&(T&)>>;
Expand All @@ -300,13 +300,13 @@ NO_DISCARD auto generate(F&& f, std::vector<T>&& v)
}

template<std::size_t Idx, typename F, typename Type, std::size_t Size>
NO_DISCARD auto constexpr generate_array__(F& f, std::array<Type, Size>& arr)
NO_DISCARD auto constexpr generate_array__(F& f, std::array<Type, Size> const& arr)
{
return f(arr[Idx]);
}

template<typename Type, std::size_t Size, typename F, std::size_t... Is>
NO_DISCARD auto constexpr generate_array_(F& f, std::array<Type, Size>& arr,
NO_DISCARD auto constexpr generate_array_(F& f, std::array<Type, Size> const& arr,
std::integer_sequence<std::size_t, Is...>)
{
return std::array{generate_array__<Is>(f, arr)...};
Expand Down Expand Up @@ -336,13 +336,41 @@ NO_DISCARD auto any(Container const& container, Fn fn = to_bool)
return std::any_of(container.begin(), container.end(), fn);
}


template<typename Container, typename Fn = decltype(to_bool)>
NO_DISCARD auto none(Container const& container, Fn fn = to_bool)
{
return std::none_of(container.begin(), container.end(), fn);
}


template<typename SignedInt, typename UnsignedInt>
bool diff_sign_int_equals(SignedInt const& i0, UnsignedInt const& i1)
{
static_assert(std::is_unsigned_v<UnsignedInt>);
static_assert(std::is_signed_v<SignedInt>);
static_assert(sizeof(UnsignedInt) >= sizeof(SignedInt), "Bad int comparison!");
if (i0 < 0)
return false;
return static_cast<UnsignedInt>(i0) == i1;
}


template<typename Int0, typename Int1>
bool int_equals(Int0 const& i0, Int1 const& i1)
{
if constexpr (std::is_same_v<Int0, Int1>)
return i0 == i1;
else
{
if constexpr (std::is_unsigned_v<Int0> and std::is_signed_v<Int1>)
return diff_sign_int_equals(i1, i0);
if constexpr (std::is_unsigned_v<Int1> and std::is_signed_v<Int0>)
return diff_sign_int_equals(i0, i1);
}
// reaching here == compiler error
}


auto inline float_equals(float const& a, float const& b, float diff = 1e-6)
{
return std::abs(a - b) < diff;
Expand Down
5 changes: 5 additions & 0 deletions src/simulator/simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class ISimulator
virtual std::string to_str() = 0;

virtual ~ISimulator() {}


virtual bool dump(double timestamp, double timestep) { return false; } // overriding optional
};

Expand Down Expand Up @@ -106,6 +108,9 @@ class Simulator : public ISimulator

using Integrator = PHARE::amr::Integrator<dimension>;

protected:
// provided to force flush for diags
void reset_dman() { this->dMan.reset(); }

private:
auto find_model(std::string name);
Expand Down
10 changes: 6 additions & 4 deletions tests/core/data/gridlayout/gridlayout_amr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ TEST(GridLayout, canTransformAnAMRIndexIntoALocalIndex)

TEST(GridLayout, canTransformAnAMRBoxIntoALocalBox)
{
GridLayout<GridLayoutImplYee<1, 1>> layout{{0.1}, {50u}, {{0.}}, Box{Point{50}, Point{99}}};
std::size_t constexpr static dim = 1;

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable dim is not used.
GridLayout<GridLayoutImplYee<dim, 1>> layout{{0.1}, {50u}, {{0.}}, Box{Point{50}, Point{99}}};

int nGhosts = layout.nbrGhosts(QtyCentering::dual);
auto AMRBox = Box{Point{55}, Point{65}};
auto expectedLocalBox = Box<std::uint32_t, 1>{Point{nGhosts + 5}, Point{nGhosts + 15}};
std::uint32_t nGhosts = layout.nbrGhosts(QtyCentering::dual);
auto AMRBox = Box<int, dim>{Point<int, dim>{55}, Point<int, dim>{65}};
auto expectedLocalBox = Box<std::uint32_t, dim>{Point<std::uint32_t, dim>{nGhosts + 5},
Point<std::uint32_t, dim>{nGhosts + 15}};

EXPECT_EQ(expectedLocalBox, layout.AMRToLocal(AMRBox));
}
93 changes: 51 additions & 42 deletions tests/core/utilities/box/test_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,54 +197,63 @@ TEST(BoxIterator, iterates)
Box<int, 2> b2{{1, 4}, {10, 12}};
Box<int, 3> b3{{1, 4, 9}, {10, 12, 24}};

auto expected = Point{2};
auto actual = std::begin(b1);
EXPECT_EQ(expected, *(++actual));

auto const cexpected = Point{2};
auto cactual = std::begin(cb1);
EXPECT_EQ(cexpected, *(++cactual));

auto expected2 = Point{1, 5};
auto actual2 = std::begin(b2);
EXPECT_EQ(expected2, *(++actual2));

auto expected3 = Point{1, 4, 10};
auto actual3 = std::begin(b3);
EXPECT_EQ(expected3, *(++actual3));

Box<int, 2> small{{2, 1}, {3, 2}};
auto it = std::begin(small);
++it;
expected = Point{2, 2};
EXPECT_EQ(expected, *it);
++it;
expected = Point{3, 1};
EXPECT_EQ(expected, *it);

auto dummy1 = Point<int, 1>{};
for (auto const& point : b1)
{
dummy1 = point;
auto expected = Point{2};
auto actual = std::begin(b1);
EXPECT_EQ(expected, *(++actual));
}
auto expected1 = Point{10};
EXPECT_EQ(expected1, dummy1);

auto dummy = Point<int, 2>{};
for (auto const& point : b2)
{
dummy = point;
auto const cexpected = Point{2};
auto cactual = std::begin(cb1);
EXPECT_EQ(cexpected, *(++cactual));
}
{
auto expected2 = Point{1, 5};
auto actual2 = std::begin(b2);
EXPECT_EQ(expected2, *(++actual2));
}
{
auto expected3 = Point{1, 4, 10};
auto actual3 = std::begin(b3);
EXPECT_EQ(expected3, *(++actual3));
}
{
Box<int, 2> small{{2, 1}, {3, 2}};
auto it = std::begin(small);
++it;
auto expected = Point{2, 2};
EXPECT_EQ(expected, *it);
++it;
expected = Point{3, 1};
EXPECT_EQ(expected, *it);
}
{
auto dummy1 = Point<int, 1>{};
for (auto const& point : b1)
{
dummy1 = point;
}
auto expected1 = Point{10};
EXPECT_EQ(expected1, dummy1);
}
{
auto dummy = Point<int, 2>{};
for (auto const& point : b2)
{
dummy = point;
}
auto expected = Point{10, 12};
EXPECT_EQ(expected, dummy);
}
expected = Point{10, 12};
EXPECT_EQ(expected, dummy);

auto dummy3 = Point<int, 3>{};
for (auto const& point : b3)
{
dummy3 = point;
auto dummy3 = Point<int, 3>{};
for (auto const& point : b3)
{
dummy3 = point;
}
auto expected = Point{10, 12, 24};
EXPECT_EQ(expected, dummy3);
}
expected = Point{10, 12, 24};
EXPECT_EQ(expected, dummy3);
}


Expand Down
5 changes: 5 additions & 0 deletions tests/diagnostic/test_diagnostics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ struct Hi5Diagnostic
return Writer_t::getFullPatchPath(timestamp, level, patch);
}

void dump(double current_timestamp = 0, double current_timestep = 1)
{
dMan.dump(current_timestamp, current_timestep);
}

Hierarchy& hierarchy_;
HybridModel& model_;
std::string out_;
Expand Down
10 changes: 5 additions & 5 deletions tests/diagnostic/test_diagnostics.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void fluid_test(Simulator&& sim, std::string out_dir)
.addDiagDict(hi5.fluid("/ions/pop/protons/density"))
.addDiagDict(hi5.fluid("/ions/pop/protons/flux"))
.addDiagDict(hi5.fluid("/ions/pop/protons/momentum_tensor"));
sim.dump(hi5.dMan);
hi5.dump();
}

Hi5Diagnostic<Hierarchy, HybridModel> hi5{hierarchy, hybridModel, out_dir,
Expand All @@ -42,7 +42,7 @@ void electromag_test(Simulator&& sim, std::string out_dir)
{ // scoped to destruct after dump
Hi5Diagnostic<Hierarchy, HybridModel> hi5{hierarchy, hybridModel, out_dir, NEW_HI5_FILE};
hi5.dMan.addDiagDict(hi5.electromag("/EM_B")).addDiagDict(hi5.electromag("/EM_E"));
sim.dump(hi5.dMan);
hi5.dump();
}

Hi5Diagnostic<Hierarchy, HybridModel> hi5{hierarchy, hybridModel, out_dir,
Expand All @@ -68,7 +68,7 @@ void particles_test(Simulator&& sim, std::string out_dir)
.addDiagDict(hi5.particles("/ions/pop/protons/domain"))
.addDiagDict(hi5.particles("/ions/pop/protons/levelGhost"))
.addDiagDict(hi5.particles("/ions/pop/protons/patchGhost"));
sim.dump(hi5.dMan);
hi5.dump();
}

Hi5Diagnostic<Hierarchy, HybridModel> hi5{hierarchy, hybridModel, out_dir,
Expand All @@ -83,11 +83,11 @@ void allFromPython_test(Simulator&& sim, std::string out_dir)
using HybridModel = typename Simulator::HybridModel;
using Hierarchy = typename Simulator::Hierarchy;

sim.dump(*sim.dMan);
sim.dump(/*timestamp= */ 0, /*timestep= */ 1);
// flush h5files, killing the DiagnosticsManager killes the H5TypeWriter shared_ptrs internally,
// and forces closed any open h5files, flushing any remaining contents, which we use below in
// this test
sim.dMan.reset();
sim.reset_dman();

auto& hybridModel = *sim.getHybridModel();
auto& hierarchy = *sim.hierarchy;
Expand Down
Loading

0 comments on commit 0076fc5

Please sign in to comment.