File: CL.pm

package info (click to toggle)
horae 063-3
  • links: PTS
  • area: contrib
  • in suites: etch, etch-m68k
  • size: 23,964 kB
  • ctags: 4,939
  • sloc: perl: 101,791; ansic: 6,700; xml: 2,019; lisp: 744; sh: 81; makefile: 76
file content (376 lines) | stat: -rw-r--r-- 11,549 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
##  This module is copyright (c) 1999-2006 Bruce Ravel
##  <bravel@anl.gov>
##  http://feff.phys.washington.edu/~ravel/software/exafs/
##
## -------------------------------------------------------------------
##     All rights reserved. This program is free software; you can
##     redistribute it and/or modify it under the same terms as Perl
##     itself.
##
##     This program is distributed in the hope that it will be useful,
##     but WITHOUT ANY WARRANTY; without even the implied warranty of
##     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##     Artistic License for more details.
## -------------------------------------------------------------------
######################################################################
## Time-stamp: <1999/05/20 15:20:00 bruce>
######################################################################
## Code:

=head1 NAME

Xray::Absorption::CL - Perl interface to the Cromer-Liberman tables

=head1 SYNOPSIS

   use Xray::Absorption;
   Xray::Absorption -> load("cl");

See the documentation for Xray::Absorption for details.

=head1 DESCRIPTION

This module is inherited by the Xray::Absorption module and provides
access to the data contained in the Cromer-Liberman tables of anomalous
scattering factors and line and edge energies.

The data in this module and the Fortran code which it calls as a
shared library, referred to as "The CL Tables", was published as

  S. Brennan and P.L. Cowen, Rev. Sci. Instrum, vol 63,
  p.850 (1992)

More information about these data is available on the Web at

   http://www.slac.ssrl.stanford.edu/absorb.html.

The values for the anomalous scattering factors are calculated by
calls to the Ifeffit library by Matt Newville.

The values of edge and line energies are contained in a database file
called F<cl.db> which is generated at install time from the flat
text files of the these data.  The data is stored in a Storable
archive using "network" ordering.  This allows speedy disk and memory
access along with network and platform portability.

The required C<File::Spec>, C<Chemistry::Elements>, C<Storable> are
available from CPAN.

=cut


package Xray::Absorption::CL;

use Exporter ();
use Config;
use strict;
use Xray::Absorption;
use vars qw(@ISA $VERSION $resource $line_rule);

use strict;
use vars qw($VERSION $cvs_info $cl_version @ISA @EXPORT @EXPORT_OK);

require Exporter;

