armi
armi copied to clipboard
Deepcopy of Blocks, Assemblies, and Blueprints also copy the whole Reactor
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:
BlockAssemblyBlueprints
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.
@keckler @jakehader @ntouran @albeanth FYI
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.
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.
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!
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.