Skip to content

Commit

Permalink
Add spin functions in docs and save functions in particle group
Browse files Browse the repository at this point in the history
  • Loading branch information
pnavaro committed Apr 11, 2020
1 parent 3b8a86d commit 32d5385
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ notebooks/weibel_instablity.ipynb
notebooks/distributions.ipynb
notebooks/strong_landau_damping.ipynb
notebooks/quietstart.ipynb
*.csv
*.jld2
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
FastGaussQuadrature = "442a2c76-b920-505d-bb47-c5924d526838"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Sobol = "ed01d8cd-4d21-5b2a-85b4-cc3bdc58bad4"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
julia = "^1"
DataFrames = "^0"
Distributions = "^0"
FFTW = "^1"
FastGaussQuadrature = "^0"
Sobol = "^1"
StaticArrays = "^0"
julia = "^1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
6 changes: 6 additions & 0 deletions docs/src/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ Modules = [GEMPIC]
Pages = ["diagnostics.jl"]
```

## Spin version

```@autodocs
Modules = [GEMPIC]
Pages = ["diagnostics_spin.jl"]
```
7 changes: 7 additions & 0 deletions docs/src/distributions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ Modules = [GEMPIC]
Pages = ["distributions.jl"]
```

## Spin version

```@autodocs
Modules = [GEMPIC]
Pages = ["distributions_spin.jl"]
```

7 changes: 7 additions & 0 deletions docs/src/hamiltonian_splitting.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ Modules = [GEMPIC]
Pages = ["hamiltonian_splitting.jl"]
```

## Spin version

```@autodocs
Modules = [GEMPIC]
Pages = ["hamiltonian_splitting_spin.jl"]
```

6 changes: 6 additions & 0 deletions docs/src/particle_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ Modules = [GEMPIC]
Pages = ["particle_group.jl"]
```

## Spin version

```@autodocs
Modules = [GEMPIC]
Pages = ["particle_group_spin.jl"]
```
7 changes: 7 additions & 0 deletions docs/src/particle_mesh_coupling.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ Modules = [GEMPIC]
Pages = ["particle_mesh_coupling.jl"]
```

## Spin version

```@autodocs
Modules = [GEMPIC]
Pages = ["particle_mesh_coupling_spin.jl"]
```

7 changes: 7 additions & 0 deletions docs/src/particle_sampling.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ Modules = [GEMPIC]
Pages = ["particle_sampling.jl"]
```

## Spin version

```@autodocs
Modules = [GEMPIC]
Pages = ["particle_sampling_spin.jl"]
```

26 changes: 4 additions & 22 deletions notebooks/strong_landau_damping_spin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,6 @@ function run( steps)

store = zeros(ComplexF64,nx)

ss11 = Float64[]
ss12 = Float64[]
ss13 = Float64[]
ss21 = Float64[]
ss22 = Float64[]
ss23 = Float64[]
ss31 = Float64[]
ss32 = Float64[]
ss33 = Float64[]

electric = zeros(ComplexF64, steps, nx)

@showprogress 1 for j = 1:steps # loop over time
Expand All @@ -150,21 +140,13 @@ function run( steps)
fft!(store)
electric[j,:] .= store

push!(ss11, GEMPIC.get_s1(propagator.particle_group, 1))
push!(ss12, GEMPIC.get_s2(propagator.particle_group, 1))
push!(ss13, GEMPIC.get_s3(propagator.particle_group, 1))

push!(ss21, GEMPIC.get_s1(propagator.particle_group, 100))
push!(ss22, GEMPIC.get_s2(propagator.particle_group, 100))
push!(ss23, GEMPIC.get_s3(propagator.particle_group, 100))

push!(ss31, GEMPIC.get_s1(propagator.particle_group, 800))
push!(ss32, GEMPIC.get_s2(propagator.particle_group, 800))
push!(ss33, GEMPIC.get_s3(propagator.particle_group, 800))
if step % 1000 == 0
save( "particles", step, particle_group)
end

end

