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 #include "insert-molecules.h"
48 #include "gromacs/math/utilities.h"
49 #include "gromacs/fileio/confio.h"
51 #include "gromacs/random/random.h"
52 #include "gromacs/fileio/futil.h"
56 #include "gmx_fatal.h"
57 #include "gromacs/commandline/pargs.h"
58 #include "gromacs/gmxlib/conformation-utilities.h"
60 #include "read-conformation.h"
64 static gmx_bool in_box(t_pbc *pbc, rvec x)
69 /* pbc_dx_aiuc only works correctly with the rectangular box center */
70 calc_box_center(ecenterRECT, pbc->box, box_center);
72 shift = pbc_dx_aiuc(pbc, x, box_center, dx);
74 return (shift == CENTRAL);
77 /* This is a (maybe) slow workaround to avoid the neighbor searching in addconf.c, which
78 * leaks memory (May 2012). The function could be deleted as soon as the memory leaks
80 * However, when inserting a small molecule in a system containing not too many atoms,
81 * allPairsDistOk is probably even faster than the other code.
84 allPairsDistOk(t_atoms *atoms, rvec *x, real *r,
86 t_atoms *atoms_insrt, rvec *x_n, real *r_insrt)
93 set_pbc(&pbc, ePBC, box);
94 for (i = 0; i < atoms->nr; i++)
96 for (j = 0; j < atoms_insrt->nr; j++)
98 pbc_dx(&pbc, x[i], x_n[j], dx);
100 r2 = sqr(r[i]+r_insrt[j]);
110 /* enum for random rotations of inserted solutes */
112 en_rot, en_rotXYZ, en_rotZ, en_rotNone, en_NR
115 static char *insert_mols(const char *mol_insrt, int nmol_insrt, int ntry, int seed,
116 t_atoms *atoms, rvec **x, real **r, int ePBC, matrix box,
118 real r_distance, real r_scale, real rshell,
119 const output_env_t oenv,
120 const char* posfn, const rvec deltaR, int enum_rot,
121 gmx_bool bCheckAllPairDist)
124 static char *title_insrt;
130 int i, mol, onr, ncol;
131 real alfa = 0., beta = 0., gamma = 0.;
137 rng = gmx_rng_init(seed);
138 set_pbc(&pbc, ePBC, box);
140 /* read number of atoms of insert molecules */
141 get_stx_coordnum(mol_insrt, &atoms_insrt.nr);
142 if (atoms_insrt.nr == 0)
144 gmx_fatal(FARGS, "No molecule in %s, please check your input\n", mol_insrt);
146 /* allocate memory for atom coordinates of insert molecules */
147 snew(x_insrt, atoms_insrt.nr);
148 snew(r_insrt, atoms_insrt.nr);
149 snew(atoms_insrt.resinfo, atoms_insrt.nr);
150 snew(atoms_insrt.atomname, atoms_insrt.nr);
151 snew(atoms_insrt.atom, atoms_insrt.nr);
152 atoms_insrt.pdbinfo = NULL;
153 snew(x_n, atoms_insrt.nr);
154 snew(title_insrt, STRLEN);
156 /* read residue number, residue names, atomnames, coordinates etc. */
157 fprintf(stderr, "Reading molecule configuration \n");
158 read_stx_conf(mol_insrt, title_insrt, &atoms_insrt, x_insrt, NULL,
159 &ePBC_insrt, box_insrt);
160 fprintf(stderr, "%s\nContaining %d atoms in %d residue\n",
161 title_insrt, atoms_insrt.nr, atoms_insrt.nres);
162 srenew(atoms_insrt.resinfo, atoms_insrt.nres);
164 /* initialise van der waals arrays for inserted molecules */
165 mk_vdw(&atoms_insrt, r_insrt, aps, r_distance, r_scale);
167 /* With -ip, take nmol_insrt from file posfn */
170 nmol_insrt = read_xvg(posfn, &rpos, &ncol);
173 gmx_fatal(FARGS, "Expected 3 columns (x/y/z coordinates) in file %s\n", ncol, posfn);
175 fprintf(stderr, "Read %d positions from file %s\n\n", nmol_insrt, posfn);
178 srenew(atoms->resinfo, (atoms->nres+nmol_insrt*atoms_insrt.nres));
179 srenew(atoms->atomname, (atoms->nr+atoms_insrt.nr*nmol_insrt));
180 srenew(atoms->atom, (atoms->nr+atoms_insrt.nr*nmol_insrt));
181 srenew(*x, (atoms->nr+atoms_insrt.nr*nmol_insrt));
182 srenew(*r, (atoms->nr+atoms_insrt.nr*nmol_insrt));
185 while ((mol < nmol_insrt) && (trial < ntry*nmol_insrt))
187 fprintf(stderr, "\rTry %d", trial++);
188 for (i = 0; (i < atoms_insrt.nr); i++)
190 copy_rvec(x_insrt[i], x_n[i]);
195 alfa = 2*M_PI * gmx_rng_uniform_real(rng);
196 beta = 2*M_PI * gmx_rng_uniform_real(rng);
197 gamma = 2*M_PI * gmx_rng_uniform_real(rng);
201 gamma = 2*M_PI * gmx_rng_uniform_real(rng);
204 alfa = beta = gamma = 0.;
207 if (enum_rot == en_rotXYZ || (enum_rot == en_rotZ))
209 rotate_conf(atoms_insrt.nr, x_n, NULL, alfa, beta, gamma);
213 /* insert at random positions */
214 offset_x[XX] = box[XX][XX] * gmx_rng_uniform_real(rng);
215 offset_x[YY] = box[YY][YY] * gmx_rng_uniform_real(rng);
216 offset_x[ZZ] = box[ZZ][ZZ] * gmx_rng_uniform_real(rng);
217 make_new_box(atoms_insrt.nr, x_n, box_insrt, offset_x, TRUE);
218 if (!in_box(&pbc, x_n[0]) || !in_box(&pbc, x_n[atoms_insrt.nr-1]))
225 /* Insert at positions taken from option -ip file */
226 offset_x[XX] = rpos[XX][mol] + deltaR[XX]*(2 * gmx_rng_uniform_real(rng)-1);
227 offset_x[YY] = rpos[YY][mol] + deltaR[YY]*(2 * gmx_rng_uniform_real(rng)-1);
228 offset_x[ZZ] = rpos[ZZ][mol] + deltaR[ZZ]*(2 * gmx_rng_uniform_real(rng)-1);
229 for (i = 0; i < atoms_insrt.nr; i++)
231 rvec_inc(x_n[i], offset_x);
237 /* This is a (maybe) slow workaround to avoid too many calls of add_conf, which
238 * leaks memory (status May 2012). If the momory leaks in add_conf() are fixed,
239 * this check could be removed. Note, however, that allPairsDistOk is probably
240 * even faster than add_conf() when inserting a small molecule into a moderately
243 if (bCheckAllPairDist && !allPairsDistOk(atoms, *x, *r, ePBC, box, &atoms_insrt, x_n, r_insrt))
248 add_conf(atoms, x, NULL, r, FALSE, ePBC, box, TRUE,
249 &atoms_insrt, x_n, NULL, r_insrt, FALSE, rshell, 0, oenv);
251 if (atoms->nr == (atoms_insrt.nr+onr))
254 fprintf(stderr, " success (now %d atoms)!\n", atoms->nr);
257 gmx_rng_destroy(rng);
258 srenew(atoms->resinfo, atoms->nres);
259 srenew(atoms->atomname, atoms->nr);
260 srenew(atoms->atom, atoms->nr);
261 srenew(*x, atoms->nr);
262 srenew(*r, atoms->nr);
264 fprintf(stderr, "\n");
265 /* print number of molecules added */
266 fprintf(stderr, "Added %d molecules (out of %d requested) of %s\n",
267 mol, nmol_insrt, *atoms_insrt.resinfo[0].name);
272 int gmx_insert_molecules(int argc, char *argv[])
274 const char *desc[] = {
275 "[THISMODULE] inserts [TT]-nmol[tt] copies of the system specified in",
276 "the [TT]-ci[tt] input file. The insertions take place either into",
277 "vacant space in the solute conformation given with [TT]-f[tt], or",
278 "into an empty box given by [TT]-box[tt]. Specifying both [TT]-f[tt]",
279 "and [TT]-box[tt] behaves like [TT]-f[tt], but places a new box",
280 "around the solute before insertions. Any velocities present are",
283 "By default, the insertion positions are random (with initial seed",
284 "specified by [TT]-seed[tt]). The program iterates until [TT]-nmol[tt]",
285 "molecules have been inserted in the box. Molecules are not inserted",
286 "where the distance between any existing atom and any atom of ",
287 "the inserted molecule is less than the sum of the van der Waals radii of ",
288 "both atoms. A database ([TT]vdwradii.dat[tt]) of van der Waals radii is ",
289 "read by the program, and atoms not in the database are ",
290 "assigned a default distance [TT]-vdwd[tt].[PAR]",
292 "A total of [TT]-nmol[tt] * [TT]-try[tt] insertion attempts are made",
293 "before giving up. Increase [TT]-try[tt] if you have several small",
294 "holes to fill. Option [TT]-rot[tt] specifies whether the insertion",
295 "molecules are randomly oriented before insertion attempts.[PAR]",
297 "Alternatively, the molecules can be inserted only at positions defined in",
298 "positions.dat ([TT]-ip[tt]). That file should have 3 columns (x,y,z),",
299 "that give the displacements compared to the input molecule position",
300 "([TT]-ci[tt]). Hence, if that file should contain the absolute",
301 "positions, the molecule must be centered on (0,0,0) before using",
302 "[THISMODULE] (e.g. from [gmx-editconf] [TT]-center[tt]).",
303 "Comments in that file starting with # are ignored. Option [TT]-dr[tt]",
304 "defines the maximally allowed displacements during insertial trials.",
305 "[TT]-try[tt] and [TT]-rot[tt] work as in the default mode (see above).",
309 const char *bugs[] = {
310 "Molecules must be whole in the initial configurations.",
311 "Many repeated neighbor searchings with -ci blows up the allocated memory. "
312 "Option -allpair avoids this using all-to-all distance checks (slow for large systems)"
316 gmx_bool bProt, bBox;
317 const char *conf_prot, *confout;
319 char *title_ins = NULL;
322 /* protein configuration data */
330 { efSTX, "-f", "protein", ffOPTRD },
331 { efSTX, "-ci", "insert", ffREAD},
332 { efDAT, "-ip", "positions", ffOPTRD},
333 { efSTO, NULL, NULL, ffWRITE},
335 #define NFILE asize(fnm)
337 static int nmol_ins = 0, nmol_try = 10, seed = 1997, enum_rot;
338 static real r_distance = 0.105, r_scale = 0.57;
339 static rvec new_box = {0.0, 0.0, 0.0}, deltaR = {0.0, 0.0, 0.0};
340 static gmx_bool bCheckAllPairDist = FALSE;
342 const char *enum_rot_string[] = {NULL, "xyz", "z", "none", NULL};
344 { "-box", FALSE, etRVEC, {new_box},
345 "Box size (in nm)" },
346 { "-nmol", FALSE, etINT, {&nmol_ins},
347 "Number of extra molecules to insert" },
348 { "-try", FALSE, etINT, {&nmol_try},
349 "Try inserting [TT]-nmol[tt] times [TT]-try[tt] times" },
350 { "-seed", FALSE, etINT, {&seed},
351 "Random generator seed"},
352 { "-vdwd", FALSE, etREAL, {&r_distance},
353 "Default van der Waals distance"},
354 { "-vdwscale", FALSE, etREAL, {&r_scale},
355 "HIDDENScale factor to multiply Van der Waals radii from the database in share/gromacs/top/vdwradii.dat. The default value of 0.57 yields density close to 1000 g/l for proteins in water." },
356 { "-dr", FALSE, etRVEC, {deltaR},
357 "Allowed displacement in x/y/z from positions in [TT]-ip[tt] file" },
358 { "-rot", FALSE, etENUM, {enum_rot_string},
359 "rotate inserted molecules randomly" },
360 { "-allpair", FALSE, etBOOL, {&bCheckAllPairDist},
361 "Avoid momory leaks during neighbor searching with option -ci. May be slow for large systems." },
364 if (!parse_common_args(&argc, argv, PCA_BE_NICE, NFILE, fnm, asize(pa), pa,
365 asize(desc), desc, asize(bugs), bugs, &oenv))
370 bProt = opt2bSet("-f", NFILE, fnm);
371 bBox = opt2parg_bSet("-box", asize(pa), pa);
372 enum_rot = nenum(enum_rot_string);
375 const char *insertionMoleculeFileName = opt2fn("-ci", NFILE, fnm);
376 if (!gmx_fexist(insertionMoleculeFileName))
379 "A molecule conformation to insert is required in -ci. %s was not found!",
380 insertionMoleculeFileName);
382 if (nmol_ins <= 0 && !opt2bSet("-ip", NFILE, fnm))
384 gmx_fatal(FARGS, "Either -nmol must be larger than 0, "
385 "or positions must be given with -ip");
389 gmx_fatal(FARGS, "When no solute (-f) is specified, "
390 "a box size (-box) must be specified");
393 aps = gmx_atomprop_init();
397 /*generate a solute configuration */
398 conf_prot = opt2fn("-f", NFILE, fnm);
399 title = read_conformation(conf_prot, &atoms, &x, NULL, &r, &ePBC, box,
400 aps, r_distance, r_scale);
403 fprintf(stderr, "Note: no atoms in %s\n", conf_prot);
411 atoms.resinfo = NULL;
412 atoms.atomname = NULL;
414 atoms.pdbinfo = NULL;
422 box[XX][XX] = new_box[XX];
423 box[YY][YY] = new_box[YY];
424 box[ZZ][ZZ] = new_box[ZZ];
428 gmx_fatal(FARGS, "Undefined solute box.\nCreate one with gmx editconf "
429 "or give explicit -box command line option");
432 /* add nmol_ins molecules of atoms_ins
433 in random orientation at random place */
434 title_ins = insert_mols(insertionMoleculeFileName, nmol_ins, nmol_try, seed,
435 &atoms, &x, &r, ePBC, box, aps,
436 r_distance, r_scale, 0,
437 oenv, opt2fn_null("-ip", NFILE, fnm), deltaR, enum_rot,
440 /* write new configuration to file confout */
441 confout = ftp2fn(efSTO, NFILE, fnm);
442 fprintf(stderr, "Writing generated configuration to %s\n", confout);
445 write_sto_conf(confout, title, &atoms, x, NULL, ePBC, box);
446 /* print box sizes and box type to stderr */
447 fprintf(stderr, "%s\n", title);
451 write_sto_conf(confout, title_ins, &atoms, x, NULL, ePBC, box);
454 /* print size of generated configuration */
455 fprintf(stderr, "\nOutput configuration contains %d atoms in %d residues\n",
456 atoms.nr, atoms.nres);
458 gmx_atomprop_destroy(aps);