Skip to content

Commit

Permalink
- Apply ruff format .:
Browse files Browse the repository at this point in the history
-- affected: testers + phonopy/calculator.py
-- local tests all pass
  • Loading branch information
ndaelman-hu committed Feb 21, 2024
1 parent 80342a5 commit 7ca8d98
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 190 deletions.
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
#
import os
import pytest

# Set up pytest to pass control to the debugger on an exception.
if os.getenv('_PYTEST_RAISE', "0") != "0":
if os.getenv("_PYTEST_RAISE", "0") != "0":

@pytest.hookimpl(tryfirst=True)
def pytest_exception_interact(call):
Expand Down
44 changes: 27 additions & 17 deletions tests/test_aflowparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,41 @@ def approx(value, abs=0, rel=1e-6):
return pytest.approx(value, abs=abs, rel=rel)


@pytest.fixture(scope='module')
@pytest.fixture(scope="module")
def parser():
return AFLOWParser()


def test_aflowlib(parser):
archive = EntryArchive()
parser.parse('tests/data/aflow/Ag1Co1O2_ICSD_246157/aflowlib.json', archive, None)
parser.parse("tests/data/aflow/Ag1Co1O2_ICSD_246157/aflowlib.json", archive, None)

assert len(archive.run) == 3

assert archive.run[0].program.version == 'aflow30847'
assert archive.run[0].x_aflow_auid == 'aflow:fbc2cf03b9659c90'
assert archive.run[0].program.version == "aflow30847"
assert archive.run[0].x_aflow_auid == "aflow:fbc2cf03b9659c90"
sec_system = archive.run[0].system[0]
assert sec_system.atoms.lattice_vectors[1][0].magnitude == approx(-1.45323479e-10)
assert sec_system.x_aflow_Pearson_symbol_superlattice == 'hP8'
assert list(sec_system.x_aflow_Wyckoff_letters_orig) == [['c'], ['a'], ['f']]
assert sec_system.x_aflow_Pearson_symbol_superlattice == "hP8"
assert list(sec_system.x_aflow_Wyckoff_letters_orig) == [["c"], ["a"], ["f"]]
sec_method = archive.run[0].method[0]
assert sec_method.x_aflow_delta_electronic_energy_convergence == approx(3.06569e-05)
sec_calculation = archive.run[0].calculation[0]
assert sec_calculation.energy.total.value.magnitude == approx(-5.58872856e-18)
assert sec_calculation.forces.total.value[0][1].magnitude == approx(-2.14691669e-13)
assert sec_calculation.thermodynamics[0].enthalpy.magnitude == approx(-5.58872856e-18)
assert sec_calculation.thermodynamics[0].enthalpy.magnitude == approx(
-5.58872856e-18
)
assert sec_calculation.x_aflow_pressure_residual == approx(2.95)

run = archive.run[1]
assert len(run.system) == len(run.calculation) == 24
assert run.system[7].atoms.labels[6] == 'O'
assert run.system[21].atoms.lattice_vectors[0][1].magnitude == approx(-2.5241880209758e-10)
assert run.system[7].atoms.labels[6] == "O"
assert run.system[21].atoms.lattice_vectors[0][1].magnitude == approx(
-2.5241880209758e-10
)
assert run.calculation[15].energy.total.value.magnitude == approx(-6.9854741e-19)
assert run.calculation[9].stress.total.value[2][2].magnitude == approx(-1.594e+08)
assert run.calculation[9].stress.total.value[2][2].magnitude == approx(-1.594e08)
# TODO currently workflow is not a repeating section
# assert archive.workflow.results.n_deformations == 3
# assert archive.workflow.results.strain_maximum == pytest.approx(0.01)
Expand All @@ -68,7 +72,7 @@ def test_aflowlib(parser):
run = archive.run[2]
assert len(run.system) == len(run.calculation) == 28
assert run.system[3].atoms.positions[3][2].magnitude == approx(5.53515521e-10)
assert run.calculation[19].thermodynamics[0].pressure.magnitude == (-1.6886e+09)
assert run.calculation[19].thermodynamics[0].pressure.magnitude == (-1.6886e09)
sec_thermo = archive.workflow2.results
assert sec_thermo.temperature[12].magnitude == 120
# assert sec_thermo.thermal_conductivity[18].magnitude == approx(4.924586)
Expand All @@ -79,24 +83,30 @@ def test_aflowlib(parser):

def test_aflowin(parser):
archive = EntryArchive()
parser.parse('tests/data/aflow/MgO/aflow.in', archive, None)
parser.parse("tests/data/aflow/MgO/aflow.in", archive, None)

assert len(archive.run) == 2

assert archive.run[0].program.version == '3.2.1'
assert archive.run[0].program.version == "3.2.1"
sec_system = archive.run[0].system[0]
assert sec_system.atoms.lattice_vectors[1][2].magnitude == approx(2.1277509e-10)
assert sec_system.atoms.positions[1][1].magnitude == approx(2.1277509e-10)

