diff --git a/docs/Project.toml b/docs/Project.toml index fb5235a..c3b4a31 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -11,5 +11,6 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [compat] +DemoCards = "0.5.1" Documenter = "0.27" Literate = "2" diff --git a/docs/make.jl b/docs/make.jl index 70d212b..ae3a478 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -3,15 +3,9 @@ using Documenter, DocumenterCitations, DemoCards using Literate using ParticleInCell2 -@info "Generating tutorial materials from Literate script" -tutorial_path = joinpath(@__DIR__, "literate_src", "tutorial.jl") -tutorial_output_path = joinpath(@__DIR__, "src") -Literate.markdown(tutorial_path, tutorial_output_path, documenter = true) -Literate.notebook(tutorial_path, tutorial_output_path, documenter = true) -Literate.script(tutorial_path, tutorial_output_path, documenter = true) - @info "Generating examples using DemoCards" -examples_page, postprocess_democard_cb, demo_assets = makedemos("literate_src/examples") +examples_page, postprocess_democard_cb, demo_assets = + makedemos("../examples", filter_function = x -> !endswith(x.path, "_test.jl")) @info "Gathering information from Project.toml" PROJECT_TOML = Pkg.TOML.parsefile(joinpath(@__DIR__, "..", "Project.toml")) @@ -37,7 +31,7 @@ makedocs( ), pages = [ "Introduction" => "index.md", - "Tutorial" => "tutorial.md", + "Tutorial" => "examples/tutorial/langmuir_oscillation.md", examples_page, "Plasma simulation theory" => [ "Introduction" => "theory/index.md", @@ -52,10 +46,5 @@ makedocs( @info "Postprocessing DemoCards" postprocess_democard_cb() -@info "Clean up generated tutorial materials" -rm(joinpath(tutorial_output_path, "tutorial.md")) -rm(joinpath(tutorial_output_path, "tutorial.jl")) -rm(joinpath(tutorial_output_path, "tutorial.ipynb")) - @info "Deploying docs" deploydocs(repo = "github.com/adamslc/ParticleInCell2.jl.git") diff --git a/docs/src/index.md b/docs/src/index.md index 0a52b44..71a87e3 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -4,7 +4,7 @@ ParticleInCell2 ``` ## Documentation sections -To get started, look at the [Tutorial](@ref), which includes step-by-step +To get started, look at the [Tutorial](@ref tutorial_langmuir), which includes step-by-step instructions for running your first simulation using `ParticleInCell2`. After that, you may want to browse the [Example Gallery](@ref), to see other problems that this package can be used to solve. The [Plasma Simulation Theory](@ref) diff --git a/docs/literate_src/examples/config.json b/examples/config.json similarity index 82% rename from docs/literate_src/examples/config.json rename to examples/config.json index 94635dc..c75d518 100644 --- a/docs/literate_src/examples/config.json +++ b/examples/config.json @@ -1,6 +1,7 @@ { "theme": "list", "order": [ + "tutorial", "electrostatic", "electromagnetic" ] diff --git a/docs/literate_src/examples/electromagnetic/beam_heating.jl b/examples/electromagnetic/beam_heating.jl similarity index 100% rename from docs/literate_src/examples/electromagnetic/beam_heating.jl rename to examples/electromagnetic/beam_heating.jl diff --git a/examples/electromagnetic/electromagnetic_test.jl b/examples/electromagnetic/electromagnetic_test.jl new file mode 100644 index 0000000..6da63ad --- /dev/null +++ b/examples/electromagnetic/electromagnetic_test.jl @@ -0,0 +1,3 @@ +@testitem "Beam heating example" tags = [:integration, :example] begin + include("beam_heating.jl") +end diff --git a/docs/literate_src/examples/electrostatic/beam_cyclotron.jl b/examples/electrostatic/beam_cyclotron.jl similarity index 100% rename from docs/literate_src/examples/electrostatic/beam_cyclotron.jl rename to examples/electrostatic/beam_cyclotron.jl diff --git a/docs/literate_src/examples/electrostatic/beam_plasma.jl b/examples/electrostatic/beam_plasma.jl similarity index 100% rename from docs/literate_src/examples/electrostatic/beam_plasma.jl rename to examples/electrostatic/beam_plasma.jl diff --git a/docs/literate_src/examples/electrostatic/config.json b/examples/electrostatic/config.json similarity index 100% rename from docs/literate_src/examples/electrostatic/config.json rename to examples/electrostatic/config.json diff --git a/examples/electrostatic/electrostatic_test.jl b/examples/electrostatic/electrostatic_test.jl new file mode 100644 index 0000000..0b86780 --- /dev/null +++ b/examples/electrostatic/electrostatic_test.jl @@ -0,0 +1,15 @@ +@testitem "Two-stream example" tags = [:integration, :example] begin + include("two_stream.jl") +end + +@testitem "beam-plasma example" tags = [:integration, :example] begin + include("beam_plasma.jl") +end + +@testitem "beam-cyclotron example" tags = [:integration, :example] begin + include("beam_cyclotron.jl") +end + +@testitem "Landau damping example" tags = [:integration, :example] begin + include("landau_damping.jl") +end diff --git a/docs/literate_src/examples/electrostatic/landau_damping.jl b/examples/electrostatic/landau_damping.jl similarity index 100% rename from docs/literate_src/examples/electrostatic/landau_damping.jl rename to examples/electrostatic/landau_damping.jl diff --git a/docs/literate_src/examples/electrostatic/two_stream.jl b/examples/electrostatic/two_stream.jl similarity index 100% rename from docs/literate_src/examples/electrostatic/two_stream.jl rename to examples/electrostatic/two_stream.jl diff --git a/docs/literate_src/examples/index.md b/examples/index.md similarity index 100% rename from docs/literate_src/examples/index.md rename to examples/index.md diff --git a/docs/literate_src/tutorial.jl b/examples/tutorial/langmuir_oscillation.jl similarity index 97% rename from docs/literate_src/tutorial.jl rename to examples/tutorial/langmuir_oscillation.jl index 3f31a54..814fee6 100644 --- a/docs/literate_src/tutorial.jl +++ b/examples/tutorial/langmuir_oscillation.jl @@ -1,9 +1,7 @@ -# # Tutorial -#md # !!! tip -#md # You can also view this tutorial as a -#md # [Jupyter notebook](@__NBVIEWER_ROOT_URL__/tutorial.ipynb) or you can -#md # download the plain Julia [script](tutorial.jl). -# +# --- +# title: "Tutorial: Langmuir oscillations" +# id: "tutorial_langmuir" +# --- # This tutorial will get you up and running using `ParticleInCell2` by showing # you how to model one of the simplest phenomena in plasma physics: an # electrostatic (or Langmuir) oscillation. diff --git a/examples/tutorial/tutorial_test.jl b/examples/tutorial/tutorial_test.jl new file mode 100644 index 0000000..15716a3 --- /dev/null +++ b/examples/tutorial/tutorial_test.jl @@ -0,0 +1,3 @@ +@testitem "LangmuirOscillationTest" tags = [:integration, :example, :tutorial] begin + include("langmuir_oscillation.jl") +end diff --git a/test/Project.toml b/test/Project.toml index ee5657e..61039fa 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,6 +1,8 @@ [deps] Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" +CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341" +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" ReTestItems = "817f1d60-ba6b-4fd5-9520-3cf149f6a823" StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/test/langmuir_oscillation_test.jl b/test/langmuir_oscillation_test.jl deleted file mode 100644 index 27364e7..0000000 --- a/test/langmuir_oscillation_test.jl +++ /dev/null @@ -1,100 +0,0 @@ -@testitem "Langmuir Oscillation" tags = [:integration] begin - using StaticArrays - using FFTW - - function find_max_freq(xs, dt) - amps = abs.(fft(xs)) - amps[1] = 0 - max_index = findmax(amps)[2] - return fftfreq(length(xs), 1 / dt)[max_index] * 2pi - end - - function compute_plasma_freq(nom_density) - epsilon_0 = 8.8e-12 - charge = 1.6e-19 - mass = 9e-31 - - return sqrt(nom_density * charge^2 / mass / epsilon_0) - end - - function simulate_plamsa_freq(nom_density) - sim_length = 1.0 - num_cells = 32 - dx = sim_length / num_cells - num_particles = num_cells * 10 - particles_per_macro = nom_density * sim_length / num_particles - dt = 1.11e-11 * 2 - n_steps = 1000 - k = 1 - amplitude = 1e3 - thermal_amp = 0.0 - - epsilon_0 = 8.8e-12 - charge = 1.6e-19 * particles_per_macro - mass = 9e-31 * particles_per_macro - - g = UniformCartesianGrid((0.0,), (sim_length,), (num_cells,), (true,)) - rho = Field(g, ParticleInCell2.node, 1, 1) - phi = Field(g, ParticleInCell2.node, 1, 1) - Eedge = Field(g, ParticleInCell2.edge, 1, 1) - Enode = Field(g, ParticleInCell2.node, 1, 1) - - positions = Vector{SVector{1,Float64}}(undef, num_particles) - momentums = Vector{SVector{1,Float64}}(undef, num_particles) - weights = Vector{Float64}(undef, num_particles) - - for i in eachindex(positions) - positions[i] = SVector((i - 1) / num_particles) - momentums[i] = SVector( - mass * ( - amplitude * sin((i - 1) / num_particles * k * 2pi) + - thermal_amp * randn() - ), - ) - weights[i] = 1.0 - end - electrons = Species(positions, momentums, weights, charge, mass) - - charge_interp = BSplineChargeInterpolation(electrons, rho, 1) - comm_rho = CommunicateGuardCells(rho, true) - field_solve = PoissonSolveFFT(rho, phi) - comm_phi = CommunicateGuardCells(phi) - finite_diff = FiniteDifferenceToEdges(phi, Eedge) - comm_Eedge = CommunicateGuardCells(Eedge) - elec_ave = AverageEdgesToNodes(Eedge, Enode) - comm_Enode = CommunicateGuardCells(Enode) - push = ElectrostaticParticlePush(electrons, Enode, dt) - comm_electrons = CommunicateSpecies(electrons, g) - - electric_field_energy = Vector{Float64}(undef, n_steps) - for n = 1:n_steps - electric_field_energy[n] = 0 - for I in eachindex(Enode) - electric_field_energy[n] += (dx * epsilon_0 / 2) * (Enode.values[I])^2 - end - - # TODO - rho.values .= 0 - - step!(charge_interp) - step!(comm_rho) - step!(field_solve) - step!(comm_phi) - step!(finite_diff) - step!(comm_Eedge) - step!(elec_ave) - step!(comm_Enode) - step!(push) - step!(comm_electrons) - end - - max_freq = abs(find_max_freq(electric_field_energy, dt)) - # Divide by 2 because the electric field energy goes through a maximum twice - # per plasma oscillation - return max_freq / 2 - end - - # Allow a 10% error - @test isapprox(compute_plasma_freq(1e14), simulate_plamsa_freq(1e14), rtol = 0.1) - @test isapprox(compute_plasma_freq(1e15), simulate_plamsa_freq(1e15), rtol = 0.1) -end