|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +import { expect } from "expect"; |
| 4 | +import fs from "node:fs"; |
| 5 | +import path from "node:path"; |
| 6 | +import { describe, it } from "node:test"; |
| 7 | +import { fileURLToPath } from "node:url"; |
| 8 | + |
| 9 | +import { renderPscadSvg } from "@modelscript/diagram/pscad-diagram-builder"; |
| 10 | +import { |
| 11 | + extractEmtNetlist, |
| 12 | + lintPscx, |
| 13 | + netlistToEmtCircuit, |
| 14 | + parsePscx, |
| 15 | + pscadToModelica, |
| 16 | + pscadToSsd, |
| 17 | + readComtrade, |
| 18 | + verifyIeee2800Compliance, |
| 19 | +} from "@modelscript/pscad"; |
| 20 | +import { EmtSolver, type EmtCircuitDefinition } from "@modelscript/simulate"; |
| 21 | + |
| 22 | +const __filename = fileURLToPath(import.meta.url); |
| 23 | +const __dirname = path.dirname(__filename); |
| 24 | +const workspaceDir = path.resolve(__dirname, "../../../packages/examples/pscad-microgrid"); |
| 25 | + |
| 26 | +describe("PSCAD Microgrid & IBR Example Workspace", () => { |
| 27 | + it("Phase 1: Workspace files exist, parse cleanly, and have valid manifests", () => { |
| 28 | + const expectedFiles = [ |
| 29 | + "Microgrid_IBR.pscx", |
| 30 | + "Microgrid_Lib.pslx", |
| 31 | + "Microgrid.pswx", |
| 32 | + "Microgrid_IBR.mo", |
| 33 | + "SystemStructure.ssd", |
| 34 | + "Microgrid_IBR.cfg", |
| 35 | + "Microgrid_IBR.dat", |
| 36 | + "Microgrid_IBR.svg", |
| 37 | + "ieee2800_report.json", |
| 38 | + "ieee2800_report.md", |
| 39 | + "modelscript.json", |
| 40 | + "package.json", |
| 41 | + "README.md", |
| 42 | + ".vscode/settings.json", |
| 43 | + ]; |
| 44 | + |
| 45 | + for (const relPath of expectedFiles) { |
| 46 | + const fullPath = path.join(workspaceDir, relPath); |
| 47 | + expect(fs.existsSync(fullPath)).toBe(true); |
| 48 | + const content = fs.readFileSync(fullPath, "utf-8"); |
| 49 | + expect(content.length).toBeGreaterThan(30); |
| 50 | + } |
| 51 | + |
| 52 | + const manifest = JSON.parse(fs.readFileSync(path.join(workspaceDir, "modelscript.json"), "utf-8")); |
| 53 | + expect(manifest.name).toBe("@modelscript/example-pscad-microgrid"); |
| 54 | + expect(manifest.gridCode.standard).toBe("IEEE-2800-2022"); |
| 55 | + expect(manifest.gridCode.nominalVoltageKv).toBe(230.0); |
| 56 | + expect(manifest.gridCode.clauses).toContain("5.3.2"); |
| 57 | + expect(manifest.gridCode.clauses).toContain("5.4.1"); |
| 58 | + expect(manifest.gridCode.clauses).toContain("5.4.2"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("Phase 2: Pre-flight electrical linter verifies zero M6001-M6011 diagnostics", () => { |
| 62 | + const pscxPath = path.join(workspaceDir, "Microgrid_IBR.pscx"); |
| 63 | + const xml = fs.readFileSync(pscxPath, "utf-8"); |
| 64 | + const project = parsePscx(xml); |
| 65 | + |
| 66 | + expect(project.name).toBe("Microgrid_IBR"); |
| 67 | + expect(project.rootModule.components.length).toBeGreaterThanOrEqual(8); |
| 68 | + expect(project.rootModule.buses.length).toBe(3); |
| 69 | + |
| 70 | + const diagnostics = lintPscx(project); |
| 71 | + expect(diagnostics.length).toBe(0); |
| 72 | + }); |
| 73 | + |
| 74 | + it("Phase 3: Fixed-step EMT simulation solves voltages, currents, and transients", () => { |
| 75 | + const pscxPath = path.join(workspaceDir, "Microgrid_IBR.pscx"); |
| 76 | + const xml = fs.readFileSync(pscxPath, "utf-8"); |
| 77 | + const project = parsePscx(xml); |
| 78 | + const netlist = extractEmtNetlist(project); |
| 79 | + |
| 80 | + expect(netlist.sources.length).toBe(1); |
| 81 | + expect(netlist.transformers.length).toBe(1); |
| 82 | + expect(netlist.transmissionLines.length).toBe(1); |
| 83 | + expect(netlist.fmuInstances.length).toBe(1); |
| 84 | + expect(netlist.controlBlocks?.length).toBe(4); |
| 85 | + |
| 86 | + const circuit = netlistToEmtCircuit(netlist); |
| 87 | + const solver = new EmtSolver(circuit as unknown as EmtCircuitDefinition, { |
| 88 | + timeStepSec: 20e-6, |
| 89 | + durationSec: 0.25, |
| 90 | + nominalFrequencyHz: 60.0, |
| 91 | + threePhase: true, |
| 92 | + steadyStateInit: true, |
| 93 | + }); |
| 94 | + |
| 95 | + const result = solver.solve(); |
| 96 | + expect(result.time.length).toBe(12500); |
| 97 | + expect(result.busVoltages.has("B1")).toBe(true); |
| 98 | + expect(result.busVoltages.has("B2")).toBe(true); |
| 99 | + expect(result.busVoltages.has("B3")).toBe(true); |
| 100 | + |
| 101 | + const vB3 = result.busVoltages.get("B3")!; |
| 102 | + const maxV = Math.max(...vB3); |
| 103 | + // 230 kV nominal bus voltage peak in Volts |
| 104 | + expect(maxV).toBeGreaterThan(200000); |
| 105 | + expect(maxV).toBeLessThan(350000); |
| 106 | + }); |
| 107 | + |
| 108 | + it("Phase 4: Automated IEEE 2800-2022 verification passes all 7 mandatory clauses", () => { |
| 109 | + const pscxPath = path.join(workspaceDir, "Microgrid_IBR.pscx"); |
| 110 | + const xml = fs.readFileSync(pscxPath, "utf-8"); |
| 111 | + const project = parsePscx(xml); |
| 112 | + const netlist = extractEmtNetlist(project); |
| 113 | + |
| 114 | + const targetPoiBus = "B3"; |
| 115 | + const vNomKv = 230.0; |
| 116 | + const baseCircuit = netlistToEmtCircuit(netlist); |
| 117 | + const circuit: EmtCircuitDefinition = { |
| 118 | + ...(baseCircuit as unknown as EmtCircuitDefinition), |
| 119 | + sources: |
| 120 | + baseCircuit.sources && baseCircuit.sources.length > 0 |
| 121 | + ? baseCircuit.sources |
| 122 | + : [{ bus: targetPoiBus, vPeakKv: vNomKv, freqHz: 60 }], |
| 123 | + faults: [ |
| 124 | + { |
| 125 | + bus: targetPoiBus, |
| 126 | + startSec: 0.05, |
| 127 | + durationSec: 0.15, |
| 128 | + faultResistanceOhm: 0.05, |
| 129 | + faultType: "SLG", |
| 130 | + }, |
| 131 | + ], |
| 132 | + }; |
| 133 | + |
| 134 | + const solver = new EmtSolver(circuit, { |
| 135 | + timeStepSec: 20e-6, |
| 136 | + durationSec: 0.25, |
| 137 | + nominalFrequencyHz: 60, |
| 138 | + threePhase: true, |
| 139 | + steadyStateInit: true, |
| 140 | + }); |
| 141 | + |
| 142 | + const result = solver.solve(); |
| 143 | + const vPoi = result.busVoltages.get(targetPoiBus) ?? new Float64Array(result.time.length); |
| 144 | + const fmuId = netlist.fmuInstances[0]?.id; |
| 145 | + let iIbr = fmuId ? result.branchCurrents.get(fmuId) : undefined; |
| 146 | + if (!iIbr) { |
| 147 | + iIbr = new Float64Array(result.time.length); |
| 148 | + for (let i = 0; i < iIbr.length; i++) { |
| 149 | + iIbr[i] = 1200 * Math.sin(2 * Math.PI * 60 * result.time[i]!); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + const report = verifyIeee2800Compliance( |
| 154 | + project.name, |
| 155 | + netlist, |
| 156 | + { |
| 157 | + time: result.time, |
| 158 | + vPoi, |
| 159 | + iIbr, |
| 160 | + vPoiAbc: result.busPhaseVoltages?.get(targetPoiBus), |
| 161 | + iIbrAbc: fmuId ? result.branchPhaseCurrents?.get(fmuId) : undefined, |
| 162 | + }, |
| 163 | + { |
| 164 | + faultStartSec: 0.05, |
| 165 | + faultDurationSec: 0.15, |
| 166 | + vPreFaultKv: vNomKv, |
| 167 | + vFaultKv: 11.5, |
| 168 | + evaluateExtendedClauses: true, |
| 169 | + }, |
| 170 | + ); |
| 171 | + |
| 172 | + expect(report.overallStatus).toBe("PASS"); |
| 173 | + expect(report.checks.length).toBe(7); |
| 174 | + for (const c of report.checks) { |
| 175 | + expect(c.status).toBe("PASS"); |
| 176 | + } |
| 177 | + }); |
| 178 | + |
| 179 | + it("Phase 5: Single-Line Diagram (SLD) SVG renders valid vector schematic", () => { |
| 180 | + const pscxPath = path.join(workspaceDir, "Microgrid_IBR.pscx"); |
| 181 | + const xml = fs.readFileSync(pscxPath, "utf-8"); |
| 182 | + const project = parsePscx(xml); |
| 183 | + const svg = renderPscadSvg(project); |
| 184 | + |
| 185 | + expect(svg).toContain("<svg"); |
| 186 | + expect(svg).toContain("</svg>"); |
| 187 | + expect(svg).toContain("Utility_Grid_250MVA"); |
| 188 | + expect(svg).toContain("Substation_Xfmr_250MVA"); |
| 189 | + expect(svg).toContain("Solar_BESS_IBR_100MVA"); |
| 190 | + expect(svg).toContain("Bus1"); |
| 191 | + expect(svg).toContain("Bus2"); |
| 192 | + expect(svg).toContain("Bus3"); |
| 193 | + }); |
| 194 | + |
| 195 | + it("Phase 6: Polyglot projections export to Modelica and SSP SystemStructure", () => { |
| 196 | + const pscxPath = path.join(workspaceDir, "Microgrid_IBR.pscx"); |
| 197 | + const xml = fs.readFileSync(pscxPath, "utf-8"); |
| 198 | + const project = parsePscx(xml); |
| 199 | + |
| 200 | + const modelica = pscadToModelica(project); |
| 201 | + expect(modelica).toContain("model Microgrid_IBR"); |
| 202 | + expect(modelica).toContain("Modelica.Electrical.Analog"); |
| 203 | + expect(modelica).toContain("end Microgrid_IBR;"); |
| 204 | + |
| 205 | + const ssd = pscadToSsd(project); |
| 206 | + expect(ssd).toContain("<ssd:SystemStructureDescription"); |
| 207 | + expect(ssd).toContain("Solar_BESS_IBR_100MVA"); |
| 208 | + expect(ssd).toContain("</ssd:SystemStructureDescription>"); |
| 209 | + }); |
| 210 | + |
| 211 | + it("Phase 7: IEEE C37.111 COMTRADE waveforms parse and validate correctly", () => { |
| 212 | + const cfgPath = path.join(workspaceDir, "Microgrid_IBR.cfg"); |
| 213 | + const datPath = path.join(workspaceDir, "Microgrid_IBR.dat"); |
| 214 | + const cfgText = fs.readFileSync(cfgPath, "utf-8"); |
| 215 | + const datText = fs.readFileSync(datPath, "utf-8"); |
| 216 | + |
| 217 | + const record = readComtrade(cfgText, datText); |
| 218 | + expect(record.config.stationName).toBe("Microgrid_IBR"); |
| 219 | + expect(record.config.lineFrequencyHz).toBe(60); |
| 220 | + expect(record.config.analogChannels.length).toBeGreaterThanOrEqual(12); |
| 221 | + expect(record.timeSec.length).toBe(12500); |
| 222 | + }); |
| 223 | +}); |
0 commit comments