function []=mpt_maker(filename,mptname,mptmaterial,lambda,lambda_unit,varargin) %mpt_maker Makes optical inputs for GEANT4/GAMOS optical package % []=mpt_maker(filename,mptname,lambda,lambda_unit,varargin) % % The first argument is the name of the .txt file containing the material % properties table outputs. The second argument is the name of the material % property table, the third the material to attach the table to. % The fourth input is the wavelength vector, and five input the wavelength % unit specification. % % Units are defined in the following way '*unit'. For inverse length units, % place a '/' followed by the desired length unit (e.g., */mm, */cm, */um, etc.) % % This varargin supports the following inputs % % ('refractive index',RINDEX vector) - the input following % the 'refractive index' tag must be the RINDEX vector. % % ('absorption',ABSCOEF vector, unit) - the input following % the 'absorption' tag must be the ABSCOEF vector followed by the unit. % % ('rayleigh',RAYLEIGH_SCATCOEF vector, unit) - the input following the 'rayleigh' % tag must be the RAYLEIGH_SCATCOEF vector followed by the unit. % % ('modified rayleigh',RAYLEIGH_MHG_SCATCOEF vector, unit) - the input following % the 'modified rayleigh' tag must be the RAYLEIGH_MHG_SCATCOEF vector followed by % the unit. % % ('henyey-greenstein',MIE_SCATCOEF vector, unit, MIE_GVALUE vector) - the input % following the 'henyey-greenstein' tag must be the MIE_SCATCOEF vector % followed by the unit, followed by the GVALUE vector. % % ('modified henyey-greenstein',MHG_SCATCOEF vector, unit, MHG_GVALUE vector, MHG alpha value) % - the input following the 'modified henyey-greenstein' tag must be the MHG_SCATCOEF % vector followed by the unit, followed by the GVALUE vector, followed by the ALPHAVALUE % vector. % % ('user-scattering',USER_SCAT vector, unit, cos(theta) vector, wavelength vector, wavelength unit, P[wavelength, cos(theta)] vector) % - the input following the 'user-scattering' tag must be the USER_SCATCOEF % vector followed by the unit, followed by the cos(theta), wavelength, and P[wavelength,cos(theta)] 2D phase function. % The phase function will be output in a file phase_function.mptmaterialname.txt to be called into % the simulation.in file. % % ('fluorescence',FLUOR_ABSCOEF vector, unit, FLUOR_EMISSION vector, quantum yield, lifetime, unit) % - the input following the 'fluorescence' tag must be the ABS_COEF % vector followed by the unit, followed by the emission vector, followed by the quantum yield, % followed by the lifetime, followed by the lifetime unit. The emission spectrum % will be output in a file spectrum.txt to be called into the simulation .in file. % % % ('scintillation', SCINTILLATIONYIELD, RESOLUTIONSCALE, YIELDRATIO, % SLOWCOMPONENT vector, FASTCOMPONENT vector, SLOWTIMECONSTANT, unit, % FASTTIMECONSTANT, unit) % - the input following the 'scintllation' tag must be the SCINTILLATION % YIELD which is in units of photons/MeV, RESOLUTIONSCALE which indicates % the standard deviation in number of photons released per a normal % distribution about SCINTILLATIONYIELD, i.e. % sigmaN=RESOLUTIONSCALE*sqrt(Eavg*SCINTILLATIONYIELD) where sigmaN is the % standard deviation in number of emitted photons/MeV and Eavg is the % average scintillation photon energy. YIELDRATIO must then be defined % which scales between 0-1.0 and indicates the proportional ammount of the % fast versus total emission. The two types of emission (slow and fast) % are defined by their emission spectra, SLOWCOMPONENT and FASTCOMPONENT, % as well as their exponential lifetimes, SLOWTIMECONSTANT and % FASTTIMECONSTANT. % % Examples: % % mpt_maker('test','mpt1','G4_WATER',linspace(400,800,100),'*wvnm','refractive index',1.33*ones(100,1),'absorption',0.1*ones(100,1),'*/mm','rayleigh', ... % ones(100,1),'*/mm','fluorescence',normpdf(linspace(400,800,100),500,25),'*/mm',normpdf(linspace(400,800,100),600,25),0.1,10,'*ns'); % % mpt_maker('test','mpt1','G4_WATER',linspace(400,800,100),'*wvnm','refractive % index',1.33*ones(100,1), 'user-scattering', 1.0*ones(100,1),'*/mm', % linspace(-1,1,100), linspace(400,800,100), '*wvnm', ones(100,100)); % % $ Written by Adam Glaser % $ Last modified 2/22/2013 fid=fopen([filename '.txt'],'w'); fprintf(fid,[':MATE_PROPERTIES_TABLE ' mptname]); fprintf(fid,'\n'); energies=lambda; mpt_energies=[':MATEPT_ADD_WAVELENGTHS ' mptname ' ']; if lambda_unit(1)~='*' error('No wavelength unit specified, please specify with *unit.'); end for i=1:length(energies) mpt_energies=[mpt_energies num2str(energies(i), '%3.9f') lambda_unit ' ']; if rem(i,10)==0 mpt_energies=[mpt_energies '\n']; end end fprintf(fid,mpt_energies); fprintf(fid,'\n'); RINDEX_flag=0; for i=1:length(varargin) arg=varargin{i}; if strcmp(varargin(i),'refractive index')==1 mpt_rindex=[':MATEPT_ADD_PROPERTY ' mptname ' RINDEX ']; rindex=varargin{i+1}; if isnumeric(rindex)==0 error('No RINDEX specified.'); end if length(rindex)~=length(energies) error('Number of RINDEX entries does not match number wavelength entries.'); end for j=1:length(rindex) mpt_rindex=[mpt_rindex num2str(rindex(j), '%3.9f') ' ']; if rem(j,10)==0 mpt_rindex=[mpt_rindex '\n']; end end fprintf(fid,mpt_rindex); fprintf(fid,'\n'); RINDEX_flag=1; elseif strcmp(varargin(i),'absorption')==1 mpt_abscoef=[':MATEPT_ADD_PROPERTY ' mptname ' ABSCOEF ']; abscoef=varargin{i+1}+1e-9; if isnumeric(abscoef)==0 error('No ABSCOEF specified.'); end if length(abscoef)~=length(energies) error('Number of ABSCOEF entries does not match number wavelength entries.'); end if length(find(abscoef==0)>0) error('ABSCOEF cannot equal 0.'); end abscoef_unit=varargin{i+2}; if abscoef_unit(1)~='*' error('No ABSCOEF unit specified, please specify with *unit.'); end for j=1:length(abscoef) mpt_abscoef=[mpt_abscoef num2str(abscoef(j), '%3.9f') abscoef_unit ' ']; if rem(j,10)==0 mpt_abscoef=[mpt_abscoef '\n']; end end fprintf(fid,mpt_abscoef); fprintf(fid,'\n'); elseif strcmp(varargin(i),'rayleigh')==1 mpt_rayleigh_scatcoef=[':MATEPT_ADD_PROPERTY ' mptname ' RAYLEIGH_SCATCOEF ']; rayleigh_scatcoef=varargin{i+1}; if isnumeric(rayleigh_scatcoef)==0 error('No RAYLEIGH_SCATCOEF specified.'); end if length(rayleigh_scatcoef)~=length(energies) error('Number of RAYLEIGH_SCATCOEF entries does not match number wavelength entries.'); end if length(find(rayleigh_scatcoef==0)>0) error('RAYLEIGH_SCATCOEF cannot equal 0.'); end rayleigh_scatcoef_unit=varargin{i+2}; if rayleigh_scatcoef_unit(1)~='*' error('No RAYLEIGH_SCATCOEF unit specified, please specify with *unit.'); end for j=1:length(rayleigh_scatcoef) mpt_rayleigh_scatcoef=[mpt_rayleigh_scatcoef num2str(rayleigh_scatcoef(j), '%3.9f') rayleigh_scatcoef_unit ' ']; if rem(j,10)==0 mpt_rayleigh_scatcoef=[mpt_rayleigh_scatcoef '\n']; end end fprintf(fid,mpt_rayleigh_scatcoef); fprintf(fid,'\n'); elseif strcmp(varargin(i),'modified rayleigh')==1 mpt_rayleigh_mhg_scatcoef=[':MATEPT_ADD_PROPERTY ' mptname ' RAYLEIGH_MHG_SCATCOEF ']; rayleigh_mhg_scatcoef=varargin{i+1}; if isnumeric(rayleigh_mhg_scatcoef)==0 error('No RAYLEIGH_MHG_SCATCOEF specified.'); end if length(rayleigh_mhg_scatcoef)~=length(energies) error('Number of RAYLEIGH_MHG_SCATCOEF entries does not match number wavelength entries.'); end if length(find(rayleigh_mhg_scatcoef==0)>0) error('RAYLEIGH_MHG_SCATCOEF cannot equal 0.'); end rayleigh_mhg_scatcoef_unit=varargin{i+2}; if rayleigh_mhg_scatcoef_unit(1)~='*' error('No RAYLEIGH_MHG_SCATCOEF unit specified, please specify with *unit.'); end for j=1:length(rayleigh_mhg_scatcoef) mpt_rayleigh_mhg_scatcoef=[mpt_rayleigh_mhg_scatcoef num2str(rayleigh_mhg_scatcoef(j), '%3.9f') rayleigh_mhg_scatcoef_unit ' ']; if rem(j,10)==0 mpt_rayleigh_mhg_scatcoef=[mpt_rayleigh_mhg_scatcoef '\n']; end end fprintf(fid,mpt_rayleigh_mhg_scatcoef); fprintf(fid,'\n'); elseif strcmp(varargin(i),'henyey-greenstein')==1 mpt_mie_scatcoef=[':MATEPT_ADD_PROPERTY ' mptname ' MIE_SCATCOEF ']; mie_scatcoef=varargin{i+1}; if isnumeric(mie_scatcoef)==0 error('No MIE_SCATCOEF specified.'); end if length(mie_scatcoef)~=length(energies) error('Number of MIE_SCATCOEF entries does not match number wavelength entries.'); end if length(find(mie_scatcoef==0)>0) error('MIE_SCATCOEF cannot equal 0.'); end mie_scatcoef_unit=varargin{i+2}; if mie_scatcoef_unit(1)~='*' error('No MIE_SCATCOEF unit specified, please specify with *unit.'); end for j=1:length(mie_scatcoef) mpt_mie_scatcoef=[mpt_mie_scatcoef num2str(mie_scatcoef(j), '%3.9f') mie_scatcoef_unit ' ']; if rem(j,10)==0 mpt_mie_scatcoef=[mpt_mie_scatcoef '\n']; end end fprintf(fid,mpt_mie_scatcoef); fprintf(fid,'\n'); mpt_mie_gvalue=[':MATEPT_ADD_PROPERTY ' mptname ' MIE_GVALUE ']; mie_gvalue=varargin{i+3}; if isnumeric(mie_gvalue)==0 error('No MIE_GVALUE specified.'); end if length(mie_gvalue)~=length(energies) error('Number of MIE_GVALUE entries does not match number wavelength entries.'); end for j=1:length(mie_gvalue) mpt_mie_gvalue=[mpt_mie_gvalue num2str(mie_gvalue(j), '%3.9f') ' ']; if rem(j,10)==0 mpt_mie_gvalue=[mpt_mie_gvalue '\n']; end end fprintf(fid,mpt_mie_gvalue); fprintf(fid,'\n'); elseif strcmp(varargin(i),'modified henyey-greenstein')==1 mpt_mhg_scatcoef=[':MATEPT_ADD_PROPERTY ' mptname ' MHG_SCATCOEF ']; mhg_scatcoef=varargin{i+1}; if isnumeric(mhg_scatcoef)==0 error('No mhg_scatcoef specified.'); end if length(mhg_scatcoef)~=length(energies) error('Number of MHG_SCATCOEF entries does not match number wavelength entries.'); end if length(find(mhg_scatcoef==0)>0) error('MHG_SCATCOEF cannot equal 0.'); end mhg_scatcoef_unit=varargin{i+2}; if mhg_scatcoef_unit(1)~='*' error('No MHG_SCATCOEF unit specified, please specify with *unit.'); end for j=1:length(mhg_scatcoef) mpt_mhg_scatcoef=[mpt_mhg_scatcoef num2str(mhg_scatcoef(j), '%3.9f') mhg_scatcoef_unit ' ']; if rem(j,10)==0 mpt_mhg_scatcoef=[mpt_mhg_scatcoef '\n']; end end fprintf(fid,mpt_mhg_scatcoef); fprintf(fid,'\n'); mpt_mie_gvalue=[':MATEPT_ADD_PROPERTY ' mptname ' MHG_GVALUE ']; mhg_gvalue=varargin{i+3}; if isnumeric(mhg_gvalue)==0 error('No MHG_GVALUE specified.'); end if length(mhg_gvalue)~=length(energies) error('Number of MHG_GVALUE entries does not match number wavelength entries.'); end for j=1:length(mhg_gvalue) mpt_mhg_gvalue=[mpt_mhg_gvalue num2str(mhg_gvalue(j), '%3.9f') ' ']; if rem(j,10)==0 mpt_mhg_gvalue=[mpt_mhg_gvalue '\n']; end end fprintf(fid,mpt_mhg_gvalue); fprintf(fid,'\n'); mpt_mie_gvalue=[':MATEPT_ADD_PROPERTY ' mptname ' MHG_ALPAVALUE ']; mhg_alphavalue=varargin{i+4}; if isnumeric(mhg_alphavalue)==0 error('No MHG_ALPAVALUE specified.'); end if length(mhg_alphavalue)~=length(energies) error('Number of MHG_ALPAVALUE entries does not match number wavelength entries.'); end for j=1:length(mhg_alphavalue) mpt_mhg_alphavalue=[mpt_mhg_alphavalue num2str(mhg_alphavalue(j), '%3.9f') ' ']; if rem(j,10)==0 mpt_mhg_alphavalue=[mpt_mhg_alphavalue '\n']; end end fprintf(fid,mpt_mhg_alphavalue); fprintf(fid,'\n'); elseif strcmp(varargin(i),'user-scattering')==1 mpt_user_scatcoef=[':MATEPT_ADD_PROPERTY ' mptname ' USER_SCATCOEF ']; user_scatcoef=varargin{i+1}; if isnumeric(user_scatcoef)==0 error('No USER_SCATCOEF specified.'); end if length(user_scatcoef)~=length(energies) error('Number of USER_SCATCOEF entries does not match number wavelength entries.'); end if length(find(user_scatcoef==0)>0) error('USER_SCATCOEF cannot equal 0.'); end user_scatcoef_unit=varargin{i+2}; if user_scatcoef_unit(1)~='*' error('No USER_SCATCOEF unit specified, please specify with *unit.'); end for j=1:length(user_scatcoef) mpt_user_scatcoef=[mpt_user_scatcoef num2str(user_scatcoef(j), '%3.9f') user_scatcoef_unit ' ']; if rem(j,10)==0 mpt_user_scatcoef=[mpt_user_scatcoef '\n']; end end fprintf(fid,mpt_user_scatcoef); fprintf(fid,'\n'); usercostheta=varargin{i+3}; userwavelength=varargin{i+4}; userwavelength_unit=varargin{i+5}; userprob=varargin{i+6}; if user_scatcoef_unit(1)~='*' error('No wavelength unit specified, please specify with *unit.'); end if isnumeric(usercostheta)==0 error('No cos(theta) specified.'); end if isnumeric(userwavelength)==0 error('No wavelength specified.'); end if isnumeric(userprob)==0 error('No probability map specified.'); end if length(size(userprob))~=2 error('Please specify the probability as P[wavelength,cos(theta)] where each row is the cos(theta) probability for a single wavelength.'); end if max(usercostheta)>1 error('Max cos(theta)>1. Please enter in terms of cos(theta) not theta.'); end if max(usercostheta)<-1 error('Max cos(theta)<-1. Please enter in terms of cos(theta) not theta.'); end if length(usercostheta)*length(userwavelength)~=numel(userprob) error('Number of cos(theta) and wavelength entries does not match number P[wavelength,cos(theta)] entries.'); end fid2=fopen(['phase_function.' mptmaterial '.txt'],'w'); fprintf(fid2,':COSTHETA '); for j=1:length(usercostheta) fprintf(fid2,[num2str(usercostheta(j),'%3.9f') ' ']); end fprintf(fid2,'\n'); fprintf(fid2,':WAVELENGTH '); for j=1:length(userwavelength) fprintf(fid2,[num2str(userwavelength(j),'%3.9f') userwavelength_unit ' ']); end fprintf(fid2,'\n'); for j=1:size(userprob,1) costhetaline=[]; for k=1:size(userprob,2) costhetaline=[costhetaline num2str(userprob(j,k),'%3.9f') ' ']; end fprintf(fid2,costhetaline); fprintf(fid2,'\n'); end fprintf(fid2,':ENDFILE'); fclose(fid2); elseif strcmp(varargin(i),'fluorescence')==1 mpt_fluorabscoef=[':MATEPT_ADD_PROPERTY ' mptname ' FLUOR_ABSCOEF ']; fluorabscoef=varargin{i+1}+1e-9; if isnumeric(fluorabscoef)==0 error('No FLUOR_ABSCOEF specified.'); end if length(fluorabscoef)~=length(energies) error('Number of FLUOR_ABSCOEF entries does not match number wavelength entries.'); end if length(find(fluorabscoef==0)>0) error('FLUOR_ABSCOEF cannot equal 0.'); end fluorabscoef_unit=varargin{i+2}; if fluorabscoef_unit(1)~='*' error('No FLUOR_ABSCOEF unit specified, please specify with *unit.'); end for j=1:length(fluorabscoef) mpt_fluorabscoef=[mpt_fluorabscoef num2str(fluorabscoef(j), '%3.9f') fluorabscoef_unit ' ']; if rem(j,10)==0 mpt_fluorabscoef=[mpt_fluorabscoef '\n']; end end fprintf(fid,mpt_fluorabscoef); fprintf(fid,'\n'); mpt_fluoremission=[':MATEPT_ADD_PROPERTY ' mptname ' FLUOR_EMISSION ']; fluoremission=varargin{i+3}; if isnumeric(fluoremission)==0 error('No emission spectrum specified.'); end if length(fluoremission)~=length(energies) error('Number of emission spectrum entries does not match number wavelength entries.'); end for j=1:length(fluoremission) mpt_fluoremission=[mpt_fluoremission num2str(fluoremission(j), '%3.9f') ' ']; if rem(j,10)==0 mpt_fluoremission=[mpt_fluoremission '\n']; end end fprintf(fid,mpt_fluoremission); fprintf(fid,'\n'); fluorqy=varargin{i+4}; if isnumeric(fluorqy)==0 error('No FLUOR_QUANTUMYIELD specified.'); end if length(fluorqy)>1 error('FLUOR_QUANTUMYIELD should be a single number.'); end mpt_fluorqy=[':MATEPT_ADD_CONST_PROPERTY ' mptname ' FLUOR_QUANTUMYIELD ' num2str(fluorqy, '%3.9f')]; fprintf(fid,mpt_fluorqy); fprintf(fid,'\n'); fluort=varargin{i+5}; if isnumeric(fluort)==0 error('No FLUORABS_LIFETIME specified.'); end if length(fluort)>1 error('FLUOR_LIFETIME should be a single number.'); end fluort_unit=varargin{i+6}; if fluort_unit(1)~='*' error('No FLUOR_LIFETIME unit specified, please specify with *unit.'); end mpt_fluort=[':MATEPT_ADD_CONST_PROPERTY ' mptname ' FLUOR_LIFETIME ' num2str(fluort, '%3.9f') fluort_unit]; fprintf(fid,mpt_fluort); fprintf(fid,'\n'); elseif strcmp(varargin(i),'scintillation')==1 yield=varargin{i+1}; if isnumeric(yield)==0 error('No SCINTILLATIONYIELD specified.'); end if length(yield)>1 error('SCINTILLATIONYIELD should be a single number.'); end mpt_yield=[':MATEPT_ADD_CONST_PROPERTY ' mptname ' SCINTILLATIONYIELD ' num2str(yield, '%3.9f') '1/MeV']; fprintf(fid,mpt_yield); fprintf(fid,'\n'); resolution=varargin{i+2}; if isnumeric(resolution)==0 error('No RESOLUTIONSCALE specified.'); end if length(resolution)>1 error('RESOLUTIONSCALE should be a single number.'); end mpt_resolution=[':MATEPT_ADD_CONST_PROPERTY ' mptname ' RESOLUTIONSCALE ' num2str(resolution, '%3.9f')]; fprintf(fid,mpt_resolution); fprintf(fid,'\n'); ratio=varargin{i+3}; if isnumeric(ratio)==0 error('No YIELDRATIO specified.'); end if length(ratio)>1 error('YIELDRATIO should be a single number.'); end mpt_ratio=[':MATEPT_ADD_CONST_PROPERTY ' mptname ' YIELDRATIO ' num2str(ratio, '%3.9f')]; fprintf(fid,mpt_ratio); fprintf(fid,'\n'); mpt_slowcomp=[':MATEPT_ADD_PROPERTY ' mptname ' SLOWCOMPONENT ']; slowcomp=varargin{i+4}; if isnumeric(slowcomp)==0 error('No SLOWCOMPONENT specified.'); end if length(slowcomp)~=length(energies) error('Number of SLOWCOMPONENT entries does not match number wavelength entries.'); end for j=1:length(slowcomp) mpt_slowcomp=[mpt_slowcomp num2str(slowcomp(j), '%3.9f') ' ']; if rem(j,10)==0 mpt_slowcomp=[mpt_slowcomp '\n']; end end fprintf(fid,mpt_slowcomp); fprintf(fid,'\n'); mpt_fastcomp=[':MATEPT_ADD_PROPERTY ' mptname ' FASTCOMPONENT ']; fastcomp=varargin{i+5}; if isnumeric(fastcomp)==0 error('No FASTCOMPONENT specified.'); end if length(fastcomp)~=length(energies) error('Number of FASTCOMPONENT entries does not match number wavelength entries.'); end for j=1:length(fastcomp) mpt_fastcomp=[mpt_fastcomp num2str(fastcomp(j), '%3.9f') ' ']; if rem(j,10)==0 mpt_fastcomp=[mpt_fastcomp '\n']; end end fprintf(fid,mpt_fastcomp); fprintf(fid,'\n'); tslow=varargin{i+6}; if isnumeric(tslow)==0 error('No SLOWTIMECONSTANT specified.'); end if length(tslow)>1 error('SLOWTIMECONSTANT should be a single number.'); end tslow_unit=varargin{i+7}; if tslow_unit(1)~='*' error('No SLOWTIMECONSTANT unit specified, please specify with *unit.'); end mpt_tslow=[':MATEPT_ADD_CONST_PROPERTY ' mptname ' SLOWTIMECONSTANT ' num2str(tslow, '%3.9f') tslow_unit]; fprintf(fid,mpt_tslow); fprintf(fid,'\n'); tfast=varargin{i+8}; if isnumeric(tfast)==0 error('No FASTTIMECONSTANT specified.'); end if length(tfast)>1 error('FASTTIMECONSTANT should be a single number.'); end tfast_unit=varargin{i+9}; if tfast_unit(1)~='*' error('No FASTTIMECONSTANT unit specified, please specify with *unit.'); end mpt_tfast=[':MATEPT_ADD_CONST_PROPERTY ' mptname ' FASTTIMECONSTANT ' num2str(tfast, '%3.9f') tfast_unit]; fprintf(fid,mpt_tfast); fprintf(fid,'\n'); elseif isnumeric(varargin{i})==1 elseif strcmp(arg(1),'*')==1 else error('Unknown material properties table input. Please use refractive index, absorption, rayleigh, modified rayleigh, henyey-greenstien, modified henyey-greenstein, user-scattering, or fluorescence.'); end end if RINDEX_flag==0 error('Error, no refractive index specified.'); end fprintf(fid,[':MATEPT_ATTACH_TO_MATERIAL ' mptname ' ' mptmaterial]); fclose(fid);