Permalink
Browse files

got old pygame working to show sph in 2d

  • Loading branch information...
1 parent a26fb00 commit 667f38f221539d7d90734622ba3609443c986c83 @enjalot committed Aug 24, 2011
Binary file not shown.
@@ -368,8 +368,12 @@ namespace rtps
//defined in Sort.cpp
timers["bitonic"]->start();
bitonic_sort();
- //radix_sort();
timers["bitonic"]->stop();
+ //timers["radix"]->start();
+ //timers["bitonic"]->start();
+ //radix_sort();
+ //timers["bitonic"]->stop();
+ //timers["radix"]->stop();
}
//----------------------------------------------------------------------
@@ -31,9 +31,12 @@ def __array_finalize__(self, obj):
def __array_wrap__(self, out_arr, context=None):
#this gets called after numpy functions are called on the array
#out_arr is the output (resulting) array
+ oa = Vec(out_arr)
for i in range(len(out_arr)):
- setattr(out_arr, Vec.props[i], out_arr[i])
- return np.ndarray.__array_wrap__(self, out_arr, context)
+ setattr(oa, Vec.props[i], oa[i])
+ #setattr(out_arr, Vec.props[i], out_arr[i])
+ #return np.ndarray.__array_wrap__(self, out_arr, context)
+ return np.ndarray.__array_wrap__(self, oa, context)
def __repr__(self):
desc="""Vec2(data=%(data)s,""" # x=%(x)s, y=%(y)s)"""
@@ -31,9 +31,12 @@ def __array_finalize__(self, obj):
def __array_wrap__(self, out_arr, context=None):
#this gets called after numpy functions are called on the array
#out_arr is the output (resulting) array
+ oa = Vec(out_arr)
for i in range(len(out_arr)):
- setattr(out_arr, Vec.props[i], out_arr[i])
- return np.ndarray.__array_wrap__(self, out_arr, context)
+ setattr(oa, Vec.props[i], oa[i])
+ #setattr(out_arr, Vec.props[i], out_arr[i])
+ #return np.ndarray.__array_wrap__(self, out_arr, context)
+ return np.ndarray.__array_wrap__(self, oa, context)
def __repr__(self):
desc="""Vec2(data=%(data)s,""" # x=%(x)s, y=%(y)s)"""
View
@@ -21,8 +21,10 @@ def __init__(self, dt, sph, is_ghost=False, ghost_system=None):
self.clinit()
self.prgs = {} #store our programs
#of course hardcoding paths here is terrible
- self.clsph_dir = "/Users/enjalot/code/sph/teach/sph/cl_src"
- self.clcommon_dir = "/Users/enjalot/code/sph/teach/sph/cl_common"
+ self.clsph_dir = "/panfs/panasas1/users/idj03/code/master/teach/sph/cl_src"
+ #self.clsph_dir = "/Users/enjalot/code/sph/teach/sph/cl_src"
+ #self.clcommon_dir = "/Users/enjalot/code/sph/teach/sph/cl_common"
+ self.clcommon_dir = "/panfs/panasas1/users/idj03/code/master/teach/sph/cl_common"
self.global_color = [0., 1., 0., 1.]
View
@@ -17,6 +17,7 @@ def density_update(sphp, particles):
for pi in particles:
pi.dens = 0.
for pj in particles:
+ #print pi.pos, pj.pos
r = pi.pos - pj.pos
#print r
if mag(r) > pi.h: continue
View
@@ -11,8 +11,8 @@
from vector import Vec
#from forces import *
-import forces
-import sph
+#import forces
+from sph import SPH, addRect
import clsph
from hash import Domain
@@ -73,11 +73,11 @@ def __init__(self, *args, **kwargs):
dmin = Vec([0,0,0])
dmax = Vec([1,1,1])
self.domain = Domain(dmin, dmax)
- self.system = sph.SPH(max_num, self.domain)
+ self.system = SPH(max_num, self.domain)
self.ghost_domain = Domain(dmin, dmax)
- self.ghost = sph.SPH(max_num * 36, self.ghost_domain)
- ipos = sph.addRect(512, Vec([0.1, 0.1, 0.,0.]), Vec([1.,1.,0.,0.]), self.system)
- gpos = sph.addRect(8192, Vec([0.1, 0.1, 0.,0.]), Vec([1.,1.,0.,0.]), self.ghost)
+ self.ghost = SPH(max_num * 36, self.ghost_domain)
+ ipos = addRect(512, Vec([0.1, 0.1, 0.,0.]), Vec([1.,1.,0.,0.]), self.system)
+ gpos = addRect(8192, Vec([0.1, 0.1, 0.,0.]), Vec([1.,1.,0.,0.]), self.ghost)
#print ipos, "LEN", len(ipos)
#print gpos, "LEN", len(gpos)
self.clghost_system = clsph.CLSPH(dt, self.ghost, is_ghost=True)
@@ -94,6 +94,7 @@ def __init__(self, *args, **kwargs):
self.clghost_system.set_color(color)
#########################################################################
+ print "about to start main loop"
glutMainLoop()
View
@@ -33,8 +33,15 @@ def toscreen(p, surface, screen_scale):
#@Timing(timing_coroutine)
@timings
def draw_particles(ps):
+ ps.reverse()
+ i = 0
for p in ps:
- p.draw()
+ if i == len(ps)-1:
+ p.draw(True)
+ else:
+ p.draw()
+ i += 1
+ ps.reverse()
def main():
@@ -55,15 +62,18 @@ def main():
dmin = Vec([0,0,0])
dmax = Vec([5,5,5])
- domain = Domain(dmin, dmax, screen)
+ domain = Domain(dmin, dmax)#, screen)
system = sph.SPH(max_num, domain)
- particles = sph.init_particles(50, system, domain, screen)
+ particles = sph.init_particles(5, system, domain, screen)
+ print "p0.pos:", particles[0].pos
mouse_down = False
+ pause = False
+ pi = 0
while 1:
clock.tick(60)
key = pygame.key.get_pressed()
@@ -73,26 +83,45 @@ def main():
return
elif key[K_t]:
print timings
+
+ elif key[K_0]:
+ pi = 0
+ elif key[K_1]:
+ pi = 1
+ elif key[K_2]:
+ pi = 2
+ elif key[K_3]:
+ pi = 3
+ elif key[K_4]:
+ pi = 4
+ elif key[K_5]:
+ pi = 5
+
+ elif key[K_p]:
+ pause = not pause
+
elif event.type == MOUSEBUTTONDOWN:
mouse_down = True
elif event.type == MOUSEMOTION:
if(mouse_down):
v = Vec([event.pos[0], event.pos[1]])
v = fromscreen(v, screen)
- particles[0].move(v)
+ particles[pi].move(v)
elif event.type == MOUSEBUTTONUP:
mouse_down = False
screen.blit(background, (0, 0))
density_update(system, particles)
- force_update(system, particles)
- collision_wall(system, domain, particles)
- #euler_update(system, particles)
- leapfrog_update(system, particles)
+ if not pause:
+ force_update(system, particles)
+ collision_wall(system, domain, particles)
+ #euler_update(system, particles)
+ leapfrog_update(system, particles)
draw_particles(particles)
+ #print "ASDF"
pygame.display.flip()
if __name__ == "__main__":
View
@@ -112,7 +112,7 @@ def __init__(self, pos, sphp, color, surface):
self.pos = pos
self.h = sphp.smoothing_radius
self.scale = sphp.sim_scale
- self.mass = sphp.m
+ self.mass = sphp.mass
self.vel = Vec([0.,0.])
self.veleval = Vec([0.,0.])
@@ -123,17 +123,21 @@ def __init__(self, pos, sphp, color, surface):
self.screen_scale = self.surface.get_width() / sphp.domain.width
def move(self, pos):
- self.pos = pos * self.scale
+ self.pos = pos * self.scale / self.screen_scale
#print "dens", self.dens
- def draw(self):
+ def draw(self, show_dense = False):
#draw circle representing particle smoothing radius
dp = toscreen(self.pos / self.scale, self.surface, self.screen_scale)
pygame.draw.circle(self.surface, self.col, dp, self.screen_scale * self.h / self.scale, 1)
#draw filled circle representing density
- pygame.draw.circle(self.surface, self.col, dp, self.dens / 40., 0)
+ #pygame.draw.circle(self.surface, self.col, dp, self.dens / 40., 0)
+ if show_dense:
+ pygame.draw.circle(self.surface, self.col, dp, self.dens / 10., 0)
+ else:
+ pygame.draw.circle(self.surface, self.col, dp, 30., 0)
#TODO draw force vector (make optional)
#vec = [self.x - f[0]*fdraw/fscale, self.y - f[1]*fdraw/fscale]
@@ -200,18 +204,47 @@ def addRect3D(num, pmin, pmax, sphp):
return rvecnp;
+def addRect_old(num, pmin, pmax, sphp):
+ #Create a rectangle with at most num particles in it. The size of the return
+ #vector will be the actual number of particles used to fill the rectangle
+ print "**** addRect ****"
+ print "rest dist:", sphp.rest_distance
+ print "sim_scale:", sphp.sim_scale
+ spacing = 1.0 * sphp.rest_distance / sphp.sim_scale;
+ print "spacing", spacing
+
+ xmin = pmin.x# * scale
+ xmax = pmax.x# * scale
+ ymin = pmin.y# * scale
+ ymax = pmax.y# * scale
+
+ print "min, max", xmin, xmax, ymin, ymax
+ rvec = [];
+ i=0;
+ for y in np.arange(ymin, ymax, spacing):
+ for x in np.arange(xmin, xmax, spacing):
+ if i >= num: break
+ print "x, y", x, y
+ rvec += [ Vec([x,y]) * sphp.sim_scale];
+ #rvec += [[x, y, 0., 1.]]
+ i+=1;
+ print "%d particles added" % i
+ #rvecnp = np.array(rvec, dtype=np.float32)
+ #return rvecnp;
+ return rvec
+
def init_particles(num, sphp, domain, surface):
particles = []
p1 = Vec([.5, 2.]) * sphp.sim_scale
- particles += [ Particle(p1, sphp, [255,0,0], surface) ]
+ particles += [ Particle(p1, sphp, [0,0,255], surface) ]
pmin = Vec([.5, .5])
pmax = Vec([2., 3.])
- ps = addRect(num, pmin, pmax, sphp)
+ ps = addRect_old(num, pmin, pmax, sphp)
for p in ps:
- particles += [ Particle(p, sphp, [0,0,255], surface) ]
+ particles += [ Particle(p, sphp, [255,0,0], surface) ]
"""
p2 = Vec([400., 400.]) * sphp.sim_scale
View
@@ -1,8 +1,8 @@
#Vector class
+#Author - Ian Johnson | enjalot@gmail.com
#http://docs.scipy.org/doc/numpy/user/basics.subclassing.html#slightly-more-realistic-example-attribute-added-to-existing-array
-#TODO: generalize to 3 and 4 dimensions
import numpy as np
import math
@@ -31,9 +31,13 @@ def __array_finalize__(self, obj):
def __array_wrap__(self, out_arr, context=None):
#this gets called after numpy functions are called on the array
#out_arr is the output (resulting) array
+ #oa = Vec(out_arr)
for i in range(len(out_arr)):
+ #setattr(oa, Vec.props[i], oa[i])
setattr(out_arr, Vec.props[i], out_arr[i])
+ #out_arr = oa
return np.ndarray.__array_wrap__(self, out_arr, context)
+ #return np.ndarray.__array_wrap__(self, oa, context)
def __repr__(self):
desc="""Vec2(data=%(data)s,""" # x=%(x)s, y=%(y)s)"""
@@ -54,6 +58,12 @@ def __setattr__(self, item, val):
self[Vec.props.index(item)] = val
+ ###
+ #math utilities
+ ###
+
+def normalize(u):
+ return u / (math.sqrt(np.dot(u, u)))
if __name__ == "__main__":
@@ -81,19 +91,12 @@ def __setattr__(self, item, val):
va.y = 9
va.z = 5
print "va: ", repr(va)
- vb = Vec([1.,1.,1.])
+ vb = Vec([1,1,1])
vc = va + vb
- print "vc: ", repr(vc)
+ print "v3: ", repr(vc)
print "dot(va, vb):", np.dot(va, vb)
- vb += va
- print "vb:", repr(vb)
- vb *= .1
- print "vb:", repr(vb)
- vb += vc*.1
- print "vb:", repr(vb)
-

0 comments on commit 667f38f

Please sign in to comment.