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,2016,2017, 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.
45 #include "gromacs/commandline/filenm.h"
46 #include "gromacs/domdec/domdec_struct.h"
47 #include "gromacs/fileio/gmxfio.h"
48 #include "gromacs/fileio/xvgr.h"
49 #include "gromacs/gmxlib/network.h"
50 #include "gromacs/gmxlib/nrnb.h"
51 #include "gromacs/linearalgebra/nrjac.h"
52 #include "gromacs/math/functions.h"
53 #include "gromacs/math/utilities.h"
54 #include "gromacs/math/vec.h"
55 #include "gromacs/math/vectypes.h"
56 #include "gromacs/mdlib/broadcaststructs.h"
57 #include "gromacs/mdlib/groupcoord.h"
58 #include "gromacs/mdlib/mdrun.h"
59 #include "gromacs/mdlib/sim_util.h"
60 #include "gromacs/mdlib/update.h"
61 #include "gromacs/mdtypes/commrec.h"
62 #include "gromacs/mdtypes/inputrec.h"
63 #include "gromacs/mdtypes/md_enums.h"
64 #include "gromacs/mdtypes/state.h"
65 #include "gromacs/pbcutil/pbc.h"
66 #include "gromacs/topology/mtop_lookup.h"
67 #include "gromacs/topology/topology.h"
68 #include "gromacs/utility/cstringutil.h"
69 #include "gromacs/utility/fatalerror.h"
70 #include "gromacs/utility/gmxassert.h"
71 #include "gromacs/utility/smalloc.h"
74 /* enum to identify the type of ED: none, normal ED, flooding */
76 eEDnone
, eEDedsam
, eEDflood
, eEDnr
79 /* enum to identify operations on reference, average, origin, target structures */
81 eedREF
, eedAV
, eedORI
, eedTAR
, eedNR
87 int neig
; /* nr of eigenvectors */
88 int *ieig
; /* index nrs of eigenvectors */
89 real
*stpsz
; /* stepsizes (per eigenvector) */
90 rvec
**vec
; /* eigenvector components */
91 real
*xproj
; /* instantaneous x projections */
92 real
*fproj
; /* instantaneous f projections */
93 real radius
; /* instantaneous radius */
94 real
*refproj
; /* starting or target projections */
95 /* When using flooding as harmonic restraint: The current reference projection
96 * is at each step calculated from the initial refproj0 and the slope. */
97 real
*refproj0
, *refprojslope
;
103 t_eigvec mon
; /* only monitored, no constraints */
104 t_eigvec linfix
; /* fixed linear constraints */
105 t_eigvec linacc
; /* acceptance linear constraints */
106 t_eigvec radfix
; /* fixed radial constraints (exp) */
107 t_eigvec radacc
; /* acceptance radial constraints (exp) */
108 t_eigvec radcon
; /* acceptance rad. contraction constr. */
115 gmx_bool bHarmonic
; /* Use flooding for harmonic restraint on
117 gmx_bool bConstForce
; /* Do not calculate a flooding potential,
118 instead flood with a constant force */
127 rvec
*forces_cartesian
;
128 t_eigvec vecs
; /* use flooding for these */
132 /* This type is for the average, reference, target, and origin structure */
133 typedef struct gmx_edx
135 int nr
; /* number of atoms this structure contains */
136 int nr_loc
; /* number of atoms on local node */
137 int *anrs
; /* atom index numbers */
138 int *anrs_loc
; /* local atom index numbers */
139 int nalloc_loc
; /* allocation size of anrs_loc */
140 int *c_ind
; /* at which position of the whole anrs
141 * array is a local atom?, i.e.
142 * c_ind[0...nr_loc-1] gives the atom index
143 * with respect to the collective
144 * anrs[0...nr-1] array */
145 rvec
*x
; /* positions for this structure */
146 rvec
*x_old
; /* Last positions which have the correct PBC
147 representation of the ED group. In
148 combination with keeping track of the
149 shift vectors, the ED group can always
151 real
*m
; /* masses */
152 real mtot
; /* total mass (only used in sref) */
153 real
*sqrtm
; /* sqrt of the masses used for mass-
154 * weighting of analysis (only used in sav) */
160 int nini
; /* total Nr of atoms */
161 gmx_bool fitmas
; /* true if trans fit with cm */
162 gmx_bool pcamas
; /* true if mass-weighted PCA */
163 int presteps
; /* number of steps to run without any
164 * perturbations ... just monitoring */
165 int outfrq
; /* freq (in steps) of writing to edo */
166 int maxedsteps
; /* max nr of steps per cycle */
168 /* all gmx_edx datasets are copied to all nodes in the parallel case */
169 struct gmx_edx sref
; /* reference positions, to these fitting
171 gmx_bool bRefEqAv
; /* If true, reference & average indices
172 * are the same. Used for optimization */
173 struct gmx_edx sav
; /* average positions */
174 struct gmx_edx star
; /* target positions */
175 struct gmx_edx sori
; /* origin positions */
177 t_edvecs vecs
; /* eigenvectors */
178 real slope
; /* minimal slope in acceptance radexp */
180 t_edflood flood
; /* parameters especially for flooding */
181 struct t_ed_buffer
*buf
; /* handle to local buffers */
182 struct edpar
*next_edi
; /* Pointer to another ED group */
186 typedef struct gmx_edsam
188 int eEDtype
; /* Type of ED: see enums above */
189 FILE *edo
; /* output file pointer */
199 rvec old_transvec
, older_transvec
, transvec_compact
;
200 rvec
*xcoll
; /* Positions from all nodes, this is the
201 collective set we work on.
202 These are the positions of atoms with
203 average structure indices */
204 rvec
*xc_ref
; /* same but with reference structure indices */
205 ivec
*shifts_xcoll
; /* Shifts for xcoll */
206 ivec
*extra_shifts_xcoll
; /* xcoll shift changes since last NS step */
207 ivec
*shifts_xc_ref
; /* Shifts for xc_ref */
208 ivec
*extra_shifts_xc_ref
; /* xc_ref shift changes since last NS step */
209 gmx_bool bUpdateShifts
; /* TRUE in NS steps to indicate that the
210 ED shifts for this ED group need to
215 /* definition of ED buffer structure */
218 struct t_fit_to_ref
* fit_to_ref
;
219 struct t_do_edfit
* do_edfit
;
220 struct t_do_edsam
* do_edsam
;
221 struct t_do_radcon
* do_radcon
;
225 /* Function declarations */
226 static void fit_to_reference(rvec
*xcoll
, rvec transvec
, matrix rotmat
, t_edpar
*edi
);
227 static void translate_and_rotate(rvec
*x
, int nat
, rvec transvec
, matrix rotmat
);
228 static real
rmsd_from_structure(rvec
*x
, struct gmx_edx
*s
);
229 static int read_edi_file(const char *fn
, t_edpar
*edi
, int nr_mdatoms
);
230 static void crosscheck_edi_file_vs_checkpoint(gmx_edsam_t ed
, edsamstate_t
*EDstate
);
231 static void init_edsamstate(gmx_edsam_t ed
, edsamstate_t
*EDstate
);
232 static void write_edo_legend(gmx_edsam_t ed
, int nED
, const gmx_output_env_t
*oenv
);
233 /* End function declarations */
235 /* Define formats for the column width in the output file */
236 const char EDcol_sfmt
[] = "%17s";
237 const char EDcol_efmt
[] = "%17.5e";
238 const char EDcol_ffmt
[] = "%17f";
240 /* Define a formats for reading, otherwise cppcheck complains for scanf without width limits */
241 const char max_ev_fmt_d
[] = "%7d"; /* Number of eigenvectors. 9,999,999 should be enough */
242 const char max_ev_fmt_lf
[] = "%12lf";
243 const char max_ev_fmt_dlf
[] = "%7d%12lf";
244 const char max_ev_fmt_dlflflf
[] = "%7d%12lf%12lf%12lf";
245 const char max_ev_fmt_lelele
[] = "%12le%12le%12le";
247 /* Do we have to perform essential dynamics constraints or possibly only flooding
248 * for any of the ED groups? */
249 static gmx_bool
bNeedDoEdsam(t_edpar
*edi
)
251 return edi
->vecs
.mon
.neig
252 || edi
->vecs
.linfix
.neig
253 || edi
->vecs
.linacc
.neig
254 || edi
->vecs
.radfix
.neig
255 || edi
->vecs
.radacc
.neig
256 || edi
->vecs
.radcon
.neig
;
260 /* Multiple ED groups will be labeled with letters instead of numbers
261 * to avoid confusion with eigenvector indices */
262 static char get_EDgroupChar(int nr_edi
, int nED
)
270 * nr_edi = 2 -> B ...
272 return 'A' + nr_edi
- 1;
276 /* Does not subtract average positions, projection on single eigenvector is returned
277 * used by: do_linfix, do_linacc, do_radfix, do_radacc, do_radcon
278 * Average position is subtracted in ed_apply_constraints prior to calling projectx
280 static real
projectx(t_edpar
*edi
, rvec
*xcoll
, rvec
*vec
)
286 for (i
= 0; i
< edi
->sav
.nr
; i
++)
288 proj
+= edi
->sav
.sqrtm
[i
]*iprod(vec
[i
], xcoll
[i
]);
295 /* Specialized: projection is stored in vec->refproj
296 * -> used for radacc, radfix, radcon and center of flooding potential
297 * subtracts average positions, projects vector x */
298 static void rad_project(t_edpar
*edi
, rvec
*x
, t_eigvec
*vec
)
303 /* Subtract average positions */
304 for (i
= 0; i
< edi
->sav
.nr
; i
++)
306 rvec_dec(x
[i
], edi
->sav
.x
[i
]);
309 for (i
= 0; i
< vec
->neig
; i
++)
311 vec
->refproj
[i
] = projectx(edi
, x
, vec
->vec
[i
]);
312 rad
+= gmx::square((vec
->refproj
[i
]-vec
->xproj
[i
]));
314 vec
->radius
= sqrt(rad
);
316 /* Add average positions */
317 for (i
= 0; i
< edi
->sav
.nr
; i
++)
319 rvec_inc(x
[i
], edi
->sav
.x
[i
]);
324 /* Project vector x, subtract average positions prior to projection and add
325 * them afterwards to retain the unchanged vector. Store in xproj. Mass-weighting
327 static void project_to_eigvectors(rvec
*x
, /* The positions to project to an eigenvector */
328 t_eigvec
*vec
, /* The eigenvectors */
339 /* Subtract average positions */
340 for (i
= 0; i
< edi
->sav
.nr
; i
++)
342 rvec_dec(x
[i
], edi
->sav
.x
[i
]);
345 for (i
= 0; i
< vec
->neig
; i
++)
347 vec
->xproj
[i
] = projectx(edi
, x
, vec
->vec
[i
]);
350 /* Add average positions */
351 for (i
= 0; i
< edi
->sav
.nr
; i
++)
353 rvec_inc(x
[i
], edi
->sav
.x
[i
]);
358 /* Project vector x onto all edi->vecs (mon, linfix,...) */
359 static void project(rvec
*x
, /* positions to project */
360 t_edpar
*edi
) /* edi data set */
362 /* It is not more work to subtract the average position in every
363 * subroutine again, because these routines are rarely used simultaneously */
364 project_to_eigvectors(x
, &edi
->vecs
.mon
, edi
);
365 project_to_eigvectors(x
, &edi
->vecs
.linfix
, edi
);
366 project_to_eigvectors(x
, &edi
->vecs
.linacc
, edi
);
367 project_to_eigvectors(x
, &edi
->vecs
.radfix
, edi
);
368 project_to_eigvectors(x
, &edi
->vecs
.radacc
, edi
);
369 project_to_eigvectors(x
, &edi
->vecs
.radcon
, edi
);
373 static real
calc_radius(t_eigvec
*vec
)
379 for (i
= 0; i
< vec
->neig
; i
++)
381 rad
+= gmx::square((vec
->refproj
[i
]-vec
->xproj
[i
]));
384 return rad
= sqrt(rad
);
390 static void dump_xcoll(t_edpar
*edi
, struct t_do_edsam
*buf
, t_commrec
*cr
,
397 ivec
*shifts
, *eshifts
;
406 shifts
= buf
->shifts_xcoll
;
407 eshifts
= buf
->extra_shifts_xcoll
;
409 sprintf(fn
, "xcolldump_step%d.txt", step
);
412 for (i
= 0; i
< edi
->sav
.nr
; i
++)
414 fprintf(fp
, "%d %9.5f %9.5f %9.5f %d %d %d %d %d %d\n",
416 xcoll
[i
][XX
], xcoll
[i
][YY
], xcoll
[i
][ZZ
],
417 shifts
[i
][XX
], shifts
[i
][YY
], shifts
[i
][ZZ
],
418 eshifts
[i
][XX
], eshifts
[i
][YY
], eshifts
[i
][ZZ
]);
426 static void dump_edi_positions(FILE *out
, struct gmx_edx
*s
, const char name
[])
431 fprintf(out
, "#%s positions:\n%d\n", name
, s
->nr
);
437 fprintf(out
, "#index, x, y, z");
440 fprintf(out
, ", sqrt(m)");
442 for (i
= 0; i
< s
->nr
; i
++)
444 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
]);
447 fprintf(out
, "%9.3f", s
->sqrtm
[i
]);
455 static void dump_edi_eigenvecs(FILE *out
, t_eigvec
*ev
,
456 const char name
[], int length
)
461 fprintf(out
, "#%s eigenvectors:\n%d\n", name
, ev
->neig
);
462 /* Dump the data for every eigenvector: */
463 for (i
= 0; i
< ev
->neig
; i
++)
465 fprintf(out
, "EV %4d\ncomponents %d\nstepsize %f\nxproj %f\nfproj %f\nrefproj %f\nradius %f\nComponents:\n",
466 ev
->ieig
[i
], length
, ev
->stpsz
[i
], ev
->xproj
[i
], ev
->fproj
[i
], ev
->refproj
[i
], ev
->radius
);
467 for (j
= 0; j
< length
; j
++)
469 fprintf(out
, "%11.6f %11.6f %11.6f\n", ev
->vec
[i
][j
][XX
], ev
->vec
[i
][j
][YY
], ev
->vec
[i
][j
][ZZ
]);
476 static void dump_edi(t_edpar
*edpars
, t_commrec
*cr
, int nr_edi
)
482 sprintf(fn
, "EDdump_rank%d_edi%d", cr
->nodeid
, nr_edi
);
483 out
= gmx_ffopen(fn
, "w");
485 fprintf(out
, "#NINI\n %d\n#FITMAS\n %d\n#ANALYSIS_MAS\n %d\n",
486 edpars
->nini
, edpars
->fitmas
, edpars
->pcamas
);
487 fprintf(out
, "#OUTFRQ\n %d\n#MAXLEN\n %d\n#SLOPECRIT\n %f\n",
488 edpars
->outfrq
, edpars
->maxedsteps
, edpars
->slope
);
489 fprintf(out
, "#PRESTEPS\n %d\n#DELTA_F0\n %f\n#TAU\n %f\n#EFL_NULL\n %f\n#ALPHA2\n %f\n",
490 edpars
->presteps
, edpars
->flood
.deltaF0
, edpars
->flood
.tau
,
491 edpars
->flood
.constEfl
, edpars
->flood
.alpha2
);
493 /* Dump reference, average, target, origin positions */
494 dump_edi_positions(out
, &edpars
->sref
, "REFERENCE");
495 dump_edi_positions(out
, &edpars
->sav
, "AVERAGE" );
496 dump_edi_positions(out
, &edpars
->star
, "TARGET" );
497 dump_edi_positions(out
, &edpars
->sori
, "ORIGIN" );
499 /* Dump eigenvectors */
500 dump_edi_eigenvecs(out
, &edpars
->vecs
.mon
, "MONITORED", edpars
->sav
.nr
);
501 dump_edi_eigenvecs(out
, &edpars
->vecs
.linfix
, "LINFIX", edpars
->sav
.nr
);
502 dump_edi_eigenvecs(out
, &edpars
->vecs
.linacc
, "LINACC", edpars
->sav
.nr
);
503 dump_edi_eigenvecs(out
, &edpars
->vecs
.radfix
, "RADFIX", edpars
->sav
.nr
);
504 dump_edi_eigenvecs(out
, &edpars
->vecs
.radacc
, "RADACC", edpars
->sav
.nr
);
505 dump_edi_eigenvecs(out
, &edpars
->vecs
.radcon
, "RADCON", edpars
->sav
.nr
);
507 /* Dump flooding eigenvectors */
508 dump_edi_eigenvecs(out
, &edpars
->flood
.vecs
, "FLOODING", edpars
->sav
.nr
);
510 /* Dump ed local buffer */
511 fprintf(out
, "buf->do_edfit =%p\n", (void*)edpars
->buf
->do_edfit
);
512 fprintf(out
, "buf->do_edsam =%p\n", (void*)edpars
->buf
->do_edsam
);
513 fprintf(out
, "buf->do_radcon =%p\n", (void*)edpars
->buf
->do_radcon
);
520 static void dump_rotmat(FILE* out
, matrix rotmat
)
522 fprintf(out
, "ROTMAT: %12.8f %12.8f %12.8f\n", rotmat
[XX
][XX
], rotmat
[XX
][YY
], rotmat
[XX
][ZZ
]);
523 fprintf(out
, "ROTMAT: %12.8f %12.8f %12.8f\n", rotmat
[YY
][XX
], rotmat
[YY
][YY
], rotmat
[YY
][ZZ
]);
524 fprintf(out
, "ROTMAT: %12.8f %12.8f %12.8f\n", rotmat
[ZZ
][XX
], rotmat
[ZZ
][YY
], rotmat
[ZZ
][ZZ
]);
529 static void dump_rvec(FILE *out
, int dim
, rvec
*x
)
534 for (i
= 0; i
< dim
; i
++)
536 fprintf(out
, "%4d %f %f %f\n", i
, x
[i
][XX
], x
[i
][YY
], x
[i
][ZZ
]);
542 static void dump_mat(FILE* out
, int dim
, double** mat
)
547 fprintf(out
, "MATRIX:\n");
548 for (i
= 0; i
< dim
; i
++)
550 for (j
= 0; j
< dim
; j
++)
552 fprintf(out
, "%f ", mat
[i
][j
]);
565 static void do_edfit(int natoms
, rvec
*xp
, rvec
*x
, matrix R
, t_edpar
*edi
)
567 /* this is a copy of do_fit with some modifications */
568 int c
, r
, n
, j
, i
, irot
;
569 double d
[6], xnr
, xpc
;
574 struct t_do_edfit
*loc
;
577 if (edi
->buf
->do_edfit
!= nullptr)
584 snew(edi
->buf
->do_edfit
, 1);
586 loc
= edi
->buf
->do_edfit
;
590 snew(loc
->omega
, 2*DIM
);
591 snew(loc
->om
, 2*DIM
);
592 for (i
= 0; i
< 2*DIM
; i
++)
594 snew(loc
->omega
[i
], 2*DIM
);
595 snew(loc
->om
[i
], 2*DIM
);
599 for (i
= 0; (i
< 6); i
++)
602 for (j
= 0; (j
< 6); j
++)
604 loc
->omega
[i
][j
] = 0;
609 /* calculate the matrix U */
611 for (n
= 0; (n
< natoms
); n
++)
613 for (c
= 0; (c
< DIM
); c
++)
616 for (r
= 0; (r
< DIM
); r
++)
624 /* construct loc->omega */
625 /* loc->omega is symmetric -> loc->omega==loc->omega' */
626 for (r
= 0; (r
< 6); r
++)
628 for (c
= 0; (c
<= r
); c
++)
630 if ((r
>= 3) && (c
< 3))
632 loc
->omega
[r
][c
] = u
[r
-3][c
];
633 loc
->omega
[c
][r
] = u
[r
-3][c
];
637 loc
->omega
[r
][c
] = 0;
638 loc
->omega
[c
][r
] = 0;
643 /* determine h and k */
647 dump_mat(stderr
, 2*DIM
, loc
->omega
);
648 for (i
= 0; i
< 6; i
++)
650 fprintf(stderr
, "d[%d] = %f\n", i
, d
[i
]);
654 jacobi(loc
->omega
, 6, d
, loc
->om
, &irot
);
658 fprintf(stderr
, "IROT=0\n");
661 index
= 0; /* For the compiler only */
663 for (j
= 0; (j
< 3); j
++)
666 for (i
= 0; (i
< 6); i
++)
675 for (i
= 0; (i
< 3); i
++)
677 vh
[j
][i
] = M_SQRT2
*loc
->om
[i
][index
];
678 vk
[j
][i
] = M_SQRT2
*loc
->om
[i
+DIM
][index
];
683 for (c
= 0; (c
< 3); c
++)
685 for (r
= 0; (r
< 3); r
++)
687 R
[c
][r
] = vk
[0][r
]*vh
[0][c
]+
694 for (c
= 0; (c
< 3); c
++)
696 for (r
= 0; (r
< 3); r
++)
698 R
[c
][r
] = vk
[0][r
]*vh
[0][c
]+
707 static void rmfit(int nat
, rvec
*xcoll
, rvec transvec
, matrix rotmat
)
714 * The inverse rotation is described by the transposed rotation matrix */
715 transpose(rotmat
, tmat
);
716 rotate_x(xcoll
, nat
, tmat
);
718 /* Remove translation */
719 vec
[XX
] = -transvec
[XX
];
720 vec
[YY
] = -transvec
[YY
];
721 vec
[ZZ
] = -transvec
[ZZ
];
722 translate_x(xcoll
, nat
, vec
);
726 /**********************************************************************************
727 ******************** FLOODING ****************************************************
728 **********************************************************************************
730 The flooding ability was added later to edsam. Many of the edsam functionality could be reused for that purpose.
731 The flooding covariance matrix, i.e. the selected eigenvectors and their corresponding eigenvalues are
732 read as 7th Component Group. The eigenvalues are coded into the stepsize parameter (as used by -linfix or -linacc).
734 do_md clls right in the beginning the function init_edsam, which reads the edi file, saves all the necessary information in
735 the edi structure and calls init_flood, to initialise some extra fields in the edi->flood structure.
737 since the flooding acts on forces do_flood is called from the function force() (force.c), while the other
738 edsam functionality is hooked into md via the update() (update.c) function acting as constraint on positions.
740 do_flood makes a copy of the positions,
741 fits them, projects them computes flooding_energy, and flooding forces. The forces are computed in the
742 space of the eigenvectors and are then blown up to the full cartesian space and rotated back to remove the
743 fit. Then do_flood adds these forces to the forcefield-forces
744 (given as parameter) and updates the adaptive flooding parameters Efl and deltaF.
746 To center the flooding potential at a different location one can use the -ori option in make_edi. The ori
747 structure is projected to the system of eigenvectors and then this position in the subspace is used as
748 center of the flooding potential. If the option is not used, the center will be zero in the subspace,
749 i.e. the average structure as given in the make_edi file.
751 To use the flooding potential as restraint, make_edi has the option -restrain, which leads to inverted
752 signs of alpha2 and Efl, such that the sign in the exponential of Vfl is not inverted but the sign of
753 Vfl is inverted. Vfl = Efl * exp (- .../Efl/alpha2*x^2...) With tau>0 the negative Efl will grow slowly
754 so that the restraint is switched off slowly. When Efl==0 and inverted flooding is ON is reached no
755 further adaption is applied, Efl will stay constant at zero.
757 To use restraints with harmonic potentials switch -restrain and -harmonic. Then the eigenvalues are
758 used as spring constants for the harmonic potential.
759 Note that eq3 in the flooding paper (J. Comp. Chem. 2006, 27, 1693-1702) defines the parameter lambda \
760 as the inverse of the spring constant, whereas the implementation uses lambda as the spring constant.
762 To use more than one flooding matrix just concatenate several .edi files (cat flood1.edi flood2.edi > flood_all.edi)
763 the routine read_edi_file reads all of theses flooding files.
764 The structure t_edi is now organized as a list of t_edis and the function do_flood cycles through the list
765 calling the do_single_flood() routine for every single entry. Since every state variables have been kept in one
766 edi there is no interdependence whatsoever. The forces are added together.
768 To write energies into the .edr file, call the function
769 get_flood_enx_names(char**, int *nnames) to get the Header (Vfl1 Vfl2... Vfln)
771 get_flood_energies(real Vfl[],int nnames);
774 - 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.
776 Maybe one should give a range of atoms for which to remove motion, so that motion is removed with
777 two edsam files from two peptide chains
780 static void write_edo_flood(t_edpar
*edi
, FILE *fp
, real rmsd
)
785 /* Output how well we fit to the reference structure */
786 fprintf(fp
, EDcol_ffmt
, rmsd
);
788 for (i
= 0; i
< edi
->flood
.vecs
.neig
; i
++)
790 fprintf(fp
, EDcol_efmt
, edi
->flood
.vecs
.xproj
[i
]);
792 /* Check whether the reference projection changes with time (this can happen
793 * in case flooding is used as harmonic restraint). If so, output the
794 * current reference projection */
795 if (edi
->flood
.bHarmonic
&& edi
->flood
.vecs
.refprojslope
[i
] != 0.0)
797 fprintf(fp
, EDcol_efmt
, edi
->flood
.vecs
.refproj
[i
]);
800 /* Output Efl if we are doing adaptive flooding */
801 if (0 != edi
->flood
.tau
)
803 fprintf(fp
, EDcol_efmt
, edi
->flood
.Efl
);
805 fprintf(fp
, EDcol_efmt
, edi
->flood
.Vfl
);
807 /* Output deltaF if we are doing adaptive flooding */
808 if (0 != edi
->flood
.tau
)
810 fprintf(fp
, EDcol_efmt
, edi
->flood
.deltaF
);
812 fprintf(fp
, EDcol_efmt
, edi
->flood
.vecs
.fproj
[i
]);
817 /* From flood.xproj compute the Vfl(x) at this point */
818 static real
flood_energy(t_edpar
*edi
, gmx_int64_t step
)
820 /* compute flooding energy Vfl
821 Vfl = Efl * exp( - \frac {kT} {2Efl alpha^2} * sum_i { \lambda_i c_i^2 } )
822 \lambda_i is the reciprocal eigenvalue 1/\sigma_i
823 it is already computed by make_edi and stored in stpsz[i]
825 Vfl = - Efl * 1/2(sum _i {\frac 1{\lambda_i} c_i^2})
832 /* Each time this routine is called (i.e. each time step), we add a small
833 * value to the reference projection. This way a harmonic restraint towards
834 * a moving reference is realized. If no value for the additive constant
835 * is provided in the edi file, the reference will not change. */
836 if (edi
->flood
.bHarmonic
)
838 for (i
= 0; i
< edi
->flood
.vecs
.neig
; i
++)
840 edi
->flood
.vecs
.refproj
[i
] = edi
->flood
.vecs
.refproj0
[i
] + step
* edi
->flood
.vecs
.refprojslope
[i
];
845 /* Compute sum which will be the exponent of the exponential */
846 for (i
= 0; i
< edi
->flood
.vecs
.neig
; i
++)
848 /* stpsz stores the reciprocal eigenvalue 1/sigma_i */
849 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
]);
852 /* Compute the Gauss function*/
853 if (edi
->flood
.bHarmonic
)
855 Vfl
= -0.5*edi
->flood
.Efl
*sum
; /* minus sign because Efl is negative, if restrain is on. */
859 Vfl
= edi
->flood
.Efl
!= 0 ? edi
->flood
.Efl
*exp(-edi
->flood
.kT
/2/edi
->flood
.Efl
/edi
->flood
.alpha2
*sum
) : 0;
866 /* From the position and from Vfl compute forces in subspace -> store in edi->vec.flood.fproj */
867 static void flood_forces(t_edpar
*edi
)
869 /* compute the forces in the subspace of the flooding eigenvectors
870 * by the formula F_i= V_{fl}(c) * ( \frac {kT} {E_{fl}} \lambda_i c_i */
873 real energy
= edi
->flood
.Vfl
;
876 if (edi
->flood
.bHarmonic
)
878 for (i
= 0; i
< edi
->flood
.vecs
.neig
; i
++)
880 edi
->flood
.vecs
.fproj
[i
] = edi
->flood
.Efl
* edi
->flood
.vecs
.stpsz
[i
]*(edi
->flood
.vecs
.xproj
[i
]-edi
->flood
.vecs
.refproj
[i
]);
885 for (i
= 0; i
< edi
->flood
.vecs
.neig
; i
++)
887 /* if Efl is zero the forces are zero if not use the formula */
888 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;
894 /* Raise forces from subspace into cartesian space */
895 static void flood_blowup(t_edpar
*edi
, rvec
*forces_cart
)
897 /* this function lifts the forces from the subspace to the cartesian space
898 all the values not contained in the subspace are assumed to be zero and then
899 a coordinate transformation from eigenvector to cartesian vectors is performed
900 The nonexistent values don't have to be set to zero explicitly, they would occur
901 as zero valued summands, hence we just stop to compute this part of the sum.
903 for every atom we add all the contributions to this atom from all the different eigenvectors.
905 NOTE: one could add directly to the forcefield forces, would mean we wouldn't have to clear the
906 field forces_cart prior the computation, but we compute the forces separately
907 to have them accessible for diagnostics
914 forces_sub
= edi
->flood
.vecs
.fproj
;
917 /* Calculate the cartesian forces for the local atoms */
919 /* Clear forces first */
920 for (j
= 0; j
< edi
->sav
.nr_loc
; j
++)
922 clear_rvec(forces_cart
[j
]);
925 /* Now compute atomwise */
926 for (j
= 0; j
< edi
->sav
.nr_loc
; j
++)
928 /* Compute forces_cart[edi->sav.anrs[j]] */
929 for (eig
= 0; eig
< edi
->flood
.vecs
.neig
; eig
++)
931 /* Force vector is force * eigenvector (compute only atom j) */
932 svmul(forces_sub
[eig
], edi
->flood
.vecs
.vec
[eig
][edi
->sav
.c_ind
[j
]], dum
);
933 /* Add this vector to the cartesian forces */
934 rvec_inc(forces_cart
[j
], dum
);
940 /* Update the values of Efl, deltaF depending on tau and Vfl */
941 static void update_adaption(t_edpar
*edi
)
943 /* this function updates the parameter Efl and deltaF according to the rules given in
944 * 'predicting unimolecular chemical reactions: chemical flooding' M Mueller et al,
947 if ((edi
->flood
.tau
< 0 ? -edi
->flood
.tau
: edi
->flood
.tau
) > 0.00000001)
949 edi
->flood
.Efl
= edi
->flood
.Efl
+edi
->flood
.dt
/edi
->flood
.tau
*(edi
->flood
.deltaF0
-edi
->flood
.deltaF
);
950 /* check if restrain (inverted flooding) -> don't let EFL become positive */
951 if (edi
->flood
.alpha2
< 0 && edi
->flood
.Efl
> -0.00000001)
956 edi
->flood
.deltaF
= (1-edi
->flood
.dt
/edi
->flood
.tau
)*edi
->flood
.deltaF
+edi
->flood
.dt
/edi
->flood
.tau
*edi
->flood
.Vfl
;
961 static void do_single_flood(
969 gmx_bool bNS
) /* Are we in a neighbor searching step? */
972 matrix rotmat
; /* rotation matrix */
973 matrix tmat
; /* inverse rotation */
974 rvec transvec
; /* translation vector */
976 struct t_do_edsam
*buf
;
979 buf
= edi
->buf
->do_edsam
;
982 /* Broadcast the positions of the AVERAGE structure such that they are known on
983 * every processor. Each node contributes its local positions x and stores them in
984 * the collective ED array buf->xcoll */
985 communicate_group_positions(cr
, buf
->xcoll
, buf
->shifts_xcoll
, buf
->extra_shifts_xcoll
, bNS
, x
,
986 edi
->sav
.nr
, edi
->sav
.nr_loc
, edi
->sav
.anrs_loc
, edi
->sav
.c_ind
, edi
->sav
.x_old
, box
);
988 /* Only assembly REFERENCE positions if their indices differ from the average ones */
991 communicate_group_positions(cr
, buf
->xc_ref
, buf
->shifts_xc_ref
, buf
->extra_shifts_xc_ref
, bNS
, x
,
992 edi
->sref
.nr
, edi
->sref
.nr_loc
, edi
->sref
.anrs_loc
, edi
->sref
.c_ind
, edi
->sref
.x_old
, box
);
995 /* If bUpdateShifts was TRUE, the shifts have just been updated in get_positions.
996 * We do not need to update the shifts until the next NS step */
997 buf
->bUpdateShifts
= FALSE
;
999 /* Now all nodes have all of the ED/flooding positions in edi->sav->xcoll,
1000 * as well as the indices in edi->sav.anrs */
1002 /* Fit the reference indices to the reference structure */
1005 fit_to_reference(buf
->xcoll
, transvec
, rotmat
, edi
);
1009 fit_to_reference(buf
->xc_ref
, transvec
, rotmat
, edi
);
1012 /* Now apply the translation and rotation to the ED structure */
1013 translate_and_rotate(buf
->xcoll
, edi
->sav
.nr
, transvec
, rotmat
);
1015 /* Project fitted structure onto supbspace -> store in edi->flood.vecs.xproj */
1016 project_to_eigvectors(buf
->xcoll
, &edi
->flood
.vecs
, edi
);
1018 if (FALSE
== edi
->flood
.bConstForce
)
1020 /* Compute Vfl(x) from flood.xproj */
1021 edi
->flood
.Vfl
= flood_energy(edi
, step
);
1023 update_adaption(edi
);
1025 /* Compute the flooding forces */
1029 /* Translate them into cartesian positions */
1030 flood_blowup(edi
, edi
->flood
.forces_cartesian
);
1032 /* Rotate forces back so that they correspond to the given structure and not to the fitted one */
1033 /* Each node rotates back its local forces */
1034 transpose(rotmat
, tmat
);
1035 rotate_x(edi
->flood
.forces_cartesian
, edi
->sav
.nr_loc
, tmat
);
1037 /* Finally add forces to the main force variable */
1038 for (i
= 0; i
< edi
->sav
.nr_loc
; i
++)
1040 rvec_inc(force
[edi
->sav
.anrs_loc
[i
]], edi
->flood
.forces_cartesian
[i
]);
1043 /* Output is written by the master process */
1044 if (do_per_step(step
, edi
->outfrq
) && MASTER(cr
))
1046 /* Output how well we fit to the reference */
1049 /* Indices of reference and average structures are identical,
1050 * thus we can calculate the rmsd to SREF using xcoll */
1051 rmsdev
= rmsd_from_structure(buf
->xcoll
, &edi
->sref
);
1055 /* We have to translate & rotate the reference atoms first */
1056 translate_and_rotate(buf
->xc_ref
, edi
->sref
.nr
, transvec
, rotmat
);
1057 rmsdev
= rmsd_from_structure(buf
->xc_ref
, &edi
->sref
);
1060 write_edo_flood(edi
, edo
, rmsdev
);
1065 /* Main flooding routine, called from do_force */
1066 extern void do_flood(t_commrec
*cr
,
1067 const t_inputrec
*ir
,
1080 /* Write time to edo, when required. Output the time anyhow since we need
1081 * it in the output file for ED constraints. */
1082 if (MASTER(cr
) && do_per_step(step
, edi
->outfrq
))
1084 fprintf(ed
->edo
, "\n%12f", ir
->init_t
+ step
*ir
->delta_t
);
1087 if (ed
->eEDtype
!= eEDflood
)
1094 /* Call flooding for one matrix */
1095 if (edi
->flood
.vecs
.neig
)
1097 do_single_flood(ed
->edo
, x
, force
, edi
, step
, box
, cr
, bNS
);
1099 edi
= edi
->next_edi
;
1104 /* Called by init_edi, configure some flooding related variables and structures,
1105 * print headers to output files */
1106 static void init_flood(t_edpar
*edi
, gmx_edsam_t ed
, real dt
)
1111 edi
->flood
.Efl
= edi
->flood
.constEfl
;
1115 if (edi
->flood
.vecs
.neig
)
1117 /* If in any of the ED groups we find a flooding vector, flooding is turned on */
1118 ed
->eEDtype
= eEDflood
;
1120 fprintf(stderr
, "ED: Flooding %d eigenvector%s.\n", edi
->flood
.vecs
.neig
, edi
->flood
.vecs
.neig
> 1 ? "s" : "");
1122 if (edi
->flood
.bConstForce
)
1124 /* We have used stpsz as a vehicle to carry the fproj values for constant
1125 * force flooding. Now we copy that to flood.vecs.fproj. Note that
1126 * in const force flooding, fproj is never changed. */
1127 for (i
= 0; i
< edi
->flood
.vecs
.neig
; i
++)
1129 edi
->flood
.vecs
.fproj
[i
] = edi
->flood
.vecs
.stpsz
[i
];
1131 fprintf(stderr
, "ED: applying on eigenvector %d a constant force of %g\n",
1132 edi
->flood
.vecs
.ieig
[i
], edi
->flood
.vecs
.fproj
[i
]);
1140 /*********** Energy book keeping ******/
1141 static void get_flood_enx_names(t_edpar
*edi
, char** names
, int *nnames
) /* get header of energies */
1150 srenew(names
, count
);
1151 sprintf(buf
, "Vfl_%d", count
);
1152 names
[count
-1] = gmx_strdup(buf
);
1153 actual
= actual
->next_edi
;
1160 static void get_flood_energies(t_edpar
*edi
, real Vfl
[], int nnames
)
1162 /*fl has to be big enough to capture nnames-many entries*/
1171 Vfl
[count
-1] = actual
->flood
.Vfl
;
1172 actual
= actual
->next_edi
;
1175 if (nnames
!= count
-1)
1177 gmx_fatal(FARGS
, "Number of energies is not consistent with t_edi structure");
1180 /************* END of FLOODING IMPLEMENTATION ****************************/
1184 gmx_edsam_t
ed_open(int natoms
, edsamstate_t
**EDstatePtr
, int nfile
, const t_filenm fnm
[], unsigned long Flags
, const gmx_output_env_t
*oenv
, t_commrec
*cr
)
1190 /* Allocate space for the ED data structure */
1193 if (*EDstatePtr
== nullptr)
1195 snew(*EDstatePtr
, 1);
1197 edsamstate_t
*EDstate
= *EDstatePtr
;
1199 /* We want to perform ED (this switch might later be upgraded to eEDflood) */
1200 ed
->eEDtype
= eEDedsam
;
1204 fprintf(stderr
, "ED sampling will be performed!\n");
1207 /* Read the edi input file: */
1208 nED
= read_edi_file(ftp2fn(efEDI
, nfile
, fnm
), ed
->edpar
, natoms
);
1210 /* Make sure the checkpoint was produced in a run using this .edi file */
1211 if (EDstate
->bFromCpt
)
1213 crosscheck_edi_file_vs_checkpoint(ed
, EDstate
);
1219 init_edsamstate(ed
, EDstate
);
1221 /* The master opens the ED output file */
1222 /* TODO This file is never closed... */
1223 if (Flags
& MD_APPENDFILES
)
1225 ed
->edo
= gmx_fio_fopen(opt2fn("-eo", nfile
, fnm
), "a+");
1229 ed
->edo
= xvgropen(opt2fn("-eo", nfile
, fnm
),
1230 "Essential dynamics / flooding output",
1232 "RMSDs (nm), projections on EVs (nm), ...", oenv
);
1234 /* Make a descriptive legend */
1235 write_edo_legend(ed
, EDstate
->nED
, oenv
);
1242 /* Broadcasts the structure data */
1243 static void bc_ed_positions(t_commrec
*cr
, struct gmx_edx
*s
, int stype
)
1245 snew_bc(cr
, s
->anrs
, s
->nr
); /* Index numbers */
1246 snew_bc(cr
, s
->x
, s
->nr
); /* Positions */
1247 nblock_bc(cr
, s
->nr
, s
->anrs
);
1248 nblock_bc(cr
, s
->nr
, s
->x
);
1250 /* For the average & reference structures we need an array for the collective indices,
1251 * and we need to broadcast the masses as well */
1252 if (stype
== eedAV
|| stype
== eedREF
)
1254 /* We need these additional variables in the parallel case: */
1255 snew(s
->c_ind
, s
->nr
); /* Collective indices */
1256 /* Local atom indices get assigned in dd_make_local_group_indices.
1257 * There, also memory is allocated */
1258 s
->nalloc_loc
= 0; /* allocation size of s->anrs_loc */
1259 snew_bc(cr
, s
->x_old
, s
->nr
); /* To be able to always make the ED molecule whole, ... */
1260 nblock_bc(cr
, s
->nr
, s
->x_old
); /* ... keep track of shift changes with the help of old coords */
1263 /* broadcast masses for the reference structure (for mass-weighted fitting) */
1264 if (stype
== eedREF
)
1266 snew_bc(cr
, s
->m
, s
->nr
);
1267 nblock_bc(cr
, s
->nr
, s
->m
);
1270 /* For the average structure we might need the masses for mass-weighting */
1273 snew_bc(cr
, s
->sqrtm
, s
->nr
);
1274 nblock_bc(cr
, s
->nr
, s
->sqrtm
);
1275 snew_bc(cr
, s
->m
, s
->nr
);
1276 nblock_bc(cr
, s
->nr
, s
->m
);
1281 /* Broadcasts the eigenvector data */
1282 static void bc_ed_vecs(t_commrec
*cr
, t_eigvec
*ev
, int length
, gmx_bool bHarmonic
)
1286 snew_bc(cr
, ev
->ieig
, ev
->neig
); /* index numbers of eigenvector */
1287 snew_bc(cr
, ev
->stpsz
, ev
->neig
); /* stepsizes per eigenvector */
1288 snew_bc(cr
, ev
->xproj
, ev
->neig
); /* instantaneous x projection */
1289 snew_bc(cr
, ev
->fproj
, ev
->neig
); /* instantaneous f projection */
1290 snew_bc(cr
, ev
->refproj
, ev
->neig
); /* starting or target projection */
1292 nblock_bc(cr
, ev
->neig
, ev
->ieig
);
1293 nblock_bc(cr
, ev
->neig
, ev
->stpsz
);
1294 nblock_bc(cr
, ev
->neig
, ev
->xproj
);
1295 nblock_bc(cr
, ev
->neig
, ev
->fproj
);
1296 nblock_bc(cr
, ev
->neig
, ev
->refproj
);
1298 snew_bc(cr
, ev
->vec
, ev
->neig
); /* Eigenvector components */
1299 for (i
= 0; i
< ev
->neig
; i
++)
1301 snew_bc(cr
, ev
->vec
[i
], length
);
1302 nblock_bc(cr
, length
, ev
->vec
[i
]);
1305 /* For harmonic restraints the reference projections can change with time */
1308 snew_bc(cr
, ev
->refproj0
, ev
->neig
);
1309 snew_bc(cr
, ev
->refprojslope
, ev
->neig
);
1310 nblock_bc(cr
, ev
->neig
, ev
->refproj0
);
1311 nblock_bc(cr
, ev
->neig
, ev
->refprojslope
);
1316 /* Broadcasts the ED / flooding data to other nodes
1317 * and allocates memory where needed */
1318 static void broadcast_ed_data(t_commrec
*cr
, gmx_edsam_t ed
, int numedis
)
1324 /* Master lets the other nodes know if its ED only or also flooding */
1325 gmx_bcast(sizeof(ed
->eEDtype
), &(ed
->eEDtype
), cr
);
1327 snew_bc(cr
, ed
->edpar
, 1);
1328 /* Now transfer the ED data set(s) */
1330 for (nr
= 0; nr
< numedis
; nr
++)
1332 /* Broadcast a single ED data set */
1335 /* Broadcast positions */
1336 bc_ed_positions(cr
, &(edi
->sref
), eedREF
); /* reference positions (don't broadcast masses) */
1337 bc_ed_positions(cr
, &(edi
->sav
), eedAV
); /* average positions (do broadcast masses as well) */
1338 bc_ed_positions(cr
, &(edi
->star
), eedTAR
); /* target positions */
1339 bc_ed_positions(cr
, &(edi
->sori
), eedORI
); /* origin positions */
1341 /* Broadcast eigenvectors */
1342 bc_ed_vecs(cr
, &edi
->vecs
.mon
, edi
->sav
.nr
, FALSE
);
1343 bc_ed_vecs(cr
, &edi
->vecs
.linfix
, edi
->sav
.nr
, FALSE
);
1344 bc_ed_vecs(cr
, &edi
->vecs
.linacc
, edi
->sav
.nr
, FALSE
);
1345 bc_ed_vecs(cr
, &edi
->vecs
.radfix
, edi
->sav
.nr
, FALSE
);
1346 bc_ed_vecs(cr
, &edi
->vecs
.radacc
, edi
->sav
.nr
, FALSE
);
1347 bc_ed_vecs(cr
, &edi
->vecs
.radcon
, edi
->sav
.nr
, FALSE
);
1348 /* Broadcast flooding eigenvectors and, if needed, values for the moving reference */
1349 bc_ed_vecs(cr
, &edi
->flood
.vecs
, edi
->sav
.nr
, edi
->flood
.bHarmonic
);
1351 /* Set the pointer to the next ED group */
1354 snew_bc(cr
, edi
->next_edi
, 1);
1355 edi
= edi
->next_edi
;
1361 /* init-routine called for every *.edi-cycle, initialises t_edpar structure */
1362 static void init_edi(const gmx_mtop_t
*mtop
, t_edpar
*edi
)
1365 real totalmass
= 0.0;
1368 /* NOTE Init_edi is executed on the master process only
1369 * The initialized data sets are then transmitted to the
1370 * other nodes in broadcast_ed_data */
1372 /* evaluate masses (reference structure) */
1373 snew(edi
->sref
.m
, edi
->sref
.nr
);
1375 for (i
= 0; i
< edi
->sref
.nr
; i
++)
1379 edi
->sref
.m
[i
] = mtopGetAtomMass(mtop
, edi
->sref
.anrs
[i
], &molb
);
1383 edi
->sref
.m
[i
] = 1.0;
1386 /* Check that every m > 0. Bad things will happen otherwise. */
1387 if (edi
->sref
.m
[i
] <= 0.0)
1389 gmx_fatal(FARGS
, "Reference structure atom %d (sam.edi index %d) has a mass of %g.\n"
1390 "For a mass-weighted fit, all reference structure atoms need to have a mass >0.\n"
1391 "Either make the covariance analysis non-mass-weighted, or exclude massless\n"
1392 "atoms from the reference structure by creating a proper index group.\n",
1393 i
, edi
->sref
.anrs
[i
]+1, edi
->sref
.m
[i
]);
1396 totalmass
+= edi
->sref
.m
[i
];
1398 edi
->sref
.mtot
= totalmass
;
1400 /* Masses m and sqrt(m) for the average structure. Note that m
1401 * is needed if forces have to be evaluated in do_edsam */
1402 snew(edi
->sav
.sqrtm
, edi
->sav
.nr
);
1403 snew(edi
->sav
.m
, edi
->sav
.nr
);
1404 for (i
= 0; i
< edi
->sav
.nr
; i
++)
1406 edi
->sav
.m
[i
] = mtopGetAtomMass(mtop
, edi
->sav
.anrs
[i
], &molb
);
1409 edi
->sav
.sqrtm
[i
] = sqrt(edi
->sav
.m
[i
]);
1413 edi
->sav
.sqrtm
[i
] = 1.0;
1416 /* Check that every m > 0. Bad things will happen otherwise. */
1417 if (edi
->sav
.sqrtm
[i
] <= 0.0)
1419 gmx_fatal(FARGS
, "Average structure atom %d (sam.edi index %d) has a mass of %g.\n"
1420 "For ED with mass-weighting, all average structure atoms need to have a mass >0.\n"
1421 "Either make the covariance analysis non-mass-weighted, or exclude massless\n"
1422 "atoms from the average structure by creating a proper index group.\n",
1423 i
, edi
->sav
.anrs
[i
] + 1, edi
->sav
.m
[i
]);
1427 /* put reference structure in origin */
1428 get_center(edi
->sref
.x
, edi
->sref
.m
, edi
->sref
.nr
, com
);
1432 translate_x(edi
->sref
.x
, edi
->sref
.nr
, com
);
1434 /* Init ED buffer */
1439 static void check(const char *line
, const char *label
)
1441 if (!strstr(line
, label
))
1443 gmx_fatal(FARGS
, "Could not find input parameter %s at expected position in edsam input-file (.edi)\nline read instead is %s", label
, line
);
1448 static int read_checked_edint(FILE *file
, const char *label
)
1450 char line
[STRLEN
+1];
1453 fgets2 (line
, STRLEN
, file
);
1455 fgets2 (line
, STRLEN
, file
);
1456 sscanf (line
, max_ev_fmt_d
, &idum
);
1461 static int read_edint(FILE *file
, gmx_bool
*bEOF
)
1463 char line
[STRLEN
+1];
1468 eof
= fgets2 (line
, STRLEN
, file
);
1474 eof
= fgets2 (line
, STRLEN
, file
);
1480 sscanf (line
, max_ev_fmt_d
, &idum
);
1486 static real
read_checked_edreal(FILE *file
, const char *label
)
1488 char line
[STRLEN
+1];
1492 fgets2 (line
, STRLEN
, file
);
1494 fgets2 (line
, STRLEN
, file
);
1495 sscanf (line
, max_ev_fmt_lf
, &rdum
);
1496 return (real
) rdum
; /* always read as double and convert to single */
1500 static void read_edx(FILE *file
, int number
, int *anrs
, rvec
*x
)
1503 char line
[STRLEN
+1];
1507 for (i
= 0; i
< number
; i
++)
1509 fgets2 (line
, STRLEN
, file
);
1510 sscanf (line
, max_ev_fmt_dlflflf
, &anrs
[i
], &d
[0], &d
[1], &d
[2]);
1511 anrs
[i
]--; /* we are reading FORTRAN indices */
1512 for (j
= 0; j
< 3; j
++)
1514 x
[i
][j
] = d
[j
]; /* always read as double and convert to single */
1520 static void scan_edvec(FILE *in
, int nr
, rvec
*vec
)
1522 char line
[STRLEN
+1];
1527 for (i
= 0; (i
< nr
); i
++)
1529 fgets2 (line
, STRLEN
, in
);
1530 sscanf (line
, max_ev_fmt_lelele
, &x
, &y
, &z
);
1538 static void read_edvec(FILE *in
, int nr
, t_eigvec
*tvec
, gmx_bool bReadRefproj
, gmx_bool
*bHaveReference
)
1541 double rdum
, refproj_dum
= 0.0, refprojslope_dum
= 0.0;
1542 char line
[STRLEN
+1];
1545 tvec
->neig
= read_checked_edint(in
, "NUMBER OF EIGENVECTORS");
1548 snew(tvec
->ieig
, tvec
->neig
);
1549 snew(tvec
->stpsz
, tvec
->neig
);
1550 snew(tvec
->vec
, tvec
->neig
);
1551 snew(tvec
->xproj
, tvec
->neig
);
1552 snew(tvec
->fproj
, tvec
->neig
);
1553 snew(tvec
->refproj
, tvec
->neig
);
1556 snew(tvec
->refproj0
, tvec
->neig
);
1557 snew(tvec
->refprojslope
, tvec
->neig
);
1560 for (i
= 0; (i
< tvec
->neig
); i
++)
1562 fgets2 (line
, STRLEN
, in
);
1563 if (bReadRefproj
) /* ONLY when using flooding as harmonic restraint */
1565 nscan
= sscanf(line
, max_ev_fmt_dlflflf
, &idum
, &rdum
, &refproj_dum
, &refprojslope_dum
);
1566 /* Zero out values which were not scanned */
1570 /* Every 4 values read, including reference position */
1571 *bHaveReference
= TRUE
;
1574 /* A reference position is provided */
1575 *bHaveReference
= TRUE
;
1576 /* No value for slope, set to 0 */
1577 refprojslope_dum
= 0.0;
1580 /* No values for reference projection and slope, set to 0 */
1582 refprojslope_dum
= 0.0;
1585 gmx_fatal(FARGS
, "Expected 2 - 4 (not %d) values for flooding vec: <nr> <spring const> <refproj> <refproj-slope>\n", nscan
);
1588 tvec
->refproj
[i
] = refproj_dum
;
1589 tvec
->refproj0
[i
] = refproj_dum
;
1590 tvec
->refprojslope
[i
] = refprojslope_dum
;
1592 else /* Normal flooding */
1594 nscan
= sscanf(line
, max_ev_fmt_dlf
, &idum
, &rdum
);
1597 gmx_fatal(FARGS
, "Expected 2 values for flooding vec: <nr> <stpsz>\n");
1600 tvec
->ieig
[i
] = idum
;
1601 tvec
->stpsz
[i
] = rdum
;
1602 } /* end of loop over eigenvectors */
1604 for (i
= 0; (i
< tvec
->neig
); i
++)
1606 snew(tvec
->vec
[i
], nr
);
1607 scan_edvec(in
, nr
, tvec
->vec
[i
]);
1613 /* calls read_edvec for the vector groups, only for flooding there is an extra call */
1614 static void read_edvecs(FILE *in
, int nr
, t_edvecs
*vecs
)
1616 gmx_bool bHaveReference
= FALSE
;
1619 read_edvec(in
, nr
, &vecs
->mon
, FALSE
, &bHaveReference
);
1620 read_edvec(in
, nr
, &vecs
->linfix
, FALSE
, &bHaveReference
);
1621 read_edvec(in
, nr
, &vecs
->linacc
, FALSE
, &bHaveReference
);
1622 read_edvec(in
, nr
, &vecs
->radfix
, FALSE
, &bHaveReference
);
1623 read_edvec(in
, nr
, &vecs
->radacc
, FALSE
, &bHaveReference
);
1624 read_edvec(in
, nr
, &vecs
->radcon
, FALSE
, &bHaveReference
);
1628 /* Check if the same atom indices are used for reference and average positions */
1629 static gmx_bool
check_if_same(struct gmx_edx sref
, struct gmx_edx sav
)
1634 /* If the number of atoms differs between the two structures,
1635 * they cannot be identical */
1636 if (sref
.nr
!= sav
.nr
)
1641 /* Now that we know that both stuctures have the same number of atoms,
1642 * check if also the indices are identical */
1643 for (i
= 0; i
< sav
.nr
; i
++)
1645 if (sref
.anrs
[i
] != sav
.anrs
[i
])
1650 fprintf(stderr
, "ED: Note: Reference and average structure are composed of the same atom indices.\n");
1656 static int read_edi(FILE* in
, t_edpar
*edi
, int nr_mdatoms
, const char *fn
)
1659 const int magic
= 670;
1662 /* Was a specific reference point for the flooding/umbrella potential provided in the edi file? */
1663 gmx_bool bHaveReference
= FALSE
;
1666 /* the edi file is not free format, so expect problems if the input is corrupt. */
1668 /* check the magic number */
1669 readmagic
= read_edint(in
, &bEOF
);
1670 /* Check whether we have reached the end of the input file */
1676 if (readmagic
!= magic
)
1678 if (readmagic
== 666 || readmagic
== 667 || readmagic
== 668)
1680 gmx_fatal(FARGS
, "Wrong magic number: Use newest version of make_edi to produce edi file");
1682 else if (readmagic
!= 669)
1684 gmx_fatal(FARGS
, "Wrong magic number %d in %s", readmagic
, fn
);
1688 /* check the number of atoms */
1689 edi
->nini
= read_edint(in
, &bEOF
);
1690 if (edi
->nini
!= nr_mdatoms
)
1692 gmx_fatal(FARGS
, "Nr of atoms in %s (%d) does not match nr of md atoms (%d)", fn
, edi
->nini
, nr_mdatoms
);
1695 /* Done checking. For the rest we blindly trust the input */
1696 edi
->fitmas
= read_checked_edint(in
, "FITMAS");
1697 edi
->pcamas
= read_checked_edint(in
, "ANALYSIS_MAS");
1698 edi
->outfrq
= read_checked_edint(in
, "OUTFRQ");
1699 edi
->maxedsteps
= read_checked_edint(in
, "MAXLEN");
1700 edi
->slope
= read_checked_edreal(in
, "SLOPECRIT");
1702 edi
->presteps
= read_checked_edint(in
, "PRESTEPS");
1703 edi
->flood
.deltaF0
= read_checked_edreal(in
, "DELTA_F0");
1704 edi
->flood
.deltaF
= read_checked_edreal(in
, "INIT_DELTA_F");
1705 edi
->flood
.tau
= read_checked_edreal(in
, "TAU");
1706 edi
->flood
.constEfl
= read_checked_edreal(in
, "EFL_NULL");
1707 edi
->flood
.alpha2
= read_checked_edreal(in
, "ALPHA2");
1708 edi
->flood
.kT
= read_checked_edreal(in
, "KT");
1709 edi
->flood
.bHarmonic
= read_checked_edint(in
, "HARMONIC");
1710 if (readmagic
> 669)
1712 edi
->flood
.bConstForce
= read_checked_edint(in
, "CONST_FORCE_FLOODING");
1716 edi
->flood
.bConstForce
= FALSE
;
1718 edi
->sref
.nr
= read_checked_edint(in
, "NREF");
1720 /* allocate space for reference positions and read them */
1721 snew(edi
->sref
.anrs
, edi
->sref
.nr
);
1722 snew(edi
->sref
.x
, edi
->sref
.nr
);
1723 snew(edi
->sref
.x_old
, edi
->sref
.nr
);
1724 edi
->sref
.sqrtm
= nullptr;
1725 read_edx(in
, edi
->sref
.nr
, edi
->sref
.anrs
, edi
->sref
.x
);
1727 /* average positions. they define which atoms will be used for ED sampling */
1728 edi
->sav
.nr
= read_checked_edint(in
, "NAV");
1729 snew(edi
->sav
.anrs
, edi
->sav
.nr
);
1730 snew(edi
->sav
.x
, edi
->sav
.nr
);
1731 snew(edi
->sav
.x_old
, edi
->sav
.nr
);
1732 read_edx(in
, edi
->sav
.nr
, edi
->sav
.anrs
, edi
->sav
.x
);
1734 /* Check if the same atom indices are used for reference and average positions */
1735 edi
->bRefEqAv
= check_if_same(edi
->sref
, edi
->sav
);
1738 read_edvecs(in
, edi
->sav
.nr
, &edi
->vecs
);
1739 read_edvec(in
, edi
->sav
.nr
, &edi
->flood
.vecs
, edi
->flood
.bHarmonic
, &bHaveReference
);
1741 /* target positions */
1742 edi
->star
.nr
= read_edint(in
, &bEOF
);
1743 if (edi
->star
.nr
> 0)
1745 snew(edi
->star
.anrs
, edi
->star
.nr
);
1746 snew(edi
->star
.x
, edi
->star
.nr
);
1747 edi
->star
.sqrtm
= nullptr;
1748 read_edx(in
, edi
->star
.nr
, edi
->star
.anrs
, edi
->star
.x
);
1751 /* positions defining origin of expansion circle */
1752 edi
->sori
.nr
= read_edint(in
, &bEOF
);
1753 if (edi
->sori
.nr
> 0)
1757 /* Both an -ori structure and a at least one manual reference point have been
1758 * specified. That's ambiguous and probably not intentional. */
1759 gmx_fatal(FARGS
, "ED: An origin structure has been provided and a at least one (moving) reference\n"
1760 " point was manually specified in the edi file. That is ambiguous. Aborting.\n");
1762 snew(edi
->sori
.anrs
, edi
->sori
.nr
);
1763 snew(edi
->sori
.x
, edi
->sori
.nr
);
1764 edi
->sori
.sqrtm
= nullptr;
1765 read_edx(in
, edi
->sori
.nr
, edi
->sori
.anrs
, edi
->sori
.x
);
1774 /* Read in the edi input file. Note that it may contain several ED data sets which were
1775 * achieved by concatenating multiple edi files. The standard case would be a single ED
1776 * data set, though. */
1777 static int read_edi_file(const char *fn
, t_edpar
*edi
, int nr_mdatoms
)
1780 t_edpar
*curr_edi
, *last_edi
;
1785 /* This routine is executed on the master only */
1787 /* Open the .edi parameter input file */
1788 in
= gmx_fio_fopen(fn
, "r");
1789 fprintf(stderr
, "ED: Reading edi file %s\n", fn
);
1791 /* Now read a sequence of ED input parameter sets from the edi file */
1794 while (read_edi(in
, curr_edi
, nr_mdatoms
, fn
) )
1798 /* Since we arrived within this while loop we know that there is still another data set to be read in */
1799 /* We need to allocate space for the data: */
1801 /* Point the 'next_edi' entry to the next edi: */
1802 curr_edi
->next_edi
= edi_read
;
1803 /* Keep the curr_edi pointer for the case that the next group is empty: */
1804 last_edi
= curr_edi
;
1805 /* Let's prepare to read in the next edi data set: */
1806 curr_edi
= edi_read
;
1810 gmx_fatal(FARGS
, "No complete ED data set found in edi file %s.", fn
);
1813 /* Terminate the edi group list with a NULL pointer: */
1814 last_edi
->next_edi
= nullptr;
1816 fprintf(stderr
, "ED: Found %d ED group%s.\n", edi_nr
, edi_nr
> 1 ? "s" : "");
1818 /* Close the .edi file again */
1825 struct t_fit_to_ref
{
1826 rvec
*xcopy
; /* Working copy of the positions in fit_to_reference */
1829 /* Fit the current positions to the reference positions
1830 * Do not actually do the fit, just return rotation and translation.
1831 * Note that the COM of the reference structure was already put into
1832 * the origin by init_edi. */
1833 static void fit_to_reference(rvec
*xcoll
, /* The positions to be fitted */
1834 rvec transvec
, /* The translation vector */
1835 matrix rotmat
, /* The rotation matrix */
1836 t_edpar
*edi
) /* Just needed for do_edfit */
1838 rvec com
; /* center of mass */
1840 struct t_fit_to_ref
*loc
;
1843 /* Allocate memory the first time this routine is called for each edi group */
1844 if (nullptr == edi
->buf
->fit_to_ref
)
1846 snew(edi
->buf
->fit_to_ref
, 1);
1847 snew(edi
->buf
->fit_to_ref
->xcopy
, edi
->sref
.nr
);
1849 loc
= edi
->buf
->fit_to_ref
;
1851 /* We do not touch the original positions but work on a copy. */
1852 for (i
= 0; i
< edi
->sref
.nr
; i
++)
1854 copy_rvec(xcoll
[i
], loc
->xcopy
[i
]);
1857 /* Calculate the center of mass */
1858 get_center(loc
->xcopy
, edi
->sref
.m
, edi
->sref
.nr
, com
);
1860 transvec
[XX
] = -com
[XX
];
1861 transvec
[YY
] = -com
[YY
];
1862 transvec
[ZZ
] = -com
[ZZ
];
1864 /* Subtract the center of mass from the copy */
1865 translate_x(loc
->xcopy
, edi
->sref
.nr
, transvec
);
1867 /* Determine the rotation matrix */
1868 do_edfit(edi
->sref
.nr
, edi
->sref
.x
, loc
->xcopy
, rotmat
, edi
);
1872 static void translate_and_rotate(rvec
*x
, /* The positions to be translated and rotated */
1873 int nat
, /* How many positions are there? */
1874 rvec transvec
, /* The translation vector */
1875 matrix rotmat
) /* The rotation matrix */
1878 translate_x(x
, nat
, transvec
);
1881 rotate_x(x
, nat
, rotmat
);
1885 /* Gets the rms deviation of the positions to the structure s */
1886 /* fit_to_structure has to be called before calling this routine! */
1887 static real
rmsd_from_structure(rvec
*x
, /* The positions under consideration */
1888 struct gmx_edx
*s
) /* The structure from which the rmsd shall be computed */
1894 for (i
= 0; i
< s
->nr
; i
++)
1896 rmsd
+= distance2(s
->x
[i
], x
[i
]);
1899 rmsd
/= (real
) s
->nr
;
1906 void dd_make_local_ed_indices(gmx_domdec_t
*dd
, struct gmx_edsam
*ed
)
1911 if (ed
->eEDtype
!= eEDnone
)
1913 /* Loop over ED groups */
1917 /* Local atoms of the reference structure (for fitting), need only be assembled
1918 * if their indices differ from the average ones */
1921 dd_make_local_group_indices(dd
->ga2la
, edi
->sref
.nr
, edi
->sref
.anrs
,
1922 &edi
->sref
.nr_loc
, &edi
->sref
.anrs_loc
, &edi
->sref
.nalloc_loc
, edi
->sref
.c_ind
);
1925 /* Local atoms of the average structure (on these ED will be performed) */
1926 dd_make_local_group_indices(dd
->ga2la
, edi
->sav
.nr
, edi
->sav
.anrs
,
1927 &edi
->sav
.nr_loc
, &edi
->sav
.anrs_loc
, &edi
->sav
.nalloc_loc
, edi
->sav
.c_ind
);
1929 /* Indicate that the ED shift vectors for this structure need to be updated
1930 * at the next call to communicate_group_positions, since obviously we are in a NS step */
1931 edi
->buf
->do_edsam
->bUpdateShifts
= TRUE
;
1933 /* Set the pointer to the next ED group (if any) */
1934 edi
= edi
->next_edi
;
1940 static gmx_inline
void ed_unshift_single_coord(matrix box
, const rvec x
, const ivec is
, rvec xu
)
1951 xu
[XX
] = x
[XX
]-tx
*box
[XX
][XX
]-ty
*box
[YY
][XX
]-tz
*box
[ZZ
][XX
];
1952 xu
[YY
] = x
[YY
]-ty
*box
[YY
][YY
]-tz
*box
[ZZ
][YY
];
1953 xu
[ZZ
] = x
[ZZ
]-tz
*box
[ZZ
][ZZ
];
1957 xu
[XX
] = x
[XX
]-tx
*box
[XX
][XX
];
1958 xu
[YY
] = x
[YY
]-ty
*box
[YY
][YY
];
1959 xu
[ZZ
] = x
[ZZ
]-tz
*box
[ZZ
][ZZ
];
1964 static void do_linfix(rvec
*xcoll
, t_edpar
*edi
, gmx_int64_t step
)
1971 /* loop over linfix vectors */
1972 for (i
= 0; i
< edi
->vecs
.linfix
.neig
; i
++)
1974 /* calculate the projection */
1975 proj
= projectx(edi
, xcoll
, edi
->vecs
.linfix
.vec
[i
]);
1977 /* calculate the correction */
1978 add
= edi
->vecs
.linfix
.refproj
[i
] + step
*edi
->vecs
.linfix
.stpsz
[i
] - proj
;
1980 /* apply the correction */
1981 add
/= edi
->sav
.sqrtm
[i
];
1982 for (j
= 0; j
< edi
->sav
.nr
; j
++)
1984 svmul(add
, edi
->vecs
.linfix
.vec
[i
][j
], vec_dum
);
1985 rvec_inc(xcoll
[j
], vec_dum
);
1991 static void do_linacc(rvec
*xcoll
, t_edpar
*edi
)
1998 /* loop over linacc vectors */
1999 for (i
= 0; i
< edi
->vecs
.linacc
.neig
; i
++)
2001 /* calculate the projection */
2002 proj
= projectx(edi
, xcoll
, edi
->vecs
.linacc
.vec
[i
]);
2004 /* calculate the correction */
2006 if (edi
->vecs
.linacc
.stpsz
[i
] > 0.0)
2008 if ((proj
-edi
->vecs
.linacc
.refproj
[i
]) < 0.0)
2010 add
= edi
->vecs
.linacc
.refproj
[i
] - proj
;
2013 if (edi
->vecs
.linacc
.stpsz
[i
] < 0.0)
2015 if ((proj
-edi
->vecs
.linacc
.refproj
[i
]) > 0.0)
2017 add
= edi
->vecs
.linacc
.refproj
[i
] - proj
;
2021 /* apply the correction */
2022 add
/= edi
->sav
.sqrtm
[i
];
2023 for (j
= 0; j
< edi
->sav
.nr
; j
++)
2025 svmul(add
, edi
->vecs
.linacc
.vec
[i
][j
], vec_dum
);
2026 rvec_inc(xcoll
[j
], vec_dum
);
2029 /* new positions will act as reference */
2030 edi
->vecs
.linacc
.refproj
[i
] = proj
+ add
;
2035 static void do_radfix(rvec
*xcoll
, t_edpar
*edi
)
2038 real
*proj
, rad
= 0.0, ratio
;
2042 if (edi
->vecs
.radfix
.neig
== 0)
2047 snew(proj
, edi
->vecs
.radfix
.neig
);
2049 /* loop over radfix vectors */
2050 for (i
= 0; i
< edi
->vecs
.radfix
.neig
; i
++)
2052 /* calculate the projections, radius */
2053 proj
[i
] = projectx(edi
, xcoll
, edi
->vecs
.radfix
.vec
[i
]);
2054 rad
+= gmx::square(proj
[i
] - edi
->vecs
.radfix
.refproj
[i
]);
2058 ratio
= (edi
->vecs
.radfix
.stpsz
[0]+edi
->vecs
.radfix
.radius
)/rad
- 1.0;
2059 edi
->vecs
.radfix
.radius
+= edi
->vecs
.radfix
.stpsz
[0];
2061 /* loop over radfix vectors */
2062 for (i
= 0; i
< edi
->vecs
.radfix
.neig
; i
++)
2064 proj
[i
] -= edi
->vecs
.radfix
.refproj
[i
];
2066 /* apply the correction */
2067 proj
[i
] /= edi
->sav
.sqrtm
[i
];
2069 for (j
= 0; j
< edi
->sav
.nr
; j
++)
2071 svmul(proj
[i
], edi
->vecs
.radfix
.vec
[i
][j
], vec_dum
);
2072 rvec_inc(xcoll
[j
], vec_dum
);
2080 static void do_radacc(rvec
*xcoll
, t_edpar
*edi
)
2083 real
*proj
, rad
= 0.0, ratio
= 0.0;
2087 if (edi
->vecs
.radacc
.neig
== 0)
2092 snew(proj
, edi
->vecs
.radacc
.neig
);
2094 /* loop over radacc vectors */
2095 for (i
= 0; i
< edi
->vecs
.radacc
.neig
; i
++)
2097 /* calculate the projections, radius */
2098 proj
[i
] = projectx(edi
, xcoll
, edi
->vecs
.radacc
.vec
[i
]);
2099 rad
+= gmx::square(proj
[i
] - edi
->vecs
.radacc
.refproj
[i
]);
2103 /* only correct when radius decreased */
2104 if (rad
< edi
->vecs
.radacc
.radius
)
2106 ratio
= edi
->vecs
.radacc
.radius
/rad
- 1.0;
2110 edi
->vecs
.radacc
.radius
= rad
;
2113 /* loop over radacc vectors */
2114 for (i
= 0; i
< edi
->vecs
.radacc
.neig
; i
++)
2116 proj
[i
] -= edi
->vecs
.radacc
.refproj
[i
];
2118 /* apply the correction */
2119 proj
[i
] /= edi
->sav
.sqrtm
[i
];
2121 for (j
= 0; j
< edi
->sav
.nr
; j
++)
2123 svmul(proj
[i
], edi
->vecs
.radacc
.vec
[i
][j
], vec_dum
);
2124 rvec_inc(xcoll
[j
], vec_dum
);
2131 struct t_do_radcon
{
2135 static void do_radcon(rvec
*xcoll
, t_edpar
*edi
)
2138 real rad
= 0.0, ratio
= 0.0;
2139 struct t_do_radcon
*loc
;
2144 if (edi
->buf
->do_radcon
!= nullptr)
2151 snew(edi
->buf
->do_radcon
, 1);
2153 loc
= edi
->buf
->do_radcon
;
2155 if (edi
->vecs
.radcon
.neig
== 0)
2162 snew(loc
->proj
, edi
->vecs
.radcon
.neig
);
2165 /* loop over radcon vectors */
2166 for (i
= 0; i
< edi
->vecs
.radcon
.neig
; i
++)
2168 /* calculate the projections, radius */
2169 loc
->proj
[i
] = projectx(edi
, xcoll
, edi
->vecs
.radcon
.vec
[i
]);
2170 rad
+= gmx::square(loc
->proj
[i
] - edi
->vecs
.radcon
.refproj
[i
]);
2173 /* only correct when radius increased */
2174 if (rad
> edi
->vecs
.radcon
.radius
)
2176 ratio
= edi
->vecs
.radcon
.radius
/rad
- 1.0;
2178 /* loop over radcon vectors */
2179 for (i
= 0; i
< edi
->vecs
.radcon
.neig
; i
++)
2181 /* apply the correction */
2182 loc
->proj
[i
] -= edi
->vecs
.radcon
.refproj
[i
];
2183 loc
->proj
[i
] /= edi
->sav
.sqrtm
[i
];
2184 loc
->proj
[i
] *= ratio
;
2186 for (j
= 0; j
< edi
->sav
.nr
; j
++)
2188 svmul(loc
->proj
[i
], edi
->vecs
.radcon
.vec
[i
][j
], vec_dum
);
2189 rvec_inc(xcoll
[j
], vec_dum
);
2196 edi
->vecs
.radcon
.radius
= rad
;
2202 static void ed_apply_constraints(rvec
*xcoll
, t_edpar
*edi
, gmx_int64_t step
)
2207 /* subtract the average positions */
2208 for (i
= 0; i
< edi
->sav
.nr
; i
++)
2210 rvec_dec(xcoll
[i
], edi
->sav
.x
[i
]);
2213 /* apply the constraints */
2216 do_linfix(xcoll
, edi
, step
);
2218 do_linacc(xcoll
, edi
);
2221 do_radfix(xcoll
, edi
);
2223 do_radacc(xcoll
, edi
);
2224 do_radcon(xcoll
, edi
);
2226 /* add back the average positions */
2227 for (i
= 0; i
< edi
->sav
.nr
; i
++)
2229 rvec_inc(xcoll
[i
], edi
->sav
.x
[i
]);
2234 /* Write out the projections onto the eigenvectors. The order of output
2235 * corresponds to ed_output_legend() */
2236 static void write_edo(t_edpar
*edi
, FILE *fp
, real rmsd
)
2241 /* Output how well we fit to the reference structure */
2242 fprintf(fp
, EDcol_ffmt
, rmsd
);
2244 for (i
= 0; i
< edi
->vecs
.mon
.neig
; i
++)
2246 fprintf(fp
, EDcol_efmt
, edi
->vecs
.mon
.xproj
[i
]);
2249 for (i
= 0; i
< edi
->vecs
.linfix
.neig
; i
++)
2251 fprintf(fp
, EDcol_efmt
, edi
->vecs
.linfix
.xproj
[i
]);
2254 for (i
= 0; i
< edi
->vecs
.linacc
.neig
; i
++)
2256 fprintf(fp
, EDcol_efmt
, edi
->vecs
.linacc
.xproj
[i
]);
2259 for (i
= 0; i
< edi
->vecs
.radfix
.neig
; i
++)
2261 fprintf(fp
, EDcol_efmt
, edi
->vecs
.radfix
.xproj
[i
]);
2263 if (edi
->vecs
.radfix
.neig
)
2265 fprintf(fp
, EDcol_ffmt
, calc_radius(&edi
->vecs
.radfix
)); /* fixed increment radius */
2268 for (i
= 0; i
< edi
->vecs
.radacc
.neig
; i
++)
2270 fprintf(fp
, EDcol_efmt
, edi
->vecs
.radacc
.xproj
[i
]);
2272 if (edi
->vecs
.radacc
.neig
)
2274 fprintf(fp
, EDcol_ffmt
, calc_radius(&edi
->vecs
.radacc
)); /* acceptance radius */
2277 for (i
= 0; i
< edi
->vecs
.radcon
.neig
; i
++)
2279 fprintf(fp
, EDcol_efmt
, edi
->vecs
.radcon
.xproj
[i
]);
2281 if (edi
->vecs
.radcon
.neig
)
2283 fprintf(fp
, EDcol_ffmt
, calc_radius(&edi
->vecs
.radcon
)); /* contracting radius */
2287 /* Returns if any constraints are switched on */
2288 static int ed_constraints(gmx_bool edtype
, t_edpar
*edi
)
2290 if (edtype
== eEDedsam
|| edtype
== eEDflood
)
2292 return (edi
->vecs
.linfix
.neig
|| edi
->vecs
.linacc
.neig
||
2293 edi
->vecs
.radfix
.neig
|| edi
->vecs
.radacc
.neig
||
2294 edi
->vecs
.radcon
.neig
);
2300 /* Copies reference projection 'refproj' to fixed 'refproj0' variable for flooding/
2301 * umbrella sampling simulations. */
2302 static void copyEvecReference(t_eigvec
* floodvecs
)
2307 if (nullptr == floodvecs
->refproj0
)
2309 snew(floodvecs
->refproj0
, floodvecs
->neig
);
2312 for (i
= 0; i
< floodvecs
->neig
; i
++)
2314 floodvecs
->refproj0
[i
] = floodvecs
->refproj
[i
];
2319 /* Call on MASTER only. Check whether the essential dynamics / flooding
2320 * groups of the checkpoint file are consistent with the provided .edi file. */
2321 static void crosscheck_edi_file_vs_checkpoint(gmx_edsam_t ed
, edsamstate_t
*EDstate
)
2323 t_edpar
*edi
= nullptr; /* points to a single edi data set */
2327 if (nullptr == EDstate
->nref
|| nullptr == EDstate
->nav
)
2329 gmx_fatal(FARGS
, "Essential dynamics and flooding can only be switched on (or off) at the\n"
2330 "start of a new simulation. If a simulation runs with/without ED constraints,\n"
2331 "it must also continue with/without ED constraints when checkpointing.\n"
2332 "To switch on (or off) ED constraints, please prepare a new .tpr to start\n"
2333 "from without a checkpoint.\n");
2338 while (edi
!= nullptr)
2340 /* Check number of atoms in the reference and average structures */
2341 if (EDstate
->nref
[edinum
] != edi
->sref
.nr
)
2343 gmx_fatal(FARGS
, "The number of reference structure atoms in ED group %c is\n"
2344 "not the same in .cpt (NREF=%d) and .edi (NREF=%d) files!\n",
2345 get_EDgroupChar(edinum
+1, 0), EDstate
->nref
[edinum
], edi
->sref
.nr
);
2347 if (EDstate
->nav
[edinum
] != edi
->sav
.nr
)
2349 gmx_fatal(FARGS
, "The number of average structure atoms in ED group %c is\n"
2350 "not the same in .cpt (NREF=%d) and .edi (NREF=%d) files!\n",
2351 get_EDgroupChar(edinum
+1, 0), EDstate
->nav
[edinum
], edi
->sav
.nr
);
2353 edi
= edi
->next_edi
;
2357 if (edinum
!= EDstate
->nED
)
2359 gmx_fatal(FARGS
, "The number of essential dynamics / flooding groups is not consistent.\n"
2360 "There are %d ED groups in the .cpt file, but %d in the .edi file!\n"
2361 "Are you sure this is the correct .edi file?\n", EDstate
->nED
, edinum
);
2366 /* The edsamstate struct stores the information we need to make the ED group
2367 * whole again after restarts from a checkpoint file. Here we do the following:
2368 * a) If we did not start from .cpt, we prepare the struct for proper .cpt writing,
2369 * b) if we did start from .cpt, we copy over the last whole structures from .cpt,
2370 * c) in any case, for subsequent checkpoint writing, we set the pointers in
2371 * edsamstate to the x_old arrays, which contain the correct PBC representation of
2372 * all ED structures at the last time step. */
2373 static void init_edsamstate(gmx_edsam_t ed
, edsamstate_t
*EDstate
)
2379 snew(EDstate
->old_sref_p
, EDstate
->nED
);
2380 snew(EDstate
->old_sav_p
, EDstate
->nED
);
2382 /* If we did not read in a .cpt file, these arrays are not yet allocated */
2383 if (!EDstate
->bFromCpt
)
2385 snew(EDstate
->nref
, EDstate
->nED
);
2386 snew(EDstate
->nav
, EDstate
->nED
);
2389 /* Loop over all ED/flooding data sets (usually only one, though) */
2391 for (nr_edi
= 1; nr_edi
<= EDstate
->nED
; nr_edi
++)
2393 /* We always need the last reference and average positions such that
2394 * in the next time step we can make the ED group whole again
2395 * if the atoms do not have the correct PBC representation */
2396 if (EDstate
->bFromCpt
)
2398 /* Copy the last whole positions of reference and average group from .cpt */
2399 for (i
= 0; i
< edi
->sref
.nr
; i
++)
2401 copy_rvec(EDstate
->old_sref
[nr_edi
-1][i
], edi
->sref
.x_old
[i
]);
2403 for (i
= 0; i
< edi
->sav
.nr
; i
++)
2405 copy_rvec(EDstate
->old_sav
[nr_edi
-1][i
], edi
->sav
.x_old
[i
]);
2410 EDstate
->nref
[nr_edi
-1] = edi
->sref
.nr
;
2411 EDstate
->nav
[nr_edi
-1] = edi
->sav
.nr
;
2414 /* For subsequent checkpoint writing, set the edsamstate pointers to the edi arrays: */
2415 EDstate
->old_sref_p
[nr_edi
-1] = edi
->sref
.x_old
;
2416 EDstate
->old_sav_p
[nr_edi
-1] = edi
->sav
.x_old
;
2418 edi
= edi
->next_edi
;
2423 /* Adds 'buf' to 'str' */
2424 static void add_to_string(char **str
, char *buf
)
2429 len
= strlen(*str
) + strlen(buf
) + 1;
2435 static void add_to_string_aligned(char **str
, char *buf
)
2437 char buf_aligned
[STRLEN
];
2439 sprintf(buf_aligned
, EDcol_sfmt
, buf
);
2440 add_to_string(str
, buf_aligned
);
2444 static void nice_legend(const char ***setname
, int *nsets
, char **LegendStr
, const char *value
, const char *unit
, char EDgroupchar
)
2446 char tmp
[STRLEN
], tmp2
[STRLEN
];
2449 sprintf(tmp
, "%c %s", EDgroupchar
, value
);
2450 add_to_string_aligned(LegendStr
, tmp
);
2451 sprintf(tmp2
, "%s (%s)", tmp
, unit
);
2452 (*setname
)[*nsets
] = gmx_strdup(tmp2
);
2457 static void nice_legend_evec(const char ***setname
, int *nsets
, char **LegendStr
, t_eigvec
*evec
, char EDgroupChar
, const char *EDtype
)
2463 for (i
= 0; i
< evec
->neig
; i
++)
2465 sprintf(tmp
, "EV%dprj%s", evec
->ieig
[i
], EDtype
);
2466 nice_legend(setname
, nsets
, LegendStr
, tmp
, "nm", EDgroupChar
);
2471 /* Makes a legend for the xvg output file. Call on MASTER only! */
2472 static void write_edo_legend(gmx_edsam_t ed
, int nED
, const gmx_output_env_t
*oenv
)
2474 t_edpar
*edi
= nullptr;
2476 int nr_edi
, nsets
, n_flood
, n_edsam
;
2477 const char **setname
;
2479 char *LegendStr
= nullptr;
2484 fprintf(ed
->edo
, "# Output will be written every %d step%s\n", ed
->edpar
->outfrq
, ed
->edpar
->outfrq
!= 1 ? "s" : "");
2486 for (nr_edi
= 1; nr_edi
<= nED
; nr_edi
++)
2488 fprintf(ed
->edo
, "#\n");
2489 fprintf(ed
->edo
, "# Summary of applied con/restraints for the ED group %c\n", get_EDgroupChar(nr_edi
, nED
));
2490 fprintf(ed
->edo
, "# Atoms in average structure: %d\n", edi
->sav
.nr
);
2491 fprintf(ed
->edo
, "# monitor : %d vec%s\n", edi
->vecs
.mon
.neig
, edi
->vecs
.mon
.neig
!= 1 ? "s" : "");
2492 fprintf(ed
->edo
, "# LINFIX : %d vec%s\n", edi
->vecs
.linfix
.neig
, edi
->vecs
.linfix
.neig
!= 1 ? "s" : "");
2493 fprintf(ed
->edo
, "# LINACC : %d vec%s\n", edi
->vecs
.linacc
.neig
, edi
->vecs
.linacc
.neig
!= 1 ? "s" : "");
2494 fprintf(ed
->edo
, "# RADFIX : %d vec%s\n", edi
->vecs
.radfix
.neig
, edi
->vecs
.radfix
.neig
!= 1 ? "s" : "");
2495 fprintf(ed
->edo
, "# RADACC : %d vec%s\n", edi
->vecs
.radacc
.neig
, edi
->vecs
.radacc
.neig
!= 1 ? "s" : "");
2496 fprintf(ed
->edo
, "# RADCON : %d vec%s\n", edi
->vecs
.radcon
.neig
, edi
->vecs
.radcon
.neig
!= 1 ? "s" : "");
2497 fprintf(ed
->edo
, "# FLOODING : %d vec%s ", edi
->flood
.vecs
.neig
, edi
->flood
.vecs
.neig
!= 1 ? "s" : "");
2499 if (edi
->flood
.vecs
.neig
)
2501 /* If in any of the groups we find a flooding vector, flooding is turned on */
2502 ed
->eEDtype
= eEDflood
;
2504 /* Print what flavor of flooding we will do */
2505 if (0 == edi
->flood
.tau
) /* constant flooding strength */
2507 fprintf(ed
->edo
, "Efl_null = %g", edi
->flood
.constEfl
);
2508 if (edi
->flood
.bHarmonic
)
2510 fprintf(ed
->edo
, ", harmonic");
2513 else /* adaptive flooding */
2515 fprintf(ed
->edo
, ", adaptive");
2518 fprintf(ed
->edo
, "\n");
2520 edi
= edi
->next_edi
;
2523 /* Print a nice legend */
2525 LegendStr
[0] = '\0';
2526 sprintf(buf
, "# %6s", "time");
2527 add_to_string(&LegendStr
, buf
);
2529 /* Calculate the maximum number of columns we could end up with */
2532 for (nr_edi
= 1; nr_edi
<= nED
; nr_edi
++)
2534 nsets
+= 5 +edi
->vecs
.mon
.neig
2535 +edi
->vecs
.linfix
.neig
2536 +edi
->vecs
.linacc
.neig
2537 +edi
->vecs
.radfix
.neig
2538 +edi
->vecs
.radacc
.neig
2539 +edi
->vecs
.radcon
.neig
2540 + 6*edi
->flood
.vecs
.neig
;
2541 edi
= edi
->next_edi
;
2543 snew(setname
, nsets
);
2545 /* In the mdrun time step in a first function call (do_flood()) the flooding
2546 * forces are calculated and in a second function call (do_edsam()) the
2547 * ED constraints. To get a corresponding legend, we need to loop twice
2548 * over the edi groups and output first the flooding, then the ED part */
2550 /* The flooding-related legend entries, if flooding is done */
2552 if (eEDflood
== ed
->eEDtype
)
2555 for (nr_edi
= 1; nr_edi
<= nED
; nr_edi
++)
2557 /* Always write out the projection on the flooding EVs. Of course, this can also
2558 * be achieved with the monitoring option in do_edsam() (if switched on by the
2559 * user), but in that case the positions need to be communicated in do_edsam(),
2560 * which is not necessary when doing flooding only. */
2561 nice_legend(&setname
, &nsets
, &LegendStr
, "RMSD to ref", "nm", get_EDgroupChar(nr_edi
, nED
) );
2563 for (i
= 0; i
< edi
->flood
.vecs
.neig
; i
++)
2565 sprintf(buf
, "EV%dprjFLOOD", edi
->flood
.vecs
.ieig
[i
]);
2566 nice_legend(&setname
, &nsets
, &LegendStr
, buf
, "nm", get_EDgroupChar(nr_edi
, nED
));
2568 /* Output the current reference projection if it changes with time;
2569 * this can happen when flooding is used as harmonic restraint */
2570 if (edi
->flood
.bHarmonic
&& edi
->flood
.vecs
.refprojslope
[i
] != 0.0)
2572 sprintf(buf
, "EV%d ref.prj.", edi
->flood
.vecs
.ieig
[i
]);
2573 nice_legend(&setname
, &nsets
, &LegendStr
, buf
, "nm", get_EDgroupChar(nr_edi
, nED
));
2576 /* For flooding we also output Efl, Vfl, deltaF, and the flooding forces */
2577 if (0 != edi
->flood
.tau
) /* only output Efl for adaptive flooding (constant otherwise) */
2579 sprintf(buf
, "EV%d-Efl", edi
->flood
.vecs
.ieig
[i
]);
2580 nice_legend(&setname
, &nsets
, &LegendStr
, buf
, "kJ/mol", get_EDgroupChar(nr_edi
, nED
));
2583 sprintf(buf
, "EV%d-Vfl", edi
->flood
.vecs
.ieig
[i
]);
2584 nice_legend(&setname
, &nsets
, &LegendStr
, buf
, "kJ/mol", get_EDgroupChar(nr_edi
, nED
));
2586 if (0 != edi
->flood
.tau
) /* only output deltaF for adaptive flooding (zero otherwise) */
2588 sprintf(buf
, "EV%d-deltaF", edi
->flood
.vecs
.ieig
[i
]);
2589 nice_legend(&setname
, &nsets
, &LegendStr
, buf
, "kJ/mol", get_EDgroupChar(nr_edi
, nED
));
2592 sprintf(buf
, "EV%d-FLforces", edi
->flood
.vecs
.ieig
[i
]);
2593 nice_legend(&setname
, &nsets
, &LegendStr
, buf
, "kJ/mol/nm", get_EDgroupChar(nr_edi
, nED
));
2596 edi
= edi
->next_edi
;
2597 } /* End of flooding-related legend entries */
2601 /* Now the ED-related entries, if essential dynamics is done */
2603 for (nr_edi
= 1; nr_edi
<= nED
; nr_edi
++)
2605 if (bNeedDoEdsam(edi
)) /* Only print ED legend if at least one ED option is on */
2607 nice_legend(&setname
, &nsets
, &LegendStr
, "RMSD to ref", "nm", get_EDgroupChar(nr_edi
, nED
) );
2609 /* Essential dynamics, projections on eigenvectors */
2610 nice_legend_evec(&setname
, &nsets
, &LegendStr
, &edi
->vecs
.mon
, get_EDgroupChar(nr_edi
, nED
), "MON" );
2611 nice_legend_evec(&setname
, &nsets
, &LegendStr
, &edi
->vecs
.linfix
, get_EDgroupChar(nr_edi
, nED
), "LINFIX");
2612 nice_legend_evec(&setname
, &nsets
, &LegendStr
, &edi
->vecs
.linacc
, get_EDgroupChar(nr_edi
, nED
), "LINACC");
2613 nice_legend_evec(&setname
, &nsets
, &LegendStr
, &edi
->vecs
.radfix
, get_EDgroupChar(nr_edi
, nED
), "RADFIX");
2614 if (edi
->vecs
.radfix
.neig
)
2616 nice_legend(&setname
, &nsets
, &LegendStr
, "RADFIX radius", "nm", get_EDgroupChar(nr_edi
, nED
));
2618 nice_legend_evec(&setname
, &nsets
, &LegendStr
, &edi
->vecs
.radacc
, get_EDgroupChar(nr_edi
, nED
), "RADACC");
2619 if (edi
->vecs
.radacc
.neig
)
2621 nice_legend(&setname
, &nsets
, &LegendStr
, "RADACC radius", "nm", get_EDgroupChar(nr_edi
, nED
));
2623 nice_legend_evec(&setname
, &nsets
, &LegendStr
, &edi
->vecs
.radcon
, get_EDgroupChar(nr_edi
, nED
), "RADCON");
2624 if (edi
->vecs
.radcon
.neig
)
2626 nice_legend(&setname
, &nsets
, &LegendStr
, "RADCON radius", "nm", get_EDgroupChar(nr_edi
, nED
));
2629 edi
= edi
->next_edi
;
2630 } /* end of 'pure' essential dynamics legend entries */
2631 n_edsam
= nsets
- n_flood
;
2633 xvgr_legend(ed
->edo
, nsets
, setname
, oenv
);
2636 fprintf(ed
->edo
, "#\n"
2637 "# Legend for %d column%s of flooding plus %d column%s of essential dynamics data:\n",
2638 n_flood
, 1 == n_flood
? "" : "s",
2639 n_edsam
, 1 == n_edsam
? "" : "s");
2640 fprintf(ed
->edo
, "%s", LegendStr
);
2647 /* Init routine for ED and flooding. Calls init_edi in a loop for every .edi-cycle
2648 * contained in the input file, creates a NULL terminated list of t_edpar structures */
2649 void init_edsam(const gmx_mtop_t
*mtop
,
2650 const t_inputrec
*ir
,
2655 edsamstate_t
*EDstate
)
2657 t_edpar
*edi
= nullptr; /* points to a single edi data set */
2658 int i
, nr_edi
, avindex
;
2659 rvec
*x_pbc
= nullptr; /* positions of the whole MD system with pbc removed */
2660 rvec
*xfit
= nullptr, *xstart
= nullptr; /* dummy arrays to determine initial RMSDs */
2661 rvec fit_transvec
; /* translation ... */
2662 matrix fit_rotmat
; /* ... and rotation from fit to reference structure */
2663 rvec
*ref_x_old
= nullptr; /* helper pointer */
2667 fprintf(stderr
, "ED: Initializing essential dynamics constraints.\n");
2671 gmx_fatal(FARGS
, "The checkpoint file you provided is from an essential dynamics or\n"
2672 "flooding simulation. Please also provide the correct .edi file with -ei.\n");
2676 /* Needed for initializing radacc radius in do_edsam */
2679 /* The input file is read by the master and the edi structures are
2680 * initialized here. Input is stored in ed->edpar. Then the edi
2681 * structures are transferred to the other nodes */
2684 /* Initialization for every ED/flooding group. Flooding uses one edi group per
2685 * flooding vector, Essential dynamics can be applied to more than one structure
2686 * as well, but will be done in the order given in the edi file, so
2687 * expect different results for different order of edi file concatenation! */
2689 while (edi
!= nullptr)
2691 init_edi(mtop
, edi
);
2692 init_flood(edi
, ed
, ir
->delta_t
);
2693 edi
= edi
->next_edi
;
2697 /* The master does the work here. The other nodes get the positions
2698 * not before dd_partition_system which is called after init_edsam */
2701 if (!EDstate
->bFromCpt
)
2703 /* Remove PBC, make molecule(s) subject to ED whole. */
2704 snew(x_pbc
, mtop
->natoms
);
2705 copy_rvecn(x
, x_pbc
, 0, mtop
->natoms
);
2706 do_pbc_first_mtop(nullptr, ir
->ePBC
, box
, mtop
, x_pbc
);
2708 /* Reset pointer to first ED data set which contains the actual ED data */
2710 GMX_ASSERT(NULL
!= edi
,
2711 "The pointer to the essential dynamics parameters is undefined");
2713 /* Loop over all ED/flooding data sets (usually only one, though) */
2714 for (nr_edi
= 1; nr_edi
<= EDstate
->nED
; nr_edi
++)
2716 /* For multiple ED groups we use the output frequency that was specified
2717 * in the first set */
2720 edi
->outfrq
= ed
->edpar
->outfrq
;
2723 /* Extract the initial reference and average positions. When starting
2724 * from .cpt, these have already been read into sref.x_old
2725 * in init_edsamstate() */
2726 if (!EDstate
->bFromCpt
)
2728 /* If this is the first run (i.e. no checkpoint present) we assume
2729 * that the starting positions give us the correct PBC representation */
2730 for (i
= 0; i
< edi
->sref
.nr
; i
++)
2732 copy_rvec(x_pbc
[edi
->sref
.anrs
[i
]], edi
->sref
.x_old
[i
]);
2735 for (i
= 0; i
< edi
->sav
.nr
; i
++)
2737 copy_rvec(x_pbc
[edi
->sav
.anrs
[i
]], edi
->sav
.x_old
[i
]);
2741 /* Now we have the PBC-correct start positions of the reference and
2742 average structure. We copy that over to dummy arrays on which we
2743 can apply fitting to print out the RMSD. We srenew the memory since
2744 the size of the buffers is likely different for every ED group */
2745 srenew(xfit
, edi
->sref
.nr
);
2746 srenew(xstart
, edi
->sav
.nr
);
2749 /* Reference indices are the same as average indices */
2750 ref_x_old
= edi
->sav
.x_old
;
2754 ref_x_old
= edi
->sref
.x_old
;
2756 copy_rvecn(ref_x_old
, xfit
, 0, edi
->sref
.nr
);
2757 copy_rvecn(edi
->sav
.x_old
, xstart
, 0, edi
->sav
.nr
);
2759 /* Make the fit to the REFERENCE structure, get translation and rotation */
2760 fit_to_reference(xfit
, fit_transvec
, fit_rotmat
, edi
);
2762 /* Output how well we fit to the reference at the start */
2763 translate_and_rotate(xfit
, edi
->sref
.nr
, fit_transvec
, fit_rotmat
);
2764 fprintf(stderr
, "ED: Initial RMSD from reference after fit = %f nm",
2765 rmsd_from_structure(xfit
, &edi
->sref
));
2766 if (EDstate
->nED
> 1)
2768 fprintf(stderr
, " (ED group %c)", get_EDgroupChar(nr_edi
, EDstate
->nED
));
2770 fprintf(stderr
, "\n");
2772 /* Now apply the translation and rotation to the atoms on which ED sampling will be performed */
2773 translate_and_rotate(xstart
, edi
->sav
.nr
, fit_transvec
, fit_rotmat
);
2775 /* calculate initial projections */
2776 project(xstart
, edi
);
2778 /* For the target and origin structure both a reference (fit) and an
2779 * average structure can be provided in make_edi. If both structures
2780 * are the same, make_edi only stores one of them in the .edi file.
2781 * If they differ, first the fit and then the average structure is stored
2782 * in star (or sor), thus the number of entries in star/sor is
2783 * (n_fit + n_av) with n_fit the size of the fitting group and n_av
2784 * the size of the average group. */
2786 /* process target structure, if required */
2787 if (edi
->star
.nr
> 0)
2789 fprintf(stderr
, "ED: Fitting target structure to reference structure\n");
2791 /* get translation & rotation for fit of target structure to reference structure */
2792 fit_to_reference(edi
->star
.x
, fit_transvec
, fit_rotmat
, edi
);
2794 translate_and_rotate(edi
->star
.x
, edi
->star
.nr
, fit_transvec
, fit_rotmat
);
2795 if (edi
->star
.nr
== edi
->sav
.nr
)
2799 else /* edi->star.nr = edi->sref.nr + edi->sav.nr */
2801 /* The last sav.nr indices of the target structure correspond to
2802 * the average structure, which must be projected */
2803 avindex
= edi
->star
.nr
- edi
->sav
.nr
;
2805 rad_project(edi
, &edi
->star
.x
[avindex
], &edi
->vecs
.radcon
);
2809 rad_project(edi
, xstart
, &edi
->vecs
.radcon
);
2812 /* process structure that will serve as origin of expansion circle */
2813 if ( (eEDflood
== ed
->eEDtype
) && (FALSE
== edi
->flood
.bConstForce
) )
2815 fprintf(stderr
, "ED: Setting center of flooding potential (0 = average structure)\n");
2818 if (edi
->sori
.nr
> 0)
2820 fprintf(stderr
, "ED: Fitting origin structure to reference structure\n");
2822 /* fit this structure to reference structure */
2823 fit_to_reference(edi
->sori
.x
, fit_transvec
, fit_rotmat
, edi
);
2825 translate_and_rotate(edi
->sori
.x
, edi
->sori
.nr
, fit_transvec
, fit_rotmat
);
2826 if (edi
->sori
.nr
== edi
->sav
.nr
)
2830 else /* edi->sori.nr = edi->sref.nr + edi->sav.nr */
2832 /* For the projection, we need the last sav.nr indices of sori */
2833 avindex
= edi
->sori
.nr
- edi
->sav
.nr
;
2836 rad_project(edi
, &edi
->sori
.x
[avindex
], &edi
->vecs
.radacc
);
2837 rad_project(edi
, &edi
->sori
.x
[avindex
], &edi
->vecs
.radfix
);
2838 if ( (eEDflood
== ed
->eEDtype
) && (FALSE
== edi
->flood
.bConstForce
) )
2840 fprintf(stderr
, "ED: The ORIGIN structure will define the flooding potential center.\n");
2841 /* Set center of flooding potential to the ORIGIN structure */
2842 rad_project(edi
, &edi
->sori
.x
[avindex
], &edi
->flood
.vecs
);
2843 /* We already know that no (moving) reference position was provided,
2844 * therefore we can overwrite refproj[0]*/
2845 copyEvecReference(&edi
->flood
.vecs
);
2848 else /* No origin structure given */
2850 rad_project(edi
, xstart
, &edi
->vecs
.radacc
);
2851 rad_project(edi
, xstart
, &edi
->vecs
.radfix
);
2852 if ( (eEDflood
== ed
->eEDtype
) && (FALSE
== edi
->flood
.bConstForce
) )
2854 if (edi
->flood
.bHarmonic
)
2856 fprintf(stderr
, "ED: A (possibly changing) ref. projection will define the flooding potential center.\n");
2857 for (i
= 0; i
< edi
->flood
.vecs
.neig
; i
++)
2859 edi
->flood
.vecs
.refproj
[i
] = edi
->flood
.vecs
.refproj0
[i
];
2864 fprintf(stderr
, "ED: The AVERAGE structure will define the flooding potential center.\n");
2865 /* Set center of flooding potential to the center of the covariance matrix,
2866 * i.e. the average structure, i.e. zero in the projected system */
2867 for (i
= 0; i
< edi
->flood
.vecs
.neig
; i
++)
2869 edi
->flood
.vecs
.refproj
[i
] = 0.0;
2874 /* For convenience, output the center of the flooding potential for the eigenvectors */
2875 if ( (eEDflood
== ed
->eEDtype
) && (FALSE
== edi
->flood
.bConstForce
) )
2877 for (i
= 0; i
< edi
->flood
.vecs
.neig
; i
++)
2879 fprintf(stdout
, "ED: EV %d flooding potential center: %11.4e", edi
->flood
.vecs
.ieig
[i
], edi
->flood
.vecs
.refproj
[i
]);
2880 if (edi
->flood
.bHarmonic
)
2882 fprintf(stdout
, " (adding %11.4e/timestep)", edi
->flood
.vecs
.refprojslope
[i
]);
2884 fprintf(stdout
, "\n");
2888 /* set starting projections for linsam */
2889 rad_project(edi
, xstart
, &edi
->vecs
.linacc
);
2890 rad_project(edi
, xstart
, &edi
->vecs
.linfix
);
2892 /* Prepare for the next edi data set: */
2893 edi
= edi
->next_edi
;
2895 /* Cleaning up on the master node: */
2896 if (!EDstate
->bFromCpt
)
2903 } /* end of MASTER only section */
2907 /* First let everybody know how many ED data sets to expect */
2908 gmx_bcast(sizeof(EDstate
->nED
), &EDstate
->nED
, cr
);
2909 /* Broadcast the essential dynamics / flooding data to all nodes */
2910 broadcast_ed_data(cr
, ed
, EDstate
->nED
);
2914 /* In the single-CPU case, point the local atom numbers pointers to the global
2915 * one, so that we can use the same notation in serial and parallel case: */
2917 /* Loop over all ED data sets (usually only one, though) */
2919 for (nr_edi
= 1; nr_edi
<= EDstate
->nED
; nr_edi
++)
2921 edi
->sref
.anrs_loc
= edi
->sref
.anrs
;
2922 edi
->sav
.anrs_loc
= edi
->sav
.anrs
;
2923 edi
->star
.anrs_loc
= edi
->star
.anrs
;
2924 edi
->sori
.anrs_loc
= edi
->sori
.anrs
;
2925 /* For the same reason as above, make a dummy c_ind array: */
2926 snew(edi
->sav
.c_ind
, edi
->sav
.nr
);
2927 /* Initialize the array */
2928 for (i
= 0; i
< edi
->sav
.nr
; i
++)
2930 edi
->sav
.c_ind
[i
] = i
;
2932 /* In the general case we will need a different-sized array for the reference indices: */
2935 snew(edi
->sref
.c_ind
, edi
->sref
.nr
);
2936 for (i
= 0; i
< edi
->sref
.nr
; i
++)
2938 edi
->sref
.c_ind
[i
] = i
;
2941 /* Point to the very same array in case of other structures: */
2942 edi
->star
.c_ind
= edi
->sav
.c_ind
;
2943 edi
->sori
.c_ind
= edi
->sav
.c_ind
;
2944 /* In the serial case, the local number of atoms is the global one: */
2945 edi
->sref
.nr_loc
= edi
->sref
.nr
;
2946 edi
->sav
.nr_loc
= edi
->sav
.nr
;
2947 edi
->star
.nr_loc
= edi
->star
.nr
;
2948 edi
->sori
.nr_loc
= edi
->sori
.nr
;
2950 /* An on we go to the next ED group */
2951 edi
= edi
->next_edi
;
2955 /* Allocate space for ED buffer variables */
2956 /* Again, loop over ED data sets */
2958 for (nr_edi
= 1; nr_edi
<= EDstate
->nED
; nr_edi
++)
2960 /* Allocate space for ED buffer variables */
2961 snew_bc(cr
, edi
->buf
, 1); /* MASTER has already allocated edi->buf in init_edi() */
2962 snew(edi
->buf
->do_edsam
, 1);
2964 /* Space for collective ED buffer variables */
2966 /* Collective positions of atoms with the average indices */
2967 snew(edi
->buf
->do_edsam
->xcoll
, edi
->sav
.nr
);
2968 snew(edi
->buf
->do_edsam
->shifts_xcoll
, edi
->sav
.nr
); /* buffer for xcoll shifts */
2969 snew(edi
->buf
->do_edsam
->extra_shifts_xcoll
, edi
->sav
.nr
);
2970 /* Collective positions of atoms with the reference indices */
2973 snew(edi
->buf
->do_edsam
->xc_ref
, edi
->sref
.nr
);
2974 snew(edi
->buf
->do_edsam
->shifts_xc_ref
, edi
->sref
.nr
); /* To store the shifts in */
2975 snew(edi
->buf
->do_edsam
->extra_shifts_xc_ref
, edi
->sref
.nr
);
2978 /* Get memory for flooding forces */
2979 snew(edi
->flood
.forces_cartesian
, edi
->sav
.nr
);
2982 /* Dump it all into one file per process */
2983 dump_edi(edi
, cr
, nr_edi
);
2987 edi
= edi
->next_edi
;
2990 /* Flush the edo file so that the user can check some things
2991 * when the simulation has started */
2999 void do_edsam(const t_inputrec
*ir
,
3007 int i
, edinr
, iupdate
= 500;
3008 matrix rotmat
; /* rotation matrix */
3009 rvec transvec
; /* translation vector */
3010 rvec dv
, dx
, x_unsh
; /* tmp vectors for velocity, distance, unshifted x coordinate */
3011 real dt_1
; /* 1/dt */
3012 struct t_do_edsam
*buf
;
3014 real rmsdev
= -1; /* RMSD from reference structure prior to applying the constraints */
3015 gmx_bool bSuppress
= FALSE
; /* Write .xvg output file on master? */
3018 /* Check if ED sampling has to be performed */
3019 if (ed
->eEDtype
== eEDnone
)
3024 dt_1
= 1.0/ir
->delta_t
;
3026 /* Loop over all ED groups (usually one) */
3029 while (edi
!= nullptr)
3032 if (bNeedDoEdsam(edi
))
3035 buf
= edi
->buf
->do_edsam
;
3039 /* initialize radacc radius for slope criterion */
3040 buf
->oldrad
= calc_radius(&edi
->vecs
.radacc
);
3043 /* Copy the positions into buf->xc* arrays and after ED
3044 * feed back corrections to the official positions */
3046 /* Broadcast the ED positions such that every node has all of them
3047 * Every node contributes its local positions xs and stores it in
3048 * the collective buf->xcoll array. Note that for edinr > 1
3049 * xs could already have been modified by an earlier ED */
3051 communicate_group_positions(cr
, buf
->xcoll
, buf
->shifts_xcoll
, buf
->extra_shifts_xcoll
, PAR(cr
) ? buf
->bUpdateShifts
: TRUE
, xs
,
3052 edi
->sav
.nr
, edi
->sav
.nr_loc
, edi
->sav
.anrs_loc
, edi
->sav
.c_ind
, edi
->sav
.x_old
, box
);
3054 /* Only assembly reference positions if their indices differ from the average ones */
3057 communicate_group_positions(cr
, buf
->xc_ref
, buf
->shifts_xc_ref
, buf
->extra_shifts_xc_ref
, PAR(cr
) ? buf
->bUpdateShifts
: TRUE
, xs
,
3058 edi
->sref
.nr
, edi
->sref
.nr_loc
, edi
->sref
.anrs_loc
, edi
->sref
.c_ind
, edi
->sref
.x_old
, box
);
3061 /* If bUpdateShifts was TRUE then the shifts have just been updated in communicate_group_positions.
3062 * We do not need to update the shifts until the next NS step. Note that dd_make_local_ed_indices
3063 * set bUpdateShifts=TRUE in the parallel case. */
3064 buf
->bUpdateShifts
= FALSE
;
3066 /* Now all nodes have all of the ED positions in edi->sav->xcoll,
3067 * as well as the indices in edi->sav.anrs */
3069 /* Fit the reference indices to the reference structure */
3072 fit_to_reference(buf
->xcoll
, transvec
, rotmat
, edi
);
3076 fit_to_reference(buf
->xc_ref
, transvec
, rotmat
, edi
);
3079 /* Now apply the translation and rotation to the ED structure */
3080 translate_and_rotate(buf
->xcoll
, edi
->sav
.nr
, transvec
, rotmat
);
3082 /* Find out how well we fit to the reference (just for output steps) */
3083 if (do_per_step(step
, edi
->outfrq
) && MASTER(cr
))
3087 /* Indices of reference and average structures are identical,
3088 * thus we can calculate the rmsd to SREF using xcoll */
3089 rmsdev
= rmsd_from_structure(buf
->xcoll
, &edi
->sref
);
3093 /* We have to translate & rotate the reference atoms first */
3094 translate_and_rotate(buf
->xc_ref
, edi
->sref
.nr
, transvec
, rotmat
);
3095 rmsdev
= rmsd_from_structure(buf
->xc_ref
, &edi
->sref
);
3099 /* update radsam references, when required */
3100 if (do_per_step(step
, edi
->maxedsteps
) && step
>= edi
->presteps
)
3102 project(buf
->xcoll
, edi
);
3103 rad_project(edi
, buf
->xcoll
, &edi
->vecs
.radacc
);
3104 rad_project(edi
, buf
->xcoll
, &edi
->vecs
.radfix
);
3105 buf
->oldrad
= -1.e5
;
3108 /* update radacc references, when required */
3109 if (do_per_step(step
, iupdate
) && step
>= edi
->presteps
)
3111 edi
->vecs
.radacc
.radius
= calc_radius(&edi
->vecs
.radacc
);
3112 if (edi
->vecs
.radacc
.radius
- buf
->oldrad
< edi
->slope
)
3114 project(buf
->xcoll
, edi
);
3115 rad_project(edi
, buf
->xcoll
, &edi
->vecs
.radacc
);
3120 buf
->oldrad
= edi
->vecs
.radacc
.radius
;
3124 /* apply the constraints */
3125 if (step
>= edi
->presteps
&& ed_constraints(ed
->eEDtype
, edi
))
3127 /* ED constraints should be applied already in the first MD step
3128 * (which is step 0), therefore we pass step+1 to the routine */
3129 ed_apply_constraints(buf
->xcoll
, edi
, step
+1 - ir
->init_step
);
3132 /* write to edo, when required */
3133 if (do_per_step(step
, edi
->outfrq
))
3135 project(buf
->xcoll
, edi
);
3136 if (MASTER(cr
) && !bSuppress
)
3138 write_edo(edi
, ed
->edo
, rmsdev
);
3142 /* Copy back the positions unless monitoring only */
3143 if (ed_constraints(ed
->eEDtype
, edi
))
3145 /* remove fitting */
3146 rmfit(edi
->sav
.nr
, buf
->xcoll
, transvec
, rotmat
);
3148 /* Copy the ED corrected positions into the coordinate array */
3149 /* Each node copies its local part. In the serial case, nat_loc is the
3150 * total number of ED atoms */
3151 for (i
= 0; i
< edi
->sav
.nr_loc
; i
++)
3153 /* Unshift local ED coordinate and store in x_unsh */
3154 ed_unshift_single_coord(box
, buf
->xcoll
[edi
->sav
.c_ind
[i
]],
3155 buf
->shifts_xcoll
[edi
->sav
.c_ind
[i
]], x_unsh
);
3157 /* dx is the ED correction to the positions: */
3158 rvec_sub(x_unsh
, xs
[edi
->sav
.anrs_loc
[i
]], dx
);
3162 /* dv is the ED correction to the velocity: */
3163 svmul(dt_1
, dx
, dv
);
3164 /* apply the velocity correction: */
3165 rvec_inc(v
[edi
->sav
.anrs_loc
[i
]], dv
);
3167 /* Finally apply the position correction due to ED: */
3168 copy_rvec(x_unsh
, xs
[edi
->sav
.anrs_loc
[i
]]);
3171 } /* END of if ( bNeedDoEdsam(edi) ) */
3173 /* Prepare for the next ED group */
3174 edi
= edi
->next_edi
;
3176 } /* END of loop over ED groups */
3181 void done_ed(gmx_edsam_t
*ed
)
3185 /* ed->edo is opened sometimes with xvgropen, sometimes with
3186 * gmx_fio_fopen, so we use the least common denominator for
3188 gmx_fio_fclose((*ed
)->edo
);
3191 /* TODO deallocate ed and set pointer to NULL */