Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
b079ae8
added pytest requirement
Ash-Pera Nov 11, 2021
0c200c4
added testing directory
Ash-Pera Nov 11, 2021
9cd9570
Added simple poscar read test
Ash-Pera Nov 12, 2021
14c0ff2
Added read_cryst_defualts test
Ash-Pera Nov 12, 2021
dff4dd9
added total integration test
Ash-Pera Nov 12, 2021
a8447ab
spilt off unit test file
Ash-Pera Nov 12, 2021
5df0bdb
started writing crystallography test
Ash-Pera Nov 12, 2021
31660ac
added unit testing
Ash-Pera Nov 12, 2021
25e4a1b
hid cystallography integration test
Ash-Pera Nov 12, 2021
02350e6
fixed comment block
Ash-Pera Nov 12, 2021
3d6b833
started optimization test
Ash-Pera Nov 12, 2021
a4ea1d6
skipped optimization-level test
Ash-Pera Nov 16, 2021
cb0fefd
added third dmin test
Ash-Pera Nov 16, 2021
6b85b33
added transition test
Ash-Pera Nov 16, 2021
bc81754
cleaned up structure assertions
Ash-Pera Nov 16, 2021
aba7058
cleaned up tmat test
Ash-Pera Nov 16, 2021
bbcd300
refactored matching test to use fixtures
Ash-Pera Nov 16, 2021
5cf0838
Finished migrating to fixtures
Ash-Pera Nov 17, 2021
c94247c
added more docstrings
Ash-Pera Nov 18, 2021
90ab124
added LAP test
Ash-Pera Nov 18, 2021
324a00c
started interface test
Ash-Pera Nov 18, 2021
bf89f37
refactored optimizing interface to fixture
Ash-Pera Nov 18, 2021
753eda7
added pytest requirement
Ash-Pera Nov 11, 2021
c7640f7
added testing directory
Ash-Pera Nov 11, 2021
b1eca1d
Added simple poscar read test
Ash-Pera Nov 12, 2021
2cc7b44
Added read_cryst_defualts test
Ash-Pera Nov 12, 2021
accb84c
added total integration test
Ash-Pera Nov 12, 2021
0bf64aa
spilt off unit test file
Ash-Pera Nov 12, 2021
43b9579
started writing crystallography test
Ash-Pera Nov 12, 2021
aadcf77
added unit testing
Ash-Pera Nov 12, 2021
ccf3308
hid cystallography integration test
Ash-Pera Nov 12, 2021
407fc16
fixed comment block
Ash-Pera Nov 12, 2021
6e0dd11
started optimization test
Ash-Pera Nov 12, 2021
ca2e201
skipped optimization-level test
Ash-Pera Nov 16, 2021
2472204
added third dmin test
Ash-Pera Nov 16, 2021
9d07b68
added transition test
Ash-Pera Nov 16, 2021
fcadcd6
cleaned up structure assertions
Ash-Pera Nov 16, 2021
78a5a26
cleaned up tmat test
Ash-Pera Nov 16, 2021
7562d7c
refactored matching test to use fixtures
Ash-Pera Nov 16, 2021
f292594
Finished migrating to fixtures
Ash-Pera Nov 17, 2021
f159a3c
added more docstrings
Ash-Pera Nov 18, 2021
2c537c2
added LAP test
Ash-Pera Nov 18, 2021
aa6e001
started interface test
Ash-Pera Nov 18, 2021
4a33141
refactored optimizing interface to fixture
Ash-Pera Nov 18, 2021
2ab69ed
Merge branch 'testing' of github.com:ftherrien/p2ptrans into testing
Ash-Pera Nov 18, 2021
28fadf5
moved interface and transition tests to own classes
Ash-Pera Nov 18, 2021
0062cc5
skip interface tests. too inconsitant.
Ash-Pera Nov 19, 2021
099c59a
added back looser transition vec_classes test
Ash-Pera Nov 19, 2021
161b443
added back weak interface ttrans test
Ash-Pera Nov 19, 2021
9262518
decreased test iterations to 5000
Ash-Pera Nov 20, 2021
78a5fb5
Merge branch 'master' into testing, applyinh typing changes
Ash-Pera Nov 24, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import setuptools
from numpy.distutils.core import setup, Extension

