Converted essentialdynamics module to C++.
authorRossen Apostolov <rossen@kth.se>
Tue, 9 Sep 2014 12:15:51 +0000 (14:15 +0200)
committerDavid van der Spoel <spoel@xray.bmc.uu.se>
Fri, 4 Sep 2015 11:32:04 +0000 (13:32 +0200)
Small refactoring in do_radcon. Defined formats for
reading eigenvector values.

Change-Id: I7dd4fd782b0ff595b78f1f75cbcb698322067add

src/gromacs/essentialdynamics/CMakeLists.txt
src/gromacs/essentialdynamics/edsam.cpp [moved from src/gromacs/essentialdynamics/edsam.c with 98% similarity]

index 0b5ec4bc2634b191d4882ea16094717c3debd457..1edaf7a796dc55c901aa7ae376905af4fe4a2153 100644 (file)
@@ -1,7 +1,7 @@
 #
 # This file is part of the GROMACS molecular simulation package.
 #
-# Copyright (c) 2014, by the GROMACS development team, led by
+# Copyright (c) 2014,2015, by the GROMACS development team, led by
 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
 # and including many others, as listed in the AUTHORS file in the
 # top-level source directory and at http://www.gromacs.org.
@@ -32,7 +32,7 @@
 # To help us fund GROMACS development, we humbly ask that you cite
 # the research papers on the package. Check out http://www.gromacs.org.
 
-file(GLOB ESSENTIALDYNAMICS_SOURCES *.cpp *.c)
+file(GLOB ESSENTIALDYNAMICS_SOURCES *.cpp)
 set(LIBGROMACS_SOURCES ${LIBGROMACS_SOURCES} ${ESSENTIALDYNAMICS_SOURCES} PARENT_SCOPE)
 
 if (BUILD_TESTING)
similarity index 98%
rename from src/gromacs/essentialdynamics/edsam.c
rename to src/gromacs/essentialdynamics/edsam.cpp
index 0e479c7972474690d2584e22548ec668ba08fdf1..9caf58aeaa0b23c491c82ea1505df0263b170187 100644 (file)
 #include "gromacs/topology/mtop_util.h"
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/smalloc.h"
 
+
 /* We use the same defines as in broadcaststructs.cpp here */
 #define  block_bc(cr,   d) gmx_bcast(     sizeof(d),     &(d), (cr))
 #define nblock_bc(cr, nr, d) gmx_bcast((nr)*sizeof((d)[0]), (d), (cr))
 #define   snew_bc(cr, d, nr) { if (!MASTER(cr)) {snew((d), (nr)); }}
 
-/* These macros determine the column width in the output file */
-#define EDcol_sfmt "%17s"
-#define EDcol_efmt "%17.5e"
-#define EDcol_ffmt "%17f"
-
 /* enum to identify the type of ED: none, normal ED, flooding */
 enum {
     eEDnone, eEDedsam, eEDflood, eEDnr
@@ -231,6 +228,17 @@ static void init_edsamstate(gmx_edsam_t ed, edsamstate_t *EDstate);
 static void write_edo_legend(gmx_edsam_t ed, int nED, const output_env_t oenv);
 /* End function declarations */
 
+/* Define formats for the column width in the output file */
+const char EDcol_sfmt[]         = "%17s";
+const char EDcol_efmt[]         = "%17.5e";
+const char EDcol_ffmt[]         = "%17f";
+
+/* Define a formats for reading, otherwise cppcheck complains for scanf without width limits */
+const char max_ev_fmt_d[]       = "%7d";             /* Number of eigenvectors. 9,999,999 should be enough */
+const char max_ev_fmt_lf[]      = "%12lf";
+const char max_ev_fmt_dlf[]     = "%7d%12lf";
+const char max_ev_fmt_dlflflf[] = "%7d%12lf%12lf%12lf";
+const char max_ev_fmt_lelele[]  = "%12le%12le%12le";
 
 /* Do we have to perform essential dynamics constraints or possibly only flooding
  * for any of the ED groups? */
@@ -1439,11 +1447,10 @@ static int read_checked_edint(FILE *file, const char *label)
     char line[STRLEN+1];
     int  idum;
 
-
     fgets2 (line, STRLEN, file);
     check(line, label);
     fgets2 (line, STRLEN, file);
-    sscanf (line, "%d", &idum);
+    sscanf (line, max_ev_fmt_d, &idum);
     return idum;
 }
 
@@ -1467,7 +1474,7 @@ static int read_edint(FILE *file, gmx_bool *bEOF)
         *bEOF = TRUE;
         return -1;
     }
-    sscanf (line, "%d", &idum);
+    sscanf (line, max_ev_fmt_d, &idum);
     *bEOF = FALSE;
     return idum;
 }
