session.ts 12 KB
Newer Older
Dorchies David's avatar
Dorchies David committed
import { CalculatorType, ComputeNodeType } from "./compute-node";
import { Nub } from "./nub";
import { Props } from "./props";
Dorchies David's avatar
Dorchies David committed

// Calculettes
import { ConduiteDistrib, ConduiteDistribParams } from "./cond_distri";
import { LechaptCalmon, LechaptCalmonParams } from "./lechaptcalmon";
import { MacroRugo, MacrorugoParams } from "./macrorugo/macrorugo";
Dorchies David's avatar
Dorchies David committed
import { PabDimension, PabDimensionParams } from "./pab/pab_dimension";
import { PabPuissance, PabPuissanceParams } from "./pab/pab_puissance";
Dorchies David's avatar
Dorchies David committed
import { RegimeUniforme } from "./regime_uniforme";
import { CourbeRemous, CourbeRemousParams, MethodeResolution } from "./remous";
import { SectionParametree } from "./section/section_nub";
Dorchies David's avatar
Dorchies David committed
import { Cloisons, CloisonsParams } from "./structure/cloisons";
Dorchies David's avatar
Dorchies David committed
import { Dever, DeverParams } from "./structure/dever";
import { ParallelStructure, ParallelStructureParams } from "./structure/parallel_structure";

// Classes relatives aux sections
import { cSnCirc, ParamsSectionCirc } from "./section/section_circulaire";
import { cSnPuiss, ParamsSectionPuiss } from "./section/section_puissance";
import { cSnRectang, ParamsSectionRectang } from "./section/section_rectang";
import { cSnTrapez, ParamsSectionTrapez } from "./section/section_trapez";
import { acSection } from "./section/section_type";

// Classes relatives aux structures
import { CreateStructure } from "./structure/factory_structure";
import { Structure } from "./structure/structure";
Dorchies David's avatar
Dorchies David committed
import { LoiDebit, StructureType } from "./structure/structure_props";
export class Session {
Dorchies David's avatar
Dorchies David committed

    public static getInstance() {
        if (Session._instance === undefined) {
            Session._instance = new Session();
Dorchies David's avatar
Dorchies David committed
        }
        return Session._instance;
Dorchies David's avatar
Dorchies David committed
    }
    /** instance pour le pattern singleton */
    private static _instance: Session;
    private defaultPrecision: number = 0.001;
Dorchies David's avatar
Dorchies David committed

