147 lines
7.3 KiB
Makefile
147 lines
7.3 KiB
Makefile
# =============================================================================
|
||
# Makefile for TVS/TRIAC DEVSIM Simulation Pipeline
|
||
# =============================================================================
|
||
|
||
# Limit threads for parallel solvers to avoid starving the WSL VM resources
|
||
export OMP_NUM_THREADS = 4
|
||
export MKL_NUM_THREADS = 4
|
||
export TBB_NUM_THREADS = 4
|
||
export OPENBLAS_NUM_THREADS = 4
|
||
|
||
# Default simulation control options
|
||
avalanche ?= false
|
||
refine ?= true
|
||
refine_v_step ?= 50.0
|
||
|
||
PYTHON := .venv/bin/python
|
||
|
||
.PHONY: help help-detail clean mesh static sweep resume resume-bg show-conv monitor backup-run
|
||
|
||
help:
|
||
@echo "============================================================================="
|
||
@echo "TVS/TRIAC Simulation Pipeline - Developer Operations & Maintenance"
|
||
@echo "============================================================================="
|
||
@echo "Core Workflow:"
|
||
@echo " 1. make mesh - Build base grid device_2d.msh under 0V built-in field"
|
||
@echo " 2. make static - Verify equilibrium Poisson/doping convergence at 0V"
|
||
@echo " 3. make sweep - Run high-voltage bias sweep (avalanche & refinement controls)"
|
||
@echo ""
|
||
@echo "High-Level Operations:"
|
||
@echo " make static - Solve equilibrium 0V Poisson state"
|
||
@echo " make sweep - Execute bias sweep simulation"
|
||
@echo " make resume - Resume from latest or specific checkpoint"
|
||
@echo " make resume-bg - Resume simulation in the background (nohup)"
|
||
@echo ""
|
||
@echo "Maintenance & Diagnostics:"
|
||
@echo " make mesh - (Re)generate adaptive base mesh"
|
||
@echo " make clean - Clear temporary files and simulation logs"
|
||
@echo " make monitor - Live-monitor convergence log of active sweep"
|
||
@echo " make show-conv - Print last few convergence step error details"
|
||
@echo ""
|
||
@echo "Output & Backup Rules:"
|
||
@echo " output_this_run/ - Current logs, checkpoints & visualization plots"
|
||
@echo " output_last_run/ - Previous run archive (triggered by new make mesh or sweep)"
|
||
@echo " * Note: Rebuilding mesh via make mesh auto-archives output_this_run to prevent"
|
||
@echo " loading old checkpoints on the updated grid structure."
|
||
@echo ""
|
||
@echo "For detail variables (e.g., avalanche=true, refine=false, checkpoint=xxx), run:"
|
||
@echo " make help-detail"
|
||
@echo "============================================================================="
|
||
|
||
help-detail:
|
||
@echo "============================================================================="
|
||
@echo "Detailed Command Parameters and Variables:"
|
||
@echo "============================================================================="
|
||
@echo "Command Variables:"
|
||
@echo " avalanche=true|false - Toggle impact ionization (avalanche) model"
|
||
@echo " Default: false (normal sweep without avalanche)"
|
||
@echo " refine=true|false - Toggle dynamic adaptive refinement during sweep"
|
||
@echo " Default: true (enable grid splitting at milestones)"
|
||
@echo " refine_v_step=<voltage> - Set voltage interval (V) to trigger dynamic refinement"
|
||
@echo " Default: 50.0 (e.g., every 50V). Less than 1.0 disables it."
|
||
@echo " checkpoint=<filepath> - Specify seed/recovery pickle file to resume from"
|
||
@echo " Default: automatically searches for latest checkpoints"
|
||
@echo ""
|
||
@echo "Usage Examples:"
|
||
@echo " make sweep avalanche=true"
|
||
@echo " make sweep refine=false"
|
||
@echo " make sweep refine_v_step=25.0"
|
||
@echo " make resume checkpoint=output_this_run/seed_500V.pkl"
|
||
@echo " make resume-bg avalanche=true refine_v_step=30.0"
|
||
@echo "============================================================================="
|
||
|
||
# --- 歷史運行歸檔備份 ---
|
||
# 當 output_this_run 內有模擬日誌(表示有前一次運行的結果)時,才執行歸檔,防止以空目錄覆寫 backup
|
||
backup-run:
|
||
@if [ -f output_this_run/sweeping.log ] || [ -f output_this_run/simulation_time.log ]; then \
|
||
echo ">>> [Backup] 備份上一次的日誌與輸出檔案..."; \
|
||
rm -rf output_last_run; \
|
||
mv output_this_run output_last_run || true; \
|
||
fi
|
||
@mkdir -p output_this_run
|
||
|
||
# --- 網格自適應優化流程 ---
|
||
# 1. 刪除舊的 bgmesh,以確保 generate_mesh_2d.py 產生的是最乾淨之基礎網格
|
||
# 2. 執行基礎網格生成
|
||
# 3. 執行 run_refinement_2d.py 讀取基礎網格,求解電場並寫出新的 device_bgmesh.pos
|
||
# 4. 再次執行 generate_mesh_2d.py,此時會自動載入 bgmesh 並輸出最終優化網格 device_2d.msh
|
||
mesh: device_config.py generate_mesh_2d.py generate_analytical_bgmesh.py
|
||
$(MAKE) backup-run
|
||
@echo ">>> [Mesh] 開始進行自適應網格重構流程..."
|
||
rm -f device_bgmesh.pos
|
||
$(PYTHON) generate_mesh_2d.py
|
||
$(PYTHON) generate_analytical_bgmesh.py
|
||
$(PYTHON) generate_mesh_2d.py
|
||
@echo ">>> [Mesh] 自適應優化網格生成完畢!(Saved: device_2d.msh)"
|
||
@cp device_config.py output_this_run/device_config.py
|
||
@cp device_2d.msh output_this_run/device_2d.msh
|
||
@-[ -f device_bgmesh.pos ] && cp device_bgmesh.pos output_this_run/device_bgmesh.pos || true
|
||
|
||
# --- 熱平衡電位求解 ---
|
||
# 依賴於對應的網格與求解腳本
|
||
static: device_2d.msh solve_static_2d.py
|
||
@echo ">>> [Static] 求解零偏壓熱平衡狀態..."
|
||
$(PYTHON) solve_static_2d.py
|
||
|
||
# --- 高壓偏壓掃描 ---
|
||
# 依賴於對應的網格與掃描腳本
|
||
sweep: device_2d.msh solve_sweep_recon.py
|
||
$(MAKE) backup-run
|
||
@echo ">>> [Sweep] 開始高壓偏壓漂移-擴散模擬 (avalanche=$(avalanche), refine=$(refine), refine_v_step=$(refine_v_step))..."
|
||
AVALANCHE=$(avalanche) REFINE=$(refine) REFINE_V_STEP=$(refine_v_step) $(PYTHON) solve_sweep_recon.py > output_this_run/sweeping.log 2>&1
|
||
|
||
resume:
|
||
@echo ">>> [Resume] 從指定的 Checkpoint ($(checkpoint)) 或最新的自動備份接續掃描 (avalanche=$(avalanche), refine=$(refine), refine_v_step=$(refine_v_step))..."
|
||
@mkdir -p output_this_run
|
||
AVALANCHE=$(avalanche) REFINE=$(refine) REFINE_V_STEP=$(refine_v_step) $(PYTHON) resume_run.py $(checkpoint) >> output_this_run/sweeping.log 2>&1
|
||
|
||
resume-bg:
|
||
@echo ">>> [Resume-BG] 在背景從指定的 Checkpoint ($(checkpoint)) 或最新的自動備份接續掃描 (avalanche=$(avalanche), refine=$(refine), refine_v_step=$(refine_v_step))..."
|
||
@mkdir -p output_this_run
|
||
AVALANCHE=$(avalanche) REFINE=$(refine) REFINE_V_STEP=$(refine_v_step) nohup $(PYTHON) resume_run.py $(checkpoint) >> output_this_run/sweeping.log 2>&1 &
|
||
|
||
# --- 萃取與監控收斂曲線 ---
|
||
show-conv:
|
||
@if [ -f output_this_run/sweeping.log ]; then \
|
||
awk '/Iteration:/ {printf "Iteration %s:", $$2} /Device:/ {print $$4}' output_this_run/sweeping.log | tail -n 10; \
|
||
else \
|
||
echo "sweeping.log does not exist."; \
|
||
fi
|
||
|
||
monitor:
|
||
@if [ -f output_this_run/sweeping.log ]; then \
|
||
tail -f output_this_run/sweeping.log | awk '/Iteration:/ {printf "Iteration %s:", $$2; fflush()} /Device:/ {print $$4; fflush()}'; \
|
||
else \
|
||
echo "sweeping.log does not exist."; \
|
||
fi
|
||
|
||
# --- 網格依賴規則 ---
|
||
# 當沒有 device_2d.msh 或 device_config.py 有更動時,自動觸發 mesh 流程
|
||
device_2d.msh: device_config.py generate_mesh_2d.py generate_analytical_bgmesh.py
|
||
$(MAKE) mesh
|
||
|
||
clean:
|
||
@echo ">>> 清除暫存與網格檔案..."
|
||
rm -f *.msh *.pos *.tec *.png *.csv *.vtm *.vtu *.visit
|
||
rm -rf __pycache__ physics/__pycache__
|