Skip to content

Commit

Permalink
nice cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zachschuermann committed Dec 18, 2019
1 parent 15e2aca commit b1d729a
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -24,3 +24,4 @@ sph.cabal
*~
__pycache__
threadscope
.DS_Store
15 changes: 5 additions & 10 deletions app/Main.hs
Expand Up @@ -22,7 +22,7 @@ main :: IO ()
main = do
args <- getArgs
case args of
-- need more elegant way of doing this..
-- just doing simple parsing for now
[] -> guiMain num_points iter chunks
["-t"] -> cliMainPar num_points chunks iter
["-t", "-n", n, "-c", c] -> cliMainPar (read n) (read c) iter
Expand All @@ -43,24 +43,18 @@ usage = do pn <- getProgName
" [-ts] [-n <number particles>] [-i <number iterations>]" ++
" [-c <number of chunks>]"


cliMain :: ([Particle] -> [Particle]) -> Int -> Int -> Int -> IO ()
cliMain f nps chks iters = do
particles <- simInit nps iters chks
let sol = iterate f particles !! iters
sol `deepseq` putStrLn "Done."

-- cliMainSeq :: Int -> Int -> IO ()
-- cliMainSeq nps iters = do
-- particles <- simInit nps iters
-- let sol = iterate supdate particles !! iters
-- sol `deepseq` putStrLn "Done."

