Move physics.* to math/units.*
[gromacs.git] / src / gromacs / gmxpreprocess / topio.c
blob40dfb57220e6620423c372d778cd0677c600c1e6
1 /*
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.
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
41 #include <assert.h>
42 #include <ctype.h>
43 #include <errno.h>
44 #include <math.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
49 #include <sys/types.h>
51 #include "gromacs/utility/futil.h"
52 #include "typedefs.h"
53 #include "gromacs/utility/smalloc.h"
54 #include "macros.h"
55 #include "gromacs/fileio/gmxfio.h"
56 #include "txtdump.h"
57 #include "gromacs/math/units.h"
58 #include "macros.h"
59 #include "names.h"
60 #include "gromacs/utility/cstringutil.h"
61 #include "symtab.h"
62 #include "gromacs/utility/fatalerror.h"
63 #include "warninp.h"
64 #include "vsite_parm.h"
66 #include "grompp-impl.h"
67 #include "toputil.h"
68 #include "toppush.h"
69 #include "topdirs.h"
70 #include "gpp_nextnb.h"
71 #include "topio.h"
72 #include "topshake.h"
73 #include "gmxcpp.h"
74 #include "gpp_bond_atomtype.h"
75 #include "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)
83 int i;
85 for (i = 0; i < nr; i++)
87 sfree(param[i]);
89 sfree(param);
92 static int copy_nbparams(t_nbparam **param, int ftype, t_params *plist, int nr)
94 int i, j, f;
95 int nrfp, ncopy;
97 nrfp = NRFP(ftype);
99 ncopy = 0;
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];
111 ncopy++;
116 return ncopy;
119 static void gen_pairs(t_params *nbs, t_params *pairs, real fudge, int comb)
121 int i, j, ntp, nrfp, nrfpA, nrfpB, nnn;
122 real scaling;
123 ntp = nbs->nr;
124 nnn = sqrt(ntp);
125 nrfp = NRFP(F_LJ);
126 nrfpA = interaction_function[F_LJ14].nrfpA;
127 nrfpB = interaction_function[F_LJ14].nrfpB;
128 pairs->nr = ntp;
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);
136 if (debug)
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++)
144 /* Copy param.a */
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))
159 scaling = 1.0;
161 else
163 scaling = fudge;
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)
174 char buf[256];
175 int i, mb, nmol, ri, pt;
176 double q;
177 real m;
178 t_atoms *atoms;
180 /* Check mass and charge */
181 q = 0.0;
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 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) && ((pt == eptAtom) || (pt == eptNucleus)))
197 ri = atoms->atom[i].resind;
198 sprintf(buf, "atom %s (Res %s-%d) has mass %g\n",
199 *(atoms->atomname[i]),
200 *(atoms->resinfo[ri].name),
201 atoms->resinfo[ri].nr,
203 warning_error(wi, buf);
205 else
206 if ((m != 0) && (pt == eptVSite))
208 ri = atoms->atom[i].resind;
209 sprintf(buf, "virtual site %s (Res %s-%d) has non-zero mass %g\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; */
221 return q;
224 static void sum_q(t_atoms *atoms, int n, double *qt, double *qBt)
226 double qmolA, qmolB;
227 int i;
229 /* sum charge */
230 qmolA = 0;
231 qmolB = 0;
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)
242 *qt += n*qmolA;
243 *qBt += n*qmolB;
247 static void get_nbparm(char *nb_str, char *comb_str, int *nb, int *comb,
248 warninp_t wi)
250 int i;
251 char warn_buf[STRLEN];
253 *nb = -1;
254 for (i = 1; (i < eNBF_NR); i++)
256 if (gmx_strcasecmp(nb_str, enbf_names[i]) == 0)
258 *nb = i;
261 if (*nb == -1)
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);
270 *nb = 1;
272 *comb = -1;
273 for (i = 1; (i < eCOMB_NR); i++)
275 if (gmx_strcasecmp(comb_str, ecomb_names[i]) == 0)
277 *comb = i;
280 if (*comb == -1)
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);
289 *comb = 1;
293 static char ** cpp_opts(const char *define, const char *include,
294 warninp_t wi)
296 int n, len;
297 int ncppopts = 0;
298 const char *cppadds[2];
299 char **cppopts = NULL;
300 const char *option[2] = { "-D", "-I" };
301 const char *nopt[2] = { "define", "include" };
302 const char *ptr;
303 const char *rptr;
304 char *buf;
305 char warn_buf[STRLEN];
307 cppadds[0] = define;
308 cppadds[1] = include;
309 for (n = 0; (n < 2); n++)
311 if (cppadds[n])
313 ptr = cppadds[n];
314 while (*ptr != '\0')
316 while ((*ptr != '\0') && isspace(*ptr))
318 ptr++;
320 rptr = ptr;
321 while ((*rptr != '\0') && !isspace(*rptr))
323 rptr++;
325 len = (rptr - ptr);
326 if (len > 2)
328 snew(buf, (len+1));
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);
336 else
338 srenew(cppopts, ++ncppopts);
339 cppopts[ncppopts-1] = strdup(buf);
341 sfree(buf);
342 ptr = rptr;
347 srenew(cppopts, ++ncppopts);
348 cppopts[ncppopts-1] = NULL;
350 return cppopts;
355 find_gb_bondlength(t_params *plist, int ai, int aj, real *length)
357 int i, j, a1, a2;
359 int found = 0;
360 int status;
362 for (i = 0; i < F_NRE && !found; i++)
364 if (IS_CHEMBOND(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];
375 found = 1;
380 status = !found;
382 return status;
387 find_gb_anglelength(t_params *plist, int ai, int ak, real *length)
389 int i, j, a1, a2, a3;
390 real r12, r23, a123;
391 int found = 0;
392 int status, status1, status2;
394 r12 = r23 = 0;
396 for (i = 0; i < F_NRE && !found; i++)
398 if (IS_ANGLE(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)));
419 found = 1;
425 status = !found;
427 return status;
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;
434 int n12, n13, n14;
435 int ftype;
436 t_param param;
437 t_params * plist;
438 t_atoms * at;
439 real radiusi, radiusj;
440 real gb_radiusi, gb_radiusj;
441 real param_c2, param_c4;
442 real distance;
444 plist = mi->plist;
445 at = &mi->atoms;
447 for (n = 1; n <= nnb->nrex; n++)
449 switch (n)
451 case 1:
452 ftype = F_GB12;
453 param_c2 = STILL_P2;
454 param_c4 = 0.8875;
455 break;
456 case 2:
457 ftype = F_GB13;
458 param_c2 = STILL_P3;
459 param_c4 = 0.3516;
460 break;
461 default:
462 /* Put all higher-order exclusions into 1,4 list so we dont miss them */
463 ftype = F_GB14;
464 param_c2 = STILL_P3;
465 param_c4 = 0.3516;
466 break;
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 */
480 if (aj > ai)
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 */
487 param.a[0] = ai;
488 param.a[1] = aj;
490 /* Reference distance, not used for 1-4 interactions */
491 switch (ftype)
493 case F_GB12:
494 if (find_gb_bondlength(plist, ai, aj, &distance) != 0)
496 gmx_fatal(FARGS, "Cannot find bond length for atoms %d-%d", ai, aj);
498 break;
499 case F_GB13:
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);
504 break;
505 default:
506 distance = -1;
507 break;
509 /* Assign GB parameters */
510 /* Sum of radii */
511 param.c[0] = radiusi+radiusj;
512 /* Reference distance distance */
513 param.c[1] = distance;
514 /* Still parameter */
515 param.c[2] = param_c2;
516 /* GB radius */
517 param.c[3] = gb_radiusi+gb_radiusj;
518 /* Parameter */
519 param.c[4] = param_c4;
521 /* Add it to the parameter list */
522 add_param_to_list(&plist[ftype], &param);
527 return 0;
533 static char **read_topol(const char *infile, const char *outfile,
534 const char *define, const char *include,
535 t_symtab *symtab,
536 gpp_atomtype_t atype,
537 int *nrmols,
538 t_molinfo **molinfo,
539 t_params plist[],
540 int *combination_rule,
541 double *reppow,
542 t_gromppopts *opts,
543 real *fudgeQQ,
544 int *nmolblock,
545 gmx_molblock_t **molblock,
546 gmx_bool bFEP,
547 gmx_bool bGenborn,
548 gmx_bool bZero,
549 warninp_t wi)
551 FILE *out;
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];
555 char genpairs[32];
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;
562 DirStack *DS;
563 directive d, newd;
564 t_nbparam **nbparam, **pair;
565 t_block2 *block2;
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;
570 int lastcg = -1;
571 int dcatt = -1, nmol_couple;
572 /* File handling variables */
573 int status, done;
574 gmx_cpp_t handle;
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.
584 if (outfile)
586 out = gmx_fio_fopen(outfile, "w");
588 else
590 out = NULL;
593 /* open input file */
594 status = cpp_open_file(infile, &handle, cpp_opts(define, include, wi));
595 if (status != 0)
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 */
607 nb_funct = F_LJ;
608 *reppow = 12.0; /* Default value for repulsion power */
610 comb = 0;
612 /* Init the number of CMAP torsion angles and grid spacing */
613 plist->grid_spacing = 0;
614 plist->nc = 0;
616 bWarn_copy_A_B = bFEP;
618 batype = init_bond_atomtype();
619 /* parse the actual file */
620 bReadDefaults = FALSE;
621 bGenPairs = FALSE;
622 bReadMolType = FALSE;
623 nmol_couple = 0;
627 status = cpp_read_line(&handle, STRLEN, line);
628 done = (status == eCPP_EOF);
629 if (!done)
631 if (status != eCPP_OK)
633 gmx_fatal(FARGS, cpp_error(&handle, status));
635 else if (out)
637 fprintf(out, "%s\n", line);
640 set_warning_line(wi, cpp_cur_file(&handle), cpp_cur_linenr(&handle));
642 pline = strdup(line);
644 /* Strip trailing '\' from pline, if it exists */
645 sl = strlen(pline);
646 if ((sl > 0) && (pline[sl-1] == CONTINUE))
648 pline[sl-1] = ' ';
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 = 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);
669 if (!done)
671 if (status != eCPP_OK)
673 gmx_fatal(FARGS, cpp_error(&handle, status));
675 else if (out)
677 fprintf(out, "%s\n", line);
681 srenew(pline, strlen(pline)+strlen(tmp_line)+1);
682 strcat(pline, tmp_line);
683 sfree(tmp_line);
686 /* skip trailing and leading spaces and comment text */
687 strip_comment (pline);
688 trim (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 = strdup((pline+1));
700 if ((dummy2 = strchr (dirstr, CLOSEDIR)) != NULL)
702 (*dummy2) = 0;
704 trim (dirstr);
706 if ((newd = str2dir(dirstr)) == d_invalid)
708 sprintf(errbuf, "Invalid directive %s", dirstr);
709 warning_error(wi, errbuf);
711 else
713 /* Directive found */
714 if (debug)
716 fprintf(debug, "found directive '%s'\n", dir2str(newd));
718 if (DS_Check_Order (DS, newd))
720 DS_Push (&DS, newd);
721 d = newd;
723 else
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));
729 /* d = d_invalid; */
732 sfree(dirstr);
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!
740 switch (d)
742 case d_defaults:
743 if (bReadDefaults)
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);
751 if (nscan < 2)
753 too_few(wi);
755 else
757 bGenPairs = FALSE;
758 fudgeLJ = 1.0;
759 *fudgeQQ = 1.0;
761 get_nbparm(nb_str, comb_str, &nb_funct, &comb, wi);
762 *combination_rule = comb;
763 if (nscan >= 3)
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");
771 if (nscan >= 4)
773 fudgeLJ = fLJ;
775 if (nscan >= 5)
777 *fudgeQQ = fQQ;
779 if (nscan >= 6)
781 *reppow = fPOW;
784 nb_funct = ifunc_index(d_nonbond_params, nb_funct);
786 break;
787 case d_atomtypes:
788 push_at(symtab, atype, batype, pline, nb_funct,
789 &nbparam, bGenPairs ? &pair : NULL, wi);
790 break;
792 case d_bondtypes:
793 push_bt(d, plist, 2, NULL, batype, pline, wi);
794 break;
795 case d_constrainttypes:
796 push_bt(d, plist, 2, NULL, batype, pline, wi);
797 break;
798 case d_pairtypes:
799 if (bGenPairs)
801 push_nbt(d, pair, atype, pline, F_LJ14, wi);
803 else
805 push_bt(d, plist, 2, atype, NULL, pline, wi);
807 break;
808 case d_angletypes:
809 push_bt(d, plist, 3, NULL, batype, pline, wi);
810 break;
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);
814 break;
816 case d_nonbond_params:
817 push_nbt(d, nbparam, atype, pline, nb_funct, wi);
818 break;
820 case d_blocktype:
821 nblock++;
822 srenew(block,nblock);
823 srenew(blockinfo,nblock);
824 blk0=&(block[nblock-1]);
825 bi0=&(blockinfo[nblock-1]);
826 init_top(blk0);
827 init_molinfo(bi0);
828 push_molt(symtab,bi0,pline);
829 break;
832 case d_implicit_genborn_params:
833 push_gb_params(atype, pline, wi);
834 break;
836 case d_implicit_surface_params:
837 gmx_fatal(FARGS, "Implicit surface directive not supported yet.");
838 break;
840 case d_cmaptypes:
841 push_cmaptype(d, plist, 5, atype, batype, pline, wi);
842 break;
844 case d_moleculetype:
846 if (!bReadMolType)
848 int ntype;
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]),
862 ntype);
863 fprintf(stderr, "Generated %d of the %d non-bonded parameter combinations\n", ncombs-ncopy, ncombs);
864 free_nbparam(nbparam, ntype);
865 if (bGenPairs)
867 gen_pairs(&(plist[nb_funct]), &(plist[F_LJ14]), fudgeLJ, comb);
868 ncopy = copy_nbparams(pair, nb_funct, &(plist[F_LJ14]),
869 ntype);
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? */
875 bReadMolType = TRUE;
878 push_molt(symtab, &nmol, molinfo, pline, wi);
879 srenew(block2, nmol);
880 block2[nmol-1].nr = 0;
881 mi0 = &((*molinfo)[nmol-1]);
882 break;
884 case d_atoms:
885 push_atom(symtab, &(mi0->cgs), &(mi0->atoms), atype, pline, &lastcg, wi);
886 break;
888 case d_pairs:
889 push_bond(d, plist, mi0->plist, &(mi0->atoms), atype, pline, FALSE,
890 bGenPairs, *fudgeQQ, bZero, &bWarn_copy_A_B, wi);
891 break;
893 case d_vsites2:
894 case d_vsites3:
895 case d_vsites4:
896 case d_bonds:
897 case d_angles:
898 case d_constraints:
899 case d_settles:
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:
906 case d_dihedrals:
907 case d_polarization:
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);
912 break;
913 case d_cmap:
914 push_cmap(d, plist, mi0->plist, &(mi0->atoms), atype, pline, wi);
915 break;
917 case d_vsitesn:
918 push_vsitesn(d, mi0->plist, &(mi0->atoms), pline, wi);
919 break;
920 case d_exclusions:
921 assert(block2);
922 if (!block2[nmol-1].nr)
924 init_block2(&(block2[nmol-1]), mi0->atoms.nr);
926 push_excl(pline, &(block2[nmol-1]));
927 break;
928 case d_system:
929 trim(pline);
930 title = put_symtab(symtab, pline);
931 break;
932 case d_molecules:
934 int whichmol;
935 gmx_bool bCouple;
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;
942 nmolb++;
944 bCouple = (opts->couple_moltype != NULL &&
945 (gmx_strcasecmp("system", opts->couple_moltype) == 0 ||
946 gmx_strcasecmp(*(mi0->name), opts->couple_moltype) == 0));
947 if (bCouple)
949 nmol_couple += nrcopies;
952 if (mi0->atoms.nr == 0)
954 gmx_fatal(FARGS, "Molecule type '%s' contains no atoms",
955 *mi0->name);
957 fprintf(stderr,
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)
963 t_nextnb nnb;
964 generate_excl(mi0->nrexcl,
965 mi0->atoms.nr,
966 mi0->plist,
967 &nnb,
968 &(mi0->excls));
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);
983 done_nnb(&nnb);
985 if (bCouple)
987 convert_moltype_couple(mi0, dcatt, *fudgeQQ,
988 opts->couple_lam0, opts->couple_lam1,
989 opts->bCoupleIntra,
990 nb_funct, &(plist[nb_funct]));
992 stupid_fill_block(&mi0->mols, mi0->atoms.nr, TRUE);
993 mi0->bProcessed = TRUE;
995 break;
997 default:
998 fprintf (stderr, "case: %d\n", d);
999 gmx_incons("unknown directive");
1003 sfree(pline);
1004 pline = NULL;
1007 while (!done);
1008 status = cpp_close_file(&handle);
1009 if (status != eCPP_OK)
1011 gmx_fatal(FARGS, cpp_error(&handle, status));
1013 cpp_done();
1014 if (out)
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 */
1031 if (!title)
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);
1045 DS_Done (&DS);
1046 for (i = 0; i < nmol; i++)
1048 done_block2(&(block2[i]));
1050 free(block2);
1052 done_bond_atomtype(&batype);
1054 *nrmols = nmol;
1056 *nmolblock = nmolb;
1057 *molblock = molb;
1059 return title;
1062 char **do_top(gmx_bool bVerbose,
1063 const char *topfile,
1064 const char *topppfile,
1065 t_gromppopts *opts,
1066 gmx_bool bZero,
1067 t_symtab *symtab,
1068 t_params plist[],
1069 int *combination_rule,
1070 double *repulsion_power,
1071 real *fudgeQQ,
1072 gpp_atomtype_t atype,
1073 int *nrmols,
1074 t_molinfo **molinfo,
1075 t_inputrec *ir,
1076 int *nmolblock,
1077 gmx_molblock_t **molblock,
1078 gmx_bool bGenborn,
1079 warninp_t wi)
1081 /* Tmpfile might contain a long path */
1082 const char *tmpfile;
1083 char **title;
1085 if (topppfile)
1087 tmpfile = topppfile;
1089 else
1091 tmpfile = NULL;
1094 if (bVerbose)
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"
1108 " results");
1111 return title;
1115 static void generate_qmexcl_moltype(gmx_moltype_t *molt, unsigned char *grpnr,
1116 t_inputrec *ir)
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;
1126 atom_id
1127 *qm_arr = NULL, *link_arr = NULL, a1, a2, a3, a4, ftype = 0;
1128 t_blocka
1129 qmexcl;
1130 t_block2
1131 qmexcl2;
1132 gmx_bool
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)
1154 qm_max += 100;
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
1166 * to TRUE.
1168 snew(bQMMM, molt->atoms.nr);
1169 for (i = 0; i < molt->atoms.nr; i++)
1171 bQMMM[i] = FALSE;
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)
1203 continue;
1205 nratoms = interaction_function[i].nratoms;
1206 j = 0;
1207 while (j < molt->ilist[i].nr)
1209 bexcl = FALSE;
1210 switch (nratoms)
1212 case 2:
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;
1227 break;
1228 case 3:
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]));
1235 break;
1236 case 4:
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]));
1245 break;
1246 default:
1247 gmx_fatal(FARGS, "no such bonded interactions with %d atoms\n", nratoms);
1249 if (bexcl)
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)];
1260 else
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
1270 * twice. */
1272 for (i = 0; i < F_NRE; i++)
1274 if (IS_CHEMBOND(i))
1276 j = 0;
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)
1285 link_max += 10;
1286 srenew(link_arr, link_max);
1288 if (bQMMM[a1])
1290 link_arr[link_nr++] = a2;
1292 else
1294 link_arr[link_nr++] = a1;
1297 j += 3;
1301 snew(blink, molt->atoms.nr);
1302 for (i = 0; i < molt->atoms.nr; i++)
1304 blink[i] = FALSE;
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);
1317 j = 0;
1318 for (i = 0; i < qmexcl.nr; i++)
1320 qmexcl.index[i] = j;
1321 if (bQMMM[i])
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);
1333 if (blink[i])
1335 for (k = 0; k < qm_nr; k++)
1337 qmexcl.a[k+j] = qm_arr[k];
1339 j += qm_nr;
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;
1360 j = 0;
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]));
1368 if (bexcl)
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)];
1379 else
1381 j += nratoms+1; /* the +1 is for the functype */
1386 free(qm_arr);
1387 free(bQMMM);
1388 free(link_arr);
1389 free(blink);
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;
1400 gmx_bool bQMMM;
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++)
1410 bQMMM = FALSE;
1411 for (i = 0; i < nat_mol; i++)
1413 if ((grpnr ? grpnr[i] : 0) < ir->opts.ngQM)
1415 bQMMM = TRUE;
1418 if (bQMMM)
1420 nr_mol_with_qm_atoms++;
1421 if (molb->nmol > 1)
1423 /* We need to split this molblock */
1424 if (mol > 0)
1426 /* Split the molblock at this molecule */
1427 sys->nmolblock++;
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;
1435 mb++;
1436 molb = &sys->molblock[mb];
1438 if (molb->nmol > 1)
1440 /* Split the molblock after this molecule */
1441 sys->nmolblock++;
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 */
1453 sys->nmoltype++;
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);
1467 if (grpnr)
1469 grpnr += nat_mol;
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
1481 warning_note(wi,
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"
1491 "\n\n");