Statistiques
| Révision :

root / TrouNoir / trou_noir.c @ 230

Historique | Voir | Annoter | Télécharger (9,98 ko)

1
/*
2
        Programme original realise en Fortran 77 en mars 1994
3
        pour les Travaux Pratiques de Modelisation Numerique
4
        DEA d'astrophysique et techniques spatiales de Meudon
5

6
                par Herve Aussel et Emmanuel Quemener
7

8
        Conversion en C par Emmanuel Quemener en aout 1997
9
        Modification par Emmanuel Quemener en aout 2019
10

11
        Remerciements a :
12

13
        - Herve Aussel pour sa procedure sur le spectre de corps noir
14
        - Didier Pelat pour l'aide lors de ce travail
15
        - Jean-Pierre Luminet pour son article de 1979
16
        - Le Numerical Recipies pour ses recettes de calcul
17
        - Luc Blanchet pour sa disponibilite lors de mes interrogations en RG
18

19
        Compilation sous gcc ( Compilateur GNU sous Linux ) :
20

21
        Version FP32 :        gcc -O3 -ffast-math -FP32 -o trou_noir_FP32 trou_noir.c -lm
22
        Version FP64 :        gcc -O3 -ffast-math -FP64 -o trou_noir_FP64 trou_noir.c -lm
23
*/ 
24

    
25
#include <stdio.h>
26
#include <math.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include <sys/time.h>
30
#include <time.h>
31

    
32
#define nbr 256 /* Nombre de colonnes du spectre */
33

    
34
#define PI 3.14159265359
35

    
36
#define TRACKPOINTS 2048
37

    
38
#if TYPE == FP32
39
#define MYFLOAT float
40
#else
41
#define MYFLOAT double
42
#endif
43

    
44
MYFLOAT atanp(MYFLOAT x,MYFLOAT y)
45
{
46
  MYFLOAT angle;
47

    
48
  angle=atan2(y,x);
49

    
50
  if (angle<0)
51
    {
52
      angle+=2*PI;
53
    }
54

    
55
  return angle;
56
}
57

    
58

    
59
MYFLOAT f(MYFLOAT v)
60
{
61
  return v;
62
}
63

    
64
MYFLOAT g(MYFLOAT u,MYFLOAT m,MYFLOAT b)
65
{
66
  return (3.*m/b*pow(u,2)-u);
67
}
68

    
69

    
70
void calcul(MYFLOAT *us,MYFLOAT *vs,MYFLOAT up,MYFLOAT vp,
71
            MYFLOAT h,MYFLOAT m,MYFLOAT b)
72
{
73
  MYFLOAT c[4],d[4];
74

    
75
  c[0]=h*f(vp);
76
  c[1]=h*f(vp+c[0]/2.);
77
  c[2]=h*f(vp+c[1]/2.);
78
  c[3]=h*f(vp+c[2]);
79
  d[0]=h*g(up,m,b);
80
  d[1]=h*g(up+d[0]/2.,m,b);
81
  d[2]=h*g(up+d[1]/2.,m,b);
82
  d[3]=h*g(up+d[2],m,b);
83

    
84
  *us=up+(c[0]+2.*c[1]+2.*c[2]+c[3])/6.;
85
  *vs=vp+(d[0]+2.*d[1]+2.*d[2]+d[3])/6.;
86
}
87

    
88
void rungekutta(MYFLOAT *ps,MYFLOAT *us,MYFLOAT *vs,
89
                MYFLOAT pp,MYFLOAT up,MYFLOAT vp,
90
                MYFLOAT h,MYFLOAT m,MYFLOAT b)
91
{
92
  calcul(us,vs,up,vp,h,m,b);
93
  *ps=pp+h;
94
}
95

    
96

    
97
MYFLOAT decalage_spectral(MYFLOAT r,MYFLOAT b,MYFLOAT phi,
98
                         MYFLOAT tho,MYFLOAT m)
99
{
100
  return (sqrt(1-3*m/r)/(1+sqrt(m/pow(r,3))*b*sin(tho)*sin(phi)));
101
}
102

    
103
MYFLOAT spectre(MYFLOAT rf,MYFLOAT q,MYFLOAT b,MYFLOAT db,
104
             MYFLOAT h,MYFLOAT r,MYFLOAT m,MYFLOAT bss)
