use strict;
use warnings;

use ChemPlugin;

$|=1;

my $nx = 5; my $ny = 3;
print "Link ChemPlugin instances into a $nx by $ny grid\n\n";

# Create the ChemPlugin instances
my $cp_left = new ChemPlugin();
my $cp_right = new ChemPlugin();
my $cp_bottom = new ChemPlugin();
my $cp_top = new ChemPlugin();
my @cp = ();
for (my $i=0; $i<($nx*$ny); $i++){
   push(@cp, new ChemPlugin());
}

# Link the instances into a grid
for(my $j=0; $j<$ny; $j++){
   for(my $i=0; $i<$nx; $i++){
      my $ij = $i + $j * $nx;
      if($i == 0){
         $cp[$ij]->Link($cp_left);
      }
      else {
         $cp[$ij]->Link($cp[$ij-1]);
      }
      if($j==0){
         $cp[$ij]->Link($cp_bottom);
      }
      else{
         $cp[$ij]->Link($cp[$ij-$nx]);
      }
      if($j == $ny-1){
         $cp_top->Link($cp[$ij]);
      }
   }
   $cp_right->Link($cp[($j+1)*$nx-1]);

}

# Report the number of links to each instance
print "Left boundary is linked to ". $cp_left->nLinks() ." instances\n";
print "Bottom boundary is linked to ". $cp_bottom->nLinks() ." instances\n";

for (my$i=0; $i<($nx*$ny); $i++){
   printf "Instance %2d is linked to %d instances\n", $i, $cp[$i]->nLinks();
}

print "Top boundary is linked to ". $cp_top->nLinks() ." instances\n";
print "Right boundary is linked to ". $cp_right->nLinks() ." instances\n";


