Skip to content

Latest commit

 

History

History
494 lines (360 loc) · 21.8 KB

File metadata and controls

494 lines (360 loc) · 21.8 KB

10回ランダム注入(ensemble)で分布を作る手順

このリポジトリは「1回の注入 → vacancy_list / N_list → ヒストグラム」になっていたので、 注入位置 (x,y) を run ごとに変えた 10回分の結果をまとめてヒストグラム化できるようにしました。

何を変えたか

前提

  • Python でグラフを保存するために matplotlib が必要です。
    • 例: python -m pip install matplotlib

1) 10回分の run フォルダを作る(注入位置をランダム化)

リポジトリルートで:

python tools/run_ensemble_implantation.py `
  --template-dir "10Ncluster_implantation to C_5keV" `
  --input "10atoms_5keV_N_implantation_to_C_ZBL_potential_filedata.txt" `
  --out "10Ncluster_implantation to C_5keV/runs" `
  --runs 10 `
  --seed 12345 `
  --x-range -20 20 `
  --y-range -20 20

WSL/Linux(bash) で実行する場合(重要)

  • bash では 改行継続にバッククォート ` は使いません(PowerShell 用です)。
  • bash の改行継続は バックスラッシュ \ です。
  • 迷ったら、まずは「1行」で実行してください(スペース入りパスは必ずダブルクォート)。

1行版(bash):

python3 tools/run_ensemble_implantation.py --template-dir "10Ncluster_implantation to C_5keV" --input "10atoms_5keV_N_implantation_to_C_ZBL_potential_filedata.txt" --out "10Ncluster_implantation to C_5keV/runs" --runs 10 --seed 12345 --x-range -20 20 --y-range -20 20

複数行版(bash):

python3 tools/run_ensemble_implantation.py \
  --template-dir "10Ncluster_implantation to C_5keV" \
  --input "10atoms_5keV_N_implantation_to_C_ZBL_potential_filedata.txt" \
  --out "10Ncluster_implantation to C_5keV/runs" \
  --runs 10 \
  --seed 12345 \
  --x-range -20 20 \
  --y-range -20 20

例: C_7keV(final) に runs を作って、そのまま10回自動実行(WSL/bash)

python3 tools/run_ensemble_implantation.py --template-dir "10Ncluster_implantation to C_7keV(final)" --input "10atoms_7keV_N_implantation_to_C_ZBL_potential_filedata.txt" --out "10Ncluster_implantation to C_7keV(final)/runs" --runs 10 --seed 12345 --x-range -20 20 --y-range -20 20 --lammps "mpirun -np 8 lmp -in in.lmp"

これで 10Ncluster_implantation to C_5keV/runs/run_01/in.lmp のように生成されます。

そのまま LAMMPS も10回回す場合(任意)

LAMMPS のコマンド名は環境により違います(lmp, lmp_mpi, lammps など)。例:

python tools/run_ensemble_implantation.py `
  --template-dir "10Ncluster_implantation to C_5keV" `
  --input "10atoms_5keV_N_implantation_to_C_ZBL_potential_filedata.txt" `
  --out "10Ncluster_implantation to C_5keV/runs" `
  --runs 10 `
  --seed 12345 `
  --x-range -20 20 `
  --y-range -20 20 `
  --lammps "lmp -in in.lmp"

1b) 連続注入(noreset, 10回注入)をさらに ensemble で10回回す

例: N_implantation_to_C_random_5keV_noreset_10_10(final)/1atoms_5keV_N_implantation_to_C_ZBL_potential_filedata.txt

このタイプのテンプレは LAMMPS入力の中で variable m loop 10 のように「10回の連続注入」を自前で回す作りになっています。 なので「10回連続注入」を さらに ensemble で10回(= 10注入×10run)やるには、 テンプレを run_01 .. run_10 に複製して 各runで乱数の基準seed(例: 12345/67890)だけを変えて LAMMPS を実行するのが簡単です。

ポイント:

  • このテンプレは x_pos/y_pos を入力内で random(...) で決めます(Python側で x-range/y-range を渡す方式ではありません)。
  • 12345+${m} / 67890+${m} の「12345/67890」を run ごとに変えると、10回注入の乱数系列が run ごとに変わります。

まずはこれ(おすすめ): run_ensemble_implantation.py で一発

--template-style loop-random-xy を使うと、連続注入テンプレ内の

  • rnd_seed / rnd_seed_y の基準値
  • x_pos / y_posrandom(xmin,xmax,...) の範囲

を run ごとに差し替えた run_01..run_10 を作り、そのまま LAMMPS まで実行できます。

