# !/usr/lanl/bin/perl
use File::Copy;
use File::Path;
#  Version 5 May 1999
#print "Enter base name of input files\n";
#chomp ($basename = <STDIN>);
$basename="XXXXXX";
$infile = join ('',$basename,'.inp');
copy ("$infile","mb.inp");
#
#  Determine which OS is being used (from monteburns input) 
#
open(MBINP, "mb.inp");
$line = <MBINP>;
read MBINP, $OS, 2;
print "OS = $OS \n";
#  CLEAR LOGFILE
open (LOGFILE, ">log.txt");
close (LOGFILE);
#  redirect standard output to LOGFILE
open (LOGFILE, ">>log.txt");
$old_fh = select(LOGFILE);
#
#  Show date and/or time if not a PC system (too messy on 
#    the PC).
if ($OS =~ /UN/) {
  system ("date");
}
elsif ($OS =~ /VM/) {
  system ("show time"); 
}
#
# Get shell variables ------------------------------
# Run monteb, directing output & errors to a single file.  The main
#  log file is then appended by this info.
#
if ($OS !~ /VM/) {
  system ("monteb a 1>mblog.txt 2>&1");
}
else {
  system ("monteb 'a' 1>mblog.txt 2>&1");
}
open (MBLOG, "<mblog.txt");
while ($line = <MBLOG>) {
  print "$line";
}
close (MBLOG);
$filen = "params.dat";
open(PARAMS, "$filen");
read PARAMS, $var, 4;
$nout = int $var;
read PARAMS, $buf, 8;
read PARAMS, $var, 3;
$npre = int $var;
read PARAMS, $buf, 8;
read PARAMS, $var, 3;
$nrst = int $var;
read PARAMS, $buf, 8;
read PARAMS, $var, 3;
$nkeff = int $var;
read PARAMS, $buf, 8;
read PARAMS, $var, 4;
$nmat = int $var;
read PARAMS, $buf, 8;
read PARAMS, $var, 4;
$code = $var;
read PARAMS, $buf, 8;
#
close (PARAMS);
print "$nout  $npre  $nrst  $nkeff  $nmat\n";
#
# File management ----------------------------------
$feedfile = join ('',$basename,'.feed');
if (-e "$feedfile") {
  copy ("$feedfile", "feed");
}
#  Assign "tmpfile" affiliation for system
if ($nrst != 0) {
  if (!-e "tmpfile") {
    die "Restart run requires old files from previous run in 'tmpfile' subdirectory";
  }
}
if ($OS =~ /VM/) {
  $tmpfile = "[.tmpfile]";
}
else {
  $tmpfile = "./tmpfile/";
}
#  Determine if beginning of step MCNP runs are done or not
#   (mb13exist parameter will be set to 1 if so)
$mb13exist = 0;
#
#  Remove old cumulative files for multi-materials
$filen = $tmpfile . "mb1.txt";
if (-e "$filen") {
  unlink "$filen";
  $filen = $tmpfile . "mb2.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb3.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb4b.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb4a.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb5.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb6.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb7.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb8.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb9.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb10.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb12.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb14.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb15.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb16.txt";
  unlink "$filen";
  $filen = $tmpfile . "mb17.txt";
  unlink "$filen";
}
#
if ($nrst == 0) {
# Create clean 'tmpfile' directory  ----------------------------------
  if (!-e "tmpfile") {
    mkdir ("tmpfile", 0777);
  }
  else {
    rmtree("tmpfile");
    mkdir ("tmpfile", 0777);
  }    
}
# Set up initial run -------------------------------
print "...MonteBurns: Write natural element and origen input files\n";
system ("monteb e 1>mblog.txt 2>&1");
open (MBLOG, "<mblog.txt");
while ($line = <MBLOG>) {
  print "$line";
}
close (MBLOG);
system ("monteb 5 1>mblog.txt 2>&1");
open (MBLOG, "<mblog.txt");
while ($line = <MBLOG>) {
  print "$line";
}
close (MBLOG);
#
if ($nrst == 0) { 
# Set up initial run -------------------------------
# ..Backup TAPE9.INP
#
    for ($i3 = 1; $i3 <= $nmat; $i3++) {
      $tape9 = "TAPE9_$i3.INP";
      if (-e "TAPE9_0.INP") {
        copy ("TAPE9_0.INP", $tape9); 
      }
      else {
        copy ($tape9, "TAPE9_0.INP");
      }
    }
  print "...MonteBurns: Delete Old MCNP Files\n";
  if ($OS !~ /VM/) {
    if (-e "mbmcm") {
       unlink "mbmcm";
    }
    if (-e "mbmco") {
      unlink "mbmco";
    }
    if (-e "mbmcr") {
      unlink "mbmcr";
    }
    if (-e "mbmcs") {
      unlink "mbmcs";
    }
  }
  else {
    if (-e "mbmcm.dat") {
       unlink "mbmcm.dat";
    }
    if (-e "mbmco.dat") {
      unlink "mbmco.dat";
    }
    if (-e "mbmcr.dat") {
      unlink "mbmcr.dat";
    }
    if (-e "mbmcs") {
      unlink "mbmcs.dat";
    }
  }
  print "...MonteBurns: Check Print Card and create skeleton mcnp input\n";
  system ("monteb 1 < $basename 1>mblog.txt 2>&1");
  open (MBLOG, "<mblog.txt");
  while ($line = <MBLOG>) {
    print "$line";
  }
  close (MBLOG);
  print "...MonteBurns: Run MCNP Input Module to get initial comps\n";
  select($old_fh);
  print "...MonteBurns: Run MCNP Input Module to get initial comps\n";
  $old_fh = select(LOGFILE);
  if (-e "histp") {
    unlink ("histp");
  }
  copy ("mbmc", "mcnp-1");
  system ("mcnp5 ix n=mbmc 1>mblog.txt 2>&1");
  if (-e "histp") {
    unlink ("histp");
  }
  open (MBLOG, "<mblog.txt");
  while ($line = <MBLOG>) {
    print "$line";
  }
  close (MBLOG);
  print "...MonteBurns: Write tally file tal2.inp\n";
  # Get number of predictors from status
  system ("monteb 2 1>mblog.txt 2>&1"); 
  open (MBLOG, "<mblog.txt");
  while ($line = <MBLOG>) {
    print "$line";  
  }
  close (MBLOG);
#  Save initial MCNP input information for restart
  $filen = $tmpfile . "mbmc.skl";
  copy ("mbmc.skl", "$filen");
  print "...MonteBurns: Write initial origen comp file TAPE7 and nat isos\n";
  system ("monteb 4 1>mblog.txt 2>&1");
  open (MBLOG, "<mblog.txt");
  while ($line = <MBLOG>) {
    print "$line";
  }
  close (MBLOG);
  $i1 = 0
}
else {
#
# Set up restart run --------------------------
  $i1 = $nrst + 1;
  for ($i3 = 1; $i3 <= $nmat; $i3++) {
    if ($nrst < 10) {
      $tape7 = $tmpfile . "TAPE7_$i3.OU$nrst";
      $tape9 = $tmpfile . "TAPE9_$i3.IN$nrst";
      $filec = $tmpfile . "alldnz_$i3.OU$nrst";
      $filef = $tmpfile . "fluxes.OU$nrst";
      $filex = $tmpfile . "cxupdate_$i3.OU$nrst";
    }
    elsif ($nrst < 100) {
      $tape7 = $tmpfile . "TAPE7_$i3.O$nrst";
      $tape9 = $tmpfile . "TAPE9_$i3.I$nrst";
      $filec = $tmpfile . "alldnz_$i3.O$nrst";
      $filef = $tmpfile . "fluxes.O$nrst";
      $filex = $tmpfile . "cxupdate_$i3.O$nrst";
    }
    else {
      $tape7 = $tmpfile . "TAPE7_$i3.$nrst";
      $tape9 = $tmpfile . "TAPE9_$i3.$nrst";
      $filec = $tmpfile . "alldnz_$i3.$nrst";
      $filef = $tmpfile . "fluxes.$nrst";
      $filex = $tmpfile . "cxupdate_$i3.$nrst";      
    }
    if ($i1 < 10) {
  print "...tape5 i reached here -1\n";
      $tape5t = $tmpfile . "TAPE5_$i3.t$i1";
      $tape5 = $tmpfile . "TAPE5_$i3.IN$i1";
    }
    elsif ($i1 < 100) {
  print "...tape5 i reached here -2\n";
      $tape5t = $tmpfile . "TAPE5_$i3.t$i1";
      $tape5 = $tmpfile . "TAPE5_$i3.I$i1";
    }
    else {
  print "...tape5 i reached here -3\n";
      $tape5t = $tmpfile . "TAPE5_$i3.t$i1";
      $tape5 = $tmpfile . "TAPE5_$i3.$i1";
    }
    copy ("$tape7", "TAPE7_$i3.OUT");
    copy ("$tape9", "TAPE9_$i3.INP");
    if (!-e "$tape5t") {
  print "...tape5 i reached here -4\n";
      copy ("$tape5", "$tape5t");
    }
    }
  $filen = $tmpfile . "mbinp.$nrst";
  copy ("$filen", "mb.inp");
#  Recover initial MCNP input information
  $filen = $tmpfile . "mbmc.skl";
  copy ("$filen", "mbmc.skl");
}
#
# Beginning of outer loop ----------------------
for ($i = $i1; $i <= $nout; $i++) {
#
#  redirect standard output to LOGFILE at the beginning
#    each step so old errors can be viewed after each step
  select($old_fh);
  close (LOGFILE);
  open (LOGFILE, ">>log.txt");
  $old_fh = select(LOGFILE);
#
  print "...MonteBurns: Begin outer loop $i\n";
#
# tally nrst in mb.inp so monteb knows what step
#
  if ($i > 0) {
    system ("monteb 9 1>mblog.txt 2>&1");
    open (MBLOG, "<mblog.txt");
    while ($line = <MBLOG>) {
      print "$line";
    }
    close (MBLOG);
#
# determine material in each MCNP region
#
    system ("monteb c 1>mblog.txt 2>&1");
    open (MBLOG, "<mblog.txt");
    while ($line = <MBLOG>) {
      print "$line";
    }
    close (MBLOG);
    for ($i3 = 1; $i3 <= $nmat; $i3++) {
      $filen = $tmpfile . "para3_$i3.dat";
      open(PARAM3, "$filen");
      read PARAM3, $var, 4;
      $nval = int $var;
      close(PARAM3);
        if ($i < 10 ) { 
  print "...tape5 i reached here -5\n";
          $tape5t = $tmpfile . "TAPE5_$i3.t$i";
          $tape5i = $tmpfile . "TAPE5_$i3.IN$i";
        }
        elsif ($i < 100 ) {
  print "...tape5 i reached here -6\n";
          $tape5t = $tmpfile . "TAPE5_$i3.t$i";
          $tape5i = $tmpfile . "TAPE5_$i3.I$i";
        }
        else { 
  print "...tape5 i reached here -7\n";
          $tape5t = $tmpfile . "TAPE5_$i3.t$i";
          $tape5i = $tmpfile . "TAPE5_$i3.$i";
        }
  print "...tape5 i reached here -7\n";
        move ("$tape5t", "$tape5i");
        copy ("$tape5i", "TAPE5_$i3.INP");
#
# see if the same material is present in each region and if not,
# copy new material to current $i3 value TAPE.7 file
#
        copy ("TAPE7_$nval.OUT", "TAPE7_$i3.TMP");
      $mnat = $tmpfile . "mnat_$nval.tmp";
      $mnat2 = $tmpfile . "mnat_$i3.tem";
      copy ("$mnat", "$mnat2");
    }
  }
  for ($i3 = 1; $i3 <= $nmat; $i3++) {
    if ($i > 0) {
      $mnat2 = $tmpfile . "mnat_$i3.tem";
      $mnat = $tmpfile . "mnat_$i3.tmp";
      move ("$mnat2", "$mnat");
        move ("TAPE7_$i3.TMP", "TAPE7_$i3.OUT");
     }
      copy ("TAPE7_$i3.OUT", "TAPE4_$i3.INP");
  }
#
  if ($i == 1)  {
    $npre2 = $npre + 1;
  }
  elsif ($i == 0) {
    $npre2 = 1;
  }
  else {
    $npre2 = $npre;
  }
#
  $ndsc = 0;
  for ($i3 = 1; $i3 <= $nmat; $i3++) {
    $param = $tmpfile . "param_$i3.$i";
    if (-e "$param") {
      open(PARAM, "$param");
      read PARAM, $var, 4;
      $ndisc = int $var;
      close(PARAM);
      if ($ndisc == 1) {
        $ndsc = 1;
      }
    }
  }
  if ($ndsc == 1) { 
    print "...Monteburns: Add discrete feed to TAPE7\n";
    system ("monteb b 1>mblog.txt 2>&1");
    open (MBLOG, "<mblog.txt");
    while ($line = <MBLOG>) {
      print "$line";
    }
    close (MBLOG);
    for ($i3 = 1; $i3 <= $nmat; $i3++) {
        move ("TAPE7_$i3.TMP", "TAPE7_$i3.OUT");
        copy ("TAPE7_$i3.OUT", "TAPE4_$i3.INP");
      }
    if ($nkeff == 1) { 
#  Indicate that a beginning MCNP run has been done
#
      $mb13exist = 1;
      print "...Monteburns: Add discrete feed to mcnp input file\n";
      if ($OS =~ /UN/) {
        system ("date");
      }
      elsif ($OS =~ /VM/) {
        system ("show time"); 
      }
      system ("monteb 7b 1>mblog.txt 2>&1");
      open (MBLOG, "<mblog.txt");
      while ($line = <MBLOG>) {
        print "$line";
      }
      close (MBLOG);
      if ($OS =~ /UN/) {
        system ("date");
      }
      elsif ($OS =~ /VM/) {
        system ("show time"); 
      }
      copy ("mbmc.tmp", "mbmc.skl");
      copy ("mbmc.skl", "mbmc");
      open (MBMC, ">>mbmc");     
      for ($i3 = 1; $i3 <= $nmat; $i3++) {
        $filen = $tmpfile . "mb7t_$i3.out";
        $filen2 = $tmpfile . "mb7_$i3.out";
        open (MB7T, ">>$filen"); 
        open (MB7, "<$filen2");
        while ($line = <MB7>) {
          print MB7T "$line";
        }
        close (MB7);
        close (MB7T);
        $filen = $tmpfile . "mat_$i3.inp";
        open (MAT, "<$filen");
        while ($line = <MAT>) {
          print MBMC "$line";
        }
        close (MAT);
      }
      close (MBMC);
      print "...MonteBurns:  Run MCNP for discrete feed\n";
      select($old_fh);
      print "...MonteBurns:  Run MCNP for discrete feed\n";
      $old_fh = select(LOGFILE);
      if ($OS !~ /VM/) {
        if (-e "mbmcm") {
           unlink "mbmcm";
        }
        if (-e "mbmco") {
          unlink "mbmco";
        }
        if (-e "mbmcr") {
          unlink "mbmcr";
        }
        if (-e "mbmcs") {
          unlink "mbmcs";
        }
      }
      else {
        if (-e "mbmcm.dat") {
           unlink "mbmcm.dat";
        }
        if (-e "mbmco.dat") {
          unlink "mbmco.dat";
        }
        if (-e "mbmcr.dat") {
          unlink "mbmcr.dat";
        }
        if (-e "mbmcs") {
          unlink "mbmcs.dat";
        }
      }
      if (-e "histp") {
        move ("histp", "oldhist");
      }
  copy ("mbmc", "mcnp-2");
      system ("mcnp5 n=mbmc 1>mblog.txt 2>&1");
      if (-e "histp") {
        move ("oldhist", "histp");
      }
      copy ("mbmc", $tmpfile . "mbmc.beg$i");
      open (MBLOG, "<mblog.txt");
      while ($line = <MBLOG>) {
        print "$line";
      }
      close (MBLOG);
      if ($OS =~ /UN/) {
        system ("date");
      }
      elsif ($OS =~ /VM/) {
        system ("show time"); 
      }
      system ("monteb 6b 1>mblog.txt 2>&1");
      open (MBLOG, "<mblog.txt");
      while ($line = <MBLOG>) {
        print "$line";
      }
      close (MBLOG);
      if ($OS =~ /UN/) {
        system ("date");
      }
      elsif ($OS =~ /VM/) {
        system ("show time"); 
      }
      $filen = $tmpfile . "mb11t.out";
      $filen2 = $tmpfile . "mb11.out";
      open (MB11T, ">>$filen"); 
      open (MB11, "<$filen2");
      while ($line = <MB11>) {
       print MB11T "$line";
      }
      close (MB11);
      close (MB11T);
      $filen = $tmpfile . "mb13t.out";
      open (MB13T, ">>$filen"); 
      open (MB11, "<$filen2");
      while ($line = <MB11>) {
        print MB13T "$line";
      }
      close (MB11);
      close (MB13T);
    }
  }
#
# Determine grams of feed at the beginning of each step
#
  system ("monteb 8b 1>mblog.txt 2>&1");
  open (MBLOG, "<mblog.txt");
  while ($line = <MBLOG>) {
    print "$line";
  }
  close (MBLOG);
  for ($i3 = 1; $i3 <= $nmat; $i3++) { 
    $filen = $tmpfile . "mb12t_$i3.out";
    $filen2 = $tmpfile . "mb12_$i3.out";
    open (MB12T, ">>$filen"); 
    open (MB12, "<$filen2");
    while ($line = <MB12>) {
      print MB12T "$line";
    }
    close (MB12);
    close (MB12T);
    $filen = $tmpfile . "mb12a_$i3.out";
    $filen2 = $tmpfile . "mb12x_$i3.out";
    open (MB12A, ">>$filen"); 
    open (MB12X, "<$filen2");
    while ($line = <MB12X>) {
      print MB12A "$line";
    }
    close (MB12X);
    close (MB12A);
  }
#
# Beginning of inner loop -----------------------
#
  for ($i2 = 1; $i2 <= $npre2; $i2++) {
    if ($i > 0) {
      print "...MonteBurns: Run origen predictor $i2 for outer $i \n";
      select($old_fh);
      print "...MonteBurns: Run origen predictor $i2 for outer $i \n";
      $old_fh = select(LOGFILE);
      for ($i3 = 1; $i3 <= $nmat; $i3++) {
  print "...tape5 i reached here -8\n";
#  exec "ls -l";
        copy ("TAPE5_$i3.INP", "TAPE5.INP");
        copy ("TAPE9_$i3.INP", "TAPE9.INP");
        copy ("TAPE4_$i3.INP", "TAPE4.INP");
        system ("origen2 1>mblog.txt 2>&1");
        open (MBLOG, "<mblog.txt");
        while ($line = <MBLOG>) {
          print "$line";
        }
        close (MBLOG);
        print "\n"; 
        move ("TAPE6.OUT", "TAPE6_$i3.OUT");       
        move ("TAPE7.OUT", "TAPE7_$i3.OUT");
      }
      }
#
#   Temp. fix
#
    else {
      for ($i3 = 1; $i3 <= $nmat; $i3++) {
        copy ("TAPE9res", "TAPE9_$i3.INP");
      }
    }
    print "...Monteburns: Determine important players / make new mcnp mat\n";
    if ($OS =~ /UN/) {
      system ("date");
    }
    elsif ($OS =~ /VM/) {
      system ("show time"); 
    }
    system ("monteb 7m 1>mblog.txt 2>&1");
    open (MBLOG, "<mblog.txt");
    while ($line = <MBLOG>) {
      print "$line";
    }
    close (MBLOG);
    if ($OS =~ /UN/) {
      system ("date");
    }
    elsif ($OS =~ /VM/) {
      system ("show time"); 
    }
#
#   Determine if long MCNP run should be done or not
#
    $param = $tmpfile . "power.$i";
    if (-e "$param") {
      open(PWRF, "$param");
      read PWRF, $pwr, 4;
      $npwr = int $pwr;
      close(PWRF);
    }
    if (-e "$feedfile") {
    }
    else {
       $npwr = 1;
    }
    if ($npwr != 0) {
    copy ("mbmc.tmp", "mbmc.skl"); 
    print "...MonteBurns: Write new mcnp tallies and cat new mcnp input\n";
    system ("monteb 3 1>mblog.txt 2>&1");
    open (MBLOG, "<mblog.txt");
    while ($line = <MBLOG>) {
      print "$line";
    }
    close (MBLOG);
    print "...MonteBurns: Create complete MCNP input file\n";
    copy ("mbmc.skl", "mbmc");
    open (MBMC, ">>mbmc");
    for ($i3 = 1; $i3 <= $nmat; $i3++) {
      $filen = $tmpfile . "mb7t_$i3.out";
      $filen2 = $tmpfile . "mb7_$i3.out";
      open (MB7T, ">>$filen");
      open (MB7, "<$filen2");
      while ($line = <MB7>) {
        print MB7T "$line";
      }
      close (MB7);
      close (MB7T);
      $filen = $tmpfile . "mat_$i3.inp";
      $filen1 = $tmpfile . "tal1_$i3.inp";
      $filen2 = $tmpfile . "tal2_$i3.inp";
      $filen3 = $tmpfile . "tal3_$i3.inp";
      open (MAT, "<$filen"); 
      open (TAL1, "<$filen1");
      open (TAL2, "<$filen2");      
      open (TAL3, "<$filen3");     
      while ($line = <MAT>) {
        print MBMC "$line";
      }
      while ($line = <TAL1>) {
        print MBMC "$line";
      }
      while ($line = <TAL2>) {
        print MBMC "$line";
      }
      while ($line = <TAL3>) {
        print MBMC "$line";
      }
      close (MAT);
      close (TAL1);
      close (TAL2);
      close (TAL3);
      unlink ("$filen1"); 
      unlink ("$filen3");
    }
    close (MBMC);
    print "...MonteBurns: Run MCNP for outer step $i\n";
    select($old_fh);
    print "\n...MonteBurns: Run MCNP for outer step $i\n";
    $old_fh = select(LOGFILE);
    if ($OS !~ "VM") {
      if (-e "mbmcm") {
         unlink "mbmcm";
      }
      if (-e "mbmco") {
        unlink "mbmco";
      }
      if (-e "mbmcr") {
        unlink "mbmcr";
      }
      if (-e "mbmcs") {
        unlink "mbmcs";
      }
    }
    else {
      if (-e "mbmcm.dat") {
         unlink "mbmcm.dat";
      }
      if (-e "mbmco.dat") {
        unlink "mbmco.dat";
      }
      if (-e "mbmcr.dat") {
        unlink "mbmcr.dat";
      }
      if (-e "mbmcs") {
        unlink "mbmcs.dat";
      }
    }
    if (-e "histp") {
      unlink ("histp");
    }
  copy ("mbmc", "mcnp-3");
    system ("mcnp5 n=mbmc 1>mblog.txt 2>&1");
    copy ("mbmc", $tmpfile . "mbmc.mid$i");
    open (MBLOG, "<mblog.txt");
    while ($line = <MBLOG>) {
      print "$line";
    }
    close (MBLOG);
    print ("...MonteBurns: Modify orig xs file TAPE9 and TAPE5 with new flux\n");
    if ($OS =~ /UN/) {
      system ("date");
    }
    elsif ($OS =~ /VM/) {
      system ("show time"); 
    }
    system ("monteb 6m 1>mblog.txt 2>&1");
    open (MBLOG, "<mblog.txt");
    while ($line = <MBLOG>) {
      print "$line";
    }
    close (MBLOG);
    if ($OS =~ /UN/) {
      system ("date");
    }
    elsif ($OS =~ /VM/) {
      system ("show time"); 
    }
    }
    for ($i3 = 1; $i3 <= $nmat; $i3++) { 
      if ($i > 0 ) {
          move ("TAPE5_$i3.TMP", "TAPE5_$i3.INP"); 
       }
      move ("TAPE9_$i3.TMP", "TAPE9_$i3.INP");
      $filen = $tmpfile . "mb4a_$i3.out";
      $filen2 = $tmpfile . "mb6_$i3.out";
      open (MB4A, ">>$filen"); 
      open (MB6, "<$filen2");
      while ($line = <MB6>) {
        print MB4A "$line";
      }
      close (MB6);
      close (MB4A);
    }
  }
  $filen = $tmpfile . "mb11t.out";
  $filen2 = $tmpfile . "mb11.out";
  open (MB11T, ">>$filen"); 
  open (MB11, "<$filen2");
  while ($line = <MB11>) {
    print MB11T "$line";
  }
  close (MB11);
  close (MB11T);
  if ($i == 0) {
    if ($nkeff == 1) {
      $filen = $tmpfile . "mb13t.out";
      open (MB13T, ">>$filen"); 
      open (MB11, "<$filen2");
      while ($line = <MB11>) {
        print MB13T "$line";
      }
      close (MB11);
      close (MB13T);
    }
  }
# End of inner loop -------------------------------
#
  if ($i > 0) {
    print "...MonteBurns: Run origen to compare 1/2 way comps\n";
    select($old_fh);
    print "...MonteBurns: Run origen to compare 1/2 way comps\n";
    $old_fh = select(LOGFILE);
    for ($i3 = 1; $i3 <= $nmat; $i3++) { 
      copy ("TAPE9_$i3.INP", "TAPE9.INP");
      copy ("TAPE4_$i3.INP", "TAPE4.INP");
      copy ("TAPE5_$i3.INP", "TAPE5.INP");
      system ("origen2 1>mblog.txt 2>&1");
      open (MBLOG, "<mblog.txt");
      while ($line = <MBLOG>) {
        print "$line";
      }
      close (MBLOG);
      print "\n";
      move ("TAPE6.OUT", "TAPE6_$i3.OUT");       
      move ("TAPE7.OUT", "TAPE7_$i3.OUT");
    }
#
    system ("monteb 8e 1>mblog.txt 2>&1");
    open (MBLOG, "<mblog.txt");
    while ($line = <MBLOG>) {
      print "$line";
    } 
    close (MBLOG);
    for ($i3 = 1; $i3 <= $nmat; $i3++) { 
      $filen = $tmpfile . "mb4b_$i3.out";
      $filen2 = $tmpfile . "mb5_$i3.out";
      open (MB4B, ">>$filen"); 
      open (MB5, "<$filen2");
      while ($line = <MB5>) {
        print MB4B "$line";
      }
      close (MB5);
      close (MB4B);
    }
#
# Remove 1/2 way predictor stuff in TAPE5 
    system ("monteb 0 1>mblog.txt 2>&1");
    open (MBLOG, "<mblog.txt");
    while ($line = <MBLOG>) {
      print "$line";
    }
    close (MBLOG);
#    
    print "...MonteBurns: Run origen for complete outer step $i\n";
    select($old_fh);
    print "...MonteBurns: Run origen for complete outer step $i\n";  
    $old_fh = select(LOGFILE);
    for ($i3 = 1; $i3 <= $nmat; $i3++) {  
      move ("TAPE5_$i3.TMP", "TAPE5_$i3.INP");
      copy ("TAPE9_$i3.INP", "TAPE9.INP");
      copy ("TAPE4_$i3.INP", "TAPE4.INP");
      copy ("TAPE5_$i3.INP", "TAPE5.INP");
      system ("origen2 1>mblog.txt 2>&1");
      open (MBLOG, "<mblog.txt");
      while ($line = <MBLOG>) {
        print "$line";
      }
      close (MBLOG);
      print "\n";
      move ("TAPE6.OUT", "TAPE6_$i3.OUT");       
      move ("TAPE7.OUT", "TAPE7_$i3.OUT");
      if ($i < 10) {
        $filen = $tmpfile . "TAPE9_$i3.IN$i";
        copy ("TAPE9_$i3.INP", "$filen");
      }
      elsif ($i < 100) {
        $filen = $tmpfile . "TAPE9_$i3.I$i";
        copy ("TAPE9_$i3.INP", "$filen");
      }
      else {
        $filen = $tmpfile . "TAPE9_$i3.$i";
        copy ("TAPE9_$i3.INP", "$filen");
      }
    }
#
#  Save stuff for restart -------------------
#
    $filen = $tmpfile . "mbinp.$i";
    $filen2 = $tmpfile . "mbmc.skl";
    $filen3 = $tmpfile . "mbmc.$i";
    copy ("mb.inp", "$filen");
    copy ("mbmc.skl", "$filen2");
    if ($OS !~ /VM/) {
      copy ("mbmc", "$filen3");
    }
    else {
      copy ("mbmc.dat", "$filen3");
    }
#
#  Calculate k-eff at end of burn step ------
#
    if ($nkeff == 1) {
      print "...MonteBurns: Determine important players / make new mcnp mat\n";
      if ($OS =~ /UN/) {
        system ("date");
      }
      elsif ($OS =~ /VM/) {
        system ("show time"); 
      }
      system ("monteb 7e 1>mblog.txt 2>&1");
      open (MBLOG, "<mblog.txt");
      while ($line = <MBLOG>) {
        print "$line";
      }
      close (MBLOG);
      if ($OS =~ /UN/) {
        system ("date");
      }
      elsif ($OS =~ /VM/) {
        system ("show time"); 
      }
      copy ("mbmc.tmp", "mbmc.skl");
      copy ("mbmc.skl", "mbmc");
      open (MBMC, ">>mbmc");
      for ($i3 = 1; $i3 <= $nmat; $i3++) {      
        $filen = $tmpfile . "mb7t_$i3.out";
        $filen2 = $tmpfile . "mb7_$i3.out";
        open (MB7T, ">>$filen"); 
        open (MB7, "<$filen2");
        while ($line = <MB7>) {
          print MB7T "$line";
        }
        close (MB7);
        close (MB7T);
        $filen = $tmpfile . "mat_$i3.inp";
        open (MAT, "<$filen");
        while ($line = <MAT>) {
          print MBMC "$line";
        }
        close (MAT);
      }
      close (MBMC);
      print "...MonteBurns: Run MCNP for complete outer step $i\n";
      select($old_fh);
      print "...MonteBurns: Run MCNP for complete outer step $i\n";
      $old_fh = select(LOGFILE);
      if ($OS !~ /VM/) {
        if (-e "mbmcm") {
           unlink "mbmcm";
        }
        if (-e "mbmco") {
          unlink "mbmco";
        }
        if (-e "mbmcr") {
          unlink "mbmcr";
        }
        if (-e "mbmcs") {
          unlink "mbmcs";
        }
      }
      else {
        if (-e "mbmcm.dat") {
           unlink "mbmcm.dat";
        }
        if (-e "mbmco.dat") {
          unlink "mbmco.dat";
        }
        if (-e "mbmcr.dat") {
          unlink "mbmcr.dat";
        }
        if (-e "mbmcs") {
          unlink "mbmcs.dat";
        }
      }
      if (-e "histp") {
        move ("histp", "oldhist");
      }
  copy ("mbmc", "mcnp-4");
      system ("mcnp5 n=mbmc 1>mblog.txt 2>&1");
      if (-e "histp") {
        move ("oldhist", "histp");
      }
      copy ("mbmc", $tmpfile . "mbmc.end$i");
      open (MBLOG, "<mblog.txt");
      while ($line = <MBLOG>) {
        print "$line";
      }
      close (MBLOG);
      if ($OS =~ /UN/) {
        system ("date");
      }
      elsif ($OS =~ /VM/) {
        system ("show time"); 
      }
      system ("monteb 6e 1>mblog.txt 2>&1");
      open (MBLOG, "<mblog.txt");
      while ($line = <MBLOG>) {
        print "$line";
      }
      close (MBLOG);
      if ($OS =~ /UN/) {
        system ("date");
      }
      elsif ($OS =~ /VM/) {
        system ("show time"); 
      }
      $filen = $tmpfile . "mb11t.out";
      $filen2 = $tmpfile . "mb11.out";
      open (MB11T, ">>$filen");
      open (MB11, "<$filen2");
      while ($line = <MB11>) {
        print MB11T "$line";
      }
      close (MB11);
      close (MB11T);
    }
#
#   Remove discrete removal group elements
#
    system ("monteb d 1>mblog.txt 2>&1");
    open (MBLOG, "<mblog.txt");
    while ($line = <MBLOG>) {
      print "$line";
    }
    close (MBLOG);
    for ($i3 = 1; $i3 <= $nmat; $i3++) {
        if (-e "TAPE7_$i3.TEM") {
          move ("TAPE7_$i3.TEM", "TAPE7_$i3.OUT");
        }
      }
    }
#
  system ("monteb 8e 1>mblog.txt 2>&1");
  open (MBLOG, "<mblog.txt");
  while ($line = <MBLOG>) {
    print "$line";
  }
  close (MBLOG);
  for ($i3 = 1; $i3 <= $nmat; $i3++) {
    $filen = $tmpfile . "mb5t_$i3.out";
    $filen2 = $tmpfile . "mb5_$i3.out";
    open (MB5T, ">>$filen"); 
    open (MB5, "<$filen2");
    while ($line = <MB5>) {
      print MB5T "$line";
    }
    close (MB5);
    close (MB5T);
    $filen = $tmpfile . "mb5tx_$i3.out";
    $filen2 = $tmpfile . "mb5x_$i3.out";
    open (MB5TX, ">>$filen"); 
    open (MB5X, "<$filen2");
    while ($line = <MB5X>) {
      print MB5TX "$line";
    }
    close (MB5X);
    close (MB5TX);
  }
  if ($i > 0) {
    system ("monteb z 1>mblog.txt 2>&1");
    open (MBLOG, "<mblog.txt");
    while ($line = <MBLOG>) {
      print "$line";
    }
    close (MBLOG);
    for ($i3 = 1; $i3 <= $nmat; $i3++) {  
      $filen = $tmpfile . "mb9t_$i3.out";
      $filen2 = $tmpfile . "mb9_$i3.out";
      open (MB9T, ">>$filen");
      open (MB9, "<$filen2");
      while ($line = <MB9>) {
        print MB9T "$line";
      }
      close (MB9);
      close (MB9T);
    }
  }
#
# Copy to output files -----------------------
  for ($i3 = 1; $i3 <= $nmat; $i3++) {
    $filen = $tmpfile . "mb1t_$i3.out";
    $filen2 = $tmpfile . "mb1_$i3.out";
    open (MB1T, ">>$filen");
    open (MB1, "<$filen2");
    while ($line = <MB1>) {
      print MB1T $line;
    }
    close (MB1);
    close (MB1T);
#
    if ($i < 10) {
      $filen = $tmpfile . "TAPE7_$i3.OU$i";
      $filec = $tmpfile . "alldnz_$i3.OU$i";
      $filef = $tmpfile . "fluxes.OU$i";
      $filex = $tmpfile . "cxupdate_$i3.OU$i";
    }
    elsif ($i < 100) {
      $filen = $tmpfile . "TAPE7_$i3.O$i";
      $filec = $tmpfile . "alldnz_$i3.O$i";
      $filef = $tmpfile . "fluxes.O$i";
      $filex = $tmpfile . "cxupdate_$i3.O$i";
    }
    else {
      $filen = $tmpfile . "TAPE7_$i3.$i";
      $filec = $tmpfile . "alldnz_$i3.$i";
      $filef = $tmpfile . "fluxes.$i";
      $filex = $tmpfile . "cxupdate_$i3.$i";         
    }
      copy ("TAPE7_$i3.OUT", "$filen");
    $filen = $tmpfile . "mb6t_$i3.out";
    $filen2 = $tmpfile . "mb6_$i3.out";
    open (MB6T, ">>$filen");
    open (MB6, "<$filen2");
    while ($line = <MB6>) {
      print MB6T $line;
    }
    close (MB6);
    close (MB6T);
    $filen = $tmpfile . "mb2t_$i3.out";
    $filen2 = $tmpfile . "mb2_$i3.out";
    open (MB2T, ">>$filen");
    open (MB2, "<$filen2");
    while ($line = <MB2>) {
      print MB2T $line;
    }
    close (MB2);
    close (MB2T);
    $filen = $tmpfile . "mb3t_$i3.out";
    $filen2 = $tmpfile . "mb3_$i3.out";
    open (MB3T, ">>$filen");
    open (MB3, "<$filen2");
    while ($line = <MB3>) {
      print MB3T $line;
    }
    close (MB3);
    close (MB3T);
    $filen = $tmpfile . "mb8t_$i3.out";
    $filen2 = $tmpfile . "mb8_$i3.out";
    open (MB8T, ">>$filen");
    open (MB8, "<$filen2");
    while ($line = <MB8>) {
      print MB8T $line;
    }
    close (MB8);
    close (MB8T);
    $filen = $tmpfile . "mb4b_$i3.out";
    $filen2 = $tmpfile . "mb4_$i3.out";
    open (MB4B, ">>$filen");
    open (MB4, "<$filen2");
    while ($line = <MB4>) {
      print MB4B $line;
    }
    close (MB4);
    close (MB4B);
    $filen = $tmpfile . "mb14t_$i3.out";
    $filen2 = $tmpfile . "mb14_$i3.out";
    open (MB14T, ">>$filen");
    open (MB14, "<$filen2");
    while ($line = <MB14>) {
      print MB14T $line;
    }
    close (MB14);
    close (MB14T);
    $filen = $tmpfile . "mb15t_$i3.out";
    $filen2 = $tmpfile . "mb15_$i3.out";
    open (MB15T, ">>$filen");
    open (MB15, "<$filen2");
    while ($line = <MB15>) {
      print MB15T $line;
    }
    close (MB15);
    close (MB15T);
    $filen = $tmpfile . "mb16t_$i3.out";
    $filen2 = $tmpfile . "mb16_$i3.out";
    open (MB16T, ">>$filen");
    open (MB16, "<$filen2");
    while ($line = <MB16>) {
      print MB16T $line;
    }
    close (MB16);
    close (MB16T);
    $filen = $tmpfile . "mb17t_$i3.out";
    $filen2 = $tmpfile . "mb17_$i3.out";
    open (MB17T, ">>$filen");
    open (MB17, "<$filen2");
    while ($line = <MB17>) {
      print MB17T $line;
    }
    close (MB17);
    close (MB17T);
  }
#
  $val = $i + 1;
  print "$nout $val\n";
}
# End of outer loop -----------------------------------
$filen = $tmpfile . "mb1.txt";
open (MB1, ">>$filen");
$filen = $tmpfile . "mb2.txt";
open (MB2, ">>$filen");
$filen = $tmpfile . "mb3.txt";
open (MB3, ">>$filen");
$filen = $tmpfile . "mb4a.txt";
open (MB4A, ">>$filen");
$filen = $tmpfile . "mb4b.txt";
open (MB4B, ">>$filen");
$filen = $tmpfile . "mb5.txt";
open (MB5, ">>$filen");
$filen = $tmpfile . "mb6.txt";
open (MB6, ">>$filen");
$filen = $tmpfile . "mb7.txt";
open (MB7, ">>$filen");
$filen = $tmpfile . "mb8.txt";
open (MB8, ">>$filen");
$filen = $tmpfile . "mb9.txt";
open (MB9, ">>$filen");
$filen = $tmpfile . "mb10.txt";
open (MB10, ">>$filen");
$filen = $tmpfile . "mb12.txt";
open (MB12, ">>$filen");
$filen = $tmpfile . "mb14.txt";
open (MB14, ">>$filen");
$filen = $tmpfile . "mb15.txt";
open (MB15, ">>$filen");
$filen = $tmpfile . "mb16.txt";
open (MB16, ">>$filen");
$filen = $tmpfile . "mb17.txt";
open (MB17, ">>$filen");
for ($i3 = 1; $i3 <= $nmat; $i3++) {
  $filen = $tmpfile . "mb1t_$i3.out";
  open (MB1T, "<$filen");
  while ($line = <MB1T>) {
    print MB1 $line;
  }
  close (MB1T);
  $filen = $tmpfile . "mb2t_$i3.out";
  open (MB2T, "<$filen");
  while ($line = <MB2T>) {
    print MB2 $line;
  }
  close (MB2T);
  $filen = $tmpfile . "mb3t_$i3.out";
  open (MB3T, "<$filen");
  while ($line = <MB3T>) {
    print MB3 $line;
  }
  close (MB3T);
  $filen = $tmpfile . "mb4a_$i3.out";
  open (MB4AT, "<$filen");
  while ($line = <MB4AT>) {
    print MB4A $line;
  }
  close (MB4AT);
  $filen = $tmpfile . "mb4b_$i3.out";
  open (MB4BT, "<$filen");
  while ($line = <MB4BT>) {
    print MB4B $line;
  }
  close (MB4BT);
  $filen = $tmpfile . "mb5t_$i3.out";
  open (MB5T, "<$filen");
  while ($line = <MB5T>) {
    print MB5 $line;
  }
  close (MB5T);
  $filen = $tmpfile . "mb6t_$i3.out";
  open (MB6T, "<$filen");
  while ($line = <MB6T>) {
    print MB6 $line;
  }
  close (MB6T);
  $filen = $tmpfile . "mb7t_$i3.out";
  open (MB7T, "<$filen");
  while ($line = <MB7T>) {
    print MB7 $line;
  }
  close (MB7T);
  $filen = $tmpfile . "mb8t_$i3.out";
  open (MB8T, "<$filen");
  while ($line = <MB8T>) {
    print MB8 $line;
  }
  close (MB8T);
  $filen = $tmpfile . "mb9t_$i3.out";
  open (MB9T, "<$filen");
  while ($line = <MB9T>) {
    print MB9 $line;
  }
  close (MB9T);
  $filen = $tmpfile . "mb10t_$i3.out";
  open (MB10T, "<$filen");
  while ($line = <MB10T>) {
    print MB10 $line;
  }
  close (MB10T);
  $filen = $tmpfile . "mb12t_$i3.out";
  open (MB12T, "<$filen");
  while ($line = <MB12T>) {
    print MB12 $line;
  }
  close (MB12T);
  $filen = $tmpfile . "mb14t_$i3.out";
  open (MB14T, "<$filen");
  while ($line = <MB14T>) {
    print MB14 $line;
  }
  close (MB14T);
  $filen = $tmpfile . "mb15t_$i3.out";
  open (MB15T, "<$filen");
  while ($line = <MB15T>) {
    print MB15 $line;
  }
  close (MB15T);
  $filen = $tmpfile . "mb16t_$i3.out";
  open (MB16T, "<$filen");
  while ($line = <MB16T>) {
    print MB16 $line;
  }
  close (MB16T);
  $filen = $tmpfile . "mb17t_$i3.out";
  open (MB17T, "<$filen");
  while ($line = <MB17T>) {
    print MB17 $line;
  }
  close (MB17T);
}
close (MB1);
close (MB2);
close (MB3);
close (MB4A);
close (MB4B);
close (MB5);
close (MB6);
close (MB7);
close (MB8);
close (MB9);
close (MB10);
close (MB12);
close (MB14);
close (MB15);
close (MB16);
close (MB17);
#
#  Make $1.mbout
$filen = $tmpfile . "mb11t.out";
copy ("$filen", "crit.txt");
open (CRIT, ">>crit.txt");
if ($mb13exist == 1) { 
  $filen = $tmpfile . "mb13t.out";
  open (MB13, "<$filen");
  while ($line = <MB13>) {
    print CRIT $line;
  }
  close (MB13);
}
#
$filen = $tmpfile . "mb1.txt";
open (MB1, "<$filen");
while ($line = <MB1>) {
  print CRIT $line;
}
close (MB1);
#
$filen = $tmpfile . "mb6.txt";
open (MB6, "<$filen");
while ($line = <MB6>) {
  print CRIT $line;
}
close (MB6);
#
$filen = $tmpfile . "mb2.txt";
open (MB2, "<$filen");
while ($line = <MB2>) {
  print CRIT $line;
}
close (MB2);
#
$filen = $tmpfile . "mb3.txt";
open (MB3, "<$filen");
while ($line = <MB3>) {
  print CRIT $line;
}
close (MB3);
#
$filen = $tmpfile . "mb8.txt";
open (MB8, "<$filen");
while ($line = <MB8>) {
  print CRIT $line;
}
close (MB8);
#
$filen = $tmpfile . "mb12.txt";
open (MB12, "<$filen");
while ($line = <MB12>) {
  print CRIT $line;
}
close (MB12);
#
$filen = $tmpfile . "mb5.txt";
open (MB5, "<$filen");
while ($line = <MB5>) {
  print CRIT $line;
}
close (MB5);
#
$filen = $tmpfile . "mb14.txt";
open (MB14, "<$filen");
while ($line = <MB14>) {
  print CRIT $line;
}
close (MB14);
#
$filen = $tmpfile . "mb15.txt";
open (MB15, "<$filen");
while ($line = <MB15>) {
  print CRIT $line;
}
close (MB15);
#
$filen = $tmpfile . "mb16.txt";
open (MB16, "<$filen");
while ($line = <MB16>) {
  print CRIT $line;
}
close (MB16);
#
$filen = $tmpfile . "mb17.txt";
open (MB17, "<$filen");
while ($line = <MB17>) {
  print CRIT $line;
}
close (MB17);
#
$filen = $tmpfile . "mb9.txt";
open (MB9, "<$filen");
while ($line = <MB9>) {
  print CRIT $line;
}
close (MB9);
#
$filen = $tmpfile . "mb10.txt";
open (MB10, "<$filen");
while ($line = <MB10>) {
  print CRIT $line;
}
close (MB10);
#
if (-e "feed") {
  open (FEED, "<feed");
  while ($line = <FEED>) {
    print CRIT $line;
  }
  close (FEED);
}
close (CRIT);
#
move ("crit.txt", "$basename.out");
#  Make $1.chk
if (-e "$basename.chk") {
  unlink "$basename.chk";
}
open (MBTMP, ">>$basename.chk");
#
$filen = $tmpfile . "mb4a.txt";
open (MB4A, "<$filen");
while ($line = <MB4A>) {
  print MBTMP $line;
}
close (MB4A);
#
$filen = $tmpfile . "mb4b.txt";
open (MB4B, "<$filen");
while ($line = <MB4B>) {
  print MBTMP $line;
}
close (MB4B);
#
$filen = $tmpfile . "mb7.txt";
open (MB7, "<$filen");
while ($line = <MB7>) {
  print MBTMP $line;
}
close (MB7);
#
if (-e "txt2ps-sw") {
  system ("txt2ps-sw $1.mbout > $1.ps");
  system ("txt2ps-sw $1.mbchk > $1c.ps");
  system ("txt2ps-xs $1.mbout > $1.pss");
  system ("txt2ps-xs $1.mbchk > $1c.pss");
}
#
#  Clean files with tool "cleanmb"
#
# print "...Cleaning Files with 'cleanmb'\n";
# system ("cleanmb 1");
#
print "...Monteburns: Completed\n";
if ($OS =~ /UN/) {
  system ("date");
}
elsif ($OS =~ /VM/) {
  system ("show time"); 
}
close (LOGFILE);
#
#  Clean files
#
select($old_fh);
print "...Cleaning up Files \n";
#
#if (-e "mblog.txt") {
#   unlink "mblog.txt";
#}
if (-e "mbmc.tmp") {
   unlink "mbmc.tmp";
}
if (-e "mbmc.skl") {
  unlink "mbmc.skl";
}
if ($OS !~ /VM/) {
  if (-e "mbmc") {
     unlink "mbmc";
  }
  if (-e "mbmcm") {
     unlink "mbmcm";
  }
#  if (-e "mbmco") {
#    unlink "mbmco";
#  }
  if (-e "mbmcr") {
    unlink "mbmcr";
  }
  if (-e "mbmcs") {
    unlink "mbmcs";
  }
}
else {
  if (-e "mbmc.dat") {
    unlink "mbmc.dat";
  }
  if (-e "mbmcm.dat") {
    unlink "mbmcm.dat";
  }
#  if (-e "mbmco.dat") {
#    unlink "mbmco.dat";
#  }
  if (-e "mbmcr.dat") {
    unlink "mbmcr.dat";
  }
  if (-e "mbmcs.dat") {
    unlink "mbmcs.dat";
  }
}
if (-e "feed") {
  unlink "feed";
}
if (-e "feed.dat") {
  unlink "feed.dat";
}
 if (-e "TAPE10.INP") {
  unlink "TAPE10.INP";
 }
 if (-e "TAPE11.OUT") {
  unlink "TAPE11.OUT";
 }
 if (-e "TAPE12.OUT") {
  unlink "TAPE12.OUT";
 }
 if (-e "TAPE13.OUT") {
  unlink "TAPE13.OUT";
 }
 if (-e "TAPE15.OUT") {
  unlink "TAPE15.OUT";
 }
 if (-e "TAPE16.OUT") {
  unlink "TAPE16.OUT";
 }
 if (-e "TAPE9.INP") {
  unlink "TAPE9.INP";
 }
 if (-e "TAPE3.INP") {
  unlink "TAPE3.INP";
 }
 if (-e "TAPE4.INP") {
    unlink "TAPE4.INP";
 }
 if (-e "TAPE5.INP") {
  unlink "TAPE5.INP";
 }
 if (-e "TAPE50.OUT") {
    unlink "TAPE50.OUT";
 }
 if (-e "TAPE6.OUT") {
  unlink "TAPE6.OUT";
 }
 if (-e "TAPE7.OUT") {
    unlink "TAPE7.OUT";
 }
 if (-e "TAPE9_0.INP") {
  unlink "TAPE9_0.INP";
 }
#if (-e "params.dat") {
#  unlink "params.dat";
#}
if (-e "tape.tmp") {
  unlink "tape.tmp";
}
$filen = $tmpfile . "natelem";
unlink "natelem";
# unlink "mb.inp";
for ($i3 = 1; $i3 <= $nmat; $i3++) {
    unlink "TAPE4_$i3.INP";
    unlink "TAPE5_$i3.INP";
    unlink "TAPE7_$i3.OUT";
    unlink "TAPE6_$i3.OUT";
    unlink "TAPE9_$i3.INP";
  }
