# How to create unstructured meshes for tallying

**URL:** <https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501>\
**Category:** User Support\
**Created:** [November 22, 2022, 6:07am UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501 "2022-11-22T06:07:56Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![maarten](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@maarten](https://openmc.discourse.group/u/maarten)\
**Post date:** [November 22, 2022, 6:07am UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/1 "2022-11-22T06:07:56Z")

</div>

Hello,

I am using openmc to model fixed sources. I am just starting out so keeping it simple.

I am able to run simulations using CAD that I made in FreeCAD and then turned into .h5m-files using cad\_to\_dagmc ([GitHub - fusion-energy/cad\_to\_dagmc: Convert CAD geometry (STP files) and Cadquery shapes to DAGMC h5m files](https://github.com/fusion-energy/cad_to_dagmc)). I can analyse the results like I normally would using structured meshes.

My question is how to best create an unstructured mesh to be used for tallying? I can’t find much information online so I would appreciate any help!

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [November 22, 2022, 10:17am UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/2 "2022-11-22T10:17:31Z")

</div>

Welcome to the community Maarten

Great to hear that [cad-to-dagmc](https://github.com/fusion-energy/cad_to_dagmc) is useful for the triangular surface meshes. I wrote that recently and am delighted to hear of people using it.

Patrick is the expert here but I can give some pointers while the US is asleep. There are examples on using an unstructured mesh here [GitHub - openmc-dev/openmc-notebooks: Example Jupyter notebooks for OpenMC](https://github.com/openmc-dev/openmc-notebooks) but these do start from a mesh file.

There are two routes for making the unstructured mesh.

Option one.  
Produce a tetrahedral mesh using Cubit, save as a h5 file, make sure dagmc is included in the openmc binary (either via conda or compilation), use the h5 file as shown by the example notebooks.  
openmc.UnstructuredMesh(“filename.h5”, library=‘moab’)

Option two  
Produce a mesh in Exodus and save it in .exo format. Make sure libmesh is included in the openmc binary (not currently available with conda, possible via the compile route). Use the exo file as shown in the notebooks.  
openmc.UnstructuredMesh(“filename.exo”, library=‘libmesh’)

To make this easier we should look into including libmesh in the conda package. Otherwise I would suggest making use of the dockerfile with libmesh as it takes a while to compile.  
[https://hub.docker.com/r/openmc/openmc/tags](https://hub.docker.com/r/openmc/openmc/tags)

---

<div class="post-metadata">

**Author:** ![lorenzoloi](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@lorenzoloi](https://openmc.discourse.group/u/lorenzoloi)\
**Post date:** [November 23, 2022, 3:21pm UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/3 "2022-11-23T15:21:01Z")

</div>

Hello, I’m interested in the topic.

I have a CAD file (in .stp format). If I have correctly understood, I have to first assign the materials/boundary conditions to the CAD file and then, using the “cad\_to\_openmc” package, export it in “.h5m”.

My question is: how should I assign the materials/BC to the CAD file?

Thanks

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [November 23, 2022, 5:47pm UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/4 "2022-11-23T17:47:34Z")

</div>

The making of a h5m file for use in DAGMC can also be done in a variety of ways, essentially one needs triangle sets and their connectivity to make the geometry:

Here are some methods of the top of my head, but we maintain a complete list [here](https://github.com/svalinn/DAGMC/discussions/812)

- openmc-to-cad [link](https://github.com/openmsr/CAD_to_openMC)
- Cubit and the DAGMC-plugin [[link]](https://coreform.com/products/coreform-cubit/) [[link]](https://github.com/svalinn/Cubit-plugin)
- cad-to-h5m (a Cubit wrapper) [[link]](https://github.com/fusion-energy/cad_to_h5m)
- stl-to-h5m (requires non overlapping stl files) [[link]](https://github.com/fusion-energy/stl_to_h5m)
- vertices-to-h5m (requires mesh info) [[link]](https://github.com/fusion-energy/vertices_to_h5m)
- brep-to-h5m (requires brep cad file) [[link]](https://github.com/fusion-energy/brep_to_h5m)
- cad-to-dagmc (my latest creation and new personal favorite) [[link]](https://github.com/fusion-energy/cad_to_dagmc)

Assigning the outer vacuum boundary conditions to the h5m DAGMC geometry was recently discussed on [this](https://openmc.discourse.group/t/dagmc-h5m-set-boundary/2487/3) thread.

There are a few options but in summary the boundary condition can be made automatically by using the `bounded_universe` introduced by an awesome [PR](https://github.com/openmc-dev/openmc/pull/2118) in version 0.13.1

```python
dagunv = openmc.DAGMCUniverse("dagmc.h5m").bounded_universe()

```

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [November 23, 2022, 5:53pm UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/5 "2022-11-23T17:53:27Z")

</div>

Assigning materials to a DAGMC Universe can be done in two steps.

First, identify the material names within the universe

This can now be done with another awesome [PR](https://github.com/openmc-dev/openmc/pull/2230) in openmc 0.13.1 and above using the `material_names` property

```auto
dagunv = openmc.DAGMCUniverse("dagmc.h5m").bounded_universe()
dagunv.material_names
>>> ["openmc_is_great", "especially_with_dagmc"]

```

Assuming this returns two materials called “openmc\_is\_great” and another one called “especially\_with\_dagmc” which I would imaging are ever ones first choice of material names 😉

Secondly, make use of these material names when making materials

```python
mat1 = openmc.Material(name="openmc_is_great")
mat1.add_element('Li', 1)
...
mat2 = openmc.Material(name="especially_with_dagmc")
mat1.add_element('Pb', 1)
...

```

---

<div class="post-metadata">

**Author:** ![AnthonyB08](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@AnthonyB08](https://openmc.discourse.group/u/AnthonyB08)\
**Post date:** [February 6, 2023, 4:23pm UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/6 "2023-02-06T16:23:37Z")

</div>

Hi Shimwell,

Instead of opening another forum post I figure I’d ask in this forum since the subject matter is similar. In Cubit Coreform I meshed my geometry, simple sphere, as a Tetmesh and exported the file as a dagmc .h5m. I run the simulation and it converges nicely, expect that openmc cannot read the mesh as seen below:

 ![image](https://global.discourse-cdn.com/free1/uploads/openmc/original/2X/d/dfd2cbbf1b3927bc1aef73ad39afc8bf5065a86e.png)

Moreover, when I go to post-process the results I see no data in my tallies…

Here is my code for the meshed geometry and tallies :

# Geometry input

mesh\_library = ‘moab’ # change to ‘libmesh’ to use libMesh instead

# Import Tetmesh’d Geometry

mesh\_file = ‘NeutronBall.h5m’ # Import h5m files (mesh)

# Create DAGMC universes out of h5m files

NeutronBall= openmc.DAGMCUniverse(filename=mesh\_file)

# Create regions

Fuel\_region = NeutronBall.bounding\_region()

# Create openmc Cells

Fuel\_cell = openmc.Cell(region=Fuel\_region , fill=plutonium)

# Read Mesh

umesh = openmc.UnstructuredMesh(mesh\_file, library=mesh\_library)  
mesh\_filter = openmc.MeshFilter(umesh)

# Create openmc Geometry object

geo = openmc.Geometry([Fuel\_cell,])

geo.export\_to\_xml()

# Tallies

tally = openmc.Tally(name=‘NeutronFlux’)  
tally.filters = [openmc.MeshFilter(umesh)]  
tally.scores = [“flux”]  
tallies\_file.append(tally)

---

<div class="post-metadata">

**Author:** ![AnthonyB08](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@AnthonyB08](https://openmc.discourse.group/u/AnthonyB08)\
**Post date:** [February 6, 2023, 4:45pm UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/7 "2023-02-06T16:45:38Z")

</div>

It seems settings.dagmc = True is not creating an element in the settings xml file

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [February 6, 2023, 5:09pm UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/8 "2023-02-06T17:09:39Z")

</div>

`settings.dagmc=True` has been deprecated.

The current version of openmc has an improved method of making dagmc geometries.

```auto
# makes a bounding box around the CAD geometry with vacuum surfaces
bound_dag_univ = openmc.DAGMCUniverse(filename='dagmc.h5m').bounded_universe() 

```

Just to mention we also have a DAGMC discussion forum [over here](https://github.com/svalinn/DAGMC/discussions) which could be a good source of DAGMC knowledge (especially Cubit related workflow).

---

<div class="post-metadata">

**Author:** ![AnthonyB08](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@AnthonyB08](https://openmc.discourse.group/u/AnthonyB08)\
**Post date:** [February 6, 2023, 6:48pm UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/9 "2023-02-06T18:48:12Z")

</div>

Thank you, I am very new to all of this so my apologies. Thank you for this link I will discuss there! 🙂

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [February 6, 2023, 7:02pm UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/10 "2023-02-06T19:02:00Z")

</div>

Happy to answer questions here or on the DAGMC discussions. A few of us are on both forums but not all the DAGMC developers are here on the openmc forum.

The dagmc=True was a fairly recent change

---

<div class="post-metadata">

**Author:** ![Jacob](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@Jacob](https://openmc.discourse.group/u/Jacob)\
**Post date:** [February 25, 2023, 1:27am UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/11 "2023-02-25T01:27:19Z")

</div>

Hello @Shimwell

Could you please tell me how to export mesh files created in cubit in format of h5 or h5m? Cause I cannot find the file format in the list.

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [February 25, 2023, 3:29am UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/12 "2023-02-25T03:29:01Z")

</div>

@Jacob unfortunately the export is not in the menu but can be achieved with the Cubit terminal command

export dagmc dagmc.h5m

For this to work you will need the Cubit plugin installing

> **[GitHub - svalinn/Cubit-plugin: Plugins and command extensions for Coreform Cubit](https://github.com/svalinn/Cubit-plugin)**
>
> Plugins and command extensions for Coreform Cubit. Contribute to svalinn/Cubit-plugin development by creating an account on GitHub.

There are also a few other steps like imprinting and merging

I wrote a package for automating the loading, merging, imprinting and exporting all with python if that helps

> **[GitHub - fusion-energy/cad\_to\_h5m: Converts CAD file(s) such as STP and SAT to...](https://github.com/fusion-energy/cad_to_h5m)**
>
> Converts CAD file(s) such as STP and SAT to a h5m file compatible with DAGMC based simulations using the Cubit Python API - GitHub - fusion-energy/cad\_to\_h5m: Converts CAD file(s) such as STP and S...

---

<div class="post-metadata">

**Author:** ![Jacob](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@Jacob](https://openmc.discourse.group/u/Jacob)\
**Post date:** [February 25, 2023, 12:40pm UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/13 "2023-02-25T12:40:17Z")

</div>

Okay，thank you so much! @Shimwell

---

<div class="post-metadata">

**Author:** ![qianni](https://avatars.discourse-cdn.com/v4/letter/q/cc9497/32.png) [@qianni](https://openmc.discourse.group/u/qianni)\
**Post date:** [April 10, 2023, 4:37am UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/14 "2023-04-10T04:37:06Z")

</div>

Hi, I have a question about unstructured grids. My computer configuration and plugin (dagmc cubit) have been installed. Can be done the case “[https://nbviewer.org/github/openmc-dev/openmc-notebooks/blob/main/unstructured-mesh-part-i.ipynb](https://nbviewer.org/github/openmc-dev/openmc-notebooks/blob/main/unstructured-mesh-part-i.ipynb)”, However, for my simple geometry, I built the mesh in cubit, export dagmc “test.h5m” and took .h5m file as umesh. The whole process can be calculated, but the tallies.out file is empty and cannot identify and loading my mesh file .This question diffuse me.

 ![image](https://global.discourse-cdn.com/free1/uploads/openmc/original/2X/6/675d762d8ec45a0ef5041af929f64fa7d74fe802.png)

---

<div class="post-metadata">

**Author:** ![Sabyasachi](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@Sabyasachi](https://openmc.discourse.group/u/Sabyasachi)\
**Post date:** [November 16, 2023, 7:08am UTC](https://openmc.discourse.group/t/how-to-create-unstructured-meshes-for-tallying/2501/15 "2023-11-16T07:08:06Z")

</div>

I have got the same problem as you. If you have got the solution please suggest me how can I create tetrahedral mesh in cubit and then export it into dagmc file.
