Merge remote-tracking branch 'gerrit/release-4-6'
[alexxy/gromacs.git] / src / gromacs / mdlib / mdatom.c
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
2  *
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.2.0
11  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13  * Copyright (c) 2001-2004, The GROMACS development team,
14  * check out http://www.gromacs.org for more information.
15
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * For more info, check our website at http://www.gromacs.org
32  * 
33  * And Hey:
34  * GROwing Monsters And Cloning Shrimps
35  */
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "typedefs.h"
41 #include "mdatoms.h"
42 #include "smalloc.h"
43 #include "main.h"
44 #include "qmmm.h"
45 #include "mtop_util.h"
46
47 #define ALMOST_ZERO 1e-30
48
49 t_mdatoms *init_mdatoms(FILE *fp,gmx_mtop_t *mtop,gmx_bool bFreeEnergy)
50 {
51   int    mb,a,g,nmol;
52   double tmA,tmB;
53   t_atom *atom;
54   t_mdatoms *md;
55   gmx_mtop_atomloop_all_t aloop;
56   t_ilist *ilist;
57
58   snew(md,1);
59
60   md->nenergrp = mtop->groups.grps[egcENER].nr;
61   md->bVCMgrps = FALSE;
62   tmA = 0.0;
63   tmB = 0.0;
64
65   aloop = gmx_mtop_atomloop_all_init(mtop);
66   while(gmx_mtop_atomloop_all_next(aloop,&a,&atom)) {
67     if (ggrpnr(&mtop->groups,egcVCM,a) > 0)
68       md->bVCMgrps = TRUE;
69     
70     if (bFreeEnergy && PERTURBED(*atom)) {
71       md->nPerturbed++;
72       if (atom->mB != atom->m)
73         md->nMassPerturbed++;
74       if (atom->qB != atom->q)
75         md->nChargePerturbed++;
76     }
77     
78     tmA += atom->m;
79     tmB += atom->mB;
80   }
81
82   md->tmassA = tmA;
83   md->tmassB = tmB;
84   
85   if (bFreeEnergy && fp)
86     fprintf(fp,
87             "There are %d atoms and %d charges for free energy perturbation\n",
88             md->nPerturbed,md->nChargePerturbed);
89
90   md->bOrires = gmx_mtop_ftype_count(mtop,F_ORIRES);
91
92   return md;
93 }
94
95 void atoms2md(gmx_mtop_t *mtop,t_inputrec *ir,
96               int nindex,int *index,
97               int start,int homenr,
98               t_mdatoms *md)
99 {
100   t_atoms   *atoms_mol;
101   int       i,g,ag,as,ae,molb;
102   real      mA,mB,fac;
103   t_atom    *atom;
104   t_grpopts *opts;
105   gmx_groups_t *groups;
106   gmx_molblock_t *molblock;
107
108   opts = &ir->opts;
109
110   groups = &mtop->groups;
111
112   molblock = mtop->molblock;
113
114   /* Index==NULL indicates particle decomposition,
115    * unless we have an empty DD node, so also check for homenr and start.
116    * This should be signaled properly with an extra parameter or nindex==-1.
117    */
118   if (index == NULL && (homenr > 0 || start > 0)) {
119     md->nr = mtop->natoms;
120   } else {
121     md->nr = nindex;
122   }
123
124   if (md->nr > md->nalloc) {
125     md->nalloc = over_alloc_dd(md->nr);
126
127     if (md->nMassPerturbed) {
128       srenew(md->massA,md->nalloc);
129       srenew(md->massB,md->nalloc);
130     }
131     srenew(md->massT,md->nalloc);
132     srenew(md->invmass,md->nalloc);
133     srenew(md->chargeA,md->nalloc);
134     if (md->nPerturbed) {
135       srenew(md->chargeB,md->nalloc);
136     }
137     srenew(md->typeA,md->nalloc);
138     if (md->nPerturbed) {
139       srenew(md->typeB,md->nalloc);
140     }
141     srenew(md->ptype,md->nalloc);
142     if (opts->ngtc > 1) {
143       srenew(md->cTC,md->nalloc);
144       /* We always copy cTC with domain decomposition */
145     }
146     srenew(md->cENER,md->nalloc);
147     if (opts->ngacc > 1)
148       srenew(md->cACC,md->nalloc);
149     if (opts->nFreeze &&
150         (opts->ngfrz > 1 ||
151          opts->nFreeze[0][XX] || opts->nFreeze[0][YY] || opts->nFreeze[0][ZZ]))
152       srenew(md->cFREEZE,md->nalloc);
153     if (md->bVCMgrps)
154       srenew(md->cVCM,md->nalloc);
155     if (md->bOrires)
156       srenew(md->cORF,md->nalloc);
157     if (md->nPerturbed)
158       srenew(md->bPerturbed,md->nalloc);
159     
160     /* Note that these user t_mdatoms array pointers are NULL
161      * when there is only one group present.
162      * Therefore, when adding code, the user should use something like:
163      * gprnrU1 = (md->cU1==NULL ? 0 : md->cU1[localatindex])
164      */
165     if (mtop->groups.grpnr[egcUser1] != NULL)
166       srenew(md->cU1,md->nalloc);
167     if (mtop->groups.grpnr[egcUser2] != NULL)
168       srenew(md->cU2,md->nalloc);
169     
170     if (ir->bQMMM)
171       srenew(md->bQM,md->nalloc);
172     if (ir->bAdress)
173       srenew(md->wf,md->nalloc);
174       srenew(md->tf_table_index,md->nalloc);
175
176       md->purecg = FALSE;
177       md->pureex = FALSE;
178   }
179
180   for(i=0; (i<md->nr); i++) {
181     if (index == NULL) {
182       ag = i;
183       gmx_mtop_atomnr_to_atom(mtop,ag,&atom);
184     } else {
185       ag   = index[i];
186       molb = -1;
187       ae   = 0;
188       do {
189         molb++;
190         as = ae;
191         ae = as + molblock[molb].nmol*molblock[molb].natoms_mol;
192       } while (ag >= ae);
193       atoms_mol = &mtop->moltype[molblock[molb].type].atoms;
194       atom = &atoms_mol->atom[(ag - as) % atoms_mol->nr];
195     }
196
197     if (md->cFREEZE) {
198       md->cFREEZE[i] = ggrpnr(groups,egcFREEZE,ag);
199     }
200         if (EI_ENERGY_MINIMIZATION(ir->eI))
201         {
202             /* Displacement is proportional to F, masses used for constraints */
203             mA = 1.0;
204             mB = 1.0;
205         }
206         else if (ir->eI == eiBD)
207         {
208             /* With BD the physical masses are irrelevant.
209              * To keep the code simple we use most of the normal MD code path
210              * for BD. Thus for constraining the masses should be proportional
211              * to the friction coefficient. We set the absolute value such that
212              * m/2<(dx/dt)^2> = m/2*2kT/fric*dt = kT/2 => m=fric*dt/2
213              * Then if we set the (meaningless) velocity to v=dx/dt, we get the
214              * correct kinetic energy and temperature using the usual code path.
215              * Thus with BD v*dt will give the displacement and the reported
216              * temperature can signal bad integration (too large time step).
217              */
218             if (ir->bd_fric > 0)
219             {
220                 mA = 0.5*ir->bd_fric*ir->delta_t;
221                 mB = 0.5*ir->bd_fric*ir->delta_t;
222             }
223             else
224             {
225                 /* The friction coefficient is mass/tau_t */
226                 fac = ir->delta_t/opts->tau_t[md->cTC ? groups->grpnr[egcTC][ag] : 0];
227                 mA = 0.5*atom->m*fac;
228                 mB = 0.5*atom->mB*fac;
229             }
230         }
231         else
232         {
233             mA = atom->m;
234             mB = atom->mB;
235         }
236     if (md->nMassPerturbed) {
237       md->massA[i]      = mA;
238       md->massB[i]      = mB;
239     }
240     md->massT[i]        = mA;
241     if (mA == 0.0) {
242       md->invmass[i]    = 0;
243     } else if (md->cFREEZE) {
244       g = md->cFREEZE[i];
245       if (opts->nFreeze[g][XX] && opts->nFreeze[g][YY] && opts->nFreeze[g][ZZ])
246         /* Set the mass of completely frozen particles to ALMOST_ZERO iso 0
247          * to avoid div by zero in lincs or shake.
248          * Note that constraints can still move a partially frozen particle.
249          */
250         md->invmass[i]  = ALMOST_ZERO;
251       else
252         md->invmass[i]  = 1.0/mA;
253     } else {
254       md->invmass[i]    = 1.0/mA;
255     }
256     md->chargeA[i]      = atom->q;
257     md->typeA[i]        = atom->type;
258     if (md->nPerturbed) {
259       md->chargeB[i]    = atom->qB;
260       md->typeB[i]      = atom->typeB;
261       md->bPerturbed[i] = PERTURBED(*atom);
262     }
263     md->ptype[i]        = atom->ptype;
264     if (md->cTC)
265       md->cTC[i]        = groups->grpnr[egcTC][ag];
266     md->cENER[i]        =
267       (groups->grpnr[egcENER] ? groups->grpnr[egcENER][ag] : 0);
268     if (md->cACC)
269       md->cACC[i]       = groups->grpnr[egcACC][ag];
270     if (md->cVCM)
271       md->cVCM[i]       = groups->grpnr[egcVCM][ag];
272     if (md->cORF)
273       md->cORF[i]       = groups->grpnr[egcORFIT][ag];
274
275     if (md->cU1)
276       md->cU1[i]        = groups->grpnr[egcUser1][ag];
277     if (md->cU2)
278       md->cU2[i]        = groups->grpnr[egcUser2][ag];
279
280     if (ir->bQMMM) {
281       if (groups->grpnr[egcQMMM] == 0 || 
282           groups->grpnr[egcQMMM][ag] < groups->grps[egcQMMM].nr-1) {
283         md->bQM[i]      = TRUE;
284       } else {
285         md->bQM[i]      = FALSE;
286       }
287     }
288     /* Initialize AdResS weighting functions to adressw */
289     if (ir->bAdress){
290        md->wf[i]           = 1.0;
291         /* if no tf table groups specified, use default table */
292        md->tf_table_index[i] = DEFAULT_TF_TABLE;
293        if (ir->adress->n_tf_grps > 0){
294             /* if tf table groups specified, tf is only applied to thoose energy groups*/
295             md->tf_table_index[i] = NO_TF_TABLE;
296             /* check wether atom is in one of the relevant energy groups and assign a table index */
297             for (g=0; g<ir->adress->n_tf_grps; g++){
298                 if (md->cENER[i] == ir->adress->tf_table_index[g]){
299                    md->tf_table_index[i] = g;
300                 }
301             }
302         }
303     }
304   }
305
306   md->start  = start;
307   md->homenr = homenr;
308   md->lambda = 0;
309 }
310
311 void update_mdatoms(t_mdatoms *md,real lambda)
312 {
313   int    al,end;
314   real   L1=1.0-lambda;
315   
316   end=md->nr;
317
318   if (md->nMassPerturbed) {
319     for(al=0; (al<end); al++) {
320       if (md->bPerturbed[al]) {
321         md->massT[al] = L1*md->massA[al]+ lambda*md->massB[al];
322         if (md->invmass[al] > 1.1*ALMOST_ZERO)
323           md->invmass[al] = 1.0/md->massT[al];
324       }
325     }
326     md->tmass = L1*md->tmassA + lambda*md->tmassB;
327   } else {
328     md->tmass = md->tmassA;
329   }
330   md->lambda = lambda;
331 }