# Path: mpinterfaces/transformations.py
# def get_trans_matrices(n):
# def get_uv(ab, t_mat):
# def get_reduced_uv(uv, tm):
# def reduced_supercell_vectors(ab, n):
# def get_r_list(area1, area2, max_area, tol=0.02):
# def get_mismatch(a, b):
# def get_angle(a, b):
# def surface_area(cell):
# def get_area(uv):
# def remove_duplicates(uv_list, tm_list):
# def get_matching_lattices(iface1, iface2, max_area=100,
#                           max_mismatch=0.01, max_angle_diff=1,
#                           r1r2_tol=0.02, opt=False, best_match='area',
#                           return_all_matches=None):
# def get_uniq_layercoords(struct, nlayers, top=True):
# def get_interface(substrate, mat2d, nlayers_2d=2, nlayers_substrate=2,
#                                  separation=5):
# def get_aligned_lattices(sub, twod, max_area=200,
#                          max_mismatch=0.05,
#                          max_angle_diff=1, r1r2_tol=0.2, best_match='area',
#                          uv_matched=None):
# def get_all_aligned_lattices(sub, twod, max_area=200,
#                              max_mismatch=0.05, max_angle_diff=1,
#                              r1r2_tol=0.2, best_match='area'):
# def rotate_to_principal_directions(cell):
# def run_lat_match(substrate, twod_layer, match_constraints):
# def get_all_matches(sub, twod, match_constraints, write_all=False):


from mpinterfaces import transformations
from pymatgen import Structure, Lattice
import numpy as np
#### Inputs for lattice matching #####
# substrate file path
sub_file = '../test_files/POSCAR_substrate'
# twod file path
twod_file = '../test_files/POSCAR_2D'
sub = Structure.from_file(sub_file)
twod = Structure.from_file(twod_file)

# provide input match constraints as a dictionary
match_constraints = {'max_area':90, 'max_mismatch':0.06, 'max_angle_diff':2,
                     'r1r2_tol':0.04, 'separation': 2.2, 'nlayers_substrate':1,
                     'nlayers_2d':1, 'sd_layers':0, 'best_match': 'area'}
#########################################

# Lowest area (default) or minimum uv mismatched lattice match
# is returned based on 'best_match' option in match_constraints
# best_match should be either 'area' or 'mismatch'
iface, n_sub, z_ub = transformations.run_lat_match(sub, twod, match_constraints)

if iface is None:
    print ('Current twod lattice matches with substrate. No changes needed!')
else:
    strct = Structure(iface.lattice, iface.species, iface.frac_coords)
    strct.to(filename='../test_files/POSCAR_iface', fmt='poscar')


#########################################
### To write all matches to a folder in POSCAR format ###
#########################################


# Path: mpinterfaces/transformations.py
# def get_trans_matrices(n):
# def get_uv(ab, t_mat):
# def get_reduced_uv(uv, tm):
# def reduced_supercell_vectors(ab, n):
# def get_r_list(area1, area2, max_area, tol=0.02):
# def get_mismatch(a, b):
# def get_angle(a, b):
# def surface_area(cell):
# def get_area(uv):
# def remove_duplicates(uv_list, tm_list):
# def get_matching_lattices(iface1, iface2, max_area=100,
#                           max_mismatch=0.01, max_angle_diff=1,
#                           r1r2_tol=0.02, opt=False, best_match='area',
#                           return_all_matches=None):
# def get_uniq_layercoords(struct, nlayers, top=True):
# def get_interface(substrate, mat2d, nlayers_2d=2, nlayers_substrate=2,
#                                  separation=5):
# def get_aligned_lattices(sub, twod, max_area=200,
#                          max_mismatch=0.05,
#                          max_angle_diff=1, r1r2_tol=0.2, best_match='area',
#                          uv_matched=None):
# def get_all_aligned_lattices(sub, twod, max_area=200,
#                              max_mismatch=0.05, max_angle_diff=1,
#                              r1r2_tol=0.2, best_match='area'):
# def rotate_to_principal_directions(cell):
# def run_lat_match(substrate, twod_layer, match_constraints):
# def get_all_matches(sub, twod, match_constraints, write_all=False):


# Path: mpinterfaces/tests/eg_lma.py
from mpinterfaces import transformations
from pymatgen import Structure, Lattice
import numpy as np

#### Inputs for lattice matching #####
# substrate file path
sub_file = '../test_files/POSCAR_substrate'
# twod file path
twod_file = '../test_files/POSCAR_2D'
sub = Structure.from_file(sub_file)
twod = Structure.from_file(twod_file)

# provide input match constraints as a dictionary
match_constraints = {'max_area':90, 'max_mismatch':0.06, 'max_angle_diff':2,
                     'r1r2_tol':0.04, 'separation': 2.2, 'nlayers_substrate':1,
                     'nlayers_2d':1, 'sd_layers':0, 'best_match': 'area'}
#########################################

# Lowest area (default) or minimum uv mismatched lattice match
# is returned based on 'best_match' option in match_constraints
# best_match should be either 'area' or 'mismatch'
iface, n_sub, z_ub = transformations.run_lat_match(sub, twod, match_constraints)

if iface is None:
    print ('Current twod lattice matches with substrate. No changes needed!')
else:
    strct = Structure(iface.lattice, iface.species, iface.frac_coords)
    strct.to(filename='../test_files/POSCAR_iface', fmt='poscar')


#########################################
### To write all matches to a folder in POSCAR format ###
#########################################


all_interfaces = transformations.get_all_matches(sub, twod, match_constraints,