Skip to content

Fixed the equality compare of MeshFilter class in PythonAPI - #1130

Closed
wuwenbin2006 wants to merge 1 commit into
openmc-dev:developfrom
wuwenbin2006:Fix-meshFilter-compare
Closed

wuwenbin2006 wants to merge 1 commit into
openmc-dev:developfrom
wuwenbin2006:Fix-meshFilter-compare

Conversation

@wuwenbin2006

Copy link
Copy Markdown

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.

@paulromano paulromano left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

@wuwenbin2006

wuwenbin2006 commented Nov 28, 2018 •

Copy link
Copy Markdown
Author

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 mesh

mesh4 = openmc.Mesh()
mesh4.type = 'regular'
mesh4.dimension = [17, 17, 81]
center = np.array([0, 0, 0])
mesh4.lower_left = center - np.array([10.71, 10.71, 32.13])
mesh4.upper_right = center + np.array([10.71, 10.71, 32.13])

Instantiate some tally Filters

mesh_filter4 = openmc.MeshFilter(mesh4, filter_id=14)

Instantiate a tally mesh

mesh5 = openmc.Mesh()
mesh5.type = 'regular'
mesh5.dimension = [17, 17, 81]
center = np.array([0, 0, 0])
mesh5.lower_left = center - np.array([10.71, 10.71, 32.13])
mesh5.upper_right = center + np.array([10.71, 10.71, 32.13])

Instantiate some tally Filters

mesh_filter5 = openmc.MeshFilter(mesh5, filter_id=15)

@paulromano

Copy link
Copy Markdown
Contributor

The concern mostly comes with respect to openmc.mgxs where tally arithmetic results in duplication of filters. However, there is a difference between what you showed above (where the new filter and mesh will have unique IDs) and what openmc.mgxs does, which is to call copy.deepcopy(filter). This results in a new filter that references a new mesh, but the mesh actually has the same ID and other attributes:

>>> 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants