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

openmc-dev / openmc / 20278515727

16 Dec 2025 06:26PM UTC coverage: 81.822% (-0.1%) from 81.92%
20278515727

Pull #3493

github

web-flow
Merge 7f36869a3 into bbfa18d72
Pull Request #3493: Implement vector fitting to replace external `vectfit` package

17013 of 23575 branches covered (72.17%)

Branch coverage included in aggregate %.

188 of 207 new or added lines in 3 files covered. (90.82%)

3101 existing lines in 56 files now uncovered.

54973 of 64404 relevant lines covered (85.36%)

41385524.25 hits per line

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

76.86
/openmc/lib/plot.py
1
from ctypes import (c_bool, c_int, c_size_t, c_int32,
10✔
2
                    c_double, Structure, POINTER)
3

4
from . import _dll
10✔
5
from .error import _error_handler
10✔
6

7
import numpy as np
10✔
8

9

10
class _Position(Structure):
10✔
11
    """Definition of an xyz location in space with underlying c-types
12

13
    C-type Attributes
14
    -----------------
15
    x : c_double
16
        Position's x value (default: 0.0)
17
    y : c_double
18
        Position's y value (default: 0.0)
19
    z : c_double
20
        Position's z value (default: 0.0)
21
    """
22
    _fields_ = [('x', c_double),
10✔
23
                ('y', c_double),
24
                ('z', c_double)]
25

26
    def __getitem__(self, idx):
10✔
27
        if idx == 0:
10✔
28
            return self.x
10✔
29
        elif idx == 1:
10✔
30
            return self.y
10✔
31
        elif idx == 2:
10✔
32
            return self.z
10✔
33
        else:
34
            raise IndexError(f"{idx} index is invalid for _Position")
10✔
35

36
    def __setitem__(self, idx, val):
10✔
37
        if idx == 0:
10✔
38
            self.x = val
10✔
39
        elif idx == 1:
10✔
40
            self.y = val
10✔
41
        elif idx == 2:
10✔
42
            self.z = val
10✔
43
        else:
44
            raise IndexError(f"{idx} index is invalid for _Position")
×
45

46
    def __repr__(self):
10✔
47
        return f"({self.x}, {self.y}, {self.z})"
×
48

49

50
class _PlotBase(Structure):
10✔
51
    """A structure defining a 2-D geometry slice with underlying c-types
52

53
    C-Type Attributes
54
    -----------------
55
    origin_ : openmc.lib.plot._Position
56
        A position defining the origin of the plot.
57
    width_ : openmc.lib.plot._Position
58
        The width of the plot along the x, y, and z axes, respectively
59
    basis_ : c_int
60
        The axes basis of the plot view.
61
    pixels_ : c_size_t[3]
62
        The resolution of the plot in the horizontal and vertical dimensions
63
    color_overlaps_ : c_bool
64
        Whether to assign unique IDs (-3) to overlapping regions.
65
    level_ : c_int
66
        The universe level for the plot view
67

68
    Attributes
69
    ----------
70
    origin : tuple or list of ndarray
71
        Origin (center) of the plot
72
    width : float
73
        The horizontal dimension of the plot in geometry units (cm)
74
    height : float
75
        The vertical dimension of the plot in geometry units (cm)
76
    basis : string
77
        One of {'xy', 'xz', 'yz'} indicating the horizontal and vertical
78
        axes of the plot.
79
    h_res : int
80
        The horizontal resolution of the plot in pixels
81
    v_res : int
82
        The vertical resolution of the plot in pixels
83
    level : int
84
        The universe level for the plot (default: -1 -> all universes shown)
85
    """
86
    _fields_ = [('origin_', _Position),
10✔
87
                ('width_', _Position),
88
                ('basis_', c_int),
89
                ('pixels_', 3*c_size_t),
90
                ('color_overlaps_', c_bool),
91
                ('level_', c_int)]
92

93
    def __init__(self):
10✔
94
        self.level_ = -1
10✔
95
        self.basis_ = 1
10✔
96
        self.color_overlaps_ = False
10✔
97

98
    @property
10✔
99
    def origin(self):
10✔
UNCOV
100
        return self.origin_
×
101

102
    @origin.setter
10✔
103
    def origin(self, origin):
10✔
104
        self.origin_.x = origin[0]
10✔
105
        self.origin_.y = origin[1]
10✔
106
        self.origin_.z = origin[2]
10✔
107

108
    @property
10✔
109
    def width(self):
10✔
UNCOV
110
        return self.width_.x
×
111

112
    @width.setter
10✔
113
    def width(self, width):
10✔
114
        self.width_.x = width
10✔
115

116
    @property
10✔
117
    def height(self):
10✔
UNCOV
118
        return self.width_.y
×
119

120
    @height.setter
10✔
121
    def height(self, height):
10✔
122
        self.width_.y = height
10✔
123

124
    @property
10✔
125
    def basis(self):
10✔
126
        if self.basis_ == 1:
×
127
            return 'xy'
×
128
        elif self.basis_ == 2:
×
129
            return 'xz'
×
UNCOV
130
        elif self.basis_ == 3:
