Welcome To Support Community

Materials Studio

Advanced Search
Ask Search:

the code to obtain the unit vector from water dipole moment

Dear all, I am trying to obtain the unit vector of the dipole moment (a vector) using sub Normalize.

 # get dipole moment
     my $dipoleMoment = $setMolecule->DipoleMoment;
       # get the unit vector of dipole moment
     my $direction=Normalize($dipoleMoment);

sub Normalize {}

The logic is unit vector=±a/|a|. For example, if I want to have the unit vector of (2,-3,6), the calculation process is unit vector=±(2,-3,6)/√4+9+36=±(2/7,-3/7,6/7).

$dipoleMoment is a vector with (X, Y, Z), I am trying to have the unit vector of $dipoleMoment, would you please write sub Normalize to fulfill this calculation?
Best Answer chosen by windcrazy86
Jason DeJoannisJason DeJoannis
my $doc = $Documents{"water.xsd"};
my $mol = $doc->Molecule;
my $unitvector = Normalize($mol->DipoleMoment);

# this subroutine can be applied to any Point() object
sub Normalize
    my $p = shift;
    my $length = sqrt($p->X*$p->X +    $p->Y*$p->Y + $p->Z*$p->Z);
    $p->X /= $length;
    $p->Y /= $length;
    $p->Z /= $length;
    return $p;

sub Print
    my $p = shift;
    my $length = sqrt($p->X*$p->X +    $p->Y*$p->Y + $p->Z*$p->Z);
    printf "%.3f %.3f %.3f length=%f\n", $p->X, $p->Y, $p->Z, $length;

1.380 1.308 1.305 length=2.306240
0.599 0.567 0.566 length=1.000000