Commit 5ea96a6e authored by Mathias Chouet's avatar Mathias Chouet 🍝
Browse files

Fix nghyd#451 - merge PreBarrage duplicate walls at calculate time

parent ee83e0f5
This diff is collapsed.
......@@ -228,12 +228,90 @@ export class PreBarrage extends Nub {
this.updatePointers();
}
/**
* Detect duplicate walls between same basins pair, and merges
* them into one wall containing all the previous walls' structures.
*/
private detectAndMergeDuplicateWalls() {
// browse all upstream basins
for (const b of this._bassins) {
// for each upstream basin, group walls by downstream basin
this.mergeDuplicateWalls(b);
}
// walls between upstream and another basin
for (const b of this.cloisonsAmont.map((ca) => ca.bassinAval)) {
if (b !== undefined) {
this.mergeDuplicateWalls(b, false);
}
}
// walls between river upstream and downstream
const udWalls: PbCloison[] = [];
for (const w of this._children) {
if (w instanceof PbCloison) {
if (w.bassinAmont === undefined && w.bassinAval === undefined) {
udWalls.push(w);
}
}
}
this.mergeStructuresInFirstWall(udWalls);
}
/**
* Finds all walls connected to the given basin's upstream (is upstream is true) or
* downstream. Among those walls, finds those who have the same basin at their
* opposite connection and merges them into one wall.
* @param b
* @param upstream
*/
private mergeDuplicateWalls(b: PbBassin, upstream: boolean = true) {
// group walls attached to b by the other basin they are attached to
const wallsByOtherBasin: { [key: string]: PbCloison[] } = {};
const linkedWalls = upstream ? b.cloisonsAval : b.cloisonsAmont;
for (const w of linkedWalls) {
let dbId = "-"; // river upstream / downstream
const otherBasin = upstream ? w.bassinAval : w.bassinAmont;
if (otherBasin !== undefined) {
dbId = otherBasin.uid;
}
if (!Object.keys(wallsByOtherBasin).includes(dbId)) {
wallsByOtherBasin[dbId] = [];
}
wallsByOtherBasin[dbId].push(w);
}
// if multiple walls attached to b share the same other basin, merge them
for (const dbUid of Object.keys(wallsByOtherBasin)) {
const dbWalls = wallsByOtherBasin[dbUid];
this.mergeStructuresInFirstWall(dbWalls);
}
}
/**
* Given a list of walls, moves all structures belonging to
* structures #2+ into structure #1, and removes structures
* #2+ from the current PreBarrage
* @param walls
*/
private mergeStructuresInFirstWall(walls: PbCloison[]) {
if (walls.length > 1) {
// move all structures of walls > 1 to 1st wall
for (let i = 1; i < walls.length; i++) {
const dbw = walls[i];
for (const s of dbw.structures) {
walls[0].addChild(s);
}
// delete "merged" wall
this.deleteChild(dbw.findPositionInParent());
}
}
}
/**
* Effectue une série de calculs sur un paramètre; déclenche le calcul en chaîne
* des modules en amont si nécessaire
* @param rInit solution approximative du paramètre
*/
public CalcSerie(rInit?: number): Result {
this.detectAndMergeDuplicateWalls();
this._precision = Math.max(5E-4, SessionSettings.precision);
return super.CalcSerie(rInit);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment