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.
47 #include <sys/types.h>
49 #include "gromacs/utility/futil.h"
50 #include "gromacs/legacyheaders/typedefs.h"
51 #include "gromacs/utility/smalloc.h"
52 #include "gromacs/legacyheaders/macros.h"
53 #include "gromacs/fileio/gmxfio.h"
54 #include "gromacs/legacyheaders/txtdump.h"
55 #include "gromacs/math/units.h"
56 #include "gromacs/legacyheaders/macros.h"
57 #include "gromacs/legacyheaders/names.h"
58 #include "gromacs/utility/cstringutil.h"
59 #include "gromacs/topology/block.h"
60 #include "gromacs/topology/symtab.h"
61 #include "gromacs/topology/topology.h"
62 #include "gromacs/utility/fatalerror.h"
63 #include "gromacs/legacyheaders/warninp.h"
64 #include "vsite_parm.h"
66 #include "grompp-impl.h"
70 #include "gpp_nextnb.h"
74 #include "gpp_bond_atomtype.h"
75 #include "gromacs/legacyheaders/genborn.h"
76 #include "gromacs/math/utilities.h"
78 #define OPENDIR '[' /* starting sign for directive */
79 #define CLOSEDIR ']' /* ending sign for directive */
81 static void free_nbparam(t_nbparam **param, int nr)
85 for (i = 0; i < nr; i++)
92 static int copy_nbparams(t_nbparam **param, int ftype, t_params *plist, int nr)
100 for (i = 0; i < nr; i++)
102 for (j = 0; j <= i; j++)
104 if (param[i][j].bSet)
106 for (f = 0; f < nrfp; f++)
108 plist->param[nr*i+j].c[f] = param[i][j].c[f];
109 plist->param[nr*j+i].c[f] = param[i][j].c[f];
119 static void gen_pairs(t_params *nbs, t_params *pairs, real fudge, int comb)
121 int i, j, ntp, nrfp, nrfpA, nrfpB, nnn;
126 nrfpA = interaction_function[F_LJ14].nrfpA;
127 nrfpB = interaction_function[F_LJ14].nrfpB;
130 if ((nrfp != nrfpA) || (nrfpA != nrfpB))
132 gmx_incons("Number of force parameters in gen_pairs wrong");
135 fprintf(stderr, "Generating 1-4 interactions: fudge = %g\n", fudge);
138 fprintf(debug, "Fudge factor for 1-4 interactions: %g\n", fudge);
139 fprintf(debug, "Holy Cow! there are %d types\n", ntp);
141 snew(pairs->param, pairs->nr);
142 for (i = 0; (i < ntp); i++)
145 pairs->param[i].a[0] = i / nnn;
146 pairs->param[i].a[1] = i % nnn;
147 /* Copy normal and FEP parameters and multiply by fudge factor */
151 for (j = 0; (j < nrfp); j++)
153 /* If we are using sigma/epsilon values, only the epsilon values
154 * should be scaled, but not sigma.
155 * The sigma values have even indices 0,2, etc.
157 if ((comb == eCOMB_ARITHMETIC || comb == eCOMB_GEOM_SIG_EPS) && (j%2 == 0))
166 pairs->param[i].c[j] = scaling*nbs->param[i].c[j];
167 pairs->param[i].c[nrfp+j] = scaling*nbs->param[i].c[j];
172 double check_mol(gmx_mtop_t *mtop, warninp_t wi)
175 int i, mb, nmol, ri, pt;
180 /* Check mass and charge */
183 for (mb = 0; mb < mtop->nmoltype; mb++)
185 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
186 nmol = mtop->molblock[mb].nmol;
187 for (i = 0; (i < atoms->nr); i++)
189 q += nmol*atoms->atom[i].q;
190 m = atoms->atom[i].m;
191 mB = atoms->atom[i].mB;
192 pt = atoms->atom[i].ptype;
193 /* If the particle is an atom or a nucleus it must have a mass,
194 * else, if it is a shell, a vsite or a bondshell it can have mass zero
196 if (((m <= 0.0) || (mB <= 0.0)) && ((pt == eptAtom) || (pt == eptNucleus)))
198 ri = atoms->atom[i].resind;
199 sprintf(buf, "atom %s (Res %s-%d) has mass %g (state A) / %g (state B)\n",
200 *(atoms->atomname[i]),
201 *(atoms->resinfo[ri].name),
202 atoms->resinfo[ri].nr,
204 warning_error(wi, buf);
207 if (((m != 0) || (mB != 0)) && (pt == eptVSite))
209 ri = atoms->atom[i].resind;
210 sprintf(buf, "virtual site %s (Res %s-%d) has non-zero mass %g (state A) / %g (state B)\n"
211 " Check your topology.\n",
212 *(atoms->atomname[i]),
213 *(atoms->resinfo[ri].name),
214 atoms->resinfo[ri].nr,
216 warning_error(wi, buf);
217 /* The following statements make LINCS break! */
218 /* atoms->atom[i].m=0; */
225 static void sum_q(t_atoms *atoms, int n, double *qt, double *qBt)
233 for (i = 0; i < atoms->nr; i++)
235 qmolA += atoms->atom[i].q;
236 qmolB += atoms->atom[i].qB;
238 /* Unfortunately an absolute comparison,
239 * but this avoids unnecessary warnings and gmx-users mails.
241 if (fabs(qmolA) >= 1e-6 || fabs(qmolB) >= 1e-6)
248 static void get_nbparm(char *nb_str, char *comb_str, int *nb, int *comb,
252 char warn_buf[STRLEN];
255 for (i = 1; (i < eNBF_NR); i++)
257 if (gmx_strcasecmp(nb_str, enbf_names[i]) == 0)
264 *nb = strtol(nb_str, NULL, 10);
266 if ((*nb < 1) || (*nb >= eNBF_NR))
268 sprintf(warn_buf, "Invalid nonbond function selector '%s' using %s",
269 nb_str, enbf_names[1]);
270 warning_error(wi, warn_buf);
274 for (i = 1; (i < eCOMB_NR); i++)
276 if (gmx_strcasecmp(comb_str, ecomb_names[i]) == 0)
283 *comb = strtol(comb_str, NULL, 10);
285 if ((*comb < 1) || (*comb >= eCOMB_NR))
287 sprintf(warn_buf, "Invalid combination rule selector '%s' using %s",
288 comb_str, ecomb_names[1]);
289 warning_error(wi, warn_buf);
294 static char ** cpp_opts(const char *define, const char *include,
299 const char *cppadds[2];
300 char **cppopts = NULL;
301 const char *option[2] = { "-D", "-I" };
302 const char *nopt[2] = { "define", "include" };
306 char warn_buf[STRLEN];
309 cppadds[1] = include;
310 for (n = 0; (n < 2); n++)
317 while ((*ptr != '\0') && isspace(*ptr))
322 while ((*rptr != '\0') && !isspace(*rptr))
330 strncpy(buf, ptr, len);
331 if (strstr(ptr, option[n]) != ptr)
333 set_warning_line(wi, "mdp file", -1);
334 sprintf(warn_buf, "Malformed %s option %s", nopt[n], buf);
335 warning(wi, warn_buf);
339 srenew(cppopts, ++ncppopts);
340 cppopts[ncppopts-1] = gmx_strdup(buf);
348 srenew(cppopts, ++ncppopts);
349 cppopts[ncppopts-1] = NULL;
356 find_gb_bondlength(t_params *plist, int ai, int aj, real *length)
363 for (i = 0; i < F_NRE && !found; i++)
367 for (j = 0; j < plist[i].nr; j++)
369 a1 = plist[i].param[j].a[0];
370 a2 = plist[i].param[j].a[1];
372 if ( (a1 == ai && a2 == aj) || (a1 == aj && a2 == ai))
374 /* Equilibrium bond distance */
375 *length = plist[i].param[j].c[0];
388 find_gb_anglelength(t_params *plist, int ai, int ak, real *length)
390 int i, j, a1, a2, a3;
393 int status, status1, status2;
397 for (i = 0; i < F_NRE && !found; i++)
401 for (j = 0; j < plist[i].nr; j++)
403 a1 = plist[i].param[j].a[0];
404 a2 = plist[i].param[j].a[1];
405 a3 = plist[i].param[j].a[2];
407 /* We dont care what the middle atom is, but use it below */
408 if ( (a1 == ai && a3 == ak) || (a1 == ak && a3 == ai) )
410 /* Equilibrium bond distance */
411 a123 = plist[i].param[j].c[0];
412 /* Use middle atom to find reference distances r12 and r23 */
413 status1 = find_gb_bondlength(plist, a1, a2, &r12);
414 status2 = find_gb_bondlength(plist, a2, a3, &r23);
416 if (status1 == 0 && status2 == 0)
418 /* cosine theorem to get r13 */
419 *length = sqrt(r12*r12+r23*r23-(2*r12*r23*cos(a123/RAD2DEG)));
432 generate_gb_exclusion_interactions(t_molinfo *mi, gpp_atomtype_t atype, t_nextnb *nnb)
434 int i, j, k, n, ai, aj, ti, tj;
440 real radiusi, radiusj;
441 real gb_radiusi, gb_radiusj;
442 real param_c2, param_c4;
448 for (n = 1; n <= nnb->nrex; n++)
463 /* Put all higher-order exclusions into 1,4 list so we dont miss them */
470 for (ai = 0; ai < nnb->nr; ai++)
472 ti = at->atom[ai].type;
473 radiusi = get_atomtype_radius(ti, atype);
474 gb_radiusi = get_atomtype_gb_radius(ti, atype);
476 for (j = 0; j < nnb->nrexcl[ai][n]; j++)
478 aj = nnb->a[ai][n][j];
480 /* Only add the interactions once */
483 tj = at->atom[aj].type;
484 radiusj = get_atomtype_radius(tj, atype);
485 gb_radiusj = get_atomtype_gb_radius(tj, atype);
487 /* There is an exclusion of type "ftype" between atoms ai and aj */
491 /* Reference distance, not used for 1-4 interactions */
495 if (find_gb_bondlength(plist, ai, aj, &distance) != 0)
497 gmx_fatal(FARGS, "Cannot find bond length for atoms %d-%d", ai, aj);
501 if (find_gb_anglelength(plist, ai, aj, &distance) != 0)
503 gmx_fatal(FARGS, "Cannot find length for atoms %d-%d involved in angle", ai, aj);
510 /* Assign GB parameters */
512 param.c[0] = radiusi+radiusj;
513 /* Reference distance distance */
514 param.c[1] = distance;
515 /* Still parameter */
516 param.c[2] = param_c2;
518 param.c[3] = gb_radiusi+gb_radiusj;
520 param.c[4] = param_c4;
522 /* Add it to the parameter list */
523 add_param_to_list(&plist[ftype], ¶m);
534 static char **read_topol(const char *infile, const char *outfile,
535 const char *define, const char *include,
537 gpp_atomtype_t atype,
541 int *combination_rule,
546 gmx_molblock_t **molblock,
553 int i, sl, nb_funct, comb;
554 char *pline = NULL, **title = NULL;
555 char line[STRLEN], errbuf[256], comb_str[256], nb_str[256];
557 char *dirstr, *dummy2;
558 int nrcopies, nmol, nmolb = 0, nscan, ncombs, ncopy;
559 double fLJ, fQQ, fPOW;
560 gmx_molblock_t *molb = NULL;
561 t_topology *block = NULL;
562 t_molinfo *mi0 = NULL;
565 t_nbparam **nbparam, **pair;
567 real fudgeLJ = -1; /* Multiplication factor to generate 1-4 from LJ */
568 gmx_bool bReadDefaults, bReadMolType, bGenPairs, bWarn_copy_A_B;
569 double qt = 0, qBt = 0; /* total charge */
570 t_bond_atomtype batype;
572 int dcatt = -1, nmol_couple;
573 /* File handling variables */
576 char *tmp_line = NULL;
577 char warn_buf[STRLEN];
578 const char *floating_point_arithmetic_tip =
579 "Total charge should normally be an integer. See\n"
580 "http://www.gromacs.org/Documentation/Floating_Point_Arithmetic\n"
581 "for discussion on how close it should be to an integer.\n";
582 /* We need to open the output file before opening the input file,
583 * because cpp_open_file can change the current working directory.
587 out = gmx_fio_fopen(outfile, "w");
594 /* open input file */
595 status = cpp_open_file(infile, &handle, cpp_opts(define, include, wi));
598 gmx_fatal(FARGS, cpp_error(&handle, status));
601 /* some local variables */
602 DS_Init(&DS); /* directive stack */
603 nmol = 0; /* no molecules yet... */
604 d = d_invalid; /* first thing should be a directive */
605 nbparam = NULL; /* The temporary non-bonded matrix */
606 pair = NULL; /* The temporary pair interaction matrix */
607 block2 = NULL; /* the extra exclusions */
609 *reppow = 12.0; /* Default value for repulsion power */
613 /* Init the number of CMAP torsion angles and grid spacing */
614 plist[F_CMAP].grid_spacing = 0;
615 plist[F_CMAP].nc = 0;
617 bWarn_copy_A_B = bFEP;
619 batype = init_bond_atomtype();
620 /* parse the actual file */
621 bReadDefaults = FALSE;
623 bReadMolType = FALSE;
628 status = cpp_read_line(&handle, STRLEN, line);
629 done = (status == eCPP_EOF);
632 if (status != eCPP_OK)
634 gmx_fatal(FARGS, cpp_error(&handle, status));
638 fprintf(out, "%s\n", line);
641 set_warning_line(wi, cpp_cur_file(&handle), cpp_cur_linenr(&handle));
643 pline = gmx_strdup(line);
645 /* Strip trailing '\' from pline, if it exists */
647 if ((sl > 0) && (pline[sl-1] == CONTINUE))
652 /* build one long line from several fragments - necessary for CMAP */
653 while (continuing(line))
655 status = cpp_read_line(&handle, STRLEN, line);
656 set_warning_line(wi, cpp_cur_file(&handle), cpp_cur_linenr(&handle));
658 /* Since we depend on the '\' being present to continue to read, we copy line
659 * to a tmp string, strip the '\' from that string, and cat it to pline
661 tmp_line = gmx_strdup(line);
663 sl = strlen(tmp_line);
664 if ((sl > 0) && (tmp_line[sl-1] == CONTINUE))
666 tmp_line[sl-1] = ' ';
669 done = (status == eCPP_EOF);
672 if (status != eCPP_OK)
674 gmx_fatal(FARGS, cpp_error(&handle, status));
678 fprintf(out, "%s\n", line);
682 srenew(pline, strlen(pline)+strlen(tmp_line)+1);
683 strcat(pline, tmp_line);
687 /* skip trailing and leading spaces and comment text */
688 strip_comment (pline);
691 /* if there is something left... */
692 if ((int)strlen(pline) > 0)
694 if (pline[0] == OPENDIR)
696 /* A directive on this line: copy the directive
697 * without the brackets into dirstr, then
698 * skip spaces and tabs on either side of directive
700 dirstr = gmx_strdup((pline+1));
701 if ((dummy2 = strchr (dirstr, CLOSEDIR)) != NULL)
707 if ((newd = str2dir(dirstr)) == d_invalid)
709 sprintf(errbuf, "Invalid directive %s", dirstr);
710 warning_error(wi, errbuf);
714 /* Directive found */
717 fprintf(debug, "found directive '%s'\n", dir2str(newd));
719 if (DS_Check_Order (DS, newd))
726 /* we should print here which directives should have
727 been present, and which actually are */
728 gmx_fatal(FARGS, "%s\nInvalid order for directive %s",
729 cpp_error(&handle, eCPP_SYNTAX), dir2str(newd));
735 else if (d != d_invalid)
737 /* Not a directive, just a plain string
738 * use a gigantic switch to decode,
739 * if there is a valid directive!
746 gmx_fatal(FARGS, "%s\nFound a second defaults directive.\n",
747 cpp_error(&handle, eCPP_SYNTAX));
749 bReadDefaults = TRUE;
750 nscan = sscanf(pline, "%s%s%s%lf%lf%lf",
751 nb_str, comb_str, genpairs, &fLJ, &fQQ, &fPOW);
762 get_nbparm(nb_str, comb_str, &nb_funct, &comb, wi);
763 *combination_rule = comb;
766 bGenPairs = (gmx_strncasecmp(genpairs, "Y", 1) == 0);
767 if (nb_funct != eNBF_LJ && bGenPairs)
769 gmx_fatal(FARGS, "Generating pair parameters is only supported with LJ non-bonded interactions");
785 nb_funct = ifunc_index(d_nonbond_params, nb_funct);
789 push_at(symtab, atype, batype, pline, nb_funct,
790 &nbparam, bGenPairs ? &pair : NULL, wi);
794 push_bt(d, plist, 2, NULL, batype, pline, wi);
796 case d_constrainttypes:
797 push_bt(d, plist, 2, NULL, batype, pline, wi);
802 push_nbt(d, pair, atype, pline, F_LJ14, wi);
806 push_bt(d, plist, 2, atype, NULL, pline, wi);
810 push_bt(d, plist, 3, NULL, batype, pline, wi);
812 case d_dihedraltypes:
813 /* Special routine that can read both 2 and 4 atom dihedral definitions. */
814 push_dihedraltype(d, plist, batype, pline, wi);
817 case d_nonbond_params:
818 push_nbt(d, nbparam, atype, pline, nb_funct, wi);
823 srenew(block,nblock);
824 srenew(blockinfo,nblock);
825 blk0=&(block[nblock-1]);
826 bi0=&(blockinfo[nblock-1]);
829 push_molt(symtab,bi0,pline);
833 case d_implicit_genborn_params:
834 push_gb_params(atype, pline, wi);
837 case d_implicit_surface_params:
838 gmx_fatal(FARGS, "Implicit surface directive not supported yet.");
842 push_cmaptype(d, plist, 5, atype, batype, pline, wi);
850 if (opts->couple_moltype != NULL &&
851 (opts->couple_lam0 == ecouplamNONE ||
852 opts->couple_lam0 == ecouplamQ ||
853 opts->couple_lam1 == ecouplamNONE ||
854 opts->couple_lam1 == ecouplamQ))
856 dcatt = add_atomtype_decoupled(symtab, atype,
857 &nbparam, bGenPairs ? &pair : NULL);
859 ntype = get_atomtype_ntypes(atype);
860 ncombs = (ntype*(ntype+1))/2;
861 generate_nbparams(comb, nb_funct, &(plist[nb_funct]), atype, wi);
862 ncopy = copy_nbparams(nbparam, nb_funct, &(plist[nb_funct]),
864 fprintf(stderr, "Generated %d of the %d non-bonded parameter combinations\n", ncombs-ncopy, ncombs);
865 free_nbparam(nbparam, ntype);
868 gen_pairs(&(plist[nb_funct]), &(plist[F_LJ14]), fudgeLJ, comb);
869 ncopy = copy_nbparams(pair, nb_funct, &(plist[F_LJ14]),
871 fprintf(stderr, "Generated %d of the %d 1-4 parameter combinations\n", ncombs-ncopy, ncombs);
872 free_nbparam(pair, ntype);
874 /* Copy GBSA parameters to atomtype array? */
879 push_molt(symtab, &nmol, molinfo, pline, wi);
880 srenew(block2, nmol);
881 block2[nmol-1].nr = 0;
882 mi0 = &((*molinfo)[nmol-1]);
886 push_atom(symtab, &(mi0->cgs), &(mi0->atoms), atype, pline, &lastcg, wi);
890 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, FALSE,
891 bGenPairs, *fudgeQQ, bZero, &bWarn_copy_A_B, wi);
901 case d_position_restraints:
902 case d_angle_restraints:
903 case d_angle_restraints_z:
904 case d_distance_restraints:
905 case d_orientation_restraints:
906 case d_dihedral_restraints:
909 case d_water_polarization:
910 case d_thole_polarization:
911 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, TRUE,
912 bGenPairs, *fudgeQQ, bZero, &bWarn_copy_A_B, wi);
915 push_cmap(d, plist, mi0->plist, &(mi0->atoms), atype, pline, wi);
919 push_vsitesn(d, mi0->plist, &(mi0->atoms), pline, wi);
923 if (!block2[nmol-1].nr)
925 init_block2(&(block2[nmol-1]), mi0->atoms.nr);
927 push_excl(pline, &(block2[nmol-1]));
931 title = put_symtab(symtab, pline);
938 push_mol(nmol, *molinfo, pline, &whichmol, &nrcopies, wi);
939 mi0 = &((*molinfo)[whichmol]);
940 srenew(molb, nmolb+1);
941 molb[nmolb].type = whichmol;
942 molb[nmolb].nmol = nrcopies;
945 bCouple = (opts->couple_moltype != NULL &&
946 (gmx_strcasecmp("system", opts->couple_moltype) == 0 ||
947 gmx_strcasecmp(*(mi0->name), opts->couple_moltype) == 0));
950 nmol_couple += nrcopies;
953 if (mi0->atoms.nr == 0)
955 gmx_fatal(FARGS, "Molecule type '%s' contains no atoms",
959 "Excluding %d bonded neighbours molecule type '%s'\n",
960 mi0->nrexcl, *mi0->name);
961 sum_q(&mi0->atoms, nrcopies, &qt, &qBt);
962 if (!mi0->bProcessed)
965 generate_excl(mi0->nrexcl,
970 merge_excl(&(mi0->excls), &(block2[whichmol]));
971 done_block2(&(block2[whichmol]));
972 make_shake(mi0->plist, &mi0->atoms, opts->nshake);
976 /* nnb contains information about first,2nd,3rd bonded neighbors.
977 * Use this to generate GB 1-2,1-3,1-4 interactions when necessary.
979 if (bGenborn == TRUE)
981 generate_gb_exclusion_interactions(mi0, atype, &nnb);
988 convert_moltype_couple(mi0, dcatt, *fudgeQQ,
989 opts->couple_lam0, opts->couple_lam1,
991 nb_funct, &(plist[nb_funct]));
993 stupid_fill_block(&mi0->mols, mi0->atoms.nr, TRUE);
994 mi0->bProcessed = TRUE;
999 fprintf (stderr, "case: %d\n", d);
1000 gmx_incons("unknown directive");
1009 status = cpp_close_file(&handle);
1010 if (status != eCPP_OK)
1012 gmx_fatal(FARGS, cpp_error(&handle, status));
1017 gmx_fio_fclose(out);
1020 if (opts->couple_moltype)
1022 if (nmol_couple == 0)
1024 gmx_fatal(FARGS, "Did not find any molecules of type '%s' for coupling",
1025 opts->couple_moltype);
1027 fprintf(stderr, "Coupling %d copies of molecule type '%s'\n",
1028 nmol_couple, opts->couple_moltype);
1031 /* this is not very clean, but fixes core dump on empty system name */
1034 title = put_symtab(symtab, "");
1036 if (fabs(qt) > 1e-4)
1038 sprintf(warn_buf, "System has non-zero total charge: %.6f\n%s\n", qt, floating_point_arithmetic_tip);
1039 warning_note(wi, warn_buf);
1041 if (fabs(qBt) > 1e-4 && !gmx_within_tol(qBt, qt, 1e-6))
1043 sprintf(warn_buf, "State B has non-zero total charge: %.6f\n%s\n", qBt, floating_point_arithmetic_tip);
1044 warning_note(wi, warn_buf);
1047 for (i = 0; i < nmol; i++)
1049 done_block2(&(block2[i]));
1053 done_bond_atomtype(&batype);
1063 char **do_top(gmx_bool bVerbose,
1064 const char *topfile,
1065 const char *topppfile,
1070 int *combination_rule,
1071 double *repulsion_power,
1073 gpp_atomtype_t atype,
1075 t_molinfo **molinfo,
1078 gmx_molblock_t **molblock,
1082 /* Tmpfile might contain a long path */
1083 const char *tmpfile;
1088 tmpfile = topppfile;
1097 printf("processing topology...\n");
1099 title = read_topol(topfile, tmpfile, opts->define, opts->include,
1100 symtab, atype, nrmols, molinfo,
1101 plist, combination_rule, repulsion_power,
1102 opts, fudgeQQ, nmolblock, molblock,
1103 ir->efep != efepNO, bGenborn, bZero, wi);
1104 if ((*combination_rule != eCOMB_GEOMETRIC) &&
1105 (ir->vdwtype == evdwUSER))
1107 warning(wi, "Using sigma/epsilon based combination rules with"
1108 " user supplied potential function may produce unwanted"
1116 static void generate_qmexcl_moltype(gmx_moltype_t *molt, unsigned char *grpnr,
1119 /* This routine expects molt->ilist to be of size F_NRE and ordered. */
1121 /* generates the exclusions between the individual QM atoms, as
1122 * these interactions should be handled by the QM subroutines and
1123 * not by the gromacs routines
1126 i, j, l, k = 0, jmax, qm_max = 0, qm_nr = 0, nratoms = 0, link_nr = 0, link_max = 0;
1128 *qm_arr = NULL, *link_arr = NULL, a1, a2, a3, a4, ftype = 0;
1134 *bQMMM, *blink, bexcl;
1136 /* First we search and select the QM atoms in an qm_arr array that
1137 * we use to create the exclusions.
1139 * we take the possibility into account that a user has defined more
1140 * than one QM group:
1142 * for that we also need to do this an ugly work-about just in case
1143 * the QM group contains the entire system...
1145 jmax = ir->opts.ngQM;
1147 /* we first search for all the QM atoms and put them in an array
1149 for (j = 0; j < jmax; j++)
1151 for (i = 0; i < molt->atoms.nr; i++)
1153 if (qm_nr >= qm_max)
1156 srenew(qm_arr, qm_max);
1158 if ((grpnr ? grpnr[i] : 0) == j)
1160 qm_arr[qm_nr++] = i;
1164 /* bQMMM[..] is an array containin TRUE/FALSE for atoms that are
1165 * QM/not QM. We first set all elements to false. Afterwards we use
1166 * the qm_arr to change the elements corresponding to the QM atoms
1169 snew(bQMMM, molt->atoms.nr);
1170 for (i = 0; i < molt->atoms.nr; i++)
1174 for (i = 0; i < qm_nr; i++)
1176 bQMMM[qm_arr[i]] = TRUE;
1179 /* We remove all bonded interactions (i.e. bonds,
1180 * angles, dihedrals, 1-4's), involving the QM atoms. The way they
1181 * are removed is as follows: if the interaction invloves 2 atoms,
1182 * it is removed if both atoms are QMatoms. If it involves 3 atoms,
1183 * it is removed if at least two of the atoms are QM atoms, if the
1184 * interaction involves 4 atoms, it is removed if there are at least
1185 * 2 QM atoms. Since this routine is called once before any forces
1186 * are computed, the top->idef.il[N].iatom[] array (see idef.h) can
1187 * be rewritten at this poitn without any problem. 25-9-2002 */
1189 /* first check weter we already have CONNBONDS: */
1190 if (molt->ilist[F_CONNBONDS].nr != 0)
1192 fprintf(stderr, "nr. of CONNBONDS present already: %d\n",
1193 molt->ilist[F_CONNBONDS].nr/3);
1194 ftype = molt->ilist[F_CONNBONDS].iatoms[0];
1195 k = molt->ilist[F_CONNBONDS].nr;
1197 /* now we delete all bonded interactions, except the ones describing
1198 * a chemical bond. These are converted to CONNBONDS
1200 for (i = 0; i < F_LJ; i++)
1202 if (i == F_CONNBONDS)
1206 nratoms = interaction_function[i].nratoms;
1208 while (j < molt->ilist[i].nr)
1214 a1 = molt->ilist[i].iatoms[j+1];
1215 a2 = molt->ilist[i].iatoms[j+2];
1216 bexcl = (bQMMM[a1] && bQMMM[a2]);
1217 /* a bonded beteen two QM atoms will be copied to the
1218 * CONNBONDS list, for reasons mentioned above
1220 if (bexcl && i < F_ANGLES)
1222 srenew(molt->ilist[F_CONNBONDS].iatoms, k+3);
1223 molt->ilist[F_CONNBONDS].nr += 3;
1224 molt->ilist[F_CONNBONDS].iatoms[k++] = ftype;
1225 molt->ilist[F_CONNBONDS].iatoms[k++] = a1;
1226 molt->ilist[F_CONNBONDS].iatoms[k++] = a2;
1230 a1 = molt->ilist[i].iatoms[j+1];
1231 a2 = molt->ilist[i].iatoms[j+2];
1232 a3 = molt->ilist[i].iatoms[j+3];
1233 bexcl = ((bQMMM[a1] && bQMMM[a2]) ||
1234 (bQMMM[a1] && bQMMM[a3]) ||
1235 (bQMMM[a2] && bQMMM[a3]));
1238 a1 = molt->ilist[i].iatoms[j+1];
1239 a2 = molt->ilist[i].iatoms[j+2];
1240 a3 = molt->ilist[i].iatoms[j+3];
1241 a4 = molt->ilist[i].iatoms[j+4];
1242 bexcl = ((bQMMM[a1] && bQMMM[a2] && bQMMM[a3]) ||
1243 (bQMMM[a1] && bQMMM[a2] && bQMMM[a4]) ||
1244 (bQMMM[a1] && bQMMM[a3] && bQMMM[a4]) ||
1245 (bQMMM[a2] && bQMMM[a3] && bQMMM[a4]));
1248 gmx_fatal(FARGS, "no such bonded interactions with %d atoms\n", nratoms);
1252 /* since the interaction involves QM atoms, these should be
1253 * removed from the MM ilist
1255 molt->ilist[i].nr -= (nratoms+1);
1256 for (l = j; l < molt->ilist[i].nr; l++)
1258 molt->ilist[i].iatoms[l] = molt->ilist[i].iatoms[l+(nratoms+1)];
1263 j += nratoms+1; /* the +1 is for the functype */
1267 /* Now, we search for atoms bonded to a QM atom because we also want
1268 * to exclude their nonbonded interactions with the QM atoms. The
1269 * reason for this is that this interaction is accounted for in the
1270 * linkatoms interaction with the QMatoms and would be counted
1273 for (i = 0; i < F_NRE; i++)
1278 while (j < molt->ilist[i].nr)
1280 a1 = molt->ilist[i].iatoms[j+1];
1281 a2 = molt->ilist[i].iatoms[j+2];
1282 if ((bQMMM[a1] && !bQMMM[a2]) || (!bQMMM[a1] && bQMMM[a2]))
1284 if (link_nr >= link_max)
1287 srenew(link_arr, link_max);
1291 link_arr[link_nr++] = a2;
1295 link_arr[link_nr++] = a1;
1302 snew(blink, molt->atoms.nr);
1303 for (i = 0; i < molt->atoms.nr; i++)
1307 for (i = 0; i < link_nr; i++)
1309 blink[link_arr[i]] = TRUE;
1311 /* creating the exclusion block for the QM atoms. Each QM atom has
1312 * as excluded elements all the other QMatoms (and itself).
1314 qmexcl.nr = molt->atoms.nr;
1315 qmexcl.nra = qm_nr*(qm_nr+link_nr)+link_nr*qm_nr;
1316 snew(qmexcl.index, qmexcl.nr+1);
1317 snew(qmexcl.a, qmexcl.nra);
1319 for (i = 0; i < qmexcl.nr; i++)
1321 qmexcl.index[i] = j;
1324 for (k = 0; k < qm_nr; k++)
1326 qmexcl.a[k+j] = qm_arr[k];
1328 for (k = 0; k < link_nr; k++)
1330 qmexcl.a[qm_nr+k+j] = link_arr[k];
1332 j += (qm_nr+link_nr);
1336 for (k = 0; k < qm_nr; k++)
1338 qmexcl.a[k+j] = qm_arr[k];
1343 qmexcl.index[qmexcl.nr] = j;
1345 /* and merging with the exclusions already present in sys.
1348 init_block2(&qmexcl2, molt->atoms.nr);
1349 b_to_b2(&qmexcl, &qmexcl2);
1350 merge_excl(&(molt->excls), &qmexcl2);
1351 done_block2(&qmexcl2);
1353 /* Finally, we also need to get rid of the pair interactions of the
1354 * classical atom bonded to the boundary QM atoms with the QMatoms,
1355 * as this interaction is already accounted for by the QM, so also
1356 * here we run the risk of double counting! We proceed in a similar
1357 * way as we did above for the other bonded interactions: */
1358 for (i = F_LJ14; i < F_COUL14; i++)
1360 nratoms = interaction_function[i].nratoms;
1362 while (j < molt->ilist[i].nr)
1364 a1 = molt->ilist[i].iatoms[j+1];
1365 a2 = molt->ilist[i].iatoms[j+2];
1366 bexcl = ((bQMMM[a1] && bQMMM[a2]) ||
1367 (blink[a1] && bQMMM[a2]) ||
1368 (bQMMM[a1] && blink[a2]));
1371 /* since the interaction involves QM atoms, these should be
1372 * removed from the MM ilist
1374 molt->ilist[i].nr -= (nratoms+1);
1375 for (k = j; k < molt->ilist[i].nr; k++)
1377 molt->ilist[i].iatoms[k] = molt->ilist[i].iatoms[k+(nratoms+1)];
1382 j += nratoms+1; /* the +1 is for the functype */
1391 } /* generate_qmexcl */
1393 void generate_qmexcl(gmx_mtop_t *sys, t_inputrec *ir, warninp_t wi)
1395 /* This routine expects molt->molt[m].ilist to be of size F_NRE and ordered.
1398 unsigned char *grpnr;
1399 int mb, mol, nat_mol, i, nr_mol_with_qm_atoms = 0;
1400 gmx_molblock_t *molb;
1403 grpnr = sys->groups.grpnr[egcQMMM];
1405 for (mb = 0; mb < sys->nmolblock; mb++)
1407 molb = &sys->molblock[mb];
1408 nat_mol = sys->moltype[molb->type].atoms.nr;
1409 for (mol = 0; mol < molb->nmol; mol++)
1412 for (i = 0; i < nat_mol; i++)
1414 if ((grpnr ? grpnr[i] : 0) < ir->opts.ngQM)
1421 nr_mol_with_qm_atoms++;
1424 /* We need to split this molblock */
1427 /* Split the molblock at this molecule */
1429 srenew(sys->molblock, sys->nmolblock);
1430 for (i = sys->nmolblock-2; i >= mb; i--)
1432 sys->molblock[i+1] = sys->molblock[i];
1434 sys->molblock[mb ].nmol = mol;
1435 sys->molblock[mb+1].nmol -= mol;
1437 molb = &sys->molblock[mb];
1441 /* Split the molblock after this molecule */
1443 srenew(sys->molblock, sys->nmolblock);
1444 molb = &sys->molblock[mb];
1445 for (i = sys->nmolblock-2; i >= mb; i--)
1447 sys->molblock[i+1] = sys->molblock[i];
1449 sys->molblock[mb ].nmol = 1;
1450 sys->molblock[mb+1].nmol -= 1;
1453 /* Add a moltype for the QMMM molecule */
1455 srenew(sys->moltype, sys->nmoltype);
1456 /* Copy the moltype struct */
1457 sys->moltype[sys->nmoltype-1] = sys->moltype[molb->type];
1458 /* Copy the exclusions to a new array, since this is the only
1459 * thing that needs to be modified for QMMM.
1461 copy_blocka(&sys->moltype[molb->type ].excls,
1462 &sys->moltype[sys->nmoltype-1].excls);
1463 /* Set the molecule type for the QMMM molblock */
1464 molb->type = sys->nmoltype - 1;
1466 generate_qmexcl_moltype(&sys->moltype[molb->type], grpnr, ir);
1474 if (nr_mol_with_qm_atoms > 1)
1476 /* generate a warning is there are QM atoms in different
1477 * topologies. In this case it is not possible at this stage to
1478 * mutualy exclude the non-bonded interactions via the
1479 * exclusions (AFAIK). Instead, the user is advised to use the
1480 * energy group exclusions in the mdp file
1483 "\nThe QM subsystem is divided over multiple topologies. "
1484 "The mutual non-bonded interactions cannot be excluded. "
1485 "There are two ways to achieve this:\n\n"
1486 "1) merge the topologies, such that the atoms of the QM "
1487 "subsystem are all present in one single topology file. "
1488 "In this case this warning will dissappear\n\n"
1489 "2) exclude the non-bonded interactions explicitly via the "
1490 "energygrp-excl option in the mdp file. if this is the case "
1491 "this warning may be ignored"