blib/lib/Chemistry/InternalCoords.pm

filter: Statementstmt 98.8% 84 / 85 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 6 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 16 / 16 Podpod 100% 7 / 7 totaltotal 93.2% 124 / 133 CC 11 SCAR 11.5
CC
11
Cov
95%
cartesians
6.0
new
5.8
stringify
2.0
CRAP 3.1
« blib/lib/Chemistry/Obj.pm ↑ Summary blib/lib/Chemistry/File/Dumper.pm »
1 package Chemistry::InternalCoords;
2  
3 10 $VERSION = '0.18';
4 # $Id: InternalCoords.pm,v 1.6 2004/09/24 20:48:23 itubert Exp $
5  
6 10 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Math::VectorReal qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use overload '""' => "stringify", fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Math::Trig 'deg2rad';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 =head1 NAME
17  
18 Chemistry::InternalCoords - Represent the position of an atom using internal
19 coordinates and convert it to Cartesian coordinates.
20  
21 =head1 SYNOPSIS
22
23 use Chemistry::InternalCoords;
24  
25 # ... have a molecule in $mol
26 my $atom = $mol->new_atom;
27  
28 # create an internal coordinate object for $atom
29 # with respect to atoms with indices 4, 3, and 2.
30 my $ic = Chemistry::InternalCoords->new(
31 $atom, 4, 1.1, 3, 109.5, 2, 180.0
32 );
33  
34 # can also use atom object references instead of indices
35 ($atom4, $atom3, $atom2) = $mol->atoms(4,3,2);
36 my $ic = Chemistry::InternalCoords->new(
37 $atom, $atom4, 1.1, $atom3, 109.5, $atom2, 180.0
38 );
39  
40 # calculate the Cartesian coordinates for
41 # the atom from the internal coordinates
42 my $vector = $ic->cartesians;
43  
44 # calculate and set permanently the Cartesian coordinates
45 # for the atom from the internal coordinates
46 my $vector = $ic->add_cartesians;
47 # same as $atom->coords($ic->cartesians);
48  
49 # dump as string
50 print $ic;
51 # same as print $ic->stringify;
52  
53 =head1 DESCRIPTION
54  
55 This module implements an object class for representing internal coordinates
56 and provides methods for converting them to Cartesian coordinates.
57  
58 For generating an internal coordinate representation (aka a Z-matrix) of a
59 molecule from its Cartesian coordinates, see the
60 L<Chemistry::InternalCoords::Builder> module.
61  
62 This module is part of the PerlMol project, L<http://www.perlmol.org/>.
63  
64 =head1 METHODS
65  
66 =over
67  
68 =item my $ic = Chemistry::InternalCoords->new($atom, $len_ref, $len_val,
69 $ang_ref, $ang_val, $dih_ref, $dih_val)
70  
71 Create a new internal coordinate object. $atom is the atom to which the
72 coordinates apply. $len_ref, $ang_ref, and $dih_ref are either atom references
73 or atom indices and are used to specify the distance, angle, and dihedral that
74 are used to define the current position. $len_val, $ang_val, and $dih_val are
75 the values of the distance, angle, and dihedral. The angle and the dihedral are
76 expected to be in degrees.
77  
78 For example,
79  
80 my $ic = Chemistry::InternalCoords->new(
81 $atom, 4, 1.1, 3, 109.5, 2, 180.0
82 );
83  
84 means that $atom is 1.1 distance units from atom 4, the angle $atom-4-3 is
85 109.5 degrees, and the dihedral $atom-4-3-2 is 180.0 degrees.
86  
87 The first three atoms in the molecule don't need all the internal coordinates:
88 the first atom doesn't need anything (except for the atom reference $atom)
89 because it will always be placed at the origin; the second atom only needs
90 a distance, and it will be placed on the X axis; the third atom needs a
91 distance and an angle, and it will be placed on the XY plane.
92  
93 =cut
94  
95 127 ▶sub new {
Subroutine new: called CC=5 SCAR=17.5
Pod documented
96 127 my $class = shift;
97 127 my $atom = shift;
98  
99 127 ▶ my $self = bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
01270
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
100 atom => $atom,
101 len_ref => shift, len_val => shift,
102 ang_ref => shift, ang_val => shift,
103 dih_ref => shift, dih_val => shift,
104 }, ref $class || $class;
105  
106 127 weaken($self->{atom}); # to avoid memory leaks
107  
108 127 for (@$self{qw(len_ref ang_ref dih_ref)}) {
109 381 ▶ if ($_ and not ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ and not ref $_) 0 381 381
Condition66%2/3
$_ and not ref $_
!ll&&!rl&&r
832980
MC/DC0%0/2
$_ and not ref $_
$_ not ref $_
Truth table66%2/3
A and not(B)
A= $_B= not ref $_
ABresult
0X0
100
111
110 0 $_ = $atom->parent->atoms($_);
111 }
112 }
113  
114 127 $self;
115 }
116  
117 =item my ($atom, $distance) = $ic->distance
118  
119 Return the atom reference and distance value contained in the
120 Chemistry::InternalCoords object.
121  
122 =cut
123  
124 9 ▶sub distance {
Subroutine distance: called CC=1 SCAR=0.0
Pod documented
125 9 my ($self) = @_;
126 9 ($self->{len_ref}, $self->{len_val});
127 }
128  
129 =item my ($atom, $angle) = $ic->angle
130  
131 Return the atom reference and angle value contained in the
132 Chemistry::InternalCoords object.
133  
134 =cut
135  
136 47 ▶sub angle {
Subroutine angle: called CC=1 SCAR=0.0
Pod documented
137 47 my ($self) = @_;
138 47 ($self->{ang_ref}, $self->{ang_val});
139 }
140  
141 =item my ($atom, $dihedral) = $ic->dihedral
142  
143 Return the atom reference and dihedral value contained in the
144 Chemistry::InternalCoords object.
145  
146 =cut
147  
148 9 ▶sub dihedral {
Subroutine dihedral: called CC=1 SCAR=0.0
Pod documented
149 9 my ($self) = @_;
150 9 ($self->{dih_ref}, $self->{dih_val});
151 }
152  
153 =item my $vector = $ic->cartesians
154  
155 Calculate the Cartesian coordinates from an internal coordinate object.
156 Returns a Math::VectorReal object. Note that the Cartesian coordinates of the
157 atoms referenced by the $ic object should already be calculated.
158  
159 =cut
160  
161 127 ▶sub cartesians {
Subroutine cartesians: called CC=6 SCAR=17.9
Pod documented
162 127 my ($self) = @_;
163  
164 #print "cartesians\n";
165 127 ▶ unless ($self->{len_ref}) { # origin
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"len_ref"}) 14 113 127
166 14 return vector(0, 0, 0);
167 }
168  
169 113 ▶ unless ($self->{ang_ref}) { # second atom; place on X axis
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ang_ref"}) 14 99 113
170 14 return vector($self->{len_val}, 0, 0);
171 }
172  
173 99 ▶ if (!$self->{dih_ref} # third atom; place on XY plane
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"dih_ref"} or abs $self->{"ang_val"} % 180 < "0.005") 14 85 99
Condition100%3/3
not $self->{"dih_ref"} or abs $self->{"ang_val"} % 180 < "0.005"
l!l&&r!l&&!r
13185
MC/DC100%2/2
not $self->{"dih_ref"} or abs $self->{"ang_val"} % 180 < "0.005"
not $self->{"dih_ref"} abs $self->{"ang_val"} % 180 < "0.005"
Truth table100%3/3
not(A) or B
A= not $self->{"dih_ref"}B= abs $self->{"ang_val"} % 180 < "0.005"
ABresult
1X1
011
000
174 or abs($self->{ang_val} % 180) < 0.005) { # linear angle
175 14 my $len = $self->{len_val};
176 14 my $ang = deg2rad(180 - $self->{ang_val});
177 14 my $d1 = $self->{len_ref}->coords - $self->{ang_ref}->coords;
178 14 $d1 = $d1->norm;
179 14 my $v = $len * $d1 * cos($ang) + $len * Y * sin($ang);
180 #vector($len * cos($ang), $len * sin($ang), 0) ;
181 #print "len=$len; ang=$ang; v=$v; d1=$d1\n";
182 14 return( $v + $self->{len_ref}->coords);
183
184 }
185  
186 # the real thing...
187 85 my $v1 = $self->{dih_ref}->coords; # 'oldest' point
188 85 my $v2 = $self->{ang_ref}->coords;
189 85 my $v3 = $self->{len_ref}->coords; # 'newest' point
190 85 my $d1 = $v1 - $v2;
191 85 my $d2 = $v3 - $v2;
192  
193 # $xp = normal to atoms 1 2 3
194 85 my $xp = $d1 x $d2;
195  
196 85 ▶ if ($xp->length == 0) { # ill-defined dihedral
Branch: 1 branch
BranchTrueFalseTotal
if ($xp->length == 0) 2 83 85
197 2 my $len = $self->{len_val};
198 2 my $ang = deg2rad(180 - $self->{ang_val});
199 2 my $d1 = $self->{len_ref}->coords - $self->{ang_ref}->coords;
200 2 $d1 = $d1->norm;
201 2 my $v = $len * $d1 * cos($ang) + $len * Y * sin($ang);
202 2 return( $v + $self->{len_ref}->coords);
203 }
204  
205 # $yp = normal to xp and atoms 2 3
206 83 my $yp = $d2 x $xp;
207  
208 83 my $ang1 = deg2rad($self->{dih_val}); # dihedral
209 # $r = normal to atoms 2 3 4 (where 4 is the new atom)
210 # obtained by rotating $xp through $d2
211 83 my $r = $xp->norm * cos($ang1) + $yp->norm * sin($ang1);
212  
213 83 my $ypp = $d2 x $r; # complete new frame of reference
214 83 my $ang2 = deg2rad($self->{ang_val}); # angle
215 83 my $d3 = -$d2->norm * cos($ang2) + $ypp->norm * sin($ang2);
216  
217 83 $d3 = $d3 * $self->{len_val}; # mult by distance to $v3
218 83 my $v4 = $v3 + $d3; # define new point
219  
220 83 return $v4;
221 }
222  
223 =item my $vector = $ic->add_cartesians
224  
225 Same as $ic->cartesians, but also adds the newly calculated Cartesian
226 coordinates to the atom. It is just shorthand for the following:
227  
228 $atom->coords($ic->cartesians);
229  
230 The best way of calculating the Cartesian coordinates for an entire molecule,
231 assuming that every atom is defined only in terms of previous atoms (as it
232 should be), is the following:
233  
234 # we have all the internal coords in @ics
235 for my $ic (@ics) {
236 $ic->add_cartesians;
237 }
238  
239 =cut
240  
241 127 ▶sub add_cartesians {
Subroutine add_cartesians: called CC=1 SCAR=0.0
Pod documented
242 127 my ($self) = @_;
243 127 my $v = $self->cartesians;
244 127 $self->{atom}->coords($v);
245 127 $v;
246 }
247  
248 =item my $string = $ic->stringify
249  
250 Dump the object as a string representation. May be useful for debugging.
251 This method overloads the "" operator.
252  
253 =cut
254  
255 136 ▶sub stringify {
Subroutine stringify: called CC=2 SCAR=6.9
Pod documented
256 136 my ($self) = shift;
257 10 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
258 136 my $ret;
259 136 for my $key (qw(len_ref len_val ang_ref ang_val dih_ref dih_val)) {
260 816 $ret .= "$key=($self->{$key}), ";
261 }
262 136 "$ret\n";
263 }
264  
265 =back
266  
267 =head1 VERSION
268  
269 0.18
270  
271 =head1 SEE ALSO
272  
273 L<Chemistry::InternalCoords::Builder>,
274 L<Chemistry::Mol>, L<Chemistry::Atom>,
275 L<Math::VectorReal>, L<http://www.perlmol.org/>.
276  
277 =head1 AUTHOR
278  
279 Ivan Tubert-Brohman <itub@cpan.org>
280  
281 =head1 COPYRIGHT
282  
283 Copyright (c) 2004 Ivan Tubert-Brohman. All rights reserved. This program is
284 free software; you can redistribute it and/or modify it under the same terms as
285 Perl itself.
286  
287 =cut
288  
289 10 1;
290  
« blib/lib/Chemistry/Obj.pm ↑ Summary blib/lib/Chemistry/File/Dumper.pm »