24#ifdef ESPRESSO_ELECTROSTATICS
31#include "communication.hpp"
34#include "system/System.hpp"
39#include <boost/accumulators/accumulators.hpp>
40#include <boost/accumulators/statistics/sum_kahan.hpp>
41#include <boost/mpi/collectives/broadcast.hpp>
42#include <boost/mpi/collectives/gather.hpp>
60 impl = std::make_unique<Implementation>();
66 std::visit([](
auto const &ptr) { ptr->sanity_checks(); }, *
impl->solver);
85 std::visit([](
auto &ptr) { ptr->on_node_grid_change(); }, *
impl->solver);
105 auto operator()(std::shared_ptr<CoulombP3M>
const &actor)
const {
106 return actor->long_range_pressure();
110 auto operator()(std::shared_ptr<DebyeHueckel>
const &)
const {
114 auto operator()(std::shared_ptr<ReactionField>
const &)
const {
118 template <
typename T>
120 auto operator()(std::shared_ptr<T>
const &)
const {
122 <<
"electrostatics method " << Utils::demangle<T>();
136 auto operator()(std::shared_ptr<CoulombP3M>
const &actor)
const {
137 return actor->p3m_params.r_cut;
140 operator()(std::shared_ptr<ElectrostaticLayerCorrection>
const &actor)
const {
141 return std::max(actor->elc.space_layer,
142 std::visit(*
this, actor->base_solver));
146 auto operator()(std::shared_ptr<CoulombMMM1D>
const &)
const {
147 return std::numeric_limits<double>::infinity();
150#ifdef ESPRESSO_SCAFACOS
151 auto operator()(std::shared_ptr<CoulombScafacos>
const &actor)
const {
152 return actor->get_r_cut();
155 auto operator()(std::shared_ptr<ReactionField>
const &actor)
const {
158 auto operator()(std::shared_ptr<DebyeHueckel>
const &actor)
const {
171 template <
typename T>
void operator()(std::shared_ptr<T>
const &)
const {}
174 void operator()(std::shared_ptr<CoulombP3M>
const &actor)
const {
175 actor->count_charged_particles();
178 operator()(std::shared_ptr<ElectrostaticLayerCorrection>
const &actor)
const {
179 std::visit(*
this, actor->base_solver);
194 template <
class Solver>
195 void operator()(std::shared_ptr<Solver>
const &actor)
const {
196 actor->add_long_range_forces();
200 void operator()(std::shared_ptr<CoulombMMM1D>
const &)
const {}
202 void operator()(std::shared_ptr<DebyeHueckel>
const &)
const {}
203 void operator()(std::shared_ptr<ReactionField>
const &)
const {}
207 template <
class Solver>
208 auto operator()(std::shared_ptr<Solver>
const &actor)
const {
209 return actor->long_range_energy();
213 auto operator()(std::shared_ptr<CoulombMMM1D>
const &)
const {
return 0.; }
215 auto operator()(std::shared_ptr<DebyeHueckel>
const &)
const {
return 0.; }
216 auto operator()(std::shared_ptr<ReactionField>
const &)
const {
return 0.; }
234 using namespace boost::accumulators;
235 using KahanSum = accumulator_set<double, features<tag::sum_kahan>>;
238 auto q_min = std::numeric_limits<double>::infinity();
240 for (
auto const q : charges) {
243 q_min = std::min(q_min, std::abs(q));
247 return std::abs(sum_kahan(q_sum)) / q_min;
251 double relative_tolerance) {
253 std::vector<double> local_charges;
255 local_charges.push_back(p.q());
257 std::vector<std::vector<double>> node_charges;
258 boost::mpi::gather(
comm_cart, local_charges, node_charges, 0);
261 auto excess_ratio = 0.;
263 auto charges = std::move(local_charges);
264 for (
auto it = ++node_charges.begin(); it != node_charges.end(); ++it) {
265 charges.insert(charges.end(), it->begin(), it->end());
269 boost::mpi::broadcast(
comm_cart, excess_ratio, 0);
271 if (excess_ratio >= relative_tolerance) {
272 std::ostringstream serializer;
273 serializer << std::scientific << std::setprecision(4);
274 serializer << excess_ratio;
275 throw std::runtime_error(
276 "The system is not charge neutral. Please neutralize the system "
277 "before adding a new actor by adding the corresponding counterions "
278 "to the system. Alternatively you can turn off the electroneutrality "
279 "check by supplying check_neutrality=False when creating the actor. "
280 "In this case you may be simulating a non-neutral system which will "
281 "affect physical observables like e.g. the pressure, the chemical "
282 "potentials of charged species or potential energies of the system. "
283 "Since simulations of non charge neutral systems are special please "
284 "make sure you know what you are doing. The relative charge excess "
Vector implementation and trait types for boost qvm interoperability.
std::shared_ptr< CellStructure > cell_structure
boost::mpi::communicator comm_cart
The communicator.
int this_node
The number of this node.
constexpr double inactive_cutoff
Special cutoff value for an inactive interaction.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
#define runtimeWarningMsg()
ICC is a method that allows to take into account the influence of arbitrarily shaped dielectric inter...
void check_charge_neutrality(System::System const &system, double relative_tolerance)
Check if the system is charge-neutral.
static auto calc_charge_excess_ratio(std::vector< double > const &charges)
Compute the net charge rescaled by the smallest non-zero charge.
void operator()(std::shared_ptr< T > const &) const
void operator()(std::shared_ptr< ElectrostaticLayerCorrection > const &actor) const
void operator()(std::shared_ptr< CoulombP3M > const &actor) const
auto operator()(std::shared_ptr< ReactionField > const &) const
auto operator()(std::shared_ptr< Solver > const &actor) const
auto operator()(std::shared_ptr< CoulombMMM1D > const &) const
auto operator()(std::shared_ptr< DebyeHueckel > const &) const
void operator()(std::shared_ptr< ReactionField > const &) const
void operator()(std::shared_ptr< CoulombMMM1D > const &) const
void operator()(std::shared_ptr< Solver > const &actor) const
void operator()(std::shared_ptr< DebyeHueckel > const &) const
auto operator()(std::shared_ptr< ReactionField > const &) const
auto operator()(std::shared_ptr< DebyeHueckel > const &) const
auto operator()(std::shared_ptr< CoulombP3M > const &actor) const
auto operator()(std::shared_ptr< CoulombP3M > const &actor) const
auto operator()(std::shared_ptr< CoulombMMM1D > const &) const
auto operator()(std::shared_ptr< ElectrostaticLayerCorrection > const &actor) const
auto operator()(std::shared_ptr< ReactionField > const &actor) const
auto operator()(std::shared_ptr< DebyeHueckel > const &actor) const
auto operator()(std::shared_ptr< CoulombScafacos > const &actor) const
Utils::Vector9d calc_pressure_long_range() const
double calc_energy_long_range() const
void on_observable_calc()
void sanity_checks() const
void on_cell_structure_change()
void on_periodicity_change()
void calc_long_range_force() const
void on_node_grid_change()
std::unique_ptr< Implementation > impl
Pointer-to-implementation.
bool reinit_on_observable_calc
Whether to reinitialize the solver on observable calculation.
The electrostatic method supports pressure calculation.
void visit_try_catch(Visitor &&visitor, Variant &actor)
Run a kernel on a variant and queue errors.