-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
101 lines (86 loc) · 3.75 KB
/
main.py
File metadata and controls
101 lines (86 loc) · 3.75 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import numpy as np
from functions import rockfall_physics_stable, plot_with_speed_and_energy
class RockfallGUI:
def __init__(self, root):
self.root = root
self.root.title("Rockfall Simulation")
self.root.geometry("350x400")
# 左侧控制区
control_frame = ttk.Frame(root, width=300)
control_frame.pack(fill='both', expand=True, padx=10, pady=10)
ttk.Label(
control_frame,
text="Rockfall Simulation Parameters",
font=('Arial', 12, 'bold')
).pack(pady=5)
# 参数变量
self.cellsize = tk.DoubleVar(value=30.0)
self.dt = tk.DoubleVar(value=0.1)
self.friction = tk.DoubleVar(value=0.15)
self.max_steps = tk.IntVar(value=5000)
self.start_row = tk.IntVar(value=357)
self.start_col = tk.IntVar(value=100)
self.g = tk.DoubleVar(value=9.81)
# 创建输入框
self.entries = {}
self._add_param(control_frame, "Cellsize (m):", self.cellsize)
self._add_param(control_frame, "Time Step (s):", self.dt)
self._add_param(control_frame, "Friction:", self.friction)
self._add_param(control_frame, "Max Steps:", self.max_steps)
self._add_param(control_frame, "Start Row:", self.start_row)
self._add_param(control_frame, "Start Col:", self.start_col)
self._add_param(control_frame, "Gravity:", self.g)
# 操作按钮
ttk.Button(control_frame, text="Load DEM", command=self.load_dem).pack(pady=10, fill='x')
ttk.Button(control_frame, text="Run Simulation", command=self.run_simulation).pack(pady=5, fill='x')
self.dem = None
def _add_param(self, parent, label, variable):
frame = ttk.Frame(parent)
frame.pack(fill='x', pady=3)
ttk.Label(frame, text=label, width=15).pack(side='left')
entry = ttk.Entry(frame, textvariable=variable)
entry.pack(side='right', fill='x', expand=True)
self.entries[label] = entry # 存起来备用
def load_dem(self):
filepath = filedialog.askopenfilename(
title="Select DEM File",
filetypes=[("ASCII DEM (*.asc)", "*.asc"), ("All Files", "*.*")]
)
if not filepath:
return
try:
self.dem = np.loadtxt(filepath, skiprows=6)
messagebox.showinfo("Success", f"DEM loaded: shape = {self.dem.shape}")
except Exception as e:
messagebox.showerror("Error", f"Failed to load DEM:\n{e}")
def run_simulation(self):
if self.dem is None:
messagebox.showwarning("No DEM", "Please load a DEM file first.")
return
try:
cellsize = self.cellsize.get()
dt = self.dt.get()
friction = self.friction.get()
max_steps = self.max_steps.get()
start_row = self.start_row.get()
start_col = self.start_col.get()
g = self.g.get()
except tk.TclError:
messagebox.showerror("Invalid Input", "Please check your parameter values.")
return
start = (start_row, start_col)
try:
path_idx, speed, energy = rockfall_physics_stable(
self.dem, start,
cellsize=cellsize, g=g, dt=dt,
friction=friction, max_steps=max_steps
)
plot_with_speed_and_energy(self.dem, path_idx, speed, energy, start)
except Exception as e:
messagebox.showerror("Error", f"Simulation failed:\n{e}")
if __name__ == "__main__":
root = tk.Tk()
app = RockfallGUI(root)
root.mainloop()