Advertisement
karamaz0v

xdatacar to input for fittin program

Apr 20th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.95 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $i ; my $j ; my $buf ; my $line ; my $aa=8.4216417639317700 ;
  6. my @empline ; my @ener; my @xdatcar ; my $xdhead=7 ;
  7.  
  8. open (XDAT, '<', 'XDATCAR') or die "'XDATCAR' $!";
  9. open (ENER, '<', 'energy.dat') or die "'energy.dat' $!";
  10.  
  11. @ener=<ENER> ; chomp for @ener ;            ## reading energy.dat
  12. my $Ndata = scalar @ener ;                 ## getting total lines number in energy.dat
  13. foreach $buf (@ener) { $buf=sprintf("%.10f",$buf); }   ##convert string to float
  14. close (ENER) ;
  15.  
  16. @xdatcar=<XDAT>; chomp for @xdatcar;    ## reading XDATCAR
  17. close (XDAT) ;
  18.  
  19. my @nspe = split(' ',$xdatcar[$xdhead-1]) ;         ##getting number of species array from XDATCAR
  20. my $Nat=0 ; foreach $buf (@nspe) { $Nat+=$buf }    ## sumup nspe array
  21.  
  22. $i = 0 ;
  23. $j = 0 ;
  24. foreach $line (@xdatcar) {          ## searching for empty line or Direct line, to be replaced with energy values
  25.   $i++ ;
  26.   if ( $line ~~ /^\s*$/ or $line ~~ /Direct/i ) {   ##matching empty lines with any kind of whitespaces, or lines containing 'Direct' word
  27.       $empline[$j]= $i ;                    ## saving pointer to those lines, empline
  28.       $line = "    $aa    $ener[$j]   $Nat   1" ;                  ## replacing those empty lines with energies and data
  29.       $j++ ;
  30.   }
  31. }
  32.  
  33. my $off=$xdhead+1 ;
  34.  
  35. for($i = 0; $i < $Ndata-1; $i++) {
  36.   my @block = @xdatcar[$empline[$i]..$empline[$i]+$Nat-1] ;   ## extracting blocks between empty lines
  37.   my @revblock = reverse(@block) ;                            ## inverting block
  38.  
  39.   my @sub_block = @revblock[$Nat-5..$Nat-1] ;                            ## inverting molecule sub_block
  40.   my @revsub_block = reverse(@sub_block) ;
  41.   splice(@revblock,$Nat-5,5,@revsub_block) ;
  42.  
  43.   splice(@xdatcar, $off+$i*($Nat+1), $Nat, @revblock) ;       ## replacing with correct block
  44. }
  45.  
  46. splice(@xdatcar, 0, $xdhead) ;       ## removing header
  47.  
  48. open(OUT,'>','tofit.in');
  49. foreach $line (@xdatcar) { print OUT "$line \n" ; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement