# Partition ionic MgO cluster using the implicit interface
# For testing purposes, exclude oxygen ions from region 2
# (useful if e.g. anionic ECPs are not available)

from chemsh import *
from chemsh.utils import testutils

# Cluster with QM atoms labelled
mgo = Fragment(coords='mgo_cluster_qm.pun', connect_mode='ionic')

# Origin from original cluster cutting process
# Using coordinate array for testing purposes
origin = [ 3.97900846268733, 3.97900846268733, -3.97900846268733 ]

cluster = mgo.partition(origin=origin, cutoff_boundary=6.0, radius_active=10.0,
                        qmmm_interface='implicit', interface_exclude=["O"])

cluster.save('mgo_cluster_implicit.pun', 'pun')

# Tcl-ChemShell reference
ref = Fragment(coords='tcl-chemshell/ref/ref_bulk_partition_implicit.pun', connect_mode='ionic')

# Test atoms
testutils.validate(cluster.natoms , ref.natoms)
# TODO test region labels
# testutils.validate(cluster.names  , ref.names)
testutils.validate(cluster.znums  , ref.znums)
testutils.validate(cluster.coords , ref.coords , tol=1.0e-10)
testutils.validate(cluster.charges, ref.charges, tol=1.0e-10)
testutils.validate(cluster.shellatoms, ref.shellatoms)

# Test shells
testutils.validate(cluster.shells.coreatoms, ref.shells.coreatoms)
testutils.validate(cluster.shells.coords   , ref.shells.coords , tol=1.0e-10)
testutils.validate(cluster.shells.charges  , ref.shells.charges, tol=1.0e-10)

# Test QM charge of neutral cluster
testutils.validate(cluster.totalcharge, ref.totalcharge, tol=1.0e-10)
