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
46 #include "gmx_fatal.h"
54 int nener,nomega,nq; /* Number of entries in the table */
55 real *ener; /* Energy values */
56 real **omega; /* Omega values (energy dependent) */
57 real ***prob,***q; /* Probability and Q */
61 int nener,n2Ddata; /* Number of entries in the table */
62 real *ener; /* Energy values */
63 real **prob,**data; /* Probability and data (energy loss) */
66 static int realcomp(const void *a,const void *b)
80 static t_p2Ddata *read_p2Ddata(char *fn)
87 fprintf(stdout,"Going to read %s\n",fn);
90 /* Allocate memory and set constants */
92 if (fscanf(fp,"%d%d",&p2Ddata->nener,&p2Ddata->n2Ddata) != 2)
93 gmx_fatal(FARGS,"I need two integers: nener, n in file %s",fn);
95 snew(p2Ddata->ener,p2Ddata->nener);
96 snew(p2Ddata->prob,p2Ddata->nener);
97 snew(p2Ddata->data,p2Ddata->nener);
99 /* Double loop to read data */
100 for(i=0; (i<p2Ddata->nener); i++) {
101 fprintf(stderr,"\rEnergy %d/%d",i+1,p2Ddata->nener);
102 snew(p2Ddata->prob[i],p2Ddata->n2Ddata);
103 snew(p2Ddata->data[i],p2Ddata->n2Ddata);
105 for(j=0; (j<p2Ddata->n2Ddata); j++) {
106 fscanf(fp,"%lf%lf%lf",&e,&p,&o);
108 /* Consistency check */
110 p2Ddata->ener[i] = e;
111 else if (fabs(p2Ddata->ener[i]-e) > 1e-6*e)
112 gmx_fatal(FARGS,"Inconsistent energy %f i=%d j=%d",e,i,j);
113 p2Ddata->prob[i][j] = p;
114 p2Ddata->data[i][j] = o;
116 /* There is some noise on the data, take it away by sorting,
117 * because otherwise binary search does not work.
118 * This is equivalent to shifting in the data slightly along the X-axis
119 * but better than linear search with the "real" data.
121 qsort(p2Ddata->data[i],p2Ddata->n2Ddata,sizeof(p2Ddata->data[0][0]),
124 fprintf(stderr,"\n");
131 static t_pq_inel *read_pq(char *fn)
138 fprintf(stdout,"Going to read %s\n",fn);
141 /* Allocate memory and set constants */
143 if (fscanf(fp,"%d%d%d",&pq->nener,&pq->nomega,&pq->nq) != 3)
144 gmx_fatal(FARGS,"I need three integers: nener, nomega, nq in file %s",fn);
146 snew(pq->ener,pq->nener);
147 snew(pq->omega,pq->nener);
148 snew(pq->prob,pq->nener);
149 snew(pq->q,pq->nener);
151 /* Triple loop to read data */
152 for(i=0; (i<pq->nener); i++) {
153 fprintf(stderr,"\rEnergy %d/%d",i+1,pq->nener);
154 snew(pq->prob[i],pq->nomega);
155 snew(pq->q[i],pq->nomega);
156 snew(pq->omega[i],pq->nomega);
158 for(j=0; (j<pq->nomega); j++) {
159 snew(pq->prob[i][j],pq->nq);
160 snew(pq->q[i][j],pq->nq);
162 for(k=0; (k<pq->nq); k++) {
163 fscanf(fp,"%lf%lf%lf%lf",&e,&o,&p,&t);
165 /* Consistency check */
166 if ((j == 0) && (k == 0))
168 else if (fabs(pq->ener[i]-e) > 1e-6*e)
169 gmx_fatal(FARGS,"Inconsistent energy %f i=%d j=%d k=%d",e,i,j,k);
173 else if (fabs(pq->omega[i][j]-o) > 1e-6*o)
174 gmx_fatal(FARGS,"Inconsistent omega %f i=%d j=%d k=%d",o,i,j,k);
176 pq->prob[i][j][k] = p;
181 fprintf(stderr,"\n");
188 static int my_bsearch(real val,int ndata,real data[],gmx_bool bLower)
196 while ((ihi - ilo) > 1) {
198 if (data[imed] > val)
203 /* Now val should be in between data[ilo] and data[ihi] */
204 /* Decide which one is closest */
205 if (bLower || ((val-data[ilo]) <= (data[ihi]-val)))
211 static real interpolate2D(int nx,int ny,real *dx,real **data,
212 real x0,real fy,int nx0,int ny0)
216 fx = (x0-dx[nx0])/(dx[nx0+1]-dx[nx0]);
218 return (fx*fy*data[nx0][ny0] + fx*(1-fy)*data[nx0][ny0+1] +
219 (1-fx)*fy*data[nx0+1][ny0] + (1-fx)*(1-fy)*data[nx0+1][ny0+1]);
222 real get_omega(real ekin,int *seed,FILE *fp,char *fn)
224 static t_p2Ddata *p2Ddata = NULL;
229 p2Ddata = read_p2Ddata(fn);
231 /* Get energy index by binary search */
232 if ((eindex = my_bsearch(ekin,p2Ddata->nener,p2Ddata->ener,TRUE)) >= 0) {
234 if (eindex >= p2Ddata->nener)
235 gmx_fatal(FARGS,"eindex (%d) out of range (max %d) in get_omega",
236 eindex,p2Ddata->nener);
239 /* Start with random number */
242 /* Do binary search in the energy table */
243 if ((oindex = my_bsearch(r,p2Ddata->n2Ddata,p2Ddata->prob[eindex],TRUE)) >= 0) {
245 if (oindex >= p2Ddata->n2Ddata)
246 gmx_fatal(FARGS,"oindex (%d) out of range (max %d) in get_omega",
247 oindex,p2Ddata->n2Ddata);
250 fy = ((r-p2Ddata->prob[eindex][oindex])/
251 (p2Ddata->prob[eindex][oindex+1]-p2Ddata->prob[eindex][oindex]));
252 ome = interpolate2D(p2Ddata->nener,p2Ddata->n2Ddata,p2Ddata->ener,
253 p2Ddata->data,ekin,fy,
255 /* ome = p2Ddata->data[eindex][oindex];*/
258 fprintf(fp,"%8.3f %8.3f\n",ome,r);
266 real get_theta_el(real ekin,int *seed,FILE *fp,char *fn)
268 static t_p2Ddata *p2Ddata = NULL;
273 p2Ddata = read_p2Ddata(fn);
275 /* Get energy index by binary search */
276 if ((eindex = my_bsearch(ekin,p2Ddata->nener,p2Ddata->ener,TRUE)) >= 0) {
278 /* Start with random number */
281 /* Do binary search in the energy table */
282 if ((tindex = my_bsearch(r,p2Ddata->n2Ddata,p2Ddata->prob[eindex],FALSE)) >= 0) {
284 theta = p2Ddata->data[eindex][tindex];
287 fprintf(fp,"%8.3f %8.3f\n",theta,r);
295 real get_q_inel(real ekin,real omega,int *seed,FILE *fp,char *fn)
297 static t_pq_inel *pq = NULL;
298 int eindex,oindex,tindex;
304 /* Get energy index by binary search */
305 if ((eindex = my_bsearch(ekin,pq->nener,pq->ener,TRUE)) >= 0) {
307 if (eindex >= pq->nener)
308 gmx_fatal(FARGS,"eindex out of range (%d >= %d)",eindex,pq->nener);
311 /* Do binary search in the energy table */
312 if ((oindex = my_bsearch(omega,pq->nomega,pq->omega[eindex],FALSE)) >= 0) {
314 if (oindex >= pq->nomega)
315 gmx_fatal(FARGS,"oindex out of range (%d >= %d)",oindex,pq->nomega);
318 /* Start with random number */
321 if ((tindex = my_bsearch(r,pq->nq,pq->prob[eindex][oindex],FALSE)) >= 0) {
323 if (tindex >= pq->nq)
324 gmx_fatal(FARGS,"tindex out of range (%d >= %d)",tindex,pq->nq);
327 theta = pq->q[eindex][oindex][tindex];
330 fprintf(fp,"get_q_inel: %8.3f %8.3f\n",theta,r);
339 static int read_cross(char *fn,real **ener,real **cross,real factor)
345 fprintf(stdout,"Going to read %s\n",fn);
346 n = get_file(fn,&data);
348 /* Allocate memory */
351 for(i=j=0; (i<n); i++) {
352 if (sscanf(data[i],"%lf%lf",&e,&c) == 2) {
354 (*cross)[j] = c*factor;
361 fprintf(stderr,"There were %d empty lines in file %s\n",n-j,fn);
366 real cross_inel(real ekin,real rho,char *fn)
368 static real *ener = NULL;
369 static real *cross = NULL;
373 /* Read data at first call, convert A^2 to nm^2 */
375 ninel = read_cross(fn,&ener,&cross,rho*0.01);
377 /* Compute index with binary search */
378 if ((eindex = my_bsearch(ekin,ninel,ener,FALSE)) >= 0) {
381 gmx_fatal(FARGS,"ekin = %f, ener[0] = %f, ener[%d] = %f",ekin,
382 ener[0],ener[ninel-1]);
384 return cross[eindex];
390 real cross_el(real ekin,real rho,char *fn)
392 static real *ener = NULL;
393 static real *cross = NULL;
397 /* Read data at first call, convert A^2 to nm^2 */
399 nel = read_cross(fn,&ener,&cross,rho*0.01);
401 /* Compute index with binary search */
402 if ((eindex = my_bsearch(ekin,nel,ener,FALSE)) >= 0) {
405 gmx_fatal(FARGS,"ekin = %f, ener[0] = %f, ener[%d] = %f",ekin,
406 ener[0],ener[nel-1]);
409 return cross[eindex];
414 real band_ener(int *seed,FILE *fp,char *fn)
416 static real *ener = NULL;
417 static real *prob = NULL;
422 /* Read data at first call, misuse read_cross function */
424 nener = read_cross(fn,&ener,&prob,1.0);
428 if ((eindex = my_bsearch(r,nener,prob,FALSE)) >= 0) {
431 gmx_fatal(FARGS,"r = %f, prob[0] = %f, prob[%d] = %f",r,
432 prob[0],prob[nener-1]);
436 fprintf(fp,"%8.3f %8.3f\n",ener[eindex],r);
443 static void test_omega(FILE *fp,int *seed)
447 fprintf(fp,"Testing the energy loss tables\n");
448 for(i=0; (i<1000); i++) {
449 (void) get_omega(500*rando(seed),seed,fp,NULL);
453 static void test_q_inel(FILE *fp,int *seed)
457 fprintf(fp,"Testing the energy/omega dependent inelastic scattering q tables\n");
458 for(i=0; (i<1000); i++) {
459 (void) get_q_inel(500*rando(seed),400*rando(seed),seed,fp,NULL);
463 static void test_theta_el(FILE *fp,int *seed)
467 fprintf(fp,"Testing the energy dependent elastic scattering theta tables\n");
468 for(i=0; (i<1000); i++) {
469 (void) get_theta_el(500*rando(seed),seed,fp,NULL);
473 static void test_sigma_el(FILE *fp,real rho)
477 fprintf(fp,"Testing the elastic cross sections table\n");
478 for(i=0; (i<500); i++) {
479 fprintf(fp,"%3d %8.3f\n",i,cross_el(i,rho,NULL));
483 static void test_sigma_inel(FILE *fp,real rho)
487 fprintf(fp,"Testing the inelastic cross sections table\n");
488 for(i=0; (i<500); i++) {
489 fprintf(fp,"%3d %8.3f\n",i,cross_inel(i,rho,NULL));
493 static void test_band_ener(int *seed,FILE *fp)
497 for(i=0; (i<500); i++) {
498 band_ener(seed,fp,NULL);
502 void init_tables(int nfile,t_filenm fnm[],real rho)
508 (void) band_ener(&seed,NULL,opt2fn("-band",nfile,fnm));
509 (void) cross_el(ekin,rho,opt2fn("-sigel",nfile,fnm));
510 (void) cross_inel(ekin,rho,opt2fn("-sigin",nfile,fnm));
511 (void) get_theta_el(ekin,&seed,NULL,opt2fn("-thetael",nfile,fnm));
512 (void) get_omega(ekin,&seed,NULL,opt2fn("-eloss",nfile,fnm));
513 (void) get_q_inel(ekin,omega,&seed,NULL,opt2fn("-qtrans",nfile,fnm));
516 void test_tables(int *seed,char *fn,real rho)
523 test_q_inel(fp,seed);
524 test_theta_el(fp,seed);
525 test_sigma_el(fp,rho);
526 test_sigma_inel(fp,rho);
527 test_band_ener(seed,fp);