Compare commits

...

5 Commits

@ -0,0 +1,48 @@
/*
* 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 assert from "node:assert";
import { BusesColumnSpec, generateBusesData } from "./Buses";
import { generateCsv, parseCsv } from "../Common/Forms/DataTable";
import { TEST_DATA_1 } from "../../core/fixtures.test";
test("generate CSV", () => {
const [data, columns] = generateBusesData(TEST_DATA_1);
const actualCsv = generateCsv(data, columns);
const expectedCsv =
"Name,Load (MW) 00:00,Load (MW) 01:00,Load (MW) 02:00,Load (MW) 03:00,Load (MW) 04:00\n" +
"b1,35.79534,34.38835,33.45083,32.89729,33.25044\n" +
"b2,14.03739,13.48563,13.11797,12.9009,13.03939\n" +
"b3,27.3729,26.29698,25.58005,25.15675,25.4268";
assert.strictEqual(actualCsv, expectedCsv);
});
test("parse CSV", () => {
const csvContents =
"Name,Load (MW) 00:00,Load (MW) 01:00,Load (MW) 02:00,Load (MW) 03:00,Load (MW) 04:00\n" +
"b1,0,1,2,3,4\n" +
"b3,27.3729,26.29698,25.58005,25.15675,25.4268";
const [newBuses, err] = parseCsv(csvContents, BusesColumnSpec, TEST_DATA_1);
assert(err === null);
assert.deepEqual(newBuses, {
b1: {
"Load (MW)": [0, 1, 2, 3, 4],
},
b3: {
"Load (MW)": [27.3729, 26.29698, 25.58005, 25.15675, 25.4268],
},
});
});
test("parse CSV with duplicated names", () => {
const csvContents =
"Name,Load (MW) 00:00,Load (MW) 01:00,Load (MW) 02:00,Load (MW) 03:00,Load (MW) 04:00\n" +
"b1,0,0,0,0,0\n" +
"b1,0,0,0,0,0";
const [, err] = parseCsv(csvContents, BusesColumnSpec, TEST_DATA_1);
assert(err !== null);
assert.equal(err.message, `Name "b1" is duplicated (row 2)`);
});

@ -0,0 +1,111 @@
/*
* 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 {
floatFormatter,
generateTableColumns,
parseCsv,
} from "../Common/Forms/DataTable";
import {
parseProfiledUnitsCsv,
ProfiledUnitsColumnSpec,
} from "./ProfiledUnits";
import { TEST_DATA_1 } from "../../core/fixtures.test";
import assert from "node:assert";
import {
getProfiledGenerators,
getThermalGenerators,
} from "../../core/fixtures";
test("parse CSV", () => {
const csvContents =
"Name,Bus,Cost ($/MW),Maximum power (MW) 00:00,Maximum power (MW) 01:00," +
"Maximum power (MW) 02:00,Maximum power (MW) 03:00," +
"Maximum power (MW) 04:00,Minimum power (MW) 00:00," +
"Minimum power (MW) 01:00,Minimum power (MW) 02:00," +
"Minimum power (MW) 03:00,Minimum power (MW) 04:00\n" +
"pu1,b1,50,260.25384545,72.89148068,377.17886108,336.66732361," +
"376.82781758,52.05076909,14.57829614,75.43577222,67.33346472,75.36556352\n" +
"pu2,b1,0,0,0,0,0,0,0,0,0,0,0";
const [scenario, err] = parseProfiledUnitsCsv(csvContents, TEST_DATA_1);
assert(err === null);
const thermalGens = getThermalGenerators(scenario);
const profGens = getProfiledGenerators(scenario);
assert.equal(Object.keys(thermalGens).length, 1);
assert.equal(Object.keys(profGens).length, 2);
assert.deepEqual(profGens, {
pu1: {
Bus: "b1",
"Minimum power (MW)": [
52.05076909, 14.57829614, 75.43577222, 67.33346472, 75.36556352,
],
"Maximum power (MW)": [
260.25384545, 72.89148068, 377.17886108, 336.66732361, 376.82781758,
],
"Cost ($/MW)": 50.0,
Type: "Profiled",
},
pu2: {
Bus: "b1",
"Minimum power (MW)": [0, 0, 0, 0, 0],
"Maximum power (MW)": [0, 0, 0, 0, 0],
"Cost ($/MW)": 0.0,
Type: "Profiled",
},
});
});
test("parse CSV with invalid bus", () => {
const csvContents =
"Name,Bus,Cost ($/MW),Maximum power (MW) 00:00,Maximum power (MW) 01:00," +
"Maximum power (MW) 02:00,Maximum power (MW) 03:00," +
"Maximum power (MW) 04:00,Minimum power (MW) 00:00," +
"Minimum power (MW) 01:00,Minimum power (MW) 02:00," +
"Minimum power (MW) 03:00,Minimum power (MW) 04:00\n" +
"pu1,b99,50,260.25384545,72.89148068,377.17886108,336.66732361," +
"376.82781758,52.05076909,14.57829614,75.43577222,67.33346472,75.36556352\n" +
"pu2,b1,0,0,0,0,0,0,0,0,0,0,0";
const [, err] = parseCsv(csvContents, ProfiledUnitsColumnSpec, TEST_DATA_1);
assert(err !== null);
assert.equal(err.message, 'Bus "b99" does not exist (row 1)');
});
test("generateTableColumns", () => {
const columns = generateTableColumns(TEST_DATA_1, ProfiledUnitsColumnSpec);
assert.equal(columns.length, 5);
assert.deepEqual(columns[0], {
editor: "input",
editorParams: {
selectContents: true,
},
field: "Name",
formatter: "plaintext",
headerHozAlign: "left",
headerSort: false,
headerWordWrap: true,
hozAlign: "left",
minWidth: 100,
resizable: false,
title: "Name",
});
assert.equal(columns[3]!["columns"]!.length, 5);
assert.deepEqual(columns[3]!["columns"]![0], {
editor: "input",
editorParams: {
selectContents: true,
},
field: "Maximum power (MW) 00:00",
formatter: floatFormatter,
headerHozAlign: "left",
headerSort: false,
headerWordWrap: true,
hozAlign: "left",
minWidth: 60,
resizable: false,
title: "00:00",
});
});

@ -20,6 +20,7 @@ import DataTable, {
} from "../Common/Forms/DataTable";
import {
getProfiledGenerators,
getThermalGenerators,
UnitCommitmentScenario,
} from "../../core/fixtures";
import { ColumnDefinition } from "tabulator-tables";
@ -75,6 +76,31 @@ const generateProfiledUnitsData = (
return [data, columns];
};
export const parseProfiledUnitsCsv = (
csvContents: string,
scenario: UnitCommitmentScenario,
): [UnitCommitmentScenario, ValidationError | null] => {
const [profGens, err] = parseCsv(
csvContents,
ProfiledUnitsColumnSpec,
scenario,
);
if (err) return [scenario, err];
// Process imported generators
for (const gen in profGens) {
profGens[gen]["Type"] = "Profiled";
}
// Merge with existing data
const thermalGens = getThermalGenerators(scenario);
const newScenario = {
...scenario,
Generators: { ...thermalGens, ...profGens },
};
return [newScenario, null];
};
const ProfiledUnitsComponent = (props: CaseBuilderSectionProps) => {
const fileUploadElem = useRef<FileUploadElement>(null);
@ -85,24 +111,12 @@ const ProfiledUnitsComponent = (props: CaseBuilderSectionProps) => {
};
const onUpload = () => {
fileUploadElem.current!.showFilePicker((csvContents: any) => {
const [newGenerators, err] = parseCsv(
csvContents,
ProfiledUnitsColumnSpec,
props.scenario,
);
fileUploadElem.current!.showFilePicker((csv: any) => {
const [newScenario, err] = parseProfiledUnitsCsv(csv, props.scenario);
if (err) {
props.onError(err.message);
return;
}
for (const gen in newGenerators) {
newGenerators[gen]["Type"] = "Profiled";
}
const newScenario = {
...props.scenario,
Generators: newGenerators,
};
props.onDataChanged(newScenario);
});
};

@ -0,0 +1,209 @@
/*
* 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 {
floatFormatter,
generateCsv,
generateTableColumns,
generateTableData,
} from "../Common/Forms/DataTable";
import { TEST_DATA_1 } from "../../core/fixtures.test";
import {
generateThermalUnitsData,
parseThermalUnitsCsv,
ThermalUnitsColumnSpec,
} from "./ThermalUnits";
import assert from "node:assert";
import {
getProfiledGenerators,
getThermalGenerators,
} from "../../core/fixtures";
test("generateTableColumns", () => {
const columns = generateTableColumns(TEST_DATA_1, ThermalUnitsColumnSpec);
assert.equal(columns[2]!["columns"]!.length, 10);
assert.deepEqual(columns[2]!["columns"]![0], {
editor: "input",
editorParams: {
selectContents: true,
},
field: "Production cost curve (MW) 1",
formatter: floatFormatter,
headerHozAlign: "left",
headerSort: false,
headerWordWrap: true,
hozAlign: "left",
minWidth: 60,
resizable: false,
title: "1",
});
});
test("generateTableData", () => {
const data = generateTableData(
getThermalGenerators(TEST_DATA_1),
ThermalUnitsColumnSpec,
TEST_DATA_1,
);
assert.deepEqual(data[0], {
Name: "g1",
Bus: "b1",
"Initial power (MW)": 115,
"Initial status (h)": 12,
"Minimum downtime (h)": 4,
"Minimum uptime (h)": 4,
"Ramp down limit (MW)": 232.68,
"Ramp up limit (MW)": 232.68,
"Shutdown limit (MW)": 232.68,
"Startup limit (MW)": 232.68,
"Production cost curve ($) 1": 1400,
"Production cost curve ($) 2": 1600,
"Production cost curve ($) 3": 2200,
"Production cost curve ($) 4": 2400,
"Production cost curve ($) 5": "",
"Production cost curve ($) 6": "",
"Production cost curve ($) 7": "",
"Production cost curve ($) 8": "",
"Production cost curve ($) 9": "",
"Production cost curve ($) 10": "",
"Production cost curve (MW) 1": 100,
"Production cost curve (MW) 2": 110,
"Production cost curve (MW) 3": 130,
"Production cost curve (MW) 4": 135,
"Production cost curve (MW) 5": "",
"Production cost curve (MW) 6": "",
"Production cost curve (MW) 7": "",
"Production cost curve (MW) 8": "",
"Production cost curve (MW) 9": "",
"Production cost curve (MW) 10": "",
"Startup costs ($) 1": 300,
"Startup costs ($) 2": 400,
"Startup costs ($) 3": "",
"Startup costs ($) 4": "",
"Startup costs ($) 5": "",
"Startup delays (h) 1": 1,
"Startup delays (h) 2": 4,
"Startup delays (h) 3": "",
"Startup delays (h) 4": "",
"Startup delays (h) 5": "",
"Must run?": false,
});
});
const expectedCsvContents =
"Name,Bus," +
"Production cost curve (MW) 1," +
"Production cost curve (MW) 2," +
"Production cost curve (MW) 3," +
"Production cost curve (MW) 4," +
"Production cost curve (MW) 5," +
"Production cost curve (MW) 6," +
"Production cost curve (MW) 7," +
"Production cost curve (MW) 8," +
"Production cost curve (MW) 9," +
"Production cost curve (MW) 10," +
"Production cost curve ($) 1," +
"Production cost curve ($) 2," +
"Production cost curve ($) 3," +
"Production cost curve ($) 4," +
"Production cost curve ($) 5," +
"Production cost curve ($) 6," +
"Production cost curve ($) 7," +
"Production cost curve ($) 8," +
"Production cost curve ($) 9," +
"Production cost curve ($) 10," +
"Startup costs ($) 1," +
"Startup costs ($) 2," +
"Startup costs ($) 3," +
"Startup costs ($) 4," +
"Startup costs ($) 5," +
"Startup delays (h) 1," +
"Startup delays (h) 2," +
"Startup delays (h) 3," +
"Startup delays (h) 4," +
"Startup delays (h) 5," +
"Minimum uptime (h),Minimum downtime (h),Ramp up limit (MW)," +
"Ramp down limit (MW),Startup limit (MW),Shutdown limit (MW)," +
"Initial status (h),Initial power (MW),Must run?\n" +
"g1,b1,100,110,130,135,,,,,,,1400,1600,2200,2400,,,,,,,300,400,,,,1,4,,,,4,4,232.68,232.68,232.68,232.68,12,115,false";
const invalidCsv =
"Name,Bus," +
"Production cost curve (MW) 1," +
"Production cost curve (MW) 2," +
"Production cost curve (MW) 3," +
"Production cost curve (MW) 4," +
"Production cost curve (MW) 5," +
"Production cost curve (MW) 6," +
"Production cost curve (MW) 7," +
"Production cost curve (MW) 8," +
"Production cost curve (MW) 9," +
"Production cost curve (MW) 10," +
"Production cost curve ($) 1," +
"Production cost curve ($) 2," +
"Production cost curve ($) 3," +
"Production cost curve ($) 4," +
"Production cost curve ($) 5," +
"Production cost curve ($) 6," +
"Production cost curve ($) 7," +
"Production cost curve ($) 8," +
"Production cost curve ($) 9," +
"Production cost curve ($) 10," +
"Startup costs ($) 1," +
"Startup costs ($) 2," +
"Startup costs ($) 3," +
"Startup costs ($) 4," +
"Startup costs ($) 5," +
"Startup delays (h) 1," +
"Startup delays (h) 2," +
"Startup delays (h) 3," +
"Startup delays (h) 4," +
"Startup delays (h) 5," +
"Minimum uptime (h),Minimum downtime (h),Ramp up limit (MW)," +
"Ramp down limit (MW),Startup limit (MW),Shutdown limit (MW)," +
"Initial status (h),Initial power (MW),Must run?\n" +
"g1,b1,100,110,130,x,,,,,,,1400,1600,2200,2400,,,,,,,300,400,,,,1,4,,,,4,4,232.68,232.68,232.68,232.68,12,115,false";
test("generateCSV", () => {
const [data, columns] = generateThermalUnitsData(TEST_DATA_1);
const actualCsvContents = generateCsv(data, columns);
assert.equal(actualCsvContents, expectedCsvContents);
});
test("parseCSV", () => {
const [scenario, err] = parseThermalUnitsCsv(
expectedCsvContents,
TEST_DATA_1,
);
assert(!err);
const thermalGens = getThermalGenerators(scenario);
const profGens = getProfiledGenerators(scenario);
assert.equal(Object.keys(thermalGens).length, 1);
assert.equal(Object.keys(profGens).length, 2);
assert.deepEqual(thermalGens["g1"], {
Bus: "b1",
Type: "Thermal",
"Production cost curve (MW)": [100.0, 110.0, 130.0, 135.0],
"Production cost curve ($)": [1400.0, 1600.0, 2200.0, 2400.0],
"Startup costs ($)": [300.0, 400.0],
"Startup delays (h)": [1, 4],
"Ramp up limit (MW)": 232.68,
"Ramp down limit (MW)": 232.68,
"Startup limit (MW)": 232.68,
"Shutdown limit (MW)": 232.68,
"Minimum downtime (h)": 4,
"Minimum uptime (h)": 4,
"Initial status (h)": 12,
"Initial power (MW)": 115,
"Must run?": false,
});
});
test("parseCSV with invalid number[T]", () => {
const [, err] = parseThermalUnitsCsv(invalidCsv, TEST_DATA_1);
assert(err);
assert.equal(err.message, '"x" is not a valid number (row 1)');
});

@ -6,8 +6,10 @@
import DataTable, {
ColumnSpec,
generateCsv,
generateTableColumns,
generateTableData,
parseCsv,
} from "../Common/Forms/DataTable";
import { CaseBuilderSectionProps } from "./CaseBuilder";
import { useRef } from "react";
@ -21,10 +23,18 @@ import {
faUpload,
} from "@fortawesome/free-solid-svg-icons";
import {
getProfiledGenerators,
getThermalGenerators,
UnitCommitmentScenario,
} from "../../core/fixtures";
import { ColumnDefinition } from "tabulator-tables";
import { offerDownload } from "../Common/io";
import {
changeThermalUnitData,
createThermalUnit,
deleteGenerator,
renameGenerator,
} from "../../core/Operations/generatorOps";
export const ThermalUnitsColumnSpec: ColumnSpec[] = [
{
@ -108,7 +118,7 @@ export const ThermalUnitsColumnSpec: ColumnSpec[] = [
},
];
const generateThermalUnitsData = (
export const generateThermalUnitsData = (
scenario: UnitCommitmentScenario,
): [any[], ColumnDefinition[]] => {
const columns = generateTableColumns(scenario, ThermalUnitsColumnSpec);
@ -120,50 +130,63 @@ const generateThermalUnitsData = (
return [data, columns];
};
export const parseThermalUnitsCsv = (
csvContents: string,
scenario: UnitCommitmentScenario,
): [UnitCommitmentScenario, ValidationError | null] => {
const [thermalGens, err] = parseCsv(
csvContents,
ThermalUnitsColumnSpec,
scenario,
);
if (err) return [scenario, err];
// Process imported generators
for (const gen in thermalGens) {
thermalGens[gen]["Type"] = "Thermal";
}
// Merge with existing data
const profGens = getProfiledGenerators(scenario);
const newScenario = {
...scenario,
Generators: { ...thermalGens, ...profGens },
};
return [newScenario, null];
};
const ThermalUnitsComponent = (props: CaseBuilderSectionProps) => {
const fileUploadElem = useRef<FileUploadElement>(null);
const onDownload = () => {
// const [data, columns] = generateThermalUnitsData(props.scenario);
// const csvContents = generateCsv(data, columns);
// offerDownload(csvContents, "text/csv", "profiled_units.csv");
const [data, columns] = generateThermalUnitsData(props.scenario);
const csvContents = generateCsv(data, columns);
offerDownload(csvContents, "text/csv", "thermal_units.csv");
};
const onUpload = () => {
// fileUploadElem.current!.showFilePicker((csvContents: any) => {
// const [newGenerators, err] = parseCsv(
// csvContents,
// ThermalUnitsColumnSpec,
// props.scenario,
// );
// if (err) {
// props.onError(err.message);
// return;
// }
// for (const gen in newGenerators) {
// newGenerators[gen]["Type"] = "Thermal";
// }
//
// const newScenario = {
// ...props.scenario,
// Generators: newGenerators,
// };
// props.onDataChanged(newScenario);
// });
fileUploadElem.current!.showFilePicker((csv: any) => {
const [newScenario, err] = parseThermalUnitsCsv(csv, props.scenario);
if (err) {
props.onError(err.message);
return;
}
props.onDataChanged(newScenario);
});
};
const onAdd = () => {
// const [newScenario, err] = createThermalUnit(props.scenario);
// if (err) {
// props.onError(err.message);
// return;
// }
// props.onDataChanged(newScenario);
const [newScenario, err] = createThermalUnit(props.scenario);
if (err) {
props.onError(err.message);
return;
}
props.onDataChanged(newScenario);
};
const onDelete = (name: string): ValidationError | null => {
// const newScenario = deleteGenerator(name, props.scenario);
// props.onDataChanged(newScenario);
const newScenario = deleteGenerator(name, props.scenario);
props.onDataChanged(newScenario);
return null;
};
@ -172,17 +195,17 @@ const ThermalUnitsComponent = (props: CaseBuilderSectionProps) => {
field: string,
newValue: string,
): ValidationError | null => {
// const [newScenario, err] = changeThermalUnitData(
// name,
// field,
// newValue,
// props.scenario,
// );
// if (err) {
// props.onError(err.message);
// return err;
// }
// props.onDataChanged(newScenario);
const [newScenario, err] = changeThermalUnitData(
name,
field,
newValue,
props.scenario,
);
if (err) {
props.onError(err.message);
return err;
}
props.onDataChanged(newScenario);
return null;
};
@ -190,16 +213,16 @@ const ThermalUnitsComponent = (props: CaseBuilderSectionProps) => {
oldName: string,
newName: string,
): ValidationError | null => {
// const [newScenario, err] = renameGenerator(
// oldName,
// newName,
// props.scenario,
// );
// if (err) {
// props.onError(err.message);
// return err;
// }
// props.onDataChanged(newScenario);
const [newScenario, err] = renameGenerator(
oldName,
newName,
props.scenario,
);
if (err) {
props.onError(err.message);
return err;
}
props.onDataChanged(newScenario);
return null;
};

@ -1,214 +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 assert from "node:assert";
import { BusesColumnSpec, generateBusesData } from "../../CaseBuilder/Buses";
import {
floatFormatter,
generateCsv,
generateTableColumns,
generateTableData,
parseCsv,
} from "./DataTable";
import { TEST_DATA_1 } from "../../../core/fixtures.test";
import { ProfiledUnitsColumnSpec } from "../../CaseBuilder/ProfiledUnits";
import { ThermalUnitsColumnSpec } from "../../CaseBuilder/ThermalUnits";
import { getThermalGenerators } from "../../../core/fixtures";
test("generateTableColumns (ProfiledUnits)", () => {
const columns = generateTableColumns(TEST_DATA_1, ProfiledUnitsColumnSpec);
assert.equal(columns.length, 5);
assert.deepEqual(columns[0], {
editor: "input",
editorParams: {
selectContents: true,
},
field: "Name",
formatter: "plaintext",
headerHozAlign: "left",
headerSort: false,
headerWordWrap: true,
hozAlign: "left",
minWidth: 100,
resizable: false,
title: "Name",
});
assert.equal(columns[3]!["columns"]!.length, 5);
assert.deepEqual(columns[3]!["columns"]![0], {
editor: "input",
editorParams: {
selectContents: true,
},
field: "Maximum power (MW) 00:00",
formatter: floatFormatter,
headerHozAlign: "left",
headerSort: false,
headerWordWrap: true,
hozAlign: "left",
minWidth: 60,
resizable: false,
title: "00:00",
});
});
test("generateTableColumns (ThermalUnits)", () => {
const columns = generateTableColumns(TEST_DATA_1, ThermalUnitsColumnSpec);
assert.equal(columns[2]!["columns"]!.length, 10);
assert.deepEqual(columns[2]!["columns"]![0], {
editor: "input",
editorParams: {
selectContents: true,
},
field: "Production cost curve (MW) 1",
formatter: floatFormatter,
headerHozAlign: "left",
headerSort: false,
headerWordWrap: true,
hozAlign: "left",
minWidth: 60,
resizable: false,
title: "1",
});
});
test("generateTableData (ThermalUnits)", () => {
const data = generateTableData(
getThermalGenerators(TEST_DATA_1),
ThermalUnitsColumnSpec,
TEST_DATA_1,
);
assert.deepEqual(data[0], {
Name: "gen1",
Bus: "b1",
"Initial power (MW)": 115,
"Initial status (h)": 12,
"Minimum downtime (h)": 4,
"Minimum uptime (h)": 4,
"Ramp down limit (MW)": 232.68,
"Ramp up limit (MW)": 232.68,
"Shutdown limit (MW)": 232.68,
"Startup limit (MW)": 232.68,
"Production cost curve ($) 1": 1400,
"Production cost curve ($) 2": 1600,
"Production cost curve ($) 3": 2200,
"Production cost curve ($) 4": 2400,
"Production cost curve ($) 5": "",
"Production cost curve ($) 6": "",
"Production cost curve ($) 7": "",
"Production cost curve ($) 8": "",
"Production cost curve ($) 9": "",
"Production cost curve ($) 10": "",
"Production cost curve (MW) 1": 100,
"Production cost curve (MW) 2": 110,
"Production cost curve (MW) 3": 130,
"Production cost curve (MW) 4": 135,
"Production cost curve (MW) 5": "",
"Production cost curve (MW) 6": "",
"Production cost curve (MW) 7": "",
"Production cost curve (MW) 8": "",
"Production cost curve (MW) 9": "",
"Production cost curve (MW) 10": "",
"Startup costs ($) 1": 300,
"Startup costs ($) 2": 400,
"Startup costs ($) 3": "",
"Startup costs ($) 4": "",
"Startup costs ($) 5": "",
"Startup delays (h) 1": 1,
"Startup delays (h) 2": 4,
"Startup delays (h) 3": "",
"Startup delays (h) 4": "",
"Startup delays (h) 5": "",
});
});
test("generate CSV", () => {
const [data, columns] = generateBusesData(TEST_DATA_1);
const actualCsv = generateCsv(data, columns);
const expectedCsv =
"Name,Load (MW) 00:00,Load (MW) 01:00,Load (MW) 02:00,Load (MW) 03:00,Load (MW) 04:00\n" +
"b1,35.79534,34.38835,33.45083,32.89729,33.25044\n" +
"b2,14.03739,13.48563,13.11797,12.9009,13.03939\n" +
"b3,27.3729,26.29698,25.58005,25.15675,25.4268";
assert.strictEqual(actualCsv, expectedCsv);
});
test("parse CSV (Buses)", () => {
const csvContents =
"Name,Load (MW) 00:00,Load (MW) 01:00,Load (MW) 02:00,Load (MW) 03:00,Load (MW) 04:00\n" +
"b1,0,1,2,3,4\n" +
"b3,27.3729,26.29698,25.58005,25.15675,25.4268";
const [newBuses, err] = parseCsv(csvContents, BusesColumnSpec, TEST_DATA_1);
assert(err === null);
assert.deepEqual(newBuses, {
b1: {
"Load (MW)": [0, 1, 2, 3, 4],
},
b3: {
"Load (MW)": [27.3729, 26.29698, 25.58005, 25.15675, 25.4268],
},
});
});
test("parse CSV with duplicated names", () => {
const csvContents =
"Name,Load (MW) 00:00,Load (MW) 01:00,Load (MW) 02:00,Load (MW) 03:00,Load (MW) 04:00\n" +
"b1,0,0,0,0,0\n" +
"b1,0,0,0,0,0";
const [, err] = parseCsv(csvContents, BusesColumnSpec, TEST_DATA_1);
assert(err !== null);
assert.equal(err.message, `Name "b1" is duplicated (row 2)`);
});
test("parse CSV (Profiled Units)", () => {
const csvContents =
"Name,Bus,Cost ($/MW),Maximum power (MW) 00:00,Maximum power (MW) 01:00," +
"Maximum power (MW) 02:00,Maximum power (MW) 03:00," +
"Maximum power (MW) 04:00,Minimum power (MW) 00:00," +
"Minimum power (MW) 01:00,Minimum power (MW) 02:00," +
"Minimum power (MW) 03:00,Minimum power (MW) 04:00\n" +
"pu1,b1,50,260.25384545,72.89148068,377.17886108,336.66732361," +
"376.82781758,52.05076909,14.57829614,75.43577222,67.33346472,75.36556352\n" +
"pu2,b1,0,0,0,0,0,0,0,0,0,0,0";
const [newGenerators, err] = parseCsv(
csvContents,
ProfiledUnitsColumnSpec,
TEST_DATA_1,
);
assert(err === null);
assert.deepEqual(newGenerators, {
pu1: {
Bus: "b1",
"Minimum power (MW)": [
52.05076909, 14.57829614, 75.43577222, 67.33346472, 75.36556352,
],
"Maximum power (MW)": [
260.25384545, 72.89148068, 377.17886108, 336.66732361, 376.82781758,
],
"Cost ($/MW)": 50.0,
},
pu2: {
Bus: "b1",
"Minimum power (MW)": [0, 0, 0, 0, 0],
"Maximum power (MW)": [0, 0, 0, 0, 0],
"Cost ($/MW)": 0.0,
},
});
});
test("parse CSV with invalid bus", () => {
const csvContents =
"Name,Bus,Cost ($/MW),Maximum power (MW) 00:00,Maximum power (MW) 01:00," +
"Maximum power (MW) 02:00,Maximum power (MW) 03:00," +
"Maximum power (MW) 04:00,Minimum power (MW) 00:00," +
"Minimum power (MW) 01:00,Minimum power (MW) 02:00," +
"Minimum power (MW) 03:00,Minimum power (MW) 04:00\n" +
"pu1,b99,50,260.25384545,72.89148068,377.17886108,336.66732361," +
"376.82781758,52.05076909,14.57829614,75.43577222,67.33346472,75.36556352\n" +
"pu2,b1,0,0,0,0,0,0,0,0,0,0,0";
const [, err] = parseCsv(csvContents, ProfiledUnitsColumnSpec, TEST_DATA_1);
assert(err !== null);
assert.equal(err.message, 'Bus "b99" does not exist (row 1)');
});

@ -13,7 +13,7 @@ import {
import { ValidationError } from "../../../core/Validation/validate";
import { UnitCommitmentScenario } from "../../../core/fixtures";
import Papa from "papaparse";
import { parseNumber } from "../../../core/Operations/commonOps";
import { parseBool, parseNumber } from "../../../core/Operations/commonOps";
export interface ColumnSpec {
title: string;
@ -129,7 +129,9 @@ export const generateTableData = (
break;
case "number[N]":
for (let i = 0; i < spec.length!; i++) {
entry[`${spec.title} ${i + 1}`] = entryData[spec.title][i] || "";
let v = entryData[spec.title][i];
if (v === undefined || v === null) v = "";
entry[`${spec.title} ${i + 1}`] = v;
}
break;
default:
@ -226,11 +228,12 @@ export const parseCsv = (
case "string":
data[name][spec.title] = row[spec.title];
break;
case "number":
case "number": {
const [val, err] = parseNumber(row[spec.title]);
if (err) return [null, { message: err.message + rowRef }];
data[name][spec.title] = val;
break;
}
case "busRef":
const busName = row[spec.title];
if (!(busName in scenario.Buses)) {
@ -241,15 +244,36 @@ export const parseCsv = (
}
data[name][spec.title] = row[spec.title];
break;
case "number[T]":
case "number[T]": {
data[name][spec.title] = Array(timeslots.length);
for (let i = 0; i < timeslots.length; i++) {
data[name][spec.title][i] = parseFloat(
row[`${spec.title} ${timeslots[i]}`],
);
const [vf, err] = parseNumber(row[`${spec.title} ${timeslots[i]}`]);
if (err) return [data, { message: err.message + rowRef }];
data[name][spec.title][i] = vf;
}
break;
}
case "number[N]": {
data[name][spec.title] = Array(spec.length).fill(0);
for (let i = 0; i < spec.length!; i++) {
let v = row[`${spec.title} ${i + 1}`];
if (v.trim() === "") {
data[name][spec.title].splice(i, spec.length! - i);
break;
} else {
const [vf, err] = parseNumber(row[`${spec.title} ${i + 1}`]);
if (err) return [data, { message: err.message + rowRef }];
data[name][spec.title][i] = vf;
}
}
break;
}
case "boolean": {
const [val, err] = parseBool(row[spec.title]);
if (err) return [data, { message: err.message + rowRef }];
data[name][spec.title] = val;
break;
}
default:
throw Error(`Unknown type: ${spec.type}`);
}
@ -326,10 +350,9 @@ const DataTable = (props: DataTableProps) => {
useEffect(() => {
const onCellEdited = (cell: CellComponent) => {
let newValue = cell.getValue();
let oldValue = cell.getOldValue();
// eslint-disable-next-line eqeqeq
if (newValue == oldValue) return;
let newValue = `${cell.getValue()}`;
let oldValue = `${cell.getOldValue()}`;
if (newValue === oldValue) return;
if (cell.getField() === "Name") {
if (newValue === "") {
const err = props.onRowDeleted(oldValue);

@ -0,0 +1,30 @@
/*
* 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 { parseBool } from "./commonOps";
import assert from "node:assert";
test("parseBool", () => {
// True values
for (const str of ["true", "TRUE", "1"]) {
let [v, err] = parseBool(str);
assert(!err);
assert.equal(v, true);
}
// False values
for (const str of ["false", "FALSE", "0"]) {
let [v, err] = parseBool(str);
assert(!err);
assert.equal(v, false);
}
// Invalid values
for (const str of ["qwe", ""]) {
let [, err] = parseBool(str);
assert(err);
}
});

@ -51,6 +51,18 @@ export const parseNumber = (
}
};
export const parseBool = (
valueStr: string,
): [boolean, ValidationError | null] => {
if (["true", "1"].includes(valueStr.toLowerCase())) {
return [true, null];
}
if (["false", "0"].includes(valueStr.toLowerCase())) {
return [false, null];
}
return [true, { message: `"${valueStr}" is not a valid boolean value` }];
};
export const changeStringData = (
field: string,
newValue: string,
@ -96,7 +108,26 @@ export const changeNumberData = (
];
};
export const changeNumberVecData = (
export const changeBooleanData = (
field: string,
newValueStr: string,
container: { [key: string]: any },
): [{ [key: string]: any }, ValidationError | null] => {
// Parse value
const [newValueBool, err] = parseBool(newValueStr);
if (err) return [container, err];
// Build the new object
return [
{
...container,
[field]: newValueBool,
},
null,
];
};
export const changeNumberVecTData = (
field: string,
time: string,
newValueStr: string,
@ -124,6 +155,43 @@ export const changeNumberVecData = (
];
};
export const changeNumberVecNData = (
field: string,
offset: string,
newValueStr: string,
container: { [key: string]: any },
): [{ [key: string]: any }, ValidationError | null] => {
const oldVec = container[field];
const newVec = [...container[field]];
const idx = parseInt(offset) - 1;
if (newValueStr === "") {
// Trim the vector
newVec.splice(idx, oldVec.length - idx);
} else {
// Parse new value
const [newValueFloat, err] = parseNumber(newValueStr);
if (err) return [container, err];
// Increase the length of the vector
if (idx >= oldVec.length) {
for (let i = oldVec.length; i < idx; i++) {
newVec[i] = 0;
}
}
// Assign new value
newVec[idx] = newValueFloat;
}
return [
{
...container,
[field]: newVec,
},
null,
];
};
export const changeData = (
field: string,
newValueStr: string,
@ -131,9 +199,9 @@ export const changeData = (
colSpecs: ColumnSpec[],
scenario: UnitCommitmentScenario,
): [{ [key: string]: any }, ValidationError | null] => {
const match = field.match(/^([^0-9]+)(\d+:\d+)?$/);
const match = field.match(/^([^0-9]+)([0-9:]+)?$/);
const fieldName = match![1]!.trim();
const fieldTime = match![2];
const fieldOffset = match![2];
for (const spec of colSpecs) {
if (spec.title !== fieldName) continue;
switch (spec.type) {
@ -144,13 +212,22 @@ export const changeData = (
case "number":
return changeNumberData(fieldName, newValueStr, container);
case "number[T]":
return changeNumberVecData(
return changeNumberVecTData(
fieldName,
fieldTime!,
fieldOffset!,
newValueStr,
container,
scenario,
);
case "number[N]":
return changeNumberVecNData(
fieldName,
fieldOffset!,
newValueStr,
container,
);
case "boolean":
return changeBooleanData(fieldName, newValueStr, container);
default:
throw Error(`Unknown type: ${spec.type}`);
}

@ -8,22 +8,26 @@ import { TEST_DATA_1, TEST_DATA_BLANK } from "../fixtures.test";
import assert from "node:assert";
import {
changeProfiledUnitData,
changeThermalUnitData,
createProfiledUnit,
createThermalUnit,
deleteGenerator,
renameGenerator,
} from "./generatorOps";
import { ValidationError } from "../Validation/validate";
test("createProfiledUnit", () => {
const [newScenario, err] = createProfiledUnit(TEST_DATA_1);
assert(err === null);
assert.equal(Object.keys(newScenario.Generators).length, 4);
assert.deepEqual(newScenario.Generators["pu3"], {
Bus: "b1",
Type: "Profiled",
"Cost ($/MW)": 0,
"Maximum power (MW)": [0, 0, 0, 0, 0],
"Minimum power (MW)": [0, 0, 0, 0, 0],
});
assert("pu3" in newScenario.Generators);
});
test("createThermalUnit", () => {
const [newScenario, err] = createThermalUnit(TEST_DATA_1);
assert(err === null);
assert.equal(Object.keys(newScenario.Generators).length, 4);
assert("g2" in newScenario.Generators);
});
test("createProfiledUnit with blank file", () => {
@ -34,7 +38,7 @@ test("createProfiledUnit with blank file", () => {
test("changeProfiledUnitData", () => {
let scenario = TEST_DATA_1;
let err = null;
let err: ValidationError | null;
[scenario, err] = changeProfiledUnitData(
"pu1",
"Cost ($/MW)",
@ -60,6 +64,58 @@ test("changeProfiledUnitData", () => {
});
});
test("changeThermalUnitData", () => {
let scenario = TEST_DATA_1;
let err: ValidationError | null;
[scenario, err] = changeThermalUnitData(
"g1",
"Ramp up limit (MW)",
"99",
scenario,
);
assert(!err);
[scenario, err] = changeThermalUnitData(
"g1",
"Startup costs ($) 2",
"99",
scenario,
);
assert(!err);
[scenario, err] = changeThermalUnitData(
"g1",
"Production cost curve ($) 7",
"99",
scenario,
);
assert(!err);
[scenario, err] = changeThermalUnitData(
"g1",
"Production cost curve (MW) 3",
"",
scenario,
);
assert(!err);
[scenario, err] = changeThermalUnitData("g1", "Must run?", "true", scenario);
assert(!err);
assert.deepEqual(scenario.Generators["g1"], {
Bus: "b1",
Type: "Thermal",
"Production cost curve (MW)": [100.0, 110],
"Production cost curve ($)": [1400.0, 1600.0, 2200.0, 2400.0, 0, 0, 99],
"Startup costs ($)": [300.0, 99.0],
"Startup delays (h)": [1, 4],
"Ramp up limit (MW)": 99,
"Ramp down limit (MW)": 232.68,
"Startup limit (MW)": 232.68,
"Shutdown limit (MW)": 232.68,
"Minimum downtime (h)": 4,
"Minimum uptime (h)": 4,
"Initial status (h)": 12,
"Initial power (MW)": 115,
"Must run?": true,
});
});
test("changeProfiledUnitData with invalid bus", () => {
let scenario = TEST_DATA_1;
let err = null;
@ -71,7 +127,7 @@ test("changeProfiledUnitData with invalid bus", () => {
test("deleteGenerator", () => {
const newScenario = deleteGenerator("pu1", TEST_DATA_1);
assert.equal(Object.keys(newScenario.Generators).length, 2);
assert("gen1" in newScenario.Generators);
assert("g1" in newScenario.Generators);
assert("pu2" in newScenario.Generators);
});

@ -13,14 +13,22 @@ import {
renameItemInObject,
} from "./commonOps";
import { ProfiledUnitsColumnSpec } from "../../components/CaseBuilder/ProfiledUnits";
import { ThermalUnitsColumnSpec } from "../../components/CaseBuilder/ThermalUnits";
const assertBusesNotEmpty = (
scenario: UnitCommitmentScenario,
): ValidationError | null => {
if (Object.keys(scenario.Buses).length === 0)
return { message: "Profiled unit requires an existing bus." };
return null;
};
export const createProfiledUnit = (
scenario: UnitCommitmentScenario,
): [UnitCommitmentScenario, ValidationError | null] => {
const busNames = Object.keys(scenario.Buses);
if (busNames.length === 0) {
return [scenario, { message: "Profiled unit requires an existing bus." }];
}
const err = assertBusesNotEmpty(scenario);
if (err) return [scenario, err];
const busName = Object.keys(scenario.Buses)[0]!;
const timeslots = generateTimeslots(scenario);
const name = generateUniqueName(scenario.Generators, "pu");
return [
@ -29,7 +37,7 @@ export const createProfiledUnit = (
Generators: {
...scenario.Generators,
[name]: {
Bus: busNames[0]!,
Bus: busName,
Type: "Profiled",
"Cost ($/MW)": 0,
"Minimum power (MW)": Array(timeslots.length).fill(0),
@ -41,6 +49,41 @@ export const createProfiledUnit = (
];
};
export const createThermalUnit = (
scenario: UnitCommitmentScenario,
): [UnitCommitmentScenario, ValidationError | null] => {
const err = assertBusesNotEmpty(scenario);
if (err) return [scenario, err];
const busName = Object.keys(scenario.Buses)[0]!;
const name = generateUniqueName(scenario.Generators, "g");
return [
{
...scenario,
Generators: {
...scenario.Generators,
[name]: {
Bus: busName,
Type: "Thermal",
"Production cost curve (MW)": [0, 100],
"Production cost curve ($)": [0, 10],
"Startup costs ($)": [0],
"Startup delays (h)": [1],
"Ramp up limit (MW)": "",
"Ramp down limit (MW)": "",
"Startup limit (MW)": "",
"Shutdown limit (MW)": "",
"Minimum downtime (h)": 1,
"Minimum uptime (h)": 1,
"Initial status (h)": -24,
"Initial power (MW)": 0,
"Must run?": false,
},
},
},
null,
];
};
export const changeProfiledUnitData = (
generator: string,
field: string,
@ -67,6 +110,32 @@ export const changeProfiledUnitData = (
];
};
export const changeThermalUnitData = (
generator: string,
field: string,
newValueStr: string,
scenario: UnitCommitmentScenario,
): [UnitCommitmentScenario, ValidationError | null] => {
const [newGen, err] = changeData(
field,
newValueStr,
scenario.Generators[generator]!,
ThermalUnitsColumnSpec,
scenario,
);
if (err) return [scenario, err];
return [
{
...scenario,
Generators: {
...scenario.Generators,
[generator]: newGen,
} as Generators,
},
null,
];
};
export const deleteGenerator = (
name: string,
scenario: UnitCommitmentScenario,

@ -13,7 +13,7 @@ export const TEST_DATA_1: UnitCommitmentScenario = {
b3: { "Load (MW)": [27.3729, 26.29698, 25.58005, 25.15675, 25.4268] },
},
Generators: {
gen1: {
g1: {
Bus: "b1",
Type: "Thermal",
"Production cost curve (MW)": [100.0, 110.0, 130.0, 135.0],

@ -27,10 +27,10 @@ export interface ThermalUnit {
"Production cost curve ($)": number[];
"Startup costs ($)": number[];
"Startup delays (h)": number[];
"Ramp up limit (MW)": number;
"Ramp down limit (MW)": number;
"Startup limit (MW)": number;
"Shutdown limit (MW)": number;
"Ramp up limit (MW)": number | "";
"Ramp down limit (MW)": number | "";
"Startup limit (MW)": number | "";
"Shutdown limit (MW)": number | "";
"Minimum downtime (h)": number;
"Minimum uptime (h)": number;
"Initial status (h)": number;
@ -158,7 +158,7 @@ export const TEST_SCENARIO: UnitCommitmentScenario = {
],
"Cost ($/MW)": 50.0,
},
gen1: {
g1: {
Bus: "b1",
Type: "Thermal",
"Production cost curve (MW)": [100.0, 110.0, 130.0, 135.0],

Loading…
Cancel
Save