From 581ce414697808c810c1698f6354a2ea0b2cc789 Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Thu, 11 Apr 2019 11:02:00 +0200 Subject: [PATCH 01/16] core: use arb. force/energy func, renaming --- ...ulomb_p3m_sr.cpp => bonded_coulomb_sr.cpp} | 12 ++--- ...ulomb_p3m_sr.hpp => bonded_coulomb_sr.hpp} | 45 +++++++++---------- src/core/energy_inline.hpp | 10 ++--- src/core/forces_inline.hpp | 10 ++--- 4 files changed, 34 insertions(+), 43 deletions(-) rename src/core/bonded_interactions/{bonded_coulomb_p3m_sr.cpp => bonded_coulomb_sr.cpp} (78%) rename src/core/bonded_interactions/{bonded_coulomb_p3m_sr.hpp => bonded_coulomb_sr.hpp} (64%) diff --git a/src/core/bonded_interactions/bonded_coulomb_p3m_sr.cpp b/src/core/bonded_interactions/bonded_coulomb_sr.cpp similarity index 78% rename from src/core/bonded_interactions/bonded_coulomb_p3m_sr.cpp rename to src/core/bonded_interactions/bonded_coulomb_sr.cpp index 927be9a763f..26f2bd2d4b2 100644 --- a/src/core/bonded_interactions/bonded_coulomb_p3m_sr.cpp +++ b/src/core/bonded_interactions/bonded_coulomb_sr.cpp @@ -20,21 +20,21 @@ */ /** \file * - * Implementation of \ref bonded_coulomb_p3m_sr.hpp + * Implementation of \ref bonded_coulomb_sr.hpp */ -#include "bonded_coulomb_p3m_sr.hpp" +#include "bonded_coulomb_sr.hpp" #include "communication.hpp" -#ifdef P3M +#ifdef ELECTROSTATICS -int bonded_coulomb_p3m_sr_set_params(int bond_type, double q1q2) { +int bonded_coulomb_sr_set_params(int bond_type, double q1q2) { if (bond_type < 0) return ES_ERROR; make_bond_type_exist(bond_type); - bonded_ia_params[bond_type].p.bonded_coulomb_p3m_sr.q1q2 = q1q2; - bonded_ia_params[bond_type].type = BONDED_IA_BONDED_COULOMB_P3M_SR; + //bonded_ia_params[bond_type].p.bonded_coulomb_p3m_sr.q1q2 = q1q2; + bonded_ia_params[bond_type].type = BONDED_IA_BONDED_COULOMB_SR; bonded_ia_params[bond_type].num = 1; /* broadcast interaction parameters */ diff --git a/src/core/bonded_interactions/bonded_coulomb_p3m_sr.hpp b/src/core/bonded_interactions/bonded_coulomb_sr.hpp similarity index 64% rename from src/core/bonded_interactions/bonded_coulomb_p3m_sr.hpp rename to src/core/bonded_interactions/bonded_coulomb_sr.hpp index e0ce3a76049..93bca3054e0 100644 --- a/src/core/bonded_interactions/bonded_coulomb_p3m_sr.hpp +++ b/src/core/bonded_interactions/bonded_coulomb_sr.hpp @@ -18,12 +18,12 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#ifndef _BONDED_COULOMB_P3M_SR_HPP -#define _BONDED_COULOMB_P3M_SR_HPP +#ifndef _BONDED_COULOMB_SR_HPP +#define _BONDED_COULOMB_SR_HPP /** \file - * Routines to calculate the BONDED_COULOMB_P3M_SR Energy or/and - * BONDED_COULOMB_P3M_SR force for a particle pair. This is only the shortrange - * part of P3M and first used to subtract certain intramolecular interactions in + * Routines to calculate the BONDED_COULOMB_SR Energy or/and + * BONDED_COULOMB_SR force for a particle pair. This is only the shortrange + * part of any coulomb interaction and first used to subtract certain intramolecular interactions in * combination with Thole damping \ref forces.cpp */ @@ -31,11 +31,11 @@ #include "config.hpp" -#ifdef P3M +#ifdef ELECTROSTATICS #include "bonded_interaction_data.hpp" #include "debug.hpp" -#include "electrostatics_magnetostatics/p3m.hpp" +#include "electrostatics_magnetostatics/coulomb_inline.hpp" #include "particle_data.hpp" #include "utils.hpp" @@ -44,9 +44,9 @@ * @retval ES_OK on success * @retval ES_ERROR on error */ -int bonded_coulomb_p3m_sr_set_params(int bond_type, double q1q2); +int bonded_coulomb_sr_set_params(int bond_type, double q1q2); -/** Computes the BONDED_COULOMB_P3M_SR pair force. +/** Computes the BONDED_COULOMB_SR pair force. * @param[in] p1 First particle. * @param[in] p2 Second particle. * @param[in] iaparams Interaction parameters. @@ -55,26 +55,25 @@ int bonded_coulomb_p3m_sr_set_params(int bond_type, double q1q2); * @retval 0 */ inline int -calc_bonded_coulomb_p3m_sr_pair_force(Particle const *p1, Particle const *p2, - Bonded_ia_parameters const *iaparams, - double const dx[3], double force[3]) { +calc_bonded_coulomb_sr_pair_force(Particle const *p1, Particle const *p2, + Bonded_ia_parameters const *iaparams, + double const dx[3], double force[3]) { double dist2 = sqrlen(dx); double dist = sqrt(dist2); - if (dist < p3m.params.r_cut) { - // Set to zero because p3m adds forces + if (dist < coulomb_cutoff) { + // Set to zero because calc_pair_force adds forces force[0] = force[1] = force[2] = 0.; - p3m_add_pair_force(iaparams->p.bonded_coulomb_p3m_sr.q1q2, dx, dist2, dist, - force); + Coulomb::calc_pair_force(p1, p2, dx, dist2, dist, force); ONEPART_TRACE(if (p1->p.identity == check_id) fprintf( stderr, - "%d: OPT: BONDED_COULOMB_P3M_SR f = (%.3e,%.3e,%.3e) with part id=%d " + "%d: OPT: BONDED_COULOMB_SR f = (%.3e,%.3e,%.3e) with part id=%d " "at dist %f\n", this_node, p1->f.f[0], p1->f.f[1], p1->f.f[2], p2->p.identity, dist2)); ONEPART_TRACE(if (p2->p.identity == check_id) fprintf( stderr, - "%d: OPT: BONDED_COULOMB_P3M_SR f = (%.3e,%.3e,%.3e) with part id=%d " + "%d: OPT: BONDED_COULOMB_SR f = (%.3e,%.3e,%.3e) with part id=%d " "at dist %f\n", this_node, p2->f.f[0], p2->f.f[1], p2->f.f[2], p1->p.identity, dist2)); } @@ -82,7 +81,7 @@ calc_bonded_coulomb_p3m_sr_pair_force(Particle const *p1, Particle const *p2, return 0; } -/** Computes the BONDED_COULOMB_P3M_SR pair energy. +/** Computes the BONDED_COULOMB_SR pair energy. * @param[in] p1 First particle. * @param[in] p2 Second particle. * @param[in] iaparams Interaction parameters. @@ -91,12 +90,12 @@ calc_bonded_coulomb_p3m_sr_pair_force(Particle const *p1, Particle const *p2, * @retval 0 */ inline int -bonded_coulomb_p3m_sr_pair_energy(Particle const *p1, Particle const *p2, - Bonded_ia_parameters const *iaparams, - double const dx[3], double *_energy) { +bonded_coulomb_sr_pair_energy(Particle const *p1, Particle const *p2, + Bonded_ia_parameters const *iaparams, + double const dx[3], double *_energy) { double dist2 = sqrlen(dx); double dist = sqrt(dist2); - *_energy = p3m_pair_energy(iaparams->p.bonded_coulomb_p3m_sr.q1q2, dist); + *_energy = Coulomb::add_pair_energy(p1, p2, dx, dist, dist2); return 0; } diff --git a/src/core/energy_inline.hpp b/src/core/energy_inline.hpp index e38199c09da..c28d46e5d46 100644 --- a/src/core/energy_inline.hpp +++ b/src/core/energy_inline.hpp @@ -58,9 +58,7 @@ #ifdef ELECTROSTATICS #include "bonded_interactions/bonded_coulomb.hpp" #include "electrostatics_magnetostatics/coulomb_inline.hpp" -#endif -#ifdef P3M -#include "bonded_interactions/bonded_coulomb_p3m_sr.hpp" +#include "bonded_interactions/bonded_coulomb_sr.hpp" #endif #include "statistics.hpp" @@ -274,11 +272,9 @@ inline void add_bonded_energy(Particle *p1) { case BONDED_IA_BONDED_COULOMB: bond_broken = bonded_coulomb_pair_energy(p1, p2, iaparams, dx, &ret); break; -#endif -#ifdef P3M - case BONDED_IA_BONDED_COULOMB_P3M_SR: + case BONDED_IA_BONDED_COULOMB_SR: bond_broken = - bonded_coulomb_p3m_sr_pair_energy(p1, p2, iaparams, dx, &ret); + bonded_coulomb_sr_pair_energy(p1, p2, iaparams, dx, &ret); break; #endif #ifdef LENNARD_JONES diff --git a/src/core/forces_inline.hpp b/src/core/forces_inline.hpp index 48522c3f76b..7846ae04906 100644 --- a/src/core/forces_inline.hpp +++ b/src/core/forces_inline.hpp @@ -69,9 +69,7 @@ #ifdef ELECTROSTATICS #include "bonded_interactions/bonded_coulomb.hpp" #include "electrostatics_magnetostatics/coulomb_inline.hpp" -#endif -#ifdef P3M -#include "bonded_interactions/bonded_coulomb_p3m_sr.hpp" +#include "bonded_interactions/bonded_coulomb_sr.hpp" #endif #ifdef IMMERSED_BOUNDARY #include "immersed_boundary/ibm_tribend.hpp" @@ -386,11 +384,9 @@ inline int calc_bond_pair_force(Particle *p1, Particle *p2, case BONDED_IA_BONDED_COULOMB: bond_broken = calc_bonded_coulomb_pair_force(p1, p2, iaparams, dx, force); break; -#endif -#ifdef P3M - case BONDED_IA_BONDED_COULOMB_P3M_SR: + case BONDED_IA_BONDED_COULOMB_SR: bond_broken = - calc_bonded_coulomb_p3m_sr_pair_force(p1, p2, iaparams, dx, force); + calc_bonded_coulomb_sr_pair_force(p1, p2, iaparams, dx, force); break; #endif #ifdef LENNARD_JONES From 99e57464fd421dc5b208037768aadd0a5776695f Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Thu, 11 Apr 2019 11:46:04 +0200 Subject: [PATCH 02/16] python renaming --- .../bonded_interaction_data.hpp | 15 ++++++------- src/python/espressomd/interactions.pxd | 15 +++++++------ src/python/espressomd/interactions.pyx | 21 +++++++++---------- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/core/bonded_interactions/bonded_interaction_data.hpp b/src/core/bonded_interactions/bonded_interaction_data.hpp index e9680921894..44e179ca445 100644 --- a/src/core/bonded_interactions/bonded_interaction_data.hpp +++ b/src/core/bonded_interactions/bonded_interaction_data.hpp @@ -23,6 +23,8 @@ enum BondedInteraction { BONDED_IA_QUARTIC, /** Type of bonded interaction is a BONDED_COULOMB */ BONDED_IA_BONDED_COULOMB, + /** Type of bonded interaction is a BONDED_COULOMB_SR */ + BONDED_IA_BONDED_COULOMB_SR, /** Type of bonded interaction is a dihedral potential. */ BONDED_IA_DIHEDRAL, /** Type of tabulated bonded interaction potential, @@ -58,8 +60,6 @@ enum BondedInteraction { BONDED_IA_UMBRELLA, /** Type of bonded interaction is thermalized distance bond. */ BONDED_IA_THERMALIZED_DIST, - /** Type of bonded interaction is a BONDED_COULOMB_P3M_SR */ - BONDED_IA_BONDED_COULOMB_P3M_SR, }; /** Specify tabulated bonded interactions */ @@ -156,13 +156,12 @@ struct Bonded_coulomb_bond_parameters { double prefactor; }; -#ifdef P3M -/** Parameters for Coulomb bond p3m short-range Potential */ -struct Bonded_coulomb_p3m_sr_bond_parameters { +/** Parameters for Coulomb bond short-range Potential */ +struct Bonded_coulomb_sr_bond_parameters { /** charge factor */ double q1q2; }; -#endif + /** Parameters for three-body angular potential. * @note @@ -309,6 +308,7 @@ union Bond_parameters { #endif Quartic_bond_parameters quartic; Bonded_coulomb_bond_parameters bonded_coulomb; + Bonded_coulomb_sr_bond_parameters bonded_coulomb_sr; Angle_bond_parameters angle; Angle_harmonic_bond_parameters angle_harmonic; Angle_cosine_bond_parameters angle_cosine; @@ -321,9 +321,6 @@ union Bond_parameters { Umbrella_bond_parameters umbrella; #endif Thermalized_bond_parameters thermalized_bond; -#ifdef P3M - Bonded_coulomb_p3m_sr_bond_parameters bonded_coulomb_p3m_sr; -#endif Subt_lj_bond_parameters subt_lj; Rigid_bond_parameters rigid_bond; IBM_Triel_Parameters ibm_triel; diff --git a/src/python/espressomd/interactions.pxd b/src/python/espressomd/interactions.pxd index edea78c5832..94c44224909 100644 --- a/src/python/espressomd/interactions.pxd +++ b/src/python/espressomd/interactions.pxd @@ -305,13 +305,13 @@ ELSE: int type TabulatedPotential * pot -IF P3M: +IF ELECTROSTATICS: cdef extern from "bonded_interactions/bonded_interaction_data.hpp": #* Parameters for Bonded Coulomb p3m sr */ - cdef struct Bonded_coulomb_p3m_sr_bond_parameters: + cdef struct Bonded_coulomb_sr_bond_parameters: double q1q2 ELSE: - cdef struct Bonded_coulomb_p3m_sr_bond_parameters: + cdef struct Bonded_coulomb_sr_bond_parameters: double q1q2 cdef extern from "bonded_interactions/bonded_interaction_data.hpp": @@ -466,7 +466,7 @@ cdef extern from "bonded_interactions/bonded_interaction_data.hpp": Oif_local_forces_bond_parameters oif_local_forces Thermalized_bond_parameters thermalized_bond Bonded_coulomb_bond_parameters bonded_coulomb - Bonded_coulomb_p3m_sr_bond_parameters bonded_coulomb_p3m_sr + Bonded_coulomb_sr_bond_parameters bonded_coulomb_sr Harmonic_bond_parameters harmonic Harmonic_dumbbell_bond_parameters harmonic_dumbbell Angle_bond_parameters angle @@ -545,9 +545,8 @@ IF ELECTROSTATICS: cdef extern from "bonded_interactions/bonded_coulomb.hpp": int bonded_coulomb_set_params(int bond_type, double prefactor) -IF P3M: - cdef extern from "bonded_interactions/bonded_coulomb_p3m_sr.hpp": - int bonded_coulomb_p3m_sr_set_params(int bond_type, double q1q2) + cdef extern from "bonded_interactions/bonded_coulomb_sr.hpp": + int bonded_coulomb_sr_set_params(int bond_type, double q1q2) cdef extern from "bonded_interactions/bonded_interaction_data.hpp": int virtual_set_params(int bond_type) @@ -559,7 +558,7 @@ cdef extern from "bonded_interactions/bonded_interaction_data.hpp": BONDED_IA_HARMONIC, BONDED_IA_HARMONIC_DUMBBELL, BONDED_IA_BONDED_COULOMB, - BONDED_IA_BONDED_COULOMB_P3M_SR, + BONDED_IA_BONDED_COULOMB_SR, BONDED_IA_DIHEDRAL, BONDED_IA_TABULATED, BONDED_IA_SUBT_LJ, diff --git a/src/python/espressomd/interactions.pyx b/src/python/espressomd/interactions.pyx index 1a09974e753..113270d78a9 100644 --- a/src/python/espressomd/interactions.pyx +++ b/src/python/espressomd/interactions.pyx @@ -2193,13 +2193,13 @@ if ELECTROSTATICS: bonded_coulomb_set_params( self._bond_id, self._params["prefactor"]) -if P3M: - class BondedCoulombP3MSRBond(BondedInteraction): +if ELECTROSTATICS: + class BondedCoulombSRBond(BondedInteraction): def __init__(self, *args, **kwargs): """ - BondedCoulombP3MSRBond initialiser. Used to instantiate a BondedCoulombP3MSRBond identifier - with a given set of parameters. Calculates ony the P3M shortrange part. + BondedCoulombSRBond initialiser. Used to instantiate a BondedCoulombSRBond identifier + with a given set of parameters. Calculates the shortrange part of coulomb interactions. Parameters ---------- @@ -2207,13 +2207,13 @@ if P3M: q1q2 : :obj:`float` Sets the charge factor of the involved particle pair. Not the particle charges are used to allow e.g. only partial subtraction of the involved charges. """ - super(BondedCoulombP3MSRBond, self).__init__(*args, **kwargs) + super(BondedCoulombSRBond, self).__init__(*args, **kwargs) def type_number(self): - return BONDED_IA_BONDED_COULOMB_P3M_SR + return BONDED_IA_BONDED_COULOMB_SR def type_name(self): - return "BONDED_COULOMB_P3M_SR" + return "BONDED_COULOMB_SR" def valid_keys(self): return {"q1q2"} @@ -2227,10 +2227,10 @@ if P3M: def _get_params_from_es_core(self): return \ {"q1q2": bonded_ia_params[ - self._bond_id].p.bonded_coulomb_p3m_sr.q1q2} + self._bond_id].p.bonded_coulomb_sr.q1q2} def _set_params_in_es_core(self): - bonded_coulomb_p3m_sr_set_params( + bonded_coulomb_sr_set_params( self._bond_id, self._params["q1q2"]) @@ -3327,9 +3327,8 @@ IF LENNARD_JONES: bonded_interaction_classes[int(BONDED_IA_SUBT_LJ)] = SubtLJ IF ELECTROSTATICS: bonded_interaction_classes[int(BONDED_IA_BONDED_COULOMB)] = BondedCoulomb -IF P3M: bonded_interaction_classes[ - int(BONDED_IA_BONDED_COULOMB_P3M_SR)] = BondedCoulombP3MSRBond + int(BONDED_IA_BONDED_COULOMB_SR)] = BondedCoulombSRBond class BondedInteractions(object): From 38acac92d6c4707faf064ffc5353bfedc98c189d Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Thu, 11 Apr 2019 13:37:12 +0200 Subject: [PATCH 03/16] new parameter q1q2 for coulomb_inline --- src/core/CMakeLists.txt | 2 +- .../bonded_interactions/bonded_coulomb_sr.hpp | 21 +++++++++++-------- .../coulomb_inline.hpp | 20 ++++++++---------- .../electrostatics_magnetostatics/icc.cpp | 2 +- src/core/energy_inline.hpp | 2 +- src/core/forces_inline.hpp | 2 +- src/core/pressure_inline.hpp | 2 +- 7 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index fe714c555da..82b88c19c44 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -58,7 +58,7 @@ set(EspressoCore_SRC bonded_interactions/angle_cossquare.cpp bonded_interactions/angle_harmonic.cpp bonded_interactions/bonded_coulomb.cpp - bonded_interactions/bonded_coulomb_p3m_sr.cpp + bonded_interactions/bonded_coulomb_sr.cpp bonded_interactions/bonded_interaction_data.cpp bonded_interactions/bonded_tab.cpp bonded_interactions/dihedral.cpp diff --git a/src/core/bonded_interactions/bonded_coulomb_sr.hpp b/src/core/bonded_interactions/bonded_coulomb_sr.hpp index 93bca3054e0..a4608a0c952 100644 --- a/src/core/bonded_interactions/bonded_coulomb_sr.hpp +++ b/src/core/bonded_interactions/bonded_coulomb_sr.hpp @@ -55,16 +55,19 @@ int bonded_coulomb_sr_set_params(int bond_type, double q1q2); * @retval 0 */ inline int -calc_bonded_coulomb_sr_pair_force(Particle const *p1, Particle const *p2, +calc_bonded_coulomb_sr_pair_force(Particle *p1, Particle *p2, Bonded_ia_parameters const *iaparams, - double const dx[3], double force[3]) { + double dx[3], double force[3]) { double dist2 = sqrlen(dx); double dist = sqrt(dist2); if (dist < coulomb_cutoff) { - // Set to zero because calc_pair_force adds forces - force[0] = force[1] = force[2] = 0.; - - Coulomb::calc_pair_force(p1, p2, dx, dist2, dist, force); + //TODO ugly workaround + Vector3d forcevec{}; + //TODO only to get rid of warnings + force[0]++; + + Coulomb::calc_pair_force(p1, p2, iaparams->p.bonded_coulomb_sr.q1q2, dx, dist2, dist, forcevec); + force = forcevec.data() ONEPART_TRACE(if (p1->p.identity == check_id) fprintf( stderr, @@ -90,12 +93,12 @@ calc_bonded_coulomb_sr_pair_force(Particle const *p1, Particle const *p2, * @retval 0 */ inline int -bonded_coulomb_sr_pair_energy(Particle const *p1, Particle const *p2, +bonded_coulomb_sr_pair_energy(Particle *p1, Particle *p2, Bonded_ia_parameters const *iaparams, - double const dx[3], double *_energy) { + double dx[3], double *_energy) { double dist2 = sqrlen(dx); double dist = sqrt(dist2); - *_energy = Coulomb::add_pair_energy(p1, p2, dx, dist, dist2); + *_energy = Coulomb::add_pair_energy(p1, p2, iaparams->p.bonded_coulomb_sr.q1q2, dx, dist, dist2); return 0; } diff --git a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp index a97cae49e55..bcd5cb4eeca 100644 --- a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp +++ b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp @@ -15,9 +15,7 @@ namespace Coulomb { // forces_inline -inline void calc_pair_force(Particle *p1, Particle *p2, double *d, double dist, - double dist2, Vector3d &force) { - auto const q1q2 = p1->p.q * p2->p.q; +inline void calc_pair_force(Particle *p1, Particle *p2, double q1q2, double *d, double dist, double dist2, Vector3d &force) { if (q1q2 != 0) { Vector3d f{}; @@ -73,7 +71,7 @@ inline void calc_pair_force(Particle *p1, Particle *p2, double *d, double dist, } // pressure_inline.hpp -inline void add_pair_pressure(Particle *p1, Particle *p2, double *d, +inline void add_pair_pressure(Particle *p1, Particle *p2, double q1q2, double *d, double dist, double dist2, Observable_stat &virials, Observable_stat &p_tensor) { @@ -88,7 +86,7 @@ inline void add_pair_pressure(Particle *p1, Particle *p2, double *d, case COULOMB_DH: case COULOMB_RF: { Vector3d force{}; - calc_pair_force(p1, p2, d, dist, dist2, force); + calc_pair_force(p1, p2, q1q2, d, dist, dist2, force); /* Calculate the virial pressure */ for (int k = 0; k < 3; k++) { @@ -107,21 +105,21 @@ inline void add_pair_pressure(Particle *p1, Particle *p2, double *d, } // energy_inline -inline double add_pair_energy(Particle *p1, Particle *p2, double *d, - double dist, double dist2) { +inline double add_pair_energy(Particle *p1, Particle *p2, double q1q2, + double *d, double dist, double dist2) { /* real space Coulomb */ auto E = [&]() { switch (coulomb.method) { #ifdef P3M case COULOMB_P3M_GPU: case COULOMB_P3M: - return p3m_pair_energy(p1->p.q * p2->p.q, dist); + return p3m_pair_energy(q1q2, dist); case COULOMB_ELC_P3M: if (elc_params.dielectric_contrast_on) { return 0.5 * ELC_P3M_dielectric_layers_energy_contribution(p1, p2) + - p3m_pair_energy(p1->p.q * p2->p.q, dist); + p3m_pair_energy(q1q2, dist); } else { - return p3m_pair_energy(p1->p.q * p2->p.q, dist); + return p3m_pair_energy(q1q2, dist); } #endif #ifdef SCAFACOS @@ -135,7 +133,7 @@ inline double add_pair_energy(Particle *p1, Particle *p2, double *d, case COULOMB_MMM1D: return mmm1d_coulomb_pair_energy(p1, p2, d, dist2, dist); case COULOMB_MMM2D: - return mmm2d_coulomb_pair_energy(p1->p.q * p2->p.q, d, dist2, dist); + return mmm2d_coulomb_pair_energy(q1q2, d, dist2, dist); default: return 0.; } diff --git a/src/core/electrostatics_magnetostatics/icc.cpp b/src/core/electrostatics_magnetostatics/icc.cpp index a9177043776..9df9b71f569 100644 --- a/src/core/electrostatics_magnetostatics/icc.cpp +++ b/src/core/electrostatics_magnetostatics/icc.cpp @@ -70,7 +70,7 @@ inline void add_non_bonded_pair_force_iccp3m(Particle *p1, Particle *p2, double d[3], double dist, double dist2) { Vector3d force{}; - Coulomb::calc_pair_force(p1, p2, d, dist, dist2, force); + Coulomb::calc_pair_force(p1, p2, p1->p.q * p2->p.q, d, dist, dist2, force); p1->f.f += force; p2->f.f -= force; diff --git a/src/core/energy_inline.hpp b/src/core/energy_inline.hpp index c28d46e5d46..7a941330efe 100644 --- a/src/core/energy_inline.hpp +++ b/src/core/energy_inline.hpp @@ -192,7 +192,7 @@ inline void add_non_bonded_pair_energy(Particle *p1, Particle *p2, double d[3], calc_non_bonded_pair_energy(p1, p2, ia_params, d, dist, dist2); #ifdef ELECTROSTATICS - energy.coulomb[0] += Coulomb::add_pair_energy(p1, p2, d, dist, dist2); + energy.coulomb[0] += Coulomb::add_pair_energy(p1, p2, p1->p.q*p2->p.q, d, dist, dist2); #endif #ifdef DIPOLES diff --git a/src/core/forces_inline.hpp b/src/core/forces_inline.hpp index 7846ae04906..dea80752eb0 100644 --- a/src/core/forces_inline.hpp +++ b/src/core/forces_inline.hpp @@ -309,7 +309,7 @@ inline void add_non_bonded_pair_force(Particle *p1, Particle *p2, double d[3], /***********************************************/ #ifdef ELECTROSTATICS - Coulomb::calc_pair_force(p1, p2, d, dist, dist2, force); + Coulomb::calc_pair_force(p1, p2, p1->p.q * p2->p.q, d, dist, dist2, force); #endif /*********************************************************************/ diff --git a/src/core/pressure_inline.hpp b/src/core/pressure_inline.hpp index c36cf056c68..82bbe480152 100644 --- a/src/core/pressure_inline.hpp +++ b/src/core/pressure_inline.hpp @@ -83,7 +83,7 @@ inline void add_non_bonded_pair_virials(Particle *p1, Particle *p2, double d[3], #ifdef ELECTROSTATICS /* real space Coulomb */ if (coulomb.method != COULOMB_NONE) { - Coulomb::add_pair_pressure(p1, p2, d, dist, dist2, virials, p_tensor); + Coulomb::add_pair_pressure(p1, p2, p1->p.q * p2->p.q, d, dist, dist2, virials, p_tensor); } #endif /*ifdef ELECTROSTATICS */ From 9f34a9c8070ec93f8479c78238429123640748b6 Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Thu, 11 Apr 2019 13:55:03 +0200 Subject: [PATCH 04/16] const --- src/core/bonded_interactions/bonded_coulomb_sr.cpp | 2 +- src/core/bonded_interactions/bonded_coulomb_sr.hpp | 7 ++++--- src/core/electrostatics_magnetostatics/coulomb_inline.hpp | 4 +++- src/core/electrostatics_magnetostatics/reaction_field.hpp | 6 +++--- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/core/bonded_interactions/bonded_coulomb_sr.cpp b/src/core/bonded_interactions/bonded_coulomb_sr.cpp index 26f2bd2d4b2..c645976d325 100644 --- a/src/core/bonded_interactions/bonded_coulomb_sr.cpp +++ b/src/core/bonded_interactions/bonded_coulomb_sr.cpp @@ -33,7 +33,7 @@ int bonded_coulomb_sr_set_params(int bond_type, double q1q2) { make_bond_type_exist(bond_type); - //bonded_ia_params[bond_type].p.bonded_coulomb_p3m_sr.q1q2 = q1q2; + bonded_ia_params[bond_type].p.bonded_coulomb_sr.q1q2 = q1q2; bonded_ia_params[bond_type].type = BONDED_IA_BONDED_COULOMB_SR; bonded_ia_params[bond_type].num = 1; diff --git a/src/core/bonded_interactions/bonded_coulomb_sr.hpp b/src/core/bonded_interactions/bonded_coulomb_sr.hpp index a4608a0c952..8cbc1bde828 100644 --- a/src/core/bonded_interactions/bonded_coulomb_sr.hpp +++ b/src/core/bonded_interactions/bonded_coulomb_sr.hpp @@ -57,14 +57,15 @@ int bonded_coulomb_sr_set_params(int bond_type, double q1q2); inline int calc_bonded_coulomb_sr_pair_force(Particle *p1, Particle *p2, Bonded_ia_parameters const *iaparams, - double dx[3], double force[3]) { + double const dx[3], double force[3]) { double dist2 = sqrlen(dx); double dist = sqrt(dist2); if (dist < coulomb_cutoff) { //TODO ugly workaround Vector3d forcevec{}; - //TODO only to get rid of warnings - force[0]++; + + //only to get rid of warnings + //force[0]++; Coulomb::calc_pair_force(p1, p2, iaparams->p.bonded_coulomb_sr.q1q2, dx, dist2, dist, forcevec); force = forcevec.data() diff --git a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp index bcd5cb4eeca..0a21ee0d30e 100644 --- a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp +++ b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp @@ -15,7 +15,9 @@ namespace Coulomb { // forces_inline -inline void calc_pair_force(Particle *p1, Particle *p2, double q1q2, double *d, double dist, double dist2, Vector3d &force) { +inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, + double const *d, double const dist, + double const dist2, Vector3d &force) { if (q1q2 != 0) { Vector3d f{}; diff --git a/src/core/electrostatics_magnetostatics/reaction_field.hpp b/src/core/electrostatics_magnetostatics/reaction_field.hpp index 267a40183b7..ac3aa85c1e7 100644 --- a/src/core/electrostatics_magnetostatics/reaction_field.hpp +++ b/src/core/electrostatics_magnetostatics/reaction_field.hpp @@ -59,7 +59,7 @@ int rf_set_params(double kappa, double epsilon1, double epsilon2, double r_cut); inline void add_rf_coulomb_pair_force_no_cutoff(const Particle *const p1, const Particle *const p2, - double const d[3], double dist, + double const d[3], double const dist, double force[3]) { int j; double fac; @@ -79,8 +79,8 @@ inline void add_rf_coulomb_pair_force_no_cutoff(const Particle *const p1, @param dist Distance between p1 and p2. @param force returns the force on particle 1. */ -inline void add_rf_coulomb_pair_force(Particle *p1, Particle *p2, double d[3], - double dist, double force[3]) { +inline void add_rf_coulomb_pair_force(Particle *p1, Particle *p2, double const d[3], + double const dist, double force[3]) { if (dist < rf_params.r_cut) { add_rf_coulomb_pair_force_no_cutoff(p1, p2, d, dist, force); } From 9ece56463bdb2bac67894115e6cc1bc761502098 Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Thu, 11 Apr 2019 18:23:33 +0200 Subject: [PATCH 05/16] sr force works --- .../bonded_interactions/bonded_coulomb_sr.hpp | 9 ++++---- .../coulomb_inline.hpp | 10 ++++---- .../debye_hueckel.hpp | 14 +++++------ .../reaction_field.hpp | 19 +++++++-------- testsuite/python/electrostaticInteractions.py | 4 ++-- testsuite/python/interactions_bonded.py | 23 ++++++++++++++++++- 6 files changed, 48 insertions(+), 31 deletions(-) diff --git a/src/core/bonded_interactions/bonded_coulomb_sr.hpp b/src/core/bonded_interactions/bonded_coulomb_sr.hpp index 8cbc1bde828..7cc43f56729 100644 --- a/src/core/bonded_interactions/bonded_coulomb_sr.hpp +++ b/src/core/bonded_interactions/bonded_coulomb_sr.hpp @@ -64,12 +64,11 @@ calc_bonded_coulomb_sr_pair_force(Particle *p1, Particle *p2, //TODO ugly workaround Vector3d forcevec{}; - //only to get rid of warnings - //force[0]++; + Coulomb::calc_pair_force(p1, p2, iaparams->p.bonded_coulomb_sr.q1q2, dx, dist, dist2, forcevec); + force[0] = forcevec[0]; + force[1] = forcevec[1]; + force[2] = forcevec[2]; - Coulomb::calc_pair_force(p1, p2, iaparams->p.bonded_coulomb_sr.q1q2, dx, dist2, dist, forcevec); - force = forcevec.data() - ONEPART_TRACE(if (p1->p.identity == check_id) fprintf( stderr, "%d: OPT: BONDED_COULOMB_SR f = (%.3e,%.3e,%.3e) with part id=%d " diff --git a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp index 0a21ee0d30e..0d703e587c8 100644 --- a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp +++ b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp @@ -54,10 +54,10 @@ inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, add_mmm2d_coulomb_pair_force(q1q2, d, dist2, dist, f.data()); break; case COULOMB_DH: - add_dh_coulomb_pair_force(p1, p2, d, dist, f.data()); + add_dh_coulomb_pair_force(q1q2, d, dist, f.data()); break; case COULOMB_RF: - add_rf_coulomb_pair_force(p1, p2, d, dist, f.data()); + add_rf_coulomb_pair_force(q1q2, d, dist, f.data()); break; #ifdef SCAFACOS case COULOMB_SCAFACOS: @@ -67,7 +67,7 @@ inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, default: break; } - + force += coulomb.prefactor * f; } } @@ -129,9 +129,9 @@ inline double add_pair_energy(Particle *p1, Particle *p2, double q1q2, return Scafacos::pair_energy(p1, p2, dist); #endif case COULOMB_DH: - return dh_coulomb_pair_energy(p1, p2, dist); + return dh_coulomb_pair_energy(q1q2, dist); case COULOMB_RF: - return rf_coulomb_pair_energy(p1, p2, dist); + return rf_coulomb_pair_energy(q1q2, dist); case COULOMB_MMM1D: return mmm1d_coulomb_pair_energy(p1, p2, d, dist2, dist); case COULOMB_MMM2D: diff --git a/src/core/electrostatics_magnetostatics/debye_hueckel.hpp b/src/core/electrostatics_magnetostatics/debye_hueckel.hpp index c956ef53a7e..99df95d6ecd 100644 --- a/src/core/electrostatics_magnetostatics/debye_hueckel.hpp +++ b/src/core/electrostatics_magnetostatics/debye_hueckel.hpp @@ -56,32 +56,32 @@ int dh_set_params(double kappa, double r_cut); @param dist Distance between p1 and p2. @param force returns the force on particle 1. */ -inline void add_dh_coulomb_pair_force(Particle *p1, Particle *p2, - double const d[3], double dist, +inline void add_dh_coulomb_pair_force(double const q1q2, + double const d[3], double const dist, double force[3]) { if (dist < dh_params.r_cut) { double fac; if (dh_params.kappa > 0.0) { /* debye hueckel case: */ double kappa_dist = dh_params.kappa * dist; - fac = coulomb.prefactor * p1->p.q * p2->p.q * + fac = coulomb.prefactor * q1q2 * (exp(-kappa_dist) / (dist * dist * dist)) * (1.0 + kappa_dist); } else { /* pure Coulomb case: */ - fac = coulomb.prefactor * p1->p.q * p2->p.q / (dist * dist * dist); + fac = coulomb.prefactor * q1q2 / (dist * dist * dist); } for (int j = 0; j < 3; j++) force[j] += fac * d[j]; } } -inline double dh_coulomb_pair_energy(Particle *p1, Particle *p2, double dist) { +inline double dh_coulomb_pair_energy(double const q1q2, double dist) { if (dist < dh_params.r_cut) { if (dh_params.kappa > 0.0) - return coulomb.prefactor * p1->p.q * p2->p.q * + return coulomb.prefactor * q1q2 * exp(-dh_params.kappa * dist) / dist; - return coulomb.prefactor * p1->p.q * p2->p.q / dist; + return coulomb.prefactor * q1q2 / dist; } return 0.0; } diff --git a/src/core/electrostatics_magnetostatics/reaction_field.hpp b/src/core/electrostatics_magnetostatics/reaction_field.hpp index ac3aa85c1e7..bd5bd41a9a3 100644 --- a/src/core/electrostatics_magnetostatics/reaction_field.hpp +++ b/src/core/electrostatics_magnetostatics/reaction_field.hpp @@ -57,15 +57,14 @@ extern Reaction_field_params rf_params; /// int rf_set_params(double kappa, double epsilon1, double epsilon2, double r_cut); -inline void add_rf_coulomb_pair_force_no_cutoff(const Particle *const p1, - const Particle *const p2, +inline void add_rf_coulomb_pair_force_no_cutoff(double const q1q2, double const d[3], double const dist, double force[3]) { int j; double fac; fac = 1.0 / (dist * dist * dist) + rf_params.B / (rf_params.r_cut * rf_params.r_cut * rf_params.r_cut); - fac *= p1->p.q * p2->p.q; + fac *= q1q2; for (j = 0; j < 3; j++) force[j] += fac * d[j]; @@ -79,29 +78,27 @@ inline void add_rf_coulomb_pair_force_no_cutoff(const Particle *const p1, @param dist Distance between p1 and p2. @param force returns the force on particle 1. */ -inline void add_rf_coulomb_pair_force(Particle *p1, Particle *p2, double const d[3], +inline void add_rf_coulomb_pair_force(double const q1q2, double const d[3], double const dist, double force[3]) { if (dist < rf_params.r_cut) { - add_rf_coulomb_pair_force_no_cutoff(p1, p2, d, dist, force); + add_rf_coulomb_pair_force_no_cutoff(q1q2, d, dist, force); } } -inline double rf_coulomb_pair_energy_no_cutoff(const Particle *p1, - const Particle *p2, - double dist) { +inline double rf_coulomb_pair_energy_no_cutoff(double const q1q2, double const dist) { double fac; fac = 1.0 / dist - (rf_params.B * dist * dist) / (2 * rf_params.r_cut * rf_params.r_cut * rf_params.r_cut); // cut off part fac -= (1 - rf_params.B / 2) / rf_params.r_cut; - fac *= p1->p.q * p2->p.q; + fac *= q1q2; return fac; } -inline double rf_coulomb_pair_energy(Particle *p1, Particle *p2, double dist) { +inline double rf_coulomb_pair_energy(double const q1q2, double const dist) { if (dist < rf_params.r_cut) { - return rf_coulomb_pair_energy_no_cutoff(p1, p2, dist); + return rf_coulomb_pair_energy_no_cutoff(q1q2, dist); } return 0.0; } diff --git a/testsuite/python/electrostaticInteractions.py b/testsuite/python/electrostaticInteractions.py index 221f26cac5c..50251e56f7c 100644 --- a/testsuite/python/electrostaticInteractions.py +++ b/testsuite/python/electrostaticInteractions.py @@ -90,8 +90,8 @@ def test_p3m(self): self.system.actors.remove(p3m) def test_dh(self): - dh_params = dict(prefactor=1.0, - kappa=2.0, + dh_params = dict(prefactor=1.2, + kappa=0.8, r_cut=2.0) test_DH = tests_common.generate_test_for_class( self.system, diff --git a/testsuite/python/interactions_bonded.py b/testsuite/python/interactions_bonded.py index 6f67a2c6122..829a0e2ba4d 100644 --- a/testsuite/python/interactions_bonded.py +++ b/testsuite/python/interactions_bonded.py @@ -22,6 +22,7 @@ import numpy as np import espressomd +import espressomd.electrostatics import tests_common @@ -92,6 +93,26 @@ def test_coulomb(self): lambda r: tests_common.coulomb_force(r, coulomb_k, q1, q2), lambda r: tests_common.coulomb_potential(r, coulomb_k, q1, q2), 0.01, self.system.box_l[0] / 3) + + @ut.skipIf(not espressomd.has_features(["ELECTROSTATICS"]), + "ELECTROSTATICS feature is not available, skipping coulomb short range test.") + def test_coulomb_sr(self): + #with negated actual charges and only short range int: cancels out all interactions + q1 = 1.2 + q2 = -q1 + self.system.part[0].q = q1 + self.system.part[1].q = q2 + r_cut = 2 + + sr_solver = espressomd.electrostatics.DH(prefactor = 2, kappa = 0.8, r_cut = r_cut) + self.system.actors.add(sr_solver) + coulomb_sr = espressomd.interactions.BondedCoulombSRBond(q1q2 = - q1*q2) + + self.run_test(coulomb_sr, lambda r: [0.,0.,0.], lambda r: 0, 0.01, r_cut) + + + + def run_test( self, @@ -121,7 +142,7 @@ def run_test( f1_ref = self.axis * force_func(dist) # Check that energies match, ... - np.testing.assert_almost_equal(E_sim, E_ref) + #np.testing.assert_almost_equal(E_sim, E_ref) # force equals minus the counter-force ... np.testing.assert_allclose(f0_sim, -f1_sim, 1E-12) # and has correct value. From c84490187bfa8f593824931522a1f19c7d3f6903 Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Fri, 12 Apr 2019 11:03:29 +0200 Subject: [PATCH 06/16] coulomb prefactor, consts --- .../bonded_interactions/bonded_coulomb_sr.hpp | 1 - .../coulomb_inline.hpp | 9 +++---- .../debye_hueckel.hpp | 2 +- .../reaction_field.hpp | 5 ++-- testsuite/python/interactions_bonded.py | 26 ++++++++++--------- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/core/bonded_interactions/bonded_coulomb_sr.hpp b/src/core/bonded_interactions/bonded_coulomb_sr.hpp index 7cc43f56729..70c0d8ae072 100644 --- a/src/core/bonded_interactions/bonded_coulomb_sr.hpp +++ b/src/core/bonded_interactions/bonded_coulomb_sr.hpp @@ -99,7 +99,6 @@ bonded_coulomb_sr_pair_energy(Particle *p1, Particle *p2, double dist2 = sqrlen(dx); double dist = sqrt(dist2); *_energy = Coulomb::add_pair_energy(p1, p2, iaparams->p.bonded_coulomb_sr.q1q2, dx, dist, dist2); - return 0; } diff --git a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp index 0d703e587c8..e9ae7a305ce 100644 --- a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp +++ b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp @@ -68,7 +68,7 @@ inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, break; } - force += coulomb.prefactor * f; + force += f; } } @@ -107,8 +107,8 @@ inline void add_pair_pressure(Particle *p1, Particle *p2, double q1q2, double *d } // energy_inline -inline double add_pair_energy(Particle *p1, Particle *p2, double q1q2, - double *d, double dist, double dist2) { +inline double add_pair_energy(Particle *p1, Particle *p2, double const q1q2, + double *d, double const dist, double const dist2) { /* real space Coulomb */ auto E = [&]() { switch (coulomb.method) { @@ -140,8 +140,7 @@ inline double add_pair_energy(Particle *p1, Particle *p2, double q1q2, return 0.; } }(); - - return coulomb.prefactor * E; + return E; } } // namespace Coulomb diff --git a/src/core/electrostatics_magnetostatics/debye_hueckel.hpp b/src/core/electrostatics_magnetostatics/debye_hueckel.hpp index 99df95d6ecd..d7ffe6d9006 100644 --- a/src/core/electrostatics_magnetostatics/debye_hueckel.hpp +++ b/src/core/electrostatics_magnetostatics/debye_hueckel.hpp @@ -75,7 +75,7 @@ inline void add_dh_coulomb_pair_force(double const q1q2, } } -inline double dh_coulomb_pair_energy(double const q1q2, double dist) { +inline double dh_coulomb_pair_energy(double const q1q2, double const dist) { if (dist < dh_params.r_cut) { if (dh_params.kappa > 0.0) return coulomb.prefactor * q1q2 * diff --git a/src/core/electrostatics_magnetostatics/reaction_field.hpp b/src/core/electrostatics_magnetostatics/reaction_field.hpp index bd5bd41a9a3..fc7ef6f3f34 100644 --- a/src/core/electrostatics_magnetostatics/reaction_field.hpp +++ b/src/core/electrostatics_magnetostatics/reaction_field.hpp @@ -32,6 +32,7 @@ #ifdef ELECTROSTATICS #include "particle_data.hpp" +#include "electrostatics_magnetostatics/coulomb.hpp" /** Structure to hold Reaction Field Parameters. */ typedef struct { @@ -64,7 +65,7 @@ inline void add_rf_coulomb_pair_force_no_cutoff(double const q1q2, double fac; fac = 1.0 / (dist * dist * dist) + rf_params.B / (rf_params.r_cut * rf_params.r_cut * rf_params.r_cut); - fac *= q1q2; + fac *= q1q2 * coulomb.prefactor; for (j = 0; j < 3; j++) force[j] += fac * d[j]; @@ -92,7 +93,7 @@ inline double rf_coulomb_pair_energy_no_cutoff(double const q1q2, double const d (2 * rf_params.r_cut * rf_params.r_cut * rf_params.r_cut); // cut off part fac -= (1 - rf_params.B / 2) / rf_params.r_cut; - fac *= q1q2; + fac *= q1q2 * coulomb.prefactor; return fac; } diff --git a/testsuite/python/interactions_bonded.py b/testsuite/python/interactions_bonded.py index 829a0e2ba4d..e305de9d386 100644 --- a/testsuite/python/interactions_bonded.py +++ b/testsuite/python/interactions_bonded.py @@ -72,12 +72,12 @@ def test_fene(self): fene = espressomd.interactions.FeneBond( k=fene_k, d_r_max=fene_d_r_max, r_0=fene_r_0) - self.run_test(fene, - lambda r: tests_common.fene_force( - scalar_r=r, k=fene_k, d_r_max=fene_d_r_max, r_0=fene_r_0), - lambda r: tests_common.fene_potential( - scalar_r=r, k=fene_k, d_r_max=fene_d_r_max, r_0=fene_r_0), - 0.01, fene_r_0 + fene_d_r_max, True) + #self.run_test(fene, + #lambda r: tests_common.fene_force( + #scalar_r=r, k=fene_k, d_r_max=fene_d_r_max, r_0=fene_r_0), + #lambda r: tests_common.fene_potential( + #scalar_r=r, k=fene_k, d_r_max=fene_d_r_max, r_0=fene_r_0), + #0.01, fene_r_0 + fene_d_r_max, True) @ut.skipIf(not espressomd.has_features(["ELECTROSTATICS"]), "ELECTROSTATICS feature is not available, skipping coulomb test.") @@ -87,13 +87,14 @@ def test_coulomb(self): q2 = -1 self.system.part[0].q = q1 self.system.part[1].q = q2 - self.run_test( espressomd.interactions.BondedCoulomb(prefactor=coulomb_k), lambda r: tests_common.coulomb_force(r, coulomb_k, q1, q2), lambda r: tests_common.coulomb_potential(r, coulomb_k, q1, q2), 0.01, self.system.box_l[0] / 3) + + @ut.skipIf(not espressomd.has_features(["ELECTROSTATICS"]), "ELECTROSTATICS feature is not available, skipping coulomb short range test.") def test_coulomb_sr(self): @@ -108,7 +109,8 @@ def test_coulomb_sr(self): self.system.actors.add(sr_solver) coulomb_sr = espressomd.interactions.BondedCoulombSRBond(q1q2 = - q1*q2) - self.run_test(coulomb_sr, lambda r: [0.,0.,0.], lambda r: 0, 0.01, r_cut) + #no break test, bond can't break. it extends as far as the short range part of the electrostatics actor + self.run_test(coulomb_sr, lambda r: [0.,0.,0.], lambda r: 0, 0.01, r_cut, test_breakage=False) @@ -133,7 +135,7 @@ def run_test( self.system.integrator.run(recalc_forces=True, steps=0) # Calculate energies - E_sim = self.system.analysis.energy()["bonded"] + E_sim = self.system.analysis.energy()["total"] E_ref = energy_func(dist) # Calculate forces @@ -142,7 +144,7 @@ def run_test( f1_ref = self.axis * force_func(dist) # Check that energies match, ... - #np.testing.assert_almost_equal(E_sim, E_ref) + self.assertAlmostEqual(E_sim, E_ref) # force equals minus the counter-force ... np.testing.assert_allclose(f0_sim, -f1_sim, 1E-12) # and has correct value. @@ -154,14 +156,14 @@ def run_test( # where F is the force between the particles and r their distance p_expected = 1. / 3. * \ np.dot(f1_sim, self.axis * dist) / self.system.volume() - p_sim = self.system.analysis.pressure()["bonded"] + p_sim = self.system.analysis.pressure()["total"] self.assertAlmostEqual(p_sim, p_expected, delta=1E-12) # Pressure tensor # P_ij = 1/V F_i r_j p_tensor_expected = np.outer( f1_sim, self.axis * dist) / self.system.volume() - p_tensor_sim = self.system.analysis.stress_tensor()["bonded"] + p_tensor_sim = self.system.analysis.stress_tensor()["total"] np.testing.assert_allclose( p_tensor_sim, p_tensor_expected, atol=1E-12) if test_breakage: From d79e2517323e3b9a57c1da4d458a67c0ded7bb50 Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Fri, 12 Apr 2019 11:41:30 +0200 Subject: [PATCH 07/16] coulomb_inline: restored prefactor for p3m --- .../electrostatics_magnetostatics/coulomb_inline.hpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp index e9ae7a305ce..b55be145b5e 100644 --- a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp +++ b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp @@ -26,6 +26,8 @@ inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, #ifdef P3M case COULOMB_ELC_P3M: { p3m_add_pair_force(q1q2, d, dist2, dist, f.data()); + //TODO some of the force functions include the prefactor, some don't + f *= coulomb.prefactor; // forces from the virtual charges // they go directly onto the particles, since they are not pairwise forces @@ -44,6 +46,7 @@ inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, case COULOMB_P3M_GPU: case COULOMB_P3M: { p3m_add_pair_force(q1q2, d, dist2, dist, f.data()); + f *= coulomb.prefactor; break; } #endif @@ -115,13 +118,14 @@ inline double add_pair_energy(Particle *p1, Particle *p2, double const q1q2, #ifdef P3M case COULOMB_P3M_GPU: case COULOMB_P3M: - return p3m_pair_energy(q1q2, dist); + //TODO some energy functions include the prefactor, some don't + return coulomb.prefactor * p3m_pair_energy(q1q2, dist); case COULOMB_ELC_P3M: if (elc_params.dielectric_contrast_on) { - return 0.5 * ELC_P3M_dielectric_layers_energy_contribution(p1, p2) + - p3m_pair_energy(q1q2, dist); + return coulomb.prefactor * (0.5 * ELC_P3M_dielectric_layers_energy_contribution(p1, p2) + + p3m_pair_energy(q1q2, dist)); } else { - return p3m_pair_energy(q1q2, dist); + return coulomb.prefactor * p3m_pair_energy(q1q2, dist); } #endif #ifdef SCAFACOS From d11cee79bed281caf1bcd94a74da770360a45b49 Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Fri, 12 Apr 2019 11:57:40 +0200 Subject: [PATCH 08/16] removed const because of scafacos --- src/core/electrostatics_magnetostatics/coulomb_inline.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp index b55be145b5e..516d90d2631 100644 --- a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp +++ b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp @@ -16,7 +16,7 @@ namespace Coulomb { // forces_inline inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, - double const *d, double const dist, + double const *d, double dist, double const dist2, Vector3d &force) { if (q1q2 != 0) { @@ -111,7 +111,7 @@ inline void add_pair_pressure(Particle *p1, Particle *p2, double q1q2, double *d // energy_inline inline double add_pair_energy(Particle *p1, Particle *p2, double const q1q2, - double *d, double const dist, double const dist2) { + double *d, double dist, double const dist2) { /* real space Coulomb */ auto E = [&]() { switch (coulomb.method) { From fa28888e747f2aa851589904966eb61fe72228d6 Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Fri, 12 Apr 2019 12:57:31 +0200 Subject: [PATCH 09/16] removed const because of scafacos --- src/core/electrostatics_magnetostatics/coulomb_inline.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp index 516d90d2631..aa100a8c7cf 100644 --- a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp +++ b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp @@ -16,7 +16,7 @@ namespace Coulomb { // forces_inline inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, - double const *d, double dist, + double *d, dist, double const dist2, Vector3d &force) { if (q1q2 != 0) { @@ -111,7 +111,7 @@ inline void add_pair_pressure(Particle *p1, Particle *p2, double q1q2, double *d // energy_inline inline double add_pair_energy(Particle *p1, Particle *p2, double const q1q2, - double *d, double dist, double const dist2) { + double *d, double dist, double dist2) { /* real space Coulomb */ auto E = [&]() { switch (coulomb.method) { From b99eb6e8fb07f25fd31ff6b2db1a3e5c2002ac31 Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Fri, 12 Apr 2019 13:04:34 +0200 Subject: [PATCH 10/16] even more consts removed --- src/core/bonded_interactions/bonded_coulomb_sr.hpp | 2 +- src/core/electrostatics_magnetostatics/coulomb_inline.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/bonded_interactions/bonded_coulomb_sr.hpp b/src/core/bonded_interactions/bonded_coulomb_sr.hpp index 70c0d8ae072..6513db9666f 100644 --- a/src/core/bonded_interactions/bonded_coulomb_sr.hpp +++ b/src/core/bonded_interactions/bonded_coulomb_sr.hpp @@ -57,7 +57,7 @@ int bonded_coulomb_sr_set_params(int bond_type, double q1q2); inline int calc_bonded_coulomb_sr_pair_force(Particle *p1, Particle *p2, Bonded_ia_parameters const *iaparams, - double const dx[3], double force[3]) { + double dx[3], double force[3]) { double dist2 = sqrlen(dx); double dist = sqrt(dist2); if (dist < coulomb_cutoff) { diff --git a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp index aa100a8c7cf..89902ed7239 100644 --- a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp +++ b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp @@ -16,7 +16,7 @@ namespace Coulomb { // forces_inline inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, - double *d, dist, + double *d, double dist, double const dist2, Vector3d &force) { if (q1q2 != 0) { From ba939c6015f7ae4a76d8d2786676a4784fca22e7 Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Fri, 12 Apr 2019 15:33:35 +0200 Subject: [PATCH 11/16] coulomb.prefactor for scafacos --- .../coulomb_inline.hpp | 3 +- src/python/espressomd/drude_helpers.py | 32 +++++++++---------- testsuite/python/drude.py | 2 +- testsuite/python/electrostaticInteractions.py | 8 +++-- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp index 89902ed7239..a2f8720c3d1 100644 --- a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp +++ b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp @@ -65,6 +65,7 @@ inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, #ifdef SCAFACOS case COULOMB_SCAFACOS: Scafacos::add_pair_force(p1, p2, d, dist, f.data()); + f *= coulomb.prefactor; break; #endif default: @@ -130,7 +131,7 @@ inline double add_pair_energy(Particle *p1, Particle *p2, double const q1q2, #endif #ifdef SCAFACOS case COULOMB_SCAFACOS: - return Scafacos::pair_energy(p1, p2, dist); + return coulomb.prefactor * Scafacos::pair_energy(p1, p2, dist); #endif case COULOMB_DH: return dh_coulomb_pair_energy(q1q2, dist); diff --git a/src/python/espressomd/drude_helpers.py b/src/python/espressomd/drude_helpers.py index 958bd7cad4f..f23aba103b6 100644 --- a/src/python/espressomd/drude_helpers.py +++ b/src/python/espressomd/drude_helpers.py @@ -203,22 +203,22 @@ def setup_and_add_drude_exclusion_bonds(system, verbose=False): #...exclusions with core qd = drude_dict[td]["q"] # Drude charge qc = drude_dict[td]["qc"] # Core charge - subtr_p3m_sr_bond = espressomd.interactions.BondedCoulombP3MSRBond( + subtr_sr_bond = espressomd.interactions.BondedCoulombSRBond( q1q2=-qd * qc) - system.bonded_inter.add(subtr_p3m_sr_bond) - drude_dict[td]["subtr_p3m_sr_bonds_drude-core"] = subtr_p3m_sr_bond + system.bonded_inter.add(subtr_sr_bond) + drude_dict[td]["subtr_sr_bonds_drude-core"] = subtr_sr_bond if verbose: - print("Added drude-core p3m SR exclusion bond ", - subtr_p3m_sr_bond, "for drude", qd, "<-> core", qc, "to system") + print("Added drude-core SR exclusion bond ", + subtr_sr_bond, "for drude", qd, "<-> core", qc, "to system") for drude_id in drude_id_list: core_id = core_id_from_drude_id[drude_id] pd = system.part[drude_id] pc = system.part[core_id] - bond = drude_dict[pd.type]["subtr_p3m_sr_bonds_drude-core"] + bond = drude_dict[pd.type]["subtr_sr_bonds_drude-core"] pc.add_bond((bond, drude_id)) if verbose: - print("Added drude-core p3m SR bond", bond, + print("Added drude-core SR bond", bond, "between ids", drude_id, "and", core_id) @@ -243,20 +243,20 @@ def setup_intramol_exclusion_bonds(system, mol_drude_types, mol_core_types, # All Drude types need... for td in mol_drude_types: - drude_dict[td]["subtr_p3m_sr_bonds_intramol"] = {} + drude_dict[td]["subtr_sr_bonds_intramol"] = {} - #...p3m sr exclusion bond with other partial core charges... + #... sr exclusion bond with other partial core charges... for tc, qp in zip(mol_core_types, mol_core_partial_charges): #...excluding the Drude core partner if drude_dict[td]["core_type"] != tc: qd = drude_dict[td]["q"] # Drude charge - subtr_p3m_sr_bond = espressomd.interactions.BondedCoulombP3MSRBond( + subtr_sr_bond = espressomd.interactions.BondedCoulombSRBond( q1q2=-qd * qp) - system.bonded_inter.add(subtr_p3m_sr_bond) - drude_dict[td]["subtr_p3m_sr_bonds_intramol"][ - tc] = subtr_p3m_sr_bond + system.bonded_inter.add(subtr_sr_bond) + drude_dict[td]["subtr_sr_bonds_intramol"][ + tc] = subtr_sr_bond if verbose: - print("Added intramolecular exclusion", subtr_p3m_sr_bond, + print("Added intramolecular exclusion", subtr_sr_bond, "for drude", qd, "<-> core", qp, "to system") @@ -282,8 +282,8 @@ def add_intramol_exclusion_bonds(system, drude_ids, core_ids, verbose=False): pd = system.part[drude_id] pc = system.part[core_id] bond = drude_dict[pd.type][ - "subtr_p3m_sr_bonds_intramol"][pc.type] + "subtr_sr_bonds_intramol"][pc.type] pd.add_bond((bond, core_id)) if verbose: - print("Added subtr_p3m_sr bond", bond, + print("Added subtr_sr bond", bond, "between ids", drude_id, "and", core_id) diff --git a/testsuite/python/drude.py b/testsuite/python/drude.py index 0d702966305..51f6bd92e47 100644 --- a/testsuite/python/drude.py +++ b/testsuite/python/drude.py @@ -25,7 +25,7 @@ class Drude(ut.TestCase): - @ut.skipIf(not espressomd.has_features("P3M", "THOLE", "LANGEVIN_PER_PARTICLE"), "Test needs P3M, THOLE and LANGEVIN_PER_PARTICLE") + @ut.skipIf(not espressomd.has_features("P3M", "ELECTROSTATICS", "THOLE", "LANGEVIN_PER_PARTICLE", "MASS"), "Test needs P3M, ELECTROSTATICS, THOLE and LANGEVIN_PER_PARTICLE") def test(self): """ Sets up a BMIM PF6 pair separated in y-direction with fixed cores. diff --git a/testsuite/python/electrostaticInteractions.py b/testsuite/python/electrostaticInteractions.py index 50251e56f7c..ca7995799fb 100644 --- a/testsuite/python/electrostaticInteractions.py +++ b/testsuite/python/electrostaticInteractions.py @@ -56,11 +56,13 @@ def calc_dh_potential(self, r, df_params): @ut.skipIf(not espressomd.has_features(["P3M"]), "Features not available, skipping test!") def test_p3m(self): + prefactor = 1.1 self.system.part[0].pos = [1.0, 2.0, 2.0] self.system.part[1].pos = [3.0, 2.0, 2.0] # results, - p3m_energy = -0.501062398379 - p3m_force = 2.48921612e-01 + #reference values for energy and force only calculated for prefactor = 1 + p3m_energy = -0.501062398379 *prefactor + p3m_force = 2.48921612e-01 *prefactor test_P3M = tests_common.generate_test_for_class( self.system, electrostatics.P3M, @@ -71,7 +73,7 @@ def test_p3m(self): r_cut=8.906249999999998, alpha=0.387611049779351, tune=False)) - p3m = espressomd.electrostatics.P3M(prefactor=1.0, + p3m = espressomd.electrostatics.P3M(prefactor=prefactor, accuracy=9.910945054074526e-08, mesh=[22, 22, 22], cao=7, From 725a8f1c9dd2a509b968a1d2e0f25ab2e13129b4 Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Fri, 12 Apr 2019 16:23:30 +0200 Subject: [PATCH 12/16] moved coulomb.prefactor stuff to coulomb_inline --- .../coulomb_inline.hpp | 20 ++++++++----------- .../debye_hueckel.hpp | 11 ++++------ .../electrostatics_magnetostatics/mmm1d.cpp | 13 ++++++------ .../electrostatics_magnetostatics/mmm1d.hpp | 2 +- .../electrostatics_magnetostatics/mmm2d.cpp | 2 +- .../reaction_field.hpp | 5 ++--- 6 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp index a2f8720c3d1..f569de849dc 100644 --- a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp +++ b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp @@ -26,8 +26,6 @@ inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, #ifdef P3M case COULOMB_ELC_P3M: { p3m_add_pair_force(q1q2, d, dist2, dist, f.data()); - //TODO some of the force functions include the prefactor, some don't - f *= coulomb.prefactor; // forces from the virtual charges // they go directly onto the particles, since they are not pairwise forces @@ -46,7 +44,6 @@ inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, case COULOMB_P3M_GPU: case COULOMB_P3M: { p3m_add_pair_force(q1q2, d, dist2, dist, f.data()); - f *= coulomb.prefactor; break; } #endif @@ -65,14 +62,13 @@ inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, #ifdef SCAFACOS case COULOMB_SCAFACOS: Scafacos::add_pair_force(p1, p2, d, dist, f.data()); - f *= coulomb.prefactor; break; #endif default: break; } - force += f; + force += coulomb.prefactor * f; } } @@ -120,32 +116,32 @@ inline double add_pair_energy(Particle *p1, Particle *p2, double const q1q2, case COULOMB_P3M_GPU: case COULOMB_P3M: //TODO some energy functions include the prefactor, some don't - return coulomb.prefactor * p3m_pair_energy(q1q2, dist); + return p3m_pair_energy(q1q2, dist); case COULOMB_ELC_P3M: if (elc_params.dielectric_contrast_on) { - return coulomb.prefactor * (0.5 * ELC_P3M_dielectric_layers_energy_contribution(p1, p2) + - p3m_pair_energy(q1q2, dist)); + return 0.5 * ELC_P3M_dielectric_layers_energy_contribution(p1, p2) + + p3m_pair_energy(q1q2, dist); } else { - return coulomb.prefactor * p3m_pair_energy(q1q2, dist); + return p3m_pair_energy(q1q2, dist); } #endif #ifdef SCAFACOS case COULOMB_SCAFACOS: - return coulomb.prefactor * Scafacos::pair_energy(p1, p2, dist); + return Scafacos::pair_energy(p1, p2, dist); #endif case COULOMB_DH: return dh_coulomb_pair_energy(q1q2, dist); case COULOMB_RF: return rf_coulomb_pair_energy(q1q2, dist); case COULOMB_MMM1D: - return mmm1d_coulomb_pair_energy(p1, p2, d, dist2, dist); + return mmm1d_coulomb_pair_energy(q1q2, d, dist2, dist); case COULOMB_MMM2D: return mmm2d_coulomb_pair_energy(q1q2, d, dist2, dist); default: return 0.; } }(); - return E; + return coulomb.prefactor * E; } } // namespace Coulomb diff --git a/src/core/electrostatics_magnetostatics/debye_hueckel.hpp b/src/core/electrostatics_magnetostatics/debye_hueckel.hpp index d7ffe6d9006..9eacd5e33b3 100644 --- a/src/core/electrostatics_magnetostatics/debye_hueckel.hpp +++ b/src/core/electrostatics_magnetostatics/debye_hueckel.hpp @@ -25,7 +25,6 @@ * for a particle pair. */ #include "config.hpp" -#include "electrostatics_magnetostatics/coulomb.hpp" #ifdef ELECTROSTATICS @@ -64,11 +63,10 @@ inline void add_dh_coulomb_pair_force(double const q1q2, if (dh_params.kappa > 0.0) { /* debye hueckel case: */ double kappa_dist = dh_params.kappa * dist; - fac = coulomb.prefactor * q1q2 * - (exp(-kappa_dist) / (dist * dist * dist)) * (1.0 + kappa_dist); + fac = q1q2 * (exp(-kappa_dist) / (dist * dist * dist)) * (1.0 + kappa_dist); } else { /* pure Coulomb case: */ - fac = coulomb.prefactor * q1q2 / (dist * dist * dist); + fac = q1q2 / (dist * dist * dist); } for (int j = 0; j < 3; j++) force[j] += fac * d[j]; @@ -78,10 +76,9 @@ inline void add_dh_coulomb_pair_force(double const q1q2, inline double dh_coulomb_pair_energy(double const q1q2, double const dist) { if (dist < dh_params.r_cut) { if (dh_params.kappa > 0.0) - return coulomb.prefactor * q1q2 * - exp(-dh_params.kappa * dist) / dist; + return q1q2 * exp(-dh_params.kappa * dist) / dist; - return coulomb.prefactor * q1q2 / dist; + return q1q2 / dist; } return 0.0; } diff --git a/src/core/electrostatics_magnetostatics/mmm1d.cpp b/src/core/electrostatics_magnetostatics/mmm1d.cpp index 9b332801a8b..3e2cf9e2727 100644 --- a/src/core/electrostatics_magnetostatics/mmm1d.cpp +++ b/src/core/electrostatics_magnetostatics/mmm1d.cpp @@ -282,9 +282,8 @@ void add_mmm1d_coulomb_pair_force(double chpref, double const d[3], double r2, force[dim] += chpref * F[dim]; } -double mmm1d_coulomb_pair_energy(Particle *p1, Particle *p2, double const d[3], +double mmm1d_coulomb_pair_energy(double const chpref, double const d[3], double r2, double r) { - double chpref = p1->p.q * p2->p.q; double rxy2, rxy2_d, z_d; double E; @@ -313,19 +312,19 @@ double mmm1d_coulomb_pair_energy(Particle *p1, Particle *p2, double const d[3], r2n *= rxy2_d; } - E *= coulomb.prefactor * uz; + E *= uz; /* real space parts */ - E += coulomb.prefactor / r; + E += 1 / r; shift_z = d[2] + box_l[2]; rt = sqrt(rxy2 + shift_z * shift_z); - E += coulomb.prefactor / rt; + E += 1 / rt; shift_z = d[2] - box_l[2]; rt = sqrt(rxy2 + shift_z * shift_z); - E += coulomb.prefactor / rt; + E += 1 / rt; } else { /* far range formula */ double rxy = sqrt(rxy2); @@ -341,7 +340,7 @@ double mmm1d_coulomb_pair_energy(Particle *p1, Particle *p2, double const d[3], double fq = C_2PI * bp; E += K0(fq * rxy_d) * cos(fq * z_d); } - E *= 4 * coulomb.prefactor * uz; + E *= 4 * uz; } return chpref * E; diff --git a/src/core/electrostatics_magnetostatics/mmm1d.hpp b/src/core/electrostatics_magnetostatics/mmm1d.hpp index 220e8386517..cb6cdc2a0d1 100644 --- a/src/core/electrostatics_magnetostatics/mmm1d.hpp +++ b/src/core/electrostatics_magnetostatics/mmm1d.hpp @@ -68,7 +68,7 @@ void MMM1D_init(); void add_mmm1d_coulomb_pair_force(double chprf, double const d[3], double dist2, double dist, double force[3]); -double mmm1d_coulomb_pair_energy(Particle *p1, Particle *p2, double const d[3], +double mmm1d_coulomb_pair_energy(double const q1q2, double const d[3], double r2, double r); /** Tuning of the parameters which are not set by the user, e.g. the diff --git a/src/core/electrostatics_magnetostatics/mmm2d.cpp b/src/core/electrostatics_magnetostatics/mmm2d.cpp index f18744caf7f..be8c3bfe755 100644 --- a/src/core/electrostatics_magnetostatics/mmm2d.cpp +++ b/src/core/electrostatics_magnetostatics/mmm2d.cpp @@ -1682,7 +1682,7 @@ inline double calc_mmm2d_copy_pair_energy(double const d[3]) { double mmm2d_coulomb_pair_energy(double charge_factor, double dv[3], double, double d) { - double eng, pref = coulomb.prefactor * charge_factor; + double eng, pref = charge_factor; if (pref != 0.0) { eng = calc_mmm2d_copy_pair_energy(dv); return pref * (eng + 1 / d); diff --git a/src/core/electrostatics_magnetostatics/reaction_field.hpp b/src/core/electrostatics_magnetostatics/reaction_field.hpp index fc7ef6f3f34..bd5bd41a9a3 100644 --- a/src/core/electrostatics_magnetostatics/reaction_field.hpp +++ b/src/core/electrostatics_magnetostatics/reaction_field.hpp @@ -32,7 +32,6 @@ #ifdef ELECTROSTATICS #include "particle_data.hpp" -#include "electrostatics_magnetostatics/coulomb.hpp" /** Structure to hold Reaction Field Parameters. */ typedef struct { @@ -65,7 +64,7 @@ inline void add_rf_coulomb_pair_force_no_cutoff(double const q1q2, double fac; fac = 1.0 / (dist * dist * dist) + rf_params.B / (rf_params.r_cut * rf_params.r_cut * rf_params.r_cut); - fac *= q1q2 * coulomb.prefactor; + fac *= q1q2; for (j = 0; j < 3; j++) force[j] += fac * d[j]; @@ -93,7 +92,7 @@ inline double rf_coulomb_pair_energy_no_cutoff(double const q1q2, double const d (2 * rf_params.r_cut * rf_params.r_cut * rf_params.r_cut); // cut off part fac -= (1 - rf_params.B / 2) / rf_params.r_cut; - fac *= q1q2 * coulomb.prefactor; + fac *= q1q2; return fac; } From 9eeecb64f22dfb58964701740f27c3023e583ad5 Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Fri, 12 Apr 2019 16:59:46 +0200 Subject: [PATCH 13/16] removed accidental comments --- testsuite/python/interactions_bonded.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/testsuite/python/interactions_bonded.py b/testsuite/python/interactions_bonded.py index e305de9d386..df94cd35f67 100644 --- a/testsuite/python/interactions_bonded.py +++ b/testsuite/python/interactions_bonded.py @@ -72,12 +72,12 @@ def test_fene(self): fene = espressomd.interactions.FeneBond( k=fene_k, d_r_max=fene_d_r_max, r_0=fene_r_0) - #self.run_test(fene, - #lambda r: tests_common.fene_force( - #scalar_r=r, k=fene_k, d_r_max=fene_d_r_max, r_0=fene_r_0), - #lambda r: tests_common.fene_potential( - #scalar_r=r, k=fene_k, d_r_max=fene_d_r_max, r_0=fene_r_0), - #0.01, fene_r_0 + fene_d_r_max, True) + self.run_test(fene, + lambda r: tests_common.fene_force( + scalar_r=r, k=fene_k, d_r_max=fene_d_r_max, r_0=fene_r_0), + lambda r: tests_common.fene_potential( + scalar_r=r, k=fene_k, d_r_max=fene_d_r_max, r_0=fene_r_0), + 0.01, fene_r_0 + fene_d_r_max, True) @ut.skipIf(not espressomd.has_features(["ELECTROSTATICS"]), "ELECTROSTATICS feature is not available, skipping coulomb test.") From 9ab6e24f494f461bc4642659b70fb19c16b6bf2a Mon Sep 17 00:00:00 2001 From: Christoph Lohrmann Date: Fri, 12 Apr 2019 17:20:22 +0200 Subject: [PATCH 14/16] formatting --- .../bonded_interactions/bonded_coulomb_sr.hpp | 23 ++++++------ .../bonded_interaction_data.hpp | 1 - .../coulomb_inline.hpp | 16 ++++----- .../debye_hueckel.hpp | 8 ++--- .../electrostatics_magnetostatics/mmm1d.cpp | 2 +- .../reaction_field.hpp | 6 ++-- src/core/energy_inline.hpp | 8 ++--- src/core/forces_inline.hpp | 2 +- src/core/pressure_inline.hpp | 3 +- testsuite/python/electrostaticInteractions.py | 7 ++-- testsuite/python/interactions_bonded.py | 36 +++++++++---------- 11 files changed, 57 insertions(+), 55 deletions(-) diff --git a/src/core/bonded_interactions/bonded_coulomb_sr.hpp b/src/core/bonded_interactions/bonded_coulomb_sr.hpp index 6513db9666f..d7468f0e4ac 100644 --- a/src/core/bonded_interactions/bonded_coulomb_sr.hpp +++ b/src/core/bonded_interactions/bonded_coulomb_sr.hpp @@ -23,8 +23,8 @@ /** \file * Routines to calculate the BONDED_COULOMB_SR Energy or/and * BONDED_COULOMB_SR force for a particle pair. This is only the shortrange - * part of any coulomb interaction and first used to subtract certain intramolecular interactions in - * combination with Thole damping \ref forces.cpp + * part of any coulomb interaction and first used to subtract certain + * intramolecular interactions in combination with Thole damping \ref forces.cpp */ /************************************************************/ @@ -61,14 +61,15 @@ calc_bonded_coulomb_sr_pair_force(Particle *p1, Particle *p2, double dist2 = sqrlen(dx); double dist = sqrt(dist2); if (dist < coulomb_cutoff) { - //TODO ugly workaround + // TODO ugly workaround Vector3d forcevec{}; - - Coulomb::calc_pair_force(p1, p2, iaparams->p.bonded_coulomb_sr.q1q2, dx, dist, dist2, forcevec); + + Coulomb::calc_pair_force(p1, p2, iaparams->p.bonded_coulomb_sr.q1q2, dx, + dist, dist2, forcevec); force[0] = forcevec[0]; force[1] = forcevec[1]; force[2] = forcevec[2]; - + ONEPART_TRACE(if (p1->p.identity == check_id) fprintf( stderr, "%d: OPT: BONDED_COULOMB_SR f = (%.3e,%.3e,%.3e) with part id=%d " @@ -92,13 +93,13 @@ calc_bonded_coulomb_sr_pair_force(Particle *p1, Particle *p2, * @param[out] _energy Energy. * @retval 0 */ -inline int -bonded_coulomb_sr_pair_energy(Particle *p1, Particle *p2, - Bonded_ia_parameters const *iaparams, - double dx[3], double *_energy) { +inline int bonded_coulomb_sr_pair_energy(Particle *p1, Particle *p2, + Bonded_ia_parameters const *iaparams, + double dx[3], double *_energy) { double dist2 = sqrlen(dx); double dist = sqrt(dist2); - *_energy = Coulomb::add_pair_energy(p1, p2, iaparams->p.bonded_coulomb_sr.q1q2, dx, dist, dist2); + *_energy = Coulomb::add_pair_energy( + p1, p2, iaparams->p.bonded_coulomb_sr.q1q2, dx, dist, dist2); return 0; } diff --git a/src/core/bonded_interactions/bonded_interaction_data.hpp b/src/core/bonded_interactions/bonded_interaction_data.hpp index 44e179ca445..fa7b3ed42dd 100644 --- a/src/core/bonded_interactions/bonded_interaction_data.hpp +++ b/src/core/bonded_interactions/bonded_interaction_data.hpp @@ -162,7 +162,6 @@ struct Bonded_coulomb_sr_bond_parameters { double q1q2; }; - /** Parameters for three-body angular potential. * @note * ATTENTION: there are different implementations of the bond angle diff --git a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp index f569de849dc..c62d720f373 100644 --- a/src/core/electrostatics_magnetostatics/coulomb_inline.hpp +++ b/src/core/electrostatics_magnetostatics/coulomb_inline.hpp @@ -15,9 +15,9 @@ namespace Coulomb { // forces_inline -inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, - double *d, double dist, - double const dist2, Vector3d &force) { +inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, + double *d, double dist, double const dist2, + Vector3d &force) { if (q1q2 != 0) { Vector3d f{}; @@ -67,14 +67,14 @@ inline void calc_pair_force(Particle *p1, Particle *p2, double const q1q2, default: break; } - + force += coulomb.prefactor * f; } } // pressure_inline.hpp -inline void add_pair_pressure(Particle *p1, Particle *p2, double q1q2, double *d, - double dist, double dist2, +inline void add_pair_pressure(Particle *p1, Particle *p2, double q1q2, + double *d, double dist, double dist2, Observable_stat &virials, Observable_stat &p_tensor) { switch (coulomb.method) { @@ -115,8 +115,8 @@ inline double add_pair_energy(Particle *p1, Particle *p2, double const q1q2, #ifdef P3M case COULOMB_P3M_GPU: case COULOMB_P3M: - //TODO some energy functions include the prefactor, some don't - return p3m_pair_energy(q1q2, dist); + // TODO some energy functions include the prefactor, some don't + return p3m_pair_energy(q1q2, dist); case COULOMB_ELC_P3M: if (elc_params.dielectric_contrast_on) { return 0.5 * ELC_P3M_dielectric_layers_energy_contribution(p1, p2) + diff --git a/src/core/electrostatics_magnetostatics/debye_hueckel.hpp b/src/core/electrostatics_magnetostatics/debye_hueckel.hpp index 9eacd5e33b3..a59ae2ae0bd 100644 --- a/src/core/electrostatics_magnetostatics/debye_hueckel.hpp +++ b/src/core/electrostatics_magnetostatics/debye_hueckel.hpp @@ -55,15 +55,15 @@ int dh_set_params(double kappa, double r_cut); @param dist Distance between p1 and p2. @param force returns the force on particle 1. */ -inline void add_dh_coulomb_pair_force(double const q1q2, - double const d[3], double const dist, - double force[3]) { +inline void add_dh_coulomb_pair_force(double const q1q2, double const d[3], + double const dist, double force[3]) { if (dist < dh_params.r_cut) { double fac; if (dh_params.kappa > 0.0) { /* debye hueckel case: */ double kappa_dist = dh_params.kappa * dist; - fac = q1q2 * (exp(-kappa_dist) / (dist * dist * dist)) * (1.0 + kappa_dist); + fac = + q1q2 * (exp(-kappa_dist) / (dist * dist * dist)) * (1.0 + kappa_dist); } else { /* pure Coulomb case: */ fac = q1q2 / (dist * dist * dist); diff --git a/src/core/electrostatics_magnetostatics/mmm1d.cpp b/src/core/electrostatics_magnetostatics/mmm1d.cpp index 3e2cf9e2727..8d335aaaf66 100644 --- a/src/core/electrostatics_magnetostatics/mmm1d.cpp +++ b/src/core/electrostatics_magnetostatics/mmm1d.cpp @@ -312,7 +312,7 @@ double mmm1d_coulomb_pair_energy(double const chpref, double const d[3], r2n *= rxy2_d; } - E *= uz; + E *= uz; /* real space parts */ diff --git a/src/core/electrostatics_magnetostatics/reaction_field.hpp b/src/core/electrostatics_magnetostatics/reaction_field.hpp index bd5bd41a9a3..01f4bfc5b88 100644 --- a/src/core/electrostatics_magnetostatics/reaction_field.hpp +++ b/src/core/electrostatics_magnetostatics/reaction_field.hpp @@ -58,7 +58,8 @@ extern Reaction_field_params rf_params; int rf_set_params(double kappa, double epsilon1, double epsilon2, double r_cut); inline void add_rf_coulomb_pair_force_no_cutoff(double const q1q2, - double const d[3], double const dist, + double const d[3], + double const dist, double force[3]) { int j; double fac; @@ -85,7 +86,8 @@ inline void add_rf_coulomb_pair_force(double const q1q2, double const d[3], } } -inline double rf_coulomb_pair_energy_no_cutoff(double const q1q2, double const dist) { +inline double rf_coulomb_pair_energy_no_cutoff(double const q1q2, + double const dist) { double fac; fac = 1.0 / dist - (rf_params.B * dist * dist) / diff --git a/src/core/energy_inline.hpp b/src/core/energy_inline.hpp index 7a941330efe..c7360e7f64e 100644 --- a/src/core/energy_inline.hpp +++ b/src/core/energy_inline.hpp @@ -57,8 +57,8 @@ #include "nonbonded_interactions/wca.hpp" #ifdef ELECTROSTATICS #include "bonded_interactions/bonded_coulomb.hpp" -#include "electrostatics_magnetostatics/coulomb_inline.hpp" #include "bonded_interactions/bonded_coulomb_sr.hpp" +#include "electrostatics_magnetostatics/coulomb_inline.hpp" #endif #include "statistics.hpp" @@ -192,7 +192,8 @@ inline void add_non_bonded_pair_energy(Particle *p1, Particle *p2, double d[3], calc_non_bonded_pair_energy(p1, p2, ia_params, d, dist, dist2); #ifdef ELECTROSTATICS - energy.coulomb[0] += Coulomb::add_pair_energy(p1, p2, p1->p.q*p2->p.q, d, dist, dist2); + energy.coulomb[0] += + Coulomb::add_pair_energy(p1, p2, p1->p.q * p2->p.q, d, dist, dist2); #endif #ifdef DIPOLES @@ -273,8 +274,7 @@ inline void add_bonded_energy(Particle *p1) { bond_broken = bonded_coulomb_pair_energy(p1, p2, iaparams, dx, &ret); break; case BONDED_IA_BONDED_COULOMB_SR: - bond_broken = - bonded_coulomb_sr_pair_energy(p1, p2, iaparams, dx, &ret); + bond_broken = bonded_coulomb_sr_pair_energy(p1, p2, iaparams, dx, &ret); break; #endif #ifdef LENNARD_JONES diff --git a/src/core/forces_inline.hpp b/src/core/forces_inline.hpp index dea80752eb0..2ff88d318d2 100644 --- a/src/core/forces_inline.hpp +++ b/src/core/forces_inline.hpp @@ -68,8 +68,8 @@ #ifdef ELECTROSTATICS #include "bonded_interactions/bonded_coulomb.hpp" -#include "electrostatics_magnetostatics/coulomb_inline.hpp" #include "bonded_interactions/bonded_coulomb_sr.hpp" +#include "electrostatics_magnetostatics/coulomb_inline.hpp" #endif #ifdef IMMERSED_BOUNDARY #include "immersed_boundary/ibm_tribend.hpp" diff --git a/src/core/pressure_inline.hpp b/src/core/pressure_inline.hpp index 82bbe480152..42d2d8fff5c 100644 --- a/src/core/pressure_inline.hpp +++ b/src/core/pressure_inline.hpp @@ -83,7 +83,8 @@ inline void add_non_bonded_pair_virials(Particle *p1, Particle *p2, double d[3], #ifdef ELECTROSTATICS /* real space Coulomb */ if (coulomb.method != COULOMB_NONE) { - Coulomb::add_pair_pressure(p1, p2, p1->p.q * p2->p.q, d, dist, dist2, virials, p_tensor); + Coulomb::add_pair_pressure(p1, p2, p1->p.q * p2->p.q, d, dist, dist2, + virials, p_tensor); } #endif /*ifdef ELECTROSTATICS */ diff --git a/testsuite/python/electrostaticInteractions.py b/testsuite/python/electrostaticInteractions.py index ca7995799fb..52b72e2258c 100644 --- a/testsuite/python/electrostaticInteractions.py +++ b/testsuite/python/electrostaticInteractions.py @@ -60,9 +60,10 @@ def test_p3m(self): self.system.part[0].pos = [1.0, 2.0, 2.0] self.system.part[1].pos = [3.0, 2.0, 2.0] # results, - #reference values for energy and force only calculated for prefactor = 1 - p3m_energy = -0.501062398379 *prefactor - p3m_force = 2.48921612e-01 *prefactor + # reference values for energy and force only calculated for prefactor = + # 1 + p3m_energy = -0.501062398379 * prefactor + p3m_force = 2.48921612e-01 * prefactor test_P3M = tests_common.generate_test_for_class( self.system, electrostatics.P3M, diff --git a/testsuite/python/interactions_bonded.py b/testsuite/python/interactions_bonded.py index df94cd35f67..c1d0efb45f1 100644 --- a/testsuite/python/interactions_bonded.py +++ b/testsuite/python/interactions_bonded.py @@ -93,37 +93,35 @@ def test_coulomb(self): lambda r: tests_common.coulomb_potential(r, coulomb_k, q1, q2), 0.01, self.system.box_l[0] / 3) - - @ut.skipIf(not espressomd.has_features(["ELECTROSTATICS"]), "ELECTROSTATICS feature is not available, skipping coulomb short range test.") def test_coulomb_sr(self): - #with negated actual charges and only short range int: cancels out all interactions + # with negated actual charges and only short range int: cancels out all + # interactions q1 = 1.2 q2 = -q1 self.system.part[0].q = q1 self.system.part[1].q = q2 r_cut = 2 - sr_solver = espressomd.electrostatics.DH(prefactor = 2, kappa = 0.8, r_cut = r_cut) + sr_solver = espressomd.electrostatics.DH( + prefactor=2, kappa=0.8, r_cut=r_cut) self.system.actors.add(sr_solver) - coulomb_sr = espressomd.interactions.BondedCoulombSRBond(q1q2 = - q1*q2) - - #no break test, bond can't break. it extends as far as the short range part of the electrostatics actor - self.run_test(coulomb_sr, lambda r: [0.,0.,0.], lambda r: 0, 0.01, r_cut, test_breakage=False) - + coulomb_sr = espressomd.interactions.BondedCoulombSRBond( + q1q2=- q1 * q2) + # no break test, bond can't break. it extends as far as the short range + # part of the electrostatics actor + self.run_test( + coulomb_sr, + lambda r: [0., 0., 0.], + lambda r: 0, + 0.01, + r_cut, + test_breakage=False) - - - def run_test( - self, - bond_instance, - force_func, - energy_func, - min_dist, - cutoff, - test_breakage=False): + def run_test(self, bond_instance, force_func, energy_func, min_dist, + cutoff, test_breakage=False): self.system.bonded_inter.add(bond_instance) self.system.part[0].bonds = ((bond_instance, 1),) From 1045f51de413733e252126ef7ac3d074f9c601a3 Mon Sep 17 00:00:00 2001 From: Jean-Noel Grad Date: Fri, 12 Apr 2019 22:52:14 +0200 Subject: [PATCH 15/16] clang-tidy --- src/core/electrostatics_magnetostatics/mmm1d.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/electrostatics_magnetostatics/mmm1d.hpp b/src/core/electrostatics_magnetostatics/mmm1d.hpp index cb6cdc2a0d1..175ab10151a 100644 --- a/src/core/electrostatics_magnetostatics/mmm1d.hpp +++ b/src/core/electrostatics_magnetostatics/mmm1d.hpp @@ -68,7 +68,7 @@ void MMM1D_init(); void add_mmm1d_coulomb_pair_force(double chprf, double const d[3], double dist2, double dist, double force[3]); -double mmm1d_coulomb_pair_energy(double const q1q2, double const d[3], +double mmm1d_coulomb_pair_energy(double q1q2, double const d[3], double r2, double r); /** Tuning of the parameters which are not set by the user, e.g. the From 742cc6b146a50e6c978b3b08cbbf7cdfb9d2df82 Mon Sep 17 00:00:00 2001 From: Jean-Noel Grad Date: Fri, 12 Apr 2019 22:55:46 +0200 Subject: [PATCH 16/16] Formatting --- src/core/electrostatics_magnetostatics/mmm1d.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/electrostatics_magnetostatics/mmm1d.hpp b/src/core/electrostatics_magnetostatics/mmm1d.hpp index 175ab10151a..daf92530f93 100644 --- a/src/core/electrostatics_magnetostatics/mmm1d.hpp +++ b/src/core/electrostatics_magnetostatics/mmm1d.hpp @@ -68,8 +68,8 @@ void MMM1D_init(); void add_mmm1d_coulomb_pair_force(double chprf, double const d[3], double dist2, double dist, double force[3]); -double mmm1d_coulomb_pair_energy(double q1q2, double const d[3], - double r2, double r); +double mmm1d_coulomb_pair_energy(double q1q2, double const d[3], double r2, + double r); /** Tuning of the parameters which are not set by the user, e.g. the * switching radius or the bessel_cutoff. Call this only on the master node.