python - Generating 3D graphic by PyOpenGL -
i'm using pyopengl generate 3d sea surface according "2d wave equation". main purpose show dynamic graphic of "2d wave equation".but keeps telling me error:
e:\winpython-64bit-3.4.3.5\python-3.4.3.amd64\mytest>python seawave_2d_opengl.py traceback (most recent call last): file "e:\winpython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\opengl\glut\ special.py", line 130, in safecall return function( *args, **named ) file "seawave_2d_opengl.py", line 106, in draw glvertex3f(x,y,z) file "e:\winpython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\opengl\p latform\baseplatform.py", line 402, in __call__ return self( *args, **named ) ctypes.argumenterror: argument 3: <class 'typeerror'>: wrong type glut display callback <function draw @ 0x0000000004086950> (),{} failed: r eturning none argument 3: <class 'typeerror'>: wrong type e:\winpython-64bit-3.4.3.5\python-3.4.3.amd64\mytest> here code:
from numpy import linspace,zeros,sin,pi,exp,sqrt opengl.gl import * opengl.glu import * opengl.glut import * import sys def solver0(i, f, c, bc, lx, ly, nx, ny, dt, tstop, user_action=none): dx = lx/float(nx) dy = ly/float(ny) x = linspace(0, lx, nx+1) #grid points in x dir y = linspace(0, ly, ny+1) #grid points in y dir if dt <= 0: #max time step? dt = (1/float(c))*(1/sqrt(1/dx**2 + 1/dy**2)) cx2 = (c*dt/dx)**2 cy2 = (c*dt/dy)**2 #help variables dt2 = dt**2 = zeros((nx+1,ny+1)) #solution array u = up.copy() #solution @ t-dt um = up.copy() #solution @ t-2*dt #set initial condition: t =0.0 in range(0,nx): j in range(0,ny): u[i,j] = i(x[i], y[j]) in range(1,nx-1): j in range(1,ny-1): um[i,j] = u[i,j] + \ 0.5*cx2*(u[i-1,j] - 2*u[i,j] + u[i+1,j]) + \ 0.5*cy2*(u[i,j-1] - 2*u[i,j] + u[i,j+1]) + \ dt2*f(x[i], y[j], t) #boundary values of um (equals t=dt when du/dt=0) = 0 j in range(0,ny): um[i,j] = bc(x[i], y[j], t+dt) j = 0 in range(0,nx): um[i,j] = bc(x[i], y[j], t+dt) = nx j in range(0,ny): um[i,j] = bc(x[i], y[j], t+dt) j = ny in range(0,nx): um[i,j] = bc(x[i], y[j], t+dt) if user_action not none: user_action(u, x, y, t) #allow user plot etc. while t <= tstop: t_old = t t += dt #update inner points: in range(1,nx-1): j in range(1,ny-1): up[i,j] = -um[i,j] + 2*u[i,j] + \ cx2*(u[i-1,j] - 2*u[i,j] + u[i+1,j]) + \ cy2*(u[i,j-1] - 2*u[i,j] + u[i,j+1]) + \ dt2*f(x[i], y[j], t_old) #insert boundary conditions: = 0 j in range(0,ny): up[i,j] = bc(x[i], y[j], t) j = 0 in range(0,nx): up[i,j] = bc(x[i], y[j], t) = nx j in range(0,ny): up[i,j] = bc(x[i], y[j], t) j = ny in range(0,nx): up[i,j] = bc(x[i], y[j], t) if user_action not none: user_action(up, x, y, t) um, u, = u, up, um #update data structures return u #dt might computed in function #actually,the book wrote `return dt`,but changed `dt` `u` def i(x, y): return exp(-(x-lx/2.0)**2/2.0 -(y-ly/2.0)**2/2.0) def f(x, y, t): return sin(2*x*y*pi*t/lx) #defined myself def bc(x, y, t): return 0.0 #these 3 functions basic functions related first function "solver0" lx = 10 ly = 10 c = 1.0 dt = 0 nx = 40 ny = 40 tstop = 20 #the following part generate 3d graphics,where must make mistakes: def init(): glclearcolor(1.0,1.0,1.0,0.0) def draw(): glclear(gl_color_buffer_bit) glcolor3f(0,0,1.0) glbegin(gl_lines) t in range(0,20,1): z = solver0(i, f, c, bc, lx, ly, nx, ny, dt, t, user_action=none) glvertex3f(x,y,z) #x , y cannot used here because not defined global variables. glend() glflush def update(): global t t += 0.1 glutpostredisplay() def main(): glutinit(sys.argv) glutinitdisplaymode(glut_single | glut_rgba) glutinitwindowsize(800,600) glutinitwindowposition(100,50) glutcreatewindow("2d wave equations".encode("cp932")) init() glutdisplayfunc(draw) glutidlefunc(update) glutmainloop() main() what mistake did made? can me of this? :(
as can guess stacktrace, 1 of arguments (the third?!?) in glvertex3f(x,y,z) has wrong type. discussion in comments made clear z 2 dimensional ndarray while glvertex3f() expects scalars. looks solver0() computes array of z values instead of 1 z-value per call.
edit sort of understand solver0() does. function should documented in book printed in. although stackoverflow not meant give interpretations of copy , paste code, i'll give little overview of think function does:
- lx , ly give range of x , y
- nx , ny give number of x , y value between 0 , lx, ly used.
- the function computes array of x , y values @ wich z-value (
up) computed. - it computes
upseveral time values 0tstopstep widthdt. - if user function
user_actiongiven, called afterupcomputed. userfunction calledup, x, y, targuments.
to sum things up: 1 call of solver0 computes x, y, , z values given range of x , y value , given time span given resolution.
Comments
Post a Comment