2015-08-10 11:32:08 +02:00
|
|
|
r"""
|
|
|
|
Tests of 'read_plocar()' from module 'plocar_io.c_plocar_io'
|
|
|
|
"""
|
|
|
|
import mytest
|
|
|
|
import numpy as np
|
2018-09-06 13:48:24 +02:00
|
|
|
from triqs_dft_tools.plocar_io.c_plocar_io import read_plocar
|
2015-08-10 11:32:08 +02:00
|
|
|
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# TestFileIO
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
class TestFileIO(mytest.MyTestCase):
|
|
|
|
"""
|
|
|
|
Function:
|
|
|
|
|
|
|
|
def read_plocar(filename)
|
|
|
|
|
|
|
|
Scenarios:
|
|
|
|
|
|
|
|
- **if** file PLOCAR does not exist **raise** IOError
|
|
|
|
- **if** PLOCAR is truncated **raise** IOError
|
|
|
|
- **if** the precision flag is not 4 or 8 **raise** ValueError
|
|
|
|
- **if** PLOCAR with prec=8 is read **compare** the output
|
|
|
|
- **if** PLOCAR with prec=4 is read **compare** the output
|
|
|
|
"""
|
|
|
|
# Scenario 1
|
|
|
|
def test_no_plocar(self):
|
|
|
|
err_mess = "Error opening xPLOCAR"
|
2020-04-08 21:35:59 +02:00
|
|
|
with self.assertRaisesRegex(IOError, err_mess):
|
2015-08-10 11:32:08 +02:00
|
|
|
read_plocar('xPLOCAR')
|
|
|
|
|
|
|
|
# Scenario 2
|
|
|
|
def test_end_of_file(self):
|
|
|
|
err_mess = "End-of-file reading"
|
2020-04-08 21:35:59 +02:00
|
|
|
with self.assertRaisesRegex(IOError, err_mess):
|
2015-08-10 11:32:08 +02:00
|
|
|
read_plocar('PLOCAR.trunc')
|
|
|
|
|
|
|
|
# Scenario 3
|
|
|
|
def test_wrong_prec(self):
|
|
|
|
err_mess = "only 'prec = 4, 8' are supported"
|
2020-04-08 21:35:59 +02:00
|
|
|
with self.assertRaisesRegex(ValueError, err_mess):
|
2015-08-10 11:32:08 +02:00
|
|
|
read_plocar('PLOCAR.noprec')
|
|
|
|
|
|
|
|
# Scenario 4
|
|
|
|
def test_plocar_prec8(self):
|
|
|
|
pars, plo, ferw = read_plocar('PLOCAR.prec8')
|
|
|
|
nion, ns, nk, nb, nlm = plo.shape
|
|
|
|
|
|
|
|
test_file = 'PLOCAR.prec8.out.test'
|
|
|
|
with open(test_file, 'wt') as f:
|
|
|
|
f.write(" nlm =%5i\n"%(nlm))
|
|
|
|
ion = 1
|
|
|
|
isp = 1
|
2020-04-08 21:35:59 +02:00
|
|
|
for ik in range(nk):
|
|
|
|
for ib in range(nb):
|
2015-08-10 11:32:08 +02:00
|
|
|
f.write("%5i%5i%5i%5i%10.5f\n"%(ion, isp, ik+1, ib+1, ferw[0, 0, ik, ib]))
|
2020-04-08 21:35:59 +02:00
|
|
|
for ilm in range(nlm):
|
2015-08-10 11:32:08 +02:00
|
|
|
p = plo[0, 0, ik, ib, ilm]
|
|
|
|
f.write("%5i%15.7f%15.7f\n"%(ilm+1, p.real, p.imag))
|
|
|
|
|
|
|
|
expected_file = 'PLOCAR.prec8.out'
|
|
|
|
self.assertFileEqual(test_file, expected_file)
|
|
|
|
|
|
|
|
# Scenario 5
|
|
|
|
def test_plocar_example(self):
|
|
|
|
pars, plo, ferw = read_plocar('PLOCAR.example')
|
|
|
|
nion, ns, nk, nb, nlm = plo.shape
|
|
|
|
|
|
|
|
self.assertEqual(pars['nion'], nion)
|
|
|
|
self.assertEqual(pars['ns'], ns)
|
|
|
|
self.assertEqual(pars['nk'], nk)
|
|
|
|
self.assertEqual(pars['nb'], nb)
|
|
|
|
|
|
|
|
test_file = 'PLOCAR.example.out.test'
|
|
|
|
with open(test_file, 'wt') as f:
|
|
|
|
f.write("pars: %s\n"%(pars))
|
2020-04-08 21:35:59 +02:00
|
|
|
for ion in range(nion):
|
|
|
|
for isp in range(ns):
|
|
|
|
for ik in range(nk):
|
|
|
|
for ib in range(nb):
|
2015-08-10 11:32:08 +02:00
|
|
|
f.write("%5i%5i%5i%5i %s\n"%(ion+1, isp+1, ik+1, ib+1,
|
|
|
|
ferw[ion, isp, ik, ib]))
|
2020-04-08 21:35:59 +02:00
|
|
|
for ilm in range(nlm):
|
2015-08-10 11:32:08 +02:00
|
|
|
p = plo[ion, isp, ik, ib, ilm]
|
|
|
|
f.write("%5i %s\n"%(ilm+1, p))
|
|
|
|
|
|
|
|
expected_file = 'PLOCAR.example.out'
|
|
|
|
self.assertFileEqual(test_file, expected_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|