{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE Safe #-}
module Physics.Learn.Charge
(
Charge
, ChargeDistribution(..)
, totalCharge
, eField
, eFieldFromPointCharge
, eFieldFromLineCharge
, eFieldFromSurfaceCharge
, eFieldFromVolumeCharge
, electricFlux
, electricPotentialFromField
, electricPotentialFromCharge
)
where
import Physics.Learn.CarrotVec
( magnitude
, (*^)
, (^/)
)
import Physics.Learn.Position
( Position
, ScalarField
, VectorField
, displacement
, addFields
)
import Physics.Learn.Curve
( Curve(..)
, straightLine
, simpleLineIntegral
, dottedLineIntegral
)
import Physics.Learn.Surface
( Surface(..)
, surfaceIntegral
, dottedSurfaceIntegral
)
import Physics.Learn.Volume
( Volume(..)
, volumeIntegral
)
type Charge = Double
data ChargeDistribution = PointCharge Charge Position
| LineCharge ScalarField Curve
| SurfaceCharge ScalarField Surface
| VolumeCharge ScalarField Volume
| MultipleCharges [ChargeDistribution]
totalCharge :: ChargeDistribution -> Charge
totalCharge :: ChargeDistribution -> Double
totalCharge (PointCharge Double
q Position
_) = Double
q
totalCharge (LineCharge ScalarField
lambda Curve
c) = forall v.
(InnerSpace v, Scalar v ~ Double) =>
Int -> Field v -> Curve -> v
simpleLineIntegral Int
1000 ScalarField
lambda Curve
c
totalCharge (SurfaceCharge ScalarField
sigma Surface
s) = forall v.
(VectorSpace v, Scalar v ~ Double) =>
Int -> Int -> Field v -> Surface -> v
surfaceIntegral Int
200 Int
200 ScalarField
sigma Surface
s
totalCharge (VolumeCharge ScalarField
rho Volume
v) = forall v.
(VectorSpace v, Scalar v ~ Double) =>
Int -> Int -> Int -> Field v -> Volume -> v
volumeIntegral Int
50 Int
50 Int
50 ScalarField
rho Volume
v
totalCharge (MultipleCharges [ChargeDistribution]
ds) = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [ChargeDistribution -> Double
totalCharge ChargeDistribution
d | ChargeDistribution
d <- [ChargeDistribution]
ds]
eFieldFromPointCharge
:: Charge
-> Position
-> VectorField
eFieldFromPointCharge :: Double -> Position -> VectorField
eFieldFromPointCharge Double
q Position
r' Position
r
= (Double
k forall a. Num a => a -> a -> a
* Double
q) forall v. VectorSpace v => Scalar v -> v -> v
*^ Displacement
d forall v s.
(VectorSpace v, s ~ Scalar v, Fractional s) =>
v -> s -> v
^/ forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude Displacement
d forall a. Floating a => a -> a -> a
** Double
3
where
k :: Double
k = Double
9e9
d :: Displacement
d = Position -> VectorField
displacement Position
r' Position
r
eFieldFromLineCharge
:: ScalarField
-> Curve
-> VectorField
eFieldFromLineCharge :: ScalarField -> Curve -> VectorField
eFieldFromLineCharge ScalarField
lambda Curve
c Position
r
= Double
k forall v. VectorSpace v => Scalar v -> v -> v
*^ forall v.
(InnerSpace v, Scalar v ~ Double) =>
Int -> Field v -> Curve -> v
simpleLineIntegral Int
1000 VectorField
integrand Curve
c
where
k :: Double
k = Double
9e9
integrand :: VectorField
integrand Position
r' = ScalarField
lambda Position
r' forall v. VectorSpace v => Scalar v -> v -> v
*^ Displacement
d forall v s.
(VectorSpace v, s ~ Scalar v, Fractional s) =>
v -> s -> v
^/ forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude Displacement
d forall a. Floating a => a -> a -> a
** Double
3
where
d :: Displacement
d = Position -> VectorField
displacement Position
r' Position
r
eFieldFromSurfaceCharge
:: ScalarField
-> Surface
-> VectorField
eFieldFromSurfaceCharge :: ScalarField -> Surface -> VectorField
eFieldFromSurfaceCharge ScalarField
sigma Surface
s Position
r
= Double
k forall v. VectorSpace v => Scalar v -> v -> v
*^ forall v.
(VectorSpace v, Scalar v ~ Double) =>
Int -> Int -> Field v -> Surface -> v
surfaceIntegral Int
200 Int
200 VectorField
integrand Surface
s
where
k :: Double
k = Double
9e9
integrand :: VectorField
integrand Position
r' = ScalarField
sigma Position
r' forall v. VectorSpace v => Scalar v -> v -> v
*^ Displacement
d forall v s.
(VectorSpace v, s ~ Scalar v, Fractional s) =>
v -> s -> v
^/ forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude Displacement
d forall a. Floating a => a -> a -> a
** Double
3
where
d :: Displacement
d = Position -> VectorField
displacement Position
r' Position
r
eFieldFromVolumeCharge
:: ScalarField
-> Volume
-> VectorField
eFieldFromVolumeCharge :: ScalarField -> Volume -> VectorField
eFieldFromVolumeCharge ScalarField
rho Volume
v Position
r
= Double
k forall v. VectorSpace v => Scalar v -> v -> v
*^ forall v.
(VectorSpace v, Scalar v ~ Double) =>
Int -> Int -> Int -> Field v -> Volume -> v
volumeIntegral Int
50 Int
50 Int
50 VectorField
integrand Volume
v
where
k :: Double
k = Double
9e9
integrand :: VectorField
integrand Position
r' = ScalarField
rho Position
r' forall v. VectorSpace v => Scalar v -> v -> v
*^ Displacement
d forall v s.
(VectorSpace v, s ~ Scalar v, Fractional s) =>
v -> s -> v
^/ forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude Displacement
d forall a. Floating a => a -> a -> a
** Double
3
where
d :: Displacement
d = Position -> VectorField
displacement Position
r' Position
r
eField :: ChargeDistribution -> VectorField
eField :: ChargeDistribution -> VectorField
eField (PointCharge Double
q Position
r') = Double -> Position -> VectorField
eFieldFromPointCharge Double
q Position
r'
eField (LineCharge ScalarField
lam Curve
c) = ScalarField -> Curve -> VectorField
eFieldFromLineCharge ScalarField
lam Curve
c
eField (SurfaceCharge ScalarField
sig Surface
s) = ScalarField -> Surface -> VectorField
eFieldFromSurfaceCharge ScalarField
sig Surface
s
eField (VolumeCharge ScalarField
rho Volume
v) = ScalarField -> Volume -> VectorField
eFieldFromVolumeCharge ScalarField
rho Volume
v
eField (MultipleCharges [ChargeDistribution]
cds) = forall v. AdditiveGroup v => [Field v] -> Field v
addFields forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map ChargeDistribution -> VectorField
eField [ChargeDistribution]
cds
electricFlux :: Surface -> ChargeDistribution -> Double
electricFlux :: Surface -> ChargeDistribution -> Double
electricFlux Surface
surf ChargeDistribution
dist = Int -> Int -> VectorField -> Surface -> Double
dottedSurfaceIntegral Int
200 Int
200 (ChargeDistribution -> VectorField
eField ChargeDistribution
dist) Surface
surf
electricPotentialFromField :: Position
-> VectorField
-> ScalarField
electricPotentialFromField :: Position -> VectorField -> ScalarField
electricPotentialFromField Position
base VectorField
ef Position
r = -Int -> VectorField -> Curve -> Double
dottedLineIntegral Int
1000 VectorField
ef (Position -> Position -> Curve
straightLine Position
base Position
r)
electricPotentialFromCharge :: ChargeDistribution -> ScalarField
electricPotentialFromCharge :: ChargeDistribution -> ScalarField
electricPotentialFromCharge (PointCharge Double
q Position
r') = Double -> Position -> ScalarField
ePotFromPointCharge Double
q Position
r'
electricPotentialFromCharge (LineCharge ScalarField
lam Curve
c) = ScalarField -> Curve -> ScalarField
ePotFromLineCharge ScalarField
lam Curve
c
electricPotentialFromCharge (SurfaceCharge ScalarField
sig Surface
s) = ScalarField -> Surface -> ScalarField
ePotFromSurfaceCharge ScalarField
sig Surface
s
electricPotentialFromCharge (VolumeCharge ScalarField
rho Volume
v) = ScalarField -> Volume -> ScalarField
ePotFromVolumeCharge ScalarField
rho Volume
v
electricPotentialFromCharge (MultipleCharges [ChargeDistribution]
cds) = forall v. AdditiveGroup v => [Field v] -> Field v
addFields forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map ChargeDistribution -> ScalarField
electricPotentialFromCharge [ChargeDistribution]
cds
ePotFromPointCharge
:: Charge
-> Position
-> ScalarField
ePotFromPointCharge :: Double -> Position -> ScalarField
ePotFromPointCharge Double
q Position
r' Position
r
= (Double
k forall a. Num a => a -> a -> a
* Double
q) forall a. Fractional a => a -> a -> a
/ forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude Displacement
d
where
k :: Double
k = Double
9e9
d :: Displacement
d = Position -> VectorField
displacement Position
r' Position
r
ePotFromLineCharge
:: ScalarField
-> Curve
-> ScalarField
ePotFromLineCharge :: ScalarField -> Curve -> ScalarField
ePotFromLineCharge ScalarField
lambda Curve
c Position
r
= Double
k forall v. VectorSpace v => Scalar v -> v -> v
*^ forall v.
(InnerSpace v, Scalar v ~ Double) =>
Int -> Field v -> Curve -> v
simpleLineIntegral Int
1000 ScalarField
integrand Curve
c
where
k :: Double
k = Double
9e9
integrand :: ScalarField
integrand Position
r' = ScalarField
lambda Position
r' forall a. Fractional a => a -> a -> a
/ forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude Displacement
d
where
d :: Displacement
d = Position -> VectorField
displacement Position
r' Position
r
ePotFromSurfaceCharge
:: ScalarField
-> Surface
-> ScalarField
ePotFromSurfaceCharge :: ScalarField -> Surface -> ScalarField
ePotFromSurfaceCharge ScalarField
sigma Surface
s Position
r
= Double
k forall v. VectorSpace v => Scalar v -> v -> v
*^ forall v.
(VectorSpace v, Scalar v ~ Double) =>
Int -> Int -> Field v -> Surface -> v
surfaceIntegral Int
200 Int
200 ScalarField
integrand Surface
s
where
k :: Double
k = Double
9e9
integrand :: ScalarField
integrand Position
r' = ScalarField
sigma Position
r' forall a. Fractional a => a -> a -> a
/ forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude Displacement
d
where
d :: Displacement
d = Position -> VectorField
displacement Position
r' Position
r
ePotFromVolumeCharge
:: ScalarField
-> Volume
-> ScalarField
ePotFromVolumeCharge :: ScalarField -> Volume -> ScalarField
ePotFromVolumeCharge ScalarField
rho Volume
v Position
r
= Double
k forall v. VectorSpace v => Scalar v -> v -> v
*^ forall v.
(VectorSpace v, Scalar v ~ Double) =>
Int -> Int -> Int -> Field v -> Volume -> v
volumeIntegral Int
50 Int
50 Int
50 ScalarField
integrand Volume
v
where
k :: Double
k = Double
9e9
integrand :: ScalarField
integrand Position
r' = ScalarField
rho Position
r' forall a. Fractional a => a -> a -> a
/ forall v s. (InnerSpace v, s ~ Scalar v, Floating s) => v -> s
magnitude Displacement
d
where
d :: Displacement
d = Position -> VectorField
displacement Position
r' Position
r