STArray stuff

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import Data.List
import Data.Array
import Data.Array.ST
import Control.Monad
import Control.Monad.ST

type Particle = (Vec3D, Vec3D)
type Vec3D = (Double, Double, Double)
add = undefined
mulSc = undefined

doubleDensityRelaxation :: Array Int Vec3D -> Double -> Double -> Double -> Double -> Double -> Array Int Vec3D
doubleDensityRelaxation = undefined

step :: Array Int (Vec3D, Vec3D) -> Vec3D -> Double -> Double -> Double -> Double -> Double -> Array Int (Vec3D, Vec3D)
step ps g restDen k kNear t h = runSTArray $ do
    ps' <- thaw ps :: ST s (STArray s Int Particle)
    --GRAVITY
    forM_ [min..max] $ \i -> do
        (x, v) <- readArray ps' i
        writeArray ps' i (x, addGravity v g t)
    --TODO - VISCOSITY
    --MOVE
    xsOld <- newArray (min, max) (0.0,0.0,0.0) :: ST s (STArray s Int Vec3D)
    forM_ [min..max] $ \i -> do
        (x, v) <- readArray ps' i
        writeArray xsOld i x
        writeArray ps' i (x `add` (v `mulSc` t), v)
    --TODO - SPRINGS
    --DOUBLE DENSITY RELAXATION
    xs' <- newArray (min, max) (0.0,0.0,0.0) :: ST s (STArray s Int Vec3D)
    forM_ [min..max] $ \j -> do
        (x, v) <- readArray ps' j
        writeArray xs' j x
    xs'' <- freeze xs'
    let displacements = doubleDensityRelaxation xs'' restDen k kNear t h
    ps <- newArray (min, max) ((0.0,0.0,0.0), (0.0,0.0,0.0)) :: ST s (STArray s Int (Vec3D, Vec3D))
    --TODO incomplete
    return ps
    where
        addGravity v g t = v `add` (g `mulSc` t)
        (min, max) = bounds ps
16:46: Error: Redundant return
Found:
do ps' <- thaw ps :: ST s (STArray s Int Particle)
forM_ [min .. max] $
\ i ->
do (x, v) <- readArray ps' i
writeArray ps' i (x, addGravity v g t)
xsOld <- newArray (min, max) (0.0, 0.0, 0.0) ::
ST s (STArray s Int Vec3D)
forM_ [min .. max] $
\ i ->
do (x, v) <- readArray ps' i
writeArray xsOld i x
writeArray ps' i (x `add` (v `mulSc` t), v)
xs' <- newArray (min, max) (0.0, 0.0, 0.0) ::
ST s (STArray s Int Vec3D)
forM_ [min .. max] $
\ j ->
do (x, v) <- readArray ps' j
writeArray xs' j x
xs'' <- freeze xs'
let displacements
= doubleDensityRelaxation xs'' restDen k kNear t h
ps <- newArray (min, max) ((0.0, 0.0, 0.0), (0.0, 0.0, 0.0)) ::
ST s (STArray s Int (Vec3D, Vec3D))
return ps
Why not:
do ps' <- thaw ps :: ST s (STArray s Int Particle)
forM_ [min .. max] $
\ i ->
do (x, v) <- readArray ps' i
writeArray ps' i (x, addGravity v g t)
xsOld <- newArray (min, max) (0.0, 0.0, 0.0) ::
ST s (STArray s Int Vec3D)
forM_ [min .. max] $
\ i ->
do (x, v) <- readArray ps' i
writeArray xsOld i x
writeArray ps' i (x `add` (v `mulSc` t), v)
xs' <- newArray (min, max) (0.0, 0.0, 0.0) ::
ST s (STArray s Int Vec3D)
forM_ [min .. max] $
\ j ->
do (x, v) <- readArray ps' j
writeArray xs' j x
xs'' <- freeze xs'
let displacements
= doubleDensityRelaxation xs'' restDen k kNear t h
newArray (min, max) ((0.0, 0.0, 0.0), (0.0, 0.0, 0.0)) ::
ST s (STArray s Int (Vec3D, Vec3D))