| 1 |
#! /usr/bin/perl -w |
| 2 |
###################################################################### |
| 3 |
## This is the Xray::Atoms.pm module. It contains various subroutines |
| 4 |
## used by the various versions of atoms. See the pod for details and |
| 5 |
## one of the atoms versions for how they are implemented. |
| 6 |
## |
| 7 |
## This program is copyright (c) 1998-2006, 2009 Bruce Ravel |
| 8 |
## <[email protected]> |
| 9 |
## http://cars9.uchicago.edu/~ravel/software/ |
| 10 |
## |
| 11 |
## ------------------------------------------------------------------- |
| 12 |
## All rights reserved. This program is free software; you can |
| 13 |
## redistribute it and/or modify it under the same terms as Perl |
| 14 |
## itself. |
| 15 |
## |
| 16 |
## This program is distributed in the hope that it will be useful, |
| 17 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
## Artistic License for more details. |
| 20 |
## ------------------------------------------------------------------- |
| 21 |
## |
| 22 |
###################################################################### |
| 23 |
## Time-stamp: <2009-11-03 16:40:40 bruce> |
| 24 |
###################################################################### |
| 25 |
## Code: |
| 26 |
|
| 27 |
=head1 NAME |
| 28 |
|
| 29 |
Xray::Atoms - Utilities and data structures for the Atoms program |
| 30 |
|
| 31 |
=head1 SYNOPSIS |
| 32 |
|
| 33 |
use Xray::Xtal; |
| 34 |
use Xray::Atoms qw(parse_input keyword_defaults parse_atp |
| 35 |
absorption mcmaster); |
| 36 |
|
| 37 |
=head1 DESCRIPTION |
| 38 |
|
| 39 |
This module contains the utility subroutines used by the program |
| 40 |
Atoms. It defines a number of exportable routines for performing |
| 41 |
chores specific to Atoms. It also defines an object for storing input |
| 42 |
data to the various programs which use this file. For details about |
| 43 |
the crystallographic objects used by Atoms, see L<Xray::Xtal>. |
| 44 |
|
| 45 |
There are several versions of Atoms with different user interfaces all |
| 46 |
of which use routines from this module. These include the command |
| 47 |
line, Tk, and CGI versions. |
| 48 |
|
| 49 |
=cut |
| 50 |
; |
| 51 |
|
| 52 |
package Xray::Atoms; |
| 53 |
|
| 54 |
use strict; |
| 55 |
use vars qw($VERSION $atoms_dir $languages @available_languages |
| 56 |
$cvs_info $module_version $messages @ISA @EXPORT @EXPORT_OK); |
| 57 |
|
| 58 |
require Exporter; |
| 59 |
|
| 60 |
@ISA = qw(Exporter AutoLoader); |
| 61 |
# Items to export into callers namespace by default. Note: do not export |
| 62 |
# names by default without a very good reason. Use EXPORT_OK instead. |
| 63 |
# Do not simply export all your public functions/methods/constants. |
| 64 |
@EXPORT_OK = qw(build_cluster number rcdirectory rcfile_name |
| 65 |
absorption mcmaster i_zero self); |
| 66 |
|
| 67 |
$VERSION = '3.0beta10'; |
| 68 |
$cvs_info = '$Id: Atoms.pm,v 1.40 2001/12/21 02:28:44 bruce Exp $ '; |
| 69 |
$module_version = 1.41; #(split(' ', $cvs_info))[2]; |
| 70 |
|
| 71 |
use Carp; |
| 72 |
##use Safe; |
| 73 |
use Xray::Xtal; |
| 74 |
use Xray::Absorption; |
| 75 |
use Xray::FluorescenceEXAFS; |
| 76 |
use Chemistry::Elements qw(get_name get_Z get_symbol); |
| 77 |
use Statistics::Descriptive; |
| 78 |
use Text::Abbrev; |
| 79 |
use File::Basename; |
| 80 |
use Ifeffit::FindFile; |
| 81 |
my $STAR_Parser_exists = (eval "require STAR::Parser"); |
| 82 |
if ($STAR_Parser_exists) { |
| 83 |
import STAR::Parser; |
| 84 |
require STAR::DataBlock; |
| 85 |
import STAR::DataBlock; |
| 86 |
}; |
| 87 |
|
| 88 |
#use constant EV2RYD => 13.605698; |
| 89 |
use constant EPSILON => 0.00001; |
| 90 |
|
| 91 |
my $elem_match = '([BCFHIKNOPSUVWY]|A[cglrstu]|B[aeir]|C[adelorsu]|Dy|E[ru]|F[er]|G[ade]|H[efgo]|I[nr]|Kr|L[aiu]|M[gno]|N[abdeip]|Os|P[abdmortu]|R[abehnu]|S[bceimnr]|T[abcehilm]|Xe|Yb|Z[nr])'; |
| 92 |
|
| 93 |
## location of atp files |
| 94 |
use File::Spec; |
| 95 |
use vars qw($atp_dir $lib_dir $is_windows); |
| 96 |
$is_windows = $Ifeffit::FindFile::is_windows; |
| 97 |
$atoms_dir = Xray::Xtal::identify_self(); |
| 98 |
$atp_dir = ($is_windows) ? |
| 99 |
Ifeffit::FindFile->find("atoms", "atp_sys") : |
| 100 |
File::Spec->catfile($atoms_dir, "atp"); |
| 101 |
$lib_dir = ($is_windows) ? |
| 102 |
Ifeffit::FindFile->find("atoms", "xray_lib") : |
| 103 |
File::Spec->catfile($atoms_dir, "lib"); |
| 104 |
|
| 105 |
## my $languagerc = (($^O eq 'MSWin32') or ($^O eq 'cygwin')) ? |
| 106 |
## File::Spec->catfile($lib_dir, "languages"); |
| 107 |
## eval "do '$languagerc'" or warn "Language rc file not found. Using English.$/"; |
| 108 |
|
| 109 |
|
| 110 |
use vars qw(%meta %colors %fonts); |
| 111 |
## use vars qw($always_write_feff $atoms_language $write_to_pwd |
| 112 |
## $prefer_feff_eight $absorption_tables $dafs_default |
| 113 |
## $plotting_hook $default_filepath $unused_modifier |
| 114 |
## $display_balloons $no_crystal_warnings $one_frame $convolve_dafs |
| 115 |
## $never_ask_to_save $ADB_location); |
| 116 |
%meta = (ADB_location => "http://cars9.uchicago.edu/atomsdb/", |
| 117 |
absorption_tables => 'elam', |
| 118 |
always_write_feff => 0, |
| 119 |
atoms_language => 'english', |
| 120 |
convolve_dafs => 1, |
| 121 |
dafs_default => 'cl', |
| 122 |
default_filepath => '', |
| 123 |
display_balloons => 1, |
| 124 |
never_ask_to_save => 0, |
| 125 |
no_crystal_warnings => 0, |
| 126 |
one_frame => 1, |
| 127 |
plotting_hook => '', |
| 128 |
prefer_feff_eight => 0, |
| 129 |
unused_modifier => 'Shift', |
| 130 |
write_to_pwd => 1, |
| 131 |
); |
| 132 |
|
| 133 |
## keep these refs to anon hashes so as not to break rcfiles from |
| 134 |
## alpha16 and earlier |
| 135 |
## use vars qw(); # unused variables from atomsrc |
| 136 |
## ## must declare variables for colors and fonts for TkAtoms |
| 137 |
## use vars qw($c_foreground $c_background $c_trough $c_entry $c_label |
| 138 |
## $c_balloon $c_button $c_buttonActive $c_sgbActive $c_sgbGroup |
| 139 |
## $c_done $c_todo $c_plot |
| 140 |
## $f_balloon $f_label $f_menu $f_button $f_header $f_entry $f_sgb); |
| 141 |
|
| 142 |
use vars qw($rcfile); |
| 143 |
$rcfile = (($^O eq 'MSWin32') or ($^O eq 'cygwin')) ? |
| 144 |
File::Spec->catfile($lib_dir, "tkatoms.ini"): |
| 145 |
File::Spec->catfile($lib_dir, "atomsrc"); |
| 146 |
(-e $rcfile) and &read_rc($rcfile); |
| 147 |
my $users_rc = &rcfile_name; |
| 148 |
(-e $users_rc) and &read_rc($users_rc); |
| 149 |
$meta{atoms_language} = lc($meta{atoms_language}); |
| 150 |
$meta{absorption_tables} = lc($meta{absorption_tables}); |
| 151 |
Xray::Absorption -> load($meta{absorption_tables}); |
| 152 |
#Xray::Absorption -> load('Elam'); |
| 153 |
$meta{unused_modifier} = ucfirst($meta{unused_modifier}); |
| 154 |
($meta{unused_modifier} =~ /Alt|Control|Meta|Shift/) or |
| 155 |
$meta{unused_modifier} = 'Shift'; |
| 156 |
|
| 157 |
my $language_file; |
| 158 |
if (not $languages) { |
| 159 |
$language_file = (($^O eq 'MSWin32') or ($^O eq 'cygwin')) ? |
| 160 |
File::Spec->catfile($lib_dir, "atomsrc.en"): |
| 161 |
File::Spec->catfile($lib_dir, "atomsrc.en"); |
| 162 |
} else { |
| 163 |
$language_file = "atomsrc." . $$languages{$meta{atoms_language}}; |
| 164 |
$language_file = (($^O eq 'MSWin32') or ($^O eq 'cygwin')) ? |
| 165 |
File::Spec->catfile($lib_dir, $language_file): |
| 166 |
File::Spec->catfile($lib_dir, $language_file); |
| 167 |
unless (-e $language_file) { |
| 168 |
$language_file = (($^O eq 'MSWin32') or ($^O eq 'cygwin')) ? |
| 169 |
File::Spec->catfile($lib_dir, "atomsrc.en"): |
| 170 |
File::Spec->catfile($lib_dir, "atomsrc.en"); |
| 171 |
}; |
| 172 |
}; |
| 173 |
eval "do '$language_file'"; # read strings |
| 174 |
($meta{atoms_language} eq 'english') or |
| 175 |
Xray::Xtal::set_language($meta{atoms_language}); |
| 176 |
|
| 177 |
my $overfull_margin = 0.1; |
| 178 |
|
| 179 |
# Preloaded methods go here. |
| 180 |
|
| 181 |
=head1 THE KEYWORDS OBJECT |
| 182 |
|
| 183 |
To simplify the handling of input data, this module provides an |
| 184 |
object. It is used like this: |
| 185 |
|
| 186 |
my $keywords = Xray::Atoms -> new(); |
| 187 |
$keywords -> make('rmax' => 5.7) |
| 188 |
|
| 189 |
The make method is the general way of storing data in the keywords |
| 190 |
object. In general, this method assumes that the keyword takes a |
| 191 |
scalar value. The data structure is just a hash, so the keyword can |
| 192 |
be anything. Indeed, keyword recognition is not done in this module. |
| 193 |
Keyword/value pairs are just blindly stored for later processing. |
| 194 |
Several keywords are treated specially because they are commonly used |
| 195 |
in the programs included in the Atoms package. These keywords |
| 196 |
|
| 197 |
a b c alpha beta gamma argon krypton nitrogen rmax |
| 198 |
|
| 199 |
are treated as numbers. This means that they are eval-ed (and so can |
| 200 |
be short expressions like 1/2 or 3.5+0.001) and return 0 if they |
| 201 |
cannot be interpreted as numbers. This evaluation is done in a |
| 202 |
taint-safe manner. |
| 203 |
|
| 204 |
A couple of keywords are treated specially. |
| 205 |
|
| 206 |
$keywords -> make(title=>'This is a title!'); |
| 207 |
|
| 208 |
pushes the given string onto an anonymous array of strings. In this |
| 209 |
way, any number of title lines can be kept in the object. |
| 210 |
|
| 211 |
The C<shift> keyword is also handled specially. This takes three |
| 212 |
arguments, the three components of the shift vector. A syntax like |
| 213 |
this works fine: |
| 214 |
|
| 215 |
$keywords -> make('shift' => $x, $y, $z); |
| 216 |
|
| 217 |
This stores the three values of the shift vector as an anonymous |
| 218 |
array. |
| 219 |
|
| 220 |
|
| 221 |
=cut |
| 222 |
|
| 223 |
|
| 224 |
sub new { |
| 225 |
my $classname = shift; |
| 226 |
my $self = {}; |
| 227 |
# fuctional keywords |
| 228 |
$self->{'title'} = []; |
| 229 |
$self->{'edge'} = ""; |
| 230 |
$self->{'core'} = ""; |
| 231 |
$self->{'argon'} = 0; |
| 232 |
$self->{'krypton'} = 0; |
| 233 |
$self->{'nitrogen'} = 0; |
| 234 |
$self->{'rmax'} = 0; |
| 235 |
$self->{'shift'} = [0,0,0]; |
| 236 |
# lattice parameters |
| 237 |
$self->{'space'} = ''; |
| 238 |
$self->{'a'} = 0; |
| 239 |
$self->{'b'} = 0; |
| 240 |
$self->{'c'} = 0; |
| 241 |
$self->{'alpha'} = 0; |
| 242 |
$self->{'beta'} = 0; |
| 243 |
$self->{'gamma'} = 0; |
| 244 |
# sites |
| 245 |
$self->{'sites'} = []; |
| 246 |
# operational keywords |
| 247 |
$self->{'overfull_margin'} = 0.1; |
| 248 |
$self->{'found_output'} = 0; |
| 249 |
$self->{'quiet'} = 0; |
| 250 |
$self->{'program'} = 'atoms'; |
| 251 |
# atp flags |
| 252 |
my @atpfiles = (); |
| 253 |
my @atp_dir_list = (); |
| 254 |
push @atp_dir_list, $Xray::Atoms::atp_dir; |
| 255 |
push @atp_dir_list, File::Spec->catfile(&rcdirectory, "atp"); |
| 256 |
foreach my $dir (@atp_dir_list) { |
| 257 |
if (-d $dir) { |
| 258 |
opendir (ATPDIR, $dir) || |
| 259 |
die $$messages{'no_directory'} . " " . $dir . $/; |
| 260 |
push @atpfiles, grep /\.atp/, readdir ATPDIR; |
| 261 |
closedir ATPDIR; |
| 262 |
}; |
| 263 |
}; ## feff feff8 p1 unit alchemy xyz geom symmetry test |
| 264 |
$self->{'files'} = {}; |
| 265 |
# feff potentials |
| 266 |
$self->{'ipots'} = 'species'; # 'sites' |
| 267 |
$self->{'ipot_vals'} = {}; |
| 268 |
$self->{'always_feff'} = $meta{always_write_feff}; |
| 269 |
$self->{'prefer_feff_8'} = $meta{prefer_feff_eight}; |
| 270 |
$self->{'language'} = $meta{atoms_language}; |
| 271 |
$self->{'write_to_pwd'} = $meta{write_to_pwd}; |
| 272 |
|
| 273 |
bless($self, $classname); |
| 274 |
return $self; |
| 275 |
}; |
| 276 |
# dafs |
| 277 |
##"fdat" => 0, "refile" => "reflect.dat", |
| 278 |
##"qvec" => (0,0,0), "nepoints" => 100, |
| 279 |
##"feout" => "fe.dat", "egrid" => 1, |
| 280 |
##"reflections" => (0,0,0), "noanomalous" => 0 ); |
| 281 |
## corrections, index |
| 282 |
|
| 283 |
|
| 284 |
sub make { |
| 285 |
my $self = shift; |
| 286 |
unless (($#_ % 2) || (grep /\b(files|sites)\b/, @_)) { |
| 287 |
my $this = (caller(0))[3]; |
| 288 |
croak "$this " . $$messages{make_error}; |
| 289 |
return; |
| 290 |
}; |
| 291 |
|
| 292 |
my $die = $self->{die} || 1; |
| 293 |
while (@_) { |
| 294 |
my $att = lc(shift); |
| 295 |
my $value = shift; |
| 296 |
KEYWORDS: { |
| 297 |
($att eq 'title') && do { |
| 298 |
foreach my $l (split(/\n/, $value)) { |
| 299 |
push @{$self->{'title'}}, $l unless ($l =~ /^\s*$/); |
| 300 |
}; |
| 301 |
last KEYWORDS; |
| 302 |
}; |
| 303 |
($att =~ /(shift|qvec)/) && do { |
| 304 |
my $v1 = number($value, $die, $self); |
| 305 |
my $v2 = number(shift, $die, $self); |
| 306 |
my $v3 = number(shift, $die, $self); |
| 307 |
$self->{$1} = [$v1, $v2, $v3]; |
| 308 |
last KEYWORDS; |
| 309 |
}; |
| 310 |
($att =~ /^(argon|krypton|nitrogen|rmax)$/) && do { |
| 311 |
$self->{$1} = number($value, $die, $self); |
| 312 |
last KEYWORDS; |
| 313 |
}; |
| 314 |
($att =~ /^(a|b|c|alpha|beta|gamma)$/) && do { |
| 315 |
$self->{$1} = number($value, $die, $self); |
| 316 |
last KEYWORDS; |
| 317 |
}; |
| 318 |
($att eq 'atp') && do { |
| 319 |
$value = $value; |
| 320 |
$self->{atp}{$value} = 1; |
| 321 |
last KEYWORDS; |
| 322 |
}; |
| 323 |
($att eq 'files') && do { |
| 324 |
$value = lc($value); |
| 325 |
my $v2 = shift; |
| 326 |
($v2 =~ /^\s*$/) and $v2 = undef; |
| 327 |
$self->{files}{$value} = $v2; |
| 328 |
last KEYWORDS; |
| 329 |
}; |
| 330 |
($att eq 'sites') && do { |
| 331 |
my $e = $value; |
| 332 |
my $x = number(shift, $die, $self); |
| 333 |
my $y = number(shift, $die, $self); |
| 334 |
my $z = number(shift, $die, $self); |
| 335 |
my $t = shift; |
| 336 |
my $o = number(shift, $die, $self); |
| 337 |
push @{$self->{'sites'}}, [$e, $x, $y, $z, $t, $o]; |
| 338 |
last KEYWORDS; |
| 339 |
}; |
| 340 |
do { |
| 341 |
$self->{$att} = $value; |
| 342 |
last KEYWORDS; |
| 343 |
}; |
| 344 |
|
| 345 |
}; |
| 346 |
|
| 347 |
}; |
| 348 |
|
| 349 |
return $self; |
| 350 |
}; |
| 351 |
|
| 352 |
|
| 353 |
=head1 METHODS |
| 354 |
|
| 355 |
=head2 C<parse_input> |
| 356 |
|
| 357 |
This method is used by atoms to read an atoms input file. It is |
| 358 |
typically called by |
| 359 |
|
| 360 |
$keywords -> parse_input($file, 0); |
| 361 |
|
| 362 |
The first argument is the name of the input file. The input file is |
| 363 |
presumed to be of the atoms.inp sort unless the file extension is |
| 364 |
C<.cif>, in which case it is presumed to be a CIF file. This check is |
| 365 |
made case-insensitively. |
| 366 |
|
| 367 |
The second argument is tells the error trapping mechnism what kind of |
| 368 |
environment you are working in. If you have written a command-line |
| 369 |
program, it should be 0. In a Tk program, it should 1. In a CGI |
| 370 |
script it should be 2. |
| 371 |
|
| 372 |
The contents of the file are parsed and stored in a keywords object. |
| 373 |
The method is fairly clever about interpreting the file and offering |
| 374 |
warning and error messages. It interprets numbers in a fairly secure |
| 375 |
manner, thus allowing simple math expressions to be used in the atom |
| 376 |
list and as the shift vectors. |
| 377 |
|
| 378 |
Alternately, you can pass a CIF file with the syntax |
| 379 |
|
| 380 |
$keywords -> parse_input($file, 0, 'cif'); |
| 381 |
|
| 382 |
or |
| 383 |
|
| 384 |
$keywords -> parse_input($file, 0, 'cif', 2); |
| 385 |
|
| 386 |
The third argument is "cif" or "inp" to specify the input file type. |
| 387 |
The fourth argument tells the CIF parser which structure to use from a |
| 388 |
multi-structure file. |
| 389 |
|
| 390 |
=cut |
| 391 |
|
| 392 |
$$::messages{adb_unknown} = "You requested an unknown ADB file"; |
| 393 |
sub parse_input { |
| 394 |
|
| 395 |
my $href = abbrev qw(a alpha argon atoms b basis beta c core corrections |
| 396 |
edge egrid emax emin egrid estep dopants fdat feff |
| 397 |
feff8 gamma geom index ipots krypton |
| 398 |
nepoints nitrogen noanomalous out output p1 |
| 399 |
qvec refile reflection reflections rmax shift |
| 400 |
space title unit xanes); |
| 401 |
|
| 402 |
my $keys = shift; |
| 403 |
my ($file, $die, $type, $entry) = @_; |
| 404 |
$type ||= 'inp'; |
| 405 |
$type = ($type =~ /cif/i) ? "cif" : "inp"; |
| 406 |
$entry ||= 0; |
| 407 |
my ($nsites, $ntitles, $iline) = (0,0,0); |
| 408 |
|
| 409 |
## is this a CIF file? |
| 410 |
my ($nn,$pp,$suff) = fileparse($file, |
| 411 |
".inp", ".INP", ".Inp", |
| 412 |
".cif", ".CIF", ".Cif"); |
| 413 |
cif($keys, $file ,$entry), return |
| 414 |
if ($STAR_Parser_exists and ((lc($type) eq 'cif') or ($suff =~ /cif$/i))); |
| 415 |
|
| 416 |
|
| 417 |
## divy up the keywords into birds of |
| 418 |
## a feather |
| 419 |
my @normal = ("argon", "center", "core", "ipots", "krypton", |
| 420 |
"nitrogen", "rmax"); |
| 421 |
##my @logicals = (); |
| 422 |
my @threevecs = ("shift", "dafs", "qvec", "reflection"); |
| 423 |
my @cellwords = ("a", "alpha", "b", "beta", "c", "gamma"); |
| 424 |
my @output_types = ("feff", "feff8", "p1", "geom", "unit", "out"); |
| 425 |
my @dafs = ("emin", "emax", "estep", "egrid"); |
| 426 |
my @deprecated = ("fdat", "nepoints", "xanes", "modules", |
| 427 |
"message", "noanomalous", "self", "i0", "mcmaster", |
| 428 |
"dwarf", "reflections", "refile", |
| 429 |
"egrid", "index", "corrections"); |
| 430 |
my ($term, $prompt, $stdin); |
| 431 |
my $fh; |
| 432 |
if ($file =~ /^http:/) { |
| 433 |
if (eval "require LWP::Simple") { |
| 434 |
unless (LWP::Simple::head($file)) { # handle unknown ADB file |
| 435 |
$keys -> warn_or_die("$$::messages{adb_unknown}:\n\t$file.\n", $die); |
| 436 |
return; |
| 437 |
}; |
| 438 |
open ($fh, "GET $file |") |
| 439 |
or die "A pipe using LWP::Simple could not be opened to fetch ADB file.\n"; |
| 440 |
} else { |
| 441 |
die "You must install LWP::Simple to fetch ADB files.\n"; |
| 442 |
}; |
| 443 |
} elsif ($file ne '____stdin') { |
| 444 |
open ($fh, $file) || die "could not open " . $file . " for reading" . $/; |
| 445 |
}; |
| 446 |
## what about STDIN??? |
| 447 |
|
| 448 |
READ: while (<$fh>) { |
| 449 |
|
| 450 |
my @line = (); |
| 451 |
++$iline; |
| 452 |
next if /^\s*$/; # skip blanks |
| 453 |
next if /^\s*[!\#%*]/; # skip comment lines |
| 454 |
chomp; |
| 455 |
(m/<HTML>/) and do { # handle an unknown ADB file name |
| 456 |
|
| 457 |
}; |
| 458 |
(my $line = $_) =~ s/^\s+//; # trim leading blanks |
| 459 |
$line =~ s/\r//g; # strip spurious control-M characters |
| 460 |
@line = split(/\s*[ \t=,]\s*/, $line); |
| 461 |
|
| 462 |
KEYWORDS: while (@line) { |
| 463 |
my $word = lc(shift @line); |
| 464 |
next READ if ($word =~ /^[!\#%*]/); # skip comment lines |
| 465 |
my $wasword = $word; |
| 466 |
$word = $href -> {$word}; |
| 467 |
$word ||= $wasword; |
| 468 |
## unless ($word) { |
| 469 |
## my $message = "\"$wasword\": " . $$messages{'unknown_keyword'} . |
| 470 |
## ", " . $$messages{'line'} . " $..$/"; |
| 471 |
## $keys -> warn_or_die($message, $die); |
| 472 |
## next READ; |
| 473 |
## }; |
| 474 |
($word eq "end") && last READ; |
| 475 |
|
| 476 |
## title/comment |
| 477 |
|
| 478 |
($word =~ /^(com|tit)/) && do { |
| 479 |
## trim `title' or `comment' and leading spaces |
| 480 |
$line =~ s/^\s+//; |
| 481 |
my $string = substr($line, length($word)); |
| 482 |
$string =~ s/^\s+//; $string =~ s/^=//; $string =~ s/^\s+//; |
| 483 |
unless ($keys -> {"quiet"}) { |
| 484 |
print STDOUT " ", $$messages{'title'}, " > ", |
| 485 |
substr($string, 0, 59), "$/"; |
| 486 |
}; |
| 487 |
$keys -> make('title'=>$string); |
| 488 |
foreach my $a (@line) { |
| 489 |
shift @line; |
| 490 |
}; |
| 491 |
next READ; |
| 492 |
}; |
| 493 |
## edge/hole can be error-checked en |
| 494 |
## passant |
| 495 |
($word =~ /^(edg|hol)/) && do { |
| 496 |
my $value = shift @line; |
| 497 |
$keys -> make('edge'=>$value); |
| 498 |
next KEYWORDS; |
| 499 |
}; |
| 500 |
## keywords which take the next word |
| 501 |
## literally |
| 502 |
## argon center core krypton nitrogen rmax |
| 503 |
(grep(/\b$word\b/, @normal)) && do { |
| 504 |
($word eq "center") && ($word = "core"); |
| 505 |
my $value = shift @line; |
| 506 |
$keys -> make($word=>$value); |
| 507 |
next KEYWORDS; |
| 508 |
}; |
| 509 |
## DAFS keywords which take the next |
| 510 |
## word literally: emin estep emax |
| 511 |
(grep(/\b$word\b/, @dafs)) && do { |
| 512 |
($word eq "egrid") && ($word = "estep"); |
| 513 |
my $value = shift @line; |
| 514 |
$keys -> make($word=>$value); |
| 515 |
next KEYWORDS; |
| 516 |
}; |
| 517 |
## three vectors, take the next three |
| 518 |
## words literally |
| 519 |
(grep(/\b$word\b/, @threevecs)) && do { |
| 520 |
my @value; |
| 521 |
$value[0] = shift @line; |
| 522 |
$value[1] = shift @line; |
| 523 |
$value[2] = shift @line; |
| 524 |
($word eq "dafs") && ($word = "qvec"); |
| 525 |
($word eq "reflection") && ($word = "qvec"); |
| 526 |
|
| 527 |
## ----- evaluate the coordinates safely |
| 528 |
## see `perldoc Safe' for details |
| 529 |
## my $message = $$messages{'tainted_vector'} . $/; |
| 530 |
## $message .= $$messages{'file'} . ": $file, "; |
| 531 |
## $message .= $$messages{'line'} . ": $.$/"; |
| 532 |
## my $cpt = new Safe; |
| 533 |
## ##$cpt->share('@value'); |
| 534 |
## $ {$cpt->varglob('x')} = $cpt->reval($value[0]); |
| 535 |
## $ {$cpt->varglob('y')} = $cpt->reval($value[1]); |
| 536 |
## $ {$cpt->varglob('z')} = $cpt->reval($value[2]); |
| 537 |
## @value = ($ {$cpt->varglob('x')}, $ {$cpt->varglob('y')}, |
| 538 |
## $ {$cpt->varglob('z')} ); |
| 539 |
## test_safe_return($message, @value); |
| 540 |
|
| 541 |
## this is a much weaker form of security, but it works with |
| 542 |
## PerlApp |
| 543 |
@value = map {number($_,$die,$keys)} @value; |
| 544 |
## ----- end of safe evaluation |
| 545 |
|
| 546 |
$keys -> make($word=>$value[0],$value[1],$value[2]); |
| 547 |
next KEYWORDS; |
| 548 |
}; |
| 549 |
## logical flags and diagnostics take |
| 550 |
## boolean values, on if next word is |
| 551 |
## yes, true, or on |
| 552 |
##(grep(/\b$word\b/, @logicals)) && do { |
| 553 |
## my $value = shift @line; |
| 554 |
## $keywords{$word} = ($value =~ /^(t|y|on)/) ? 1 : 0; |
| 555 |
## next KEYWORDS; |
| 556 |
##}; |
| 557 |
## output styles take filenames |
| 558 |
($word =~ /^outp/) && do { |
| 559 |
my $type = shift @line; |
| 560 |
my $value = shift @line; |
| 561 |
#push @$outputs, $type; |
| 562 |
if ($keys->{'program'} eq 'dafs') { |
| 563 |
undef $keys->{atp}; undef $keys->{files}; |
| 564 |
}; |
| 565 |
$keys -> make('atp' => $type); |
| 566 |
$keys -> make('files' => $type, $value); |
| 567 |
$keys -> make("found_output" => 1); |
| 568 |
next KEYWORDS; |
| 569 |
}; |
| 570 |
## backwards compatability |
| 571 |
(grep(/\b$word\b/, @output_types)) && do { |
| 572 |
my $value = shift @line; |
| 573 |
if ($word eq "out") { |
| 574 |
$keys -> make('atp'=>'feff', 'feff'=>$value); |
| 575 |
#push @$outputs, "feff"; |
| 576 |
} else { |
| 577 |
my $set = ($value =~ /^(t|y|on)/); |
| 578 |
if ($set) { |
| 579 |
$keys -> make('atp'=>$word); |
| 580 |
## ($word eq "feff") && ($keys -> make('feff' => "feff.inp")); |
| 581 |
## ($word eq "feff8") && ($keys -> make('feff8'=> "feff.inp")); |
| 582 |
## ($word eq "geom") && ($keys -> make('geom' => "geom.dat")); |
| 583 |
## ($word eq "p1") && ($keys -> make('p1' => "p1.inp" )); |
| 584 |
## ($word eq "unit") && ($keys -> make('unit' => "unit.dat")); |
| 585 |
#push @$outputs, $word; |
| 586 |
}; |
| 587 |
}; |
| 588 |
next KEYWORDS; |
| 589 |
}; |
| 590 |
## deprecated keywords |
| 591 |
(grep(/\b$word\b/, @deprecated)) && do { |
| 592 |
shift @line; |
| 593 |
my $mess = "\"$word\": " . $$messages{'deprecated'} . ", " . |
| 594 |
$$messages{'line'} . " $..$/"; |
| 595 |
warn $mess; |
| 596 |
($word =~ /dafs|reflections|qvec/) && do { |
| 597 |
shift @line; shift @line; |
| 598 |
}; |
| 599 |
next KEYWORDS; |
| 600 |
}; |
| 601 |
## space group symbol |
| 602 |
($word =~ /^spa/) && do { |
| 603 |
my $lline = lc($line); |
| 604 |
my $space = substr($line, index($lline,"space")+6); |
| 605 |
$space =~ s/^[\s=,]+//; |
| 606 |
$space = substr($space, 0, 10); # next 10 characters |
| 607 |
$space =~ s/[!\#%*].*$//; # trim off comments |
| 608 |
$keys -> make('space'=>$space); |
| 609 |
foreach my $x (split " ", $space) { |
| 610 |
shift @line; |
| 611 |
}; |
| 612 |
next KEYWORDS; |
| 613 |
}; |
| 614 |
## a,b,c,alpha,beta,gamma |
| 615 |
(grep(/\b$word\b/, @cellwords)) && do { |
| 616 |
my $value = shift @line; |
| 617 |
$value = sprintf "%11.7f", $value; |
| 618 |
my $attribute = lc $word; |
| 619 |
$keys -> make($attribute=>$value); |
| 620 |
## the angle attribute carries the most recently set angle |
| 621 |
## this is needed to correctly interpret monoclinic cells |
| 622 |
## with all angles equal to 90 (pathology!!) |
| 623 |
##($attribute =~ /(alpha|beta|gamma)/i) && |
| 624 |
## ($cell -> make( "Angle"=>$1 ) ); |
| 625 |
next KEYWORDS; |
| 626 |
}; |
| 627 |
## site methods |
| 628 |
($word =~ /^dop/) && do { |
| 629 |
my $mess = $$messages{'dopants'} . ", " . $$messages{'line'} . " $..$/"; |
| 630 |
$keys -> warn_or_die($mess, $die); |
| 631 |
shift @line; shift @line; shift @line; |
| 632 |
next KEYWORDS; |
| 633 |
}; |
| 634 |
($word =~ /^bas/) && do { |
| 635 |
croak $$messages{'basis'}, $/; |
| 636 |
next KEYWORDS; |
| 637 |
}; |
| 638 |
($word =~ /^ato/) && do { |
| 639 |
my $nip = 0; |
| 640 |
ATOMS: while (1) { |
| 641 |
$_ = <$fh>; |
| 642 |
(! $_) && last KEYWORDS; |
| 643 |
++$iline; |
| 644 |
last KEYWORDS if /---/; |
| 645 |
next ATOMS if /^\s*$/; # skip blanks |
| 646 |
next ATOMS if /^\s*[!\#%*]/; # skip comment lines |
| 647 |
chomp; |
| 648 |
my @line = split; |
| 649 |
my $save = $line[0]; |
| 650 |
$line[0] = get_symbol($line[0]); |
| 651 |
unless ($line[0]) { |
| 652 |
if ($die == 0) { |
| 653 |
my $mess = join(" ", $save, $$messages{not_an_element}, |
| 654 |
$$messages{at_line}, $iline, $/); |
| 655 |
die $mess; |
| 656 |
} else { |
| 657 |
$line[0] = "??"; |
| 658 |
##my $mess = join(" ", $save, "is not an element symbol", $/); |
| 659 |
##$keys -> warn_or_die($mess, $die); |
| 660 |
}; |
| 661 |
}; |
| 662 |
## ----- evaluate the coordinates safely |
| 663 |
## see `perldoc Safe' for details |
| 664 |
|
| 665 |
## this is a much weaker form of security, but it works with |
| 666 |
## PerlApp |
| 667 |
my @xyz = map {number($_, $die, $keys)} ($line[1], $line[2], $line[3]); |
| 668 |
|
| 669 |
## my $message = $$messages{'tainted_atoms'} . $/; |
| 670 |
## $message .= $$messages{'file'} . ": $file, "; |
| 671 |
## $message .= $$messages{'line'} . ": $.$/"; |
| 672 |
## my $cpt = new Safe; |
| 673 |
## ##$cpt->share('@xyz'); |
| 674 |
## $ {$cpt->varglob('x')} = $cpt->reval($xyz[0]); |
| 675 |
## $ {$cpt->varglob('y')} = $cpt->reval($xyz[1]); |
| 676 |
## $ {$cpt->varglob('z')} = $cpt->reval($xyz[2]); |
| 677 |
## @xyz = ($ {$cpt->varglob('x')}, $ {$cpt->varglob('y')}, |
| 678 |
## $ {$cpt->varglob('z')} ); |
| 679 |
## test_safe_return($message, @xyz); |
| 680 |
# @xyz = map {sprintf "%11.7f", $_} @xyz; |
| 681 |
## ----- end of safe evaluation |
| 682 |
|
| 683 |
if (defined $line[4]) { |
| 684 |
## if the fourth column matches a C float, assume it is an |
| 685 |
## occupancy and that the tag should be "" |
| 686 |
if ($line[4] =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) { |
| 687 |
$line[5] = $line[4]; |
| 688 |
$line[4] = ""; |
| 689 |
}; |
| 690 |
} else { |
| 691 |
($line[4] = ""); |
| 692 |
}; |
| 693 |
## test fifth column to see if it is an occupancy |
| 694 |
(defined $line[5]) || ($line[5] = 1); |
| 695 |
if ($line[5] =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) { |
| 696 |
$line[5] = number($line[5], $die, $keys); |
| 697 |
($line[5] =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/) |
| 698 |
|| ($line[5] = 1); |
| 699 |
} else { |
| 700 |
$line[5] = 1; |
| 701 |
}; |
| 702 |
$keys -> make('sites'=> |
| 703 |
$line[0], $xyz[0], $xyz[1], $xyz[2], $line[4], $line[5]); |
| 704 |
++$nsites; |
| 705 |
my $this = lc($line[0]); |
| 706 |
}; |
| 707 |
}; |
| 708 |
## unknown keyword, store it anyway |
| 709 |
($word) && do { |
| 710 |
my $value = shift @line; |
| 711 |
$keys -> make($word=>$value); |
| 712 |
## my $message = "\"$word\": " . $$messages{'unknown_keyword'} . |
| 713 |
## ", " . $$messages{'line'} . "$..$/"; |
| 714 |
## warn $message; |
| 715 |
## shift @line; |
| 716 |
next KEYWORDS; |
| 717 |
}; |
| 718 |
}; |
| 719 |
}; |
| 720 |
close $fh; |
| 721 |
}; |
| 722 |
|
| 723 |
|
| 724 |
=head2 C<cif> |
| 725 |
|
| 726 |
This method reads from a CIF file and loads the data into keywords |
| 727 |
object. |
| 728 |
|
| 729 |
=cut |
| 730 |
|
| 731 |
|
| 732 |
## this is ungainly due to the mismatch in nomenclature and verbosity |
| 733 |
## of my stuff and the STAR stuff. |
| 734 |
sub cif { |
| 735 |
my ($keys, $file, $which) = @_; |
| 736 |
my @datablocks = STAR::Parser->parse($file); |
| 737 |
my $datablock = $datablocks[$which]; |
| 738 |
## if STAR::Checker->check(-datablock=>$datablocks[0]); |
| 739 |
|
| 740 |
my @item; |
| 741 |
|
| 742 |
## titles: consider various common title-like entries, strip white |
| 743 |
## space characters and stuff them into the title attribute |
| 744 |
@item = $datablock->get_item_data(-item=>"_chemical_name_mineral"); |
| 745 |
$item[0] ||= ""; $item[0] =~ s/
//g; chomp $item[0]; |
| 746 |
$keys->make(title=>$item[0]) unless ($item[0] =~ /^\s*$/); |
| 747 |
@item = $datablock->get_item_data(-item=>"_chemical_name_systematic"); |
| 748 |
$item[0] ||= ""; $item[0] =~ s/
//g; chomp $item[0]; |
| 749 |
$keys->make(title=>$item[0]) unless ($item[0] =~ /^\s*$/); |
| 750 |
@item = $datablock->get_item_data(-item=>"_chemical_formula_structural"); |
| 751 |
$item[0] ||= ""; $item[0] =~ s/
//g; chomp $item[0]; |
| 752 |
$keys->make(title=>$item[0]) unless ($item[0] =~ /^\s*$/); |
| 753 |
@item = $datablock->get_item_data(-item=>"_chemical_formula_sum"); |
| 754 |
$item[0] ||= ""; $item[0] =~ s/
//g; chomp $item[0]; |
| 755 |
$keys->make(title=>$item[0]) unless ($item[0] =~ /^\s*$/); |
| 756 |
@item = $datablock->get_item_data(-item=>"_publ_author_name"); |
| 757 |
$item[0] ||= ""; $item[0] =~ s/
//g; chomp $item[0]; |
| 758 |
$keys->make(title=>$item[0]) unless ($item[0] =~ /^\s*$/); |
| 759 |
@item = $datablock->get_item_data(-item=>"_citation_journal_abbrev"); |
| 760 |
$item[0] ||= ""; $item[0] =~ s/
//g; chomp $item[0]; |
| 761 |
$keys->make(title=>$item[0]) unless ($item[0] =~ /^\s*$/); |
| 762 |
@item = $datablock->get_item_data(-item=>"_publ_section_title"); |
| 763 |
$item[0] ||= ""; $item[0] =~ s/
//g; chomp $item[0]; |
| 764 |
$keys->make(title=>$item[0]) unless ($item[0] =~ /^\s*$/); |
| 765 |
|
| 766 |
## space group: try the number then the NM symbol and canonicalize it |
| 767 |
@item = $datablock->get_item_data(-item=>"_symmetry_Int_Tables_number"); |
| 768 |
my @sg = Xray::Xtal::Cell::canonicalize_symbol($item[0]); |
| 769 |
unless ($sg[0]) { |
| 770 |
@item = $datablock->get_item_data(-item=>"_symmetry_space_group_name_H-M"); |
| 771 |
@sg = Xray::Xtal::Cell::canonicalize_symbol($item[0]); |
| 772 |
}; |
| 773 |
$keys->make(space=>$sg[0]) if $sg[0]; |
| 774 |
|
| 775 |
## lattic parameters |
| 776 |
my $min = 100000; # use lattice constants to compute default for Rmax |
| 777 |
foreach my $k (qw(a b c)) { |
| 778 |
@item = $datablock->get_item_data(-item=>"_cell_length_$k"); |
| 779 |
(my $this = $item[0]) =~ s/\(\d+\)//; |
| 780 |
#print "$k $this\n"; |
| 781 |
$keys->make($k=>$this); |
| 782 |
$min = 1.1*$this if ($min > 1.1*$this); |
| 783 |
}; |
| 784 |
$min = 7 if ($min > 11); |
| 785 |
$keys->make(rmax=>$min); |
| 786 |
foreach my $k (qw(alpha beta gamma)) { |
| 787 |
@item = $datablock->get_item_data(-item=>"_cell_angle_$k"); |
| 788 |
(my $this = $item[0]) =~ s/\(\d+\)//; |
| 789 |
#print "$k $this\n"; |
| 790 |
$keys->make($k=>$this); |
| 791 |
}; |
| 792 |
|
| 793 |
## load up and clean up the atom positions |
| 794 |
my @tag = $datablock->get_item_data(-item=>"_atom_site_label"); |
| 795 |
my @el = $datablock->get_item_data(-item=>"_atom_site_type_symbol"); |
| 796 |
my @x = $datablock->get_item_data(-item=>"_atom_site_fract_x"); |
| 797 |
my @y = $datablock->get_item_data(-item=>"_atom_site_fract_y"); |
| 798 |
my @z = $datablock->get_item_data(-item=>"_atom_site_fract_z"); |
| 799 |
my @occ = $datablock->get_item_data(-item=>"_atom_site_occupancy"); |
| 800 |
foreach my $i (0 .. $#tag) { |
| 801 |
my $ee = $el[$i] || $tag[$i]; |
| 802 |
$ee = get_elem($ee); |
| 803 |
(my $xx = $x[$i]) =~ s/\(\d+\)//; # remove parenthesized error bars |
| 804 |
(my $yy = $y[$i]) =~ s/\(\d+\)//; |
| 805 |
(my $zz = $z[$i]) =~ s/\(\d+\)//; |
| 806 |
(my $oo = $occ[$i]||1) =~ s/\(\d+\)//; |
| 807 |
##print "$ee, $xx, $yy, $zz, $tag[$i], $oo\n"; |
| 808 |
$keys -> make('sites'=> $ee, $xx, $yy, $zz, $tag[$i], $oo); |
| 809 |
}; |
| 810 |
}; |
| 811 |
|
| 812 |
## Tags in the cif file seem to mostly be the two letter element |
| 813 |
## symbol concatinated with a number (possibly concatinated with a |
| 814 |
## non-alphanumeric, such as a plus sign or parens). Also "Wat" and "OH" |
| 815 |
## indicate oxygen sites (with one or two associated hydrogens) and D |
| 816 |
## means deuterium. |
| 817 |
sub get_elem { |
| 818 |
my $elem = $_[0]; |
| 819 |
($elem =~ /Wat/) and return "O"; |
| 820 |
($elem =~ /OH/) and return "O"; |
| 821 |
($elem =~ /^D$/) and return "H"; |
| 822 |
## snip off the last character until an element symbol is found |
| 823 |
while ($elem) { |
| 824 |
return $elem if ($elem =~ /^$elem_match$/o); |
| 825 |
chop $elem; |
| 826 |
}; |
| 827 |
return "??"; |
| 828 |
}; |
| 829 |
|
| 830 |
|
| 831 |
sub test_safe_return { |
| 832 |
my $message = shift(@_); |
| 833 |
my @list = @_; |
| 834 |
foreach my $item (@list) { |
| 835 |
(defined $item) || die $message . $/; |
| 836 |
}; |
| 837 |
return 1; |
| 838 |
}; |
| 839 |
|
| 840 |
=head2 C<verify_keywords> |
| 841 |
|
| 842 |
This method is used to perform some sanity checks on the keyword |
| 843 |
values after reading in the input data and populating the sites and the |
| 844 |
cell. It is a good idea to call this before building a cluster or |
| 845 |
starting some other calculation. |
| 846 |
|
| 847 |
$keywords -> verify_keywords($cell, \@sites, 0, 0); |
| 848 |
|
| 849 |
The first two arguments are a Cell object and a reference to a |
| 850 |
list of Site objects. The third argument is the same as the second |
| 851 |
argument to the C<parse_input> method. The last argument should be |
| 852 |
non-zero only for a calculation that does not need to know which is |
| 853 |
the central atom (e.g. a powder diffraction simulation). |
| 854 |
|
| 855 |
=cut |
| 856 |
|
| 857 |
sub verify_keywords { |
| 858 |
my $keys = shift; |
| 859 |
my ($cell,$r_sites,$die,$no_core) = @_; |
| 860 |
my $def = 7; |
| 861 |
$keys ->{dopant_core} = ""; |
| 862 |
$keys -> set_rmax($cell, $def, $die); |
| 863 |
unless ($no_core) { |
| 864 |
my $checkit = $keys -> set_core($r_sites, $die); |
| 865 |
return 1 if ($checkit eq '-1'); |
| 866 |
#$keys -> check_core($r_sites, $die); |
| 867 |
$keys -> set_edge($cell,$die); |
| 868 |
}; |
| 869 |
foreach my $word (qw/argon krypton nitrogen/) { |
| 870 |
if (($keys->{$word} < 0) || ($keys->{$word} > 1)) { |
| 871 |
$keys->make($word=>'0'); |
| 872 |
my $message = "\"$word\": $$messages{'one_gas'}$/"; |
| 873 |
$keys -> warn_or_die($message, $die); |
| 874 |
}; |
| 875 |
}; |
| 876 |
if (($keys->{nitrogen} + $keys->{argon} + $keys->{krypton}) > 1) { |
| 877 |
my $message = $$messages{'all_gases'} . $/; |
| 878 |
$keys->make('nitrogen'=> '0'); |
| 879 |
$keys->make('argon'=> '0'); |
| 880 |
$keys->make('krypton'=> '0'); |
| 881 |
$keys -> warn_or_die($message, $die); |
| 882 |
return 1; |
| 883 |
}; |
| 884 |
if ($keys->{dopant_core}) { |
| 885 |
my $z = &get_Z($keys->{dopant_core}); |
| 886 |
unless ($z) { |
| 887 |
my $message = $$::messages{dopant_core} . $/; |
| 888 |
$keys -> warn_or_die($message, $die); |
| 889 |
return 1; |
| 890 |
}; |
| 891 |
}; |
| 892 |
}; |
| 893 |
# perhaps take as arguments refs to subroutines for further tests |
| 894 |
|
| 895 |
## the next several subroutines are used internally, thus not pod |
| 896 |
## docmented. |
| 897 |
|
| 898 |
## reference to a cell and default rmax value (defaults to 7) |
| 899 |
sub set_rmax { |
| 900 |
my $keys = shift; |
| 901 |
my ($cell, $def, $die) = @_; |
| 902 |
my $message = $$messages{'rmax'} . $/; |
| 903 |
$def ||= 7; |
| 904 |
my $rm = number($keys->{'rmax'}, $die, $keys); |
| 905 |
if (($rm != 0) && ($rm < EPSILON)) { |
| 906 |
$rm = 0; |
| 907 |
$keys -> warn_or_die($message, $die); |
| 908 |
}; |
| 909 |
($rm > EPSILON) || do { |
| 910 |
## rmax is the larger of (7 and 1.1 times the shortest axis) |
| 911 |
my @list = sort ($cell->attributes('A','B','C')); |
| 912 |
@list = sort ($list[0], $def/1.1); |
| 913 |
$keys->{"rmax"} = 1.1*$list[1]; |
| 914 |
}; |
| 915 |
return $keys->{"rmax"}; |
| 916 |
}; |
| 917 |
|
| 918 |
## reference to an array of sites |
| 919 |
sub set_core { |
| 920 |
my $keys = shift; |
| 921 |
my $sites = $_[0]; |
| 922 |
my $die = $_[1]; |
| 923 |
#unless ($keys->{"core"}) { |
| 924 |
if ($#{$sites} == 0) { |
| 925 |
my ($t) = $$sites[0] -> attributes('Tag'); |
| 926 |
$keys->make("core", $t); |
| 927 |
return $keys->{"core"}; |
| 928 |
} elsif (($#{$sites} != 0) and ($keys->{"core"} =~ /^\s*$/)) { |
| 929 |
$keys -> warn_or_die($$messages{'no_core'} . $/, $die); |
| 930 |
my ($t) = $$sites[0] -> attributes('Tag'); |
| 931 |
$keys->make("core", $t); |
| 932 |
return $keys->{"core"}; |
| 933 |
} elsif ($keys -> check_core($sites, $die)) { |
| 934 |
return $keys->{"core"}; |
| 935 |
} else { |
| 936 |
$keys -> warn_or_die($$messages{'no_core'} . $/, $die); |
| 937 |
}; |
| 938 |
#return $keys->{"core"}; |
| 939 |
#}; |
| 940 |
}; |
| 941 |
|
| 942 |
sub check_core { |
| 943 |
my $keys = shift; |
| 944 |
my ($sites, $die) = @_; |
| 945 |
my $c = lc($keys->{'core'}); |
| 946 |
my $found = 0; |
| 947 |
foreach my $site (@$sites) { |
| 948 |
my ($t) = $site -> attributes('Tag'); |
| 949 |
$t = lc($t); |
| 950 |
$found = 1, last if ($t eq $c); |
| 951 |
}; |
| 952 |
$| = 1; |
| 953 |
($found) || |
| 954 |
$keys -> warn_or_die("\"" . $keys->{'core'} . "\" " . |
| 955 |
$$messages{'unknown_core'} . $/, |
| 956 |
$die); |
| 957 |
(not $found) and ($die == 0) and die "\n"; |
| 958 |
#$::help{'check_core'}; |
| 959 |
return $found; |
| 960 |
}; |
| 961 |
|
| 962 |
sub set_edge { |
| 963 |
my $keys = shift; |
| 964 |
my ($cell,$die) = @_; |
| 965 |
if ($cell->{Contents}) { |
| 966 |
unless ($keys->{'edge'}) { # default based on Z number |
| 967 |
my ($central, $xcenter, $ycenter, $zcenter) = |
| 968 |
$cell -> central($keys->{"core"}); |
| 969 |
my $z = &get_Z($central); |
| 970 |
$keys->{'edge'} = 'K'; |
| 971 |
if ($z > 57) { |
| 972 |
$keys->{'edge'} = 'L3'; |
| 973 |
}; |
| 974 |
|
| 975 |
}; |
| 976 |
($keys->{"edge"} =~ /k|l[123]|m[1-5]|n[1-7]|o[1-7]|p[1-3]/i) || do { |
| 977 |
my $message = $$messages{'bad_edge'} . $/; |
| 978 |
($keys->{"edge"} = 'k'); |
| 979 |
$keys -> warn_or_die($message, $die); |
| 980 |
return 0; |
| 981 |
}; |
| 982 |
}; |
| 983 |
return $keys->{'edge'}; |
| 984 |
}; |
| 985 |
|
| 986 |
|
| 987 |
sub atp_selected { |
| 988 |
my $keys = shift; |
| 989 |
my $atp = $_[0]; |
| 990 |
return $keys->{'atp'}{$atp}; |
| 991 |
}; |
| 992 |
sub toggle_atp { |
| 993 |
my $keys = shift; |
| 994 |
my $atp = $_[0]; |
| 995 |
if ($keys->{'atp'}{$atp}) { |
| 996 |
$keys->{'atp'}{$atp} = 0; |
| 997 |
} else { |
| 998 |
$keys->{'atp'}{$atp} = 1; |
| 999 |
}; |
| 1000 |
}; |
| 1001 |
|
| 1002 |
=head2 C<warn_or_die> |
| 1003 |
|
| 1004 |
This is a subroutine rather than a method, so that is can be used even |
| 1005 |
without an active keywords object. It is a wrapper for generating |
| 1006 |
error and warning methods that will work in command-line, GUI, and CGI |
| 1007 |
environments. |
| 1008 |
|
| 1009 |
$keywords -> warn_or_die("This is a warning message", $die); |
| 1010 |
|
| 1011 |
The first argument is the actual message. The C<die> argument is 0, |
| 1012 |
1, or 2 as described for C<parse_input>. The final argument is a |
| 1013 |
keywords object and is only used in the CGI environment. In that |
| 1014 |
case, here are no convenient output or error channels, so messages are |
| 1015 |
stored in the keywords hash as a single string under the key |
| 1016 |
"www_warn". |
| 1017 |
|
| 1018 |
=cut |
| 1019 |
|
| 1020 |
sub warn_or_die { |
| 1021 |
my $keys = shift; |
| 1022 |
my ($message, $die) = @_; |
| 1023 |
if ($die == 1) { # Tk |
| 1024 |
##my $temp = MainWindow->new; |
| 1025 |
##$temp -> withdraw; |
| 1026 |
$::top -> messageBox(-icon => 'error', |
| 1027 |
-message => $message, |
| 1028 |
-title => 'Atoms: Error', |
| 1029 |
-type => 'OK'); |
| 1030 |
##undef $temp; |
| 1031 |
return -1; |
| 1032 |
} elsif ($die == 2) { # CGI |
| 1033 |
$keys -> {www_warn} .= $message; |
| 1034 |
} elsif ($die == 0) { # CLI |
| 1035 |
warn $message; |
| 1036 |
$keys -> {cli_warn} .= $message if $keys; |
| 1037 |
} elsif ($die == 4) { # ignore |
| 1038 |
return -1; |
| 1039 |
} else { |
| 1040 |
die "Programmer error! Invalid run level $die in warn_or_die!\n"; |
| 1041 |
}; |
| 1042 |
}; |
| 1043 |
|
| 1044 |
## =head2 C<available_languages> |
| 1045 |
## |
| 1046 |
## This subroutine takes no arguments and returns a list of the languages |
| 1047 |
## for which translations of the language data exist. |
| 1048 |
## |
| 1049 |
## =cut |
| 1050 |
|
| 1051 |
sub available_languages { |
| 1052 |
return @available_languages; |
| 1053 |
}; |
| 1054 |
|
| 1055 |
|
| 1056 |
=head2 C<parse_atp> |
| 1057 |
|
| 1058 |
This subroutine is the output engine. It parses the appropriate atp |
| 1059 |
file, interprets its content, and generates output data. This one |
| 1060 |
subroutine handles the creation of F<feff.inp> and the other sorts of |
| 1061 |
lists generated by Atoms. It also generates reports for the |
| 1062 |
Absorption notecard in TkAtoms and for the lists describing the DAFS |
| 1063 |
calculation as a function of energy and the powder calculation as a |
| 1064 |
function of angle. Essentially all output from all programs that come |
| 1065 |
with the Atoms package is generated here. |
| 1066 |
|
| 1067 |
my ($default_name, $is_feff) = |
| 1068 |
&parse_atp($atp, $cell, $keywords, \@cluster, \@neutral, \$contents); |
| 1069 |
open (INP, ">".$default_name) or die $!; |
| 1070 |
print INP $contents; |
| 1071 |
close INP; |
| 1072 |
|
| 1073 |
The arguments are |
| 1074 |
|
| 1075 |
=over 4 |
| 1076 |
|
| 1077 |
=item 1. |
| 1078 |
|
| 1079 |
A string specifying the atp file to read. The actual atp file either |
| 1080 |
lives in the F<atp/> subdirectory of the Atoms installation or in the |
| 1081 |
F<.atoms/> directory of the individual user. |
| 1082 |
|
| 1083 |
=item 2. |
| 1084 |
|
| 1085 |
A Cell object. |
| 1086 |
|
| 1087 |
=item 3. |
| 1088 |
|
| 1089 |
A keywords object. |
| 1090 |
|
| 1091 |
=item 4. |
| 1092 |
|
| 1093 |
A reference to the list generated by the C<build_cluster> subroutine, |
| 1094 |
to the list containing a calculation such as the one in DAFS or |
| 1095 |
Powder, or 'whatever the thing is that I did to make atoms.inp output |
| 1096 |
work.' |
| 1097 |
|
| 1098 |
=item 5. |
| 1099 |
|
| 1100 |
A reference to a list. This is just a place holder and is not |
| 1101 |
currently used for anything. |
| 1102 |
|
| 1103 |
=item 6. |
| 1104 |
|
| 1105 |
A scalar reference. This is assumed to be empty on entry, and is |
| 1106 |
filled with contents intended for the output file on exit. |
| 1107 |
|
| 1108 |
=back |
| 1109 |
|
| 1110 |
Typically the C<contents> variables is the things written to the |
| 1111 |
output channel. The two return values are the default name for the |
| 1112 |
output file and a flag indicating whether the output is intended to |
| 1113 |
run FEFF. Both of these pieces of information are read from the |
| 1114 |
C<meta> line of the atp file. |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
=head1 EXPORTED SUBROUTINES |
| 1119 |
|
| 1120 |
=head2 C<absorption> |
| 1121 |
|
| 1122 |
This subroutine calculates the total absorption (or one over the |
| 1123 |
e-fold absorption length), the delta_mu across the edge (or one over |
| 1124 |
the unit edge step length) and the specific gravity of the crystal. |
| 1125 |
|
| 1126 |
($total_absorption, $delta_mu, $specific_gravity) = |
| 1127 |
&absorption(\$cell, $central, $edge); |
| 1128 |
|
| 1129 |
The input arguments are a populated cell, the tag of the central atom, |
| 1130 |
and the alphanumeric (i.e. K, L1, etc) symbol of the absorption edge. |
| 1131 |
|
| 1132 |
Note that the values returned depend on the data resource used. See |
| 1133 |
L<Xray::Absorption>. |
| 1134 |
|
| 1135 |
|
| 1136 |
=cut |
| 1137 |
|
| 1138 |
|
| 1139 |
## this reproduces well the results from the Fortran atoms. |
| 1140 |
# sub absorption { |
| 1141 |
# my ($cell, $central, $edge) = @_; |
| 1142 |
# $central = lc($central); |
| 1143 |
# my $specified = ($edge =~ /\b\d+\b/); |
| 1144 |
# my $energy = ($edge =~ /\b\d+\b/) ? |
| 1145 |
# $edge : Xray::Absorption -> get_energy($central, $edge); |
| 1146 |
# my ($contents) = $cell -> attributes("Contents"); |
| 1147 |
# my ($bravais) = $cell -> attributes("Bravais"); |
| 1148 |
# my $brav = ($#{$bravais}+4) / 3; |
| 1149 |
# my ($volume, $occ) = $cell -> attributes("Volume", "Occupancy"); |
| 1150 |
# my ($mass, $xsec, $delta_mu, $den, $conv) = (0,0,0,0,0); |
| 1151 |
# my %cache = (); # memoize and call cross_section less often |
| 1152 |
# foreach my $atom (@{$contents}) { |
| 1153 |
# my ($element, $this_occ) = |
| 1154 |
# $ {$$atom[3]} -> attributes("Element", "Occupancy"); |
| 1155 |
# # print join(" ", $element, $this_occ, $/); |
| 1156 |
# $element = lc($element); |
| 1157 |
# my $factor = $this_occ; # $occ ? $this_occ : 1; # consider site occupancy?? |
| 1158 |
# my $weight = Xray::Absorption -> get_atomic_weight($element); |
| 1159 |
# $mass += $weight*$factor; |
| 1160 |
# $cache{lc($element)} ||= |
| 1161 |
# scalar Xray::Absorption -> cross_section($element, $energy+50); |
| 1162 |
# printf "%s %.7f %.3f\n", $element, $cache{lc($element)}, $factor; |
| 1163 |
# $xsec += $cache{lc($element)} * $factor; |
| 1164 |
# if (($central eq $element) and not $specified) { |
| 1165 |
# $delta_mu += |
| 1166 |
# ($factor/$brav) * |
| 1167 |
# ( $cache{lc($central)} - |
| 1168 |
# scalar Xray::Absorption -> cross_section($central, $energy-50) ); |
| 1169 |
# printf "%.2f %s %.7f %.7f\n", $brav, $element, $cache{lc($element)}, scalar Xray::Absorption -> cross_section($central, $energy-50); |
| 1170 |
# }; |
| 1171 |
# }; |
| 1172 |
# $mass *= 1.66053/$volume; ## atomic mass unit = 1.66053e-24 gram |
| 1173 |
# $xsec /= $volume; |
| 1174 |
# $delta_mu /= $volume; |
| 1175 |
# print join(" ", $xsec, $delta_mu, $mass), $/; |
| 1176 |
# return ($xsec, $delta_mu, $mass); |
| 1177 |
# }; |
| 1178 |
sub absorption { |
| 1179 |
my ($cell, $central, $edge) = @_; |
| 1180 |
$central = lc($central); |
| 1181 |
my $specified = ($edge =~ /\b\d+\b/); |
| 1182 |
my $energy = ($edge =~ /\b\d+\b/) ? |
| 1183 |
$edge : Xray::Absorption -> get_energy($central, $edge); |
| 1184 |
my ($contents) = $cell -> attributes("Contents"); |
| 1185 |
my ($bravais) = $cell -> attributes("Bravais"); |
| 1186 |
my $brav = ($#{$bravais}+4) / 3; |
| 1187 |
my ($volume, $occ) = $cell -> attributes("Volume", "Occupancy"); |
| 1188 |
my ($mass, $xseca, $xsecb, $delta_mu, $den, $conv) = (0,0,0,0,0,0); |
| 1189 |
my %cache = (); # memoize and call cross_section less often |
| 1190 |
## above |
| 1191 |
foreach my $atom (@{$contents}) { |
| 1192 |
my ($element, $this_occ) = |
| 1193 |
$ {$$atom[3]} -> attributes("Element", "Occupancy"); |
| 1194 |
# print join(" ", $element, $this_occ, $/); |
| 1195 |
$element = lc($element); |
| 1196 |
my $factor = $this_occ; # $occ ? $this_occ : 1; # consider site occupancy?? |
| 1197 |
my $weight = Xray::Absorption -> get_atomic_weight($element); |
| 1198 |
$mass += $weight*$factor; |
| 1199 |
$cache{lc($element)} ||= |
| 1200 |
scalar Xray::Absorption -> cross_section($element, $energy+50); |
| 1201 |
$xseca += $cache{lc($element)} * $factor; |
| 1202 |
}; |
| 1203 |
## below |
| 1204 |
%cache = (); # memoize and call cross_section less often |
| 1205 |
foreach my $atom (@{$contents}) { |
| 1206 |
my ($element, $this_occ) = |
| 1207 |
$ {$$atom[3]} -> attributes("Element", "Occupancy"); |
| 1208 |
# print join(" ", $element, $this_occ, $/); |
| 1209 |
$element = lc($element); |
| 1210 |
my $factor = $this_occ; # $occ ? $this_occ : 1; # consider site occupancy?? |
| 1211 |
$cache{lc($element)} ||= |
| 1212 |
scalar Xray::Absorption -> cross_section($element, $energy-50); |
| 1213 |
$xsecb += $cache{lc($element)} * $factor; |
| 1214 |
}; |
| 1215 |
$mass *= 1.66053/$volume; ## atomic mass unit = 1.66053e-24 gram |
| 1216 |
$xseca /= $volume; |
| 1217 |
$xsecb /= $volume; |
| 1218 |
$delta_mu = $xseca - $xsecb; |
| 1219 |
return ($xseca, $delta_mu, $mass); |
| 1220 |
}; |
| 1221 |
|
| 1222 |
|
| 1223 |
=head2 C<xsec> |
| 1224 |
|
| 1225 |
This subroutine calculates the total absorption (or one over the |
| 1226 |
e-fold absorption length) and the specific gravity of the crystal at a |
| 1227 |
specified energy (rather than around the edge energy of an element). |
| 1228 |
|
| 1229 |
In scalar context: |
| 1230 |
|
| 1231 |
($total_absorption, $specific_gravity) = |
| 1232 |
&xsec($cell, $central, $energy); |
| 1233 |
|
| 1234 |
In list context: |
| 1235 |
|
| 1236 |
(\@total_absorption, $specific_gravity) = |
| 1237 |
&xsec($cell, $central, \@energies); |
| 1238 |
|
| 1239 |
The input arguments are a populated cell and the tag of the central |
| 1240 |
atom. The third argument is either the desired energy in eV or a |
| 1241 |
reference to a list of energies in eV. |
| 1242 |
|
| 1243 |
Note that the values returned depend on the data resource used. See |
| 1244 |
L<Xray::Absorption>. |
| 1245 |
|
| 1246 |
|
| 1247 |
=cut |
| 1248 |
|
| 1249 |
sub xsec { |
| 1250 |
my ($cell, $central, $energy) = @_; |
| 1251 |
$central = lc($central); |
| 1252 |
my ($contents) = $cell -> attributes("Contents"); |
| 1253 |
my ($bravais) = $cell -> attributes("Bravais"); |
| 1254 |
my $brav = ($#{$bravais}+4) / 3; |
| 1255 |
my ($volume, $occ) = $cell -> attributes("Volume", "Occupancy"); |
| 1256 |
my ($xsec, $delta_mu, $den, $conv) = (0,0,0,0); |
| 1257 |
my @xsec = (); |
| 1258 |
my %cache = (); # memoize and call cross_section less often |
| 1259 |
foreach my $atom (@{$contents}) { |
| 1260 |
my ($element, $this_occ) = |
| 1261 |
$ {$$atom[3]} -> attributes("Element", "Occupancy"); |
| 1262 |
$element = lc($element); |
| 1263 |
my $factor = $this_occ; # $occ ? $this_occ : 1; # consider site occupancy?? |
| 1264 |
if (wantarray) { |
| 1265 |
exists $cache{$element} or |
| 1266 |
@{$cache{$element}} = |
| 1267 |
Xray::Absorption -> cross_section($element, $energy); |
| 1268 |
foreach (0 .. $#{$energy}) { |
| 1269 |
$xsec[$_] += $ {$cache{$element}}[$_] * $factor; |
| 1270 |
}; |
| 1271 |
} else { |
| 1272 |
$cache{$element} ||= |
| 1273 |
scalar Xray::Absorption -> cross_section($element, $energy); |
| 1274 |
$xsec += $cache{$element} * $factor; |
| 1275 |
}; |
| 1276 |
}; |
| 1277 |
$xsec /= $volume; |
| 1278 |
return wantarray ? @xsec : $xsec; |
| 1279 |
}; |
| 1280 |
|
| 1281 |
sub density { |
| 1282 |
my $cell = $_[0]; |
| 1283 |
my ($contents, $bravais) = $cell -> attributes("Contents", "Bravais"); |
| 1284 |
my $brav = ($#{$bravais}+4) / 3; |
| 1285 |
my ($volume, $occ) = $cell -> attributes("Volume", "Occupancy"); |
| 1286 |
my ($mass, $den, $conv) = (0,0,0); |
| 1287 |
foreach my $atom (@{$contents}) { |
| 1288 |
my ($element, $this_occ) = |
| 1289 |
$ {$$atom[3]} -> attributes("Element", "Occupancy"); |
| 1290 |
$element = lc($element); |
| 1291 |
my $factor = $this_occ; # $occ ? $this_occ : 1; # consider site occupancy?? |
| 1292 |
my $weight = Xray::Absorption -> get_atomic_weight($element); |
| 1293 |
$mass += $weight*$factor; |
| 1294 |
}; |
| 1295 |
$mass *= 1.66053/$volume; ## atomic mass unit = 1.66053e-24 gram |
| 1296 |
return $mass; |
| 1297 |
}; |
| 1298 |
|
| 1299 |
|
| 1300 |
sub mcmaster_pre_edge { |
| 1301 |
my ($central, $edge) = @_; |
| 1302 |
$edge = lc($edge); |
| 1303 |
my $emin = Xray::Absorption -> get_energy($central, $edge) - 10; |
| 1304 |
## find the pre-edge line |
| 1305 |
my %next_e = ("k"=>"l1", "l1"=>"l2", "l2"=>"l3", "l3"=>"m"); |
| 1306 |
my $ebelow; |
| 1307 |
if (exists $next_e{$edge}) { |
| 1308 |
$ebelow = Xray::Absorption -> get_energy($central, $next_e{$edge}) + 10; |
| 1309 |
$ebelow = (($emin - $ebelow) > 100) ? $emin - 100 : $ebelow; |
| 1310 |
} else { |
| 1311 |
$ebelow = $emin - 100; |
| 1312 |
}; |
| 1313 |
my $delta = ($emin - $ebelow)/10;; |
| 1314 |
my @i=(0..9); # load the pre edge energies/sigmas |
| 1315 |
my @energy = map {$ebelow + $delta*$_} @i; |
| 1316 |
my @sigma = Xray::Absorption -> cross_section($central, \@energy); |
| 1317 |
# and fit 'em |
| 1318 |
my $pre_edge = Statistics::Descriptive::Full->new(); |
| 1319 |
$pre_edge -> add_data(@sigma); |
| 1320 |
my ($bpre, $slope) = $pre_edge -> least_squares_fit(@energy); |
| 1321 |
$bpre ||= 0; $slope ||= 0; |
| 1322 |
return ($bpre, $slope); |
| 1323 |
}; |
| 1324 |
|
| 1325 |
=head2 C<mcmaster> |
| 1326 |
|
| 1327 |
This is called C<mcmaster> for historical reasons. It calculates the |
| 1328 |
normalization correcion for a given central atom. |
| 1329 |
|
| 1330 |
$sigma_mm = &mcmaster($central, $edge); |
| 1331 |
|
| 1332 |
It takes the central atoms tag and the alphanumeric edge symbol as |
| 1333 |
arguments and returns the normalization correction in units of |
| 1334 |
Angstrom squared. |
| 1335 |
|
| 1336 |
Note that the values returned depend on the data resource used. See |
| 1337 |
L<Xray::Absorption>. |
| 1338 |
|
| 1339 |
=cut |
| 1340 |
|
| 1341 |
|
| 1342 |
## $span in the fortran version was 500eV and the regression was |
| 1343 |
## performed with a square term. |
| 1344 |
## Statistics::Descriptive::least_squares_fit only fits a line, so I |
| 1345 |
## drew the #span back to 300 volts. This gives the "canonical" |
| 1346 |
## 0.00052 for copper. |
| 1347 |
sub mcmaster { |
| 1348 |
my ($central, $edge) = @_; |
| 1349 |
return Xray::FluorescenceEXAFS->mcmaster($central, $edge); |
| 1350 |
}; |
| 1351 |
|
| 1352 |
|
| 1353 |
=head2 C<i_zero> |
| 1354 |
|
| 1355 |
This calculates the correcion due to the I0 fill gases in a |
| 1356 |
fluorescence experiment. |
| 1357 |
|
| 1358 |
$sigma_i0 = &i_zero($central, $edge, $nitrogen, $argon, $krypton); |
| 1359 |
|
| 1360 |
It takes the central atoms tag, the alphanumeric edge symbol, and the |
| 1361 |
volume percentages of the three gases as arguments. It assumes that |
| 1362 |
any remaining volume is filled with helium and it correctly accounts |
| 1363 |
for the fact that nitrogen is a diatom. It returns the I0 correction |
| 1364 |
in units of Angstrom squared. |
| 1365 |
|
| 1366 |
Note that the values returned depend on the data resource used. See |
| 1367 |
L<Xray::Absorption>. |
| 1368 |
|
| 1369 |
=cut |
| 1370 |
|
| 1371 |
sub i_zero { |
| 1372 |
my ($central, $edge, $nitrogen, $argon, $krypton) = @_; |
| 1373 |
my $gases = {nitrogen=>$nitrogen, argon=>$argon, krypton=>$krypton}; |
| 1374 |
return Xray::FluorescenceEXAFS->i0($central, $edge, $gases); |
| 1375 |
}; |
| 1376 |
|
| 1377 |
=head2 C<self> |
| 1378 |
|
| 1379 |
This calculates the correcion due to self-absorption fluorescence |
| 1380 |
experiment. It assumes that the sample is infinately thick and that |
| 1381 |
the entry and exit angles of the photons are the same. |
| 1382 |
|
| 1383 |
$sigma_i0 = &self($central, $edge, $cell); |
| 1384 |
|
| 1385 |
It takes the central atoms tag, the alphanumeric edge symbol, and a |
| 1386 |
populated cell. It returns a list whose zeroth element is the |
| 1387 |
multiplicative amplitude correction and whose first element is the a |
| 1388 |
correction in units of Angstrom squared. |
| 1389 |
|
| 1390 |
Note that the values returned depend on the data resource used. See |
| 1391 |
L<Xray::Absorption>. |
| 1392 |
|
| 1393 |
=cut |
| 1394 |
|
| 1395 |
sub self { |
| 1396 |
my ($central, $edge, $cell) = @_; |
| 1397 |
my @list = (); |
| 1398 |
my ($contents) = $cell -> attributes("Contents"); |
| 1399 |
my ($occ) = $cell -> attributes("Occupancy"); |
| 1400 |
my %count = (); |
| 1401 |
foreach my $atom (@{$contents}) { |
| 1402 |
## apparetnly this needs to be coerced into a string |
| 1403 |
my $this = sprintf("%s", $ {$$atom[3]} -> attributes("Element") ); |
| 1404 |
++$count{$this}; |
| 1405 |
}; |
| 1406 |
return Xray::FluorescenceEXAFS->self($central, $edge, \%count); |
| 1407 |
}; |
| 1408 |
|
| 1409 |
|
| 1410 |
## =over 4 |
| 1411 |
## |
| 1412 |
## =item &bravais_string |
| 1413 |
## |
| 1414 |
## Returns the peculiar array from &bravais as a pretty-printed string. |
| 1415 |
## This is not method of the Cell class, it is just a normal function. |
| 1416 |
## This demonstrates the differnt return values of bravais and |
| 1417 |
## bravais_string |
| 1418 |
## |
| 1419 |
## @list = Xray::Xtal::Cell::bravais("F m 3 m", 0); |
| 1420 |
## print Xray::Xtal::Cell::bravais_string(@list), "\n", |
| 1421 |
## join(", ", @list), "\n": |
| 1422 |
## |
| 1423 |
## |-> (0, 0, 0), (0, 1/2, 1/2), (1/2, 0, 1/2), (1/2, 1/2, 0) |
| 1424 |
## (0, 0, 0, 0, 0.5, 0.5, 0.5, 0, 0.5, 0.5, 0.5, 0) |
| 1425 |
## |
| 1426 |
## =back |
| 1427 |
## |
| 1428 |
## =cut |
| 1429 |
|
| 1430 |
sub bravais_string { |
| 1431 |
my ($bravais, $gnxas) = @_; |
| 1432 |
return "" unless ($bravais); |
| 1433 |
my @temp = @$bravais; |
| 1434 |
length(join("", @temp)) || return ($gnxas) ? "1$/0,0,0" : "(0, 0, 0)"; |
| 1435 |
@temp = map { |
| 1436 |
if (abs($_ - 0.5) < EPSILON) { |
| 1437 |
($_ = "1/2") |
| 1438 |
} elsif (abs($_ - 2/3) < EPSILON) { |
| 1439 |
($_ = "2/3") |
| 1440 |
} elsif (abs($_ - 1/3) < EPSILON) { |
| 1441 |
($_ = "1/3") |
| 1442 |
} else { |
| 1443 |
($_ = "0") |
| 1444 |
} } |
| 1445 |
@temp; |
| 1446 |
my $string = ""; |
| 1447 |
if ($gnxas) { |
| 1448 |
($#temp == 2) and $string = "2$/"; |
| 1449 |
($#temp == 5) and $string = "3$/"; |
| 1450 |
($#temp == 8) and $string = "4$/"; |
| 1451 |
$string .= "0.0000,0.0000,0.0000"; |
| 1452 |
while (@temp) { |
| 1453 |
$string .= sprintf("$/%6.4f,%6.4f,%6.4f", |
| 1454 |
eval(shift @temp), eval(shift @temp), eval(shift @temp)); |
| 1455 |
}; |
| 1456 |
} else { |
| 1457 |
$string = "(0, 0, 0), ("; |
| 1458 |
$string .= join(", ", $temp[0], $temp[1], $temp[2]); |
| 1459 |
($#{temp} > 2) && |
| 1460 |
($string .= "), (" . join(", ", $temp[3], $temp[4], $temp[5])); |
| 1461 |
($#{temp} > 5) && |
| 1462 |
($string .= "), (" . join(", ", $temp[6], $temp[7], $temp[8])); |
| 1463 |
$string .= ")"; |
| 1464 |
}; |
| 1465 |
return $string; |
| 1466 |
}; |
| 1467 |
|
| 1468 |
=head2 C<build_cluster> |
| 1469 |
|
| 1470 |
This exported routine builds a spherical cluster from a populated unit |
| 1471 |
cell by constructing a rhomboid of an intger number of complete cells |
| 1472 |
which fully encloses the sphere. The spherical cluster is probably |
| 1473 |
not stoichiometric. |
| 1474 |
|
| 1475 |
build_cluster($cell, $keywords, \@cluster, \@neutral); |
| 1476 |
|
| 1477 |
The first two arguments are cell and keywords objects. The cell |
| 1478 |
should already have been populated by the C<populate> method. The |
| 1479 |
thrid argument is a reference to an array that will contain the |
| 1480 |
cluster. The fourth argument is an unused placeholder. |
| 1481 |
|
| 1482 |
The C<@cluster> array is an anonymous array or arrays. Each entry in |
| 1483 |
C<@cluster> is an array containing x, y, z, r, r squared, x, y, z, the |
| 1484 |
formula for x, the formula for y , and the formula for z. The first |
| 1485 |
three arguments are the coordinates to full precision. The fourth |
| 1486 |
through seventh arguments are of limited precision and are used |
| 1487 |
internally for sorting the cluster. |
| 1488 |
|
| 1489 |
=cut |
| 1490 |
|
| 1491 |
|
| 1492 |
# cell, rmax, position of central atom |
| 1493 |
# return cluster and neutral cluster |
| 1494 |
sub build_cluster { |
| 1495 |
use POSIX qw(ceil); |
| 1496 |
my ($cell, $keys, $r_cluster, $r_neutral) = @_; |
| 1497 |
my $rmax = $keys->{'rmax'}; |
| 1498 |
@$r_cluster = (); |
| 1499 |
@$r_neutral = (); |
| 1500 |
my ($central, $xcenter, $ycenter, $zcenter) = |
| 1501 |
$cell -> central($keys->{"core"}); |
| 1502 |
my $setting = $cell->{Setting}; |
| 1503 |
my $crystal_class = $cell -> crystal_class(); |
| 1504 |
my $do_tetr = ($crystal_class eq "tetragonal" ) && ($setting); |
| 1505 |
##print join(" ", $central, $xcenter, $ycenter, $zcenter), $/; |
| 1506 |
my ($aa, $bb, $cc) = $cell -> attributes("A", "B", "C"); |
| 1507 |
my $xup = ceil($rmax/$aa - 1 + $xcenter); |
| 1508 |
my $xdn = ceil($rmax/$aa - $xcenter); |
| 1509 |
my $yup = ceil($rmax/$bb - 1 + $ycenter); |
| 1510 |
my $ydn = ceil($rmax/$bb - $ycenter); |
| 1511 |
my $zup = ceil($rmax/$cc - 1 + $zcenter); |
| 1512 |
my $zdn = ceil($rmax/$cc - $zcenter); |
| 1513 |
##print join(" ", "up,dn", $xup, $xdn, $yup, $ydn, $zup, $zdn), $/; |
| 1514 |
#my $num_z = int($rmax/$cc) + 1; # | |
| 1515 |
my $rmax_squared = $rmax**2; # (sprintf "%9.5f", $rmax**2); |
| 1516 |
my ($contents) = $cell -> attributes("Contents"); |
| 1517 |
foreach my $nz (-$zdn .. $zup) { |
| 1518 |
foreach my $ny (-$ydn .. $yup) { |
| 1519 |
foreach my $nx (-$xdn .. $xup) { |
| 1520 |
foreach my $pos (@{$contents}) { |
| 1521 |
my ($x, $y, $z) = ($$pos[0]+$nx, $$pos[1]+$ny, $$pos[2]+$nz); |
| 1522 |
($x, $y, $z) = ($x-$xcenter, $y-$ycenter, $z-$zcenter); |
| 1523 |
($x, $y, $z) = $cell -> metric($x, $y, $z); |
| 1524 |
($do_tetr) and ($x, $y) = (($x+$y)/sqrt(2), ($x-$y)/sqrt(2)); |
| 1525 |
#printf "in: %25s %25s %25s %3d %3d %3d\n", @$pos[4..6],$nx,$ny,$nz; |
| 1526 |
my ($fx, $fy, $fz) = &rectify_formula(@$pos[4..6], $nx, $ny, $nz); |
| 1527 |
#printf "out: %25s %25s %25s\n\n", $fx, $fy, $fz; |
| 1528 |
my $r_squared = (sprintf "%9.5f", $x**2 + $y**2 + $z**2); |
| 1529 |
my $this_site = [$x, $y, $z, $$pos[3], |
| 1530 |
$r_squared, # cache the |
| 1531 |
(sprintf "%11.7f", $x), # stuff needed |
| 1532 |
(sprintf "%11.7f", $y), # for sorting |
| 1533 |
(sprintf "%11.7f", $z), |
| 1534 |
$fx, $fy, $fz ]; |
| 1535 |
($r_squared < $rmax_squared) && # keep the ones within rmax |
| 1536 |
(push @$r_cluster, $this_site); |
| 1537 |
## (push @$r_neutral, $this_site); |
| 1538 |
}; |
| 1539 |
}; |
| 1540 |
}; |
| 1541 |
}; |
| 1542 |
|
| 1543 |
## =============================== sort the cluster (& neutral clus.) |
| 1544 |
foreach my $r_list ($r_cluster) { ##, $r_neutral) { |
| 1545 |
@$r_list = sort { |
| 1546 |
($a->[4] cmp $b->[4]) # sort by distance squared or ... |
| 1547 |
or |
| 1548 |
($ {$a->[3]}->{Element} cmp $ {$b->[3]}->{Element}) |
| 1549 |
or |
| 1550 |
($a->[7] cmp $b->[7]) # by z value or ... |
| 1551 |
or |
| 1552 |
($a->[6] cmp $b->[6]) # by y value or ... |
| 1553 |
or |
| 1554 |
($a->[5] cmp $b->[5]); # by x value |
| 1555 |
## or |
| 1556 |
## ($ {$b->[3]}->{Host} <=> $ {$a->[3]}->{Host}); # hosts before dopants |
| 1557 |
} @$r_list; |
| 1558 |
}; |
| 1559 |
|
| 1560 |
## final adjustment to the formulas, store the formulas for the |
| 1561 |
## central atom ... |
| 1562 |
$keys -> {cformulas} = |
| 1563 |
[$$r_cluster[0][8], $$r_cluster[0][9], $$r_cluster[0][10]]; |
| 1564 |
## ## ... subtract the central atom coordinates from each site ... |
| 1565 |
## foreach my $site (reverse(@$r_cluster)) { |
| 1566 |
## (@$site[8..10]) = |
| 1567 |
## ($$site[8] . " - Xc", $$site[9] . " - Yc", $$site[10] . " - Zc"); |
| 1568 |
## }; |
| 1569 |
## ## ... and set the central atom to an empty string |
| 1570 |
## ($$r_cluster[0][8], $$r_cluster[0][9], $$r_cluster[0][10]) = ("", "", ""); |
| 1571 |
|
| 1572 |
## if this is a tetragonal crystal in the C or F setting , rotate |
| 1573 |
## all the coordinates back to the original setting |
| 1574 |
if ($do_tetr) { |
| 1575 |
my ($a, $b) = ($cell->{A}, $cell->{B}); |
| 1576 |
$cell -> make(A=>$a*sqrt(2), B=>$b*sqrt(2)); |
| 1577 |
}; |
| 1578 |
}; |
| 1579 |
|
| 1580 |
|
| 1581 |
sub rectify_formula { |
| 1582 |
my ($fx, $fy, $fz, $nnx, $nny, $nnz, $r_cformula) = @_; |
| 1583 |
my $debug_formulas = 1; |
| 1584 |
##open DEBUG, '>debug.formulas' or die $!; |
| 1585 |
#warn "input: ", join(" | ",$fx, $fy, $fz, $nnx, $nny, $nnz, $/); |
| 1586 |
## apply bravais translation |
| 1587 |
map { |
| 1588 |
if ($_ < 0) { $_ = ' + '.$_ } |
| 1589 |
elsif ($_ == 0) { $_ = '' } |
| 1590 |
else { $_ = ' + '.$_ }; |
| 1591 |
} ($nnx, $nny, $nnz); |
| 1592 |
($fx, $fy, $fz) = ($fx . $nnx, $fy . $nny, $fz . $nnz); |
| 1593 |
## convert fractions to decimal |
| 1594 |
map { s|(\d)/(\d)|$1/$2|ge } ($fx, $fy, $fz); |
| 1595 |
# collapse the numeric fields |
| 1596 |
map { |
| 1597 |
my @list = reverse(split(/\s*\+\s*/, $_)); |
| 1598 |
#print join('|', @list, $/); |
| 1599 |
my $sum = 0; |
| 1600 |
LIST: while (@list) { |
| 1601 |
my $this = shift @list; |
| 1602 |
#print $this, " "; |
| 1603 |
if ($this =~ /^-?[xyz]/) { # whoops! put the variable back on |
| 1604 |
unshift @list, $this; |
| 1605 |
last LIST; |
| 1606 |
}; |
| 1607 |
$sum = eval '$sum + $this'; # eval the constants |
| 1608 |
}; |
| 1609 |
if ($sum) { # prettify the number and unshift it |
| 1610 |
$sum = sprintf("%10.6f", $sum); |
| 1611 |
$sum =~ s/\.?0+$//; |
| 1612 |
$sum =~ s/^\s+//; |
| 1613 |
unshift @list, $sum; |
| 1614 |
}; |
| 1615 |
@list = reverse(@list); |
| 1616 |
$_ = join("+", @list); # make a math expression |
| 1617 |
s/\+-/-/g; # finally clean up the + and - signs |
| 1618 |
s/([+-])/ $1 /g; |
| 1619 |
s|^ - |-|; |
| 1620 |
} ($fx, $fy, $fz); |
| 1621 |
# all done! |
| 1622 |
#warn "output: ", join(" ", $fx, $fy, $fz, $/ x 2); |
| 1623 |
return ($fx, $fy, $fz); |
| 1624 |
}; |
| 1625 |
|
| 1626 |
=head2 C<rcfile_name> |
| 1627 |
|
| 1628 |
This takes no arguments and returns the name of the Atoms runtime |
| 1629 |
configuration file belonging to the user. This does the ``right |
| 1630 |
thing'' on the different platforms. |
| 1631 |
|
| 1632 |
=cut |
| 1633 |
|
| 1634 |
sub rcfile_name { |
| 1635 |
return "???" if (&rcdirectory() eq '???'); |
| 1636 |
return File::Spec -> catfile(&rcdirectory(), "tkatoms.ini") |
| 1637 |
if (($^O eq 'MSWin32') or ($^O eq 'cygwin')); |
| 1638 |
return File::Spec -> catfile(&rcdirectory(), "atomsrc"); |
| 1639 |
}; |
| 1640 |
sub rcdirectory { |
| 1641 |
if ($^O eq 'VMS') { |
| 1642 |
return "???"; |
| 1643 |
} elsif ($^O eq 'os2') { |
| 1644 |
return "???"; |
| 1645 |
} elsif ($^O eq 'MacOS') { |
| 1646 |
return $lib_dir; |
| 1647 |
} elsif (($^O eq 'MSWin32') or ($^O eq 'cygwin')) { |
| 1648 |
return File::Spec->catfile($ENV{FEFFIT_DIR}, "horae") if defined($ENV{FEFFIT_DIR}); |
| 1649 |
return "???"; |
| 1650 |
} else { |
| 1651 |
my $home; |
| 1652 |
eval '$home = $ENV{"HOME"} || $ENV{"LOGDIR"} || (getpwuid($<))[7];' or |
| 1653 |
$home = ""; |
| 1654 |
return $home . "/.horae/"; |
| 1655 |
}; |
| 1656 |
}; |
| 1657 |
|
| 1658 |
sub read_rc { |
| 1659 |
open RC, $_[0] or die "could not open $_[0] as a configuration file\n"; |
| 1660 |
my $mode = ""; |
| 1661 |
while (<RC>) { |
| 1662 |
next if (/^\s*$/); |
| 1663 |
next if (/^\s*\#/); |
| 1664 |
if (/\[(\w+)\]/) { |
| 1665 |
$mode = lc($1); |
| 1666 |
next; |
| 1667 |
}; |
| 1668 |
chomp; |
| 1669 |
s/^\s+//; |
| 1670 |
unless ($_ =~ /\$c_/) {s/\s*\#.*$//;}; |
| 1671 |
my @line = split(/\s*[ \t=]\s*/, $_); |
| 1672 |
(defined $line[1]) and |
| 1673 |
(($line[1] eq "''") or ($line[1] eq '""')) and $line[1] = ''; |
| 1674 |
MODE: { |
| 1675 |
($line[0] =~ /^\s*\$([a-zA-Z_]+)/) and do { |
| 1676 |
my $var = $1; |
| 1677 |
$line[1] =~ s/[;\'\"]//g; |
| 1678 |
if ($var =~ /^c_/) { # colors |
| 1679 |
## my $v = substr($var, 2); |
| 1680 |
## $colors{$v} = $line[1]; |
| 1681 |
## if (($colors{$v} =~ /^[0-9a-fA-F]{6}$/) or |
| 1682 |
## ($colors{$v} =~ /^[0-9a-fA-F]{12}$/)) { |
| 1683 |
## $colors{$v} = '#'.$line[1]; |
| 1684 |
## }; |
| 1685 |
} elsif ($var =~ /^f_/) { # fonts |
| 1686 |
## my $v = substr($var, 2); |
| 1687 |
## $fonts{$v} = join(" ", @line[1..$#line]); |
| 1688 |
## $fonts{$v} =~ s/[;\'\"]//g; |
| 1689 |
} else { # meta |
| 1690 |
$meta{$var} = $line[1]; |
| 1691 |
}; |
| 1692 |
last MODE; |
| 1693 |
}; |
| 1694 |
($mode eq 'meta') and do { |
| 1695 |
$meta{$line[0]} = $line[1]; |
| 1696 |
last MODE; |
| 1697 |
}; |
| 1698 |
### don't need to parse colors and fonts here |
| 1699 |
## ($mode eq 'colors') and do { |
| 1700 |
## $colors{$line[0]} = $line[1]; |
| 1701 |
## (($colors{$line[0]} =~ /[0-9a-fA-F]{6}/) or |
| 1702 |
## ($colors{$line[0]} =~ /[0-9a-fA-F]{12}/)) and |
| 1703 |
## $colors{$line[0]} = '#'.$line[1]; |
| 1704 |
## last MODE; |
| 1705 |
## }; |
| 1706 |
## ($mode eq 'fonts') and do { |
| 1707 |
## $fonts{$line[0]} = join(" ", @line[1..$#line]); |
| 1708 |
## last MODE; |
| 1709 |
## }; |
| 1710 |
}; |
| 1711 |
}; |
| 1712 |
}; |
| 1713 |
|
| 1714 |
=head2 C<rcvalues> |
| 1715 |
|
| 1716 |
This takes no argmuents and returns the hash containing the values of |
| 1717 |
variables (but not of fonts or colors) read from the rc file. |
| 1718 |
|
| 1719 |
=cut |
| 1720 |
|
| 1721 |
sub rcvalues { |
| 1722 |
shift; |
| 1723 |
return %meta; |
| 1724 |
## ## 0 1 2 |
| 1725 |
## return ($always_write_feff, $atoms_language, $write_to_pwd, |
| 1726 |
## ## 3 4 5 |
| 1727 |
## $prefer_feff_eight, $absorption_tables, $dafs_default, |
| 1728 |
## ## 6 7 8 |
| 1729 |
## $plotting_hook, $default_filepath, $display_balloons, |
| 1730 |
## ## 9 10 11 |
| 1731 |
## $no_crystal_warnings, $one_frame, $convolve_dafs, |
| 1732 |
## ## 12 13 |
| 1733 |
## $never_ask_to_save, $ADB_location) |
| 1734 |
}; |
| 1735 |
|
| 1736 |
=head2 C<number> |
| 1737 |
|
| 1738 |
This takes a text string and attempts to evaluate it as a number. It |
| 1739 |
uses eval and so allows for simple math expressions, but it tries to |
| 1740 |
be safe and not eval just any old math expression. You can use this |
| 1741 |
if you want to evaluate numbers in the same manner as Atoms. |
| 1742 |
|
| 1743 |
$x = number(1/3); |
| 1744 |
printf "%7.5f\n", $x; |
| 1745 |
|--> 0.33333 |
| 1746 |
|
| 1747 |
=cut |
| 1748 |
|
| 1749 |
## this returns a text string as a proper number |
| 1750 |
sub number { |
| 1751 |
my $num = $_[0]; |
| 1752 |
(my $input = $num) =~ s/\s//g; # trim blanks |
| 1753 |
my $die = $_[1] || 0; |
| 1754 |
my $keys = $_[2] || Xray::Atoms->new(); |
| 1755 |
##my $m = $num+0; |
| 1756 |
my $num_match = '([+-]?(\d+\.?\d*|\.\d+))'; |
| 1757 |
($num =~ /^\s*$/) && return 0; # null value |
| 1758 |
##if ($num == $m) { |
| 1759 |
if ($num =~ /^\s*$num_match\s*$/) { # floating point number |
| 1760 |
($num = sprintf "%9.5f", $num) =~ s/^\s+//; |
| 1761 |
return $num; |
| 1762 |
}; # simple binary operation |
| 1763 |
if ($num =~ /^\s*($num_match)\s*(\-|\+|\/|\*)\s*($num_match)\s*$/) { |
| 1764 |
$num = eval $num; |
| 1765 |
($num = sprintf "%9.5f", $num) =~ s/^\s+//; |
| 1766 |
return $num; |
| 1767 |
}; |
| 1768 |
#(abs($num) < $Xray::Atoms::epsilon) && ($num = 0); |
| 1769 |
## interpret angle as 80.23'45" |
| 1770 |
my $message = join("", "The string \"", $num, "\" was found among the atom coordinates. Atoms doesn't know what to do with it. The parameter was set to zero. You might want to verify your input data.", $/); |
| 1771 |
$keys -> warn_or_die($message, $die); |
| 1772 |
print caller; |
| 1773 |
return 0; |
| 1774 |
}; |
| 1775 |
|
| 1776 |
# Autoload methods go after =cut, and are processed by the autosplit program. |
| 1777 |
|
| 1778 |
1; |
| 1779 |
__END__ |
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
=head1 MORE INFORMATION |
| 1784 |
|
| 1785 |
There is more information available in the Atoms document. There you |
| 1786 |
will find complete descriptions of atp files, calculations using the |
| 1787 |
Xray::Absorption package, keywords in atoms input files and lots of |
| 1788 |
other topics. |
| 1789 |
|
| 1790 |
|
| 1791 |
=head1 AUTHOR |
| 1792 |
|
| 1793 |
Bruce Ravel <[email protected]> |
| 1794 |
Atoms URL: http://feff.phys.washington.edu/~ravel/software/atoms/ |
| 1795 |
|
| 1796 |
|
| 1797 |
=cut |
| 1798 |
|
| 1799 |
## Local Variables: |
| 1800 |
## time-stamp-line-limit: 25 |
| 1801 |
## End: |