• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 12776996362

14 Jan 2025 09:49PM UTC coverage: 84.938% (+0.2%) from 84.729%
12776996362

Pull #3133

github

web-flow
Merge 0495246d9 into 549cc0973
Pull Request #3133: Kinetics parameters using Iterated Fission Probability

318 of 330 new or added lines in 10 files covered. (96.36%)

1658 existing lines in 66 files now uncovered.

50402 of 59340 relevant lines covered (84.94%)

33987813.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/openmc/mixin.py
1
from numbers import Integral
12✔
2
from warnings import warn
12✔
3

4
import numpy as np
12✔
5

6
import openmc.checkvalue as cv
12✔
7

8

9
class EqualityMixin:
12✔
10
    """A Class which provides a generic __eq__ method that can be inherited
11
    by downstream classes.
12
    """
13

14
    def __eq__(self, other):
12✔
15
        if isinstance(other, type(self)):
12✔
16
            for key, value in self.__dict__.items():
12✔
17
                if isinstance(value, np.ndarray):
12✔
18
                    if not np.array_equal(value, other.__dict__.get(key)):
12✔
19
                        return False
×
20
                else:
21
                    return value == other.__dict__.get(key)
12✔
22
        else:
23
            return False
×
24

25
        return True
12✔
26

27

28
class IDWarning(UserWarning):
12✔
29
    pass
12✔
30

31

32
class IDManagerMixin:
12✔
33
    """A Class which automatically manages unique IDs.
34

35
    This mixin gives any subclass the ability to assign unique IDs through an
36
    'id' property and keeps track of which ones have already been
37
    assigned. Crucially, each subclass must define class variables 'next_id' and
38
    'used_ids' as they are used in the 'id' property that is supplied here.
39

40
    """
41

42
    @property
12✔
43
    def id(self):
12✔
44
        return self._id
12✔
45

46
    @id.setter
12✔
47
    def id(self, uid):
12✔
48
        # The first time this is called for a class, we search through the MRO
49
        # to determine which class actually holds next_id and used_ids. Since
50
        # next_id is an integer (immutable), we can't modify it directly through
51
        # the instance without just creating a new attribute
52
        try:
12✔
53
            cls = self._id_class
12✔
54
        except AttributeError:
12✔
55
            for cls in self.__class__.__mro__:
12✔
56
                if 'next_id' in cls.__dict__:
12✔
57
                    break
12✔
58

59
        if uid is None:
12✔
60
            while cls.next_id in cls.used_ids:
12✔
61
                cls.next_id += 1
12✔
62
            self._id = cls.next_id
12✔
63
            cls.used_ids.add(cls.next_id)
12✔
64
        else:
65
            name = cls.__name__
12✔
66
            cv.check_type(f'{name} ID', uid, Integral)
12✔
67
            cv.check_greater_than(f'{name} ID', uid, 0, equality=True)
12✔
68
            if uid in cls.used_ids:
12✔
69
                msg = f'Another {name} instance already exists with id={uid}.'
12✔
70
                warn(msg, IDWarning)
12✔
71
            else:
72
                cls.used_ids.add(uid)
12✔
73
            self._id = uid
12✔
74

75
    @classmethod
12✔
76
    def reset_ids(cls):
12✔
77
        """Reset counters"""
78
        cls.used_ids.clear()
12✔
79
        cls.next_id = 1
12✔
80

81

82
def reset_auto_ids():
12✔
83
    """Reset counters for all auto-generated IDs"""
84
    for cls in IDManagerMixin.__subclasses__():
12✔
85
        cls.reset_ids()
12✔
86

87

88
def reserve_ids(ids, cls=None):
12✔
89
    """Reserve a set of IDs that won't be used for auto-generated IDs.
90

91
    Parameters
92
    ----------
93
    ids : iterable of int
94
        IDs to reserve
95
    cls : type or None
96
        Class for which IDs should be reserved (e.g., :class:`openmc.Cell`). If
97
        None, all classes that have auto-generated IDs will be used.
98

99
    """
UNCOV
100
    if cls is None:
×
UNCOV
101
        for cls in IDManagerMixin.__subclasses__():
×
UNCOV
102
            cls.used_ids |= set(ids)
×
103
    else:
UNCOV
104
        cls.used_ids |= set(ids)
×
105

106

107
def set_auto_id(next_id):
12✔
108
    """Set the next ID for auto-generated IDs.
109

110
    Parameters
111
    ----------
112
    next_id : int
113
        The next ID to assign to objects with auto-generated IDs.
114

115
    """
UNCOV
116
    for cls in IDManagerMixin.__subclasses__():
×
UNCOV
117
        cls.next_id = next_id
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc