Skip to content

Commit ab6edc6

Browse files
committed
particle beam and xyφ for interactive
1 parent 79af5cb commit ab6edc6

File tree

5 files changed

+52
-19
lines changed

5 files changed

+52
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# 3.11.0
2+
* `particlebeam` and `randominside_xyφ` functions.
13
# 3.10.0
24
* It is now possible in `timeseries!` to evolve a particle until a certain boolean condition is met.
35

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DynamicalBilliards"
22
uuid = "4986ee89-4ee5-5cef-b6b8-e49ba721d7a5"
33
repo = "https://github.com/JuliaDynamics/DynamicalBilliards.jl.git"
4-
version = "3.10.2"
4+
version = "3.11.0"
55

66
[deps]
77
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"

docs/src/basic/high_level.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ Currently there are two types of particles:
3636
* [`Particle`](@ref), which propagates as a straight line.
3737
* [`MagneticParticle`](@ref), which propagates as a circle instead of a line (similar to electrons in a perpendicular magnetic field).
3838

39-
There are two ways to create a particle. The first one is to provide the
40-
constructor with some initial conditions:
39+
To make a particle, provide the constructor with some initial conditions:
4140
```@example 2
4241
x0 = rand(); y0 = rand();
4342
φ0 = 2π*rand()
@@ -53,14 +52,20 @@ mp = MagneticParticle(x0, y0, φ0, ω)
5352

5453
When creating a billiard or a particle, the object is printed with `{Float64}` at the end. This shows what type of numbers are used for *all* numerical operations. If you are curious you can learn more about it in the [Numerical Precision](@ref).
5554

55+
You can initialize several particles with the same direction but slightly different position is the following function:
56+
```@docs
57+
particlebeam
58+
```
59+
5660
!!! danger "Particles must be inside the Billiard!"
5761
Keep in mind that the particle must be initialized **inside a billiard** for any functionality to work properly and make sense. If you are not sure what we mean by that, then you should check out the [Internals](@ref) page.
5862

5963
## Random initial conditions
6064

61-
If you have a `Billiard` which is not a rectangle, creating many random initial conditions inside it can be a pain. Fortunately, the second way to create a particle is to use the following function:
65+
If you have a `Billiard` which is not a rectangle, creating many random initial conditions inside it can be a pain. Fortunately, we have the following function:
6266
```@docs
6367
randominside
68+
randominside_xyφ
6469
```
6570
---
6671

src/billiards/billiardtable.jl

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export Billiard, randominside
1+
export Billiard, randominside, randominside_xyφ
22
#######################################################################################
33
## Billiard Table
44
#######################################################################################
@@ -122,37 +122,40 @@ function cellsize(
122122
end
123123

124124
"""
125-
randominside(bd::Billiard [, ω])
126-
Return a particle with random allowed initial conditions inside the given
125+
randominside(bd::Billiard [, ω]) → p
126+
Return a particle `p` with random allowed initial conditions inside the given
127127
billiard. If supplied with a second argument the
128128
type of the returned particle is `MagneticParticle`, with angular velocity `ω`.
129129
130130
**WARNING** : `randominside` works for any **convex** billiard but it does
131131
not work for non-convex billiards. (this is because it uses `distance`)
132132
"""
133-
randominside(bd::Billiard) = Particle(_randominside(bd)...)
134-
randominside(bd::Billiard{T}, ω) where {T} =
133+
randominside(bd::Billiard, ω::Nothing = nothing) = Particle(_randominside(bd)...)
134+
randominside(bd::Billiard{T}, ω::Real) where {T} =
135135
MagneticParticle(_randominside(bd)..., T(ω))
136136

137-
138-
139-
function _randominside(bd::Billiard{T}) where {T<:AbstractFloat}
140-
#1. position
137+
"""
138+
randominside_xyφ(bd::Billiard) → x, y, φ
139+
Same as [`randominside`](@ref) but only returns position and direction.
140+
"""
141+
function randominside_xyφ(bd::Billiard{T}) where {T<:AbstractFloat}
141142
xmin::T, ymin::T, xmax::T, ymax::T = cellsize(bd)
142143
xp = T(rand())*(xmax-xmin) + xmin
143144
yp = T(rand())*(ymax-ymin) + ymin
144145
pos = SV{T}(xp, yp)
145146
dist = distance_init(pos, bd)
146-
147147
while dist <= sqrt(eps(T))
148148
xp = T(rand())*(xmax-xmin) + xmin
149149
yp = T(rand())*(ymax-ymin) + ymin
150150
pos = SV{T}(xp, yp)
151151
dist = distance_init(pos, bd)
152152
end
153+
return pos[1], pos[2], T(2π*rand())
154+
end
153155

154-
#2. velocity
155-
φ = T(2π*rand())
156+
function _randominside(bd::Billiard{T}) where {T<:AbstractFloat}
157+
x, y, φ = randominside_xyφ(bd)
158+
pos = SV{T}(x, y)
156159
vel = SV{T}(sincos(φ)...)
157160
return pos, vel, zero(SV{T}) # this zero is the `current_cell`
158161
end

src/billiards/particles.jl

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export AbstractParticle, Particle, MagneticParticle
1+
export AbstractParticle, Particle, MagneticParticle, particlebeam
22
####################################################
3-
## Particles
3+
## Particle
44
####################################################
55
"""
66
AbstractParticle
@@ -52,7 +52,9 @@ print(io, "Particle{$T}\n",
5252
"position: $(p.pos+p.current_cell)\nvelocity: $(p.vel)")
5353

5454

55-
55+
####################################################
56+
## MagneticParticle
57+
####################################################
5658
"""
5759
```julia
5860
MagneticParticle(ic::AbstractVector{T}, ω::Real) # where ic = [x0, y0, φ0]
@@ -134,3 +136,24 @@ function cyclotron(p, use_cell)
134136
pos = use_cell ? p.pos + p.current_cell : p.pos
135137
return pos, p.r
136138
end
139+
140+
####################################################
141+
## Aux
142+
####################################################
143+
"""
144+
particlebeam(x0, y0, φ, N, dx, ω = nothing) → ps
145+
Make `N` particles, all with direction `φ`, starting at `x0, y0`. The particles
146+
don't all have the same position, but are instead spread by up to `dx` in the
147+
direction normal to `φ`.
148+
"""
149+
function particlebeam(x0, y0, φ, N, dx, ω = nothing, T = Float64)
150+
n = sincos(φ)
151+
xyφs = [
152+
T.((x0 + i*dx*n[1]/N, y0 + i*dx*n[2]/N, φ)) for i in range(-N/2, N/2; length = N)
153+
]
154+
if isnothing(ω)
155+
ps = [Particle(z...) for z in xyφs]
156+
else
157+
ps = [MagneticParticle(z..., T(ω)) for z in xyφs]
158+
end
159+
end

0 commit comments

Comments
 (0)