Skip to content

Commit 38454fd

Browse files
committed
Added to docstrings & more tidying up
1 parent 1f6d2c8 commit 38454fd

File tree

3 files changed

+128
-51
lines changed

3 files changed

+128
-51
lines changed

atomate/vasp/analysis/linear_response.py

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11

2+
from pymatgen import Structure
3+
from pymatgen.analysis.magnetism import CollinearMagneticStructureAnalyzer
4+
25
import numpy as np
36

47
def procure_response_dict(
5-
struct_final,
8+
struct_final, num_perturb_sites,
69
incar_dict, outcar_dict,
7-
inv_block_dict, response_dict,
10+
inv_block_dict, response_dict, perturb_dict,
11+
rkey, keys, ldaul_vals, analyzer_gs,
812
):
13+
"""
14+
Function to gather response data, in preparation for linear regression.
15+
This data is organized into `response_dict`.
16+
"""
17+
918
# perform magnetic ordering analysis
1019
analyzer_output = CollinearMagneticStructureAnalyzer(
1120
struct_final, threshold=0.61)
@@ -26,7 +35,6 @@ def procure_response_dict(
2635
specie = struct_final[i].specie
2736
ldaul = ldaul_vals[i]
2837

29-
# if ldaul != -1 and rkey:
3038
orbital = inv_block_dict[str(ldaul)]
3139
perturb_dict.update(
3240
{"site"+str(i): {"specie": str(specie),
@@ -52,18 +60,23 @@ def procure_response_dict(
5260

5361
response_dict[rkey]['magnetic order'].append(magnet_order)
5462

55-
# Function for fitting to response data. Returns: slope and associated error
5663
def response_fit(x, y):
64+
"""
65+
Function for fitting to response data. Returns: slope and associated error
66+
"""
5767

5868
(p, pcov) = np.polyfit(x, y, 1, cov=True)
5969
perr = np.sqrt(np.diag(pcov))
6070

6171
return p, perr
6272

63-
# Function for fitting to response data
64-
# - includes the "slope ~ zero" case for stepped data due to low precision
65-
# Returns: slope and associated error
6673
def response_fit_stepped(x, y, tol=1.0e-6):
74+
"""
75+
Function for fitting to response data
76+
- includes the "slope ~ zero" case for stepped data due to low precision
77+
Returns: slope and associated error
78+
"""
79+
6780
is_stepped = False
6881
step_id = -1
6982

@@ -96,8 +109,15 @@ def response_fit_stepped(x, y, tol=1.0e-6):
96109

97110
def obtain_response_matrices(
98111
n_response, spin_polarized,
99-
response_dict,
112+
response_dict, keys,
100113
):
114+
"""
115+
Function to compute self-consistent (SCF) and non-self-consistent (NSCF)
116+
linear response "chi" matrices; In addition to using linear regression
117+
to compute slopes about zero potential, the uncertainty associated
118+
with these values are also stored for subsequent error quantification.
119+
Returns: chi_matrix_nscf, chi_matrix_scf, chi_nscf_err, chi_scf_err
120+
"""
101121

102122
# Matrices for self-consistent and non-self-consistent responses
103123
# & associated element-wise errors
@@ -175,8 +195,11 @@ def obtain_response_matrices(
175195

176196
return chi_matrix_nscf, chi_matrix_scf, chi_nscf_err, chi_scf_err
177197

178-
# Function to compute the element-wise error propagation in matrix inversion
179198
def inverse_matrix_uncertainty(matrix, matrix_covar):
199+
"""
200+
Function to compute the element-wise error propagation in matrix inversion
201+
"""
202+
180203
m,n = matrix.shape
181204
if m != n:
182205
logger.warning("Matrix dimension error")
@@ -232,10 +255,12 @@ def det_deriv(matrix,i,j):
232255

233256
return matrixinv, matrixinv_var, jacobians
234257

235-
# Function to compute inverse of response matrix and associated
236-
# element-wise uncertainty for point-wise, atom-wise,
237-
# and full matrix inversion
238258
def chi_inverse(chi, chi_err, method="full"):
259+
"""
260+
Function to compute inverse of response matrix and associated
261+
element-wise uncertainty for point-wise, atom-wise,
262+
and full matrix inversion
263+
"""
239264

240265
n_response = len(chi)
241266

@@ -267,10 +292,15 @@ def chi_inverse(chi, chi_err, method="full"):
267292

268293
return chi_block, chi_inv, chi_inv_var, chi_inv_jacobs
269294

270-
def hubbard_hund_pointwise(
295+
def compute_u_pointwise(
271296
site_index,
272297
f_matrix, f_matrix_err,
273298
):
299+
"""
300+
Function to compute Hubbard U value using point-wise (diagonal) inversion,
301+
in addition to the associated uncertainty value
302+
- based on the study by Linscott et. al.
303+
"""
274304

275305
i = site_index
276306

@@ -281,10 +311,15 @@ def hubbard_hund_pointwise(
281311

282312
return uval, uval_err
283313

284-
def hubbard_hund_simple_two_by_two(
314+
def compute_uj_simple_two_by_two(
285315
site_index,
286316
f_matrix, f_matrix_err,
287317
):
318+
"""
319+
Function to compute Hubbard U and Hund J values using simple 2x2 formula,
320+
in addition to the associated uncertainty values
321+
- based on the study by Linscott et. al.
322+
"""
288323

289324
i = site_index
290325

@@ -299,14 +334,19 @@ def hubbard_hund_simple_two_by_two(
299334
jval = 0.25 * np.sum(jmat)
300335
jval_err = 0.25 * np.sqrt(np.sum(jmat_err**2))
301336

302-
return uval, uval_err, jmat, jmat_err
337+
return uval, uval_err, jval, jval_err
303338

304-
def hubbard_hund_scaled_two_by_two(
339+
def compute_uj_scaled_two_by_two(
305340
site_index,
306341
f_matrix, f_matrix_err,
307342
chi_matrix_scf, chi_scf_err, chi_matrix_nscf, chi_nscf_err,
308343
chi_scf_inv_jacobs, chi_nscf_inv_jacobs,
309344
):
345+
"""
346+
Function to compute Hubbard U and Hund J values using scaled 2x2 formula,
347+
in addition to the associated uncertainty values
348+
- based on the study by Linscott et. al.
349+
"""
310350

311351
i = site_index
312352

@@ -401,4 +441,4 @@ def fmat_deriv_nscf(kk,ll,k,l):
401441
# compute std
402442
jval_err = np.sqrt(jval_err)
403443

404-
return uval, uval_err, jmat, jmat_err
444+
return uval, uval_err, jval, jval_err

atomate/vasp/firetasks/parse_outputs.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -784,8 +784,8 @@ class HubbardHundLinRespToDb(FiretaskBase):
784784
785785
Required parameters:
786786
num_perturb (int): number of perturbed sites
787-
spin_polarized (bool):
788-
relax_nonmagnetic (bool):
787+
spin_polarized (bool): (please see `get_wf_hubbard_hund_linresp`)
788+
relax_nonmagnetic (bool): (please see `get_wf_hubbard_hund_linresp`)
789789
db_file (str): path to the db file that holds your tasks
790790
collection and that you want to hold the hubbard_hund_linresp
791791
collection
@@ -910,8 +910,11 @@ def run_task(self, fw_spec):
910910

911911
if use_calc:
912912
procure_response_dict(
913-
struct_final, incar_dict, outcar_dict,
914-
inv_block_dict, response_dict)
913+
struct_final, num_perturb_sites,
914+
incar_dict, outcar_dict,
915+
inv_block_dict, response_dict, perturb_dict,
916+
rkey, keys, ldaul_vals,
917+
analyzer_gs)
915918

916919
for j in range(num_perturb_sites):
917920
for qkey in ['Vup', 'Nup', 'Vdn', 'Ndn', 'Ntot', 'Mz']:
@@ -928,7 +931,7 @@ def run_task(self, fw_spec):
928931
n_response = num_perturb_sites
929932

930933
chi_matrix_nscf, chi_matrix_scf, chi_nscf_err, chi_scf_err = obtain_response_matrices(
931-
n_response, spin_polarized, response_dict)
934+
n_response, spin_polarized, response_dict, keys)
932935

933936
# Functions to help serialize numpy matrices
934937
def array_to_list(a):
@@ -972,7 +975,9 @@ def nested_copy(a):
972975
if spin_polarized:
973976
if method == "point":
974977
for i in range(num_perturb_sites):
975-
uval, uval_err = hubbard_hund_pointwise(
978+
979+
# point-wise (diagonal 2x2) formula
980+
uval, uval_err = compute_u_pointwise(
976981
i, f_matrix, f_matrix_err,
977982
)
978983

@@ -984,7 +989,7 @@ def nested_copy(a):
984989
for i in range(num_perturb_sites):
985990

986991
# first "simple 2x2" formula
987-
uval, uval_err, jmat, jmat_err = hubbard_hund_simple_two_by_two(
992+
uval, uval_err, jval, jval_err = compute_uj_simple_two_by_two(
988993
i, f_matrix, f_matrix_err,
989994
)
990995

@@ -999,7 +1004,7 @@ def nested_copy(a):
9991004

10001005
try:
10011006
# second "scaled 2x2" formula
1002-
uval, uval_err, jmat, jmat_err = hubbard_hund_scaled_two_by_two(
1007+
uval, uval_err, jval, jval_err = compute_uj_scaled_two_by_two(
10031008
i, f_matrix, f_matrix_err,
10041009
chi_matrix_scf, chi_scf_err, chi_matrix_nscf, chi_nscf_err,
10051010
chi_scf_inv_jacobs, chi_nscf_inv_jacobs,

0 commit comments

Comments
 (0)