Introduce SimulatorBuilder
[gromacs.git] / src / gromacs / gmxana / gmx_sans.cpp
blobb08c5dc36f2b3fbb622cb520d484412dfeb146ce
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.
36 #include "gmxpre.h"
38 #include "config.h"
40 #include <array>
42 #include "gromacs/commandline/pargs.h"
43 #include "gromacs/fileio/confio.h"
44 #include "gromacs/fileio/trxio.h"
45 #include "gromacs/fileio/xvgr.h"
46 #include "gromacs/gmxana/gmx_ana.h"
47 #include "gromacs/gmxana/gstat.h"
48 #include "gromacs/gmxana/nsfactor.h"
49 #include "gromacs/math/vec.h"
50 #include "gromacs/pbcutil/rmpbc.h"
51 #include "gromacs/topology/index.h"
52 #include "gromacs/topology/topology.h"
53 #include "gromacs/utility/arraysize.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/fatalerror.h"
56 #include "gromacs/utility/futil.h"
57 #include "gromacs/utility/gmxassert.h"
58 #include "gromacs/utility/gmxomp.h"
59 #include "gromacs/utility/pleasecite.h"
60 #include "gromacs/utility/smalloc.h"
62 int gmx_sans(int argc, char *argv[])
64 const char *desc[] = {
65 "[THISMODULE] computes SANS spectra using Debye formula.",
66 "It currently uses topology file (since it need to assigne element for each atom).",
67 "[PAR]",
68 "Parameters:[PAR]",
69 "[TT]-pr[tt] Computes normalized g(r) function averaged over trajectory[PAR]",
70 "[TT]-prframe[tt] Computes normalized g(r) function for each frame[PAR]",
71 "[TT]-sq[tt] Computes SANS intensity curve averaged over trajectory[PAR]",
72 "[TT]-sqframe[tt] Computes SANS intensity curve for each frame[PAR]",
73 "[TT]-startq[tt] Starting q value in nm[PAR]",
74 "[TT]-endq[tt] Ending q value in nm[PAR]",
75 "[TT]-qstep[tt] Stepping in q space[PAR]",
76 "Note: When using Debye direct method computational cost increases as",
77 "1/2 * N * (N - 1) where N is atom number in group of interest.",
78 "[PAR]",
79 "WARNING: If sq or pr specified this tool can produce large number of files! Up to two times larger than number of frames!"
81 static gmx_bool bPBC = TRUE;
82 static gmx_bool bNORM = FALSE;
83 static real binwidth = 0.2, grid = 0.05; /* bins shouldn't be smaller then smallest bond (~0.1nm) length */
84 static real start_q = 0.0, end_q = 2.0, q_step = 0.01;
85 static real mcover = -1;
86 static unsigned int seed = 0;
87 static int nthreads = -1;
89 static const char *emode[] = { nullptr, "direct", "mc", nullptr };
90 static const char *emethod[] = { nullptr, "debye", "fft", nullptr };
92 gmx_neutron_atomic_structurefactors_t *gnsf;
93 gmx_sans_t *gsans;
95 #define NPA asize(pa)
97 t_pargs pa[] = {
98 { "-bin", FALSE, etREAL, {&binwidth},
99 "[HIDDEN]Binwidth (nm)" },
100 { "-mode", FALSE, etENUM, {emode},
101 "Mode for sans spectra calculation" },
102 { "-mcover", FALSE, etREAL, {&mcover},
103 "Monte-Carlo coverage should be -1(default) or (0,1]"},
104 { "-method", FALSE, etENUM, {emethod},
105 "[HIDDEN]Method for sans spectra calculation" },
106 { "-pbc", FALSE, etBOOL, {&bPBC},
107 "Use periodic boundary conditions for computing distances" },
108 { "-grid", FALSE, etREAL, {&grid},
109 "[HIDDEN]Grid spacing (in nm) for FFTs" },
110 {"-startq", FALSE, etREAL, {&start_q},
111 "Starting q (1/nm) "},
112 {"-endq", FALSE, etREAL, {&end_q},
113 "Ending q (1/nm)"},
114 { "-qstep", FALSE, etREAL, {&q_step},
115 "Stepping in q (1/nm)"},
116 { "-seed", FALSE, etINT, {&seed},
117 "Random seed for Monte-Carlo"},
118 #if GMX_OPENMP
119 { "-nt", FALSE, etINT, {&nthreads},
120 "Number of threads to start"},
121 #endif
123 FILE *fp;
124 const char *fnTPX, *fnTRX, *fnDAT = nullptr;
125 t_trxstatus *status;
126 t_topology *top = nullptr;
127 gmx_rmpbc_t gpbc = nullptr;
128 gmx_bool bFFT = FALSE, bDEBYE = FALSE;
129 gmx_bool bMC = FALSE;
130 int ePBC = -1;
131 matrix box;
132 rvec *x;
133 int natoms;
134 real t;
135 char **grpname = nullptr;
136 int *index = nullptr;
137 int isize;
138 int i;
139 char *hdr = nullptr;
140 char *suffix = nullptr;
141 gmx_radial_distribution_histogram_t *prframecurrent = nullptr, *pr = nullptr;
142 gmx_static_structurefactor_t *sqframecurrent = nullptr, *sq = nullptr;
143 gmx_output_env_t *oenv;
145 std::array<t_filenm, 8> filenames =
146 { { { efTPR, "-s", nullptr, ffREAD },
147 { efTRX, "-f", nullptr, ffREAD },
148 { efNDX, nullptr, nullptr, ffOPTRD },
149 { efDAT, "-d", "nsfactor", ffOPTRD },
150 { efXVG, "-pr", "pr", ffWRITE },
151 { efXVG, "-sq", "sq", ffWRITE },
152 { efXVG, "-prframe", "prframe", ffOPTWR },
153 { efXVG, "-sqframe", "sqframe", ffOPTWR } } };
154 t_filenm *fnm = filenames.data();
156 const auto NFILE = filenames.size();
158 nthreads = gmx_omp_get_max_threads();
160 if (!parse_common_args(&argc, argv, PCA_CAN_TIME | PCA_TIME_UNIT,
161 NFILE, fnm, asize(pa), pa, asize(desc), desc, 0, nullptr, &oenv))
163 return 0;
166 /* check that binwidth not smaller than smallers distance */
167 check_binwidth(binwidth);
168 check_mcover(mcover);
170 /* setting number of omp threads globaly */
171 gmx_omp_set_num_threads(nthreads);
173 /* Now try to parse opts for modes */
174 GMX_RELEASE_ASSERT(emethod[0] != nullptr, "Options inconsistency; emethod[0] is NULL");
175 switch (emethod[0][0])
177 case 'd':
178 bDEBYE = TRUE;
179 switch (emode[0][0])
181 case 'd':
182 bMC = FALSE;
183 break;
184 case 'm':
185 bMC = TRUE;
186 break;
187 default:
188 break;
190 break;
191 case 'f':
192 bFFT = TRUE;
193 break;
194 default:
195 break;
198 if (bDEBYE)
200 if (bMC)
202 fprintf(stderr, "Using Monte Carlo Debye method to calculate spectrum\n");
204 else
206 fprintf(stderr, "Using direct Debye method to calculate spectrum\n");
209 else if (bFFT)
211 gmx_fatal(FARGS, "FFT method not implemented!");
213 else
215 gmx_fatal(FARGS, "Unknown combination for mode and method!");
218 /* Try to read files */
219 fnDAT = ftp2fn(efDAT, NFILE, fnm);
220 fnTPX = ftp2fn(efTPR, NFILE, fnm);
221 fnTRX = ftp2fn(efTRX, NFILE, fnm);
223 gnsf = gmx_neutronstructurefactors_init(fnDAT);
224 fprintf(stderr, "Read %d atom names from %s with neutron scattering parameters\n\n", gnsf->nratoms, fnDAT);
226 snew(top, 1);
227 snew(grpname, 1);
228 snew(index, 1);
230 read_tps_conf(fnTPX, top, &ePBC, &x, nullptr, box, TRUE);
232 printf("\nPlease select group for SANS spectra calculation:\n");
233 get_index(&(top->atoms), ftp2fn_null(efNDX, NFILE, fnm), 1, &isize, &index, grpname);
235 gsans = gmx_sans_init(top, gnsf);
237 /* Prepare reference frame */
238 if (bPBC)
240 gpbc = gmx_rmpbc_init(&top->idef, ePBC, top->atoms.nr);
241 gmx_rmpbc(gpbc, top->atoms.nr, box, x);
244 natoms = read_first_x(oenv, &status, fnTRX, &t, &x, box);
245 if (natoms != top->atoms.nr)
247 fprintf(stderr, "\nWARNING: number of atoms in tpx (%d) and trajectory (%d) do not match\n", natoms, top->atoms.nr);
252 if (bPBC)
254 gmx_rmpbc(gpbc, top->atoms.nr, box, x);
256 /* allocate memory for pr */
257 if (pr == nullptr)
259 /* in case its first frame to read */
260 snew(pr, 1);
262 /* realy calc p(r) */
263 prframecurrent = calc_radial_distribution_histogram(gsans, x, box, index, isize, binwidth, bMC, bNORM, mcover, seed);
264 /* copy prframecurrent -> pr and summ up pr->gr[i] */
265 /* allocate and/or resize memory for pr->gr[i] and pr->r[i] */
266 if (pr->gr == nullptr)
268 /* check if we use pr->gr first time */
269 snew(pr->gr, prframecurrent->grn);
270 snew(pr->r, prframecurrent->grn);
272 else
274 /* resize pr->gr and pr->r if needed to preven overruns */
275 if (prframecurrent->grn > pr->grn)
277 srenew(pr->gr, prframecurrent->grn);
278 srenew(pr->r, prframecurrent->grn);
281 pr->grn = prframecurrent->grn;
282 pr->binwidth = prframecurrent->binwidth;
283 /* summ up gr and fill r */
284 for (i = 0; i < prframecurrent->grn; i++)
286 pr->gr[i] += prframecurrent->gr[i];
287 pr->r[i] = prframecurrent->r[i];
289 /* normalize histo */
290 normalize_probability(prframecurrent->grn, prframecurrent->gr);
291 /* convert p(r) to sq */
292 sqframecurrent = convert_histogram_to_intensity_curve(prframecurrent, start_q, end_q, q_step);
293 /* print frame data if needed */
294 if (opt2fn_null("-prframe", NFILE, fnm))
296 snew(hdr, 25);
297 snew(suffix, GMX_PATH_MAX);
298 /* prepare header */
299 sprintf(hdr, "g(r), t = %f", t);
300 /* prepare output filename */
301 auto fnmdup = filenames;
302 sprintf(suffix, "-t%.2f", t);
303 add_suffix_to_output_names(fnmdup.data(), NFILE, suffix);
304 fp = xvgropen(opt2fn_null("-prframe", NFILE, fnmdup.data()), hdr, "Distance (nm)", "Probability", oenv);
305 for (i = 0; i < prframecurrent->grn; i++)
307 fprintf(fp, "%10.6f%10.6f\n", prframecurrent->r[i], prframecurrent->gr[i]);
309 xvgrclose(fp);
310 sfree(hdr);
311 sfree(suffix);
313 if (opt2fn_null("-sqframe", NFILE, fnm))
315 snew(hdr, 25);
316 snew(suffix, GMX_PATH_MAX);
317 /* prepare header */
318 sprintf(hdr, "I(q), t = %f", t);
319 /* prepare output filename */
320 auto fnmdup = filenames;
321 sprintf(suffix, "-t%.2f", t);
322 add_suffix_to_output_names(fnmdup.data(), NFILE, suffix);
323 fp = xvgropen(opt2fn_null("-sqframe", NFILE, fnmdup.data()), hdr, "q (nm^-1)", "s(q)/s(0)", oenv);
324 for (i = 0; i < sqframecurrent->qn; i++)
326 fprintf(fp, "%10.6f%10.6f\n", sqframecurrent->q[i], sqframecurrent->s[i]);
328 xvgrclose(fp);
329 sfree(hdr);
330 sfree(suffix);
332 /* free pr structure */
333 sfree(prframecurrent->gr);
334 sfree(prframecurrent->r);
335 sfree(prframecurrent);
336 /* free sq structure */
337 sfree(sqframecurrent->q);
338 sfree(sqframecurrent->s);
339 sfree(sqframecurrent);
341 while (read_next_x(oenv, status, &t, x, box));
342 close_trx(status);
344 /* normalize histo */
345 normalize_probability(pr->grn, pr->gr);
346 sq = convert_histogram_to_intensity_curve(pr, start_q, end_q, q_step);
347 /* prepare pr.xvg */
348 fp = xvgropen(opt2fn_null("-pr", NFILE, fnm), "G(r)", "Distance (nm)", "Probability", oenv);
349 for (i = 0; i < pr->grn; i++)
351 fprintf(fp, "%10.6f%10.6f\n", pr->r[i], pr->gr[i]);
353 xvgrclose(fp);
355 /* prepare sq.xvg */
356 fp = xvgropen(opt2fn_null("-sq", NFILE, fnm), "I(q)", "q (nm^-1)", "s(q)/s(0)", oenv);
357 for (i = 0; i < sq->qn; i++)
359 fprintf(fp, "%10.6f%10.6f\n", sq->q[i], sq->s[i]);
361 xvgrclose(fp);
363 * Clean up memory
365 sfree(pr->gr);
366 sfree(pr->r);
367 sfree(pr);
368 sfree(sq->q);
369 sfree(sq->s);
370 sfree(sq);
372 please_cite(stdout, "Garmay2012");
374 return 0;