ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
charge_neutrality.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef UTILS_CHECKS_CHARGE_NEUTRALITY_HPP
20#define UTILS_CHECKS_CHARGE_NEUTRALITY_HPP
21
22#include <boost/accumulators/accumulators.hpp>
23#include <boost/accumulators/statistics/sum_kahan.hpp>
24
25#include <cmath>
26#include <limits>
27#include <stdexcept>
28
29namespace Utils {
30template <typename ParticleRange>
32 using namespace boost::accumulators;
33 using KahanSum = accumulator_set<double, features<tag::sum_kahan>>;
34
35 KahanSum q_sum;
36 auto q_min = std::numeric_limits<double>::infinity();
37 constexpr auto relative_tolerance = 2e-12;
38
39 for (auto const &p : prange) {
40 auto const &q = p.q();
41
42 if (q != 0.0) {
43 q_sum(q);
44 q_min = std::min(q_min, std::abs(q));
45 }
46 }
47
48 auto const excess_ratio = std::abs(sum_kahan(q_sum)) / q_min;
49
50 return excess_ratio <= relative_tolerance;
51}
52} // namespace Utils
53#endif
A range of particles.
bool check_charge_neutrality(ParticleRange &prange)