3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2007, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * Groningen Machine for Chemical Simulation
64 #include "mtop_util.h"
68 static void clust_size(const char *ndx
,const char *trx
,const char *xpm
,
69 const char *xpmw
,const char *ncl
,const char *acl
,
70 const char *mcl
,const char *histo
,const char *tempf
,
71 const char *mcn
,bool bMol
,bool bPBC
,const char *tpr
,
72 real cut
,int nskip
,int nlevels
,
73 t_rgb rmid
,t_rgb rhi
,int ndf
,
74 const output_env_t oenv
)
78 int nindex
,natoms
,status
;
79 rvec
*x
=NULL
,*v
=NULL
,dx
;
83 bool bSame
,bTPRwarn
=TRUE
;
87 gmx_mtop_t
*mtop
=NULL
;
91 int version
,generation
,ii
,jj
,nsame
;
93 /* Cluster size distribution (matrix) */
95 real tf
,dx2
,cut2
,*t_x
=NULL
,*t_y
,cmid
,cmax
,cav
,ekin
;
96 int i
,j
,k
,ai
,aj
,ak
,ci
,cj
,nframe
,nclust
,n_x
,n_y
,max_size
=0;
97 int *clust_index
,*clust_size
,max_clust_size
,max_clust_ind
,nav
,nhisto
;
98 t_rgb rlo
= { 1.0, 1.0, 1.0 };
100 clear_trxframe(&fr
,TRUE
);
101 sprintf(timebuf
,"Time (%s)",output_env_get_time_unit(oenv
));
102 tf
= output_env_get_time_factor(oenv
);
103 fp
= xvgropen(ncl
,"Number of clusters",timebuf
,"N",oenv
);
104 gp
= xvgropen(acl
,"Average cluster size",timebuf
,"#molecules",oenv
);
105 hp
= xvgropen(mcl
,"Max cluster size",timebuf
,"#molecules",oenv
);
106 tp
= xvgropen(tempf
,"Temperature of largest cluster",timebuf
,"T (K)",
109 if (!read_first_frame(oenv
,&status
,trx
,&fr
,TRX_NEED_X
| TRX_READ_V
))
117 read_tpxheader(tpr
,&tpxh
,TRUE
,&version
,&generation
);
118 if (tpxh
.natoms
!= natoms
)
119 gmx_fatal(FARGS
,"tpr (%d atoms) and xtc (%d atoms) do not match!",
121 ePBC
= read_tpx(tpr
,NULL
,NULL
,&natoms
,NULL
,NULL
,NULL
,mtop
);
126 tfac
= ndf
/(3.0*natoms
);
130 printf("Using molecules rather than atoms. Not reading index file %s\n",
132 mols
= &(mtop
->mols
);
134 /* Make dummy index */
137 for(i
=0; (i
<nindex
); i
++)
139 gname
= strdup("mols");
142 rd_index(ndx
,1,&nindex
,&index
,&gname
);
144 snew(clust_index
,nindex
);
145 snew(clust_size
,nindex
);
150 for(i
=0; (i
<nindex
); i
++)
155 if ((nskip
== 0) || ((nskip
> 0) && ((nframe
% nskip
) == 0))) {
157 set_pbc(&pbc
,ePBC
,fr
.box
);
161 /* Put all atoms/molecules in their own cluster, with size 1 */
162 for(i
=0; (i
<nindex
); i
++) {
163 /* Cluster index is indexed with atom index number */
165 /* Cluster size is indexed with cluster number */
169 /* Loop over atoms */
170 for(i
=0; (i
<nindex
); i
++) {
174 /* Loop over atoms (only half a matrix) */
175 for(j
=i
+1; (j
<nindex
); j
++) {
178 /* If they are not in the same cluster already */
182 /* Compute distance */
185 for(ii
=mols
->index
[ai
]; !bSame
&& (ii
<mols
->index
[ai
+1]); ii
++) {
186 for(jj
=mols
->index
[aj
]; !bSame
&& (jj
<mols
->index
[aj
+1]); jj
++) {
188 pbc_dx(&pbc
,x
[ii
],x
[jj
],dx
);
190 rvec_sub(x
[ii
],x
[jj
],dx
);
192 bSame
= (dx2
< cut2
);
198 pbc_dx(&pbc
,x
[ai
],x
[aj
],dx
);
200 rvec_sub(x
[ai
],x
[aj
],dx
);
202 bSame
= (dx2
< cut2
);
204 /* If distance less than cut-off */
206 /* Merge clusters: check for all atoms whether they are in
207 * cluster cj and if so, put them in ci
209 for(k
=0; (k
<nindex
); k
++) {
210 if ((clust_index
[k
] == cj
)) {
211 if (clust_size
[cj
] <= 0)
212 gmx_fatal(FARGS
,"negative cluster size %d for element %d",
225 t_x
[n_x
-1] = fr
.time
*tf
;
227 snew(cs_dist
[n_x
-1],nindex
);
231 for(i
=0; (i
<nindex
); i
++) {
233 if (ci
> max_clust_size
) {
239 cs_dist
[n_x
-1][ci
-1] += 1.0;
240 max_size
= max(max_size
,ci
);
247 fprintf(fp
,"%14.6e %10d\n",fr
.time
,nclust
);
249 fprintf(gp
,"%14.6e %10.3f\n",fr
.time
,cav
/nav
);
250 fprintf(hp
, "%14.6e %10d\n",fr
.time
,max_clust_size
);
252 /* Analyse velocities, if present */
256 printf("You need a tpr file to analyse temperatures\n");
262 /* Loop over clusters and for each cluster compute 1/2 m v^2 */
263 if (max_clust_ind
>= 0) {
265 for(i
=0; (i
<nindex
); i
++)
266 if (clust_index
[i
] == max_clust_ind
) {
268 gmx_mtop_atomnr_to_atom(mtop
,ai
,&atom
);
269 ekin
+= 0.5*atom
->m
*iprod(v
[ai
],v
[ai
]);
271 temp
= (ekin
*2.0)/(3.0*tfac
*max_clust_size
*BOLTZ
);
272 fprintf(tp
,"%10.3f %10.3f\n",fr
.time
,temp
);
277 } while (read_next_frame(oenv
,status
,&fr
));
283 if (max_clust_ind
>= 0) {
284 fp
= ffopen(mcn
,"w");
285 fprintf(fp
,"[ max_clust ]\n");
286 for(i
=0; (i
<nindex
); i
++)
287 if (clust_index
[i
] == max_clust_ind
) {
289 for(j
=mols
->index
[i
]; (j
<mols
->index
[i
+1]); j
++)
290 fprintf(fp
,"%d\n",j
+1);
293 fprintf(fp
,"%d\n",index
[i
]+1);
299 /* Print the real distribution cluster-size/numer, averaged over the trajectory. */
300 fp
= xvgropen(histo
,"Cluster size distribution","Cluster size","()",oenv
);
302 fprintf(fp
,"%5d %8.3f\n",0,0.0);
303 for(j
=0; (j
<max_size
); j
++) {
305 for(i
=0; (i
<n_x
); i
++)
306 nelem
+= cs_dist
[i
][j
];
307 fprintf(fp
,"%5d %8.3f\n",j
+1,nelem
/n_x
);
308 nhisto
+= (int)((j
+1)*nelem
/n_x
);
310 fprintf(fp
,"%5d %8.3f\n",j
+1,0.0);
313 fprintf(stderr
,"Total number of atoms in clusters = %d\n",nhisto
);
315 /* Look for the smallest entry that is not zero
316 * This will make that zero is white, and not zero is coloured.
320 for(i
=0; (i
<n_x
); i
++)
321 for(j
=0; (j
<max_size
); j
++) {
322 if ((cs_dist
[i
][j
] > 0) && (cs_dist
[i
][j
] < cmid
))
323 cmid
= cs_dist
[i
][j
];
324 cmax
= max(cs_dist
[i
][j
],cmax
);
326 fprintf(stderr
,"cmid: %g, cmax: %g, max_size: %d\n",cmid
,cmax
,max_size
);
328 fp
= ffopen(xpm
,"w");
329 write_xpm3(fp
,0,"Cluster size distribution","# clusters",timebuf
,"Size",
330 n_x
,max_size
,t_x
,t_y
,cs_dist
,0,cmid
,cmax
,
331 rlo
,rmid
,rhi
,&nlevels
);
335 for(i
=0; (i
<n_x
); i
++)
336 for(j
=0; (j
<max_size
); j
++) {
337 cs_dist
[i
][j
] *= (j
+1);
338 if ((cs_dist
[i
][j
] > 0) && (cs_dist
[i
][j
] < cmid
))
339 cmid
= cs_dist
[i
][j
];
340 cmax
= max(cs_dist
[i
][j
],cmax
);
342 fprintf(stderr
,"cmid: %g, cmax: %g, max_size: %d\n",cmid
,cmax
,max_size
);
343 fp
= ffopen(xpmw
,"w");
344 write_xpm3(fp
,0,"Weighted cluster size distribution","Fraction",timebuf
,
345 "Size", n_x
,max_size
,t_x
,t_y
,cs_dist
,0,cmid
,cmax
,
346 rlo
,rmid
,rhi
,&nlevels
);
354 int gmx_clustsize(int argc
,char *argv
[])
356 const char *desc
[] = {
357 "This program computes the size distributions of molecular/atomic clusters in",
358 "the gas phase. The output is given in the form of a XPM file.",
359 "The total number of clusters is written to a XVG file.[PAR]",
360 "When the [TT]-mol[tt] option is given clusters will be made out of",
361 "molecules rather than atoms, which allows clustering of large molecules.",
362 "In this case an index file would still contain atom numbers",
363 "or your calculcation will die with a SEGV.[PAR]",
364 "When velocities are present in your trajectory, the temperature of",
365 "the largest cluster will be printed in a separate xvg file assuming",
366 "that the particles are free to move. If you are using constraints,",
367 "please correct the temperature. For instance water simulated with SHAKE",
368 "or SETTLE will yield a temperature that is 1.5 times too low. You can",
369 "compensate for this with the -ndf option. Remember to take the removal",
370 "of center of mass motion into account.[PAR]",
371 "The [TT]-mc[tt] option will produce an index file containing the",
372 "atom numbers of the largest cluster."
375 static real cutoff
= 0.35;
376 static int nskip
= 0;
377 static int nlevels
= 20;
379 static bool bMol
= FALSE
;
380 static bool bPBC
= TRUE
;
381 static rvec rlo
= { 1.0, 1.0, 0.0 };
382 static rvec rhi
= { 0.0, 0.0, 1.0 };
387 { "-cut", FALSE
, etREAL
, {&cutoff
},
388 "Largest distance (nm) to be considered in a cluster" },
389 { "-mol", FALSE
, etBOOL
, {&bMol
},
390 "Cluster molecules rather than atoms (needs tpr file)" },
391 { "-pbc", FALSE
, etBOOL
, {&bPBC
},
392 "Use periodic boundary conditions" },
393 { "-nskip", FALSE
, etINT
, {&nskip
},
394 "Number of frames to skip between writing" },
395 { "-nlevels", FALSE
, etINT
, {&nlevels
},
396 "Number of levels of grey in xpm output" },
397 { "-ndf", FALSE
, etINT
, {&ndf
},
398 "Number of degrees of freedom of the entire system for temperature calculation. If not set the number of atoms times three is used." },
399 { "-rgblo", FALSE
, etRVEC
, {rlo
},
400 "RGB values for the color of the lowest occupied cluster size" },
401 { "-rgbhi", FALSE
, etRVEC
, {rhi
},
402 "RGB values for the color of the highest occupied cluster size" }
404 #define NPA asize(pa)
405 const char *fnNDX
,*fnTPR
;
410 { efTRX
, "-f", NULL
, ffREAD
},
411 { efTPR
, NULL
, NULL
, ffOPTRD
},
412 { efNDX
, NULL
, NULL
, ffOPTRD
},
413 { efXPM
, "-o", "csize", ffWRITE
},
414 { efXPM
, "-ow","csizew", ffWRITE
},
415 { efXVG
, "-nc","nclust", ffWRITE
},
416 { efXVG
, "-mc","maxclust", ffWRITE
},
417 { efXVG
, "-ac","avclust", ffWRITE
},
418 { efXVG
, "-hc","histo-clust", ffWRITE
},
419 { efXVG
, "-temp","temp", ffOPTWR
},
420 { efNDX
, "-mcn", "maxclust", ffOPTWR
}
422 #define NFILE asize(fnm)
424 CopyRight(stderr
,argv
[0]);
425 parse_common_args(&argc
,argv
,
426 PCA_CAN_VIEW
| PCA_CAN_TIME
| PCA_TIME_UNIT
| PCA_BE_NICE
,
427 NFILE
,fnm
,NPA
,pa
,asize(desc
),desc
,0,NULL
,&oenv
);
429 fnNDX
= ftp2fn_null(efNDX
,NFILE
,fnm
);
430 rgblo
.r
= rlo
[XX
],rgblo
.g
= rlo
[YY
],rgblo
.b
= rlo
[ZZ
];
431 rgbhi
.r
= rhi
[XX
],rgbhi
.g
= rhi
[YY
],rgbhi
.b
= rhi
[ZZ
];
433 fnTPR
= ftp2fn_null(efTPR
,NFILE
,fnm
);
435 gmx_fatal(FARGS
,"You need a tpr file for the -mol option");
437 clust_size(fnNDX
,ftp2fn(efTRX
,NFILE
,fnm
),opt2fn("-o",NFILE
,fnm
),
438 opt2fn("-ow",NFILE
,fnm
),
439 opt2fn("-nc",NFILE
,fnm
),opt2fn("-ac",NFILE
,fnm
),
440 opt2fn("-mc",NFILE
,fnm
),opt2fn("-hc",NFILE
,fnm
),
441 opt2fn("-temp",NFILE
,fnm
),opt2fn("-mcn",NFILE
,fnm
),
443 cutoff
,nskip
,nlevels
,rgblo
,rgbhi
,ndf
,oenv
);