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#.
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.
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.