visualizer-writing a script to color atoms by forces

I have found a couple of script similar to what I am looking for, but they are written for Discovery Studio.  Is there anything out there for Materials Studio?

Answers 22
RA
RA 2015-05-08

Hi Kady,

Below is a script to color atoms by force in MaterialsScript. The color range is blue for the lowest force magnitude, and red for the highest, and linear interpolation in between. The test structure is from the Structures/organics folder.

The script has two loops, one to determine the force range, and one to apply the colors. A Perl array is used to store the atom data to avoid calculating the forces twice.

Cheers,

Reinier

my $doc = $Documents{"p-xylene.xsd"};
# get the forces on the atoms
my $forceMin;
my $forceMax;
my @data;
foreach my $atom (@{$doc->Atoms})
{
my $F = $atom->Force;
my $force = sqrt($F->X*$F->X+$F->Y*$F->Y+$F->Z*$F->Z);
$forceMin = !defined($forceMin) || $force < $forceMin  ? $force : $forceMin;
$forceMax = !defined($forceMax) || $force > $forceMax  ? $force : $forceMax;
push @data, [ $atom, $force ];
}
# set the colors from blue (low force) to red (high force)
my $forceRange = $forceMax - $forceMin;
for my $item (@data)
{
my $force = $item->[1];
my $red = $forceRange == 0 ? 128 : int(($force-$forceMin)/$forceRange*255);
$item->[0]->Color = RGB($red,0,255-$red);
}
comment imagecomment imagecomment imagecomment image
KG
KG 2010-07-23

Thank you!  I finally got it to work.

RA
RA 2011-12-07

Hi Ori/G,

The comment was just to say that instead of looping over the atoms twice, e.g. in the second loop

foreach my $atom (@{$doc->Atoms})

{

my $F = $atom->Force;

my $force = sqrt($F->X*$F->X+$F->Y*$F->Y+$F->Z*$F->Z);

my $red = $forceRange == 0 ? 128 : int(($force-$forceMin)/$forceRange*255);

$atom->Color = RGB($red,0,255-$red);

}

the relevant data are temporarily stored in an array, to speed up the execution.

The error you get suggests that $doc is undefined. Did you assign a value? It should be pointing to a .xsd or .xtd document containing a non periodic structure, with force information, e.g. from a Forcite Dynamics run.

I don't know Discover very well, but believe it does not support outputting force data; perhaps someone else can comment on this.

Reinier

HP
HP 2011-12-07

Hi Reinier,

what do you mean for "A Perl array is used to store the atom data.."..

I tried your script but I got "can't call method "Atoms" on an undefined value at -e line 8"...

Could you help me..??

Last question...(hopefully)

How can I extract the forces on each atoms from a Discover MD..?? Where are the forces located?

thanks in advance

G

HP
HP 2011-12-08

Hi Reinier,

I tried again with a .xtd file from Forcite MD output (plus forces).. but I I obtained the same problem..

What do you mean with "did you assign a value?"...I thougth I just need a .xtd or .xsd file with force values..

sorry for the picky questions..

Guido

HP
HP 2011-12-08

Hi

If you had notassigned a value to $doc you would see that error message e.g. running the following

#!perl

use strict;

use MaterialsScript qw(:all);

my $doc;

my $forceMin;

my $forceMax;

my @data;

foreach my $atom (@{$doc->Atoms})

{

# work

}

you would get:

Can't call method "Atoms" on an undefined value at -e line 8.

You need to something like:

my $doc = $Documents{"135benz.xsd"}; # structure

or

my $doc = $Documents{"135benz.xtd"}; # trajectory

Regards

Simon

RA
RA 2011-12-08

I don't know what is the problem; perhaps you could zip up the script and the input file and attach it to the post, so people can have a look?

RA
RA 2015-05-08

Hi Guido,

The zip contains a .xdyn and a .trj file. None of those is supported in MaterialsScript. Please have a look at the MaterialsScript API topic in the documentation for the supported document types.

Note that, as of version 5.5, Materials Studio offers a much easier way to visualize force data, by using vectors. This works for atoms, beads, molecules and substructures, shows you directions and magnitude, and can be animated too. You find this functionality using Tools > Vectors. Here is an example:

Reinier

comment imagecomment imagecomment imagecomment image
HP
HP 2011-12-09

Hi Reinier,

I am trying to draw an ionic liquic ((trihexyl)tetradecylphosphonium bis(trifluoromethylsulfonyl)imide) and I stuck at point where I have to assign the anion and the cation. I don’t know how to assign the negative charge for one part and the negative charge for the other part.

Thanks,

Ahmed

HP
HP 2011-12-09

Hi,

I still don`t understand what`s wrong..

here you can find the .xtd and the script

thanks for your time

comment imagecomment image
RA
RA 2011-12-10

Hi Ahmed, your question is not related to the current topic; please create a new discussion for it. Thanks.

HP
HP 2011-12-11

Hi Reinier,

Thanks for the prompt replay and yes I created a new discussion base on your advise. Thanks

Ahmed

