From 24781a5018d5a522da4ffb8923cb7f1179291cdd Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Tue, 29 Oct 2019 15:47:23 +0100
Subject: [PATCH] Fix jalhyd#163 - ParallelStructure results when calc fails

---
 spec/structure/ps_jalhyd163.spec.ts | 49 +++++++++++++++++++++++++++++
 spec/structure/structure.spec.ts    |  5 +--
 src/structure/parallel_structure.ts | 15 +++++----
 src/structure/structure.ts          |  7 +++--
 4 files changed, 64 insertions(+), 12 deletions(-)
 create mode 100644 spec/structure/ps_jalhyd163.spec.ts

diff --git a/spec/structure/ps_jalhyd163.spec.ts b/spec/structure/ps_jalhyd163.spec.ts
new file mode 100644
index 00000000..4702e955
--- /dev/null
+++ b/spec/structure/ps_jalhyd163.spec.ts
@@ -0,0 +1,49 @@
+import { ParallelStructure, ParallelStructureParams, Session } from "../../src/index";
+import { StructureOrificeFree } from "../../src/structure/structure_orifice_free";
+import { StructureOrificeFreeParams } from "../../src/structure/structure_orifice_free_params";
+
+describe("ParallelStructures, jalhyd#163, calculated variables present in the results − ", () => {
+
+    it("when Z1 is high enough and Zco calc succeeds, 10 variables should be present", () => {
+        // tslint:disable-next-line:max-line-length
+        const sess = `{"header":{"source":"jalhyd","format_version":"1.3","created":"2019-10-29T14:06:53.775Z"},"settings":{"precision":0.0001,"maxIterations":100,"displayPrecision":3},"documentation":"","session":[{"uid":"c292bn","props":{"calcType":"ParallelStructure"},"meta":{"title":"Ouvrages 1"},"children":[{"uid":"bGxmYT","props":{"calcType":"Structure","structureType":"Orifice","loiDebit":"OrificeFree"},"children":[],"parameters":[{"symbol":"S","mode":"SINGLE","value":0.1},{"symbol":"CdO","mode":"SINGLE","value":0.7},{"symbol":"Zco","mode":"CALCUL"}]}],"parameters":[{"symbol":"Q","mode":"SINGLE","value":0.5},{"symbol":"Z1","mode":"SINGLE","value":105},{"symbol":"Z2","mode":"SINGLE","value":100}]}]}`;
+        Session.getInstance().clear();
+        Session.getInstance().unserialise(sess);
+        const nub = Session.getInstance().findNubByUid("c292bn");
+        nub.CalcSerie();
+        const vN = nub.result.resultElement.values;
+        const vS = nub.getChildren()[0].result.resultElement.values;
+        const vSK = Object.keys(vS);
+        vSK.sort();
+        const vNK = Object.keys(vN);
+        vNK.sort();
+        expect(vNK.length).toBe(1);
+        expect(vNK).toEqual([ "Zco" ]);
+        expect(vSK.length).toBe(5);
+        expect(vSK).toEqual(
+            [ "ENUM_StructureFlowMode", "ENUM_StructureFlowRegime", "ENUM_StructureJetType", "Q", "Zco" ]
+        );
+    });
+
+    it("when Z1 is too low and Zco calc fails, 10 variables should be present", () => {
+        // tslint:disable-next-line:max-line-length
+        const sess = `{"header":{"source":"jalhyd","format_version":"1.3","created":"2019-10-29T14:06:53.775Z"},"settings":{"precision":0.0001,"maxIterations":100,"displayPrecision":3},"documentation":"","session":[{"uid":"c292bn","props":{"calcType":"ParallelStructure"},"meta":{"title":"Ouvrages 1"},"children":[{"uid":"bGxmYT","props":{"calcType":"Structure","structureType":"Orifice","loiDebit":"OrificeFree"},"children":[],"parameters":[{"symbol":"S","mode":"SINGLE","value":0.1},{"symbol":"CdO","mode":"SINGLE","value":0.7},{"symbol":"Zco","mode":"CALCUL"}]}],"parameters":[{"symbol":"Q","mode":"SINGLE","value":0.5},{"symbol":"Z1","mode":"SINGLE","value":90},{"symbol":"Z2","mode":"SINGLE","value":100}]}]}`;
+        Session.getInstance().clear();
+        Session.getInstance().unserialise(sess);
+        const nub = Session.getInstance().findNubByUid("c292bn");
+        nub.CalcSerie();
+        const vN = nub.result.resultElement.values;
+        const vS = nub.getChildren()[0].result.resultElement.values;
+        const vSK = Object.keys(vS);
+        vSK.sort();
+        const vNK = Object.keys(vN);
+        vNK.sort();
+        expect(vNK.length).toBe(1);
+        expect(vNK).toEqual([ "Zco" ]);
+        expect(vSK.length).toBe(5);
+        expect(vSK).toEqual(
+            [ "ENUM_StructureFlowMode", "ENUM_StructureFlowRegime", "ENUM_StructureJetType", "Q", "Zco" ]
+        );
+    });
+
+});
diff --git a/spec/structure/structure.spec.ts b/spec/structure/structure.spec.ts
index 5f8be3b5..ec004db0 100644
--- a/spec/structure/structure.spec.ts
+++ b/spec/structure/structure.spec.ts
@@ -1,4 +1,4 @@
-import { StructureFlowMode, StructureFlowRegime } from "../../src/structure/structure";
+import { StructureFlowMode, StructureFlowRegime, StructureJetType } from "../../src/structure/structure";
 import { checkResult } from "../test_func";
 import { CreateStructTest, StructureTest } from "./structure_test";
 
