From 36dc4faf144b68c8d851a12db808c8f28da09e98 Mon Sep 17 00:00:00 2001
From: "francois.grand" <francois.grand@irstea.fr>
Date: Mon, 16 Apr 2018 16:43:18 +0200
Subject: [PATCH] dichotomie : ajout du nombre maxi d'iterations pour la
 recherche dichotomique

---
 src/dichotomie.ts | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/dichotomie.ts b/src/dichotomie.ts
index 62876caf..8ed64a1c 100644
--- a/src/dichotomie.ts
+++ b/src/dichotomie.ts
@@ -118,6 +118,13 @@ export class Dichotomie extends Debug {
      */
     private _startIntervalMaxSteps = 100;
 
+    /**
+     * nombre d'itérations maxi pour la recherche dichotomique
+     */
+    private _maxIterations: number = 100;
+
+    private _currentIterations: number;
+
     /**
      * nom du paramètre calculé analytiquement par Equation()
      */
@@ -139,6 +146,10 @@ export class Dichotomie extends Debug {
         }
     }
 
+    public set maxIterations(n: number) {
+        this._maxIterations = Math.max(n, 1);
+    }
+
     /**
      * Valeur inconnue à rechercher
      */
@@ -188,6 +199,7 @@ export class Dichotomie extends Debug {
         const r = this.getStartInterval(rTarget, rInit);
         if (r.ok) {
             const interv: SearchInterval = r.intSearch;
+            this._currentIterations = 0;
             // Dichotomie
             return this.dichoSearch(rTarget, rTol, interv.min, interv.max, interv.targetLower, interv.targetUpper);
         } else {
@@ -371,6 +383,13 @@ export class Dichotomie extends Debug {
      */
     // tslint:disable-next-line:variable-name
     private dichoSearch(Ytarg: number, rTol: number, Xmin: number, Xmax: number, Ymin: number, Ymax: number): Result {
+        this._currentIterations++;
+        if (this._currentIterations > this._maxIterations) {
+            const r = new Result();
+            r.globalLog.add(new Message(MessageCode.ERROR_DICHO_CONVERGE));
+            return r;
+        }
+
         this.debug("dichoSearch : yTarget " + Ytarg + " X " + Xmin + "->" + Xmax + " tol " + rTol);
 
         // tslint:disable-next-line:variable-name
-- 
GitLab