Welcome To Support Community

Materials Studio

Advanced Search
Ask Search:
worldnautworldnaut 

Scripting of Atom Selection

Greetings All,
I would like to write scripts taking full advantage of the equivalent complexity available in the atom selection dialog in MS. The only hint at the syntax I can find in the help files is:

Applying Further Filters

Further filters can be applied to the collection that is returned from the filter call. These calls can be chained allowing quite complex operations to be performed easily. For example the following further process the collection returned from a call to UnitCell to extract a subset of atoms from a document:

my $methaneAtoms = $doc->UnitCell->Sets("Methane")->Atoms;

A simple example for instance is I want to pick all the atoms in the unit cell with the charge == -0.57 and create two sets, one for the atoms in the upper membrane leaflet and one for the atoms in the lower membrane leaflet.

I can do so by looping through all the atoms individually, but the complexity of the filters I want to apply beg a simpler approach. My attempt at my example syntax is

my $chrgdAtoms = $doc->UnitCell->Atoms->Atom->Charge == -0.57;

The error I get is:
There is more than one object in the "Atom"s collection (function/property "Atom") at -e line 28.

I'm not sure if the problem with my syntax is on the LHS of the statement or the RHS. I'm not even sure scripting can replicate the functions of the Atom Selection dialog, but if it can, it would greatly simplify the selections I want to make.

TIA.

I have included a working solution for my simple example where the offending statement has been commented out. You will also observe my clumsy attempt to create a pair of empty collections ($tAtoms,$bAtoms) which I also don't know any better way to create. Any suggestions are appreciated.

#!perl

use strict;
use Getopt::Long;
use MaterialsScript qw(:all);

printf("################################################################################\n");
printf("#                                                                              #\n");
printf("# PARAMETER ANALYSIS - output to file results.txt                              #\n");
printf("#                                                                              #\n");
printf("################################################################################\n");
printf("\n");

my $doc = $Documents{"temp.xtd"};
my $TXTFILE = Documents->New("results.txt");

my $temp = $doc->UnitCell->Atoms(0);
   $doc->CreateSet("hook",$temp);

my $tAtoms = $doc->UnitCell->Sets("hook")->Atoms;
my $bAtoms = $doc->UnitCell->Sets("hook")->Atoms;

for (my $j = 0; $j<$doc->UnitCell->Sets("hook")->Atoms->Count; ++$j) {
   $tAtoms->Remove($doc->UnitCell->Sets("hook")->Atoms($j));
   $bAtoms->Remove($doc->UnitCell->Sets("hook")->Atoms($j));
}

#my $chrgdAtoms = $doc->UnitCell->Atoms->Atom->Charge == -0.57;

for (my $i = 0; $i<$doc->UnitCell->Atoms->Count; ++$i) {
    my $atom = $doc->UnitCell->Atoms($i);

    if($atom->Charge eq -0.569999992847443) {
        if($atom->Z > 50) {
            $tAtoms->Add($atom);}
        else {
            $bAtoms->Add($atom);}
    }

}

for (my $i = 0; $i<$tAtoms->Count; ++$i) {
    my $atom = $tAtoms->Item($i);
    $atom->Style = "CPK";
}

for (my $i = 0; $i<$bAtoms->Count; ++$i) {
    my $atom = $bAtoms->Item($i);
    $atom->Style = "CPK";
}

$doc->CreateSet("top-o's", $tAtoms);
$doc->CreateSet("bot-o's", $bAtoms);

my $centroidT = $doc->CreateCentroid($tAtoms);
my $centroidB = $doc->CreateCentroid($bAtoms);

my $thickT = $centroidT->CentroidXYZ;
my $thickB = $centroidB->CentroidXYZ;
my $thick  = $thickT->Z - $thickB->Z;

$TXTFILE->Append(sprintf "membrane thickness %.2f\n",$thick);
$TXTFILE->Close;  
reinierreinier
Hi worldnaut,

Charge is not valid filter (see help topic Scripting in Materials Studio > MaterialsScript API > Functions and Filters).

To get all atoms with a certain charge you would need to iterate over the collection, e.g.
my @atomsWithSpecificCharge;
foreach my $atom (@{$doc->UnitCell->Atoms})
{
    push(@atomsWithSpecificCharge, $atom) if $atom->Charge == -0.57;
}
Regards,
Reinier