cpixe/cpixe.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 
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <math.h>
00037 #include <string.h>
00038 
00039 #include "../libcpixe/libcpixe.h"
00040 #include "../libcpixe/compilopt.h"
00041 
00042 
00043 int main(int argc, char *argv[]);
00044 int cpixemain(int NCALL);
00045 
00046 STATFILENAME inputfilename="input.in";
00047 
00054 int main(int argc, char *argv[])
00055 {
00056   extern STATFILENAME inputfilename;
00057 
00058   if(argc>1)strcpy(inputfilename,argv[1]);
00059   cpixemain(0);
00060   cpixemain(1);
00061   cpixemain(-1);
00062 
00063   return EXIT_SUCCESS;
00064 }
00065 
00077 int cpixemain(int NCALL)
00078 {
00079   extern STATFILENAME inputfilename;
00080   STATFILENAME fckfilename="";
00081   STATFILENAME abscoeffilename="";
00082   STATFILENAME xenerfilename="";
00083 
00084 
00085   LYR *lyr;
00086   CalibYld *XYldSums;
00087   int i,j,l;
00088   EXP_PARAM *pExpPar;
00089   SIM_PARAM *pSimPar;
00090 
00091    /*Static variables (Those that should be maintained between calls of CPIXEMAIN()*/
00092   static int *PresentElems;
00093   static CPIXERESULTS *CalcFlags;
00094 //   static XrayYield *XYldArray;
00095   static CalibYld *LinesEnerArray;
00096   static CalibYld *EffArray;
00097   static FluorCKCoef *FCKCoefArray;
00098   static AbsCoef *TotAbsCoefArray;
00099   static SPT *SPTArray;
00100   static SFCListElem *SFCList;
00101   static int nSFCList;
00102   static int lastNcall=-1;
00103   static FILTER Filter;
00104 
00105   static EXP_PARAM ExpPar;
00106   static foil *Sample;
00107   static int NFoil, NFoilUsed, dimSums;
00108   static FILE *outputfile;
00109   static EXTRAINFO ExtraInfo;
00110 
00111   #if CPIXEVERBOSITY > 0
00112   printf("\n\n\n************ BEGIN OF CPIXE OUTPUT ****************\n\n");
00113   #endif
00114 
00115   pExpPar=&ExpPar;
00116   pSimPar=&pExpPar->simpar;
00117 
00118   if(NCALL>0){
00119     if(lastNcall<0){fprintf(stderr,"\n ERROR: CPIXEMAIN: CPIXEMAIN was not initialized.\n");exit(1);}
00120     /******* BEGIN CALCULATION*********/
00121     /*
00122     These calls are all what is needed to do for each iteration
00123     (But don't forget also the call to freeReusable() before starting a new iteration)
00124     */
00125 
00126 
00127     //Calculate Filter Transmission if the filter changed
00128     if(Filter.changes){
00129 //       FilterTrans(pExpPar, XYldArray, TotAbsCoefArray, PresentElems, &Filter);
00130       FilterTrans(pExpPar->simpar.MaxZinsample, LinesEnerArray, TotAbsCoefArray, PresentElems, &Filter);
00132       Filter.changes=0;
00133     }
00134 
00135 //     initlyrarray(pExpPar, Sample, XYldArray, TotAbsCoefArray, SPTArray, PresentElems, NFoil, &NFoilUsed, &lyr);
00136     initlyrarray(pExpPar, Sample, LinesEnerArray, TotAbsCoefArray, SPTArray, PresentElems, NFoil, &NFoilUsed, &lyr);
00137     dimSums=pSimPar->MaxZinsample+1;
00138     XYldSums=(CalibYld*)calloc(dimSums,sizeof(CalibYld));  //Note that XYldSums is 0-initialized
00139 //     integrate_Simpson(pExpPar, TotAbsCoefArray, FCKCoefArray, XYldArray ,NFoilUsed, &Filter, lyr, XYldSums);
00140     integrate_Simpson2(pExpPar, TotAbsCoefArray, FCKCoefArray, EffArray ,NFoilUsed, &Filter, lyr, XYldSums);
00141 //    SFCorr(pExpPar, TotAbsCoefArray, FCKCoefArray, XYldArray ,NFoilUsed, &Filter, lyr, XYldSums);
00142       /******END CALCULATION**********/
00143 
00144 
00145     #if CPIXEVERBOSITY > 0
00146     /**********BEGIN DEBUG LINES*******/
00147     /* Just write stuff for debugging*/
00148     printf("\n\n Ebeam=%lf IncAng=%lf  DetAng=%lf \n",
00149           pExpPar->BeamEner, pExpPar->IncAng/DEG2RAD, pExpPar->DetAng/DEG2RAD);
00150     printf("\n Charge=%lf   DTCC=%lf DetColFac=%lf\n",
00151           pExpPar->simpar.ColCharge, pExpPar->simpar.DTCC, pExpPar->DetColFac);
00152 
00153     printf("\n\nIteration %d\n",NCALL);
00154 
00155     for(i=0;i<NFoil;i++){
00156       printf("\nSample[%d]  (%d elem)\n",i,Sample[i].nfoilelm);
00157       printf("\n---Eff. thickness=%.3le  (%d sublyrs)\n",lyr[i+1].ThickIn,lyr[i+1].FESxlen);
00158 
00159       for(j=0;j<Sample[i].comp.nelem;j++){
00160       printf("'%s'(Z=%d) X[%d]=%lf (%lfat%% / %lfM%%)\n",
00161           ChemicalSymbol[Sample[i].comp.elem[j].Z],Sample[i].comp.elem[j].Z,j,Sample[i].comp.X[j],100*Sample[i].comp.xn[j],100*Sample[i].comp.w[j]);
00162       }
00163       printf("\nFinal Energy in this layer=%lf keV\n\n",lyr[i+1].FoilOutEner);
00164     }
00165 
00166     for(i=0;i<Filter.nlyr;i++){
00167       printf("\nFilter[%d] (%lf 1e15at/cm2)  (%d elem)  \n",i,Filter.foil[i].thick,Filter.foil[i].nfoilelm);
00168       for(j=0;j<Filter.foil[i].comp.nelem;j++){
00169       printf("(Z=%d) X[%d]=%lf (%lfat%% / %lfM%%)\n",
00170           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]);
00171       }
00172     }
00173 
00174     for(i=0;i<dimSums;i++){
00175       if(i<=pSimPar->MaxZinsample && PresentElems[i]){
00176         printf("\n\n ************RESULTS for %s (Z=%d)\n",ChemicalSymbol[i],i);
00177         /*K lines:*/
00178         for (j = 1; j <= 3; j++)
00179           if(XYldSums[i].K_[j] > 0.) {
00180               fprintf(stdout,"\n%10s (%2.2lfkeV)\t%le counts   (Eff=%.3le Flt=%.2le)", LineNm[1][j], LinesEnerArray[i].K_[j], XYldSums[i].K_[j],EffArray[i].K_[j],Filter.Trans[i].K_[j]);
00181               fprintf(stdout,"\n%10s \texp:\t%le counts   (err=%.1le) ", "", CalcFlags[i].simareas.K_[j],CalcFlags[i].err.K_[j]);
00182           }
00183         /*L-Lines*/
00184         for (j = 1; j <= 3; j++)
00185           for (l = 1; l <= 3; l++)
00186             if(XYldSums[i].L_[j][l] > 0.){
00187                 fprintf(stdout,"\n%10s (%2.2lfkeV)\t%le counts   (cal=%.3le Flt=%.2le)", LineNm[j+1][l], LinesEnerArray[i].L_[j][l], XYldSums[i].L_[j][l],EffArray[i].L_[j][l],Filter.Trans[i].L_[j][l]);
00188                 fprintf(stdout,"\n%10s \texp:\t%le counts   (err=%.1le) ", "", CalcFlags[i].simareas.L_[j][l],CalcFlags[i].err.L_[j][l]);
00189             }
00190         /*M lines:*/
00191         for (j = 1; j <= 3; j++)
00192           if(XYldSums[i].M_[j] > 0.) {
00193               fprintf(stdout,"\n%10s (%2.2lfkeV)\t%le counts   (cal=%.3le Flt=%.2le)",LineNm[5][j], LinesEnerArray[i].M_[j], XYldSums[i].M_[j],EffArray[i].M_[j],Filter.Trans[i].M_[j]);
00194               fprintf(stdout,"\n%10s \texp:\t%le counts   (err=%.1le) ", "", CalcFlags[i].simareas.M_[j],CalcFlags[i].err.M_[j]);
00195           }
00196       }
00197     }
00198 //    getchar();
00199     /**********END DEBUG LINES*******/
00200     #endif
00201 
00202     /**********BEGIN FILE OUTPUT LINES*******/
00203 
00204     if(ExtraInfo.WantOutputfile){
00205       fprintf(outputfile,"\n\n Ebeam=%lf IncAng=%lf  DetAng=%lf \n",
00206             pExpPar->BeamEner, pExpPar->IncAng/DEG2RAD, pExpPar->DetAng/DEG2RAD);
00207       fprintf(outputfile,"\n Charge=%lf   DTCC=%lf DetColFac=%lf\n",
00208             pExpPar->simpar.ColCharge, pExpPar->simpar.DTCC, pExpPar->DetColFac);
00209 
00210       fprintf(outputfile,"\n\nIteration %d\n",NCALL);
00211 
00212       for(i=0;i<NFoil;i++){
00213         fprintf(outputfile,"\nSample[%d]  (%d elem)\n",i,Sample[i].nfoilelm);
00214         fprintf(outputfile,"\n---Eff. thickness=%.3le  (%d sublyrs)\n",lyr[i+1].ThickIn,lyr[i+1].FESxlen);
00215 
00216         for(j=0;j<Sample[i].comp.nelem;j++){
00217         fprintf(outputfile,"'%s'(Z=%d) X[%d]=%lf (%lfat%% / %lfM%%)\n",
00218             ChemicalSymbol[Sample[i].comp.elem[j].Z],Sample[i].comp.elem[j].Z,j,Sample[i].comp.X[j],100*Sample[i].comp.xn[j],100*Sample[i].comp.w[j]);
00219         }
00220         fprintf(outputfile,"\nFinal Energy in this layer=%lf keV\n\n",lyr[i+1].FoilOutEner);
00221       }
00222 
00223       for(i=0;i<Filter.nlyr;i++){
00224         fprintf(outputfile,"\nFilter[%d] (%lf 1e15at/cm2)  (%d elem)  \n",i,Filter.foil[i].thick,Filter.foil[i].nfoilelm);
00225         for(j=0;j<Filter.foil[i].comp.nelem;j++){
00226         fprintf(outputfile,"(Z=%d) X[%d]=%lf (%lfat%% / %lfM%%)\n",
00227             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]);
00228         }
00229       }
00230       for(i=0;i<dimSums;i++){
00231         if(i<=pSimPar->MaxZinsample && PresentElems[i]){
00232           fprintf(outputfile,"\n\n ************RESULTS for %s (Z=%d)\n",ChemicalSymbol[i],i);
00233           /*K lines:*/
00234           for (j = 1; j <= 3; j++)
00235             if(XYldSums[i].K_[j] > 0.) {
00236               fprintf(outputfile,"\n%10s (%2.2lfkeV)\t%le counts   (Eff=%.3le Flt=%.2le)", LineNm[1][j], LinesEnerArray[i].K_[j], XYldSums[i].K_[j],EffArray[i].K_[j],Filter.Trans[i].K_[j]);
00237               fprintf(outputfile,"\n%10s \texp:\t%le counts   (err=%.1le) ", "", CalcFlags[i].simareas.K_[j],CalcFlags[i].err.K_[j]);
00238             }
00239           /*L-Lines*/
00240           for (j = 1; j <= 3; j++)
00241             for (l = 1; l <= 3; l++)
00242               if(XYldSums[i].L_[j][l] > 0.){
00243                 fprintf(outputfile,"\n%10s (%2.2lfkeV)\t%le counts   (cal=%.3le Flt=%.2le)", LineNm[j+1][l], LinesEnerArray[i].L_[j][l], XYldSums[i].L_[j][l],EffArray[i].L_[j][l],Filter.Trans[i].L_[j][l]);
00244                 fprintf(outputfile,"\n%10s \texp:\t%le counts   (err=%.1le) ", "", CalcFlags[i].simareas.L_[j][l],CalcFlags[i].err.L_[j][l]);
00245               }
00246           /*M lines:*/
00247           for (j = 1; j <= 3; j++)
00248             if(XYldSums[i].M_[j] > 0.) {
00249               fprintf(outputfile,"\n%10s (%2.2lfkeV)\t%le counts   (cal=%.3le Flt=%.2le)",LineNm[5][j], LinesEnerArray[i].M_[j], XYldSums[i].M_[j],EffArray[i].M_[j],Filter.Trans[i].M_[j]);
00250               fprintf(outputfile,"\n%10s \texp:\t%le counts   (err=%.1le) ", "", CalcFlags[i].simareas.M_[j],CalcFlags[i].err.M_[j]);
00251             }
00252         }
00253       }
00254     }
00255     /**********END FILE OUTPUT LINES*******/
00256 
00257     /*It is VERY important to call freeReusable() before starting a new iteration*/
00258     freeReusable(NFoil,&lyr,&Sample,&XYldSums);
00259 //    printf("\n!!!!!!! %d  %d",NCALL,Filter.changes);
00260     if(Filter.changes)freeFilter(&Filter);
00261 
00262   }
00263 
00264 
00265   else if(NCALL==0){
00266     /**********BEGIN INITIALIZATION*********/
00267     /*
00268     This Block is run only when initialization is required.
00269     It reads the databases (Fluorescence, Stoppings,...) and performs some operations on
00270     data that never change within a fit.
00271     */
00272 
00273     readINPUT(inputfilename, pExpPar, &ExtraInfo);
00274     /*Disallow the DEPRECATED CalibYld Method*/
00276     if(!ExtraInfo.useefficiencies){
00277       fprintf(stderr,"\nERROR: The DATTPIXE calibration scheme is now deprecated. Use Efficiencies instead.\n");exit(1);}
00278 
00279     readsample(ExtraInfo.SampleFileNm, &pSimPar->MaxZinsample, &NFoil, &Sample);
00280     readFilter(ExtraInfo.FilterFileNm, &Filter);
00281     //translate Filter definition
00282     Filter.changes=1;    //First time at least Filter should be read:
00283 
00284     snprintf(fckfilename, FILENMLENGTH,"%sSC_PRA74.fck",ExtraInfo.DBpath);
00285     snprintf(abscoeffilename, FILENMLENGTH,"%sxabs.abs",ExtraInfo.DBpath);
00286     snprintf(xenerfilename, FILENMLENGTH,"%sxener.dat",ExtraInfo.DBpath);
00287 
00288     #if CPIXEVERBOSITY > 0
00289     printf("\nInitializing. Reading databases...\n\n");
00290     printf("\nStopping Coefs DB: '%sSCOEF.95[A/B]'",ExtraInfo.DBpath);
00291     printf("\nAbsorption Coefs DB: '%s'",abscoeffilename);
00292     printf("\nFluorescence Coefs DB: '%s'",fckfilename);
00293     printf("\nX-ray Energies DB: '%s'",xenerfilename);
00294     printf("\nDetector Calibration DB: '%s'",ExtraInfo.CalibFileNm);
00295     printf("\nLines to calculate defined in: '%s'\n",ExtraInfo.AreasFileNm);
00296     #endif
00297 
00298     createPresentElems(pSimPar->MaxZinsample, NFoil, Sample, &PresentElems);
00299     readCalcFlags(ExtraInfo.AreasFileNm, PresentElems, pSimPar->MaxZinsample, ExtraInfo.AreasFormat, &CalcFlags);
00300 
00301     //Read Efficiencies for the lines used
00302     //2006/04/05: changed this to get the efficiencies from a list (see below)
00303     //readEff(ExtraInfo.CalibFileNm, PresentElems, pSimPar->MaxZinsample, &EffArray);
00304 
00305     //Read the energies for the lines used
00306     readLinesEner(xenerfilename, PresentElems, pSimPar->MaxZinsample, &LinesEnerArray);
00307 
00308     //2006/04/05:Read the efficiencies for the lines used (from a two-column "Energy|Effi" formated file)
00309     //readEff2(ExtraInfo.CalibFileNm, PresentElems, pSimPar->MaxZinsample, LinesEnerArray, &EffArray);
00310 
00311 
00312 
00313     readFCK(fckfilename, pSimPar->MaxZinsample,&FCKCoefArray);
00314 //     readAbsCoef_OLD(abscoeffilename, pSimPar->MaxZinsample, PresentElems, &Filter, &TotAbsCoefArray);
00315     readAbsCoef(abscoeffilename, pSimPar->MaxZinsample, &TotAbsCoefArray);
00316     createSPTs(ExtraInfo.DBpath,pExpPar, pSimPar->MaxZinsample, PresentElems, (double)(2*pExpPar->ion.A), &SPTArray);
00317 
00318     //2006/04/07:Read the efficiencies with readeff3
00319     readEff3(ExtraInfo.CalibFileNm, PresentElems, pSimPar->MaxZinsample, LinesEnerArray, &EffArray);
00320 
00321     //If Sec Fluorescence is required, determine which elements produce fluorescence on which
00322 //     if(pSimPar->AllowSXFCorr){
00323 //       createSFCList(pExpPar, PresentElems, XYldArray, TotAbsCoefArray, &nSFCList, &SFCList);
00324 //      prepareSFC(pExpPar, PresentElems);
00325 //       };
00326 
00327     //safefree((void*)&Sample); //Clear the initialized Matrix Array.
00328     //safefree((void*)&Filter.foil); //Clear the initialized Filter Foil.
00329     /********** END INITIALIZATION *********/
00330 
00331      //Write initialization parameters to output file
00332     if(ExtraInfo.WantOutputfile){
00333       outputfile = fopen(ExtraInfo.OutputFileNm, "w");
00334       if(outputfile== NULL)
00335         {fprintf(stderr,"\nERROR: Could not open file '%s' for write.\n",ExtraInfo.OutputFileNm);exit(1);}
00336       fprintf(outputfile,"\nCPIXE Output File\n\n");
00337       fprintf(outputfile,"\nStopping Coefs DB: '%sSCOEF.95[A/B]'",ExtraInfo.DBpath);
00338       fprintf(outputfile,"\nAbsorption Coefs DB: '%s'",abscoeffilename);
00339       fprintf(outputfile,"\nFluorescence Coefs DB: '%s'",fckfilename);
00340       fprintf(outputfile,"\nX-ray Energies DB:'%s'",xenerfilename);
00341       fprintf(outputfile,"\nDetector Calibration DB: '%s'",ExtraInfo.CalibFileNm);
00342       fprintf(outputfile,"\nLines to calculate defined in: '%s'\n",ExtraInfo.AreasFileNm);
00343     }
00344   }
00345 
00346   else if(NCALL<0){
00347     /******** BEGIN TIDING UP BEFORE FINAL EXIT*********/
00348     /*Free everything for a clean exit*/
00349     #if CPIXEVERBOSITY > 0
00350     printf("\nCleaning CPIXE Memory usage...\n\n");
00351     #endif
00352 
00353     //Note: Filter.changes=0 implies that freeFilter was not called in last iteration:
00354     if(!Filter.changes)freeFilter(&Filter);
00355 
00356     for(i=1; i<=pSimPar->MaxZinsample ;i++){
00357       if(PresentElems[i]){
00358         free(SPTArray[i].S);
00359         free(SPTArray[i].E);
00360         free(SPTArray[i].dSdE);
00361       }
00362     }
00363     free(SPTArray);
00364     free(PresentElems);
00365     free(EffArray);
00366     free(LinesEnerArray);
00367     free(FCKCoefArray);
00368     free(TotAbsCoefArray);
00369     free(CalcFlags);
00370 
00371 
00372     if(ExtraInfo.WantOutputfile)fclose(outputfile);
00373     freeExtraInfo(&ExtraInfo);
00374 
00375     /******** END TIDING UP BEFORE FINAL EXIT*********/
00376   }
00377 
00378   /*Store Number of Ncall*/
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 

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