-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathPlotDMC.pl
executable file
·206 lines (178 loc) · 5.64 KB
/
PlotDMC.pl
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
#!/usr/bin/env perl
use POSIX qw/floor/;
use strict;
use FileHandle;
use Getopt::Long;
my %config = do "/gprojects/qmcpack/qmcpack/utils/setup-qmc-conf.pl";
my $gnuplot = $config{gnuplot};
my $epsfile;
GetOptions('eps=s' => \$epsfile);
my $file = shift;
my $start = shift;
(defined $start) || ($start = 0);
my $end = shift @ARGV;
if (!(defined $end)) {
$end = -1;
}
open(FILE, "$file");
my @filedata;
while (defined (my $line = <FILE>)) {
my @tmp = split('\s+', $line);
push(@filedata, [@tmp]);
}
close(FILE);
my $min;
my $max;
($min, $max) = getMinMax(\@filedata, 2, $start, $end);
my $enTicString = getTicString($min,$max);
my $popmin;
my $popmax;
($popmin, $popmax) = getMinMax(\@filedata, 5, $start, $end);
my $popTicString = getTicString($popmin,$popmax);
my $plotstring = "set title \"$file\"\n";
if ($epsfile) {
$plotstring .= "set term post color enhanced 20\n set output \"$epsfile\"\n";
}
$plotstring .= "set multiplot\n set ylabel \"Energy (Hartrees)\"\n ";
$plotstring .= "set origin 0,0.5\n set size 0.82,0.5\n";
$plotstring .= "set format x \"\"\n set ytics $enTicString \n unset y2tics\n set lmargin 12\n";
if ($end < 0) {
$plotstring .= "plot [$start:][$min:$max] \"$file\" u 1:2 w l notitle, \"\" u 1:5 lt 3 notitle w l \n";
} else {
$plotstring .= "plot [$start:$end][$min:$max] \"$file\" u 1:2 w l notitle, \"\" u 1:5 lt 3 notitle w l \n";
}
$plotstring .= "set format x \n unset title\n set xlabel \"DMC step\"\n set ylabel \"Population (Walkers)\"\n";
$plotstring .= "set xtics\n set ytics $popTicString\n set bmargin 4\n";
if ($end < 0) {
$plotstring .= "set origin 0,0\n plot [$start:][$popmin:$popmax] \"\" u 1:5 w l notitle\n";
} else {
$plotstring .= "set origin 0,0\n plot [$start:$end][$popmin:$popmax] \"\" u 1:5 w l notitle\n";
}
$plotstring .= "set format x \"\"\n";
$plotstring .= "set origin 0.8,0.5\n set size 0.2, 0.5\n set title \"histogram\"\n";
$plotstring .= "set lmargin 0\n unset xlabel\n unset ylabel \n set bmargin 1\n";
$plotstring .= "unset ytics \n set y2tics $enTicString \n set format y2 \"\"\n ";
$plotstring .= getGPLHistString(\@filedata, 2, $start, $end);
$plotstring .= "set origin 0.8,0.0\n set size 0.2, 0.5\n unset title \n set y2tics $popTicString \n";
$plotstring .= "set format y2 \"\"\n set xlabel \"counts\"\n set bmargin 4\n";
$plotstring .= getGPLHistString(\@filedata, 5, $start, $end);
$plotstring .= "unset multiplot\n";
unless($epsfile) {
$plotstring .= "pause -1\n";
}
open(GPL, "|$gnuplot");
GPL->autoflush(1);
print GPL $plotstring;
unless($epsfile) {
my $blah = <>; # Hack to leave graph up until user hits a key
}
close(GPL);
#############################################################################
sub getTicString {
use POSIX qw/floor ceil/;
my $low = shift;
my $high = shift;
# print " $low $high\n";
my $range = $high - $low;
my $scale = 10 ** floor(log($range)/log(10));
my $ntics = int($range/$scale);
if ($ntics == 1) {
$scale *= 0.2;
$ntics = int($range/$scale);
} elsif ($ntics == 2) {
$scale *= 0.4;
$ntics = int($range/$scale);
} elsif ($ntics == 3) {
$scale *= 0.4;
$ntics = int($range/$scale);
} elsif ($ntics == 4) {
$scale *= 0.5;
$ntics = int($range/$scale);
} elsif ($ntics == 5) {
$scale *= 0.8;
$ntics = int($range/$scale);
}
my $lowval = ceil($low/$scale)*$scale;
my $highval = floor($high/$scale)*$scale;
my $ticstring = "$lowval,$scale,$highval";
return $ticstring;
}
sub getMinMax {
my $ArrayRef = shift;
my $column = shift;
my $start = shift;
my $stop = shift;
if ($stop < 0) {
$stop = $#{$ArrayRef};
}
my $min = 1000000000;
my $max = -1000000000;
my $numsamples = 0;
for (my $i = $start+1; $i <= $stop; $i++) {
my $data = ${$ArrayRef}[$i][$column];
if ($data < $min) {
$min = $data;
}
if ($data > $max) {
$max = $data;
}
$numsamples++;
}
return ($min, $max, $numsamples);
}
sub getGPLHistString {
my $ArrayRef = shift;
my $column = shift;
my $start = shift;
my $stop = shift;
my $min;
my $max;
my $numsamples;
($min, $max, $numsamples) = getMinMax($ArrayRef,$column,$start,$stop);
my @arr = getColumn($ArrayRef,$column,$start,$stop);
my $numbins = floor($numsamples / 50);
my @histdata;
for (my $i = 0; $i < $numbins; $i++) {
push @histdata, 0;
}
my $binwidth = ($max-$min)/($numbins+0.0);
my $maxcount = 0;
foreach my $i (@arr) {
my $binnum = floor(($i-$min)/$binwidth);
if ($binnum == $numbins || $binnum < 0) {
$binnum--;
}
$histdata[$binnum]++;
if ($histdata[$binnum] > $maxcount) {
$maxcount = $histdata[$binnum];
}
}
my $plotmax = $maxcount+2;
# my $plotstring = "unset ytics; set y2tics\n";
my $plotstring .= "plot [$plotmax:0][$min:$max] \"-\" u 1:2 w l notitle\n ";
# my $plotstring .= "plot [10:0][$min:$max] \"-\" u 1:2 w l notitle\n ";
for (my $i = 0; $i < $numbins; $i++) {
my $boxxmin = $min+$i*$binwidth;
my $boxxmax = $min+($i+1)*$binwidth;
my $boxymax = $histdata[$i];
my $tag = $i+1;
$plotstring .= "0 $boxxmin\n $boxymax $boxxmin\n $boxymax $boxxmax\n 0 $boxxmax\n";
}
$plotstring .= "end \n";
return $plotstring;
}
sub getColumn {
my $matrix = shift;
my $colnum = shift;
my $startnum = shift;
my $end = shift;
if ($end < 0) {
$end = $#{$matrix};
}
$startnum || ($startnum = 0);
my @outmat;
for (my $i = $startnum+1; $i <= $end; $i++) {
push @outmat, ${$matrix}[$i][$colnum];
}
return @outmat;
}