# Manual Calculation of Coulomb (SR) is inconsistent with Gromacs

**URL:** <https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777>\
**Category:** User discussions\
**Created:** [March 20, 2025, 10:03am UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777 "2025-03-20T10:03:54Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![CJWRW](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CJWRW](https://gromacs.bioexcel.eu/u/CJWRW)\
**Post date:** [March 20, 2025, 10:03am UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/1 "2025-03-20T10:03:54Z")

</div>

GROMACS version: 2024.1  
GROMACS modification: No

I have been trying to manually calculate the interaction energy of a test system, a simple 5x5x5 nm box of TIP3 water in the CHARMM36 forcefield.

I am using PME with a 1.2nm cutoff and for a single frame, gromacs calculates a coulomb SR potential of around -177,000.

I have taken this frame using MDAnalysis to get the atomic coordinates and the charges and attempted to calculate the interaction energy using equation 168 in the 2024.1 documentation. As I understand this, the entire coulomb SR interaction should be a sum over atoms of charge products (qij) times coulomb constant times the complementary error function of the inter-atomic separation times the ewald splitting constant divided by the interatomic distance.

When I apply this formula to the system with the same cutoff and ewald splitting parameter, my energy is inconsistent with gromacs, I get -153,000. Am I missing any extra contribution towards the SR coulomb?

I want to use this to calculate interaction energies for use in a widom particle insertion analysis and the standard shifted coulomb potential up to now does not seem to produce the correct shape excess chemical potential profile for membranes, hence the need to try and reproduce PME.

---

<div class="post-metadata">

**Author:** ![hess](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/hess/32/416_2.png) [@hess](https://gromacs.bioexcel.eu/u/hess)\
**Post date:** [March 20, 2025, 1:32pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/2 "2025-03-20T13:32:52Z")

</div>

I can think of two causes of the difference:

1. GROMACS shifts the pair interaction by a constant such that the potential is zero at the cut-off
2. Pairs up to 1-4 interactions are excluded.

Are you using the GROMACS TPI integrator for your test particle insertion?

---

<div class="post-metadata">

**Author:** ![CJWRW](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CJWRW](https://gromacs.bioexcel.eu/u/CJWRW)\
**Post date:** [March 20, 2025, 1:43pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/3 "2025-03-20T13:43:13Z")

</div>

The gromacs TPI integrator as far as I can see does not allow us to calculate how the excess chemical potential changes across the box so I am writing my own as a post-processing tool.

---

<div class="post-metadata">

**Author:** ![hess](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/hess/32/416_2.png) [@hess](https://gromacs.bioexcel.eu/u/hess)\
**Post date:** [March 20, 2025, 1:55pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/4 "2025-03-20T13:55:26Z")

</div>

You can insert around a given location using TPIC.

It wouldn’t be much work to add a scan along z.

---

<div class="post-metadata">

**Author:** ![CJWRW](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CJWRW](https://gromacs.bioexcel.eu/u/CJWRW)\
**Post date:** [March 20, 2025, 2:05pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/5 "2025-03-20T14:05:07Z")

</div>

We have a cavity locating script already written and outputting cavity coordinates for each trajectory frame, do you have any guidance on adding our pre-defined cavity coordinates to the trajectory as required by TPIC?

---

<div class="post-metadata">

**Author:** ![hess](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/hess/32/416_2.png) [@hess](https://gromacs.bioexcel.eu/u/hess)\
**Post date:** [March 20, 2025, 2:11pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/6 "2025-03-20T14:11:43Z")

</div>

I did this by spitting out pdb files using trjconv and pasting the extra line for the cavity location. That’s a bit cumbersome though. If you want to do a scan along z, the simplest way is to add a few line to tpi.cpp to read an environment variable that sets the z-location.

---

<div class="post-metadata">

**Author:** ![CJWRW](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CJWRW](https://gromacs.bioexcel.eu/u/CJWRW)\
**Post date:** [March 20, 2025, 2:20pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/7 "2025-03-20T14:20:47Z")

</div>

Thank you, I’ll take a look at modifying the .cpp file.

In the meantime, just to clarify, it is possible to do this by dumping the pdb or .gro files for each frame, adding the extra coordinate for TPIC, generate the tpr file using the tpic integrator and run this using gmx mdrun -v -deffnm tpic -rerun Frame\_n.gro

This should allow us to perform test particle insertion on a particular pre-defined cavity, in a particular frame so we can calculate across the box?

---

<div class="post-metadata">

**Author:** ![hess](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/hess/32/416_2.png) [@hess](https://gromacs.bioexcel.eu/u/hess)\
**Post date:** [March 20, 2025, 2:26pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/8 "2025-03-20T14:26:09Z")

</div>

Correct.

---

<div class="post-metadata">

**Author:** ![hess](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/hess/32/416_2.png) [@hess](https://gromacs.bioexcel.eu/u/hess)\
**Post date:** [March 20, 2025, 2:29pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/9 "2025-03-20T14:29:07Z")

</div>

Here is the modification for a scan along z for GROMACS 2025. You can modify zRange to your liking. I guess that the location should be at least zRange from the box edge. I haven’t tried the code though.

```auto
diff --git a/src/gromacs/mdrun/tpi.cpp b/src/gromacs/mdrun/tpi.cpp
index 70af0a4fed..cb85efab7d 100644
--- a/src/gromacs/mdrun/tpi.cpp
+++ b/src/gromacs/mdrun/tpi.cpp
@@ -728,6 +728,15 @@ double TestParticleInsertion::insertIntoFrame(const double t,
                                               gmx_wallcycle* wallCycleCounters,
                                               t_nrnb* nrnb)
 {
+ bool limitZ = false;
+ real zRange = 0.25;
+ real zLocation = 0;
+ if (char* result = std::getenv("GMX_TPI_Z"))
+ {
+ limitZ = true;
+ zLocation = std::strtod(result, nullptr);
+ }
+
     /* Copy the coordinates from the input trajectory */
     auto x = makeArrayRef(stateGlobal->x);
     for (Index i = 0; i < rerunX.ssize(); i++)
@@ -769,7 +778,14 @@ double TestParticleInsertion::insertIntoFrame(const double t,
                 /* Generate a random position in the box */
                 for (int d = 0; d < DIM; d++)
                 {
- xInit[d] = dist_(rng_) * box[d][d];
+ if (d < ZZ || !limitZ)
+ {
+ xInit[d] = dist_(rng_) * box[d][d];
+ }
+ else
+ {
+ xInit[d] = zLocation + (dist_(rng_) * 2 - 1) * zRange;
+ }
                 }
             }
         }

```

---

<div class="post-metadata">

**Author:** ![hess](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/hess/32/416_2.png) [@hess](https://gromacs.bioexcel.eu/u/hess)\
**Post date:** [March 20, 2025, 2:32pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/10 "2025-03-20T14:32:03Z")

</div>

I also added the shift of the Ewald direct potential to the manual.

---

<div class="post-metadata">

**Author:** ![CJWRW](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CJWRW](https://gromacs.bioexcel.eu/u/CJWRW)\
**Post date:** [March 20, 2025, 2:44pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/11 "2025-03-20T14:44:38Z")

</div>

That is brilliant, thank you for your time!

If I could trouble you for one more thing, when gromacs is calculating the short range coulomb interaction using PME, is there any other contribution to the short range potential other than the direct space sum of equation 168 in the gromacs manual. I could get my plain and shifted coulomb potential python code to return consistent results with gromacs but the SR coulomb using PME varied greatly from that returned by gromacs.

---

<div class="post-metadata">

**Author:** ![hess](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/hess/32/416_2.png) [@hess](https://gromacs.bioexcel.eu/u/hess)\
**Post date:** [March 20, 2025, 2:47pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/12 "2025-03-20T14:47:17Z")

</div>

There shouldn’t be. The long-range correction terms are added to the LR energy.

---

<div class="post-metadata">

**Author:** ![CJWRW](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CJWRW](https://gromacs.bioexcel.eu/u/CJWRW)\
**Post date:** [March 20, 2025, 2:57pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/13 "2025-03-20T14:57:49Z")

</div>

It’s quite strange, I have made sure I am using the same gaussian width (beta) parameter as gromacs and cutoff however the potential is consistently different.

---

<div class="post-metadata">

**Author:** ![hess](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/hess/32/416_2.png) [@hess](https://gromacs.bioexcel.eu/u/hess)\
**Post date:** [March 20, 2025, 3:05pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/14 "2025-03-20T15:05:58Z")

</div>

I think I know the source of the difference. GROMACS includes the mesh energy of excluded pairs in the mesh energy. This energy is subtracted in the short range term.

---

<div class="post-metadata">

**Author:** ![CJWRW](https://avatars.discourse-cdn.com/v4/letter/c/e274bd/32.png) [@CJWRW](https://gromacs.bioexcel.eu/u/CJWRW)\
**Post date:** [March 20, 2025, 3:32pm UTC](https://gromacs.bioexcel.eu/t/manual-calculation-of-coulomb-sr-is-inconsistent-with-gromacs/11777/15 "2025-03-20T15:32:49Z")

</div>

I’ll take a look and see if this brings the energies in line, thank you!
