parallel_structure.spec.ts 9.3 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
import { LoiDebit } from "../../src/structure/structure_props";
10
import { StructureRectangularOrificeFree } from "../../src/structure/structure_rectangular_orifice_free";
Dorchies David's avatar
Dorchies David committed
11
import { MessageCode } from "../../src/util/message";
12
import { Result } from "../../src/util/result";
Dorchies David's avatar
Dorchies David committed
13
import { precDigits } from "../test_config";
14
import { checkResult } from "../test_func";
15
import { CreateParalleleStructureTest, testParallelStructures } from "./functions";
Dorchies David's avatar
Dorchies David committed
16
import { CreateStructTest } from "./structure_test";
17

18
function createEnv() {
19
    const ps = new ParallelStructure(
20
21
22
23
24
25
        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
26
27
    ps.addChild(CreateStructTest());
    ps.addChild(CreateStructTest());
28
    return ps;
29
}
30

31
let pstruct: ParallelStructure;
32

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

44
45
46
47
48
49
/**
 * 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)
 */
50
function itParallelStructure(structIndex: number, sVarCalc: string, rVcalc: number, Q?: number) {
51
    it(`${sVarCalc} should be ${rVcalc}`, () => {
52
53
        const VC1: any = getVarCalc(pstruct, structIndex, sVarCalc);
        checkResult(pstruct.Calc(VC1), rVcalc);
54
    });
Dorchies David's avatar
Dorchies David committed
55
    if (Q !== undefined) {
56
57
        const pstructLocal = createEnv();
        for (let i = 0; i < pstructLocal.structures.length; i++) {
58
            it(`Calc(${JSON.stringify(sVarCalc)}) ouvrage[${i}].Q should be ${Q}`, () => {
59
                const VC2: any = getVarCalc(pstruct, structIndex, sVarCalc);
60
                pstruct.Calc(VC2);
61
                const val = pstruct.structures[i].result.resultElement.values.Q;
62
                expect(val).toBeCloseTo(Q, Math.max(0, precDigits - 1));
Dorchies David's avatar
Dorchies David committed
63
            });
64
65
            it(`Calc(${JSON.stringify(sVarCalc)}) ExtraResult[ouvrage[${i}].Q_Mode] should be 0`, () => {
                const VC3 = getVarCalc(pstruct, structIndex, sVarCalc);
66
                pstruct.Calc(VC3);
67
                const val = pstruct.structures[i].result.resultElement.values.ENUM_StructureFlowMode;
68
                expect(val).toEqual(0);
Dorchies David's avatar
Dorchies David committed
69
            });
70
71
            it(`Calc(${JSON.stringify(sVarCalc)}) ExtraResult[ouvrage[${i}].Q_Regime] should be 0`, () => {
                const VC4 = getVarCalc(pstruct, structIndex, sVarCalc);
72
                pstruct.Calc(VC4);
73
                const val = pstruct.structures[i].result.resultElement.values.ENUM_StructureFlowRegime;
74
                expect(val).toEqual(0);
Dorchies David's avatar
Dorchies David committed
75
76
77
            });
        }
    }
78
}
79

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

    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
132
133
            ps3.calculatedParam = ps3.structures[0].prms.ZDV;
            expect(ps3.CalcSerie().vCalc).toBeCloseTo(101.57, 2);
134
135
        });
    });
Dorchies David's avatar
Dorchies David committed
136
137
138
    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
139
            const ps = Session.getInstance().createNub(
140
141
                new Props({ calcType: CalculatorType.ParallelStructure })
            ) as ParallelStructure;
Dorchies David's avatar
Dorchies David committed
142
            // Add Free flow weir
143
            ps.addChild(CreateStructure(LoiDebit.WeirFree));
Dorchies David's avatar
Dorchies David committed
144
            // Define Z2 to calculate
145
            ps.calculatedParam = ps.prms.Z2;
Dorchies David's avatar
Dorchies David committed
146

147
148
149
150
151
152
            expect(ps.CalcSerie().ok).toBeFalsy();
        });
    });
    describe("#38 Calcul de Z2 avec Q > Qmax", () => {
        it("Calc(Z2) should return error", () => {
            const ps = Session.getInstance().createNub(
153
154
                new Props({ calcType: CalculatorType.ParallelStructure })
            ) as ParallelStructure;
155
156
157
158
159
160
161
162
163
164
            // 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
165
166
        });
    });
167
168
169
170
171
172
173
174
175
176
177
178
    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);
        });
    });
Mathias Chouet's avatar
Oops    
Mathias Chouet committed
179
    describe("Strucure logs should be propagated to parent Result - ", () => {
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
        it("example with StructureRectangularOrificeFree, h2 > 0, calculating Q", () => {
            const ps = Session.getInstance().createNub(
                new Props({ calcType: CalculatorType.ParallelStructure })
            ) as ParallelStructure;
            ps.prms.Q.singleValue = 2.309;
            ps.addChild(CreateStructure(LoiDebit.RectangularOrificeFree));
            ps.prms.Z1.singleValue = 105;
            ps.prms.Z2.singleValue = 102;
            ps.structures[0].prms.ZDV.singleValue = 101.5;
            const res = ps.CalcSerie().resultElement;
            expect(res.log.messages.length).toBe(1);
            expect(
                res.log.messages[0].code
            ).toBe(MessageCode.WARNING_DOWNSTREAM_ELEVATION_POSSIBLE_SUBMERSION);
        });
        it("example with StructureRectangularOrificeFree, h2 > 0, calculating something other than Q", () => {
            const ps = Session.getInstance().createNub(
                new Props({ calcType: CalculatorType.ParallelStructure })
            ) as ParallelStructure;
            ps.prms.Q.singleValue = 2.309;
            const c = CreateStructure(LoiDebit.RectangularOrificeFree) as StructureRectangularOrificeFree;
            ps.addChild(c);
            ps.prms.Z1.singleValue = 105;
            ps.prms.Z2.singleValue = 102;
            ps.structures[0].prms.ZDV.singleValue = 101.5;
            ps.calculatedParam = c.prms.L;
            const res = ps.CalcSerie().resultElement;
            expect(res.log.messages.length).toBe(1);
            expect(
                res.log.messages[0].code
            ).toBe(MessageCode.WARNING_DOWNSTREAM_ELEVATION_POSSIBLE_SUBMERSION);
        });
    });
213
});