00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifdef HAVE_CONFIG_H
00032 #include <config.h>
00033 #endif
00034
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <math.h>
00038 #include <string.h>
00039
00040 #include "libcpixe.h"
00041 #include "compilopt.h"
00042
00043
00044 int main(int argc, char *argv[]);
00045 int cpixemain(int NCALL);
00046
00047 STATFILENAME inputfilename="input.in";
00048
00049
00050
00051
00052
00053
00054
00055 int main(int argc, char *argv[])
00056 {
00057 extern STATFILENAME inputfilename;
00058
00059 if(argc>1)strcpy(inputfilename,argv[1]);
00060 cpixemain(0);
00061 cpixemain(1);
00062 cpixemain(-1);
00063
00064 return EXIT_SUCCESS;
00065 }
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 int cpixemain(int NCALL)
00079 {
00080 STATFILENAME dbpath="./";
00081 extern STATFILENAME inputfilename;
00082 STATFILENAME samplefilenm="";
00083 STATFILENAME filterfilenm="";
00084 STATFILENAME calibfilenm="";
00085 STATFILENAME areasfilenm="";
00086 STATFILENAME fckfilename="";
00087 STATFILENAME abscoeffilename="";
00088 STATFILENAME outputfilenm="";
00089
00090
00091 LYR *lyr;
00092 CalibYld *XYldSums;
00093 int i,j,l;
00094 EXP_PARAM *pExpPar;
00095 SIM_PARAM *pSimPar;
00096
00097
00098 static int *PresentElems;
00099 static CPIXERESULTS *CalcFlags;
00100 static XrayYield *XYldArray;
00101 static FluorCKCoef *FCKCoefArray;
00102 static AbsCoef *TotAbsCoefArray;
00103 static SPT *SPTArray;
00104 static SFCListElem *SFCList;
00105 static int nSFCList;
00106 static int lastNcall=-1;
00107 static FILTER Filter;
00108
00109 static EXP_PARAM ExpPar;
00110 static foil *Sample;
00111 static int NFoil, NFoilUsed, dimSums;
00112 static FILE *outputfile;
00113 static EXTRAINFO ExtraInfo;
00114
00115 #if CPIXEVERBOSITY > 0
00116 printf("\n\n\n************ BEGIN OF CPIXE OUTPUT ****************\n\n");
00117 #endif
00118
00119 pExpPar=&ExpPar;
00120 pSimPar=&pExpPar->simpar;
00121
00122 if(NCALL>0){
00123 if(lastNcall<0){fprintf(stderr,"\n ERROR: CPIXEMAIN: CPIXEMAIN was not initialized.\n");exit(1);}
00124
00125
00126
00127
00128
00129
00130
00131
00132 if(Filter.changes){
00133 FilterTrans(pExpPar, XYldArray, TotAbsCoefArray, PresentElems, &Filter);
00134
00135 Filter.changes=0;
00136 }
00137
00138 initlyrarray(pExpPar, Sample, XYldArray, TotAbsCoefArray, SPTArray, PresentElems, NFoil, &NFoilUsed, &lyr);
00139 dimSums=pSimPar->MaxZinsample+1;
00140 XYldSums=(CalibYld*)calloc(dimSums,sizeof(CalibYld));
00141 integrate_Simpson(pExpPar, TotAbsCoefArray, FCKCoefArray, XYldArray ,NFoilUsed, &Filter, lyr, XYldSums);
00142
00143
00144
00145
00146 #if CPIXEVERBOSITY > 0
00147
00148
00149 printf("\n\n Ebeam=%lf IncAng=%lf DetAng=%lf \n",
00150 pExpPar->BeamEner, pExpPar->IncAng/DEG2RAD, pExpPar->DetAng/DEG2RAD);
00151 printf("\n Charge=%lf DTCC=%lf DetColFac=%lf\n",
00152 pExpPar->simpar.ColCharge, pExpPar->simpar.DTCC, pExpPar->DetColFac);
00153
00154 printf("\n\nIteration %d\n",NCALL);
00155
00156 for(i=0;i<NFoil;i++){
00157 printf("\nSample[%d] (%d elem)\n",i,Sample[i].nfoilelm);
00158 printf("\n---Eff. thickness=%.3le (%d sublyrs)\n",lyr[i+1].ThickIn,lyr[i+1].FESxlen);
00159
00160 for(j=0;j<Sample[i].comp.nelem;j++){
00161 printf("'%s'(Z=%d) X[%d]=%lf (%lfat%% / %lfM%%)\n",
00162 XYldArray[Sample[i].comp.elem[j].Z].symb,Sample[i].comp.elem[j].Z,j,Sample[i].comp.X[j],100*Sample[i].comp.xn[j],100*Sample[i].comp.w[j]);
00163 }
00164 printf("\nFinal Energy in this layer=%lf keV\n\n",lyr[i+1].FoilOutEner);
00165 }
00166
00167 for(i=0;i<Filter.nlyr;i++){
00168 printf("\nFilter[%d] (%lf 1e15at/cm2) (%d elem) \n",i,Filter.foil[i].thick,Filter.foil[i].nfoilelm);
00169 for(j=0;j<Filter.foil[i].comp.nelem;j++){
00170 printf("(Z=%d) X[%d]=%lf (%lfat%% / %lfM%%)\n",
00171 Filter.foil[i].comp.elem[j].Z,j,Filter.foil[i].comp.X[j],100*Filter.foil[i].comp.xn[j],100*Filter.foil[i].comp.w[j]);
00172 }
00173 }
00174
00175 for(i=0;i<dimSums;i++){
00176 if(i<=pSimPar->MaxZinsample && PresentElems[i]){
00177 printf("\n\n ************RESULTS for %s (Z=%d)\n",XYldArray[i].symb,i);
00178
00179 for (j = 1; j <= 3; j++)
00180 if(XYldSums[i].K_[j] > 0.) {
00181 printf("\n%10s (%2.2lfkeV)\t%le counts (cal=%.3le Flt=%.2le)", LineNm[1][j], XYldArray[i].ener.K_[j], XYldSums[i].K_[j],XYldArray[i].XYld.K_[j],Filter.Trans[i].K_[j]);
00182 printf("\n%10s \texp:\t%le counts (err=%.1le) ", "", CalcFlags[i].simareas.K_[j],CalcFlags[i].err.K_[j]);
00183 }
00184
00185 for (j = 1; j <= 3; j++)
00186 for (l = 1; l <= 3; l++)
00187 if(XYldSums[i].L_[j][l] > 0.){
00188 printf("\n%10s (%2.2lfkeV)\t%le counts (cal=%.3le Flt=%.2le)", LineNm[j+1][l], XYldArray[i].ener.L_[j][l], XYldSums[i].L_[j][l],XYldArray[i].XYld.L_[j][l],Filter.Trans[i].L_[j][l]);
00189 printf("\n%10s \texp:\t%le counts (err=%.1le) ", "", CalcFlags[i].simareas.L_[j][l],CalcFlags[i].err.L_[j][l]);
00190 }
00191
00192 for (j = 1; j <= 3; j++)
00193 if(XYldSums[i].M_[j] > 0.) {
00194 printf("\n%10s (%2.2lfkeV)\t%le counts (cal=%.3le Flt=%.2le)",LineNm[5][j], XYldArray[i].ener.M_[j], XYldSums[i].M_[j],XYldArray[i].XYld.M_[j],Filter.Trans[i].M_[j]);
00195 printf("\n%10s \texp:\t%le counts (err=%.1le) ", "", CalcFlags[i].simareas.M_[j],CalcFlags[i].err.M_[j]);
00196 }
00197 }
00198 }
00199
00200
00201 #endif
00202
00203
00204
00205 if(ExtraInfo.WantOutputfile){
00206 fprintf(outputfile,"\n\n Ebeam=%lf IncAng=%lf DetAng=%lf \n",
00207 pExpPar->BeamEner, pExpPar->IncAng/DEG2RAD, pExpPar->DetAng/DEG2RAD);
00208 fprintf(outputfile,"\n Charge=%lf DTCC=%lf DetColFac=%lf\n",
00209 pExpPar->simpar.ColCharge, pExpPar->simpar.DTCC, pExpPar->DetColFac);
00210
00211 fprintf(outputfile,"\n\nIteration %d\n",NCALL);
00212
00213 for(i=0;i<NFoil;i++){
00214 fprintf(outputfile,"\nSample[%d] (%d elem)\n",i,Sample[i].nfoilelm);
00215 fprintf(outputfile,"\n---Eff. thickness=%.3le (%d sublyrs)\n",lyr[i+1].ThickIn,lyr[i+1].FESxlen);
00216
00217 for(j=0;j<Sample[i].comp.nelem;j++){
00218 fprintf(outputfile,"'%s'(Z=%d) X[%d]=%lf (%lfat%% / %lfM%%)\n",
00219 XYldArray[Sample[i].comp.elem[j].Z].symb,Sample[i].comp.elem[j].Z,j,Sample[i].comp.X[j],100*Sample[i].comp.xn[j],100*Sample[i].comp.w[j]);
00220 }
00221 fprintf(outputfile,"\nFinal Energy in this layer=%lf keV\n\n",lyr[i+1].FoilOutEner);
00222 }
00223
00224 for(i=0;i<Filter.nlyr;i++){
00225 fprintf(outputfile,"\nFilter[%d] (%lf 1e15at/cm2) (%d elem) \n",i,Filter.foil[i].thick,Filter.foil[i].nfoilelm);
00226 for(j=0;j<Filter.foil[i].comp.nelem;j++){
00227 fprintf(outputfile,"(Z=%d) X[%d]=%lf (%lfat%% / %lfM%%)\n",
00228 Filter.foil[i].comp.elem[j].Z,j,Filter.foil[i].comp.X[j],100*Filter.foil[i].comp.xn[j],100*Filter.foil[i].comp.w[j]);
00229 }
00230 }
00231 for(i=0;i<dimSums;i++){
00232 if(i<=pSimPar->MaxZinsample && PresentElems[i]){
00233 fprintf(outputfile,"\n\n ************RESULTS for %s (Z=%d)\n",XYldArray[i].symb,i);
00234
00235 for (j = 1; j <= 3; j++)
00236 if(XYldSums[i].K_[j] > 0.) {
00237 fprintf(outputfile,"\n%10s (%2.2lfkeV)\t%le counts (cal=%.3le Flt=%.2le)", LineNm[1][j], XYldArray[i].ener.K_[j], XYldSums[i].K_[j],XYldArray[i].XYld.K_[j],Filter.Trans[i].K_[j]);
00238 fprintf(outputfile,"\n%10s \texp:\t%le counts (err=%.1le) ", "", CalcFlags[i].simareas.K_[j],CalcFlags[i].err.K_[j]);
00239 }
00240
00241 for (j = 1; j <= 3; j++)
00242 for (l = 1; l <= 3; l++)
00243 if(XYldSums[i].L_[j][l] > 0.){
00244 fprintf(outputfile,"\n%10s (%2.2lfkeV)\t%le counts (cal=%.3le Flt=%.2le)", LineNm[j+1][l], XYldArray[i].ener.L_[j][l], XYldSums[i].L_[j][l],XYldArray[i].XYld.L_[j][l],Filter.Trans[i].L_[j][l]);
00245 fprintf(outputfile,"\n%10s \texp:\t%le counts (err=%.1le) ", "", CalcFlags[i].simareas.L_[j][l],CalcFlags[i].err.L_[j][l]);
00246 }
00247
00248 for (j = 1; j <= 3; j++)
00249 if(XYldSums[i].M_[j] > 0.) {
00250 fprintf(outputfile,"\n%10s (%2.2lfkeV)\t%le counts (cal=%.3le Flt=%.2le)",LineNm[5][j], XYldArray[i].ener.M_[j], XYldSums[i].M_[j],XYldArray[i].XYld.M_[j],Filter.Trans[i].M_[j]);
00251 fprintf(outputfile,"\n%10s \texp:\t%le counts (err=%.1le) ", "", CalcFlags[i].simareas.M_[j],CalcFlags[i].err.M_[j]);
00252 }
00253 }
00254 }
00255 }
00256
00257
00258
00259 freeReusable(NFoil,&lyr,&Sample,&XYldSums);
00260
00261 if(Filter.changes)freeFilter(&Filter);
00262
00263 }
00264
00265
00266 else if(NCALL==0){
00267
00268
00269
00270
00271
00272
00273
00274
00275 ExtraInfo.SampleFileNm=samplefilenm;
00276 ExtraInfo.FilterFileNm=filterfilenm;
00277 ExtraInfo.XYldFileNm=calibfilenm;
00278 ExtraInfo.AreasFileNm=areasfilenm;
00279 ExtraInfo.DBpath=dbpath;
00280 ExtraInfo.OutputFileNm=outputfilenm;
00281
00282 readINPUT(inputfilename, pExpPar, &ExtraInfo);
00283 readsample(samplefilenm, &pSimPar->MaxZinsample, &NFoil, &Sample);
00284 readFilter(filterfilenm, &Filter);
00285
00286 Filter.changes=1;
00287
00288 snprintf(fckfilename, FILENMLENGTH,"%sSC_PRA74.fck",dbpath);
00289 snprintf(abscoeffilename, FILENMLENGTH,"%sxabs.abs",dbpath);
00290
00291 #if CPIXEVERBOSITY > 0
00292 printf("\nInitializing. Reading databases...\n\n");
00293 printf("\nStopping Coefs DB: '%sSCOEF.95[A/B]'",dbpath);
00294 printf("\nAbsorption Coefs DB: '%s'",abscoeffilename);
00295 printf("\nFluorescence Coefs DB: '%s'",fckfilename);
00296 printf("\nDetector Calibration DB: '%s'",calibfilenm);
00297 printf("\nLines to calculate defined in: '%s'\n",areasfilenm);
00298 #endif
00299
00300 createPresentElems(pSimPar->MaxZinsample, NFoil, Sample, &PresentElems);
00301 readCalcFlags(areasfilenm, PresentElems, pSimPar->MaxZinsample, ExtraInfo.AreasFormat, &CalcFlags);
00302
00303 #if CPIXEVERBOSITY > 0
00304 for(i=1;i<pSimPar->MaxZinsample+1;i++){
00305 if(PresentElems[i]){
00306 printf("\nZ=%d (%d)\n ",i,CalcFlags[i].atnum);
00307 for (j = 1; j <= 3; j++) printf("\t%.0lf",CalcFlags[i].simareas.K_[j]);
00308 for (j = 1; j <= 3; j++) {
00309 printf("\n");
00310 for (l = 1; l <= 3; l++)printf("\t%.0lf",CalcFlags[i].simareas.L_[j][l]);
00311 }
00312 printf("\n");
00313 for (j = 1; j <= 3; j++) printf("\t%.0lf",CalcFlags[i].simareas.M_[j]);
00314 }
00315 }
00316 #endif
00317
00318
00319 readXYld(calibfilenm, PresentElems, CalcFlags, pSimPar->MaxZinsample, &pSimPar->CalEner, &XYldArray);
00320 readFCK(fckfilename, pSimPar->MaxZinsample,&FCKCoefArray);
00321 readAbsCoef(abscoeffilename, pSimPar->MaxZinsample, PresentElems, &Filter, &TotAbsCoefArray);
00322 createSPTs(dbpath,pExpPar, pSimPar->MaxZinsample, PresentElems, (double)(2*pExpPar->ion.A), &SPTArray);
00323
00324 if(pSimPar->AllowSXFCorr){
00325 createSFCList(pExpPar, PresentElems, XYldArray, TotAbsCoefArray, &nSFCList, &SFCList);
00326
00327 };
00328
00329
00330
00331
00332
00333
00334 if(ExtraInfo.WantOutputfile){
00335 outputfile = fopen(outputfilenm, "w");
00336 if(outputfile== NULL)
00337 {fprintf(stderr,"\nERROR: Could not open file '%s' for write.\n",outputfilenm);exit(1);}
00338 fprintf(outputfile,"\nCPIXE Output File\n\n");
00339 fprintf(outputfile,"\nStopping Coefs DB: '%sSCOEF.95[A/B]'",dbpath);
00340 fprintf(outputfile,"\nAbsorption Coefs DB: '%s'",abscoeffilename);
00341 fprintf(outputfile,"\nFluorescence Coefs DB: '%s'",fckfilename);
00342 fprintf(outputfile,"\nDetector Calibration DB: '%s'",calibfilenm);
00343 fprintf(outputfile,"\nLines to calculate defined in: '%s'\n",areasfilenm);
00344
00345 }
00346 }
00347
00348 else if(NCALL<0){
00349
00350
00351 #if CPIXEVERBOSITY > 0
00352 printf("\nCleaning CPIXE Memory usage...\n\n");
00353 #endif
00354
00355
00356 if(!Filter.changes)freeFilter(&Filter);
00357
00358 for(i=1; i<=pSimPar->MaxZinsample ;i++){
00359 if(PresentElems[i]){
00360 free(SPTArray[i].S);
00361 free(SPTArray[i].E);
00362 free(SPTArray[i].dSdE);
00363 }
00364 }
00365 free(SPTArray);
00366 free(PresentElems);
00367 free(XYldArray);
00368 free(FCKCoefArray);
00369 free(TotAbsCoefArray);
00370 free(CalcFlags);
00371
00372
00373 if(ExtraInfo.WantOutputfile)fclose(outputfile);
00374
00375
00376 }
00377
00378
00379 lastNcall=NCALL;
00380
00381 #if CPIXEVERBOSITY > 0
00382 printf("\n\n\n************ END OF CPIXE OUTPUT ****************\n\n");
00383 #endif
00384
00385 return EXIT_SUCCESS;
00386 }
00387
00388
00389
00390
00391