3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2008, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * Groningen Machine for Chemical Simulation
42 #include "gromacs/utility/smalloc.h"
44 #include "gmx_fatal.h"
46 #include "gromacs/fileio/strdb.h"
47 #include "gromacs/fileio/futil.h"
52 int nener,nomega,nq; /* Number of entries in the table */
53 real *ener; /* Energy values */
54 real **omega; /* Omega values (energy dependent) */
55 real ***prob,***q; /* Probability and Q */
59 int nener,n2Ddata; /* Number of entries in the table */
60 real *ener; /* Energy values */
61 real **prob,**data; /* Probability and data (energy loss) */
64 static int realcomp(const void *a,const void *b)
78 static t_p2Ddata *read_p2Ddata(char *fn)
85 fprintf(stdout,"Going to read %s\n",fn);
86 fp = gmx_ffopen(fn,"r");
88 /* Allocate memory and set constants */
90 if (fscanf(fp,"%d%d",&p2Ddata->nener,&p2Ddata->n2Ddata) != 2)
91 gmx_fatal(FARGS,"I need two integers: nener, n in file %s",fn);
93 snew(p2Ddata->ener,p2Ddata->nener);
94 snew(p2Ddata->prob,p2Ddata->nener);
95 snew(p2Ddata->data,p2Ddata->nener);
97 /* Double loop to read data */
98 for(i=0; (i<p2Ddata->nener); i++) {
99 fprintf(stderr,"\rEnergy %d/%d",i+1,p2Ddata->nener);
100 snew(p2Ddata->prob[i],p2Ddata->n2Ddata);
101 snew(p2Ddata->data[i],p2Ddata->n2Ddata);
103 for(j=0; (j<p2Ddata->n2Ddata); j++) {
104 fscanf(fp,"%lf%lf%lf",&e,&p,&o);
106 /* Consistency check */
108 p2Ddata->ener[i] = e;
109 else if (fabs(p2Ddata->ener[i]-e) > 1e-6*e)
110 gmx_fatal(FARGS,"Inconsistent energy %f i=%d j=%d",e,i,j);
111 p2Ddata->prob[i][j] = p;
112 p2Ddata->data[i][j] = o;
114 /* There is some noise on the data, take it away by sorting,
115 * because otherwise binary search does not work.
116 * This is equivalent to shifting in the data slightly along the X-axis
117 * but better than linear search with the "real" data.
119 qsort(p2Ddata->data[i],p2Ddata->n2Ddata,sizeof(p2Ddata->data[0][0]),
122 fprintf(stderr,"\n");
129 static t_pq_inel *read_pq(char *fn)
136 fprintf(stdout,"Going to read %s\n",fn);
137 fp = gmx_ffopen(fn,"r");
139 /* Allocate memory and set constants */
141 if (fscanf(fp,"%d%d%d",&pq->nener,&pq->nomega,&pq->nq) != 3)
142 gmx_fatal(FARGS,"I need three integers: nener, nomega, nq in file %s",fn);
144 snew(pq->ener,pq->nener);
145 snew(pq->omega,pq->nener);
146 snew(pq->prob,pq->nener);
147 snew(pq->q,pq->nener);
149 /* Triple loop to read data */
150 for(i=0; (i<pq->nener); i++) {
151 fprintf(stderr,"\rEnergy %d/%d",i+1,pq->nener);
152 snew(pq->prob[i],pq->nomega);
153 snew(pq->q[i],pq->nomega);
154 snew(pq->omega[i],pq->nomega);
156 for(j=0; (j<pq->nomega); j++) {
157 snew(pq->prob[i][j],pq->nq);
158 snew(pq->q[i][j],pq->nq);
160 for(k=0; (k<pq->nq); k++) {
161 fscanf(fp,"%lf%lf%lf%lf",&e,&o,&p,&t);
163 /* Consistency check */
164 if ((j == 0) && (k == 0))
166 else if (fabs(pq->ener[i]-e) > 1e-6*e)
167 gmx_fatal(FARGS,"Inconsistent energy %f i=%d j=%d k=%d",e,i,j,k);
171 else if (fabs(pq->omega[i][j]-o) > 1e-6*o)
172 gmx_fatal(FARGS,"Inconsistent omega %f i=%d j=%d k=%d",o,i,j,k);
174 pq->prob[i][j][k] = p;
179 fprintf(stderr,"\n");
186 static int my_bsearch(real val,int ndata,real data[],gmx_bool bLower)
194 while ((ihi - ilo) > 1) {
196 if (data[imed] > val)
201 /* Now val should be in between data[ilo] and data[ihi] */
202 /* Decide which one is closest */
203 if (bLower || ((val-data[ilo]) <= (data[ihi]-val)))
209 static real interpolate2D(int nx,int ny,real *dx,real **data,
210 real x0,real fy,int nx0,int ny0)
214 fx = (x0-dx[nx0])/(dx[nx0+1]-dx[nx0]);
216 return (fx*fy*data[nx0][ny0] + fx*(1-fy)*data[nx0][ny0+1] +
217 (1-fx)*fy*data[nx0+1][ny0] + (1-fx)*(1-fy)*data[nx0+1][ny0+1]);
220 real get_omega(real ekin,int *seed,FILE *fp,char *fn)
222 static t_p2Ddata *p2Ddata = NULL;
227 p2Ddata = read_p2Ddata(fn);
229 /* Get energy index by binary search */
230 if ((eindex = my_bsearch(ekin,p2Ddata->nener,p2Ddata->ener,TRUE)) >= 0) {
232 if (eindex >= p2Ddata->nener)
233 gmx_fatal(FARGS,"eindex (%d) out of range (max %d) in get_omega",
234 eindex,p2Ddata->nener);
237 /* Start with random number */
240 /* Do binary search in the energy table */
241 if ((oindex = my_bsearch(r,p2Ddata->n2Ddata,p2Ddata->prob[eindex],TRUE)) >= 0) {
243 if (oindex >= p2Ddata->n2Ddata)
244 gmx_fatal(FARGS,"oindex (%d) out of range (max %d) in get_omega",
245 oindex,p2Ddata->n2Ddata);
248 fy = ((r-p2Ddata->prob[eindex][oindex])/
249 (p2Ddata->prob[eindex][oindex+1]-p2Ddata->prob[eindex][oindex]));
250 ome = interpolate2D(p2Ddata->nener,p2Ddata->n2Ddata,p2Ddata->ener,
251 p2Ddata->data,ekin,fy,
253 /* ome = p2Ddata->data[eindex][oindex];*/
256 fprintf(fp,"%8.3f %8.3f\n",ome,r);
264 real get_theta_el(real ekin,int *seed,FILE *fp,char *fn)
266 static t_p2Ddata *p2Ddata = NULL;
271 p2Ddata = read_p2Ddata(fn);
273 /* Get energy index by binary search */
274 if ((eindex = my_bsearch(ekin,p2Ddata->nener,p2Ddata->ener,TRUE)) >= 0) {
276 /* Start with random number */
279 /* Do binary search in the energy table */
280 if ((tindex = my_bsearch(r,p2Ddata->n2Ddata,p2Ddata->prob[eindex],FALSE)) >= 0) {
282 theta = p2Ddata->data[eindex][tindex];
285 fprintf(fp,"%8.3f %8.3f\n",theta,r);
293 real get_q_inel(real ekin,real omega,int *seed,FILE *fp,char *fn)
295 static t_pq_inel *pq = NULL;
296 int eindex,oindex,tindex;
302 /* Get energy index by binary search */
303 if ((eindex = my_bsearch(ekin,pq->nener,pq->ener,TRUE)) >= 0) {
305 if (eindex >= pq->nener)
306 gmx_fatal(FARGS,"eindex out of range (%d >= %d)",eindex,pq->nener);
309 /* Do binary search in the energy table */
310 if ((oindex = my_bsearch(omega,pq->nomega,pq->omega[eindex],FALSE)) >= 0) {
312 if (oindex >= pq->nomega)
313 gmx_fatal(FARGS,"oindex out of range (%d >= %d)",oindex,pq->nomega);
316 /* Start with random number */
319 if ((tindex = my_bsearch(r,pq->nq,pq->prob[eindex][oindex],FALSE)) >= 0) {
321 if (tindex >= pq->nq)
322 gmx_fatal(FARGS,"tindex out of range (%d >= %d)",tindex,pq->nq);
325 theta = pq->q[eindex][oindex][tindex];
328 fprintf(fp,"get_q_inel: %8.3f %8.3f\n",theta,r);
337 static int read_cross(char *fn,real **ener,real **cross,real factor)
343 fprintf(stdout,"Going to read %s\n",fn);
344 n = get_file(fn,&data);
346 /* Allocate memory */
349 for(i=j=0; (i<n); i++) {
350 if (sscanf(data[i],"%lf%lf",&e,&c) == 2) {
352 (*cross)[j] = c*factor;
359 fprintf(stderr,"There were %d empty lines in file %s\n",n-j,fn);
364 real cross_inel(real ekin,real rho,char *fn)
366 static real *ener = NULL;
367 static real *cross = NULL;
371 /* Read data at first call, convert A^2 to nm^2 */
373 ninel = read_cross(fn,&ener,&cross,rho*0.01);
375 /* Compute index with binary search */
376 if ((eindex = my_bsearch(ekin,ninel,ener,FALSE)) >= 0) {
379 gmx_fatal(FARGS,"ekin = %f, ener[0] = %f, ener[%d] = %f",ekin,
380 ener[0],ener[ninel-1]);
382 return cross[eindex];
388 real cross_el(real ekin,real rho,char *fn)
390 static real *ener = NULL;
391 static real *cross = NULL;
395 /* Read data at first call, convert A^2 to nm^2 */
397 nel = read_cross(fn,&ener,&cross,rho*0.01);
399 /* Compute index with binary search */
400 if ((eindex = my_bsearch(ekin,nel,ener,FALSE)) >= 0) {
403 gmx_fatal(FARGS,"ekin = %f, ener[0] = %f, ener[%d] = %f",ekin,
404 ener[0],ener[nel-1]);
407 return cross[eindex];
412 real band_ener(int *seed,FILE *fp,char *fn)
414 static real *ener = NULL;
415 static real *prob = NULL;
420 /* Read data at first call, misuse read_cross function */
422 nener = read_cross(fn,&ener,&prob,1.0);
426 if ((eindex = my_bsearch(r,nener,prob,FALSE)) >= 0) {
429 gmx_fatal(FARGS,"r = %f, prob[0] = %f, prob[%d] = %f",r,
430 prob[0],prob[nener-1]);
434 fprintf(fp,"%8.3f %8.3f\n",ener[eindex],r);
441 static void test_omega(FILE *fp,int *seed)
445 fprintf(fp,"Testing the energy loss tables\n");
446 for(i=0; (i<1000); i++) {
447 (void) get_omega(500*rando(seed),seed,fp,NULL);
451 static void test_q_inel(FILE *fp,int *seed)
455 fprintf(fp,"Testing the energy/omega dependent inelastic scattering q tables\n");
456 for(i=0; (i<1000); i++) {
457 (void) get_q_inel(500*rando(seed),400*rando(seed),seed,fp,NULL);
461 static void test_theta_el(FILE *fp,int *seed)
465 fprintf(fp,"Testing the energy dependent elastic scattering theta tables\n");
466 for(i=0; (i<1000); i++) {
467 (void) get_theta_el(500*rando(seed),seed,fp,NULL);
471 static void test_sigma_el(FILE *fp,real rho)
475 fprintf(fp,"Testing the elastic cross sections table\n");
476 for(i=0; (i<500); i++) {
477 fprintf(fp,"%3d %8.3f\n",i,cross_el(i,rho,NULL));
481 static void test_sigma_inel(FILE *fp,real rho)
485 fprintf(fp,"Testing the inelastic cross sections table\n");
486 for(i=0; (i<500); i++) {
487 fprintf(fp,"%3d %8.3f\n",i,cross_inel(i,rho,NULL));
491 static void test_band_ener(int *seed,FILE *fp)
495 for(i=0; (i<500); i++) {
496 band_ener(seed,fp,NULL);
500 void init_tables(int nfile,t_filenm fnm[],real rho)
506 (void) band_ener(&seed,NULL,opt2fn("-band",nfile,fnm));
507 (void) cross_el(ekin,rho,opt2fn("-sigel",nfile,fnm));
508 (void) cross_inel(ekin,rho,opt2fn("-sigin",nfile,fnm));
509 (void) get_theta_el(ekin,&seed,NULL,opt2fn("-thetael",nfile,fnm));
510 (void) get_omega(ekin,&seed,NULL,opt2fn("-eloss",nfile,fnm));
511 (void) get_q_inel(ekin,omega,&seed,NULL,opt2fn("-qtrans",nfile,fnm));
514 void test_tables(int *seed,char *fn,real rho)
521 test_q_inel(fp,seed);
522 test_theta_el(fp,seed);
523 test_sigma_el(fp,rho);
524 test_sigma_inel(fp,rho);
525 test_band_ener(seed,fp);