guiMain :: Int -> Int -> Int -> IO ()
guiMain nps iters chks = do
(_progName, _args) <- getArgsAndInitialize
initialDisplayMode $= [DoubleBuffered] -- unused: RGBMode, WithDepthBuffer
initialWindowSize $= Size (fromIntegral window_width) (fromIntegral window_height)
initialWindowSize $=
Size (fromIntegral window_width) (fromIntegral window_height)
_window <- createWindow "SPH"
reshapeCallback $= Just reshape
init_
Expand Down Expand Up @@ -105,7 +99,8 @@ keyboard key keyState _ _ = do
initPoints :: [Double] -> [(Double, Double)]
initPoints jitters = zipWith (\(x, y) j -> (x+j, y)) makePoints jitters
where makePoints :: [(Double, Double)]
makePoints = [(x, y) | y <- [view_width/4, view_width/4 + h..view_height-eps*2],
makePoints = [(x, y) | y <- [view_width/4, view_width/4
+ h..view_height-eps*2],
x <- [eps, eps+h..view_width/2]]

makeParticles :: [(Double, Double)] -> [Particle]
Expand Down
4 changes: 2 additions & 2 deletions src/Lib.hs
Expand Up @@ -33,14 +33,14 @@ view_height :: Double
window_width :: Int
window_height :: Int

g = V2 0 (12000 * (-9.8)) -- 12000?
g = V2 0 (12000 * (-9.8)) -- 12000
rest_dens = 1000.0
gas_const = 2000.0
h = 16.0 -- kernel radius
hsq = h*h
mass = 65.0 -- assume all particles have the same mass
visc = 250.0 -- viscosity constant
dt = 0.0009 -- 0.0008
dt = 0.0009 -- 0.0008

poly6 = 315.0 / (65.0*pi*(h^(9 :: Int)))
spiky_grad = (-45.0) / (pi*h^(6 :: Int))
Expand Down
47 changes: 6 additions & 41 deletions src/Solver.hs
Expand Up @@ -3,53 +3,15 @@ import Lib
import Linear.V2
import Linear.Metric (norm, quadrance)
import Control.Parallel.Strategies (using, parListChunk, rseq)
-- (runEval, Eval, rpar, withStrategy, parBuffer, rdeepseq, using, parList, parListChunk, rseq)

{-
parMap :: (a -> b) -> [a] -> [b]
parMap f xs = map f xs `using` parList rseq
-- parMap f [] = return []
-- parMap f (x:y:[]) = do
-- as <- rpar (f x)
-- bs <- rpar (f y)
-- rseq as
-- rseq bs
-- return (as:bs:[])
-- parMap f (a:as) = do
-- b <- rpar (f a)
-- bs <- parMap f as
-- return (b:bs)
-}
update :: Int -> [Particle] -> [Particle]
update partPerChunk ps = map integrate (forces (densityPressure ps))
`using` parListChunk partPerChunk rseq

--update ps = concatMap (update' ps) chunks -- `using` parList rseq)
-- update ps = concat $ parMap (update' ps) chunks -- `using` parList rseq)
-- where chunks = split 2 ps
{-
update' :: [Particle] -> [Particle] -> [Particle]
update' allps ps = map (integrate . calcPV alldpps) dpps -- `using` parList rseq
where dpps = map (calcDP allps) ps
alldpps = map (calcDP allps) allps -- `using` parList rseq
-}

-- . withStrategy (parBuffer 100 rdeepseq)
--update ps = (integrate (forces (densityPressure ps))) `using` parList rseq

-- sequential
supdate :: [Particle] -> [Particle]
supdate ps = map integrate (forces (densityPressure ps))

{-
split :: Int -> [a] -> [[a]]
split numChunks xs = chunk (length xs `quot` numChunks) xs
chunk :: Int -> [a] -> [[a]]
chunk _ [] = []
chunk n xs = let (as, bs) = splitAt n xs in as : chunk n bs
-}

integrate :: Particle -> Particle
integrate (Particle p v f d pr) = enforceBC $ Particle updateP updateV f d pr
where updateP = p + ((realToFrac dt) * updateV)
Expand All @@ -68,7 +30,8 @@ enforceBC = bot . top . left . right
| otherwise = pa
where check (V2 px _) = px + eps > view_width
newp (V2 _ py) (V2 vx vy) f' d' pr' =
Particle (V2 (view_width - eps) py) (V2 (vx * bound_damping) vy) f' d' pr'
Particle (V2 (view_width - eps) py)
(V2 (vx * bound_damping) vy) f' d' pr'
left pa@(Particle p v f d pr)
| check p = newp p v f d pr
| otherwise = pa
Expand All @@ -80,7 +43,8 @@ enforceBC = bot . top . left . right
| otherwise = pa
where check (V2 _ py) = py + eps > view_height
newp (V2 px _) (V2 vx vy) f' d' pr' =
Particle (V2 px (view_height - eps)) (V2 vx (vy * bound_damping)) f' d' pr'
Particle (V2 px (view_height - eps))
(V2 vx (vy * bound_damping)) f' d' pr'

densityPressure :: [Particle] -> [Particle]
densityPressure ps = map (calcDP ps) ps
Expand All @@ -102,7 +66,8 @@ forces :: [Particle] -> [Particle]
forces ps = map (calcPV ps) ps

calcPV :: [Particle] -> Particle -> Particle
calcPV ps pi'@(Particle pix piv _ pid pipr) = Particle pix piv (fpress + fvisc + fgrav) pid pipr
calcPV ps pi'@(Particle pix piv _ pid pipr) =
Particle pix piv (fpress + fvisc + fgrav) pid pipr
where
fgrav = fGravity pid
(fpress, fvisc) = folder $ map go ps
Expand Down
1 change: 1 addition & 0 deletions test/stat-20191218-01-19-48.json
@@ -0,0 +1 @@
{"8": {"150": [1.8699999999999999, 0.12209627348940665]}}
1 change: 1 addition & 0 deletions test/test-20191218-01-19-48.json
@@ -0,0 +1 @@
{"8": {"150": [2.086, 1.814, 1.837, 1.825, 1.788]}}
30 changes: 22 additions & 8 deletions test/tester.py
@@ -1,5 +1,6 @@
#!/usr/local/bin/python3

import time
import glob
import os
import shutil
Expand All @@ -19,9 +20,10 @@ def run_test(n, c, threads):
raise Exception("Failed subprocess")
with open("timekeeper.log") as f:
for line in f:
if "real" in line:
m = re.search('real\t0m([^s]*)', line)
return float(m.group(1))
m = re.search('real\t0m([^s]*)', line)
m2 = re.search('user\t0m([^s]*)', line)
m3 = re.search('sys\t0m([^s]*)', line)
return float(m.group(1)), float(m2.group(1)) + float(m3.group(1))

def do_make():
os.chdir('../')
Expand Down Expand Up @@ -58,30 +60,42 @@ def seq_test(n):
print("stddev: ", statistics.stdev(times))

def main():
nps = 500
nps = 1000
print('running tester...')
do_make()
#seq_test(nps)
tests = {} # map: cores -> chunks -> test
cores = [2, 4, 8, 12, 16]
chunks = [2, 4, 8, 12, 16, 20, 25, 35, 45, 60, 75, 90, 120, 166, 250]
#chunks = [2, 4, 8, 12, 16, 20, 25, 35, 45, 60, 75, 90, 120, 166, 250]
chunks = [2, 4, 8, 12, 16, 20, 25, 35, 45, 60, 75, 100, 150, 200, 250, 333, 400]
stat = {}
totals = {}
for c in tqdm(cores, desc='cores'):
tests[c] = {}
stat[c] = {}
totals[c] = {}
for chks in tqdm(chunks, desc='chunks'):
tests[c][chks] = []
tot = []
for i in range(NUM_TESTS):
tests[c][chks].append(run_test(nps, chks, c))
real, total = run_test(nps, chks, c)
tests[c][chks].append(real)
tot.append(total)
stat[c][chks] = (statistics.mean(tests[c][chks]),
statistics.stdev(tests[c][chks]))
totals[c][chks] = (statistics.mean(tot),
statistics.stdev(tot))

totsave = json.dumps(totals)
tsave = json.dumps(tests)
ssave = json.dumps(stat)
with open("test2.json","w") as f:
timestr = time.strftime("%Y%m%d-%H-%M-%S")
with open("test-" + timestr + ".json","w") as f:
f.write(tsave)
with open("stat2.json","w") as f:
with open("stat-" + timestr + ".json","w") as f:
f.write(ssave)
with open("total-" + timestr + ".json","w") as f:
f.write(totsave)

if __name__ == '__main__':
sys.exit(main())
8 changes: 4 additions & 4 deletions test/timekeeper.log
@@ -1,6 +1,6 @@
Starting 500 point simulation, 1000 iterations (250 chunks)...
Starting 1000 point simulation, 1000 iterations (12 chunks)...
Done.

real 0m1.993s
user 0m26.803s
sys 0m1.111s
real 0m7.081s
user 1m13.150s
sys 0m0.687s
1 change: 1 addition & 0 deletions test/total-20191218-01-19-48.json
@@ -0,0 +1 @@
{"8": {"150": [14.2974, 0.2704686673165677]}}

0 comments on commit b1d729a

Please sign in to comment.