Révision 230

TrouNoir/trou_noir_OpenMP.c (revision 230)
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 -O3 -ffast-math -FP32 -o trou_noir_FP32 trou_noir.c -lm
22
	Version FP64 :	gcc -O3 -ffast-math -FP64 -o trou_noir_FP64 trou_noir.c -lm
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 == 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 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
  double elapsed;
228
  int mtv1,mtv2;
229

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

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

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

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

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

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

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

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

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

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

  
341
  // Set start timer
342
  //  gettimeofday(&tv1, &tz);
343
  gettimeofday(&tv1, NULL);
344
  //
345
  mtv1=clock()*1000/CLOCKS_PER_SEC;
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, &tz);
426
  gettimeofday(&tv2, NULL);
427
  mtv2=clock()*1000/CLOCKS_PER_SEC;
428
  
429
  //  elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L +
430
  //			  (tv2.tv_usec-tv1.tv_usec))/1000000;  
431
  elapsed=(double)((mtv2-mtv1)/1000.);  
432
  
433
  fmx=fp[0][0];
434
  zmx=zp[0][0];
435
  
436
  for (int i=0;i<dim;i++) for (int j=0;j<dim;j++)
437
    {
438
      if (fmx<fp[i][j])
439
	{
440
	  fimx=i;
441
	  fjmx=j;
442
	  fmx=fp[i][j];
443
	}
444
      
445
      if (zmx<zp[i][j])
446
	{
447
	  zimx=i;
448
	  zjmx=j;
449
	  zmx=zp[i][j];
450
	}
451
    }
452

  
453
  printf("\nElapsed Time : %lf",(double)elapsed);
454
  printf("\nZ max @(%i,%i) : %f",zimx,zjmx,zmx);
455
  printf("\nFlux max @(%i,%i) : %f\n\n",fimx,fjmx,fmx);
456

  
457
  for (int i=0;i<dim;i++) for (int j=0;j<dim;j++)
458
    {
459
      zcl=(int)(255/zmx*zp[i][dim-1-j]);
460
      fcl=(int)(255/fmx*fp[i][dim-1-j]);
461

  
462
      if (strcmp(argv[6],"NEGATIVE")==0)
463
	{
464
	  if (zcl>255)
465
	    {
466
	      izp[i][j]=0;
467
	    }
468
	  else
469
	    {
470
	      izp[i][j]=255-zcl;
471
	    }
472
	  
473
	  if (fcl>255)
474
	    {
475
	      ifp[i][j]=0;
476
	    }
477
	  else
478
	    {
479
	      ifp[i][j]=255-fcl;
480
	    } 
481
	  
482
	}
483
      else
484
	{
485
	  if (zcl>255)
486
	    {
487
	      izp[i][j]=255;
488
	    }
489
	  else
490
	    {
491
	      izp[i][j]=zcl;
492
	    }
493
	  
494
	  if (fcl>255)
495
	    {
496
	      ifp[i][j]=255;
497
	    }
498
	  else
499
	    {
500
	      ifp[i][j]=fcl;
501
	    } 
502
	  
503
	}
504
	
505
    }
506

  
507
  if (argc==9)
508
   {
509
     sauvegarde_pgm(argv[7],ifp,dim);
510
     sauvegarde_pgm(argv[8],izp,dim);
511
   }
512
  else
513
    {
514
      sauvegarde_pgm("z.pgm",izp,dim);
515
      sauvegarde_pgm("flux.pgm",ifp,dim);
516
    }
517

  
518
  free(zp[0]);
519
  free(zp);
520
  free(fp[0]);
521
  free(fp);
522

  
523
  free(izp[0]);
524
  free(izp);
525
  free(ifp[0]);
526
  free(ifp);
527

  
528
}
529

  
530

  
TrouNoir/trou_noir.c (revision 230)
223 223
  MYFLOAT rp[TRACKPOINTS];
224 224
  MYFLOAT **zp,**fp;
225 225
  unsigned int **izp,**ifp;
226
  MYFLOAT zmx,fmx,zen,fen;
226
  MYFLOAT zmx,fmx;
227 227
  int zimx=0,zjmx=0,fimx=0,fjmx=0;
228
  MYFLOAT flux_tot,impc_tot;
229
  MYFLOAT phi,thi,thx,phd,php,nr,r;
230
  int ni,ii,i,imx,j,n,tst,dist,raie,pc,fcl,zcl;
228
  MYFLOAT phi,phd,php,nr,r;
229
  int ni,ii,i,imx,j,n,tst,raie,fcl,zcl;
231 230
  MYFLOAT nh;
232 231
  struct timeval tv1,tv2;
233
  struct timezone tz;  
234 232
  double elapsed;
235 233
  int mtv1,mtv2;
236 234
  
......
338 336
  stp=dim/(2.*nmx);
339 337
  bmx=1.25*re;
340 338
  b=0.;
341
  pc=0;
342 339

  
343 340
  // Set start timer
344
  gettimeofday(&tv1, &tz);
341
  gettimeofday(&tv1, NULL);
345 342
  //
346 343
  mtv1=clock()*1000/CLOCKS_PER_SEC;
347 344
  
......
416 413
    }
417 414

  
418 415
  // Set stop timer
419
  gettimeofday(&tv2, &tz);
416
  gettimeofday(&tv2, NULL);
420 417
  mtv2=clock()*1000/CLOCKS_PER_SEC;
421 418
  
422 419
  //  elapsed=(double)((tv2.tv_sec-tv1.tv_sec) * 1000000L +

Formats disponibles : Unified diff