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

openmc-dev / openmc / 29052849368

09 Jul 2026 09:53PM UTC coverage: 81.29% (+0.01%) from 81.28%
29052849368

Pull #4006

github

web-flow
Merge 1b1346d52 into 3cdb67e50
Pull Request #4006: Support cell densities per instances in plots

18198 of 26402 branches covered (68.93%)

Branch coverage included in aggregate %.

4 of 6 new or added lines in 2 files covered. (66.67%)

1 existing line in 1 file now uncovered.

59366 of 69014 relevant lines covered (86.02%)

48644690.94 hits per line

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

89.7
/openmc/lib/plot.py
1
from collections.abc import Mapping
11✔
2
from ctypes import (c_bool, c_int, c_size_t, c_int32,
11✔
3
                    c_double, c_uint8, Structure, POINTER)
4
from weakref import WeakValueDictionary
11✔
5

6
from ..exceptions import AllocationError, InvalidIDError
11✔
7
from . import _dll
11✔
8
from .core import _FortranObjectWithID
11✔
9
from .error import _error_handler
11✔
10

11
import numpy as np
11✔
12
import warnings
11✔
13

14

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

18
    C-type Attributes
19
    -----------------
20
    x : c_double
21
        Position's x value (default: 0.0)
22
    y : c_double
23
        Position's y value (default: 0.0)
24
    z : c_double
25
        Position's z value (default: 0.0)
26
    """
27
    _fields_ = [('x', c_double),
11✔
28
                ('y', c_double),
29
                ('z', c_double)]
30

31
    def __getitem__(self, idx):
11✔
32
        if idx == 0:
11✔
33
            return self.x
11✔
34
        elif idx == 1:
11✔
35
            return self.y
11✔
36
        elif idx == 2:
11✔
37
            return self.z
11✔
38
        else:
39
            raise IndexError(f"{idx} index is invalid for _Position")
11✔
40

41
    def __setitem__(self, idx, val):
11✔
42
        if idx == 0:
11✔
43
            self.x = val
11✔
44
        elif idx == 1:
11✔
45
            self.y = val
11✔
46
        elif idx == 2:
11✔
47
            self.z = val
11✔
48
        else:
49
            raise IndexError(f"{idx} index is invalid for _Position")
×
50

51
    def __repr__(self):
11✔
52
        return f"({self.x}, {self.y}, {self.z})"
×
53

54

55
def _extract_slice_data_args(plot):
11✔
56
    """Convert a legacy plot-like object into slice_data keyword arguments."""
57
    try:
11✔
58
        kwargs = {
11✔
59
            'origin': tuple(plot.origin),
60
            'width': (plot.width, plot.height),
61
            'basis': plot.basis,
62
            'pixels': (plot.h_res, plot.v_res),
63
            'show_overlaps': getattr(plot, 'color_overlaps', False),
64
            'level': getattr(plot, 'level', -1),
65
        }
66
    except AttributeError as exc:
×
67
        raise TypeError(
×
68
            "plot must be a legacy plot-like object with origin, width, "
69
            "height, basis, h_res, and v_res attributes."
70
        ) from exc
71
    return kwargs
11✔
72

73

74
_dll.openmc_slice_data.argtypes = [
11✔
75
    POINTER(c_double * 3),   # origin
76
    POINTER(c_double * 3),   # u_span
77
    POINTER(c_double * 3),   # v_span
78
    POINTER(c_size_t * 2),   # pixels
79
    c_bool,                  # show_overlaps
80
    c_int,                   # level
81
    c_int32,                 # filter_index
82
    POINTER(c_int32),        # geom_data
83
    POINTER(c_double),       # property_data (can be None)
84
]
85
_dll.openmc_slice_data.restype = c_int
11✔
86
_dll.openmc_slice_data.errcheck = _error_handler
11✔
87

88

89
def slice_data(origin, width=None, basis='xy', u_span=None, v_span=None,
11✔
90
                pixels=None, show_overlaps=False, level=None, filter=None,
91
                include_properties=True):
92
    """Generate a 2D raster of geometry and property data for plotting.
93

94
    Parameters
95
    ----------
96
    origin : sequence of float
97
        Center position of the plot [x, y, z]
