3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * GROningen Mixture of Alchemy and Childrens' Stories
41 #include "gmx_fatal.h"
50 /************************************************************
52 * S H I F T U T I L I T I E S
54 ************************************************************/
57 /************************************************************
59 * G R A P H G E N E R A T I O N C O D E
61 ************************************************************/
63 static void add_gbond(t_graph *g,atom_id a0,atom_id a1)
69 inda0 = a0 - g->start;
70 inda1 = a1 - g->start;
72 /* Search for a direct edge between a0 and a1.
73 * All egdes are bidirectional, so we only need to search one way.
75 for(i=0; (i<g->nedge[inda0] && !bFound); i++) {
76 bFound = (g->edge[inda0][i] == a1);
80 g->edge[inda0][g->nedge[inda0]++] = a1;
81 g->edge[inda1][g->nedge[inda1]++] = a0;
85 static void mk_igraph(t_graph *g,int ftype,t_ilist *il,
86 int at_start,int at_end,
98 np = interaction_function[ftype].nratoms;
100 if (ia[1] >= at_start && ia[1] < at_end) {
101 if (ia[np] >= at_end)
103 "Molecule in topology has atom numbers below and "
104 "above natoms (%d).\n"
105 "You are probably trying to use a trajectory which does "
106 "not match the first %d atoms of the run input file.\n"
107 "You can make a matching run input file with tpbconv.",
109 if (ftype == F_SETTLE) {
110 /* Bond all the atoms in the settle */
111 add_gbond(g,ia[1],ia[1]+1);
112 add_gbond(g,ia[1],ia[1]+2);
113 } else if (part == NULL) {
114 /* Simply add this bond */
115 for(j=1; j<np; j++) {
116 add_gbond(g,ia[j],ia[j+1]);
119 /* Add this bond when it connects two unlinked parts of the graph */
120 for(j=1; j<np; j++) {
121 if (part[ia[j]] != part[ia[j+1]]) {
122 add_gbond(g,ia[j],ia[j+1]);
132 static void g_error(int line,const char *file)
134 gmx_fatal(FARGS,"Tring to print non existant graph (file %s, line %d)",
137 #define GCHECK(g) if (g == NULL) g_error(__LINE__,__FILE__)
139 void p_graph(FILE *log,const char *title,t_graph *g)
142 const char *cc[egcolNR] = { "W", "G", "B" };
145 fprintf(log,"graph: %s\n",title);
146 fprintf(log,"nnodes: %d\n",g->nnodes);
147 fprintf(log,"nbound: %d\n",g->nbound);
148 fprintf(log,"start: %d\n",g->start);
149 fprintf(log,"end: %d\n",g->end);
150 fprintf(log," atom shiftx shifty shiftz C nedg e1 e2 etc.\n");
151 for(i=0; (i<g->nnodes); i++)
152 if (g->nedge[i] > 0) {
153 fprintf(log,"%5d%7d%7d%7d %1s%5d",g->start+i+1,
154 g->ishift[i][XX],g->ishift[i][YY],
156 (g->negc > 0) ? cc[g->egc[i]] : " ",
158 for(j=0; (j<g->nedge[i]); j++)
159 fprintf(log," %5u",g->edge[i][j]+1);
165 static void calc_1se(t_graph *g,int ftype,t_ilist *il,
166 int nbond[],int at_start,int at_end)
174 for(j=0; (j<end); j+=nratoms+1,ia+=nratoms+1) {
175 nratoms = interaction_function[ftype].nratoms;
177 if (ftype == F_SETTLE) {
179 if (iaa >= at_start && iaa < at_end) {
183 g->start = min(g->start,iaa);
184 g->end = max(g->end,iaa+2);
187 for(k=1; (k<=nratoms); k++) {
189 if (iaa >= at_start && iaa < at_end) {
190 g->start=min(g->start,iaa);
191 g->end =max(g->end, iaa);
192 /* When making the graph we (might) link all atoms in an interaction
193 * sequentially. Therefore the end atoms add 1 to the count,
194 * the middle atoms 2.
196 if (k == 1 || k == nratoms) {
207 static int calc_start_end(FILE *fplog,t_graph *g,t_ilist il[],
208 int at_start,int at_end,
216 /* First add all the real bonds: they should determine the molecular
219 for(i=0; (i<F_NRE); i++)
220 if (interaction_function[i].flags & IF_CHEMBOND)
221 calc_1se(g,i,&il[i],nbond,at_start,at_end);
222 /* Then add all the other interactions in fixed lists, but first
223 * check to see what's there already.
225 for(i=0; (i<F_NRE); i++) {
226 if (!(interaction_function[i].flags & IF_CHEMBOND)) {
227 calc_1se(g,i,&il[i],nbond,at_start,at_end);
233 for(i=g->start; (i<=g->end); i++) {
235 nnb = max(nnb,nbond[i]);
238 fprintf(fplog,"Max number of connections per atom is %d\n",nnb);
239 fprintf(fplog,"Total number of connections is %d\n",nbtot);
246 static void compact_graph(FILE *fplog,t_graph *g)
253 for(i=0; i<g->nnodes; i++) {
254 for(j=0; j<g->nedge[i]; j++) {
255 g->edge[0][n++] = g->edge[i][j];
257 max_nedge = max(max_nedge,g->nedge[i]);
259 srenew(g->edge[0],n);
260 /* set pointers after srenew because edge[0] might move */
261 for(i=1; i<g->nnodes; i++) {
262 g->edge[i] = g->edge[i-1] + g->nedge[i-1];
266 fprintf(fplog,"Max number of graph edges per atom is %d\n",
268 fprintf(fplog,"Total number of graph edges is %d\n",n);
272 static gmx_bool determine_graph_parts(t_graph *g,int *part)
279 /* Initialize the part array with all entries different */
280 for(at_i=g->start; at_i<g->end; at_i++) {
284 /* Loop over the graph until the part array is fixed */
288 for(i=0; (i<g->nnodes); i++) {
291 for(e=0; e<g->nedge[i]; e++) {
292 /* Set part for both nodes to the minimum */
293 if (part[at_i2[e]] > part[at_i]) {
294 part[at_i2[e]] = part[at_i];
296 } else if (part[at_i2[e]] < part[at_i]) {
297 part[at_i] = part[at_i2[e]];
301 if (part[at_i] != part[g->start]) {
306 fprintf(debug,"graph part[] nchanged=%d, bMultiPart=%d\n",
307 nchanged,bMultiPart);
309 } while (nchanged > 0);
314 void mk_graph_ilist(FILE *fplog,
315 t_ilist *ilist,int at_start,int at_end,
316 gmx_bool bShakeOnly,gmx_bool bSettle,
324 nbtot = calc_start_end(fplog,g,ilist,at_start,at_end,nbond);
326 if (g->start >= g->end) {
331 g->nnodes = g->end - g->start + 1;
332 snew(g->ishift,g->nnodes);
333 snew(g->nedge,g->nnodes);
334 snew(g->edge,g->nnodes);
335 /* Allocate a single array and set pointers into it */
336 snew(g->edge[0],nbtot);
337 for(i=1; (i<g->nnodes); i++) {
338 g->edge[i] = g->edge[i-1] + nbond[g->start+i-1];
342 /* First add all the real bonds: they should determine the molecular
345 for(i=0; (i<F_NRE); i++)
346 if (interaction_function[i].flags & IF_CHEMBOND)
347 mk_igraph(g,i,&(ilist[i]),at_start,at_end,NULL);
349 /* Determine of which separated parts the IF_CHEMBOND graph consists.
350 * Store the parts in the nbond array.
352 bMultiPart = determine_graph_parts(g,nbond);
355 /* Then add all the other interactions in fixed lists,
356 * but only when they connect parts of the graph
357 * that are not connected through IF_CHEMBOND interactions.
359 for(i=0; (i<F_NRE); i++) {
360 if (!(interaction_function[i].flags & IF_CHEMBOND)) {
361 mk_igraph(g,i,&(ilist[i]),at_start,at_end,nbond);
366 /* Removed all the unused space from the edge array */
367 compact_graph(fplog,g);
370 /* This is a special thing used in splitter.c to generate shake-blocks */
371 mk_igraph(g,F_CONSTR,&(ilist[F_CONSTR]),at_start,at_end,NULL);
373 mk_igraph(g,F_SETTLE,&(ilist[F_SETTLE]),at_start,at_end,NULL);
376 for(i=0; (i<g->nnodes); i++)
387 p_graph(debug,"graph",g);
390 t_graph *mk_graph(FILE *fplog,
391 t_idef *idef,int at_start,int at_end,
392 gmx_bool bShakeOnly,gmx_bool bSettle)
398 mk_graph_ilist(fplog,idef->il,at_start,at_end,bShakeOnly,bSettle,g);
403 void done_graph(t_graph *g)
417 /************************************************************
419 * S H I F T C A L C U L A T I O N C O D E
421 ************************************************************/
423 static void mk_1shift_tric(int npbcdim,matrix box,rvec hbox,
424 rvec xi,rvec xj,int *mi,int *mj)
426 /* Calculate periodicity for triclinic box... */
433 for(m=npbcdim-1; (m>=0); m--) {
434 /* If dx < hbox, then xj will be reduced by box, so that
435 * xi - xj will be bigger
437 if (dx[m] < -hbox[m]) {
439 for(d=m-1; d>=0; d--)
441 } else if (dx[m] >= hbox[m]) {
443 for(d=m-1; d>=0; d--)
450 static void mk_1shift(int npbcdim,rvec hbox,rvec xi,rvec xj,int *mi,int *mj)
452 /* Calculate periodicity for rectangular box... */
459 for(m=0; (m<npbcdim); m++) {
460 /* If dx < hbox, then xj will be reduced by box, so that
461 * xi - xj will be bigger
463 if (dx[m] < -hbox[m])
465 else if (dx[m] >= hbox[m])
472 static void mk_1shift_screw(matrix box,rvec hbox,
473 rvec xi,rvec xj,int *mi,int *mj)
475 /* Calculate periodicity for rectangular box... */
479 if ((mi[XX] > 0 && mi[XX] % 2 == 1) ||
480 (mi[XX] < 0 && -mi[XX] % 2 == 1)) {
488 if (dx[XX] < -hbox[XX])
490 else if (dx[XX] >= hbox[XX])
494 if (mj[XX] != mi[XX]) {
496 dx[YY] = xi[YY] - (box[YY][YY] + box[ZZ][YY] - xj[YY]);
497 dx[ZZ] = xi[ZZ] - (box[ZZ][ZZ] - xj[ZZ]);
499 for(m=1; (m<DIM); m++) {
500 /* The signs are taken such that we can first shift x and rotate
501 * and then shift y and z.
503 if (dx[m] < -hbox[m])
504 mj[m] = mi[m] - signi;
505 else if (dx[m] >= hbox[m])
506 mj[m] = mi[m] + signi;
512 static int mk_grey(FILE *log,int nnodes,egCol egc[],t_graph *g,int *AtomI,
513 int npbcdim,matrix box,rvec x[],int *nerror)
521 for(m=0; (m<DIM); m++)
522 hbox[m]=box[m][m]*0.5;
523 bTriclinic = TRICLINIC(box);
529 /* Loop over all the bonds */
530 for(j=0; (j<g->nedge[ai]); j++) {
531 aj=g->edge[ai][j]-g0;
532 /* If there is a white one, make it grey and set pbc */
534 mk_1shift_screw(box,hbox,x[g0+ai],x[g0+aj],g->ishift[ai],is_aj);
536 mk_1shift_tric(npbcdim,box,hbox,x[g0+ai],x[g0+aj],g->ishift[ai],is_aj);
538 mk_1shift(npbcdim,hbox,x[g0+ai],x[g0+aj],g->ishift[ai],is_aj);
540 if (egc[aj] == egcolWhite) {
545 copy_ivec(is_aj,g->ishift[aj]);
549 else if ((is_aj[XX] != g->ishift[aj][XX]) ||
550 (is_aj[YY] != g->ishift[aj][YY]) ||
551 (is_aj[ZZ] != g->ishift[aj][ZZ])) {
553 set_pbc(&pbc,-1,box);
554 pbc_dx(&pbc,x[g0+ai],x[g0+aj],dx);
555 fprintf(debug,"mk_grey: shifts for atom %d due to atom %d\n"
556 "are (%d,%d,%d), should be (%d,%d,%d)\n"
558 aj+g0+1,ai+g0+1,is_aj[XX],is_aj[YY],is_aj[ZZ],
559 g->ishift[aj][XX],g->ishift[aj][YY],g->ishift[aj][ZZ],
560 dx[XX],dx[YY],dx[ZZ]);
568 static int first_colour(int fC,egCol Col,t_graph *g,egCol egc[])
569 /* Return the first node with colour Col starting at fC.
570 * return -1 if none found.
575 for(i=fC; (i<g->nnodes); i++)
576 if ((g->nedge[i] > 0) && (egc[i]==Col))
582 void mk_mshift(FILE *log,t_graph *g,int ePBC,matrix box,rvec x[])
584 static int nerror_tot = 0;
587 int nW,nG,nB; /* Number of Grey, Black, White */
588 int fW,fG; /* First of each category */
591 g->bScrewPBC = (ePBC == epbcSCREW);
599 /* This puts everything in the central box, that is does not move it
600 * at all. If we return without doing this for a system without bonds
601 * (i.e. only settles) all water molecules are moved to the opposite octant
603 for(i=0; (i<g->nnodes); i++) {
604 g->ishift[i][XX]=g->ishift[i][YY]=g->ishift[i][ZZ]=0;
611 if (nnodes > g->negc) {
613 srenew(g->egc,g->negc);
615 memset(g->egc,0,(size_t)(nnodes*sizeof(g->egc[0])));
623 /* We even have a loop invariant:
624 * nW+nG+nB == g->nbound
627 fprintf(log,"Starting W loop\n");
630 /* Find the first white, this will allways be a larger
631 * number than before, because no nodes are made white
634 if ((fW=first_colour(fW,egcolWhite,g,g->egc)) == -1)
635 gmx_fatal(FARGS,"No WHITE nodes found while nW=%d\n",nW);
637 /* Make the first white node grey */
638 g->egc[fW]=egcolGrey;
642 /* Initial value for the first grey */
645 fprintf(log,"Starting G loop (nW=%d, nG=%d, nB=%d, total %d)\n",
649 if ((fG=first_colour(fG,egcolGrey,g,g->egc)) == -1)
650 gmx_fatal(FARGS,"No GREY nodes found while nG=%d\n",nG);
652 /* Make the first grey node black */
653 g->egc[fG]=egcolBlack;
657 /* Make all the neighbours of this black node grey
658 * and set their periodicity
660 ng=mk_grey(log,nnodes,g->egc,g,&fG,npbcdim,box,x,&nerror);
661 /* ng is the number of white nodes made grey */
668 if (nerror_tot <= 100) {
669 fprintf(stderr,"There were %d inconsistent shifts. Check your topology\n",
672 fprintf(log,"There were %d inconsistent shifts. Check your topology\n",
676 if (nerror_tot == 100) {
677 fprintf(stderr,"Will stop reporting inconsistent shifts\n");
679 fprintf(log,"Will stop reporting inconsistent shifts\n");
685 /************************************************************
687 * A C T U A L S H I F T C O D E
689 ************************************************************/
691 void shift_x(t_graph *g,matrix box,rvec x[],rvec x_s[])
703 for(i=0,j=g0; (i<gn); i++,j++) {
708 if ((tx > 0 && tx % 2 == 1) ||
709 (tx < 0 && -tx %2 == 1)) {
710 x_s[j][XX] = x[j][XX] + tx*box[XX][XX];
711 x_s[j][YY] = box[YY][YY] + box[ZZ][YY] - x[j][YY];
712 x_s[j][ZZ] = box[ZZ][ZZ] - x[j][ZZ];
714 x_s[j][XX] = x[j][XX];
716 x_s[j][YY] = x[j][YY] + ty*box[YY][YY] + tz*box[ZZ][YY];
717 x_s[j][ZZ] = x[j][ZZ] + tz*box[ZZ][ZZ];
719 } else if (TRICLINIC(box)) {
720 for(i=0,j=g0; (i<gn); i++,j++) {
725 x_s[j][XX]=x[j][XX]+tx*box[XX][XX]+ty*box[YY][XX]+tz*box[ZZ][XX];
726 x_s[j][YY]=x[j][YY]+ty*box[YY][YY]+tz*box[ZZ][YY];
727 x_s[j][ZZ]=x[j][ZZ]+tz*box[ZZ][ZZ];
730 for(i=0,j=g0; (i<gn); i++,j++) {
735 x_s[j][XX]=x[j][XX]+tx*box[XX][XX];
736 x_s[j][YY]=x[j][YY]+ty*box[YY][YY];
737 x_s[j][ZZ]=x[j][ZZ]+tz*box[ZZ][ZZ];
743 void shift_self(t_graph *g,matrix box,rvec x[])
750 gmx_incons("screw pbc not implemented for shift_self");
757 fprintf(stderr,"Shifting atoms %d to %d\n",g0,g0+gn);
760 for(i=0,j=g0; (i<gn); i++,j++) {
765 x[j][XX]=x[j][XX]+tx*box[XX][XX]+ty*box[YY][XX]+tz*box[ZZ][XX];
766 x[j][YY]=x[j][YY]+ty*box[YY][YY]+tz*box[ZZ][YY];
767 x[j][ZZ]=x[j][ZZ]+tz*box[ZZ][ZZ];
770 for(i=0,j=g0; (i<gn); i++,j++) {
775 x[j][XX]=x[j][XX]+tx*box[XX][XX];
776 x[j][YY]=x[j][YY]+ty*box[YY][YY];
777 x[j][ZZ]=x[j][ZZ]+tz*box[ZZ][ZZ];
783 void unshift_x(t_graph *g,matrix box,rvec x[],rvec x_s[])
790 gmx_incons("screw pbc not implemented for unshift_x");
796 for(i=0,j=g0; (i<gn); i++,j++) {
801 x[j][XX]=x_s[j][XX]-tx*box[XX][XX]-ty*box[YY][XX]-tz*box[ZZ][XX];
802 x[j][YY]=x_s[j][YY]-ty*box[YY][YY]-tz*box[ZZ][YY];
803 x[j][ZZ]=x_s[j][ZZ]-tz*box[ZZ][ZZ];
806 for(i=0,j=g0; (i<gn); i++,j++) {
811 x[j][XX]=x_s[j][XX]-tx*box[XX][XX];
812 x[j][YY]=x_s[j][YY]-ty*box[YY][YY];
813 x[j][ZZ]=x_s[j][ZZ]-tz*box[ZZ][ZZ];
818 void unshift_self(t_graph *g,matrix box,rvec x[])
825 gmx_incons("screw pbc not implemented for unshift_self");
831 for(i=0,j=g0; (i<gn); i++,j++) {
836 x[j][XX]=x[j][XX]-tx*box[XX][XX]-ty*box[YY][XX]-tz*box[ZZ][XX];
837 x[j][YY]=x[j][YY]-ty*box[YY][YY]-tz*box[ZZ][YY];
838 x[j][ZZ]=x[j][ZZ]-tz*box[ZZ][ZZ];
841 for(i=0,j=g0; (i<gn); i++,j++) {
846 x[j][XX]=x[j][XX]-tx*box[XX][XX];
847 x[j][YY]=x[j][YY]-ty*box[YY][YY];
848 x[j][ZZ]=x[j][ZZ]-tz*box[ZZ][ZZ];