cwadayi commited on
Commit
80df50a
·
verified ·
1 Parent(s): f8967d1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -0
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import matplotlib.pyplot as plt
4
+
5
+ # --- 核心計算與繪圖函數 ---
6
+ def plot_seismic_refraction(v1, v2, h, x_max):
7
+ """
8
+ 根據輸入的地層參數,計算並繪製折射震測的走時曲線。
9
+ 圖表內的標籤已改為英文。
10
+ """
11
+ # 物理條件檢查:折射必須 V2 > V1
12
+ if v2 <= v1:
13
+ fig, ax = plt.subplots(figsize=(10, 6))
14
+ ax.text(0.5, 0.5, 'Error: V2 must be greater than V1 for critical refraction to occur.',
15
+ ha='center', va='center', fontsize=12, color='red')
16
+ ax.set_xlabel("Distance (m)")
17
+ ax.set_ylabel("Travel Time (s)")
18
+ ax.grid(True)
19
+ ax.set_title("Travel-Time Curve")
20
+ return fig, "### 參數錯誤\n請確保第二層速度 (V2) 大於第一層速度 (V1)。"
21
+
22
+ # 1. 計算關鍵物理量
23
+ theta_c_rad = np.arcsin(v1 / v2)
24
+ theta_c_deg = np.rad2deg(theta_c_rad)
25
+ ti = (2 * h * np.cos(theta_c_rad)) / v1
26
+ xc = 2 * h * np.sqrt((v2 + v1) / (v2 - v1))
27
+
28
+ # 2. 準備繪圖數據
29
+ x = np.linspace(0, x_max, 500)
30
+ t_direct = x / v1
31
+ t_refracted = (x / v2) + ti
32
+ t_first_arrival = np.minimum(t_direct, t_refracted)
33
+
34
+ # 3. 使用 Matplotlib 繪圖 (標籤已改為英文)
35
+ fig, ax = plt.subplots(figsize=(10, 6))
36
+
37
+ # --- 英文圖例標籤修改處 ---
38
+ ax.plot(x, t_direct, 'b--', label=f'Direct Wave (Slope 1/{v1:.0f})')
39
+ ax.plot(x, t_refracted, 'g--', label=f'Refracted Wave (Slope 1/{v2:.0f})')
40
+ ax.plot(x, t_first_arrival, 'r-', linewidth=3, label='First Arrival')
41
+
42
+ if xc < x_max:
43
+ ax.axvline(x=xc, color='k', linestyle=':', label=f'Crossover Distance = {xc:.1f} m')
44
+ ax.plot(xc, xc/v1, 'ko')
45
+
46
+ ax.plot(0, ti, 'mo', markersize=8, label=f'Intercept Time = {ti*1000:.1f} ms')
47
+
48
+ # --- 英文圖表/座標軸標題修改處 ---
49
+ ax.set_title("Interactive Seismic Refraction T-X Plot", fontsize=16)
50
+ ax.set_xlabel("Distance (m)", fontsize=12)
51
+ ax.set_ylabel("Travel Time (s)", fontsize=12)
52
+
53
+ ax.legend()
54
+ ax.grid(True)
55
+ ax.set_xlim(0, x_max)
56
+ ax.set_ylim(0, max(t_direct) * 1.1)
57
+ plt.tight_layout()
58
+
59
+ # 4. 準備輸出的說明文字 (這部分維持中文)
60
+ results_md = f"""
61
+ ### 🔬 分析結果
62
+
63
+ 根據您設定的參數,我們計算出以下關鍵物理量:
64
+
65
+ - **臨界角 (Critical Angle, θc)**:
66
+ - 公式: `θc = arcsin(V1 / V2)`
67
+ - 計算: `arcsin({v1:.0f} / {v2:.0f})` = **{theta_c_deg:.2f}°**
68
+ - *這是產生「抄捷徑」折射波的關鍵入射角度。*
69
+
70
+ - **截時 (Intercept Time, tᵢ)**:
71
+ - 公式: `tᵢ = (2 * h * cos(θc)) / V1`
72
+ - 計算: `(2 * {h:.0f} * cos({theta_c_deg:.2f}°)) / {v1:.0f}` = **{ti*1000:.1f} ms**
73
+ - *這是折射波走時線在時間軸上的截距,它隱含了第一層的厚度資訊。*
74
+
75
+ - **交越距離 (Crossover Distance, Xc)**:
76
+ - 公式: `Xc = 2 * h * sqrt((V2 + V1) / (V2 - V1))`
77
+ - 計算: `2 * {h:.0f} * sqrt(({v2:.0f} + {v1:.0f}) / ({v2:.0f} - {v1:.0f}))` = **{xc:.1f} m**
78
+ - *在這個距離上,直達波和折射波會同時抵達。超過這個距離後,折射波會先到。*
79
+ """
80
+
81
+ return fig, results_md
82
+
83
+ # --- Gradio 介面設定 (這部分維持中文) ---
84
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
85
+ gr.Markdown("# 地心震波奇幻之旅:互動折射震測實驗室 🌍")
86
+ gr.Markdown(
87
+ """
88
+ 歡迎來到地球物理小教室!這個互動工具是根據「地心震波奇幻之旅」課程內容設計的。
89
+ 您可以透過下方的滑桿來模擬一個簡單的雙層地層模型。調整第一層的速度 `V1`、第二層的速度 `V2` 以及第一層的厚度 `h`,
90
+ 觀察右側的「走時-距離圖」如何即時變化,並從中學習折射震測法的核心原理!
91
+ """
92
+ )
93
+
94
+ with gr.Row():
95
+ with gr.Column(scale=1):
96
+ gr.Markdown("### ⚙️ 調整地層參數")
97
+ v1_slider = gr.Slider(label="V1: 第一層速度 (m/s)", minimum=300, maximum=3000, value=800, step=50)
98
+ v2_slider = gr.Slider(label="V2: 第二層速度 (m/s)", minimum=500, maximum=6000, value=2500, step=50)
99
+ h_slider = gr.Slider(label="h: 第一層厚度 (m)", minimum=5, maximum=100, value=20, step=1)
100
+ xmax_slider = gr.Slider(label="最大觀測距離 (m)", minimum=100, maximum=500, value=250, step=10)
101
+
102
+ submit_btn = gr.Button("產生圖表", variant="primary")
103
+
104
+ with gr.Column(scale=2):
105
+ gr.Markdown("### 📊 走時-距離圖 (T-X Plot)")
106
+ plot_output = gr.Plot()
107
+
108
+ with gr.Row():
109
+ results_output = gr.Markdown("### 🔬 分析結果\n請調整參數並點擊按鈕以顯示計算結果。")
110
+
111
+ # --- 事件監聽 ---
112
+ submit_btn.click(
113
+ fn=plot_seismic_refraction,
114
+ inputs=[v1_slider, v2_slider, h_slider, xmax_slider],
115
+ outputs=[plot_output, results_output]
116
+ )
117
+
118
+ gr.Markdown(
119
+ """
120
+ ---
121
+ ### 學習重點
122
+ 1. **觀察斜率**: 直達波的斜率是 `1/V1`,折射波的斜率是 `1/V2`。試著調整 `V1` 和 `V2`,看看線條的陡峭程度如何變化?(速度越快,線越陡,斜率越小)。
123
+ 2. **觀察截時 (Intercept Time)**: 試著只增加厚度 `h`,看看 Y 軸上的截距有什麼變化?(厚度越厚,截時越大)。
124
+ 3. **觀察交越距離 (Crossover Distance)**: 試著增加 `h` 或減小 `V2` 與 `V1` 的速度差,看看交叉點如何向右移動?
125
+ 4. **必要條件**: 試著將 `V2` 調整到比 `V1` 小,看看會發生什麼事?你會發現,臨界折射的現象消失了!
126
+
127
+ <footer>
128
+ <p style="text-align:center; color:grey;">地球物理小教室 &copy; 2025 - 由 Gemini 根據課程文件生成</p>
129
+ </footer>
130
+ """
131
+ )
132
+
133
+ if __name__ == "__main__":
134
+ demo.launch()