-
Notifications
You must be signed in to change notification settings - Fork 24
Add tutorial on visualization with Plots.jl and interpolation #1298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
efaulhaber
wants to merge
9
commits into
main
Choose a base branch
from
ef/tut-visualization-plots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
36f1217
Add tutorial on visualization with Plots.jl and interpolation
efaulhaber 220e331
Rename file
efaulhaber 8223058
Fix
efaulhaber 1d5f08a
Fix
efaulhaber cb74254
Fix CI
efaulhaber b8b40e5
Fix broken plots
efaulhaber 9356af0
Merge branch 'main' into ef/tut-visualization-plots
efaulhaber edd4dfd
Implement and use plotting of a single system
efaulhaber df6c37e
Add NEWS.md entry
efaulhaber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # # [Visualizing particle data with Plots.jl](@id tut_visualization) | ||
|
|
||
| # In this tutorial, we run the two-dimensional vortex street from | ||
| # [`examples/fluid/vortex_street_2d.jl`](https://github.com/trixi-framework/TrixiParticles.jl/blob/main/examples/fluid/vortex_street_2d.jl) | ||
| # and visualize the particle data with [`Plots.jl`](https://github.com/juliaplots/plots.jl). | ||
|
|
||
| using TrixiParticles | ||
| using Plots | ||
| #src # Reset GR's process-wide color table, which can be exhausted by earlier tutorials. | ||
| Plots.closeall() # hide | ||
|
|
||
| # The example defines the particle spacing as `particle_spacing_factor * cylinder_diameter`. | ||
| # We deliberately use a very coarse particle resolution. This makes the distinction between | ||
| # the discrete particles and the interpolated field in the next section clear. | ||
| # To remove visual clutter, we disable the info callback. | ||
| # Since we visualize with Plots.jl, we also disable the saving callback. | ||
| trixi_include(@__MODULE__, | ||
| joinpath(examples_dir(), "fluid", "vortex_street_2d.jl"); | ||
| particle_spacing_factor=0.2, | ||
| info_callback=nothing, saving_callback=nothing); | ||
| nothing # hide | ||
|
|
||
| # ## Visualizing discrete particles | ||
|
|
||
| # SPH stores the solution on moving particles. The standard plotting recipe provides the | ||
| # quickest way to inspect their distribution at the final time. We color the fluid particles | ||
| # by the magnitude of the velocity stored on each particle. | ||
| v_ode, _ = sol.u[end].x | ||
|
efaulhaber marked this conversation as resolved.
|
||
| v_fluid = TrixiParticles.wrap_v(v_ode, fluid_system, semi) | ||
|
|
||
| active_particles = TrixiParticles.eachparticle(fluid_system) | ||
| particle_velocity = TrixiParticles.current_velocity(v_fluid, | ||
| fluid_system)[:, active_particles] | ||
| particle_velocity_magnitude = vec(sqrt.(sum(abs2, particle_velocity; dims=1))) | ||
|
|
||
| particle_plot = plot(fluid_system, sol; zcolor=particle_velocity_magnitude, color=:viridis, | ||
| xlims=(0.25, 1.8), ylims=(0.1, 0.9), legend=false, | ||
| xlabel="x", ylabel="y", colorbar=true, colorbar_title="|v|", | ||
| size=(900, 450)) | ||
| plot!(particle_plot; dpi=200) # hide | ||
| savefig(particle_plot, "tut_visualization_particles.png") # hide | ||
| nothing # hide | ||
|
|
||
| #  | ||
|
|
||
| # ## Interpolating particle data onto a regular grid | ||
|
|
||
| # Smoothed particle hydrodynamics (SPH) represents a continuous (smoothed) field | ||
| # by a discrete set of particles. While visualizing individual particles is straightforward | ||
| # and often sufficient, in order to visualize the actual field approximation, | ||
| # the particle data must be interpolated. | ||
| # | ||
| # Importantly, interpolation does not add physical resolution: features that are not resolved | ||
| # by the particles cannot be recovered by choosing a finer interpolation grid. | ||
| # It simply visualizes the SPH approximation instead of only the interpolation points. | ||
|
efaulhaber marked this conversation as resolved.
|
||
|
|
||
| # [`interpolate_plane_2d`](@ref) constructs regularly spaced sample points between two corners | ||
| # and uses the SPH kernel to reconstruct the requested fields there. The interpolation spacing | ||
| # is one quarter of the particle spacing, so the plot contains many more pixels than | ||
| # the simulation contains particles. | ||
| interpolation_min = [0.0, 0.0] | ||
| interpolation_max = domain_size | ||
| interpolation_spacing = particle_spacing / 4 | ||
|
|
||
| interpolated = interpolate_plane_2d(interpolation_min, interpolation_max, | ||
| interpolation_spacing, semi, fluid_system, sol) | ||
| interpolated_velocity_magnitude = vec(sqrt.(sum(abs2, interpolated.velocity; dims=1))) | ||
| nothing # hide | ||
|
|
||
| # The returned named tuple also contains `pressure`, `density`, `neighbor_count`, and | ||
| # `computed_density`. Here we visualize the magnitude of the interpolated velocity. | ||
| interpolated_plot = scatter(interpolated.point_coords[1, :], | ||
| interpolated.point_coords[2, :]; | ||
| marker_z=interpolated_velocity_magnitude, | ||
| color=:viridis, | ||
| marker=:square, markerstrokewidth=0, markersize=2.5, | ||
| aspect_ratio=:equal, size=(900, 450), | ||
| xlims=(0.25, 1.8), ylims=(0.1, 0.9), xlabel="x", ylabel="y", | ||
| label=nothing, colorbar_title="|v|") | ||
| plot!(interpolated_plot; dpi=200) # hide | ||
| savefig(interpolated_plot, "tut_visualization_interpolated_velocity.png") # hide | ||
| nothing # hide | ||
|
|
||
| #  | ||
|
|
||
| # Compared with the visibly discrete particle distribution, the interpolated field shows | ||
|
efaulhaber marked this conversation as resolved.
|
||
| # much more detail, representing the continuous SPH approximation of the solution. | ||
|
|
||
| # To write the same reconstruction as a VTI image for ParaView, replace the interpolation call | ||
| # above with [`interpolate_plane_2d_vtk`](@ref): | ||
| interpolate_plane_2d_vtk(interpolation_min, interpolation_max, interpolation_spacing, | ||
|
efaulhaber marked this conversation as resolved.
|
||
| semi, fluid_system, sol; filename="vortex_street_velocity") | ||
| nothing # hide | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.