OpenBioSim / sire

Sire Molecular Simulations Framework
https://sire.openbiosim.org
GNU General Public License v3.0
41 stars 11 forks source link

[BUG] somd-freenrg is expanding the water box #203

Closed nigel-palmer-cresset closed 4 months ago

nigel-palmer-cresset commented 4 months ago

Describe the bug

I have been seeing the following error from analyse_freenrg.py mbar.

Traceback (most recent call last):
  File "/data/tmp/nigel.palmer/ceb3/conan/p/sirebb1417ebb1a62/p/lib/python3.11/site-packages/sire/legacy/Tools/FreeEnergyAnalysis.py", line 136, in run_mbar
    results = MBAR_obj.compute_free_energy_differences()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/tmp/nigel.palmer/ceb3/conan/p/sirebb1417ebb1a62/p/lib/python3.11/site-packages/pymbar/mbar.py", line 698, in compute_free_energy_differences
    Theta_ij = self._computeAsymptoticCovarianceMatrix(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/tmp/nigel.palmer/ceb3/conan/p/sirebb1417ebb1a62/p/lib/python3.11/site-packages/pymbar/mbar.py", line 1793, in _computeAsymptoticCovarianceMatrix
    check_w_normalized(W, N_k)
  File "/data/tmp/nigel.palmer/ceb3/conan/p/sirebb1417ebb1a62/p/lib/python3.11/site-packages/pymbar/utils.py", line 371, in check_w_normalized
    raise ParameterError(
pymbar.utils.ParameterError: Warning: Should have \sum_n W_nk = 1. Actual column sum for state 0 was 10.971530. 11 other columns have similar problems. 
This generally indicates the free energies are not converged.

While investigating it I have found that the size of the water box is significantly larger between the beginning and end of the somd-freenrg calculation.

In the image below the input waterbox is about 30A afterwards its about 350A.

image

Do you know what could be causing this?

I have included the input, output and config files in somd-freenrg_30~ABFE_Free_Discharge_0.000_2630_1.zip

(please complete the following information):

The command was "bin/sire_python" --ignore-ipython "share/Sire/scripts/somd-freenrg.py" -C Config.cfg

Additional context Add any other context about the problem here.

lohedges commented 4 months ago

Just to confirm that I see the same thing locally with the latest development version of Sire. I also see the same thing for both PME and RF cutoffs. I have also run the same perturbation using somd2 and see no such explosion.

fjclark commented 4 months ago

I've seen similar things so thought I'd jump in.

Box Expansion

I think the apparent expansion is due to the coordinates being unwrapped. You can wrap your coordinates with cpptraj - here's a wrap_coordinates.in script:

parm Input.prm7
trajin traj000000001.dcd
autoimage
trajout wrapped_trajectory.dcd
run

which can be run with cpptraj -i wrap_coordinates.in. When I rerun your input and use this script, wrapped_coordinates.dcd shows no apparent expansion.

PyMBAR Convergence

I assume you're using pymbar 4? I have a memory of seeing this error after upgrading from pymbar 3 to 4 on a perturbation which previously converged with pymbar 3, but didn't have time to dig into exactly why. Could you try downgrading to pymbar 3?

lohedges commented 4 months ago

Thanks, @fjclark. I can confirm that things look fine once the trajectory is wrapped.

fjclark commented 4 months ago

I've checked with sire 2023.1.3 and the wrapped trajectory is saved by default with the above input files.

lohedges commented 4 months ago

Thanks. I think Sire has a wrap boolean option that can be passed via the map kwarg. I'll see if I can get it to image correctly using that. I'm assuming the default behaviour for the DCDWriter must have changed for 2024.1.0, i.e. it writes the raw, unimaged coordinates. (This makes sense if you want to analyse things like particle displacements, etc.)

lohedges commented 4 months ago

The change in imaging is the result of this fix to OpenMM. Previously the MonteCarloBarostat would automatically image the molecules, which isn't desired if you wish to calculate displacements along a trajectory.

nigel-palmer-cresset commented 4 months ago

@lohedges @fjclark Thanks for the help.

cpptraj does fix the water boxes on my machine. You are correct that I am using pymbar 4. I will try to downgrade it and see if that helps.

Thanks

nigel-palmer-cresset commented 4 months ago

Downgrading pymbar to version 3 did fix the convergence error.

Many thanks for all your help.

lohedges commented 4 months ago

No problem, thanks for confirming.