Another quick bug fix.
[gromacs/rigid-bodies.git] / src / mdlib / constr.c
blob54863c57d62b29d8ce45ddf9e832c710b810b44d
1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 3.2.0
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
33 * And Hey:
34 * GROwing Monsters And Cloning Shrimps
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
40 #include "confio.h"
41 #include "constr.h"
42 #include "copyrite.h"
43 #include "invblock.h"
44 #include "main.h"
45 #include "mdrun.h"
46 #include "nrnb.h"
47 #include "smalloc.h"
48 #include "vec.h"
49 #include "physics.h"
50 #include "names.h"
51 #include "txtdump.h"
52 #include "domdec.h"
53 #include "pdbio.h"
54 #include "partdec.h"
55 #include "splitter.h"
56 #include "mtop_util.h"
57 #include "gmxfio.h"
59 typedef struct gmx_constr {
60 int ncon_tot; /* The total number of constraints */
61 int nflexcon; /* The number of flexible constraints */
62 int n_at2con_mt; /* The size of at2con = #moltypes */
63 t_blocka *at2con_mt; /* A list of atoms to constraints */
64 gmx_lincsdata_t lincsd; /* LINCS data */
65 gmx_shakedata_t shaked; /* SHAKE data */
66 gmx_settledata_t settled; /* SETTLE data */
67 int nblocks; /* The number of SHAKE blocks */
68 int *sblock; /* The SHAKE blocks */
69 int sblock_nalloc;/* The allocation size of sblock */
70 real *lagr; /* Lagrange multipliers for SHAKE */
71 int lagr_nalloc; /* The allocation size of lagr */
72 int maxwarn; /* The maximum number of warnings */
73 int warncount_lincs;
74 int warncount_settle;
75 gmx_edsam_t ed; /* The essential dynamics data */
77 gmx_mtop_t *warn_mtop; /* Only used for printing warnings */
78 } t_gmx_constr;
80 typedef struct {
81 atom_id iatom[3];
82 atom_id blocknr;
83 } t_sortblock;
85 t_vetavars *init_vetavars(real veta,real vetanew, t_inputrec *ir)
87 t_vetavars *vars;
88 double g;
89 int i;
91 snew(vars,1);
92 snew(vars->vscale_nhc,ir->opts.ngtc);
93 /* first, set the alpha integrator variable */
94 if (ir->opts.nrdf[0] > 0)
96 vars->alpha = 1.0 + DIM/((double)ir->opts.nrdf[0]);
97 } else {
98 vars->alpha = 1.0;
100 g = 0.5*veta*ir->delta_t;
101 vars->rscale = exp(g)*series_sinhx(g);
102 g = -0.25*vars->alpha*veta*ir->delta_t;
103 vars->vscale = exp(g)*series_sinhx(g);
104 vars->rvscale = vars->vscale*vars->rscale;
105 vars->veta = vetanew;
106 for (i=0;i<ir->opts.ngtc;i++)
108 vars->vscale_nhc[i] = ir->opts.vscale_nhc[i];
111 return vars;
114 void free_vetavars(t_vetavars *vars)
116 sfree(vars->vscale_nhc);
117 sfree(vars);
120 static int pcomp(const void *p1, const void *p2)
122 int db;
123 atom_id min1,min2,max1,max2;
124 t_sortblock *a1=(t_sortblock *)p1;
125 t_sortblock *a2=(t_sortblock *)p2;
127 db=a1->blocknr-a2->blocknr;
129 if (db != 0)
130 return db;
132 min1=min(a1->iatom[1],a1->iatom[2]);
133 max1=max(a1->iatom[1],a1->iatom[2]);
134 min2=min(a2->iatom[1],a2->iatom[2]);
135 max2=max(a2->iatom[1],a2->iatom[2]);
137 if (min1 == min2)
138 return max1-max2;
139 else
140 return min1-min2;
143 static int icomp(const void *p1, const void *p2)
145 atom_id *a1=(atom_id *)p1;
146 atom_id *a2=(atom_id *)p2;
148 return (*a1)-(*a2);
151 int n_flexible_constraints(struct gmx_constr *constr)
153 int nflexcon;
155 if (constr)
156 nflexcon = constr->nflexcon;
157 else
158 nflexcon = 0;
160 return nflexcon;
163 void too_many_constraint_warnings(int eConstrAlg,int warncount)
165 const char *abort="- aborting to avoid logfile runaway.\n"
166 "This normally happens when your system is not sufficiently equilibrated,"
167 "or if you are changing lambda too fast in free energy simulations.\n";
169 gmx_fatal(FARGS,
170 "Too many %s warnings (%d)\n"
171 "If you know what you are doing you can %s"
172 "set the environment variable GMX_MAXCONSTRWARN to -1,\n"
173 "but normally it is better to fix the problem",
174 (eConstrAlg == econtLINCS) ? "LINCS" : "SETTLE",warncount,
175 (eConstrAlg == econtLINCS) ?
176 "adjust the lincs warning threshold in your mdp file\nor " : "\n");
179 static void write_constr_pdb(const char *fn,const char *title,
180 gmx_mtop_t *mtop,
181 int start,int homenr,t_commrec *cr,
182 rvec x[],matrix box)
184 char fname[STRLEN],format[STRLEN];
185 FILE *out;
186 int dd_ac0=0,dd_ac1=0,i,ii,resnr;
187 gmx_domdec_t *dd;
188 char *anm,*resnm;
190 dd = NULL;
191 if (PAR(cr)) {
192 sprintf(fname,"%s_n%d.pdb",fn,cr->sim_nodeid);
193 if (DOMAINDECOMP(cr)) {
194 dd = cr->dd;
195 dd_get_constraint_range(dd,&dd_ac0,&dd_ac1);
196 start = 0;
197 homenr = dd_ac1;
199 } else {
200 sprintf(fname,"%s.pdb",fn);
202 sprintf(format,"%s\n",pdbformat);
204 out = gmx_fio_fopen(fname,"w");
206 fprintf(out,"TITLE %s\n",title);
207 gmx_write_pdb_box(out,-1,box);
208 for(i=start; i<start+homenr; i++) {
209 if (dd) {
210 if (i >= dd->nat_home && i < dd_ac0)
211 continue;
212 ii = dd->gatindex[i];
213 } else {
214 ii = i;
216 gmx_mtop_atominfo_global(mtop,ii,&anm,&resnr,&resnm);
217 fprintf(out,format,"ATOM",(ii+1)%100000,
218 anm,resnm,' ',(resnr+1)%10000,' ',
219 10*x[i][XX],10*x[i][YY],10*x[i][ZZ]);
221 fprintf(out,"TER\n");
223 gmx_fio_fclose(out);
226 static void dump_confs(FILE *fplog,gmx_large_int_t step,gmx_mtop_t *mtop,
227 int start,int homenr,t_commrec *cr,
228 rvec x[],rvec xprime[],matrix box)
230 char buf[256],buf2[22];
232 char *env=getenv("GMX_SUPPRESS_DUMP");
233 if (env)
234 return;
236 sprintf(buf,"step%sb",gmx_step_str(step,buf2));
237 write_constr_pdb(buf,"initial coordinates",
238 mtop,start,homenr,cr,x,box);
239 sprintf(buf,"step%sc",gmx_step_str(step,buf2));
240 write_constr_pdb(buf,"coordinates after constraining",
241 mtop,start,homenr,cr,xprime,box);
242 if (fplog)
244 fprintf(fplog,"Wrote pdb files with previous and current coordinates\n");
246 fprintf(stderr,"Wrote pdb files with previous and current coordinates\n");
249 static void pr_sortblock(FILE *fp,const char *title,int nsb,t_sortblock sb[])
251 int i;
253 fprintf(fp,"%s\n",title);
254 for(i=0; (i<nsb); i++)
255 fprintf(fp,"i: %5d, iatom: (%5d %5d %5d), blocknr: %5d\n",
256 i,sb[i].iatom[0],sb[i].iatom[1],sb[i].iatom[2],
257 sb[i].blocknr);
260 bool constrain(FILE *fplog,bool bLog,bool bEner,
261 struct gmx_constr *constr,
262 t_idef *idef,t_inputrec *ir,
263 t_commrec *cr,
264 gmx_large_int_t step,int delta_step,
265 t_mdatoms *md,
266 rvec *x,rvec *xprime,rvec *min_proj,matrix box,
267 real lambda,real *dvdlambda,
268 rvec *v,tensor *vir,
269 t_nrnb *nrnb,int econq,bool bPscal,real veta, real vetanew)
271 bool bOK;
272 int start,homenr,nrend;
273 int i,j,d;
274 int ncons,error;
275 tensor rmdr;
276 rvec *vstor;
277 real invdt,vir_fac,t;
278 t_ilist *settle;
279 int nsettle;
280 t_pbc pbc;
281 char buf[22];
282 t_vetavars *vetavar;
284 if (econq == econqForceDispl && !EI_ENERGY_MINIMIZATION(ir->eI))
286 gmx_incons("constrain called for forces displacements while not doing energy minimization, can not do this while the LINCS and SETTLE constraint connection matrices are mass weighted");
289 bOK = TRUE;
291 start = md->start;
292 homenr = md->homenr;
293 nrend = start+homenr;
295 /* set constants for pressure control integration */
296 vetavar = init_vetavars(veta,vetanew,ir);
298 if (ir->delta_t == 0)
300 invdt = 0;
302 else
304 invdt = 1/ir->delta_t;
307 if (ir->efep != efepNO && EI_DYNAMICS(ir->eI))
309 /* Set the constraint lengths for the step at which this configuration
310 * is meant to be. The invmasses should not be changed.
312 lambda += delta_step*ir->delta_lambda;
315 if (vir != NULL)
317 clear_mat(rmdr);
320 where();
321 if (constr->lincsd)
323 bOK = constrain_lincs(fplog,bLog,bEner,ir,step,constr->lincsd,md,cr,
324 x,xprime,min_proj,box,lambda,dvdlambda,
325 invdt,v,vir!=NULL,rmdr,
326 econq,nrnb,
327 constr->maxwarn,&constr->warncount_lincs);
328 if (!bOK && constr->maxwarn >= 0 && fplog)
330 fprintf(fplog,"Constraint error in algorithm %s at step %s\n",
331 econstr_names[econtLINCS],gmx_step_str(step,buf));
335 if (constr->nblocks > 0)
337 switch (econq) {
338 case (econqCoord):
339 bOK = bshakef(fplog,constr->shaked,
340 homenr,md->invmass,constr->nblocks,constr->sblock,
341 idef,ir,box,x,xprime,nrnb,
342 constr->lagr,lambda,dvdlambda,
343 invdt,v,vir!=NULL,rmdr,constr->maxwarn>=0,econq,vetavar);
344 break;
345 case (econqVeloc):
346 bOK = bshakef(fplog,constr->shaked,
347 homenr,md->invmass,constr->nblocks,constr->sblock,
348 idef,ir,box,x,min_proj,nrnb,
349 constr->lagr,lambda,dvdlambda,
350 invdt,NULL,vir!=NULL,rmdr,constr->maxwarn>=0,econq,vetavar);
351 break;
352 default:
353 gmx_fatal(FARGS,"Internal error, SHAKE called for constraining something else than coordinates");
354 break;
356 if (!bOK && constr->maxwarn >= 0 && fplog)
358 fprintf(fplog,"Constraint error in algorithm %s at step %s\n",
359 econstr_names[econtSHAKE],gmx_step_str(step,buf));
363 settle = &idef->il[F_SETTLE];
364 if (settle->nr > 0)
366 nsettle = settle->nr/2;
368 switch (econq)
370 case econqCoord:
371 csettle(constr->settled,
372 nsettle,settle->iatoms,x[0],xprime[0],
373 invdt,v[0],vir!=NULL,rmdr,&error,vetavar);
374 inc_nrnb(nrnb,eNR_SETTLE,nsettle);
375 if (v != NULL)
377 inc_nrnb(nrnb,eNR_CONSTR_V,nsettle*3);
379 if (vir != NULL)
381 inc_nrnb(nrnb,eNR_CONSTR_VIR,nsettle*3);
384 bOK = (error < 0);
385 if (!bOK && constr->maxwarn >= 0)
387 char buf[256];
388 sprintf(buf,
389 "\nt = %.3f ps: Water molecule starting at atom %d can not be "
390 "settled.\nCheck for bad contacts and/or reduce the timestep.\n",
391 ir->init_t+step*ir->delta_t,
392 ddglatnr(cr->dd,settle->iatoms[error*2+1]));
393 if (fplog)
395 fprintf(fplog,"%s",buf);
397 fprintf(stderr,"%s",buf);
398 constr->warncount_settle++;
399 if (constr->warncount_settle > constr->maxwarn)
401 too_many_constraint_warnings(-1,constr->warncount_settle);
403 break;
404 case econqVeloc:
405 case econqDeriv:
406 case econqForce:
407 case econqForceDispl:
408 settle_proj(fplog,constr->settled,econq,
409 nsettle,settle->iatoms,x,
410 xprime,min_proj,vir!=NULL,rmdr,vetavar);
411 /* This is an overestimate */
412 inc_nrnb(nrnb,eNR_SETTLE,nsettle);
413 break;
414 case econqDeriv_FlexCon:
415 /* Nothing to do, since the are no flexible constraints in settles */
416 break;
417 default:
418 gmx_incons("Unknown constraint quantity for settle");
423 free_vetavars(vetavar);
425 if (vir != NULL)
427 switch (econq)
429 case econqCoord:
430 vir_fac = 0.5/(ir->delta_t*ir->delta_t);
431 break;
432 case econqVeloc:
433 vir_fac = 0.5/ir->delta_t;
434 break;
435 case econqForce:
436 case econqForceDispl:
437 vir_fac = 0.5;
438 break;
439 default:
440 vir_fac = 0;
441 gmx_incons("Unsupported constraint quantity for virial");
444 if (ir->eI == eiVV)
446 vir_fac *= 2; /* only constraining over half the distance here */
448 for(i=0; i<DIM; i++)
450 for(j=0; j<DIM; j++)
452 (*vir)[i][j] = vir_fac*rmdr[i][j];
457 if (!bOK && constr->maxwarn >= 0)
459 dump_confs(fplog,step,constr->warn_mtop,start,homenr,cr,x,xprime,box);
462 if (econq == econqCoord)
464 if (ir->ePull == epullCONSTRAINT)
466 if (EI_DYNAMICS(ir->eI))
468 t = ir->init_t + (step + delta_step)*ir->delta_t;
470 else
472 t = ir->init_t;
474 set_pbc(&pbc,ir->ePBC,box);
475 pull_constraint(ir->pull,md,&pbc,cr,ir->delta_t,t,x,xprime,v,*vir);
477 if (constr->ed && delta_step > 0)
479 /* apply the essential dynamcs constraints here */
480 do_edsam(ir,step,md,cr,xprime,v,box,constr->ed);
484 return bOK;
487 real *constr_rmsd_data(struct gmx_constr *constr)
489 if (constr->lincsd)
490 return lincs_rmsd_data(constr->lincsd);
491 else
492 return NULL;
495 real constr_rmsd(struct gmx_constr *constr,bool bSD2)
497 if (constr->lincsd)
498 return lincs_rmsd(constr->lincsd,bSD2);
499 else
500 return 0;
503 static void make_shake_sblock_pd(struct gmx_constr *constr,
504 t_idef *idef,t_mdatoms *md)
506 int i,j,m,ncons;
507 int bstart,bnr;
508 t_blocka sblocks;
509 t_sortblock *sb;
510 t_iatom *iatom;
511 atom_id *inv_sblock;
513 /* Since we are processing the local topology,
514 * the F_CONSTRNC ilist has been concatenated to the F_CONSTR ilist.
516 ncons = idef->il[F_CONSTR].nr/3;
518 init_blocka(&sblocks);
519 gen_sblocks(NULL,md->start,md->start+md->homenr,idef,&sblocks,FALSE);
522 bstart=(idef->nodeid > 0) ? blocks->multinr[idef->nodeid-1] : 0;
523 nblocks=blocks->multinr[idef->nodeid] - bstart;
525 bstart = 0;
526 constr->nblocks = sblocks.nr;
527 if (debug)
528 fprintf(debug,"ncons: %d, bstart: %d, nblocks: %d\n",
529 ncons,bstart,constr->nblocks);
531 /* Calculate block number for each atom */
532 inv_sblock = make_invblocka(&sblocks,md->nr);
534 done_blocka(&sblocks);
536 /* Store the block number in temp array and
537 * sort the constraints in order of the sblock number
538 * and the atom numbers, really sorting a segment of the array!
540 #ifdef DEBUGIDEF
541 pr_idef(fplog,0,"Before Sort",idef);
542 #endif
543 iatom=idef->il[F_CONSTR].iatoms;
544 snew(sb,ncons);
545 for(i=0; (i<ncons); i++,iatom+=3) {
546 for(m=0; (m<3); m++)
547 sb[i].iatom[m] = iatom[m];
548 sb[i].blocknr = inv_sblock[iatom[1]];
551 /* Now sort the blocks */
552 if (debug) {
553 pr_sortblock(debug,"Before sorting",ncons,sb);
554 fprintf(debug,"Going to sort constraints\n");
557 qsort(sb,ncons,(size_t)sizeof(*sb),pcomp);
559 if (debug) {
560 pr_sortblock(debug,"After sorting",ncons,sb);
563 iatom=idef->il[F_CONSTR].iatoms;
564 for(i=0; (i<ncons); i++,iatom+=3)
565 for(m=0; (m<3); m++)
566 iatom[m]=sb[i].iatom[m];
567 #ifdef DEBUGIDEF
568 pr_idef(fplog,0,"After Sort",idef);
569 #endif
571 j=0;
572 snew(constr->sblock,constr->nblocks+1);
573 bnr=-2;
574 for(i=0; (i<ncons); i++) {
575 if (sb[i].blocknr != bnr) {
576 bnr=sb[i].blocknr;
577 constr->sblock[j++]=3*i;
580 /* Last block... */
581 constr->sblock[j++] = 3*ncons;
583 if (j != (constr->nblocks+1)) {
584 fprintf(stderr,"bstart: %d\n",bstart);
585 fprintf(stderr,"j: %d, nblocks: %d, ncons: %d\n",
586 j,constr->nblocks,ncons);
587 for(i=0; (i<ncons); i++)
588 fprintf(stderr,"i: %5d sb[i].blocknr: %5u\n",i,sb[i].blocknr);
589 for(j=0; (j<=constr->nblocks); j++)
590 fprintf(stderr,"sblock[%3d]=%5d\n",j,(int)constr->sblock[j]);
591 gmx_fatal(FARGS,"DEATH HORROR: "
592 "sblocks does not match idef->il[F_CONSTR]");
594 sfree(sb);
595 sfree(inv_sblock);
598 static void make_shake_sblock_dd(struct gmx_constr *constr,
599 t_ilist *ilcon,t_block *cgs,
600 gmx_domdec_t *dd)
602 int ncons,c,cg;
603 t_iatom *iatom;
605 if (dd->ncg_home+1 > constr->sblock_nalloc) {
606 constr->sblock_nalloc = over_alloc_dd(dd->ncg_home+1);
607 srenew(constr->sblock,constr->sblock_nalloc);
610 ncons = ilcon->nr/3;
611 iatom = ilcon->iatoms;
612 constr->nblocks = 0;
613 cg = 0;
614 for(c=0; c<ncons; c++) {
615 if (c == 0 || iatom[1] >= cgs->index[cg+1]) {
616 constr->sblock[constr->nblocks++] = 3*c;
617 while (iatom[1] >= cgs->index[cg+1])
618 cg++;
620 iatom += 3;
622 constr->sblock[constr->nblocks] = 3*ncons;
625 t_blocka make_at2con(int start,int natoms,
626 t_ilist *ilist,t_iparams *iparams,
627 bool bDynamics,int *nflexiblecons)
629 int *count,ncon,con,con_tot,nflexcon,ftype,i,a;
630 t_iatom *ia;
631 t_blocka at2con;
632 bool bFlexCon;
634 snew(count,natoms);
635 nflexcon = 0;
636 for(ftype=F_CONSTR; ftype<=F_CONSTRNC; ftype++) {
637 ncon = ilist[ftype].nr/3;
638 ia = ilist[ftype].iatoms;
639 for(con=0; con<ncon; con++) {
640 bFlexCon = (iparams[ia[0]].constr.dA == 0 &&
641 iparams[ia[0]].constr.dB == 0);
642 if (bFlexCon)
643 nflexcon++;
644 if (bDynamics || !bFlexCon) {
645 for(i=1; i<3; i++) {
646 a = ia[i] - start;
647 count[a]++;
650 ia += 3;
653 *nflexiblecons = nflexcon;
655 at2con.nr = natoms;
656 at2con.nalloc_index = at2con.nr+1;
657 snew(at2con.index,at2con.nalloc_index);
658 at2con.index[0] = 0;
659 for(a=0; a<natoms; a++) {
660 at2con.index[a+1] = at2con.index[a] + count[a];
661 count[a] = 0;
663 at2con.nra = at2con.index[natoms];
664 at2con.nalloc_a = at2con.nra;
665 snew(at2con.a,at2con.nalloc_a);
667 /* The F_CONSTRNC constraints have constraint numbers
668 * that continue after the last F_CONSTR constraint.
670 con_tot = 0;
671 for(ftype=F_CONSTR; ftype<=F_CONSTRNC; ftype++) {
672 ncon = ilist[ftype].nr/3;
673 ia = ilist[ftype].iatoms;
674 for(con=0; con<ncon; con++) {
675 bFlexCon = (iparams[ia[0]].constr.dA == 0 &&
676 iparams[ia[0]].constr.dB == 0);
677 if (bDynamics || !bFlexCon) {
678 for(i=1; i<3; i++) {
679 a = ia[i] - start;
680 at2con.a[at2con.index[a]+count[a]++] = con_tot;
683 con_tot++;
684 ia += 3;
688 sfree(count);
690 return at2con;
693 void set_constraints(struct gmx_constr *constr,
694 gmx_localtop_t *top,t_inputrec *ir,
695 t_mdatoms *md,t_commrec *cr)
697 t_idef *idef;
698 int ncons;
699 t_ilist *settle;
700 int iO,iH;
702 idef = &top->idef;
704 if (constr->ncon_tot > 0)
706 /* We are using the local topology,
707 * so there are only F_CONSTR constraints.
709 ncons = idef->il[F_CONSTR].nr/3;
711 /* With DD we might also need to call LINCS with ncons=0 for
712 * communicating coordinates to other nodes that do have constraints.
714 if (ir->eConstrAlg == econtLINCS)
716 set_lincs(idef,md,EI_DYNAMICS(ir->eI),cr,constr->lincsd);
718 if (ir->eConstrAlg == econtSHAKE)
720 if (cr->dd)
722 make_shake_sblock_dd(constr,&idef->il[F_CONSTR],&top->cgs,cr->dd);
724 else
726 make_shake_sblock_pd(constr,idef,md);
728 if (ncons > constr->lagr_nalloc)
730 constr->lagr_nalloc = over_alloc_dd(ncons);
731 srenew(constr->lagr,constr->lagr_nalloc);
734 constr->shaked = shake_init();
738 if (idef->il[F_SETTLE].nr > 0 && constr->settled == NULL)
740 settle = &idef->il[F_SETTLE];
741 iO = settle->iatoms[1];
742 iH = settle->iatoms[1]+1;
743 constr->settled =
744 settle_init(md->massT[iO],md->massT[iH],
745 md->invmass[iO],md->invmass[iH],
746 idef->iparams[settle->iatoms[0]].settle.doh,
747 idef->iparams[settle->iatoms[0]].settle.dhh);
750 /* Make a selection of the local atoms for essential dynamics */
751 if (constr->ed && cr->dd)
753 dd_make_local_ed_indices(cr->dd,constr->ed,md);
757 static void constr_recur(t_blocka *at2con,
758 t_ilist *ilist,t_iparams *iparams,bool bTopB,
759 int at,int depth,int nc,int *path,
760 real r0,real r1,real *r2max,
761 int *count)
763 int ncon1;
764 t_iatom *ia1,*ia2;
765 int c,con,a1;
766 bool bUse;
767 t_iatom *ia;
768 real len,rn0,rn1;
770 (*count)++;
772 ncon1 = ilist[F_CONSTR].nr/3;
773 ia1 = ilist[F_CONSTR].iatoms;
774 ia2 = ilist[F_CONSTRNC].iatoms;
776 /* Loop over all constraints connected to this atom */
777 for(c=at2con->index[at]; c<at2con->index[at+1]; c++) {
778 con = at2con->a[c];
779 /* Do not walk over already used constraints */
780 bUse = TRUE;
781 for(a1=0; a1<depth; a1++) {
782 if (con == path[a1])
783 bUse = FALSE;
785 if (bUse) {
786 ia = constr_iatomptr(ncon1,ia1,ia2,con);
787 /* Flexible constraints currently have length 0, which is incorrect */
788 if (!bTopB)
789 len = iparams[ia[0]].constr.dA;
790 else
791 len = iparams[ia[0]].constr.dB;
792 /* In the worst case the bond directions alternate */
793 if (nc % 2 == 0) {
794 rn0 = r0 + len;
795 rn1 = r1;
796 } else {
797 rn0 = r0;
798 rn1 = r1 + len;
800 /* Assume angles of 120 degrees between all bonds */
801 if (rn0*rn0 + rn1*rn1 + rn0*rn1 > *r2max) {
802 *r2max = rn0*rn0 + rn1*rn1 + r0*rn1;
803 if (debug) {
804 fprintf(debug,"Found longer constraint distance: r0 %5.3f r1 %5.3f rmax %5.3f\n", rn0,rn1,sqrt(*r2max));
805 for(a1=0; a1<depth; a1++)
806 fprintf(debug," %d %5.3f",
807 path[a1],
808 iparams[constr_iatomptr(ncon1,ia1,ia2,con)[0]].constr.dA);
809 fprintf(debug," %d %5.3f\n",con,len);
812 /* Limit the number of recursions to 1000*nc,
813 * so a call does not take more than a second,
814 * even for highly connected systems.
816 if (depth + 1 < nc && *count < 1000*nc) {
817 if (ia[1] == at)
818 a1 = ia[2];
819 else
820 a1 = ia[1];
821 /* Recursion */
822 path[depth] = con;
823 constr_recur(at2con,ilist,iparams,
824 bTopB,a1,depth+1,nc,path,rn0,rn1,r2max,count);
825 path[depth] = -1;
831 static real constr_r_max_moltype(FILE *fplog,
832 gmx_moltype_t *molt,t_iparams *iparams,
833 t_inputrec *ir)
835 int natoms,nflexcon,*path,at,count;
837 t_blocka at2con;
838 real r0,r1,r2maxA,r2maxB,rmax,lam0,lam1;
840 if (molt->ilist[F_CONSTR].nr == 0 &&
841 molt->ilist[F_CONSTRNC].nr == 0) {
842 return 0;
845 natoms = molt->atoms.nr;
847 at2con = make_at2con(0,natoms,molt->ilist,iparams,
848 EI_DYNAMICS(ir->eI),&nflexcon);
849 snew(path,1+ir->nProjOrder);
850 for(at=0; at<1+ir->nProjOrder; at++)
851 path[at] = -1;
853 r2maxA = 0;
854 for(at=0; at<natoms; at++) {
855 r0 = 0;
856 r1 = 0;
858 count = 0;
859 constr_recur(&at2con,molt->ilist,iparams,
860 FALSE,at,0,1+ir->nProjOrder,path,r0,r1,&r2maxA,&count);
862 if (ir->efep == efepNO) {
863 rmax = sqrt(r2maxA);
864 } else {
865 r2maxB = 0;
866 for(at=0; at<natoms; at++) {
867 r0 = 0;
868 r1 = 0;
869 count = 0;
870 constr_recur(&at2con,molt->ilist,iparams,
871 TRUE,at,0,1+ir->nProjOrder,path,r0,r1,&r2maxB,&count);
873 lam0 = ir->init_lambda;
874 if (EI_DYNAMICS(ir->eI))
875 lam0 += ir->init_step*ir->delta_lambda;
876 rmax = (1 - lam0)*sqrt(r2maxA) + lam0*sqrt(r2maxB);
877 if (EI_DYNAMICS(ir->eI)) {
878 lam1 = ir->init_lambda + (ir->init_step + ir->nsteps)*ir->delta_lambda;
879 rmax = max(rmax,(1 - lam1)*sqrt(r2maxA) + lam1*sqrt(r2maxB));
883 done_blocka(&at2con);
884 sfree(path);
886 return rmax;
889 real constr_r_max(FILE *fplog,gmx_mtop_t *mtop,t_inputrec *ir)
891 int mt;
892 real rmax;
894 rmax = 0;
895 for(mt=0; mt<mtop->nmoltype; mt++) {
896 rmax = max(rmax,
897 constr_r_max_moltype(fplog,&mtop->moltype[mt],
898 mtop->ffparams.iparams,ir));
901 if (fplog)
902 fprintf(fplog,"Maximum distance for %d constraints, at 120 deg. angles, all-trans: %.3f nm\n",1+ir->nProjOrder,rmax);
904 return rmax;
907 gmx_constr_t init_constraints(FILE *fplog,
908 gmx_mtop_t *mtop,t_inputrec *ir,
909 gmx_edsam_t ed,t_state *state,
910 t_commrec *cr)
912 int ncon,nset,nmol,settle_type,i,natoms,mt,nflexcon;
913 struct gmx_constr *constr;
914 char *env;
915 t_ilist *ilist;
916 gmx_mtop_ilistloop_t iloop;
918 ncon =
919 gmx_mtop_ftype_count(mtop,F_CONSTR) +
920 gmx_mtop_ftype_count(mtop,F_CONSTRNC);
921 nset = gmx_mtop_ftype_count(mtop,F_SETTLE);
923 if (ncon+nset == 0 && ir->ePull != epullCONSTRAINT && ed == NULL)
925 return NULL;
928 snew(constr,1);
930 constr->ncon_tot = ncon;
931 constr->nflexcon = 0;
932 if (ncon > 0)
934 constr->n_at2con_mt = mtop->nmoltype;
935 snew(constr->at2con_mt,constr->n_at2con_mt);
936 for(mt=0; mt<mtop->nmoltype; mt++)
938 constr->at2con_mt[mt] = make_at2con(0,mtop->moltype[mt].atoms.nr,
939 mtop->moltype[mt].ilist,
940 mtop->ffparams.iparams,
941 EI_DYNAMICS(ir->eI),&nflexcon);
942 for(i=0; i<mtop->nmolblock; i++)
944 if (mtop->molblock[i].type == mt)
946 constr->nflexcon += mtop->molblock[i].nmol*nflexcon;
951 if (constr->nflexcon > 0)
953 if (fplog)
955 fprintf(fplog,"There are %d flexible constraints\n",
956 constr->nflexcon);
957 if (ir->fc_stepsize == 0)
959 fprintf(fplog,"\n"
960 "WARNING: step size for flexible constraining = 0\n"
961 " All flexible constraints will be rigid.\n"
962 " Will try to keep all flexible constraints at their original length,\n"
963 " but the lengths may exhibit some drift.\n\n");
964 constr->nflexcon = 0;
967 if (constr->nflexcon > 0)
969 please_cite(fplog,"Hess2002");
973 if (ir->eConstrAlg == econtLINCS)
975 constr->lincsd = init_lincs(fplog,mtop,
976 constr->nflexcon,constr->at2con_mt,
977 DOMAINDECOMP(cr) && cr->dd->bInterCGcons,
978 ir->nLincsIter,ir->nProjOrder);
981 if (ir->eConstrAlg == econtSHAKE) {
982 if (DOMAINDECOMP(cr) && cr->dd->bInterCGcons)
984 gmx_fatal(FARGS,"SHAKE is not supported with domain decomposition and constraint that cross charge group boundaries, use LINCS");
986 if (constr->nflexcon)
988 gmx_fatal(FARGS,"For this system also velocities and/or forces need to be constrained, this can not be done with SHAKE, you should select LINCS");
990 please_cite(fplog,"Ryckaert77a");
991 if (ir->bShakeSOR)
993 please_cite(fplog,"Barth95a");
998 if (nset > 0) {
999 please_cite(fplog,"Miyamoto92a");
1001 /* Check that we have only one settle type */
1002 settle_type = -1;
1003 iloop = gmx_mtop_ilistloop_init(mtop);
1004 while (gmx_mtop_ilistloop_next(iloop,&ilist,&nmol))
1006 for (i=0; i<ilist[F_SETTLE].nr; i+=2)
1008 if (settle_type == -1)
1010 settle_type = ilist[F_SETTLE].iatoms[i];
1012 else if (ilist[F_SETTLE].iatoms[i] != settle_type)
1014 gmx_fatal(FARGS,"More than one settle type.\n"
1015 "Suggestion: change the least use settle constraints into 3 normal constraints.");
1021 constr->maxwarn = 999;
1022 env = getenv("GMX_MAXCONSTRWARN");
1023 if (env)
1025 constr->maxwarn = 0;
1026 sscanf(env,"%d",&constr->maxwarn);
1027 if (fplog)
1029 fprintf(fplog,
1030 "Setting the maximum number of constraint warnings to %d\n",
1031 constr->maxwarn);
1033 if (MASTER(cr))
1035 fprintf(stderr,
1036 "Setting the maximum number of constraint warnings to %d\n",
1037 constr->maxwarn);
1040 if (constr->maxwarn < 0 && fplog)
1042 fprintf(fplog,"maxwarn < 0, will not stop on constraint errors\n");
1044 constr->warncount_lincs = 0;
1045 constr->warncount_settle = 0;
1047 /* Initialize the essential dynamics sampling.
1048 * Put the pointer to the ED struct in constr */
1049 constr->ed = ed;
1050 if (ed != NULL)
1052 init_edsam(mtop,ir,cr,ed,state->x,state->box);
1055 constr->warn_mtop = mtop;
1057 return constr;
1060 t_blocka *atom2constraints_moltype(gmx_constr_t constr)
1062 return constr->at2con_mt;
1066 bool inter_charge_group_constraints(gmx_mtop_t *mtop)
1068 const gmx_moltype_t *molt;
1069 const t_block *cgs;
1070 const t_ilist *il;
1071 int mb;
1072 int nat,*at2cg,cg,a,ftype,i;
1073 bool bInterCG;
1075 bInterCG = FALSE;
1076 for(mb=0; mb<mtop->nmolblock && !bInterCG; mb++) {
1077 molt = &mtop->moltype[mtop->molblock[mb].type];
1079 if (molt->ilist[F_CONSTR].nr > 0 ||
1080 molt->ilist[F_CONSTRNC].nr > 0) {
1081 cgs = &molt->cgs;
1082 snew(at2cg,molt->atoms.nr);
1083 for(cg=0; cg<cgs->nr; cg++) {
1084 for(a=cgs->index[cg]; a<cgs->index[cg+1]; a++)
1085 at2cg[a] = cg;
1088 for(ftype=F_CONSTR; ftype<=F_CONSTRNC; ftype++) {
1089 il = &molt->ilist[ftype];
1090 for(i=0; i<il->nr && !bInterCG; i+=3) {
1091 if (at2cg[il->iatoms[i+1]] != at2cg[il->iatoms[i+2]])
1092 bInterCG = TRUE;
1095 sfree(at2cg);
1099 return bInterCG;