Debian Codegraph @

attic/Xtal/example.pl

raw
1 #!/usr/bin/perl -w
2
3 ## This is a simple example of the use of the Xtal.pm module.
4 ##
5 ## Given this information about a crystal, rhombohedral AgNO3
6 ##
7 ## title = silver nitrate
8 ## space = r 3 c
9 ## a = 6.342
10 ## alpha = 47.8167
11 ## atoms
12 ## Ag 0.25000 0.25000 0.25000 Ag1 (2b)
13 ## N 0.00000 0.00000 0.00000 N1 (2a)
14 ## O 0.25000 -0.25000 0.00000 O1 (6e)
15 ##
16 ## this example will write the contents of the unit cell in cartesian
17 ## coordinates to standard output. I chose a rhombohedral cell for
18 ## this example because the volume and metric tensor are non-trivial
19 ## but still easy to calculate by hand.
20
21 use Xray::Xtal;
22
23 ## create a new cell and set its attributes
24 $cell = Xray::Xtal::Cell -> new()
25 -> make(Space_group => "r 3 c", A => 6.342, Alpha => 47.8167);
26 ## (you could also say Space_group => 161 or Space_group => "c_3v^6")
27 ## (you could also use the hexagonal setting, although the cartesian
28 ## coordinates would come out differently)
29
30 ## some work space
31 @sites = ();
32 $n = 0;
33
34 ## create the sites and define their attributes
35 $sites[$n++] = Xray::Xtal::Site -> new()
36 -> make(Element=>"Ag", X=>0.25, Y=>0.25, Z=>0.25);
37
38 $sites[$n++] = Xray::Xtal::Site -> new()
39 -> make(Element=>"N", X=>0, Y=>0, Z=>0 );
40
41 $sites[$n++] = Xray::Xtal::Site -> new()
42 -> make(Element=>"O", X=>0.25, Y=>-0.25, Z=>0 );
43
44 ## now fill out the contents of the unit cell. this line demonstartes
45 ## the relationship between the Cell and Site objects
46 $cell -> populate(\@sites);
47
48 ## the populate method fills up the contents attribute of the cell
49 ## with the complete description of the unit cell (assuming, of
50 ## course, that all the sites were defined)
51 ($contents) = $cell -> attributes("Contents");
52 @list = @$contents; # the notation gets hairy, this helps
53
54 ## $contents is an anonymous array which we want to access in list
55 ## context. Each element of that array is itself an anonymous array
56 ## describing a position in the unit cell. The first three elements
57 ## of the position description are the fractional x, y, and z
58 ## coordinates of that position. The last element is a *reference* to
59 ## the site that generated that position. This leads to a lot of
60 ## notation...
61
62 ## note that all of the positions in the unit cell have been
63 ## canonicalized to the first octant (well, actually to the first
64 ## octant in fractional coordinates. this example just happens to
65 ## also be in the first octant in Cartesian coordinates.)
66
67 print <<EOH
68 From this description of a unit cell in crystallographic notation:
69
70 title = silver nitrate
71 space = r 3 c
72 a = 6.342
73 alpha = 47.8167
74 atoms
75 Ag 0.25000 0.25000 0.25000 Ag1 (2b)
76 N 0.00000 0.00000 0.00000 N1 (2a)
77 O 0.25000 -0.25000 0.00000 O1 (6e)
78
79 We get this description in Cartesian coordinates:
80
81 EOH
82 ;
83 while (@list) {
84 $this = shift(@list);
85 ($x, $y, $z) = $cell -> metric($$this[0], $$this[1], $$this[2]);
86 ($elem) = $ {$$this[3]} -> attributes("Element");
87 printf " %-2s\t%8.5f\t%8.5f\t%8.5f$/", $elem, $x, $y, $z;
88 };
89
90 ## all done!