Merge branch release-2018
[alexxy/gromacs.git] / src / gromacs / gmxpreprocess / solvate.cpp
index e56719cc63bc4bf4f95f5e548ab145edfb435c78..bfe2722db0f8e2181ca8731819c985c7c47f35f1 100644 (file)
@@ -38,7 +38,7 @@
 
 #include "solvate.h"
 
-#include <string.h>
+#include <cstring>
 
 #include <algorithm>
 #include <vector>
@@ -75,12 +75,14 @@ typedef struct {
     int   res0;
 } t_moltypes;
 
-static void sort_molecule(t_atoms **atoms_solvt, std::vector<RVec> *x,
+static void sort_molecule(t_atoms          **atoms_solvt,
+                          t_atoms          **newatoms,
+                          std::vector<RVec> *x,
                           std::vector<RVec> *v)
 {
     int         atnr, i, j, moltp = 0, nrmoltypes, resi_o, resi_n, resnr;
     t_moltypes *moltypes;
-    t_atoms    *atoms, *newatoms;
+    t_atoms    *atoms;
 
     fprintf(stderr, "Sorting configuration\n");
 
@@ -97,8 +99,7 @@ static void sort_molecule(t_atoms **atoms_solvt, std::vector<RVec> *x,
             moltp = -1;
             for (j = 0; (j < nrmoltypes) && (moltp == -1); j++)
             {
-                /* cppcheck-suppress nullPointer
-                 * moltypes is guaranteed to be allocated because otherwise
+                /* moltypes is guaranteed to be allocated because otherwise
                  * nrmoltypes is 0. */
                 if (strcmp(*(atoms->resinfo[atoms->atom[i].resind].name), moltypes[j].name) == 0)
                 {
@@ -153,10 +154,10 @@ static void sort_molecule(t_atoms **atoms_solvt, std::vector<RVec> *x,
         }
 
         /* now put them there: */
-        snew(newatoms, 1);
-        init_t_atoms(newatoms, atoms->nr, FALSE);
-        newatoms->nres = atoms->nres;
-        snew(newatoms->resinfo, atoms->nres);
+        snew(*newatoms, 1);
+        init_t_atoms(*newatoms, atoms->nr, FALSE);
+        (*newatoms)->nres = atoms->nres;
+        srenew((*newatoms)->resinfo, atoms->nres);
         std::vector<RVec> newx(x->size());
         std::vector<RVec> newv(v->size());
 
@@ -172,14 +173,14 @@ static void sort_molecule(t_atoms **atoms_solvt, std::vector<RVec> *x,
                 if (strcmp(*atoms->resinfo[resi_o].name, moltypes[moltp].name) == 0)
                 {
                     /* Copy the residue info */
-                    newatoms->resinfo[resi_n]    = atoms->resinfo[resi_o];
-                    newatoms->resinfo[resi_n].nr = resnr;
+                    (*newatoms)->resinfo[resi_n]    = atoms->resinfo[resi_o];
+                    (*newatoms)->resinfo[resi_n].nr = resnr;
                     /* Copy the atom info */
                     do
                     {
-                        newatoms->atom[j]        = atoms->atom[i];
-                        newatoms->atomname[j]    = atoms->atomname[i];
-                        newatoms->atom[j].resind = resi_n;
+                        (*newatoms)->atom[j]        = atoms->atom[i];
+                        (*newatoms)->atomname[j]    = atoms->atomname[i];
+                        (*newatoms)->atom[j].resind = resi_n;
                         copy_rvec((*x)[i], newx[j]);
                         if (!v->empty())
                         {
@@ -207,7 +208,7 @@ static void sort_molecule(t_atoms **atoms_solvt, std::vector<RVec> *x,
 
         /* put them back into the original arrays and throw away temporary arrays */
         done_atom(atoms);
-        *atoms_solvt = newatoms;
+        *atoms_solvt = (*newatoms);
         std::swap(*x, newx);
         std::swap(*v, newv);
     }
@@ -710,7 +711,8 @@ static void add_solv(const char *fn, t_topology *top,
     }
 
     /* Sort the solvent mixture, not the protein... */
-    sort_molecule(&atoms_solvt, &x_solvt, &v_solvt);
+    t_atoms *newatoms = nullptr;
+    sort_molecule(&atoms_solvt, &newatoms, &x_solvt, &v_solvt);
 
     // Merge the two configurations.
     x->insert(x->end(), x_solvt.begin(), x_solvt.end());
@@ -727,6 +729,11 @@ static void add_solv(const char *fn, t_topology *top,
 
     done_top(top_solvt);
     sfree(top_solvt);
+    if (newatoms)
+    {
+        done_atom(newatoms);
+        sfree(newatoms);
+    }
 }
 
 static void update_top(t_atoms *atoms, int firstSolventResidueIndex, matrix box, int NFILE, t_filenm fnm[],
@@ -736,14 +743,14 @@ static void update_top(t_atoms *atoms, int firstSolventResidueIndex, matrix box,
     char         buf[STRLEN], buf2[STRLEN], *temp;
     const char  *topinout;
     int          line;
-    gmx_bool     bSystem;
+    bool         bSystem;
     int          i;
     double       mtot;
     real         vol, mm;
 
-    int          nsol                     =  atoms->nres - firstSolventResidueIndex;
+    int          nsol = atoms->nres - firstSolventResidueIndex;
 
-    mtot                     = 0;
+    mtot = 0;
     for (i = 0; (i < atoms->nr); i++)
     {
         gmx_atomprop_query(aps, epropMass,
@@ -770,7 +777,7 @@ static void update_top(t_atoms *atoms, int firstSolventResidueIndex, matrix box,
         fpin    = gmx_ffopen(topinout, "r");
         fpout   = gmx_fopen_temporary(temporary_filename);
         line    = 0;
-        bSystem = FALSE;
+        bSystem = false;
         while (fgets(buf, STRLEN, fpin))
         {
             line++;
@@ -803,7 +810,7 @@ static void update_top(t_atoms *atoms, int firstSolventResidueIndex, matrix box,
                 if (buf2[0] && (!strstr(buf2, " water")) )
                 {
                     sprintf(buf, "%s in water\n", buf2);
-                    bSystem = FALSE;
+                    bSystem = false;
                 }
             }
             fprintf(fpout, "%s", buf);
@@ -819,7 +826,7 @@ static void update_top(t_atoms *atoms, int firstSolventResidueIndex, matrix box,
             // Iterate through solvent molecules and increment a count until new resname found
             for (int i = firstSolventResidueIndex; i < atoms->nres; i++)
             {
-                if ((currRes.compare(*atoms->resinfo[i].name) == 0))
+                if ((currRes == *atoms->resinfo[i].name))
                 {
                     resCount += 1;
                 }
@@ -1017,7 +1024,6 @@ int gmx_solvate(int argc, char *argv[])
     done_top(top);
     sfree(top);
     output_env_done(oenv);
-    done_filenms(NFILE, fnm);
 
     return 0;
 }