From eba8d51e3e8364023d19c3d53d020216d9361928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Mon, 3 Feb 2025 15:57:04 +0100 Subject: [PATCH] code + log clean --- .../src/app/editor/circuit/Calculation.ts | 41 +++++++++++-------- .../src/app/editor/junction/Junction.ts | 2 +- src/main/angular/src/app/editor/wire/Wire.ts | 4 +- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/main/angular/src/app/editor/circuit/Calculation.ts b/src/main/angular/src/app/editor/circuit/Calculation.ts index cb22976..1b67f46 100644 --- a/src/main/angular/src/app/editor/circuit/Calculation.ts +++ b/src/main/angular/src/app/editor/circuit/Calculation.ts @@ -16,6 +16,7 @@ export class Calculation { private potentials: number[] = []; constructor( + readonly number: number, readonly pivot: Junction, readonly parts: Part[], readonly junctionsWithoutPivot: Junction[], @@ -31,10 +32,13 @@ export class Calculation { junction.minCircuitVoltage = minCircuitVoltage; junction.maxCircuitVoltage = maxCircuitVoltage; junction.voltage = this.getPotential(junctionsWithoutPivot.indexOf(junction)); + console.debug(" junction", junction.fullName, junction.voltage, 'V'); }); pivot.minCircuitVoltage = minCircuitVoltage; pivot.maxCircuitVoltage = maxCircuitVoltage; pivot.voltage = 0; + const junctionCountIncludingPivot = junctionsWithoutPivot.length + 1; + console.debug(` => Circuit #${number} (${parts.length} parts, ${junctionCountIncludingPivot} junctions, ${wires.length} wires)`); } private matrixInit(size: number) { @@ -71,6 +75,7 @@ export class Calculation { const potentialEnd = this.getPotential(indexEnd); const potentialDifference = potentialStart === null || potentialEnd === null ? 0 : potentialEnd - potentialStart; wire.current = conductance * potentialDifference; + console.debug(" wire", wire.toString(), wire.current, 'A'); } private getPotential(index: number): number | null { @@ -101,31 +106,32 @@ export class Calculation { static calculate(circuit: Circuit): Calculation[] { const restParts = [...circuit.parts]; - console.debug("Recalculating circuit...") + console.debug("Calculating circuit...") const circuits: Calculation[] = []; - let circuitNumber = 0; + let circuitCount = 0; while (true) { - const battery = restParts.filter(p => p instanceof Battery)[0]; - if (!battery) { + const pivot = restParts.filter(p => p instanceof Battery)[0]; + if (!pivot) { break; } - const circuit = this.calculateDisjunct(battery, restParts, circuitNumber++); + const circuit = this.calculateDisjunct(pivot.minus, restParts, circuitCount++); circuits.push(circuit); } + console.debug(`Found ${circuitCount} disjunct circuits.`); if (restParts.length > 0) { - console.debug(`found ${restParts.length} not connected to any battery`); + console.debug(`Got ${restParts.length} unconnected parts.`); } return circuits; } - private static calculateDisjunct(battery: Battery, restParts: Part[], circuitNumber: number): Calculation { + private static calculateDisjunct(pivot: Junction, restParts: Part[], circuitNumber: number): Calculation { console.debug(` Circuit #${circuitNumber}:`) + console.debug(" pivot:", pivot.fullName); - const foundParts: Part[] = [battery]; - restParts.splice(restParts.indexOf(battery), 1); + const foundParts: Part[] = [pivot.part]; + restParts.splice(restParts.indexOf(pivot.part), 1); - const pivot = battery.minus; - const foundJunctions: Junction[] = []; + const foundJunctionsWithoutPivot: Junction[] = []; const todo: Junction[] = [pivot]; const foundWires: Wire[] = []; @@ -141,21 +147,20 @@ export class Calculation { } const destinationJunction = wire.traverse(sourceJunction); const destinationPart = destinationJunction.part; - const newJunction = destinationJunction !== pivot && !foundJunctions.includes(destinationJunction); - console.debug(` ${newJunction ? "[NEW]" : "[___]"} ${wire}`) - if (newJunction) { + const newJunctionAndNotPivot = destinationJunction !== pivot && !foundJunctionsWithoutPivot.includes(destinationJunction); + console.debug(` ${newJunctionAndNotPivot ? "[NEW]" : "[___]"} Wire ${wire}`) + if (newJunctionAndNotPivot) { const newPart = !foundParts.includes(destinationPart); if (newPart) { foundParts.push(destinationPart); restParts.splice(restParts.indexOf(destinationPart), 1); } todo.push(destinationJunction); - foundJunctions.push(destinationJunction); + foundJunctionsWithoutPivot.push(destinationJunction); } } } - const junctionCountIncludingPivot = foundJunctions.length + 1; - console.debug(` => Circuit #${circuitNumber} (${foundParts.length} parts, ${junctionCountIncludingPivot} junctions, ${foundWires.length} wires)`); - return new Calculation(pivot, foundParts, foundJunctions, foundWires); + return new Calculation(circuitNumber, pivot, foundParts, foundJunctionsWithoutPivot, foundWires); } + } diff --git a/src/main/angular/src/app/editor/junction/Junction.ts b/src/main/angular/src/app/editor/junction/Junction.ts index a932ec5..43fa1c5 100644 --- a/src/main/angular/src/app/editor/junction/Junction.ts +++ b/src/main/angular/src/app/editor/junction/Junction.ts @@ -34,7 +34,7 @@ export class Junction { } get fullName(): string { - return `'${this.part.name}' '${this.name}'`; + return `"${this.part.name} (Pin ${this.name})"`; } get pixelX(): number { diff --git a/src/main/angular/src/app/editor/wire/Wire.ts b/src/main/angular/src/app/editor/wire/Wire.ts index c605814..eb70c50 100644 --- a/src/main/angular/src/app/editor/wire/Wire.ts +++ b/src/main/angular/src/app/editor/wire/Wire.ts @@ -28,9 +28,9 @@ export class Wire { toString() { if (this.start.part === this.end.part && this.name !== null) { - return `'${this.start.part}' "${this.name}"`; + return `"${this.start.part}" "${this.name}"`; } - return `${this.name !== null ? this.name + ' ' : ''}${this.start.fullName} ==> ${this.end.fullName}`; + return `${this.start.fullName} ==> ${this.end.fullName}`; } traverse(junction: Junction) {