2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
43 #include <sys/types.h>
62 #include "gmx_fatal.h"
64 #include "vsite_parm.h"
70 #include "gpp_nextnb.h"
74 #include "gpp_bond_atomtype.h"
78 #define CPPMARK '#' /* mark from cpp */
79 #define OPENDIR '[' /* starting sign for directive */
80 #define CLOSEDIR ']' /* ending sign for directive */
83 printf("line: %d, maxavail: %d\n",__LINE__,maxavail()); \
86 static void free_nbparam(t_nbparam **param,int nr)
95 static int copy_nbparams(t_nbparam **param,int ftype,t_params *plist,int nr)
105 if (param[i][j].bSet) {
106 for(f=0; f<nrfp; f++) {
107 plist->param[nr*i+j].c[f] = param[i][j].c[f];
108 plist->param[nr*j+i].c[f] = param[i][j].c[f];
116 static void gen_pairs(t_params *nbs,t_params *pairs,real fudge, int comb, gmx_bool bVerbose)
118 int i,j,ntp,nrfp,nrfpA,nrfpB,nnn;
123 nrfpA = interaction_function[F_LJ14].nrfpA;
124 nrfpB = interaction_function[F_LJ14].nrfpB;
127 if ((nrfp != nrfpA) || (nrfpA != nrfpB))
128 gmx_incons("Number of force parameters in gen_pairs wrong");
130 fprintf(stderr,"Generating 1-4 interactions: fudge = %g\n",fudge);
132 fprintf(debug,"Fudge factor for 1-4 interactions: %g\n",fudge);
133 fprintf(debug,"Holy Cow! there are %d types\n",ntp);
135 snew(pairs->param,pairs->nr);
136 for(i=0; (i<ntp); i++) {
138 pairs->param[i].a[0] = i / nnn;
139 pairs->param[i].a[1] = i % nnn;
140 /* Copy normal and FEP parameters and multiply by fudge factor */
144 for(j=0; (j<nrfp); j++) {
145 /* If we are using sigma/epsilon values, only the epsilon values
146 * should be scaled, but not sigma.
147 * The sigma values have even indices 0,2, etc.
149 if ((comb == eCOMB_ARITHMETIC || comb == eCOMB_GEOM_SIG_EPS) && (j%2==0))
154 pairs->param[i].c[j]=scaling*nbs->param[i].c[j];
155 pairs->param[i].c[nrfp+j]=scaling*nbs->param[i].c[j];
160 double check_mol(gmx_mtop_t *mtop,warninp_t wi)
168 /* Check mass and charge */
171 for(mb=0; mb<mtop->nmoltype; mb++) {
172 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
173 nmol = mtop->molblock[mb].nmol;
174 for (i=0; (i<atoms->nr); i++) {
175 q += nmol*atoms->atom[i].q;
176 m = atoms->atom[i].m;
177 pt = atoms->atom[i].ptype;
178 /* If the particle is an atom or a nucleus it must have a mass,
179 * else, if it is a shell, a vsite or a bondshell it can have mass zero
181 if ((m <= 0.0) && ((pt == eptAtom) || (pt == eptNucleus))) {
182 ri = atoms->atom[i].resind;
183 sprintf(buf,"atom %s (Res %s-%d) has mass %g\n",
184 *(atoms->atomname[i]),
185 *(atoms->resinfo[ri].name),
186 atoms->resinfo[ri].nr,
188 warning_error(wi,buf);
190 if ((m!=0) && (pt == eptVSite)) {
191 ri = atoms->atom[i].resind;
192 sprintf(buf,"virtual site %s (Res %s-%d) has non-zero mass %g\n"
193 " Check your topology.\n",
194 *(atoms->atomname[i]),
195 *(atoms->resinfo[ri].name),
196 atoms->resinfo[ri].nr,
198 warning_error(wi,buf);
199 /* The following statements make LINCS break! */
200 /* atoms->atom[i].m=0; */
207 static void sum_q(t_atoms *atoms,int n,double *qt,double *qBt)
215 for (i=0; i<atoms->nr; i++)
217 qmolA += atoms->atom[i].q;
218 qmolB += atoms->atom[i].qB;
220 /* Unfortunately an absolute comparison,
221 * but this avoids unnecessary warnings and gmx-users mails.
223 if (fabs(qmolA) >= 1e-6 || fabs(qmolB) >= 1e-6)
230 static void get_nbparm(char *nb_str,char *comb_str,int *nb,int *comb,
234 char warn_buf[STRLEN];
237 for(i=1; (i<eNBF_NR); i++)
238 if (gmx_strcasecmp(nb_str,enbf_names[i]) == 0)
241 *nb = strtol(nb_str,NULL,10);
242 if ((*nb < 1) || (*nb >= eNBF_NR)) {
243 sprintf(warn_buf,"Invalid nonbond function selector '%s' using %s",
244 nb_str,enbf_names[1]);
245 warning_error(wi,warn_buf);
249 for(i=1; (i<eCOMB_NR); i++)
250 if (gmx_strcasecmp(comb_str,ecomb_names[i]) == 0)
253 *comb = strtol(comb_str,NULL,10);
254 if ((*comb < 1) || (*comb >= eCOMB_NR)) {
255 sprintf(warn_buf,"Invalid combination rule selector '%s' using %s",
256 comb_str,ecomb_names[1]);
257 warning_error(wi,warn_buf);
262 static char ** cpp_opts(const char *define,const char *include,
268 const char *cppadds[2];
269 char **cppopts = NULL;
270 const char *option[2] = { "-D","-I" };
271 const char *nopt[2] = { "define", "include" };
275 char warn_buf[STRLEN];
278 cppadds[1] = include;
279 for(n=0; (n<2); n++) {
282 while(*ptr != '\0') {
283 while((*ptr != '\0') && isspace(*ptr))
286 while((*rptr != '\0') && !isspace(*rptr))
291 strncpy(buf,ptr,len);
292 if (strstr(ptr,option[n]) != ptr) {
293 set_warning_line(wi,"mdp file",-1);
294 sprintf(warn_buf,"Malformed %s option %s",nopt[n],buf);
295 warning(wi,warn_buf);
298 srenew(cppopts,++ncppopts);
299 cppopts[ncppopts-1] = strdup(buf);
307 srenew(cppopts,++ncppopts);
308 cppopts[ncppopts-1] = NULL;
315 find_gb_bondlength(t_params *plist,int ai,int aj, real *length)
322 for(i=0;i<F_NRE && !found;i++)
326 for(j=0;j<plist[i].nr; j++)
328 a1=plist[i].param[j].a[0];
329 a2=plist[i].param[j].a[1];
331 if( (a1==ai && a2==aj) || (a1==aj && a2==ai))
333 /* Equilibrium bond distance */
334 *length=plist[i].param[j].c[0];
347 find_gb_anglelength(t_params *plist,int ai,int ak, real *length)
352 int status,status1,status2;
356 for(i=0;i<F_NRE && !found;i++)
360 for(j=0;j<plist[i].nr; j++)
362 a1=plist[i].param[j].a[0];
363 a2=plist[i].param[j].a[1];
364 a3=plist[i].param[j].a[2];
366 /* We dont care what the middle atom is, but use it below */
367 if( (a1==ai && a3==ak) || (a1==ak && a3==ai) )
369 /* Equilibrium bond distance */
370 a123 = plist[i].param[j].c[0];
371 /* Use middle atom to find reference distances r12 and r23 */
372 status1 = find_gb_bondlength(plist,a1,a2,&r12);
373 status2 = find_gb_bondlength(plist,a2,a3,&r23);
375 if(status1==0 && status2==0)
377 /* cosine theorem to get r13 */
378 *length=sqrt(r12*r12+r23*r23-(2*r12*r23*cos(a123/RAD2DEG)));
391 generate_gb_exclusion_interactions(t_molinfo *mi,gpp_atomtype_t atype,t_nextnb *nnb)
393 int i,j,k,n,ai,aj,ti,tj;
399 real radiusi,radiusj;
400 real gb_radiusi,gb_radiusj;
401 real param_c2,param_c4;
407 for(n=1;n<=nnb->nrex;n++)
422 /* Put all higher-order exclusions into 1,4 list so we dont miss them */
429 for(ai=0;ai<nnb->nr;ai++)
431 ti = at->atom[ai].type;
432 radiusi = get_atomtype_radius(ti,atype);
433 gb_radiusi = get_atomtype_gb_radius(ti,atype);
435 for(j=0;j<nnb->nrexcl[ai][n];j++)
437 aj = nnb->a[ai][n][j];
439 /* Only add the interactions once */
442 tj = at->atom[aj].type;
443 radiusj = get_atomtype_radius(tj,atype);
444 gb_radiusj = get_atomtype_gb_radius(tj,atype);
446 /* There is an exclusion of type "ftype" between atoms ai and aj */
450 /* Reference distance, not used for 1-4 interactions */
454 if(find_gb_bondlength(plist,ai,aj,&distance)!=0)
456 gmx_fatal(FARGS,"Cannot find bond length for atoms %d-%d",ai,aj);
460 if(find_gb_anglelength(plist,ai,aj,&distance)!=0)
462 gmx_fatal(FARGS,"Cannot find length for atoms %d-%d involved in angle",ai,aj);
469 /* Assign GB parameters */
471 param.c[0] = radiusi+radiusj;
472 /* Reference distance distance */
473 param.c[1] = distance;
474 /* Still parameter */
475 param.c[2] = param_c2;
477 param.c[3] = gb_radiusi+gb_radiusj;
479 param.c[4] = param_c4;
481 /* Add it to the parameter list */
482 add_param_to_list(&plist[ftype],¶m);
493 static char **read_topol(const char *infile,const char *outfile,
494 const char *define,const char *include,
496 gpp_atomtype_t atype,
500 int *combination_rule,
505 gmx_molblock_t **molblock,
513 int i,sl,nb_funct,comb;
514 char *pline=NULL,**title=NULL;
515 char line[STRLEN],errbuf[256],comb_str[256],nb_str[256];
517 char *dirstr,*dummy2;
518 int nrcopies,nmol,nmolb=0,nscan,ncombs,ncopy;
520 gmx_molblock_t *molb=NULL;
521 t_topology *block=NULL;
525 t_nbparam **nbparam,**pair;
527 real fudgeLJ=-1; /* Multiplication factor to generate 1-4 from LJ */
528 gmx_bool bReadDefaults,bReadMolType,bGenPairs,bWarn_copy_A_B;
529 double qt=0,qBt=0; /* total charge */
530 t_bond_atomtype batype;
532 int dcatt=-1,nmol_couple;
533 /* File handling variables */
537 char warn_buf[STRLEN];
538 const char *floating_point_arithmetic_tip =
539 "Total charge should normally be an integer. See\n"
540 "http://www.gromacs.org/Documentation/Floating_Point_Arithmetic\n"
541 "for discussion on how close it should be to an integer.\n";
542 /* We need to open the output file before opening the input file,
543 * because cpp_open_file can change the current working directory.
546 out = gmx_fio_fopen(outfile,"w");
551 /* open input file */
552 status = cpp_open_file(infile,&handle,cpp_opts(define,include,infile,wi));
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 */
564 *reppow = 12.0; /* Default value for repulsion power */
568 /* Init the number of CMAP torsion angles and grid spacing */
569 plist->grid_spacing = 0;
572 bWarn_copy_A_B = bFEP;
574 batype = init_bond_atomtype();
575 /* parse the actual file */
576 bReadDefaults = FALSE;
578 bReadMolType = FALSE;
582 status = cpp_read_line(&handle,STRLEN,line);
583 done = (status == eCPP_EOF);
585 if (status != eCPP_OK)
586 gmx_fatal(FARGS,cpp_error(&handle,status));
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 */
596 if ((sl > 0) && (pline[sl-1] == CONTINUE)) {
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);
618 if (status != eCPP_OK)
619 gmx_fatal(FARGS,cpp_error(&handle,status));
621 fprintf(out,"%s\n",line);
624 srenew(pline,strlen(pline)+strlen(tmp_line)+1);
625 strcat(pline,tmp_line);
629 /* skip trailing and leading spaces and comment text */
630 strip_comment (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)
645 if ((newd = str2dir(dirstr)) == d_invalid) {
646 sprintf(errbuf,"Invalid directive %s",dirstr);
647 warning_error(wi,errbuf);
650 /* Directive found */
652 fprintf(debug,"found directive '%s'\n",dir2str(newd));
653 if (DS_Check_Order (DS,newd)) {
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));
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!
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);
687 get_nbparm(nb_str,comb_str,&nb_funct,&comb,wi);
688 *combination_rule = comb;
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");
702 nb_funct = ifunc_index(d_nonbond_params,nb_funct);
706 push_at(symtab,atype,batype,pline,nb_funct,
707 &nbparam,bGenPairs ? &pair : NULL,wi);
711 push_bt(d,plist,2,NULL,batype,pline,wi);
713 case d_constrainttypes:
714 push_bt(d,plist,2,NULL,batype,pline,wi);
718 push_nbt(d,pair,atype,pline,F_LJ14,wi);
720 push_bt(d,plist,2,atype,NULL,pline,wi);
723 push_bt(d,plist,3,NULL,batype,pline,wi);
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);
730 case d_nonbond_params:
731 push_nbt(d,nbparam,atype,pline,nb_funct,wi);
736 srenew(block,nblock);
737 srenew(blockinfo,nblock);
738 blk0=&(block[nblock-1]);
739 bi0=&(blockinfo[nblock-1]);
742 push_molt(symtab,bi0,pline);
746 case d_implicit_genborn_params:
747 push_gb_params(atype,pline,wi);
750 case d_implicit_surface_params:
751 gmx_fatal(FARGS,"Implicit surface directive not supported yet.");
755 push_cmaptype(d, plist, 5, atype, batype,pline,wi);
758 case d_moleculetype: {
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]),
775 fprintf(stderr,"Generated %d of the %d non-bonded parameter combinations\n",ncombs-ncopy,ncombs);
776 free_nbparam(nbparam,ntype);
778 gen_pairs(&(plist[nb_funct]),&(plist[F_LJ14]),fudgeLJ,comb,bVerbose);
779 ncopy = copy_nbparams(pair,nb_funct,&(plist[F_LJ14]),
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? */
789 push_molt(symtab,&nmol,molinfo,pline,wi);
792 mi0=&((*molinfo)[nmol-1]);
796 push_atom(symtab,&(mi0->cgs),&(mi0->atoms),atype,pline,&lastcg,wi);
800 push_bond(d,plist,mi0->plist,&(mi0->atoms),atype,pline,FALSE,
801 bGenPairs,*fudgeQQ,bZero,&bWarn_copy_A_B,wi);
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:
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);
825 push_cmap(d,plist,mi0->plist,&(mi0->atoms),atype,pline,
830 push_vsitesn(d,plist,mi0->plist,&(mi0->atoms),atype,pline,wi);
834 if (!block2[nmol-1].nr)
835 init_block2(&(block2[nmol-1]),mi0->atoms.nr);
836 push_excl(pline,&(block2[nmol-1]));
840 title=put_symtab(symtab,pline);
846 push_mol(nmol,*molinfo,pline,&whichmol,&nrcopies,wi);
847 mi0=&((*molinfo)[whichmol]);
848 srenew(molb,nmolb+1);
849 molb[nmolb].type = whichmol;
850 molb[nmolb].nmol = nrcopies;
853 bCouple = (opts->couple_moltype != NULL &&
854 (gmx_strcasecmp("system" ,opts->couple_moltype) == 0 ||
855 gmx_strcasecmp(*(mi0->name),opts->couple_moltype) == 0));
857 nmol_couple += nrcopies;
860 if (mi0->atoms.nr == 0) {
861 gmx_fatal(FARGS,"Molecule type '%s' contains no atoms",
865 "Excluding %d bonded neighbours molecule type '%s'\n",
866 mi0->nrexcl,*mi0->name);
867 sum_q(&mi0->atoms,nrcopies,&qt,&qBt);
868 if (!mi0->bProcessed)
871 generate_excl(mi0->nrexcl,
876 merge_excl(&(mi0->excls),&(block2[whichmol]));
877 done_block2(&(block2[whichmol]));
878 make_shake(mi0->plist,&mi0->atoms,atype,opts->nshake);
882 /* nnb contains information about first,2nd,3rd bonded neighbors.
883 * Use this to generate GB 1-2,1-3,1-4 interactions when necessary.
887 generate_gb_exclusion_interactions(mi0,atype,&nnb);
894 convert_moltype_couple(mi0,dcatt,*fudgeQQ,
895 opts->couple_lam0,opts->couple_lam1,
897 nb_funct,&(plist[nb_funct]));
899 stupid_fill_block(&mi0->mols,mi0->atoms.nr,TRUE);
900 mi0->bProcessed=TRUE;
905 fprintf (stderr,"case: %d\n",d);
914 status = cpp_close_file(&handle);
915 if (status != eCPP_OK)
916 gmx_fatal(FARGS,cpp_error(&handle,status));
920 if (opts->couple_moltype) {
921 if (nmol_couple == 0) {
922 gmx_fatal(FARGS,"Did not find any molecules of type '%s' for coupling",
923 opts->couple_moltype);
925 fprintf(stderr,"Coupling %d copies of molecule type '%s'\n",
926 nmol_couple,opts->couple_moltype);
929 /* this is not very clean, but fixes core dump on empty system name */
931 title=put_symtab(symtab,"");
932 if (fabs(qt) > 1e-4) {
933 sprintf(warn_buf,"System has non-zero total charge: %.6f\n%s\n",qt,floating_point_arithmetic_tip);
934 warning_note(wi,warn_buf);
936 if (fabs(qBt) > 1e-4 && !gmx_within_tol(qBt,qt,1e-6)) {
937 sprintf(warn_buf,"State B has non-zero total charge: %.6f\n%s\n",qBt,floating_point_arithmetic_tip);
938 warning_note(wi,warn_buf);
941 for(i=0; i<nmol; i++)
942 done_block2(&(block2[i]));
945 done_bond_atomtype(&batype);
955 char **do_top(gmx_bool bVerbose,
957 const char *topppfile,
962 int *combination_rule,
963 double *repulsion_power,
965 gpp_atomtype_t atype,
970 gmx_molblock_t **molblock,
974 /* Tmpfile might contain a long path */
989 printf("processing topology...\n");
991 title = read_topol(topfile,tmpfile,opts->define,opts->include,
992 symtab,atype,nrmols,molinfo,
993 plist,combination_rule,repulsion_power,
994 opts,fudgeQQ,nmolblock,molblock,
995 ir->efep!=efepNO,bGenborn,bZero,bVerbose,
997 if ((*combination_rule != eCOMB_GEOMETRIC) &&
998 (ir->vdwtype == evdwUSER))
1000 warning(wi,"Using sigma/epsilon based combination rules with"
1001 " user supplied potential function may produce unwanted"
1009 static void generate_qmexcl_moltype(gmx_moltype_t *molt,unsigned char *grpnr,
1012 /* This routine expects molt->ilist to be of size F_NRE and ordered. */
1014 /* generates the exclusions between the individual QM atoms, as
1015 * these interactions should be handled by the QM subroutines and
1016 * not by the gromacs routines
1019 i,j,l,k=0,jmax,qm_max=0,qm_nr=0,nratoms=0,link_nr=0,link_max=0;
1021 *qm_arr=NULL,*link_arr=NULL,a1,a2,a3,a4,ftype=0;
1027 *bQMMM,*blink,bexcl;
1029 /* First we search and select the QM atoms in an qm_arr array that
1030 * we use to create the exclusions.
1032 * we take the possibility into account that a user has defined more
1033 * than one QM group:
1035 * for that we also need to do this an ugly work-about just in case
1036 * the QM group contains the entire system...
1038 jmax = ir->opts.ngQM;
1040 /* we first search for all the QM atoms and put them in an array
1042 for(j=0;j<jmax;j++){
1043 for(i=0;i<molt->atoms.nr;i++){
1046 srenew(qm_arr,qm_max);
1048 if ((grpnr ? grpnr[i] : 0) == j){
1049 qm_arr[qm_nr++] = i;
1053 /* bQMMM[..] is an array containin TRUE/FALSE for atoms that are
1054 * QM/not QM. We first set all elements to false. Afterwards we use
1055 * the qm_arr to change the elements corresponding to the QM atoms
1058 snew(bQMMM,molt->atoms.nr);
1059 for (i=0;i<molt->atoms.nr;i++)
1061 for (i=0;i<qm_nr;i++)
1062 bQMMM[qm_arr[i]]=TRUE;
1064 /* We remove all bonded interactions (i.e. bonds,
1065 * angles, dihedrals, 1-4's), involving the QM atoms. The way they
1066 * are removed is as follows: if the interaction invloves 2 atoms,
1067 * it is removed if both atoms are QMatoms. If it involves 3 atoms,
1068 * it is removed if at least two of the atoms are QM atoms, if the
1069 * interaction involves 4 atoms, it is removed if there are at least
1070 * 2 QM atoms. Since this routine is called once before any forces
1071 * are computed, the top->idef.il[N].iatom[] array (see idef.h) can
1072 * be rewritten at this poitn without any problem. 25-9-2002 */
1074 /* first check weter we already have CONNBONDS: */
1075 if (molt->ilist[F_CONNBONDS].nr != 0){
1076 fprintf(stderr,"nr. of CONNBONDS present already: %d\n",
1077 molt->ilist[F_CONNBONDS].nr/3);
1078 ftype = molt->ilist[F_CONNBONDS].iatoms[0];
1079 k = molt->ilist[F_CONNBONDS].nr;
1081 /* now we delete all bonded interactions, except the ones describing
1082 * a chemical bond. These are converted to CONNBONDS
1084 for (i=0;i<F_LJ;i++){
1087 nratoms = interaction_function[i].nratoms;
1089 while (j<molt->ilist[i].nr){
1093 a1 = molt->ilist[i].iatoms[j+1];
1094 a2 = molt->ilist[i].iatoms[j+2];
1095 bexcl = (bQMMM[a1] && bQMMM[a2]);
1096 /* a bonded beteen two QM atoms will be copied to the
1097 * CONNBONDS list, for reasons mentioned above
1099 if(bexcl && i<F_ANGLES){
1100 srenew(molt->ilist[F_CONNBONDS].iatoms,k+3);
1101 molt->ilist[F_CONNBONDS].nr += 3;
1102 molt->ilist[F_CONNBONDS].iatoms[k++] = ftype;
1103 molt->ilist[F_CONNBONDS].iatoms[k++] = a1;
1104 molt->ilist[F_CONNBONDS].iatoms[k++] = a2;
1108 a1 = molt->ilist[i].iatoms[j+1];
1109 a2 = molt->ilist[i].iatoms[j+2];
1110 a3 = molt->ilist[i].iatoms[j+3];
1111 bexcl = ((bQMMM[a1] && bQMMM[a2]) ||
1112 (bQMMM[a1] && bQMMM[a3]) ||
1113 (bQMMM[a2] && bQMMM[a3]));
1116 a1 = molt->ilist[i].iatoms[j+1];
1117 a2 = molt->ilist[i].iatoms[j+2];
1118 a3 = molt->ilist[i].iatoms[j+3];
1119 a4 = molt->ilist[i].iatoms[j+4];
1120 bexcl = ((bQMMM[a1] && bQMMM[a2] && bQMMM[a3]) ||
1121 (bQMMM[a1] && bQMMM[a2] && bQMMM[a4]) ||
1122 (bQMMM[a1] && bQMMM[a3] && bQMMM[a4]) ||
1123 (bQMMM[a2] && bQMMM[a3] && bQMMM[a4]));
1126 gmx_fatal(FARGS,"no such bonded interactions with %d atoms\n",nratoms);
1129 /* since the interaction involves QM atoms, these should be
1130 * removed from the MM ilist
1132 molt->ilist[i].nr -= (nratoms+1);
1133 for (l=j;l<molt->ilist[i].nr;l++)
1134 molt->ilist[i].iatoms[l] = molt->ilist[i].iatoms[l+(nratoms+1)];
1136 j += nratoms+1; /* the +1 is for the functype */
1140 /* Now, we search for atoms bonded to a QM atom because we also want
1141 * to exclude their nonbonded interactions with the QM atoms. The
1142 * reason for this is that this interaction is accounted for in the
1143 * linkatoms interaction with the QMatoms and would be counted
1146 for(i=0;i<F_NRE;i++){
1149 while(j<molt->ilist[i].nr){
1150 a1 = molt->ilist[i].iatoms[j+1];
1151 a2 = molt->ilist[i].iatoms[j+2];
1152 if((bQMMM[a1] && !bQMMM[a2])||(!bQMMM[a1] && bQMMM[a2])){
1153 if(link_nr>=link_max){
1155 srenew(link_arr,link_max);
1158 link_arr[link_nr++] = a2;
1160 link_arr[link_nr++] = a1;
1167 snew(blink,molt->atoms.nr);
1168 for (i=0;i<molt->atoms.nr;i++)
1170 for (i=0;i<link_nr;i++)
1171 blink[link_arr[i]]=TRUE;
1172 /* creating the exclusion block for the QM atoms. Each QM atom has
1173 * as excluded elements all the other QMatoms (and itself).
1175 qmexcl.nr = molt->atoms.nr;
1176 qmexcl.nra = qm_nr*(qm_nr+link_nr)+link_nr*qm_nr;
1177 snew(qmexcl.index,qmexcl.nr+1);
1178 snew(qmexcl.a,qmexcl.nra);
1180 for(i=0;i<qmexcl.nr;i++){
1183 for(k=0;k<qm_nr;k++){
1184 qmexcl.a[k+j] = qm_arr[k];
1186 for(k=0;k<link_nr;k++){
1187 qmexcl.a[qm_nr+k+j] = link_arr[k];
1192 for(k=0;k<qm_nr;k++){
1193 qmexcl.a[k+j] = qm_arr[k];
1198 qmexcl.index[qmexcl.nr]=j;
1200 /* and merging with the exclusions already present in sys.
1203 init_block2(&qmexcl2,molt->atoms.nr);
1204 b_to_b2(&qmexcl, &qmexcl2);
1205 merge_excl(&(molt->excls),&qmexcl2);
1206 done_block2(&qmexcl2);
1208 /* Finally, we also need to get rid of the pair interactions of the
1209 * classical atom bonded to the boundary QM atoms with the QMatoms,
1210 * as this interaction is already accounted for by the QM, so also
1211 * here we run the risk of double counting! We proceed in a similar
1212 * way as we did above for the other bonded interactions: */
1213 for (i=F_LJ14;i<F_COUL14;i++){
1214 nratoms = interaction_function[i].nratoms;
1216 while (j<molt->ilist[i].nr){
1218 a1 = molt->ilist[i].iatoms[j+1];
1219 a2 = molt->ilist[i].iatoms[j+2];
1220 bexcl = ((bQMMM[a1] && bQMMM[a2])||
1221 (blink[a1] && bQMMM[a2])||
1222 (bQMMM[a1] && blink[a2]));
1224 /* since the interaction involves QM atoms, these should be
1225 * removed from the MM ilist
1227 molt->ilist[i].nr -= (nratoms+1);
1228 for (k=j;k<molt->ilist[i].nr;k++)
1229 molt->ilist[i].iatoms[k] = molt->ilist[i].iatoms[k+(nratoms+1)];
1231 j += nratoms+1; /* the +1 is for the functype */
1240 } /* generate_qmexcl */
1242 void generate_qmexcl(gmx_mtop_t *sys,t_inputrec *ir,warninp_t wi)
1244 /* This routine expects molt->molt[m].ilist to be of size F_NRE and ordered.
1247 unsigned char *grpnr;
1248 int mb,mol,nat_mol,i,nr_mol_with_qm_atoms=0;
1249 gmx_molblock_t *molb;
1252 grpnr = sys->groups.grpnr[egcQMMM];
1254 for(mb=0; mb<sys->nmolblock; mb++) {
1255 molb = &sys->molblock[mb];
1256 nat_mol = sys->moltype[molb->type].atoms.nr;
1257 for(mol=0; mol<molb->nmol; mol++) {
1259 for(i=0; i<nat_mol; i++) {
1260 if ((grpnr ? grpnr[i] : 0) < ir->opts.ngQM) {
1265 nr_mol_with_qm_atoms++;
1266 if (molb->nmol > 1) {
1267 /* We need to split this molblock */
1269 /* Split the molblock at this molecule */
1271 srenew(sys->molblock,sys->nmolblock);
1272 for(i=sys->nmolblock-2; i >= mb;i--) {
1273 sys->molblock[i+1] = sys->molblock[i];
1275 sys->molblock[mb ].nmol = mol;
1276 sys->molblock[mb+1].nmol -= mol;
1278 molb = &sys->molblock[mb];
1280 if (molb->nmol > 1) {
1281 /* Split the molblock after this molecule */
1283 srenew(sys->molblock,sys->nmolblock);
1284 molb = &sys->molblock[mb];
1285 for(i=sys->nmolblock-2; i >= mb;i--) {
1286 sys->molblock[i+1] = sys->molblock[i];
1288 sys->molblock[mb ].nmol = 1;
1289 sys->molblock[mb+1].nmol -= 1;
1292 /* Add a moltype for the QMMM molecule */
1294 srenew(sys->moltype,sys->nmoltype);
1295 /* Copy the moltype struct */
1296 sys->moltype[sys->nmoltype-1] = sys->moltype[molb->type];
1297 /* Copy the exclusions to a new array, since this is the only
1298 * thing that needs to be modified for QMMM.
1300 copy_blocka(&sys->moltype[molb->type ].excls,
1301 &sys->moltype[sys->nmoltype-1].excls);
1302 /* Set the molecule type for the QMMM molblock */
1303 molb->type = sys->nmoltype - 1;
1305 generate_qmexcl_moltype(&sys->moltype[molb->type],grpnr,ir);
1312 if(nr_mol_with_qm_atoms>1){
1313 /* generate a warning is there are QM atoms in different
1314 * topologies. In this case it is not possible at this stage to
1315 * mutualy exclude the non-bonded interactions via the
1316 * exclusions (AFAIK). Instead, the user is advised to use the
1317 * energy group exclusions in the mdp file
1320 "\nThe QM subsystem is divided over multiple topologies. "
1321 "The mutual non-bonded interactions cannot be excluded. "
1322 "There are two ways to achieve this:\n\n"
1323 "1) merge the topologies, such that the atoms of the QM "
1324 "subsystem are all present in one single topology file. "
1325 "In this case this warning will dissappear\n\n"
1326 "2) exclude the non-bonded interactions explicitly via the "
1327 "energygrp-excl option in the mdp file. if this is the case "
1328 "this warning may be ignored"