Skip to content

Commit 94c61b6

Browse files
authored
Merge pull request #2136 from mzbush/rigid-body-collision
Make MPCD collision methods compatible with rigid bodies
2 parents 6fad59c + a5d4343 commit 94c61b6

18 files changed

+2347
-119
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Next release
99

1010
*Added*
1111

12+
* Implemented rigid body compatibility with MPCD without MPI
13+
(`#2136 <https://github.com/glotzerlab/hoomd-blue/pull/2136>`__).
1214
* Add thermodynamically consistent frictional contact forces: ``hoomd.md.pair.friction``
1315
(`#2116 <https://github.com/glotzerlab/hoomd-blue/pull/2116>`__).
1416

hoomd/md/ForceComposite.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ ForceComposite::ForceComposite(std::shared_ptr<SystemDefinition> sysdef)
4747
h_body_len.data[i] = 0;
4848
}
4949

50+
GPUVector<unsigned int> rigid_center(m_exec_conf);
51+
m_rigid_center.swap(rigid_center);
52+
53+
GPUVector<unsigned int> lookup_center(m_exec_conf);
54+
m_lookup_center.swap(lookup_center);
55+
5056
m_d_max.resize(m_pdata->getNTypes(), Scalar(0.0));
5157
m_d_max_changed.resize(m_pdata->getNTypes(), false);
5258

@@ -1048,6 +1054,45 @@ void ForceComposite::updateCompositeParticles(uint64_t timestep)
10481054
}
10491055
}
10501056

1057+
void ForceComposite::findRigidCenters()
1058+
{
1059+
ArrayHandle<unsigned int> h_tag(m_pdata->getTags(), access_location::host, access_mode::read);
1060+
ArrayHandle<unsigned int> h_rtag(m_pdata->getRTags(), access_location::host, access_mode::read);
1061+
ArrayHandle<unsigned int> h_body(m_pdata->getBodies(),
1062+
access_location::host,
1063+
access_mode::read);
1064+
1065+
m_rigid_center.resize(m_pdata->getN() + m_pdata->getNGhosts());
1066+
m_lookup_center.resize(m_pdata->getN() + m_pdata->getNGhosts());
1067+
ArrayHandle<unsigned int> h_rigid_center(m_rigid_center,
1068+
access_location::host,
1069+
access_mode::overwrite);
1070+
ArrayHandle<unsigned int> h_lookup_center(m_lookup_center,
1071+
access_location::host,
1072+
access_mode::overwrite);
1073+
unsigned int num_centers = 0;
1074+
const unsigned int n_particles_local = m_pdata->getN() + m_pdata->getNGhosts();
1075+
for (unsigned int idx = 0; idx < n_particles_local; ++idx)
1076+
{
1077+
unsigned int lookup_center = NO_BODY;
1078+
1079+
// if particle is in a rigid body, find its center
1080+
const unsigned int central_tag = h_body.data[idx];
1081+
if (central_tag < MIN_FLOPPY)
1082+
{
1083+
lookup_center = h_rtag.data[central_tag];
1084+
// also record rigid particles that are centers
1085+
if (lookup_center == idx)
1086+
{
1087+
h_rigid_center.data[num_centers++] = lookup_center;
1088+
}
1089+
}
1090+
1091+
h_lookup_center.data[idx] = lookup_center;
1092+
}
1093+
m_n_rigid = num_centers;
1094+
}
1095+
10511096
namespace detail
10521097
{
10531098
void export_ForceComposite(pybind11::module& m)

hoomd/md/ForceComposite.h

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class PYBIND11_EXPORT ForceComposite : public MolecularForceCompute
8787

8888
#ifdef ENABLE_MPI
8989
//! Get ghost particle fields requested by this pair potential
90-
virtual CommFlags getRequestedCommFlags(uint64_t timestep);
90+
CommFlags getRequestedCommFlags(uint64_t timestep) override;
9191
#endif
9292

9393
/// Update the constituent particles of a composite particle using the position, velocity
@@ -199,6 +199,56 @@ class PYBIND11_EXPORT ForceComposite : public MolecularForceCompute
199199
return m_n_free_particles_global;
200200
}
201201

