code + log clean

This commit is contained in:
Patrick Haßel 2025-02-03 15:57:04 +01:00
parent 8f33a16843
commit eba8d51e3e
3 changed files with 26 additions and 21 deletions

View File

@ -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);
}
}

View File

@ -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 {

View File

@ -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) {