98
    width : sequence of float
99
        Width of the plot [horizontal, vertical]. Mutually exclusive with
100
        u_span/v_span.
101
    basis : {'xy', 'xz', 'yz'} or int
102
        Plot basis. Ignored if u_span/v_span are provided.
103
    u_span : sequence of float, optional
104
        Full-width span vector for the horizontal axis (3 values). Mutually
105
        exclusive with width.
106
    v_span : sequence of float, optional
107
        Full-height span vector for the vertical axis (3 values). Mutually
108
        exclusive with width.
109
    pixels : sequence of int
110
        Number of pixels [horizontal, vertical]
111
    show_overlaps : bool, optional
112
        Whether to detect overlapping cells
113
    level : int, optional
114
        Universe level (None for deepest)
115
    filter : openmc.lib.Filter, optional
116
        Filter for bin index lookup
117
    include_properties : bool, optional
118
        Whether to compute temperature/density
119

120
    Returns
121
    -------
122
    geom_data : numpy.ndarray
123
        Array of shape (v_res, h_res, 3) or (v_res, h_res, 4) with int32 dtype.
124
        Contains [cell_id, cell_instance, material_id] when no filter is provided,
125
        or [cell_id, cell_instance, material_id, filter_bin] when a filter is provided.
126
    property_data : numpy.ndarray or None
127
        Array of shape (v_res, h_res, 2) with float64 dtype containing
128
        [temperature, density], or None if include_properties=False
129
    """
130
    # Set deepest level as default
131
    if level is None:
11✔
132
        level = -1
11✔
133
    if not isinstance(level, int):
11✔
NEW
134
        raise TypeError("level must be an integer.")
×
135

136
    if pixels is None:
11✔
137
        raise ValueError("pixels must be specified.")
×
138
    if len(pixels) != 2:
11✔
139
        raise ValueError("pixels must be a length-2 sequence.")
×
140

141
    if width is not None and (u_span is not None or v_span is not None):
11✔
142
        raise ValueError("width is mutually exclusive with u_span/v_span.")
×
143

144
    if u_span is not None or v_span is not None:
11✔
145
        if u_span is None or v_span is None:
11✔
146
            raise ValueError("Both u_span and v_span must be provided.")
×
147
        u_span = np.asarray(u_span, dtype=float)
11✔
148
        v_span = np.asarray(v_span, dtype=float)
11✔
149
        if u_span.shape != (3,) or v_span.shape != (3,):
11✔
150
            raise ValueError("u_span and v_span must be length-3 sequences.")
×
151
        u_norm = np.linalg.norm(u_span)
11✔
152
        v_norm = np.linalg.norm(v_span)
11✔
153
        if u_norm == 0.0 or v_norm == 0.0:
11✔
154
            raise ValueError("u_span and v_span must be non-zero vectors.")
×
155
        dot = float(np.dot(u_span, v_span))
11✔
156
        ortho_tol = 1.0e-10 * u_norm * v_norm
11✔
157
        if abs(dot) > ortho_tol:
11✔
158
            raise ValueError("u_span and v_span must be orthogonal.")
×
159
    else:
160
        if width is None:
11✔
161
            raise ValueError("width must be provided when u_span/v_span are not set.")
×
162
        if len(width) != 2:
11✔
163
            raise ValueError("width must be a length-2 sequence.")
×
164
        basis_map = {'xy': 1, 'xz': 2, 'yz': 3}
11✔
165
        if isinstance(basis, str):
11✔
166
            basis = basis.lower()
11✔
167
            if basis not in basis_map:
11✔
168
                raise ValueError(f"{basis} is not a valid plot basis.")
×
169
            basis = basis_map[basis]
11✔
170
        elif isinstance(basis, int):
×
171
            if basis not in basis_map.values():
×
172
                raise ValueError(f"{basis} is not a valid plot basis.")
×
173
        else:
174
            raise ValueError(f"{basis} is not a valid plot basis.")
×
175

176
        if basis == 1:
11✔
177
            u_span = np.array([width[0], 0.0, 0.0], dtype=float)
11✔
178
            v_span = np.array([0.0, width[1], 0.0], dtype=float)
11✔
179
        elif basis == 2:
11✔
180
            u_span = np.array([width[0], 0.0, 0.0], dtype=float)
11✔
181
            v_span = np.array([0.0, 0.0, width[1]], dtype=float)
11✔
182
        else:
183
            u_span = np.array([0.0, width[0], 0.0], dtype=float)
11✔
184
            v_span = np.array([0.0, 0.0, width[1]], dtype=float)
11✔
185

186
    origin = np.asarray(origin, dtype=float)
11✔
187
    if origin.shape != (3,):
11✔
188
        raise ValueError("origin must be a length-3 sequence.")
×
189

190
    # Prepare ctypes arrays
191
    origin_arr = (c_double * 3)(*origin)
11✔
192
    u_span_arr = (c_double * 3)(*u_span)
11✔
193
    v_span_arr = (c_double * 3)(*v_span)
11✔
194
    pixels_arr = (c_size_t * 2)(*pixels)
11✔
195

196
    # Get internal filter index from filter ID if filter is provided
197
    if filter is not None:
11✔
198
        filter_index = c_int32()
11✔
199
        _dll.openmc_get_filter_index(filter.id, filter_index)
11✔
200
        filter_index = filter_index.value
11✔
201
    else:
202
        filter_index = -1
11✔
203

204
    # Allocate output arrays with dynamic size based on filter
205
    n_geom_fields = 4 if filter is not None else 3
11✔
206
    geom_data = np.zeros((pixels[1], pixels[0], n_geom_fields), dtype=np.int32)
11✔
207
    if include_properties:
11✔
208
        property_data = np.zeros((pixels[1], pixels[0], 2), dtype=np.float64)
11✔
209
        prop_ptr = property_data.ctypes.data_as(POINTER(c_double))
11✔
210
    else:
211
        property_data = None
11✔
212
        prop_ptr = None
11✔
213

214
    _dll.openmc_slice_data(
11✔
215
        origin_arr,
216
        u_span_arr,
217
        v_span_arr,
218
        pixels_arr,
219
        show_overlaps,
220
        level,
221
        filter_index,
222
        geom_data.ctypes.data_as(POINTER(c_int32)),
223
        prop_ptr
224
    )
225

226
    return geom_data, property_data
11✔
227

228

229
def id_map(plot):
11✔
230
    """Deprecated compatibility wrapper for geometry ID maps.
