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
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
|
| 5 |
+
# --- 核心計算與繪圖函數 (維持不變) ---
|
| 6 |
+
def plot_travel_times(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 |
+
# 即使沒有折射,我們仍然可以顯示直達波和反射波
|
| 15 |
+
x = np.linspace(0, x_max, 500)
|
| 16 |
+
t_direct = x / v1
|
| 17 |
+
t_reflected = np.sqrt(x**2 + (2 * h)**2) / v1
|
| 18 |
+
|
| 19 |
+
ax.plot(x, t_direct, 'b--', label='Direct Wave')
|
| 20 |
+
ax.plot(x, t_reflected, 'm-.', label='Reflected Wave')
|
| 21 |
+
|
| 22 |
+
ax.text(0.5, 0.5, 'V2 <= V1: Critical refraction does not occur.\nOnly Direct and Reflected waves are shown.',
|
| 23 |
+
ha='center', va='center', fontsize=12, color='orange')
|
| 24 |
+
ax.set_xlabel("Distance (m)")
|
| 25 |
+
ax.set_ylabel("Travel Time (s)")
|
| 26 |
+
ax.grid(True)
|
| 27 |
+
ax.legend()
|
| 28 |
+
ax.set_title("Travel-Time Curve (No Refraction)")
|
| 29 |
+
results_md = "### ⚠️ 捷運停駛!\n高速層 V2 的速度必須比 V1 快,聰明的折射波選手才會登場喔!"
|
| 30 |
+
return fig, results_md
|
| 31 |
+
|
| 32 |
+
# 1. 計算關鍵物理量
|
| 33 |
+
theta_c_rad = np.arcsin(v1 / v2)
|
| 34 |
+
theta_c_deg = np.rad2deg(theta_c_rad)
|
| 35 |
+
ti = (2 * h * np.cos(theta_c_rad)) / v1
|
| 36 |
+
xc = 2 * h * np.sqrt((v2 + v1) / (v2 - v1))
|
| 37 |
+
xcrit = 2 * h * np.tan(theta_c_rad)
|
| 38 |
+
|
| 39 |
+
# 2. 準備繪圖數據
|
| 40 |
+
x = np.linspace(0, x_max, 500)
|
| 41 |
+
t_direct = x / v1
|
| 42 |
+
t_refracted = (x / v2) + ti
|
| 43 |
+
t_reflected = np.sqrt(x**2 + (2 * h)**2) / v1
|
| 44 |
+
t_first_arrival = np.minimum(t_direct, t_refracted)
|
| 45 |
+
|
| 46 |
+
# 3. 使用 Matplotlib 繪圖
|
| 47 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 48 |
+
|
| 49 |
+
ax.plot(x, t_direct, 'b--', label=f'Direct Wave (Sprinter)')
|
| 50 |
+
ax.plot(x, t_refracted, 'g--', label=f'Refracted Wave (Subway Rider)')
|
| 51 |
+
ax.plot(x, t_reflected, 'm-.', label='Reflected Wave (Bouncy Ball)')
|
| 52 |
+
ax.plot(x, t_first_arrival, 'r-', linewidth=3, label='WINNER (First Arrival)')
|
| 53 |
+
|
| 54 |
+
if xc < x_max:
|
| 55 |
+
ax.axvline(x=xc, color='k', linestyle=':', label=f'Crossover Point = {xc:.1f} m')
|
| 56 |
+
ax.plot(xc, xc/v1, 'ko')
|
| 57 |
+
|
| 58 |
+
if xcrit < x_max:
|
| 59 |
+
tcrit = (xcrit / v2) + ti
|
| 60 |
+
ax.axvline(x=xcrit, color='c', linestyle=':', label=f'Critical Point = {xcrit:.1f} m')
|
| 61 |
+
ax.plot(xcrit, tcrit, 'co', markersize=8)
|
| 62 |
+
|
| 63 |
+
ax.plot(0, ti, 'mo', markersize=8, label=f'Intercept Time = {ti*1000:.1f} ms')
|
| 64 |
+
|
| 65 |
+
ax.set_title("The Great Seismic Race: T-X Plot", fontsize=16)
|
| 66 |
+
ax.set_xlabel("Race Distance (m)", fontsize=12)
|
| 67 |
+
ax.set_ylabel("Travel Time (s)", fontsize=12)
|
| 68 |
+
|
| 69 |
+
ax.legend()
|
| 70 |
+
ax.grid(True)
|
| 71 |
+
ax.set_xlim(0, x_max)
|
| 72 |
+
max_time = np.max([t_direct[-1], t_refracted[-1], t_reflected[-1]])
|
| 73 |
+
ax.set_ylim(0, max_time * 1.1)
|
| 74 |
+
plt.tight_layout()
|
| 75 |
+
|
| 76 |
+
# 4. 準備輸出的說明文字 (生動有趣版)
|
| 77 |
+
results_md = f"""
|
| 78 |
+
### 📣 賽況播報:認識你的選手!
|
| 79 |
+
|
| 80 |
+
一場精彩的地底競速正在上演!三位選手的表現完全取決於您設計的賽道:
|
| 81 |
+
|
| 82 |
+
- 🏃♂️ **直達波 (The Sprinter)**:
|
| 83 |
+
- **策略**: 硬派跑法!只沿著地表(速度V1)直線衝刺。
|
| 84 |
+
- **特性**: 它是最老實的選手,在短距離賽中通常保持領先。但隨著賽道變長,它可能會被下方的捷運選手超越。
|
| 85 |
+
|
| 86 |
+
- bouncing ball
|
| 87 |
+
- **策略**: 垂直彈跳!它會潛入地底,在第一個地層邊界(深度h)觸地反彈,然後返回地表。
|
| 88 |
+
- **特性**: 它的路徑像個 V 字形,因為需要向下再向上,所以它永遠無法贏得比賽(成為初達波),但它忠實地回報了地層的深度資訊!
|
| 89 |
+
|
| 90 |
+
- 🚄 **折射波 (The Subway Rider)**:
|
| 91 |
+
- **策略**: 抄捷徑!這位是「機會主義者」。它知道地底下有一條速度為 `V2` 的「高速捷運」。
|
| 92 |
+
- **臨界點 (Critical Point)**: 在 **{xcrit:.1f} m** 處,是它能進入高速捷運的最早入口。它必須以一個完美的**臨界角 ({theta_c_deg:.2f}°)** 切入才能搭上車。
|
| 93 |
+
- **截時 (Intercept Time)**: 搭捷運總要花時間下到月台吧?這 **{ti*1000:.1f} ms** 就是它為了走捷徑付出的「前期時間成本」。這個時間也巧妙地告訴我們月台有多深(`h`)!
|
| 94 |
+
|
| 95 |
+
---
|
| 96 |
+
### 🏁 勝負的交叉點:交越距離 (Crossover Distance)
|
| 97 |
+
|
| 98 |
+
在 **{xc:.1f} m** 這個神奇的位置,短跑選手被捷運選手追上了!
|
| 99 |
+
- **在此之前**: 直達波(短跑選手)最快。
|
| 100 |
+
- **在此之後**: 折射波(捷運選手)才是永遠的贏家!
|
| 101 |
+
這個交叉點是我們解開地層秘密最重要的線索之一。
|
| 102 |
+
"""
|
| 103 |
+
return fig, results_md
|
| 104 |
+
|
| 105 |
+
# --- Gradio 介面設定 (生動有趣版) ---
|
| 106 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 107 |
+
gr.Markdown("# 地心震波奇幻之旅:地底競速大賽 🌍🏆")
|
| 108 |
+
gr.Markdown(
|
| 109 |
+
"""
|
| 110 |
+
**準備好了嗎,地球探險家?** 你現在正操控著一台虛擬震測儀,就像是地球的「超音波」掃描機。我們的任務是透過發射震波,並聆聽它們的回音,來揭開地底深處的秘密。
|
| 111 |
+
|
| 112 |
+
你將扮演三位賽跑選手——**直達波**、**反射波**與**折射波**——的總教練。調整下方的賽道參數,看看誰會在這場地底競速中脫穎而出!
|
| 113 |
+
"""
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
with gr.Row():
|
| 117 |
+
with gr.Column(scale=1):
|
| 118 |
+
gr.Markdown("### 🗺️ 設計你的賽道")
|
| 119 |
+
v1_slider = gr.Slider(label="V1: 地表跑道速度 (m/s)", minimum=300, maximum=3000, value=800, step=50)
|
| 120 |
+
v2_slider = gr.Slider(label="V2: 地底捷運速度 (m/s)", minimum=500, maximum=6000, value=2500, step=50)
|
| 121 |
+
h_slider = gr.Slider(label="h: 捷運月台深度 (m)", minimum=5, maximum=100, value=20, step=1)
|
| 122 |
+
xmax_slider = gr.Slider(label="賽道總長度 (m)", minimum=100, maximum=500, value=250, step=10)
|
| 123 |
+
|
| 124 |
+
submit_btn = gr.Button("開賽!", variant="primary")
|
| 125 |
+
|
| 126 |
+
with gr.Column(scale=2):
|
| 127 |
+
gr.Markdown("### 📊 即時賽況 (T-X Plot)")
|
| 128 |
+
plot_output = gr.Plot()
|
| 129 |
+
|
| 130 |
+
with gr.Row():
|
| 131 |
+
results_output = gr.Markdown("### 🎙️ 賽事分析\n請設計好賽道並點擊「開賽!」按鈕,查看專業分析。")
|
| 132 |
+
|
| 133 |
+
submit_btn.click(
|
| 134 |
+
fn=plot_travel_times,
|
| 135 |
+
inputs=[v1_slider, v2_slider, h_slider, xmax_slider],
|
| 136 |
+
outputs=[plot_output, results_output]
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
gr.Markdown(
|
| 140 |
+
"""
|
| 141 |
+
---
|
| 142 |
+
### 🚀 探險家任務
|
| 143 |
+
試試看,你能解開這些謎題嗎?
|
| 144 |
+
|
| 145 |
+
1. **斜率的秘密**: 試著把 `V1` 或 `V2` 調快,看看發生了什麼?賽道變平坦了!沒錯,**速度越快,跑完相同距離的時間就越短,所以線條(斜率)就越平緩。**
|
| 146 |
+
2. **「截時」的詛咒**: 如果你把地層 `h` 加厚,就像把捷運月台蓋得更深。觀察折射波的綠線,是不是整條都往上平移了?這就是增加的「通勤時間」!
|
| 147 |
+
3. **拉開差距**: 怎麼讓短跑選手領先久一點?你可以把 `h` 加厚(起跑線拉遠),或是讓 `V1` 和 `V2` 速度更接近(捷運沒快多少),看看那個黑色的交叉點是不是就往右邊跑了?
|
| 148 |
+
4. **尋找捷運入口**: 觀察青色的「臨界點」。它永遠落在彈跳球選手(反射波)的路線上。這是物理定律給我們的一個有趣彩蛋!
|
| 149 |
+
5. **取消捷運**: 如果你讓地底捷運的速度 `V2` 比地表跑道 `V1` 還要慢呢?你會發現捷運選手直接罷工不出現了!因為這樣抄捷徑根本不划算。
|
| 150 |
+
|
| 151 |
+
<footer>
|
| 152 |
+
<p style="text-align:center; color:grey;">地球物理小教室 © 2025 - 由 Gemini 根據課程文件生成</p>
|
| 153 |
+
</footer>
|
| 154 |
+
"""
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
if __name__ == "__main__":
|
| 158 |
+
demo.launch()
|