Merge remote-tracking branch 'origin/release-4-6' into HEAD
[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 #include "gmx_omp_nthreads.h"
47
48 #define ALMOST_ZERO 1e-30
49
50 t_mdatoms *init_mdatoms(FILE *fp,gmx_mtop_t *mtop,gmx_bool bFreeEnergy)
51 {
52   int    mb,a,g,nmol;
53   double tmA,tmB;
54   t_atom *atom;
55   t_mdatoms *md;
56   gmx_mtop_atomloop_all_t aloop;
57   t_ilist *ilist;
58
59   snew(md,1);
60
61   md->nenergrp = mtop->groups.grps[egcENER].nr;
62   md->bVCMgrps = FALSE;
63   tmA = 0.0;
64   tmB = 0.0;
65
66   aloop = gmx_mtop_atomloop_all_init(mtop);
67   while(gmx_mtop_atomloop_all_next(aloop,&a,&atom)) {
68     if (ggrpnr(&mtop->groups,egcVCM,a) > 0)
69       md->bVCMgrps = TRUE;
70     
71     if (bFreeEnergy && PERTURBED(*atom)) {
72       md->nPerturbed++;
73       if (atom->mB != atom->m)
74         md->nMassPerturbed++;
75       if (atom->qB != atom->q)
76         md->nChargePerturbed++;
77     }
78     
79     tmA += atom->m;
80     tmB += atom->mB;
81   }
82
83   md->tmassA = tmA;
84   md->tmassB = tmB;
85   
86   if (bFreeEnergy && fp)
87     fprintf(fp,
88             "There are %d atoms and %d charges for free energy perturbation\n",
89             md->nPerturbed,md->nChargePerturbed);
90
91   md->bOrires = gmx_mtop_ftype_count(mtop,F_ORIRES);
92
93   return md;
94 }
95
96 void atoms2md(gmx_mtop_t *mtop,t_inputrec *ir,
97               int nindex,int *index,
98               int start,int homenr,
99               t_mdatoms *md)
100 {
101   gmx_mtop_atomlookup_t alook;
102   int       i;
103   t_grpopts *opts;
104   gmx_groups_t *groups;
105   gmx_molblock_t *molblock;
106
107   opts = &ir->opts;
108
109   groups = &mtop->groups;
110
111   molblock = mtop->molblock;
112
113   /* Index==NULL indicates particle decomposition,
114    * unless we have an empty DD node, so also check for homenr and start.
115    * This should be signaled properly with an extra parameter or nindex==-1.
116    */
117   if (index == NULL && (homenr > 0 || start > 0)) {
118     md->nr = mtop->natoms;
119   } else {
120     md->nr = nindex;
121   }
122
123   if (md->nr > md->nalloc) {
124     md->nalloc = over_alloc_dd(md->nr);
125
126     if (md->nMassPerturbed) {
127       srenew(md->massA,md->nalloc);
128       srenew(md->massB,md->nalloc);
129     }
130     srenew(md->massT,md->nalloc);
131     srenew(md->invmass,md->nalloc);
132     srenew(md->chargeA,md->nalloc);
133     if (md->nPerturbed) {
134       srenew(md->chargeB,md->nalloc);
135     }
136     srenew(md->typeA,md->nalloc);
137     if (md->nPerturbed) {
138       srenew(md->typeB,md->nalloc);
139     }
140     srenew(md->ptype,md->nalloc);
141     if (opts->ngtc > 1) {
142       srenew(md->cTC,md->nalloc);
143       /* We always copy cTC with domain decomposition */
144     }
145     srenew(md->cENER,md->nalloc);
146     if (opts->ngacc > 1)
147       srenew(md->cACC,md->nalloc);
148     if (opts->nFreeze &&
149         (opts->ngfrz > 1 ||
150          opts->nFreeze[0][XX] || opts->nFreeze[0][YY] || opts->nFreeze[0][ZZ]))
151       srenew(md->cFREEZE,md->nalloc);
152     if (md->bVCMgrps)
153       srenew(md->cVCM,md->nalloc);
154     if (md->bOrires)
155       srenew(md->cORF,md->nalloc);
156     if (md->nPerturbed)
157       srenew(md->bPerturbed,md->nalloc);
158     
159     /* Note that these user t_mdatoms array pointers are NULL
160      * when there is only one group present.
161      * Therefore, when adding code, the user should use something like:
162      * gprnrU1 = (md->cU1==NULL ? 0 : md->cU1[localatindex])
163      */
164     if (mtop->groups.grpnr[egcUser1] != NULL)
165       srenew(md->cU1,md->nalloc);
166     if (mtop->groups.grpnr[egcUser2] != NULL)
167       srenew(md->cU2,md->nalloc);
168     
169     if (ir->bQMMM)
170       srenew(md->bQM,md->nalloc);
171     if (ir->bAdress)
172       srenew(md->wf,md->nalloc);
173       srenew(md->tf_table_index,md->nalloc);
174
175       md->purecg = FALSE;
176       md->pureex = FALSE;
177   }
178
179   alook = gmx_mtop_atomlookup_init(mtop);
180
181 #pragma omp parallel for num_threads(gmx_omp_nthreads_get(emntDefault)) schedule(static)
182   for(i=0; i<md->nr; i++) {
183     int     g,ag,molb;
184     real    mA,mB,fac;
185     t_atom  *atom;
186
187     if (index == NULL) {
188       ag = i;
189     } else {
190       ag   = index[i];
191     }
192     gmx_mtop_atomnr_to_atom(alook,ag,&atom);
193
194     if (md->cFREEZE) {
195       md->cFREEZE[i] = ggrpnr(groups,egcFREEZE,ag);
196     }
197         if (EI_ENERGY_MINIMIZATION(ir->eI))
198         {
199             /* Displacement is proportional to F, masses used for constraints */
200             mA = 1.0;
201             mB = 1.0;
202         }
203         else if (ir->eI == eiBD)
204         {
205             /* With BD the physical masses are irrelevant.
206              * To keep the code simple we use most of the normal MD code path
207              * for BD. Thus for constraining the masses should be proportional
208              * to the friction coefficient. We set the absolute value such that
209              * m/2<(dx/dt)^2> = m/2*2kT/fric*dt = kT/2 => m=fric*dt/2
210              * Then if we set the (meaningless) velocity to v=dx/dt, we get the
211              * correct kinetic energy and temperature using the usual code path.
212              * Thus with BD v*dt will give the displacement and the reported
213              * temperature can signal bad integration (too large time step).
214              */
215             if (ir->bd_fric > 0)
216             {
217                 mA = 0.5*ir->bd_fric*ir->delta_t;
218                 mB = 0.5*ir->bd_fric*ir->delta_t;
219             }
220             else
221             {
222                 /* The friction coefficient is mass/tau_t */
223                 fac = ir->delta_t/opts->tau_t[md->cTC ? groups->grpnr[egcTC][ag] : 0];
224                 mA = 0.5*atom->m*fac;
225                 mB = 0.5*atom->mB*fac;
226             }
227         }
228         else
229         {
230             mA = atom->m;
231             mB = atom->mB;
232         }
233     if (md->nMassPerturbed) {
234       md->massA[i]      = mA;
235       md->massB[i]      = mB;
236     }
237     md->massT[i]        = mA;
238     if (mA == 0.0) {
239       md->invmass[i]    = 0;
240     } else if (md->cFREEZE) {
241       g = md->cFREEZE[i];
242       if (opts->nFreeze[g][XX] && opts->nFreeze[g][YY] && opts->nFreeze[g][ZZ])
243         /* Set the mass of completely frozen particles to ALMOST_ZERO iso 0
244          * to avoid div by zero in lincs or shake.
245          * Note that constraints can still move a partially frozen particle.
246          */
247         md->invmass[i]  = ALMOST_ZERO;
248       else
249         md->invmass[i]  = 1.0/mA;
250     } else {
251       md->invmass[i]    = 1.0/mA;
252     }
253     md->chargeA[i]      = atom->q;
254     md->typeA[i]        = atom->type;
255     if (md->nPerturbed) {
256       md->chargeB[i]    = atom->qB;
257       md->typeB[i]      = atom->typeB;
258       md->bPerturbed[i] = PERTURBED(*atom);
259     }
260     md->ptype[i]        = atom->ptype;
261     if (md->cTC)
262       md->cTC[i]        = groups->grpnr[egcTC][ag];
263     md->cENER[i]        =
264       (groups->grpnr[egcENER] ? groups->grpnr[egcENER][ag] : 0);
265     if (md->cACC)
266       md->cACC[i]       = groups->grpnr[egcACC][ag];
267     if (md->cVCM)
268       md->cVCM[i]       = groups->grpnr[egcVCM][ag];
269     if (md->cORF)
270       md->cORF[i]       = groups->grpnr[egcORFIT][ag];
271
272     if (md->cU1)
273       md->cU1[i]        = groups->grpnr[egcUser1][ag];
274     if (md->cU2)
275       md->cU2[i]        = groups->grpnr[egcUser2][ag];
276
277     if (ir->bQMMM) {
278       if (groups->grpnr[egcQMMM] == 0 || 
279           groups->grpnr[egcQMMM][ag] < groups->grps[egcQMMM].nr-1) {
280         md->bQM[i]      = TRUE;
281       } else {
282         md->bQM[i]      = FALSE;
283       }
284     }
285     /* Initialize AdResS weighting functions to adressw */
286     if (ir->bAdress){
287        md->wf[i]           = 1.0;
288         /* if no tf table groups specified, use default table */
289        md->tf_table_index[i] = DEFAULT_TF_TABLE;
290        if (ir->adress->n_tf_grps > 0){
291             /* if tf table groups specified, tf is only applied to thoose energy groups*/
292             md->tf_table_index[i] = NO_TF_TABLE;
293             /* check wether atom is in one of the relevant energy groups and assign a table index */
294             for (g=0; g<ir->adress->n_tf_grps; g++){
295                 if (md->cENER[i] == ir->adress->tf_table_index[g]){
296                    md->tf_table_index[i] = g;
297                 }
298             }
299         }
300     }
301   }
302
303   gmx_mtop_atomlookup_destroy(alook);
304
305   md->start  = start;
306   md->homenr = homenr;
307   md->lambda = 0;
308 }
309
310 void update_mdatoms(t_mdatoms *md,real lambda)
311 {
312   int    al,end;
313   real   L1=1.0-lambda;
314   
315   end=md->nr;
316
317   if (md->nMassPerturbed) {
318     for(al=0; (al<end); al++) {
319       if (md->bPerturbed[al]) {
320         md->massT[al] = L1*md->massA[al]+ lambda*md->massB[al];
321         if (md->invmass[al] > 1.1*ALMOST_ZERO)
322           md->invmass[al] = 1.0/md->massT[al];
323       }
324     }
325     md->tmass = L1*md->tmassA + lambda*md->tmassB;
326   } else {
327     md->tmass = md->tmassA;
328   }
329   md->lambda = lambda;
330 }