31#ifdef ESPRESSO_ELECTROSTATICS
39#include "communication.hpp"
46#include "system/System.hpp"
48#include <boost/mpi/collectives/all_reduce.hpp>
49#include <boost/mpi/operations.hpp>
51#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
52#include <Kokkos_Core.hpp>
80#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
89 auto const q1q2 =
p1.q() *
p2.q();
91 auto force = (*coulomb_kernel_ptr)(q1q2, d.vec21, std::sqrt(d.dist2));
96 (*elc_kernel_ptr)(
p1.pos(),
p2.pos(),
p1.force_and_torque().f,
97 p2.force_and_torque().f, q1q2);
107 }
catch (std::runtime_error
const &
err) {
113 auto &cell_structure = *
system.cell_structure;
114 auto const &coulomb =
system.coulomb;
115 auto const particles = cell_structure.local_particles();
116 auto const prefactor = std::visit(
117 [](
auto const &ptr) {
return ptr->prefactor; }, *coulomb.impl->solver);
118 auto const pref = 1. / (prefactor * 2. * std::numbers::pi);
119 auto const kernel = coulomb.pair_force_kernel();
120 auto const elc_kernel = coulomb.pair_force_elc_kernel();
123#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
125 auto const &unique_particles = cell_structure.get_unique_particles();
126 auto const &local_force = cell_structure.get_local_force();
136 system.coulomb.calc_long_range_force();
137 cell_structure.ghosts_reduce_forces();
138#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
142 "reduction", std::size_t{0}, unique_particles.size(),
143 [&local_force, &unique_particles,
num_threads](std::size_t
const i) {
144 auto &force = unique_particles.at(i)->force();
146 force[0] += local_force(i,
tid, 0);
147 force[1] += local_force(i,
tid, 1);
148 force[2] += local_force(i,
tid, 2);
156 for (
auto &p : particles) {
157 auto const pid = p.id();
161 <<
"ICC found zero electric charge on a particle. This must "
175 <<
"ICC found zero electric field on a charge. This must "
207 if (std::abs(p.q()) > 1
e6) {
209 <<
"Particle with id " << p.id() <<
" has a charge (q=" << p.q()
210 <<
") that is too large for the ICC algorithm";
220#ifdef ESPRESSO_SHARED_MEMORY_PARALLELISM
228 boost::mpi::maximum<double>());
236 <<
"ICC failed to converge in the given number of maximal steps.";
239 system.on_particle_charge_change();
244 throw std::domain_error(
"Parameter 'n_icc' must be >= 1");
246 throw std::domain_error(
"Parameter 'convergence' must be > 0");
248 throw std::domain_error(
"Parameter 'relaxation' must be >= 0 and <= 2");
250 throw std::domain_error(
"Parameter 'max_iterations' must be > 0");
252 throw std::domain_error(
"Parameter 'first_id' must be >= 0");
254 throw std::domain_error(
"Parameter 'eps_out' must be > 0");
255 if (
areas.size() !=
static_cast<std::size_t
>(
n_icc))
256 throw std::invalid_argument(
"Parameter 'areas' has incorrect shape");
258 throw std::invalid_argument(
"Parameter 'epsilons' has incorrect shape");
259 if (
sigmas.size() !=
static_cast<std::size_t
>(
n_icc))
260 throw std::invalid_argument(
"Parameter 'sigmas' has incorrect shape");
262 throw std::invalid_argument(
"Parameter 'normals' has incorrect shape");
273 system.on_particle_charge_change();
277 template <
typename T>
void operator()(std::shared_ptr<T>
const &)
const {}
280 void operator()(std::shared_ptr<CoulombP3M>
const &p)
const {
282 throw std::runtime_error(
"ICC does not work with P3M on GPU");
287 operator()(std::shared_ptr<ElectrostaticLayerCorrection>
const &actor)
const {
288 if (actor->elc.dielectric_contrast_on) {
289 throw std::runtime_error(
"ICC conflicts with ELC dielectric contrast");
291 std::visit(*
this, actor->base_solver);
295 throw std::runtime_error(
"ICC does not work with DebyeHueckel.");
298 throw std::runtime_error(
"ICC does not work with ReactionField.");
306 throw std::runtime_error(
"ICC does not work in the NpT ensemble");
313 if (
system.coulomb.impl->solver) {
316 throw std::runtime_error(
"An electrostatics solver is needed by ICC");
321 return coulomb.impl->extension
and
322 std::holds_alternative<std::shared_ptr<ICCStar>>(
323 *coulomb.impl->extension);
327 if (coulomb.impl->extension) {
328 if (
auto icc = std::get_if<std::shared_ptr<ICCStar>>(
329 get_ptr(coulomb.impl->extension))) {
Describes a cell structure / cell system.
void for_each_local_particle(ParticleUnaryOp &&f, bool parallel=true) const
Run a kernel on all local particles.
void non_bonded_loop(PairKernel pair_kernel)
Non-bonded pair loop.
void for_each_ghost_particle(ParticleUnaryOp &&f) const
Run a kernel on all ghost particles.
void update_icc_particles()
bool has_icc_enabled() const
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
boost::mpi::communicator comm_cart
The communicator.
const T * get_ptr(std::optional< T > const &opt)
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
#define runtimeErrorMsg()
static void force_calc_icc(CellStructure &cell_structure, Coulomb::ShortRangeForceKernel::result_type const &coulomb_kernel, Coulomb::ShortRangeForceCorrectionsKernel::result_type const &elc_kernel)
Calculate the electrostatic forces between source charges (= real charges) and wall charges.
ICC is a method that allows to take into account the influence of arbitrarily shaped dielectric inter...
@ DATA_PART_PROPERTIES
Particle::p.
P3M algorithm for long-range Coulomb interaction.
ESPRESSO_ATTR_ALWAYS_INLINE void update_aosoa_charges(CellStructure &cell_structure)
std::optional< kernel_type > result_type
std::optional< kernel_type > result_type
Distance vector and length handed to pair kernels.
void sanity_checks_active_solver() const
void iteration()
The main iterative scheme, where the surface element charges are calculated self-consistently.
icc_data icc_cfg
ICC parameters.
void on_activation() const
void sanity_check() const
Struct holding all information for one particle.
void operator()(std::shared_ptr< T > const &) const
void operator()(std::shared_ptr< ReactionField > const &) const
void operator()(std::shared_ptr< CoulombP3M > const &p) const
void operator()(std::shared_ptr< DebyeHueckel > const &) const
void operator()(std::shared_ptr< ElectrostaticLayerCorrection > const &actor) const
double convergence
convergence criteria
int first_id
first ICC particle id
double relaxation
relaxation parameter
std::vector< Utils::Vector3d > normals
surface normal vectors
int max_iterations
maximum number of iterations
int n_icc
First id of ICC particle.
double eps_out
bulk dielectric constant
void sanity_checks() const
std::vector< double > sigmas
surface charge density of the particles
std::vector< double > epsilons
dielectric constants of the particles
Utils::Vector3d ext_field
external electric field
int citeration
last number of iterations
std::vector< double > areas
areas of the particles