Statistiques
| Révision :

root / TrouNoir / trou_noir_OpenMP.c @ 301

Historique | Voir | Annoter | Télécharger (10,82 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
        Version FP32 : gcc -fopenmp -Ofast -DFP32 -o trou_noir_OMP_FP32 trou_noir_OpenMP.c -lm -lgomp
24
        Version FP64 : gcc -fopenmp -Ofast -DFP64 -o trou_noir_OMP_FP64 trou_noir_OpenMP.c -lm -lgomp
25
*/ 
26

    
27
#include <stdio.h>
28
#include <math.h>
29
#include <stdlib.h>
30
#include <string.h>
31
#include <sys/time.h>
32
#include <time.h>
33

    
34
#define nbr 256 /* Nombre de colonnes du spectre */
35

    
36
#define PI 3.14159265359
37

    
38
#define TRACKPOINTS 2048
39

    
40
#if TYPE == FP64
41
#define MYFLOAT double
42
#else
43
#define MYFLOAT float
44
#endif
45

    
46
MYFLOAT atanp(MYFLOAT x,MYFLOAT y)
47
{
48
  MYFLOAT angle;
49

    
50
  angle=atan2(y,x);
51

    
52
  if (angle<0)
53
    {
54
      angle+=2*PI;
55
    }
56

    
57
  return angle;
58
}
59

    
60

    
61
MYFLOAT f(MYFLOAT v)
62
{
63
  return v;
64
}
65

    
66
MYFLOAT g(MYFLOAT u,MYFLOAT m,MYFLOAT b)
67
{
68
  return (3.*m/b*pow(u,2)-u);
69
}
70

    
71

    
72
void calcul(MYFLOAT *us,MYFLOAT *vs,MYFLOAT up,MYFLOAT vp,
73
            MYFLOAT h,MYFLOAT m,MYFLOAT b)
74
{
75
  MYFLOAT c[4],d[4];
76

    
77
  c[0]=h*f(vp);
78
  c[1]=h*f(vp+c[0]/2.);
79
  c[2]=h*f(vp+c[1]/2.);
80
  c[3]=h*f(vp+c[2]);
81
  d[0]=h*g(up,m,b);
82
  d[1]=h*g(up+d[0]/2.,m,b);
83
  d[2]=h*g(up+d[1]/2.,m,b);
84
  d[3]=h*g(up+d[2],m,b);
85

    
86
  *us=up+(c[0]+2.*c[1]+2.*c[2]+c[3])/6.;
87
  *vs=vp+(d[0]+2.*d[1]+2.*d[2]+d[3])/6.;
88
}
89

    
90
void rungekutta(MYFLOAT *ps,MYFLOAT *us,MYFLOAT *vs,
91
                MYFLOAT pp,MYFLOAT up,MYFLOAT vp,
92
                MYFLOAT h,MYFLOAT m,MYFLOAT b)
93
{
94
  calcul(us,vs,up,vp,h,m,b);
95
  *ps=pp+h;
96
}
97

    
98

    
99
MYFLOAT decalage_spectral(MYFLOAT r,MYFLOAT b,MYFLOAT phi,
100
                         MYFLOAT tho,MYFLOAT m)
101
{
102
  return (sqrt(1-3*m/r)/(1+sqrt(m/pow(r,3))*b*sin(tho)*sin(phi)));
103
}
104

    
105
MYFLOAT spectre(MYFLOAT rf,MYFLOAT q,MYFLOAT b,MYFLOAT db,
106
             MYFLOAT h,MYFLOAT r,MYFLOAT m,MYFLOAT bss)
107
{
108
  MYFLOAT flx;
109

    
110
  flx=exp(q*log(r/m))*pow(rf,4)*b*db*h;
111
  return(flx);
112
}
113

    
114
MYFLOAT spectre_cn(MYFLOAT rf,MYFLOAT b,MYFLOAT db,
115
                MYFLOAT h,MYFLOAT r,MYFLOAT m,MYFLOAT bss)
116
{
117
  
118
  MYFLOAT flx;
119
  MYFLOAT nu_rec,nu_em,qu,temp_em,flux_int;
120
  int fi,posfreq;
121

    
122
#define planck 6.62e-34
123
#define k 1.38e-23
124
#define c2 9.e16
125
#define temp 3.e7
126
#define m_point 1.
127

    
128
#define lplanck (log(6.62)-34.*log(10.))
129
#define lk (log(1.38)-23.*log(10.))
130
#define lc2 (log(9.)+16.*log(10.))
131
  
132
  MYFLOAT v=1.-3./r;
133

    
134
  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 ));
135

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

    
138
  flux_int=0.;
139
  flx=0.;
140

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

    
153
  return((MYFLOAT)flx);
154
}
155

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

    
167
  xi=(int)xe+dim/2;
168
  yi=(int)ye+dim/2;
169

    
170
  rf=decalage_spectral(r,b,phi,tho,m);
171

    
172
  if (raie==0)
173
    {
174
      bss=1.e19;
175
      flx=spectre_cn(rf,b,db,h,r,m,bss);
176
    }
177
  else
178
    {
179
      bss=2.;
180
      flx=spectre(rf,q,b,db,h,r,m,bss);
181
    }
182
  
183
  if (zp[xi][yi]==0.)
184
    {
185
      zp[xi][yi]=1./rf;
186
    }
187
  
188
  if (fp[xi][yi]==0.)
189
    {
190
      fp[xi][yi]=flx;
191
    }
192

    
193
}
194

    
195
void sauvegarde_pgm(char nom[24],unsigned int **image,int dim)
196
{
197
  FILE            *sortie;
198
  unsigned long   i,j;
199
  
200
  sortie=fopen(nom,"w");
201
  
202
  fprintf(sortie,"P5\n");
203
  fprintf(sortie,"%i %i\n",dim,dim);
204
  fprintf(sortie,"255\n");
205

    
206
  for (j=0;j<dim;j++) for (i=0;i<dim;i++)
207
    {
208
      fputc(image[i][j],sortie);
209
    }
210

    
211
  fclose(sortie);
212
}
213

    
214
int main(int argc,char *argv[])
215
{
216

    
217
  MYFLOAT m,rs,ri,re,tho;
218
  int q;
219

    
220
  MYFLOAT bss,stp;
221
  int nmx,dim;
222
  MYFLOAT bmx;
223
  MYFLOAT **zp,**fp;
224
  unsigned int **izp,**ifp;
225
  MYFLOAT zmx,fmx;
226
  int zimx=0,zjmx=0,fimx=0,fjmx=0;
227
  int raie,fcl,zcl;
228
  struct timeval tv1,tv2;
229
  MYFLOAT elapsed,cputime,epoch;
230
  int mtv1,mtv2;
231
  unsigned int epoch1,epoch2;
232

    
233
  /* Variables used inside pragma need to be placed inside loop ! */
234

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
534
  free(zp[0]);
535
  free(zp);
536
  free(fp[0]);
537
  free(fp);
538
  
539
  free(izp[0]);
540
  free(izp);
541
  free(ifp[0]);
542
  free(ifp);
543

    
544
}
545

    
546