Statistiques
| Révision :

root / TrouNoir / trou_noir_MyFloat.c @ 206

Historique | Voir | Annoter | Télécharger (10,86 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
  int fi;
106

    
107
  fi=(int)(rf*nbr/bss);
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
  MYFLOAT v=1.-3./r;
127

    
128
  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 ));
129

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

    
132
  flux_int=0;
133
  flx=0;
134

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

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

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

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

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

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

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

    
198
  for (j=0;j<dim;j++) for (i=0;i<dim;i++)
199
    {
200
      fputc(image[i][j],sortie);
201
    }
202

    
203
  fclose(sortie);
204
}
205

    
206
int main(int argc,char *argv[])
207
{
208

    
209
  MYFLOAT m,rs,ri,re,tho,ro;
210
  int q;
211

    
212
  MYFLOAT bss,stp;
213
  int nmx,dim;
214
  MYFLOAT d,bmx,db,b,h;
215
  MYFLOAT up,vp,pp;
216
  MYFLOAT us,vs,ps;
217
  MYFLOAT rp[TRACKPOINTS];
218
  MYFLOAT **zp,**fp;
219
  unsigned int **izp,**ifp;
220
  MYFLOAT zmx,fmx,zen,fen;
221
  MYFLOAT flux_tot,impc_tot;
222
  MYFLOAT phi,thi,thx,phd,php,nr,r;
223
  int ni,ii,i,imx,j,n,tst,dist,raie,pc,fcl,zcl;
224
  MYFLOAT nh;
225

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

    
277
      if (strcmp(argv[6],"FINIE")==0)
278
        {
279
          dist=0;
280
        }
281
      else
282
        {
283
          dist=1;
284
        }
285

    
286
      if (strcmp(argv[7],"CORPS_NOIR")==0)
287
        {
288
          raie=0;
289
        }
290
      else
291
        {
292
          raie=1;
293
        }
294

    
295
      if (strcmp(argv[8],"EXTERNE")==0)
296
        {
297
          fen=atof(argv[14]);
298
        }
299
      
300
      if (strcmp(argv[9],"EXTERNE")==0)
301
        {
302
          zen=atof(argv[15]);
303
        }
304
      
305
    }
306
  else
307
    {
308
      printf("# Utilisation les valeurs par defaut\n");
309
      
310
      dim=1024;
311
      m=1.;
312
      rs=2.*m;
313
      ri=3.*rs;
314
      re=12.;
315
      ro=100.;
316
      tho=PI/180.*80;
317
      // Distance finie
318
      dist=0;
319
      // Corps noir
320
      raie=0;
321
    }
322

    
323
  if (raie==1)
324
    {
325
      bss=2.;
326
      q=-2;
327
    }
328
  else
329
    {
330
      bss=1e19;
331
      q=-0.75;
332
    }
333

    
334
      printf("# Dimension de l'image : %i\n",dim);
335
      printf("# Masse : %f\n",m);
336
      printf("# Rayon singularite : %f\n",rs);
337
      printf("# Rayon interne : %f\n",ri);
338
      printf("# Rayon externe : %f\n",re);
339
      printf("# Distance de l'observateur : %f\n",ro);
340
      printf("# Inclinaison a la normale en radian : %f\n",tho);
341
  
342
  zp=(MYFLOAT**)calloc(dim,sizeof(MYFLOAT*));
343
  zp[0]=(MYFLOAT*)calloc(dim*dim,sizeof(MYFLOAT));
344
  
345
  fp=(MYFLOAT**)calloc(dim,sizeof(MYFLOAT*));
346
  fp[0]=(MYFLOAT*)calloc(dim*dim,sizeof(MYFLOAT));
347

    
348
  izp=(unsigned int**)calloc(dim,sizeof(unsigned int*));
349
  izp[0]=(unsigned int*)calloc(dim*dim,sizeof(unsigned int));
350
  
351
  ifp=(unsigned int**)calloc(dim,sizeof(unsigned int*));
352
  ifp[0]=(unsigned int*)calloc(dim*dim,sizeof(unsigned int));
353

    
354
  for (i=1;i<dim;i++)
355
    {
356
      zp[i]=zp[i-1]+dim;
357
      fp[i]=fp[i-1]+dim;
358
      izp[i]=izp[i-1]+dim;
359
      ifp[i]=ifp[i-1]+dim;
360
    }
361

    
362
  nmx=dim;
363
  stp=dim/(2.*nmx);
364
  bmx=1.25*re;
365
  b=0.;
366
  thx=asin(bmx/ro);
367
  pc=0;
368

    
369
  struct timeval tv1,tv2;
370
  struct timezone tz;
371
      
372
  // Set start timer
373
  gettimeofday(&tv1, &tz);
374

    
375
  for (n=1;n<=nmx;n++)
