idaholab / MontePy

MontePy is the most user friendly Python library (API) to read, edit, and write MCNP input files.
https://www.montepy.org/
MIT License
32 stars 6 forks source link

Report orphaned objects #128

Open MicahGale opened 1 year ago

MicahGale commented 1 year ago

Create a tool to quickly see objects such as transforms, materials, surfaces.

In the immediate create a tips and tricks for this hack:

unused = set()
for surface in problem.surfaces:
    if len(surface.cells) == 0:
         unused.add(surface)

Proposed solution in MCNP_Problem:

def find_unused_objects(self):
    """
    Finds all unused objects.

    :returns: A dictionary mapping a class to the set of unused objects
    :rtype: dict
    """

@bascandr

MicahGale commented 1 year ago

In GitLab by @tjlaboss on Feb 3, 2023, 12:48

unused = {s for s in problem.surfaces if not len(s.cells)}
MicahGale commented 1 year ago

To research: if generator yields nothing is that falsey?

MicahGale commented 1 year ago

In GitLab by @tjlaboss on Feb 3, 2023, 12:55

Yes

MicahGale commented 1 year ago

Nope in my testing it's truthy:

test = []
def func():
    for val in test:
          yield val

bool(func())

is True, but next(func()) raises StopIteration though

MicahGale commented 1 year ago

In GitLab by @tjlaboss on Feb 3, 2023, 13:01

Whoa, I guess it only works for range. Which as it turns out, is a class.

MicahGale commented 1 year ago

Right so thinking about it more it makes complete sense. Iterators in python are super basic. They only need to implement __next__ and raise StopIteration when there is nothing else. There is no standard way to peek ahead and see if there is a value without calling next and crossing your fingers that an exception isn't raised. So all the __bool__ can really do is see if it's a valid function and hasn't seen stopIteration... yet.

MicahGale commented 1 year ago

So this raises a new longer but funner way to do things:

unused = set()
for surface in problem:
     try:
          next(surface.cells)
     except StopIteration:
          unused.add(surface)
MicahGale commented 1 year ago

Upon further inspection I don't know if a Generator is every falsy with codespace from above test I did:


>>> handle = func()
>>> bool(handle)
True
>>> next(handle)
.... STopIteration
>>> bool(handle)
True.
MicahGale commented 1 year ago

Back on topic: thoughts on the proposed function signature?

MicahGale commented 1 year ago

In GitLab by @bascandr on Feb 3, 2023, 13:58

Let's take a second to think about what these unused objects could be:

  1. Surfaces. Should be obvious if they aren't used by a cell, but we should check if they are referenced by a tally (fs card).
  2. Universes. If they don't appear on a fill card.
  3. Cells. The only way for these too be orphaned would be to be assigned to an orphaned universe.
  4. Materials. These could be used by a tally or perturbation.
  5. Transforms.
MicahGale commented 1 year ago

I'm nervous about universes though, because it would meaning shifting a lot of things out into a new universe (0) that wasn't expecting it.

Very good point about materials. Also transforms can be used in trcl and fill and on surfaces. Any other uses?

I'm thinking only support objects once MCNPy can parse all dependent use cases.

MicahGale commented 1 year ago

In GitLab by @bascandr on Feb 3, 2023, 16:28

The universes may need to just start out as an identify only type of thing. I still want a tool for figuring out what may be safe to delete, but the process of modifying them is more complex.

The surface use of transforms is one of the things that really slowed me down this week. Any tool that can recognize (and replace) these would be helpful.

MicahGale commented 1 year ago

So first, I think this function should never just delete them, just ID them. How would you replace them though? Kind of by definition they shouldn't need to be replaced.

MicahGale commented 1 year ago

In GitLab by @bascandr on Feb 6, 2023, 13:05

In the case of some of these older ATR models, I would consider either renumbering them to reclaim the universe numbers or deleting them and reclaiming the cell/surface numbers that they contain.