File: Feffit.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 (543 lines) | stat: -rw-r--r-- 15,502 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
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
package Demeter::Fit::Feffit;

=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 autodie qw(open close);

use Moose;
extends 'Demeter';

use Cwd qw(realpath);
use File::Basename;
use File::Spec;
use List::MoreUtils qw(any);
use Regexp::Assemble;
use Demeter::Constants qw($NUMBER);
use Const::Fast;
const my %REPLACEMENT => ( e0     => 'enot',
			   ei     => 'eimag',
			   s02    => 's_02',
			   sigma2 => 'sigsqr',
			   third  => 'cumul3',
			   fourth => 'cumul4',
			   dr     => 'd_r',
			   dr1    => 'dr_1',
			   dr2    => 'dr_2',
			   dk     => 'd_k',
			   dk1    => 'dk_1',
			   dk2    => 'dk_2',
			   etok   => 'e2k',
			   pi     => 'pie',
			 );
## see line 488 and following in src/feffit/fitinp.f from the ifeffit source tree
const my %SYNONYMS => (path	  => 'path',
		       file	  => 'path',
		       feff	  => 'path',
		       id	  => 'id',
		       e0	  => 'e0',
		       esh	  => 'e0',
		       ee	  => 'e0',
		       e0s	  => 'e0',
		       s02	  => 's02',
		       so2	  => 's02',
		       amp	  => 's02',
		       sigma2  => 'sigma2',
		       ss2	  => 'sigma2',
		       delr	  => 'delr',
		       deltar  => 'delr',
		       ei	  => 'ei',
		       third	  => 'third',
		       '3rd'	  => 'third',
		       cubic	  => 'third',
		       fourth  => 'fourth',
		       '4th'	  => 'fourth',
		       quartic => 'fourth',
		      );

has 'file'    => (is => 'rw', isa => 'Str',  default => q{},
		  trigger => sub{shift -> Read} );
has 'cwd'     => (is => 'rw', isa => 'Str', default => q{});
has 'ndata'   => (is => 'rw', isa => 'Int', default => 0);

has 'e0'     => (is => 'rw', isa => 'Str', default => q{});
has 's02'    => (is => 'rw', isa => 'Str', default => q{});
has 'delr'   => (is => 'rw', isa => 'Str', default => q{});
has 'sigma2' => (is => 'rw', isa => 'Str', default => q{});

## feffit keywords
has 'all_re'       => (is => 'ro', isa => 'RegexpRef',
		       default => sub{ Regexp::Assemble->new()->add(qw(format formin formout rspout kspout qspout allout bkgfile out output
								       bkg data kmin kmax rmin rmax dk dk1 dk2 dr dr1 dr2 rlast kw kweight
								       nodegen noout nofit norun kspfit rspfit qspfit fit_space
								     ))->re });
has 'flag_re'      => (is => 'ro', isa => 'RegexpRef',
		       default => sub{ Regexp::Assemble->new()->add(qw(nodegen noout nofit norun kspfit rspfit qspfit))->re });
has 'kop_re'       => (is => 'ro', isa => 'RegexpRef',
		       default => sub{ Regexp::Assemble->new()->add(qw(kmin kmax dk dk1 dk2 kw kweight qmin qmax
								       dq dq1 dq2 qw qweight))->re });
has 'rop_re'       => (is => 'ro', isa => 'RegexpRef',
		       default => sub{ Regexp::Assemble->new()->add(qw(rmin rmax dr dr1 dr2 rlast))->re });
has 'ignore_re'    => (is => 'ro', isa => 'RegexpRef',
		       default => sub{ Regexp::Assemble->new()->add(qw(form format formin formout rspout kspout qspout allout
								       prmout pcout kfull fullk nerstp
								       bkgfile out output mftwrt mftfit mdocxx comment asccmt))->re });
has 'opparam_re'   => (is => 'ro', isa => 'RegexpRef',
		       default => sub{ Regexp::Assemble->new()->add(qw(bkg data kmin kmax rmin rmax dk dk1 dk2 dr dr1 dr2
								       kw kweight nodegen fit_space))->re });
has 'pathparam_re' => (is => 'ro', isa => 'RegexpRef',
		       default => sub{ Regexp::Assemble->new()->add(qw(path feff
								       id
								       e0 esh ee e0s
								       s02 so2 amp
								       sigma2 ss2
								       delr deltar
								       ei
								       third 3rd cubic
								       fourth 4th quartic))->re });
