524d2fe2cccbb1dd664c432e7fe35f8c7e84a5cd
[alexxy/gromacs.git] / src / gromacs / gmxana / gmx_genconf.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include "gromacs/math/utilities.h"
42 #include "macros.h"
43 #include "string2.h"
44 #include "smalloc.h"
45 #include "sysstuff.h"
46 #include "gromacs/fileio/confio.h"
47 #include "gromacs/commandline/pargs.h"
48 #include "vec.h"
49 #include "random.h"
50 #include "gromacs/math/3dview.h"
51 #include "txtdump.h"
52 #include "readinp.h"
53 #include "names.h"
54 #include "sortwater.h"
55 #include "gmx_ana.h"
56 #include "gromacs/fileio/trxio.h"
57
58 static void rand_rot(int natoms, rvec x[], rvec v[], vec4 xrot[], vec4 vrot[],
59                      int *seed, rvec max_rot)
60 {
61     mat4 mt1, mt2, mr[DIM], mtemp1, mtemp2, mtemp3, mxtot, mvtot;
62     rvec xcm;
63     real phi;
64     int  i, m;
65
66     clear_rvec(xcm);
67     for (i = 0; (i < natoms); i++)
68     {
69         for (m = 0; (m < DIM); m++)
70         {
71             xcm[m] += x[i][m]/natoms; /* get center of mass of one molecule  */
72         }
73     }
74     fprintf(stderr, "center of geometry: %f, %f, %f\n", xcm[0], xcm[1], xcm[2]);
75
76     translate(-xcm[XX], -xcm[YY], -xcm[ZZ], mt1); /* move c.o.ma to origin */
77     for (m = 0; (m < DIM); m++)
78     {
79         phi = M_PI*max_rot[m]*(2*rando(seed) - 1)/180;
80         rotate(m, phi, mr[m]);
81     }
82     translate(xcm[XX], xcm[YY], xcm[ZZ], mt2);
83
84     /* For mult_matrix we need to multiply in the opposite order
85      * compared to normal mathematical notation.
86      */
87     mult_matrix(mtemp1, mt1, mr[XX]);
88     mult_matrix(mtemp2, mr[YY], mr[ZZ]);
89     mult_matrix(mtemp3, mtemp1, mtemp2);
90     mult_matrix(mxtot, mtemp3, mt2);
91     mult_matrix(mvtot, mr[XX], mtemp2);
92
93     for (i = 0; (i < natoms); i++)
94     {
95         m4_op(mxtot, x[i], xrot[i]);
96         m4_op(mvtot, v[i], vrot[i]);
97     }
98 }
99
100 static void move_x(int natoms, rvec x[], matrix box)
101 {
102     int  i, m;
103     rvec xcm;
104
105     clear_rvec(xcm);
106     for (i = 0; (i < natoms); i++)
107     {
108         for (m = 0; (m < DIM); m++)
109         {
110             xcm[m] += x[i][m];
111         }
112     }
113     for (m = 0; (m < DIM); m++)
114     {
115         xcm[m] = 0.5*box[m][m]-xcm[m]/natoms;
116     }
117     for (i = 0; (i < natoms); i++)
118     {
119         for (m = 0; (m < DIM); m++)
120         {
121             x[i][m] += xcm[m];
122         }
123     }
124 }
125
126 int gmx_genconf(int argc, char *argv[])
127 {
128     const char     *desc[] = {
129         "[THISMODULE] multiplies a given coordinate file by simply stacking them",
130         "on top of each other, like a small child playing with wooden blocks.",
131         "The program makes a grid of [IT]user-defined[it]",
132         "proportions ([TT]-nbox[tt]), ",
133         "and interspaces the grid point with an extra space [TT]-dist[tt].[PAR]",
134         "When option [TT]-rot[tt] is used the program does not check for overlap",
135         "between molecules on grid points. It is recommended to make the box in",
136         "the input file at least as big as the coordinates + ",
137         "van der Waals radius.[PAR]",
138         "If the optional trajectory file is given, conformations are not",
139         "generated, but read from this file and translated appropriately to",
140         "build the grid."
141
142     };
143     const char     *bugs[] = {
144         "The program should allow for random displacement of lattice points."
145     };
146
147     int             vol;
148     t_atoms        *atoms;      /* list with all atoms */
149     char            title[STRLEN];
150     rvec           *x, *xx, *v; /* coordinates? */
151     real            t;
152     vec4           *xrot, *vrot;
153     int             ePBC;
154     matrix          box, boxx; /* box length matrix */
155     rvec            shift;
156     int             natoms;    /* number of atoms in one molecule  */
157     int             nres;      /* number of molecules? */
158     int             i, j, k, l, m, ndx, nrdx, nx, ny, nz;
159     t_trxstatus    *status;
160     gmx_bool        bTRX;
161     output_env_t    oenv;
162
163     t_filenm        fnm[] = {
164         { efSTX, "-f", "conf", ffREAD  },
165         { efSTO, "-o", "out",  ffWRITE },
166         { efTRX, "-trj", NULL,  ffOPTRD }
167     };
168 #define NFILE asize(fnm)
169     static rvec     nrbox    = {1, 1, 1};
170     static int      seed     = 0;    /* seed for random number generator */
171     static int      nmolat   = 3;
172     static int      nblock   = 1;
173     static gmx_bool bShuffle = FALSE;
174     static gmx_bool bSort    = FALSE;
175     static gmx_bool bRandom  = FALSE;           /* False: no random rotations */
176     static gmx_bool bRenum   = TRUE;            /* renumber residues */
177     static rvec     dist     = {0, 0, 0};       /* space added between molecules ? */
178     static rvec     max_rot  = {180, 180, 180}; /* maximum rotation */
179     t_pargs         pa[]     = {
180         { "-nbox",   FALSE, etRVEC, {nrbox},   "Number of boxes" },
181         { "-dist",   FALSE, etRVEC, {dist},    "Distance between boxes" },
182         { "-seed",   FALSE, etINT,  {&seed},
183           "Random generator seed, if 0 generated from the time" },
184         { "-rot",    FALSE, etBOOL, {&bRandom}, "Randomly rotate conformations" },
185         { "-shuffle", FALSE, etBOOL, {&bShuffle}, "Random shuffling of molecules" },
186         { "-sort",   FALSE, etBOOL, {&bSort},   "Sort molecules on X coord" },
187         { "-block",  FALSE, etINT,  {&nblock},
188           "Divide the box in blocks on this number of cpus" },
189         { "-nmolat", FALSE, etINT,  {&nmolat},
190           "Number of atoms per molecule, assumed to start from 0. "
191           "If you set this wrong, it will screw up your system!" },
192         { "-maxrot", FALSE, etRVEC, {max_rot}, "Maximum random rotation" },
193         { "-renumber", FALSE, etBOOL, {&bRenum},  "Renumber residues" }
194     };
195
196     if (!parse_common_args(&argc, argv, 0, NFILE, fnm, asize(pa), pa,
197                            asize(desc), desc, asize(bugs), bugs, &oenv))
198     {
199         return 0;
200     }
201
202     if (bRandom && (seed == 0))
203     {
204         seed = make_seed();
205     }
206
207     bTRX = ftp2bSet(efTRX, NFILE, fnm);
208     nx   = (int)(nrbox[XX]+0.5);
209     ny   = (int)(nrbox[YY]+0.5);
210     nz   = (int)(nrbox[ZZ]+0.5);
211
212     if ((nx <= 0) || (ny <= 0) || (nz <= 0))
213     {
214         gmx_fatal(FARGS, "Number of boxes (-nbox) should be larger than zero");
215     }
216     if ((nmolat <= 0) && bShuffle)
217     {
218         gmx_fatal(FARGS, "Can not shuffle if the molecules only have %d atoms",
219                   nmolat);
220     }
221
222     vol = nx*ny*nz; /* calculate volume in grid points (= nr. molecules) */
223
224     get_stx_coordnum(opt2fn("-f", NFILE, fnm), &natoms);
225     snew(atoms, 1);
226     /* make space for all the atoms */
227     init_t_atoms(atoms, natoms*vol, FALSE);
228     snew(x, natoms*vol);           /* get space for coordinates of all atoms */
229     snew(xrot, natoms);            /* get space for rotation matrix? */
230     snew(v, natoms*vol);           /* velocities. not really needed? */
231     snew(vrot, natoms);
232     /* set atoms->nr to the number in one box *
233      * to avoid complaints in read_stx_conf   *
234      */
235     atoms->nr = natoms;
236     read_stx_conf(opt2fn("-f", NFILE, fnm), title, atoms, x, v, &ePBC, box);
237
238     nres = atoms->nres;            /* nr of residues in one element? */
239
240     if (bTRX)
241     {
242         if (!read_first_x(oenv, &status, ftp2fn(efTRX, NFILE, fnm), &t, &xx, boxx))
243         {
244             gmx_fatal(FARGS, "No atoms in trajectory %s", ftp2fn(efTRX, NFILE, fnm));
245         }
246     }
247     else
248     {
249         snew(xx, natoms);
250         for (i = 0; i < natoms; i++)
251         {
252             copy_rvec(x[i], xx[i]);
253         }
254     }
255
256
257     for (k = 0; (k < nz); k++)     /* loop over all gridpositions    */
258     {
259         shift[ZZ] = k*(dist[ZZ]+box[ZZ][ZZ]);
260
261         for (j = 0; (j < ny); j++)
262         {
263             shift[YY] = j*(dist[YY]+box[YY][YY])+k*box[ZZ][YY];
264
265             for (i = 0; (i < nx); i++)
266             {
267                 shift[XX] = i*(dist[XX]+box[XX][XX])+j*box[YY][XX]+k*box[ZZ][XX];
268
269                 ndx  = (i*ny*nz+j*nz+k)*natoms;
270                 nrdx = (i*ny*nz+j*nz+k)*nres;
271
272                 /* Random rotation on input coords */
273                 if (bRandom)
274                 {
275                     rand_rot(natoms, xx, v, xrot, vrot, &seed, max_rot);
276                 }
277
278                 for (l = 0; (l < natoms); l++)
279                 {
280                     for (m = 0; (m < DIM); m++)
281                     {
282                         if (bRandom)
283                         {
284                             x[ndx+l][m] = xrot[l][m];
285                             v[ndx+l][m] = vrot[l][m];
286                         }
287                         else
288                         {
289                             x[ndx+l][m] = xx[l][m];
290                             v[ndx+l][m] = v[l][m];
291                         }
292                     }
293                     if (ePBC == epbcSCREW && i % 2 == 1)
294                     {
295                         /* Rotate around x axis */
296                         for (m = YY; m <= ZZ; m++)
297                         {
298                             x[ndx+l][m] = box[YY][m] + box[ZZ][m] - x[ndx+l][m];
299                             v[ndx+l][m] = -v[ndx+l][m];
300                         }
301                     }
302                     for (m = 0; (m < DIM); m++)
303                     {
304                         x[ndx+l][m] += shift[m];
305                     }
306                     atoms->atom[ndx+l].resind = nrdx + atoms->atom[l].resind;
307                     atoms->atomname[ndx+l]    = atoms->atomname[l];
308                 }
309
310                 for (l = 0; (l < nres); l++)
311                 {
312                     atoms->resinfo[nrdx+l] = atoms->resinfo[l];
313                     if (bRenum)
314                     {
315                         atoms->resinfo[nrdx+l].nr += nrdx;
316                     }
317                 }
318                 if (bTRX)
319                 {
320                     if (!read_next_x(oenv, status, &t, xx, boxx) &&
321                         ((i+1)*(j+1)*(k+1) < vol))
322                     {
323                         gmx_fatal(FARGS, "Not enough frames in trajectory");
324                     }
325                 }
326             }
327         }
328     }
329     if (bTRX)
330     {
331         close_trj(status);
332     }
333
334     /* make box bigger */
335     for (m = 0; (m < DIM); m++)
336     {
337         box[m][m] += dist[m];
338     }
339     svmul(nx, box[XX], box[XX]);
340     svmul(ny, box[YY], box[YY]);
341     svmul(nz, box[ZZ], box[ZZ]);
342     if (ePBC == epbcSCREW && nx % 2 == 0)
343     {
344         /* With an even number of boxes in x we can forgot about the screw */
345         ePBC = epbcXYZ;
346     }
347
348     /* move_x(natoms*vol,x,box); */          /* put atoms in box? */
349
350     atoms->nr   *= vol;
351     atoms->nres *= vol;
352
353     /*depending on how you look at it, this is either a nasty hack or the way it should work*/
354     if (bRenum)
355     {
356         for (i = 0; i < atoms->nres; i++)
357         {
358             atoms->resinfo[i].nr = i+1;
359         }
360     }
361
362
363     if (bShuffle)
364     {
365         randwater(0, atoms->nr/nmolat, nmolat, x, v, &seed);
366     }
367     else if (bSort)
368     {
369         sortwater(0, atoms->nr/nmolat, nmolat, x, v);
370     }
371     else if (opt2parg_bSet("-block", asize(pa), pa))
372     {
373         mkcompact(0, atoms->nr/nmolat, nmolat, x, v, nblock, box);
374     }
375
376     write_sto_conf(opt2fn("-o", NFILE, fnm), title, atoms, x, v, ePBC, box);
377
378     return 0;
379 }