ad5e7d9295b26d7eca62b9bbf8f5d98047c669e0
[alexxy/gromacs.git] / src / gromacs / essentialdynamics / edsam.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 #include "gmxpre.h"
39
40 #include "edsam.h"
41
42 #include <cmath>
43 #include <cstdio>
44 #include <cstring>
45 #include <ctime>
46
47 #include <memory>
48
49 #include "gromacs/commandline/filenm.h"
50 #include "gromacs/domdec/domdec_struct.h"
51 #include "gromacs/fileio/gmxfio.h"
52 #include "gromacs/fileio/xvgr.h"
53 #include "gromacs/gmxlib/network.h"
54 #include "gromacs/gmxlib/nrnb.h"
55 #include "gromacs/linearalgebra/nrjac.h"
56 #include "gromacs/math/functions.h"
57 #include "gromacs/math/units.h"
58 #include "gromacs/math/utilities.h"
59 #include "gromacs/math/vec.h"
60 #include "gromacs/math/vectypes.h"
61 #include "gromacs/mdlib/broadcaststructs.h"
62 #include "gromacs/mdlib/constr.h"
63 #include "gromacs/mdlib/groupcoord.h"
64 #include "gromacs/mdlib/stat.h"
65 #include "gromacs/mdlib/update.h"
66 #include "gromacs/mdrunutility/handlerestart.h"
67 #include "gromacs/mdtypes/commrec.h"
68 #include "gromacs/mdtypes/edsamhistory.h"
69 #include "gromacs/mdtypes/inputrec.h"
70 #include "gromacs/mdtypes/md_enums.h"
71 #include "gromacs/mdtypes/observableshistory.h"
72 #include "gromacs/mdtypes/state.h"
73 #include "gromacs/pbcutil/pbc.h"
74 #include "gromacs/topology/mtop_lookup.h"
75 #include "gromacs/topology/topology.h"
76 #include "gromacs/utility/cstringutil.h"
77 #include "gromacs/utility/fatalerror.h"
78 #include "gromacs/utility/gmxassert.h"
79 #include "gromacs/utility/logger.h"
80 #include "gromacs/utility/smalloc.h"
81 #include "gromacs/utility/strconvert.h"
82
83 namespace
84 {
85 /*! \brief Identifies the type of ED: none, normal ED, flooding. */
86 enum class EssentialDynamicsType
87 {
88     //! No essential dynamics
89     None,
90     //! Essential dynamics sampling
91     EDSampling,
92     //! Essential dynamics flooding
93     Flooding
94 };
95
96 /*! \brief Identify on which structure to operate. */
97 enum class EssentialDynamicsStructure
98 {
99     //! Reference structure
100     Reference,
101     //! Average Structure
102     Average,
103     //! Origin Structure
104     Origin,
105     //! Target Structure
106     Target
107 };
108
109 /*! \brief Essential dynamics vector.
110  * TODO: split into working data and input data
111  * NOTE: called eigvec, because traditionally eigenvectors from PCA analysis
112  * were used as essential dynamics vectors, however, vectors used for ED need
113  * not have any special properties
114  */
115 struct t_eigvec
116 {
117     //! nr of eigenvectors
118     int neig = 0;
119     //! index nrs of eigenvectors
120     int* ieig = nullptr;
121     //! stepsizes (per eigenvector)
122     real* stpsz = nullptr;
123     //! eigenvector components
124     rvec** vec = nullptr;
125     //! instantaneous x projections
126     real* xproj = nullptr;
127     //! instantaneous f projections
128     real* fproj = nullptr;
129     //! instantaneous radius
130     real radius = 0.;
131     //! starting or target projections
132     real* refproj = nullptr;
133 };
134
135 /*! \brief Essential dynamics vectors per method implementation.
136  */
137 struct t_edvecs
138 {
139     //! only monitored, no constraints
140     t_eigvec mon = {};
141     //! fixed linear constraints
142     t_eigvec linfix = {};
143     //! acceptance linear constraints
144     t_eigvec linacc = {};
145     //! fixed radial constraints (exp)
146     t_eigvec radfix = {};
147     //! acceptance radial constraints (exp)
148     t_eigvec radacc = {};
149     //! acceptance rad. contraction constr.
150     t_eigvec radcon = {};
151 };
152
153 /*! \brief Essential dynamics flooding parameters and work data.
154  * TODO: split into working data and input parameters
155  * NOTE: The implementation here follows:
156  * O.E. Lange, L.V. Schafer, and H. Grubmuller,
157  * “Flooding in GROMACS: Accelerated barrier crossings in molecular dynamics,”
158  *  J. Comp. Chem., 27 1693–1702 (2006)
159  */
160 struct t_edflood
161 {
162     /*! \brief Target destabilisation free energy.
163      * Controls the flooding potential strength.
164      * Linked to the expected speed-up of mean first passage time out of flooded minimum */
165     real deltaF0 = 0;
166     //! Do not calculate a flooding potential, instead flood with a constant force
167     bool bConstForce = false;
168     //! Relaxation time scale for slowest flooded degree of freedom
169     real tau = 0;
170     //! Current estimated destabilisation free energy
171     real deltaF = 0;
172     //! Flooding energy, acting as a proportionality factor for the flooding potential
173     real Efl = 0;
174     //! Boltzmann constant times temperature, provided by user
175     real kT = 0;
176     //! The flooding potential
177     real Vfl = 0;
178     //! Integration time step
179     real dt = 0;
180     //! Inital flooding strenth
181     real constEfl = 0;
182     //! Empirical global scaling parameter of the essential dynamics vectors.
183     real alpha2 = 0;
184     //! The forces from flooding in atom coordinate space (in contrast to projections onto essential dynamics vectors)
185     rvec* forces_cartesian = nullptr;
186     //! The vectors along which to flood
187     t_eigvec vecs = {};
188     //! Use flooding for harmonic restraint on the eigenvector
189     bool bHarmonic = false;
190     //! The initial reference projection of the flooding vectors. Only with harmonic restraint.
191     real* initialReferenceProjection = nullptr;
192     //! The current reference projection is the initialReferenceProjection + step * slope. Only with harmonic restraint.
193     real* referenceProjectionSlope = nullptr;
194 };
195 } // namespace
196
197
198 /* This type is for the average, reference, target, and origin structure    */
199 struct gmx_edx
200 {
201     int  nr         = 0;       /* number of atoms this structure contains  */
202     int  nr_loc     = 0;       /* number of atoms on local node            */
203     int* anrs       = nullptr; /* atom index numbers                       */
204     int* anrs_loc   = nullptr; /* local atom index numbers                 */
205     int  nalloc_loc = 0;       /* allocation size of anrs_loc              */
206     int* c_ind      = nullptr; /* at which position of the whole anrs
207                                 * array is a local atom?, i.e.
208                                 * c_ind[0...nr_loc-1] gives the atom index
209                                 * with respect to the collective
210                                 * anrs[0...nr-1] array                     */
211     rvec* x     = nullptr;     /* positions for this structure             */
212     rvec* x_old = nullptr;     /* Last positions which have the correct PBC
213                                   representation of the ED group. In
214                                   combination with keeping track of the
215                                   shift vectors, the ED group can always
216                                   be made whole                            */
217     real* m     = nullptr;     /* masses                                   */
218     real  mtot  = 0.;          /* total mass (only used in sref)           */
219     real* sqrtm = nullptr;     /* sqrt of the masses used for mass-
220                                 * weighting of analysis (only used in sav) */
221 };
222
223
224 typedef struct edpar
225 {
226     int      nini     = 0;     /* total Nr of atoms                    */
227     gmx_bool fitmas   = false; /* true if trans fit with cm            */
228     gmx_bool pcamas   = false; /* true if mass-weighted PCA            */
229     int      presteps = 0;     /* number of steps to run without any
230                                 *    perturbations ... just monitoring */
231     int outfrq     = 0;        /* freq (in steps) of writing to edo    */
232     int maxedsteps = 0;        /* max nr of steps per cycle            */
233
234     /* all gmx_edx datasets are copied to all nodes in the parallel case   */
235     gmx_edx sref = {};         /* reference positions, to these fitting
236                                 * will be done                         */
237     gmx_bool bRefEqAv = false; /* If true, reference & average indices
238                                 * are the same. Used for optimization  */
239     gmx_edx sav  = {};         /* average positions                    */
240     gmx_edx star = {};         /* target positions                     */
241     gmx_edx sori = {};         /* origin positions                     */
242
243     t_edvecs vecs  = {}; /* eigenvectors                         */
244     real     slope = 0;  /* minimal slope in acceptance radexp   */
245
246     t_edflood           flood = {};      /* parameters especially for flooding   */
247     struct t_ed_buffer* buf   = nullptr; /* handle to local buffers              */
248 } t_edpar;
249
250
251 struct gmx_edsam
252 {
253     ~gmx_edsam();
254     //! The type of ED
255     EssentialDynamicsType eEDtype = EssentialDynamicsType::None;
256     //! output file pointer
257     FILE*                edo = nullptr;
258     std::vector<t_edpar> edpar;
259     gmx_bool             bFirst = false;
260 };
261 gmx_edsam::~gmx_edsam()
262 {
263     if (edo)
264     {
265         /* edo is opened sometimes with xvgropen, sometimes with
266          * gmx_fio_fopen, so we use the least common denominator for
267          * closing. */
268         gmx_fio_fclose(edo);
269     }
270 }
271
272 struct t_do_edsam
273 {
274     matrix old_rotmat;
275     real   oldrad;
276     rvec   old_transvec, older_transvec, transvec_compact;
277     rvec*  xcoll;                 /* Positions from all nodes, this is the
278                                      collective set we work on.
279                                      These are the positions of atoms with
280                                      average structure indices */
281     rvec*    xc_ref;              /* same but with reference structure indices */
282     ivec*    shifts_xcoll;        /* Shifts for xcoll  */
283     ivec*    extra_shifts_xcoll;  /* xcoll shift changes since last NS step */
284     ivec*    shifts_xc_ref;       /* Shifts for xc_ref */
285     ivec*    extra_shifts_xc_ref; /* xc_ref shift changes since last NS step */
286     gmx_bool bUpdateShifts;       /* TRUE in NS steps to indicate that the
287                                      ED shifts for this ED group need to
288                                      be updated */
289 };
290
291
292 /* definition of ED buffer structure */
293 struct t_ed_buffer
294 {
295     struct t_fit_to_ref* fit_to_ref;
296     struct t_do_edfit*   do_edfit;
297     struct t_do_edsam*   do_edsam;
298     struct t_do_radcon*  do_radcon;
299 };
300
301 namespace gmx
302 {
303 class EssentialDynamics::Impl
304 {
305 public:
306     // TODO: move all fields from gmx_edsam here and remove gmx_edsam
307     gmx_edsam essentialDynamics_;
308 };
309 EssentialDynamics::EssentialDynamics() : impl_(new Impl) {}
310 EssentialDynamics::~EssentialDynamics() = default;
311
312 gmx_edsam* EssentialDynamics::getLegacyED()
313 {
314     return &impl_->essentialDynamics_;
315 }
316 } // namespace gmx
317
318 /* Function declarations */
319 static void fit_to_reference(rvec* xcoll, rvec transvec, matrix rotmat, t_edpar* edi);
320 static void translate_and_rotate(rvec* x, int nat, rvec transvec, matrix rotmat);
321 static real rmsd_from_structure(rvec* x, struct gmx_edx* s);
322 namespace
323 {
324 /*! \brief Read in the essential dynamics input file and return its contents.
325  * \param[in] fn the file name of the edi file to be read
326  * \param[in] nr_mdatoms the number of atoms in the simulation
327  * \returns A vector of containing the essentiail dyanmics parameters.
328  * NOTE: edi files may that it may contain several ED data sets from concatenated edi files.
329  * The standard case would be a single ED data set, though. */
330 std::vector<t_edpar> read_edi_file(const char* fn, int nr_mdatoms);
331 } // namespace
332 static void crosscheck_edi_file_vs_checkpoint(const gmx_edsam& ed, edsamhistory_t* EDstate);
333 static void init_edsamstate(const gmx_edsam& ed, edsamhistory_t* EDstate);
334 static void write_edo_legend(gmx_edsam* ed, int nED, const gmx_output_env_t* oenv);
335 /* End function declarations */
336
337 /* Define formats for the column width in the output file */
338 const char EDcol_sfmt[] = "%17s";
339 const char EDcol_efmt[] = "%17.5e";
340 const char EDcol_ffmt[] = "%17f";
341
342 /* Define a formats for reading, otherwise cppcheck complains for scanf without width limits */
343 const char max_ev_fmt_d[]       = "%7d"; /* Number of eigenvectors. 9,999,999 should be enough */
344 const char max_ev_fmt_lf[]      = "%12lf";
345 const char max_ev_fmt_dlf[]     = "%7d%12lf";
346 const char max_ev_fmt_dlflflf[] = "%7d%12lf%12lf%12lf";
347 const char max_ev_fmt_lelele[]  = "%12le%12le%12le";
348
349 namespace
350 {
351 /*!\brief Determines whether to perform essential dynamics constraints other than flooding.
352  * \param[in] edi the essential dynamics parameters
353  * \returns true if essential dyanmics constraints need to be performed
354  */
355 bool bNeedDoEdsam(const t_edpar& edi)
356 {
357     return (edi.vecs.mon.neig != 0) || (edi.vecs.linfix.neig != 0) || (edi.vecs.linacc.neig != 0)
358            || (edi.vecs.radfix.neig != 0) || (edi.vecs.radacc.neig != 0) || (edi.vecs.radcon.neig != 0);
359 }
360 } // namespace
361
362
363 /* Multiple ED groups will be labeled with letters instead of numbers
364  * to avoid confusion with eigenvector indices */
365 static char get_EDgroupChar(int nr_edi, int nED)
366 {
367     if (nED == 1)
368     {
369         return ' ';
370     }
371
372     /* nr_edi = 1 -> A
373      * nr_edi = 2 -> B ...
374      */
375     return 'A' + nr_edi - 1;
376 }
377
378 namespace
379 {
380 /*! \brief The mass-weighted inner product of two coordinate vectors.
381  * Does not subtract average positions, projection on single eigenvector is returned
382  * used by: do_linfix, do_linacc, do_radfix, do_radacc, do_radcon
383  * Average position is subtracted in ed_apply_constraints prior to calling projectx
384  * \param[in] edi Essential dynamics parameters
385  * \param[in] xcoll vector of atom coordinates
386  * \param[in] vec vector of coordinates to project onto
387  * \return mass-weighted projection.
388  */
389 real projectx(const t_edpar& edi, rvec* xcoll, rvec* vec)
390 {
391     int  i;
392     real proj = 0.0;
393
394
395     for (i = 0; i < edi.sav.nr; i++)
396     {
397         proj += edi.sav.sqrtm[i] * iprod(vec[i], xcoll[i]);
398     }
399
400     return proj;
401 }
402 /*!\brief Project coordinates onto vector after substracting average position.
403  * projection is stored in vec->refproj which is used for radacc, radfix,
404  * radcon and center of flooding potential.
405  * Average positions are first substracted from x, then added back again.
406  * \param[in] edi essential dynamics parameters with average position
407  * \param[in] x Coordinates to be projected
408  * \param[out] vec eigenvector, radius and refproj are overwritten here
409  */
410 void rad_project(const t_edpar& edi, rvec* x, t_eigvec* vec)
411 {
412     int  i;
413     real rad = 0.0;
414
415     /* Subtract average positions */
416     for (i = 0; i < edi.sav.nr; i++)
417     {
418         rvec_dec(x[i], edi.sav.x[i]);
419     }
420
421     for (i = 0; i < vec->neig; i++)
422     {
423         vec->refproj[i] = projectx(edi, x, vec->vec[i]);
424         rad += gmx::square((vec->refproj[i] - vec->xproj[i]));
425     }
426     vec->radius = sqrt(rad);
427
428     /* Add average positions */
429     for (i = 0; i < edi.sav.nr; i++)
430     {
431         rvec_inc(x[i], edi.sav.x[i]);
432     }
433 }
434
435 /*!\brief Projects coordinates onto eigenvectors and stores result in vec->xproj.
436  * Mass-weighting is applied. Subtracts average positions prior to projection and add
437  * them afterwards to retain the unchanged vector.
438  * \param[in] x The coordinates to project to an eigenvector
439  * \param[in,out] vec The eigenvectors
440  * \param[in] edi essential dynamics parameters holding average structure and masses
441  */
442 void project_to_eigvectors(rvec* x, t_eigvec* vec, const t_edpar& edi)
443 {
444     if (!vec->neig)
445     {
446         return;
447     }
448
449     /* Subtract average positions */
450     for (int i = 0; i < edi.sav.nr; i++)
451     {
452         rvec_dec(x[i], edi.sav.x[i]);
453     }
454
455     for (int i = 0; i < vec->neig; i++)
456     {
457         vec->xproj[i] = projectx(edi, x, vec->vec[i]);
458     }
459
460     /* Add average positions */
461     for (int i = 0; i < edi.sav.nr; i++)
462     {
463         rvec_inc(x[i], edi.sav.x[i]);
464     }
465 }
466 } // namespace
467
468 /* Project vector x onto all edi->vecs (mon, linfix,...) */
469 static void project(rvec*    x,   /* positions to project */
470                     t_edpar* edi) /* edi data set */
471 {
472     /* It is not more work to subtract the average position in every
473      * subroutine again, because these routines are rarely used simultaneously */
474     project_to_eigvectors(x, &edi->vecs.mon, *edi);
475     project_to_eigvectors(x, &edi->vecs.linfix, *edi);
476     project_to_eigvectors(x, &edi->vecs.linacc, *edi);
477     project_to_eigvectors(x, &edi->vecs.radfix, *edi);
478     project_to_eigvectors(x, &edi->vecs.radacc, *edi);
479     project_to_eigvectors(x, &edi->vecs.radcon, *edi);
480 }
481
482 namespace
483 {
484 /*!\brief Evaluates the distance from reference to current eigenvector projection.
485  * \param[in] vec eigenvector
486  * \returns distance
487  */
488 real calc_radius(const t_eigvec& vec)
489 {
490     real rad = 0.0;
491
492     for (int i = 0; i < vec.neig; i++)
493     {
494         rad += gmx::square((vec.refproj[i] - vec.xproj[i]));
495     }
496
497     return sqrt(rad);
498 }
499 } // namespace
500
501 struct t_do_edfit
502 {
503     double** omega;
504     double** om;
505 };
506
507 static void do_edfit(int natoms, rvec* xp, rvec* x, matrix R, t_edpar* edi)
508 {
509     /* this is a copy of do_fit with some modifications */
510     int    c, r, n, j, i, irot;
511     double d[6], xnr, xpc;
512     matrix vh, vk, u;
513     int    index;
514     real   max_d;
515
516     struct t_do_edfit* loc;
517     gmx_bool           bFirst;
518
519     if (edi->buf->do_edfit != nullptr)
520     {
521         bFirst = FALSE;
522     }
523     else
524     {
525         bFirst = TRUE;
526         snew(edi->buf->do_edfit, 1);
527     }
528     loc = edi->buf->do_edfit;
529
530     if (bFirst)
531     {
532         snew(loc->omega, 2 * DIM);
533         snew(loc->om, 2 * DIM);
534         for (i = 0; i < 2 * DIM; i++)
535         {
536             snew(loc->omega[i], 2 * DIM);
537             snew(loc->om[i], 2 * DIM);
538         }
539     }
540
541     for (i = 0; (i < 6); i++)
542     {
543         d[i] = 0;
544         for (j = 0; (j < 6); j++)
545         {
546             loc->omega[i][j] = 0;
547             loc->om[i][j]    = 0;
548         }
549     }
550
551     /* calculate the matrix U */
552     clear_mat(u);
553     for (n = 0; (n < natoms); n++)
554     {
555         for (c = 0; (c < DIM); c++)
556         {
557             xpc = xp[n][c];
558             for (r = 0; (r < DIM); r++)
559             {
560                 xnr = x[n][r];
561                 u[c][r] += xnr * xpc;
562             }
563         }
564     }
565
566     /* construct loc->omega */
567     /* loc->omega is symmetric -> loc->omega==loc->omega' */
568     for (r = 0; (r < 6); r++)
569     {
570         for (c = 0; (c <= r); c++)
571         {
572             if ((r >= 3) && (c < 3))
573             {
574                 loc->omega[r][c] = u[r - 3][c];
575                 loc->omega[c][r] = u[r - 3][c];
576             }
577             else
578             {
579                 loc->omega[r][c] = 0;
580                 loc->omega[c][r] = 0;
581             }
582         }
583     }
584
585     /* determine h and k */
586     jacobi(loc->omega, 6, d, loc->om, &irot);
587
588     if (irot == 0)
589     {
590         fprintf(stderr, "IROT=0\n");
591     }
592
593     index = 0; /* For the compiler only */
594
595     for (j = 0; (j < 3); j++)
596     {
597         max_d = -1000;
598         for (i = 0; (i < 6); i++)
599         {
600             if (d[i] > max_d)
601             {
602                 max_d = d[i];
603                 index = i;
604             }
605         }
606         d[index] = -10000;
607         for (i = 0; (i < 3); i++)
608         {
609             vh[j][i] = M_SQRT2 * loc->om[i][index];
610             vk[j][i] = M_SQRT2 * loc->om[i + DIM][index];
611         }
612     }
613
614     /* determine R */
615     for (c = 0; (c < 3); c++)
616     {
617         for (r = 0; (r < 3); r++)
618         {
619             R[c][r] = vk[0][r] * vh[0][c] + vk[1][r] * vh[1][c] + vk[2][r] * vh[2][c];
620         }
621     }
622     if (det(R) < 0)
623     {
624         for (c = 0; (c < 3); c++)
625         {
626             for (r = 0; (r < 3); r++)
627             {
628                 R[c][r] = vk[0][r] * vh[0][c] + vk[1][r] * vh[1][c] - vk[2][r] * vh[2][c];
629             }
630         }
631     }
632 }
633
634
635 static void rmfit(int nat, rvec* xcoll, const rvec transvec, matrix rotmat)
636 {
637     rvec   vec;
638     matrix tmat;
639
640
641     /* Remove rotation.
642      * The inverse rotation is described by the transposed rotation matrix */
643     transpose(rotmat, tmat);
644     rotate_x(xcoll, nat, tmat);
645
646     /* Remove translation */
647     vec[XX] = -transvec[XX];
648     vec[YY] = -transvec[YY];
649     vec[ZZ] = -transvec[ZZ];
650     translate_x(xcoll, nat, vec);
651 }
652
653
654 /**********************************************************************************
655  ******************** FLOODING ****************************************************
656  **********************************************************************************
657
658    The flooding ability was added later to edsam. Many of the edsam functionality could be reused for that purpose.
659    The flooding covariance matrix, i.e. the selected eigenvectors and their corresponding eigenvalues are
660    read as 7th Component Group. The eigenvalues are coded into the stepsize parameter (as used by -linfix or -linacc).
661
662    do_md clls right in the beginning the function init_edsam, which reads the edi file, saves all the necessary information in
663    the edi structure and calls init_flood, to initialise some extra fields in the edi->flood structure.
664
665    since the flooding acts on forces do_flood is called from the function force() (force.c), while the other
666    edsam functionality is hooked into md via the update() (update.c) function acting as constraint on positions.
667
668    do_flood makes a copy of the positions,
669    fits them, projects them computes flooding_energy, and flooding forces. The forces are computed in the
670    space of the eigenvectors and are then blown up to the full cartesian space and rotated back to remove the
671    fit. Then do_flood adds these forces to the forcefield-forces
672    (given as parameter) and updates the adaptive flooding parameters Efl and deltaF.
673
674    To center the flooding potential at a different location one can use the -ori option in make_edi. The ori
675    structure is projected to the system of eigenvectors and then this position in the subspace is used as
676    center of the flooding potential.   If the option is not used, the center will be zero in the subspace,
677    i.e. the average structure as given in the make_edi file.
678
679    To use the flooding potential as restraint, make_edi has the option -restrain, which leads to inverted
680    signs of alpha2 and Efl, such that the sign in the exponential of Vfl is not inverted but the sign of
681    Vfl is inverted. Vfl = Efl * exp (- .../Efl/alpha2*x^2...) With tau>0 the negative Efl will grow slowly
682    so that the restraint is switched off slowly. When Efl==0 and inverted flooding is ON is reached no
683    further adaption is applied, Efl will stay constant at zero.
684
685    To use restraints with harmonic potentials switch -restrain and -harmonic. Then the eigenvalues are
686    used as spring constants for the harmonic potential.
687    Note that eq3 in the flooding paper (J. Comp. Chem. 2006, 27, 1693-1702) defines the parameter lambda \
688    as the inverse of the spring constant, whereas the implementation uses lambda as the spring constant.
689
690    To use more than one flooding matrix just concatenate several .edi files (cat flood1.edi flood2.edi > flood_all.edi)
691    the routine read_edi_file reads all of theses flooding files.
692    The structure t_edi is now organized as a list of t_edis and the function do_flood cycles through the list
693    calling the do_single_flood() routine for every single entry. Since every state variables have been kept in one
694    edi there is no interdependence whatsoever. The forces are added together.
695
696    To write energies into the .edr file, call the function
697         get_flood_enx_names(char**, int *nnames) to get the Header (Vfl1 Vfl2... Vfln)
698    and call
699         get_flood_energies(real Vfl[],int nnames);
700
701    TODO:
702    - one could program the whole thing such that Efl, Vfl and deltaF is written to the .edr file. -- i dont know how to do that, yet.
703
704    Maybe one should give a range of atoms for which to remove motion, so that motion is removed with
705    two edsam files from two peptide chains
706  */
707
708 // TODO split this into multiple files
709 namespace
710 {
711 /*!\brief Output flooding simulation settings and results to file.
712  * \param[in] edi Essential dynamics input parameters
713  * \param[in] fp output file
714  * \param[in] rmsd rmsd to reference structure
715  */
716 void write_edo_flood(const t_edpar& edi, FILE* fp, real rmsd)
717 {
718     /* Output how well we fit to the reference structure */
719     fprintf(fp, EDcol_ffmt, rmsd);
720
721     for (int i = 0; i < edi.flood.vecs.neig; i++)
722     {
723         fprintf(fp, EDcol_efmt, edi.flood.vecs.xproj[i]);
724
725         /* Check whether the reference projection changes with time (this can happen
726          * in case flooding is used as harmonic restraint). If so, output the
727          * current reference projection */
728         if (edi.flood.bHarmonic && edi.flood.referenceProjectionSlope[i] != 0.0)
729         {
730             fprintf(fp, EDcol_efmt, edi.flood.vecs.refproj[i]);
731         }
732
733         /* Output Efl if we are doing adaptive flooding */
734         if (0 != edi.flood.tau)
735         {
736             fprintf(fp, EDcol_efmt, edi.flood.Efl);
737         }
738         fprintf(fp, EDcol_efmt, edi.flood.Vfl);
739
740         /* Output deltaF if we are doing adaptive flooding */
741         if (0 != edi.flood.tau)
742         {
743             fprintf(fp, EDcol_efmt, edi.flood.deltaF);
744         }
745         fprintf(fp, EDcol_efmt, edi.flood.vecs.fproj[i]);
746     }
747 }
748 } // namespace
749
750
751 /* From flood.xproj compute the Vfl(x) at this point */
752 static real flood_energy(t_edpar* edi, int64_t step)
753 {
754     /* compute flooding energy Vfl
755        Vfl = Efl * exp( - \frac {kT} {2Efl alpha^2} * sum_i { \lambda_i c_i^2 } )
756        \lambda_i is the reciprocal eigenvalue 1/\sigma_i
757          it is already computed by make_edi and stored in stpsz[i]
758        bHarmonic:
759        Vfl = - Efl * 1/2(sum _i {\frac 1{\lambda_i} c_i^2})
760      */
761     real sum;
762     real Vfl;
763     int  i;
764
765
766     /* Each time this routine is called (i.e. each time step), we add a small
767      * value to the reference projection. This way a harmonic restraint towards
768      * a moving reference is realized. If no value for the additive constant
769      * is provided in the edi file, the reference will not change. */
770     if (edi->flood.bHarmonic)
771     {
772         for (i = 0; i < edi->flood.vecs.neig; i++)
773         {
774             edi->flood.vecs.refproj[i] = edi->flood.initialReferenceProjection[i]
775                                          + step * edi->flood.referenceProjectionSlope[i];
776         }
777     }
778
779     sum = 0.0;
780     /* Compute sum which will be the exponent of the exponential */
781     for (i = 0; i < edi->flood.vecs.neig; i++)
782     {
783         /* stpsz stores the reciprocal eigenvalue 1/sigma_i */
784         sum += edi->flood.vecs.stpsz[i] * (edi->flood.vecs.xproj[i] - edi->flood.vecs.refproj[i])
785                * (edi->flood.vecs.xproj[i] - edi->flood.vecs.refproj[i]);
786     }
787
788     /* Compute the Gauss function*/
789     if (edi->flood.bHarmonic)
790     {
791         Vfl = -0.5 * edi->flood.Efl * sum; /* minus sign because Efl is negative, if restrain is on. */
792     }
793     else
794     {
795         Vfl = edi->flood.Efl != 0
796                       ? edi->flood.Efl
797                                 * std::exp(-edi->flood.kT / 2 / edi->flood.Efl / edi->flood.alpha2 * sum)
798                       : 0;
799     }
800
801     return Vfl;
802 }
803
804
805 /* From the position and from Vfl compute forces in subspace -> store in edi->vec.flood.fproj */
806 static void flood_forces(t_edpar* edi)
807 {
808     /* compute the forces in the subspace of the flooding eigenvectors
809      * by the formula F_i= V_{fl}(c) * ( \frac {kT} {E_{fl}} \lambda_i c_i */
810
811     int  i;
812     real energy = edi->flood.Vfl;
813
814
815     if (edi->flood.bHarmonic)
816     {
817         for (i = 0; i < edi->flood.vecs.neig; i++)
818         {
819             edi->flood.vecs.fproj[i] = edi->flood.Efl * edi->flood.vecs.stpsz[i]
820                                        * (edi->flood.vecs.xproj[i] - edi->flood.vecs.refproj[i]);
821         }
822     }
823     else
824     {
825         for (i = 0; i < edi->flood.vecs.neig; i++)
826         {
827             /* if Efl is zero the forces are zero if not use the formula */
828             edi->flood.vecs.fproj[i] =
829                     edi->flood.Efl != 0
830                             ? edi->flood.kT / edi->flood.Efl / edi->flood.alpha2 * energy
831                                       * edi->flood.vecs.stpsz[i]
832                                       * (edi->flood.vecs.xproj[i] - edi->flood.vecs.refproj[i])
833                             : 0;
834         }
835     }
836 }
837
838 namespace
839 {
840 /*!\brief Raise forces from subspace into cartesian space.
841  * This function lifts the forces from the subspace to the cartesian space
842  * all the values not contained in the subspace are assumed to be zero and then
843  * a coordinate transformation from eigenvector to cartesian vectors is performed
844  * The nonexistent values don't have to be set to zero explicitly, they would occur
845  * as zero valued summands, hence we just stop to compute this part of the sum.
846  * For every atom we add all the contributions to this atom from all the different eigenvectors.
847  * NOTE: one could add directly to the forcefield forces, would mean we wouldn't have to clear the
848  * field forces_cart prior the computation, but we compute the forces separately
849  * to have them accessible for diagnostics
850  *
851  * \param[in] edi Essential dynamics input parameters
852  * \param[out] forces_cart The cartesian forces
853  */
854
855 void flood_blowup(const t_edpar& edi, rvec* forces_cart)
856 {
857     const real* forces_sub = edi.flood.vecs.fproj;
858     /* Calculate the cartesian forces for the local atoms */
859
860     /* Clear forces first */
861     for (int j = 0; j < edi.sav.nr_loc; j++)
862     {
863         clear_rvec(forces_cart[j]);
864     }
865
866     /* Now compute atomwise */
867     for (int j = 0; j < edi.sav.nr_loc; j++)
868     {
869         /* Compute forces_cart[edi.sav.anrs[j]] */
870         for (int eig = 0; eig < edi.flood.vecs.neig; eig++)
871         {
872             rvec addedForce;
873             /* Force vector is force * eigenvector (compute only atom j) */
874             svmul(forces_sub[eig], edi.flood.vecs.vec[eig][edi.sav.c_ind[j]], addedForce);
875             /* Add this vector to the cartesian forces */
876             rvec_inc(forces_cart[j], addedForce);
877         }
878     }
879 }
880
881 } // namespace
882
883
884 /* Update the values of Efl, deltaF depending on tau and Vfl */
885 static void update_adaption(t_edpar* edi)
886 {
887     /* this function updates the parameter Efl and deltaF according to the rules given in
888      * 'predicting unimolecular chemical reactions: chemical flooding' M Mueller et al,
889      * J. chem Phys. */
890
891     if ((edi->flood.tau < 0 ? -edi->flood.tau : edi->flood.tau) > 0.00000001)
892     {
893         edi->flood.Efl = edi->flood.Efl
894                          + edi->flood.dt / edi->flood.tau * (edi->flood.deltaF0 - edi->flood.deltaF);
895         /* check if restrain (inverted flooding) -> don't let EFL become positive */
896         if (edi->flood.alpha2 < 0 && edi->flood.Efl > -0.00000001)
897         {
898             edi->flood.Efl = 0;
899         }
900
901         edi->flood.deltaF = (1 - edi->flood.dt / edi->flood.tau) * edi->flood.deltaF
902                             + edi->flood.dt / edi->flood.tau * edi->flood.Vfl;
903     }
904 }
905
906
907 static void do_single_flood(FILE*                          edo,
908                             gmx::ArrayRef<const gmx::RVec> coords,
909                             gmx::ArrayRef<gmx::RVec>       force,
910                             t_edpar*                       edi,
911                             int64_t                        step,
912                             const matrix                   box,
913                             const t_commrec*               cr,
914                             gmx_bool bNS) /* Are we in a neighbor searching step? */
915 {
916     int                i;
917     matrix             rotmat;   /* rotation matrix */
918     matrix             tmat;     /* inverse rotation */
919     rvec               transvec; /* translation vector */
920     real               rmsdev;
921     struct t_do_edsam* buf;
922
923
924     buf = edi->buf->do_edsam;
925
926
927     /* Broadcast the positions of the AVERAGE structure such that they are known on
928      * every processor. Each node contributes its local positions x and stores them in
929      * the collective ED array buf->xcoll */
930     communicate_group_positions(cr,
931                                 buf->xcoll,
932                                 buf->shifts_xcoll,
933                                 buf->extra_shifts_xcoll,
934                                 bNS,
935                                 as_rvec_array(coords.data()),
936                                 edi->sav.nr,
937                                 edi->sav.nr_loc,
938                                 edi->sav.anrs_loc,
939                                 edi->sav.c_ind,
940                                 edi->sav.x_old,
941                                 box);
942
943     /* Only assembly REFERENCE positions if their indices differ from the average ones */
944     if (!edi->bRefEqAv)
945     {
946         communicate_group_positions(cr,
947                                     buf->xc_ref,
948                                     buf->shifts_xc_ref,
949                                     buf->extra_shifts_xc_ref,
950                                     bNS,
951                                     as_rvec_array(coords.data()),
952                                     edi->sref.nr,
953                                     edi->sref.nr_loc,
954                                     edi->sref.anrs_loc,
955                                     edi->sref.c_ind,
956                                     edi->sref.x_old,
957                                     box);
958     }
959
960     /* If bUpdateShifts was TRUE, the shifts have just been updated in get_positions.
961      * We do not need to update the shifts until the next NS step */
962     buf->bUpdateShifts = FALSE;
963
964     /* Now all nodes have all of the ED/flooding positions in edi->sav->xcoll,
965      * as well as the indices in edi->sav.anrs */
966
967     /* Fit the reference indices to the reference structure */
968     if (edi->bRefEqAv)
969     {
970         fit_to_reference(buf->xcoll, transvec, rotmat, edi);
971     }
972     else
973     {
974         fit_to_reference(buf->xc_ref, transvec, rotmat, edi);
975     }
976
977     /* Now apply the translation and rotation to the ED structure */
978     translate_and_rotate(buf->xcoll, edi->sav.nr, transvec, rotmat);
979
980     /* Project fitted structure onto supbspace -> store in edi->flood.vecs.xproj */
981     project_to_eigvectors(buf->xcoll, &edi->flood.vecs, *edi);
982
983     if (!edi->flood.bConstForce)
984     {
985         /* Compute Vfl(x) from flood.xproj */
986         edi->flood.Vfl = flood_energy(edi, step);
987
988         update_adaption(edi);
989
990         /* Compute the flooding forces */
991         flood_forces(edi);
992     }
993
994     /* Translate them into cartesian positions */
995     flood_blowup(*edi, edi->flood.forces_cartesian);
996
997     /* Rotate forces back so that they correspond to the given structure and not to the fitted one */
998     /* Each node rotates back its local forces */
999     transpose(rotmat, tmat);
1000     rotate_x(edi->flood.forces_cartesian, edi->sav.nr_loc, tmat);
1001
1002     /* Finally add forces to the main force variable */
1003     for (i = 0; i < edi->sav.nr_loc; i++)
1004     {
1005         rvec_inc(force[edi->sav.anrs_loc[i]], edi->flood.forces_cartesian[i]);
1006     }
1007
1008     /* Output is written by the master process */
1009     if (do_per_step(step, edi->outfrq) && MASTER(cr))
1010     {
1011         /* Output how well we fit to the reference */
1012         if (edi->bRefEqAv)
1013         {
1014             /* Indices of reference and average structures are identical,
1015              * thus we can calculate the rmsd to SREF using xcoll */
1016             rmsdev = rmsd_from_structure(buf->xcoll, &edi->sref);
1017         }
1018         else
1019         {
1020             /* We have to translate & rotate the reference atoms first */
1021             translate_and_rotate(buf->xc_ref, edi->sref.nr, transvec, rotmat);
1022             rmsdev = rmsd_from_structure(buf->xc_ref, &edi->sref);
1023         }
1024
1025         write_edo_flood(*edi, edo, rmsdev);
1026     }
1027 }
1028
1029
1030 /* Main flooding routine, called from do_force */
1031 void do_flood(const t_commrec*               cr,
1032               const t_inputrec&              ir,
1033               gmx::ArrayRef<const gmx::RVec> coords,
1034               gmx::ArrayRef<gmx::RVec>       force,
1035               gmx_edsam*                     ed,
1036               const matrix                   box,
1037               int64_t                        step,
1038               bool                           bNS)
1039 {
1040     /* Write time to edo, when required. Output the time anyhow since we need
1041      * it in the output file for ED constraints. */
1042     if (MASTER(cr) && do_per_step(step, ed->edpar.begin()->outfrq))
1043     {
1044         fprintf(ed->edo, "\n%12f", ir.init_t + step * ir.delta_t);
1045     }
1046
1047     if (ed->eEDtype != EssentialDynamicsType::Flooding)
1048     {
1049         return;
1050     }
1051
1052     for (auto& edi : ed->edpar)
1053     {
1054         /* Call flooding for one matrix */
1055         if (edi.flood.vecs.neig)
1056         {
1057             do_single_flood(ed->edo, coords, force, &edi, step, box, cr, bNS);
1058         }
1059     }
1060 }
1061
1062
1063 /* Called by init_edi, configure some flooding related variables and structures,
1064  * print headers to output files */
1065 static void init_flood(t_edpar* edi, gmx_edsam* ed, real dt)
1066 {
1067     int i;
1068
1069
1070     edi->flood.Efl = edi->flood.constEfl;
1071     edi->flood.Vfl = 0;
1072     edi->flood.dt  = dt;
1073
1074     if (edi->flood.vecs.neig)
1075     {
1076         /* If in any of the ED groups we find a flooding vector, flooding is turned on */
1077         ed->eEDtype = EssentialDynamicsType::Flooding;
1078
1079         fprintf(stderr,
1080                 "ED: Flooding %d eigenvector%s.\n",
1081                 edi->flood.vecs.neig,
1082                 edi->flood.vecs.neig > 1 ? "s" : "");
1083
1084         if (edi->flood.bConstForce)
1085         {
1086             /* We have used stpsz as a vehicle to carry the fproj values for constant
1087              * force flooding. Now we copy that to flood.vecs.fproj. Note that
1088              * in const force flooding, fproj is never changed. */
1089             for (i = 0; i < edi->flood.vecs.neig; i++)
1090             {
1091                 edi->flood.vecs.fproj[i] = edi->flood.vecs.stpsz[i];
1092
1093                 fprintf(stderr,
1094                         "ED: applying on eigenvector %d a constant force of %g\n",
1095                         edi->flood.vecs.ieig[i],
1096                         edi->flood.vecs.fproj[i]);
1097             }
1098         }
1099     }
1100 }
1101
1102
1103 #ifdef DEBUGHELPERS
1104 /*********** Energy book keeping ******/
1105 static void get_flood_enx_names(t_edpar* edi, char** names, int* nnames) /* get header of energies */
1106 {
1107     t_edpar* actual;
1108     int      count;
1109     char     buf[STRLEN];
1110     actual = edi;
1111     count  = 1;
1112     while (actual)
1113     {
1114         srenew(names, count);
1115         sprintf(buf, "Vfl_%d", count);
1116         names[count - 1] = gmx_strdup(buf);
1117         actual           = actual->next_edi;
1118         count++;
1119     }
1120     *nnames = count - 1;
1121 }
1122
1123
1124 static void get_flood_energies(t_edpar* edi, real Vfl[], int nnames)
1125 {
1126     /*fl has to be big enough to capture nnames-many entries*/
1127     t_edpar* actual;
1128     int      count;
1129
1130
1131     actual = edi;
1132     count  = 1;
1133     while (actual)
1134     {
1135         Vfl[count - 1] = actual->flood.Vfl;
1136         actual         = actual->next_edi;
1137         count++;
1138     }
1139     if (nnames != count - 1)
1140     {
1141         gmx_fatal(FARGS, "Number of energies is not consistent with t_edi structure");
1142     }
1143 }
1144 /************* END of FLOODING IMPLEMENTATION ****************************/
1145 #endif
1146
1147
1148 /* This function opens the ED input and output files, reads in all datasets it finds
1149  * in the input file, and cross-checks whether the .edi file information is consistent
1150  * with the essential dynamics data found in the checkpoint file (if present).
1151  * gmx make_edi can be used to create an .edi input file.
1152  */
1153 static std::unique_ptr<gmx::EssentialDynamics> ed_open(int                         natoms,
1154                                                        ObservablesHistory*         oh,
1155                                                        const char*                 ediFileName,
1156                                                        const char*                 edoFileName,
1157                                                        const gmx::StartingBehavior startingBehavior,
1158                                                        const gmx_output_env_t*     oenv,
1159                                                        const t_commrec*            cr)
1160 {
1161     auto  edHandle = std::make_unique<gmx::EssentialDynamics>();
1162     auto* ed       = edHandle->getLegacyED();
1163     /* We want to perform ED (this switch might later be upgraded to EssentialDynamicsType::Flooding) */
1164     ed->eEDtype = EssentialDynamicsType::EDSampling;
1165
1166     if (MASTER(cr))
1167     {
1168         // If we start from a checkpoint file, we already have an edsamHistory struct
1169         if (oh->edsamHistory == nullptr)
1170         {
1171             oh->edsamHistory = std::make_unique<edsamhistory_t>(edsamhistory_t{});
1172         }
1173         edsamhistory_t* EDstate = oh->edsamHistory.get();
1174
1175         /* Read the edi input file: */
1176         ed->edpar = read_edi_file(ediFileName, natoms);
1177
1178         /* Make sure the checkpoint was produced in a run using this .edi file */
1179         if (EDstate->bFromCpt)
1180         {
1181             crosscheck_edi_file_vs_checkpoint(*ed, EDstate);
1182         }
1183         else
1184         {
1185             EDstate->nED = ed->edpar.size();
1186         }
1187         init_edsamstate(*ed, EDstate);
1188
1189         /* The master opens the ED output file */
1190         if (startingBehavior == gmx::StartingBehavior::RestartWithAppending)
1191         {
1192             ed->edo = gmx_fio_fopen(edoFileName, "a+");
1193         }
1194         else
1195         {
1196             ed->edo = xvgropen(edoFileName,
1197                                "Essential dynamics / flooding output",
1198                                "Time (ps)",
1199                                "RMSDs (nm), projections on EVs (nm), ...",
1200                                oenv);
1201
1202             /* Make a descriptive legend */
1203             write_edo_legend(ed, EDstate->nED, oenv);
1204         }
1205     }
1206     return edHandle;
1207 }
1208
1209
1210 /* Broadcasts the structure data */
1211 static void bc_ed_positions(const t_commrec* cr, struct gmx_edx* s, EssentialDynamicsStructure stype)
1212 {
1213     snew_bc(MASTER(cr), s->anrs, s->nr); /* Index numbers     */
1214     snew_bc(MASTER(cr), s->x, s->nr);    /* Positions         */
1215     nblock_bc(cr->mpi_comm_mygroup, s->nr, s->anrs);
1216     nblock_bc(cr->mpi_comm_mygroup, s->nr, s->x);
1217
1218     /* For the average & reference structures we need an array for the collective indices,
1219      * and we need to broadcast the masses as well */
1220     if (stype == EssentialDynamicsStructure::Average || stype == EssentialDynamicsStructure::Reference)
1221     {
1222         /* We need these additional variables in the parallel case: */
1223         snew(s->c_ind, s->nr); /* Collective indices */
1224         /* Local atom indices get assigned in dd_make_local_group_indices.
1225          * There, also memory is allocated */
1226         s->nalloc_loc = 0;                    /* allocation size of s->anrs_loc */
1227         snew_bc(MASTER(cr), s->x_old, s->nr); /* To be able to always make the ED molecule whole, ... */
1228         nblock_bc(cr->mpi_comm_mygroup, s->nr, s->x_old); /* ... keep track of shift changes with the help of old coords */
1229     }
1230
1231     /* broadcast masses for the reference structure (for mass-weighted fitting) */
1232     if (stype == EssentialDynamicsStructure::Reference)
1233     {
1234         snew_bc(MASTER(cr), s->m, s->nr);
1235         nblock_bc(cr->mpi_comm_mygroup, s->nr, s->m);
1236     }
1237
1238     /* For the average structure we might need the masses for mass-weighting */
1239     if (stype == EssentialDynamicsStructure::Average)
1240     {
1241         snew_bc(MASTER(cr), s->sqrtm, s->nr);
1242         nblock_bc(cr->mpi_comm_mygroup, s->nr, s->sqrtm);
1243         snew_bc(MASTER(cr), s->m, s->nr);
1244         nblock_bc(cr->mpi_comm_mygroup, s->nr, s->m);
1245     }
1246 }
1247
1248
1249 /* Broadcasts the eigenvector data */
1250 static void bc_ed_vecs(const t_commrec* cr, t_eigvec* ev, int length)
1251 {
1252     int i;
1253
1254     snew_bc(MASTER(cr), ev->ieig, ev->neig);    /* index numbers of eigenvector  */
1255     snew_bc(MASTER(cr), ev->stpsz, ev->neig);   /* stepsizes per eigenvector     */
1256     snew_bc(MASTER(cr), ev->xproj, ev->neig);   /* instantaneous x projection    */
1257     snew_bc(MASTER(cr), ev->fproj, ev->neig);   /* instantaneous f projection    */
1258     snew_bc(MASTER(cr), ev->refproj, ev->neig); /* starting or target projection */
1259
1260     nblock_bc(cr->mpi_comm_mygroup, ev->neig, ev->ieig);
1261     nblock_bc(cr->mpi_comm_mygroup, ev->neig, ev->stpsz);
1262     nblock_bc(cr->mpi_comm_mygroup, ev->neig, ev->xproj);
1263     nblock_bc(cr->mpi_comm_mygroup, ev->neig, ev->fproj);
1264     nblock_bc(cr->mpi_comm_mygroup, ev->neig, ev->refproj);
1265
1266     snew_bc(MASTER(cr), ev->vec, ev->neig); /* Eigenvector components        */
1267     for (i = 0; i < ev->neig; i++)
1268     {
1269         snew_bc(MASTER(cr), ev->vec[i], length);
1270         nblock_bc(cr->mpi_comm_mygroup, length, ev->vec[i]);
1271     }
1272 }
1273
1274
1275 /* Broadcasts the ED / flooding data to other nodes
1276  * and allocates memory where needed */
1277 static void broadcast_ed_data(const t_commrec* cr, gmx_edsam* ed)
1278 {
1279     /* Master lets the other nodes know if its ED only or also flooding */
1280     gmx_bcast(sizeof(ed->eEDtype), &(ed->eEDtype), cr->mpi_comm_mygroup);
1281
1282     int numedis = ed->edpar.size();
1283     /* First let everybody know how many ED data sets to expect */
1284     gmx_bcast(sizeof(numedis), &numedis, cr->mpi_comm_mygroup);
1285     nblock_abc(MASTER(cr), cr->mpi_comm_mygroup, numedis, &(ed->edpar));
1286     /* Now transfer the ED data set(s) */
1287     for (auto& edi : ed->edpar)
1288     {
1289         /* Broadcast a single ED data set */
1290         block_bc(cr->mpi_comm_mygroup, edi);
1291
1292         /* Broadcast positions */
1293         bc_ed_positions(cr, &(edi.sref), EssentialDynamicsStructure::Reference); /* reference positions (don't broadcast masses)    */
1294         bc_ed_positions(cr, &(edi.sav), EssentialDynamicsStructure::Average); /* average positions (do broadcast masses as well) */
1295         bc_ed_positions(cr, &(edi.star), EssentialDynamicsStructure::Target); /* target positions */
1296         bc_ed_positions(cr, &(edi.sori), EssentialDynamicsStructure::Origin); /* origin positions */
1297
1298         /* Broadcast eigenvectors */
1299         bc_ed_vecs(cr, &edi.vecs.mon, edi.sav.nr);
1300         bc_ed_vecs(cr, &edi.vecs.linfix, edi.sav.nr);
1301         bc_ed_vecs(cr, &edi.vecs.linacc, edi.sav.nr);
1302         bc_ed_vecs(cr, &edi.vecs.radfix, edi.sav.nr);
1303         bc_ed_vecs(cr, &edi.vecs.radacc, edi.sav.nr);
1304         bc_ed_vecs(cr, &edi.vecs.radcon, edi.sav.nr);
1305         /* Broadcast flooding eigenvectors and, if needed, values for the moving reference */
1306         bc_ed_vecs(cr, &edi.flood.vecs, edi.sav.nr);
1307
1308         /* For harmonic restraints the reference projections can change with time */
1309         if (edi.flood.bHarmonic)
1310         {
1311             snew_bc(MASTER(cr), edi.flood.initialReferenceProjection, edi.flood.vecs.neig);
1312             snew_bc(MASTER(cr), edi.flood.referenceProjectionSlope, edi.flood.vecs.neig);
1313             nblock_bc(cr->mpi_comm_mygroup, edi.flood.vecs.neig, edi.flood.initialReferenceProjection);
1314             nblock_bc(cr->mpi_comm_mygroup, edi.flood.vecs.neig, edi.flood.referenceProjectionSlope);
1315         }
1316     }
1317 }
1318
1319
1320 /* init-routine called for every *.edi-cycle, initialises t_edpar structure */
1321 static void init_edi(const gmx_mtop_t& mtop, t_edpar* edi)
1322 {
1323     int  i;
1324     real totalmass = 0.0;
1325     rvec com;
1326
1327     /* NOTE Init_edi is executed on the master process only
1328      * The initialized data sets are then transmitted to the
1329      * other nodes in broadcast_ed_data */
1330
1331     /* evaluate masses (reference structure) */
1332     snew(edi->sref.m, edi->sref.nr);
1333     int molb = 0;
1334     for (i = 0; i < edi->sref.nr; i++)
1335     {
1336         if (edi->fitmas)
1337         {
1338             edi->sref.m[i] = mtopGetAtomMass(mtop, edi->sref.anrs[i], &molb);
1339         }
1340         else
1341         {
1342             edi->sref.m[i] = 1.0;
1343         }
1344
1345         /* Check that every m > 0. Bad things will happen otherwise. */
1346         if (edi->sref.m[i] <= 0.0)
1347         {
1348             gmx_fatal(FARGS,
1349                       "Reference structure atom %d (sam.edi index %d) has a mass of %g.\n"
1350                       "For a mass-weighted fit, all reference structure atoms need to have a mass "
1351                       ">0.\n"
1352                       "Either make the covariance analysis non-mass-weighted, or exclude massless\n"
1353                       "atoms from the reference structure by creating a proper index group.\n",
1354                       i,
1355                       edi->sref.anrs[i] + 1,
1356                       edi->sref.m[i]);
1357         }
1358
1359         totalmass += edi->sref.m[i];
1360     }
1361     edi->sref.mtot = totalmass;
1362
1363     /* Masses m and sqrt(m) for the average structure. Note that m
1364      * is needed if forces have to be evaluated in do_edsam */
1365     snew(edi->sav.sqrtm, edi->sav.nr);
1366     snew(edi->sav.m, edi->sav.nr);
1367     for (i = 0; i < edi->sav.nr; i++)
1368     {
1369         edi->sav.m[i] = mtopGetAtomMass(mtop, edi->sav.anrs[i], &molb);
1370         if (edi->pcamas)
1371         {
1372             edi->sav.sqrtm[i] = sqrt(edi->sav.m[i]);
1373         }
1374         else
1375         {
1376             edi->sav.sqrtm[i] = 1.0;
1377         }
1378
1379         /* Check that every m > 0. Bad things will happen otherwise. */
1380         if (edi->sav.sqrtm[i] <= 0.0)
1381         {
1382             gmx_fatal(FARGS,
1383                       "Average structure atom %d (sam.edi index %d) has a mass of %g.\n"
1384                       "For ED with mass-weighting, all average structure atoms need to have a mass "
1385                       ">0.\n"
1386                       "Either make the covariance analysis non-mass-weighted, or exclude massless\n"
1387                       "atoms from the average structure by creating a proper index group.\n",
1388                       i,
1389                       edi->sav.anrs[i] + 1,
1390                       edi->sav.m[i]);
1391         }
1392     }
1393
1394     /* put reference structure in origin */
1395     get_center(edi->sref.x, edi->sref.m, edi->sref.nr, com);
1396     com[XX] = -com[XX];
1397     com[YY] = -com[YY];
1398     com[ZZ] = -com[ZZ];
1399     translate_x(edi->sref.x, edi->sref.nr, com);
1400
1401     /* Init ED buffer */
1402     snew(edi->buf, 1);
1403 }
1404
1405
1406 static void check(const char* line, const char* label)
1407 {
1408     if (!strstr(line, label))
1409     {
1410         gmx_fatal(FARGS,
1411                   "Could not find input parameter %s at expected position in edsam input-file "
1412                   "(.edi)\nline read instead is %s",
1413                   label,
1414                   line);
1415     }
1416 }
1417
1418
1419 static int read_checked_edint(FILE* file, const char* label)
1420 {
1421     char line[STRLEN + 1];
1422     int  idum;
1423
1424     fgets2(line, STRLEN, file);
1425     check(line, label);
1426     fgets2(line, STRLEN, file);
1427     sscanf(line, max_ev_fmt_d, &idum);
1428     return idum;
1429 }
1430
1431 static bool read_checked_edbool(FILE* file, const char* label)
1432 {
1433     return read_checked_edint(file, label) != 0;
1434 }
1435
1436 static int read_edint(FILE* file, bool* bEOF)
1437 {
1438     char  line[STRLEN + 1];
1439     int   idum;
1440     char* eof;
1441
1442
1443     eof = fgets2(line, STRLEN, file);
1444     if (eof == nullptr)
1445     {
1446         *bEOF = TRUE;
1447         return -1;
1448     }
1449     eof = fgets2(line, STRLEN, file);
1450     if (eof == nullptr)
1451     {
1452         *bEOF = TRUE;
1453         return -1;
1454     }
1455     sscanf(line, max_ev_fmt_d, &idum);
1456     *bEOF = FALSE;
1457     return idum;
1458 }
1459
1460
1461 static real read_checked_edreal(FILE* file, const char* label)
1462 {
1463     char   line[STRLEN + 1];
1464     double rdum;
1465
1466
1467     fgets2(line, STRLEN, file);
1468     check(line, label);
1469     fgets2(line, STRLEN, file);
1470     sscanf(line, max_ev_fmt_lf, &rdum);
1471     return static_cast<real>(rdum); /* always read as double and convert to single */
1472 }
1473
1474
1475 static void read_edx(FILE* file, int number, int* anrs, rvec* x)
1476 {
1477     int    i, j;
1478     char   line[STRLEN + 1];
1479     double d[3];
1480
1481
1482     for (i = 0; i < number; i++)
1483     {
1484         fgets2(line, STRLEN, file);
1485         sscanf(line, max_ev_fmt_dlflflf, &anrs[i], &d[0], &d[1], &d[2]);
1486         anrs[i]--; /* we are reading FORTRAN indices */
1487         for (j = 0; j < 3; j++)
1488         {
1489             x[i][j] = d[j]; /* always read as double and convert to single */
1490         }
1491     }
1492 }
1493
1494 namespace
1495 {
1496 /*!\brief Read eigenvector coordinates for multiple eigenvectors from a file.
1497  * \param[in] in the file to read from
1498  * \param[in] numAtoms the number of atoms/coordinates for the eigenvector
1499  * \param[out] vec the eigen-vectors
1500  * \param[in] nEig the number of eigenvectors
1501  */
1502 void scan_edvec(FILE* in, int numAtoms, rvec*** vec, int nEig)
1503 {
1504     snew(*vec, nEig);
1505     for (int iEigenvector = 0; iEigenvector < nEig; iEigenvector++)
1506     {
1507         snew((*vec)[iEigenvector], numAtoms);
1508         for (int iAtom = 0; iAtom < numAtoms; iAtom++)
1509         {
1510             char   line[STRLEN + 1];
1511             double x, y, z;
1512             fgets2(line, STRLEN, in);
1513             sscanf(line, max_ev_fmt_lelele, &x, &y, &z);
1514             (*vec)[iEigenvector][iAtom][XX] = x;
1515             (*vec)[iEigenvector][iAtom][YY] = y;
1516             (*vec)[iEigenvector][iAtom][ZZ] = z;
1517         }
1518     }
1519 }
1520 /*!\brief Read an essential dynamics eigenvector and corresponding step size.
1521  * \param[in] in input file
1522  * \param[in] nrAtoms number of atoms/coordinates
1523  * \param[out] tvec the eigenvector
1524  */
1525 void read_edvec(FILE* in, int nrAtoms, t_eigvec* tvec)
1526 {
1527     tvec->neig = read_checked_edint(in, "NUMBER OF EIGENVECTORS");
1528     if (tvec->neig <= 0)
1529     {
1530         return;
1531     }
1532
1533     snew(tvec->ieig, tvec->neig);
1534     snew(tvec->stpsz, tvec->neig);
1535     for (int i = 0; i < tvec->neig; i++)
1536     {
1537         char line[STRLEN + 1];
1538         fgets2(line, STRLEN, in);
1539         int       numEigenVectors;
1540         double    stepSize;
1541         const int nscan = sscanf(line, max_ev_fmt_dlf, &numEigenVectors, &stepSize);
1542         if (nscan != 2)
1543         {
1544             gmx_fatal(FARGS, "Expected 2 values for flooding vec: <nr> <stpsz>\n");
1545         }
1546         tvec->ieig[i]  = numEigenVectors;
1547         tvec->stpsz[i] = stepSize;
1548     } /* end of loop over eigenvectors */
1549
1550     scan_edvec(in, nrAtoms, &tvec->vec, tvec->neig);
1551 }
1552 /*!\brief Read an essential dynamics eigenvector and intial reference projections and slopes if available.
1553  *
1554  * Eigenvector and its intial reference and slope are stored on the
1555  * same line in the .edi format. To avoid re-winding the .edi file,
1556  * the reference eigen-vector and reference data are read in one go.
1557  *
1558  * \param[in] in input file
1559  * \param[in] nrAtoms number of atoms/coordinates
1560  * \param[out] tvec the eigenvector
1561  * \param[out] initialReferenceProjection The projection onto eigenvectors as read from file.
1562  * \param[out] referenceProjectionSlope The slope of the reference projections.
1563  */
1564 bool readEdVecWithReferenceProjection(FILE*     in,
1565                                       int       nrAtoms,
1566                                       t_eigvec* tvec,
1567                                       real**    initialReferenceProjection,
1568                                       real**    referenceProjectionSlope)
1569 {
1570     bool providesReference = false;
1571     tvec->neig             = read_checked_edint(in, "NUMBER OF EIGENVECTORS");
1572     if (tvec->neig <= 0)
1573     {
1574         return false;
1575     }
1576
1577     snew(tvec->ieig, tvec->neig);
1578     snew(tvec->stpsz, tvec->neig);
1579     snew(*initialReferenceProjection, tvec->neig);
1580     snew(*referenceProjectionSlope, tvec->neig);
1581     for (int i = 0; i < tvec->neig; i++)
1582     {
1583         char line[STRLEN + 1];
1584         fgets2(line, STRLEN, in);
1585         int       numEigenVectors;
1586         double    stepSize            = 0.;
1587         double    referenceProjection = 0.;
1588         double    referenceSlope      = 0.;
1589         const int nscan               = sscanf(
1590                 line, max_ev_fmt_dlflflf, &numEigenVectors, &stepSize, &referenceProjection, &referenceSlope);
1591         /* Zero out values which were not scanned */
1592         switch (nscan)
1593         {
1594             case 4:
1595                 /* Every 4 values read, including reference position */
1596                 providesReference = true;
1597                 break;
1598             case 3:
1599                 /* A reference position is provided */
1600                 providesReference = true;
1601                 /* No value for slope, set to 0 */
1602                 referenceSlope = 0.0;
1603                 break;
1604             case 2:
1605                 /* No values for reference projection and slope, set to 0 */
1606                 referenceProjection = 0.0;
1607                 referenceSlope      = 0.0;
1608                 break;
1609             default:
1610                 gmx_fatal(FARGS,
1611                           "Expected 2 - 4 (not %d) values for flooding vec: <nr> <spring const> "
1612                           "<refproj> <refproj-slope>\n",
1613                           nscan);
1614         }
1615         (*initialReferenceProjection)[i] = referenceProjection;
1616         (*referenceProjectionSlope)[i]   = referenceSlope;
1617
1618         tvec->ieig[i]  = numEigenVectors;
1619         tvec->stpsz[i] = stepSize;
1620     } /* end of loop over eigenvectors */
1621
1622     scan_edvec(in, nrAtoms, &tvec->vec, tvec->neig);
1623     return providesReference;
1624 }
1625
1626 /*!\brief Allocate working memory for the eigenvectors.
1627  * \param[in,out] tvec eigenvector for which memory will be allocated
1628  */
1629 void setup_edvec(t_eigvec* tvec)
1630 {
1631     snew(tvec->xproj, tvec->neig);
1632     snew(tvec->fproj, tvec->neig);
1633     snew(tvec->refproj, tvec->neig);
1634 }
1635 } // namespace
1636
1637
1638 /* Check if the same atom indices are used for reference and average positions */
1639 static gmx_bool check_if_same(struct gmx_edx sref, struct gmx_edx sav)
1640 {
1641     int i;
1642
1643
1644     /* If the number of atoms differs between the two structures,
1645      * they cannot be identical */
1646     if (sref.nr != sav.nr)
1647     {
1648         return FALSE;
1649     }
1650
1651     /* Now that we know that both stuctures have the same number of atoms,
1652      * check if also the indices are identical */
1653     for (i = 0; i < sav.nr; i++)
1654     {
1655         if (sref.anrs[i] != sav.anrs[i])
1656         {
1657             return FALSE;
1658         }
1659     }
1660     fprintf(stderr,
1661             "ED: Note: Reference and average structure are composed of the same atom indices.\n");
1662
1663     return TRUE;
1664 }
1665
1666 namespace
1667 {
1668
1669 //! Oldest (lowest) magic number indicating unsupported essential dynamics input
1670 constexpr int c_oldestUnsupportedMagicNumber = 666;
1671 //! Oldest (lowest) magic number indicating supported essential dynamics input
1672 constexpr int c_oldestSupportedMagicNumber = 669;
1673 //! Latest (highest) magic number indicating supported essential dynamics input
1674 constexpr int c_latestSupportedMagicNumber = 670;
1675
1676 /*!\brief Set up essential dynamics work parameters.
1677  * \param[in] edi Essential dynamics working structure.
1678  */
1679 void setup_edi(t_edpar* edi)
1680 {
1681     snew(edi->sref.x_old, edi->sref.nr);
1682     edi->sref.sqrtm = nullptr;
1683     snew(edi->sav.x_old, edi->sav.nr);
1684     if (edi->star.nr > 0)
1685     {
1686         edi->star.sqrtm = nullptr;
1687     }
1688     if (edi->sori.nr > 0)
1689     {
1690         edi->sori.sqrtm = nullptr;
1691     }
1692     setup_edvec(&edi->vecs.linacc);
1693     setup_edvec(&edi->vecs.mon);
1694     setup_edvec(&edi->vecs.linfix);
1695     setup_edvec(&edi->vecs.linacc);
1696     setup_edvec(&edi->vecs.radfix);
1697     setup_edvec(&edi->vecs.radacc);
1698     setup_edvec(&edi->vecs.radcon);
1699     setup_edvec(&edi->flood.vecs);
1700 }
1701
1702 /*!\brief Check if edi file version supports CONST_FORCE_FLOODING.
1703  * \param[in] magicNumber indicates the essential dynamics input file version
1704  * \returns true if CONST_FORCE_FLOODING is supported
1705  */
1706 bool ediFileHasConstForceFlooding(int magicNumber)
1707 {
1708     return magicNumber > c_oldestSupportedMagicNumber;
1709 };
1710
1711 /*!\brief Reports reading success of the essential dynamics input file magic number.
1712  * \param[in] in pointer to input file
1713  * \param[out] magicNumber determines the edi file version
1714  * \returns true if setting file version from input file was successful.
1715  */
1716 bool ediFileHasValidDataSet(FILE* in, int* magicNumber)
1717 {
1718     /* the edi file is not free format, so expect problems if the input is corrupt. */
1719     bool reachedEndOfFile = true;
1720     /* check the magic number */
1721     *magicNumber = read_edint(in, &reachedEndOfFile);
1722     /* Check whether we have reached the end of the input file */
1723     return !reachedEndOfFile;
1724 }
1725
1726 /*!\brief Trigger fatal error if magic number is unsupported.
1727  * \param[in] magicNumber A magic number read from the edi file
1728  * \param[in] fn name of input file for error reporting
1729  */
1730 void exitWhenMagicNumberIsInvalid(int magicNumber, const char* fn)
1731 {
1732
1733     if (magicNumber < c_oldestSupportedMagicNumber || magicNumber > c_latestSupportedMagicNumber)
1734     {
1735         if (magicNumber >= c_oldestUnsupportedMagicNumber && magicNumber < c_oldestSupportedMagicNumber)
1736         {
1737             gmx_fatal(FARGS,
1738                       "Wrong magic number: Use newest version of make_edi to produce edi file");
1739         }
1740         else
1741         {
1742             gmx_fatal(FARGS, "Wrong magic number %d in %s", magicNumber, fn);
1743         }
1744     }
1745 }
1746
1747 /*!\brief reads an essential dynamics input file into a essential dynamics data structure.
1748  *
1749  * \param[in] in input file
1750  * \param[in] nr_mdatoms the number of md atoms, used for consistency checking
1751  * \param[in] hasConstForceFlooding determines if field "CONST_FORCE_FLOODING" is available in input file
1752  * \param[in] fn the file name of the input file for error reporting
1753  * \returns edi essential dynamics parameters
1754  */
1755 t_edpar read_edi(FILE* in, int nr_mdatoms, bool hasConstForceFlooding, const char* fn)
1756 {
1757     t_edpar edi;
1758     bool    bEOF;
1759     /* check the number of atoms */
1760     edi.nini = read_edint(in, &bEOF);
1761     if (edi.nini != nr_mdatoms)
1762     {
1763         gmx_fatal(FARGS, "Nr of atoms in %s (%d) does not match nr of md atoms (%d)", fn, edi.nini, nr_mdatoms);
1764     }
1765
1766     /* Done checking. For the rest we blindly trust the input */
1767     edi.fitmas     = read_checked_edbool(in, "FITMAS");
1768     edi.pcamas     = read_checked_edbool(in, "ANALYSIS_MAS");
1769     edi.outfrq     = read_checked_edint(in, "OUTFRQ");
1770     edi.maxedsteps = read_checked_edint(in, "MAXLEN");
1771     edi.slope      = read_checked_edreal(in, "SLOPECRIT");
1772
1773     edi.presteps        = read_checked_edint(in, "PRESTEPS");
1774     edi.flood.deltaF0   = read_checked_edreal(in, "DELTA_F0");
1775     edi.flood.deltaF    = read_checked_edreal(in, "INIT_DELTA_F");
1776     edi.flood.tau       = read_checked_edreal(in, "TAU");
1777     edi.flood.constEfl  = read_checked_edreal(in, "EFL_NULL");
1778     edi.flood.alpha2    = read_checked_edreal(in, "ALPHA2");
1779     edi.flood.kT        = read_checked_edreal(in, "KT");
1780     edi.flood.bHarmonic = read_checked_edbool(in, "HARMONIC");
1781     if (hasConstForceFlooding)
1782     {
1783         edi.flood.bConstForce = read_checked_edbool(in, "CONST_FORCE_FLOODING");
1784     }
1785     else
1786     {
1787         edi.flood.bConstForce = FALSE;
1788     }
1789     edi.sref.nr = read_checked_edint(in, "NREF");
1790
1791     /* allocate space for reference positions and read them */
1792     snew(edi.sref.anrs, edi.sref.nr);
1793     snew(edi.sref.x, edi.sref.nr);
1794     read_edx(in, edi.sref.nr, edi.sref.anrs, edi.sref.x);
1795
1796     /* average positions. they define which atoms will be used for ED sampling */
1797     edi.sav.nr = read_checked_edint(in, "NAV");
1798     snew(edi.sav.anrs, edi.sav.nr);
1799     snew(edi.sav.x, edi.sav.nr);
1800     read_edx(in, edi.sav.nr, edi.sav.anrs, edi.sav.x);
1801
1802     /* Check if the same atom indices are used for reference and average positions */
1803     edi.bRefEqAv = check_if_same(edi.sref, edi.sav);
1804
1805     /* eigenvectors */
1806
1807     read_edvec(in, edi.sav.nr, &edi.vecs.mon);
1808     read_edvec(in, edi.sav.nr, &edi.vecs.linfix);
1809     read_edvec(in, edi.sav.nr, &edi.vecs.linacc);
1810     read_edvec(in, edi.sav.nr, &edi.vecs.radfix);
1811     read_edvec(in, edi.sav.nr, &edi.vecs.radacc);
1812     read_edvec(in, edi.sav.nr, &edi.vecs.radcon);
1813
1814     /* Was a specific reference point for the flooding/umbrella potential provided in the edi file? */
1815     bool bHaveReference = false;
1816     if (edi.flood.bHarmonic)
1817     {
1818         bHaveReference = readEdVecWithReferenceProjection(in,
1819                                                           edi.sav.nr,
1820                                                           &edi.flood.vecs,
1821                                                           &edi.flood.initialReferenceProjection,
1822                                                           &edi.flood.referenceProjectionSlope);
1823     }
1824     else
1825     {
1826         read_edvec(in, edi.sav.nr, &edi.flood.vecs);
1827     }
1828
1829     /* target positions */
1830     edi.star.nr = read_edint(in, &bEOF);
1831     if (edi.star.nr > 0)
1832     {
1833         snew(edi.star.anrs, edi.star.nr);
1834         snew(edi.star.x, edi.star.nr);
1835         read_edx(in, edi.star.nr, edi.star.anrs, edi.star.x);
1836     }
1837
1838     /* positions defining origin of expansion circle */
1839     edi.sori.nr = read_edint(in, &bEOF);
1840     if (edi.sori.nr > 0)
1841     {
1842         if (bHaveReference)
1843         {
1844             /* Both an -ori structure and a at least one manual reference point have been
1845              * specified. That's ambiguous and probably not intentional. */
1846             gmx_fatal(FARGS,
1847                       "ED: An origin structure has been provided and a at least one (moving) "
1848                       "reference\n"
1849                       "    point was manually specified in the edi file. That is ambiguous. "
1850                       "Aborting.\n");
1851         }
1852         snew(edi.sori.anrs, edi.sori.nr);
1853         snew(edi.sori.x, edi.sori.nr);
1854         read_edx(in, edi.sori.nr, edi.sori.anrs, edi.sori.x);
1855     }
1856     return edi;
1857 }
1858
1859 std::vector<t_edpar> read_edi_file(const char* fn, int nr_mdatoms)
1860 {
1861     std::vector<t_edpar> essentialDynamicsParameters;
1862     FILE*                in;
1863     /* This routine is executed on the master only */
1864
1865     /* Open the .edi parameter input file */
1866     in = gmx_fio_fopen(fn, "r");
1867     fprintf(stderr, "ED: Reading edi file %s\n", fn);
1868
1869     /* Now read a sequence of ED input parameter sets from the edi file */
1870     int ediFileMagicNumber;
1871     while (ediFileHasValidDataSet(in, &ediFileMagicNumber))
1872     {
1873         exitWhenMagicNumberIsInvalid(ediFileMagicNumber, fn);
1874         bool hasConstForceFlooding = ediFileHasConstForceFlooding(ediFileMagicNumber);
1875         auto edi                   = read_edi(in, nr_mdatoms, hasConstForceFlooding, fn);
1876         setup_edi(&edi);
1877         essentialDynamicsParameters.emplace_back(edi);
1878
1879         /* Since we arrived within this while loop we know that there is still another data set to be read in */
1880         /* We need to allocate space for the data: */
1881     }
1882     if (essentialDynamicsParameters.empty())
1883     {
1884         gmx_fatal(FARGS, "No complete ED data set found in edi file %s.", fn);
1885     }
1886
1887     fprintf(stderr,
1888             "ED: Found %zu ED group%s.\n",
1889             essentialDynamicsParameters.size(),
1890             essentialDynamicsParameters.size() > 1 ? "s" : "");
1891
1892     /* Close the .edi file again */
1893     gmx_fio_fclose(in);
1894
1895     return essentialDynamicsParameters;
1896 }
1897 } // anonymous namespace
1898
1899
1900 struct t_fit_to_ref
1901 {
1902     rvec* xcopy; /* Working copy of the positions in fit_to_reference */
1903 };
1904
1905 /* Fit the current positions to the reference positions
1906  * Do not actually do the fit, just return rotation and translation.
1907  * Note that the COM of the reference structure was already put into
1908  * the origin by init_edi. */
1909 static void fit_to_reference(rvec*    xcoll,    /* The positions to be fitted */
1910                              rvec     transvec, /* The translation vector */
1911                              matrix   rotmat,   /* The rotation matrix */
1912                              t_edpar* edi)      /* Just needed for do_edfit */
1913 {
1914     rvec                 com; /* center of mass */
1915     int                  i;
1916     struct t_fit_to_ref* loc;
1917
1918
1919     /* Allocate memory the first time this routine is called for each edi group */
1920     if (nullptr == edi->buf->fit_to_ref)
1921     {
1922         snew(edi->buf->fit_to_ref, 1);
1923         snew(edi->buf->fit_to_ref->xcopy, edi->sref.nr);
1924     }
1925     loc = edi->buf->fit_to_ref;
1926
1927     /* We do not touch the original positions but work on a copy. */
1928     for (i = 0; i < edi->sref.nr; i++)
1929     {
1930         copy_rvec(xcoll[i], loc->xcopy[i]);
1931     }
1932
1933     /* Calculate the center of mass */
1934     get_center(loc->xcopy, edi->sref.m, edi->sref.nr, com);
1935
1936     transvec[XX] = -com[XX];
1937     transvec[YY] = -com[YY];
1938     transvec[ZZ] = -com[ZZ];
1939
1940     /* Subtract the center of mass from the copy */
1941     translate_x(loc->xcopy, edi->sref.nr, transvec);
1942
1943     /* Determine the rotation matrix */
1944     do_edfit(edi->sref.nr, edi->sref.x, loc->xcopy, rotmat, edi);
1945 }
1946
1947
1948 static void translate_and_rotate(rvec*  x,        /* The positions to be translated and rotated */
1949                                  int    nat,      /* How many positions are there? */
1950                                  rvec   transvec, /* The translation vector */
1951                                  matrix rotmat)   /* The rotation matrix */
1952 {
1953     /* Translation */
1954     translate_x(x, nat, transvec);
1955
1956     /* Rotation */
1957     rotate_x(x, nat, rotmat);
1958 }
1959
1960
1961 /* Gets the rms deviation of the positions to the structure s */
1962 /* fit_to_structure has to be called before calling this routine! */
1963 static real rmsd_from_structure(rvec*           x, /* The positions under consideration */
1964                                 struct gmx_edx* s) /* The structure from which the rmsd shall be computed */
1965 {
1966     real rmsd = 0.0;
1967     int  i;
1968
1969
1970     for (i = 0; i < s->nr; i++)
1971     {
1972         rmsd += distance2(s->x[i], x[i]);
1973     }
1974
1975     rmsd /= static_cast<real>(s->nr);
1976     rmsd = sqrt(rmsd);
1977
1978     return rmsd;
1979 }
1980
1981
1982 void dd_make_local_ed_indices(gmx_domdec_t* dd, struct gmx_edsam* ed)
1983 {
1984     if (ed->eEDtype != EssentialDynamicsType::None)
1985     {
1986         /* Loop over ED groups */
1987
1988         for (auto& edi : ed->edpar)
1989         {
1990             /* Local atoms of the reference structure (for fitting), need only be assembled
1991              * if their indices differ from the average ones */
1992             if (!edi.bRefEqAv)
1993             {
1994                 dd_make_local_group_indices(dd->ga2la,
1995                                             edi.sref.nr,
1996                                             edi.sref.anrs,
1997                                             &edi.sref.nr_loc,
1998                                             &edi.sref.anrs_loc,
1999                                             &edi.sref.nalloc_loc,
2000                                             edi.sref.c_ind);
2001             }
2002
2003             /* Local atoms of the average structure (on these ED will be performed) */
2004             dd_make_local_group_indices(dd->ga2la,
2005                                         edi.sav.nr,
2006                                         edi.sav.anrs,
2007                                         &edi.sav.nr_loc,
2008                                         &edi.sav.anrs_loc,
2009                                         &edi.sav.nalloc_loc,
2010                                         edi.sav.c_ind);
2011
2012             /* Indicate that the ED shift vectors for this structure need to be updated
2013              * at the next call to communicate_group_positions, since obviously we are in a NS step */
2014             edi.buf->do_edsam->bUpdateShifts = TRUE;
2015         }
2016     }
2017 }
2018
2019
2020 static inline void ed_unshift_single_coord(const matrix box, const rvec x, const ivec is, rvec xu)
2021 {
2022     int tx, ty, tz;
2023
2024
2025     tx = is[XX];
2026     ty = is[YY];
2027     tz = is[ZZ];
2028
2029     if (TRICLINIC(box))
2030     {
2031         xu[XX] = x[XX] - tx * box[XX][XX] - ty * box[YY][XX] - tz * box[ZZ][XX];
2032         xu[YY] = x[YY] - ty * box[YY][YY] - tz * box[ZZ][YY];
2033         xu[ZZ] = x[ZZ] - tz * box[ZZ][ZZ];
2034     }
2035     else
2036     {
2037         xu[XX] = x[XX] - tx * box[XX][XX];
2038         xu[YY] = x[YY] - ty * box[YY][YY];
2039         xu[ZZ] = x[ZZ] - tz * box[ZZ][ZZ];
2040     }
2041 }
2042
2043 namespace
2044 {
2045 /*!\brief Apply fixed linear constraints to essential dynamics variable.
2046  * \param[in,out] xcoll The collected coordinates.
2047  * \param[in] edi the essential dynamics parameters
2048  * \param[in] step the current simulation step
2049  */
2050 void do_linfix(rvec* xcoll, const t_edpar& edi, int64_t step)
2051 {
2052     /* loop over linfix vectors */
2053     for (int i = 0; i < edi.vecs.linfix.neig; i++)
2054     {
2055         /* calculate the projection */
2056         real proj = projectx(edi, xcoll, edi.vecs.linfix.vec[i]);
2057
2058         /* calculate the correction */
2059         real preFactor = edi.vecs.linfix.refproj[i] + step * edi.vecs.linfix.stpsz[i] - proj;
2060
2061         /* apply the correction */
2062         preFactor /= edi.sav.sqrtm[i];
2063         for (int j = 0; j < edi.sav.nr; j++)
2064         {
2065             rvec differenceVector;
2066             svmul(preFactor, edi.vecs.linfix.vec[i][j], differenceVector);
2067             rvec_inc(xcoll[j], differenceVector);
2068         }
2069     }
2070 }
2071
2072 /*!\brief Apply acceptance linear constraints to essential dynamics variable.
2073  * \param[in,out] xcoll The collected coordinates.
2074  * \param[in] edi the essential dynamics parameters
2075  */
2076 void do_linacc(rvec* xcoll, t_edpar* edi)
2077 {
2078     /* loop over linacc vectors */
2079     for (int i = 0; i < edi->vecs.linacc.neig; i++)
2080     {
2081         /* calculate the projection */
2082         real proj = projectx(*edi, xcoll, edi->vecs.linacc.vec[i]);
2083
2084         /* calculate the correction */
2085         real preFactor = 0.0;
2086         if (edi->vecs.linacc.stpsz[i] > 0.0)
2087         {
2088             if ((proj - edi->vecs.linacc.refproj[i]) < 0.0)
2089             {
2090                 preFactor = edi->vecs.linacc.refproj[i] - proj;
2091             }
2092         }
2093         if (edi->vecs.linacc.stpsz[i] < 0.0)
2094         {
2095             if ((proj - edi->vecs.linacc.refproj[i]) > 0.0)
2096             {
2097                 preFactor = edi->vecs.linacc.refproj[i] - proj;
2098             }
2099         }
2100
2101         /* apply the correction */
2102         preFactor /= edi->sav.sqrtm[i];
2103         for (int j = 0; j < edi->sav.nr; j++)
2104         {
2105             rvec differenceVector;
2106             svmul(preFactor, edi->vecs.linacc.vec[i][j], differenceVector);
2107             rvec_inc(xcoll[j], differenceVector);
2108         }
2109
2110         /* new positions will act as reference */
2111         edi->vecs.linacc.refproj[i] = proj + preFactor;
2112     }
2113 }
2114 } // namespace
2115
2116
2117 static void do_radfix(rvec* xcoll, t_edpar* edi)
2118 {
2119     int   i, j;
2120     real *proj, rad = 0.0, ratio;
2121     rvec  vec_dum;
2122
2123
2124     if (edi->vecs.radfix.neig == 0)
2125     {
2126         return;
2127     }
2128
2129     snew(proj, edi->vecs.radfix.neig);
2130
2131     /* loop over radfix vectors */
2132     for (i = 0; i < edi->vecs.radfix.neig; i++)
2133     {
2134         /* calculate the projections, radius */
2135         proj[i] = projectx(*edi, xcoll, edi->vecs.radfix.vec[i]);
2136         rad += gmx::square(proj[i] - edi->vecs.radfix.refproj[i]);
2137     }
2138
2139     rad   = sqrt(rad);
2140     ratio = (edi->vecs.radfix.stpsz[0] + edi->vecs.radfix.radius) / rad - 1.0;
2141     edi->vecs.radfix.radius += edi->vecs.radfix.stpsz[0];
2142
2143     /* loop over radfix vectors */
2144     for (i = 0; i < edi->vecs.radfix.neig; i++)
2145     {
2146         proj[i] -= edi->vecs.radfix.refproj[i];
2147
2148         /* apply the correction */
2149         proj[i] /= edi->sav.sqrtm[i];
2150         proj[i] *= ratio;
2151         for (j = 0; j < edi->sav.nr; j++)
2152         {
2153             svmul(proj[i], edi->vecs.radfix.vec[i][j], vec_dum);
2154             rvec_inc(xcoll[j], vec_dum);
2155         }
2156     }
2157
2158     sfree(proj);
2159 }
2160
2161
2162 static void do_radacc(rvec* xcoll, t_edpar* edi)
2163 {
2164     int   i, j;
2165     real *proj, rad = 0.0, ratio = 0.0;
2166     rvec  vec_dum;
2167
2168
2169     if (edi->vecs.radacc.neig == 0)
2170     {
2171         return;
2172     }
2173
2174     snew(proj, edi->vecs.radacc.neig);
2175
2176     /* loop over radacc vectors */
2177     for (i = 0; i < edi->vecs.radacc.neig; i++)
2178     {
2179         /* calculate the projections, radius */
2180         proj[i] = projectx(*edi, xcoll, edi->vecs.radacc.vec[i]);
2181         rad += gmx::square(proj[i] - edi->vecs.radacc.refproj[i]);
2182     }
2183     rad = sqrt(rad);
2184
2185     /* only correct when radius decreased */
2186     if (rad < edi->vecs.radacc.radius)
2187     {
2188         ratio = edi->vecs.radacc.radius / rad - 1.0;
2189     }
2190     else
2191     {
2192         edi->vecs.radacc.radius = rad;
2193     }
2194
2195     /* loop over radacc vectors */
2196     for (i = 0; i < edi->vecs.radacc.neig; i++)
2197     {
2198         proj[i] -= edi->vecs.radacc.refproj[i];
2199
2200         /* apply the correction */
2201         proj[i] /= edi->sav.sqrtm[i];
2202         proj[i] *= ratio;
2203         for (j = 0; j < edi->sav.nr; j++)
2204         {
2205             svmul(proj[i], edi->vecs.radacc.vec[i][j], vec_dum);
2206             rvec_inc(xcoll[j], vec_dum);
2207         }
2208     }
2209     sfree(proj);
2210 }
2211
2212
2213 struct t_do_radcon
2214 {
2215     real* proj;
2216 };
2217
2218 static void do_radcon(rvec* xcoll, t_edpar* edi)
2219 {
2220     int                 i, j;
2221     real                rad = 0.0, ratio = 0.0;
2222     struct t_do_radcon* loc;
2223     gmx_bool            bFirst;
2224     rvec                vec_dum;
2225
2226
2227     if (edi->buf->do_radcon != nullptr)
2228     {
2229         bFirst = FALSE;
2230     }
2231     else
2232     {
2233         bFirst = TRUE;
2234         snew(edi->buf->do_radcon, 1);
2235     }
2236     loc = edi->buf->do_radcon;
2237
2238     if (edi->vecs.radcon.neig == 0)
2239     {
2240         return;
2241     }
2242
2243     if (bFirst)
2244     {
2245         snew(loc->proj, edi->vecs.radcon.neig);
2246     }
2247
2248     /* loop over radcon vectors */
2249     for (i = 0; i < edi->vecs.radcon.neig; i++)
2250     {
2251         /* calculate the projections, radius */
2252         loc->proj[i] = projectx(*edi, xcoll, edi->vecs.radcon.vec[i]);
2253         rad += gmx::square(loc->proj[i] - edi->vecs.radcon.refproj[i]);
2254     }
2255     rad = sqrt(rad);
2256     /* only correct when radius increased */
2257     if (rad > edi->vecs.radcon.radius)
2258     {
2259         ratio = edi->vecs.radcon.radius / rad - 1.0;
2260
2261         /* loop over radcon vectors */
2262         for (i = 0; i < edi->vecs.radcon.neig; i++)
2263         {
2264             /* apply the correction */
2265             loc->proj[i] -= edi->vecs.radcon.refproj[i];
2266             loc->proj[i] /= edi->sav.sqrtm[i];
2267             loc->proj[i] *= ratio;
2268
2269             for (j = 0; j < edi->sav.nr; j++)
2270             {
2271                 svmul(loc->proj[i], edi->vecs.radcon.vec[i][j], vec_dum);
2272                 rvec_inc(xcoll[j], vec_dum);
2273             }
2274         }
2275     }
2276     else
2277     {
2278         edi->vecs.radcon.radius = rad;
2279     }
2280 }
2281
2282
2283 static void ed_apply_constraints(rvec* xcoll, t_edpar* edi, int64_t step)
2284 {
2285     int i;
2286
2287
2288     /* subtract the average positions */
2289     for (i = 0; i < edi->sav.nr; i++)
2290     {
2291         rvec_dec(xcoll[i], edi->sav.x[i]);
2292     }
2293
2294     /* apply the constraints */
2295     if (step >= 0)
2296     {
2297         do_linfix(xcoll, *edi, step);
2298     }
2299     do_linacc(xcoll, edi);
2300     if (step >= 0)
2301     {
2302         do_radfix(xcoll, edi);
2303     }
2304     do_radacc(xcoll, edi);
2305     do_radcon(xcoll, edi);
2306
2307     /* add back the average positions */
2308     for (i = 0; i < edi->sav.nr; i++)
2309     {
2310         rvec_inc(xcoll[i], edi->sav.x[i]);
2311     }
2312 }
2313
2314
2315 namespace
2316 {
2317 /*!\brief Write out the projections onto the eigenvectors.
2318  * The order of output corresponds to ed_output_legend().
2319  * \param[in] edi The essential dyanmics parameters
2320  * \param[in] fp The output file
2321  * \param[in] rmsd the rmsd to the reference structure
2322  */
2323 void write_edo(const t_edpar& edi, FILE* fp, real rmsd)
2324 {
2325     /* Output how well we fit to the reference structure */
2326     fprintf(fp, EDcol_ffmt, rmsd);
2327
2328     for (int i = 0; i < edi.vecs.mon.neig; i++)
2329     {
2330         fprintf(fp, EDcol_efmt, edi.vecs.mon.xproj[i]);
2331     }
2332
2333     for (int i = 0; i < edi.vecs.linfix.neig; i++)
2334     {
2335         fprintf(fp, EDcol_efmt, edi.vecs.linfix.xproj[i]);
2336     }
2337
2338     for (int i = 0; i < edi.vecs.linacc.neig; i++)
2339     {
2340         fprintf(fp, EDcol_efmt, edi.vecs.linacc.xproj[i]);
2341     }
2342
2343     for (int i = 0; i < edi.vecs.radfix.neig; i++)
2344     {
2345         fprintf(fp, EDcol_efmt, edi.vecs.radfix.xproj[i]);
2346     }
2347     if (edi.vecs.radfix.neig)
2348     {
2349         fprintf(fp, EDcol_ffmt, calc_radius(edi.vecs.radfix)); /* fixed increment radius */
2350     }
2351
2352     for (int i = 0; i < edi.vecs.radacc.neig; i++)
2353     {
2354         fprintf(fp, EDcol_efmt, edi.vecs.radacc.xproj[i]);
2355     }
2356     if (edi.vecs.radacc.neig)
2357     {
2358         fprintf(fp, EDcol_ffmt, calc_radius(edi.vecs.radacc)); /* acceptance radius */
2359     }
2360
2361     for (int i = 0; i < edi.vecs.radcon.neig; i++)
2362     {
2363         fprintf(fp, EDcol_efmt, edi.vecs.radcon.xproj[i]);
2364     }
2365     if (edi.vecs.radcon.neig)
2366     {
2367         fprintf(fp, EDcol_ffmt, calc_radius(edi.vecs.radcon)); /* contracting radius */
2368     }
2369 }
2370 } // namespace
2371
2372
2373 /* Returns if any constraints are switched on */
2374 static bool ed_constraints(EssentialDynamicsType edtype, const t_edpar& edi)
2375 {
2376     if (edtype == EssentialDynamicsType::EDSampling || edtype == EssentialDynamicsType::Flooding)
2377     {
2378         return ((edi.vecs.linfix.neig != 0) || (edi.vecs.linacc.neig != 0) || (edi.vecs.radfix.neig != 0)
2379                 || (edi.vecs.radacc.neig != 0) || (edi.vecs.radcon.neig != 0));
2380     }
2381     return false;
2382 }
2383
2384
2385 /* Copies reference projection 'refproj' to fixed 'initialReferenceProjection' variable for
2386  * flooding/ umbrella sampling simulations. */
2387 static void copyEvecReference(t_eigvec* floodvecs, real* initialReferenceProjection)
2388 {
2389     int i;
2390
2391
2392     if (nullptr == initialReferenceProjection)
2393     {
2394         snew(initialReferenceProjection, floodvecs->neig);
2395     }
2396
2397     for (i = 0; i < floodvecs->neig; i++)
2398     {
2399         initialReferenceProjection[i] = floodvecs->refproj[i];
2400     }
2401 }
2402
2403
2404 /* Call on MASTER only. Check whether the essential dynamics / flooding
2405  * groups of the checkpoint file are consistent with the provided .edi file. */
2406 static void crosscheck_edi_file_vs_checkpoint(const gmx_edsam& ed, edsamhistory_t* EDstate)
2407 {
2408     if (nullptr == EDstate->nref || nullptr == EDstate->nav)
2409     {
2410         gmx_fatal(FARGS,
2411                   "Essential dynamics and flooding can only be switched on (or off) at the\n"
2412                   "start of a new simulation. If a simulation runs with/without ED constraints,\n"
2413                   "it must also continue with/without ED constraints when checkpointing.\n"
2414                   "To switch on (or off) ED constraints, please prepare a new .tpr to start\n"
2415                   "from without a checkpoint.\n");
2416     }
2417
2418     for (size_t edinum = 0; edinum < ed.edpar.size(); ++edinum)
2419     {
2420         /* Check number of atoms in the reference and average structures */
2421         if (EDstate->nref[edinum] != ed.edpar[edinum].sref.nr)
2422         {
2423             gmx_fatal(FARGS,
2424                       "The number of reference structure atoms in ED group %c is\n"
2425                       "not the same in .cpt (NREF=%d) and .edi (NREF=%d) files!\n",
2426                       get_EDgroupChar(edinum + 1, 0),
2427                       EDstate->nref[edinum],
2428                       ed.edpar[edinum].sref.nr);
2429         }
2430         if (EDstate->nav[edinum] != ed.edpar[edinum].sav.nr)
2431         {
2432             gmx_fatal(FARGS,
2433                       "The number of average structure atoms in ED group %c is\n"
2434                       "not the same in .cpt (NREF=%d) and .edi (NREF=%d) files!\n",
2435                       get_EDgroupChar(edinum + 1, 0),
2436                       EDstate->nav[edinum],
2437                       ed.edpar[edinum].sav.nr);
2438         }
2439     }
2440
2441     if (gmx::ssize(ed.edpar) != EDstate->nED)
2442     {
2443         gmx_fatal(FARGS,
2444                   "The number of essential dynamics / flooding groups is not consistent.\n"
2445                   "There are %d ED groups in the .cpt file, but %zu in the .edi file!\n"
2446                   "Are you sure this is the correct .edi file?\n",
2447                   EDstate->nED,
2448                   ed.edpar.size());
2449     }
2450 }
2451
2452
2453 /* The edsamstate struct stores the information we need to make the ED group
2454  * whole again after restarts from a checkpoint file. Here we do the following:
2455  * a) If we did not start from .cpt, we prepare the struct for proper .cpt writing,
2456  * b) if we did start from .cpt, we copy over the last whole structures from .cpt,
2457  * c) in any case, for subsequent checkpoint writing, we set the pointers in
2458  * edsamstate to the x_old arrays, which contain the correct PBC representation of
2459  * all ED structures at the last time step. */
2460 static void init_edsamstate(const gmx_edsam& ed, edsamhistory_t* EDstate)
2461 {
2462     snew(EDstate->old_sref_p, EDstate->nED);
2463     snew(EDstate->old_sav_p, EDstate->nED);
2464
2465     /* If we did not read in a .cpt file, these arrays are not yet allocated */
2466     if (!EDstate->bFromCpt)
2467     {
2468         snew(EDstate->nref, EDstate->nED);
2469         snew(EDstate->nav, EDstate->nED);
2470     }
2471
2472     /* Loop over all ED/flooding data sets (usually only one, though) */
2473     for (int nr_edi = 0; nr_edi < EDstate->nED; nr_edi++)
2474     {
2475         const auto& edi = ed.edpar[nr_edi];
2476         /* We always need the last reference and average positions such that
2477          * in the next time step we can make the ED group whole again
2478          * if the atoms do not have the correct PBC representation */
2479         if (EDstate->bFromCpt)
2480         {
2481             /* Copy the last whole positions of reference and average group from .cpt */
2482             for (int i = 0; i < edi.sref.nr; i++)
2483             {
2484                 copy_rvec(EDstate->old_sref[nr_edi][i], edi.sref.x_old[i]);
2485             }
2486             for (int i = 0; i < edi.sav.nr; i++)
2487             {
2488                 copy_rvec(EDstate->old_sav[nr_edi][i], edi.sav.x_old[i]);
2489             }
2490         }
2491         else
2492         {
2493             EDstate->nref[nr_edi] = edi.sref.nr;
2494             EDstate->nav[nr_edi]  = edi.sav.nr;
2495         }
2496
2497         /* For subsequent checkpoint writing, set the edsamstate pointers to the edi arrays: */
2498         EDstate->old_sref_p[nr_edi] = edi.sref.x_old;
2499         EDstate->old_sav_p[nr_edi]  = edi.sav.x_old;
2500     }
2501 }
2502
2503
2504 /* Adds 'buf' to 'str' */
2505 static void add_to_string(char** str, const char* buf)
2506 {
2507     int len;
2508
2509
2510     len = strlen(*str) + strlen(buf) + 1;
2511     srenew(*str, len);
2512     strcat(*str, buf);
2513 }
2514
2515
2516 static void add_to_string_aligned(char** str, const char* buf)
2517 {
2518     char buf_aligned[STRLEN];
2519
2520     sprintf(buf_aligned, EDcol_sfmt, buf);
2521     add_to_string(str, buf_aligned);
2522 }
2523
2524
2525 static void nice_legend(const char*** setname,
2526                         int*          nsets,
2527                         char**        LegendStr,
2528                         const char*   value,
2529                         const char*   unit,
2530                         char          EDgroupchar)
2531 {
2532     auto tmp = gmx::formatString("%c %s", EDgroupchar, value);
2533     add_to_string_aligned(LegendStr, tmp.c_str());
2534     tmp += gmx::formatString(" (%s)", unit);
2535     (*setname)[*nsets] = gmx_strdup(tmp.c_str());
2536     (*nsets)++;
2537 }
2538
2539
2540 static void nice_legend_evec(const char*** setname,
2541                              int*          nsets,
2542                              char**        LegendStr,
2543                              t_eigvec*     evec,
2544                              char          EDgroupChar,
2545                              const char*   EDtype)
2546 {
2547     int  i;
2548     char tmp[STRLEN];
2549
2550
2551     for (i = 0; i < evec->neig; i++)
2552     {
2553         sprintf(tmp, "EV%dprj%s", evec->ieig[i], EDtype);
2554         nice_legend(setname, nsets, LegendStr, tmp, "nm", EDgroupChar);
2555     }
2556 }
2557
2558
2559 /* Makes a legend for the xvg output file. Call on MASTER only! */
2560 static void write_edo_legend(gmx_edsam* ed, int nED, const gmx_output_env_t* oenv)
2561 {
2562     int          i;
2563     int          nr_edi, nsets, n_flood, n_edsam;
2564     const char** setname;
2565     char         buf[STRLEN];
2566     char*        LegendStr = nullptr;
2567
2568
2569     auto edi = ed->edpar.begin();
2570
2571     fprintf(ed->edo, "# Output will be written every %d step%s\n", edi->outfrq, edi->outfrq != 1 ? "s" : "");
2572
2573     for (nr_edi = 1; nr_edi <= nED; nr_edi++)
2574     {
2575         fprintf(ed->edo, "#\n");
2576         fprintf(ed->edo,
2577                 "# Summary of applied con/restraints for the ED group %c\n",
2578                 get_EDgroupChar(nr_edi, nED));
2579         fprintf(ed->edo, "# Atoms in average structure: %d\n", edi->sav.nr);
2580         fprintf(ed->edo, "#    monitor  : %d vec%s\n", edi->vecs.mon.neig, edi->vecs.mon.neig != 1 ? "s" : "");
2581         fprintf(ed->edo,
2582                 "#    LINFIX   : %d vec%s\n",
2583                 edi->vecs.linfix.neig,
2584                 edi->vecs.linfix.neig != 1 ? "s" : "");
2585         fprintf(ed->edo,
2586                 "#    LINACC   : %d vec%s\n",
2587                 edi->vecs.linacc.neig,
2588                 edi->vecs.linacc.neig != 1 ? "s" : "");
2589         fprintf(ed->edo,
2590                 "#    RADFIX   : %d vec%s\n",
2591                 edi->vecs.radfix.neig,
2592                 edi->vecs.radfix.neig != 1 ? "s" : "");
2593         fprintf(ed->edo,
2594                 "#    RADACC   : %d vec%s\n",
2595                 edi->vecs.radacc.neig,
2596                 edi->vecs.radacc.neig != 1 ? "s" : "");
2597         fprintf(ed->edo,
2598                 "#    RADCON   : %d vec%s\n",
2599                 edi->vecs.radcon.neig,
2600                 edi->vecs.radcon.neig != 1 ? "s" : "");
2601         fprintf(ed->edo, "#    FLOODING : %d vec%s  ", edi->flood.vecs.neig, edi->flood.vecs.neig != 1 ? "s" : "");
2602
2603         if (edi->flood.vecs.neig)
2604         {
2605             /* If in any of the groups we find a flooding vector, flooding is turned on */
2606             ed->eEDtype = EssentialDynamicsType::Flooding;
2607
2608             /* Print what flavor of flooding we will do */
2609             if (0 == edi->flood.tau) /* constant flooding strength */
2610             {
2611                 fprintf(ed->edo, "Efl_null = %g", edi->flood.constEfl);
2612                 if (edi->flood.bHarmonic)
2613                 {
2614                     fprintf(ed->edo, ", harmonic");
2615                 }
2616             }
2617             else /* adaptive flooding */
2618             {
2619                 fprintf(ed->edo, ", adaptive");
2620             }
2621         }
2622         fprintf(ed->edo, "\n");
2623
2624         ++edi;
2625     }
2626
2627     /* Print a nice legend */
2628     snew(LegendStr, 1);
2629     LegendStr[0] = '\0';
2630     sprintf(buf, "#     %6s", "time");
2631     add_to_string(&LegendStr, buf);
2632
2633     /* Calculate the maximum number of columns we could end up with */
2634     edi   = ed->edpar.begin();
2635     nsets = 0;
2636     for (nr_edi = 1; nr_edi <= nED; nr_edi++)
2637     {
2638         nsets += 5 + edi->vecs.mon.neig + edi->vecs.linfix.neig + edi->vecs.linacc.neig
2639                  + edi->vecs.radfix.neig + edi->vecs.radacc.neig + edi->vecs.radcon.neig
2640                  + 6 * edi->flood.vecs.neig;
2641         ++edi;
2642     }
2643     snew(setname, nsets);
2644
2645     /* In the mdrun time step in a first function call (do_flood()) the flooding
2646      * forces are calculated and in a second function call (do_edsam()) the
2647      * ED constraints. To get a corresponding legend, we need to loop twice
2648      * over the edi groups and output first the flooding, then the ED part */
2649
2650     /* The flooding-related legend entries, if flooding is done */
2651     nsets = 0;
2652     if (EssentialDynamicsType::Flooding == ed->eEDtype)
2653     {
2654         edi = ed->edpar.begin();
2655         for (nr_edi = 1; nr_edi <= nED; nr_edi++)
2656         {
2657             /* Always write out the projection on the flooding EVs. Of course, this can also
2658              * be achieved with the monitoring option in do_edsam() (if switched on by the
2659              * user), but in that case the positions need to be communicated in do_edsam(),
2660              * which is not necessary when doing flooding only. */
2661             nice_legend(&setname, &nsets, &LegendStr, "RMSD to ref", "nm", get_EDgroupChar(nr_edi, nED));
2662
2663             for (i = 0; i < edi->flood.vecs.neig; i++)
2664             {
2665                 sprintf(buf, "EV%dprjFLOOD", edi->flood.vecs.ieig[i]);
2666                 nice_legend(&setname, &nsets, &LegendStr, buf, "nm", get_EDgroupChar(nr_edi, nED));
2667
2668                 /* Output the current reference projection if it changes with time;
2669                  * this can happen when flooding is used as harmonic restraint */
2670                 if (edi->flood.bHarmonic && edi->flood.referenceProjectionSlope[i] != 0.0)
2671                 {
2672                     sprintf(buf, "EV%d ref.prj.", edi->flood.vecs.ieig[i]);
2673                     nice_legend(&setname, &nsets, &LegendStr, buf, "nm", get_EDgroupChar(nr_edi, nED));
2674                 }
2675
2676                 /* For flooding we also output Efl, Vfl, deltaF, and the flooding forces */
2677                 if (0 != edi->flood.tau) /* only output Efl for adaptive flooding (constant otherwise) */
2678                 {
2679                     sprintf(buf, "EV%d-Efl", edi->flood.vecs.ieig[i]);
2680                     nice_legend(&setname, &nsets, &LegendStr, buf, "kJ/mol", get_EDgroupChar(nr_edi, nED));
2681                 }
2682
2683                 sprintf(buf, "EV%d-Vfl", edi->flood.vecs.ieig[i]);
2684                 nice_legend(&setname, &nsets, &LegendStr, buf, "kJ/mol", get_EDgroupChar(nr_edi, nED));
2685
2686                 if (0 != edi->flood.tau) /* only output deltaF for adaptive flooding (zero otherwise) */
2687                 {
2688                     sprintf(buf, "EV%d-deltaF", edi->flood.vecs.ieig[i]);
2689                     nice_legend(&setname, &nsets, &LegendStr, buf, "kJ/mol", get_EDgroupChar(nr_edi, nED));
2690                 }
2691
2692                 sprintf(buf, "EV%d-FLforces", edi->flood.vecs.ieig[i]);
2693                 nice_legend(&setname, &nsets, &LegendStr, buf, "kJ/mol/nm", get_EDgroupChar(nr_edi, nED));
2694             }
2695
2696             ++edi;
2697         } /* End of flooding-related legend entries */
2698     }
2699     n_flood = nsets;
2700
2701     /* Now the ED-related entries, if essential dynamics is done */
2702     edi = ed->edpar.begin();
2703     for (nr_edi = 1; nr_edi <= nED; nr_edi++)
2704     {
2705         if (bNeedDoEdsam(*edi)) /* Only print ED legend if at least one ED option is on */
2706         {
2707             nice_legend(&setname, &nsets, &LegendStr, "RMSD to ref", "nm", get_EDgroupChar(nr_edi, nED));
2708
2709             /* Essential dynamics, projections on eigenvectors */
2710             nice_legend_evec(&setname,
2711                              &nsets,
2712                              &LegendStr,
2713                              &edi->vecs.mon,
2714                              get_EDgroupChar(nr_edi, nED),
2715                              "MON");
2716             nice_legend_evec(&setname,
2717                              &nsets,
2718                              &LegendStr,
2719                              &edi->vecs.linfix,
2720                              get_EDgroupChar(nr_edi, nED),
2721                              "LINFIX");
2722             nice_legend_evec(&setname,
2723                              &nsets,
2724                              &LegendStr,
2725                              &edi->vecs.linacc,
2726                              get_EDgroupChar(nr_edi, nED),
2727                              "LINACC");
2728             nice_legend_evec(&setname,
2729                              &nsets,
2730                              &LegendStr,
2731                              &edi->vecs.radfix,
2732                              get_EDgroupChar(nr_edi, nED),
2733                              "RADFIX");
2734             if (edi->vecs.radfix.neig)
2735             {
2736                 nice_legend(&setname, &nsets, &LegendStr, "RADFIX radius", "nm", get_EDgroupChar(nr_edi, nED));
2737             }
2738             nice_legend_evec(&setname,
2739                              &nsets,
2740                              &LegendStr,
2741                              &edi->vecs.radacc,
2742                              get_EDgroupChar(nr_edi, nED),
2743                              "RADACC");
2744             if (edi->vecs.radacc.neig)
2745             {
2746                 nice_legend(&setname, &nsets, &LegendStr, "RADACC radius", "nm", get_EDgroupChar(nr_edi, nED));
2747             }
2748             nice_legend_evec(&setname,
2749                              &nsets,
2750                              &LegendStr,
2751                              &edi->vecs.radcon,
2752                              get_EDgroupChar(nr_edi, nED),
2753                              "RADCON");
2754             if (edi->vecs.radcon.neig)
2755             {
2756                 nice_legend(&setname, &nsets, &LegendStr, "RADCON radius", "nm", get_EDgroupChar(nr_edi, nED));
2757             }
2758         }
2759         ++edi;
2760     } /* end of 'pure' essential dynamics legend entries */
2761     n_edsam = nsets - n_flood;
2762
2763     xvgr_legend(ed->edo, nsets, setname, oenv);
2764     sfree(setname);
2765
2766     fprintf(ed->edo,
2767             "#\n"
2768             "# Legend for %d column%s of flooding plus %d column%s of essential dynamics data:\n",
2769             n_flood,
2770             1 == n_flood ? "" : "s",
2771             n_edsam,
2772             1 == n_edsam ? "" : "s");
2773     fprintf(ed->edo, "%s", LegendStr);
2774     sfree(LegendStr);
2775
2776     fflush(ed->edo);
2777 }
2778
2779
2780 /* Init routine for ED and flooding. Calls init_edi in a loop for every .edi-cycle
2781  * contained in the input file, creates a NULL terminated list of t_edpar structures */
2782 std::unique_ptr<gmx::EssentialDynamics> init_edsam(const gmx::MDLogger&        mdlog,
2783                                                    const char*                 ediFileName,
2784                                                    const char*                 edoFileName,
2785                                                    const gmx_mtop_t&           mtop,
2786                                                    const t_inputrec&           ir,
2787                                                    const t_commrec*            cr,
2788                                                    gmx::Constraints*           constr,
2789                                                    const t_state*              globalState,
2790                                                    ObservablesHistory*         oh,
2791                                                    const gmx_output_env_t*     oenv,
2792                                                    const gmx::StartingBehavior startingBehavior)
2793 {
2794     int    i, avindex;
2795     rvec*  x_pbc = nullptr; /* positions of the whole MD system with pbc removed  */
2796     rvec * xfit = nullptr, *xstart = nullptr; /* dummy arrays to determine initial RMSDs  */
2797     rvec   fit_transvec;                      /* translation ... */
2798     matrix fit_rotmat;                        /* ... and rotation from fit to reference structure */
2799     rvec*  ref_x_old = nullptr;               /* helper pointer */
2800
2801
2802     if (MASTER(cr))
2803     {
2804         fprintf(stderr, "ED: Initializing essential dynamics constraints.\n");
2805
2806         if (oh->edsamHistory != nullptr && oh->edsamHistory->bFromCpt && !gmx_fexist(ediFileName))
2807         {
2808             gmx_fatal(FARGS,
2809                       "The checkpoint file you provided is from an essential dynamics or flooding\n"
2810                       "simulation. Please also set the .edi file on the command line with -ei.\n");
2811         }
2812     }
2813     GMX_LOG(mdlog.info)
2814             .asParagraph()
2815             .appendText(
2816                     "Activating essential dynamics via files passed to "
2817                     "gmx mdrun -edi will change in future to be files passed to "
2818                     "gmx grompp and the related .mdp options may change also.");
2819
2820     /* Open input and output files, allocate space for ED data structure */
2821     auto  edHandle = ed_open(mtop.natoms, oh, ediFileName, edoFileName, startingBehavior, oenv, cr);
2822     auto* ed       = edHandle->getLegacyED();
2823     GMX_RELEASE_ASSERT(constr != nullptr, "Must have valid constraints object");
2824     constr->saveEdsamPointer(ed);
2825
2826     /* Needed for initializing radacc radius in do_edsam */
2827     ed->bFirst = TRUE;
2828
2829     /* The input file is read by the master and the edi structures are
2830      * initialized here. Input is stored in ed->edpar. Then the edi
2831      * structures are transferred to the other nodes */
2832     if (MASTER(cr))
2833     {
2834         /* Initialization for every ED/flooding group. Flooding uses one edi group per
2835          * flooding vector, Essential dynamics can be applied to more than one structure
2836          * as well, but will be done in the order given in the edi file, so
2837          * expect different results for different order of edi file concatenation! */
2838         for (auto& edi : ed->edpar)
2839         {
2840             init_edi(mtop, &edi);
2841             init_flood(&edi, ed, ir.delta_t);
2842         }
2843     }
2844
2845     /* The master does the work here. The other nodes get the positions
2846      * not before dd_partition_system which is called after init_edsam */
2847     if (MASTER(cr))
2848     {
2849         edsamhistory_t* EDstate = oh->edsamHistory.get();
2850
2851         if (!EDstate->bFromCpt)
2852         {
2853             /* Remove PBC, make molecule(s) subject to ED whole. */
2854             snew(x_pbc, mtop.natoms);
2855             copy_rvecn(globalState->x.rvec_array(), x_pbc, 0, mtop.natoms);
2856             do_pbc_first_mtop(nullptr, ir.pbcType, globalState->box, &mtop, x_pbc);
2857         }
2858         /* Reset pointer to first ED data set which contains the actual ED data */
2859         auto edi = ed->edpar.begin();
2860         GMX_ASSERT(!ed->edpar.empty(), "Essential dynamics parameters is undefined");
2861
2862         /* Loop over all ED/flooding data sets (usually only one, though) */
2863         for (int nr_edi = 1; nr_edi <= EDstate->nED; nr_edi++)
2864         {
2865             /* For multiple ED groups we use the output frequency that was specified
2866              * in the first set */
2867             if (nr_edi > 1)
2868             {
2869                 edi->outfrq = ed->edpar.begin()->outfrq;
2870             }
2871
2872             /* Extract the initial reference and average positions. When starting
2873              * from .cpt, these have already been read into sref.x_old
2874              * in init_edsamstate() */
2875             if (!EDstate->bFromCpt)
2876             {
2877                 /* If this is the first run (i.e. no checkpoint present) we assume
2878                  * that the starting positions give us the correct PBC representation */
2879                 for (i = 0; i < edi->sref.nr; i++)
2880                 {
2881                     copy_rvec(x_pbc[edi->sref.anrs[i]], edi->sref.x_old[i]);
2882                 }
2883
2884                 for (i = 0; i < edi->sav.nr; i++)
2885                 {
2886                     copy_rvec(x_pbc[edi->sav.anrs[i]], edi->sav.x_old[i]);
2887                 }
2888             }
2889
2890             /* Now we have the PBC-correct start positions of the reference and
2891                average structure. We copy that over to dummy arrays on which we
2892                can apply fitting to print out the RMSD. We srenew the memory since
2893                the size of the buffers is likely different for every ED group */
2894             srenew(xfit, edi->sref.nr);
2895             srenew(xstart, edi->sav.nr);
2896             if (edi->bRefEqAv)
2897             {
2898                 /* Reference indices are the same as average indices */
2899                 ref_x_old = edi->sav.x_old;
2900             }
2901             else
2902             {
2903                 ref_x_old = edi->sref.x_old;
2904             }
2905             copy_rvecn(ref_x_old, xfit, 0, edi->sref.nr);
2906             copy_rvecn(edi->sav.x_old, xstart, 0, edi->sav.nr);
2907
2908             /* Make the fit to the REFERENCE structure, get translation and rotation */
2909             fit_to_reference(xfit, fit_transvec, fit_rotmat, &(*edi));
2910
2911             /* Output how well we fit to the reference at the start */
2912             translate_and_rotate(xfit, edi->sref.nr, fit_transvec, fit_rotmat);
2913             fprintf(stderr,
2914                     "ED: Initial RMSD from reference after fit = %f nm",
2915                     rmsd_from_structure(xfit, &edi->sref));
2916             if (EDstate->nED > 1)
2917             {
2918                 fprintf(stderr, " (ED group %c)", get_EDgroupChar(nr_edi, EDstate->nED));
2919             }
2920             fprintf(stderr, "\n");
2921
2922             /* Now apply the translation and rotation to the atoms on which ED sampling will be performed */
2923             translate_and_rotate(xstart, edi->sav.nr, fit_transvec, fit_rotmat);
2924
2925             /* calculate initial projections */
2926             project(xstart, &(*edi));
2927
2928             /* For the target and origin structure both a reference (fit) and an
2929              * average structure can be provided in make_edi. If both structures
2930              * are the same, make_edi only stores one of them in the .edi file.
2931              * If they differ, first the fit and then the average structure is stored
2932              * in star (or sor), thus the number of entries in star/sor is
2933              * (n_fit + n_av) with n_fit the size of the fitting group and n_av
2934              * the size of the average group. */
2935
2936             /* process target structure, if required */
2937             if (edi->star.nr > 0)
2938             {
2939                 fprintf(stderr, "ED: Fitting target structure to reference structure\n");
2940
2941                 /* get translation & rotation for fit of target structure to reference structure */
2942                 fit_to_reference(edi->star.x, fit_transvec, fit_rotmat, &(*edi));
2943                 /* do the fit */
2944                 translate_and_rotate(edi->star.x, edi->star.nr, fit_transvec, fit_rotmat);
2945                 if (edi->star.nr == edi->sav.nr)
2946                 {
2947                     avindex = 0;
2948                 }
2949                 else /* edi->star.nr = edi->sref.nr + edi->sav.nr */
2950                 {
2951                     /* The last sav.nr indices of the target structure correspond to
2952                      * the average structure, which must be projected */
2953                     avindex = edi->star.nr - edi->sav.nr;
2954                 }
2955                 rad_project(*edi, &edi->star.x[avindex], &edi->vecs.radcon);
2956             }
2957             else
2958             {
2959                 rad_project(*edi, xstart, &edi->vecs.radcon);
2960             }
2961
2962             /* process structure that will serve as origin of expansion circle */
2963             if ((EssentialDynamicsType::Flooding == ed->eEDtype) && (!edi->flood.bConstForce))
2964             {
2965                 fprintf(stderr,
2966                         "ED: Setting center of flooding potential (0 = average structure)\n");
2967             }
2968
2969             if (edi->sori.nr > 0)
2970             {
2971                 fprintf(stderr, "ED: Fitting origin structure to reference structure\n");
2972
2973                 /* fit this structure to reference structure */
2974                 fit_to_reference(edi->sori.x, fit_transvec, fit_rotmat, &(*edi));
2975                 /* do the fit */
2976                 translate_and_rotate(edi->sori.x, edi->sori.nr, fit_transvec, fit_rotmat);
2977                 if (edi->sori.nr == edi->sav.nr)
2978                 {
2979                     avindex = 0;
2980                 }
2981                 else /* edi->sori.nr = edi->sref.nr + edi->sav.nr */
2982                 {
2983                     /* For the projection, we need the last sav.nr indices of sori */
2984                     avindex = edi->sori.nr - edi->sav.nr;
2985                 }
2986
2987                 rad_project(*edi, &edi->sori.x[avindex], &edi->vecs.radacc);
2988                 rad_project(*edi, &edi->sori.x[avindex], &edi->vecs.radfix);
2989                 if ((EssentialDynamicsType::Flooding == ed->eEDtype) && (!edi->flood.bConstForce))
2990                 {
2991                     fprintf(stderr,
2992                             "ED: The ORIGIN structure will define the flooding potential "
2993                             "center.\n");
2994                     /* Set center of flooding potential to the ORIGIN structure */
2995                     rad_project(*edi, &edi->sori.x[avindex], &edi->flood.vecs);
2996                     /* We already know that no (moving) reference position was provided,
2997                      * therefore we can overwrite refproj[0]*/
2998                     copyEvecReference(&edi->flood.vecs, edi->flood.initialReferenceProjection);
2999                 }
3000             }
3001             else /* No origin structure given */
3002             {
3003                 rad_project(*edi, xstart, &edi->vecs.radacc);
3004                 rad_project(*edi, xstart, &edi->vecs.radfix);
3005                 if ((EssentialDynamicsType::Flooding == ed->eEDtype) && (!edi->flood.bConstForce))
3006                 {
3007                     if (edi->flood.bHarmonic)
3008                     {
3009                         fprintf(stderr,
3010                                 "ED: A (possibly changing) ref. projection will define the "
3011                                 "flooding potential center.\n");
3012                         for (i = 0; i < edi->flood.vecs.neig; i++)
3013                         {
3014                             edi->flood.vecs.refproj[i] = edi->flood.initialReferenceProjection[i];
3015                         }
3016                     }
3017                     else
3018                     {
3019                         fprintf(stderr,
3020                                 "ED: The AVERAGE structure will define the flooding potential "
3021                                 "center.\n");
3022                         /* Set center of flooding potential to the center of the covariance matrix,
3023                          * i.e. the average structure, i.e. zero in the projected system */
3024                         for (i = 0; i < edi->flood.vecs.neig; i++)
3025                         {
3026                             edi->flood.vecs.refproj[i] = 0.0;
3027                         }
3028                     }
3029                 }
3030             }
3031             /* For convenience, output the center of the flooding potential for the eigenvectors */
3032             if ((EssentialDynamicsType::Flooding == ed->eEDtype) && (!edi->flood.bConstForce))
3033             {
3034                 for (i = 0; i < edi->flood.vecs.neig; i++)
3035                 {
3036                     fprintf(stdout,
3037                             "ED: EV %d flooding potential center: %11.4e",
3038                             edi->flood.vecs.ieig[i],
3039                             edi->flood.vecs.refproj[i]);
3040                     if (edi->flood.bHarmonic)
3041                     {
3042                         fprintf(stdout, " (adding %11.4e/timestep)", edi->flood.referenceProjectionSlope[i]);
3043                     }
3044                     fprintf(stdout, "\n");
3045                 }
3046             }
3047
3048             /* set starting projections for linsam */
3049             rad_project(*edi, xstart, &edi->vecs.linacc);
3050             rad_project(*edi, xstart, &edi->vecs.linfix);
3051
3052             /* Prepare for the next edi data set: */
3053             ++edi;
3054         }
3055         /* Cleaning up on the master node: */
3056         if (!EDstate->bFromCpt)
3057         {
3058             sfree(x_pbc);
3059         }
3060         sfree(xfit);
3061         sfree(xstart);
3062
3063     } /* end of MASTER only section */
3064
3065     if (PAR(cr))
3066     {
3067         /* Broadcast the essential dynamics / flooding data to all nodes */
3068         broadcast_ed_data(cr, ed);
3069     }
3070     else
3071     {
3072         /* In the single-CPU case, point the local atom numbers pointers to the global
3073          * one, so that we can use the same notation in serial and parallel case: */
3074         /* Loop over all ED data sets (usually only one, though) */
3075         for (auto edi = ed->edpar.begin(); edi != ed->edpar.end(); ++edi)
3076         {
3077             edi->sref.anrs_loc = edi->sref.anrs;
3078             edi->sav.anrs_loc  = edi->sav.anrs;
3079             edi->star.anrs_loc = edi->star.anrs;
3080             edi->sori.anrs_loc = edi->sori.anrs;
3081             /* For the same reason as above, make a dummy c_ind array: */
3082             snew(edi->sav.c_ind, edi->sav.nr);
3083             /* Initialize the array */
3084             for (i = 0; i < edi->sav.nr; i++)
3085             {
3086                 edi->sav.c_ind[i] = i;
3087             }
3088             /* In the general case we will need a different-sized array for the reference indices: */
3089             if (!edi->bRefEqAv)
3090             {
3091                 snew(edi->sref.c_ind, edi->sref.nr);
3092                 for (i = 0; i < edi->sref.nr; i++)
3093                 {
3094                     edi->sref.c_ind[i] = i;
3095                 }
3096             }
3097             /* Point to the very same array in case of other structures: */
3098             edi->star.c_ind = edi->sav.c_ind;
3099             edi->sori.c_ind = edi->sav.c_ind;
3100             /* In the serial case, the local number of atoms is the global one: */
3101             edi->sref.nr_loc = edi->sref.nr;
3102             edi->sav.nr_loc  = edi->sav.nr;
3103             edi->star.nr_loc = edi->star.nr;
3104             edi->sori.nr_loc = edi->sori.nr;
3105         }
3106     }
3107
3108     /* Allocate space for ED buffer variables */
3109     /* Again, loop over ED data sets */
3110     for (auto edi = ed->edpar.begin(); edi != ed->edpar.end(); ++edi)
3111     {
3112         /* Allocate space for ED buffer variables */
3113         snew_bc(MASTER(cr), edi->buf, 1); /* MASTER has already allocated edi->buf in init_edi() */
3114         snew(edi->buf->do_edsam, 1);
3115
3116         /* Space for collective ED buffer variables */
3117
3118         /* Collective positions of atoms with the average indices */
3119         snew(edi->buf->do_edsam->xcoll, edi->sav.nr);
3120         snew(edi->buf->do_edsam->shifts_xcoll, edi->sav.nr); /* buffer for xcoll shifts */
3121         snew(edi->buf->do_edsam->extra_shifts_xcoll, edi->sav.nr);
3122         /* Collective positions of atoms with the reference indices */
3123         if (!edi->bRefEqAv)
3124         {
3125             snew(edi->buf->do_edsam->xc_ref, edi->sref.nr);
3126             snew(edi->buf->do_edsam->shifts_xc_ref, edi->sref.nr); /* To store the shifts in */
3127             snew(edi->buf->do_edsam->extra_shifts_xc_ref, edi->sref.nr);
3128         }
3129
3130         /* Get memory for flooding forces */
3131         snew(edi->flood.forces_cartesian, edi->sav.nr);
3132     }
3133
3134     /* Flush the edo file so that the user can check some things
3135      * when the simulation has started */
3136     if (ed->edo)
3137     {
3138         fflush(ed->edo);
3139     }
3140
3141     return edHandle;
3142 }
3143
3144
3145 void do_edsam(const t_inputrec*        ir,
3146               int64_t                  step,
3147               const t_commrec*         cr,
3148               gmx::ArrayRef<gmx::RVec> coords,
3149               gmx::ArrayRef<gmx::RVec> velocities,
3150               const matrix             box,
3151               gmx_edsam*               ed)
3152 {
3153     int    i, edinr, iupdate = 500;
3154     matrix rotmat;         /* rotation matrix */
3155     rvec   transvec;       /* translation vector */
3156     rvec   dv, dx, x_unsh; /* tmp vectors for velocity, distance, unshifted x coordinate */
3157     real   dt_1;           /* 1/dt */
3158     struct t_do_edsam* buf;
3159     real     rmsdev    = -1; /* RMSD from reference structure prior to applying the constraints */
3160     gmx_bool bSuppress = FALSE; /* Write .xvg output file on master? */
3161
3162
3163     /* Check if ED sampling has to be performed */
3164     if (ed->eEDtype == EssentialDynamicsType::None)
3165     {
3166         return;
3167     }
3168
3169     dt_1 = 1.0 / ir->delta_t;
3170
3171     /* Loop over all ED groups (usually one) */
3172     edinr = 0;
3173     for (auto& edi : ed->edpar)
3174     {
3175         edinr++;
3176         if (bNeedDoEdsam(edi))
3177         {
3178
3179             buf = edi.buf->do_edsam;
3180
3181             if (ed->bFirst)
3182             {
3183                 /* initialize radacc radius for slope criterion */
3184                 buf->oldrad = calc_radius(edi.vecs.radacc);
3185             }
3186
3187             /* Copy the positions into buf->xc* arrays and after ED
3188              * feed back corrections to the official positions */
3189
3190             /* Broadcast the ED positions such that every node has all of them
3191              * Every node contributes its local positions xs and stores it in
3192              * the collective buf->xcoll array. Note that for edinr > 1
3193              * xs could already have been modified by an earlier ED */
3194
3195             communicate_group_positions(cr,
3196                                         buf->xcoll,
3197                                         buf->shifts_xcoll,
3198                                         buf->extra_shifts_xcoll,
3199                                         PAR(cr) ? buf->bUpdateShifts : TRUE,
3200                                         as_rvec_array(coords.data()),
3201                                         edi.sav.nr,
3202                                         edi.sav.nr_loc,
3203                                         edi.sav.anrs_loc,
3204                                         edi.sav.c_ind,
3205                                         edi.sav.x_old,
3206                                         box);
3207
3208             /* Only assembly reference positions if their indices differ from the average ones */
3209             if (!edi.bRefEqAv)
3210             {
3211                 communicate_group_positions(cr,
3212                                             buf->xc_ref,
3213                                             buf->shifts_xc_ref,
3214                                             buf->extra_shifts_xc_ref,
3215                                             PAR(cr) ? buf->bUpdateShifts : TRUE,
3216                                             as_rvec_array(coords.data()),
3217                                             edi.sref.nr,
3218                                             edi.sref.nr_loc,
3219                                             edi.sref.anrs_loc,
3220                                             edi.sref.c_ind,
3221                                             edi.sref.x_old,
3222                                             box);
3223             }
3224
3225             /* If bUpdateShifts was TRUE then the shifts have just been updated in communicate_group_positions.
3226              * We do not need to update the shifts until the next NS step. Note that dd_make_local_ed_indices
3227              * set bUpdateShifts=TRUE in the parallel case. */
3228             buf->bUpdateShifts = FALSE;
3229
3230             /* Now all nodes have all of the ED positions in edi.sav->xcoll,
3231              * as well as the indices in edi.sav.anrs */
3232
3233             /* Fit the reference indices to the reference structure */
3234             if (edi.bRefEqAv)
3235             {
3236                 fit_to_reference(buf->xcoll, transvec, rotmat, &edi);
3237             }
3238             else
3239             {
3240                 fit_to_reference(buf->xc_ref, transvec, rotmat, &edi);
3241             }
3242
3243             /* Now apply the translation and rotation to the ED structure */
3244             translate_and_rotate(buf->xcoll, edi.sav.nr, transvec, rotmat);
3245
3246             /* Find out how well we fit to the reference (just for output steps) */
3247             if (do_per_step(step, edi.outfrq) && MASTER(cr))
3248             {
3249                 if (edi.bRefEqAv)
3250                 {
3251                     /* Indices of reference and average structures are identical,
3252                      * thus we can calculate the rmsd to SREF using xcoll */
3253                     rmsdev = rmsd_from_structure(buf->xcoll, &edi.sref);
3254                 }
3255                 else
3256                 {
3257                     /* We have to translate & rotate the reference atoms first */
3258                     translate_and_rotate(buf->xc_ref, edi.sref.nr, transvec, rotmat);
3259                     rmsdev = rmsd_from_structure(buf->xc_ref, &edi.sref);
3260                 }
3261             }
3262
3263             /* update radsam references, when required */
3264             if (do_per_step(step, edi.maxedsteps) && step >= edi.presteps)
3265             {
3266                 project(buf->xcoll, &edi);
3267                 rad_project(edi, buf->xcoll, &edi.vecs.radacc);
3268                 rad_project(edi, buf->xcoll, &edi.vecs.radfix);
3269                 buf->oldrad = -1.e5;
3270             }
3271
3272             /* update radacc references, when required */
3273             if (do_per_step(step, iupdate) && step >= edi.presteps)
3274             {
3275                 edi.vecs.radacc.radius = calc_radius(edi.vecs.radacc);
3276                 if (edi.vecs.radacc.radius - buf->oldrad < edi.slope)
3277                 {
3278                     project(buf->xcoll, &edi);
3279                     rad_project(edi, buf->xcoll, &edi.vecs.radacc);
3280                     buf->oldrad = 0.0;
3281                 }
3282                 else
3283                 {
3284                     buf->oldrad = edi.vecs.radacc.radius;
3285                 }
3286             }
3287
3288             /* apply the constraints */
3289             if (step >= edi.presteps && ed_constraints(ed->eEDtype, edi))
3290             {
3291                 /* ED constraints should be applied already in the first MD step
3292                  * (which is step 0), therefore we pass step+1 to the routine */
3293                 ed_apply_constraints(buf->xcoll, &edi, step + 1 - ir->init_step);
3294             }
3295
3296             /* write to edo, when required */
3297             if (do_per_step(step, edi.outfrq))
3298             {
3299                 project(buf->xcoll, &edi);
3300                 if (MASTER(cr) && !bSuppress)
3301                 {
3302                     write_edo(edi, ed->edo, rmsdev);
3303                 }
3304             }
3305
3306             /* Copy back the positions unless monitoring only */
3307             if (ed_constraints(ed->eEDtype, edi))
3308             {
3309                 /* remove fitting */
3310                 rmfit(edi.sav.nr, buf->xcoll, transvec, rotmat);
3311
3312                 /* Copy the ED corrected positions into the coordinate array */
3313                 /* Each node copies its local part. In the serial case, nat_loc is the
3314                  * total number of ED atoms */
3315                 for (i = 0; i < edi.sav.nr_loc; i++)
3316                 {
3317                     /* Unshift local ED coordinate and store in x_unsh */
3318                     ed_unshift_single_coord(
3319                             box, buf->xcoll[edi.sav.c_ind[i]], buf->shifts_xcoll[edi.sav.c_ind[i]], x_unsh);
3320
3321                     /* dx is the ED correction to the positions: */
3322                     rvec_sub(x_unsh, coords[edi.sav.anrs_loc[i]], dx);
3323
3324                     if (!velocities.empty())
3325                     {
3326                         /* dv is the ED correction to the velocity: */
3327                         svmul(dt_1, dx, dv);
3328                         /* apply the velocity correction: */
3329                         rvec_inc(velocities[edi.sav.anrs_loc[i]], dv);
3330                     }
3331                     /* Finally apply the position correction due to ED: */
3332                     copy_rvec(x_unsh, coords[edi.sav.anrs_loc[i]]);
3333                 }
3334             }
3335         } /* END of if ( bNeedDoEdsam(edi) ) */
3336
3337         /* Prepare for the next ED group */
3338
3339     } /* END of loop over ED groups */
3340
3341     ed->bFirst = FALSE;
3342 }