cpixe_fif/cpixe_fif.c

Go to the documentation of this file.
00001 
00002 /***************************************************************************
00003     Copyright (C) 2007 by Carlos Pascual-Izarra
00004     carlos.pascual_AT_users.sourceforge.net                                                  *
00005 
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 //Function prototypes:
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 //Functions:
00069 
00070 /*Translation of sample structures. */
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   /*Initialize  Sample*/
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     //obtain the number of elements for this layer
00086     for(nelem=0, iels=0; iels<Nels ; iels++) if(pAMNT[iels+Nels*i]>0.) nelem++;  //CHECK THIS!!!
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]);  //NOTE!!: The indexes are switched and "offseted" to deal with the the Fortran convention!!
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];  //The mass passed by NDF is put on the "natural mass"
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     /*Normalize concentrations and calculate the MASS concentration*/
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 //  for(i=0;i<NFoil;i++){
00118 //    printf("\nFoil %d: th=%le nelem=%d",i,(*MatArray)[i].thick,(*MatArray)[i].nfoilelm);
00119 //    for(j=0;j<(*MatArray)[i].nfoilelm;j++){
00120 //      printf("\nTrans: (i=%d , j=%d) (*MatArray)[i].comp.elem[j].Z=%d ",i,j,(*MatArray)[i].comp.elem[j].Z);
00121 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].M=%lf ",(*MatArray)[i].comp.elem[j].M);
00122 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].IM=%lf ",(*MatArray)[i].comp.elem[j].IM);
00123 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].A=%d ",(*MatArray)[i].comp.elem[j].A);
00124 //    printf("\nTrans:      X=%lf   xn=%lf   w=%lf",(*MatArray)[i].comp.X[j],(*MatArray)[i].comp.xn[j],(*MatArray)[i].comp.w[j]);
00125 //    }
00126 //  }
00127   return(0);
00128 
00129 }
00130 
00131 /*Alternative translation of sample structures. */
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   /*Initialize  Sample*/
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     //obtain the number of elements for this layer
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]);  //NOTE!!: The indexes are switched and "offseted" to deal with the the Fortran convention!!
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];  //The mass passed by NDF is put on the "natural mass"
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     /*Normalize concentrations and calculate the MASS concentration*/
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 //  for(i=0;i<NFoil;i++){
00183 //    printf("\nFoil %d: th=%le nelem=%d",i,(*MatArray)[i].thick,(*MatArray)[i].nfoilelm);
00184 //    for(j=0;j<(*MatArray)[i].nfoilelm;j++){
00185 //      printf("\nTrans: (i=%d , j=%d) (*MatArray)[i].comp.elem[j].Z=%d ",i,j,(*MatArray)[i].comp.elem[j].Z);
00186 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].M=%lf ",(*MatArray)[i].comp.elem[j].M);
00187 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].IM=%lf ",(*MatArray)[i].comp.elem[j].IM);
00188 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].A=%d ",(*MatArray)[i].comp.elem[j].A);
00189 //    printf("\nTrans:      X=%lf   xn=%lf   w=%lf",(*MatArray)[i].comp.X[j],(*MatArray)[i].comp.xn[j],(*MatArray)[i].comp.w[j]);
00190 //    }
00191 //  }
00192   return(0);
00193 
00194 
00195 }
00196 
00197 
00198 /*Copies the results of XYldSums to the array pRESULTS */
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   /*Static variables (Those that should be maintained between calls of CPIXEMAIN()*/
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   /*Extern Variables (defined in the CPIXEIF Fortran module)*/
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;     //these are DEC pointers to ints
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 ;  //these are DEC pointers to floats
00276   extern int CPIXEIF_mp_POINT_PIXERESULTS; //This is a DEC pointer to a CPIXERESULT
00277   extern int CPIXEIF_mp_POINT_PIXECALC;
00278 
00279   int *pFOILZ, *pFILTERZ, *pFILTERNELS; //Those are the "true" C pointers to ints
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   //Transforming fortran (DEC) pointers (which are integers) into true C pointers
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 // printf("\nFOILTHICK->%d    NELEM-->%d\n",pFOILTHICK,pNELEM);
00303 //  printf("\nNCALL=%d\t Z=%d\n",NCALL,CPIXEIF_mp_EXPPAR.ion.Z);
00304 //  for(i=0;i<=10;i++)for(j=0;j<3;j++)
00305 //    printf("\nF.coef[%d][%d]=%lf",i,j,CPIXEIF_mp_SIMPAR.Filter.coef[i][j]);
00306 //  for(j=0;j<3;j++)printf("\nF.h[%d]=%lf\n",j,CPIXEIF_mp_SIMPAR.Filter.h[j]);
00307 //
00308 //  printf("\nCPIXE: '%s'",CPIXEIF_mp_CALFILENM);
00309 //  printf("\nCPIXE: 'MAXELEM=%d'",CPIXEIF_mp_NELS);
00310 //
00311 //  for(i=0 ; i<=3; i++) printf("\nCPIXE: '%s'",CPIXEIF_mp_CALFILENM);
00312 //
00313 //      /*K lines:*/
00314 //      for (j = 1; j <= 3; j++)  printf("\n%10s %1.1lf ", LineNm[1][j], pRESULTS->K_[j] );
00315 //      /*L-Lines*/
00316 //      for (j = 1; j <= 3; j++)
00317 //        for (l = 1; l <= 3; l++)
00318 //          printf("\n%10s %1.1lf ", LineNm[j+1][l], pRESULTS->L_[j][l] );
00319 //      /*M lines:*/
00320 //      for (j = 1; j <= 3; j++)  printf("\n%10s %1.1lf ", LineNm[5][j], pRESULTS->M_[j] );
00321 // printf("\n!!!!!!!!!!!\n");
00322 // getchar();
00323 
00324 
00325 
00326   /*Note: The following initialization needs to be done each time the function is called*/
00327   MatArray=NULL;
00328   lyr=NULL;
00329 
00330   //make a convenience pointer to the EXPPAR and SIMPAR structures defined in the fortran module
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     /******* BEGIN CALCULATION*********/
00337     /*
00338     These calls are all what is needed to do for each iteration
00339     (But don't forget also the call to freeReusable() before starting a new iteration)
00340     */
00341 
00342     //translate the sample definition made in NDF to that used in CPIXE
00343     NFoil=CPIXEIF_mp_CPNLAY;
00344     translatesample(NFoil,CPIXEIF_mp_CPNELS,pFOILZ,pFOILM,pAMNT,pFOILTHICK,&pSimPar->MaxZinsample,&MatArray);
00345 
00346   //translate Filter definition
00347     if(Filter.changes){
00348       Filter.nlyr=CPIXEIF_mp_NLAYFILTER;
00349       Filter.geomcorr=1; //filter must be perpendicular
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 //       FilterTrans(pExpPar, XYldArray, TotAbsCoefArray, PresentElems, &Filter);
00355 //      FilterTrans(pExpPar->simpar.MaxZinsample, LinesEnerArray, TotAbsCoefArray, PresentElems, &Filter);
00356       //the Filter.changes variable is set externally
00357       Filter.changes=CPIXEIF_mp_FILTERCHANGES;
00358     }
00359 
00360 //     initlyrarray(pExpPar, MatArray, XYldArray, TotAbsCoefArray, SPTArray, PresentElems, NFoil, &NFoilUsed, &lyr);
00361     dimSums=pSimPar->MaxZinsample+1;
00362     XYldSums=(CalibYld*)calloc(dimSums,sizeof(CalibYld));  //Note that XYldSums is 0-initialized
00363     integrate_Simpson(pExpPar, TotAbsCoefArray, FCKCoefArray, XYldArray ,NFoilUsed, &Filter, lyr, XYldSums);
00364     translateresultsNDF(CPIXEIF_mp_CPNELS, pFOILZ, XYldSums, pRESULTS);
00365       /******END CALCULATION**********/
00366 
00367 
00368     #if CPIXEVERBOSITY > 0
00369     /**********BEGIN DEBUG LINES*******/
00370     /* Just write stuff for debugging*/
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         /*K lines:*/
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         /*L-Lines*/
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         /*M lines:*/
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 //    getchar();
00416     /**********END DEBUG LINES*******/
00417     #endif
00418 
00419     /*It is VERY important to call freeReusable() before starting a new iteration*/
00420     freeReusable(NFoil,&lyr,&MatArray,&XYldSums);
00421 //    printf("\n!!!!!!! %d  %d",NCALL,Filter.changes);
00422     if(Filter.changes)freeFilter(&Filter);
00423 
00424   }
00425 
00426   else if(NCALL==0){
00427     /**********BEGIN INITIALIZATION*********/
00428     /*
00429     This Block is run only when initialization is required.
00430     It reads the databases (Fluorescence, Stoppings,...) and performs some operations on data
00431     other data that never change within a fit.
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     //translate the sample definition made in NDF to that used in CPIXE
00449     NFoil=CPIXEIF_mp_CPNLAY;
00450     translatesample(NFoil,CPIXEIF_mp_CPNELS,pFOILZ,pFOILM,pAMNT,pFOILTHICK,&pSimPar->MaxZinsample,&MatArray);
00451 
00452     //translate Filter definition
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;    //First time at least Filter should be read:
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     //readAbsCoef_OLD(abscoeffilename, pSimPar->MaxZinsample, PresentElems, &Filter, &TotAbsCoefArray);
00464     readAbsCoef(abscoeffilename, pSimPar->MaxZinsample, &TotAbsCoefArray);
00465     createSPTs(dbpath,pExpPar, pSimPar->MaxZinsample, PresentElems, (double)(2*pExpPar->ion.A), &SPTArray);
00466 
00467     safefree((void*)&MatArray); //Clear the initialized Matrix Array.
00468     safefree((void*)&Filter.foil); //Clear the initialized Filter Foil.
00469     /********** END INITIALIZATION *********/
00470   }
00471 
00472   else if(NCALL<0){
00473     /******** BEGIN TIDING UP BEFORE FINAL EXIT*********/
00474     /*Free everything for a clean exit*/
00475     #if CPIXEVERBOSITY > 0
00476     printf("\nDEBUG: Cleaning CPIXE Memory usage...\n\n");
00477     #endif
00478 
00479     //Note: Filter.changes=0 implies that freeFilter was not called in last iteration:
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     /******** END TIDING UP BEFORE FINAL EXIT*********/
00496   }
00497 
00498 //  printf("\n\nDEBUG: End of program reached\n\n");getchar();
00499 
00500   /*Store Number of Ncall*/
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 

Generated on Sat Dec 8 23:11:36 2007 for libCPIXE by  doxygen 1.5.4