Skip to content

niels747/2D-Weather-Sandbox

Repository files navigation

2D Weather Sandbox

Previously called "Weather_Sim_4"

This projects aims to produce a semirealistic two-dimensional, realtime, interactive simulation of the weather in earth's troposphere.

Clouds and precipitation

Simulating clouds and precipitation are the main objectives of this project. All the equations relating to water phase change are simplified versions of the real ones, to improve performance and ease programming. Precipitation is simulated using discrete particles but can be viualized as both partiles and smooth realistic looking curtains.

Forming Cell: Screenshot (7)

Mature Cell 1: Screenshot (23) Mature Cell 1 with Precipitation Particles: Screenshot (26)

Mature Cell 2: Screenshot (10)

Mature Cell 3 zoomed out: Screenshot (17)

Sunlight with realistic colors makes clouds and precipitation look real Screenshot (31) Screenshot (32)

Bui5

Limitations

Due to the two-dimensional nature of the simulation, it cannot simulate 3D vortices such as tornadoes, dust devils or hurricanes. It can only simulate linear storm systems.

Example: low intensity cell analysis

Warm moist air is rising into the cloud on the right condensing water and releasing heat until it eventually rises well above the freezing level and forming ice (snow), realeasing even more heat. The snow grows until it descents down under the freezing level and melts into rain, absorbing heat and creating a downdraft. Below the cloud level some of the rain evaporates, absorbing even more heat and strengtening the downward motion. Near the surface the air spreads out sideways and in this case mostly to the right, creating a small low level cold front. This cold front pushes even more warm air up. Schermopname (92)_LI The dew points and cape are very low in this example, but it's just enough to form a nice stable cell. Naamloos

The Code

The simulation is based on a simple fluid simulation topology. In total the project contains arround 5000 lines of code of wich 2800 in JS and 2000 in GLSL. All code was written by me (Niels Daemen) except for the libraries included

Fluid Model

The fluid model consists of a 2 dimensional grid of cells. Each cell has the following properties:

image

These properties are not simply defined at the center of each cell, but using a so called staggered grid. the velocities are defined exactly on the border between cells. They are therefore exactly in between the centers of the cells, which is where the pressures are defined. In this way the velocities define the exact flow rate from one cell to its neighbor. This makes calculating new velocities and pressures very simple.

image

Iteration

One iteration or timestep consists of the following steps:

  1. Calculating pressure
  2. Calculating velocities
  3. Advection Every step is completed for each cell in the grid, before the next step is executed.

Calculating pressure

Pressure can be imagined as being the amount of fluid that is in a cell. The velocities can be imagined as a flow of fluid from a cell to a neighbouring cell. Therefore the change in pressure is equal to the net inflow to the cell this is also known as the divergence (or convergence) in the velocity vector field. To calculate the net inflow to the cell, the total outflow is simply subtracted from the total inflow. This is done for both x and y directions.

image

image

Calculating velocities

The change in the velocity trough a point is proportional to the pressure across it. This is basically just Newton's 2nd law (F = m * a) Pressure is force / area, and since the area of the cell is constant, the force (F) is simply the pressure gradient across the point. The mass (M) is also assumed to be constant as if the fluid has a constant density, this is not physically accurate. The acceleration (a) is then simply a function of the pressure gradient. Acceleration is simply the change in velocity / time, and because the timestep is constant all that remains is simply adding the pressure gradients to the velocities for both x and y axis:

image

Libraries

The simulation and visualization itself is entirely custom js and glsl written by me (Niels Daemen). The sounding graph, keyboard and mouse controls are also custom code. The simulation can run and be partly controlled without any libraries. I do however use the following libraries for part of the user interface and file compression:

DatGui: Used for the user interface because it's very easy to add more controllable variables. It is however getting a bit cluttered and other options should be evaluated. Building a custom interface is also an option.

Pako: Only used for data compression to reduce save file sizes. Size reduction can be 2-4 times depending on the state of the simulation. Not essential. Downside is that it takes longer to save and load files.

How to run it locally and modify code

  1. Install VS code: https://code.visualstudio.com/
  2. Install VS Code extensions:
  1. Clone project using GIT, or just download ZIP
  2. Open project folder in VS Code
  3. Open index.html
  4. Start live server (Go Live), automatically opens page in browser