Writing docstrings following pydocstring

This commit is contained in:
Atsushi Togo 2021-10-25 14:18:37 +09:00
parent 39765d0ae4
commit 5bc2d0513e
7 changed files with 37 additions and 157 deletions

View File

@ -33,7 +33,7 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
from typing import List, Union from typing import Union
import numpy as np import numpy as np
@ -69,17 +69,22 @@ class Modulation:
self._derivative_order = derivative_order self._derivative_order = derivative_order
self._factor = factor self._factor = factor
self._u = []
self._eigvecs = []
self._eigvals = []
self._supercell = None
dim = self._get_dimension_3x3() dim = self._get_dimension_3x3()
self._supercell = get_supercell(self._primitive, dim) self._supercell = get_supercell(self._primitive, dim)
complex_dtype = "c%d" % (np.dtype("double").itemsize * 2)
self._u = np.zeros(
(len(self._phonon_modes), len(self._supercell), 3),
dtype=complex_dtype,
order="C",
)
self._eigvals = np.zeros(len(self._phonon_modes), dtype="double")
self._eigvecs = np.zeros(
(len(self._phonon_modes), len(self._primitive) * 3), dtype=complex_dtype
)
def run(self): def run(self):
"""Calculate modulations.""" """Calculate modulations."""
for ph_mode in self._phonon_modes: for i, ph_mode in enumerate(self._phonon_modes):
q, band_index, amplitude, argument = ph_mode q, band_index, amplitude, argument = ph_mode
eigvals, eigvecs = get_eigenvectors( eigvals, eigvecs = get_eigenvectors(
q, q,
@ -90,9 +95,9 @@ class Modulation:
nac_q_direction=self._nac_q_direction, nac_q_direction=self._nac_q_direction,
) )
u = self._get_displacements(eigvecs[:, band_index], q, amplitude, argument) u = self._get_displacements(eigvecs[:, band_index], q, amplitude, argument)
self._u.append(u) self._u[i] = u
self._eigvecs.append(eigvecs[:, band_index]) self._eigvecs[i] = eigvecs[:, band_index]
self._eigvals.append(eigvals[band_index]) self._eigvals[i] = eigvals[band_index]
def get_modulated_supercells(self): def get_modulated_supercells(self):
"""Return modulations.""" """Return modulations."""

View File

@ -233,9 +233,9 @@ class DynamicStructureFactor:
freq_min=self._fmin, freq_min=self._fmin,
freq_max=self._fmax, freq_max=self._fmax,
) )
td.set_temperatures([self._T]) td.temperatures = [self._T]
td.run() td.run()
return td.get_thermal_displacements() return td.temperatures, td.thermal_displacements
def _phonon_structure_factor(self, Q_cart, G, DW, freq, eigvec): def _phonon_structure_factor(self, Q_cart, G, DW, freq, eigvec):
symbols = self._primitive.get_chemical_symbols() symbols = self._primitive.get_chemical_symbols()

View File

