Skip to content

Commit a3833fc

Browse files
committed
Performance: short range forces low hanging fruits
- avoid a few constructions of ParticleForce - avoid calls to npt and gay-berne if not active - in the linked cell algorithm, first do cell-internal interactions to have better memory locality. Improves thins by ~5% on my system (Intel)
1 parent 17a181a commit a3833fc

File tree

6 files changed

+62
-23
lines changed

6 files changed

+62
-23
lines changed

src/core/algorithm/link_cell.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ void link_cell(CellIterator first, CellIterator last,
4040
for (auto jt = std::next(it); jt != local_particles.end(); ++jt) {
4141
pair_kernel(p1, *jt);
4242
}
43-
43+
}
44+
for (auto it = local_particles.begin(); it != local_particles.end(); ++it) {
45+
auto &p1 = *it;
4446
/* Pairs with neighbors */
4547
for (auto &neighbor : cell->neighbors().red()) {
4648
for (auto &p2 : neighbor->particles()) {

src/core/constraints/ShapeBasedConstraint.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#include "errorhandling.hpp"
3030
#include "forces_inline.hpp"
3131
#include "nonbonded_interactions/nonbonded_interaction_data.hpp"
32+
#ifdef GAY_BERNE
33+
#include "nonbonded_interactions/gay_berne.hpp"
34+
#endif
3235
#include "system/System.hpp"
3336
#include "thermostat.hpp"
3437

@@ -111,7 +114,12 @@ ParticleForce ShapeBasedConstraint::force(Particle const &p,
111114
pf.f += thole_pair_force(p, part_rep, ia_params, dist_vec, dist,
112115
*system.bonded_ias, get_ptr(coulomb_kernel));
113116
#endif
114-
pf += calc_non_central_force(p, part_rep, ia_params, dist_vec, dist);
117+
#ifdef GAY_BERNE
118+
if (gb_active(ia_params)) {
119+
pf +=
120+
gb_pair_force(p.quat(), part_rep.quat(), ia_params, dist_vec, dist);
121+
}
122+
#endif
115123

116124
#ifdef DPD
117125
if (system.thermostat->thermo_switch & THERMO_DPD) {
@@ -130,7 +138,12 @@ ParticleForce ShapeBasedConstraint::force(Particle const &p,
130138
pf.f += thole_pair_force(p, part_rep, ia_params, dist_vec, -dist,
131139
*system.bonded_ias, get_ptr(coulomb_kernel));
132140
#endif
133-
pf += calc_non_central_force(p, part_rep, ia_params, dist_vec, -dist);
141+
#ifdef GAY_BERNE
142+
if (gb_active(ia_params)) {
143+
pf += gb_pair_force(p.quat(), part_rep.quat(), ia_params, dist_vec,
144+
-dist);
145+
}
146+
#endif
134147

135148
#ifdef DPD
136149
if (system.thermostat->thermo_switch & THERMO_DPD) {

src/core/forces_inline.hpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
#include "errorhandling.hpp"
6868
#include "exclusions.hpp"
6969
#include "thermostat.hpp"
70+
#ifdef NPT
71+
#include "npt.hpp"
72+
#endif
7073

7174
#include <utils/Vector.hpp>
7275

@@ -140,19 +143,11 @@ inline Utils::Vector3d calc_central_radial_force(IA_parameters const &ia_params,
140143
return force_factor * d;
141144
}
142145

143-
inline ParticleForce calc_non_central_force(Particle const &p1,
144-
Particle const &p2,
145-
IA_parameters const &ia_params,
146-
Utils::Vector3d const &d,
147-
double const dist) {
148-
149-
ParticleForce pf{};
150-
/* Gay-Berne */
151146
#ifdef GAY_BERNE
152-
pf += gb_pair_force(p1.quat(), p2.quat(), ia_params, d, dist);
153-
#endif
154-
return pf;
147+
inline bool gb_active(IA_parameters const &ia_params) {
148+
return ia_params.gay_berne.cut != INACTIVE_CUTOFF;
155149
}
150+
#endif
156151

157152
inline void apply_opposing_force(ParticleForce &pf, Utils::Vector3d const &d) {
158153
#ifdef ROTATION
@@ -207,7 +202,11 @@ inline void add_non_bonded_pair_force(
207202
pf.f += thole_pair_force(p1, p2, ia_params, d, dist, bonded_ias,
208203
coulomb_kernel);
209204
#endif
210-
pf += calc_non_central_force(p1, p2, ia_params, d, dist);
205+
#ifdef GAY_BERNE
206+
if (gb_active(ia_params)) {
207+
pf += gb_pair_force(p1.quat(), p2.quat(), ia_params, d, dist);
208+
}
209+
#endif
211210
#ifdef EXCLUSIONS
212211
}
213212
#endif
@@ -219,7 +218,9 @@ inline void add_non_bonded_pair_force(
219218
/* electrostatic is calculated by energy */
220219
/*********************************************************************/
221220
#ifdef NPT
222-
npt_add_virial_force_contribution(pf.f, d);
221+
if (npt_active()) {
222+
npt_add_virial_force_contribution(pf.f, d);
223+
}
223224
#endif
224225

225226
/***********************************************/
@@ -232,8 +233,10 @@ inline void add_non_bonded_pair_force(
232233
if (q1q2 != 0. and coulomb_kernel != nullptr) {
233234
pf.f += (*coulomb_kernel)(q1q2, d, dist);
234235
#ifdef NPT
235-
npt_add_virial_diagonalSum_contribution(
236-
(*coulomb_u_kernel)(p1, p2, q1q2, d, dist));
236+
if (npt_active()) {
237+
npt_add_virial_diagonalSum_contribution(
238+
(*coulomb_u_kernel)(p1, p2, q1q2, d, dist));
239+
}
237240
#endif
238241
#ifdef P3M
239242
if (elc_kernel)
@@ -345,7 +348,9 @@ inline bool add_bonded_two_body_force(
345348
p2.force() -= result.value();
346349

347350
#ifdef NPT
348-
npt_add_virial_force_contribution(result.value(), dx);
351+
if (npt_active()) {
352+
npt_add_virial_force_contribution(result.value(), dx);
353+
}
349354
#endif
350355
return false;
351356
}

src/core/npt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,5 @@ void System::System::npt_add_virial_contribution(Utils::Vector3d const &force,
148148
npt_inst_pressure->p_vir += hadamard_product(force, d);
149149
}
150150
}
151+
151152
#endif // NPT

src/core/npt.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
#include <cstddef>
3535
#include <vector>
3636

37+
#include "PropagationMode.hpp"
38+
#include "integrators/Propagation.hpp"
39+
#include "system/System.hpp"
40+
3741
namespace System {
3842
class System;
3943
} // namespace System
@@ -99,4 +103,11 @@ struct InstantaneousPressure {
99103
Utils::Vector3d p_vel = {0., 0., 0.};
100104
};
101105

106+
/** Check if NPT integration is active. */
107+
inline bool npt_active() {
108+
auto const &system = ::System::get_system();
109+
return (system.propagation->integ_switch == INTEG_METHOD_NPT_ISO_AND) ||
110+
(system.propagation->integ_switch == INTEG_METHOD_NPT_ISO_MTK);
111+
}
112+
102113
#endif // NPT

src/core/pressure_inline.hpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#include "errorhandling.hpp"
3434
#include "exclusions.hpp"
3535
#include "forces_inline.hpp"
36+
#ifdef GAY_BERNE
37+
#include "nonbonded_interactions/gay_berne.hpp"
38+
#endif
3639

3740
#include <utils/Vector.hpp>
3841
#include <utils/math/tensor_product.hpp>
@@ -67,12 +70,16 @@ inline void add_non_bonded_pair_virials(
6770
if (do_nonbonded(p1, p2))
6871
#endif
6972
{
70-
auto const force = calc_central_radial_force(ia_params, d, dist) +
73+
auto force = calc_central_radial_force(ia_params, d, dist);
7174
#ifdef THOLE
72-
thole_pair_force(p1, p2, ia_params, d, dist, bonded_ias,
73-
kernel_forces) +
75+
force +=
76+
thole_pair_force(p1, p2, ia_params, d, dist, bonded_ias, kernel_forces);
77+
#endif
78+
#ifdef GAY_BERNE
79+
if (gb_active(ia_params)) {
80+
force += gb_pair_force(p1.quat(), p2.quat(), ia_params, d, dist).f;
81+
}
7482
#endif
75-
calc_non_central_force(p1, p2, ia_params, d, dist).f;
7683
auto const stress = Utils::tensor_product(d, force);
7784
obs_pressure.add_non_bonded_contribution(p1.type(), p2.type(), p1.mol_id(),
7885
p2.mol_id(), flatten(stress));

0 commit comments

Comments
 (0)