231

232
    This function is kept for compatibility and will be removed in a future
233
    release. Use `slice_data(..., include_properties=False)` instead.
234
    """
235
    warnings.warn(
11✔
236
        "openmc.lib.id_map is deprecated and will be removed in a future "
237
        "release; use openmc.lib.slice_data(..., include_properties=False).",
238
        FutureWarning,
239
    )
240

241
    kwargs = _extract_slice_data_args(plot)
11✔
242
    geom_data, _ = slice_data(include_properties=False, **kwargs)
11✔
243
    return geom_data[:, :, :3]
11✔
244

245

246
def property_map(plot):
11✔
247
    """Deprecated compatibility wrapper for temperature/density maps.
248

249
    This function is kept for compatibility and will be removed in a future
250
    release. Use `slice_data(..., include_properties=True)` instead.
251
    """
252
    warnings.warn(
11✔
253
        "openmc.lib.property_map is deprecated and will be removed in a "
254
        "future release; use openmc.lib.slice_data(..., "
255
        "include_properties=True).",
256
        FutureWarning,
257
    )
258

259
    kwargs = _extract_slice_data_args(plot)
11✔
260
    _, prop_data = slice_data(include_properties=True, **kwargs)
11✔
261
    return prop_data
11✔
262

263

264
_dll.openmc_slice_data_overlap_count.argtypes = [POINTER(c_size_t)]
11✔
265
_dll.openmc_slice_data_overlap_count.restype = c_int
11✔
266
_dll.openmc_slice_data_overlap_count.errcheck = _error_handler
11✔
267

268
_dll.openmc_slice_data_overlap_info.argtypes = [c_size_t, POINTER(c_int32)]
11✔
269
_dll.openmc_slice_data_overlap_info.restype = c_int
11✔
270
_dll.openmc_slice_data_overlap_info.errcheck = _error_handler
11✔
271

272

273
# Python wrappings for overlap functions
274
def slice_data_overlap_count() -> int:
11✔
275
    """Return the number of unique overlaps from the last slice plot.