has 'comment_re'   => (is => 'ro', isa => 'RegexpRef',
		       default => sub{ qr([!#%]) });


my @gds  = ();
my @data = ();


sub BUILD {
  my ($self, @params) = @_;
  $self->mode->push_Feffit($self);
};

sub DEMOLISH {
  my ($self) = @_;
  $self->alldone;
};

sub Read {
  my ($self) = @_;
  my $file = $self->file;
  return 0 if not $file;
  if (not -e $file) {
    carp(ref($self) . ": $file does not exist\n\n");
    return -1;
  };
  if (not -r $file) {
    carp(ref($self) . ": $file cannot be read (permissions?)\n\n");
    return -1;
  };
  my ($name,$path,$suffix) = fileparse($file);
  $self->cwd($path);

  @gds  = ();
  @data = ();
  $data[0] = ({titles=>[], opparams=>{}, path=>[], feffcalcs=>[]});
  open *I, $file;
  $self->parse_file($path, *I);
  close *I;
  return $self;
};


sub parse_file {
  my ($self, $path, $file) = @_;
  while (<$file>) {
    next if m{^\s*$};		# blank lines
    next if m{^\s*[#!*%]};	# comment lines
    chomp;
    my $done = $self->parse_line($path, $_);
    return $self if $done;
  };
  return $self;
};

sub parse_line {
  my ($self, $path, $line) = @_;

  $line =~ s{^\s+}{};		# trim leading blanks
  $line =~ s{\#.*$}{};		# trim trailing comments
  $line =~ s{\s+$}{};		# trim trailing blanks
  my $flag = $self->flag_re;
  $line =~ s{($flag)}{$1=1};
  #$line = lc($line);

  my $all = $self->all_re;
  my $pp  = $self->pathparam_re;
 LINE: {
    ($line =~ m{\Anext}i) and do {
      $self->ndata($self->ndata+1);
      $data[$self->ndata] = ({titles=>[], opparams=>{}, path=>[], feffcalcs=>[]});
      last LINE;
    };

    ($line =~ m{\Atitle}i) and do {
      $line =~ s{\Atitle\s*[ \t=,]\s*}{}i;
      ## $line now contains the title line, push it onto titles list
      push @{ $data[$self->ndata]->{titles} }, $line;
      last LINE;
    };

    ($line =~ m{\A(?:guess|local|set)}i) and do {
      ## $line now contains the gds line, push it onto gds list
      push @gds, lc($line);
      last LINE;
    };

    ($line =~ m{\A(?:end|quit)}i) and do {
      return 1;
      last LINE;
    };

    ($line =~ m{^include}i) and do {
      $line =~ s{\Ainclude\s*[ \t=,]\s*}{}i;
      ## $line now contains the include file, call feffit_parse_file
      my $newfile = File::Spec->catfile($path,$line);
      open *INC, $newfile;
      $self->parse_file($path, *INC);
      close *INC;
      last LINE;
    };

    ($line =~ m{^($pp)\s*[ \t=,]\s*(\d+)\s*[ \t=,]\s*(.*)}i) and do {
      ## push this path parameter onto its list
      $self->parse_pathparam($line);
      last LINE;
    };

    ($line =~ m{^(?:$all)\s*[ \t=,]\s*}i) and do {
      $self->parse_opparam($line, $path);
      last LINE;
    };
  };
  return 0;
};

sub parse_pathparam {
  my ($self, $line) = @_;
  my $ppre  = $self->pathparam_re;
  $line =~ s{[#!%].*$}{};	# remove end of line comments
  $line =~ m{^($ppre)\s*[ \t=,]\s*(\d+)\s*[ \t=,]\s*(.*)}i;
  my ($pp, $index, $me) = ($1, $2, $3);
  $pp = $SYNONYMS{$pp};
  if ($index == 0) {
    foreach my $v (keys %REPLACEMENT) {
      ($me =~ s{\b$v\b}{$REPLACEMENT{$v}}g) if ($me =~ m{$v});
    };
    $self->$pp($me);
  } else {
    $data[$self->ndata]->{path}->[$index]->{$pp} = ($pp eq 'path') ? $me : lc($me);
  };
};

sub parse_opparam {
  my ($self, $line, $path) = @_;
  $line =~ s{[#!%].*$}{};	# remove end of line comments
  my $ig  = $self->ignore_re;
  my %words = split(/\s*[ \t=,]\s*/, $line);
  foreach my $key (keys %words) {
    next if (lc($key) =~ m{\A$ig\z}); # ignore some opparams
    ## store the good opparams for this data set
    if (lc($key) eq 'data') {
      my $datafile = File::Spec->catfile($path, $words{$key});
      $data[$self->ndata]->{opparams}->{data} = $datafile;
    } elsif (lc($key) eq 'kw') {
      $data[$self->ndata]->{opparams}->{$key} = int($words{$key});
    } elsif (lc($key) eq 'bkg') {
      if ($words{$key} =~ m{\A[1yt]}) {
	$data[$self->ndata]->{opparams}->{bkg} = 1;
      } else {
	$data[$self->ndata]->{opparams}->{bkg} = 0;
      };
    } else {
      $data[$self->ndata]->{opparams}->{$key} = $words{$key};
    };
  };
};

sub convert {
  my ($self, $path, $file) = @_;
  $self -> cull_mkw;

  #use Data::Dumper;
  #print Data::Dumper->Dump([\@data, \@gds], [qw(data gds)]);


  my $comment = $self->comment_re;
  my $kop     = $self->kop_re;
  my $rop     = $self->rop_re;

  my @list_of_gds;
  foreach my $g (@gds) {
    $g =~ s{$comment.*\z}{};	# strip comments
    $g =~ s{\s+\z}{};
    my ($gds, $name, @rest) = split(" ", $g);
    my $mathexp = join(" ", @rest);
    ($gds = 'def') if (($gds eq 'set') and ($mathexp !~ m{\A$NUMBER\z}));
    ($name = $REPLACEMENT{$name}) if (any {$name eq $_} keys(%REPLACEMENT));
    foreach my $v (keys %REPLACEMENT) {
      ($mathexp =~ s{\b$v\b}{$REPLACEMENT{$v}}g) if ($mathexp =~ m{$v});
    };
    push @list_of_gds, Demeter::GDS->new(gds=>$gds, name=>$name, mathexp=>$mathexp);
  };

  my @list_of_data;
  my @list_of_paths;
  my $index = 0;
  foreach my $d (@data) {
    next if not defined($d);
    ++$index;
    my $this_data = Demeter::Data->new();

    ## -------- set title lines
    $this_data->titles($d->{titles});

    ## -------- set operational parameters
    my $nodegen = 0;
    foreach my $o (keys %{ $d->{opparams} }) {
      #print $o, "  ", $d->{opparams}->{$o}, $/;

    OP: {
	($o eq "nodegen") and do {
	  $nodegen = $d->{opparams}->{$o};
	  last OP;
	};

	($o eq "data") and do {
	  $file = realpath($d->{opparams}->{$o});
	  $this_data->file($file);
	  $this_data->name(basename($file, ".dat", ".xmu", ".chi"));
	  last OP;
	};

	($o eq "bkg") and do {
	  $this_data->fit_do_bkg($d->{opparams}->{$o});
	  last OP;
	};

	($o =~ m{\A([kqr])spfit\z}) and do {
	  $this_data->fit_space($1);
	  last OP;
	};
	($o eq 'fit_space') and do {
	  $this_data->fit_space($d->{opparams}->{$o});
	  last OP;
	};

	($o =~ m{\Akw}) and do {
	  $this_data->fit_k1(1) if ($d->{opparams}->{$o} =~ m{1});
	  $this_data->fit_k2(1) if ($d->{opparams}->{$o} =~ m{2});
	  $this_data->fit_k3(1) if ($d->{opparams}->{$o} =~ m{3});
	  last OP;
	};

	($o =~ m{\A(?:$kop)\z}) and do{
	  (my $oo = $o) =~ s{q}{k};
	  my $att = ($oo =~ m{dk}) ? 'fft_dk' : "fft_$o";
	  $this_data->$att($d->{opparams}->{$o});
	  last OP;
	};

	($o =~ m{\A(?:$rop)\z}) and do{
	  my $att = ($o =~ m{dr}) ? 'bft_dr' : "bft_$o";
	  $this_data->$att($d->{opparams}->{$o});
	  last OP;
	};
      };
    };

    ## -------- set paths and path parameters
    foreach my $p (@{ $d->{path} }) {
      next if not defined($p);
      my $this_path = Demeter::Path->new(data=>$this_data);
      my %is_set = (e0=>0, s02=>0, delr=>0, sigma2=>0);
      foreach my $pp (keys %$p) {
	$is_set{$pp} = 1;
      PP: {
	  ($pp eq 'path') and do {
	    my ($file, $folder) = fileparse($p->{$pp});
	    $this_path->set(folder=>File::Spec->catfile($self->cwd, $folder), file=>$file);
	    last PP;
	  };

	  ($pp eq 'id') and do {
	    $this_path->id($p->{id});
	    last PP;
	  };

	  my $me = $p->{$pp};
	  foreach my $v (keys %REPLACEMENT) {
	    ($me =~ s{\b$v\b}{$REPLACEMENT{$v}}) if ($me =~ m{$v});
	  };
	  $this_path->$pp($me);

	};
      };
      $this_path->n(1) if $nodegen;
      foreach my $def (qw(e0 s02 delr sigma2)) {
	$this_path->$def($self->$def) if (not $is_set{$def});
      };
      push @list_of_paths, $this_path;
      $this_path->name("path ".($#list_of_paths+1));
    };

    push @list_of_data, $this_data;
  };

  my $fit = Demeter::Fit->new(
			      gds   => \@list_of_gds,
			      data  => \@list_of_data,
			      paths => \@list_of_paths,
			     );
  return $fit;
};


## this does not work for an MDS + MKW fit -- need an outer loop
sub cull_mkw {
  my ($self) = @_;
  my $first_data = $data[0]->{opparams}->{data};
  foreach my $i (1 .. $self->ndata) {
    my $this_data = $data[$i]->{opparams}->{data};
    next if ($this_data ne $first_data);
    my $first_kw = $data[0]->{opparams}->{kw};
    $first_kw .= ',' . $data[$i]->{opparams}->{kw};
    $data[0]->{opparams}->{kw} = $first_kw;
    $data[$i] = undef;
  };
};

__PACKAGE__->meta->make_immutable;
1;


=head1 NAME

Demeter::Fit::Feffit - Convert a feffit.inp file into a Fit object

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 DESCRIPTION

Convert an old-style F<feffit.inp> file into the equivalent Demeter
Fit object.

   $inp = Demeter::Fit::Feffit->new(file=>$inpfile);
   $fit = $inp -> convert;
   $fit -> fit;

This can be used to drive a Demeter-based Feffit act-alike or it can
be used, along with the demeter template set, to convert a
F<feffit.inp> into a Demeter-based perl script.

Most features of Feffit are handled correctly, including C<include>
files.  A few of the more obscure Feffit options are not handled at
all.  Several Feffit options do not have Ifeffit or Demeter analogs,
and so are ignored.  All the various synonyms of the feffit path
parameters are correctly recognized, however this makes no attempt to
follow the same (somewhat erratic) abbreviations of some parameters
recognized by Feffit.

=head1 METHODS

Simply point this class at a F<feffit.inp> file by specifying the
C<file> attribute at creation or via it's standard Moose accessor:

   $inp = Demeter::Fit::Feffit->new(file=>$inpfile);
     or
   $inp = Demeter::Fit::Feffit->new;
   $inp->file($inpfile);

The F<feffit.inp> file will be parsed into an intermediate data
structure when the C<file> attribute is set.

The only outward looking method is C<convert>, which turns the
intermediate data structure into a Fit object.  See L<Demeter::Fit>
for what to do with that.

=head1 CONFIGURATION

There are no configuration options for this class.

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

=head1 DEPENDENCIES

Demeter's dependencies are in the F<Build.PL> file.

=head1 BUGS AND LIMITATIONS

=over 4

=item *

The algorithm for recognizing a multiple k-weight fit in the
F<feffit.inp> file is somewhat simplistic.  If a "multiple data set"
fit (in the sense that the C<next data set> keyword is in the
F<feffit.inp> file) has two data sets with the same input data file
and different k-weights.  No attempt is made to verify that the
fitting model used for each data set is, in fact, the same.  Since the
MKW fit is usually implemented in Feffit using an include file, this
seems like a safe approach.  But it is, in fact, possible to use a
different model with the different k-weights.

=item *

C<cull_mkw> only works for a single data set, multiple k-weight fit.

=item *

Only uses integer part of kw value.

=item *

cormin, case insensitivity, pathparam synonyms, local, epsdat, epsr,
rlast, iwindow, ikwindow, irwindow

  from fitinp.f:
       wins(1) =  'hanning'
       wins(2) =  'fhanning'
       wins(3) =  'gaussian window'
       wins(4) =  'kaiser-bessel'
       wins(5) =  'parzen'
       wins(6) =  'welch'
       wins(7) =  'sine'

=back

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