Statistiques
| Révision :

root / TrouNoir / trou_noir_OpenMP.c @ 235

Historique | Voir | Annoter | Télécharger (10,65 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 -fopenmp -O3 -ffast-math -FP32 -o trou_noir_OMP_FP32 trou_noir_OpenMP.c -lm -lgomp
22
        Version FP64 : gcc -fopenmp -O3 -ffast-math -FP64 -o trou_noir_OMP_FP64 trou_noir_OpenMP.c -lm -lgomp
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 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 bmx;
221
  MYFLOAT **zp,**fp;
222
  unsigned int **izp,**ifp;
223
  MYFLOAT zmx,fmx;
224
  int zimx=0,zjmx=0,fimx=0,fjmx=0;
225
  int raie,fcl,zcl;
226
  struct timeval tv1,tv2;
227
  MYFLOAT elapsed,cputime,epoch;
228
  int mtv1,mtv2;
229
  unsigned int epoch1,epoch2;
230

    
231
  /* Variables used inside pragma need to be placed inside loop ! */
232

    
233
  /* MYFLOAT d,db,b,nh,h,up,vp,pp,us,vs,ps; */
234
  /* MYFLOAT phi,phd,php,nr,r; */
235
  /* int ni,ii,imx,tst; */
236
  /* MYFLOAT rp[TRACKPOINTS]; */
237

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

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

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

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

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

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

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

    
338
  nmx=dim;
339
  stp=dim/(2.*nmx);
340
  bmx=1.25*re;
341

    
342
  // Set start timer
343
  gettimeofday(&tv1, NULL);
344
  mtv1=clock()*1000/CLOCKS_PER_SEC;
345
  epoch1=time(NULL);
346
  
347
#pragma omp parallel for
348
  for (int n=1;n<=nmx;n++)
349
    { 
350

    
351
      MYFLOAT d,db,b,nh,h,up,vp,pp,us,vs,ps;
352
      MYFLOAT phi,phd,php,nr,r;
353
      int ni,ii,imx,tst;
354
      MYFLOAT rp[TRACKPOINTS];
355

    
356
      h=4.*PI/(MYFLOAT)TRACKPOINTS;
357
      d=stp*(MYFLOAT)n;
358

    
359
      db=bmx/(MYFLOAT)nmx;
360
      b=db*(MYFLOAT)n;
361
      up=0.;
362
      vp=1.;
363
      
364
      pp=0.;
365
      nh=1;
366

    
367
      rungekutta(&ps,&us,&vs,pp,up,vp,h,m,b);
368
    
369
      rp[(int)nh]=fabs(b/us);
370
      
371
      do
372
        {
373
          nh++;
374
          pp=ps;
375
          up=us;
376
          vp=vs;
377
          rungekutta(&ps,&us,&vs,pp,up,vp,h,m,b);
378
          
379
          rp[(int)nh]=b/us;
380
          
381
        } while ((rp[(int)nh]>=rs)&&(rp[(int)nh]<=rp[1]));
382
      
383
      for (int i=nh+1;i<TRACKPOINTS;i++)
384
        {
385
          rp[i]=0.; 
386
        }
387
      
388
      imx=(int)(8*d);
389

    
390
      for (int i=0;i<=imx;i++)
391
        {
392
          phi=2.*PI/(MYFLOAT)imx*(MYFLOAT)i;
393
          phd=atanp(cos(phi)*sin(tho),cos(tho));
394
          phd=fmod(phd,PI);
395
          ii=0;
396
          tst=0;
397
          
398
          do
399
            {
400
              php=phd+(MYFLOAT)ii*PI;
401
              nr=php/h;
402
              ni=(int)nr;
403
          
404
              if ((MYFLOAT)ni<nh)
405
                {
406
                  r=(rp[ni+1]-rp[ni])*(nr-ni*1.)+rp[ni];
407
                }
408
              else
409
                {
410
                  r=rp[ni];
411
                }
412
              
413
              if ((r<=re)&&(r>=ri))
414
                {
415
                  tst=1;
416
                  impact(d,phi,dim,r,b,tho,m,zp,fp,q,db,h,bss,raie);
417
                }
418
              
419
              ii++;
420
            } while ((ii<=2)&&(tst==0));
421
        }
422
    }
423

    
424
  // Set stop timer
425
  gettimeofday(&tv2, NULL);
426
  mtv2=clock()*1000/CLOCKS_PER_SEC;
427
  epoch2=time(NULL);
428
  
429
  elapsed=(MYFLOAT)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
430
                    (tv2.tv_usec-tv1.tv_usec))/1000000;
431
  cputime=(MYFLOAT)((mtv2-mtv1)/1000.);  
432
  epoch=(MYFLOAT)(epoch2-epoch1);  
433

    
434
  fmx=fp[0][0];
435
  zmx=zp[0][0];
436
  
437
  for (int i=0;i<dim;i++) for (int j=0;j<dim;j++)
438
    {
439
      if (fmx<fp[i][j])
440
        {
441
          fimx=i;
442
          fjmx=j;
443
          fmx=fp[i][j];
444
        }
445
      
446
      if (zmx<zp[i][j])
447
        {
448
          zimx=i;
449
          zjmx=j;
450
          zmx=zp[i][j];
451
        }
452
    }
453

    
454
  printf("\nElapsed Time : %lf",(double)elapsed);
455
  printf("\nCPU Time : %lf",(double)cputime);
456
  printf("\nEpoch Time : %lf",(double)epoch);
457
  printf("\nZ max @(%i,%i) : %f",zimx,zjmx,zmx);
458
  printf("\nFlux max @(%i,%i) : %f\n\n",fimx,fjmx,fmx);
459

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

    
530
  free(zp[0]);
531
  free(zp);
532
  free(fp[0]);
533
  free(fp);
534
  
535
  free(izp[0]);
536
  free(izp);
537
  free(ifp[0]);
538
  free(ifp);
539

    
540
}
541

    
542