77*
88* %I
99* Written by: Victor Laliena
10- * Date: 27.10.18 (last version, 09.07.19)
11- * Version: $Revision: 0.1 $
10+ * Date: 2018-2019
1211* Origin: University of Zaragoza
1312*
1413* Component for simulating textured powder in Union components
@@ -251,7 +250,7 @@ double plgndr(int l, int m, double x)
251250 printf("Bad arguments in routine plgndr\n");
252251 printf("m: %d l: %d x: %f\n",m,l,x);
253252 printf("Exiting...\n");
254- exit(1);
253+ exit(- 1);
255254 }
256255 //Compute Pmm
257256 pmm=1.0;
@@ -305,20 +304,27 @@ struct legendre * initLegendre(int lmax)
305304 printf("initLegendre: negative lmax = %d\n",lmax);
306305 printf("Exiting...\n");
307306 fflush(stdout);
308- exit(1);
307+ exit(- 1);
309308 }
310309
311310 nlm = (lmax+1)*(lmax+1);
312311
313312 lgdr = (struct legendre *)malloc(sizeof(struct legendre));
314-
313+ if (!lgdr) {
314+ fprintf(stderr,"Texture_process: Failed struct malloc() in initLegendre. Exit!\n");
315+ exit(-1);
316+ }
315317 lgdr->lmax = lmax;
316318 lgdr->index = (int **)malloc((lmax+1)*sizeof(int *));
317319 lgdr->l = (int *)malloc(nlm*sizeof(int));
318320 lgdr->m = (int *)malloc(nlm*sizeof(int));
319321 lgdr->nplm = (int *)malloc(nlm*sizeof(int));
320322 lgdr->x = (double **)malloc(nlm*sizeof(double *));
321323 lgdr->Plm = (double **)malloc(nlm*sizeof(double *));
324+ if (!lgdr->index || !lgdr->l || !lgdr->m || !lgdr->nplm || !lgdr->x || !lgdr->Plm) {
325+ fprintf(stderr,"Texture_process: Failed malloc() in initLegendre. Exit!\n");
326+ exit(-1);
327+ }
322328 i = -1;
323329 for(l=0;l<=lmax;l++) {
324330 lgdr->index[l] = (int *)malloc((2*l+1)*sizeof(int));
@@ -395,10 +401,14 @@ double total_hkl_XS(double k, double kct, double kphi, struct hkl_data_texture *
395401 //printf("Exiting...");
396402 fflush(stdout);
397403 filep = fopen("negativeXS.txt","a");
398- fprintf(filep,"%d %d %d %.6e %.6e %.6e %.6e %.6e %.6e %.6e\n",
399- L->h,L->k,L->l,L->Gx,L->Gy,L->Gz,k,kct,kphi,XS);
400- fclose(filep);
401- //exit(1);
404+ if(filep) {
405+ fprintf(filep,"%d %d %d %.6e %.6e %.6e %.6e %.6e %.6e %.6e\n",
406+ L->h,L->k,L->l,L->Gx,L->Gy,L->Gz,k,kct,kphi,XS);
407+ fclose(filep);
408+ } else {
409+ fprintf(stderr,"Texture_process/routine total_hkl_XS: failed fopen in add mode for file 'negativeXS.txt'. Exit!\n");
410+ exit(-1);
411+ }
402412 }
403413
404414 // this is not the cross section. A different name (reflectivity??) would be appropriate
@@ -438,9 +448,14 @@ double total_hkl_XS_interp(double k, double kct, double kphi, struct hkl_data_te
438448 printf("total_hkl_XS: negative XS: %.6e\n",XS);
439449 fflush(stdout);
440450 filep = fopen("negativeXS.txt","a");
441- fprintf(filep,"%d %d %d %.6e %.6e %.6e %.6e %.6e %.6e %.6e\n",
442- L->h,L->k,L->l,L->Gx,L->Gy,L->Gz,k,kct,kphi,XS);
443- fclose(filep);
451+ if(filep) {
452+ fprintf(filep,"%d %d %d %.6e %.6e %.6e %.6e %.6e %.6e %.6e\n",
453+ L->h,L->k,L->l,L->Gx,L->Gy,L->Gz,k,kct,kphi,XS);
454+ fclose(filep);
455+ } else {
456+ fprintf(stderr,"Texture_process/total_hkl_XS_interp: failed fopen in add mode for file 'negativeXS.txt'. Exit!\n");
457+ exit(-1);
458+ }
444459 }
445460
446461 // this is not the cross section. A different name (reflectivity??)
@@ -564,7 +579,7 @@ int computeV(struct fourier_coef *fc, struct legendre *lgdr, struct hkl_data_tex
564579 printf("computeV: negative lmax = %d\n",lmax);
565580 printf("Exiting...\n");
566581 fflush(stdout);
567- exit(1);
582+ exit(- 1);
568583 }
569584
570585 Gct = L->Gct;
@@ -884,6 +899,10 @@ int sampleKprime_hkl(double *kf, double *ki, struct hkl_data_texture *L, struct
884899 printf("find distribution probability in file sampleKprime_hkl_fail.txt\n");
885900 //sprintf(fn,"probPhiQ_%d%d%d.txt",L->h,L->k,L->l);
886901 filep = fopen("sampleKprime_hkl_fail.txt","w");
902+ if(!filep) {
903+ fprintf(stderr,"Texture_process/sampleKprime_hkl: failed fopen write mode for file 'sampleKprime_hkl_fail.txt'. Exit!\n");
904+ exit(-1);
905+ }
887906 //filep = fopen(fn,"a");
888907 fprintf(filep,"#Plane hkl: %d %d %d\n",L->h,L->k,L->l);
889908 fprintf(filep,"#Incoming neutron:\n");
@@ -906,7 +925,7 @@ int sampleKprime_hkl(double *kf, double *ki, struct hkl_data_texture *L, struct
906925 fclose(filep);
907926 printf("Exiting\n");
908927 fflush(stdout);
909- exit(1);
928+ exit(- 1);
910929}
911930
912931double probPhiq(double ctq, double phiq, struct hkl_data_texture *L, struct legendre *lgdr)
@@ -1299,6 +1318,10 @@ int read_hkl_data_texture(char *SC_file, struct hkl_info_struct_texture *info)
12991318
13001319 // print crystal info
13011320 filep = fopen("crystal.txt","w");
1321+ if(!filep) {
1322+ fprintf(stderr,"Texture_process: failed fopen write mode for file 'crystal.txt'. Exit!\n");
1323+ exit(-1);
1324+ }
13021325 fprintf(filep,"Direct lattice:\n");
13031326 fprintf(filep,"a: %f %f %f\n",info->m_ax,info->m_ay,info->m_az);
13041327 fprintf(filep,"b: %f %f %f\n",info->m_bx,info->m_by,info->m_bz);
@@ -1389,7 +1412,7 @@ void readFourierCoef(char *filename, struct fourier_coef *c)
13891412 printf("%s\n",filename);
13901413 printf("not found. Exiting...\n");
13911414 fflush(stdout);
1392- exit(1);
1415+ exit(- 1);
13931416 }
13941417
13951418 str[0]=0;
@@ -1420,7 +1443,7 @@ void readFourierCoef(char *filename, struct fourier_coef *c)
14201443 printf("with no spaces or other characters) is exactly");
14211444 printf(" present just above the data\n");
14221445 printf("Exiting...\n");
1423- exit(1);
1446+ exit(- 1);
14241447 }
14251448
14261449 np = 0;
@@ -1433,7 +1456,7 @@ void readFourierCoef(char *filename, struct fourier_coef *c)
14331456 printf("different than expected\n");
14341457 printf("lmax: %d np: %d nlines: %d\n",lmax,np,nlines);
14351458 printf("Exiting...\n");
1436- exit(1);
1459+ exit(- 1);
14371460 }
14381461
14391462 printf("readFourierCoef: lmax: %d np: %d nlines: %d\n",lmax,np,nlines);
@@ -1490,6 +1513,13 @@ void readFourierCoef(char *filename, struct fourier_coef *c)
14901513
14911514 // check reality of ODF
14921515 filep = fopen("coef_original.txt","w");
1516+ if(!filep) {
1517+ printf("readFourierCoef: fourier coefficient file\n");
1518+ printf("%s\n",filename);
1519+ printf("not found. Exiting...\n");
1520+ fflush(stdout);
1521+ exit(-1);
1522+ }
14931523 rmax = 0;
14941524 for(l=0;l<=lmax;l++) {
14951525 for(m=-l;m<=l;m++) {
@@ -1552,6 +1582,10 @@ void readFourierCoef(char *filename, struct fourier_coef *c)
15521582 // print new coefficients
15531583 rmax = 0;
15541584 filep = fopen("coef_modified.txt","w");
1585+ if(!filep) {
1586+ fprintf(stderr,"Texture_process: failed fopen write mode for file 'coef_modified.txt'. Exit!\n");
1587+ exit(-1);
1588+ }
15551589 for(l=0;l<=lmax;l++) {
15561590 for(m=-l;m<=l;m++) {
15571591 for(n=-l;n<=l;n++) {
@@ -1636,7 +1670,7 @@ double interp(double x, int n, double *xp, double *yp)
16361670 printf("x[n-1] = %.16e\n",xp[n-1]);
16371671 printf("Exiting...\n");
16381672 fflush(stdout);
1639- exit(1);
1673+ exit(- 1);
16401674 }
16411675 i1 = 0;
16421676 i2 = n-1;
@@ -1652,7 +1686,7 @@ double interp(double x, int n, double *xp, double *yp)
16521686 printf("x[1] = %.16e\n",xp[1]);
16531687 printf("Exiting...\n");
16541688 fflush(stdout);
1655- exit(1);
1689+ exit(- 1);
16561690 }
16571691 return yp[i1]+((x-xp[i1])/(xp[i2]-xp[i1]))*(yp[i2]-yp[i1]);
16581692}
@@ -1671,7 +1705,7 @@ double interp2D(double x, double y, int nx, int ny, double *xp, double *yp, doub
16711705 } else {
16721706 printf("interp2D: bad i: %d, nx: %d x: %.16e y: %.16e\n",i,nx,x,y);
16731707 fflush(stdout);
1674- exit(1);
1708+ exit(- 1);
16751709 }
16761710 } else if(i>0 && x<xp[i]) {
16771711 i = i-1;
@@ -1683,7 +1717,7 @@ double interp2D(double x, double y, int nx, int ny, double *xp, double *yp, doub
16831717 } else {
16841718 printf("interp2D: bad j: %d, ny: %d x: %.16e y: %.16e\n",j,ny,x,y);
16851719 fflush(stdout);
1686- exit(1);
1720+ exit(- 1);
16871721 }
16881722 } else if(j>0 && y<yp[j]) {
16891723 j = j-1;
@@ -1701,7 +1735,7 @@ double interp2D(double x, double y, int nx, int ny, double *xp, double *yp, doub
17011735 printf("interp2D: bad t: %.6e, u: %.6e x: %f y: %f i: %d nx: %d\n",t,u,x,y,i,nx);
17021736 printf("%f %f %f\n",x,xp[i],xp[i+1]);
17031737 fflush(stdout);
1704- //exit(1);
1738+ //exit(- 1);
17051739 }
17061740 if(u<0 || u>1) {
17071741 printf("interp2D: bad u: %.6e, t: %.6e x: %f y: %f j: %d ny: %d\n",u,t,x,y,j,ny);
@@ -1711,7 +1745,7 @@ double interp2D(double x, double y, int nx, int ny, double *xp, double *yp, doub
17111745 printf("\n");
17121746 for(j=0;j<ny;j++) { printf("%f\n",yp[j]); }
17131747 fflush(stdout);
1714- exit(1);
1748+ exit(- 1);
17151749 }
17161750
17171751 return (1-t)*(1-u)*f1 + t*(1-u)*f2 + t*u*f3 + (1-t)*u*f4;
@@ -1808,12 +1842,16 @@ int initData(char *coef_fn, char *crystal_fn, struct hkl_info_struct_texture *in
18081842 printf("Exiting...\n");
18091843 fclose(filep);
18101844 fflush(stdout);
1811- exit(1);
1845+ exit(- 1);
18121846 }
18131847 Li->lv = (int *)malloc((Li->numV)*sizeof(int));
18141848 Li->mv = (int *)malloc((Li->numV)*sizeof(int));
18151849 Li->RV = (double *)malloc((Li->numV)*sizeof(double));
18161850 Li->phiV = (double *)malloc((Li->numV)*sizeof(double));
1851+ if (!Li->lv || !Li->mv || !Li->RV || !Li->phiV) {
1852+ fprintf(stderr,"Texture_process / initData: Failed malloc(s). Exit!\n");
1853+ exit(-1);
1854+ }
18171855 for(j=0;j<Li->numV;j++) {
18181856 fscanf(filep,"%d %d %d %d %d %d %lf %lf",
18191857 &lm,&hm,&km,&jj,(Li->lv)+j,(Li->mv)+j,(Li->RV)+j,(Li->phiV)+j);
@@ -1826,7 +1864,7 @@ int initData(char *coef_fn, char *crystal_fn, struct hkl_info_struct_texture *in
18261864 printf("Exiting...\n");
18271865 fclose(filep);
18281866 fflush(stdout);
1829- exit(1);
1867+ exit(- 1);
18301868 }
18311869 }
18321870 }
@@ -2146,7 +2184,7 @@ int Texture_physics_scattering(double *k_final, double *k_initial, double *weigh
21462184 printf("%d %.6e %.6e\n",i,cumProb_hkl[i],r);
21472185 }
21482186 fflush(stdout);
2149- exit(1);
2187+ exit(- 1);
21502188 }
21512189
21522190 // sampling kprime
@@ -2225,7 +2263,7 @@ INITIALIZE
22252263 if ( initData(fcoef_fn, crystal_fn, &hkl_info_texture, maxNeutronSaved) ) {
22262264 printf("Texture_process: %s: Error in initData: Aborting.\n", NAME_CURRENT_COMP);
22272265 fflush(stdout);
2228- exit(1);
2266+ exit(- 1);
22292267 }
22302268
22312269 // precomputed factors
@@ -2316,11 +2354,15 @@ struct hkl_info_struct_texture *hkl_info
23162354printf("numReused: %.6e\n",hkl_info->numReused);
23172355// print rejection statistics
23182356FILE *filep = fopen("rejectioStatistics.txt","w");
2319- struct hkl_data_texture *L = hkl_info->list;
2320- for(i=0;i<hkl_info->num_hkl;i++) {
2321- fprintf(filep,"%d %d %d %.6e %.6e\n",L[i].h,L[i].k,L[i].l,L[i].numReject,L[i].numScatt);
2322- }
2323- fclose(filep);
2357+ if (!filep) {
2358+ struct hkl_data_texture *L = hkl_info->list;
2359+ for(i=0;i<hkl_info->num_hkl;i++) {
2360+ fprintf(filep,"%d %d %d %.6e %.6e\n",L[i].h,L[i].k,L[i].l,L[i].numReject,L[i].numScatt);
2361+ }
2362+ fclose(filep);
2363+ } else {
2364+ fprintf("Texture_process %s WARNING: Failed to open file 'rejectioStatistics.txt' in write mode. No data saved!\n", NAME_CURRENT_COMP);
2365+ }
23242366// deallocate allocated memory, etc.
23252367free_texture(hkl_info);
23262368%}
0 commit comments