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.
46 #include "gromacs/math/vec.h"
48 #include "types/commrec.h"
55 #include "mtop_util.h"
56 #include "chargegroup.h"
58 #include "gromacs/pbcutil/pbc.h"
59 #include "gromacs/topology/block.h"
60 #include "gromacs/topology/topology.h"
61 #include "gromacs/utility/smalloc.h"
63 static real box_margin;
65 static real max_dist(rvec *x, real *r, int start, int end)
71 for (i = start; i < end; i++)
73 for (j = i+1; j < end; j++)
75 maxd = max(maxd, sqrt(distance2(x[i], x[j]))+0.5*(r[i]+r[j]));
82 static gmx_bool outside_box_minus_margin2(rvec x, matrix box)
84 return ( (x[XX] < 2*box_margin) || (x[XX] > box[XX][XX]-2*box_margin) ||
85 (x[YY] < 2*box_margin) || (x[YY] > box[YY][YY]-2*box_margin) ||
86 (x[ZZ] < 2*box_margin) || (x[ZZ] > box[ZZ][ZZ]-2*box_margin) );
89 static gmx_bool outside_box_plus_margin(rvec x, matrix box)
91 return ( (x[XX] < -box_margin) || (x[XX] > box[XX][XX]+box_margin) ||
92 (x[YY] < -box_margin) || (x[YY] > box[YY][YY]+box_margin) ||
93 (x[ZZ] < -box_margin) || (x[ZZ] > box[ZZ][ZZ]+box_margin) );
96 static int mark_res(int at, gmx_bool *mark, int natoms, t_atom *atom, int *nmark)
100 resind = atom[at].resind;
101 while ( (at > 0) && (resind == atom[at-1].resind) )
105 while ( (at < natoms) && (resind == atom[at].resind) )
118 static real find_max_real(int n, real radius[])
127 for (i = 1; (i < n); i++)
129 rmax = max(rmax, radius[i]);
135 static void combine_atoms(t_atoms *ap, t_atoms *as,
136 rvec xp[], rvec *vp, rvec xs[], rvec *vs,
137 t_atoms **a_comb, rvec **x_comb, rvec **v_comb)
140 rvec *xc, *vc = NULL;
141 int i, j, natot, res0;
143 /* Total number of atoms */
144 natot = ap->nr+as->nr;
147 init_t_atoms(ac, natot, FALSE);
155 /* Fill the new structures */
156 for (i = j = 0; (i < ap->nr); i++, j++)
158 copy_rvec(xp[i], xc[j]);
161 copy_rvec(vp[i], vc[j]);
163 memcpy(&(ac->atom[j]), &(ap->atom[i]), sizeof(ap->atom[i]));
164 ac->atom[j].type = 0;
167 for (i = 0; (i < as->nr); i++, j++)
169 copy_rvec(xs[i], xc[j]);
172 copy_rvec(vs[i], vc[j]);
174 memcpy(&(ac->atom[j]), &(as->atom[i]), sizeof(as->atom[i]));
175 ac->atom[j].type = 0;
176 ac->atom[j].resind += res0;
179 ac->nres = ac->atom[j-1].resind+1;
180 /* Fill all elements to prevent uninitialized memory */
181 for (i = 0; i < ac->nr; i++)
187 ac->atom[i].type = 0;
188 ac->atom[i].typeB = 0;
189 ac->atom[i].ptype = eptAtom;
198 static t_forcerec *fr = NULL;
200 static void do_nsgrid(FILE *fp, gmx_bool bVerbose,
201 matrix box, rvec x[], t_atoms *atoms, real rlong,
202 const output_env_t oenv)
221 /* Charge group index */
222 snew(cg_index, natoms);
223 for (i = 0; (i < natoms); i++)
228 /* Topology needs charge groups and exclusions */
231 mtop->natoms = natoms;
232 /* Make one moltype that contains the whol system */
234 snew(mtop->moltype, mtop->nmoltype);
235 molt = &mtop->moltype[0];
236 molt->name = mtop->name;
237 molt->atoms = *atoms;
238 stupid_fill_block(&molt->cgs, mtop->natoms, FALSE);
239 stupid_fill_blocka(&molt->excls, natoms);
240 /* Make one molblock for the whole system */
242 snew(mtop->molblock, mtop->nmolblock);
243 mtop->molblock[0].type = 0;
244 mtop->molblock[0].nmol = 1;
245 mtop->molblock[0].natoms_mol = natoms;
246 /* Initialize a single energy group */
247 mtop->groups.grps[egcENER].nr = 1;
248 mtop->groups.ngrpnr[egcENER] = 0;
249 mtop->groups.grpnr[egcENER] = NULL;
251 ffp = &mtop->ffparams;
256 snew(ffp->functype, 1);
257 snew(ffp->iparams, 1);
258 ffp->iparams[0].lj.c6 = 1;
259 ffp->iparams[0].lj.c12 = 1;
261 /* inputrec structure */
263 ir->cutoff_scheme = ecutsGROUP;
264 ir->coulomb_modifier = eintmodNONE;
265 ir->vdw_modifier = eintmodNONE;
266 ir->coulombtype = eelCUT;
267 ir->vdwtype = evdwCUT;
269 ir->ns_type = ensGRID;
270 snew(ir->opts.egp_flags, 1);
272 top = gmx_mtop_generate_local_top(mtop, ir);
274 /* Some nasty shortcuts */
277 /* mdatoms structure */
280 md = init_mdatoms(fp, mtop, FALSE);
281 atoms2md(mtop, ir, 0, NULL, mtop->natoms, md);
284 /* forcerec structure */
291 /* cr->nthreads = 1; */
293 /* ir->rlist = ir->rcoulomb = ir->rvdw = rlong;
294 printf("Neighborsearching with a cut-off of %g\n",rlong);
295 init_forcerec(stdout,fr,ir,top,cr,md,box,FALSE,NULL,NULL,NULL,TRUE);*/
297 fr->hcg = top->cgs.nr;
300 /* Prepare for neighboursearching */
303 /* Init things dependent on parameters */
304 ir->rlistlong = ir->rlist = ir->rcoulomb = ir->rvdw = rlong;
305 /* create free energy data to avoid NULLs */
306 snew(ir->fepvals, 1);
307 printf("Neighborsearching with a cut-off of %g\n", rlong);
308 init_forcerec(stdout, oenv, fr, NULL, ir, mtop, cr, box,
309 NULL, NULL, NULL, NULL, NULL, TRUE, -1);
312 pr_forcerec(debug, fr);
315 /* Calculate new stuff dependent on coords and box */
316 for (m = 0; (m < DIM); m++)
318 box_size[m] = box[m][m];
320 calc_shifts(box, fr->shift_vec);
321 put_charge_groups_in_box(fp, 0, cgs->nr, fr->ePBC, box, cgs, x, fr->cg_cm);
323 /* Do the actual neighboursearching */
324 snew(lambda, efptNR);
326 init_neighbor_list(fp, fr, md->homenr);
327 search_neighbours(fp, fr, box, top,
328 &mtop->groups, cr, &nrnb, md, TRUE, FALSE);
332 dump_nblist(debug, cr, fr, 0);
337 fprintf(stderr, "Successfully made neighbourlist\n");
341 static gmx_bool bXor(gmx_bool b1, gmx_bool b2)
343 return (b1 && !b2) || (b2 && !b1);
346 void add_conf(t_atoms *atoms, rvec **x, rvec **v, real **r, gmx_bool bSrenew,
347 int ePBC, matrix box, gmx_bool bInsert,
348 t_atoms *atoms_solvt, rvec *x_solvt, rvec *v_solvt, real *r_solvt,
349 gmx_bool bVerbose, real rshell, int max_sol, const output_env_t oenv)
353 real max_vdw, *r_prot, *r_all, n2, r2, ib1, ib2;
354 int natoms_prot, natoms_solvt;
355 int i, j, jj, m, j0, j1, jjj, jnres, jnr, inr, iprot, is1, is2;
356 int prev, resnr, nresadd, d, k, ncells, maxincell;
357 int dx0, dx1, dy0, dy1, dz0, dz1;
358 int ntest, nremove, nkeep;
359 rvec dx, xi, xj, xpp, *x_all, *v_all;
360 gmx_bool *remove, *keep;
363 natoms_prot = atoms->nr;
364 natoms_solvt = atoms_solvt->nr;
365 if (natoms_solvt <= 0)
367 fprintf(stderr, "WARNING: Nothing to add\n");
371 if (ePBC == epbcSCREW)
373 gmx_fatal(FARGS, "Sorry, %s pbc is not yet supported", epbc_names[ePBC]);
378 fprintf(stderr, "Calculating Overlap...\n");
381 /* Set margin around box edges to largest solvent dimension.
382 * The maximum distance between atoms in a solvent molecule should
383 * be calculated. At the moment a fudge factor of 3 is used.
386 box_margin = 3*find_max_real(natoms_solvt, r_solvt);
387 max_vdw = max(3*find_max_real(natoms_prot, r_prot), box_margin);
388 fprintf(stderr, "box_margin = %g\n", box_margin);
390 snew(remove, natoms_solvt);
395 for (i = 0; i < atoms_solvt->nr; i++)
397 if (outside_box_plus_margin(x_solvt[i], box) )
399 i = mark_res(i, remove, atoms_solvt->nr, atoms_solvt->atom, &nremove);
402 fprintf(stderr, "Removed %d atoms that were outside the box\n", nremove);
405 /* Define grid stuff */
406 /* Largest VDW radius */
407 snew(r_all, natoms_prot+natoms_solvt);
408 for (i = j = 0; i < natoms_prot; i++, j++)
410 r_all[j] = r_prot[i];
412 for (i = 0; i < natoms_solvt; i++, j++)
414 r_all[j] = r_solvt[i];
418 combine_atoms(atoms, atoms_solvt, *x, v ? *v : NULL, x_solvt, v_solvt,
419 &atoms_all, &x_all, &v_all);
421 /* Do neighboursearching step */
422 do_nsgrid(stdout, bVerbose, box, x_all, atoms_all, max_vdw, oenv);
424 /* check solvent with solute */
425 nlist = &(fr->nblists[0].nlist_sr[eNL_VDW]);
426 fprintf(stderr, "nri = %d, nrj = %d\n", nlist->nri, nlist->nrj);
427 for (bSolSol = 0; (bSolSol <= (bInsert ? 0 : 1)); bSolSol++)
430 fprintf(stderr, "Checking %s-Solvent overlap:",
431 bSolSol ? "Solvent" : "Protein");
432 for (i = 0; (i < nlist->nri && nremove < natoms_solvt); i++)
434 inr = nlist->iinr[i];
435 j0 = nlist->jindex[i];
436 j1 = nlist->jindex[i+1];
437 rvec_add(x_all[inr], fr->shift_vec[nlist->shift[i]], xi);
439 for (j = j0; (j < j1 && nremove < natoms_solvt); j++)
441 jnr = nlist->jjnr[j];
442 copy_rvec(x_all[jnr], xj);
444 /* Check solvent-protein and solvent-solvent */
445 is1 = inr-natoms_prot;
446 is2 = jnr-natoms_prot;
448 /* Check if at least one of the atoms is a solvent that is not yet
449 * listed for removal, and if both are solvent, that they are not in the
453 bXor((is1 >= 0), (is2 >= 0)) && /* One atom is protein */
454 ((is1 < 0) || ((is1 >= 0) && !remove[is1])) &&
455 ((is2 < 0) || ((is2 >= 0) && !remove[is2]))) ||
458 (is1 >= 0) && (!remove[is1]) && /* is1 is solvent */
459 (is2 >= 0) && (!remove[is2]) && /* is2 is solvent */
460 (bInsert || /* when inserting also check inside the box */
461 (outside_box_minus_margin2(x_solvt[is1], box) && /* is1 on edge */
462 outside_box_minus_margin2(x_solvt[is2], box)) /* is2 on edge */
464 (atoms_solvt->atom[is1].resind != /* Not the same residue */
465 atoms_solvt->atom[is2].resind)))
469 rvec_sub(xi, xj, dx);
471 r2 = sqr(r_all[inr]+r_all[jnr]);
476 nremove = natoms_solvt;
477 for (k = 0; k < nremove; k++)
482 /* Need only remove one of the solvents... */
485 (void) mark_res(is2, remove, natoms_solvt, atoms_solvt->atom,
490 (void) mark_res(is1, remove, natoms_solvt, atoms_solvt->atom,
495 fprintf(stderr, "Neither atom is solvent%d %d\n", is1, is2);
503 fprintf(stderr, " tested %d pairs, removed %d atoms.\n", ntest, nremove);
508 for (i = 0; i < natoms_solvt; i++)
510 fprintf(debug, "remove[%5d] = %s\n", i, bool_names[remove[i]]);
514 /* Search again, now with another cut-off */
517 do_nsgrid(stdout, bVerbose, box, x_all, atoms_all, rshell, oenv);
518 nlist = &(fr->nblists[0].nlist_sr[eNL_VDW]);
519 fprintf(stderr, "nri = %d, nrj = %d\n", nlist->nri, nlist->nrj);
521 snew(keep, natoms_solvt);
522 for (i = 0; i < nlist->nri; i++)
524 inr = nlist->iinr[i];
525 j0 = nlist->jindex[i];
526 j1 = nlist->jindex[i+1];
528 for (j = j0; j < j1; j++)
530 jnr = nlist->jjnr[j];
532 /* Check solvent-protein and solvent-solvent */
533 is1 = inr-natoms_prot;
534 is2 = jnr-natoms_prot;
536 /* Check if at least one of the atoms is a solvent that is not yet
537 * listed for removal, and if both are solvent, that they are not in the
540 if (is1 >= 0 && is2 < 0)
542 mark_res(is1, keep, natoms_solvt, atoms_solvt->atom, &nkeep);
544 else if (is1 < 0 && is2 >= 0)
546 mark_res(is2, keep, natoms_solvt, atoms_solvt->atom, &nkeep);
550 fprintf(stderr, "Keeping %d solvent atoms after proximity check\n",
552 for (i = 0; i < natoms_solvt; i++)
554 remove[i] = remove[i] || !keep[i];
558 /* count how many atoms and residues will be added and make space */
562 jnres = atoms_solvt->nres;
568 for (i = 0; ((i < atoms_solvt->nr) &&
569 ((max_sol == 0) || (jnres < max_sol))); i++)
575 (atoms_solvt->atom[i].resind != atoms_solvt->atom[i-1].resind))
584 fprintf(debug, "Will add %d atoms in %d residues\n", j, jnres);
588 /* Flag the remaing solvent atoms to be removed */
589 jjj = atoms_solvt->atom[i-1].resind;
590 for (; (i < atoms_solvt->nr); i++)
592 if (atoms_solvt->atom[i].resind > jjj)
605 srenew(atoms->resinfo, atoms->nres+jnres);
606 srenew(atoms->atomname, atoms->nr+j);
607 srenew(atoms->atom, atoms->nr+j);
608 srenew(*x, atoms->nr+j);
611 srenew(*v, atoms->nr+j);
613 srenew(*r, atoms->nr+j);
616 /* add the selected atoms_solvt to atoms */
619 resnr = atoms->resinfo[atoms->atom[atoms->nr-1].resind].nr;
627 for (i = 0; i < atoms_solvt->nr; i++)
632 atoms_solvt->atom[i].resind != atoms_solvt->atom[prev].resind)
637 atoms->resinfo[atoms->nres-1] =
638 atoms_solvt->resinfo[atoms_solvt->atom[i].resind];
639 atoms->resinfo[atoms->nres-1].nr = resnr;
640 /* calculate shift of the solvent molecule using the first atom */
641 copy_rvec(x_solvt[i], dx);
642 put_atoms_in_box(ePBC, box, 1, &dx);
643 rvec_dec(dx, x_solvt[i]);
645 atoms->atom[atoms->nr] = atoms_solvt->atom[i];
646 atoms->atomname[atoms->nr] = atoms_solvt->atomname[i];
647 rvec_add(x_solvt[i], dx, (*x)[atoms->nr]);
650 copy_rvec(v_solvt[i], (*v)[atoms->nr]);
652 (*r)[atoms->nr] = r_solvt[i];
653 atoms->atom[atoms->nr].resind = atoms->nres-1;
660 srenew(atoms->resinfo, atoms->nres+nresadd);
665 fprintf(stderr, "Added %d molecules\n", nresadd);
669 done_atom(atoms_all);