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, 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.
50 #include "gmx_fatal.h"
53 #include "gmx_omp_nthreads.h"
55 /* Skip 0 so we have more chance of detecting if we forgot to call set_pbc. */
57 epbcdxRECTANGULAR = 1, epbcdxTRICLINIC,
58 epbcdx2D_RECT, epbcdx2D_TRIC,
59 epbcdx1D_RECT, epbcdx1D_TRIC,
60 epbcdxSCREW_RECT, epbcdxSCREW_TRIC,
61 epbcdxNOPBC, epbcdxUNSUPPORTED
64 /* Margin factor for error message and correction if the box is too skewed */
65 #define BOX_MARGIN 1.0010
66 #define BOX_MARGIN_CORRECT 1.0005
68 int ePBC2npbcdim(int ePBC)
74 case epbcXYZ: npbcdim = 3; break;
75 case epbcXY: npbcdim = 2; break;
76 case epbcSCREW: npbcdim = 3; break;
77 case epbcNONE: npbcdim = 0; break;
78 default: gmx_fatal(FARGS, "Unknown ePBC=%d in ePBC2npbcdim", ePBC);
84 int inputrec2nboundeddim(t_inputrec *ir)
86 if (ir->nwall == 2 && ir->ePBC == epbcXY)
92 return ePBC2npbcdim(ir->ePBC);
96 void dump_pbc(FILE *fp, t_pbc *pbc)
100 fprintf(fp, "ePBCDX = %d\n", pbc->ePBCDX);
101 pr_rvecs(fp, 0, "box", pbc->box, DIM);
102 pr_rvecs(fp, 0, "fbox_diag", &pbc->fbox_diag, 1);
103 pr_rvecs(fp, 0, "hbox_diag", &pbc->hbox_diag, 1);
104 pr_rvecs(fp, 0, "mhbox_diag", &pbc->mhbox_diag, 1);
105 rvec_add(pbc->hbox_diag, pbc->mhbox_diag, sum_box);
106 pr_rvecs(fp, 0, "sum of the above two", &sum_box, 1);
107 fprintf(fp, "max_cutoff2 = %g\n", pbc->max_cutoff2);
108 fprintf(fp, "bLimitDistance = %s\n", EBOOL(pbc->bLimitDistance));
109 fprintf(fp, "limit_distance2 = %g\n", pbc->limit_distance2);
110 fprintf(fp, "ntric_vec = %d\n", pbc->ntric_vec);
111 if (pbc->ntric_vec > 0)
113 pr_ivecs(fp, 0, "tric_shift", pbc->tric_shift, pbc->ntric_vec, FALSE);
114 pr_rvecs(fp, 0, "tric_vec", pbc->tric_vec, pbc->ntric_vec);
118 const char *check_box(int ePBC, matrix box)
124 ePBC = guess_ePBC(box);
127 if (ePBC == epbcNONE)
132 if ((box[XX][YY] != 0) || (box[XX][ZZ] != 0) || (box[YY][ZZ] != 0))
134 ptr = "Only triclinic boxes with the first vector parallel to the x-axis and the second vector in the xy-plane are supported.";
136 else if (ePBC == epbcSCREW && (box[YY][XX] != 0 || box[ZZ][XX] != 0))
138 ptr = "The unit cell can not have off-diagonal x-components with screw pbc";
140 else if (fabs(box[YY][XX]) > BOX_MARGIN*0.5*box[XX][XX] ||
142 (fabs(box[ZZ][XX]) > BOX_MARGIN*0.5*box[XX][XX] ||
143 fabs(box[ZZ][YY]) > BOX_MARGIN*0.5*box[YY][YY])))
145 ptr = "Triclinic box is too skewed.";
155 real max_cutoff2(int ePBC, matrix box)
157 real min_hv2, min_ss;
159 /* Physical limitation of the cut-off
160 * by half the length of the shortest box vector.
162 min_hv2 = min(0.25*norm2(box[XX]), 0.25*norm2(box[YY]));
165 min_hv2 = min(min_hv2, 0.25*norm2(box[ZZ]));
168 /* Limitation to the smallest diagonal element due to optimizations:
169 * checking only linear combinations of single box-vectors (2 in x)
170 * in the grid search and pbc_dx is a lot faster
171 * than checking all possible combinations.
175 min_ss = min(box[XX][XX], box[YY][YY]);
179 min_ss = min(box[XX][XX], min(box[YY][YY]-fabs(box[ZZ][YY]), box[ZZ][ZZ]));
182 return min(min_hv2, min_ss*min_ss);
185 /* this one is mostly harmless... */
186 static gmx_bool bWarnedGuess = FALSE;
188 int guess_ePBC(matrix box)
192 if (box[XX][XX] > 0 && box[YY][YY] > 0 && box[ZZ][ZZ] > 0)
196 else if (box[XX][XX] > 0 && box[YY][YY] > 0 && box[ZZ][ZZ] == 0)
200 else if (box[XX][XX] == 0 && box[YY][YY] == 0 && box[ZZ][ZZ] == 0)
208 fprintf(stderr, "WARNING: Unsupported box diagonal %f %f %f, "
209 "will not use periodic boundary conditions\n\n",
210 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
218 fprintf(debug, "Guessed pbc = %s from the box matrix\n", epbc_names[ePBC]);
224 static int correct_box_elem(FILE *fplog, int step, tensor box, int v, int d)
226 int shift, maxshift = 10;
230 /* correct elem d of vector v with vector d */
231 while (box[v][d] > BOX_MARGIN_CORRECT*0.5*box[d][d])
235 fprintf(fplog, "Step %d: correcting invalid box:\n", step);
236 pr_rvecs(fplog, 0, "old box", box, DIM);
238 rvec_dec(box[v], box[d]);
242 pr_rvecs(fplog, 0, "new box", box, DIM);
244 if (shift <= -maxshift)
247 "Box was shifted at least %d times. Please see log-file.",
251 while (box[v][d] < -BOX_MARGIN_CORRECT*0.5*box[d][d])
255 fprintf(fplog, "Step %d: correcting invalid box:\n", step);
256 pr_rvecs(fplog, 0, "old box", box, DIM);
258 rvec_inc(box[v], box[d]);
262 pr_rvecs(fplog, 0, "new box", box, DIM);
264 if (shift >= maxshift)
267 "Box was shifted at least %d times. Please see log-file.",
275 gmx_bool correct_box(FILE *fplog, int step, tensor box, t_graph *graph)
280 /* check if the box still obeys the restrictions, if not, correct it */
281 zy = correct_box_elem(fplog, step, box, ZZ, YY);
282 zx = correct_box_elem(fplog, step, box, ZZ, XX);
283 yx = correct_box_elem(fplog, step, box, YY, XX);
285 bCorrected = (zy || zx || yx);
287 if (bCorrected && graph)
289 /* correct the graph */
290 for (i = graph->at_start; i < graph->at_end; i++)
292 graph->ishift[i][YY] -= graph->ishift[i][ZZ]*zy;
293 graph->ishift[i][XX] -= graph->ishift[i][ZZ]*zx;
294 graph->ishift[i][XX] -= graph->ishift[i][YY]*yx;
301 int ndof_com(t_inputrec *ir)
312 n = (ir->nwall == 0 ? 3 : 2);
318 gmx_incons("Unknown pbc in calc_nrdf");
324 static void low_set_pbc(t_pbc *pbc, int ePBC, ivec *dd_nc, matrix box)
326 int order[5] = {0, -1, 1, -2, 2};
327 int ii, jj, kk, i, j, k, d, dd, jc, kc, npbcdim, shift;
329 real d2old, d2new, d2new_c;
335 pbc->ndim_ePBC = ePBC2npbcdim(ePBC);
337 copy_mat(box, pbc->box);
338 pbc->bLimitDistance = FALSE;
339 pbc->max_cutoff2 = 0;
342 for (i = 0; (i < DIM); i++)
344 pbc->fbox_diag[i] = box[i][i];
345 pbc->hbox_diag[i] = pbc->fbox_diag[i]*0.5;
346 pbc->mhbox_diag[i] = -pbc->hbox_diag[i];
349 ptr = check_box(ePBC, box);
350 if (ePBC == epbcNONE)
352 pbc->ePBCDX = epbcdxNOPBC;
356 fprintf(stderr, "Warning: %s\n", ptr);
357 pr_rvecs(stderr, 0, " Box", box, DIM);
358 fprintf(stderr, " Can not fix pbc.\n");
359 pbc->ePBCDX = epbcdxUNSUPPORTED;
360 pbc->bLimitDistance = TRUE;
361 pbc->limit_distance2 = 0;
365 if (ePBC == epbcSCREW && dd_nc)
367 /* This combinated should never appear here */
368 gmx_incons("low_set_pbc called with screw pbc and dd_nc != NULL");
372 for (i = 0; i < DIM; i++)
374 if ((dd_nc && (*dd_nc)[i] > 1) || (ePBC == epbcXY && i == ZZ))
387 /* 1D pbc is not an mdp option and it is therefore only used
388 * with single shifts.
390 pbc->ePBCDX = epbcdx1D_RECT;
391 for (i = 0; i < DIM; i++)
398 for (i = 0; i < pbc->dim; i++)
400 if (pbc->box[pbc->dim][i] != 0)
402 pbc->ePBCDX = epbcdx1D_TRIC;
407 pbc->ePBCDX = epbcdx2D_RECT;
408 for (i = 0; i < DIM; i++)
415 for (i = 0; i < DIM; i++)
419 for (j = 0; j < i; j++)
421 if (pbc->box[i][j] != 0)
423 pbc->ePBCDX = epbcdx2D_TRIC;
430 if (ePBC != epbcSCREW)
434 pbc->ePBCDX = epbcdxTRICLINIC;
438 pbc->ePBCDX = epbcdxRECTANGULAR;
443 pbc->ePBCDX = (box[ZZ][YY] == 0 ? epbcdxSCREW_RECT : epbcdxSCREW_TRIC);
444 if (pbc->ePBCDX == epbcdxSCREW_TRIC)
447 "Screw pbc is not yet implemented for triclinic boxes.\n"
448 "Can not fix pbc.\n");
449 pbc->ePBCDX = epbcdxUNSUPPORTED;
454 gmx_fatal(FARGS, "Incorrect number of pbc dimensions with DD: %d",
457 pbc->max_cutoff2 = max_cutoff2(ePBC, box);
459 if (pbc->ePBCDX == epbcdxTRICLINIC ||
460 pbc->ePBCDX == epbcdx2D_TRIC ||
461 pbc->ePBCDX == epbcdxSCREW_TRIC)
465 pr_rvecs(debug, 0, "Box", box, DIM);
466 fprintf(debug, "max cutoff %.3f\n", sqrt(pbc->max_cutoff2));
469 /* We will only use single shifts, but we will check a few
470 * more shifts to see if there is a limiting distance
471 * above which we can not be sure of the correct distance.
473 for (kk = 0; kk < 5; kk++)
476 if (!bPBC[ZZ] && k != 0)
480 for (jj = 0; jj < 5; jj++)
483 if (!bPBC[YY] && j != 0)
487 for (ii = 0; ii < 3; ii++)
490 if (!bPBC[XX] && i != 0)
494 /* A shift is only useful when it is trilinic */
495 if (j != 0 || k != 0)
499 for (d = 0; d < DIM; d++)
501 trial[d] = i*box[XX][d] + j*box[YY][d] + k*box[ZZ][d];
502 /* Choose the vector within the brick around 0,0,0 that
503 * will become the shortest due to shift try.
514 pos[d] = min( pbc->hbox_diag[d], -trial[d]);
518 pos[d] = max(-pbc->hbox_diag[d], -trial[d]);
521 d2old += sqr(pos[d]);
522 d2new += sqr(pos[d] + trial[d]);
524 if (BOX_MARGIN*d2new < d2old)
526 if (j < -1 || j > 1 || k < -1 || k > 1)
528 /* Check if there is a single shift vector
529 * that decreases this distance even more.
542 for (d = 0; d < DIM; d++)
544 d2new_c += sqr(pos[d] + trial[d]
545 - jc*box[YY][d] - kc*box[ZZ][d]);
547 if (d2new_c > BOX_MARGIN*d2new)
549 /* Reject this shift vector, as there is no a priori limit
550 * to the number of shifts that decrease distances.
552 if (!pbc->bLimitDistance || d2new < pbc->limit_distance2)
554 pbc->limit_distance2 = d2new;
556 pbc->bLimitDistance = TRUE;
561 /* Check if shifts with one box vector less do better */
563 for (dd = 0; dd < DIM; dd++)
565 shift = (dd == 0 ? i : (dd == 1 ? j : k));
569 for (d = 0; d < DIM; d++)
571 d2new_c += sqr(pos[d] + trial[d] - shift*box[dd][d]);
573 if (d2new_c <= BOX_MARGIN*d2new)
581 /* Accept this shift vector. */
582 if (pbc->ntric_vec >= MAX_NTRICVEC)
584 fprintf(stderr, "\nWARNING: Found more than %d triclinic correction vectors, ignoring some.\n"
585 " There is probably something wrong with your box.\n", MAX_NTRICVEC);
586 pr_rvecs(stderr, 0, " Box", box, DIM);
590 copy_rvec(trial, pbc->tric_vec[pbc->ntric_vec]);
591 pbc->tric_shift[pbc->ntric_vec][XX] = i;
592 pbc->tric_shift[pbc->ntric_vec][YY] = j;
593 pbc->tric_shift[pbc->ntric_vec][ZZ] = k;
600 fprintf(debug, " tricvec %2d = %2d %2d %2d %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
601 pbc->ntric_vec, i, j, k,
602 sqrt(d2old), sqrt(d2new),
603 trial[XX], trial[YY], trial[ZZ],
604 pos[XX], pos[YY], pos[ZZ]);
615 void set_pbc(t_pbc *pbc, int ePBC, matrix box)
619 ePBC = guess_ePBC(box);
622 low_set_pbc(pbc, ePBC, NULL, box);
625 t_pbc *set_pbc_dd(t_pbc *pbc, int ePBC,
626 gmx_domdec_t *dd, gmx_bool bSingleDir, matrix box)
637 if (ePBC == epbcSCREW && dd->nc[XX] > 1)
639 /* The rotation has been taken care of during coordinate communication */
643 for (i = 0; i < DIM; i++)
645 if (dd->nc[i] <= (bSingleDir ? 1 : 2))
648 if (!(ePBC == epbcXY && i == ZZ))
662 low_set_pbc(pbc, ePBC, npbcdim < DIM ? &nc2 : NULL, box);
665 return (npbcdim > 0 ? pbc : NULL);
668 void pbc_dx(const t_pbc *pbc, const rvec x1, const rvec x2, rvec dx)
671 rvec dx_start, trial;
675 rvec_sub(x1, x2, dx);
679 case epbcdxRECTANGULAR:
680 for (i = 0; i < DIM; i++)
682 while (dx[i] > pbc->hbox_diag[i])
684 dx[i] -= pbc->fbox_diag[i];
686 while (dx[i] <= pbc->mhbox_diag[i])
688 dx[i] += pbc->fbox_diag[i];
692 case epbcdxTRICLINIC:
693 for (i = DIM-1; i >= 0; i--)
695 while (dx[i] > pbc->hbox_diag[i])
697 for (j = i; j >= 0; j--)
699 dx[j] -= pbc->box[i][j];
702 while (dx[i] <= pbc->mhbox_diag[i])
704 for (j = i; j >= 0; j--)
706 dx[j] += pbc->box[i][j];
710 /* dx is the distance in a rectangular box */
712 if (d2min > pbc->max_cutoff2)
714 copy_rvec(dx, dx_start);
716 /* Now try all possible shifts, when the distance is within max_cutoff
717 * it must be the shortest possible distance.
720 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
722 rvec_add(dx_start, pbc->tric_vec[i], trial);
723 d2trial = norm2(trial);
726 copy_rvec(trial, dx);
734 for (i = 0; i < DIM; i++)
738 while (dx[i] > pbc->hbox_diag[i])
740 dx[i] -= pbc->fbox_diag[i];
742 while (dx[i] <= pbc->mhbox_diag[i])
744 dx[i] += pbc->fbox_diag[i];
751 for (i = DIM-1; i >= 0; i--)
755 while (dx[i] > pbc->hbox_diag[i])
757 for (j = i; j >= 0; j--)
759 dx[j] -= pbc->box[i][j];
762 while (dx[i] <= pbc->mhbox_diag[i])
764 for (j = i; j >= 0; j--)
766 dx[j] += pbc->box[i][j];
769 d2min += dx[i]*dx[i];
772 if (d2min > pbc->max_cutoff2)
774 copy_rvec(dx, dx_start);
776 /* Now try all possible shifts, when the distance is within max_cutoff
777 * it must be the shortest possible distance.
780 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
782 rvec_add(dx_start, pbc->tric_vec[i], trial);
784 for (j = 0; j < DIM; j++)
788 d2trial += trial[j]*trial[j];
793 copy_rvec(trial, dx);
800 case epbcdxSCREW_RECT:
801 /* The shift definition requires x first */
803 while (dx[XX] > pbc->hbox_diag[XX])
805 dx[XX] -= pbc->fbox_diag[XX];
808 while (dx[XX] <= pbc->mhbox_diag[XX])
810 dx[XX] += pbc->fbox_diag[YY];
815 /* Rotate around the x-axis in the middle of the box */
816 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
817 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
819 /* Normal pbc for y and z */
820 for (i = YY; i <= ZZ; i++)
822 while (dx[i] > pbc->hbox_diag[i])
824 dx[i] -= pbc->fbox_diag[i];
826 while (dx[i] <= pbc->mhbox_diag[i])
828 dx[i] += pbc->fbox_diag[i];
833 case epbcdxUNSUPPORTED:
836 gmx_fatal(FARGS, "Internal error in pbc_dx, set_pbc has not been called");
841 int pbc_dx_aiuc(const t_pbc *pbc, const rvec x1, const rvec x2, rvec dx)
844 rvec dx_start, trial;
846 ivec ishift, ishift_start;
848 rvec_sub(x1, x2, dx);
853 case epbcdxRECTANGULAR:
854 for (i = 0; i < DIM; i++)
856 if (dx[i] > pbc->hbox_diag[i])
858 dx[i] -= pbc->fbox_diag[i];
861 else if (dx[i] <= pbc->mhbox_diag[i])
863 dx[i] += pbc->fbox_diag[i];
868 case epbcdxTRICLINIC:
869 /* For triclinic boxes the performance difference between
870 * if/else and two while loops is negligible.
871 * However, the while version can cause extreme delays
872 * before a simulation crashes due to large forces which
873 * can cause unlimited displacements.
874 * Also allowing multiple shifts would index fshift beyond bounds.
876 for (i = DIM-1; i >= 1; i--)
878 if (dx[i] > pbc->hbox_diag[i])
880 for (j = i; j >= 0; j--)
882 dx[j] -= pbc->box[i][j];
886 else if (dx[i] <= pbc->mhbox_diag[i])
888 for (j = i; j >= 0; j--)
890 dx[j] += pbc->box[i][j];
895 /* Allow 2 shifts in x */
896 if (dx[XX] > pbc->hbox_diag[XX])
898 dx[XX] -= pbc->fbox_diag[XX];
900 if (dx[XX] > pbc->hbox_diag[XX])
902 dx[XX] -= pbc->fbox_diag[XX];
906 else if (dx[XX] <= pbc->mhbox_diag[XX])
908 dx[XX] += pbc->fbox_diag[XX];
910 if (dx[XX] <= pbc->mhbox_diag[XX])
912 dx[XX] += pbc->fbox_diag[XX];
916 /* dx is the distance in a rectangular box */
918 if (d2min > pbc->max_cutoff2)
920 copy_rvec(dx, dx_start);
921 copy_ivec(ishift, ishift_start);
923 /* Now try all possible shifts, when the distance is within max_cutoff
924 * it must be the shortest possible distance.
927 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
929 rvec_add(dx_start, pbc->tric_vec[i], trial);
930 d2trial = norm2(trial);
933 copy_rvec(trial, dx);
934 ivec_add(ishift_start, pbc->tric_shift[i], ishift);
942 for (i = 0; i < DIM; i++)
946 if (dx[i] > pbc->hbox_diag[i])
948 dx[i] -= pbc->fbox_diag[i];
951 else if (dx[i] <= pbc->mhbox_diag[i])
953 dx[i] += pbc->fbox_diag[i];
961 for (i = DIM-1; i >= 1; i--)
965 if (dx[i] > pbc->hbox_diag[i])
967 for (j = i; j >= 0; j--)
969 dx[j] -= pbc->box[i][j];
973 else if (dx[i] <= pbc->mhbox_diag[i])
975 for (j = i; j >= 0; j--)
977 dx[j] += pbc->box[i][j];
981 d2min += dx[i]*dx[i];
986 /* Allow 2 shifts in x */
987 if (dx[XX] > pbc->hbox_diag[XX])
989 dx[XX] -= pbc->fbox_diag[XX];
991 if (dx[XX] > pbc->hbox_diag[XX])
993 dx[XX] -= pbc->fbox_diag[XX];
997 else if (dx[XX] <= pbc->mhbox_diag[XX])
999 dx[XX] += pbc->fbox_diag[XX];
1001 if (dx[XX] <= pbc->mhbox_diag[XX])
1003 dx[XX] += pbc->fbox_diag[XX];
1007 d2min += dx[XX]*dx[XX];
1009 if (d2min > pbc->max_cutoff2)
1011 copy_rvec(dx, dx_start);
1012 copy_ivec(ishift, ishift_start);
1013 /* Now try all possible shifts, when the distance is within max_cutoff
1014 * it must be the shortest possible distance.
1017 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
1019 rvec_add(dx_start, pbc->tric_vec[i], trial);
1021 for (j = 0; j < DIM; j++)
1025 d2trial += trial[j]*trial[j];
1028 if (d2trial < d2min)
1030 copy_rvec(trial, dx);
1031 ivec_add(ishift_start, pbc->tric_shift[i], ishift);
1040 if (dx[i] > pbc->hbox_diag[i])
1042 dx[i] -= pbc->fbox_diag[i];
1045 else if (dx[i] <= pbc->mhbox_diag[i])
1047 dx[i] += pbc->fbox_diag[i];
1053 if (dx[i] > pbc->hbox_diag[i])
1055 rvec_dec(dx, pbc->box[i]);
1058 else if (dx[i] <= pbc->mhbox_diag[i])
1060 rvec_inc(dx, pbc->box[i]);
1064 case epbcdxSCREW_RECT:
1065 /* The shift definition requires x first */
1066 if (dx[XX] > pbc->hbox_diag[XX])
1068 dx[XX] -= pbc->fbox_diag[XX];
1071 else if (dx[XX] <= pbc->mhbox_diag[XX])
1073 dx[XX] += pbc->fbox_diag[XX];
1076 if (ishift[XX] == 1 || ishift[XX] == -1)
1078 /* Rotate around the x-axis in the middle of the box */
1079 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
1080 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
1082 /* Normal pbc for y and z */
1083 for (i = YY; i <= ZZ; i++)
1085 if (dx[i] > pbc->hbox_diag[i])
1087 dx[i] -= pbc->fbox_diag[i];
1090 else if (dx[i] <= pbc->mhbox_diag[i])
1092 dx[i] += pbc->fbox_diag[i];
1098 case epbcdxUNSUPPORTED:
1101 gmx_fatal(FARGS, "Internal error in pbc_dx_aiuc, set_pbc_dd or set_pbc has not been called");
1105 is = IVEC2IS(ishift);
1108 range_check_mesg(is, 0, SHIFTS, "PBC shift vector index range check.");
1114 void pbc_dx_d(const t_pbc *pbc, const dvec x1, const dvec x2, dvec dx)
1117 dvec dx_start, trial;
1118 double d2min, d2trial;
1121 dvec_sub(x1, x2, dx);
1123 switch (pbc->ePBCDX)
1125 case epbcdxRECTANGULAR:
1127 for (i = 0; i < DIM; i++)
1131 while (dx[i] > pbc->hbox_diag[i])
1133 dx[i] -= pbc->fbox_diag[i];
1135 while (dx[i] <= pbc->mhbox_diag[i])
1137 dx[i] += pbc->fbox_diag[i];
1142 case epbcdxTRICLINIC:
1145 for (i = DIM-1; i >= 0; i--)
1149 while (dx[i] > pbc->hbox_diag[i])
1151 for (j = i; j >= 0; j--)
1153 dx[j] -= pbc->box[i][j];
1156 while (dx[i] <= pbc->mhbox_diag[i])
1158 for (j = i; j >= 0; j--)
1160 dx[j] += pbc->box[i][j];
1163 d2min += dx[i]*dx[i];
1166 if (d2min > pbc->max_cutoff2)
1168 copy_dvec(dx, dx_start);
1169 /* Now try all possible shifts, when the distance is within max_cutoff
1170 * it must be the shortest possible distance.
1173 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
1175 for (j = 0; j < DIM; j++)
1177 trial[j] = dx_start[j] + pbc->tric_vec[i][j];
1180 for (j = 0; j < DIM; j++)
1184 d2trial += trial[j]*trial[j];
1187 if (d2trial < d2min)
1189 copy_dvec(trial, dx);
1196 case epbcdxSCREW_RECT:
1197 /* The shift definition requires x first */
1199 while (dx[XX] > pbc->hbox_diag[XX])
1201 dx[XX] -= pbc->fbox_diag[XX];
1204 while (dx[XX] <= pbc->mhbox_diag[XX])
1206 dx[XX] += pbc->fbox_diag[YY];
1211 /* Rotate around the x-axis in the middle of the box */
1212 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
1213 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
1215 /* Normal pbc for y and z */
1216 for (i = YY; i <= ZZ; i++)
1218 while (dx[i] > pbc->hbox_diag[i])
1220 dx[i] -= pbc->fbox_diag[i];
1222 while (dx[i] <= pbc->mhbox_diag[i])
1224 dx[i] += pbc->fbox_diag[i];
1229 case epbcdxUNSUPPORTED:
1232 gmx_fatal(FARGS, "Internal error in pbc_dx, set_pbc has not been called");
1237 gmx_bool image_rect(ivec xi, ivec xj, ivec box_size, real rlong2, int *shift, real *r2)
1245 for (m = 0; (m < DIM); m++)
1277 gmx_bool image_cylindric(ivec xi, ivec xj, ivec box_size, real rlong2,
1278 int *shift, real *r2)
1286 for (m = 0; (m < DIM); m++)
1322 void calc_shifts(matrix box, rvec shift_vec[])
1324 int k, l, m, d, n, test;
1327 for (m = -D_BOX_Z; m <= D_BOX_Z; m++)
1329 for (l = -D_BOX_Y; l <= D_BOX_Y; l++)
1331 for (k = -D_BOX_X; k <= D_BOX_X; k++, n++)
1333 test = XYZ2IS(k, l, m);
1336 gmx_incons("inconsistent shift numbering");
1338 for (d = 0; d < DIM; d++)
1340 shift_vec[n][d] = k*box[XX][d] + l*box[YY][d] + m*box[ZZ][d];
1347 void calc_box_center(int ecenter, matrix box, rvec box_center)
1351 clear_rvec(box_center);
1355 for (m = 0; (m < DIM); m++)
1357 for (d = 0; d < DIM; d++)
1359 box_center[d] += 0.5*box[m][d];
1364 for (d = 0; d < DIM; d++)
1366 box_center[d] = 0.5*box[d][d];
1372 gmx_fatal(FARGS, "Unsupported value %d for ecenter", ecenter);
1376 void calc_triclinic_images(matrix box, rvec img[])
1380 /* Calculate 3 adjacent images in the xy-plane */
1381 copy_rvec(box[0], img[0]);
1382 copy_rvec(box[1], img[1]);
1385 svmul(-1, img[1], img[1]);
1387 rvec_sub(img[1], img[0], img[2]);
1389 /* Get the next 3 in the xy-plane as mirror images */
1390 for (i = 0; i < 3; i++)
1392 svmul(-1, img[i], img[3+i]);
1395 /* Calculate the first 4 out of xy-plane images */
1396 copy_rvec(box[2], img[6]);
1399 svmul(-1, img[6], img[6]);
1401 for (i = 0; i < 3; i++)
1403 rvec_add(img[6], img[i+1], img[7+i]);
1406 /* Mirror the last 4 from the previous in opposite rotation */
1407 for (i = 0; i < 4; i++)
1409 svmul(-1, img[6 + (2+i) % 4], img[10+i]);
1413 void calc_compact_unitcell_vertices(int ecenter, matrix box, rvec vert[])
1415 rvec img[NTRICIMG], box_center;
1416 int n, i, j, tmp[4], d;
1418 calc_triclinic_images(box, img);
1421 for (i = 2; i <= 5; i += 3)
1434 for (j = 0; j < 4; j++)
1436 for (d = 0; d < DIM; d++)
1438 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1443 for (i = 7; i <= 13; i += 6)
1456 for (j = 0; j < 4; j++)
1458 for (d = 0; d < DIM; d++)
1460 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1465 for (i = 9; i <= 11; i += 2)
1485 for (j = 0; j < 4; j++)
1487 for (d = 0; d < DIM; d++)
1489 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1495 calc_box_center(ecenter, box, box_center);
1496 for (i = 0; i < NCUCVERT; i++)
1498 for (d = 0; d < DIM; d++)
1500 vert[i][d] = vert[i][d]*0.25+box_center[d];
1505 int *compact_unitcell_edges()
1507 /* this is an index in vert[] (see calc_box_vertices) */
1508 /*static int edge[NCUCEDGE*2];*/
1510 static const int hexcon[24] = {
1511 0, 9, 1, 19, 2, 15, 3, 21,
1512 4, 17, 5, 11, 6, 23, 7, 13,
1513 8, 20, 10, 18, 12, 16, 14, 22
1516 gmx_bool bFirst = TRUE;
1518 snew(edge, NCUCEDGE*2);
1523 for (i = 0; i < 6; i++)
1525 for (j = 0; j < 4; j++)
1527 edge[e++] = 4*i + j;
1528 edge[e++] = 4*i + (j+1) % 4;
1531 for (i = 0; i < 12*2; i++)
1533 edge[e++] = hexcon[i];
1542 void put_atoms_in_box_omp(int ePBC, matrix box, int natoms, rvec x[])
1545 nth = gmx_omp_nthreads_get(emntDefault);
1547 #pragma omp parallel for num_threads(nth) schedule(static)
1548 for (t = 0; t < nth; t++)
1552 offset = (natoms*t )/nth;
1553 len = (natoms*(t + 1))/nth - offset;
1554 put_atoms_in_box(ePBC, box, len, x + offset);
1558 void put_atoms_in_box(int ePBC, matrix box, int natoms, rvec x[])
1560 int npbcdim, i, m, d;
1562 if (ePBC == epbcSCREW)
1564 gmx_fatal(FARGS, "Sorry, %s pbc is not yet supported", epbc_names[ePBC]);
1578 for (i = 0; (i < natoms); i++)
1580 for (m = npbcdim-1; m >= 0; m--)
1584 for (d = 0; d <= m; d++)
1586 x[i][d] += box[m][d];
1589 while (x[i][m] >= box[m][m])
1591 for (d = 0; d <= m; d++)
1593 x[i][d] -= box[m][d];
1601 for (i = 0; i < natoms; i++)
1603 for (d = 0; d < npbcdim; d++)
1607 x[i][d] += box[d][d];
1609 while (x[i][d] >= box[d][d])
1611 x[i][d] -= box[d][d];
1618 void put_atoms_in_triclinic_unitcell(int ecenter, matrix box,
1619 int natoms, rvec x[])
1621 rvec box_center, shift_center;
1622 real shm01, shm02, shm12, shift;
1625 calc_box_center(ecenter, box, box_center);
1627 /* The product of matrix shm with a coordinate gives the shift vector
1628 which is required determine the periodic cell position */
1629 shm01 = box[1][0]/box[1][1];
1630 shm02 = (box[1][1]*box[2][0] - box[2][1]*box[1][0])/(box[1][1]*box[2][2]);
1631 shm12 = box[2][1]/box[2][2];
1633 clear_rvec(shift_center);
1634 for (d = 0; d < DIM; d++)
1636 rvec_inc(shift_center, box[d]);
1638 svmul(0.5, shift_center, shift_center);
1639 rvec_sub(box_center, shift_center, shift_center);
1641 shift_center[0] = shm01*shift_center[1] + shm02*shift_center[2];
1642 shift_center[1] = shm12*shift_center[2];
1643 shift_center[2] = 0;
1645 for (i = 0; (i < natoms); i++)
1647 for (m = DIM-1; m >= 0; m--)
1649 shift = shift_center[m];
1652 shift += shm01*x[i][1] + shm02*x[i][2];
1656 shift += shm12*x[i][2];
1658 while (x[i][m]-shift < 0)
1660 for (d = 0; d <= m; d++)
1662 x[i][d] += box[m][d];
1665 while (x[i][m]-shift >= box[m][m])
1667 for (d = 0; d <= m; d++)
1669 x[i][d] -= box[m][d];
1677 put_atoms_in_compact_unitcell(int ePBC, int ecenter, matrix box,
1678 int natoms, rvec x[])
1681 rvec box_center, dx;
1684 set_pbc(&pbc, ePBC, box);
1685 calc_box_center(ecenter, box, box_center);
1686 for (i = 0; i < natoms; i++)
1688 pbc_dx(&pbc, x[i], box_center, dx);
1689 rvec_add(box_center, dx, x[i]);
1692 return pbc.bLimitDistance ?
1693 "WARNING: Could not put all atoms in the compact unitcell\n"