Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Model attributes only if needed #3209

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

azimgivron
Copy link
Contributor

@azimgivron azimgivron commented Nov 27, 2024

Description

I made a minor adjustment to the model constructor: model elements are now initialized only if the corresponding argument is None. This change was made to support the following use case.

class MyGeometry(openmc.Geometry):
        def __init__(self, my_attr, **kwargs):
              self.my_attr = my_attr
              super().__init__(**kwargs)

class MyModel(openmc.Model):
        def __init__(self, 
            geometry: MyGeometry = None,
            materials: openmc.Materials = None,
            settings: openmc.Settings = None,
            tallies: openmc.Tallies = None,
            plots: openmc.Plots = None
         ):
              super().__init__(
                     geometry,
                      materials,
                      settings,
                      tallies,
                      plots,
              )
        
          @openmc.Model.geometry.setter
          def geometry(self, geometry: MyGeometry):
              """Geometry setter.
      
              Args:
                  geometry (MyGeometry): The geometry.
      
              """
              if not isinstance(geometry, MyGeometry):
                  raise ValueError("The geometry must be of type {MyGeometry.__name__}.")
              openmc.Model.geometry.fset(self, geometry)

Without the change, setting the geometry to an openmc.Geometry will raise an error.

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 15) on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

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.

1 participant