From 0ab3d3a3d259e60362fbf739c566dbbcd852bab1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Wed, 2 Nov 2022 15:30:26 +0100
Subject: [PATCH] refactor: PreBarrage: do not store iteration count in
 constructor

refs #330
---
 src/prebarrage/pre_barrage.ts | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/prebarrage/pre_barrage.ts b/src/prebarrage/pre_barrage.ts
index 49a40966..79719f20 100644
--- a/src/prebarrage/pre_barrage.ts
+++ b/src/prebarrage/pre_barrage.ts
@@ -15,6 +15,7 @@ export class PreBarrage extends Nub {
     /** Liste des cloisons amont */
     private _cloisonsAmont: PbCloison[];
 
+    // public pour pouvoir le modifier depuis les tests
     public maxIterations: number;
 
     /** Pointeurs vers les bassins dans this.children */
@@ -31,7 +32,6 @@ export class PreBarrage extends Nub {
         this.setCalculatorType(CalculatorType.PreBarrage);
         this._cloisonsAmont = [];
         this._bassins = [];
-        this.maxIterations = SessionSettings.maxIterations;
         //this._intlType = "Cloison";
     }
 
@@ -359,6 +359,11 @@ export class PreBarrage extends Nub {
      * @param sVarCalc ignoré: Z1 uniquement
      */
     public Equation(sVarCalc: string): Result {
+        const maxIter = this.maxIterations !== undefined ? this.maxIterations : SessionSettings.maxIterations;
+        if (maxIter === undefined || maxIter < 1) {
+            throw new Error("invalid iteration count");
+        }
+
         // Initialisation des cotes sur les bassins et la CL amont
         if (this.isMeshed()) {
             for(const b of this.bassins) {
@@ -385,13 +390,13 @@ export class PreBarrage extends Nub {
                 }
             }
         }
-        let iter: number = this.maxIterations;
+        let iter: number = maxIter;
         let bConverged;
         let z1stat: IPbBassinZstat;
         // const tZ1: IPbBassinZstat[] = [];
         // const tQ: number[][] = [];
         while (iter-- > 0) {
-            this._relax = Math.pow(iter / this.maxIterations, 3) * 0.25 + 0.05;
+            this._relax = Math.pow(iter / maxIter, 3) * 0.25 + 0.05;
             this.debug(`***** Iteration n°${iter} relax=${this._relax} *****`);
             bConverged = true;
             if (this.isMeshed()) {
-- 
GitLab