Statistiques
| Révision :

root / TrouNoir / trou_noir_MyFloat.c @ 261

Historique | Voir | Annoter | Télécharger (9,87 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
        Licence CC BY-NC-SA Emmanuel QUEMENER <emmanuel.quemener@gmail.com>
12

13
        Remerciements a :
14

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

21
        Compilation sous gcc ( Compilateur GNU sous Linux ) :
22

23
                gcc -O3 -DFP32 -o trou_noir_FP32 trou_noir_MyFloat.c -lm
24
*/ 
25

    
26
#include <stdio.h>
27
#include <math.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <sys/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
#ifdef 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,zen,fen;
227
  MYFLOAT flux_tot,impc_tot;
228
  MYFLOAT phi,thi,thx,phd,php,nr,r;
229
  int ni,ii,i,imx,j,n,tst,dist,raie,pc,fcl,zcl;
230
  MYFLOAT nh;
231

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
517
}
518

    
519