plplot 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,280 @@
1
+ #!/usr/bin/env ruby
2
+ $-w = true if $0 == __FILE__
3
+
4
+ require 'rubygems'
5
+ require 'plplot'
6
+ include PLplot
7
+ include PLplot::PL
8
+ include Math
9
+
10
+ # Grid data demo
11
+ #
12
+ # Copyright (C) 2004 Joao Cardoso
13
+ #
14
+ # This file is part of PLplot.
15
+ #
16
+ # PLplot is free software; you can redistribute it and/or modify
17
+ # it under the terms of the GNU General Library Public License as published
18
+ # by the Free Software Foundation; either version 2 of the License, or
19
+ # (at your option) any later version.
20
+ #
21
+ # PLplot is distributed in the hope that it will be useful,
22
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
23
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
+ # GNU Library General Public License for more details.
25
+ #
26
+ # You should have received a copy of the GNU Library General Public License
27
+ # along with PLplot; if not, write to the Free Software
28
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29
+
30
+ # Options data structure definition
31
+
32
+ # Defaults for user options. Use global variables to track user options since
33
+ # that matches the C example most closely.
34
+
35
+ $pts = 500
36
+ $xp = 25
37
+ $yp = 20
38
+ $nl = 16
39
+ $knn_order = 20
40
+ $threshold = 1.001
41
+ $wmin = -1e3 # No option for this?
42
+ $randn = false
43
+ $rosen = false
44
+
45
+ PLOP = PLOptionParser.new do |op|
46
+ op.separator('')
47
+ op.separator('x21 options:')
48
+ op.on('--npts NPOINTS', Integer, 'Specify number of random points',
49
+ "to generate [#{$pts}]") do |o|
50
+ $npts = o
51
+ end
52
+ op.on('--randn', 'Normal instead of uniform sampling',
53
+ 'the effective number of points will be',
54
+ 'smaller than the specified number') do
55
+ $randn = true
56
+ end
57
+ op.on('--rosen', 'Generate using the Rosenbrock function.') do
58
+ $rosen = true
59
+ end
60
+ op.on('--nx NXPOINTS', Integer, "Specify grid x dimension [#{$xp}]") do |o|
61
+ $xp = o
62
+ end
63
+ op.on('--ny NYPOINTS', Integer, "Specify grid y dimension [#{$yp}]") do |o|
64
+ $yp = o
65
+ end
66
+ op.on('--nlevel NLEVELS', Integer, "Specify number of contour levels [#{$nl}]") do |o|
67
+ $nl = o
68
+ end
69
+ op.on('--knn_order ORDER', Integer, "Specify the number of neighbors [#{$knn_order}]") do |o|
70
+ $knn_order = o
71
+ end
72
+ op.on('--threshold FLOAT', Float, "Specify what a thin triangle is", "[1 < [#{$threshold}] < 2]") do |o|
73
+ $threshold = o
74
+ end
75
+ end
76
+
77
+ # Makes transliteration from C example easier
78
+ def pow(a,b)
79
+ a ** b
80
+ end
81
+
82
+ def cmap1_init
83
+ i = [ 0.0, 1.0 ] # left, right boundary
84
+
85
+ h = [ 240, 0 ] # blue -> green -> yellow -> red
86
+
87
+ l = [ 0.6, 0.6 ]
88
+
89
+ s = [ 0.8, 0.8 ]
90
+
91
+ plscmap1n(256)
92
+ plscmap1l(0, i, h, l, s)
93
+ end
94
+
95
+ def create_grid(px, py)
96
+ x = NArray.float(px)
97
+ y = NArray.float(py)
98
+
99
+ px.times do |i|
100
+ x[i] = XMIN + (XMAX - XMIN) * i / (px - 1.0)
101
+ end
102
+
103
+ py.times do |i|
104
+ y[i] = YMIN + (YMAX - YMIN) * i / (py - 1.0)
105
+ end
106
+
107
+ [x, y]
108
+ end
109
+
110
+ def create_data(pts, randn, rosen)
111
+ x = NArray.float(pts)
112
+ y = NArray.float(pts)
113
+ z = NArray.float(pts)
114
+
115
+ pts.times do |i|
116
+ xt = (XMAX - XMIN) * plrandd
117
+ yt = (YMAX - YMIN) * plrandd
118
+ if !randn
119
+ x[i] = xt + XMIN
120
+ y[i] = yt + YMIN
121
+ else # std=1, meaning that many points are outside the plot range
122
+ x[i] = sqrt(-2 * log(xt)) * cos(2 * PI * yt) + XMIN
123
+ y[i] = sqrt(-2 * log(xt)) * sin(2 * PI * yt) + YMIN
124
+ end
125
+ if (!rosen)
126
+ r = sqrt(x[i] * x[i] + y[i] * y[i])
127
+ z[i] = exp(-r * r) * cos(2 * PI * r)
128
+ else
129
+ z[i] = log(pow(1 - x[i], 2) + 100 * pow(y[i] - pow(x[i], 2), 2))
130
+ end
131
+ end
132
+
133
+ [x, y, z]
134
+ end
135
+
136
+ title = [
137
+ "Cubic Spline Approximation",
138
+ "Delaunay Linear Interpolation",
139
+ "Natural Neighbors Interpolation",
140
+ "KNN Inv. Distance Weighted",
141
+ "3NN Linear Interpolation",
142
+ "4NN Around Inv. Dist. Weighted"
143
+ ]
144
+
145
+ opt = [ 0, 0, 0, 0, 0, 0 ]
146
+
147
+ XMIN = YMIN = -0.2
148
+ XMAX = YMAX = 0.6
149
+
150
+ PLOP.parse!
151
+
152
+ opt[2] = $wmin
153
+ opt[3] = $knn_order
154
+ opt[4] = $threshold
155
+
156
+ # Initialize plplot
157
+
158
+ plinit
159
+
160
+ # Initialise random number generator
161
+ plseed(5489)
162
+
163
+ x, y, z = create_data($pts, $randn, $rosen); # the sampled data
164
+ zmin, zmax = plMinMax2dGrid(z)
165
+
166
+ xg, yg = create_grid($xp, $yp) # grid the data at
167
+ zg = NArray.float($yp, $xp) # the output grided data
168
+ clev = NArray.float($nl)
169
+
170
+ # printf("Npts=%d gridx=%d gridy=%d", $pts, $xp, $yp)
171
+ plcol0(1)
172
+ plenv(XMIN, XMAX, YMIN, YMAX, 2, 0)
173
+ plcol0(15)
174
+ pllab("X", "Y", "The original data sampling")
175
+ plcol0(2)
176
+ plpoin(x, y, 5)
177
+ pladv(0)
178
+
179
+ plssub(3, 2)
180
+
181
+ 2.times do |k|
182
+ pladv(0)
183
+ (1...7).each do |alg|
184
+ plgriddata(x, y, z, zg, alg, opt[alg - 1], xg, yg)
185
+
186
+ # - CSA can generate NaNs (only interpolates?!).
187
+ # - DTLI and NNI can generate NaNs for points outside the convex hull
188
+ # of the data points.
189
+ # - NNLI can generate NaNs if a sufficiently thick triangle is not found
190
+ #
191
+ # PLplot should be NaN/Inf aware, but changing it now is quite a job...
192
+ # so, instead of not plotting the NaN regions, a weighted average over
193
+ # the neighbors is done.
194
+
195
+ if (alg == GRID_CSA || alg == GRID_DTLI || alg == GRID_NNLI || alg == GRID_NNI)
196
+ $xp.times do |i|
197
+ $yp.times do |j|
198
+ if zg[j,i].nan? # average (IDW) over the 8 neighbors
199
+
200
+ zg[j,i] = 0.0; dist = 0.0
201
+
202
+ (i-1..i+1).each do |ii|
203
+ next unless (0...$xp) === ii
204
+
205
+ (j-1..j+1).each do |jj|
206
+ next unless (0...$yp) === jj
207
+
208
+ if !zg[jj,ii].nan?
209
+ d = (ii - i).abs + (jj - j).abs == 1 ? 1.0 : 1.4142
210
+ zg[j,i] += zg[jj,ii] / (d * d)
211
+ dist += d
212
+ end
213
+ end
214
+ end
215
+ if (dist != 0.0)
216
+ zg[j,i] /= dist
217
+ else
218
+ zg[j,i] = zmin
219
+ end
220
+ end
221
+ end
222
+ end
223
+ end
224
+
225
+ lzmin, lzmax = plMinMax2dGrid(zg)
226
+
227
+ lzmin = zmin if zmin < lzmin
228
+ lzmax = zmax if zmax > lzmax
229
+
230
+ # Increase limits slightly to prevent spurious contours
231
+ # due to rounding errors
232
+ lzmin = lzmin - 0.01
233
+ lzmax = lzmax + 0.01
234
+
235
+ plcol0(1)
236
+
237
+ pladv(alg)
238
+
239
+ if k == 0
240
+ $nl.times do |i|
241
+ clev[i] = lzmin + (lzmax - lzmin) / ($nl - 1) * i
242
+ end
243
+
244
+ plenv0(XMIN, XMAX, YMIN, YMAX, 2, 0)
245
+ plcol0(15)
246
+ pllab("X", "Y", title[alg - 1])
247
+ plshade(zg, XMIN..XMAX, YMIN..YMAX, clev,
248
+ 1, # fill_width
249
+ 0, # cont_color
250
+ 1, # cont_width
251
+ 1 # rectangular?
252
+ )
253
+ plcol0(2)
254
+ else
255
+ $nl.times do |i|
256
+ clev[i] = lzmin + (lzmax - lzmin) / ($nl - 1) * i
257
+ end
258
+
259
+ cmap1_init
260
+ plvpor(0.0, 1.0, 0.0, 0.9)
261
+ plwind(-1.1, 0.75, -0.65, 1.20)
262
+
263
+ # For the comparison to be fair, all plots should have the
264
+ # same z values, but to get the max/min of the data generated
265
+ # by all algorithms would imply two passes. Keep it simple.
266
+ #
267
+ # plw3d(1, 1, 1, XMIN, XMAX, YMIN, YMAX, zmin, zmax, 30, -60)
268
+
269
+ plw3d(1, 1, 1, XMIN, XMAX, YMIN, YMAX, lzmin, lzmax, 30, -40)
270
+ plbox3("bntu", "X", 0, 0,
271
+ "bntu", "Y", 0.0, 0,
272
+ "bcdfntu", "Z", 0.5, 0)
273
+ plcol0(15)
274
+ pllab("", "", title[alg - 1])
275
+ plot3dc(xg, yg, zg, DRAW_LINEXY | MAG_COLOR | BASE_CONT, clev)
276
+ end
277
+ end
278
+ end
279
+
280
+ plend
@@ -0,0 +1,242 @@
1
+ #!/usr/bin/env ruby
2
+ $-w = true if $0 == __FILE__
3
+
4
+ require 'rubygems'
5
+ require 'plplot'
6
+ include PLplot
7
+ include Math
8
+
9
+ # Simple vector plot example
10
+ # Copyright (C) 2004 Andrew Ross <andrewross@users.sourceforge.net>
11
+ # Copyright (C) 2004 Rafael Laboissiere
12
+ #
13
+ #
14
+ # This file is part of PLplot.
15
+ #
16
+ # PLplot is free software; you can redistribute it and/or modify
17
+ # it under the terms of the GNU General Library Public License as published
18
+ # by the Free Software Foundation; either version 2 of the License, or
19
+ # (at your option) any later version.
20
+ #
21
+ # PLplot is distributed in the hope that it will be useful,
22
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
23
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
+ # GNU Library General Public License for more details.
25
+ #
26
+ # You should have received a copy of the GNU Library General Public License
27
+ # along with PLplot; if not, write to the Free Software
28
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29
+
30
+ # Makes transliteration from C example easier
31
+ def pow(a,b)
32
+ a ** b
33
+ end
34
+
35
+ # Pairs of points making the line segments used to plot the user defined arrow
36
+ ARROW_X = [ -0.5, 0.5, 0.3, 0.5, 0.3, 0.5 ]
37
+ ARROW_Y = [ 0.0, 0.0, 0.2, 0.0, -0.2, 0.0 ]
38
+ ARROW2_X = [ -0.5, 0.3, 0.3, 0.5, 0.3, 0.3 ]
39
+ ARROW2_Y = [ 0.0, 0.0, 0.2, 0.0, -0.2, 0.0 ]
40
+
41
+ # Generates several simple vector plots.
42
+
43
+ # Vector plot of the circulation about the origin
44
+ def circulation
45
+ nx = 20
46
+ ny = 20
47
+
48
+ dx = 1.0
49
+ dy = 1.0
50
+
51
+ xmin = -nx / 2 * dx
52
+ xmax = nx / 2 * dx
53
+ ymin = -ny / 2 * dy
54
+ ymax = ny / 2 * dy
55
+
56
+ xg = NArray.float(ny, nx)
57
+ yg = NArray.float(ny, nx)
58
+ u = NArray.float(ny, nx)
59
+ v = NArray.float(ny, nx)
60
+
61
+ # Create data - circulation around the origin.
62
+ nx.times do |i|
63
+ x = (i - nx / 2 + 0.5) * dx
64
+ ny.times do |j|
65
+ y = (j - ny / 2 + 0.5) * dy
66
+ xg[j,i] = x
67
+ yg[j,i] = y
68
+ u[j,i] = y
69
+ v[j,i] = -x
70
+ end
71
+ end
72
+
73
+ # Plot vectors with default arrows
74
+ plenv(xmin, xmax, ymin, ymax, 0, 0)
75
+ pllab("(x)", "(y)", "#frPLplot Example 22 - circulation")
76
+ plcol0(2)
77
+ plvect(u, v, xg, yg)
78
+ plcol0(1)
79
+ end
80
+
81
+ # Vector plot of flow through a constricted pipe
82
+ def constriction
83
+ nx = 20
84
+ ny = 20
85
+
86
+ dx = 1.0
87
+ dy = 1.0
88
+
89
+ xmin = -nx / 2 * dx
90
+ xmax = nx / 2 * dx
91
+ ymin = -ny / 2 * dy
92
+ ymax = ny / 2 * dy
93
+
94
+ xg = NArray.float(ny, nx)
95
+ yg = NArray.float(ny, nx)
96
+ u = NArray.float(ny, nx)
97
+ v = NArray.float(ny, nx)
98
+
99
+ q = 2.0
100
+ nx.times do |i|
101
+ x = (i - nx / 2 + 0.5) * dx
102
+ ny.times do |j|
103
+ y = (j - ny / 2 + 0.5) * dy
104
+ xg[j,i] = x
105
+ yg[j,i] = y
106
+ b = ymax / 4.0 * (3 - cos(PI * x / xmax))
107
+ if (y.abs < b)
108
+ dbdx = ymax / 4.0 * sin(PI * x / xmax) * y / b
109
+ u[j,i] = q * ymax / b
110
+ v[j,i] = dbdx * u[j,i]
111
+ else
112
+ u[j,i] = 0.0
113
+ v[j,i] = 0.0
114
+ end
115
+ end
116
+ end
117
+
118
+ plenv(xmin, xmax, ymin, ymax, 0, 0)
119
+ pllab("(x)", "(y)", "#frPLplot Example 22 - constriction")
120
+ plcol0(2)
121
+ plvect(u, v, xg, yg, -0.5)
122
+ plcol0(1)
123
+ end
124
+
125
+
126
+ # Vector plot of the gradient of a shielded potential (see example 9)
127
+ def potential
128
+ nper = 100
129
+ nlevel = 10
130
+ nr = 20
131
+ ntheta = 20
132
+
133
+ px = NArray.float(nper)
134
+ py = NArray.float(nper)
135
+ clevel = NArray.float(nlevel)
136
+
137
+ # Create data to be plotted
138
+ xg = NArray.float(ntheta, nr)
139
+ yg = NArray.float(ntheta, nr)
140
+ u = NArray.float(ntheta, nr)
141
+ v = NArray.float(ntheta, nr)
142
+ z = NArray.float(ntheta, nr)
143
+
144
+ # Potential inside a conducting cylinder (or sphere) by method of images.
145
+ # Charge 1 is placed at (d1, d1), with image charge at (d2, d2).
146
+ # Charge 2 is placed at (d1, -d1), with image charge at (d2, -d2).
147
+ # Also put in smoothing term at small distances.
148
+
149
+ rmax = nr.to_f
150
+
151
+ eps = 2.0
152
+
153
+ q1 = 1.0
154
+ d1 = rmax / 4.0
155
+
156
+ q1i = -q1 * rmax / d1
157
+ d1i = pow(rmax, 2.0) / d1
158
+
159
+ q2 = -1.0
160
+ d2 = rmax / 4.0
161
+
162
+ q2i = -q2 * rmax / d2
163
+ d2i = pow(rmax, 2.0) / d2
164
+
165
+ nr.times do |i|
166
+ r = 0.5 + i
167
+ ntheta.times do |j|
168
+ theta = 2.0 * PI / (ntheta - 1) * (0.5 + j)
169
+ x = r * cos(theta)
170
+ y = r * sin(theta)
171
+ xg[j,i] = x
172
+ yg[j,i] = y
173
+ div1 = sqrt(pow(x - d1, 2.0) + pow(y - d1, 2.0) + pow(eps, 2.0))
174
+ div1i = sqrt(pow(x - d1i, 2.0) + pow(y - d1i, 2.0) + pow(eps, 2.0))
175
+ div2 = sqrt(pow(x - d2, 2.0) + pow(y + d2, 2.0) + pow(eps, 2.0))
176
+ div2i = sqrt(pow(x - d2i, 2.0) + pow(y + d2i, 2.0) + pow(eps, 2.0))
177
+ z[j,i] = q1 / div1 + q1i / div1i + q2 / div2 + q2i / div2i
178
+ u[j,i] = -q1 * (x - d1) / pow(div1, 3.0) - q1i * (x - d1i) / pow(div1i, 3.0) \
179
+ - q2 * (x - d2) / pow(div2, 3.0) - q2i * (x - d2i) / pow(div2i, 3.0)
180
+ v[j,i] = -q1 * (y - d1) / pow(div1, 3.0) - q1i * (y - d1i) / pow(div1i, 3.0) \
181
+ - q2 * (y + d2) / pow(div2, 3.0) - q2i * (y + d2i) / pow(div2i, 3.0)
182
+ end
183
+ end
184
+
185
+ xmin, xmax = plMinMax2dGrid(xg)
186
+ ymin, ymax = plMinMax2dGrid(yg)
187
+ zmin, zmax = plMinMax2dGrid( z)
188
+
189
+ plenv(xmin, xmax, ymin, ymax, 0, 0)
190
+ pllab("(x)", "(y)", "#frPLplot Example 22 - potential gradient vector plot")
191
+ # Plot contours of the potential
192
+ dz = (zmax - zmin) / nlevel
193
+ nlevel.times do |i|
194
+ clevel[i] = zmin + (i + 0.5) * dz
195
+ end
196
+ plcol0(3)
197
+ pllsty(2)
198
+ plcont(z, nil, nil, clevel, xg, yg)
199
+ pllsty(1)
200
+ plcol0(1)
201
+
202
+ # Plot the vectors of the gradient of the potential
203
+ plcol0(2)
204
+ plvect(u, v, xg, yg, 25.0)
205
+ plcol0(1)
206
+
207
+ # Plot the perimeter of the cylinder
208
+ nper.times do |i|
209
+ theta = (2.0 * PI / (nper - 1)) * i
210
+ px[i] = rmax * cos(theta)
211
+ py[i] = rmax * sin(theta)
212
+ end
213
+ plline(px, py)
214
+
215
+ end
216
+
217
+ # Parse and process command line arguments
218
+
219
+ PLOptionParser.parse!
220
+
221
+ # Initialize plplot
222
+
223
+ plinit
224
+
225
+ circulation
226
+
227
+ fill = 0
228
+
229
+ # Set arrow style using ARROW_X and ARROW_Y then
230
+ # plot using these arrows.
231
+ plsvect(ARROW_X, ARROW_Y, fill)
232
+ constriction
233
+
234
+ # Set arrow style using ARROW2_X and ARROW2_Y then
235
+ # plot using these filled arrows. */
236
+ fill = 1
237
+ plsvect(ARROW2_X, ARROW2_Y, fill)
238
+ constriction
239
+
240
+ potential
241
+
242
+ plend