@ -247,7 +247,9 @@ def helper_methods():
class HelperMethods: class HelperMethods:
@classmethod @classmethod
def compare_cells_with_order(cls, cell, cell_ref, symprec=1e-5): def compare_cells_with_order(
cls, cell: PhonopyAtoms, cell_ref: PhonopyAtoms, symprec=1e-5
):
"""Compare two cells with the same orders of positions.""" """Compare two cells with the same orders of positions."""
np.testing.assert_allclose(cell.cell, cell_ref.cell, atol=symprec) np.testing.assert_allclose(cell.cell, cell_ref.cell, atol=symprec)
cls.compare_positions_with_order( cls.compare_positions_with_order(
@ -270,7 +272,9 @@ def helper_methods():
assert (dist < symprec).all() assert (dist < symprec).all()
@classmethod @classmethod
def compare_cells(cls, cell, cell_ref, symprec=1e-5): def compare_cells(
cls, cell: PhonopyAtoms, cell_ref: PhonopyAtoms, symprec=1e-5
):
"""Compare two cells where position orders can be different.""" """Compare two cells where position orders can be different."""
np.testing.assert_allclose(cell.cell, cell_ref.cell, atol=symprec) np.testing.assert_allclose(cell.cell, cell_ref.cell, atol=symprec)

View File

@ -91,7 +91,7 @@ def _assert(ph: Phonopy, ref_vals, show=False):
dynmat = ph.dynamical_matrix dynmat = ph.dynamical_matrix
ddynmat = DerivativeOfDynamicalMatrix(dynmat) ddynmat = DerivativeOfDynamicalMatrix(dynmat)
ddynmat.run([0, 0.1, 0.1]) ddynmat.run([0, 0.1, 0.1])
ddm = ddynmat.get_derivative_of_dynamical_matrix() ddm = ddynmat.d_dynamical_matrix
condition = np.abs(ddm) > 1e-8 condition = np.abs(ddm) > 1e-8
vals = np.extract(condition, ddm).real vals = np.extract(condition, ddm).real
if show: if show:

View File

@ -1,6 +1,8 @@
"""Tests for routines in force_constants.py."""
import numpy as np import numpy as np
import pytest import pytest
from phonopy import Phonopy
from phonopy.harmonic.force_constants import cutoff_force_constants from phonopy.harmonic.force_constants import cutoff_force_constants
from phonopy.structure.cells import get_primitive from phonopy.structure.cells import get_primitive
@ -41,26 +43,30 @@ fc_1_10_compact_fcsym_ref = [
] ]
def test_fc(ph_nacl): def test_fc(ph_nacl: Phonopy):
"""Test of force constants calculation with fcsym by NaCl."""
fc_1_10 = ph_nacl.force_constants[1, 10] fc_1_10 = ph_nacl.force_constants[1, 10]
# print("".join(["%f, " % v for v in fc_1_10.ravel()])) # print("".join(["%f, " % v for v in fc_1_10.ravel()]))
np.testing.assert_allclose(fc_1_10.ravel(), fc_1_10_ref, atol=1e-5) np.testing.assert_allclose(fc_1_10.ravel(), fc_1_10_ref, atol=1e-5)
def test_fc_nofcsym(ph_nacl_nofcsym): def test_fc_nofcsym(ph_nacl_nofcsym: Phonopy):
"""Test of force constants calculation without fcsym by NaCl."""
fc_1_10 = ph_nacl_nofcsym.force_constants[1, 10] fc_1_10 = ph_nacl_nofcsym.force_constants[1, 10]
# print("".join(["%f, " % v for v in fc_1_10.ravel()])) # print("".join(["%f, " % v for v in fc_1_10.ravel()]))
np.testing.assert_allclose(fc_1_10.ravel(), fc_1_10_nofcsym_ref, atol=1e-5) np.testing.assert_allclose(fc_1_10.ravel(), fc_1_10_nofcsym_ref, atol=1e-5)
def test_fc_compact_fcsym(ph_nacl_compact_fcsym): def test_fc_compact_fcsym(ph_nacl_compact_fcsym: Phonopy):
"""Test of force constants calculation in compact format with fcsym by NaCl."""
fc_1_10 = ph_nacl_compact_fcsym.force_constants[1, 10] fc_1_10 = ph_nacl_compact_fcsym.force_constants[1, 10]
# print("".join(["%f, " % v for v in fc_1_10.ravel()])) # print("".join(["%f, " % v for v in fc_1_10.ravel()]))
np.testing.assert_allclose(fc_1_10.ravel(), fc_1_10_compact_fcsym_ref, atol=1e-5) np.testing.assert_allclose(fc_1_10.ravel(), fc_1_10_compact_fcsym_ref, atol=1e-5)
@pytest.mark.parametrize("is_compact", [False, True]) @pytest.mark.parametrize("is_compact", [False, True])
def test_fc_cutoff_radius(ph_nacl, ph_nacl_compact_fcsym, is_compact): def test_fc_cutoff_radius(ph_nacl: Phonopy, ph_nacl_compact_fcsym: Phonopy, is_compact):
"""Test of cutoff radius of force constants calculation by NaCl."""
if is_compact: if is_compact:
ph = ph_nacl_compact_fcsym ph = ph_nacl_compact_fcsym
else: else:
@ -83,8 +89,9 @@ def test_fc_cutoff_radius(ph_nacl, ph_nacl_compact_fcsym, is_compact):
[(False, False), (False, True), (True, False), (True, True)], [(False, False), (False, True), (True, False), (True, True)],
) )
def test_fc_cutoff_radius_svecs( def test_fc_cutoff_radius_svecs(
ph_nacl, ph_nacl_compact_fcsym, is_compact, store_dense_svecs ph_nacl: Phonopy, ph_nacl_compact_fcsym: Phonopy, is_compact, store_dense_svecs
): ):
"""Test of cutoff radius with dense-svecs format by NaCl."""
if is_compact: if is_compact:
ph = ph_nacl_compact_fcsym ph = ph_nacl_compact_fcsym
else: else:

