物質表データベースからのPHITS入力ファイルの作成 (materials__fromDatabase.py)

物質データの管理

  • 物質データを一覧としてもっておくと便利

    • 大体使用する物質は同じ

    • 都度、文献値を探すのは面倒

    • マスターデータベースとしてもっておいて、データベースは同じものを使用.

    • 使用する元素を指定して、使用する元素のみを引っ張ってきてソース入力ファイルを生成.


データベースの構造

  • データベースファイルとして以下の形式が使用できる.

    • csv

    • json

  • csvからjsonへ変更

    • 可読性高い.データ構造を理解しやすい.取り扱いやすい.


使い方

データベースの記載方法例

{
  Air: {
    Name: "NormalAir",
    Density: -0.0012049,
    Color: "cyan",
    Comment: "JAERI-Tech-96-001 <-- JAERI-M6928",
    Composition: {
      H: -0.001,
      C: -0.0126,
      N: -75.5,
      O: -23.2,
    },
  },
  H2O: {
    Name: "H2O",
    Density: -1.0,
    Color: "cyanblue",
    Comment: "",
    Composition: {
      H: 2.0,
      O: 1.0,
    },
  },
}

記載項目

Item

Type

Description

Name

(string)

名前

Density

(float)

数密度(+) / 質量密度 (-)

Color

(string)

Comment

(string)

コメント(自由記述)

Composition

(dict of float)

物質の構成元素と数密度比(+) / 質量密度比(-)

  • 数密度は正の値、質量密度は負の値で記載する.

    • H2Oは H:2, O:1 の数密度比

    • 構造鉄(SS400)に含まれる鉄と不純物炭素は質量%で Fe:-99.6% とC:-0.13% など負の値で質量密度比を記載.

  • 元素には質量数を書くことができる.

  • 例えば、 ホウ素 (B)の安定同位体比は、10B/11B:19.9%/80.1%


使用する物質の指定方法

  1. materials.json 冒頭の設定箇所:settings 中の配列 "materialList" にタグを記載(推奨).

  2. 記載しない:全ての物質が "materials_phits.inp" に記載される.


実装

コード ( materials__fromJSON.py )

import os, sys, json5

# ========================================================= #
# ===  materials__fromJSON.py                           === #
# ========================================================= #

def materials__fromJSON( inpFile=None, outFile=None, keys=None ):
    
    # ------------------------------------------------- #
    # --- [1] arguments                             --- #
    # ------------------------------------------------- #
    if ( inpFile is None ): sys.exit( "[materials__fromJSON.py] inpFile == ???" )
    if ( outFile is None ):
        outFile   = inpFile.replace( os.path.splitext(inpFile)[1], "_phits.inp" )
    
    # ------------------------------------------------- #
    # --- [2] load json file                        --- #
    # ------------------------------------------------- #
    with open( inpFile, "r" ) as f:
        matDB = json5.load( f )
    try:
        settings = matDB.pop( "settings" )
    except KeyError:
        settings = None
    
    if ( ( keys is None ) and ( settings is not None ) ) :
        if ( "materialList" in settings ):
            keys = settings["materialList"]
            
    # ------------------------------------------------- #
    # --- [3] format as a material_phits.inp        --- #
    # ------------------------------------------------- #
    ret = generate__materialFile( matDB=matDB, outFile=outFile, settings=settings, keys=keys )
    return()


# ========================================================= #
# ===  generate__materialFile                           === #
# ========================================================= #

