# Cut a cluster from a slab shell model MgO, with coordination-scaled charges

from chemsh import *
from chemsh.utils import testutils

# Note as we're reading coordinates directly in rather than using
# a c_create-like routine there is no testing for equivalent sites
# so haven't shifted any atoms or shells around as the Tcl test did
mgo = Fragment(coords='mgo_2d_coord_scaled_periodic.pun', connect_mode='ionic')

cluster = mgo.construct_cluster(origin=40,
                                radius_cluster=20.0,
                                radius_active=10.0,
                                bq_margin=7.0,
                                bq_layer=7.0,
                                bq_density=3,
                                adjust_charge='coordination_scaled')

cluster.save('mgo_coordination_scaled_scal-cart_cluster.pun', 'pun')

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

# Test atoms
testutils.validate(cluster.natoms , ref.natoms)
testutils.validate(cluster.znums  , ref.znums)
testutils.validate(cluster.coords , ref.coords , tol=1.0e-10)
# relaxed tolerance due to numerical differences in bq least squares fit
testutils.validate(cluster.charges, ref.charges, tol=1.0e-6)

# 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 connectivity
testutils.validate(cluster.conn, ref.conn)
