You are browsing as a guest. Sign up (or log in) to start making projects!

Fluid Chemistry Simulation

  • 7 Devlogs
  • 12 Total hours

The idea is to expand upon a fluid simulation I built last year and add in the capacity to simulate chemical reactions. The original simulation does an okay job at simulating an incompressible fluid, but considering my new focus on simulating chemistry I'm very prepared to sacrifice a lot of my previous focus on fluid mechanics to actually allow for chemical reactions. I also plan to switch my focus to compressible fluids (to simulate gases).

Open comments for this post

1h 56m 38s logged

Today I mostly pondered my simulation mechanics and did some amount of polishing.

Today’s Work:

  • Observing my simulation rendering and the values that I was getting from my mouse, it felt like the values weren’t exactly lining up.After ~30 minutes of pondering I have realized that the issue is that my rendering is flipped diagonally.
    • I’m not sure exactly why this is. It is probably due to how unity turns 2D arrays into buffersI
    • fixed it for good hopefully by re-writing the grid into a 1D array before passing it to the shaders
  • I also did more messing around with variables, and realized that the simulation is also somewhat unstable at really low temperatures
    • This is because I was diving diffused energy by zero at some point, I think I have fixed it
  • Fixed the temperature view. Though I will note that with how I currently measure it it is almost by definition uniform throughout the mixture.

Possible Next Steps:

  • Add in the capacity to simulate an arbitrary amount of gases
0
0
10
Open comments for this post

2h 47m 51s logged

I’m going to try switching to a slightly different style of devlog, where I effectively write somewhat comprehensive notes about what I’m doing as I do it, and then post them here. I write these in an external note program (obsidian) and copy paste them in here.

30/07/26

  • Added in some UI that lets you see the gas amount/pressure/temperature(computed via ideal gas law) at any given point
    • This makes me realize some potential problems with my temperature systems, as it seems like with how the system is currently designed if I add in more gas the temperature does not increase (though this kind of makes sense, considering the assumptions of the ideal gas law)
  • It turns out that with my current framerate (60 FPS) I cannot run gasses at much higher than 1 kelvin, so I tried increasing the simulation framerate to 600 FPS. To my astonishment, my CPU is perfectly capable of running my simulation timestep at 600HZ. This being stated, it is not in fact capable of running it much higher, and 600HZ seems to only really be enough to simulate gases at ~50K. In order to go higher, for now I think I’ll resort to “slowing down” the simulation speed by lowering the timestep of the simulation (how much time it believes is in-between each frame) without really lowering the speed my CPU runs the simulation at (keep it at ~600HZ). This has the effect of increasing the simulation stability massively, with the downside of making it run much slower than realtime. What I found to work okay-ish was running my simulation running at 500000 frames per simulation seconds, at about 1/80th of realtime. Unfortunately even when I slow it down there are a lot of artifacts made by high speed gases, but I’m not sure I can really do much about those without really going hard into CFD (computational fluid dynamics).
  • The last thing I did was modify my rendering shader so that it can visualize the gases’ pressure. I also coded in temperature but that does not seem to function for some inexplicable reason and I don’t quite have the time at the moment to fix it.

I’m also going to try my absolute hardest to get a video attachment working.

0
0
15
Open comments for this post

2h 42m 24s logged

Going to try putting a gif in a devlog again(Edit: I have replaced it with a video(Edit yet again: It seems that stardance REFUSES to let me put in any functional media besides a still image, so I suppose that is what I must put)).

It turns out, temperature is kind of hard to add! I sorta had to re-examine most of what I’ve been doing so far in order to get it remotely stable.
I’ve also somewhat standardized the units used in the various equations in my code, so that my values somewhat make sense.
One of the things that I realized after doing this is that gases would realistically accelerate to absurd speeds in a vacuum, so I have had to make each cell much larger (now they are each ~1 meter in size).
Of course the main reason I had to re-examine the logical consistency of my code was because I wanted to add temperature to the simulation. Though it would not be accurate to call it ‘temperature’; instead it would be ‘heat’. Really just the internal energy of the gas particles. It turns out that the pressure of a gas in a given area can be written to be directly proportional to the internal energy of the gas in that area, with no regard for the number of particles(it makes sense if you think about it for a while). This pretty much lead me to add in a new value to each cell to correspond to the total internal energy of all of the gas in the cell. This value is advected in the exact same way as the amount of each gas in each cell (which is what is visualized). As far as I can tell through my reading, the three quantities that are relevant to propagate and conserve if I want a decent simulation are mass, momentum, and energy. Out of those I think the most important for me to conserve are mass and energy (I’m not really giving any regard to conservation of momentum at the moment, though I am tracking it for each cell to allow for better gas-gas interactions). One thing to note is that the current simulation doesn’t really have energy conservation, as the acceleration of the gasses provoked by pressure differences sort of comes from no where. This being said, I think I might be able to just call the kinetic energy negligible to the thermal energy? I’m not sure, and will need to do some number crunching I think. I think I’ll want to add in some temperature visualization so I can get some idea of what is going on (and to see if it’s remotely realistic!). Afterwards I think I’ll want to look into adding more types of gases(really this means generalizing each gas and allowing for an infinite quantity of gases).

