fusion-energy / openmc-dagmc-wrapper

A Python package that extends OpenMC base classes to provide convenience features and standardized tallies when simulating DAGMC geometry with OpenMC.
https://openmc-dagmc-wrapper.readthedocs.io/
MIT License
7 stars 2 forks source link

Add get material tags method #32

Closed shimwell closed 2 years ago

shimwell commented 3 years ago

pshriwise wrote this code snippet a while back and I have not got around to incorporating it quite yet.

I shall get this done next week

#! /bin/env python

​

from argparse import ArgumentParser

​

from pymoab import core, types

​

​

_VALID_GEOM_TYPES =  ("Group", "Volume", "Surface", "Curve", "Vertex")

​

​

def ids_by_type(mb, geom_type):

    """

    Returns the ids for a given geometry entity type.

​

    Parameters

    ----------

​

    mb : pymoab.core

        A PyMOAB instance with the file of interest already loaded

    geom_type : string

        The type of geometry entity to provide IDs for.

        One of ("Group", "Volume", "Surface", "Curve", "Vertex")

​

    Returns

    -------

    list[int] : a sorted list of IDs for the geometry type

​

    """

    if geom_type not in _VALID_GEOM_TYPES:

        raise

    # retrieve the category tag on the instance

    try:

        cat_tag = mb.tag_get_handle(types.CATEGORY_TAG_NAME)

    except types.MB_ENTITY_NOT_FOUND:

        raise RuntimeError("The category tag could not be found in the PyMOAB instance."

                           "Please check that the DAGMC file has been loaded.")

    # get the id tag

    gid_tag = mb.tag_get_handle(types.GLOBAL_ID_TAG_NAME)

    # get the set of entities using the provided category tag name

    # (0 means search on the instance's root set)

    ents = mb.get_entities_by_type_and_tag(0, types.MBENTITYSET, [cat_tag], [geom_type])

    # retrieve the IDs of the entities

    ids = mb.tag_get_data(gid_tag, ents).flatten()

    return sorted(list(ids))

​

if __name__ == "__main__":

    ap = ArgumentParser(description="Program to report DAGMC IDs of various entities")

    ap.add_argument('filename', type=str, help='Filename of the DAGMC model')

    ap.add_argument('geom_type', type=str, help='Type of geometry entity to report IDs for')

    args = ap.parse_args()

    # create a new PyMOAB instance and load the specified DAGMC file

    mb = core.Core()

    mb.load_file(args.filename)

    # get the IDs for the requested geometry type

    ids = ids_by_type(mb, args.geom_type)

    print("{} IDs in {}: {}".format(args.geom_type, args.filename, ids))
shimwell commented 3 years ago

ah sorry I misunderstood, the above script gets tags while this one gets material ids https://gist.github.com/pshriwise/ba911ea159d3b6cc0152cd6c4c2f7864

shimwell commented 3 years ago
from pymoab import core, types

def load_moab_file(filename):
    mb = core.Core()
    mb.load_file(filename)
    return mb

def find_moab_ids_by_type(mb: core.Core(), geom_type: str) -> List[int]:
    """Returns the ids for a given geometry entity type.

    mb: A PyMOAB instance with the file of interest already loaded
    geom_type: The type of geometry entity to provide IDs for. One of ("Group",
        "Volume", "Surface", "Curve", "Vertex")

    Returns: a sorted list of IDs for the geometry type
    """

    _VALID_GEOM_TYPES =  ("Group", "Volume", "Surface", "Curve", "Vertex")

    if geom_type not in _VALID_GEOM_TYPES:
        raise
    # retrieve the category tag on the instance
    try:

        cat_tag = mb.tag_get_handle(types.CATEGORY_TAG_NAME)

    except types.MB_ENTITY_NOT_FOUND:

        raise RuntimeError("The category tag could not be found in the PyMOAB instance."

                           "Please check that the DAGMC file has been loaded.")

    # get the id tag
    gid_tag = mb.tag_get_handle(types.GLOBAL_ID_TAG_NAME)
    print(gid_tag)

    # get the set of entities using the provided category tag name
    # (0 means search on the instance's root set)
    ents = mb.get_entities_by_type_and_tag(0, types.MBENTITYSET, [cat_tag], [geom_type])

    # retrieve the IDs of the entities
    ids = mb.tag_get_data(gid_tag, ents).flatten()

    return sorted(list(ids))
shimwell commented 2 years ago

required functionality has bee added to dagmc-h5m-file-inspector package