web: Reorganize

This commit is contained in:
2025-05-16 14:37:53 -05:00
parent 957294f220
commit 6d9bbaab4e
15 changed files with 34 additions and 32 deletions

View File

@@ -1,122 +0,0 @@
/*
* UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment
* Copyright (C) 2020-2025, UChicago Argonne, LLC. All rights reserved.
* Released under the modified BSD license. See COPYING.md for more details.
*/
import { UnitCommitmentScenario } from "../../../core/data";
import {
changeBusData,
createBus,
deleteBus,
renameBus,
} from "./BusOperations";
import assert from "node:assert";
export const BUS_TEST_DATA_1: UnitCommitmentScenario = {
Parameters: {
Version: "0.4",
"Power balance penalty ($/MW)": 1000.0,
"Time horizon (h)": 5,
"Time step (min)": 60,
},
Buses: {
b1: { "Load (MW)": [35.79534, 34.38835, 33.45083, 32.89729, 33.25044] },
b2: { "Load (MW)": [14.03739, 13.48563, 13.11797, 12.9009, 13.03939] },
b3: { "Load (MW)": [27.3729, 26.29698, 25.58005, 25.15675, 25.4268] },
},
};
export const BUS_TEST_DATA_2: UnitCommitmentScenario = {
Parameters: {
Version: "0.4",
"Power balance penalty ($/MW)": 1000.0,
"Time horizon (h)": 2,
"Time step (min)": 30,
},
Buses: {
b1: { "Load (MW)": [30, 30, 30, 30] },
b2: { "Load (MW)": [10, 20, 30, 40] },
b3: { "Load (MW)": [0, 30, 0, 40] },
},
};
test("createBus", () => {
const newScenario = createBus(BUS_TEST_DATA_1);
assert.deepEqual(Object.keys(newScenario.Buses), ["b1", "b2", "b3", "b4"]);
});
test("changeBusData", () => {
let scenario = BUS_TEST_DATA_1;
let err = null;
[scenario, err] = changeBusData("b1", "Load 0", "99", scenario);
assert.equal(err, null);
[scenario, err] = changeBusData("b1", "Load 3", "99", scenario);
assert.equal(err, null);
[scenario, err] = changeBusData("b3", "Load 4", "99", scenario);
assert.equal(err, null);
assert.deepEqual(scenario, {
Parameters: {
Version: "0.4",
"Power balance penalty ($/MW)": 1000.0,
"Time horizon (h)": 5,
"Time step (min)": 60,
},
Buses: {
b1: { "Load (MW)": [99, 34.38835, 33.45083, 99, 33.25044] },
b2: { "Load (MW)": [14.03739, 13.48563, 13.11797, 12.9009, 13.03939] },
b3: { "Load (MW)": [27.3729, 26.29698, 25.58005, 25.15675, 99] },
},
});
});
test("changeBusData with invalid numbers", () => {
let [, err] = changeBusData("b1", "Load 0", "xx", BUS_TEST_DATA_1);
assert(err !== null);
assert.equal(err.message, "Invalid value: xx");
});
test("deleteBus", () => {
let scenario = BUS_TEST_DATA_1;
scenario = deleteBus("b2", scenario);
assert.deepEqual(scenario, {
Parameters: {
Version: "0.4",
"Power balance penalty ($/MW)": 1000.0,
"Time horizon (h)": 5,
"Time step (min)": 60,
},
Buses: {
b1: { "Load (MW)": [35.79534, 34.38835, 33.45083, 32.89729, 33.25044] },
b3: { "Load (MW)": [27.3729, 26.29698, 25.58005, 25.15675, 25.4268] },
},
});
});
test("renameBus", () => {
let [scenario, err] = renameBus("b2", "b99", BUS_TEST_DATA_1);
assert(err === null);
assert.deepEqual(scenario, {
Parameters: {
Version: "0.4",
"Power balance penalty ($/MW)": 1000.0,
"Time horizon (h)": 5,
"Time step (min)": 60,
},
Buses: {
b1: { "Load (MW)": [35.79534, 34.38835, 33.45083, 32.89729, 33.25044] },
b99: { "Load (MW)": [14.03739, 13.48563, 13.11797, 12.9009, 13.03939] },
b3: { "Load (MW)": [27.3729, 26.29698, 25.58005, 25.15675, 25.4268] },
},
});
});
test("renameBus with duplicated name", () => {
let [, err] = renameBus("b3", "b1", BUS_TEST_DATA_1);
assert(err != null);
assert.equal(err.message, `Bus b1 already exists`);
});

