2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
41 #include "gromacs/random/random.h"
47 #include "gromacs/fileio/futil.h"
48 #include "gromacs/fileio/strdb.h"
49 #include "gromacs/utility/gmxomp.h"
51 void check_binwidth(real binwidth)
53 real smallest_bin = 0.1;
54 if (binwidth < smallest_bin)
56 gmx_fatal(FARGS, "Binwidth shouldnt be smaller then smallest bond length (H-H bond ~0.1nm) in a box");
60 void check_mcover(real mcover)
64 gmx_fatal(FARGS, "mcover should be -1 or (0,1]");
66 else if ((mcover < 0)&(mcover != -1))
68 gmx_fatal(FARGS, "mcover should be -1 or (0,1]");
76 void normalize_probability(int n, double *a)
80 for (i = 0; i < n; i++)
84 for (i = 0; i < n; i++)
90 gmx_neutron_atomic_structurefactors_t *gmx_neutronstructurefactors_init(const char *datfn)
92 /* read nsfactor.dat */
100 gmx_neutron_atomic_structurefactors_t *gnsf;
104 /* allocate memory for structure */
106 snew(gnsf->atomnm, nralloc);
107 snew(gnsf->p, nralloc);
108 snew(gnsf->n, nralloc);
109 snew(gnsf->slength, nralloc);
111 gnsf->nratoms = line_no;
113 while (get_a_line(fp, line, STRLEN))
116 if (sscanf(line, "%s %d %d %lf", atomnm, &p, &n, &slength) == 4)
118 gnsf->atomnm[i] = strdup(atomnm);
121 gnsf->slength[i] = slength;
123 gnsf->nratoms = line_no;
124 if (line_no == nralloc)
127 srenew(gnsf->atomnm, nralloc);
128 srenew(gnsf->p, nralloc);
129 srenew(gnsf->n, nralloc);
130 srenew(gnsf->slength, nralloc);
135 fprintf(stderr, "WARNING: Error in file %s at line %d ignored\n",
139 srenew(gnsf->atomnm, gnsf->nratoms);
140 srenew(gnsf->p, gnsf->nratoms);
141 srenew(gnsf->n, gnsf->nratoms);
142 srenew(gnsf->slength, gnsf->nratoms);
146 return (gmx_neutron_atomic_structurefactors_t *) gnsf;
149 gmx_sans_t *gmx_sans_init (t_topology *top, gmx_neutron_atomic_structurefactors_t *gnsf)
151 gmx_sans_t *gsans = NULL;
153 /* Try to assing scattering length from nsfactor.dat */
155 snew(gsans->slength, top->atoms.nr);
156 /* copy topology data */
158 for (i = 0; i < top->atoms.nr; i++)
160 for (j = 0; j < gnsf->nratoms; j++)
162 if (top->atoms.atom[i].atomnumber == gnsf->p[j])
164 /* we need special case for H and D */
165 if (top->atoms.atom[i].atomnumber == 1)
167 if (top->atoms.atom[i].m == 1.008000)
169 gsans->slength[i] = gnsf->slength[0];
173 gsans->slength[i] = gnsf->slength[1];
178 gsans->slength[i] = gnsf->slength[j];
184 return (gmx_sans_t *) gsans;
187 gmx_radial_distribution_histogram_t *calc_radial_distribution_histogram (
199 gmx_radial_distribution_histogram_t *pr = NULL;
207 gmx_rng_t *trng = NULL;
209 gmx_int64_t mc = 0, max;
210 gmx_rng_t rng = NULL;
212 /* allocate memory for pr */
214 /* set some fields */
215 pr->binwidth = binwidth;
218 * create max dist rvec
219 * dist = box[xx] + box[yy] + box[zz]
221 rvec_add(box[XX], box[YY], dist);
222 rvec_add(box[ZZ], dist, dist);
226 pr->grn = (int)floor(rmax/pr->binwidth)+1;
227 rmax = pr->grn*pr->binwidth;
229 snew(pr->gr, pr->grn);
233 /* Special case for setting automaticaly number of mc iterations to 1% of total number of direct iterations */
236 max = (gmx_int64_t)floor(0.5*0.01*isize*(isize-1));
240 max = (gmx_int64_t)floor(0.5*mcover*isize*(isize-1));
242 rng = gmx_rng_init(seed);
244 nthreads = gmx_omp_get_max_threads();
246 snew(trng, nthreads);
247 for (i = 0; i < nthreads; i++)
249 snew(tgr[i], pr->grn);
250 trng[i] = gmx_rng_init(gmx_rng_uniform_uint32(rng));
252 #pragma omp parallel shared(tgr,trng,mc) private(tid,i,j)
254 tid = gmx_omp_get_thread_num();
255 /* now starting parallel threads */
257 for (mc = 0; mc < max; mc++)
259 i = (int)floor(gmx_rng_uniform_real(trng[tid])*isize);
260 j = (int)floor(gmx_rng_uniform_real(trng[tid])*isize);
263 tgr[tid][(int)floor(sqrt(distance2(x[index[i]], x[index[j]]))/binwidth)] += gsans->slength[index[i]]*gsans->slength[index[j]];
267 /* collecting data from threads */
268 for (i = 0; i < pr->grn; i++)
270 for (j = 0; j < nthreads; j++)
272 pr->gr[i] += tgr[j][i];
275 /* freeing memory for tgr and destroying trng */
276 for (i = 0; i < nthreads; i++)
279 gmx_rng_destroy(trng[i]);
284 for (mc = 0; mc < max; mc++)
286 i = (int)floor(gmx_rng_uniform_real(rng)*isize);
287 j = (int)floor(gmx_rng_uniform_real(rng)*isize);
290 pr->gr[(int)floor(sqrt(distance2(x[index[i]], x[index[j]]))/binwidth)] += gsans->slength[index[i]]*gsans->slength[index[j]];
294 gmx_rng_destroy(rng);
299 nthreads = gmx_omp_get_max_threads();
300 /* Allocating memory for tgr arrays */
302 for (i = 0; i < nthreads; i++)
304 snew(tgr[i], pr->grn);
306 #pragma omp parallel shared(tgr) private(tid,i,j)
308 tid = gmx_omp_get_thread_num();
309 /* starting parallel threads */
311 for (i = 0; i < isize; i++)
313 for (j = 0; j < i; j++)
315 tgr[tid][(int)floor(sqrt(distance2(x[index[i]], x[index[j]]))/binwidth)] += gsans->slength[index[i]]*gsans->slength[index[j]];
319 /* collecating data for pr->gr */
320 for (i = 0; i < pr->grn; i++)
322 for (j = 0; j < nthreads; j++)
324 pr->gr[i] += tgr[j][i];
327 /* freeing memory for tgr */
328 for (i = 0; i < nthreads; i++)
334 for (i = 0; i < isize; i++)
336 for (j = 0; j < i; j++)
338 pr->gr[(int)floor(sqrt(distance2(x[index[i]], x[index[j]]))/binwidth)] += gsans->slength[index[i]]*gsans->slength[index[j]];
344 /* normalize if needed */
347 normalize_probability(pr->grn, pr->gr);
350 snew(pr->r, pr->grn);
351 for (i = 0; i < pr->grn; i++)
353 pr->r[i] = (pr->binwidth*i+pr->binwidth*0.5);
356 return (gmx_radial_distribution_histogram_t *) pr;
359 gmx_static_structurefactor_t *convert_histogram_to_intensity_curve (gmx_radial_distribution_histogram_t *pr, double start_q, double end_q, double q_step)
361 gmx_static_structurefactor_t *sq = NULL;
365 sq->qn = (int)floor((end_q-start_q)/q_step);
368 for (i = 0; i < sq->qn; i++)
370 sq->q[i] = start_q+i*q_step;
376 for (i = 1; i < sq->qn; i++)
378 for (j = 0; j < pr->grn; j++)
380 sq->s[i] += (pr->gr[j]/pr->r[j])*sin(sq->q[i]*pr->r[j]);
382 sq->s[i] /= sq->q[i];
387 for (i = 0; i < sq->qn; i++)
389 for (j = 0; j < pr->grn; j++)
391 sq->s[i] += (pr->gr[j]/pr->r[j])*sin(sq->q[i]*pr->r[j]);
393 sq->s[i] /= sq->q[i];
397 return (gmx_static_structurefactor_t *) sq;