How to compute close contacts in periodics in a script?

Hello, All,

I am working on a script to insert an ion into a zeolite and optimize the position. I want to check first that the selected position is not close to any atoms, so I wrote a loop like the one below. The variable \$newAtom is the ion, which has already been inserted at a specific coordinate. It computes the distance to every other atom in the cell, and as soon as it finds one that's too close, it returns 'false'. If all distances are ok it returns 'true'.

My problem is this: sometimes \$newAtom is very very close to the periodic image of an atom rather than to one of the atoms in the cell. How can I test for this case? The obvious way is to explicitely compute distance to each atom and to periodic images, but that will be at least 6x as much work. Is there a quicker way to perform such a check?

thanks,

George

sub DistanceOK {
my (\$newAtom, \$doc) = @_;
my \$isOK = 1;
my \$atoms = \$doc->DisplayRange->Atoms;
my \$newname = \$newAtom->Name;
foreach my \$atom (@\$atoms) {
  # do not test the atom against itself
  if (\$newname ne \$atom->Name) {
   my \$distance = \$doc->CreateDistance([\$atom, \$newAtom]);
   my \$radius = (\$newAtom->CovalentRadius + \$atom->CovalentRadius);
   if (\$dist < \$radius) {
    \$isOK = 0;
    last;
   }
   \$distance->Delete;
  }
}
return \$isOK;
}