Skip to content

Commit

Permalink
started optimization test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-Pera committed Nov 12, 2021
1 parent 02350e6 commit 3d6b833
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 12 deletions.
Binary file added tests/stats
Binary file not shown.
14 changes: 3 additions & 11 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from p2ptrans import analysis, findMatching
import pytest
import os, glob
import os
import numpy as np
from test_units import read_FCC_BCC, BCC_file, FCC_file, tol
from test_units import read_FCC_BCC, BCC_file, FCC_file, tol, cleanup



Expand All @@ -14,14 +14,6 @@ def default_options():
False, False, True, False, False, False, False, './cryst.in', 60,
False, None)


def cleanup():
for dat in glob.iglob('*dat'):
os.remove(dat)
if os.path.exists('progress.txt'):
os.remove('progress.txt')


def test_matching():
'''Runs a full matching, then tests that the tmats, dispCell, and first two dmins are the same'''
cleanup()
Expand Down Expand Up @@ -81,7 +73,7 @@ def test_matching():
# [ 6.65388928e+01 1.67598176e-01 -1.17542355e-02]
cleanup()


@pytest.mark.skip(reason="Currently borked...")
def test_crystallography():
test_tmat = [[-8.03921569e-01, -8.03921569e-01, 0],
[ 8.03921569e-01, -8.03921569e-01, 0],
Expand Down
21 changes: 20 additions & 1 deletion tests/test_units.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from p2ptrans import read, analysis
import p2ptrans
import os, glob


BCC_file = './BCC_POSCAR'
Expand All @@ -9,7 +11,13 @@
def read_FCC_BCC():
BCC = read.poscar(BCC_file)
FCC = read.poscar(FCC_file)
return BCC, FCC
return BCC, FCC

def cleanup():
for dat in glob.iglob('*dat'):
os.remove(dat)
if os.path.exists('progress.txt'):
os.remove('progress.txt')

def test_read_poscars():
'''Test that pylada reads test files correctly'''
Expand Down Expand Up @@ -48,3 +56,14 @@ def test_read_cryst():
assert ccell2 == [[9., 8., 7.],[6., 5., 4.],[3., 2., 1.]]
assert planehkl == [1, 2, 3]
assert diruvw == [3, 2, 1]

def test_optimize():
cleanup()
BCC, FCC = read_FCC_BCC()
BCC_cell = BCC.cell * BCC.scale
FCC_cell = FCC.cell * FCC.scale
# optimization(A, Acell, mulA, B, Bcell, mulB, ncell, filename, outdir, max_cell_size)
result = p2ptrans.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)
cleanup()
47 changes: 47 additions & 0 deletions tests/time_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from p2ptrans import analysis, findMatching
import pytest
import os, glob
import numpy as np
from test_units import read_FCC_BCC, BCC_file, FCC_file, tol, cleanup
import cProfile


def default_options():
# (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)

if __name__ == "__main__":
'''Runs a full matching, then tests that the tmats, dispCell, and first two dmins are the same'''
cleanup()
(_, _, ncell, filename, interactive, savedisplay, outdir,
use, switch, prim, anim, vol, minimize, test, crystfile, n_steps,
showversion, map_ncell) = default_options()

BCC, FCC = read_FCC_BCC()

if not os.path.exists(outdir):
os.makedirs(outdir)

try:
with open(filename, "r") as f:
filecontent = f.readlines()
except FileNotFoundError:
filecontent = ""

ccell1, ccell2, planehkl, diruvw = analysis.readCrystParam('./FILE_DNE')

matchCode = 'findMatching(BCC, FCC, ncell, fileA=BCC_file, fileB=FCC_file,'+\
'ccellA=ccell1, ccellB=ccell2,'+\
'filename=filename, interactive=interactive,'+\
'savedisplay=savedisplay, outdir=outdir,'+\
'switch=switch, prim=prim, vol=vol,'+\
'minimize=minimize, test=test, map_ncell=map_ncell)'

cProfile.run(matchCode, 'stats')


cleanup()

3 comments on commit 3d6b833

@ftherrien
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Ash-Pera, since there is randomness in the optimization and it makes it hard to test results that are equivalent under symmetry, I suggest that we set the seeds for the random numbers (or the random numbers themselves) in Fortran to a constant such that you can compare the results with numerical precision accuracy. I could add a test variable in the namelist (p2p.in) and if test=.true. we use the same exact seed. What do you think?

@Ash-Pera
Copy link
Collaborator Author

@Ash-Pera Ash-Pera commented on 3d6b833 Nov 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking through that one.
I think we definitely need the overall test of findMatching on the small unit cell, since it is very unlikely that it won't find the same mapping (that's the point, isn't it? ;).
While my first instinct is a yes on the set seed, my concern is that it might produce false failures. Where does it consume the random numbers? Is it just for the intial rotation/translation or are they also used elsewhere? I'm thinking of a senario where the LAP part is changed at it eats a different amount of random numbers. Maybe we should ditch this specific test in favor of an overall and a smaller test of LAP.

@ftherrien
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but even for small unit cells there is still many symmetry equivalent results (100)/(100), (100)/(010), (100)/(001) which each have three combinations of directions + all the possible translations. Although the spheres themselves might not have as much symmetry. So definitely, that is one way to go about the problem, but there will be a lot of cases to consider.

Yes, the randomness is only in the initial rotation and translation.

I will let you decide which way you want to go. If you need my help to implement a testing fixed seed in Fortran, let me know.

Also, I suggest you create a Pull Request for the test branch and mark it as Work In Progress and we can take this discussion there.

Thanks so much for doing this!

Please sign in to comment.