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.
45 #include "types/commrec.h"
46 #include "gromacs/math/vec.h"
47 #include "gromacs/math/utilities.h"
49 #include "gromacs/utility/smalloc.h"
51 #include "gromacs/utility/fatalerror.h"
54 #include "gmx_omp_nthreads.h"
56 /* Skip 0 so we have more chance of detecting if we forgot to call set_pbc. */
58 epbcdxRECTANGULAR = 1, epbcdxTRICLINIC,
59 epbcdx2D_RECT, epbcdx2D_TRIC,
60 epbcdx1D_RECT, epbcdx1D_TRIC,
61 epbcdxSCREW_RECT, epbcdxSCREW_TRIC,
62 epbcdxNOPBC, epbcdxUNSUPPORTED
65 /* Margin factor for error message and correction if the box is too skewed */
66 #define BOX_MARGIN 1.0010
67 #define BOX_MARGIN_CORRECT 1.0005
69 int ePBC2npbcdim(int ePBC)
75 case epbcXYZ: npbcdim = 3; break;
76 case epbcXY: npbcdim = 2; break;
77 case epbcSCREW: npbcdim = 3; break;
78 case epbcNONE: npbcdim = 0; break;
79 default: gmx_fatal(FARGS, "Unknown ePBC=%d in ePBC2npbcdim", ePBC);
85 int inputrec2nboundeddim(t_inputrec *ir)
87 if (ir->nwall == 2 && ir->ePBC == epbcXY)
93 return ePBC2npbcdim(ir->ePBC);
97 void dump_pbc(FILE *fp, t_pbc *pbc)
101 fprintf(fp, "ePBCDX = %d\n", pbc->ePBCDX);
102 pr_rvecs(fp, 0, "box", pbc->box, DIM);
103 pr_rvecs(fp, 0, "fbox_diag", &pbc->fbox_diag, 1);
104 pr_rvecs(fp, 0, "hbox_diag", &pbc->hbox_diag, 1);
105 pr_rvecs(fp, 0, "mhbox_diag", &pbc->mhbox_diag, 1);
106 rvec_add(pbc->hbox_diag, pbc->mhbox_diag, sum_box);
107 pr_rvecs(fp, 0, "sum of the above two", &sum_box, 1);
108 fprintf(fp, "max_cutoff2 = %g\n", pbc->max_cutoff2);
109 fprintf(fp, "bLimitDistance = %s\n", EBOOL(pbc->bLimitDistance));
110 fprintf(fp, "limit_distance2 = %g\n", pbc->limit_distance2);
111 fprintf(fp, "ntric_vec = %d\n", pbc->ntric_vec);
112 if (pbc->ntric_vec > 0)
114 pr_ivecs(fp, 0, "tric_shift", pbc->tric_shift, pbc->ntric_vec, FALSE);
115 pr_rvecs(fp, 0, "tric_vec", pbc->tric_vec, pbc->ntric_vec);
119 const char *check_box(int ePBC, matrix box)
125 ePBC = guess_ePBC(box);
128 if (ePBC == epbcNONE)
133 if ((box[XX][YY] != 0) || (box[XX][ZZ] != 0) || (box[YY][ZZ] != 0))
135 ptr = "Only triclinic boxes with the first vector parallel to the x-axis and the second vector in the xy-plane are supported.";
137 else if (ePBC == epbcSCREW && (box[YY][XX] != 0 || box[ZZ][XX] != 0))
139 ptr = "The unit cell can not have off-diagonal x-components with screw pbc";
141 else if (fabs(box[YY][XX]) > BOX_MARGIN*0.5*box[XX][XX] ||
143 (fabs(box[ZZ][XX]) > BOX_MARGIN*0.5*box[XX][XX] ||
144 fabs(box[ZZ][YY]) > BOX_MARGIN*0.5*box[YY][YY])))
146 ptr = "Triclinic box is too skewed.";
156 real max_cutoff2(int ePBC, matrix box)
158 real min_hv2, min_ss;
160 /* Physical limitation of the cut-off
161 * by half the length of the shortest box vector.
163 min_hv2 = min(0.25*norm2(box[XX]), 0.25*norm2(box[YY]));
166 min_hv2 = min(min_hv2, 0.25*norm2(box[ZZ]));
169 /* Limitation to the smallest diagonal element due to optimizations:
170 * checking only linear combinations of single box-vectors (2 in x)
171 * in the grid search and pbc_dx is a lot faster
172 * than checking all possible combinations.
176 min_ss = min(box[XX][XX], box[YY][YY]);
180 min_ss = min(box[XX][XX], min(box[YY][YY]-fabs(box[ZZ][YY]), box[ZZ][ZZ]));
183 return min(min_hv2, min_ss*min_ss);
186 /* this one is mostly harmless... */
187 static gmx_bool bWarnedGuess = FALSE;
189 int guess_ePBC(matrix box)
193 if (box[XX][XX] > 0 && box[YY][YY] > 0 && box[ZZ][ZZ] > 0)
197 else if (box[XX][XX] > 0 && box[YY][YY] > 0 && box[ZZ][ZZ] == 0)
201 else if (box[XX][XX] == 0 && box[YY][YY] == 0 && box[ZZ][ZZ] == 0)
209 fprintf(stderr, "WARNING: Unsupported box diagonal %f %f %f, "
210 "will not use periodic boundary conditions\n\n",
211 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
219 fprintf(debug, "Guessed pbc = %s from the box matrix\n", epbc_names[ePBC]);
225 static int correct_box_elem(FILE *fplog, int step, tensor box, int v, int d)
227 int shift, maxshift = 10;
231 /* correct elem d of vector v with vector d */
232 while (box[v][d] > BOX_MARGIN_CORRECT*0.5*box[d][d])
236 fprintf(fplog, "Step %d: correcting invalid box:\n", step);
237 pr_rvecs(fplog, 0, "old box", box, DIM);
239 rvec_dec(box[v], box[d]);
243 pr_rvecs(fplog, 0, "new box", box, DIM);
245 if (shift <= -maxshift)
248 "Box was shifted at least %d times. Please see log-file.",
252 while (box[v][d] < -BOX_MARGIN_CORRECT*0.5*box[d][d])
256 fprintf(fplog, "Step %d: correcting invalid box:\n", step);
257 pr_rvecs(fplog, 0, "old box", box, DIM);
259 rvec_inc(box[v], box[d]);
263 pr_rvecs(fplog, 0, "new box", box, DIM);
265 if (shift >= maxshift)
268 "Box was shifted at least %d times. Please see log-file.",
276 gmx_bool correct_box(FILE *fplog, int step, tensor box, t_graph *graph)
281 /* check if the box still obeys the restrictions, if not, correct it */
282 zy = correct_box_elem(fplog, step, box, ZZ, YY);
283 zx = correct_box_elem(fplog, step, box, ZZ, XX);
284 yx = correct_box_elem(fplog, step, box, YY, XX);
286 bCorrected = (zy || zx || yx);
288 if (bCorrected && graph)
290 /* correct the graph */
291 for (i = graph->at_start; i < graph->at_end; i++)
293 graph->ishift[i][YY] -= graph->ishift[i][ZZ]*zy;
294 graph->ishift[i][XX] -= graph->ishift[i][ZZ]*zx;
295 graph->ishift[i][XX] -= graph->ishift[i][YY]*yx;
302 int ndof_com(t_inputrec *ir)
313 n = (ir->nwall == 0 ? 3 : 2);
319 gmx_incons("Unknown pbc in calc_nrdf");
325 static void low_set_pbc(t_pbc *pbc, int ePBC, ivec *dd_nc, matrix box)
327 int order[5] = {0, -1, 1, -2, 2};
328 int ii, jj, kk, i, j, k, d, dd, jc, kc, npbcdim, shift;
330 real d2old, d2new, d2new_c;
336 pbc->ndim_ePBC = ePBC2npbcdim(ePBC);
338 copy_mat(box, pbc->box);
339 pbc->bLimitDistance = FALSE;
340 pbc->max_cutoff2 = 0;
343 for (i = 0; (i < DIM); i++)
345 pbc->fbox_diag[i] = box[i][i];
346 pbc->hbox_diag[i] = pbc->fbox_diag[i]*0.5;
347 pbc->mhbox_diag[i] = -pbc->hbox_diag[i];
350 ptr = check_box(ePBC, box);
351 if (ePBC == epbcNONE)
353 pbc->ePBCDX = epbcdxNOPBC;
357 fprintf(stderr, "Warning: %s\n", ptr);
358 pr_rvecs(stderr, 0, " Box", box, DIM);
359 fprintf(stderr, " Can not fix pbc.\n");
360 pbc->ePBCDX = epbcdxUNSUPPORTED;
361 pbc->bLimitDistance = TRUE;
362 pbc->limit_distance2 = 0;
366 if (ePBC == epbcSCREW && dd_nc)
368 /* This combinated should never appear here */
369 gmx_incons("low_set_pbc called with screw pbc and dd_nc != NULL");
373 for (i = 0; i < DIM; i++)
375 if ((dd_nc && (*dd_nc)[i] > 1) || (ePBC == epbcXY && i == ZZ))
388 /* 1D pbc is not an mdp option and it is therefore only used
389 * with single shifts.
391 pbc->ePBCDX = epbcdx1D_RECT;
392 for (i = 0; i < DIM; i++)
399 assert(pbc->dim < DIM);
400 for (i = 0; i < pbc->dim; i++)
402 if (pbc->box[pbc->dim][i] != 0)
404 pbc->ePBCDX = epbcdx1D_TRIC;
409 pbc->ePBCDX = epbcdx2D_RECT;
410 for (i = 0; i < DIM; i++)
417 for (i = 0; i < DIM; i++)
421 for (j = 0; j < i; j++)
423 if (pbc->box[i][j] != 0)
425 pbc->ePBCDX = epbcdx2D_TRIC;
432 if (ePBC != epbcSCREW)
436 pbc->ePBCDX = epbcdxTRICLINIC;
440 pbc->ePBCDX = epbcdxRECTANGULAR;
445 pbc->ePBCDX = (box[ZZ][YY] == 0 ? epbcdxSCREW_RECT : epbcdxSCREW_TRIC);
446 if (pbc->ePBCDX == epbcdxSCREW_TRIC)
449 "Screw pbc is not yet implemented for triclinic boxes.\n"
450 "Can not fix pbc.\n");
451 pbc->ePBCDX = epbcdxUNSUPPORTED;
456 gmx_fatal(FARGS, "Incorrect number of pbc dimensions with DD: %d",
459 pbc->max_cutoff2 = max_cutoff2(ePBC, box);
461 if (pbc->ePBCDX == epbcdxTRICLINIC ||
462 pbc->ePBCDX == epbcdx2D_TRIC ||
463 pbc->ePBCDX == epbcdxSCREW_TRIC)
467 pr_rvecs(debug, 0, "Box", box, DIM);
468 fprintf(debug, "max cutoff %.3f\n", sqrt(pbc->max_cutoff2));
471 /* We will only use single shifts, but we will check a few
472 * more shifts to see if there is a limiting distance
473 * above which we can not be sure of the correct distance.
475 for (kk = 0; kk < 5; kk++)
478 if (!bPBC[ZZ] && k != 0)
482 for (jj = 0; jj < 5; jj++)
485 if (!bPBC[YY] && j != 0)
489 for (ii = 0; ii < 3; ii++)
492 if (!bPBC[XX] && i != 0)
496 /* A shift is only useful when it is trilinic */
497 if (j != 0 || k != 0)
501 for (d = 0; d < DIM; d++)
503 trial[d] = i*box[XX][d] + j*box[YY][d] + k*box[ZZ][d];
504 /* Choose the vector within the brick around 0,0,0 that
505 * will become the shortest due to shift try.
516 pos[d] = min( pbc->hbox_diag[d], -trial[d]);
520 pos[d] = max(-pbc->hbox_diag[d], -trial[d]);
523 d2old += sqr(pos[d]);
524 d2new += sqr(pos[d] + trial[d]);
526 if (BOX_MARGIN*d2new < d2old)
528 if (j < -1 || j > 1 || k < -1 || k > 1)
530 /* Check if there is a single shift vector
531 * that decreases this distance even more.
544 for (d = 0; d < DIM; d++)
546 d2new_c += sqr(pos[d] + trial[d]
547 - jc*box[YY][d] - kc*box[ZZ][d]);
549 if (d2new_c > BOX_MARGIN*d2new)
551 /* Reject this shift vector, as there is no a priori limit
552 * to the number of shifts that decrease distances.
554 if (!pbc->bLimitDistance || d2new < pbc->limit_distance2)
556 pbc->limit_distance2 = d2new;
558 pbc->bLimitDistance = TRUE;
563 /* Check if shifts with one box vector less do better */
565 for (dd = 0; dd < DIM; dd++)
567 shift = (dd == 0 ? i : (dd == 1 ? j : k));
571 for (d = 0; d < DIM; d++)
573 d2new_c += sqr(pos[d] + trial[d] - shift*box[dd][d]);
575 if (d2new_c <= BOX_MARGIN*d2new)
583 /* Accept this shift vector. */
584 if (pbc->ntric_vec >= MAX_NTRICVEC)
586 fprintf(stderr, "\nWARNING: Found more than %d triclinic correction vectors, ignoring some.\n"
587 " There is probably something wrong with your box.\n", MAX_NTRICVEC);
588 pr_rvecs(stderr, 0, " Box", box, DIM);
592 copy_rvec(trial, pbc->tric_vec[pbc->ntric_vec]);
593 pbc->tric_shift[pbc->ntric_vec][XX] = i;
594 pbc->tric_shift[pbc->ntric_vec][YY] = j;
595 pbc->tric_shift[pbc->ntric_vec][ZZ] = k;
602 fprintf(debug, " tricvec %2d = %2d %2d %2d %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
603 pbc->ntric_vec, i, j, k,
604 sqrt(d2old), sqrt(d2new),
605 trial[XX], trial[YY], trial[ZZ],
606 pos[XX], pos[YY], pos[ZZ]);
617 void set_pbc(t_pbc *pbc, int ePBC, matrix box)
621 ePBC = guess_ePBC(box);
624 low_set_pbc(pbc, ePBC, NULL, box);
627 t_pbc *set_pbc_dd(t_pbc *pbc, int ePBC,
628 gmx_domdec_t *dd, gmx_bool bSingleDir, matrix box)
639 if (ePBC == epbcSCREW && dd->nc[XX] > 1)
641 /* The rotation has been taken care of during coordinate communication */
645 for (i = 0; i < DIM; i++)
647 if (dd->nc[i] <= (bSingleDir ? 1 : 2))
650 if (!(ePBC == epbcXY && i == ZZ))
664 low_set_pbc(pbc, ePBC, npbcdim < DIM ? &nc2 : NULL, box);
667 return (npbcdim > 0 ? pbc : NULL);
670 void pbc_dx(const t_pbc *pbc, const rvec x1, const rvec x2, rvec dx)
673 rvec dx_start, trial;
677 rvec_sub(x1, x2, dx);
681 case epbcdxRECTANGULAR:
682 for (i = 0; i < DIM; i++)
684 while (dx[i] > pbc->hbox_diag[i])
686 dx[i] -= pbc->fbox_diag[i];
688 while (dx[i] <= pbc->mhbox_diag[i])
690 dx[i] += pbc->fbox_diag[i];
694 case epbcdxTRICLINIC:
695 for (i = DIM-1; i >= 0; i--)
697 while (dx[i] > pbc->hbox_diag[i])
699 for (j = i; j >= 0; j--)
701 dx[j] -= pbc->box[i][j];
704 while (dx[i] <= pbc->mhbox_diag[i])
706 for (j = i; j >= 0; j--)
708 dx[j] += pbc->box[i][j];
712 /* dx is the distance in a rectangular box */
714 if (d2min > pbc->max_cutoff2)
716 copy_rvec(dx, dx_start);
718 /* Now try all possible shifts, when the distance is within max_cutoff
719 * it must be the shortest possible distance.
722 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
724 rvec_add(dx_start, pbc->tric_vec[i], trial);
725 d2trial = norm2(trial);
728 copy_rvec(trial, dx);
736 for (i = 0; i < DIM; i++)
740 while (dx[i] > pbc->hbox_diag[i])
742 dx[i] -= pbc->fbox_diag[i];
744 while (dx[i] <= pbc->mhbox_diag[i])
746 dx[i] += pbc->fbox_diag[i];
753 for (i = DIM-1; i >= 0; i--)
757 while (dx[i] > pbc->hbox_diag[i])
759 for (j = i; j >= 0; j--)
761 dx[j] -= pbc->box[i][j];
764 while (dx[i] <= pbc->mhbox_diag[i])
766 for (j = i; j >= 0; j--)
768 dx[j] += pbc->box[i][j];
771 d2min += dx[i]*dx[i];
774 if (d2min > pbc->max_cutoff2)
776 copy_rvec(dx, dx_start);
778 /* Now try all possible shifts, when the distance is within max_cutoff
779 * it must be the shortest possible distance.
782 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
784 rvec_add(dx_start, pbc->tric_vec[i], trial);
786 for (j = 0; j < DIM; j++)
790 d2trial += trial[j]*trial[j];
795 copy_rvec(trial, dx);
802 case epbcdxSCREW_RECT:
803 /* The shift definition requires x first */
805 while (dx[XX] > pbc->hbox_diag[XX])
807 dx[XX] -= pbc->fbox_diag[XX];
810 while (dx[XX] <= pbc->mhbox_diag[XX])
812 dx[XX] += pbc->fbox_diag[YY];
817 /* Rotate around the x-axis in the middle of the box */
818 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
819 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
821 /* Normal pbc for y and z */
822 for (i = YY; i <= ZZ; i++)
824 while (dx[i] > pbc->hbox_diag[i])
826 dx[i] -= pbc->fbox_diag[i];
828 while (dx[i] <= pbc->mhbox_diag[i])
830 dx[i] += pbc->fbox_diag[i];
835 case epbcdxUNSUPPORTED:
838 gmx_fatal(FARGS, "Internal error in pbc_dx, set_pbc has not been called");
843 int pbc_dx_aiuc(const t_pbc *pbc, const rvec x1, const rvec x2, rvec dx)
846 rvec dx_start, trial;
848 ivec ishift, ishift_start;
850 rvec_sub(x1, x2, dx);
855 case epbcdxRECTANGULAR:
856 for (i = 0; i < DIM; i++)
858 if (dx[i] > pbc->hbox_diag[i])
860 dx[i] -= pbc->fbox_diag[i];
863 else if (dx[i] <= pbc->mhbox_diag[i])
865 dx[i] += pbc->fbox_diag[i];
870 case epbcdxTRICLINIC:
871 /* For triclinic boxes the performance difference between
872 * if/else and two while loops is negligible.
873 * However, the while version can cause extreme delays
874 * before a simulation crashes due to large forces which
875 * can cause unlimited displacements.
876 * Also allowing multiple shifts would index fshift beyond bounds.
878 for (i = DIM-1; i >= 1; i--)
880 if (dx[i] > pbc->hbox_diag[i])
882 for (j = i; j >= 0; j--)
884 dx[j] -= pbc->box[i][j];
888 else if (dx[i] <= pbc->mhbox_diag[i])
890 for (j = i; j >= 0; j--)
892 dx[j] += pbc->box[i][j];
897 /* Allow 2 shifts in x */
898 if (dx[XX] > pbc->hbox_diag[XX])
900 dx[XX] -= pbc->fbox_diag[XX];
902 if (dx[XX] > pbc->hbox_diag[XX])
904 dx[XX] -= pbc->fbox_diag[XX];
908 else if (dx[XX] <= pbc->mhbox_diag[XX])
910 dx[XX] += pbc->fbox_diag[XX];
912 if (dx[XX] <= pbc->mhbox_diag[XX])
914 dx[XX] += pbc->fbox_diag[XX];
918 /* dx is the distance in a rectangular box */
920 if (d2min > pbc->max_cutoff2)
922 copy_rvec(dx, dx_start);
923 copy_ivec(ishift, ishift_start);
925 /* Now try all possible shifts, when the distance is within max_cutoff
926 * it must be the shortest possible distance.
929 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
931 rvec_add(dx_start, pbc->tric_vec[i], trial);
932 d2trial = norm2(trial);
935 copy_rvec(trial, dx);
936 ivec_add(ishift_start, pbc->tric_shift[i], ishift);
944 for (i = 0; i < DIM; i++)
948 if (dx[i] > pbc->hbox_diag[i])
950 dx[i] -= pbc->fbox_diag[i];
953 else if (dx[i] <= pbc->mhbox_diag[i])
955 dx[i] += pbc->fbox_diag[i];
963 for (i = DIM-1; i >= 1; i--)
967 if (dx[i] > pbc->hbox_diag[i])
969 for (j = i; j >= 0; j--)
971 dx[j] -= pbc->box[i][j];
975 else if (dx[i] <= pbc->mhbox_diag[i])
977 for (j = i; j >= 0; j--)
979 dx[j] += pbc->box[i][j];
983 d2min += dx[i]*dx[i];
988 /* Allow 2 shifts in x */
989 if (dx[XX] > pbc->hbox_diag[XX])
991 dx[XX] -= pbc->fbox_diag[XX];
993 if (dx[XX] > pbc->hbox_diag[XX])
995 dx[XX] -= pbc->fbox_diag[XX];
999 else if (dx[XX] <= pbc->mhbox_diag[XX])
1001 dx[XX] += pbc->fbox_diag[XX];
1003 if (dx[XX] <= pbc->mhbox_diag[XX])
1005 dx[XX] += pbc->fbox_diag[XX];
1009 d2min += dx[XX]*dx[XX];
1011 if (d2min > pbc->max_cutoff2)
1013 copy_rvec(dx, dx_start);
1014 copy_ivec(ishift, ishift_start);
1015 /* Now try all possible shifts, when the distance is within max_cutoff
1016 * it must be the shortest possible distance.
1019 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
1021 rvec_add(dx_start, pbc->tric_vec[i], trial);
1023 for (j = 0; j < DIM; j++)
1027 d2trial += trial[j]*trial[j];
1030 if (d2trial < d2min)
1032 copy_rvec(trial, dx);
1033 ivec_add(ishift_start, pbc->tric_shift[i], ishift);
1042 if (dx[i] > pbc->hbox_diag[i])
1044 dx[i] -= pbc->fbox_diag[i];
1047 else if (dx[i] <= pbc->mhbox_diag[i])
1049 dx[i] += pbc->fbox_diag[i];
1055 if (dx[i] > pbc->hbox_diag[i])
1057 rvec_dec(dx, pbc->box[i]);
1060 else if (dx[i] <= pbc->mhbox_diag[i])
1062 rvec_inc(dx, pbc->box[i]);
1066 case epbcdxSCREW_RECT:
1067 /* The shift definition requires x first */
1068 if (dx[XX] > pbc->hbox_diag[XX])
1070 dx[XX] -= pbc->fbox_diag[XX];
1073 else if (dx[XX] <= pbc->mhbox_diag[XX])
1075 dx[XX] += pbc->fbox_diag[XX];
1078 if (ishift[XX] == 1 || ishift[XX] == -1)
1080 /* Rotate around the x-axis in the middle of the box */
1081 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
1082 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
1084 /* Normal pbc for y and z */
1085 for (i = YY; i <= ZZ; i++)
1087 if (dx[i] > pbc->hbox_diag[i])
1089 dx[i] -= pbc->fbox_diag[i];
1092 else if (dx[i] <= pbc->mhbox_diag[i])
1094 dx[i] += pbc->fbox_diag[i];
1100 case epbcdxUNSUPPORTED:
1103 gmx_fatal(FARGS, "Internal error in pbc_dx_aiuc, set_pbc_dd or set_pbc has not been called");
1107 is = IVEC2IS(ishift);
1110 range_check_mesg(is, 0, SHIFTS, "PBC shift vector index range check.");
1116 void pbc_dx_d(const t_pbc *pbc, const dvec x1, const dvec x2, dvec dx)
1119 dvec dx_start, trial;
1120 double d2min, d2trial;
1123 dvec_sub(x1, x2, dx);
1125 switch (pbc->ePBCDX)
1127 case epbcdxRECTANGULAR:
1129 for (i = 0; i < DIM; i++)
1133 while (dx[i] > pbc->hbox_diag[i])
1135 dx[i] -= pbc->fbox_diag[i];
1137 while (dx[i] <= pbc->mhbox_diag[i])
1139 dx[i] += pbc->fbox_diag[i];
1144 case epbcdxTRICLINIC:
1147 for (i = DIM-1; i >= 0; i--)
1151 while (dx[i] > pbc->hbox_diag[i])
1153 for (j = i; j >= 0; j--)
1155 dx[j] -= pbc->box[i][j];
1158 while (dx[i] <= pbc->mhbox_diag[i])
1160 for (j = i; j >= 0; j--)
1162 dx[j] += pbc->box[i][j];
1165 d2min += dx[i]*dx[i];
1168 if (d2min > pbc->max_cutoff2)
1170 copy_dvec(dx, dx_start);
1171 /* Now try all possible shifts, when the distance is within max_cutoff
1172 * it must be the shortest possible distance.
1175 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
1177 for (j = 0; j < DIM; j++)
1179 trial[j] = dx_start[j] + pbc->tric_vec[i][j];
1182 for (j = 0; j < DIM; j++)
1186 d2trial += trial[j]*trial[j];
1189 if (d2trial < d2min)
1191 copy_dvec(trial, dx);
1198 case epbcdxSCREW_RECT:
1199 /* The shift definition requires x first */
1201 while (dx[XX] > pbc->hbox_diag[XX])
1203 dx[XX] -= pbc->fbox_diag[XX];
1206 while (dx[XX] <= pbc->mhbox_diag[XX])
1208 dx[XX] += pbc->fbox_diag[YY];
1213 /* Rotate around the x-axis in the middle of the box */
1214 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
1215 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
1217 /* Normal pbc for y and z */
1218 for (i = YY; i <= ZZ; i++)
1220 while (dx[i] > pbc->hbox_diag[i])
1222 dx[i] -= pbc->fbox_diag[i];
1224 while (dx[i] <= pbc->mhbox_diag[i])
1226 dx[i] += pbc->fbox_diag[i];
1231 case epbcdxUNSUPPORTED:
1234 gmx_fatal(FARGS, "Internal error in pbc_dx, set_pbc has not been called");
1239 gmx_bool image_rect(ivec xi, ivec xj, ivec box_size, real rlong2, int *shift, real *r2)
1247 for (m = 0; (m < DIM); m++)
1279 gmx_bool image_cylindric(ivec xi, ivec xj, ivec box_size, real rlong2,
1280 int *shift, real *r2)
1288 for (m = 0; (m < DIM); m++)
1324 void calc_shifts(matrix box, rvec shift_vec[])
1326 int k, l, m, d, n, test;
1329 for (m = -D_BOX_Z; m <= D_BOX_Z; m++)
1331 for (l = -D_BOX_Y; l <= D_BOX_Y; l++)
1333 for (k = -D_BOX_X; k <= D_BOX_X; k++, n++)
1335 test = XYZ2IS(k, l, m);
1338 gmx_incons("inconsistent shift numbering");
1340 for (d = 0; d < DIM; d++)
1342 shift_vec[n][d] = k*box[XX][d] + l*box[YY][d] + m*box[ZZ][d];
1349 void calc_box_center(int ecenter, matrix box, rvec box_center)
1353 clear_rvec(box_center);
1357 for (m = 0; (m < DIM); m++)
1359 for (d = 0; d < DIM; d++)
1361 box_center[d] += 0.5*box[m][d];
1366 for (d = 0; d < DIM; d++)
1368 box_center[d] = 0.5*box[d][d];
1374 gmx_fatal(FARGS, "Unsupported value %d for ecenter", ecenter);
1378 void calc_triclinic_images(matrix box, rvec img[])
1382 /* Calculate 3 adjacent images in the xy-plane */
1383 copy_rvec(box[0], img[0]);
1384 copy_rvec(box[1], img[1]);
1387 svmul(-1, img[1], img[1]);
1389 rvec_sub(img[1], img[0], img[2]);
1391 /* Get the next 3 in the xy-plane as mirror images */
1392 for (i = 0; i < 3; i++)
1394 svmul(-1, img[i], img[3+i]);
1397 /* Calculate the first 4 out of xy-plane images */
1398 copy_rvec(box[2], img[6]);
1401 svmul(-1, img[6], img[6]);
1403 for (i = 0; i < 3; i++)
1405 rvec_add(img[6], img[i+1], img[7+i]);
1408 /* Mirror the last 4 from the previous in opposite rotation */
1409 for (i = 0; i < 4; i++)
1411 svmul(-1, img[6 + (2+i) % 4], img[10+i]);
1415 void calc_compact_unitcell_vertices(int ecenter, matrix box, rvec vert[])
1417 rvec img[NTRICIMG], box_center;
1418 int n, i, j, tmp[4], d;
1420 calc_triclinic_images(box, img);
1423 for (i = 2; i <= 5; i += 3)
1436 for (j = 0; j < 4; j++)
1438 for (d = 0; d < DIM; d++)
1440 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1445 for (i = 7; i <= 13; i += 6)
1458 for (j = 0; j < 4; j++)
1460 for (d = 0; d < DIM; d++)
1462 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1467 for (i = 9; i <= 11; i += 2)
1487 for (j = 0; j < 4; j++)
1489 for (d = 0; d < DIM; d++)
1491 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1497 calc_box_center(ecenter, box, box_center);
1498 for (i = 0; i < NCUCVERT; i++)
1500 for (d = 0; d < DIM; d++)
1502 vert[i][d] = vert[i][d]*0.25+box_center[d];
1507 int *compact_unitcell_edges()
1509 /* this is an index in vert[] (see calc_box_vertices) */
1510 /*static int edge[NCUCEDGE*2];*/
1512 static const int hexcon[24] = {
1513 0, 9, 1, 19, 2, 15, 3, 21,
1514 4, 17, 5, 11, 6, 23, 7, 13,
1515 8, 20, 10, 18, 12, 16, 14, 22
1518 gmx_bool bFirst = TRUE;
1520 snew(edge, NCUCEDGE*2);
1525 for (i = 0; i < 6; i++)
1527 for (j = 0; j < 4; j++)
1529 edge[e++] = 4*i + j;
1530 edge[e++] = 4*i + (j+1) % 4;
1533 for (i = 0; i < 12*2; i++)
1535 edge[e++] = hexcon[i];
1544 void put_atoms_in_box_omp(int ePBC, matrix box, int natoms, rvec x[])
1547 nth = gmx_omp_nthreads_get(emntDefault);
1549 #pragma omp parallel for num_threads(nth) schedule(static)
1550 for (t = 0; t < nth; t++)
1554 offset = (natoms*t )/nth;
1555 len = (natoms*(t + 1))/nth - offset;
1556 put_atoms_in_box(ePBC, box, len, x + offset);
1560 void put_atoms_in_box(int ePBC, matrix box, int natoms, rvec x[])
1562 int npbcdim, i, m, d;
1564 if (ePBC == epbcSCREW)
1566 gmx_fatal(FARGS, "Sorry, %s pbc is not yet supported", epbc_names[ePBC]);
1580 for (i = 0; (i < natoms); i++)
1582 for (m = npbcdim-1; m >= 0; m--)
1586 for (d = 0; d <= m; d++)
1588 x[i][d] += box[m][d];
1591 while (x[i][m] >= box[m][m])
1593 for (d = 0; d <= m; d++)
1595 x[i][d] -= box[m][d];
1603 for (i = 0; i < natoms; i++)
1605 for (d = 0; d < npbcdim; d++)
1609 x[i][d] += box[d][d];
1611 while (x[i][d] >= box[d][d])
1613 x[i][d] -= box[d][d];
1620 void put_atoms_in_triclinic_unitcell(int ecenter, matrix box,
1621 int natoms, rvec x[])
1623 rvec box_center, shift_center;
1624 real shm01, shm02, shm12, shift;
1627 calc_box_center(ecenter, box, box_center);
1629 /* The product of matrix shm with a coordinate gives the shift vector
1630 which is required determine the periodic cell position */
1631 shm01 = box[1][0]/box[1][1];
1632 shm02 = (box[1][1]*box[2][0] - box[2][1]*box[1][0])/(box[1][1]*box[2][2]);
1633 shm12 = box[2][1]/box[2][2];
1635 clear_rvec(shift_center);
1636 for (d = 0; d < DIM; d++)
1638 rvec_inc(shift_center, box[d]);
1640 svmul(0.5, shift_center, shift_center);
1641 rvec_sub(box_center, shift_center, shift_center);
1643 shift_center[0] = shm01*shift_center[1] + shm02*shift_center[2];
1644 shift_center[1] = shm12*shift_center[2];
1645 shift_center[2] = 0;
1647 for (i = 0; (i < natoms); i++)
1649 for (m = DIM-1; m >= 0; m--)
1651 shift = shift_center[m];
1654 shift += shm01*x[i][1] + shm02*x[i][2];
1658 shift += shm12*x[i][2];
1660 while (x[i][m]-shift < 0)
1662 for (d = 0; d <= m; d++)
1664 x[i][d] += box[m][d];
1667 while (x[i][m]-shift >= box[m][m])
1669 for (d = 0; d <= m; d++)
1671 x[i][d] -= box[m][d];
1679 put_atoms_in_compact_unitcell(int ePBC, int ecenter, matrix box,
1680 int natoms, rvec x[])
1683 rvec box_center, dx;
1686 set_pbc(&pbc, ePBC, box);
1687 calc_box_center(ecenter, box, box_center);
1688 for (i = 0; i < natoms; i++)
1690 pbc_dx(&pbc, x[i], box_center, dx);
1691 rvec_add(box_center, dx, x[i]);
1694 return pbc.bLimitDistance ?
1695 "WARNING: Could not put all atoms in the compact unitcell\n"