105
{
106
  MYFLOAT flx;
107

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

    
112
MYFLOAT spectre_cn(MYFLOAT rf,MYFLOAT b,MYFLOAT db,
113
                MYFLOAT h,MYFLOAT r,MYFLOAT m,MYFLOAT bss)
114
{
115
  
116
  MYFLOAT flx;
117
  MYFLOAT nu_rec,nu_em,qu,temp_em,flux_int;
118
  int fi,posfreq;
119

    
120
#define planck 6.62e-34
121
#define k 1.38e-23
122
#define c2 9.e16
123
#define temp 3.e7
124
#define m_point 1.
125

    
126
#define lplanck (log(6.62)-34.*log(10.))
127
#define lk (log(1.38)-23.*log(10.))
128
#define lc2 (log(9.)+16.*log(10.))
129
  
130
  MYFLOAT v=1.-3./r;
131

    
132
  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 ));
133

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

    
136
  flux_int=0.;
137
  flx=0.;
138

    
139
  for (fi=0;fi<nbr;fi++)
140
    {
141
      nu_em=bss*(MYFLOAT)fi/(MYFLOAT)nbr;
142
      nu_rec=nu_em*rf;
143
      posfreq=(int)(nu_rec*(MYFLOAT)nbr/bss);
144
      if ((posfreq>0)&&(posfreq<nbr))
145
          {
146
          flux_int=2.*planck/c2*pow(nu_em,3)/(exp(planck*nu_em/(k*temp_em))-1.)*exp(3.*log(rf))*b*db*h;
147
            flx+=flux_int;
148
          }
149
    }
150

    
151
  return((MYFLOAT)flx);
152
}
153

    
154
void impact(MYFLOAT d,MYFLOAT phi,int dim,MYFLOAT r,MYFLOAT b,MYFLOAT tho,MYFLOAT m,
155
            MYFLOAT **zp,MYFLOAT **fp,
156
            MYFLOAT q,MYFLOAT db,
157
            MYFLOAT h,MYFLOAT bss,int raie)
158
{
159
  MYFLOAT xe,ye;
160
  int xi,yi;
161
  MYFLOAT flx,rf;
162
  xe=d*sin(phi);
163
  ye=-d*cos(phi);
164

    
165
  xi=(int)xe+dim/2;
166
  yi=(int)ye+dim/2;
167

    
168
  rf=decalage_spectral(r,b,phi,tho,m);
169

    
170
  if (raie==0)
171
    {
172
      bss=1.e19;
173
      flx=spectre_cn(rf,b,db,h,r,m,bss);
174
    }
175
  else
176
    {
177
      bss=2.;
178
      flx=spectre(rf,q,b,db,h,r,m,bss);
179
    }
180
  
181
  if (zp[xi][yi]==0.)
182
    {
183
      zp[xi][yi]=1./rf;
184
    }
185
  
186
  if (fp[xi][yi]==0.)
187
    {
188
      fp[xi][yi]=flx;
189
    }
190

    
191
}
192

    
193
void sauvegarde_pgm(char nom[24],unsigned int **image,int dim)
194
{
195
  FILE            *sortie;
196
  unsigned long   i,j;
197
  
198
  sortie=fopen(nom,"w");
199
  
200
  fprintf(sortie,"P5\n");
201
  fprintf(sortie,"%i %i\n",dim,dim);
202
  fprintf(sortie,"255\n");
203

    
204
  for (j=0;j<dim;j++) for (i=0;i<dim;i++)
205
    {
206
      fputc(image[i][j],sortie);
207
    }
208

    
209
  fclose(sortie);
210
}
211

    
212
int main(int argc,char *argv[])
213
{
214

    
215
  MYFLOAT m,rs,ri,re,tho;
216
  int q;
217

    
218
  MYFLOAT bss,stp;
219
  int nmx,dim;
220
  MYFLOAT d,bmx,db,b,h;
221
  MYFLOAT up,vp,pp;
222
  MYFLOAT us,vs,ps;
223
  MYFLOAT rp[TRACKPOINTS];
224
  MYFLOAT **zp,**fp;
225
  unsigned int **izp,**ifp;
226
  MYFLOAT zmx,fmx;
227
  int zimx=0,zjmx=0,fimx=0,fjmx=0;
228
  MYFLOAT phi,phd,php,nr,r;
229
  int ni,ii,i,imx,j,n,tst,raie,fcl,zcl;
230
  MYFLOAT nh;
231
  struct timeval tv1,tv2;
232
  double elapsed;
233
  int mtv1,mtv2;
234
  
235
  if (argc==2)
236
    {
237
      if (strcmp(argv[1],"-aide")==0)
238
        {
239
          printf("\nSimulation d'un disque d'accretion autour d'un trou noir\n");
240
          printf("\nParametres a definir :\n\n");
241
          printf("  1) Dimension de l'Image\n");
242
          printf("  2) Masse relative du trou noir\n");
243
          printf("  3) Dimension du disque exterieur\n");
244
          printf("  4) Inclinaison par rapport au disque (en degres)\n");
245
          printf("  5) Rayonnement de disque MONOCHROMATIQUE ou CORPS_NOIR\n");
246
          printf("  6) Impression des images NEGATIVE ou POSITIVE\n"); 
247
          printf("  7) Nom de l'image des Flux\n");
248
          printf("  8) Nom de l'image des decalages spectraux\n");
249
          printf("\nSi aucun parametre defini, parametres par defaut :\n\n");
250
          printf("  1) Dimension de l'image : 1024 pixels de cote\n");
251
          printf("  2) Masse relative du trou noir : 1\n");
252
          printf("  3) Dimension du disque exterieur : 12 \n");
253
          printf("  4) Inclinaison par rapport au disque (en degres) : 10\n");
254
          printf("  5) Rayonnement de disque CORPS_NOIR\n");
255
          printf("  6) Impression des images NEGATIVE ou POSITIVE\n"); 
256
                 printf("  7) Nom de l'image des flux : flux.pgm\n");
257
          printf("  8) Nom de l'image des z : z.pgm\n");
258
        }
259
    }
260
  
261
  if (argc==9)
262
    {
263
      printf("# Utilisation les valeurs definies par l'utilisateur\n");
264
      
265
      dim=atoi(argv[1]);
266
      m=atof(argv[2]);
267
      re=atof(argv[3]);
268
      tho=PI/180.*(90-atof(argv[4]));
269
      
270
      rs=2.*m;
271
      ri=3.*rs;
272

    
273
      if (strcmp(argv[5],"CORPS_NOIR")==0)
274
        {
275
          raie=0;
276
        }
277
      else
278
        {
279
          raie=1;
280
        }
281

    
282
    }
283
  else
284
    {
285
      printf("# Utilisation les valeurs par defaut\n");
286
      
287
      dim=1024;
288
      m=1.;
289
      rs=2.*m;
290
      ri=3.*rs;
291
      re=12.;
292
      tho=PI/180.*80;
293
      // Corps noir
294
      raie=0;
295
    }
296

    
297
  if (raie==1)
298
    {
299
      bss=2.;
300
      q=-2;
301
    }
302
  else
303
    {
304
      bss=1.e19;
305
      q=-0.75;
306
    }
307

    
308
      printf("# Dimension de l'image : %i\n",dim);
309
      printf("# Masse : %f\n",m);
310
      printf("# Rayon singularite : %f\n",rs);
311
      printf("# Rayon interne : %f\n",ri);
312
      printf("# Rayon externe : %f\n",re);
313
      printf("# Inclinaison a la normale en radian : %f\n",tho);
314
  
315
  zp=(MYFLOAT**)calloc(dim,sizeof(MYFLOAT*));
316
  zp[0]=(MYFLOAT*)calloc(dim*dim,sizeof(MYFLOAT));
317
  
318
  fp=(MYFLOAT**)calloc(dim,sizeof(MYFLOAT*));
319
  fp[0]=(MYFLOAT*)calloc(dim*dim,sizeof(MYFLOAT));
320

    
321
  izp=(unsigned int**)calloc(dim,sizeof(unsigned int*));
322
  izp[0]=(unsigned int*)calloc(dim*dim,sizeof(unsigned int));
323
  
324
  ifp=(unsigned int**)calloc(dim,sizeof(unsigned int*));
325
  ifp[0]=(unsigned int*)calloc(dim*dim,sizeof(unsigned int));
326

    
327
  for (i=1;i<dim;i++)
328
    {
329
      zp[i]=zp[i-1]+dim;
330
      fp[i]=fp[i-1]+dim;
331
      izp[i]=izp[i-1]+dim;
332
      ifp[i]=ifp[i-1]+dim;
333
    }
334

    
335
  nmx=dim;
336
  stp=dim/(2.*nmx);
337
  bmx=1.25*re;
338
  b=0.;
339

    
340
  // Set start timer
341
  gettimeofday(&tv1, NULL);
342
  //
343
  mtv1=clock()*1000/CLOCKS_PER_SEC;
344
  
345
  for (n=1;n<=nmx;n++)
346
    {     
347
      h=4.*PI/(MYFLOAT)TRACKPOINTS;
348
      d=stp*n;
349

    
350
      db=bmx/(MYFLOAT)nmx;
351
      b=db*(MYFLOAT)n;
352
      up=0.;
353
      vp=1.;
354
      
355
      pp=0.;
356
      nh=1;
357

    
358
      rungekutta(&ps,&us,&vs,pp,up,vp,h,m,b);
359
    
360
      rp[(int)nh]=fabs(b/us);
361
      
362
      do
363
        {
364
          nh++;
365
          pp=ps;
366
          up=us;
367
          vp=vs;
368
          rungekutta(&ps,&us,&vs,pp,up,vp,h,m,b);
369
          
370
          rp[(int)nh]=b/us;
371
          
372
        } while ((rp[(int)nh]>=rs)&&(rp[(int)nh]<=rp[1]));
373
      
374
      for (i=nh+1;i<TRACKPOINTS;i++)
375
        {
376
          rp[i]=0.; 
377
        }
378
      
379
      imx=(int)(8*d);
380
      
381
      for (i=0;i<=imx;i++)
382
        {
383
          phi=2.*PI/(MYFLOAT)imx*(MYFLOAT)i;
384
          phd=atanp(cos(phi)*sin(tho),cos(tho));
385
          phd=fmod(phd,PI);
386
          ii=0;
387
          tst=0;
388
          
389
          do
390
            {
391
              php=phd+(MYFLOAT)ii*PI;
392
              nr=php/h;
393
              ni=(int)nr;
394

    
395
              if ((MYFLOAT)ni<nh)
396
                {
397
                  r=(rp[ni+1]-rp[ni])*(nr-ni*1.)+rp[ni];
398
                }
399
              else
400
                {
401
                  r=rp[ni];
402
                }
403
           
404
              if ((r<=re)&&(r>=ri))
405
                {
406
                  tst=1;
407
                  impact(d,phi,dim,r,b,tho,m,zp,fp,q,db,h,bss,raie);
408
                }
409
              
410
              ii++;
411
            } while ((ii<=2)&&(tst==0));
412
        }
413
    }
414

    
415
  // Set stop timer
416
  gettimeofday(&tv2, NULL);
417
  mtv2=clock()*1000/CLOCKS_PER_SEC;
418
  
419
  //  elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
420
  //                          (tv2.tv_usec-tv1.tv_usec))/1000000;  
421
  elapsed=(double)((mtv2-mtv1)/1000.);  
422
  
423
  fmx=fp[0][0];
424
  zmx=zp[0][0];
425
  
426
  for (i=0;i<dim;i++) for (j=0;j<dim;j++)
427
    {
428
      if (fmx<fp[i][j])
429
        {
430
          fimx=i;
431
          fjmx=j;
432
          fmx=fp[i][j];
433
        }
434
      
435
      if (zmx<zp[i][j])
436
        {
437
          zimx=i;
438
          zjmx=j;
439
          zmx=zp[i][j];
440
        }
441
    }
442

    
443
  printf("\nElapsed Time : %lf",(double)elapsed);
444
  printf("\nZ max @(%i,%i) : %f",zimx,zjmx,zmx);
445
  printf("\nFlux max @(%i,%i) : %f\n\n",fimx,fjmx,fmx);
446

    
447
  for (i=0;i<dim;i++) for (j=0;j<dim;j++)
448
    {
449
      zcl=(int)(255/zmx*zp[i][dim-1-j]);
450
      fcl=(int)(255/fmx*fp[i][dim-1-j]);
451

    
452
      if (strcmp(argv[6],"NEGATIVE")==0)
453
        {
454
          if (zcl>255)
455
            {
456
              izp[i][j]=0;
457
            }
458
          else
459
            {
460
              izp[i][j]=255-zcl;
461
            }
462
          
463
          if (fcl>255)
464
            {
465
              ifp[i][j]=0;
466
            }
467
          else
468
            {
469
              ifp[i][j]=255-fcl;
470
            } 
471
          
472
        }
473
      else
474
        {
475
          if (zcl>255)
476
            {
477
              izp[i][j]=255;
478
            }
479
          else
480
            {
481
              izp[i][j]=zcl;
482
            }
483
          
484
          if (fcl>255)
485
            {
486
              ifp[i][j]=255;
487
            }
488
          else
489
            {
490
              ifp[i][j]=fcl;
491
            } 
492
          
493
        }
494
        
495
    }
496

    
497
  if (argc==9)
498
   {
499
     sauvegarde_pgm(argv[7],ifp,dim);
500
     sauvegarde_pgm(argv[8],izp,dim);
501
   }
502
  else
503
    {
504
      sauvegarde_pgm("z.pgm",izp,dim);
505
      sauvegarde_pgm("flux.pgm",ifp,dim);
506
    }
507

    
508
  free(zp[0]);
509
  free(zp);
510
  free(fp[0]);
511
  free(fp);
512

    
513
  free(izp[0]);
514
  free(izp);
515
  free(ifp[0]);
516
  free(ifp);
517

    
518
}
519

    
520