Jump to content
  • Advertisement
Sign in to follow this  

HALP! Simultaneous Equations with Chemistry and Physics Simulation

This topic is 552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites

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. 

Share this post

Link to post
Share on other sites

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.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!