-
Notifications
You must be signed in to change notification settings - Fork 17
/
plotmodel.py
1336 lines (1126 loc) · 49.2 KB
/
plotmodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from ast import literal_eval
from collections import defaultdict
import copy
import itertools
import threading
import os
import pickle
import hashlib
from PySide2.QtWidgets import QItemDelegate, QColorDialog, QLineEdit, QMessageBox
from PySide2.QtCore import QAbstractTableModel, QModelIndex, Qt, QSize, QEvent
from PySide2.QtGui import QColor
import openmc
import openmc.lib
import numpy as np
from .statepointmodel import StatePointModel
from .plot_colors import random_rgb, reset_seed
ID, NAME, COLOR, COLORLABEL, MASK, HIGHLIGHT = tuple(range(0, 6))
__VERSION__ = "0.2.2"
_VOID_REGION = -1
_NOT_FOUND = -2
_OVERLAP = -3
_MODEL_PROPERTIES = ('temperature', 'density')
_PROPERTY_INDICES = {'temperature': 0, 'density': 1}
_REACTION_UNITS = 'Reactions per Source Particle'
_FLUX_UNITS = 'Particle-cm per Source Particle'
_PRODUCTION_UNITS = 'Particles Produced per Source Particle'
_ENERGY_UNITS = 'eV per Source Particle'
_SPATIAL_FILTERS = (openmc.UniverseFilter,
openmc.MaterialFilter,
openmc.CellFilter,
openmc.DistribcellFilter,
openmc.CellInstanceFilter,
openmc.MeshFilter)
_PRODUCTIONS = ('delayed-nu-fission', 'prompt-nu-fission', 'nu-fission',
'nu-scatter', 'H1-production', 'H2-production',
'H3-production', 'He3-production', 'He4-production')
_SCORE_UNITS = {p: _PRODUCTION_UNITS for p in _PRODUCTIONS}
_SCORE_UNITS['flux'] = 'Particle-cm/Particle'
_SCORE_UNITS['current'] = 'Particles per source Particle'
_SCORE_UNITS['events'] = 'Events per Source Particle'
_SCORE_UNITS['inverse-velocity'] = 'Particle-seconds per Source Particle'
_SCORE_UNITS['heating'] = _ENERGY_UNITS
_SCORE_UNITS['heating-local'] = _ENERGY_UNITS
_SCORE_UNITS['kappa-fission'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-prompt'] = _ENERGY_UNITS
_SCORE_UNITS['fission-q-recoverable'] = _ENERGY_UNITS
_SCORE_UNITS['decay-rate'] = 'Seconds^-1'
_SCORE_UNITS['damage-energy'] = _ENERGY_UNITS
_TALLY_VALUES = {'Mean': 'mean',
'Std. Dev.': 'std_dev',
'Rel. Error': 'rel_err'}
def hash_file(filename):
# return the md5 hash of a file
h = hashlib.md5()
with open(filename,'rb') as file:
chunk = 0
while chunk != b'':
# read 32768 bytes at a time
chunk = file.read(32768)
h.update(chunk)
return h.hexdigest()
class PlotModel():
""" Geometry and plot settings for OpenMC Plot Explorer model
Parameters
----------
use_settings_pkl : bool
If True, use plot_settings.pkl file to reload settings
Attributes
----------
geom : openmc.Geometry instance
OpenMC Geometry of the model
modelCells : collections.OrderedDict
Dictionary mapping cell IDs to openmc.Cell instances
modelMaterials : collections.OrderedDict
Dictionary mapping material IDs to openmc.Material instances
ids : NumPy int array (v_res, h_res, 1)
Mapping of plot coordinates to cell/material ID by pixel
ids_map : NumPy int32 array (v_res, h_res, 3)
Mapping of cell and material ids
properties : Numpy float array (v_res, h_res, 3)
Mapping of cell temperatures and material densities
image : NumPy int array (v_res, h_res, 3)
The current RGB image data
statepoint : StatePointModel
Simulation data model used to display tally results
applied_filters : tuple of ints
IDs of the applied filters for the displayed tally
previousViews : list of PlotView instances
List of previously created plot view settings used to undo
changes made in plot explorer
subsequentViews : list of PlotView instances
List of undone plot view settings used to redo changes made
in plot explorer
defaultView : PlotView instance
Default settings for given geometry
currentView : PlotView instance
Currently displayed plot settings in plot explorer
activeView : PlotView instance
Active state of settings in plot explorer, which may or may not
have unapplied changes
"""
def __init__(self, use_settings_pkl):
""" Initialize PlotModel class attributes """
# Retrieve OpenMC Cells/Materials
self.modelCells = openmc.lib.cells
self.modelMaterials = openmc.lib.materials
self.max_universe_levels = openmc.lib._coord_levels()
# Cell/Material ID by coordinates
self.ids = None
# Return values from id_map and property_map
self.ids_map = None
self.properties = None
self.version = __VERSION__
# default statepoint value
self._statepoint = None
# default tally/filter info
self.appliedFilters = ()
self.appliedScores = ()
self.appliedNuclides = ()
# reset random number seed for consistent
# coloring when reloading a model
reset_seed()
self.previousViews = []
self.subsequentViews = []
self.defaultView = self.getDefaultView()
if use_settings_pkl and os.path.isfile('plot_settings.pkl'):
with open('plot_settings.pkl', 'rb') as file:
try:
data = pickle.load(file)
except AttributeError:
msg_box = QMessageBox()
msg = "WARNING: previous plot settings are in an incompatible format. " +\
"They will be ignored."
msg_box.setText(msg)
msg_box.setIcon(QMessageBox.Warning)
msg_box.setStandardButtons(QMessageBox.Ok)
msg_box.exec_()
self.currentView = copy.deepcopy(self.defaultView)
else:
restore_domains = False
# check GUI version
if data['version'] != self.version:
print("WARNING: previous plot settings are for a different "
"version of the GUI. They will be ignored.")
wrn_msg = "Existing version: {}, Current GUI version: {}"
print(wrn_msg.format(data['version'], self.version))
view = None
else:
view = data['currentView']
# get materials.xml and geometry.xml hashes to
# restore additional settings if possible
mat_xml_hash = hash_file('materials.xml')
geom_xml_hash = hash_file('geometry.xml')
if mat_xml_hash == data['mat_xml_hash'] and \
geom_xml_hash == data['geom_xml_hash']:
restore_domains = True
# restore statepoint file
try:
self.statepoint = data['statepoint']
except OSError:
msg_box = QMessageBox()
msg = "Could not open statepoint file: \n\n {} \n"
msg_box.setText(msg.format(self.model.statepoint.filename))
msg_box.setIcon(QMessageBox.Warning)
msg_box.setStandardButtons(QMessageBox.Ok)
msg_box.exec_()
self.statepoint = None
self.currentView = PlotView(restore_view=view,
restore_domains=restore_domains)
else:
self.currentView = copy.deepcopy(self.defaultView)
self.activeView = copy.deepcopy(self.currentView)
def openStatePoint(self, filename):
self.statepoint = StatePointModel(filename, open_file=True)
@property
def statepoint(self):
return self._statepoint
@statepoint.setter
def statepoint(self, statepoint):
if statepoint is None:
self._statepoint = None
elif isinstance(statepoint, StatePointModel):
self._statepoint = statepoint
elif isinstance(statepoint, str):
self._statepoint = StatePointModel(statepoint, open_file=True)
else:
raise TypeError("Invalid statepoint object")
if self._statepoint and not self._statepoint.is_open:
self._statepoint.open()
def getDefaultView(self):
""" Generates default PlotView instance for OpenMC geometry
Centers plot view origin in every dimension if possible. Defaults
to xy basis, with height and width to accomodate full size of
geometry. Defaults to (0, 0, 0) origin with width and heigth of
25 if geometry bounding box cannot be generated.
Returns
-------
default : PlotView instance
PlotView instance with default view settings
"""
lower_left, upper_right = openmc.lib.global_bounding_box()
# Check for valid bounding_box dimensions
if -np.inf not in lower_left[:2] and np.inf not in upper_right[:2]:
xcenter = (upper_right[0] + lower_left[0])/2
width = abs(upper_right[0] - lower_left[0]) * 1.005
ycenter = (upper_right[1] + lower_left[1])/2
height = abs(upper_right[1] - lower_left[1]) * 1.005
else:
xcenter, ycenter, width, height = (0.00, 0.00, 25, 25)
if lower_left[2] != -np.inf and upper_right[2] != np.inf:
zcenter = (upper_right[2] + lower_left[2])/2
else:
zcenter = 0.00
default = PlotView([xcenter, ycenter, zcenter], width, height)
return default
def resetColors(self):
""" Reset colors to those generated in the default view """
self.activeView.cells = self.defaultView.cells
self.activeView.materials = self.defaultView.materials
def generatePlot(self):
""" Spawn thread from which to generate new plot image """
t = threading.Thread(target=self.makePlot)
t.start()
t.join()
def makePlot(self):
""" Generate new plot image from active view settings
Creates corresponding .xml files from user-chosen settings.
Runs OpenMC in plot mode to generate new plot image.
"""
# update/call maps under 2 circumstances
# 1. this is the intial plot (ids_map/properties are None)
# 2. The active (desired) view differs from the current view parameters
if (self.currentView.view_params != self.activeView.view_params) or \
(self.ids_map is None) or (self.properties is None):
# get ids from the active (desired) view
self.ids_map = openmc.lib.id_map(self.activeView.view_params)
self.properties = openmc.lib.property_map(self.activeView.view_params)
# update current view
cv = self.currentView = copy.deepcopy(self.activeView)
# set model ids based on domain
if cv.colorby == 'cell':
self.ids = self.cell_ids
domain = cv.cells
source = self.modelCells
else:
self.ids = self.mat_ids
domain = cv.materials
source = self.modelMaterials
# generate colors if not present
for cell_id, cell in cv.cells.items():
if cell.color is None:
cell.color = random_rgb()
for mat_id, mat in cv.materials.items():
if mat.color is None:
mat.color = random_rgb()
# construct image data
domain[_OVERLAP] = DomainView(_OVERLAP, "Overlap", cv.overlap_color)
domain[_NOT_FOUND] = DomainView(_NOT_FOUND, "Not Found", cv.domainBackground)
u, inv = np.unique(self.ids, return_inverse=True)
image = np.array([domain[id].color for id in u])[inv]
image.shape = (cv.v_res, cv.h_res, 3)
if cv.masking:
for id, dom in domain.items():
if dom.masked:
image[self.ids == int(id)] = cv.maskBackground
if cv.highlighting:
for id, dom in domain.items():
if dom.highlight:
image[self.ids == int(id)] = cv.highlightBackground
# set model image
self.image = image
# tally data
self.tally_data = None
self.properties[self.properties < 0.0] = np.nan
self.temperatures = self.properties[..., _PROPERTY_INDICES['temperature']]
self.densities = self.properties[..., _PROPERTY_INDICES['density']]
minmax = {}
for prop in _MODEL_PROPERTIES:
idx = _PROPERTY_INDICES[prop]
prop_data = self.properties[:, :, idx]
minmax[prop] = (np.min(np.nan_to_num(prop_data)),
np.max(np.nan_to_num(prop_data)))
self.activeView.data_minmax = minmax
def undo(self):
""" Revert to previous PlotView instance. Re-generate plot image """
if self.previousViews:
self.subsequentViews.append(copy.deepcopy(self.currentView))
self.activeView = self.previousViews.pop()
self.generatePlot()
def redo(self):
""" Revert to subsequent PlotView instance. Re-generate plot image """
if self.subsequentViews:
self.storeCurrent()
self.activeView = self.subsequentViews.pop()
self.generatePlot()
def storeCurrent(self):
""" Add current view to previousViews list """
self.previousViews.append(copy.deepcopy(self.currentView))
def create_tally_image(self, view=None):
"""
Parameters
----------
view :
View used to set bounds of the tally data
Returns
-------
tuple
image data (numpy.ndarray), data extents (optional),
data_min_value (float), data_max_value (float),
data label (str)
"""
if view is None:
view = self.currentView
tally_id = view.selectedTally
scores = self.appliedScores
nuclides = self.appliedNuclides
tally_selected = view.selectedTally is not None
tally_visible = view.tallyDataVisible
visible_selection = scores and nuclides
if not tally_selected or not tally_visible or not visible_selection:
return (None, None, None, None, None)
tally = self.statepoint.tallies[tally_id]
tally_value = _TALLY_VALUES[view.tallyValue]
# check score units
units = {_SCORE_UNITS.get(score, _REACTION_UNITS) for score in scores}
if len(units) != 1:
msg_box = QMessageBox()
unit_str = " ".join(units)
msg = "The scores selected have incompatible units:\n"
for unit in units:
msg += " - {}\n".format(unit)
msg_box.setText(msg)
msg_box.setIcon(QMessageBox.Information)
msg_box.setStandardButtons(QMessageBox.Ok)
msg_box.exec_()
return (None, None, None, None, None)
units_out = list(units)[0]
contains_distribcell = tally.contains_filter(openmc.DistribcellFilter)
contains_cellinstance = tally.contains_filter(openmc.CellInstanceFilter)
if tally.contains_filter(openmc.MeshFilter):
if tally_value == 'rel_err':
# get both the std. dev. data and mean data
# to create the relative error data
mean_data = self._create_tally_mesh_image(tally,
'mean',
scores,
nuclides,
view)
std_dev_data = self._create_tally_mesh_image(tally,
'std_dev',
scores,
nuclides,
view)
image_data = 100 * np.divide(std_dev_data[0],
mean_data[0],
out=np.zeros_like(mean_data[0]),
where=mean_data != 0)
extents = mean_data[1]
data_min = np.min(image_data)
data_max = np.max(image_data)
return image_data, extents, data_min, data_max, '% error'
else:
image = self._create_tally_mesh_image(tally,
tally_value,
scores,
nuclides,
view)
return image + (units_out,)
elif contains_distribcell or contains_cellinstance:
if tally_value == 'rel_err':
mean_data = self._create_distribcell_image(
tally, 'mean', scores, nuclides, contains_cellinstance)
std_dev_data = self._create_distribcell_image(
tally, 'std_dev', scores, nuclides)
image_data = 100 * np.divide(
std_dev_data[0], mean_data[0],
out=np.zeros_like(mean_data[0]),
where=mean_data != 0
)
data_min = np.min(image_data)
data_max = np.max(image_data)
return image_data, None, data_min, data_max, '% error'
else:
image = self._create_distribcell_image(
tally, tally_value, scores, nuclides, contains_cellinstance)
return image + (units_out,)
else:
# same as above, get the std. dev. data
# and mean date to produce the relative error data
if tally_value == 'rel_err':
mean_data = self._create_tally_domain_image(tally,
'mean',
scores,
nuclides)
std_dev_data = self._create_tally_domain_image(tally,
'std_dev',
scores,
nuclides)
image_data = 100 * np.divide(std_dev_data[0],
mean_data[0],
out=np.zeros_like(mean_data[0]),
where=mean_data != 0)
# adjust for NaNs in bins without tallies
image_data = np.nan_to_num(image_data,
nan=0.0,
posinf=0.0,
neginf=0.0)
extents = mean_data[1]
data_min = np.min(image_data)
data_max = np.max(image_data)
return image_data, extents, data_min, data_max, '% error'
else:
image = self._create_tally_domain_image(tally,
tally_value,
scores,
nuclides)
return image + (units_out,)
def _create_tally_domain_image(self, tally, tally_value, scores, nuclides, view=None):
# data resources used throughout
if view is None:
view = self.currentView
data = tally.get_reshaped_data(tally_value)
data_out = np.full(self.ids.shape, -1.0)
def _do_op(array, tally_value, ax=0):
if tally_value == 'mean':
return np.sum(array, axis=ax)
elif tally_value == 'std_dev':
return np.sqrt(np.sum(array**2, axis=ax))
# data structure for tracking which spatial
# filter bins are enabled
spatial_filter_bins = defaultdict(list)
n_spatial_filters = 0
for tally_filter in tally.filters:
if tally_filter in self.appliedFilters:
selected_bins = self.appliedFilters[tally_filter]
if type(tally_filter) in _SPATIAL_FILTERS:
spatial_filter_bins[tally_filter] = selected_bins
n_spatial_filters += 1
else:
slc = [slice(None)] * len(data.shape)
slc[n_spatial_filters] = selected_bins
slc = tuple(slc)
data = _do_op(data[slc], tally_value, n_spatial_filters)
else:
data[:, ...] = 0.0
data = _do_op(data, tally_value, n_spatial_filters)
# filter by selected scores
selected_scores = []
for idx, score in enumerate(tally.scores):
if score in scores:
selected_scores.append(idx)
data = _do_op(data[..., np.array(selected_scores)], tally_value, -1)
# filter by selected nuclides
selected_nuclides = []
for idx, nuclide in enumerate(tally.nuclides):
if nuclide in nuclides:
selected_nuclides.append(idx)
data = _do_op(data[..., np.array(selected_nuclides)], tally_value, -1)
# get data limits
data_min = np.min(data)
data_max = np.max(data)
# for all combinations of spatial bins, create a mask
# and set image data values
spatial_filters = list(spatial_filter_bins.keys())
spatial_bins = list(spatial_filter_bins.values())
for bin_indices in itertools.product(*spatial_bins):
# look up the tally value
tally_val = data[bin_indices]
if tally_val == 0.0:
continue
# generate a mask with the correct size
mask = np.full(self.ids.shape, True, dtype=bool)
for tally_filter, bin_idx in zip(spatial_filters, bin_indices):
bin = tally_filter.bins[bin_idx]
if isinstance(tally_filter, openmc.CellFilter):
mask &= self.cell_ids == bin
elif isinstance(tally_filter, openmc.MaterialFilter):
mask &= self.mat_ids == bin
elif isinstance(tally_filter, openmc.UniverseFilter):
# get the statepoint summary
univ_cells = self.statepoint.universes[bin].cells
for cell in univ_cells:
mask &= self.cell_ids == cell
# set image data values
data_out[mask] = tally_val
# mask out invalid values
image_data = np.ma.masked_where(data_out < 0.0, data_out)
return image_data, None, data_min, data_max
def _create_distribcell_image(self, tally, tally_value, scores, nuclides, cellinstance=False):
# Get flattened array of tally results
data = tally.get_values(scores=scores, nuclides=nuclides, value=tally_value)
data = data.flatten()
# Create an empty array of appropriate shape for image
image_data = np.full_like(self.ids, np.nan, dtype=float)
# Determine mapping of cell IDs to list of (instance, tally value).
if cellinstance:
f = tally.find_filter(openmc.CellInstanceFilter)
cell_id_to_inst_value = defaultdict(list)
for value, (cell_id, instance) in zip(data, f.bins):
cell_id_to_inst_value[cell_id].append((instance, value))
else:
f = tally.find_filter(openmc.DistribcellFilter)
cell_id_to_inst_value = {f.bins[0]: list(enumerate(data))}
for cell_id, value_list in cell_id_to_inst_value.items():
# Get mask for each relevant cell
cell_id_mask = (self.cell_ids == cell_id)
# For each cell, iterate over instances and corresponding tally
# values and set any matching pixels
for instance, value in value_list:
instance_mask = (self.instances == instance)
image_data[cell_id_mask & instance_mask] = value
data_min = np.min(data)
data_max = np.max(data)
image_data = np.ma.masked_where(image_data < 0.0, image_data)
return image_data, None, data_min, data_max
def _create_tally_mesh_image(self, tally, tally_value, scores, nuclides, view=None):
# some variables used throughout
if view is None:
view = self.currentView
sp = self.statepoint
mesh_filter = tally.find_filter(openmc.MeshFilter)
mesh = mesh_filter.mesh
def _do_op(array, tally_value, ax=0):
if tally_value == 'mean':
return np.sum(array, axis=ax)
elif tally_value == 'std_dev':
return np.sqrt(np.sum(array**2, axis=ax))
# start with reshaped data
data = tally.get_reshaped_data(tally_value)
# determine basis indices
if view.basis == 'xy':
h_ind = 0
v_ind = 1
ax = 2
elif view.basis == 'yz':
h_ind = 1
v_ind = 2
ax = 0
else:
h_ind = 0
v_ind = 2
ax = 1
# adjust corners of the mesh for a translation
# applied to the mesh filter
lower_left = mesh.lower_left
upper_right = mesh.upper_right
width = mesh.width
dimension = mesh.dimension
if hasattr(mesh_filter, 'translation') and mesh_filter.translation is not None:
lower_left += mesh_filter.translation
upper_right += mesh_filter.translation
# For 2D meshes, add an extra z dimension
if len(mesh.dimension) == 2:
lower_left = np.hstack((lower_left, -1e50))
upper_right = np.hstack((upper_right, 1e50))
width = np.hstack((width, 2e50))
dimension = np.hstack((dimension, 1))
# reduce data to the visible slice of the mesh values
k = int((view.origin[ax] - lower_left[ax]) // width[ax])
# setup slice
data_slice = [None, None, None]
data_slice[h_ind] = slice(dimension[h_ind])
data_slice[v_ind] = slice(dimension[v_ind])
data_slice[ax] = k
if k < 0 or k > dimension[ax]:
return (None, None, None, None)
# move mesh axes to the end of the filters
filter_idx = [type(filter) for filter in tally.filters].index(openmc.MeshFilter)
data = np.moveaxis(data, filter_idx, -1)
# reshape data (with zyx ordering for mesh data)
data = data.reshape(data.shape[:-1] + tuple(dimension[::-1]))
data = data[..., data_slice[2], data_slice[1], data_slice[0]]
# sum over the rest of the tally filters
for tally_filter in tally.filters:
if type(tally_filter) == openmc.MeshFilter:
continue
selected_bins = self.appliedFilters[tally_filter]
if selected_bins:
# sum filter data for the selected bins
data = data[np.array(selected_bins)].sum(axis=0)
else:
# if the filter is completely unselected,
# set all of it's data to zero and remove the axis
data[:, ...] = 0.0
data = _do_op(data, tally_value)
# filter by selected nuclides
if not nuclides:
data = 0.0
selected_nuclides = []
for idx, nuclide in enumerate(tally.nuclides):
if nuclide in nuclides:
selected_nuclides.append(idx)
data = _do_op(data[np.array(selected_nuclides)], tally_value)
# filter by selected scores
if not scores:
data = 0.0
selected_scores = []
for idx, score in enumerate(tally.scores):
if score in scores:
selected_scores.append(idx)
data = _do_op(data[np.array(selected_scores)], tally_value)
# get dataset's min/max
data_min = np.min(data)
data_max = np.max(data)
# set image data, reverse y-axis
image_data = data[::-1, ...]
# return data extents (in cm) for the tally
extents = [lower_left[h_ind], upper_right[h_ind],
lower_left[v_ind], upper_right[v_ind]]
return image_data, extents, data_min, data_max
@property
def cell_ids(self):
return self.ids_map[:, :, 0]
@property
def instances(self):
return self.ids_map[:, :, 1]
@property
def mat_ids(self):
return self.ids_map[:, :, 2]
class ViewParam(openmc.lib.plot._PlotBase):
"""Viewer settings that are needed for _PlotBase and are independent
of all other plotter/model settings.
Parameters
----------
origin : 3-tuple of floats
Origin (center) of plot view
width: float
Width of plot view in model units
height : float
Height of plot view in model units
Attributes
----------
origin : 3-tuple of floats
Origin (center) of plot view
width : float
Width of the plot view in model units
height : float
Height of the plot view in model units
h_res : int
Horizontal resolution of plot image
v_res : int
Vertical resolution of plot image
basis : {'xy', 'xz', 'yz'}
The basis directions for the plot
color_overlaps : bool
Indicator of whether or not overlaps will be shown
level : int
The universe level for the plot (default: -1 -> all universes shown)
"""
def __init__(self, origin=(0, 0, 0), width=10, height=10):
"""Initialize ViewParam attributes"""
super().__init__()
# View Parameters
self.level = -1
self.origin = origin
self.width = width
self.height = height
self.h_res = 1000
self.v_res = 1000
self.basis = 'xy'
self.color_overlaps = False
def __eq__(self, other):
return repr(self) == repr(other)
class PlotViewIndependent:
"""View settings for OpenMC plot, independent of the model.
Attributes
----------
aspectLock : bool
Indication of whether aspect lock should be maintained to
prevent image stretching/warping
colorby : {'cell', 'material', 'temperature', 'density'}
Indication of whether the plot should be colored by cell or material
masking : bool
Indication of whether cell/material masking is active
maskBackground : 3-tuple of int
RGB color to apply to masked cells/materials
highlighting: bool
Indication of whether cell/material highlighting is active
highlightBackground : 3-tuple of int
RGB color to apply to non-highlighted cells/materials
highlightAlpha : float between 0 and 1
Alpha value for highlight background color
highlightSeed : int
Random number seed used to generate color scheme when highlighting
is active
domainBackground : 3-tuple of int
RGB color to apply to plot background
overlap_color : 3-tuple of int
RGB color to apply for cell overlap regions
domainAlpha : float between 0 and 1
Alpha value of the geometry plot
plotVisibile : bool
Controls visibility of geometry
outlines: bool
Controls visibility of geometry outlines
tallyDataColormap : str
Name of the colormap used for tally data
tallyDataVisible : bool
Indicator for whether or not the tally data is visible
tallyDataAlpha : float
Value of the tally image alpha
tallyDataIndicator : bool
Indicates whether or not the data indicator is active on the tally colorbar
tallyDataMin : float
Minimum scale value for tally data
tallyDataMax : float
Minimum scale value for tally data
tallyDataLogScale : bool
Indicator of logarithmic scale for tally data
tallyMaskZeroValues : bool
Indicates whether or not zero values in tally data should be masked
clipTallyData: bool
Indicates whether or not tally data is clipped by the colorbar min/max
tallyValue : str
Indicator for what type of value is displayed in plots.
tallyContours : bool
Indicates whether or not tallies are displayed as contours
tallyContourLevels : str
Number of contours levels or explicit level values
"""
def __init__(self):
"""Initialize PlotViewIndependent attributes"""
# Geometry Plot
self.aspectLock = True
self.colorby = 'material'
self.masking = True
self.maskBackground = (0, 0, 0)
self.highlighting = False
self.highlightBackground = (80, 80, 80)
self.highlightAlpha = 0.5
self.highlightSeed = 1
self.domainBackground = (50, 50, 50)
self.overlap_color = (255, 0, 0)
self.domainAlpha = 1.0
self.domainVisible = True
self.outlines = False
self.colormaps = {'temperature': 'Oranges', 'density': 'Greys'}
# set defaults for color dialog
self.data_minmax = {prop: (0.0, 0.0) for prop in _MODEL_PROPERTIES}
self.user_minmax = {prop: (0.0, 0.0) for prop in _MODEL_PROPERTIES}
self.use_custom_minmax = {prop: False for prop in _MODEL_PROPERTIES}
self.data_indicator_enabled = {prop: False for prop in _MODEL_PROPERTIES}
self.color_scale_log = {prop: False for prop in _MODEL_PROPERTIES}
# Tally Viz Settings
self.tallyDataColormap = 'Spectral'
self.tallyDataVisible = True
self.tallyDataAlpha = 1.0
self.tallyDataIndicator = False
self.tallyDataUserMinMax = False
self.tallyDataMin = 0.0
self.tallyDataMax = np.inf
self.tallyDataLogScale = False
self.tallyMaskZeroValues = False
self.clipTallyData = False
self.tallyValue = "Mean"
self.tallyContours = False
self.tallyContourLevels = ""
def getDataLimits(self):
return self.data_minmax
def getColorLimits(self, property):
if self.use_custom_minmax[property]:
return self.user_minmax[property]
else:
return self.data_minmax[property]
@property
def llc(self):
if self.basis == 'xy':
x = self.origin[0] - self.width / 2.0
y = self.origin[1] - self.height / 2.0
z = self.origin[2]
elif self.basis == 'yz':
x = self.origin[0]
y = self.origin[1] - self.width / 2.0
z = self.origin[2] - self.height / 2.0
else:
x = self.origin[0] - self.width / 2.0
y = self.origin[1]
z = self.origin[2] - self.height / 2.0
return x, y, z
@property
def urc(self):
if self.basis == 'xy':
x = self.origin[0] + self.width / 2.0
y = self.origin[1] + self.height / 2.0
z = self.origin[2]
elif self.basis == 'yz':
x = self.origin[0]
y = self.origin[1] + self.width / 2.0
z = self.origin[2] + self.height / 2.0
else:
x = self.origin[0] + self.width / 2.0
y = self.origin[1]
z = self.origin[2] + self.height / 2.0
return x, y, z
class PlotView:
"""Setup the view of the model.
Parameters
----------
origin : 3-tuple of floats
Origin (center) of plot view
width : float
Width of plot view in model units
height : float
Height of plot view in model units
restore_view : PlotView or None
view object with specified parameters to restore
restore_domains : bool (optional)
If True and restore_view is provided, then also restore domain
properties. Default False.
Attributes
----------
view_ind : PlotViewIndependent instance
viewing parameters that are independent of the model
view_params : ViewParam instance
view parameters necesary for _PlotBase
cells : Dict of DomainView instances
Dictionary of cell view settings by ID
materials : Dict of DomainView instances
Dictionary of material view settings by ID
selectedTally : str
Label of the currently selected tally
"""
attrs = ('view_ind', 'view_params', 'cells', 'materials', 'selectedTally')
plotbase_attrs = ('level', 'origin', 'width', 'height',
'h_res', 'v_res', 'basis', 'color_overlaps')
def __init__(self, origin=(0, 0, 0), width=10, height=10, restore_view=None,
restore_domains=False):
"""Initialize PlotView attributes"""
if restore_view is not None:
self.view_ind = copy.copy(restore_view.view_ind)
self.view_params = copy.copy(restore_view.view_params)
else:
self.view_ind = PlotViewIndependent()
self.view_params = ViewParam(origin=origin, width=width, height=height)
# Get model domain info
if restore_domains and restore_view is not None:
self.cells = restore_view.cells
self.materials = restore_view.materials
self.selectedTally = restore_view.selectedTally
else:
self.cells = self.getDomains('cell')
self.materials = self.getDomains('material')
self.selectedTally = None
def __getattr__(self, name):
if name in self.attrs:
if name not in self.__dict__:
raise AttributeError('{} not in PlotView dict'.format(name))
return self.__dict__[name]
elif name in self.plotbase_attrs:
return getattr(self.view_params, name)
else: