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

openmc-dev / openmc / 20113241616

10 Dec 2025 09:05PM UTC coverage: 82.165% (+0.07%) from 82.1%
20113241616

Pull #3674

github

web-flow
Merge 589cc1264 into d09fbc61b
Pull Request #3674: Add user documentation for DAGMCUniverse synchronization

16996 of 23543 branches covered (72.19%)

Branch coverage included in aggregate %.

55087 of 64187 relevant lines covered (85.82%)

43499274.33 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,
11✔
2
                    c_double, Structure, POINTER)
3

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

7
import numpy as np
11✔
8

9

10
class _Position(Structure):
11✔
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),
11✔
23
                ('y', c_double),
24
                ('z', c_double)]
25

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

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

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

49

50
class _PlotBase(Structure):
11✔
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),
11✔
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):
11✔
94
        self.level_ = -1
11✔
95
        self.basis_ = 1
11✔
96
        self.color_overlaps_ = False
11✔
97

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

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

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

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

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

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

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

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

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

143
            if basis == 'xy':
11✔
144
                self.basis_ = 1
11✔
145
            elif basis == 'xz':
×
146
                self.basis_ = 2
×
147
            elif basis == 'yz':
×
148
                self.basis_ = 3
×
149
            return
11✔
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.")
×
155
            self.basis_ = basis
×
156
            return
×
157

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

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

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

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

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

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

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

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

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

192
    def __repr__(self):
11✔
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}"]
204
        return '\n'.join(out_str)
×
205

206

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

211

212
def id_map(plot):
11✔
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),
11✔
231
                        dtype=np.dtype('int32'))
232
    _dll.openmc_id_map(plot, img_data.ctypes.data_as(POINTER(c_int32)))
11✔
233
    return img_data
11✔
234

235

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

240

241
def property_map(plot):
11✔
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))
11✔
259
    _dll.openmc_property_map(plot, prop_data.ctypes.data_as(POINTER(c_double)))
11✔
260
    return prop_data
11✔
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