sec_scc = archive.run[1].calculation[0]
assert sec_scc.dos_phonon[0].energies[80].magnitude == approx(5.71011064e-22)
assert sec_scc.dos_phonon[0].total[0].value[1866].magnitude == approx(2.06688135e+20)
assert sec_scc.dos_phonon[0].total[0].value[1866].magnitude == approx(2.06688135e20)
assert len(sec_scc.band_structure_phonon[0].segment) == 10
assert sec_scc.band_structure_phonon[0].segment[3].kpoints[7][1] == approx(1.02984830)
assert sec_scc.band_structure_phonon[0].segment[9].energies[0][10][3].magnitude == approx(1.92480691e-21)
assert sec_scc.band_structure_phonon[0].segment[3].kpoints[7][1] == approx(
1.02984830
)
assert sec_scc.band_structure_phonon[0].segment[9].energies[0][10][
3
].magnitude == approx(1.92480691e-21)

assert archive.workflow2.results.qpoints[9249][0] == approx(-4.7619047619e-02)
assert archive.workflow2.results.group_velocity[234][2][0].magnitude == approx(-133.348333)
assert archive.workflow2.results.group_velocity[234][2][0].magnitude == approx(
-133.348333
)

# TODO currently workflow is not a repeating section
# assert archive.workflow.results.temperature[161].magnitude == approx(1610)
Expand Down
18 changes: 11 additions & 7 deletions tests/test_asrparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

# TODO this does not work for the current version of ASR

tests_path = 'tests/data/asr'
tests_path = "tests/data/asr"


def approx(value, abs=0, rel=1e-6):
Expand All @@ -37,21 +37,25 @@ def approx(value, abs=0, rel=1e-6):

def clear_database():
try:
subprocess.Popen('rm -rf %s/.asr' % tests_path)
subprocess.Popen("rm -rf %s/.asr" % tests_path)
except Exception:
pass


