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/utility/futil.h"
53 #include "gromacs/utility/smalloc.h"
55 #include "gromacs/fileio/gmxfio.h"
57 #include "gromacs/math/units.h"
60 #include "gromacs/utility/cstringutil.h"
61 #include "gromacs/topology/block.h"
62 #include "gromacs/topology/symtab.h"
63 #include "gromacs/topology/topology.h"
64 #include "gromacs/utility/fatalerror.h"
66 #include "vsite_parm.h"
68 #include "grompp-impl.h"
72 #include "gpp_nextnb.h"
76 #include "gpp_bond_atomtype.h"
78 #include "gromacs/math/utilities.h"
80 #define OPENDIR '[' /* starting sign for directive */
81 #define CLOSEDIR ']' /* ending sign for directive */
83 static void free_nbparam(t_nbparam **param, int nr)
87 for (i = 0; i < nr; i++)
94 static int copy_nbparams(t_nbparam **param, int ftype, t_params *plist, int nr)
102 for (i = 0; i < nr; i++)
104 for (j = 0; j <= i; j++)
106 if (param[i][j].bSet)
108 for (f = 0; f < nrfp; f++)
110 plist->param[nr*i+j].c[f] = param[i][j].c[f];
111 plist->param[nr*j+i].c[f] = param[i][j].c[f];
121 static void gen_pairs(t_params *nbs, t_params *pairs, real fudge, int comb)
123 int i, j, ntp, nrfp, nrfpA, nrfpB, nnn;
128 nrfpA = interaction_function[F_LJ14].nrfpA;
129 nrfpB = interaction_function[F_LJ14].nrfpB;
132 if ((nrfp != nrfpA) || (nrfpA != nrfpB))
134 gmx_incons("Number of force parameters in gen_pairs wrong");
137 fprintf(stderr, "Generating 1-4 interactions: fudge = %g\n", fudge);
140 fprintf(debug, "Fudge factor for 1-4 interactions: %g\n", fudge);
141 fprintf(debug, "Holy Cow! there are %d types\n", ntp);
143 snew(pairs->param, pairs->nr);
144 for (i = 0; (i < ntp); i++)
147 pairs->param[i].a[0] = i / nnn;
148 pairs->param[i].a[1] = i % nnn;
149 /* Copy normal and FEP parameters and multiply by fudge factor */
153 for (j = 0; (j < nrfp); j++)
155 /* If we are using sigma/epsilon values, only the epsilon values
156 * should be scaled, but not sigma.
157 * The sigma values have even indices 0,2, etc.
159 if ((comb == eCOMB_ARITHMETIC || comb == eCOMB_GEOM_SIG_EPS) && (j%2 == 0))
168 pairs->param[i].c[j] = scaling*nbs->param[i].c[j];
169 pairs->param[i].c[nrfp+j] = scaling*nbs->param[i].c[j];
174 double check_mol(gmx_mtop_t *mtop, warninp_t wi)
177 int i, mb, nmol, ri, pt;
182 /* Check mass and charge */
185 for (mb = 0; mb < mtop->nmoltype; mb++)
187 atoms = &mtop->moltype[mtop->molblock[mb].type].atoms;
188 nmol = mtop->molblock[mb].nmol;
189 for (i = 0; (i < atoms->nr); i++)
191 q += nmol*atoms->atom[i].q;
192 m = atoms->atom[i].m;
193 pt = atoms->atom[i].ptype;
194 /* If the particle is an atom or a nucleus it must have a mass,
195 * else, if it is a shell, a vsite or a bondshell it can have mass zero
197 if ((m <= 0.0) && ((pt == eptAtom) || (pt == eptNucleus)))
199 ri = atoms->atom[i].resind;
200 sprintf(buf, "atom %s (Res %s-%d) has mass %g\n",
201 *(atoms->atomname[i]),
202 *(atoms->resinfo[ri].name),
203 atoms->resinfo[ri].nr,
205 warning_error(wi, buf);
208 if ((m != 0) && (pt == eptVSite))
210 ri = atoms->atom[i].resind;
211 sprintf(buf, "virtual site %s (Res %s-%d) has non-zero mass %g\n"
212 " Check your topology.\n",
213 *(atoms->atomname[i]),
214 *(atoms->resinfo[ri].name),
215 atoms->resinfo[ri].nr,
217 warning_error(wi, buf);
218 /* The following statements make LINCS break! */
219 /* atoms->atom[i].m=0; */
226 static void sum_q(t_atoms *atoms, int n, double *qt, double *qBt)
234 for (i = 0; i < atoms->nr; i++)
236 qmolA += atoms->atom[i].q;
237 qmolB += atoms->atom[i].qB;
239 /* Unfortunately an absolute comparison,
240 * but this avoids unnecessary warnings and gmx-users mails.
242 if (fabs(qmolA) >= 1e-6 || fabs(qmolB) >= 1e-6)
249 static void get_nbparm(char *nb_str, char *comb_str, int *nb, int *comb,
253 char warn_buf[STRLEN];
256 for (i = 1; (i < eNBF_NR); i++)
258 if (gmx_strcasecmp(nb_str, enbf_names[i]) == 0)
265 *nb = strtol(nb_str, NULL, 10);
267 if ((*nb < 1) || (*nb >= eNBF_NR))
269 sprintf(warn_buf, "Invalid nonbond function selector '%s' using %s",
270 nb_str, enbf_names[1]);
271 warning_error(wi, warn_buf);
275 for (i = 1; (i < eCOMB_NR); i++)
277 if (gmx_strcasecmp(comb_str, ecomb_names[i]) == 0)
284 *comb = strtol(comb_str, NULL, 10);
286 if ((*comb < 1) || (*comb >= eCOMB_NR))
288 sprintf(warn_buf, "Invalid combination rule selector '%s' using %s",
289 comb_str, ecomb_names[1]);
290 warning_error(wi, warn_buf);
295 static char ** cpp_opts(const char *define, const char *include,
300 const char *cppadds[2];
301 char **cppopts = NULL;
302 const char *option[2] = { "-D", "-I" };
303 const char *nopt[2] = { "define", "include" };
307 char warn_buf[STRLEN];
310 cppadds[1] = include;
311 for (n = 0; (n < 2); n++)
318 while ((*ptr != '\0') && isspace(*ptr))
323 while ((*rptr != '\0') && !isspace(*rptr))
331 strncpy(buf, ptr, len);
332 if (strstr(ptr, option[n]) != ptr)
334 set_warning_line(wi, "mdp file", -1);
335 sprintf(warn_buf, "Malformed %s option %s", nopt[n], buf);
336 warning(wi, warn_buf);
340 srenew(cppopts, ++ncppopts);
341 cppopts[ncppopts-1] = strdup(buf);
349 srenew(cppopts, ++ncppopts);
350 cppopts[ncppopts-1] = NULL;
357 find_gb_bondlength(t_params *plist, int ai, int aj, real *length)
364 for (i = 0; i < F_NRE && !found; i++)
368 for (j = 0; j < plist[i].nr; j++)
370 a1 = plist[i].param[j].a[0];
371 a2 = plist[i].param[j].a[1];
373 if ( (a1 == ai && a2 == aj) || (a1 == aj && a2 == ai))
375 /* Equilibrium bond distance */
376 *length = plist[i].param[j].c[0];
389 find_gb_anglelength(t_params *plist, int ai, int ak, real *length)
391 int i, j, a1, a2, a3;
394 int status, status1, status2;
398 for (i = 0; i < F_NRE && !found; i++)
402 for (j = 0; j < plist[i].nr; j++)
404 a1 = plist[i].param[j].a[0];
405 a2 = plist[i].param[j].a[1];
406 a3 = plist[i].param[j].a[2];
408 /* We dont care what the middle atom is, but use it below */
409 if ( (a1 == ai && a3 == ak) || (a1 == ak && a3 == ai) )
411 /* Equilibrium bond distance */
412 a123 = plist[i].param[j].c[0];
413 /* Use middle atom to find reference distances r12 and r23 */
414 status1 = find_gb_bondlength(plist, a1, a2, &r12);
415 status2 = find_gb_bondlength(plist, a2, a3, &r23);
417 if (status1 == 0 && status2 == 0)
419 /* cosine theorem to get r13 */
420 *length = sqrt(r12*r12+r23*r23-(2*r12*r23*cos(a123/RAD2DEG)));
433 generate_gb_exclusion_interactions(t_molinfo *mi, gpp_atomtype_t atype, t_nextnb *nnb)
435 int i, j, k, n, ai, aj, ti, tj;
441 real radiusi, radiusj;
442 real gb_radiusi, gb_radiusj;
443 real param_c2, param_c4;
449 for (n = 1; n <= nnb->nrex; n++)
464 /* Put all higher-order exclusions into 1,4 list so we dont miss them */
471 for (ai = 0; ai < nnb->nr; ai++)
473 ti = at->atom[ai].type;
474 radiusi = get_atomtype_radius(ti, atype);
475 gb_radiusi = get_atomtype_gb_radius(ti, atype);
477 for (j = 0; j < nnb->nrexcl[ai][n]; j++)
479 aj = nnb->a[ai][n][j];
481 /* Only add the interactions once */
484 tj = at->atom[aj].type;
485 radiusj = get_atomtype_radius(tj, atype);
486 gb_radiusj = get_atomtype_gb_radius(tj, atype);
488 /* There is an exclusion of type "ftype" between atoms ai and aj */
492 /* Reference distance, not used for 1-4 interactions */
496 if (find_gb_bondlength(plist, ai, aj, &distance) != 0)
498 gmx_fatal(FARGS, "Cannot find bond length for atoms %d-%d", ai, aj);
502 if (find_gb_anglelength(plist, ai, aj, &distance) != 0)
504 gmx_fatal(FARGS, "Cannot find length for atoms %d-%d involved in angle", ai, aj);
511 /* Assign GB parameters */
513 param.c[0] = radiusi+radiusj;
514 /* Reference distance distance */
515 param.c[1] = distance;
516 /* Still parameter */
517 param.c[2] = param_c2;
519 param.c[3] = gb_radiusi+gb_radiusj;
521 param.c[4] = param_c4;
523 /* Add it to the parameter list */
524 add_param_to_list(&plist[ftype], ¶m);
535 static char **read_topol(const char *infile, const char *outfile,
536 const char *define, const char *include,
538 gpp_atomtype_t atype,
542 int *combination_rule,
547 gmx_molblock_t **molblock,
554 int i, sl, nb_funct, comb;
555 char *pline = NULL, **title = NULL;
556 char line[STRLEN], errbuf[256], comb_str[256], nb_str[256];
558 char *dirstr, *dummy2;
559 int nrcopies, nmol, nmolb = 0, nscan, ncombs, ncopy;
560 double fLJ, fQQ, fPOW;
561 gmx_molblock_t *molb = NULL;
562 t_topology *block = NULL;
563 t_molinfo *mi0 = NULL;
566 t_nbparam **nbparam, **pair;
568 real fudgeLJ = -1; /* Multiplication factor to generate 1-4 from LJ */
569 gmx_bool bReadDefaults, bReadMolType, bGenPairs, bWarn_copy_A_B;
570 double qt = 0, qBt = 0; /* total charge */
571 t_bond_atomtype batype;
573 int dcatt = -1, nmol_couple;
574 /* File handling variables */
577 char *tmp_line = NULL;
578 char warn_buf[STRLEN];
579 const char *floating_point_arithmetic_tip =
580 "Total charge should normally be an integer. See\n"
581 "http://www.gromacs.org/Documentation/Floating_Point_Arithmetic\n"
582 "for discussion on how close it should be to an integer.\n";
583 /* We need to open the output file before opening the input file,
584 * because cpp_open_file can change the current working directory.
588 out = gmx_fio_fopen(outfile, "w");
595 /* open input file */
596 status = cpp_open_file(infile, &handle, cpp_opts(define, include, wi));
599 gmx_fatal(FARGS, cpp_error(&handle, status));
602 /* some local variables */
603 DS_Init(&DS); /* directive stack */
604 nmol = 0; /* no molecules yet... */
605 d = d_invalid; /* first thing should be a directive */
606 nbparam = NULL; /* The temporary non-bonded matrix */
607 pair = NULL; /* The temporary pair interaction matrix */
608 block2 = NULL; /* the extra exclusions */
610 *reppow = 12.0; /* Default value for repulsion power */
614 /* Init the number of CMAP torsion angles and grid spacing */
615 plist->grid_spacing = 0;
618 bWarn_copy_A_B = bFEP;
620 batype = init_bond_atomtype();
621 /* parse the actual file */
622 bReadDefaults = FALSE;
624 bReadMolType = FALSE;
629 status = cpp_read_line(&handle, STRLEN, line);
630 done = (status == eCPP_EOF);
633 if (status != eCPP_OK)
635 gmx_fatal(FARGS, cpp_error(&handle, status));
639 fprintf(out, "%s\n", line);
642 set_warning_line(wi, cpp_cur_file(&handle), cpp_cur_linenr(&handle));
644 pline = strdup(line);
646 /* Strip trailing '\' from pline, if it exists */
648 if ((sl > 0) && (pline[sl-1] == CONTINUE))
653 /* build one long line from several fragments - necessary for CMAP */
654 while (continuing(line))
656 status = cpp_read_line(&handle, STRLEN, line);
657 set_warning_line(wi, cpp_cur_file(&handle), cpp_cur_linenr(&handle));
659 /* Since we depend on the '\' being present to continue to read, we copy line
660 * to a tmp string, strip the '\' from that string, and cat it to pline
662 tmp_line = strdup(line);
664 sl = strlen(tmp_line);
665 if ((sl > 0) && (tmp_line[sl-1] == CONTINUE))
667 tmp_line[sl-1] = ' ';
670 done = (status == eCPP_EOF);
673 if (status != eCPP_OK)
675 gmx_fatal(FARGS, cpp_error(&handle, status));
679 fprintf(out, "%s\n", line);
683 srenew(pline, strlen(pline)+strlen(tmp_line)+1);
684 strcat(pline, tmp_line);
688 /* skip trailing and leading spaces and comment text */
689 strip_comment (pline);
692 /* if there is something left... */
693 if ((int)strlen(pline) > 0)
695 if (pline[0] == OPENDIR)
697 /* A directive on this line: copy the directive
698 * without the brackets into dirstr, then
699 * skip spaces and tabs on either side of directive
701 dirstr = strdup((pline+1));
702 if ((dummy2 = strchr (dirstr, CLOSEDIR)) != NULL)
708 if ((newd = str2dir(dirstr)) == d_invalid)
710 sprintf(errbuf, "Invalid directive %s", dirstr);
711 warning_error(wi, errbuf);
715 /* Directive found */
718 fprintf(debug, "found directive '%s'\n", dir2str(newd));
720 if (DS_Check_Order (DS, newd))
727 /* we should print here which directives should have
728 been present, and which actually are */
729 gmx_fatal(FARGS, "%s\nInvalid order for directive %s",
730 cpp_error(&handle, eCPP_SYNTAX), dir2str(newd));
736 else if (d != d_invalid)
738 /* Not a directive, just a plain string
739 * use a gigantic switch to decode,
740 * if there is a valid directive!
747 gmx_fatal(FARGS, "%s\nFound a second defaults directive.\n",
748 cpp_error(&handle, eCPP_SYNTAX));
750 bReadDefaults = TRUE;
751 nscan = sscanf(pline, "%s%s%s%lf%lf%lf",
752 nb_str, comb_str, genpairs, &fLJ, &fQQ, &fPOW);
763 get_nbparm(nb_str, comb_str, &nb_funct, &comb, wi);
764 *combination_rule = comb;
767 bGenPairs = (gmx_strncasecmp(genpairs, "Y", 1) == 0);
768 if (nb_funct != eNBF_LJ && bGenPairs)
770 gmx_fatal(FARGS, "Generating pair parameters is only supported with LJ non-bonded interactions");
786 nb_funct = ifunc_index(d_nonbond_params, nb_funct);
790 push_at(symtab, atype, batype, pline, nb_funct,
791 &nbparam, bGenPairs ? &pair : NULL, wi);
795 push_bt(d, plist, 2, NULL, batype, pline, wi);
797 case d_constrainttypes:
798 push_bt(d, plist, 2, NULL, batype, pline, wi);
803 push_nbt(d, pair, atype, pline, F_LJ14, wi);
807 push_bt(d, plist, 2, atype, NULL, pline, wi);
811 push_bt(d, plist, 3, NULL, batype, pline, wi);
813 case d_dihedraltypes:
814 /* Special routine that can read both 2 and 4 atom dihedral definitions. */
815 push_dihedraltype(d, plist, batype, pline, wi);
818 case d_nonbond_params:
819 push_nbt(d, nbparam, atype, pline, nb_funct, wi);
824 srenew(block,nblock);
825 srenew(blockinfo,nblock);
826 blk0=&(block[nblock-1]);
827 bi0=&(blockinfo[nblock-1]);
830 push_molt(symtab,bi0,pline);
834 case d_implicit_genborn_params:
835 push_gb_params(atype, pline, wi);
838 case d_implicit_surface_params:
839 gmx_fatal(FARGS, "Implicit surface directive not supported yet.");
843 push_cmaptype(d, plist, 5, atype, batype, pline, wi);
851 if (opts->couple_moltype != NULL &&
852 (opts->couple_lam0 == ecouplamNONE ||
853 opts->couple_lam0 == ecouplamQ ||
854 opts->couple_lam1 == ecouplamNONE ||
855 opts->couple_lam1 == ecouplamQ))
857 dcatt = add_atomtype_decoupled(symtab, atype,
858 &nbparam, bGenPairs ? &pair : NULL);
860 ntype = get_atomtype_ntypes(atype);
861 ncombs = (ntype*(ntype+1))/2;
862 generate_nbparams(comb, nb_funct, &(plist[nb_funct]), atype, wi);
863 ncopy = copy_nbparams(nbparam, nb_funct, &(plist[nb_funct]),
865 fprintf(stderr, "Generated %d of the %d non-bonded parameter combinations\n", ncombs-ncopy, ncombs);
866 free_nbparam(nbparam, ntype);
869 gen_pairs(&(plist[nb_funct]), &(plist[F_LJ14]), fudgeLJ, comb);
870 ncopy = copy_nbparams(pair, nb_funct, &(plist[F_LJ14]),
872 fprintf(stderr, "Generated %d of the %d 1-4 parameter combinations\n", ncombs-ncopy, ncombs);
873 free_nbparam(pair, ntype);
875 /* Copy GBSA parameters to atomtype array? */
880 push_molt(symtab, &nmol, molinfo, pline, wi);
881 srenew(block2, nmol);
882 block2[nmol-1].nr = 0;
883 mi0 = &((*molinfo)[nmol-1]);
887 push_atom(symtab, &(mi0->cgs), &(mi0->atoms), atype, pline, &lastcg, wi);
891 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, FALSE,
892 bGenPairs, *fudgeQQ, bZero, &bWarn_copy_A_B, wi);
902 case d_position_restraints:
903 case d_angle_restraints:
904 case d_angle_restraints_z:
905 case d_distance_restraints:
906 case d_orientation_restraints:
907 case d_dihedral_restraints:
910 case d_water_polarization:
911 case d_thole_polarization:
912 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, TRUE,
913 bGenPairs, *fudgeQQ, bZero, &bWarn_copy_A_B, wi);
916 push_cmap(d, plist, mi0->plist, &(mi0->atoms), atype, pline, wi);
920 push_vsitesn(d, mi0->plist, &(mi0->atoms), pline, wi);
924 if (!block2[nmol-1].nr)
926 init_block2(&(block2[nmol-1]), mi0->atoms.nr);
928 push_excl(pline, &(block2[nmol-1]));
932 title = put_symtab(symtab, pline);
939 push_mol(nmol, *molinfo, pline, &whichmol, &nrcopies, wi);
940 mi0 = &((*molinfo)[whichmol]);
941 srenew(molb, nmolb+1);
942 molb[nmolb].type = whichmol;
943 molb[nmolb].nmol = nrcopies;
946 bCouple = (opts->couple_moltype != NULL &&
947 (gmx_strcasecmp("system", opts->couple_moltype) == 0 ||
948 gmx_strcasecmp(*(mi0->name), opts->couple_moltype) == 0));
951 nmol_couple += nrcopies;
954 if (mi0->atoms.nr == 0)
956 gmx_fatal(FARGS, "Molecule type '%s' contains no atoms",
960 "Excluding %d bonded neighbours molecule type '%s'\n",
961 mi0->nrexcl, *mi0->name);
962 sum_q(&mi0->atoms, nrcopies, &qt, &qBt);
963 if (!mi0->bProcessed)
966 generate_excl(mi0->nrexcl,
971 merge_excl(&(mi0->excls), &(block2[whichmol]));
972 done_block2(&(block2[whichmol]));
973 make_shake(mi0->plist, &mi0->atoms, opts->nshake);
977 /* nnb contains information about first,2nd,3rd bonded neighbors.
978 * Use this to generate GB 1-2,1-3,1-4 interactions when necessary.
980 if (bGenborn == TRUE)
982 generate_gb_exclusion_interactions(mi0, atype, &nnb);
989 convert_moltype_couple(mi0, dcatt, *fudgeQQ,
990 opts->couple_lam0, opts->couple_lam1,
992 nb_funct, &(plist[nb_funct]));
994 stupid_fill_block(&mi0->mols, mi0->atoms.nr, TRUE);
995 mi0->bProcessed = TRUE;
1000 fprintf (stderr, "case: %d\n", d);
1001 gmx_incons("unknown directive");
1010 status = cpp_close_file(&handle);
1011 if (status != eCPP_OK)
1013 gmx_fatal(FARGS, cpp_error(&handle, status));
1018 gmx_fio_fclose(out);
1021 if (opts->couple_moltype)
1023 if (nmol_couple == 0)
1025 gmx_fatal(FARGS, "Did not find any molecules of type '%s' for coupling",
1026 opts->couple_moltype);
1028 fprintf(stderr, "Coupling %d copies of molecule type '%s'\n",
1029 nmol_couple, opts->couple_moltype);
1032 /* this is not very clean, but fixes core dump on empty system name */
1035 title = put_symtab(symtab, "");
1037 if (fabs(qt) > 1e-4)
1039 sprintf(warn_buf, "System has non-zero total charge: %.6f\n%s\n", qt, floating_point_arithmetic_tip);
1040 warning_note(wi, warn_buf);
1042 if (fabs(qBt) > 1e-4 && !gmx_within_tol(qBt, qt, 1e-6))
1044 sprintf(warn_buf, "State B has non-zero total charge: %.6f\n%s\n", qBt, floating_point_arithmetic_tip);
1045 warning_note(wi, warn_buf);
1048 for (i = 0; i < nmol; i++)
1050 done_block2(&(block2[i]));
1054 done_bond_atomtype(&batype);
1064 char **do_top(gmx_bool bVerbose,
1065 const char *topfile,
1066 const char *topppfile,
1071 int *combination_rule,
1072 double *repulsion_power,
1074 gpp_atomtype_t atype,
1076 t_molinfo **molinfo,
1079 gmx_molblock_t **molblock,
1083 /* Tmpfile might contain a long path */
1084 const char *tmpfile;
1089 tmpfile = topppfile;
1098 printf("processing topology...\n");
1100 title = read_topol(topfile, tmpfile, opts->define, opts->include,
1101 symtab, atype, nrmols, molinfo,
1102 plist, combination_rule, repulsion_power,
1103 opts, fudgeQQ, nmolblock, molblock,
1104 ir->efep != efepNO, bGenborn, bZero, wi);
1105 if ((*combination_rule != eCOMB_GEOMETRIC) &&
1106 (ir->vdwtype == evdwUSER))
1108 warning(wi, "Using sigma/epsilon based combination rules with"
1109 " user supplied potential function may produce unwanted"
1117 static void generate_qmexcl_moltype(gmx_moltype_t *molt, unsigned char *grpnr,
1120 /* This routine expects molt->ilist to be of size F_NRE and ordered. */
1122 /* generates the exclusions between the individual QM atoms, as
1123 * these interactions should be handled by the QM subroutines and
1124 * not by the gromacs routines
1127 i, j, l, k = 0, jmax, qm_max = 0, qm_nr = 0, nratoms = 0, link_nr = 0, link_max = 0;
1129 *qm_arr = NULL, *link_arr = NULL, a1, a2, a3, a4, ftype = 0;
1135 *bQMMM, *blink, bexcl;
1137 /* First we search and select the QM atoms in an qm_arr array that
1138 * we use to create the exclusions.
1140 * we take the possibility into account that a user has defined more
1141 * than one QM group:
1143 * for that we also need to do this an ugly work-about just in case
1144 * the QM group contains the entire system...
1146 jmax = ir->opts.ngQM;
1148 /* we first search for all the QM atoms and put them in an array
1150 for (j = 0; j < jmax; j++)
1152 for (i = 0; i < molt->atoms.nr; i++)
1154 if (qm_nr >= qm_max)
1157 srenew(qm_arr, qm_max);
1159 if ((grpnr ? grpnr[i] : 0) == j)
1161 qm_arr[qm_nr++] = i;
1165 /* bQMMM[..] is an array containin TRUE/FALSE for atoms that are
1166 * QM/not QM. We first set all elements to false. Afterwards we use
1167 * the qm_arr to change the elements corresponding to the QM atoms
1170 snew(bQMMM, molt->atoms.nr);
1171 for (i = 0; i < molt->atoms.nr; i++)
1175 for (i = 0; i < qm_nr; i++)
1177 bQMMM[qm_arr[i]] = TRUE;
1180 /* We remove all bonded interactions (i.e. bonds,
1181 * angles, dihedrals, 1-4's), involving the QM atoms. The way they
1182 * are removed is as follows: if the interaction invloves 2 atoms,
1183 * it is removed if both atoms are QMatoms. If it involves 3 atoms,
1184 * it is removed if at least two of the atoms are QM atoms, if the
1185 * interaction involves 4 atoms, it is removed if there are at least
1186 * 2 QM atoms. Since this routine is called once before any forces
1187 * are computed, the top->idef.il[N].iatom[] array (see idef.h) can
1188 * be rewritten at this poitn without any problem. 25-9-2002 */
1190 /* first check weter we already have CONNBONDS: */
1191 if (molt->ilist[F_CONNBONDS].nr != 0)
1193 fprintf(stderr, "nr. of CONNBONDS present already: %d\n",
1194 molt->ilist[F_CONNBONDS].nr/3);
1195 ftype = molt->ilist[F_CONNBONDS].iatoms[0];
1196 k = molt->ilist[F_CONNBONDS].nr;
1198 /* now we delete all bonded interactions, except the ones describing
1199 * a chemical bond. These are converted to CONNBONDS
1201 for (i = 0; i < F_LJ; i++)
1203 if (i == F_CONNBONDS)
1207 nratoms = interaction_function[i].nratoms;
1209 while (j < molt->ilist[i].nr)
1215 a1 = molt->ilist[i].iatoms[j+1];
1216 a2 = molt->ilist[i].iatoms[j+2];
1217 bexcl = (bQMMM[a1] && bQMMM[a2]);
1218 /* a bonded beteen two QM atoms will be copied to the
1219 * CONNBONDS list, for reasons mentioned above
1221 if (bexcl && i < F_ANGLES)
1223 srenew(molt->ilist[F_CONNBONDS].iatoms, k+3);
1224 molt->ilist[F_CONNBONDS].nr += 3;
1225 molt->ilist[F_CONNBONDS].iatoms[k++] = ftype;
1226 molt->ilist[F_CONNBONDS].iatoms[k++] = a1;
1227 molt->ilist[F_CONNBONDS].iatoms[k++] = a2;
1231 a1 = molt->ilist[i].iatoms[j+1];
1232 a2 = molt->ilist[i].iatoms[j+2];
1233 a3 = molt->ilist[i].iatoms[j+3];
1234 bexcl = ((bQMMM[a1] && bQMMM[a2]) ||
1235 (bQMMM[a1] && bQMMM[a3]) ||
1236 (bQMMM[a2] && bQMMM[a3]));
1239 a1 = molt->ilist[i].iatoms[j+1];
1240 a2 = molt->ilist[i].iatoms[j+2];
1241 a3 = molt->ilist[i].iatoms[j+3];
1242 a4 = molt->ilist[i].iatoms[j+4];
1243 bexcl = ((bQMMM[a1] && bQMMM[a2] && bQMMM[a3]) ||
1244 (bQMMM[a1] && bQMMM[a2] && bQMMM[a4]) ||
1245 (bQMMM[a1] && bQMMM[a3] && bQMMM[a4]) ||
1246 (bQMMM[a2] && bQMMM[a3] && bQMMM[a4]));
1249 gmx_fatal(FARGS, "no such bonded interactions with %d atoms\n", nratoms);
1253 /* since the interaction involves QM atoms, these should be
1254 * removed from the MM ilist
1256 molt->ilist[i].nr -= (nratoms+1);
1257 for (l = j; l < molt->ilist[i].nr; l++)
1259 molt->ilist[i].iatoms[l] = molt->ilist[i].iatoms[l+(nratoms+1)];
1264 j += nratoms+1; /* the +1 is for the functype */
1268 /* Now, we search for atoms bonded to a QM atom because we also want
1269 * to exclude their nonbonded interactions with the QM atoms. The
1270 * reason for this is that this interaction is accounted for in the
1271 * linkatoms interaction with the QMatoms and would be counted
1274 for (i = 0; i < F_NRE; i++)
1279 while (j < molt->ilist[i].nr)
1281 a1 = molt->ilist[i].iatoms[j+1];
1282 a2 = molt->ilist[i].iatoms[j+2];
1283 if ((bQMMM[a1] && !bQMMM[a2]) || (!bQMMM[a1] && bQMMM[a2]))
1285 if (link_nr >= link_max)
1288 srenew(link_arr, link_max);
1292 link_arr[link_nr++] = a2;
1296 link_arr[link_nr++] = a1;
1303 snew(blink, molt->atoms.nr);
1304 for (i = 0; i < molt->atoms.nr; i++)
1308 for (i = 0; i < link_nr; i++)
1310 blink[link_arr[i]] = TRUE;
1312 /* creating the exclusion block for the QM atoms. Each QM atom has
1313 * as excluded elements all the other QMatoms (and itself).
1315 qmexcl.nr = molt->atoms.nr;
1316 qmexcl.nra = qm_nr*(qm_nr+link_nr)+link_nr*qm_nr;
1317 snew(qmexcl.index, qmexcl.nr+1);
1318 snew(qmexcl.a, qmexcl.nra);
1320 for (i = 0; i < qmexcl.nr; i++)
1322 qmexcl.index[i] = j;
1325 for (k = 0; k < qm_nr; k++)
1327 qmexcl.a[k+j] = qm_arr[k];
1329 for (k = 0; k < link_nr; k++)
1331 qmexcl.a[qm_nr+k+j] = link_arr[k];
1333 j += (qm_nr+link_nr);
1337 for (k = 0; k < qm_nr; k++)
1339 qmexcl.a[k+j] = qm_arr[k];
1344 qmexcl.index[qmexcl.nr] = j;
1346 /* and merging with the exclusions already present in sys.
1349 init_block2(&qmexcl2, molt->atoms.nr);
1350 b_to_b2(&qmexcl, &qmexcl2);
1351 merge_excl(&(molt->excls), &qmexcl2);
1352 done_block2(&qmexcl2);
1354 /* Finally, we also need to get rid of the pair interactions of the
1355 * classical atom bonded to the boundary QM atoms with the QMatoms,
1356 * as this interaction is already accounted for by the QM, so also
1357 * here we run the risk of double counting! We proceed in a similar
1358 * way as we did above for the other bonded interactions: */
1359 for (i = F_LJ14; i < F_COUL14; i++)
1361 nratoms = interaction_function[i].nratoms;
1363 while (j < molt->ilist[i].nr)
1365 a1 = molt->ilist[i].iatoms[j+1];
1366 a2 = molt->ilist[i].iatoms[j+2];
1367 bexcl = ((bQMMM[a1] && bQMMM[a2]) ||
1368 (blink[a1] && bQMMM[a2]) ||
1369 (bQMMM[a1] && blink[a2]));
1372 /* since the interaction involves QM atoms, these should be
1373 * removed from the MM ilist
1375 molt->ilist[i].nr -= (nratoms+1);
1376 for (k = j; k < molt->ilist[i].nr; k++)
1378 molt->ilist[i].iatoms[k] = molt->ilist[i].iatoms[k+(nratoms+1)];
1383 j += nratoms+1; /* the +1 is for the functype */
1392 } /* generate_qmexcl */
1394 void generate_qmexcl(gmx_mtop_t *sys, t_inputrec *ir, warninp_t wi)
1396 /* This routine expects molt->molt[m].ilist to be of size F_NRE and ordered.
1399 unsigned char *grpnr;
1400 int mb, mol, nat_mol, i, nr_mol_with_qm_atoms = 0;
1401 gmx_molblock_t *molb;
1404 grpnr = sys->groups.grpnr[egcQMMM];
1406 for (mb = 0; mb < sys->nmolblock; mb++)
1408 molb = &sys->molblock[mb];
1409 nat_mol = sys->moltype[molb->type].atoms.nr;
1410 for (mol = 0; mol < molb->nmol; mol++)
1413 for (i = 0; i < nat_mol; i++)
1415 if ((grpnr ? grpnr[i] : 0) < ir->opts.ngQM)
1422 nr_mol_with_qm_atoms++;
1425 /* We need to split this molblock */
1428 /* Split the molblock at this molecule */
1430 srenew(sys->molblock, sys->nmolblock);
1431 for (i = sys->nmolblock-2; i >= mb; i--)
1433 sys->molblock[i+1] = sys->molblock[i];
1435 sys->molblock[mb ].nmol = mol;
1436 sys->molblock[mb+1].nmol -= mol;
1438 molb = &sys->molblock[mb];
1442 /* Split the molblock after this molecule */
1444 srenew(sys->molblock, sys->nmolblock);
1445 molb = &sys->molblock[mb];
1446 for (i = sys->nmolblock-2; i >= mb; i--)
1448 sys->molblock[i+1] = sys->molblock[i];
1450 sys->molblock[mb ].nmol = 1;
1451 sys->molblock[mb+1].nmol -= 1;
1454 /* Add a moltype for the QMMM molecule */
1456 srenew(sys->moltype, sys->nmoltype);
1457 /* Copy the moltype struct */
1458 sys->moltype[sys->nmoltype-1] = sys->moltype[molb->type];
1459 /* Copy the exclusions to a new array, since this is the only
1460 * thing that needs to be modified for QMMM.
1462 copy_blocka(&sys->moltype[molb->type ].excls,
1463 &sys->moltype[sys->nmoltype-1].excls);
1464 /* Set the molecule type for the QMMM molblock */
1465 molb->type = sys->nmoltype - 1;
1467 generate_qmexcl_moltype(&sys->moltype[molb->type], grpnr, ir);
1475 if (nr_mol_with_qm_atoms > 1)
1477 /* generate a warning is there are QM atoms in different
1478 * topologies. In this case it is not possible at this stage to
1479 * mutualy exclude the non-bonded interactions via the
1480 * exclusions (AFAIK). Instead, the user is advised to use the
1481 * energy group exclusions in the mdp file
1484 "\nThe QM subsystem is divided over multiple topologies. "
1485 "The mutual non-bonded interactions cannot be excluded. "
1486 "There are two ways to achieve this:\n\n"
1487 "1) merge the topologies, such that the atoms of the QM "
1488 "subsystem are all present in one single topology file. "
1489 "In this case this warning will dissappear\n\n"
1490 "2) exclude the non-bonded interactions explicitly via the "
1491 "energygrp-excl option in the mdp file. if this is the case "
1492 "this warning may be ignored"