return thdiag.data
thdiag.data

end

Expand Down
2 changes: 1 addition & 1 deletion src/landau_damping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct LandauDamping
end

"""
sample!( LandauDamping(α, kx), pg)
sample!( d, pg)
Sampling from a probability distribution to initialize
a Landau damping
Expand Down
16 changes: 14 additions & 2 deletions src/particle_group.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Printf, FileIO, JLD2

export ParticleGroup

abstract type AbstractParticleGroup end
Expand Down Expand Up @@ -164,9 +166,9 @@ end
Set weights of particle @ i
"""
@generated function set_weights( p :: ParticleGroup{D,V}, i :: Int64, w :: Float64 ) where {D, V}
function set_weights( p :: ParticleGroup{D,V}, i :: Int64, w :: Float64 ) where {D, V}

:(p.particle_array[$D+$V+1, i] = w)
p.particle_array[D+V+1, i] = w

end

Expand All @@ -180,3 +182,13 @@ function set_common_weight( p :: AbstractParticleGroup, x :: Float64 )
p.common_weight = x

end

function save( file, step, p :: ParticleGroup{D,V}) where {D, V}

datafile = @sprintf("%s-%06d.jld2", file, step)

FileIO.save(datafile, Dict("x" => p.particle_array[1:D,:],
"v" => p.particle_array[D+1:D+V,:],
"w" => p.particle_array[D+V+1:D+V+1,:]))

end
15 changes: 15 additions & 0 deletions src/particle_group_spin.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using JLD2, FileIO, Printf


export SpinParticleGroup

"""
Expand Down Expand Up @@ -234,3 +237,15 @@ Set weights of particle @ i
:(p.particle_array[$D+$V+$S+1, i] = w)

end


function save( file, step, p :: SpinParticleGroup{D,V,S}) where {D, V, S}

datafile = @sprintf("%s-%06d.jld2", file, step)

FileIO.save(datafile, Dict("x" => p.particle_array[1:D,:],
"v" => p.particle_array[D+1:D+V,:],
"s" => p.particle_array[D+V+1:D+V+S,:],
"w" => p.particle_array[D+V+S+1,:]))

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ include("test_sampling.jl")
include("test_particle_mesh_coupling_spline_1d.jl")
include("test_hamiltonian_splitting.jl")
include("test_hamiltonian_splitting_boris.jl")
include("test_save_particles.jl")
53 changes: 53 additions & 0 deletions test/test_save_particles.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@testset "Save particles data into file" begin

n_particles = 100000
xmin = 1.0 :: Float64
xmax = 4π + 1.0
Lx = xmax - xmin
nx = 64

mesh = Mesh( xmax, xmin, nx)

pg = ParticleGroup{1,2}(n_particles, 1.0, 1.0, 1)

params = ( k = [[0.5]],
α = [0.01],
σ = [[0.1, 2.0]],
μ = [[0.0, 0.0]]
)

df = CosSumGaussian{1,2}(params...)

n_particles = pg.n_particles

sampler = ParticleSampler{1, 2}( :sobol, true, n_particles )

sample!( pg, sampler, df, mesh )

GEMPIC.save( "test", 1, pg)

@test isfile("test-000001.jld2")

σ, μ = 0.02, 0.0
kx, α = 1.004355, 0.001
xmin, xmax = 0, 2π/kx
domain = [xmin, xmax, xmax - xmin]
nx = 64
n_particles = 1000
mesh = Mesh( xmin, xmax, nx)
spline_degree = 3

df = SpinCosSumGaussian{1,1,3}([[kx]], [α], [[σ]], [[μ]] )

mass, charge = 1.0, 1.0

spg = SpinParticleGroup{1,1,3}( n_particles, mass, charge, 1)
sampler = SpinParticleSampler{1,1,3}( :sobol, n_particles)

sample!(spg, sampler, df, mesh)

GEMPIC.save( "test", 2, spg)

@test isfile("test-000002.jld2")

end

0 comments on commit 32d5385

Please sign in to comment.