For every hour I spend on this project I feel like I spend at least half an hour reading (Which hackatime of course does not track). This is also an especially rambly devlog, so if you’re genuinely curious about what the hell is going on and I did a bad job explaining, post a comment and I am very happy to attempt to explain.

0
0
7
Open comments for this post

47m 51s logged

Adding another gas was relatively trivial lmao. Technically I realized partway in that I was somewhat jumping the gun but eh this is okay.
I realized partway through that the fact I was tracking flow velocity is a bit sketch when multiple gases of different molar masses are interacting, so I replaced it with momentum. Pretty much heavier gases should influence lighter gases more, but lighter gases will move much faster.
The second gas is visualized as green on the shader, and is like fake O2. The only difference between the gases currently is a higher molar mass(O2 would be 16x heavier!). The only noticeable difference in how the behave at the moment is that the O2 is denser, thereby being accelerated slower and having higher momentum.

Next up should be temperature. technically the temperature of a gas should change as its pressure changes. Though the changing of temperature will also effect its pressure, so that might be a bit hard to balance. We’ll see!

2
1
135
Open comments for this post

1h 35m 19s logged

Bulk flow as been added! Now it kind of behaves like a gas lmfao.

Edit: Unfortunately it seems like stardance does not allow for gifs 😭. I’ve replaced it with a static image.

Pretty much, each cell has it’s own flow velocity vector and gets a
force applied to it if it has neighbors with a lower pressure than its.

granted, there are a good few issues with the approach. For one, as far
as I know from my previous CFD (computational fluid dynamics) reading,
this method of giving individual cells their own flow velocity is
somewhat unstable. Also my method of advection (moving the cell
quantities around depending on their velocity) is forward advection,
which is also known to be quite unstable.

Another issue with my current algorithm is that to keep the fluid in the
grid I clamp which cells it can access. I’m not sure how well that
explains it, but it has the effect of making the fluid clump up and
build high pressures in corners (as can be seen in the gif below).
technically the fluid should be bouncing off of the walls.

One last issue: At this point I pretty much have the basics of an actual
fluid simulation, and fluid simulations are SUPER LAGGY when done in
the cpu! The resolution in the gif below is a tenth of what was in the
last devlog, as at 500x500 cells my cpu simply can’t keep up. Luckily I
already know the solution for this: Parallelization on the GPU! That
being said I’m not sure I’ll bother going down that rabbit hole just
yet…

Next up I think i’ll do my best to improve the advection algorithm a
little bit, then I’ll actually add more gases to this simulation (that
is, kind of the point of this lmao). Maybe I’ll add in some UI after
that (going to be hard convincing myself against going down the rabbit hole of
rending my own text 💀)

I guess I will note that I am basing this initial gas off of molecular
hydrogen. The only real thing that that effects in the current state of
the simulation is the molar mass of the gas. Also each cell should be
sized at ~1 cm in size.

0
0
25
Open comments for this post

1h 3m 41s logged

Spent quite a bit of time re-remembering how to code shaders. Though it didn’t take too much time as I could read and sample my past shader code.

None of the simulation code has been changed lmao, it’s still only basic diffusion. However, now that a proper shader has been made I can instantly afford to run much higher resolutions without a billion unity gizmos being spawned and lagging out my computer.

Shown below is about the same thing I showed yesterday, but at 100x the resolution! It can probably go much higher if I tried but it doesn’t really matter too much.

Up next is adding proper pressure simulation & bulk flow! technically the whole space should be a vacuum right now so really it should get filled really quickly with the ‘gas’.

0
0
20
Open comments for this post

44m 43s logged

First I started by spending quite a bit of time refreshing myself on fluid dynamics and getting somewhat overwhelmed. I had to remind myself that for the purposes of this I Don’t really need to bother 100% accurately simulate everything in a fluid, not even close.

As stated in the project description now, I have decided to focus this project on compressible flows (really just simulating gases), as that seems like a better place to start than working with liquids and solutions in liquids (but I do want to end up there eventually!).

The first while here will be spent just getting pseudo ‘gases’ flowing through a grid. All that I threw together in the first hour or so was effectively a grid where values are diffused from one cell to another. What is shown below is a very simple & temporary visual display made via unity gizmos of that happening. I’ll be throwing together a shader to properly display it promptly (which will likely be made up of recycled code from my previous fluid simulation, as this is pretty much the same thing lmao).

1
0
27

Delete project?

Are you sure you want to permanently delete this project? This action cannot be undone.

All devlogs, followers, and associated data will be removed.

Followers

Loading…