View File

@@ -1,106 +0,0 @@
/*
* UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment
* Copyright (C) 2020-2025, UChicago Argonne, LLC. All rights reserved.
* Released under the modified BSD license. See COPYING.md for more details.
*/
import { Buses, UnitCommitmentScenario } from "../../../core/data";
import { ValidationError } from "../../../core/Validation/validate";
const generateUniqueBusName = (scenario: UnitCommitmentScenario) => {
let newBusName = "b";
let counter = 1;
let name = `${newBusName}${counter}`;
while (name in scenario.Buses) {
counter++;
name = `${newBusName}${counter}`;
}
return name;
};
const generateDefaultBusLoad = (scenario: UnitCommitmentScenario) => {
const T =
scenario.Parameters["Time horizon (h)"] *
(60 / scenario.Parameters["Time step (min)"]);
return new Array(T).fill(0);
};
export const createBus = (scenario: UnitCommitmentScenario) => {
const load = generateDefaultBusLoad(scenario);
let name = generateUniqueBusName(scenario);
return {
...scenario,
Buses: {
...scenario.Buses,
[name]: {
"Load (MW)": load,
},
},
};
};
export const changeBusData = (
bus: string,
field: string,
newValueStr: string,
scenario: UnitCommitmentScenario,
): [UnitCommitmentScenario, ValidationError | null] => {
// Load (MW)
const match = field.match(/Load (\d+)/);
if (match) {
const newValueFloat = parseFloat(newValueStr);
if (isNaN(newValueFloat)) {
return [scenario, { message: `Invalid value: ${newValueStr}` }];
}
const idx = parseInt(match[1]!, 10);
const newLoad = [...scenario.Buses[bus]!["Load (MW)"]];
newLoad[idx] = newValueFloat;
return [
{
...scenario,
Buses: {
...scenario.Buses,
[bus]: {
"Load (MW)": newLoad,
},
},
},
null,
];
}
throw Error(`Unknown field: ${field}`);
};
export const deleteBus = (bus: string, scenario: UnitCommitmentScenario) => {
const { [bus]: _, ...newBuses } = scenario.Buses;
return {
...scenario,
Buses: newBuses,
};
};
export const renameBus = (
oldName: string,
newName: string,
scenario: UnitCommitmentScenario,
): [UnitCommitmentScenario, ValidationError | null] => {
if (newName in scenario.Buses) {
return [scenario, { message: `Bus ${newName} already exists` }];
}
const newBuses: Buses = Object.keys(scenario.Buses).reduce((acc, val) => {
if (val === oldName) {
acc[newName] = scenario.Buses[val]!;
} else {
acc[val] = scenario.Buses[val]!;
}
return acc;
}, {} as Buses);
return [
{
...scenario,
Buses: newBuses,
},
null,
];
};

View File

@@ -4,19 +4,19 @@
* Released under the modified BSD license. See COPYING.md for more details.
*/
import SectionHeader from "../../Common/SectionHeader/SectionHeader";
import { UnitCommitmentScenario } from "../../../core/data";
import SectionHeader from "../Common/SectionHeader/SectionHeader";
import { UnitCommitmentScenario } from "../../core/fixtures";
import BusesTable, { generateBusesCsv, parseBusesCsv } from "./BusesTable";
import SectionButton from "../../Common/Buttons/SectionButton";
import SectionButton from "../Common/Buttons/SectionButton";
import {
faDownload,
faPlus,
faUpload,
} from "@fortawesome/free-solid-svg-icons";
import { offerDownload } from "../../Common/io";
import FileUploadElement from "../../Common/Buttons/FileUploadElement";
import { offerDownload } from "../Common/io";
import FileUploadElement from "../Common/Buttons/FileUploadElement";
import { useRef } from "react";
import { ValidationError } from "../../../core/Validation/validate";
import { ValidationError } from "../../core/Validation/validate";
interface BusesProps {
scenario: UnitCommitmentScenario;

View File

@@ -6,7 +6,7 @@
import assert from "node:assert";
import { generateBusesCsv, parseBusesCsv } from "./BusesTable";
import { BUS_TEST_DATA_1 } from "./BusOperations.test";
import { BUS_TEST_DATA_1 } from "../../core/Operations/busOperations.test";
test("generate CSV", () => {
const actualCsv = generateBusesCsv(BUS_TEST_DATA_1);

View File

@@ -5,14 +5,14 @@
*/
import Papa from "papaparse";
import { Buses, UnitCommitmentScenario } from "../../../core/data";
import { Buses, UnitCommitmentScenario } from "../../core/fixtures";
import { useEffect, useRef } from "react";
import {
CellComponent,
ColumnDefinition,
TabulatorFull as Tabulator,
} from "tabulator-tables";
import { ValidationError } from "../../../core/Validation/validate";
import { ValidationError } from "../../core/Validation/validate";
const generateBusesTableData = (scenario: UnitCommitmentScenario) => {
const tableData: { [name: string]: any }[] = [];

View File

@@ -4,19 +4,19 @@
* Released under the modified BSD license. See COPYING.md for more details.
*/
import Header from "./Header/Header";
import Parameters from "./Parameters/Parameters";
import BusesComponent from "./Buses/BusesComponent";
import Header from "./Header";
import Parameters from "./Parameters";
import BusesComponent from "./BusesComponent";
import {
BLANK_SCENARIO,
TEST_SCENARIO,
UnitCommitmentScenario,
} from "../../core/data";
} from "../../core/fixtures";
import "tabulator-tables/dist/css/tabulator.min.css";
import "../Common/Forms/Tables.css";
import { useState } from "react";
import Footer from "./Footer/Footer";
import Footer from "./Footer";
import { validate, ValidationError } from "../../core/Validation/validate";
import { offerDownload } from "../Common/io";
import {
@@ -24,11 +24,11 @@ import {
createBus,
deleteBus,
renameBus,
} from "./Buses/BusOperations";
} from "../../core/Operations/busOperations";
import {
changeTimeHorizon,
changeTimeStep,
} from "./Parameters/ParameterOperations";
} from "../../core/Operations/parameterOperations";
const CaseBuilder = () => {
const [scenario, setScenario] = useState(TEST_SCENARIO);

View File

@@ -5,10 +5,10 @@
*/
import styles from "./Header.module.css";
import SiteHeaderButton from "../../Common/Buttons/SiteHeaderButton";
import { UnitCommitmentScenario } from "../../../core/data";
import SiteHeaderButton from "../Common/Buttons/SiteHeaderButton";
import { UnitCommitmentScenario } from "../../core/fixtures";
import { useRef } from "react";
import FileUploadElement from "../../Common/Buttons/FileUploadElement";
import FileUploadElement from "../Common/Buttons/FileUploadElement";
interface HeaderProps {
onClear: () => void;

View File

@@ -4,11 +4,11 @@
* Released under the modified BSD license. See COPYING.md for more details.
*/
import SectionHeader from "../../Common/SectionHeader/SectionHeader";
import Form from "../../Common/Forms/Form";
import TextInputRow from "../../Common/Forms/TextInputRow";
import { UnitCommitmentScenario } from "../../../core/data";
import { ValidationError } from "../../../core/Validation/validate";
import SectionHeader from "../Common/SectionHeader/SectionHeader";
import Form from "../Common/Forms/Form";
import TextInputRow from "../Common/Forms/TextInputRow";
import { UnitCommitmentScenario } from "../../core/fixtures";
import { ValidationError } from "../../core/Validation/validate";
interface ParametersProps {
scenario: UnitCommitmentScenario;

View File

@@ -1,147 +0,0 @@
/*
* UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment
* Copyright (C) 2020-2025, UChicago Argonne, LLC. All rights reserved.
* Released under the modified BSD license. See COPYING.md for more details.
*/
import {
changeTimeHorizon,
changeTimeStep,
evaluatePwlFunction,
} from "./ParameterOperations";
import { BUS_TEST_DATA_1, BUS_TEST_DATA_2 } from "../Buses/BusOperations.test";
import assert from "node:assert";
test("changeTimeHorizon: Shrink 1", () => {
const [newScenario, err] = changeTimeHorizon(BUS_TEST_DATA_1, "3");
assert(err === null);
assert.deepEqual(newScenario, {
Parameters: {
Version: "0.4",
"Power balance penalty ($/MW)": 1000.0,
"Time horizon (h)": 3,
"Time step (min)": 60,
},
Buses: {
b1: { "Load (MW)": [35.79534, 34.38835, 33.45083] },
b2: { "Load (MW)": [14.03739, 13.48563, 13.11797] },
b3: { "Load (MW)": [27.3729, 26.29698, 25.58005] },
},
});
});
test("changeTimeHorizon: Shrink 2", () => {
const [newScenario, err] = changeTimeHorizon(BUS_TEST_DATA_2, "1");
assert(err === null);
assert.deepEqual(newScenario, {
Parameters: {
Version: "0.4",
"Power balance penalty ($/MW)": 1000.0,
"Time horizon (h)": 1,
"Time step (min)": 30,
},
Buses: {
b1: { "Load (MW)": [30, 30] },
b2: { "Load (MW)": [10, 20] },
b3: { "Load (MW)": [0, 30] },
},
});
});
test("changeTimeHorizon grow", () => {
const [newScenario, err] = changeTimeHorizon(BUS_TEST_DATA_1, "7");
assert(err === null);
assert.deepEqual(newScenario, {
Parameters: {
Version: "0.4",
"Power balance penalty ($/MW)": 1000.0,
"Time horizon (h)": 7,
"Time step (min)": 60,
},
Buses: {
b1: {
"Load (MW)": [35.79534, 34.38835, 33.45083, 32.89729, 33.25044, 0, 0],
},
b2: {
"Load (MW)": [14.03739, 13.48563, 13.11797, 12.9009, 13.03939, 0, 0],
},
b3: {
"Load (MW)": [27.3729, 26.29698, 25.58005, 25.15675, 25.4268, 0, 0],
},
},
});
});
test("changeTimeHorizon invalid", () => {
let [, err] = changeTimeHorizon(BUS_TEST_DATA_1, "x");
assert(err !== null);
assert.equal(err.message, "Invalid value: x");
[, err] = changeTimeHorizon(BUS_TEST_DATA_1, "-3");
assert(err !== null);
assert.equal(err.message, "Invalid value: -3");
});
test("evaluatePwlFunction", () => {
const data_x = [0, 60, 120, 180];
const data_y = [100, 200, 250, 100];
assert.equal(evaluatePwlFunction(data_x, data_y, 0), 100);
assert.equal(evaluatePwlFunction(data_x, data_y, 15), 125);
assert.equal(evaluatePwlFunction(data_x, data_y, 30), 150);
assert.equal(evaluatePwlFunction(data_x, data_y, 60), 200);
assert.equal(evaluatePwlFunction(data_x, data_y, 180), 100);
});
test("changeTimeStep", () => {
let [scenario, err] = changeTimeStep(BUS_TEST_DATA_2, "15");
assert(err === null);
assert.deepEqual(scenario, {
Parameters: {
Version: "0.4",
"Power balance penalty ($/MW)": 1000.0,
"Time horizon (h)": 2,
"Time step (min)": 15,
},
Buses: {
b1: { "Load (MW)": [30, 30, 30, 30, 30, 30, 30, 30] },
b2: { "Load (MW)": [10, 15, 20, 25, 30, 35, 40, 25] },
b3: { "Load (MW)": [0, 15, 30, 15, 0, 20, 40, 20] },
},
});
[scenario, err] = changeTimeStep(BUS_TEST_DATA_2, "60");
assert(err === null);
assert.deepEqual(scenario, {
Parameters: {
Version: "0.4",
"Power balance penalty ($/MW)": 1000.0,
"Time horizon (h)": 2,
"Time step (min)": 60,
},
Buses: {
b1: { "Load (MW)": [30, 30] },
b2: { "Load (MW)": [10, 30] },
b3: { "Load (MW)": [0, 0] },
},
});
});
test("changeTimeStep invalid", () => {
let [, err] = changeTimeStep(BUS_TEST_DATA_2, "x");
assert(err !== null);
assert.equal(err.message, "Invalid value: x");
[, err] = changeTimeStep(BUS_TEST_DATA_2, "-10");
assert(err !== null);
assert.equal(err.message, "Invalid value: -10");
[, err] = changeTimeStep(BUS_TEST_DATA_2, "120");
assert(err !== null);
assert.equal(err.message, "Invalid value: 120");
[, err] = changeTimeStep(BUS_TEST_DATA_2, "7");
assert(err !== null);
assert.equal(err.message, "Time step must be a divisor of 60: 7");
});
export {};

View File

@@ -1,123 +0,0 @@
/*
* UnitCommitment.jl: Optimization Package for Security-Constrained Unit Commitment
* Copyright (C) 2020-2025, UChicago Argonne, LLC. All rights reserved.
* Released under the modified BSD license. See COPYING.md for more details.
*/
import { Buses, UnitCommitmentScenario } from "../../../core/data";
import { ValidationError } from "../../../core/Validation/validate";
export const changeTimeHorizon = (
scenario: UnitCommitmentScenario,
newTimeHorizonStr: string,
): [UnitCommitmentScenario, ValidationError | null] => {
// Parse string
const newTimeHorizon = parseInt(newTimeHorizonStr);
if (isNaN(newTimeHorizon) || newTimeHorizon <= 0) {
return [scenario, { message: `Invalid value: ${newTimeHorizonStr}` }];
}
const newScenario = JSON.parse(
JSON.stringify(scenario),
) as UnitCommitmentScenario;
newScenario.Parameters["Time horizon (h)"] = newTimeHorizon;
const newT = (newTimeHorizon * 60) / scenario.Parameters["Time step (min)"];
const oldT =
(scenario.Parameters["Time horizon (h)"] * 60) /
scenario.Parameters["Time step (min)"];
if (newT < oldT) {
Object.values(newScenario.Buses).forEach((bus) => {
bus["Load (MW)"] = bus["Load (MW)"].slice(0, newT);
});
} else {
const padding = Array(newT - oldT).fill(0);
Object.values(newScenario.Buses).forEach((bus) => {
bus["Load (MW)"] = bus["Load (MW)"].concat(padding);
});
}
return [newScenario, null];
};
export const evaluatePwlFunction = (
data_x: number[],
data_y: number[],
x: number,
) => {
if (x < data_x[0]! || x > data_x[data_x.length - 1]!) {
throw Error("PWL interpolation: Out of bounds");
}
if (x === data_x[0]) return data_y[0];
// Binary search to find the interval containing x
let low = 0;
let high = data_x.length - 1;
while (low < high) {
let mid = Math.floor((low + high) / 2);
if (data_x[mid]! < x) low = mid + 1;
else high = mid;
}
// Linear interpolation within the found interval
const x1 = data_x[low - 1]!;
const y1 = data_y[low - 1]!;
const x2 = data_x[low]!;
const y2 = data_y[low]!;
return y1 + ((x - x1) * (y2 - y1)) / (x2 - x1);
};
export const changeTimeStep = (
scenario: UnitCommitmentScenario,
newTimeStepStr: string,
): [UnitCommitmentScenario, ValidationError | null] => {
// Parse string and perform validation
const newTimeStep = parseFloat(newTimeStepStr);
if (isNaN(newTimeStep) || newTimeStep < 1 || newTimeStep > 60) {
return [scenario, { message: `Invalid value: ${newTimeStepStr}` }];
}
if (60 % newTimeStep !== 0) {
return [
scenario,
{ message: `Time step must be a divisor of 60: ${newTimeStepStr}` },
];
}
// Build data_x
let timeHorizon = scenario.Parameters["Time horizon (h)"];
const oldTimeStep = scenario.Parameters["Time step (min)"];
const oldT = (timeHorizon * 60) / oldTimeStep;
const newT = (timeHorizon * 60) / newTimeStep;
const data_x = Array(oldT + 1).fill(0);
for (let i = 0; i <= oldT; i++) data_x[i] = i * oldTimeStep;
const newBuses: Buses = {};
for (const busName in scenario.Buses) {
// Build data_y
const busLoad = scenario.Buses[busName]!["Load (MW)"];
const data_y = Array(oldT + 1).fill(0);
for (let i = 0; i < oldT; i++) data_y[i] = busLoad[i];
data_y[oldT] = data_y[0];
// Run interpolation
const newBusLoad = Array(newT).fill(0);
for (let i = 0; i < newT; i++) {
newBusLoad[i] = evaluatePwlFunction(data_x, data_y, newTimeStep * i);
}
newBuses[busName] = {
...scenario.Buses[busName],
"Load (MW)": newBusLoad,
};
}
return [
{
...scenario,
Parameters: {
...scenario.Parameters,
"Time step (min)": newTimeStep,
},
Buses: newBuses,
},
null,
];
};