    /** Nubs de la session */
    private _nubs: Nub[];
Dorchies David's avatar
Dorchies David committed
    constructor() {
        this._nubs = [];
     * crée un Nub et l'ajoute à la session
    public createSessionNub(p: Props, dbg: boolean = false): Nub {
        const res = this.newNubWithProps(p, dbg);
        this._nubs.push(res);
     * remplace un Nub par un nouveau dans la session
     * @param sn Nub à remplacer
     * @param params propriété du nouveau Nub
    public replaceNub(sn: Nub, params: Props): Nub {
        for (const n of this._nubs) {
Dorchies David's avatar
Dorchies David committed
            if (n.uid === sn.uid) {
                const newNub = this.newNubWithProps(params);
                this._nubs[i] = newNub;
                if (sn instanceof Structure) {
                    const struct = sn as Structure;
                        struct.parent.replaceStructureInplace(struct, newNub as Structure);
                    } else {
                        throw new Error("Structure nub had no parent");
                    }
                }
    /**
     * Removes a Nub from the session
     * @param sn the Nub to remove
     */
    public deleteNub(sn: Nub) {
Dorchies David's avatar
Dorchies David committed
        let i = 0;
        for (const n of this._nubs) {
Dorchies David's avatar
Dorchies David committed
            if (n.uid === sn.uid) {
                this._nubs.splice(i, 1);
Dorchies David's avatar
Dorchies David committed
                return;
            }
            i++;
        }
        throw new Error(`Session.deleteNub() : le Nub (uid ${sn.uid}) à supprimer n'a pas été trouvé`);
Dorchies David's avatar
Dorchies David committed
    }

    private newNubWithProps(p: Props, dbg: boolean = false): Nub {
        const nub = this.createNub(p, dbg);
        return nub;
Dorchies David's avatar
Dorchies David committed
    }

     * Crée un Nub à partir d'une description (Props)
     * @param params paramètres supplémentaires spécifiques
     *        - calcType: type de Nub
     *        - nodeType: sous type de Nub
     * @param dbg activer débogage
    private createNub(params: Props, dbg: boolean = false): Nub {
        const calcType: CalculatorType = params.getPropValue("calcType");
        const nodeType: ComputeNodeType = params.getPropValue("nodeType");

        let nub: Nub;
Mathias Chouet's avatar
Mathias Chouet committed
        let prms: any;
        switch (calcType) {
            case CalculatorType.ConduiteDistributrice:
            {
                prms = new ConduiteDistribParams(3, // débit Q
                    1.2, // diamètre D
                    0.6, // perte de charge J
                    100, // Longueur de la conduite Lg
                    1e-6, // Viscosité dynamique Nu
                );
                nub = new ConduiteDistrib(prms, dbg);
                break;
            }

            case CalculatorType.LechaptCalmon:
            {
                prms = new LechaptCalmonParams(3, // débit
                    1.2, // diamètre
                    0.6, /// perte de charge
                    100, // longueur du toyo
                    1.863, // paramètre L du matériau
                    2, // paramètre M du matériau
                    5.33// paramètre N du matériau
                );
                nub = new LechaptCalmon(prms, dbg);
                break;
            }

            case CalculatorType.SectionParametree:
                nub = new SectionParametree(this.createSection(nodeType, dbg), dbg);
                break;

            case CalculatorType.RegimeUniforme:
                const sect: acSection = this.createSection(nodeType, dbg);
                const ru = new RegimeUniforme(sect, dbg);
                nub = ru;
                break;

            case CalculatorType.CourbeRemous:
            {
                const sectCR: acSection = this.createSection(nodeType, dbg);
                prms = new CourbeRemousParams(sectCR, 0.15, // Yamont = tirant amont
                    0.4, // Yaval = tirant aval
                    100,  // Long= Longueur du bief
                    5,  // Dx=Pas d'espace
                    MethodeResolution.EulerExplicite
                );
                nub = new CourbeRemous(prms, dbg);
                break;
            }

            case CalculatorType.PabDimensions:
            {
                prms = new PabDimensionParams(
                    2,      // Longueur L
                    1,      // Largeur W
                    0.5,    // Tirant d'eau Y
                    2       // Volume V
                );
                nub = new PabDimension(prms, dbg);
                break;
            }

            case CalculatorType.PabPuissance:
            {
                prms = new PabPuissanceParams(
                    0.3,      // Chute entre bassins DH (m)
                    0.1,      // Débit Q (m3/s)
                    0.5,    // Volume V (m3)
                    588.6   // Puissance dissipée PV (W/m3)
                );
                nub = new PabPuissance(prms, dbg);
                break;
            }

            case CalculatorType.Structure:
                const structType: StructureType = params.getPropValue("structureType");
                const loiDebit: LoiDebit = params.getPropValue("loiDebit");
                nub = CreateStructure(structType, loiDebit);
                break;

            case CalculatorType.ParallelStructure:
            {
                prms = new ParallelStructureParams(0.5, // Q
                    102, // Z1
                    101.5 // Z2
                );
                nub = new ParallelStructure(prms, dbg);
                break;
            }
            case CalculatorType.Dever:
            {
                const deverPrms = new DeverParams(0.5, // Q
                    102, // Z1
                    10,  // BR : largeur du cours d'eau
                    99   // ZR : cote du lit du cours d'eau
                );
                nub = new Dever(deverPrms, dbg);
                break;
            }
Dorchies David's avatar
Dorchies David committed
            case CalculatorType.Cloisons:
            {
                nub = new Cloisons(
                    new CloisonsParams(
                        0,      // Débit total (m3/s)
                        102,    // Cote de l'eau amont (m)
                        10,     // Longueur des bassins (m)
                        1,      // Largeur des bassins (m)
                        1,      // Profondeur moyenne (m)
                        0.5     // Hauteur de chute (m)
                    ), dbg
                );
                break;
            }
            case CalculatorType.MacroRugo:
            {
                nub = new MacroRugo(
                    new MacrorugoParams(
                        12.5,   // ZF1
                        6,      // L
                        1,      // B
                        0.05,   // If
                        1.57,   // Q
                        0.6,    // h
                        0.01,   // Ks
                        0.05,   // C
                        0.5,    // D
                        0.8,    // k
                        1.5     // Cd0
                    )
                );
Dorchies David's avatar
Dorchies David committed

Dorchies David's avatar
Dorchies David committed
                throw new Error(
                    // tslint:disable-next-line:max-line-length
                    `Session.createNub() : calculatrice '${CalculatorType[calcType]}' / noeud de calcul '${ComputeNodeType[nodeType]}' non pris en charge`
Dorchies David's avatar
Dorchies David committed
                );

        // propagate properties
        nub.properties = params;

        return nub;
    /**
     * Crée un Nub de type Section
     * @param nt ComputeNodeType
     * @param dbg activer débogage
     */
    private createSection(nt: ComputeNodeType, dbg: boolean = false): acSection {
        switch (nt) {
            case ComputeNodeType.None: // pour les paramètres communs, n'importe quelle section convient
            case ComputeNodeType.SectionTrapeze:
                {
                    const prms = new ParamsSectionTrapez(2.5, // largeur de fond
                        0.56, // fruit
                        0.8, // tirant d'eau
                        40, //  Ks=Strickler
                        1.2,  //  Q=Débit
                        0.001, //  If=pente du fond
                        this.defaultPrecision, // précision
                        1, // YB= hauteur de berge
                    );

                    return new cSnTrapez(prms, dbg);
                }

            case ComputeNodeType.SectionRectangle:
                {
                    const prms = new ParamsSectionRectang(0.8, // tirant d'eau
                        2.5, // largeur de fond
                        40, //  Ks=Strickler
                        1.2, // Q=Débit
                        0.001, // If=pente du fond
                        this.defaultPrecision, // précision
                        1 // YB=hauteur de berge
                    );
                    return new cSnRectang(prms, dbg);
                }

            case ComputeNodeType.SectionCercle:
                {
                    const prms = new ParamsSectionCirc(2, // diamètre
                        0.8, // tirant d'eau
                        40, //  Ks=Strickler
                        1.2,  //  Q=Débit
                        0.001, //  If=pente du fond
                        this.defaultPrecision, // précision
                        1, // YB= hauteur de berge
                    );
                    return new cSnCirc(prms, dbg);
                }

            case ComputeNodeType.SectionPuissance:
                {
                    const prms = new ParamsSectionPuiss(0.5, // coefficient
                        0.8, // tirant d'eau
                        4, // largeur de berge
                        40, //  Ks=Strickler
                        1.2,  //  Q=Débit
                        0.001, //  If=pente du fond
                        this.defaultPrecision, // précision
                        1, // YB= hauteur de berge
                    );
                    return new cSnPuiss(prms, dbg);
                }

            default:
                throw new Error(`type de section ${ComputeNodeType[nt]} non pris en charge`);
        }
    }
Dorchies David's avatar
Dorchies David committed
}