Skip to content

Commit 52c5581

Browse files
committed
add magnetic option to bulk calculation
1 parent bf07625 commit 52c5581

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

adlib/bulk/calc.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,13 @@ def make_scf_run_file_array(dest_dir, N_runs, job_name='bulk_energy', nproc=16):
9999
f.write(f'python calc.py\n')
100100

101101

102-
def make_scf_calc_file(calc_dir, lattice_constant, metal='Cu', crystal_structure='fcc', ecutwfc=1000, kpt=9, smear=0.1, nproc=16):
102+
def make_scf_calc_file(calc_dir, lattice_constant, metal='Cu', crystal_structure='fcc', ecutwfc=1000, kpt=9, smear=0.1, nproc=16, magnetism=None):
103+
104+
magnetism_line = ""
105+
if str(magnetism).lower() == 'antiferromagnetic':
106+
magnetism_line = "bulk.set_initial_magnetic_moments([1.0, -1.0])\nassert len(bulk) == 2"
107+
elif str(magnetism).lower() == 'ferromagnetic':
108+
magnetism_line = "bulk.set_initial_magnetic_moments([1.0, 1.0])\nassert len(bulk) == 2"
103109

104110
python_file_lines = [
105111
"import os",
@@ -134,6 +140,7 @@ def make_scf_calc_file(calc_dir, lattice_constant, metal='Cu', crystal_structure
134140
"",
135141
f"bulk = ase.build.bulk('{metal}', crystalstructure='{crystal_structure}', a={lattice_constant}, cubic=True)",
136142
"",
143+
magnetism_line,
137144
"pw_executable = os.environ['PW_EXECUTABLE']",
138145
"",
139146
"pseudopotentials = {",

adlib/bulk/convergence.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import adlib.bulk.calc
4747

4848

49-
def setup_kpts_converge(bulk_dir, lattice_constant_center, metal='Cu', crystal_structure='fcc'):
49+
def setup_kpts_converge(bulk_dir, lattice_constant_center, metal='Cu', crystal_structure='fcc', magnetism=None):
5050
"""
5151
script to set up N jobs to check kpts convergence
5252
"""
@@ -61,12 +61,12 @@ def setup_kpts_converge(bulk_dir, lattice_constant_center, metal='Cu', crystal_s
6161
for i, k in enumerate(kpts):
6262
for j, lattice_constant in enumerate(lattice_constants):
6363
calc_dir = os.path.join(kpts_dir, str(k), f'run_{j:04}')
64-
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal, crystal_structure=crystal_structure, ecutwfc=500, kpt=k, smear=0.1, nproc=16)
64+
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal, crystal_structure=crystal_structure, magnetism=magnetism, ecutwfc=500, kpt=k, smear=0.1, nproc=16)
6565

6666
adlib.bulk.calc.make_scf_run_file_array(os.path.join(kpts_dir, str(k)), j, job_name='kpts_bulk_converge')
6767

6868

69-
def setup_ecutwfc_converge(bulk_dir, lattice_constant_center, metal='Cu', crystal_structure='fcc'):
69+
def setup_ecutwfc_converge(bulk_dir, lattice_constant_center, metal='Cu', crystal_structure='fcc', magnetism=None):
7070
"""
7171
script to set up N jobs to check ecutwfc convergence
7272
"""
@@ -80,12 +80,12 @@ def setup_ecutwfc_converge(bulk_dir, lattice_constant_center, metal='Cu', crysta
8080
for i, ecut in enumerate(ecuts):
8181
for j, lattice_constant in enumerate(lattice_constants):
8282
calc_dir = os.path.join(ecutwfc_dir, str(ecut), f'run_{j:04}')
83-
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal, crystal_structure=crystal_structure, ecutwfc=ecut, kpt=7, smear=0.1, nproc=16)
83+
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal, crystal_structure=crystal_structure, magnetism=magnetism, ecutwfc=ecut, kpt=7, smear=0.1, nproc=16)
8484

8585
adlib.bulk.calc.make_scf_run_file_array(os.path.join(ecutwfc_dir, str(ecut)), j, job_name='ecut_bulk_converge')
8686

8787

88-
def setup_smear_converge(bulk_dir, lattice_constant_center, metal='Cu', crystal_structure='fcc'):
88+
def setup_smear_converge(bulk_dir, lattice_constant_center, metal='Cu', crystal_structure='fcc', magnetism=None):
8989
"""
9090
script to set up N jobs to check MV smearing convergence
9191
"""
@@ -98,7 +98,7 @@ def setup_smear_converge(bulk_dir, lattice_constant_center, metal='Cu', crystal_
9898
for i, smear in enumerate(smears):
9999
for j, lattice_constant in enumerate(lattice_constants):
100100
calc_dir = os.path.join(smear_dir, str(smear), f'run_{j:04}')
101-
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal, crystal_structure=crystal_structure, ecutwfc=1000, kpt=7, smear=smear, nproc=16)
101+
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal, crystal_structure=crystal_structure, magnetism=magnetism, ecutwfc=1000, kpt=7, smear=smear, nproc=16)
102102

103103
adlib.bulk.calc.make_scf_run_file_array(os.path.join(smear_dir, str(smear)), j, job_name='smear_bulk_converge')
104104

adlib/bulk/eos.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import adlib.bulk.calc
5454

5555

