I wish to write a simple simulation of an ideal gas, but my maths and how I should have a model based the equation isn't quite up to it. I need some help understanding the relationship between pressure, the amount of the gas, and the temperature (the volume is a constant), not just at one moment, but as one or more of the variables change...

Right now, I have global variables for $P$, $T$ and $n$, and an update loop in my code where I check to see if $T$ has changed. If so, I can solve the ideal gas equation $P = nRT/V$ as the others are unchanged. The results look ok to me. I can warm the tank, and the pressure rises.

Now, if I add or remove some gas, by changing $n$, I am unsure how to change both $P$ and $T$. If I just recalculate $P$, I can remove gas until the pressure falls to $0$, yet the temp remains constant the whole time. This doesn't seem right, shouldn't there be some temperature drop too?

Can you explain how I should go about improving this simulation?

  • 2
    $\begingroup$ I don’t consider what you are doing to be a “simulation” of an ideal gas. To me, a simulation would be one in which you simulate a lot of atoms moving around in a box, bouncing off the walls and each other. They have masses, positions, and velocities. The macroscopic thermodynamic stuff then gets calculated from the microscopic dynamics. $\endgroup$
    – G. Smith
    Jun 19, 2019 at 22:32
  • $\begingroup$ Perhaps a poor choice of words. It is a black box, where the user can read the pressure, read and adjust temperature, and add or remove gas. Maybe "model" is a better term? $\endgroup$
    – Innovine
    Jun 19, 2019 at 22:38
  • $\begingroup$ OK, I understand now what you are trying to do. $\endgroup$
    – G. Smith
    Jun 19, 2019 at 22:40

2 Answers 2


Now, if I add or remove some gas, by changing n, I am unsure how to change both P and T. If I just recalculate P, I can remove gas until the pressure falls to 0, yet the temp remains constant the whole time. This doesn't seem right, shouldn't there be some temperature drop too?

Let the container of the ideal gas be rigid ($V$= constant) and thermally insulated so that there is no heat transfer between the gas in the container and the surroundings, and no change in volume, when you remove some the gas. Under these conditions if you remove some gas from the container then, yes, the temperature should drop too.

Rearranging the ideal gas equation we have


If we hold $V$ constant, and remove say, one half of the moles of gas, then since $R$ is constant the ratio of the new pressure to temperature will also be one half of what it was prior to removal of the gas in order to continue to satisfy the ideal gas equation.

Another way to think about it is when you remove some of the gas from the container, you also remove some of the internal energy that it possessed in the container, thus lowering the internal energy contents of the container. For an ideal gas the internal energy per mole is directly proportional to its temperature according to


Since the internal energy of an ideal gas depends only on its temperature, if we reduce the internal energy of the gas by one half by removing one half of the gas, the temperature will be reduced by one half as well. Since the volume is fixed, that means the pressure is reduced by one half as well to satisfy the ideal gas equation.

Hope this helps.

  • $\begingroup$ What is U here? $\endgroup$
    – Innovine
    Jun 19, 2019 at 22:06
  • $\begingroup$ Typical symbol for Internal energy $\endgroup$
    – Bob D
    Jun 19, 2019 at 22:09
  • $\begingroup$ Is it correct to say then that P, T and n all decrease in a linear fashion until they all reach 0 at the same time? $\endgroup$
    – Innovine
    Jun 19, 2019 at 22:13
  • $\begingroup$ @Innovine In theory perhaps, but it is generally acknowledged that absolute zero temperature is not possible thermodynamically. It is my understanding that scientists can experimentally achieve temperatures within a few degrees of absolute zero, but not absolute zero. The lowest temperature in outer space is the cosmic microwave background radiation which according to NASA is 2.725 K. $\endgroup$
    – Bob D
    Jun 20, 2019 at 12:48
  • $\begingroup$ Thanks, I can fudge it as it approaches zero for sure. Just wanted to verify I had it right in general $\endgroup$
    – Innovine
    Jun 20, 2019 at 12:51

You can't use equilibrium states with the ideal gas law to model a change in the moles of gas because equilibrium assumes a closed system with constant mass, but that is clearly not the case here so you will have to use an open system to figure how $T$ and $P$ change with $n$. I will show an example with an ideal gas leaving a vessel with constant volume $V$ to answer your question in the comments that $P$ and $T$ do not decrease linearly.

You need an energy balance and a material balance around a control volume (i.e. the tank) and let's assign mass/energy entering the tank be positive and leaving be negative:

$\frac{dn_{cv}U_{cv}}{dt} = -H_{cv}*\dot{n_{out}} \\ \frac{dn_{cv}}{dt}=-\dot{n_{out}}$

In the first equation you can eliminate $n_{cv}U_{cv}$ using:

$nU = nH - nPV$

and eliminate $n_{out}$ using the material balance above to get:

$\frac{d(nH_{cv}-nPV)}{dt} = H_{out}*\frac{dn_{cv}}{dt}$

Then after simplifying and by substituting $dH$ with $C_pdT$, and letting $\gamma = \frac{C_p}{R}$ you get:


Finally, use the material balance to eliminate $dt$ and after some rearranging you will get:

$\frac{dT}{T}=(\gamma-1)\frac{dn_{cv}}{n_{cv}}$ and integrating from initial to final states yields:


So that equation will tell you how the temperature drops as moles of ideal gas are leaving your system. Finally you can relate pressure and temperature by substituting $PV=nRT$ for the moles and after rearranging you will get:




So as you can see, the temperature and pressure do not decrease at the same rate and you cannot simply use the ideal gas to solve this problem because the moles of gas leaving are carrying enthalpy (energy) with them as they leave.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.