@@ -1482,7 +1489,7 @@ static real read_checked_edreal(FILE *file, const char *label)
     fgets2 (line, STRLEN, file);
     check(line, label);
     fgets2 (line, STRLEN, file);
-    sscanf (line, "%lf", &rdum);
+    sscanf (line, max_ev_fmt_lf, &rdum);
     return (real) rdum; /* always read as double and convert to single */
 }
 
@@ -1497,7 +1504,7 @@ static void read_edx(FILE *file, int number, int *anrs, rvec *x)
     for (i = 0; i < number; i++)
     {
         fgets2 (line, STRLEN, file);
-        sscanf (line, "%d%lf%lf%lf", &anrs[i], &d[0], &d[1], &d[2]);
+        sscanf (line, max_ev_fmt_dlflflf, &anrs[i], &d[0], &d[1], &d[2]);
         anrs[i]--; /* we are reading FORTRAN indices */
         for (j = 0; j < 3; j++)
         {
@@ -1517,7 +1524,7 @@ static void scan_edvec(FILE *in, int nr, rvec *vec)
     for (i = 0; (i < nr); i++)
     {
         fgets2 (line, STRLEN, in);
-        sscanf (line, "%le%le%le", &x, &y, &z);
+        sscanf (line, max_ev_fmt_lelele, &x, &y, &z);
         vec[i][XX] = x;
         vec[i][YY] = y;
         vec[i][ZZ] = z;
@@ -1552,7 +1559,7 @@ static void read_edvec(FILE *in, int nr, t_eigvec *tvec, gmx_bool bReadRefproj,
             fgets2 (line, STRLEN, in);
             if (bReadRefproj) /* ONLY when using flooding as harmonic restraint */
             {
-                nscan = sscanf(line, "%d%lf%lf%lf", &idum, &rdum, &refproj_dum, &refprojslope_dum);
+                nscan = sscanf(line, max_ev_fmt_dlflflf, &idum, &rdum, &refproj_dum, &refprojslope_dum);
                 /* Zero out values which were not scanned */
                 switch (nscan)
                 {
@@ -1581,7 +1588,7 @@ static void read_edvec(FILE *in, int nr, t_eigvec *tvec, gmx_bool bReadRefproj,
             }
             else /* Normal flooding */
             {
-                nscan = sscanf(line, "%d%lf", &idum, &rdum);
+                nscan = sscanf(line, max_ev_fmt_dlf, &idum, &rdum);
                 if (nscan != 2)
                 {
                     gmx_fatal(FARGS, "Expected 2 values for flooding vec: <nr> <stpsz>\n");
@@ -2094,7 +2101,6 @@ static void do_radacc(rvec *xcoll, t_edpar *edi)
     if (rad < edi->vecs.radacc.radius)
     {
         ratio = edi->vecs.radacc.radius/rad - 1.0;
-        rad   = edi->vecs.radacc.radius;
     }
     else
     {
@@ -2135,7 +2141,6 @@ static void do_radcon(rvec *xcoll, t_edpar *edi)
     if (edi->buf->do_radcon != NULL)
     {
         bFirst = FALSE;
-        loc    = edi->buf->do_radcon;
     }
     else
     {
@@ -2181,23 +2186,13 @@ static void do_radcon(rvec *xcoll, t_edpar *edi)
                 rvec_inc(xcoll[j], vec_dum);
             }
         }
+
     }
     else
     {
         edi->vecs.radcon.radius = rad;
     }
 
-    if (rad != edi->vecs.radcon.radius)
-    {
-        rad = 0.0;
-        for (i = 0; i < edi->vecs.radcon.neig; i++)
-        {
-            /* calculate the projections, radius */
-            loc->proj[i] = projectx(edi, xcoll, edi->vecs.radcon.vec[i]);
-            rad         += pow(loc->proj[i] - edi->vecs.radcon.refproj[i], 2);
-        }
-        rad = sqrt(rad);
-    }
 }
 
 
@@ -2443,7 +2438,7 @@ static void add_to_string_aligned(char **str, char *buf)
 }
 
 
-static void nice_legend(const char ***setname, int *nsets, char **LegendStr, char *value, char *unit, char EDgroupchar)
+static void nice_legend(const char ***setname, int *nsets, char **LegendStr, const char *value, const char *unit, char EDgroupchar)
 {
     char tmp[STRLEN], tmp2[STRLEN];
 
@@ -2709,6 +2704,9 @@ void init_edsam(const gmx_mtop_t *mtop,
         }
         /* Reset pointer to first ED data set which contains the actual ED data */
         edi = ed->edpar;
+        GMX_ASSERT(NULL != edi,
+                   "The pointer to the essential dynamics parameters is undefined");
+
         /* Loop over all ED/flooding data sets (usually only one, though) */
         for (nr_edi = 1; nr_edi <= EDstate->nED; nr_edi++)
         {