Statistiques
| Révision :

root / TrouNoir / trou_noir.c @ 235

Historique | Voir | Annoter | Télécharger (10,34 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 -DFP32 -o trou_noir_FP32 trou_noir.c -lm
22
        Version FP64 :        gcc -O3 -ffast-math -DFP64 -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 == FP64
39
#define MYFLOAT double
40
#else
41
#define MYFLOAT float
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
  MYFLOAT elapsed,cputime,epoch;
233
  int mtv1,mtv2;
234
  unsigned int epoch1,epoch2;
235
  
236
  if (argc==2)
237
    {
238
      if (strcmp(argv[1],"-aide")==0)
239
        {
240
          printf("\nSimulation d'un disque d'accretion autour d'un trou noir\n");
241
          printf("\nParametres a definir :\n\n");
242
          printf("  1) Dimension de l'Image\n");
243
          printf("  2) Masse relative du trou noir\n");
244
          printf("  3) Dimension du disque exterieur\n");
245
          printf("  4) Inclinaison par rapport au disque (en degres)\n");
246
          printf("  5) Rayonnement de disque MONOCHROMATIQUE ou CORPS_NOIR\n");
247
          printf("  6) Impression des images NEGATIVE ou POSITIVE\n"); 
248
          printf("  7) Nom de l'image des Flux\n");
249
          printf("  8) Nom de l'image des decalages spectraux\n");
250
          printf("\nSi aucun parametre defini, parametres par defaut :\n\n");
251
          printf("  1) Dimension de l'image : 1024 pixels de cote\n");
252
          printf("  2) Masse relative du trou noir : 1\n");
253
          printf("  3) Dimension du disque exterieur : 12 \n");
254
          printf("  4) Inclinaison par rapport au disque (en degres) : 10\n");
255
          printf("  5) Rayonnement de disque CORPS_NOIR\n");
256
          printf("  6) Impression des images NEGATIVE ou POSITIVE\n"); 
257
                 printf("  7) Nom de l'image des flux : flux.pgm\n");
258
          printf("  8) Nom de l'image des z : z.pgm\n");
259
        }
260
    }
261
  
262
  if (argc==9)
263
    {
264
      printf("# Utilisation les valeurs definies par l'utilisateur\n");
265
      
266
      dim=atoi(argv[1]);
267
      m=atof(argv[2]);
268
      re=atof(argv[3]);
269
      tho=PI/180.*(90-atof(argv[4]));
270
      
271
      rs=2.*m;
272
      ri=3.*rs;
273

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

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

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

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

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

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

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

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

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

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

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

    
416
  // Set stop timer
417
  gettimeofday(&tv2, NULL);
418
  mtv2=clock()*1000/CLOCKS_PER_SEC;
419
  epoch2=time(NULL);
420

    
421
  elapsed=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
422
                   (tv2.tv_usec-tv1.tv_usec))/1000000;  
423
  cputime=(MYFLOAT)((mtv2-mtv1)/1000.);  
424
  epoch=(MYFLOAT)(epoch2-epoch1);  
425
  
426
  fmx=fp[0][0];
427
  zmx=zp[0][0];
428
  
429
  for (i=0;i<dim;i++) for (j=0;j<dim;j++)
430
    {
431
      if (fmx<fp[i][j])
432
        {
433
          fimx=i;
434
          fjmx=j;
435
          fmx=fp[i][j];
436
        }
437
      
438
      if (zmx<zp[i][j])
439
        {
440
          zimx=i;
441
          zjmx=j;
442
          zmx=zp[i][j];
443
        }
444
    }
445

    
446
  printf("\nElapsed Time : %lf",(double)elapsed);
447
  printf("\nCPU Time : %lf",(double)cputime);
448
  printf("\nEpoch Time : %lf",(double)epoch);
449
  printf("\nZ max @(%i,%i) : %f",zimx,zjmx,zmx);
450
  printf("\nFlux max @(%i,%i) : %f\n\n",fimx,fjmx,fmx);
451

    
452
  // If input parameters set without output files precised
453
  if (argc!=7) {
454
    for (int i=0;i<dim;i++)
455
      for (int j=0;j<dim;j++)
456
        {
457
          zcl=(int)(255/zmx*zp[i][dim-1-j]);
458
          fcl=(int)(255/fmx*fp[i][dim-1-j]);
459
          
460
          if (strcmp(argv[6],"NEGATIVE")==0)
461
            {
462
              if (zcl>255)
463
                {
464
                  izp[i][j]=0;
465
                }
466
              else
467
                {
468
                  izp[i][j]=255-zcl;
469
                }
470
              
471
              if (fcl>255)
472
                {
473
                  ifp[i][j]=0;
474
                }
475
              else
476
                {
477
                  ifp[i][j]=255-fcl;
478
                } 
479
              
480
            }
481
          else
482
            {
483
              if (zcl>255)
484
                {
485
                  izp[i][j]=255;
486
                }
487
              else
488
                {
489
                  izp[i][j]=zcl;
490
                }
491
              
492
              if (fcl>255)
493
                {
494
                  ifp[i][j]=255;
495
                }
496
              else
497
                {
498
                  ifp[i][j]=fcl;
499
                } 
500
              
501
            }
502
          
503
        }
504
    
505
    if (argc==9)
506
      {
507
        sauvegarde_pgm(argv[7],ifp,dim);
508
        sauvegarde_pgm(argv[8],izp,dim);
509
      }
510
    else
511
      {
512
        sauvegarde_pgm("z.pgm",izp,dim);
513
        sauvegarde_pgm("flux.pgm",ifp,dim);
514
      }
515
  }
516
  else
517
    {
518
      printf("No output file precised, useful for benchmarks...\n\n");
519
    }
520
  
521
  free(zp[0]);
522
  free(zp);
523
  free(fp[0]);
524
  free(fp);
525

    
526
  free(izp[0]);
527
  free(izp);
528
  free(ifp[0]);
529
  free(ifp);
530

    
531
}
532

    
533