Statistiques
| Révision :

root / TrouNoir / trou_noir.c @ 214

Historique | Voir | Annoter | Télécharger (9,79 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
                gcc -O2 -o trou_noir trou_noir.c -lm
22
*/ 
23

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

    
30
#define nbr 256 /* Nombre de colonnes du spectre */
31

    
32
#define PI 3.14159265359
33

    
34
#define TRACKPOINTS 2048
35

    
36
#if TYPE == FP32
37
#define MYFLOAT float
38
#else
39
#define MYFLOAT double
40
#endif
41

    
42
MYFLOAT atanp(MYFLOAT x,MYFLOAT y)
43
{
44
  MYFLOAT angle;
45

    
46
  angle=atan2(y,x);
47

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

    
53
  return angle;
54
}
55

    
56

    
57
MYFLOAT f(MYFLOAT v)
58
{
59
  return v;
60
}
61

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

    
67

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

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

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

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

    
94

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

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

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

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

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

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

    
130
  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 ));
131

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

    
134
  flux_int=0.;
135
  flx=0.;
136

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

    
149
  return((MYFLOAT)flx);
150
}
151

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

    
163
  xi=(int)xe+dim/2;
164
  yi=(int)ye+dim/2;
165

    
166
  rf=decalage_spectral(r,b,phi,tho,m);
167

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

    
189
}
190

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

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

    
207
  fclose(sortie);
208
}
209

    
210
int main(int argc,char *argv[])
211
{
212

    
213
  MYFLOAT m,rs,ri,re,tho;
214
  int q;
215

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

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

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

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

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

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

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

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

    
340
  // Set start timer
341
  gettimeofday(&tv1, &tz);
342

    
343
  for (n=1;n<=nmx;n++)
344
    {     
345
      h=4.*PI/(MYFLOAT)TRACKPOINTS;
346
      d=stp*n;
347

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

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

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

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

    
439
  printf("\nElapsed Time : %lf",(double)elapsed);
440
  printf("\nZ max @(%i,%i) : %f",zimx,zjmx,zmx);
441
  printf("\nFlux max @(%i,%i) : %f\n\n",fimx,fjmx,fmx);
442

    
443
  for (i=0;i<dim;i++) for (j=0;j<dim;j++)
444
    {
445
      zcl=(int)(255/zmx*zp[i][dim-1-j]);
446
      fcl=(int)(255/fmx*fp[i][dim-1-j]);
447

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

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

    
504
  free(zp[0]);
505
  free(zp);
506
  free(fp[0]);
507
  free(fp);
508

    
509
  free(izp[0]);
510
  free(izp);
511
  free(ifp[0]);
512
  free(ifp);
513

    
514
}
515

    
516