276

277
    Returns
278
    -------
279
    int
280
        Number of unique overlapping cell pairs detected by the most recent
281
        :func:`slice_data` call with overlap checking enabled.
282
    """
283
    count = c_size_t()
11✔
284
    _dll.openmc_slice_data_overlap_count(count)
11✔
285
    return count.value
11✔
286

287

288
def slice_data_overlap_info() -> np.ndarray:
11✔
289
    """Return identifying information for overlaps from the last slice plot.
290

291
    Returns
292
    -------
293
    numpy.ndarray
294
        Array of shape ``(n, 3)`` with int32 dtype, where ``n`` is the number
295
        of unique overlaps detected by the most recent :func:`slice_data` call
296
        with overlap checking enabled. Each row contains ``[universe_id,
297
        cell1_id, cell2_id]``.
298
    """
299
    n = slice_data_overlap_count()
11✔
300
    overlap_info = np.empty((n, 3), dtype=np.int32)
11✔
301

302
    if n > 0:
11✔
303
        _dll.openmc_slice_data_overlap_info(
11✔
304
            n,
305
            overlap_info.ctypes.data_as(POINTER(c_int32)),
306
        )
307
    return overlap_info
11✔
308

309

310
_dll.openmc_get_plot_index.argtypes = [c_int32, POINTER(c_int32)]
11✔
311
_dll.openmc_get_plot_index.restype = c_int
11✔
312
_dll.openmc_get_plot_index.errcheck = _error_handler
11✔
313

314
_dll.openmc_plot_get_id.argtypes = [c_int32, POINTER(c_int32)]
11✔
315
_dll.openmc_plot_get_id.restype = c_int
11✔
316
_dll.openmc_plot_get_id.errcheck = _error_handler
11✔
317

318
_dll.openmc_plot_set_id.argtypes = [c_int32, c_int32]
11✔
319
_dll.openmc_plot_set_id.restype = c_int
11✔
320
_dll.openmc_plot_set_id.errcheck = _error_handler
11✔
321

322
_dll.openmc_plots_size.restype = c_size_t
11✔
323

324
_dll.openmc_solidraytrace_plot_create.argtypes = [POINTER(c_int32)]
11✔
325
_dll.openmc_solidraytrace_plot_create.restype = c_int
11✔
326
_dll.openmc_solidraytrace_plot_create.errcheck = _error_handler
11✔
327

328
_dll.openmc_solidraytrace_plot_get_pixels.argtypes = [
11✔
329
    c_int32, POINTER(c_int32), POINTER(c_int32)]
330
_dll.openmc_solidraytrace_plot_get_pixels.restype = c_int
11✔
331
_dll.openmc_solidraytrace_plot_get_pixels.errcheck = _error_handler
11✔
332

333
_dll.openmc_solidraytrace_plot_set_pixels.argtypes = [c_int32, c_int32, c_int32]
11✔
334
_dll.openmc_solidraytrace_plot_set_pixels.restype = c_int
11✔
335
_dll.openmc_solidraytrace_plot_set_pixels.errcheck = _error_handler
11✔
336

337
_dll.openmc_solidraytrace_plot_get_color_by.argtypes = [c_int32, POINTER(c_int32)]
11✔
338
_dll.openmc_solidraytrace_plot_get_color_by.restype = c_int
11✔
339
_dll.openmc_solidraytrace_plot_get_color_by.errcheck = _error_handler
11✔
340

341
_dll.openmc_solidraytrace_plot_set_color_by.argtypes = [c_int32, c_int32]
11✔
342
_dll.openmc_solidraytrace_plot_set_color_by.restype = c_int
11✔
343
_dll.openmc_solidraytrace_plot_set_color_by.errcheck = _error_handler
11✔
344

345
_dll.openmc_solidraytrace_plot_set_default_colors.argtypes = [c_int32]
11✔
346
_dll.openmc_solidraytrace_plot_set_default_colors.restype = c_int
11✔
347
_dll.openmc_solidraytrace_plot_set_default_colors.errcheck = _error_handler
11✔
348

349
_dll.openmc_solidraytrace_plot_set_all_opaque.argtypes = [c_int32]
11✔
350
_dll.openmc_solidraytrace_plot_set_all_opaque.restype = c_int
11✔
351
_dll.openmc_solidraytrace_plot_set_all_opaque.errcheck = _error_handler
11✔
352

353
_dll.openmc_solidraytrace_plot_set_opaque.argtypes = [c_int32, c_int32, c_bool]
11✔
354
_dll.openmc_solidraytrace_plot_set_opaque.restype = c_int
11✔
355
_dll.openmc_solidraytrace_plot_set_opaque.errcheck = _error_handler
11✔
356

357
_dll.openmc_solidraytrace_plot_set_color.argtypes = [c_int32, c_int32, c_uint8, c_uint8, c_uint8]
11✔
358
_dll.openmc_solidraytrace_plot_set_color.restype = c_int
11✔
359
_dll.openmc_solidraytrace_plot_set_color.errcheck = _error_handler
11✔
360

361
_dll.openmc_solidraytrace_plot_get_camera_position.argtypes = [
11✔
362
    c_int32, POINTER(c_double), POINTER(c_double), POINTER(c_double)]
363
_dll.openmc_solidraytrace_plot_get_camera_position.restype = c_int
11✔
364
_dll.openmc_solidraytrace_plot_get_camera_position.errcheck = _error_handler
11✔
365

366
_dll.openmc_solidraytrace_plot_set_camera_position.argtypes = [c_int32, c_double, c_double, c_double]
11✔
367
_dll.openmc_solidraytrace_plot_set_camera_position.restype = c_int
11✔
368
_dll.openmc_solidraytrace_plot_set_camera_position.errcheck = _error_handler
11✔
369

370
_dll.openmc_solidraytrace_plot_get_look_at.argtypes = [
11✔
371
    c_int32, POINTER(c_double), POINTER(c_double), POINTER(c_double)]
372
_dll.openmc_solidraytrace_plot_get_look_at.restype = c_int
11✔
373
_dll.openmc_solidraytrace_plot_get_look_at.errcheck = _error_handler
11✔
374

375
_dll.openmc_solidraytrace_plot_set_look_at.argtypes = [c_int32, c_double, c_double, c_double]
11✔
376
_dll.openmc_solidraytrace_plot_set_look_at.restype = c_int
11✔
377
_dll.openmc_solidraytrace_plot_set_look_at.errcheck = _error_handler
11✔
378

379
_dll.openmc_solidraytrace_plot_get_up.argtypes = [
11✔
380
    c_int32, POINTER(c_double), POINTER(c_double), POINTER(c_double)]
381
_dll.openmc_solidraytrace_plot_get_up.restype = c_int
11✔
382
_dll.openmc_solidraytrace_plot_get_up.errcheck = _error_handler
11✔
383

384
_dll.openmc_solidraytrace_plot_set_up.argtypes = [c_int32, c_double, c_double, c_double]
11✔
385
_dll.openmc_solidraytrace_plot_set_up.restype = c_int
11✔
386
_dll.openmc_solidraytrace_plot_set_up.errcheck = _error_handler
11✔
387

388
_dll.openmc_solidraytrace_plot_get_light_position.argtypes = [
11✔
389
    c_int32, POINTER(c_double), POINTER(c_double), POINTER(c_double)]
390
_dll.openmc_solidraytrace_plot_get_light_position.restype = c_int
11✔
391
_dll.openmc_solidraytrace_plot_get_light_position.errcheck = _error_handler
11✔
392

393
_dll.openmc_solidraytrace_plot_set_light_position.argtypes = [c_int32, c_double, c_double, c_double]
11✔
394
_dll.openmc_solidraytrace_plot_set_light_position.restype = c_int
11✔
395
_dll.openmc_solidraytrace_plot_set_light_position.errcheck = _error_handler
11✔
396

397
_dll.openmc_solidraytrace_plot_get_fov.argtypes = [c_int32, POINTER(c_double)]
11✔
398
_dll.openmc_solidraytrace_plot_get_fov.restype = c_int
11✔
399
_dll.openmc_solidraytrace_plot_get_fov.errcheck = _error_handler
11✔
400

401
_dll.openmc_solidraytrace_plot_set_fov.argtypes = [c_int32, c_double]
11✔
402
_dll.openmc_solidraytrace_plot_set_fov.restype = c_int
11✔
403
_dll.openmc_solidraytrace_plot_set_fov.errcheck = _error_handler
11✔
404

405
_dll.openmc_solidraytrace_plot_update_view.argtypes = [c_int32]
11✔
406
_dll.openmc_solidraytrace_plot_update_view.restype = c_int
11✔
407
_dll.openmc_solidraytrace_plot_update_view.errcheck = _error_handler
11✔
408

409
_dll.openmc_solidraytrace_plot_create_image.argtypes = [c_int32, POINTER(c_uint8), c_int32, c_int32]
11✔
410
_dll.openmc_solidraytrace_plot_create_image.restype = c_int
11✔
411
_dll.openmc_solidraytrace_plot_create_image.errcheck = _error_handler
11✔
412

413
_dll.openmc_solidraytrace_plot_get_color.argtypes = [c_int32, c_int32,
11✔
414
                                             POINTER(c_uint8), POINTER(c_uint8), POINTER(c_uint8)]
415
_dll.openmc_solidraytrace_plot_get_color.restype = c_int
11✔
416
_dll.openmc_solidraytrace_plot_get_color.errcheck = _error_handler
11✔
417

418
_dll.openmc_solidraytrace_plot_get_diffuse_fraction.argtypes = [
11✔
419
    c_int32, POINTER(c_double)]
420
_dll.openmc_solidraytrace_plot_get_diffuse_fraction.restype = c_int
11✔
421
_dll.openmc_solidraytrace_plot_get_diffuse_fraction.errcheck = _error_handler
11✔
422

423
_dll.openmc_solidraytrace_plot_set_diffuse_fraction.argtypes = [c_int32, c_double]
11✔
424
_dll.openmc_solidraytrace_plot_set_diffuse_fraction.restype = c_int
11✔
425
_dll.openmc_solidraytrace_plot_set_diffuse_fraction.errcheck = _error_handler
11✔
426

427

428
class SolidRayTracePlot(_FortranObjectWithID):
11✔
429
    """Solid ray-traced plot stored internally.
