Debian Codegraph @

attic/Xtal/Xtal.pm

raw
1 #! /usr/bin/perl -w
2 ######################################################################
3 ## This module is copyright (c) 1998-2004 Bruce Ravel
4 ## <[email protected]>
5 ## http://feff.phys.washington.edu/~ravel/software/Xray/
6 ## http://feff.phys.washington.edu/~ravel/software/atoms/
7 ##
8 ## -------------------------------------------------------------------
9 ## All rights reserved. This program is free software; you can
10 ## redistribute it and/or modify it under the same terms as Perl
11 ## itself.
12 ##
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ## Artistic License for more details.
17 ## -------------------------------------------------------------------
18 ##
19 ######################################################################
20 ##
21 ## This is the Xray::Xtal.pm module. It contains the Xtal, Cell, and
22 ## Site packages which are used to define objects for a unit cell and
23 ## a unique site in a crystallography problem. See the pod for usage
24 ## details or the program Atoms for an implementation.
25 ##
26 ######################################################################
27 ## Time-stamp: <1999-07-19 19:06:19 bruce>
28 ######################################################################
29 ## Code:
30
31 package Xray::Xtal;
32
33 =head1 NAME
34
35 Xray::Xtal - A Perl extension for crystallography data classes
36
37 =head1 SYNOPSIS
38
39 use Xray::Xtal;
40
41 my $cell = Xray::Xtal::Cell -> new();
42 $cell -> make( Space_group=>"f m -3 m", A=>3.961, );
43
44 my @sites;
45 $sites[0] = Xray::Xtal::Site -> new();
46 $sites[0] -> make( X=>0.0, Y=>0.0, Z=>0.0, Element=>"Cu" );
47
48 $cell -> populate(\@sites);
49 $cell -> verify_cell();
50
51 The preceding lines define a unit cell for FCC copper.
52
53
54 =head1 DESCRIPTION
55
56 Xray::Xtal is a module defining packages for unit cell and
57 crystallographic site objects useful in a crystallography problem.
58
59 As suggested in the synopsis, these objects are closely related to one
60 another. In fact, the method of translating a unique site to a set of
61 symmetry-related sites requires a Cell object as an argument.
62 Similarly, the method of populating a unit cell requires a list of
63 Site objects as its argument. Consequently it is very unusual to use
64 one of these classes and not the other.
65
66 In its current form, Xtal.pm is intended to solve simple
67 crystallography problems of the sort commonly encountered by the X-ray
68 absorption spectroscopist. Given a description of a crystal in terms
69 of a space group symbol, axis lengths and angles, and the fractional
70 coordinates of the unique crystallographic sites, F<Xtal.pm> is used
71 to describe the entire contents of the unit cell either in fractional
72 or Cartesian coordinates.
73
74 To this end, two packages are included in Xtal.pm which define two
75 data classes. One is a Cell and the other is a Site. Alone,
76 F<Xtal.pm> provides only these data classes. This, however, is the
77 basis of several useful applications. The initial use of this module
78 is to create lists of atomic coordinates of the sort needed for
79 ball-and-stick figures or real-sapce multiple scattering calculations
80 (which is the reason F<Xtal.pm> was created). F<Xtal.pm> can also be
81 useful, for example, for powder diffraction or anomalous scattering
82 simulations.
83
84 F<Xtal.pm> obtains its crystallography data from a database which
85 comes with the distribution. The space groups database is stored to
86 disk using the C<Storable> module with portable binary ordering. This
87 choice allows both speed and networked applicability.
88
89 The following CPAN modules may not be part of a normal perl
90 installation, but are needed by F<Xtal.pm>:
91
92 Storable, File::Spec, Chemistry::Elements
93
94 =head1 THE CELL AND SITE PACKAGES.
95
96 =cut
97 ;
98
99 ## use Safe;
100 ## Storable:
101 use Storable;
102 ##end Storable:
103 ##MLDBM:
104 ## use MLDBM qw(DB_File Storable);
105 ## use Fcntl;
106 ##end MLDBM:
107
108 use File::Spec;
109 use Ifeffit::FindFile;
110 my $data_dir = Xray::Xtal::identify_self();
111 my $dbfile = ($Ifeffit::FindFile::is_windows) ?
112 Ifeffit::FindFile->find("atoms", "space_group_db") :
113 File::Spec->catfile($data_dir, "space_groups.db");
114 ##Storable:
115 use vars qw($r_space_groups);
116 $r_space_groups = retrieve($dbfile);
117 ##end Storable:
118 ##MLDBM:
119 ## tie my %space_groups, 'MLDBM', $dbfile, O_RDONLY or die $!;
120 ## my $r_space_groups = \%space_groups;
121 ##end MLDBM:
122
123
124
125
126 ######################################################################
127
128 use strict;
129 use Carp;
130 use vars qw($VERSION $cvs_info $cvs_version $sg_version
131 @ISA @EXPORT @EXPORT_OK);
132
133 require Exporter;
134
135 @ISA = qw(Exporter AutoLoader);
136 ## @EXPORT_OK = qw();
137 $cvs_info = '$Id: Xtal.pm,v 1.30 2001/09/21 18:17:11 bruce Exp $ ';
138 $cvs_version = (split(' ', $cvs_info))[2];
139 $VERSION = 0.31;
140 $sg_version = $$Xray::Xtal::r_space_groups{'version'};
141
142 use vars qw($run_level $xtal_warnings $xtal_fatals);
143 $run_level = 0; # 0 = command line (warn is ok)
144 # 1 = Tk (always die, never warn)
145 # 2 = CGI (no error channel, use storage bins)
146 # 3 = testing (no error channel, use storage bins, fragile)
147
148 ## these are bins for storing warning and error messages. they are
149 ## used in situations where STDERR is not convenient, such as CGI.
150 ## they are cleared each time a new cell item is created.
151 $xtal_warnings = "";
152 $xtal_fatals = "";
153
154 ## method for getting attributes of a Site or Cell object
155 ## this is the sole method in the Xray::Xtal class and is
156 ## inherited by both the Site and Cell classes.
157 ## input: a list of attributes
158 ## output: a list of attribute values in the order specified on input.
159 ## an unknown attribute returns undef.
160 sub attributes {
161 my $self = shift;
162 my @ret = ();
163 foreach my $att (@_) {
164 if (exists($self->{ucfirst($att)})) {
165 push @ret, $self->{ucfirst($att)} ;
166 } else {
167 push @ret, undef;
168 };
169 };
170 return @ret;
171 };
172
173 ## return the path to this file, this seems to be a pretty bomber way
174 ## of doing that
175 sub identify_self {
176 my @caller = caller;
177 use File::Basename qw(dirname);
178 return dirname($caller[1]);
179 };
180
181 ## does the right thing for different run levels
182 sub trap_error {
183 my ($message, $just_warn) = @_;
184 if ($run_level == 0) { # command line
185 if ($just_warn) {
186 warn $message, $/;
187 return;
188 } else {
189 die $message, $/;
190 };
191 } elsif ($run_level == 1) { # Tk
192 $::top -> messageBox(-icon => 'error',
193 -message => $message,
194 -title => 'Atoms: Error',
195 -type => 'OK');
196 return;
197 } elsif ($run_level == 2) { # CGI
198 if ($just_warn) {
199 $xtal_warnings .= $message . $/;
200 } else {
201 $xtal_fatals .= $message . $/;
202 };
203 } elsif ($run_level == 3) { # testing
204 if ($just_warn) {
205 $xtal_warnings .= $message . $/;
206 } else {
207 $xtal_fatals .= $message . $/;
208 };
209 } else {
210 croak "Programmer error. Invalid run level. 0=>cli, 1=>tk, 2=>cgi, 3=>test"
211 };
212 };
213
214
215 ## Internationalization:
216
217 use vars qw($languages $strings);
218 my $libdir = (($^O eq 'MSWin32') or ($^O eq 'cygwin')) ?
219 Ifeffit::FindFile->find("atoms", "xray_lib") :
220 File::Spec->catfile($data_dir, "lib");
221 my $languagerc = File::Spec->catfile($libdir, 'languages');
222
223 sub set_language {
224 ##eval "do '$languagerc'" or warn "Language rc file not found. Using English.$/";
225 my $xtal_language;
226 if (not $languages) {
227 $xtal_language = File::Spec->catfile($libdir, "xtalrc.en");
228 } else {
229 $xtal_language = "xtalrc." . $$languages{lc($_[0])};
230 $xtal_language = File::Spec->catfile($libdir, $xtal_language);
231 unless (-e $xtal_language) {
232 $xtal_language = File::Spec->catfile($libdir, "xtalrc.en");
233 };
234 };
235 eval "do '$xtal_language'"; # read strings
236 };
237
238 &set_language("english");
239
240 1;
241
242 ######################################################################
243
244 package Xray::Xtal::Cell;
245
246 use constant EPSILON => 0.00001;
247 use constant PI => 4*atan2(1,1);
248
249 use strict;
250 use vars qw(@ISA @EXPORT @EXPORT_OK);
251
252 require Exporter;
253 require Xray::Xtal;
254
255 @ISA = qw(Exporter AutoLoader Xray::Xtal);
256 ## @EXPORT_OK = qw();
257
258 use Carp;
259
260 # constructor for a new unit cell + initializations
261 sub new {
262 my $classname = shift;
263 my $self = {};
264 $self->{Space_group} = ""; # supplied & calculated
265 $self->{Given_group} = ""; # calculated
266 $self->{A} = 0; # supplied
267 $self->{B} = 0; # supplied | calculated
268 $self->{C} = 0; # supplied | calculated
269 $self->{Alpha} = 90; # supplied | calculated
270 $self->{Beta} = 90; # supplied | calculated
271 $self->{Gamma} = 90; # supplied | calculated
272 $self->{Angle} = ""; # calculated
273 $self->{Setting} = 0; # calculated
274 $self->{Contents} = 0; # calculated
275 $self->{Bravais} = (); # calculated
276 $self->{Volume} = 1; # calculated
277 $self->{Txx} = 0; # calculated
278 $self->{Tyx} = 0; # calculated
279 $self->{Tyz} = 0; # calculated
280 $self->{Tzx} = 0; # calculated
281 $self->{Tzz} = 0; # calculated
282 $self->{Occupancy} = 1; # supplied
283 bless($self, $classname);
284 $Xray::Xtal::xtal_warnings = "";
285 $Xray::Xtal::xtal_fatals = "";
286 return $self;
287 };
288
289 =head2 The Cell object
290
291 A unit cell is constructed by C<new();> and the attributes of the unit
292 cell are set using the C<make> method. An example of the use of
293 C<make> to set the a lattice constant:
294
295 $cell -> new() -> make( A=4.0 );
296
297 The attributes of the Cell object are stored internally as capitalized
298 words i.e. words that begin with an upper case letter and have lower
299 case for the remaining letters, however the user is free to mix case
300 in any convenient manner.
301
302
303 The Cell attributes that can be set using C<make> are:
304
305 =over 4
306
307 =item B<A>
308
309 The a lattice constant.
310
311 =item B<B>
312
313 The b lattice constant.
314
315 =item B<C>
316
317 The c lattice constant.
318
319 =item B<Alpha>
320
321 The angle between the b and c lattice constants.
322
323 =item B<Beta>
324
325 The angle between the a and c lattice constants.
326
327 =item B<Gamma>
328
329 The angle between the a and b lattice constants.
330
331 =item B<Angle>
332
333 This takes the value of the most recently set angle. This is only
334 needed for the peculiar situation of a monoclinic space group with all
335 three angles equal to 90. The function determine monoclinic will not
336 be able to resolve the setting in that situation without a little
337 help. The idea is that the user has to specify at least one angle in
338 order to unambiguously determine the setting.
339
340 =item B<Space_group>
341
342 A string specifying the space group of the cell. The supplied value is
343 stored in the C<Given_group> attribute and this is filled with the
344 canonical symbol.
345
346 =back
347
348 The bare minimum required to define is a cell is the a lattice
349 constant and the space group symbol. All other attributes have
350 sensible defaults or are calculated quantities. Of course, any space
351 group of lower than cubic symmetry will require that other axes and/or
352 angles be specified.
353
354 There are several other Cell attributes. Except for the Contents
355 attribute, these are updated every time the C<make> method is called.
356 These include:
357
358 =over 4
359
360 =item B<Given_group>
361
362 The space group symbol used as the argument for
363 the C<Space_group> attribute when the C<make> method is called.
364
365 =item B<Setting>
366
367 The setting of a low symmetry space group. See
368 below for a discussion of low symmetry space groups.
369
370 =item B<Contents>
371
372 This is an anonymous list of anonymous lists specifying the contents
373 of the fully decoded unit cell. This attribute is set by caling the
374 C<populate> method. Each list element is itself a list containing the
375 x, y, and z fractional coordinates of the site and a reference to the
376 Site obect which generated that site. To examine the contents of the
377 cell, do something like this:
378
379 my ($contents) = $cell -> attributes("Contents");
380 foreach my $pos (@{$contents}) {
381 printf "x=%8.5f, y=%8.5f, z=%8.5f$/",
382 $$pos[0], $$pos[1], $$pos[2]
383 };
384
385 =item B<Volume>
386
387 The volume of the unit cell computed from the axes and angles.
388
389 =item B<Txx>
390
391 The x-x element of the metric tensor computed from the axes and
392 angles. This is used to translate from fractional to cartesian
393 coordinates.
394
395 =item B<Tyx>
396
397 The y-x element of the metric tensor computed from the axes and
398 angles.
399
400 =item B<Tyz>
401
402 The y-z element of the metric tensor computed from the axes and
403 angles.
404
405 =item B<Tzx>
406
407 The z-x element of the metric tensor computed from the axes and
408 angles.
409
410 =item B<Tzz>
411
412 The z-z element of the metric tensor computed from the axes and
413 angles.
414
415 =item other metric tensor elements
416
417 The yy element of the metric tensor is unity and the other three are
418 zero.
419
420 =back
421
422 C<Xray::Xtal::Cell> will only allow you to directly modify the lattice
423 constants and angles and the space group using the make method. While
424 it is common for object definitions to allow the user to define new
425 attributes, that is not allowed for a Cell object. There truly only
426 are 8 attributes of a Cell which the user should allowed to set.
427
428
429 =head2 Methods and Functions in the Cell Package
430
431 There are several other function that may be useful in your programs.
432 Some of these are methods and some are non-exported functions. Except
433 as noted, these subroutines are from the C<Xray::Xtal::Cell> package.
434
435 =over 4
436
437 =item C<attributes>
438
439 Takes a list of attributes as its input and returns the values of the
440 attributes as a list in the same order as the input list of attribute
441 names. The return value is always an array, even if only one element
442 is requested. This is the sole method of the C<Xray::Xtal> package and
443 is inherited by both the Site and Cell packages.
444
445 $cell -> Xray::Xtal::Cell::new()
446 -> make("A"=>4.0, "B"=>4.3, "C"=>4.6)
447 ($a, $b, $c) = $cell -> attributes("A", "B", "C");
448
449 $site -> Xray::Xtal::Site::new()
450 -> make("X"=>0.5, "Y"=>0.25, "Z"=>0)
451 ($x, $y, $z) = $site -> attributes("X", "Y", "Z");
452
453 print "$x, $y, $z, $a, $b, $c$/";
454 |-> 0.5, 0.25, 0, 4.0, 4.3, 4.6
455
456 =back
457
458 =cut
459 ;
460 ## do error checking, canonicalize group, determine seting for low
461 ## symmetry space groups, calculate volume and metric tensor elements,
462 ## then return the current cell object basically this handles all
463 ## attributes of a cell object except for Contents. Use
464 ## $cell->populate for that purpose.
465 sub make {
466 my $self = shift;
467 ($#_ % 2) || do {
468 my $this = (caller(0))[3];
469 croak "$this takes an even number of arguments";
470 return; };
471 my @attributes = qw(Space_group A B C Alpha Beta Gamma Angle Occupancy);
472 ##my $epsilon = $Xray::Atoms::epsilon || 0.00001;
473
474 while (@_) {
475 my $att = ucfirst($_[0]);
476 unless (grep /\b$att\b/, @attributes) {
477 carp "$_[0] : You should not attempt to set this by hand";
478 shift; shift;
479 } else {
480 if ($att eq "Space_group") {
481 $self->{Given_group} = $_[1];
482 ($self->{Space_group}, $self->{Setting}) =
483 canonicalize_symbol($_[1]) ;
484 unless ($self->{Space_group}) {
485 Xray::Xtal::trap_error($$Xray::Xtal::strings{not_a_group}, 0);
486 };
487 } else {
488 $self->{ $att } = $_[1];
489 };
490 shift; shift;
491 };
492 (lc($att) =~ /(alpha|beta|gamma)/) && ($self->{Angle} = $1);
493 };
494 my $cryscls = $self->crystal_class();
495
496 ## try to recognize some easy mistakes
497 SWITCH: {
498 ($cryscls eq 'cubic') and do {
499 if (($self->{A} < EPSILON) and ($self->{B} > EPSILON) and
500 ($self->{C} < EPSILON)) {
501 $self->{A} = $self->{B};
502 } elsif (($self->{A} < EPSILON) and ($self->{B} < EPSILON) and
503 ($self->{C} > EPSILON)) {
504 $self->{A} = $self->{C};
505 };
506 last SWITCH;
507 };
508 (($cryscls eq 'tetragonal') or ($cryscls eq 'hexagonal')) and do {
509 if (($self->{A} < EPSILON) and ($self->{B} > EPSILON)) {
510 $self->{A} = $self->{B};
511 };
512 last SWITCH;
513 };
514 (($cryscls eq 'trigonal') and ($self->{Space_group} !~ /^[Rr]/)) and do {
515 if (($self->{A} < EPSILON) and ($self->{B} > EPSILON)) {
516 $self->{A} = $self->{B};
517 };
518 last SWITCH;
519 };
520 (($cryscls eq 'trigonal') and ($self->{Space_group} =~ /^[Rr]/)) and do {
521 if (($self->{A} < EPSILON) and ($self->{B} > EPSILON) and
522 ($self->{C} < EPSILON)) {
523 $self->{A} = $self->{B};
524 } elsif (($self->{A} < EPSILON) and ($self->{B} < EPSILON) and
525 ($self->{C} > EPSILON)) {
526 $self->{A} = $self->{C};
527 };
528 if (($self->{Alpha} < EPSILON) and ($self->{Beta} > EPSILON) and
529 ($self->{Gamma} < EPSILON)) {
530 $self->{Alpha} = $self->{Beta};
531 } elsif (($self->{Alpha} < EPSILON) and ($self->{Beta} < EPSILON) and
532 ($self->{Gamma} > EPSILON)) {
533 ($self->{Alpha} = $self->{Gamma}) unless (abs($self->{Gamma}-120) < EPSILON);
534 };
535 last SWITCH;
536 };
537 };
538
539 $self->{B} ||= $self->{A}; # B and C default to the value of A
540 $self->{C} ||= $self->{A};
541 ($self->{B} < EPSILON) and $self->{B} = $self->{A};
542 ($self->{C} < EPSILON) and $self->{C} = $self->{A};
543 unless ($self->{Alpha}) {$self->{Alpha} = 90};
544 unless ($self->{Beta}) {$self->{Beta} = 90};
545 unless ($self->{Gamma}) {$self->{Gamma} = 90};
546 ($self->{Alpha} < EPSILON) and $self->{Alpha} = 90;
547 ($self->{Beta} < EPSILON) and $self->{Beta} = 90;
548 ($self->{Gamma} < EPSILON) and $self->{Gamma} = 90;
549
550 # Rhombohedral groups
551 ($self->{Space_group} =~ /^[Rr]/) && do {
552 if (abs(90-$self->{Alpha}) > EPSILON) {
553 $self->{Setting} = "rhombohedral";
554 $self->{B} = $self->{A}; # this flags the use of the rhomb. positions
555 $self->{C} = $self->{A};
556 $self->{Beta} = $self->{Alpha};
557 $self->{Gamma} = $self->{Alpha};
558 ## } elsif ($self->{C} != $self->{A}) {
559 ## $self->{Setting} = 0;
560 ## $self->{B} = $self->{A};
561 ## $self->{Alpha} = 90;
562 ## $self->{Beta} = 90;
563 ## $self->{Gamma} = 120;
564 } else {
565 $self->{Setting} = 0;
566 $self->{B} = $self->{A};
567 $self->{Alpha} = 90;
568 $self->{Beta} = 90;
569 $self->{Gamma} = 120;
570 };
571 };
572 # Trigonal and Hexagonal
573 ((($self -> crystal_class() eq "trigonal") ||
574 ($self -> crystal_class() eq "hexagonal")) &&
575 ($self->{Space_group} !~ /^[Rr]/)) && do {
576 $self->{B} = $self->{A};
577 $self->{Alpha} = 90;
578 $self->{Beta} = 90;
579 $self->{Gamma} = 120;
580 };
581
582 ## now is the right time to set this...
583 $self->{Bravais} = [bravais($self->{Space_group},$self->{Setting})];
584
585 # Monoclinic
586 if ($self -> crystal_class() eq "monoclinic") {
587 $self->{Setting} = determine_monoclinic($self);
588 $self->{Bravais} = [bravais($self->{Given_group},0)];
589 };
590 # Set metric tensor elements (this is
591 # some ugly stuff!)
592 my $alpha = $self->{Alpha} * PI / 180;
593 my $beta = $self->{Beta} * PI / 180;
594 my $gamma = $self->{Gamma} * PI / 180;
595 my $cosxx = ( cos($alpha)*cos($gamma) - cos($beta) ) /
596 ( sin($alpha)*sin($gamma) );
597 my $cosyy = ( cos($alpha)*cos($beta) - cos($gamma) ) /
598 ( sin($alpha)*sin($beta) );
599 # careful for the sqrt!
600 my $sinxx = ($cosxx**2 < 1) ? sqrt(1-$cosxx**2) : 0;
601 my $sinyy = ($cosyy**2 < 1) ? sqrt(1-$cosyy**2) : 0;
602 $self->{Txx} = sprintf "%11.7f", $sinyy*sin($beta);
603 $self->{Tyx} = sprintf "%11.7f", -( ($cosyy/($sinyy*sin($alpha)) ) +
604 (cos($alpha)*$cosxx)/($sinxx*sin($alpha)))
605 * ($sinyy*sin($beta));
606 $self->{Tyz} = sprintf "%11.7f", cos($alpha);
607 $self->{Tzx} = sprintf "%11.7f", -( $cosxx*$sinyy*sin($beta) ) / $sinxx;
608 $self->{Tzz} = sprintf "%11.7f", sin($alpha);
609
610 # Set cell volume
611 my $term = 1 - cos($alpha)**2 - cos($beta)**2 - cos($gamma)**2 +
612 2*cos($alpha)*cos($beta)*cos($gamma);
613 $self->{Volume} = $self->{A} * $self->{B} * $self->{C} * sqrt($term);
614
615 return $self;
616 };
617
618
619 =over 4
620
621 =item C<clear>
622
623 Reset a cell without destroying it.
624
625 $cell -> clear();
626
627 =back
628
629 =cut
630 ## reset a cell without destroying it
631 sub clear {
632 my $self = shift;
633 $self->{Space_group} = ""; # supplied & calculated
634 $self->{Given_group} = ""; # calculated
635 $self->{A} = 0; # supplied
636 $self->{B} = 0; # supplied | calculated
637 $self->{C} = 0; # supplied | calculated
638 $self->{Alpha} = 90; # supplied | calculated
639 $self->{Beta} = 90; # supplied | calculated
640 $self->{Gamma} = 90; # supplied | calculated
641 $self->{Angle} = ""; # calculated
642 $self->{Setting} = 0; # calculated
643 $self->{Contents} = 0; # calculated
644 $self->{Bravais} = (); # calculated
645 $self->{Volume} = 1; # calculated
646 $self->{Txx} = 0; # calculated
647 $self->{Tyx} = 0; # calculated
648 $self->{Tyz} = 0; # calculated
649 $self->{Tzx} = 0; # calculated
650 $self->{Tzz} = 0; # calculated
651 $self->{Occupancy} = 1; # supplied
652 return $self;
653 };
654
655 =over 4
656
657 =item C<populate>
658
659 Populate a unit cell given a list of sites. Each element of the list
660 of sites must be a Site object. The symmetries operations implied by
661 the space group are applied to each unique site to generate a
662 description of the stoichiometric contents of the unit cell.
663
664 $cell -> populate(\@sites)
665
666 This fills the C<Contents> attribute of the Cell with an anonymous
667 array. Each element of the anonymous array is itself an anonymous
668 array whose first three elements are the x, y, and z fractional
669 coordinates of the site and whose fourth element is a reference to the
670 Site that generated the position. This is, admitedly, a complicated
671 data structure and requires a lot of ``line-noise'' style perl to
672 dereference all its elements. It is, however, fairly efficient.
673
674 =back
675
676 =cut
677
678 sub populate {
679 my $self = shift;
680 my $r_sites = $_[0];
681 my @unit_cell = ();
682
683 my $cnt = 0;
684 my %seen; # need unique tags for formulas
685 foreach my $site (@{$r_sites}) {
686 ++$cnt;
687 if ($seen{$site->{Tag}}++) {
688 $site->{Utag} = $site->{Tag} . '_' . $cnt;
689 } else {
690 $site->{Utag} = $site->{Tag};
691 };
692 };
693 my $crystal_class = $self -> crystal_class();
694 my $setting = $self->{Setting};
695 my $do_tetr = ($crystal_class eq "tetragonal" ) && ($setting);
696 if ($do_tetr) {
697 my ($a, $b) = ($self->{A}, $self->{B});
698 $self -> make(A=>$a/sqrt(2), B=>$b/sqrt(2));
699 };
700 foreach my $site (@{$r_sites}) {
701 $site -> populate($self);
702 my ($t) = $site -> attributes("tag");
703 my $cnt = 0;
704 foreach my $list (@{$site->{Positions}}) {
705 ## ($t eq "V1-5") and print join(" ", @$list[0..2], $/);
706 my $form = $ {$site->{Formulas}}[$cnt];
707 push @unit_cell, [$$list[0], $$list[1], $$list[2], \$site, @$form];
708 ++$cnt;
709 };
710 };
711 $self->{Contents} = [@unit_cell];
712 #---------------------------- Check for repeats.
713 my %occ = ();
714 %seen = (); # similar to section 4.6, p.102,
715 foreach my $item (@unit_cell) { # The Perl Cookbook, 1st edition
716 my $keya = sprintf "%7.5f", $$item[0]; chop $keya;
717 my $keyb = sprintf "%7.5f", $$item[1]; chop $keyb;
718 my $keyc = sprintf "%7.5f", $$item[2]; chop $keyc;
719 my $key = $keya . $keyb . $keyc;
720 (exists $seen{$key}) && do {
721 my ($that, $this) = ($ {$seen{$key}->[3]}->{Tag}, $ {$$item[3]}->{Tag});
722 $occ{$key}->[0] += $ {$$item[3]}->{Occupancy};
723 push @{$occ{$key}}, $this; # add tag to list
724 ## flag this as a dopant
725 $ {$$item[3]} -> make(Host=>0);
726 ## croak "The sites \"" . $this . "\" and \"" . $that .
727 ## "\" generate the same position in space.$/" .
728 ## "Multiple occupancy is not allowed in this program.$/" .
729 ## "This program cannot continue due to the error";
730 };
731 $seen{$key} = $item; # $ {$$item[3]}->{Tag};
732 $occ{$key} ||= [ $ {$$item[3]}->{Occupancy}, $ {$$item[3]}->{Tag} ];
733 };
734 ## now check that a site is not overly occupied
735 #if ($self->{Occupancy}) {
736 my @croak;
737 foreach my $k (keys %occ) {
738 my @list = @{$occ{$k}};
739 my $val = shift @list;
740 if ($val > (1+EPSILON)) {
741 push @croak,
742 $$Xray::Xtal::strings{these_sites} .
743 "$/\t" . join(" ", map {sprintf "\"%s\"", $_} @list) .
744 "$/" . $$Xray::Xtal::strings{occupied} ;
745 };
746 };
747 if (@croak) { # weed out repititious error messages
748 my %seen = ();
749 my @unique = ();
750 foreach my $item (@croak) {
751 unless ($seen{$item}) {
752 $seen{$item} = 1;
753 push (@unique, $item);
754 };
755 };
756 #unless ($Xray::Xtal::Site::molecule) {
757 Xray::Xtal::trap_error(join("$/", @unique), 0);
758 #};
759 };
760 #};
761 return $self;
762 };
763
764 =over 4
765
766 =item C<canonicalize_symbol>
767
768 This takes a character string representing a space group and returns
769 the canonical symbol for that group. See the Atoms document and
770 L<"INTERPRETING SPACE GROUP SYMBOLS"> below for complete details about
771 space group symbols and how they are interpretted by this function.
772 This is not a method of the Cell class, it is just a normal function.
773
774 $string = Xray::Xtal::Cell::canonicalize_symbol($string);
775
776 For example
777
778 Xray::Xtal::Cell::canonicalize_symbol('pm3m')
779 yields "P m -3 m"
780
781 =back
782
783 =cut
784
785 sub canonicalize_symbol {
786 my $symbol = $_[0];
787 my $sym;
788 # this is a null value
789 (! $symbol) && return (0,0);
790
791 $symbol = lc($symbol); # lower case and ...
792 $symbol =~ s/[!\#%*].*$//; # trim off comments
793 $symbol =~ s/^\s+//; # trim leading spaces
794 $symbol =~ s/\s+$//; # trim trailing spaces
795 $symbol =~ s/\s+/ /g; # ... single space
796 $symbol =~ s|\s*/\s*|/|g; # spaces around slash
797
798 $symbol =~ s/2_1/21/g; # replace `i 4_1' with `i 41'
799 $symbol =~ s/3_([12])/2$1/g; # and so on ...
800 $symbol =~ s/4_([1-3])/2$1/g;
801 $symbol =~ s/6_([1-5])/2$1/g;
802
803 my $shorthands = 'bcc|c(scl|ubic)|diamond|fcc|gra(|phite)|h(cp|ex)';
804 $shorthands = $shorthands . '|nacl|perov(|skite)|salt|z(incblende|ns)';
805 unless ( ($symbol =~ /[_^ ]/) or # HM with spaces, schoen
806 ($symbol =~ /\b\d{1,3}\b/) or # 1-230
807 ($symbol =~ /\b($shorthands)\b/io) # shorthands like 'cubic', 'zns'
808 ) {
809 $symbol = insert_spaces($symbol);
810 };
811 # this is the standard symbol
812 (exists($$Xray::Xtal::r_space_groups{$symbol})) && return ($symbol, 0);
813
814 foreach $sym (keys %$Xray::Xtal::r_space_groups ) {
815 next if ($sym eq "version");
816 my %hash = %{$$Xray::Xtal::r_space_groups{$sym}};
817
818 # scalar valued fields
819 # this is a number between 1 and 230
820 # or the 1935 symbol
821 # or a double glide plane symbol
822 # or the full symbol
823 foreach my $field ("thirtyfive", "new_symbol", "full") {
824 exists $hash{$field} && ($symbol eq $hash{$field}) &&
825 return ($sym, 0);
826 };
827 if ($symbol eq $hash{number}) {
828 if (($symbol > 2) and ($symbol < 16)) {
829 return ($sym, 1);
830 } else {
831 return ($sym, 0);
832 };
833 };
834 # this is the Schoenflies symbol, (it
835 # must have a caret in it)
836 ($symbol =~ /\^/) && do {
837 $symbol =~ s/\s+//g; # no spaces
838 $symbol =~ s/^v/d/g; # V -> D
839 # put ^ and _ in correct order
840 $symbol =~ s/([cdost])(\^[0-9]{1,2})(_[12346dihsv]{1,2})/$1$3$2/;
841 exists $hash{"schoenflies"} and
842 ($symbol eq $hash{"schoenflies"}) and
843 return ($sym, 0);
844 };
845 # now check the array values fields
846 foreach my $field ("settings", "short", "shorthand") {
847 (exists($hash{$field})) && do {
848 my $i=0;
849 foreach my $setting ( @{$hash{$field}} ) {
850 ++$i;
851 my $s = ($field eq "settings") ? $i : 0;
852 if ($symbol eq $setting) {
853 ##print "setting : $s\n";
854 return ($sym, $s);
855 };
856 }; # "settings" is an array field
857 };
858 };
859 };
860 # this is not a symbol
861 1 && do { return (0,0); };
862 }
863
864 ## This is the algorithm for dealing with user-supplied space group
865 ## symbols that do not have the canonical single space separating the
866 ## part of the symbol.
867 sub insert_spaces {
868 my $sym = $_[0];
869
870 my ($first, $second, $third, $fourth) = ("", "", "", "");
871
872 ## a few groups don't follow the rules below ...
873 ($sym =~ /\b([rhc])32\b/i) && return "$1 3 2";
874 ($sym =~ /\bp31([2cm])\b/i) && return "p 3 1 $1";
875 ($sym =~ /\bp(3[12]?)[22][12]\b/i) && return "p $1 2 1";
876 ($sym =~ /\bp(6[1-5]?)22\b/i) && return "p $1 2 2";
877 ($sym =~ /\b([fip])(4[1-3]?)32\b/i) && return "$1 $2 3 2";
878 ($sym =~ /\b([fipc])(4[1-3]?)(21?)(2)\b/i) && return "$1 $2 $3 $4";
879
880 ## the first symbol is always a single letter
881 $first = substr($sym, 0, 1);
882 my $index = 1;
883
884 if (substr($sym, $index, 4) =~ /([2346][12345]\/[mnabcd])/) {
885 ## second symbol as in p 42/n c m
886 $second = $1;
887 $index += 4;
888 } elsif (substr($sym, $index, 3) =~ /([2346]\/[mnabcd])/) {
889 ## second symbol as in p 4/n n c
890 $second = $1;
891 $index += 3;
892 } elsif (substr($sym, $index, 2) =~ /(-[1346])/) {
893 ## second symbol as in p -3 1 m
894 $second = $1;
895 $index += 2;
896 } elsif (substr($sym, $index, 2) =~ /(21|3[12]|4[123]|6[12345])/) {
897 ## second symbol as in p 32 1 2
898 $second = $1;
899 $index += 2;
900 } else {
901 $second = substr($sym, $index, 1);
902 $index += 1;
903 };
904
905 if (substr($sym, $index, 4) =~ /([2346][12345]\/[mnabcd])/) {
906 ## third symbol as in full symbol p 21/c 21/c 2/n
907 $third = $1;
908 $index += 4;
909 } elsif (substr($sym, $index, 3) =~ /([2346]\/[mnabcd])/) {
910 ## third symbol as in full symbol p 4/m 21/b 2/m
911 $third = $1;
912 $index += 3;
913 } elsif (substr($sym, $index, 2) =~ /(-[1346])/) {
914 ## third symbol as in f d -3 m
915 $third = $1;
916 $index += 2;
917 } elsif (substr($sym, $index, 2) =~ /(21|3[12]|4[123]|6[12345])/) {
918 ## third symbol as in p 21 21 2
919 $third = $1;
920 $index += 2;
921 } else {
922 $third = substr($sym, $index, 1);
923 $index += 1;
924 };
925
926 ($index < length($sym)) and $fourth = substr($sym, $index);
927
928 $sym = join(" ", $first, $second, $third, $fourth);
929 $sym =~ s/\s+$//; # trim trailing spaces
930 return $sym;
931 };
932
933 ## =over 4
934 ##
935 ## =item C<bravais>
936 ##
937 ## This takes the values of the C<Space_group> and C<Setting> attributes
938 ## of a Cell and returns a list which specifies the Bravais translation
939 ## vectors. This list is the truncation of the various Bravais
940 ## translation three-vectors, thus has 0, 3, 6, or 9 elements. This is
941 ## not method of the Cell class, it is just a normal function. It should
942 ## rarely be necessary to call this, as the Bravais vector gets stored in
943 ## the "Bravais" attribute of a cell.
944 ##
945 ## ($group, $setting) =
946 ## $cell -> attributes("Space_group", "Setting");
947 ## @list = Xray::Xtal::Cell::bravais($group, $setting);
948 ##
949 ## =back
950 ##
951 ## =cut
952
953 sub bravais {
954 my $group = lc(substr($_[0], 0, 1));
955 my $setting = $_[1];
956 ( $group eq "f") && return ( 0, 1/2, 1/2, 1/2, 0, 1/2, 1/2, 1/2, 0);
957 ( $group eq "i") && return (1/2, 1/2, 1/2);
958 ( $group eq "c") && return (1/2, 1/2, 0);
959 ( $group eq "a") && return ( 0, 1/2, 1/2);
960 ( $group eq "b") && return (1/2, 0, 1/2);
961 (($group eq "r") && ($setting eq 0))
962 && return (2/3, 1/3, 1/3, 1/3, 2/3, 2/3);
963 return ();
964 };
965
966
967 =over 4
968
969 =item C<crystal_class>
970
971 This returns a character string specifying the crystal class. The
972 return value is one of ``cubic'', ``hexagonal'', ``trigonal'',
973 ``tetragonal'', ``orthorhombic'', ``monoclinic'', or ``triclinic''.
974 This is a method of the Cell class.
975
976 $class = $cell -> crystal_class();
977
978 =back
979
980 =cut
981
982 sub crystal_class {
983 my $self = shift;
984 my $group = $self->{Space_group};
985 (exists $$Xray::Xtal::r_space_groups{$group}{number}) && do {
986 my $hash_element = $$Xray::Xtal::r_space_groups{$group};
987 ($$hash_element{number} <= 0) && return "";
988 ($$hash_element{number} <= 2) && return "triclinic";
989 ($$hash_element{number} <= 15) && return "monoclinic";
990 ($$hash_element{number} <= 74) && return "orthorhombic";
991 ($$hash_element{number} <= 142) && return "tetragonal";
992 ($$hash_element{number} <= 167) && return "trigonal";
993 ($$hash_element{number} <= 194) && return "hexagonal";
994 ($$hash_element{number} <= 230) && return "cubic";
995 return "";
996 }; # need to return something if $group not yet defined or
997 return ""; # defined incorrectly
998 };
999
1000 ## input: reference to a cell
1001 ## output: string specifying the monoclinic setting for that cell
1002 ## this is called by the make method
1003 sub determine_monoclinic {
1004 my $self = $_[0];
1005 ## need to know the space group, the user-provided symbol, and the
1006 ## unique axis (determined from the non-90 degree angle)
1007 my $group = $self->{Space_group};
1008 ($self -> crystal_class() eq "monoclinic") || return;
1009 my $given = $self->{Given_group};
1010 my $axis = 0;
1011 ((abs( 90 - $self->{Alpha} )) > EPSILON) && ($axis = "a");
1012 ((abs( 90 - $self->{Beta} )) > EPSILON) && ($axis = "b");
1013 ((abs( 90 - $self->{Gamma} )) > EPSILON) && ($axis = "c");
1014 (! $axis) && do {
1015 if ($self->{Angle}) {
1016 $axis = substr($self->{Angle}, 0, 1);
1017 ($axis = lc($axis)) =~ tr/g/c/;
1018 };
1019 };
1020 (! $axis) && do { # apparently the non-90 angle has not
1021 return 0; # been set yet...
1022 };
1023 my $number = $$Xray::Xtal::r_space_groups{$group}{number};
1024 # if it has, then continue...
1025 ($given =lc($given)) =~ s/^\s+//; # lower case and strip leading blanks
1026 $given =~ s/\s+$//; # strip trailing blanks
1027 my $setting = $axis . "_unique";
1028 if ($given eq $number) {
1029 $given = $$Xray::Xtal::r_space_groups{$group}{settings}[0];
1030 };
1031 ## these groups have one cell choice for each unique axis
1032 foreach my $n (3,4,5,6,8,10,11,12) {
1033 ($number == $n) && return $setting;
1034 };
1035 ## groups 7, 13, 14 are p centered and have multiple cell choices
1036 ($group =~ /^p/i) && do {
1037 ($axis eq "b") && do {
1038 ($given =~ /c/i) && ($setting .= "_1");
1039 ($given =~ /n/i) && ($setting .= "_2");
1040 ($given =~ /a/i) && ($setting .= "_3");
1041 };
1042 ($axis eq "c") && do {
1043 ($given =~ /a/i) && ($setting .= "_1");
1044 ($given =~ /n/i) && ($setting .= "_2");
1045 ($given =~ /b/i) && ($setting .= "_3");
1046 };
1047 ($axis eq "a") && do {
1048 ($given =~ /b/i) && ($setting .= "_1");
1049 ($given =~ /n/i) && ($setting .= "_2");
1050 ($given =~ /c/i) && ($setting .= "_3");
1051 };
1052 };
1053 ## groups 9, 15 are c centered and have multiple cell choices
1054 ($group =~ /^c/i) && do {
1055 ($axis eq "b") && do {
1056 ($given =~ /^c/i) && ($setting .= "_1");
1057 ($given =~ /^a/i) && ($setting .= "_2");
1058 ($given =~ /^i/i) && ($setting .= "_3");
1059 };
1060 ($axis eq "c") && do {
1061 ($given =~ /^a/i) && ($setting .= "_1");
1062 ($given =~ /^b/i) && ($setting .= "_2");
1063 ($given =~ /^i/i) && ($setting .= "_3");
1064 };
1065 ($axis eq "a") && do {
1066 ($given =~ /^b/i) && ($setting .= "_1");
1067 ($given =~ /^c/i) && ($setting .= "_2");
1068 ($given =~ /^i/i) && ($setting .= "_3");
1069 };
1070 };
1071 ## if none of the preceding 6 blocks altered setting then there is a
1072 ## mismatch between the symbol and the unique axis, so return 0.
1073 ($setting =~ /_[123]$/) || ($setting = 0);
1074 return $setting;
1075 };
1076
1077 ## explicitly set the value of $self->{Setting} for a monoclinic
1078 ## groups regardless of how it was set in canonicalize_coordinate.
1079 sub enforce_monoclinic {
1080 my $self = shift;
1081 (($#_ == 1)||($#_ == 2)) || do {
1082 my $this = (caller(0))[3];
1083 croak "$this takes 1 or 2 arguments arguments";
1084 return;
1085 };
1086 my ($axis,$choice) = @_;
1087 ($axis =~ /\b[abc]\b/i) || do {
1088 my $this = (caller(0))[3];
1089 croak "The unique axis must be a b or c in $this";
1090 return;
1091 };
1092 ($choice) && do {
1093 ($choice =~ /\b[123]\b/) || do {
1094 my $this = (caller(0))[3];
1095 croak "The cell choice must be 1 2 or 3 in $this";
1096 return;
1097 };
1098 };
1099 $self->{Setting} = $axis . "_unique";
1100 ($choice) && ($self->{Setting} .= "_" . $choice);
1101 $self->{Setting} = lc($self->{Setting});
1102 return $self;
1103 };
1104
1105 =over 4
1106
1107 =item C<verify_cell>
1108
1109 This performs consistency checks on the all cell attributes. If
1110 anything suspicious turns up, warning or error messages are issued.
1111
1112 $cell -> verify_cell();
1113
1114 =back
1115
1116 =cut
1117
1118 sub verify_cell {
1119 my $self = shift;
1120 my ($fault, $error, $warning, $caution) = (0, 3, 2, 1);
1121 my $normal = 0;
1122 my $message = "";
1123 # 1: verify space group
1124 my $group = $self -> {Space_group};
1125 (exists $$Xray::Xtal::r_space_groups{$group}) || do {
1126 $fault = &set_fault($fault, $error);
1127 $message .= $self->{Given_group} . " " . $$Xray::Xtal::strings{not_valid};
1128 };
1129 # 2: axes must be positive
1130 (($self->{A}) > EPSILON) || do {
1131 $fault = &set_fault($fault, $error);
1132 ($message !~ /^\s*$/) && ($message .= $/);
1133 $message .= "x=$self->{A}, x " . $$Xray::Xtal::strings{positive} . ".";
1134 };
1135 (($self->{B}) > EPSILON) || do {
1136 $fault = &set_fault($fault, $error);
1137 ($message !~ /^\s*$/) && ($message .= $/);
1138 $message .= "y=$self->{B}, y " . $$Xray::Xtal::strings{positive} . ".";
1139 };
1140 (($self->{C}) > EPSILON) || do {
1141 $fault = &set_fault($fault, $error);
1142 ($message !~ /^\s*$/) && ($message .= $/);
1143 $message .= "z=$self->{C}, z " . $$Xray::Xtal::strings{positive} . ".";
1144 };
1145 # 3: rules for crystal classes
1146 my $class = $self -> crystal_class();
1147 # Triclinic
1148 ($class eq "triclinic") && do {
1149 $normal = ( (abs(90-$self->{Alpha}) > EPSILON) &&
1150 (abs(90-$self->{Beta} ) > EPSILON) &&
1151 (abs(90-$self->{Gamma}) > EPSILON) &&
1152 (abs($self->{A}-$self->{B}) > EPSILON) &&
1153 (abs($self->{A}-$self->{C}) > EPSILON) &&
1154 (abs($self->{B}-$self->{C}) > EPSILON) );
1155 $normal || do {
1156 $fault = &set_fault($fault, $caution);
1157 ($message !~ /^\s*$/) && ($message .= $/);
1158 $message .= $$Xray::Xtal::strings{triclinic_desc};
1159 };
1160 };
1161 # Monoclinic
1162 ($class eq "monoclinic") && do {
1163 $normal = ( ( ((abs(90-$self->{Alpha}) > EPSILON)
1164 xor
1165 (abs(90-$self->{Beta} ) > EPSILON))
1166 xor
1167 (abs(90-$self->{Gamma}) > EPSILON) ) &&
1168 (abs($self->{A}-$self->{B}) > EPSILON) &&
1169 (abs($self->{A}-$self->{C}) > EPSILON) &&
1170 (abs($self->{B}-$self->{C}) > EPSILON) );
1171 $normal || do {
1172 $fault = &set_fault($fault, $caution);
1173 ($message !~ /^\s*$/) && ($message .= $/);
1174 $message .= $$Xray::Xtal::strings{monoclinic_desc};
1175 };
1176 ($self->{Setting} =~ /unique/i) || do {
1177 $fault = &set_fault($fault, $error);
1178 ($message !~ /^\s*$/) && ($message .= $/);
1179 $message .= $$Xray::Xtal::strings{monoclinic_unknown};
1180 };
1181 };
1182 # Orthorhombic
1183 ($class eq "orthorhombic") && do {
1184 $normal = ( (abs(90-$self->{Alpha}) < EPSILON) &&
1185 (abs(90-$self->{Beta} ) < EPSILON) &&
1186 (abs(90-$self->{Gamma}) < EPSILON) &&
1187 (abs($self->{A}-$self->{B}) > EPSILON) &&
1188 (abs($self->{A}-$self->{C}) > EPSILON) &&
1189 (abs($self->{B}-$self->{C}) > EPSILON) );
1190 $normal || do {
1191 $fault = &set_fault($fault, $caution);
1192 ($message !~ /^\s*$/) && ($message .= $/);
1193 $message .= $$Xray::Xtal::strings{orthorhombic_desc};
1194 };
1195 };
1196 # Tetragonal
1197 ($class eq "tetragonal") && do {
1198 $normal = ( (abs(90-$self->{Alpha}) < EPSILON) &&
1199 (abs(90-$self->{Beta} ) < EPSILON) &&
1200 (abs(90-$self->{Gamma}) < EPSILON) &&
1201 (abs($self->{A}-$self->{B}) < EPSILON) &&
1202 (abs($self->{A}-$self->{C}) > EPSILON) );
1203 $normal || do {
1204 $fault = &set_fault($fault, $caution);
1205 ($message !~ /^\s*$/) && ($message .= $/);
1206 $message .= $$Xray::Xtal::strings{tetragonal_desc};
1207 };
1208 };
1209 # Hexagonal
1210 ($class eq "hexagonal") && do {
1211 $normal = ( (abs( 90-$self->{Alpha}) < EPSILON) &&
1212 (abs( 90-$self->{Beta} ) < EPSILON) &&
1213 (abs(120-$self->{Gamma}) < EPSILON) &&
1214 (abs($self->{A}-$self->{B}) < EPSILON) &&
1215 (abs($self->{A}-$self->{C}) > EPSILON) );
1216 $normal || do {
1217 $fault = &set_fault($fault, $warning);
1218 ($message !~ /^\s*$/) && ($message .= $/);
1219 $message .= $$Xray::Xtal::strings{hexagonal_desc};
1220 };
1221 };
1222 # Trigonal
1223 ($class eq "trigonal") && do {
1224 # rhombohedral
1225 if (($group =~ /^r/i) && (abs( 90-$self->{Alpha}) > EPSILON)) {
1226 $normal = ( (abs($self->{Alpha}- $self->{Beta}) < EPSILON) &&
1227 (abs($self->{Alpha}- $self->{Gamma}) < EPSILON) &&
1228 (abs($self->{Beta} - $self->{Gamma}) < EPSILON) &&
1229 (abs($self->{A} - $self->{B}) < EPSILON) &&
1230 (abs($self->{A} - $self->{C}) < EPSILON) &&
1231 (abs($self->{B} - $self->{C}) < EPSILON) );
1232 $normal || do {
1233 $fault = &set_fault($fault, $warning);
1234 ($message !~ /^\s*$/) && ($message .= $/);
1235 $message .= $$Xray::Xtal::strings{rhombohedral_desc};
1236 };
1237 # not rhombohedral
1238 } else {
1239 $normal = ( (abs($self->{A}-$self->{B}) < EPSILON) &&
1240 (abs($self->{A}-$self->{C}) > EPSILON) );
1241 $normal || do {
1242 $fault = &set_fault($fault, $warning);
1243 ($message !~ /^\s*$/) && ($message .= $/);
1244 $message .= $$Xray::Xtal::strings{trigonal_desc};
1245 };
1246 $normal = ( (abs( 90-$self->{Alpha}) < EPSILON) &&
1247 (abs( 90-$self->{Beta} ) < EPSILON) &&
1248 (abs(120-$self->{Gamma}) < EPSILON) );
1249 $normal || do {
1250 $fault = &set_fault($fault, $error);
1251 ($message !~ /^\s*$/) && ($message .= $/);
1252 $message .= $$Xray::Xtal::strings{trigonal_desc};
1253 };
1254 };
1255 };
1256 # Cubic
1257 ($class eq "cubic") && do {
1258 $normal = ( (abs(90-$self->{Alpha}) < EPSILON) &&
1259 (abs(90-$self->{Beta} ) < EPSILON) &&
1260 (abs(90-$self->{Gamma}) < EPSILON) &&
1261 (abs($self->{A}-$self->{B}) < EPSILON) &&
1262 (abs($self->{B}-$self->{C}) < EPSILON) &&
1263 (abs($self->{A}-$self->{C}) < EPSILON) );
1264 $normal || do {
1265 $fault = &set_fault($fault, $caution);
1266 ($message !~ /^\s*$/) && ($message .= $/);
1267 $message .= $$Xray::Xtal::strings{cubic_desc};
1268 };
1269 };
1270 ($fault >=3 ) &&
1271 Xray::Xtal::trap_error($message . "$/$/ " .
1272 $$Xray::Xtal::strings{fatal_error} . "$/", 0);
1273 return $fault;
1274 };
1275
1276 sub set_fault {
1277 ##use Term::ANSIColor;
1278 my ($fault, $level) = @_;
1279 my @message = (" ", " $$Xray::Xtal::strings{caution}: ",
1280 " $$Xray::Xtal::strings{warning}: ",
1281 " $$Xray::Xtal::strings{error} ");
1282 my @colors = ("", "green", "yellow", "red");
1283 ##warn color($colors[$level]), $message[$level], $string, color("reset"), "$/";
1284 $fault = ($level > $fault) ? $level : $fault;
1285 return $fault;
1286 };
1287
1288 ## sub color {
1289 ## ($_[0] eq "") && return;
1290 ## ($_[0] eq "red") && return "";
1291 ## ($_[0] eq "green") && return "";
1292 ## ($_[0] eq "yellow") && return "";
1293 ## ($_[0] eq "blue") && return "";
1294 ## ($_[0] eq "magenta") && return "";
1295 ## ($_[0] eq "cyan") && return "";
1296 ## ($_[0] eq "white") && return "";
1297 ## ($_[0] eq "reset") && return "";
1298 ## };
1299
1300 =over 4
1301
1302 =item C<metric>
1303
1304 Takes the three fractional coordinates and returns the cartesian
1305 coordinates of the position. The fractional coordinates need not be
1306 canonicalized into the first octant, thus this method can be used to
1307 generate the cartesian coordinates for any atom in a cluster.
1308
1309 ($x,$y,$z) = $cell -> metric($xf, $yf, $zf);
1310
1311 This method is called repeatedly by the C<build_cluster> function in
1312 the Xray::Atoms module. The elements of the metric tensor, i.e. the
1313 C<Txx>, C<Tyx>, C<Tyz>, C<Tzx>, and C<Tz> Cell attributes, are used to
1314 make the transformation according to this formula:
1315
1316 / Txx 0 0 \ / xf \
1317 (x y z) = | Tyx 1 Tyz | | yf |
1318 \ Tzx 0 Tzz / \ zf /
1319
1320 =back
1321
1322 =cut
1323
1324 ## note that sprintf is a significant speed hit given the large number
1325 ## of times this function is called, but it does assure a given level
1326 ## of precision
1327 sub metric {
1328 my $self = shift;
1329 my ($x,$y,$z) = @_;
1330 my ($xp, $yp, $zp);
1331 ($x, $y, $z) = ($x*$self->{A}, $y*$self->{B}, $z*$self->{C});
1332 $xp = sprintf "%11.7f", $x*$self->{Txx};
1333 $yp = sprintf "%11.7f", $x*$self->{Tyx} + $y + $z*$self->{Tyz};
1334 $zp = sprintf "%11.7f", $x*$self->{Tzx} + $z*$self->{Tzz};
1335 return ($xp,$yp,$zp);
1336 };
1337
1338 =over 4
1339
1340 =item C<d_spacing>
1341
1342 Takes the Miller indeces of a scattering plane and returns the d
1343 spacing of that plane in Angstroms.
1344
1345 $d = $cell -> d_spacing($h, $k, $l);
1346
1347 =back
1348
1349 =cut
1350
1351 sub d_spacing {
1352 my $self = shift;
1353 my ($h, $k, $l) = @_;
1354 return 0 unless ($h or $k or $l);
1355 my $alpha = $self->{Alpha} * PI / 180;
1356 my $beta = $self->{Beta} * PI / 180;
1357 my $gamma = $self->{Gamma} * PI / 180;
1358
1359 my $s11 = ($self->{B}*$self->{C}*sin($alpha))**2;
1360 my $s22 = ($self->{A}*$self->{C}*sin($beta ))**2;
1361 my $s33 = ($self->{A}*$self->{B}*sin($gamma))**2;
1362
1363 my $s12 = $self->{A} * $self->{B} * ($self->{C}**2) *
1364 ( cos($alpha)*cos($beta) - cos($gamma) );
1365 my $s23 = $self->{B} * $self->{C} * ($self->{A}**2) *
1366 ( cos($beta) *cos($gamma) - cos($alpha) );
1367 my $s13 = $self->{C} * $self->{A} * ($self->{B}**2) *
1368 ( cos($gamma)*cos($alpha) - cos($beta) );
1369
1370 my $d = $s11*($h**2) + $s22*($k**2) + $s33*($l**2) +
1371 2*$s12*$h*$k + 2*$s23*$k*$l + 2*$s13*$h*$l;
1372 $d = $self->{Volume} / sqrt($d);
1373 return $d;
1374 };
1375
1376
1377
1378 =over 4
1379
1380 =item C<multiplicity>
1381
1382 Returns the multiplicity of a reflection hkl for the cell.
1383
1384 $p = $cell -> multiplicity($h, $k, $l);
1385
1386 See the footnote in Cullity page 523 for a caveat.
1387
1388 =back
1389
1390 =cut
1391
1392 sub multiplicity {
1393 my $self= shift;
1394 my $class = $self -> crystal_class();
1395 my ($h, $k, $l) = @_;
1396 my @r = sort($h, $k, $l);
1397 SWITCH:
1398 ($class eq 'cubic') and do {
1399 (not $r[0]) && (not $r[1]) && $r[2] && return 6;
1400 ($r[0] == $r[1]) && ($r[1] == $r[2]) && return 8;
1401 (not $r[0]) && ($r[1] == $r[2]) && return 12;
1402 (not $r[0]) && ($r[1] != $r[2]) && return 24; #*
1403 ($r[0] == $r[1]) && ($r[1] != $r[2]) && return 24;
1404 ($r[0] != $r[1]) && ($r[1] == $r[2]) && return 24;
1405 return 48
1406 };
1407 (($class eq 'hexagonal') || ($class eq 'trigonal')) and do {
1408 (not $h) && (not $k) && $l && return 2;
1409 (not $l) && ((not $h) || (not $k)) && return 6;
1410 (not $l) && ($h == $k) && return 6;
1411 (not $l) && ($h != $k) && return 12; #*
1412 $l && ((not $h) || (not $k)) && return 12; #*
1413 $l && ($h == $k) && return 12; #*
1414 return 24; #*
1415 };
1416 ($class eq 'tetragonal') and do{
1417 (not $h) && (not $k) && $l && return 2;
1418 (not $l) && ((not $h) || (not $k)) && return 4;
1419 (not $l) && ($h == $k) && return 4;
1420 (not $l) && ($h != $k) && return 8; #*
1421 $l && ((not $h) || (not $k)) && return 8;
1422 $l && ($h == $k) && return 8;
1423 return 16; #*
1424 };
1425 ($class eq 'orthorhombic') and do{
1426 (not $r[0]) && (not $r[1]) && return 2;
1427 (not $r[0]) || (not $r[1]) && return 4;
1428 return 4;
1429 };
1430 ($class eq 'monoclinic') and do{
1431 (not $r[0]) || (not $r[1]) && return 2;
1432 return 4;
1433 };
1434 ($class eq 'triclinic') and return 2;
1435 };
1436
1437
1438 =over 4
1439
1440 =item C<central>
1441
1442 This method takes one argument, a string specifying the tag of the
1443 central atom. It returns a four element list of the central atom
1444 elemental symbol and its fractional coordinates.
1445
1446 ($elem_c, $x_c, $y_c, $z_c) = $cell -> central($central_tag)
1447
1448 In many cases there is more than one choice for the central atom. Any
1449 of the various crystallographically identical positions matching the
1450 tag of the central atom can be chosen. This method returns the one
1451 closest to the center of the unit cell, i.e. the one closest to
1452 (1/2,1/2,1/2). The meaning of the word "closest" is a bit strange in
1453 this context. It is the position with the smallest value of
1454
1455 sqrt( (x-0.5)**2 + (y-0.5)**2 + (z-0.5)**2 )
1456
1457 This is chosen for speed and efficiency in building a spherical
1458 cluster of atoms.
1459
1460 =back
1461
1462 =cut
1463
1464 sub central {
1465 my $self = shift;
1466 my $core = $_[0];
1467 my $list;
1468 my ($xcenter, $ycenter, $zcenter, $central, $is_host);
1469 return ("",0,0,0,0) unless ($self->{Contents});
1470 FIND: foreach my $site (@{$self->{Contents}}) {
1471 #print lc($core), " ", lc($ {$$site[3]}->{Tag}), $/ ;
1472 (lc($core) eq lc($ {$$site[3]}->{Tag})) && do {
1473 $central = $ {$$site[3]}->{Element};
1474 $is_host = $ {$$site[3]}->{Host};
1475 $list = $ {$$site[3]}->{Positions};
1476 last FIND;
1477 };
1478 };
1479 #my @cformula = ("", "", "");
1480 if ($list) {
1481 my ($dist, $best) = (0, 100000);
1482 foreach my $site (@$list) {
1483 $dist =
1484 sqrt((0.5-$$site[0])**2 + (0.5-$$site[1])**2 + (0.5-$$site[2])**2);
1485 if ($dist < $best) {
1486 ($xcenter, $ycenter, $zcenter) = ($$site[0], $$site[1], $$site[2]);
1487 $best = $dist;
1488 };
1489 };
1490 #print join(" ", $central, $xcenter, $ycenter, $zcenter), $/;
1491 ##($xcenter, $ycenter, $zcenter) =
1492 ## $self -> metric($xcenter, $ycenter, $zcenter);
1493 return ($central, $xcenter, $ycenter, $zcenter, $is_host);
1494 };
1495 return ("",0,0,0,0);
1496 };
1497
1498
1499 =over 4
1500
1501 =item C<overfull>
1502
1503 This method returns the overfilled unit cell with atom positions in
1504 Cartesian coordinates. It takes an optional argument for specifying
1505 the epsilon defining which atoms should be considered close to a side,
1506 edge, or corner of the unit cell. The default for this value is 0.1
1507 and the units are fractional cell coordinates. The return value is a
1508 list structured the same as the Contents attribute of the cell.
1509
1510 @overfull = $cell -> overfull($epsi);
1511
1512 Each element of C<@overfull> is an anonymous array containing the x,
1513 y, and z fractional coordinates and a reference to the Site that
1514 generated the position.
1515
1516 =back
1517
1518 =cut
1519 ;
1520 sub overfull {
1521 my $self = shift;
1522 my $epsi = $_[0] || EPSILON;
1523 my @list = ();
1524 foreach my $site (@{$self->{Contents}}) {
1525 my @p = ([0],[0],[0]);
1526 unless ($epsi < 0) {
1527 foreach my $i (0..2) {
1528 ($$site[$i] < $epsi) && ($p[$i] = [0,1]); # near 0
1529 ((1-$$site[$i]) < $epsi) && ($p[$i] = [-1,0]); # near 1
1530 };
1531 };
1532 foreach my $a (@{$p[0]}) {
1533 foreach my $b (@{$p[1]}) {
1534 foreach my $c (@{$p[2]}) {
1535 my ($x, $y, $z) =
1536 $self -> metric($$site[0]+$a, $$site[1]+$b, $$site[2]+$c);
1537 push @list, [$x, $y, $z, $$site[3]];
1538 #push @list, [$$site[0]+$a, $$site[1]+$b, $$site[2]+$c, $$site[3]];
1539 };
1540 };
1541 };
1542 };
1543 return @list;
1544 };
1545
1546 =over 4
1547
1548 =item C<warn_shift>
1549
1550 This method returns a warning string if the space group is one for
1551 which the International Tables give two possible origin positions.
1552 This string suggests a value for the shift vector. An empty string
1553 is returned if only one origin is given.
1554
1555 print $cell -> warn_shift;
1556
1557 =back
1558
1559 =cut
1560
1561 sub get_shift {
1562 my $group = $_[0];
1563 my $setting;
1564 ($group, $setting) = canonicalize_symbol($group);
1565 (exists $$Xray::Xtal::r_space_groups{$group}{shiftvec}) &&
1566 return @{$$Xray::Xtal::r_space_groups{$group}{shiftvec}};
1567 return ();
1568 };
1569 sub warn_shift {
1570 my $self = shift;
1571 my $group = $self->{Space_group};
1572 (exists $$Xray::Xtal::r_space_groups{$group}{shiftvec}) && do {
1573 my $vec = join(", ", @{$$Xray::Xtal::r_space_groups{$group}{shiftvec}});
1574 my $message = join("", $/, " \"", ucfirst($group), "\" ",
1575 $$Xray::Xtal::strings{warn_shift},
1576 $/, "\t", $vec, $/, $/);
1577 return $message;
1578 };
1579 return "";
1580 };
1581
1582 =over 4
1583
1584 =item C<cell_check>
1585
1586 This method returns a warning if the cell axes and angles are not
1587 appropriate to the space group. It returns an empty string if they
1588 are appropriate.
1589
1590 print $cell -> cell_check;
1591
1592 =back
1593
1594 =cut
1595
1596 sub cell_check {
1597 my $self = shift;
1598 my $class = $self -> crystal_class();
1599 my $aa = $self->{A};
1600 my $bb = $self->{B};
1601 my $cc = $self->{C};
1602 my $alpha = $self->{Alpha};
1603 my $beta = $self->{Beta};
1604 my $gamma = $self->{Gamma};
1605 my $from_cell = "";
1606 DETERMINE: {
1607 # cubic
1608 ((abs($aa-$bb) < EPSILON) &&
1609 (abs($aa-$cc) < EPSILON) &&
1610 (abs($bb-$cc) < EPSILON) &&
1611 (abs($alpha-90) < EPSILON) &&
1612 (abs($beta-90) < EPSILON) &&
1613 (abs($gamma-90) < EPSILON)) && do {
1614 $from_cell = "cubic";
1615 last DETERMINE;
1616 };
1617 # tetragonal
1618 ((abs($aa-$bb) < EPSILON) &&
1619 (abs($aa-$cc) > EPSILON) &&
1620 (abs($alpha-90) < EPSILON) &&
1621 (abs($beta-90) < EPSILON) &&
1622 (abs($gamma-90) < EPSILON)) && do {
1623 $from_cell = "tetragonal";
1624 last DETERMINE;
1625 };
1626 # hexagonal or trigonal
1627 ((abs($aa-$bb) < EPSILON) &&
1628 (abs($aa-$cc) > EPSILON) &&
1629 (abs($alpha-90) < EPSILON) &&
1630 (abs($beta-90) < EPSILON) &&
1631 (abs($gamma-120) < EPSILON)) && do {
1632 $from_cell = "hexagonal";
1633 last DETERMINE;
1634 };
1635 # rhombohedral
1636 ((abs($aa-$bb) < EPSILON) &&
1637 (abs($aa-$cc) < EPSILON) &&
1638 (abs($bb-$cc) < EPSILON) &&
1639 (abs($alpha-$beta) < EPSILON) &&
1640 (abs($alpha-$gamma) < EPSILON) &&
1641 (abs($beta-$gamma) < EPSILON)) && do {
1642 $from_cell = "hexagonal";
1643 last DETERMINE;
1644 };
1645 # orthorhombic
1646 ((abs($aa-$bb) > EPSILON) &&
1647 (abs($aa-$cc) > EPSILON) &&
1648 (abs($bb-$cc) > EPSILON) &&
1649 (abs($alpha-90) < EPSILON) &&
1650 (abs($beta-90) < EPSILON) &&
1651 (abs($gamma-90) < EPSILON)) && do {
1652 $from_cell = "orthorhombic";
1653 last DETERMINE;
1654 };
1655 # triclinic
1656 ((abs($aa-$bb) > EPSILON) &&
1657 (abs($aa-$cc) > EPSILON) &&
1658 (abs($bb-$cc) > EPSILON) &&
1659 (abs($alpha-90) > EPSILON) &&
1660 (abs($beta-90) > EPSILON) &&
1661 (abs($gamma-90) > EPSILON)) && do {
1662 $from_cell = "triclinic";
1663 last DETERMINE;
1664 };
1665 # monoclinic
1666 ((abs($aa-$bb) > EPSILON) &&
1667 (abs($aa-$cc) > EPSILON) &&
1668 (abs($bb-$cc) > EPSILON) &&
1669 ((abs($alpha-90) > EPSILON) ||
1670 (abs($beta-90) > EPSILON) ||
1671 (abs($gamma-90) > EPSILON))) && do {
1672 $from_cell = "monoclinic";
1673 last DETERMINE;
1674 };
1675 };
1676 ( (($from_cell) eq "hexagonal") &&
1677 (($class eq "trigonal") || ($class eq "hexagonal")) ) &&
1678 return "";
1679 my $extra_message = "";
1680 ($extra_message = "Trigonal cells have x=y<>z and alpha=beta=90 and gamma=120.")
1681 if ($class eq "trigonal");
1682 ($extra_message = "Triclinic cells have all unequal axes and angles.")
1683 if ($class eq "triclinic");
1684
1685 ($class eq $from_cell) || do {
1686 my $message = join("", $/, " ",
1687 "The axis lengths and angles specified are not",
1688 $/,
1689 "appropriate for the given space group.",
1690 $/,
1691 $extra_message,
1692 $/, $/);
1693 return $message;
1694 };
1695 return "";
1696 };
1697
1698 =over 4
1699
1700 =item C<get_symmetry_table>
1701
1702 Return the list of symmetry operations for a space group (and,
1703 optionally, a setting). This returns an anonymous list of lists.
1704 Each list element contains three strings -- the three strings which
1705 are eval-ed to generate the positions in the unit cell.
1706
1707 $positions = $cell -> get_symmetry_table;
1708 @first_position = @ {$positions[0]};
1709
1710 =back
1711
1712 =cut
1713
1714 sub get_symmetry_table {
1715 my $self = shift;
1716 my $group = $self->{Space_group};
1717 my $positions = $self->{Setting};
1718 ($positions !~ /\b[0-5]\b/) || ($positions = "positions");
1719 (defined $$Xray::Xtal::r_space_groups{$group}{$positions}) &&
1720 return $$Xray::Xtal::r_space_groups{$group}{$positions};
1721 (defined $$Xray::Xtal::r_space_groups{$group}{b_unique}) &&
1722 return $$Xray::Xtal::r_space_groups{$group}{b_unique};
1723 (defined $$Xray::Xtal::r_space_groups{$group}{b_unique_1}) &&
1724 return $$Xray::Xtal::r_space_groups{$group}{b_unique_1};
1725 return [];
1726 };
1727
1728
1729
1730
1731 sub set_ipots {
1732 my $self = shift;
1733 my $style = $_[0]; # assignment scheme
1734 my $is_mol = $_[1]; # optional molecule flag
1735 my $tag = $_[2]; # optional central atom tag
1736 my $nip = 0;
1737 my %ipots = ();
1738 my $which;
1739 STYLES: {
1740 ($style eq "species") && do {
1741 $which = 'Element'; last STYLES;
1742 };
1743 ($style eq "tags") && do {
1744 $which = 'Tag'; last STYLES;
1745 };
1746 ($style eq "sites") && do {
1747 $which = 'Id'; last STYLES;
1748 };
1749 };
1750 foreach my $site (@{$self->{Contents}}) {
1751 #require Data::Dumper;
1752 #print Data::Dumper->Dump([\$site],[qw(*site)]);
1753 next unless ( $ {$$site[3]}->{Host} );
1754 next if ( $is_mol and (lc($tag) eq lc($ {$$site[3]}->{Tag})) );
1755 my $this = $ {$$site[3]}->{$which};
1756 ($style eq "tags") or $this = lc($this);
1757 (exists $ipots{$this}) or ($ipots{$this} = ++$nip);
1758 };
1759 return %ipots;
1760 };
1761
1762
1763 sub describe_group {
1764 my $group = $_[0];
1765 return $$Xray::Xtal::r_space_groups{$group};
1766 };
1767
1768
1769
1770 1;
1771
1772
1773 ######################################################################
1774
1775 =head2 The Site object
1776
1777 The Site object is a blessed hash. It predefines several attributes
1778 of a Site, but allows the user to define new attributes on the fly.
1779 The predefined (i.e. the ones that are used in Atoms) attributes are:
1780
1781 =over 4
1782
1783 =item B<Element>
1784
1785 The two letter symbol for the chemical species.
1786
1787 =item B<Tag>
1788
1789 A character string identifying a unique crystallographic site.
1790
1791 =item B<X>, B<Y>, B<Z>
1792
1793 The fractional coordinates of the sites (but see L<"MOLECULES"> below).
1794
1795 =item B<B>
1796
1797 The thermal spheroid parameter for the site.
1798
1799 =item B<Valence>
1800
1801 The formal valence for the element occupying the site.
1802
1803 =item B<Occupancy>
1804
1805 The fractional occupancy of the site. This allows the user to specify
1806 dopants.
1807
1808 =item B<Host>
1809
1810 This is 1 if the site is a host atom and 0 if it is a dopant.
1811
1812 =item B<Positions>
1813
1814 This takes an anonymous array of symmetry equivalent sites. This is
1815 filled after calling the C<populate> method.
1816
1817 =item B<File>
1818
1819 The name of an external file to be used with the site.
1820
1821 =item B<Id>
1822
1823 A pseudo-random number assigned by the C<new> method to uniquely
1824 identify the object.
1825
1826 =item B<Color>
1827
1828 The color assigned to the site in a ball-and-stick image.
1829
1830 =back
1831
1832
1833 =head2 Methods and Functions of the Site Package
1834
1835 See the descriptions of C<attributes> and C<clear> in the Cell Package
1836 section.
1837
1838 =cut
1839
1840 package Xray::Xtal::Site;
1841
1842 use constant EPSILON => 0.00001;
1843
1844 use strict;
1845 use vars qw(@ISA @EXPORT @EXPORT_OK);
1846
1847 require Exporter;
1848 require Xray::Xtal;
1849
1850 @ISA = qw(Exporter AutoLoader Xray::Xtal);
1851 @EXPORT_OK = qw(canonicalize_coordinate);
1852
1853 use Carp;
1854 use Chemistry::Elements qw(get_name get_Z get_symbol);
1855
1856 use vars qw($molecule);
1857 $molecule = 0;
1858
1859 ## constructor for a new crystallographic site
1860 sub new {
1861 my $classname = shift;
1862 my $self = {};
1863 my $index = shift || 0;
1864 $self->{Element} = q{};
1865 $self->{Tag} = q{};
1866 $self->{Utag} = q{};
1867 $self->{X} = 0;
1868 $self->{Y} = 0;
1869 $self->{Z} = 0;
1870 $self->{B} = 0;
1871 $self->{Bx} = 0; # |
1872 $self->{By} = 0; # > crystallographic thermal factors
1873 $self->{Bz} = 0; # |
1874 $self->{Valence} = 0; # intended for Cromer-Mann tables
1875 $self->{Occupancy} = 1;
1876 $self->{Host} = 1;
1877 $self->{Positions} = 0; # filled in by populate method
1878 $self->{Formulas} = 0; # filled in by populate method
1879 $self->{File} = q{};
1880 $self->{Id} = int(rand 100000) + 1;
1881 bless($self, $classname);
1882 $self->{Color} = $self -> default_color($index);
1883 $self->{message_buffer} = q{},
1884 return $self;
1885 };
1886 ## $self->{Dopants} = 0; # a list of some sort...
1887
1888 ## method for filling in attributes of a site
1889 sub make {
1890 my $self = shift;
1891 my @attributes = qw(Element Tag X Y Z Bx By Bz Valence Color
1892 Occupancy Host Positions);
1893 ($#_ % 2) || do {
1894 my $this = (caller(0))[3];
1895 croak "$this takes an even number of arguments";
1896 return; };
1897 my $this_message = q{};
1898 while (@_) {
1899 my $att = ucfirst($_[0]);
1900 ## there are a few checks that can be done...
1901 ATT: {
1902 ($att eq "Positions") && do {
1903 my $this = (caller(0))[3];
1904 ##carp "You should not set the Positions attribute of a site by hand$/";
1905 ##carp "Use the populate method instead.$/";
1906 $this_message = "You should not set the Positions attribute of a site by hand$/Use the populate method instead.$/";
1907 $self->make(message_buffer => $self->{message_buffer} . $this_message);
1908 last ATT;
1909 };
1910 ($att eq "Id") && do {
1911 my $this = (caller(0))[3];
1912 ##carp "You should not set the Id attribute of a site by hand$/";
1913 $this_message = "You should not set the Id attribute of a site by hand$/";
1914 $self->make(message_buffer => $self->{message_buffer} . $this_message);
1915 last ATT;
1916 };
1917 ## element can be a symbol, name, or number
1918 ## return "--" for an improper element
1919 ($att eq "Element") && do {
1920 my $sym = get_symbol($_[1]);
1921 unless (defined $sym) {$sym = "--"};
1922 $self->{"Element"} = $sym;
1923 last ATT;
1924 };
1925 ## occupancy must be between 0 and 1
1926 ($att eq "Occupancy") && do {
1927 if ($_[1] < 0) {
1928 $self->{"Occupancy"} = 0;
1929 $this_message = " " . $$Xray::Xtal::strings{site_occ} . $/;
1930 $self->make(message_buffer => $self->{message_buffer} . $this_message);
1931 last ATT;
1932 };
1933 if ($_[1] > 1) {
1934 $self->{"Occupancy"} = 1;
1935 $this_message = " " . $$Xray::Xtal::strings{site_occ} . $/;
1936 $self->make(message_buffer => $self->{message_buffer} . $this_message);
1937 last ATT;
1938 };
1939 $self->{"Occupancy"} = $_[1];
1940 last ATT;
1941 };
1942 ## thermal factors are positive definate (units of AA^2)
1943 ($att =~ /B[xyz]/) && do {
1944 my $val = ($_[1] > 0) ? $_[1] : 0;
1945 $self->{$att} = $val;
1946 last ATT;
1947 };
1948 ## some other possibilities:
1949 ## -- check valence against Cromer-Mann tables
1950 ## -- convert color to an RGB triplet
1951 do {
1952 $self->{ $att } = $_[1];
1953 $self->{ $att } =~ s/^\s+$//;
1954 };
1955 };
1956 shift; shift;
1957 }
1958 $self->{Tag} ||= ucfirst($self->{Element});
1959 unless ($molecule) {
1960 $self->{X} = canonicalize_coordinate($self->{X});
1961 $self->{Y} = canonicalize_coordinate($self->{Y});
1962 $self->{Z} = canonicalize_coordinate($self->{Z});
1963 };
1964 return $self;
1965 };
1966
1967 =over 4
1968
1969 =item C<reset_message_buffer>
1970
1971 Reset the message buffer for this object to the empty string.
1972
1973 =back
1974
1975 =cut
1976
1977 sub reset_message_buffer {
1978 my $self = shift;
1979 $self -> make(message_buffer => q{});
1980 }
1981
1982 =over 4
1983
1984 =item C<clear>
1985
1986 Reset a site without destroying it. For a site, this resets all
1987 predefined attributes to their initial values and all user-defined
1988 attributes to 0.
1989
1990 $site -> clear();
1991
1992 =back
1993
1994 =cut
1995 ## reset a site without destroying it
1996 sub clear {
1997 my $self = shift;
1998 foreach my $key (keys %$self) {
1999 next if ($key eq "Id");
2000 $self->{$key} = 0;
2001 };
2002 ## these three predefined attributes are reset to strings
2003 $self->{Element} = "";
2004 $self->{Tag} = "";
2005 $self->{Color} = "black";
2006 return $self;
2007 };
2008
2009 =over 4
2010
2011 =item C<populate>
2012
2013 This applied the symmetry operations implied by the space group of the
2014 Cell object C<$cell> to a unique crystallographic site. The
2015 collection of symmetry related sites will be stored in the site object
2016 as the C<Positions> attribute.
2017
2018 $site -> populate($cell);
2019
2020 Typically, it is not necessary to call this explicitly as it is called
2021 for each site by the Cell populate method.
2022
2023 =back
2024
2025 =cut
2026 ## apply the symmetries of $group to a site, canonicalize to the first
2027 ## octant, and weed out the repeats. This subroutine is the real
2028 ## workhorse of the Site package.
2029 ## input: reference to a cell object
2030 ## output: a list of lists, the coordinates of the equivalent sites in
2031 ## the unit cell
2032 sub populate {
2033 my $self = shift;
2034 my ($cell) = $_[0];
2035 my $group = $cell->{Space_group};
2036 my $given = $cell->{Given_group};
2037 my $setting = $cell->{Setting};
2038 my $x = $self->{X}; # take x,y,z from $self
2039 my $y = $self->{Y};
2040 my $z = $self->{Z};
2041 my $utag = "_" . $self->{Utag};
2042 my @list;
2043 my $bravais = $cell->{Bravais};
2044 ## (@bravais) && (print join(",", @$bravais), "$/");
2045
2046 #-------------------------- handle different settings as needed
2047 my $positions = "positions";
2048 my $crystal_class = $cell -> crystal_class();
2049 my $do_ortho = ($crystal_class eq "orthorhombic" ) && ($setting);
2050 my $do_tetr = ($crystal_class eq "tetragonal" ) && ($setting);
2051 ($crystal_class eq "monoclinic") && do {
2052 $positions = $cell->{Setting};
2053 # bravais vector for the //given// symbol
2054 };
2055 ($group =~ /^r/i) && ($positions = $setting ? $setting : $positions);
2056 ($positions) || do {
2057 my $this = (caller(0))[3];
2058 croak "Invalid positions specifier in $this";
2059 return;
2060 };
2061
2062 #-------------------------- permute to alternate settings (orthorhombic)
2063 # 1..5 |--> [ ba-c, cab, -cba, bca, a-cb ]
2064 ($do_ortho) && do {
2065 FORWARD: {
2066 ($setting == 1) && do {
2067 ( ($x, $y, $z) = ( $y, $x, -$z) ); last FORWARD;
2068 };
2069 ($setting == 2) && do {
2070 ( ($x, $y, $z) = ( $y, $z, $x) ); last FORWARD;
2071 };
2072 ($setting == 3) && do {
2073 ( ($x, $y, $z) = ( $z, $y, -$x) ); last FORWARD;
2074 };
2075 ($setting == 4) && do {
2076 ( ($x, $y, $z) = ( $z, $x, $y) ); last FORWARD;
2077 };
2078 ($setting == 5) && do {
2079 ( ($x, $y, $z) = ( $x, $z, -$y) ); last FORWARD;
2080 };
2081 };
2082 };
2083 #-------------------------- rotate from F or C settings to P or I
2084 ($do_tetr) and do {
2085 ($x, $y) = ($x-$y, $x+$y);
2086 #my ($a, $b) = ($$r_cell->{A}, $$r_cell->{B});
2087 #$$r_cell -> make(A=>$a/sqrt(2), B=>$b/sqrt(2));
2088 #($$r_cell->{A}, $$r_cell->{B}) =
2089 # ($$r_cell->{A}/sqrt(2), $$r_cell->{B}/sqrt(2));
2090 };
2091
2092 ## ----- evaluate the coordinates safely
2093 ## see `perldoc Safe' for details
2094 my $message = $$Xray::Xtal::strings{tainted_sgdb} . $/;
2095 ## my $cpt = new Safe;
2096 ## ## need to load $x, $y, $z into the safe compartment
2097 ## $ {$cpt->varglob('x')} = $x;
2098 ## $ {$cpt->varglob('y')} = $y;
2099 ## $ {$cpt->varglob('z')} = $z;
2100 #print join(" ", $x, $y, $z, $/);
2101
2102 #---------------------------- loop over all symmetry operations
2103 foreach my $position (@{ $$Xray::Xtal::r_space_groups{$group}{$positions} }) {
2104 my $i = 0;
2105 my ($xpos, $ypos, $zpos) = ( $$position[0], $$position[1], $$position[2] );
2106
2107 foreach ($xpos, $ypos, $zpos) {
2108 ## the regex is intended to be an exhaustive list of characters
2109 ## found in the symmetry part of the space groups database.
2110 ## This is not bomber security as it is possible to, say,
2111 ## somehow alias "y56z-3" to "rm -rf ~". But I think this will
2112 ## foil the casual black hat.
2113 ($_ =~ /([^-1-6xyzXYZ+\$\/])/) and
2114 Xray::Xtal::trap_error("$message\nfirst bad character: $1$/", 0);
2115 };
2116 ($xpos, $ypos, $zpos) = map {eval $_} ($xpos, $ypos, $zpos);
2117
2118 ##print join(" ", $xpos, $ypos, $zpos, $/);
2119 my @f = @$position; # store formulas for this position
2120 map {s/\$//g} @f; # remove dollar sign
2121 map {s/([xyz])/$1$utag/g} @f; # append unique tag
2122
2123 ## $ {$cpt->varglob('xx')} = $cpt->reval($xpos);
2124 ## $ {$cpt->varglob('yy')} = $cpt->reval($ypos);
2125 ## $ {$cpt->varglob('zz')} = $cpt->reval($zpos);
2126 ## ($xpos, $ypos, $zpos) = ($ {$cpt->varglob('xx')},
2127 ## $ {$cpt->varglob('yy')},
2128 ## $ {$cpt->varglob('zz')} );
2129 ## test_safe_return($message, $xpos, $ypos, $zpos);
2130 ## ----- end of safe evaluation
2131
2132 my ($xposi, $yposi, $zposi) = ($xpos, $ypos, $zpos);
2133
2134 #-------------------------- permute back from alt. settings (orthorhombic)
2135 ($do_ortho) && do {
2136 ($setting == 1) && (($xposi, $yposi, $zposi) = ( $yposi, $xposi,-$zposi));
2137 ($setting == 2) && (($xposi, $yposi, $zposi) = ( $zposi, $xposi, $yposi));
2138 ($setting == 3) && (($xposi, $yposi, $zposi) = (-$zposi, $yposi, $xposi));
2139 ($setting == 4) && (($xposi, $yposi, $zposi) = ( $yposi, $zposi, $xposi));
2140 ($setting == 5) && (($xposi, $yposi, $zposi) = ( $xposi,-$zposi, $yposi));
2141 };
2142 # need to rectify formulas for orthorhombic settings
2143
2144 #-------------------------- permute back to F or C settings from P or I
2145 #($do_tetr) and ($x, $y) = ($x-$y, $x+$y);
2146
2147 #-------------------------- canonicalize and push onto list
2148 ($xposi, $yposi, $zposi) = (&canonicalize_coordinate($xposi),
2149 &canonicalize_coordinate($yposi),
2150 &canonicalize_coordinate($zposi));
2151 push @list, [$xposi, $yposi, $zposi, @f];
2152
2153 #-------------------------- do Bravais translations
2154 while ($i < $#{$bravais}) {
2155 ($xposi, $yposi, $zposi) = ($xpos+$$bravais[$i],
2156 $ypos+$$bravais[$i+1],
2157 $zpos+$$bravais[$i+2]);
2158 #------------------------ permute back from alt. settings (orthorhombic)
2159 ($do_ortho) && do {
2160 BACKWARD: {
2161 ($setting == 1) && do {
2162 (($xposi, $yposi, $zposi)=( $yposi, $xposi,-$zposi));
2163 last BACKWARD;
2164 };
2165 ($setting == 2) && do {
2166 (($xposi, $yposi, $zposi)=( $zposi, $xposi, $yposi));
2167 last BACKWARD;
2168 };
2169 ($setting == 3) && do {
2170 (($xposi, $yposi, $zposi)=(-$zposi, $yposi, $xposi));
2171 last BACKWARD;
2172 };
2173 ($setting == 4) && do {
2174 (($xposi, $yposi, $zposi)=( $yposi, $zposi, $xposi));
2175 last BACKWARD;
2176 };
2177 ($setting == 5) && do {
2178 (($xposi, $yposi, $zposi)=( $xposi,-$zposi, $yposi));
2179 last BACKWARD;
2180 };
2181 };
2182 };
2183
2184 #-------------------------- canonicalize and push this bravais position
2185 ($xposi, $yposi, $zposi) = (&canonicalize_coordinate($xposi),
2186 &canonicalize_coordinate($yposi),
2187 &canonicalize_coordinate($zposi));
2188 my @ff = @f; # append bravais translation to formulas
2189 map {$ff[$_] = $f[$_] . " + " . $$bravais[$i+$_]} (0 .. 2);
2190 push @list, [$xposi, $yposi, $zposi, @ff];
2191 $i+=3;
2192 };
2193 };
2194 #---------------------------- Weed out repeats.
2195 my @form = ();
2196 my %seen = ();
2197 my @uniq = (); # see section 4.6, p.102,
2198 foreach my $item (@list) { # The Perl Cookbook, 1st edition
2199 #my $keya = sprintf "%7.5f", $$item[0]; chop $keya;
2200 #my $keyb = sprintf "%7.5f", $$item[1]; chop $keyb;
2201 #my $keyc = sprintf "%7.5f", $$item[2]; chop $keyc;
2202 my $keya = sprintf "%6.4f", $$item[0]; chop $keya;
2203 my $keyb = sprintf "%6.4f", $$item[1]; chop $keyb;
2204 my $keyc = sprintf "%6.4f", $$item[2]; chop $keyc;
2205 my $key = $keya . $keyb . $keyc;
2206 unless ($seen{$key}++) {
2207 push (@uniq, [@$item[0..2]]);
2208 push (@form, [@$item[3..5]]);
2209 };
2210 };
2211 #---------------------------- return an anonymous array
2212 $self -> {Positions} = [@uniq];
2213 $self -> {Formulas} = [@form];
2214 return $self;
2215 };
2216
2217 ## an unsafe string will result in an undefined value
2218 sub test_safe_return {
2219 my $message = shift(@_);
2220 my @list = @_;
2221 foreach my $item (@list) {
2222 (defined $item) || Xray::Xtal::trap_error("$message$/", 0);
2223 };
2224 return 1;
2225 };
2226
2227
2228 my @default_color_list = qw(black red blue green brown orange yellow
2229 cyan magenta grey);
2230 sub default_color {
2231 my ($self, $index) = (shift, shift||0);
2232 $index %= 10;
2233 return $default_color_list[$index];
2234 };
2235
2236 =over 4
2237
2238 =item C<canonicalize_coordinate>
2239
2240 This the subroutine is part of the Site package. It takes a
2241 fractional coordinate and returns it shifted into the first octant.
2242 This is not actually a method of the Site class, it is just a normal
2243 function.
2244
2245 $coord = Xray::Xtal::Site::canonicalize_coordniate($coord);
2246
2247 =back
2248
2249 =cut
2250
2251 sub canonicalize_coordinate {
2252 my $pos = $_[0];
2253 return $pos if ($Xray::Xtal::Site::molecule == 1);
2254 $pos -= int($pos); # move to first octant
2255 ($pos < -1*EPSILON) && ($pos += 1);
2256 (abs($pos) < EPSILON) && ($pos = 0);
2257 SYM: { # positions of special symmetry
2258 if (abs($pos) < 0.00001) {($pos = 0); last SYM;}
2259 if (abs($pos-0.125) < 0.00001) {($pos = 1/8); last SYM;}
2260 if (abs($pos-0.1666) < 0.00001) {($pos = 1/6); last SYM;}
2261 if (abs($pos-0.25) < 0.00001) {($pos = 1/4); last SYM;}
2262 if (abs($pos-0.3333) < 0.00001) {($pos = 1/3); last SYM;}
2263 if (abs($pos-0.375) < 0.00001) {($pos = 3/8); last SYM;}
2264 if (abs($pos-0.5) < 0.00001) {($pos = 1/2); last SYM;}
2265 if (abs($pos-0.625) < 0.00001) {($pos = 5/8); last SYM;}
2266 if (abs($pos-0.6666) < 0.00001) {($pos = 2/3); last SYM;}
2267 if (abs($pos-0.75) < 0.00001) {($pos = 3/4); last SYM;}
2268 if (abs($pos-0.8333) < 0.00001) {($pos = 5/6); last SYM;}
2269 if (abs($pos-0.875) < 0.00001) {($pos = 7/8); last SYM;}
2270 if (abs($pos-1) < 0.00001) {($pos = 0); last SYM;}
2271 };
2272 return $pos;
2273 };
2274
2275 1;
2276
2277 package Xray::Xtal;
2278 1;
2279
2280 # Autoload methods go after =cut, and are processed by the autosplit program.
2281
2282 __END__
2283
2284
2285 =head1 INTERPRETING SPACE GROUP SYMBOLS
2286
2287 You may specify space group symbols in a variety of ways. When
2288 presented with a possible symbol, the Xtal module will first clean the
2289 symbol up by adding and/or removing whitespace then by trying each of
2290 the following ways of interpreting the symbol.
2291
2292 =over 4
2293
2294 =item 1.
2295
2296 A standard symbol from the 1995 edition of the International Tables
2297 for Crystallography. For example, the group for the cubic perovskite
2298 structure is C<P m -3 m>.
2299
2300 =item 2.
2301
2302 A Schoenflies symbol. The Scoenflies symbol for C<P m -3 m> is C<O_1^h>.
2303
2304 =item 3.
2305
2306 A symbol from the 1935 edition of the International tables. The 1935
2307 symbol for C<P m -3 m> is C<P m 3 m>.
2308
2309 =item 4.
2310
2311 The number for the entry of the space group in the International
2312 Tables. The number for C<P m -3 m> is 221.
2313
2314 =item 5.
2315
2316 The full symbol. For C<P m -3 m> this is C<P 4/m -3 2/m>.
2317
2318 =item 6.
2319
2320 An alternative symbol. See L<"LOW SYMMETRY SPACE GROUPS"> for details.
2321
2322 =item 7.
2323
2324 A shorthand phrase, such as C<fcc> for C<F m -3 m>. See (somewhere)
2325 for a complete list of shorthand phases.
2326
2327 =item 8.
2328
2329 A short symbol for a monoclinic space group. See
2330 L<"LOW SYMMETRY SPACE GROUPS"> for details.
2331
2332 =back
2333
2334 Since whitespace in the symbol is regularized, you can usually include
2335 or omit whitespace in any fashion. For example C<pm3m> and
2336 C<P m -3 m> both come out as C<P m -3 m>, just
2337 as you would expect.
2338
2339 A space group is specified using the C<make> method from the Cell
2340 class. When this is done the original symbol is stored in the
2341 Given_group attribute of the Cell object and the standard symbol
2342 interpreted from it is stored in the Space_group attribute. If the
2343 given symbol cannot be interpretted, the Space_group attribute is set
2344 to 0.
2345
2346 In the future, more flexibility may be added to the space group symbol
2347 interpretation scheme, including full symbols for alternate settings
2348 and more obsolete notation. I am open to suggestions.
2349
2350
2351
2352 =head1 LOW SYMMETRY SPACE GROUPS
2353
2354 There is a more complete discussion of low symmetry groups in the
2355 Atoms document. Here are the highlights:
2356
2357 =head2 Monoclinic Space Groups
2358
2359 Monoclinic groups can be quite confusing for the user. Because any of
2360 the three angles can be the unique (acute or obtuse rather than right)
2361 angle and because there are three possible settings for the cell, the
2362 standard symbol can be ambiguous. In that case, using the full symbol
2363 or the short monoclinic symbol would break the ambiguity and allow the
2364 make method to correctly assign the space group. An example would
2365 help illustrate this.
2366
2367 In "Structural phase diagram of La(1-x)Sr(x)MnO(3+delta): Relationship
2368 to magnetic and transport properties." by J.F. Mitchell et al,
2369 Phys. Rev. B54, no. 9, (1996), pp./ 6172-6183 the monoclinic structure
2370 for room temperature LnMnO3 is given in Table IV. The space group is
2371 given as C<P 21/c> and the setting is given as C<P 1 21/n 1>.
2372 Specifying the space group symbol will result in the wrong application
2373 of symmetries when the Contents attribute of the cell is filled.
2374 Specifying the full symbol for that setting will work correctly. The
2375 shorthand C<P 21/n> will also result in a correct applciation of
2376 symmetries.
2377
2378 In the Atoms document, there is a complete list of symbols recognized
2379 by Atoms, including all the short and full symbols for the different
2380 settings of monoclinic groups. It is essential to always use site
2381 coordinates and unique angles appropriate to the symbol (and vice
2382 versa).
2383
2384 In the strange case of crystal data in a monoclinic setting but with
2385 the unique angle equal to 90, it is necessary to specify the unique
2386 angle like so
2387
2388 $cell -> make(Angle=>'beta')
2389
2390 Without this, there is no way for the Cell class to know which setting
2391 to use.
2392
2393 =head2 Orthorhombic Space Groups
2394
2395 You may use any setting of an orthorhombic crystal, although it is up
2396 to you to be sure that the unique coordinates are appropriate to that
2397 setting. Atoms has no way of checking that!
2398
2399 =head2 Tetragonal Space Groups
2400
2401 F and C centered tetragonal cells are handled transparently by
2402 Atoms. If you find a literature reference using one of these, you can
2403 usually use the crystal data given in the article as written.
2404
2405 =head2 Rhombohedral Space Groups
2406
2407 You can specify either hexagonal or rhombohedral parameters for
2408 rhombohedral space groups. For the hexagonal representation, you must
2409 specify C<a> and C<b> and C<c>, C<alpha>, C<beta>, and C<gamma> will
2410 be set correctly by the C<make> method. For the rhombohedral
2411 representation, you must specify C<a> and C<alpha> and the rest will
2412 be set appropriately.
2413
2414 =head1 CHANGE LOG
2415
2416 =over 4
2417
2418 =item *
2419
2420 Apr 26 2004: fixed the shift vector for space group #129. (thanks to
2421 Pieter Glatzel)
2422
2423 =back
2424
2425 =head1 BUGS
2426
2427 Surely there are plenty. Send me email if you find any.
2428
2429 =head1 AUTHOR
2430
2431 Bruce Ravel <[email protected]>
2432 http://feff.phys.washington.edu/~ravel/software/
2433
2434 =head1 SEE ALSO
2435
2436 The documentation that comes with the Atoms package.
2437
2438 =cut
2439
2440 ## Local Variables:
2441 ## time-stamp-line-limit: 25
2442 ## End: