PHITS出力からの情報取り出し

ANGEL形式からの情報取得

使い方

import nkScripts.parse__phits as pph
searchList = [ "xmin", "xmax", "zmin", "zmax", "hc" ]
ret        = pph.parse__phits( inpFile="out/sample.dat", searchList=searchList )

コード

以下で情報を取得する.

import os, sys, re
import numpy as np

# ========================================================= #
# ===  parse__phits.py                                  === #
# ========================================================= #

def parse__phits( inpFile=None, \
                  searchList=[ "xmin","xmax","ymin","ymax","zmin","zmax","nx","ny","nz","hc"] ):

    # ------------------------------------------------- #
    # --- [1] preparation                           --- #
    # ------------------------------------------------- #
    ret      = {}
    with open( inpFile, 'r' ) as f:
        contents = f.readlines()
    
    # ------------------------------------------------- #
    # --- [2] search query                          --- #
    # ------------------------------------------------- #
    for word in searchList:
        query = r"\s*{0}\s*=\s*(.*?)\s*(#.*)?$".format( word )
        for line in contents:
            match = re.match( query, line )
            if ( match ):
                ret[word] = match.group(1)
                break

    # ------------------------------------------------- #
    # --- [3] histogram color                       --- #
    # ------------------------------------------------- #
    if ( "hc" in searchList ):
        pack  = []
        query = r"\s*hc:\s*y\s*=.*to.*by.*;.*x\s*=.*to.*by.*;\s*"
        for ik,line in enumerate(contents):
            match = re.match( query, line )
            if ( match ):
                istart = ik + 1
                break
        lines = contents[istart:]
        stack = []
        for ik,line in enumerate(lines):
            if ( len( line.strip() ) == 0 ):
                break
            else:
                stack += [ float(val) for val in ( line.strip() ).split() ]
        hc_values = np.array( stack )
        ret["hc"] = hc_values
    return( ret )


# ========================================================= #
# ===   Execution of Pragram                            === #
# ========================================================= #

if ( __name__=="__main__" ):

    inpFile    = "dat/tally.dat"
    searchList = [ "xmin", "ymin", "hc" ]
    
    ret        = parse__phits( inpFile=inpFile )
    print( ret )

実行結果

kent@maxwell ~/.../parse__phits/code $ python pyt/parse__phits.py 
{'xmin': '0.0', 'xmax': '1.0', 'ymin': '0.0', 'ymax': '1.0', 'zmin': '0.0', 'zmax': '1.0', 'nx': '3', 'ny': '1', 'nz': '3', 'hc': array([1., 2., 3., 4., 5., 6., 7., 8., 9.])}
[T-Deposit]
  x-type = 2          # comment
    xmin = 0.0        # comment
    xmax = 1.0        # comment
      nx = 3          # comment

  y-type = 2          # comment
    ymin = 0.0        # comment
    ymax = 1.0        # comment
      ny = 1          # comment

  z-type = 2          # comment
    zmin = 0.0        # comment
    zmax = 1.0        # comment
      nz = 3          # comment

hc: y = 0.0 to 1.0 by 0.5 ; x = 0.0 to 1.0 by 0.5 ;
  1 2 3 4 5 6 7 8 9