1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROwing Monsters And Cloning Shrimps
36 /* This file is completely threadsafe - keep it that way! */
53 #include "mtop_util.h"
55 #include "gmx_omp_nthreads.h"
59 int b0; /* first constraint for this thread */
60 int b1; /* b1-1 is the last constraint for this thread */
61 int nind; /* number of indices */
62 int *ind; /* constraint index for updating atom data */
63 int nind_r; /* number of indices */
64 int *ind_r; /* constraint index for updating atom data */
65 int ind_nalloc; /* allocation size of ind and ind_r */
68 typedef struct gmx_lincsdata {
69 int ncg; /* the global number of constraints */
70 int ncg_flex; /* the global number of flexible constraints */
71 int ncg_triangle;/* the global number of constraints in triangles */
72 int nIter; /* the number of iterations */
73 int nOrder; /* the order of the matrix expansion */
74 int nc; /* the number of constraints */
75 int nc_alloc; /* the number we allocated memory for */
76 int ncc; /* the number of constraint connections */
77 int ncc_alloc; /* the number we allocated memory for */
78 real matlam; /* the FE lambda value used for filling blc and blmf */
79 real *bllen0; /* the reference distance in topology A */
80 real *ddist; /* the reference distance in top B - the r.d. in top A */
81 int *bla; /* the atom pairs involved in the constraints */
82 real *blc; /* 1/sqrt(invmass1 + invmass2) */
83 real *blc1; /* as blc, but with all masses 1 */
84 int *blnr; /* index into blbnb and blmf */
85 int *blbnb; /* list of constraint connections */
86 int ntriangle; /* the local number of constraints in triangles */
87 int *triangle; /* the list of triangle constraints */
88 int *tri_bits; /* the bits tell if the matrix element should be used */
89 int ncc_triangle;/* the number of constraint connections in triangles */
90 real *blmf; /* matrix of mass factors for constraint connections */
91 real *blmf1; /* as blmf, but with all masses 1 */
92 real *bllen; /* the reference bond length */
93 int nth; /* The number of threads doing LINCS */
94 lincs_thread_t *th; /* LINCS thread division */
95 unsigned *atf; /* atom flags for thread parallelization */
96 int atf_nalloc; /* allocation size of atf */
97 /* arrays for temporary storage in the LINCS algorithm */
104 real *mlambda; /* the Lagrange multipliers * -1 */
105 /* storage for the constraint RMS relative deviation output */
109 real *lincs_rmsd_data(struct gmx_lincsdata *lincsd)
111 return lincsd->rmsd_data;
114 real lincs_rmsd(struct gmx_lincsdata *lincsd,gmx_bool bSD2)
116 if (lincsd->rmsd_data[0] > 0)
118 return sqrt(lincsd->rmsd_data[bSD2 ? 2 : 1]/lincsd->rmsd_data[0]);
126 /* Do a set of nrec LINCS matrix multiplications.
127 * This function will return with up to date thread-local
128 * constraint data, without an OpenMP barrier.
130 static void lincs_matrix_expand(const struct gmx_lincsdata *lincsd,
133 real *rhs1,real *rhs2,real *sol)
135 int nrec,rec,b,j,n,nr0,nr1;
137 int ntriangle,tb,bits;
138 const int *blnr=lincsd->blnr,*blbnb=lincsd->blbnb;
139 const int *triangle=lincsd->triangle,*tri_bits=lincsd->tri_bits;
141 ntriangle = lincsd->ntriangle;
142 nrec = lincsd->nOrder;
144 for(rec=0; rec<nrec; rec++)
150 for(n=blnr[b]; n<blnr[b+1]; n++)
153 mvb = mvb + blcc[n]*rhs1[j];
156 sol[b] = sol[b] + mvb;
161 } /* nrec*(ncons+2*nrtot) flops */
165 /* Perform an extra nrec recursions for only the constraints
166 * involved in rigid triangles.
167 * In this way their accuracy should come close to those of the other
168 * constraints, since traingles of constraints can produce eigenvalues
169 * around 0.7, while the effective eigenvalue for bond constraints
170 * is around 0.4 (and 0.7*0.7=0.5).
172 /* We need to copy the temporary array, since only the elements
173 * for constraints involved in triangles are updated and then
174 * the pointers are swapped. This saving copying the whole arrary.
175 * We need barrier as other threads might still be reading from rhs2.
185 for(rec=0; rec<nrec; rec++)
187 for(tb=0; tb<ntriangle; tb++)
194 for(n=nr0; n<nr1; n++)
196 if (bits & (1<<(n-nr0)))
199 mvb = mvb + blcc[n]*rhs1[j];
203 sol[b] = sol[b] + mvb;
209 } /* flops count is missing here */
211 /* We need a barrier here as the calling routine will continue
212 * to operate on the thread-local constraints without barrier.
218 static void lincs_update_atoms_noind(int ncons,const int *bla,
220 const real *fac,rvec *r,
225 real mvb,im1,im2,tmp0,tmp1,tmp2;
227 for(b=0; b<ncons; b++)
243 } /* 16 ncons flops */
246 static void lincs_update_atoms_ind(int ncons,const int *ind,const int *bla,
248 const real *fac,rvec *r,
253 real mvb,im1,im2,tmp0,tmp1,tmp2;
255 for(bi=0; bi<ncons; bi++)
272 } /* 16 ncons flops */
275 static void lincs_update_atoms(struct gmx_lincsdata *li,int th,
277 const real *fac,rvec *r,
283 /* Single thread, we simply update for all constraints */
284 lincs_update_atoms_noind(li->nc,li->bla,prefac,fac,r,invmass,x);
288 /* Update the atom vector components for our thread local
289 * constraints that only access our local atom range.
290 * This can be done without a barrier.
292 lincs_update_atoms_ind(li->th[th].nind,li->th[th].ind,
293 li->bla,prefac,fac,r,invmass,x);
295 if (li->th[li->nth].nind > 0)
297 /* Update the constraints that operate on atoms
298 * in multiple thread atom blocks on the master thread.
303 lincs_update_atoms_ind(li->th[li->nth].nind,
305 li->bla,prefac,fac,r,invmass,x);
311 static void do_lincsp(rvec *x,rvec *f,rvec *fp,t_pbc *pbc,
312 struct gmx_lincsdata *lincsd,real *invmass,
313 int econq,real *dvdlambda,
314 gmx_bool bCalcVir,tensor rmdf)
317 real tmp0,tmp1,tmp2,im1,im2,mvb,rlen,len,wfac,lam;
319 int ncons,*bla,*blnr,*blbnb;
321 real *blc,*blmf,*blcc,*rhs1,*rhs2,*sol;
327 blbnb = lincsd->blbnb;
328 if (econq != econqForce)
330 /* Use mass-weighted parameters */
336 /* Use non mass-weighted parameters */
338 blmf = lincsd->blmf1;
340 blcc = lincsd->tmpncc;
345 if (econq != econqForce)
350 /* Compute normalized i-j vectors */
353 for(b=0; b<ncons; b++)
355 pbc_dx_aiuc(pbc,x[bla[2*b]],x[bla[2*b+1]],dx);
361 for(b=0; b<ncons; b++)
363 rvec_sub(x[bla[2*b]],x[bla[2*b+1]],dx);
365 } /* 16 ncons flops */
368 for(b=0; b<ncons; b++)
375 for(n=blnr[b]; n<blnr[b+1]; n++)
378 blcc[n] = blmf[n]*(tmp0*r[k][0] + tmp1*r[k][1] + tmp2*r[k][2]);
380 mvb = blc[b]*(tmp0*(f[i][0] - f[j][0]) +
381 tmp1*(f[i][1] - f[j][1]) +
382 tmp2*(f[i][2] - f[j][2]));
387 /* Together: 23*ncons + 6*nrtot flops */
389 lincs_matrix_expand(lincsd,0,ncons,blcc,rhs1,rhs2,sol);
390 /* nrec*(ncons+2*nrtot) flops */
392 if (econq != econqForce)
394 for(b=0; b<ncons; b++)
396 /* With econqDeriv_FlexCon only use the flexible constraints */
397 if (econq != econqDeriv_FlexCon ||
398 (lincsd->bllen0[b] == 0 && lincsd->ddist[b] == 0))
408 fp[i][0] -= tmp0*im1;
409 fp[i][1] -= tmp1*im1;
410 fp[i][2] -= tmp2*im1;
411 fp[j][0] += tmp0*im2;
412 fp[j][1] += tmp1*im2;
413 fp[j][2] += tmp2*im2;
416 /* This is only correct with forces and invmass=1 */
417 *dvdlambda -= mvb*lincsd->ddist[b];
420 } /* 16 ncons flops */
424 for(b=0; b<ncons; b++)
440 *dvdlambda -= mvb*lincsd->ddist[b];
448 /* Constraint virial,
449 * determines sum r_bond x delta f,
450 * where delta f is the constraint correction
451 * of the quantity that is being constrained.
453 for(b=0; b<ncons; b++)
455 mvb = lincsd->bllen[b]*blc[b]*sol[b];
461 rmdf[i][j] += tmp1*r[b][j];
464 } /* 23 ncons flops */
468 static void do_lincs(rvec *x,rvec *xp,matrix box,t_pbc *pbc,
469 struct gmx_lincsdata *lincsd,int th,
472 gmx_bool bCalcLambda,
473 real wangle,int *warn,
475 gmx_bool bCalcVir,tensor rmdr)
477 int b0,b1,b,i,j,k,n,iter;
478 real tmp0,tmp1,tmp2,im1,im2,mvb,rlen,len,len2,dlen2,wfac;
480 int ncons,*bla,*blnr,*blbnb;
482 real *blc,*blmf,*bllen,*blcc,*rhs1,*rhs2,*sol,*blc_sol,*mlambda;
485 b0 = lincsd->th[th].b0;
486 b1 = lincsd->th[th].b1;
492 blbnb = lincsd->blbnb;
495 bllen = lincsd->bllen;
496 blcc = lincsd->tmpncc;
500 blc_sol= lincsd->tmp4;
501 mlambda= lincsd->mlambda;
503 if (DOMAINDECOMP(cr) && cr->dd->constraints)
505 nlocat = dd_constraints_nlocalatoms(cr->dd);
507 else if (PARTDECOMP(cr))
509 nlocat = pd_constraints_nlocalatoms(cr->pd);
520 /* Compute normalized i-j vectors */
523 pbc_dx_aiuc(pbc,x[bla[2*b]],x[bla[2*b+1]],dx);
529 for(n=blnr[b]; n<blnr[b+1]; n++)
531 blcc[n] = blmf[n]*iprod(r[b],r[blbnb[n]]);
533 pbc_dx_aiuc(pbc,xp[bla[2*b]],xp[bla[2*b+1]],dx);
534 mvb = blc[b]*(iprod(r[b],dx) - bllen[b]);
541 /* Compute normalized i-j vectors */
546 tmp0 = x[i][0] - x[j][0];
547 tmp1 = x[i][1] - x[j][1];
548 tmp2 = x[i][2] - x[j][2];
549 rlen = gmx_invsqrt(tmp0*tmp0+tmp1*tmp1+tmp2*tmp2);
553 } /* 16 ncons flops */
564 for(n=blnr[b]; n<blnr[b+1]; n++)
567 blcc[n] = blmf[n]*(tmp0*r[k][0] + tmp1*r[k][1] + tmp2*r[k][2]);
569 mvb = blc[b]*(tmp0*(xp[i][0] - xp[j][0]) +
570 tmp1*(xp[i][1] - xp[j][1]) +
571 tmp2*(xp[i][2] - xp[j][2]) - len);
576 /* Together: 26*ncons + 6*nrtot flops */
579 lincs_matrix_expand(lincsd,b0,b1,blcc,rhs1,rhs2,sol);
580 /* nrec*(ncons+2*nrtot) flops */
584 mlambda[b] = blc[b]*sol[b];
587 /* Update the coordinates */
588 lincs_update_atoms(lincsd,th,1.0,mlambda,r,invmass,xp);
591 ******** Correction for centripetal effects ********
594 wfac = cos(DEG2RAD*wangle);
597 for(iter=0; iter<lincsd->nIter; iter++)
599 if ((DOMAINDECOMP(cr) && cr->dd->constraints) ||
605 /* Communicate the corrected non-local coordinates */
606 if (DOMAINDECOMP(cr))
608 dd_move_x_constraints(cr->dd,box,xp,NULL);
612 pd_move_x_constraints(cr,xp,NULL);
623 pbc_dx_aiuc(pbc,xp[bla[2*b]],xp[bla[2*b+1]],dx);
627 rvec_sub(xp[bla[2*b]],xp[bla[2*b+1]],dx);
630 dlen2 = 2*len2 - norm2(dx);
631 if (dlen2 < wfac*len2 && (nlocat==NULL || nlocat[b]))
637 mvb = blc[b]*(len - dlen2*gmx_invsqrt(dlen2));
645 } /* 20*ncons flops */
647 lincs_matrix_expand(lincsd,b0,b1,blcc,rhs1,rhs2,sol);
648 /* nrec*(ncons+2*nrtot) flops */
657 /* Update the coordinates */
658 lincs_update_atoms(lincsd,th,1.0,blc_sol,r,invmass,xp);
660 /* nit*ncons*(37+9*nrec) flops */
664 /* Update the velocities */
665 lincs_update_atoms(lincsd,th,invdt,mlambda,r,invmass,v);
669 if (nlocat && bCalcLambda)
671 /* Only account for local atoms */
674 mlambda[b] *= 0.5*nlocat[b];
683 /* Constraint virial */
684 for(b=0; b<ncons; b++)
686 tmp0 = -bllen[b]*mlambda[b];
692 rmdr[i][j] -= tmp1*r[b][j];
695 } /* 22 ncons flops */
700 * 26*ncons + 6*nrtot + nrec*(ncons+2*nrtot)
701 * + nit * (20*ncons + nrec*(ncons+2*nrtot) + 17 ncons)
703 * (26+nrec)*ncons + (6+2*nrec)*nrtot
704 * + nit * ((37+nrec)*ncons + 2*nrec*nrtot)
706 * (63+nrec)*ncons + (6+4*nrec)*nrtot
710 void set_lincs_matrix(struct gmx_lincsdata *li,real *invmass,real lambda)
712 int i,a1,a2,n,k,sign,center;
714 const real invsqrt2=0.7071067811865475244;
716 for(i=0; (i<li->nc); i++)
720 li->blc[i] = gmx_invsqrt(invmass[a1] + invmass[a2]);
721 li->blc1[i] = invsqrt2;
724 /* Construct the coupling coefficient matrix blmf */
726 li->ncc_triangle = 0;
727 for(i=0; (i<li->nc); i++)
731 for(n=li->blnr[i]; (n<li->blnr[i+1]); n++)
734 if (a1 == li->bla[2*k] || a2 == li->bla[2*k+1])
742 if (a1 == li->bla[2*k] || a1 == li->bla[2*k+1])
752 li->blmf[n] = sign*invmass[center]*li->blc[i]*li->blc[k];
753 li->blmf1[n] = sign*0.5;
754 if (li->ncg_triangle > 0)
756 /* Look for constraint triangles */
757 for(nk=li->blnr[k]; (nk<li->blnr[k+1]); nk++)
760 if (kk != i && kk != k &&
761 (li->bla[2*kk] == end || li->bla[2*kk+1] == end))
763 if (li->ntriangle == 0 ||
764 li->triangle[li->ntriangle-1] < i)
766 /* Add this constraint to the triangle list */
767 li->triangle[li->ntriangle] = i;
768 li->tri_bits[li->ntriangle] = 0;
770 if (li->blnr[i+1] - li->blnr[i] > sizeof(li->tri_bits[0])*8 - 1)
772 gmx_fatal(FARGS,"A constraint is connected to %d constraints, this is more than the %d allowed for constraints participating in triangles",
773 li->blnr[i+1] - li->blnr[i],
774 sizeof(li->tri_bits[0])*8-1);
777 li->tri_bits[li->ntriangle-1] |= (1<<(n-li->blnr[i]));
787 fprintf(debug,"Of the %d constraints %d participate in triangles\n",
788 li->nc,li->ntriangle);
789 fprintf(debug,"There are %d couplings of which %d in triangles\n",
790 li->ncc,li->ncc_triangle);
794 * so we know with which lambda value the masses have been set.
799 static int count_triangle_constraints(t_ilist *ilist,t_blocka *at2con)
802 int c0,a00,a01,n1,c1,a10,a11,ac1,n2,c2,a20,a21;
805 t_iatom *ia1,*ia2,*iap;
807 ncon1 = ilist[F_CONSTR].nr/3;
808 ncon_tot = ncon1 + ilist[F_CONSTRNC].nr/3;
810 ia1 = ilist[F_CONSTR].iatoms;
811 ia2 = ilist[F_CONSTRNC].iatoms;
814 for(c0=0; c0<ncon_tot; c0++)
817 iap = constr_iatomptr(ncon1,ia1,ia2,c0);
820 for(n1=at2con->index[a01]; n1<at2con->index[a01+1]; n1++)
825 iap = constr_iatomptr(ncon1,ia1,ia2,c1);
836 for(n2=at2con->index[ac1]; n2<at2con->index[ac1+1]; n2++)
839 if (c2 != c0 && c2 != c1)
841 iap = constr_iatomptr(ncon1,ia1,ia2,c2);
844 if (a20 == a00 || a21 == a00)
858 return ncon_triangle;
861 static int int_comp(const void *a,const void *b)
863 return (*(int *)a) - (*(int *)b);
866 gmx_lincsdata_t init_lincs(FILE *fplog,gmx_mtop_t *mtop,
867 int nflexcon_global,t_blocka *at2con,
868 gmx_bool bPLINCS,int nIter,int nProjOrder)
870 struct gmx_lincsdata *li;
876 fprintf(fplog,"\nInitializing%s LINear Constraint Solver\n",
877 bPLINCS ? " Parallel" : "");
883 gmx_mtop_ftype_count(mtop,F_CONSTR) +
884 gmx_mtop_ftype_count(mtop,F_CONSTRNC);
885 li->ncg_flex = nflexcon_global;
887 li->ncg_triangle = 0;
888 for(mb=0; mb<mtop->nmolblock; mb++)
890 molt = &mtop->moltype[mtop->molblock[mb].type];
892 mtop->molblock[mb].nmol*
893 count_triangle_constraints(molt->ilist,
894 &at2con[mtop->molblock[mb].type]);
898 li->nOrder = nProjOrder;
900 /* LINCS can run on any number of threads.
901 * Currently the number is fixed for the whole simulation,
902 * but it could be set in set_lincs().
904 li->nth = gmx_omp_nthreads_get(emntLINCS);
911 /* Allocate an extra elements for "thread-overlap" constraints */
912 snew(li->th,li->nth+1);
916 fprintf(debug,"LINCS: using %d threads\n",li->nth);
919 if (bPLINCS || li->ncg_triangle > 0)
921 please_cite(fplog,"Hess2008a");
925 please_cite(fplog,"Hess97a");
930 fprintf(fplog,"The number of constraints is %d\n",li->ncg);
933 fprintf(fplog,"There are inter charge-group constraints,\n"
934 "will communicate selected coordinates each lincs iteration\n");
936 if (li->ncg_triangle > 0)
939 "%d constraints are involved in constraint triangles,\n"
940 "will apply an additional matrix expansion of order %d for couplings\n"
941 "between constraints inside triangles\n",
942 li->ncg_triangle,li->nOrder);
949 /* Sets up the work division over the threads */
950 static void lincs_thread_setup(struct gmx_lincsdata *li,int natoms)
952 lincs_thread_t *li_m;
957 if (natoms > li->atf_nalloc)
959 li->atf_nalloc = over_alloc_large(natoms);
960 srenew(li->atf,li->atf_nalloc);
964 /* Clear the atom flags */
965 for(a=0; a<natoms; a++)
970 for(th=0; th<li->nth; th++)
972 lincs_thread_t *li_th;
977 /* The constraints are divided equally over the threads */
978 li_th->b0 = (li->nc* th )/li->nth;
979 li_th->b1 = (li->nc*(th+1))/li->nth;
981 if (th < sizeof(*atf)*8)
983 /* For each atom set a flag for constraints from each */
984 for(b=li_th->b0; b<li_th->b1; b++)
986 atf[li->bla[b*2] ] |= (1U<<th);
987 atf[li->bla[b*2+1]] |= (1U<<th);
992 #pragma omp parallel for num_threads(li->nth) schedule(static)
993 for(th=0; th<li->nth; th++)
995 lincs_thread_t *li_th;
1001 if (li_th->b1 - li_th->b0 > li_th->ind_nalloc)
1003 li_th->ind_nalloc = over_alloc_large(li_th->b1-li_th->b0);
1004 srenew(li_th->ind,li_th->ind_nalloc);
1005 srenew(li_th->ind_r,li_th->ind_nalloc);
1008 if (th < sizeof(*atf)*8)
1010 mask = (1U<<th) - 1U;
1014 for(b=li_th->b0; b<li_th->b1; b++)
1016 /* We let the constraint with the lowest thread index
1017 * operate on atoms with constraints from multiple threads.
1019 if (((atf[li->bla[b*2]] & mask) == 0) &&
1020 ((atf[li->bla[b*2+1]] & mask) == 0))
1022 /* Add the constraint to the local atom update index */
1023 li_th->ind[li_th->nind++] = b;
1027 /* Add the constraint to the rest block */
1028 li_th->ind_r[li_th->nind_r++] = b;
1034 /* We are out of bits, assign all constraints to rest */
1035 for(b=li_th->b0; b<li_th->b1; b++)
1037 li_th->ind_r[li_th->nind_r++] = b;
1042 /* We need to copy all constraints which have not be assigned
1043 * to a thread to a separate list which will be handled by one thread.
1045 li_m = &li->th[li->nth];
1048 for(th=0; th<li->nth; th++)
1050 lincs_thread_t *li_th;
1053 li_th = &li->th[th];
1055 if (li_m->nind + li_th->nind_r > li_m->ind_nalloc)
1057 li_m->ind_nalloc = over_alloc_large(li_m->nind+li_th->nind_r);
1058 srenew(li_m->ind,li_m->ind_nalloc);
1061 for(b=0; b<li_th->nind_r; b++)
1063 li_m->ind[li_m->nind++] = li_th->ind_r[b];
1068 fprintf(debug,"LINCS thread %d: %d constraints\n",
1075 fprintf(debug,"LINCS thread r: %d constraints\n",
1081 void set_lincs(t_idef *idef,t_mdatoms *md,
1082 gmx_bool bDynamics,t_commrec *cr,
1083 struct gmx_lincsdata *li)
1085 int start,natoms,nflexcon;
1088 int i,k,ncc_alloc,ni,con,nconnect,concon;
1095 /* Zero the thread index ranges.
1096 * Otherwise without local constraints we could return with old ranges.
1098 for(i=0; i<li->nth; i++)
1106 li->th[li->nth].nind = 0;
1109 /* This is the local topology, so there are only F_CONSTR constraints */
1110 if (idef->il[F_CONSTR].nr == 0)
1112 /* There are no constraints,
1113 * we do not need to fill any data structures.
1120 fprintf(debug,"Building the LINCS connectivity\n");
1123 if (DOMAINDECOMP(cr))
1125 if (cr->dd->constraints)
1127 dd_get_constraint_range(cr->dd,&start,&natoms);
1131 natoms = cr->dd->nat_home;
1135 else if(PARTDECOMP(cr))
1137 pd_get_constraint_range(cr->pd,&start,&natoms);
1142 natoms = md->homenr;
1144 at2con = make_at2con(start,natoms,idef->il,idef->iparams,bDynamics,
1148 if (idef->il[F_CONSTR].nr/3 > li->nc_alloc || li->nc_alloc == 0)
1150 li->nc_alloc = over_alloc_dd(idef->il[F_CONSTR].nr/3);
1151 srenew(li->bllen0,li->nc_alloc);
1152 srenew(li->ddist,li->nc_alloc);
1153 srenew(li->bla,2*li->nc_alloc);
1154 srenew(li->blc,li->nc_alloc);
1155 srenew(li->blc1,li->nc_alloc);
1156 srenew(li->blnr,li->nc_alloc+1);
1157 srenew(li->bllen,li->nc_alloc);
1158 srenew(li->tmpv,li->nc_alloc);
1159 srenew(li->tmp1,li->nc_alloc);
1160 srenew(li->tmp2,li->nc_alloc);
1161 srenew(li->tmp3,li->nc_alloc);
1162 srenew(li->tmp4,li->nc_alloc);
1163 srenew(li->mlambda,li->nc_alloc);
1164 if (li->ncg_triangle > 0)
1166 /* This is allocating too much, but it is difficult to improve */
1167 srenew(li->triangle,li->nc_alloc);
1168 srenew(li->tri_bits,li->nc_alloc);
1172 iatom = idef->il[F_CONSTR].iatoms;
1174 ncc_alloc = li->ncc_alloc;
1177 ni = idef->il[F_CONSTR].nr/3;
1181 li->blnr[con] = nconnect;
1188 lenA = idef->iparams[type].constr.dA;
1189 lenB = idef->iparams[type].constr.dB;
1190 /* Skip the flexible constraints when not doing dynamics */
1191 if (bDynamics || lenA!=0 || lenB!=0)
1193 li->bllen0[con] = lenA;
1194 li->ddist[con] = lenB - lenA;
1195 /* Set the length to the topology A length */
1196 li->bllen[con] = li->bllen0[con];
1197 li->bla[2*con] = a1;
1198 li->bla[2*con+1] = a2;
1199 /* Construct the constraint connection matrix blbnb */
1200 for(k=at2con.index[a1-start]; k<at2con.index[a1-start+1]; k++)
1202 concon = at2con.a[k];
1205 if (nconnect >= ncc_alloc)
1207 ncc_alloc = over_alloc_small(nconnect+1);
1208 srenew(li->blbnb,ncc_alloc);
1210 li->blbnb[nconnect++] = concon;
1213 for(k=at2con.index[a2-start]; k<at2con.index[a2-start+1]; k++)
1215 concon = at2con.a[k];
1218 if (nconnect+1 > ncc_alloc)
1220 ncc_alloc = over_alloc_small(nconnect+1);
1221 srenew(li->blbnb,ncc_alloc);
1223 li->blbnb[nconnect++] = concon;
1226 li->blnr[con+1] = nconnect;
1230 /* Order the blbnb matrix to optimize memory access */
1231 qsort(&(li->blbnb[li->blnr[con]]),li->blnr[con+1]-li->blnr[con],
1232 sizeof(li->blbnb[0]),int_comp);
1234 /* Increase the constraint count */
1239 done_blocka(&at2con);
1241 /* This is the real number of constraints,
1242 * without dynamics the flexible constraints are not present.
1246 li->ncc = li->blnr[con];
1249 /* Since the matrix is static, we can free some memory */
1250 ncc_alloc = li->ncc;
1251 srenew(li->blbnb,ncc_alloc);
1254 if (ncc_alloc > li->ncc_alloc)
1256 li->ncc_alloc = ncc_alloc;
1257 srenew(li->blmf,li->ncc_alloc);
1258 srenew(li->blmf1,li->ncc_alloc);
1259 srenew(li->tmpncc,li->ncc_alloc);
1264 fprintf(debug,"Number of constraints is %d, couplings %d\n",
1271 li->th[0].b1 = li->nc;
1275 lincs_thread_setup(li,md->nr);
1278 set_lincs_matrix(li,md->invmass,md->lambda);
1281 static void lincs_warning(FILE *fplog,
1282 gmx_domdec_t *dd,rvec *x,rvec *xprime,t_pbc *pbc,
1283 int ncons,int *bla,real *bllen,real wangle,
1284 int maxwarn,int *warncount)
1288 real wfac,d0,d1,cosine;
1291 wfac=cos(DEG2RAD*wangle);
1293 sprintf(buf,"bonds that rotated more than %g degrees:\n"
1294 " atom 1 atom 2 angle previous, current, constraint length\n",
1296 fprintf(stderr,"%s",buf);
1299 fprintf(fplog,"%s",buf);
1302 for(b=0;b<ncons;b++)
1308 pbc_dx_aiuc(pbc,x[i],x[j],v0);
1309 pbc_dx_aiuc(pbc,xprime[i],xprime[j],v1);
1313 rvec_sub(x[i],x[j],v0);
1314 rvec_sub(xprime[i],xprime[j],v1);
1318 cosine = iprod(v0,v1)/(d0*d1);
1321 sprintf(buf," %6d %6d %5.1f %8.4f %8.4f %8.4f\n",
1322 ddglatnr(dd,i),ddglatnr(dd,j),
1323 RAD2DEG*acos(cosine),d0,d1,bllen[b]);
1324 fprintf(stderr,"%s",buf);
1327 fprintf(fplog,"%s",buf);
1329 if (!gmx_isfinite(d1))
1331 gmx_fatal(FARGS,"Bond length not finite.");
1337 if (*warncount > maxwarn)
1339 too_many_constraint_warnings(econtLINCS,*warncount);
1343 static void cconerr(gmx_domdec_t *dd,
1344 int ncons,int *bla,real *bllen,rvec *x,t_pbc *pbc,
1345 real *ncons_loc,real *ssd,real *max,int *imax)
1347 real len,d,ma,ssd2,r2;
1348 int *nlocat,count,b,im;
1351 if (dd && dd->constraints)
1353 nlocat = dd_constraints_nlocalatoms(dd);
1364 for(b=0;b<ncons;b++)
1368 pbc_dx_aiuc(pbc,x[bla[2*b]],x[bla[2*b+1]],dx);
1371 rvec_sub(x[bla[2*b]],x[bla[2*b+1]],dx);
1374 len = r2*gmx_invsqrt(r2);
1375 d = fabs(len/bllen[b]-1);
1376 if (d > ma && (nlocat==NULL || nlocat[b]))
1388 ssd2 += nlocat[b]*d*d;
1393 *ncons_loc = (nlocat ? 0.5 : 1)*count;
1394 *ssd = (nlocat ? 0.5 : 1)*ssd2;
1399 static void dump_conf(gmx_domdec_t *dd,struct gmx_lincsdata *li,
1401 char *name,gmx_bool bAll,rvec *x,matrix box)
1407 dd_get_constraint_range(dd,&ac0,&ac1);
1409 sprintf(str,"%s_%d_%d_%d.pdb",name,dd->ci[XX],dd->ci[YY],dd->ci[ZZ]);
1410 fp = gmx_fio_fopen(str,"w");
1411 fprintf(fp,"CRYST1%9.3f%9.3f%9.3f%7.2f%7.2f%7.2f P 1 1\n",
1412 10*norm(box[XX]),10*norm(box[YY]),10*norm(box[ZZ]),
1414 for(i=0; i<ac1; i++)
1416 if (i < dd->nat_home || (bAll && i >= ac0 && i < ac1))
1418 fprintf(fp,"%-6s%5u %-4.4s%3.3s %c%4d %8.3f%8.3f%8.3f%6.2f%6.2f\n",
1419 "ATOM",ddglatnr(dd,i),"C","ALA",' ',i+1,
1420 10*x[i][XX],10*x[i][YY],10*x[i][ZZ],
1421 1.0,i<dd->nat_tot ? 0.0 : 1.0);
1426 for(i=0; i<li->nc; i++)
1428 fprintf(fp,"CONECT%5d%5d\n",
1429 ddglatnr(dd,li->bla[2*i]),
1430 ddglatnr(dd,li->bla[2*i+1]));
1436 gmx_bool constrain_lincs(FILE *fplog,gmx_bool bLog,gmx_bool bEner,
1438 gmx_large_int_t step,
1439 struct gmx_lincsdata *lincsd,t_mdatoms *md,
1441 rvec *x,rvec *xprime,rvec *min_proj,
1442 matrix box,t_pbc *pbc,
1443 real lambda,real *dvdlambda,
1445 gmx_bool bCalcVir,tensor rmdr,
1448 int maxwarn,int *warncount)
1450 char buf[STRLEN],buf2[22],buf3[STRLEN];
1451 int i,warn=0,p_imax,error;
1452 real ncons_loc,p_ssd,p_max=0;
1458 if (lincsd->nc == 0 && cr->dd == NULL)
1462 lincsd->rmsd_data[0] = 0;
1463 if (ir->eI == eiSD2 && v == NULL)
1471 lincsd->rmsd_data[i] = 0;
1477 if (econq == econqCoord)
1479 if (ir->efep != efepNO)
1481 if (md->nMassPerturbed && lincsd->matlam != md->lambda)
1483 set_lincs_matrix(lincsd,md->invmass,md->lambda);
1486 for(i=0; i<lincsd->nc; i++)
1488 lincsd->bllen[i] = lincsd->bllen0[i] + lambda*lincsd->ddist[i];
1492 if (lincsd->ncg_flex)
1494 /* Set the flexible constraint lengths to the old lengths */
1497 for(i=0; i<lincsd->nc; i++)
1499 if (lincsd->bllen[i] == 0) {
1500 pbc_dx_aiuc(pbc,x[lincsd->bla[2*i]],x[lincsd->bla[2*i+1]],dx);
1501 lincsd->bllen[i] = norm(dx);
1507 for(i=0; i<lincsd->nc; i++)
1509 if (lincsd->bllen[i] == 0)
1512 sqrt(distance2(x[lincsd->bla[2*i]],
1513 x[lincsd->bla[2*i+1]]));
1521 cconerr(cr->dd,lincsd->nc,lincsd->bla,lincsd->bllen,xprime,pbc,
1522 &ncons_loc,&p_ssd,&p_max,&p_imax);
1525 /* The (only) OpenMP parallel region of constrain_lincs */
1526 #pragma omp parallel num_threads(lincsd->nth)
1528 int th=gmx_omp_get_thread_num();
1529 do_lincs(x,xprime,box,pbc,lincsd,th,
1531 bCalcVir || (ir->efep != efepNO),
1532 ir->LincsWarnAngle,&warn,
1533 invdt,v,bCalcVir,rmdr);
1536 if (ir->efep != efepNO)
1540 dt_2 = 1.0/(ir->delta_t*ir->delta_t);
1541 for(i=0; (i<lincsd->nc); i++)
1543 dvdl -= lincsd->mlambda[i]*dt_2*lincsd->ddist[i];
1548 if (bLog && fplog && lincsd->nc > 0)
1550 fprintf(fplog," Rel. Constraint Deviation: RMS MAX between atoms\n");
1551 fprintf(fplog," Before LINCS %.6f %.6f %6d %6d\n",
1552 sqrt(p_ssd/ncons_loc),p_max,
1553 ddglatnr(cr->dd,lincsd->bla[2*p_imax]),
1554 ddglatnr(cr->dd,lincsd->bla[2*p_imax+1]));
1558 cconerr(cr->dd,lincsd->nc,lincsd->bla,lincsd->bllen,xprime,pbc,
1559 &ncons_loc,&p_ssd,&p_max,&p_imax);
1560 /* Check if we are doing the second part of SD */
1561 if (ir->eI == eiSD2 && v == NULL)
1569 lincsd->rmsd_data[0] = ncons_loc;
1570 lincsd->rmsd_data[i] = p_ssd;
1574 lincsd->rmsd_data[0] = 0;
1575 lincsd->rmsd_data[1] = 0;
1576 lincsd->rmsd_data[2] = 0;
1578 if (bLog && fplog && lincsd->nc > 0)
1581 " After LINCS %.6f %.6f %6d %6d\n\n",
1582 sqrt(p_ssd/ncons_loc),p_max,
1583 ddglatnr(cr->dd,lincsd->bla[2*p_imax]),
1584 ddglatnr(cr->dd,lincsd->bla[2*p_imax+1]));
1591 cconerr(cr->dd,lincsd->nc,lincsd->bla,lincsd->bllen,xprime,pbc,
1592 &ncons_loc,&p_ssd,&p_max,&p_imax);
1595 sprintf(buf3," in simulation %d", cr->ms->sim);
1601 sprintf(buf,"\nStep %s, time %g (ps) LINCS WARNING%s\n"
1602 "relative constraint deviation after LINCS:\n"
1603 "rms %.6f, max %.6f (between atoms %d and %d)\n",
1604 gmx_step_str(step,buf2),ir->init_t+step*ir->delta_t,
1606 sqrt(p_ssd/ncons_loc),p_max,
1607 ddglatnr(cr->dd,lincsd->bla[2*p_imax]),
1608 ddglatnr(cr->dd,lincsd->bla[2*p_imax+1]));
1611 fprintf(fplog,"%s",buf);
1613 fprintf(stderr,"%s",buf);
1614 lincs_warning(fplog,cr->dd,x,xprime,pbc,
1615 lincsd->nc,lincsd->bla,lincsd->bllen,
1616 ir->LincsWarnAngle,maxwarn,warncount);
1618 bOK = (p_max < 0.5);
1621 if (lincsd->ncg_flex) {
1622 for(i=0; (i<lincsd->nc); i++)
1623 if (lincsd->bllen0[i] == 0 && lincsd->ddist[i] == 0)
1624 lincsd->bllen[i] = 0;
1629 do_lincsp(x,xprime,min_proj,pbc,lincsd,md->invmass,econq,dvdlambda,
1633 /* count assuming nit=1 */
1634 inc_nrnb(nrnb,eNR_LINCS,lincsd->nc);
1635 inc_nrnb(nrnb,eNR_LINCSMAT,(2+lincsd->nOrder)*lincsd->ncc);
1636 if (lincsd->ntriangle > 0)
1638 inc_nrnb(nrnb,eNR_LINCSMAT,lincsd->nOrder*lincsd->ncc_triangle);
1642 inc_nrnb(nrnb,eNR_CONSTR_V,lincsd->nc*2);
1646 inc_nrnb(nrnb,eNR_CONSTR_VIR,lincsd->nc);