Révision 209 TrouNoir/TrouNoir.py

TrouNoir.py (revision 209)
43 43
KERNEL_CODE=string.Template("""
44 44

  
45 45
#define PI (float)3.14159265359
46
#define nbr 200
46
#define nbr 256
47 47

  
48 48
float atanp(float x,float y)
49 49
{
......
107 107
{
108 108
  float flx;
109 109

  
110
  flx=pow(r/m,q)*pow(rf,4)*b;
110
  flx=exp(q*log(r/m))*pow(rf,4)*b*db*h;
111 111
  return(flx);
112 112
}
113 113

  
114
float spectre_cn(float rf,float b,float db,float h,float r,float m,float bss)
114
float spectre_cn(float rf32,float b32,float db32,
115
		 float h32,float r32,float m32,float bss32)
115 116
{
116
  double flx;
117
  double nu_rec,nu_em,qu,v,temp,temp_em,flux_int,m_point,planck,c,k,psc2,psk;
117
  
118
#define MYFLOAT double
119

  
120
  MYFLOAT rf=(MYFLOAT)(rf32);
121
  MYFLOAT b=(MYFLOAT)(b32);
122
  MYFLOAT db=(MYFLOAT)(db32);
123
  MYFLOAT h=(MYFLOAT)(h32);
124
  MYFLOAT r=(MYFLOAT)(r32);
125
  MYFLOAT m=(MYFLOAT)(m32);
126
  MYFLOAT bss=(MYFLOAT)(bss32);
127

  
128
  MYFLOAT flx;
129
  MYFLOAT nu_rec,nu_em,qu,temp_em,flux_int;
118 130
  int fi,posfreq;
119 131

  
120
  planck=6.62e-34;
121
  k=1.38e-23;
122
  psk=5.38e-11;
123
  psc2=7.35e-51;
124
  temp=3.e7;
125
  m_point=1.e14;
126
  v=1.-3./r;
132
#define planck 6.62e-34
133
#define k 1.38e-23
134
#define c2 9.e16
135
#define temp 3.e7
136
#define m_point 1.
127 137

  
128
  qu=1./sqrt(1.-3./r)/sqrt(r)*(sqrt(r)-sqrt(6.)+sqrt(3.)/2.*log((sqrt(r)+sqrt(3.))/(sqrt(r)-sqrt(3.))*(sqrt(6.)-sqrt(3.))/(sqrt(6.)+sqrt(3.))));
138
#define lplanck (log(6.62)-34.*log(10.))
139
#define lk (log(1.38)-23.*log(10.))
140
#define lc2 (log(9.)+16.*log(10.))
129 141

  
130
  temp_em=temp*sqrt(m)*exp(0.25*log(m_point))*exp(-0.75*log(r))*exp(-0.125*log(v))*exp(0.25*log(qu));
142
  MYFLOAT v=1.-3./r;
131 143

  
132
  flux_int=0;
133
  flx=0;
144
  qu=1./sqrt((1.-3./r)*r)*(sqrt(r)-sqrt(6.)+sqrt(3.)/2.*log((sqrt(r)+sqrt(3.))/(sqrt(r)-sqrt(3.))* 0.17157287525380988 ));
134 145

  
146
  temp_em=temp*sqrt(m)*exp(0.25*log(m_point)-0.75*log(r)-0.125*log(v)+0.25*log(fabs(qu)));
147

  
148
  flux_int=0.;
149
  flx=0.;
150

  
135 151
  for (fi=0;fi<nbr;fi++)
136 152
    {
137
      nu_em=bss*(float)fi/(float)nbr;
138
      nu_rec=nu_em*rf; 
139
      posfreq=(int)(1./bss*nu_rec*(float)nbr);
153
      nu_em=bss*(MYFLOAT)fi/(MYFLOAT)nbr;
154
      nu_rec=nu_em*rf;
155
      posfreq=(int)(nu_rec*(MYFLOAT)nbr/bss);
140 156
      if ((posfreq>0)&&(posfreq<nbr))
141 157
	{
142
	  //flux_int=2*planck/9.e16f*pow(nu_em,3)/(exp(planck*nu_em/k/temp_em)-1.)*pow(rf,3)*b*db*h;
143
	  //flux_int=pow(nu_em,3)/(exp(planck*nu_em/k/temp_em)-1.)*pow(rf,3)*b;
144
	  flux_int=2.*psc2*pow(nu_em,3)/(exp(psk*nu_em/temp_em)-1.)*pow(rf,3)*b;
158
          // Initial version
159
	  flux_int=2.*planck/c2*pow(nu_em,3)/(exp(planck*nu_em/(k*temp_em))-1.);
160
          // Version with log used
161
	  //flux_int=2.*exp(lplanck-lc2+3.*log(nu_em))/(exp(exp(lplanck-lk+log(nu_em/temp_em)))-1.);
162
	  flux_int*=pow(rf,3)*b*db*h;
145 163
	  flx+=flux_int;
146 164
	}
147 165
    }
148
  //return(pow(rf,3)*b*temp_em);
149
  return((float)flx);
150
  return(flx);
166

  
167
  return((float)(flx));
151 168
}
152 169

  
153 170
void impact(float phi,float r,float b,float tho,float m,
......
161 178

  
162 179
  if (raie==0)
163 180
    {
181
      bss=1.e19;
182
      flx=spectre_cn(rf,b,db,h,r,m,bss);
183
    }
184
  else
185
    { 
164 186
      bss=2.;
165 187
      flx=spectre(rf,q,b,db,h,r,m,bss);
166 188
    }
167
  else
168
    {
169
      bss=1.e6;
170
      flx=spectre_cn(rf,b,db,h,r,m,bss);
171
    }
172 189
  
173 190
  *zp=1./rf;
174 191
  *fp=flx;
......
178 195
__kernel void EachPixel(__global float *zImage,__global float *fImage,
179 196
                        float Mass,float InternalRadius,
180 197
                        float ExternalRadius,float Angle,
181
                        float ObserverDistance,
182
                        int BlackBody,int AngularCamera)
198
                        int Line)
183 199
{
184 200
   uint xi=(uint)get_global_id(0);
185 201
   uint yi=(uint)get_global_id(1);
......
188 204

  
189 205
   // Perform trajectory for each pixel, exit on hit
190 206

  
191
  float m,rs,ri,re,tho,ro;
192
  int q,dist,raie;
207
  float m,rs,ri,re,tho;
208
  int q,raie;
193 209

  
194 210
  m=Mass;
195 211
  rs=2.*m;
196 212
  ri=InternalRadius;
197 213
  re=ExternalRadius;
198
  ro=ObserverDistance;
199 214
  tho=Angle;
200 215
  q=-2;
201
  dist=AngularCamera;
202
  raie=BlackBody;
216
  raie=Line;
203 217

  
204 218
  float d,bmx,db,b,h;
205 219
  float rp[2048];
......
225 239
  // impact parameter
226 240
  b=sqrt(x*x+y*y)*(float)2.e0f/(float)sizex*bmx;
227 241
  // step of impact parameter;
228
  db=bmx/(float)(sizex/2);
242
//  db=bmx/(float)(sizex/2);
243
  db=bmx/(float)(sizex);
229 244

  
230
  if (dist==1) 
231
  {
232
     up=0.;
233
     vp=1.;
234
  }
235
  else 
236
  {
237
     up=sin(thi);
238
     vp=cos(thi);
239
  }
245
  up=0.;
246
  vp=1.;
240 247
      
241 248
  pp=0.;
242 249
  nh=0;
......
279 286
                    uint ImpactParameter,uint TrackPoints,
280 287
                    float Mass,float InternalRadius,
281 288
                    float ExternalRadius,float Angle,
282
                    float ObserverDistance,
283
                    int BlackBody,int AngularCamera)
289
                    int Line)
284 290
{
285 291
   uint xi=(uint)get_global_id(0);
286 292
   uint yi=(uint)get_global_id(1);
......
289 295

  
290 296
   // Perform trajectory for each pixel
291 297

  
292
  float m,rs,ri,re,tho,ro;
293
  int q,dist,raie;
298
  float m,rs,ri,re,tho;
299
  int q,raie;
294 300

  
295 301
  m=Mass;
296 302
  rs=2.*m;
297 303
  ri=InternalRadius;
298 304
  re=ExternalRadius;
299
  ro=ObserverDistance;
300 305
  tho=Angle;
301 306
  q=-2;
302
  dist=AngularCamera;
303
  raie=BlackBody;
307
  raie=Line;
304 308

  
305 309
  float d,bmx,db,b,h;
306 310
  float phi,thi,phd,php,nr,r;
307 311
  int nh;
308 312
  float zp=0,fp=0;
309 313

  
310
  // Autosize for image, 25% greater than externa radius
314
  // Autosize for image, 25% greater than external radius
311 315
  bmx=1.25*re;
312 316

  
313 317
  // Angular step of integration
314 318
  h=4.e0f*PI/(float)TrackPoints;
315 319

  
316 320
  // Step of Impact Parameter
317
  db=bmx/(float)ImpactParameter;
321
  db=bmx/(2.e0*(float)ImpactParameter);
318 322

  
319 323
  // set origin as center of image
320 324
  float x=(float)xi-(float)(sizex/2)+(float)5e-1f;
......
371 375
                     int TrackPoints,
372 376
                     float Mass,float InternalRadius,
373 377
                     float ExternalRadius,float Angle,
374
                     float ObserverDistance,
375
                     int BlackBody,int AngularCamera)
378
                     int Line)
376 379
{
377 380
   // Integer Impact Parameter ID 
378 381
   int bi=get_global_id(0);
......
385 388

  
386 389
   // Perform trajectory for each pixel
387 390

  
388
  float m,rs,ri,re,tho,ro;
389
  int q,dist,raie;
391
  float m,rs,ri,re,tho;
392
  int q,raie;
390 393

  
391 394
  m=Mass;
392 395
  rs=2.*m;
393 396
  ri=InternalRadius;
394 397
  re=ExternalRadius;
395
  ro=ObserverDistance;
396 398
  tho=Angle;
397
  q=-2;
398
  dist=AngularCamera;
399
  raie=BlackBody;
399
  raie=Line;
400 400

  
401 401
  float bmx,db,b,h;
402 402
  float phi,thi,phd;
......
411 411

  
412 412
  // impact parameter
413 413
  b=(float)bi/(float)bmaxi*bmx;
414
  db=bmx/(float)bmaxi;
414
  db=bmx/(2.e0*(float)bmaxi);
415 415

  
416 416
  phi=2.*PI/(float)imx*(float)i;
417 417
  phd=atanp(cos(phi)*sin(tho),cos(tho));
......
456 456
                         __global int *IdLast,int TrackPoints,
457 457
                         float Mass,float InternalRadius,
458 458
                         float ExternalRadius,float Angle,
459
                         float ObserverDistance,
460
                         int BlackBody,int AngularCamera)
459
                         int Line)
461 460
{
462 461
  // Integer Impact Parameter ID 
463 462
  int bi=get_global_id(0);
......
466 465

  
467 466
  // Perform trajectory for each pixel
468 467

  
469
  float m,rs,ri,re,tho,ro;
470
  int dist,raie,q;
468
  float m,rs,ri,re,tho;
469
  int raie,q;
471 470

  
472 471
  m=Mass;
473 472
  rs=2.*m;
474 473
  ri=InternalRadius;
475 474
  re=ExternalRadius;
476
  ro=ObserverDistance;
477 475
  tho=Angle;
478 476
  q=-2;
479
  dist=AngularCamera;
480
  raie=BlackBody;
477
  raie=Line;
481 478

  
482 479
  float d,bmx,db,b,h;
483 480
  float phi,thi,phd,php,nr,r;
......
495 492

  
496 493
  float up,vp,pp,us,vs,ps;
497 494

  
498
  if (dist==1) 
499
  {
500
     up=0.;
501
     vp=1.;
502
  }
503
  else 
504
  {
505
     thi=asin(b/ro);
506
     up=sin(thi);
507
     vp=cos(thi);
508
  }
495
  up=0.;
496
  vp=1.;
509 497
      
510 498
  pp=0.;
511 499
  nh=0;
......
559 547
    InternalRadius=InputCL['InternalRadius']
560 548
    ExternalRadius=InputCL['ExternalRadius']
561 549
    Angle=InputCL['Angle']
562
    ObserverDistance=InputCL['ObserverDistance']
563 550
    Method=InputCL['Method']
564 551
    TrackPoints=InputCL['TrackPoints']
565 552

  
566 553
    if InputCL['BlackBody']:
567
        BlackBody=0
554
        # Spectrum is Black Body one
555
        Line=0
568 556
    else:
569
        BlackBody=1
557
        # Spectrum is Monochromatic Line one
558
        Line=1
570 559

  
571
    if InputCL['AngularCamera']:
572
        AngularCamera=1
573
    else:
574
        AngularCamera=0
575
        
576 560
    Trajectories=numpy.zeros((InputCL['Size']/2,InputCL['TrackPoints']),
577 561
                              dtype=numpy.float32)
578 562
    IdLast=numpy.zeros(InputCL['Size']/2,dtype=numpy.int32)
......
606 590
    TrajectoriesCL = cl.Buffer(ctx, mf.WRITE_ONLY | mf.COPY_HOST_PTR, hostbuf=Trajectories)
607 591
    zImageCL = cl.Buffer(ctx, mf.WRITE_ONLY | mf.COPY_HOST_PTR, hostbuf=zImage)
608 592
    fImageCL = cl.Buffer(ctx, mf.WRITE_ONLY | mf.COPY_HOST_PTR, hostbuf=fImage)
609
    fImageCL = cl.Buffer(ctx, mf.WRITE_ONLY | mf.COPY_HOST_PTR, hostbuf=fImage)
610 593
    IdLastCL = cl.Buffer(ctx, mf.WRITE_ONLY | mf.COPY_HOST_PTR, hostbuf=IdLast)
611 594
    
612 595
    start_time=time.time() 
......
619 602
                                       numpy.float32(InternalRadius),
620 603
                                       numpy.float32(ExternalRadius),
621 604
                                       numpy.float32(Angle),
622
                                       numpy.float32(ObserverDistance),
623
                                       numpy.int32(BlackBody),
624
                                       numpy.int32(AngularCamera))
605
                                       numpy.int32(Line))
625 606
        CLLaunch.wait()
626 607
    elif Method=='TrajectoCircle':
627 608
        CLLaunch=BlackHoleCL.Trajectory(queue,(Trajectories.shape[0],),
......
631 612
                                        numpy.float32(InternalRadius),
632 613
                                        numpy.float32(ExternalRadius),
633 614
                                        numpy.float32(Angle),
634
                                        numpy.float32(ObserverDistance),
635
                                        numpy.int32(BlackBody),
636
                                        numpy.int32(AngularCamera))
615
                                        numpy.int32(Line))
637 616
        
638 617
        CLLaunch=BlackHoleCL.Circle(queue,(Trajectories.shape[0],
639 618
                                           zImage.shape[0]*4),None,
......
644 623
                                    numpy.float32(InternalRadius),
645 624
                                    numpy.float32(ExternalRadius),
646 625
                                    numpy.float32(Angle),
647
                                    numpy.float32(ObserverDistance),
648
                                    numpy.int32(BlackBody),
649
                                    numpy.int32(AngularCamera))
626
                                    numpy.int32(Line))
650 627
        CLLaunch.wait()
651 628
    else:
652 629
        CLLaunch=BlackHoleCL.Trajectory(queue,(Trajectories.shape[0],),
......
656 633
                                        numpy.float32(InternalRadius),
657 634
                                        numpy.float32(ExternalRadius),
658 635
                                        numpy.float32(Angle),
659
                                        numpy.float32(ObserverDistance),
660
                                        numpy.int32(BlackBody),
661
                                        numpy.int32(AngularCamera))
636
                                        numpy.int32(Line))
662 637
        
663 638
        CLLaunch=BlackHoleCL.Pixel(queue,(zImage.shape[0],
664 639
                                                  zImage.shape[1]),None,
......
669 644
                                        numpy.float32(InternalRadius),
670 645
                                        numpy.float32(ExternalRadius),
671 646
                                        numpy.float32(Angle),
672
                                        numpy.float32(ObserverDistance),
673
                                        numpy.int32(BlackBody),
674
                                        numpy.int32(AngularCamera))
647
                                        numpy.int32(Line))
675 648
        CLLaunch.wait()
676 649
    
677 650
    elapsed = time.time()-start_time
......
682 655
    cl.enqueue_copy(queue,Trajectories,TrajectoriesCL).wait()
683 656
    cl.enqueue_copy(queue,IdLast,IdLastCL).wait()
684 657
    
685
    print(zImage.max())
686
    print(fImage.max())
658
    print(zImage.max(),numpy.where(zImage[:,:]==zImage.max()))
659
    print(fImage.max(),numpy.where(fImage[:,:]==fImage.max()))
687 660
    zImageCL.release()
688 661
    fImageCL.release()
689 662

  
......
701 674
    #
702 675
    ExternalRadius=12.
703 676
    #
704
    ObserverDistance=100.
705 677
    # Angle with normal to disc 10 degrees
706 678
    Angle = numpy.pi/180.*(90.-10.)
707 679
    # Radiation of disc : BlackBody or Monochromatic
708
    BlackBody = True
709
    # Camera : Angular Camera or plate with the size of camera
710
    AngularCamera = True
680
    BlackBody = False
711 681
    # Size of image
712 682
    Size=256
713 683
    # Variable Type
......
715 685
    # ?
716 686
    q=-2
717 687
    # Method of resolution
718
    Method='Trajecto'
688
    Method='TrajectoPixel'
719 689
          
720
    HowToUse='%s -h [Help] -b [BlackBodyEmission] -c [AngularCamera] -s <SizeInPixels> -m <Mass> -i <DiscInternalRadius> -x <DiscExternalRadius> -o <ObservatorDistance> -a <AngleAboveDisc> -d <DeviceId> -g <CUDA/OpenCL> -t <EachPixel/TrajectoCircle/TrajectoPixel> -v <FP32/FP64>'
690
    HowToUse='%s -h [Help] -b [BlackBodyEmission] -s <SizeInPixels> -m <Mass> -i <DiscInternalRadius> -x <DiscExternalRadius> -a <AngleAboveDisc> -d <DeviceId> -g <CUDA/OpenCL> -t <EachPixel/TrajectoCircle/TrajectoPixel> -v <FP32/FP64>'
721 691

  
722 692
    try:
723
        opts, args = getopt.getopt(sys.argv[1:],"hbcs:m:i:x:o:a:d:g:v:t:",["blackbody","camera","size=","mass=","internal=","external=","observer=","angle=","device=","gpustyle=","variabletype=","method="])
693
        opts, args = getopt.getopt(sys.argv[1:],"hbs:m:i:x:a:d:g:v:t:",["blackbody","camera","size=","mass=","internal=","external=","angle=","device=","gpustyle=","variabletype=","method="])
724 694
    except getopt.GetoptError:
725 695
        print(HowToUse % sys.argv[0])
726 696
        sys.exit(2)
......
777 747
            InternalRadius = float(arg)
778 748
        elif opt in ("-e", "--external"):
779 749
            ExternalRadius = float(arg)
780
        elif opt in ("-o", "--observer"):
781
            ObserverDistance = float(arg)
782 750
        elif opt in ("-a", "--angle"):
783 751
            Angle = numpy.pi/180.*(90.-float(arg))
784 752
        elif opt in ("-b", "--blackbody"):
785 753
            BlackBody = True
786
        elif opt in ("-c", "--camera"):
787
            AngularCamera = True
788 754
        elif opt in ("-t", "--method"):
789 755
            Method = arg
790 756

  
......
795 761
    print("Mass : %f" % Mass)
796 762
    print("Internal Radius : %f" % InternalRadius)
797 763
    print("External Radius : %f" % ExternalRadius)
798
    print("Observer Distance : %f" % ObserverDistance)
799 764
    print("Angle with normal of (in radians) : %f" % Angle)
800 765
    print("Black Body Disc Emission (monochromatic instead) : %s" % BlackBody)
801
    print("Angular Camera (dimension of object instead) : %s" % AngularCamera)
802 766
    print("Method of resolution : %s" % Method)
803 767

  
804 768
    if GpuStyle=='CUDA':
......
851 815
    InputCL['Mass']=Mass
852 816
    InputCL['InternalRadius']=InternalRadius
853 817
    InputCL['ExternalRadius']=ExternalRadius
854
    InputCL['ObserverDistance']=ObserverDistance
855 818
    InputCL['Angle']=Angle
856 819
    InputCL['BlackBody']=BlackBody
857
    InputCL['AngularCamera']=AngularCamera
858 820
    InputCL['Method']=Method
859 821
    InputCL['TrackPoints']=TrackPoints
860 822
    

Formats disponibles : Unified diff