@ISA = qw(Exporter AutoLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT_OK = qw();
$cvs_info = '$Id: CL.pm,v 1.2 2001/09/20 17:42:04 bruce Exp $ ';
$VERSION = '1.21'; #(split(' ', $cvs_info))[2] || "pre_release";

my $epsilon = 0.001;		# a milivolt

use strict;
use Carp;
use File::Spec;
use Storable;
use Chemistry::Elements qw(get_name get_Z get_symbol);
#use Xray::Absorption::CLdata qw(cl_f1 cl_f2);
use Ifeffit qw(ifeffit put_array get_array);
## the next two line initialize Ifeffit under perl without changing
## any ifeffit global variables
my $foo = Ifeffit::get_scalar("\&screen_echo");
ifeffit("\&screen_echo = $foo\n");

use constant PI    => 4*atan2(1,1);
use constant RE    => 0.00002817938; # Classical electron radius in Angstroms
use constant HBARC => 1973.2858;     # in eV*Angstrom

my $dbfile = File::Spec->catfile($Xray::Absorption::data_dir, "cl.db");
my $r_cl     = retrieve($dbfile);

$cl_version = $$r_cl{'version'};


=head1 METHODS

The behaviour of the methods in this module is a bit different from
other modules used by C<Xray::Absorption>.  This section describes
methods which behave differently for this data resource.

=cut

sub current_resource {
  "CL.pm version $VERSION, database version $cl_version";
};

## is this element actually tabulated in these tables?
##    Xray::Absorption -> in_resource($elem) $elem can be Z, symbol, name
sub in_resource {
  shift;
  my $z = $_[0];
  $z = get_Z($z);
  (defined $z) || return 0;
  return 0 if $z < 1;
  return ( $z > 92 ) ? 0 : 1;
};

=over 4

=item C<get_energy>

Example:

   $energy = Xray::Absorption -> get_energy($elem, $edge);

This behaves similarly to the C<get_energy> method of the other
resources.  When using the CL data resource, C<$edge> can be any of
K, L1-L3, M1-M5, N1-N7, O1-O7, or P1-P3.  Line energies are not
supplied with the CL data set.  The line energies from the McMaster
tables are used.

=back

=cut

## $edge should be one of
##    Xray::Absorption -> get_energy($elem, $edge)
sub get_energy {
  shift;
  my ($sym,$edge) = @_;
  $sym = lc( get_symbol($sym) );
  Xray::Absorption -> in_resource($sym) || return 0;
  (defined $sym) || return 0;
  $edge = lc($edge);

  ## absorption edges
  if (($edge =~ /\b(k|l[1-3]|m[1-5]|n[1-7]|o[1-5]|p[1-3])\b/)
      and
      ($edge !~ /-\b(k|l[1-3]|m[1-5]|n[1-7]|o[1-5]|p[1-3])\b/)) {
    $edge = "energy_$edge";
    return (exists($$r_cl{$sym}{$edge})) ? $$r_cl{$sym}{$edge} : 0;
  };

  my $sieg_edge = lc(Xray::Absorption->get_Siegbahn($edge));
  if (($edge =~ /^ka/i) or ($sieg_edge =~ /^ka/i)) {
    ($edge = "energy_kalpha");
  };
  if (($edge =~ /^kb/i) or ($sieg_edge =~ /^kb/i)) {
    ($edge = "energy_kbeta");
  };
  if (($edge =~ /^la/i) or ($sieg_edge =~ /^la/i)) {
    ($edge = "energy_lalpha");
  };
  if (($edge =~ /^lb/i) or ($sieg_edge =~ /^lb/i)) {
    ($edge = "energy_lbeta");
  };
  return (exists($$r_cl{$sym}{$edge})) ? $$r_cl{$sym}{$edge} : 0;


  ## no such edge
  return 0;
};

## the list required by this method is preloaded into the database at
## install-time.
sub next_energy {
  shift;
  my $elem = shift;		# atom in question
  my $edge = shift;		# edge in question
  my @list = @_;		# other atoms in material
  my $hash = $$r_cl{energy_list};
  my $key = lc($elem) . "_" . lc($edge);
  while (1) {
    my ($el, $ed, $en) = @{$$hash{$key}};
    return () unless defined $el;
    return ($el, $ed, $en) if (grep(/^$el$/i, @list));
    $key = lc($el) . "_" . lc($ed);
  };

};

sub data_available {
  shift;
  my ($sym, $edge) = @_;
  $sym = lc( get_symbol($sym) );
  $edge = lc($edge);
  (defined $sym) or return 0;
  Xray::Absorption -> in_resource($sym) or return 0;
  return exists($$r_cl{$sym}{"energy_".$edge});
  ## worry about very low eneries
};


=over 4

=item C<cross_section>

Example:

   $xsec = Xray::Absorption -> cross_section($elem, $energy, $mode);

   @xsec = Xray::Absorption -> cross_section($elem, \@energy, $mode);

For this data resource, one call in list context is considerably
faster than repeated calls in scalar context.  It is well worth the
trouble of organizing your code to make a single call in list context
and store the results for later use.

This behaves slightly differently from the similar method for the
McMaster and Elam resources.  The CL tables are actually tables of
anomalous scattering factors and do not come with coherent and
incoherent scattering cross-sections.  The photo-electric
cross-section is calculated from the imaginary part of the anomalous
scattering by the formula

     mu = 2 * r_e * lambda * conv * f_2

where, C<r_e> is the classical electron radius, lamdba is the photon
wavelength, and conv is a units conversion factor.

     r_e    = 2.817938 x 10^-15 m
     lambda = 2 pi hbar c / energy
     hbar*c = 1973.27053324 eV*Angstrom
     conv   = Avagadro / atomic weight
            = 6.022045e7 / weight in cgs

The C<$mode> argument is different here than for the other resources.
The options are "xsec", "f1", and "f2", telling this method to return
the cross-section or the real or imaginary anomalous scattering
factor, respectively.

The values for f1 and f2 are computed by linear interpolation of a
semi-log scale.  Care is taken to avoid the discontinuities at the
edges.

Because the CL tables do not include the coherent and incoherent
scattering terms, the value returned by C<get_energy> is a bit smaller
using the CL tables than using the others.

=back

=cut

sub cross_section {
  shift;
  die "cross_section takes a single energy or a reference to an array\n" if
    ($#_ > 2);
  my ($sym, $energy, $mode) = @_;
  ##print wantarray ? "array context\n" : "scalar context\n";
  $sym = lc( get_symbol($sym) );
  my $z = get_Z($sym);
  Xray::Absorption -> in_resource($sym) || return 0;
  (defined $sym) || return 0;
  ## cache this hash element
  my $hash_element = $$r_cl{$sym};
##   my $n = $#{$$r_cl{$sym}{energy}};
##   if (($energy < $$r_cl{$sym}{energy}->[0]) or
##       ($energy > $$r_cl{$sym}{energy}->[$n]) ) {
##     my $message = sprintf
##       "The CL Tables for element %s are only valid " .
## 	"between %7.4f and %7.1f eV%s",
## 	ucfirst($sym), $$r_cl{$sym}{energy}->[0],
## 	$$r_cl{$sym}{energy}->[$n], $/;
##     $Xray::Absorption::verbose and warn $message;
##     return 0;
##   };
  ## watch out for an input energy that is right at the edge
 EDGE_CHECK: foreach my $edge ("k" , "l1", "l2", "l3",
			       "m1", "m2", "m3", "m4", "m5",
			       "n1", "n2", "n3", "n4", "n5", "n6", "n7",
			       "o1", "o2", "o3", "o4", "o5", "o6", "o7",
			       "p1", "p2", "p3") {
    ##  define the dge to be 10 meV above the edge
    if ((exists $$hash_element{"energy_".$edge}) and
	(abs($energy - $$hash_element{"energy_".$edge}) < $epsilon)) {
      ($energy += 10*$epsilon);
      last EDGE_CHECK;
    };
  };

  ## fetch Cromer-Liberman values from Ifeffit.  Ifeffit requires that
  ## arrays be 2 or more elements long, so tack on a throw-away value
  my @ener;
  if (wantarray) {
    @ener = @$energy;
  } else {
    @ener = ($energy, $energy+$epsilon/10);
  };
  put_array("absorption_cl.energy", \@ener);
  ifeffit("f1f2(z=$z, energy=absorption_cl.energy)\n");
  my @f1 = get_array("absorption_cl.f1");
  my @f2 = get_array("absorption_cl.f2");
  #print join(" ", @f1), $/;
  #print join(" ", @f2), $/;

  $mode ||= "xsec";
  ($mode =~ /\b(x|f[12])/i) or $mode = "xsec";

  if ($mode =~ /\b(f2|x)/i) {
    ##my $fpp = cl_f2($z, $energy);  # using old CLdata
    if ($mode =~ /\bf2/) {
      return wantarray ? @f2 : $f2[0];
    } else {

      my $factor  = Xray::Absorption -> get_conversion($sym);
      my $weight  = Xray::Absorption -> get_atomic_weight($sym);
      my @mu;
      foreach my $i (0 .. $#ener) {
	my $lambda  = 2 * PI * HBARC / $ener[$i];
	$mu[$i] = 2*RE * $lambda * $f2[$i] * 0.6022045 * 1e8 * $factor / $weight;
      }; #                                                ^       ^
      ##                                                  |       |
      ##                              avagadro's / barn __|       |
      ##                                        angstroms -> cm __|
      ($mode =~ /\bx/) and return wantarray ? @mu : $mu[0];
      #($mode =~ /\bx/) and return $mu;
    };
  } elsif ($mode =~ /\bf1/i) {
    ##my $fp = cl_f1($z, $energy);  # using old CLdata
    return wantarray ? @f1 : $f1[0];
  };

  return 0; ## it should never get here!

};





1;

__END__


=head1 EDGE AND LINE ENERGIES

The CL data resource provides a fairly complete set of edge energies.
Any edge tabulated on the Gwyn William's Table of Electron Binding
Energies for the Elements (that's the one published by NSLS and on the
door of just about every hutch at NSLS) is in the CL data resource.
The CL data comes with the same, limited set of fluorescence energies
as McMaster.

=head1 BUGS AND THINGS TO DO

None that I know about...

=head1 AUTHOR

  Bruce Ravel, bruce@phys.washington.edu
  http://feff.phys.washington.edu/~ravel/software/Absorption

=cut