Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ used in the Julia ecosystem. Notable changes will be documented in this file for
- Fixed the viscous force on structures in fluid-structure interaction, which had the wrong
sign (#1348).

### Documentation

- Added a tutorial for setting up 2D simulations from geometry files, including a curved
pipe and a coastline dam-break basin (#1094).

## Version 0.5.3

### Features
Expand Down
143 changes: 143 additions & 0 deletions docs/literate/src/tut_2d_geometry.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# # [Setting up a 2D simulation from geometry files](@id tut_2d_geometry)

# In this tutorial, we build two 2D setups from geometry files:
# 1. a curved pipe, where one geometry file defines the outer wall and another
# defines the channel cut out of it,
# 2. a dam-break basin with a coastline profile, where one geometry file defines
# the coastline wall and the seawall on the right.
#
# We use 2D geometry formats such as `.asc` or `.dxf` for 2D setups.
# STL files are supported only for 3D geometries.

# First, we import TrixiParticles.jl together with
# `OrdinaryDiffEqLowStorageRK` of
# [OrdinaryDiffEq.jl](https://github.com/SciML/OrdinaryDiffEq.jl)
# and [Plots.jl](https://docs.juliaplots.org/stable/).
using TrixiParticles
using OrdinaryDiffEqLowStorageRK
using Plots

# ## Building the geometry

# We use the same particle spacing for the fluid and for the wall geometries.
particle_spacing = 0.03
fluid_density = 1000.0
gravity = 9.81
sound_speed = 10.0
state_equation = StateEquationCole(; sound_speed, reference_density=fluid_density,
exponent=7)
nothing # hide

# ### Loading the 2D geometry files

# The following helper loads a closed 2D geometry file and samples particles in its interior:
# 1. load the polygon with [`load_geometry`](@ref),
# 2. fill the polygon with [`ComplexShape`](@ref).
#
# This creates a filled 2D solid region rather than particles only along the polygon edges.
# The half-spacing `grid_offset` shifts the sampling grid in each coordinate direction
# to avoid placing particles directly on the grid-aligned edges of these geometries.
function solid_from_geometry_file(file; particle_spacing, density)
geometry = load_geometry(file)
solid = ComplexShape(geometry; particle_spacing, density,
grid_offset=0.5particle_spacing)
Comment thread
svchb marked this conversation as resolved.
Resolved

return (; geometry, solid)
end
Comment thread
svchb marked this conversation as resolved.
Resolved
nothing # hide

# ### A curved pipe from two filled geometries

# The pipe wall is an L-shaped solid region with a channel cut out of it:
# 1. one geometry file describes the outer pipe envelope,
# 2. one geometry file describes the empty channel,
# 3. `setdiff` subtracts the channel from the solid envelope.
pipe_outer_file = pkgdir(TrixiParticles, "examples", "preprocessing", "data",
"curved_pipe_outer_2d.asc")
pipe_channel_file = pkgdir(TrixiParticles, "examples", "preprocessing", "data",
"curved_pipe_channel_2d.asc")

pipe_outer = solid_from_geometry_file(pipe_outer_file; particle_spacing,
density=fluid_density)
pipe_channel = load_geometry(pipe_channel_file)

pipe_setup = (; wall=setdiff(pipe_outer.solid, pipe_channel),
outer_geometry=pipe_outer.geometry,
channel_geometry=pipe_channel)
Comment thread
svchb marked this conversation as resolved.
Resolved

p_pipe = plot(pipe_setup.wall, label="wall", size=(450, 450), showaxis=false,
xlims=(-0.03, 1.23), ylims=(-0.03, 1.23))
plot!(p_pipe, dpi=300) # hide
savefig(p_pipe, "tut_2d_geometry_pipe.png"); # hide
# ![Curved pipe wall particles](tut_2d_geometry_pipe.png)

# ### A dam-break basin with a coastline profile

# In the second setup, one 2D geometry file defines the coastline wall:
# the beach profile, a finite wall thickness below it, and the seawall on the right.
coast_file = pkgdir(TrixiParticles, "examples", "preprocessing", "data",
"coastline_profile_2d.asc")
coast = solid_from_geometry_file(coast_file; particle_spacing, density=fluid_density)
Comment thread
svchb marked this conversation as resolved.
Resolved
nothing # hide

# The geometry file defines the coastline bed and right wall as a solid region.
# We add the left wall as a rectangular particle block and place a rectangular
# dam-break water column beside it.
left_wall = RectangularShape(particle_spacing, (5, 50), (0.0, -0.12),
density=fluid_density)
reservoir = RectangularShape(particle_spacing, (28, 42), (0.15, 0.03),
acceleration=(0.0, -gravity),
state_equation=state_equation)
coast_setup = (; geometry=coast.geometry,
wall=union(coast.solid, left_wall),
fluid=setdiff(reservoir, coast.geometry))

p_coast = plot(coast_setup.fluid, coast_setup.wall, labels=["fluid" "wall"],
size=(700, 360), showaxis=false, xlims=(0.0, 2.75), ylims=(-0.15, 1.35))
plot!(p_coast, dpi=200) # hide
savefig(p_coast, "tut_2d_geometry_coast.png"); # hide
# ![Coastline dam-break initial conditions](tut_2d_geometry_coast.png)

# ## Running a coastline simulation

# We continue with the coastline setup using the systems, semidiscretization, and time
# integration described in [Setting up your simulation from scratch](@ref tut_setup).
setup = coast_setup
tspan = (0.0, 1.0)

smoothing_length = 1.2 * particle_spacing
smoothing_kernel = SchoenbergCubicSplineKernel{2}()
viscosity = ArtificialViscosityMonaghan(alpha=0.02, beta=0.0)

fluid_density_calculator = ContinuityDensity()
density_diffusion = DensityDiffusionMolteniColagrossi(delta=0.1)

fluid_system = WeaklyCompressibleSPHSystem(setup.fluid;
density_calculator=fluid_density_calculator,
state_equation, smoothing_kernel,
smoothing_length, viscosity=viscosity,
density_diffusion=density_diffusion,
acceleration=(0.0, -gravity))

boundary_model = BoundaryModelDummyParticles(setup.wall.density, setup.wall.mass,
state_equation=state_equation,
AdamiPressureExtrapolation(),
smoothing_kernel, smoothing_length)
boundary_system = WallBoundarySystem(setup.wall, boundary_model)

semi = Semidiscretization(fluid_system, boundary_system)
ode = semidiscretize(semi, tspan)

callbacks = CallbackSet(InfoCallback(interval=10))

sol = solve(ode, RDPK3SpFSAL35(), save_everystep=false, callback=callbacks)

plot(sol, title="Coastline dam break at t = $(sol.t[end])",
size=(700, 360), showaxis=false, legend=:topleft)
plot!(dpi=200) # hide
savefig("tut_2d_geometry_solution.png"); # hide
# ![Coastline dam-break final result](tut_2d_geometry_solution.png)

# For more accurate body-fitted particles around sharper features, you can also
# apply the [particle packing workflow](@ref tut_packing) to the 2D geometry files
# before starting the simulation.
3 changes: 2 additions & 1 deletion docs/literate/src/tut_packing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ plot!(geometry, linestyle=:dash, label=nothing, showaxis=false, color=:black,
# ## Particle packing

# In the following, we will essentially follow the same steps described in the fluid tutorials.
# That means we will generate systems that are then passed to the [`Semidiscretization`](@ref).
# That means we will generate systems that are then passed to the
# [`Semidiscretization`](@ref TrixiParticles.Semidiscretization).
# The difference from a typical physical simulation is that we use [`ParticlePackingSystem`](@ref),
# which does not represent any physical law. Instead, we only use the simulation framework to time-integrate
# the packing process.
Expand Down
3 changes: 2 additions & 1 deletion docs/literate/src/tut_setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ nothing # hide

# ## [Semidiscretization](@id tut_setup_semi)

# The key component of every simulation is the [`Semidiscretization`](@ref),
# The key component of every simulation is the
# [`Semidiscretization`](@ref TrixiParticles.Semidiscretization),
# which couples all systems of the simulation.
# All simulation methods in TrixiParticles.jl are semidiscretizations, which discretize
# the equations in space to provide an ordinary differential equation that still
Expand Down
4 changes: 4 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Literate.markdown(joinpath("docs", "literate", "src", "tut_rigid_body_fsi.jl"),
joinpath("docs", "src", "tutorials"))
Literate.markdown(joinpath("docs", "literate", "src", "tut_packing.jl"),
joinpath("docs", "src", "tutorials"))
Literate.markdown(joinpath("docs", "literate", "src", "tut_2d_geometry.jl"),
joinpath("docs", "src", "tutorials"))

copy_file("AUTHORS.md",
"in the [LICENSE.md](LICENSE.md) file" => "under [License](@ref)")
Expand Down Expand Up @@ -94,6 +96,8 @@ makedocs(sitename="TrixiParticles.jl",
"tut_rigid_body_fsi.md")
],
"Preprocessing" => [
"Setting up a 2D simulation from geometry files" => joinpath("tutorials",
"tut_2d_geometry.md"),
"Particle packing tutorial" => joinpath("tutorials",
"tut_packing.md")
]
Expand Down
3 changes: 2 additions & 1 deletion docs/src/general/neighborhood_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ different implementations.

!!! note "Usage"
To run a simulation with a neighborhood search implementation, pass a neighborhood
search template to the constructor of the [`Semidiscretization`](@ref).
search template to the constructor of the
[`Semidiscretization`](@ref TrixiParticles.Semidiscretization).
A template is just an empty neighborhood search with search radius `0.0`.
See [`copy_neighborhood_search`](@ref) and the examples below for more details.
```jldoctest semi_example; output=false, setup = :(using TrixiParticles; trixi_include(@__MODULE__, joinpath(examples_dir(), "fluid", "hydrostatic_water_column_2d.jl"), sol=nothing); system1 = fluid_system; system2 = boundary_system)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/gpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ FullGridCellList{PointNeighbors.DynamicVectorOfVectors{...}(...)
```

We then need to pass this cell list to the neighborhood search and the neighborhood search
to the [`Semidiscretization`](@ref).
to the [`Semidiscretization`](@ref TrixiParticles.Semidiscretization).
```jldoctest gpu; output=false
semi = Semidiscretization(fluid_system, boundary_system,
neighborhood_search=GridNeighborhoodSearch{2}(; cell_list))
Expand Down
18 changes: 17 additions & 1 deletion docs/src/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

1. [Setting up your simulation from scratch](tutorials/tut_setup.md): learn the structure of a simulation file and run a complete WCSPH example.
2. [Modifying or extending components of TrixiParticles.jl within a simulation file](tutorials/tut_custom_kernel.md): replace selected parts of an existing setup without cloning the package.
3. [Particle packing tutorial](tutorials/tut_packing.md): build a body-fitted particle configuration for complex geometries.
3. [Setting up a 2D simulation from geometry files](tutorials/tut_2d_geometry.md): load 2D geometry files, create wall regions, and combine them with a rectangular fluid region.
4. [Particle packing tutorial](tutorials/tut_packing.md): build a body-fitted particle configuration for complex geometries.
5. [Fluid-structure interaction with rigid bodies](tutorials/tut_rigid_body_fsi.md): simulate objects moving in a fluid.

## Tutorials

Expand Down Expand Up @@ -38,6 +40,20 @@ directly in the file you run.
- Focus: `trixi_include`, custom kernels, rapid iteration
- Choose this if: you want to prototype changes without cloning and modifying the package

### [Setting up a 2D simulation from geometry files](tutorials/tut_2d_geometry.md)

```@raw html
<img src="../tutorials/tut_2d_geometry_plot.png"
alt="2D pipe and coastline geometries converted to wall and fluid particles"
style="max-width: 360px; width: 100%; border-radius: 12px;" />
```

Load 2D geometry files, fill them with particles using `ComplexShape`, and build
setups such as a curved pipe and a coastline dam break.

- Focus: `load_geometry`, `ComplexShape`, and set operations
- Choose this if: you want to build a 2D setup from geometry files

### [Particle packing tutorial](tutorials/tut_packing.md)

```@raw html
Expand Down
20 changes: 20 additions & 0 deletions examples/preprocessing/data/coastline_profile_2d.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ASCII
0.15 -0.12 0
2.68 -0.12 0
2.68 1.08 0
2.62 0.66 0
2.53 0.52 0
2.42 0.40 0
2.30 0.42 0
2.18 0.33 0
2.05 0.24 0
1.92 0.26 0
1.78 0.18 0
1.62 0.11 0
1.46 0.14 0
1.28 0.06 0
1.05 0.02 0
0.82 0.05 0
0.55 0.03 0
0.15 0.03 0
0.15 -0.12 0
20 changes: 20 additions & 0 deletions examples/preprocessing/data/curved_pipe_channel_2d.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ASCII
0.00 0.12 0
0.60 0.12 0
0.72423 0.13646 0
0.84000 0.18431 0
0.93941 0.26059 0
1.01569 0.36000 0
1.06354 0.47577 0
1.08 0.60 0
1.08 1.20 0
0.72 1.20 0
0.72 0.60 0
0.71591 0.56894 0
0.70392 0.54000 0
0.68485 0.51515 0
0.66000 0.49608 0
0.63106 0.48409 0
0.60 0.48 0
0.00 0.48 0
0.00 0.12 0
14 changes: 14 additions & 0 deletions examples/preprocessing/data/curved_pipe_outer_2d.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ASCII
0.00 0.00 0
0.60 0.00 0
0.75529 0.02044 0
0.90000 0.08038 0
1.02426 0.17574 0
1.11962 0.30000 0
1.17956 0.44471 0
1.20 0.60 0
1.20 1.20 0
0.60 1.20 0
0.60 0.60 0
0.00 0.60 0
0.00 0.00 0
7 changes: 4 additions & 3 deletions src/callbacks/density_reinit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ end

Callback to reinitialize the density field when using [`ContinuityDensity`](@ref) [Panizzo2007](@cite).

Pass `system` and the [`Semidiscretization`](@ref) containing it. The callback stores
the system index and uses the corresponding system from the integrator semidiscretization
at runtime, which remains valid if [`semidiscretize`](@ref) replaces systems internally.
Pass a `system` together with the [`Semidiscretization`](@ref TrixiParticles.Semidiscretization) that contains it.
The callback stores the system index and retrieves the corresponding system from the
integrator semidiscretization at runtime. This remains valid if [`semidiscretize`](@ref)
replaces systems internally.

# Keywords
- `interval=0`: Reinitialize the density every `interval` time steps.
Expand Down
2 changes: 1 addition & 1 deletion src/general/mechanical_work_calculator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The accumulated value can be retrieved via [`calculated_mechanical_work`](@ref).

# Arguments
- `system`: The [`TotalLagrangianSPHSystem`](@ref) whose particles should be monitored.
- `semi`: The [`Semidiscretization`](@ref) that contains `system`.
- `semi`: The [`Semidiscretization`](@ref TrixiParticles.Semidiscretization) that contains `system`.

# Keywords
- `eachparticle=(n_integrated_particles(system) + 1):nparticles(system)`: Iterator
Expand Down
17 changes: 10 additions & 7 deletions src/general/semidiscretization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ end
Create an `ODEProblem` from the semidiscretization with the specified `tspan`.

# Arguments
- `semi`: A [`Semidiscretization`](@ref) holding the systems involved in the simulation.
- `semi`: A [`Semidiscretization`](@ref TrixiParticles.Semidiscretization)
holding the systems involved in the simulation.
- `tspan`: The time span over which the simulation will be run.

# Keywords
Expand All @@ -263,11 +264,12 @@ Create an `ODEProblem` from the semidiscretization with the specified `tspan`.
[trixi-framework/Trixi.jl#1583](https://github.com/trixi-framework/Trixi.jl/issues/1583).
- `restart_with=nothing`: Restart the simulation from VTK solution files created by
[`SolutionSavingCallback`](@ref). This can be either `nothing` (default, no restart) or
a tuple of filenames, one for each system in the [`Semidiscretization`](@ref). The tuple
order must match the system order. When restarting, `semidiscretize` replaces the initial
time (`tspan[1]`) with the timestamp read from the VTK files. If the provided `tspan[1]`
does not match the restart time, it is adjusted and an info message is logged. Timestamps
in multiple files must match.
a tuple of filenames, one for each system in the
[`Semidiscretization`](@ref TrixiParticles.Semidiscretization). The tuple order must match
the system order. When restarting, `semidiscretize` replaces the initial time (`tspan[1]`)
with the timestamp read from the VTK files. If the provided `tspan[1]` does not match the
restart time, it is adjusted and an info message is logged. Timestamps in multiple files
must match.

# Returns
A `DynamicalODEProblem` (see [the OrdinaryDiffEq.jl docs](https://docs.sciml.ai/DiffEqDocs/stable/types/dynamical_types/))
Expand Down Expand Up @@ -420,7 +422,8 @@ Set the restartable state of all systems in `semi` to the final values in the so
`sol`. This includes coordinates and velocities as well as integrated state variables such
as density or pressure where applicable.
[`semidiscretize`](@ref) has to be called again afterwards, or another
[`Semidiscretization`](@ref) can be created with the updated systems.
[`Semidiscretization`](@ref TrixiParticles.Semidiscretization) can be created
with the updated systems.

# Arguments
- `semi`: The semidiscretization to update.
Expand Down
Loading