Geometry xml file fail

Hi ,

I want to undergo OPENMC simulations from .step files such as Catia.

First I did a fuction in order to convert my .step file , using cad_to_dagmc

The code:

#Libraries
import cadquery as cq
from cad_to_dagmc import CadToDagmc
import os
import openmc

def step_to_openmc_h5m(input_step_file_path: str, output_file_name: str, output_dir: str) → str:
“”"Converts a STEP file to a DAGMC h5m file usable for simulations on OpenMC.

Args:
    input_step_file_path (str): The path to the input STEP file.
    output_file_name (str): The desired name of the output h5m file.
    output_dir (str): The directory where the output h5m file will be saved.

Returns:
    str: The path to the generated h5m file.
"""
try:
    
    converter = CadToDagmc()# Initialize CadToDagmc instance

    # Add the STEP file to the converter
    converter.add_stp_file(input_step_file_path,material_tags=["mat_air"]) #fill the geometry with a defined material

    # Generate the output h5m file path
    output_h5m_file_path = os.path.join(output_dir, output_file_name)

    # Export the DAGMC h5m file
    converter.export_dagmc_h5m_file(filename=output_h5m_file_path)

    return output_h5m_file_path
except Exception as e:
    print(f"Error occurred while converting the STEP file: {e}")
    return ""

I tried with a basic .step file and It’s successfull

Then i want to do a simple simulation but first I have to get my xml file with all the parameters :

def create_simulation_file(h5m_file_path,output_directory):

# Define the number of particles
n_particles = 1000

# Create settings for neutron simulation
my_neutron_settings = openmc.Settings()
my_neutron_settings.run_mode = "fixed source"
my_neutron_settings.particles = n_particles
my_neutron_settings.batches = 10
my_neutron_settings.photon_transport = False

# Create a neutron source
my_source = openmc.IndependentSource()
my_source.space = openmc.stats.Point((0, 0, 0))
my_source.angle = openmc.stats.Isotropic()
my_source.energy = openmc.stats.Discrete([14.06e6], [1])
my_source.particle = "neutron"

# Assign the source to settings
my_neutron_settings.source = my_source

# Create tallies for neutron flux
tally = openmc.Tally()
tally.scores = ['flux']
tallies = openmc.Tallies([tally])

# Create materials
mat_air = openmc.Material(name="Air")
mat_air.add_element("N", 0.784431)
mat_air.add_element("O", 0.210748)
mat_air.add_element("Ar", 0.0046)
mat_air.set_density("g/cc", 0.001205)
materials = openmc.Materials([mat_air])




# Create geometry with h5m file
geometry = openmc.Geometry()
geometry.file = h5m_file_path

# Create an OpenMC Model
model = openmc.model.Model(geometry=geometry, 
                   materials=materials, 
                   settings=my_neutron_settings, 
                   tallies=tallies)



model.export_to_model_xml(output_directory)

this xml file is aimed to umdergo the simulation in an another function.

I unfortunately get this error , compiling create_simulation_file :

AttributeError Traceback (most recent call last)
Cell In[15], line 50
48 # Export the model to XML
49 output_directory =“/home/gauthier/Stage/Catia/”
—> 50 model.export_to_model_xml(output_directory)

File ~/miniforge3/envs/openmc-env/lib/python3.12/site-packages/openmc/model/model.py:506, in Model.export_to_model_xml(self, path, remove_surfs)
504 mesh_memo = set()
505 settings_element = self.settings.to_xml_element(mesh_memo)
→ 506 geometry_element = self.geometry.to_xml_element()
508 xml.clean_indentation(geometry_element, level=1)
509 xml.clean_indentation(settings_element, level=1)

File ~/miniforge3/envs/openmc-env/lib/python3.12/site-packages/openmc/geometry.py:137, in Geometry.to_xml_element(self, remove_surfs)
135 # Create XML representation
136 element = ET.Element(“geometry”)
→ 137 self.root_universe.create_xml_subelement(element, memo=set())
139 # Sort the elements in the file
140 element[:] = sorted(element, key=lambda x: (
141 x.tag, int(x.get(‘id’))))

AttributeError: ‘NoneType’ object has no attribute ‘create_xml_subelement’

and I dont really know how to fix it.
Does somebody knows why it does this?

Best regards.

Great to see cad-to-dagmc is working for you.

I think the issue here is that the way you are loading the dagmc file into openmc.

This part looks like the error

geometry = openmc.Geometry()
geometry.file = h5m_file_path

Try this instead, I grabbed it from the neutronics-workshop repo.

bound_dag_univ = openmc.DAGMCUniverse(filename='dagmc.h5m').bounded_universe()
geometry = openmc.Geometry(root=bound_dag_univ)

By the way i hope to update the cad based neutronics examples in the neutronics workshop soon