python3 tools/run_ensemble_implantation.py \
  --template-style loop-random-xy \
  --template-dir "N_implantation_to_C_random_5keV_noreset_10_10(final)" \
  --input "1atoms_5keV_N_implantation_to_C_ZBL_potential_filedata.txt" \
  --out "N_implantation_to_C_random_5keV_noreset_10_10(final)/runs_ensemble" \
  --runs 10 \
  --seed 12345 \
  --x-range -4.6 4.6 \
  --y-range -4.6 4.6 \
  --lammps "mpirun -np 8 lmp -in in.lmp"

PowerShell 例(run_01..run_10 を作って順に実行)

$templateDir = "N_implantation_to_C_random_5keV_noreset_10_10(final)"
$templateIn  = "1atoms_5keV_N_implantation_to_C_ZBL_potential_filedata.txt"
$outRoot     = "N_implantation_to_C_random_5keV_noreset_10_10(final)/runs_ensemble"

New-Item -ItemType Directory -Force $outRoot | Out-Null

for ($i=1; $i -le 10; $i++) {
  $runDir = Join-Path $outRoot ("run_{0:D2}" -f $i)
  New-Item -ItemType Directory -Force $runDir | Out-Null

  # テンプレフォルダから必要ファイルをコピー(状況に応じて追加)
  Copy-Item -Force (Join-Path $templateDir "*.data")   $runDir -ErrorAction SilentlyContinue
  Copy-Item -Force (Join-Path $templateDir "*.zbl")    $runDir -ErrorAction SilentlyContinue
  Copy-Item -Force (Join-Path $templateDir "*.tersoff*") $runDir -ErrorAction SilentlyContinue

  # 入力ファイルを in.lmp として複製し、seed基準を run ごとに変更
  $text = Get-Content (Join-Path $templateDir $templateIn) -Raw
  $seedBaseX = 12345 + 1000*$i
  $seedBaseY = 67890 + 1000*$i
  $text = $text.Replace("variable        rnd_seed equal 12345+${m}",  "variable        rnd_seed equal $seedBaseX+${m}")
  $text = $text.Replace("variable        rnd_seed_y equal 67890+${m}","variable        rnd_seed_y equal $seedBaseY+${m}")
  Set-Content -Path (Join-Path $runDir "in.lmp") -Value $text -Encoding utf8

  # 実行(LAMMPSコマンドは環境に合わせて)
  Push-Location $runDir
  # 例: mpirun / lmp は環境で調整
  mpirun -np 8 lmp -in in.lmp
  Pop-Location
}

WSL/Linux(bash) 例(run_01..run_10 を作って順に実行)

template_dir="N_implantation_to_C_random_5keV_noreset_10_10(final)"
template_in="1atoms_5keV_N_implantation_to_C_ZBL_potential_filedata.txt"
out_root="N_implantation_to_C_random_5keV_noreset_10_10(final)/runs_ensemble"

mkdir -p "$out_root"

for i in $(seq -w 1 10); do
  run_dir="$out_root/run_$i"
  mkdir -p "$run_dir"

  cp -f "$template_dir"/*.data "$run_dir" 2>/dev/null || true
  cp -f "$template_dir"/*.zbl "$run_dir" 2>/dev/null || true
  cp -f "$template_dir"/*.tersoff* "$run_dir" 2>/dev/null || true

  seed_base_x=$((12345 + 1000*10#$i))
  seed_base_y=$((67890 + 1000*10#$i))

  sed \
    -e "s/variable        rnd_seed equal 12345+\${m}/variable        rnd_seed equal ${seed_base_x}+\${m}/" \
    -e "s/variable        rnd_seed_y equal 67890+\${m}/variable        rnd_seed_y equal ${seed_base_y}+\${m}/" \
    "$template_dir/$template_in" > "$run_dir/in.lmp"

  (cd "$run_dir" && mpirun -np 8 lmp -in in.lmp)
done

調整したい場合:

  • 位置の範囲はテンプレ内の random(-4.6,4.6,...) を基板サイズに合わせて調整してください。
  • 各runが dump_all.xyzfinal_data_run_*.data を出します(runフォルダ分けしているので上書きはしません)。

2) vacancy の深さ分布(10回分をまとめて)

run_* をフォルダ指定で渡せます:

python "10Ncluster_implantation to C_0.5keV(final)/analysis3_graph_vacancy_list.py" `
  "10Ncluster_implantation to C_5keV/runs/run_*" `
  --out "vacancy_depths_ensemble.png" `
  --surface-z 125 `
  --bin-width 1

Ubuntu/WSL(bash) 例(C_7keV(final) の runs から出す):

python3 "10Ncluster_implantation to C_0.5keV(final)/analysis3_graph_vacancy_list.py" \
  "10Ncluster_implantation to C_7keV(final)/runs/run_*" \
  --out "10Ncluster_implantation to C_7keV(final)/runs/vacancy_depths_ensemble.png" \
  --surface-z 125 \
  --bin-width 1

2b) vacancy の深さ分布(2条件を重ね描き: 例 5keV vs 7keV)

analysis3_graph_vacancy_list2.py は、2つの vacancy_list(またはその親フォルダ)を指定して、 深さ分布を 線グラフ2本で同じ図に重ね描きして保存します。

前提(あなたの想定):

  • 10Ncluster_implantation to C_5keV(final) と同じ階層に analysis3_graph_vacancy_list2.py を置く
    • 例: 10Ncluster_implantation to C_5keV(final)/analysis3_graph_vacancy_list2.py
  • 比較したい入力は
    • 10Ncluster_implantation to C_5keV(final)/runs/run_01/vacancy_list
    • 10Ncluster_implantation to C_7keV(final)/runs/run_01/vacancy_list

PowerShell 例(リポジトリルートで実行):

python "10Ncluster_implantation to C_5keV(final)/analysis3_graph_vacancy_list2.py" `
  --a "10Ncluster_implantation to C_5keV(final)/runs/run_01/vacancy_list" `
  --b "10Ncluster_implantation to C_7keV(final)/runs/run_01/vacancy_list" `
  --label-a "10cluster total 5keV" `
  --label-b "10cluster 7keV" `
  --surface-z 125 `
  --bin-width 1 `
  --out "vacancy_depths_5keV_vs_7keV_run01.png"

Ubuntu/WSL(bash) 例:

python3 "10Ncluster_implantation to C_5keV(final)/analysis3_graph_vacancy_list2.py" \
  --a "10Ncluster_implantation to C_5keV(final)/runs/run_01/vacancy_list" \
  --b "10Ncluster_implantation to C_7keV(final)/runs/run_01/vacancy_list" \
  --label-a "10cluster total 5keV" \
  --label-b "10cluster 7keV" \
  --surface-z 125 \
  --bin-width 1 \
  --out "vacancy_depths_5keV_vs_7keV_run01.png"

補足:

  • --a / --b には vacancy_list ファイルだけでなく、run_01 のような フォルダも指定できます(その場合はフォルダ内の vacancy_list.xyz / vacancy_list / vacancy_list.txt を自動探索します)。
  • --bin-width は「線グラフの刻み(ヒストグラムのビン幅)」です。滑らかにしたい場合は大きく(例: 2〜5Å)、細かく見たい場合は小さく(例: 1Å)します。

3) N の深さ分布(10回分をまとめて)

注意:

  • このスクリプトは 引数なしで実行すると「スクリプトと同じフォルダ」にある N_list を探します。
  • runs 配下のデータで分布を作る場合は、必ず run_*/...入力パターンを引数として渡してください

dump_run_1_min0K.lammpstrj を直接読む例(N が type=3 の場合):

python "10Ncluster_implantation to C_0.5keV(final)/analysis2_graph_N_list3.py" `
  "10Ncluster_implantation to C_5keV/runs/run_*/dump_run_1_min0K.lammpstrj" `
  --atom-type 3 `
  --out "nitrogen_depths_ensemble.png" `
  --surface-z 125 `
  --bin-width 5 `
  --max-depth 250

Ubuntu/WSL(bash) 例(C_7keV(final) の runs から出す):

python3 "10Ncluster_implantation to C_0.5keV(final)/analysis2_graph_N_list3.py" \
  "10Ncluster_implantation to C_7keV(final)/runs/run_*/dump_run_1_min0K.lammpstrj" \
  --atom-type 3 \
  --out "10Ncluster_implantation to C_7keV(final)/runs/nitrogen_depths_ensemble.png" \
  --surface-z 125 \
  --bin-width 5 \
  --max-depth 250

補足:

  • パスにスペースがあるので、run_*/... の引数はダブルクォートで囲んでOKです。 その場合でもスクリプト側でワイルドカードを展開して読み込みます。
  • --max-depth 250 を付けると、基板厚(例: 250Å)より深い値(底から抜けた原子など)を描画から除外できます。

事前確認(ファイルが本当にあるか):

