Debian Codegraph @

attic/scripts/dafs.pl

raw
1 #!/usr/bin/perl -w
2 my $cvs_info = '$Id: dafs.pl,v 1.5 2001/09/20 17:26:41 bruce Exp $ ';
3 ## Time-stamp: <14 December, 2005>
4 ######################################################################
5 ## DAFS for Atoms version 3.0.1 copyright (c) 1998 Bruce Ravel
6 ## [email protected]
7 ## http://feff.phys.washington.edu/~ravel/
8 ##
9 ## The latest version of Atoms can always be found at
10 ## http://feff.phys.washington.edu/~ravel/software/atoms/
11 ##
12 ## -------------------------------------------------------------------
13 ## All rights reserved. This program is free software; you can
14 ## redistribute it and/or modify it under the same terms as Perl
15 ## itself.
16 ##
17 ## This program is distributed in the hope that it will be useful,
18 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ## Artistic License for more details.
21 ## -------------------------------------------------------------------
22 ######################################################################
23 ## This file is the main program for DAFS (command line).
24 ##
25 ## This version of atoms is intended for running from the command
26 ## line. It requires an input file identical to that used by the
27 ## command line version of Atoms, but with a few additional keywords.
28 ## Also the occupancy column in the input file is observed by this
29 ## program, whereas it is ignored by Atoms.
30 ######################################################################
31 ## Code:
32
33
34 ## =============================== load methods
35 require 5.004;
36 use strict;
37 use Carp;
38 use IO::File;
39 use Xray::Xtal;
40 $Xray::Xtal::run_level = 0;
41 use Xray::Atoms qw(build_cluster rcfile_name);
42 use Xray::ATP;
43 use File::Basename;
44 use File::Spec;
45 use Xray::Absorption;
46 use Xray::Scattering;
47 use POSIX qw/atan/;
48 ## use Math::Complex;
49 $| = 1;
50 use constant PI => 3.14159265358979323844;
51 use constant RE => 0.00002817938;
52 use constant HBARC => 1973.27053324;
53
54 ## =============================== process command line switches
55 use Getopt::Std;
56 use vars qw(%opt);
57 ($opt{o}, $opt{r}, $opt{d}, $opt{T}) = ("", 0, "", 0);
58 getopts('ADvsqho:t:r:d:n:x:p:T:', \%opt);
59 $opt{s} and $opt{q} = 1;
60
61 my $matchstring = join("|", Xray::Absorption -> scattering);
62 if ($opt{d} =~ /\b($matchstring)\b/i) {
63 Xray::Absorption -> load($opt{d});
64 } else {
65 my %meta = Xray::Atoms -> rcvalues();
66 Xray::Absorption -> load($meta{dafs_default});
67 };
68 die $$Xray::Atoms::messages{'require_fp'} unless
69 (Xray::Absorption -> current_resource =~ /\b(Henke|Chantler|CL|Sasaki)\b/i);
70
71 my $file;
72 if ($opt{A}) {
73 my $inp = $ARGV[0];
74 unless ($inp =~ /\.inp$/) {
75 $inp .= ($inp =~ /\.$/) ? 'inp' : '.inp';
76 };
77 $file = $Xray::Atoms::meta{ADB_location} . $inp;
78 } else {
79 unless ($^O eq 'MacOS') {
80 INPUT: {
81 $file = "atoms.inp", last INPUT if (not $ARGV[0]);
82 ##$file = "dafs.inp", last INPUT if (-e $file);
83 $file = $ARGV[0], last INPUT if (-e $ARGV[0]);
84 $file = $ARGV[0] . ".inp", last INPUT if (-e "$ARGV[0].inp");
85 $file = $ARGV[0] . "inp", last INPUT if (-e "$ARGV[0]inp");
86 die $ARGV[0] . ": " . $$Xray::Atoms::messages{'invalid_input'} . $/;
87 };
88 } else {
89 require Mac::StandardFile;
90 $file = Mac::StandardFile::StandardGetFile(0, 'TEXTclpt');
91 if ($file -> sfGood()) {
92 $file = $file->sfFile();
93 } else {
94 die "File opening canceled. Atoms quitting.$/";
95 };
96 };
97 };
98 my $inputdir = dirname($file); # this may not work as intended...
99
100
101 ## =============================== run time screen messages
102 my $v = (split(" ", $cvs_info))[2] || "pre_release";
103 my $date = (split(" ", $cvs_info))[3] || "";
104 my $atoms_version = "3.0.1";
105 my $screen_line = "=" x 71;
106
107 if ($opt{h}) {
108 require Pod::Text;
109 $^W=0;
110 if ($Pod::Text::VERSION < 2.0) {
111 Pod::Text::pod2text($0, *STDOUT);
112 } elsif ($Pod::Text::VERSION >= 2.0) {
113 my $parser = Pod::Text->new;
114 open STDIN, $0;
115 $parser->parse_from_filehandle;
116 };
117 exit;
118 };
119
120 print STDOUT <<EOH
121 $screen_line
122 DAFS $v for Atoms $atoms_version ($^O) $date
123 $screen_line
124 EOH
125 unless ($opt{q});
126
127 my $absorption_resource = Xray::Absorption -> current_resource;
128 print STDOUT <<EOH
129 by Bruce Ravel, copyright (c) 1999
130 <ravel\@phys.washington.edu>
131
132 using Atoms.pm $Xray::Atoms::module_version
133 Xtal.pm $Xray::Xtal::VERSION with space groups database $Xray::Xtal::sg_version
134 Absorption.pm $Xray::Absorption::cvs_version
135 $absorption_resource
136
137 EOH
138 if ($opt{v});
139 exit if ($opt{v});
140
141
142 ## =============================== define a cell, parse the input file,
143 my @sites = (); # list of sites
144 my $cell = Xray::Xtal::Cell -> new();
145 my $keywords = Xray::Atoms -> new();
146 $keywords->make('program'=>'dafs', dafs=>0);
147 $opt{q} && $keywords->make('quiet'=>1);
148 $keywords->make('identity'=>"DAFS $v", 'emin'=>0, 'emax'=>0, 'estep'=>0);
149 $keywords -> parse_input($file);
150 die "\n" if $keywords->{cli_warn};
151 exists($keywords->{thickness}) or $keywords->{thickness} = 1;
152
153 ## =============================== fill up the cell and the sites
154 $cell -> make( Space_group=>$keywords->{'space'} );
155 foreach my $param ('a', 'b', 'c', 'alpha', 'beta', 'gamma') {
156 $cell -> make( $param=>$keywords->{$param} );
157 };
158 ( @{$keywords->{'sites'}} ) or
159 croak "$$Xray::Atoms::messages{no_sites} $file\n";
160
161 my $nsites = 0;
162 foreach my $this (@{$keywords->{'sites'}}) {
163 $sites[$nsites] = Xray::Xtal::Site -> new();
164 $sites[$nsites] -> make(Element=>$$this[0],
165 X=>$$this[1]+${$keywords->{"shift"}}[0],
166 Y=>$$this[2]+${$keywords->{"shift"}}[1],
167 Z=>$$this[3]+${$keywords->{"shift"}}[2] );
168 ($$this[4]) && ( $sites[$nsites] -> make(Tag=>$$this[4]) );
169 ($$this[5]) && ( $sites[$nsites] -> make(Occupancy=>$$this[5]) );
170 ++$nsites;
171 };
172
173 ## =============================== error check, populate the cell
174 $cell -> verify_cell();
175 $cell -> populate(\@sites);
176 $keywords -> verify_keywords($cell, \@sites, 0);
177
178 ## =============================== set bounds of calculation
179 ($opt{n}) and $keywords->make('emin' => $opt{n});
180 ($opt{x}) and $keywords->make('emax' => $opt{x});
181 ($opt{p}) and $keywords->make('estep' => $opt{p});
182 ($opt{T}) and $keywords->make('thickness' => $opt{T});
183 my ($central, $xc, $yc, $zc) = $cell -> central($keywords->{'core'});
184 my $edge = $keywords->{'edge'};
185 my $e0 = Xray::Absorption -> get_energy($central, $edge);
186 my $emin = $keywords->{'emin'} || 300;
187 my $emax = $keywords->{'emax'} || 500;
188 ($emin, $emax) = ($e0-$emin, $e0+$emax);
189 my $estep = $keywords->{'estep'} || 15;
190 ($estep <= 0) and $estep = 15; # some error checking
191 ($emin > $emax) and ($emin, $emax) = ($emax, $emin);
192 $keywords -> make('emin'=>$emin, 'emax'=>$emax, 'estep'=>$estep);
193 my $e = $emin;
194
195 ## =============================== set reflection
196 my @reflection;
197 if (length($opt{r}) == 3) {
198 @reflection = map {sprintf "%d", $_} split("", $opt{r});
199 $keywords->make("qvec"=>split("", $opt{r}));
200 } else {
201 die $$Xray::Atoms::messages{'no_reflection'} . $/
202 unless (defined @{$keywords->{'qvec'}} and @{$keywords->{'qvec'}});
203 @reflection = @{$keywords->{'qvec'}};
204 };
205 my $d = $cell -> d_spacing(@reflection);
206
207 ## =============================== cache the f0 and phase values
208 my ($contents) = $cell -> attributes("contents");
209 my %fnot;
210 my @phase;
211 my $counter = 0;
212 foreach my $s (@{$contents}) {
213 my ($e, $v) = $ {$$s[3]} -> attributes('Element', 'Valence');
214 my $sym = Xray::Scattering->get_valence($e, $v);
215 $ {$$s[3]} -> make('CromerMann'=>$sym);
216 $fnot{$sym} = Xray::Scattering->get_f($sym, $d);
217 $phase[$counter] = $$s[0] * $reflection[0] + $$s[1] * $reflection[1] +
218 $$s[2] * $reflection[2];
219 $phase[$counter] *= 2*PI;
220 ++$counter;
221 };
222
223 ## =============================== dump...
224 if ($opt{D}) {
225 use Data::Dumper;
226 print Data::Dumper->Dump([$keywords, $cell, \%fnot],
227 [qw/*keywords *cell *fnot/]);
228 exit;
229 };
230
231 ## =============================== the big energy loop
232 ($keywords->{"quiet"}) or
233 print STDOUT " ", $$Xray::Atoms::messages{'calculating_dafs'}, " ...", $/;
234 my (%fp, %fpp, $ampsqr, $r, $i, $la, %mu, $mutot, @calculation);
235
236 my $lambda = 2*PI*HBARC / $e;
237 my $dsp = $cell -> d_spacing(@reflection);
238 my $sinthnot = $lambda / (2 * $cell -> d_spacing(@reflection));
239 my $munot = Xray::Atoms::xsec($cell, $central, $e);
240 $munot *= 10e-8; # undef $dens;
241 my $absnot = (1 - exp(-2*$keywords->{thickness}*$munot/$sinthnot)) / (2*$munot);
242 my $sinth;
243
244 ## ------------------------------------------------------------------------
245 ## cache all anomalous scattering values for this calculation
246 foreach my $s (@{$contents}) {
247 my ($el) = $ {$$s[3]} -> attributes('Element');
248 next if exists $fp{$el};
249 $fp{$el} = [];
250 };
251 my $ee = $e;
252 my @energies = ();
253 while ($ee < $emax) {
254 push @energies, $ee;
255 $ee += $estep;
256 };
257 my $foo = 0;
258 foreach my $l (keys %fp) {
259 @{$fp{$l}} = Xray::Absorption -> cross_section($l, \@energies, 'f1');
260 @{$fpp{$l}} = Xray::Absorption -> cross_section($l, \@energies, 'f2');
261 my $factor = Xray::Absorption -> get_conversion($l);
262 my $weight = Xray::Absorption -> get_atomic_weight($l);
263 foreach my $i (0 .. $#energies) {
264 my $lambda = 2 * PI * HBARC / $energies[$i];
265 ${$mu{$l}}[$i] = 2*RE * $lambda * ${$fpp{$l}}[$i] *
266 0.6022045 * 1e8 * $factor / $weight;
267 };
268 ##@{$mu{$_}} = Xray::Absorption -> cross_section($_, \@energies, 'xsec');
269 };
270 my @total = Xray::Atoms::xsec($cell, $central, \@energies);
271 ## ------------------------------------------------------------------------
272
273 my $pt = 0;
274 while ($e < $emax) {
275 ($r, $i, $la) = (0,0,0); ## clear these at each energy
276 $mutot = 0;
277 $counter = 0;
278 $lambda = 2*PI*HBARC / $e;
279 $sinth = $lambda / (2 * $dsp);
280 foreach my $s (@{$contents}) {
281 my ($el, $sym, $occ, $id) =
282 $ {$$s[3]} -> attributes('Element', 'CromerMann', 'Occupancy', 'Id');
283 my $phase = $phase[$counter];
284 my $fone = $fnot{$sym} + $ {$fp{$el}}[$pt];
285 my $ftwo = $ {$fpp{$el}}[$pt];
286 ## do the complex arithmatic by hand
287 $r += $occ * ($fone * cos($phase) - $ftwo * sin($phase));
288 $i += $occ * ($fone * sin($phase) + $ftwo * cos($phase));
289 $mutot += $occ * $ {$mu{$el}}[$pt];
290 ++$counter;
291 };
292 ## Lorentz and absoprtion correction
293 $total[$pt] *= 10e-8;
294 $la = (1 - exp(-2*$keywords->{thickness}*$total[$pt]/$sinth)) / (2*$total[$pt]);
295 $la /= $absnot;
296 $la *= ($emin**3 * $sinthnot) / ($e**3 * $sinth);
297 my $this_energy = [$e, $r, $i, $la];
298 push @calculation, $this_energy;
299 $e += $estep; # increment energy
300 ++$pt;
301 };
302
303 ## =============================== open the output file and make the file header
304 my ($fh, $atp, $outfile);
305 if (defined %{$keywords->{'atp'}}) {
306 LOOP: while (($atp, $outfile) = each(%{$keywords->{'files'}})) {
307 last LOOP; # just the first (should only be one)
308 }; # there must be a better way!
309 } else {
310 $atp = 'dafs';
311 $outfile = sprintf("dafs_%d%d%d.dat", @reflection);
312 };
313 ($opt{t}) and $atp = $opt{t};
314 if ($opt{s}) {
315 $fh = *STDOUT;
316 $outfile = "to standard output";
317 } else {
318 ($opt{o}) and $outfile = $opt{o};
319 unless ($keywords->{'write_to_pwd'}) {
320 $outfile = File::Spec -> catfile($inputdir, $outfile);
321 };
322 $fh = IO::File->new();
323 open $fh, ">".$outfile
324 or die $$Xray::Atoms::messages{cannot_write} . $outfile . $/;
325 }
326
327 my $data = "";
328 my @neutral;
329 my ($default_name, $is_feff) =
330 &parse_atp($atp, $cell, $keywords, \@calculation, \@neutral, \$data);
331 print $fh $data;
332 ($opt{s}) or close $fh;
333
334 ## =============================== finish up
335 ($keywords->{"quiet"}) or
336 print STDOUT " ", $atp, ": ", $$Xray::Atoms::messages{'writing'},
337 " ", $outfile, $/;
338 ($keywords->{"quiet"}) or print STDOUT $screen_line, $/, $/;
339 1;
340
341 ######################################################################
342 ## End of main program atoms
343
344 =head1 NAME
345
346 DAFS - Simulate energy dependent scattering factors
347
348 =head1 SYNOPSIS
349
350 dafs -r hkl [-t file] [-o file] [-d data_resource]
351 [-n #] [-x #] [-p #] [-sqvh] <input_file>
352
353 =head1 DESCRIPTION
354
355 Take crystallographic data from the input file given on the command
356 line and write a file containing the energy dependent scattering
357 factor for the crystal. Tables of f' and f" are used along with
358 tables of the Thomson scattering. The input file for this program is
359 almost identical to Atoms' input file. See the Atoms documentation
360 for a complete description of the input file.
361
362 The keywords "emin", "emax", "estep", "qvec", and "feout" are
363 recognized by DAFS and ignored by Atoms. If no input file is given at
364 the command line, F<atoms.inp> is used. Several command line switches
365 can be used to override the contents of the input files.
366
367 command line switches:
368 -r ### hkl of a reflection (only hkl between 0 and 9)
369 -T ### sample thickness in Angstroms
370 -t s atp file to use for the output (default is 'dafs')
371 -o s output file name (default is 'dafs_hkl.dat')
372 -d s name of absorption data resource (default is Henke)
373 -n # lower bound of energy relative to edge (eV)
374 -x # upper bound of energy relative to edge (eV)
375 -p # energy step (eV)
376 -A use a named file from the Atoms Database
377 -s write calculation to standard output
378 -q suppress all screen messages
379 -v write version information and exit
380 -h write this help message and exit
381
382 # = number s = string
383
384 For complete information about DAFS, consult the documentation.
385
386 =head1 AUTHOR
387
388 Bruce Ravel, [email protected]
389 http://feff.phys.washington.edu/~ravel
390
391 =cut