Dear all, I am tring to calculate the angle between water diplole moments and the normal vector of a surface, the problem of "Undefined subroutine &main::Normalize called at -e line 74." appeared when I trying to get the unit vector of the dipole moment (my $direction=Normalize($dipoleMoment);). Following is the code.

#!perl use strict; use MaterialsScript qw(:all); use Math::Trig; my $doc = $Documents{"Layerhalf.xtd"}; my $setA = "Si"; my $setB = "wat"; # get the trajectory data my $trj = $doc->Trajectory; my $numFrames = $trj->NumFrames; # create a study table for the results my $std = Documents->New($doc->Name.".std"); $std->ColumnHeading(0) = "Angle (deg)"; $std->ColumnHeading(1) = "Distance (angstrom)"; my $setMolecules = $doc->UnitCell->Sets($setB)->Molecules; # run over all frames in the trajectory my $irow = 0; my $setAtoms = $doc->UnitCell->Sets($setA)->Atoms; my $plane = $doc->UnitCell->CreateBestFitPlane($setAtoms); my $bestFitPlane = $plane->BestFitPlane; for(my $frame = 201; $frame <= 300; $frame++) { $trj->CurrentFrame = $frame; # get the normal vector of the plane my $normal = $plane->BestFitPlane->UnitNormalVector; foreach my $setMolecule(@$setMolecules) { # z position of the molecule my $distance = $setMolecule->Center->Z; # get dipole moment my $dipoleMoment = $setMolecule->DipoleMoment; my $direction=Normalize($dipoleMoment); # calculate the angle between the dipole and the surface normal my $cosAngle = InProduct($direction,$normal); my $angle =180/pi*acos($cosAngle); # store the data in the study table $std->Cell($irow,0) = $angle; $std->Cell($irow++,1) = $distance; } } $doc->Discard; # discard any changes made sub InProduct{ my($a, $b) = @_; return $a->X*$b->X+$a->Y*$b->Y+$a->Z*$b->Z }

Thank you very much! Have a good day!

Winston


In reply to The problem of getting the unit vector "Undefined subroutine &main::Normalize" by windcrazy86

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.