Skip to content

Commit

Permalink
Virtual Sites Cleanup (espressomd#3564)
Browse files Browse the repository at this point in the history
This is some intermediate cleanup of virtual sites. Biggest change
is that the velocities are now always updated. This has actually very
little performance impact, and is less error prone. 

Description of changes:
 - Simplified interface of `VirtualSites`
 - Some cleanup of the pressure calc
 - Removed `VirtualSites::have_velocity()` mechanism
  • Loading branch information
kodiakhq[bot] authored Mar 4, 2020
2 parents aa19013 + 3b81f05 commit 0436a79
Show file tree
Hide file tree
Showing 24 changed files with 245 additions and 225 deletions.
3 changes: 1 addition & 2 deletions doc/sphinx/particles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,11 @@ To switch the active scheme, the attribute :attr:`espressomd.system.System.virtu
from espressomd.virtual_sites import VirtualSitesOff, VirtualSitesRelative

system = espressomd.System()
system.virtual_sites = VirtualSitesRelative(have_velocity=True, have_quaternion=False)
system.virtual_sites = VirtualSitesRelative(have_quaternion=False)
# or
system.virtual_sites = VirtualSitesOff()

By default, :class:`espressomd.virtual_sites.VirtualSitesOff` is selected. This means that virtual particles are not touched during integration.
The ``have_velocity`` parameter determines whether or not the velocity of virtual sites is calculated, which carries a performance cost.
The ``have_quaternion`` parameter determines whether the quaternion of the virtual particle is updated (useful in combination with the
:attr:`espressomd.particle_data.ParticleHandle.vs_quat` property of the virtual particle which defines the orientation of the virtual particle
in the body fixed frame of the related real particle.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
"outputs": [],
"source": [
"# Select the desired implementation for virtual sites\n",
"system.virtual_sites = VirtualSitesRelative(have_velocity=True)\n",
"system.virtual_sites = VirtualSitesRelative()\n",
"# Setting min_global_cut is necessary when there is no interaction defined with a range larger than\n",
"# the colloid such that the virtual particles are able to communicate their forces to the real particle\n",
"# at the center of the colloid\n",
Expand Down
2 changes: 1 addition & 1 deletion samples/drude_bmimpf6.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
print("\n-->Ion pairs:", n_ionpairs, "Box size:", box_l)

system = espressomd.System(box_l=[box_l, box_l, box_l])
system.virtual_sites = VirtualSitesRelative(have_velocity=True)
system.virtual_sites = VirtualSitesRelative()

if args.visu:
d_scale = 0.988 * 0.5
Expand Down
3 changes: 1 addition & 2 deletions samples/rigid_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@


system = espressomd.System(box_l=[10.0] * 3)
system.virtual_sites = espressomd.virtual_sites.VirtualSitesRelative(
have_velocity=True)
system.virtual_sites = espressomd.virtual_sites.VirtualSitesRelative()
system.time_step = 0.01
system.thermostat.set_langevin(kT=1.0, gamma=20.0, seed=42)

Expand Down
2 changes: 1 addition & 1 deletion src/core/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ unsigned global_ghost_flags() {

void update_dependent_particles() {
#ifdef VIRTUAL_SITES
virtual_sites()->update(true);
virtual_sites()->update();
cells_update_ghosts(global_ghost_flags());
#endif

Expand Down
3 changes: 3 additions & 0 deletions src/core/ghosts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ static bool is_poststorable(GhostCommunication const &ghost_comm) {
}

void ghost_communicator(GhostCommunicator *gcr, unsigned int data_parts) {
if (GHOSTTRANS_NONE == data_parts)
return;

static CommBuf send_buffer, recv_buffer;

for (auto it = gcr->comm.begin(); it != gcr->comm.end(); ++it) {
Expand Down
7 changes: 3 additions & 4 deletions src/core/integrate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ int integrate(int n_steps, int reuse_forces) {
lb_lbcoupling_deactivate();

#ifdef VIRTUAL_SITES
virtual_sites()->update(true);
virtual_sites()->update();
#endif

// Communication step: distribute ghost positions
Expand Down Expand Up @@ -239,7 +239,7 @@ int integrate(int n_steps, int reuse_forces) {
#endif

#ifdef VIRTUAL_SITES
virtual_sites()->update(true);
virtual_sites()->update();
#endif

// Communication step: distribute ghost positions
Expand Down Expand Up @@ -292,8 +292,7 @@ int integrate(int n_steps, int reuse_forces) {
#endif

#ifdef VIRTUAL_SITES
// VIRTUAL_SITES update vel
virtual_sites()->update(false); // Recalc positions = false
virtual_sites()->update();
#endif

/* verlet list statistics */
Expand Down
5 changes: 1 addition & 4 deletions src/core/integrators/brownian_inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,8 @@ inline void brownian_dynamics_propagator(BrownianThermostat const &brownian,
const ParticleRange &particles) {
for (auto &p : particles) {
// Don't propagate translational degrees of freedom of vs
#ifdef VIRTUAL_SITES
extern bool thermo_virtual;
if (!(p.p.is_virtual) or thermo_virtual)
#endif
{
if (!(p.p.is_virtual) or thermo_virtual) {
p.r.p += bd_drag(brownian.gamma, p, time_step);
p.m.v = bd_drag_vel(brownian.gamma, p);
p.r.p += bd_random_walk(brownian, p, time_step);
Expand Down
3 changes: 1 addition & 2 deletions src/core/integrators/velocity_verlet_inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ inline void
velocity_verlet_propagate_vel_final(const ParticleRange &particles) {

for (auto &p : particles) {
#ifdef VIRTUAL_SITES
// Virtual sites are not propagated during integration
if (p.p.is_virtual)
continue;
#endif

for (int j = 0; j < 3; j++) {
if (!(p.p.ext_flag & COORD_FIXED(j))) {
/* Propagate velocity: v(t+dt) = v(t+0.5*dt) + 0.5*dt * a(t+dt) */
Expand Down
13 changes: 9 additions & 4 deletions src/core/pressure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "npt.hpp"
#include "pressure_inline.hpp"
#include "virtual_sites.hpp"
#include <boost/range/algorithm/copy.hpp>

#include "short_range_loop.hpp"

Expand Down Expand Up @@ -127,8 +128,12 @@ void pressure_calc(double *result, double *result_t, double *result_nb,
calc_long_range_virials(cell_structure.local_cells().particles());

#ifdef VIRTUAL_SITES
virtual_sites()->pressure_and_stress_tensor_contribution(
virials.virtual_sites, p_tensor.virtual_sites);
{
auto const vs_stress = virtual_sites()->stress_tensor();

*virials.virtual_sites += trace(vs_stress);
boost::copy(flatten(vs_stress), p_tensor.virtual_sites);
}
#endif

for (int n = 1; n < virials.data.n; n++)
Expand Down Expand Up @@ -190,7 +195,7 @@ void init_virials(Observable_stat *stat) {
Dipole::pressure_n();
#endif
#ifdef VIRTUAL_SITES
n_vs = virtual_sites()->n_pressure_contribs();
n_vs = 1;
#endif

// Allocate memory for the data
Expand Down Expand Up @@ -228,7 +233,7 @@ void init_p_tensor(Observable_stat *stat) {
auto const n_dipolar = 0;
#endif
#ifdef VIRTUAL_SITES
n_vs = virtual_sites()->n_pressure_contribs();
n_vs = 1;
#endif

obsstat_realloc_and_clear(stat, n_pre, bonded_ia_params.size(), n_non_bonded,
Expand Down
16 changes: 8 additions & 8 deletions src/core/pressure_inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ inline void add_non_bonded_pair_virials(Particle const &p1, Particle const &p2,
}

#ifdef ELECTROSTATICS
/* real space Coulomb */
auto const p_coulomb = Coulomb::pair_pressure(p1, p2, d, dist);
{
/* real space Coulomb */
auto const p_coulomb = Coulomb::pair_pressure(p1, p2, d, dist);

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
p_tensor.coulomb[i * 3 + j] += p_coulomb[i][j];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
p_tensor.coulomb[i * 3 + j] += p_coulomb[i][j];
}
}
}

for (int i = 0; i < 3; i++) {
virials.coulomb[0] += p_coulomb[i][i];
virials.coulomb[0] += trace(p_coulomb);
}
#endif /*ifdef ELECTROSTATICS */

Expand Down
2 changes: 2 additions & 0 deletions src/core/virtual_sites.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "virtual_sites/VirtualSites.hpp"

#include <memory>

/** @brief get active virtual sites implementation */
const std::shared_ptr<VirtualSites> &virtual_sites();

Expand Down
29 changes: 10 additions & 19 deletions src/core/virtual_sites/VirtualSites.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,33 @@
*/

#ifdef VIRTUAL_SITES
#include <memory>
#include <utils/Vector.hpp>

/** @brief Base class for virtual sites implementations */
class VirtualSites {
public:
VirtualSites() : m_have_velocity(true), m_have_quaternion(false){};
/** @brief Update positions and/or velocities of virtual sites.
* Velocities are only updated if get_have_velocity() returns true.
* @param recalc_positions Skip the recalculation of positions if false.
VirtualSites() = default;
virtual ~VirtualSites() = default;

/**
* @brief Update positions and velocities of virtual sites.
*/
virtual void update(bool recalc_positions) const = 0;
virtual void update() const {}
/** Back-transfer forces (and torques) to non-virtual particles. */
virtual void back_transfer_forces_and_torques() const = 0;
virtual void back_transfer_forces_and_torques() const {}
/** @brief Called after force calculation (and before rattle/shake) */
virtual void after_force_calc(){};
virtual void after_lb_propagation(){};
/** @brief Number of pressure contributions */
virtual int n_pressure_contribs() const { return 0; };
/** @brief Pressure contribution. */
virtual void
pressure_and_stress_tensor_contribution(double *pressure,
double *stress_tensor) const {};
/** @brief Enable/disable velocity calculations for vs. */
void set_have_velocity(const bool &v) { m_have_velocity = v; };
const bool &get_have_velocity() const { return m_have_velocity; };
virtual Utils::Matrix<double, 3, 3> stress_tensor() const { return {}; };
/** @brief Enable/disable quaternion calculations for vs.*/
void set_have_quaternion(const bool &have_quaternion) {
m_have_quaternion = have_quaternion;
};
bool get_have_quaternion() const { return m_have_quaternion; };

virtual ~VirtualSites() = default;

private:
bool m_have_velocity;
bool m_have_quaternion;
bool m_have_quaternion = false;
};

#endif
Expand Down
2 changes: 0 additions & 2 deletions src/core/virtual_sites/VirtualSitesInertialessTracers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
* instantaneously transferred to the fluid
*/
class VirtualSitesInertialessTracers : public VirtualSites {
void update(bool recalc_positions) const override{};
void back_transfer_forces_and_torques() const override{};
void after_force_calc() override;
void after_lb_propagation() override;
};
Expand Down
5 changes: 1 addition & 4 deletions src/core/virtual_sites/VirtualSitesOff.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
#include "VirtualSites.hpp"

/** @brief Do-nothing virtual-sites implementation */
class VirtualSitesOff : public VirtualSites {
void update(bool) const override{};
void back_transfer_forces_and_torques() const override{};
};
class VirtualSitesOff : public VirtualSites {};

#endif
#endif
Loading

0 comments on commit 0436a79

Please sign in to comment.