improved option compatibility checking with OpenMM
[gromacs/AngularHB.git] / src / kernel / topio.c
blob4e291bd336ed132be27b8955f156110d22d38e55
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 * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
40 #include <math.h>
41 #include <sys/types.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <ctype.h>
46 #include "futil.h"
47 #include "sysstuff.h"
48 #include "typedefs.h"
49 #include "smalloc.h"
50 #include "macros.h"
51 #include "gmxfio.h"
52 #include "txtdump.h"
53 #include "physics.h"
54 #include "macros.h"
55 #include "names.h"
56 #include "string2.h"
57 #include "symtab.h"
58 #include "gmx_fatal.h"
59 #include "warninp.h"
60 #include "vsite_parm.h"
62 #include "grompp.h"
63 #include "toputil.h"
64 #include "toppush.h"
65 #include "topdirs.h"
66 #include "gpp_nextnb.h"
67 #include "topio.h"
68 #include "topshake.h"
69 #include "gmxcpp.h"
70 #include "gpp_bond_atomtype.h"
71 #include "genborn.h"
73 #define CPPMARK '#' /* mark from cpp */
74 #define OPENDIR '[' /* starting sign for directive */
75 #define CLOSEDIR ']' /* ending sign for directive */
77 #define PM() \
78 printf("line: %d, maxavail: %d\n",__LINE__,maxavail()); \
79 fflush(stdout)
81 static void free_nbparam(t_nbparam **param,int nr)
83 int i;
85 for(i=0; i<nr; i++)
86 sfree(param[i]);
87 sfree(param);
90 static int copy_nbparams(t_nbparam **param,int ftype,t_params *plist,int nr)
92 int i,j,f;
93 int nrfp,ncopy;
95 nrfp = NRFP(ftype);
97 ncopy = 0;
98 for(i=0; i<nr; i++)
99 for(j=0; j<=i; j++)
100 if (param[i][j].bSet) {
101 for(f=0; f<nrfp; f++) {
102 plist->param[nr*i+j].c[f] = param[i][j].c[f];
103 plist->param[nr*j+i].c[f] = param[i][j].c[f];
105 ncopy++;
108 return ncopy;
111 static void gen_pairs(t_params *nbs,t_params *pairs,real fudge, int comb, gmx_bool bVerbose)
113 int i,j,ntp,nrfp,nrfpA,nrfpB,nnn;
114 real scaling;
115 ntp = nbs->nr;
116 nnn = sqrt(ntp);
117 nrfp = NRFP(F_LJ);
118 nrfpA = interaction_function[F_LJ14].nrfpA;
119 nrfpB = interaction_function[F_LJ14].nrfpB;
120 pairs->nr = ntp;
122 if ((nrfp != nrfpA) || (nrfpA != nrfpB))
123 gmx_incons("Number of force parameters in gen_pairs wrong");
125 fprintf(stderr,"Generating 1-4 interactions: fudge = %g\n",fudge);
126 if (debug) {
127 fprintf(debug,"Fudge factor for 1-4 interactions: %g\n",fudge);
128 fprintf(debug,"Holy Cow! there are %d types\n",ntp);
130 snew(pairs->param,pairs->nr);
131 for(i=0; (i<ntp); i++) {
132 /* Copy param.a */
133 pairs->param[i].a[0] = i / nnn;
134 pairs->param[i].a[1] = i % nnn;
135 /* Copy normal and FEP parameters and multiply by fudge factor */
139 for(j=0; (j<nrfp); j++) {
140 /* If we are using sigma/epsilon values, only the epsilon values
141 * should be scaled, but not sigma.
142 * The sigma values have even indices 0,2, etc.
144 if ((comb == eCOMB_ARITHMETIC || comb == eCOMB_GEOM_SIG_EPS) && (j%2==0))
145 scaling = 1.0;
146 else
147 scaling = fudge;
149 pairs->param[i].c[j]=scaling*nbs->param[i].c[j];
150 pairs->param[i].c[nrfp+j]=scaling*nbs->param[i].c[j];
155 double check_mol(gmx_mtop_t *mtop,warninp_t wi)
157 char buf[256];
158 int i,mb,nmol,ri,pt;
159 double q;
160 real m;
161 t_atoms *atoms;
163 /* Check mass and charge */
164 q=0.0;
166 for(mb=0; mb<mtop->nmoltype; mb++) {
167 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
168 nmol = mtop->molblock[mb].nmol;
169 for (i=0; (i<atoms->nr); i++) {
170 q += nmol*atoms->atom[i].q;
171 m = atoms->atom[i].m;
172 pt = atoms->atom[i].ptype;
173 /* If the particle is an atom or a nucleus it must have a mass,
174 * else, if it is a shell, a vsite or a bondshell it can have mass zero
176 if ((m <= 0.0) && ((pt == eptAtom) || (pt == eptNucleus))) {
177 ri = atoms->atom[i].resind;
178 sprintf(buf,"atom %s (Res %s-%d) has mass %g\n",
179 *(atoms->atomname[i]),
180 *(atoms->resinfo[ri].name),
181 atoms->resinfo[ri].nr,
183 warning_error(wi,buf);
184 } else
185 if ((m!=0) && (pt == eptVSite)) {
186 ri = atoms->atom[i].resind;
187 sprintf(buf,"virtual site %s (Res %s-%d) has non-zero mass %g\n"
188 " Check your topology.\n",
189 *(atoms->atomname[i]),
190 *(atoms->resinfo[ri].name),
191 atoms->resinfo[ri].nr,
193 warning_error(wi,buf);
194 /* The following statements make LINCS break! */
195 /* atoms->atom[i].m=0; */
199 return q;
202 static void sum_q(t_atoms *atoms,int n,double *qt,double *qBt)
204 double qmolA,qmolB;
205 int i;
207 /* sum charge */
208 qmolA = 0;
209 qmolB = 0;
210 for (i=0; i<atoms->nr; i++)
212 qmolA += atoms->atom[i].q;
213 qmolB += atoms->atom[i].qB;
215 /* Unfortunately an absolute comparison,
216 * but this avoids unnecessary warnings and gmx-users mails.
218 if (fabs(qmolA) >= 1e-6 || fabs(qmolB) >= 1e-6)
220 *qt += n*qmolA;
221 *qBt += n*qmolB;
225 static void get_nbparm(char *nb_str,char *comb_str,int *nb,int *comb,
226 warninp_t wi)
228 int i;
229 char warn_buf[STRLEN];
231 *nb = -1;
232 for(i=1; (i<eNBF_NR); i++)
233 if (gmx_strcasecmp(nb_str,enbf_names[i]) == 0)
234 *nb = i;
235 if (*nb == -1)
236 *nb = strtol(nb_str,NULL,10);
237 if ((*nb < 1) || (*nb >= eNBF_NR)) {
238 sprintf(warn_buf,"Invalid nonbond function selector '%s' using %s",
239 nb_str,enbf_names[1]);
240 warning_error(wi,warn_buf);
241 *nb = 1;
243 *comb = -1;
244 for(i=1; (i<eCOMB_NR); i++)
245 if (gmx_strcasecmp(comb_str,ecomb_names[i]) == 0)
246 *comb = i;
247 if (*comb == -1)
248 *comb = strtol(comb_str,NULL,10);
249 if ((*comb < 1) || (*comb >= eCOMB_NR)) {
250 sprintf(warn_buf,"Invalid combination rule selector '%s' using %s",
251 comb_str,ecomb_names[1]);
252 warning_error(wi,warn_buf);
253 *comb = 1;
257 static char ** cpp_opts(const char *define,const char *include,
258 const char *infile,
259 warninp_t wi)
261 int n,len;
262 int ncppopts=0;
263 const char *cppadds[2];
264 char **cppopts = NULL;
265 const char *option[2] = { "-D","-I" };
266 const char *nopt[2] = { "define", "include" };
267 const char *ptr;
268 const char *rptr;
269 char *buf;
270 char warn_buf[STRLEN];
272 cppadds[0] = define;
273 cppadds[1] = include;
274 for(n=0; (n<2); n++) {
275 if (cppadds[n]) {
276 ptr = cppadds[n];
277 while(*ptr != '\0') {
278 while((*ptr != '\0') && isspace(*ptr))
279 ptr++;
280 rptr = ptr;
281 while((*rptr != '\0') && !isspace(*rptr))
282 rptr++;
283 len = (rptr - ptr);
284 if (len > 2) {
285 snew(buf,(len+1));
286 strncpy(buf,ptr,len);
287 if (strstr(ptr,option[n]) != ptr) {
288 set_warning_line(wi,"mdp file",-1);
289 sprintf(warn_buf,"Malformed %s option %s",nopt[n],buf);
290 warning(wi,warn_buf);
292 else {
293 srenew(cppopts,++ncppopts);
294 cppopts[ncppopts-1] = strdup(buf);
296 sfree(buf);
297 ptr = rptr;
302 if ((rptr=strrchr(infile,DIR_SEPARATOR)) != NULL) {
303 buf = strdup(infile);
304 buf[(int)(rptr-infile)] = '\0';
305 srenew(cppopts,++ncppopts);
306 snew(cppopts[ncppopts-1],strlen(buf)+4);
307 sprintf(cppopts[ncppopts-1],"-I%s",buf);
308 sfree(buf);
310 srenew(cppopts,++ncppopts);
311 cppopts[ncppopts-1] = NULL;
313 return cppopts;
318 find_gb_bondlength(t_params *plist,int ai,int aj, real *length)
320 int i,j,a1,a2;
322 int found=0;
323 int status;
325 for(i=0;i<F_NRE && !found;i++)
327 if(IS_CHEMBOND(i))
329 for(j=0;j<plist[i].nr; j++)
331 a1=plist[i].param[j].a[0];
332 a2=plist[i].param[j].a[1];
334 if( (a1==ai && a2==aj) || (a1==aj && a2==ai))
336 /* Equilibrium bond distance */
337 *length=plist[i].param[j].c[0];
338 found=1;
343 status = !found;
345 return status;
350 find_gb_anglelength(t_params *plist,int ai,int ak, real *length)
352 int i,j,a1,a2,a3;
353 real r12,r23,a123;
354 int found=0;
355 int status,status1,status2;
357 r12 = r23 = 0;
359 for(i=0;i<F_NRE && !found;i++)
361 if(IS_ANGLE(i))
363 for(j=0;j<plist[i].nr; j++)
365 a1=plist[i].param[j].a[0];
366 a2=plist[i].param[j].a[1];
367 a3=plist[i].param[j].a[2];
369 /* We dont care what the middle atom is, but use it below */
370 if( (a1==ai && a3==ak) || (a1==ak && a3==ai) )
372 /* Equilibrium bond distance */
373 a123 = plist[i].param[j].c[0];
374 /* Use middle atom to find reference distances r12 and r23 */
375 status1 = find_gb_bondlength(plist,a1,a2,&r12);
376 status2 = find_gb_bondlength(plist,a2,a3,&r23);
378 if(status1==0 && status2==0)
380 /* cosine theorem to get r13 */
381 *length=sqrt(r12*r12+r23*r23-(2*r12*r23*cos(a123/RAD2DEG)));
382 found=1;
388 status = !found;
390 return status;
393 int
394 generate_gb_exclusion_interactions(t_molinfo *mi,gpp_atomtype_t atype,t_nextnb *nnb)
396 int i,j,k,n,ai,aj,ti,tj;
397 int n12,n13,n14;
398 int ftype;
399 t_param param;
400 t_params * plist;
401 t_atoms * at;
402 real radiusi,radiusj;
403 real gb_radiusi,gb_radiusj;
404 real param_c2,param_c4;
405 real distance;
407 plist = mi->plist;
408 at = &mi->atoms;
410 for(n=1;n<=nnb->nrex;n++)
412 switch(n)
414 case 1:
415 ftype=F_GB12;
416 param_c2 = STILL_P2;
417 param_c4 = 0.8875;
418 break;
419 case 2:
420 ftype=F_GB13;
421 param_c2 = STILL_P3;
422 param_c4 = 0.3516;
423 break;
424 default:
425 /* Put all higher-order exclusions into 1,4 list so we dont miss them */
426 ftype=F_GB14;
427 param_c2 = STILL_P3;
428 param_c4 = 0.3516;
429 break;
432 for(ai=0;ai<nnb->nr;ai++)
434 ti = at->atom[ai].type;
435 radiusi = get_atomtype_radius(ti,atype);
436 gb_radiusi = get_atomtype_gb_radius(ti,atype);
438 for(j=0;j<nnb->nrexcl[ai][n];j++)
440 aj = nnb->a[ai][n][j];
442 /* Only add the interactions once */
443 if(aj>ai)
445 tj = at->atom[aj].type;
446 radiusj = get_atomtype_radius(tj,atype);
447 gb_radiusj = get_atomtype_gb_radius(tj,atype);
449 /* There is an exclusion of type "ftype" between atoms ai and aj */
450 param.a[0] = ai;
451 param.a[1] = aj;
453 /* Reference distance, not used for 1-4 interactions */
454 switch(ftype)
456 case F_GB12:
457 if(find_gb_bondlength(plist,ai,aj,&distance)!=0)
459 gmx_fatal(FARGS,"Cannot find bond length for atoms %d-%d",ai,aj);
461 break;
462 case F_GB13:
463 if(find_gb_anglelength(plist,ai,aj,&distance)!=0)
465 gmx_fatal(FARGS,"Cannot find length for atoms %d-%d involved in angle",ai,aj);
467 break;
468 default:
469 distance=-1;
470 break;
472 /* Assign GB parameters */
473 /* Sum of radii */
474 param.c[0] = radiusi+radiusj;
475 /* Reference distance distance */
476 param.c[1] = distance;
477 /* Still parameter */
478 param.c[2] = param_c2;
479 /* GB radius */
480 param.c[3] = gb_radiusi+gb_radiusj;
481 /* Parameter */
482 param.c[4] = param_c4;
484 /* Add it to the parameter list */
485 add_param_to_list(&plist[ftype],&param);
490 return 0;
496 static char **read_topol(const char *infile,const char *outfile,
497 const char *define,const char *include,
498 t_symtab *symtab,
499 gpp_atomtype_t atype,
500 int *nrmols,
501 t_molinfo **molinfo,
502 t_params plist[],
503 int *combination_rule,
504 double *reppow,
505 t_gromppopts *opts,
506 real *fudgeQQ,
507 int *nmolblock,
508 gmx_molblock_t **molblock,
509 gmx_bool bFEP,
510 gmx_bool bGenborn,
511 gmx_bool bZero,
512 gmx_bool bVerbose,
513 warninp_t wi)
515 FILE *out;
516 int i,sl,nb_funct,comb;
517 char *pline=NULL,**title=NULL;
518 char line[STRLEN],errbuf[256],comb_str[256],nb_str[256];
519 char genpairs[32];
520 char *dirstr,*dummy2;
521 int nrcopies,nmol,nmolb=0,nscan,ncombs,ncopy;
522 double fLJ,fQQ,fPOW;
523 gmx_molblock_t *molb=NULL;
524 t_topology *block=NULL;
525 t_molinfo *mi0=NULL;
526 DirStack *DS;
527 directive d,newd;
528 t_nbparam **nbparam,**pair;
529 t_block2 *block2;
530 real fudgeLJ=-1; /* Multiplication factor to generate 1-4 from LJ */
531 gmx_bool bReadDefaults,bReadMolType,bGenPairs,bWarn_copy_A_B;
532 double qt=0,qBt=0; /* total charge */
533 t_bond_atomtype batype;
534 int lastcg=-1;
535 int dcatt=-1,nmol_couple;
536 /* File handling variables */
537 int status,done;
538 gmx_cpp_t handle;
539 char *tmp_line=NULL;
540 char warn_buf[STRLEN];
542 /* We need to open the output file before opening the input file,
543 * because cpp_open_file can change the current working directory.
545 if (outfile) {
546 out = gmx_fio_fopen(outfile,"w");
547 } else {
548 out = NULL;
551 /* open input file */
552 status = cpp_open_file(infile,&handle,cpp_opts(define,include,infile,wi));
553 if (status != 0)
554 gmx_fatal(FARGS,cpp_error(&handle,status));
556 /* some local variables */
557 DS_Init(&DS); /* directive stack */
558 nmol = 0; /* no molecules yet... */
559 d = d_invalid; /* first thing should be a directive */
560 nbparam = NULL; /* The temporary non-bonded matrix */
561 pair = NULL; /* The temporary pair interaction matrix */
562 block2 = NULL; /* the extra exclusions */
563 nb_funct = F_LJ;
564 *reppow = 12.0; /* Default value for repulsion power */
566 comb = 0;
568 /* Init the number of CMAP torsion angles and grid spacing */
569 plist->grid_spacing = 0;
570 plist->nc = 0;
572 bWarn_copy_A_B = bFEP;
574 batype = init_bond_atomtype();
575 /* parse the actual file */
576 bReadDefaults = FALSE;
577 bGenPairs = FALSE;
578 bReadMolType = FALSE;
579 nmol_couple = 0;
581 do {
582 status = cpp_read_line(&handle,STRLEN,line);
583 done = (status == eCPP_EOF);
584 if (!done) {
585 if (status != eCPP_OK)
586 gmx_fatal(FARGS,cpp_error(&handle,status));
587 else if (out)
588 fprintf(out,"%s\n",line);
590 set_warning_line(wi,cpp_cur_file(&handle),cpp_cur_linenr(&handle));
592 pline = strdup(line);
594 /* Strip trailing '\' from pline, if it exists */
595 sl = strlen(pline);
596 if ((sl > 0) && (pline[sl-1] == CONTINUE)) {
597 pline[sl-1] = ' ';
600 /* build one long line from several fragments - necessary for CMAP */
601 while (continuing(line))
603 status = cpp_read_line(&handle,STRLEN,line);
604 set_warning_line(wi,cpp_cur_file(&handle),cpp_cur_linenr(&handle));
606 /* Since we depend on the '\' being present to continue to read, we copy line
607 * to a tmp string, strip the '\' from that string, and cat it to pline
609 tmp_line=strdup(line);
611 sl = strlen(tmp_line);
612 if ((sl > 0) && (tmp_line[sl-1] == CONTINUE)) {
613 tmp_line[sl-1] = ' ';
616 done = (status == eCPP_EOF);
617 if (!done) {
618 if (status != eCPP_OK)
619 gmx_fatal(FARGS,cpp_error(&handle,status));
620 else if (out)
621 fprintf(out,"%s\n",line);
624 srenew(pline,strlen(pline)+strlen(tmp_line)+1);
625 strcat(pline,tmp_line);
626 sfree(tmp_line);
629 /* skip trailing and leading spaces and comment text */
630 strip_comment (pline);
631 trim (pline);
633 /* if there is something left... */
634 if ((int)strlen(pline) > 0) {
635 if (pline[0] == OPENDIR) {
636 /* A directive on this line: copy the directive
637 * without the brackets into dirstr, then
638 * skip spaces and tabs on either side of directive
640 dirstr = strdup((pline+1));
641 if ((dummy2 = strchr (dirstr,CLOSEDIR)) != NULL)
642 (*dummy2) = 0;
643 trim (dirstr);
645 if ((newd = str2dir(dirstr)) == d_invalid) {
646 sprintf(errbuf,"Invalid directive %s",dirstr);
647 warning_error(wi,errbuf);
649 else {
650 /* Directive found */
651 if (debug)
652 fprintf(debug,"found directive '%s'\n",dir2str(newd));
653 if (DS_Check_Order (DS,newd)) {
654 DS_Push (&DS,newd);
655 d = newd;
657 else {
658 /* we should print here which directives should have
659 been present, and which actually are */
660 gmx_fatal(FARGS,"%s\nInvalid order for directive %s",
661 cpp_error(&handle,eCPP_SYNTAX),dir2str(newd));
662 /* d = d_invalid; */
665 sfree(dirstr);
667 else if (d != d_invalid) {
668 /* Not a directive, just a plain string
669 * use a gigantic switch to decode,
670 * if there is a valid directive!
672 switch (d) {
673 case d_defaults:
674 if (bReadDefaults)
675 gmx_fatal(FARGS,"%s\nFound a second defaults directive.\n",
676 cpp_error(&handle,eCPP_SYNTAX));
677 bReadDefaults = TRUE;
678 nscan = sscanf(pline,"%s%s%s%lf%lf%lf",
679 nb_str,comb_str,genpairs,&fLJ,&fQQ,&fPOW);
680 if (nscan < 2)
681 too_few(wi);
682 else {
683 bGenPairs = FALSE;
684 fudgeLJ = 1.0;
685 *fudgeQQ = 1.0;
687 get_nbparm(nb_str,comb_str,&nb_funct,&comb,wi);
688 *combination_rule = comb;
689 if (nscan >= 3) {
690 bGenPairs = (gmx_strncasecmp(genpairs,"Y",1) == 0);
691 if (nb_funct != eNBF_LJ && bGenPairs) {
692 gmx_fatal(FARGS,"Generating pair parameters is only supported with LJ non-bonded interactions");
695 if (nscan >= 4)
696 fudgeLJ = fLJ;
697 if (nscan >= 5)
698 *fudgeQQ = fQQ;
699 if (nscan >= 6)
700 *reppow = fPOW;
702 nb_funct = ifunc_index(d_nonbond_params,nb_funct);
704 break;
705 case d_atomtypes:
706 push_at(symtab,atype,batype,pline,nb_funct,
707 &nbparam,bGenPairs ? &pair : NULL,wi);
708 break;
710 case d_bondtypes:
711 push_bt(d,plist,2,NULL,batype,pline,wi);
712 break;
713 case d_constrainttypes:
714 push_bt(d,plist,2,NULL,batype,pline,wi);
715 break;
716 case d_pairtypes:
717 if (bGenPairs)
718 push_nbt(d,pair,atype,pline,F_LJ14,wi);
719 else
720 push_bt(d,plist,2,atype,NULL,pline,wi);
721 break;
722 case d_angletypes:
723 push_bt(d,plist,3,NULL,batype,pline,wi);
724 break;
725 case d_dihedraltypes:
726 /* Special routine that can read both 2 and 4 atom dihedral definitions. */
727 push_dihedraltype(d,plist,batype,pline,wi);
728 break;
730 case d_nonbond_params:
731 push_nbt(d,nbparam,atype,pline,nb_funct,wi);
732 break;
734 case d_blocktype:
735 nblock++;
736 srenew(block,nblock);
737 srenew(blockinfo,nblock);
738 blk0=&(block[nblock-1]);
739 bi0=&(blockinfo[nblock-1]);
740 init_top(blk0);
741 init_molinfo(bi0);
742 push_molt(symtab,bi0,pline);
743 break;
746 case d_implicit_genborn_params:
747 push_gb_params(atype,pline,wi);
748 break;
750 case d_implicit_surface_params:
751 gmx_fatal(FARGS,"Implicit surface directive not supported yet.");
752 break;
754 case d_cmaptypes:
755 push_cmaptype(d, plist, 5, atype, batype,pline,wi);
756 break;
758 case d_moleculetype: {
759 if (!bReadMolType) {
760 int ntype;
761 if (opts->couple_moltype != NULL &&
762 (opts->couple_lam0 == ecouplamNONE ||
763 opts->couple_lam0 == ecouplamQ ||
764 opts->couple_lam1 == ecouplamNONE ||
765 opts->couple_lam1 == ecouplamQ))
767 dcatt = add_atomtype_decoupled(symtab,atype,
768 &nbparam,bGenPairs?&pair:NULL);
770 ntype = get_atomtype_ntypes(atype);
771 ncombs = (ntype*(ntype+1))/2;
772 generate_nbparams(comb,nb_funct,&(plist[nb_funct]),atype,wi);
773 ncopy = copy_nbparams(nbparam,nb_funct,&(plist[nb_funct]),
774 ntype);
775 fprintf(stderr,"Generated %d of the %d non-bonded parameter combinations\n",ncombs-ncopy,ncombs);
776 free_nbparam(nbparam,ntype);
777 if (bGenPairs) {
778 gen_pairs(&(plist[nb_funct]),&(plist[F_LJ14]),fudgeLJ,comb,bVerbose);
779 ncopy = copy_nbparams(pair,nb_funct,&(plist[F_LJ14]),
780 ntype);
781 fprintf(stderr,"Generated %d of the %d 1-4 parameter combinations\n",ncombs-ncopy,ncombs);
782 free_nbparam(pair,ntype);
784 /* Copy GBSA parameters to atomtype array? */
786 bReadMolType = TRUE;
789 push_molt(symtab,&nmol,molinfo,pline,wi);
790 srenew(block2,nmol);
791 block2[nmol-1].nr=0;
792 mi0=&((*molinfo)[nmol-1]);
793 break;
795 case d_atoms:
796 push_atom(symtab,&(mi0->cgs),&(mi0->atoms),atype,pline,&lastcg,wi);
797 break;
799 case d_pairs:
800 push_bond(d,plist,mi0->plist,&(mi0->atoms),atype,pline,FALSE,
801 bGenPairs,*fudgeQQ,bZero,&bWarn_copy_A_B,wi);
802 break;
804 case d_vsites2:
805 case d_vsites3:
806 case d_vsites4:
807 case d_bonds:
808 case d_angles:
809 case d_constraints:
810 case d_settles:
811 case d_position_restraints:
812 case d_angle_restraints:
813 case d_angle_restraints_z:
814 case d_distance_restraints:
815 case d_orientation_restraints:
816 case d_dihedral_restraints:
817 case d_dihedrals:
818 case d_polarization:
819 case d_water_polarization:
820 case d_thole_polarization:
821 push_bond(d,plist,mi0->plist,&(mi0->atoms),atype,pline,TRUE,
822 bGenPairs,*fudgeQQ,bZero,&bWarn_copy_A_B,wi);
823 break;
824 case d_cmap:
825 push_cmap(d,plist,mi0->plist,&(mi0->atoms),atype,pline,
826 &bWarn_copy_A_B,wi);
827 break;
829 case d_vsitesn:
830 push_vsitesn(d,plist,mi0->plist,&(mi0->atoms),atype,pline,wi);
831 break;
832 case d_exclusions:
833 if (!block2[nmol-1].nr)
834 init_block2(&(block2[nmol-1]),mi0->atoms.nr);
835 push_excl(pline,&(block2[nmol-1]));
836 break;
837 case d_system:
838 trim(pline);
839 title=put_symtab(symtab,pline);
840 break;
841 case d_molecules: {
842 int whichmol;
843 gmx_bool bCouple;
845 push_mol(nmol,*molinfo,pline,&whichmol,&nrcopies,wi);
846 mi0=&((*molinfo)[whichmol]);
847 srenew(molb,nmolb+1);
848 molb[nmolb].type = whichmol;
849 molb[nmolb].nmol = nrcopies;
850 nmolb++;
852 bCouple = (opts->couple_moltype != NULL &&
853 (gmx_strcasecmp("system" ,opts->couple_moltype) == 0 ||
854 gmx_strcasecmp(*(mi0->name),opts->couple_moltype) == 0));
855 if (bCouple) {
856 nmol_couple += nrcopies;
859 if (mi0->atoms.nr == 0) {
860 gmx_fatal(FARGS,"Molecule type '%s' contains no atoms",
861 *mi0->name);
863 fprintf(stderr,
864 "Excluding %d bonded neighbours molecule type '%s'\n",
865 mi0->nrexcl,*mi0->name);
866 sum_q(&mi0->atoms,nrcopies,&qt,&qBt);
867 if (!mi0->bProcessed)
869 t_nextnb nnb;
870 generate_excl(mi0->nrexcl,
871 mi0->atoms.nr,
872 mi0->plist,
873 &nnb,
874 &(mi0->excls));
875 merge_excl(&(mi0->excls),&(block2[whichmol]));
876 done_block2(&(block2[whichmol]));
877 make_shake(mi0->plist,&mi0->atoms,atype,opts->nshake);
881 /* nnb contains information about first,2nd,3rd bonded neighbors.
882 * Use this to generate GB 1-2,1-3,1-4 interactions when necessary.
884 if (bGenborn==TRUE)
886 generate_gb_exclusion_interactions(mi0,atype,&nnb);
889 done_nnb(&nnb);
891 if (bCouple)
893 convert_moltype_couple(mi0,dcatt,*fudgeQQ,
894 opts->couple_lam0,opts->couple_lam1,
895 opts->bCoupleIntra,
896 nb_funct,&(plist[nb_funct]));
898 stupid_fill_block(&mi0->mols,mi0->atoms.nr,TRUE);
899 mi0->bProcessed=TRUE;
901 break;
903 default:
904 fprintf (stderr,"case: %d\n",d);
905 invalid_case();
909 sfree(pline);
910 pline=NULL;
912 } while (!done);
913 status = cpp_close_file(&handle);
914 if (status != eCPP_OK)
915 gmx_fatal(FARGS,cpp_error(&handle,status));
916 if (out)
917 gmx_fio_fclose(out);
919 if (opts->couple_moltype) {
920 if (nmol_couple == 0) {
921 gmx_fatal(FARGS,"Did not find any molecules of type '%s' for coupling",
922 opts->couple_moltype);
924 fprintf(stderr,"Coupling %d copies of molecule type '%s'\n",
925 nmol_couple,opts->couple_moltype);
928 /* this is not very clean, but fixes core dump on empty system name */
929 if(!title)
930 title=put_symtab(symtab,"");
931 if (fabs(qt) > 1e-4) {
932 sprintf(warn_buf,"System has non-zero total charge: %e\n\n",qt);
933 warning_note(wi,warn_buf);
935 if (fabs(qBt) > 1e-4 && qBt != qt) {
936 sprintf(warn_buf,"State B has non-zero total charge: %e\n\n",qBt);
937 warning_note(wi,warn_buf);
939 DS_Done (&DS);
940 for(i=0; i<nmol; i++)
941 done_block2(&(block2[i]));
942 free(block2);
944 done_bond_atomtype(&batype);
946 *nrmols=nmol;
948 *nmolblock = nmolb;
949 *molblock = molb;
951 return title;
954 char **do_top(gmx_bool bVerbose,
955 const char *topfile,
956 const char *topppfile,
957 t_gromppopts *opts,
958 gmx_bool bZero,
959 t_symtab *symtab,
960 t_params plist[],
961 int *combination_rule,
962 double *repulsion_power,
963 real *fudgeQQ,
964 gpp_atomtype_t atype,
965 int *nrmols,
966 t_molinfo **molinfo,
967 t_inputrec *ir,
968 int *nmolblock,
969 gmx_molblock_t **molblock,
970 gmx_bool bGenborn,
971 warninp_t wi)
973 /* Tmpfile might contain a long path */
974 const char *tmpfile;
975 char **title;
977 if (topppfile)
979 tmpfile = topppfile;
981 else
983 tmpfile = NULL;
986 if (bVerbose)
988 printf("processing topology...\n");
990 title = read_topol(topfile,tmpfile,opts->define,opts->include,
991 symtab,atype,nrmols,molinfo,
992 plist,combination_rule,repulsion_power,
993 opts,fudgeQQ,nmolblock,molblock,
994 ir->efep!=efepNO,bGenborn,bZero,bVerbose,
995 wi);
996 if ((*combination_rule != eCOMB_GEOMETRIC) &&
997 (ir->vdwtype == evdwUSER))
999 warning(wi,"Using sigma/epsilon based combination rules with"
1000 " user supplied potential function may produce unwanted"
1001 " results");
1004 return title;
1008 static void generate_qmexcl_moltype(gmx_moltype_t *molt,unsigned char *grpnr,
1009 t_inputrec *ir)
1011 /* This routine expects molt->ilist to be of size F_NRE and ordered. */
1013 /* generates the exclusions between the individual QM atoms, as
1014 * these interactions should be handled by the QM subroutines and
1015 * not by the gromacs routines
1018 i,j,l,k=0,jmax,qm_max=0,qm_nr=0,nratoms=0,link_nr=0,link_max=0;
1019 atom_id
1020 *qm_arr=NULL,*link_arr=NULL,a1,a2,a3,a4,ftype=0;
1021 t_blocka
1022 qmexcl;
1023 t_block2
1024 qmexcl2;
1025 gmx_bool
1026 *bQMMM,*blink,bexcl;
1028 /* First we search and select the QM atoms in an qm_arr array that
1029 * we use to create the exclusions.
1031 * we take the possibility into account that a user has defined more
1032 * than one QM group:
1034 * for that we also need to do this an ugly work-about just in case
1035 * the QM group contains the entire system...
1037 fprintf(stderr,"excluding classical QM-QM interactions...\n");
1039 jmax = ir->opts.ngQM;
1041 /* we first search for all the QM atoms and put them in an array
1043 for(j=0;j<jmax;j++){
1044 for(i=0;i<molt->atoms.nr;i++){
1045 if(qm_nr>=qm_max){
1046 qm_max += 100;
1047 srenew(qm_arr,qm_max);
1049 if ((grpnr ? grpnr[i] : 0) == j){
1050 qm_arr[qm_nr++] = i;
1054 /* bQMMM[..] is an array containin TRUE/FALSE for atoms that are
1055 * QM/not QM. We first set all elements to false. Afterwards we use
1056 * the qm_arr to change the elements corresponding to the QM atoms
1057 * to TRUE.
1059 snew(bQMMM,molt->atoms.nr);
1060 for (i=0;i<molt->atoms.nr;i++)
1061 bQMMM[i]=FALSE;
1062 for (i=0;i<qm_nr;i++)
1063 bQMMM[qm_arr[i]]=TRUE;
1065 /* We remove all bonded interactions (i.e. bonds,
1066 * angles, dihedrals, 1-4's), involving the QM atoms. The way they
1067 * are removed is as follows: if the interaction invloves 2 atoms,
1068 * it is removed if both atoms are QMatoms. If it involves 3 atoms,
1069 * it is removed if at least two of the atoms are QM atoms, if the
1070 * interaction involves 4 atoms, it is removed if there are at least
1071 * 2 QM atoms. Since this routine is called once before any forces
1072 * are computed, the top->idef.il[N].iatom[] array (see idef.h) can
1073 * be rewritten at this poitn without any problem. 25-9-2002 */
1075 /* first check weter we already have CONNBONDS: */
1076 if (molt->ilist[F_CONNBONDS].nr != 0){
1077 fprintf(stderr,"nr. of CONNBONDS present already: %d\n",
1078 molt->ilist[F_CONNBONDS].nr/3);
1079 ftype = molt->ilist[F_CONNBONDS].iatoms[0];
1080 k = molt->ilist[F_CONNBONDS].nr;
1082 /* now we delete all bonded interactions, except the ones describing
1083 * a chemical bond. These are converted to CONNBONDS
1085 for (i=0;i<F_LJ;i++){
1086 if(i==F_CONNBONDS)
1087 continue;
1088 nratoms = interaction_function[i].nratoms;
1089 j = 0;
1090 while (j<molt->ilist[i].nr){
1091 bexcl = FALSE;
1092 switch (nratoms){
1093 case 2:
1094 a1 = molt->ilist[i].iatoms[j+1];
1095 a2 = molt->ilist[i].iatoms[j+2];
1096 bexcl = (bQMMM[a1] && bQMMM[a2]);
1097 /* a bonded beteen two QM atoms will be copied to the
1098 * CONNBONDS list, for reasons mentioned above
1100 if(bexcl && i<F_ANGLES){
1101 srenew(molt->ilist[F_CONNBONDS].iatoms,k+3);
1102 molt->ilist[F_CONNBONDS].nr += 3;
1103 molt->ilist[F_CONNBONDS].iatoms[k++] = ftype;
1104 molt->ilist[F_CONNBONDS].iatoms[k++] = a1;
1105 molt->ilist[F_CONNBONDS].iatoms[k++] = a2;
1107 break;
1108 case 3:
1109 a1 = molt->ilist[i].iatoms[j+1];
1110 a2 = molt->ilist[i].iatoms[j+2];
1111 a3 = molt->ilist[i].iatoms[j+3];
1112 bexcl = ((bQMMM[a1] && bQMMM[a2]) ||
1113 (bQMMM[a1] && bQMMM[a3]) ||
1114 (bQMMM[a2] && bQMMM[a3]));
1115 break;
1116 case 4:
1117 a1 = molt->ilist[i].iatoms[j+1];
1118 a2 = molt->ilist[i].iatoms[j+2];
1119 a3 = molt->ilist[i].iatoms[j+3];
1120 a4 = molt->ilist[i].iatoms[j+4];
1121 bexcl = ((bQMMM[a1] && bQMMM[a2] && bQMMM[a3]) ||
1122 (bQMMM[a1] && bQMMM[a2] && bQMMM[a4]) ||
1123 (bQMMM[a1] && bQMMM[a3] && bQMMM[a4]) ||
1124 (bQMMM[a2] && bQMMM[a3] && bQMMM[a4]));
1125 break;
1126 default:
1127 gmx_fatal(FARGS,"no such bonded interactions with %d atoms\n",nratoms);
1129 if (bexcl){
1130 /* since the interaction involves QM atoms, these should be
1131 * removed from the MM ilist
1133 molt->ilist[i].nr -= (nratoms+1);
1134 for (l=j;l<molt->ilist[i].nr;l++)
1135 molt->ilist[i].iatoms[l] = molt->ilist[i].iatoms[l+(nratoms+1)];
1136 } else {
1137 j += nratoms+1; /* the +1 is for the functype */
1141 /* Now, we search for atoms bonded to a QM atom because we also want
1142 * to exclude their nonbonded interactions with the QM atoms. The
1143 * reason for this is that this interaction is accounted for in the
1144 * linkatoms interaction with the QMatoms and would be counted
1145 * twice. */
1147 for(i=0;i<F_NRE;i++){
1148 if(IS_CHEMBOND(i)){
1149 j=0;
1150 while(j<molt->ilist[i].nr){
1151 a1 = molt->ilist[i].iatoms[j+1];
1152 a2 = molt->ilist[i].iatoms[j+2];
1153 if((bQMMM[a1] && !bQMMM[a2])||(!bQMMM[a1] && bQMMM[a2])){
1154 if(link_nr>=link_max){
1155 link_max += 10;
1156 srenew(link_arr,link_max);
1158 if(bQMMM[a1]){
1159 link_arr[link_nr++] = a2;
1160 } else {
1161 link_arr[link_nr++] = a1;
1164 j+=3;
1168 snew(blink,molt->atoms.nr);
1169 for (i=0;i<molt->atoms.nr;i++)
1170 blink[i]=FALSE;
1171 for (i=0;i<link_nr;i++)
1172 blink[link_arr[i]]=TRUE;
1173 /* creating the exclusion block for the QM atoms. Each QM atom has
1174 * as excluded elements all the other QMatoms (and itself).
1176 qmexcl.nr = molt->atoms.nr;
1177 qmexcl.nra = qm_nr*(qm_nr+link_nr)+link_nr*qm_nr;
1178 snew(qmexcl.index,qmexcl.nr+1);
1179 snew(qmexcl.a,qmexcl.nra);
1180 j=0;
1181 for(i=0;i<qmexcl.nr;i++){
1182 qmexcl.index[i]=j;
1183 if(bQMMM[i]){
1184 for(k=0;k<qm_nr;k++){
1185 qmexcl.a[k+j] = qm_arr[k];
1187 for(k=0;k<link_nr;k++){
1188 qmexcl.a[qm_nr+k+j] = link_arr[k];
1190 j+=(qm_nr+link_nr);
1192 if(blink[i]){
1193 for(k=0;k<qm_nr;k++){
1194 qmexcl.a[k+j] = qm_arr[k];
1196 j+=qm_nr;
1199 qmexcl.index[qmexcl.nr]=j;
1201 /* and merging with the exclusions already present in sys.
1204 init_block2(&qmexcl2,molt->atoms.nr);
1205 b_to_b2(&qmexcl, &qmexcl2);
1206 merge_excl(&(molt->excls),&qmexcl2);
1207 done_block2(&qmexcl2);
1209 /* Finally, we also need to get rid of the pair interactions of the
1210 * classical atom bonded to the boundary QM atoms with the QMatoms,
1211 * as this interaction is already accounted for by the QM, so also
1212 * here we run the risk of double counting! We proceed in a similar
1213 * way as we did above for the other bonded interactions: */
1214 for (i=F_LJ14;i<F_COUL14;i++){
1215 nratoms = interaction_function[i].nratoms;
1216 j = 0;
1217 while (j<molt->ilist[i].nr){
1218 bexcl = FALSE;
1219 a1 = molt->ilist[i].iatoms[j+1];
1220 a2 = molt->ilist[i].iatoms[j+2];
1221 bexcl = ((bQMMM[a1] && bQMMM[a2])||
1222 (blink[a1] && bQMMM[a2])||
1223 (bQMMM[a1] && blink[a2]));
1224 if (bexcl){
1225 /* since the interaction involves QM atoms, these should be
1226 * removed from the MM ilist
1228 molt->ilist[i].nr -= (nratoms+1);
1229 for (k=j;k<molt->ilist[i].nr;k++)
1230 molt->ilist[i].iatoms[k] = molt->ilist[i].iatoms[k+(nratoms+1)];
1231 } else {
1232 j += nratoms+1; /* the +1 is for the functype */
1237 free(qm_arr);
1238 free(bQMMM);
1239 free(link_arr);
1240 free(blink);
1241 } /* generate_qmexcl */
1243 void generate_qmexcl(gmx_mtop_t *sys,t_inputrec *ir)
1245 /* This routine expects molt->molt[m].ilist to be of size F_NRE and ordered.
1248 unsigned char *grpnr;
1249 int mb,mol,nat_mol,i;
1250 gmx_molblock_t *molb;
1251 gmx_bool bQMMM;
1253 grpnr = sys->groups.grpnr[egcQMMM];
1255 for(mb=0; mb<sys->nmolblock; mb++) {
1256 molb = &sys->molblock[mb];
1257 nat_mol = sys->moltype[molb->type].atoms.nr;
1258 for(mol=0; mol<molb->nmol; mol++) {
1259 bQMMM = FALSE;
1260 for(i=0; i<nat_mol; i++) {
1261 if ((grpnr ? grpnr[i] : 0) < ir->opts.ngQM) {
1262 bQMMM = TRUE;
1265 if (bQMMM) {
1266 if (molb->nmol > 1) {
1267 /* We need to split this molblock */
1268 if (mol > 0) {
1269 /* Split the molblock at this molecule */
1270 sys->nmolblock++;
1271 srenew(sys->molblock,sys->nmolblock);
1272 for(i=mb; i<sys->nmolblock-1; i++) {
1273 sys->molblock[i+1] = sys->molblock[i];
1275 sys->molblock[mb ].nmol = mol;
1276 sys->molblock[mb+1].nmol -= mol;
1277 mb++;
1278 molb = &sys->molblock[mb];
1280 if (molb->nmol > 1) {
1281 /* Split the molblock after this molecule */
1282 sys->nmolblock++;
1283 srenew(sys->molblock,sys->nmolblock);
1284 for(i=mb; i<sys->nmolblock-1; i++) {
1285 sys->molblock[i+1] = sys->molblock[i];
1287 sys->molblock[mb ].nmol = 1;
1288 sys->molblock[mb+1].nmol -= 1;
1291 /* Add a moltype for the QMMM molecule */
1292 sys->nmoltype++;
1293 srenew(sys->moltype,sys->nmoltype);
1294 /* Copy the moltype struct */
1295 sys->moltype[sys->nmoltype-1] = sys->moltype[molb->type];
1296 /* Copy the exclusions to a new array, since this is the only
1297 * thing that needs to be modified for QMMM.
1299 copy_blocka(&sys->moltype[molb->type ].excls,
1300 &sys->moltype[sys->nmoltype-1].excls);
1301 /* Set the molecule type for the QMMM molblock */
1302 molb->type = sys->nmoltype - 1;
1305 generate_qmexcl_moltype(&sys->moltype[molb->type],grpnr,ir);
1307 if (grpnr) {
1308 grpnr += nat_mol;