|
|
Rather convoluted but successful , Find below - a macro that identifies
masses that are greater than 'Distance' from 'Position'. Expired mass
indicies are written to 'ExpireFile' seperated by newlines , with the first
line containing the name of 'LoadFile' from which these masses were chosen.
Also find perl code that when called will load the file named as it's first
argument (the expiry file) , then parse the named msim datafile , remove
the masses at indicies listed and out (hopefully) correct msim data to
STDOUT.
BUGS: mechsim_expire.pl is completly ignorant of the connections of masses
to other mechsim objects , hence connections would snap to different
apparent masses . It is by no means complete , but does all I need it to
right now.
// "mechsim_expire.inc"
#version unofficial MegaPov 1.2;
#macro ExpireMasses(LoadFile,ExpireFile,Position,Distance)
#if (mechsim:mass_count)
#local Cnt = 0;
#fopen Expiry ExpireFile write
#write ( Expiry , LoadFile, "n" )
#while (Cnt<mechsim:mass_count)
#local Mass_Pos = mechsim:mass(Cnt):position;
#local Length = vlength( Mass_Pos - Position );
//#debug concat( str(Cnt,0,0) , " has vlength from origin of " ,
str(Length,3,3) , "n")
#if ( Length < Distance )
//Do nothing mass in still in range
#else
//#debug concat ( "Mass" , str(Cnt,0,0) , "is out of range!n")
#write ( Expiry , Cnt , "n" )
//Mass is out of range and should be removed
// Cnt to a temporary file and shell-out to script to remove
//from MSIM
#end
#local Cnt=Cnt+1;
#end
#end
#end
// mechsim_expire.pl
#!/usr/bin/perl -w
use strict;
my $expiry = shift @ARGV;
die "mechsim_expire.pl <filename>
Will read a target file and list of indices from <filename> , remove the
listed mass(es) and write MSIM data to STDOUTn";
die "$expiry does not exist " unless -f $expiry;
open ( my $exp , $expiry ) or die $! ;
my $target = <$exp>;
my $destination = <$exp>;
chomp($target,$destination);
open ( my $fh , $target ) or die "Cannot open target MSIM file $!";
my @expired = <$exp>;
chomp (@expired);
my $MSIM = ParseMSIM($fh);
RemoveExpired( @expired , $MSIM );
Output($MSIM);
sub Output {
my $data = shift;
print $data->{version};
print $data->{info};
print $data->{timing};
print @{$data->{mass}} if @{$data->{mass}};
print @{$data->{face}} if @{$data->{face}};
print @{$data->{connection}} if @{$data->{connection}};
print @{$data->{viscoelastic}} if @{$data->{viscoelastic}};
}
sub RemoveExpired {
my $expired = shift;
my $data = shift;
## remove mass elements
foreach my $dead ( @$expired ){
$data->{mass}->[$dead] = undef;
}
my @remain = grep { defined } @{$data->{mass}};
$data->{mass} = @remain;
warn scalar @remain , " masses remainingn";
my @info = split ' ' , $data->{info};
$info[1] -= scalar(@$expired);
$data->{info} = join (" " , @info) . $/;
}
sub ParseMSIM {
my $msim = shift;
my $version = <$msim>;
my $info = <$msim>;
my $timing = <$msim>;
my @mass;
my @connection;
my @viscoelastic;
my @face;
while (my $line = <$msim>) {
my $type = substr($line,0,1);
push @mass , $line if ('M' eq $type);
push @connection , $line if ('C' eq $type);
push @viscoelastic , $line if ('V' eq $type );
push @face , $line if ('F' eq $type);
}
my %data;
@data{qw/version info timing/} = ($version , $info , $timing);
@data{qw/mass connection viscoelastic face/} = (@mass , @connection,
@viscoelastic, @face);
return %data;
}
Post a reply to this message
|
|