376
    {     
377
      h=4.*PI/(MYFLOAT)TRACKPOINTS;
378
      d=stp*n;
379

    
380
      if (dist==1)
381
        {
382
          db=bmx/(MYFLOAT)nmx;
383
          b=db*(MYFLOAT)n;
384
          up=0.;
385
          vp=1.;
386
        }
387
      else
388
        {
389
          thi=thx/(MYFLOAT)nmx*(MYFLOAT)n;
390
          db=ro*sin(thi)-b;
391
          b=ro*sin(thi);
392
          up=sin(thi);
393
          vp=cos(thi);
394
        }
395
      
396
      pp=0.;
397
      nh=1;
398

    
399
      rungekutta(&ps,&us,&vs,pp,up,vp,h,m,b);
400
    
401
      rp[(int)nh]=fabs(b/us);
402
      
403
      do
404
        {
405
          nh++;
406
          pp=ps;
407
          up=us;
408
          vp=vs;
409
          rungekutta(&ps,&us,&vs,pp,up,vp,h,m,b);
410
          
411
          rp[(int)nh]=b/us;
412
          
413
        } while ((rp[(int)nh]>=rs)&&(rp[(int)nh]<=rp[1]));
414
      
415
      for (i=nh+1;i<TRACKPOINTS;i++)
416
        {
417
          rp[i]=0.; 
418
        }
419
      
420
      imx=(int)(8*d);
421
      
422
      for (i=0;i<=imx;i++)
423
        {
424
          phi=2.*PI/(MYFLOAT)imx*(MYFLOAT)i;
425
          phd=atanp(cos(phi)*sin(tho),cos(tho));
426
          phd=fmod(phd,PI);
427
          ii=0;
428
          tst=0;
429
          
430
          do
431
            {
432
              php=phd+(MYFLOAT)ii*PI;
433
              nr=php/h;
434
              ni=(int)nr;
435

    
436
              if ((MYFLOAT)ni<nh)
437
                {
438
                  r=(rp[ni+1]-rp[ni])*(nr-ni*1.)+rp[ni];
439
                }
440
              else
441
                {
442
                  r=rp[ni];
443
                }
444
           
445
              if ((r<=re)&&(r>=ri))
446
                {
447
                  tst=1;
448
                  impact(d,phi,dim,r,b,tho,m,zp,fp,q,db,h,bss,raie);
449
                }
450
              
451
              ii++;
452
            } while ((ii<=2)&&(tst==0));
453
        }
454
    }
455

    
456
  // Set stop timer
457
  gettimeofday(&tv2, &tz);
458
  
459
  double elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
460
                          (tv2.tv_usec-tv1.tv_usec))/1000000;  
461
  
462
  fmx=fp[0][0];
463
  zmx=zp[0][0];
464
  
465
  for (i=0;i<dim;i++) for (j=0;j<dim;j++)
466
    {
467
      if (fmx<fp[i][j])
468
        {
469
          fmx=fp[i][j];
470
        }
471
      
472
      if (zmx<zp[i][j])
473
        {
474
          zmx=zp[i][j];
475
        }
476
    }
477

    
478
  printf("\nElapsed Time : %lf",(double)elapsed);
479
  printf("\nFlux max : %f",fmx);
480
  printf("\nZ max : %f\n\n",zmx);
481

    
482
  if (strcmp(argv[8],"EXTERNE")==0)
483
    {
484
      fmx=fen;
485
    }
486

    
487
  if (strcmp(argv[9],"EXTERNE")==0)
488
    {  
489
      zmx=zen;
490
    }
491

    
492
  for (i=0;i<dim;i++) for (j=0;j<dim;j++)
493
    {
494
      zcl=(int)(255/zmx*zp[i][dim-1-j]);
495
      fcl=(int)(255/fmx*fp[i][dim-1-j]);
496

    
497
      if (strcmp(argv[8],"NEGATIVE")==0)
498
        {
499
          if (zcl>255)
500
            {
501
              izp[i][j]=0;
502
            }
503
          else
504
            {
505
              izp[i][j]=255-zcl;
506
            }
507
          
508
          if (fcl>255)
509
            {
510
              ifp[i][j]=0;
511
            }
512
          else
513
            {
514
              ifp[i][j]=255-fcl;
515
            } 
516
          
517
        }
518
      else
519
        {
520
          if (zcl>255)
521
            {
522
              izp[i][j]=255;
523
            }
524
          else
525
            {
526
              izp[i][j]=zcl;
527
            }
528
          
529
          if (fcl>255)
530
            {
531
              ifp[i][j]=255;
532
            }
533
          else
534
            {
535
              ifp[i][j]=fcl;
536
            } 
537
          
538
        }
539
        
540
    }
541

    
542
  if ((argc==14)||(argc==16))
543
   {
544
     sauvegarde_pgm(argv[11],ifp,dim);
545
     sauvegarde_pgm(argv[12],izp,dim);
546
   }
547
  else
548
    {
549
      sauvegarde_pgm("z.pgm",izp,dim);
550
      sauvegarde_pgm("flux.pgm",ifp,dim);
551
    }
552

    
553
  free(zp[0]);
554
  free(zp);
555
  free(fp[0]);
556
  free(fp);
557

    
558
  free(izp[0]);
559
  free(izp);
560
  free(ifp[0]);
561
  free(ifp);
562

    
563
}
564

    
565