4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
27 * GRowing Old MAkes el Chrono Sweat
29 static char *SRCID_toppush_c
= "$Id$";
32 #ident "@(#) toppush.c 1.72 9/30/97"
49 static char errbuf
[256];
51 void generate_nbparams(int comb
,int ftype
,t_params
*plist
,t_atomtype
*atype
)
55 real c
,sig6
,sigma_ij
,eps_ij
,bi
,bj
;
57 /* Lean mean shortcuts */
60 snew(plist
->param
,nr
*nr
);
63 /* Fill the matrix with force parameters */
67 case eCOMB_ARITHMETIC
:
69 for(i
=k
=0; (i
<nr
); i
++)
70 for(j
=0; (j
<nr
); j
++,k
++)
71 for(nf
=0; (nf
<nrfp
); nf
++) {
72 c
= sqrt(atype
->nb
[i
].c
[nf
] * atype
->nb
[j
].c
[nf
]);
73 plist
->param
[k
].c
[nf
] = c
;
78 /* c0 and c1 are epsilon and sigma */
79 for (i
=k
=0; (i
< nr
); i
++)
80 for (j
=0; (j
< nr
); j
++,k
++) {
81 sigma_ij
= (atype
->nb
[i
].C0
+atype
->nb
[j
].C0
)*0.5;
82 eps_ij
= sqrt(atype
->nb
[i
].C1
*atype
->nb
[j
].C1
);
83 sig6
= pow(sigma_ij
,6.0);
85 plist
->param
[k
].c
[0] = 4*eps_ij
*sig6
;
86 plist
->param
[k
].c
[1] = 4*eps_ij
*sig6
*sig6
;
90 case eCOMB_ARITH_SIG_EPS
:
91 /* c0 and c1 are epsilon and sigma */
92 for (i
=k
=0; (i
< nr
); i
++)
93 for (j
=0; (j
< nr
); j
++,k
++) {
94 sigma_ij
= sqrt(atype
->nb
[i
].C0
*atype
->nb
[j
].C0
);
95 eps_ij
= sqrt(atype
->nb
[i
].C1
*atype
->nb
[j
].C1
);
96 sig6
= pow(sigma_ij
,6.0);
98 plist
->param
[k
].c
[0] = 4*eps_ij
*sig6
;
99 plist
->param
[k
].c
[1] = 4*eps_ij
*sig6
*sig6
;
104 fatal_error(0,"No such combination rule %d",comb
);
106 assert(plist
->nr
== k
);
110 /* Buckingham rules */
111 for(i
=k
=0; (i
<nr
); i
++)
112 for(j
=0; (j
<nr
); j
++,k
++) {
113 plist
->param
[k
].c
[0] = sqrt(atype
->nb
[i
].c
[0] * atype
->nb
[j
].c
[0]);
114 bi
= atype
->nb
[i
].c
[1];
115 bj
= atype
->nb
[j
].c
[1];
116 if ((bi
== 0) || (bj
== 0))
117 plist
->param
[k
].c
[1] = 0;
119 plist
->param
[k
].c
[1] = 2.0/(1/bi
+1/bj
);
120 plist
->param
[k
].c
[2] = sqrt(atype
->nb
[i
].c
[2] * atype
->nb
[j
].c
[2]);
125 sprintf(errbuf
,"Invalid nonbonded type %s",
126 interaction_function
[ftype
].longname
);
131 void push_at (t_symtab
*symtab
, t_atomtype
*at
, char *line
,int nb_funct
)
137 t_xlate xl
[eptNR
] = {
145 int nr
,i
,j
,pt
,nfp0
=-1;
146 char type
[STRLEN
],ptype
[STRLEN
];
148 double c
[MAXFORCEPARAM
];
155 if (sscanf (line
,"%s%lf%lf%s%lf%lf",type
,&m
,&q
,ptype
,&c
[0],&c
[1]) != 6) {
162 if (sscanf (line
,"%s%lf%lf%s%lf%lf%lf",
163 type
,&m
,&q
,ptype
,&c
[0],&c
[1],&c
[2]) != 7) {
169 fatal_error(0,"Invalid function type %d in push_at %s %d",nb_funct
,
172 for(j
=nfp0
; (j
<MAXFORCEPARAM
); j
++)
175 for(j
=0; (j
<eptNR
); j
++)
176 if (strcasecmp(ptype
,xl
[j
].entry
) == 0)
179 fatal_error(0,"Invalid particle type %s on line %s",
183 fprintf(debug
,"ptype: %s\n",ptype_str
[pt
]);
185 /* Test if this type overwrites another */
186 for (i
=0; ((i
<nr
) && (strcasecmp(*(at
->atomname
[i
]),type
) != 0)); i
++)
189 if ((i
==nr
) || (nr
==0)) {
190 /* New atomtype, get new space for arrays */
191 srenew(at
->atom
,nr
+1);
192 srenew(at
->atomname
,nr
+1);
197 sprintf(errbuf
,"Overriding atomtype %s",type
);
201 /* fill the arrays */
202 at
->atomname
[nr
] = put_symtab(symtab
,type
);
203 at
->atom
[nr
].ptype
= pt
;
207 for (i
=0; (i
<MAXFORCEPARAM
); i
++)
208 at
->nb
[nr
].c
[i
] = c
[i
];
211 static void push_bondtype(t_params
*bt
,t_param
*b
,int nral
,int ftype
)
216 int nrfp
= NRFP(ftype
);
218 /* Check if this entry overwrites another */
220 for (i
=0; (i
< nr
); i
++) {
222 for (j
=0; (j
< nral
); j
++)
223 bTest
=(bTest
&& (b
->a
[j
] == bt
->param
[i
].a
[j
]));
226 for(j
=0; (j
<nral
); j
++)
227 bTest
=(bTest
&& (b
->a
[nral
-1-j
] == bt
->param
[i
].a
[j
]));
231 for (j
=0; (j
< nrfp
); j
++)
232 bt
->param
[i
].c
[j
] = b
->c
[j
];
240 /* fill the arrays up and down */
241 memcpy((char *) bt
->param
[bt
->nr
].c
, (char *) b
->c
,(size_t)sizeof(b
->c
));
242 memcpy((char *) bt
->param
[bt
->nr
].a
, (char *) b
->a
,(size_t)sizeof(b
->a
));
243 memcpy((char *) bt
->param
[bt
->nr
+1].c
,(char *) b
->c
,(size_t)sizeof(b
->c
));
244 for (j
=0; (j
< nral
); j
++)
245 bt
->param
[bt
->nr
+1].a
[j
] = b
->a
[nral
-1-j
];
251 void push_bt(directive d
,t_params bt
[],int nral
,t_atomtype
*at
,char *line
)
253 static char *formal
[MAXATOMLIST
+1] = {
261 static char *formnl
[MAXATOMLIST
+1] = {
269 static char *formlf
[MAXFORCEPARAM
] = {
275 "%lf%lf%lf%lf%lf%lf",
277 int i
,ft
,ftype
,nn
,nrfp
;
279 char alc
[MAXATOMLIST
+1][20];
280 double c
[MAXFORCEPARAM
];
283 /* Make format string (nral ints+functype) */
284 if ((nn
=sscanf(line
,formal
[nral
],
285 alc
[0],alc
[1],alc
[2],alc
[3],alc
[4],alc
[5])) != nral
+1) {
286 sprintf(errbuf
,"Not enough atomtypes (%d instead of %d)",nn
-1,nral
);
291 ft
= atoi(alc
[nral
]);
292 ftype
= ifunc_index(d
,ft
);
294 strcpy(f1
,formnl
[nral
]);
295 strcat(f1
,formlf
[nrfp
-1]);
296 if ((nn
=sscanf(line
,f1
,&c
[0],&c
[1],&c
[2],&c
[3],&c
[4],&c
[5]))
298 for( ; (nn
<nrfp
); nn
++)
301 for(i
=0; (i
<nral
); i
++)
302 p
.a
[i
]=at2type(alc
[i
],at
);
303 for(i
=0; (i
<nrfp
); i
++)
305 push_bondtype (&(bt
[ftype
]),&p
,nral
,ftype
);
308 void push_nbt(directive d
,t_params nbt
[],t_atomtype
*atype
,
309 char *pline
,int nb_funct
)
312 static char *form2
="%*s%*s%*s%lf%lf";
313 static char *form3
="%*s%*s%*s%lf%lf%lf";
315 int i
,f
,k
,ftype
,atnr
,nrfp
;
319 if (sscanf (pline
,"%s%s%d",a0
,a1
,&f
) != 3) {
324 ftype
=ifunc_index(d
,f
);
326 if (ftype
!= nb_funct
) {
327 sprintf(errbuf
,"Trying to add %s while the default nonbond type is %s",
328 interaction_function
[ftype
].longname
,
329 interaction_function
[nb_funct
].longname
);
334 /* Get the force parameters */
335 if (NRFP(ftype
) == 2) {
336 if (sscanf(pline
,form2
,&c
[0],&c
[1]) != 2) {
342 if (sscanf(pline
,form3
,&c
[0],&c
[1],&c
[2]) != 3) {
347 /* Put the parameters in the matrix */
348 ai
= at2type (a0
,atype
);
349 aj
= at2type (a1
,atype
);
353 for (i
=0; (i
< nrfp
); i
++) {
355 nbt
[ftype
].param
[k
].c
[i
] = c
[i
];
357 nbt
[ftype
].param
[k
].c
[i
] = c
[i
];
361 static void push_atom_now(t_symtab
*symtab
,t_atoms
*at
,int atomnr
,
362 int type
,int ptype
,int resnumber
,int cgnumber
,
363 char *resname
,char *name
,real m0
,real q0
,
364 int typeB
,real mB
,real qB
)
369 if (((nr
==0) && (atomnr
!= 1)) || (nr
&& (atomnr
!= at
->nr
+1)))
370 fatal_error(0,"Atoms in the .top are not numbered consecutively from 1\n");
372 resnr_diff
= resnumber
- (at
->atom
[at
->nr
-1].resnr
+1);
373 if (((nr
==0) && (resnumber
!= 1)) ||
374 (nr
&& (resnr_diff
!= 0) && (resnr_diff
!= 1)))
375 fatal_error(0,"Residue numbers in the .top are not numbered consecutively from 1\n");
378 * get new space for arrays
380 srenew(at
->atom
,nr
+1);
381 srenew(at
->atomname
,nr
+1);
383 if (resnumber
> at
->nres
) {
385 srenew(at
->resname
,resnumber
);
386 at
->resname
[resnumber
-1] = put_symtab(symtab
,resname
);
389 at
->atom
[nr
].type
= type
;
390 at
->atom
[nr
].ptype
= ptype
;
393 at
->atom
[nr
].typeB
= typeB
;
394 at
->atom
[nr
].qB
= qB
;
395 at
->atom
[nr
].mB
= mB
;
397 for(j
=0; (j
<egcNR
); j
++)
398 at
->atom
[nr
].grpnr
[j
] = -1;
399 at
->atom
[nr
].resnr
= resnumber
-1;
400 at
->atomname
[nr
] = put_symtab(symtab
,name
);
404 void push_cg(t_block
*block
, int *lastindex
, int index
, int a
)
407 fprintf (debug
,"Index %d, Atom %d\n",index
,a
);
409 if (((block
->nr
) && (*lastindex
!= index
)) || (!block
->nr
)) {
410 /* add a new block */
412 srenew(block
->index
,block
->nr
+1);
414 srenew(block
->a
,block
->nra
+1);
415 block
->a
[block
->nra
++]=a
;
416 block
->index
[block
->nr
]=block
->nra
;
420 void push_atom(t_symtab
*symtab
,t_block
*cgs
,
421 t_atoms
*at
,t_atomtype
*atype
,char *line
)
425 int resnumber
,cgnumber
,atomnr
,type
,typeB
,nscan
;
426 char id
[STRLEN
],ctype
[STRLEN
],
427 resname
[STRLEN
],name
[STRLEN
];
431 /* Make a shortcut for writing in this molecule */
434 /* Fixed parameters */
435 if (sscanf(line
,"%s%s%d%s%s%d",
436 id
,ctype
,&resnumber
,resname
,name
,&cgnumber
) != 6) {
440 sscanf(id
,"%d",&atomnr
);
441 type
= at2type(ctype
,atype
);
442 ptype
= atype
->atom
[type
].ptype
;
444 /* Set default from type */
445 q0
= atype
->atom
[type
].q
;
446 m0
= atype
->atom
[type
].m
;
451 /* Optional parameters */
452 nscan
=sscanf(line
,"%*s%*s%*s%*s%*s%*s%lf%lf%s%lf%lf",
453 &q
,&m
,ctype
,&qb
,&mb
);
455 /* Nasty switch that falls thru all the way down! */
461 typeB
=at2type(ctype
,atype
);
462 qB
= atype
->atom
[typeB
].q
;
463 mB
= atype
->atom
[typeB
].m
;
473 fprintf(debug
,"mB=%g, qB=%g, typeB=%d\n",mB
,qB
,typeB
);
475 push_cg(cgs
,&lastcg
,cgnumber
,nr
);
477 push_atom_now(symtab
,at
,atomnr
,type
,ptype
,resnumber
,cgnumber
,
478 resname
,name
,m0
,q0
,typeB
,mB
,qB
);
481 void push_molt(t_symtab
*symtab
,t_molinfo
*newmol
,char *line
)
486 if ((sscanf(line
,"%s%d",type
,&nrexcl
)) != 2) {
491 /* Fill in the values */
492 newmol
->name
= put_symtab(symtab
,type
);
493 newmol
->nrexcl
= nrexcl
;
494 newmol
->excl_set
= FALSE
;
497 static bool default_params(int ftype
,t_params bt
[],t_atoms
*at
,t_param
*p
,
503 int nr
= bt
[ftype
].nr
;
504 int nral
= NRAL(ftype
);
505 int nrfp
= interaction_function
[ftype
].nrfpA
;
506 int nrfpB
= interaction_function
[ftype
].nrfpB
;
509 for (i
=0; ((i
< nr
) && !bFound
); i
++) {
510 pi
=&(bt
[ftype
].param
[i
]);
511 if ((ftype
== F_PDIHS
) || (ftype
== F_RBDIHS
)) {
512 /* The j and k atoms are decisive about which dihedral type
516 bFound
=((at
->atom
[p
->AJ
].typeB
==pi
->AI
) &&
517 (at
->atom
[p
->AK
].typeB
==pi
->AJ
));
519 bFound
=((at
->atom
[p
->AJ
].type
==pi
->AI
) &&
520 (at
->atom
[p
->AK
].type
==pi
->AJ
));
522 else if (ftype
== F_IDIHS
) {
523 /* The i and l atoms are decisive about which dihedral type
527 bFound
=((at
->atom
[p
->AI
].typeB
==pi
->AI
) &&
528 (at
->atom
[p
->AL
].typeB
==pi
->AJ
));
530 bFound
=((at
->atom
[p
->AI
].type
==pi
->AI
) &&
531 (at
->atom
[p
->AL
].type
==pi
->AJ
));
535 for (j
=0; ((j
< nral
) &&
536 (at
->atom
[p
->a
[j
]].typeB
== pi
->a
[j
])); j
++);
538 for (j
=0; ((j
< nral
) &&
539 (at
->atom
[p
->a
[j
]].type
== pi
->a
[j
])); j
++);
545 assert(nrfp
+nrfpB
<= MAXFORCEPARAM
);
546 for (j
=0; (j
< nrfpB
); j
++)
547 p
->c
[nrfp
+j
] = pi
->c
[j
];
550 for (j
=0; (j
< nrfp
); j
++)
554 for (j
=0; (j
< nrfp
); j
++)
560 void push_bondnow(t_params
*bond
, t_param
*b
)
563 fprintf(debug
,"push_bondnow: nr = %d\n",bond
->nr
);
566 /* allocate one position extra */
569 /* fill the arrays */
570 memcpy ((char *)&(bond
->param
[bond
->nr
]),(char *)b
,(size_t)sizeof(t_param
));
575 void push_bond(directive d
,t_params bondtype
[],t_params bond
[],
576 t_atoms
*at
,char *line
)
578 static char *aaformat
[MAXATOMLIST
]= {
585 static char *asformat
[MAXATOMLIST
]= {
592 static char *ccformat
[MAXFORCEPARAM
]= {
600 int nr
,i
,j
,nrfp
,nrfpA
,nral
,nread
,ftype
;
602 double cc
[MAXFORCEPARAM
];
603 int aa
[MAXATOMLIST
+1];
604 t_param param
,paramB
;
605 bool bFoundA
,bFoundB
,bDef
,bPert
,bSwapParity
=FALSE
;
607 ftype
= ifunc_index(d
,1);
609 for(j
=0; j
<MAXATOMLIST
; j
++)
611 nread
= sscanf(line
,aaformat
[nral
-1],
612 &aa
[0],&aa
[1],&aa
[2],&aa
[3],&aa
[4],&aa
[5]);
616 } else if (nread
== nral
)
617 ftype
= ifunc_index(d
,1);
619 /* this is a hack to allow for dummy atoms with swapped parity */
620 bSwapParity
= (aa
[nral
]<0);
622 aa
[nral
] = -aa
[nral
];
623 ftype
= ifunc_index(d
,aa
[nral
]);
630 fatal_error(0,"Negative function types only allowed for %s and %s",
631 interaction_function
[F_DUMMY3FAD
].longname
,
632 interaction_function
[F_DUMMY3OUT
].longname
);
636 /* Check for double atoms */
637 for(i
=0; (i
<nral
); i
++)
638 for(j
=i
+1; (j
<nral
); j
++)
639 if (aa
[i
] == aa
[j
]) {
640 sprintf(errbuf
,"Double atoms in %s (%d)",dir2str(d
),aa
[i
]);
644 /* default force parameters */
645 for(j
=0; (j
<MAXATOMLIST
); j
++)
646 param
.a
[j
] = aa
[j
]-1;
647 for(j
=0; (j
<MAXFORCEPARAM
); j
++)
650 /* Get force params for normal and free energy perturbation
651 * studies, as determined by types!
653 bFoundA
= default_params(ftype
,bondtype
,at
,¶m
,FALSE
);
654 bFoundB
= default_params(ftype
,bondtype
,at
,¶m
,TRUE
);
659 strcpy(format
,asformat
[nral
-1]);
660 strcat(format
,ccformat
[nrfp
-1]);
662 nread
= sscanf(line
,format
,&cc
[0],&cc
[1],&cc
[2],&cc
[3],&cc
[4],&cc
[5]);
664 warning("Too many parameters");
668 for(j
=0; (j
<nread
); j
++)
671 /* Check whether we have to use the defaults */
676 /* nread now holds the number of force parameters read! */
680 nrfpA
=interaction_function
[ftype
].nrfpA
;
684 if (interaction_function
[ftype
].flags
& IF_DUMMY
) {
685 /* set them to NOTSET, will be calculated later */
686 for(j
=0; (j
<MAXFORCEPARAM
); j
++)
689 param
.C1
= -1; /* flag to swap parity of dummy construction */
691 sprintf(errbuf
,"No default %s types, using zeroes",
692 interaction_function
[ftype
].longname
);
699 param
.C0
= 360-param
.C0
;
702 param
.C2
= -param
.C2
;
706 /* This implies nread < nrfp, and so perturbed values have not
709 /* We only have to issue a warning if these atoms are perturbed! */
711 for(j
=0; (j
<nral
); j
++)
712 bPert
= bPert
|| PERTURBED(at
->atom
[param
.a
[j
]]);
715 sprintf(errbuf
,"No default %s types for perturbed atoms, "
716 "using normal values",interaction_function
[ftype
].longname
);
719 for(j
=nrfpA
; (j
<nrfp
); j
++)
720 param
.c
[j
]=param
.c
[j
-nrfpA
];
724 /* Put the values in the appropriate arrays */
725 push_bondnow (&bond
[ftype
],¶m
);
728 void push_mol(int nrmols
,t_molinfo mols
[],char *pline
,int *whichmol
,
735 if (sscanf(pline
,"%s%d",type
,&copies
) != 2) {
740 /* search moleculename */
741 for (i
=0; ((i
<nrmols
) && strcasecmp(type
,*(mols
[i
].name
))); i
++)
749 sprintf(errbuf
,"No such moleculetype %s",type
);
754 void init_block2(t_block2
*b2
, int natom
)
759 snew(b2
->nra
,b2
->nr
);
761 for(i
=0; (i
<b2
->nr
); i
++)
765 void done_block2(t_block2
*b2
)
770 for(i
=0; (i
<b2
->nr
); i
++)
778 void push_excl(char *line
, t_block2
*b2
)
782 char base
[STRLEN
],format
[STRLEN
];
784 if (sscanf(line
,"%d",&i
)==0)
787 if ((1 <= i
) && (i
<= b2
->nr
))
791 fprintf(debug
,"Unbound atom %d\n",i
-1);
798 n
=sscanf(line
,format
,&j
);
800 if ((1 <= j
) && (j
<= b2
->nr
)) {
802 srenew(b2
->a
[i
],++(b2
->nra
[i
]));
803 b2
->a
[i
][b2
->nra
[i
]-1]=j
;
804 /* also add the reverse exclusion! */
805 srenew(b2
->a
[j
],++(b2
->nra
[j
]));
806 b2
->a
[j
][b2
->nra
[j
]-1]=i
;
810 fatal_error(0,"Invalid Atomnr j: %d, b2->nr: %d\n",j
,b2
->nr
);
815 void b_to_b2(t_block
*b
, t_block2
*b2
)
820 for(i
=0; (i
<b
->nr
); i
++)
821 for(j
=b
->index
[i
]; (j
<b
->index
[i
+1]); j
++) {
823 srenew(b2
->a
[i
],++b2
->nra
[i
]);
824 b2
->a
[i
][b2
->nra
[i
]-1]=a
;
828 void b2_to_b(t_block2
*b2
, t_block
*b
)
834 for(i
=0; (i
<b2
->nr
); i
++) {
836 for(j
=0; (j
<b2
->nra
[i
]); j
++)
837 b
->a
[nra
+j
]=b2
->a
[i
][j
];
844 static int icomp(const void *v1
, const void *v2
)
846 return (*((atom_id
*) v1
))-(*((atom_id
*) v2
));
849 void merge_excl(t_block
*excl
, t_block2
*b2
)
857 else if (b2
->nr
!= excl
->nr
) {
858 fatal_error(0,"DEATH HORROR: b2->nr = %d, while excl->nr = %d",
862 fprintf(debug
,"Entering merge_excl\n");
864 /* First copy all entries from excl to b2 */
867 /* Count and sort the exclusions */
869 for(i
=0; (i
<b2
->nr
); i
++) {
870 if (b2
->nra
[i
] > 0) {
871 /* remove double entries */
872 qsort(b2
->a
[i
],(size_t)b2
->nra
[i
],(size_t)sizeof(b2
->a
[i
][0]),icomp
);
874 for(j
=1; (j
<b2
->nra
[i
]); j
++)
875 if (b2
->a
[i
][j
]!=b2
->a
[i
][k
-1]) {
876 b2
->a
[i
][k
]=b2
->a
[i
][j
];
884 srenew(excl
->a
,excl
->nra
);