-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathplotSolutionScript.js
More file actions
111 lines (101 loc) · 3.41 KB
/
plotSolutionScript.js
File metadata and controls
111 lines (101 loc) · 3.41 KB
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
/**
* ════════════════════════════════════════════════════════════
* FEAScript Library
* Lightweight Finite Element Simulation in JavaScript
* Version: 0.1.4 | https://feascript.com
* ════════════════════════════════════════════════════════════
*/
/**
* Function to create plots of the solution vector
* @param {*} solutionVector - The computed solution vector
* @param {*} nodesCoordinates - Object containing x and y coordinates for the nodes
* @param {string} solverConfig - Parameter specifying the type of solver
* @param {string} meshDimension - The dimension of the solution
* @param {string} plotType - The type of plot
* @param {string} plotDivId - The id of the div where the plot will be rendered
*/
export function plotSolution(
solutionVector,
nodesCoordinates,
solverConfig,
meshDimension,
plotType,
plotDivId
) {
const { nodesXCoordinates, nodesYCoordinates } = nodesCoordinates;
if (meshDimension === "1D" && plotType === "line") {
// Check if solutionVector is a nested array
let yData;
if (solutionVector.length > 0 && Array.isArray(solutionVector[0])) {
yData = solutionVector.map((arr) => arr[0]);
} else {
yData = solutionVector;
}
let xData = Array.from(nodesXCoordinates);
let lineData = {
x: nodesXCoordinates,
y: yData,
mode: "lines",
type: "scatter",
line: { color: "rgb(219, 64, 82)", width: 2 },
name: "Solution",
};
let maxWindowWidth = Math.min(window.innerWidth, 700);
let plotWidth = Math.min(maxWindowWidth, 600);
let plotHeight = 350;
let layout = {
title: `line plot - ${solverConfig}`,
width: plotWidth,
height: plotHeight,
xaxis: { title: "x" },
yaxis: { title: "Solution" },
margin: { l: 50, r: 50, t: 50, b: 50 },
};
Plotly.newPlot(plotDivId, [lineData], layout, { responsive: true });
} else if (meshDimension === "2D" && plotType === "contour") {
// Check if solutionVector is a nested array
let zData;
if (Array.isArray(solutionVector[0])) {
zData = solutionVector.map((val) => val[0]);
} else {
zData = solutionVector;
}
// Sizing parameters
let maxWindowWidth = Math.min(window.innerWidth, 700);
let maxX = Math.max(...nodesXCoordinates);
let maxY = Math.max(...nodesYCoordinates);
let aspectRatio = maxY / maxX;
let plotWidth = Math.min(maxWindowWidth, 600);
let plotHeight = plotWidth * aspectRatio;
// Layout properties
let layout = {
title: `${plotType} plot - ${solverConfig}`,
width: plotWidth,
height: plotHeight,
xaxis: { title: "x" },
yaxis: { title: "y" },
margin: { l: 50, r: 50, t: 50, b: 50 },
hovermode: "closest",
};
// Create the plot
let contourData = {
x: nodesXCoordinates,
y: nodesYCoordinates,
z: zData,
type: "contour",
line: {
smoothing: 0.85,
},
contours: {
coloring: "heatmap",
showlabels: false,
},
//colorscale: 'Viridis',
colorbar: {
title: "Solution",
},
name: "Solution Field",
};
Plotly.newPlot(plotDivId, [contourData], layout, { responsive: true });
}
}