@@ -43,7 +43,8 @@ describe("Class Structure: ", () => {
     describe("Calc()", () => {
         const flagsNull = {
             ENUM_StructureFlowMode: StructureFlowMode.NULL,
-            ENUM_StructureFlowRegime: StructureFlowRegime.NULL
+            ENUM_StructureFlowRegime: StructureFlowRegime.NULL,
+            ENUM_StructureJetType: StructureJetType.SO
         };
         it("Z1=Z2 => Q=0", () => {
             structTest.prms.Z2.v = structTest.prms.Z1.v;
diff --git a/src/structure/parallel_structure.ts b/src/structure/parallel_structure.ts
index 766c2481..3fd67012 100644
--- a/src/structure/parallel_structure.ts
+++ b/src/structure/parallel_structure.ts
@@ -127,14 +127,13 @@ export class ParallelStructure extends Nub {
                 // Pour les caractéristiques des ouvrages
                 const structureIndex = this.getIndexForChild(sVarCalc.uid);
                 const r = this.CalcStructPrm(structureIndex, sVarCalc.symbol, rInit);
-                if (r.ok) {
-                    this.result.symbol = r.symbol;
-                    this.result.vCalc = r.vCalc;
-                    // merge logs
-                    this.result.log.addLog(r.log);
-                } else {
-                    this.currentResult = r;
-                }
+                // whether r is .ok() or not, just copy vCalc and logs to avoid
+                // this._result being polluted by structure.flagsNull
+                this.result.symbol = r.symbol;
+                this.result.vCalc = r.vCalc;
+                // merge logs
+                this.result.log.addLog(r.log);
+                this.result.globalLog.addLog(r.globalLog);
         }
 
         return this.result;
diff --git a/src/structure/structure.ts b/src/structure/structure.ts
index 25930e83..84a4b8c2 100644
--- a/src/structure/structure.ts
+++ b/src/structure/structure.ts
@@ -210,8 +210,11 @@ export abstract class Structure extends Nub {
         this.prms.update_h1h2();
 
         // Gestion du débit nul
-        const flagsNull = { ENUM_StructureFlowMode: StructureFlowMode.NULL,
-                            ENUM_StructureFlowRegime: StructureFlowRegime.NULL };
+        const flagsNull = {
+            ENUM_StructureFlowMode: StructureFlowMode.NULL,
+            ENUM_StructureFlowRegime: StructureFlowRegime.NULL,
+            ENUM_StructureJetType: StructureJetType.SO
+        };
         if (sVarCalc === "Q") {
             if (this.prms.h1.v <= 0 || Math.abs(this.prms.h1.v - this.prms.h2.v) < 1E-20 || this.W <= 1E-20) {
                 return new Result(0, this, flagsNull);
-- 
GitLab