Skip to content

Commit

Permalink
core: Use accumulators
Browse files Browse the repository at this point in the history
  • Loading branch information
jngrad committed Jun 26, 2020
1 parent 25b4b30 commit 5a7f751
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/core/reaction_ensemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "statistics.hpp"

#include <utils/constants.hpp>
#include <utils/contains.hpp>
#include <utils/index.hpp>

#include <cstdio>
Expand Down Expand Up @@ -711,7 +712,7 @@ bool ReactionAlgorithm::do_global_mc_move_for_particles_of_type(
int random_index_in_type_map = i_random(number_of_particles_with_type(type));
int p_id = get_random_p_id(type, random_index_in_type_map);
for (int i = 0; i < particle_number_of_type_to_be_changed; i++) {
while (is_in_vector(p_id, p_id_s_changed_particles)) {
while (Utils::contains(p_id_s_changed_particles, p_id)) {
random_index_in_type_map = i_random(number_of_particles_with_type(type));
p_id = get_random_p_id(
type, random_index_in_type_map); // check whether you already touched
Expand Down
34 changes: 16 additions & 18 deletions src/core/reaction_ensemble.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
#include <map>
#include <string>

#include <boost/range/algorithm.hpp>

namespace ReactionEnsemble {

struct SingleReaction {
Expand Down Expand Up @@ -426,26 +424,26 @@ double average_list_of_allowed_entries(const std::vector<T> &rng) {
}

/**
* Checks whether a number is in an array.
*/
inline bool is_in_vector(int value, std::vector<int> const &rng) {
return boost::range::find(rng, value) != rng.end();
}

/**
* Finds the minimum non negative value in the provided double array and returns
* Finds the minimum non negative value in the provided range and returns
* this value.
*/
inline double find_minimum_non_negative_value(std::vector<double> const &rng) {
double minimum = rng[0];
for (auto &val : rng) {
if (minimum < 0)
minimum = val; // think of negative histogram values that indicate not
// allowed energies in the case of an energy observable
if (val < minimum && val >= 0)
minimum = val;
if (rng.size() == 0)
throw std::runtime_error("range is empty\n");
// think of negative histogram values that indicate not
// allowed energies in the case of an energy observable
auto const it = std::min_element(rng.begin(), rng.end(),
[](double const &a, double const &b) {
if (a <= 0)
return false;
if (b <= 0)
return true;
return a < b;
});
if (it == rng.end() or *it < 0) {
return rng[rng.size() - 1];
}
return minimum;
return *it;
}

} // namespace ReactionEnsemble
Expand Down
12 changes: 3 additions & 9 deletions src/core/unit_tests/reaction_ensemble_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@ BOOST_AUTO_TEST_CASE(find_minimum_non_negative_value_test) {
BOOST_CHECK_EQUAL(find_minimum_non_negative_value({3, 2, 1}), 1);
BOOST_CHECK_EQUAL(find_minimum_non_negative_value({-1, 2, 3}), 2);
BOOST_CHECK_EQUAL(find_minimum_non_negative_value({-1, -2, -3}), -3);
}

BOOST_AUTO_TEST_CASE(is_in_vector_test) {
using namespace ReactionEnsemble;

BOOST_CHECK(is_in_vector(1, {1, 2, 3}));
BOOST_CHECK(is_in_vector(1, {3, 2, 1}));
BOOST_CHECK(not is_in_vector(1, {2, 3}));
BOOST_CHECK(not is_in_vector(1, {2, -1}));
BOOST_CHECK_THROW(find_minimum_non_negative_value({}), std::runtime_error);
}

BOOST_AUTO_TEST_CASE(average_list_of_allowed_entries_test) {
Expand All @@ -55,6 +47,8 @@ BOOST_AUTO_TEST_CASE(average_list_of_allowed_entries_test) {
average_list_of_allowed_entries(std::vector<double>{1.5, -3.}), 1.5, tol);
BOOST_CHECK_CLOSE(
average_list_of_allowed_entries(std::vector<int>{-1, -2, -3}), 0.0, tol);
BOOST_CHECK_CLOSE(average_list_of_allowed_entries(std::vector<double>{}), 0.0,
tol);
}

BOOST_AUTO_TEST_CASE(factorial_Ni0_divided_by_factorial_Ni0_plus_nu_i_test) {
Expand Down

0 comments on commit 5a7f751

Please sign in to comment.