View File

@ -1,136 +0,0 @@
64
2
1
0.0100000000000000 0.0000000000000000 0.0000000000000000
-0.0180619400 0.0000000000 0.0000000000
0.0030240400 0.0000000000 0.0000000000
-0.0000434000 0.0000000000 0.0000000000
0.0001066900 0.0000000000 0.0000000000
-0.0000434000 0.0000000000 0.0000000000
0.0001066900 0.0000000000 0.0000000000
-0.0001589900 0.0000000000 0.0000000000
0.0001344200 0.0000000000 0.0000000000
-0.0005716800 -0.0000021200 -0.0000021200
0.0003817600 0.0000002400 0.0000002400
-0.0005716800 0.0000021200 -0.0000021200
0.0003817600 -0.0000002400 0.0000002400
-0.0005716800 -0.0000021200 0.0000021200
0.0003817600 0.0000002400 -0.0000002400
-0.0005716800 0.0000021200 0.0000021200
0.0003817600 -0.0000002400 -0.0000002400
0.0004845700 0.0000000000 0.0019354900
0.0004850100 0.0000000000 -0.0019214300
-0.0000183800 0.0000000000 -0.0000178800
-0.0000173700 0.0000000000 0.0000170400
0.0004845700 0.0000000000 -0.0019354900
0.0004850100 0.0000000000 0.0019214300
-0.0000183800 0.0000000000 0.0000178800
-0.0000173700 0.0000000000 -0.0000170400
0.0004845700 0.0019354900 0.0000000000
0.0004850100 -0.0019214300 0.0000000000
0.0004845700 -0.0019354900 0.0000000000
0.0004850100 0.0019214300 0.0000000000
-0.0000183800 -0.0000178800 0.0000000000
-0.0000173700 0.0000170400 0.0000000000
-0.0000183800 0.0000178800 0.0000000000
-0.0000173700 -0.0000170400 0.0000000000
-0.0000810800 -0.0004282000 -0.0004282000
-0.0000835100 0.0004258800 0.0004258800
-0.0000810800 0.0004282000 -0.0004282000
-0.0000835100 -0.0004258800 0.0004258800
-0.0000810800 -0.0004282000 0.0004282000
-0.0000835100 0.0004258800 -0.0004258800
-0.0000810800 0.0004282000 0.0004282000
-0.0000835100 -0.0004258800 -0.0004258800
0.0050201700 0.0000000000 0.0000000000
0.0046140400 0.0000000000 0.0000000000
0.0000408300 0.0000000000 0.0000000000
0.0000373700 0.0000000000 0.0000000000
0.0000408300 0.0000000000 0.0000000000
0.0000373700 0.0000000000 0.0000000000
-0.0000105300 0.0000000000 0.0000000000
-0.0000118000 0.0000000000 0.0000000000
0.0017148800 -0.0000110200 0.0000000000
-0.0009600300 -0.0000013600 0.0000000000
0.0017148800 0.0000110200 0.0000000000
-0.0009600300 0.0000013600 0.0000000000
0.0001936100 0.0000011800 0.0000000000
-0.0002359000 0.0000000900 0.0000000000
0.0001936100 -0.0000011800 0.0000000000
-0.0002359000 -0.0000000900 0.0000000000
0.0017148800 0.0000000000 -0.0000110200
-0.0009600300 0.0000000000 -0.0000013600
0.0001936100 0.0000000000 0.0000011800
-0.0002359000 0.0000000000 0.0000000900
0.0017148800 0.0000000000 0.0000110200
-0.0009600300 0.0000000000 0.0000013600
0.0001936100 0.0000000000 -0.0000011800
-0.0002359000 0.0000000000 -0.0000000900
33
0.0100000000000000 0.0000000000000000 0.0000000000000000
-0.0000721800 -0.0004247300 -0.0004247300
-0.0000667600 0.0004272300 0.0004272300
-0.0000721800 0.0004247300 -0.0004247300
-0.0000667600 -0.0004272300 0.0004272300
-0.0000721800 -0.0004247300 0.0004247300
-0.0000667600 0.0004272300 -0.0004272300
-0.0000721800 0.0004247300 0.0004247300
-0.0000667600 -0.0004272300 -0.0004272300
0.0046345100 0.0000000000 0.0000000000
0.0050314800 0.0000000000 0.0000000000
0.0000499300 0.0000000000 0.0000000000
0.0000510900 0.0000000000 0.0000000000
0.0000499300 0.0000000000 0.0000000000
0.0000510900 0.0000000000 0.0000000000
-0.0000030400 0.0000000000 0.0000000000
0.0000012000 0.0000000000 0.0000000000
0.0017234100 0.0000077800 0.0000000000
-0.0009445700 -0.0000004700 0.0000000000
0.0017234100 -0.0000077800 0.0000000000
-0.0009445700 0.0000004700 0.0000000000
0.0002042400 0.0000019500 0.0000000000
-0.0002202400 -0.0000015500 0.0000000000
0.0002042400 -0.0000019500 0.0000000000
-0.0002202400 0.0000015500 0.0000000000
0.0017234100 0.0000000000 0.0000077800
-0.0009445700 0.0000000000 -0.0000004700
0.0002042400 0.0000000000 0.0000019500
-0.0002202400 0.0000000000 -0.0000015500
0.0017234100 0.0000000000 -0.0000077800
-0.0009445700 0.0000000000 0.0000004700
0.0002042400 0.0000000000 -0.0000019500
-0.0002202400 0.0000000000 0.0000015500
-0.0236731700 0.0000000000 0.0000000000
0.0013201600 0.0000000000 0.0000000000
-0.0005159800 0.0000000000 0.0000000000
0.0006463500 0.0000000000 0.0000000000
-0.0005159800 0.0000000000 0.0000000000
0.0006463500 0.0000000000 0.0000000000
-0.0001691500 0.0000000000 0.0000000000
0.0001742700 0.0000000000 0.0000000000
-0.0008016000 0.0000039800 0.0000039800
0.0005170500 -0.0000010700 -0.0000010700
-0.0008016000 -0.0000039800 0.0000039800
0.0005170500 0.0000010700 -0.0000010700
-0.0008016000 0.0000039800 -0.0000039800
0.0005170500 -0.0000010700 0.0000010700
-0.0008016000 -0.0000039800 -0.0000039800
0.0005170500 0.0000010700 0.0000010700
0.0013674700 0.0000000000 0.0020506700
0.0013743700 0.0000000000 -0.0020695000
-0.0000146100 0.0000000000 0.0001925900
-0.0000113300 0.0000000000 -0.0001898400
0.0013674700 0.0000000000 -0.0020506700
0.0013743700 0.0000000000 0.0020695000
-0.0000146100 0.0000000000 -0.0001925900
-0.0000113300 0.0000000000 0.0001898400
0.0013674700 0.0020506700 0.0000000000
0.0013743700 -0.0020695000 0.0000000000
0.0013674700 -0.0020506700 0.0000000000
0.0013743700 0.0020695000 0.0000000000
-0.0000146100 0.0001925900 0.0000000000
-0.0000113300 -0.0001898400 0.0000000000
-0.0000146100 -0.0001925900 0.0000000000
-0.0000113300 0.0001898400 0.0000000000

View File

@ -28,7 +28,7 @@ def test_parse_vasprun_xml():
"""Test parsing vasprun.xml with expat.""" """Test parsing vasprun.xml with expat."""
filename_vasprun = os.path.join(data_dir, "vasprun.xml.tar.bz2") filename_vasprun = os.path.join(data_dir, "vasprun.xml.tar.bz2")
_tar = tarfile.open(filename_vasprun) _tar = tarfile.open(filename_vasprun)
filename = os.path.join(data_dir, "FORCE_SETS_NaCl") filename = os.path.join(data_dir, "../FORCE_SETS_NaCl")
dataset = parse_FORCE_SETS(filename=filename) dataset = parse_FORCE_SETS(filename=filename)
for i, member in enumerate(_tar.getmembers()): for i, member in enumerate(_tar.getmembers()):
vr = Vasprun(_tar.extractfile(member), use_expat=True) vr = Vasprun(_tar.extractfile(member), use_expat=True)