Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_make_ndx.c
index 1cb884fcfdbc73f467c2ead0eba062b7b0448174..55dc3ab47b4027dda9599d1634b2d3994fb9d124 100644 (file)
@@ -1,58 +1,61 @@
 /*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *                This source code is part of
- *
- *                 G   R   O   M   A   C   S
- *
- *          GROningen MAchine for Chemical Simulations
- *
- *                        VERSION 3.2.0
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2004, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
+ * Copyright (c) 2001-2004, The GROMACS development team.
+ * Copyright (c) 2012,2013,2014, 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.
+ *
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
  * of the License, or (at your option) any later version.
  *
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
  *
- * For more info, check our website at http://www.gromacs.org
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
  *
- * And Hey:
- * Green Red Orange Magenta Azure Cyan Skyblue
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gmxpre.h"
 
 #include <ctype.h>
-#include "sysstuff.h"
-#include "strdb.h"
-#include "futil.h"
-#include "macros.h"
-#include "string2.h"
-#include "statutil.h"
-#include "confio.h"
-#include "copyrite.h"
-#include "typedefs.h"
-#include "index.h"
-#include "smalloc.h"
-#include "vec.h"
-#include "index.h"
-
-#define MAXNAMES 30
-#define NAME_LEN 30
+#include <string.h>
+
+#include "gromacs/commandline/pargs.h"
+#include "gromacs/fileio/confio.h"
+#include "gromacs/legacyheaders/macros.h"
+#include "gromacs/legacyheaders/typedefs.h"
+#include "gromacs/math/vec.h"
+#include "gromacs/topology/block.h"
+#include "gromacs/topology/index.h"
+#include "gromacs/utility/cstringutil.h"
+#include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/futil.h"
+#include "gromacs/utility/smalloc.h"
+
+/* It's not nice to have size limits, but we should not spend more time
+ * on this ancient tool, but instead use the new selection library.
+ */
+#define MAXNAMES 1024
+#define NAME_LEN 1024
 
 gmx_bool bCase = FALSE;
 
@@ -110,7 +113,7 @@ static int or_groups(atom_id nr1, atom_id *at1, atom_id nr2, atom_id *at2,
             }
         }
 
-        printf("Merged two groups with OR: %u %u -> %u\n", nr1, nr2, *nr);
+        printf("Merged two groups with OR: %d %d -> %d\n", nr1, nr2, *nr);
     }
 
     return *nr;
@@ -134,7 +137,7 @@ static int and_groups(atom_id nr1, atom_id *at1, atom_id nr2, atom_id *at2,
         }
     }
 
-    printf("Merged two groups with AND: %u %u -> %u\n", nr1, nr2, *nr);
+    printf("Merged two groups with AND: %d %d -> %d\n", nr1, nr2, *nr);
 
     return *nr;
 }
@@ -274,7 +277,7 @@ static gmx_bool parse_string(char **string, int *nr, int ngrps, char **grpname)
     {
         c = (*string)[0];
         (*string)++;
-        s  = strdup((*string));
+        s  = gmx_strdup((*string));
         sp = strchr(s, c);
         if (sp != NULL)
         {
@@ -313,14 +316,14 @@ static int select_atomnumbers(char **string, t_atoms *atoms, atom_id n1,
                 index[*nr] = i;
                 (*nr)++;
             }
-            printf("Found %u atom%s in range %u-%d\n", *nr, (*nr == 1) ? "" : "s", n1, up);
+            printf("Found %d atom%s in range %d-%d\n", *nr, (*nr == 1) ? "" : "s", n1, up);
             if (n1 == up)
             {
-                sprintf(buf, "a_%u", n1);
+                sprintf(buf, "a_%d", n1);
             }
             else
             {
-                sprintf(buf, "a_%u-%d", n1, up);
+                sprintf(buf, "a_%d-%d", n1, up);
             }
             strcpy(gname, buf);
         }
@@ -386,15 +389,15 @@ static int select_residuenumbers(char **string, t_atoms *atoms,
                 }
             }
         }
-        printf("Found %u atom%s with res.nr. in range %u-%d\n",
+        printf("Found %d atom%s with res.nr. in range %d-%d\n",
                *nr, (*nr == 1) ? "" : "s", n1, up);
         if (n1 == up)
         {
-            sprintf(buf, "r_%u", n1);
+            sprintf(buf, "r_%d", n1);
         }
         else
         {
-            sprintf(buf, "r_%u-%d", n1, up);
+            sprintf(buf, "r_%d-%d", n1, up);
         }
         strcpy(gname, buf);
     }
@@ -461,15 +464,15 @@ static int select_residueindices(char **string, t_atoms *atoms,
                 }
             }
         }