mod = Extension(name='p2ptrans.fmodules', sources=['source/lap.f90', 'source/utils.f90', 'source/tiling.f90', 'source/potential.f90', 'source/transform.f90'], extra_f90_compile_args=["-fopenmp","-O3"], extra_link_args=['-lgomp'], f2py_options=["only:","munkres", "free_trans", "rot_mat", "center", "eye", "norm", "split", "det", "sort", "sphere", "circle", "distance", "derivative", "closest", "fixed_tmat", "fixed_tmat_int", "intoptimization", "fastoptimization", "optimize_vec", ":"])
mod = Extension(name='p2ptrans.fmodules', sources=['source/lap.f90', 'source/utils.f90', 'source/tiling.f90', 'source/potential.f90', 'source/transform.f90'],\
extra_f90_compile_args=["-fopenmp","-O3"], extra_link_args=['-lgomp'],\
f2py_options=["only:","munkres", "free_trans", "rot_mat", "center", "eye", "norm", "split", "det", "sort", "sphere", "circle", "distance", "derivative", "closest", "fixed_tmat", "fixed_tmat_int", "intoptimization", "fastoptimization", "optimize_vec", ":"])

setup(name='p2ptrans',
version='2.0.2',
Expand All @@ -15,6 +17,7 @@
python_requires='>=3',
install_requires=[
'numpy',
'pytest',
'matplotlib',
'spglib',
'pylada @ git+https://github.com/pylada/pylada-light#egg=pylada',
Expand Down
9 changes: 9 additions & 0 deletions tests/BCC_POSCAR
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BCC structure
2.87
0.5 0.5 -0.5
-0.5 0.5 0.5
0.5 -0.5 0.5
Fe
1
Direct
0.0 0.0 0.0
16 changes: 16 additions & 0 deletions tests/DC_C_POSCAR
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
C8
1.0
3.573710 0.000000 0.000000
0.000000 3.573710 0.000000
0.000000 0.000000 3.573710
C
8
direct
0.250000 0.750000 0.250000 C
0.000000 0.000000 0.500000 C
0.250000 0.250000 0.750000 C
0.000000 0.500000 0.000000 C
0.750000 0.750000 0.750000 C
0.500000 0.000000 0.000000 C
0.750000 0.250000 0.250000 C
0.500000 0.500000 0.500000 C
16 changes: 16 additions & 0 deletions tests/DC_Si_POSCAR
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Si8
1.0
5.468728 0.000000 0.000000
0.000000 5.468728 0.000000
0.000000 0.000000 5.468728
Si
8
direct
0.250000 0.750000 0.250000 Si
0.000000 0.000000 0.500000 Si
0.250000 0.250000 0.750000 Si
0.000000 0.500000 0.000000 Si
0.750000 0.750000 0.750000 Si
0.500000 0.000000 0.000000 Si
0.750000 0.250000 0.250000 Si
0.500000 0.500000 0.500000 Si
9 changes: 9 additions & 0 deletions tests/FCC_POSCAR
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FCC structure
3.57
0.5 0.5 0.0
0.5 0.0 0.5
0.0 0.5 0.5
Fe
1
Direct
0.0 0.0 0.0
112 changes: 112 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
from pylada.crystal import Structure
from p2ptrans import read
import os, glob, shutil
import numpy as np
import pytest

BCC_filename = './BCC_POSCAR'
FCC_filename = './FCC_POSCAR'
testmat_filename = './testmat.txt'
testmat_map_filename = './testmat_map.txt'

_bcc = Structure([[0.5,-0.5, 0.5],
[ 0.5, 0.5,-0.5],
[-0.5, 0.5, 0.5]], scale=2.87, name='BCC structure')\
.add_atom(0., 0., 0., 'Fe')

_fcc = Structure([[0.5, 0.5, 0.0],
[ 0.5, 0.0, 0.5],
[ 0.0, 0.5, 0.5]], scale=3.57, name='FCC structure')\
.add_atom(0., 0., 0., 'Fe')

@pytest.fixture(scope="session")
def bcc_fcc_s():
'''Fixture that returns the test fcc and bcc structures (session scope)'''
return _bcc, _fcc

@pytest.fixture()
def bcc_fcc():
'''Fixture that returns the test fcc and bcc structures'''
return _bcc, _fcc

@pytest.fixture()
def bcc_fcc_filenames():
'''Fixture that returns the fcc and bcc filenames'''
return BCC_filename, FCC_filename

@pytest.fixture(scope='session')
def bcc_fcc_filenames_s():
'''Fixture that returns the fcc and bcc filenames (session scope)'''
return BCC_filename, FCC_filename



@pytest.fixture(scope="session")
def p2p_default_options_s():
'''Fixture that returns the defualt options from readOptions in the runner'''
# (fileA, fileB, ncell, filename, interactive, savedisplay, outdir,
# use, switch, prim, anim, vol, minimize, test, crystfile, n_steps,
# showversion, map_ncell)
return ('./POSCAR_A', './POSCAR_B', 300, './p2p.in', False, False, '.',
False, False, True, False, False, False, False, './cryst.in', 60,
False, None)

def p2p_int_default_options():
'''Fixture that returns the defualt p2pint options'''
# (fileA, fileB, planeA, planeB,
# ruleA, ruleB, ncell, n_iter, filename,
# interactive, savedisplay, outdir, use, minimize, test, n_iter, sym,
# vacuum, layers, surface, max_thickness, showversion)
return [['./POSCAR_A'], ['./POSCAR_B'], [0, 0, 1], [1, 1, 0],
{'1': {'Si'}}, {'1': {'Si'}}, 100, 1000, './p2p.in',
False, False, '.', False, False, False, 1000, 1,
10, 1, False, None, False]


@pytest.fixture()
def lap_test_matrix():
return np.loadtxt(testmat_filename)

@pytest.fixture()
def lap_test_map():
return np.loadtxt(testmat_map_filename)



def assert_structs_approx_eq(A, B, tol=0.001):
'''Assert that the two structures have the same cell, scales, and atoms of the same type in the same positions'''
assert len(A) == len(B)
np.testing.assert_allclose(A.cell, B.cell, tol)
assert A.scale == pytest.approx(B.scale)
for aAtom, bAtom in zip(A, B):
assert aAtom.type == bAtom.type
assert aAtom.pos == pytest.approx(bAtom.pos, tol)


def cleanup():
'''Removes dat files, the progress file, and tranPOSCARS from the current directory'''
for dat in glob.iglob('*dat'):
os.remove(dat)
if os.path.exists('progress.txt'):
os.remove('progress.txt')
if os.path.exists('out.txt'):
os.remove('out.txt')
if os.path.exists('TransPOSCARS'):
shutil.rmtree('TransPOSCARS')
if os.path.exists('DC_C_POSCAR-DC_Si_POSCAR'):
shutil.rmtree('DC_C_POSCAR-DC_Si_POSCAR')


@pytest.fixture()
def double_cleanup():
'''Fixture that cleans up both before and after'''
cleanup()
yield
cleanup()

@pytest.fixture(scope="session")
def double_cleanup_s():
'''Fixture that cleans up both before and after (session scope)'''
cleanup()
yield
cleanup()
5 changes: 5 additions & 0 deletions tests/cryst_test_file
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# test comment
ccell1 = [[1., 2., 3.],[4., 5., 6.],[7., 8., 9.]]
ccell2 = [[9., 8., 7.],[6., 5., 4.],[3., 2., 1.]]
planehkl = [1, 2, 3]
diruvw = [3, 2, 1]
95 changes: 95 additions & 0 deletions tests/interface_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from p2ptrans.interfaces import findMatchingInterfaces
from conftest import assert_structs_approx_eq
from pylada.crystal import Structure
import numpy as np
import pytest
import os


@pytest.fixture(scope="module")
def find_matching_interface(double_cleanup_s):
(A, B, ncell, n_iter, sym, filename, interactive,
savedisplay, term_outdir, minimize, test, A3D, B3D) = (\
Structure(np.array([[3.57371 , 0. , 0. ],
[0. , 2.52699457, 0. ],
[0. , 0. , 2.52699457]]), name='C8 (1 1 0) 0')\
.add_atom(0.0, 0.0, 0.0, '1', site=0)\
.add_atom(2.6802824999999992, 1.2634972874970873, 6.661338147750939e-16, '1', site=1),
Structure(np.array([[ 3.86697465, -6.89049062e-16, 0. ],
[ 0., 3.86697465e+00, 0. ],
[ 0., 0., 5.46872800]]), name='Si8 (0 0 1) 0')\
.add_atom(0.0, 0.0, 0.0, '1', site=0),
100, 1000, 1, './p2p.in', False, False, './DC_C_POSCAR-DC_Si_POSCAR/term_000-000', False, False,
np.array([[ 0., -0.70710678, 0.70710678],
[ 0., 0.70710678, 0.70710678],
[-1., 0., 0., ]]),
np.array([[-0.70710678, 0.70710678, 0.],
[-0.70710678, -0.70710678, 0.],
[ 0., 0., 1.]]))

ttrans, dispStruc, vec_classes, dmin = findMatchingInterfaces(A, B, ncell, 5000,
sym=sym, filename=filename,
interactive=interactive,
savedisplay=savedisplay,
outdir=term_outdir,
minimize=minimize, test=test,
A3D=A3D, B3D=B3D)
return (ttrans, dispStruc, vec_classes, dmin)

def test_interface_ttrans(find_matching_interface):
'''Test that the left 3x3 of the ttrans matrix is similar'''
#[[[ 0.924162, -0.23104 , 0. , -0.893427], another possiblity...
# [ 0.326741, 0.980222, 0. , 1.895085],
# [ 0. , 0. , 1. , 2.247815]]]
#[[[ 0.92416173, -0.23104043, 0., -0.89342744],
# [ 0.32674051, 0.98022154, 0., 1.89508484],
# [ 0., 0., 1., 2.24781482]]]
# [[[ 0.23104043 -0.92416173 0. -3.57370943]
# [ 0.98022154 0.32674051 0. 0.63191172]
# [ 0. 0. 1. 2.2478148 ]]]
ttrans, dispStruc, vec_classes, dmin = find_matching_interface
test_ttrans = np.array([[[ 0.92416173, -0.23104043, 0., -0.89342701],
[ 0.32674051, 0.98022154, 0., 1.89508287],
[ 0., 0., 1., 2.2478148 ]]])

np.testing.assert_allclose(np.sort(abs(ttrans[0, 0:2,0:2]), axis=None),
[0.23104 , 0.326741, 0.924162, 0.980222], 0.001)
np.testing.assert_allclose(ttrans[0, 2, :3], [0, 0, 1], 0.001)
np.testing.assert_allclose(ttrans[0, :4, 2], [0, 0, 1], 0.001)
# ignores last column


@pytest.mark.skip(reason="Too inconsistant...")
def test_interface_dispStruct(find_matching_interface):
#TODO: need to fix cell negetives
ttrans, dispStruc, vec_classes, dmin = find_matching_interface
test_dispStruc = Structure(np.array([[ -7.14742 , 3.57371 , 0. ],
[ -2.52699457, -15.16196742, 0. ],
[ 0. , -0. , 7.99572257]]))\
.add_atom(-3.5737104474246926, -17.6888747705878, 2.247814822377832, '0', atom='1', site=0)\
.add_atom(-0.893427947424688, -12.6348856305878, 2.247814822377832, '1', atom='1', site=1)\
.add_atom(-4.467137947424693, -13.898382915587801, 2.247814822377832, '2', atom='1', site=2)\
.add_atom(-4.474246906305268e-07, -16.4253774855878, 2.247814822377832, '3', atom='1', site=3)\
.add_atom(-6.253992947424691, -6.317399205587799, 2.247814822377832, '0', atom='1', site=4)\
.add_atom(-5.360565447424691, -10.107891060587797, 2.247814822377832, '2', atom='1', site=5)\
.add_atom(-2.680282947424688, -5.0539019205878, 2.247814822377832, '3', atom='1', site=6)\
.add_atom(-1.78685544742469, -8.844393775587802, 2.247814822377832, '1', atom='1', site=7)
assert_structs_approx_eq(dispStruc[0], test_dispStruc)

def test_interface_vec_classes(find_matching_interface):
'''Test that four vec classes are found, and that they are all similar to expected'''
ttrans, dispStruc, vec_classes, dmin = find_matching_interface
vec_classes = np.array(vec_classes[0])
#sorted_vec_classes = vec_classes[np.lexsort(np.transpose(np.round(vec_classes, 2)))]
assert len(vec_classes == 4)
for vec_class in vec_classes:
np.testing.assert_allclose(abs(vec_class), [ 0.893501, 0.631446, 2.247841], 0.001)

def test_interface_dmin(find_matching_interface):
#TODO: what is the reasonable tolerance on this?
ttrans, dispStruc, vec_classes, dmin = find_matching_interface
test_dmin = -44.17527158660921
assert -50 <= dmin <= -40



78 changes: 78 additions & 0 deletions tests/test_units.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from p2ptrans import read, analysis, core, format_spglib
from p2ptrans.fmodules import hungarian as lap
from conftest import assert_structs_approx_eq
from spglib import get_spacegroup
import numpy as np
import pytest


def test_read_poscars(bcc_fcc, bcc_fcc_filenames):
'''Test that pylada reads test files correctly'''
bcc, fcc = bcc_fcc
bcc_file, fcc_file = bcc_fcc_filenames
read_bcc = read.poscar(bcc_file)
read_fcc = read.poscar(fcc_file)

assert_structs_approx_eq(read_bcc, bcc)
assert_structs_approx_eq(read_fcc, fcc)

def test_spacegroup(bcc_fcc):
'''Tests that spglib reports the correct spacegroups'''
bcc, fcc = bcc_fcc
assert get_spacegroup(format_spglib.to_spglib(bcc), symprec=0.3, angle_tolerance=3.0) == 'Im-3m (229)'
assert get_spacegroup(format_spglib.to_spglib(fcc), symprec=0.3, angle_tolerance=3.0) == 'Fm-3m (225)'

def test_read_cryst_defaults():
'''Test that the cryst param defaults are the unit matrix'''
ccell1, ccell2, planehkl, diruvw = analysis.readCrystParam('./FILE_DNE')
assert (ccell1 == [[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]).all
assert (ccell2 == [[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]).all
assert planehkl == [1, 0, 0]
assert diruvw == [0, 1, 0]

def test_read_cryst():
'''Tests that the cryst param file is read correctly'''
ccell1, ccell2, planehkl, diruvw = analysis.readCrystParam('./cryst_test_file')
assert ccell1 == [[1., 2., 3.],[4., 5., 6.],[7., 8., 9.]]
assert ccell2 == [[9., 8., 7.],[6., 5., 4.],[3., 2., 1.]]
assert planehkl == [1, 2, 3]
assert diruvw == [3, 2, 1]

#def test_p2pint_extra_params(): #TODO:this
# assert False

@pytest.mark.skip(reason="Replaced with smaller and larger tests...")
def test_optimize(double_cleanup, bcc_fcc):
BCC, FCC = bcc_fcc
BCC_cell = BCC.cell * 2.87
FCC_cell = FCC.cell * 3.57
# optimization(A, Acell, mulA, B, Bcell, mulB, ncell, filename, outdir, max_cell_size)
result = core.optimization(BCC, BCC_cell, 1, FCC, FCC_cell, 1, 300, './p2p.in', '.', 1000)
Apos, Apos_map, Bpos, Bposst, n_map, natA, class_list, tmat, dmin, atoms, atom_types, foundcell, vec = result
print(Apos, Apos_map, Bpos, Bposst, n_map, natA, class_list, tmat, dmin, atoms, atom_types, foundcell, vec)


def test_LAP(lap_test_matrix, lap_test_map):
test_mat = lap_test_matrix
test_map = lap_test_map
dist, mapping = lap.munkres(test_mat, )
assert dist == pytest.approx(94.08380269604208, 0.0001)
np.testing.assert_allclose(mapping, test_map)

'''!ASHHH ! way to print given matrix
integer :: L

Print *, "start Matrix"
DO i = 1, N ! print row-by-row
DO j = 1, N
print *, C(i, j)
END DO
!WRITE(*,*) (C(i,j), j=1, L)
END DO
print *, "end Matrix"
stop
!END ASHHHH'''
7 changes: 7 additions & 0 deletions tests/testinglist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

- resuses results
- runs from continue.dat

- individual bits of p2ptrans work

- does interfaces
Loading