def generate__materialFile( outFile=None, matDB=None, keys=None, settings=None ):

    default_settings = { "characterSize":2.0,  }
    
    # ------------------------------------------------- #
    # --- [1] arguments                             --- #
    # ------------------------------------------------- #
    if ( outFile  is None ): sys.exit( "[save__materialFile] outFile     == ???" )
    if ( matDB    is None ): sys.exit( "[save__materialFile] matDB   == ???" )
    if ( settings is None ): settings = default_settings
    if ( keys     is None ): keys     = matDB.keys()
    
    # ------------------------------------------------- #
    # --- [2] make contents                         --- #
    # ------------------------------------------------- #
    pageTitle  = show__section( section="material_phits.inp (PHITS)", \
                                bar_mark="=", comment_mark="$$", newLine=False )
    matTitle   = show__section( section="Material Section", \
                                bar_mark="-", comment_mark="$$", newLine=False )
    matSection = "[Material]\n"
    block1     = "\n" + pageTitle + "\n" + matTitle + "\n" + matSection + "\n"
    for ik,key in enumerate(keys):
        item    = matDB[key]
        title   = "matNum[{0}] :: {1}".format( ik+1, item["Name"] )
        section = show__section( section=title, bar_mark="-", comment_mark="$$" )
        if ( len( item["Comment"] ) > 0 ):
            comment = "$$ comment :: {}\n".format( item["Comment"] )
        else:
            comment = ""
        matNumSection = "mat[{}]\n".format( ik+1 )
        composition   = item["Composition"]
        composit_note = [ " "*4 + "{0:<10} {1:12.5e}\n".format(key,rate) \
                          for key,rate in composition.items() ]
        matNumDefine  = "$ <define> @{0:<25} = {1:10}\n"\
            .format( item["Name"]+".matNum" , ik+1 )
        DensityDefine = "$ <define> @{0:<25} = {1:10}\n"\
            .format( item["Name"]+".Density", item["Density"] )
        block1       += section + comment + matNumSection + "".join( composit_note ) + "\n"
        block1       += matNumDefine + DensityDefine

    # ------------------------------------------------- #
    # --- [3] matNameColor section                  --- #
    # ------------------------------------------------- #
    colTitle = show__section( section="matNameColor section (PHITS)", \
                              bar_mark="-", comment_mark="$$" )
    block2   = colTitle + "[MatNameColor]\n"
    block2  += "    {0:<4} {1:<18} {2:<10} {3:<20}\n".format("mat","name","size","color")
    for ik,key in enumerate(keys):
        item = matDB[key]
        line = "    {0:<4} {1:<18} {2:<10} {3:<20}\n"\
            .format( ik+1, item["Name"], settings["characterSize"], item["Color"] )
        block2 += line
    block    = block1 + "\n" + block2
        
    # ------------------------------------------------- #
    # --- [3] save in a file                        --- #
    # ------------------------------------------------- #
    with open( outFile, "w" ) as f:
        f.write( block )
    print( "[materials__fromJSON.py] outFile :: {} ".format( outFile ) )
    return( block )



# ========================================================= #
# ===  show__section                                    === #
# ========================================================= #

def show__section( section=None, length=71, bar_mark="-", comment_mark="#", \
                   sidebarLen=3, sideSpaceLen=1, newLine=True ):

    # ------------------------------------------------- #
    # --- [1] arguments                             --- #
    # ------------------------------------------------- #
    if ( section is None ): sys.exit( "[show__section.py] section == ???" )

    # ------------------------------------------------- #
    # --- [2] Length determination                  --- #
    # ------------------------------------------------- #
    sectLen        = len(section)
    uprlwrbar_Len  = length - ( len( comment_mark ) + sideSpaceLen )*2
    space_t_Len    = ( length - len(section) - 2*( len( comment_mark ) \
                                                   + sideSpaceLen*2 + sidebarLen ) )
    space_f_Len    = space_t_Len // 2
    space_r_Len    = space_t_Len - space_f_Len

    # ------------------------------------------------- #
    # --- [3] preparation                           --- #
    # ------------------------------------------------- #
    space_f        = " "*space_f_Len
    space_r        = " "*space_r_Len
    side1          = comment_mark + " "*sideSpaceLen
    side2          = comment_mark + " "*sideSpaceLen + bar_mark*sidebarLen + " "*sideSpaceLen

    # ------------------------------------------------- #
    # --- [4] section contents                      --- #
    # ------------------------------------------------- #
    line1          = side1 + bar_mark*uprlwrbar_Len + side1[::-1] + "\n"
    line2          = side2 + space_f + section + space_r + side2[::-1] + "\n"
    lines          = line1 + line2 + line1
    if ( newLine ):
        lines = "\n" + lines + "\n"
    return( lines )


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

if ( __name__=="__main__" ):
    inpFile = "test/materials__fromJSON/materials.json"
    materials__fromJSON( inpFile=inpFile )


入力ファイル ( materials.json )

