31#ifdef ESPRESSO_ELECTROSTATICS
40#include "communication.hpp"
47#include "system/System.hpp"
49#include <boost/mpi/collectives/all_reduce.hpp>
50#include <boost/mpi/operations.hpp>
52#include <Kokkos_Core.hpp>
53#include <Kokkos_ScatterView.hpp>
86 auto const q1q2 =
p1.q() *
p2.q();
88 auto force = (*coulomb_kernel_ptr)(q1q2, d.vec21, std::sqrt(d.dist2));
93 (*elc_kernel_ptr)(
p1.pos(),
p2.pos(),
p1.force_and_torque().f,
94 p2.force_and_torque().f, q1q2);
104 }
catch (std::runtime_error
const &
err) {
110 auto &cell_structure = *
system.cell_structure;
111 auto const &coulomb =
system.coulomb;
112 auto const particles = cell_structure.local_particles();
113 auto const prefactor = std::visit(
114 [](
auto const &ptr) {
return ptr->prefactor; }, *coulomb.impl->solver);
115 auto const pref = 1. / (prefactor * 2. * std::numbers::pi);
116 auto const kernel = coulomb.pair_force_kernel();
117 auto const elc_kernel = coulomb.pair_force_elc_kernel();
121 auto const &unique_particles = cell_structure.get_unique_particles();
122 auto &local_force = cell_structure.get_local_force();
132 system.coulomb.calc_long_range_force();
133 cell_structure.ghosts_reduce_forces();
135 Kokkos::Experimental::contribute(local_force,
scatter_force);
137 "reduction", std::size_t{0}, unique_particles.size(),
138 [&local_force, &unique_particles](std::size_t
const i) {
139 auto &force = unique_particles.at(i)->force();
140 force[0] += local_force(i, 0);
141 force[1] += local_force(i, 1);
142 force[2] += local_force(i, 2);
148 for (
auto &p : particles) {
149 auto const pid = p.id();
153 <<
"ICC found zero electric charge on a particle. This must "
167 <<
"ICC found zero electric field on a charge. This must "
199 if (std::abs(p.q()) > 1
e6) {
201 <<
"Particle with id " << p.id() <<
" has a charge (q=" << p.q()
202 <<
") that is too large for the ICC algorithm";
218 boost::mpi::maximum<double>());
226 <<
"ICC failed to converge in the given number of maximal steps.";
229 system.on_particle_charge_change();
234 throw std::domain_error(
"Parameter 'n_icc' must be >= 1");
236 throw std::domain_error(
"Parameter 'convergence' must be > 0");
238 throw std::domain_error(
"Parameter 'relaxation' must be >= 0 and <= 2");
240 throw std::domain_error(
"Parameter 'max_iterations' must be > 0");
242 throw std::domain_error(
"Parameter 'first_id' must be >= 0");
244 throw std::domain_error(
"Parameter 'eps_out' must be > 0");
245 if (
areas.size() !=
static_cast<std::size_t
>(
n_icc))
246 throw std::invalid_argument(
"Parameter 'areas' has incorrect shape");
248 throw std::invalid_argument(
"Parameter 'epsilons' has incorrect shape");
249 if (
sigmas.size() !=
static_cast<std::size_t
>(
n_icc))
250 throw std::invalid_argument(
"Parameter 'sigmas' has incorrect shape");
252 throw std::invalid_argument(
"Parameter 'normals' has incorrect shape");
263 system.on_particle_charge_change();
267 template <
typename T>
void operator()(std::shared_ptr<T>
const &)
const {}
270 void operator()(std::shared_ptr<CoulombP3M>
const &p)
const {
272 throw std::runtime_error(
"ICC does not work with P3M on GPU");
277 operator()(std::shared_ptr<ElectrostaticLayerCorrection>
const &actor)
const {
278 if (actor->elc.dielectric_contrast_on) {
279 throw std::runtime_error(
"ICC conflicts with ELC dielectric contrast");
281 std::visit(*
this, actor->base_solver);
285 throw std::runtime_error(
"ICC does not work with DebyeHueckel.");
288 throw std::runtime_error(
"ICC does not work with ReactionField.");
296 throw std::runtime_error(
"ICC does not work in the NpT ensemble");
303 if (
system.coulomb.impl->solver) {
306 throw std::runtime_error(
"An electrostatics solver is needed by ICC");
311 return coulomb.impl->extension
and
312 std::holds_alternative<std::shared_ptr<ICCStar>>(
313 *coulomb.impl->extension);
317 if (coulomb.impl->extension) {
318 if (
auto icc = std::get_if<std::shared_ptr<ICCStar>>(
319 get_ptr(coulomb.impl->extension))) {
Describes a cell structure / cell system.
void for_each_ghost_particle(Callable &&f) const
Run a kernel on all ghost particles.
void for_each_local_particle(Callable &&f, bool parallel=true) const
Run a kernel on all local particles.
void non_bonded_loop(PairKernel pair_kernel)
Non-bonded pair loop.
void reset_local_force_and_torque()
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