Merge branch 'master' of git://git.gromacs.org/gromacs
[gromacs/adressmacs.git] / src / mdlib / mdatom.c
blobfa3049c699cf2c2cc91ddeb169f4d36686bd92de
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * VERSION 3.2.0
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * GROwing Monsters And Cloning Shrimps
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include "typedefs.h"
40 #include "mdatoms.h"
41 #include "smalloc.h"
42 #include "main.h"
43 #include "qmmm.h"
44 #include "mtop_util.h"
46 #define ALMOST_ZERO 1e-30
48 t_mdatoms *init_mdatoms(FILE *fp,gmx_mtop_t *mtop,gmx_bool bFreeEnergy)
50 int mb,a,g,nmol;
51 double tmA,tmB;
52 t_atom *atom;
53 t_mdatoms *md;
54 gmx_mtop_atomloop_all_t aloop;
55 t_ilist *ilist;
57 snew(md,1);
59 md->nenergrp = mtop->groups.grps[egcENER].nr;
60 md->bVCMgrps = FALSE;
61 tmA = 0.0;
62 tmB = 0.0;
64 aloop = gmx_mtop_atomloop_all_init(mtop);
65 while(gmx_mtop_atomloop_all_next(aloop,&a,&atom)) {
66 if (ggrpnr(&mtop->groups,egcVCM,a) > 0)
67 md->bVCMgrps = TRUE;
69 if (bFreeEnergy && PERTURBED(*atom)) {
70 md->nPerturbed++;
71 if (atom->mB != atom->m)
72 md->nMassPerturbed++;
73 if (atom->qB != atom->q)
74 md->nChargePerturbed++;
77 tmA += atom->m;
78 tmB += atom->mB;
81 md->tmassA = tmA;
82 md->tmassB = tmB;
84 if (bFreeEnergy && fp)
85 fprintf(fp,
86 "There are %d atoms and %d charges for free energy perturbation\n",
87 md->nPerturbed,md->nChargePerturbed);
89 md->bOrires = gmx_mtop_ftype_count(mtop,F_ORIRES);
91 return md;
94 void atoms2md(gmx_mtop_t *mtop,t_inputrec *ir,
95 int nindex,int *index,
96 int start,int homenr,
97 t_mdatoms *md)
99 t_atoms *atoms_mol;
100 int i,g,ag,as,ae,molb;
101 real mA,mB,fac;
102 t_atom *atom;
103 t_grpopts *opts;
104 gmx_groups_t *groups;
105 gmx_molblock_t *molblock;
107 opts = &ir->opts;
109 groups = &mtop->groups;
111 molblock = mtop->molblock;
113 if (index == NULL) {
114 md->nr = mtop->natoms;
115 } else {
116 md->nr = nindex;
119 if (md->nr > md->nalloc) {
120 md->nalloc = over_alloc_dd(md->nr);
122 if (md->nMassPerturbed) {
123 srenew(md->massA,md->nalloc);
124 srenew(md->massB,md->nalloc);
126 srenew(md->massT,md->nalloc);
127 srenew(md->invmass,md->nalloc);
128 srenew(md->chargeA,md->nalloc);
129 if (md->nPerturbed) {
130 srenew(md->chargeB,md->nalloc);
132 srenew(md->typeA,md->nalloc);
133 if (md->nPerturbed) {
134 srenew(md->typeB,md->nalloc);
136 srenew(md->ptype,md->nalloc);
137 if (opts->ngtc > 1) {
138 srenew(md->cTC,md->nalloc);
139 /* We always copy cTC with domain decomposition */
141 srenew(md->cENER,md->nalloc);
142 if (opts->ngacc > 1)
143 srenew(md->cACC,md->nalloc);
144 if (opts->nFreeze &&
145 (opts->ngfrz > 1 ||
146 opts->nFreeze[0][XX] || opts->nFreeze[0][YY] || opts->nFreeze[0][ZZ]))
147 srenew(md->cFREEZE,md->nalloc);
148 if (md->bVCMgrps)
149 srenew(md->cVCM,md->nalloc);
150 if (md->bOrires)
151 srenew(md->cORF,md->nalloc);
152 if (md->nPerturbed)
153 srenew(md->bPerturbed,md->nalloc);
155 /* Note that these user t_mdatoms array pointers are NULL
156 * when there is only one group present.
157 * Therefore, when adding code, the user should use something like:
158 * gprnrU1 = (md->cU1==NULL ? 0 : md->cU1[localatindex])
160 if (mtop->groups.grpnr[egcUser1] != NULL)
161 srenew(md->cU1,md->nalloc);
162 if (mtop->groups.grpnr[egcUser2] != NULL)
163 srenew(md->cU2,md->nalloc);
165 if (ir->bQMMM)
166 srenew(md->bQM,md->nalloc);
167 if (ir->adress_type!=eAdressOff)
168 srenew(md->wf,md->nalloc);
169 srenew(md->tf_table_index,md->nalloc);
171 md->purecg = FALSE;
172 md->pureex = FALSE;
175 for(i=0; (i<md->nr); i++) {
176 if (index == NULL) {
177 ag = i;
178 gmx_mtop_atomnr_to_atom(mtop,ag,&atom);
179 } else {
180 ag = index[i];
181 molb = -1;
182 ae = 0;
183 do {
184 molb++;
185 as = ae;
186 ae = as + molblock[molb].nmol*molblock[molb].natoms_mol;
187 } while (ag >= ae);
188 atoms_mol = &mtop->moltype[molblock[molb].type].atoms;
189 atom = &atoms_mol->atom[(ag - as) % atoms_mol->nr];
192 if (md->cFREEZE) {
193 md->cFREEZE[i] = ggrpnr(groups,egcFREEZE,ag);
195 if (EI_ENERGY_MINIMIZATION(ir->eI)) {
196 mA = 1.0;
197 mB = 1.0;
198 } else if (ir->eI == eiBD) {
199 /* Make the mass proportional to the friction coefficient for BD.
200 * This is necessary for the constraint algorithms.
202 if (ir->bd_fric) {
203 mA = ir->bd_fric*ir->delta_t;
204 mB = ir->bd_fric*ir->delta_t;
205 } else {
206 fac = ir->delta_t/opts->tau_t[md->cTC ? groups->grpnr[egcTC][ag] : 0];
207 mA = atom->m*fac;
208 mB = atom->mB*fac;
210 } else {
211 mA = atom->m;
212 mB = atom->mB;
214 if (md->nMassPerturbed) {
215 md->massA[i] = mA;
216 md->massB[i] = mB;
218 md->massT[i] = mA;
219 if (mA == 0.0) {
220 md->invmass[i] = 0;
221 } else if (md->cFREEZE) {
222 g = md->cFREEZE[i];
223 if (opts->nFreeze[g][XX] && opts->nFreeze[g][YY] && opts->nFreeze[g][ZZ])
224 /* Set the mass of completely frozen particles to ALMOST_ZERO iso 0
225 * to avoid div by zero in lincs or shake.
226 * Note that constraints can still move a partially frozen particle.
228 md->invmass[i] = ALMOST_ZERO;
229 else
230 md->invmass[i] = 1.0/mA;
231 } else {
232 md->invmass[i] = 1.0/mA;
234 md->chargeA[i] = atom->q;
235 md->typeA[i] = atom->type;
236 if (md->nPerturbed) {
237 md->chargeB[i] = atom->qB;
238 md->typeB[i] = atom->typeB;
239 md->bPerturbed[i] = PERTURBED(*atom);
241 md->ptype[i] = atom->ptype;
242 if (md->cTC)
243 md->cTC[i] = groups->grpnr[egcTC][ag];
244 md->cENER[i] =
245 (groups->grpnr[egcENER] ? groups->grpnr[egcENER][ag] : 0);
246 if (md->cACC)
247 md->cACC[i] = groups->grpnr[egcACC][ag];
248 if (md->cVCM)
249 md->cVCM[i] = groups->grpnr[egcVCM][ag];
250 if (md->cORF)
251 md->cORF[i] = groups->grpnr[egcORFIT][ag];
253 if (md->cU1)
254 md->cU1[i] = groups->grpnr[egcUser1][ag];
255 if (md->cU2)
256 md->cU2[i] = groups->grpnr[egcUser2][ag];
258 if (ir->bQMMM) {
259 if (groups->grpnr[egcQMMM] == 0 ||
260 groups->grpnr[egcQMMM][ag] < groups->grps[egcQMMM].nr-1) {
261 md->bQM[i] = TRUE;
262 } else {
263 md->bQM[i] = FALSE;
266 /* Initialize AdResS weighting functions to adressw */
267 if (ir->adress_type!=eAdressOff){
268 md->wf[i] = 1.0;
269 /* if no tf table groups specified, use default table */
270 md->tf_table_index[i] = DEFAULT_TF_TABLE;
271 if (ir->n_adress_tf_grps > 0){
272 /* if tf table groups specified, tf is only applied to thoose energy groups*/
273 md->tf_table_index[i] = NO_TF_TABLE;
274 /* check wether atom is in one of the relevant energy groups and assign a table index */
275 for (g=0; g<ir->n_adress_tf_grps; g++){
276 if (md->cENER[i] == ir->adress_tf_table_index[g]){
277 md->tf_table_index[i] = g;
284 md->start = start;
285 md->homenr = homenr;
286 md->lambda = 0;
289 void update_mdatoms(t_mdatoms *md,real lambda)
291 int al,end;
292 real L1=1.0-lambda;
294 end=md->nr;
296 if (md->nMassPerturbed) {
297 for(al=0; (al<end); al++) {
298 if (md->bPerturbed[al]) {
299 md->massT[al] = L1*md->massA[al]+ lambda*md->massB[al];
300 if (md->invmass[al] > 1.1*ALMOST_ZERO)
301 md->invmass[al] = 1.0/md->massT[al];
304 md->tmass = L1*md->tmassA + lambda*md->tmassB;
305 } else {
306 md->tmass = md->tmassA;
308 md->lambda = lambda;