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, 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 /* This file is completely threadsafe - keep it that way! */
50 #include "gromacs/gmxlib/network.h"
51 #include "gromacs/math/functions.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/mdlib/main.h"
54 #include "gromacs/mdtypes/commrec.h"
55 #include "gromacs/mdtypes/fcdata.h"
56 #include "gromacs/mdtypes/inputrec.h"
57 #include "gromacs/mdtypes/md_enums.h"
58 #include "gromacs/mdtypes/state.h"
59 #include "gromacs/pbcutil/ishift.h"
60 #include "gromacs/pbcutil/mshift.h"
61 #include "gromacs/pbcutil/pbc.h"
62 #include "gromacs/topology/mtop_util.h"
63 #include "gromacs/topology/topology.h"
64 #include "gromacs/utility/fatalerror.h"
65 #include "gromacs/utility/futil.h"
66 #include "gromacs/utility/pleasecite.h"
67 #include "gromacs/utility/smalloc.h"
69 void init_disres(FILE *fplog, const gmx_mtop_t *mtop,
70 t_inputrec *ir, const t_commrec *cr,
71 t_fcdata *fcd, t_state *state, gmx_bool bIsREMD)
73 int fa, nmol, npair, np;
76 gmx_mtop_ilistloop_t iloop;
82 if (gmx_mtop_ftype_count(mtop, F_DISRES) == 0)
91 fprintf(fplog, "Initializing the distance restraints\n");
95 if (ir->eDisre == edrEnsemble)
97 gmx_fatal(FARGS, "Sorry, distance restraints with ensemble averaging over multiple molecules in one system are not functional in this version of GROMACS");
100 dd->dr_weighting = ir->eDisreWeighting;
101 dd->dr_fc = ir->dr_fc;
102 if (EI_DYNAMICS(ir->eI))
104 dd->dr_tau = ir->dr_tau;
110 if (dd->dr_tau == 0.0)
112 dd->dr_bMixed = FALSE;
117 dd->dr_bMixed = ir->bDisreMixed;
118 dd->ETerm = std::exp(-(ir->delta_t/ir->dr_tau));
120 dd->ETerm1 = 1.0 - dd->ETerm;
124 iloop = gmx_mtop_ilistloop_init(mtop);
125 while (gmx_mtop_ilistloop_next(iloop, &il, &nmol))
128 for (fa = 0; fa < il[F_DISRES].nr; fa += 3)
131 npair = mtop->ffparams.iparams[il[F_DISRES].iatoms[fa]].disres.npair;
134 dd->nres += (ir->eDisre == edrEnsemble ? 1 : nmol)*npair;
135 dd->npair += nmol*npair;
143 /* Temporary check, will be removed when disre is implemented with DD */
144 const char *notestr = "NOTE: atoms involved in distance restraints should be within the same domain. If this is not the case mdrun generates a fatal error. If you encounter this, use a single MPI rank (Verlet+OpenMP+GPUs work fine).";
148 fprintf(stderr, "\n%s\n\n", notestr);
152 fprintf(fplog, "%s\n", notestr);
155 if (dd->dr_tau != 0 || ir->eDisre == edrEnsemble ||
156 dd->nres != dd->npair)
158 gmx_fatal(FARGS, "Time or ensemble averaged or multiple pair distance restraints do not work (yet) with domain decomposition, use a single MPI rank%s", cr->ms ? " per simulation" : "");
160 if (ir->nstdisreout != 0)
164 fprintf(fplog, "\nWARNING: Can not write distance restraint data to energy file with domain decomposition\n\n");
168 fprintf(stderr, "\nWARNING: Can not write distance restraint data to energy file with domain decomposition\n");
174 snew(dd->rt, dd->npair);
176 if (dd->dr_tau != 0.0)
179 /* Set the "history lack" factor to 1 */
180 state->flags |= (1<<estDISRE_INITF);
181 hist->disre_initf = 1.0;
182 /* Allocate space for the r^-3 time averages */
183 state->flags |= (1<<estDISRE_RM3TAV);
184 hist->ndisrepairs = dd->npair;
185 snew(hist->disre_rm3tav, hist->ndisrepairs);
187 /* Allocate space for a copy of rm3tav,
188 * so we can call do_force without modifying the state.
190 snew(dd->rm3tav, dd->npair);
192 /* Allocate Rt_6 and Rtav_6 consecutively in memory so they can be
193 * averaged over the processors in one call (in calc_disre_R_6)
195 snew(dd->Rt_6, 2*dd->nres);
196 dd->Rtav_6 = &(dd->Rt_6[dd->nres]);
198 ptr = getenv("GMX_DISRE_ENSEMBLE_SIZE");
199 if (cr && cr->ms != NULL && ptr != NULL && !bIsREMD)
203 sscanf(ptr, "%d", &dd->nsystems);
206 fprintf(fplog, "Found GMX_DISRE_ENSEMBLE_SIZE set to %d systems per ensemble\n", dd->nsystems);
208 /* This check is only valid on MASTER(cr), so probably
209 * ensemble-averaged distance restraints are broken on more
210 * than one processor per simulation system. */
213 check_multi_int(fplog, cr->ms, dd->nsystems,
214 "the number of systems per ensemble",
217 gmx_bcast_sim(sizeof(int), &dd->nsystems, cr);
219 /* We use to allow any value of nsystems which was a divisor
220 * of ms->nsim. But this required an extra communicator which
221 * was stored in t_fcdata. This pulled in mpi.h in nearly all C files.
223 if (!(cr->ms->nsim == 1 || cr->ms->nsim == dd->nsystems))
225 gmx_fatal(FARGS, "GMX_DISRE_ENSEMBLE_SIZE (%d) is not equal to 1 or the number of systems (option -multi) %d", dd->nsystems, cr->ms->nsim);
229 fprintf(fplog, "Our ensemble consists of systems:");
230 for (int i = 0; i < dd->nsystems; i++)
232 fprintf(fplog, " %d",
233 (cr->ms->sim/dd->nsystems)*dd->nsystems+i);
235 fprintf(fplog, "\n");
237 snew(dd->Rtl_6, dd->nres);
243 dd->Rtl_6 = dd->Rt_6;
250 fprintf(fplog, "There are %d distance restraints involving %d atom pairs\n", dd->nres, dd->npair);
252 /* Have to avoid g_disre de-referencing cr blindly, mdrun not
253 * doing consistency checks for ensemble-averaged distance
254 * restraints when that's not happening, and only doing those
255 * checks from appropriate processes (since check_multi_int is
256 * too broken to check whether the communication will
258 if (cr && cr->ms && dd->nsystems > 1 && MASTER(cr))
260 check_multi_int(fplog, cr->ms, fcd->disres.nres,
261 "the number of distance restraints",
264 please_cite(fplog, "Tropp80a");
265 please_cite(fplog, "Torda89a");
269 void calc_disres_R_6(int nfa, const t_iatom forceatoms[], const t_iparams ip[],
270 const rvec x[], const t_pbc *pbc,
271 t_fcdata *fcd, history_t *hist)
277 real *rt, *rm3tav, *Rtl_6, *Rt_6, *Rtav_6;
278 real rt_1, rt_3, rt2;
280 real ETerm, ETerm1, cf1 = 0, cf2 = 0, invn = 0;
284 bTav = (dd->dr_tau != 0);
295 /* scaling factor to smoothly turn on the restraint forces *
296 * when using time averaging */
297 dd->exp_min_t_tau = hist->disre_initf*ETerm;
299 cf1 = dd->exp_min_t_tau;
300 cf2 = 1.0/(1.0 - dd->exp_min_t_tau);
303 if (dd->nsystems > 1)
305 invn = 1.0/dd->nsystems;
308 /* 'loop' over all atom pairs (pair_nr=fa/3) involved in restraints, *
309 * the total number of atoms pairs is nfa/3 */
314 type = forceatoms[fa];
315 npair = ip[type].disres.npair;
320 /* Loop over the atom pairs of 'this' restraint */
322 while (fa < nfa && np < npair)
325 ai = forceatoms[fa+1];
326 aj = forceatoms[fa+2];
330 pbc_dx_aiuc(pbc, x[ai], x[aj], dx);
334 rvec_sub(x[ai], x[aj], dx);
337 rt_1 = gmx::invsqrt(rt2);
338 rt_3 = rt_1*rt_1*rt_1;
340 rt[pair] = std::sqrt(rt2);
343 /* Here we update rm3tav in t_fcdata using the data
345 * Thus the results stay correct when this routine
346 * is called multiple times.
348 rm3tav[pair] = cf2*((ETerm - cf1)*hist->disre_rm3tav[pair] +
356 Rt_6[res] += rt_3*rt_3;
357 Rtav_6[res] += rm3tav[pair]*rm3tav[pair];
362 if (dd->nsystems > 1)
364 Rtl_6[res] = Rt_6[res];
373 real ta_disres(int nfa, const t_iatom forceatoms[], const t_iparams ip[],
374 const rvec x[], rvec4 f[], rvec fshift[],
375 const t_pbc *pbc, const t_graph *g,
376 real gmx_unused lambda, real gmx_unused *dvdlambda,
377 const t_mdatoms gmx_unused *md, t_fcdata *fcd,
378 int gmx_unused *global_atom_index)
380 const real seven_three = 7.0/3.0;
383 int fa, res, npair, p, pair, ki = CENTRAL, m;
387 real smooth_fc, Rt, Rtav, rt2, *Rtl_6, *Rt_6, *Rtav_6;
388 real k0, f_scal = 0, fmax_scal, fk_scal, fij;
389 real tav_viol, instant_viol, mixed_viol, violtot, vtot;
390 real tav_viol_Rtav7, instant_viol_Rtav7;
392 gmx_bool bConservative, bMixed, bViolation;
399 dr_weighting = dd->dr_weighting;
400 dr_bMixed = dd->dr_bMixed;
405 tav_viol = instant_viol = mixed_viol = tav_viol_Rtav7 = instant_viol_Rtav7 = 0;
407 smooth_fc = dd->dr_fc;
410 /* scaling factor to smoothly turn on the restraint forces *
411 * when using time averaging */
412 smooth_fc *= (1.0 - dd->exp_min_t_tau);
418 /* 'loop' over all atom pairs (pair_nr=fa/3) involved in restraints, *
419 * the total number of atoms pairs is nfa/3 */
424 type = forceatoms[fa];
425 /* Take action depending on restraint, calculate scalar force */
426 npair = ip[type].disres.npair;
427 up1 = ip[type].disres.up1;
428 up2 = ip[type].disres.up2;
429 low = ip[type].disres.low;
430 k0 = smooth_fc*ip[type].disres.kfac;
432 /* save some flops when there is only one pair */
433 if (ip[type].disres.type != 2)
435 bConservative = (dr_weighting == edrwConservative) && (npair > 1);
437 Rt = gmx::invsixthroot(Rt_6[res]);
438 Rtav = gmx::invsixthroot(Rtav_6[res]);
442 /* When rtype=2 use instantaneous not ensemble avereged distance */
443 bConservative = (npair > 1);
445 Rt = gmx::invsixthroot(Rtl_6[res]);
452 tav_viol = Rtav - up1;
457 tav_viol = Rtav - low;
467 * there is no real potential when time averaging is applied
469 vtot += 0.5*k0*gmx::square(tav_viol);
472 printf("vtot is inf: %f\n", vtot);
476 f_scal = -k0*tav_viol;
477 violtot += fabs(tav_viol);
485 instant_viol = Rt - up1;
496 instant_viol = Rt - low;
509 mixed_viol = std::sqrt(tav_viol*instant_viol);
510 f_scal = -k0*mixed_viol;
511 violtot += mixed_viol;
518 fmax_scal = -k0*(up2-up1);
519 /* Correct the force for the number of restraints */
522 f_scal = std::max(f_scal, fmax_scal);
525 f_scal *= Rtav/Rtav_6[res];
529 f_scal /= 2*mixed_viol;
530 tav_viol_Rtav7 = tav_viol*Rtav/Rtav_6[res];
531 instant_viol_Rtav7 = instant_viol*Rt/Rt_6[res];
537 f_scal = std::max(f_scal, fmax_scal);
540 /* Exert the force ... */
542 /* Loop over the atom pairs of 'this' restraint */
543 for (p = 0; p < npair; p++)
546 ai = forceatoms[fa+1];
547 aj = forceatoms[fa+2];
551 ki = pbc_dx_aiuc(pbc, x[ai], x[aj], dx);
555 rvec_sub(x[ai], x[aj], dx);
559 weight_rt_1 = gmx::invsqrt(rt2);
565 weight_rt_1 *= std::pow(dd->rm3tav[pair], seven_three);
569 weight_rt_1 *= tav_viol_Rtav7*std::pow(dd->rm3tav[pair], seven_three)+
570 instant_viol_Rtav7/(dd->rt[pair]*gmx::power6(dd->rt[pair]));
574 fk_scal = f_scal*weight_rt_1;
578 ivec_sub(SHIFT_IVEC(g, ai), SHIFT_IVEC(g, aj), dt);
582 for (m = 0; m < DIM; m++)
588 fshift[ki][m] += fij;
589 fshift[CENTRAL][m] -= fij;
596 /* No violation so force and potential contributions */
602 dd->sumviol = violtot;
608 void update_disres_history(t_fcdata *fcd, history_t *hist)
616 /* Copy the new time averages that have been calculated
617 * in calc_disres_R_6.
619 hist->disre_initf = dd->exp_min_t_tau;
620 for (pair = 0; pair < dd->npair; pair++)
622 hist->disre_rm3tav[pair] = dd->rm3tav[pair];