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