Statistiques
| Révision :

root / TrouNoir / trou_noir_MyFloat.c @ 235

Historique | Voir | Annoter | Télécharger (9,78 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
  MYFLOAT flux_tot,impc_tot;
226
  MYFLOAT phi,thi,thx,phd,php,nr,r;
227
  int ni,ii,i,imx,j,n,tst,dist,raie,pc,fcl,zcl;
228
  MYFLOAT nh;
229

    
230
  if (argc==2)
231
    {
232
      if (strcmp(argv[1],"-aide")==0)
233
        {
234
          printf("\nSimulation d'un disque d'accretion autour d'un trou noir\n");
235
          printf("\nParametres a definir :\n\n");
236
          printf("  1) Dimension de l'Image\n");
237
          printf("  2) Masse relative du trou noir\n");
238
          printf("  3) Dimension du disque exterieur\n");
239
          printf("  4) Inclinaison par rapport au disque (en degres)\n");
240
          printf("  5) Rayonnement de disque MONOCHROMATIQUE ou CORPS_NOIR\n");
241
          printf("  6) Impression des images NEGATIVE ou POSITIVE\n"); 
242
          printf("  7) Nom de l'image des Flux\n");
243
          printf("  8) Nom de l'image des decalages spectraux\n");
244
          printf("\nSi aucun parametre defini, parametres par defaut :\n\n");
245
          printf("  1) Dimension de l'image : 1024 pixels de cote\n");
246
          printf("  2) Masse relative du trou noir : 1\n");
247
          printf("  3) Dimension du disque exterieur : 12 \n");
248
          printf("  4) Inclinaison par rapport au disque (en degres) : 10\n");
249
          printf("  5) Rayonnement de disque CORPS_NOIR\n");
250
          printf("  6) Impression des images NEGATIVE ou POSITIVE\n"); 
251
                 printf("  7) Nom de l'image des flux : flux.pgm\n");
252
          printf("  8) Nom de l'image des z : z.pgm\n");
253
        }
254
    }
255
  
256
  if (argc==9)
257
    {
258
      printf("# Utilisation les valeurs definies par l'utilisateur\n");
259
      
260
      dim=atoi(argv[1]);
261
      m=atof(argv[2]);
262
      re=atof(argv[3]);
263
      tho=PI/180.*(90-atof(argv[4]));
264
      
265
      rs=2.*m;
266
      ri=3.*rs;
267

    
268
      if (strcmp(argv[5],"CORPS_NOIR")==0)
269
        {
270
          raie=0;
271
        }
272
      else
273
        {
274
          raie=1;
275
        }
276

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

    
292
  if (raie==1)
293
    {
294
      bss=2.;
295
      q=-2;
296
    }
297
  else
298
    {
299
      bss=1.e19;
300
      q=-0.75;
301
    }
302

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

    
316
  izp=(unsigned int**)calloc(dim,sizeof(unsigned int*));
317
  izp[0]=(unsigned int*)calloc(dim*dim,sizeof(unsigned int));
318
  
319
  ifp=(unsigned int**)calloc(dim,sizeof(unsigned int*));
320
  ifp[0]=(unsigned int*)calloc(dim*dim,sizeof(unsigned int));
321

    
322
  for (i=1;i<dim;i++)
323
    {
324
      zp[i]=zp[i-1]+dim;
325
      fp[i]=fp[i-1]+dim;
326
      izp[i]=izp[i-1]+dim;
327
      ifp[i]=ifp[i-1]+dim;
328
    }
329

    
330
  nmx=dim;
331
  stp=dim/(2.*nmx);
332
  bmx=1.25*re;
333
  b=0.;
334
  pc=0;
335

    
336
  struct timeval tv1,tv2;
337
  struct timezone tz;
338
      
339
  // Set start timer
340
  gettimeofday(&tv1, &tz);
341

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

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

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

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

    
412
  // Set stop timer
413
  gettimeofday(&tv2, &tz);
414
  
415
  double elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
416
                          (tv2.tv_usec-tv1.tv_usec))/1000000;  
417
  
418
  fmx=fp[0][0];
419
  zmx=zp[0][0];
420
  
421
  int zimx=0,zjmx=0,fimx=0,fjmx=0;
422

    
423
  for (i=0;i<dim;i++) for (j=0;j<dim;j++)
424
    {
425
      if (fmx<fp[i][j])
426
        {
427
          fimx=i;
428
          fjmx=j;
429
          fmx=fp[i][j];
430
        }
431
      
432
      if (zmx<zp[i][j])
433
        {
434
          zimx=i;
435
          zjmx=j;
436
          zmx=zp[i][j];
437
        }
438
    }
439

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

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

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

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

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

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

    
515
}
516

    
517