Skip to content
Open
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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
Expand All @@ -31,6 +32,7 @@ InteractiveUtils = "1.11.0"
LibGit2 = "1.11.0"
LoggingExtras = "^1.2.0"
Parameters = "^0.12.3"
PrecompileTools = "1.3.3"
StaticArrays = "^1.9.15"
StructArrays = "^0.7.2"
TimerOutputs = "^0.5.29"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ using Pkg
Pkg.add(url="https://github.com/AhmedSalih3d/SPHExample")
```

The package uses `PrecompileTools` to precompile representative workloads on
first load, improving time-to-first-simulation across platforms. The initial
`using SPHExample` may take a little longer while the precompile cache is
generated, but subsequent loads will be noticeably faster.

### Running an Example

Open one of the files in `example/`, for instance `example/StillWedgeMDBC.jl`,
Expand Down
82 changes: 81 additions & 1 deletion src/SPHExample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module SPHExample
using .ProduceHDFVTK
export SaveVTKHDF, GenerateGeometryStructure, GenerateStepStructure, AppendVTKHDFData, SaveCellGridVTKHDF, AppendVTKHDFGridData, SetupVTKOutput

using .TimeStepping: Δt
using .TimeStepping: Δt, FinalizeTimeStep, UpdateTimeStepBuffers!, HalfTimeStep, FullTimeStep
export Δt

using .SimulationEquations
Expand All @@ -65,4 +65,84 @@ module SPHExample
using .OpenExternalPrograms
export AutoOpenLogFile, AutoOpenParaview

using PrecompileTools
using LinearAlgebra
using StaticArrays
using StructArrays

@setup_workload begin
KernelInstance = SPHKernelInstance{2, Float64}(WendlandC2(); dx=0.02)
CubicKernelInstance = SPHKernelInstance{2, Float64}(CubicSpline{Float64}(); dx=0.02)
Constants = SimulationConstants(ρ₀ = 1000.0, dx = 0.02, γ = 7.0)
Position = [SVector(0.0, 0.0), SVector(0.02, 0.0), SVector(0.04, 0.0)]
Velocity = [SVector(0.0, 0.0), SVector(0.1, 0.0), SVector(-0.05, 0.0)]
Acceleration = [SVector(0.0, 0.0), SVector(0.0, -9.81), SVector(0.0, -9.81)]
Density = fill(Constants.ρ₀, 3)
Pressure = zeros(Float64, 3)
GravityFactor = [-1.0, 1.0, -1.0]
MotionLimiter = [1.0, 0.0, 1.0]
Types = [Fluid, Moving, Fluid]
GroupMarker = [UInt(1), UInt(1), UInt(2)]
Cells = fill(CartesianIndex{2}(0, 0), 3)
Kernel = zeros(Float64, 3)
KernelGradient = zeros(eltype(Position), 3)
GhostPoints = zeros(eltype(Position), 3)
GhostNormals = zeros(eltype(Position), 3)
SimParticles = StructArray((;
Cells = Cells,
Position = Position,
Acceleration = Acceleration,
Velocity = Velocity,
Density = Density,
Pressure = Pressure,
GravityFactor = GravityFactor,
MotionLimiter = MotionLimiter,
Type = Types,
GroupMarker = GroupMarker,
Kernel = Kernel,
KernelGradient = KernelGradient,
GhostPoints = GhostPoints,
GhostNormals = GhostNormals,
))
Xij = Position[1] - Position[2]
Vij = Velocity[1] - Velocity[2]
DistanceSquared = dot(Xij, Xij)
Q = sqrt(DistanceSquared) * KernelInstance.h⁻¹
GradientWij = ∇Wᵢⱼ(KernelInstance, Q, Xij)
CubicGradientWij = ∇Wᵢⱼ(CubicKernelInstance, Q, Xij)
SimMetaData = SimulationMetaData{2, Float64, NoShifting, StoreKernelOutput, SimpleMDBC, NoLog}(SimulationName = "Precompile", SaveLocation = ".")
DensityRate, VelocityNext, PositionNext, DensityNext, ShiftGradient, ShiftDivergence = AllocateSupportDataStructures(SimMetaData, Position)
HalfStep = 0.5
FullStep = 0.01
MaxVisc = zeros(Float64, 3)
MinDtForce = fill(Inf, 3)

@compile_workload begin
Wᵢⱼ(KernelInstance, Q)
∇Wᵢⱼ(KernelInstance, Q, Xij)
tensile_correction(KernelInstance, 0.0, 1.0, 0.0, 1.0, Q, 0.02)
Wᵢⱼ(CubicKernelInstance, Q)
∇Wᵢⱼ(CubicKernelInstance, Q, Xij)
tensile_correction(CubicKernelInstance, 0.0, 1.0, 0.0, 1.0, Q, 0.02)
EquationOfStateGamma7(Constants.ρ₀, Constants.c₀, Constants.ρ₀)
ConstructGravitySVector(Xij, Constants.g)
Pressure!(Pressure, Density, Constants)
LimitDensityAtBoundary!(Density, Constants.ρ₀, MotionLimiter)
compute_viscosity(ZeroViscosity(), KernelInstance, Constants, SimParticles, Xij, Vij, GradientWij, DistanceSquared, 1, 2)
compute_viscosity(ArtificialViscosity(), KernelInstance, Constants, SimParticles, Xij, Vij, GradientWij, DistanceSquared, 1, 2)
compute_viscosity(Laminar(), KernelInstance, Constants, SimParticles, Xij, Vij, GradientWij, DistanceSquared, 1, 2)
compute_viscosity(LaminarSPS(), KernelInstance, Constants, SimParticles, Xij, Vij, GradientWij, DistanceSquared, 1, 2)
compute_density_diffusion(ZeroDensityDiffusion(), KernelInstance, Constants, SimParticles, Xij, GradientWij, DistanceSquared, 1, 2, MotionLimiter)
compute_density_diffusion(LinearDensityDiffusion(), KernelInstance, Constants, SimParticles, Xij, GradientWij, DistanceSquared, 1, 2, MotionLimiter)
compute_density_diffusion(ComplexDensityDiffusion(), KernelInstance, Constants, SimParticles, Xij, GradientWij, DistanceSquared, 1, 2, MotionLimiter)
compute_density_diffusion(LinearDensityDiffusion(), CubicKernelInstance, Constants, SimParticles, Xij, CubicGradientWij, DistanceSquared, 1, 2, MotionLimiter)
HalfTimeStep(SimMetaData, Constants, SimParticles, PositionNext, VelocityNext, DensityNext, DensityRate, HalfStep)
FullTimeStep(SimMetaData, KernelInstance, Constants, SimParticles, ShiftGradient, ShiftDivergence, FullStep)
UpdateTimeStepBuffers!(MaxVisc, MinDtForce, 1, Position[1], Velocity[1], Acceleration[1], KernelInstance)
UpdateTimeStepBuffers!(MaxVisc, MinDtForce, 2, Position[2], Velocity[2], Acceleration[2], KernelInstance)
FinalizeTimeStep(MaxVisc, MinDtForce, Constants, KernelInstance)
Δt(Position, Velocity, Acceleration, Constants, KernelInstance)
end
end

end