202+
/// Get constituent particle types per type id
203+
const GPUArray<unsigned int>& getBodyTypes() const
204+
{
205+
return m_body_types;
206+
}
207+
208+
/// Get constituent particle offsets per type id
209+
const GPUArray<Scalar3>& getBodyOffsets() const
210+
{
211+
return m_body_pos;
212+
}
213+
214+
/// Get constituent particle orientations per type id
215+
const GPUArray<Scalar4>& getBodyOrientations() const
216+
{
217+
return m_body_orientation;
218+
}
219+
220+
/// Get length of body per type id
221+
const GPUArray<unsigned int>& getBodyLengths() const
222+
{
223+
return m_body_len;
224+
}
225+
226+
/// Get body parameter indexer
227+
const Index2D& getBodyIndexer()
228+
{
229+
return m_body_idx;
230+
}
231+
232+
//! Get rigid centers
233+
GPUVector<unsigned int>& getRigidCenters()
234+
{
235+
checkParticlesSorted();
236+
return m_rigid_center;
237+
}
238+
239+
//! Get lookup centers
240+
GPUVector<unsigned int>& getLookupCenters()
241+
{
242+
checkParticlesSorted();
243+
return m_lookup_center;
244+
}
245+
246+
//! Get number of local rigid bodies
247+
const unsigned int getNLocal() const
248+
{
249+
return m_n_rigid;
250+
}
251+
202252
protected:
203253
bool m_bodies_changed; //!< True if constituent particles have changed
204254
bool m_particles_added_removed; //!< True if particles have been added or removed
@@ -216,6 +266,10 @@ class PYBIND11_EXPORT ForceComposite : public MolecularForceCompute
216266
std::vector<Scalar> m_d_max; //!< Maximum body diameter per constituent particle type
217267
std::vector<bool> m_d_max_changed; //!< True if maximum body diameter changed (per type)
218268

269+
unsigned int m_n_rigid; //!< Number of rigid bodies on the local rank.
270+
GPUVector<unsigned int> m_rigid_center; //!< Local particle indices of all central particles
271+
GPUVector<unsigned int> m_lookup_center; //!< Lookup particle index -> central particle index
272+
219273
#ifdef ENABLE_MPI
220274
/// The system's communicator.
221275
std::shared_ptr<Communicator> m_comm;
@@ -227,11 +281,26 @@ class PYBIND11_EXPORT ForceComposite : public MolecularForceCompute
227281
m_particles_added_removed = true;
228282
}
229283

284+
//! Helper function to check if particles have been sorted and rebuild indices if necessary
285+
void checkParticlesSorted() override
286+
{
287+
if (m_rebuild_molecules)
288+
// identify center particles for use in GPU kernel
289+
findRigidCenters();
290+
291+
// Must be called second since the method sets m_rebuild_molecules
292+
// to false if it is true.
293+
MolecularForceCompute::checkParticlesSorted();
294+
}
295+
296+
//! Helper kernel to sort rigid bodies by their center particles
297+
virtual void findRigidCenters();
298+
230299
/// Return the requested minimum ghost layer width for a body's central particle.
231300
virtual Scalar requestBodyGhostLayerWidth(unsigned int type, Scalar* h_r_ghost);
232301

233302
//! Compute the forces and torques on the central particle
234-
virtual void computeForces(uint64_t timestep);
303+
void computeForces(uint64_t timestep) override;
235304
};
236305

237306
} // end namespace md

hoomd/md/ForceCompositeGPU.h

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,14 @@ class PYBIND11_EXPORT ForceCompositeGPU : public ForceComposite
3636
* \param remote If true, consider remote bodies, otherwise bodies
3737
* with a local central particle
3838
*/
39-
virtual void updateCompositeParticles(uint64_t timestep);
39+
void updateCompositeParticles(uint64_t timestep) override;
4040

4141
protected:
4242
//! Compute the forces and torques on the central particle
43-
virtual void computeForces(uint64_t timestep);
43+
void computeForces(uint64_t timestep) override;
4444

4545
//! Helper kernel to sort rigid bodies by their center particles
46-
virtual void findRigidCenters();
47-
48-
//! Helper function to check if particles have been sorted and rebuild indices if necessary
49-
virtual void checkParticlesSorted()
50-
{
51-
if (m_rebuild_molecules)
52-
// identify center particles for use in GPU kernel
53-
findRigidCenters();
54-
55-
// Must be called second since the method sets m_rebuild_molecules
56-
// to false if it is true.
57-
MolecularForceCompute::checkParticlesSorted();
58-
}
46+
void findRigidCenters() override;
5947