HP
HP 2011-12-21

Hi Reinier,

..finally I got it!!

there was just a problem with the script...I was using!!

many thanks for your help!!

just other few questions:

1- what are the units of the force values obtained by forcite (there are both negative and positive values)??

          1a- the relation between the color & force data come from an averaged value between the forces on all the coordinates for one atom??

2-from the second way of visualizing force data (..Tools > Vectors) you showed me...is there the possibility to obtain the relation between  force value---size vectors...??

thanks again..

Guido

RA
RA 2011-12-21

Hi Guido,

Good to hear it is working now. Feel free to create new discussion topics as new questions arise!

When you press F1 with the cursor on the word "Force" in the Script Viewer, you get the Help page on this property which tell you the units (kcal/mol/Å), object type (Point), examples, etc. So $atom->Force returns a Point object, ie. a vector with 3 components, which can be positive or negative depending on the direction of the force.

The script above simply uses the magnitude of the force vector to color the atom; the foreach loop does this for each atom individually.

The Vectors tab on the Display Style dialog has a property Scale factor, which sets the ratio between the force value and the size of the vector. For instance, if the force value is 2, and the scale factor is 6 the length will be 2*6 = 12, in units of Angstrom.

Reinier

BD
BD 2014-06-03

Hi:

    I run the script above ,just repalce" p-xylene.xsd" to my cacultated file name,such as "gra.xsd" .But a error occured."Can't call method "Atoms" on an undefined value at -e line ..."

I don't how to fix this script .Please give me some advices. Thx a lot.

my $doc = $Documents{"gra.xsd"};

# get the forces on the atoms

my $forceMin;

my $forceMax;

my @data;

foreach my $atom (@{$doc->Atoms})

{

my $F = $atom->Force;

my $force = sqrt($F->X*$F->X+$F->Y*$F->Y+$F->Z*$F->Z);

$forceMin = !defined($forceMin) || $force < $forceMin  ? $force : $forceMin;

$forceMax = !defined($forceMax) || $force > $forceMax  ? $force : $forceMax;

push @data, [ $atom, $force ];

}

# set the colors from blue (low force) to red (high force)

my $forceRange = $forceMax - $forceMin;

for my $item (@data)

{

my $force = $item->[1];

my $red = $forceRange == 0 ? 128 : int(($force-$forceMin)/$forceRange*255);

$item->[0]->Color = RGB($red,0,255-$red);

Wishes

Dow

KG
KG 2014-06-06

If you read the discussion above you'll see that others ran into this same issue.  I think it can be fixed by simply putting "my $doc;" after "my $forceMax;"

BD
BD 2014-06-09

Hi:

    I run this script sucessfully,but the displayed colors almost just only red and blue .why? In fact ,it should be colorfull. desn't it? Is there something wrong with the caculation of force by

"my $force = sqrt($F->X*$F->X+$F->Y*$F->Y+$F->Z*$F->Z)" ?

   Would you help me fix this question up? Any advices will be apprieate!

   Wishes!

   Dow

BD
BD 2014-06-09

Hi,Simon:

    I run this script sucessfully,but the displayed colors almost just only red and blue .why? In fact ,it should be colorfull. desn't it? Is there something wrong with the caculation of force by

"my $force = sqrt($F->X*$F->X+$F->Y*$F->Y+$F->Z*$F->Z)" ?

   Would you help me fix this question up? Any advices will be apprieate!

   Wishes!

   Dow

KG
KG 2014-06-09

Glad you got the script working.  This is the line where you set your colors:

$item->[0]->Color = RGB ($red,0,255-$red);

You have Green set to 0, so there will be no green in your color display.

I created a loop in my script which had 5-10 color options based on the forces to get different colors

BD
BD 2014-06-09

Hi,Kady:

     Thank you for your reply.I don't understand perl language very well.I just a freshman about this.I am studying it now .How can I set more color option?I try to change 0 to another number,but just get two color too.Should I set 0 to a varies? How to set?

     Looking forward to your replying! Thank you again!

     Dow

KG
KG 2014-06-09

So, what I did was find the max and the min to get a range for the forces ($z_range_total) then color based off that value.  it's name total not because I summed them, but because I was running the script over multiple frames.

Below is a loop (located within another loop not shown), $z_ave changes as you loop over each atom

if ($z_ave >=$z_range_total*0.8){

$a->Color = RGB(255, 50, 150);

     } else {if ($z_ave >=$z_range_total*0.6){

     $a->Color = RGB(255, 150, 0);

          } else {if ($z_ave >=$z_range_total*0.4){

          $a->Color = RGB(200, 200, 0);

         

               } else {if ($z_ave >=$z_range_total*0.2){

               $a->Color = RGB(0, 200, 0);



                    } else {

                         $a->Color = RGB(0,0,0);

                         }





BD
BD 2014-06-09
Hi,I try to run the script again after modified with your advice,but the  same error occured again.Can't  call method "Atoms" on an undefined value at -e line ..." Idon't kow how to asign value to atoms ?what doea that  mean?

THX.

Dow