Converted essentialdynamics module to C++.
[gromacs.git] / src / gromacs / essentialdynamics / edsam.cpp
blob9caf58aeaa0b23c491c82ea1505df0263b170187
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "gmxpre.h"
39 #include "edsam.h"
41 #include <stdio.h>
42 #include <string.h>
43 #include <time.h>
45 #include "gromacs/fileio/confio.h"
46 #include "gromacs/fileio/gmxfio.h"
47 #include "gromacs/fileio/xvgr.h"
48 #include "gromacs/legacyheaders/mdrun.h"
49 #include "gromacs/legacyheaders/names.h"
50 #include "gromacs/legacyheaders/nrnb.h"
51 #include "gromacs/legacyheaders/txtdump.h"
52 #include "gromacs/legacyheaders/typedefs.h"
53 #include "gromacs/legacyheaders/update.h"
54 #include "gromacs/linearalgebra/nrjac.h"
55 #include "gromacs/math/vec.h"
56 #include "gromacs/mdlib/groupcoord.h"
57 #include "gromacs/pbcutil/pbc.h"
58 #include "gromacs/topology/mtop_util.h"
59 #include "gromacs/utility/cstringutil.h"
60 #include "gromacs/utility/fatalerror.h"
61 #include "gromacs/utility/gmxassert.h"
62 #include "gromacs/utility/smalloc.h"
65 /* We use the same defines as in broadcaststructs.cpp here */
66 #define block_bc(cr, d) gmx_bcast( sizeof(d), &(d), (cr))
67 #define nblock_bc(cr, nr, d) gmx_bcast((nr)*sizeof((d)[0]), (d), (cr))
68 #define snew_bc(cr, d, nr) { if (!MASTER(cr)) {snew((d), (nr)); }}
70 /* enum to identify the type of ED: none, normal ED, flooding */
71 enum {
72 eEDnone, eEDedsam, eEDflood, eEDnr
75 /* enum to identify operations on reference, average, origin, target structures */
76 enum {
77 eedREF, eedAV, eedORI, eedTAR, eedNR
81 typedef struct
83 int neig; /* nr of eigenvectors */
84 int *ieig; /* index nrs of eigenvectors */
85 real *stpsz; /* stepsizes (per eigenvector) */
86 rvec **vec; /* eigenvector components */
87 real *xproj; /* instantaneous x projections */
88 real *fproj; /* instantaneous f projections */
89 real radius; /* instantaneous radius */
90 real *refproj; /* starting or target projections */
91 /* When using flooding as harmonic restraint: The current reference projection
92 * is at each step calculated from the initial refproj0 and the slope. */
93 real *refproj0, *refprojslope;
94 } t_eigvec;
97 typedef struct
99 t_eigvec mon; /* only monitored, no constraints */
100 t_eigvec linfix; /* fixed linear constraints */
101 t_eigvec linacc; /* acceptance linear constraints */
102 t_eigvec radfix; /* fixed radial constraints (exp) */
103 t_eigvec radacc; /* acceptance radial constraints (exp) */
104 t_eigvec radcon; /* acceptance rad. contraction constr. */
105 } t_edvecs;
108 typedef struct
110 real deltaF0;
111 gmx_bool bHarmonic; /* Use flooding for harmonic restraint on
112 the eigenvector */
113 gmx_bool bConstForce; /* Do not calculate a flooding potential,
114 instead flood with a constant force */
115 real tau;
116 real deltaF;
117 real Efl;
118 real kT;
119 real Vfl;
120 real dt;
121 real constEfl;
122 real alpha2;
123 rvec *forces_cartesian;
124 t_eigvec vecs; /* use flooding for these */
125 } t_edflood;
128 /* This type is for the average, reference, target, and origin structure */
129 typedef struct gmx_edx
131 int nr; /* number of atoms this structure contains */
132 int nr_loc; /* number of atoms on local node */
133 int *anrs; /* atom index numbers */
134 int *anrs_loc; /* local atom index numbers */
135 int nalloc_loc; /* allocation size of anrs_loc */
136 int *c_ind; /* at which position of the whole anrs
137 * array is a local atom?, i.e.
138 * c_ind[0...nr_loc-1] gives the atom index
139 * with respect to the collective
140 * anrs[0...nr-1] array */
141 rvec *x; /* positions for this structure */
142 rvec *x_old; /* Last positions which have the correct PBC
143 representation of the ED group. In
144 combination with keeping track of the
145 shift vectors, the ED group can always
146 be made whole */
147 real *m; /* masses */
148 real mtot; /* total mass (only used in sref) */
149 real *sqrtm; /* sqrt of the masses used for mass-
150 * weighting of analysis (only used in sav) */
151 } t_gmx_edx;
154 typedef struct edpar
156 int nini; /* total Nr of atoms */
157 gmx_bool fitmas; /* true if trans fit with cm */
158 gmx_bool pcamas; /* true if mass-weighted PCA */
159 int presteps; /* number of steps to run without any
160 * perturbations ... just monitoring */
161 int outfrq; /* freq (in steps) of writing to edo */
162 int maxedsteps; /* max nr of steps per cycle */
164 /* all gmx_edx datasets are copied to all nodes in the parallel case */
165 struct gmx_edx sref; /* reference positions, to these fitting
166 * will be done */
167 gmx_bool bRefEqAv; /* If true, reference & average indices
168 * are the same. Used for optimization */
169 struct gmx_edx sav; /* average positions */
170 struct gmx_edx star; /* target positions */
171 struct gmx_edx sori; /* origin positions */
173 t_edvecs vecs; /* eigenvectors */
174 real slope; /* minimal slope in acceptance radexp */
176 t_edflood flood; /* parameters especially for flooding */
177 struct t_ed_buffer *buf; /* handle to local buffers */
178 struct edpar *next_edi; /* Pointer to another ED group */
179 } t_edpar;
182 typedef struct gmx_edsam
184 int eEDtype; /* Type of ED: see enums above */
185 FILE *edo; /* output file pointer */
186 t_edpar *edpar;
187 gmx_bool bFirst;
188 } t_gmx_edsam;
191 struct t_do_edsam
193 matrix old_rotmat;
194 real oldrad;
195 rvec old_transvec, older_transvec, transvec_compact;
196 rvec *xcoll; /* Positions from all nodes, this is the
197 collective set we work on.
198 These are the positions of atoms with
199 average structure indices */
200 rvec *xc_ref; /* same but with reference structure indices */
201 ivec *shifts_xcoll; /* Shifts for xcoll */
202 ivec *extra_shifts_xcoll; /* xcoll shift changes since last NS step */
203 ivec *shifts_xc_ref; /* Shifts for xc_ref */
204 ivec *extra_shifts_xc_ref; /* xc_ref shift changes since last NS step */
205 gmx_bool bUpdateShifts; /* TRUE in NS steps to indicate that the
206 ED shifts for this ED group need to
207 be updated */
211 /* definition of ED buffer structure */
212 struct t_ed_buffer
214 struct t_fit_to_ref * fit_to_ref;
215 struct t_do_edfit * do_edfit;
216 struct t_do_edsam * do_edsam;
217 struct t_do_radcon * do_radcon;
221 /* Function declarations */
222 static void fit_to_reference(rvec *xcoll, rvec transvec, matrix rotmat, t_edpar *edi);
223 static void translate_and_rotate(rvec *x, int nat, rvec transvec, matrix rotmat);
224 static real rmsd_from_structure(rvec *x, struct gmx_edx *s);
225 static int read_edi_file(const char *fn, t_edpar *edi, int nr_mdatoms);
226 static void crosscheck_edi_file_vs_checkpoint(gmx_edsam_t ed, edsamstate_t *EDstate);
227 static void init_edsamstate(gmx_edsam_t ed, edsamstate_t *EDstate);
228 static void write_edo_legend(gmx_edsam_t ed, int nED, const output_env_t oenv);
229 /* End function declarations */
231 /* Define formats for the column width in the output file */
232 const char EDcol_sfmt[] = "%17s";
233 const char EDcol_efmt[] = "%17.5e";
234 const char EDcol_ffmt[] = "%17f";
236 /* Define a formats for reading, otherwise cppcheck complains for scanf without width limits */
237 const char max_ev_fmt_d[] = "%7d"; /* Number of eigenvectors. 9,999,999 should be enough */
238 const char max_ev_fmt_lf[] = "%12lf";
239 const char max_ev_fmt_dlf[] = "%7d%12lf";
240 const char max_ev_fmt_dlflflf[] = "%7d%12lf%12lf%12lf";
241 const char max_ev_fmt_lelele[] = "%12le%12le%12le";
243 /* Do we have to perform essential dynamics constraints or possibly only flooding
244 * for any of the ED groups? */
245 static gmx_bool bNeedDoEdsam(t_edpar *edi)
247 return edi->vecs.mon.neig
248 || edi->vecs.linfix.neig
249 || edi->vecs.linacc.neig
250 || edi->vecs.radfix.neig
251 || edi->vecs.radacc.neig
252 || edi->vecs.radcon.neig;
256 /* Multiple ED groups will be labeled with letters instead of numbers
257 * to avoid confusion with eigenvector indices */
258 static char get_EDgroupChar(int nr_edi, int nED)
260 if (nED == 1)
262 return ' ';
265 /* nr_edi = 1 -> A
266 * nr_edi = 2 -> B ...
268 return 'A' + nr_edi - 1;
272 /* Does not subtract average positions, projection on single eigenvector is returned
273 * used by: do_linfix, do_linacc, do_radfix, do_radacc, do_radcon
274 * Average position is subtracted in ed_apply_constraints prior to calling projectx
276 static real projectx(t_edpar *edi, rvec *xcoll, rvec *vec)
278 int i;
279 real proj = 0.0;
282 for (i = 0; i < edi->sav.nr; i++)
284 proj += edi->sav.sqrtm[i]*iprod(vec[i], xcoll[i]);
287 return proj;
291 /* Specialized: projection is stored in vec->refproj
292 * -> used for radacc, radfix, radcon and center of flooding potential
293 * subtracts average positions, projects vector x */
294 static void rad_project(t_edpar *edi, rvec *x, t_eigvec *vec)
296 int i;
297 real rad = 0.0;
299 /* Subtract average positions */
300 for (i = 0; i < edi->sav.nr; i++)
302 rvec_dec(x[i], edi->sav.x[i]);
305 for (i = 0; i < vec->neig; i++)
307 vec->refproj[i] = projectx(edi, x, vec->vec[i]);
308 rad += pow((vec->refproj[i]-vec->xproj[i]), 2);
310 vec->radius = sqrt(rad);
312 /* Add average positions */
313 for (i = 0; i < edi->sav.nr; i++)
315 rvec_inc(x[i], edi->sav.x[i]);
320 /* Project vector x, subtract average positions prior to projection and add
321 * them afterwards to retain the unchanged vector. Store in xproj. Mass-weighting
322 * is applied. */
323 static void project_to_eigvectors(rvec *x, /* The positions to project to an eigenvector */
324 t_eigvec *vec, /* The eigenvectors */
325 t_edpar *edi)
327 int i;
330 if (!vec->neig)
332 return;
335 /* Subtract average positions */
336 for (i = 0; i < edi->sav.nr; i++)
338 rvec_dec(x[i], edi->sav.x[i]);
341 for (i = 0; i < vec->neig; i++)
343 vec->xproj[i] = projectx(edi, x, vec->vec[i]);
346 /* Add average positions */
347 for (i = 0; i < edi->sav.nr; i++)
349 rvec_inc(x[i], edi->sav.x[i]);
354 /* Project vector x onto all edi->vecs (mon, linfix,...) */
355 static void project(rvec *x, /* positions to project */
356 t_edpar *edi) /* edi data set */
358 /* It is not more work to subtract the average position in every
359 * subroutine again, because these routines are rarely used simultaneously */
360 project_to_eigvectors(x, &edi->vecs.mon, edi);
361 project_to_eigvectors(x, &edi->vecs.linfix, edi);
362 project_to_eigvectors(x, &edi->vecs.linacc, edi);
363 project_to_eigvectors(x, &edi->vecs.radfix, edi);
364 project_to_eigvectors(x, &edi->vecs.radacc, edi);
365 project_to_eigvectors(x, &edi->vecs.radcon, edi);
369 static real calc_radius(t_eigvec *vec)
371 int i;
372 real rad = 0.0;
375 for (i = 0; i < vec->neig; i++)
377 rad += pow((vec->refproj[i]-vec->xproj[i]), 2);
380 return rad = sqrt(rad);
384 /* Debug helper */
385 #ifdef DEBUGHELPERS
386 static void dump_xcoll(t_edpar *edi, struct t_do_edsam *buf, t_commrec *cr,
387 int step)
389 int i;
390 FILE *fp;
391 char fn[STRLEN];
392 rvec *xcoll;
393 ivec *shifts, *eshifts;
396 if (!MASTER(cr))
398 return;
401 xcoll = buf->xcoll;
402 shifts = buf->shifts_xcoll;
403 eshifts = buf->extra_shifts_xcoll;
405 sprintf(fn, "xcolldump_step%d.txt", step);
406 fp = fopen(fn, "w");
408 for (i = 0; i < edi->sav.nr; i++)
410 fprintf(fp, "%d %9.5f %9.5f %9.5f %d %d %d %d %d %d\n",
411 edi->sav.anrs[i]+1,
412 xcoll[i][XX], xcoll[i][YY], xcoll[i][ZZ],
413 shifts[i][XX], shifts[i][YY], shifts[i][ZZ],
414 eshifts[i][XX], eshifts[i][YY], eshifts[i][ZZ]);
417 fclose(fp);
421 /* Debug helper */
422 static void dump_edi_positions(FILE *out, struct gmx_edx *s, const char name[])
424 int i;
427 fprintf(out, "#%s positions:\n%d\n", name, s->nr);
428 if (s->nr == 0)
430 return;
433 fprintf(out, "#index, x, y, z");
434 if (s->sqrtm)
436 fprintf(out, ", sqrt(m)");
438 for (i = 0; i < s->nr; i++)
440 fprintf(out, "\n%6d %11.6f %11.6f %11.6f", s->anrs[i], s->x[i][XX], s->x[i][YY], s->x[i][ZZ]);
441 if (s->sqrtm)
443 fprintf(out, "%9.3f", s->sqrtm[i]);
446 fprintf(out, "\n");
450 /* Debug helper */
451 static void dump_edi_eigenvecs(FILE *out, t_eigvec *ev,
452 const char name[], int length)
454 int i, j;
457 fprintf(out, "#%s eigenvectors:\n%d\n", name, ev->neig);
458 /* Dump the data for every eigenvector: */
459 for (i = 0; i < ev->neig; i++)
461 fprintf(out, "EV %4d\ncomponents %d\nstepsize %f\nxproj %f\nfproj %f\nrefproj %f\nradius %f\nComponents:\n",
462 ev->ieig[i], length, ev->stpsz[i], ev->xproj[i], ev->fproj[i], ev->refproj[i], ev->radius);
463 for (j = 0; j < length; j++)
465 fprintf(out, "%11.6f %11.6f %11.6f\n", ev->vec[i][j][XX], ev->vec[i][j][YY], ev->vec[i][j][ZZ]);
471 /* Debug helper */
472 static void dump_edi(t_edpar *edpars, t_commrec *cr, int nr_edi)
474 FILE *out;
475 char fn[STRLEN];
478 sprintf(fn, "EDdump_rank%d_edi%d", cr->nodeid, nr_edi);
479 out = gmx_ffopen(fn, "w");
481 fprintf(out, "#NINI\n %d\n#FITMAS\n %d\n#ANALYSIS_MAS\n %d\n",
482 edpars->nini, edpars->fitmas, edpars->pcamas);
483 fprintf(out, "#OUTFRQ\n %d\n#MAXLEN\n %d\n#SLOPECRIT\n %f\n",
484 edpars->outfrq, edpars->maxedsteps, edpars->slope);
485 fprintf(out, "#PRESTEPS\n %d\n#DELTA_F0\n %f\n#TAU\n %f\n#EFL_NULL\n %f\n#ALPHA2\n %f\n",
486 edpars->presteps, edpars->flood.deltaF0, edpars->flood.tau,
487 edpars->flood.constEfl, edpars->flood.alpha2);
489 /* Dump reference, average, target, origin positions */
490 dump_edi_positions(out, &edpars->sref, "REFERENCE");
491 dump_edi_positions(out, &edpars->sav, "AVERAGE" );
492 dump_edi_positions(out, &edpars->star, "TARGET" );
493 dump_edi_positions(out, &edpars->sori, "ORIGIN" );
495 /* Dump eigenvectors */
496 dump_edi_eigenvecs(out, &edpars->vecs.mon, "MONITORED", edpars->sav.nr);
497 dump_edi_eigenvecs(out, &edpars->vecs.linfix, "LINFIX", edpars->sav.nr);
498 dump_edi_eigenvecs(out, &edpars->vecs.linacc, "LINACC", edpars->sav.nr);
499 dump_edi_eigenvecs(out, &edpars->vecs.radfix, "RADFIX", edpars->sav.nr);
500 dump_edi_eigenvecs(out, &edpars->vecs.radacc, "RADACC", edpars->sav.nr);
501 dump_edi_eigenvecs(out, &edpars->vecs.radcon, "RADCON", edpars->sav.nr);
503 /* Dump flooding eigenvectors */
504 dump_edi_eigenvecs(out, &edpars->flood.vecs, "FLOODING", edpars->sav.nr);
506 /* Dump ed local buffer */
507 fprintf(out, "buf->do_edfit =%p\n", (void*)edpars->buf->do_edfit );
508 fprintf(out, "buf->do_edsam =%p\n", (void*)edpars->buf->do_edsam );
509 fprintf(out, "buf->do_radcon =%p\n", (void*)edpars->buf->do_radcon );
511 gmx_ffclose(out);
515 /* Debug helper */
516 static void dump_rotmat(FILE* out, matrix rotmat)
518 fprintf(out, "ROTMAT: %12.8f %12.8f %12.8f\n", rotmat[XX][XX], rotmat[XX][YY], rotmat[XX][ZZ]);
519 fprintf(out, "ROTMAT: %12.8f %12.8f %12.8f\n", rotmat[YY][XX], rotmat[YY][YY], rotmat[YY][ZZ]);
520 fprintf(out, "ROTMAT: %12.8f %12.8f %12.8f\n", rotmat[ZZ][XX], rotmat[ZZ][YY], rotmat[ZZ][ZZ]);
524 /* Debug helper */
525 static void dump_rvec(FILE *out, int dim, rvec *x)
527 int i;
530 for (i = 0; i < dim; i++)
532 fprintf(out, "%4d %f %f %f\n", i, x[i][XX], x[i][YY], x[i][ZZ]);
537 /* Debug helper */
538 static void dump_mat(FILE* out, int dim, double** mat)
540 int i, j;
543 fprintf(out, "MATRIX:\n");
544 for (i = 0; i < dim; i++)
546 for (j = 0; j < dim; j++)
548 fprintf(out, "%f ", mat[i][j]);
550 fprintf(out, "\n");
553 #endif
556 struct t_do_edfit {
557 double **omega;
558 double **om;
561 static void do_edfit(int natoms, rvec *xp, rvec *x, matrix R, t_edpar *edi)
563 /* this is a copy of do_fit with some modifications */
564 int c, r, n, j, i, irot;
565 double d[6], xnr, xpc;
566 matrix vh, vk, u;
567 int index;
568 real max_d;
570 struct t_do_edfit *loc;
571 gmx_bool bFirst;
573 if (edi->buf->do_edfit != NULL)
575 bFirst = FALSE;
577 else
579 bFirst = TRUE;
580 snew(edi->buf->do_edfit, 1);
582 loc = edi->buf->do_edfit;
584 if (bFirst)
586 snew(loc->omega, 2*DIM);
587 snew(loc->om, 2*DIM);
588 for (i = 0; i < 2*DIM; i++)
590 snew(loc->omega[i], 2*DIM);
591 snew(loc->om[i], 2*DIM);
595 for (i = 0; (i < 6); i++)
597 d[i] = 0;
598 for (j = 0; (j < 6); j++)
600 loc->omega[i][j] = 0;
601 loc->om[i][j] = 0;
605 /* calculate the matrix U */
606 clear_mat(u);
607 for (n = 0; (n < natoms); n++)
609 for (c = 0; (c < DIM); c++)
611 xpc = xp[n][c];
612 for (r = 0; (r < DIM); r++)
614 xnr = x[n][r];
615 u[c][r] += xnr*xpc;
620 /* construct loc->omega */
621 /* loc->omega is symmetric -> loc->omega==loc->omega' */
622 for (r = 0; (r < 6); r++)
624 for (c = 0; (c <= r); c++)
626 if ((r >= 3) && (c < 3))
628 loc->omega[r][c] = u[r-3][c];
629 loc->omega[c][r] = u[r-3][c];
631 else
633 loc->omega[r][c] = 0;
634 loc->omega[c][r] = 0;
639 /* determine h and k */
640 #ifdef DEBUG
642 int i;
643 dump_mat(stderr, 2*DIM, loc->omega);
644 for (i = 0; i < 6; i++)
646 fprintf(stderr, "d[%d] = %f\n", i, d[i]);
649 #endif
650 jacobi(loc->omega, 6, d, loc->om, &irot);
652 if (irot == 0)
654 fprintf(stderr, "IROT=0\n");
657 index = 0; /* For the compiler only */
659 for (j = 0; (j < 3); j++)
661 max_d = -1000;
662 for (i = 0; (i < 6); i++)
664 if (d[i] > max_d)
666 max_d = d[i];
667 index = i;
670 d[index] = -10000;
671 for (i = 0; (i < 3); i++)
673 vh[j][i] = M_SQRT2*loc->om[i][index];
674 vk[j][i] = M_SQRT2*loc->om[i+DIM][index];
678 /* determine R */
679 for (c = 0; (c < 3); c++)
681 for (r = 0; (r < 3); r++)
683 R[c][r] = vk[0][r]*vh[0][c]+
684 vk[1][r]*vh[1][c]+
685 vk[2][r]*vh[2][c];
688 if (det(R) < 0)
690 for (c = 0; (c < 3); c++)
692 for (r = 0; (r < 3); r++)
694 R[c][r] = vk[0][r]*vh[0][c]+
695 vk[1][r]*vh[1][c]-
696 vk[2][r]*vh[2][c];
703 static void rmfit(int nat, rvec *xcoll, rvec transvec, matrix rotmat)
705 rvec vec;
706 matrix tmat;
709 /* Remove rotation.
710 * The inverse rotation is described by the transposed rotation matrix */
711 transpose(rotmat, tmat);
712 rotate_x(xcoll, nat, tmat);
714 /* Remove translation */
715 vec[XX] = -transvec[XX];
716 vec[YY] = -transvec[YY];
717 vec[ZZ] = -transvec[ZZ];
718 translate_x(xcoll, nat, vec);
722 /**********************************************************************************
723 ******************** FLOODING ****************************************************
724 **********************************************************************************
726 The flooding ability was added later to edsam. Many of the edsam functionality could be reused for that purpose.
727 The flooding covariance matrix, i.e. the selected eigenvectors and their corresponding eigenvalues are
728 read as 7th Component Group. The eigenvalues are coded into the stepsize parameter (as used by -linfix or -linacc).
730 do_md clls right in the beginning the function init_edsam, which reads the edi file, saves all the necessary information in
731 the edi structure and calls init_flood, to initialise some extra fields in the edi->flood structure.
733 since the flooding acts on forces do_flood is called from the function force() (force.c), while the other
734 edsam functionality is hooked into md via the update() (update.c) function acting as constraint on positions.
736 do_flood makes a copy of the positions,
737 fits them, projects them computes flooding_energy, and flooding forces. The forces are computed in the
738 space of the eigenvectors and are then blown up to the full cartesian space and rotated back to remove the
739 fit. Then do_flood adds these forces to the forcefield-forces
740 (given as parameter) and updates the adaptive flooding parameters Efl and deltaF.
742 To center the flooding potential at a different location one can use the -ori option in make_edi. The ori
743 structure is projected to the system of eigenvectors and then this position in the subspace is used as
744 center of the flooding potential. If the option is not used, the center will be zero in the subspace,
745 i.e. the average structure as given in the make_edi file.
747 To use the flooding potential as restraint, make_edi has the option -restrain, which leads to inverted
748 signs of alpha2 and Efl, such that the sign in the exponential of Vfl is not inverted but the sign of
749 Vfl is inverted. Vfl = Efl * exp (- .../Efl/alpha2*x^2...) With tau>0 the negative Efl will grow slowly
750 so that the restraint is switched off slowly. When Efl==0 and inverted flooding is ON is reached no
751 further adaption is applied, Efl will stay constant at zero.
753 To use restraints with harmonic potentials switch -restrain and -harmonic. Then the eigenvalues are
754 used as spring constants for the harmonic potential.
755 Note that eq3 in the flooding paper (J. Comp. Chem. 2006, 27, 1693-1702) defines the parameter lambda \
756 as the inverse of the spring constant, whereas the implementation uses lambda as the spring constant.
758 To use more than one flooding matrix just concatenate several .edi files (cat flood1.edi flood2.edi > flood_all.edi)
759 the routine read_edi_file reads all of theses flooding files.
760 The structure t_edi is now organized as a list of t_edis and the function do_flood cycles through the list
761 calling the do_single_flood() routine for every single entry. Since every state variables have been kept in one
762 edi there is no interdependence whatsoever. The forces are added together.
764 To write energies into the .edr file, call the function
765 get_flood_enx_names(char**, int *nnames) to get the Header (Vfl1 Vfl2... Vfln)
766 and call
767 get_flood_energies(real Vfl[],int nnames);
769 TODO:
770 - 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.
772 Maybe one should give a range of atoms for which to remove motion, so that motion is removed with
773 two edsam files from two peptide chains
776 static void write_edo_flood(t_edpar *edi, FILE *fp, real rmsd)
778 int i;
781 /* Output how well we fit to the reference structure */
782 fprintf(fp, EDcol_ffmt, rmsd);
784 for (i = 0; i < edi->flood.vecs.neig; i++)
786 fprintf(fp, EDcol_efmt, edi->flood.vecs.xproj[i]);
788 /* Check whether the reference projection changes with time (this can happen
789 * in case flooding is used as harmonic restraint). If so, output the
790 * current reference projection */
791 if (edi->flood.bHarmonic && edi->flood.vecs.refprojslope[i] != 0.0)
793 fprintf(fp, EDcol_efmt, edi->flood.vecs.refproj[i]);
796 /* Output Efl if we are doing adaptive flooding */
797 if (0 != edi->flood.tau)
799 fprintf(fp, EDcol_efmt, edi->flood.Efl);
801 fprintf(fp, EDcol_efmt, edi->flood.Vfl);
803 /* Output deltaF if we are doing adaptive flooding */
804 if (0 != edi->flood.tau)
806 fprintf(fp, EDcol_efmt, edi->flood.deltaF);
808 fprintf(fp, EDcol_efmt, edi->flood.vecs.fproj[i]);
813 /* From flood.xproj compute the Vfl(x) at this point */
814 static real flood_energy(t_edpar *edi, gmx_int64_t step)
816 /* compute flooding energy Vfl
817 Vfl = Efl * exp( - \frac {kT} {2Efl alpha^2} * sum_i { \lambda_i c_i^2 } )
818 \lambda_i is the reciprocal eigenvalue 1/\sigma_i
819 it is already computed by make_edi and stored in stpsz[i]
820 bHarmonic:
821 Vfl = - Efl * 1/2(sum _i {\frac 1{\lambda_i} c_i^2})
823 real sum;
824 real Vfl;
825 int i;
828 /* Each time this routine is called (i.e. each time step), we add a small
829 * value to the reference projection. This way a harmonic restraint towards
830 * a moving reference is realized. If no value for the additive constant
831 * is provided in the edi file, the reference will not change. */
832 if (edi->flood.bHarmonic)
834 for (i = 0; i < edi->flood.vecs.neig; i++)
836 edi->flood.vecs.refproj[i] = edi->flood.vecs.refproj0[i] + step * edi->flood.vecs.refprojslope[i];
840 sum = 0.0;
841 /* Compute sum which will be the exponent of the exponential */
842 for (i = 0; i < edi->flood.vecs.neig; i++)
844 /* stpsz stores the reciprocal eigenvalue 1/sigma_i */
845 sum += edi->flood.vecs.stpsz[i]*(edi->flood.vecs.xproj[i]-edi->flood.vecs.refproj[i])*(edi->flood.vecs.xproj[i]-edi->flood.vecs.refproj[i]);
848 /* Compute the Gauss function*/
849 if (edi->flood.bHarmonic)
851 Vfl = -0.5*edi->flood.Efl*sum; /* minus sign because Efl is negative, if restrain is on. */
853 else
855 Vfl = edi->flood.Efl != 0 ? edi->flood.Efl*exp(-edi->flood.kT/2/edi->flood.Efl/edi->flood.alpha2*sum) : 0;
858 return Vfl;
862 /* From the position and from Vfl compute forces in subspace -> store in edi->vec.flood.fproj */
863 static void flood_forces(t_edpar *edi)
865 /* compute the forces in the subspace of the flooding eigenvectors
866 * by the formula F_i= V_{fl}(c) * ( \frac {kT} {E_{fl}} \lambda_i c_i */
868 int i;
869 real energy = edi->flood.Vfl;
872 if (edi->flood.bHarmonic)
874 for (i = 0; i < edi->flood.vecs.neig; i++)
876 edi->flood.vecs.fproj[i] = edi->flood.Efl* edi->flood.vecs.stpsz[i]*(edi->flood.vecs.xproj[i]-edi->flood.vecs.refproj[i]);
879 else
881 for (i = 0; i < edi->flood.vecs.neig; i++)
883 /* if Efl is zero the forces are zero if not use the formula */
884 edi->flood.vecs.fproj[i] = edi->flood.Efl != 0 ? edi->flood.kT/edi->flood.Efl/edi->flood.alpha2*energy*edi->flood.vecs.stpsz[i]*(edi->flood.vecs.xproj[i]-edi->flood.vecs.refproj[i]) : 0;
890 /* Raise forces from subspace into cartesian space */
891 static void flood_blowup(t_edpar *edi, rvec *forces_cart)
893 /* this function lifts the forces from the subspace to the cartesian space
894 all the values not contained in the subspace are assumed to be zero and then
895 a coordinate transformation from eigenvector to cartesian vectors is performed
896 The nonexistent values don't have to be set to zero explicitly, they would occur
897 as zero valued summands, hence we just stop to compute this part of the sum.
899 for every atom we add all the contributions to this atom from all the different eigenvectors.
901 NOTE: one could add directly to the forcefield forces, would mean we wouldn't have to clear the
902 field forces_cart prior the computation, but we compute the forces separately
903 to have them accessible for diagnostics
905 int j, eig;
906 rvec dum;
907 real *forces_sub;
910 forces_sub = edi->flood.vecs.fproj;
913 /* Calculate the cartesian forces for the local atoms */
915 /* Clear forces first */
916 for (j = 0; j < edi->sav.nr_loc; j++)
918 clear_rvec(forces_cart[j]);
921 /* Now compute atomwise */
922 for (j = 0; j < edi->sav.nr_loc; j++)
924 /* Compute forces_cart[edi->sav.anrs[j]] */
925 for (eig = 0; eig < edi->flood.vecs.neig; eig++)
927 /* Force vector is force * eigenvector (compute only atom j) */
928 svmul(forces_sub[eig], edi->flood.vecs.vec[eig][edi->sav.c_ind[j]], dum);
929 /* Add this vector to the cartesian forces */
930 rvec_inc(forces_cart[j], dum);
936 /* Update the values of Efl, deltaF depending on tau and Vfl */
937 static void update_adaption(t_edpar *edi)
939 /* this function updates the parameter Efl and deltaF according to the rules given in
940 * 'predicting unimolecular chemical reactions: chemical flooding' M Mueller et al,
941 * J. chem Phys. */
943 if ((edi->flood.tau < 0 ? -edi->flood.tau : edi->flood.tau ) > 0.00000001)
945 edi->flood.Efl = edi->flood.Efl+edi->flood.dt/edi->flood.tau*(edi->flood.deltaF0-edi->flood.deltaF);
946 /* check if restrain (inverted flooding) -> don't let EFL become positive */
947 if (edi->flood.alpha2 < 0 && edi->flood.Efl > -0.00000001)
949 edi->flood.Efl = 0;
952 edi->flood.deltaF = (1-edi->flood.dt/edi->flood.tau)*edi->flood.deltaF+edi->flood.dt/edi->flood.tau*edi->flood.Vfl;
957 static void do_single_flood(
958 FILE *edo,
959 rvec x[],
960 rvec force[],
961 t_edpar *edi,
962 gmx_int64_t step,
963 matrix box,
964 t_commrec *cr,
965 gmx_bool bNS) /* Are we in a neighbor searching step? */
967 int i;
968 matrix rotmat; /* rotation matrix */
969 matrix tmat; /* inverse rotation */
970 rvec transvec; /* translation vector */
971 real rmsdev;
972 struct t_do_edsam *buf;
975 buf = edi->buf->do_edsam;
978 /* Broadcast the positions of the AVERAGE structure such that they are known on
979 * every processor. Each node contributes its local positions x and stores them in
980 * the collective ED array buf->xcoll */
981 communicate_group_positions(cr, buf->xcoll, buf->shifts_xcoll, buf->extra_shifts_xcoll, bNS, x,
982 edi->sav.nr, edi->sav.nr_loc, edi->sav.anrs_loc, edi->sav.c_ind, edi->sav.x_old, box);
984 /* Only assembly REFERENCE positions if their indices differ from the average ones */
985 if (!edi->bRefEqAv)
987 communicate_group_positions(cr, buf->xc_ref, buf->shifts_xc_ref, buf->extra_shifts_xc_ref, bNS, x,
988 edi->sref.nr, edi->sref.nr_loc, edi->sref.anrs_loc, edi->sref.c_ind, edi->sref.x_old, box);
991 /* If bUpdateShifts was TRUE, the shifts have just been updated in get_positions.
992 * We do not need to update the shifts until the next NS step */
993 buf->bUpdateShifts = FALSE;
995 /* Now all nodes have all of the ED/flooding positions in edi->sav->xcoll,
996 * as well as the indices in edi->sav.anrs */
998 /* Fit the reference indices to the reference structure */
999 if (edi->bRefEqAv)
1001 fit_to_reference(buf->xcoll, transvec, rotmat, edi);
1003 else
1005 fit_to_reference(buf->xc_ref, transvec, rotmat, edi);
1008 /* Now apply the translation and rotation to the ED structure */
1009 translate_and_rotate(buf->xcoll, edi->sav.nr, transvec, rotmat);
1011 /* Project fitted structure onto supbspace -> store in edi->flood.vecs.xproj */
1012 project_to_eigvectors(buf->xcoll, &edi->flood.vecs, edi);
1014 if (FALSE == edi->flood.bConstForce)
1016 /* Compute Vfl(x) from flood.xproj */
1017 edi->flood.Vfl = flood_energy(edi, step);
1019 update_adaption(edi);
1021 /* Compute the flooding forces */
1022 flood_forces(edi);
1025 /* Translate them into cartesian positions */
1026 flood_blowup(edi, edi->flood.forces_cartesian);
1028 /* Rotate forces back so that they correspond to the given structure and not to the fitted one */
1029 /* Each node rotates back its local forces */
1030 transpose(rotmat, tmat);
1031 rotate_x(edi->flood.forces_cartesian, edi->sav.nr_loc, tmat);
1033 /* Finally add forces to the main force variable */
1034 for (i = 0; i < edi->sav.nr_loc; i++)
1036 rvec_inc(force[edi->sav.anrs_loc[i]], edi->flood.forces_cartesian[i]);
1039 /* Output is written by the master process */
1040 if (do_per_step(step, edi->outfrq) && MASTER(cr))
1042 /* Output how well we fit to the reference */
1043 if (edi->bRefEqAv)
1045 /* Indices of reference and average structures are identical,
1046 * thus we can calculate the rmsd to SREF using xcoll */
1047 rmsdev = rmsd_from_structure(buf->xcoll, &edi->sref);
1049 else
1051 /* We have to translate & rotate the reference atoms first */
1052 translate_and_rotate(buf->xc_ref, edi->sref.nr, transvec, rotmat);
1053 rmsdev = rmsd_from_structure(buf->xc_ref, &edi->sref);
1056 write_edo_flood(edi, edo, rmsdev);
1061 /* Main flooding routine, called from do_force */
1062 extern void do_flood(t_commrec *cr,
1063 const t_inputrec *ir,
1064 rvec x[],
1065 rvec force[],
1066 gmx_edsam_t ed,
1067 matrix box,
1068 gmx_int64_t step,
1069 gmx_bool bNS)
1071 t_edpar *edi;
1074 edi = ed->edpar;
1076 /* Write time to edo, when required. Output the time anyhow since we need
1077 * it in the output file for ED constraints. */
1078 if (MASTER(cr) && do_per_step(step, edi->outfrq))
1080 fprintf(ed->edo, "\n%12f", ir->init_t + step*ir->delta_t);
1083 if (ed->eEDtype != eEDflood)
1085 return;
1088 while (edi)
1090 /* Call flooding for one matrix */
1091 if (edi->flood.vecs.neig)
1093 do_single_flood(ed->edo, x, force, edi, step, box, cr, bNS);
1095 edi = edi->next_edi;
1100 /* Called by init_edi, configure some flooding related variables and structures,
1101 * print headers to output files */
1102 static void init_flood(t_edpar *edi, gmx_edsam_t ed, real dt)
1104 int i;
1107 edi->flood.Efl = edi->flood.constEfl;
1108 edi->flood.Vfl = 0;
1109 edi->flood.dt = dt;
1111 if (edi->flood.vecs.neig)
1113 /* If in any of the ED groups we find a flooding vector, flooding is turned on */
1114 ed->eEDtype = eEDflood;
1116 fprintf(stderr, "ED: Flooding %d eigenvector%s.\n", edi->flood.vecs.neig, edi->flood.vecs.neig > 1 ? "s" : "");
1118 if (edi->flood.bConstForce)
1120 /* We have used stpsz as a vehicle to carry the fproj values for constant
1121 * force flooding. Now we copy that to flood.vecs.fproj. Note that
1122 * in const force flooding, fproj is never changed. */
1123 for (i = 0; i < edi->flood.vecs.neig; i++)
1125 edi->flood.vecs.fproj[i] = edi->flood.vecs.stpsz[i];
1127 fprintf(stderr, "ED: applying on eigenvector %d a constant force of %g\n",
1128 edi->flood.vecs.ieig[i], edi->flood.vecs.fproj[i]);
1135 #ifdef DEBUGHELPERS
1136 /*********** Energy book keeping ******/
1137 static void get_flood_enx_names(t_edpar *edi, char** names, int *nnames) /* get header of energies */
1139 t_edpar *actual;
1140 int count;
1141 char buf[STRLEN];
1142 actual = edi;
1143 count = 1;
1144 while (actual)
1146 srenew(names, count);
1147 sprintf(buf, "Vfl_%d", count);
1148 names[count-1] = gmx_strdup(buf);
1149 actual = actual->next_edi;
1150 count++;
1152 *nnames = count-1;
1156 static void get_flood_energies(t_edpar *edi, real Vfl[], int nnames)
1158 /*fl has to be big enough to capture nnames-many entries*/
1159 t_edpar *actual;
1160 int count;
1163 actual = edi;
1164 count = 1;
1165 while (actual)
1167 Vfl[count-1] = actual->flood.Vfl;
1168 actual = actual->next_edi;
1169 count++;
1171 if (nnames != count-1)
1173 gmx_fatal(FARGS, "Number of energies is not consistent with t_edi structure");
1176 /************* END of FLOODING IMPLEMENTATION ****************************/
1177 #endif
1180 gmx_edsam_t ed_open(int natoms, edsamstate_t *EDstate, int nfile, const t_filenm fnm[], unsigned long Flags, const output_env_t oenv, t_commrec *cr)
1182 gmx_edsam_t ed;
1183 int nED;
1186 /* Allocate space for the ED data structure */
1187 snew(ed, 1);
1189 /* We want to perform ED (this switch might later be upgraded to eEDflood) */
1190 ed->eEDtype = eEDedsam;
1192 if (MASTER(cr))
1194 fprintf(stderr, "ED sampling will be performed!\n");
1195 snew(ed->edpar, 1);
1197 /* Read the edi input file: */
1198 nED = read_edi_file(ftp2fn(efEDI, nfile, fnm), ed->edpar, natoms);
1200 /* Make sure the checkpoint was produced in a run using this .edi file */
1201 if (EDstate->bFromCpt)
1203 crosscheck_edi_file_vs_checkpoint(ed, EDstate);
1205 else
1207 EDstate->nED = nED;
1209 init_edsamstate(ed, EDstate);
1211 /* The master opens the ED output file */
1212 /* TODO This file is never closed... */
1213 if (Flags & MD_APPENDFILES)
1215 ed->edo = gmx_fio_fopen(opt2fn("-eo", nfile, fnm), "a+");
1217 else
1219 ed->edo = xvgropen(opt2fn("-eo", nfile, fnm),
1220 "Essential dynamics / flooding output",
1221 "Time (ps)",
1222 "RMSDs (nm), projections on EVs (nm), ...", oenv);
1224 /* Make a descriptive legend */
1225 write_edo_legend(ed, EDstate->nED, oenv);
1228 return ed;
1232 /* Broadcasts the structure data */
1233 static void bc_ed_positions(t_commrec *cr, struct gmx_edx *s, int stype)
1235 snew_bc(cr, s->anrs, s->nr ); /* Index numbers */
1236 snew_bc(cr, s->x, s->nr ); /* Positions */
1237 nblock_bc(cr, s->nr, s->anrs );
1238 nblock_bc(cr, s->nr, s->x );
1240 /* For the average & reference structures we need an array for the collective indices,
1241 * and we need to broadcast the masses as well */
1242 if (stype == eedAV || stype == eedREF)
1244 /* We need these additional variables in the parallel case: */
1245 snew(s->c_ind, s->nr ); /* Collective indices */
1246 /* Local atom indices get assigned in dd_make_local_group_indices.
1247 * There, also memory is allocated */
1248 s->nalloc_loc = 0; /* allocation size of s->anrs_loc */
1249 snew_bc(cr, s->x_old, s->nr); /* To be able to always make the ED molecule whole, ... */
1250 nblock_bc(cr, s->nr, s->x_old); /* ... keep track of shift changes with the help of old coords */
1253 /* broadcast masses for the reference structure (for mass-weighted fitting) */
1254 if (stype == eedREF)
1256 snew_bc(cr, s->m, s->nr);
1257 nblock_bc(cr, s->nr, s->m);
1260 /* For the average structure we might need the masses for mass-weighting */
1261 if (stype == eedAV)
1263 snew_bc(cr, s->sqrtm, s->nr);
1264 nblock_bc(cr, s->nr, s->sqrtm);
1265 snew_bc(cr, s->m, s->nr);
1266 nblock_bc(cr, s->nr, s->m);
1271 /* Broadcasts the eigenvector data */
1272 static void bc_ed_vecs(t_commrec *cr, t_eigvec *ev, int length, gmx_bool bHarmonic)
1274 int i;
1276 snew_bc(cr, ev->ieig, ev->neig); /* index numbers of eigenvector */
1277 snew_bc(cr, ev->stpsz, ev->neig); /* stepsizes per eigenvector */
1278 snew_bc(cr, ev->xproj, ev->neig); /* instantaneous x projection */
1279 snew_bc(cr, ev->fproj, ev->neig); /* instantaneous f projection */
1280 snew_bc(cr, ev->refproj, ev->neig); /* starting or target projection */
1282 nblock_bc(cr, ev->neig, ev->ieig );
1283 nblock_bc(cr, ev->neig, ev->stpsz );
1284 nblock_bc(cr, ev->neig, ev->xproj );
1285 nblock_bc(cr, ev->neig, ev->fproj );
1286 nblock_bc(cr, ev->neig, ev->refproj);
1288 snew_bc(cr, ev->vec, ev->neig); /* Eigenvector components */
1289 for (i = 0; i < ev->neig; i++)
1291 snew_bc(cr, ev->vec[i], length);
1292 nblock_bc(cr, length, ev->vec[i]);
1295 /* For harmonic restraints the reference projections can change with time */
1296 if (bHarmonic)
1298 snew_bc(cr, ev->refproj0, ev->neig);
1299 snew_bc(cr, ev->refprojslope, ev->neig);
1300 nblock_bc(cr, ev->neig, ev->refproj0 );
1301 nblock_bc(cr, ev->neig, ev->refprojslope);
1306 /* Broadcasts the ED / flooding data to other nodes
1307 * and allocates memory where needed */
1308 static void broadcast_ed_data(t_commrec *cr, gmx_edsam_t ed, int numedis)
1310 int nr;
1311 t_edpar *edi;
1314 /* Master lets the other nodes know if its ED only or also flooding */
1315 gmx_bcast(sizeof(ed->eEDtype), &(ed->eEDtype), cr);
1317 snew_bc(cr, ed->edpar, 1);
1318 /* Now transfer the ED data set(s) */
1319 edi = ed->edpar;
1320 for (nr = 0; nr < numedis; nr++)
1322 /* Broadcast a single ED data set */
1323 block_bc(cr, *edi);
1325 /* Broadcast positions */
1326 bc_ed_positions(cr, &(edi->sref), eedREF); /* reference positions (don't broadcast masses) */
1327 bc_ed_positions(cr, &(edi->sav ), eedAV ); /* average positions (do broadcast masses as well) */
1328 bc_ed_positions(cr, &(edi->star), eedTAR); /* target positions */
1329 bc_ed_positions(cr, &(edi->sori), eedORI); /* origin positions */
1331 /* Broadcast eigenvectors */
1332 bc_ed_vecs(cr, &edi->vecs.mon, edi->sav.nr, FALSE);
1333 bc_ed_vecs(cr, &edi->vecs.linfix, edi->sav.nr, FALSE);
1334 bc_ed_vecs(cr, &edi->vecs.linacc, edi->sav.nr, FALSE);
1335 bc_ed_vecs(cr, &edi->vecs.radfix, edi->sav.nr, FALSE);
1336 bc_ed_vecs(cr, &edi->vecs.radacc, edi->sav.nr, FALSE);
1337 bc_ed_vecs(cr, &edi->vecs.radcon, edi->sav.nr, FALSE);
1338 /* Broadcast flooding eigenvectors and, if needed, values for the moving reference */
1339 bc_ed_vecs(cr, &edi->flood.vecs, edi->sav.nr, edi->flood.bHarmonic);
1341 /* Set the pointer to the next ED group */
1342 if (edi->next_edi)
1344 snew_bc(cr, edi->next_edi, 1);
1345 edi = edi->next_edi;
1351 /* init-routine called for every *.edi-cycle, initialises t_edpar structure */
1352 static void init_edi(const gmx_mtop_t *mtop, t_edpar *edi)
1354 int i;
1355 real totalmass = 0.0;
1356 rvec com;
1357 gmx_mtop_atomlookup_t alook = NULL;
1358 t_atom *atom;
1360 /* NOTE Init_edi is executed on the master process only
1361 * The initialized data sets are then transmitted to the
1362 * other nodes in broadcast_ed_data */
1364 alook = gmx_mtop_atomlookup_init(mtop);
1366 /* evaluate masses (reference structure) */
1367 snew(edi->sref.m, edi->sref.nr);
1368 for (i = 0; i < edi->sref.nr; i++)
1370 if (edi->fitmas)
1372 gmx_mtop_atomnr_to_atom(alook, edi->sref.anrs[i], &atom);
1373 edi->sref.m[i] = atom->m;
1375 else
1377 edi->sref.m[i] = 1.0;
1380 /* Check that every m > 0. Bad things will happen otherwise. */
1381 if (edi->sref.m[i] <= 0.0)
1383 gmx_fatal(FARGS, "Reference structure atom %d (sam.edi index %d) has a mass of %g.\n"
1384 "For a mass-weighted fit, all reference structure atoms need to have a mass >0.\n"
1385 "Either make the covariance analysis non-mass-weighted, or exclude massless\n"
1386 "atoms from the reference structure by creating a proper index group.\n",
1387 i, edi->sref.anrs[i]+1, edi->sref.m[i]);
1390 totalmass += edi->sref.m[i];
1392 edi->sref.mtot = totalmass;
1394 /* Masses m and sqrt(m) for the average structure. Note that m
1395 * is needed if forces have to be evaluated in do_edsam */
1396 snew(edi->sav.sqrtm, edi->sav.nr );
1397 snew(edi->sav.m, edi->sav.nr );
1398 for (i = 0; i < edi->sav.nr; i++)
1400 gmx_mtop_atomnr_to_atom(alook, edi->sav.anrs[i], &atom);
1401 edi->sav.m[i] = atom->m;
1402 if (edi->pcamas)
1404 edi->sav.sqrtm[i] = sqrt(atom->m);
1406 else
1408 edi->sav.sqrtm[i] = 1.0;
1411 /* Check that every m > 0. Bad things will happen otherwise. */
1412 if (edi->sav.sqrtm[i] <= 0.0)
1414 gmx_fatal(FARGS, "Average structure atom %d (sam.edi index %d) has a mass of %g.\n"
1415 "For ED with mass-weighting, all average structure atoms need to have a mass >0.\n"
1416 "Either make the covariance analysis non-mass-weighted, or exclude massless\n"
1417 "atoms from the average structure by creating a proper index group.\n",
1418 i, edi->sav.anrs[i]+1, atom->m);
1422 gmx_mtop_atomlookup_destroy(alook);
1424 /* put reference structure in origin */
1425 get_center(edi->sref.x, edi->sref.m, edi->sref.nr, com);
1426 com[XX] = -com[XX];
1427 com[YY] = -com[YY];
1428 com[ZZ] = -com[ZZ];
1429 translate_x(edi->sref.x, edi->sref.nr, com);
1431 /* Init ED buffer */
1432 snew(edi->buf, 1);
1436 static void check(const char *line, const char *label)
1438 if (!strstr(line, label))
1440 gmx_fatal(FARGS, "Could not find input parameter %s at expected position in edsam input-file (.edi)\nline read instead is %s", label, line);
1445 static int read_checked_edint(FILE *file, const char *label)
1447 char line[STRLEN+1];
1448 int idum;
1450 fgets2 (line, STRLEN, file);
1451 check(line, label);
1452 fgets2 (line, STRLEN, file);
1453 sscanf (line, max_ev_fmt_d, &idum);
1454 return idum;
1458 static int read_edint(FILE *file, gmx_bool *bEOF)
1460 char line[STRLEN+1];
1461 int idum;
1462 char *eof;
1465 eof = fgets2 (line, STRLEN, file);
1466 if (eof == NULL)
1468 *bEOF = TRUE;
1469 return -1;
1471 eof = fgets2 (line, STRLEN, file);
1472 if (eof == NULL)
1474 *bEOF = TRUE;
1475 return -1;
1477 sscanf (line, max_ev_fmt_d, &idum);
1478 *bEOF = FALSE;
1479 return idum;
1483 static real read_checked_edreal(FILE *file, const char *label)
1485 char line[STRLEN+1];
1486 double rdum;
1489 fgets2 (line, STRLEN, file);
1490 check(line, label);
1491 fgets2 (line, STRLEN, file);
1492 sscanf (line, max_ev_fmt_lf, &rdum);
1493 return (real) rdum; /* always read as double and convert to single */
1497 static void read_edx(FILE *file, int number, int *anrs, rvec *x)
1499 int i, j;
1500 char line[STRLEN+1];
1501 double d[3];
1504 for (i = 0; i < number; i++)
1506 fgets2 (line, STRLEN, file);
1507 sscanf (line, max_ev_fmt_dlflflf, &anrs[i], &d[0], &d[1], &d[2]);
1508 anrs[i]--; /* we are reading FORTRAN indices */
1509 for (j = 0; j < 3; j++)
1511 x[i][j] = d[j]; /* always read as double and convert to single */
1517 static void scan_edvec(FILE *in, int nr, rvec *vec)
1519 char line[STRLEN+1];
1520 int i;
1521 double x, y, z;
1524 for (i = 0; (i < nr); i++)
1526 fgets2 (line, STRLEN, in);
1527 sscanf (line, max_ev_fmt_lelele, &x, &y, &z);
1528 vec[i][XX] = x;
1529 vec[i][YY] = y;
1530 vec[i][ZZ] = z;
1535 static void read_edvec(FILE *in, int nr, t_eigvec *tvec, gmx_bool bReadRefproj, gmx_bool *bHaveReference)
1537 int i, idum, nscan;
1538 double rdum, refproj_dum = 0.0, refprojslope_dum = 0.0;
1539 char line[STRLEN+1];
1542 tvec->neig = read_checked_edint(in, "NUMBER OF EIGENVECTORS");
1543 if (tvec->neig > 0)
1545 snew(tvec->ieig, tvec->neig);
1546 snew(tvec->stpsz, tvec->neig);
1547 snew(tvec->vec, tvec->neig);
1548 snew(tvec->xproj, tvec->neig);
1549 snew(tvec->fproj, tvec->neig);
1550 snew(tvec->refproj, tvec->neig);
1551 if (bReadRefproj)
1553 snew(tvec->refproj0, tvec->neig);
1554 snew(tvec->refprojslope, tvec->neig);
1557 for (i = 0; (i < tvec->neig); i++)
1559 fgets2 (line, STRLEN, in);
1560 if (bReadRefproj) /* ONLY when using flooding as harmonic restraint */
1562 nscan = sscanf(line, max_ev_fmt_dlflflf, &idum, &rdum, &refproj_dum, &refprojslope_dum);
1563 /* Zero out values which were not scanned */
1564 switch (nscan)
1566 case 4:
1567 /* Every 4 values read, including reference position */
1568 *bHaveReference = TRUE;
1569 break;
1570 case 3:
1571 /* A reference position is provided */
1572 *bHaveReference = TRUE;
1573 /* No value for slope, set to 0 */
1574 refprojslope_dum = 0.0;
1575 break;
1576 case 2:
1577 /* No values for reference projection and slope, set to 0 */
1578 refproj_dum = 0.0;
1579 refprojslope_dum = 0.0;
1580 break;
1581 default:
1582 gmx_fatal(FARGS, "Expected 2 - 4 (not %d) values for flooding vec: <nr> <spring const> <refproj> <refproj-slope>\n", nscan);
1583 break;
1585 tvec->refproj[i] = refproj_dum;
1586 tvec->refproj0[i] = refproj_dum;
1587 tvec->refprojslope[i] = refprojslope_dum;
1589 else /* Normal flooding */
1591 nscan = sscanf(line, max_ev_fmt_dlf, &idum, &rdum);
1592 if (nscan != 2)
1594 gmx_fatal(FARGS, "Expected 2 values for flooding vec: <nr> <stpsz>\n");
1597 tvec->ieig[i] = idum;
1598 tvec->stpsz[i] = rdum;
1599 } /* end of loop over eigenvectors */
1601 for (i = 0; (i < tvec->neig); i++)
1603 snew(tvec->vec[i], nr);
1604 scan_edvec(in, nr, tvec->vec[i]);
1610 /* calls read_edvec for the vector groups, only for flooding there is an extra call */
1611 static void read_edvecs(FILE *in, int nr, t_edvecs *vecs)
1613 gmx_bool bHaveReference = FALSE;
1616 read_edvec(in, nr, &vecs->mon, FALSE, &bHaveReference);
1617 read_edvec(in, nr, &vecs->linfix, FALSE, &bHaveReference);
1618 read_edvec(in, nr, &vecs->linacc, FALSE, &bHaveReference);
1619 read_edvec(in, nr, &vecs->radfix, FALSE, &bHaveReference);
1620 read_edvec(in, nr, &vecs->radacc, FALSE, &bHaveReference);
1621 read_edvec(in, nr, &vecs->radcon, FALSE, &bHaveReference);
1625 /* Check if the same atom indices are used for reference and average positions */
1626 static gmx_bool check_if_same(struct gmx_edx sref, struct gmx_edx sav)
1628 int i;
1631 /* If the number of atoms differs between the two structures,
1632 * they cannot be identical */
1633 if (sref.nr != sav.nr)
1635 return FALSE;
1638 /* Now that we know that both stuctures have the same number of atoms,
1639 * check if also the indices are identical */
1640 for (i = 0; i < sav.nr; i++)
1642 if (sref.anrs[i] != sav.anrs[i])
1644 return FALSE;
1647 fprintf(stderr, "ED: Note: Reference and average structure are composed of the same atom indices.\n");
1649 return TRUE;
1653 static int read_edi(FILE* in, t_edpar *edi, int nr_mdatoms, const char *fn)
1655 int readmagic;
1656 const int magic = 670;
1657 gmx_bool bEOF;
1659 /* Was a specific reference point for the flooding/umbrella potential provided in the edi file? */
1660 gmx_bool bHaveReference = FALSE;
1663 /* the edi file is not free format, so expect problems if the input is corrupt. */
1665 /* check the magic number */
1666 readmagic = read_edint(in, &bEOF);
1667 /* Check whether we have reached the end of the input file */
1668 if (bEOF)
1670 return 0;
1673 if (readmagic != magic)
1675 if (readmagic == 666 || readmagic == 667 || readmagic == 668)
1677 gmx_fatal(FARGS, "Wrong magic number: Use newest version of make_edi to produce edi file");
1679 else if (readmagic != 669)
1681 gmx_fatal(FARGS, "Wrong magic number %d in %s", readmagic, fn);
1685 /* check the number of atoms */
1686 edi->nini = read_edint(in, &bEOF);
1687 if (edi->nini != nr_mdatoms)
1689 gmx_fatal(FARGS, "Nr of atoms in %s (%d) does not match nr of md atoms (%d)", fn, edi->nini, nr_mdatoms);
1692 /* Done checking. For the rest we blindly trust the input */
1693 edi->fitmas = read_checked_edint(in, "FITMAS");
1694 edi->pcamas = read_checked_edint(in, "ANALYSIS_MAS");
1695 edi->outfrq = read_checked_edint(in, "OUTFRQ");
1696 edi->maxedsteps = read_checked_edint(in, "MAXLEN");
1697 edi->slope = read_checked_edreal(in, "SLOPECRIT");
1699 edi->presteps = read_checked_edint(in, "PRESTEPS");
1700 edi->flood.deltaF0 = read_checked_edreal(in, "DELTA_F0");
1701 edi->flood.deltaF = read_checked_edreal(in, "INIT_DELTA_F");
1702 edi->flood.tau = read_checked_edreal(in, "TAU");
1703 edi->flood.constEfl = read_checked_edreal(in, "EFL_NULL");
1704 edi->flood.alpha2 = read_checked_edreal(in, "ALPHA2");
1705 edi->flood.kT = read_checked_edreal(in, "KT");
1706 edi->flood.bHarmonic = read_checked_edint(in, "HARMONIC");
1707 if (readmagic > 669)
1709 edi->flood.bConstForce = read_checked_edint(in, "CONST_FORCE_FLOODING");
1711 else
1713 edi->flood.bConstForce = FALSE;
1715 edi->sref.nr = read_checked_edint(in, "NREF");
1717 /* allocate space for reference positions and read them */
1718 snew(edi->sref.anrs, edi->sref.nr);
1719 snew(edi->sref.x, edi->sref.nr);
1720 snew(edi->sref.x_old, edi->sref.nr);
1721 edi->sref.sqrtm = NULL;
1722 read_edx(in, edi->sref.nr, edi->sref.anrs, edi->sref.x);
1724 /* average positions. they define which atoms will be used for ED sampling */
1725 edi->sav.nr = read_checked_edint(in, "NAV");
1726 snew(edi->sav.anrs, edi->sav.nr);
1727 snew(edi->sav.x, edi->sav.nr);
1728 snew(edi->sav.x_old, edi->sav.nr);
1729 read_edx(in, edi->sav.nr, edi->sav.anrs, edi->sav.x);
1731 /* Check if the same atom indices are used for reference and average positions */
1732 edi->bRefEqAv = check_if_same(edi->sref, edi->sav);
1734 /* eigenvectors */
1735 read_edvecs(in, edi->sav.nr, &edi->vecs);
1736 read_edvec(in, edi->sav.nr, &edi->flood.vecs, edi->flood.bHarmonic, &bHaveReference);
1738 /* target positions */
1739 edi->star.nr = read_edint(in, &bEOF);
1740 if (edi->star.nr > 0)
1742 snew(edi->star.anrs, edi->star.nr);
1743 snew(edi->star.x, edi->star.nr);
1744 edi->star.sqrtm = NULL;
1745 read_edx(in, edi->star.nr, edi->star.anrs, edi->star.x);
1748 /* positions defining origin of expansion circle */
1749 edi->sori.nr = read_edint(in, &bEOF);
1750 if (edi->sori.nr > 0)
1752 if (bHaveReference)
1754 /* Both an -ori structure and a at least one manual reference point have been
1755 * specified. That's ambiguous and probably not intentional. */
1756 gmx_fatal(FARGS, "ED: An origin structure has been provided and a at least one (moving) reference\n"
1757 " point was manually specified in the edi file. That is ambiguous. Aborting.\n");
1759 snew(edi->sori.anrs, edi->sori.nr);
1760 snew(edi->sori.x, edi->sori.nr);
1761 edi->sori.sqrtm = NULL;
1762 read_edx(in, edi->sori.nr, edi->sori.anrs, edi->sori.x);
1765 /* all done */
1766 return 1;
1771 /* Read in the edi input file. Note that it may contain several ED data sets which were
1772 * achieved by concatenating multiple edi files. The standard case would be a single ED
1773 * data set, though. */
1774 static int read_edi_file(const char *fn, t_edpar *edi, int nr_mdatoms)
1776 FILE *in;
1777 t_edpar *curr_edi, *last_edi;
1778 t_edpar *edi_read;
1779 int edi_nr = 0;
1782 /* This routine is executed on the master only */
1784 /* Open the .edi parameter input file */
1785 in = gmx_fio_fopen(fn, "r");
1786 fprintf(stderr, "ED: Reading edi file %s\n", fn);
1788 /* Now read a sequence of ED input parameter sets from the edi file */
1789 curr_edi = edi;
1790 last_edi = edi;
1791 while (read_edi(in, curr_edi, nr_mdatoms, fn) )
1793 edi_nr++;
1795 /* Since we arrived within this while loop we know that there is still another data set to be read in */
1796 /* We need to allocate space for the data: */
1797 snew(edi_read, 1);
1798 /* Point the 'next_edi' entry to the next edi: */
1799 curr_edi->next_edi = edi_read;
1800 /* Keep the curr_edi pointer for the case that the next group is empty: */
1801 last_edi = curr_edi;
1802 /* Let's prepare to read in the next edi data set: */
1803 curr_edi = edi_read;
1805 if (edi_nr == 0)
1807 gmx_fatal(FARGS, "No complete ED data set found in edi file %s.", fn);
1810 /* Terminate the edi group list with a NULL pointer: */
1811 last_edi->next_edi = NULL;
1813 fprintf(stderr, "ED: Found %d ED group%s.\n", edi_nr, edi_nr > 1 ? "s" : "");
1815 /* Close the .edi file again */
1816 gmx_fio_fclose(in);
1818 return edi_nr;
1822 struct t_fit_to_ref {
1823 rvec *xcopy; /* Working copy of the positions in fit_to_reference */
1826 /* Fit the current positions to the reference positions
1827 * Do not actually do the fit, just return rotation and translation.
1828 * Note that the COM of the reference structure was already put into
1829 * the origin by init_edi. */
1830 static void fit_to_reference(rvec *xcoll, /* The positions to be fitted */
1831 rvec transvec, /* The translation vector */
1832 matrix rotmat, /* The rotation matrix */
1833 t_edpar *edi) /* Just needed for do_edfit */
1835 rvec com; /* center of mass */
1836 int i;
1837 struct t_fit_to_ref *loc;
1840 /* Allocate memory the first time this routine is called for each edi group */
1841 if (NULL == edi->buf->fit_to_ref)
1843 snew(edi->buf->fit_to_ref, 1);
1844 snew(edi->buf->fit_to_ref->xcopy, edi->sref.nr);
1846 loc = edi->buf->fit_to_ref;
1848 /* We do not touch the original positions but work on a copy. */
1849 for (i = 0; i < edi->sref.nr; i++)
1851 copy_rvec(xcoll[i], loc->xcopy[i]);
1854 /* Calculate the center of mass */
1855 get_center(loc->xcopy, edi->sref.m, edi->sref.nr, com);
1857 transvec[XX] = -com[XX];
1858 transvec[YY] = -com[YY];
1859 transvec[ZZ] = -com[ZZ];
1861 /* Subtract the center of mass from the copy */
1862 translate_x(loc->xcopy, edi->sref.nr, transvec);
1864 /* Determine the rotation matrix */
1865 do_edfit(edi->sref.nr, edi->sref.x, loc->xcopy, rotmat, edi);
1869 static void translate_and_rotate(rvec *x, /* The positions to be translated and rotated */
1870 int nat, /* How many positions are there? */
1871 rvec transvec, /* The translation vector */
1872 matrix rotmat) /* The rotation matrix */
1874 /* Translation */
1875 translate_x(x, nat, transvec);
1877 /* Rotation */
1878 rotate_x(x, nat, rotmat);
1882 /* Gets the rms deviation of the positions to the structure s */
1883 /* fit_to_structure has to be called before calling this routine! */
1884 static real rmsd_from_structure(rvec *x, /* The positions under consideration */
1885 struct gmx_edx *s) /* The structure from which the rmsd shall be computed */
1887 real rmsd = 0.0;
1888 int i;
1891 for (i = 0; i < s->nr; i++)
1893 rmsd += distance2(s->x[i], x[i]);
1896 rmsd /= (real) s->nr;
1897 rmsd = sqrt(rmsd);
1899 return rmsd;
1903 void dd_make_local_ed_indices(gmx_domdec_t *dd, struct gmx_edsam *ed)
1905 t_edpar *edi;
1908 if (ed->eEDtype != eEDnone)
1910 /* Loop over ED groups */
1911 edi = ed->edpar;
1912 while (edi)
1914 /* Local atoms of the reference structure (for fitting), need only be assembled
1915 * if their indices differ from the average ones */
1916 if (!edi->bRefEqAv)
1918 dd_make_local_group_indices(dd->ga2la, edi->sref.nr, edi->sref.anrs,
1919 &edi->sref.nr_loc, &edi->sref.anrs_loc, &edi->sref.nalloc_loc, edi->sref.c_ind);
1922 /* Local atoms of the average structure (on these ED will be performed) */
1923 dd_make_local_group_indices(dd->ga2la, edi->sav.nr, edi->sav.anrs,
1924 &edi->sav.nr_loc, &edi->sav.anrs_loc, &edi->sav.nalloc_loc, edi->sav.c_ind);
1926 /* Indicate that the ED shift vectors for this structure need to be updated
1927 * at the next call to communicate_group_positions, since obviously we are in a NS step */
1928 edi->buf->do_edsam->bUpdateShifts = TRUE;
1930 /* Set the pointer to the next ED group (if any) */
1931 edi = edi->next_edi;
1937 static gmx_inline void ed_unshift_single_coord(matrix box, const rvec x, const ivec is, rvec xu)
1939 int tx, ty, tz;
1942 tx = is[XX];
1943 ty = is[YY];
1944 tz = is[ZZ];
1946 if (TRICLINIC(box))
1948 xu[XX] = x[XX]-tx*box[XX][XX]-ty*box[YY][XX]-tz*box[ZZ][XX];
1949 xu[YY] = x[YY]-ty*box[YY][YY]-tz*box[ZZ][YY];
1950 xu[ZZ] = x[ZZ]-tz*box[ZZ][ZZ];
1952 else
1954 xu[XX] = x[XX]-tx*box[XX][XX];
1955 xu[YY] = x[YY]-ty*box[YY][YY];
1956 xu[ZZ] = x[ZZ]-tz*box[ZZ][ZZ];
1961 static void do_linfix(rvec *xcoll, t_edpar *edi, gmx_int64_t step)
1963 int i, j;
1964 real proj, add;
1965 rvec vec_dum;
1968 /* loop over linfix vectors */
1969 for (i = 0; i < edi->vecs.linfix.neig; i++)
1971 /* calculate the projection */
1972 proj = projectx(edi, xcoll, edi->vecs.linfix.vec[i]);
1974 /* calculate the correction */
1975 add = edi->vecs.linfix.refproj[i] + step*edi->vecs.linfix.stpsz[i] - proj;
1977 /* apply the correction */
1978 add /= edi->sav.sqrtm[i];
1979 for (j = 0; j < edi->sav.nr; j++)
1981 svmul(add, edi->vecs.linfix.vec[i][j], vec_dum);
1982 rvec_inc(xcoll[j], vec_dum);
1988 static void do_linacc(rvec *xcoll, t_edpar *edi)
1990 int i, j;
1991 real proj, add;
1992 rvec vec_dum;
1995 /* loop over linacc vectors */
1996 for (i = 0; i < edi->vecs.linacc.neig; i++)
1998 /* calculate the projection */
1999 proj = projectx(edi, xcoll, edi->vecs.linacc.vec[i]);
2001 /* calculate the correction */
2002 add = 0.0;
2003 if (edi->vecs.linacc.stpsz[i] > 0.0)
2005 if ((proj-edi->vecs.linacc.refproj[i]) < 0.0)
2007 add = edi->vecs.linacc.refproj[i] - proj;
2010 if (edi->vecs.linacc.stpsz[i] < 0.0)
2012 if ((proj-edi->vecs.linacc.refproj[i]) > 0.0)
2014 add = edi->vecs.linacc.refproj[i] - proj;
2018 /* apply the correction */
2019 add /= edi->sav.sqrtm[i];
2020 for (j = 0; j < edi->sav.nr; j++)
2022 svmul(add, edi->vecs.linacc.vec[i][j], vec_dum);
2023 rvec_inc(xcoll[j], vec_dum);
2026 /* new positions will act as reference */
2027 edi->vecs.linacc.refproj[i] = proj + add;
2032 static void do_radfix(rvec *xcoll, t_edpar *edi)
2034 int i, j;
2035 real *proj, rad = 0.0, ratio;
2036 rvec vec_dum;
2039 if (edi->vecs.radfix.neig == 0)
2041 return;
2044 snew(proj, edi->vecs.radfix.neig);
2046 /* loop over radfix vectors */
2047 for (i = 0; i < edi->vecs.radfix.neig; i++)
2049 /* calculate the projections, radius */
2050 proj[i] = projectx(edi, xcoll, edi->vecs.radfix.vec[i]);
2051 rad += pow(proj[i] - edi->vecs.radfix.refproj[i], 2);
2054 rad = sqrt(rad);
2055 ratio = (edi->vecs.radfix.stpsz[0]+edi->vecs.radfix.radius)/rad - 1.0;
2056 edi->vecs.radfix.radius += edi->vecs.radfix.stpsz[0];
2058 /* loop over radfix vectors */
2059 for (i = 0; i < edi->vecs.radfix.neig; i++)
2061 proj[i] -= edi->vecs.radfix.refproj[i];
2063 /* apply the correction */
2064 proj[i] /= edi->sav.sqrtm[i];
2065 proj[i] *= ratio;
2066 for (j = 0; j < edi->sav.nr; j++)
2068 svmul(proj[i], edi->vecs.radfix.vec[i][j], vec_dum);
2069 rvec_inc(xcoll[j], vec_dum);
2073 sfree(proj);
2077 static void do_radacc(rvec *xcoll, t_edpar *edi)
2079 int i, j;
2080 real *proj, rad = 0.0, ratio = 0.0;
2081 rvec vec_dum;
2084 if (edi->vecs.radacc.neig == 0)
2086 return;
2089 snew(proj, edi->vecs.radacc.neig);
2091 /* loop over radacc vectors */
2092 for (i = 0; i < edi->vecs.radacc.neig; i++)
2094 /* calculate the projections, radius */
2095 proj[i] = projectx(edi, xcoll, edi->vecs.radacc.vec[i]);
2096 rad += pow(proj[i] - edi->vecs.radacc.refproj[i], 2);
2098 rad = sqrt(rad);
2100 /* only correct when radius decreased */
2101 if (rad < edi->vecs.radacc.radius)
2103 ratio = edi->vecs.radacc.radius/rad - 1.0;
2105 else
2107 edi->vecs.radacc.radius = rad;
2110 /* loop over radacc vectors */
2111 for (i = 0; i < edi->vecs.radacc.neig; i++)
2113 proj[i] -= edi->vecs.radacc.refproj[i];
2115 /* apply the correction */
2116 proj[i] /= edi->sav.sqrtm[i];
2117 proj[i] *= ratio;
2118 for (j = 0; j < edi->sav.nr; j++)
2120 svmul(proj[i], edi->vecs.radacc.vec[i][j], vec_dum);
2121 rvec_inc(xcoll[j], vec_dum);
2124 sfree(proj);
2128 struct t_do_radcon {
2129 real *proj;
2132 static void do_radcon(rvec *xcoll, t_edpar *edi)
2134 int i, j;
2135 real rad = 0.0, ratio = 0.0;
2136 struct t_do_radcon *loc;
2137 gmx_bool bFirst;
2138 rvec vec_dum;
2141 if (edi->buf->do_radcon != NULL)
2143 bFirst = FALSE;
2145 else
2147 bFirst = TRUE;
2148 snew(edi->buf->do_radcon, 1);
2150 loc = edi->buf->do_radcon;
2152 if (edi->vecs.radcon.neig == 0)
2154 return;
2157 if (bFirst)
2159 snew(loc->proj, edi->vecs.radcon.neig);
2162 /* loop over radcon vectors */
2163 for (i = 0; i < edi->vecs.radcon.neig; i++)
2165 /* calculate the projections, radius */
2166 loc->proj[i] = projectx(edi, xcoll, edi->vecs.radcon.vec[i]);
2167 rad += pow(loc->proj[i] - edi->vecs.radcon.refproj[i], 2);
2169 rad = sqrt(rad);
2170 /* only correct when radius increased */
2171 if (rad > edi->vecs.radcon.radius)
2173 ratio = edi->vecs.radcon.radius/rad - 1.0;
2175 /* loop over radcon vectors */
2176 for (i = 0; i < edi->vecs.radcon.neig; i++)
2178 /* apply the correction */
2179 loc->proj[i] -= edi->vecs.radcon.refproj[i];
2180 loc->proj[i] /= edi->sav.sqrtm[i];
2181 loc->proj[i] *= ratio;
2183 for (j = 0; j < edi->sav.nr; j++)
2185 svmul(loc->proj[i], edi->vecs.radcon.vec[i][j], vec_dum);
2186 rvec_inc(xcoll[j], vec_dum);
2191 else
2193 edi->vecs.radcon.radius = rad;
2199 static void ed_apply_constraints(rvec *xcoll, t_edpar *edi, gmx_int64_t step)
2201 int i;
2204 /* subtract the average positions */
2205 for (i = 0; i < edi->sav.nr; i++)
2207 rvec_dec(xcoll[i], edi->sav.x[i]);
2210 /* apply the constraints */
2211 if (step >= 0)
2213 do_linfix(xcoll, edi, step);
2215 do_linacc(xcoll, edi);
2216 if (step >= 0)
2218 do_radfix(xcoll, edi);
2220 do_radacc(xcoll, edi);
2221 do_radcon(xcoll, edi);
2223 /* add back the average positions */
2224 for (i = 0; i < edi->sav.nr; i++)
2226 rvec_inc(xcoll[i], edi->sav.x[i]);
2231 /* Write out the projections onto the eigenvectors. The order of output
2232 * corresponds to ed_output_legend() */
2233 static void write_edo(t_edpar *edi, FILE *fp, real rmsd)
2235 int i;
2238 /* Output how well we fit to the reference structure */
2239 fprintf(fp, EDcol_ffmt, rmsd);
2241 for (i = 0; i < edi->vecs.mon.neig; i++)
2243 fprintf(fp, EDcol_efmt, edi->vecs.mon.xproj[i]);
2246 for (i = 0; i < edi->vecs.linfix.neig; i++)
2248 fprintf(fp, EDcol_efmt, edi->vecs.linfix.xproj[i]);
2251 for (i = 0; i < edi->vecs.linacc.neig; i++)
2253 fprintf(fp, EDcol_efmt, edi->vecs.linacc.xproj[i]);
2256 for (i = 0; i < edi->vecs.radfix.neig; i++)
2258 fprintf(fp, EDcol_efmt, edi->vecs.radfix.xproj[i]);
2260 if (edi->vecs.radfix.neig)
2262 fprintf(fp, EDcol_ffmt, calc_radius(&edi->vecs.radfix)); /* fixed increment radius */
2265 for (i = 0; i < edi->vecs.radacc.neig; i++)
2267 fprintf(fp, EDcol_efmt, edi->vecs.radacc.xproj[i]);
2269 if (edi->vecs.radacc.neig)
2271 fprintf(fp, EDcol_ffmt, calc_radius(&edi->vecs.radacc)); /* acceptance radius */
2274 for (i = 0; i < edi->vecs.radcon.neig; i++)
2276 fprintf(fp, EDcol_efmt, edi->vecs.radcon.xproj[i]);
2278 if (edi->vecs.radcon.neig)
2280 fprintf(fp, EDcol_ffmt, calc_radius(&edi->vecs.radcon)); /* contracting radius */
2284 /* Returns if any constraints are switched on */
2285 static int ed_constraints(gmx_bool edtype, t_edpar *edi)
2287 if (edtype == eEDedsam || edtype == eEDflood)
2289 return (edi->vecs.linfix.neig || edi->vecs.linacc.neig ||
2290 edi->vecs.radfix.neig || edi->vecs.radacc.neig ||
2291 edi->vecs.radcon.neig);
2293 return 0;
2297 /* Copies reference projection 'refproj' to fixed 'refproj0' variable for flooding/
2298 * umbrella sampling simulations. */
2299 static void copyEvecReference(t_eigvec* floodvecs)
2301 int i;
2304 if (NULL == floodvecs->refproj0)
2306 snew(floodvecs->refproj0, floodvecs->neig);
2309 for (i = 0; i < floodvecs->neig; i++)
2311 floodvecs->refproj0[i] = floodvecs->refproj[i];
2316 /* Call on MASTER only. Check whether the essential dynamics / flooding
2317 * groups of the checkpoint file are consistent with the provided .edi file. */
2318 static void crosscheck_edi_file_vs_checkpoint(gmx_edsam_t ed, edsamstate_t *EDstate)
2320 t_edpar *edi = NULL; /* points to a single edi data set */
2321 int edinum;
2324 if (NULL == EDstate->nref || NULL == EDstate->nav)
2326 gmx_fatal(FARGS, "Essential dynamics and flooding can only be switched on (or off) at the\n"
2327 "start of a new simulation. If a simulation runs with/without ED constraints,\n"
2328 "it must also continue with/without ED constraints when checkpointing.\n"
2329 "To switch on (or off) ED constraints, please prepare a new .tpr to start\n"
2330 "from without a checkpoint.\n");
2333 edi = ed->edpar;
2334 edinum = 0;
2335 while (edi != NULL)
2337 /* Check number of atoms in the reference and average structures */
2338 if (EDstate->nref[edinum] != edi->sref.nr)
2340 gmx_fatal(FARGS, "The number of reference structure atoms in ED group %c is\n"
2341 "not the same in .cpt (NREF=%d) and .edi (NREF=%d) files!\n",
2342 get_EDgroupChar(edinum+1, 0), EDstate->nref[edinum], edi->sref.nr);
2344 if (EDstate->nav[edinum] != edi->sav.nr)
2346 gmx_fatal(FARGS, "The number of average structure atoms in ED group %c is\n"
2347 "not the same in .cpt (NREF=%d) and .edi (NREF=%d) files!\n",
2348 get_EDgroupChar(edinum+1, 0), EDstate->nav[edinum], edi->sav.nr);
2350 edi = edi->next_edi;
2351 edinum++;
2354 if (edinum != EDstate->nED)
2356 gmx_fatal(FARGS, "The number of essential dynamics / flooding groups is not consistent.\n"
2357 "There are %d ED groups in the .cpt file, but %d in the .edi file!\n"
2358 "Are you sure this is the correct .edi file?\n", EDstate->nED, edinum);
2363 /* The edsamstate struct stores the information we need to make the ED group
2364 * whole again after restarts from a checkpoint file. Here we do the following:
2365 * a) If we did not start from .cpt, we prepare the struct for proper .cpt writing,
2366 * b) if we did start from .cpt, we copy over the last whole structures from .cpt,
2367 * c) in any case, for subsequent checkpoint writing, we set the pointers in
2368 * edsamstate to the x_old arrays, which contain the correct PBC representation of
2369 * all ED structures at the last time step. */
2370 static void init_edsamstate(gmx_edsam_t ed, edsamstate_t *EDstate)
2372 int i, nr_edi;
2373 t_edpar *edi;
2376 snew(EDstate->old_sref_p, EDstate->nED);
2377 snew(EDstate->old_sav_p, EDstate->nED);
2379 /* If we did not read in a .cpt file, these arrays are not yet allocated */
2380 if (!EDstate->bFromCpt)
2382 snew(EDstate->nref, EDstate->nED);
2383 snew(EDstate->nav, EDstate->nED);
2386 /* Loop over all ED/flooding data sets (usually only one, though) */
2387 edi = ed->edpar;
2388 for (nr_edi = 1; nr_edi <= EDstate->nED; nr_edi++)
2390 /* We always need the last reference and average positions such that
2391 * in the next time step we can make the ED group whole again
2392 * if the atoms do not have the correct PBC representation */
2393 if (EDstate->bFromCpt)
2395 /* Copy the last whole positions of reference and average group from .cpt */
2396 for (i = 0; i < edi->sref.nr; i++)
2398 copy_rvec(EDstate->old_sref[nr_edi-1][i], edi->sref.x_old[i]);
2400 for (i = 0; i < edi->sav.nr; i++)
2402 copy_rvec(EDstate->old_sav [nr_edi-1][i], edi->sav.x_old [i]);
2405 else
2407 EDstate->nref[nr_edi-1] = edi->sref.nr;
2408 EDstate->nav [nr_edi-1] = edi->sav.nr;
2411 /* For subsequent checkpoint writing, set the edsamstate pointers to the edi arrays: */
2412 EDstate->old_sref_p[nr_edi-1] = edi->sref.x_old;
2413 EDstate->old_sav_p [nr_edi-1] = edi->sav.x_old;
2415 edi = edi->next_edi;
2420 /* Adds 'buf' to 'str' */
2421 static void add_to_string(char **str, char *buf)
2423 int len;
2426 len = strlen(*str) + strlen(buf) + 1;
2427 srenew(*str, len);
2428 strcat(*str, buf);
2432 static void add_to_string_aligned(char **str, char *buf)
2434 char buf_aligned[STRLEN];
2436 sprintf(buf_aligned, EDcol_sfmt, buf);
2437 add_to_string(str, buf_aligned);
2441 static void nice_legend(const char ***setname, int *nsets, char **LegendStr, const char *value, const char *unit, char EDgroupchar)
2443 char tmp[STRLEN], tmp2[STRLEN];
2446 sprintf(tmp, "%c %s", EDgroupchar, value);
2447 add_to_string_aligned(LegendStr, tmp);
2448 sprintf(tmp2, "%s (%s)", tmp, unit);
2449 (*setname)[*nsets] = gmx_strdup(tmp2);
2450 (*nsets)++;
2454 static void nice_legend_evec(const char ***setname, int *nsets, char **LegendStr, t_eigvec *evec, char EDgroupChar, const char *EDtype)
2456 int i;
2457 char tmp[STRLEN];
2460 for (i = 0; i < evec->neig; i++)
2462 sprintf(tmp, "EV%dprj%s", evec->ieig[i], EDtype);
2463 nice_legend(setname, nsets, LegendStr, tmp, "nm", EDgroupChar);
2468 /* Makes a legend for the xvg output file. Call on MASTER only! */
2469 static void write_edo_legend(gmx_edsam_t ed, int nED, const output_env_t oenv)
2471 t_edpar *edi = NULL;
2472 int i;
2473 int nr_edi, nsets, n_flood, n_edsam;
2474 const char **setname;
2475 char buf[STRLEN];
2476 char *LegendStr = NULL;
2479 edi = ed->edpar;
2481 fprintf(ed->edo, "# Output will be written every %d step%s\n", ed->edpar->outfrq, ed->edpar->outfrq != 1 ? "s" : "");
2483 for (nr_edi = 1; nr_edi <= nED; nr_edi++)
2485 fprintf(ed->edo, "#\n");
2486 fprintf(ed->edo, "# Summary of applied con/restraints for the ED group %c\n", get_EDgroupChar(nr_edi, nED));
2487 fprintf(ed->edo, "# Atoms in average structure: %d\n", edi->sav.nr);
2488 fprintf(ed->edo, "# monitor : %d vec%s\n", edi->vecs.mon.neig, edi->vecs.mon.neig != 1 ? "s" : "");
2489 fprintf(ed->edo, "# LINFIX : %d vec%s\n", edi->vecs.linfix.neig, edi->vecs.linfix.neig != 1 ? "s" : "");
2490 fprintf(ed->edo, "# LINACC : %d vec%s\n", edi->vecs.linacc.neig, edi->vecs.linacc.neig != 1 ? "s" : "");
2491 fprintf(ed->edo, "# RADFIX : %d vec%s\n", edi->vecs.radfix.neig, edi->vecs.radfix.neig != 1 ? "s" : "");
2492 fprintf(ed->edo, "# RADACC : %d vec%s\n", edi->vecs.radacc.neig, edi->vecs.radacc.neig != 1 ? "s" : "");
2493 fprintf(ed->edo, "# RADCON : %d vec%s\n", edi->vecs.radcon.neig, edi->vecs.radcon.neig != 1 ? "s" : "");
2494 fprintf(ed->edo, "# FLOODING : %d vec%s ", edi->flood.vecs.neig, edi->flood.vecs.neig != 1 ? "s" : "");
2496 if (edi->flood.vecs.neig)
2498 /* If in any of the groups we find a flooding vector, flooding is turned on */
2499 ed->eEDtype = eEDflood;
2501 /* Print what flavor of flooding we will do */
2502 if (0 == edi->flood.tau) /* constant flooding strength */
2504 fprintf(ed->edo, "Efl_null = %g", edi->flood.constEfl);
2505 if (edi->flood.bHarmonic)
2507 fprintf(ed->edo, ", harmonic");
2510 else /* adaptive flooding */
2512 fprintf(ed->edo, ", adaptive");
2515 fprintf(ed->edo, "\n");
2517 edi = edi->next_edi;
2520 /* Print a nice legend */
2521 snew(LegendStr, 1);
2522 LegendStr[0] = '\0';
2523 sprintf(buf, "# %6s", "time");
2524 add_to_string(&LegendStr, buf);
2526 /* Calculate the maximum number of columns we could end up with */
2527 edi = ed->edpar;
2528 nsets = 0;
2529 for (nr_edi = 1; nr_edi <= nED; nr_edi++)
2531 nsets += 5 +edi->vecs.mon.neig
2532 +edi->vecs.linfix.neig
2533 +edi->vecs.linacc.neig
2534 +edi->vecs.radfix.neig
2535 +edi->vecs.radacc.neig
2536 +edi->vecs.radcon.neig
2537 + 6*edi->flood.vecs.neig;
2538 edi = edi->next_edi;
2540 snew(setname, nsets);
2542 /* In the mdrun time step in a first function call (do_flood()) the flooding
2543 * forces are calculated and in a second function call (do_edsam()) the
2544 * ED constraints. To get a corresponding legend, we need to loop twice
2545 * over the edi groups and output first the flooding, then the ED part */
2547 /* The flooding-related legend entries, if flooding is done */
2548 nsets = 0;
2549 if (eEDflood == ed->eEDtype)
2551 edi = ed->edpar;
2552 for (nr_edi = 1; nr_edi <= nED; nr_edi++)
2554 /* Always write out the projection on the flooding EVs. Of course, this can also
2555 * be achieved with the monitoring option in do_edsam() (if switched on by the
2556 * user), but in that case the positions need to be communicated in do_edsam(),
2557 * which is not necessary when doing flooding only. */
2558 nice_legend(&setname, &nsets, &LegendStr, "RMSD to ref", "nm", get_EDgroupChar(nr_edi, nED) );
2560 for (i = 0; i < edi->flood.vecs.neig; i++)
2562 sprintf(buf, "EV%dprjFLOOD", edi->flood.vecs.ieig[i]);
2563 nice_legend(&setname, &nsets, &LegendStr, buf, "nm", get_EDgroupChar(nr_edi, nED));
2565 /* Output the current reference projection if it changes with time;
2566 * this can happen when flooding is used as harmonic restraint */
2567 if (edi->flood.bHarmonic && edi->flood.vecs.refprojslope[i] != 0.0)
2569 sprintf(buf, "EV%d ref.prj.", edi->flood.vecs.ieig[i]);
2570 nice_legend(&setname, &nsets, &LegendStr, buf, "nm", get_EDgroupChar(nr_edi, nED));
2573 /* For flooding we also output Efl, Vfl, deltaF, and the flooding forces */
2574 if (0 != edi->flood.tau) /* only output Efl for adaptive flooding (constant otherwise) */
2576 sprintf(buf, "EV%d-Efl", edi->flood.vecs.ieig[i]);
2577 nice_legend(&setname, &nsets, &LegendStr, buf, "kJ/mol", get_EDgroupChar(nr_edi, nED));
2580 sprintf(buf, "EV%d-Vfl", edi->flood.vecs.ieig[i]);
2581 nice_legend(&setname, &nsets, &LegendStr, buf, "kJ/mol", get_EDgroupChar(nr_edi, nED));
2583 if (0 != edi->flood.tau) /* only output deltaF for adaptive flooding (zero otherwise) */
2585 sprintf(buf, "EV%d-deltaF", edi->flood.vecs.ieig[i]);
2586 nice_legend(&setname, &nsets, &LegendStr, buf, "kJ/mol", get_EDgroupChar(nr_edi, nED));
2589 sprintf(buf, "EV%d-FLforces", edi->flood.vecs.ieig[i]);
2590 nice_legend(&setname, &nsets, &LegendStr, buf, "kJ/mol/nm", get_EDgroupChar(nr_edi, nED));
2593 edi = edi->next_edi;
2594 } /* End of flooding-related legend entries */
2596 n_flood = nsets;
2598 /* Now the ED-related entries, if essential dynamics is done */
2599 edi = ed->edpar;
2600 for (nr_edi = 1; nr_edi <= nED; nr_edi++)
2602 if (bNeedDoEdsam(edi)) /* Only print ED legend if at least one ED option is on */
2604 nice_legend(&setname, &nsets, &LegendStr, "RMSD to ref", "nm", get_EDgroupChar(nr_edi, nED) );
2606 /* Essential dynamics, projections on eigenvectors */
2607 nice_legend_evec(&setname, &nsets, &LegendStr, &edi->vecs.mon, get_EDgroupChar(nr_edi, nED), "MON" );
2608 nice_legend_evec(&setname, &nsets, &LegendStr, &edi->vecs.linfix, get_EDgroupChar(nr_edi, nED), "LINFIX");
2609 nice_legend_evec(&setname, &nsets, &LegendStr, &edi->vecs.linacc, get_EDgroupChar(nr_edi, nED), "LINACC");
2610 nice_legend_evec(&setname, &nsets, &LegendStr, &edi->vecs.radfix, get_EDgroupChar(nr_edi, nED), "RADFIX");
2611 if (edi->vecs.radfix.neig)
2613 nice_legend(&setname, &nsets, &LegendStr, "RADFIX radius", "nm", get_EDgroupChar(nr_edi, nED));
2615 nice_legend_evec(&setname, &nsets, &LegendStr, &edi->vecs.radacc, get_EDgroupChar(nr_edi, nED), "RADACC");
2616 if (edi->vecs.radacc.neig)
2618 nice_legend(&setname, &nsets, &LegendStr, "RADACC radius", "nm", get_EDgroupChar(nr_edi, nED));
2620 nice_legend_evec(&setname, &nsets, &LegendStr, &edi->vecs.radcon, get_EDgroupChar(nr_edi, nED), "RADCON");
2621 if (edi->vecs.radcon.neig)
2623 nice_legend(&setname, &nsets, &LegendStr, "RADCON radius", "nm", get_EDgroupChar(nr_edi, nED));
2626 edi = edi->next_edi;
2627 } /* end of 'pure' essential dynamics legend entries */
2628 n_edsam = nsets - n_flood;
2630 xvgr_legend(ed->edo, nsets, setname, oenv);
2631 sfree(setname);
2633 fprintf(ed->edo, "#\n"
2634 "# Legend for %d column%s of flooding plus %d column%s of essential dynamics data:\n",
2635 n_flood, 1 == n_flood ? "" : "s",
2636 n_edsam, 1 == n_edsam ? "" : "s");
2637 fprintf(ed->edo, "%s", LegendStr);
2638 sfree(LegendStr);
2640 fflush(ed->edo);
2644 /* Init routine for ED and flooding. Calls init_edi in a loop for every .edi-cycle
2645 * contained in the input file, creates a NULL terminated list of t_edpar structures */
2646 void init_edsam(const gmx_mtop_t *mtop,
2647 const t_inputrec *ir,
2648 t_commrec *cr,
2649 gmx_edsam_t ed,
2650 rvec x[],
2651 matrix box,
2652 edsamstate_t *EDstate)
2654 t_edpar *edi = NULL; /* points to a single edi data set */
2655 int i, nr_edi, avindex;
2656 rvec *x_pbc = NULL; /* positions of the whole MD system with pbc removed */
2657 rvec *xfit = NULL, *xstart = NULL; /* dummy arrays to determine initial RMSDs */
2658 rvec fit_transvec; /* translation ... */
2659 matrix fit_rotmat; /* ... and rotation from fit to reference structure */
2660 rvec *ref_x_old = NULL; /* helper pointer */
2662 if (MASTER(cr))
2664 fprintf(stderr, "ED: Initializing essential dynamics constraints.\n");
2666 if (NULL == ed)
2668 gmx_fatal(FARGS, "The checkpoint file you provided is from an essential dynamics or\n"
2669 "flooding simulation. Please also provide the correct .edi file with -ei.\n");
2673 /* Needed for initializing radacc radius in do_edsam */
2674 ed->bFirst = TRUE;
2676 /* The input file is read by the master and the edi structures are
2677 * initialized here. Input is stored in ed->edpar. Then the edi
2678 * structures are transferred to the other nodes */
2679 if (MASTER(cr))
2681 /* Initialization for every ED/flooding group. Flooding uses one edi group per
2682 * flooding vector, Essential dynamics can be applied to more than one structure
2683 * as well, but will be done in the order given in the edi file, so
2684 * expect different results for different order of edi file concatenation! */
2685 edi = ed->edpar;
2686 while (edi != NULL)
2688 init_edi(mtop, edi);
2689 init_flood(edi, ed, ir->delta_t);
2690 edi = edi->next_edi;
2694 /* The master does the work here. The other nodes get the positions
2695 * not before dd_partition_system which is called after init_edsam */
2696 if (MASTER(cr))
2698 if (!EDstate->bFromCpt)
2700 /* Remove PBC, make molecule(s) subject to ED whole. */
2701 snew(x_pbc, mtop->natoms);
2702 m_rveccopy(mtop->natoms, x, x_pbc);
2703 do_pbc_first_mtop(NULL, ir->ePBC, box, mtop, x_pbc);
2705 /* Reset pointer to first ED data set which contains the actual ED data */
2706 edi = ed->edpar;
2707 GMX_ASSERT(NULL != edi,
2708 "The pointer to the essential dynamics parameters is undefined");
2710 /* Loop over all ED/flooding data sets (usually only one, though) */
2711 for (nr_edi = 1; nr_edi <= EDstate->nED; nr_edi++)
2713 /* For multiple ED groups we use the output frequency that was specified
2714 * in the first set */
2715 if (nr_edi > 1)
2717 edi->outfrq = ed->edpar->outfrq;
2720 /* Extract the initial reference and average positions. When starting
2721 * from .cpt, these have already been read into sref.x_old
2722 * in init_edsamstate() */
2723 if (!EDstate->bFromCpt)
2725 /* If this is the first run (i.e. no checkpoint present) we assume
2726 * that the starting positions give us the correct PBC representation */
2727 for (i = 0; i < edi->sref.nr; i++)
2729 copy_rvec(x_pbc[edi->sref.anrs[i]], edi->sref.x_old[i]);
2732 for (i = 0; i < edi->sav.nr; i++)
2734 copy_rvec(x_pbc[edi->sav.anrs[i]], edi->sav.x_old[i]);
2738 /* Now we have the PBC-correct start positions of the reference and
2739 average structure. We copy that over to dummy arrays on which we
2740 can apply fitting to print out the RMSD. We srenew the memory since
2741 the size of the buffers is likely different for every ED group */
2742 srenew(xfit, edi->sref.nr );
2743 srenew(xstart, edi->sav.nr );
2744 if (edi->bRefEqAv)
2746 /* Reference indices are the same as average indices */
2747 ref_x_old = edi->sav.x_old;
2749 else
2751 ref_x_old = edi->sref.x_old;
2753 copy_rvecn(ref_x_old, xfit, 0, edi->sref.nr);
2754 copy_rvecn(edi->sav.x_old, xstart, 0, edi->sav.nr);
2756 /* Make the fit to the REFERENCE structure, get translation and rotation */
2757 fit_to_reference(xfit, fit_transvec, fit_rotmat, edi);
2759 /* Output how well we fit to the reference at the start */
2760 translate_and_rotate(xfit, edi->sref.nr, fit_transvec, fit_rotmat);
2761 fprintf(stderr, "ED: Initial RMSD from reference after fit = %f nm",
2762 rmsd_from_structure(xfit, &edi->sref));
2763 if (EDstate->nED > 1)
2765 fprintf(stderr, " (ED group %c)", get_EDgroupChar(nr_edi, EDstate->nED));
2767 fprintf(stderr, "\n");
2769 /* Now apply the translation and rotation to the atoms on which ED sampling will be performed */
2770 translate_and_rotate(xstart, edi->sav.nr, fit_transvec, fit_rotmat);
2772 /* calculate initial projections */
2773 project(xstart, edi);
2775 /* For the target and origin structure both a reference (fit) and an
2776 * average structure can be provided in make_edi. If both structures
2777 * are the same, make_edi only stores one of them in the .edi file.
2778 * If they differ, first the fit and then the average structure is stored
2779 * in star (or sor), thus the number of entries in star/sor is
2780 * (n_fit + n_av) with n_fit the size of the fitting group and n_av
2781 * the size of the average group. */
2783 /* process target structure, if required */
2784 if (edi->star.nr > 0)
2786 fprintf(stderr, "ED: Fitting target structure to reference structure\n");
2788 /* get translation & rotation for fit of target structure to reference structure */
2789 fit_to_reference(edi->star.x, fit_transvec, fit_rotmat, edi);
2790 /* do the fit */
2791 translate_and_rotate(edi->star.x, edi->star.nr, fit_transvec, fit_rotmat);
2792 if (edi->star.nr == edi->sav.nr)
2794 avindex = 0;
2796 else /* edi->star.nr = edi->sref.nr + edi->sav.nr */
2798 /* The last sav.nr indices of the target structure correspond to
2799 * the average structure, which must be projected */
2800 avindex = edi->star.nr - edi->sav.nr;
2802 rad_project(edi, &edi->star.x[avindex], &edi->vecs.radcon);
2804 else
2806 rad_project(edi, xstart, &edi->vecs.radcon);
2809 /* process structure that will serve as origin of expansion circle */
2810 if ( (eEDflood == ed->eEDtype) && (FALSE == edi->flood.bConstForce) )
2812 fprintf(stderr, "ED: Setting center of flooding potential (0 = average structure)\n");
2815 if (edi->sori.nr > 0)
2817 fprintf(stderr, "ED: Fitting origin structure to reference structure\n");
2819 /* fit this structure to reference structure */
2820 fit_to_reference(edi->sori.x, fit_transvec, fit_rotmat, edi);
2821 /* do the fit */
2822 translate_and_rotate(edi->sori.x, edi->sori.nr, fit_transvec, fit_rotmat);
2823 if (edi->sori.nr == edi->sav.nr)
2825 avindex = 0;
2827 else /* edi->sori.nr = edi->sref.nr + edi->sav.nr */
2829 /* For the projection, we need the last sav.nr indices of sori */
2830 avindex = edi->sori.nr - edi->sav.nr;
2833 rad_project(edi, &edi->sori.x[avindex], &edi->vecs.radacc);
2834 rad_project(edi, &edi->sori.x[avindex], &edi->vecs.radfix);
2835 if ( (eEDflood == ed->eEDtype) && (FALSE == edi->flood.bConstForce) )
2837 fprintf(stderr, "ED: The ORIGIN structure will define the flooding potential center.\n");
2838 /* Set center of flooding potential to the ORIGIN structure */
2839 rad_project(edi, &edi->sori.x[avindex], &edi->flood.vecs);
2840 /* We already know that no (moving) reference position was provided,
2841 * therefore we can overwrite refproj[0]*/
2842 copyEvecReference(&edi->flood.vecs);
2845 else /* No origin structure given */
2847 rad_project(edi, xstart, &edi->vecs.radacc);
2848 rad_project(edi, xstart, &edi->vecs.radfix);
2849 if ( (eEDflood == ed->eEDtype) && (FALSE == edi->flood.bConstForce) )
2851 if (edi->flood.bHarmonic)
2853 fprintf(stderr, "ED: A (possibly changing) ref. projection will define the flooding potential center.\n");
2854 for (i = 0; i < edi->flood.vecs.neig; i++)
2856 edi->flood.vecs.refproj[i] = edi->flood.vecs.refproj0[i];
2859 else
2861 fprintf(stderr, "ED: The AVERAGE structure will define the flooding potential center.\n");
2862 /* Set center of flooding potential to the center of the covariance matrix,
2863 * i.e. the average structure, i.e. zero in the projected system */
2864 for (i = 0; i < edi->flood.vecs.neig; i++)
2866 edi->flood.vecs.refproj[i] = 0.0;
2871 /* For convenience, output the center of the flooding potential for the eigenvectors */
2872 if ( (eEDflood == ed->eEDtype) && (FALSE == edi->flood.bConstForce) )
2874 for (i = 0; i < edi->flood.vecs.neig; i++)
2876 fprintf(stdout, "ED: EV %d flooding potential center: %11.4e", edi->flood.vecs.ieig[i], edi->flood.vecs.refproj[i]);
2877 if (edi->flood.bHarmonic)
2879 fprintf(stdout, " (adding %11.4e/timestep)", edi->flood.vecs.refprojslope[i]);
2881 fprintf(stdout, "\n");
2885 /* set starting projections for linsam */
2886 rad_project(edi, xstart, &edi->vecs.linacc);
2887 rad_project(edi, xstart, &edi->vecs.linfix);
2889 /* Prepare for the next edi data set: */
2890 edi = edi->next_edi;
2892 /* Cleaning up on the master node: */
2893 if (!EDstate->bFromCpt)
2895 sfree(x_pbc);
2897 sfree(xfit);
2898 sfree(xstart);
2900 } /* end of MASTER only section */
2902 if (PAR(cr))
2904 /* First let everybody know how many ED data sets to expect */
2905 gmx_bcast(sizeof(EDstate->nED), &EDstate->nED, cr);
2906 /* Broadcast the essential dynamics / flooding data to all nodes */
2907 broadcast_ed_data(cr, ed, EDstate->nED);
2909 else
2911 /* In the single-CPU case, point the local atom numbers pointers to the global
2912 * one, so that we can use the same notation in serial and parallel case: */
2914 /* Loop over all ED data sets (usually only one, though) */
2915 edi = ed->edpar;
2916 for (nr_edi = 1; nr_edi <= EDstate->nED; nr_edi++)
2918 edi->sref.anrs_loc = edi->sref.anrs;
2919 edi->sav.anrs_loc = edi->sav.anrs;
2920 edi->star.anrs_loc = edi->star.anrs;
2921 edi->sori.anrs_loc = edi->sori.anrs;
2922 /* For the same reason as above, make a dummy c_ind array: */
2923 snew(edi->sav.c_ind, edi->sav.nr);
2924 /* Initialize the array */
2925 for (i = 0; i < edi->sav.nr; i++)
2927 edi->sav.c_ind[i] = i;
2929 /* In the general case we will need a different-sized array for the reference indices: */
2930 if (!edi->bRefEqAv)
2932 snew(edi->sref.c_ind, edi->sref.nr);
2933 for (i = 0; i < edi->sref.nr; i++)
2935 edi->sref.c_ind[i] = i;
2938 /* Point to the very same array in case of other structures: */
2939 edi->star.c_ind = edi->sav.c_ind;
2940 edi->sori.c_ind = edi->sav.c_ind;
2941 /* In the serial case, the local number of atoms is the global one: */
2942 edi->sref.nr_loc = edi->sref.nr;
2943 edi->sav.nr_loc = edi->sav.nr;
2944 edi->star.nr_loc = edi->star.nr;
2945 edi->sori.nr_loc = edi->sori.nr;
2947 /* An on we go to the next ED group */
2948 edi = edi->next_edi;
2952 /* Allocate space for ED buffer variables */
2953 /* Again, loop over ED data sets */
2954 edi = ed->edpar;
2955 for (nr_edi = 1; nr_edi <= EDstate->nED; nr_edi++)
2957 /* Allocate space for ED buffer variables */
2958 snew_bc(cr, edi->buf, 1); /* MASTER has already allocated edi->buf in init_edi() */
2959 snew(edi->buf->do_edsam, 1);
2961 /* Space for collective ED buffer variables */
2963 /* Collective positions of atoms with the average indices */
2964 snew(edi->buf->do_edsam->xcoll, edi->sav.nr);
2965 snew(edi->buf->do_edsam->shifts_xcoll, edi->sav.nr); /* buffer for xcoll shifts */
2966 snew(edi->buf->do_edsam->extra_shifts_xcoll, edi->sav.nr);
2967 /* Collective positions of atoms with the reference indices */
2968 if (!edi->bRefEqAv)
2970 snew(edi->buf->do_edsam->xc_ref, edi->sref.nr);
2971 snew(edi->buf->do_edsam->shifts_xc_ref, edi->sref.nr); /* To store the shifts in */
2972 snew(edi->buf->do_edsam->extra_shifts_xc_ref, edi->sref.nr);
2975 /* Get memory for flooding forces */
2976 snew(edi->flood.forces_cartesian, edi->sav.nr);
2978 #ifdef DUMPEDI
2979 /* Dump it all into one file per process */
2980 dump_edi(edi, cr, nr_edi);
2981 #endif
2983 /* Next ED group */
2984 edi = edi->next_edi;
2987 /* Flush the edo file so that the user can check some things
2988 * when the simulation has started */
2989 if (ed->edo)
2991 fflush(ed->edo);
2996 void do_edsam(const t_inputrec *ir,
2997 gmx_int64_t step,
2998 t_commrec *cr,
2999 rvec xs[],
3000 rvec v[],
3001 matrix box,
3002 gmx_edsam_t ed)
3004 int i, edinr, iupdate = 500;
3005 matrix rotmat; /* rotation matrix */
3006 rvec transvec; /* translation vector */
3007 rvec dv, dx, x_unsh; /* tmp vectors for velocity, distance, unshifted x coordinate */
3008 real dt_1; /* 1/dt */
3009 struct t_do_edsam *buf;
3010 t_edpar *edi;
3011 real rmsdev = -1; /* RMSD from reference structure prior to applying the constraints */
3012 gmx_bool bSuppress = FALSE; /* Write .xvg output file on master? */
3015 /* Check if ED sampling has to be performed */
3016 if (ed->eEDtype == eEDnone)
3018 return;
3021 /* Suppress output on first call of do_edsam if
3022 * two-step sd2 integrator is used */
3023 if ( (ir->eI == eiSD2) && (v != NULL) )
3025 bSuppress = TRUE;
3028 dt_1 = 1.0/ir->delta_t;
3030 /* Loop over all ED groups (usually one) */
3031 edi = ed->edpar;
3032 edinr = 0;
3033 while (edi != NULL)
3035 edinr++;
3036 if (bNeedDoEdsam(edi))
3039 buf = edi->buf->do_edsam;
3041 if (ed->bFirst)
3043 /* initialize radacc radius for slope criterion */
3044 buf->oldrad = calc_radius(&edi->vecs.radacc);
3047 /* Copy the positions into buf->xc* arrays and after ED
3048 * feed back corrections to the official positions */
3050 /* Broadcast the ED positions such that every node has all of them
3051 * Every node contributes its local positions xs and stores it in
3052 * the collective buf->xcoll array. Note that for edinr > 1
3053 * xs could already have been modified by an earlier ED */
3055 communicate_group_positions(cr, buf->xcoll, buf->shifts_xcoll, buf->extra_shifts_xcoll, PAR(cr) ? buf->bUpdateShifts : TRUE, xs,
3056 edi->sav.nr, edi->sav.nr_loc, edi->sav.anrs_loc, edi->sav.c_ind, edi->sav.x_old, box);
3058 /* Only assembly reference positions if their indices differ from the average ones */
3059 if (!edi->bRefEqAv)
3061 communicate_group_positions(cr, buf->xc_ref, buf->shifts_xc_ref, buf->extra_shifts_xc_ref, PAR(cr) ? buf->bUpdateShifts : TRUE, xs,
3062 edi->sref.nr, edi->sref.nr_loc, edi->sref.anrs_loc, edi->sref.c_ind, edi->sref.x_old, box);
3065 /* If bUpdateShifts was TRUE then the shifts have just been updated in communicate_group_positions.
3066 * We do not need to update the shifts until the next NS step. Note that dd_make_local_ed_indices
3067 * set bUpdateShifts=TRUE in the parallel case. */
3068 buf->bUpdateShifts = FALSE;
3070 /* Now all nodes have all of the ED positions in edi->sav->xcoll,
3071 * as well as the indices in edi->sav.anrs */
3073 /* Fit the reference indices to the reference structure */
3074 if (edi->bRefEqAv)
3076 fit_to_reference(buf->xcoll, transvec, rotmat, edi);
3078 else
3080 fit_to_reference(buf->xc_ref, transvec, rotmat, edi);
3083 /* Now apply the translation and rotation to the ED structure */
3084 translate_and_rotate(buf->xcoll, edi->sav.nr, transvec, rotmat);
3086 /* Find out how well we fit to the reference (just for output steps) */
3087 if (do_per_step(step, edi->outfrq) && MASTER(cr))
3089 if (edi->bRefEqAv)
3091 /* Indices of reference and average structures are identical,
3092 * thus we can calculate the rmsd to SREF using xcoll */
3093 rmsdev = rmsd_from_structure(buf->xcoll, &edi->sref);
3095 else
3097 /* We have to translate & rotate the reference atoms first */
3098 translate_and_rotate(buf->xc_ref, edi->sref.nr, transvec, rotmat);
3099 rmsdev = rmsd_from_structure(buf->xc_ref, &edi->sref);
3103 /* update radsam references, when required */
3104 if (do_per_step(step, edi->maxedsteps) && step >= edi->presteps)
3106 project(buf->xcoll, edi);
3107 rad_project(edi, buf->xcoll, &edi->vecs.radacc);
3108 rad_project(edi, buf->xcoll, &edi->vecs.radfix);
3109 buf->oldrad = -1.e5;
3112 /* update radacc references, when required */
3113 if (do_per_step(step, iupdate) && step >= edi->presteps)
3115 edi->vecs.radacc.radius = calc_radius(&edi->vecs.radacc);
3116 if (edi->vecs.radacc.radius - buf->oldrad < edi->slope)
3118 project(buf->xcoll, edi);
3119 rad_project(edi, buf->xcoll, &edi->vecs.radacc);
3120 buf->oldrad = 0.0;
3122 else
3124 buf->oldrad = edi->vecs.radacc.radius;
3128 /* apply the constraints */
3129 if (step >= edi->presteps && ed_constraints(ed->eEDtype, edi))
3131 /* ED constraints should be applied already in the first MD step
3132 * (which is step 0), therefore we pass step+1 to the routine */
3133 ed_apply_constraints(buf->xcoll, edi, step+1 - ir->init_step);
3136 /* write to edo, when required */
3137 if (do_per_step(step, edi->outfrq))
3139 project(buf->xcoll, edi);
3140 if (MASTER(cr) && !bSuppress)
3142 write_edo(edi, ed->edo, rmsdev);
3146 /* Copy back the positions unless monitoring only */
3147 if (ed_constraints(ed->eEDtype, edi))
3149 /* remove fitting */
3150 rmfit(edi->sav.nr, buf->xcoll, transvec, rotmat);
3152 /* Copy the ED corrected positions into the coordinate array */
3153 /* Each node copies its local part. In the serial case, nat_loc is the
3154 * total number of ED atoms */
3155 for (i = 0; i < edi->sav.nr_loc; i++)
3157 /* Unshift local ED coordinate and store in x_unsh */
3158 ed_unshift_single_coord(box, buf->xcoll[edi->sav.c_ind[i]],
3159 buf->shifts_xcoll[edi->sav.c_ind[i]], x_unsh);
3161 /* dx is the ED correction to the positions: */
3162 rvec_sub(x_unsh, xs[edi->sav.anrs_loc[i]], dx);
3164 if (v != NULL)
3166 /* dv is the ED correction to the velocity: */
3167 svmul(dt_1, dx, dv);
3168 /* apply the velocity correction: */
3169 rvec_inc(v[edi->sav.anrs_loc[i]], dv);
3171 /* Finally apply the position correction due to ED: */
3172 copy_rvec(x_unsh, xs[edi->sav.anrs_loc[i]]);
3175 } /* END of if ( bNeedDoEdsam(edi) ) */
3177 /* Prepare for the next ED group */
3178 edi = edi->next_edi;
3180 } /* END of loop over ED groups */
3182 ed->bFirst = FALSE;
3185 void done_ed(gmx_edsam_t *ed)
3187 if (*ed)
3189 /* ed->edo is opened sometimes with xvgropen, sometimes with
3190 * gmx_fio_fopen, so we use the least common denominator for
3191 * closing. */
3192 gmx_fio_fclose((*ed)->edo);
3195 /* TODO deallocate ed and set pointer to NULL */