{
    settings: {
	    characterSize:2.0,
	    materialList:[ "Air", "H2O", "RaCl2", "SiO2", "Ti", "He", "Ta", "Al" ], 
    },
    
    Air: {
	Name: "Air",
	Density: -0.0012049,
	Color: "cyan",
	Comment: "JAERI-Tech-96-001 <-- JAERI-M6928",
	Composition: {
	    H: -0.001,
	    C: -0.0126,
	    N: -75.5,
	    O: -23.2,
	},
    },
    H2O: {
	Name: "H2O",
	Density: -1.0,
	Color: "cyanblue",
	Comment: "",
	Composition: {
	    H: 2.0,
	    O: 1.0,
	},
    },
    Concrete: {
	Name: "Concrete",
	Density: -2.1,
	Color: "yellow",
	Comment: "Shahei-Keisan-Jitsumu-Manual-2007",
	Composition: {
	    H: -0.0103,
	    C: -0.001,
	    O: -0.5446,
	    Mg: -0.0022,
	    Al: -0.0348,
	    Si: -0.346,
	    Ca: -0.0446,
	    Fe: -0.0143,
	},
    },
    Polyethylen: {
	Name: "Polyethylen",
	Density: -0.87,
	Color: "yellowgreen",
	Comment: "PE:: Data from JAERI-Tech-96-001",
	Composition: {
	    H: 2.0,
	    C: 1.0,
	},
    },
    Fe: {
	Name: "Fe",
	Density: -7.86,
	Color: "red",
	Comment: "",
	Composition: {
	    Fe: 1.0,
	},
    },
    SS400: {
	Name: "SS400",
	Density: -7.85,
	Color: "red",
	Comment: "Structure Steel 400 N/m2",
	Composition: {
	    Fe: -0.996,
	    Si: -0.002,
	    C: -0.0013,
	    S: -0.00025,
	    P: -0.0002,
	},
    },
    SUS316: {
	Name: "SUS316",
	Density: -7.98,
	Color: "red",
	Comment: "Stainless Steel",
	Composition: {
	    Fe: -0.669,
	    C: -0.0004,
	    Si: -0.00015,
	    Mn: -0.01,
	    P: -0.000225,
	    Ni: -0.12,
	    Cr: -0.17,
	    Mo: -0.025,
	},
    },
    Al: {
	Name: "Al",
	Density: -2.7,
	Color: "pink",
	Comment: "pure Aluminum",
	Composition: {
	    Al: 1.0,
	},
    },
    Pb: {
	Name: "Pb",
	Density: -11.34,
	Color: "orange",
	Comment: "pure Lead",
	Composition: {
	    Pb: 1.0,
	},
    },
    Ti: {
	Name: "Ti",
	Density: -4.51,
	Color: "darkred",
	Comment: "pure Titanium",
	Composition: {
	    Ti: 1.0,
	},
    },
    Ta: {
	Name: "Ta",
	Density: -16.654,
	Color: "purple",
	Comment: "pure Tantal",
	Composition: {
	    Ta: 1.0,
	},
    },
    Ra: {
	Name: "Ra",
	Density: -5.5,
	Color: "violet",
	Comment: "pure Radium",
	Composition: {
	    "226Ra": 1.0,
	},
    },
    He: {
	Name: "He",
	Density: -0.000179,
	Color: "pastelcyan",
	Comment: "",
	Composition: {
	    He: 1.0,
	},
    },
    N: {
	Name: "N",
	Density: -0.00125,
	Color: "pastelgreen",
	Comment: "",
	Composition: {
	    N: 1.0,
	},
    },
    "SiO2": {
	Name: "SiO2",
	Density: -2.196,
	Color: "blue",
	Comment: "Quartz-Glass",
	Composition: {
	    Si: 1.0,
	    O: 2.0,
	},
    },
    Soil: {
	Name: "Soil",
	Density: -1.52,
	Color: "brown",
	Comment: "Houshasen-Shahei-Handbook-Kisohen <-- JAERI-M8171",
	Composition: {
	    H: 0.0226,
	    O: 0.0326,
	    Na: 0.000401,
	    Mg: 0.000254,
	    Al: 0.0034,
	    Si: 0.00691,
	    K: 0.000102,
	    Ca: 0.000226,
	    Ti: 9.13e-05,
	    Fe: 0.000957,
	},
    },
    "Boron-PE": {
	Name: "Boron-PE",
	Density: -0.98,
	Color: "yellowgreen",
	Comment: "PE B-770  wt.10% B2O3 wt.90% HDPE(C2H4)",
	Composition: {
	    H: -0.129,
	    C: -0.771,
	    B: -0.031,
	    O: -0.069,
	},
    },
    RaCl2: {
	Name: "RaCl2",
	Density: -4.9,
	Color: "violet",
	Comment: "",
	Composition: {
	    "226Ra": 1.0,
	    Cl: 2.0,
	},
    },
}