56-
def setup_eos(bulk_dir, lattice_constant_guess, metal='Cu', N=21, half_range=0.05):
56+
def setup_eos(bulk_dir, lattice_constant_guess, metal='Cu', N=21, half_range=0.05, magnetism=None):
5757
"""
5858
script to set up N jobs
5959
@@ -71,7 +71,7 @@ def setup_eos(bulk_dir, lattice_constant_guess, metal='Cu', N=21, half_range=0.0
7171

7272
for i, lattice_constant in enumerate(lattice_constants):
7373
calc_dir = os.path.join(eos_dir, f'run_{i:04}')
74-
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal, ecutwfc=1000, kpt=9, smear=0.1, nproc=16)
74+
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal, magnetism=magnetism, ecutwfc=1000, kpt=9, smear=0.1, nproc=16)
7575

7676
adlib.bulk.calc.make_scf_run_file_array(eos_dir, i, job_name='bulk_eos')
7777

@@ -90,7 +90,7 @@ def get_lattice_constant_from_atoms(atoms):
9090
return lattice_constant
9191

9292

93-
def setup_eos_coarse(bulk_dir, lattice_constant_guess, metal='Cu'):
93+
def setup_eos_coarse(bulk_dir, lattice_constant_guess, metal='Cu', magnetism=None):
9494
"""
9595
script to set up coarse calculation of E vs. lattice constant
9696
"""
@@ -102,12 +102,12 @@ def setup_eos_coarse(bulk_dir, lattice_constant_guess, metal='Cu'):
102102

103103
for i, lattice_constant in enumerate(lattice_constants):
104104
calc_dir = os.path.join(eos_dir, f'run_{i:04}')
105-
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal)
105+
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal, magnetism=magnetism)
106106

107107
adlib.bulk.calc.make_scf_run_file_array(eos_dir, i, job_name='bulk_eos_coarse')
108108

109109

110-
def setup_eos_fine(bulk_dir, lattice_constant_guess, metal='Cu', crystal_structure='fcc'):
110+
def setup_eos_fine(bulk_dir, lattice_constant_guess, metal='Cu', crystal_structure='fcc', magnetism=None):
111111
"""
112112
script to set up fine calculation of E vs. lattice constant
113113
"""
@@ -119,7 +119,7 @@ def setup_eos_fine(bulk_dir, lattice_constant_guess, metal='Cu', crystal_structu
119119

120120
for i, lattice_constant in enumerate(lattice_constants):
121121
calc_dir = os.path.join(eos_dir, f'run_{i:04}')
122-
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal, crystal_structure=crystal_structure)
122+
adlib.bulk.calc.make_scf_calc_file(calc_dir, lattice_constant, metal=metal, crystal_structure=crystal_structure, magnetism=magnetism)
123123

124124
adlib.bulk.calc.make_scf_run_file_array(eos_dir, i, job_name='bulk_eos_fine')
125125

@@ -182,8 +182,11 @@ def plot_energy_vs_lattice(calc_dir, dest_dir=None):
182182
for pwo_file in pwo_files:
183183
with open(pwo_file, 'r') as f:
184184
traj = list(read_espresso_out(f, index=slice(None)))
185-
atoms = traj[-1]
186-
energies.append(atoms.get_potential_energy())
185+
try:
186+
atoms = traj[-1]
187+
energies.append(atoms.get_potential_energy())
188+
except IndexError:
189+
energies.append(np.nan)
187190

188191
lattice_constant = get_lattice_constant_from_atoms(atoms)
189192
lattice_constants.append(lattice_constant)

adlib/bulk/vcrelax.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def make_run_vc_relax_script(calc_dir, nproc=16):
7676
f.write(f'python relax_bulk.py\n')
7777

7878

79-
def make_vc_relax_script(calc_dir, metal, lattice_constant, crystal_structure='fcc', nproc=16):
79+
def make_vc_relax_script(calc_dir, metal, lattice_constant, crystal_structure='fcc', magnetism=None, nproc=16):
8080
"""
8181
Make a python script that uses ase to run quantum espresso
8282
"""
@@ -85,12 +85,18 @@ def make_vc_relax_script(calc_dir, metal, lattice_constant, crystal_structure='f
8585
kpts = 5
8686
ecutwfc = 500
8787

88+
magnetism_line = ""
89+
if str(magnetism).lower() == 'antiferromagnetic':
90+
magnetism_line = "bulk.set_initial_magnetic_moments([1.0, -1.0])\nassert len(bulk) == 2"
91+
elif str(magnetism).lower() == 'ferromagnetic':
92+
magnetism_line = "bulk.set_initial_magnetic_moments([1.0, 1.0])\nassert len(bulk) == 2"
93+
8894
python_file_lines = [
8995
"import os",
9096
"import sys",
9197
"import time",
9298
"from ase.calculators.espresso import Espresso, EspressoProfile",
93-
"from ase.build import bulk",
99+
"import ase.build",
94100
"",
95101
"",
96102
"T = time.localtime()",
@@ -123,7 +129,8 @@ def make_vc_relax_script(calc_dir, metal, lattice_constant, crystal_structure='f
123129
"}",
124130
"",
125131
"",
126-
f"bulk_metal = bulk('{metal}', crystalstructure='{crystal_structure}', a={lattice_constant}, cubic=True)",
132+
f"bulk_metal = ase.build.bulk('{metal}', crystalstructure='{crystal_structure}', a={lattice_constant}, cubic=True)",
133+
magnetism_line,
127134
"",
128135
"pw_executable = os.environ['PW_EXECUTABLE']",
129136
"",
@@ -183,7 +190,7 @@ def run_vc_relax(bulk_dir):
183190
calc_dir = os.path.join(bulk_dir, 'vc_relax')
184191
cur_dir = os.getcwd()
185192
os.chdir(calc_dir)
186-
if environment == 'DISCOVERY':
193+
if environment in ['DISCOVERY', 'EXPLORER']:
187194
vc_relax_job = job_manager.SlurmJob()
188195
cmd = "sbatch run.sh"
189196
elif environment == 'SINGLE_NODE':

0 commit comments

Comments
 (0)