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

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

34
let pstruct: ParallelStructure;
35

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

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

Dorchies David's avatar
Dorchies David committed
83
describe("Class ParallelStructure: ", () => {
84
85
86

    beforeAll(() => {
        // restore default precision to avoid side-effects from other tests
87
        SessionSettings.precision = 1e-7;
88
89
    });

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

    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
141
142
            ps3.calculatedParam = ps3.structures[0].prms.ZDV;
            expect(ps3.CalcSerie().vCalc).toBeCloseTo(101.57, 2);
143
144
        });
    });
Dorchies David's avatar
Dorchies David committed
145
146
147
    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
148
            const ps = Session.getInstance().createNub(
149
150
                new Props({ calcType: CalculatorType.ParallelStructure })
            ) as ParallelStructure;
Dorchies David's avatar
Dorchies David committed
151
            // Add Free flow weir
152
            ps.addChild(CreateStructure(LoiDebit.WeirFree));
Dorchies David's avatar
Dorchies David committed
153
            // Define Z2 to calculate
154
            ps.calculatedParam = ps.prms.Z2;
Dorchies David's avatar
Dorchies David committed
155

156
157
158
159
160
161
            expect(ps.CalcSerie().ok).toBeFalsy();
        });
    });
    describe("#38 Calcul de Z2 avec Q > Qmax", () => {
        it("Calc(Z2) should return error", () => {
            const ps = Session.getInstance().createNub(
162
163
                new Props({ calcType: CalculatorType.ParallelStructure })
            ) as ParallelStructure;
164
165
166
167
168
169
170
171
172
173
            // 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
174
175
        });
    });
176
177
178
179
180
181
182
183
184
185
186
187
    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
188
    describe("Strucure logs should be propagated to parent Result - ", () => {
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
        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);
        });
    });
222
});
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

describe("switching between LoiDebit should load a new default Cd", () => {

    it("test 1", () => {
        const ps = new ParallelStructure(new ParallelStructureParams(1, 102, 100));
        const s = CreateStructure(LoiDebit.WeirSubmergedLarinier, ps);
        expect((s.prms as RectangularStructureParams).CdWSL.singleValue).toBe(0.65);
        ps.addChild(s);
        expect((ps.getChildren()[0] as RectangularStructure).prms.CdWSL.singleValue).toBe(0.65);
        const s2 = CreateStructure(LoiDebit.WeirSubmerged, ps);
        expect((s2.prms as RectangularStructureParams).CdWS.singleValue).toBe(0.9);
        ps.replaceChildInplace(s, s2);
        expect((ps.getChildren()[0] as RectangularStructure).prms.CdWS.singleValue).toBe(0.9);
    });

    it("test 2", () => {
        const ps = new ParallelStructure(new ParallelStructureParams(1, 102, 100));
        const s = CreateStructure(LoiDebit.WeirSubmerged, ps);
        expect((s.prms as RectangularStructureParams).CdWS.singleValue).toBe(0.9);
        ps.addChild(s);
        expect((ps.getChildren()[0] as RectangularStructure).prms.CdWS.singleValue).toBe(0.9);
        const s2 = CreateStructure(LoiDebit.WeirSubmergedLarinier, ps);
        expect((s2.prms as RectangularStructureParams).CdWSL.singleValue).toBe(0.65);
        ps.replaceChildInplace(s, s2);
        expect((ps.getChildren()[0] as RectangularStructure).prms.CdWSL.singleValue).toBe(0.65);
    });

});