Skip to content

Commit

Permalink
for #7332 move outline frames to server
Browse files Browse the repository at this point in the history
  • Loading branch information
moellep committed Oct 28, 2024
1 parent ddc54e2 commit 361361f
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 93 deletions.
191 changes: 101 additions & 90 deletions sirepo/package_data/static/js/openmc.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ SIREPO.app.factory('openmcService', function(appState, panelState, requestSender
self.canNormalizeScore = score => ! SIREPO.APP_SCHEMA.constants.unnormalizableScores.includes(score);

self.computeModel = (modelKey) => {
if (modelKey === "energyAnimation") {
if (['energyAnimation', 'outlineAnimation'].includes(modelKey)) {
return "openmcAnimation";
}
return modelKey;
Expand Down Expand Up @@ -532,7 +532,6 @@ SIREPO.app.factory('tallyService', function(appState, openmcService, utilities,
fieldData: null,
minField: 0,
maxField: 0,
outlines: null,
sourceParticles: [],
};

Expand All @@ -558,7 +557,6 @@ SIREPO.app.factory('tallyService', function(appState, openmcService, utilities,
self.clearMesh = () => {
self.mesh = null;
self.fieldData = null;
self.outlines = null;
};

self.colorScale = modelName => {
Expand Down Expand Up @@ -598,20 +596,6 @@ SIREPO.app.factory('tallyService', function(appState, openmcService, utilities,
]);
};

self.getOutlines = (volId, dim, index) => {
if (! self.outlines) {
return [];
}
const t = self.outlines[appState.applicationState().openmcAnimation.tally];
if (t && t[`${volId}`]) {
const o = t[`${volId}`][dim];
if (o.length) {
return o[index];
}
}
return [];
};

self.getSourceParticles = () => self.sourceParticles;

self.setFieldData = (fieldData, min, max, numParticles) => {
Expand All @@ -631,18 +615,11 @@ SIREPO.app.factory('tallyService', function(appState, openmcService, utilities,
f.stop,
];
}
appState.models.tallyReport.planePos = 0;
//TODO(pjm): planePos will become an integer with -1 out of range
appState.models.tallyReport.planePos = -1;
initMesh();
};

self.setOutlines = (tally, outlines) => {
if (appState.applicationState().openmcAnimation.tally === tally) {
self.outlines = {
[tally]: outlines,
};
}
};

self.setSourceParticles = particles => {
self.sourceParticles = particles;
};
Expand Down Expand Up @@ -930,7 +907,6 @@ SIREPO.app.directive('tallyViewer', function(appState, openmcService, plotting,
return;
}
tallyService.setFieldData(json.field_data, json.min_field, json.max_field, json.num_particles);
tallyService.setOutlines(json.summaryData.tally, json.summaryData.outlines || {});
tallyService.setSourceParticles(json.summaryData.sourceParticles || []);
};

Expand All @@ -947,7 +923,7 @@ SIREPO.app.directive('tallyViewer', function(appState, openmcService, plotting,
};
});

SIREPO.app.directive('geometry2d', function(appState, openmcService, panelState, tallyService) {
SIREPO.app.directive('geometry2d', function(appState, frameCache, openmcService, panelState, tallyService) {
return {
restrict: 'A',
scope: {
Expand All @@ -959,7 +935,8 @@ SIREPO.app.directive('geometry2d', function(appState, openmcService, panelState,
controller: function($scope) {
$scope.tallyService = tallyService;
const displayRanges = {};
let lastTally = [null, null];
let geometryOutlines;
let lastTally = [null, null, null];
const sources = openmcService.getSourceVisualizations(
{
box: space => {
Expand Down Expand Up @@ -995,9 +972,64 @@ SIREPO.app.directive('geometry2d', function(appState, openmcService, panelState,
}

function buildTallyReport() {
if (! tallyService.fieldData) {
if (! tallyService.fieldData || appState.models.tallyReport.planePos === -1) {
return;
}
if (
(lastTally[0] != appState.models.openmcAnimation.tally)
|| (lastTally[1] != appState.models.tallyReport.axis)
|| (lastTally[2] != appState.models.tallyReport.planePos)
) {
const newTally = [
appState.models.openmcAnimation.tally,
appState.models.tallyReport.axis,
appState.models.tallyReport.planePos,
];
lastTally = newTally;
const o = appState.models.outlineAnimation;
o.tally = appState.models.openmcAnimation.tally;
o.axis = appState.models.tallyReport.axis;
appState.saveQuietly('outlineAnimation');

//TODO(pjm): refactor this
const [n, m, l] = tallyReportAxisIndices();
const dimIndex = n;
const ranges = tallyService.getMeshRanges();
const range = ranges[n];
const pos = appState.models.tallyReport.planePos;

frameCache.getFrame('outlineAnimation', fieldIndex(pos, range, dimIndex), false, function(index, data) {
if (! data.outlines) {
return;
}
const outlines = [];
for (const volId of openmcService.getNonGraveyardVolumes()) {
const v = openmcService.getVolumeById(volId);
if (! v.isVisibleWithTallies) {
continue;
}
const o = data.outlines[volId];
if (o) {
o.forEach((arr, i) => {
outlines.push({
name: `${v.name}-${i}`,
color: v.color,
data: arr,
});
});
}
}
geometryOutlines = outlines;
buildTallyReportsWithOutlines();
});
return;
}
if (geometryOutlines) {
buildTallyReportsWithOutlines();
}
}

function buildTallyReportsWithOutlines() {
const [z, x, y] = tallyReportAxes();
const [n, m, l] = tallyReportAxisIndices();
const ranges = tallyService.getMeshRanges();
Expand Down Expand Up @@ -1025,20 +1057,11 @@ SIREPO.app.directive('geometry2d', function(appState, openmcService, panelState,
y_range: ranges[m],
z_matrix: sliceFieldData(),
z_range: ranges[n],
overlayData: getOutlines(pos, ranges[n], n),
overlayData: geometryOutlines.concat(getSourceOutlines()),
selectedCoords: $scope.energyFilter ? tallyService.getEnergyReportCoords() : null,
};
panelState.setData('tallyReport', r);
$scope.$broadcast('tallyReport.reload', r);

if (
(lastTally[0] != appState.models.openmcAnimation.tally)
|| (lastTally[1] != appState.models.tallyReport.planePos)
) {
const newTally = [appState.models.openmcAnimation.tally, appState.models.tallyReport.planePos];
//console.log('HERE request outlines', lastTally, newTally);
lastTally = newTally;
}
}

function energySumLabel() {
Expand All @@ -1054,8 +1077,12 @@ SIREPO.app.directive('geometry2d', function(appState, openmcService, panelState,
);
}

function getOutlines(pos, range, dimIndex) {

function getSourceOutlines() {
//TODO(pjm): rework this method
const [n, m, l] = tallyReportAxisIndices();
const dimIndex = n;
const dim = SIREPO.GEOMETRY.GeometryUtils.BASIS()[dimIndex];
const outlines = [];
const particleColors = SIREPO.UTILS.unique(
tallyService.getSourceParticles().map(p => particleColor(p))
);
Expand All @@ -1065,6 +1092,21 @@ SIREPO.app.directive('geometry2d', function(appState, openmcService, panelState,
return pos[j] < r[j][0] || pos[j] > r[j][1] || pos[k] < r[k][0] || pos[k] > r[k][1];
}

// we cannot set the color of an instance of a marker ref, so we will
// have to create them on the fly
function placeMarkers() {
const ns = 'http://www.w3.org/2000/svg';
let ds = d3.select('svg.sr-plot defs')
.selectAll('marker')
.data(particleColors);
ds.exit().remove();
ds.enter()
.append(d => document.createElementNS(ns, 'marker'))
.append('path')
.attr('d', 'M0,0 L0,4 L9,2 z');
ds.call(updateMarkers);
}

function particleColor(p) {
return tallyService.sourceParticleColorScale(
appState.models.openmcAnimation.sourceColorMap
Expand All @@ -1079,19 +1121,14 @@ SIREPO.app.directive('geometry2d', function(appState, openmcService, panelState,
return `arrow-${c.slice(1)}`;
}

// we cannot set the color of an instance of a marker ref, so we will
// have to create them on the fly
function placeMarkers() {
const ns = 'http://www.w3.org/2000/svg';
let ds = d3.select('svg.sr-plot defs')
.selectAll('marker')
.data(particleColors);
ds.exit().remove();
ds.enter()
.append(d => document.createElementNS(ns, 'marker'))
.append('path')
.attr('d', 'M0,0 L0,4 L9,2 z');
ds.call(updateMarkers);
function toReversed(arr) {
// Array.toReversed() is not available in all active browsers
// not using a polyfill due to array iteration bugs
const r = [];
for (let i = (arr.length - 1); i >= 0; --i) {
r.push(arr[i]);
}
return r;
}

function updateMarkers(selection) {
Expand All @@ -1107,32 +1144,6 @@ SIREPO.app.directive('geometry2d', function(appState, openmcService, panelState,
.attr('fill', d => sourceColor(d));
}

function toReversed(arr) {
// Array.toReversed() is not available in all active browsers
// not using a polyfill due to array iteration bugs
const r = [];
for (let i = (arr.length - 1); i >= 0; --i) {
r.push(arr[i]);
}
return r;
}

const outlines = [];
const dim = SIREPO.GEOMETRY.GeometryUtils.BASIS()[dimIndex];
for (const volId of openmcService.getNonGraveyardVolumes()) {
const v = openmcService.getVolumeById(volId);
if (! v.isVisibleWithTallies) {
continue;
}
const o = tallyService.getOutlines(volId, dim, fieldIndex(pos, range, dimIndex));
o.forEach((arr, i) => {
outlines.push({
name: `${v.name}-${i}`,
color: v.color,
data: arr,
});
});
}
sources.forEach((view, i) => {
const s = appState.models.settings.sources[i];
if (view instanceof SIREPO.VTK.SphereViews) {
Expand Down Expand Up @@ -1746,9 +1757,7 @@ SIREPO.app.directive('geometry3d', function(appState, openmcService, plotting, p

$scope.init = () => {};

$scope.resize = () => {
//TODO(pjm): reposition camera?
};
$scope.resize = () => {};

$scope.sizeStyle = () => {
if (hasTallies) {
Expand Down Expand Up @@ -3308,10 +3317,11 @@ SIREPO.app.directive('planePositionSlider', function(appState, panelState, tally
function updateRange() {
if ($scope.dim) {
newRange = tallyService.tallyRange($scope.dim, true);
newRange.axis = appState.models.tallyReport.axis;
newRange.axis = $scope.dim;
}
}

//TODO(pjm): update the slider component so the range and axis is dynamic
$scope.hasRange = () => {
// the slider component has fixed range, so the component needs to get recreated
// when the range or axis changes
Expand All @@ -3320,16 +3330,17 @@ SIREPO.app.directive('planePositionSlider', function(appState, panelState, tally
&& $scope.range.max === newRange.max
&& $scope.range.steps === newRange.steps
&& $scope.range.axis === oldAxis
&& newRange.axis === oldAxis
) {
return true;
}
if (newRange.steps > 1) {
$scope.range = newRange;
if (! appState.models.tallyReport.planePos
|| $scope.range.axis !== oldAxis) {
appState.models.tallyReport.planePos = $scope.range.min;
oldAxis = $scope.range.axis;
}
}
if (appState.models.tallyReport.planePos === -1
|| newRange.axis !== oldAxis) {
appState.models.tallyReport.planePos = newRange.min;
oldAxis = newRange.axis;
}
}
return false;
Expand Down
8 changes: 8 additions & 0 deletions sirepo/package_data/static/json/openmc-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@
"x",
"y",
"z"
],
"outlineAnimation": [
"tally",
"axis"
]
},
"localRoutes": {
Expand Down Expand Up @@ -973,6 +977,10 @@
"ompThreads": ["OMP threads", "Integer", 16, "", 1],
"selectedVolumes": ["", "SelectedTallyVolumes", ""]
},
"outlineAnimation": {
"tally": ["", "PlotTallyList"],
"axis": ["", "Axis", "y"]
},
"particle": {
"value": ["Particle", "FilterParticle"]
},
Expand Down
3 changes: 2 additions & 1 deletion sirepo/sim_data/openmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def _fix_val(model, field):
"geometry3DReport",
"geometryInput",
"openmcAnimation",
"outlineAnimation",
"reflectivePlanes",
"settings",
"tallyReport",
Expand Down Expand Up @@ -134,7 +135,7 @@ def _compute_job_fields(cls, data, *args, **kwargs):
def _compute_model(cls, analysis_model, *args, **kwargs):
if analysis_model == "geometry3DReport":
return "dagmcAnimation"
if analysis_model == "energyAnimation":
if analysis_model in ("energyAnimation", "outlineAnimation"):
return "openmcAnimation"
return analysis_model

Expand Down
13 changes: 11 additions & 2 deletions sirepo/template/openmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ def sim_frame(frame_args):
return _energy_plot(
frame_args.run_dir, frame_args.sim_in, frame_args.frameIndex
)
if frame_args.frameReport == "outlineAnimation":
res = PKDict()
o = simulation_db.read_json(frame_args.run_dir.join(_OUTLINES_FILE))
if frame_args.tally in o:
for v, d in o[frame_args.tally].items():
if frame_args.axis in d:
if frame_args.frameIndex < len(d[frame_args.axis]):
res[v] = d[frame_args.axis][frame_args.frameIndex]
return PKDict(
outlines=res,
)

def _sample_sources(filename, num_samples):
samples = []
Expand Down Expand Up @@ -183,15 +194,13 @@ def _tally_index(frame_args):

# volume normalize copied from openmc.UnstructuredMesh.write_data_to_vtk()
v /= t.find_filter(openmc.MeshFilter).mesh.volumes.ravel()
o = simulation_db.read_json(frame_args.run_dir.join(_OUTLINES_FILE))
return PKDict(
field_data=v.tolist(),
min_field=v.min(),
max_field=v.max(),
num_particles=frame_args.sim_in.models.settings.particles,
summaryData=PKDict(
tally=frame_args.tally,
outlines=o[frame_args.tally] if frame_args.tally in o else {},
sourceParticles=_sample_sources(
_source_filename(frame_args.sim_in),
frame_args.numSampleSourceParticles,
Expand Down

0 comments on commit 361361f

Please sign in to comment.