parallel_structure.spec.ts 7.74 KB
Newer Older
1
2
3
4
5
6
/**
 * IMPORTANT !
 * Décommenter temporairement la ligne suivante (import { } from "./mock_jasmine")
 * Pour exécuter ce code dans le débugger.
 * Faire de même avec le fichier test_func.ts
 */
Grand Francois's avatar
Grand Francois committed
7
// import { describe, expect, it, xdescribe, xit } from "../mock_jasmine";
8

Dorchies David's avatar
Dorchies David committed
9
10
11
12
import { CalculatorType } from "../../src/compute-node";
import { Props } from "../../src/props";
import { Session } from "../../src/session";
import { CreateStructure } from "../../src/structure/factory_structure";
13
14
import { ParallelStructure } from "../../src/structure/parallel_structure";
import { ParallelStructureParams } from "../../src/structure/parallel_structure_params";
15
16
import { StructureKivi } from "../../src/structure/structure_kivi";
import { StructureKiviParams } from "../../src/structure/structure_kivi_params";
Dorchies David's avatar
Dorchies David committed
17
18
import { LoiDebit } from "../../src/structure/structure_props";
import { MessageCode } from "../../src/util/message";
19
import { Result } from "../../src/util/result";
Dorchies David's avatar
Dorchies David committed
20
import { precDigits } from "../test_config";
21
import { checkResult } from "../test_func";
22
import { CreateParalleleStructureTest, testParallelStructures } from "./functions";
Dorchies David's avatar
Dorchies David committed
23
import { CreateStructTest } from "./structure_test";
24

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

38
let pstruct: ParallelStructure;
39

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

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

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

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

154
155
156
157
158
159
            expect(ps.CalcSerie().ok).toBeFalsy();
        });
    });
    describe("#38 Calcul de Z2 avec Q > Qmax", () => {
        it("Calc(Z2) should return error", () => {
            const ps = Session.getInstance().createNub(
160
161
                new Props({ calcType: CalculatorType.ParallelStructure })
            ) as ParallelStructure;
162
163
164
165
166
167
168
169
170
171
            // 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
172
173
        });
    });
174
175
176
177
178
179
180
181
182
183
184
185
    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);
        });
    });
186
});