3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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
33 * GROwing Monsters And Cloning Shrimps
35 /* This file is completely threadsafe - keep it that way! */
46 static void upd_vir(rvec vir, real dvx, real dvy, real dvz)
53 void calc_vir(FILE *log, int nxf, rvec x[], rvec f[], tensor vir,
54 gmx_bool bScrewPBC, matrix box)
57 double dvxx = 0, dvxy = 0, dvxz = 0, dvyx = 0, dvyy = 0, dvyz = 0, dvzx = 0, dvzy = 0, dvzz = 0;
59 for (i = 0; (i < nxf); i++)
61 dvxx += x[i][XX]*f[i][XX];
62 dvxy += x[i][XX]*f[i][YY];
63 dvxz += x[i][XX]*f[i][ZZ];
65 dvyx += x[i][YY]*f[i][XX];
66 dvyy += x[i][YY]*f[i][YY];
67 dvyz += x[i][YY]*f[i][ZZ];
69 dvzx += x[i][ZZ]*f[i][XX];
70 dvzy += x[i][ZZ]*f[i][YY];
71 dvzz += x[i][ZZ]*f[i][ZZ];
76 /* We should correct all odd x-shifts, but the range of isx is -2 to 2 */
77 if (isx == 1 || isx == -1)
79 dvyy += box[YY][YY]*f[i][YY];
80 dvyz += box[YY][YY]*f[i][ZZ];
82 dvzy += box[ZZ][ZZ]*f[i][YY];
83 dvzz += box[ZZ][ZZ]*f[i][ZZ];
88 upd_vir(vir[XX], dvxx, dvxy, dvxz);
89 upd_vir(vir[YY], dvyx, dvyy, dvyz);
90 upd_vir(vir[ZZ], dvzx, dvzy, dvzz);
94 static void lo_fcv(int i0, int i1,
95 real x[], real f[], tensor vir,
96 int is[], real box[], gmx_bool bTriclinic)
98 int i, i3, tx, ty, tz;
100 real dvxx = 0, dvxy = 0, dvxz = 0, dvyx = 0, dvyy = 0, dvyz = 0, dvzx = 0, dvzy = 0, dvzz = 0;
104 for (i = i0; (i < i1); i++)
111 xx = x[i3+XX]-tx*box[XXXX]-ty*box[YYXX]-tz*box[ZZXX];
116 yy = x[i3+YY]-ty*box[YYYY]-tz*box[ZZYY];
121 zz = x[i3+ZZ]-tz*box[ZZZZ];
129 for (i = i0; (i < i1); i++)
136 xx = x[i3+XX]-tx*box[XXXX];
141 yy = x[i3+YY]-ty*box[YYYY];
146 zz = x[i3+ZZ]-tz*box[ZZZZ];
153 upd_vir(vir[XX], dvxx, dvxy, dvxz);
154 upd_vir(vir[YY], dvyx, dvyy, dvyz);
155 upd_vir(vir[ZZ], dvzx, dvzy, dvzz);
158 static void lo_fcv2(int i0, int i1,
159 rvec x[], rvec f[], tensor vir,
160 ivec is[], matrix box, gmx_bool bTriclinic)
162 int i, gg, tx, ty, tz;
164 real dvxx = 0, dvxy = 0, dvxz = 0, dvyx = 0, dvyy = 0, dvyz = 0, dvzx = 0, dvzy = 0, dvzz = 0;
168 for (i = i0, gg = 0; (i < i1); i++, gg++)
174 xx = x[i][XX]-tx*box[XX][XX]-ty*box[YY][XX]-tz*box[ZZ][XX];
179 yy = x[i][YY]-ty*box[YY][YY]-tz*box[ZZ][YY];
184 zz = x[i][ZZ]-tz*box[ZZ][ZZ];
192 for (i = i0, gg = 0; (i < i1); i++, gg++)
198 xx = x[i][XX]-tx*box[XX][XX];
203 yy = x[i][YY]-ty*box[YY][YY];
208 zz = x[i][ZZ]-tz*box[ZZ][ZZ];
215 upd_vir(vir[XX], dvxx, dvxy, dvxz);
216 upd_vir(vir[YY], dvyx, dvyy, dvyz);
217 upd_vir(vir[ZZ], dvzx, dvzy, dvzz);
220 void f_calc_vir(FILE *log, int i0, int i1, rvec x[], rvec f[], tensor vir,
221 t_graph *g, matrix box)
225 if (g && (g->nnodes > 0))
227 /* Calculate virial for bonded forces only when they belong to
230 start = max(i0, g->at_start);
231 end = min(i1, g->at_end);
233 lo_fcv2(start, end, x, f, vir, g->ishift, box, TRICLINIC(box));
235 lo_fcv(start, end, x[0], f[0], vir, g->ishift[0], box[0], TRICLINIC(box));
238 /* If not all atoms are bonded, calculate their virial contribution
239 * anyway, without shifting back their coordinates.
240 * Note the nifty pointer arithmetic...
244 calc_vir(log, start-i0, x + i0, f + i0, vir, FALSE, box);
248 calc_vir(log, i1-end, x + end, f + end, vir, FALSE, box);
253 calc_vir(log, i1-i0, x + i0, f + i0, vir, FALSE, box);