I have encountered a problem assigning an energy source to a cell of complex shape. I have a cell in the form of a hexagonal prism filled with a mixture of materials, and it is necessary for the energy source to exactly match this prism in volume and spatial position.
In this regard, I have the following questions:
Is it possible to directly assign an energy source to a specific cell or material?
What methods or approaches do you use to assign an energy source to complex geometric shapes?
I would be very grateful for your help.
This is the method I use::::
# Source and geometry parameters
radius_hexagon = 234 / 2 # Half the distance between opposite flat sides of the hexagon (in millimeters)
height = tail_height # Height of the tail section (in millimeters)
# Number of point sources to create
num_sources = 10000
# Define a function to check if a point is inside the hexagon
def is_point_in_hexagon(x, y, radius):
# This function checks if the given point (x, y) is inside a regular hexagon centered at (0, 0) with the given radius
qx = abs(x)
qy = abs(y)
if qy > np.sqrt(3) * radius / 2 or qx > radius:
return False
if np.sqrt(3) * qx + qy <= np.sqrt(3) * radius:
return True
return False
# Generate points inside the hexagonal prism using rejection sampling
x_points = []
y_points = []
z_points = []
while len(x_points) < num_sources:
x_try = np.random.uniform(-radius_hexagon, radius_hexagon)
y_try = np.random.uniform(-radius_hexagon, radius_hexagon)
if is_point_in_hexagon(x_try, y_try, radius_hexagon):
x_points.append(x_try)
y_points.append(y_try)
z_points.append(np.random.uniform(0, height))
# Create a list of IndependentSource objects for point sources
source_particles = []
for i in range(num_sources):
my_source = openmc.IndependentSource()
my_source.space = openmc.stats.Point((x_points[i], y_points[i], z_points[i])) # Create a point source at the specified location
my_source.angle = openmc.stats.Isotropic() # Set isotropic distribution
my_source.particle = 'photon' # Set particle type to 'photon'
my_source.energy = openmc.stats.Discrete([1.1732e6, 1.3325e6], [0.5, 0.5]) # Energy in keV, two energies with equal probabilities
my_source.strength = 2.55e+13 / num_sources # Set the strength of each point source
source_particles.append(my_source)
# Set the generated sources directly in the settings
settings.source = source_particles
# Export simulation settings to an XML file
settings.export_to_xml()
Is this the right approach? How do you do it? Are there any simpler methods?