×
131
            return 'yz'
×
132

UNCOV
133
        raise ValueError(f"Plot basis {self.basis_} is invalid")
×
134

135
    @basis.setter
10✔
136
    def basis(self, basis):
10✔
137
        if isinstance(basis, str):
10✔
138
            valid_bases = ('xy', 'xz', 'yz')
10✔
139
            basis = basis.lower()
10✔
140
            if basis not in valid_bases:
10✔
UNCOV
141
                raise ValueError(f"{basis} is not a valid plot basis.")
×
142

143
            if basis == 'xy':
10✔
144
                self.basis_ = 1
10✔
145
            elif basis == 'xz':
×
146
                self.basis_ = 2
×
UNCOV
147
            elif basis == 'yz':
×
UNCOV
148
                self.basis_ = 3
×
149
            return
10✔
150

151
        if isinstance(basis, int):
×
152
            valid_bases = (1, 2, 3)
×
153
            if basis not in valid_bases:
×
154
                raise ValueError(f"{basis} is not a valid plot basis.")
×
UNCOV
155
            self.basis_ = basis
×
156
            return
×
157

UNCOV
158
        raise ValueError(f"{basis} of type {type(basis)} is an invalid plot basis")
×
159

160
    @property
10✔
161
    def h_res(self):
10✔
162
        return self.pixels_[0]
10✔
163

164
    @h_res.setter
10✔
165
    def h_res(self, h_res):
10✔
166
        self.pixels_[0] = h_res
10✔
167

168
    @property
10✔
169
    def v_res(self):
10✔
170
        return self.pixels_[1]
10✔
171

172
    @v_res.setter
10✔
173
    def v_res(self, v_res):
10✔
174
        self.pixels_[1] = v_res
10✔
175

176
    @property
10✔
177
    def level(self):
10✔
UNCOV
178
        return int(self.level_)
×
179

180
    @level.setter
10✔
181
    def level(self, level):
10✔
182
        self.level_ = level
10✔
183

184
    @property
10✔
185
    def color_overlaps(self):
10✔
UNCOV
186
        return self.color_overlaps_
×
187

188
    @color_overlaps.setter
10✔
189
    def color_overlaps(self, color_overlaps):
10✔
190
        self.color_overlaps_ = color_overlaps
10✔
191

192
    def __repr__(self):
10✔
UNCOV
193
        out_str = ["-----",
×
194
                   "Plot:",
195
                   "-----",
196
                   f"Origin: {self.origin}",
197
                   f"Width: {self.width}",
198
                   f"Height: {self.height}",
199
                   f"Basis: {self.basis}",
200
                   f"HRes: {self.h_res}",
201
                   f"VRes: {self.v_res}",
202
                   f"Color Overlaps: {self.color_overlaps}",
203
                   f"Level: {self.level}"]
UNCOV
204
        return '\n'.join(out_str)
×
205

206

207
_dll.openmc_id_map.argtypes = [POINTER(_PlotBase), POINTER(c_int32)]
10✔
208
_dll.openmc_id_map.restype = c_int
10✔
209
_dll.openmc_id_map.errcheck = _error_handler
10✔
210

211

212
def id_map(plot):
10✔
213
    """
214
    Generate a 2-D map of cell and material IDs. Used for in-memory image
215
    generation.
216

217
    Parameters
218
    ----------
219
    plot : openmc.lib.plot._PlotBase
220
        Object describing the slice of the model to be generated
221

222
    Returns
223
    -------
224
    id_map : numpy.ndarray
225
        A NumPy array with shape (vertical pixels, horizontal pixels, 3) of
226
        OpenMC property ids with dtype int32. The last dimension of the array
227
        contains, in order, cell IDs, cell instances, and material IDs.
228

229
    """
230
    img_data = np.zeros((plot.v_res, plot.h_res, 3),
10✔
231
                        dtype=np.dtype('int32'))
232
    _dll.openmc_id_map(plot, img_data.ctypes.data_as(POINTER(c_int32)))
10✔
233
    return img_data
10✔
234

235

236
_dll.openmc_property_map.argtypes = [POINTER(_PlotBase), POINTER(c_double)]
10✔
237
_dll.openmc_property_map.restype = c_int
10✔
238
_dll.openmc_property_map.errcheck = _error_handler
10✔
239

240

241
def property_map(plot):
10✔
242
    """
243
    Generate a 2-D map of cell temperatures and material densities. Used for
244
    in-memory image generation.
245

246
    Parameters
247
    ----------
248
    plot : openmc.lib.plot._PlotBase
249
        Object describing the slice of the model to be generated
250

251
    Returns
252
    -------
253
    property_map : numpy.ndarray
254
        A NumPy array with shape (vertical pixels, horizontal pixels, 2) of
255
        OpenMC property ids with dtype float
256

257
    """
258
    prop_data = np.zeros((plot.v_res, plot.h_res, 2))
10✔
259
    _dll.openmc_property_map(plot, prop_data.ctypes.data_as(POINTER(c_double)))
10✔
260
    return prop_data
10✔
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

© 2026 Coveralls, Inc