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 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
49 #include <sys/types.h>
51 #include "gromacs/fileio/gmxfio.h"
52 #include "gromacs/gmxpreprocess/gmxcpp.h"
53 #include "gromacs/gmxpreprocess/gpp_bond_atomtype.h"
54 #include "gromacs/gmxpreprocess/gpp_nextnb.h"
55 #include "gromacs/gmxpreprocess/grompp-impl.h"
56 #include "gromacs/gmxpreprocess/topdirs.h"
57 #include "gromacs/gmxpreprocess/toppush.h"
58 #include "gromacs/gmxpreprocess/topshake.h"
59 #include "gromacs/gmxpreprocess/toputil.h"
60 #include "gromacs/gmxpreprocess/vsite_parm.h"
61 #include "gromacs/legacyheaders/genborn.h"
62 #include "gromacs/legacyheaders/macros.h"
63 #include "gromacs/legacyheaders/names.h"
64 #include "gromacs/legacyheaders/txtdump.h"
65 #include "gromacs/legacyheaders/typedefs.h"
66 #include "gromacs/legacyheaders/warninp.h"
67 #include "gromacs/math/units.h"
68 #include "gromacs/math/utilities.h"
69 #include "gromacs/topology/block.h"
70 #include "gromacs/topology/symtab.h"
71 #include "gromacs/topology/topology.h"
72 #include "gromacs/utility/cstringutil.h"
73 #include "gromacs/utility/fatalerror.h"
74 #include "gromacs/utility/futil.h"
75 #include "gromacs/utility/smalloc.h"
77 #define OPENDIR '[' /* starting sign for directive */
78 #define CLOSEDIR ']' /* ending sign for directive */
80 static void free_nbparam(t_nbparam **param, int nr)
84 for (i = 0; i < nr; i++)
91 static int copy_nbparams(t_nbparam **param, int ftype, t_params *plist, int nr)
99 for (i = 0; i < nr; i++)
101 for (j = 0; j <= i; j++)
103 if (param[i][j].bSet)
105 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];
118 static void gen_pairs(t_params *nbs, t_params *pairs, real fudge, int comb)
120 int i, j, ntp, nrfp, nrfpA, nrfpB, nnn;
125 nrfpA = interaction_function[F_LJ14].nrfpA;
126 nrfpB = interaction_function[F_LJ14].nrfpB;
129 if ((nrfp != nrfpA) || (nrfpA != nrfpB))
131 gmx_incons("Number of force parameters in gen_pairs wrong");
134 fprintf(stderr, "Generating 1-4 interactions: fudge = %g\n", fudge);
137 fprintf(debug, "Fudge factor for 1-4 interactions: %g\n", fudge);
138 fprintf(debug, "Holy Cow! there are %d types\n", ntp);
140 snew(pairs->param, pairs->nr);
141 for (i = 0; (i < ntp); i++)
144 pairs->param[i].a[0] = i / nnn;
145 pairs->param[i].a[1] = i % nnn;
146 /* Copy normal and FEP parameters and multiply by fudge factor */
150 for (j = 0; (j < nrfp); j++)
152 /* If we are using sigma/epsilon values, only the epsilon values
153 * should be scaled, but not sigma.
154 * The sigma values have even indices 0,2, etc.
156 if ((comb == eCOMB_ARITHMETIC || comb == eCOMB_GEOM_SIG_EPS) && (j%2 == 0))
165 pairs->param[i].c[j] = scaling*nbs->param[i].c[j];
166 pairs->param[i].c[nrfp+j] = scaling*nbs->param[i].c[j];
171 double check_mol(gmx_mtop_t *mtop, warninp_t wi)
174 int i, mb, nmol, ri, pt;
179 /* Check mass and charge */
182 for (mb = 0; mb < mtop->nmoltype; mb++)
184 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
185 nmol = mtop->molblock[mb].nmol;
186 for (i = 0; (i < atoms->nr); i++)
188 q += nmol*atoms->atom[i].q;
189 m = atoms->atom[i].m;
190 mB = atoms->atom[i].mB;
191 pt = atoms->atom[i].ptype;
192 /* If the particle is an atom or a nucleus it must have a mass,
193 * else, if it is a shell, a vsite or a bondshell it can have mass zero
195 if (((m <= 0.0) || (mB <= 0.0)) && ((pt == eptAtom) || (pt == eptNucleus)))
197 ri = atoms->atom[i].resind;
198 sprintf(buf, "atom %s (Res %s-%d) has mass %g (state A) / %g (state B)\n",
199 *(atoms->atomname[i]),
200 *(atoms->resinfo[ri].name),
201 atoms->resinfo[ri].nr,
203 warning_error(wi, buf);
206 if (((m != 0) || (mB != 0)) && (pt == eptVSite))
208 ri = atoms->atom[i].resind;
209 sprintf(buf, "virtual site %s (Res %s-%d) has non-zero mass %g (state A) / %g (state B)\n"
210 " Check your topology.\n",
211 *(atoms->atomname[i]),
212 *(atoms->resinfo[ri].name),
213 atoms->resinfo[ri].nr,
215 warning_error(wi, buf);
216 /* The following statements make LINCS break! */
217 /* atoms->atom[i].m=0; */
224 static void sum_q(t_atoms *atoms, int n, double *qt, double *qBt)
232 for (i = 0; i < atoms->nr; i++)
234 qmolA += atoms->atom[i].q;
235 qmolB += atoms->atom[i].qB;
237 /* Unfortunately an absolute comparison,
238 * but this avoids unnecessary warnings and gmx-users mails.
240 if (fabs(qmolA) >= 1e-6 || fabs(qmolB) >= 1e-6)
247 static void get_nbparm(char *nb_str, char *comb_str, int *nb, int *comb,
251 char warn_buf[STRLEN];
254 for (i = 1; (i < eNBF_NR); i++)
256 if (gmx_strcasecmp(nb_str, enbf_names[i]) == 0)
263 *nb = strtol(nb_str, NULL, 10);
265 if ((*nb < 1) || (*nb >= eNBF_NR))
267 sprintf(warn_buf, "Invalid nonbond function selector '%s' using %s",
268 nb_str, enbf_names[1]);
269 warning_error(wi, warn_buf);
273 for (i = 1; (i < eCOMB_NR); i++)
275 if (gmx_strcasecmp(comb_str, ecomb_names[i]) == 0)
282 *comb = strtol(comb_str, NULL, 10);
284 if ((*comb < 1) || (*comb >= eCOMB_NR))
286 sprintf(warn_buf, "Invalid combination rule selector '%s' using %s",
287 comb_str, ecomb_names[1]);
288 warning_error(wi, warn_buf);
293 static char ** cpp_opts(const char *define, const char *include,
298 const char *cppadds[2];
299 char **cppopts = NULL;
300 const char *option[2] = { "-D", "-I" };
301 const char *nopt[2] = { "define", "include" };
305 char warn_buf[STRLEN];
308 cppadds[1] = include;
309 for (n = 0; (n < 2); n++)
316 while ((*ptr != '\0') && isspace(*ptr))
321 while ((*rptr != '\0') && !isspace(*rptr))
329 strncpy(buf, ptr, len);
330 if (strstr(ptr, option[n]) != ptr)
332 set_warning_line(wi, "mdp file", -1);
333 sprintf(warn_buf, "Malformed %s option %s", nopt[n], buf);
334 warning(wi, warn_buf);
338 srenew(cppopts, ++ncppopts);
339 cppopts[ncppopts-1] = gmx_strdup(buf);
347 srenew(cppopts, ++ncppopts);
348 cppopts[ncppopts-1] = NULL;
355 find_gb_bondlength(t_params *plist, int ai, int aj, real *length)
362 for (i = 0; i < F_NRE && !found; i++)
366 for (j = 0; j < plist[i].nr; j++)
368 a1 = plist[i].param[j].a[0];
369 a2 = plist[i].param[j].a[1];
371 if ( (a1 == ai && a2 == aj) || (a1 == aj && a2 == ai))
373 /* Equilibrium bond distance */
374 *length = plist[i].param[j].c[0];
387 find_gb_anglelength(t_params *plist, int ai, int ak, real *length)
389 int i, j, a1, a2, a3;
392 int status, status1, status2;
396 for (i = 0; i < F_NRE && !found; i++)
400 for (j = 0; j < plist[i].nr; j++)
402 a1 = plist[i].param[j].a[0];
403 a2 = plist[i].param[j].a[1];
404 a3 = plist[i].param[j].a[2];
406 /* We dont care what the middle atom is, but use it below */
407 if ( (a1 == ai && a3 == ak) || (a1 == ak && a3 == ai) )
409 /* Equilibrium bond distance */
410 a123 = plist[i].param[j].c[0];
411 /* Use middle atom to find reference distances r12 and r23 */
412 status1 = find_gb_bondlength(plist, a1, a2, &r12);
413 status2 = find_gb_bondlength(plist, a2, a3, &r23);
415 if (status1 == 0 && status2 == 0)
417 /* cosine theorem to get r13 */
418 *length = sqrt(r12*r12+r23*r23-(2*r12*r23*cos(a123/RAD2DEG)));
431 generate_gb_exclusion_interactions(t_molinfo *mi, gpp_atomtype_t atype, t_nextnb *nnb)
433 int i, j, k, n, ai, aj, ti, tj;
439 real radiusi, radiusj;
440 real gb_radiusi, gb_radiusj;
441 real param_c2, param_c4;
447 for (n = 1; n <= nnb->nrex; n++)
462 /* Put all higher-order exclusions into 1,4 list so we dont miss them */
469 for (ai = 0; ai < nnb->nr; ai++)
471 ti = at->atom[ai].type;
472 radiusi = get_atomtype_radius(ti, atype);
473 gb_radiusi = get_atomtype_gb_radius(ti, atype);
475 for (j = 0; j < nnb->nrexcl[ai][n]; j++)
477 aj = nnb->a[ai][n][j];
479 /* Only add the interactions once */
482 tj = at->atom[aj].type;
483 radiusj = get_atomtype_radius(tj, atype);
484 gb_radiusj = get_atomtype_gb_radius(tj, atype);
486 /* There is an exclusion of type "ftype" between atoms ai and aj */
490 /* Reference distance, not used for 1-4 interactions */
494 if (find_gb_bondlength(plist, ai, aj, &distance) != 0)
496 gmx_fatal(FARGS, "Cannot find bond length for atoms %d-%d", ai, aj);
500 if (find_gb_anglelength(plist, ai, aj, &distance) != 0)
502 gmx_fatal(FARGS, "Cannot find length for atoms %d-%d involved in angle", ai, aj);
509 /* Assign GB parameters */
511 param.c[0] = radiusi+radiusj;
512 /* Reference distance distance */
513 param.c[1] = distance;
514 /* Still parameter */
515 param.c[2] = param_c2;
517 param.c[3] = gb_radiusi+gb_radiusj;
519 param.c[4] = param_c4;
521 /* Add it to the parameter list */
522 add_param_to_list(&plist[ftype], ¶m);
533 static char **read_topol(const char *infile, const char *outfile,
534 const char *define, const char *include,
536 gpp_atomtype_t atype,
540 int *combination_rule,
545 gmx_molblock_t **molblock,
552 int i, sl, nb_funct, comb;
553 char *pline = NULL, **title = NULL;
554 char line[STRLEN], errbuf[256], comb_str[256], nb_str[256];
556 char *dirstr, *dummy2;
557 int nrcopies, nmol, nmolb = 0, nscan, ncombs, ncopy;
558 double fLJ, fQQ, fPOW;
559 gmx_molblock_t *molb = NULL;
560 t_topology *block = NULL;
561 t_molinfo *mi0 = NULL;
564 t_nbparam **nbparam, **pair;
566 real fudgeLJ = -1; /* Multiplication factor to generate 1-4 from LJ */
567 gmx_bool bReadDefaults, bReadMolType, bGenPairs, bWarn_copy_A_B;
568 double qt = 0, qBt = 0; /* total charge */
569 t_bond_atomtype batype;
571 int dcatt = -1, nmol_couple;
572 /* File handling variables */
575 char *tmp_line = NULL;
576 char warn_buf[STRLEN];
577 const char *floating_point_arithmetic_tip =
578 "Total charge should normally be an integer. See\n"
579 "http://www.gromacs.org/Documentation/Floating_Point_Arithmetic\n"
580 "for discussion on how close it should be to an integer.\n";
581 /* We need to open the output file before opening the input file,
582 * because cpp_open_file can change the current working directory.
586 out = gmx_fio_fopen(outfile, "w");
593 /* open input file */
594 status = cpp_open_file(infile, &handle, cpp_opts(define, include, wi));
597 gmx_fatal(FARGS, cpp_error(&handle, status));
600 /* some local variables */
601 DS_Init(&DS); /* directive stack */
602 nmol = 0; /* no molecules yet... */
603 d = d_invalid; /* first thing should be a directive */
604 nbparam = NULL; /* The temporary non-bonded matrix */
605 pair = NULL; /* The temporary pair interaction matrix */
606 block2 = NULL; /* the extra exclusions */
608 *reppow = 12.0; /* Default value for repulsion power */
612 /* Init the number of CMAP torsion angles and grid spacing */
613 plist[F_CMAP].grid_spacing = 0;
614 plist[F_CMAP].nc = 0;
616 bWarn_copy_A_B = bFEP;
618 batype = init_bond_atomtype();
619 /* parse the actual file */
620 bReadDefaults = FALSE;
622 bReadMolType = FALSE;
627 status = cpp_read_line(&handle, STRLEN, line);
628 done = (status == eCPP_EOF);
631 if (status != eCPP_OK)
633 gmx_fatal(FARGS, cpp_error(&handle, status));
637 fprintf(out, "%s\n", line);
640 set_warning_line(wi, cpp_cur_file(&handle), cpp_cur_linenr(&handle));
642 pline = gmx_strdup(line);
644 /* Strip trailing '\' from pline, if it exists */
646 if ((sl > 0) && (pline[sl-1] == CONTINUE))
651 /* build one long line from several fragments - necessary for CMAP */
652 while (continuing(line))
654 status = cpp_read_line(&handle, STRLEN, line);
655 set_warning_line(wi, cpp_cur_file(&handle), cpp_cur_linenr(&handle));
657 /* Since we depend on the '\' being present to continue to read, we copy line
658 * to a tmp string, strip the '\' from that string, and cat it to pline
660 tmp_line = gmx_strdup(line);
662 sl = strlen(tmp_line);
663 if ((sl > 0) && (tmp_line[sl-1] == CONTINUE))
665 tmp_line[sl-1] = ' ';
668 done = (status == eCPP_EOF);
671 if (status != eCPP_OK)
673 gmx_fatal(FARGS, cpp_error(&handle, status));
677 fprintf(out, "%s\n", line);
681 srenew(pline, strlen(pline)+strlen(tmp_line)+1);
682 strcat(pline, tmp_line);
686 /* skip trailing and leading spaces and comment text */
687 strip_comment (pline);
690 /* if there is something left... */
691 if ((int)strlen(pline) > 0)
693 if (pline[0] == OPENDIR)
695 /* A directive on this line: copy the directive
696 * without the brackets into dirstr, then
697 * skip spaces and tabs on either side of directive
699 dirstr = gmx_strdup((pline+1));
700 if ((dummy2 = strchr (dirstr, CLOSEDIR)) != NULL)
706 if ((newd = str2dir(dirstr)) == d_invalid)
708 sprintf(errbuf, "Invalid directive %s", dirstr);
709 warning_error(wi, errbuf);
713 /* Directive found */
716 fprintf(debug, "found directive '%s'\n", dir2str(newd));
718 if (DS_Check_Order (DS, newd))
725 /* we should print here which directives should have
726 been present, and which actually are */
727 gmx_fatal(FARGS, "%s\nInvalid order for directive %s",
728 cpp_error(&handle, eCPP_SYNTAX), dir2str(newd));
734 else if (d != d_invalid)
736 /* Not a directive, just a plain string
737 * use a gigantic switch to decode,
738 * if there is a valid directive!
745 gmx_fatal(FARGS, "%s\nFound a second defaults directive.\n",
746 cpp_error(&handle, eCPP_SYNTAX));
748 bReadDefaults = TRUE;
749 nscan = sscanf(pline, "%s%s%s%lf%lf%lf",
750 nb_str, comb_str, genpairs, &fLJ, &fQQ, &fPOW);
761 get_nbparm(nb_str, comb_str, &nb_funct, &comb, wi);
762 *combination_rule = comb;
765 bGenPairs = (gmx_strncasecmp(genpairs, "Y", 1) == 0);
766 if (nb_funct != eNBF_LJ && bGenPairs)
768 gmx_fatal(FARGS, "Generating pair parameters is only supported with LJ non-bonded interactions");
784 nb_funct = ifunc_index(d_nonbond_params, nb_funct);
788 push_at(symtab, atype, batype, pline, nb_funct,
789 &nbparam, bGenPairs ? &pair : NULL, wi);
793 push_bt(d, plist, 2, NULL, batype, pline, wi);
795 case d_constrainttypes:
796 push_bt(d, plist, 2, NULL, batype, pline, wi);
801 push_nbt(d, pair, atype, pline, F_LJ14, wi);
805 push_bt(d, plist, 2, atype, NULL, pline, wi);
809 push_bt(d, plist, 3, NULL, batype, pline, wi);
811 case d_dihedraltypes:
812 /* Special routine that can read both 2 and 4 atom dihedral definitions. */
813 push_dihedraltype(d, plist, batype, pline, wi);
816 case d_nonbond_params:
817 push_nbt(d, nbparam, atype, pline, nb_funct, wi);
822 srenew(block,nblock);
823 srenew(blockinfo,nblock);
824 blk0=&(block[nblock-1]);
825 bi0=&(blockinfo[nblock-1]);
828 push_molt(symtab,bi0,pline);
832 case d_implicit_genborn_params:
833 push_gb_params(atype, pline, wi);
836 case d_implicit_surface_params:
837 gmx_fatal(FARGS, "Implicit surface directive not supported yet.");
841 push_cmaptype(d, plist, 5, atype, batype, pline, wi);
849 if (opts->couple_moltype != NULL &&
850 (opts->couple_lam0 == ecouplamNONE ||
851 opts->couple_lam0 == ecouplamQ ||
852 opts->couple_lam1 == ecouplamNONE ||
853 opts->couple_lam1 == ecouplamQ))
855 dcatt = add_atomtype_decoupled(symtab, atype,
856 &nbparam, bGenPairs ? &pair : NULL);
858 ntype = get_atomtype_ntypes(atype);
859 ncombs = (ntype*(ntype+1))/2;
860 generate_nbparams(comb, nb_funct, &(plist[nb_funct]), atype, wi);
861 ncopy = copy_nbparams(nbparam, nb_funct, &(plist[nb_funct]),
863 fprintf(stderr, "Generated %d of the %d non-bonded parameter combinations\n", ncombs-ncopy, ncombs);
864 free_nbparam(nbparam, ntype);
867 gen_pairs(&(plist[nb_funct]), &(plist[F_LJ14]), fudgeLJ, comb);
868 ncopy = copy_nbparams(pair, nb_funct, &(plist[F_LJ14]),
870 fprintf(stderr, "Generated %d of the %d 1-4 parameter combinations\n", ncombs-ncopy, ncombs);
871 free_nbparam(pair, ntype);
873 /* Copy GBSA parameters to atomtype array? */
878 push_molt(symtab, &nmol, molinfo, pline, wi);
879 srenew(block2, nmol);
880 block2[nmol-1].nr = 0;
881 mi0 = &((*molinfo)[nmol-1]);
885 push_atom(symtab, &(mi0->cgs), &(mi0->atoms), atype, pline, &lastcg, wi);
889 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, FALSE,
890 bGenPairs, *fudgeQQ, bZero, &bWarn_copy_A_B, wi);
900 case d_position_restraints:
901 case d_angle_restraints:
902 case d_angle_restraints_z:
903 case d_distance_restraints:
904 case d_orientation_restraints:
905 case d_dihedral_restraints:
908 case d_water_polarization:
909 case d_thole_polarization:
910 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, TRUE,
911 bGenPairs, *fudgeQQ, bZero, &bWarn_copy_A_B, wi);
914 push_cmap(d, plist, mi0->plist, &(mi0->atoms), atype, pline, wi);
918 push_vsitesn(d, mi0->plist, &(mi0->atoms), pline, wi);
922 if (!block2[nmol-1].nr)
924 init_block2(&(block2[nmol-1]), mi0->atoms.nr);
926 push_excl(pline, &(block2[nmol-1]));
930 title = put_symtab(symtab, pline);
937 push_mol(nmol, *molinfo, pline, &whichmol, &nrcopies, wi);
938 mi0 = &((*molinfo)[whichmol]);
939 srenew(molb, nmolb+1);
940 molb[nmolb].type = whichmol;
941 molb[nmolb].nmol = nrcopies;
944 bCouple = (opts->couple_moltype != NULL &&
945 (gmx_strcasecmp("system", opts->couple_moltype) == 0 ||
946 gmx_strcasecmp(*(mi0->name), opts->couple_moltype) == 0));
949 nmol_couple += nrcopies;
952 if (mi0->atoms.nr == 0)
954 gmx_fatal(FARGS, "Molecule type '%s' contains no atoms",
958 "Excluding %d bonded neighbours molecule type '%s'\n",
959 mi0->nrexcl, *mi0->name);
960 sum_q(&mi0->atoms, nrcopies, &qt, &qBt);
961 if (!mi0->bProcessed)
964 generate_excl(mi0->nrexcl,
969 merge_excl(&(mi0->excls), &(block2[whichmol]));
970 done_block2(&(block2[whichmol]));
971 make_shake(mi0->plist, &mi0->atoms, opts->nshake);
975 /* nnb contains information about first,2nd,3rd bonded neighbors.
976 * Use this to generate GB 1-2,1-3,1-4 interactions when necessary.
978 if (bGenborn == TRUE)
980 generate_gb_exclusion_interactions(mi0, atype, &nnb);
987 convert_moltype_couple(mi0, dcatt, *fudgeQQ,
988 opts->couple_lam0, opts->couple_lam1,
990 nb_funct, &(plist[nb_funct]));
992 stupid_fill_block(&mi0->mols, mi0->atoms.nr, TRUE);
993 mi0->bProcessed = TRUE;
998 fprintf (stderr, "case: %d\n", (int)d);
999 gmx_incons("unknown directive");
1008 status = cpp_close_file(&handle);
1009 if (status != eCPP_OK)
1011 gmx_fatal(FARGS, cpp_error(&handle, status));
1016 gmx_fio_fclose(out);
1019 if (opts->couple_moltype)
1021 if (nmol_couple == 0)
1023 gmx_fatal(FARGS, "Did not find any molecules of type '%s' for coupling",
1024 opts->couple_moltype);
1026 fprintf(stderr, "Coupling %d copies of molecule type '%s'\n",
1027 nmol_couple, opts->couple_moltype);
1030 /* this is not very clean, but fixes core dump on empty system name */
1033 title = put_symtab(symtab, "");
1035 if (fabs(qt) > 1e-4)
1037 sprintf(warn_buf, "System has non-zero total charge: %.6f\n%s\n", qt, floating_point_arithmetic_tip);
1038 warning_note(wi, warn_buf);
1040 if (fabs(qBt) > 1e-4 && !gmx_within_tol(qBt, qt, 1e-6))
1042 sprintf(warn_buf, "State B has non-zero total charge: %.6f\n%s\n", qBt, floating_point_arithmetic_tip);
1043 warning_note(wi, warn_buf);
1046 for (i = 0; i < nmol; i++)
1048 done_block2(&(block2[i]));
1052 done_bond_atomtype(&batype);
1062 char **do_top(gmx_bool bVerbose,
1063 const char *topfile,
1064 const char *topppfile,
1069 int *combination_rule,
1070 double *repulsion_power,
1072 gpp_atomtype_t atype,
1074 t_molinfo **molinfo,
1077 gmx_molblock_t **molblock,
1081 /* Tmpfile might contain a long path */
1082 const char *tmpfile;
1087 tmpfile = topppfile;
1096 printf("processing topology...\n");
1098 title = read_topol(topfile, tmpfile, opts->define, opts->include,
1099 symtab, atype, nrmols, molinfo,
1100 plist, combination_rule, repulsion_power,
1101 opts, fudgeQQ, nmolblock, molblock,
1102 ir->efep != efepNO, bGenborn, bZero, wi);
1103 if ((*combination_rule != eCOMB_GEOMETRIC) &&
1104 (ir->vdwtype == evdwUSER))
1106 warning(wi, "Using sigma/epsilon based combination rules with"
1107 " user supplied potential function may produce unwanted"
1115 static void generate_qmexcl_moltype(gmx_moltype_t *molt, unsigned char *grpnr,
1118 /* This routine expects molt->ilist to be of size F_NRE and ordered. */
1120 /* generates the exclusions between the individual QM atoms, as
1121 * these interactions should be handled by the QM subroutines and
1122 * not by the gromacs routines
1125 i, j, l, k = 0, jmax, qm_max = 0, qm_nr = 0, nratoms = 0, link_nr = 0, link_max = 0;
1127 *qm_arr = NULL, *link_arr = NULL, a1, a2, a3, a4, ftype = 0;
1133 *bQMMM, *blink, bexcl;
1135 /* First we search and select the QM atoms in an qm_arr array that
1136 * we use to create the exclusions.
1138 * we take the possibility into account that a user has defined more
1139 * than one QM group:
1141 * for that we also need to do this an ugly work-about just in case
1142 * the QM group contains the entire system...
1144 jmax = ir->opts.ngQM;
1146 /* we first search for all the QM atoms and put them in an array
1148 for (j = 0; j < jmax; j++)
1150 for (i = 0; i < molt->atoms.nr; i++)
1152 if (qm_nr >= qm_max)
1155 srenew(qm_arr, qm_max);
1157 if ((grpnr ? grpnr[i] : 0) == j)
1159 qm_arr[qm_nr++] = i;
1163 /* bQMMM[..] is an array containin TRUE/FALSE for atoms that are
1164 * QM/not QM. We first set all elements to false. Afterwards we use
1165 * the qm_arr to change the elements corresponding to the QM atoms
1168 snew(bQMMM, molt->atoms.nr);
1169 for (i = 0; i < molt->atoms.nr; i++)
1173 for (i = 0; i < qm_nr; i++)
1175 bQMMM[qm_arr[i]] = TRUE;
1178 /* We remove all bonded interactions (i.e. bonds,
1179 * angles, dihedrals, 1-4's), involving the QM atoms. The way they
1180 * are removed is as follows: if the interaction invloves 2 atoms,
1181 * it is removed if both atoms are QMatoms. If it involves 3 atoms,
1182 * it is removed if at least two of the atoms are QM atoms, if the
1183 * interaction involves 4 atoms, it is removed if there are at least
1184 * 2 QM atoms. Since this routine is called once before any forces
1185 * are computed, the top->idef.il[N].iatom[] array (see idef.h) can
1186 * be rewritten at this poitn without any problem. 25-9-2002 */
1188 /* first check weter we already have CONNBONDS: */
1189 if (molt->ilist[F_CONNBONDS].nr != 0)
1191 fprintf(stderr, "nr. of CONNBONDS present already: %d\n",
1192 molt->ilist[F_CONNBONDS].nr/3);
1193 ftype = molt->ilist[F_CONNBONDS].iatoms[0];
1194 k = molt->ilist[F_CONNBONDS].nr;
1196 /* now we delete all bonded interactions, except the ones describing
1197 * a chemical bond. These are converted to CONNBONDS
1199 for (i = 0; i < F_LJ; i++)
1201 if (i == F_CONNBONDS)
1205 nratoms = interaction_function[i].nratoms;
1207 while (j < molt->ilist[i].nr)
1213 a1 = molt->ilist[i].iatoms[j+1];
1214 a2 = molt->ilist[i].iatoms[j+2];
1215 bexcl = (bQMMM[a1] && bQMMM[a2]);
1216 /* a bonded beteen two QM atoms will be copied to the
1217 * CONNBONDS list, for reasons mentioned above
1219 if (bexcl && i < F_ANGLES)
1221 srenew(molt->ilist[F_CONNBONDS].iatoms, k+3);
1222 molt->ilist[F_CONNBONDS].nr += 3;
1223 molt->ilist[F_CONNBONDS].iatoms[k++] = ftype;
1224 molt->ilist[F_CONNBONDS].iatoms[k++] = a1;
1225 molt->ilist[F_CONNBONDS].iatoms[k++] = a2;
1229 a1 = molt->ilist[i].iatoms[j+1];
1230 a2 = molt->ilist[i].iatoms[j+2];
1231 a3 = molt->ilist[i].iatoms[j+3];
1232 bexcl = ((bQMMM[a1] && bQMMM[a2]) ||
1233 (bQMMM[a1] && bQMMM[a3]) ||
1234 (bQMMM[a2] && bQMMM[a3]));
1237 a1 = molt->ilist[i].iatoms[j+1];
1238 a2 = molt->ilist[i].iatoms[j+2];
1239 a3 = molt->ilist[i].iatoms[j+3];
1240 a4 = molt->ilist[i].iatoms[j+4];
1241 bexcl = ((bQMMM[a1] && bQMMM[a2] && bQMMM[a3]) ||
1242 (bQMMM[a1] && bQMMM[a2] && bQMMM[a4]) ||
1243 (bQMMM[a1] && bQMMM[a3] && bQMMM[a4]) ||
1244 (bQMMM[a2] && bQMMM[a3] && bQMMM[a4]));
1247 gmx_fatal(FARGS, "no such bonded interactions with %d atoms\n", nratoms);
1251 /* since the interaction involves QM atoms, these should be
1252 * removed from the MM ilist
1254 molt->ilist[i].nr -= (nratoms+1);
1255 for (l = j; l < molt->ilist[i].nr; l++)
1257 molt->ilist[i].iatoms[l] = molt->ilist[i].iatoms[l+(nratoms+1)];
1262 j += nratoms+1; /* the +1 is for the functype */
1266 /* Now, we search for atoms bonded to a QM atom because we also want
1267 * to exclude their nonbonded interactions with the QM atoms. The
1268 * reason for this is that this interaction is accounted for in the
1269 * linkatoms interaction with the QMatoms and would be counted
1272 for (i = 0; i < F_NRE; i++)
1277 while (j < molt->ilist[i].nr)
1279 a1 = molt->ilist[i].iatoms[j+1];
1280 a2 = molt->ilist[i].iatoms[j+2];
1281 if ((bQMMM[a1] && !bQMMM[a2]) || (!bQMMM[a1] && bQMMM[a2]))
1283 if (link_nr >= link_max)
1286 srenew(link_arr, link_max);
1290 link_arr[link_nr++] = a2;
1294 link_arr[link_nr++] = a1;
1301 snew(blink, molt->atoms.nr);
1302 for (i = 0; i < molt->atoms.nr; i++)
1306 for (i = 0; i < link_nr; i++)
1308 blink[link_arr[i]] = TRUE;
1310 /* creating the exclusion block for the QM atoms. Each QM atom has
1311 * as excluded elements all the other QMatoms (and itself).
1313 qmexcl.nr = molt->atoms.nr;
1314 qmexcl.nra = qm_nr*(qm_nr+link_nr)+link_nr*qm_nr;
1315 snew(qmexcl.index, qmexcl.nr+1);
1316 snew(qmexcl.a, qmexcl.nra);
1318 for (i = 0; i < qmexcl.nr; i++)
1320 qmexcl.index[i] = j;
1323 for (k = 0; k < qm_nr; k++)
1325 qmexcl.a[k+j] = qm_arr[k];
1327 for (k = 0; k < link_nr; k++)
1329 qmexcl.a[qm_nr+k+j] = link_arr[k];
1331 j += (qm_nr+link_nr);
1335 for (k = 0; k < qm_nr; k++)
1337 qmexcl.a[k+j] = qm_arr[k];
1342 qmexcl.index[qmexcl.nr] = j;
1344 /* and merging with the exclusions already present in sys.
1347 init_block2(&qmexcl2, molt->atoms.nr);
1348 b_to_b2(&qmexcl, &qmexcl2);
1349 merge_excl(&(molt->excls), &qmexcl2);
1350 done_block2(&qmexcl2);
1352 /* Finally, we also need to get rid of the pair interactions of the
1353 * classical atom bonded to the boundary QM atoms with the QMatoms,
1354 * as this interaction is already accounted for by the QM, so also
1355 * here we run the risk of double counting! We proceed in a similar
1356 * way as we did above for the other bonded interactions: */
1357 for (i = F_LJ14; i < F_COUL14; i++)
1359 nratoms = interaction_function[i].nratoms;
1361 while (j < molt->ilist[i].nr)
1363 a1 = molt->ilist[i].iatoms[j+1];
1364 a2 = molt->ilist[i].iatoms[j+2];
1365 bexcl = ((bQMMM[a1] && bQMMM[a2]) ||
1366 (blink[a1] && bQMMM[a2]) ||
1367 (bQMMM[a1] && blink[a2]));
1370 /* since the interaction involves QM atoms, these should be
1371 * removed from the MM ilist
1373 molt->ilist[i].nr -= (nratoms+1);
1374 for (k = j; k < molt->ilist[i].nr; k++)
1376 molt->ilist[i].iatoms[k] = molt->ilist[i].iatoms[k+(nratoms+1)];
1381 j += nratoms+1; /* the +1 is for the functype */
1390 } /* generate_qmexcl */
1392 void generate_qmexcl(gmx_mtop_t *sys, t_inputrec *ir, warninp_t wi)
1394 /* This routine expects molt->molt[m].ilist to be of size F_NRE and ordered.
1397 unsigned char *grpnr;
1398 int mb, mol, nat_mol, i, nr_mol_with_qm_atoms = 0;
1399 gmx_molblock_t *molb;
1402 grpnr = sys->groups.grpnr[egcQMMM];
1404 for (mb = 0; mb < sys->nmolblock; mb++)
1406 molb = &sys->molblock[mb];
1407 nat_mol = sys->moltype[molb->type].atoms.nr;
1408 for (mol = 0; mol < molb->nmol; mol++)
1411 for (i = 0; i < nat_mol; i++)
1413 if ((grpnr ? grpnr[i] : 0) < ir->opts.ngQM)
1420 nr_mol_with_qm_atoms++;
1423 /* We need to split this molblock */
1426 /* Split the molblock at this molecule */
1428 srenew(sys->molblock, sys->nmolblock);
1429 for (i = sys->nmolblock-2; i >= mb; i--)
1431 sys->molblock[i+1] = sys->molblock[i];
1433 sys->molblock[mb ].nmol = mol;
1434 sys->molblock[mb+1].nmol -= mol;
1436 molb = &sys->molblock[mb];
1440 /* Split the molblock after this molecule */
1442 srenew(sys->molblock, sys->nmolblock);
1443 molb = &sys->molblock[mb];
1444 for (i = sys->nmolblock-2; i >= mb; i--)
1446 sys->molblock[i+1] = sys->molblock[i];
1448 sys->molblock[mb ].nmol = 1;
1449 sys->molblock[mb+1].nmol -= 1;
1452 /* Add a moltype for the QMMM molecule */
1454 srenew(sys->moltype, sys->nmoltype);
1455 /* Copy the moltype struct */
1456 sys->moltype[sys->nmoltype-1] = sys->moltype[molb->type];
1457 /* Copy the exclusions to a new array, since this is the only
1458 * thing that needs to be modified for QMMM.
1460 copy_blocka(&sys->moltype[molb->type ].excls,
1461 &sys->moltype[sys->nmoltype-1].excls);
1462 /* Set the molecule type for the QMMM molblock */
1463 molb->type = sys->nmoltype - 1;
1465 generate_qmexcl_moltype(&sys->moltype[molb->type], grpnr, ir);
1473 if (nr_mol_with_qm_atoms > 1)
1475 /* generate a warning is there are QM atoms in different
1476 * topologies. In this case it is not possible at this stage to
1477 * mutualy exclude the non-bonded interactions via the
1478 * exclusions (AFAIK). Instead, the user is advised to use the
1479 * energy group exclusions in the mdp file
1482 "\nThe QM subsystem is divided over multiple topologies. "
1483 "The mutual non-bonded interactions cannot be excluded. "
1484 "There are two ways to achieve this:\n\n"
1485 "1) merge the topologies, such that the atoms of the QM "
1486 "subsystem are all present in one single topology file. "
1487 "In this case this warning will dissappear\n\n"
1488 "2) exclude the non-bonded interactions explicitly via the "
1489 "energygrp-excl option in the mdp file. if this is the case "
1490 "this warning may be ignored"