Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

cpixe.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Carlos Pascual-Izarra                           *
00003  *   carlos.pascual@itn.pt                                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 
00022  
00023 /**\file cpixe.c
00024 CPIXE is a small program for demonstrating (and testing) the capabilities of the LibCPIXE library.
00025 
00026 See the libcpixe.c file for more information.
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 /**Main program:
00050  * The executable accepts 1 optional command line argument indicating the input
00051  * file name (which defaults to "input.in").
00052  *
00053  * @return EXIT_SUCCESS if normal finishing
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 /** Simulates PIXE yields. See NCALL for mode of opertaion.
00068  * 
00069  * @param NCALL (I) Flag controlling the mode of operation of cpixemain: \n
00070       If NCALL = 0, initialization is done \n
00071       If NCALL > 0, the calculations are done \n
00072       If NCALL < 0, cleaning of memory is performed 
00073  *       
00074  *
00075  *
00076  * @return EXIT_SUCCESS if no error ocurred and "-1" if some error happened. 
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    /*Static variables (Those that should be maintained between calls of CPIXEMAIN()*/
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     /******* BEGIN CALCULATION*********/
00125     /*
00126     These calls are all what is needed to do for each iteration 
00127     (But don't forget also the call to freeReusable() before starting a new iteration)
00128     */
00129     
00130 
00131     //Calculate Filter Transmission if the filter changed
00132     if(Filter.changes){
00133       FilterTrans(pExpPar, XYldArray, TotAbsCoefArray, PresentElems, &Filter);
00134       ///@todo This indicates that the filter is not going to be varied (Provisional)
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));  //Note that XYldSums is 0-initialized
00141     integrate_Simpson(pExpPar, TotAbsCoefArray, FCKCoefArray, XYldArray ,NFoilUsed, &Filter, lyr, XYldSums);
00142 //    SFCorr(pExpPar, TotAbsCoefArray, FCKCoefArray, XYldArray ,NFoilUsed, &Filter, lyr, XYldSums);
00143       /******END CALCULATION**********/
00144     
00145       
00146     #if CPIXEVERBOSITY > 0  
00147     /**********BEGIN DEBUG LINES*******/
00148     /* Just write stuff for debugging*/
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         /*K lines:*/
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         /*L-Lines*/
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         /*M lines:*/
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 //    getchar();
00200     /**********END DEBUG LINES*******/
00201     #endif  
00202     
00203     /**********BEGIN FILE OUTPUT LINES*******/
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           /*K lines:*/
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           /*L-Lines*/
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           /*M lines:*/
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     /**********END FILE OUTPUT LINES*******/
00257       
00258     /*It is VERY important to call freeReusable() before starting a new iteration*/
00259     freeReusable(NFoil,&lyr,&Sample,&XYldSums);
00260 //    printf("\n!!!!!!! %d  %d",NCALL,Filter.changes);
00261     if(Filter.changes)freeFilter(&Filter);
00262     
00263   }
00264   
00265 
00266   else if(NCALL==0){ 
00267     /**********BEGIN INITIALIZATION*********/
00268     /*
00269     This Block is run only when initialization is required.
00270     It reads the databases (Fluorescence, Stoppings,...) and performs some operations on data
00271     other data that never change within a fit.    
00272     */
00273     
00274     //initialize the file name pointers of the ExtraInfo structure
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     //translate Filter definition                       
00286     Filter.changes=1;    //First time at least Filter should be read:
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 //     printf("\n\n!!!!!!!!!!");getchar();
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     //If Sec Fluorescence is required, determine which elements produce fluorescence on which
00324     if(pSimPar->AllowSXFCorr){
00325       createSFCList(pExpPar, PresentElems, XYldArray, TotAbsCoefArray, &nSFCList, &SFCList);
00326 //      prepareSFC(pExpPar, PresentElems);
00327       };
00328     
00329     //safefree((void*)&Sample); //Clear the initialized Matrix Array.
00330     //safefree((void*)&Filter.foil); //Clear the initialized Filter Foil.
00331     /********** END INITIALIZATION *********/
00332 
00333      //Write initialization parameters to output file
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     /******** BEGIN TIDING UP BEFORE FINAL EXIT*********/
00350     /*Free everything for a clean exit*/
00351     #if CPIXEVERBOSITY > 0  
00352     printf("\nCleaning CPIXE Memory usage...\n\n");
00353     #endif
00354     
00355     //Note: Filter.changes=0 implies that freeFilter was not called in last iteration:
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     /******** 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 Fri Jul 15 20:43:49 2005 for LibCPIXE API by  doxygen 1.3.9.1