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.
39 #include "gromacs/pbcutil/pbc.h"
44 #include "gromacs/legacyheaders/types/inputrec.h"
45 #include "gromacs/legacyheaders/types/commrec.h"
46 #include "gromacs/legacyheaders/txtdump.h"
47 #include "gromacs/legacyheaders/names.h"
48 #include "gromacs/legacyheaders/macros.h"
49 #include "gromacs/legacyheaders/gmx_omp_nthreads.h"
51 #include "gromacs/math/utilities.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/pbcutil/ishift.h"
54 #include "gromacs/pbcutil/mshift.h"
55 #include "gromacs/utility/fatalerror.h"
56 #include "gromacs/utility/smalloc.h"
58 /* Skip 0 so we have more chance of detecting if we forgot to call set_pbc. */
60 epbcdxRECTANGULAR = 1, epbcdxTRICLINIC,
61 epbcdx2D_RECT, epbcdx2D_TRIC,
62 epbcdx1D_RECT, epbcdx1D_TRIC,
63 epbcdxSCREW_RECT, epbcdxSCREW_TRIC,
64 epbcdxNOPBC, epbcdxUNSUPPORTED
67 /* Margin factor for error message and correction if the box is too skewed */
68 #define BOX_MARGIN 1.0010
69 #define BOX_MARGIN_CORRECT 1.0005
71 int ePBC2npbcdim(int ePBC)
77 case epbcXYZ: npbcdim = 3; break;
78 case epbcXY: npbcdim = 2; break;
79 case epbcSCREW: npbcdim = 3; break;
80 case epbcNONE: npbcdim = 0; break;
81 default: gmx_fatal(FARGS, "Unknown ePBC=%d in ePBC2npbcdim", ePBC);
87 int inputrec2nboundeddim(t_inputrec *ir)
89 if (ir->nwall == 2 && ir->ePBC == epbcXY)
95 return ePBC2npbcdim(ir->ePBC);
99 void dump_pbc(FILE *fp, t_pbc *pbc)
103 fprintf(fp, "ePBCDX = %d\n", pbc->ePBCDX);
104 pr_rvecs(fp, 0, "box", pbc->box, DIM);
105 pr_rvecs(fp, 0, "fbox_diag", &pbc->fbox_diag, 1);
106 pr_rvecs(fp, 0, "hbox_diag", &pbc->hbox_diag, 1);
107 pr_rvecs(fp, 0, "mhbox_diag", &pbc->mhbox_diag, 1);
108 rvec_add(pbc->hbox_diag, pbc->mhbox_diag, sum_box);
109 pr_rvecs(fp, 0, "sum of the above two", &sum_box, 1);
110 fprintf(fp, "max_cutoff2 = %g\n", pbc->max_cutoff2);
111 fprintf(fp, "bLimitDistance = %s\n", EBOOL(pbc->bLimitDistance));
112 fprintf(fp, "limit_distance2 = %g\n", pbc->limit_distance2);
113 fprintf(fp, "ntric_vec = %d\n", pbc->ntric_vec);
114 if (pbc->ntric_vec > 0)
116 pr_ivecs(fp, 0, "tric_shift", pbc->tric_shift, pbc->ntric_vec, FALSE);
117 pr_rvecs(fp, 0, "tric_vec", pbc->tric_vec, pbc->ntric_vec);
121 const char *check_box(int ePBC, matrix box)
127 ePBC = guess_ePBC(box);
130 if (ePBC == epbcNONE)
135 if ((box[XX][YY] != 0) || (box[XX][ZZ] != 0) || (box[YY][ZZ] != 0))
137 ptr = "Only triclinic boxes with the first vector parallel to the x-axis and the second vector in the xy-plane are supported.";
139 else if (ePBC == epbcSCREW && (box[YY][XX] != 0 || box[ZZ][XX] != 0))
141 ptr = "The unit cell can not have off-diagonal x-components with screw pbc";
143 else if (fabs(box[YY][XX]) > BOX_MARGIN*0.5*box[XX][XX] ||
145 (fabs(box[ZZ][XX]) > BOX_MARGIN*0.5*box[XX][XX] ||
146 fabs(box[ZZ][YY]) > BOX_MARGIN*0.5*box[YY][YY])))
148 ptr = "Triclinic box is too skewed.";
158 real max_cutoff2(int ePBC, matrix box)
160 real min_hv2, min_ss;
162 /* Physical limitation of the cut-off
163 * by half the length of the shortest box vector.
165 min_hv2 = min(0.25*norm2(box[XX]), 0.25*norm2(box[YY]));
168 min_hv2 = min(min_hv2, 0.25*norm2(box[ZZ]));
171 /* Limitation to the smallest diagonal element due to optimizations:
172 * checking only linear combinations of single box-vectors (2 in x)
173 * in the grid search and pbc_dx is a lot faster
174 * than checking all possible combinations.
178 min_ss = min(box[XX][XX], box[YY][YY]);
182 min_ss = min(box[XX][XX], min(box[YY][YY]-fabs(box[ZZ][YY]), box[ZZ][ZZ]));
185 return min(min_hv2, min_ss*min_ss);
188 /* this one is mostly harmless... */
189 static gmx_bool bWarnedGuess = FALSE;
191 int guess_ePBC(matrix box)
195 if (box[XX][XX] > 0 && box[YY][YY] > 0 && box[ZZ][ZZ] > 0)
199 else if (box[XX][XX] > 0 && box[YY][YY] > 0 && box[ZZ][ZZ] == 0)
203 else if (box[XX][XX] == 0 && box[YY][YY] == 0 && box[ZZ][ZZ] == 0)
211 fprintf(stderr, "WARNING: Unsupported box diagonal %f %f %f, "
212 "will not use periodic boundary conditions\n\n",
213 box[XX][XX], box[YY][YY], box[ZZ][ZZ]);
221 fprintf(debug, "Guessed pbc = %s from the box matrix\n", epbc_names[ePBC]);
227 static int correct_box_elem(FILE *fplog, int step, tensor box, int v, int d)
229 int shift, maxshift = 10;
233 /* correct elem d of vector v with vector d */
234 while (box[v][d] > BOX_MARGIN_CORRECT*0.5*box[d][d])
238 fprintf(fplog, "Step %d: correcting invalid box:\n", step);
239 pr_rvecs(fplog, 0, "old box", box, DIM);
241 rvec_dec(box[v], box[d]);
245 pr_rvecs(fplog, 0, "new box", box, DIM);
247 if (shift <= -maxshift)
250 "Box was shifted at least %d times. Please see log-file.",
254 while (box[v][d] < -BOX_MARGIN_CORRECT*0.5*box[d][d])
258 fprintf(fplog, "Step %d: correcting invalid box:\n", step);
259 pr_rvecs(fplog, 0, "old box", box, DIM);
261 rvec_inc(box[v], box[d]);
265 pr_rvecs(fplog, 0, "new box", box, DIM);
267 if (shift >= maxshift)
270 "Box was shifted at least %d times. Please see log-file.",
278 gmx_bool correct_box(FILE *fplog, int step, tensor box, t_graph *graph)
283 /* check if the box still obeys the restrictions, if not, correct it */
284 zy = correct_box_elem(fplog, step, box, ZZ, YY);
285 zx = correct_box_elem(fplog, step, box, ZZ, XX);
286 yx = correct_box_elem(fplog, step, box, YY, XX);
288 bCorrected = (zy || zx || yx);
290 if (bCorrected && graph)
292 /* correct the graph */
293 for (i = graph->at_start; i < graph->at_end; i++)
295 graph->ishift[i][YY] -= graph->ishift[i][ZZ]*zy;
296 graph->ishift[i][XX] -= graph->ishift[i][ZZ]*zx;
297 graph->ishift[i][XX] -= graph->ishift[i][YY]*yx;
304 int ndof_com(t_inputrec *ir)
315 n = (ir->nwall == 0 ? 3 : 2);
321 gmx_incons("Unknown pbc in calc_nrdf");
327 static void low_set_pbc(t_pbc *pbc, int ePBC, ivec *dd_nc, matrix box)
329 int order[5] = {0, -1, 1, -2, 2};
330 int ii, jj, kk, i, j, k, d, dd, jc, kc, npbcdim, shift;
332 real d2old, d2new, d2new_c;
338 pbc->ndim_ePBC = ePBC2npbcdim(ePBC);
340 copy_mat(box, pbc->box);
341 pbc->bLimitDistance = FALSE;
342 pbc->max_cutoff2 = 0;
345 for (i = 0; (i < DIM); i++)
347 pbc->fbox_diag[i] = box[i][i];
348 pbc->hbox_diag[i] = pbc->fbox_diag[i]*0.5;
349 pbc->mhbox_diag[i] = -pbc->hbox_diag[i];
352 ptr = check_box(ePBC, box);
353 if (ePBC == epbcNONE)
355 pbc->ePBCDX = epbcdxNOPBC;
359 fprintf(stderr, "Warning: %s\n", ptr);
360 pr_rvecs(stderr, 0, " Box", box, DIM);
361 fprintf(stderr, " Can not fix pbc.\n");
362 pbc->ePBCDX = epbcdxUNSUPPORTED;
363 pbc->bLimitDistance = TRUE;
364 pbc->limit_distance2 = 0;
368 if (ePBC == epbcSCREW && dd_nc)
370 /* This combinated should never appear here */
371 gmx_incons("low_set_pbc called with screw pbc and dd_nc != NULL");
375 for (i = 0; i < DIM; i++)
377 if ((dd_nc && (*dd_nc)[i] > 1) || (ePBC == epbcXY && i == ZZ))
390 /* 1D pbc is not an mdp option and it is therefore only used
391 * with single shifts.
393 pbc->ePBCDX = epbcdx1D_RECT;
394 for (i = 0; i < DIM; i++)
401 assert(pbc->dim < DIM);
402 for (i = 0; i < pbc->dim; i++)
404 if (pbc->box[pbc->dim][i] != 0)
406 pbc->ePBCDX = epbcdx1D_TRIC;
411 pbc->ePBCDX = epbcdx2D_RECT;
412 for (i = 0; i < DIM; i++)
419 for (i = 0; i < DIM; i++)
423 for (j = 0; j < i; j++)
425 if (pbc->box[i][j] != 0)
427 pbc->ePBCDX = epbcdx2D_TRIC;
434 if (ePBC != epbcSCREW)
438 pbc->ePBCDX = epbcdxTRICLINIC;
442 pbc->ePBCDX = epbcdxRECTANGULAR;
447 pbc->ePBCDX = (box[ZZ][YY] == 0 ? epbcdxSCREW_RECT : epbcdxSCREW_TRIC);
448 if (pbc->ePBCDX == epbcdxSCREW_TRIC)
451 "Screw pbc is not yet implemented for triclinic boxes.\n"
452 "Can not fix pbc.\n");
453 pbc->ePBCDX = epbcdxUNSUPPORTED;
458 gmx_fatal(FARGS, "Incorrect number of pbc dimensions with DD: %d",
461 pbc->max_cutoff2 = max_cutoff2(ePBC, box);
463 if (pbc->ePBCDX == epbcdxTRICLINIC ||
464 pbc->ePBCDX == epbcdx2D_TRIC ||
465 pbc->ePBCDX == epbcdxSCREW_TRIC)
469 pr_rvecs(debug, 0, "Box", box, DIM);
470 fprintf(debug, "max cutoff %.3f\n", sqrt(pbc->max_cutoff2));
473 /* We will only use single shifts, but we will check a few
474 * more shifts to see if there is a limiting distance
475 * above which we can not be sure of the correct distance.
477 for (kk = 0; kk < 5; kk++)
480 if (!bPBC[ZZ] && k != 0)
484 for (jj = 0; jj < 5; jj++)
487 if (!bPBC[YY] && j != 0)
491 for (ii = 0; ii < 3; ii++)
494 if (!bPBC[XX] && i != 0)
498 /* A shift is only useful when it is trilinic */
499 if (j != 0 || k != 0)
503 for (d = 0; d < DIM; d++)
505 trial[d] = i*box[XX][d] + j*box[YY][d] + k*box[ZZ][d];
506 /* Choose the vector within the brick around 0,0,0 that
507 * will become the shortest due to shift try.
518 pos[d] = min( pbc->hbox_diag[d], -trial[d]);
522 pos[d] = max(-pbc->hbox_diag[d], -trial[d]);
525 d2old += sqr(pos[d]);
526 d2new += sqr(pos[d] + trial[d]);
528 if (BOX_MARGIN*d2new < d2old)
530 if (j < -1 || j > 1 || k < -1 || k > 1)
532 /* Check if there is a single shift vector
533 * that decreases this distance even more.
546 for (d = 0; d < DIM; d++)
548 d2new_c += sqr(pos[d] + trial[d]
549 - jc*box[YY][d] - kc*box[ZZ][d]);
551 if (d2new_c > BOX_MARGIN*d2new)
553 /* Reject this shift vector, as there is no a priori limit
554 * to the number of shifts that decrease distances.
556 if (!pbc->bLimitDistance || d2new < pbc->limit_distance2)
558 pbc->limit_distance2 = d2new;
560 pbc->bLimitDistance = TRUE;
565 /* Check if shifts with one box vector less do better */
567 for (dd = 0; dd < DIM; dd++)
569 shift = (dd == 0 ? i : (dd == 1 ? j : k));
573 for (d = 0; d < DIM; d++)
575 d2new_c += sqr(pos[d] + trial[d] - shift*box[dd][d]);
577 if (d2new_c <= BOX_MARGIN*d2new)
585 /* Accept this shift vector. */
586 if (pbc->ntric_vec >= MAX_NTRICVEC)
588 fprintf(stderr, "\nWARNING: Found more than %d triclinic correction vectors, ignoring some.\n"
589 " There is probably something wrong with your box.\n", MAX_NTRICVEC);
590 pr_rvecs(stderr, 0, " Box", box, DIM);
594 copy_rvec(trial, pbc->tric_vec[pbc->ntric_vec]);
595 pbc->tric_shift[pbc->ntric_vec][XX] = i;
596 pbc->tric_shift[pbc->ntric_vec][YY] = j;
597 pbc->tric_shift[pbc->ntric_vec][ZZ] = k;
604 fprintf(debug, " tricvec %2d = %2d %2d %2d %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
605 pbc->ntric_vec, i, j, k,
606 sqrt(d2old), sqrt(d2new),
607 trial[XX], trial[YY], trial[ZZ],
608 pos[XX], pos[YY], pos[ZZ]);
619 void set_pbc(t_pbc *pbc, int ePBC, matrix box)
623 ePBC = guess_ePBC(box);
626 low_set_pbc(pbc, ePBC, NULL, box);
629 t_pbc *set_pbc_dd(t_pbc *pbc, int ePBC,
630 gmx_domdec_t *dd, gmx_bool bSingleDir, matrix box)
641 if (ePBC == epbcSCREW && dd->nc[XX] > 1)
643 /* The rotation has been taken care of during coordinate communication */
647 for (i = 0; i < DIM; i++)
649 if (dd->nc[i] <= (bSingleDir ? 1 : 2))
652 if (!(ePBC == epbcXY && i == ZZ))
666 low_set_pbc(pbc, ePBC, npbcdim < DIM ? &nc2 : NULL, box);
669 return (npbcdim > 0 ? pbc : NULL);
672 void pbc_dx(const t_pbc *pbc, const rvec x1, const rvec x2, rvec dx)
675 rvec dx_start, trial;
679 rvec_sub(x1, x2, dx);
683 case epbcdxRECTANGULAR:
684 for (i = 0; i < DIM; i++)
686 while (dx[i] > pbc->hbox_diag[i])
688 dx[i] -= pbc->fbox_diag[i];
690 while (dx[i] <= pbc->mhbox_diag[i])
692 dx[i] += pbc->fbox_diag[i];
696 case epbcdxTRICLINIC:
697 for (i = DIM-1; i >= 0; i--)
699 while (dx[i] > pbc->hbox_diag[i])
701 for (j = i; j >= 0; j--)
703 dx[j] -= pbc->box[i][j];
706 while (dx[i] <= pbc->mhbox_diag[i])
708 for (j = i; j >= 0; j--)
710 dx[j] += pbc->box[i][j];
714 /* dx is the distance in a rectangular box */
716 if (d2min > pbc->max_cutoff2)
718 copy_rvec(dx, dx_start);
720 /* Now try all possible shifts, when the distance is within max_cutoff
721 * it must be the shortest possible distance.
724 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
726 rvec_add(dx_start, pbc->tric_vec[i], trial);
727 d2trial = norm2(trial);
730 copy_rvec(trial, dx);
738 for (i = 0; i < DIM; i++)
742 while (dx[i] > pbc->hbox_diag[i])
744 dx[i] -= pbc->fbox_diag[i];
746 while (dx[i] <= pbc->mhbox_diag[i])
748 dx[i] += pbc->fbox_diag[i];
755 for (i = DIM-1; i >= 0; i--)
759 while (dx[i] > pbc->hbox_diag[i])
761 for (j = i; j >= 0; j--)
763 dx[j] -= pbc->box[i][j];
766 while (dx[i] <= pbc->mhbox_diag[i])
768 for (j = i; j >= 0; j--)
770 dx[j] += pbc->box[i][j];
773 d2min += dx[i]*dx[i];
776 if (d2min > pbc->max_cutoff2)
778 copy_rvec(dx, dx_start);
780 /* Now try all possible shifts, when the distance is within max_cutoff
781 * it must be the shortest possible distance.
784 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
786 rvec_add(dx_start, pbc->tric_vec[i], trial);
788 for (j = 0; j < DIM; j++)
792 d2trial += trial[j]*trial[j];
797 copy_rvec(trial, dx);
804 case epbcdxSCREW_RECT:
805 /* The shift definition requires x first */
807 while (dx[XX] > pbc->hbox_diag[XX])
809 dx[XX] -= pbc->fbox_diag[XX];
812 while (dx[XX] <= pbc->mhbox_diag[XX])
814 dx[XX] += pbc->fbox_diag[YY];
819 /* Rotate around the x-axis in the middle of the box */
820 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
821 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
823 /* Normal pbc for y and z */
824 for (i = YY; i <= ZZ; i++)
826 while (dx[i] > pbc->hbox_diag[i])
828 dx[i] -= pbc->fbox_diag[i];
830 while (dx[i] <= pbc->mhbox_diag[i])
832 dx[i] += pbc->fbox_diag[i];
837 case epbcdxUNSUPPORTED:
840 gmx_fatal(FARGS, "Internal error in pbc_dx, set_pbc has not been called");
845 int pbc_dx_aiuc(const t_pbc *pbc, const rvec x1, const rvec x2, rvec dx)
848 rvec dx_start, trial;
850 ivec ishift, ishift_start;
852 rvec_sub(x1, x2, dx);
857 case epbcdxRECTANGULAR:
858 for (i = 0; i < DIM; i++)
860 if (dx[i] > pbc->hbox_diag[i])
862 dx[i] -= pbc->fbox_diag[i];
865 else if (dx[i] <= pbc->mhbox_diag[i])
867 dx[i] += pbc->fbox_diag[i];
872 case epbcdxTRICLINIC:
873 /* For triclinic boxes the performance difference between
874 * if/else and two while loops is negligible.
875 * However, the while version can cause extreme delays
876 * before a simulation crashes due to large forces which
877 * can cause unlimited displacements.
878 * Also allowing multiple shifts would index fshift beyond bounds.
880 for (i = DIM-1; i >= 1; i--)
882 if (dx[i] > pbc->hbox_diag[i])
884 for (j = i; j >= 0; j--)
886 dx[j] -= pbc->box[i][j];
890 else if (dx[i] <= pbc->mhbox_diag[i])
892 for (j = i; j >= 0; j--)
894 dx[j] += pbc->box[i][j];
899 /* Allow 2 shifts in x */
900 if (dx[XX] > pbc->hbox_diag[XX])
902 dx[XX] -= pbc->fbox_diag[XX];
904 if (dx[XX] > pbc->hbox_diag[XX])
906 dx[XX] -= pbc->fbox_diag[XX];
910 else if (dx[XX] <= pbc->mhbox_diag[XX])
912 dx[XX] += pbc->fbox_diag[XX];
914 if (dx[XX] <= pbc->mhbox_diag[XX])
916 dx[XX] += pbc->fbox_diag[XX];
920 /* dx is the distance in a rectangular box */
922 if (d2min > pbc->max_cutoff2)
924 copy_rvec(dx, dx_start);
925 copy_ivec(ishift, ishift_start);
927 /* Now try all possible shifts, when the distance is within max_cutoff
928 * it must be the shortest possible distance.
931 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
933 rvec_add(dx_start, pbc->tric_vec[i], trial);
934 d2trial = norm2(trial);
937 copy_rvec(trial, dx);
938 ivec_add(ishift_start, pbc->tric_shift[i], ishift);
946 for (i = 0; i < DIM; i++)
950 if (dx[i] > pbc->hbox_diag[i])
952 dx[i] -= pbc->fbox_diag[i];
955 else if (dx[i] <= pbc->mhbox_diag[i])
957 dx[i] += pbc->fbox_diag[i];
965 for (i = DIM-1; i >= 1; i--)
969 if (dx[i] > pbc->hbox_diag[i])
971 for (j = i; j >= 0; j--)
973 dx[j] -= pbc->box[i][j];
977 else if (dx[i] <= pbc->mhbox_diag[i])
979 for (j = i; j >= 0; j--)
981 dx[j] += pbc->box[i][j];
985 d2min += dx[i]*dx[i];
990 /* Allow 2 shifts in x */
991 if (dx[XX] > pbc->hbox_diag[XX])
993 dx[XX] -= pbc->fbox_diag[XX];
995 if (dx[XX] > pbc->hbox_diag[XX])
997 dx[XX] -= pbc->fbox_diag[XX];
1001 else if (dx[XX] <= pbc->mhbox_diag[XX])
1003 dx[XX] += pbc->fbox_diag[XX];
1005 if (dx[XX] <= pbc->mhbox_diag[XX])
1007 dx[XX] += pbc->fbox_diag[XX];
1011 d2min += dx[XX]*dx[XX];
1013 if (d2min > pbc->max_cutoff2)
1015 copy_rvec(dx, dx_start);
1016 copy_ivec(ishift, ishift_start);
1017 /* Now try all possible shifts, when the distance is within max_cutoff
1018 * it must be the shortest possible distance.
1021 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
1023 rvec_add(dx_start, pbc->tric_vec[i], trial);
1025 for (j = 0; j < DIM; j++)
1029 d2trial += trial[j]*trial[j];
1032 if (d2trial < d2min)
1034 copy_rvec(trial, dx);
1035 ivec_add(ishift_start, pbc->tric_shift[i], ishift);
1044 if (dx[i] > pbc->hbox_diag[i])
1046 dx[i] -= pbc->fbox_diag[i];
1049 else if (dx[i] <= pbc->mhbox_diag[i])
1051 dx[i] += pbc->fbox_diag[i];
1057 if (dx[i] > pbc->hbox_diag[i])
1059 rvec_dec(dx, pbc->box[i]);
1062 else if (dx[i] <= pbc->mhbox_diag[i])
1064 rvec_inc(dx, pbc->box[i]);
1068 case epbcdxSCREW_RECT:
1069 /* The shift definition requires x first */
1070 if (dx[XX] > pbc->hbox_diag[XX])
1072 dx[XX] -= pbc->fbox_diag[XX];
1075 else if (dx[XX] <= pbc->mhbox_diag[XX])
1077 dx[XX] += pbc->fbox_diag[XX];
1080 if (ishift[XX] == 1 || ishift[XX] == -1)
1082 /* Rotate around the x-axis in the middle of the box */
1083 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
1084 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
1086 /* Normal pbc for y and z */
1087 for (i = YY; i <= ZZ; i++)
1089 if (dx[i] > pbc->hbox_diag[i])
1091 dx[i] -= pbc->fbox_diag[i];
1094 else if (dx[i] <= pbc->mhbox_diag[i])
1096 dx[i] += pbc->fbox_diag[i];
1102 case epbcdxUNSUPPORTED:
1105 gmx_fatal(FARGS, "Internal error in pbc_dx_aiuc, set_pbc_dd or set_pbc has not been called");
1109 is = IVEC2IS(ishift);
1112 range_check_mesg(is, 0, SHIFTS, "PBC shift vector index range check.");
1118 void pbc_dx_d(const t_pbc *pbc, const dvec x1, const dvec x2, dvec dx)
1121 dvec dx_start, trial;
1122 double d2min, d2trial;
1125 dvec_sub(x1, x2, dx);
1127 switch (pbc->ePBCDX)
1129 case epbcdxRECTANGULAR:
1131 for (i = 0; i < DIM; i++)
1135 while (dx[i] > pbc->hbox_diag[i])
1137 dx[i] -= pbc->fbox_diag[i];
1139 while (dx[i] <= pbc->mhbox_diag[i])
1141 dx[i] += pbc->fbox_diag[i];
1146 case epbcdxTRICLINIC:
1149 for (i = DIM-1; i >= 0; i--)
1153 while (dx[i] > pbc->hbox_diag[i])
1155 for (j = i; j >= 0; j--)
1157 dx[j] -= pbc->box[i][j];
1160 while (dx[i] <= pbc->mhbox_diag[i])
1162 for (j = i; j >= 0; j--)
1164 dx[j] += pbc->box[i][j];
1167 d2min += dx[i]*dx[i];
1170 if (d2min > pbc->max_cutoff2)
1172 copy_dvec(dx, dx_start);
1173 /* Now try all possible shifts, when the distance is within max_cutoff
1174 * it must be the shortest possible distance.
1177 while ((d2min > pbc->max_cutoff2) && (i < pbc->ntric_vec))
1179 for (j = 0; j < DIM; j++)
1181 trial[j] = dx_start[j] + pbc->tric_vec[i][j];
1184 for (j = 0; j < DIM; j++)
1188 d2trial += trial[j]*trial[j];
1191 if (d2trial < d2min)
1193 copy_dvec(trial, dx);
1200 case epbcdxSCREW_RECT:
1201 /* The shift definition requires x first */
1203 while (dx[XX] > pbc->hbox_diag[XX])
1205 dx[XX] -= pbc->fbox_diag[XX];
1208 while (dx[XX] <= pbc->mhbox_diag[XX])
1210 dx[XX] += pbc->fbox_diag[YY];
1215 /* Rotate around the x-axis in the middle of the box */
1216 dx[YY] = pbc->box[YY][YY] - x1[YY] - x2[YY];
1217 dx[ZZ] = pbc->box[ZZ][ZZ] - x1[ZZ] - x2[ZZ];
1219 /* Normal pbc for y and z */
1220 for (i = YY; i <= ZZ; i++)
1222 while (dx[i] > pbc->hbox_diag[i])
1224 dx[i] -= pbc->fbox_diag[i];
1226 while (dx[i] <= pbc->mhbox_diag[i])
1228 dx[i] += pbc->fbox_diag[i];
1233 case epbcdxUNSUPPORTED:
1236 gmx_fatal(FARGS, "Internal error in pbc_dx, set_pbc has not been called");
1241 gmx_bool image_rect(ivec xi, ivec xj, ivec box_size, real rlong2, int *shift, real *r2)
1249 for (m = 0; (m < DIM); m++)
1281 gmx_bool image_cylindric(ivec xi, ivec xj, ivec box_size, real rlong2,
1282 int *shift, real *r2)
1290 for (m = 0; (m < DIM); m++)
1326 void calc_shifts(matrix box, rvec shift_vec[])
1328 int k, l, m, d, n, test;
1331 for (m = -D_BOX_Z; m <= D_BOX_Z; m++)
1333 for (l = -D_BOX_Y; l <= D_BOX_Y; l++)
1335 for (k = -D_BOX_X; k <= D_BOX_X; k++, n++)
1337 test = XYZ2IS(k, l, m);
1340 gmx_incons("inconsistent shift numbering");
1342 for (d = 0; d < DIM; d++)
1344 shift_vec[n][d] = k*box[XX][d] + l*box[YY][d] + m*box[ZZ][d];
1351 void calc_box_center(int ecenter, matrix box, rvec box_center)
1355 clear_rvec(box_center);
1359 for (m = 0; (m < DIM); m++)
1361 for (d = 0; d < DIM; d++)
1363 box_center[d] += 0.5*box[m][d];
1368 for (d = 0; d < DIM; d++)
1370 box_center[d] = 0.5*box[d][d];
1376 gmx_fatal(FARGS, "Unsupported value %d for ecenter", ecenter);
1380 void calc_triclinic_images(matrix box, rvec img[])
1384 /* Calculate 3 adjacent images in the xy-plane */
1385 copy_rvec(box[0], img[0]);
1386 copy_rvec(box[1], img[1]);
1389 svmul(-1, img[1], img[1]);
1391 rvec_sub(img[1], img[0], img[2]);
1393 /* Get the next 3 in the xy-plane as mirror images */
1394 for (i = 0; i < 3; i++)
1396 svmul(-1, img[i], img[3+i]);
1399 /* Calculate the first 4 out of xy-plane images */
1400 copy_rvec(box[2], img[6]);
1403 svmul(-1, img[6], img[6]);
1405 for (i = 0; i < 3; i++)
1407 rvec_add(img[6], img[i+1], img[7+i]);
1410 /* Mirror the last 4 from the previous in opposite rotation */
1411 for (i = 0; i < 4; i++)
1413 svmul(-1, img[6 + (2+i) % 4], img[10+i]);
1417 void calc_compact_unitcell_vertices(int ecenter, matrix box, rvec vert[])
1419 rvec img[NTRICIMG], box_center;
1420 int n, i, j, tmp[4], d;
1422 calc_triclinic_images(box, img);
1425 for (i = 2; i <= 5; i += 3)
1438 for (j = 0; j < 4; j++)
1440 for (d = 0; d < DIM; d++)
1442 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1447 for (i = 7; i <= 13; i += 6)
1460 for (j = 0; j < 4; j++)
1462 for (d = 0; d < DIM; d++)
1464 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1469 for (i = 9; i <= 11; i += 2)
1489 for (j = 0; j < 4; j++)
1491 for (d = 0; d < DIM; d++)
1493 vert[n][d] = img[i][d]+img[tmp[j]][d]+img[tmp[(j+1)%4]][d];
1499 calc_box_center(ecenter, box, box_center);
1500 for (i = 0; i < NCUCVERT; i++)
1502 for (d = 0; d < DIM; d++)
1504 vert[i][d] = vert[i][d]*0.25+box_center[d];
1509 int *compact_unitcell_edges()
1511 /* this is an index in vert[] (see calc_box_vertices) */
1512 /*static int edge[NCUCEDGE*2];*/
1514 static const int hexcon[24] = {
1515 0, 9, 1, 19, 2, 15, 3, 21,
1516 4, 17, 5, 11, 6, 23, 7, 13,
1517 8, 20, 10, 18, 12, 16, 14, 22
1520 gmx_bool bFirst = TRUE;
1522 snew(edge, NCUCEDGE*2);
1527 for (i = 0; i < 6; i++)
1529 for (j = 0; j < 4; j++)
1531 edge[e++] = 4*i + j;
1532 edge[e++] = 4*i + (j+1) % 4;
1535 for (i = 0; i < 12*2; i++)
1537 edge[e++] = hexcon[i];
1546 void put_atoms_in_box_omp(int ePBC, matrix box, int natoms, rvec x[])
1549 nth = gmx_omp_nthreads_get(emntDefault);
1551 #pragma omp parallel for num_threads(nth) schedule(static)
1552 for (t = 0; t < nth; t++)
1556 offset = (natoms*t )/nth;
1557 len = (natoms*(t + 1))/nth - offset;
1558 put_atoms_in_box(ePBC, box, len, x + offset);
1562 void put_atoms_in_box(int ePBC, matrix box, int natoms, rvec x[])
1564 int npbcdim, i, m, d;
1566 if (ePBC == epbcSCREW)
1568 gmx_fatal(FARGS, "Sorry, %s pbc is not yet supported", epbc_names[ePBC]);
1582 for (i = 0; (i < natoms); i++)
1584 for (m = npbcdim-1; m >= 0; m--)
1588 for (d = 0; d <= m; d++)
1590 x[i][d] += box[m][d];
1593 while (x[i][m] >= box[m][m])
1595 for (d = 0; d <= m; d++)
1597 x[i][d] -= box[m][d];
1605 for (i = 0; i < natoms; i++)
1607 for (d = 0; d < npbcdim; d++)
1611 x[i][d] += box[d][d];
1613 while (x[i][d] >= box[d][d])
1615 x[i][d] -= box[d][d];
1622 void put_atoms_in_triclinic_unitcell(int ecenter, matrix box,
1623 int natoms, rvec x[])
1625 rvec box_center, shift_center;
1626 real shm01, shm02, shm12, shift;
1629 calc_box_center(ecenter, box, box_center);
1631 /* The product of matrix shm with a coordinate gives the shift vector
1632 which is required determine the periodic cell position */
1633 shm01 = box[1][0]/box[1][1];
1634 shm02 = (box[1][1]*box[2][0] - box[2][1]*box[1][0])/(box[1][1]*box[2][2]);
1635 shm12 = box[2][1]/box[2][2];
1637 clear_rvec(shift_center);
1638 for (d = 0; d < DIM; d++)
1640 rvec_inc(shift_center, box[d]);
1642 svmul(0.5, shift_center, shift_center);
1643 rvec_sub(box_center, shift_center, shift_center);
1645 shift_center[0] = shm01*shift_center[1] + shm02*shift_center[2];
1646 shift_center[1] = shm12*shift_center[2];
1647 shift_center[2] = 0;
1649 for (i = 0; (i < natoms); i++)
1651 for (m = DIM-1; m >= 0; m--)
1653 shift = shift_center[m];
1656 shift += shm01*x[i][1] + shm02*x[i][2];
1660 shift += shm12*x[i][2];
1662 while (x[i][m]-shift < 0)
1664 for (d = 0; d <= m; d++)
1666 x[i][d] += box[m][d];
1669 while (x[i][m]-shift >= box[m][m])
1671 for (d = 0; d <= m; d++)
1673 x[i][d] -= box[m][d];
1681 put_atoms_in_compact_unitcell(int ePBC, int ecenter, matrix box,
1682 int natoms, rvec x[])
1685 rvec box_center, dx;
1688 set_pbc(&pbc, ePBC, box);
1689 calc_box_center(ecenter, box, box_center);
1690 for (i = 0; i < natoms; i++)
1692 pbc_dx(&pbc, x[i], box_center, dx);
1693 rvec_add(box_center, dx, x[i]);
1696 return pbc.bLimitDistance ?
1697 "WARNING: Could not put all atoms in the compact unitcell\n"