Skip to content

Commit 53c067c

Browse files
committed
Modification for qha.py
1 parent 33e372c commit 53c067c

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

abipy/dfpt/qha.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def plot_energies(self, tstart=0, tstop=800, num=10, ax=None, **kwargs) -> Figur
188188

189189
return fig
190190

191-
def get_thermal_expansion_coeff(self, tstart=0, tstop=800, num=100, tref=None) -> Function1D:
191+
def get_thermal_expansion_coeff(self, tstart=0, tstop=800, num=100, tref=None, method=None) -> Function1D:
192192
"""
193193
Calculates the thermal expansion coefficient as a function of temperature, using
194194
finite difference on the fitted values of the volume as a function of temperature.
@@ -209,33 +209,35 @@ def get_thermal_expansion_coeff(self, tstart=0, tstop=800, num=100, tref=None) -
209209
eos_list = [ 'taylor', 'murnaghan', 'birch', 'birch_murnaghan','pourier_tarantola', 'vinet', 'antonschmidt']
210210

211211
dt = f.temp[1] - f.temp[0]
212-
if (self.eos_name in eos_list) :
213-
thermo = self.get_thermodynamic_properties(tstart=tstart, tstop=tstop, num=num)
214-
entropy = thermo.entropy.T #* abu.e_Cb * abu.Avogadro
215-
df_t = np.zeros((num,self.nvols))
216-
df_t = - entropy
217-
param = np.zeros((num,4))
218-
param2 = np.zeros((num,3))
219-
d2f_t_v = np.zeros(num)
220-
gamma = np.zeros(num)
221-
222-
for j in range (1,num-1):
223-
param[j]=np.polyfit(self.volumes,df_t[j] , 3)
224-
param2[j] = np.array([3*param[j][0],2*param[j][1],param[j][2]])
225-
226-
p = np.poly1d(param2[j])
227-
d2f_t_v[j]= p(f.min_vol[j])
228-
229-
if tref is None:
230-
alpha= - 1/f.min_vol[1:-1] *d2f_t_v[1:-1] / f.F2D[1:-1]
231-
else :
232-
alpha= - 1/f0.min_vol * d2f_t_v[1:-1] / f.F2D[1:-1]
212+
if (method=="finite_difference"):
213+
alpha = (f.min_vol[2:] - f.min_vol[:-2]) / (2 * dt) / f.min_vol[1:-1]
233214
else :
234-
if tref is None:
235-
alpha = (f.min_vol[2:] - f.min_vol[:-2]) / (2 * dt) / f.min_vol[1:-1]
215+
if (self.eos_name in eos_list) :
216+
thermo = self.get_thermodynamic_properties(tstart=tstart, tstop=tstop, num=num)
217+
entropy = thermo.entropy.T #* abu.e_Cb * abu.Avogadro
218+
df_t = np.zeros((num,self.nvols))
219+
df_t = - entropy
220+
param = np.zeros((num,4))
221+
param2 = np.zeros((num,3))
222+
d2f_t_v = np.zeros(num)
223+
gamma = np.zeros(num)
224+
225+
for j in range (1,num-1):
226+
param[j]=np.polyfit(self.volumes,df_t[j] , 3)
227+
param2[j] = np.array([3*param[j][0],2*param[j][1],param[j][2]])
228+
229+
p = np.poly1d(param2[j])
230+
d2f_t_v[j]= p(f.min_vol[j])
231+
232+
if tref is None:
233+
alpha= - 1/f.min_vol[1:-1] *d2f_t_v[1:-1] / f.F2D[1:-1]
234+
else :
235+
alpha= - 1/f0.min_vol * d2f_t_v[1:-1] / f.F2D[1:-1]
236236
else :
237-
alpha = (f.min_vol[2:] - f.min_vol[:-2]) / (2 * dt) / f0.min_vol
238-
237+
if tref is None:
238+
alpha = (f.min_vol[2:] - f.min_vol[:-2]) / (2 * dt) / f.min_vol[1:-1]
239+
else :
240+
alpha = (f.min_vol[2:] - f.min_vol[:-2]) / (2 * dt) / f0.min_vol
239241
return Function1D(f.temp[1:-1], alpha)
240242

241243
@add_fig_kwargs

abipy/dfpt/tests/test_qha.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_qha(self):
3535
qha.set_eos("murnaghan")
3636
self.assert_equal(qha.eos._eos_name, "murnaghan")
3737

38-
te = qha.get_thermal_expansion_coeff(num=4)
38+
te = qha.get_thermal_expansion_coeff(num=4,method="finite_difference")
3939
self.assert_almost_equal(te.values[1], 1.4676820862386381e-05)
4040

4141
self.assert_almost_equal(qha.get_vol_at_t(200), 41.07441539803265, decimal=4)
@@ -118,7 +118,7 @@ def test_qha3pf(self):
118118
qha.set_eos("murnaghan")
119119
self.assert_equal(qha.eos._eos_name, "murnaghan")
120120

121-
te = qha.get_thermal_expansion_coeff(num=4)
121+
te = qha.get_thermal_expansion_coeff(num=4,method="finite_difference")
122122
self.assert_almost_equal(te.values[1], 1.2773693323408941e-05)
123123

124124
self.assert_almost_equal(qha.get_vol_at_t(200), 41.10212044734946, decimal=4)
@@ -172,7 +172,7 @@ def test_qha3p(self):
172172
qha.set_eos("murnaghan")
173173
self.assert_equal(qha.eos._eos_name, "murnaghan")
174174

175-
te = qha.get_thermal_expansion_coeff(num=4)
175+
te = qha.get_thermal_expansion_coeff(num=4,method="finite_difference")
176176
self.assert_almost_equal(te.values[1], 1.2725767394824783e-05)
177177

178178
self.assert_almost_equal(qha.get_vol_at_t(200), 41.1083743159003, decimal=4)

0 commit comments

Comments
 (0)