diff --git a/spec/mock_jasmine.ts b/spec/mock_jasmine.ts index fccc570b35876578a2c996eb441cca097c20574f..61bae1f503b01a63743515782823e7b9f1dceefb 100644 --- a/spec/mock_jasmine.ts +++ b/spec/mock_jasmine.ts @@ -34,7 +34,7 @@ export function it(sTxt: string, fFun: () => void) { * @param fFun Fonction de la suite de test */ export function xit(sTxt: string, fFun: () => void) { - console.log(sTxt + " ignored ***"); + console.warn("*** " + sTxt + " ignored ***"); } /** @@ -58,7 +58,7 @@ class Expect { const pow = Math.pow(10, precision + 1); const delta = Math.abs(expected - this.actual); const maxDelta = Math.pow(10, -precision) / 2; - if (Math.round(delta * pow) / pow > maxDelta) { + if (typeof this.actual !== "number" || Math.round(delta * pow) / pow > maxDelta) { console.error("Expected " + this.actual + " to be close to " + expected + " with precision " + precision); } return this; diff --git a/spec/structure/parallel_structure.spec.ts b/spec/structure/parallel_structure.spec.ts index 980a852db3918d0030726d15898a54f9e68acf20..79f77158e2b5ca821957f3d12f0f1a9f54fbe60e 100644 --- a/spec/structure/parallel_structure.spec.ts +++ b/spec/structure/parallel_structure.spec.ts @@ -14,6 +14,7 @@ import { Structure } from "../../src/structure/structure"; import { Describer } from "../../src/util/describer"; import { EnumEx } from "../../src/util/enum"; import { Result } from "../../src/util/result"; +import { precDigits } from "../test_config"; import { checkResult } from "../test_func"; import { structTest } from "./structure_test"; @@ -31,6 +32,10 @@ pstruct.addStructure(structTest); describe("Class ParallelStructure: ", () => { describe("Calc()", () => { + it("should return 1 result", () => { + const res: Result = pstruct.Calc("Q"); + expect(pstruct.Calc("Q").nbResults).toEqual(1); + }); itParallelStructure("Q", 30, 15); itParallelStructure("Z1", 30, 15); itParallelStructure("Z2", 15, 15); @@ -45,14 +50,28 @@ describe("Class ParallelStructure: ", () => { */ function itParallelStructure(sVarCalc: string, rVcalc: number, Q?: number) { it(`${sVarCalc} should be ${rVcalc}`, () => { - const res: Result = pstruct.Calc(sVarCalc); - checkResult(res, rVcalc); - if (Q !== undefined) { - for (let i = 1; i < res.nbResults; i++) { - checkResult(res.extractResult(i), Q); - } - } + checkResult(pstruct.Calc(sVarCalc), rVcalc); }); + if (Q !== undefined) { + for (let i = 0; i < pstruct.structures.length; i++) { + it(`ExtraResult[${i}.Q] should be ${Q}`, () => { + expect( + pstruct.Calc(sVarCalc).result.extraResults[`${i}.Q`] + ).toBeCloseTo(Q, Math.max(0, precDigits - 1)); + }); + it(`ExtraResult[${i}.Q_Mode] should be 0`, () => { + expect( + pstruct.Calc(sVarCalc).result.extraResults[`${i}.Q_Mode`] + ).toEqual(0); + }); + it(`ExtraResult[${i}.Q_Regime] should be 0`, () => { + expect( + pstruct.Calc(sVarCalc).result.extraResults[`${i}.Q_Regime`] + ).toEqual(0); + }); + + } + } } /* diff --git a/spec/test_func.ts b/spec/test_func.ts index dbf9a5aff6f4e278a637280856dd477cbf636a84..8e5de0e65e1c5ee215e638d3f706b4affa39e396 100644 --- a/spec/test_func.ts +++ b/spec/test_func.ts @@ -8,8 +8,7 @@ import { cLog } from "../src/util/log"; import { Message, MessageCode } from "../src/util/message"; import { Result } from "../src/util/result"; -import { precDist } from "./test_config"; -import { precDigits } from "./test_config"; +import { precDigits, precDist } from "./test_config"; /** * Compare deux valeurs réelles à un epsilon près diff --git a/src/structure/parallel_structure.ts b/src/structure/parallel_structure.ts index afd91b8b056ef79425614182312f0aed2aec8ae0..da9d4fdb5fdee3c8cc38ae243b85cbfed16a1262 100644 --- a/src/structure/parallel_structure.ts +++ b/src/structure/parallel_structure.ts @@ -5,6 +5,14 @@ import { Result } from "../util/result"; import { ParallelStructureParams } from "./parallel_structure_params"; import { Structure } from "./structure"; +/** + * Interface pour mémoriser le n° d'ouvrage et le paramètre à calculer + */ +interface IStructureVarCalc { + index: number; + prm: string; +} + /** * Calcul de une ou plusieurs structures hydrauliques en parallèles * reliées par les cotes amont et aval et dont le débit est égal à la @@ -80,17 +88,17 @@ export class ParallelStructure extends Nub { ); } } - const calcRes: Result = new Result(); + const calcRes: Result = new Result(0); let qTot: number = 0; for (let i = 0; i < this.structures.length; i++) { if (i !== iExcept) { const res: Result = this.structures[i].Calc("Q"); - calcRes.addResult(res.result); + calcRes.result.AddResultElementToExtra(res.result, `${i}.Q`); qTot += res.vCalc; } } - // Insert le débit total en premier résultat - calcRes.insertResult(new Result(qTot).result, 0); + // Assigne le débit total dans le résultat + calcRes.result.vCalc = qTot; return calcRes; } @@ -102,15 +110,34 @@ export class ParallelStructure extends Nub { * @param rPrec Précision attendue */ public Calc(sVarCalc: string, rInit?: number, rPrec: number = 0.001): Result { + let res: Result; switch (sVarCalc) { case "Z1": case "Z2": case "Q": - return super.Calc(sVarCalc, rInit, rPrec); + res = super.Calc(sVarCalc, rInit, rPrec); + if (res.ok) { + this.prms.map[sVarCalc].v = res.vCalc; + } + break; default: // Pour les caractéristiques des ouvrages - return this.CalcStructPrm(sVarCalc, rInit, rPrec); + const sVC = this.getStructureVarCalc(sVarCalc); + res = this.CalcStructPrm(sVC, rInit, rPrec); + if (res.ok) { + this.structures[sVC.index].prms.map[sVC.prm].v = res.vCalc; + } } + if (res.ok) { + // Recalcul du débit total pour récupérer les résultats des ouvrages dans les résultats complémentaires + const resQtot: Result = this.CalcQ(); + for (const extraResKey in resQtot.extraResults) { + if (resQtot.extraResults.hasOwnProperty(extraResKey)) { + res.result.addExtraResult(extraResKey, resQtot.extraResults[extraResKey]); + } + } + } + return res; } /** @@ -134,22 +161,28 @@ export class ParallelStructure extends Nub { } /** - * Calcul du paramètre d'un des ouvrages en parallèle + * Renvoie le n° de structure et le paramètre à calculer * @param sVarCalc Nom du paramètre à calculer : "n.X" avec "n" l'index de l'ouvrage et "X" son paramètre - * @param rInit Valeur initiale - * @param rPrec Précision attendue */ - private CalcStructPrm(sVarCalc: string, rInit?: number, rPrec: number = 0.001): Result { - // Détection de la structure où calculer le paramètre + private getStructureVarCalc(sVarCalc: string): IStructureVarCalc { let sIndex: string; let sPrm: string; [sIndex, sPrm] = sVarCalc.split("."); - const index = parseInt(sIndex, 10); + const i = parseInt(sIndex, 10); + return { index: i, prm: sPrm }; + } + /** + * Calcul du paramètre d'un des ouvrages en parallèle + * @param sVC Index de l'ouvrage et paramètre à calculer + * @param rInit Valeur initiale + * @param rPrec Précision attendue + */ + private CalcStructPrm(sVC: IStructureVarCalc, rInit?: number, rPrec: number = 0.001): Result { // Le débit restant sur la structure en calcul est : - this.structures[index].prms.Q.v = this.prms.Q.v - this.CalcQ(index).vCalc; + this.structures[sVC.index].prms.Q.v = this.prms.Q.v - this.CalcQ(sVC.index).vCalc; // Calcul du paramètre de la structure en calcul - return this.structures[index].Calc(sPrm, rInit, rPrec); + return this.structures[sVC.index].Calc(sVC.prm, rInit, rPrec); } }