-        printf("Found %u atom%s with resind.+1 in range %u-%d\n",
+        printf("Found %d atom%s with resind.+1 in range %d-%d\n",
                *nr, (*nr == 1) ? "" : "s", n1, up);
         if (n1 == up)
         {
-            sprintf(buf, "r_%u", n1);
+            sprintf(buf, "r_%d", n1);
         }
         else
         {
-            sprintf(buf, "r_%u-%d", n1, up);
+            sprintf(buf, "r_%d-%d", n1, up);
         }
         strcpy(gname, buf);
     }
@@ -529,7 +532,7 @@ static gmx_bool atoms_from_residuenumbers(t_atoms *atoms, int group, t_blocka *b
             }
         }
     }
-    printf("Found %u atom%s in %d residues from group %s\n",
+    printf("Found %d atom%s in %d residues from group %s\n",
            *nr, (*nr == 1) ? "" : "s", j1-j0, gname);
     return *nr;
 }
@@ -592,7 +595,7 @@ static int select_chainnames(t_atoms *atoms, int n_names, char **names,
             (*nr)++;
         }
     }
-    printf("Found %u atom%s with chain identifier%s",
+    printf("Found %d atom%s with chain identifier%s",
            *nr, (*nr == 1) ? "" : "s", (n_names == 1) ? "" : "s");
     for (j = 0; (j < n_names); j++)
     {
@@ -632,7 +635,7 @@ static int select_atomnames(t_atoms *atoms, int n_names, char **names,
             (*nr)++;
         }
     }
-    printf("Found %u atoms with %s%s",
+    printf("Found %d atoms with %s%s",
            *nr, bType ? "type" : "name", (n_names == 1) ? "" : "s");
     for (j = 0; (j < n_names); j++)
     {
@@ -665,7 +668,7 @@ static int select_residuenames(t_atoms *atoms, int n_names, char **names,
             (*nr)++;
         }
     }
-    printf("Found %u atoms with residue name%s", *nr, (n_names == 1) ? "" : "s");
+    printf("Found %d atoms with residue name%s", *nr, (n_names == 1) ? "" : "s");
     for (j = 0; (j < n_names); j++)
     {
         printf(" %s", names[j]);
@@ -743,7 +746,7 @@ static void remove_group(int nr, int nr2, t_blocka *block, char ***gn)
             {
                 block->index[i] = block->index[i+1]-shift;
             }
-            name = strdup((*gn)[nr]);
+            name = gmx_strdup((*gn)[nr]);
             sfree((*gn)[nr]);
             for (i = nr; i < block->nr-1; i++)
             {
@@ -786,13 +789,13 @@ static void split_group(t_atoms *atoms, int sel_nr, t_blocka *block, char ***gn,
             srenew(*gn, block->nr);
             if (bAtom)
             {
-                sprintf(buf, "%s_%s_%u", (*gn)[sel_nr], *atoms->atomname[a], a+1);
+                sprintf(buf, "%s_%s_%d", (*gn)[sel_nr], *atoms->atomname[a], a+1);
             }
             else
             {
                 sprintf(buf, "%s_%s_%d", (*gn)[sel_nr], name, atoms->resinfo[resind].nr);
             }
-            (*gn)[block->nr-1] = strdup(buf);
+            (*gn)[block->nr-1] = gmx_strdup(buf);
         }
         block->a[block->nra] = a;
         block->nra++;
@@ -843,8 +846,12 @@ static int split_chain(t_atoms *atoms, rvec *x,
                 {
                     rvec_sub(x[ca_end], x[i], vec);
                 }
+                else
+                {
+                    break;
+                }
             }
-            while ((i < natoms) && (norm(vec) < 0.45));
+            while (norm(vec) < 0.45);
 
             end[nchain] = ca_end;
             while ((end[nchain]+1 < natoms) &&
@@ -866,7 +873,7 @@ static int split_chain(t_atoms *atoms, rvec *x,
     }
     for (j = 0; j < nchain; j++)
     {
-        printf("%d:%6u atoms (%u to %u)\n",
+        printf("%d:%6d atoms (%d to %d)\n",
                j+1, end[j]-start[j]+1, start[j]+1, end[j]+1);
     }
 
@@ -879,7 +886,7 @@ static int split_chain(t_atoms *atoms, rvec *x,
             srenew(block->index, block->nr+1);
             srenew(*gn, block->nr);
             sprintf(buf, "%s_chain%d", (*gn)[sel_nr], j+1);
-            (*gn)[block->nr-1] = strdup(buf);
+            (*gn)[block->nr-1] = gmx_strdup(buf);
             for (i = block->index[sel_nr]; i < block->index[sel_nr+1]; i++)
             {
                 a = block->a[i];
@@ -1093,7 +1100,7 @@ static gmx_bool parse_entry(char **string, int natoms, t_atoms *atoms,
             gname[i] = gname[i-1];
         }
         gname[0] = '!';
-        printf("Complemented group: %u atoms\n", *nr);
+        printf("Complemented group: %d atoms\n", *nr);
     }
 
     return bRet;
@@ -1191,7 +1198,7 @@ static void edit_index(int natoms, t_atoms *atoms, rvec *x, t_blocka *block, cha
             }
             for (i = i0; i < i1; i++)
             {
-                printf("%3d %-20s: %5u atoms\n", i, (*gn)[i],
+                printf("%3d %-20s: %5d atoms\n", i, (*gn)[i],
                        block->index[i+1]-block->index[i]);
             }
             newgroup = NOTSET;
@@ -1322,7 +1329,7 @@ static void edit_index(int natoms, t_atoms *atoms, rvec *x, t_blocka *block, cha
                 {
                     sscanf(string, "%s", gname);
                     sfree((*gn)[sel_nr]);
-                    (*gn)[sel_nr] = strdup(gname);
+                    (*gn)[sel_nr] = gmx_strdup(gname);
                 }
             }
         }
@@ -1440,7 +1447,7 @@ static void edit_index(int natoms, t_atoms *atoms, rvec *x, t_blocka *block, cha
                 copy2block(nr, index, block);
                 srenew(*gn, block->nr);
                 newgroup        = block->nr-1;
-                (*gn)[newgroup] = strdup(gname);
+                (*gn)[newgroup] = gmx_strdup(gname);
             }
             else
             {
@@ -1498,27 +1505,33 @@ void merge_blocks(t_blocka *dest, t_blocka *source)
 int gmx_make_ndx(int argc, char *argv[])
 {
     const char     *desc[] = {
-        "Index groups are necessary for almost every gromacs program.",
+        "Index groups are necessary for almost every GROMACS program.",
         "All these programs can generate default index groups. You ONLY",
-        "have to use [TT]make_ndx[tt] when you need SPECIAL index groups.",
+        "have to use [THISMODULE] when you need SPECIAL index groups.",
         "There is a default index group for the whole system, 9 default",
         "index groups for proteins, and a default index group",
         "is generated for every other residue name.[PAR]",
-        "When no index file is supplied, also [TT]make_ndx[tt] will generate the",
+        "When no index file is supplied, also [THISMODULE] will generate the",
         "default groups.",
         "With the index editor you can select on atom, residue and chain names",
         "and numbers.",
         "When a run input file is supplied you can also select on atom type.",
         "You can use NOT, AND and OR, you can split groups",
         "into chains, residues or atoms. You can delete and rename groups.[PAR]",
-        "The atom numbering in the editor and the index file starts at 1."
+        "The atom numbering in the editor and the index file starts at 1.[PAR]",
+        "The [TT]-twin[tt] switch duplicates all index groups with an offset of",
+        "[TT]-natoms[tt], which is useful for Computational Electrophysiology",
+        "double-layer membrane setups."
     };
 
-    static int      natoms   = 0;
-    static gmx_bool bVerbose = FALSE;
-    t_pargs         pa[]     = {
+    static int      natoms     = 0;
+    static gmx_bool bVerbose   = FALSE;
+    static gmx_bool bDuplicate = FALSE;
+    t_pargs         pa[]       = {
         { "-natoms",  FALSE, etINT, {&natoms},
           "set number of atoms (default: read from coordinate or index file)" },
+        { "-twin",     FALSE, etBOOL, {&bDuplicate},
+          "Duplicate all index groups with an offset of -natoms" },
         { "-verbose", FALSE, etBOOL, {&bVerbose},
           "HIDDENVerbose output" }
     };
@@ -1545,8 +1558,11 @@ int gmx_make_ndx(int argc, char *argv[])
     };
 #define NFILE asize(fnm)
 
-    parse_common_args(&argc, argv, 0, NFILE, fnm, NPA, pa, asize(desc), desc,
-                      0, NULL, &oenv);
+    if (!parse_common_args(&argc, argv, 0, NFILE, fnm, NPA, pa, asize(desc), desc,
+                           0, NULL, &oenv))
+    {
+        return 0;
+    }
 
     stxfile = ftp2fn_null(efSTX, NFILE, fnm);
     if (opt2bSet("-n", NFILE, fnm))
@@ -1619,9 +1635,7 @@ int gmx_make_ndx(int argc, char *argv[])
 
     edit_index(natoms, atoms, x, block, &gnames, bVerbose);
 
-    write_index(ndxoutfile, block, gnames);
-
-    thanx(stderr);
+    write_index(ndxoutfile, block, gnames, bDuplicate, natoms);
 
     return 0;
 }