430

431
    This class exposes a solid ray-traced plot that is stored internally in
432
    the OpenMC library. To obtain a view of an existing plot with a given ID,
433
    use the :data:`openmc.lib.plots` mapping.
434

435
    Parameters
436
    ----------
437
    uid : int or None
438
        Unique ID of the plot
439
    new : bool
440
        When `index` is None, this argument controls whether a new object is
441
        created or a view of an existing object is returned.
442
    index : int or None
443
        Index in the internal plots array.
444

445
    Attributes
446
    ----------
447
    id : int
448
        Unique ID of the plot.
449
    pixels : tuple of int
450
        Plot image dimensions as ``(width, height)``.
451
    color_by : int
452
        Coloring mode. Use :attr:`COLOR_BY_MATERIAL` or
453
        :attr:`COLOR_BY_CELL`.
454
    camera_position : tuple of float
455
        Camera position as ``(x, y, z)``.
456
    look_at : tuple of float
457
        Point the camera is aimed at as ``(x, y, z)``.
458
    up : tuple of float
459
        Up direction as ``(x, y, z)``.
460
    light_position : tuple of float
461
        Position of the light source as ``(x, y, z)``.
462
    fov : float
463
        Horizontal field-of-view angle in degrees.
464
    diffuse_fraction : float
