From 4f70905573fbbe1ee697f4bd72434b130597f650 Mon Sep 17 00:00:00 2001
From: fiskissimo <luca.pesce@live.it>
Date: Wed, 17 Oct 2018 18:38:27 +0300
Subject: [PATCH] Fix: Segmentation fault whenever more than 2 CVs are used for
 METAD with REWEIGHTING_NGRID #328

I tried to interpret and fix the code and for me it works nicely with 1 to 3 CVs.
Here a brief description: the index i spans through the whole reweghting-grid (according to the mpi rank in case of mpi).
rewf_grid_ contains the size on each dimension of the reweghting-grid. t_index is the array containing the coordinates of i in each dimension of the grid. The indexes had to be changed from i to j in line 1967, which indeed spans correctly the dimensions of the grid. In 1968 in order to get the correct index, we need to subtract the former index (ncv-2 -- instead of "ncv-1" which is yet not assigned -- from what is left of kk).

I didn't attempt any further test.

Please let me know if I had misinterpreted the code.

Fixes #399
---
 src/bias/MetaD.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/bias/MetaD.cpp b/src/bias/MetaD.cpp
index 852cf78c5..6c3b87fe6 100644
--- a/src/bias/MetaD.cpp
+++ b/src/bias/MetaD.cpp
@@ -1542,8 +1542,8 @@ void MetaD::computeReweightingFactor()
   for(unsigned i=rank; i<ntotgrid; i+=stride) {
     t_index[0]=(i%rewf_grid_[0]);
     unsigned kk=i;
-    for(unsigned j=1; j<ncv-1; ++j) { kk=(kk-t_index[j-1])/rewf_grid_[i-1]; t_index[j]=(kk%rewf_grid_[i]); }
-    if( ncv>=2 ) t_index[ncv-1]=((kk-t_index[ncv-1])/rewf_grid_[ncv-2]);
+    for(unsigned j=1; j<ncv-1; ++j) { kk=(kk-t_index[j-1])/rewf_grid_[j-1]; t_index[j]=(kk%rewf_grid_[j]); }
+    if( ncv>=2 ) t_index[ncv-1]=((kk-t_index[ncv-2])/rewf_grid_[ncv-2]);
 
     for(unsigned j=0; j<ncv; ++j) vals[j]=dmin[j] + t_index[j]*grid_spacing[j];
 
-- 
GitLab