Root/nanonote-files/example-files/data/Examples/lua-plplot-examples/x16.lua

1--[[ $Id: x16.lua 10304 2009-08-20 09:05:43Z andrewross $
2
3    plshade demo, using color fill.
4
5  Copyright (C) 2008 Werner Smekal
6
7  This file is part of PLplot.
8
9  PLplot is free software you can redistribute it and/or modify
10  it under the terms of the GNU General Library Public License as published
11  by the Free Software Foundation either version 2 of the License, or
12  (at your option) any later version.
13
14  PLplot is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Library General Public License for more details.
18
19  You should have received a copy of the GNU Library General Public License
20  along with PLplot if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22--]]
23
24-- initialise Lua bindings for PLplot examples.
25dofile("plplot_examples.lua")
26
27-- Fundamental settings. See notes[] for more info.
28ns = 20 -- Default number of shade levels
29nx = 35 -- Default number of data points in x
30ny = 46 -- Default number of data points in y
31exclude = 0 -- By default do not plot a page illustrating
32              -- exclusion. API is probably going to change
33              -- anyway, and cannot be reproduced by any
34                      -- front end other than the C one.
35
36-- polar plot data
37PERIMETERPTS = 100
38
39-- Transformation function
40tr = {}
41
42function mypltr(x, y)
43    tx = tr[1] * x + tr[2] * y + tr[3]
44    ty = tr[4] * x + tr[5] * y + tr[6]
45    
46    return tx, ty
47end
48
49----------------------------------------------------------------------------
50-- f2mnmx
51--
52-- Returns min & max of input 2d array.
53----------------------------------------------------------------------------
54function f2mnmx(f, nx, ny)
55  fmax = f[1][1]
56  fmin = fmax
57
58  for i = 1, nx do
59    for j = 1, ny do
60      fmax = math.max(fmax, f[i][j])
61      fmin = math.min(fmin, f[i][j])
62    end
63  end
64  
65  return fmin, fmax
66end
67
68
69function zdefined(x, y)
70  z = math.sqrt(x^2 + y^2)
71
72  return z<0.4 or z>0.6
73end
74
75
76----------------------------------------------------------------------------
77-- main
78--
79-- Does several shade plots using different coordinate mappings.
80----------------------------------------------------------------------------
81
82px = {}
83py = {}
84
85fill_width = 2
86cont_color = 0
87cont_width = 0
88
89-- Parse and process command line arguments
90pl.parseopts(arg, pl.PL_PARSE_FULL)
91
92-- Load colour palettes
93pl.spal0("cmap0_black_on_white.pal");
94pl.spal1("cmap1_gray.pal",1);
95
96-- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display
97pl.scmap0n(3)
98
99-- Initialize plplot
100pl.init()
101
102-- Set up transformation function
103tr = { 2/(nx-1), 0, -1, 0, 2/(ny-1), -1 }
104
105-- Allocate data structures
106clevel = {}
107shedge = {}
108z = {}
109w = {}
110
111-- Set up data array
112for i = 1, nx do
113    x = (i-1 - math.floor(nx/2))/math.floor(nx/2)
114  z[i] = {}
115  w[i] = {}
116    for j = 1, ny do
117    y = (j-1 - math.floor(ny/2))/math.floor(ny/2)-1
118    z[i][j] = -math.sin(7*x) * math.cos(7*y) + x^2 - y^2
119    w[i][j] = -math.cos(7*x) * math.sin(7*y) + 2*x*y
120    end
121end
122
123zmin, zmax = f2mnmx(z, nx, ny)
124for i = 1, ns do
125    clevel[i] = zmin + (zmax-zmin)*(i-0.5)/ns
126end
127
128for i = 1, ns+1 do
129    shedge[i] = zmin + (zmax-zmin)*(i-1)/ns
130end
131
132-- Set up coordinate grids
133cgrid1 = {}
134cgrid1["xg"] = {}
135cgrid1["yg"] = {}
136cgrid1["nx"] = nx
137cgrid1["ny"] = ny
138
139cgrid2 = {}
140cgrid2["xg"] = {}
141cgrid2["yg"] = {}
142cgrid2["nx"] = nx
143cgrid2["ny"] = ny
144
145for i = 1, nx do
146  cgrid2["xg"][i] = {}
147  cgrid2["yg"][i] = {}
148    for j = 1, ny do
149    x, y = mypltr(i-1, j-1)
150
151    argx = x*math.pi/2
152    argy = y*math.pi/2
153    distort = 0.4
154
155    cgrid1["xg"][i] = x + distort * math.cos(argx)
156    cgrid1["yg"][j] = y - distort * math.cos(argy)
157
158    cgrid2["xg"][i][j] = x + distort * math.cos(argx) * math.cos(argy)
159    cgrid2["yg"][i][j] = y - distort * math.cos(argx) * math.cos(argy)
160  end
161end
162
163-- Plot using identity transform
164pl.adv(0)
165pl.vpor(0.1, 0.9, 0.1, 0.9)
166pl.wind(-1, 1, -1, 1)
167
168pl.psty(0)
169
170pl.shades(z, -1, 1, -1, 1, shedge, fill_width, cont_color, cont_width, 1)
171
172pl.col0(1)
173pl.box("bcnst", 0, 0, "bcnstv", 0, 0)
174pl.col0(2)
175
176--pl.cont(w, 1, nx, 1, ny, clevel, mypltr, {})
177pl.lab("distance", "altitude", "Bogon density")
178
179-- Plot using 1d coordinate transform
180
181-- Load colour palettes
182pl.spal0("cmap0_black_on_white.pal");
183pl.spal1("cmap1_blue_yellow.pal",1);
184
185-- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display
186pl.scmap0n(3);
187
188pl.adv(0)
189pl.vpor(0.1, 0.9, 0.1, 0.9)
190pl.wind(-1, 1, -1, 1)
191
192pl.psty(0)
193
194pl.shades(z, -1, 1, -1, 1, shedge, fill_width, cont_color, cont_width, 1, "pltr1", cgrid1)
195
196pl.col0(1)
197pl.box("bcnst", 0, 0, "bcnstv", 0, 0)
198pl.col0(2)
199pl.lab("distance", "altitude", "Bogon density")
200
201-- Plot using 2d coordinate transform
202
203-- Load colour palettes
204pl.spal0("cmap0_black_on_white.pal");
205pl.spal1("cmap1_blue_red.pal",1);
206
207-- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display
208pl.scmap0n(3);
209
210pl.adv(0)
211pl.vpor(0.1, 0.9, 0.1, 0.9)
212pl.wind(-1, 1, -1, 1)
213
214pl.psty(0)
215
216pl.shades(z, -1, 1, -1, 1, shedge, fill_width, cont_color, cont_width, 0, "pltr2", cgrid2)
217
218pl.col0(1)
219pl.box("bcnst", 0, 0, "bcnstv", 0, 0)
220pl.col0(2)
221pl.cont(w, 1, nx, 1, ny, clevel, "pltr2", cgrid2)
222
223pl.lab("distance", "altitude", "Bogon density, with streamlines")
224
225-- Plot using 2d coordinate transform
226
227-- Load colour palettes
228pl.spal0("");
229pl.spal1("",1);
230
231-- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display
232pl.scmap0n(3);
233
234pl.adv(0)
235pl.vpor(0.1, 0.9, 0.1, 0.9)
236pl.wind(-1, 1, -1, 1)
237
238pl.psty(0)
239
240pl.shades(z, -1, 1, -1, 1, shedge, fill_width, 2, 3, 0, "pltr2", cgrid2)
241
242pl.col0(1)
243pl.box("bcnst", 0, 0, "bcnstv", 0, 0)
244pl.col0(2)
245
246pl.lab("distance", "altitude", "Bogon density")
247
248-- Note this exclusion API will probably change.
249
250-- Plot using 2d coordinate transform and exclusion
251if exclude~=0 then
252
253    -- Load colour palettes
254  pl.spal0("cmap0_black_on_white.pal");
255  pl.spal1("cmap1_gray.pal",1);
256    
257    -- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display
258  pl.scmap0n(3);
259
260  pl.adv(0)
261  pl.vpor(0.1, 0.9, 0.1, 0.9)
262  pl.wind(-1, 1, -1, 1)
263
264  plpsty(0)
265
266  pl.shades(z, zdefined, -1, 1, -1, 1, shedge, fill_width, cont_color, cont_width,
267            0, "pltr2", cgrid2)
268
269  pl.col0(1)
270  pl.box("bcnst", 0, 0, "bcnstv", 0, 0)
271
272  pl.lab("distance", "altitude", "Bogon density with exclusion")
273end
274
275-- Example with polar coordinates.
276
277-- Load colour palettes
278pl.spal0("cmap0_black_on_white.pal");
279pl.spal1("cmap1_gray.pal",1);
280
281-- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display
282pl.scmap0n(3);
283
284pl.adv(0)
285pl.vpor(.1, .9, .1, .9)
286pl.wind(-1, 1, -1, 1)
287
288pl.psty(0)
289
290-- Build new coordinate matrices.
291for i = 1, nx do
292  r = (i-1)/(nx-1)
293    for j = 1, ny do
294       t = 2*math.pi/(ny-1)*(j-1)
295       cgrid2["xg"][i][j] = r*math.cos(t)
296       cgrid2["yg"][i][j] = r*math.sin(t)
297       z[i][j] = math.exp(-r^2)*math.cos(5*math.pi*r)*math.cos(5*t)
298    end
299end
300
301-- Need a new shedge to go along with the new data set.
302zmin, zmax = f2mnmx(z, nx, ny)
303
304for i = 1, ns+1 do
305    shedge[i] = zmin + (zmax-zmin)*(i-1)/ns
306end
307
308-- Now we can shade the interior region.
309pl.shades(z, -1, 1, -1, 1, shedge, fill_width, cont_color, cont_width, 0, "pltr2", cgrid2)
310
311-- Now we can draw the perimeter. (If do before, shade stuff may overlap.)
312for i = 1, PERIMETERPTS do
313   t = 2*math.pi/(PERIMETERPTS-1)*(i-1)
314   px[i] = math.cos(t)
315   py[i] = math.sin(t)
316end
317pl.col0(1)
318pl.line(px, py)
319            
320-- And label the plot.
321pl.col0(2)
322pl.lab( "", "", "Tokamak Bogon Instability" )
323
324pl.plend()
325

Archive Download this file



interactive