Repository navigation
Fixed the equality compare of MeshFilter class in PythonAPI - #1130
wuwenbin2006 wants to merge 1 commit into
Conversation
paulromano
left a comment
There was a problem hiding this comment.
Thanks for pointing this out @wuwenbin2006. I think an easier approach would be to just use the mesh ID when comparing/hashing. Right now, we use the mesh ID when deciding whether to write meshes to the XML file (see Tallies._create_mesh_subelements). This way, it would look something like:
def __eq__(self, other):
if type(self) is not type(other):
return False
else:
return self.mesh.id == other.mesh.id
def __hash__(self):
return hash(type(self).__name__ + str(self.mesh.id))|
Thanks @paulromano . I suppose the motivation of _create_filter_subelements is to exclude duplicated Filters in Tallies. Duplicated should have two types: 1) The same Filter instance referenced by different Tally. 2) Different instances but has the same properties other than the IDs ( and names maybe). If we really need to exclude the second case for performance, merely comparing the ID is not enough. An example below. mesh_filter4 and mesh_filter5 are essencially the same, do we need to exclude one of them? Instantiate a tally meshmesh4 = openmc.Mesh() Instantiate some tally Filtersmesh_filter4 = openmc.MeshFilter(mesh4, filter_id=14) Instantiate a tally meshmesh5 = openmc.Mesh() Instantiate some tally Filtersmesh_filter5 = openmc.MeshFilter(mesh5, filter_id=15) |
|
The concern mostly comes with respect to >>> import copy, openmc
>>> m = openmc.Mesh()
>>> m.dimension = (2, 2, 2)
>>> m.lower_left = (-1., -1., -1.)
>>> m.width = (1., 1., 1.)
>>> mf = openmc.MeshFilter(m)
>>> mf2 = copy.deepcopy(mf)
>>> mf.mesh
Mesh
ID = 1
Name =
Type = regular
Basis = (2, 2, 2)
Width = (-1.0, -1.0, -1.0)
Origin = None
Pixels = (1.0, 1.0, 1.0)
>>> mf2 = copy.deepcopy(mf)
>>> mf2.mesh
Mesh
ID = 1
Name =
Type = regular
Basis = (2, 2, 2)
Width = (-1.0, -1.0, -1.0)
Origin = None
Pixels = (1.0, 1.0, 1.0)
>>> mf.mesh is mf2.mesh
False
In the case where the user actually creates two different meshes that have the same exact attributes, I don't see a compelling reason to omit one of them. |
For creating multiple MeshFilters , OpenMOC pythonAPI will take them the same if they have the same mesh dimensions but different lower_left and upper_right. This is because the eq is too simple in the base class Filter. And the wrong equality affects the _create_filter_subelements(tallies.py:3149) called by Tallies::export_to_xml().
Just override the eq and hash in class MeshFilter.