-
Notifications
You must be signed in to change notification settings - Fork 1
/
thermodynamics.py
1129 lines (956 loc) · 37.2 KB
/
thermodynamics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
import collections
import os
import matplotlib.pyplot as plt
import numpy as np
from ase import units
from ase.dft import monkhorst_pack
from ase.phonons import Phonons
from ase.thermochemistry import CrystalThermo
from atatutils.str2emt import ATAT2EMT
import BayesianLinearRegression.myBayesianLinearRegression as BLR
import BayesianLinearRegression.myExpansionBasis as EB
from utils.finite_differences import FiniteDifference as FD
class AP2EOS(object):
"""Adapted Polynomial (order 2) Equation of state as described in [1]_
References
----------
.. [1] W. B. Holzapfel, *Equations of state for solids under strong compression*,
High Pressure Research, 16 (2), 81-126. http://dx.doi.org/10.1080/08957959808200283
"""
def __init__(self):
self.V0_mean = []
self.E0_mean = []
self.B0_mean = []
self.B1_mean = []
self.V0s = []
self.E0s = []
self.B0s = []
self.B1s = []
def clean(self):
"""Reset all model fits.
"""
self.V0_mean = []
self.E0_mean = []
self.B0_mean = []
self.B1_mean = []
self.V0s = []
self.E0s = []
self.B0s = []
self.B1s = []
def fit(self, volume, energy, beta=1e7, samples=1):
"""Fit the SJEOS to the given volume energy data.
Parameters
----------
volume : np.ndarray
Volumes to use for the fit.
energy : np.ndarray
Energies to use for the fit.
beta :
Precision of the energy data.
samples :
Number of samples from the fitted model.
"""
pass
def get_bulk_modulus(self, i=None, mean=False):
"""Return the bulk modulus.
Parameters
----------
i : int > 0
Sample for which the bulk modulus is returned.
mean : bool
Whether to return the bulk modulus for the mean model.
Returns
-------
B0s : (list of) float
Bulk modulus for the selected models.
"""
if i is not None:
if mean:
return self.B0_mean[i]
return self.B0s[i]
if mean:
return self.B0_mean
return self.B0s
def get_bulk_modulus_derivative(self, i=None, mean=False):
"""Return the bulk modulus derivative.
Parameters
----------
i : int > 0
Sample for which the bulk modulus derivative is returned.
mean : bool
Whether to return the bulk modulus derivative for the mean model.
Returns
-------
B0s : (list of) float
Bulk modulus derivative for the selected models.
"""
if i is not None:
if mean:
return self.B1_mean[i]
return self.B1s[i]
if mean:
return self.B1_mean
return self.B1s
def get_cohesive_energy(self, i=None, mean=False):
"""Return the cohesive energy.
Parameters
----------
i : int > 0
Sample for which the cohesive energy is returned.
mean : bool
Whether to return the cohesive energy for the mean model.
Returns
-------
B0s : (list of) float
Cohesive energy for the selected models.
"""
if i is not None:
if mean:
return self.E0_mean[i]
return self.E0s[i]
if mean:
return self.E0_mean
return self.E0s
def get_equilibrium_volume(self, i=None, mean=False):
"""Return the equilibrium volume.
Parameters
----------
i : int > 0
Sample for which the equilibrium volume is returned.
mean : bool
Whether to return the equilibrium volume for the mean model.
Returns
-------
B0s : (list of) float
Equilibrium volume for the selected models.
"""
if i is not None:
if mean:
return self.V0_mean[i]
return self.V0s[i]
if mean:
return self.V0_mean
return self.V0s
def plot(self, block=False):
"""Plot the last fitted SJEOS.
Parameters
----------
block : bool
Whether the plot is blocking.
"""
pass
class SJEOS(object):
"""Stabilized Jellium Equation of State as described in [1]_
Attributes
----------
sjeos_m : np.ndarray
Coefficients to transform between model and physical parameters.
V0_mean : list of float
Equilibrium volume from the mean model.
E0_mean : list of float
Cohesive energy from the mean model.
B0_mean : list of float
Bulk modulus from the mean model.
B1_mean : list of float
Pressure derivative of the bulk modulus from the mean model.
V0s : list of list of float
Samples of the equilibrium volume.
E0s : list of list of float
Samples of the cohesive energy.
B0s : list of list of float
Samples of the bulk modulus.
B1s : list of list of float
Samples of the pressure derivative of the bulk modulus.
blr : myBayesianLinearRegression
Bayesian lineal regression object for the fit.
fit_volumes : list of float
Last used volumes for the fit.
fit_energy : list of float
Last used energies for the fit.
References
----------
.. [1] A. B. Alchagirov, J. P. Perdew, J. C. Boettger, R. C. Albers, and C. Fiolhais,
*Energy and pressure versus volume: Equations of state motivated by the stabilized jellium model*.
Physical Review B, 63, 224115 (2001). http://dx.doi.org/10.1103/PhysRevB.63.224115
"""
sjeos_m = np.array([[1, 1, 1, 1],
[3, 2, 1, 0],
[18, 10, 4, 0],
[108, 50, 16, 0]
])
def __init__(self):
self.V0_mean = []
self.E0_mean = []
self.B0_mean = []
self.B1_mean = []
self.V0s = []
self.E0s = []
self.B0s = []
self.B1s = []
self.blr = BLR.BLR()
def clean(self):
"""Reset all model fits.
"""
self.V0_mean = []
self.E0_mean = []
self.B0_mean = []
self.B1_mean = []
self.V0s = []
self.E0s = []
self.B0s = []
self.B1s = []
def fit(self, volume, energy, beta=1e7, samples=1):
"""Fit the SJEOS to the given volume energy data.
Parameters
----------
volume : np.ndarray
Volumes to use for the fit.
energy : np.ndarray
Energies to use for the fit.
beta :
Precision of the energy data.
samples :
Number of samples from the fitted model.
"""
self.blr.regression(energy.reshape(len(energy), 1),
X=volume.reshape(len(volume), 1)**(1./3),
basis=EB.Basis('inverse_monomial', 4),
alpha_mode='scalar', beta=beta)
self.fit_volumes = volume
self.fit_energy = energy
properties = self._fit_to_properties(self.blr.m.reshape(4))
if properties is not None:
V0, E0, B0, B1 = properties
self.V0_mean.append(V0)
self.E0_mean.append(E0)
self.B0_mean.append(B0)
self.B1_mean.append(B1)
else:
print('ERROR fitting SJEOS:\nVolumes: {}\nEnergies: {}'.format(volume, energy))
print('Mean coefficients: {}'.format(self.blr.m.reshape(4)))
self.plot(True)
self.V0s.append([])
self.E0s.append([])
self.B0s.append([])
self.B1s.append([])
valid = 0
while valid < samples:
rc = np.random.multivariate_normal(self.blr.m.reshape(4),
self.blr.SN,
samples - valid)
for i in range(samples - valid):
rproperties = self._fit_to_properties(rc[i])
if rproperties is None:
continue
valid += 1
V0, E0, B0, B1 = rproperties
self.V0s[-1].append(V0)
self.E0s[-1].append(E0)
self.B0s[-1].append(B0)
self.B1s[-1].append(B1)
assert len(self.V0s[-1]) == samples
"""
c = rc[i, ::-1]
t = (-c[1] + np.sqrt(c[1]*c[1] - 3*c[2]*c[0]))/(3*c[0])
v0 = t**-3
self.V0s[-1][i] = v0
if t != t:
continue
d = np.array([v0**(-1), v0**(-2./3), v0**(-1./3), 1])
d = np.diag(d)
o = np.dot(np.dot(self.sjeos_m, d), c)
self.E0s[-1][i] = -o[0] # fit0(t)
self.B0s[-1][i] = o[2] / v0 / 9 # (t**5 * fit2(t) / 9)
self.B0s[-1][i] = self.B0s[-1][i] / units.kJ * 1.e24
self.B1s[-1][i] = o[3] / o[2] / 3
"""
def _fit_to_properties(self, fit_coeffs):
""" Tranforms the fit coefficients into the physical properties.
Parameters
----------
fit_coeffs : np.ndarray
Coefficients from a fit to the SJEOS.
"""
c = fit_coeffs[::-1]
t = (-c[1] + np.sqrt(c[1] * c[1] - 3 * c[2] * c[0])) / (3 * c[0])
v0 = t ** -3
V0 = v0
if t != t:
return None
d = np.array([v0 ** (-1), v0 ** (-2. / 3), v0 ** (-1. / 3), 1])
d = np.diag(d)
o = np.dot(np.dot(self.sjeos_m, d), c)
E0 = -o[0] # fit0(t)
B0 = o[2] / v0 / 9 # (t**5 * fit2(t) / 9)
B0 = B0 / units.kJ * 1.e24
B1 = o[3] / o[2] / 3
return V0, E0, B0, B1
def get_bulk_modulus(self, i=None, mean=False):
"""Return the bulk modulus.
Parameters
----------
i : int > 0
Sample for which the bulk modulus is returned.
mean : bool
Whether to return the bulk modulus for the mean model.
Returns
-------
B0s : (list of) float
Bulk modulus for the selected models.
"""
if i is not None:
if mean:
return self.B0_mean[i]
return self.B0s[i]
if mean:
return self.B0_mean
return self.B0s
def get_bulk_modulus_derivative(self, i=None, mean=False):
"""Return the bulk modulus derivative.
Parameters
----------
i : int > 0
Sample for which the bulk modulus derivative is returned.
mean : bool
Whether to return the bulk modulus derivative for the mean model.
Returns
-------
B0s : (list of) float
Bulk modulus derivative for the selected models.
"""
if i is not None:
if mean:
return self.B1_mean[i]
return self.B1s[i]
if mean:
return self.B1_mean
return self.B1s
def get_cohesive_energy(self, i=None, mean=False):
"""Return the cohesive energy.
Parameters
----------
i : int > 0
Sample for which the cohesive energy is returned.
mean : bool
Whether to return the cohesive energy for the mean model.
Returns
-------
B0s : (list of) float
Cohesive energy for the selected models.
"""
if i is not None:
if mean:
return self.E0_mean[i]
return self.E0s[i]
if mean:
return self.E0_mean
return self.E0s
def get_equilibrium_volume(self, i=None, mean=False):
"""Return the equilibrium volume.
Parameters
----------
i : int > 0
Sample for which the equilibrium volume is returned.
mean : bool
Whether to return the equilibrium volume for the mean model.
Returns
-------
B0s : (list of) float
Equilibrium volume for the selected models.
"""
if i is not None:
if mean:
return self.V0_mean[i]
return self.V0s[i]
if mean:
return self.V0_mean
return self.V0s
def plot(self, block=False):
"""Plot the last fitted SJEOS.
Parameters
----------
block : bool
Whether the plot is blocking.
"""
xrange = [np.min(self.fit_volumes), np.max(self.fit_volumes)]
xs = np.linspace(xrange[0], xrange[1], 50)
ys, stdys = self.blr.eval_regression(xs[:, np.newaxis]**(1./3))
plt.figure()
plt.fill_between(xs,
ys.ravel() - 1.96 * stdys.ravel(),
ys.ravel() + 1.96 * stdys.ravel(),
alpha=0.2)
plt.scatter(self.fit_volumes, self.fit_energy)
plt.xlabel(r'Volume [$\AA^3$]', fontsize=30)
plt.ylabel('Energy [eV]', fontsize=30)
plt.gcf().set_tight_layout(True)
plt.plot(xs, ys.ravel())
plt.show(block=block)
class ThermalProperties(object):
"""Class with methods to calculate thermal properties of a structure within the QHA
Attributes
----------
name : str
Base name for the files used for the phonon calculations.
structure : ATAT2EMT
ATAT structure file describing the primitive cell.
atoms : ase.Atoms
Atoms object with the primitive cell.
calc : ase.Calculator
Callable returning a Calculator to use for all energy and force calculations.
n_atoms : int
Number of atoms in the primitive cell.
temperature : Iterable of float
Temperatures at which to calculate temperature dependent properties.
strains : Iterable of float
Strains to apply to the atoms for volume dependent properties.
strained_thermo : list of ThermalProperties
List with classes for the thermal properties of strained versions of the atoms object.
sjeos : SJEOS
Class to fit the equation of state at constant temperature.
base_dir : str
Base directory for the calculations. (WARNING: not fully implemented)
verbosity : int >= 0
Verbosity level.
do_plotting : bool
Determines if the plottings are activated.
supercell_size : tuple of 3 int
Size of the supercell to do the phonon calculations.
thermo : ase.CrystalThermo
Class to delegate the calculation of some thermodynamic properties.
phonons : ase.Phonons
Class to perform phonon calculations using the supercell approach.
phonon_kpts_mp : (N, 3) np.ndarray
Monkhorst-Pack k-point grid.
phonon_energy_mp : (N,) np.ndarray
Energies of the corresponding MP k-points.
phonon_energy : np.ndarray
Energies to calculate the Phonon density of states.
phonon_dos : np.ndarray
Phonon density of states at given energies.
Parameters
----------
atoms :
calc :
supercell_size :
atat_structure :
plot :
verbosity :
name :
"""
def __init__(self, atoms=None, calc=None, supercell_size=5,
atat_structure=None, plot=False, verbosity=0,
name='thermo'):
# relaxed structure
self.name = name
self.structure = None
self.atoms = None
self.calc = None
self.n_atoms = 0
self.temperature = None
self.strains = None
self.strained_thermo = []
if atoms is not None and atat_structure is not None:
print('ERROR: only atoms OR atat_structure can be specified')
return
if atoms is not None:
self.atoms = atoms
self.n_atoms = len(self.atoms)
if self.atoms.calc is None:
assert calc is not None
self.atoms.set_calculator(calc())
self.calc = calc
else:
self.calc = atoms.calc
elif atat_structure is not None:
assert calc is not None
self.calc = calc
self.structure = ATAT2EMT(atat_structure, calc(), to_niggli=True, verbosity=verbosity)
self.structure.atoms.wrap()
self.atoms = self.structure.atoms
self.n_atoms = len(self.atoms)
# isgn = spglib.get_symmetry_dataset(self.atoms, symprec=1e-3)['number']
# self.symmetry = el.crystal_system(isgn)
self.sjeos = SJEOS()
self.base_dir = os.getcwd()
self.verbosity = verbosity
self.do_plotting = plot
if isinstance(supercell_size, int):
self.supercell_size = (supercell_size, supercell_size, supercell_size)
else:
assert len(supercell_size) == 3
self.supercell_size = supercell_size
self.get_phonons()
self.thermo = CrystalThermo(phonon_energies=self.phonon_energy,
phonon_DOS=self.phonon_dos,
potentialenergy=self.atoms.get_potential_energy(),
formula_units=self.n_atoms)
def set_temperature(self, temperature, save_at='.'):
"""Set the temperature grid.
Parameters
----------
temperature : iterable of float
Iterable containing the temperatures at which to calculate the properties.
save_at : string
Path (relative or absolute) in which to store the value.
"""
if save_at is not None:
if not os.path.exists(save_at):
os.makedirs(save_at)
save_name = os.path.join(save_at, 'T.dat')
np.savetxt(save_name, temperature)
self.temperature = temperature
def get_phonons(self, kpts=(50, 50, 50), npts=5000):
"""Calculate the phonon spectrum and DOS.
Parameters
----------
kpts : tuple
Number of points in each directions of the k-space grid.
npts : int
Number of energy points to calculate the DOS at.
"""
self.phonons = Phonons(self.atoms, self.calc(),
supercell=self.supercell_size, delta=0.05,
name=self.name)
self.phonons.run()
# Read forces and assemble the dynamical matrix
self.phonons.read(acoustic=True)
self.phonon_kpts_mp = monkhorst_pack(kpts)
self.phonon_energy_mp = self.phonons.band_structure(self.phonon_kpts_mp)
self.phonon_energy, self.phonon_dos = \
self.phonons.dos(kpts=kpts, npts=npts, delta=5e-4)
def get_volume_phonons(self, nvolumes=5, max_strain=0.02):
"""Calculate the volume dependent phonons.
Parameters
----------
nvolumes : int > 0
Number of volumes to calculate the phonon spectrum.
max_strain : float > 0
Maximum (isotropic) strain used to deform equilibrium volume.
"""
strains = np.linspace(-max_strain, max_strain, nvolumes)
load = False
if self.strains is None:
self.strains = strains
else:
if not (strains == self.strains).all():
self.strains = strains
self.strained_thermo = []
else:
load = True
strain_matrices = [np.eye(3) * (1 + s) for s in self.strains]
atoms = self.atoms
cell = atoms.cell
for i, s in enumerate(strain_matrices):
satoms = atoms.copy()
satoms.set_cell(np.dot(cell, s.T), scale_atoms=True)
if load:
pass
else:
satoms.set_calculator(None)
sthermo = ThermalProperties(satoms, self.calc, name='thermo_{:.2f}'.format(self.strains[i]))
self.strained_thermo.append(sthermo)
def get_volume_energy(self, temperature=None, nvolumes=5, max_strain=0.02):
"""Return the volume dependent (Helmholtz) energy.
Parameters
----------
temperature : float > 0
Temeprature at which the volume-energy curve is calculated.
nvolumes : int > 0
Number of volumes to calculate the energy at.
max_strain : float > 0
Maximum (isotropic) strain used to deform equilibrium volume.
save_at : string
Path (relative or absolute) in which to store the value.
Returns
-------
volume : list of double
Volumes at which the entropy was calculated.
energy : list of double
Helmholtz energy for each of the volumes.
"""
if temperature is None and self.temperature is None:
print('ERROR. You nee to specify a temperature for the calculations.')
return
elif temperature is None:
temperature = self.temperature
if isinstance(temperature, collections.Iterable):
volume_energy = [self.get_volume_energy(T, nvolumes, max_strain) for T in temperature]
return volume_energy
self.get_volume_phonons(nvolumes, max_strain)
energy = []
volume = []
for sthermo in self.strained_thermo:
energy.append(sthermo.get_helmholtz_energy(temperature, save_at=None))
volume.append(sthermo.atoms.get_volume())
return volume, energy
def get_volume_entropy(self, temperature=None, nvolumes=5, max_strain=0.02):
"""Return the volume dependent entropy.
Parameters
----------
temperature : float > 0
Temeprature at which the volume-entropy curve is calculated.
nvolumes : int > 0
Number of volumes to calculate the entropy at.
max_strain : float > 0
Maximum (isotropic) strain used to deform equilibrium volume.
save_at : string
Path (relative or absolute) in which to store the value.
Returns
-------
volume : list of double
Volumes at which the entropy was calculated.
entropy : list of double
Entropy for each of the volumes.
"""
if temperature is None and self.temperature is None:
print('ERROR. You nee to specify a temperature for the calculations.')
return
elif temperature is None:
temperature = self.temperature
if isinstance(temperature, collections.Iterable):
volume_entropy = [self.get_volume_entropy(T, nvolumes, max_strain) for T in temperature]
return volume_entropy
self.get_volume_phonons(nvolumes, max_strain)
entropy = []
volume = []
for sthermo in self.strained_thermo:
entropy.append(sthermo.get_entropy(temperature, save_at=None))
volume.append(sthermo.atoms.get_volume())
return volume, entropy
def get_entropy(self, temperature=None, save_at='.'):
"""Return entropy per atom in eV / atom.
Parameters
----------
temperature : float > 0
Temeprature at which the Helmholtz energy is calculated.
save_at : string
Path (relative or absolute) in which to store the value.
Returns
-------
entropy : float
Entropy in eV / atom
Notes
-----
To convert to SI units, divide by units.J.
At the moment only vibrational entropy is included. Electronic entropy can
be included if the calculator provides the electronic DOS.
"""
if temperature is None and self.temperature is None:
print('ERROR. You nee to specify a temperature for the calculations.')
return
elif temperature is None:
temperature = self.temperature
if save_at is not None:
if not os.path.exists(save_at):
os.makedirs(save_at)
save_name = os.path.join(save_at, 'S.dat')
if isinstance(temperature, collections.Iterable):
vib_entropy = [self.get_entropy(T, save_at=None) for T in temperature]
if save_at is not None:
np.savetxt(save_name, vib_entropy)
return np.array(vib_entropy)
if temperature == 0.:
if save_at is not None:
np.savetxt(save_name, np.asarray([0.]))
return 0.
vib_entropy = self.thermo.get_entropy(temperature, self.verbosity)
if save_at is not None:
np.savetxt(save_name, vib_entropy)
return vib_entropy
def get_helmholtz_energy(self, temperature=None, save_at='.'):
"""Return Helmholtz energy per atom in eV / atom.
Parameters
----------
temperature : float > 0
Temeprature at which the Helmholtz energy is calculated.
save_at : string
Path (relative or absolute) in which to store the value.
Returns
-------
helmholtz_energy : float
Helmholtz energy in eV / atom
Notes
-----
To convert to SI units, divide by units.J.
"""
if temperature is None and self.temperature is None:
print('ERROR. You nee to specify a temperature for the calculations.')
return
elif temperature is None:
temperature = self.temperature
if save_at is not None:
if not os.path.exists(save_at):
os.makedirs(save_at)
save_name = os.path.join(save_at, 'F.dat')
if isinstance(temperature, collections.Iterable):
helmholtz_energy = [self.get_helmholtz_energy(T, save_at=None) for T in temperature]
if save_at is not None:
np.savetxt(save_name, helmholtz_energy)
return np.array(helmholtz_energy)
if temperature == 0.:
helmholtz_energy = self.get_zero_point_energy() + self.thermo.potentialenergy
if save_at is not None:
np.savetxt(save_name, helmholtz_energy)
return helmholtz_energy
helmholtz_energy = self.thermo.get_helmholtz_energy(temperature, self.verbosity)
if save_at is not None:
np.savetxt(save_name, helmholtz_energy)
return helmholtz_energy
def get_internal_energy(self, temperature=None, save_at='.'):
"""Return internal energy per atom in eV / atom.
Parameters
----------
temperature : float > 0
Temeprature at which the internal energy is calculated.
save_at : string
Path (relative or absolute) in which to store the value.
Returns
-------
internal_energy : float
Internal energy in eV / atom
Notes
-----
To convert to SI units, divide by units.J.
"""
if temperature is None and self.temperature is None:
print('ERROR. You nee to specify a temperature for the calculations.')
return
elif temperature is None:
temperature = self.temperature
if save_at is not None:
if not os.path.exists(save_at):
os.makedirs(save_at)
save_name = os.path.join(save_at, 'U.dat')
if isinstance(temperature, collections.Iterable):
internal_energy = [self.get_internal_energy(T, save_at=None) for T in temperature]
if save_at is not None:
np.savetxt(save_name, internal_energy)
return np.array(internal_energy)
if temperature == 0.:
internal_energy = self.get_zero_point_energy() + self.thermo.potentialenergy
if save_at is not None:
np.savetxt(save_name, internal_energy)
return internal_energy
internal_energy = self.thermo.get_internal_energy(temperature, self.verbosity)
if save_at is not None:
np.savetxt(save_name, internal_energy)
return internal_energy
def get_zero_point_energy(self):
"""Return the Zero Point Energy in eV / atom.
Returns
-------
zpe: float
Zero point energy in eV / atom.
"""
zpe_list = self.phonon_energy / 2.
zpe = np.trapz(zpe_list * self.phonon_dos, self.phonon_energy) / self.n_atoms
return zpe
def get_specific_heat(self, temperature=None, save_at='.'):
"""Return heat capacity per atom in eV / atom K.
Parameters
----------
temperature : float > 0
Temeprature at which the specific heat is calculated.
save_at : string
Path (relative or absolute) in which to store the value.
Returns
-------
C_V : float
Specific heat in eV / atom K
Notes
-----
To convert to SI units, multiply by (units.mol / units.J).
"""
if temperature is None and self.temperature is None:
print('ERROR. You nee to specify a temperature for the calculations.')
return
elif temperature is None:
temperature = self.temperature
if save_at is not None:
if not os.path.exists(save_at):
os.makedirs(save_at)
save_name = os.path.join(save_at, 'Cv.dat')
if isinstance(temperature, collections.Iterable):
C_V = [self.get_specific_heat(T, save_at=None) for T in temperature]
if save_at is not None:
np.savetxt(save_name, C_V)
return np.array(C_V)
if temperature == 0.:
if save_at is not None:
np.savetxt(save_name, np.asarray([0.]))
return 0.
if self.phonon_energy[0] == 0.:
self.phonon_energy = np.delete(self.phonon_energy, 0)
self.phonon_dos = np.delete(self.phonon_dos, 0)
i2kT = 1. / (2. * units.kB * temperature)
arg = self.phonon_energy * i2kT
C_v = units.kB * arg**2 / np.sinh(arg)**2
C_V = np.trapz(C_v * self.phonon_dos, self.phonon_energy) / self.n_atoms
if save_at is not None:
np.savetxt(save_name, np.asarray([C_V]))
return C_V
def get_thermal_expansion(self, temperature=None, exp_norm_temp=None,
nvolumes=5, max_strain=0.02,
ntemperatures=5, delta_t=1.,
save_at='.'):
"""Return the isotropic volumetric thermal expansion in K^-1.
Parameters
----------
temperature : float > 0
Temeprature at which the expansion coefficient is calculated.
exp_norm_temp : float > 0
Temperature for the normalization of the thermal expansion (usually to compare with experiment).
nvolumes : int > 0
Number of volumes to fit the equation of state to extract equilibrium volumes.
max_strain : float > 0
Maximum strain used to fit the equation of state to extract equilibrium volumes.
ntemperatures : int > 0
Number of temperatures to approximate the temperature derivative of the volume.
delta_t : float >0
Temperature step to approximate the temperature derivative of the volume.
save_at : string
Path (relative or absolute) in which to store the value.
Returns
-------
: double
Isotropic volumetric thermal expansion in K^-1
"""
if temperature is None and self.temperature is None:
print('ERROR. You nee to specify a temperature for the calculations.')
return
elif temperature is None:
temperature = self.temperature
if save_at is not None:
if not os.path.exists(save_at):
os.makedirs(save_at)
save_name = os.path.join(save_at, 'thermal_expansion.dat')
if isinstance(temperature, collections.Iterable):
alpha_v = [self.get_thermal_expansion(T, exp_norm_temp,
nvolumes, max_strain,
ntemperatures, delta_t,
save_at=None)
for T in temperature]
if save_at is not None:
np.savetxt(save_name, alpha_v)
return np.array(alpha_v)
max_delta_t = (ntemperatures - 1) * delta_t
if temperature - max_delta_t / 2. > 0.:
temperatures = np.linspace(temperature - max_delta_t / 2., temperature + max_delta_t / 2., ntemperatures)
t0 = (ntemperatures - 1) / 2
mode = 'c'
else:
ntemperatures = (ntemperatures + 2) / 2
temperatures = np.linspace(temperature, temperature + max_delta_t / 2., ntemperatures)