-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
read_mmn
and read_chk
and associated tests
- Loading branch information
Showing
10 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from . import _schema | ||
from ._amn import * | ||
from ._chk import * | ||
from ._eig import * | ||
from ._mmn import * | ||
from ._nnkp import * | ||
from ._win import * | ||
from ._wout import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from __future__ import annotations | ||
import typing | ||
|
||
import numpy as np | ||
|
||
|
||
__all__ = ['read_chk'] | ||
|
||
|
||
def read_chk(stream: typing.TextIO) -> dict: | ||
""" | ||
Read checkpoint | ||
Arguments: | ||
stream: a file-like stream | ||
Returns: | ||
dict | ||
""" | ||
chk = {} | ||
|
||
chk['header'] = stream.readline() | ||
chk['num_bands'] = Nb = int(stream.readline()) | ||
chk['num_exclude_bands'] = int(stream.readline()) | ||
if chk['num_exclude_bands'] > 0: | ||
chk['num_exclude_bands'] = np.fromstring(stream.readline(), dtype=int) | ||
chk['real_lattice'] = np.fromstring(stream.readline(), sep=' ', dtype=float).reshape((3, 3), order='F') | ||
chk['recip_lattice'] = np.fromstring(stream.readline(), sep=' ', dtype=float).reshape((3, 3), order='F') | ||
chk['num_kpts'] = Nk = int(stream.readline()) | ||
chk['mp_grid'] = np.fromstring(stream.readline(), sep=' ', dtype=int) | ||
chk['kpt_latt'] = np.zeros((chk['num_kpts'], 3)) | ||
for idx in range(chk['num_kpts']): | ||
chk['kpt_latt'][idx] = np.fromstring(stream.readline(), sep=' ', dtype=float) | ||
chk['nntot'] = Nn = int(stream.readline()) | ||
chk['num_wann'] = Nw = int(stream.readline()) | ||
chk['checkpoint'] = stream.readline() | ||
chk['have_disentangled'] = bool(int(stream.readline())) | ||
if chk['have_disentangled']: | ||
chk['omega_invariant'] = float(stream.readline()) | ||
chk['lwindow'] = np.loadtxt(stream, max_rows=(Nk*Nb), dtype=bool).reshape((Nk, Nb)) | ||
chk['nwindim'] = np.loadtxt(stream, max_rows=Nk, dtype=int) | ||
chk['u_matrix_opt'] = np.loadtxt(stream, max_rows=(Nk*Nw*Nb), dtype=float).view(complex).reshape((Nk, Nw, Nb)) | ||
chk['u_matrix'] = np.loadtxt(stream, max_rows=(Nk*Nw*Nw), dtype=float).view(complex).reshape((Nw, Nw, Nk), order='F').transpose((2, 0, 1)) | ||
chk['m_matrix'] = np.loadtxt(stream, max_rows=(Nk*Nn*Nw*Nw), dtype=float).view(complex).reshape((Nw, Nw, Nn, Nk), order='F').transpose((3, 2, 0, 1)) | ||
|
||
return chk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from __future__ import annotations | ||
import itertools | ||
import typing | ||
|
||
import numpy as np | ||
|
||
|
||
__all__ = ['read_mmn'] | ||
|
||
|
||
def read_mmn(stream: typing.TextIO) -> tuple[np.ndarray, np.ndarray]: | ||
""" | ||
Read overlaps matrix | ||
Arguments: | ||
stream: a file-like stream | ||
Returns: | ||
overlaps matrix (Nk, Nn, Nb, Nb) | ||
nnkps (Nk, Nn, 5) | ||
""" | ||
stream.readline() # header | ||
|
||
[Nb, Nk, Nn] = np.fromstring(stream.readline(), sep=' ', dtype=int) | ||
|
||
mmn = np.zeros((Nk, Nn, Nb, Nb), dtype=complex) | ||
nnkpts = np.zeros((Nk, Nn, 5), dtype=int) | ||
|
||
for (ik, ikb) in itertools.product(range(Nk), range(Nn)): | ||
nnkpts[ik, ikb] = np.fromstring(stream.readline(), sep=' ', dtype=int) | ||
mmn[ik, ikb] = np.loadtxt(stream, max_rows=(Nb*Nb)).view(complex).reshape((Nb, Nb), order='F') | ||
|
||
nnkpts[:, :, 0] -= 1 | ||
nnkpts[:, :, 1] -= 1 | ||
|
||
return (mmn, nnkpts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ else | |
echo "LIBS=-lblas -llapack" >> make.inc | ||
fi | ||
|
||
make | ||
make wannier post w90chk2chk | ||
|
||
popd | ||
popd | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import itertools | ||
import pathlib | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import w90io | ||
|
||
|
||
@pytest.mark.parametrize('example', ['example01', 'example02']) | ||
def test_read_chk(wannier90, example): | ||
with open(pathlib.Path(wannier90)/f'examples/{example}/wannier.chk.fmt', 'r') as fh: | ||
chk = w90io.read_chk(fh) | ||
umn = chk['u_matrix'] | ||
mmn_ref = chk['m_matrix'] | ||
|
||
with open(pathlib.Path(wannier90)/f'examples/{example}/wannier.nnkp', 'r') as fh: | ||
nnkp = w90io.parse_nnkp_raw(fh.read()) | ||
|
||
with open(pathlib.Path(wannier90)/f'examples/{example}/wannier.mmn', 'r') as fh: | ||
(mmn_test, nnkpts) = w90io.read_mmn(fh) | ||
nnkpts1 = np.copy(nnkpts) | ||
nnkpts1[:, :, 0] += 1 | ||
nnkpts1[:, :, 1] += 1 | ||
|
||
for (ik, ikb) in itertools.product(range(mmn_test.shape[0]), range(mmn_test.shape[1])): | ||
mmn_test[ik, ikb] = np.dot(np.dot(umn[ik].conj().T, mmn_test[ik, ikb]), umn[nnkpts[:, :, 1][ik, ikb]]) | ||
|
||
assert np.allclose(np.array(nnkp['nnkpts']).reshape(nnkpts.shape), nnkpts1) | ||
assert np.allclose(mmn_test, mmn_ref) | ||
assert np.allclose(chk['real_lattice'][0], nnkp['direct_lattice']['a1']) | ||
assert np.allclose(chk['real_lattice'][1], nnkp['direct_lattice']['a2']) | ||
assert np.allclose(chk['real_lattice'][2], nnkp['direct_lattice']['a3']) | ||
assert np.allclose(chk['recip_lattice'][0], nnkp['reciprocal_lattice']['b1']) | ||
assert np.allclose(chk['recip_lattice'][1], nnkp['reciprocal_lattice']['b2']) | ||
assert np.allclose(chk['recip_lattice'][2], nnkp['reciprocal_lattice']['b3']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pathlib | ||
|
||
import numpy as np | ||
import pytest | ||
import w90io | ||
|
||
|
||
@pytest.mark.parametrize('example', ['example01', 'example02', 'example04']) | ||
def test_read_mmn(wannier90, example): | ||
with open(pathlib.Path(wannier90)/f'examples/{example}/wannier.mmn', 'r') as fh: | ||
(mmn, nnkpts) = w90io.read_mmn(fh) | ||
nnkpts[:, :, 0] += 1 | ||
nnkpts[:, :, 1] += 1 | ||
|
||
with open(pathlib.Path(wannier90)/f'examples/{example}/wannier.nnkp', 'r') as fh: | ||
nnkp = w90io.parse_nnkp_raw(fh.read()) | ||
|
||
assert np.allclose(nnkpts, np.asarray(nnkp['nnkpts'], dtype=int).reshape(nnkpts.shape)) |