How to calculate the minimum distance between atom?(Append PS in 2020/12/01)

Dear all.

I am currently trying to find the shortest interatomic distance in a cell.
This is because we are trying to predict the electrical properties of metals and crystals from that value (although there are others).

I think sample file of Materials Script(ex. CalcUniqueBondLengthAngles.pl), but this code wasn't helpful as it's supposed to contain join information(use "collection = source->Bonds").

How to calculate the minimum distance between atom(Atoms Distance under fig.)??

Thank You!

PS(2020/12/01).
I write this code.
This code's flow is
"select two atoms -> calculate distance -> remove Duplicate value"

However, this code is heavy because it calculates (ex. A_B and B_A) for all combinations.
Can you give me some advice on how to improve this code and make it lighter?

#!perl
use strict;
use warnings;
use MaterialsScript qw(:all);

use Math::Trig;

sub Length {
  my (\$atom1, \$atom2) = @_;
  return sqrt((\$atom1->X - \$atom2->X)**2 + (\$atom1->Y - \$atom2->Y)**2 + (\$atom1->Z - \$atom2->Z)**2);
}

my \$doc = \$Documents{"urea.xsd"};
my \$atoms = \$doc->AsymmetricUnit->Atoms;
my (\$name1, \$name2, \$length, @lengths);

for (my \$i=0; \$i<\$atoms->Count; ++\$i) {
    my \$atom1 = \$doc->AsymmetricUnit->Atoms(\$i);
    for (my \$j=0; \$j<\$atoms->Count; ++\$j) {
    my \$atom2 = \$doc->AsymmetricUnit->Atoms(\$j);
    if (\$atom1->X == \$atom2->X and \$atom1->Y == \$atom2->Y and \$atom1->Z == \$atom2->Z){
        next;
    }
    \$length = Length(\$atom1, \$atom2);
    push(@lengths, \$length);
    }
}

my %uniq = map {\$_ => 1} @lengths;
my @uniq_lengths = keys %uniq;

@uniq_lengths = sort{\$a <=> \$b} @uniq_lengths;
print "first   min ".\$uniq_lengths[0]."\n";
print "secound min ".\$uniq_lengths[1]."\n";
print "therd   min ".\$uniq_lengths[2]."\n";