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

cpixe_fif.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_fif.c
00024 This is a fortran interface, not properly part of the LibCPIXE lybrary.
00025 
00026 I wrote it to facilitate the integration with the NDF program by N.P. Barradas but it might be useful as well in the case that other people wants to link libCPIXE to a Fortran program.
00027 
00028 I try to maintain cpixe_fif.c with at least the same funcionalities available in the cpixe.c code.
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 //Function prototypes:
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 //Functions:
00065     
00066 /*Translation of sample structures. */
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   /*Initialize  Sample*/
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     //obtain the number of elements for this layer 
00082     for(nelem=0, iels=0; iels<Nels ; iels++) if(pAMNT[iels+Nels*i]>0.) nelem++;  //CHECK THIS!!!
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]);  //NOTE!!: The indexes are switched and "offseted" to deal with the the Fortran convention!!
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];  //The mass passed by NDF is put on the "natural mass"
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     /*Normalize concentrations and calculate the MASS concentration*/
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 //  for(i=0;i<NFoil;i++){
00114 //    printf("\nFoil %d: th=%le nelem=%d",i,(*MatArray)[i].thick,(*MatArray)[i].nfoilelm);
00115 //    for(j=0;j<(*MatArray)[i].nfoilelm;j++){
00116 //      printf("\nTrans: (i=%d , j=%d) (*MatArray)[i].comp.elem[j].Z=%d ",i,j,(*MatArray)[i].comp.elem[j].Z);
00117 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].M=%lf ",(*MatArray)[i].comp.elem[j].M);
00118 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].IM=%lf ",(*MatArray)[i].comp.elem[j].IM);
00119 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].A=%d ",(*MatArray)[i].comp.elem[j].A);
00120 //    printf("\nTrans:      X=%lf   xn=%lf   w=%lf",(*MatArray)[i].comp.X[j],(*MatArray)[i].comp.xn[j],(*MatArray)[i].comp.w[j]);
00121 //    }
00122 //  }
00123   return(0);
00124 
00125 }
00126 
00127 /*Alternative translation of sample structures. */
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   /*Initialize  Sample*/
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     //obtain the number of elements for this layer 
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]);  //NOTE!!: The indexes are switched and "offseted" to deal with the the Fortran convention!!
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];  //The mass passed by NDF is put on the "natural mass"
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     /*Normalize concentrations and calculate the MASS concentration*/
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 //  for(i=0;i<NFoil;i++){
00179 //    printf("\nFoil %d: th=%le nelem=%d",i,(*MatArray)[i].thick,(*MatArray)[i].nfoilelm);
00180 //    for(j=0;j<(*MatArray)[i].nfoilelm;j++){
00181 //      printf("\nTrans: (i=%d , j=%d) (*MatArray)[i].comp.elem[j].Z=%d ",i,j,(*MatArray)[i].comp.elem[j].Z);
00182 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].M=%lf ",(*MatArray)[i].comp.elem[j].M);
00183 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].IM=%lf ",(*MatArray)[i].comp.elem[j].IM);
00184 //    printf("\nTrans:             (*MatArray)[i].comp.elem[j].A=%d ",(*MatArray)[i].comp.elem[j].A);
00185 //    printf("\nTrans:      X=%lf   xn=%lf   w=%lf",(*MatArray)[i].comp.X[j],(*MatArray)[i].comp.xn[j],(*MatArray)[i].comp.w[j]);
00186 //    }
00187 //  }
00188   return(0);
00189 
00190 
00191 }
00192 
00193 
00194 /*Copies the results of XYldSums to the array pRESULTS */
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];  ///IMPORTANT!!!!!!: Each isotope gets the FULL number of counts for the element!!!
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   /*Static variables (Those that should be maintained between calls of CPIXEMAIN()*/
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   /*Extern Variables (defined in the CPIXEIF Fortran module)*/
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;     //these are DEC pointers to ints
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 ;  //these are DEC pointers to floats
00272   extern int CPIXEIF_mp_POINT_PIXERESULTS; //This is a DEC pointer to a CPIXERESULT
00273   extern int CPIXEIF_mp_POINT_PIXECALC;
00274 
00275   int *pFOILZ, *pFILTERZ, *pFILTERNELS; //Those are the "true" C pointers to ints
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   //Transforming fortran (DEC) pointers (which are integers) into true C pointers
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 // printf("\nFOILTHICK->%d    NELEM-->%d\n",pFOILTHICK,pNELEM);
00299 //  printf("\nNCALL=%d\t Z=%d\n",NCALL,CPIXEIF_mp_EXPPAR.ion.Z);
00300 //  for(i=0;i<=10;i++)for(j=0;j<3;j++)
00301 //    printf("\nF.coef[%d][%d]=%lf",i,j,CPIXEIF_mp_SIMPAR.Filter.coef[i][j]);
00302 //  for(j=0;j<3;j++)printf("\nF.h[%d]=%lf\n",j,CPIXEIF_mp_SIMPAR.Filter.h[j]);
00303 //
00304 //  printf("\nCPIXE: '%s'",CPIXEIF_mp_CALFILENM);
00305 //  printf("\nCPIXE: 'MAXELEM=%d'",CPIXEIF_mp_NELS);
00306 //
00307 //  for(i=0 ; i<=3; i++) printf("\nCPIXE: '%s'",CPIXEIF_mp_CALFILENM);
00308 //   
00309 //      /*K lines:*/
00310 //      for (j = 1; j <= 3; j++)  printf("\n%10s %1.1lf ", LineNm[1][j], pRESULTS->K_[j] );
00311 //      /*L-Lines*/
00312 //      for (j = 1; j <= 3; j++) 
00313 //        for (l = 1; l <= 3; l++)
00314 //          printf("\n%10s %1.1lf ", LineNm[j+1][l], pRESULTS->L_[j][l] );
00315 //      /*M lines:*/
00316 //      for (j = 1; j <= 3; j++)  printf("\n%10s %1.1lf ", LineNm[5][j], pRESULTS->M_[j] );
00317 // printf("\n!!!!!!!!!!!\n");
00318 // getchar();
00319  
00320   
00321   
00322   /*Note: The following initialization needs to be done each time the function is called*/
00323   MatArray=NULL;
00324   lyr=NULL;
00325   
00326   //make a convenience pointer to the EXPPAR and SIMPAR structures defined in the fortran module
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     /******* BEGIN CALCULATION*********/
00333     /*
00334     These calls are all what is needed to do for each iteration 
00335     (But don't forget also the call to freeReusable() before starting a new iteration)
00336     */
00337     
00338     //translate the sample definition made in NDF to that used in CPIXE
00339     NFoil=CPIXEIF_mp_CPNLAY;
00340     translatesample(NFoil,CPIXEIF_mp_CPNELS,pFOILZ,pFOILM,pAMNT,pFOILTHICK,&pSimPar->MaxZinsample,&MatArray);
00341 
00342   //translate Filter definition
00343     if(Filter.changes){
00344       Filter.nlyr=CPIXEIF_mp_NLAYFILTER;
00345       Filter.geomcorr=1; //filter must be perpendicular
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       //the Filter.changes variable is set externally
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));  //Note that XYldSums is 0-initialized
00358     integrate_Simpson(pExpPar, TotAbsCoefArray, FCKCoefArray, XYldArray ,NFoilUsed, &Filter, lyr, XYldSums);
00359     translateresultsNDF(CPIXEIF_mp_CPNELS, pFOILZ, XYldSums, pRESULTS);
00360       /******END CALCULATION**********/
00361     
00362       
00363     #if CPIXEVERBOSITY > 0  
00364     /**********BEGIN DEBUG LINES*******/
00365     /* Just write stuff for debugging*/
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         /*K lines:*/
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         /*L-Lines*/
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         /*M lines:*/
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 //    getchar();
00411     /**********END DEBUG LINES*******/
00412     #endif  
00413       
00414     /*It is VERY important to call freeReusable() before starting a new iteration*/
00415     freeReusable(NFoil,&lyr,&MatArray,&XYldSums);
00416 //    printf("\n!!!!!!! %d  %d",NCALL,Filter.changes);
00417     if(Filter.changes)freeFilter(&Filter);
00418     
00419   }
00420   
00421   else if(NCALL==0){ 
00422     /**********BEGIN INITIALIZATION*********/
00423     /*
00424     This Block is run only when initialization is required.
00425     It reads the databases (Fluorescence, Stoppings,...) and performs some operations on data
00426     other data that never change within a fit.    
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     //translate the sample definition made in NDF to that used in CPIXE
00444     NFoil=CPIXEIF_mp_CPNLAY;
00445     translatesample(NFoil,CPIXEIF_mp_CPNELS,pFOILZ,pFOILM,pAMNT,pFOILTHICK,&pSimPar->MaxZinsample,&MatArray);
00446 
00447     //translate Filter definition
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;    //First time at least Filter should be read:
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); //Clear the initialized Matrix Array.
00462     safefree((void*)&Filter.foil); //Clear the initialized Filter Foil.
00463     /********** END INITIALIZATION *********/
00464   }
00465 
00466   else if(NCALL<0){
00467     /******** BEGIN TIDING UP BEFORE FINAL EXIT*********/
00468     /*Free everything for a clean exit*/
00469     #if CPIXEVERBOSITY > 0  
00470     printf("\nDEBUG: Cleaning CPIXE Memory usage...\n\n");
00471     #endif
00472     
00473     //Note: Filter.changes=0 implies that freeFilter was not called in last iteration:
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     /******** END TIDING UP BEFORE FINAL EXIT*********/
00490   }   
00491   
00492 //  printf("\n\nDEBUG: End of program reached\n\n");getchar();
00493   
00494   /*Store Number of Ncall*/
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 }

Generated on Fri Jul 15 20:43:49 2005 for LibCPIXE API by  doxygen 1.3.9.1