#!/usr/bin/perl # the above is the standard linux/unix path for perl: change if your computer is nonstandard sub print_help{ print STDERR <0 or die "Invalid cube side $side\n"; $sidex=$side; $sidey=$side; $sidez=$side; next; } if($p eq "-lx"){ $sidex=shift(@ARGV)+0; $sidex>0 or die "Invalid cube side $sidex\n"; next; } if($p eq "-ly"){ $sidey=shift(@ARGV)+0; $sidey>0 or die "Invalid cube side $sidey\n"; next; } if($p eq "-lz"){ $sidez=shift(@ARGV)+0; $sidez>0 or die "Invalid cube side $sidez\n"; next; } if($p eq "-x"){ $xcube=shift(@ARGV)+0; next; } if($p eq "-y"){ $ycube=shift(@ARGV)+0; next; } if($p eq "-z"){ $zcube=shift(@ARGV)+0; next; } if($p eq "-i"){ $initialframe=1; next; } if($p eq "-ti"){ $timein=shift(@ARGV)+0.; next; } if($p eq "-tf"){ $timefi=shift(@ARGV)+0.; next; } if($p eq "-h"){ # help print_help; die "\n"; } print_help; die "invalid option $p\n"; } $sidex*=0.5; $sidey*=0.5; $sidez*=0.5; $maxatoms=0; $actualframes=0; open(INPUT,"$filename") || die "Cannot open $filename\n"; while(){ s/^\s*//; # remove leading spaces @li = split('\s+'); if($#li==0 && $li[0]+0==$li[0]){ $natms = $li[0]; $_=; $commentline=$_; s/^\s*//; @li = split('\s+'); $time=$li[1]+0.; if($timein <= $time && $time <= $timefi){ if($timeinActual==-$BIG){ $timeinActual=$time; } $timefiActual=$time; # eventually it remains equal to the last processed time $actualframes++; # @nli=(); @xli=(); @yli=(); @zli=(); @eli=(); if($initialframe){ for($i=0;$i<$natms;$i++){ $_=; s/^\s*//; @li = split('\s+'); push @eli,$_; if($actualframes==1 && abs($li[1]-$xcube)<$sidex && abs($li[2]-$ycube)<$sidey && abs($li[3]-$zcube)<$sidez ){ push @indexes,$i } } }else{ for($i=0;$i<$natms;$i++){ $_=; s/^\s*//; @li = split('\s+'); if(abs($li[1]-$xcube)<$sidex && abs($li[2]-$ycube)<$sidey && abs($li[3]-$zcube)<$sidez ){ push @eli,$_ } } } if($initialframe){ $nat=$#indexes+1; }else{ $nat=$#eli+1; } if($nat>$maxatoms){ $maxatoms=$nat; } $natms>=$nat or die "cut_cube_in_xyz: input file $filename incompatible with option -i from frame $actualframes\n"; print $nat,"\n"; # number of atoms in cube print $commentline; if($initialframe){ for($j=0;$j<$nat;$j++){ $i=$indexes[$j]; print $eli[$i]; } }else{ for($i=0;$i<=$#eli;$i++){ print $eli[$i]; } } }else{ # the following is not really needed, but makes skips quicker: for($i=0;$i<$natms;$i++){;}; } } } close(INPUT); print STDERR "cut_cube_in_xyz: selected ",$actualframes," frames"; if($timeinActual!=-$BIG){ print STDERR ", from t=", $timeinActual; } if($timefi!=$BIG){ print STDERR ", until t=",$timefiActual; } print STDERR ", with "; if(!$initialframe){ print STDERR "at most "; } print STDERR $maxatoms," atoms per frame"; if($initialframe && $actualframes>1){ print STDERR ", based on their positions in the first frame"; } print STDERR ".\n";