learn-physics 0.3.1 → 0.4
raw patch · 6 files changed
+143/−29 lines, 6 filesdep +gnuplot
Dependencies added: gnuplot
Files
- learn-physics.cabal +4/−2
- src/Physics/Learn.hs +26/−5
- src/Physics/Learn/Curve.hs +3/−3
- src/Physics/Learn/Mechanics.hs +5/−5
- src/Physics/Learn/StateSpace.hs +48/−14
- src/Physics/Learn/Visual/PlotTools.hs +57/−0
learn-physics.cabal view
@@ -1,5 +1,5 @@ Name: learn-physics-Version: 0.3.1+Version: 0.4 Synopsis: Haskell code for learning physics Description: A library of functions for vector calculus, calculation of electric field, electric flux,@@ -31,11 +31,13 @@ Physics.Learn.RootFinding Physics.Learn.Mechanics Physics.Learn+ Physics.Learn.Visual.PlotTools Physics.Learn.Visual.VisTools Build-depends: base >= 4.2 && < 4.8, vector-space >= 0.8.4 && < 0.9, not-gloss >= 0.5.0.4 && < 0.7,- spatial-math >= 0.1.7 && < 0.3+ spatial-math >= 0.1.7 && < 0.3,+ gnuplot >= 0.5 && < 0.6 Hs-source-dirs: src Source-repository head
src/Physics/Learn.hs view
@@ -145,13 +145,23 @@ , shiftVolume -- ** Volume Integral , volumeIntegral- -- * Utilities+ -- * Differential Equations , StateSpace(..)--- , (.-^)--- , Time+ , (.-^)+ , Time+ , DifferentialEquation+ , InitialValueProblem+ , EvolutionMethod+ , SolutionMethod+ , stepSolution+ , eulerMethod , rungeKutta4 , integrateSystem -- * Visualization+ -- ** Plotting+ , label+ , postscript+ , psFile -- ** Vis library , xyzFromVec , xyzFromPos@@ -274,8 +284,14 @@ ) import Physics.Learn.StateSpace ( StateSpace(..)--- , (.-^)--- , Time+ , (.-^)+ , Time+ , DifferentialEquation+ , InitialValueProblem+ , EvolutionMethod+ , SolutionMethod+ , stepSolution+ , eulerMethod ) import Physics.Learn.RungeKutta ( rungeKutta4@@ -304,4 +320,9 @@ , ManyParticleAccelerationFunction , manyParticleStateDeriv , manyParticleRungeKuttaStep+ )+import Physics.Learn.Visual.PlotTools+ ( label+ , postscript+ , psFile )
src/Physics/Learn/Curve.hs view
@@ -62,9 +62,9 @@ ) -- | 'Curve' is a parametrized function into three-space, an initial limit, and a final limit.-data Curve = Curve { curveFunc :: (Double -> Position) -- ^ function from one parameter into space- , startingCurveParam :: Double -- ^ starting value of the parameter- , endingCurveParam :: Double -- ^ ending value of the parameter+data Curve = Curve { curveFunc :: Double -> Position -- ^ function from one parameter into space+ , startingCurveParam :: Double -- ^ starting value of the parameter+ , endingCurveParam :: Double -- ^ ending value of the parameter } -- | A dotted line integral.
src/Physics/Learn/Mechanics.hs view
@@ -49,7 +49,7 @@ import Physics.Learn.StateSpace ( StateSpace(..) , Diff- , TimeDerivative+ , DifferentialEquation ) import Physics.Learn.RungeKutta ( rungeKutta4@@ -91,7 +91,7 @@ -- | Time derivative of state for a single particle -- with a constant mass. simpleStateDeriv :: SimpleAccelerationFunction -- ^ acceleration function for the particle- -> TimeDerivative SimpleState -- ^ derivatives as a function of state+ -> DifferentialEquation SimpleState -- ^ differential equation simpleStateDeriv a (t, r, v) = (1, v, a(t, r, v)) -- | Single Runge-Kutta step@@ -144,7 +144,7 @@ -- | Time derivative of state for a single particle -- with a constant mass. oneParticleStateDeriv :: OneParticleAccelerationFunction -- ^ acceleration function for the particle- -> TimeDerivative OneParticleSystemState -- ^ derivatives as a function of state+ -> DifferentialEquation OneParticleSystemState -- ^ differential equation oneParticleStateDeriv a st@(_t, St _r v) = (1, DSt v (a st)) -- | Single Runge-Kutta step@@ -178,7 +178,7 @@ -- | Time derivative of state for two particles -- with constant mass. twoParticleStateDeriv :: TwoParticleAccelerationFunction -- ^ acceleration function for two particles- -> TimeDerivative TwoParticleSystemState -- ^ derivatives as a function of state+ -> DifferentialEquation TwoParticleSystemState -- ^ differential equation twoParticleStateDeriv af2 st2@(_t, St _r1 v1, St _r2 v2) = (1, DSt v1 a1, DSt v2 a2) where (a1,a2) = af2 st2@@ -206,7 +206,7 @@ -- | Time derivative of state for many particles -- with constant mass. manyParticleStateDeriv :: ManyParticleAccelerationFunction -- ^ acceleration function for many particles- -> TimeDerivative ManyParticleSystemState -- ^ derivatives as a function of state+ -> DifferentialEquation ManyParticleSystemState -- ^ differential equation manyParticleStateDeriv af st@(_t, sts) = (1, [DSt v a | (v,a) <- zip vs as]) where vs = map velocity sts
src/Physics/Learn/StateSpace.hs view
@@ -12,7 +12,7 @@ A 'StateSpace' is an affine space where the associated vector space has scalars that are instances of 'Fractional'. If p is an instance of 'StateSpace', then the associated vectorspace-'Diff' p is intended to represent the space of time derivatives+'Diff' p is intended to represent the space of (time) derivatives of paths in p. 'StateSpace' is very similar to Conal Elliott's 'AffineSpace'.@@ -22,7 +22,12 @@ ( StateSpace(..) , (.-^) , Time- , TimeDerivative+ , DifferentialEquation+ , InitialValueProblem+ , EvolutionMethod+ , SolutionMethod+ , stepSolution+ , eulerMethod ) where @@ -31,7 +36,6 @@ ) import Data.VectorSpace ( VectorSpace(..)--- , Scalar ) import Physics.Learn.Position ( Position@@ -40,17 +44,19 @@ ) import Physics.Learn.CarrotVec ( Vec--- , (^+^)+ , (^*) , (^-^) ) infixl 6 .+^, .-^ infix 6 .-. --- | A 'StateSpace' has an associated vector space, the vectors of which--- can be multiplied or divided by scalars.--- An example would be the set of positions of a particle.--- Position is not a vector, but displacement (difference in position) is a vector.+-- | An instance of 'StateSpace' is a data type that can serve as the state+-- of some system. Alternatively, a 'StateSpace' is a collection of dependent+-- variables for a differential equation.+-- A 'StateSpace' has an associated vector space for the (time) derivatives+-- of the state. The associated vector space is a linearized version of+-- the 'StateSpace'. class (VectorSpace (Diff p), Fractional (Scalar (Diff p))) => StateSpace p where -- | Associated vector space type Diff p@@ -76,6 +82,7 @@ (.-.) = (^-^) (.+^) = (^+^) +-- | Position is not a vector, but displacement (difference in position) is a vector. instance StateSpace Position where type Diff Position = Vec (.-.) = flip displacement@@ -109,10 +116,37 @@ (.-.) = zipWith (.-.) (.+^) = zipWith (.+^) --- | The time derivative of a state is an element of the associated vector space.-type TimeDerivative state = state -> Diff state+-- | A differential equation expresses how the dependent variables (state)+-- change with the independent variable (time).+-- A differential equation is specified by giving the (time) derivative+-- of the state as a function of the state.+-- The (time) derivative of a state is an element of the associated vector space.+type DifferentialEquation state = state -> Diff state -{--class HasTimeDerivative state where- timeDeriv :: state -> Diff state--}+-- | An initial value problem is a differential equation along with an initial state.+type InitialValueProblem state = (DifferentialEquation state, state)++-- | A (numerical) solution method is a way of converting+-- an initial value problem into a list of states (a solution).+-- The list of states need not be equally spaced in time.+type SolutionMethod state = InitialValueProblem state -> [state]++-- | An evolution method is a way of approximating the state+-- after advancing a finite interval in the independent+-- variable (time) from a given state.+type EvolutionMethod state+ = DifferentialEquation state -- ^ differential equation+ -> Time state -- ^ time interval+ -> state -- ^ initial state+ -> state -- ^ evolved state++-- | Given an evolution method and a time step, return the solution method+-- which applies the evolution method repeatedly with with given time step.+-- The solution method returned will produce an infinite list of states.+stepSolution :: EvolutionMethod state -> Time state -> SolutionMethod state+stepSolution ev dt (de, ic) = iterate (ev de dt) ic++-- | The Euler method is the simplest evolution method.+-- It increments the state by the derivative times the time step.+eulerMethod :: StateSpace state => EvolutionMethod state+eulerMethod de dt st = st .+^ de st ^* dt
+ src/Physics/Learn/Visual/PlotTools.hs view
@@ -0,0 +1,57 @@+{-# OPTIONS_GHC -Wall #-}++{- | +Module : Physics.Learn.Visual.PlotTools+Copyright : (c) Scott N. Walck 2011-2014+License : BSD3 (see LICENSE)+Maintainer : Scott N. Walck <walck@lvc.edu>+Stability : experimental++This module contains helping functions for using Gnuplot.+-}++module Physics.Learn.Visual.PlotTools+ ( label+ , postscript+ , psFile+ , examplePlot1+ , examplePlot2+ )+ where++import Graphics.Gnuplot.Simple+ ( Attribute(..)+ , plotFunc+ )++-- | An 'Attribute' with a given label at a given position.+label :: String -> (Double,Double) -> Attribute+label name (x,y) + = Custom "label" [show name ++ " at " ++ show x ++ "," ++ show y]++-- | An 'Attribute' that requests postscript output.+postscript :: Attribute+postscript = Custom "term" ["postscript"]++-- | An 'Attribute' giving the postscript file name.+psFile :: FilePath -> Attribute+psFile file = Custom "output" [show file]++-- | An example of the use of 'label'. See the source code.+examplePlot1 :: IO ()+examplePlot1 = plotFunc [Title "Cosine Wave"+ ,XLabel "Time (ms)"+ ,YLabel "Velocity"+ ,label "Albert Einstein" (2,0.8)+ ] [0,0.01..10::Double] cos++-- | An example of the use of 'postscript' and 'psFile'. See the source code.+examplePlot2 :: IO ()+examplePlot2 = plotFunc [Title "Cosine Wave"+ ,XLabel "Time (ms)"+ ,YLabel "Velocity of Car"+ ,label "Albert Einstein" (2,0.8)+ ,postscript+ ,psFile "post1.ps"+ ] [0,0.01..10::Double] cos+