summaryrefslogtreecommitdiff
path: root/src/fluid_simulator.adb
blob: b8d47cee40397465f48ff481d3ade310972a69e9 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

with

    Ada.Numerics.Generic_Complex_Types,
    Ada.Containers.Vectors,
    Ada.Characters.Latin_1,
    Ada.Strings.Fixed,
    Ada.Text_IO;

procedure Fluid_Simulator is

    package Latin renames Ada.Characters.Latin_1;
    package IO renames Ada.Text_IO;



    procedure Clear_Screen is
    begin
        --  ANSI control sequence Erase in Display
        --  Variant to clear entire screen
        IO.Put (Latin.ESC & "[2J");
    end Clear_Screen;

    procedure Reset_Cursor is
    begin
        --  ANSI control sequence Cursor Position
        --  Parameters to move cursor to top left corner
        IO.Put (Latin.ESC & "[1;1H");
    end Reset_Cursor;



    type Quantity is digits 18;

    package Fixed is new Ada.Numerics.Generic_Complex_Types (Real => Quantity);
    use type Fixed.Complex;

    type Particle is record
        Place        : Fixed.Complex;
        Solid        : Boolean;
        Density      : Quantity := 0.0;
        Acceleration : Fixed.Complex := Fixed.Compose_From_Cartesian (0.0, 0.0);
        Velocity     : Fixed.Complex := Fixed.Compose_From_Cartesian (0.0, 0.0);
    end record;

    function Create
           (X, Y  : Quantity;
            Solid : in Boolean)
        return Particle is
    begin
        return (Place => Fixed.Compose_From_Cartesian (X, Y), Solid => Solid, others => <>);
    end Create;



    --  Constant properties of particles
    Particle_Radius : constant Quantity := 2.0;
    Particle_Mass   : constant Quantity := 1.0;

    --  Constant used in calculating fluid interaction forces
    P0 : constant Quantity := 1.5;

    --  Other constant force factors
    Gravity_Factor : constant Fixed.Complex := Fixed.Compose_From_Cartesian (0.0, 1.0);
    Pressure_Factor : constant Quantity := 4.0;
    Viscosity_Factor : constant Quantity := 8.0;



    package Particle_Vectors is new Ada.Containers.Vectors
       (Index_Type   => Positive,
        Element_Type => Particle);

    Particles : Particle_Vectors.Vector := Particle_Vectors.Empty_Vector;



    procedure Read_Input
           (Store : out Particle_Vectors.Vector)
    is
        Input : Character;
        X, Y : Quantity := 1.0;
    begin
        while not IO.End_Of_File (IO.Standard_Input) loop
            IO.Get_Immediate (Input);
            if Input = Latin.LF then
                X := 1.0;
                Y := Y + 2.0;
            else
                if Input > Latin.Space and Input < Latin.DEL then
                    Store.Append (Create (X, Y, (Input = '#')));
                    Store.Append (Create (X, Y + 1.0, (Input = '#')));
                end if;
                X := X + 1.0;
            end if;
        end loop;
    end Read_Input;



    --  Liquid_Chars : constant String (1 .. 16) := " .,_`/[/']\\-/\#";
    Liquid_Chars : constant String (1 .. 16) := " ,.-`[//'\]\-\/#";

    type Liquidex is mod 2**4;

    type March_Cell is record
        Index : Liquidex := 0;
        Density : Quantity := 0.0;
    end record;

    type March_Cell_Grid is array (Integer range <>, Integer range <>) of March_Cell;



    function BG_Color_Code
           (Value : in Natural)
        return String
    is
        use Ada.Strings;
        use Ada.Strings.Fixed;
    begin
        --  Total length is always 11 characters
        return Latin.ESC & "[48;5;" & Tail (Trim (Integer'Image (Value), Left), 3, '0') & "m";
    end BG_Color_Code;



    function Lookup
           (Input : in March_Cell_Grid;
            X, Y  : in Integer)
        return String
    is
        Average_Density : Natural := Integer (Quantity'Ceiling (Input (X, Y).Density / 4.0));
        Bit_Index : Positive := Integer (Input (X, Y).Index) + 1;
        Choice : Natural;
    begin
        case Average_Density is
            when 1 .. 2 => Choice := 19; --  dark blue
            when 3 .. 4 => Choice := 20; --  slightly less dark blue
            when 5 .. 6 => Choice := 21; --  slightly dark blue
            when 7 .. 8 => Choice := 12; --  blue
            when 9 .. 10 => Choice := 14; --  cyan
            when 11 .. 12 => Choice := 10; --  green
            when 13 .. 14 => Choice := 11; --  yellow
            when 15 .. 16 => Choice := 3; --  dark yellow
            when 17 .. 18 => Choice := 9; --  red
            when 19 .. 20 => Choice := 1; --  dark red
            when others => Choice := 0; --  black
        end case;
        --  Total length should always be 12 characters
        return BG_Color_Code (Choice) & Liquid_Chars (Bit_Index);
    end Lookup;



    function Marching_Squares
           (Input : in Particle_Vectors.Vector)
        return String
    is
        --  Having the grid be one bigger around the edges simplifies calculations
        Grid : March_Cell_Grid (0 .. 81, 0 .. 26);

        --  80 cols * 25 rows * 12 chars/cell + 24 linefeeds + 4 char color reset = 24028
        --  Oh yeah, baby, big strings
        Output : String (1 .. 24028);

        X, Y, S : Integer;
    begin
        for P of Input loop
            X := Integer (Fixed.Re (P.Place) - 0.5);
            Y := Integer (Fixed.Im (P.Place) / 2.0 - 0.5);
            if X >= 0 and X <= 80 and Y >= 0 and Y <= 25 then
                for J in Integer range 0 .. 1 loop
                    for I in Integer range 0 .. 1 loop
                        Grid (X + I, Y + J).Index :=
                            Grid (X + I, Y + J).Index or (2 ** (I + 2 * J));
                        Grid (X + I, Y + J).Density :=
                            Grid (X + I, Y + J).Density + P.Density;
                    end loop;
                end loop;
            end if;
        end loop;
        for J in Integer range 1 .. 25 loop
            for I in Integer range 1 .. 80 loop
                S := (J - 1) * 961 + (I - 1) * 12 + 1;
                Output (S .. S + 11) := Lookup (Grid, I, J);
            end loop;
            Output (J * 961) := Latin.LF;
        end loop;
        Output (24025 .. 24028) := Latin.ESC & "[0m";
        return Output;
    end Marching_Squares;



    procedure Calculate_Density
           (Store : in out Particle_Vectors.Vector)
    is
        Rij, W : Quantity;
    begin
        for P of Store loop
            P.Density := (if P.Solid then 9.0 else 0.0);
            for Q of Store loop
                Rij := Fixed.Modulus (P.Place - Q.Place);
                W := (Rij / Particle_Radius - 1.0) ** 2;
                if Rij < Particle_Radius then
                    P.Density := P.Density + Particle_Mass * W;
                end if;
            end loop;
        end loop;
    end Calculate_Density;



    procedure Calculate_Interaction
           (Store : in out Particle_Vectors.Vector)
    is
        Displacement, Pressure, Viscosity : Fixed.Complex;
        Rij : Quantity;
    begin
        for P of Store loop
            P.Acceleration := Gravity_Factor;
            for Q of Store loop
                Displacement := P.Place - Q.Place;
                Rij := Fixed.Modulus (Displacement);
                if Rij < Particle_Radius then
                    Pressure := (P.Density + Q.Density - 2.0 * P0) * Pressure_Factor * Displacement;
                    Viscosity := (P.Velocity - Q.Velocity) * Viscosity_Factor;
                    P.Acceleration := P.Acceleration +
                        Fixed.Compose_From_Cartesian (1.0 - Rij / Particle_Radius) /
                        P.Density * (Pressure - Viscosity);
                end if;
            end loop;
        end loop;
    end Calculate_Interaction;



    procedure Update_Position
           (Store : in out Particle_Vectors.Vector) is
    begin
        for P of Store loop
            if not P.Solid then
                P.Velocity := P.Velocity + P.Acceleration / 10.0;
                P.Place := P.Place + P.Velocity;
            end if;
        end loop;
    end Update_Position;



    procedure Cull_Outside_Bounds
           (Store     : in out Particle_Vectors.Vector;
            Threshold : in Quantity) is
    begin
        for C in reverse Store.First_Index .. Store.Last_Index loop
            if Fixed.Re (Store (C).Place) < 1.0 - Threshold or
               Fixed.Re (Store (C).Place) > 80.0 + Threshold or
               Fixed.Im (Store (C).Place) < 1.0 - Threshold or
               Fixed.Im (Store (C).Place) > 50.0 + Threshold
            then
                Store.Delete (C);
            end if;
        end loop;
    end Cull_Outside_Bounds;

begin

    Read_Input (Particles);
    loop
        Clear_Screen;
        Reset_Cursor;
        IO.Put (Marching_Squares (Particles));
        Calculate_Density (Particles);
        Calculate_Interaction (Particles);
        Update_Position (Particles);
        Cull_Outside_Bounds (Particles, 50.0);
        delay 0.012321;
    end loop;

end Fluid_Simulator;