Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

HALP! Simultaneous Equations with Chemistry and Physics Simulation

4: Adsense
  • You cannot reply to this topic
2 replies to this topic

#1 nullie   Members   


Posted 20 March 2017 - 09:33 PM

As part of one of my projects, there is a /chemistry/ simulator. It's not intended to be exact to real-life chemistry, but is still a fair bit more complex than just "A + B = C" This is being done in an Object-Oriented programming language similar to java and c#.

Some background: 
Before I started working on the project:


The container was an instantiated object. It contained a variable, with an array of instantiated chemicals that *existed in the container*.
The chemicals themselves were instantiated objects, with a reference to their container, and a volume.

Whenever the container was interacted with, (such as a new chemical added), the function would instantiate a new chemical, add linking references, and update the volume of the chemical. Then it would iterate through *EVERY* chemical in the container to check for possible reactions using a lookup table, and if a match was found, it would run the reaction, and restart. This included a potential for infinitely 'reacting' chemicals, but the workaround was just careful implementation of recipes to prevent infinite loops.


Now i saw this, and was absolutely disgusted. And changed it to something more akin to:


In this model, the container has a 2D array of the chemical, and it's volume inside of the container.
The chemical is only ever instantiated ONCE, and if a chemical is accessed, the properties of the master chemical are read.

(Each unique chemical has static properties in both models, and the only dynamic variable was *volume*)

Then when the contents of the container are modified, it is *flagged* to receive an update, iterates through the contents, performs all currently possible reactions unless that reaction was already performed during this update. If any reactions occured, it would flag the container to be updated *next* process. This removed the potential for problems caused be recursive reactions, but prevented instantaneous consecutive reactions.


The problem!

NOW, i want to add some more realism and complexity to the system, as well as make it more efficient if possible. I want to iterate as infrequently as possible.

containers now store the following variables and properties regarding its chemical contents:

Quantity - The quantity of a chemical within. Equivalent to moles.
Thermal Energy - The total thermal energy of chemicals within. Equivalent to joules.
Volume - The volume of the container. This is the *SAME* as the total volume of the container's contents. But the volume of each chemical should also be tracked separately. ie. 50 unit container contains 30 units of AIR and 1 unit of OIL and 19 units of WATER. This should be calculated based on the quantity of each substance in the container, the temperature, and the total volume of the container. As well as some data based on the individual chemical's properties.
Temperature - This is calculated based on the thermal energy, and the chemical properties of the chemicals within. (IE. Temperature = Total thermal energy / Total specific heat capacity of chemicals.)
Pressure - This is calculated based on volume, temperature, quantity, and some chemical properties.


What is the most efficient way for me to *add* or *remove* a chemical from the container?

For example "I add 30 units of water. This displaces the gas in the container, and modifies the specific heat and thermal energy of the container. As a result, modifying the temperature, volume, and pressure of the container contents." 

How can i adjust all relevant container values using *just* the values before the water is added, and the properties of the water? What additional information might i need to store? 

The volume of the contents of the container is *ALWAYS* equal to the volume of the container. This means that if the container is sealed, and contains 30 units of water @ standard temperature, but the volume of the container is *larger* than the volume of liquid water. How do i find the equilibrium point for the volume of *liquid* water and water *vapour* using only temperature, quantity, total volume, and a single extra variable/coefficient for vapour pressure?

How do i quickly and efficiently calculate that, when there are say, n other chemicals inside the container with different properties, that also need to be in equilibrium?


Are there any faster and more efficient methods i can use to look-up all possible reaction matches, and perform the reactions, so that the reactions are in equilibrium?

IE if i have : "1 water + 1 agent A = volatile acid" and "1 water + 1 agent B = volatile explosive" in the recipe list, and i have:
"1 water, 1 agent A, and 1 agent B" in the container,

How can i make sure that i end up with 25% acid, 25% explosive, 25% A, 25% B, instead of 50% acid, 50% agent B, with as few iterations as possible?

In the case that "acid" and "explosive" react together to form 3 parts agent C and 1 part water, How can i make the reaction find equilibrium in only one update, with as few iterations as possible? 

Edited by nullie, 21 March 2017 - 01:07 AM.


#2 between3and26characters   Members   


Posted 14 April 2017 - 06:13 PM

If the space is fine grained, you could try random action. Throw dice, 1-3 makes acid, 4-6 makes explosive. Just as quantum physics. This should give it some reality. If their masses are not integer but any ratio, then you would need to alter the probability with it so 1-2 acid 3-6 explosive is B is more. Something exponential should do the trick.


Enable finer grained sub 2D-map for the mixed cells. 

Need an open-source multi-gpu OpenCL load-balancer for C#? Here it is= https://github.com/tugrul512bit/Cekirdekler/wiki it also has pipelining.

Hello world in all GPUs:

ClNumberCruncher cr = new ClNumberCruncher(
    AcceleratorType.GPU, @"
      __kernel void hello(__global char * arr)
           printf(""hello world"");
ClArray<byte> array = new ClArray<byte>(1000);
array.compute(cr, 1, "hello", 1000, 100); 

#3 Mats1   Members   


Posted 14 April 2017 - 08:17 PM

This all seems a bit vaguely complex. What factors are you supposed to allow for here? e.g. Are you supposed to change reaction rates based on temperature and pressure? Are you supposed to say how much of each chemical is in what state (solid, liquid, gas...) and adapt rates appropriately? Is this supposed to be some kind of simulation of Le Chatelier's principle and take into account endothermic and exothermic reactions?

n the case that "acid" and "explosive" react together to form 3 parts agent C and 1 part water, How can i make the reaction find equilibrium in only one update, with as few iterations as possible?

I don't think we have enough information. Are you supposed to be adapting the reaction rate over time, to react to changes in pressure, temperature, concentration etc... Or are all reaction rates supposed to be (unrealistically) constantly occuring? Reactions at a constant rate would be very easy and could use the simulated dice rolls suggested above. Reactions at a variable rate are much harder. If you wanted to calculate in one step, you could use some calculus to obtain your answer.