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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
|
package Demeter::ScatteringPath;
=for Copyright
.
Copyright (c) 2006-2019 Bruce Ravel (http://bruceravel.github.io/home).
All rights reserved.
.
This file is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See The Perl
Artistic License.
.
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.
=cut
=for LiteratureReference (scattering path)
The idea of a house built expressly so that people will become lost
in it may be stranger than the idea of a man with the head of a
bull, and yet the two ideas may reinforce one another. Indeed, the
image of the Labyrinth and the image of the Minotaur seem to "go
together": it is fitting that at the center of a monstrous house
there should live a monstrous inhabitant.
Jorge Luis Borges
The Book of Imaginary Beings
=cut
use autodie qw(open close);
use Moose;
use MooseX::Aliases;
extends 'Demeter';
use Demeter::NumTypes qw( Natural );
with "Demeter::ScatteringPath::Rank";
use Chemistry::Elements qw(get_symbol);
use Carp;
use File::Spec;
use List::Util qw(reduce);
use List::MoreUtils qw(pairwise notall all any);
#use Math::Complex;
use Math::Round qw(round);
#use Math::Trig qw(acos atan);
use POSIX qw(acos);
use Demeter::Constants qw($PI $EPSILON5 $EPSILON6 $FEFFNOTOK);
# used in compute_beta and identify_path; made global as a speed
# optimization I am trying to avoid directly calling variables of
# other packages, but in this case it is warrented as a matter of
# speed
my $fsangle = $Demeter::config->default("pathfinder", "fs_angle");
my $ncangle = $Demeter::config->default("pathfinder", "nc_angle");
my $rtangle = $Demeter::config->default("pathfinder", "rt_angle");
## In principle, I would like to use Moose::Meta::Attribute::Native with
## this class as well. I find that doing for the the ArrayRef valued
## attributes adds a measurable amount of overhead. Since this is
## called SO MANY TIMES, it seems prudent to reduce the amount of
## Moose-y overhead
## caller provides these two
has 'feff' => (is => 'rw', isa => 'Demeter::Feff', alias => 'parent');
has 'string' => (is => 'rw', isa => 'Str', default => q{});
## this is used only by paths coming from an aggregate Feff
## calculation, which *has* to resolve details of the paths
## relatively early since the Feff calcualtion of origin will
## eventually be thrown away
has 'ipot' => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
has 'nkey' => (is => 'rw', isa => 'Int', default => 0); # integer key built from atoms indeces
has 'rleg' => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
has 'beta' => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
has 'fs' => (is => 'rw', isa => 'Int', default => 0);
has 'eta' => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
has 'betanotstraightish' => (is => 'rw', isa => 'Bool', default => 0);
has 'etanonzero' => (is => 'rw', isa => 'Bool', default => 0);
has 'betakey' => (is => 'rw', isa => 'Str', default => q{});
has 'etakey' => (is => 'rw', isa => 'Str', default => q{});
has 'nleg' => (is => 'rw', isa => 'Int', default => 2);
has 'halflength' => (is => 'rw', isa => 'LaxNum', default => 0);
has 'anglein' => (is => 'rw', isa => 'LaxNum', default => 0);
has 'angleout' => (is => 'rw', isa => 'LaxNum', default => 0);
has 'cosinout' => (is => 'rw', isa => 'LaxNum', default => 0);
has 'heapvalue' => (is => 'rw', isa => 'Any', default => 0);
has 'n' => (is => 'rw', isa => 'LaxNum', default => 1);
has 'zcwif' => (is => 'rw', isa => 'LaxNum', default => -1);
has 'degeneracies' => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
has 'fuzzy' => (is => 'rw', isa => 'LaxNum', default => 0);
has 'Type' => (is => 'rw', isa => 'Str', default => q{});
has 'weight' => (is => 'rw', isa => 'Int', default => 0);
has 'randstring' => (is => 'rw', isa => 'Str', default => q{});
has 'folder' => (is => 'rw', isa => 'Str', default => q{});
has 'file' => (is => 'rw', isa => 'Str', default => q{});
has 'fromnnnn' => (is => 'rw', isa => 'Str', default => q{});
has 'orig_nnnn' => (is => 'rw', isa => 'Str', default => q{});
has 'site_fraction'=> (is => 'rw', isa => 'LaxNum', default => 1);
has 'pathfinding' => (is => 'rw', isa => 'Bool', default => 1);
has 'pathfinder_index'=> (is=>'rw', isa=> Natural, default => 0);
has 'cleanup' => (is => 'rw', isa => 'Bool', default => 1);
## set by details method:
#has 'tags' => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
#has 'ipots' => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
#has 'elements' => (is => 'rw', isa => 'ArrayRef', default => sub{[]});
sub BUILD {
#my ($self, @params) = @_;
## cannot do this now, keeping track of SP objects in this while
## during pathfinder is too damn inefficient
#$self->mo->push_ScatteringPath($self);
return $_[0];
};
# a bit of optimization, skipping the "($self) = @_" step
override remove => sub {
return $_[0] if $_[0]->pathfinding;
$_[0]->mo->remove($_[0]) if (defined($_[0]) and ref($_[0]) =~ m{Demeter} and defined($_[0]->mo));
return $_[0];
};
sub DEMOLISH {
#my ($self) = @_;
$_[0]->remove;
};
# override all => sub {
# my ($self) = @_;
# my %all = $self->SUPER::all;
# delete $all{feff};
# return %all;
# };
override 'alldone' => sub {
my ($self) = @_;
my $nnnn = File::Spec->catfile($self->folder, $self->file);
unlink $nnnn if ((-e $nnnn) and ($self->cleanup));
$self->remove;
return $self;
};
sub _betakey {
my ($self) = @_;
my @beta = sort @{ $self->beta };
return join(q{}, @beta);
};
sub _etakey {
my ($self) = @_;
my @eta = sort @{ $self->eta };
return join(q{}, @eta);
};
sub attributes { # returns all SP attributes
my ($self) = @_;
return ($self->meta->get_attribute_list, qw(group name));
};
sub savelist { # returns all SP attributes that are saved when a Feff calc is serialized
my ($self) = @_;
##print join(" ", $self->attributes), $/;
return grep { $_ !~ m{feff|heapvalue|data|plot|mode} } $self->attributes;
};
override serialization => sub {
my ($self) = @_;
my %pathinfo = ();
foreach my $key ($self->savelist) {
$pathinfo{$key} = $self->$key;
};
return YAML::Tiny::Dump(\%pathinfo);
};
## identify the scatter for a single scattering path, return He (obviously silly) is MS
sub scatterer {
my ($self) = @_;
return 'He' if $self->nleg > 2;
my @atoms = split(/\./, $self->string);
return $self->feff->site_species($atoms[1]);
};
## construct the intrp line by disentangling the SP string
sub intrplist {
my ($self, $string) = @_;
$string ||= $self->string;
my $feff = $self->feff;
my $token = $self->co->default("pathfinder", "token") || '<+>';
my @atoms = split(/\./, $self->string);
my @intrp = ($token);
my @sites = @{ $feff->sites };
if ($#{$self->ipot} > -1) { ## this is an aggregate feff calc
foreach my $i (1 .. $#{$self->ipot}-1) {
my $this;
my $a = $self->ipot->[$i];
if ($a == 0) {
$this = $token;
} else {
$this = $self->feff->potentials->[$a]->[2] || get_symbol($self->feff->potentials->[$a]->[1]);
};
#$this =~ s{$FEFFNOTOK}{}g; # scrub characters that will confuse Feff
push @intrp, sprintf("%-6s", $this);
};
} else { ## this is a normal feff calc
foreach my $a (@atoms[1 .. $#atoms-1]) {
my $this = ($a == $feff->abs_index) ? $token : $feff->site_tag($a);
#Demeter->pjoin($self->group, $a);
#Demeter->trace;
#$this =~ s{$FEFFNOTOK}{}g; # scrub characters that will confuse Feff
push @intrp, sprintf("%-6s", $this);
};
}
push @intrp, $token;
my $text = sprintf("%-29s", join(" ", @intrp));
if ($feff->is_polarization) {
$text .= sprintf("%5.1f/%5.1f", $self->angleout, $self->anglein);
};
return $text;
};
sub intrpline {
my ($self, $i) = @_;
$i ||= 9999;
my $rank = $self->get_rank(Demeter->co->default('pathfinder', 'rank'));
$rank ||= 0;
my $format = " %4.4d %6.3F %6.3f --- %-29s %2d %6.2f %d %s";
$format = " %4.4d %6.3F %6.3f --- %-39s %2d %6.2f %d %s" if $self->feff->is_polarization;
if ($self->feff->nlegs == 5) {
$format = " %4.4d %6.3F %6.3f --- %-36s %2d %6.2f %d %s";
$format = " %4.4d %6.3F %6.3f --- %-46s %2d %6.2f %d %s" if $self->feff->is_polarization;
};
if ($self->feff->nlegs == 6) {
$format = " %4.4d %6.3F %6.3f --- %-43s %2d %6.2f %d %s";
$format = " %4.4d %6.3F %6.3f --- %-53s %2d %6.2f %d %s" if $self->feff->is_polarization;
};
return sprintf $format,
$i, $self->n, $self->fuzzy, $self->intrplist, $self->weight,
$rank, $self->nleg, $self->Type;
};
sub labelline {
my ($self) = @_;
return sprintf("Reff=%5.3f, nleg=%d, degen=%-2d", $self->fuzzy, $self->nleg, $self->n);
};
alias interplist => 'intrplist';
alias interpline => 'intrpline';
sub ssipot {
my ($self) = @_;
my @hits = split(/\./, $self->string);
my $this_site = $hits[1];
my $ipot = $self->feff->sites->[$this_site]->[3];
return $ipot;
};
sub fetch_ipots {
my ($self) = @_;
my @hits = split(/\./, $self->string);
my @these;
foreach my $h (@hits) {
my $this_site = $h;
my $ipot = ($this_site eq '+') ? 0 : $self->feff->sites->[$this_site]->[3];
#$ipot = 0 if ($ipot eq Demeter->co->default('pathfinder', 'token');
push @these, $ipot;
};
return @these;
};
## set halflength and beta list for this path
sub evaluate {
my ($self) = @_;
my ($feff, $string) = ($self->feff, $self->string);
## compute nlegs
$self -> compute_nleg_nkey($string);
$self -> compute_halflength($feff, $string);
$self -> compute_beta($feff, $string);
$self -> betakey($self->_betakey);
$self -> etakey($self->_etakey);
$self -> identify_path;
$self -> randstring(Demeter->randomstring(9).'.sp');
return $self;
};
sub compute_nleg_nkey {
my ($self, $string) = @_;
my @atoms = split(/\./, $string);
my $na = $#atoms;
shift(@atoms); pop(@atoms); # remove central atom tokens
## compute the numeric key built from the atoms in this path
## this is used to assure order of how paths come off the heap
my ($nkey, $cofactor) = (0,1);
foreach (reverse @atoms) {
$nkey += $cofactor * $_;
$cofactor *= 1000;
};
$self->nleg($na);
$self->nkey($nkey);
return ($na, $nkey);
};
sub compute_halflength {
my ($self, $feff, $string) = @_;
croak("Demeter::ScatteringPath::compute_halflength: feff and string attributes unset")
if not ( (ref($feff) =~ m{Feff}) and $string);
my @sites = @{ $feff->sites };
## keep a list of cartesian coordinates in this path
my @coords = @{ $feff->absorber };
## each part of the string is a number which is the index of that atom
## in the sites list of the Feff object
my @atoms = split(/\./, $string);
shift(@atoms); pop(@atoms); # remove central atom tokens
## deprecated
#foreach my $i (@atoms) {
# ## so this pushes the cartesian coordinates of that site onto the coords list
# push @coords, @{ $sites[$i] }[0..2];
#};
#my $halflength = sprintf("%.5f", Tools->halflength(@coords));
my $cindex = $feff->abs_index;
#my $halflength = sprintf("%.5f", 0.5*$feff->_length($cindex, @atoms, $cindex));
my $halflength = sprintf("%.5f", 0.5*Demeter::Feff::_length($cindex, @atoms, $cindex));
$self->halflength($halflength);
$self->heapvalue($halflength);
$self->compute_polarization_angles($feff, @atoms);
return $halflength;
};
sub compute_polarization_angles {
my ($self, $feff, @atoms) = @_;
if (not $feff->is_polarization) {
$self->anglein(0);
$self->angleout(0);
$self->cosinout(0);
return $self;
};
my $first = $feff->sites->[$atoms[0]];
my $costheta = ($first->[0]*$feff->polarization->[0] +
$first->[1]*$feff->polarization->[1] +
$first->[2]*$feff->polarization->[2]) /
(sqrt($first->[0]**2 + $first->[1]**2 + $first->[2]**2) *
sqrt($feff->polarization->[0]**2 + $feff->polarization->[1]**2 + $feff->polarization->[2]**2));
$self->angleout(180*acos($costheta)/$PI);
$self->cosinout($costheta);
my $last = $feff->sites->[$atoms[-1]];
$costheta = ($last->[0]*$feff->polarization->[0] +
$last->[1]*$feff->polarization->[1] +
$last->[2]*$feff->polarization->[2]) /
(sqrt($last->[0]**2 + $last->[1]**2 + $last->[2]**2) *
sqrt($feff->polarization->[0]**2 + $feff->polarization->[1]**2 + $feff->polarization->[2]**2));
$self->anglein(180*acos($costheta)/$PI);
$self->cosinout(abs($costheta * $self->cosinout));
};
=for Explanation (compute_beta)
trigonometry to determine beta and eta angles. these are straight
translations from mpprmd.f in the feff6 code base
$EPSILON6 is set to 1e-6, as in rdpath.f
_trig
compute Eulerian angles for each path vertex
conventions from Feff6:
x=y=0 and z>0 ==> phi=0, cp=1, sp=0
x=y=0 and z<0 ==> phi=180, cp=-1, sp=0
x=y=z=0, theta=0, ct=1, st=0
_arg
alph = exp( i*alpha )
gamm = exp( i*gamma )
This sub returns atan2(imag_part, real_part), taking care with
numbers near zero
compute_beta
This is a straight translation of lines 32-114 in mpprmd.f. It
then sets the beta and eta attributes of the ScatteringPath
object and returns the beta angles. Most variable names were
chosen to be the same as in the fortran source.
=cut
# sub _trig {
# my ($x, $y, $z) = @_;
# my $EPSILON6 = 1e-6;
# my $rxysqr = $x*$x + $y*$y;
# my $r = sqrt($rxysqr + $z*$z);
# my $rxy = sqrt($rxysqr);
# my ($ct, $st, $cp, $sp) = (1, 0, 1, 0);
# ($ct, $st) = ($z/$r, $rxy/$r) if ($r > $EPSILON6);
# ($cp, $sp) = ($x/$rxy, $y/$rxy) if ($rxy > $EPSILON6);
# return ($ct, $st, $cp, $sp);
# };
sub _trig {
my $rxysqr = $_[0]*$_[0] + $_[1]*$_[1];
my $r = sqrt($rxysqr + $_[2]*$_[2]);
my $rxy = sqrt($rxysqr);
my ($ct, $st, $cp, $sp) = (1, 0, 1, 0);
($ct, $st) = ($_[2]/$r, $rxy/$r) if ($r > $EPSILON6);
($cp, $sp) = ($_[0]/$rxy, $_[1]/$rxy) if ($rxy > $EPSILON6);
return ($ct, $st, $cp, $sp);
};
sub _arg {
#my ($real, $imag) = @_;
#my $th = 0;
($_[0] = 0) if (abs($_[0]) < $EPSILON6);
($_[1] = 0) if (abs($_[1]) < $EPSILON6);
#if ((abs($real) > $EPSILON6) or (abs($imag) > $EPSILON6)) {
return atan2($_[1], $_[0]) if ($_[0] || $_[1]);
return 0;
};
## this sub is not necessarily as readable as possible. this is a big
## time-sink for the pathfinder, so I am trying any little tweak that
## doesn't break things to get a bit better performance out. in
## particular, I apologize for the confusing dereferencing in the
## lines with asite/bsite/csite
sub compute_beta {
my ($self, $feff, $string) = @_;
#my @sites = @{ $feff->sites };
my $rsites = $feff->sites;
my $ai = $feff->abs_index;
#my @atoms = split(/\./, $self->string);
my @atoms = split(/\./, $string);
$atoms[0] = $ai; # replace central atom tokens
$atoms[-1] = $ai;
## predefine variables so they do not re-instantiated during loops
my ($im1, $i, $ip1, @asite, @bsite, @csite, @vector, $ct, $st, $cp, $sp, $ctp, $stp, $cpp, $spp, $cppp, $sppp, $b);
my (@alpha, @beta, @gamma, @eta, @aleph, @gimel, @rleg);
$rleg[0] = 0;
$alpha[0] = 0;
$beta[0] = 0;
$gamma[0] = 0;
$eta[0] = 0;
$aleph[0] = [0,0];
$gimel[0] = [0,0];
foreach my $j (1 .. $#atoms) {
## nothing gets left undefined
#$alpha[$j] = 0;
#$beta[$j] = 0;
#$gamma[$j] = 0;
#$eta[$j] = 0;
#$aleph[$j] = [0,0];
#$gimel[$j] = [0,0];
($im1, $i, $ip1) = ($j-1, $j, $j+1);
if ($j == $#atoms) {
($im1, $i, $ip1) = ($j-1, 0, 1);
};# elsif ($j == $#atoms+1) {
# ($im1, $i, $ip1) = ($#atoms-1, $#atoms, 0);
# };
@asite = @{ $rsites->[$atoms[$im1]] }[0..2];
@bsite = @{ $rsites->[$atoms[$i ]] }[0..2];
@csite = @{ $rsites->[$atoms[$ip1]] }[0..2];
@vector = ( $csite[0]-$bsite[0], $csite[1]-$bsite[1], $csite[2]-$bsite[2]);
($ct, $st, $cp, $sp) = _trig(@vector);
@vector = ( $bsite[0]-$asite[0], $bsite[1]-$asite[1], $bsite[2]-$asite[2]);
$rleg[$j] = sqrt($vector[0]**2 + $vector[1]**2 +$vector[2]**2);
($ctp, $stp, $cpp, $spp) = _trig(@vector);
$cppp = $cp*$cpp + $sp*$spp;
$sppp = $spp*$cp - $cpp*$sp;
#my $phi = atan2($sp, $cp);
#my $phip = atan2($spp, $cpp);
$b = $ct*$ctp + $st*$stp*$cppp;
if ($b < -1) {
$beta[$j] = "180.0000";
} elsif ($b > 1) {
$beta[$j] = "0.0000";
} else {
$beta[$j] = sprintf("%.4f", 180 * acos($b) / $PI);
};
# $beta[$j] = $ct*$ctp + $st*$stp*$cppp;
# $beta[$j] = -1 if ($beta[$j] < -1); # care with roundoff
# $beta[$j] = 1 if ($beta[$j] > 1);
# #$beta[$j] = acos($beta[$j]);
# $beta[$j] = sprintf("%.4f", 180 * acos($beta[$j]) / $PI);
$aleph[$j] = [-$st*$ctp + $ct*$stp*$cppp, $stp*$sppp];
$gimel[$j] = [-$st*$ctp*$cppp + $ct*$stp, -$st *$sppp];
};
@asite = @{ $rsites->[$atoms[$#atoms]] }[0..2];
@bsite = @{ $rsites->[$atoms[0 ]] }[0..2];
@vector = ( $bsite[0]-$asite[0], $bsite[1]-$asite[1], $bsite[2]-$asite[2]);
$rleg[$#atoms+1] = sqrt($vector[0]**2 + $vector[1]**2 +$vector[2]**2);
#$alpha[0] = $alpha[$#atoms];
push @gimel, $gimel[0];
my $nonzero = 0;
foreach my $j (0 .. $#atoms) {
my $eer = ($aleph[$j]->[0] * $gimel[$j+1]->[0]) - ($aleph[$j]->[1] * $gimel[$j+1]->[1]);
my $eei = ($aleph[$j]->[1] * $gimel[$j+1]->[0]) + ($aleph[$j]->[0] * $gimel[$j+1]->[1]);
#my $ee = $aleph[$j] * $gimel[$j+1];
$eta[$j] = _arg($eer, $eei);
$eta[$j] = sprintf("%.4f", 180 * $eta[$j] / $PI);
($nonzero=1) if ($eta[$j] > $EPSILON6);
};
my $fs = 0;
foreach my $j (1 .. $#beta-1) {
++$fs if ($beta[$j] < $fsangle); # fsangle defined globally, near line 49
};
$self->rleg(\@rleg);
$self->beta(\@beta);
$self->betanotstraightish(1) if (any {($_ > $fsangle) and ($_ < (180-$fsangle))} @beta);
$self->eta(\@eta);
$self->etanonzero($nonzero);
$self->fs($fs);
return @beta;
};
## degeneracy checking
sub compare {
my ($self, $other) = @_;
# croak("ScatteringPaths from different Feff objects") if ($self->feff ne $other->feff);
my $feff = $self->feff;
## compare path lengths
return "lengths different" if ( abs($self->halflength - $other->halflength) > $feff->fuzz );
my @sites = @{ $feff->sites };
## compare number of legs and ipots
my @this = split(/\./, $self->string);
shift @this; pop @this;
my @that = split(/\./, $other->string);
shift @that; pop @that;
## number of legs
return "nlegs different" if ($#this != $#that);
if ($#{$self->ipot} > -1) { ## this is an Aggregate calculation
#print $/, '>> ', join("|", @{$self->ipot}, $self->halflength), $/;
#print '<< ', join("|", @{$other->ipot}), $/;
my @ipot_compare = pairwise {$a == $b} @{$self->ipot}, @{$other->ipot};
if (notall {$_} @ipot_compare) { # time reversal
my @that = reverse(@{$other->ipot});
@ipot_compare = pairwise {$a == $b} @{$self->ipot}, @that;
return "ipots different" if (notall {$_} @ipot_compare);
};
} else { ## this is a normal Feff calculation
## ipots
my @this_ipot = map { ($_ eq '+') ? 0 : $sites[$_] -> [3] } @this;
my @that_ipot = map { ($_ eq '+') ? 0 : $sites[$_] -> [3] } @that;
my @ipot_compare = pairwise {$a == $b} @this_ipot, @that_ipot;
if (notall {$_} @ipot_compare) { # time reversal
##($that_ipot[0], $that_ipot[-1]) = ($that_ipot[-1], $that_ipot[0]);
@that_ipot = reverse @that_ipot;
@ipot_compare = pairwise {$a == $b} @this_ipot, @that_ipot;
return "ipots different" if (notall {$_} @ipot_compare);
};
};
## beta angles
@this = @{ $self ->beta };
@that = @{ $other->beta };
my $bfuzz = $feff->betafuzz;
return "nlegs different" if ($#this != $#that);
my @angle_compare = pairwise { abs($a - $b) < $bfuzz } @this, @that;
if (notall {$_} @angle_compare) { # time reversal
($that[0], $that[-1]) = ($that[-1], $that[0]);
@that = reverse @that;
@angle_compare = pairwise { abs($a - $b) < $bfuzz } @this, @that;
return "betas different" if (notall {$_} @angle_compare);
};
## eta angles
my @this_eta = @{ $self ->eta };
my @that_eta = @{ $other->eta };
my @eta_compare = pairwise { abs(abs($a) - abs($b)) < $feff->betafuzz } @this_eta, @that_eta;
if (notall {$_} @eta_compare) { # time reversal
($that_eta[0], $that_eta[-1]) = ($that_eta[-1], $that_eta[0]);
@that_eta = reverse @that_eta;
@eta_compare = pairwise { abs(abs($a) - abs($b)) < $feff->betafuzz } @this_eta, @that_eta;
return "etas different" if (notall {$_} @eta_compare);
};
## polarization angles
return "polarization angle product different" if (abs($self->cosinout - $other->cosinout) > $EPSILON5);
#$self->set(fuzzy=>$fuzzy) if ( abs($self->halflength - $other->halflength) > $EPSILON5 );
return q{};
};
## ----------------------------------------------------------------
## textual reporting methods
=for Explanation (pathsdat)
pathsdat writes out a paragraph in the format read from the paths.dat file by genfmt
args is a hash reference with these keys:
index: the numerical index of this path in the sense of NNNN from feffNNNN.dat
angles: a boolean, true measn to write out the rleg, beta, and eta columns to the
paths.dat file. note that those columns are optional -- feff does not read them
string: the string (in the sense of the ScatteringPath string method) to be used to expand
into a paths.dat paragraph. use the all_strings method to get the strings of
all degeneracies for this SP object
=cut
sub pathsdat {
my ($self, @arguments) = @_;
my %args = @arguments;
$args{index} ||= 1;
$args{angles} = 1 if (not defined($args{angles}));
$args{string} ||= $self -> string;
$self -> randstring(Demeter->randomstring(9).'.sp') if ($self->randstring =~ m{\A\s*\z});
my $feff = $self->feff;
my @sites = @{ $feff->sites };
my $pd = q{};
$pd .= sprintf(" %4d %d %6.3f index, nleg, degeneracy, r= %.4f\n",
$args{index}, $self->get(qw(nleg n fuzzy)) );
$pd .= " x y z ipot label";
$pd .= " rleg beta eta" if ($args{angles});
$pd .= "\n";
my @atoms = split(/\./, $args{string});
shift @atoms; pop @atoms;
my $i=1;
my ($rrleg, $rbeta, $reta) = $self->get(qw(rleg beta eta));
my @c = $feff->central;
foreach my $a (@atoms) {
my @coords = @{ $sites[$a] };
## use fuzzy length for fuzzily degenerate paths, need to scale coordinates
my $scale = $self->fuzzy / $self->halflength;
foreach my $j (0..2) {
$coords[$j] = $c[$j] + ($coords[$j]-$c[$j])*$scale;
};
## this bit o' yuck gets a tag from the potentials list entry if not in the sites list
$coords[4] ||= $feff->site_tag($a);
$coords[4] =~ s{$FEFFNOTOK}{}g; # scrub characters that will confuse Feff
$pd .= sprintf(" %11.6f %11.6f %11.6f %d '%-6s'", @coords);
$pd .= sprintf(" %9.4f %9.4f %9.4f",$rrleg->[$i], $rbeta->[$i], $reta->[$i]) if $args{angles};
$pd .= "\n";
++$i;
};
$pd .= sprintf(" %11.6f %11.6f %11.6f %d '%-6s'", $feff->central, 0, 'abs');
$pd .= sprintf(" %9.4f %9.4f %9.4f", $rrleg->[$i], $rbeta->[$i], $reta->[$i]) if $args{angles};
$pd .= "\n";
return $pd;
};
sub details {
my ($self, $string) = @_;
my $feff = $self->feff;
my @list_of_sites = @{ $feff->sites };
my @list_of_ipots = @{ $feff->potentials };
my $pathstring = $string || $self->string;
my @this_path = split(/\./, $pathstring);
shift @this_path; pop @this_path;
my @ipots = map { $list_of_sites[$_]->[3] || -1 } @this_path;
my @tags = map { $list_of_sites[$_]->[4] || q{} } @this_path;
my @elems = map { get_symbol($list_of_ipots[$_]->[1]) || q{} } @ipots;
return (ipots=>\@ipots, tags=>\@tags, elements=>\@elems);
};
sub all_strings {
my ($self) = @_;
return @{ $self->degeneracies };
};
sub all_degeneracies {
my ($self) = @_;
my @dlist = @{ $self->degeneracies };
return
map {
my $this = $self->intrplist($_);
$this =~ s{ +}{ }g;
$this;
} @{ $self->degeneracies };
};
=for LiteratureReference (identify_path)
And out of the ground the LORD God formed every beast of the field,
and every fowl of the air; and brought them unto Adam to see what he
would call them: and whatsoever Adam called every living creature,
that was the name thereof.
Genesis 2:19, KJB
=cut
=for Explanation
An obtuse triangle has one internal angle larger than 90° (an obtuse
angle).
.
An acute triangle has internal angles that are all smaller than 90°
(three acute angles). An equilateral triangle is an acute triangle,
but not all acute triangles are equilateral triangles.
from Wikipedia
=cut
sub identify_path {
my ($self) = @_;
my ($nleg, $feff) = ($self->nleg, $self->feff);
my @beta = @{ $self->beta };
my ($type, $weight) = (q{}, 0);
TYPE: {
($nleg == 2) and do {
($weight, $type) = (2, "single scattering");
last TYPE;
};
(($nleg == 3) and (any {($_ < $ncangle)} @beta[1..2]) ) and do {
($weight, $type) = (2, "forward scattering");
last TYPE;
};
(($nleg == 3) and (any {($_ > (180-$ncangle))} @beta[1..2]) ) and do {
($weight, $type) = (2, "non-forward linear");
last TYPE;
};
(($nleg == 4) and ($beta[2] < $ncangle) and (all {($_ == 180)} ($beta[1],$beta[3])) ) and do {
($weight, $type) = (2, "forward through absorber");
last TYPE;
};
(($nleg == 4) and (all {($_ < $ncangle)} ($beta[1],$beta[3])) ) and do {
($weight, $type) = (2, "double forward scattering");
last TYPE;
};
(($nleg == 3) and (all {$_ >= (180-$rtangle)} @beta[1..3])) and do {
($weight, $type) = (1, "acute triangle");
last TYPE;
};
(($nleg == 4) and (all {$_ == 180} @beta[1..3])) and do {
($weight, $type) = (1, "rattle");
last TYPE;
};
(($nleg == 3) and (any {($_ < $rtangle) and ($_ > $fsangle)} @beta[1..2]) ) and do {
($weight, $type) = (1, "obtuse triangle");
last TYPE;
};
(($nleg == 3) and (any {($_ < $rtangle) and ($_ > $fsangle)} @beta[2..3]) ) and do {
($weight, $type) = (1, "obtuse triangle");
last TYPE;
};
(($nleg == 3) and (any {($_ < $fsangle) and ($_ > $ncangle)} @beta[1..2]) ) and do {
($weight, $type) = (1, "forward triangle");
last TYPE;
};
(($nleg == 4) and (all {($_ < $fsangle) and ($_ > $ncangle)} ($beta[1],$beta[3])) ) and do {
($weight, $type) = (1, "forward triangle");
last TYPE;
};
(($nleg == 4) and ($beta[2] != 180) and (all {$_ == 180} ($beta[1],$beta[3]))) and do {
($weight, $type) = (0, "hinge");
last TYPE;
};
(($nleg == 4) and ($beta[2] == 180) and (all {$_ != 180} ($beta[1],$beta[3]))) and do {
($weight, $type) = (0, "dog-leg");
last TYPE;
};
(($nleg == 4) and (all {($_ < $rtangle) and ($_ > $fsangle)} ($beta[1],$beta[3])) ) and do {
($weight, $type) = (0, "obtuse triangle");
last TYPE;
};
(($nleg == 5) and (any {($_ < $ncangle)} @beta[1..2]) ) and do {
($weight, $type) = (2, "5-legged forward scattering");
last TYPE;
};
(($nleg == 6) and (any {($_ < $ncangle)} @beta[1..2]) ) and do {
($weight, $type) = (2, "6-legged forward scattering");
last TYPE;
};
($nleg == 3) and do {
($weight, $type) = (0, "other double scattering");
last TYPE;
};
($nleg == 4) and do {
($weight, $type) = (0, "other triple scattering");
last TYPE;
};
($nleg == 5) and do {
($weight, $type) = (0, "other 5-legged scattering");
last TYPE;
};
($nleg == 6) and do {
($weight, $type) = (0, "other 6-legged scattering");
last TYPE;
};
};
$self -> weight($weight);
$self -> Type($type);
return $self;
};
## ----------------------------------------------------------------
## methods required by the Heap module
sub heap {
my ($self, $value) = @_;
if ($value) {
$self->heapvalue($value);
return 1;
} else {
return $self->heapvalue;
};
};
sub cmp {
my ($self, $other) = @_;
return $self->halflength <=> $other->halflength
||
$self->nleg <=> $other->nleg
||
$self->etakey cmp $other->etakey
||
$self->betakey cmp $other->betakey
||
$self->nkey <=> $other->nkey
||
$self->cosinout <=> $other->cosinout;
};
__PACKAGE__->meta->make_immutable;
1;
=head1 NAME
Demeter::ScatteringPath - Create and manipulate scattering paths
=head1 VERSION
This documentation refers to Demeter version 0.9.26.
=head1 SYNOPSIS
$sp_object -> new(feff=>$feff, string=>$string);
$sp_object -> evaluate;
Those are the only two attributes provided to the object. Everything
else is computed from those two.
=head1 DESCRIPTION
This object handles the abstract representation of the scattering
path. This is part of Demeter's theory subsystem, unlike
Demeter::Path, which is part of Demeter's fitting subsytem. This
object is used by Demeter::Feff's path finder and
just-in-time path calculator.
This object requires that a Feff object has already been created and
fully populated. That is either done by the Feff object's C<rdinp>
method or in some other manner. These objects are created during the
Feff object's C<pathfinder> method. Once a tree of scattering paths is
created from the Feff object's atoms list, the tree is traversed and a
ScatteringPath object is made from each visitation of the tree. The
tree is completely depopulated, transfering each ScatteringPath object
to a heap. ScatteringPaths are removed from the heap and placed onto
a well-ordered list of paths. As the list is created, the paths are
collapsed by degeneracy.
Although you may interact with ScatteringPath objects extensively in
your programs, typically creation is left up to the Feff object's
C<pathfinder> method. Similarly, you will find that you rarely C<set>
attributes, but often C<get> them.
=head1 ATTRIBUTES
=over 4
=item C<feff>
A reference to the Feff object from which this ScatteringPath was
created. C<parent> is an alias for C<feff>.
=item C<string>
A string denoting the route this path takes through the cluster. This
string has a very specific form. The first and last tokens in the
string represent the absorber and can be almost anything. A plus sign
(+) is typical. Each intermediate token is the index in the Feff
objects atoms list for that atom in the scattering path. The tokens
are joined by dots. For example, a ScatteringPath that represents the
path from the absorber to the 7th atom in the list to the 23rd atom in
the list and back (a double scattering path) would have this string:
+.7.23.+
These numbers are interpreted by referring to the Feff object
contained in the feff attribute. Typically, the value of this
attribute is the first degenerate scattering geometry found by the
path finder.
=item C<nkey>
This is a integer constructed from the atoms indeces that is used to
sort the scattering paths in the heap a predictable manner.
=item C<rleg>
This is a reference to a list of path lengths in the path.
=item C<beta>
This is a reference to a list of beta angle in the path.
=item C<eta>
This is a reference to a list of eta angle in the path.
=item C<nleg>
This is the number of legs in the path, stored for easy reference.
=item C<halflength>
This is the half path length of this path. This is the primary
sorting criterion.
=item C<heapvalue>
This is a value required by and used by the Heap algorithm. It is
computed using the halflength class method from L<Demeter::Tools>.
=item C<n>
This is the degeneracy of this path after the paths have been collapsed.
=item C<degeneracies>
This is a reference to a list containing the string attribute of each
ScatteringPath object that was collapsed into this one.
=item C<fuzzy>
This is the fuzzy path length. It is set to the average of the
lengths of the nearly degenerate paths. For truly degenerate paths,
the half length and the fuzzy length will be the same.
=back
=head1 METHODS
=head2 Accessor methods
The accessor methods of the parent class, C<get> and C<set> are used
my this class.
=over 4
=item C<attributes>
This returns a list of all ScatteringPath object attributes.
print join(" ", $sp -> attributes), $/;
==prints==>
nleg string heapvalue group nkey weight etanonzero rleg fs
degeneracies n randstring beta eta feff halflength fuzzy type
=item C<savelist>
This returns a list containing the subset of all ScatteringPath object
attributes that need to be saved when a Feff calculation is serialized.
print join(" ", $sp -> savelist), $/;
==prints==>
nleg string group nkey weight etanonzero rleg fs degeneracies
n randstring beta eta halflength fuzzy type
=back
=head2 Evaluation methods
Once a ScatteringPath object if defined by the C<new> method and given
feff and string attributes, the object must be evaluated. For reasons
of efficiency, the evaluation is not done automatically, so the
example given for the C<evaluate> method should become your common
idiom for using this object.
=over 4
=item C<evaluate>
This method sets most attributes for the object based on the values of
the feff and string attributes. It calls the remaining methods in
sequence.
$sp_object -> new(feff=>$feff, string=>$string);
$sp_object -> evaluate;
=item C<compute_nleg_nkey>
Determine the number of legs of this path and compute the nkey from
the atoms in this path. Set the nleg and nkey attributes.
=item C<compute_halflength>
Determine the half path length of this path and set the halfpath
attribute. Note that the Demeter::Tools::halflength class method is
used to compute this.
=item C<compute_beta>
Compute the beta and eta angles for this path and set the beta
and eta attributes.
=back
=head2 Textual reporting methods
=over 4
=item C<pathsdat>
This method writes out a paragraph in the format read from the
paths.dat file by genfmt
print $sp_object -> pathsdat(\%args);
This method takes an optional argument which is a hash reference. The
hash has can have these keys:
=over 4
=item I<index>
the numerical index of this path in the sense of NNNN from feffNNNN.dat
=item I<angles>
a boolean, true measn to write out the rleg, beta, and eta columns to
the paths.dat file. note that those columns are optional -- feff does
not read them
=item I<string>
the string (in the sense of the ScatteringPath string method) to be
used to expand into a paths.dat paragraph. use the all_strings method
to get the strings of all degeneracies for this SP object
=back
=item C<all_strings>
This method returns a list of text strings of the sort returned by the
C<string> method. These can be used to reconstruct the geometry of
any of the degenerate paths subsumed into this ScatteringPath object.
@strings = $sp_object -> all_strings;
=item C<all_degeneracies>
This returns a list of text strings. The list contains the list from
the C<all_strings> method with each list element passed through the
C<intrplist> method.
=item C<intrplist>
Compute the interpretation line from the string attribute. This looks
something like this:
[+] O_1 Ti_1 O_1 [+]
With no argument, this returns the interpretation line for the primary
path:
print $sp_object -> intrplist;
Alternately, you can provide a string from the list returned by the
C<all_strings> method to generate the interpretation line for that
degenerate path:
print $sp_object -> intrplist($some_string);
=back
=head2 Heap methods
These are the two methods required by the L<Heap> module.
=over 4
=item C<heap>
This is used to set and access the heapvalue attribute.
=item C<cmp>
This is the comparison method used to order the items on the heap. It
sorts first by half path length, then by nleg, then by the output of
the C<betakey> method, and finally by the nkey.
In practice this means that paths are ordered by increasing path
length, then by increasing nleg. In the case of collinear paths, it
is guaranteed that the single scattering paths will come before the
doubles which will come before the triples. The sort by beta assures
that forward scattering paths come before paths which scatter at other
angles.
Finally, the nkey portion of the sort makes it clear which path from a
degenerate set will be selected as the representative path. This sort
is done in the order the atoms appear in the Feff object's atoms list.
For example, for the single scattering from the first shell, the
representative path will always be the one that scatters from the
first atom from the first coordination shell to appear in the atoms
list.
=back
=head1 COERCIONS
When the reference to the ScatteringPath object is used in string
context, it returns the group name, like other Demeter objects.
When the reference to the ScatteringPath object is used in numerical
context, it returns the half length of the path from the C<halfpath>
method.
=head1 DIAGNOSTICS
=over 4
=item C<Demeter::ScatteringPath: \"$key\" is not a valid parameter>
You have tried to set or get an invalid ScatteringPath attribute.
=item C<Demeter::ScatteringPath::compute_halflength: feff and string attributes unset>
You have attempted to compute a halflength without defining the path geometry.
=item C<ScatteringPaths from different Feff objects>
You have attempted to compare ScatteringPath objects associated with
different Feff objects.
=back
=head1 CONFIGURATION AND ENVIRONMENT
See L<Demeter::Config> for a description of the configuration
system. The C<pathfinder> parameter group is used to configure the
behavior of this module.
=head1 DEPENDENCIES
The dependencies of the Demeter system are listed in the
F<Build.PL> file.
=head1 BUGS AND LIMITATIONS
A few features have not yet been implemented:
=over 4
=item *
It is currently very awkward to get a F<feffNNNN.dat> written for one
of the degeneracies associated with a ScatteringPath.
=item *
Final eta angle is not computed correctly
=item *
Amplitude approximation
=item *
polarization
=item *
changing species of an atom in a path
=back
And testing has been limited.
Please report problems to the Ifeffit Mailing List
(L<http://cars9.uchicago.edu/mailman/listinfo/ifeffit/>)
Patches are welcome.
=head1 AUTHOR
Bruce Ravel, L<http://bruceravel.github.io/home>
L<http://bruceravel.github.io/demeter/>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2006-2019 Bruce Ravel (L<http://bruceravel.github.io/home>). All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlgpl>.
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.
=cut
|