Commit 969fe180 authored by Grand Francois's avatar Grand Francois
Browse files

Merge branch 'master' of gitlab-ssh.irstea.fr:david.dorchies/jalhyd into conduitedistributrice

Showing with 58 additions and 40 deletions
+58 -40
...@@ -4,11 +4,10 @@ import { nub, res } from "./nubtest"; ...@@ -4,11 +4,10 @@ import { nub, res } from "./nubtest";
describe('Class Nub: ', () => { describe('Class Nub: ', () => {
beforeEach(() => { beforeEach(() => {
nub.sVarsEq = ["C"];
res.vCalc = 3; res.vCalc = 3;
}); });
describe('Calc(): ', () => { describe('Calc(): ', () => {
it('should return a result equal to 3', () => { it('should return a result.vCalc equal to 3', () => {
expect(nub.Calc("C")).toEqual(res); expect(nub.Calc("C")).toEqual(res);
}); });
}); });
......
/// <reference path="../node_modules/@types/jasmine/index.d.ts" />
import { nub, res } from "./nubtest";
import { Dichotomie } from "../src/dichotomie"
let dicho: Dichotomie = new Dichotomie(nub, "A");
describe('Class Dichotomie: ', () => {
describe('Dichotomie(3, 1E-6, 0): ', () => {
it('should return a result close to 1', () => {
expect(dicho.Dichotomie(3, 1E-6, 0).vCalc).toBeCloseTo(1,1E-6);
});
});
});
import { Nub, Result } from "../src/base"; import { Nub, Result, IParametres } from "../src/base";
export class NubTest extends Nub { export class NubTest extends Nub {
constructor(v: IParametres) {
super(v);
this.sVarsEq = ["C"];
}
Equation(): Result { Equation(): Result {
let res: Result = new Result(); let res: Result = new Result();
res.vCalc = this.v["A"] + this.v["B"]; res.vCalc = this.v["A"] + this.v["B"];
...@@ -8,4 +12,4 @@ export class NubTest extends Nub { ...@@ -8,4 +12,4 @@ export class NubTest extends Nub {
} }
} }
export let nub = new NubTest({ "A": 1, "B": 2, "C": null }); export let nub = new NubTest({ "A": 1, "B": 2, "C": null });
export let res = new Result; export let res = new Result;
\ No newline at end of file
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
// ], // ],
"include": [ "include": [
"../src/**/*.ts", "../src/**/*.ts",
"../spec/**/*.ts", "../spec/**/*.ts"
"../typings/**/*.d.ts"
] ]
} }
...@@ -2,11 +2,11 @@ import { Debug, Nub, Result } from "./base"; ...@@ -2,11 +2,11 @@ import { Debug, Nub, Result } from "./base";
export class Dichotomie extends Debug { export class Dichotomie extends Debug {
/** Pas de parcours de l'intervalle pour initialisation dichotomie */ /** Pas de parcours de l'intervalle pour initialisation dichotomie */
readonly IDEFINT = 100; readonly IDEFINT = 100;
/** Nombre d'itérations maximum de la dichotomie */ /** Nombre d'itérations maximum de la dichotomie */
readonly IDICMAX = 100; readonly IDICMAX = 100;
/** /**
* Construction de la classe. * Construction de la classe.
* @param nub Noeud de calcul contenant la méthode de calcul Equation * @param nub Noeud de calcul contenant la méthode de calcul Equation
...@@ -25,7 +25,7 @@ export class Dichotomie extends Debug { ...@@ -25,7 +25,7 @@ export class Dichotomie extends Debug {
set vX(vCalc) { set vX(vCalc) {
this.nub.v[this.sVarCalc] = vCalc; this.nub.v[this.sVarCalc] = vCalc;
} }
/** /**
* Méthode simulant l'opérateur booléen xor * Méthode simulant l'opérateur booléen xor
* @see http://www.howtocreate.co.uk/xor.html * @see http://www.howtocreate.co.uk/xor.html
...@@ -57,7 +57,7 @@ export class Dichotomie extends Debug { ...@@ -57,7 +57,7 @@ export class Dichotomie extends Debug {
* @param rInit Valeur initiale de l'inconnue à rechercher * @param rInit Valeur initiale de l'inconnue à rechercher
*/ */
Dichotomie(rTarget: number, rTol: number, rInit: number): Result { Dichotomie(rTarget: number, rTol: number, rInit: number): Result {
let res: Result let res: Result;
let XminInit = 1E-8; let XminInit = 1E-8;
this.vX = XminInit; this.vX = XminInit;
...@@ -66,7 +66,7 @@ export class Dichotomie extends Debug { ...@@ -66,7 +66,7 @@ export class Dichotomie extends Debug {
let XmaxInit: number = Math.max(1, rInit) * 100; let XmaxInit: number = Math.max(1, rInit) * 100;
this.vX = XmaxInit; this.vX = XmaxInit;
let v2 = this.Calcul().vCalc; let v2 = this.Calcul().vCalc;
let DX = (XmaxInit - XminInit) / this.IDEFINT; let DX = (XmaxInit - XminInit) / this.IDEFINT;
let nIterMax = Math.floor(Math.max(XmaxInit - rInit, rInit - XminInit) / DX + 1); let nIterMax = Math.floor(Math.max(XmaxInit - rInit, rInit - XminInit) / DX + 1);
let Xmin = rInit; let Xmin = rInit;
...@@ -76,7 +76,7 @@ export class Dichotomie extends Debug { ...@@ -76,7 +76,7 @@ export class Dichotomie extends Debug {
this.debug("rInit: " + rInit); this.debug("rInit: " + rInit);
this.vX = rInit; this.vX = rInit;
let v = this.Calcul().vCalc; let v = this.Calcul().vCalc;
v1 = v; v1 = v;
v2 = v; v2 = v;
this.debug(nIterMax); this.debug(nIterMax);
...@@ -113,9 +113,11 @@ export class Dichotomie extends Debug { ...@@ -113,9 +113,11 @@ export class Dichotomie extends Debug {
} }
if (this.XOR(rTarget > v1, rTarget >= v2)) { break; } if (this.XOR(rTarget > v1, rTarget >= v2)) { break; }
} }
// Gestion de l'absence de solution dans l'intervalle de recherche
if (nIter >= this.IDEFINT) { if (nIter >= this.IDEFINT) {
this.debug("in if"); this.debug("nIter >= this.IDEFINT");
// Pas d'intervalle trouvé avec au moins une solution
if (v2 < rTarget && v1 < rTarget) { if (v2 < rTarget && v1 < rTarget) {
// Cote de l'eau trop basse pour passer le débit il faut ouvrir un autre ouvrage // Cote de l'eau trop basse pour passer le débit il faut ouvrir un autre ouvrage
this.vX = XmaxInit; this.vX = XmaxInit;
...@@ -128,32 +130,32 @@ export class Dichotomie extends Debug { ...@@ -128,32 +130,32 @@ export class Dichotomie extends Debug {
res.extraVar["flag"] = -1; // la valeur cible n'est pas dans l'intervalle de recherche res.extraVar["flag"] = -1; // la valeur cible n'est pas dans l'intervalle de recherche
return res; return res;
} }
else {
// Dichotomie // Dichotomie
this.debug("in dicho"); this.debug("start dicho");
let X = rInit; let X = rInit;
for (nIter = 1; nIter <= this.IDICMAX; nIter++) { for (nIter = 1; nIter <= this.IDICMAX; nIter++) {
this.vX = X; this.vX = X;
v = this.Calcul().vCalc; v = this.Calcul().vCalc;
if (rTarget != 0 && Math.abs(X1 - X2) <= rTol) { break; } if (rTarget != 0 && Math.abs(X1 - X2) <= rTol) { break; }
if (this.XOR(rTarget < v, v1 <= v2)) { if (this.XOR(rTarget < v, v1 <= v2)) {
// rTarget < IQ et v(X1) > v(X2) ou pareil en inversant les inégalités // rTarget < IQ et v(X1) > v(X2) ou pareil en inversant les inégalités
X1 = this.vX; X1 = this.vX;
}
else {
// rTarget < IQ et v(X1) < v(X2) ou pareil en inversant les inégalités
X2 = this.vX;
}
X = (X2 + X1) * 0.5;
this.debug((X));
} }
if (nIter == this.IDICMAX) { else {
res = this.Calcul(); // rTarget < IQ et v(X1) < v(X2) ou pareil en inversant les inégalités
res.extraVar["flag"] = -1; // la valeur cible n'est pas dans l'intervalle de recherche X2 = this.vX;
return res;
} }
X = (X2 + X1) * 0.5;
this.debug((X));
}
if (nIter == this.IDICMAX) {
res = this.Calcul();
res.extraVar["flag"] = -1; // la valeur cible n'est pas dans l'intervalle de recherche
return res;
} }
res.vCalc = v res = new Result();
res.vCalc = X;
return res; return res;
} }
} }
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment