Program Summary
Program Title: equilibrium-c
CPC Library link to program files: (to be added by Technical Editor)
Developer’s repository link:
github.com/uqngibbo/equilibrium-c
Code Ocean capsule: (to be added by Technical Editor)
Licensing provisions: MIT
Programming language: C, Python
Nature of problem:
equilibrium-c solves the reacting thermochemistry equations to find the equilibrium composition for a given thermodynamic state. The code is open-source and has been developed with a minimal dependencies philosophy. equilibrium-c be used on its own via a python interface or embedded into a more elaborate multi-physics code that may require chemical equilibrium as a submodule.
Solution method:
The equations are solved using a modified Newton’s method for multidimensional nonlinear equations, with analytic derivatives for the Jacobian. These modifications consist of an underrelaxation factor for each update, as well as careful handling of small species and a novel convergence metric.
Additional comments:
Emphasis is placed, in the paper, on reacting supersonic flow, owing to the nature of the author’s primary research interest. However, the code is applicable to a diverse range of applications in research and beyond. Readers who are interested in pursuing these applications are encouraged to contact the author via the corresponding email address.
E-mail address: n.gibbons@uq.edu.au
Introduction
Many research problems involving rockets, high-speed aeroplanes, and spacecraft reentering the atmosphere encounter fluid flows with chemical reactions. These reactions affect the dynamics and thermochemistry of the flow, and must be considered in modelling calculations in order to compute accurate values of drag, heat transfer, or radiation that are needed for engineering design. In some situations, considerable time and complexity may be saved in these calculations by assuming the gas is in chemical equilibrium: The state that the gas would reach in the limit of a large time relative to the reaction time constants, as in figure 1.
Figure 1. Chemically reacting flow passing through a shockwave.
The equilibrium composition of a gas depends only on its thermodynamic state, which means a dedicated computer program for computing it would be valuable across a wide range of applications, from hypersonic wind-tunnel design to research in combustion and surface chemistry. In addition, computer programs designed for scientific research have a range of desiderata beyond their functional capabilities: they should be open source and freely available, have good documentation that explains their structure and algorithmic design, they should be verified and validated, preferably using automated tests that can be run by a user, and they should be easy to install and have minimal dependencies. One of the earliest efforts to provide a program for equilibrium calculations was STANJAN [1 ] , a Fortran-77 code that introduced the Method of Element Potentials. This method considers the equilibrium calculation as a constrained non-linear optimisation problem using Lagrange multipliers, a formulation that can handle complex mixtures and a wide range of initial conditions, and for these reasons it was adopted by most subsequent programs. Some further development of the method was introduced in Steven Pope’s ceq [2 ] , written in Fortran-90, which implemented a procedure
called “Gibbs function continuation” that aimed to improve convergence in the case of linear constraints on the final species composition. Element potentials were also adopted in NASA’s Chemical Equilibrium with Applications (CEA) [3 ] , a widely used Fortran-77 code and associated database developed at NASA’s Lewis Research Centre, with a most recent release (CEAv2) from 2002. CEA has found widespread use in the aerospace research community, as in addition to solving chemical equilibrium problems, it also provides a range of application modes for solving problems such as rocket nozzles or shockwaves. More recent work in this space includes the equilibrium solver in the C++ combustion toolkit Cantera [4 ] , and a Python toolkit called EQTK [5 ] which was developed for dilute liquid phase calculations needed for DNA and RNA research.
This brief review demonstrates that there is a broad interest in chemical equilibrium solvers for enabling various fields of applied research, but also serves to highlight some of the problems with the existing programs. STANJAN, ceq, and CEA are essentially un-maintained, and their use of old-school FORTRAN and text-based input files make them difficult for modern programs to interface with. Cantera is modern and reasonably well maintained, though its large size and project complexity means that linking it to existing code is also difficult. EQTK is lighter in weight and has good design and documentation, though its intended field of application is dilute liquid chemistry, and thus it does not use the Element Potential method that was developed to handle the very hot gas phase flows encountered in hypersonics.
To try and address these shortcomings, this paper introduces a new program for the calculation of chemical equilibrium problems, called equilibrium-c or eqc for short. eqc is a re-implementation of the same equations solved by CEA, using an updated numerical method and a focus on the non-functional desiderata of a good research code. The program is open-sourced under the permissive MIT license and can be freely downloaded from the project’s github page. It requires minimal dependencies (basic development libraries, a C compiler, and python3-numpy), works on both Linux and Windows, and can be compiled into a dynamic library for easy embedding in high performance multiphysics codes. It comes with a dozen unit tests which are continuously integrated whenever new changes are made, but can also be run by any user to verify that the implementation is functioning as advertised. Finally, it is documented in this paper and in a number of example scripts, some of which serve to verify the code against experimental data.
Thermodynamics of Chemical Equilibrium
The program described in this paper follows the element potential method by Gordon and McBride [3 ] , in which we seek an equilibrium state by minimising the value of a certain thermodynamic potential. For a problem at fixed temperature and pressure, the relevant potential would be the Gibbs energy G = U + p V − T S G = U + pV - TS G = U + p V − T S , or for a fixed internal energy and volume problem, the potential would be the Helmholtz energy F = U − T S F = U - TS F = U − T S . These potentials change as the composition of the gas is varied, even if the rest of the thermodynamic state is kept fixed, which allows the equilibrium state to be obtained via constrained minimisation, with the constraints provided by the initial composition and the requirement of elemental conservation.
The basic theory of this process is presented in many textbooks, which would ordinarily preclude a derivation in a publication such as this one; however, the author has found that almost always this material is presented in a way that is hand-wavy or otherwise unsatisfying. Gordon and McBride [3 ] , for example, offer no explanation whatsoever for their formulation. Turns and Haworth [6 ] , one of the best selling undergraduate textbooks on combustion theory, merely introduce the derivatives of the Gibbs energy as a mysterious “chemical potential”, as if such a definition were an explanation for why the method works. A slightly better approach is given Anderson [7 ] , who introduces an analytic form of the second law of thermodynamics that includes a term for non-equilibrium or irreversible entropy change.
d S = p d V + d E T + d S i r r e v (1) dS = \frac{p\,dV + dE}{T} + dS_{\mathrm{irrev}} \tag*{(1)} d S = T p d V + d E + d S irrev (1)
Putting aside, for the moment, any curiosity about where this equation comes from, we can proceed to rearrange it to isolate the irreversible entropy change, and then introduce differential changes in the pressure V d p Vdp V d p and S d T S\,dT S d T .
T d S − p d V − d E = d S i r r e v T\,dS - p\,dV - dE = dS_{\mathrm{irrev}} T d S − p d V − d E = d S irrev
T d S + S d T − S d T − p d V + V d p − V d p − d E = d S i r r e v T\,dS + S\,dT - S\,dT - p\,dV + V\,dp - V\,dp - dE = dS_{\mathrm{irrev}} T d S + S d T − S d T − p d V + V d p − V d p − d E = d S irrev
d ( T S ) + S d T − d ( p V ) − V d p − d E = d S i r r e v d(TS) + S\,dT - d(pV) - V\,dp - dE = dS_{\mathrm{irrev}} d ( T S ) + S d T − d ( p V ) − V d p − d E = d S irrev
d ( T S − p V − E ) + S d T − V d p = d S i r r e v d(TS - pV - E) + S\,dT - V\,dp = dS_{\mathrm{irrev}} d ( T S − p V − E ) + S d T − V d p = d S irrev
The differential term in brackets is minus the Gibbs energy, and assuming a constant pressure and temperature process, we seem to be entitled to assume d p dp d p and d T dT d T are equal to zero. These simplifications lead to the final expression as follows.
− d G = d S i r r e v (2) -dG = dS_{\mathrm{irrev}} \tag*{(2)} − d G = d S irrev (2)
This derivation appears to show that the change in the Gibbs energy for a gas mixture at constant pressure and temperature is equal to the change in irreversible entropy. Anderson asserts that these irreversibilities “are caused chemical nonequilibrium during the process”, but “irreversible” is just thermodynamics jargon for “non-equilibrium”, so this explanation offers no insight into why this is the case or where equation 1 actually comes from.
To understand why minimising the Gibbs energy results in chemical equilibrium, or indeed why chemistry has to do with thermodynamics in the first place, we must start with the principles of statistical mechanics that underpin the more familiar laws of classical thermodynamics. To begin with, we follow chapter 11 of Anderson [7 ] and consider a box of uniform gas molecules with volume V V V , without the complexity of reactions for the moment. This gas will consist of a large number of individual particles with different energies, which are constantly colliding with each other and exchanging energy through various means, though these energies can be added up to get a total energy E E E which is conserved in the melee of collisions.
To develop a statistical model for the distribution of particle energies, we need one major starting assumption, which is that the energy states available to the particles are quantized . This means that there are a finite (but very large) number of different energy states, and that these states are occupied by individual particles for brief moments of time as they rattle around the box. A schematic depiction of this is shown in Figure 2 .
Figure 2. Illustration of particles and energy levels in an ideal gas.
In this figure, the energy levels are shown with a certain number of particles occupying them, for example there are three particles occupying the 5th energy level (N 5 = 3 N_5 = 3 N 5 = 3 ). The complete distribution of these occupation numbers N j N_j N j is of interest to us for many reasons, since they can be summed over to give the total number of particles, as well as the total energy.
N = ∑ j N j E = ∑ j ε j N j N = \sum_j N_j \qquad E = \sum_j \varepsilon_j N_j N = j ∑ N j E = j ∑ ε j N j
Where ε j \varepsilon_j ε j is the amount of energy in the jth energy level. A given distribution of N j N_j N j is called a macrostate , for example, the macrostate shown in figure 2 is N = [ 0 , 0 , 1 , 1 , 0 , 3 , 1 , 2 , 0 , … ] N = [0, 0, 1, 1, 0, 3, 1, 2, 0, \ldots] N = [ 0 , 0 , 1 , 1 , 0 , 3 , 1 , 2 , 0 , … ] . If we were to take particle 4 and move it down one energy level we would get a different macrostate, namely N = [ 0 , 0 , 1 , 1 , 1 , 2 , 1 , 2 , 0 , … ] N = [0, 0, 1, 1, 1, 2, 1, 2, 0, \ldots] N = [ 0 , 0 , 1 , 1 , 1 , 2 , 1 , 2 , 0 , … ] . Importantly, if we were to take particle 4 and swap it with particle 3, we would not change the overall set of occupation numbers, meaning that the macrostate would be the same. This implies that there may be multiple ways of arranging a set of particles into a given macrostate. Indeed, a quick calculation shows that there are 8 ! / 3 ! / 2 ! = 3360 8!/3!/2!=3360 8 ! /3 ! /2 ! = 3360 ways of arranging the 8 particles into the macrostate in figure 2. These individual arrangements are called microstates , and though they are physically identical in terms of energy, the number of available microstates affect the probability of each macrostate showing up the random dance of molecular collisions.
To further understand this point, an analogy may be helpful. Consider rolling a pair of six-sided dice and adding the results together, as is done in many board games. A pair of 1s gives a total of two, and a pair of 6s gives a total of twelve, the two rarest totals, each with only one permutation of individual rolls that can make it. A total of three however, can be achieved with a 1 and a 2, or a 2 and a 1, making it twice as likely. The number of permutations increases as the totals increase up to seven, which can be made with 1&6, 2&5, 3&4, 4&3, 5&2, 6&1, making it much more likely to appear than the one or the twelve, as can be seen in table 2.
Table 1. Totals (macrostates) resulting from a pair of six-sided dice rolls.
In this analogy, the totals are the macrostates, and the individual dice roll combinations are the microstates. Since the seven has the most microstates it is the most likely total to appear in a random pair of rolls, and we might say it is the most probable macrostate.
This analogy extends to the particles of gas in the box. Some sets of occupation numbers will have more microstates associated with them than others, and the most likely macrostate is of interest for predicting the behaviour of the collective. Anderson [7 ] provides an expression for the number of microstates W W W involved in any given macrostate or set of occupation numbers N j N_j N j .
W = ∏ j ( N j + g j − 1 ) ! ( g j − 1 ) ! N j ! (3) W = \prod_j \frac{(N_j+g_j-1)!}{(g_j-1)!N_j!} \tag*{(3)} W = j ∏ ( g j − 1 )! N j ! ( N j + g j − 1 )! (3)
This equation introduces the degeneracy, or statistical weight of an energy level g j g_j g j . Degeneracies arise due to the fact that multiple distinct quantum states may have the same energy, such as the angular momentum of a particle in different directions. To extend our analogy from before, imagine instead of rolling a pair of six-sided dice with exactly one of each number on the faces, we were instead to roll a pair of twenty sided dice with some duplicates of the numerals 1-6 on them. When counting the microstates then, we would have to account for the number of duplicates to determine the most likely total.
Equation 3 can be used to determine the most likely macrostate via mathematical optimisation. First, we choose to work with the logarithm of W W W rather than the actual value, as its maximum will occur at the same place, and the corresponding sum of terms is easier to deal with than a product.
ln ( W ) = ∑ j ln ( N j + g j − 1 ) ! − ln ( g j − 1 ) ! − ln ( N j ) ! \ln(W) = \sum_j \ln(N_j+g_j-1)! - \ln(g_j-1)! - \ln(N_j)! ln ( W ) = j ∑ ln ( N j + g j − 1 )! − ln ( g j − 1 )! − ln ( N j )!
Next we assume that the energy levels can be lumped into a succession of quasi-levels with similar energy levels. This leads to each state having a large number of occupying particles and a large number of degeneracies, N j ≫ 1 N_j \gg1 N j ≫ 1 and g j ≫ 1 g_j \gg1 g j ≫ 1 .
ln ( W ) = ∑ j ln ( N j + g j ) ! − ln ( g j ) ! − ln ( N j ) ! \ln(W) = \sum_j \ln(N_j+g_j)! - \ln(g_j)! - \ln(N_j)! ln ( W ) = j ∑ ln ( N j + g j )! − ln ( g j )! − ln ( N j )!
We can then employ Stirling’s approximation to remove the factorials: ln ( a ! ) = a ln ( a ) − a \ln(a!) = a\ln(a) - a ln ( a !) = a ln ( a ) − a .
ln ( W ) = ∑ j [ N j ln ( 1 + g j N j ) + g j ln ( N j g j + 1 ) ] \ln(W) = \sum_j \left[N_j\ln\left(1+\frac{g_j}{N_j}\right) + g_j\ln\left(\frac{N_j}{g_j}+1\right)\right] ln ( W ) = j ∑ [ N j ln ( 1 + N j g j ) + g j ln ( g j N j + 1 ) ]
This equation can be maximised to determine the most probable macrostate, the set of N j N_j N j ’s with the most number of microstates. To do this we again follow Anderson [7 ] , who employs the method of Lagrange multipliers to maximise subject to a constrained number of total particles N N N and total energy E E E .
N = ∑ j N j N = \sum_j N_j N = j ∑ N j
E = ∑ j ε j N j E = \sum_j \varepsilon_j N_j E = j ∑ ε j N j
Skipping the intermediate steps, the result of the minimisation is as follows, introducing Lagrange multipliers α \alpha α and β \beta β .
N j ∗ = g j e α e β ε j N_j^* = \frac{g_j}{e^{\alpha}e^{\beta\varepsilon_j}} N j ∗ = e α e β ε j g j
α \alpha α can be removed from this expression by introducing the constraint of total N N N , and it can be shown that β \beta β is the inverse of the quantity normally referred to as the temperature (or in physical units, the Boltzmann constant k k k times the temperature). This leads to an expression for the most probable macrostate N j ∗ N_j^* N j ∗ :
N j ∗ = N g j e − ε j / k T ∑ i g i e − ε i / k T (4) N_j^* = N \frac{g_j e^{-\varepsilon_j/kT}}{\sum_i g_i e^{-\varepsilon_i/kT}} \tag*{(4)} N j ∗ = N ∑ i g i e − ε i / k T g j e − ε j / k T (4)
This is the famous Boltzmann distribution. The Boltzmann distribution is interesting because although it underpins much of classical thermodynamics, there is no fundamental law of physics that compels a system to exist or remain in it. As we have seen it is simply one configuration out of countless others, though it is the one with the most microstates that can manifest it.
In order to understand why this distribution is so important, consider again the rolling of a pair of six-sided dice, but this time we should think of the average of the two rolls, which is merely the total divided by two. The most likely average of a given pair is three-and-a-half, corresponding to the sum of seven discussed earlier, though a circumspect gambler would not be terribly surprised to find either a one (from a pair of 1’s) or a six (from a pair of 6’s) should arise from time to time.
Consider however, what happens to the average as the number of dice rolls increases. With three dice there are now 6 3 = 216 6^3 = 216 6 3 = 216 total permutations, but still only one way of rolling an average of one, i.e. three 1’s, so extreme values have become less likely, and for the same reason, the intermediate values have become more likely. The general formula for the number of permutations associated with a given total when rolling n n n six sided dice is given in Appendix A, which has been used to generate Figure 3 , in which the discrete probability functions of different n n n values are plotted with both axes normalised.
Figure 3. Discrete PDFs of multiple dice rolls, normalised across domain and range.
This figure demonstrates an important property of additive probabilistic systems that is related to the Law of Large Numbers. This property is that, as the number of degrees of freedom (dice, in this example) increases, the PDF contracts about the mean value, making extreme values less and less likely and the near-mean values more and more likely. With one thousand dice, the chances of getting the most probable macrostate (a total of exactly 3500 for a mean of 3.5) is 0.738 %, but the chances of getting approximately the macrostate, say 3.5 ± \pm ± 0.1 is ≈ \approx ≈ 93.6 %. With a million dice rolls, the chances of being within half a percent of the mean are 99.99997 %.
The number of particles making up a hot gas is, of course, much larger even than this. And so when we do thermodynamics, we can bet that the gas will be in the mostly likely macrostate, or any of the huge number of nearly identical nearby ones. Thanks to the astronomical number of particles in the system, and Law of Large numbers, this bet will be right every time.
Having understood why the most probable macrostate is so important, we now want to connect the statistical mechanics expressions considered so far into the more familiar ones of classical thermodynamics. To compute the properties of a system at the most probable macrostate (i.e. the thermodynamic equilibrium), we must evaluate ln ( W ) \ln(W) ln ( W ) using the expression for N j ∗ N_j^* N j ∗ . (Recall that N j ∗ N_j^* N j ∗ is the set of occupation numbers that define the most probable macrostate). This results in ln ( W m a x ) \ln(W^{max}) ln ( W ma x ) , the log of the number of microstates included with the most probable macrostate. ln ( W m a x ) \ln(W^{max}) ln ( W ma x ) is a number which has a very special place in thermodynamics, we call it the entropy.
S = k B ln ( W max ) (5) S = k_B \ln\left(W^{\max}\right) \tag*{(5)} S = k B ln ( W m a x ) (5)
Combining equations 4 and 3 gives the following expression for ln ( W max ) \ln\left(W^{\max}\right) ln ( W m a x ) .
ln ( W max ) = ∑ j N j ∗ [ ln g j N j + 1 ] \ln\left(W^{\max}\right) = \sum_j N_j^* \left[\ln\frac{g_j}{N_j} + 1\right] ln ( W m a x ) = j ∑ N j ∗ [ ln N j g j + 1 ]
Substituting equation 4 results in a simple expression with the summation over states removed.
ln ( W max ) = N [ ln Q N + 1 ] + E k T (6) \ln\left(W^{\max}\right) = N\left[\ln\frac{Q}{N} + 1\right] + \frac{E}{kT} \tag*{(6)} ln ( W m a x ) = N [ ln N Q + 1 ] + k T E (6)
This introduces the partition function Q Q Q , which is defined as Q = ∑ j g j e − ε j / k T Q = \sum_j g_j e^{-\varepsilon_j/kT} Q = ∑ j g j e − ε j / k T . The specifics of the partition function are important for actually evaluating a statistical mechanics problem, but for the purposes of this derivation we can just note that Q Q Q is a function of both the volume and the temperature Q ( V , T ) Q(V,T) Q ( V , T ) . Simplifying equation 6 further, we can swap W max W^{\max} W m a x for S S S and get an expression that is very close to the standard thermodynamics-style definition of entropy.
S = k N [ ln Q ( V , T ) N + 1 ] + E ( V , T ) T (7) S = kN\left[\ln\frac{Q(V,T)}{N} + 1\right] + \frac{E(V,T)}{T} \tag*{(7)} S = k N [ ln N Q ( V , T ) + 1 ] + T E ( V , T ) (7)
This expression is a function of two independent variables, though exactly which two can be chosen for convenience. In the following section, we will be interested in the derivative of this expression, which will allow us to track the entropy as the properties of the system change. Considering T T T and V V V as independent variables, the infinitesimal difference in entropy is:
d S = ∂ S ∂ T ∣ V d T + ∂ S ∂ V ∣ T d V dS = \left.\frac{\partial S}{\partial T}\right|_V dT + \left.\frac{\partial S}{\partial V}\right|_T dV d S = ∂ T ∂ S V d T + ∂ V ∂ S T d V
Considering first term, it can be shown that (see Appendix B).
∂ S ∂ T ∣ V = ∂ E ∂ T ∣ V 1 T (8) \left.\frac{\partial S}{\partial T}\right|_V = \left.\frac{\partial E}{\partial T}\right|_V \frac{1}{T} \tag*{(8)} ∂ T ∂ S V = ∂ T ∂ E V T 1 (8)
The V V V derivative of the entropy is slightly more involved. It involves the pressure p p p , which in statistical mechanics is defined as minus the derivative of the energy with respect to volume, at constant entropy.
p = − ∂ E ∂ V ∣ S (9) p = -\left.\frac{\partial E}{\partial V}\right|_S \tag*{(9)} p = − ∂ V ∂ E S (9)
This, rather abstract, definition is a kind of generalised pressure that reduces to the familiar quantity in the typical case. It can be used to show (see Appendix C) that:
∂ S ∂ V ∣ T = p T + ∂ E ∂ V ∣ T 1 T (10) \left.\frac{\partial S}{\partial V}\right|_T = \frac{p}{T} + \left.\frac{\partial E}{\partial V}\right|_T \frac{1}{T} \tag*{(10)} ∂ V ∂ S T = T p + ∂ V ∂ E T T 1 (10)
Putting these equations together we end up with a familiar expression for the second law of thermodynamics.
d S = 1 T [ p d V + ∂ E ∂ V ∣ T d V + ∂ E ∂ T ∣ V d T ] dS = \frac{1}{T}\left[p\,dV + \left.\frac{\partial E}{\partial V}\right|_T dV + \left.\frac{\partial E}{\partial T}\right|_V dT\right] d S = T 1 [ p d V + ∂ V ∂ E T d V + ∂ T ∂ E V d T ]
d S = p d V + d E T (11) dS = \frac{p\,dV + dE}{T} \tag*{(11)} d S = T p d V + d E (11)
We are now in a position to understand exactly what this expression means. Since we have used N j ∗ N_j^* N j ∗ to derive it, d S dS d S apparently refers to the change in equilibrium entropy, the extra microstates that get added to the most probable macrostate due to slow changes in the system volume and temperature. If these changes are done rapidly, the system may wobble away from this equilibrium state temporarily, but in time it will eventually arrive there.
So far we have been considering a single species gas, to try and get a clear picture of what thermodynamic equilibrium is and how it is calculated. In order to derive the chemical equilibrium equations we will need to consider a mixture of species, but thankfully the equations are just a linear combination of those already established. Again following Anderson [7 ] , we can derive the following expression for the number of microstates associated with a specific macrostate, where the s s s index runs over the different chemical species in the mixture.
ln ( W ) = ∑ s ∑ j N j s [ ln g j s N j s + 1 ] \ln(W) = \sum_s \sum_j N_j^s \left[\ln\frac{g_j^s}{N_j^s} + 1\right] ln ( W ) = s ∑ j ∑ N j s [ ln N j s g j s + 1 ]
The most probable macrostate is then.
N j s ∗ = N s Q s g j s e − ε j s / k T (12) N_j^{s*} = \frac{N^s}{Q^s}g_j^s e^{-\varepsilon_j^s/kT} \tag*{(12)} N j s ∗ = Q s N s g j s e − ε j s / k T (12)
And ln ( W max ) \ln(W^{\max}) ln ( W m a x ) is therefore.
ln ( W max ) = ∑ s N s [ ln Q s N s + 1 ] + E s k T (13) \ln(W^{\max}) = \sum_s N^s\left[\ln\frac{Q^s}{N^s} + 1\right] + \frac{E^s}{kT} \tag*{(13)} ln ( W m a x ) = s ∑ N s [ ln N s Q s + 1 ] + k T E s (13)
It is not immediately obvious from this expression, but at this point we have run into a problem which was not present for the single species gas. Previously, we considered the total number of particles N N N to be a known constant, introduced into the minimisation process as a constraint via a Lagrange multiplier. But for the multi-species case, what is known is not the individual species totals N s N^s N s , but the total number of atoms of each element in the initial mixture. For example, in a simple binary dissociation reaction A + B ⇌ A B A+B \rightleftharpoons AB A + B ⇌ A B , the constraint would be formulated as.
N A + N A B = N A N^{A}+N^{AB}=N_{A} N A + N A B = N A
N B + N A B = N B N^{B}+N^{AB}=N_{B} N B + N A B = N B
It is as if we have maximised the individual species macrostates, but not the macrostate considering the entire mixture collectively. Nonetheless, the same logic from before applies: The state of thermodynamic equilibrium occurs at the macrostate with the most microstates, and that means we need to perform another maximisation, this time using equation 13, to find the set of N s N_s N s that maximise the total W W W . The equilibrium condition therefore occurs at the stationary point of W W W , computed using N s N_s N s as independent variables and with the thermodynamic state fixed.
0 = ∑ s ∂ ln ( W max ) ∂ N s ∣ E , V d N s (14) 0=\sum_s\left.\frac{\partial\ln(W^{\max})}{\partial N_s}\right|_{E,V}dN_s \tag*{(14)} 0 = s ∑ ∂ N s ∂ ln ( W m a x ) E , V d N s (14)
At this point we could go ahead and solve equation 14 for an equilibrium state at fixed volume and total energy. However, this kind of thermodynamic state is unusual, and not terribly helpful for gas dynamics calculations, which usually use intensive variables with more physical meaning such as the temperature and pressure. We will therefore introduce a helpful trick that makes the problem more tractable. Consider the following equation for the differential change in ln W \ln W ln W , which considers changes to both the thermodynamic state and composition.
d ln W max = ∂ ln W max ∂ V ∣ E , N s d V + ∂ ln W max ∂ E ∣ V , N s d E + ∑ s ∂ ln W max ∂ N s ∣ E , V d N s d\ln W^{\max}=\left.\frac{\partial\ln W^{\max}}{\partial V}\right|_{E,N_s}dV+\left.\frac{\partial\ln W^{\max}}{\partial E}\right|_{V,N_s}dE+\sum_s\left.\frac{\partial\ln W^{\max}}{\partial N_s}\right|_{E,V}dN_s d ln W m a x = ∂ V ∂ ln W m a x E , N s d V + ∂ E ∂ ln W m a x V , N s d E + s ∑ ∂ N s ∂ ln W m a x E , V d N s
The first two terms on the right hand side are the change in disorder affected by the changing thermodynamic state. The final term is the change in disorder associated with chemical change, and looking at equation 14, we can see that the condition for equilibrium is that it is zero. Rearranging to isolate this term we get.
d ln W max − ∂ ln W max ∂ V d V − ∂ ln W max ∂ E d E = ∑ s ∂ ln W max ∂ N s d N s (15) d\ln W^{\max}-\frac{\partial\ln W^{\max}}{\partial V}dV-\frac{\partial\ln W^{\max}}{\partial E}dE=\sum_s\frac{\partial\ln W^{\max}}{\partial N_s}dN_s \tag*{(15)} d ln W m a x − ∂ V ∂ ln W m a x d V − ∂ E ∂ ln W m a x d E = s ∑ ∂ N s ∂ ln W m a x d N s (15)
Note carefully the conceptual interpretation of this equation. The right hand side is the disorder associated with chemical nonequilibrium, which is the part we are interested in solving. But the left hand side is the difference between the total disorder change and the disorder change attributed solely to the thermodynamic state change. By finding a composition where this difference is zero, we can determine the equilibrium state without ever having to actually differentiate ln ( W max ) \ln(W^{\max}) ln ( W m a x ) with respect to the composition, which is how a chemical equilibrium solver can do its work using only standard thermodynamic quantities. More formally, this amounts to solving for the following expression:
d ln W max − ∂ ln W max ∂ V d V − ∂ ln W max ∂ E d E = 0 (16) d\ln W^{\max}-\frac{\partial\ln W^{\max}}{\partial V}dV-\frac{\partial\ln W^{\max}}{\partial E}dE=0 \tag*{(16)} d ln W m a x − ∂ V ∂ ln W m a x d V − ∂ E ∂ ln W m a x d E = 0 (16)
We can assume that the individual species are in their respective Boltzmann distributions, and so ln W max \ln W^{\max} ln W m a x is really S / k S/k S / k , the total entropy divided by the Boltzmann constant.
d S − ∂ S ∂ V d V − ∂ S ∂ E d E = 0 dS-\frac{\partial S}{\partial V}dV-\frac{\partial S}{\partial E}dE=0 d S − ∂ V ∂ S d V − ∂ E ∂ S d E = 0
The rest of the derivation is the same as for the single species gas, and we end up with a slightly rearranged copy of equation 11.
d S − p d V + d E T = 0 (17) dS-\frac{p\,dV+dE}{T}=0 \tag*{(17)} d S − T p d V + d E = 0 (17)
The derivation given by given by Anderson [7 ] begins at this point, starting with the second law expression expression for entropy change and asserting that the right hand is zero in a reversible or equilibrium process. We are now in a position to understand what this means: Equation 17 is also the stationary point of the mixture entropy with respect to composition. And that is the condition of chemical equilibrium.
To complete our derivation, we must show that minimising the Gibbs energy reduces to equation 17. The Gibbs energy is a thermodynamic potential that measures the maximum amount of work that can be extracted from a closed system at constant temperature and pressure, defined as follows.
G = E + p V − T S (18) G=E+pV-TS \tag*{(18)} G = E + p V − T S (18)
G G G can be expressed as sum of individual species, using the number of particles (in moles) of each species N s N_s N s and the species energy and entropy per mole E s E_s E s and S s S_s S s .
G = ∑ s E s N s + p V − ∑ s T S s N s G=\sum_s E_sN_s+pV-\sum_s TS_sN_s G = s ∑ E s N s + p V − s ∑ T S s N s
Using the ideal gas law p V = N R u T pV = NR_uT p V = N R u T , and noting that N = ∑ s N s N = \sum_s N_s N = ∑ s N s , the p V pV p V term can also be removed.
G = ∑ s E s N s + R u T ∑ s N s − ∑ s T S s N s G = \sum_s E_sN_s + R_uT\sum_s N_s - \sum_s TS_sN_s G = s ∑ E s N s + R u T s ∑ N s − s ∑ T S s N s
G = ∑ s [ E s + R u T − T S s ] N s (19) G = \sum_s [E_s + R_uT - TS_s]N_s \tag*{(19)} G = s ∑ [ E s + R u T − T S s ] N s (19)
The bracketed term in equation 19 is the Gibbs energy per mole of species s s s , and can be defined as:
G s ≡ E s + R u T − T S s (20) G_s \equiv E_s + R_uT - TS_s \tag*{(20)} G s ≡ E s + R u T − T S s (20)
Leading to a tidy formula:
G = ∑ s G s N s (21) G = \sum_s G_sN_s \tag*{(21)} G = s ∑ G s N s (21)
We are interested in a stationary point of G G G , the point at which d G dG d G is equal to zero in every direction in species space.
d G = ∑ s ∂ G ∂ N s d N s = 0 (22) dG = \sum_s \frac{\partial G}{\partial N_s}dN_s = 0 \tag*{(22)} d G = s ∑ ∂ N s ∂ G d N s = 0 (22)
Evaluating the partial derivatives of G G G with respect to N s N_s N s is actually a little bit involved, because of the entropy S s S_s S s depending on the complete set of of N s N_s N s ’s. The full derivation of this expression is given in Appendix D, but the end result is a surprisingly tidy expression where all of the extra terms from the product rule cancel to get:
∂ G ∂ N s = G s \frac{\partial G}{\partial N_s} = G_s ∂ N s ∂ G = G s
Continuing with equation 22, we expand using the definition of G s G_s G s , equation 20 to get:
d G = ∑ s [ E s + R u T − T S s ] d N s = 0 dG = \sum_s [E_s + R_uT - TS_s]dN_s = 0 d G = s ∑ [ E s + R u T − T S s ] d N s = 0
Note that, since E = N s E s E = N_sE_s E = N s E s , d E = E s d N s dE = E_sdN_s d E = E s d N s , and likewise d S = S s d N s dS = S_sdN_s d S = S s d N s . Slightly more complicated is the following expression for d V dV d V coming again from the ideal gas law:
V = R u T p ∑ s N s V = \frac{R_uT}{p}\sum_s N_s V = p R u T s ∑ N s
∴ d V = R u T p ∑ s d N s \therefore dV = \frac{R_uT}{p}\sum_s dN_s ∴ d V = p R u T s ∑ d N s
These expressions allow us to remove the sums over d N s dN_s d N s as follows.
d G = ∑ s E s d N s + R u T ∑ s d N s − T ∑ s S s d N s = 0 dG = \sum_s E_sdN_s + R_uT\sum_s dN_s - T\sum_s S_sdN_s = 0 d G = s ∑ E s d N s + R u T s ∑ d N s − T s ∑ S s d N s = 0
0 = d E + p d V − T d S 0 = dE + pdV - TdS 0 = d E + p d V − T d S
d E + p d V − d S dE + pdV
- dS d E + p d V − d S
This is almost, but not quite the same as equation 17. The final step is to flip the signs by multiplying the expression by negative one, to get:
0 = d S − d E + p d V T (23) 0 = dS - \frac{dE + pdV}{T} \tag*{(23)} 0 = d S − T d E + p d V (23)
Incidentally, this sign flip is why minimising G G G corresponds to maximising disorder, and the equivalence of the two expressions is why minimising G G G is the same as finding the state of equilibrium.
Governing Equations
The methodology used by eqc to solve for chemical equilibrium is based off of Gordon and McBride [3 ] . Here we present a derivation of the equations for solving a constrained pressure and temperature problem, with other problem types given in the Appendices. We will be interested in minimising the Gibbs Energy G G G , but we will begin by dividing it by the total mixture mass ρ V \rho V ρ V , to get the Gibbs energy per unit mass g g g . This quantity is more convenient to work with, and will have its stationary point at the same place because the total mass ρ V \rho V ρ V is constant.
g = G ρ V = N s ρ V G s = n s G s g = \frac{G}{\rho V} = \frac{N_s}{\rho V}G_s = n_sG_s g = ρ V G = ρ V N s G s = n s G s
This expression introduces the unusual intensive quantity n s n_s n s , the number of moles of species s s s present per unit mixture mass. As far as the author is aware this quantity does not have a standardised name, however the term specific molarity seems appropriate. Its relationship to the more familiar measures of composition such as molar concentration c s c_s c s , mass fraction Y s Y_s Y s , and mole fraction X s X_s X s are as follows.
n s = N s ρ V : c s = ρ n s : Y s = M s n s : X s = n s n n_s = \frac{N_s}{\rho V}
\quad: \quad
c_s = \rho n_s
\quad: \quad
Y_s = M_sn_s
\quad: \quad
X_s = \frac{n_s}{n} n s = ρ V N s : c s = ρ n s : Y s = M s n s : X s = n n s
This last equation uses the sum of the specific molarities n = ∑ s n s n = \sum_s n_s n = ∑ s n s , which is also notable as being the inverse of of the mixture molar mass n = 1 / M n = 1/M n = 1/ M .
We will need the Gibbs energy in its explicit form, with all of the terms expanded out. Equation (20) is the definition of G s G_s G s , which gives the following.
g = ∑ s n s G s = ∑ s n s [ E s + R u T − T S s ] g = \sum_s n_s G_s = \sum_s n_s \left[E_s + R_uT - TS_s\right] g = s ∑ n s G s = s ∑ n s [ E s + R u T − T S s ]
Tabulated values of the internal energy per mole E s E_s E s are available from the NASA Lewis thermodynamic database, which have been computed at a standard pressure of 100, 000 Pa or 1 BAR. This standard state is sometimes written as E s ∘ E_s^\circ E s ∘ , however, we will exclusively by considering thermally perfect gases where E s E_s E s has no dependency on pressure, so E s = E s ∘ E_s = E_s^\circ E s = E s ∘ in general. The same is unfortunately not true of the entropy S s S_s S s , which does vary with pressure. The molar entropy of species s s s at an arbitrary pressure can be written as follows, using the proof in Appendix § 13 .
S s = S s ∘ − R u ln ( n s n ) − R u ln ( p p ∘ ) (24) S_s = S_s^\circ- R_u \ln\left(\frac{n_s}{n}\right) - R_u \ln\left(\frac{p}{p^\circ}\right) \tag*{(24)} S s = S s ∘ − R u ln ( n n s ) − R u ln ( p ∘ p ) (24)
S s ∘ S_s^\circ S s ∘ can be computed using the Lewis tables, which assume p ∘ = 100 , 000 p^\circ= 100, 000 p ∘ = 100 , 000 Pa. The fully expanded expression for minimisation is then as follows.
g = ∑ s n s [ E s + R u T − T S s ∘ + R u T ln ( n s n ) + R u T ln ( p p ∘ ) ] g = \sum_s n_s \left[E_s + R_uT - TS_s^\circ+ R_uT \ln\left(\frac{n_s}{n}\right) + R_uT \ln\left(\frac{p}{p^\circ}\right)\right] g = s ∑ n s [ E s + R u T − T S s ∘ + R u T ln ( n n s ) + R u T ln ( p ∘ p ) ]
Note that the first three terms in the bracket of this equation are constant throughout the minimisation process, and are, on a closer inspection, just the Gibbs energy at the standard conditions. To save some ink and space then, our final step will be to define G s ∘ ≡ E s + R u T − T S s ∘ G_s^\circ\equiv E_s + R_uT - TS_s^\circ G s ∘ ≡ E s + R u T − T S s ∘ , and then we can write the objective function to be minimised as follows.
g = ∑ s n s [ G s ∘ + R u T ln ( n s n ) + R u T ln ( p p ∘ ) ] (25) g = \sum_s n_s \left[G_s^\circ+ R_uT \ln\left(\frac{n_s}{n}\right) + R_uT \ln\left(\frac{p}{p^\circ}\right)\right] \tag*{(25)} g = s ∑ n s [ G s ∘ + R u T ln ( n n s ) + R u T ln ( p ∘ p ) ] (25)
Minimising this expression is a non-linear constrained optimisation problem in n s n_s n s . The constraints are the available elements in some initial composition n s 0 n_s^0 n s 0 , which can be expressed using a matrix of elemental constituents a j s a_{js} a j s , the number of atoms of element j j j in species s s s . As an example, consider the dissociation of C O 2 \mathrm{CO_2} C O 2 .
C O 2 ⇌ C O + 1 2 O 2 \mathrm{CO_2} \rightleftharpoons\mathrm{CO} + \frac{1}{2}\mathrm{O_2} C O 2 ⇌ CO + 2 1 O 2
This problem contains only two atomic elements, C C C , and O O O , and the constraint matrix would be as follows.
caption:
:::
Given an initial composition n s 0 n_s^0 n s 0 then, the constraint equation can be written.
a j s n s − a j s n s 0 = 0 (26) a_{js}n_s - a_{js}n_s^0 = 0 \tag*{(26)} a j s n s − a j s n s 0 = 0 (26)
Following Gordon and McBride [3 ] , the minimisation process proceeds using the method of Lagrange multipliers, which introduces Lagrange multipliers λ j \lambda_j λ j , one for each atomic element in the mixture, and for each row in the matrix a j s a_{js} a j s . Defining the Lagrangian L \mathcal{L} L as:
L = ∑ s n s [ G s ∘ + R u T ln ( n s n ) + R u T ln ( p p ∘ ) ] + ∑ j λ j ( ∑ s a j s n s − a j s n s 0 ) (27) \mathcal{L} = \sum_s n_s \left[G_s^\circ+ R_uT \ln\left(\frac{n_s}{n}\right) + R_uT \ln\left(\frac{p}{p^\circ}\right)\right] + \sum_j \lambda_j \left(\sum_s a_{js}n_s - a_{js}n_s^0\right) \tag*{(27)} L = s ∑ n s [ G s ∘ + R u T ln ( n n s ) + R u T ln ( p ∘ p ) ] + j ∑ λ j ( s ∑ a j s n s − a j s n s 0 ) (27)
Solving the optimisation problem then becomes a matter of seeking the set of n s n_s n s and λ j \lambda_j λ j that satisfy ∂ L / ∂ n s = 0 \partial\mathcal{L}/\partial n_s = 0 ∂ L / ∂ n s = 0 and ∂ L / ∂ λ j = 0 \partial\mathcal{L}/\partial\lambda_j = 0 ∂ L / ∂ λ j = 0 simultaneously. This set of equations can be derived by differentiating equation 27 with respect to each unknown. This derivation is given in Appendix § 14 , with the final results shown in equations 4 - 30 below.
G s ∘ R u T + ln ( n s n ) + ln ( p p ∘ ) + ∑ j λ j a j s R u T = 0 (28) \frac{G_s^\circ}{R_uT} + \ln\left(\frac{n_s}{n}\right) + \ln\left(\frac{p}{p^\circ}\right) + \sum_j \lambda_j \frac{a_{js}}{R_uT} = 0 \tag*{(28)} R u T G s ∘ + ln ( n n s ) + ln ( p ∘ p ) + j ∑ λ j R u T a j s = 0 (28)
( s = 0 , … , n s ) (s = 0, \ldots, n_s) ( s = 0 , … , n s )
∑ s a j s n s − a j s n s 0 = 0 (29) \sum_s a_{js}n_s - a_{js}n_s^0 = 0 \tag*{(29)} s ∑ a j s n s − a j s n s 0 = 0 (29)
( j = 0 , … , n e ) (j = 0, \ldots, n_e) ( j = 0 , … , n e )
We also introduce the apparently redundant linear equation for n n n , the sum of the unknown molarities. This is because we will want to treat n n n as a separate unknown to be solved along with the individual species ones, which simplifies the resulting expressions and adds some robustness to the numerical method for solving them.
∑ s n s − n = 0 (30) \sum_s n_s - n = 0 \tag*{(30)} s ∑ n s − n = 0 (30)
( j = 0 , … , n e ) (j = 0, \ldots, n_e) ( j = 0 , … , n e )
Numerical Method
The basic algorithm used to solve equations 4-29 is a generalisation of the familiar Newton’s method. For a single nonlinear equation f ( x ) = 0 f(x)=0 f ( x ) = 0 , this method begins by taking an initial guess of the unknown variable x 0 x_0 x 0 , then computes the gradient of the objective function f f f at that point d f / d x ( x 0 ) df/dx(x_0) df / d x ( x 0 ) , and finally descends to the x-axis in a straight line to find a new guess x 1 x_1 x 1 . This process is repeated until an error criterion drops below a specified tolerance, at which point the value x n x_n x n should be extremely close to the actual root.
When solving a coupled system of nonlinear equations, a multidimensional generalisation of Newton’s method exists that can be employed in an analogous way. Figure 4 visualises one step of this process, using a pair of concave example functions f 1 ( x , y ) f_1(x,y) f 1 ( x , y ) and f 2 ( x , y ) f_2(x,y) f 2 ( x , y ) that are plotted in blue and red respectively.
Figure 4.
A given guess for the independent variables x 0 x_0 x 0 and y 0 y_0 y 0 produces a pair of errors f 1 ( x 0 , y 0 ) f_1(x_0,y_0) f 1 ( x 0 , y 0 ) and f 2 ( x 0 , y 0 ) f_2(x_0,y_0) f 2 ( x 0 , y 0 ) which are represented by the blue and red circular markers. The root of the two equations is point where both functions are equal to zero and equal to each other, which is shown in the figure as the green star-shaped marker. To update an initial guess in 2D we first differentiate both functions at the point x 0 , y 0 x_0,y_0 x 0 , y 0 , which produces a tangent plane for each function that is centred on each of the coloured markers. Each of these planes cuts through the z = 0 z=0 z = 0 plane in a line rather than a point, and any position along each line would be a better guess for the zero of that function, though not necessarily a better guess for the entire coupled system of equations. What the multidimensional Newton’s method actually does is choose the point where the two lines intersect, marked with a green circle in figure 4, as the next iteration x 1 , y 1 x_1,y_1 x 1 , y 1 , which will tend to be closer to the actual root for sufficiently well behaved functions. The method can be formalised in any number of dimensions by considering the first order Taylor series expansion of a multivariate function f f f , one of a set of functions we would like to simultaneously solve.
d f = ∂ f ∂ x 0 d x 0 + ∂ f ∂ x 1 d x 1 + … ∂ f ∂ x n d x n df = \frac{\partial f}{\partial x_0}dx_0 + \frac{\partial f}{\partial x_1}dx_1 + \ldots\frac{\partial f}{\partial x_n}dx_n df = ∂ x 0 ∂ f d x 0 + ∂ x 1 ∂ f d x 1 + … ∂ x n ∂ f d x n
Geometrically, this expression defines a tangent plane to the function at a given point x \mathbf{x} x , by proscribing a linear variation d f df df for an arbitrary movement in the dependant variables d x d\mathbf{x} d x . The dotted lines in figure 4 are obtained by descending all the way to the x-y axis, i.e. d f = − f ( x 0 , x 1 , x 2 , … ) df=-f(x_0,x_1,x_2,\ldots) df = − f ( x 0 , x 1 , x 2 , … ) .
− f ( x 0 , x 1 , x 2 , … ) = ∂ f ∂ x 0 d x 0 + ∂ f ∂ x 1 d x 1 + … ∂ f ∂ x n d x n -f(x_0,x_1,x_2,\ldots) = \frac{\partial f}{\partial x_0}dx_0 + \frac{\partial f}{\partial x_1}dx_1 + \ldots\frac{\partial f}{\partial x_n}dx_n − f ( x 0 , x 1 , x 2 , … ) = ∂ x 0 ∂ f d x 0 + ∂ x 1 ∂ f d x 1 + … ∂ x n ∂ f d x n
For a collection of m functions, we are interested in the point where the dotted lines meet, which defines a linear system.
− f 0 ( x 0 , x 1 , x 2 , … ) = ∂ f 0 ∂ x 0 d x 0 + ∂ f 0 ∂ x 1 d x 1 + … ∂ f 0 ∂ x n d x n − f 1 ( x 0 , x 1 , x 2 , … ) = ∂ f 1 ∂ x 0 d x 0 + ∂ f 1 ∂ x 1 d x 1 + … ∂ f 1 ∂ x n d x n ⋮ − f m ( x 0 , x 1 , x 2 , … ) = ∂ f m ∂ x 0 d x 0 + ∂ f m ∂ x 1 d x 1 + … ∂ f m ∂ x n d x n \begin{aligned}
-f_0(x_0,x_1,x_2,\ldots) &= \frac{\partial f_0}{\partial x_0}dx_0 + \frac{\partial f_0}{\partial x_1}dx_1 + \ldots\frac{\partial f_0}{\partial x_n}dx_n \\
-f_1(x_0,x_1,x_2,\ldots) &= \frac{\partial f_1}{\partial x_0}dx_0 + \frac{\partial f_1}{\partial x_1}dx_1 + \ldots\frac{\partial f_1}{\partial x_n}dx_n \\
&\vdots\\
-f_m(x_0,x_1,x_2,\ldots) &= \frac{\partial f_m}{\partial x_0}dx_0 + \frac{\partial f_m}{\partial x_1}dx_1 + \ldots\frac{\partial f_m}{\partial x_n}dx_n
\end{aligned} − f 0 ( x 0 , x 1 , x 2 , … ) − f 1 ( x 0 , x 1 , x 2 , … ) − f m ( x 0 , x 1 , x 2 , … ) = ∂ x 0 ∂ f 0 d x 0 + ∂ x 1 ∂ f 0 d x 1 + … ∂ x n ∂ f 0 d x n = ∂ x 0 ∂ f 1 d x 0 + ∂ x 1 ∂ f 1 d x 1 + … ∂ x n ∂ f 1 d x n ⋮ = ∂ x 0 ∂ f m d x 0 + ∂ x 1 ∂ f m d x 1 + … ∂ x n ∂ f m d x n
Abstracting this system of equations into matrix form produces the familiar expression for the multidimensional Newton’s method that can be found in many textbooks, where the matrix of partial derivations is often given the label J J J for Jacobian.
− F ( x i ) = J ( x ) Δ x i (31) -\mathbf{F}(\mathbf{x}_i)=J(\mathbf{x})\Delta\mathbf{x}_i \tag*{(31)} − F ( x i ) = J ( x ) Δ x i (31)
In this case the vector F \mathbf{F} F is the combination of equations 4 and 29 stacked on top of each other, and x i \mathbf{x}_i x i is the vector of unknowns we are trying to solve for. Clearly a random guess at this vector will not be a solution of equations 4 and 29, which is to say that it will give a non-zero remainder on the right-hand side. However, by starting with such a guess and iterating over i i i using equation 31, these remainders can be reduced with each iteration until values of x i x_i x i are correct to within a specified precision. A slight complicating factor is that the magnitudes of the molarities can vary dramatically between species, so we actually want to solve for the natural logarithms of the composition ln ( n s ) \ln(n_s) ln ( n s ) and ln ( n ) \ln(n) ln ( n ) . This affects the Jacobian matrix J J J , which is made of the following first derivatives.
∂ F s ∂ ln ( n s ) = 1 ∂ F s ∂ ln ( n ) = − 1 ∂ F s ∂ λ j = a j s R u T \frac{\partial F_s}{\partial\ln(n_s)}=1 \qquad\frac{\partial F_s}{\partial\ln(n)}=-1 \qquad\frac{\partial F_s}{\partial\lambda_j}=\frac{a_{js}}{R_uT} ∂ ln ( n s ) ∂ F s = 1 ∂ ln ( n ) ∂ F s = − 1 ∂ λ j ∂ F s = R u T a j s
∂ F j ∂ ln ( n s ) = a j s n s ∂ F j ∂ ln ( n ) = 0 ∂ F j ∂ λ k = 0 \frac{\partial F_j}{\partial\ln(n_s)}=a_{js}n_s \qquad\frac{\partial F_j}{\partial\ln(n)}=0 \qquad\frac{\partial F_j}{\partial\lambda_k}=0 ∂ ln ( n s ) ∂ F j = a j s n s ∂ ln ( n ) ∂ F j = 0 ∂ λ k ∂ F j = 0
∂ F n ∂ ln ( n s ) = n s ∂ F n ∂ ln ( n ) = − n ∂ F n ∂ λ k = 0 \frac{\partial F_n}{\partial\ln(n_s)}=n_s \qquad\frac{\partial F_n}{\partial\ln(n)}=-n \qquad\frac{\partial F_n}{\partial\lambda_k}=0 ∂ ln ( n s ) ∂ F n = n s ∂ ln ( n ) ∂ F n = − n ∂ λ k ∂ F n = 0
In these equations, the expression F s F_s F s is used to represent one instance of equation 1, the expression F j F_j F j is used to represent an instance of equation 29, and the expression F n F_n F n means equation 30. These are the set of coupled nonlinear equations we wish to solve. Gordon and McBride [3 ] then actually perform the matrix multiplication by J J J analytically to get what they call the Gibbs iteration equations, beginning with F s F_s F s .
Δ ln ( n s ) − Δ ln ( n ) + ∑ j a s j Δ λ j R u T = − G s R u T − ∑ j λ j a j s R u T \Delta\ln(n_s)-\Delta\ln(n)+\sum_j a_{sj}\frac{\Delta\lambda_j}{R_uT}=-\frac{G_s}{R_uT}-\sum_j\frac{\lambda_j a_{js}}{R_uT} Δ ln ( n s ) − Δ ln ( n ) + j ∑ a s j R u T Δ λ j = − R u T G s − j ∑ R u T λ j a j s
Before we get to the rest of the Gibbs iteration equations, this one deserves some attention. The first thing to note about it is that the Lagrange multipliers appear linearly in the update equation, as well as linearly in the original Lagrangian. Since a Newton’s method will always solve a linear set of equations in one step, we can exempt the Lagrange multipliers from the iteration process by guessing that they are zero at the beginning of each step, and recognising that Δ λ j \Delta\lambda_j Δ λ j is the correct and final value of the Lagrange multiplier needed to implement the constraints. A second point to note is that Lagrange multipliers are arbitrary non-physical quantities, that can always be redefined for our convenience.
Following Gordon and McBride [3 ] then, we set λ j = 0 \lambda_j=0 λ j = 0 and redefine − Δ λ j / R u T ≡ π j -\Delta\lambda_j/R_uT\equiv\pi_j − Δ λ j / R u T ≡ π j . This gives Gordon and McBride [3 ] ’s equation 2.18, and the other Gibbs equations are straightforwardly derived in the same manner.2
Δ ln ( n s ) − Δ ln ( n ) − ∑ j a s j π j = − G s R u T (32) \Delta\ln(n_s)-\Delta\ln(n)-\sum_j a_{sj}\pi_j=-\frac{G_s}{R_uT} \tag*{(32)} Δ ln ( n s ) − Δ ln ( n ) − j ∑ a s j π j = − R u T G s (32)
∑ s a s j n s Δ ln ( n s ) = ∑ s a j s n s 0 − a j s n s (33) \sum_s a_{sj}n_s\Delta\ln(n_s)=\sum_s a_{js}n_s^0-a_{js}n_s \tag*{(33)} s ∑ a s j n s Δ ln ( n s ) = s ∑ a j s n s 0 − a j s n s (33)
∑ s n s Δ ln ( n s ) − n Δ ln ( n ) = n − ∑ s n s (34) \sum_s n_s\Delta\ln(n_s)-n\Delta\ln(n)=n-\sum_s n_s \tag*{(34)} s ∑ n s Δ ln ( n s ) − n Δ ln ( n ) = n − s ∑ n s (34)
Equation 32 provides one expression for each species involved with the calculation, equation 33 one for each chemical element, and equation 34 one more to implement the total n n n constraint. These equations would be sufficient to solve for the equilibrium composition at fixed temperature and pressure, however, a keen observer may have noticed that equation 32 contains only a single unknown Δ ln ( n s ) \Delta\ln(n_s) Δ ln ( n s ) which could be computed directly if the Δ ln ( n ) \Delta\ln(n) Δ ln ( n ) and π j \pi_j π j were known. Gordon and McBride [3 ] suggests exploiting this structure by using equation 32 to get an expression for each Δ ln ( n s ) \Delta\ln(n_s) Δ ln ( n s ) , and substituting them into the other two expressions to get what they call the Reduced Gibbs Iteration equations.
∑ i ∑ s a j s a i s n s π i + ∑ s a j s n s Δ ln ( n ) = ∑ s a j s n s 0 − a j s n s + ∑ s a j s n s G s R u T (35) \sum_i\sum_s a_{js}a_{is}n_s\pi_i+\sum_s a_{js}n_s\Delta\ln(n)=\sum_s a_{js}n_s^0-a_{js}n_s+\sum_s\frac{a_{js}n_sG_s}{R_uT} \tag*{(35)} i ∑ s ∑ a j s a i s n s π i + s ∑ a j s n s Δ ln ( n ) = s ∑ a j s n s 0 − a j s n s + s ∑ R u T a j s n s G s (35)
∑ i ∑ s a i s n s π i + ( ∑ s n s − n ) Δ ln ( n ) = n − ∑ s n s + ∑ s n s G s R u T (36) \sum_i\sum_s a_{is}n_s\pi_i+\left(\sum_s n_s-n\right)\Delta\ln(n)=n-\sum_s n_s+\sum_s\frac{n_sG_s}{R_uT} \tag*{(36)} i ∑ s ∑ a i s n s π i + ( s ∑ n s − n ) Δ ln ( n ) = n − s ∑ n s + s ∑ R u T n s G s (36)
These are the actual equations solved by eqc to get the equilibrium composition at fixed temperature and pressure. There are n e + 1 n_e+1 n e + 1 of them, where n e n_e n e is the number of chemical elements involved in the reactions, which is a major reduction of the original system which had an additional one for each species. Having solved for Δ ln ( n ) \Delta\ln(n) Δ ln ( n ) and the Lagrange multipliers π j \pi_j π j , equation 32 is used to directly obtain Δ ln ( n s ) \Delta\ln(n_s) Δ ln ( n s ) , which is used to update the specific molarities, and a new iteration begins. This process is continued until the system of equations converges, or perhaps, does not.
Numerical Convergence and Robustness
The basic algorithm described so far is capable of solving many chemical equilibrium problems, but early testing of the solver revealed many cases where convergence failed, either due to numerical instability in the algorithm or because of oscillation in the solution that eventually hit the maximum step limit. This subsection describes a handful additions to the method that were needed for robust convergence, and taken together allow eqc to solve a comprehensive range of equilibrium problems, including combustion for propulsion applications, high temperature ionisation for re-entry strength shockwaves, and facility calculations in reflected shock tunnels and expansion tubes.
The first addition is similar to a fix suggested by Gordon and McBride [3 ] : an under-relaxation factor on the unknown variable updates. In pressure-temperature problems, this consists of limiting the updates to n n n and n s n_s n s by a factor Λ \Lambda Λ , which is defined using a fraction of the current guess of the total specific molarity n n n .
Λ = min ( 1.0 , 1 2 ∣ ln ( n ) ∣ ∣ Δ ln ( n s ) ∣ ) (37) \Lambda= \min\left(1.0,\frac{1}{2}\frac{|\ln(n)|}{|\Delta\ln(n_s)|}\right) \tag*{(37)} Λ = min ( 1.0 , 2 1 ∣Δ ln ( n s ) ∣ ∣ ln ( n ) ∣ ) (37)
The fraction 1 / 2 1/2 1/2 is an adjustable factor that seems to work well for most purposes. The equations for updating the unknown molarities from iteration i i i to i + 1 i+1 i + 1 are then:
ln ( n s ) i + 1 = ln ( n s ) i + Λ Δ ln ( n s ) (38) \ln(n_s)^{i+1} = \ln(n_s)^i + \Lambda\,\Delta\ln(n_s) \tag*{(38)} ln ( n s ) i + 1 = ln ( n s ) i + Λ Δ ln ( n s ) (38)
ln ( n ) i + 1 = ln ( n ) i + Λ Δ ln ( n ) (39) \ln(n)^{i+1} = \ln(n)^i + \Lambda\,\Delta\ln(n) \tag*{(39)} ln ( n ) i + 1 = ln ( n ) i + Λ Δ ln ( n ) (39)
For problems where the gas temperature is also an unknown, such as fixed density and internal energy (ρ e \rho e ρ e ) problems (see Appendix § 15 and Appendix H), Λ \Lambda Λ is also applied to the temperature update, though it is computed using ∣ ln ( T ) ∣ |\ln(T)| ∣ ln ( T ) ∣ instead of ∣ ln ( n ) ∣ |\ln(n)| ∣ ln ( n ) ∣ .
T i + 1 = exp ( ln ( T ) i + Λ Δ ln ( T ) ) (40) T^{i+1} = \exp\left(\ln(T)^i + \Lambda\,\Delta\ln(T)\right) \tag*{(40)} T i + 1 = exp ( ln ( T ) i + Λ Δ ln ( T ) ) (40)
Notice that in these update equations, the actual first class unknown is ln ( n s ) \ln(n_s) ln ( n s ) . This is important enough to deserves some extended discussion.
The number one cause of numerical difficulties in solving the equilibrium equations is that, very often, one more species compositions can become extremely small. This can lead to the matrix in the Newton method becoming ill-conditioned and difficult to solve. Solving for the logarithms of the compositions fixes this problem at the cost of introducing a new one, which is that at precisely n s = 0 n_s = 0 n s = 0 , the logarithm is undefined.3
Gordon and McBride [3 ] deal with this problem by automatically detecting when a species’s molarity has dropped below a trace value and then removing it from the calculation, but this adds complexity and can cause convergence problems when a species’s equilibrium concentration is near the trace limit, since it may oscillate between being present and absent. In this work I have developed the following algorithm for ensuring convergence.
At the beginning of the calculation, the elemental constraint vector X 0 X_0 X 0 is used to set an initial guess for the molarities, with a floor to prevent n s n_s n s being zero.
for (s=0; s<nsp; s++){
ns[s] = fmax(X0[s]/M0, n*1e-4);
}
Once set to known nonzero values, the species molarities are used to compute the logarithmic species molarities.
for (s=0; s<nsp; s++){
lnns[s] = log(ns[s]);
}
This is the only time that the log function is called. For the rest of the algorithm, lnns is treated as the first class unknown, from which n s n_s n s may be computed, but never the other way around.
At the end of each iteration of the Newton’s method, Δ ln ( n s ) \Delta\ln(n_s) Δ ln ( n s ) is computed using equation 38, and then n s n_s n s is computed using the exponential function.
for (s=0; s<nsp; s++){
ns[s] = exp(lnns[s]);
}
At around ln n s = − 746 \ln n_s=-746 ln n s = − 746 , this call to the exponential function will underflow, giving n s = 0.0 n_s=0.0 n s = 0.0 . However, as long as the logarithmic value is still defined, the floating point math will correctly give n s × ln ( n s ) = 0.0 n_s \times\ln(n_s)=0.0 n s × ln ( n s ) = 0.0 in the various places in the algorithm where it is needed. In practice, I have found that the calculations always converge before the values of ln n s \ln n_s ln n s get anywhere close to underflowing.
Continue iterating until the right hand side of the nonlinear equations is close enough to zero.
This final step also requires some discussion. In general, a Newton’s method can be terminated in one of two possible ways. Either one can monitor the independent variables being solved for, in this case Δ ln ( n s ) \Delta\ln(n_s) Δ ln ( n s ) , and stop iterating when they stop changing, or one can monitor the remaining error in the nonlinear equations, and stop iterating when the remainder on the right hand side is sufficiently close to zero for all of them. Both of these methods, if implemented naively, fail frequently when solving the chemical equilibrium equations implemented in this work.
The problem with monitoring Δ ln ( n s ) \Delta\ln(n_s) Δ ln ( n s ) is illustrated in the following example, which comes from the cold air example script include in the code’s repository. Turning on verbose debugging to get extra information, we consider the following output, from iteration 11.
iter 9: [34.55] 26.50 8.05 0.00 0.00 0.00 (3.549e-11)
sp: N2 lnns: 3.2771 dlnns: -0.000000 lambda: 1.0000
sp: O2 lnns: 2.0857 dlnns: -0.000000 lambda: 1.0000
sp: N lnns: -34.9580 dlnns: -146.8798 lambda: 0.0180
sp: O lnns: -34.9580 dlnns: -57.75229 lambda: 0.0460
sp: NO lnns: -32.422 dlnns: -0.120385 lambda: 1.0000
At room temperature, the dissociated species N and O are present in negligible quantities, ln n s ≈ − 34.9 \ln n_s \approx-34.9 ln n s ≈ − 34.9 , which corresponds to a mole fraction of about 1e-17. Note however, that Δ ln ( n s ) \Delta\ln(n_s) Δ ln ( n s ) for these species is still large, so large in fact, that the relaxation factor lambda is attempting to limit it. If the solver is allowed to continue, it takes a total of 65 iterations for ln n s [ 3 ] \ln n_s[3] ln n s [ 3 ] to reach its converged value of -179.18 or a mole fraction of ≈ 1.8 e − 78 \approx1.8\mathrm{e}{-78} ≈ 1.8 e − 78 , which is a ridiculous level of precision for such an insignificant value. Largely because of this problem, the code does not monitor Δ ln ( n s ) \Delta\ln(n_s) Δ ln ( n s ) to check for convergence.
A typically better way to terminate a Newton’s method is by looking at the right-hand side of the equations being solved, a vector quantity sometimes called the residual . For a fixed pressure/temperature problem, the residuals are given by equations 4, 29, and 30, reproduced below.
G s ∘ R u T + ln ( n s n ) + ln ( p p ∘ ) + ∑ j λ j a j s R u T = F s = 0 \frac{G_s^\circ}{R_uT}+\ln\left(\frac{n_s}{n}\right)+\ln\left(\frac{p}{p^\circ}\right)+\sum_j\lambda_j\frac{a_{js}}{R_uT}=F_s=0 R u T G s ∘ + ln ( n n s ) + ln ( p ∘ p ) + j ∑ λ j R u T a j s = F s = 0
( s = 0 , … , n s ) \left(s=0,\ldots,n_s\right) ( s = 0 , … , n s )
∑ s a j s n s − a j s n s 0 = F j = 0 ( j = 0 , … , n e ) \sum_s a_{js}n_s-a_{js}n_s^0=F_j=0 \qquad\left(j=0,\ldots,n_e\right) s ∑ a j s n s − a j s n s 0 = F j = 0 ( j = 0 , … , n e )
∑ s n s − n = F n = 0 ( j = 0 , … , n e ) \sum_s n_s-n=F_n=0 \qquad\left(j=0,\ldots,n_e\right) s ∑ n s − n = F n = 0 ( j = 0 , … , n e )
We can monitor the convergence by looking at the L2 norm of the remainder on the right hand side, defined as follows.
ε = ∑ s F s 2 + ∑ j F j 2 + F n 2 \varepsilon=\sqrt{\sum_s F_s^2+\sum_j F_j^2+F_n^2} ε = s ∑ F s 2 + j ∑ F j 2 + F n 2
This almost, but not quite works. Note that the equation for F s F_s F s has a logarithm in it, which causes the exact same problem as before; the residual can be large even when the molarity n s n_s n s is insignificant, indeed at n s = 0 n_s=0 n s = 0 the equations become singular.
To track convergence, eqc actually does use this second method, where we track the L2 norm of the residual and stop iterating when it becomes small. However, to avoid the aforementioned problem, when computing the L2 norm of the residual, eqc actually uses n s F s n_sF_s n s F s when considering equation 4. This is a new function which has its zero in the same place as F s F_s F s , but is not singular as n s → 0 n_s\to0 n s → 0 and has better behaviour when n s n_s n s is small. The actual quantity that is monitored for convergence then is given by equation 41, where a tolerance of ε < 1 × 10 − 11 \varepsilon<1\times10^{-11} ε < 1 × 1 0 − 11 is the default behaviour.
ε = ∑ s ( n s F s ) 2 + ∑ j F j 2 + F n 2 (41) \varepsilon=\sqrt{\sum_s(n_sF_s)^2+\sum_jF_j^2+F_n^2} \tag*{(41)} ε = s ∑ ( n s F s ) 2 + j ∑ F j 2 + F n 2 (41)
Example Problems
This section introduces some example problems to verify and validate the solver. First up is a single reaction problem taken from a popular combustion textbook [6 ] , in this case the dissociation of carbon dioxide.
C O 2 ⇌ C O + 1 2 O 2 CO_2 \rightleftharpoons CO + \frac{1}{2}O_2 C O 2 ⇌ C O + 2 1 O 2
Carbon Dioxide Textbook Problem
An attraction of a studying a single reaction is that the equilibrium problem has an analytic solution that the numerical solver can be compared to. Deriving this solution begins with the equation for the Gibbs energy, written in terms of the mole densities N s N_s N s , the species partial pressures p s p_s p s , and the Gibbs energy per mole of species s s s .
G = ∑ s N s G s = ∑ s N s [ G s ∘ + R u T ln ( p s p ∘ ) ] G = \sum_s N_s G_s = \sum_s N_s \left[G_s^{\circ} + R_u T \ln\left(\frac{p_s}{p^{\circ}}\right)\right] G = s ∑ N s G s = s ∑ N s [ G s ∘ + R u T ln ( p ∘ p s ) ]
At a fixed temperature and pressure, the equilibrium condition is given by d G = 0 dG = 0 d G = 0 , which amounts to solving for the following equation.
0 = ∑ s [ G s ∘ + R u T ln ( p s p ∘ ) ] d N s (42) 0 = \sum_s \left[G_s^{\circ} + R_u T \ln\left(\frac{p_s}{p^{\circ}}\right)\right]dN_s \tag*{(42)} 0 = s ∑ [ G s ∘ + R u T ln ( p ∘ p s ) ] d N s (42)
Rather than separate this expression into a set of nonlinear equations as before, we can take advantage of the single reaction to replace the three species composition unknowns with a single unknown quantity α \alpha α , which is a kind of reaction progress variable.
( 1 − α ) C O 2 ⇌ α C O + α 2 O 2 (1-\alpha)CO_2 \rightleftharpoons\alpha CO + \frac{\alpha}{2}O_2 ( 1 − α ) C O 2 ⇌ α C O + 2 α O 2
At α = 0 \alpha= 0 α = 0 the mixture is entirely C O 2 CO_2 C O 2 , while at α = 1 \alpha= 1 α = 1 the mixture is entirely dissociated, consisting of C O CO C O and O 2 O_2 O 2 in a 2:1 ratio. The single reaction also allows us to package up the thermochemistry into a single value K p K_p K p , the famous equilibrium constant, which is in fact not a constant, but a function of temperature and pressure.
K p ≡ exp [ G C O 2 ∘ − G C O ∘ − 1 2 G O 2 ∘ R u T ] K_p \equiv\exp\left[\frac{G_{CO_2}^{\circ} - G_{CO}^{\circ} - \frac{1}{2}G_{O_2}^{\circ}}{R_u T}\right] K p ≡ exp [ R u T G C O 2 ∘ − G C O ∘ − 2 1 G O 2 ∘ ]
The equilibrium condition is given by a cubic polynomial which is derived in Appendix I, with the final answer shown in equation (43) .
α 3 ( 1 − p p ∘ 1 K p 2 ) − 3 α + 2 = 0 (43) \alpha^3\left(1-\frac{p}{p^{\circ}}\frac{1}{K_p^2}\right)-3\alpha+2=0 \tag*{(43)} α 3 ( 1 − p ∘ p K p 2 1 ) − 3 α + 2 = 0 (43)
Equation 43 has one real and two imaginary solutions which can be solved for analytically, specifically the real solution gives the equilibrium value of α \alpha α , and then the mole fractions using equation Appendix I. A python script, included in the eqc repository, has been written that solves equation 43 using the thermodynamic tables from Turns and Haworth [6 ] to compute G s ∘ G_s^{\circ} G s ∘ for each species, which ensures a completely separate comparison to eqc with no shared code or methods.
Figure 4 shows the output of this script compared with curves from eqc, solving the equilibrium composition over a series of fixed temperature and pressures. These results show that the code agrees well with the independent analytic solution.
Figure 4 (PDF p. 14). Comparison of eqc numerical solutions with CO2 dissociation problem from Turns and Haworth [6 ] , Chapter 2.
Equilibrium Air Chemistry
With minor algorithmic differences, the equations that are solved by eqc are the same as those described in [3 ] , and the thermodynamic tables used by both codes are identical. This means for any problem that both codes can solve, the two should give the same answers to within a range of numerical error associated with the Newton’s method convergence tolerance. This section performs such test using a commonly encountered gas mixture for modelling hypersonic wind tunnels, a five species air mixture at known temperature and pressure.
A minimal working example the eqc code, written in the Python programming language is shown below. As mentioned in the preamble to this paper, the core equilibrium equations of eqc are written in the C programming language, but they are accessed by the user through a Python layer that handles various IO and memory allocation tasks. This separation of responsibilities is convenient from a programming perspective, and leads to a nice user interface.
from numpy import array
from pyeq import EqCalculator
spnames = ['N2', 'O2', 'N', 'O', 'NO']
eq = EqCalculator(spnames)
T = 2500.0
p = 0.1*101.35e3
Xs0 = array([0.76, 0.23, 0.0, 0.0, 0.0])
Xs1 = eq.pt(p, T, Xs0, 0)
Results from CEA can be obtained from the online webpage cearun.grc.nasa.gov, which gives an interface to the CEA solver that accepts the same inputs as above. Results from the two solvers are compared below.
Mole Fractions:
N2 O2 N O NO
------------------------------------------------------------------
eqc: 0.747849 0.209004 7.93101e-07 0.0207964 0.0223493
CEA: 0.74785 0.20900 7.93200e-07 0.020799 0.022349
Although the agreement between the two codes is good, the numbers are not precisely identical, particularly the atomic species which differ in the 4th and 5th significant figure. Since the two codes are nominally solving the same equations with the same thermodynamic data, this discrepancy might just be a difference in convergence, or it could be indicative of a problem with one of both codes.
Is there any way that we can independently verify the state arrived at is the correct mathematical solution to the equations? There is. Recall that the mathematical foundation of both codes is solving a constrained minimisation problem, formulated using the method of Lagrange multipliers. Recall that the Lagrangian, equation (27) , is as follows.
L = ∑ s n s [ G s ∘ + R u T ln ( n s n ) + R u T ln ( p p ∘ ) ] + ∑ j λ j ( ∑ s a j s n s − a j s n s 0 ) \mathcal{L} = \sum_s n_s \left[G_s^\circ+ R_u T \ln\left(\frac{n_s}{n}\right) + R_u T \ln\left(\frac{p}{p^\circ}\right)\right] + \sum_j \lambda_j \left(\sum_s a_{js}n_s - a_{js}n_s^0\right) L = s ∑ n s [ G s ∘ + R u T ln ( n n s ) + R u T ln ( p ∘ p ) ] + j ∑ λ j ( s ∑ a j s n s − a j s n s 0 )
Note that this equation is not actually used directly anywhere in the code. Instead we took derivatives of it and then differentiated again to get an analytic Newton’s method. However, if all of the algebra and code has been implemented correctly, the solution we eventually arrive at should be a local minimum of this function.
We can take advantage of this fact to verify the implementation in the following way: Firstly, take the equilibrium state computed above and put it into the full Lagrangian which has been implemented in the C code, purely for the purpose of this exercise. Then, slightly perturb one of the species compositions and compute L L L again, and do this for all of the species in the calculation. This gives a vector of differences that can be used to compute the derivatives of L L L with respect to n s n_s n s , which should be zero or near zero if we are indeed at the equilibrium state.
The results of this calculation are shown below, along with the L2 norm of the derivatives, which is on the order of 0.01. Given that the actual Lagrangian is on the order of 1 × 10 10 1 \times10^{10} 1 × 1 0 10 , and knowing that finite differences have a small amount of numerical error, this is a convincing demonstration that the code has found the stationary point, and hence the correct equilibrium composition.
dLdn_N2 = 0
dLdn_O2 = 0.0051049762
dLdn_N = 0
dLdn_O = 0
dLdn_NO = 0.047740311
-----------------------
|dLdns|: 0.04801247861015218
Equilibrium Normal Shock with Ionisation
One of the most common uses for equilibrium chemistry in hypersonics research is computing the properties of shockwaves, particular for hypersonic flight in the atmosphere, where significant dissociation of the Nitrogen and Oxygen molecules is observed behind the shock. This dissociation cools the shocked gas and alters the shock propagation speed, affecting everything from hypersonic wind tunnel operation to flight vehicle heating and transition to turbulence. This section demonstrates an application code that solves for the equilibrium postshock gas state using eqc, designed to be robust at very high shock speeds, high enough to cause ionisation of the postshock gas.
Normal shock waves are described by the Rankine-Hugoniot jump conditions, a set of equations that describe equality of flux between two gas states on either side of the shock.
ρ 1 v 1 = ρ 2 v 2 ρ 1 v 1 2 + p 1 = ρ 2 v 2 2 + p 2 h 1 + 1 2 v 1 2 = h 2 + 1 2 v 2 2 (44) \begin{aligned}
\rho_1 v_1 &= \rho_2 v_2 \\
\rho_1 v_1^2 + p_1 &= \rho_2 v_2^2 + p_2 \\
h_1 + \frac{1}{2}v_1^2 &= h_2 + \frac{1}{2}v_2^2 \tag*{(44)}
\end{aligned} ρ 1 v 1 ρ 1 v 1 2 + p 1 h 1 + 2 1 v 1 2 = ρ 2 v 2 = ρ 2 v 2 2 + p 2 = h 2 + 2 1 v 2 2 (44)
With no reactions, these equations can be solved analytically to get a postshock state from a preshock mach number M 2 M_2 M 2 , a formula which is present in many textbooks. With dissociation however, the equations must be solved numerically, with a fixed density/internal energy equilibrium problem solved at each step of the outer iteration loop. Although it is possible to use a multi-equation non-linear solver to solve the equations in 44 separately, this turns out to be a bad idea. Instead, an improved method has been derived which reduces the problem to a single nonlinear root-finding problem, beginning by rearranging the Rankine-Hugoniot equations to compute state 2 assuming that the velocity v 2 v_2 v 2 is known.
ρ 2 = ρ 1 v 1 v 2 p 2 = ( ρ 1 v 1 2 + p 1 ) − ρ 2 v 2 2 e 2 = ( h 1 + 1 2 v 1 2 ) − 1 2 ρ 2 v 2 3 − p 2 v 2 ρ 1 v 1 (45) \begin{aligned}
\rho_2 &= \frac{\rho_1 v_1}{v_2} \\
p_2 &= \left(\rho_1 v_1^2 + p_1\right) - \rho_2 v_2^2 \\
e_2 &= \frac{\left(h_1 + \frac{1}{2}v_1^2\right) - \frac{1}{2}\rho_2v_2^3 - p_2v_2}{\rho_1v_1} \tag*{(45)}
\end{aligned} ρ 2 p 2 e 2 = v 2 ρ 1 v 1 = ( ρ 1 v 1 2 + p 1 ) − ρ 2 v 2 2 = ρ 1 v 1 ( h 1 + 2 1 v 1 2 ) − 2 1 ρ 2 v 2 3 − p 2 v 2 (45)
With the density ρ 2 \rho_2 ρ 2 and internal energy e 2 e_2 e 2 computed from the guess v 2 v_2 v 2 , eqc can be called in ρ e \rho e ρ e mode to compute a postshock temperature T 2 T_2 T 2 and composition X 2 X_2 X 2 . However, this composition is not certain to obey the jump conditions in equations 44, in fact for any guess of v 2 v_2 v 2 a postshock composition can be computed, which makes the problem seem ill-posed. The solution to this situation is to introduce the perfect gas equation of state, which relates the pressure to the density, composition and temperature.
p e o s = ρ 2 ∑ s R u M s X s 2 T 2 (46) p_{eos} = \rho_2 \sum_s \frac{R_u}{M_s X_s^2} T_2 \tag*{(46)} p eos = ρ 2 s ∑ M s X s 2 R u T 2 (46)
This pressure p e o s p_{eos} p eos will, in general, disagree with the pressure computed using the jump conditions p 2 p_2 p 2 , so a numerical method can be used to vary the guess v 2 v_2 v 2 until the two pressures agree, which will yield the correct postshock gas state. In this example, the single-equation Newton’s method from scipy is used to solve for ( p 2 − p e o s ) / p 2 = 0 (p_2-p_{eos})/p_2=0 ( p 2 − p eos ) / p 2 = 0 , yielding a robust normal shock calculation procedure that is well behaved even at high velocities. A comparison to CEA run in SHOCK mode for a 6 km/s, partially ionised air condition at 300 K and 600 Pa, is shown below.
Preshock Gas State (76.7% N2, 23.3% O2 by mol.):
p=600 Pa T=300 K v=6000 m/s
Postshock Gas State:
eqc CEA (% diff)
---------------------------------------------------
v (m/s) | 474.5 474.8 (0.05 %)
T (K) | 6564.6 6568.5 (0.06 %)
p (Pa) | 231418.8 234470.0 (1.32 %)
X N2 | 4.260e-01 4.261e-01 (0.04 %)
X O | 3.249e-01 3.249e-01 (0.00 %)
X N | 2.396e-01 2.394e-01 (0.09 %)
X NO | 8.549e-03 8.590e-03 (0.48 %)
X e- | 3.570e-04 3.576e-04 (0.17 %)
X NO+ | 3.295e-04 3.299e-04 (0.14 %)
X O2 | 2.591e-04 2.610e-04 (0.74 %)
X O+ | 1.437e-05 1.439e-05 (0.12 %)
X N+ | 1.015e-05 1.017e-05 (0.13 %)
X N2+ | 4.004e-06 4.020e-06 (0.41 %)
X O- | 1.004e-06 1.016e-06 (1.17 %)
The ionisation fraction associated with this condition is 0.07 %, and the electron number density is 9 × 10 20 9 \times10^{20} 9 × 1 0 20 . Normal shock analysis is frequently used to predict postshock conditions at various altitudes and flight velocities, to investigate issues of radio blackout, chemical modelling, and electromagnetic/fluid coupling effects. In Figure 5 , we can see the results of running the normal shock script over a wide range of altitude and velocities map. The results show good behaviour down to ≈ 2000 \approx2000 ≈ 2000 m/s or about Mach 6, where the ionisation is essentially negligible, though the code is still performing well in this difficult region.
Figure 5. Post-normal shock electron number density, computed vs. Altitude and Flight velocity
Reflected Shock Tunnel Analysis
A workhorse of the experimental hypersonics community is the Reflected Shock Tunnel, an apparatus for generating a brief burst of flow that can actually match the enthalpy and Reynolds numbers experienced during flight. Reflected Shock Tunnels work by creating a strong shockwave or ‘incident shock’ that travels down tube of test gas, compressing and heating it before reflecting off a solid boundary at the far end. This reflected shock further heats and compresses the test gas, while also rupturing a plastic diaphragm that allows the compressed test gas to flow into a Laval nozzle where it expands to supersonic or hypersonic speeds. A schematic depiction of this process is shown in figure 5.4.
Experimentalists measure the velocity of the incident shock and the pressure of the gas feeding into the nozzle, but the flow coming out of the nozzle cannot be measured directly, and is typically reconstructed using some kind of computer program that takes these measurements as inputs. Since temperatures in the shocked gas are high, including equilibrium chemistry in these calculations is critical to getting accurate results. This section presents an application script that uses eqc to perform such a calculation, comparing to known experimental data and to another program.
The only additional theoretical component needed for the analysis is an equation for the shock reflection, which can be obtained from the Rankine-Hugoniot equations (equations (44) ). Note that these equations are in a shock centered reference frame: they assume that the shock is stationary and the incoming flow is moving at v 1 v_1 v 1 . In the case of the incident shock in a shock tube, the test gas is stationary and the shock velocity is known, so the transformation is simply that v 1 v_1 v 1 is the shock velocity. However, in the reflected state it is v 2 v_2 v 2 that is zero and we must solve the velocity of the shock (at thus the reference frame) itself. We do this by guessing the reflected shock velocity v r v_r v r , and computing the shock-stationary frame v 2 ′ v'_2 v 2 ′ as follows:
Figure 6. Station numbering for flow states in reflected shock tunnel analysis.
v 2 ′ = v 1 + v r (47) v'_2 = v_1 + v_r \tag*{(47)} v 2 ′ = v 1 + v r (47)
The reflected shock stationary reference frame jump conditions are then the same as before, except the Newton’s method is adjusting v r v_r v r until the two pressures p e o s p_{eos} p eos and p 2 p_2 p 2 match.
In this section the code has been compared to an experiment conducted in the T4 Reflected Shock Tunnel at the University of Queensland, published in [8 ] . In particular, I have simulated shot number 11310, a so-called ‘survey’ shot in which the tunnel is fired at a rake of pressure sensors located just downstream of the nozzle. These sensors measure the pitot pressure, computed by passing the flow coming out of the nozzle through a shockwave and then isentropically compressing it to zero velocity. Figure 5.4 shows a schematic of the experimental setup and a comparison to experimental data taken from [8 ] figure 11a.
Figure 7. eqc calculations of test number 11310 compared to the measurements from [8 ] .
The solid blue line is the ratio of pitot to stagnation pressure computed using the actual area of T4’s Mach 7 expansion nozzle, however, [8 ] note that the formation of a viscous boundary layer along the nozzle wall effectively reduces this area, estimating an ≈ 6 \approx6 ≈ 6 mm displacement thickness due to this effect. The dotted blue line is a calculation with eqc that accounts for this effect by reducing the effective exit radius by this amount. Between these two calculations, the results agree nicely with the experimental data.
Conclusions
This article began with a review of existing codes for solving chemical equilibrium problems. Despite finding a number of codes in various states of publication and availability, it noted that no program exists that is simultaneously well-documented, open-source, modern, and lightweight, all important factors for a contemporary research code. The paper then introduced a new code, equilibrium-c, that aims to provide chemical equilibrium solutions while also satisfying these more nebulous criteria.
On the documentation front, the paper itself provides much of the important detail. We began with a derivation of the equations of thermochemical equilibrium, a bit of fundamental bookkeeping that is surprisingly absent from the modern literature. This section lead into the numerical discretisation of the equations, describing novel algorithms, developed here for the first time, to improve convergence prevent a complex calculation from going off its rails. The final part of the paper is concerned with examples, showing off the code’s range of applications with an emphasis on verification and validation. The program agrees with well with analytic solutions of a single reaction, as well as existing codes such as CEA. The program also performs well in a validation exercise against experimental data collected from a reflected shock tunnel.
In summary then, though much of the technical capability of equilibrium-c is not new, it fills an important gap in the ecosystem of computational research tools that should be of interest to the wider computational physics community. This paper is a part of that promise, and though it has been long, and it times, somewhat intense, I hope that it has helped the reader in understanding chemical equilibrium a little better.
Acknowledgements
In preparing this manuscript, the author benefitted greatly from Leonard Susskind’s “The Theoretical Minimum” lecture series, which is freely available online.
For n n n six-sided dice the formula for generating the number of microstates is:
N S = ∑ k = 0 M ( − 1 ) k n ! k ! ( n − k ) ! × ( S − 6 k − 1 ) ! ( n − 1 ) ! ( S − 6 k − n ) ! N_S=\sum_{k=0}^{M}(-1)^k\frac{n!}{k!(n-k)!}\times\frac{(S-6k-1)!}{(n-1)!(S-6k-n)!} N S = k = 0 ∑ M ( − 1 ) k k ! ( n − k )! n ! × ( n − 1 )! ( S − 6 k − n )! ( S − 6 k − 1 )!
M = floor ( S − n 6 ) M=\operatorname{floor}\left(\frac{S-n}{6}\right) M = floor ( 6 S − n )
This expression is related to the Irwin-Hall distribution and is courtesy of reference [9 ] .
Partial Derivative of Entropy w.r.t Temperature
Beginning with equation (7) .
S = k N [ ln Q ( V , T ) N + 1 + E ( V , T ) T ] S=kN\left[\ln\frac{Q(V,T)}{N}+1+\frac{E(V,T)}{T}\right] S = k N [ ln N Q ( V , T ) + 1 + T E ( V , T ) ]
We first differentiate with respect to T T T .
∂ S ∂ T ∣ V = k N ∂ ln Q ∂ T + ∂ E ∂ T T − E ∂ T ∂ T T 2 \left.\frac{\partial S}{\partial T}\right|_V=kN\frac{\partial\ln Q}{\partial T}+\frac{\frac{\partial E}{\partial T}T-E\frac{\partial T}{\partial T}}{T^2} ∂ T ∂ S V = k N ∂ T ∂ ln Q + T 2 ∂ T ∂ E T − E ∂ T ∂ T
∂ S ∂ T ∣ V = k N Q ∂ Q ∂ T ∣ V + ∂ E ∂ T ∣ V 1 T − E T 2 (48) \left.\frac{\partial S}{\partial T}\right|_V=\frac{kN}{Q}\left.\frac{\partial Q}{\partial T}\right|_V+\left.\frac{\partial E}{\partial T}\right|_V\frac{1}{T}-\frac{E}{T^2} \tag*{(48)} ∂ T ∂ S V = Q k N ∂ T ∂ Q V + ∂ T ∂ E V T 1 − T 2 E (48)
We now compute the partial derivative of the partition function Q Q Q , noting that the energy levels depend on volume V V V , but not the temperature.
Q = ∑ j g j e − ε j k T Q=\sum_j g_j e^{-\frac{\varepsilon_j}{kT}} Q = j ∑ g j e − k T ε j
∂ Q ∂ T ∣ V = 1 k T 2 ∑ j ε j g j e − ε j k T \left.\frac{\partial Q}{\partial T}\right|_V=\frac{1}{kT^2}\sum_j\varepsilon_j g_j e^{-\frac{\varepsilon_j}{kT}} ∂ T ∂ Q V = k T 2 1 j ∑ ε j g j e − k T ε j
Recall that the most probable set of occupation numbers N j ∗ N_j^* N j ∗ has the following expression.
N j ∗ = N Q g j e − ε j k T N_j^*=\frac{N}{Q}g_j e^{-\frac{\varepsilon_j}{kT}} N j ∗ = Q N g j e − k T ε j
Recall also that the energy at equilibrium is the number of particles in each level N j ∗ N_j^* N j ∗ , multiplied by the energy of that level, summed over the whole collection: E = ∑ j ε j N j ∗ E=\sum_j\varepsilon_jN_j^* E = ∑ j ε j N j ∗ . Putting these two pieces together, we get.
E = ∑ j ε j N Q g j e − ε j k T E=\sum_j\varepsilon_j\frac{N}{Q}g_j e^{-\frac{\varepsilon_j}{kT}} E = j ∑ ε j Q N g j e − k T ε j
E Q N = ∑ j ε j g j e − ε j k T \frac{EQ}{N}=\sum_j\varepsilon_jg_j e^{-\frac{\varepsilon_j}{kT}} N E Q = j ∑ ε j g j e − k T ε j
Notice that the left hand side of this expression matches the derivative of Q Q Q we are looking for, which gives us the expression as follows.
∂ Q ∂ T ∣ V = 1 k T 2 E Q N \left.\frac{\partial Q}{\partial T}\right|_V=\frac{1}{kT^2}\frac{EQ}{N} ∂ T ∂ Q V = k T 2 1 N E Q
Substituting this expression into B.1, we get.
∂ S ∂ T ∣ V = k N Q 1 k T 2 E Q N + ∂ E ∂ T ∣ V 1 T − E T 2 \left.\frac{\partial S}{\partial T}\right|_V=\frac{kN}{Q}\frac{1}{kT^2}\frac{EQ}{N}+\left.\frac{\partial E}{\partial T}\right|_V\frac{1}{T}-\frac{E}{T^2} ∂ T ∂ S V = Q k N k T 2 1 N E Q + ∂ T ∂ E V T 1 − T 2 E
∂ S ∂ T ∣ V = E T 2 + ∂ E ∂ T ∣ V 1 T − E T 2 \left.\frac{\partial S}{\partial T}\right|_V=\frac{E}{T^2}+\left.\frac{\partial E}{\partial T}\right|_V\frac{1}{T}-\frac{E}{T^2} ∂ T ∂ S V = T 2 E + ∂ T ∂ E V T 1 − T 2 E
Leaving us with the target expression.
∂ S ∂ T ∣ V = ∂ E ∂ T ∣ V 1 T \left.\frac{\partial S}{\partial T}\right|_V=\left.\frac{\partial E}{\partial T}\right|_V\frac{1}{T} ∂ T ∂ S V = ∂ T ∂ E V T 1
Partial Derivative of Entropy w.r.t Volume
The derivation in this section proceeds in a slightly strange way, by beginning with the statistical mechanics definition of pressure and working backwards toward the expression we are interested in. To recap, the pressure is defined as minus the partial derivative of energy with respect to volume, at constant entropy .
p = − ∂ E ∂ V ∣ S (49) p = -\left.\frac{\partial E}{\partial V}\right|_{S} \tag*{(49)} p = − ∂ V ∂ E S (49)
This expression makes some intuitive sense; the energy change of a box of gas as it is compressed is going to be a force, in this gas the pressure, integrated over the change in volume. But in mathematical terms it is a somewhat strange formula, because it is a partial derivative of a dependant variable (which is only) but with another dependant variable, S, held constant.
This is a strange thing to do, but it can be done using a lemma derived by Susskind [10 ] , which is reproduced here. Consider two functions, E ( V , T ) E(V,T) E ( V , T ) and S ( V , T ) S(V,T) S ( V , T ) , which are sketched in the figure below. We wish to move a small distance Δ E \Delta E Δ E , from E 1 E_1 E 1 to E 2 E_2 E 2 , along a line of constant S S S .
Figure C.8. Two independent functions E E E and S S S which depend on V V V and T T T .
Using the multivariate difference expression, the change in E E E can be written as follows.
Δ E = ∂ E ∂ V ∣ T Δ V + ∂ E ∂ T ∣ V Δ T \Delta E = \left.\frac{\partial E}{\partial V}\right|_{T}\Delta V + \left.\frac{\partial E}{\partial T}\right|_{V}\Delta T Δ E = ∂ V ∂ E T Δ V + ∂ T ∂ E V Δ T
The second partial derivative can be expanded using the chain rule, to introduce S S S .
Δ E = ∂ E ∂ V ∣ T Δ V + ∂ E ∂ S ∣ V ∂ S ∂ T ∣ V Δ T \Delta E = \left.\frac{\partial E}{\partial V}\right|_{T}\Delta V + \left.\frac{\partial E}{\partial S}\right|_{V}\left.\frac{\partial S}{\partial T}\right|_{V}\Delta T Δ E = ∂ V ∂ E T Δ V + ∂ S ∂ E V ∂ T ∂ S V Δ T
Δ E Δ V = ∂ E ∂ V ∣ T + ∂ E ∂ S ∣ V ∂ S ∂ T ∣ V Δ T Δ V (50) \frac{\Delta E}{\Delta V} = \left.\frac{\partial E}{\partial V}\right|_{T} + \left.\frac{\partial E}{\partial S}\right|_{V}\left.\frac{\partial S}{\partial T}\right|_{V}\frac{\Delta T}{\Delta V} \tag*{(50)} Δ V Δ E = ∂ V ∂ E T + ∂ S ∂ E V ∂ T ∂ S V Δ V Δ T (50)
We now consider moving along a line of constant S S S . Once again, the multivariate difference theorem gives:
Δ S = ∂ S ∂ V ∣ T Δ V + ∂ S ∂ T ∣ V Δ T \Delta S = \left.\frac{\partial S}{\partial V}\right|_{T}\Delta V + \left.\frac{\partial S}{\partial T}\right|_{V}\Delta T Δ S = ∂ V ∂ S T Δ V + ∂ T ∂ S V Δ T
Setting Δ S \Delta S Δ S equal to zero, this gives a constraint equation which guides the ratio of Δ V \Delta V Δ V and Δ T \Delta T Δ T we need to ensure we travel along the green line in the figure.
Δ T Δ V = − ∂ S ∂ V ∣ T ∂ S ∂ T ∣ V (51) \frac{\Delta T}{\Delta V} = -\frac{\left.\frac{\partial S}{\partial V}\right|_{T}}{\left.\frac{\partial S}{\partial T}\right|_{V}} \tag*{(51)} Δ V Δ T = − ∂ T ∂ S V ∂ V ∂ S T (51)
This expression is important because it encodes the constraint of Δ S \Delta S Δ S being zero. Accordingly, we can substitute it into C.2 to get the following.
Δ E Δ V = ∂ E ∂ V ∣ T + ∂ E ∂ S ∣ V Δ S Δ T × ∂ S ∂ V ∣ T ∂ S ∂ T ∣ V (52) \frac{\Delta E}{\Delta V} = \left.\frac{\partial E}{\partial V}\right|_{T} + \left.\frac{\partial E}{\partial S}\right|_{V}\frac{\Delta S}{\Delta T}\times\frac{\left.\frac{\partial S}{\partial V}\right|_{T}}{\left.\frac{\partial S}{\partial T}\right|_{V}} \tag*{(52)} Δ V Δ E = ∂ V ∂ E T + ∂ S ∂ E V Δ T Δ S × ∂ T ∂ S V ∂ V ∂ S T (52)
Δ E Δ V = ∂ E ∂ V ∣ T − ∂ E ∂ S ∣ V ∂ S ∂ V ∣ T \frac{\Delta E}{\Delta V} = \left.\frac{\partial E}{\partial V}\right|_{T} - \left.\frac{\partial E}{\partial S}\right|_{V}\left.\frac{\partial S}{\partial V}\right|_{T} Δ V Δ E = ∂ V ∂ E T − ∂ S ∂ E V ∂ V ∂ S T
Letting the Δ \Delta Δ differences on the left hand side go to zero, the fraction there becomes a partial derivative. But a partial derivative with which variable held constant? The answer is S S S , because of the constraint implied by equation C.3.
∂ E ∂ V ∣ S = ∂ E ∂ V ∣ T − ∂ E ∂ S ∣ V ∂ S ∂ V ∣ T (53) \left.\frac{\partial E}{\partial V}\right|_{S} = \left.\frac{\partial E}{\partial V}\right|_{T} - \left.\frac{\partial E}{\partial S}\right|_{V}\left.\frac{\partial S}{\partial V}\right|_{T} \tag*{(53)} ∂ V ∂ E S = ∂ V ∂ E T − ∂ S ∂ E V ∂ V ∂ S T (53)
This is the unusual expression derived by Susskind that relates a pair of partial derivatives with their independent variables held constant. The choice of variables is no accident; Note that partial derivative on the left hand side is just minus the pressure, and also, that one of the terms on the right hand side is the partial derivative of entropy with respect to volume.
− p = ∂ E ∂ V ∣ T − ∂ E ∂ S ∣ V ∂ S ∂ V ∣ T -p = \left.\frac{\partial E}{\partial V}\right|_{T} - \left.\frac{\partial E}{\partial S}\right|_{V}\left.\frac{\partial S}{\partial V}\right|_{T} − p = ∂ V ∂ E T − ∂ S ∂ E V ∂ V ∂ S T
∂ S ∂ V ∣ T = p + ∂ E ∂ V ∣ T ∂ E ∂ S ∣ V \left.\frac{\partial S}{\partial V}\right|_{T} = \frac{p + \left.\frac{\partial E}{\partial V}\right|_{T}}{\left.\frac{\partial E}{\partial S}\right|_{V}} ∂ V ∂ S T = ∂ S ∂ E V p + ∂ V ∂ E T
In statistical mechanics, the temperature T T T is defined as the partial derivative of energy with respect to entropy, which can be used to simplify this equation to get to the target expression we have been looking for.
∂ S ∂ V ∣ T = p T + ∂ E ∂ V ∣ T 1 T \left.\frac{\partial S}{\partial V}\right|_{T} = \frac{p}{T} + \left.\frac{\partial E}{\partial V}\right|_{T}\frac{1}{T} ∂ V ∂ S T = T p + ∂ V ∂ E T T 1
Partial Derivatives of the Gibbs Energy wrt. Composition
Recall that the Gibbs energy G G G for a mixture of species is given by:
G = ∑ s N s G s G = \sum_s N_sG_s G = s ∑ N s G s
Or, expanding G s G_s G s into its explicit form:
G = ∑ s N s [ E s + R u T − T S s ∘ − R u T ln ( N s N ) − R u T ln ( p p ∘ ) ] G = \sum_s N_s\left[E_s + R_uT - TS_s^\circ- R_uT\ln\left(\frac{N_s}{N}\right) - R_uT\ln\left(\frac{p}{p^\circ}\right)\right] G = s ∑ N s [ E s + R u T − T S s ∘ − R u T ln ( N N s ) − R u T ln ( p ∘ p ) ]
Differentiating this expression with respect to a given species amount N p N_p N p is a actually a nontrivial exercise, because of the sum over species s s s and the presence of both N s N_s N s and the total N N N in the expression for species molar entropy S s S_s S s , which appear in the log terms in the large bracket. This creates extra terms because of the product rule that must be accounted for very carefully. We are going to differentiate each term in the summation over species one at a time, considering two cases, one where the species index s s s matches the index differentiating variable p p p , and a second case where it does not. We begin by writing the derivative as follows, using the product rule.
∂ G ∂ N p = ∑ s ∂ N s ∂ N p G s + ∑ s N s ∂ G s ∂ N p (54) \frac{\partial G}{\partial N_p} = \sum_s\frac{\partial N_s}{\partial N_p}G_s + \sum_s N_s\frac{\partial G_s}{\partial N_p} \tag*{(54)} ∂ N p ∂ G = s ∑ ∂ N p ∂ N s G s + s ∑ N s ∂ N p ∂ G s (54)
The first term contains ∂ N s / ∂ N p \partial N_s/\partial N_p ∂ N s / ∂ N p , which is 1 if s = p s=p s = p and zero otherwise, leaving just G p G_p G p . The second term can be split up into one term for s = p s=p s = p , and a summation that includes all of the terms where s ≠ p s\ne p s = p .
∂ G ∂ N p = 1 × G p + N p ∂ G p ∂ N p + ∑ s s ≠ p ∂ G s ∂ N p \frac{\partial G}{\partial N_p} = 1 \times G_p + N_p\frac{\partial G_p}{\partial N_p} + \sum_s^{s\ne p}\frac{\partial G_s}{\partial N_p} ∂ N p ∂ G = 1 × G p + N p ∂ N p ∂ G p + s ∑ s = p ∂ N p ∂ G s
Using the full expression for G p G_p G p , the first term is differentiated as follows.
G p = [ E p + R u T − T S p ∘ + R u T ln ( N p N ) + R u T ln ( p p ∘ ) ] G_p = \left[E_p + R_uT - TS_p^\circ+ R_uT\ln\left(\frac{N_p}{N}\right) + R_uT\ln\left(\frac{p}{p^\circ}\right)\right] G p = [ E p + R u T − T S p ∘ + R u T ln ( N N p ) + R u T ln ( p ∘ p ) ]
∂ G p ∂ N p = [ 0 + R u T ∂ ∂ N p ln ( N p N ) + 0 ] \frac{\partial G_p}{\partial N_p} = \left[0 + R_uT\frac{\partial}{\partial N_p}\ln\left(\frac{N_p}{N}\right) + 0\right] ∂ N p ∂ G p = [ 0 + R u T ∂ N p ∂ ln ( N N p ) + 0 ]
∂ G p ∂ N p = R u T [ ∂ ∂ N p ln ( N p ) − ∂ ∂ N p ln ( N ) ] \frac{\partial G_p}{\partial N_p} = R_uT\left[\frac{\partial}{\partial N_p}\ln(N_p)-\frac{\partial}{\partial N_p}\ln(N)\right] ∂ N p ∂ G p = R u T [ ∂ N p ∂ ln ( N p ) − ∂ N p ∂ ln ( N ) ]
∂ G p ∂ N p = R u T [ 1 N p − 1 N ∂ N ∂ N p ] \frac{\partial G_p}{\partial N_p} = R_uT\left[\frac{1}{N_p}-\frac{1}{N}\frac{\partial N}{\partial N_p}\right] ∂ N p ∂ G p = R u T [ N p 1 − N 1 ∂ N p ∂ N ]
Note that N N N , the total particle count of the system in moles, is the sum of the individual species amounts N s N_s N s , ∂ N / ∂ N p \partial N/\partial N_p ∂ N / ∂ N p is equal to 1 leaving:
∂ G p ∂ N p = R u T [ 1 N p − 1 N ] (55) \frac{\partial G_p}{\partial N_p} = R_uT\left[\frac{1}{N_p}-\frac{1}{N}\right] \tag*{(55)} ∂ N p ∂ G p = R u T [ N p 1 − N 1 ] (55)
For s ≠ p s\ne p s = p , we differentiate in a similar manner, but note some minor differences, since ∂ N s / ∂ N p = 0 \partial N_s/\partial N_p=0 ∂ N s / ∂ N p = 0 .
G s = [ E s + R u T − T S s ∘ + R u T ln ( N s N ) + R u T ln ( p p ∘ ) ] G_s = \left[E_s + R_uT - TS_s^\circ+ R_uT\ln\left(\frac{N_s}{N}\right) + R_uT\ln\left(\frac{p}{p^\circ}\right)\right] G s = [ E s + R u T − T S s ∘ + R u T ln ( N N s ) + R u T ln ( p ∘ p ) ]
∂ G s ∂ N p = [ 0 + R u T ∂ ∂ N p ln ( N s N ) + 0 ] \frac{\partial G_s}{\partial N_p} = \left[0 + R_uT\frac{\partial}{\partial N_p}\ln\left(\frac{N_s}{N}\right) + 0\right] ∂ N p ∂ G s = [ 0 + R u T ∂ N p ∂ ln ( N N s ) + 0 ]
∂ G s ∂ N p = R u T [ ∂ ∂ N p ln ( N s ) − ∂ ∂ N p ln ( N ) ] \frac{\partial G_s}{\partial N_p} = R_uT\left[\frac{\partial}{\partial N_p}\ln(N_s)-\frac{\partial}{\partial N_p}\ln(N)\right] ∂ N p ∂ G s = R u T [ ∂ N p ∂ ln ( N s ) − ∂ N p ∂ ln ( N ) ]
∂ G s ∂ N p = R u T [ 0 − 1 N ∂ N ∂ N p ] \frac{\partial G_s}{\partial N_p} = R_uT\left[0-\frac{1}{N}\frac{\partial N}{\partial N_p}\right] ∂ N p ∂ G s = R u T [ 0 − N 1 ∂ N p ∂ N ]
∂ G s ∂ N p = − R u T 1 N (56) \frac{\partial G_s}{\partial N_p} = -R_uT\frac{1}{N} \tag*{(56)} ∂ N p ∂ G s = − R u T N 1 (56)
Putting the pieces back together, we pick up equation D.1, and substitute in equations D.2 and D.3.
∂ G ∂ N p = G p + N p ∂ G p ∂ N p + ∑ s s ≠ p N s ∂ G s ∂ N p \frac{\partial G}{\partial N_p} = G_p + N_p\frac{\partial G_p}{\partial N_p} + \sum_s^{s\ne p}N_s\frac{\partial G_s}{\partial N_p} ∂ N p ∂ G = G p + N p ∂ N p ∂ G p + s ∑ s = p N s ∂ N p ∂ G s
∂ G ∂ N p = G p + N p R u T [ 1 N p − 1 N ] + ∑ s s ≠ p N s × − R u T 1 N \frac{\partial G}{\partial N_p} = G_p + N_pR_uT\left[\frac{1}{N_p}-\frac{1}{N}\right] + \sum_s^{s\ne p}N_s \times-R_uT\frac{1}{N} ∂ N p ∂ G = G p + N p R u T [ N p 1 − N 1 ] + s ∑ s = p N s × − R u T N 1
∂ G ∂ N p = G p + R u T [ N p N p − N p N ] − ∑ s s ≠ p R u T N s N \frac{\partial G}{\partial N_p} = G_p + R_uT\left[\frac{N_p}{N_p}-\frac{N_p}{N}\right] - \sum_s^{s\ne p}R_uT\frac{N_s}{N} ∂ N p ∂ G = G p + R u T [ N p N p − N N p ] − s ∑ s = p R u T N N s
∂ G ∂ N p = G p + R u T [ 1 − N p N − ∑ s s ≠ p N s N ] \frac{\partial G}{\partial N_p} = G_p + R_uT\left[1-\frac{N_p}{N}-\sum_s^{s\ne p}\frac{N_s}{N}\right] ∂ N p ∂ G = G p + R u T [ 1 − N N p − s ∑ s = p N N s ]
∂ G ∂ N p = G p + R u T [ 1 − ∑ s N s N ] \frac{\partial G}{\partial N_p} = G_p + R_uT\left[1-\frac{\sum_s N_s}{N}\right] ∂ N p ∂ G = G p + R u T [ 1 − N ∑ s N s ]
∂ G ∂ N p = G p + R u T [ 1 − 1 ] \frac{\partial G}{\partial N_p} = G_p + R_uT[1-1] ∂ N p ∂ G = G p + R u T [ 1 − 1 ]
∂ G ∂ N p = G p \frac{\partial G}{\partial N_p} = G_p ∂ N p ∂ G = G p
This is a very tidy and interesting result. Note that, although the Gibbs energy of species p p p has nonlinear dependencies on the composition N s N_s N s , when we differentiate, the extra terms from the product rule cancel out exactly to zero. More concretely, we can see that if G = N s G s G=N_sG_s G = N s G s , it is true that ∂ G / ∂ N p = G p \partial G/\partial N_p=G_p ∂ G / ∂ N p = G p , almost as if G G G were not a function of N p N_p N p , even though it certainly is.
Equation for Entropy at Nonstandard Pressure
Many thermodynamics textbooks differentiate between the specific (per kilogram) entropy s s s and the molar (per mole) entropy S S S using capital and lower case letters. Unfortunately, the upper-case notation is also used in many places for the total, extrinsic entropy S \mathcal{S} S . This notation is a source of much confusion. Unfortunately this paper too has been guilty of abusing this notation in several places, which has not caused us any problems until now, where we must be very careful not to mix the two quantities. To that end, we begin by designating the total entropy using a calligraphic font S \mathcal{S} S , to distinguish it from the molar entropy S S S , and the total energy E \mathcal{E} E as opposed to the energy per mole E E E .
S = S N E = E N S = \frac{\mathcal{S}}{N} \qquad E = \frac{\mathcal{E}}{N} S = N S E = N E
Where N N N is the number of particles in the system, measured in moles. The differential form of the second law is actually about the extrinsic entropy, so it should be properly written as:
d S = d E + p d V T \mathrm{d}\mathcal{S} = \frac{\mathrm{d}\mathcal{E} + p\mathrm{d}V}{T} d S = T d E + p d V
Or, using the fact that d S = N d S \mathrm{d}\mathcal{S} = N\mathrm{d}S d S = N d S and d E = N d E \mathrm{d}\mathcal{E} = N\mathrm{d}E d E = N d E :
d S = N d E + p d V N T \mathrm{d}S = \frac{N\mathrm{d}E + p\mathrm{d}V}{NT} d S = N T N d E + p d V
We are interested in finding an expression for the entropy per mole at a given pressure and temperature, given that we can look up the entropy at a reference pressure S ∘ ( T ) S^\circ(T) S ∘ ( T ) using our thermodynamic tables.
S ( p ) = S ∘ + ∫ p ∘ p d S S(p) = S^\circ+ \int_{p^\circ}^{p} \mathrm{d}S S ( p ) = S ∘ + ∫ p ∘ p d S
S ( p ) = S ∘ + ∫ p ∘ p N d E + p d V N T S(p) = S^\circ+ \int_{p^\circ}^{p} \frac{N\mathrm{d}E + p\mathrm{d}V}{NT} S ( p ) = S ∘ + ∫ p ∘ p N T N d E + p d V
We can proceed by integrating the entropy over a change in volume; starting at whatever the volume would be at the reference pressure, to whatever the volume would be at our target pressure. We do this because of the handy d V \mathrm{d}V d V term in the second law, which can be replaced later on by pressure. Also, note that the temperature is fixed in this integral, as we have already accounted for it in the calculation of S ∘ S^\circ S ∘ , which means that d E = 0 \mathrm{d}E = 0 d E = 0 .
S ( p ) = S ∘ ( T ) + ∫ V ∘ V p d V N T S(p) = S^\circ(T) + \int_{V^\circ}^{V} \frac{p\mathrm{d}V}{NT} S ( p ) = S ∘ ( T ) + ∫ V ∘ V N T p d V
Using the ideal gas law, p V = N R u T pV = NR_uT p V = N R u T :
S ( p ) = S ∘ ( T ) + R u ∫ V ∘ V d V V S(p) = S^\circ(T) + R_u \int_{V^\circ}^{V} \frac{\mathrm{d}V}{V} S ( p ) = S ∘ ( T ) + R u ∫ V ∘ V V d V
S ( p ) = S ∘ ( T ) + R u ln ( V V ∘ ) S(p) = S^\circ(T) + R_u \ln\left(\frac{V}{V^\circ}\right) S ( p ) = S ∘ ( T ) + R u ln ( V ∘ V )
Now we use the ideal gas law to reintroduce the pressure. Note again that the temperature at our target state and the temperature at the reference state are the same.
S ( p ) = S ∘ ( T ) + R u ln ( N R u T p ∘ N R u T p ) S(p) = S^\circ(T) + R_u\ln\left(\frac{NR_uT p^\circ}{NR_uT p}\right) S ( p ) = S ∘ ( T ) + R u ln ( N R u T p N R u T p ∘ )
S ( p ) = S ∘ ( T ) − R u ln ( p p ∘ ) S(p) = S^\circ(T) - R_u\ln\left(\frac{p}{p^\circ}\right) S ( p ) = S ∘ ( T ) − R u ln ( p ∘ p )
This is the equation for a single component gas. For a mixture of species, we simply add the subscript to the molar quantities S S S , and change the pressure p p p to the partial pressure p s p_s p s .
S s = S s ∘ − R u ln ( p s p ∘ ) S_s = S_s^\circ- R_u\ln\left(\frac{p_s}{p^\circ}\right) S s = S s ∘ − R u ln ( p ∘ p s )
Using the expression for the mole fraction in terms of partial pressure X s = p s / p X_s = p_s/p X s = p s / p and the mole fraction in terms of the total amounts X s = N s / N X_s = N_s/N X s = N s / N , we can reintroduce the ordinary pressure p p p .
S s = S s ∘ − R u ln ( p p ∘ N s N ) S_s = S_s^\circ- R_u\ln\left(\frac{p}{p^\circ}\frac{N_s}{N}\right) S s = S s ∘ − R u ln ( p ∘ p N N s )
A slight rearrangement gives the species molar entropy in the standard form.
S s = S s ∘ − R u ln ( N s N ) − R u ln ( p p ∘ ) (57) S_s = S_s^\circ- R_u\ln\left(\frac{N_s}{N}\right) - R_u\ln\left(\frac{p}{p^\circ}\right) \tag*{(57)} S s = S s ∘ − R u ln ( N N s ) − R u ln ( p ∘ p ) (57)
In Gordon and McBride [3 ] , a slightly different form of the entropy is presented. This uses the specific molarities n s n_s n s , which are related to the total amounts N s N_s N s as follows.
X s = N s N = n s n X_s = \frac{N_s}{N} = \frac{n_s}{n} X s = N N s = n n s
This gives the entropy in the form that is used in section 3, equation 24.
S s = S s ∘ − R u ln ( n s n ) − R u ln ( p p ∘ ) (58) S_s = S_s^\circ- R_u\ln\left(\frac{n_s}{n}\right) - R_u\ln\left(\frac{p}{p^\circ}\right) \tag*{(58)} S s = S s ∘ − R u ln ( n n s ) − R u ln ( p ∘ p ) (58)
Differentiation of the pt Lagrangian
The fixed pressure-temperature Lagrangian (equation 27) is as follows.
L = ∑ s n s [ G s ∘ + R u T ln ( n s n ) + R u T ln ( p p ∘ ) ] + ∑ j λ j ( ∑ s a j s n s − a j s n s 0 ) \mathcal{L}=\sum_s n_s\left[G_s^\circ+R_uT\ln\left(\frac{n_s}{n}\right)+R_uT\ln\left(\frac{p}{p^\circ}\right)\right]
+\sum_j\lambda_j\left(\sum_s a_{js}n_s-a_{js}n_s^0\right) L = s ∑ n s [ G s ∘ + R u T ln ( n n s ) + R u T ln ( p ∘ p ) ] + j ∑ λ j ( s ∑ a j s n s − a j s n s 0 )
Differentiating this expression with respect to each composition unknown n p n_p n p is actually a tricky problem, because of the sum over species s s s present in the expression and the existence of n n n in the Gibbs energy. Importantly, n n n is currently not an unknown, at this stage of the process it is the sum of n s n_s n s , which introduces crucial extra terms in the chain rule.
Thankfully, this problem is actually the same tricky problem as differentiating the Gibbs energy, which we have already worked through. To advantage of this prior work, begin by applying the product rule.
L = ∑ s n s G s + ∑ j λ j ( ∑ s a j s n s − a j s n s 0 ) \mathcal{L}=\sum_s n_sG_s+\sum_j\lambda_j\left(\sum_s a_{js}n_s-a_{js}n_s^0\right) L = s ∑ n s G s + j ∑ λ j ( s ∑ a j s n s − a j s n s 0 )
∂ L ∂ n p = ∑ s ∂ n s ∂ n p G s + ∑ s n s ∂ G s ∂ n p + ∑ j λ j ∑ s a j s ∂ n s ∂ n p \frac{\partial\mathcal{L}}{\partial n_p}=\sum_s\frac{\partial n_s}{\partial n_p}G_s+\sum_s n_s\frac{\partial G_s}{\partial n_p}+\sum_j\lambda_j\sum_s a_{js}\frac{\partial n_s}{\partial n_p} ∂ n p ∂ L = s ∑ ∂ n p ∂ n s G s + s ∑ n s ∂ n p ∂ G s + j ∑ λ j s ∑ a j s ∂ n p ∂ n s
The derivative ∂ n s / ∂ n p \partial n_s/\partial n_p ∂ n s / ∂ n p is 1 when s = p s=p s = p and zero otherwise, which eliminates two of the summation operators.
∂ L ∂ n p = G p + ∑ s n s ∂ G s ∂ n p + ∑ j λ j a j p n p (59) \frac{\partial\mathcal{L}}{\partial n_p}=G_p+\sum_s n_s\frac{\partial G_s}{\partial n_p}+\sum_j\lambda_ja_{jp}n_p \tag*{(59)} ∂ n p ∂ L = G p + s ∑ n s ∂ n p ∂ G s + j ∑ λ j a j p n p (59)
The simplest way to evaluate ∂ G s / ∂ n p \partial G_s/\partial n_p ∂ G s / ∂ n p is to take advantage of the work in Appendix D. Recall that there we began with the following.
G s = [ G s ∘ + R u T ln ( N s N ) + R u T ln ( p p s ∘ ) ] G_s=\left[G_s^\circ+R_uT\ln\left(\frac{N_s}{N}\right)+R_uT\ln\left(\frac{p}{p_s^\circ}\right)\right] G s = [ G s ∘ + R u T ln ( N N s ) + R u T ln ( p s ∘ p ) ]
Recall also that the specific molarities n s n_s n s are defined as follows.
n s = N s ρ V n_s=\frac{N_s}{\rho V} n s = ρ V N s
An irritating detail of this expression is that the mixture total mass ρ V \rho V ρ V actually depends on N s N_s N s : ρ V = ∑ p N p M p \rho V=\sum_pN_pM_p ρ V = ∑ p N p M p , so differentiating it directly creates something of a mess. A simple way to get around this problem is to rewrite the molar Gibbs energy to be in terms of n s n_s n s .
G s = [ G s ∘ + R u T ln ( n s ρ V n ) + R u T ln ( p p ∘ ) ] G_s=\left[G_s^\circ+R_uT\ln\left(\frac{n_s\rho V}{n}\right)+R_uT\ln\left(\frac{p}{p^\circ}\right)\right] G s = [ G s ∘ + R u T ln ( n n s ρ V ) + R u T ln ( p ∘ p ) ]
G s = [ G s ∘ + R u T ln ( n s n ) + R u T ln ( p p ∘ ) ] G_s=\left[G_s^\circ+R_uT\ln\left(\frac{n_s}{n}\right)+R_uT\ln\left(\frac{p}{p^\circ}\right)\right] G s = [ G s ∘ + R u T ln ( n n s ) + R u T ln ( p ∘ p ) ]
Notice that this expression is precisely identical to the one in terms of N s N_s N s , which means we can simply recapitulate exactly the same algebraic operations as in Appendix D, to get the following.
∂ G p ∂ n p = R u T [ 1 n p − 1 n ] \frac{\partial G_p}{\partial n_p}=R_uT\left[\frac{1}{n_p}-\frac{1}{n}\right] ∂ n p ∂ G p = R u T [ n p 1 − n 1 ]
∂ G s ∂ n p = − R u T 1 n \frac{\partial G_s}{\partial n_p}=-R_uT\frac{1}{n} ∂ n p ∂ G s = − R u T n 1
Putting these pieces into the summation from equation F.1, we get the following.
∂ L ∂ n p = G p − ∑ s ≠ p n s R u T 1 n + n p R u T [ 1 n p − 1 n ] + ∑ j λ j a j p n p \frac{\partial\mathcal{L}}{\partial n_p}=G_p-\sum_{s\ne p}n_sR_uT\frac{1}{n}+n_pR_uT\left[\frac{1}{n_p}-\frac{1}{n}\right]+\sum_j\lambda_ja_{jp}n_p ∂ n p ∂ L = G p − s = p ∑ n s R u T n 1 + n p R u T [ n p 1 − n 1 ] + j ∑ λ j a j p n p
∂ L ∂ n p = G p − R u T ∑ s s ≠ p n s n + R u T [ n p n p − n p n ] + ∑ j λ j a j p n p \frac{\partial\mathcal{L}}{\partial n_p}=G_p-R_uT\sum_s^{s\ne p}\frac{n_s}{n}+R_uT\left[\frac{n_p}{n_p}-\frac{n_p}{n}\right]+\sum_j\lambda_ja_{jp}n_p ∂ n p ∂ L = G p − R u T s ∑ s = p n n s + R u T [ n p n p − n n p ] + j ∑ λ j a j p n p
∂ L ∂ n p = G p − R u T [ ∑ s s ≠ p n s n + n p n ] + R u T n p n p + ∑ j λ j a j p n p \frac{\partial\mathcal{L}}{\partial n_p}=G_p-R_uT\left[\sum_s^{s\ne p}\frac{n_s}{n}+\frac{n_p}{n}\right]+R_uT\frac{n_p}{n_p}+\sum_j\lambda_ja_{jp}n_p ∂ n p ∂ L = G p − R u T [ s ∑ s = p n n s + n n p ] + R u T n p n p + j ∑ λ j a j p n p
∂ L ∂ n p = G p − R u T [ ∑ s n s n ] + R u T + ∑ j λ j a j p n p \frac{\partial\mathcal{L}}{\partial n_p}=G_p-R_uT\left[\sum_s\frac{n_s}{n}\right]+R_uT+\sum_j\lambda_ja_{jp}n_p ∂ n p ∂ L = G p − R u T [ s ∑ n n s ] + R u T + j ∑ λ j a j p n p
∂ L ∂ n p = G p − R u T [ n n ] + R u T + ∑ j λ j a j p n p \frac{\partial\mathcal{L}}{\partial n_p}=G_p-R_uT\left[\frac{n}{n}\right]+R_uT+\sum_j\lambda_ja_{jp}n_p ∂ n p ∂ L = G p − R u T [ n n ] + R u T + j ∑ λ j a j p n p
∂ L ∂ n p = G p − R u T + R u T + ∑ j λ j a j p n p \frac{\partial\mathcal{L}}{\partial n_p}=G_p-R_uT+R_uT+\sum_j\lambda_ja_{jp}n_p ∂ n p ∂ L = G p − R u T + R u T + j ∑ λ j a j p n p
∂ L ∂ n p = G p + ∑ j λ j a j p n p (60) \frac{\partial\mathcal{L}}{\partial n_p}=G_p+\sum_j\lambda_ja_{jp}n_p \tag*{(60)} ∂ n p ∂ L = G p + j ∑ λ j a j p n p (60)
Once again we see that the additional terms from the chain rule cancel out, giving ∂ g / ∂ n p = G p \partial g/\partial n_p=G_p ∂ g / ∂ n p = G p . Equation F.2 is the target expression we are looking for.
Reduced Iteration Equations for known Density/Energy
The main body of this paper introduces the equations for a fixed temperature and pressure equilibrium problem. In this section, I present those needed for fixed density and internal energy ρ e \rho e ρ e , and in the next appendix, those for fixed pressure and entropy p s ps p s problems. ρ e \rho e ρ e problems are useful in constant volume reactors where energy and mass are conserved, and p s ps p s mode is helpful for fluid mechanics problems featuring isentropic flow. For ρ e \rho e ρ e problems, the key theoretical difference is the minimisation of Helmholtz energy, a thermodynamic potential defined as:
Note the similarity to the Gibbs energy E + p V − T S E + pV - TS E + p V − T S ; the pressure term has been removed. Before we begin, it will be helpful to write the Helmholtz energy per kilogram f f f in a specific way.
f = F ρ V = n s F s = n s [ E s − T S s ] f = \frac{F}{\rho V} = n_sF_s = n_s[E_s - TS_s] f = ρ V F = n s F s = n s [ E s − T S s ]
f = n s [ E s − T S s ∘ + R u T ln n s n + R u T ln p p ∘ ] f = n_s\left[E_s - TS_s^\circ+ R_uT\ln\frac{n_s}{n} + R_uT\ln\frac{p}{p^\circ}\right] f = n s [ E s − T S s ∘ + R u T ln n n s + R u T ln p ∘ p ]
f = n s [ E s + R u T − R u T − T S s ∘ + R u T ln n s n + R u T ln p p ∘ ] f = n_s\left[E_s + R_uT - R_uT - TS_s^\circ+ R_uT\ln\frac{n_s}{n} + R_uT\ln\frac{p}{p^\circ}\right] f = n s [ E s + R u T − R u T − T S s ∘ + R u T ln n n s + R u T ln p ∘ p ]
f = n s [ G s ∘ + R u T ln n s n + R u T ln p p ∘ − R u T ] f = n_s\left[G_s^\circ+ R_uT\ln\frac{n_s}{n} + R_uT\ln\frac{p}{p^\circ} - R_uT\right] f = n s [ G s ∘ + R u T ln n n s + R u T ln p ∘ p − R u T ]
f = n s [ G s ∘ + R u T ln n s p n p ∘ − R u T ] f = n_s\left[G_s^\circ+ R_uT\ln\frac{n_sp}{np^\circ} - R_uT\right] f = n s [ G s ∘ + R u T ln n p ∘ n s p − R u T ]
We then introduce the ideal gas equation, p = ρ R u T / M m i x p = \rho R_uT/M_{\mathit{mix}} p = ρ R u T / M mix , and remembering that n = 1 / M m i x n = 1/M_{\mathit{mix}} n = 1/ M mix , remove the unknown pressure from the log term, in favour of the density, which is known.
f = n s [ G s ∘ + R u T ln ρ n s R u T p ∘ − R u T ] f = n_s\left[G_s^\circ+ R_uT\ln\frac{\rho n_sR_uT}{p^\circ} - R_uT\right] f = n s [ G s ∘ + R u T ln p ∘ ρ n s R u T − R u T ]
The ρ e \rho e ρ e Lagrangian that needs to be minimised is then.
L = ∑ s n s [ G s ∘ + R u T ln ρ n s R u T p ∘ − R u T ] + ∑ j λ j ( ∑ s a j s n s − b k ∘ ) L = \sum_s n_s\left[G_s^\circ+ R_uT\ln\frac{\rho n_sR_uT}{p^\circ} - R_uT\right] + \sum_j\lambda_j\left(\sum_s a_{js}n_s-b_k^\circ\right) L = s ∑ n s [ G s ∘ + R u T ln p ∘ ρ n s R u T − R u T ] + j ∑ λ j ( s ∑ a j s n s − b k ∘ )
With no n n n term, differentiating this expression is much easier than the p t pt pt version, though note the chain rule still applies.
∂ L ∂ n s = G s ∘ + R u T ln ρ n s R u T p ∘ − R u T + n s [ R u T ∂ ∂ n s ln ρ n s R u T p ∘ + 0 ] + ∑ j λ j a j s \frac{\partial L}{\partial n_s} = G_s^\circ+ R_uT\ln\frac{\rho n_sR_uT}{p^\circ} - R_uT + n_s\left[R_uT\frac{\partial}{\partial n_s}\ln\frac{\rho n_sR_uT}{p^\circ}+0\right] + \sum_j\lambda_ja_{js} ∂ n s ∂ L = G s ∘ + R u T ln p ∘ ρ n s R u T − R u T + n s [ R u T ∂ n s ∂ ln p ∘ ρ n s R u T + 0 ] + j ∑ λ j a j s
∂ L ∂ n s = G s ∘ + R u T ln ρ n s R u T p ∘ − R u T + n s [ R u T 1 n s ] + ∑ j λ j a j s \frac{\partial L}{\partial n_s} = G_s^\circ+ R_uT\ln\frac{\rho n_sR_uT}{p^\circ} - R_uT + n_s\left[R_uT\frac{1}{n_s}\right] + \sum_j\lambda_ja_{js} ∂ n s ∂ L = G s ∘ + R u T ln p ∘ ρ n s R u T − R u T + n s [ R u T n s 1 ] + j ∑ λ j a j s
∂ L ∂ n s = G s ∘ + R u T ln ρ n s R u T p ∘ − R u T + R u T + ∑ j λ j a j s \frac{\partial L}{\partial n_s} = G_s^\circ+ R_uT\ln\frac{\rho n_sR_uT}{p^\circ} - R_uT + R_uT + \sum_j\lambda_ja_{js} ∂ n s ∂ L = G s ∘ + R u T ln p ∘ ρ n s R u T − R u T + R u T + j ∑ λ j a j s
∂ L ∂ n s = G s ∘ + R u T ln ρ n s R u T p ∘ + ∑ j λ j a j s (61) \frac{\partial L}{\partial n_s} = G_s^\circ+ R_uT\ln\frac{\rho n_sR_uT}{p^\circ} + \sum_j\lambda_ja_{js} \tag*{(61)} ∂ n s ∂ L = G s ∘ + R u T ln p ∘ ρ n s R u T + j ∑ λ j a j s (61)
And the constraint equations are the same as before.
∂ L ∂ λ j = ∑ s a j s n s − b j ∘ (62) \frac{\partial L}{\partial\lambda_j} = \sum_s a_{js}n_s-b_j^\circ \tag*{(62)} ∂ λ j ∂ L = s ∑ a j s n s − b j ∘ (62)
At this point we encounter something new. What we have just derived are the equations that maximise the equilibrium disorder of the system: Any composition that evaluates to zero on the right hand side of G.1 and G.2 is an equilibrium state. But note that the temperature appears in these equations, and the temperature is now an unknown. We solve this problem by introducing another nonlinear equation to the set being solved, an apparently trivial equation that checks whether the internal energy e ( T ) e(T) e ( T ) at our current guessed temperature matches the target e 0 e_0 e 0 .
0 = e 0 − e ( T , n s ) = e 0 − ∑ s n s E s ( T ) (63) 0 = e_0 - e(T,n_s) = e_0 - \sum_s n_sE_s(T) \tag*{(63)} 0 = e 0 − e ( T , n s ) = e 0 − s ∑ n s E s ( T ) (63)
We can now proceed to derive analytic update equations for the Newton step, following Gordon and McBride [3 ] . Our unknowns are the logarithms of the species ln ( n s ) \ln(n_s) ln ( n s ) (but not ln ( n ) \ln(n) ln ( n ) , this time), the Lagrange multipliers, which are converted to π j \pi_j π j in exactly the same way as before, and the logarithm of the temperature ln ( T ) \ln(T) ln ( T ) . The iteration equations are then as follows.
Δ ln ( n s ) − ∑ j a s j π j − E j R u T Δ ln T = − G s R u T ( s = 0 , … , n s ) (64) \Delta\ln(n_s)-\sum_j a_{sj}\pi_j-\frac{E_j}{R_uT}\Delta\ln T=-\frac{G_s}{R_uT}\qquad(s=0,\ldots,n_s) \tag*{(64)} Δ ln ( n s ) − j ∑ a s j π j − R u T E j Δ ln T = − R u T G s ( s = 0 , … , n s ) (64)
∑ s a s j n s Δ ln ( n s ) = ∑ s a s j n s 0 − a s j n s ( j = 0 , … , n e ) (65) \sum_s a_{sj}n_s\Delta\ln(n_s)=\sum_s a_{sj}n_s^0-a_{sj}n_s\qquad(j=0,\ldots,n_e) \tag*{(65)} s ∑ a s j n s Δ ln ( n s ) = s ∑ a s j n s 0 − a s j n s ( j = 0 , … , n e ) (65)
∑ s n s E s R u T Δ ln ( n s ) + ∑ s n s C V s R u Δ ln T = − e 0 − n s E s R u T \sum_s\frac{n_sE_s}{R_uT}\Delta\ln(n_s)+\sum_s\frac{n_sC_{V_s}}{R_u}\Delta\ln T=-\frac{e_0-n_sE_s}{R_uT} s ∑ R u T n s E s Δ ln ( n s ) + s ∑ R u n s C V s Δ ln T = − R u T e 0 − n s E s
( s = 0 , … , n s ) (66) (s=0,\ldots,n_s) \tag*{(66)} ( s = 0 , … , n s ) (66)
Once again the species update equations contain only Δ ln ( n s ) \Delta\ln(n_s) Δ ln ( n s ) , which means they can be eliminated analytically to produce what [3 ] call the Reduced Helmholtz Iteration Equations.
∑ i ∑ s a j s a i s n s π i + ( ∑ s a j s n s E s R u T ) Δ ln T = b j 0 − a j s n s + ∑ s a j s n s G s R u T \sum_i\sum_s a_{js}a_{is}n_s\pi_i+\left(\sum_s\frac{a_{js}n_sE_s}{R_uT}\right)\Delta\ln T=
b_j^0-a_{js}n_s+\sum_s\frac{a_{js}n_sG_s}{R_uT} i ∑ s ∑ a j s a i s n s π i + ( s ∑ R u T a j s n s E s ) Δ ln T = b j 0 − a j s n s + s ∑ R u T a j s n s G s
( j = 0 , … , n e ) (67) (j=0,\ldots,n_e) \tag*{(67)} ( j = 0 , … , n e ) (67)
∑ i ( ∑ s a i s n s E s R u T ) p i + [ ∑ s n s C V s R u + n s E s 2 R u 2 T 2 ] Δ ln T = e 0 − ∑ s n s E s R u T + ∑ s n s E s G s R u 2 T 2 (68) \sum_i\left(\sum_s\frac{a_{is}n_sE_s}{R_uT}\right)p_i+\left[\sum_s\frac{n_sC_{V_s}}{R_u}+\frac{n_sE_s^2}{R_u^2T^2}\right]\Delta\ln T=
\frac{e_0-\sum_s n_sE_s}{R_uT}+\sum_s\frac{n_sE_sG_s}{R_u^2T^2} \tag*{(68)} i ∑ ( s ∑ R u T a i s n s E s ) p i + [ s ∑ R u n s C V s + R u 2 T 2 n s E s 2 ] Δ ln T = R u T e 0 − ∑ s n s E s + s ∑ R u 2 T 2 n s E s G s (68)
These are the equations that are iterated on and solved, using the exact same methodology as from section 4.1, to solve for a known density and internal energy.
Reduced Iteration Equations for known Pressure/Entropy
The equations for known pressure and entropy, or p s ps p s problems, are based on the Gibbs free energy and are very similar to the p t pt pt problem in the main text. However, just like in the ρ e \rho e ρ e derivation in the previous appendix, the temperature is now an unknown we must add to the set of nonlinear equations needed to solve for the equilibrium state. In this case, we add an apparently trivial equation that the entropy at the current guess must match the target entropy s 0 s_0 s 0 .
0 = s 0 − n s S s ( T , p ) (69) 0=s_0-n_sS_s(T,p) \tag*{(69)} 0 = s 0 − n s S s ( T , p ) (69)
Following the same process as in the ρ e \rho e ρ e appendix, we get the following Reduced Iteration Equations.
∑ i ∑ s a j s a i s n s π i + ( ∑ s a j s n s ) Δ ln ( n ) + ( ∑ s a j s n s H s R u T ) Δ ln T = ∑ s a j s n s 0 − a j s n s + ∑ s a j s n s G s R u T \sum_i\sum_s a_{js}a_{is}n_s\pi_i+\left(\sum_s a_{js}n_s\right)\Delta\ln(n)+\left(\sum_s\frac{a_{js}n_sH_s}{R_uT}\right)\Delta\ln T
=\sum_s a_{js}n_s^0-a_{js}n_s+\sum_s\frac{a_{js}n_sG_s}{R_uT} i ∑ s ∑ a j s a i s n s π i + ( s ∑ a j s n s ) Δ ln ( n ) + ( s ∑ R u T a j s n s H s ) Δ ln T = s ∑ a j s n s 0 − a j s n s + s ∑ R u T a j s n s G s
( j = 0 , … , n e ) (70) (j=0,\ldots,n_e) \tag*{(70)} ( j = 0 , … , n e ) (70)
∑ i ∑ s a i s n s π i + ( ∑ s n s − n ) Δ ln ( n ) + ( ∑ s n s H s R u T ) Δ ln T = n − ∑ s n s + ∑ s n s G s R u T (71) \sum_i\sum_s a_{is}n_s\pi_i+\left(\sum_s n_s-n\right)\Delta\ln(n)+\left(\sum_s\frac{n_sH_s}{R_uT}\right)\Delta\ln T
=n-\sum_s n_s+\sum_s\frac{n_sG_s}{R_uT} \tag*{(71)} i ∑ s ∑ a i s n s π i + ( s ∑ n s − n ) Δ ln ( n ) + ( s ∑ R u T n s H s ) Δ ln T = n − s ∑ n s + s ∑ R u T n s G s (71)
∑ i ( ∑ s a i s n s S s R u ) π i + ( ∑ s n s S s R u ) Δ ln ( n ) + ( ∑ s n s C p , s R u + n s H s S s R u 2 T ) Δ ln T = s 0 − ∑ s n s S s R u + n − ∑ s n s + ∑ s n s S s G s R u 2 T (72) \sum_i\left(\sum_s\frac{a_{is}n_sS_s}{R_u}\right)\pi_i+\left(\sum_s\frac{n_sS_s}{R_u}\right)\Delta\ln(n)+\left(\sum_s\frac{n_sC_{p,s}}{R_u}+\frac{n_sH_sS_s}{R_u^2T}\right)\Delta\ln T
=\frac{s_0-\sum_s n_sS_s}{R_u}+n-\sum_s n_s+\sum_s\frac{n_sS_sG_s}{R_u^2T} \tag*{(72)} i ∑ ( s ∑ R u a i s n s S s ) π i + ( s ∑ R u n s S s ) Δ ln ( n ) + ( s ∑ R u n s C p , s + R u 2 T n s H s S s ) Δ ln T = R u s 0 − ∑ s n s S s + n − s ∑ n s + s ∑ R u 2 T n s S s G s (72)
Derivation of Single Reaction Analytic Solution
Recall that the equilibrium condition is given by equation 42, reproduced below.
0 = ∑ s [ G s ∘ + R u T ln ( p s p ∘ ) ] d N s 0 = \sum_s \left[G_s^\circ+ R_u T \ln\left(\frac{p_s}{p^\circ}\right)\right]dN_s 0 = s ∑ [ G s ∘ + R u T ln ( p ∘ p s ) ] d N s
The changes in species mole densities can be written in terms of alpha as follows.
d N C O 2 = − d α dN_{CO_2}=-d\alpha d N C O 2 = − d α
d N C O = d α dN_{CO}=d\alpha d N C O = d α
d N O 2 = 1 2 d α dN_{O_2}=\frac{1}{2}d\alpha d N O 2 = 2 1 d α
Substituting these expressions in equation 42 results in a single equation in terms of the partial pressures.
0 = [ G C O 2 ∘ + R u T ln ( p C O 2 p ∘ ) ] − d α + [ G C O ∘ + R u T ln ( p C O p ∘ ) ] d α + [ G O 2 ∘ + R u T ln ( p O 2 p ∘ ) ] 1 2 d α 0 = \left[G_{CO_2}^\circ+ R_u T \ln\left(\frac{p_{CO_2}}{p^\circ}\right)\right] - d\alpha
+ \left[G_{CO}^\circ+ R_u T \ln\left(\frac{p_{CO}}{p^\circ}\right)\right]d\alpha
+ \left[G_{O_2}^\circ+ R_u T \ln\left(\frac{p_{O_2}}{p^\circ}\right)\right]\frac{1}{2}d\alpha 0 = [ G C O 2 ∘ + R u T ln ( p ∘ p C O 2 ) ] − d α + [ G C O ∘ + R u T ln ( p ∘ p C O ) ] d α + [ G O 2 ∘ + R u T ln ( p ∘ p O 2 ) ] 2 1 d α
Dividing by d α d\alpha d α removes the reaction progress variable, leaving just the partial pressures.
0 = [ − G C O 2 ∘ + G C O ∘ + 1 2 G O 2 ∘ ] + R u T [ − ln ( p C O 2 p ∘ ) + ln ( p C O p ∘ ) + 1 2 ln ( p O 2 p ∘ ) ] 0 = \left[-G_{CO_2}^\circ+ G_{CO}^\circ+ \frac{1}{2}G_{O_2}^\circ\right]
+ R_u T\left[-\ln\left(\frac{p_{CO_2}}{p^\circ}\right) + \ln\left(\frac{p_{CO}}{p^\circ}\right) + \frac{1}{2}\ln\left(\frac{p_{O_2}}{p^\circ}\right)\right] 0 = [ − G C O 2 ∘ + G C O ∘ + 2 1 G O 2 ∘ ] + R u T [ − ln ( p ∘ p C O 2 ) + ln ( p ∘ p C O ) + 2 1 ln ( p ∘ p O 2 ) ]
0 = Δ G R u T + ln [ ( p C O 2 p ∘ ) − 1 × ( p C O p ∘ ) × ( p O 2 p ∘ ) 1 2 ] 0 = \frac{\Delta G}{R_u T} + \ln\left[\left(\frac{p_{CO_2}}{p^\circ}\right)^{-1}\times\left(\frac{p_{CO}}{p^\circ}\right)\times\left(\frac{p_{O_2}}{p^\circ}\right)^{\frac{1}{2}}\right] 0 = R u T Δ G + ln [ ( p ∘ p C O 2 ) − 1 × ( p ∘ p C O ) × ( p ∘ p O 2 ) 2 1 ]
Where Δ G \Delta G Δ G is the sum of the stoichiometric coefficients multiplied by the individual species energies Δ G = − G C O 2 + G C O + 1 2 G O 2 \Delta G=-G_{CO_2}+G_{CO}+\frac{1}{2}G_{O_2} Δ G = − G C O 2 + G C O + 2 1 G O 2 . We can remove the partial pressures from this expression by using the definition of the mole fractions X s = p s / p X_s=p_s/p X s = p s / p , which results in a more convenient result since the pressure p p p is known.
0 = Δ G R u T + ln [ ( p C O 2 p p ∘ ) − 1 × ( p C O p p ∘ ) × ( p O 2 p p ∘ ) 1 2 × ( p p ∘ 1 2 p ) ] 0 = \frac{\Delta G}{R_u T} + \ln\left[\left(\frac{p_{CO_2}}{pp^\circ}\right)^{-1}\times\left(\frac{p_{CO}}{pp^\circ}\right)\times\left(\frac{p_{O_2}}{pp^\circ}\right)^{\frac{1}{2}}\times\left(\frac{pp^{\circ\frac{1}{2}}}{p}\right)\right] 0 = R u T Δ G + ln [ ( p p ∘ p C O 2 ) − 1 × ( p p ∘ p C O ) × ( p p ∘ p O 2 ) 2 1 × ( p p p ∘ 2 1 ) ]
0 = Δ G R u T + ln [ X C O X O 2 1 2 X C O 2 × ( p p ∘ ) 1 2 ] 0 = \frac{\Delta G}{R_u T} + \ln\left[\frac{X_{CO}X_{O_2}^{\frac{1}{2}}}{X_{CO_2}}\times\left(\frac{p}{p^\circ}\right)^{\frac{1}{2}}\right] 0 = R u T Δ G + ln X C O 2 X C O X O 2 2 1 × ( p ∘ p ) 2 1
exp ( − Δ G R u T ) = X C O X O 2 1 2 X C O 2 × ( p p ∘ ) 1 2 \exp\left(\frac{-\Delta G}{R_u T}\right)=\frac{X_{CO}X_{O_2}^{\frac{1}{2}}}{X_{CO_2}}\times\left(\frac{p}{p^\circ}\right)^{\frac{1}{2}} exp ( R u T − Δ G ) = X C O 2 X C O X O 2 2 1 × ( p ∘ p ) 2 1
The left hand side of this expression contains the exponential of Δ G \Delta G Δ G divided by the thermal energy. This quantity is frequently referred to as the equilibrium constant K p K_p K p for the reaction, which gives the following formula, found in many textbooks.
K p = X C O X O 2 1 2 X C O 2 × ( p p ∘ ) 1 2 K_p=\frac{X_{CO}X_{O_2}^{\frac{1}{2}}}{X_{CO_2}}\times\left(\frac{p}{p^\circ}\right)^{\frac{1}{2}} K p = X C O 2 X C O X O 2 2 1 × ( p ∘ p ) 2 1
Reduction to a single unknown can be performed once again using the reaction progress variable α \alpha α . If we begin with N C O 2 0 N_{CO_2}^0 N C O 2 0 moles of carbon dioxide, for a given amount of reaction progress α \alpha α , the amount of each species is given by.
N C O 2 = N C O 2 0 ( 1 − α ) N_{CO_2}=N_{CO_2}^0(1-\alpha) N C O 2 = N C O 2 0 ( 1 − α )
N C O = N C O 2 0 α N_{CO}=N_{CO_2}^0\alpha N C O = N C O 2 0 α
N O 2 = N C O 2 0 1 2 α N_{O_2}=N_{CO_2}^0\frac{1}{2}\alpha N O 2 = N C O 2 0 2 1 α
With the total moles N T N_T N T equal to N C O 2 + N C O + N O 2 N_{CO_2}+N_{CO}+N_{O_2} N C O 2 + N C O + N O 2 or N C O 2 0 ( 1 + 1 / 2 α ) N_{CO_2}^0(1+1/2\alpha) N C O 2 0 ( 1 + 1/2 α ) , and the mole fractions written as:
X C O 2 = N C O 2 N T = 1 − α 1 + 1 / 2 α X_{CO_2}=\frac{N_{CO_2}}{N_T}=\frac{1-\alpha}{1+1/2\alpha} X C O 2 = N T N C O 2 = 1 + 1/2 α 1 − α
X C O = N C O N T = α 1 + 1 / 2 α X_{CO}=\frac{N_{CO}}{N_T}=\frac{\alpha}{1+1/2\alpha} X C O = N T N C O = 1 + 1/2 α α
X O 2 = N O 2 N T = 1 / 2 α 1 + 1 / 2 α X_{O_2}=\frac{N_{O_2}}{N_T}=\frac{1/2\alpha}{1+1/2\alpha} X O 2 = N T N O 2 = 1 + 1/2 α 1/2 α
Substituting this expression into equation Appendix I gives an expression entirely in terms of α \alpha α , which can be rearranged into a cubic polynomial as follows.
K p = α 1 + 1 / 2 α × ( 1 / 2 α ) 1 / 2 1 + 1 / 2 α × 1 + 1 / 2 α 1 − α × ( p p ∘ ) 1 2 K_p=\frac{\alpha}{1+1/2\alpha}\times\frac{(1/2\alpha)^{1/2}}{1+1/2\alpha}\times\frac{1+1/2\alpha}{1-\alpha}\times\left(\frac{p}{p^\circ}\right)^{\frac{1}{2}} K p = 1 + 1/2 α α × 1 + 1/2 α ( 1/2 α ) 1/2 × 1 − α 1 + 1/2 α × ( p ∘ p ) 2 1
K p p ∘ p = α 1.5 2 × 1 1 − α × 1 ( 1 + 1 / 2 α ) 1 / 2 K_p\sqrt{\frac{p^\circ}{p}}=\frac{\alpha^{1.5}}{\sqrt{2}}\times\frac{1}{1-\alpha}\times\frac{1}{(1+1/2\alpha)^{1/2}} K p p p ∘ = 2 α 1.5 × 1 − α 1 × ( 1 + 1/2 α ) 1/2 1
K p 2 p ∘ p = α 3 2 × 1 ( 1 − α ) 2 × 1 1 + 1 / 2 α K_p^2\frac{p^\circ}{p}=\frac{\alpha^3}{2}\times\frac{1}{(1-\alpha)^2}\times\frac{1}{1+1/2\alpha} K p 2 p p ∘ = 2 α 3 × ( 1 − α ) 2 1 × 1 + 1/2 α 1
α 3 ( 1 − p p ∘ K p 2 ) − 3 α + 2 = 0 (73) \alpha^3\left(1-\frac{p}{p^\circ K_p^2}\right)-3\alpha+2=0 \tag*{(73)} α 3 ( 1 − p ∘ K p 2 p ) − 3 α + 2 = 0 (73)
This expression is readily solved using numpy’s roots library for α \alpha α .