24#ifdef ESPRESSO_ELECTROSTATICS
32#include "communication.hpp"
35#include "system/System.hpp"
40#include <boost/accumulators/accumulators.hpp>
41#include <boost/accumulators/statistics/sum_kahan.hpp>
42#include <boost/mpi/collectives/broadcast.hpp>
43#include <boost/mpi/collectives/gather.hpp>
61 impl = std::make_unique<Implementation>();
69 std::visit([](
auto const &ptr) { ptr->sanity_checks(); }, *
impl->solver);
88 std::visit([](
auto &ptr) { ptr->on_node_grid_change(); }, *
impl->solver);
108 : m_particles{particles} {}
111 auto operator()(std::shared_ptr<CoulombP3M>
const &actor)
const {
112 return actor->long_range_pressure(m_particles);
116 auto operator()(std::shared_ptr<DebyeHueckel>
const &)
const {
120 auto operator()(std::shared_ptr<ReactionField>
const &)
const {
124 template <
typename T>
126 auto operator()(std::shared_ptr<T>
const &)
const {
128 <<
"electrostatics method " << Utils::demangle<T>();
146 auto operator()(std::shared_ptr<CoulombP3M>
const &actor)
const {
147 return actor->p3m_params.r_cut;
150 operator()(std::shared_ptr<ElectrostaticLayerCorrection>
const &actor)
const {
151 return std::max(actor->elc.space_layer,
152 std::visit(*
this, actor->base_solver));
156 auto operator()(std::shared_ptr<CoulombMMM1D>
const &)
const {
157 return std::numeric_limits<double>::infinity();
160#ifdef ESPRESSO_SCAFACOS
161 auto operator()(std::shared_ptr<CoulombScafacos>
const &actor)
const {
162 return actor->get_r_cut();
165 auto operator()(std::shared_ptr<ReactionField>
const &actor)
const {
168 auto operator()(std::shared_ptr<DebyeHueckel>
const &actor)
const {
181 template <
typename T>
void operator()(std::shared_ptr<T>
const &)
const {}
184 void operator()(std::shared_ptr<CoulombP3M>
const &actor)
const {
185 actor->count_charged_particles();
188 operator()(std::shared_ptr<ElectrostaticLayerCorrection>
const &actor)
const {
189 std::visit(*
this, actor->base_solver);
205 : m_particles(particles) {}
208 void operator()(std::shared_ptr<CoulombP3M>
const &actor)
const {
209 actor->add_long_range_forces(m_particles);
212 operator()(std::shared_ptr<ElectrostaticLayerCorrection>
const &actor)
const {
213 actor->add_long_range_forces(m_particles);
216#ifdef ESPRESSO_SCAFACOS
217 void operator()(std::shared_ptr<CoulombScafacos>
const &actor)
const {
218 actor->add_long_range_forces();
223 void operator()(std::shared_ptr<CoulombMMM1D>
const &)
const {}
225 void operator()(std::shared_ptr<DebyeHueckel>
const &)
const {}
226 void operator()(std::shared_ptr<ReactionField>
const &)
const {}
234 : m_particles(particles) {}
237 auto operator()(std::shared_ptr<CoulombP3M>
const &actor)
const {
238 return actor->long_range_energy(m_particles);
241 operator()(std::shared_ptr<ElectrostaticLayerCorrection>
const &actor)
const {
242 return actor->long_range_energy(m_particles);
245#ifdef ESPRESSO_SCAFACOS
246 auto operator()(std::shared_ptr<CoulombScafacos>
const &actor)
const {
247 return actor->long_range_energy();
252 auto operator()(std::shared_ptr<CoulombMMM1D>
const &)
const {
return 0.; }
254 auto operator()(std::shared_ptr<DebyeHueckel>
const &)
const {
return 0.; }
255 auto operator()(std::shared_ptr<ReactionField>
const &)
const {
return 0.; }
276 using namespace boost::accumulators;
277 using KahanSum = accumulator_set<double, features<tag::sum_kahan>>;
280 auto q_min = std::numeric_limits<double>::infinity();
282 for (
auto const q : charges) {
285 q_min = std::min(q_min, std::abs(q));
289 return std::abs(sum_kahan(q_sum)) / q_min;
293 double relative_tolerance) {
295 std::vector<double> local_charges;
297 local_charges.push_back(p.q());
299 std::vector<std::vector<double>> node_charges;
300 boost::mpi::gather(
comm_cart, local_charges, node_charges, 0);
303 auto excess_ratio = 0.;
305 auto charges = std::move(local_charges);
306 for (
auto it = ++node_charges.begin(); it != node_charges.end(); ++it) {
307 charges.insert(charges.end(), it->begin(), it->end());
311 boost::mpi::broadcast(
comm_cart, excess_ratio, 0);
313 if (excess_ratio >= relative_tolerance) {
314 std::ostringstream serializer;
315 serializer << std::scientific << std::setprecision(4);
316 serializer << excess_ratio;
317 throw std::runtime_error(
318 "The system is not charge neutral. Please neutralize the system "
319 "before adding a new actor by adding the corresponding counterions "
320 "to the system. Alternatively you can turn off the electroneutrality "
321 "check by supplying check_neutrality=False when creating the actor. "
322 "In this case you may be simulating a non-neutral system which will "
323 "affect physical observables like e.g. the pressure, the chemical "
324 "potentials of charged species or potential energies of the system. "
325 "Since simulations of non charge neutral systems are special please "
326 "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.
Solver const & get_coulomb()
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
LongRangeEnergy(ParticleRange const &particles)
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< CoulombScafacos > const &actor) const
auto operator()(std::shared_ptr< DebyeHueckel > const &) const
void operator()(std::shared_ptr< ReactionField > const &) const
LongRangeForce(ParticleRange const &particles)
void operator()(std::shared_ptr< CoulombScafacos > const &actor) const
void operator()(std::shared_ptr< CoulombMMM1D > const &) const
void operator()(std::shared_ptr< CoulombP3M > const &actor) const
void operator()(std::shared_ptr< DebyeHueckel > const &) const
void operator()(std::shared_ptr< ElectrostaticLayerCorrection > const &actor) const
LongRangePressure(ParticleRange const &particles)
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
void on_observable_calc()
void sanity_checks() const
void on_cell_structure_change()
void on_periodicity_change()
void calc_long_range_force(ParticleRange const &particles) const
void on_node_grid_change()
Utils::Vector9d calc_pressure_long_range(ParticleRange const &particles) const
std::unique_ptr< Implementation > impl
Pointer-to-implementation.
bool reinit_on_observable_calc
Whether to reinitialize the solver on observable calculation.
double calc_energy_long_range(ParticleRange const &particles) const
The electrostatic method supports pressure calculation.
void visit_try_catch(Visitor &&visitor, Variant &actor)
Run a kernel on a variant and queue errors.