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

1--[[ $Id: x22.lua 9526 2009-02-13 22:06:13Z smekal $
2
3    Simple vector plot example
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-- Pairs of points making the line segments used to plot the user defined arrow
28arrow_x = { -0.5, 0.5, 0.3, 0.5, 0.3, 0.5 }
29arrow_y = { 0, 0, 0.2, 0, -0.2, 0 }
30arrow2_x = { -0.5, 0.3, 0.3, 0.5, 0.3, 0.3 }
31arrow2_y = { 0, 0, 0.2, 0, -0.2, 0 }
32
33
34-- Vector plot of the circulation about the origin
35function circulation()
36    nx = 20
37    ny = 20
38    dx = 1
39    dy = 1
40
41    xmin = -nx/2*dx
42    xmax = nx/2*dx
43    ymin = -ny/2*dy
44    ymax = ny/2*dy
45
46  cgrid2 = {}
47    cgrid2["xg"] = {}
48    cgrid2["yg"] = {}
49    cgrid2["nx"] = nx
50    cgrid2["ny"] = ny
51    u = {}
52    v = {}
53    
54    -- Create data - circulation around the origin.
55    for i = 1, nx do
56        x = (i-1-nx/2+0.5)*dx
57        cgrid2["xg"][i] = {}
58        cgrid2["yg"][i] = {}
59        u[i] = {}
60        v[i] = {}
61        for j=1, ny do
62            y = (j-1-ny/2+0.5)*dy
63            cgrid2["xg"][i][j] = x
64            cgrid2["yg"][i][j] = y
65            u[i][j] = y
66            v[i][j] = -x
67        end
68    end
69
70    -- Plot vectors with default arrows
71    pl.env(xmin, xmax, ymin, ymax, 0, 0)
72    pl.lab("(x)", "(y)", "#frPLplot Example 22 - circulation")
73    pl.col0(2)
74    pl.vect(u, v, 0, "pltr2", cgrid2 )
75    pl.col0(1)
76end
77
78
79-- Vector plot of flow through a constricted pipe
80function constriction()
81    nx = 20
82    ny = 20
83    dx = 1
84    dy = 1
85
86    xmin = -nx/2*dx
87    xmax = nx/2*dx
88    ymin = -ny/2*dy
89    ymax = ny/2*dy
90
91  cgrid2 = {}
92    cgrid2["xg"] = {}
93    cgrid2["yg"] = {}
94    cgrid2["nx"] = nx
95    cgrid2["ny"] = ny
96    u = {}
97    v = {}
98    
99    Q = 2
100    for i = 1, nx do
101        x = (i-1-nx/2+0.5)*dx
102        cgrid2["xg"][i] = {}
103        cgrid2["yg"][i] = {}
104        u[i] = {}
105        v[i] = {}
106        for j = 1, ny do
107            y = (j-1-ny/2+0.5)*dy
108        cgrid2["xg"][i][j] = x
109        cgrid2["yg"][i][j] = y
110        b = ymax/4*(3-math.cos(math.pi*x/xmax))
111            if math.abs(y)<b then
112                dbdx = ymax/4*math.sin(math.pi*x/xmax)*y/b
113                u[i][j] = Q*ymax/b
114                v[i][j] = dbdx*u[i][j]
115            else
116                u[i][j] = 0
117                v[i][j] = 0
118            end
119        end
120    end
121
122    pl.env(xmin, xmax, ymin, ymax, 0, 0)
123    pl.lab("(x)", "(y)", "#frPLplot Example 22 - constriction")
124    pl.col0(2)
125    pl.vect(u, v, -0.5, "pltr2", cgrid2)
126    pl.col0(1)
127end
128
129
130function f2mnmx(f, nx, ny)
131    fmax = f[1][1]
132    fmin = fmax
133
134    for i=1, nx do
135        for j=1, ny do
136        fmax = math.max(fmax, f[i][j])
137        fmin = math.min(fmin, f[i][j])
138        end
139    end
140        
141    return fmin, fmax
142end
143
144-- Vector plot of the gradient of a shielded potential (see example 9)
145function potential()
146  nper = 100
147  nlevel = 10
148  nr = 20
149  ntheta = 20
150
151  u = {}
152  v = {}
153  z = {}
154  clevel = {}
155  px = {}
156  py = {}
157
158  cgrid2 = {}
159  cgrid2["xg"] = {}
160  cgrid2["yg"] = {}
161  cgrid2["nx"] = nr
162  cgrid2["ny"] = ntheta
163
164  -- Potential inside a conducting cylinder (or sphere) by method of images.
165  -- Charge 1 is placed at (d1, d1), with image charge at (d2, d2).
166  -- Charge 2 is placed at (d1, -d1), with image charge at (d2, -d2).
167  -- Also put in smoothing term at small distances.
168  rmax = nr
169
170  eps = 2
171
172  q1 = 1
173  d1 = rmax/4
174
175  q1i = -q1*rmax/d1
176  d1i = rmax^2/d1
177
178  q2 = -1
179  d2 = rmax/4
180
181  q2i = -q2*rmax/d2
182  d2i = rmax^2/d2
183
184  for i = 1, nr do
185      r = i - 0.5
186    cgrid2["xg"][i] = {}
187    cgrid2["yg"][i] = {}
188    u[i] = {}
189    v[i] = {}
190    z[i] = {}
191      for j = 1, ntheta do
192        theta = 2*math.pi/(ntheta-1)*(j-0.5)
193        x = r*math.cos(theta)
194        y = r*math.sin(theta)
195        cgrid2["xg"][i][j] = x
196        cgrid2["yg"][i][j] = y
197        div1 = math.sqrt((x-d1)^2 + (y-d1)^2 + eps^2)
198        div1i = math.sqrt((x-d1i)^2 + (y-d1i)^2 + eps^2)
199        div2 = math.sqrt((x-d2)^2 + (y+d2)^2 + eps^2)
200        div2i = math.sqrt((x-d2i)^2 + (y+d2i)^2 + eps^2)
201        z[i][j] = q1/div1 + q1i/div1i + q2/div2 + q2i/div2i
202        u[i][j] = -q1*(x-d1)/div1^3 - q1i*(x-d1i)/div1i^3
203                    -q2*(x-d2)/div2^3 - q2i*(x-d2i)/div2i^3
204        v[i][j] = -q1*(y-d1)/div1^3 - q1i*(y-d1i)/div1i^3
205                    -q2*(y+d2)/div2^3 - q2i*(y+d2i)/div2i^3
206      end
207  end
208
209  xmin, xmax = f2mnmx(cgrid2["xg"], nr, ntheta)
210  ymin, ymax = f2mnmx(cgrid2["yg"], nr, ntheta)
211  zmin, zmax = f2mnmx(z, nr, ntheta)
212
213  pl.env(xmin, xmax, ymin, ymax, 0, 0)
214  pl.lab("(x)", "(y)", "#frPLplot Example 22 - potential gradient vector plot")
215
216  -- Plot contours of the potential
217  dz = (zmax-zmin)/nlevel
218  for i = 1, nlevel do
219      clevel[i] = zmin + (i-0.5)*dz
220  end
221  
222  pl.col0(3)
223  pl.lsty(2)
224  pl.cont(z, 1, nr, 1, ntheta, clevel, "pltr2", cgrid2)
225  pl.lsty(1)
226  pl.col0(1)
227
228  -- Plot the vectors of the gradient of the potential
229  pl.col0(2)
230  pl.vect(u, v, 25, "pltr2", cgrid2)
231  pl.col0(1)
232
233  -- Plot the perimeter of the cylinder
234  for i=1, nper do
235    theta = 2*math.pi/(nper-1)*(i-1)
236    px[i] = rmax*math.cos(theta)
237    py[i] = rmax*math.sin(theta)
238  end
239  
240  pl.line(px, py)
241end
242
243
244----------------------------------------------------------------------------
245-- main
246--
247-- Generates several simple vector plots.
248----------------------------------------------------------------------------
249
250-- Parse and process command line arguments
251pl.parseopts(arg, pl.PL_PARSE_FULL)
252
253-- Initialize plplot
254pl.init()
255
256circulation()
257
258fill = 0
259
260-- Set arrow style using arrow_x and arrow_y then
261-- plot using these arrows.
262pl.svect(arrow_x, arrow_y, fill)
263constriction()
264
265-- Set arrow style using arrow2_x and arrow2_y then
266-- plot using these filled arrows.
267fill = 1
268pl.svect(arrow2_x, arrow2_y, fill)
269constriction()
270
271potential()
272
273pl.plend()
274

Archive Download this file



interactive