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-2008, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
49 #include "gmx_fatal.h"
57 int nener,nomega,nq; /* Number of entries in the table */
58 real *ener; /* Energy values */
59 real **omega; /* Omega values (energy dependent) */
60 real ***prob,***q; /* Probability and Q */
64 int nener,n2Ddata; /* Number of entries in the table */
65 real *ener; /* Energy values */
66 real **prob,**data; /* Probability and data (energy loss) */
69 static int realcomp(const void *a,const void *b)
83 static t_p2Ddata *read_p2Ddata(char *fn)
90 fprintf(stdout,"Going to read %s\n",fn);
93 /* Allocate memory and set constants */
95 if (fscanf(fp,"%d%d",&p2Ddata->nener,&p2Ddata->n2Ddata) != 2)
96 gmx_fatal(FARGS,"I need two integers: nener, n in file %s",fn);
98 snew(p2Ddata->ener,p2Ddata->nener);
99 snew(p2Ddata->prob,p2Ddata->nener);
100 snew(p2Ddata->data,p2Ddata->nener);
102 /* Double loop to read data */
103 for(i=0; (i<p2Ddata->nener); i++) {
104 fprintf(stderr,"\rEnergy %d/%d",i+1,p2Ddata->nener);
105 snew(p2Ddata->prob[i],p2Ddata->n2Ddata);
106 snew(p2Ddata->data[i],p2Ddata->n2Ddata);
108 for(j=0; (j<p2Ddata->n2Ddata); j++) {
109 fscanf(fp,"%lf%lf%lf",&e,&p,&o);
111 /* Consistency check */
113 p2Ddata->ener[i] = e;
114 else if (fabs(p2Ddata->ener[i]-e) > 1e-6*e)
115 gmx_fatal(FARGS,"Inconsistent energy %f i=%d j=%d",e,i,j);
116 p2Ddata->prob[i][j] = p;
117 p2Ddata->data[i][j] = o;
119 /* There is some noise on the data, take it away by sorting,
120 * because otherwise binary search does not work.
121 * This is equivalent to shifting in the data slightly along the X-axis
122 * but better than linear search with the "real" data.
124 qsort(p2Ddata->data[i],p2Ddata->n2Ddata,sizeof(p2Ddata->data[0][0]),
127 fprintf(stderr,"\n");
134 static t_pq_inel *read_pq(char *fn)
141 fprintf(stdout,"Going to read %s\n",fn);
144 /* Allocate memory and set constants */
146 if (fscanf(fp,"%d%d%d",&pq->nener,&pq->nomega,&pq->nq) != 3)
147 gmx_fatal(FARGS,"I need three integers: nener, nomega, nq in file %s",fn);
149 snew(pq->ener,pq->nener);
150 snew(pq->omega,pq->nener);
151 snew(pq->prob,pq->nener);
152 snew(pq->q,pq->nener);
154 /* Triple loop to read data */
155 for(i=0; (i<pq->nener); i++) {
156 fprintf(stderr,"\rEnergy %d/%d",i+1,pq->nener);
157 snew(pq->prob[i],pq->nomega);
158 snew(pq->q[i],pq->nomega);
159 snew(pq->omega[i],pq->nomega);
161 for(j=0; (j<pq->nomega); j++) {
162 snew(pq->prob[i][j],pq->nq);
163 snew(pq->q[i][j],pq->nq);
165 for(k=0; (k<pq->nq); k++) {
166 fscanf(fp,"%lf%lf%lf%lf",&e,&o,&p,&t);
168 /* Consistency check */
169 if ((j == 0) && (k == 0))
171 else if (fabs(pq->ener[i]-e) > 1e-6*e)
172 gmx_fatal(FARGS,"Inconsistent energy %f i=%d j=%d k=%d",e,i,j,k);
176 else if (fabs(pq->omega[i][j]-o) > 1e-6*o)
177 gmx_fatal(FARGS,"Inconsistent omega %f i=%d j=%d k=%d",o,i,j,k);
179 pq->prob[i][j][k] = p;
184 fprintf(stderr,"\n");
191 static int my_bsearch(real val,int ndata,real data[],gmx_bool bLower)
199 while ((ihi - ilo) > 1) {
201 if (data[imed] > val)
206 /* Now val should be in between data[ilo] and data[ihi] */
207 /* Decide which one is closest */
208 if (bLower || ((val-data[ilo]) <= (data[ihi]-val)))
214 static real interpolate2D(int nx,int ny,real *dx,real **data,
215 real x0,real fy,int nx0,int ny0)
219 fx = (x0-dx[nx0])/(dx[nx0+1]-dx[nx0]);
221 return (fx*fy*data[nx0][ny0] + fx*(1-fy)*data[nx0][ny0+1] +
222 (1-fx)*fy*data[nx0+1][ny0] + (1-fx)*(1-fy)*data[nx0+1][ny0+1]);
225 real get_omega(real ekin,int *seed,FILE *fp,char *fn)
227 static t_p2Ddata *p2Ddata = NULL;
232 p2Ddata = read_p2Ddata(fn);
234 /* Get energy index by binary search */
235 if ((eindex = my_bsearch(ekin,p2Ddata->nener,p2Ddata->ener,TRUE)) >= 0) {
237 if (eindex >= p2Ddata->nener)
238 gmx_fatal(FARGS,"eindex (%d) out of range (max %d) in get_omega",
239 eindex,p2Ddata->nener);
242 /* Start with random number */
245 /* Do binary search in the energy table */
246 if ((oindex = my_bsearch(r,p2Ddata->n2Ddata,p2Ddata->prob[eindex],TRUE)) >= 0) {
248 if (oindex >= p2Ddata->n2Ddata)
249 gmx_fatal(FARGS,"oindex (%d) out of range (max %d) in get_omega",
250 oindex,p2Ddata->n2Ddata);
253 fy = ((r-p2Ddata->prob[eindex][oindex])/
254 (p2Ddata->prob[eindex][oindex+1]-p2Ddata->prob[eindex][oindex]));
255 ome = interpolate2D(p2Ddata->nener,p2Ddata->n2Ddata,p2Ddata->ener,
256 p2Ddata->data,ekin,fy,
258 /* ome = p2Ddata->data[eindex][oindex];*/
261 fprintf(fp,"%8.3f %8.3f\n",ome,r);
269 real get_theta_el(real ekin,int *seed,FILE *fp,char *fn)
271 static t_p2Ddata *p2Ddata = NULL;
276 p2Ddata = read_p2Ddata(fn);
278 /* Get energy index by binary search */
279 if ((eindex = my_bsearch(ekin,p2Ddata->nener,p2Ddata->ener,TRUE)) >= 0) {
281 /* Start with random number */
284 /* Do binary search in the energy table */
285 if ((tindex = my_bsearch(r,p2Ddata->n2Ddata,p2Ddata->prob[eindex],FALSE)) >= 0) {
287 theta = p2Ddata->data[eindex][tindex];
290 fprintf(fp,"%8.3f %8.3f\n",theta,r);
298 real get_q_inel(real ekin,real omega,int *seed,FILE *fp,char *fn)
300 static t_pq_inel *pq = NULL;
301 int eindex,oindex,tindex;
307 /* Get energy index by binary search */
308 if ((eindex = my_bsearch(ekin,pq->nener,pq->ener,TRUE)) >= 0) {
310 if (eindex >= pq->nener)
311 gmx_fatal(FARGS,"eindex out of range (%d >= %d)",eindex,pq->nener);
314 /* Do binary search in the energy table */
315 if ((oindex = my_bsearch(omega,pq->nomega,pq->omega[eindex],FALSE)) >= 0) {
317 if (oindex >= pq->nomega)
318 gmx_fatal(FARGS,"oindex out of range (%d >= %d)",oindex,pq->nomega);
321 /* Start with random number */
324 if ((tindex = my_bsearch(r,pq->nq,pq->prob[eindex][oindex],FALSE)) >= 0) {
326 if (tindex >= pq->nq)
327 gmx_fatal(FARGS,"tindex out of range (%d >= %d)",tindex,pq->nq);
330 theta = pq->q[eindex][oindex][tindex];
333 fprintf(fp,"get_q_inel: %8.3f %8.3f\n",theta,r);
342 static int read_cross(char *fn,real **ener,real **cross,real factor)
348 fprintf(stdout,"Going to read %s\n",fn);
349 n = get_file(fn,&data);
351 /* Allocate memory */
354 for(i=j=0; (i<n); i++) {
355 if (sscanf(data[i],"%lf%lf",&e,&c) == 2) {
357 (*cross)[j] = c*factor;
364 fprintf(stderr,"There were %d empty lines in file %s\n",n-j,fn);
369 real cross_inel(real ekin,real rho,char *fn)
371 static real *ener = NULL;
372 static real *cross = NULL;
376 /* Read data at first call, convert A^2 to nm^2 */
378 ninel = read_cross(fn,&ener,&cross,rho*0.01);
380 /* Compute index with binary search */
381 if ((eindex = my_bsearch(ekin,ninel,ener,FALSE)) >= 0) {
384 gmx_fatal(FARGS,"ekin = %f, ener[0] = %f, ener[%d] = %f",ekin,
385 ener[0],ener[ninel-1]);
387 return cross[eindex];
393 real cross_el(real ekin,real rho,char *fn)
395 static real *ener = NULL;
396 static real *cross = NULL;
400 /* Read data at first call, convert A^2 to nm^2 */
402 nel = read_cross(fn,&ener,&cross,rho*0.01);
404 /* Compute index with binary search */
405 if ((eindex = my_bsearch(ekin,nel,ener,FALSE)) >= 0) {
408 gmx_fatal(FARGS,"ekin = %f, ener[0] = %f, ener[%d] = %f",ekin,
409 ener[0],ener[nel-1]);
412 return cross[eindex];
417 real band_ener(int *seed,FILE *fp,char *fn)
419 static real *ener = NULL;
420 static real *prob = NULL;
425 /* Read data at first call, misuse read_cross function */
427 nener = read_cross(fn,&ener,&prob,1.0);
431 if ((eindex = my_bsearch(r,nener,prob,FALSE)) >= 0) {
434 gmx_fatal(FARGS,"r = %f, prob[0] = %f, prob[%d] = %f",r,
435 prob[0],prob[nener-1]);
439 fprintf(fp,"%8.3f %8.3f\n",ener[eindex],r);
446 static void test_omega(FILE *fp,int *seed)
450 fprintf(fp,"Testing the energy loss tables\n");
451 for(i=0; (i<1000); i++) {
452 (void) get_omega(500*rando(seed),seed,fp,NULL);
456 static void test_q_inel(FILE *fp,int *seed)
460 fprintf(fp,"Testing the energy/omega dependent inelastic scattering q tables\n");
461 for(i=0; (i<1000); i++) {
462 (void) get_q_inel(500*rando(seed),400*rando(seed),seed,fp,NULL);
466 static void test_theta_el(FILE *fp,int *seed)
470 fprintf(fp,"Testing the energy dependent elastic scattering theta tables\n");
471 for(i=0; (i<1000); i++) {
472 (void) get_theta_el(500*rando(seed),seed,fp,NULL);
476 static void test_sigma_el(FILE *fp,real rho)
480 fprintf(fp,"Testing the elastic cross sections table\n");
481 for(i=0; (i<500); i++) {
482 fprintf(fp,"%3d %8.3f\n",i,cross_el(i,rho,NULL));
486 static void test_sigma_inel(FILE *fp,real rho)
490 fprintf(fp,"Testing the inelastic cross sections table\n");
491 for(i=0; (i<500); i++) {
492 fprintf(fp,"%3d %8.3f\n",i,cross_inel(i,rho,NULL));
496 static void test_band_ener(int *seed,FILE *fp)
500 for(i=0; (i<500); i++) {
501 band_ener(seed,fp,NULL);
505 void init_tables(int nfile,t_filenm fnm[],real rho)
511 (void) band_ener(&seed,NULL,opt2fn("-band",nfile,fnm));
512 (void) cross_el(ekin,rho,opt2fn("-sigel",nfile,fnm));
513 (void) cross_inel(ekin,rho,opt2fn("-sigin",nfile,fnm));
514 (void) get_theta_el(ekin,&seed,NULL,opt2fn("-thetael",nfile,fnm));
515 (void) get_omega(ekin,&seed,NULL,opt2fn("-eloss",nfile,fnm));
516 (void) get_q_inel(ekin,omega,&seed,NULL,opt2fn("-qtrans",nfile,fnm));
519 void test_tables(int *seed,char *fn,real rho)
526 test_q_inel(fp,seed);
527 test_theta_el(fp,seed);
528 test_sigma_el(fp,rho);
529 test_sigma_inel(fp,rho);
530 test_band_ener(seed,fp);