生成される入力ファイル ( materials_phits.inp )


$$ ================================================================= $$
$$ ===                material_phits.inp (PHITS)                 === $$
$$ ================================================================= $$

$$ ----------------------------------------------------------------- $$
$$ ---                     Material Section                      --- $$
$$ ----------------------------------------------------------------- $$

[Material]


$$ ----------------------------------------------------------------- $$
$$ ---                     matNum[1] :: Air                      --- $$
$$ ----------------------------------------------------------------- $$

$$ comment :: JAERI-Tech-96-001 <-- JAERI-M6928
mat[1]
    H          -1.00000e-03
    C          -1.26000e-02
    N          -7.55000e+01
    O          -2.32000e+01

$ <define> @Air.matNum                =          1
$ <define> @Air.Density               = -0.0012049

$$ ----------------------------------------------------------------- $$
$$ ---                     matNum[2] :: H2O                      --- $$
$$ ----------------------------------------------------------------- $$

mat[2]
    H           2.00000e+00
    O           1.00000e+00

$ <define> @H2O.matNum                =          2
$ <define> @H2O.Density               =       -1.0

$$ ----------------------------------------------------------------- $$
$$ ---                    matNum[3] :: RaCl2                     --- $$
$$ ----------------------------------------------------------------- $$

mat[3]
    226Ra       1.00000e+00
    Cl          2.00000e+00

$ <define> @RaCl2.matNum              =          3
$ <define> @RaCl2.Density             =       -4.9

$$ ----------------------------------------------------------------- $$
$$ ---                     matNum[4] :: SiO2                     --- $$
$$ ----------------------------------------------------------------- $$

$$ comment :: Quartz-Glass
mat[4]
    Si          1.00000e+00
    O           2.00000e+00

$ <define> @SiO2.matNum               =          4
$ <define> @SiO2.Density              =     -2.196

$$ ----------------------------------------------------------------- $$
$$ ---                      matNum[5] :: Ti                      --- $$
$$ ----------------------------------------------------------------- $$

$$ comment :: pure Titanium
mat[5]
    Ti          1.00000e+00

$ <define> @Ti.matNum                 =          5
$ <define> @Ti.Density                =      -4.51

$$ ----------------------------------------------------------------- $$
$$ ---                      matNum[6] :: He                      --- $$
$$ ----------------------------------------------------------------- $$

mat[6]
    He          1.00000e+00

$ <define> @He.matNum                 =          6
$ <define> @He.Density                =  -0.000179

$$ ----------------------------------------------------------------- $$
$$ ---                      matNum[7] :: Ta                      --- $$
$$ ----------------------------------------------------------------- $$

$$ comment :: pure Tantal
mat[7]
    Ta          1.00000e+00

$ <define> @Ta.matNum                 =          7
$ <define> @Ta.Density                =    -16.654

$$ ----------------------------------------------------------------- $$
$$ ---                      matNum[8] :: Al                      --- $$
$$ ----------------------------------------------------------------- $$

$$ comment :: pure Aluminum
mat[8]
    Al          1.00000e+00

$ <define> @Al.matNum                 =          8
$ <define> @Al.Density                =       -2.7


$$ ----------------------------------------------------------------- $$
$$ ---               matNameColor section (PHITS)                --- $$
$$ ----------------------------------------------------------------- $$

[MatNameColor]
    mat  name               size       color               
    1    Air                2.0        cyan                
    2    H2O                2.0        cyanblue            
    3    RaCl2              2.0        violet              
    4    SiO2               2.0        blue                
    5    Ti                 2.0        darkred             
    6    He                 2.0        pastelcyan          
    7    Ta                 2.0        purple              
    8    Al                 2.0        pink