465
        Fraction of reflected light treated as diffuse (0 to 1).
466
    """
467

468
    COLOR_BY_MATERIAL = 0
11✔
469
    COLOR_BY_CELL = 1
11✔
470
    __instances = WeakValueDictionary()
11✔
471

472
    def __new__(cls, uid=None, new=True, index=None):
11✔
473
        mapping = plots
11✔
474
        if index is None:
11✔
475
            if new:
11✔
476
                if uid is not None and uid in mapping:
11✔
477
                    raise AllocationError(
×
478
                        f'A plot with ID={uid} has already been allocated.'
479
                    )
480
                index = c_int32()
11✔
481
                _dll.openmc_solidraytrace_plot_create(index)
11✔
482
                index = index.value
11✔
483
            else:
484
                index = mapping[uid]._index
×
485

486
        if index not in cls.__instances:
11✔
487
            instance = super().__new__(cls)
11✔
488
            instance._index = index
11✔
489
            if uid is not None:
11✔
490
                instance.id = uid
×
491
            cls.__instances[index] = instance
11✔
492

493
        return cls.__instances[index]
11✔
494

495
    def __init__(self, uid=None, new=True, index=None):
11✔
496
        super().__init__(uid, new, index)
11✔
497

498
    @property
11✔
499
    def id(self):
11✔
500
        plot_id = c_int32()
11✔
501
        _dll.openmc_plot_get_id(self._index, plot_id)
11✔
502
        return plot_id.value
11✔
503

504
    @id.setter
11✔
505
    def id(self, plot_id):
11✔
506
        _dll.openmc_plot_set_id(self._index, plot_id)
×
507

508
    @staticmethod
11✔
509
    def _get_xyz(getter, index):
11✔
510
        x = c_double()
11✔
511
        y = c_double()
11✔
512
        z = c_double()
11✔
513
        getter(index, x, y, z)
11✔
514
        return (x.value, y.value, z.value)
11✔
515

516
    @staticmethod
11✔
517
    def _set_xyz(setter, index, xyz):
11✔
518
        x, y, z = xyz
11✔
519
        setter(index, float(x), float(y), float(z))
11✔
520

521
    @property
11✔
522
    def pixels(self):
11✔
523
        width = c_int32()
11✔
524
        height = c_int32()
11✔
525
        _dll.openmc_solidraytrace_plot_get_pixels(self._index, width, height)
11✔
526
        return (width.value, height.value)
11✔
527

528
    @pixels.setter
11✔
529
    def pixels(self, pixels):
11✔
530
        width, height = pixels
11✔
531
        _dll.openmc_solidraytrace_plot_set_pixels(
11✔
532
            self._index, int(width), int(height))
533

534
    @property
11✔
535
    def color_by(self):
11✔
536
        color_by = c_int32()
11✔
537
        _dll.openmc_solidraytrace_plot_get_color_by(self._index, color_by)
11✔
538
        return color_by.value
11✔
539

540
    @color_by.setter
11✔
541
    def color_by(self, color_by):
11✔
542
        _dll.openmc_solidraytrace_plot_set_color_by(self._index, int(color_by))
11✔
543

544
    def set_default_colors(self):
11✔
545
        _dll.openmc_solidraytrace_plot_set_default_colors(self._index)
11✔
546

547
    def set_all_opaque(self):
11✔
548
        _dll.openmc_solidraytrace_plot_set_all_opaque(self._index)
×
549

550
    def set_visibility(self, domain_id, visible):
11✔
551
        _dll.openmc_solidraytrace_plot_set_opaque(
11✔
552
            self._index, int(domain_id), bool(visible)
553
        )
554

555
    def set_color(self, domain_id, color):
11✔
556
        r, g, b = [int(c) for c in color]
11✔
557
        _dll.openmc_solidraytrace_plot_set_color(
11✔
558
            self._index, int(domain_id), r, g, b)
559

560
    @property
11✔
561
    def camera_position(self):
11✔
562
        return self._get_xyz(_dll.openmc_solidraytrace_plot_get_camera_position,
11✔
563
                             self._index)
564

565
    @camera_position.setter
11✔
566
    def camera_position(self, position):
11✔
567
        self._set_xyz(_dll.openmc_solidraytrace_plot_set_camera_position,
11✔
568
                      self._index, position)
569

570
    @property
11✔
571
    def look_at(self):
11✔
572
        return self._get_xyz(_dll.openmc_solidraytrace_plot_get_look_at,
11✔
573
                             self._index)
574

575
    @look_at.setter
11✔
576
    def look_at(self, position):
11✔
577
        self._set_xyz(_dll.openmc_solidraytrace_plot_set_look_at,
11✔
578
                      self._index, position)
579

580
    @property
11✔
581
    def up(self):
11✔
582
        return self._get_xyz(_dll.openmc_solidraytrace_plot_get_up, self._index)
11✔
583

584
    @up.setter
11✔
585
    def up(self, direction):
11✔
586
        self._set_xyz(_dll.openmc_solidraytrace_plot_set_up, self._index,
11✔
587
                      direction)
588

589
    @property
11✔
590
    def light_position(self):
11✔
591
        return self._get_xyz(_dll.openmc_solidraytrace_plot_get_light_position,
11✔
592
                             self._index)
593

594
    @light_position.setter
11✔
595
    def light_position(self, position):
11✔
596
        self._set_xyz(_dll.openmc_solidraytrace_plot_set_light_position,
11✔
597
                      self._index, position)
598

599
    @property
11✔
600
    def fov(self):
11✔
601
        fov = c_double()
11✔
602
        _dll.openmc_solidraytrace_plot_get_fov(self._index, fov)
11✔
603
        return fov.value
11✔
604

605
    @fov.setter
11✔
606
    def fov(self, fov):
11✔
607
        _dll.openmc_solidraytrace_plot_set_fov(self._index, float(fov))
11✔
608

609
    def update_view(self):
11✔
610
        _dll.openmc_solidraytrace_plot_update_view(self._index)
11✔
611

612
    def create_image(self):
11✔
613
        width, height = self.pixels
11✔
614
        image = np.zeros((height, width, 3), dtype=np.uint8)
11✔
615
        _dll.openmc_solidraytrace_plot_create_image(
11✔
616
            self._index,
617
            image.ctypes.data_as(POINTER(c_uint8)),
618
            width,
619
            height
620
        )
621
        return image
11✔
622

623
    def get_color(self, domain_id):
11✔
624
        r = c_uint8()
11✔
625
        g = c_uint8()
11✔
626
        b = c_uint8()
11✔
627
        _dll.openmc_solidraytrace_plot_get_color(
11✔
628
            self._index, int(domain_id), r, g, b)
629
        return int(r.value), int(g.value), int(b.value)
11✔
630

631
    @property
11✔
632
    def diffuse_fraction(self):
11✔
633
        value = c_double()
11✔
634
        _dll.openmc_solidraytrace_plot_get_diffuse_fraction(self._index, value)
11✔
635
        return value.value
11✔
636

637
    @diffuse_fraction.setter
11✔
638
    def diffuse_fraction(self, value):
11✔
639
        _dll.openmc_solidraytrace_plot_set_diffuse_fraction(
11✔
640
            self._index, float(value))
641

642
    # Backward-compatible setter aliases
643
    def set_pixels(self, width, height):
11✔
644
        self.pixels = (width, height)
×
645

646
    def set_color_by(self, color_by):
11✔
647
        self.color_by = color_by
×
648

649
    def set_camera_position(self, x, y, z):
11✔
650
        self.camera_position = (x, y, z)
×
651

652
    def set_look_at(self, x, y, z):
11✔
653
        self.look_at = (x, y, z)
×
654

655
    def set_up(self, x, y, z):
11✔
656
        self.up = (x, y, z)
×
657

658
    def set_light_position(self, x, y, z):
11✔
659
        self.light_position = (x, y, z)
×
660

661
    def set_fov(self, fov):
11✔
662
        self.fov = fov
×
663

664
    def set_diffuse_fraction(self, value):
11✔
665
        self.diffuse_fraction = value
×
666

667

668
class _PlotMapping(Mapping):
11✔
669
    def __getitem__(self, key):
11✔
670
        index = c_int32()
11✔
671
        try:
11✔
672
            _dll.openmc_get_plot_index(key, index)
11✔
673
        except (AllocationError, InvalidIDError) as e:
×
674
            raise KeyError(str(e))
×
675
        return SolidRayTracePlot(index=index.value)
11✔
676

677
    def __iter__(self):
11✔
678
        for i in range(len(self)):
×
679
            yield SolidRayTracePlot(index=i).id
×
680

681
    def __len__(self):
11✔
682
        return _dll.openmc_plots_size()
11✔
683

684
    def __repr__(self):
11✔
685
        return repr(dict(self))
×
686

687

688
plots = _PlotMapping()
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