進化的アルゴリズム(EA)
- CrySPYの基本的な使い方に関しては,はじめにチュートリアル > ランダムサーチ(RS)を見ること.
- アルゴリズム詳細については,探索アルゴリズム > 進化的アルゴリズム(EA)を参照すること.
入力ファイルの準備
下記のどれか一つに従い,その後CrySPY実行のセクションに移ること.
下記のどれか一つに従い,その後CrySPY実行のセクションに移ること.
2025年7月11日 更新
ここで利用しているファイルはCrySPY_utility/examples/ase_Cu8Au8_EAからダウンロードできる. このチュートリアルでは,計算が軽いASEのPure Python EMT calculatorを用いてローカルPCで動作確認を行う.対象となるシステムはCu8Au8.
cryspy.in
の例.
[basic]
algo = EA
calc_code = ASE
nstage = 1
njob = 5
jobcmd = zsh
jobfile = job_cryspy
[structure]
atype = Cu Au
nat = 8 8
[EA]
n_pop = 10
n_crsov = 5
n_perm = 2
n_strain = 2
n_rand = 1
n_elite = 1
n_fittest = 5
slct_func = TNM
t_size = 2
maxgen_ea = 0
[ASE]
ase_python = ase_in.py
[option]
algo = EA
を使用.jobcmd
をbashに変更.tot_struc
は使用しない.構造数はn_pop
で決まる.n_pop
= n_crsov
+ n_perm
+ n_strain
+ n_rand
calc_in/以下はチュートリアル > ランダムサーチ(RS) > ASE in your local PCと同様
from ase.constraints import FixSymmetry
from ase.filters import FrechetCellFilter
from ase.calculators.emt import EMT
from ase.optimize import BFGS
from ase.io import read, write
# ---------- input structure
# CrySPY outputs 'POSCAR' as an input file in work/xxxxxx directory
atoms = read('POSCAR', format='vasp')
# ---------- setting and run
atoms.calc = EMT()
atoms.set_constraint([FixSymmetry(atoms)])
cell_filter = FrechetCellFilter(atoms, hydrostatic_strain=False)
opt = BFGS(cell_filter)
# ---------- run
converged = opt.run(fmax=0.01, steps=2000)
# ---------- rule in ASE interface
# output file for energy: 'log.tote' in eV/cell
# CrySPY reads the last line of 'log.tote' file
# outimized structure: 'CONTCAR' file in vasp format
# check_opt: 'out_check_opt' file ('done' or 'not yet')
# CrySPY reads the last line of 'out_check_opt' file
# ------ energy
e = cell_filter.atoms.get_total_energy() # eV/cell
with open('log.tote', mode='w') as f:
f.write(str(e))
# ------ struc
opt_atoms = cell_filter.atoms.copy()
opt_atoms.set_constraint(None) # remove constraint for pymatgen
write('CONTCAR', opt_atoms, format='vasp', direct=True)
# ------ check_opt
with open('out_check_opt', mode='w') as f:
if converged:
f.write('done\n')
else:
f.write('not yet\n')
#!/bin/sh
# ---------- ASE
python3 ase_in.py > out.log
2025年4月6日
cryspy
を実行すると,構造生成モードになり,第一世代のランダム構造が生成されて,一旦プログラムは終了する.
cryspy
出力
...
[2025-04-06 09:15:34,720][ea_init][INFO] # ---------- Initialize evolutionary algorithm
[2025-04-06 09:15:34,720][ea_init][INFO] # ------ Generation 1
[2025-04-06 09:15:34,720][ea_init][INFO] 10 structures by random
cryspy.stat
を見ると,EAでは現在の世代の情報が追加されている.
[status]
generation = 1
id_queueing = 0 1 2 3 4 5 6 7 8 9
何度かcryspy
を実行して,第一世代の構造最適化を終わらせると,下記のように出力される.
...
[2025-04-06 09:20:26,218][ctrl_job][INFO] Done generation 1
[2025-04-06 09:20:26,218][ctrl_job][INFO]
EA is ready
準備ができた状態でもう一度cryspy
を実行すると,バックアップが実行されてから,次世代構造の生成が始まる.
cryspy
...
[2025-04-06 09:35:11,546][cryspy_restart][INFO] read input, cryspy.in
[2025-04-06 09:35:11,554][ctrl_job][INFO] # ---------- job status
[2025-04-06 09:35:11,554][ctrl_job][INFO] Done generation 1
[2025-04-06 09:35:11,554][utility][INFO] Backup data
[2025-04-06 09:35:11,611][ea_next_gen][INFO] # ---------- Evolutionary algorithm
[2025-04-06 09:35:11,611][ea_next_gen][INFO] Generation 2
[2025-04-06 09:35:11,613][ea_next_gen][INFO] # ------ natural selection
[2025-04-06 09:35:11,687][ea_next_gen][INFO] ranking without duplication (including elite):
[2025-04-06 09:35:11,687][ea_next_gen][INFO] Structure ID 1, fitness: -0.00530
[2025-04-06 09:35:11,687][ea_next_gen][INFO] Structure ID 3, fitness: 0.01490
[2025-04-06 09:35:11,687][ea_next_gen][INFO] Structure ID 4, fitness: 0.04485
[2025-04-06 09:35:11,687][ea_next_gen][INFO] Structure ID 7, fitness: 0.11501
[2025-04-06 09:35:11,687][ea_next_gen][INFO] Structure ID 8, fitness: 0.15254
[2025-04-06 09:35:11,687][ea_next_gen][INFO] # ------ Generate children
[2025-04-06 09:35:11,687][ea_child][INFO] # -- mindist
[2025-04-06 09:35:11,689][struc_util][INFO] Cu - Cu: 1.32
[2025-04-06 09:35:11,689][struc_util][INFO] Cu - Au: 1.34
[2025-04-06 09:35:11,689][struc_util][INFO] Au - Au: 1.36
[2025-04-06 09:35:11,740][crossover][INFO] Structure ID 10 (8, 8) was generated from 3 and 1 by crossover. Space group: 1 P1
[2025-04-06 09:35:11,764][crossover][WARNING] remove_within_mindist: some atoms within mindist. retry.
[2025-04-06 09:35:11,774][crossover][INFO] Structure ID 11 (8, 8) was generated from 3 and 1 by crossover. Space group: 1 P1
[2025-04-06 09:35:11,789][crossover][INFO] Structure ID 12 (8, 8) was generated from 1 and 4 by crossover. Space group: 1 P1
[2025-04-06 09:35:11,833][crossover][INFO] Structure ID 13 (8, 8) was generated from 1 and 3 by crossover. Space group: 1 P1
[2025-04-06 09:35:11,852][crossover][WARNING] mindist in _add_border_line: Cu - Cu, 0.567032320824818. retry.
[2025-04-06 09:35:11,861][crossover][INFO] Structure ID 14 (8, 8) was generated from 7 and 1 by crossover. Space group: 1 P1
[2025-04-06 09:35:11,875][permutation][INFO] Structure ID 15 (8, 8) was generated from 1 by permutation. Space group: 146 R3
[2025-04-06 09:35:11,888][permutation][INFO] Structure ID 16 (8, 8) was generated from 3 by permutation. Space group: 1 P1
[2025-04-06 09:35:11,890][strain][WARNING] mindist in strain: Cu - Cu, 1.3050485787603692. retry.
[2025-04-06 09:35:11,903][strain][INFO] Structure ID 17 (8, 8) was generated from 3 by strain. Space group: 1 P1
[2025-04-06 09:35:11,917][strain][INFO] Structure ID 18 (8, 8) was generated from 1 by strain. Space group: 1 P1
[2025-04-06 09:35:12,513][ea_child][INFO] # ------ Random structure generation
[2025-04-06 09:35:12,513][rs_gen][INFO] # ------ mindist
[2025-04-06 09:35:12,515][struc_util][INFO] Cu - Cu: 1.32
[2025-04-06 09:35:12,515][struc_util][INFO] Cu - Au: 1.34
[2025-04-06 09:35:12,515][struc_util][INFO] Au - Au: 1.36
[2025-04-06 09:35:12,516][rs_gen][INFO] # ------ generate structures
[2025-04-06 09:35:12,530][gen_pyxtal][INFO] Structure ID 19: (8, 8) Space group: 86 --> 86 P4_2/n
[2025-04-06 09:35:12,533][ea_next_gen][INFO] # ------ Select elites
[2025-04-06 09:35:12,533][ea_next_gen][INFO] Structure ID 9 keeps as the elite
あとはcryspy
の実行を繰り返せば,探索が進む.
以下は第3世代まで計算を終えたcryspy_rslt
の例である.EAでは世代の情報(Gen
)も追加される.
Gen Spg_num Spg_sym Spg_num_opt Spg_sym_opt E_eV_atom Magmom Opt
0 1 214 I4_132 230 Ia-3d 1.168743 NaN no_file
1 1 198 P2_13 198 P2_13 -0.005303 NaN no_file
2 1 95 P4_322 95 P4_322 0.389566 NaN no_file
3 1 27 Pcc2 27 Pcc2 0.014898 NaN no_file
4 1 60 Pbcn 60 Pbcn 0.044852 NaN no_file
5 1 116 P-4c2 116 P-4c2 0.403246 NaN no_file
6 1 187 P-6m2 187 P-6m2 1.054706 NaN no_file
7 1 161 R3c 160 R3m 0.115009 NaN no_file
8 1 146 R3 146 R3 0.152535 NaN no_file
9 1 60 Pbcn 47 Pmmm -0.005676 NaN no_file
10 2 1 P1 1 P1 0.026070 NaN no_file
11 2 1 P1 7 Pc 0.005898 NaN no_file
12 2 1 P1 1 P1 0.005208 NaN no_file
13 2 1 P1 1 P1 0.005506 NaN no_file
14 2 1 P1 1 P1 0.024364 NaN no_file
15 2 146 R3 146 R3 0.011525 NaN no_file
16 2 1 P1 1 P1 0.014590 NaN no_file
17 2 1 P1 1 P1 0.015236 NaN no_file
18 2 1 P1 2 P-1 -0.012335 NaN no_file
19 2 86 P4_2/n 140 I4/mcm 0.274548 NaN no_file
20 3 1 P1 1 P1 0.013611 NaN no_file
21 3 1 P1 10 P2/m -0.014166 NaN no_file
22 3 1 P1 1 P1 0.019472 NaN no_file
23 3 1 P1 1 P1 0.011641 NaN no_file
24 3 1 P1 1 P1 0.000297 NaN no_file
25 3 1 P1 1 P1 0.005596 NaN no_file
26 3 1 P1 1 P1 0.013374 NaN no_file
27 3 1 P1 2 P-1 0.005055 NaN no_file
28 3 2 P-1 12 C2/m -0.012396 NaN no_file
29 3 182 P6_322 182 P6_322 0.711472 NaN no_file
各世代ごとに用いたEAのパラメーターがea_info
に出力される.
Gen Population Crossover Permutation Strain Random Elite crs_lat slct_func
1 10 0 0 0 10 0 random TNM
2 10 5 2 2 1 1 random TNM
3 10 5 2 2 1 1 random TNM
構造生成手法や親個体の情報が,ea_origin
に出力される.
Gen Struc_ID Operation Parent
1 0 random None
1 1 random None
1 2 random None
1 3 random None
1 4 random None
1 5 random None
1 6 random None
1 7 random None
1 8 random None
1 9 random None
2 10 crossover (3, 1)
2 11 crossover (3, 1)
2 12 crossover (1, 4)
2 13 crossover (1, 3)
2 14 crossover (7, 1)
2 15 permutation (1,)
2 16 permutation (3,)
2 17 strain (3,)
2 18 strain (1,)
2 19 random None
2 9 elite elite
3 20 crossover (18, 12)
3 21 crossover (12, 9)
3 22 crossover (12, 18)
3 23 crossover (18, 9)
3 24 crossover (13, 18)
3 25 permutation (18,)
3 26 permutation (9,)
3 27 strain (18,)
3 28 strain (18,)
3 29 random None
3 18 elite elite
ここでは,CrySPYのデータをローカルPCで解析・可視化することを前提としている.
CrySPYをスーパーコンピュータやワークステーションで使用している場合は,データをローカルPCにダウンロードすること.
work
や backup
ディレクトリは,ファイルサイズが非常に大きくなる可能性があるため,不要であれば削除してよい.
先ほどダウンロードした結果の中にある data/
ディレクトリに移動する.
その後,CrySPY utilityがローカルにダウンロード してある場合は cryspy_analyzer_EA.ipynb
をコピーする.
またはGitHubから直接ダウンロードしてくる(CrySPY_utility/notebook/).
Jupyter を起動し(VScode,Jupyter Lab,Jupyter Notebook など),
セルを順番に実行するだけで,以下のような図を得ることができる.