File: Paths.pm

package info (click to toggle)
libdemeter-perl 0.9.27%2Bds6-9
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 74,028 kB
  • sloc: perl: 73,233; python: 2,196; makefile: 1,999; ansic: 1,368; lisp: 454; sh: 74
file content (404 lines) | stat: -rw-r--r-- 13,717 bytes parent folder | download
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
package Demeter::Feff::Paths;

=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

use Carp;
use Chemistry::Elements qw(get_Z);
use List::MoreUtils qw(all none pairwise);

use Moose::Role;
use Demeter::NumTypes qw( Ipot PosNum );

sub find_path {
  my ($self, @params) = @_;
  ## coerce arguments into a hash
  my %params = @params;

  ## -------- recognize singular and plural for nlegs
  ($params{nlegs}    = $params{nleg})      if (exists($params{nleg})      and not exists($params{nlegs}));
  ## -------- recognize singular and plural for list-valued criteria
  ($params{tag}      = $params{tags})      if (exists($params{tags})      and not exists($params{tag}));
  ($params{tagmatch} = $params{tagsmatch}) if (exists($params{tagsmatch}) and not exists($params{tagmatch}));
  ($params{ipot}     = $params{ipots})     if (exists($params{ipots})     and not exists($params{ipot}));
  ($params{element}  = $params{elements})  if (exists($params{elements})  and not exists($params{element}));

  ## -------- leave nothing undefined
  $params{sp}       ||= 0;
  $params{gt}       ||= 0;
  $params{lt}       ||= 0;
  $params{tagmatch} ||= 0;
  $params{tag}      ||= 0;
  $params{element}  ||= 0;
  $params{ipot}     ||= 0;
  $params{nlegs}    ||= 0;

  carp("\$feff->find_path : the sp criterion must be a ScatteringPath object\n\n"), return 0 if ($params{sp} and (ref($params{sp}) !~ m{ScatteringPath}));
  carp("\$feff->find_path : the lt criterion must be a positive number\n\n"),       return 0 if ($params{lt} and not is_PosNum($params{lt}));
  carp("\$feff->find_path : the gt criterion must be a positive number\n\n"),       return 0 if ($params{gt} and not is_PosNum($params{gt}));

  ## -------- scalar valued tests need to be made into array refs
  if ($params{tag} and (ref($params{tag}) ne 'ARRAY')) {
    $params{tag} = [ $params{tag} ];
  };
  if ($params{tagmatch} and (ref($params{tagmatch}) ne 'ARRAY')) {
    $params{tagmatch} = [ $params{tagmatch} ];
  };
  if ($params{ipot} and (ref($params{ipot}) ne 'ARRAY')) {
    $params{ipot} = [ $params{ipot} ];
  };
  if ($params{element} and (ref($params{element}) ne 'ARRAY')) {
    $params{element} = [ $params{element} ];
  };

  my $list_defined = $params{tag} || $params{tagmatch} || $params{ipot} || $params{element};

  if ($params{ipot}) {
    my $ipots_ok = 1;
    foreach my $i (@{ $params{ipot} }) {
      $ipots_ok &&= is_Ipot($i);
    };
    carp("\$feff->find_path : each part of the ipot criterion must be an integer between 0 and 7\n\n"), return 0 if not $ipots_ok;
  };
  if ($params{element}) {
    my $elements_ok = 1;
    foreach my $e (@{ $params{element} }) {
      $elements_ok &&= get_Z($e);
    };
    carp("\$feff->find_path : each part of the element criterion must be an element name, symbol, or Z number\n\n"), return 0 if not $elements_ok;
  };

  my @list_of_paths = @{ $self->pathlist };
  return $list_of_paths[0] if none {$params{$_}} (keys %params);

  my @list_of_sites = @{ $self->sites };
  my @ipots = @{ $self->potentials };

 PATHS: foreach my $p (@list_of_paths) {

    my $is_the_one = 1;

    $is_the_one &&= ($p->fuzzy > $params{sp}->fuzzy) if $params{sp};
    $is_the_one &&= ($p->nleg == $params{nlegs})     if $params{nlegs};
    $is_the_one &&= ($p->fuzzy > $params{gt})        if $params{gt};
    $is_the_one &&= ($p->fuzzy < $params{lt})        if $params{lt};

    next if not $is_the_one;

    if (not $list_defined) {
      return $p if $is_the_one;
      next PATHS;
    };

  DEGEN: foreach my $d (@{ $p->degeneracies }) {
      my $ok_so_far = $is_the_one;
      my %hash = $p->details($d);
      # print join(" ", $d, @{ $hash{tags}     }), $/;
      # print join(" ", @{ $hash{ipots}    }), $/;
      # print join(" ", @{ $hash{elements} }), $/;

      ## only consider these tests if this path has the same number of
      ## legs as the path we are looking for
      my @this = @{ $hash{tags} };
      my @test = ($params{tag})      ? @{ $params{tag} }
	       : ($params{tagmatch}) ? @{ $params{tagmatch} }
	       : ($params{ipot})     ? @{ $params{ipot} }
	       : ($params{element})  ? @{ $params{element} }
	       :                       () ;
      next PATHS if ($#this != $#test);

      $ok_so_far &&= all {$_} ( pairwise {$a eq $b}     @{ $hash{tags} },     @{ $params{tag} } )
	if $params{tag};

      $ok_so_far &&= all {$_} ( pairwise {($a =~ m{$b}i) ? 1 : 0} @{ $hash{tags} },     @{ $params{tagmatch} } )
	if $params{tagmatch};

      $ok_so_far &&= all {$_} ( pairwise {$a == $b}     @{ $hash{ipots} },    @{ $params{ipot} } )
	if $params{ipot};

      $ok_so_far &&= all {$_} ( pairwise {lc($a) eq lc($b)}     @{ $hash{elements} }, @{ $params{element} } )
	if $params{element};

      return $p if $ok_so_far;
    };
  };
  return 0;
};

sub find_all_paths {
  my ($self, @params) = @_;
  my @list_of_paths;

  my $path = $self->find_path(@params);
  while ($path) {
    push @list_of_paths, $path;
    my $next = $self->find_path(@params, sp=>$path);
    $path = $next;
  };

  return @list_of_paths;
};

sub find_nearest {
  my ($self, @params) = @_;
  my %params = @params;
  my $reff = $params{reff};
  delete $params{reff};
  my @list = $self->find_all_paths(%params);
  my ($this, $diff) = (q{}, 100000);
  foreach my $sp (@list) {
    if (abs($sp->fuzzy-$reff) < $diff) {
      $this = $sp;
      $diff = abs($sp->fuzzy-$reff);
    };
  };
  return $this;
};

1;

=head1 NAME

Demeter::Feff::Paths - Semantic descriptions of Feff paths

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

    $feff -> Demeter::Feff -> new(file=>"feff.inp");
    $feff -> potph;
    $feff -> pathfinder;
    my $scatteringpath = $feff->find_path(lt=>3.5, tag=>'Dy');

=head1 DESCRIPTION

This role for the Feff object provides a mechanism for semantically
describing paths from a Feff calculation.  The example in the synopsis
finds the single scattering path that is less than 3.5 Angstroms long
and scatters from a Dysprosium atom.

This way of interacting with the Feff calculation really shines in a
situation where structural distortions or modifications are introduced
into Feff's input data.  Because Feff orders its paths by increasing
half path length, the path index is not a reliable way of keeping
track of a particular scattering geometry as different structural
modifications are introduced.  Semantic mechanisms of probing the Feff
calculation allow you to keep track of particular geometries
regardless of the fine details of the strcuture provided to Feff.

=head1 METHODS

=over 4

=item C<find_path>

This method searches through a Feff calculation for a path that
matches B<each> of the provided criteria.  The first such path is the
one that returned.  The degeneracies associated with each path are
also searched.  Thus order does not matter for the list-valued
criteria and fuzzily degenerate paths will be found correctly.

This method returns 0 if a path meeting the criteria cannot be found
or if there is an error in specifying the criteria.

The following criteria are available:

=over 4

=item C<lt>

This criterion takes a number and requires that the returned path be
shorter in half path length than that number.

=item C<gt>

This criterion takes a number and requires that the returned path be
longer in half path length than that number.

=item C<nleg>

This criterion takes an integer and requires that the returned path
have this number of scattering legs.  This criterion is redundant when
the list-valued criteria are also used.

=item C<sp>

This criterion takes a ScatteringPath object and requires that the
returned path be longer in half path length than that path.

=item C<tag>

This criterion takes a string or an anonymous array of strings and
requires that the returned path contain the atoms which have this
(these) tags.  The tag is the optional fifth column in a F<feff.inp>
file written by Demeter's Atoms.  This criterion requires that the
tags are lexically equal.  See the C<tagmatch> criterion for matching
tags in the sense of regular expressions.

=item C<tagmatch>

This criterion takes a string or an anonymous array of strings and
requires that the returned path contain the atoms which have tags
matching the arguments of this criterion.  The tag is the optional
fifth column in a F<feff.inp> file written by Demeter's Atoms.  This
criterion requires that the tags match in the sense of regular
expressions.  See the C<tag> criterion for requiring that tags be
lexically equal.

=item C<ipot>

This criterion takes an integer from 0 to 7 or an anonymous array of
such integers and requires that the returned path contain the atoms
which have ipots equal to the arguments of this criterion.

=item C<element>

This criterion takes a string identifying an element (its name, its
one or two letter symbol or its Z number) or an anonymous array of
such strings and requires that the returned path contain the atoms
which have the elements specified.

=back

For several of the criteria, care is taken to recognize singluar and
plural forms.  That is, C<nleg> and C<nlegs> are synonymous as
criteria.

The list-valued criterion are compared in order with the scattering
atoms in a path.  As an example, this

    my $scatteringpath = $feff->find_path(lt=>4, tag=>['Fe1', 'C1']);

would return the first double scattering path that scatters from a
site with the tag C<Fe1> then from a site with the tag C<C1> before
completing the loop and returning to the central atom.

Some more examples:

   my $scatteringpath = $feff->find_path(gt=>4, ipots=>[2]);

In this example, the scattering path will be the first one that is
longer than 4 Angstroms, is a single scattering path, and scatters
from unique potential number 2.

   my $nextscatteringpath = $feff->find_path(sp=>$scatteringpath);

In this example, the path returned will be the next longer single
scattering path than the previous example and which also scatters from
unique potential number 2.

   my $scatteringpath = $feff->find_path(lt=>5.3, element=>["O", "Ti", "O"]);

This will return the shortest triple scattering path shorter than 5.3
Angstroms which scatters from oxygen, then titanium, then oxygen.

   my $scatteringpath = $feff->find_path(tagmatch=>["O[1-3]"]);

This will return the shortest first path (with no other length
restriction) which scatters from a site whose tag matches the given
regular expression -- in this case it will match any of O1, O2, or O3.


=item C<find_all_paths>

This returns a list of paths meeting the same criteria as for the
C<find_path> method.  This is just a wrapper around the C<find_path>
method which sequentially uses the C<sp> criterion to find each
subsequent path meeting the input criteria.

    my @list = $feff->find_all_paths(lt=>6, element=>['Fe', 'C']);

This returns all double scattering paths which scatter from iron and
carbon atoms and are less than 6 Angstroms.  The order of paths in the
list is in order of increasing half path length.

Another example:

   my @list =  $feff->find_all_paths(lt=>6, nleg=>2);

This returns all single scattering paths less than 6 Angstroms.

An empty list is returned if no paths meet the criteria, so a logic
test can be performed on the results.

=item C<find_nearest>

This find the path that meets a semantic description and is closest in
path length to a specified value.

  my $scatteringpath = $feff->find_nearest(reff=>2.546, element=>['Fe']);

This example will find the single scattering path with Fe as the
scatterer which has a half path length closest to 2.546 Ang.  The
C<reff> is required.  All other arguments are the same as for
C<find_all_paths>.  In fact, this method calls C<find_all_paths> with
all arguments other than C<reff> then examines the list that gets
returned for the path closest to the given value.

This method returns the reference to the ScatteringPath object which
matches.

In the event that no paths are returned, the return value will be an
empty string.  Thus you can do a logical check on the success of the
method call.

In the event that multiple ScatteringPath objects meet the semantic
criteria and are of the same path length (which could happen for
multiple scattering paths), the first such path as ordered by
Demeter's pathfinder will returned.  If this is not the path that you
want, then you will have to search through the list returned by
C<find_all_paths> yourself.

=back

=head1 CONFIGURATION AND ENVIRONMENT

See L<Demeter::Config> for a description of the configuration
system.

=head1 DEPENDENCIES

The dependencies of the Demeter system are in the
F<Build.PL> file.

=head1 BUGS AND LIMITATIONS


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