mathpaqs_20230121.0.0_773568e5/diff_eq/ode/three_lakes.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
--  This program solves a vectorial ordinary differential equation
--  (or a system of ordinary differential equations).
--
--  * The unknown is a vector containing the levels of three lakes.
--  * The lakes are connected by two channels.
--  * There is an initial condition: the levels at t = 0.
--  * Boundary conditions take the form of natural inflows into the lakes,
--    and a single, controlled outflow out of one of the lakes.
--
--  Related publication:
--    Evolution simulee des niveaux dans le systeme des Trois-Lacs,
--    F. & G. de Montmollin,
--    Bulletin de la Societe vaudoise des sciences naturelles.
--    88.2: 121-129, ISSN 0037-9603, 2002
--
--  Related post:
--    https://gautiersblog.blogspot.com/2020/05/the-three-lakes-problem.html
--

with Ada.Text_IO,
     Ada.Integer_Text_IO,
     Ada.Numerics.Generic_Elementary_Functions;

procedure Three_Lakes is

  type Real is digits 15;

  package PFIO is new Ada.Text_IO.Float_IO (Real);
  package PFEF is new Ada.Numerics.Generic_Elementary_functions (Real);

  type Lake is (Morat, Neuchatel, Bienne);

  type Lake_Vector is array (Lake) of Real;

  function "*" (l : Real; v : Lake_Vector) return Lake_Vector is
    r : Lake_Vector;
  begin
    for i in v'Range loop r (i) := v (i) * l; end loop;
    return r;
  end "*";

  function "+" (a, b : Lake_Vector) return Lake_Vector is
    r : Lake_Vector;
  begin
    for i in a'Range loop r (i) := a (i) + b (i); end loop;
    return r;
  end "+";

  function Sign (i : Real) return Real is
  begin
    if    i < 0.0 then  return -1.0;
    elsif i = 0.0 then  return  0.0;
    else                return  1.0;
    end if;
  end Sign;

  ivs : constant Lake_Vector :=
     (Morat     => 1.0 / 2.2820e7,
      Neuchatel => 1.0 / 2.1581e8,
      Bienne    => 1.0 / 4.0870e7);

  --  We solve numerically   x' (t) = f (x (t), t)   over the time step h.
  --
  procedure Evolution (x : in out Lake_Vector; q_e : Lake_Vector; q_sb, h : Real) is
    --
    function f (x : Lake_Vector) return Lake_Vector is
      q_tr_mn, q_tr_nb : Real;
      --
      procedure Flux_tansfert is
        use PFEF;
      begin
        q_tr_mn :=
          --  Canal de la Broye: Morat -> Neuchatel.
          Sign (x (Morat) - x (Neuchatel)) *                           --  sens d'ecoulement
          15.223 *                                                      --  facteur de debit
          (((x (Morat) + x (Neuchatel)) * 0.5 - 426.0)**1.868) *   --  effet du niveau moyen
          ((abs (x (Morat) - x (Neuchatel)))**0.483);       --  effet de la diff. de niveaux
        --
        q_tr_nb :=
          --  Canal de la Thielle: Neuchatel -> Bienne.
          Sign (x (Neuchatel) - x (Bienne)) *                          --  sens d'ecoulement
          18.582 *                                                      --  facteur de debit
          (((x (Neuchatel) + x (Bienne)) * 0.5 - 426.0)**2.511) *  --  effet du niveau moyen
          ((abs (x (Neuchatel) - x (Bienne)))**0.482);      --  effet de la diff. de niveaux
      end Flux_tansfert;
    begin
      Flux_tansfert;
      return
         (Morat     => (q_e (Morat)     - q_tr_mn) * ivs (Morat),
          Neuchatel => (q_e (Neuchatel) + q_tr_mn - q_tr_nb) * ivs (Neuchatel),
          Bienne    => (q_e (Bienne)              + q_tr_nb - q_sb) * ivs (Bienne));
    end f;
    k1, k2, k3, k4 : Lake_Vector;
  begin
    --  Runge-Kutta, Order 4
    k1 := f (x);
    k2 := f (x + h * 0.5 * k1);
    k3 := f (x + h * 0.5 * k2);
    k4 := f (x + h *       k3);
    x := x + h * (1.0 / 6.0) * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
  end Evolution;

  procedure Simulation is
    use Ada.Text_IO, Ada.Integer_Text_IO, PFIO;
    x, q_e : Lake_Vector;
    q_sb, h : Real;
    n_iter : Integer;
    out_step : Integer;
    rf : File_Type;
    sep : constant Character := ';';
  begin
    h := 3600.0;
    n_iter := 24 * 20;
    out_step := 3;

    x := (Morat => 428.2, Neuchatel => 429.0, Bienne => 429.4);  --  Lake levels at time t = 0.
    q_e := (Morat => 40.0, Neuchatel => 70.0, Bienne => 100.0);  --  Inflows (could be dynamic).
    q_sb := 200.0;                                               --  Outflow (could be dynamic).

    Create (rf, Out_File, "3_lakes.csv");
    Put (rf, "t");
    for l in Lake loop
      Put (rf, sep);
      Put (rf, Lake'Image (l));
    end loop;
    New_Line (rf);
    for i in 0 .. n_iter loop
      if i mod out_step = 0 then
        Put (rf, i);
        for l in Lake loop
          Put (rf, sep);
          Put (rf, x (l), 4, 5, 0);
        end loop;
        New_Line (rf);
      end if;
      Evolution (x, q_e, q_sb, h);
    end loop;
    Close (rf);
  end Simulation;

begin
  Simulation;
end Three_Lakes;