00001
00002
00003
00004 #include <cmath>
00005 #include <iostream>
00006 #include "ExtraGalacticDiffuse.h"
00007 #include "time.h"
00008 #include<vector>
00009 #include "SpectrumFactory.h"
00010
00011 static SpectrumFactory<ExtraGalacticDiffuse> factory;
00012
00013
00014 long double ExtraGalacticDiffuse::pofi(long double intensity){
00015 long double p;
00016
00017 if(intensity>=pow(10.0,-7)){
00018 p=2.49555*pow(10.0,-13)*pow(intensity,-2.5);
00019 }else{
00020 p=2.49555*pow(10.0,-13)*pow(intensity,-2.5-0.05*(7.0+(log(intensity)/log(10.0))));
00021 }
00022
00023
00024 return p;
00025 }
00026
00027
00028
00029
00030 long double ExtraGalacticDiffuse::random(){
00031 long double r=rand()/32767.0;
00032 return r;
00033 }
00034
00035
00036 ExtraGalacticDiffuse::DELTAX ExtraGalacticDiffuse::gaussianspread(){
00037 DELTAX spread;
00038 long double x,y,theta,phi;
00039
00040 theta=log(1.0/random());
00041 phi=random()*2.0*3.141592653589793238;
00042
00043 x=theta*sin(phi);
00044 y=theta*cos(phi);
00045
00046 spread.x=x;
00047 spread.y=y;
00048 return spread;
00049 }
00050
00051
00052 void ExtraGalacticDiffuse::addtophotons(long double x,long double y){
00053 int n;
00054 DELTAX smear;
00055
00056 for(n=0 ; list[n].x!='\0' ; n++){
00057 }
00058 smear=gaussianspread();
00059 list[n].x=x+smear.x;
00060 list[n+1].x='\0';
00061 list[n].y=y+smear.y;
00062 list[n+1].y='\0';
00063
00064 }
00065
00066
00067 void ExtraGalacticDiffuse::addtoctlg(long double x,long double y,long double intsty){
00068 int n;
00069 for(n=0 ; ctlg[n].x!='\0' ; n++){
00070 }
00071 ctlg[n].x=x;
00072 ctlg[n+1].x='\0';
00073 ctlg[n].y=y;
00074 ctlg[n].intensity=intsty;
00075 }
00076
00077 void ExtraGalacticDiffuse::findandaddnew(){
00078
00079 long double x=random()*skysizex;
00080 long double y=random()*skysizey;
00081 long double prob=random();
00082
00083 long double dx=0.0000001;
00084 long double i=lowthresholdofintensity;
00085
00086 while(prob > 0 && i<highthresholdofintensity){
00087 dx=0.01*pow(10.0,log10(i));
00088 prob-=(dx)*pofi(i);
00089 i+=dx;
00090
00091 }
00092
00093 addtophotons(x,y);
00094 addtoctlg(x,y,i-dx);
00095 RemInt-=i;
00096 }
00097
00098 void ExtraGalacticDiffuse::sendphotonfromcatalog(){
00099 int i=0;
00100 long double num;
00101 long double catprob=1.0-NewProb;
00102
00103 num=random()*catprob;
00104 for(i=0 ; num>0 ; i++){
00105 num-=ctlg[i].intensity/(TotInt-RemInt);}
00106 addtophotons(ctlg[i-1].x,ctlg[i-1].y);
00107 }
00108
00109
00110 ExtraGalacticDiffuse::PHOTON *ExtraGalacticDiffuse::create()
00111 {
00112 long double rand;
00113 PHOTON *pointpho=NULL;
00114
00115 int g=0;
00116
00117 srand((unsigned)time(NULL));
00118
00119
00120 while(RemInt>=threshold || g<maxnumofphotons){
00121
00122
00123 g++;
00124 rand=random();
00125 NewProb=RemInt/TotInt;
00126
00127 if(rand<=NewProb){
00128 findandaddnew();
00129
00130 }
00131 else{sendphotonfromcatalog();
00132
00133 }
00134 }
00135
00136
00137 return pointpho;
00138 };
00139
00140
00141
00142
00143
00144 ExtraGalacticDiffuse::ExtraGalacticDiffuse(const char* name,float Emin, float Emax, float index){
00145 TotInt=.0000976759*multiplierduetosizeofsky;
00146 RemInt=TotInt;
00147 list[0].x='\0';
00148 list[0].y='\0';
00149 ctlg[0].x='\0';
00150 ctlg[0].y='\0';
00151
00152
00153 SimpleSpectrum(name, Emin, Emax, index);
00154
00155
00156 const ExtraGalacticDiffuse::PHOTON* photonlist=ExtraGalacticDiffuse::create();
00157
00158
00159 std::vector<std::pair<double,double> > listofphotons;
00160
00161
00162
00163
00164 int lenlst;
00165
00166 std::pair<double,double> holder;
00167
00168
00169 for(lenlst=0 ; list[lenlst].x!='\0' ; lenlst++){
00170 holder.first=list[lenlst].x;
00171 holder.second=list[lenlst].y;
00172 listofphotons.push_back (holder);
00173 }
00174
00175
00176 srcpnt = listofphotons.begin();
00177
00178
00179 };
00180
00181
00182
00183 ExtraGalacticDiffuse::~ExtraGalacticDiffuse(){
00184 };
00185
00186
00187 std::pair<double,double> ExtraGalacticDiffuse::dir(double e){
00188
00189 std::pair<double,double> one;
00190 std::pair<double,double> coords;
00191
00192 one=*srcpnt;
00193
00194 coords.first=one.first;
00195 coords.second=one.second;
00196 srcpnt++;
00197 return coords;
00198 }
00199
00200
00201