-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwave_simulation.py
More file actions
77 lines (52 loc) · 1.7 KB
/
Copy pathwave_simulation.py
File metadata and controls
77 lines (52 loc) · 1.7 KB
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
import numpy as np # type: ignore
import matplotlib.pyplot as plt # type: ignore
from matplotlib import cm # type: ignore
from mpl_toolkits.mplot3d import Axes3D # type: ignore
from clawpack import pyclaw # type: ignore
from clawpack import riemann # type: ignore
def initial_condition(state):
x, y = state.grid.p_centers
r2 = (x-0.5)**2 + (y-0.5)**2
state.q[0,:,:] = 1 + 0.5*np.exp(-100*r2)
state.q[1,:,:] = 0
state.q[2,:,:] = 0
solver = pyclaw.ClawSolver2D(riemann.shallow_roe_with_efix_2D)
solver.limiters = pyclaw.limiters.tvd.MC
solver.bc_lower[0] = pyclaw.BC.extrap
solver.bc_upper[0] = pyclaw.BC.extrap
solver.bc_lower[1] = pyclaw.BC.extrap
solver.bc_upper[1] = pyclaw.BC.extrap
x = pyclaw.Dimension(0.0,1.0,100,name='x')
y = pyclaw.Dimension(0.0,1.0,100,name='y')
domain = pyclaw.Domain([x,y])
state = pyclaw.State(domain,3)
state.problem_data['grav'] = 1.0
initial_condition(state)
claw = pyclaw.Controller()
claw.solution = pyclaw.Solution(state,domain)
claw.solver = solver
claw.tfinal = 0.5
claw.num_output_times = 20
claw.keep_copy = True
claw.run()
# Prepare grid for 3D plotting
X, Y = state.grid.p_centers
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for frame in claw.frames:
ax.clear()
h = frame.state.q[0]
surf = ax.plot_surface(
X, Y, h,
cmap=cm.viridis,
linewidth=0,
antialiased=True
)
ax.view_init(elev=35, azim=frame.t*50) # Rotate the view over time
ax.set_zlim(0.95,1.1) # Set consistent z-axis limits for better visualization
ax.set_title(f"Wave Simulation — Time {frame.t:.2f}")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Water Height")
plt.pause(0.1)
plt.show()