openmc icon indicating copy to clipboard operation
openmc copied to clipboard

Domains settable as attribute in source.py

Open SteSeg opened this issue 2 years ago • 4 comments

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.

SteSeg avatar Dec 01 '23 18:12 SteSeg

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

pshriwise avatar Dec 01 '23 19:12 pshriwise

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 avatar Dec 01 '23 21:12 SteSeg

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.

pshriwise avatar Dec 14 '23 16:12 pshriwise

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?

SteSeg avatar Dec 19 '23 13:12 SteSeg