Skip to content

Instantly share code, notes, and snippets.

@gonuke
Last active December 18, 2021 16:12
Show Gist options
  • Save gonuke/c36e327e399c7a685cd315c738121c9a to your computer and use it in GitHub Desktop.
Save gonuke/c36e327e399c7a685cd315c738121c9a to your computer and use it in GitHub Desktop.
*untested* short script to get map of volumes and their materials from a DAGMC H5M file
import numpy as np
import pymoab as mb
def load_model(filename):
mbcore = mb.core.Core()
mbcore.load_file(filename)
return mbcore
def get_groups(mbcore):
category_tag = mbcore.tag_get_handle(mb.types.CATEGORY_TAG_NAME)
group_category = np.array(["Group"])
group_ents = mbcore.get_entities_by_type_and_tag(0, mb.types.MBENTITYSET, category_tag, group_category)
return group_ents
def get_vol_mat_map(mbcore):
name_tag = mbcore.tag_get_handle(mb.types.NAME_TAG_NAME)
id_tag = mbcore.tag_get_handle(mb.types.GLOBAL_ID_TAG_NAME)
vol_mat = {}
group_ents = get_groups(mbcore)
for group_ent in group_ents:
group_name = mbcore.tag_get_data(name_tag, group_ent)
# optionally confirm that this is a material!
vols = mbcore.get_entities_by_type(group_ent, mb.types.MBENTITYSET)
for vol in vols:
id = mbcore.tag_get_data(id_tag, vol)
vol_mat[id] = group_name
return vol_mat
@shimwell
Copy link

shimwell commented Sep 8, 2021

This looks like just the script I am after, thanks for making this, I think that I stumbled across a tiny typo that I see a fix for

id_tag = mbcore.tag_get_hanlde(mb.types.GLOBAL_ID_TAG_NAME)
should perhaps be
id_tag = mbcore.tag_get_handle(mb.types.GLOBAL_ID_TAG_NAME)

@shimwell
Copy link

shimwell commented Sep 8, 2021

I couldn't get the vols = mbcore.get_entities(group_ent) command to work though, I get the error AttributeError: 'pymoab.core.Core' object has no attribute 'get_entities'

So I am trying the script with other options

mbcore.get_entities_by_dimension(
mbcore.get_entities_by_type(
mbcore.get_entities_by_handle(
mbcore.get_entities_by_type_and_tag(

@gonuke
Copy link
Author

gonuke commented Sep 8, 2021

This looks like just the script I am after, thanks for making this, I think that I stumbled across a tiny typo that I see a fix for

id_tag = mbcore.tag_get_hanlde(mb.types.GLOBAL_ID_TAG_NAME)
should perhaps be
id_tag = mbcore.tag_get_handle(mb.types.GLOBAL_ID_TAG_NAME)

Thanks… I fixed this one

@gonuke
Copy link
Author

gonuke commented Sep 8, 2021

I couldn't get the vols = mbcore.get_entities(group_ent) command to work though, I get the error AttributeError: 'pymoab.core.Core' object has no attribute 'get_entities'

So I am trying the script with other options

mbcore.get_entities_by_dimension(
mbcore.get_entities_by_type(
mbcore.get_entities_by_handle(
mbcore.get_entities_by_type_and_tag(

I’ve updated it to use get_entities_by_type() as it’s the easiest option

@shimwell
Copy link

shimwell commented Sep 8, 2021

Excellent, I can get this working with a few more minor changes ([0][0] appears to be needed at the end of tag_get_data calls).

Would you mind if I make a tiny python package from this script?

Naturally I would cite this gist as the source and I can find a way of getting git to commit this code as @gonuke code in the git commit history.

@gonuke
Copy link
Author

gonuke commented Sep 8, 2021

Sure - feel free to make a script out of this.

@shimwell
Copy link

Just in case anyone is reading this in the future. I made a python package around this script and it can be located here
https://github.com/fusion-energy/dagmc_h5m_file_inspector
Also managed to allocate commits for the script to gonuke and added a cite to this script at the end of the readme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment