-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGridResults.tsx
More file actions
190 lines (166 loc) · 7.39 KB
/
Copy pathGridResults.tsx
File metadata and controls
190 lines (166 loc) · 7.39 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
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
import React, { useMemo, useState } from "react";
import { Autocomplete, Banner, Table, Typography } from "@equinor/eds-core-react";
import { GridSimulationResult } from "@/dto/GridSimulation";
import { formatConcentration } from "@/functions/Formatting";
import {
buildGridCsv,
collectOutputSubstances,
defaultSelectedSubstances,
pointOutput,
visiblePhaseKinds,
} from "@/functions/GridSimulation";
import { optionName } from "@/functions/Substance";
import LineChart, { LineSeries } from "@/components/LineChart";
import DownloadButton from "@/components/DownloadButton";
import { useAvailableModels } from "@/contexts/ModelContext";
import { buildModelSections, phaseLabel } from "@/utils/modelUtils";
import ModelAccordionLayout, { AccordionItem } from "@/components/ModelAccordionLayout";
interface GridResultsProps {
result: GridSimulationResult;
}
interface GridPhaseChartProps {
result: GridSimulationResult;
modelIndex: number;
phaseKind: string;
}
const GridPhaseChart: React.FC<GridPhaseChartProps> = ({ result, modelIndex, phaseKind }) => {
const { simulations, axes } = result;
const allSubstances = useMemo(
() => collectOutputSubstances(simulations, modelIndex, phaseKind),
[simulations, modelIndex, phaseKind]
);
const [selectedSubstances, setSelectedSubstances] = useState<string[]>(() =>
defaultSelectedSubstances(simulations, modelIndex, phaseKind)
);
const xAxisSubstance = axes[0]?.substance ?? "Unknown";
const xValues = simulations.map((sim) => parseFloat(sim.input.concentrations[xAxisSubstance].toFixed(2)) ?? 0);
const series: LineSeries[] = selectedSubstances.map((substance) => ({
label: optionName(substance),
data: simulations.map((sim) => pointOutput(sim, substance, modelIndex, phaseKind)),
}));
return (
<>
<Autocomplete
label="Output substances"
options={allSubstances}
selectedOptions={selectedSubstances}
multiple
onOptionsChange={({ selectedItems }) => setSelectedSubstances(selectedItems)}
optionLabel={optionName}
style={{ maxWidth: "400px", marginBottom: "1rem" }}
/>
{series.length === 0 ? (
<Typography variant="body_short" italic>
Select at least one output substance to plot.
</Typography>
) : (
<LineChart
xValues={xValues}
series={series}
xAxisLabel={`${xAxisSubstance} (ppm)`}
yAxisLabel={`Output concentration (${phaseKind === "aqueous" ? "wt%" : "ppm·mol"})`}
aspectRatio={2}
/>
)}
<Typography variant="h5" style={{ margin: "1.5rem 0 1rem" }}>
Values
</Typography>
<Table>
<Table.Head>
<Table.Row>
{axes.map((axis) => (
<Table.Cell key={axis.substance}>{axis.substance} (ppm)</Table.Cell>
))}
{selectedSubstances.map((substance) => (
<Table.Cell key={substance}>{optionName(substance)}</Table.Cell>
))}
</Table.Row>
</Table.Head>
<Table.Body>
{simulations.map((sim, idx) => (
<Table.Row key={idx}>
{axes.map((axis) => (
<Table.Cell key={axis.substance}>
{sim.input.concentrations[axis.substance] ?? 0}
</Table.Cell>
))}
{selectedSubstances.map((substance) => (
<Table.Cell key={substance}>
{sim.status === "done"
? formatConcentration(pointOutput(sim, substance, modelIndex, phaseKind) ?? 0)
: sim.status === "error"
? "error"
: "…"}
</Table.Cell>
))}
</Table.Row>
))}
</Table.Body>
</Table>
</>
);
};
interface GridSectionProps {
result: GridSimulationResult;
modelIndex: number;
}
const GridSection: React.FC<GridSectionProps> = ({ result, modelIndex }) => {
const phases = visiblePhaseKinds(result.simulations, modelIndex);
if (phases.length === 1) {
return <GridPhaseChart result={result} modelIndex={modelIndex} phaseKind={phases[0]} />;
}
return (
<div style={{ display: "flex", flexDirection: "column", gap: "1.5rem" }}>
{phases.map((kind) => (
<div key={kind}>
<Typography variant="h5" style={{ marginBottom: "0.5rem" }}>
{phaseLabel(kind)}
</Typography>
<GridPhaseChart result={result} modelIndex={modelIndex} phaseKind={kind} />
</div>
))}
</div>
);
};
const GridResults: React.FC<GridResultsProps> = ({ result }) => {
const { simulations, axes } = result;
const { models } = useAvailableModels();
const firstSim = simulations[0];
const inputModels = firstSim?.input.models ?? [];
const sections = buildModelSections(inputModels, models);
const erroredSimulations = simulations.filter((sim) => sim.status === "error");
const xAxisSubstance = axes[0]?.substance ?? "Unknown";
const modelLabel = inputModels.map((model) => model.modelId).join(" → ") || "Unknown model";
const items: AccordionItem[] = sections.flatMap((section) =>
section.indices.map((modelIndex) => ({
key: `${section.category}-${modelIndex}`,
header: `${section.category}: ${models.find((m) => m.modelId === inputModels[modelIndex]?.modelId)?.displayName ?? inputModels[modelIndex]?.modelId}`,
content: <GridSection result={result} modelIndex={modelIndex} />,
}))
);
return (
<>
<Typography variant="body_short" style={{ marginBottom: "1rem" }}>
Varying <strong>{optionName(xAxisSubstance)}</strong> across {simulations.length} values using{" "}
<strong>{modelLabel}</strong>.
</Typography>
{erroredSimulations.length > 0 && (
<Banner style={{ marginBottom: "1rem" }}>
<Banner.Icon variant="warning">⚠️</Banner.Icon>
<Banner.Message>
{erroredSimulations.length} of {simulations.length} runs failed and are omitted from the chart.
</Banner.Message>
</Banner>
)}
<ModelAccordionLayout items={items} />
<div style={{ display: "flex", justifyContent: "flex-end", marginTop: "1rem", marginBottom: "2rem" }}>
<DownloadButton
csvContent={buildGridCsv(result)}
fileName={`AcidWatch-Grid-${new Date().toISOString().replace(/[:.]/g, "-")}.csv`}
isLoading={false}
/>
</div>
</>
);
};
export default GridResults;