@pytest.fixture(scope='module')
@pytest.fixture(scope="module")
def test_database():
# TODO not sure how to do this properly
cwd = os.getcwd()
try:
os.chdir(tests_path)
subprocess.Popen(['asr', 'init'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
subprocess.Popen(
['asr', 'run', 'asr.c2db.relax -a Si.json -c {"name":"emt"}'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
["asr", "init"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
subprocess.Popen(
["asr", "run", 'asr.c2db.relax -a Si.json -c {"name":"emt"}'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except Exception:
pass
finally:
Expand All @@ -60,7 +64,7 @@ def test_database():

def test_parsing(test_database):
asr_to_archives(tests_path)
archive_files = [f for f in os.listdir(tests_path) if f.startswith('archive_')]
archive_files = [f for f in os.listdir(tests_path) if f.startswith("archive_")]
# assert len(archive_files) > 0
for f in archive_files:
data = json.load(open(os.path.join(tests_path, f)))
Expand Down
12 changes: 6 additions & 6 deletions tests/test_atomateparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ def approx(value, abs=0, rel=1e-6):
return pytest.approx(value, abs=abs, rel=rel)


@pytest.fixture(scope='module')
@pytest.fixture(scope="module")
def parser():
return AtomateParser()


def test_all(parser):
archive = EntryArchive()
parser.parse('tests/data/atomate/mp-149/mp-149_materials.json', archive, None)
parser.parse("tests/data/atomate/mp-149/mp-149_materials.json", archive, None)

run = archive.run[0]
assert run.program.name == 'MaterialsProject'
assert run.program.name == "MaterialsProject"

sec_system = run.system[0]
assert sec_system.atoms.labels == ['Si', 'Si']
assert sec_system.atoms.labels == ["Si", "Si"]
assert sec_system.atoms.lattice_vectors[1][2].magnitude == approx(2.734364e-10)
assert sec_system.atoms.positions[0][0].magnitude == approx(1.367182e-10)
assert sec_system.x_mp_composition_reduced[0].x_mp_value == approx(1.0)
assert sec_system.x_mp_symmetry[0].x_mp_symprec == approx(0.1)
assert sec_system.x_mp_elements[0] == 'Si'
assert sec_system.x_mp_elements[0] == "Si"
assert sec_system.x_mp_volume == approx(40.88829284866483)
assert sec_system.x_mp_formula_anonymous == 'A'
assert sec_system.x_mp_formula_anonymous == "A"

# TODO currently, workflow2 is not repeating
# TODO error loading metainfo in github action
Expand Down
52 changes: 30 additions & 22 deletions tests/test_elasticparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def approx(value, abs=0, rel=1e-6):
return pytest.approx(value, abs=abs, rel=rel)


@pytest.fixture(scope='module')
@pytest.fixture(scope="module")
def parser():
return ElasticParser()


def test_2nd(parser):
archive = EntryArchive()
parser.parse('tests/data/elastic/2nd/INFO_ElaStic', archive, None)
parser.parse("tests/data/elastic/2nd/INFO_ElaStic", archive, None)

sec_system = archive.run[0].system[0]
assert np.shape(sec_system.atoms.positions) == (8, 3)
Expand All @@ -46,34 +46,36 @@ def test_2nd(parser):
sec_fit_parameters = sec_method.x_elastic_section_fitting_parameters[0]
assert sec_fit_parameters.x_elastic_fitting_parameters_eta[0] == 0.05

assert archive.workflow2.method.energy_stress_calculator == 'exciting'
assert archive.workflow2.method.energy_stress_calculator == "exciting"
results = archive.workflow2.results
assert results.deformation_types[2][5] == '2eta'
assert results.deformation_types[2][5] == "2eta"
sec_strain = results.strain_diagrams
assert len(sec_strain) == 7
assert sec_strain[0].eta[1][3] == -0.02
assert sec_strain[0].value[2][5] == approx(-3.30877062e-16)
assert sec_strain[3].type == 'cross-validation'
assert sec_strain[3].type == "cross-validation"
assert sec_strain[2].eta[1][2] == 0.03
assert sec_strain[6].value[2][4] == approx(6.8708895e+12)
assert sec_strain[6].value[2][4] == approx(6.8708895e12)
assert sec_strain[4].polynomial_fit_order == 6

assert results.elastic_constants_notation_matrix_second_order[1][2] == 'C12'
assert results.elastic_constants_matrix_second_order[0][2].magnitude == approx(1.008e+11)
assert results.elastic_constants_notation_matrix_second_order[1][2] == "C12"
assert results.elastic_constants_matrix_second_order[0][2].magnitude == approx(
1.008e11
)
assert results.compliance_matrix_second_order[3][3].magnitude == approx(1.75e-12)
assert results.bulk_modulus_voigt.magnitude == approx(4.4937e+11)
assert results.shear_modulus_voigt.magnitude == approx(5.3074e+11)
assert results.bulk_modulus_reuss.magnitude == approx(4.4937e+11)
assert results.shear_modulus_reuss.magnitude == approx(5.2574e+11)
assert results.bulk_modulus_hill.magnitude == approx(4.4937e+11)
assert results.shear_modulus_hill.magnitude == approx(5.2824e+11)
assert results.young_modulus_voigt.magnitude == approx(1.14245e+12)
assert results.bulk_modulus_voigt.magnitude == approx(4.4937e11)
assert results.shear_modulus_voigt.magnitude == approx(5.3074e11)
assert results.bulk_modulus_reuss.magnitude == approx(4.4937e11)
assert results.shear_modulus_reuss.magnitude == approx(5.2574e11)
assert results.bulk_modulus_hill.magnitude == approx(4.4937e11)
assert results.shear_modulus_hill.magnitude == approx(5.2824e11)
assert results.young_modulus_voigt.magnitude == approx(1.14245e12)
assert results.poisson_ratio_voigt == 0.08
assert results.young_modulus_reuss.magnitude == approx(1.1347e+12)
assert results.young_modulus_reuss.magnitude == approx(1.1347e12)
assert results.poisson_ratio_reuss == 0.08
assert results.young_modulus_hill.magnitude == approx(1.13858e+12)
assert results.young_modulus_hill.magnitude == approx(1.13858e12)
assert results.poisson_ratio_hill == 0.08
assert results.eigenvalues_elastic[1].magnitude == approx(1.3481e+12)
assert results.eigenvalues_elastic[1].magnitude == approx(1.3481e12)

sec_scc = archive.run[0].calculation[0]
assert len(sec_scc.calculations_path) == 33
Expand All @@ -83,7 +85,7 @@ def test_2nd(parser):

def test_3rd(parser):
archive = EntryArchive()
parser.parse('tests/data/elastic/3rd/INFO_ElaStic', archive, None)
parser.parse("tests/data/elastic/3rd/INFO_ElaStic", archive, None)

# The strain diagram data cannot be parsed because of the inhomogeneous shape probably
# due to error in output.
Expand All @@ -94,9 +96,15 @@ def test_3rd(parser):
# assert sec_strain[3].value[8][7] == approx(2.06899957e-23)

results = archive.workflow2.results
assert results.elastic_constants_matrix_third_order[3][1][3].magnitude == approx(1.274e+10)
assert results.elastic_constants_matrix_third_order[5][2][5].magnitude == approx(1.2825e+10)
assert results.elastic_constants_matrix_third_order[0][0][1].magnitude == approx(-1.18334e+12)
assert results.elastic_constants_matrix_third_order[3][1][3].magnitude == approx(
1.274e10
)
assert results.elastic_constants_matrix_third_order[5][2][5].magnitude == approx(
1.2825e10
)
assert results.elastic_constants_matrix_third_order[0][0][1].magnitude == approx(
-1.18334e12
)


def test_stress(parser):
Expand Down
Loading

0 comments on commit 7ca8d98

Please sign in to comment.