glotzerlab / hoomd-blue

Molecular dynamics and Monte Carlo soft matter simulation on GPUs.
http://glotzerlab.engin.umich.edu/hoomd-blue
BSD 3-Clause "New" or "Revised" License
336 stars 131 forks source link

FreeVolume computes incorrect free area in 2D simulation boxes. #1473

Closed joaander closed 1 year ago

joaander commented 1 year ago

Description

hoomd.hpmc.compute.FreeVolume always assumes the box is 3D. This leads to errors in the free volume computation.

Script

import hoomd

snapshot = hoomd.Snapshot()
snapshot.configuration.box = (100, 100, 0, 0, 0, 0)
snapshot.particles.N = 1
snapshot.particles.types = ['A']

sim = hoomd.Simulation(device=hoomd.device.CPU(), seed=1)
sim.create_state_from_snapshot(snapshot)

mc = hoomd.hpmc.integrate.Sphere()
mc.shape['A'] = dict(diameter=1)

free_volume = hoomd.hpmc.compute.FreeVolume(test_particle_type='A', num_samples=100000)

sim.operations.integrator = mc
sim.operations.computes.append(free_volume)

sim.run(1)

print(free_volume.free_volume)

Input files

No response

Output

0.0

Expected output

Approximately 9996.85840734641 (100*100 - pi)

Platform

CPU, GPU, Linux, macOS

Installation method

Compiled from source

HOOMD-blue version

3.8.1

Python version

3.11.1

joaander commented 1 year ago

There are two errors that need to be corrected and a test to add: 1) [ ] - Pass the system dimensionality to getVolume so that it computes the box volume or area when appropriate: https://github.com/glotzerlab/hoomd-blue/blob/d71900eb2174bafb1aebede304f7ed5a77e61870/hoomd/hpmc/ComputeFreeVolume.h#L296-L298

2) [ ] - Generate 0 z coordinates when the system dimensionality is 2: https://github.com/glotzerlab/hoomd-blue/blob/d71900eb2174bafb1aebede304f7ed5a77e61870/hoomd/hpmc/ComputeFreeVolume.h#L177-L179 https://github.com/glotzerlab/hoomd-blue/blob/d71900eb2174bafb1aebede304f7ed5a77e61870/hoomd/hpmc/ComputeFreeVolumeGPU.cuh#L275-L277

3) [ ] - To test the fix, add a 2D test to test_compute_free_volume.py.

Note that the code appropriately generates rotations based on the dimensionality: https://github.com/glotzerlab/hoomd-blue/blob/d71900eb2174bafb1aebede304f7ed5a77e61870/hoomd/hpmc/ComputeFreeVolume.h#L185