00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00038 #ifdef HAVE_CONFIG_H
00039 #include <config.h>
00040 #endif
00041
00042 #include <stdio.h>
00043 #include <stdlib.h>
00044 #include <math.h>
00045 #include <string.h>
00046
00047 #include "../libcpixe/libcpixe.h"
00048 #include "../libcpixe/compilopt.h"
00049
00050
00051
00052
00053
00054
00055 int translatesample(int NFoil, int Nels, const int *pFOILZ,
00056 const float *pFOILM, const float *pAMNT, const float *pFOILTHICK,
00057 int *MaxZinsample, foil **MatArray);
00058
00059 int translatesample2(int NFoil, int MaxNels, const int *pNels, const int *pFOILZ,
00060 const float *pFOILM, const float *pAMNT, const float *pFOILTHICK,
00061 int *MaxZinsample, foil **MatArray);
00062
00063 int translateresults(int dimSums, int dimResults, const int *PresentElems,
00064 const CalibYld *XYldSums, CPIXERESULTS *pRESULTS);
00065
00066 int translateresultsNDF(int Nels, const int *pFOILZ, const CalibYld *XYldSums, CPIXERESULTS *pRESULTS);
00067
00068
00069
00070
00071 int translatesample(int NFoil, int Nels, const int *pFOILZ,
00072 const float *pFOILM, const float *pAMNT, const float *pFOILTHICK,
00073 int *MaxZinsample, foil **MatArray)
00074 {
00075 int i,j,nelem,iels;
00076 double sumM;
00077 foil *pfoil;
00078 COMPOUND *c;
00079
00080
00081 *MatArray=(foil*)malloc((NFoil)*sizeof(foil));
00082 if (*MatArray==NULL){printf("\n Error allocating memory (MatArray)\n");exit(1);}
00083 for (*MaxZinsample=0,i = 0; i < NFoil ; i++) {
00084
00085
00086 for(nelem=0, iels=0; iels<Nels ; iels++) if(pAMNT[iels+Nels*i]>0.) nelem++;
00087 if(nelem<1){fprintf(stderr,"\n ERROR: At least 1 element in each foil needed\n");exit(1);}
00088
00089 pfoil=&((*MatArray)[i]);
00090 pfoil->nfoilelm=nelem;
00091 pfoil->thick=(double)pFOILTHICK[i];
00092 c=&pfoil->comp;
00093
00094 c->nelem=nelem;
00095 if(!(c->elem=(ELEMENT*)calloc(nelem,sizeof(ELEMENT)))){fprintf(stderr,"\n Error allocating memory\n");exit(1);}
00096 if(!(c->X=(double*)calloc(nelem,sizeof(double)))){fprintf(stderr,"\n Error allocating memory\n");exit(1);}
00097 if(!(c->xn=(double*)calloc(nelem,sizeof(double)))){fprintf(stderr,"\n Error allocating memory\n");exit(1);}
00098 if(!(c->w=(double*)calloc(nelem,sizeof(double)))){fprintf(stderr,"\n Error allocating memory\n");exit(1);}
00099 for(j=0, c->sumX=0., sumM=0., iels=0 ; iels<Nels ; iels++){
00100 if(pAMNT[iels+Nels*i]>0.){
00101 c->X[j]= (double) (pAMNT[iels+Nels*i]);
00102 c->elem[j].Z=pFOILZ[iels];
00103 c->elem[j].A=Z2mass(c->elem[j].Z, &c->elem[j].IM, 'm');
00104 c->elem[j].M=(double)pFOILM[iels];
00105 c->sumX+=c->X[j];
00106 sumM+=c->elem[j].M*c->X[j];
00107 if(c->elem[j].Z>*MaxZinsample) *MaxZinsample=c->elem[j].Z;
00108 j++;
00109 }
00110 }
00111
00112 for(j=0;j<nelem;j++){
00113 c->xn[j]=c->X[j]/c->sumX;
00114 c->w[j]=c->X[j]*c->elem[j].M/sumM;
00115 }
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 return(0);
00128
00129 }
00130
00131
00132 int translatesample2(int NFoil, int MaxNels, const int *pNels, const int *pFOILZ,
00133 const float *pFOILM, const float *pAMNT, const float *pFOILTHICK,
00134 int *MaxZinsample, foil **MatArray)
00135 {
00136 int i,j,nelem;
00137 double sumM;
00138 foil *pfoil;
00139 COMPOUND *c;
00140
00141 #if CPIXEVERBOSITY >1
00142 printf("\n\nDEBUG:Translatesample2(): NFoil=%d MaxNels=%d",NFoil,MaxNels);
00143 for(i=0;i<NFoil;i++){
00144 printf("\nDEBUG:Translatesample2(): Foil %d Nels=%d",i,pNels[i]);
00145 for(j=0;j<pNels[i];j++)printf("\nDEBUG:Translatesample2():Elem %d --> Z=%d",j,pFOILZ[j+MaxNels*i]);
00146 }
00147 #endif
00148
00149 *MatArray=(foil*)malloc((NFoil)*sizeof(foil));
00150 if (*MatArray==NULL){printf("\n Error allocating memory (MatArray)\n");exit(1);}
00151 for (*MaxZinsample=0,i = 0; i < NFoil ; i++) {
00152
00153
00154 nelem=pNels[i];
00155 if(nelem<1){fprintf(stderr,"\n ERROR: At least 1 element in each foil needed\n");exit(1);}
00156
00157 pfoil=&((*MatArray)[i]);
00158 pfoil->nfoilelm=nelem;
00159 pfoil->thick=(double)pFOILTHICK[i];
00160 c=&pfoil->comp;
00161
00162 c->nelem=nelem;
00163 if(!(c->elem=(ELEMENT*)calloc(nelem,sizeof(ELEMENT)))){fprintf(stderr,"\n Error allocating memory\n");exit(1);}
00164 if(!(c->X=(double*)calloc(nelem,sizeof(double)))){fprintf(stderr,"\n Error allocating memory\n");exit(1);}
00165 if(!(c->xn=(double*)calloc(nelem,sizeof(double)))){fprintf(stderr,"\n Error allocating memory\n");exit(1);}
00166 if(!(c->w=(double*)calloc(nelem,sizeof(double)))){fprintf(stderr,"\n Error allocating memory\n");exit(1);}
00167 for(j=0, c->sumX=0., sumM=0. ; j<nelem ; j++){
00168 c->X[j]= (double) (pAMNT[j+MaxNels*i]);
00169 c->elem[j].Z=pFOILZ[j+MaxNels*i];
00170 c->elem[j].A=Z2mass(c->elem[j].Z, &c->elem[j].IM, 'm');
00171 c->elem[j].M=(double)pFOILM[j+MaxNels*i];
00172 c->sumX+=c->X[j];
00173 sumM+=c->elem[j].M*c->X[j];
00174 if(c->elem[j].Z>*MaxZinsample) *MaxZinsample=c->elem[j].Z;
00175 }
00176
00177 for(j=0;j<nelem;j++){
00178 c->xn[j]=c->X[j]/c->sumX;
00179 c->w[j]=c->X[j]*c->elem[j].M/sumM;
00180 }
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 return(0);
00193
00194
00195 }
00196
00197
00198
00199 int translateresults(int dimSums, int dimResults, const int *PresentElems, const CalibYld *XYldSums, CPIXERESULTS *pRESULTS)
00200 {
00201 int Z,ielem;
00202 for(ielem=0, Z=1 ; Z < dimSums ; Z++){
00203 if(PresentElems[Z]){
00204 ielem++;
00205 if(ielem>dimResults){fprintf(stderr,"\n Error: cpixefif: 'dimResults' too small\n");exit(1);}
00206 pRESULTS[ielem].simareas=XYldSums[Z];
00207 pRESULTS[ielem].atnum=Z;
00208 }
00209 }
00210 return(0);
00211 }
00212
00213
00214 int translateresultsNDF(int Nels, const int *pFOILZ, const CalibYld *XYldSums, CPIXERESULTS *pRESULTS)
00215 {
00216 int i,Z;
00217 for(i=0;i<Nels;i++){
00218 Z=pFOILZ[i];
00219 pRESULTS[i].atnum=Z;
00220 pRESULTS[i].simareas=XYldSums[Z];
00221 }
00222 return(0);
00223 }
00224
00225 void TESTFIF()
00226 {
00227 printf("\n testfif() is working...\n");
00228 }
00229
00230 void CPIXEMAIN(int NCALL)
00231 {
00232
00233 STATFILENAME calibfilename="";
00234 STATFILENAME fckfilename="";
00235 STATFILENAME abscoeffilename="";
00236 STATFILENAME dbpath="./";
00237
00238
00239 EXP_PARAM *pExpPar;
00240 SIM_PARAM *pSimPar;
00241
00242 foil *MatArray;
00243 int NFoil, NFoilUsed, dimSums;
00244 LYR *lyr;
00245 CalibYld *XYldSums;
00246 int i,j,l;
00247
00248
00249 static int *PresentElems;
00250 static XrayYield *XYldArray;
00251 static FluorCKCoef *FCKCoefArray;
00252 static AbsCoef *TotAbsCoefArray;
00253 static SPT *SPTArray;
00254 static int lastNcall=-1;
00255 static FILTER Filter;
00256
00257
00258 extern EXP_PARAM CPIXEIF_mp_EXPPAR;
00259 extern char CPIXEIF_mp_CALFILENM[256];
00260 extern char CPIXEIF_mp_FLTFILENM[256];
00261 extern char CPIXEIF_mp_FCKFILENM[256];
00262 extern char CPIXEIF_mp_ABSFILENM[256];
00263 extern char CPIXEIF_mp_DBPATH[256];
00264 extern int CPIXEIF_mp_CPNLAY;
00265 extern int CPIXEIF_mp_CPNELS;
00266 extern int CPIXEIF_mp_DIMRESULTS;
00267 extern int CPIXEIF_mp_NLAYFILTER;
00268 extern int CPIXEIF_mp_MAXNELSFILTER;
00269 extern int CPIXEIF_mp_FILTERCHANGES;
00270
00271 extern int CPIXEIF_mp_POINT_FOILZ, CPIXEIF_mp_POINT_FILTERZ,
00272 CPIXEIF_mp_POINT_FILTERNELS;
00273 extern int CPIXEIF_mp_POINT_FOILTHICK , CPIXEIF_mp_POINT_FOILM,
00274 CPIXEIF_mp_POINT_FOILAMNT , CPIXEIF_mp_POINT_FILTERM,
00275 CPIXEIF_mp_POINT_FILTERTHICK,CPIXEIF_mp_POINT_FILTERAMNT ;
00276 extern int CPIXEIF_mp_POINT_PIXERESULTS;
00277 extern int CPIXEIF_mp_POINT_PIXECALC;
00278
00279 int *pFOILZ, *pFILTERZ, *pFILTERNELS;
00280 float *pFOILTHICK ,*pFOILM ,*pAMNT,*pFILTERTHICK ,*pFILTERM ,*pFILTERAMNT;
00281 CPIXERESULTS *pRESULTS;
00282 CPIXERESULTS *pCALCFLAGS;
00283
00284 #if CPIXEVERBOSITY > 0
00285 printf("\n\n\n************ BEGIN OF CPIXE OUTPUT ****************\n\n");
00286 #endif
00287
00288
00289 pFOILZ=(int*)CPIXEIF_mp_POINT_FOILZ;
00290 pFOILTHICK=(float*)CPIXEIF_mp_POINT_FOILTHICK;
00291 pFOILM=(float*)CPIXEIF_mp_POINT_FOILM;
00292 pAMNT=(float*)CPIXEIF_mp_POINT_FOILAMNT;
00293 pFILTERNELS=(int*)CPIXEIF_mp_POINT_FILTERNELS;
00294 pFILTERZ=(int*)CPIXEIF_mp_POINT_FILTERZ;
00295 pFILTERTHICK=(float*)CPIXEIF_mp_POINT_FILTERTHICK;
00296 pFILTERM=(float*)CPIXEIF_mp_POINT_FILTERM;
00297 pFILTERAMNT=(float*)CPIXEIF_mp_POINT_FILTERAMNT;
00298 pRESULTS=(CPIXERESULTS*) CPIXEIF_mp_POINT_PIXERESULTS;
00299 pCALCFLAGS=(CPIXERESULTS*)CPIXEIF_mp_POINT_PIXECALC;
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 MatArray=NULL;
00328 lyr=NULL;
00329
00330
00331 pExpPar=&CPIXEIF_mp_EXPPAR;
00332 pSimPar=&pExpPar->simpar;
00333
00334 if(NCALL>0){
00335 if(lastNcall<0){fprintf(stderr,"\n ERROR: CPIXEMAIN: CPIXEMAIN was not initialized.\n");exit(1);}
00336
00337
00338
00339
00340
00341
00342
00343 NFoil=CPIXEIF_mp_CPNLAY;
00344 translatesample(NFoil,CPIXEIF_mp_CPNELS,pFOILZ,pFOILM,pAMNT,pFOILTHICK,&pSimPar->MaxZinsample,&MatArray);
00345
00346
00347 if(Filter.changes){
00348 Filter.nlyr=CPIXEIF_mp_NLAYFILTER;
00349 Filter.geomcorr=1;
00350 translatesample2(Filter.nlyr, CPIXEIF_mp_MAXNELSFILTER , pFILTERNELS, pFILTERZ,
00351 pFILTERM, pFILTERAMNT, pFILTERTHICK,
00352 &Filter.MaxZ, &Filter.foil );
00353 createPresentElems(Filter.MaxZ, Filter.nlyr, Filter.foil, &Filter.FilterElems);
00354
00355
00356
00357 Filter.changes=CPIXEIF_mp_FILTERCHANGES;
00358 }
00359
00360
00361 dimSums=pSimPar->MaxZinsample+1;
00362 XYldSums=(CalibYld*)calloc(dimSums,sizeof(CalibYld));
00363 integrate_Simpson(pExpPar, TotAbsCoefArray, FCKCoefArray, XYldArray ,NFoilUsed, &Filter, lyr, XYldSums);
00364 translateresultsNDF(CPIXEIF_mp_CPNELS, pFOILZ, XYldSums, pRESULTS);
00365
00366
00367
00368 #if CPIXEVERBOSITY > 0
00369
00370
00371 printf("\n\nDEBUG: Ebeam=%lf IncAng=%lf DetAng=%lf \n",
00372 pExpPar->BeamEner, pExpPar->IncAng, pExpPar->DetAng);
00373 printf("\nDEBUG: Charge=%lf DTCC=%lf DetColFac=%lf\n",
00374 pExpPar->simpar.ColCharge, pExpPar->simpar.DTCC, pExpPar->DetColFac);
00375
00376 printf("\n\nDEBUG: Iteration %d\n",NCALL);
00377
00378 for(i=0;i<NFoil;i++){
00379 printf("\nMatArray[%d] (%d elem)\n",i,MatArray[i].nfoilelm);
00380 printf("\n---Eff. thickness=%.3le (%d sublyrs)\n",lyr[i+1].ThickIn,lyr[i+1].FESxlen);
00381
00382 for(j=0;j<MatArray[i].comp.nelem;j++){
00383 printf("'%s'(Z=%d) X[%d]=%lf (%lfat%% / %lfM%%)\n",
00384 XYldArray[MatArray[i].comp.elem[j].Z].symb,MatArray[i].comp.elem[j].Z,j,MatArray[i].comp.X[j],100*MatArray[i].comp.xn[j],100*MatArray[i].comp.w[j]);
00385 }
00386 printf("\nFinal Energy in this layer=%lf keV\n\n",lyr[i+1].FoilOutEner);
00387 }
00388
00389 for(i=0;i<Filter.nlyr;i++){
00390 printf("\nFilter[%d] (%lf 1e15at/cm2) (%d elem) \n",i,Filter.foil[i].thick,Filter.foil[i].nfoilelm);
00391 for(j=0;j<Filter.foil[i].comp.nelem;j++){
00392 printf("(Z=%d) X[%d]=%lf (%lfat%% / %lfM%%)\n",
00393 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]);
00394 }
00395 }
00396
00397 for(i=0;i<dimSums;i++){
00398 if(i<=pSimPar->MaxZinsample && PresentElems[i]){
00399 printf("\n\n ************RESULTS for %s (Z=%d)\n",XYldArray[i].symb,i);
00400
00401 for (j = 1; j <= 3; j++)
00402 if(XYldSums[i].K_[j] > 0.)
00403 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]);
00404
00405 for (j = 1; j <= 3; j++)
00406 for (l = 1; l <= 3; l++)
00407 if(XYldSums[i].L_[j][l] > 0.)
00408 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]);
00409
00410 for (j = 1; j <= 3; j++)
00411 if(XYldSums[i].M_[j] > 0.)
00412 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]);
00413 }
00414 }
00415
00416
00417 #endif
00418
00419
00420 freeReusable(NFoil,&lyr,&MatArray,&XYldSums);
00421
00422 if(Filter.changes)freeFilter(&Filter);
00423
00424 }
00425
00426 else if(NCALL==0){
00427
00428
00429
00430
00431
00432
00433
00434
00435 strcpy(dbpath,CPIXEIF_mp_DBPATH);
00436 strcpy(calibfilename,CPIXEIF_mp_CALFILENM);
00437 strcpy(fckfilename ,CPIXEIF_mp_FCKFILENM);
00438 strcpy(abscoeffilename,CPIXEIF_mp_ABSFILENM);
00439
00440 #if CPIXEVERBOSITY > 0
00441 printf("\nDEBUG: Initializing. Reading databases...\n\n");
00442 printf("\nDEBUG: Stopping Coefs DB: '%sSCOEF.95[A/B]'",dbpath);
00443 printf("\nDEBUG: Absorption Coefs DB: '%s'",abscoeffilename);
00444 printf("\nDEBUG: Fluorescence Coefs DB: '%s'",fckfilename);
00445 printf("\nDEBUG: Detector Calibration DB: '%s'\n",calibfilename);
00446 #endif
00447
00448
00449 NFoil=CPIXEIF_mp_CPNLAY;
00450 translatesample(NFoil,CPIXEIF_mp_CPNELS,pFOILZ,pFOILM,pAMNT,pFOILTHICK,&pSimPar->MaxZinsample,&MatArray);
00451
00452
00453 Filter.nlyr=CPIXEIF_mp_NLAYFILTER;
00454 translatesample2(Filter.nlyr, CPIXEIF_mp_MAXNELSFILTER , pFILTERNELS, pFILTERZ,
00455 pFILTERM, pFILTERAMNT, pFILTERTHICK,
00456 &Filter.MaxZ, &Filter.foil );
00457 createPresentElems(Filter.MaxZ, Filter.nlyr, Filter.foil, &Filter.FilterElems);
00458 Filter.changes=1;
00459
00460 createPresentElems(pSimPar->MaxZinsample, NFoil, MatArray, &PresentElems);
00461 readXYld(calibfilename, PresentElems, pCALCFLAGS, pSimPar->MaxZinsample, &pSimPar->CalEner, &XYldArray);
00462 readFCK(fckfilename, pSimPar->MaxZinsample,&FCKCoefArray);
00463
00464 readAbsCoef(abscoeffilename, pSimPar->MaxZinsample, &TotAbsCoefArray);
00465 createSPTs(dbpath,pExpPar, pSimPar->MaxZinsample, PresentElems, (double)(2*pExpPar->ion.A), &SPTArray);
00466
00467 safefree((void*)&MatArray);
00468 safefree((void*)&Filter.foil);
00469
00470 }
00471
00472 else if(NCALL<0){
00473
00474
00475 #if CPIXEVERBOSITY > 0
00476 printf("\nDEBUG: Cleaning CPIXE Memory usage...\n\n");
00477 #endif
00478
00479
00480 if(!Filter.changes)freeFilter(&Filter);
00481
00482 for(i=1; i<=pSimPar->MaxZinsample ;i++){
00483 if(PresentElems[i]){
00484 free(SPTArray[i].S);
00485 free(SPTArray[i].E);
00486 free(SPTArray[i].dSdE);
00487 }
00488 }
00489 free(SPTArray);
00490 free(PresentElems);
00491 free(XYldArray);
00492 free(FCKCoefArray);
00493 free(TotAbsCoefArray);
00494 free(XYldSums);
00495
00496 }
00497
00498
00499
00500
00501 lastNcall=NCALL;
00502
00503 #if CPIXEVERBOSITY > 0
00504 printf("\n\n\n************ END OF CPIXE OUTPUT ****************\n\n");
00505 #endif
00506
00507 return;
00508 }
00509
00510