pyhit package
The input file syntax MOOSE uses the HIT format. The "pyhit" package provides tools for reading, writing, and manipulating these files from within python. The package uses the same underlying C library used by MOOSE. It also relies on the "moosetree" package.
The complete source documentation for the package is provided in pyhit source documentation page.
Example
The example in demonstrates the use of the module to read an input file ("input.i"; see Listing 1), modify the a parameter, retrieve and modify a comment, and write the modified content to a file using the built-in format function.
Listing 1: Input file used for the "pyhit" package example.
[Mesh]
[gen]
type = GeneratedMeshGenerator
dim = 1
xmax = 3
[]
[]
[Variables]
[u]
[]
[]
[Kernels]
[diff]
type = ADDiffusion
variable = u
[]
[]
[BCs]
[left]
type = ADDirichletBC
variable = u
boundary = left
value = 300
[]
[right]
type = ADNeumannBC
variable = u
boundary = right
value = 100
[]
[]
[Executioner]
type = Steady
solve_type = 'NEWTON'
[]
[Outputs]
csv = true
[]
(python/pyhit/tests/input.i)The example begins by loading two modules: "pyhit" and "moosetree". The pyhit package provides a wrapper class (pyhit.Node
) of the C bindings to the MOOSE HIT library. The wrapper inherits from moosetree.Node
, which is a generic python-base tree structure. As such much of the functionality, including the ability to search the tree, exist in the moosetree package.
After importing the necessary modules the desired input file (see Listing 1) is loaded using the load function, which returns the root pyhit.Node
to the tree.
Listing 2: Example use of "pyhit" package to read, modify, and write MOOSE a moose input file.
# Load the packages
import pyhit
import moosetree
# Read the file
root = pyhit.load('input.i')
# Locate and modify "x_max" parameter for the mesh
mesh = moosetree.find(root, func=lambda n: n.fullpath == '/Mesh/gen')
mesh["x_max"] = 4
# Set the comment on altered parameter
mesh.setComment("x_max", "Changed from 3 to 4")
# Write the modified file
pyhit.write("input_modified.i", root)
(python/pyhit/tests/test_examples.py)Once the content is loaded the tree can be search using the moosetree.find
function. This function requires the node to search from and a function used for searching. For this example that function is a simple lambda expression that looks for a node named "gen" that is a child of a node "Mesh".
The leading backslash ("/") in the name that is being searched indicates that the node containing the "Mesh" node is unnamed, which is typical of root nodes.
After this node is located it is modified in two ways: the "x_max" parameter is altered and the comment for this parameter is changed to note the alteration.
Finally, the modified output is written to a new file using the pyhit.write
function. The modifyied content (the "Mesh" block) is shown in Listing 3.
Listing 3: The modified "Mesh" block that is output to the "input_modified.i" file.
[Mesh]
[gen]
type = GeneratedMeshGenerator
dim = 1
xmax = 4 # Changed from 3 to 4
[]
[]