Skip to content

Commit 973ccff

Browse files
authored
Merge pull request #65 from MineralsCloud/pull-64-restyled
Restyle feat: Add entropy output
2 parents ed72d17 + cb49509 commit 973ccff

File tree

2 files changed

+73
-34
lines changed

2 files changed

+73
-34
lines changed

qha/calculator.py

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from qha.single_configuration import free_energy
2727
from qha.thermodynamics import *
2828
from qha.type_aliases import Vector
29-
from qha.unit_conversion import gpa_to_ry_b3, ry_b3_to_gpa, b3_to_a3, ry_to_j_mol, ry_to_ev
29+
from qha.unit_conversion import gpa_to_ry_b3, ry_b3_to_gpa, b3_to_a3, ry_to_j_mol, ry_to_ev, ry_to_j
3030
from qha.v2p import v2p
3131

3232
# ===================== What can be exported? =====================
@@ -48,7 +48,8 @@ def __init__(self, user_settings: Dict[str, Any]):
4848
try:
4949
runtime_settings.update({key: user_settings[key]})
5050
except KeyError:
51-
continue # If a key is not set in user settings, use the default.
51+
# If a key is not set in user settings, use the default.
52+
continue
5253

5354
self._settings = runtime_settings
5455

@@ -106,12 +107,15 @@ def v_ratio(self) -> Optional[float]:
106107

107108
def read_input(self):
108109
try:
109-
formula_unit_number, volumes, static_energies, frequencies, q_weights = read_input(self.settings['input'])
110+
formula_unit_number, volumes, static_energies, frequencies, q_weights = read_input(
111+
self.settings['input'])
110112
except KeyError:
111-
raise KeyError("The 'input' option must be given in your settings!")
113+
raise KeyError(
114+
"The 'input' option must be given in your settings!")
112115

113116
if not qha.tools.is_monotonic_decreasing(volumes):
114-
raise RuntimeError("Check the input file to make sure the volume decreases!")
117+
raise RuntimeError(
118+
"Check the input file to make sure the volume decreases!")
115119