ls "10Ncluster_implantation to C_7keV(final)/runs/run_01"
ls "10Ncluster_implantation to C_7keV(final)/runs/run_01/dump_run_1_min0K.lammpstrj"

よくある調整ポイント

  • --x-range/--y-range は「基板の有効領域(表面の中央付近)」に合わせて調整してください。
  • --surface-z は基板モデルの表面 z に合わせてください(いまは既存スクリプトと同じ 125 Å をデフォルトにしています)。

エラー対処

FileNotFoundError: テンプレ入力が見つかりません

原因はほぼ次のどれかです:

  • --template-dir のフォルダ名が実際と違う(例: ...final が付いている等)
  • --input のファイル名が実際と違う(keV値や _filedata の有無など)
  • 実行しているカレントディレクトリがリポジトリルートではない

bash/WSL なら、まず次で「存在する名前」を確認してコピペしてください:

pwd
ls
ls "10Ncluster_implantation to C_5keV"

その上で、--template-dir--input実在する名前 に合わせます。

分布(グラフ出力)でエラーが出る

まず、どのエラーでも共通で次を確認してください:

# リポジトリルートにいるか確認
pwd
ls

# runフォルダに必要ファイルがあるか確認(例: run_01)
ls "10Ncluster_implantation to C_7keV(final)/runs/run_01"

よくある原因:

  • ModuleNotFoundError: No module named 'matplotlib'
    • 対処: python3 -m pip install --user matplotlib
  • ...入力が見つかりません(vacancy_list / dump が無い)
    • N分布: 各runに dump_run_1_min0K.lammpstrj があるか確認
    • vacancy分布: 各runに vacancy_list(または vacancy_list.xyz 等)が必要です(OVITO等でrunごとに出力)
  • パスにスペースがある
    • 対処: 文字列は必ずダブルクォートで囲む(例: "10Ncluster_implantation to C_7keV(final)/..."

こちらで原因を特定するため、エラーが出たときは「実行したコマンド」と「Traceback全文」をそのまま貼ってください。

付録: 「run_01..run_10」で何を変えているか(run10まで回る仕組み)

2つのコマンド例は、どちらも tools/run_ensemble_implantation.py が テンプレート入力を複製して run_01run_10 を作り、各 run で 変数(seed / 注入位置 or 乱数基準seed)だけを差し替えることで実現しています。

共通の仕組み(どのテンプレでも同じ):

  • --runs 10 により run_01run_1010フォルダを作ります(run_{i:02d} 形式)。
  • それぞれの run フォルダに
    • テンプレフォルダから *.data, *.zbl, *.tersoff*--copy で変更可)をコピー
    • 入力ファイルをパッチして in.lmp として保存
    • 実際に差し替えた値を run_params.txt に保存
  • --lammps "..." を付けると、各 run フォルダをカレントにして、そのコマンドを run_01 → run_02 → … → run_10 の順に逐次実行します。

つまり「run10までやってる」のは、LAMMPS 側ではなく Python 側の --runs 10 の for ループです。

A) 10クラスターを10回(template-style: simple / デフォルト)

例(あなたのコマンド):

python3 tools/run_ensemble_implantation.py --template-dir "10Ncluster_implantation to C_7keV(final)" --input "10atoms_7keV_N_implantation_to_C_ZBL_potential_filedata.txt" --out "10Ncluster_implantation to C_7keV(final)/runs" --runs 10 --seed 12345 --x-range -20 20 --y-range -20 20 --lammps "mpirun -np 8 lmp -in in.lmp"

このケースでは、各 run_XX/in.lmp の中で次の行を runごとに直接書き換えます:

  • variable seed equal ...
  • variable x_pos equal ...
  • variable y_pos equal ...

runごとの値の決まり方(実装):

  • 乱数生成器は random.Random(--seed) で初期化され、x_pos/y_pos はそこから一様乱数で作られます。
    • --seed 12345 を固定すると、毎回同じ run_01..run_10 が再現されます。
  • seedseed_i = (--seed) + i(i は 1..10)になります。
    • 例: run_01 は 12346、run_10 は 12355。

ここでいう「10回」は **10回の独立な単発注入(初期状態から開始)**です。 (テンプレ側が「前runの最終構造を読み込む」作りでない限り、run間で状態は引き継がれません)

B) 10連続照射を10回(template-style: loop-random-xy)

例(あなたのコマンド):

