#Usage : perl ex2ip.pl NAME : e.g. perl ex2ip.pl left_femur

#Note  : Needs NAME.exnode and NAME.exelem as inputs and program outputs
#        NAME.ipnode NAME.ipelem with consecutive node and element numbers
#	 starting from 1. This script works ONLY for linear meshes and is
#        specifically written for the meshes created in front-end using the
#        digitised data.

#Author: Kumar Mithraratne. 07th March 2006.	 


$file_name = $ARGV[0];

######################## READING EXNODE DATA ##############################

open (EXNODE, "$file_name.exnode") || die ("NO $file_name.exnode EXISTS");

@file_data=<EXNODE>; #print "@file_data";

$total_lines = $#file_data; #print "$total_lines\n";
$total_nodes = 0;
for ($line = 0 ; $line < $total_lines + 1 ; $line = $line + 1)
  { 
   chomp $file_data[$line];
   $_ = $file_data[$line];  
       
   if (m/Node/)
     {
      $total_nodes++;
      @words = split (/ /,$file_data[$line]); #print "$words[2]\n";
      $old_node[$total_nodes] = $words[2]; $index[$words[2]] = $total_nodes;
      $new_node[$total_nodes] = $total_nodes;
            
      chomp $file_data[$line+1]; $node[$total_nodes][1] = $file_data[$line+1];
      chomp $file_data[$line+2]; $node[$total_nodes][2] = $file_data[$line+2];
      chomp $file_data[$line+3]; $node[$total_nodes][3] = $file_data[$line+3];      
     }          
  }

#print "$node[1][3]\n";

######################## CREATING IPNODE FILE ##############################

open (IPNODE, ">$file_name.ipnode") || die ("NO $file_name.ipnode EXISTS");

print IPNODE " CMISS Version 2.0  ipnode File Version 2\n";
print IPNODE " Heading: $group\n";
print IPNODE " \n"; 
print IPNODE " The number of nodes is [   $total_nodes]:    $total_nodes\n";
print IPNODE " Number of coordinates [3]: 3\n";
print IPNODE " Do you want prompting for different versions of nj=1 [N]? Y\n";
print IPNODE " Do you want prompting for different versions of nj=2 [N]? Y\n";
print IPNODE " Do you want prompting for different versions of nj=3 [N]? Y\n";
print IPNODE " The number of derivatives for coordinate 1 is [0]: 0\n";
print IPNODE " The number of derivatives for coordinate 2 is [0]: 0\n";
print IPNODE " The number of derivatives for coordinate 3 is [0]: 0\n";
print IPNODE " \n";
for ($n = 1; $n < $total_nodes + 1; $n++)
  {
   print IPNODE " Node number [    $n]:     $n\n";
   print IPNODE " The number of versions for nj=1 is [1]:  1\n";
   print IPNODE " The Xj(1) coordinate is [ 0.00000E+00]:  $node[$n][1]\n";
   print IPNODE " The number of versions for nj=2 is [1]:  1\n";
   print IPNODE " The Xj(2) coordinate is [ 0.00000E+00]:  $node[$n][2]\n";
   print IPNODE " The number of versions for nj=3 is [1]:  1\n";
   print IPNODE " The Xj(3) coordinate is [ 0.00000E+00]:  $node[$n][3]\n";
   print IPNODE " \n";
  }

######################## READING EXELEM DATA ##############################

open (EXELEM, "$file_name.exelem") || die ("NO $file_name.exelem EXISTS");

@file_data=<EXELEM>;
$total_lines = $#file_data;

for ($line = 0 ; $line < $total_lines + 1 ; $line = $line + 1)
  { 

   chomp $file_data[$line];
   $_ = $file_data[$line]; 
       
   if (m/Shape. Dimension=3/)
     {
      $start_line = $line;
     }
  }  
  
chomp $file_data[0]; @words = split (/ /,$file_data[0]); $group = $words[3]; #print "$group\n"; 

$total_elems = 0;
for ($line = $start_line ; $line < $total_lines + 1 ; $line = $line + 1)
  {
   chomp $file_data[$line];
   $_ = $file_data[$line];
   
   if (m/Element:/)
     {
      @words = split (/ /,$file_data[$line]);
      if ($words[1] = "Element:")
        {	  
	  $total_elems = $total_elems + 1;
	  $element[$total_elems][0] = $words[2];
	  @words1 = split (/ /,$file_data[$line+9]);
	  #print "$words1[1] $words1[2] $words1[3]\n";
	  for ($n = 1; $n < 9; $n++)
	    {
	     $element[$total_elems][$n] = $words1[$n];
	    }
	 }      
     }
  } 
  
#print "$element[3][7]\n";  

######################## CREATING IPELEM FILE ##############################  

open (IPELEM, ">$file_name.ipelem") || die ("NO $file_name.ipelem EXISTS");

print IPELEM " CMISS Version 2.0  ipelem File Version 2\n";
print IPELEM " Heading: $group\n";
print IPELEM " \n";   
print IPELEM " The number of elements is [$total_elems]:    $total_elems\n";
print IPELEM " \n"; 
for ($e = 1; $e < $total_elems + 1; $e++)
  {
   for ($n = 1; $n < 9; $n++)
     {
      $o_n = $element[$e][$n]; $xx = $index[$o_n]; $n_n[$n] = $new_node[$xx]; 
     }   
    
   print IPELEM " Element number [    $e]:     $e\n";
   print IPELEM " The number of geometric Xj-coordinates is [3]: 3\n";
   print IPELEM " The basis function type for geometric variable 1 is [1]:  1\n";
   print IPELEM " The basis function type for geometric variable 2 is [1]:  1\n";
   print IPELEM " The basis function type for geometric variable 3 is [1]:  1\n";
   print IPELEM " Enter the 8 global numbers for basis 1:   $n_n[1]  $n_n[2] $n_n[3] $n_n[4] $n_n[5] $n_n[6] $n_n[7] $n_n[8]\n";
   print IPELEM " Enter the 8 numbers for basis 5 [prev]:   $n_n[1]  $n_n[2] $n_n[3] $n_n[4] $n_n[5] $n_n[6] $n_n[7] $n_n[8]\n";
   print IPELEM " \n";
  }




