#!/usr/bin/perl
#Script to write time, strains and stresses for a single element which can be read by gnuplot script
#Works only for single element, but could also be extended for other situations.


$file = $ARGV[0];
open(FILEIN, "<$file") || die "Could not open $file\n";

open(FILEOUT, ">stressStrain.dat");

while($line = <FILEIN>) {   
    if($line =~ m/e l e m e n t   s t r e s s /){
	($junk, $timeTemp)=
	    split('at time', $line);

	($timeTemp, $timeStress)=
	    split(/\s+/, $timeTemp);

	for($i = 0;$i<6;$i++){
	    readline($line =<FILEIN>);
	}

	($junk, $junk, $stressXX, $stressYY, $stressZZ, $stressXY, $stressYZ, $stressZX, $junk)=
	    split( /\s+/, $line);
	
    }
        
   if($line =~ m/e l e m e n t   s t r a i n /){
	($junk, $timeTemp)=
	    split('at time', $line);

	($timeTemp, $timeStrain)=
	    split(/\s+/, $timeTemp);

	for($i = 0;$i<5;$i++){
	    readline($line =<FILEIN>);
	}
		
	($junk, $junk, $strainXX, $strainYY, $strainZZ, $strainXY, $strainYZ, $strainZX)=
	    split( /\s+/, $line);
	if($timeStrain != $timeStress){
	    print "error: time of strain and stress do not match\n";
	    exit(1);
	}
	
	print $timeStress, "\t", $strainXX, "\t", $strainYY,"\t", $strainZZ, "\t", $strainXY,"\t", $strainYZ, "\t", $strainZX, "\t", $stressXX, "\t", $stressYY,"\t", $stressZZ, "\t", $stressXY,"\t", $stressYZ, "\t", $stressZX,"\n";	
   }

}
