FZ plugin for MCNP (Monte Carlo N-Particle Transport Code) calculations.
This plugin enables parametric studies with MCNP using the fz framework.
- Input File Support: Standard MCNP input files
- Variable Syntax:
%(...)for parameters (e.g.,%(r)for radius) - Formula Syntax:
@{...}for calculated expressions - Comment Character:
C(note: includes trailing space) - Output Parsing: Automatic extraction of
mean_keffandsigma_kefffrom MCNP output
- MCNP Installation: You must have MCNP installed on your system
- Python 3.7+: Required for the fz framework
- fz framework: Install from github.com/Funz/fz
-
Clone this repository:
git clone https://github.com/Funz/fz-mcnp.git cd fz-mcnp -
Set the MCNP_PATH environment variable:
# On Linux/Mac: export MCNP_PATH="/path/to/your/MCNP6" # On Windows: set MCNP_PATH=C:\path\to\your\MCNP6
-
The plugin is ready to use! The
.fzdirectory contains:.fz/models/MCNP.json- Model definition.fz/calculators/MCNP.sh- Calculation script.fz/calculators/localhost_MCNP.json- Calculator configuration
The repository includes example files in examples/ demonstrating a parametric study of a Godiva-type critical reactor.
Input file (examples/godiva.inp):
Godiva-type critical reactor and kcode example
c skip 10 and run a total of 110 keff cycles with 1000 neutrons per cycle
1 1 -18.74 -1 imp:n=1 $ enriched uranium sphere (godiva)
2 0 1 imp:n=0 $ all space outside the sphere
1 so %(r) $ radius of the godiva sphere
kcode 1000 1.0 10 1010 $ kcode defines a criticality calculation
ksrc 0 0 0 $ initial keff spatial dist is point at origin
m1 92235 -93.71 92238 -5.27 92234 -1.02 $ define u with weight fractions
Python script to run parametric study (examples/example_godiva.py):
import fz
import os
# Set MCNP_PATH if not already set
os.environ['MCNP_PATH'] = '/Applications/MCNP6' # Adjust for your installation
# Define parameter values
input_variables = {
"r": [8.5, 8.741, 9.0] # Three different sphere radii
}
# Run parametric study
results = fz.fzr(
input_path="examples/godiva.inp",
input_variables=input_variables,
model="MCNP",
calculators="localhost_MCNP",
results_dir="results"
)
# Display results
print(results)
print(f"\nCompleted {len(results)} calculations")Expected output:
r mean_keff sigma_keff status calculator
0 8.5 0.9850 0.0012 done localhost_MCNP
1 8.741 0.9995 0.0006 done localhost_MCNP
2 9.0 1.0145 0.0008 done localhost_MCNP
Completed 3 calculations
Use %(variable_name) to define parameters:
1 so %(radius) $ sphere radius
kcode %(neutrons) 1.0 10 %(cycles) $ neutron count and cycles
Use @{expression} for calculated values:
c Calculate volume from radius
c @{volume = 4.0/3.0 * 3.14159 * %(radius)**3}
c Volume: @{volume} cm^3
MCNP comments start with C (capital C followed by space):
C This is a comment line in MCNP format
c This is also a valid comment (lowercase c)
MCNP produces several output files:
outp- Main output file containing resultsruntpe- Binary run tape filesrctp- Source tape file
The plugin automatically extracts:
- mean_keff: Final estimated combined k-effective value
- sigma_keff: Standard deviation of k-effective
Run MCNP on a remote server via SSH:
import fz
results = fz.fzr(
"examples/godiva.inp",
{"r": [8.5, 8.741, 9.0]},
"MCNP",
calculators="ssh://[email protected]/bash /path/to/fz-mcnp/.fz/calculators/MCNP.sh",
results_dir="remote_results"
)import fz
# First run
results1 = fz.fzr(
"examples/godiva.inp",
{"r": [8.5, 8.741, 9.0]},
"MCNP",
calculators="localhost_MCNP",
results_dir="run1"
)
# Second run with additional values - reuses previous results
results2 = fz.fzr(
"examples/godiva.inp",
{"r": [8.5, 8.741, 9.0, 9.5, 10.0]}, # Added 9.5 and 10.0
"MCNP",
calculators=[
"cache://run1", # Check cache first
"localhost_MCNP" # Only run new cases
],
results_dir="run2"
)Run multiple MCNP calculations in parallel:
import fz
# Use multiple calculator instances for parallel execution
results = fz.fzr(
"examples/godiva.inp",
{"r": list(range(8, 11, 0.1))}, # Many parameter values
"MCNP",
calculators=["localhost_MCNP"] * 4, # 4 parallel workers
results_dir="parallel_results"
)The MCNP model is defined in .fz/models/MCNP.json:
{
"id": "MCNP",
"varprefix": "%",
"formulaprefix": "@",
"delim": "()",
"commentline": "C ",
"output": {
"mean_keff": "grep 'the final estimated combined' outp | head -n 1 | awk '{match($0, /keff = ([0-9.]+)/, arr); print arr[1]}'",
"sigma_keff": "grep 'the final estimated combined' outp | head -n 1 | awk '{match($0, /standard deviation of ([0-9.]+)/, arr); print arr[1]}'"
}
}You can customize this configuration to extract additional outputs from MCNP.
Error: MCNP executable not found
Solution: Set the MCNP_PATH environment variable:
export MCNP_PATH="/path/to/your/MCNP6"Error: MCNP crashes with stack-related errors
Solution: The script automatically sets ulimit -s unlimited, but if issues persist, manually set it before running:
ulimit -s unlimited
python your_script.pyError: mean_keff or sigma_keff returns None
Solution: Check that your MCNP input file contains a kcode card for criticality calculations. The output parser looks for lines containing "the final estimated combined".
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Test your changes with actual MCNP calculations
- Submit a pull request
This plugin follows the same license as the fz framework (BSD 3-Clause).
- fz framework - Main parametric computing framework
- Original MCNP plugin - Legacy plugin for reference
- MCNP Documentation - Official MCNP documentation
If you use this plugin in your research, please cite:
@software{fz_mcnp,
title = {fz-mcnp: MCNP Plugin for FZ Parametric Computing Framework},
year = {2025},
url = {https://github.com/Funz/fz-mcnp}
}