Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial migration from range-v3 to std::ranges #452

Merged
merged 19 commits into from
Jul 4, 2024
Prev Previous commit
Next Next commit
Update
  • Loading branch information
mlund committed Jul 3, 2024
commit 9ce83fc1bef742085d9058974efc03b08659c7cf
17 changes: 8 additions & 9 deletions src/aux/arange.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ OTHER DEALINGS IN THE SOFTWARE.
#pragma once
#include <cmath>
#include <concepts>
#include <range/v3/view/iota.hpp>
#include <range/v3/view/transform.hpp>
#include <ranges>
#include <doctest/doctest.h>

namespace Faunus {
Expand Down Expand Up @@ -54,8 +53,8 @@ template <typename T> constexpr auto arange(const T start, const T stop, const T
using int_type = typename std::conditional<std::is_integral_v<T>, T, int>::type;
const auto length =
static_cast<int_type>(std::ceil((stop - start) / static_cast<float_type>(step)));
return ranges::cpp20::views::iota(int_type(0), length) |
ranges::cpp20::views::transform(
return std::views::iota(int_type(0), length) |
std::views::transform(
[start, step](auto i) -> T { return start + static_cast<T>(i) * step; });
}

Expand All @@ -65,7 +64,7 @@ TEST_CASE("[Faunus] arange")
{
auto r = arange(4.0, 10.0, 1.0); // --> 4 5 6 7 8 9
auto pos = r.begin();
CHECK_EQ(ranges::size(r), 6);
CHECK_EQ(std::ranges::size(r), 6);
CHECK_EQ(*(pos++), doctest::Approx(4.0));
CHECK_EQ(*(pos++), doctest::Approx(5.0));
CHECK_EQ(*(pos++), doctest::Approx(6.0));
Expand All @@ -77,7 +76,7 @@ TEST_CASE("[Faunus] arange")
{
auto r = arange(4, 10, 1); // --> 4 5 6 7 8 9
auto pos = r.begin();
CHECK_EQ(ranges::size(r), 6);
CHECK_EQ(std::ranges::size(r), 6);
CHECK_EQ(*(pos++), 4);
CHECK_EQ(*(pos++), 5);
CHECK_EQ(*(pos++), 6);
Expand All @@ -89,7 +88,7 @@ TEST_CASE("[Faunus] arange")
{
auto r = arange(4.0, 20.0, 3.0); // --> 4 7 10 13 16 19
auto pos = r.begin();
CHECK_EQ(ranges::size(r), 6);
CHECK_EQ(std::ranges::size(r), 6);
CHECK_EQ(*(pos++), doctest::Approx(4.0));
CHECK_EQ(*(pos++), doctest::Approx(7.0));
CHECK_EQ(*(pos++), doctest::Approx(10.0));
Expand All @@ -101,7 +100,7 @@ TEST_CASE("[Faunus] arange")
{
auto r = arange(4, 20, 3); // --> 4 7 10 13 16 19
auto pos = r.begin();
CHECK_EQ(ranges::size(r), 6);
CHECK_EQ(std::ranges::size(r), 6);
CHECK_EQ(*(pos++), 4);
CHECK_EQ(*(pos++), 7);
CHECK_EQ(*(pos++), 10);
Expand All @@ -114,7 +113,7 @@ TEST_CASE("[Faunus] arange")
{
auto r = arange(-1.0, 1.0, 0.5); // --> -1 -0.5 0 0.5
auto pos = r.begin();
CHECK_EQ(ranges::size(r), 4);
CHECK_EQ(std::ranges::size(r), 4);
CHECK_EQ(*(pos++), doctest::Approx(-1.0));
CHECK_EQ(*(pos++), doctest::Approx(-0.5));
CHECK_EQ(*(pos++), doctest::Approx(0.0));
Expand Down
26 changes: 12 additions & 14 deletions src/energy.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
#include "aux/pairmatrix.h"
#include "smart_montecarlo.h"
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/iota.hpp>
#include <range/v3/view/subrange.hpp>
#include <range/v3/algorithm/any_of.hpp>
#include <Eigen/Dense>
#include <spdlog/spdlog.h>
#include <ranges>
#include <numeric>
#include <algorithm>
#include <concepts>
Expand Down Expand Up @@ -174,7 +172,7 @@ class EwaldPolicyBase
auto is_partially_inactive = [](const Group& group) {
return group.size() != group.capacity();
};
if (ranges::cpp20::any_of(groups, is_partially_inactive)) {
if (std::ranges::any_of(groups, is_partially_inactive)) {
throw std::runtime_error("Eigen optimized Ewald not available with inactive groups");
}
auto first_particle = groups.front().begin();
Expand All @@ -191,7 +189,7 @@ class EwaldPolicyBase
auto is_partially_inactive = [](const Group& group) {
return group.size() != group.capacity();
};
if (ranges::cpp20::any_of(groups, is_partially_inactive)) {
if (std::ranges::any_of(groups, is_partially_inactive)) {
throw std::runtime_error("Eigen optimized Ewald not available with inactive groups");
}
auto first_particle = groups.front().begin();
Expand Down Expand Up @@ -342,7 +340,7 @@ class Bonded : public EnergyTerm
double sumBondEnergy(const BondVector& bonds) const; //!< sum energy in vector of BondData
double internalGroupEnergy(const Change::GroupChange& changed); //!< Energy from internal bonds
double sumEnergy(const BondVector& bonds,
const ranges::cpp20::range auto& particle_indices) const;
const std::ranges::range auto& particle_indices) const;
void updateInternalBonds(); //!< finds and adds all intra-molecular bonds of active molecules

public:
Expand All @@ -363,15 +361,15 @@ class Bonded : public EnergyTerm
* to simplistic search which scales as number_of_bonds x number_of_moved_particles
*/
double Bonded::sumEnergy(const Bonded::BondVector& bonds,
const ranges::cpp20::range auto& particle_indices) const
const std::ranges::range auto& particle_indices) const
{
assert(std::is_sorted(particle_indices.begin(), particle_indices.end()));

auto index_is_included = [&](auto index) {
return std::binary_search(particle_indices.begin(), particle_indices.end(), index);
};
auto affected_bonds = bonds | ranges::cpp20::views::filter([&](const auto& bond) {
return ranges::cpp20::any_of(bond->indices, index_is_included);
auto affected_bonds = bonds | std::views::filter([&](const auto& bond) {
return std::ranges::any_of(bond->indices, index_is_included);
});
auto bond_energy = [dist = spc.geometry.getDistanceFunc()](const auto& bond) {
return bond->energyFunc(dist);
Expand All @@ -388,11 +386,11 @@ double Bonded::sumEnergy(const Bonded::BondVector& bonds,
* @param range an original set of integers (must be sorted)
* @return a set of ints complementary to the original set
*/
auto indexComplement(std::integral auto size, const ranges::cpp20::range auto& range)
auto indexComplement(std::integral auto size, const std::ranges::range auto& range)
{
namespace rv = ranges::cpp20::views;
return rv::iota(0, static_cast<int>(size)) |
rv::filter([&](auto i) { return !std::binary_search(range.begin(), range.end(), i); });
using namespace std::views;
return iota(0, static_cast<int>(size)) |
filter([&](auto i) { return !std::binary_search(range.begin(), range.end(), i); });
}

/**
Expand Down Expand Up @@ -1400,7 +1398,7 @@ template <typename TPolicy> class GroupPairing
const auto fixed = indexComplement(spc.groups.size(), moved) |
ranges::to<std::vector>; // index of static groups
auto filter_active = [](int size) {
return ranges::views::filter([size](const auto i) { return i < size; });
return std::views::filter([size](const auto i) { return i < size; });
};

// loop over all changed groups
Expand Down