Debian Codegraph @

attic/scripts/Tk/Dafs.pm

raw
1 #!/usr/bin/perl -w
2 ######################################################################
3 ## DAFS notecard module for Atoms 3.0beta9
4 ## copyright (c) 1999 Bruce Ravel
5 ## [email protected]
6 ## http://feff.phys.washington.edu/~ravel/
7 ##
8 ## The latest version of Atoms can always be found at
9 ## http://feff.phys.washington.edu/~ravel/atoms/
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 package Xray::Tk::Dafs;
24
25 use strict;
26 use vars qw($VERSION $cvs_info @ISA @EXPORT @EXPORT_OK);
27
28 require Exporter;
29
30 @ISA = qw(Exporter AutoLoader Xray::Tk::Utils);
31 @EXPORT_OK = qw(dafs);
32 $cvs_info = '$Id: Dafs.pm,v 1.8 2001/09/22 00:54:23 bruce Exp $ ';
33 $VERSION = (split(' ', $cvs_info))[2] || 'pre_release';
34
35 require Tk;
36 require Xray::Atoms;
37 use Xray::ATP;
38 use Xray::Scattering;
39 ($::ifeffit_exists) && require Xray::Tk::Plotter;
40 use Xray::Tk::Utils;
41 #use Ifeffit qw(ifeffit);
42 use constant PI => 3.14159265358979323844;
43 use constant RE => 0.00002817938;
44 use constant HBARC => 1973.27053324;
45 use File::Basename qw(dirname);
46 my $is_windows = (($^O eq 'MSWin32') or ($^O eq 'cygwin'));
47
48
49 ### >> dafs control panel
50
51 ######################################################################
52 ######### ########################
53 ######### Notecard containing DAFS keywords ########################
54 ######### ########################
55 ######################################################################
56
57 my %dafs_values = ('h' => 0, # miller indeces
58 'k' => 0, #
59 'l' => 0, #
60 'emin' => 300, # energy grid
61 'emax' => 500, #
62 'estep' => 15, #
63 'table' => 'cl', # default data resource
64 ##'edge' => '', # initial edge symbol
65 'progress' => 152, # y size of progress meter
66 'canvas' => 282, # x size of plot canvas
67 'npoints' => 12, # number of points in calc.
68 'atp' => 'dafs',
69 'thickness' => 1,
70 'plot_group' => '',
71 );
72 ## widgets:
73 my ($dafs_progress, $dafs_done, $dafs_todo, $dafs_plot, $dafs_data,
74 @dafs_tics, $dafs_title, %dafs_buttons, @dafs_calculation);
75 use vars qw(@dafs_x @dafs_y);
76 @dafs_x = (); @dafs_y = ();
77 ## array for passing to parse_atp
78 my ($dafs_frame, $menubar);
79 my $dafs_running = 0;
80
81 ##my $up_img = File::Spec->catfile($::xtal_dir, 'up.xbm');
82 ##my $dn_img = File::Spec->catfile($::xtal_dir, 'dn.xbm');
83
84
85 sub dafs {
86 ##$dafs_frame = $_[0] -> Frame() -> pack(-fill=>'x');
87 $dafs_frame = $::pages{DAFS} -> Frame() -> pack(-fill=>'x');
88
89 ## ---- menubar -----------
90 $menubar = $dafs_frame -> Frame(-borderwidth=>2, -relief=>'ridge',)
91 -> pack(-anchor=>'nw', -fill=>'x', -pady=>4, -padx=>4);
92
93 my $file_menu = $menubar ->
94 Menubutton(@::file_menubutton) -> pack(-side=>'left');
95 &::manage($file_menu, "menu");
96 my $this = $file_menu
97 -> command(-label=>$$::labels{load_input}, @::menu_args,
98 -command=>\&::load_input,
99 -accelerator=>'Control+o', );
100 &::manage($this, "menu");
101 ($::LWP_simple_exists) and do {
102 $this = $file_menu
103 -> command(-label=>$$::labels{load_adb}, @::menu_args,
104 -command=>\&::load_adb, );
105 &::manage($this, "menu");
106 $this = $file_menu
107 -> command(-label=>$$::labels{download_adb}, @::menu_args,
108 -command=>\&::download_adb, );
109 &::manage($this, "menu");
110 };
111 my $recent = $file_menu ->
112 cascade(-label=>"Recent files", @::menu_args, -tearoff=>0,);
113 &::manage($recent, "menu");
114 push @::recent_registry, $recent;
115 foreach my $f (@::recent_files) {
116 $recent -> command(-label=>$f, -command=>sub{&::load_input($f, 1)});
117 };
118
119 my $sep = $file_menu -> separator();
120 &::manage($sep, "separator");
121 $this = $file_menu
122 -> command(-label=>$$::labels{save_input}, @::menu_args,
123 -command=>\&::save_input,
124 -accelerator=>'Control+s', );
125 &::manage($this, "menu");
126 $this = $file_menu ->
127 command(-label=>$$::labels{save_dafs_data}, @::menu_args,
128 -command=>[\&save_dafs, 'dat'],);
129 &::manage($this, "menu");
130 unless ($::ifeffit_exists) {
131 $this = $file_menu ->
132 command(-label=>$$::labels{save_dafs_ps}, @::menu_args,
133 -command=>[\&save_dafs, 'ps'],);
134 &::manage($this, "menu");
135 };
136 ## $sep = $file_menu -> separator();
137 ## &::manage($sep, "separator");
138 ## $this = $file_menu -> command(@::apt_args);
139 ## &::manage($this, "menu");
140 $sep = $file_menu -> separator();
141 &::manage($sep, "separator");
142 $this = $file_menu
143 -> command(-label=>$$::labels{quit}, @::menu_args,
144 -command=>\&::quit_tkatoms,
145 -accelerator=>'Control+q', );
146 &::manage($this, "menu");
147
148
149 my $clear_menu = $menubar ->
150 Menubutton(-text=>$$::labels{clear_menu}, @::menu_args) ->
151 pack(-side=>'left');
152 &::manage($clear_menu, "menu");
153 $this = $clear_menu ->
154 command(-label=>$$::labels{clear_dafs}, @::menu_args,
155 -command=>\&clear_dafs);
156 &::manage($this, "menu");
157 $this = $clear_menu ->
158 command(-label=>$$::labels{clear_lattice}, @::menu_args,
159 -command=>\&::clear_lattice);
160 &::manage($this, "menu");
161 $this = $clear_menu ->
162 command(-label=>$$::labels{clear_all}, @::menu_args,
163 -command=>sub{&clear_dafs; &::clear_lattice});
164 &::manage($this, "menu");
165
166 if ($::ifeffit_exists) {
167 my $plot_menu = $menubar ->
168 Menubutton(-text=>$$::labels{plot_menu}, @::menu_args) ->
169 pack(-side=>'left');
170 &::manage($plot_menu, "menu");
171 $this = $plot_menu ->
172 command(-label=>$$::labels{newplot}, @::menu_args,
173 -command=>\&newplot,);
174 &::manage($this, "menu");
175 $this = $plot_menu ->
176 command(-label=>$$::labels{overplot}, @::menu_args,
177 -command=>\&overplot,);
178 &::manage($this, "menu");
179 $this = $plot_menu ->
180 command(-label=>$$::labels{plotgif}, @::menu_args,
181 -command=>[\&saveplot, 'gif', 'dafs'],);
182 &::manage($this, "menu");
183 $this = $plot_menu ->
184 command(-label=>$$::labels{plotps}, @::menu_args,
185 -command=>[\&saveplot, 'ps', 'dafs'],);
186 &::manage($this, "menu");
187 };
188
189 my $help_menu = $menubar ->
190 Menubutton(@::help_menubutton) -> pack(-side=>'right');
191 &::set_help_menu($help_menu);
192
193 my $pref_menu = $menubar ->
194 Menubutton(@::pref_menubutton) -> pack(-side=>'right');
195 &::set_pref_menu($pref_menu);
196
197 &::manage($help_menu, "menu");
198 &::manage($pref_menu, "menu");
199
200
201 ## Descripton
202 my $top_frame = $dafs_frame -> Frame()
203 -> pack();
204 my $label_frame = $top_frame -> Frame(-relief=>'groove', -borderwidth=>2)
205 -> pack(-pady=>0, -side=>'left');
206 my $dafs_label = $label_frame ->
207 Label(-text=>$$::labels{'dafs_description'}, @::header_args, ) ->
208 pack();
209 &::manage($dafs_label, "header");
210
211
212 my $dframe = $dafs_frame -> Frame() -> pack(-fill=>'x', -pady=>2);
213
214
215 ## ---- frame with reflection widgets -------------------------
216 my $dfleft = $dframe -> Frame()
217 -> pack(-side=>'left', -padx=>0, -pady=>0, -anchor=>'n');
218
219 my $dfhkl = $dfleft -> LabFrame(-label=>$$::labels{reflection},
220 -foreground=>$::colors{label},
221 -labelside=>'acrosstop',
222 -borderwidth=>2,)
223 -> pack(-side=>'top', -pady=>2, -padx=>2);
224 $::balloon->attach(($dfhkl->children)[1], -msg=>$$::help{reflection},);
225 &::manage($dfhkl, "labframe");
226
227
228 my $up_bitmap = "#define up_width 14
229 #define up_height 13
230 static unsigned char up_bits[] = {
231 0xc0, 0x00, 0xc0, 0x00, 0xe0, 0x01, 0xe0, 0x01, 0xf0, 0x03, 0xf0, 0x03,
232 0xf8, 0x07, 0xf8, 0x07, 0xfc, 0x0f, 0xfc, 0x0f, 0xfe, 0x1f, 0xfe, 0x1f,
233 0x00, 0x00};";
234 my $up_img = $dfhkl -> Bitmap('up', -data=>$up_bitmap,
235 -foreground=>$::colors{entry});
236 my $dn_bitmap = "#define dn_width 14
237 #define dn_height 13
238 static unsigned char dn_bits[] = {
239 0xfe, 0x1f, 0xfe, 0x1f, 0xfc, 0x0f, 0xfc, 0x0f, 0xf8, 0x07, 0xf8, 0x07,
240 0xf0, 0x03, 0xf0, 0x03, 0xe0, 0x01, 0xe0, 0x01, 0xc0, 0x00, 0xc0, 0x00,
241 0x00, 0x00};";
242 my $dn_img = $dfhkl -> Bitmap('dn', -data=>$dn_bitmap,
243 -foreground=>$::colors{entry});
244
245 my $dcol = 0;
246 foreach my $m ('h', 'k', 'l') {
247 $this = $dfhkl -> Label(-text=>$m, -font=>$::fonts{header},
248 -foreground=>$::colors{label})
249 -> grid(-column=>$dcol, -row=>1);
250 &::manage($this, "label");
251 my $up = $dfhkl -> Button(#-text=>"^",
252 @::button_args,
253 -command=>sub{++$dafs_values{$m}},
254 -image => $up_img,
255 )
256 -> grid(-column=>$dcol, -row=>2);
257 $::balloon -> attach($up, -msg=>$$::help{reflection_up});
258 &::manage($up, "button");
259 $this = $dfhkl -> Entry(-textvariable=>\$dafs_values{$m}, @::entry_args,
260 -width=>3, -justify=>'center', -relief=>'sunken')
261 -> grid(-column=>$dcol, -row=>3);
262 &::manage($this, "entry");
263 my $dn = $dfhkl -> Button(#-text=>"v",
264 @::button_args,
265 -command=>sub{--$dafs_values{$m}},
266 -image => $dn_img,
267 )
268 -> grid(-column=>$dcol, -row=>4);
269 $::balloon -> attach($dn, -msg=>$$::help{reflection_dn});
270 &::manage($dn, "button");
271 ++$dcol;
272 };
273
274
275
276 my $atp_frame = $dfleft -> Frame() -> pack(-side=>'bottom', -pady=>1);
277 my $atp_button = $atp_frame
278 -> Optionmenu(-textvariable => \$dafs_values{'atp'},
279 -background => $::colors{'entry'},
280 -activeforeground => $::colors{'label'},
281 -activebackground => $::colors{'entry'},
282 -width=>6, -font=>$::fonts{'label'}, -relief=>'groove')
283 -> pack(-side=>'right');
284 &::manage($atp_button, "menu");
285 my $atp_label = $atp_frame ->
286 Label(-text=>$$::labels{outfiles}, @::label_args)
287 -> pack(-side=>'left');
288 $::balloon->attach($atp_label, -msg=>$$::help{'output_files'},);
289 &::manage($atp_label, "label");
290
291 foreach my $e (@::atpfiles) {
292 if ($e =~ /dafs/i) {
293 $this = $atp_button ->
294 command(-label => $e, @::menu_args,
295 -command=>sub{$dafs_values{'atp'}=$e;});
296 &::manage($this, "menu");
297 };
298 };
299
300
301 my $thickness_frame = $dfleft -> Frame() -> pack(-side=>'bottom', -pady=>1);
302 my $thickness_label = $thickness_frame ->
303 Label(-text=>$$::labels{thickness}, @::label_args)
304 -> pack(-side=>'left');
305 $::balloon->attach($thickness_label, -msg=>$$::help{'thickness'},);
306 &::manage($thickness_label, "label");
307 my $thickness_entry = $thickness_frame ->
308 Entry(-textvariable=>\$dafs_values{thickness}, @::entry_args,
309 -width=>6, -relief=>'sunken')
310 -> pack(-side=>'left');
311 &::manage($thickness_entry, "entry");
312 my $angstrom_label = $thickness_frame ->
313 Label(-text=>'A', @::label_args)
314 -> pack(-side=>'left');
315 &::manage($angstrom_label, "label");
316
317
318
319 ## -------------------------------------------------------------
320
321
322
323 ## ---- frame with energy grid and data resource widgets -------
324 my $dfright = $dframe -> Frame()
325 -> pack(-side=>'left', -padx=>0, -pady=>2, -anchor=>'n');
326
327
328 $dcol = 0;
329 my $dfenergy = $dfright -> LabFrame(-label=>$$::labels{energy_grid},
330 -foreground=>$::colors{label},
331 -labelside=>'acrosstop',
332 -borderwidth=>2)
333 -> pack(-side=>'top', -padx=>0, -anchor=>'n');
334 &::manage($dfenergy, "labframe");
335 foreach my $e ('emin', 'emax', 'estep') {
336 my $label = $dfenergy -> Label(-text=>$$::labels{$e}, @::label_args)
337 -> grid(-column=>$dcol, -row=>1);
338 $::balloon -> attach($label, -msg=>$$::help{$e});
339 &::manage($label, "label");
340 my $entry = $dfenergy ->
341 Entry(-textvariable=>\$dafs_values{$e}, @::entry_args,
342 -width=>6, -justify=>'center', -relief=>'sunken')
343 -> grid(-column=>$dcol, -row=>2);
344 &::manage($entry, "entry");
345 ++$dcol;
346 };
347 $::balloon->attach(($dfenergy->children)[1], -msg=>$$::help{energy_grid},);
348
349
350 my %dafs_resource_button;
351 my $dfresource = $dfright -> LabFrame(-label=>$$::labels{data_resources},
352 -foreground=>$::colors{label},
353 -labelside=>'acrosstop',
354 -borderwidth=>2)
355 -> pack(-side=>'top');
356 $::balloon->attach(($dfresource->children)[1], -msg=>$$::help{data_resources},);
357 &::manage($dfresource, "labframe");
358 my $dafs_resource_frame = $dfresource
359 -> Scrolled("Text", -scrollbars=>'e', -height=>5, -width=>13,
360 -relief=>'flat',)
361 -> pack();
362 $dafs_resource_frame->Subwidget("yscrollbar")->configure(-background=>$::colors{background});
363 foreach my $resource (sort(Xray::Absorption->available)) {
364 next if ($resource =~ /(elam|mcmaster|none)/i);
365 my $r = lc($resource);
366 $dafs_resource_button{lc($r)} = $dafs_resource_frame
367 -> Radiobutton(-text => $resource, @::menu_args,
368 -selectcolor=>$::colors{radio},
369 -variable=>\$::absorption_tables,
370 -value=>$r,
371 -justify=>'left',
372 -command=>sub{ Xray::Absorption->load($r);
373 $::absorption_tables = $r; });
374 &::manage($dafs_resource_button{lc($r)}, "radio");
375 $dafs_resource_frame
376 -> windowCreate('end', -window=>$dafs_resource_button{lc($r)});
377 $dafs_resource_frame -> insert('end', $/);
378 $::balloon->attach($dafs_resource_button{lc($r)}, -msg=>$$::help{lc($r)});
379 }
380 $dafs_resource_frame -> configure(-state=>'disabled');
381 ## -------------------------------------------------------------
382
383 my $dfrun = $dframe -> Frame()
384 -> pack(-side=>'left', -pady=>2, -anchor=>'n');
385
386 my $dfrun_buttons = $top_frame -> Frame()
387 -> pack(-side=>'right', -padx=>10, -pady=>0);
388 my $dfrun_canvases = $dfrun -> Frame()
389 -> pack(-side=>'bottom', -pady=>2);
390
391
392 ## put buttons above canvas
393 ## foreach my $s (qw/run_dafs save_dafs_data save_dafs_ps/) {
394 ## $dafs_buttons{'run_dafs'} =
395 do {
396 my $button = $dfrun_buttons
397 -> Button(-text=>$$::labels{'run_dafs'}, @::button_args, -borderwidth=>4,
398 -command=>\&run_dafs)
399 -> pack(-side=>'left', -padx=>2, -expand=>1 );
400 $::balloon -> attach($button, -msg=>$$::help{'run_dafs'});
401 &::manage($button, "button");
402 };
403
404 ## $dafs_progress = $dfrun_canvases
405 ## -> Canvas(-width=>22, -height=>$dafs_values{'progress'},
406 ## -background=>$::colors{'todo'})
407 ## -> pack(-side=>'left', -anchor=>'e');
408 ## &::manage($dafs_progress, "canvas");
409 ## $::balloon -> attach($dafs_progress, -msg=>$$::help{'progress_meter'});
410 ## $dafs_done = $dafs_progress ->
411 ## createRectangle(1, $dafs_values{'progress'}, 21, $dafs_values{'progress'}-1,
412 ## -fill=>$::colors{'done'}, -tags=>"done");
413 ## &::manage($dafs_done, "progress"); ## ??????
414 ## ##push @::all_progress, $dafs_progress, "done";
415 ## $dafs_todo = $dafs_progress ->
416 ## createRectangle(1,1,21, $dafs_values{'progress'}-1,
417 ## -fill=>$::colors{'todo'});
418
419
420 $dafs_plot = $dfrun_canvases
421 -> Canvas(-width=>$dafs_values{'canvas'},
422 -height=>$dafs_values{'progress'},
423 -background=>$::colors{'todo'}
424 )
425 -> pack(-side=>'right', -anchor=>'w');
426 &::manage($dafs_plot, "canvas");
427 ##if ((defined $::plotting_hook) and $::plotting_hook) {
428 $::balloon ->
429 attach($dafs_plot,
430 -msg=>$$::help{dafs_plot} . $/ . $$::help{plot_bindings});
431 ##} else {
432 ## $::balloon -> attach($dafs_plot, -msg=>$$::help{dafs_plot});
433 ##};
434 $dafs_plot -> Tk::bind('<Button-1>', \&newplot);
435 $dafs_plot -> Tk::bind('<Button-3>', \&overplot);
436 $dafs_plot ->
437 createRectangle(1,1,$dafs_values{'canvas'}, $dafs_values{'progress'},);
438 # -fill=>$::colors{'todo'});
439 $dafs_plot ->
440 createLine(36, 11, 36, $dafs_values{'progress'}-21);
441 $dafs_plot ->
442 createLine(36, $dafs_values{'progress'}-21, $dafs_values{'canvas'}-11,
443 $dafs_values{'progress'}-21);
444 $dafs_plot ->
445 createText($dafs_values{'canvas'}/2, $dafs_values{'progress'}-5,
446 -anchor=>'center', -font=>'Arial 10 normal',
447 -text=>$$::labels{energy_axis});
448 $dafs_plot -> createText(qw/40 10 -anchor w -font/, 'Arial 10 normal',
449 -text=>$$::labels{intensity_axis});
450
451 foreach my $i (0.25, 0.5, 0.75, 1) {
452 tkatoms_make_tic(\$dafs_plot, $dafs_values{'canvas'},
453 $dafs_values{'progress'}, 'x', $i, 5);
454 tkatoms_make_tic(\$dafs_plot, $dafs_values{'canvas'},
455 $dafs_values{'progress'}, 'y', $i, 5);
456 };
457
458
459 };
460
461
462 sub newplot {
463 require Xray::Tk::Plotter;
464 my $key = join(" ", (map {sprintf "%d", $_} @{$::keywords->{'qvec'}}));
465 &Xray::Tk::Plotter::plot_with_Ifeffit(\@dafs_x, \@dafs_y, 1, 'dafs',
466 \$dafs_values{plot_group}, $key);
467 };
468 sub overplot {
469 require Xray::Tk::Plotter;
470 my $key = join(" ", (map {sprintf "%d", $_} @{$::keywords->{'qvec'}}));
471 &Xray::Tk::Plotter::plot_with_Ifeffit(\@dafs_x, \@dafs_y, 0, 'dafs',
472 \$dafs_values{plot_group}, $key);
473 };
474
475 ### > dafs subroutines
476 ## --------------------------------------------------------------------
477
478 ## $dafs_running is an imperfect way of preventing problems from
479 ## double clicking the run dafs button
480 sub run_dafs {
481 ($dafs_running) and return;
482 $dafs_running = 1;
483 @dafs_calculation = ();
484 $::cell -> make( Occupancy=>1 );
485 undef $::keywords;
486 $::keywords = Xray::Atoms -> new();
487 # read the lattice and core data
488 $dafs_running = 0;
489 &::validate_lattice(0);
490 $dafs_running = 1;
491 my $core_tag = $::site_entries[$::core_index]{'tag'}->get();
492 ($core_tag =~ /^\s*$/) and
493 $core_tag = $::site_entries[$::core_index]{'elem'}->get();
494 $::keywords -> make('core'=>$core_tag);
495 # get reflection
496 my ($h, $k, $l) = map {int} ($dafs_values{'h'}, $dafs_values{'k'},
497 $dafs_values{'l'});
498 $::keywords -> make('thickness'=>$dafs_values{'thickness'});
499 unless ($h or $k or $l) {
500 $dafs_running = 0;
501 die $$::messages{'no_reflection'}.$/;
502 };
503 $::keywords -> make('qvec'=> $h, $k, $l);
504 # get central atom element
505 my ($central, $xc, $yc, $zc) = $::cell -> central($::keywords->{'core'});
506 # get edge
507 $::keywords->make('edge' => $::atoms_values{'edge'});;
508 $dafs_running = 0;
509 $::keywords->set_edge($::cell, 1);
510 $dafs_running = 1;
511 $::atoms_values{'edge'} = $::keywords->{'edge'};
512 # energy grid
513 my ($e0, $emin, $emax, $estep, $npoints);
514 ($e0, $emin, $emax, $estep, $npoints) =
515 dafs_set_grid($central, $::keywords->{'edge'}, $dafs_values{'emin'},
516 $dafs_values{'emax'}, $dafs_values{'estep'});
517 $::keywords -> make('emin'=>$emin, 'emax'=>$emax, 'estep'=>$estep);
518 ($dafs_values{'emin'}, $dafs_values{'emax'}, $dafs_values{'estep'},
519 $dafs_values{'npoints'}) = ($e0-$emin, $emax-$e0, $estep, $npoints);
520 my ($e, $cnt) = ($emin, 0);
521 # finish up cell
522 $dafs_running = 0;
523 $::keywords -> verify_keywords($::cell, \@::sites, 1);
524 $dafs_running = 1;
525 # cache f0 and phases
526 my (%fnot, @phase, @dwf);
527 &dafs_set_cache($::cell, \%fnot, \@phase, \@dwf, $h, $k, $l);
528 # clean up previous plot
529 ## update_progress_bar(\$dafs_progress, \$dafs_done, \$dafs_todo,
530 ## 0, $dafs_values{'progress'});
531 $dafs_plot -> delete($dafs_data);
532 $dafs_plot -> delete($dafs_title);
533 @dafs_tics = map {$dafs_plot -> delete($_)} @dafs_tics;
534 # calculate
535 my (%fp, %fpp, $r, $i, $la, %mu, $mutot, @plot);
536 my ($contents) = $::cell -> attributes("contents");
537 (Xray::Absorption -> current_resource =~ /$::meta{absorption_tables}/i) or do {
538 $::meta{absorption_tables} =
539 grep (/CL/, Xray::Absorption -> available()) ? "CL" : "Henke";
540 Xray::Absorption -> load($::meta{absorption_tables});
541 };
542 (Xray::Absorption->current_resource =~ /\b(Henke|Chantler|CL|Sasaki)\b/i)
543 or do {
544 $::meta{absorption_tables} =
545 grep (/CL/, Xray::Absorption -> available()) ? "CL" : "Henke";
546 Xray::Absorption -> load($::meta{absorption_tables});
547 };
548 my ($min, $max) = (100000, 0);
549
550 ## normalization terms for Lorentz-Absorption correction
551 ($::keywords->{thickness} > 0) or ($::keywords->{thickness} = 1);
552 my $lambda = 2*PI*HBARC / $e;
553 my $dsp = $::cell -> d_spacing($h, $k, $l);
554 my $sinthnot = $lambda / (2 * $::cell -> d_spacing($h, $k, $l));
555 my $munot = Xray::Atoms::xsec($::cell, $central, $e);
556 $munot *= 10e-8; #undef $dens;
557 my $absnot = (1 - exp(-2*$::keywords->{thickness}*$munot/$sinthnot)) /
558 (2*$munot);
559 $sinthnot = sin(2*asin($sinthnot));
560 my $sinth;
561
562 ## ------------------------------------------------------------------------
563 ## cache all anomalous scattering values for this calculation
564 foreach my $s (@{$contents}) {
565 my ($el) = $ {$$s[3]} -> attributes('Element');
566 next if exists $fp{$el};
567 $fp{$el} = [];
568 };
569 my $ee = $e;
570 my @energies = ();
571 while ($ee < $emax) {
572 push @energies, $ee;
573 $ee += $estep;
574 };
575 my $foo = 0;
576 foreach my $l (keys %fp) {
577 @{$fp{$l}} = Xray::Absorption -> cross_section($l, \@energies, 'f1');
578 @{$fpp{$l}} = Xray::Absorption -> cross_section($l, \@energies, 'f2');
579 my $factor = Xray::Absorption -> get_conversion($l);
580 my $weight = Xray::Absorption -> get_atomic_weight($l);
581 foreach my $i (0 .. $#energies) {
582 my $lambda = 2 * PI * HBARC / $energies[$i];
583 $ {$mu{$l}}[$i] = 2*RE * $lambda * $ {$fpp{$l}}[$i] *
584 0.6022045 * 1e8 * $factor / $weight;
585 };
586 ##@{$mu{$_}} = Xray::Absorption -> cross_section($_, \@energies, 'xsec');
587 };
588 my @total = Xray::Atoms::xsec($::cell, $central, \@energies);
589 ## ------------------------------------------------------------------------
590
591 my $pt = 0;
592 while ($e < $emax) {
593 ($r, $i) = (0,0); ## clear these at each energy
594 my $counter = 0;
595 $mutot = 0;
596 $lambda = 2*PI*HBARC / $e;
597 $sinth = $lambda / (2 * $dsp);
598 foreach my $s (@{$contents}) {
599 my ($el, $sym, $occ, $id) =
600 $ {$$s[3]} -> attributes('Element', 'CromerMann', 'Occupancy', 'Id');
601 my $phase = $phase[$counter];
602 my $dwf = $dwf[$counter];
603 my $fone = $fnot{$sym} + $ {$fp{$el}}[$pt];
604 my $ftwo = $ {$fpp{$el}}[$pt];
605 ## do the complex arithmatic by hand
606 $r += $occ * $dwf * ($fone * cos($phase) - $ftwo * sin($phase));
607 $i += $occ * $dwf * ($fone * sin($phase) + $ftwo * cos($phase));
608 $mutot += $occ * $ {$mu{$el}}[$pt];
609 ++$counter;
610 };
611 ## Lorentz and absoprtion correction
612 ##my ($total, $density) = map {sprintf "%8.2f", $_}
613 ## Xray::Atoms::xsec($::cell, $central, $e);
614 $total[$pt] *= 10e-8;
615 $la = (1 - exp(-2*$::keywords->{thickness}*$total[$pt]/$sinth)) /
616 (2*$total[$pt]);
617 $la /= $absnot;
618 $sinth = sin(2*asin($sinth));
619 $la *= ($emin**3 * $sinthnot) / ($e**3 * $sinth);
620 my $this_energy = [$e, $r, $i, $la];
621 my $as = $la*($r**2 + $i**2);
622 my $this_point = [$e, $as];
623 ($as > $max) and $max = $as;
624 ($as < $min) and $min = $as;
625 #print join(" ", $e, $r, $i), $/;
626 push @dafs_calculation, $this_energy;
627 push @plot, $this_point;
628 ++$cnt;
629 ## ($cnt % 10) or
630 ## update_progress_bar(\$dafs_progress, \$dafs_done, \$dafs_todo,
631 ## $cnt/$dafs_values{'npoints'},
632 ## $dafs_values{'progress'});
633 $e += $estep; # increment energy
634 ++$pt;
635 };
636 ## if ($::convolve_dafs) { do a convolution };
637 ## update_progress_bar(\$dafs_progress, \$dafs_done, \$dafs_todo,
638 ## 1, $dafs_values{'progress'});
639
640 ($min, $max) = (int(0.9*$min), int(1.1*$max));
641 my $yspan = ($max-$min) || (0.1);
642 my $espan = ($emax-$emin);
643
644 my @to_plot;
645 @dafs_x = (); @dafs_y = ();
646 foreach my $p (@plot) {
647 my ($x,$y) = @$p;
648 push @dafs_x, $x; # for external plot
649 push @dafs_y, $y; #
650 ($x, $y) = ((($x-$emin)/$espan), (($y-$min)/$yspan)); # fractional
651 ($x, $y) = tkatoms_fraction2canvas($dafs_values{canvas},
652 $dafs_values{progress},$x, $y); # on canvas
653 push @to_plot, $x, $y; # integers
654 };
655 $dafs_data = $dafs_plot -> createLine(@to_plot, -fill=>$::colors{'plot'});
656 foreach my $i (0..4) {
657 my $f = $i * 0.25;
658 my @font = ('-font', 'Arial 9 normal');
659 # x tics
660 my ($xp, $yp) = tkatoms_fraction2canvas($dafs_values{canvas},
661 $dafs_values{progress},$f, 0);
662 $dafs_tics[$i] = $dafs_plot
663 -> createText($xp, $yp+8, qw/-anchor center/,
664 -text=>int($emin+$f*$espan), @font);
665 # y tics
666 next unless $i; # 1st y label overlaps 1st x label
667 ($xp, $yp) = tkatoms_fraction2canvas($dafs_values{canvas},
668 $dafs_values{progress},0,$f);
669 $dafs_tics[$i+5] = $dafs_plot
670 -> createText($xp-30, $yp, qw/-anchor w/,
671 -text=>int($min+$f*$yspan), @font);
672 };
673 my $title = ucfirst($central) . " " . ucfirst($dafs_values{'edge'}) .
674 " (" . join(" ", $h, $k, $l) . ")";
675 $dafs_title = $dafs_plot -> createText(200, 15, -text=>$title);
676 $dafs_values{plot_group} = join("", $central, $h, $k, $l);
677
678 $dafs_running = 0;
679 };
680
681
682 ## ref to canvas, 'x' or 'y', percentage of full span, height in
683 ## pixels of tic
684 sub dafs_make_tic {
685 my ($canvas, $axis, $value, $size) = @_;
686 my ($x1, $y1, $x2, $y2);
687 if ($axis eq 'x') {
688 $value *= ($dafs_values{'canvas'}-36-11);
689 $x1 = int($value)+36;
690 $y1 = $dafs_values{'progress'}-21;
691 $x2 = int($value)+36;
692 $y2 = $dafs_values{'progress'}-21-$size;
693 } else {
694 $value *= $dafs_values{'progress'}-21-11;
695 $value = $dafs_values{'progress'}-21-11-int($value);
696 $x1 = 36;
697 $y1 = $value+11;
698 $x2 = 36+$size;
699 $y2 = $value+11;
700 };
701 $$canvas -> createLine($x1, $y1, $x2, $y2);
702 1;
703 }
704
705 sub dafs_fraction2canvas {
706 my ($x, $y) = @_;
707 $x = $x*($dafs_values{'canvas'}-36-11) + 36;
708 $y *= $dafs_values{'progress'}-16-11;
709 $y = $dafs_values{'progress'}-16-11 - $y + 11;
710 return (sprintf("%d", $x), sprintf("%d", $y));
711 };
712
713
714
715 ## turn relative bounds into absolute bounds, and do error checking
716 sub dafs_set_grid {
717 my ($central, $edge, $emin, $emax, $estep) = @_;
718 my $e0 = Xray::Absorption -> get_energy($central, $edge);
719 $emin ||= 300;
720 $emax ||= 500;
721 ($emin, $emax) = ($e0-$emin, $e0+$emax);
722 $estep ||= 15;
723 ($estep <= 0) and $estep = 15; # some error checking
724 ($emin > $emax) and ($emin, $emax) = ($emax, $emin);
725 my $npoints = int(($emax - $emin)/$estep) + 1;
726 return ($e0, $emin, $emax, $estep, $npoints);
727 }
728
729
730 ## my (%fnot, @phase);
731 ## &dafs_set_cache($::cell, \%fnot, \@phase, $h, $k, $l);
732 sub dafs_set_cache {
733 my ($cell, $r_fnot, $r_phase, $r_dwf, $h, $k, $l) = @_;
734 my ($contents) = $cell -> attributes("contents");
735 my $counter = 0;
736 my $d = $cell -> d_spacing($h, $k, $l);
737 foreach my $s (@{$contents}) {
738 my ($e, $v, $b, $bx, $by, $bz) =
739 $ {$$s[3]} -> attributes('Element', 'Valence', 'B', 'Bx', 'By', 'Bz');
740 ##--## ($bx, $by, $bz) = &dafs_interpret_dwf($bx, $by, $bz);
741 ## print join(" ", $b, $/);
742 my $sym = Xray::Scattering->get_valence($e, $v);
743 $ {$$s[3]} -> make('CromerMann'=>$sym);
744 $$r_fnot{$sym} = Xray::Scattering->get_f($sym, $d);
745 $$r_phase[$counter] = $$s[0] * $h + $$s[1] * $k + $$s[2] * $l;
746 $$r_phase[$counter] *= 2*PI;
747 $$r_dwf[$counter] = $b * ($h**2 + $k**2 + $l**2);
748 ##--## $$r_dwf[$counter] = $bx*$h**2 + $by*$k**2 + $bz*$l**2;
749 ## is this right? or should it be -1/2 for when the structure
750 ## factor gets squared
751 $$r_dwf[$counter] = exp(-1 * $$r_dwf[$counter]);
752 ++$counter;
753 };
754 };
755
756 sub dafs_interpret_dwf {
757 my ($bx, $by, $bz) = @_;
758 my $epsi = $Xray::Atoms::epsilon;
759 my $default = 0;
760 if ($bx > $epsi) {
761 $default = $bx;
762 } elsif ($by > $epsi) {
763 $default = $by;
764 } elsif ($bz > $epsi) {
765 $default = $bz;
766 };
767 ($bx < $epsi) and $bx = $default;
768 ($by < $epsi) and $by = $default;
769 ($bz < $epsi) and $bz = $default;
770 return ($bx, $by, $bz);
771 };
772
773 sub update_progress_bar {
774 my ($canvas, $done, $todo, $percent, $scale) = @_;
775 --$scale;
776 ($percent > 1) and $percent = 1;
777 ($percent < 0) and $percent = 0;
778 my $h = int($percent*$scale);
779 $h = $scale - $h;
780 my $hp = $h+1;
781 ($hp > $scale) and $hp = $scale;
782 $$canvas ->delete($$done);
783 $$canvas ->delete($$todo);
784 $$done = $$canvas -> createRectangle(1, $hp, 21, $scale, -fill=>$::colors{'done'});
785 $$todo = $$canvas -> createRectangle(1, 1, 21, $h, -fill=>$::colors{'todo'});
786 $::top->update;
787 };
788
789 sub save_dafs {
790 my $ext = $_[0];
791 (@dafs_calculation) or die $$::dialogs{'no_calculation'}.$/;
792 my $showall = ($ext eq 'dat') ? 'YES' : 'NO';
793 require Cwd;
794 my $path = $::default_filepath || Cwd::cwd();
795 my $fname =
796 join("", 'dafs_', (map {sprintf "%d", $_} @{$::keywords->{'qvec'}}),
797 ".", $ext);
798 my $types = [['Dat files', '.dat'],
799 ['All Files', '*', ],];
800 my $ofile = $::top -> getSaveFile(-filetypes=>$types,
801 -initialdir=>$path,
802 #(not $is_windows) ?
803 # (-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () ,
804 -initialfile=>$fname,
805 -title => $$::labels{'save_dialog'});
806 return 0 unless $ofile;
807 $::default_filepath = dirname($ofile);
808 if ($ext eq 'dat') { # save data using dafs.atp
809 my $contents = "";
810 $::keywords->make('identity'=>"TkAtoms $Xray::Atoms::VERSION");
811 my ($ofname, $is_feff)
812 = parse_atp($dafs_values{'atp'}, $::cell, $::keywords,
813 \@dafs_calculation, \@::neutral,\$contents);
814 open (OUT, ">".$ofile) or
815 die $$Xray::Atoms::messages{cannot_write} . $ofile . $/;
816 print OUT $contents;
817 close OUT;
818 } else { # save canvas as postscript
819 $dafs_plot->postscript(-file=>$ofile);
820 };
821 }
822
823 sub clear_dafs {
824 $dafs_values{'h'} = 0;
825 $dafs_values{'k'} = 0;
826 $dafs_values{'l'} = 0;
827 $dafs_values{'emin'} = 300;
828 $dafs_values{'emax'} = 500;
829 $dafs_values{'estep'} = 15;
830 ##$dafs_values{'edge'} = '';
831 ##update_progress_bar(\$dafs_progress, \$dafs_done, \$dafs_todo,
832 ## 0, $dafs_values{'progress'});
833 $dafs_plot -> delete($dafs_data);
834 $dafs_plot -> delete($dafs_title);
835 @dafs_tics = map {$dafs_plot -> delete($_)} @dafs_tics;
836 1;
837 };
838
839 sub asin { atan2($_[0], sqrt(1 - $_[0] * $_[0])) };
840
841
842 1;
843 __END__