armi icon indicating copy to clipboard operation
armi copied to clipboard

Deepcopy of Blocks, Assemblies, and Blueprints also copy the whole Reactor

Open john-science opened this issue 3 years ago • 5 comments

It appears that doing a copy.deepcopy() of certain ARMI objects will also make a "deep copy" of the entire Reactor and Core, which is slightly terrifying:

  • Block
  • Assembly
  • Blueprints

First things first, we should validate that this is true, and for what ARMI objects. For instance, it might be that Blueprints do not have this problem, but only ArmiObjects do.

Next, we should override the __deepcopy__ method on any ARMI object with this problem, and fix it. One good lead would be to utilize Python's weakref, for a little reference to the Reactor on every object that won't cause memory leaks during garbage collection.

I'm not thrill with all the backrefs in every little ArmiObject all the way up to the Reactor and the Core. But since we are here, we better protect ourselves.

john-science avatar Jun 09 '22 22:06 john-science

@keckler @jakehader @ntouran @albeanth FYI

john-science avatar Jun 09 '22 22:06 john-science

It appears Block has a first-draft version of __deepcopy__, that doesn't (yet) use weakref:

https://github.com/terrapower/armi/blob/8c46f8c4f625fcf7e72fdca45801afe46ec105aa/armi/reactor/blocks.py#L138

Ditto for Core:

https://github.com/terrapower/armi/blob/0c7ef201c9f1ce5256e925b127b4a49c40659456/armi/reactor/reactors.py#L247

Reactor:

https://github.com/terrapower/armi/blob/0c7ef201c9f1ce5256e925b127b4a49c40659456/armi/reactor/reactors.py#L95

And, for some reason, ParameterCollection:

https://github.com/terrapower/armi/blob/260f518fb9e54d14149fb5969ce83547e52bd6f6/armi/reactor/parameters/parameterCollections.py#L275

There is some coverage, but it is a bit eclectic. I would be curious if we could place a good version of this method on ArmiObject.

john-science avatar Jun 09 '22 22:06 john-science

I recently found that the operator object on the reactor, r.o, is not copied when deepcopy'ing a reactor. Which is interesting.

So the following results in a None:

from copy import deepcopy

operator, reactor = test_reactors.loadTestReactor()
print(reactor.o)
# prints --> <Operator Standard <Settings name: ... >

rCopy = deepcopy(reactor)
print(rCopy.o)
# prints --> None

Just a note for the future if/when this ticket is addressed.

albeanth avatar Jun 30 '22 17:06 albeanth

Well, the plot thickens.

I think that is actually as it should be.

So we are deep copying when we shouldn't be, but handling it with some nuance in others. So, we are just totally inconsistent right now. Joy!

john-science avatar Jun 30 '22 21:06 john-science

I think that is actually as it should be.

I agree. But to thicken it a little more, and I'd need to double check this but I think I remember right, if I commented out the __deepcopy__ method in reactors.py::Reactor, the same behavior existed. Which was unexpected for me.

albeanth avatar Jun 30 '22 22:06 albeanth