2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017,2018,2019, 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.
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.
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.
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.
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.
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.
46 #include "gromacs/fileio/confio.h"
47 #include "gromacs/fileio/trxio.h"
48 #include "gromacs/fileio/xvgr.h"
49 #include "gromacs/math/functions.h"
50 #include "gromacs/math/utilities.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/mdtypes/md_enums.h"
53 #include "gromacs/topology/index.h"
54 #include "gromacs/topology/topology.h"
55 #include "gromacs/trajectory/trajectoryframe.h"
56 #include "gromacs/utility/arraysize.h"
57 #include "gromacs/utility/cstringutil.h"
58 #include "gromacs/utility/fatalerror.h"
59 #include "gromacs/utility/futil.h"
60 #include "gromacs/utility/smalloc.h"
61 #include "gromacs/utility/strdb.h"
64 typedef struct gmx_structurefactors
67 int* p; /* proton number */
68 int* n; /* neutron number */
69 /* Parameters for the Cromer Mann fit */
70 real** a; /* parameter a */
71 real** b; /* parameter b */
72 real* c; /* parameter c */
73 char** atomnm; /* atomname */
75 } gmx_structurefactors;
77 typedef struct reduced_atom
84 typedef struct structure_factor
98 extern int* create_indexed_atom_type(reduced_atom_t* atm, int size)
101 * create an index of the atom types found in a group
102 * i.e.: for water index_atp[0]=type_number_of_O and
103 * index_atp[1]=type_number_of_H
105 * the last element is set to 0
107 int *index_atp, i, i_tmp, j;
109 reduced_atom* att = static_cast<reduced_atom*>(atm);
113 index_atp[0] = att[0].t;
114 for (i = 1; i < size; i++)
116 for (j = 0; j < i_tmp; j++)
118 if (att[i].t == index_atp[j])
123 if (j == i_tmp) /* i.e. no indexed atom type is == to atm[i].t */
126 srenew(index_atp, i_tmp * sizeof(int));
127 index_atp[i_tmp - 1] = att[i].t;
131 srenew(index_atp, i_tmp * sizeof(int));
132 index_atp[i_tmp - 1] = 0;
137 extern t_complex*** rc_tensor_allocation(int x, int y, int z)
144 snew(t[0][0], x * y * z);
146 for (j = 1; j < y; j++)
148 t[0][j] = t[0][j - 1] + z;
150 for (i = 1; i < x; i++)
153 t[i][0] = t[i - 1][0] + y * z;
154 for (j = 1; j < y; j++)
156 t[i][j] = t[i][j - 1] + z;
163 extern void compute_structure_factor(structure_factor_t* sft,
172 structure_factor* sf = static_cast<structure_factor*>(sft);
173 reduced_atom* redt = static_cast<reduced_atom*>(red);
177 real kdotx, asf, kx, ky, kz, krr;
178 int kr, maxkx, maxky, maxkz, i, j, k, p, *counter;
181 k_factor[XX] = 2 * M_PI / box[XX][XX];
182 k_factor[YY] = 2 * M_PI / box[YY][YY];
183 k_factor[ZZ] = 2 * M_PI / box[ZZ][ZZ];
185 maxkx = gmx::roundToInt(end_q / k_factor[XX]);
186 maxky = gmx::roundToInt(end_q / k_factor[YY]);
187 maxkz = gmx::roundToInt(end_q / k_factor[ZZ]);
189 snew(counter, sf->n_angles);
191 tmpSF = rc_tensor_allocation(maxkx, maxky, maxkz);
194 * compute real and imaginary part of the structure factor for every
197 fprintf(stderr, "\n");
198 for (i = 0; i < maxkx; i++)
200 fprintf(stderr, "\rdone %3.1f%% ", (100.0 * (i + 1)) / maxkx);
202 kx = i * k_factor[XX];
203 for (j = 0; j < maxky; j++)
205 ky = j * k_factor[YY];
206 for (k = 0; k < maxkz; k++)
208 if (i != 0 || j != 0 || k != 0)
210 kz = k * k_factor[ZZ];
211 krr = std::sqrt(gmx::square(kx) + gmx::square(ky) + gmx::square(kz));
212 if (krr >= start_q && krr <= end_q)
214 kr = gmx::roundToInt(krr / sf->ref_k);
215 if (kr < sf->n_angles)
217 counter[kr]++; /* will be used for the copmutation
219 for (p = 0; p < isize; p++)
221 asf = sf_table[redt[p].t][kr];
223 kdotx = kx * redt[p].x[XX] + ky * redt[p].x[YY] + kz * redt[p].x[ZZ];
225 tmpSF[i][j][k].re += std::cos(kdotx) * asf;
226 tmpSF[i][j][k].im += std::sin(kdotx) * asf;
233 } /* end loop on i */
235 * compute the square modulus of the structure factor, averaging on the surface
236 * kx*kx + ky*ky + kz*kz = krr*krr
237 * note that this is correct only for a (on the macroscopic scale)
240 for (i = 0; i < maxkx; i++)
242 kx = i * k_factor[XX];
243 for (j = 0; j < maxky; j++)
245 ky = j * k_factor[YY];
246 for (k = 0; k < maxkz; k++)
248 kz = k * k_factor[ZZ];
249 krr = std::sqrt(gmx::square(kx) + gmx::square(ky) + gmx::square(kz));
250 if (krr >= start_q && krr <= end_q)
252 kr = gmx::roundToInt(krr / sf->ref_k);
253 if (kr < sf->n_angles && counter[kr] != 0)
256 (gmx::square(tmpSF[i][j][k].re) + gmx::square(tmpSF[i][j][k].im))
270 extern gmx_structurefactors_t* gmx_structurefactors_init(const char* datfn)
273 /* Read the database for the structure factor of the different atoms */
276 gmx_structurefactors* gsf;
277 double a1, a2, a3, a4, b1, b2, b3, b4, c;
283 gmx::FilePtr fp = gmx::openLibraryFile(datfn);
287 snew(gsf->atomnm, nralloc);
288 snew(gsf->a, nralloc);
289 snew(gsf->b, nralloc);
290 snew(gsf->c, nralloc);
291 snew(gsf->p, nralloc);
293 gsf->nratoms = line_no;
294 while (get_a_line(fp.get(), line, STRLEN))
297 if (sscanf(line, "%s %d %lf %lf %lf %lf %lf %lf %lf %lf %lf", atomn, &p, &a1, &a2, &a3, &a4,
298 &b1, &b2, &b3, &b4, &c)
301 gsf->atomnm[i] = gmx_strdup(atomn);
315 gsf->nratoms = line_no;
316 if (line_no == nralloc)
319 srenew(gsf->atomnm, nralloc);
320 srenew(gsf->a, nralloc);
321 srenew(gsf->b, nralloc);
322 srenew(gsf->c, nralloc);
323 srenew(gsf->p, nralloc);
328 fprintf(stderr, "WARNING: Error in file %s at line %d ignored\n", datfn, line_no);
332 srenew(gsf->atomnm, gsf->nratoms);
333 srenew(gsf->a, gsf->nratoms);
334 srenew(gsf->b, gsf->nratoms);
335 srenew(gsf->c, gsf->nratoms);
336 srenew(gsf->p, gsf->nratoms);
338 return static_cast<gmx_structurefactors_t*>(gsf);
342 extern void rearrange_atoms(reduced_atom_t* positions,
346 const t_topology* top,
348 gmx_structurefactors_t* gsf)
349 /* given the group's index, return the (continuous) array of atoms */
353 reduced_atom* pos = static_cast<reduced_atom*>(positions);
357 for (i = 0; i < isize; i++)
359 pos[i].t = return_atom_type(*(top->atoms.atomname[index[i]]), gsf);
362 for (i = 0; i < isize; i++)
364 copy_rvec(fr->x[index[i]], pos[i].x);
369 extern int return_atom_type(const char* name, gmx_structurefactors_t* gsf)
376 t_united_h uh[] = { { "CH1", 1 }, { "CH2", 2 }, { "CH3", 3 }, { "CS1", 1 }, { "CS2", 2 },
377 { "CS3", 3 }, { "CP1", 1 }, { "CP2", 2 }, { "CP3", 3 } };
384 gmx_structurefactors* gsft = static_cast<gmx_structurefactors*>(gsf);
386 NCMT = gsft->nratoms;
390 for (i = 0; (i < asize(uh)); i++)
392 if (std::strcmp(name, uh[i].name) == 0)
394 return NCMT - 1 + uh[i].nh;
398 for (i = 0; (i < NCMT); i++)
400 if (std::strncmp(name, gsft->atomnm[i], std::strlen(gsft->atomnm[i])) == 0)
409 gmx_fatal(FARGS, "\nError: atom (%s) not in list (%d types checked)!\n", name, i);
414 for (i = 0; i < cnt; i++)
416 if (std::strlen(gsft->atomnm[tndx[i]]) > static_cast<size_t>(nrc))
418 nrc = std::strlen(gsft->atomnm[tndx[i]]);
427 extern int gmx_structurefactors_get_sf(gmx_structurefactors_t* gsf, int elem, real a[4], real b[4], real* c)
432 gmx_structurefactors* gsft = static_cast<gmx_structurefactors*>(gsf);
435 for (i = 0; i < 4; i++)
437 a[i] = gsft->a[elem][i];
438 b[i] = gsft->b[elem][i];
446 extern int do_scattering_intensity(const char* fnTPS,
455 const gmx_output_env_t* oenv)
457 int i, *isize, flags = TRX_READ_X, **index_atp;
464 reduced_atom_t** red;
465 structure_factor* sf;
471 gmx_structurefactors_t* gmx_sf;
478 gmx_sf = gmx_structurefactors_init(fnDAT);
480 gmx_structurefactors_get_sf(gmx_sf, 0, a, b, &c);
485 /* Read the topology informations */
486 read_tps_conf(fnTPS, &top, &ePBC, &xtop, nullptr, box, TRUE);
489 /* groups stuff... */
494 fprintf(stderr, "\nSelect %d group%s\n", ng, ng == 1 ? "" : "s");
497 get_index(&top.atoms, fnNDX, ng, isize, index, grpname);
501 rd_index(fnNDX, ng, isize, index, grpname);
504 /* The first time we read data is a little special */
505 read_first_frame(oenv, &status, fnTRX, &fr, flags);
507 sf->total_n_atoms = fr.natoms;
512 r_tmp = std::max(box[XX][XX], box[YY][YY]);
513 r_tmp = std::max(box[ZZ][ZZ], r_tmp);
515 sf->ref_k = (2.0 * M_PI) / (r_tmp);
516 /* ref_k will be the reference momentum unit */
517 sf->n_angles = gmx::roundToInt(end_q / sf->ref_k);
520 for (i = 0; i < ng; i++)
522 snew(sf->F[i], sf->n_angles);
524 for (i = 0; i < ng; i++)
526 snew(red[i], isize[i]);
527 rearrange_atoms(red[i], &fr, index[i], isize[i], &top, TRUE, gmx_sf);
528 index_atp[i] = create_indexed_atom_type(red[i], isize[i]);
531 sf_table = compute_scattering_factor_table(gmx_sf, static_cast<structure_factor_t*>(sf));
534 /* This is the main loop over frames */
539 for (i = 0; i < ng; i++)
541 rearrange_atoms(red[i], &fr, index[i], isize[i], &top, FALSE, gmx_sf);
543 compute_structure_factor(static_cast<structure_factor_t*>(sf), box, red[i], isize[i],
544 start_q, end_q, i, sf_table);
548 while (read_next_frame(oenv, status, &fr));
550 save_data(static_cast<structure_factor_t*>(sf), fnXVG, ng, start_q, end_q, oenv);
556 gmx_structurefactors_done(gmx_sf);
562 extern void save_data(structure_factor_t* sft,
567 const gmx_output_env_t* oenv)
572 double *tmp, polarization_factor, A;
574 structure_factor* sf = static_cast<structure_factor*>(sft);
576 fp = xvgropen(file, "Scattering Intensity", "q (1/nm)", "Intensity (a.u.)", oenv);
580 for (g = 0; g < ngrps; g++)
582 for (i = 0; i < sf->n_angles; i++)
586 * theta is half the angle between incoming and scattered vectors.
588 * polar. fact. = 0.5*(1+cos^2(2*theta)) = 1 - 0.5 * sin^2(2*theta)
590 * sin(theta) = q/(2k) := A -> sin^2(theta) = 4*A^2 (1-A^2) ->
591 * -> 0.5*(1+cos^2(2*theta)) = 1 - 2 A^2 (1-A^2)
593 A = static_cast<double>(i * sf->ref_k) / (2.0 * sf->momentum);
594 polarization_factor = 1 - 2.0 * gmx::square(A) * (1 - gmx::square(A));
595 sf->F[g][i] *= polarization_factor;
598 for (i = 0; i < sf->n_angles; i++)
600 if (i * sf->ref_k >= start_q && i * sf->ref_k <= end_q)
602 fprintf(fp, "%10.5f ", i * sf->ref_k);
603 for (g = 0; g < ngrps; g++)
605 fprintf(fp, " %10.5f ", (sf->F[g][i]) / (sf->total_n_atoms * sf->nSteps));
615 extern double CMSF(gmx_structurefactors_t* gsf, int type, int nh, double lambda, double sin_theta)
617 * return Cromer-Mann fit for the atomic scattering factor:
618 * sin_theta is the sine of half the angle between incoming and scattered
619 * vectors. See g_sq.h for a short description of CM fit.
623 double tmp = 0.0, k2;
632 * f0[k] = c + [SUM a_i*EXP(-b_i*(k^2)) ]
642 tmp = (CMSF(gsf, return_atom_type("C", gsf), 0, lambda, sin_theta)
643 + nh * CMSF(gsf, return_atom_type("H", gsf), 0, lambda, sin_theta));
648 k2 = (gmx::square(sin_theta) / gmx::square(10.0 * lambda));
649 gmx_structurefactors_get_sf(gsf, type, a, b, &c);
651 for (i = 0; (i < 4); i++)
653 tmp += a[i] * exp(-b[i] * k2);
660 extern real** gmx_structurefactors_table(gmx_structurefactors_t* gsf, real momentum, real ref_k, real lambda, int n_angles)
668 gmx_structurefactors* gsft = static_cast<gmx_structurefactors*>(gsf);
670 NCMT = gsft->nratoms;
673 snew(sf_table, nsftable);
674 for (i = 0; (i < nsftable); i++)
676 snew(sf_table[i], n_angles);
677 for (j = 0; j < n_angles; j++)
679 q = static_cast<double>(j * ref_k);
680 /* theta is half the angle between incoming
681 and scattered wavevectors. */
682 sin_theta = q / (2.0 * momentum);
685 sf_table[i][j] = CMSF(gsf, i, 0, lambda, sin_theta);
689 sf_table[i][j] = CMSF(gsf, i, i - NCMT + 1, lambda, sin_theta);
696 extern void gmx_structurefactors_done(gmx_structurefactors_t* gsf)
700 gmx_structurefactors* sf;
701 sf = static_cast<gmx_structurefactors*>(gsf);
703 for (i = 0; i < sf->nratoms; i++)
707 sfree(sf->atomnm[i]);
719 extern real** compute_scattering_factor_table(gmx_structurefactors_t* gsf, structure_factor_t* sft)
722 * this function build up a table of scattering factors for every atom
723 * type and for every scattering angle.
726 double hc = 1239.842;
729 structure_factor* sf = static_cast<structure_factor*>(sft);
732 /* \hbar \omega \lambda = hc = 1239.842 eV * nm */
733 sf->momentum = (static_cast<double>(2. * 1000.0 * M_PI * sf->energy) / hc);
734 sf->lambda = hc / (1000.0 * sf->energy);
735 fprintf(stderr, "\nwavelenght = %f nm\n", sf->lambda);
737 sf_table = gmx_structurefactors_table(gsf, sf->momentum, sf->ref_k, sf->lambda, sf->n_angles);