Skip to content

Commit e300416

Browse files
authored
Simplify plotSolution function (#57)
1 parent 2473a71 commit e300416

File tree

1 file changed

+31
-83
lines changed

1 file changed

+31
-83
lines changed

src/visualization/plotSolutionScript.js

Lines changed: 31 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414
* @param {string} meshDimension - The dimension of the solution
1515
* @param {string} plotType - The type of plot
1616
* @param {string} plotDivId - The id of the div where the plot will be rendered
17-
* @param {string} [meshType="structured"] - Type of mesh: "structured" or "unstructured"
1817
*/
1918
export function plotSolution(
2019
solutionVector,
2120
nodesCoordinates,
2221
solverConfig,
2322
meshDimension,
2423
plotType,
25-
plotDivId,
26-
meshType = "structured"
24+
plotDivId
2725
) {
2826
const { nodesXCoordinates, nodesYCoordinates } = nodesCoordinates;
2927

@@ -38,7 +36,7 @@ export function plotSolution(
3836
let xData = Array.from(nodesXCoordinates);
3937

4038
let lineData = {
41-
x: xData,
39+
x: nodesXCoordinates,
4240
y: yData,
4341
mode: "lines",
4442
type: "scatter",
@@ -47,9 +45,7 @@ export function plotSolution(
4745
};
4846

4947
let maxWindowWidth = Math.min(window.innerWidth, 700);
50-
let maxPlotWidth = Math.max(...xData);
51-
let zoomFactor = maxWindowWidth / maxPlotWidth;
52-
let plotWidth = Math.max(zoomFactor * maxPlotWidth, 400);
48+
let plotWidth = Math.min(maxWindowWidth, 600);
5349
let plotHeight = 350;
5450

5551
let layout = {
@@ -58,35 +54,28 @@ export function plotSolution(
5854
height: plotHeight,
5955
xaxis: { title: "x" },
6056
yaxis: { title: "Solution" },
61-
margin: { l: 70, r: 40, t: 50, b: 50 },
57+
margin: { l: 50, r: 50, t: 50, b: 50 },
6258
};
6359

6460
Plotly.newPlot(plotDivId, [lineData], layout, { responsive: true });
6561
} else if (meshDimension === "2D" && plotType === "contour") {
66-
// Use the user-provided mesh type
67-
const isStructured = meshType === "structured";
68-
69-
// For auto-detection (if needed)
70-
const uniqueXCoords = new Set(nodesXCoordinates).size;
71-
const uniqueYCoords = new Set(nodesYCoordinates).size;
72-
73-
// Extract scalar values from solution vector
74-
let zValues;
62+
// Check if solutionVector is a nested array
63+
let zData;
7564
if (Array.isArray(solutionVector[0])) {
76-
zValues = solutionVector.map((val) => val[0]);
65+
zData = solutionVector.map((val) => val[0]);
7766
} else {
78-
zValues = solutionVector;
67+
zData = solutionVector;
7968
}
8069

81-
// Common sizing parameters for both plot types
70+
// Sizing parameters
8271
let maxWindowWidth = Math.min(window.innerWidth, 700);
8372
let maxX = Math.max(...nodesXCoordinates);
8473
let maxY = Math.max(...nodesYCoordinates);
8574
let aspectRatio = maxY / maxX;
8675
let plotWidth = Math.min(maxWindowWidth, 600);
87-
let plotHeight = plotWidth * aspectRatio * 0.8; // Slightly reduce height for better appearance
76+
let plotHeight = plotWidth * aspectRatio;
8877

89-
// Common layout properties
78+
// Layout properties
9079
let layout = {
9180
title: `${plotType} plot - ${solverConfig}`,
9281
width: plotWidth,
@@ -97,67 +86,26 @@ export function plotSolution(
9786
hovermode: "closest",
9887
};
9988

100-
if (isStructured) {
101-
// Calculate the number of nodes along the x-axis and y-axis
102-
const numNodesX = uniqueXCoords;
103-
const numNodesY = uniqueYCoords;
104-
105-
// Reshape the nodesXCoordinates and nodesYCoordinates arrays to match the grid dimensions
106-
let reshapedXCoordinates = math.reshape(Array.from(nodesXCoordinates), [numNodesX, numNodesY]);
107-
let reshapedYCoordinates = math.reshape(Array.from(nodesYCoordinates), [numNodesX, numNodesY]);
108-
109-
// Reshape the solution array to match the grid dimensions
110-
let reshapedSolution = math.reshape(Array.from(solutionVector), [numNodesX, numNodesY]);
111-
112-
// Transpose the reshapedSolution array to get column-wise data
113-
let transposedSolution = math.transpose(reshapedSolution);
114-
115-
// Create an array for x-coordinates used in the contour plot
116-
let reshapedXForPlot = [];
117-
for (let i = 0; i < numNodesX * numNodesY; i += numNodesY) {
118-
let xValue = nodesXCoordinates[i];
119-
reshapedXForPlot.push(xValue);
120-
}
121-
122-
// Create the data structure for the contour plot
123-
let contourData = {
124-
z: transposedSolution,
125-
type: "contour",
126-
contours: {
127-
coloring: "heatmap",
128-
showlabels: false,
129-
},
130-
//colorscale: 'Viridis',
131-
colorbar: {
132-
title: "Solution",
133-
},
134-
x: reshapedXForPlot,
135-
y: reshapedYCoordinates[0],
136-
name: "Solution Field",
137-
};
138-
139-
// Create the plot using Plotly
140-
Plotly.newPlot(plotDivId, [contourData], layout, { responsive: true });
141-
} else {
142-
// Create an interpolated contour plot for the unstructured mesh
143-
let contourData = {
144-
x: nodesXCoordinates,
145-
y: nodesYCoordinates,
146-
z: zValues,
147-
type: "contour",
148-
contours: {
149-
coloring: "heatmap",
150-
showlabels: false,
151-
},
152-
//colorscale: 'Viridis',
153-
colorbar: {
154-
title: "Solution",
155-
},
156-
name: "Solution Field",
157-
};
89+
// Create the plot
90+
let contourData = {
91+
x: nodesXCoordinates,
92+
y: nodesYCoordinates,
93+
z: zData,
94+
type: "contour",
95+
line: {
96+
smoothing: 0.85,
97+
},
98+
contours: {
99+
coloring: "heatmap",
100+
showlabels: false,
101+
},
102+
//colorscale: 'Viridis',
103+
colorbar: {
104+
title: "Solution",
105+
},
106+
name: "Solution Field",
107+
};
158108

159-
// Create the plot using only the contour fill
160-
Plotly.newPlot(plotDivId, [contourData], layout, { responsive: true });
161-
}
109+
Plotly.newPlot(plotDivId, [contourData], layout, { responsive: true });
162110
}
163111
}

0 commit comments

Comments
 (0)