|
| 1 | +# # Modifying or extending components of TrixiParticles.jl within a simulation file |
| 2 | + |
| 3 | +# In this tutorial, we will show how to replace components |
| 4 | +# of TrixiParticles.jl by custom implementations from within a simulation file, |
| 5 | +# without ever cloning the repository. |
| 6 | + |
| 7 | +# First, we import TrixiParticles.jl and |
| 8 | +# [OrdinaryDiffEq.jl](https://github.com/SciML/OrdinaryDiffEq.jl), which we will |
| 9 | +# use at the very end for the time integration. |
| 10 | +using TrixiParticles |
| 11 | +using OrdinaryDiffEq |
| 12 | + |
| 13 | +# ## Load a simulation file |
| 14 | + |
| 15 | +# Instead of setting up the simulation from scratch, we run a pre-defined |
| 16 | +# simulation file that implements a 2D dam break problem with |
| 17 | +# Weakly Compressible Smoothed Particle Hydrodynamics (WCSPH). |
| 18 | +# This tutorial works with any other simulation file as well, as long as |
| 19 | +# it contains an assignment `smoothing_kernel = ...`. |
| 20 | +# By using [`trixi_include`](@ref), we can overwrite variables defined |
| 21 | +# in the simulation file. |
| 22 | +# Here, we pass `sol=nothing` to overwrite the line that defines the variable `sol`, |
| 23 | +# which is the line that runs the actual simulation. |
| 24 | +# This way, we can import all variables and definitions from the simulation file |
| 25 | +# without running the simulation right away. |
| 26 | +# In this tutorial, we only need the definition of the particle spacing, |
| 27 | +# but this approach works for any variable defined in the simulation file |
| 28 | +# and is used in many pre-defined example simulation files. |
| 29 | +# See [the tutorial on setting up a simulation](@ref tut_setup) for details on the |
| 30 | +# general structure of a simulation file. |
| 31 | +trixi_include(@__MODULE__, joinpath(examples_dir(), "fluid", "dam_break_2d.jl"), |
| 32 | + sol=nothing) |
| 33 | + |
| 34 | +# ## Replacing components with custom implementations |
| 35 | + |
| 36 | +# In this tutorial, we want to replace the smoothing kernel used in the simulation |
| 37 | +# by a custom implementation. |
| 38 | +# A good starting point is to check out the available implementations in |
| 39 | +# TrixiParticles.jl, then copy the relevant functions to the simulation file |
| 40 | +# and modify them as needed. |
| 41 | + |
| 42 | +# ### Custom smoothing kernel |
| 43 | + |
| 44 | +# To implement a custom smoothing kernel, we define a struct extending |
| 45 | +# `TrixiParticles.AbstractSmoothingKernel`. |
| 46 | +# This abstract struct has a type parameter for the number of dimensions, |
| 47 | +# which we set to 2 in this case. |
| 48 | +struct MyGaussianKernel <: TrixiParticles.AbstractSmoothingKernel{2} end |
| 49 | +# This kernel is going to be an implementation of the Gaussian kernel with |
| 50 | +# a cutoff for compact support, which reads |
| 51 | +# ```math |
| 52 | +# W(r, h) = |
| 53 | +# \begin{cases} |
| 54 | +# \frac{1}{\pi h^2} \exp(-(r/h)^2) & \text{for } r < 2h\\ |
| 55 | +# 0 & \text{for } r \geq 2h. |
| 56 | +# \end{cases} |
| 57 | +# ``` |
| 58 | +# Note that the same kernel in a more optimized version and with a cutoff at ``3`` |
| 59 | +# is already implemented in TrixiParticles.jl as [`GaussianKernel`](@ref). |
| 60 | + |
| 61 | +# By looking at the implementation of existing kernels in TrixiParticles.jl, |
| 62 | +# we can see that a kernel implementation requires three functions. |
| 63 | +# `TrixiParticles.kernel`, which is the kernel function itself, |
| 64 | +# `TrixiParticles.kernel_deriv`, which is the derivative of the kernel function, |
| 65 | +# and `TrixiParticles.compact_support`, which defines the compact support of the |
| 66 | +# kernel in relation to the smoothing length. |
| 67 | +# The latter is relevant for determining the search radius of the neighborhood search. |
| 68 | +function TrixiParticles.kernel(kernel::MyGaussianKernel, r, h) |
| 69 | + q = r / h |
| 70 | + |
| 71 | + if q < 2 |
| 72 | + return 1 / (pi * h^2) * exp(-q^2) |
| 73 | + end |
| 74 | + |
| 75 | + return 0.0 |
| 76 | +end |
| 77 | + |
| 78 | +function TrixiParticles.kernel_deriv(kernel::MyGaussianKernel, r, h) |
| 79 | + q = r / h |
| 80 | + |
| 81 | + if q < 2 |
| 82 | + return 1 / (pi * h^2) * (-2 * q) * exp(-q^2) / h |
| 83 | + end |
| 84 | + |
| 85 | + return 0.0 |
| 86 | +end |
| 87 | + |
| 88 | +TrixiParticles.compact_support(::MyGaussianKernel, h) = 2 * h |
| 89 | + |
| 90 | +# For this kernel, we use a smoothing length of 1.2 times the particle spacing, |
| 91 | +# which yields a similar kernel shape as the commonly used Wendland C2 kernel |
| 92 | +# with a smoothing length of 1.59 times the particle spacing. |
| 93 | +smoothing_length = 1.2 * fluid_particle_spacing |
| 94 | +smoothing_length_wendland = 1.59 * fluid_particle_spacing |
| 95 | +nothing # hide |
| 96 | +# We can compare these kernels in a plot. |
| 97 | +using Plots |
| 98 | +plot(r -> TrixiParticles.kernel(WendlandC2Kernel{2}(), abs(r), smoothing_length_wendland), |
| 99 | + -3 * fluid_particle_spacing, 3 * fluid_particle_spacing, |
| 100 | + label="WendlandC2Kernel", xlabel="r") |
| 101 | +plot!(r -> TrixiParticles.kernel(MyGaussianKernel(), abs(r), smoothing_length), |
| 102 | + label="MyGaussianKernel") |
| 103 | +plot!(dpi=200) # hide |
| 104 | +savefig("tut_custom_kernel_plot2.png"); # hide |
| 105 | +#  |
| 106 | + |
| 107 | +# This is all we need to use our custom kernel implementation in a simulation. |
| 108 | +# We only need to replace the definition above by |
| 109 | +smoothing_kernel = MyGaussianKernel() |
| 110 | +nothing # hide |
| 111 | +# and run the simulation file again. |
| 112 | + |
| 113 | +# In order to use our kernel in a pre-defined example file, we can use the function |
| 114 | +# [`trixi_include`](@ref) to replace the definition of the variable `smoothing_kernel`. |
| 115 | +# The following will run the example simulation |
| 116 | +# `examples/fluid/dam_break_2d.jl` with our custom kernel and the corresponding |
| 117 | +# smoothing length. |
| 118 | +# ```@cast @__NAME__; width=100, height=50, delay=0, loop=true, loop_delay=5 |
| 119 | +# trixi_include(@__MODULE__, joinpath(examples_dir(), "fluid", "dam_break_2d.jl"), |
| 120 | +# smoothing_kernel=MyGaussianKernel(), |
| 121 | +# smoothing_length=smoothing_length); |
| 122 | +# ``` |
| 123 | +trixi_include(joinpath(examples_dir(), "fluid", "dam_break_2d.jl"), #!md |
| 124 | + smoothing_kernel=MyGaussianKernel(), #!md |
| 125 | + smoothing_length=smoothing_length) #!md |
| 126 | + |
| 127 | +# See [Visualization](@ref) for how to visualize the final solution. |
| 128 | +# For the simplest visualization, we can use [Plots.jl](https://docs.juliaplots.org/stable/): |
| 129 | +using Plots |
| 130 | +plot(sol, ylims=(-Inf, 1)) |
| 131 | +plot!(dpi=200) # hide |
| 132 | +savefig("tut_custom_kernel_plot.png"); # hide |
| 133 | +#  |
0 commit comments