Domains settable as attribute in source.py
Description
Instantiating the IndependentSource() class, the only way to correctly pass the object domains was through
mysource = openmc.IndependentSource(domains=mydomains)
while this was not working properly:
mysource = openmc.IndepententSource()
mysource.domains = mydomains
With @Empi93 we tried to solved the problem and now the code in the second snippet works.
To make it work we had to add a check_domains() functions in which we moved part of the code that was in the constructor. Let me know if there is a smarter way to do so.
Thanks @SteSeg! I like the sentiment of this a lot! I think it makes sense to be able to set rejection domains using the objects themselves.
Rather than add an optional attribute to the IndependentSource class, I'm thinking we should add a domains property.
Something like:
@property
def domains(self):
return self._domains
@domains.setter
def domains(self, d):
if not isinstance(d, MutableSequence):
d = [d]
allowed_domain_types = (openmc.Cell, openmc.Material, openmc.Universe)
cv.check_iterable('source domains', d, allowed_domain_types)
self._domains = d
if isinstance(d[0], openmc.Cell):
self.domain_type = 'cell'
elif isinstance(d[0], openmc.Material):
self.domain_type = 'material'
elif isinstance(d[0], openmc.Universe):
self.domain_type = 'universe'
@property
def domain_ids(self):
return [d.id for d in self.domains]
@domains.setter
def domain_ids(self, v):
# probably do something better than this...
raise DeprecationWarning('...')
And then use the domains property throughout
Something like this?
@property
def domains(self):
return self._domains
@domains.setter
def domains(self, d):
if not isinstance(d, MutableSequence):
d = [d]
allowed_domain_types = (openmc.Cell, openmc.Material, openmc.Universe)
cv.check_iterable_type('source domains', d, allowed_domain_types)
self._domains = d
if isinstance(d[0], openmc.Cell):
self._domain_type = 'cell'
elif isinstance(d[0], openmc.Material):
self._domain_type = 'material'
elif isinstance(d[0], openmc.Universe):
self._domain_type = 'universe'
@property
def domain_ids(self):
if self.domains is not None:
return [d.id for d in self.domains]
@domain_ids.setter
def domain_ids(self):
raise DeprecationWarning('Substituted in the domain property. Will be deprecated.')
@property
def domain_type(self):
return self._domain_type
@domain_type.setter
def domain_type(self):
raise DeprecationWarning('Substituted in the domain property. Will be deprecated.')
Something like this?
@property def domains(self): return self._domains @domains.setter def domains(self, d): if not isinstance(d, MutableSequence): d = [d] allowed_domain_types = (openmc.Cell, openmc.Material, openmc.Universe) cv.check_iterable_type('source domains', d, allowed_domain_types) self._domains = d if isinstance(d[0], openmc.Cell): self._domain_type = 'cell' elif isinstance(d[0], openmc.Material): self._domain_type = 'material' elif isinstance(d[0], openmc.Universe): self._domain_type = 'universe' @property def domain_ids(self): if self.domains is not None: return [d.id for d in self.domains] @domain_ids.setter def domain_ids(self): raise DeprecationWarning('Substituted in the domain property. Will be deprecated.') @property def domain_type(self): return self._domain_type @domain_type.setter def domain_type(self): raise DeprecationWarning('Substituted in the domain property. Will be deprecated.')
@SteSeg yeah, exactly. There's probably a place for the cv.CheckedList in here, but let's start by moving toward what you've posted above.
That is already in the latest commit. I am not very familiar with how to use checkvalue. Do you suggest to use cv.CheckedList in here:
def domains(self, d):
if not isinstance(d, MutableSequence):
d = [d]
allowed_domain_types = (openmc.Cell, openmc.Material, openmc.Universe)
cv.check_iterable_type('source domains', d, allowed_domain_types)
self._domains = d
or here:
def domain_ids(self):
if self.domains is not None:
return [d.id for d in self.domains]
or maybe both?