Enable missing declarations warning
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_make_edi.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016,2017, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /* The make_edi program was generously contributed by Oliver Lange, based
36  * on the code from g_anaeig. You can reach him as olange@gwdg.de. He
37  * probably also holds copyright to the following code.
38  */
39 #include "gmxpre.h"
40
41 #include <cctype>
42 #include <cmath>
43 #include <cstdlib>
44 #include <cstring>
45
46 #include "gromacs/commandline/pargs.h"
47 #include "gromacs/fileio/confio.h"
48 #include "gromacs/fileio/pdbio.h"
49 #include "gromacs/fileio/xvgr.h"
50 #include "gromacs/gmxana/eigio.h"
51 #include "gromacs/gmxana/gmx_ana.h"
52 #include "gromacs/math/functions.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/topology/index.h"
55 #include "gromacs/topology/topology.h"
56 #include "gromacs/utility/arraysize.h"
57 #include "gromacs/utility/cstringutil.h"
58 #include "gromacs/utility/fatalerror.h"
59 #include "gromacs/utility/futil.h"
60 #include "gromacs/utility/smalloc.h"
61
62 typedef struct
63 {
64     real        deltaF0;
65     gmx_bool    bHarmonic;
66     gmx_bool    bConstForce;   /* Do constant force flooding instead of
67                                   evaluating a flooding potential             */
68     real        tau;
69     real        deltaF;
70     real        kT;
71     real        constEfl;
72     real        alpha2;
73 } t_edflood;
74
75
76 /* This type is for the average, reference, target, and origin structure   */
77 typedef struct edix
78 {
79     int          nr;            /* number of atoms this structure contains */
80     int         *anrs;          /* atom index numbers                      */
81     rvec        *x;             /* positions                               */
82     real        *sqrtm;         /* sqrt of the masses used for mass-
83                                  * weighting of analysis                   */
84 } t_edix;
85
86
87 typedef struct edipar
88 {
89     int         nini;           /* total Nr of atoms                    */
90     gmx_bool    fitmas;         /* true if trans fit with cm            */
91     gmx_bool    pcamas;         /* true if mass-weighted PCA            */
92     int         presteps;       /* number of steps to run without any
93                                  *    perturbations ... just monitoring */
94     int         outfrq;         /* freq (in steps) of writing to edo    */
95     int         maxedsteps;     /* max nr of steps per cycle            */
96     struct edix sref;           /* reference positions, to these fitting
97                                  * will be done                         */
98     struct edix sav;            /* average positions                    */
99     struct edix star;           /* target positions                     */
100     struct edix sori;           /* origin positions                     */
101     real        slope;          /* minimal slope in acceptance radexp   */
102     int         ned;            /* Nr of atoms in essdyn buffer         */
103     t_edflood   flood;          /* parameters especially for flooding   */
104 } t_edipar;
105
106
107
108 static void make_t_edx(struct edix *edx, int natoms, rvec *pos, int index[])
109 {
110     edx->nr   = natoms;
111     edx->anrs = index;
112     edx->x    = pos;
113 }
114
115 static void write_t_edx(FILE *fp, struct edix edx, const char *comment)
116 {
117     /*here we copy only the pointers into the t_edx struct
118        no data is copied and edx.box is ignored  */
119     int i;
120     fprintf(fp, "#%s \n %d \n", comment, edx.nr);
121     for (i = 0; i < edx.nr; i++)
122     {
123         fprintf(fp, "%d  %f  %f  %f\n", (edx.anrs)[i]+1, (edx.x)[i][XX], (edx.x)[i][YY], (edx.x)[i][ZZ]);
124     }
125 }
126
127 static int sscan_list(int *list[], const char *str, const char *listname)
128 {
129     /*this routine scans a string of the form 1,3-6,9 and returns the
130        selected numbers (in this case 1 3 4 5 6 9) in NULL-terminated array of integers.
131        memory for this list will be allocated  in this routine -- sscan_list expects *list to
132        be a NULL-Pointer
133
134        listname is a string used in the errormessage*/
135
136
137     int   i, istep;
138     char  c;
139     char *pos, *startpos, *step;
140     int   n = std::strlen(str);
141
142     /*enums to define the different lexical stati */
143     enum {
144         sBefore, sNumber, sMinus, sRange, sZero, sSmaller, sError, sSteppedRange
145     };
146
147     int   status     = sBefore; /*status of the deterministic automat to scan str   */
148     int   number     = 0;
149     int   end_number = 0;
150
151     char *start = nullptr; /*holds the string of the number behind a ','*/
152     char *end   = nullptr; /*holds the string of the number behind a '-' */
153
154     int   nvecs = 0;       /* counts the number of vectors in the list*/
155
156     step = nullptr;
157     snew(pos, n+4);
158     startpos = pos;
159     std::strcpy(pos, str);
160     pos[n]   = ',';
161     pos[n+1] = '1';
162     pos[n+2] = '\0';
163
164     *list = nullptr;
165
166     while ((c = *pos) != 0)
167     {
168         switch (status)
169         {
170             /* expect a number */
171             case sBefore: if (std::isdigit(c))
172                 {
173                     start  = pos;
174                     status = sNumber;
175                     break;
176                 }
177                 else
178                 {
179                     status = sError;
180                 } break;
181
182             /* have read a number, expect ',' or '-' */
183             case sNumber: if (c == ',')
184                 {
185                     /*store number*/
186                     srenew(*list, nvecs+1);
187                     (*list)[nvecs++] = number = std::strtol(start, nullptr, 10);
188                     status           = sBefore;
189                     if (number == 0)
190                     {
191                         status = sZero;
192                     }
193                     break;
194                 }
195                 else if (c == '-')
196                 {
197                     status = sMinus; break;
198                 }
199                 else if (std::isdigit(c))
200                 {
201                     break;
202                 }
203                 else
204                 {
205                     status = sError;
206                 } break;
207
208             /* have read a '-' -> expect a number */
209             case sMinus:
210                 if (std::isdigit(c))
211                 {
212                     end    = pos;
213                     status = sRange; break;
214                 }
215                 else
216                 {
217                     status = sError;
218                 } break;
219
220             case sSteppedRange:
221                 if (std::isdigit(c))
222                 {
223                     if (step)
224                     {
225                         status = sError; break;
226                     }
227                     else
228                     {
229                         step = pos;
230                     }
231                     status = sRange;
232                     break;
233                 }
234                 else
235                 {
236                     status = sError;
237                 } break;
238
239             /* have read the number after a minus, expect ',' or ':' */
240             case sRange:
241                 if (c == ',')
242                 {
243                     /*store numbers*/
244                     end_number = std::strtol(end, nullptr, 10);
245                     number     = std::strtol(start, nullptr, 10);
246                     status     = sBefore;
247                     if (number == 0)
248                     {
249                         status = sZero; break;
250                     }
251                     if (end_number <= number)
252                     {
253                         status = sSmaller; break;
254                     }
255                     srenew(*list, nvecs+end_number-number+1);
256                     if (step)
257                     {
258                         istep = strtol(step, nullptr, 10);
259                         step  = nullptr;
260                     }
261                     else
262                     {
263                         istep = 1;
264                     }
265                     for (i = number; i <= end_number; i += istep)
266                     {
267                         (*list)[nvecs++] = i;
268                     }
269                     break;
270                 }
271                 else if (c == ':')
272                 {
273                     status = sSteppedRange;
274                     break;
275                 }
276                 else if (std::isdigit(c))
277                 {
278                     break;
279                 }
280                 else
281                 {
282                     status = sError;
283                 } break;
284
285             /* format error occured */
286             case sError:
287                 gmx_fatal(FARGS, "Error in the list of eigenvectors for %s at pos %d with char %c", listname, pos-startpos, *(pos-1));
288                 break;
289             /* logical error occured */
290             case sZero:
291                 gmx_fatal(FARGS, "Error in the list of eigenvectors for %s at pos %d: eigenvector 0 is not valid", listname, pos-startpos);
292                 break;
293             case sSmaller:
294                 gmx_fatal(FARGS, "Error in the list of eigenvectors for %s at pos %d: second index %d is not bigger than %d", listname, pos-startpos, end_number, number);
295                 break;
296         }
297         ++pos; /* read next character */
298     }          /*scanner has finished */
299
300     /* append zero to list of eigenvectors */
301     srenew(*list, nvecs+1);
302     (*list)[nvecs] = 0;
303     sfree(startpos);
304     return nvecs;
305 } /*sscan_list*/
306
307 static void write_eigvec(FILE* fp, int natoms, int eig_list[], rvec** eigvecs, int nvec, const char *grouptitle, real steps[])
308 {
309 /* eig_list is a zero-terminated list of indices into the eigvecs array.
310    eigvecs are coordinates of eigenvectors
311    grouptitle to write in the comment line
312    steps  -- array with stepsizes for evLINFIX, evLINACC and evRADACC
313  */
314
315     int  n = 0, i; rvec x;
316     while (eig_list[n++])
317     {
318         ;                 /*count selected eigenvecs*/
319
320     }
321     fprintf(fp, "# NUMBER OF EIGENVECTORS + %s\n %d\n", grouptitle, n-1);
322
323     /* write list of eigenvector indicess */
324     for (n = 0; eig_list[n]; n++)
325     {
326         if (steps)
327         {
328             fprintf(fp, "%8d   %g\n", eig_list[n], steps[n]);
329         }
330         else
331         {
332             fprintf(fp, "%8d   %g\n", eig_list[n], 1.0);
333         }
334     }
335     n = 0;
336
337     /* dump coordinates of the selected eigenvectors */
338     while (eig_list[n])
339     {
340         for (i = 0; i < natoms; i++)
341         {
342             if (eig_list[n] > nvec)
343             {
344                 gmx_fatal(FARGS, "Selected eigenvector %d is higher than maximum number %d of available eigenvectors", eig_list[n], nvec);
345             }
346             copy_rvec(eigvecs[eig_list[n]-1][i], x);
347             fprintf(fp, "%8.5f %8.5f %8.5f\n", x[XX], x[YY], x[ZZ]);
348         }
349         n++;
350     }
351 }
352
353
354 /*enum referring to the different lists of eigenvectors*/
355 enum {
356     evLINFIX, evLINACC, evFLOOD, evRADFIX, evRADACC, evRADCON, evMON,  evNr
357 };
358 #define oldMAGIC 666
359 #define MAGIC 670
360
361
362 static void write_the_whole_thing(FILE* fp, t_edipar *edpars, rvec** eigvecs,
363                                   int nvec, int *eig_listen[], real* evStepList[])
364 {
365 /* write edi-file */
366
367     /*Header*/
368     fprintf(fp, "#MAGIC\n %d \n#NINI\n %d\n#FITMAS\n %d\n#ANALYSIS_MAS\n %d\n",
369             MAGIC, edpars->nini, edpars->fitmas, edpars->pcamas);
370     fprintf(fp, "#OUTFRQ\n %d\n#MAXLEN\n %d\n#SLOPECRIT\n %f\n",
371             edpars->outfrq, edpars->maxedsteps, edpars->slope);
372     fprintf(fp, "#PRESTEPS\n %d\n#DELTA_F0\n %f\n#INIT_DELTA_F\n %f\n#TAU\n %f\n#EFL_NULL\n %f\n#ALPHA2\n %f\n#KT\n %f\n#HARMONIC\n %d\n#CONST_FORCE_FLOODING\n %d\n",
373             edpars->presteps, edpars->flood.deltaF0, edpars->flood.deltaF, edpars->flood.tau, edpars->flood.constEfl,
374             edpars->flood.alpha2, edpars->flood.kT, edpars->flood.bHarmonic, edpars->flood.bConstForce);
375
376     /* Average and reference positions */
377     write_t_edx(fp, edpars->sref, "NREF, XREF");
378     write_t_edx(fp, edpars->sav, "NAV, XAV");
379
380     /*Eigenvectors */
381
382     write_eigvec(fp, edpars->ned, eig_listen[evMON], eigvecs, nvec, "COMPONENTS GROUP 1", nullptr);
383     write_eigvec(fp, edpars->ned, eig_listen[evLINFIX], eigvecs, nvec, "COMPONENTS GROUP 2", evStepList[evLINFIX]);
384     write_eigvec(fp, edpars->ned, eig_listen[evLINACC], eigvecs, nvec, "COMPONENTS GROUP 3", evStepList[evLINACC]);
385     write_eigvec(fp, edpars->ned, eig_listen[evRADFIX], eigvecs, nvec, "COMPONENTS GROUP 4", evStepList[evRADFIX]);
386     write_eigvec(fp, edpars->ned, eig_listen[evRADACC], eigvecs, nvec, "COMPONENTS GROUP 5", nullptr);
387     write_eigvec(fp, edpars->ned, eig_listen[evRADCON], eigvecs, nvec, "COMPONENTS GROUP 6", nullptr);
388     write_eigvec(fp, edpars->ned, eig_listen[evFLOOD], eigvecs, nvec, "COMPONENTS GROUP 7", evStepList[evFLOOD]);
389
390
391     /*Target and Origin positions */
392     write_t_edx(fp, edpars->star, "NTARGET, XTARGET");
393     write_t_edx(fp, edpars->sori, "NORIGIN, XORIGIN");
394 }
395
396 static int read_conffile(const char *confin, rvec **x)
397 {
398     t_topology  top;
399     matrix      box;
400     printf("read coordnumber from file %s\n", confin);
401     read_tps_conf(confin, &top, nullptr, x, nullptr, box, FALSE);
402     printf("number of coordinates in file %d\n", top.atoms.nr);
403     return top.atoms.nr;
404 }
405
406
407 static void read_eigenvalues(int vecs[], const char *eigfile, real values[],
408                              gmx_bool bHesse, real kT, int natoms_average_struct)
409 {
410     int      neig, nrow, i;
411     double **eigval;
412
413     neig = read_xvg(eigfile, &eigval, &nrow);
414
415     fprintf(stderr, "Read %d eigenvalues\n", neig);
416     for (i = bHesse ? 6 : 0; i < neig; i++)
417     {
418         if (eigval[1][i] < -0.001 && bHesse)
419         {
420             fprintf(stderr,
421                     "WARNING: The Hessian Matrix has negative eigenvalue %f, we set it to zero (no flooding in this direction)\n\n", eigval[1][i]);
422         }
423
424         if (eigval[1][i] < 0)
425         {
426             eigval[1][i] = 0;
427         }
428     }
429     if (bHesse)
430     {
431         for (i = 0; vecs[i]; i++)
432         {
433             if (vecs[i] < 7)
434             {
435                 gmx_fatal(FARGS, "ERROR: You have chosen one of the first 6 eigenvectors of the HESSE Matrix. That does not make sense, since they correspond to the 6 rotational and translational degrees of freedom.\n\n");
436             }
437             values[i] = eigval[1][vecs[i]-1]/kT;
438         }
439     }
440     else
441     {
442         for (i = 0; vecs[i]; i++)
443         {
444             /* Make sure this eigenvalue does not correspond to one of the last 6 eigenvectors of the
445              * covariance matrix. These correspond to the rotational and translational degrees of
446              * freedom and will be zero within numerical accuracy.
447              *
448              * Note that the total number of eigenvectors produced by gmx covar depends on:
449              * 1) the total number of degrees of freedom of the system (3N, with N the number of atoms)
450              * 2) the number S of independent configurations fed into gmx covar.
451              * For long trajectories with lots of frames, usually S >= 3N + 1, so that one indeed gets
452              * 3N eigenvalues (of which the last 6 will have zero eigenvalues).
453              * For S < 3N + 1, however, the covariance matrix becomes rank deficient, and the number
454              * of possible eigenvalues is just S - 1. Since in make_edi we only know N but not S, we can
455              * only warn the user if he picked one of the last 6 of 3N.
456              */
457             if (vecs[i] > ( 3 * natoms_average_struct - 6 ))
458             {
459                 gmx_fatal(FARGS, "ERROR: You have chosen one of the last 6 eigenvectors of the COVARIANCE Matrix. That does not make sense, since they correspond to the 6 rotational and translational degrees of freedom.\n\n");
460             }
461             values[i] = 1/eigval[1][vecs[i]-1];
462         }
463     }
464     /* free memory */
465     for (i = 0; i < nrow; i++)
466     {
467         sfree(eigval[i]);
468     }
469     sfree(eigval);
470 }
471
472
473 static real *scan_vecparams(const char *str, const char * par, int nvecs)
474 {
475     char    f0[256], f1[256];         /*format strings adapted every pass of the loop*/
476     double  d;
477     int     i;
478     real   *vec_params;
479
480     snew(vec_params, nvecs);
481     if (str)
482     {
483         f0[0] = '\0';
484         for (i = 0; (i < nvecs); i++)
485         {
486             std::strcpy(f1, f0);    /*f0 is the format string for the "to-be-ignored" numbers*/
487             std::strcat(f1, "%lf"); /*and f1 to read the actual number in this pass of the loop*/
488             if (sscanf(str, f1, &d) != 1)
489             {
490                 gmx_fatal(FARGS, "Not enough elements for %s parameter (I need %d)", par, nvecs);
491             }
492             vec_params[i] = d;
493             std::strcat(f0, "%*s");
494         }
495     }
496     return vec_params;
497 }
498
499
500 static void init_edx(struct edix *edx)
501 {
502     edx->nr = 0;
503     snew(edx->x, 1);
504     snew(edx->anrs, 1);
505 }
506
507 static void filter2edx(struct edix *edx, int nindex, int index[], int ngro,
508                        int igro[], const rvec *x, const char* structure)
509 {
510 /* filter2edx copies coordinates from x to edx which are given in index
511  */
512
513     int pos, i;
514     int ix = edx->nr;
515     edx->nr += nindex;
516     srenew(edx->x, edx->nr);
517     srenew(edx->anrs, edx->nr);
518     for (i = 0; i < nindex; i++, ix++)
519     {
520         for (pos = 0; pos < ngro-1 && igro[pos] != index[i]; ++pos)
521         {
522         }
523         ;                                                            /*search element in igro*/
524         if (igro[pos] != index[i])
525         {
526             gmx_fatal(FARGS, "Couldn't find atom with index %d in structure %s", index[i], structure);
527         }
528         edx->anrs[ix] = index[i];
529         copy_rvec(x[pos], edx->x[ix]);
530     }
531 }
532
533 static void get_structure(const t_atoms *atoms, const char *IndexFile,
534                           const char *StructureFile, struct edix *edx, int nfit,
535                           int ifit[], int nav, int index[])
536 {
537     int     *igro; /*index corresponding to target or origin structure*/
538     int      ngro;
539     int      ntar;
540     rvec    *xtar;
541     char   * grpname;
542
543
544     ntar = read_conffile(StructureFile, &xtar);
545     printf("Select an index group of %d elements that corresponds to the atoms in the structure file %s\n",
546            ntar, StructureFile);
547     get_index(atoms, IndexFile, 1, &ngro, &igro, &grpname);
548     if (ngro != ntar)
549     {
550         gmx_fatal(FARGS, "You selected an index group with %d elements instead of %d", ngro, ntar);
551     }
552     init_edx(edx);
553     filter2edx(edx, nfit, ifit, ngro, igro, xtar, StructureFile);
554
555     /* If average and reference/fitting structure differ, append the average structure as well */
556     if (ifit != index) /*if fit structure is different append these coordinates, too -- don't mind duplicates*/
557     {
558         filter2edx(edx, nav, index, ngro, igro, xtar, StructureFile);
559     }
560 }
561
562 int gmx_make_edi(int argc, char *argv[])
563 {
564
565     static const char *desc[] = {
566         "[THISMODULE] generates an essential dynamics (ED) sampling input file to be used with [TT]mdrun[tt]",
567         "based on eigenvectors of a covariance matrix ([gmx-covar]) or from a",
568         "normal modes analysis ([gmx-nmeig]).",
569         "ED sampling can be used to manipulate the position along collective coordinates",
570         "(eigenvectors) of (biological) macromolecules during a simulation. Particularly,",
571         "it may be used to enhance the sampling efficiency of MD simulations by stimulating",
572         "the system to explore new regions along these collective coordinates. A number",
573         "of different algorithms are implemented to drive the system along the eigenvectors",
574         "([TT]-linfix[tt], [TT]-linacc[tt], [TT]-radfix[tt], [TT]-radacc[tt], [TT]-radcon[tt]),",
575         "to keep the position along a certain (set of) coordinate(s) fixed ([TT]-linfix[tt]),",
576         "or to only monitor the projections of the positions onto",
577         "these coordinates ([TT]-mon[tt]).[PAR]",
578         "References:[PAR]",
579         "A. Amadei, A.B.M. Linssen, B.L. de Groot, D.M.F. van Aalten and ",
580         "H.J.C. Berendsen; An efficient method for sampling the essential subspace ",
581         "of proteins., J. Biomol. Struct. Dyn. 13:615-626 (1996)[PAR]",
582         "B.L. de Groot, A. Amadei, D.M.F. van Aalten and H.J.C. Berendsen; ",
583         "Towards an exhaustive sampling of the configurational spaces of the ",
584         "two forms of the peptide hormone guanylin,",
585         "J. Biomol. Struct. Dyn. 13 : 741-751 (1996)[PAR]",
586         "B.L. de Groot, A.Amadei, R.M. Scheek, N.A.J. van Nuland and H.J.C. Berendsen; ",
587         "An extended sampling of the configurational space of HPr from E. coli",
588         "Proteins: Struct. Funct. Gen. 26: 314-322 (1996)",
589         "[PAR]You will be prompted for one or more index groups that correspond to the eigenvectors,",
590         "reference structure, target positions, etc.[PAR]",
591
592         "[TT]-mon[tt]: monitor projections of the coordinates onto selected eigenvectors.[PAR]",
593         "[TT]-linfix[tt]: perform fixed-step linear expansion along selected eigenvectors.[PAR]",
594         "[TT]-linacc[tt]: perform acceptance linear expansion along selected eigenvectors.",
595         "(steps in the desired directions will be accepted, others will be rejected).[PAR]",
596         "[TT]-radfix[tt]: perform fixed-step radius expansion along selected eigenvectors.[PAR]",
597         "[TT]-radacc[tt]: perform acceptance radius expansion along selected eigenvectors.",
598         "(steps in the desired direction will be accepted, others will be rejected).",
599         "[BB]Note:[bb] by default the starting MD structure will be taken as origin of the first",
600         "expansion cycle for radius expansion. If [TT]-ori[tt] is specified, you will be able",
601         "to read in a structure file that defines an external origin.[PAR]",
602         "[TT]-radcon[tt]: perform acceptance radius contraction along selected eigenvectors",
603         "towards a target structure specified with [TT]-tar[tt].[PAR]",
604         "NOTE: each eigenvector can be selected only once. [PAR]",
605         "[TT]-outfrq[tt]: frequency (in steps) of writing out projections etc. to [REF].xvg[ref] file[PAR]",
606         "[TT]-slope[tt]: minimal slope in acceptance radius expansion. A new expansion",
607         "cycle will be started if the spontaneous increase of the radius (in nm/step)",
608         "is less than the value specified.[PAR]",
609         "[TT]-maxedsteps[tt]: maximum number of steps per cycle in radius expansion",
610         "before a new cycle is started.[PAR]",
611         "Note on the parallel implementation: since ED sampling is a 'global' thing",
612         "(collective coordinates etc.), at least on the 'protein' side, ED sampling",
613         "is not very parallel-friendly from an implementation point of view. Because",
614         "parallel ED requires some extra communication, expect the performance to be",
615         "lower as in a free MD simulation, especially on a large number of ranks and/or",
616         "when the ED group contains a lot of atoms. [PAR]",
617         "Please also note that if your ED group contains more than a single protein,",
618         "then the [REF].tpr[ref] file must contain the correct PBC representation of the ED group.",
619         "Take a look on the initial RMSD from the reference structure, which is printed",
620         "out at the start of the simulation; if this is much higher than expected, one",
621         "of the ED molecules might be shifted by a box vector. [PAR]",
622         "All ED-related output of [TT]mdrun[tt] (specify with [TT]-eo[tt]) is written to a [REF].xvg[ref] file",
623         "as a function of time in intervals of OUTFRQ steps.[PAR]",
624         "[BB]Note[bb] that you can impose multiple ED constraints and flooding potentials in",
625         "a single simulation (on different molecules) if several [REF].edi[ref] files were concatenated",
626         "first. The constraints are applied in the order they appear in the [REF].edi[ref] file. ",
627         "Depending on what was specified in the [REF].edi[ref] input file, the output file contains for each ED dataset",
628         "",
629         " * the RMSD of the fitted molecule to the reference structure (for atoms involved in fitting prior to calculating the ED constraints)",
630         " * projections of the positions onto selected eigenvectors",
631         "",
632         "FLOODING:[PAR]",
633         "with [TT]-flood[tt], you can specify which eigenvectors are used to compute a flooding potential,",
634         "which will lead to extra forces expelling the structure out of the region described",
635         "by the covariance matrix. If you switch -restrain the potential is inverted and the structure",
636         "is kept in that region.",
637         "[PAR]",
638         "The origin is normally the average structure stored in the [TT]eigvec.trr[tt] file.",
639         "It can be changed with [TT]-ori[tt] to an arbitrary position in configuration space.",
640         "With [TT]-tau[tt], [TT]-deltaF0[tt], and [TT]-Eflnull[tt] you control the flooding behaviour.",
641         "Efl is the flooding strength, it is updated according to the rule of adaptive flooding.",
642         "Tau is the time constant of adaptive flooding, high [GRK]tau[grk] means slow adaption (i.e. growth). ",
643         "DeltaF0 is the flooding strength you want to reach after tau ps of simulation.",
644         "To use constant Efl set [TT]-tau[tt] to zero.",
645         "[PAR]",
646         "[TT]-alpha[tt] is a fudge parameter to control the width of the flooding potential. A value of 2 has been found",
647         "to give good results for most standard cases in flooding of proteins.",
648         "[GRK]alpha[grk] basically accounts for incomplete sampling, if you sampled further the width of the ensemble would",
649         "increase, this is mimicked by [GRK]alpha[grk] > 1.",
650         "For restraining, [GRK]alpha[grk] < 1 can give you smaller width in the restraining potential.",
651         "[PAR]",
652         "RESTART and FLOODING:",
653         "If you want to restart a crashed flooding simulation please find the values deltaF and Efl in",
654         "the output file and manually put them into the [REF].edi[ref] file under DELTA_F0 and EFL_NULL."
655     };
656
657     /* Save all the params in this struct and then save it in an edi file.
658      * ignoring fields nmass,massnrs,mass,tmass,nfit,fitnrs,edo
659      */
660     static t_edipar edi_params;
661
662     enum  {
663         evStepNr = evRADFIX + 1
664     };
665     static const char* evSelections[evNr]      = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
666     static const char* evOptions[evNr]         = {"-linfix", "-linacc", "-flood", "-radfix", "-radacc", "-radcon", "-mon"};
667     static const char* evParams[evStepNr]      = {nullptr, nullptr};
668     static const char* evStepOptions[evStepNr] = {"-linstep", "-accdir", "-not_used", "-radstep"};
669     static const char* ConstForceStr;
670     static real      * evStepList[evStepNr];
671     static real        radstep  = 0.0;
672     static real        deltaF0  = 150;
673     static real        deltaF   = 0;
674     static real        tau      = .1;
675     static real        constEfl = 0.0;
676     static real        alpha    = 1;
677     static int         eqSteps  = 0;
678     static int       * listen[evNr];
679     static real        T         = 300.0;
680     const real         kB        = 2.5 / 300.0; /* k_boltzmann in MD units */
681     static gmx_bool    bRestrain = FALSE;
682     static gmx_bool    bHesse    = FALSE;
683     static gmx_bool    bHarmonic = FALSE;
684     t_pargs            pa[]      = {
685         { "-mon", FALSE, etSTR, {&evSelections[evMON]},
686           "Indices of eigenvectors for projections of x (e.g. 1,2-5,9) or 1-100:10 means 1 11 21 31 ... 91" },
687         { "-linfix", FALSE, etSTR, {&evSelections[0]},
688           "Indices of eigenvectors for fixed increment linear sampling" },
689         { "-linacc", FALSE, etSTR, {&evSelections[1]},
690           "Indices of eigenvectors for acceptance linear sampling" },
691         { "-radfix", FALSE, etSTR, {&evSelections[3]},
692           "Indices of eigenvectors for fixed increment radius expansion" },
693         { "-radacc", FALSE, etSTR, {&evSelections[4]},
694           "Indices of eigenvectors for acceptance radius expansion" },
695         { "-radcon", FALSE, etSTR, {&evSelections[5]},
696           "Indices of eigenvectors for acceptance radius contraction" },
697         { "-flood",  FALSE, etSTR, {&evSelections[2]},
698           "Indices of eigenvectors for flooding"},
699         { "-outfrq", FALSE, etINT, {&edi_params.outfrq},
700           "Frequency (in steps) of writing output in [REF].xvg[ref] file" },
701         { "-slope", FALSE, etREAL, { &edi_params.slope},
702           "Minimal slope in acceptance radius expansion"},
703         { "-linstep", FALSE, etSTR, {&evParams[0]},
704           "Stepsizes (nm/step) for fixed increment linear sampling (put in quotes! \"1.0 2.3 5.1 -3.1\")"},
705         { "-accdir", FALSE, etSTR, {&evParams[1]},
706           "Directions for acceptance linear sampling - only sign counts! (put in quotes! \"-1 +1 -1.1\")"},
707         { "-radstep", FALSE, etREAL, {&radstep},
708           "Stepsize (nm/step) for fixed increment radius expansion"},
709         { "-maxedsteps", FALSE, etINT, {&edi_params.maxedsteps},
710           "Maximum number of steps per cycle" },
711         { "-eqsteps", FALSE, etINT, {&eqSteps},
712           "Number of steps to run without any perturbations "},
713         { "-deltaF0", FALSE, etREAL, {&deltaF0},
714           "Target destabilization energy for flooding"},
715         { "-deltaF", FALSE, etREAL, {&deltaF},
716           "Start deltaF with this parameter - default 0, nonzero values only needed for restart"},
717         { "-tau", FALSE, etREAL, {&tau},
718           "Coupling constant for adaption of flooding strength according to deltaF0, 0 = infinity i.e. constant flooding strength"},
719         { "-Eflnull", FALSE, etREAL, {&constEfl},
720           "The starting value of the flooding strength. The flooding strength is updated "
721           "according to the adaptive flooding scheme. For a constant flooding strength use [TT]-tau[tt] 0. "},
722         { "-T", FALSE, etREAL, {&T},
723           "T is temperature, the value is needed if you want to do flooding "},
724         { "-alpha", FALSE, etREAL, {&alpha},
725           "Scale width of gaussian flooding potential with alpha^2 "},
726         { "-restrain", FALSE, etBOOL, {&bRestrain},
727           "Use the flooding potential with inverted sign -> effects as quasiharmonic restraining potential"},
728         { "-hessian", FALSE, etBOOL, {&bHesse},
729           "The eigenvectors and eigenvalues are from a Hessian matrix"},
730         { "-harmonic", FALSE, etBOOL, {&bHarmonic},
731           "The eigenvalues are interpreted as spring constant"},
732         { "-constF", FALSE, etSTR, {&ConstForceStr},
733           "Constant force flooding: manually set the forces for the eigenvectors selected with -flood "
734           "(put in quotes! \"1.0 2.3 5.1 -3.1\"). No other flooding parameters are needed when specifying the forces directly."}
735     };
736 #define NPA asize(pa)
737
738     rvec             *xref1;
739     int               nvec1, *eignr1 = nullptr;
740     rvec             *xav1, **eigvec1 = nullptr;
741     t_atoms          *atoms = nullptr;
742     int               nav; /* Number of atoms in the average structure */
743     char             *grpname;
744     const char       *indexfile;
745     int               i;
746     int              *index, *ifit;
747     int               nfit;              /* Number of atoms in the reference/fit structure */
748     int               ev_class;          /* parameter _class i.e. evMON, evRADFIX etc. */
749     int               nvecs;
750     real             *eigval1 = nullptr; /* in V3.3 this is parameter of read_eigenvectors */
751
752     const char       *EdiFile;
753     const char       *TargetFile;
754     const char       *OriginFile;
755     const char       *EigvecFile;
756
757     gmx_output_env_t *oenv;
758
759     /*to read topology file*/
760     t_topology  top;
761     int         ePBC;
762     matrix      topbox;
763     rvec       *xtop;
764     gmx_bool    bFit1;
765
766     t_filenm    fnm[] = {
767         { efTRN, "-f",    "eigenvec",    ffREAD  },
768         { efXVG, "-eig",  "eigenval",    ffOPTRD },
769         { efTPS, nullptr,    nullptr,          ffREAD },
770         { efNDX, nullptr,    nullptr,  ffOPTRD },
771         { efSTX, "-tar", "target", ffOPTRD},
772         { efSTX, "-ori", "origin", ffOPTRD},
773         { efEDI, "-o", "sam", ffWRITE }
774     };
775 #define NFILE asize(fnm)
776     edi_params.outfrq = 100; edi_params.slope = 0.0; edi_params.maxedsteps = 0;
777     if (!parse_common_args(&argc, argv, 0,
778                            NFILE, fnm, NPA, pa, asize(desc), desc, 0, nullptr, &oenv))
779     {
780         return 0;
781     }
782
783     indexfile       = ftp2fn_null(efNDX, NFILE, fnm);
784     EdiFile         = ftp2fn(efEDI, NFILE, fnm);
785     TargetFile      = opt2fn_null("-tar", NFILE, fnm);
786     OriginFile      = opt2fn_null("-ori", NFILE, fnm);
787
788
789     for (ev_class = 0; ev_class < evNr; ++ev_class)
790     {
791         if (opt2parg_bSet(evOptions[ev_class], NPA, pa))
792         {
793             /*get list of eigenvectors*/
794             nvecs = sscan_list(&(listen[ev_class]), opt2parg_str(evOptions[ev_class], NPA, pa), evOptions[ev_class]);
795             if (ev_class < evStepNr-2)
796             {
797                 /*if apropriate get list of stepsizes for these eigenvectors*/
798                 if (opt2parg_bSet(evStepOptions[ev_class], NPA, pa))
799                 {
800                     evStepList[ev_class] =
801                         scan_vecparams(opt2parg_str(evStepOptions[ev_class], NPA, pa), evStepOptions[ev_class], nvecs);
802                 }
803                 else   /*if list is not given fill with zeros */
804                 {
805                     snew(evStepList[ev_class], nvecs);
806                     for (i = 0; i < nvecs; i++)
807                     {
808                         evStepList[ev_class][i] = 0.0;
809                     }
810                 }
811             }
812             else if (ev_class == evRADFIX)
813             {
814                 snew(evStepList[ev_class], nvecs);
815                 for (i = 0; i < nvecs; i++)
816                 {
817                     evStepList[ev_class][i] = radstep;
818                 }
819             }
820             else if (ev_class == evFLOOD)
821             {
822                 snew(evStepList[ev_class], nvecs);
823
824                 /* Are we doing constant force flooding? In that case, we read in
825                  * the fproj values from the command line */
826                 if (opt2parg_bSet("-constF", NPA, pa))
827                 {
828                     evStepList[ev_class] = scan_vecparams(opt2parg_str("-constF", NPA, pa), "-constF", nvecs);
829                 }
830             }
831             else
832             {
833             };   /*to avoid ambiguity   */
834         }
835         else     /* if there are no eigenvectors for this option set list to zero */
836         {
837             listen[ev_class] = nullptr;
838             snew(listen[ev_class], 1);
839             listen[ev_class][0] = 0;
840         }
841     }
842
843     /* print the interpreted list of eigenvectors - to give some feedback*/
844     for (ev_class = 0; ev_class < evNr; ++ev_class)
845     {
846         printf("Eigenvector list %7s consists of the indices: ", evOptions[ev_class]);
847         i = 0;
848         while (listen[ev_class][i])
849         {
850             printf("%d ", listen[ev_class][i++]);
851         }
852         printf("\n");
853     }
854
855     EigvecFile = opt2fn("-f", NFILE, fnm);
856
857     /*read eigenvectors from eigvec.trr*/
858     read_eigenvectors(EigvecFile, &nav, &bFit1,
859                       &xref1, &edi_params.fitmas, &xav1, &edi_params.pcamas, &nvec1, &eignr1, &eigvec1, &eigval1);
860
861     read_tps_conf(ftp2fn(efTPS, NFILE, fnm),
862                   &top, &ePBC, &xtop, nullptr, topbox, 0);
863     atoms = &top.atoms;
864
865
866     printf("\nSelect an index group of %d elements that corresponds to the eigenvectors\n", nav);
867     get_index(atoms, indexfile, 1, &i, &index, &grpname); /*if indexfile != NULL parameter 'atoms' is ignored */
868     if (i != nav)
869     {
870         gmx_fatal(FARGS, "you selected a group with %d elements instead of %d",
871                   i, nav);
872     }
873     printf("\n");
874
875
876     if (xref1 == nullptr)
877     {
878         if (bFit1)
879         {
880             /* if g_covar used different coordinate groups to fit and to do the PCA */
881             printf("\nNote: the structure in %s should be the same\n"
882                    "      as the one used for the fit in g_covar\n", ftp2fn(efTPS, NFILE, fnm));
883             printf("\nSelect the index group that was used for the least squares fit in g_covar\n");
884         }
885         else
886         {
887             printf("\nNote: Apparently no fitting was done in g_covar.\n"
888                    "      However, you need to select a reference group for fitting in mdrun\n");
889         }
890         get_index(atoms, indexfile, 1, &nfit, &ifit, &grpname);
891         snew(xref1, nfit);
892         for (i = 0; i < nfit; i++)
893         {
894             copy_rvec(xtop[ifit[i]], xref1[i]);
895         }
896     }
897     else
898     {
899         nfit = nav;
900         ifit = index;
901     }
902
903     if (opt2parg_bSet("-constF", NPA, pa))
904     {
905         /* Constant force flooding is special: Most of the normal flooding
906          * options are not needed. */
907         edi_params.flood.bConstForce = TRUE;
908     }
909     else
910     {
911         /* For normal flooding read eigenvalues and store them in evSteplist[evFLOOD] */
912
913         if (listen[evFLOOD][0] != 0)
914         {
915             read_eigenvalues(listen[evFLOOD], opt2fn("-eig", NFILE, fnm), evStepList[evFLOOD], bHesse, kB*T, nav);
916         }
917
918         edi_params.flood.tau       = tau;
919         edi_params.flood.deltaF0   = deltaF0;
920         edi_params.flood.deltaF    = deltaF;
921         edi_params.presteps        = eqSteps;
922         edi_params.flood.kT        = kB*T;
923         edi_params.flood.bHarmonic = bHarmonic;
924         if (bRestrain)
925         {
926             /* Trick: invert sign of Efl and alpha2 then this will give the same sign in the exponential and inverted sign outside */
927             edi_params.flood.constEfl = -constEfl;
928             edi_params.flood.alpha2   = -gmx::square(alpha);
929         }
930         else
931         {
932             edi_params.flood.constEfl = constEfl;
933             edi_params.flood.alpha2   = gmx::square(alpha);
934         }
935     }
936
937     edi_params.ned = nav;
938
939     /*number of system atoms  */
940     edi_params.nini = atoms->nr;
941
942
943     /*store reference and average structure in edi_params*/
944     make_t_edx(&edi_params.sref, nfit, xref1, ifit );
945     make_t_edx(&edi_params.sav, nav, xav1, index);
946
947
948     /* Store target positions in edi_params */
949     if (opt2bSet("-tar", NFILE, fnm))
950     {
951         if (0 != listen[evFLOOD][0])
952         {
953             fprintf(stderr, "\nNote: Providing a TARGET structure has no effect when using flooding.\n"
954                     "      You may want to use -ori to define the flooding potential center.\n\n");
955         }
956         get_structure(atoms, indexfile, TargetFile, &edi_params.star, nfit, ifit, nav, index);
957     }
958     else
959     {
960         make_t_edx(&edi_params.star, 0, nullptr, index);
961     }
962
963     /* Store origin positions */
964     if (opt2bSet("-ori", NFILE, fnm))
965     {
966         get_structure(atoms, indexfile, OriginFile, &edi_params.sori, nfit, ifit, nav, index);
967     }
968     else
969     {
970         make_t_edx(&edi_params.sori, 0, nullptr, index);
971     }
972
973     /* Write edi-file */
974     write_the_whole_thing(gmx_ffopen(EdiFile, "w"), &edi_params, eigvec1, nvec1, listen, evStepList);
975
976     return 0;
977 }