Debian Codegraph @

lib/Xray/Xtal.pm

raw
1 #! /usr/bin/perl -w
2 ######################################################################
3 ## This module is copyright (c) 1998-2004, 2008, 2010 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 ($symbol =~ m{\?}) && return (0,0);
791
792 $symbol = lc($symbol); # lower case and ...
793 $symbol =~ s/[!\#%*].*$//; # trim off comments
794 $symbol =~ s/^\s+//; # trim leading spaces
795 $symbol =~ s/\s+$//; # trim trailing spaces
796 $symbol =~ s/\s+/ /g; # ... single space
797 $symbol =~ s|\s*/\s*|/|g; # spaces around slash
798
799 $symbol =~ s/2_1/21/g; # replace `i 4_1' with `i 41'
800 $symbol =~ s/3_([12])/2$1/g; # and so on ...
801 $symbol =~ s/4_([1-3])/2$1/g;
802 $symbol =~ s/6_([1-5])/2$1/g;
803
804 my $shorthands = 'bcc|c(scl|ubic)|diamond|fcc|gra(|phite)|h(cp|ex)';
805 $shorthands = $shorthands . '|nacl|perov(|skite)|salt|z(incblende|ns)';
806 unless ( ($symbol =~ /[_^ ]/) or # HM with spaces, schoen
807 ($symbol =~ /\b\d{1,3}\b/) or # 1-230
808 ($symbol =~ /\b($shorthands)\b/io) # shorthands like 'cubic', 'zns'
809 ) {
810 $symbol = insert_spaces($symbol);
811 };
812 # this is the standard symbol
813 (exists($$Xray::Xtal::r_space_groups{$symbol})) && return ($symbol, 0);
814
815 foreach $sym (keys %$Xray::Xtal::r_space_groups ) {
816 next if ($sym eq "version");
817 my %hash = %{$$Xray::Xtal::r_space_groups{$sym}};
818
819 # scalar valued fields
820 # this is a number between 1 and 230
821 # or the 1935 symbol
822 # or a double glide plane symbol
823 # or the full symbol
824 foreach my $field ("thirtyfive", "new_symbol", "full") {
825 exists $hash{$field} && ($symbol eq $hash{$field}) &&
826 return ($sym, 0);
827 };
828 if ($symbol eq $hash{number}) {
829 if (($symbol > 2) and ($symbol < 16)) {
830 return ($sym, 1);
831 } else {
832 return ($sym, 0);
833 };
834 };
835 # this is the Schoenflies symbol, (it
836 # must have a caret in it)
837 ($symbol =~ /\^/) && do {
838 $symbol =~ s/\s+//g; # no spaces
839 $symbol =~ s/^v/d/g; # V -> D
840 # put ^ and _ in correct order
841 $symbol =~ s/([cdost])(\^[0-9]{1,2})(_[12346dihsv]{1,2})/$1$3$2/;
842 exists $hash{"schoenflies"} and
843 ($symbol eq $hash{"schoenflies"}) and
844 return ($sym, 0);
845 };
846 # now check the array values fields
847 foreach my $field ("settings", "short", "shorthand") {
848 (exists($hash{$field})) && do {
849 my $i=0;
850 foreach my $setting ( @{$hash{$field}} ) {
851 ++$i;
852 my $s = ($field eq "settings") ? $i : 0;
853 if ($symbol eq $setting) {
854 ##print "setting : $s\n";
855 return ($sym, $s);
856 };
857 }; # "settings" is an array field
858 };
859 };
860 };
861 # this is not a symbol
862 1 && do { return (0,0); };
863 }
864
865 ## This is the algorithm for dealing with user-supplied space group
866 ## symbols that do not have the canonical single space separating the
867 ## part of the symbol.
868 sub insert_spaces {
869 my $sym = $_[0];
870
871 my ($first, $second, $third, $fourth) = ("", "", "", "");
872
873 ## a few groups don't follow the rules below ...
874 ($sym =~ /\b([rhc])32\b/i) && return "$1 3 2";
875 ($sym =~ /\bp31([2cm])\b/i) && return "p 3 1 $1";
876 ($sym =~ /\bp(3[12]?)[22][12]\b/i) && return "p $1 2 1";
877 ($sym =~ /\bp(6[1-5]?)22\b/i) && return "p $1 2 2";
878 ($sym =~ /\b([fip])(4[1-3]?)32\b/i) && return "$1 $2 3 2";
879 ($sym =~ /\b([fipc])(4[1-3]?)(21?)(2)\b/i) && return "$1 $2 $3 $4";
880
881 ## the first symbol is always a single letter
882 $first = substr($sym, 0, 1);
883 my $index = 1;
884
885 if (substr($sym, $index, 4) =~ /([2346][12345]\/[mnabcd])/) {
886 ## second symbol as in p 42/n c m
887 $second = $1;
888 $index += 4;
889 } elsif (substr($sym, $index, 3) =~ /([2346]\/[mnabcd])/) {
890 ## second symbol as in p 4/n n c
891 $second = $1;
892 $index += 3;
893 } elsif (substr($sym, $index, 2) =~ /(-[1346])/) {
894 ## second symbol as in p -3 1 m
895 $second = $1;
896 $index += 2;
897 } elsif (substr($sym, $index, 2) =~ /(21|3[12]|4[123]|6[12345])/) {
898 ## second symbol as in p 32 1 2
899 $second = $1;
900 $index += 2;
901 } else {
902 $second = substr($sym, $index, 1);
903 $index += 1;
904 };
905
906 if (substr($sym, $index, 4) =~ /([2346][12345]\/[mnabcd])/) {
907 ## third symbol as in full symbol p 21/c 21/c 2/n
908 $third = $1;
909 $index += 4;
910 } elsif (substr($sym, $index, 3) =~ /([2346]\/[mnabcd])/) {
911 ## third symbol as in full symbol p 4/m 21/b 2/m
912 $third = $1;
913 $index += 3;
914 } elsif (substr($sym, $index, 2) =~ /(-[1346])/) {
915 ## third symbol as in f d -3 m
916 $third = $1;
917 $index += 2;
918 } elsif (substr($sym, $index, 2) =~ /(21|3[12]|4[123]|6[12345])/) {
919 ## third symbol as in p 21 21 2
920 $third = $1;
921 $index += 2;
922 } else {
923 $third = substr($sym, $index, 1);
924 $index += 1;
925 };
926
927 ($index < length($sym)) and $fourth = substr($sym, $index);
928
929 $sym = join(" ", $first, $second, $third, $fourth);
930 $sym =~ s/\s+$//; # trim trailing spaces
931 return $sym;
932 };
933
934 ## =over 4
935 ##
936 ## =item C<bravais>
937 ##
938 ## This takes the values of the C<Space_group> and C<Setting> attributes
939 ## of a Cell and returns a list which specifies the Bravais translation
940 ## vectors. This list is the truncation of the various Bravais
941 ## translation three-vectors, thus has 0, 3, 6, or 9 elements. This is
942 ## not method of the Cell class, it is just a normal function. It should
943 ## rarely be necessary to call this, as the Bravais vector gets stored in
944 ## the "Bravais" attribute of a cell.
945 ##
946 ## ($group, $setting) =
947 ## $cell -> attributes("Space_group", "Setting");
948 ## @list = Xray::Xtal::Cell::bravais($group, $setting);
949 ##
950 ## =back
951 ##
952 ## =cut
953
954 sub bravais {
955 my $group = lc(substr($_[0], 0, 1));
956 my $setting = $_[1];
957 ( $group eq "f") && return ( 0, 1/2, 1/2, 1/2, 0, 1/2, 1/2, 1/2, 0);
958 ( $group eq "i") && return (1/2, 1/2, 1/2);
959 ( $group eq "c") && return (1/2, 1/2, 0);
960 ( $group eq "a") && return ( 0, 1/2, 1/2);
961 ( $group eq "b") && return (1/2, 0, 1/2);
962 (($group eq "r") && ($setting eq 0))
963 && return (2/3, 1/3, 1/3, 1/3, 2/3, 2/3);
964 return ();
965 };
966
967
968 =over 4
969
970 =item C<crystal_class>
971
972 This returns a character string specifying the crystal class. The
973 return value is one of ``cubic'', ``hexagonal'', ``trigonal'',
974 ``tetragonal'', ``orthorhombic'', ``monoclinic'', or ``triclinic''.
975 This is a method of the Cell class.
976
977 $class = $cell -> crystal_class();
978
979 =back
980
981 =cut
982
983 sub crystal_class {
984 my $self = shift;
985 my $group = $self->{Space_group};
986 (exists $$Xray::Xtal::r_space_groups{$group}{number}) && do {
987 my $hash_element = $$Xray::Xtal::r_space_groups{$group};
988 ($$hash_element{number} <= 0) && return "";
989 ($$hash_element{number} <= 2) && return "triclinic";
990 ($$hash_element{number} <= 15) && return "monoclinic";
991 ($$hash_element{number} <= 74) && return "orthorhombic";
992 ($$hash_element{number} <= 142) && return "tetragonal";
993 ($$hash_element{number} <= 167) && return "trigonal";
994 ($$hash_element{number} <= 194) && return "hexagonal";
995 ($$hash_element{number} <= 230) && return "cubic";
996 return "";
997 }; # need to return something if $group not yet defined or
998 return ""; # defined incorrectly
999 };
1000
1001 ## input: reference to a cell
1002 ## output: string specifying the monoclinic setting for that cell
1003 ## this is called by the make method
1004 sub determine_monoclinic {
1005 my $self = $_[0];
1006 ## need to know the space group, the user-provided symbol, and the
1007 ## unique axis (determined from the non-90 degree angle)
1008 my $group = $self->{Space_group};
1009 ($self -> crystal_class() eq "monoclinic") || return;
1010 my $given = $self->{Given_group};
1011 my $axis = 0;
1012 ((abs( 90 - $self->{Alpha} )) > EPSILON) && ($axis = "a");
1013 ((abs( 90 - $self->{Beta} )) > EPSILON) && ($axis = "b");
1014 ((abs( 90 - $self->{Gamma} )) > EPSILON) && ($axis = "c");
1015 (! $axis) && do {
1016 if ($self->{Angle}) {
1017 $axis = substr($self->{Angle}, 0, 1);
1018 ($axis = lc($axis)) =~ tr/g/c/;
1019 };
1020 };
1021 (! $axis) && do { # apparently the non-90 angle has not
1022 return 0; # been set yet...
1023 };
1024 my $number = $$Xray::Xtal::r_space_groups{$group}{number};
1025 # if it has, then continue...
1026 ($given =lc($given)) =~ s/^\s+//; # lower case and strip leading blanks
1027 $given =~ s/\s+$//; # strip trailing blanks
1028 my $setting = $axis . "_unique";
1029 if ($given eq $number) {
1030 $given = $$Xray::Xtal::r_space_groups{$group}{settings}[0];
1031 };
1032 ## these groups have one cell choice for each unique axis
1033 foreach my $n (3,4,5,6,8,10,11,12) {
1034 ($number == $n) && return $setting;
1035 };
1036 ## groups 7, 13, 14 are p centered and have multiple cell choices
1037 ($group =~ /^p/i) && do {
1038 ($axis eq "b") && do {
1039 ($given =~ /c/i) && ($setting .= "_1");
1040 ($given =~ /n/i) && ($setting .= "_2");
1041 ($given =~ /a/i) && ($setting .= "_3");
1042 };
1043 ($axis eq "c") && do {
1044 ($given =~ /a/i) && ($setting .= "_1");
1045 ($given =~ /n/i) && ($setting .= "_2");
1046 ($given =~ /b/i) && ($setting .= "_3");
1047 };
1048 ($axis eq "a") && do {
1049 ($given =~ /b/i) && ($setting .= "_1");
1050 ($given =~ /n/i) && ($setting .= "_2");
1051 ($given =~ /c/i) && ($setting .= "_3");
1052 };
1053 };
1054 ## groups 9, 15 are c centered and have multiple cell choices
1055 ($group =~ /^c/i) && do {
1056 ($axis eq "b") && do {
1057 ($given =~ /^c/i) && ($setting .= "_1");
1058 ($given =~ /^a/i) && ($setting .= "_2");
1059 ($given =~ /^i/i) && ($setting .= "_3");
1060 };
1061 ($axis eq "c") && do {
1062 ($given =~ /^a/i) && ($setting .= "_1");
1063 ($given =~ /^b/i) && ($setting .= "_2");
1064 ($given =~ /^i/i) && ($setting .= "_3");
1065 };
1066 ($axis eq "a") && do {
1067 ($given =~ /^b/i) && ($setting .= "_1");
1068 ($given =~ /^c/i) && ($setting .= "_2");
1069 ($given =~ /^i/i) && ($setting .= "_3");
1070 };
1071 };
1072 ## if none of the preceding 6 blocks altered setting then there is a
1073 ## mismatch between the symbol and the unique axis, so return 0.
1074 ($setting =~ /_[123]$/) || ($setting = 'b_unique_1');
1075 return $setting;
1076 };
1077
1078 ## explicitly set the value of $self->{Setting} for a monoclinic
1079 ## groups regardless of how it was set in canonicalize_coordinate.
1080 sub enforce_monoclinic {
1081 my $self = shift;
1082 (($#_ == 1)||($#_ == 2)) || do {
1083 my $this = (caller(0))[3];
1084 croak "$this takes 1 or 2 arguments arguments";
1085 return;
1086 };
1087 my ($axis,$choice) = @_;
1088 ($axis =~ /\b[abc]\b/i) || do {
1089 my $this = (caller(0))[3];
1090 croak "The unique axis must be a b or c in $this";
1091 return;
1092 };
1093 ($choice) && do {
1094 ($choice =~ /\b[123]\b/) || do {
1095 my $this = (caller(0))[3];
1096 croak "The cell choice must be 1 2 or 3 in $this";
1097 return;
1098 };
1099 };
1100 $self->{Setting} = $axis . "_unique";
1101 ($choice) && ($self->{Setting} .= "_" . $choice);
1102 $self->{Setting} = lc($self->{Setting});
1103 return $self;
1104 };
1105
1106 =over 4
1107
1108 =item C<verify_cell>
1109
1110 This performs consistency checks on the all cell attributes. If
1111 anything suspicious turns up, warning or error messages are issued.
1112
1113 $cell -> verify_cell();
1114
1115 =back
1116
1117 =cut
1118
1119 sub verify_cell {
1120 my $self = shift;
1121 my ($fault, $error, $warning, $caution) = (0, 3, 2, 1);
1122 my $normal = 0;
1123 my $message = "";
1124 # 1: verify space group
1125 my $group = $self -> {Space_group};
1126 (exists $$Xray::Xtal::r_space_groups{$group}) || do {
1127 $fault = &set_fault($fault, $error);
1128 $message .= $self->{Given_group} . " " . $$Xray::Xtal::strings{not_valid};
1129 };
1130 # 2: axes must be positive
1131 (($self->{A}) > EPSILON) || do {
1132 $fault = &set_fault($fault, $error);
1133 ($message !~ /^\s*$/) && ($message .= $/);
1134 $message .= "x=$self->{A}, x " . $$Xray::Xtal::strings{positive} . ".";
1135 };
1136 (($self->{B}) > EPSILON) || do {
1137 $fault = &set_fault($fault, $error);
1138 ($message !~ /^\s*$/) && ($message .= $/);
1139 $message .= "y=$self->{B}, y " . $$Xray::Xtal::strings{positive} . ".";
1140 };
1141 (($self->{C}) > EPSILON) || do {
1142 $fault = &set_fault($fault, $error);
1143 ($message !~ /^\s*$/) && ($message .= $/);
1144 $message .= "z=$self->{C}, z " . $$Xray::Xtal::strings{positive} . ".";
1145 };
1146 # 3: rules for crystal classes
1147 my $class = $self -> crystal_class();
1148 # Triclinic
1149 ($class eq "triclinic") && do {
1150 $normal = ( (abs(90-$self->{Alpha}) > EPSILON) &&
1151 (abs(90-$self->{Beta} ) > EPSILON) &&
1152 (abs(90-$self->{Gamma}) > EPSILON) &&
1153 (abs($self->{A}-$self->{B}) > EPSILON) &&
1154 (abs($self->{A}-$self->{C}) > EPSILON) &&
1155 (abs($self->{B}-$self->{C}) > EPSILON) );
1156 $normal || do {
1157 $fault = &set_fault($fault, $caution);
1158 ($message !~ /^\s*$/) && ($message .= $/);
1159 $message .= $$Xray::Xtal::strings{triclinic_desc};
1160 };
1161 };
1162 # Monoclinic
1163 ($class eq "monoclinic") && do {
1164 $normal = ( ( ((abs(90-$self->{Alpha}) > EPSILON)
1165 xor
1166 (abs(90-$self->{Beta} ) > EPSILON))
1167 xor
1168 (abs(90-$self->{Gamma}) > EPSILON) ) &&
1169 (abs($self->{A}-$self->{B}) > EPSILON) &&
1170 (abs($self->{A}-$self->{C}) > EPSILON) &&
1171 (abs($self->{B}-$self->{C}) > EPSILON) );
1172 $normal || do {
1173 $fault = &set_fault($fault, $caution);
1174 ($message !~ /^\s*$/) && ($message .= $/);
1175 $message .= $$Xray::Xtal::strings{monoclinic_desc};
1176 };
1177 ($self->{Setting} =~ /unique/i) || do {
1178 $fault = &set_fault($fault, $error);
1179 ($message !~ /^\s*$/) && ($message .= $/);
1180 $message .= $$Xray::Xtal::strings{monoclinic_unknown};
1181 };
1182 };
1183 # Orthorhombic
1184 ($class eq "orthorhombic") && do {
1185 $normal = ( (abs(90-$self->{Alpha}) < EPSILON) &&
1186 (abs(90-$self->{Beta} ) < EPSILON) &&
1187 (abs(90-$self->{Gamma}) < EPSILON) &&
1188 (abs($self->{A}-$self->{B}) > EPSILON) &&
1189 (abs($self->{A}-$self->{C}) > EPSILON) &&
1190 (abs($self->{B}-$self->{C}) > EPSILON) );
1191 $normal || do {
1192 $fault = &set_fault($fault, $caution);
1193 ($message !~ /^\s*$/) && ($message .= $/);
1194 $message .= $$Xray::Xtal::strings{orthorhombic_desc};
1195 };
1196 };
1197 # Tetragonal
1198 ($class eq "tetragonal") && do {
1199 $normal = ( (abs(90-$self->{Alpha}) < EPSILON) &&
1200 (abs(90-$self->{Beta} ) < EPSILON) &&
1201 (abs(90-$self->{Gamma}) < EPSILON) &&
1202 (abs($self->{A}-$self->{B}) < EPSILON) &&
1203 (abs($self->{A}-$self->{C}) > EPSILON) );
1204 $normal || do {
1205 $fault = &set_fault($fault, $caution);
1206 ($message !~ /^\s*$/) && ($message .= $/);
1207 $message .= $$Xray::Xtal::strings{tetragonal_desc};
1208 };
1209 };
1210 # Hexagonal
1211 ($class eq "hexagonal") && do {
1212 $normal = ( (abs( 90-$self->{Alpha}) < EPSILON) &&
1213 (abs( 90-$self->{Beta} ) < EPSILON) &&
1214 (abs(120-$self->{Gamma}) < EPSILON) &&
1215 (abs($self->{A}-$self->{B}) < EPSILON) &&
1216 (abs($self->{A}-$self->{C}) > EPSILON) );
1217 $normal || do {
1218 $fault = &set_fault($fault, $warning);
1219 ($message !~ /^\s*$/) && ($message .= $/);
1220 $message .= $$Xray::Xtal::strings{hexagonal_desc};
1221 };
1222 };
1223 # Trigonal
1224 ($class eq "trigonal") && do {
1225 # rhombohedral
1226 if (($group =~ /^r/i) && (abs( 90-$self->{Alpha}) > EPSILON)) {
1227 $normal = ( (abs($self->{Alpha}- $self->{Beta}) < EPSILON) &&
1228 (abs($self->{Alpha}- $self->{Gamma}) < EPSILON) &&
1229 (abs($self->{Beta} - $self->{Gamma}) < EPSILON) &&
1230 (abs($self->{A} - $self->{B}) < EPSILON) &&
1231 (abs($self->{A} - $self->{C}) < EPSILON) &&
1232 (abs($self->{B} - $self->{C}) < EPSILON) );
1233 $normal || do {
1234 $fault = &set_fault($fault, $warning);
1235 ($message !~ /^\s*$/) && ($message .= $/);
1236 $message .= $$Xray::Xtal::strings{rhombohedral_desc};
1237 };
1238 # not rhombohedral
1239 } else {
1240 $normal = ( (abs($self->{A}-$self->{B}) < EPSILON) &&
1241 (abs($self->{A}-$self->{C}) > EPSILON) );
1242 $normal || do {
1243 $fault = &set_fault($fault, $warning);
1244 ($message !~ /^\s*$/) && ($message .= $/);
1245 $message .= $$Xray::Xtal::strings{trigonal_desc};
1246 };
1247 $normal = ( (abs( 90-$self->{Alpha}) < EPSILON) &&
1248 (abs( 90-$self->{Beta} ) < EPSILON) &&
1249 (abs(120-$self->{Gamma}) < EPSILON) );
1250 $normal || do {
1251 $fault = &set_fault($fault, $error);
1252 ($message !~ /^\s*$/) && ($message .= $/);
1253 $message .= $$Xray::Xtal::strings{trigonal_desc};
1254 };
1255 };
1256 };
1257 # Cubic
1258 ($class eq "cubic") && do {
1259 $normal = ( (abs(90-$self->{Alpha}) < EPSILON) &&
1260 (abs(90-$self->{Beta} ) < EPSILON) &&
1261 (abs(90-$self->{Gamma}) < EPSILON) &&
1262 (abs($self->{A}-$self->{B}) < EPSILON) &&
1263 (abs($self->{B}-$self->{C}) < EPSILON) &&
1264 (abs($self->{A}-$self->{C}) < EPSILON) );
1265 $normal || do {
1266 $fault = &set_fault($fault, $caution);
1267 ($message !~ /^\s*$/) && ($message .= $/);
1268 $message .= $$Xray::Xtal::strings{cubic_desc};
1269 };
1270 };
1271 ($fault >=3 ) &&
1272 Xray::Xtal::trap_error($message . "$/$/ " .
1273 $$Xray::Xtal::strings{fatal_error} . "$/", 0);
1274 return $fault;
1275 };
1276
1277 sub set_fault {
1278 ##use Term::ANSIColor;
1279 my ($fault, $level) = @_;
1280 my @message = (" ", " $$Xray::Xtal::strings{caution}: ",
1281 " $$Xray::Xtal::strings{warning}: ",
1282 " $$Xray::Xtal::strings{error} ");
1283 my @colors = ("", "green", "yellow", "red");
1284 ##warn color($colors[$level]), $message[$level], $string, color("reset"), "$/";
1285 $fault = ($level > $fault) ? $level : $fault;
1286 return $fault;
1287 };
1288
1289 ## sub color {
1290 ## ($_[0] eq "") && return;
1291 ## ($_[0] eq "red") && return "";
1292 ## ($_[0] eq "green") && return "";
1293 ## ($_[0] eq "yellow") && return "";
1294 ## ($_[0] eq "blue") && return "";
1295 ## ($_[0] eq "magenta") && return "";
1296 ## ($_[0] eq "cyan") && return "";
1297 ## ($_[0] eq "white") && return "";
1298 ## ($_[0] eq "reset") && return "";
1299 ## };
1300
1301 =over 4
1302
1303 =item C<metric>
1304
1305 Takes the three fractional coordinates and returns the cartesian
1306 coordinates of the position. The fractional coordinates need not be
1307 canonicalized into the first octant, thus this method can be used to
1308 generate the cartesian coordinates for any atom in a cluster.
1309
1310 ($x,$y,$z) = $cell -> metric($xf, $yf, $zf);
1311
1312 This method is called repeatedly by the C<build_cluster> function in
1313 the Xray::Atoms module. The elements of the metric tensor, i.e. the
1314 C<Txx>, C<Tyx>, C<Tyz>, C<Tzx>, and C<Tz> Cell attributes, are used to
1315 make the transformation according to this formula:
1316
1317 / Txx 0 0 \ / xf \
1318 (x y z) = | Tyx 1 Tyz | | yf |
1319 \ Tzx 0 Tzz / \ zf /
1320
1321 =back
1322
1323 =cut
1324
1325 ## note that sprintf is a significant speed hit given the large number
1326 ## of times this function is called, but it does assure a given level
1327 ## of precision
1328 sub metric {
1329 my $self = shift;
1330 my ($x,$y,$z) = @_;
1331 my ($xp, $yp, $zp);
1332 ($x, $y, $z) = ($x*$self->{A}, $y*$self->{B}, $z*$self->{C});
1333 $xp = sprintf "%11.7f", $x*$self->{Txx};
1334 $yp = sprintf "%11.7f", $x*$self->{Tyx} + $y + $z*$self->{Tyz};
1335 $zp = sprintf "%11.7f", $x*$self->{Tzx} + $z*$self->{Tzz};
1336 return ($xp,$yp,$zp);
1337 };
1338
1339 =over 4
1340
1341 =item C<d_spacing>
1342
1343 Takes the Miller indeces of a scattering plane and returns the d
1344 spacing of that plane in Angstroms.
1345
1346 $d = $cell -> d_spacing($h, $k, $l);
1347
1348 =back
1349
1350 =cut
1351
1352 sub d_spacing {
1353 my $self = shift;
1354 my ($h, $k, $l) = @_;
1355 return 0 unless ($h or $k or $l);
1356 my $alpha = $self->{Alpha} * PI / 180;
1357 my $beta = $self->{Beta} * PI / 180;
1358 my $gamma = $self->{Gamma} * PI / 180;
1359
1360 my $s11 = ($self->{B}*$self->{C}*sin($alpha))**2;
1361 my $s22 = ($self->{A}*$self->{C}*sin($beta ))**2;
1362 my $s33 = ($self->{A}*$self->{B}*sin($gamma))**2;
1363
1364 my $s12 = $self->{A} * $self->{B} * ($self->{C}**2) *
1365 ( cos($alpha)*cos($beta) - cos($gamma) );
1366 my $s23 = $self->{B} * $self->{C} * ($self->{A}**2) *
1367 ( cos($beta) *cos($gamma) - cos($alpha) );
1368 my $s13 = $self->{C} * $self->{A} * ($self->{B}**2) *
1369 ( cos($gamma)*cos($alpha) - cos($beta) );
1370
1371 my $d = $s11*($h**2) + $s22*($k**2) + $s33*($l**2) +
1372 2*$s12*$h*$k + 2*$s23*$k*$l + 2*$s13*$h*$l;
1373 $d = $self->{Volume} / sqrt($d);
1374 return $d;
1375 };
1376
1377
1378
1379 =over 4
1380
1381 =item C<multiplicity>
1382
1383 Returns the multiplicity of a reflection hkl for the cell.
1384
1385 $p = $cell -> multiplicity($h, $k, $l);
1386
1387 See the footnote in Cullity page 523 for a caveat.
1388
1389 =back
1390
1391 =cut
1392
1393 sub multiplicity {
1394 my $self= shift;
1395 my $class = $self -> crystal_class();
1396 my ($h, $k, $l) = @_;
1397 my @r = sort($h, $k, $l);
1398 SWITCH:
1399 ($class eq 'cubic') and do {
1400 (not $r[0]) && (not $r[1]) && $r[2] && return 6;
1401 ($r[0] == $r[1]) && ($r[1] == $r[2]) && return 8;
1402 (not $r[0]) && ($r[1] == $r[2]) && return 12;
1403 (not $r[0]) && ($r[1] != $r[2]) && return 24; #*
1404 ($r[0] == $r[1]) && ($r[1] != $r[2]) && return 24;
1405 ($r[0] != $r[1]) && ($r[1] == $r[2]) && return 24;
1406 return 48
1407 };
1408 (($class eq 'hexagonal') || ($class eq 'trigonal')) and do {
1409 (not $h) && (not $k) && $l && return 2;
1410 (not $l) && ((not $h) || (not $k)) && return 6;
1411 (not $l) && ($h == $k) && return 6;
1412 (not $l) && ($h != $k) && return 12; #*
1413 $l && ((not $h) || (not $k)) && return 12; #*
1414 $l && ($h == $k) && return 12; #*
1415 return 24; #*
1416 };
1417 ($class eq 'tetragonal') and do{
1418 (not $h) && (not $k) && $l && return 2;
1419 (not $l) && ((not $h) || (not $k)) && return 4;
1420 (not $l) && ($h == $k) && return 4;
1421 (not $l) && ($h != $k) && return 8; #*
1422 $l && ((not $h) || (not $k)) && return 8;
1423 $l && ($h == $k) && return 8;
1424 return 16; #*
1425 };
1426 ($class eq 'orthorhombic') and do{
1427 (not $r[0]) && (not $r[1]) && return 2;
1428 (not $r[0]) || (not $r[1]) && return 4;
1429 return 4;
1430 };
1431 ($class eq 'monoclinic') and do{
1432 (not $r[0]) || (not $r[1]) && return 2;
1433 return 4;
1434 };
1435 ($class eq 'triclinic') and return 2;
1436 };
1437
1438
1439 =over 4
1440
1441 =item C<central>
1442
1443 This method takes one argument, a string specifying the tag of the
1444 central atom. It returns a four element list of the central atom
1445 elemental symbol and its fractional coordinates.
1446
1447 ($elem_c, $x_c, $y_c, $z_c) = $cell -> central($central_tag)
1448
1449 In many cases there is more than one choice for the central atom. Any
1450 of the various crystallographically identical positions matching the
1451 tag of the central atom can be chosen. This method returns the one
1452 closest to the center of the unit cell, i.e. the one closest to
1453 (1/2,1/2,1/2). The meaning of the word "closest" is a bit strange in
1454 this context. It is the position with the smallest value of
1455
1456 sqrt( (x-0.5)**2 + (y-0.5)**2 + (z-0.5)**2 )
1457
1458 This is chosen for speed and efficiency in building a spherical
1459 cluster of atoms.
1460
1461 =back
1462
1463 =cut
1464
1465 sub central {
1466 my $self = shift;
1467 my $core = $_[0];
1468 my $list;
1469 my ($xcenter, $ycenter, $zcenter, $central, $is_host);
1470 return ("",0,0,0,0) unless ($self->{Contents});
1471 FIND: foreach my $site (@{$self->{Contents}}) {
1472 #print lc($core), " ", lc($ {$$site[3]}->{Tag}), $/ ;
1473 (lc($core) eq lc($ {$$site[3]}->{Tag})) && do {
1474 $central = $ {$$site[3]}->{Element};
1475 $is_host = $ {$$site[3]}->{Host};
1476 $list = $ {$$site[3]}->{Positions};
1477 last FIND;
1478 };
1479 };
1480 #my @cformula = ("", "", "");
1481 if ($list) {
1482 my ($dist, $best) = (0, 100000);
1483 foreach my $site (@$list) {
1484 $dist =
1485 sqrt((0.5-$$site[0])**2 + (0.5-$$site[1])**2 + (0.5-$$site[2])**2);
1486 if ($dist < $best) {
1487 ($xcenter, $ycenter, $zcenter) = ($$site[0], $$site[1], $$site[2]);
1488 $best = $dist;
1489 };
1490 };
1491 #print join(" ", $central, $xcenter, $ycenter, $zcenter), $/;
1492 ##($xcenter, $ycenter, $zcenter) =
1493 ## $self -> metric($xcenter, $ycenter, $zcenter);
1494 return ($central, $xcenter, $ycenter, $zcenter, $is_host);
1495 };
1496 return ("",0,0,0,0);
1497 };
1498
1499
1500 =over 4
1501
1502 =item C<overfull>
1503
1504 This method returns the overfilled unit cell with atom positions in
1505 Cartesian coordinates. It takes an optional argument for specifying
1506 the epsilon defining which atoms should be considered close to a side,
1507 edge, or corner of the unit cell. The default for this value is 0.1
1508 and the units are fractional cell coordinates. The return value is a
1509 list structured the same as the Contents attribute of the cell.
1510
1511 @overfull = $cell -> overfull($epsi);
1512
1513 Each element of C<@overfull> is an anonymous array containing the x,
1514 y, and z fractional coordinates and a reference to the Site that
1515 generated the position.
1516
1517 =back
1518
1519 =cut
1520 ;
1521 sub overfull {
1522 my $self = shift;
1523 my $epsi = $_[0] || EPSILON;
1524 my @list = ();
1525 foreach my $site (@{$self->{Contents}}) {
1526 my @p = ([0],[0],[0]);
1527 unless ($epsi < 0) {
1528 foreach my $i (0..2) {
1529 ($$site[$i] < $epsi) && ($p[$i] = [0,1]); # near 0
1530 ((1-$$site[$i]) < $epsi) && ($p[$i] = [-1,0]); # near 1
1531 };
1532 };
1533 foreach my $a (@{$p[0]}) {
1534 foreach my $b (@{$p[1]}) {
1535 foreach my $c (@{$p[2]}) {
1536 my ($x, $y, $z) =
1537 $self -> metric($$site[0]+$a, $$site[1]+$b, $$site[2]+$c);
1538 push @list, [$x, $y, $z, $$site[3]];
1539 #push @list, [$$site[0]+$a, $$site[1]+$b, $$site[2]+$c, $$site[3]];
1540 };
1541 };
1542 };
1543 };
1544 return @list;
1545 };
1546
1547 =over 4
1548
1549 =item C<warn_shift>
1550
1551 This method returns a warning string if the space group is one for
1552 which the International Tables give two possible origin positions.
1553 This string suggests a value for the shift vector. An empty string
1554 is returned if only one origin is given.
1555
1556 print $cell -> warn_shift;
1557
1558 =back
1559
1560 =cut
1561
1562 sub get_shift {
1563 my $group = $_[0];
1564 my $setting;
1565 ($group, $setting) = canonicalize_symbol($group);
1566 (exists $$Xray::Xtal::r_space_groups{$group}{shiftvec}) &&
1567 return @{$$Xray::Xtal::r_space_groups{$group}{shiftvec}};
1568 return ();
1569 };
1570 sub warn_shift {
1571 my $self = shift;
1572 my $group = $self->{Space_group};
1573 (exists $$Xray::Xtal::r_space_groups{$group}{shiftvec}) && do {
1574 my $vec = join(", ", @{$$Xray::Xtal::r_space_groups{$group}{shiftvec}});
1575 my $message = join("", $/, " \"", ucfirst($group), "\" ",
1576 $$Xray::Xtal::strings{warn_shift},
1577 $/, "\t", $vec, $/, $/);
1578 return $message;
1579 };
1580 return "";
1581 };
1582
1583 =over 4
1584
1585 =item C<cell_check>
1586
1587 This method returns a warning if the cell axes and angles are not
1588 appropriate to the space group. It returns an empty string if they
1589 are appropriate.
1590
1591 print $cell -> cell_check;
1592
1593 =back
1594
1595 =cut
1596
1597 sub cell_check {
1598 my $self = shift;
1599 my $class = $self -> crystal_class();
1600 my $aa = $self->{A};
1601 my $bb = $self->{B};
1602 my $cc = $self->{C};
1603 my $alpha = $self->{Alpha};
1604 my $beta = $self->{Beta};
1605 my $gamma = $self->{Gamma};
1606 my $from_cell = "";
1607 DETERMINE: {
1608 # cubic
1609 ((abs($aa-$bb) < EPSILON) &&
1610 (abs($aa-$cc) < EPSILON) &&
1611 (abs($bb-$cc) < EPSILON) &&
1612 (abs($alpha-90) < EPSILON) &&
1613 (abs($beta-90) < EPSILON) &&
1614 (abs($gamma-90) < EPSILON)) && do {
1615 $from_cell = "cubic";
1616 last DETERMINE;
1617 };
1618 # tetragonal
1619 ((abs($aa-$bb) < EPSILON) &&
1620 (abs($aa-$cc) > EPSILON) &&
1621 (abs($alpha-90) < EPSILON) &&
1622 (abs($beta-90) < EPSILON) &&
1623 (abs($gamma-90) < EPSILON)) && do {
1624 $from_cell = "tetragonal";
1625 last DETERMINE;
1626 };
1627 # hexagonal or trigonal
1628 ((abs($aa-$bb) < EPSILON) &&
1629 (abs($aa-$cc) > EPSILON) &&
1630 (abs($alpha-90) < EPSILON) &&
1631 (abs($beta-90) < EPSILON) &&
1632 (abs($gamma-120) < EPSILON)) && do {
1633 $from_cell = "hexagonal";
1634 last DETERMINE;
1635 };
1636 # rhombohedral
1637 ((abs($aa-$bb) < EPSILON) &&
1638 (abs($aa-$cc) < EPSILON) &&
1639 (abs($bb-$cc) < EPSILON) &&
1640 (abs($alpha-$beta) < EPSILON) &&
1641 (abs($alpha-$gamma) < EPSILON) &&
1642 (abs($beta-$gamma) < EPSILON)) && do {
1643 $from_cell = "hexagonal";
1644 last DETERMINE;
1645 };
1646 # orthorhombic
1647 ((abs($aa-$bb) > EPSILON) &&
1648 (abs($aa-$cc) > EPSILON) &&
1649 (abs($bb-$cc) > EPSILON) &&
1650 (abs($alpha-90) < EPSILON) &&
1651 (abs($beta-90) < EPSILON) &&
1652 (abs($gamma-90) < EPSILON)) && do {
1653 $from_cell = "orthorhombic";
1654 last DETERMINE;
1655 };
1656 # triclinic
1657 ((abs($aa-$bb) > EPSILON) &&
1658 (abs($aa-$cc) > EPSILON) &&
1659 (abs($bb-$cc) > EPSILON) &&
1660 (abs($alpha-90) > EPSILON) &&
1661 (abs($beta-90) > EPSILON) &&
1662 (abs($gamma-90) > EPSILON)) && do {
1663 $from_cell = "triclinic";
1664 last DETERMINE;
1665 };
1666 # monoclinic
1667 ((abs($aa-$bb) > EPSILON) &&
1668 (abs($aa-$cc) > EPSILON) &&
1669 (abs($bb-$cc) > EPSILON) &&
1670 ((abs($alpha-90) > EPSILON) ||
1671 (abs($beta-90) > EPSILON) ||
1672 (abs($gamma-90) > EPSILON))) && do {
1673 $from_cell = "monoclinic";
1674 last DETERMINE;
1675 };
1676 };
1677 ( (($from_cell) eq "hexagonal") &&
1678 (($class eq "trigonal") || ($class eq "hexagonal")) ) &&
1679 return "";
1680 my $extra_message = "";
1681 ($extra_message = "Trigonal cells have x=y<>z and alpha=beta=90 and gamma=120.")
1682 if ($class eq "trigonal");
1683 ($extra_message = "Triclinic cells have all unequal axes and angles.")
1684 if ($class eq "triclinic");
1685
1686 ($class eq $from_cell) || do {
1687 my $message = join("", $/, " ",
1688 "The axis lengths and angles specified are not",
1689 $/,
1690 "appropriate for the given space group.",
1691 $/,
1692 $extra_message,
1693 $/, $/);
1694 return $message;
1695 };
1696 return "";
1697 };
1698
1699 =over 4
1700
1701 =item C<get_symmetry_table>
1702
1703 Return the list of symmetry operations for a space group (and,
1704 optionally, a setting). This returns an anonymous list of lists.
1705 Each list element contains three strings -- the three strings which
1706 are eval-ed to generate the positions in the unit cell.
1707
1708 $positions = $cell -> get_symmetry_table;
1709 @first_position = @ {$positions[0]};
1710
1711 =back
1712
1713 =cut
1714
1715 sub get_symmetry_table {
1716 my $self = shift;
1717 my $group = $self->{Space_group};
1718 my $positions = $self->{Setting};
1719 ($positions !~ /\b[0-5]\b/) || ($positions = "positions");
1720 (defined $$Xray::Xtal::r_space_groups{$group}{$positions}) &&
1721 return $$Xray::Xtal::r_space_groups{$group}{$positions};
1722 (defined $$Xray::Xtal::r_space_groups{$group}{b_unique}) &&
1723 return $$Xray::Xtal::r_space_groups{$group}{b_unique};
1724 (defined $$Xray::Xtal::r_space_groups{$group}{b_unique_1}) &&
1725 return $$Xray::Xtal::r_space_groups{$group}{b_unique_1};
1726 return [];
1727 };
1728
1729
1730
1731
1732 sub set_ipots {
1733 my $self = shift;
1734 my $style = $_[0]; # assignment scheme
1735 my $is_mol = $_[1]; # optional molecule flag
1736 my $tag = $_[2]; # optional central atom tag
1737 my $nip = 0;
1738 my %ipots = ();
1739 my $which;
1740 STYLES: {
1741 ($style eq "species") && do {
1742 $which = 'Element'; last STYLES;
1743 };
1744 ($style eq "tags") && do {
1745 $which = 'Tag'; last STYLES;
1746 };
1747 ($style eq "sites") && do {
1748 $which = 'Id'; last STYLES;
1749 };
1750 };
1751 foreach my $site (@{$self->{Contents}}) {
1752 #require Data::Dumper;
1753 #print Data::Dumper->Dump([\$site],[qw(*site)]);
1754 next unless ( $ {$$site[3]}->{Host} );
1755 next if ( $is_mol and (lc($tag) eq lc($ {$$site[3]}->{Tag})) );
1756 my $this = $ {$$site[3]}->{$which};
1757 ($style eq "tags") or $this = lc($this);
1758 (exists $ipots{$this}) or ($ipots{$this} = ++$nip);
1759 };
1760 return %ipots;
1761 };
1762
1763
1764 sub describe_group {
1765 my $group = $_[0];
1766 return $$Xray::Xtal::r_space_groups{$group};
1767 };
1768
1769
1770
1771 1;
1772
1773
1774 ######################################################################
1775
1776 =head2 The Site object
1777
1778 The Site object is a blessed hash. It predefines several attributes
1779 of a Site, but allows the user to define new attributes on the fly.
1780 The predefined (i.e. the ones that are used in Atoms) attributes are:
1781
1782 =over 4
1783
1784 =item B<Element>
1785
1786 The two letter symbol for the chemical species.
1787
1788 =item B<Tag>
1789
1790 A character string identifying a unique crystallographic site.
1791
1792 =item B<X>, B<Y>, B<Z>
1793
1794 The fractional coordinates of the sites (but see L<"MOLECULES"> below).
1795
1796 =item B<B>
1797
1798 The thermal spheroid parameter for the site.
1799
1800 =item B<Valence>
1801
1802 The formal valence for the element occupying the site.
1803
1804 =item B<Occupancy>
1805
1806 The fractional occupancy of the site. This allows the user to specify
1807 dopants.
1808
1809 =item B<Host>
1810
1811 This is 1 if the site is a host atom and 0 if it is a dopant.
1812
1813 =item B<Positions>
1814
1815 This takes an anonymous array of symmetry equivalent sites. This is
1816 filled after calling the C<populate> method.
1817
1818 =item B<File>
1819
1820 The name of an external file to be used with the site.
1821
1822 =item B<Id>
1823
1824 A pseudo-random number assigned by the C<new> method to uniquely
1825 identify the object.
1826
1827 =item B<Color>
1828
1829 The color assigned to the site in a ball-and-stick image.
1830
1831 =back
1832
1833
1834 =head2 Methods and Functions of the Site Package
1835
1836 See the descriptions of C<attributes> and C<clear> in the Cell Package
1837 section.
1838
1839 =cut
1840
1841 package Xray::Xtal::Site;
1842
1843 use constant EPSILON => 0.00001;
1844
1845 use strict;
1846 use vars qw(@ISA @EXPORT @EXPORT_OK);
1847
1848 require Exporter;
1849 require Xray::Xtal;
1850
1851 @ISA = qw(Exporter AutoLoader Xray::Xtal);
1852 @EXPORT_OK = qw(canonicalize_coordinate);
1853
1854 use Carp;
1855 use Chemistry::Elements qw(get_name get_Z get_symbol);
1856
1857 use vars qw($molecule);
1858 $molecule = 0;
1859
1860 ## constructor for a new crystallographic site
1861 sub new {
1862 my $classname = shift;
1863 my $self = {};
1864 my $index = shift || 0;
1865 $self->{Element} = q{};
1866 $self->{Tag} = q{};
1867 $self->{Utag} = q{};
1868 $self->{X} = 0;
1869 $self->{Y} = 0;
1870 $self->{Z} = 0;
1871 $self->{B} = 0;
1872 $self->{Bx} = 0; # |
1873 $self->{By} = 0; # > crystallographic thermal factors
1874 $self->{Bz} = 0; # |
1875 $self->{Valence} = 0; # intended for Cromer-Mann tables
1876 $self->{Occupancy} = 1;
1877 $self->{Host} = 1;
1878 $self->{Positions} = 0; # filled in by populate method
1879 $self->{Formulas} = 0; # filled in by populate method
1880 $self->{File} = q{};
1881 $self->{Id} = int(rand 100000) + 1;
1882 bless($self, $classname);
1883 $self->{Color} = $self -> default_color($index);
1884 $self->{message_buffer} = q{},
1885 return $self;
1886 };
1887 ## $self->{Dopants} = 0; # a list of some sort...
1888
1889 ## method for filling in attributes of a site
1890 sub make {
1891 my $self = shift;
1892 my @attributes = qw(Element Tag X Y Z Bx By Bz Valence Color
1893 Occupancy Host Positions);
1894 ($#_ % 2) || do {
1895 my $this = (caller(0))[3];
1896 croak "$this takes an even number of arguments";
1897 return; };
1898 my $this_message = q{};
1899 while (@_) {
1900 my $att = ucfirst($_[0]);
1901 ## there are a few checks that can be done...
1902 ATT: {
1903 ($att eq "Positions") && do {
1904 my $this = (caller(0))[3];
1905 ##carp "You should not set the Positions attribute of a site by hand$/";
1906 ##carp "Use the populate method instead.$/";
1907 $this_message = "You should not set the Positions attribute of a site by hand$/Use the populate method instead.$/";
1908 $self->make(message_buffer => $self->{message_buffer} . $this_message);
1909 last ATT;
1910 };
1911 ($att eq "Id") && do {
1912 my $this = (caller(0))[3];
1913 ##carp "You should not set the Id attribute of a site by hand$/";
1914 $this_message = "You should not set the Id attribute of a site by hand$/";
1915 $self->make(message_buffer => $self->{message_buffer} . $this_message);
1916 last ATT;
1917 };
1918 ## element can be a symbol, name, or number
1919 ## return "--" for an improper element
1920 ($att eq "Element") && do {
1921 my $sym = get_symbol($_[1]);
1922 unless (defined $sym) {$sym = "--"};
1923 $self->{"Element"} = $sym;
1924 last ATT;
1925 };
1926 ## occupancy must be between 0 and 1
1927 ($att eq "Occupancy") && do {
1928 if ($_[1] < 0) {
1929 $self->{"Occupancy"} = 0;
1930 $this_message = " " . $$Xray::Xtal::strings{site_occ} . $/;
1931 $self->make(message_buffer => $self->{message_buffer} . $this_message);
1932 last ATT;
1933 };
1934 if ($_[1] > 1) {
1935 $self->{"Occupancy"} = 1;
1936 $this_message = " " . $$Xray::Xtal::strings{site_occ} . $/;
1937 $self->make(message_buffer => $self->{message_buffer} . $this_message);
1938 last ATT;
1939 };
1940 $self->{"Occupancy"} = $_[1];
1941 last ATT;
1942 };
1943 ## thermal factors are positive definate (units of AA^2)
1944 ($att =~ /B[xyz]/) && do {
1945 my $val = ($_[1] > 0) ? $_[1] : 0;
1946 $self->{$att} = $val;
1947 last ATT;
1948 };
1949 ## some other possibilities:
1950 ## -- check valence against Cromer-Mann tables
1951 ## -- convert color to an RGB triplet
1952 do {
1953 $self->{ $att } = $_[1];
1954 $self->{ $att } =~ s/^\s+$//;
1955 };
1956 };
1957 shift; shift;
1958 }
1959 $self->{Tag} ||= ucfirst($self->{Element});
1960 unless ($molecule) {
1961 $self->{X} = canonicalize_coordinate($self->{X});
1962 $self->{Y} = canonicalize_coordinate($self->{Y});
1963 $self->{Z} = canonicalize_coordinate($self->{Z});
1964 };
1965 return $self;
1966 };
1967
1968 =over 4
1969
1970 =item C<reset_message_buffer>
1971
1972 Reset the message buffer for this object to the empty string.
1973
1974 =back
1975
1976 =cut
1977
1978 sub reset_message_buffer {
1979 my $self = shift;
1980 $self -> make(message_buffer => q{});
1981 }
1982
1983 =over 4
1984
1985 =item C<clear>
1986
1987 Reset a site without destroying it. For a site, this resets all
1988 predefined attributes to their initial values and all user-defined
1989 attributes to 0.
1990
1991 $site -> clear();
1992
1993 =back
1994
1995 =cut
1996 ## reset a site without destroying it
1997 sub clear {
1998 my $self = shift;
1999 foreach my $key (keys %$self) {
2000 next if ($key eq "Id");
2001 $self->{$key} = 0;
2002 };
2003 ## these three predefined attributes are reset to strings
2004 $self->{Element} = "";
2005 $self->{Tag} = "";
2006 $self->{Color} = "black";
2007 return $self;
2008 };
2009
2010 =over 4
2011
2012 =item C<populate>
2013
2014 This applied the symmetry operations implied by the space group of the
2015 Cell object C<$cell> to a unique crystallographic site. The
2016 collection of symmetry related sites will be stored in the site object
2017 as the C<Positions> attribute.
2018
2019 $site -> populate($cell);
2020
2021 Typically, it is not necessary to call this explicitly as it is called
2022 for each site by the Cell populate method.
2023
2024 =back
2025
2026 =cut
2027 ## apply the symmetries of $group to a site, canonicalize to the first
2028 ## octant, and weed out the repeats. This subroutine is the real
2029 ## workhorse of the Site package.
2030 ## input: reference to a cell object
2031 ## output: a list of lists, the coordinates of the equivalent sites in
2032 ## the unit cell
2033 sub populate {
2034 my $self = shift;
2035 my ($cell) = $_[0];
2036 my $group = $cell->{Space_group};
2037 my $given = $cell->{Given_group};
2038 my $setting = $cell->{Setting};
2039 my $x = $self->{X}; # take x,y,z from $self
2040 my $y = $self->{Y};
2041 my $z = $self->{Z};
2042 my $utag = "_" . $self->{Utag};
2043 my @list;
2044 my $bravais = $cell->{Bravais};
2045 ## (@bravais) && (print join(",", @$bravais), "$/");
2046
2047 #-------------------------- handle different settings as needed
2048 my $positions = "positions";
2049 my $crystal_class = $cell -> crystal_class();
2050 #print ">>>", $crystal_class, $/;
2051 my $do_ortho = ($crystal_class eq "orthorhombic" ) && ($setting);
2052 my $do_tetr = ($crystal_class eq "tetragonal" ) && ($setting);
2053 ($crystal_class eq "monoclinic") && do {
2054 $positions = $cell->{Setting};
2055 # bravais vector for the //given// symbol
2056 };
2057 ($group =~ /^r/i) && ($positions = $setting ? $setting : $positions);
2058 ($positions) || do {
2059 my $this = (caller(0))[3];
2060 croak "Invalid positions specifier in $this";
2061 return;
2062 };
2063
2064 #-------------------------- permute to alternate settings (orthorhombic)
2065 # 1..5 |--> [ ba-c, cab, -cba, bca, a-cb ]
2066 ($do_ortho) && do {
2067 FORWARD: {
2068 ($setting == 1) && do {
2069 ( ($x, $y, $z) = ( $y, $x, -$z) ); last FORWARD;
2070 };
2071 ($setting == 2) && do {
2072 ( ($x, $y, $z) = ( $y, $z, $x) ); last FORWARD;
2073 };
2074 ($setting == 3) && do {
2075 ( ($x, $y, $z) = ( $z, $y, -$x) ); last FORWARD;
2076 };
2077 ($setting == 4) && do {
2078 ( ($x, $y, $z) = ( $z, $x, $y) ); last FORWARD;
2079 };
2080 ($setting == 5) && do {
2081 ( ($x, $y, $z) = ( $x, $z, -$y) ); last FORWARD;
2082 };
2083 };
2084 };
2085 #-------------------------- rotate from F or C settings to P or I
2086 ($do_tetr) and do {
2087 ($x, $y) = ($x-$y, $x+$y);
2088 #my ($a, $b) = ($$r_cell->{A}, $$r_cell->{B});
2089 #$$r_cell -> make(A=>$a/sqrt(2), B=>$b/sqrt(2));
2090 #($$r_cell->{A}, $$r_cell->{B}) =
2091 # ($$r_cell->{A}/sqrt(2), $$r_cell->{B}/sqrt(2));
2092 };
2093
2094 ## ----- evaluate the coordinates safely
2095 ## see `perldoc Safe' for details
2096 my $message = $$Xray::Xtal::strings{tainted_sgdb} . $/;
2097 ## my $cpt = new Safe;
2098 ## ## need to load $x, $y, $z into the safe compartment
2099 ## $ {$cpt->varglob('x')} = $x;
2100 ## $ {$cpt->varglob('y')} = $y;
2101 ## $ {$cpt->varglob('z')} = $z;
2102 #print join(" ", $x, $y, $z, $/);
2103
2104 #---------------------------- loop over all symmetry operations
2105 foreach my $position (@{ $$Xray::Xtal::r_space_groups{$group}{$positions} }) {
2106 my $i = 0;
2107 my ($xpos, $ypos, $zpos) = ( $$position[0], $$position[1], $$position[2] );
2108
2109 foreach ($xpos, $ypos, $zpos) {
2110 ## the regex is intended to be an exhaustive list of characters
2111 ## found in the symmetry part of the space groups database.
2112 ## This is not bomber security as it is possible to, say,
2113 ## somehow alias "y56z-3" to "rm -rf ~". But I think this will
2114 ## foil the casual black hat.
2115 ($_ =~ /([^-1-6xyzXYZ+\$\/])/) and
2116 Xray::Xtal::trap_error("$message\nfirst bad character: $1$/", 0);
2117 };
2118 ($xpos, $ypos, $zpos) = map {eval $_} ($xpos, $ypos, $zpos);
2119
2120 ##print join(" ", $xpos, $ypos, $zpos, $/);
2121 my @f = @$position; # store formulas for this position
2122 map {s/\$//g} @f; # remove dollar sign
2123 map {s/([xyz])/$1$utag/g} @f; # append unique tag
2124
2125 ## $ {$cpt->varglob('xx')} = $cpt->reval($xpos);
2126 ## $ {$cpt->varglob('yy')} = $cpt->reval($ypos);
2127 ## $ {$cpt->varglob('zz')} = $cpt->reval($zpos);
2128 ## ($xpos, $ypos, $zpos) = ($ {$cpt->varglob('xx')},
2129 ## $ {$cpt->varglob('yy')},
2130 ## $ {$cpt->varglob('zz')} );
2131 ## test_safe_return($message, $xpos, $ypos, $zpos);
2132 ## ----- end of safe evaluation
2133
2134 my ($xposi, $yposi, $zposi) = ($xpos, $ypos, $zpos);
2135
2136 #-------------------------- permute back from alt. settings (orthorhombic)
2137 ($do_ortho) && do {
2138 ($setting == 1) && (($xposi, $yposi, $zposi) = ( $yposi, $xposi,-$zposi));
2139 ($setting == 2) && (($xposi, $yposi, $zposi) = ( $zposi, $xposi, $yposi));
2140 ($setting == 3) && (($xposi, $yposi, $zposi) = (-$zposi, $yposi, $xposi));
2141 ($setting == 4) && (($xposi, $yposi, $zposi) = ( $yposi, $zposi, $xposi));
2142 ($setting == 5) && (($xposi, $yposi, $zposi) = ( $xposi,-$zposi, $yposi));
2143 };
2144 # need to rectify formulas for orthorhombic settings
2145
2146 #-------------------------- permute back to F or C settings from P or I
2147 #($do_tetr) and ($x, $y) = ($x-$y, $x+$y);
2148
2149 #-------------------------- canonicalize and push onto list
2150 ($xposi, $yposi, $zposi) = (&canonicalize_coordinate($xposi),
2151 &canonicalize_coordinate($yposi),
2152 &canonicalize_coordinate($zposi));
2153 push @list, [$xposi, $yposi, $zposi, @f];
2154
2155 #-------------------------- do Bravais translations
2156 while ($i < $#{$bravais}) {
2157 ($xposi, $yposi, $zposi) = ($xpos+$$bravais[$i],
2158 $ypos+$$bravais[$i+1],
2159 $zpos+$$bravais[$i+2]);
2160 #------------------------ permute back from alt. settings (orthorhombic)
2161 ($do_ortho) && do {
2162 BACKWARD: {
2163 ($setting == 1) && do {
2164 (($xposi, $yposi, $zposi)=( $yposi, $xposi,-$zposi));
2165 last BACKWARD;
2166 };
2167 ($setting == 2) && do {
2168 (($xposi, $yposi, $zposi)=( $zposi, $xposi, $yposi));
2169 last BACKWARD;
2170 };
2171 ($setting == 3) && do {
2172 (($xposi, $yposi, $zposi)=(-$zposi, $yposi, $xposi));
2173 last BACKWARD;
2174 };
2175 ($setting == 4) && do {
2176 (($xposi, $yposi, $zposi)=( $yposi, $zposi, $xposi));
2177 last BACKWARD;
2178 };
2179 ($setting == 5) && do {
2180 (($xposi, $yposi, $zposi)=( $xposi,-$zposi, $yposi));
2181 last BACKWARD;
2182 };
2183 };
2184 };
2185
2186 #-------------------------- canonicalize and push this bravais position
2187 ($xposi, $yposi, $zposi) = (&canonicalize_coordinate($xposi),
2188 &canonicalize_coordinate($yposi),
2189 &canonicalize_coordinate($zposi));
2190 my @ff = @f; # append bravais translation to formulas
2191 map {$ff[$_] = $f[$_] . " + " . $$bravais[$i+$_]} (0 .. 2);
2192 push @list, [$xposi, $yposi, $zposi, @ff];
2193 $i+=3;
2194 };
2195 };
2196 #---------------------------- Weed out repeats.
2197 my @form = ();
2198 my %seen = ();
2199 my @uniq = (); # see section 4.6, p.102,
2200 foreach my $item (@list) { # The Perl Cookbook, 1st edition
2201 #my $keya = sprintf "%7.5f", $$item[0]; chop $keya;
2202 #my $keyb = sprintf "%7.5f", $$item[1]; chop $keyb;
2203 #my $keyc = sprintf "%7.5f", $$item[2]; chop $keyc;
2204 my $keya = sprintf "%6.4f", $$item[0]; chop $keya;
2205 my $keyb = sprintf "%6.4f", $$item[1]; chop $keyb;
2206 my $keyc = sprintf "%6.4f", $$item[2]; chop $keyc;
2207 my $key = $keya . $keyb . $keyc;
2208 unless ($seen{$key}++) {
2209 push (@uniq, [@$item[0..2]]);
2210 push (@form, [@$item[3..5]]);
2211 };
2212 };
2213 #---------------------------- return an anonymous array
2214 $self -> {Positions} = [@uniq];
2215 $self -> {Formulas} = [@form];
2216 return $self;
2217 };
2218
2219 ## an unsafe string will result in an undefined value
2220 sub test_safe_return {
2221 my $message = shift(@_);
2222 my @list = @_;
2223 foreach my $item (@list) {
2224 (defined $item) || Xray::Xtal::trap_error("$message$/", 0);
2225 };
2226 return 1;
2227 };
2228
2229
2230 my @default_color_list = qw(black red blue green brown orange yellow
2231 cyan magenta grey);
2232 sub default_color {
2233 my ($self, $index) = (shift, shift||0);
2234 $index %= 10;
2235 return $default_color_list[$index];
2236 };
2237
2238 =over 4
2239
2240 =item C<canonicalize_coordinate>
2241
2242 This the subroutine is part of the Site package. It takes a
2243 fractional coordinate and returns it shifted into the first octant.
2244 This is not actually a method of the Site class, it is just a normal
2245 function.
2246
2247 $coord = Xray::Xtal::Site::canonicalize_coordniate($coord);
2248
2249 =back
2250
2251 =cut
2252
2253 sub canonicalize_coordinate {
2254 my $pos = $_[0];
2255 return $pos if ($Xray::Xtal::Site::molecule == 1);
2256 $pos -= int($pos); # move to first octant
2257 ($pos < -1*EPSILON) && ($pos += 1);
2258 (abs($pos) < EPSILON) && ($pos = 0);
2259 SYM: { # positions of special symmetry
2260 if (abs($pos) < 0.00001) {($pos = 0); last SYM;}
2261 if (abs($pos-0.125) < 0.00001) {($pos = 1/8); last SYM;}
2262 if (abs($pos-0.1666) < 0.00001) {($pos = 1/6); last SYM;}
2263 if (abs($pos-0.25) < 0.00001) {($pos = 1/4); last SYM;}
2264 if (abs($pos-0.3333) < 0.00001) {($pos = 1/3); last SYM;}
2265 if (abs($pos-0.375) < 0.00001) {($pos = 3/8); last SYM;}
2266 if (abs($pos-0.5) < 0.00001) {($pos = 1/2); last SYM;}
2267 if (abs($pos-0.625) < 0.00001) {($pos = 5/8); last SYM;}
2268 if (abs($pos-0.6666) < 0.00001) {($pos = 2/3); last SYM;}
2269 if (abs($pos-0.75) < 0.00001) {($pos = 3/4); last SYM;}
2270 if (abs($pos-0.8333) < 0.00001) {($pos = 5/6); last SYM;}
2271 if (abs($pos-0.875) < 0.00001) {($pos = 7/8); last SYM;}
2272 if (abs($pos-1) < 0.00001) {($pos = 0); last SYM;}
2273 };
2274 return $pos;
2275 };
2276
2277 1;
2278
2279 package Xray::Xtal;
2280 1;
2281
2282 # Autoload methods go after =cut, and are processed by the autosplit program.
2283
2284 __END__
2285
2286
2287 =head1 INTERPRETING SPACE GROUP SYMBOLS
2288
2289 You may specify space group symbols in a variety of ways. When
2290 presented with a possible symbol, the Xtal module will first clean the
2291 symbol up by adding and/or removing whitespace then by trying each of
2292 the following ways of interpreting the symbol.
2293
2294 =over 4
2295
2296 =item 1.
2297
2298 A standard symbol from the 1995 edition of the International Tables
2299 for Crystallography. For example, the group for the cubic perovskite
2300 structure is C<P m -3 m>.
2301
2302 =item 2.
2303
2304 A Schoenflies symbol. The Scoenflies symbol for C<P m -3 m> is C<O_1^h>.
2305
2306 =item 3.
2307
2308 A symbol from the 1935 edition of the International tables. The 1935
2309 symbol for C<P m -3 m> is C<P m 3 m>.
2310
2311 =item 4.
2312
2313 The number for the entry of the space group in the International
2314 Tables. The number for C<P m -3 m> is 221.
2315
2316 =item 5.
2317
2318 The full symbol. For C<P m -3 m> this is C<P 4/m -3 2/m>.
2319
2320 =item 6.
2321
2322 An alternative symbol. See L<"LOW SYMMETRY SPACE GROUPS"> for details.
2323
2324 =item 7.
2325
2326 A shorthand phrase, such as C<fcc> for C<F m -3 m>. See (somewhere)
2327 for a complete list of shorthand phases.
2328
2329 =item 8.
2330
2331 A short symbol for a monoclinic space group. See
2332 L<"LOW SYMMETRY SPACE GROUPS"> for details.
2333
2334 =back
2335
2336 Since whitespace in the symbol is regularized, you can usually include
2337 or omit whitespace in any fashion. For example C<pm3m> and
2338 C<P m -3 m> both come out as C<P m -3 m>, just
2339 as you would expect.
2340
2341 A space group is specified using the C<make> method from the Cell
2342 class. When this is done the original symbol is stored in the
2343 Given_group attribute of the Cell object and the standard symbol
2344 interpreted from it is stored in the Space_group attribute. If the
2345 given symbol cannot be interpretted, the Space_group attribute is set
2346 to 0.
2347
2348 In the future, more flexibility may be added to the space group symbol
2349 interpretation scheme, including full symbols for alternate settings
2350 and more obsolete notation. I am open to suggestions.
2351
2352
2353
2354 =head1 LOW SYMMETRY SPACE GROUPS
2355
2356 There is a more complete discussion of low symmetry groups in the
2357 Atoms document. Here are the highlights:
2358
2359 =head2 Monoclinic Space Groups
2360
2361 Monoclinic groups can be quite confusing for the user. Because any of
2362 the three angles can be the unique (acute or obtuse rather than right)
2363 angle and because there are three possible settings for the cell, the
2364 standard symbol can be ambiguous. In that case, using the full symbol
2365 or the short monoclinic symbol would break the ambiguity and allow the
2366 make method to correctly assign the space group. An example would
2367 help illustrate this.
2368
2369 In "Structural phase diagram of La(1-x)Sr(x)MnO(3+delta): Relationship
2370 to magnetic and transport properties." by J.F. Mitchell et al,
2371 Phys. Rev. B54, no. 9, (1996), pp./ 6172-6183 the monoclinic structure
2372 for room temperature LnMnO3 is given in Table IV. The space group is
2373 given as C<P 21/c> and the setting is given as C<P 1 21/n 1>.
2374 Specifying the space group symbol will result in the wrong application
2375 of symmetries when the Contents attribute of the cell is filled.
2376 Specifying the full symbol for that setting will work correctly. The
2377 shorthand C<P 21/n> will also result in a correct applciation of
2378 symmetries.
2379
2380 In the Atoms document, there is a complete list of symbols recognized
2381 by Atoms, including all the short and full symbols for the different
2382 settings of monoclinic groups. It is essential to always use site
2383 coordinates and unique angles appropriate to the symbol (and vice
2384 versa).
2385
2386 In the strange case of crystal data in a monoclinic setting but with
2387 the unique angle equal to 90, it is necessary to specify the unique
2388 angle like so
2389
2390 $cell -> make(Angle=>'beta')
2391
2392 Without this, there is no way for the Cell class to know which setting
2393 to use.
2394
2395 =head2 Orthorhombic Space Groups
2396
2397 You may use any setting of an orthorhombic crystal, although it is up
2398 to you to be sure that the unique coordinates are appropriate to that
2399 setting. Atoms has no way of checking that!
2400
2401 =head2 Tetragonal Space Groups
2402
2403 F and C centered tetragonal cells are handled transparently by
2404 Atoms. If you find a literature reference using one of these, you can
2405 usually use the crystal data given in the article as written.
2406
2407 =head2 Rhombohedral Space Groups
2408
2409 You can specify either hexagonal or rhombohedral parameters for
2410 rhombohedral space groups. For the hexagonal representation, you must
2411 specify C<a> and C<b> and C<c>, C<alpha>, C<beta>, and C<gamma> will
2412 be set correctly by the C<make> method. For the rhombohedral
2413 representation, you must specify C<a> and C<alpha> and the rest will
2414 be set appropriately.
2415
2416 =head1 CHANGE LOG
2417
2418 =over 4
2419
2420 =item *
2421
2422 Apr 26 2004: fixed the shift vector for space group #129. (thanks to
2423 Pieter Glatzel)
2424
2425 =back
2426
2427 =head1 BUGS
2428
2429 Surely there are plenty. Send me email if you find any.
2430
2431 =head1 AUTHOR
2432
2433 Bruce Ravel <[email protected]>
2434 http://feff.phys.washington.edu/~ravel/software/
2435
2436 =head1 SEE ALSO
2437
2438 The documentation that comes with the Atoms package.
2439
2440 =cut
2441
2442 ## Local Variables:
2443 ## time-stamp-line-limit: 25
2444 ## End: