parallel_structure.spec.ts 7.47 KB
Newer Older
Dorchies David's avatar
Dorchies David committed
1
2
3
4
import { CalculatorType } from "../../src/compute-node";
import { Props } from "../../src/props";
import { Session } from "../../src/session";
import { CreateStructure } from "../../src/structure/factory_structure";
5
6
import { ParallelStructure } from "../../src/structure/parallel_structure";
import { ParallelStructureParams } from "../../src/structure/parallel_structure_params";
7
8
import { StructureKivi } from "../../src/structure/structure_kivi";
import { StructureKiviParams } from "../../src/structure/structure_kivi_params";
Dorchies David's avatar
Dorchies David committed
9
10
import { LoiDebit } from "../../src/structure/structure_props";
import { MessageCode } from "../../src/util/message";
11
import { Result } from "../../src/util/result";
Dorchies David's avatar
Dorchies David committed
12
import { precDigits } from "../test_config";
13
import { checkResult } from "../test_func";
14
import { CreateParalleleStructureTest, testParallelStructures } from "./functions";
Dorchies David's avatar
Dorchies David committed
15
import { CreateStructTest } from "./structure_test";
16

17
function createEnv() {
18
    const ps = new ParallelStructure(
19
20
21
22
23
24
        new ParallelStructureParams(30, 30, 15), // Q = 30, Z1 = 30, Z2 = 15
        false // debug
    );
    /*
     * Tests avec deux structures test identiques
     */
Dorchies David's avatar
Dorchies David committed
25
26
    ps.addChild(CreateStructTest());
    ps.addChild(CreateStructTest());
27
    return ps;
28
}
29

30
let pstruct: ParallelStructure;
31

32
function getVarCalc(pstructLoc: ParallelStructure, structIndex: number, sVarCalc: string): any {
33
34
35
36
37
38
39
40
41
    if (structIndex !== null) {
        return {
            uid: pstructLoc.structures[structIndex].uid,
            symbol: sVarCalc
        };
    } else {
        return sVarCalc;
    }
}
42

43
44
45
46
47
48
/**
 * Test sur ParallelStructure
 * @param sVarCalc Variable à calculer
 * @param rVcalc Valeur de référence à retrouver
 * @param Q Débit de chacune des structures (pour structures identiques uniquement)
 */
49
function itParallelStructure(structIndex: number, sVarCalc: string, rVcalc: number, Q?: number) {
50
    it(`${sVarCalc} should be ${rVcalc}`, () => {
51
52
        const VC1: any = getVarCalc(pstruct, structIndex, sVarCalc);
        checkResult(pstruct.Calc(VC1), rVcalc);
53
    });
Dorchies David's avatar
Dorchies David committed
54
    if (Q !== undefined) {
55
56
        const pstructLocal = createEnv();
        for (let i = 0; i < pstructLocal.structures.length; i++) {
57
            it(`Calc(${JSON.stringify(sVarCalc)}) ouvrage[${i}].Q should be ${Q}`, () => {
58
                const VC2: any = getVarCalc(pstruct, structIndex, sVarCalc);
59
                pstruct.Calc(VC2);
60
                const val = pstruct.structures[i].result.resultElement.values.Q;
61
                expect(val).toBeCloseTo(Q, Math.max(0, precDigits - 1));
Dorchies David's avatar
Dorchies David committed
62
            });
63
64
            it(`Calc(${JSON.stringify(sVarCalc)}) ExtraResult[ouvrage[${i}].Q_Mode] should be 0`, () => {
                const VC3 = getVarCalc(pstruct, structIndex, sVarCalc);
65
                pstruct.Calc(VC3);
66
                const val = pstruct.structures[i].result.resultElement.values.ENUM_StructureFlowMode;
67
                expect(val).toEqual(0);
Dorchies David's avatar
Dorchies David committed
68
            });
69
70
            it(`Calc(${JSON.stringify(sVarCalc)}) ExtraResult[ouvrage[${i}].Q_Regime] should be 0`, () => {
                const VC4 = getVarCalc(pstruct, structIndex, sVarCalc);
71
                pstruct.Calc(VC4);
72
                const val = pstruct.structures[i].result.resultElement.values.ENUM_StructureFlowRegime;
73
                expect(val).toEqual(0);
Dorchies David's avatar
Dorchies David committed
74
75
76
            });
        }
    }
77
}
78

Dorchies David's avatar
Dorchies David committed
79
describe("Class ParallelStructure: ", () => {
80
81
82
83
84
85
86
    describe("Calc()", () => {
        beforeEach(() => {
            pstruct = createEnv();
        });
        it("should return 1 result", () => {
            const p1 = createEnv();
            const res: Result = p1.Calc("Q");
87
            expect(p1.Calc("Q").resultElements.length).toEqual(1);
88
89
90
91
92
93
94
95
96
97
98
99
        });
        itParallelStructure(null, "Q", 30, 15);
        itParallelStructure(null, "Z1", 30, 15);
        itParallelStructure(null, "Z2", 15, 15);
        itParallelStructure(0, "ZDV", 0, 15);
        itParallelStructure(1, "ZDV", 0, 15);
        it("shoud return an error Q too high", () => {
            pstruct.prms.Q.v = 14;
            const res: Result = pstruct.Calc({
                uid: pstruct.structures[1].uid,
                symbol: "ZDV"
            });
100
            expect(res.log.messages[0].code).toBe(MessageCode.ERROR_STRUCTURE_Q_TROP_ELEVE);
101
        });
102
103
    });
    describe("testParallelStructures", () => {
104
105
106
        /*
        * Tests avec toutes les équations et toutes les variables (cf. jalhyd#38)
        */
107
108
109
110
111
        const parallelStructure: ParallelStructure = new ParallelStructure(
            new ParallelStructureParams(0, 102, 101.5), // Q = 0, Z1 = 102, Z2 = 101.5
            false // debug
        );
        testParallelStructures(CreateParalleleStructureTest(parallelStructure));
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    });

    describe("#94 Lois d'ouvrage: Erreur de calcul de ZDV", () => {
        it(`Calc(ZDV) should return 101.57`, () => {
            const ps3: ParallelStructure = new ParallelStructure(
                new ParallelStructureParams(0.5, 102, 101.5), // Q = 0.5, Z1 = 102, Z2 = 101.5
                false // debug
            );
            const structTestKivi3: StructureKivi = new StructureKivi(
                new StructureKiviParams(
                    0, 0, 0, 0,     // Q, ZDV, Z1, Z2
                    1,          // L
                    0.4,        // alpha
                    0.001,      // béta
                    95.7         // ZRAM : cote Radier Amont
                ),
                false
            );
            ps3.addChild(structTestKivi3);
Dorchies David's avatar
Dorchies David committed
131
132
            ps3.calculatedParam = ps3.structures[0].prms.ZDV;
            expect(ps3.CalcSerie().vCalc).toBeCloseTo(101.57, 2);
133
134
        });
    });
Dorchies David's avatar
Dorchies David committed
135
136
137
    describe("#116 Pas de calcul de Z2 sur loi dénoyée", () => {
        it("Calc(Z2) should return an error", () => {
            // Create Parallel Structure with default parameters
138
            const ps = Session.getInstance().createNub(
139
140
                new Props({ calcType: CalculatorType.ParallelStructure })
            ) as ParallelStructure;
Dorchies David's avatar
Dorchies David committed
141
            // Add Free flow weir
142
            ps.addChild(CreateStructure(LoiDebit.WeirFree));
Dorchies David's avatar
Dorchies David committed
143
            // Define Z2 to calculate
144
            ps.calculatedParam = ps.prms.Z2;
Dorchies David's avatar
Dorchies David committed
145

146
147
148
149
150
151
            expect(ps.CalcSerie().ok).toBeFalsy();
        });
    });
    describe("#38 Calcul de Z2 avec Q > Qmax", () => {
        it("Calc(Z2) should return error", () => {
            const ps = Session.getInstance().createNub(
152
153
                new Props({ calcType: CalculatorType.ParallelStructure })
            ) as ParallelStructure;
154
155
156
157
158
159
160
161
162
163
            // Add Free flow weir
            ps.addChild(CreateStructure(LoiDebit.GateCem88v));
            ps.prms.Q.singleValue = 4;
            ps.calculatedParam = ps.prms.Z2;
            ps.CalcSerie();
            const m = ps.result.resultElement.log.messages[0];
            expect(m.code).toEqual(MessageCode.ERROR_DICHO_TARGET_TOO_HIGH);
            expect(m.extraVar.variableSymbol).toEqual("Z2");
            expect(m.extraVar.targetSymbol).toEqual("Q");
            expect(m.extraVar.targetValue).toEqual(ps.prms.Q.currentValue);
Dorchies David's avatar
Dorchies David committed
164
165
        });
    });
166
167
168
169
170
171
172
173
174
175
176
177
    describe("#136 StructureGateCem88v.Calc(ZDV)", () => {
        it("Calc(ZDV) should return 100", () => {
            const ps = Session.getInstance().createNub(
                new Props({ calcType: CalculatorType.ParallelStructure })
            ) as ParallelStructure;
            ps.prms.Q.singleValue = 2.309;
            ps.addChild(CreateStructure(LoiDebit.GateCem88v));
            ps.structures[0].prms.ZDV.setCalculated();
            ps.structures[0].prms.ZDV.singleValue += 0.1;
            expect(ps.CalcSerie().vCalc).toBeCloseTo(100, 3);
        });
    });
178
});