-
Notifications
You must be signed in to change notification settings - Fork 0
/
fargnuplot2D1D
executable file
·576 lines (526 loc) · 14.7 KB
/
fargnuplot2D1D
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#!/usr/bin/perl -s
# =====================================================
# FARGNUPLOT
# =====================================================
# Plotting Tool for FARGO (Frederic Masset). Fargo is a
# simple and fast 2D polar hydrodynamic code dedicated
# to planet-disk tidal interactions. This perl script
# generates 2D plots, to quickly check the results of a
# run.
#
# Run './fargoplot -h' to see the syntax and examples.
#
# Note that this script needs a gnuplot version >= 4.2
#
# -----------------------------------------------------
# Hanno Rein, Cambridge University, 2008-2009
# http://damtp.cam.ac.uk/user/hr260/
# Thanks to Aurelien Crida and Tobias Mueller.
# -----------------------------------------------------
### CONSTANTS & INCLUDES########################################################
use constant GNUPLOTCMD => "gnuplot";
use constant PI2 => "6.28318530717959";
# needed for random hash
use Digest::MD5;
use File::Basename;
### SUB ROUTINES ###############################################################
# prints command line options supported
sub printusage
{
print <<EUS;
FarGnuPlot 0.53 of 21 March 2012. Maintained by Hanno Rein.
Send comments to hr260\@damtp.cam.ac.uk
Usage: fargnuplot [options] [in/fargo.par | directory]
-h print this message
-d do not delete temporary files, usefull
for further processing
-p persistent X11 window
-x exit gnuplot after plotting (implies -p)
-num=NUM plot file with number NUM in given
directory instead of newest file
-file=FILE plot FILE, need to specify nr and ns as well
-nr number of radial grid cells
-ns number of azimuthal grid cells
-nr1D number of radial grid cells in the 1D grid
-temp=DIR directory for creating temporary files
(ideally a tmpfs filesystem)
-g plot gas density (default) gasdens*.dat
-vr plot radial velocity gasvrad*.dat
-vt plot azimuthal velocity gasvtheta*.dat
-t plot gas temperature gasTemperature*.dat
-e plot gas energy gasenergy*.dat
-q plot toomre parameter gasToomre*.dat
-l use logarithmic scale
-lcb use logarithmic scale for cb axis
-scale=FACTOR scale by factor
-r=RANGE range for cb axis (e.g. 0:1)
-xr=RANGE range for x axis (e.g. 0:1)
-yr=RANGE range for y axis (e.g. 0:1)
-palette=PALETTE set gnuplot palette
(e.g. "( 0 \"blue\", 1 \"red\" )" )
-c circular plot (default: rectangular plot)
-axis change axis in circular plot to have positive x axis in "right" direction
-clean plots only the data, no borders, no axis
-curve plot average values as a function of radius
-all plot also the 1D grid (for FARGO-2D1D)
-s square plot
-png output png file and exit gnuplot
-eps output eps file and exit gnuplot
-title=TITLE title
-xlabel=XLABEL x-axis label
-ylabel=YLABEL y-axis label
-cblabel=CBLABEL cb-axis label
Examples:
fargnuplot -nr=384 -ns=384 out5 => plot the lates
density file in the directory out5
fargnuplot in/stockholm_MJ_visc.par => plot the latest
density file in the output directoty
specified in the .par file
fargnuplot -nr=128 -ns=384 -file=out5/gasdens125.dat
=> plot the specified file
EUS
if ($_[0]){
print "\n*** Error: ";
print $_[0]."\n";
}
exit(-1);
}
# cleanUp deletes all temporary files
sub cleanUp {
unless ($d) {
print ("Deleting temporary data files.\n");
unlink ("$tempdir.fargnuplot.$random_hash.plt");
unlink ("$tempdir.fargnuplot.$random_hash.dat");
unlink ("$tempdir.fargnuplot1D.$random_hash.dat");
}
}
# intHandler is called when SIGINT is received, e.g. if Ctrl+C is pressed
sub intHandler {
print "SIGINT received! Aborting... \n";
&cleanUp();
# return error code != 0
exit(1);
}
$SIG{'INT'} = 'intHandler';
### MAIN ROUTINE ###############################################################
## search for file in commandline arguments
print @ARGV;
$argfile = -1;
for($argvn=0;$argvn<=$#ARGV;$argvn++){
print $argvn." . ".$ARGV[$argvn]. "\n";
if ((-e $ARGV[$argvn])){
$argfile = $argvn;
}
}
# generate random hash
my $digest = new Digest::MD5;
$digest->add(rand());
$random_hash = $digest->hexdigest();
# check if a special directory for temp files was specified
if ($temp) {
$tempdir = "$temp/";
} else {
$tempdir = "./";
}
if ($vr){
$tpprefix = "gasvrad";
$zlabel = "v_rad"
} elsif($vt) {
$tpprefix = "gasvtheta";
$zlabel = "v_theta"
} elsif($t) {
$tpprefix = "gasTemperature";
$zlabel = "Temperature"
} elsif($e) {
$tpprefix = "gasenergy";
$zlabel = "Energy"
} elsif($q) {
$tpprefix = "gasToomre";
$zlabel = "Toomre"
} else {
# this is the default
$tpprefix = "gasdens";
$zlabel = "Gas Density"
}
if ($cblabel) {
$zlabel = $cblabel;
}
$dy = 1;
$rmin =0.;
$dx = 1;
if ($file) {
# user specified file
if (!(-e $file)){
&printusage("File ".$file." does not exist.");
}
if (-d $file){
&printusage("Specified file ".$file." is a directory.");
}
$tp = $file;
if ($nr==0 || $ns ==0){
&printusage("Number of gridcells not specified.");
}
$dx = PI2 / ($ns-1);
} else {
# automatically find newest file
if (!(-e $ARGV[$argfile])) {
&printusage("Directory or file '".$ARGV[$argfile]."' does not exist.");
}
if (!(-d $ARGV[$argfile])) {
# parse fargo .par file
open(FARGOPAR, $ARGV[$argfile]);
while (<FARGOPAR>) {
chomp; # no newline
s/#.*//; # no comments
s/^\s+//; # no leading white
s/\s+$//; # no trailing white
next unless length; # anything left?
my ($var, $value) = split(/[\ \t]/, $_, 2);
if ($var =~ m/\s*nrad\s*/i) {
$_ = $value;
chomp;
m/(\S+)[\ \t]*/;
$nr=$1;
}
if ($var =~ m/\s*nsec\s*/i) {
$_ = $value;
chomp;
m/(\S+)[\ \t]*/;
$ns=$1;
}
if ($var =~ m/\s*outputdir\s*/i) {
$_ = $value;
chomp;
m/(\S+)[\ \t]*/;
$ARGV[$argfile]=$1;
$dir=$1;
}
if ($var =~ m/\s*rmin\s*/i) {
$_ = $value;
chomp;
m/(\S+)[\ \t]*/;
$rmin=$1;
}
if ($var =~ m/\s*rmax\s*/i) {
$_ = $value;
chomp;
m/(\S+)[\ \t]*/;
$rmax=$1;
}
}
close(FARGOPAR);
if ((!$dir)||(!$nr)||(!$ns)) {
&printusage("File ".$ARGV[$argfile]." is not a valid FARGO .par file.");
}
if ($rmin!=$rmax) {
$dy = $rmax-$rmin;
$dy /= $nr;
}
$dx = PI2 /($ns-1);
if ($all) {
print "Using directory ".$ARGV[$argfile]. ", nr=".$nr. ", ns=".$ns. ", nr1D=".$nr1D."\n";
} else {
print "Using directory ".$ARGV[$argfile]. ", nr=".$nr. ", ns=".$ns."\n";
}
}
if ($num =~/^\d+$/) {
## user specified file number
if (!(-e $ARGV[$argfile]."/".$tpprefix.$num.".dat")){
&printusage("File ".$ARGV[$argfile]."/".$tpprefix.$num.".dat does not exist.");
}
$tp = $ARGV[$argfile]."/".$tpprefix.$num.".dat";
if ($all) {
if (!(-e $ARGV[$argfile]."/".$tpprefix."1D".$num.".dat")) {
&printusage("File ".$ARGV[$argfile]."/".$tpprefix."1D".$num.".dat does not exist.");
}
$tp1D = $ARGV[$argfile]."/".$tpprefix."1D".$num.".dat";
}
} else {
if (-d $ARGV[$argfile]) {
## find newest file in directory
@sortedfiles =`ls -lt $ARGV[$argfile]/$tpprefix*.dat | awk '{print \$9;}'| grep -v 1D | grep -v ascii`;
$tp = $sortedfiles[0];
$tp =~ s/\n//;
$num = $tp;
$num =~ s/.*\/\/[^0-9]*(\d*)\.dat/\1/;
print "Newest file: $tp (num=$num)\n";
if (!(-e $tp)) {
&printusage("Directory does not contain FARGO files.");
}
if ($all) {
@sortedfiles =`ls -lt $ARGV[$argfile]/$tpprefix*.dat | awk '{print \$9;}'| grep 1D`;
$tp1D = $sortedfiles[0];
$tp1D =~ s/\n//;
print " ".$tp1D;
if (!(-e $tp1D)) {
&printusage("Directory does not contain FARGO 1D files.");
}
}
}
}
# search for used_rad.dat and read it if available
if (-e $ARGV[$argfile]."/used_rad.dat") {
open(USED_RAD, $ARGV[$argfile]."/used_rad.dat");
@radii = <USED_RAD>;
close(USED_RAD);
} else {
print "Couldn't find any information about spacing (used_rad.dat). Using linear spacing\n";
}
# search for used_rad1D.dat and read it if available
if ($all) {
if (-e $ARGV[$argfile]."/used_rad1D.dat" ) {
open(USED_RAD, $ARGV[$argfile]."/used_rad1D.dat");
@radii1D = <USED_RAD>;
close(USED_RAD);
} else {
print "Couldn't find any information about spacing (used_rad1D.dat). Using linear spacing\n";
}
}
}
print "Plotting ".$tp."\n";
# logarithmic scaling?
if ($l){
# logarithmic scaling
$plot3 = "(log10(\$3))";
$zlabel = $zlabel ." (log10)";
} else {
# linear scaling
$plot3 = "3";
}
# circular plot?
if ($c) {
# circular plot
if ($axis) {
$plot1 = "(\$2*sin(\$1))";
$plot2 = "(\$2*cos(\$1))";
} else {
$plot1 = "(\$2*cos(\$1))";
$plot2 = "(\$2*sin(\$1))";
}
if (!($xlabel)) {
$xlabel = "radius";
}
} else {
# rectangular plot
$plot1 = "1";
$plot2 = "2";
if (!($xlabel)) {
$xlabel = "theta";
}
$xtics='("0" 0, "{/Symbol p}/2" pi/2.0, "{/Symbol p}" pi, "3{/Symbol p}/2" 3.0/2.0*pi, "2{/Symbol p}" 2*pi)';
}
# ylabel
if (!($ylabel)) {
$ylabel = "radius"
}
$ns++;
open(GNUPLOTW, ">$tempdir.fargnuplot.$random_hash.plt");
# enable EPS/PNG output
if ($eps) {
if ($file) {
my ($filename, $directories, $suffix) = fileparse($file, qr/\.[^.]*/);
$outputfile = $directories.$filename;
} else {
$outputfile = $ARGV[$argfile]."/".$tpprefix.$num;
}
print("EPS mode. Writing to ".$outputfile.".eps\n");
# set terminal
print GNUPLOTW "\tset terminal postscript color enhanced\n";
# set output filename
print GNUPLOTW "\tset output \"$outputfile.eps\"\n";
$x=1;
} elsif ($png) {
if ($file) {
my ($filename, $directories, $suffix) = fileparse($file, qr/\.[^.]*/);
$outputfile = $directories.$filename;
} else {
$outputfile = $ARGV[$argfile]."/".$tpprefix.$num;
}
print("PNG mode. Writing to ".$outputfile.".png\n");
# set terminal
print GNUPLOTW "\tset terminal pngcairo enhanced truecolor notransparent size 1280,1024\n";
# set output filename
print GNUPLOTW "\tset output \"$outputfile.png\"\n";
$x=1;
}
# square plot
if ($s) {
print GNUPLOTW "\tset size square\n";
}
print GNUPLOTW "\tset tics out\n";
if ($title) {
print GNUPLOTW "\tset title \"$title\"\n";
} else {
print GNUPLOTW "\tset title \"FARGO file: $tp\"\n";
}
if (!$scale) {
$scale = 1.0;
}
if ($curve) {
# average values plot
$plot3 =~ s/3/2/g;
print GNUPLOTW "\tset xlabel \"radius\"\n";
print GNUPLOTW "\tset autoscale xfix\n";
print GNUPLOTW "\tset ylabel \"$zlabel\"\n";
print GNUPLOTW "\tset style data lines\n";
print GNUPLOTW "\tplot \"$tempdir.fargnuplot.$random_hash.dat\" binary record=${nr} format=\"%double\" u 1:$plot3 notitle \n";
} else {
# normal plot
print GNUPLOTW "\tset pm3d map\n";
print GNUPLOTW "\tset autoscale fix\n";
print GNUPLOTW "\tset xlabel \"$xlabel\"\n";
if ($xtics) {
print GNUPLOTW "\tset xtics $xtics\n";
}
print GNUPLOTW "\tset ylabel \"$ylabel\"\n";
print GNUPLOTW "\tset cblabel \"$zlabel\"\n";
# logarithmic scale for cb axis
if ($lcb) {
print GNUPLOTW "\tset logscale cb\n"
}
# range for cb axis
if ($r) {
print GNUPLOTW "\tset cbrange [$r]\n"
}
# palette for cb axis
if ($palette) {
print GNUPLOTW "\tset palette defined $palette\n";
}
# range for x axis
if ($xr) {
print GNUPLOTW "\tset xrange [$xr]\n"
}
# range for y axis
if (yr) {
print GNUPLOTW "\tset yrange [$yr]\n"
}
if ($clean) {
print GNUPLOTW "\tunset cbtics\n";
print GNUPLOTW "\tunset ytics\n";
print GNUPLOTW "\tunset xtics\n";
print GNUPLOTW "\tunset tics\n";
print GNUPLOTW "\tunset title\n";
print GNUPLOTW "\tunset colorbox\n";
print GNUPLOTW "\tunset xlabel\n";
print GNUPLOTW "\tunset ylabel\n";
print GNUPLOTW "\tset size square\n";
print GNUPLOTW "\tset border 0\n";
print GNUPLOTW "\tset lmargin 0\n";
print GNUPLOTW "\tset rmargin 0\n";
print GNUPLOTW "\tset tmargin 0\n";
print GNUPLOTW "\tset bmargin 0\n";
print GNUPLOTW "\tset size ratio 1\n";
}
# finally plot
if ($all) {
# with 1D
print GNUPLOTW "\tsplot \"$tempdir.fargnuplot1D.$random_hash.dat\" binary record=${ns}x${nr1D} format=\"%double\" u $plot1:$plot2:$plot3 notitle, \".fargnuplot.$random_hash.dat\" binary record=${ns}x${nr} format=\"%double\" u $plot1:$plot2:$plot3 notitle\n";
} else {
# without 1D
print "\tsplot \"$tempdir.fargnuplot.$random_hash.dat\" binary record=${ns}x${nr} format=\"%double\" u $plot1:$plot2:(\$$plot3*$scale) notitle\n";
print GNUPLOTW "\tsplot \"$tempdir.fargnuplot.$random_hash.dat\" binary record=${ns}x${nr} format=\"%double\" u $plot1:$plot2:(\$$plot3*$scale) notitle\n";
}
}
$ns--;
close(GNUPLOTW);
# read data file and write temporary .dat file
open(GNUPLOTW, ">$tempdir.fargnuplot.$random_hash.dat"); binmode(GNUPLOTW);
open(INP,$tp); binmode(INP);
$i=0;
$j=$rmin;
$counti=0;
$countj=0;
$sum=0;
while (read(INP, $buff, 8 )) {
# closing the gap by printing first row twice
if ($i==0) {
$printagain=$buff;
}
if (@radii) {
$rad=$radii[$countj];
} else {
$rad=$j;
}
if (!$curve) {
$coord=pack("dd",$i,$rad);
print GNUPLOTW $coord;
print GNUPLOTW $buff;
} else {
$sum += unpack("d",$buff)/$ns;
}
$counti++;
$i=$dx*$counti;
if ($i>=$ns*$dx){
if (!$curve){
# convert to binary format
$coord=pack("dd",$i,$rad);
print GNUPLOTW $coord;
print GNUPLOTW $printagain;
}else{
print GNUPLOTW pack("dd", $rad, $sum);
$sum=0;
}
$j+=$dy;
$countj++;
$i=0;
$counti=0;
}
}
close GNUPLOTW;
# read 1D data file and write 1D temporary .dat file
if($all) {
open (GNUPLOTW, ">$tempdir.fargnuplot1D.$random_hash.dat"); binmode(GNUPLOTW);
open(INP,$tp1D); binmode(INP);
$i=0;
$j=$rmin;
$countj=0;
$counti=0;
while (read(INP, $buff, 8 ) && ($countj<=$nr1D)) {
if (@radii1D) {
$rad=$radii1D[$countj];
} else {
$rad=$j;
}
if (!$curve ){
while ($counti<($ns+1)) {
$i=$dx*$counti;
$coord=pack("dd",$i,$rad);
print GNUPLOTW $coord;
print GNUPLOTW $buff;
$counti++;
}
} else {
print GNUPLOTW pack("dd", $rad, $buff);
}
$j+=$dy;
$countj++;
$i=0;
$counti=0;
}
close GNUPLOTW;
}
# run gnuplot with persistent window?
if ($p||$x){
$gnuplotoptions = " -persist ";
}else{
$gnuplotoptions = " ";
}
# close gnuplot after plotting?
if ($x) {
$gnuplotoptions .= " $tempdir.fargnuplot.$random_hash.plt";
}else{
$gnuplotoptions .= " $tempdir.fargnuplot.$random_hash.plt -";
}
# Print info how to quit gnuplot only if gnuplot is not quit automatically
if (!($x)) {
print "All data files written. Starting gnuplot. Enter 'q' to quit.\n";
}
# Run gunplot and clean up if some errors occur
if (system(GNUPLOTCMD.$gnuplotoptions)!=0) {
print "Failed running $1\n";
&cleanUp();
# return error code != 0
exit(1);
}
# Finally clean up
&cleanUp();