python3 tools/run_ensemble_implantation.py \
  --template-style loop-random-xy \
  --template-dir "N_implantation_to_C_random_5keV_noreset_10_10(final)" \
  --input "1atoms_5keV_N_implantation_to_C_ZBL_potential_filedata.txt" \
  --out "N_implantation_to_C_random_5keV_noreset_10_10(final)/runs_ensemble" \
  --runs 10 \
  --seed 12345 \
  --x-range -4.6 4.6 \
  --y-range -4.6 4.6 \
  --lammps "mpirun -np 8 lmp -in in.lmp"

このケースは「連続注入(noreset)」テンプレが、LAMMPS 入力内部で注入ループ(例: variable m loop 10)を回し、 注入位置も入力内部の random(...) で決める前提です。 そのため Python は x_pos/y_pos を数値に固定せず、テンプレ内の次の行を runごとに差し替えます:

  • variable rnd_seed equal ...(連続注入ループで使う乱数seedの“基準値”)
  • variable rnd_seed_y equal ...
  • variable x_pos equal random(xmin,xmax,${rnd_seed})(random の範囲だけ)
  • variable y_pos equal random(ymin,ymax,${rnd_seed_y})

runごとの値の決まり方(実装):

  • seed_x_base = (--seed) + 1000*i
  • seed_y_base = (--seed) + 50000 + 1000*i
  • x_pos/y_pos の範囲は --x-range/--y-range をそのまま random(xmin,xmax,...) に反映

注意:

  • この loop-random-xy では、Python 側の乱数(random.Random(...))は注入位置の生成に使いません。 --seed は「テンプレ内で使う rnd_seed の基準値」を runごとにずらすための元になります。

具体例(デフォルト値での run1 / run2)

例として、コマンドが次だとします(あなたの例と同じ値):

  • --runs 10
  • --seed 12345
  • --x-range -4.6 4.6
  • --y-range -4.6 4.6

このとき Python が各 run の in.lmp に書き込むのは、概ね次の形です(${m} はテンプレ側の loop 変数):

  • variable rnd_seed equal seed_x_base+${m}
  • variable rnd_seed_y equal seed_y_base+${m}
  • variable x_pos equal random(-4.6,4.6,${rnd_seed})
  • variable y_pos equal random(-4.6,4.6,${rnd_seed_y})

そして seed_x_base / seed_y_base は run 番号 i(run_01 なら i=1)から次で決まります:

  • seed_x_base = 12345 + 1000*i
  • seed_y_base = 12345 + 50000 + 1000*i

なので具体的には:

  • run_01(i=1)

    • seed_x_base = 13345
    • seed_y_base = 63345
    • もしテンプレが variable m loop 10(m=1..10)なら
      • x側 seed: rnd_seed = 13345+${m} → 13346, 13347, …, 13355
      • y側 seed: rnd_seed_y = 63345+${m} → 63346, 63347, …, 63355
      • 各 m ごとに x_pos=random(-4.6,4.6,<その時のseed>)y_pos=random(-4.6,4.6,<その時のseed>) が1回ずつ決まり、合計10回の (x,y) が生成されます。
  • run_02(i=2)

    • seed_x_base = 14345
    • seed_y_base = 64345
    • m=1..10 のとき
      • x側 seed: 14346..14355
      • y側 seed: 64346..64355

ポイント:

  • run_01 と run_02 は「基準seed」が違うので、同じ m=1..10 を回しても (x,y) の系列が別物になります。
  • --x-range/--y-range範囲を変えるだけで、「10回回す」はテンプレ内の loop 回数(例: loop 10)で決まります。

「連続照射」は2通りある(意味が違う)

連続照射(10回照射)をやりたいとき、次の2パターンは物理的な意味が変わります。

  1. 同一run内で連続(テンプレ内で loop 10)
  • 1つの run フォルダで LAMMPS を1回走らせ、その入力の中で注入を10回繰り返します。
  • noreset テンプレなら、照射ダメージが蓄積した状態で次の注入が行われます。
  1. run を分けて実行(run_01..run_10 を別々に走らせる)
  • template-style simple の 10回は、基本的に 初期状態からの単発注入×10回の独立試行です。
  • template-style loop-random-xy の 10回は、「10連続照射」を1セットとしてそれを独立に10回(= 10照射×10run)やる、という意味になります。
  1. (重要)runを分けて「連続照射を繋ぐ」
  • 「run_01 の最終構造を run_02 が読み、さらに1照射…」のように、run間で状態を引き継いで連続照射したい場合、 現状の tools/run_ensemble_implantation.py はその“チェーン”を自動生成しません。
  • 実現するには、テンプレ入力側で read_data / read_restart の参照先を前runの出力に向ける(または手動で run を1つのフォルダで連続実行する)必要があります。