1import openmc
2
3###############################################################################
4#                      Simulation Input File Parameters
5###############################################################################
6
7# OpenMC simulation parameters
8batches = 20
9inactive = 10
10particles = 10000
11
12
13###############################################################################
14#                 Exporting to OpenMC materials.xml file
15###############################################################################
16
17# Instantiate some Materials and register the appropriate Nuclides
18fuel = openmc.Material(material_id=1, name='fuel')
19fuel.set_density('g/cc', 4.5)
20fuel.add_nuclide('U235', 1.)
21
22moderator = openmc.Material(material_id=2, name='moderator')
23moderator.set_density('g/cc', 1.0)
24moderator.add_element('H', 2.)
25moderator.add_element('O', 1.)
26moderator.add_s_alpha_beta('c_H_in_H2O')
27
28# Instantiate a Materials collection and export to XML
29materials_file = openmc.Materials([moderator, fuel])
30materials_file.export_to_xml()
31
32
33###############################################################################
34#                 Exporting to OpenMC geometry.xml file
35###############################################################################
36
37# Instantiate Surfaces
38left = openmc.XPlane(surface_id=1, x0=-2, name='left')
39right = openmc.XPlane(surface_id=2, x0=2, name='right')
40bottom = openmc.YPlane(surface_id=3, y0=-2, name='bottom')
41top = openmc.YPlane(surface_id=4, y0=2, name='top')
42fuel1 = openmc.ZCylinder(surface_id=5, x0=0, y0=0, r=0.4)
43fuel2 = openmc.ZCylinder(surface_id=6, x0=0, y0=0, r=0.3)
44fuel3 = openmc.ZCylinder(surface_id=7, x0=0, y0=0, r=0.2)
45
46left.boundary_type = 'vacuum'
47right.boundary_type = 'vacuum'
48top.boundary_type = 'vacuum'
49bottom.boundary_type = 'vacuum'
50
51# Instantiate Cells
52cell1 = openmc.Cell(cell_id=1, name='Cell 1')
53cell2 = openmc.Cell(cell_id=101, name='cell 2')
54cell3 = openmc.Cell(cell_id=102, name='cell 3')
55cell4 = openmc.Cell(cell_id=201, name='cell 4')
56cell5 = openmc.Cell(cell_id=202, name='cell 5')
57cell6 = openmc.Cell(cell_id=301, name='cell 6')
58cell7 = openmc.Cell(cell_id=302, name='cell 7')
59
60# Use surface half-spaces to define regions
61cell1.region = +left & -right & +bottom & -top
62cell2.region = -fuel1
63cell3.region = +fuel1
64cell4.region = -fuel2
65cell5.region = +fuel2
66cell6.region = -fuel3
67cell7.region = +fuel3
68
69# Register Materials with Cells
70cell2.fill = fuel
71cell3.fill = moderator
72cell4.fill = fuel
73cell5.fill = moderator
74cell6.fill = fuel
75cell7.fill = moderator
76
77# Instantiate Universe
78univ1 = openmc.Universe(universe_id=1)
79univ2 = openmc.Universe(universe_id=2)
80univ3 = openmc.Universe(universe_id=3)
81root = openmc.Universe(universe_id=0, name='root universe')
82
83# Register Cells with Universe
84univ1.add_cells([cell2, cell3])
85univ2.add_cells([cell4, cell5])
86univ3.add_cells([cell6, cell7])
87root.add_cell(cell1)
88
89# Instantiate a Lattice
90lattice = openmc.RectLattice(lattice_id=5)
91lattice.lower_left = [-2., -2.]
92lattice.pitch = [1., 1.]
93lattice.universes = [[univ1, univ2, univ1, univ2],
94                     [univ2, univ3, univ2, univ3],
95                     [univ1, univ2, univ1, univ2],
96                     [univ2, univ3, univ2, univ3]]
97
98# Fill Cell with the Lattice
99cell1.fill = lattice
100
101# Instantiate a Geometry, register the root Universe, and export to XML
102geometry = openmc.Geometry(root)
103geometry.export_to_xml()
104
105
106###############################################################################
107#                   Exporting to OpenMC settings.xml file
108###############################################################################
109
110# Instantiate a Settings object, set all runtime parameters, and export to XML
111settings_file = openmc.Settings()
112settings_file.batches = batches
113settings_file.inactive = inactive
114settings_file.particles = particles
115
116# Create an initial uniform spatial source distribution over fissionable zones
117bounds = [-1, -1, -1, 1, 1, 1]
118uniform_dist = openmc.stats.Box(bounds[:3], bounds[3:], only_fissionable=True)
119settings_file.source = openmc.source.Source(space=uniform_dist)
120
121settings_file.trigger_active = True
122settings_file.trigger_max_batches = 100
123settings_file.export_to_xml()
124
125
126###############################################################################
127#                   Exporting to OpenMC plots.xml file
128###############################################################################
129
130plot = openmc.Plot(plot_id=1)
131plot.origin = [0, 0, 0]
132plot.width = [4, 4]
133plot.pixels = [400, 400]
134plot.color_by = 'material'
135
136# Instantiate a Plots collection and export to XML
137plot_file = openmc.Plots([plot])
138plot_file.export_to_xml()
139
140
141###############################################################################
142#                   Exporting to OpenMC tallies.xml file
143###############################################################################
144
145# Instantiate a tally mesh
146mesh = openmc.RegularMesh(mesh_id=1)
147mesh.dimension = [4, 4]
148mesh.lower_left = [-2, -2]
149mesh.width = [1, 1]
150
151# Instantiate tally Filter
152mesh_filter = openmc.MeshFilter(mesh)
153
154# Instantiate tally Trigger
155trigger = openmc.Trigger(trigger_type='rel_err', threshold=1E-2)
156trigger.scores = ['all']
157
158# Instantiate the Tally
159tally = openmc.Tally(tally_id=1)
160tally.filters = [mesh_filter]
161tally.scores = ['total']
162tally.triggers = [trigger]
163
164# Instantiate a Tallies collection and export to XML
165tallies_file = openmc.Tallies([tally])
166tallies_file.export_to_xml()
167