116120
self._formula_unit_number: int = formula_unit_number
117121
self._volumes = volumes
@@ -127,7 +131,8 @@ def where_negative_frequencies(self) -> Optional[Vector]:
127131
:return:
128132
"""
129133
if self._frequencies is None:
130-
print("Please invoke ``read_input`` method first!") # ``None`` is returned
134+
# ``None`` is returned
135+
print("Please invoke ``read_input`` method first!")
131136
else:
132137
_ = np.transpose(np.where(self._frequencies < 0))
133138
if _.size == 0:
@@ -156,20 +161,23 @@ def refine_grid(self):
156161
try:
157162
p_min, p_min_modifier, ntv, order = d['P_MIN'], d['p_min_modifier'], d['NTV'], d['order']
158163
except KeyError:
159-
raise KeyError("All the 'P_MIN', 'p_min_modifier', 'NTV', 'order' options must be given in your settings!")
164+
raise KeyError(
165+
"All the 'P_MIN', 'p_min_modifier', 'NTV', 'order' options must be given in your settings!")
160166

161167
r = FinerGrid(p_min - p_min_modifier, ntv, order=order)
162168

163169
if 'volume_ratio' in d:
164170
self._finer_volumes_bohr3, self._f_tv_ry, self._v_ratio = r.refine_grid(self.volumes, self.vib_ry,
165171
ratio=d['volume_ratio'])
166172
else:
167-
self._finer_volumes_bohr3, self._f_tv_ry, self._v_ratio = r.refine_grid(self.volumes, self.vib_ry)
173+
self._finer_volumes_bohr3, self._f_tv_ry, self._v_ratio = r.refine_grid(
174+
self.volumes, self.vib_ry)
168175

169176
@LazyProperty
170177
def vib_ry(self):
171178
# We grep all the arguments once since they are being invoked for thousands of times, and will be an overhead.
172-
args = self.q_weights, self.static_energies, self.frequencies, self.settings['static_only']
179+
args = self.q_weights, self.static_energies, self.frequencies, self.settings[
180+
'static_only']
173181

174182
mat = np.empty((self.temperature_array.size, self.volumes.size))
175183
for i, t in enumerate(self.temperature_array):
@@ -218,7 +226,8 @@ def desired_pressure_status(self) -> None:
218226
self.p_tv_gpa[:, 0].max(), self.p_tv_gpa[:, -1].min()))
219227

220228
if self.p_tv_gpa[:, -1].min() < self.desired_pressures_gpa.max():
221-
ntv_max = int((self.p_tv_gpa[:, -1].min() - self.desired_pressures_gpa.min()) / d['DELTA_P'])
229+
ntv_max = int(
230+
(self.p_tv_gpa[:, -1].min() - self.desired_pressures_gpa.min()) / d['DELTA_P'])
222231

223232
save_to_output(d['qha_output'], textwrap.dedent("""\
224233
!!!ATTENTION!!!
@@ -228,12 +237,17 @@ def desired_pressure_status(self) -> None:
228237
Please reduce the NTV accordingly, for example, try to set NTV < {:4d}.
229238
""".format(ntv_max)))
230239

231-
raise ValueError("DESIRED PRESSURE is too high (NTV is too large), qha results might not be right!")
240+
raise ValueError(
241+
"DESIRED PRESSURE is too high (NTV is too large), qha results might not be right!")
232242

233243
@LazyProperty
234244
def p_tv_au(self):
235245
return pressure(self.finer_volumes_bohr3, self.f_tv_ry)
236246

247+
@LazyProperty
248+
def s_tv_j(self):
249+
return ry_to_j(entropy(self.temperature_array, self.f_tv_ry))
250+
237251
@LazyProperty
238252
def f_tv_ev(self):
239253
return ry_to_ev(self.f_tv_ry)
@@ -373,12 +387,15 @@ def read_input(self):
373387
q_weights = []
374388

375389
for inp in input_data_files:
376-
nm_tmp, volumes_tmp, static_energies_tmp, freq_tmp, weights_tmp = read_input(inp)
390+
nm_tmp, volumes_tmp, static_energies_tmp, freq_tmp, weights_tmp = read_input(
391+
inp)
377392

378393
if not qha.tools.is_monotonic_decreasing(volumes_tmp):
379394
# TODO: Clean this sentence
380-
save_to_output(self.settings['qha_output'], "Check the input file to make sure the volume decreases")
381-
raise ValueError("Check the input file to make sure the volumes are monotonic decreasing!")
395+
save_to_output(
396+
self.settings['qha_output'], "Check the input file to make sure the volume decreases")
397+
raise ValueError(
398+
"Check the input file to make sure the volumes are monotonic decreasing!")
382399

383400
formula_unit_numbers.append(nm_tmp)
384401
volumes.append(volumes_tmp)
@@ -393,12 +410,15 @@ def read_input(self):
393410
q_weights = np.array(q_weights)
394411

395412
if not len(set(formula_unit_numbers)) == 1:
396-
raise RuntimeError("All the formula unit number in all inputs should be the same!")
413+
raise RuntimeError(
414+
"All the formula unit number in all inputs should be the same!")
397415

398416
if len(volumes.shape) == 1:
399-
raise RuntimeError("All configurations should have same number of volumes!")
417+
raise RuntimeError(
418+
"All configurations should have same number of volumes!")
400419

401-
self._formula_unit_number = formula_unit_numbers[0] # Choose any of them since they are all the same
420+
# Choose any of them since they are all the same
421+
self._formula_unit_number = formula_unit_numbers[0]
402422
self._volumes = volumes
403423
self._static_energies = static_energies
404424
self._frequencies = frequencies
@@ -408,11 +428,12 @@ def read_input(self):
408428
def vib_ry(self):
409429
# We grep all the arguments once since they are being invoked for thousands of times, and will be an overhead.
410430
args = self.degeneracies, self.q_weights, self.static_energies, self._volumes, self.frequencies, \
411-
self.settings['static_only']
431+
self.settings['static_only']
412432

413433
mat = np.empty((self.temperature_array.size, self._volumes.shape[1]))
414434
for i, t in enumerate(self.temperature_array):
415-
mat[i] = different_phonon_dos.PartitionFunction(t, *(arg for arg in args)).get_free_energies()
435+
mat[i] = different_phonon_dos.PartitionFunction(
436+
t, *(arg for arg in args)).get_free_energies()
416437

417438
return mat
418439

@@ -424,9 +445,10 @@ def __init__(self, user_settings):
424445
@LazyProperty
425446
def vib_ry(self):
426447
args = self.degeneracies, self.q_weights[0], self.static_energies, self._volumes, self.frequencies[0], \
427-
self.settings['static_only'], self.settings['order']
448+
self.settings['static_only'], self.settings['order']
428449
mat = np.empty((self.temperature_array.size, self._volumes.shape[1]))
429450

430451
for i, t in enumerate(self.temperature_array):
431-
mat[i] = same_phonon_dos.FreeEnergy(t, *(arg for arg in args)).get_free_energies()
452+
mat[i] = same_phonon_dos.FreeEnergy(
453+
t, *(arg for arg in args)).get_free_energies()
432454
return mat

qha/cli/runner.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def run(self, namespace):
4949
if not os.path.exists(user_settings['output_directory']):
5050
os.makedirs(user_settings['output_directory'])
5151

52-
user_settings.update({'qha_output': os.path.join(user_settings['output_directory'], 'output.txt')})
52+
user_settings.update({'qha_output': os.path.join(
53+
user_settings['output_directory'], 'output.txt')})
5354

5455
try:
5556
os.remove(user_settings['qha_output'])
@@ -64,10 +65,12 @@ def run(self, namespace):
6465
print("You have single-configuration calculation assumed.")
6566
elif calculation_type == 'same phonon dos':
6667
calc = SamePhDOSCalculator(user_settings)
67-
print("You have multi-configuration calculation with the same phonon DOS assumed.")
68+
print(
69+
"You have multi-configuration calculation with the same phonon DOS assumed.")
6870
elif calculation_type == 'different phonon dos':
6971
calc = DifferentPhDOSCalculator(user_settings)
70-
print("You have multi-configuration calculation with different phonon DOS assumed.")
72+
print(
73+
"You have multi-configuration calculation with different phonon DOS assumed.")
7174
else:
7275
raise ValueError("The 'calculation' in your settings in not recognized! It must be one of:"
7376
"'single', 'same phonon dos', 'different phonon dos'!")
@@ -81,7 +84,8 @@ def run(self, namespace):
8184

8285
print("Caution: If negative frequencies found, they are currently treated as 0!")
8386
tmp = calc.where_negative_frequencies
84-
if tmp is not None and not (tmp.T[-1].max() <= 2): # Don't delete this parenthesis!
87+
# Don't delete this parenthesis!
88+
if tmp is not None and not (tmp.T[-1].max() <= 2):
8589
if calc.frequencies.ndim == 4: # Multiple configuration
8690
for indices in tmp:
8791
print(
@@ -122,20 +126,29 @@ def run(self, namespace):
122126
}
123127

124128
file_ftv_fitted = results_folder / 'f_tv_fitted_ev_ang3.txt'
125-
save_x_tv(calc.f_tv_ev, temperature_array, calc.finer_volumes_ang3, temperature_sample, file_ftv_fitted)
129+
save_x_tv(calc.f_tv_ev, temperature_array,
130+
calc.finer_volumes_ang3, temperature_sample, file_ftv_fitted)
126131

127132
file_ftv_non_fitted = results_folder / 'f_tv_nonfitted_ev_ang3.txt'
128-
save_x_tv(calc.vib_ev, temperature_array, calc.volumes_ang3, temperature_sample, file_ftv_non_fitted)
133+
save_x_tv(calc.vib_ev, temperature_array, calc.volumes_ang3,
134+
temperature_sample, file_ftv_non_fitted)
129135

130136
file_ptv_gpa = results_folder / 'p_tv_gpa.txt'
131-
save_x_tv(calc.p_tv_gpa, temperature_array, calc.finer_volumes_ang3, temperature_sample, file_ptv_gpa)
137+
save_x_tv(calc.p_tv_gpa, temperature_array,
138+
calc.finer_volumes_ang3, temperature_sample, file_ptv_gpa)
139+
140+
file_stv_j = results_folder / 's_tv_j.txt'
141+
save_x_tv(calc.s_tv_j, temperature_array,
142+
calc.finer_volumes_ang3, temperature_sample, file_stv_j)
132143

133144
for idx in calc.settings['thermodynamic_properties']:
134145
if idx in ['F', 'G', 'H', 'U']:
135-
attr_name = calculation_option[idx] + '_' + calc.settings['energy_unit']
146+
attr_name = calculation_option[idx] + \
147+
'_' + calc.settings['energy_unit']
136148
file_name = attr_name + '.txt'
137149
file_dir = results_folder / file_name
138-
save_x_tp(getattr(calc, attr_name), temperature_array, desired_pressures_gpa, p_sample_gpa, file_dir)
150+
save_x_tp(getattr(calc, attr_name), temperature_array,
151+
desired_pressures_gpa, p_sample_gpa, file_dir)
139152

140153
if idx == 'V':
141154
v_bohr3 = calculation_option[idx] + '_' + 'bohr3'
@@ -145,15 +158,19 @@ def run(self, namespace):
145158
file_name_ang3 = v_ang3 + '.txt'
146159
file_dir_ang3 = results_folder / file_name_ang3
147160

148-
save_x_tp(getattr(calc, v_bohr3), temperature_array, desired_pressures_gpa, p_sample_gpa, file_dir_au)
149-
save_x_tp(getattr(calc, v_ang3), temperature_array, desired_pressures_gpa, p_sample_gpa, file_dir_ang3)
161+
save_x_tp(getattr(calc, v_bohr3), temperature_array,
162+
desired_pressures_gpa, p_sample_gpa, file_dir_au)
163+
save_x_tp(getattr(calc, v_ang3), temperature_array,
164+
desired_pressures_gpa, p_sample_gpa, file_dir_ang3)
150165

151166
if idx in ['Cv', 'Cp', 'Bt', 'Btp', 'Bs', 'alpha', 'gamma']:
152167
attr_name = calculation_option[idx]
153168
file_name = attr_name + '.txt'
154169
file_dir = results_folder / file_name
155-
save_x_tp(getattr(calc, attr_name), temperature_array, desired_pressures_gpa, p_sample_gpa, file_dir)
170+
save_x_tp(getattr(calc, attr_name), temperature_array,
171+
desired_pressures_gpa, p_sample_gpa, file_dir)
156172

157173
end_time_total = time.time()
158174
time_elapsed = end_time_total - start_time_total
159-
save_to_output(user_settings['qha_output'], make_ending_string(time_elapsed))
175+
save_to_output(user_settings['qha_output'],
176+
make_ending_string(time_elapsed))

0 commit comments

Comments
 (0)