Repository: openmm/openmm — pull request #4980 (cpp)
Title: Fixed bug in computing pressure

This fixes a bug that caused `computeCurrentPressure()` to return an incorrect value.  The tests were passing only because they happened to set all particle masses to 1.  See https://github.com/openmm/openmm/discussions/4926#discussioncomment-13503249.

Resolved issues:
title: Fixed bug in computing pressure
body: The `computeCurrentPressure()` function was returning incorrect pressure values due to an error in how molecular velocities were calculated. This issue was not detected by existing tests because they used a simplified setup where all particle masses were set to 1, masking the problem. 

The expected behavior is that the pressure calculation should correctly account for the masses of particles in the system, ensuring accurate results regardless of the mass distribution. This is critical for simulations involving systems with varying particle masses, where accurate pressure computation is essential for reliable results.

## IMPORTANT INSTRUCTIONS

1. If you edit a python file, you MUST run 'make PythonInstall' inside the build directory
2. If you edit a c++ file, you MUST run 'make -j4 && make install" inside the build directory

Hints:

Fix patch:
diff --git a/platforms/common/src/kernels/monteCarloBarostat.cc b/platforms/common/src/kernels/monteCarloBarostat.cc
index 2d6493fe52..12c739d892 100644
--- a/platforms/common/src/kernels/monteCarloBarostat.cc
+++ b/platforms/common/src/kernels/monteCarloBarostat.cc
@@ -79,7 +79,7 @@ KERNEL void computeMolecularKineticEnergy(int numMolecules, GLOBAL mixed4* RESTR
             molVel += mass*trimTo3(v);
             molMass += mass;
         }
-        molVel *= RECIP((mixed) numAtoms);
+        molVel *= RECIP((mixed) molMass);
 #if COMPONENTS == 1
         ke[0] += 0.5f*molMass*dot(molVel, molVel);
 #else
diff --git a/platforms/reference/src/SimTKReference/ReferenceMonteCarloBarostat.cpp b/platforms/reference/src/SimTKReference/ReferenceMonteCarloBarostat.cpp
index 3d3e536221..ba80d297ff 100644
--- a/platforms/reference/src/SimTKReference/ReferenceMonteCarloBarostat.cpp
+++ b/platforms/reference/src/SimTKReference/ReferenceMonteCarloBarostat.cpp
@@ -133,7 +133,7 @@ void ReferenceMonteCarloBarostat::computeMolecularKineticEnergy(const vector<Vec
             molVel += masses[atom]*velocities[atom];
             molMass += masses[atom];
         }
-        molVel /= molecule.size();
+        molVel /= molMass;
         if (components == 1)
             ke[0] += 0.5*molMass*molVel.dot(molVel);
         else {


Test patch:
diff --git a/tests/TestMonteCarloBarostat.h b/tests/TestMonteCarloBarostat.h
index 2f23f19d65..9f317c94b1 100644
--- a/tests/TestMonteCarloBarostat.h
+++ b/tests/TestMonteCarloBarostat.h
@@ -167,8 +167,8 @@ void testMolecularGas() {
     init_gen_rand(0, sfmt);
     for (int i = 0; i < numMolecules; ++i) {
         system.addParticle(1.0);
-        system.addParticle(1.0);
-        system.addParticle(1.0);
+        system.addParticle(2.0);
+        system.addParticle(3.0);
         Vec3 pos(initialLength*genrand_real2(sfmt), 0.5*initialLength*genrand_real2(sfmt), 2*initialLength*genrand_real2(sfmt));
         bonds->addBond(positions.size(), positions.size()+1, 0.1, 10.0);
         system.addConstraint(positions.size(), positions.size()+2, 0.1);