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,2014, 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.
37 #include "gromacs/pbcutil/pbc.h"
47 #include "types/commrec.h"
51 #include "gmx_omp_nthreads.h"
53 #include "gromacs/math/utilities.h"
54 #include "gromacs/math/vec.h"
55 #include "gromacs/pbcutil/mshift.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/smalloc.h"
59 /* Skip 0 so we have more chance of detecting if we forgot to call set_pbc. */
61 epbcdxRECTANGULAR = 1, epbcdxTRICLINIC,
62 epbcdx2D_RECT, epbcdx2D_TRIC,
63 epbcdx1D_RECT, epbcdx1D_TRIC,
64 epbcdxSCREW_RECT, epbcdxSCREW_TRIC,
65 epbcdxNOPBC, epbcdxUNSUPPORTED
68 /* Margin factor for error message and correction if the box is too skewed */
69 #define BOX_MARGIN 1.0010
70 #define BOX_MARGIN_CORRECT 1.0005
72 int ePBC2npbcdim(int ePBC)
78 case epbcXYZ: npbcdim = 3; break;
79 case epbcXY: npbcdim = 2; break;
80 case epbcSCREW: npbcdim = 3; break;
81 case epbcNONE: npbcdim = 0; break;
82 default: gmx_fatal(FARGS, "Unknown ePBC=%d in ePBC2npbcdim", ePBC);
88 int inputrec2nboundeddim(t_inputrec *ir)
90 if (ir->nwall == 2 && ir->ePBC == epbcXY)
96 return ePBC2npbcdim(ir->ePBC);
100 void dump_pbc(FILE *fp, t_pbc *pbc)
104 fprintf(fp, "ePBCDX = %d\n", pbc->ePBCDX);
105 pr_rvecs(fp, 0, "box", pbc->box, DIM);
106 pr_rvecs(fp, 0, "fbox_diag", &pbc->fbox_diag, 1);
107 pr_rvecs(fp, 0, "hbox_diag", &pbc->hbox_diag, 1);
108 pr_rvecs(fp, 0, "mhbox_diag", &pbc->mhbox_diag, 1);
109 rvec_add(pbc->hbox_diag, pbc->mhbox_diag, sum_box);
110 pr_rvecs(fp, 0, "sum of the above two", &sum_box, 1);
111 fprintf(fp, "max_cutoff2 = %g\n", pbc->max_cutoff2);
112 fprintf(fp, "bLimitDistance = %s\n", EBOOL(pbc->bLimitDistance));
113 fprintf(fp, "limit_distance2 = %g\n", pbc->limit_distance2);
114 fprintf(fp, "ntric_vec = %d\n", pbc->ntric_vec);
115 if (pbc->ntric_vec > 0)
117 pr_ivecs(fp, 0, "tric_shift", pbc->tric_shift, pbc->ntric_vec, FALSE);
118 pr_rvecs(fp, 0, "tric_vec", pbc->tric_vec, pbc->ntric_vec);
122 const char *check_box(int ePBC, matrix box)
128 ePBC = guess_ePBC(box);
131 if (ePBC == epbcNONE)
136 if ((box[XX][YY] != 0) || (box[XX][ZZ] != 0) || (box[YY][ZZ] != 0))
138 ptr = "Only triclinic boxes with the first vector parallel to the x-axis and the second vector in the xy-plane are supported.";
140 else if (ePBC == epbcSCREW && (box[YY][XX] != 0 || box[ZZ][XX] != 0))
142 ptr = "The unit cell can not have off-diagonal x-components with screw pbc";
144 else if (fabs(box[YY][XX]) > BOX_MARGIN*0.5*box[XX][XX] ||
146 (fabs(box[ZZ][XX]) > BOX_MARGIN*0.5*box[XX][XX] ||
147 fabs(box[ZZ][YY]) > BOX_MARGIN*0.5*box[YY][YY])))
149 ptr = "Triclinic box is too skewed.";
159 real max_cutoff2(int ePBC, matrix box)
161 real min_hv2, min_ss;
163 /* Physical limitation of the cut-off
164 * by half the length of the shortest box vector.
166 min_hv2 = min(0.25*norm2(box[XX]), 0.25*norm2(box[YY]));
169 min_hv2 = min(min_hv2, 0.25*norm2(box[ZZ]));
172 /* Limitation to the smallest diagonal element due to optimizations:
173 * checking only linear combinations of single box-vectors (2 in x)
174 * in the grid search and pbc_dx is a lot faster
175 * than checking all possible combinations.
179 min_ss = min(box[XX][XX], box[YY][YY]);
183 min_ss = min(box[XX][XX], min(box[YY][YY]-fabs(box[ZZ][YY]), box[ZZ][ZZ]));
186 return min(min_hv2, min_ss*min_ss);
189 /* this one is mostly harmless... */
190 static gmx_bool bWarnedGuess = FALSE;
192 int guess_ePBC(matrix box)
196 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)
204 else if (box[XX][XX] == 0 && box[YY][YY] == 0 && box[ZZ][ZZ] == 0)
212 fprintf(stderr, "WARNING: Unsupported box diagonal %f %f %f, "
213 "will not use periodic boundary conditions\n\n",
214 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
222 fprintf(debug, "Guessed pbc = %s from the box matrix\n", epbc_names[ePBC]);
228 static int correct_box_elem(FILE *fplog, int step, tensor box, int v, int d)
230 int shift, maxshift = 10;
234 /* correct elem d of vector v with vector d */
235 while (box[v][d] > BOX_MARGIN_CORRECT*0.5*box[d][d])
239 fprintf(fplog, "Step %d: correcting invalid box:\n", step);
240 pr_rvecs(fplog, 0, "old box", box, DIM);
242 rvec_dec(box[v], box[d]);
246 pr_rvecs(fplog, 0, "new box", box, DIM);
248 if (shift <= -maxshift)
251 "Box was shifted at least %d times. Please see log-file.",
255 while (box[v][d] < -BOX_MARGIN_CORRECT*0.5*box[d][d])
259 fprintf(fplog, "Step %d: correcting invalid box:\n", step);
260 pr_rvecs(fplog, 0, "old box", box, DIM);
262 rvec_inc(box[v], box[d]);
266 pr_rvecs(fplog, 0, "new box", box, DIM);
268 if (shift >= maxshift)
271 "Box was shifted at least %d times. Please see log-file.",
279 gmx_bool correct_box(FILE *fplog, int step, tensor box, t_graph *graph)
284 /* check if the box still obeys the restrictions, if not, correct it */
285 zy = correct_box_elem(fplog, step, box, ZZ, YY);
286 zx = correct_box_elem(fplog, step, box, ZZ, XX);
287 yx = correct_box_elem(fplog, step, box, YY, XX);
289 bCorrected = (zy || zx || yx);
291 if (bCorrected && graph)
293 /* correct the graph */
294 for (i = graph->at_start; i < graph->at_end; i++)
296 graph->ishift[i][YY] -= graph->ishift[i][ZZ]*zy;
297 graph->ishift[i][XX] -= graph->ishift[i][ZZ]*zx;
298 graph->ishift[i][XX] -= graph->ishift[i][YY]*yx;
305 int ndof_com(t_inputrec *ir)
316 n = (ir->nwall == 0 ? 3 : 2);
322 gmx_incons("Unknown pbc in calc_nrdf");
328 static void low_set_pbc(t_pbc *pbc, int ePBC, ivec *dd_nc, matrix box)
330 int order[5] = {0, -1, 1, -2, 2};
331 int ii, jj, kk, i, j, k, d, dd, jc, kc, npbcdim, shift;
333 real d2old, d2new, d2new_c;
339 pbc->ndim_ePBC = ePBC2npbcdim(ePBC);
341 copy_mat(box, pbc->box);
342 pbc->bLimitDistance = FALSE;
343 pbc->max_cutoff2 = 0;
346 for (i = 0; (i < DIM); i++)
348 pbc->fbox_diag[i] = box[i][i];
349 pbc->hbox_diag[i] = pbc->fbox_diag[i]*0.5;
350 pbc->mhbox_diag[i] = -pbc->hbox_diag[i];
353 ptr = check_box(ePBC, box);
354 if (ePBC == epbcNONE)
356 pbc->ePBCDX = epbcdxNOPBC;
360 fprintf(stderr, "Warning: %s\n", ptr);
361 pr_rvecs(stderr, 0, " Box", box, DIM);
362 fprintf(stderr, " Can not fix pbc.\n");
363 pbc->ePBCDX = epbcdxUNSUPPORTED;
364 pbc->bLimitDistance = TRUE;
365 pbc->limit_distance2 = 0;
369 if (ePBC == epbcSCREW && dd_nc)
371 /* This combinated should never appear here */
372 gmx_incons("low_set_pbc called with screw pbc and dd_nc != NULL");
376 for (i = 0; i < DIM; i++)
378 if ((dd_nc && (*dd_nc)[i] > 1) || (ePBC == epbcXY && i == ZZ))
391 /* 1D pbc is not an mdp option and it is therefore only used
392 * with single shifts.
394 pbc->ePBCDX = epbcdx1D_RECT;
395 for (i = 0; i < DIM; i++)
402 assert(pbc->dim < DIM);
403 for (i = 0; i < pbc->dim; i++)
405 if (pbc->box[pbc->dim][i] != 0)
407 pbc->ePBCDX = epbcdx1D_TRIC;
412 pbc->ePBCDX = epbcdx2D_RECT;
413 for (i = 0; i < DIM; i++)
420 for (i = 0; i < DIM; i++)
424 for (j = 0; j < i; j++)
426 if (pbc->box[i][j] != 0)
428 pbc->ePBCDX = epbcdx2D_TRIC;
435 if (ePBC != epbcSCREW)
439 pbc->ePBCDX = epbcdxTRICLINIC;
443 pbc->ePBCDX = epbcdxRECTANGULAR;
448 pbc->ePBCDX = (box[ZZ][YY] == 0 ? epbcdxSCREW_RECT : epbcdxSCREW_TRIC);
449 if (pbc->ePBCDX == epbcdxSCREW_TRIC)
452 "Screw pbc is not yet implemented for triclinic boxes.\n"
453 "Can not fix pbc.\n");
454 pbc->ePBCDX = epbcdxUNSUPPORTED;
459 gmx_fatal(FARGS, "Incorrect number of pbc dimensions with DD: %d",
462 pbc->max_cutoff2 = max_cutoff2(ePBC, box);
464 if (pbc->ePBCDX == epbcdxTRICLINIC ||
465 pbc->ePBCDX == epbcdx2D_TRIC ||
466 pbc->ePBCDX == epbcdxSCREW_TRIC)
470 pr_rvecs(debug, 0, "Box", box, DIM);
471 fprintf(debug, "max cutoff %.3f\n", sqrt(pbc->max_cutoff2));
474 /* We will only use single shifts, but we will check a few
475 * more shifts to see if there is a limiting distance
476 * above which we can not be sure of the correct distance.
478 for (kk = 0; kk < 5; kk++)
481 if (!bPBC[ZZ] && k != 0)
485 for (jj = 0; jj < 5; jj++)
488 if (!bPBC[YY] && j != 0)
492 for (ii = 0; ii < 3; ii++)
495 if (!bPBC[XX] && i != 0)
499 /* A shift is only useful when it is trilinic */
500 if (j != 0 || k != 0)
504 for (d = 0; d < DIM; d++)
506 trial[d] = i*box[XX][d] + j*box[YY][d] + k*box[ZZ][d];
507 /* Choose the vector within the brick around 0,0,0 that
508 * will become the shortest due to shift try.
519 pos[d] = min( pbc->hbox_diag[d], -trial[d]);
523 pos[d] = max(-pbc->hbox_diag[d], -trial[d]);
526 d2old += sqr(pos[d]);
527 d2new += sqr(pos[d] + trial[d]);
529 if (BOX_MARGIN*d2new < d2old)
531 if (j < -1 || j > 1 || k < -1 || k > 1)
533 /* Check if there is a single shift vector
534 * that decreases this distance even more.
547 for (d = 0; d < DIM; d++)
549 d2new_c += sqr(pos[d] + trial[d]
550 - jc*box[YY][d] - kc*box[ZZ][d]);
552 if (d2new_c > BOX_MARGIN*d2new)
554 /* Reject this shift vector, as there is no a priori limit
555 * to the number of shifts that decrease distances.
557 if (!pbc->bLimitDistance || d2new < pbc->limit_distance2)
559 pbc->limit_distance2 = d2new;
561 pbc->bLimitDistance = TRUE;
566 /* Check if shifts with one box vector less do better */
568 for (dd = 0; dd < DIM; dd++)
570 shift = (dd == 0 ? i : (dd == 1 ? j : k));
574 for (d = 0; d < DIM; d++)
576 d2new_c += sqr(pos[d] + trial[d] - shift*box[dd][d]);
578 if (d2new_c <= BOX_MARGIN*d2new)
586 /* Accept this shift vector. */
587 if (pbc->ntric_vec >= MAX_NTRICVEC)
589 fprintf(stderr, "\nWARNING: Found more than %d triclinic correction vectors, ignoring some.\n"
590 " There is probably something wrong with your box.\n", MAX_NTRICVEC);
591 pr_rvecs(stderr, 0, " Box", box, DIM);
595 copy_rvec(trial, pbc->tric_vec[pbc->ntric_vec]);
596 pbc->tric_shift[pbc->ntric_vec][XX] = i;
597 pbc->tric_shift[pbc->ntric_vec][YY] = j;
598 pbc->tric_shift[pbc->ntric_vec][ZZ] = k;
605 fprintf(debug, " tricvec %2d = %2d %2d %2d %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
606 pbc->ntric_vec, i, j, k,
607 sqrt(d2old), sqrt(d2new),
608 trial[XX], trial[YY], trial[ZZ],
609 pos[XX], pos[YY], pos[ZZ]);
620 void set_pbc(t_pbc *pbc, int ePBC, matrix box)
624 ePBC = guess_ePBC(box);
627 low_set_pbc(pbc, ePBC, NULL, box);
630 t_pbc *set_pbc_dd(t_pbc *pbc, int ePBC,
631 gmx_domdec_t *dd, gmx_bool bSingleDir, matrix box)
642 if (ePBC == epbcSCREW && dd->nc[XX] > 1)
644 /* The rotation has been taken care of during coordinate communication */
648 for (i = 0; i < DIM; i++)
650 if (dd->nc[i] <= (bSingleDir ? 1 : 2))
653 if (!(ePBC == epbcXY && i == ZZ))
667 low_set_pbc(pbc, ePBC, npbcdim < DIM ? &nc2 : NULL, box);
670 return (npbcdim > 0 ? pbc : NULL);
673 void pbc_dx(const t_pbc *pbc, const rvec x1, const rvec x2, rvec dx)
676 rvec dx_start, trial;
680 rvec_sub(x1, x2, dx);
684 case epbcdxRECTANGULAR:
685 for (i = 0; i < DIM; i++)
687 while (dx[i] > pbc->hbox_diag[i])
689 dx[i] -= pbc->fbox_diag[i];
691 while (dx[i] <= pbc->mhbox_diag[i])
693 dx[i] += pbc->fbox_diag[i];
697 case epbcdxTRICLINIC:
698 for (i = DIM-1; i >= 0; i--)
700 while (dx[i] > pbc->hbox_diag[i])
702 for (j = i; j >= 0; j--)
704 dx[j] -= pbc->box[i][j];
707 while (dx[i] <= pbc->mhbox_diag[i])
709 for (j = i; j >= 0; j--)
711 dx[j] += pbc->box[i][j];
715 /* dx is the distance in a rectangular box */
717 if (d2min > pbc->max_cutoff2)
719 copy_rvec(dx, dx_start);
721 /* Now try all possible shifts, when the distance is within max_cutoff
722 * it must be the shortest possible distance.
725 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
727 rvec_add(dx_start, pbc->tric_vec[i], trial);
728 d2trial = norm2(trial);
731 copy_rvec(trial, dx);
739 for (i = 0; i < DIM; i++)
743 while (dx[i] > pbc->hbox_diag[i])
745 dx[i] -= pbc->fbox_diag[i];
747 while (dx[i] <= pbc->mhbox_diag[i])
749 dx[i] += pbc->fbox_diag[i];
756 for (i = DIM-1; i >= 0; i--)
760 while (dx[i] > pbc->hbox_diag[i])
762 for (j = i; j >= 0; j--)
764 dx[j] -= pbc->box[i][j];
767 while (dx[i] <= pbc->mhbox_diag[i])
769 for (j = i; j >= 0; j--)
771 dx[j] += pbc->box[i][j];
774 d2min += dx[i]*dx[i];
777 if (d2min > pbc->max_cutoff2)
779 copy_rvec(dx, dx_start);
781 /* Now try all possible shifts, when the distance is within max_cutoff
782 * it must be the shortest possible distance.
785 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
787 rvec_add(dx_start, pbc->tric_vec[i], trial);
789 for (j = 0; j < DIM; j++)
793 d2trial += trial[j]*trial[j];
798 copy_rvec(trial, dx);
805 case epbcdxSCREW_RECT:
806 /* The shift definition requires x first */
808 while (dx[XX] > pbc->hbox_diag[XX])
810 dx[XX] -= pbc->fbox_diag[XX];
813 while (dx[XX] <= pbc->mhbox_diag[XX])
815 dx[XX] += pbc->fbox_diag[YY];
820 /* Rotate around the x-axis in the middle of the box */
821 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
822 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
824 /* Normal pbc for y and z */
825 for (i = YY; i <= ZZ; i++)
827 while (dx[i] > pbc->hbox_diag[i])
829 dx[i] -= pbc->fbox_diag[i];
831 while (dx[i] <= pbc->mhbox_diag[i])
833 dx[i] += pbc->fbox_diag[i];
838 case epbcdxUNSUPPORTED:
841 gmx_fatal(FARGS, "Internal error in pbc_dx, set_pbc has not been called");
846 int pbc_dx_aiuc(const t_pbc *pbc, const rvec x1, const rvec x2, rvec dx)
849 rvec dx_start, trial;
851 ivec ishift, ishift_start;
853 rvec_sub(x1, x2, dx);
858 case epbcdxRECTANGULAR:
859 for (i = 0; i < DIM; i++)
861 if (dx[i] > pbc->hbox_diag[i])
863 dx[i] -= pbc->fbox_diag[i];
866 else if (dx[i] <= pbc->mhbox_diag[i])
868 dx[i] += pbc->fbox_diag[i];
873 case epbcdxTRICLINIC:
874 /* For triclinic boxes the performance difference between
875 * if/else and two while loops is negligible.
876 * However, the while version can cause extreme delays
877 * before a simulation crashes due to large forces which
878 * can cause unlimited displacements.
879 * Also allowing multiple shifts would index fshift beyond bounds.
881 for (i = DIM-1; i >= 1; i--)
883 if (dx[i] > pbc->hbox_diag[i])
885 for (j = i; j >= 0; j--)
887 dx[j] -= pbc->box[i][j];
891 else if (dx[i] <= pbc->mhbox_diag[i])
893 for (j = i; j >= 0; j--)
895 dx[j] += pbc->box[i][j];
900 /* Allow 2 shifts in x */
901 if (dx[XX] > pbc->hbox_diag[XX])
903 dx[XX] -= pbc->fbox_diag[XX];
905 if (dx[XX] > pbc->hbox_diag[XX])
907 dx[XX] -= pbc->fbox_diag[XX];
911 else if (dx[XX] <= pbc->mhbox_diag[XX])
913 dx[XX] += pbc->fbox_diag[XX];
915 if (dx[XX] <= pbc->mhbox_diag[XX])
917 dx[XX] += pbc->fbox_diag[XX];
921 /* dx is the distance in a rectangular box */
923 if (d2min > pbc->max_cutoff2)
925 copy_rvec(dx, dx_start);
926 copy_ivec(ishift, ishift_start);
928 /* Now try all possible shifts, when the distance is within max_cutoff
929 * it must be the shortest possible distance.
932 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
934 rvec_add(dx_start, pbc->tric_vec[i], trial);
935 d2trial = norm2(trial);
938 copy_rvec(trial, dx);
939 ivec_add(ishift_start, pbc->tric_shift[i], ishift);
947 for (i = 0; i < DIM; i++)
951 if (dx[i] > pbc->hbox_diag[i])
953 dx[i] -= pbc->fbox_diag[i];
956 else if (dx[i] <= pbc->mhbox_diag[i])
958 dx[i] += pbc->fbox_diag[i];
966 for (i = DIM-1; i >= 1; i--)
970 if (dx[i] > pbc->hbox_diag[i])
972 for (j = i; j >= 0; j--)
974 dx[j] -= pbc->box[i][j];
978 else if (dx[i] <= pbc->mhbox_diag[i])
980 for (j = i; j >= 0; j--)
982 dx[j] += pbc->box[i][j];
986 d2min += dx[i]*dx[i];
991 /* Allow 2 shifts in x */
992 if (dx[XX] > pbc->hbox_diag[XX])
994 dx[XX] -= pbc->fbox_diag[XX];
996 if (dx[XX] > pbc->hbox_diag[XX])
998 dx[XX] -= pbc->fbox_diag[XX];
1002 else if (dx[XX] <= pbc->mhbox_diag[XX])
1004 dx[XX] += pbc->fbox_diag[XX];
1006 if (dx[XX] <= pbc->mhbox_diag[XX])
1008 dx[XX] += pbc->fbox_diag[XX];
1012 d2min += dx[XX]*dx[XX];
1014 if (d2min > pbc->max_cutoff2)
1016 copy_rvec(dx, dx_start);
1017 copy_ivec(ishift, ishift_start);
1018 /* Now try all possible shifts, when the distance is within max_cutoff
1019 * it must be the shortest possible distance.
1022 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
1024 rvec_add(dx_start, pbc->tric_vec[i], trial);
1026 for (j = 0; j < DIM; j++)
1030 d2trial += trial[j]*trial[j];
1033 if (d2trial < d2min)
1035 copy_rvec(trial, dx);
1036 ivec_add(ishift_start, pbc->tric_shift[i], ishift);
1045 if (dx[i] > pbc->hbox_diag[i])
1047 dx[i] -= pbc->fbox_diag[i];
1050 else if (dx[i] <= pbc->mhbox_diag[i])
1052 dx[i] += pbc->fbox_diag[i];
1058 if (dx[i] > pbc->hbox_diag[i])
1060 rvec_dec(dx, pbc->box[i]);
1063 else if (dx[i] <= pbc->mhbox_diag[i])
1065 rvec_inc(dx, pbc->box[i]);
1069 case epbcdxSCREW_RECT:
1070 /* The shift definition requires x first */
1071 if (dx[XX] > pbc->hbox_diag[XX])
1073 dx[XX] -= pbc->fbox_diag[XX];
1076 else if (dx[XX] <= pbc->mhbox_diag[XX])
1078 dx[XX] += pbc->fbox_diag[XX];
1081 if (ishift[XX] == 1 || ishift[XX] == -1)
1083 /* Rotate around the x-axis in the middle of the box */
1084 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
1085 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
1087 /* Normal pbc for y and z */
1088 for (i = YY; i <= ZZ; i++)
1090 if (dx[i] > pbc->hbox_diag[i])
1092 dx[i] -= pbc->fbox_diag[i];
1095 else if (dx[i] <= pbc->mhbox_diag[i])
1097 dx[i] += pbc->fbox_diag[i];
1103 case epbcdxUNSUPPORTED:
1106 gmx_fatal(FARGS, "Internal error in pbc_dx_aiuc, set_pbc_dd or set_pbc has not been called");
1110 is = IVEC2IS(ishift);
1113 range_check_mesg(is, 0, SHIFTS, "PBC shift vector index range check.");
1119 void pbc_dx_d(const t_pbc *pbc, const dvec x1, const dvec x2, dvec dx)
1122 dvec dx_start, trial;
1123 double d2min, d2trial;
1126 dvec_sub(x1, x2, dx);
1128 switch (pbc->ePBCDX)
1130 case epbcdxRECTANGULAR:
1132 for (i = 0; i < DIM; i++)
1136 while (dx[i] > pbc->hbox_diag[i])
1138 dx[i] -= pbc->fbox_diag[i];
1140 while (dx[i] <= pbc->mhbox_diag[i])
1142 dx[i] += pbc->fbox_diag[i];
1147 case epbcdxTRICLINIC:
1150 for (i = DIM-1; i >= 0; i--)
1154 while (dx[i] > pbc->hbox_diag[i])
1156 for (j = i; j >= 0; j--)
1158 dx[j] -= pbc->box[i][j];
1161 while (dx[i] <= pbc->mhbox_diag[i])
1163 for (j = i; j >= 0; j--)
1165 dx[j] += pbc->box[i][j];
1168 d2min += dx[i]*dx[i];
1171 if (d2min > pbc->max_cutoff2)
1173 copy_dvec(dx, dx_start);
1174 /* Now try all possible shifts, when the distance is within max_cutoff
1175 * it must be the shortest possible distance.
1178 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
1180 for (j = 0; j < DIM; j++)
1182 trial[j] = dx_start[j] + pbc->tric_vec[i][j];
1185 for (j = 0; j < DIM; j++)
1189 d2trial += trial[j]*trial[j];
1192 if (d2trial < d2min)
1194 copy_dvec(trial, dx);
1201 case epbcdxSCREW_RECT:
1202 /* The shift definition requires x first */
1204 while (dx[XX] > pbc->hbox_diag[XX])
1206 dx[XX] -= pbc->fbox_diag[XX];
1209 while (dx[XX] <= pbc->mhbox_diag[XX])
1211 dx[XX] += pbc->fbox_diag[YY];
1216 /* Rotate around the x-axis in the middle of the box */
1217 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
1218 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
1220 /* Normal pbc for y and z */
1221 for (i = YY; i <= ZZ; i++)
1223 while (dx[i] > pbc->hbox_diag[i])
1225 dx[i] -= pbc->fbox_diag[i];
1227 while (dx[i] <= pbc->mhbox_diag[i])
1229 dx[i] += pbc->fbox_diag[i];
1234 case epbcdxUNSUPPORTED:
1237 gmx_fatal(FARGS, "Internal error in pbc_dx, set_pbc has not been called");
1242 gmx_bool image_rect(ivec xi, ivec xj, ivec box_size, real rlong2, int *shift, real *r2)
1250 for (m = 0; (m < DIM); m++)
1282 gmx_bool image_cylindric(ivec xi, ivec xj, ivec box_size, real rlong2,
1283 int *shift, real *r2)
1291 for (m = 0; (m < DIM); m++)
1327 void calc_shifts(matrix box, rvec shift_vec[])
1329 int k, l, m, d, n, test;
1332 for (m = -D_BOX_Z; m <= D_BOX_Z; m++)
1334 for (l = -D_BOX_Y; l <= D_BOX_Y; l++)
1336 for (k = -D_BOX_X; k <= D_BOX_X; k++, n++)
1338 test = XYZ2IS(k, l, m);
1341 gmx_incons("inconsistent shift numbering");
1343 for (d = 0; d < DIM; d++)
1345 shift_vec[n][d] = k*box[XX][d] + l*box[YY][d] + m*box[ZZ][d];
1352 void calc_box_center(int ecenter, matrix box, rvec box_center)
1356 clear_rvec(box_center);
1360 for (m = 0; (m < DIM); m++)
1362 for (d = 0; d < DIM; d++)
1364 box_center[d] += 0.5*box[m][d];
1369 for (d = 0; d < DIM; d++)
1371 box_center[d] = 0.5*box[d][d];
1377 gmx_fatal(FARGS, "Unsupported value %d for ecenter", ecenter);
1381 void calc_triclinic_images(matrix box, rvec img[])
1385 /* Calculate 3 adjacent images in the xy-plane */
1386 copy_rvec(box[0], img[0]);
1387 copy_rvec(box[1], img[1]);
1390 svmul(-1, img[1], img[1]);
1392 rvec_sub(img[1], img[0], img[2]);
1394 /* Get the next 3 in the xy-plane as mirror images */
1395 for (i = 0; i < 3; i++)
1397 svmul(-1, img[i], img[3+i]);
1400 /* Calculate the first 4 out of xy-plane images */
1401 copy_rvec(box[2], img[6]);
1404 svmul(-1, img[6], img[6]);
1406 for (i = 0; i < 3; i++)
1408 rvec_add(img[6], img[i+1], img[7+i]);
1411 /* Mirror the last 4 from the previous in opposite rotation */
1412 for (i = 0; i < 4; i++)
1414 svmul(-1, img[6 + (2+i) % 4], img[10+i]);
1418 void calc_compact_unitcell_vertices(int ecenter, matrix box, rvec vert[])
1420 rvec img[NTRICIMG], box_center;
1421 int n, i, j, tmp[4], d;
1423 calc_triclinic_images(box, img);
1426 for (i = 2; i <= 5; i += 3)
1439 for (j = 0; j < 4; j++)
1441 for (d = 0; d < DIM; d++)
1443 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1448 for (i = 7; i <= 13; i += 6)
1461 for (j = 0; j < 4; j++)
1463 for (d = 0; d < DIM; d++)
1465 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1470 for (i = 9; i <= 11; i += 2)
1490 for (j = 0; j < 4; j++)
1492 for (d = 0; d < DIM; d++)
1494 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1500 calc_box_center(ecenter, box, box_center);
1501 for (i = 0; i < NCUCVERT; i++)
1503 for (d = 0; d < DIM; d++)
1505 vert[i][d] = vert[i][d]*0.25+box_center[d];
1510 int *compact_unitcell_edges()
1512 /* this is an index in vert[] (see calc_box_vertices) */
1513 /*static int edge[NCUCEDGE*2];*/
1515 static const int hexcon[24] = {
1516 0, 9, 1, 19, 2, 15, 3, 21,
1517 4, 17, 5, 11, 6, 23, 7, 13,
1518 8, 20, 10, 18, 12, 16, 14, 22
1521 gmx_bool bFirst = TRUE;
1523 snew(edge, NCUCEDGE*2);
1528 for (i = 0; i < 6; i++)
1530 for (j = 0; j < 4; j++)
1532 edge[e++] = 4*i + j;
1533 edge[e++] = 4*i + (j+1) % 4;
1536 for (i = 0; i < 12*2; i++)
1538 edge[e++] = hexcon[i];
1547 void put_atoms_in_box_omp(int ePBC, matrix box, int natoms, rvec x[])
1550 nth = gmx_omp_nthreads_get(emntDefault);
1552 #pragma omp parallel for num_threads(nth) schedule(static)
1553 for (t = 0; t < nth; t++)
1557 offset = (natoms*t )/nth;
1558 len = (natoms*(t + 1))/nth - offset;
1559 put_atoms_in_box(ePBC, box, len, x + offset);
1563 void put_atoms_in_box(int ePBC, matrix box, int natoms, rvec x[])
1565 int npbcdim, i, m, d;
1567 if (ePBC == epbcSCREW)
1569 gmx_fatal(FARGS, "Sorry, %s pbc is not yet supported", epbc_names[ePBC]);
1583 for (i = 0; (i < natoms); i++)
1585 for (m = npbcdim-1; m >= 0; m--)
1589 for (d = 0; d <= m; d++)
1591 x[i][d] += box[m][d];
1594 while (x[i][m] >= box[m][m])
1596 for (d = 0; d <= m; d++)
1598 x[i][d] -= box[m][d];
1606 for (i = 0; i < natoms; i++)
1608 for (d = 0; d < npbcdim; d++)
1612 x[i][d] += box[d][d];
1614 while (x[i][d] >= box[d][d])
1616 x[i][d] -= box[d][d];
1623 void put_atoms_in_triclinic_unitcell(int ecenter, matrix box,
1624 int natoms, rvec x[])
1626 rvec box_center, shift_center;
1627 real shm01, shm02, shm12, shift;
1630 calc_box_center(ecenter, box, box_center);
1632 /* The product of matrix shm with a coordinate gives the shift vector
1633 which is required determine the periodic cell position */
1634 shm01 = box[1][0]/box[1][1];
1635 shm02 = (box[1][1]*box[2][0] - box[2][1]*box[1][0])/(box[1][1]*box[2][2]);
1636 shm12 = box[2][1]/box[2][2];
1638 clear_rvec(shift_center);
1639 for (d = 0; d < DIM; d++)
1641 rvec_inc(shift_center, box[d]);
1643 svmul(0.5, shift_center, shift_center);
1644 rvec_sub(box_center, shift_center, shift_center);
1646 shift_center[0] = shm01*shift_center[1] + shm02*shift_center[2];
1647 shift_center[1] = shm12*shift_center[2];
1648 shift_center[2] = 0;
1650 for (i = 0; (i < natoms); i++)
1652 for (m = DIM-1; m >= 0; m--)
1654 shift = shift_center[m];
1657 shift += shm01*x[i][1] + shm02*x[i][2];
1661 shift += shm12*x[i][2];
1663 while (x[i][m]-shift < 0)
1665 for (d = 0; d <= m; d++)
1667 x[i][d] += box[m][d];
1670 while (x[i][m]-shift >= box[m][m])
1672 for (d = 0; d <= m; d++)
1674 x[i][d] -= box[m][d];
1682 put_atoms_in_compact_unitcell(int ePBC, int ecenter, matrix box,
1683 int natoms, rvec x[])
1686 rvec box_center, dx;
1689 set_pbc(&pbc, ePBC, box);
1690 calc_box_center(ecenter, box, box_center);
1691 for (i = 0; i < natoms; i++)
1693 pbc_dx(&pbc, x[i], box_center, dx);
1694 rvec_add(box_center, dx, x[i]);
1697 return pbc.bLimitDistance ?
1698 "WARNING: Could not put all atoms in the compact unitcell\n"