Add replacements for pbc enumerations
[gromacs.git] / src / gromacs / gmxana / nsfactor.cpp
blobae3f658096a12b43cfa62f03829744998282b395
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016,2017,2018, 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.
35 #include "gmxpre.h"
37 #include "nsfactor.h"
39 #include "config.h"
41 #include <cmath>
42 #include <cstring>
44 #include "gromacs/math/vec.h"
45 #include "gromacs/random/threefry.h"
46 #include "gromacs/random/uniformintdistribution.h"
47 #include "gromacs/topology/topology.h"
48 #include "gromacs/utility/cstringutil.h"
49 #include "gromacs/utility/exceptions.h"
50 #include "gromacs/utility/fatalerror.h"
51 #include "gromacs/utility/futil.h"
52 #include "gromacs/utility/gmxomp.h"
53 #include "gromacs/utility/smalloc.h"
54 #include "gromacs/utility/strdb.h"
56 void check_binwidth(real binwidth)
58 real smallest_bin = 0.1;
59 if (binwidth < smallest_bin)
61 gmx_fatal(FARGS, "Binwidth shouldn't be smaller then smallest bond length (H-H bond ~0.1nm) in a box");
65 void check_mcover(real mcover)
67 if (mcover > 1.0)
69 gmx_fatal(FARGS, "mcover should be -1 or (0,1]");
71 else if ((mcover < 0) && (mcover != -1))
73 gmx_fatal(FARGS, "mcover should be -1 or (0,1]");
75 else
77 return;
81 void normalize_probability(int n, double *a)
83 int i;
84 double norm = 0.0;
85 for (i = 0; i < n; i++)
87 norm += a[i];
89 for (i = 0; i < n; i++)
91 a[i] /= norm;
95 gmx_neutron_atomic_structurefactors_t *gmx_neutronstructurefactors_init(const char *datfn)
97 /* read nsfactor.dat */
98 char line[STRLEN];
99 int nralloc = 10;
100 int n, p;
101 int i, line_no;
102 char atomnm[8];
103 double slength;
104 gmx_neutron_atomic_structurefactors_t *gnsf;
106 gmx::FilePtr fp = gmx::openLibraryFile(datfn);
107 line_no = 0;
108 /* allocate memory for structure */
109 snew(gnsf, nralloc);
110 snew(gnsf->atomnm, nralloc);
111 snew(gnsf->p, nralloc);
112 snew(gnsf->n, nralloc);
113 snew(gnsf->slength, nralloc);
115 gnsf->nratoms = line_no;
117 while (get_a_line(fp.get(), line, STRLEN))
119 i = line_no;
120 if (sscanf(line, "%s %d %d %lf", atomnm, &p, &n, &slength) == 4)
122 gnsf->atomnm[i] = gmx_strdup(atomnm);
123 gnsf->n[i] = n;
124 gnsf->p[i] = p;
125 gnsf->slength[i] = slength;
126 line_no++;
127 gnsf->nratoms = line_no;
128 if (line_no == nralloc)
130 nralloc++;
131 srenew(gnsf->atomnm, nralloc);
132 srenew(gnsf->p, nralloc);
133 srenew(gnsf->n, nralloc);
134 srenew(gnsf->slength, nralloc);
137 else
139 fprintf(stderr, "WARNING: Error in file %s at line %d ignored\n",
140 datfn, line_no);
143 srenew(gnsf->atomnm, gnsf->nratoms);
144 srenew(gnsf->p, gnsf->nratoms);
145 srenew(gnsf->n, gnsf->nratoms);
146 srenew(gnsf->slength, gnsf->nratoms);
148 return gnsf;
151 gmx_sans_t *gmx_sans_init (const t_topology *top, gmx_neutron_atomic_structurefactors_t *gnsf)
153 gmx_sans_t *gsans = nullptr;
154 int i, j;
155 /* Try to assing scattering length from nsfactor.dat */
156 snew(gsans, 1);
157 snew(gsans->slength, top->atoms.nr);
158 /* copy topology data */
159 gsans->top = top;
160 for (i = 0; i < top->atoms.nr; i++)
162 for (j = 0; j < gnsf->nratoms; j++)
164 if (top->atoms.atom[i].atomnumber == gnsf->p[j])
166 /* we need special case for H and D */
167 if (top->atoms.atom[i].atomnumber == 1)
169 if (top->atoms.atom[i].m == 1.008000)
171 gsans->slength[i] = gnsf->slength[0];
173 else
175 gsans->slength[i] = gnsf->slength[1];
178 else
180 gsans->slength[i] = gnsf->slength[j];
186 return gsans;
189 gmx_radial_distribution_histogram_t *calc_radial_distribution_histogram (
190 gmx_sans_t *gsans,
191 rvec *x,
192 matrix box,
193 const int *index,
194 int isize,
195 double binwidth,
196 gmx_bool bMC,
197 gmx_bool bNORM,
198 real mcover,
199 unsigned int seed)
201 gmx_radial_distribution_histogram_t *pr = nullptr;
202 rvec dist;
203 double rmax;
204 int i, j;
205 #if GMX_OPENMP
206 double **tgr;
207 int tid;
208 int nthreads;
209 gmx::DefaultRandomEngine *trng = nullptr;
210 #endif
211 int64_t mc = 0, mc_max;
212 gmx::DefaultRandomEngine rng(seed);
214 /* allocate memory for pr */
215 snew(pr, 1);
216 /* set some fields */
217 pr->binwidth = binwidth;
220 * create max dist rvec
221 * dist = box[xx] + box[yy] + box[zz]
223 rvec_add(box[XX], box[YY], dist);
224 rvec_add(box[ZZ], dist, dist);
226 rmax = norm(dist);
228 pr->grn = static_cast<int>(std::floor(rmax/pr->binwidth)+1);
230 snew(pr->gr, pr->grn);
232 if (bMC)
234 /* Special case for setting automaticaly number of mc iterations to 1% of total number of direct iterations */
235 if (mcover == -1)
237 mc_max = static_cast<int64_t>(std::floor(0.5*0.01*isize*(isize-1)));
239 else
241 mc_max = static_cast<int64_t>(std::floor(0.5*mcover*isize*(isize-1)));
243 #if GMX_OPENMP
244 nthreads = gmx_omp_get_max_threads();
245 snew(tgr, nthreads);
246 trng = new gmx::DefaultRandomEngine[nthreads];
247 for (i = 0; i < nthreads; i++)
249 snew(tgr[i], pr->grn);
250 trng[i].seed(rng());
252 #pragma omp parallel shared(tgr,trng,mc) private(tid,i,j)
254 gmx::UniformIntDistribution<int> tdist(0, isize-1);
255 tid = gmx_omp_get_thread_num();
256 /* now starting parallel threads */
257 #pragma omp for
258 for (mc = 0; mc < mc_max; mc++)
262 i = tdist(trng[tid]); // [0,isize-1]
263 j = tdist(trng[tid]); // [0,isize-1]
264 if (i != j)
266 tgr[tid][static_cast<int>(std::floor(std::sqrt(distance2(x[index[i]], x[index[j]]))/binwidth))] += gsans->slength[index[i]]*gsans->slength[index[j]];
269 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
272 /* collecting data from threads */
273 for (i = 0; i < pr->grn; i++)
275 for (j = 0; j < nthreads; j++)
277 pr->gr[i] += tgr[j][i];
280 /* freeing memory for tgr and destroying trng */
281 for (i = 0; i < nthreads; i++)
283 sfree(tgr[i]);
285 sfree(tgr);
286 delete[] trng;
287 #else
288 gmx::UniformIntDistribution<int> dist(0, isize-1);
289 for (mc = 0; mc < mc_max; mc++)
291 i = dist(rng); // [0,isize-1]
292 j = dist(rng); // [0,isize-1]
293 if (i != j)
295 pr->gr[static_cast<int>(std::floor(std::sqrt(distance2(x[index[i]], x[index[j]]))/binwidth))] += gsans->slength[index[i]]*gsans->slength[index[j]];
298 #endif
300 else
302 #if GMX_OPENMP
303 nthreads = gmx_omp_get_max_threads();
304 /* Allocating memory for tgr arrays */
305 snew(tgr, nthreads);
306 for (i = 0; i < nthreads; i++)
308 snew(tgr[i], pr->grn);
310 #pragma omp parallel shared(tgr) private(tid,i,j)
312 tid = gmx_omp_get_thread_num();
313 /* starting parallel threads */
314 #pragma omp for
315 for (i = 0; i < isize; i++)
319 for (j = 0; j < i; j++)
321 tgr[tid][static_cast<int>(std::floor(std::sqrt(distance2(x[index[i]], x[index[j]]))/binwidth))] += gsans->slength[index[i]]*gsans->slength[index[j]];
324 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
327 /* collecating data for pr->gr */
328 for (i = 0; i < pr->grn; i++)
330 for (j = 0; j < nthreads; j++)
332 pr->gr[i] += tgr[j][i];
335 /* freeing memory for tgr */
336 for (i = 0; i < nthreads; i++)
338 sfree(tgr[i]);
340 sfree(tgr);
341 #else
342 for (i = 0; i < isize; i++)
344 for (j = 0; j < i; j++)
346 pr->gr[static_cast<int>(std::floor(std::sqrt(distance2(x[index[i]], x[index[j]]))/binwidth))] += gsans->slength[index[i]]*gsans->slength[index[j]];
349 #endif
352 /* normalize if needed */
353 if (bNORM)
355 normalize_probability(pr->grn, pr->gr);
358 snew(pr->r, pr->grn);
359 for (i = 0; i < pr->grn; i++)
361 pr->r[i] = (pr->binwidth*i+pr->binwidth*0.5);
364 return pr;
367 gmx_static_structurefactor_t *convert_histogram_to_intensity_curve (gmx_radial_distribution_histogram_t *pr, double start_q, double end_q, double q_step)
369 gmx_static_structurefactor_t *sq = nullptr;
370 int i, j;
371 /* init data */
372 snew(sq, 1);
373 sq->qn = static_cast<int>(std::floor((end_q-start_q)/q_step));
374 snew(sq->q, sq->qn);
375 snew(sq->s, sq->qn);
376 for (i = 0; i < sq->qn; i++)
378 sq->q[i] = start_q+i*q_step;
381 if (start_q == 0.0)
383 sq->s[0] = 1.0;
384 for (i = 1; i < sq->qn; i++)
386 for (j = 0; j < pr->grn; j++)
388 sq->s[i] += (pr->gr[j]/pr->r[j])*std::sin(sq->q[i]*pr->r[j]);
390 sq->s[i] /= sq->q[i];
393 else
395 for (i = 0; i < sq->qn; i++)
397 for (j = 0; j < pr->grn; j++)
399 sq->s[i] += (pr->gr[j]/pr->r[j])*std::sin(sq->q[i]*pr->r[j]);
401 sq->s[i] /= sq->q[i];
405 return sq;