forked from atchekho/harmpi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage.c
504 lines (406 loc) · 15 KB
/
image.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//Modified by Alexander Tchekhovskoy: MPI+3D
/***********************************************************************************
Copyright 2006 Charles F. Gammie, Jonathan C. McKinney, Scott C. Noble,
Gabor Toth, and Luca Del Zanna
HARM version 1.0 (released May 1, 2006)
This file is part of HARM. HARM is a program that solves hyperbolic
partial differential equations in conservative form using high-resolution
shock-capturing techniques. This version of HARM has been configured to
solve the relativistic magnetohydrodynamic equations of motion on a
stationary black hole spacetime in Kerr-Schild coordinates to evolve
an accretion disk model.
You are morally obligated to cite the following two papers in his/her
scientific literature that results from use of any part of HARM:
[1] Gammie, C. F., McKinney, J. C., \& Toth, G.\ 2003,
Astrophysical Journal, 589, 444.
[2] Noble, S. C., Gammie, C. F., McKinney, J. C., \& Del Zanna, L. \ 2006,
Astrophysical Journal, 641, 626.
Further, we strongly encourage you to obtain the latest version of
HARM directly from our distribution website:
http://rainman.astro.uiuc.edu/codelib/
HARM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
HARM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with HARM; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
***********************************************************************************/
#include "decs.h"
#include <ctype.h>
/* Decide whether to use raw R8 (0) or PPM (1) image formats */
#define MAKE_PPM_IMAGE (1)
#if( MAKE_PPM_IMAGE )
#define IMGEXT "ppm"
#else
#define IMGEXT "r8"
#endif
/* Number of colors used in the images: */
#define NCOLORS (256)
/* Local Mnemonics */
#define IRHO (0)
#define IUU (1)
#define IBSQ (2)
#define IGAM (3)
#define RED (0)
#define GREEN (1)
#define BLUE (2)
/* Global variables for PPM images */
static unsigned int color_map[3][NCOLORS];
static int ppm_height, ppm_ncolors, ppm_width;
/* Local functions */
void get_color_map(void);
void image(double *f, char *fname);
void image_r8(double *f, char *fname);
void image_ppm(double *f, char *fname);
/******************************************************************************/
/******************************************************************************
image_all():
-----------
-- Main driver for generating "image" or snapshots of any desired quantity;
-- This is esponsible for calculating the outputted quantities and setting
the names of the images
-- This is the only routine in this file that a user should modify, all
other routines merely control how the images are created.
-- The image generating routines use a linear scale, so be sure to
take the log of any quantity you want to see in log color scale.
******************************************************************************/
void image_all( int image_count )
{
int i,j,k,m, i_img;
static int first_call = 1;
static char ifnam[3*NIMG+1][100];
double gamma;
struct of_geom geom ;
static const double fimage_logmin = 1.e-15;
char suff[100];
#ifdef MPI
//file suffixes that will be different by MPI rank
sprintf(suff, "_%04d", mpi_rank);
#else
//no suffix absent MPI
strcpy(suff, "");
#endif
if( (IGAM+1) != NIMG ) {
fprintf(stderr, "image_all(): Index problem with fimage[] \n");
fflush(stderr);
exit(1);
}
#if( MAKE_PPM_IMAGE )
if( first_call ) {
get_color_map();
first_call = 0 ;
}
#endif
/************************************************************************
Set the names of the image files to be generated now :
************************************************************************/
i_img = 0 ;
sprintf(ifnam[i_img++], "images/im_rho_%04d%s.%s",image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/im_u_%04d%s.%s" ,image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/im_bsq_%04d%s.%s",image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/im_gam_%04d%s.%s",image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/im_lrho_%04d%s.%s",image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/im_lu_%04d%s.%s" ,image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/im_lbsq_%04d%s.%s",image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/im_lgam_%04d%s.%s",image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/failu2p1_%04d%s.%s",image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/failu2p2_%04d%s.%s",image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/failu2p3_%04d%s.%s",image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/failgamc_%04d%s.%s",image_count, suff, IMGEXT) ;
sprintf(ifnam[i_img++], "images/failfint_%04d%s.%s",image_count, suff, IMGEXT) ;
/************************************************************************
Calculate the functions to be imaged :
-- the log versions overwrite the non-log versions;
-- calculate only the non-log version here
************************************************************************/
m = 0 ; //m is the linear index of pixel in the image
IMAGELOOP {
get_geometry(i,j,k,CENT,&geom) ;
if( gamma_calc(p[i][j][k],&geom,&gamma) ) { gamma = 1.; }
fimage[IRHO][m] = p[i][j][k][RHO] ;
fimage[IUU ][m] = p[i][j][k][UU ] ;
fimage[IBSQ][m] = bsq_calc( p[i][j][k], &geom ) ;
fimage[IGAM][m] = gamma ;
m++;
}
/************************************************************************
Output non-log versions:
************************************************************************/
for( i_img = 0 ; i_img < NIMG; i_img++ ) {
image( fimage[i_img], ifnam[i_img] );
}
/************************************************************************
Make log version of the image functions
************************************************************************/
for( i = 0 ; i < NIMG*N1*N2*N3; i++ ) {
fimage[0][i] = log( fabs(fimage[0][i]) + fimage_logmin );
}
for( i_img = 0 ; i_img < NIMG; i_img++ ) {
image( fimage[i_img], ifnam[i_img+NIMG] );
}
/************************************************************************
Output log versions:
************************************************************************/
for( j = 0 ; j < 5; j++ ) {
for( i = 0 ; i < N1*N2*N3; i++ ) {
fimage[0][i] = (double) failimage[j][i];
}
image( fimage[0], ifnam[j+2*NIMG] );
}
/* Reset array after every image dump: */
for( i = 0 ; i < 5*N1*N2*N3; i++ ) {
failimage[0][i] = 0;
}
return;
}
/******************************************************************************/
/******************************************************************************
image():
-----------
-- wrapper for image_r8() and image_ppm()
******************************************************************************/
void image(double *f, char *fname)
{
#if( MAKE_PPM_IMAGE )
image_ppm( f, fname );
#else
image_r8( f, fname );
#endif
return;
}
/******************************************************************************/
/******************************************************************************
image_r8():
-----------
-- generates a raw, unmapped "image" or pixelated output file.
******************************************************************************/
void image_r8(double *f, char *fname)
{
int i,j ;
double iq,liq,scale,max,min,f_ncolors;
FILE *fp;
f_ncolors = (double) (NCOLORS-1);
if( (fp = fopen(fname,"w")) == NULL ) {
fflush(stderr);
fprintf(stderr,"image(): Cannot open %s !! \n",fname);
fflush(stderr);
return;
}
/* mapping is in NCOLORS steps lmax and lmin */
max = min = f[0];
for( i = 0 ; i < N1*N2; i++ ) {
if(f[i] > max) max = f[i] ;
if(f[i] < min) min = f[i] ;
}
scale = f_ncolors/(max - min) ;
/* Header information: */
fprintf(fp, "RAW\n# min=%g , max=%g \n%d %d\n%d\n", min, max, N1, N2, (NCOLORS-1) );
fflush(fp);
for( i = 0 ; i < N1*N2; i++ ) {
iq = f[i];
liq = scale*(iq - min) ;
if(liq > f_ncolors) liq = f_ncolors ;
if(liq < 0.) liq = 0. ;
fprintf(fp,"%c",(unsigned char)((int)liq)) ;
}
fclose(fp);
return;
}
/******************************************************************************/
/******************************************************************************
image_ppm():
-----------
-- generates a color mapped "image" or pixelated output file following
the "raw" PPM format ("man ppm" for more details).
-- color map determined by get_color_map()
******************************************************************************/
void image_ppm(double *f, char *fname)
{
int i,j, icolor ;
double iq,liq,scale,max,min,f_ncolors;
FILE *fp;
f_ncolors = (double) ppm_ncolors;
if( (fp = fopen(fname,"w")) == NULL ) {
fflush(stderr);
fprintf(stderr,"image(): Cannot open %s !! \n",fname);
fflush(stderr);
return;
}
/* mapping is in ppm_ncolors steps lmax and lmin */
max = min = f[0];
for( i = 0 ; i < N1*N2; i++ ) {
if(f[i] > max) max = f[i] ;
if(f[i] < min) min = f[i] ;
}
scale = f_ncolors/(max - min) ;
/* Header information: */
fprintf(fp, "P6\n# min=%g , max=%g \n%d %d\n%d\n", min, max, N1, N2, ppm_ncolors );
fflush(fp);
for( i = 0 ; i < N1*N2; i++ ) {
iq = f[i];
liq = scale*(iq - min) ;
icolor = (int) liq;
if( icolor > ppm_ncolors ) { icolor = ppm_ncolors; }
if( icolor < 0 ) { icolor = 0; }
fputc(color_map[RED ][icolor],fp);
fputc(color_map[GREEN][icolor],fp);
fputc(color_map[BLUE ][icolor],fp);
}
fclose(fp);
return;
}
/*******************************************************************************/
/*******************************************************************************
get_color_map():
---------------
-- reads in a plain-type ppm file to generate the color map
-- see http://netpbm.sourceforge.net/doc/ppm.html or "man ppm"
for PPM format specifications
*******************************************************************************/
void get_color_map(void)
{
int i, c0,c1,c2, iw, ibody;
char c, word[100] ;
int ipixel, icolor;
FILE *fp;
const char magic_raw[] = "P6";
const char magic_plain[] = "P3";
enum GET_type { get_magic, get_width, get_height, get_ncolors, get_pixels, get_newline };
enum GET_type read_type, old_read_type;
enum PPM_type { ppm_plain, ppm_raw };
enum PPM_type ppm_type, ppm_body;
if(MASTER==mpi_rank) fprintf(stdout,"Starting get_color_map().... \n"); fflush(stdout);
if( (fp = fopen("map.ppm","r")) == NULL ) {
fflush(stderr);
fprintf(stderr,"get_color_map(): Cannot open map.ppm !! \n");
fflush(stderr);
exit(1);
}
old_read_type = read_type = get_magic; /* Need to read in the magic number or ppm type first */
ppm_type = ppm_plain; /* Always read in the header as plain text */
iw = 0;
icolor = ipixel = 0;
/* Read in the entire stream as a character stream */
while( (c = getc(fp)) != EOF ) {
//fprintf(stderr,"%c",c);fflush(stderr);
switch (read_type) {
case get_magic :
word[iw++] = c;
if( iw == 2 ) {
word[iw] = '\0';
if( strcmp(word,magic_raw ) == 0 ) { ppm_body = ppm_raw; }
else if( strcmp(word,magic_plain) == 0 ) { ppm_body = ppm_plain;}
else {
fprintf(stderr,"get_color_map(): error getting magic number, type = %s \n", word);
fflush(stderr); fclose(fp); exit(1);
}
old_read_type = read_type;
read_type = get_width;
iw = 0;
}
break;
case get_width :
if( c == '#' ) { old_read_type = get_width; read_type = get_newline; break; }
if( isdigit(c) ) { word[iw++] = c; }
else if( iw > 0 ) {
word[iw] = '\0';
ppm_width = atoi(word);
old_read_type = read_type;
read_type = get_height;
iw = 0;
}
break;
case get_height :
if( c == '#' ) { old_read_type = get_height; read_type = get_newline; break; }
if( isdigit(c) ) { word[iw++] = c; }
else if( iw > 0 ) {
word[iw] = '\0';
ppm_height = atoi(word);
old_read_type = read_type;
read_type = get_ncolors;
iw = 0;
}
break;
case get_ncolors :
if( c == '#' ) { old_read_type = get_ncolors; read_type = get_newline; break; }
if( isdigit(c) ) { word[iw++] = c; }
else if( iw > 0 ) {
word[iw] = '\0';
ppm_ncolors = atoi(word);
old_read_type = read_type;
read_type = get_pixels;
iw = 0;
if( ppm_ncolors > NCOLORS ) {
fprintf(stderr,
"get_color_map(): too many colors: ncolors = %d, maxcolors = %d \n",
ppm_ncolors, NCOLORS);
fflush(stderr); fclose(fp); exit(1);
}
}
break;
case get_pixels :
/* We assume that pixels come in threes and there are no comments within a pixel */
if( c == '#' ) { old_read_type = get_pixels; read_type = get_newline; break; }
if( isdigit(c) ) { word[iw++] = c; }
else if( iw > 0 ) {
word[iw] = '\0';
color_map[icolor++][ipixel] = atoi(word);
iw = 0;
if( icolor == 3 ) {
icolor = 0 ;
ipixel++;
}
}
break;
case get_newline :
if( c == '\n' ) {
read_type = old_read_type;
}
break;
} /* switch off */
} /* end while */
if( ipixel != (ppm_width*ppm_height) ) {
fprintf(stderr,
"get_color_map(): missing pixels: npixel = %d, width = %d, height = %d \n",
ipixel, ppm_width, ppm_height);
fflush(stderr); fclose(fp); exit(1);
}
if( (ppm_ncolors+1) != ipixel ) {
fprintf(stderr,"get_color_map(): missing colors: npixel = %d, ppm_ncolors = %d \n",
ipixel, ppm_ncolors);
fflush(stderr); fclose(fp); exit(1);
}
fclose(fp);
#if(0)
/********************************************************************************
test get_color_map(): Make sure that testmap.ppm looks identical to your map.ppm file.
********************************************************************************/
if( (fp = fopen("testmap.ppm","w")) == NULL ) {
exit(1);
}
fprintf(fp,"P6\n%d %d\n%d\n",ppm_width, ppm_height,ppm_ncolors); fflush(fp);
for( i = 0 ; i <= ppm_ncolors; i++ ) {
fputc(color_map[RED ][i],fp);
fputc(color_map[GREEN][i],fp);
fputc(color_map[BLUE ][i],fp);
}
fclose(fp);
#endif
if(MASTER==mpi_rank) fprintf(stdout,"Ending get_color_map().... \n"); fflush(stdout);
return;
}
#undef MAKE_PPM_IMAGE
#undef NCOLORS
#undef IRHO
#undef IUU
#undef IBSQ
#undef IGAM
#undef RED
#undef GREEN
#undef BLUE
#undef IMGEXT