Dealing with MCNP mesh data

MCNP mesh tallies generate a lot of numbers to deal with. It all gets written to a formatted text file. Depending on the size and resolution of your simulation, you can easily end up with GBs of data in a single text file.

The first simulations I did, I decided to stuff it all into a database and use Excel to pull out the bits I wanted. It's a little clumsy and somewhat cumbersome but it works.

As I work with MCNP more, I'll probably come up with better ways and find better tools to help deal with the data. For now, this is the snippet of PHP code I wrote to stick the data into my DB.


  // Mesh simulation data format
  // '   Energy         X         Y         Z     Result     Rel Error     Volume    Rslt * Vol'
  // '  1.000E+36   -98.000   -98.000   -94.000 8.44128E-19 4.58915E-03 6.40000E+01 5.40242E-17';

  // Open the file for reading
  if ($fh = fopen($mesh_file,"r")) {
    // Read file line by line
    while ($mesh_line = fgets($fh)) {
      // Split the data and insert into the database
      if ($c=preg_split("/\s+/",$mesh_line)) {
        $x=(float)$c[2]; // x
        $y=(float)$c[3]; // y
        $z=(float)$c[4]; // z
        $result=(float)$c[5]; // result
        $rel_err=(float)$c[6]; // rel error
//        $vol=(float)$c[7]; // volume
//        $result_vol=(float)$c[8]; // result_vol
        $res =& $dbh->query("insert into MeshData (sim_id,x,y,z,result,rel_err) values ($sim_id,$x,$y,$z,$result,$rel_err)");
        if (PEAR::isError($res)) {
          echo ($res->getMessage().' - '.$res->getUserInfo());
        }
      }
    }
  }