Better CSV export

This commit is contained in:
Dimitri Lozeve 2017-07-26 08:49:38 +02:00
parent 49116785ae
commit 7be6f684d4

View file

@ -41,25 +41,46 @@ randomBody = do
-- CSV export -- CSV export
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
csvFromPoint :: Point V3 Double -> String -- | Show a Vector as CSV
csvFromPoint (P (V3 x y z)) = csvFromVector :: V3 Double -> String
csvFromVector (V3 x y z) =
show x ++ "," ++ show y ++ "," ++ show z show x ++ "," ++ show y ++ "," ++ show z
csvFromBodies :: [Body] -> [String] -- | show a Point as CSV
csvFromBodies = csvFromPoint :: Point V3 Double -> String
map (\ x -> csvFromPoint (P v) = csvFromVector v
bodyName x ++
"," ++
show (bodyMass x) ++
"," ++ csvFromPoint (bodyPosition x) ++ "\n")
steps :: Int -> Double -> [Body] -> IO () -- | Show a Body as CSV
steps 0 _ _ = return () csvFromBody :: Double -> Body -> String
steps n dt bodies = do csvFromBody dt b =
putStr . concat $ map ((show n ++ ",") ++) $ csvFromBodies bodies show dt ++ "," ++
steps (n-1) dt (updateAll dt bodies) csvFromPoint (bodyPosition b) ++ "," ++
csvFromVector (bodySpeed b) ++ "\n"
-- | Show a list of bodies as CSV
csvFromBodies :: Double -> [Body] -> String
csvFromBodies dt bs = concat $ map (csvFromBody dt) bs
-- | Compute all the steps of the simulation
steps :: Double -- ^ The time step
-> [Body] -- ^ The initial state (list of bodies)
-> [(Double, [Body])] -- ^ List of successive states with the
-- corresponding time
steps dt b = zip (iterate (dt +) 0) (iterate (updateAll dt) b)
-- | Show all the steps as CSV
csvFromInit :: Int -- ^ The number of time steps to keep
-> Double -- ^ The time step
-> [Body] -- ^ The initial state (list of bodies)
-> String -- ^ CSV data
csvFromInit n dt b = concat . take n $ map (uncurry csvFromBodies) (steps dt b)
main :: IO ()
main = do
bodies <- replicateM 100 randomBody
putStrLn $ csvFromInit 10000 1e-1 bodies
{-
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Gloss -- Gloss
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -93,6 +114,4 @@ main = do
displayBodies displayBodies
(\_ dt bs -> updateAll (realToFrac dt*1e6) bs) (\_ dt bs -> updateAll (realToFrac dt*1e6) bs)
--main :: IO () -}
--main = steps 1000000 10 [sun, earth, moon, mercury, venus, mars]