6048
/// Autotuner for block size and threads per body.
6149
std::shared_ptr<Autotuner<2>> m_tuner_force;
@@ -67,12 +55,6 @@ class PYBIND11_EXPORT ForceCompositeGPU : public ForceComposite
6755
std::shared_ptr<Autotuner<1>> m_tuner_update;
6856

6957
GPUArray<uint2> m_flag; //!< Flag to read out error condition
70-
71-
/// Number of rigid bodies on the local rank.
72-
unsigned int m_n_rigid;
73-
74-
GPUVector<unsigned int> m_rigid_center; //!< Contains particle indices of all central particles
75-
GPUVector<unsigned int> m_lookup_center; //!< Lookup particle index -> central particle index
7658
};
7759

7860
} // end namespace md

hoomd/md/IntegratorTwoStep.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class PYBIND11_EXPORT IntegratorTwoStep : public Integrator
102102
return m_rigid_bodies;
103103
}
104104

105-
void setRigid(std::shared_ptr<ForceComposite> new_rigid)
105+
virtual void setRigid(std::shared_ptr<ForceComposite> new_rigid)
106106
{
107107
m_rigid_bodies = new_rigid;
108108
}

hoomd/mpcd/ATCollisionMethod.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ mpcd::ATCollisionMethod::ATCollisionMethod(std::shared_ptr<SystemDefinition> sys
1717
uint64_t period,
1818
int phase,
1919
std::shared_ptr<Variant> T)
20-
: mpcd::CollisionMethod(sysdef, cur_timestep, period, phase), m_T(T)
20+
: mpcd::CollisionMethod(sysdef, cur_timestep, period, phase)
2121
{
22+
setTemperature(T);
23+
requireTemperature();
2224
m_exec_conf->msg->notice(5) << "Constructing MPCD AT collision method" << std::endl;
2325
}
2426

@@ -278,10 +280,7 @@ void export_ATCollisionMethod(pybind11::module& m)
278280
uint64_t,
279281
uint64_t,
280282
int,
281-
std::shared_ptr<Variant>>())
282-
.def_property("kT",
283-
&mpcd::ATCollisionMethod::getTemperature,
284-
&mpcd::ATCollisionMethod::setTemperature);
283+
std::shared_ptr<Variant>>());
285284
}
286285
} // namespace detail
287286
} // namespace mpcd

hoomd/mpcd/ATCollisionMethod.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,9 @@ class PYBIND11_EXPORT ATCollisionMethod : public mpcd::CollisionMethod
4343

4444
void setCellList(std::shared_ptr<mpcd::CellList> cl) override;
4545

46-
//! Get the temperature
47-
std::shared_ptr<Variant> getTemperature() const
48-
{
49-
return m_T;
50-
}
51-
52-
//! Set the temperature
53-
void setTemperature(std::shared_ptr<Variant> T)
54-
{
55-
m_T = T;
56-
}
57-
5846
protected:
5947
std::shared_ptr<mpcd::CellThermoCompute> m_thermo; //!< Cell thermo
6048
std::shared_ptr<mpcd::CellThermoCompute> m_rand_thermo; //!< Cell thermo for random velocities
61-
std::shared_ptr<Variant> m_T; //!< Temperature for thermostat
6249

6350
//! Implementation of the collision rule
6451
virtual void rule(uint64_t timestep) override;

hoomd/mpcd/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ if(ENABLE_HIP)
9393
CellThermoComputeGPU.h
9494
CellListGPU.cuh
9595
CellListGPU.h
96+
CollisionMethod.cuh
9697
CommunicatorGPU.cuh
9798
CommunicatorGPU.h
9899
ParallelPlateGeometryFillerGPU.cuh
@@ -114,6 +115,7 @@ if(ENABLE_HIP)
114115
BounceBackNVEGPU.cu
115116
CellThermoComputeGPU.cu
116117
CellListGPU.cu
118+
CollisionMethod.cu
117119
CommunicatorGPU.cu
118120
ParticleData.cu
119121
ParallelPlateGeometryFillerGPU.cu

0 commit comments

Comments
 (0)