Debian Codegraph @

attic/scripts/Tk/Absorption.pm

raw
1 #!/usr/bin/perl -w
2 ######################################################################
3 ## Absorption 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::Absorption;
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);
31 @EXPORT_OK = qw(absorption);
32 $cvs_info = '$Id: Absorption.pm,v 1.6 2001/09/20 17:53:14 bruce Exp $ ';
33 $VERSION = (split(' ', $cvs_info))[2] || 'pre_release';
34
35 use Tk;
36 require Xray::Atoms;
37 use Xray::ATP;
38 use File::Basename qw(dirname);
39 use constant EPSILON => 0.00001;
40 my $is_windows = (($^O eq 'MSWin32') or ($^O eq 'cygwin'));
41
42 $$::labels{edge_energy} = 'Edge energy:';
43 $$::help{edge_energy} = 'Blah blah blah';
44
45
46 my ($abs_frame, $menubar);
47 my ($abs_units, %absorption_label, %absorption_calculation,
48 %absorption_entry, %absorption_widgets);
49
50
51 ######################################################################
52 ######### ##################
53 ######### Notecard containing Absorption keywords ##################
54 ######### ##################
55 ######################################################################
56 sub absorption {
57 #$abs_frame = $_[0] -> Frame() -> pack(-fill=>'x');
58 $abs_frame = $::pages{Absorption} -> Frame() -> pack(-fill=>'x');
59
60 ## ---- menubar -----------
61 $menubar = $abs_frame -> Frame(-borderwidth=>2, -relief=>'ridge',)
62 -> pack(-anchor=>'nw', -fill=>'x', -pady=>4, -padx=>4);
63
64 my $file_menu = $menubar ->
65 Menubutton(@::file_menubutton) -> pack(-side=>'left');
66 &::manage($file_menu, "menu");
67 my $this = $file_menu
68 -> command(-label=>$$::labels{load_input}, @::menu_args,
69 -command=>\&::load_input,
70 -accelerator=>'Control+o', );
71 &::manage($this, "menu");
72 ($::LWP_simple_exists) and do {
73 $this = $file_menu
74 -> command(-label=>$$::labels{load_adb}, @::menu_args,
75 -command=>\&::load_adb, );
76 &::manage($this, "menu");
77 $this = $file_menu
78 -> command(-label=>$$::labels{download_adb}, @::menu_args,
79 -command=>\&::download_adb, );
80 &::manage($this, "menu");
81 };
82 my $recent = $file_menu ->
83 cascade(-label=>"Recent files", @::menu_args, -tearoff=>0,);
84 &::manage($recent, "menu");
85 push @::recent_registry, $recent;
86 foreach my $f (@::recent_files) {
87 $recent -> command(-label=>$f, -command=>sub{&::load_input($f, 1)});
88 };
89
90 my $sep = $file_menu -> separator();
91 &::manage($sep, "separator");
92 $this = $file_menu
93 -> command(-label=>$$::labels{save_input}, @::menu_args,
94 -command=>\&::save_input,
95 -accelerator=>'Control+s', );
96 &::manage($this, "menu");
97 $this = $file_menu -> command(-label=>$$::labels{'save_absorption'},
98 @::menu_args,
99 -command=>\&save_absorption,);
100 &::manage($this, "menu");
101 ## $sep = $file_menu -> separator();
102 ## &::manage($sep, "separator");
103 ## $this = $file_menu -> command(@::apt_args);
104 ## &::manage($this, "menu");
105 $sep = $file_menu -> separator();
106 &::manage($sep, "separator");
107 $this = $file_menu
108 -> command(-label=>$$::labels{quit}, @::menu_args,
109 -command=>\&::quit_tkatoms,
110 -accelerator=>'Control+q', );
111 &::manage($this, "menu");
112
113
114 my $clear_menu = $menubar ->
115 Menubutton(-text=>$$::labels{'clear_menu'}, @::menu_args) ->
116 pack(-side=>'left');
117 &::manage($clear_menu, "menu");
118 $this = $clear_menu
119 -> command(-label=>$$::labels{'clear_absorption'}, @::menu_args,
120 -command=>\&clear_absorption);
121 &::manage($this, "menu");
122 $this = $clear_menu
123 -> command(-label=>$$::labels{'clear_lattice'}, @::menu_args,
124 -command=>\&::clear_lattice);
125 &::manage($this, "menu");
126 $this = $clear_menu
127 -> command(-label=>$$::labels{'clear_all'}, @::menu_args,
128 -command=>sub{&clear_absorption; &::clear_lattice});
129 &::manage($this, "menu");
130
131 my $data_menu = $menubar ->
132 Menubutton(-text=>$$::labels{'data_menu'}, @::menu_args) ->
133 pack(-side=>'left');
134 &::manage($data_menu, "menu");
135
136 ## $this = $data_menu ->
137 ## command(-label=>$$::labels{'dump'}, @::menu_args,
138 ## -command=>[\&::display_in_frame, $::top, '', 0],
139 ## -accelerator=>'Control+d', );
140 ## &::manage($this, "menu");
141
142
143 $abs_units = 'cm^-1';
144 my $abs_units_menu = $data_menu ->
145 cascade(-label=>$$::labels{abs_units},
146 -tearoff=>0,
147 -foreground=>$::colors{'label'}, );
148 &::manage($abs_units_menu, "menu");
149 foreach my $units ('cm^-1', 'mic.') {
150 my $this = $abs_units_menu ->
151 radiobutton(-label => $units, @::menu_args,
152 -selectcolor=>$::colors{button},
153 -variable=>\$abs_units,
154 -value=>$units,
155 -command=>[\&swap_absorption, $units],
156 );
157 &::manage($this, "menu");
158 &::manage($this, "radio");
159 };
160
161
162 my $abs_abs_data = $data_menu ->
163 cascade(-label=>$$::labels{data_resources},
164 -tearoff=>0,
165 -foreground=>$::colors{'label'}, );
166 &::manage($abs_abs_data, "menu");
167 my %abs_menu_abs_button;
168
169 foreach my $resource (sort(Xray::Absorption->available)) {
170 my $r = lc($resource);
171 next if ($r eq 'none');
172 $abs_menu_abs_button{lc($r)} = $abs_abs_data
173 -> radiobutton(-label => $resource, @::menu_args,
174 -selectcolor=>$::colors{button},
175 -variable=>\$::meta{absorption_tables},
176 -value=>$r,
177 -command=>sub{ Xray::Absorption->load($r);
178 $::meta{absorption_tables} = $r; });
179 &::manage($abs_menu_abs_button{lc($r)}, "menu");
180 &::manage($abs_menu_abs_button{lc($r)}, "radio");
181 };
182
183 my $help_menu = $menubar ->
184 Menubutton(@::help_menubutton) -> pack(-side=>'right');
185 &::set_help_menu($help_menu);
186
187 my $pref_menu = $menubar ->
188 Menubutton(@::pref_menubutton) -> pack(-side=>'right');
189 &::set_pref_menu($pref_menu);
190
191 &::manage($help_menu, "menu");
192 &::manage($pref_menu, "menu");
193 ## ------------------------
194
195 ## Descripton
196 my $top_frame = $abs_frame -> Frame(-relief=>'groove', -borderwidth=>2)
197 -> pack(-pady=>2, -side=>'top');
198 my $abs_label = $top_frame ->
199 Label(-text=>$$::labels{'absorption_description'}, @::header_args, ) ->
200 pack();
201 &::manage($abs_label, "header");
202
203
204 ## ---- frames ------------
205 my $abframe = $abs_frame -> Frame() -> pack(-fill=>'x', -pady=>2);
206 my $abf1 = $abframe -> Frame() -> pack(-side=>'left', -pady=>0); # results
207
208 my $abf3 = $abframe -> Frame() -> pack(-side=>'right', -pady=>2); # gasses
209
210 my $abf2 = $abframe -> Frame() # run button and resources
211 -> pack(-side=>'right', -padx=>2, -anchor=>'n');
212
213 my $ablf = $abf3 -> LabFrame(-label=>$$::labels{fill_gases},
214 -foreground=>$::colors{label},
215 -labelside=>'acrosstop',
216 -borderwidth=>2)
217 -> pack(-side=>'bottom', -padx=>5);
218 $::balloon->attach(($ablf->children)[1], -msg=>$$::help{fill_gases},);
219 &::manage($ablf, "labframe");
220 ## ------------------------
221
222
223 ## ---- frame contents -----
224 my $col = 0;
225 foreach my $which ('nitrogen', 'argon', 'krypton') {
226 my $label = $ablf -> Label(-text=>$$::labels{$which}, @::label_args,)
227 -> grid(-column=>$col, -row=>0,);
228 &::manage($label, "label");
229 my $scale = $ablf -> Scale(-from => 0,
230 -to => 1,
231 -orient => 'vertical',
232 -resolution => 0.01,
233 '-length' => 100,
234 -sliderlength => 15,
235 -command => [\&::set_gas, $which],
236 #-variable => \$keywords->{$which},
237 -foreground => $::colors{label},
238 )
239 -> grid(-column=>$col++, -row=>1, -padx=>2);
240 $absorption_widgets{$which} = $scale;
241 &::manage($scale, "scale");
242 $::balloon->attach($label, -msg=>$$::help{$which},);
243 };
244
245
246 my $absorption_button = $abf2
247 -> Button(-text=>$$::labels{'run_absorption'},
248 @::button_args,
249 -command=>\&run_absorption,
250 -borderwidth=>4,
251 ) -> pack(-side=>"top");
252 $::balloon->attach($absorption_button, -msg=>$$::help{'run_absorption'},);
253 &::manage($absorption_button, "button");
254
255
256 my $abrf = $abf2 -> LabFrame(-label=>$$::labels{data_resources},
257 -foreground=>$::colors{label},
258 -labelside=>'acrosstop',
259 -borderwidth=>2)
260 -> pack(-side=>'bottom', -pady=>2, -padx=>5);
261 $::balloon->attach(($abrf->children)[1], -msg=>$$::help{data_resources},);
262 &::manage($abrf, "labframe");
263
264 my %absorption_resource_button;
265 my $absorption_resource_frame = $abrf
266 -> Scrolled("Text", -scrollbars=>'e', -height=>5, -width=>13,
267 -relief=>'flat',)
268 -> pack(-side=>"top");
269 $absorption_resource_frame->Subwidget("yscrollbar")->configure(-background=>$::colors{background});
270 foreach my $resource (sort(Xray::Absorption->available)) {
271 my $r = lc($resource);
272 next if ($r eq 'none');
273 $absorption_resource_button{lc($r)} = $absorption_resource_frame
274 -> Radiobutton(-text => $resource, @::menu_args,
275 -selectcolor=>$::colors{radio},
276 -variable=>\$::meta{absorption_tables},
277 -value=>$r,
278 -justify=>'left',
279 -command=>sub{ Xray::Absorption->load($r);
280 $::meta{absorption_tables} = $r; });
281 &::manage($absorption_resource_button{lc($r)}, "radio");
282 $absorption_resource_frame
283 -> windowCreate('end', -window=>$absorption_resource_button{lc($r)});
284 $absorption_resource_frame -> insert('end', $/);
285 $::balloon->attach($absorption_resource_button{lc($r)},
286 -msg=>$$::help{lc($r)});
287 }
288 $absorption_resource_frame -> configure(-state=>'disabled');
289
290 my $aben = $abf2 -> Frame(-borderwidth=>2)
291 -> pack(-side=>'bottom', -pady=>4); #, -padx=>5);
292 my $aben_lab = $aben -> Label(-text=>$$::labels{absen}, @::label_args)
293 -> pack(-side=>'left', -pady=>2);
294 &::manage($aben_lab, "label");
295 $::balloon->attach($aben_lab, -msg=>$$::help{absen});
296 my $aben_ent = $aben -> Entry(-textvariable=>\$absorption_calculation{absen},
297 -font=>$::fonts{label},
298 -width=>9, -justify=>'center')
299 -> pack(-side=>'right', -pady=>2);
300 &::manage($aben_ent, "entry");
301
302
303 my $absorption_row = 0;
304 ## 'edge_energy',
305 foreach my $w ('total_abs', 'edge_step', 'density',
306 'mcmaster_corr', 'i0_corr',
307 'self_sigma', 'sum_sigma', 'self_amp') {
308 $absorption_calculation{$w} = 0;
309 $absorption_label{$w} = $abf1 ->
310 Label(-text=>$$::labels{$w}, @::label_args, -height=>0.7)
311 -> grid(-column=>0, -row=>$absorption_row,
312 -sticky=>'e', -padx=>3, -pady=>0);
313 &::manage($absorption_label{$w}, "label");
314 $::balloon->attach($absorption_label{$w}, -msg=>$$::help{$w},);
315
316 $absorption_entry{$w} = $abf1 ->
317 Entry(-textvariable=>\$absorption_calculation{$w},
318 -foreground=>$::colors{foreground},
319 -background=>$::colors{background},
320 ($Tk::VERSION >= 804) ? (-disabledforeground=>$::colors{foreground},
321 -disabledbackground=>$::colors{background},) :
322 (),
323 -font=>$::fonts{label},
324 -width=>14, -justify=>'left', -relief=>'flat')
325 -> grid(-column=>1, -row=>$absorption_row++, -sticky=>'w',
326 -padx=>2, -pady=>0);
327 #&::manage($absorption_entry{$w}, "entry"); # why is this commented?
328 $absorption_entry{$w} -> configure(-state=>'disabled');
329 };
330
331 };
332
333
334
335
336 ### > absorption subroutines
337 ## --------------------------------------------------------------------
338
339 ## change the labels and units on mu and delta mu
340 sub swap_absorption {
341 my $units = $_[0];
342 $units = $abs_units;
343 if ($units =~ /mic/) {
344 $absorption_label{'total_abs'} -> configure(-text=>$$::labels{abs_length});
345 $absorption_label{'edge_step'} -> configure(-text=>$$::labels{unit_edge});
346 } else {
347 $absorption_label{'total_abs'} -> configure(-text=>$$::labels{total_abs});
348 $absorption_label{'edge_step'} -> configure(-text=>$$::labels{edge_step});
349 };
350 foreach my $w ('total_abs', 'edge_step') {
351 if ($absorption_calculation{$w} =~ /([\d.]+) +(cm\^-1|mic\.)/ ) {
352 $absorption_entry{$w} -> configure(-state=>'normal');
353 if ($1 > 0.00001) {
354 ($absorption_calculation{$w} =
355 (sprintf "%8.2f", 10000/$1) . " " . $units) =~ s/^\s+//;
356 } else {
357 $absorption_calculation{$w} = $1 . " " . $units;
358 };
359 $absorption_entry{$w} -> configure(-state=>'disabled');
360 };
361 };
362 $abs_units = $units;
363 };
364
365
366 ## run the absorption calculation and display the results
367 sub run_absorption {
368 $::cell -> make( Occupancy=>1 );
369 undef $::keywords;
370 $::keywords = Xray::Atoms -> new();
371 &::validate_lattice(0);
372 my $core_tag = $::site_entries[$::core_index]{'tag'}->get();
373 ($core_tag =~ /^\s*$/) and
374 $core_tag = $::site_entries[$::core_index]{'elem'}->get();
375 $::keywords -> make('core'=>$core_tag);
376 &absorption_validate(0);
377 $::keywords -> verify_keywords($::cell, \@::sites, 1);
378 foreach my $w (keys %absorption_entry) {
379 $absorption_entry{$w} -> configure(-state=>'normal');
380 };
381 foreach my $k (keys %absorption_calculation) {
382 next if ($k eq 'absen');
383 $absorption_calculation{$k} = 0;
384 };
385 my ($c, $x, $y, $z) = $::cell->central($::keywords->{'core'});
386 my $e = $absorption_calculation{absen} || $::keywords->{'edge'};
387 unless ($e =~ /^(?:\d+(?:\.\d*)?|\.\d+)$/) { ## an energy value was entered
388 if ( ($e =~ /^[mnop]/i) or ## or the edge of the chosen atom is used
389 (not Xray::Absorption->data_available($c, $e)) ) {
390 &::tkatoms_text_dialog(\$::top, $$::dialogs{'no_absorption'}, 'center');
391 return 0;
392 };
393 };
394 ##$absorption_calculation{edge_energy} =
395 ## Xray::Absorption->get_energy($c, $e) . " eV";
396 ($absorption_calculation{total_abs},
397 $absorption_calculation{edge_step},
398 $absorption_calculation{density}) =
399 map {sprintf "%8.2f", $_}
400 Xray::Atoms::absorption($::cell, $c, $e);
401 if ($abs_units eq 'mic.') {
402 $absorption_calculation{total_abs} =
403 sprintf "%8.2f", 10000/$absorption_calculation{total_abs};
404 if ($absorption_calculation{edge_step} > 0.00001) {
405 $absorption_calculation{edge_step} =
406 sprintf "%8.2f", 10000/$absorption_calculation{edge_step};
407 };
408 };
409
410 unless ($e =~ /^(?:\d+(?:\.\d*)?|\.\d+)$/) {
411 ## if an energy is specified, it does not
412 ## make sense to do the exafs corrections
413 $absorption_calculation{mcmaster_corr} =
414 sprintf "%8.5f", Xray::Atoms::mcmaster($c, $e);
415 $absorption_calculation{sum_sigma} = $absorption_calculation{mcmaster_corr};
416
417 my ($n, $a, $k) =
418 ($::keywords->{nitrogen}, $::keywords->{argon}, $::keywords->{krypton});
419 if ( ($n > EPSILON) || ($a > EPSILON) || ($k > EPSILON) ) {
420 $absorption_calculation{i0_corr} =
421 sprintf "%8.5f", Xray::Atoms::i_zero($c, $e, $n, $a, $k);
422 ($absorption_calculation{self_amp}, $absorption_calculation{self_sigma}) =
423 Xray::Atoms::self($c, $e, $::cell);
424 $absorption_calculation{self_amp} =
425 sprintf "%8.2f", $absorption_calculation{self_amp};
426 $absorption_calculation{self_sigma} =
427 sprintf "%8.5f", $absorption_calculation{self_sigma};
428 $absorption_calculation{sum_sigma} +=
429 $absorption_calculation{i0_corr} +
430 $absorption_calculation{self_sigma};
431 $absorption_calculation{i0_corr} .= " cm^2";
432 $absorption_calculation{self_sigma} .= " cm^2";
433 };
434 };
435
436 ## units
437 $absorption_calculation{total_abs} .= " " . $abs_units;
438 $absorption_calculation{edge_step} .= " " . $abs_units;
439 $absorption_calculation{mcmaster_corr} .= " cm^2";
440 $absorption_calculation{sum_sigma} .= " cm^2" ;
441
442 foreach my $w (keys %absorption_entry) {
443 $absorption_entry{$w} -> configure(-state=>'disabled');
444 };
445 foreach my $k (keys %absorption_calculation) {
446 next if ($k eq 'absen');
447 $absorption_calculation{$k} =~ s/^\s+//;
448 };
449 #unless ($e =~ /^(?:\d+(?:\.\d*)?|\.\d+)$/) {
450 # $absorption_calculation{absen} = Xray::Absorption->get_energy($c, $e);
451 #};
452 };
453
454
455 ## save a calculation using the absorption.atp file
456 sub save_absorption {
457 $::cell -> make( Occupancy=>1 );
458 undef $::keywords;
459 $::keywords = Xray::Atoms -> new();
460 my $eol = $/ . "+"; # split title text into lines
461 my @t = split /$eol/, $::atoms_widgets{'title'}->get(qw/1.0 end/);
462 $::keywords->{'title'} = [];
463 foreach my $t (@t) {
464 $::keywords->make('title'=>$t);
465 };
466 &::validate_lattice(0);
467 my $core_tag = $::site_entries[$::core_index]{'tag'}->get();
468 ($core_tag =~ /^\s*$/) and
469 $core_tag = $::site_entries[$::core_index]{'elem'}->get();
470 $::keywords -> make('core'=>$core_tag);
471 &absorption_validate(0);
472 $::keywords -> verify_keywords($::cell, \@::sites, 1);
473
474 my $contents = "";
475 $::keywords -> make('identity'=>"TkAtoms $Xray::Atoms::VERSION");
476 my ($ofname, $is_feff)
477 = parse_atp('absorption', $::cell, $::keywords,
478 \@::cluster, \@::neutral, \$contents);
479
480 require Cwd;
481 my $path = $::default_filepath || Cwd::cwd();
482 my $types = [['All Files', '*', ],];
483 my $inputfile = $::top -> getSaveFile(-filetypes=>$types,
484 -initialdir=>$path,
485 #(not $is_windows) ?
486 #(-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () ,
487 -initialfile=>$ofname,
488 -title => $$::labels{'save_dialog'});
489 return 0 unless $inputfile;
490 $::default_filepath = dirname($inputfile);
491
492 open (INP, ">".$inputfile) or
493 die $$Xray::Atoms::messages{cannot_write} . $inputfile . $/;
494 print INP $contents;
495 close INP;
496 };
497
498
499 ## make sure all the values make sense
500 sub absorption_validate {
501 my $verbose = $_[0];
502 my $is_ok = 1;
503
504 foreach my $word (qw/argon krypton nitrogen/) {
505 $::keywords->make($word=>$absorption_widgets{$word}->get);
506 if (($::keywords->{$word} < 0) || ($::keywords->{$word} > 1)) {
507 $::keywords->make($word=>0);
508 my $message = "\"$word\": $$::messages{'one_gas'}$/";
509 $::keywords->warn_or_die($message, 1);
510 };
511 };
512 if (($::keywords->{nitrogen} + $::keywords->{argon} + $::keywords->{krypton}) > 1) {
513 my $message = $$::messages{'all_gases'} . $/;
514 $::keywords->make('nitrogen'=> 0);
515 $::keywords->make('argon'=> 0);
516 $::keywords->make('krypton'=> 0);
517 $::keywords->warn_or_die($message, 1);
518 return 0;
519 };
520
521 $::keywords->make('edge' => $::atoms_values{'edge'});;
522 $::keywords->set_edge($::cell, 1);
523 $::atoms_values{'edge'} = $::keywords->{'edge'};
524
525 if ($verbose and $is_ok) {
526 &tkatoms_dialog(\$::top, 'atoms_ok', 'info');
527 };
528 };
529
530
531 ## clear the absorption notecard
532 sub clear_absorption {
533 ## clear all calculations
534 foreach my $k (keys %absorption_calculation) {
535 $absorption_calculation{$k} = 0;
536 };
537 ## reset the gas sliders
538 foreach my $w ('nitrogen', 'argon', 'krypton') {
539 $absorption_widgets{$w} -> set(0);
540 };
541 ## reset the edge menu
542 ##$abs_edge_symbol = "";
543 };
544
545 1;
546 __END__