Post

Physics simulation - fluid simulation


Download file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using Plots, LinearAlgebra, Printf

# -----------------------------------------------------------------------------
#                           SPH Particle Data Structure
# -----------------------------------------------------------------------------
mutable struct Particle
    position::Vector{Float64}
    velocity::Vector{Float64}
    acceleration::Vector{Float64}
    density::Float64
    pressure::Float64
    mass::Float64
    material::String
end

# -----------------------------------------------------------------------------
#                           Initialization
# -----------------------------------------------------------------------------
function initialize_particles()
    particles = Vector{Particle}(undef, num_particles)
    
    for i in 1:num_particles
        pos = [rand() * box_size, rand() * box_size, rand() * 0.5]  # Only bottom half
        particles[i] = Particle(
            pos,
            [0.0, 0.0, 0.0],    # velocity
            [0.0, 0.0, -10.0],  # acceleration
            1000.0,             # density
            0.0,                # pressure
            1.0,                # mass
            "water"             # material 
        )
    end
    
    return particles
end

# -----------------------------------------------------------------------------
#                       SPH Kernels
# -----------------------------------------------------------------------------
function kernel(r, h)
    q = r / h
    if q <= 1.0
        return (1.0 - 1.5*q*q + 0.75*q*q*q) / (π * h^3)
    elseif q <= 2.0
        return 0.25 * (2.0 - q)^3 / (π * h^3)
    else
        return 0.0
    end
end

# -----------------------------------------------------------------------------
#                       Density and Pressure Calculation
# -----------------------------------------------------------------------------
function calculate_density_pressure!(particles)

    for i in 1:length(particles)
        particles[i].density = 0.0
        
        # Calculate density
        for j in 1:length(particles)
            r_vec = particles[i].position - particles[j].position
            r = norm(r_vec)
            
            particles[i].density += particles[j].mass * kernel(r, smoothing_length)
        end
        
        # Calculate pressure 
        particles[i].pressure = stiff_coef * ((particles[i].density/target_density)^7 - 1)
    end
end


# -----------------------------------------------------------------------------
#                       Force Calculation 
# -----------------------------------------------------------------------------
function calculate_forces!(particles)
    
    for i in 1:length(particles)
        grad_pressure = zeros(3)
        laplacian_velocity = zeros(3)
        
        # Calculate pressure gradient and velocity Laplacian
        for j in 1:length(particles)
            if i == j
                continue
            end
            
            r_vec = particles[i].position - particles[j].position
            r = norm(r_vec)
            
            if r > smoothing_length || r == 0
                continue
            end
            
            # Kernel gradient calculation
            q = r / smoothing_length
            kernel_grad = zeros(3)
            if q <= 1.0
                factor = (-3.0 + 2.25*q) / (π * smoothing_length^5)
                kernel_grad = factor * r_vec
            elseif q <= 2.0
                factor = -0.75 * (2.0 - q)^2 / (π * smoothing_length^5 * q)
                kernel_grad = factor * r_vec
            end
            
            # Pressure gradient (Equation 6)
            pressure_term = (particles[i].pressure / (particles[i].density^2) + 
                           particles[j].pressure / (particles[j].density^2))
            grad_pressure += particles[j].mass * pressure_term * kernel_grad
            
            # Velocity Laplacian (Equation 8)
            v_ij = particles[i].velocity - particles[j].velocity
            dot_r_grad = dot(r_vec, kernel_grad)
            denominator = dot(r_vec, r_vec) + 0.01 * smoothing_length^2
            
            if denominator != 0
                laplacian_velocity += 2.0 * (particles[j].mass / particles[j].density) * 
                                    v_ij * (dot_r_grad / denominator)
            end
        end
        
        # Pressure force (-∇P/ρ)
        Fi_pressure = -grad_pressure 
        
        # Viscosity force (ν∇²v)
        Fi_viscosity = particles[i].mass * viscosity_coef * laplacian_velocity
        
        # Gravity 
        Fi_gravity = particles[i].mass * [0.0, 0.0, -10.0]
        
        # Total forces
        Fi = Fi_pressure + Fi_viscosity + Fi_gravity
        
        # Update velocity and position
        particles[i].velocity .+= (Fi / particles[i].mass) .* dt
        particles[i].position .+= particles[i].velocity .* dt

        @printf "Particle %d: Pos=(%.3f, %.3f, %.3f) Vel=(%.3f, %.3f, %.3f) Density=%.2f Pressure=%.2f Fi_pressure=(%.3f, %.3f, %.3f) Fi_viscosity=(%.3f, %.3f, %.3f)\n" i particles[i].position[1] particles[i].position[2] particles[i].position[3] particles[i].velocity[1] particles[i].velocity[2] particles[i].velocity[3] particles[i].density particles[i].pressure Fi_pressure[1] Fi_pressure[2] Fi_pressure[3] Fi_viscosity[1] Fi_viscosity[2] Fi_viscosity[3]
    end
end

# -----------------------------------------------------------------------------
#                       Boundary Conditions
# -----------------------------------------------------------------------------
function apply_boundary_conditions!(particles)
    
    for p in particles
        # X boundaries
        if p.position[1] < 0.0
            p.position[1] = 0.0
            p.velocity .*= -damping  
        elseif p.position[1] > box_size
            p.position[1] = box_size
            p.velocity .*= -damping  
        end
        
        # Y boundaries
        if p.position[2] < 0.0
            p.position[2] = 0.0
            p.velocity .*= -damping  
        elseif p.position[2] > box_size
            p.position[2] = box_size
            p.velocity .*= -damping  
        end
        
        # Z boundaries 
        if p.position[3] < 0.0
            p.position[3] = 0.0
            p.velocity .*= -damping  
        elseif p.position[3] > box_size
            p.position[3] = box_size
            p.velocity .*= -damping  
        end
    end
end


# -----------------------------------------------------------------------------
#                       Main Simulation Step
# -----------------------------------------------------------------------------
function simulate_step!(particles)
    calculate_density_pressure!(particles)
    calculate_forces!(particles)
    apply_boundary_conditions!(particles)
end

# -----------------------------------------------------------------------------
#                       Visualization
# -----------------------------------------------------------------------------
function visualize_sph(particles, step)
    x = [p.position[1] for p in particles]
    y = [p.position[2] for p in particles]
    z = [p.position[3] for p in particles]
    
    plt = scatter3d(x, y, z,
            markersize=3,
            markercolor=:blue,
            xlim=(0, box_size),
            ylim=(0, box_size),
            zlim=(0, box_size),
            title="Time $(round(step, digits=2))s",
            xlabel="X", ylabel="Y", zlabel="Z",
            legend=false,
            camera=(30, 60),
            size=(400, 500)
    )
    return plt
end

# -----------------------------------------------------------------------------
#                           SPH Parameters
# -----------------------------------------------------------------------------
const num_particles = 1000
const dt = 0.01
const box_size = 1.0
const damping = 0.8

const smoothing_length = 0.1
const stiff_coef = 100.0
const target_density = 1000.0
const viscosity_coef = 0.2

# -----------------------------------------------------------------------------
#                       Main Simulation
# -----------------------------------------------------------------------------
function main()
    particles = initialize_particles()
    
    t = 0.0
    frame_count = 0
    
    while t < 10.0
        simulate_step!(particles)
        
        if frame_count % 1 == 0  # Save every frame
            plt = visualize_sph(particles, t)
            display(plt)
        end
        
        t += dt
        frame_count += 1
    end
end

main()
This post is licensed under CC BY 4.0 by the author.