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 /* This file is completely threadsafe - keep it that way! */
45 #include "gromacs/legacyheaders/types/commrec.h"
46 #include "gromacs/legacyheaders/constr.h"
47 #include "gromacs/legacyheaders/copyrite.h"
48 #include "gromacs/math/units.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/pbcutil/pbc.h"
51 #include "gromacs/legacyheaders/mdrun.h"
52 #include "gromacs/legacyheaders/nrnb.h"
53 #include "gromacs/legacyheaders/domdec.h"
54 #include "gromacs/topology/mtop_util.h"
55 #include "gromacs/legacyheaders/gmx_omp_nthreads.h"
57 #include "gromacs/fileio/gmxfio.h"
58 #include "gromacs/topology/block.h"
59 #include "gromacs/utility/fatalerror.h"
60 #include "gromacs/utility/gmxomp.h"
61 #include "gromacs/utility/smalloc.h"
64 int b0; /* first constraint for this thread */
65 int b1; /* b1-1 is the last constraint for this thread */
66 int nind; /* number of indices */
67 int *ind; /* constraint index for updating atom data */
68 int nind_r; /* number of indices */
69 int *ind_r; /* constraint index for updating atom data */
70 int ind_nalloc; /* allocation size of ind and ind_r */
71 tensor vir_r_m_dr; /* temporary variable for virial calculation */
74 typedef struct gmx_lincsdata {
75 int ncg; /* the global number of constraints */
76 int ncg_flex; /* the global number of flexible constraints */
77 int ncg_triangle; /* the global number of constraints in triangles */
78 int nIter; /* the number of iterations */
79 int nOrder; /* the order of the matrix expansion */
80 int nc; /* the number of constraints */
81 int nc_alloc; /* the number we allocated memory for */
82 int ncc; /* the number of constraint connections */
83 int ncc_alloc; /* the number we allocated memory for */
84 real matlam; /* the FE lambda value used for filling blc and blmf */
85 real *bllen0; /* the reference distance in topology A */
86 real *ddist; /* the reference distance in top B - the r.d. in top A */
87 int *bla; /* the atom pairs involved in the constraints */
88 real *blc; /* 1/sqrt(invmass1 + invmass2) */
89 real *blc1; /* as blc, but with all masses 1 */
90 int *blnr; /* index into blbnb and blmf */
91 int *blbnb; /* list of constraint connections */
92 int ntriangle; /* the local number of constraints in triangles */
93 int *triangle; /* the list of triangle constraints */
94 int *tri_bits; /* the bits tell if the matrix element should be used */
95 int ncc_triangle; /* the number of constraint connections in triangles */
96 gmx_bool bCommIter; /* communicate before each LINCS interation */
97 real *blmf; /* matrix of mass factors for constraint connections */
98 real *blmf1; /* as blmf, but with all masses 1 */
99 real *bllen; /* the reference bond length */
100 int nth; /* The number of threads doing LINCS */
101 lincs_thread_t *th; /* LINCS thread division */
102 unsigned *atf; /* atom flags for thread parallelization */
103 int atf_nalloc; /* allocation size of atf */
104 /* arrays for temporary storage in the LINCS algorithm */
111 real *mlambda; /* the Lagrange multipliers * -1 */
112 /* storage for the constraint RMS relative deviation output */
116 real *lincs_rmsd_data(struct gmx_lincsdata *lincsd)
118 return lincsd->rmsd_data;
121 real lincs_rmsd(struct gmx_lincsdata *lincsd, gmx_bool bSD2)
123 if (lincsd->rmsd_data[0] > 0)
125 return sqrt(lincsd->rmsd_data[bSD2 ? 2 : 1]/lincsd->rmsd_data[0]);
133 /* Do a set of nrec LINCS matrix multiplications.
134 * This function will return with up to date thread-local
135 * constraint data, without an OpenMP barrier.
137 static void lincs_matrix_expand(const struct gmx_lincsdata *lincsd,
140 real *rhs1, real *rhs2, real *sol)
142 int nrec, rec, b, j, n, nr0, nr1;
144 int ntriangle, tb, bits;
145 const int *blnr = lincsd->blnr, *blbnb = lincsd->blbnb;
146 const int *triangle = lincsd->triangle, *tri_bits = lincsd->tri_bits;
148 ntriangle = lincsd->ntriangle;
149 nrec = lincsd->nOrder;
151 for (rec = 0; rec < nrec; rec++)
154 for (b = b0; b < b1; b++)
157 for (n = blnr[b]; n < blnr[b+1]; n++)
160 mvb = mvb + blcc[n]*rhs1[j];
163 sol[b] = sol[b] + mvb;
168 } /* nrec*(ncons+2*nrtot) flops */
172 /* Perform an extra nrec recursions for only the constraints
173 * involved in rigid triangles.
174 * In this way their accuracy should come close to those of the other
175 * constraints, since traingles of constraints can produce eigenvalues
176 * around 0.7, while the effective eigenvalue for bond constraints
177 * is around 0.4 (and 0.7*0.7=0.5).
179 /* We need to copy the temporary array, since only the elements
180 * for constraints involved in triangles are updated and then
181 * the pointers are swapped. This saving copying the whole arrary.
182 * We need barrier as other threads might still be reading from rhs2.
185 for (b = b0; b < b1; b++)
192 for (rec = 0; rec < nrec; rec++)
194 for (tb = 0; tb < ntriangle; tb++)
201 for (n = nr0; n < nr1; n++)
203 if (bits & (1<<(n-nr0)))
206 mvb = mvb + blcc[n]*rhs1[j];
210 sol[b] = sol[b] + mvb;
216 } /* flops count is missing here */
218 /* We need a barrier here as the calling routine will continue
219 * to operate on the thread-local constraints without barrier.
225 static void lincs_update_atoms_noind(int ncons, const int *bla,
227 const real *fac, rvec *r,
232 real mvb, im1, im2, tmp0, tmp1, tmp2;
236 for (b = 0; b < ncons; b++)
252 } /* 16 ncons flops */
256 for (b = 0; b < ncons; b++)
274 static void lincs_update_atoms_ind(int ncons, const int *ind, const int *bla,
276 const real *fac, rvec *r,
281 real mvb, im1, im2, tmp0, tmp1, tmp2;
285 for (bi = 0; bi < ncons; bi++)
302 } /* 16 ncons flops */
306 for (bi = 0; bi < ncons; bi++)
321 } /* 16 ncons flops */
325 static void lincs_update_atoms(struct gmx_lincsdata *li, int th,
327 const real *fac, rvec *r,
333 /* Single thread, we simply update for all constraints */
334 lincs_update_atoms_noind(li->nc, li->bla, prefac, fac, r, invmass, x);
338 /* Update the atom vector components for our thread local
339 * constraints that only access our local atom range.
340 * This can be done without a barrier.
342 lincs_update_atoms_ind(li->th[th].nind, li->th[th].ind,
343 li->bla, prefac, fac, r, invmass, x);
345 if (li->th[li->nth].nind > 0)
347 /* Update the constraints that operate on atoms
348 * in multiple thread atom blocks on the master thread.
353 lincs_update_atoms_ind(li->th[li->nth].nind,
355 li->bla, prefac, fac, r, invmass, x);
361 /* LINCS projection, works on derivatives of the coordinates */
362 static void do_lincsp(rvec *x, rvec *f, rvec *fp, t_pbc *pbc,
363 struct gmx_lincsdata *lincsd, int th,
365 int econq, real *dvdlambda,
366 gmx_bool bCalcVir, tensor rmdf)
368 int b0, b1, b, i, j, k, n;
369 real tmp0, tmp1, tmp2, im1, im2, mvb, rlen, len, wfac, lam;
371 int *bla, *blnr, *blbnb;
373 real *blc, *blmf, *blcc, *rhs1, *rhs2, *sol;
375 b0 = lincsd->th[th].b0;
376 b1 = lincsd->th[th].b1;
381 blbnb = lincsd->blbnb;
382 if (econq != econqForce)
384 /* Use mass-weighted parameters */
390 /* Use non mass-weighted parameters */
392 blmf = lincsd->blmf1;
394 blcc = lincsd->tmpncc;
399 /* Compute normalized i-j vectors */
402 for (b = b0; b < b1; b++)
404 pbc_dx_aiuc(pbc, x[bla[2*b]], x[bla[2*b+1]], dx);
410 for (b = b0; b < b1; b++)
412 rvec_sub(x[bla[2*b]], x[bla[2*b+1]], dx);
414 } /* 16 ncons flops */
418 for (b = b0; b < b1; b++)
425 for (n = blnr[b]; n < blnr[b+1]; n++)
428 blcc[n] = blmf[n]*(tmp0*r[k][0] + tmp1*r[k][1] + tmp2*r[k][2]);
430 mvb = blc[b]*(tmp0*(f[i][0] - f[j][0]) +
431 tmp1*(f[i][1] - f[j][1]) +
432 tmp2*(f[i][2] - f[j][2]));
437 /* Together: 23*ncons + 6*nrtot flops */
439 lincs_matrix_expand(lincsd, b0, b1, blcc, rhs1, rhs2, sol);
440 /* nrec*(ncons+2*nrtot) flops */
442 if (econq == econqDeriv_FlexCon)
444 /* We only want to constraint the flexible constraints,
445 * so we mask out the normal ones by setting sol to 0.
447 for (b = b0; b < b1; b++)
449 if (!(lincsd->bllen0[b] == 0 && lincsd->ddist[b] == 0))
456 /* We multiply sol by blc, so we can use lincs_update_atoms for OpenMP */
457 for (b = b0; b < b1; b++)
462 /* When constraining forces, we should not use mass weighting,
463 * so we pass invmass=NULL, which results in the use of 1 for all atoms.
465 lincs_update_atoms(lincsd, th, 1.0, sol, r,
466 (econq != econqForce) ? invmass : NULL, fp);
468 if (dvdlambda != NULL)
471 for (b = b0; b < b1; b++)
473 *dvdlambda -= sol[b]*lincsd->ddist[b];
480 /* Constraint virial,
481 * determines sum r_bond x delta f,
482 * where delta f is the constraint correction
483 * of the quantity that is being constrained.
485 for (b = b0; b < b1; b++)
487 mvb = lincsd->bllen[b]*sol[b];
488 for (i = 0; i < DIM; i++)
491 for (j = 0; j < DIM; j++)
493 rmdf[i][j] += tmp1*r[b][j];
496 } /* 23 ncons flops */
500 static void do_lincs(rvec *x, rvec *xp, matrix box, t_pbc *pbc,
501 struct gmx_lincsdata *lincsd, int th,
504 gmx_bool bCalcLambda,
505 real wangle, int *warn,
507 gmx_bool bCalcVir, tensor vir_r_m_dr)
509 int b0, b1, b, i, j, k, n, iter;
510 real tmp0, tmp1, tmp2, im1, im2, mvb, rlen, len, len2, dlen2, wfac;
512 int *bla, *blnr, *blbnb;
514 real *blc, *blmf, *bllen, *blcc, *rhs1, *rhs2, *sol, *blc_sol, *mlambda;
517 b0 = lincsd->th[th].b0;
518 b1 = lincsd->th[th].b1;
523 blbnb = lincsd->blbnb;
526 bllen = lincsd->bllen;
527 blcc = lincsd->tmpncc;
531 blc_sol = lincsd->tmp4;
532 mlambda = lincsd->mlambda;
534 if (DOMAINDECOMP(cr) && cr->dd->constraints)
536 nlocat = dd_constraints_nlocalatoms(cr->dd);
545 /* Compute normalized i-j vectors */
546 for (b = b0; b < b1; b++)
548 pbc_dx_aiuc(pbc, x[bla[2*b]], x[bla[2*b+1]], dx);
552 for (b = b0; b < b1; b++)
554 for (n = blnr[b]; n < blnr[b+1]; n++)
556 blcc[n] = blmf[n]*iprod(r[b], r[blbnb[n]]);
558 pbc_dx_aiuc(pbc, xp[bla[2*b]], xp[bla[2*b+1]], dx);
559 mvb = blc[b]*(iprod(r[b], dx) - bllen[b]);
566 /* Compute normalized i-j vectors */
567 for (b = b0; b < b1; b++)
571 tmp0 = x[i][0] - x[j][0];
572 tmp1 = x[i][1] - x[j][1];
573 tmp2 = x[i][2] - x[j][2];
574 rlen = gmx_invsqrt(tmp0*tmp0+tmp1*tmp1+tmp2*tmp2);
578 } /* 16 ncons flops */
581 for (b = b0; b < b1; b++)
589 for (n = blnr[b]; n < blnr[b+1]; n++)
592 blcc[n] = blmf[n]*(tmp0*r[k][0] + tmp1*r[k][1] + tmp2*r[k][2]);
594 mvb = blc[b]*(tmp0*(xp[i][0] - xp[j][0]) +
595 tmp1*(xp[i][1] - xp[j][1]) +
596 tmp2*(xp[i][2] - xp[j][2]) - len);
601 /* Together: 26*ncons + 6*nrtot flops */
604 lincs_matrix_expand(lincsd, b0, b1, blcc, rhs1, rhs2, sol);
605 /* nrec*(ncons+2*nrtot) flops */
607 for (b = b0; b < b1; b++)
609 mlambda[b] = blc[b]*sol[b];
612 /* Update the coordinates */
613 lincs_update_atoms(lincsd, th, 1.0, mlambda, r, invmass, xp);
616 ******** Correction for centripetal effects ********
619 wfac = cos(DEG2RAD*wangle);
622 for (iter = 0; iter < lincsd->nIter; iter++)
624 if ((lincsd->bCommIter && DOMAINDECOMP(cr) && cr->dd->constraints))
629 /* Communicate the corrected non-local coordinates */
630 if (DOMAINDECOMP(cr))
632 dd_move_x_constraints(cr->dd, box, xp, NULL, FALSE);
638 for (b = b0; b < b1; b++)
643 pbc_dx_aiuc(pbc, xp[bla[2*b]], xp[bla[2*b+1]], dx);
647 rvec_sub(xp[bla[2*b]], xp[bla[2*b+1]], dx);
650 dlen2 = 2*len2 - norm2(dx);
651 if (dlen2 < wfac*len2 && (nlocat == NULL || nlocat[b]))
653 /* not race free - see detailed comment in caller */
658 mvb = blc[b]*(len - dlen2*gmx_invsqrt(dlen2));
666 } /* 20*ncons flops */
668 lincs_matrix_expand(lincsd, b0, b1, blcc, rhs1, rhs2, sol);
669 /* nrec*(ncons+2*nrtot) flops */
671 for (b = b0; b < b1; b++)
678 /* Update the coordinates */
679 lincs_update_atoms(lincsd, th, 1.0, blc_sol, r, invmass, xp);
681 /* nit*ncons*(37+9*nrec) flops */
685 /* Update the velocities */
686 lincs_update_atoms(lincsd, th, invdt, mlambda, r, invmass, v);
690 if (nlocat != NULL && bCalcLambda)
692 /* In lincs_update_atoms thread might cross-read mlambda */
695 /* Only account for local atoms */
696 for (b = b0; b < b1; b++)
698 mlambda[b] *= 0.5*nlocat[b];
704 /* Constraint virial */
705 for (b = b0; b < b1; b++)
707 tmp0 = -bllen[b]*mlambda[b];
708 for (i = 0; i < DIM; i++)
711 for (j = 0; j < DIM; j++)
713 vir_r_m_dr[i][j] -= tmp1*r[b][j];
716 } /* 22 ncons flops */
720 * 26*ncons + 6*nrtot + nrec*(ncons+2*nrtot)
721 * + nit * (20*ncons + nrec*(ncons+2*nrtot) + 17 ncons)
723 * (26+nrec)*ncons + (6+2*nrec)*nrtot
724 * + nit * ((37+nrec)*ncons + 2*nrec*nrtot)
726 * (63+nrec)*ncons + (6+4*nrec)*nrtot
730 void set_lincs_matrix(struct gmx_lincsdata *li, real *invmass, real lambda)
732 int i, a1, a2, n, k, sign, center;
734 const real invsqrt2 = 0.7071067811865475244;
736 for (i = 0; (i < li->nc); i++)
740 li->blc[i] = gmx_invsqrt(invmass[a1] + invmass[a2]);
741 li->blc1[i] = invsqrt2;
744 /* Construct the coupling coefficient matrix blmf */
746 li->ncc_triangle = 0;
747 for (i = 0; (i < li->nc); i++)
751 for (n = li->blnr[i]; (n < li->blnr[i+1]); n++)
754 if (a1 == li->bla[2*k] || a2 == li->bla[2*k+1])
762 if (a1 == li->bla[2*k] || a1 == li->bla[2*k+1])
772 li->blmf[n] = sign*invmass[center]*li->blc[i]*li->blc[k];
773 li->blmf1[n] = sign*0.5;
774 if (li->ncg_triangle > 0)
776 /* Look for constraint triangles */
777 for (nk = li->blnr[k]; (nk < li->blnr[k+1]); nk++)
780 if (kk != i && kk != k &&
781 (li->bla[2*kk] == end || li->bla[2*kk+1] == end))
783 if (li->ntriangle == 0 ||
784 li->triangle[li->ntriangle-1] < i)
786 /* Add this constraint to the triangle list */
787 li->triangle[li->ntriangle] = i;
788 li->tri_bits[li->ntriangle] = 0;
790 if (li->blnr[i+1] - li->blnr[i] > sizeof(li->tri_bits[0])*8 - 1)
792 gmx_fatal(FARGS, "A constraint is connected to %d constraints, this is more than the %d allowed for constraints participating in triangles",
793 li->blnr[i+1] - li->blnr[i],
794 sizeof(li->tri_bits[0])*8-1);
797 li->tri_bits[li->ntriangle-1] |= (1<<(n-li->blnr[i]));
807 fprintf(debug, "Of the %d constraints %d participate in triangles\n",
808 li->nc, li->ntriangle);
809 fprintf(debug, "There are %d couplings of which %d in triangles\n",
810 li->ncc, li->ncc_triangle);
814 * so we know with which lambda value the masses have been set.
819 static int count_triangle_constraints(t_ilist *ilist, t_blocka *at2con)
822 int c0, a00, a01, n1, c1, a10, a11, ac1, n2, c2, a20, a21;
825 t_iatom *ia1, *ia2, *iap;
827 ncon1 = ilist[F_CONSTR].nr/3;
828 ncon_tot = ncon1 + ilist[F_CONSTRNC].nr/3;
830 ia1 = ilist[F_CONSTR].iatoms;
831 ia2 = ilist[F_CONSTRNC].iatoms;
834 for (c0 = 0; c0 < ncon_tot; c0++)
837 iap = constr_iatomptr(ncon1, ia1, ia2, c0);
840 for (n1 = at2con->index[a01]; n1 < at2con->index[a01+1]; n1++)
845 iap = constr_iatomptr(ncon1, ia1, ia2, c1);
856 for (n2 = at2con->index[ac1]; n2 < at2con->index[ac1+1]; n2++)
859 if (c2 != c0 && c2 != c1)
861 iap = constr_iatomptr(ncon1, ia1, ia2, c2);
864 if (a20 == a00 || a21 == a00)
878 return ncon_triangle;
881 static gmx_bool more_than_two_sequential_constraints(const t_ilist *ilist,
882 const t_blocka *at2con)
884 t_iatom *ia1, *ia2, *iap;
885 int ncon1, ncon_tot, c;
887 gmx_bool bMoreThanTwoSequentialConstraints;
889 ncon1 = ilist[F_CONSTR].nr/3;
890 ncon_tot = ncon1 + ilist[F_CONSTRNC].nr/3;
892 ia1 = ilist[F_CONSTR].iatoms;
893 ia2 = ilist[F_CONSTRNC].iatoms;
895 bMoreThanTwoSequentialConstraints = FALSE;
896 for (c = 0; c < ncon_tot && !bMoreThanTwoSequentialConstraints; c++)
898 iap = constr_iatomptr(ncon1, ia1, ia2, c);
901 /* Check if this constraint has constraints connected at both atoms */
902 if (at2con->index[a1+1] - at2con->index[a1] > 1 &&
903 at2con->index[a2+1] - at2con->index[a2] > 1)
905 bMoreThanTwoSequentialConstraints = TRUE;
909 return bMoreThanTwoSequentialConstraints;
912 static int int_comp(const void *a, const void *b)
914 return (*(int *)a) - (*(int *)b);
917 gmx_lincsdata_t init_lincs(FILE *fplog, gmx_mtop_t *mtop,
918 int nflexcon_global, t_blocka *at2con,
919 gmx_bool bPLINCS, int nIter, int nProjOrder)
921 struct gmx_lincsdata *li;
927 fprintf(fplog, "\nInitializing%s LINear Constraint Solver\n",
928 bPLINCS ? " Parallel" : "");
934 gmx_mtop_ftype_count(mtop, F_CONSTR) +
935 gmx_mtop_ftype_count(mtop, F_CONSTRNC);
936 li->ncg_flex = nflexcon_global;
939 li->nOrder = nProjOrder;
941 li->ncg_triangle = 0;
942 li->bCommIter = FALSE;
943 for (mb = 0; mb < mtop->nmolblock; mb++)
945 molt = &mtop->moltype[mtop->molblock[mb].type];
947 mtop->molblock[mb].nmol*
948 count_triangle_constraints(molt->ilist,
949 &at2con[mtop->molblock[mb].type]);
950 if (bPLINCS && li->bCommIter == FALSE)
952 /* Check if we need to communicate not only before LINCS,
953 * but also before each iteration.
954 * The check for only two sequential constraints is only
955 * useful for the common case of H-bond only constraints.
956 * With more effort we could also make it useful for small
957 * molecules with nr. sequential constraints <= nOrder-1.
959 li->bCommIter = (li->nOrder < 1 || more_than_two_sequential_constraints(molt->ilist, &at2con[mtop->molblock[mb].type]));
962 if (debug && bPLINCS)
964 fprintf(debug, "PLINCS communication before each iteration: %d\n",
968 /* LINCS can run on any number of threads.
969 * Currently the number is fixed for the whole simulation,
970 * but it could be set in set_lincs().
972 li->nth = gmx_omp_nthreads_get(emntLINCS);
979 /* Allocate an extra elements for "thread-overlap" constraints */
980 snew(li->th, li->nth+1);
984 fprintf(debug, "LINCS: using %d threads\n", li->nth);
987 if (bPLINCS || li->ncg_triangle > 0)
989 please_cite(fplog, "Hess2008a");
993 please_cite(fplog, "Hess97a");
998 fprintf(fplog, "The number of constraints is %d\n", li->ncg);
1001 fprintf(fplog, "There are inter charge-group constraints,\n"
1002 "will communicate selected coordinates each lincs iteration\n");
1004 if (li->ncg_triangle > 0)
1007 "%d constraints are involved in constraint triangles,\n"
1008 "will apply an additional matrix expansion of order %d for couplings\n"
1009 "between constraints inside triangles\n",
1010 li->ncg_triangle, li->nOrder);
1017 /* Sets up the work division over the threads */
1018 static void lincs_thread_setup(struct gmx_lincsdata *li, int natoms)
1020 lincs_thread_t *li_m;
1025 if (natoms > li->atf_nalloc)
1027 li->atf_nalloc = over_alloc_large(natoms);
1028 srenew(li->atf, li->atf_nalloc);
1032 /* Clear the atom flags */
1033 for (a = 0; a < natoms; a++)
1038 for (th = 0; th < li->nth; th++)
1040 lincs_thread_t *li_th;
1043 li_th = &li->th[th];
1045 /* The constraints are divided equally over the threads */
1046 li_th->b0 = (li->nc* th )/li->nth;
1047 li_th->b1 = (li->nc*(th+1))/li->nth;
1049 if (th < sizeof(*atf)*8)
1051 /* For each atom set a flag for constraints from each */
1052 for (b = li_th->b0; b < li_th->b1; b++)
1054 atf[li->bla[b*2] ] |= (1U<<th);
1055 atf[li->bla[b*2+1]] |= (1U<<th);
1060 #pragma omp parallel for num_threads(li->nth) schedule(static)
1061 for (th = 0; th < li->nth; th++)
1063 lincs_thread_t *li_th;
1067 li_th = &li->th[th];
1069 if (li_th->b1 - li_th->b0 > li_th->ind_nalloc)
1071 li_th->ind_nalloc = over_alloc_large(li_th->b1-li_th->b0);
1072 srenew(li_th->ind, li_th->ind_nalloc);
1073 srenew(li_th->ind_r, li_th->ind_nalloc);
1076 if (th < sizeof(*atf)*8)
1078 mask = (1U<<th) - 1U;
1082 for (b = li_th->b0; b < li_th->b1; b++)
1084 /* We let the constraint with the lowest thread index
1085 * operate on atoms with constraints from multiple threads.
1087 if (((atf[li->bla[b*2]] & mask) == 0) &&
1088 ((atf[li->bla[b*2+1]] & mask) == 0))
1090 /* Add the constraint to the local atom update index */
1091 li_th->ind[li_th->nind++] = b;
1095 /* Add the constraint to the rest block */
1096 li_th->ind_r[li_th->nind_r++] = b;
1102 /* We are out of bits, assign all constraints to rest */
1103 for (b = li_th->b0; b < li_th->b1; b++)
1105 li_th->ind_r[li_th->nind_r++] = b;
1110 /* We need to copy all constraints which have not be assigned
1111 * to a thread to a separate list which will be handled by one thread.
1113 li_m = &li->th[li->nth];
1116 for (th = 0; th < li->nth; th++)
1118 lincs_thread_t *li_th;
1121 li_th = &li->th[th];
1123 if (li_m->nind + li_th->nind_r > li_m->ind_nalloc)
1125 li_m->ind_nalloc = over_alloc_large(li_m->nind+li_th->nind_r);
1126 srenew(li_m->ind, li_m->ind_nalloc);
1129 for (b = 0; b < li_th->nind_r; b++)
1131 li_m->ind[li_m->nind++] = li_th->ind_r[b];
1136 fprintf(debug, "LINCS thread %d: %d constraints\n",
1143 fprintf(debug, "LINCS thread r: %d constraints\n",
1149 void set_lincs(t_idef *idef, t_mdatoms *md,
1150 gmx_bool bDynamics, t_commrec *cr,
1151 struct gmx_lincsdata *li)
1153 int start, natoms, nflexcon;
1156 int i, k, ncc_alloc, ni, con, nconnect, concon;
1158 real lenA = 0, lenB;
1163 /* Zero the thread index ranges.
1164 * Otherwise without local constraints we could return with old ranges.
1166 for (i = 0; i < li->nth; i++)
1174 li->th[li->nth].nind = 0;
1177 /* This is the local topology, so there are only F_CONSTR constraints */
1178 if (idef->il[F_CONSTR].nr == 0)
1180 /* There are no constraints,
1181 * we do not need to fill any data structures.
1188 fprintf(debug, "Building the LINCS connectivity\n");
1191 if (DOMAINDECOMP(cr))
1193 if (cr->dd->constraints)
1195 dd_get_constraint_range(cr->dd, &start, &natoms);
1199 natoms = cr->dd->nat_home;
1206 natoms = md->homenr;
1208 at2con = make_at2con(start, natoms, idef->il, idef->iparams, bDynamics,
1212 if (idef->il[F_CONSTR].nr/3 > li->nc_alloc || li->nc_alloc == 0)
1214 li->nc_alloc = over_alloc_dd(idef->il[F_CONSTR].nr/3);
1215 srenew(li->bllen0, li->nc_alloc);
1216 srenew(li->ddist, li->nc_alloc);
1217 srenew(li->bla, 2*li->nc_alloc);
1218 srenew(li->blc, li->nc_alloc);
1219 srenew(li->blc1, li->nc_alloc);
1220 srenew(li->blnr, li->nc_alloc+1);
1221 srenew(li->bllen, li->nc_alloc);
1222 srenew(li->tmpv, li->nc_alloc);
1223 srenew(li->tmp1, li->nc_alloc);
1224 srenew(li->tmp2, li->nc_alloc);
1225 srenew(li->tmp3, li->nc_alloc);
1226 srenew(li->tmp4, li->nc_alloc);
1227 srenew(li->mlambda, li->nc_alloc);
1228 if (li->ncg_triangle > 0)
1230 /* This is allocating too much, but it is difficult to improve */
1231 srenew(li->triangle, li->nc_alloc);
1232 srenew(li->tri_bits, li->nc_alloc);
1236 iatom = idef->il[F_CONSTR].iatoms;
1238 ncc_alloc = li->ncc_alloc;
1241 ni = idef->il[F_CONSTR].nr/3;
1245 li->blnr[con] = nconnect;
1246 for (i = 0; i < ni; i++)
1252 lenA = idef->iparams[type].constr.dA;
1253 lenB = idef->iparams[type].constr.dB;
1254 /* Skip the flexible constraints when not doing dynamics */
1255 if (bDynamics || lenA != 0 || lenB != 0)
1257 li->bllen0[con] = lenA;
1258 li->ddist[con] = lenB - lenA;
1259 /* Set the length to the topology A length */
1260 li->bllen[con] = li->bllen0[con];
1261 li->bla[2*con] = a1;
1262 li->bla[2*con+1] = a2;
1263 /* Construct the constraint connection matrix blbnb */
1264 for (k = at2con.index[a1-start]; k < at2con.index[a1-start+1]; k++)
1266 concon = at2con.a[k];
1269 if (nconnect >= ncc_alloc)
1271 ncc_alloc = over_alloc_small(nconnect+1);
1272 srenew(li->blbnb, ncc_alloc);
1274 li->blbnb[nconnect++] = concon;
1277 for (k = at2con.index[a2-start]; k < at2con.index[a2-start+1]; k++)
1279 concon = at2con.a[k];
1282 if (nconnect+1 > ncc_alloc)
1284 ncc_alloc = over_alloc_small(nconnect+1);
1285 srenew(li->blbnb, ncc_alloc);
1287 li->blbnb[nconnect++] = concon;
1290 li->blnr[con+1] = nconnect;
1294 /* Order the blbnb matrix to optimize memory access */
1295 qsort(&(li->blbnb[li->blnr[con]]), li->blnr[con+1]-li->blnr[con],
1296 sizeof(li->blbnb[0]), int_comp);
1298 /* Increase the constraint count */
1303 done_blocka(&at2con);
1305 /* This is the real number of constraints,
1306 * without dynamics the flexible constraints are not present.
1310 li->ncc = li->blnr[con];
1313 /* Since the matrix is static, we can free some memory */
1314 ncc_alloc = li->ncc;
1315 srenew(li->blbnb, ncc_alloc);
1318 if (ncc_alloc > li->ncc_alloc)
1320 li->ncc_alloc = ncc_alloc;
1321 srenew(li->blmf, li->ncc_alloc);
1322 srenew(li->blmf1, li->ncc_alloc);
1323 srenew(li->tmpncc, li->ncc_alloc);
1328 fprintf(debug, "Number of constraints is %d, couplings %d\n",
1335 li->th[0].b1 = li->nc;
1339 lincs_thread_setup(li, md->nr);
1342 set_lincs_matrix(li, md->invmass, md->lambda);
1345 static void lincs_warning(FILE *fplog,
1346 gmx_domdec_t *dd, rvec *x, rvec *xprime, t_pbc *pbc,
1347 int ncons, int *bla, real *bllen, real wangle,
1348 int maxwarn, int *warncount)
1352 real wfac, d0, d1, cosine;
1355 wfac = cos(DEG2RAD*wangle);
1357 sprintf(buf, "bonds that rotated more than %g degrees:\n"
1358 " atom 1 atom 2 angle previous, current, constraint length\n",
1360 fprintf(stderr, "%s", buf);
1363 fprintf(fplog, "%s", buf);
1366 for (b = 0; b < ncons; b++)
1372 pbc_dx_aiuc(pbc, x[i], x[j], v0);
1373 pbc_dx_aiuc(pbc, xprime[i], xprime[j], v1);
1377 rvec_sub(x[i], x[j], v0);
1378 rvec_sub(xprime[i], xprime[j], v1);
1382 cosine = iprod(v0, v1)/(d0*d1);
1385 sprintf(buf, " %6d %6d %5.1f %8.4f %8.4f %8.4f\n",
1386 ddglatnr(dd, i), ddglatnr(dd, j),
1387 RAD2DEG*acos(cosine), d0, d1, bllen[b]);
1388 fprintf(stderr, "%s", buf);
1391 fprintf(fplog, "%s", buf);
1393 if (!gmx_isfinite(d1))
1395 gmx_fatal(FARGS, "Bond length not finite.");
1401 if (*warncount > maxwarn)
1403 too_many_constraint_warnings(econtLINCS, *warncount);
1407 static void cconerr(gmx_domdec_t *dd,
1408 int ncons, int *bla, real *bllen, rvec *x, t_pbc *pbc,
1409 real *ncons_loc, real *ssd, real *max, int *imax)
1411 real len, d, ma, ssd2, r2;
1412 int *nlocat, count, b, im;
1415 if (dd && dd->constraints)
1417 nlocat = dd_constraints_nlocalatoms(dd);
1428 for (b = 0; b < ncons; b++)
1432 pbc_dx_aiuc(pbc, x[bla[2*b]], x[bla[2*b+1]], dx);
1436 rvec_sub(x[bla[2*b]], x[bla[2*b+1]], dx);
1439 len = r2*gmx_invsqrt(r2);
1440 d = fabs(len/bllen[b]-1);
1441 if (d > ma && (nlocat == NULL || nlocat[b]))
1453 ssd2 += nlocat[b]*d*d;
1458 *ncons_loc = (nlocat ? 0.5 : 1)*count;
1459 *ssd = (nlocat ? 0.5 : 1)*ssd2;
1464 gmx_bool constrain_lincs(FILE *fplog, gmx_bool bLog, gmx_bool bEner,
1467 struct gmx_lincsdata *lincsd, t_mdatoms *md,
1469 rvec *x, rvec *xprime, rvec *min_proj,
1470 matrix box, t_pbc *pbc,
1471 real lambda, real *dvdlambda,
1472 real invdt, rvec *v,
1473 gmx_bool bCalcVir, tensor vir_r_m_dr,
1476 int maxwarn, int *warncount)
1478 char buf[STRLEN], buf2[22], buf3[STRLEN];
1479 int i, warn, p_imax, error;
1480 real ncons_loc, p_ssd, p_max = 0;
1486 if (lincsd->nc == 0 && cr->dd == NULL)
1490 lincsd->rmsd_data[0] = 0;
1491 if (ir->eI == eiSD2 && v == NULL)
1499 lincsd->rmsd_data[i] = 0;
1505 if (econq == econqCoord)
1507 if (ir->efep != efepNO)
1509 if (md->nMassPerturbed && lincsd->matlam != md->lambda)
1511 set_lincs_matrix(lincsd, md->invmass, md->lambda);
1514 for (i = 0; i < lincsd->nc; i++)
1516 lincsd->bllen[i] = lincsd->bllen0[i] + lambda*lincsd->ddist[i];
1520 if (lincsd->ncg_flex)
1522 /* Set the flexible constraint lengths to the old lengths */
1525 for (i = 0; i < lincsd->nc; i++)
1527 if (lincsd->bllen[i] == 0)
1529 pbc_dx_aiuc(pbc, x[lincsd->bla[2*i]], x[lincsd->bla[2*i+1]], dx);
1530 lincsd->bllen[i] = norm(dx);
1536 for (i = 0; i < lincsd->nc; i++)
1538 if (lincsd->bllen[i] == 0)
1541 sqrt(distance2(x[lincsd->bla[2*i]],
1542 x[lincsd->bla[2*i+1]]));
1550 cconerr(cr->dd, lincsd->nc, lincsd->bla, lincsd->bllen, xprime, pbc,
1551 &ncons_loc, &p_ssd, &p_max, &p_imax);
1554 /* This warn var can be updated by multiple threads
1555 * at the same time. But as we only need to detect
1556 * if a warning occured or not, this is not an issue.
1560 /* The OpenMP parallel region of constrain_lincs for coords */
1561 #pragma omp parallel num_threads(lincsd->nth)
1563 int th = gmx_omp_get_thread_num();
1565 clear_mat(lincsd->th[th].vir_r_m_dr);
1567 do_lincs(x, xprime, box, pbc, lincsd, th,
1569 bCalcVir || (ir->efep != efepNO),
1570 ir->LincsWarnAngle, &warn,
1572 th == 0 ? vir_r_m_dr : lincsd->th[th].vir_r_m_dr);
1575 if (ir->efep != efepNO)
1577 real dt_2, dvdl = 0;
1579 dt_2 = 1.0/(ir->delta_t*ir->delta_t);
1580 for (i = 0; (i < lincsd->nc); i++)
1582 dvdl -= lincsd->mlambda[i]*dt_2*lincsd->ddist[i];
1587 if (bLog && fplog && lincsd->nc > 0)
1589 fprintf(fplog, " Rel. Constraint Deviation: RMS MAX between atoms\n");
1590 fprintf(fplog, " Before LINCS %.6f %.6f %6d %6d\n",
1591 sqrt(p_ssd/ncons_loc), p_max,
1592 ddglatnr(cr->dd, lincsd->bla[2*p_imax]),
1593 ddglatnr(cr->dd, lincsd->bla[2*p_imax+1]));
1597 cconerr(cr->dd, lincsd->nc, lincsd->bla, lincsd->bllen, xprime, pbc,
1598 &ncons_loc, &p_ssd, &p_max, &p_imax);
1599 /* Check if we are doing the second part of SD */
1600 if (ir->eI == eiSD2 && v == NULL)
1608 lincsd->rmsd_data[0] = ncons_loc;
1609 lincsd->rmsd_data[i] = p_ssd;
1613 lincsd->rmsd_data[0] = 0;
1614 lincsd->rmsd_data[1] = 0;
1615 lincsd->rmsd_data[2] = 0;
1617 if (bLog && fplog && lincsd->nc > 0)
1620 " After LINCS %.6f %.6f %6d %6d\n\n",
1621 sqrt(p_ssd/ncons_loc), p_max,
1622 ddglatnr(cr->dd, lincsd->bla[2*p_imax]),
1623 ddglatnr(cr->dd, lincsd->bla[2*p_imax+1]));
1630 cconerr(cr->dd, lincsd->nc, lincsd->bla, lincsd->bllen, xprime, pbc,
1631 &ncons_loc, &p_ssd, &p_max, &p_imax);
1634 sprintf(buf3, " in simulation %d", cr->ms->sim);
1640 sprintf(buf, "\nStep %s, time %g (ps) LINCS WARNING%s\n"
1641 "relative constraint deviation after LINCS:\n"
1642 "rms %.6f, max %.6f (between atoms %d and %d)\n",
1643 gmx_step_str(step, buf2), ir->init_t+step*ir->delta_t,
1645 sqrt(p_ssd/ncons_loc), p_max,
1646 ddglatnr(cr->dd, lincsd->bla[2*p_imax]),
1647 ddglatnr(cr->dd, lincsd->bla[2*p_imax+1]));
1650 fprintf(fplog, "%s", buf);
1652 fprintf(stderr, "%s", buf);
1653 lincs_warning(fplog, cr->dd, x, xprime, pbc,
1654 lincsd->nc, lincsd->bla, lincsd->bllen,
1655 ir->LincsWarnAngle, maxwarn, warncount);
1657 bOK = (p_max < 0.5);
1660 if (lincsd->ncg_flex)
1662 for (i = 0; (i < lincsd->nc); i++)
1664 if (lincsd->bllen0[i] == 0 && lincsd->ddist[i] == 0)
1666 lincsd->bllen[i] = 0;
1673 /* The OpenMP parallel region of constrain_lincs for derivatives */
1674 #pragma omp parallel num_threads(lincsd->nth)
1676 int th = gmx_omp_get_thread_num();
1678 do_lincsp(x, xprime, min_proj, pbc, lincsd, th,
1679 md->invmass, econq, ir->efep != efepNO ? dvdlambda : NULL,
1680 bCalcVir, th == 0 ? vir_r_m_dr : lincsd->th[th].vir_r_m_dr);
1684 if (bCalcVir && lincsd->nth > 1)
1686 for (i = 1; i < lincsd->nth; i++)
1688 m_add(vir_r_m_dr, lincsd->th[i].vir_r_m_dr, vir_r_m_dr);
1692 /* count assuming nit=1 */
1693 inc_nrnb(nrnb, eNR_LINCS, lincsd->nc);
1694 inc_nrnb(nrnb, eNR_LINCSMAT, (2+lincsd->nOrder)*lincsd->ncc);
1695 if (lincsd->ntriangle > 0)
1697 inc_nrnb(nrnb, eNR_LINCSMAT, lincsd->nOrder*lincsd->ncc_triangle);
1701 inc_nrnb(nrnb, eNR_CONSTR_V, lincsd->nc*2);
1705 inc_nrnb(nrnb, eNR_CONSTR_VIR, lincsd->nc);