# Modeling Atmospheres in KSP

## Recommended Posts

The only thing I can add to what @Snark said is that the units in his calculation are g/mol or kg/kmol.  KSP and Kopernicus uses units of kg/mol, so you have to divide by 1000.  So for Snark's example the molar mass is 0.0288 kg/mol.

##### Share on other sites
3 hours ago, EricL said:

So,do you do the same thing when calculating average density?

Average density is total mass divided by total volume.  So you simply do what you have to do to add up the mass and volume.

##### Share on other sites
10 hours ago, EricL said:

Thanks @Snark and @OhioBob! So,do you do the same thing when calculating average density?

Just to add on to OhioBob's comment above:  Basically, yes.

Sciencey mathy stuff in spoiler.

Spoiler

Relationship between pressure, temperature, molar mass, and density:

Start from the ideal gas law, PV = nRT (where P is pressure, V is volume, n is number of moles, R is the universal gas constant, and T is absolute temperature).

Where does density fit in, here?  Well, density is mass per volume.  So, mass m = mmolarn (where n is the number of moles).  So n = m / mmolar.  So we can write the gas equation above as:

PV = mRT / mmolar

...which, with a little rearranging, becomes

m/V = mmolar * (P/RT)

But m/V is just the density, ρ.  So this gives us,

ρ = mmolar * (P/RT)

...the result of which is this formula:

ρ = mmolar * (P/RT)

In other words:  The density of a gas equals the molar mass, times the pressure, divided by absolute temperature, divided by the universal gas constant.

And when calculating density according to that formula:  yes, you would use the average molar mass as discussed above, if you're dealing with a mixture of gases.

##### Share on other sites

Thanks for the thorough answer, @Snark.  (I've learned to expect nothing less from you.)

Something interesting about ideal gases is that at a given temperature and pressure, a given volume will always contain the same number of gas molecules regardless of the molecular weight of the gas.  If we have a one liter container at standard temperature and pressure, it will contain the same number of oxygen molecules as it would methane molecules.  And since oxygen is twice as heavy as methane (mmolar = 32 vs. 16), a container holding oxygen will have twice the mass of gas and twice the density as a container holding methane.  You can see this from Snark's equation,

ρ = mmolar * (P/RT)

If P and T are constant, then ρ is directly proportional to mmolar.

From this we can also see that if we have a vessel containing a mixture of gases, then the number of molecules of each gas is directly proportional to its volumetric fraction.  Say we have a vessel containing 2 liters of oxygen and 1 liter of methane, then we have twice as many oxygen molecules as we have methane molecules.  That's is why it's possible to easily compute the average molecular weight as Snark described.  In this example, 32*2/3+16*1/3 = 26.667.

##### Share on other sites

@OhioBob I may have found something that can boost the realism of atmospheres in KSP further (also tagging @GregroxMun here because I think this might interest him)

Looking at the calc sheet AtmoModel.xlsx I see that the Molar Mass is defined to be constant throughout the entire atmosphere, which is okay with the given atmosphere composition. In reality however, molecules are distributed throughout an atmosphere based on their mass, lighter molecules receive a weaker gravitational pull and are thus found more commonly higher up.

Here is an example from Earth's atmosphere:

 Name Range (m) Composition troposphere 0 - 10000 N2 O2 Ar CO2 Ne He CH4 NO Kr Xe H2 stratosphere 10000 - 75000 N2 O2 CO2 H2O O3 Ar ionosphere 75000 - 1000000 N2 N O2 O NO H exosphere >1000000 H He N2 O N

Exact fractions are not given in the 'Composition' tab because I was too lazy to convert the volume percentages. As you can see the more massive noble gases are only found in the troposphere with the exception of Argon because it is much more commonly found in the atmosphere of Earth when compared to the other noble gases such as Krypton, Neon, Helium and Xenon. Molecules get lighter and lighter as altitude increases.

So, what am I proposing?

I suggest creating a new curve that is not used by KSP and used only when creating the atmospheric model. As you can see in the table above, it gets pretty close to a FloatCurve if the composition is converted to average molar mass.

So basically defining ranges for the atmospheric layers and specifying their average molar masses to create a FloatCurve, turn that into Polynomials and enter that curve into the spreadsheet. This will create atmosphere composition variation by altitude, the molar mass slowly changing as more and more lighter molecules begin to filter in.

I shall test this idea soon to see how it behaves outside of a theory, and if it produces an atmosphere that behaves nicely.

Given the fact that the pressure at these altitudes is very, very low, the final effect may not be that noticeable, though it's pretty cool to be able to define whole atmospheric layers such as an ozone layer (stratosphere) and, once you get the hang of the sheet FloatCurve.xlsx, becomes surprisingly easy to make.

##### Share on other sites

@The White Guardian, the lower areas of an atmosphere are pretty thoroughly mixed and can be considered a homogeneous mixture of gases.  In fact, this region of an atmosphere is called the homosphere.  Higher up the gases begin to stratify, with each gas having its own scale height.  This part of the atmosphere is called the heterosphere.  The transition point between homosphere and heterosphere is called the homopause.  On Earth the homopause lies at about 80 km.  My own study of the atmospheres of Venus and Mars has found that the homopause lies at about 120 km for both planets.

For stock sized planets, the atmospheric model will generally terminate at or before we reach the homopause.  Therefore I've never found any reason to consider changes in the molar mass of the gas.  I think that the KSP method of assuming constant molar mass works fine in that circumstance.

However, when producing atmospheric models for life-sized planets, I have investigated this phenomenon and worked up some models and spreadsheets that take it into consideration.  Although KSP doesn't not allow us to change the molar mass, I did devise a method that allows us to fake it out.  My method was to have my spreadsheet develop the atmospheric model as it would be in real life, with the molar mass changing once we get above the homopause.  However, when it came time to compute pressueCurve, instead of using the actual pressure, I added another column for effective pressure.  The important parameter in determining how the atmosphere will effect a body aerodynamically is the density, not the pressure.  Effective pressure was computed from the actual density assuming a constant molar mass at all altitudes of the atmosphere (like KSP does).  By having pressureCurve switch from actual pressure to effective pressure at the homopause, KSP would compute the correct density everywhere.

When I applied this technique to some sample models, I found something interesting.  Once we get into the heterosphere, the molar mass of the gas decreases with increasing height.  However, this also means that the scale height is greater, so the pressure rate of change is less.  In other words, as we go higher, the molar mass decreases but the atmospheric pressure is higher than it would be had the molar mass not decreased.  These two effects almost exactly cancel each other out to where the density of the atmosphere follows the same curve regardless of whether we take the changing molar mass into account or not.

My final conclusion was that it's just not worth the trouble to consider the changing molar mass.  As far as atmospheric density is concerned, it is just as accurate to assume the molar mass is constant throughout the atmosphere as it is to assume otherwise.  The pressureCurve may not be entirely realistic in this circumstance, but that's not what's important when considering the aerodynamic effects.  I think getting the density correct is more important, and that we can do without having to mess around with changing molar mass.

##### Share on other sites

To any one interested in creating their own atmospheres, I've just completed and released an Excel spreadsheet that automates the process (or at least much of it).  It follows the methods described in this thread, though with a few changes to simplify things and make the calculations less buggy.

##### Share on other sites
• 2 months later...

@OhioBob Great tutorial! I was wondering, how would you model an atmosphere in a slightly egg shaped world, with the atmosphere extending almost like a cone on one end (like an additional 50 km)

Edited by Spaceception
##### Share on other sites
8 hours ago, Spaceception said:

@OhioBob Great tutorial! I was wondering, how would you model an atmosphere in a slightly egg shaped world, with the atmosphere extending almost like a cone on one end (like an additional 50 km)

As far as I know, there is no way to deform an atmosphere in KSP; it will always behave as if it's spherical.

##### Share on other sites
2 hours ago, OhioBob said:

As far as I know, there is no way to deform an atmosphere in KSP; it will always behave as if it's spherical.

Okay, thanks, does that mean it wouldn't work over a slightly egg shaped world? Would it not have any air on either side?

##### Share on other sites
3 minutes ago, Spaceception said:

Okay, thanks, does that mean it wouldn't work over a slightly egg shaped world? Would it not have any air on either side?

If parts of the world extend past wherever the atmosphere ends, then yes.

##### Share on other sites
2 minutes ago, Starwaster said:

If parts of the world extend past wherever the atmosphere ends, then yes.

Damn, my planet would've looked so unique too. Well, I can manage without that I guess.

##### Share on other sites

@Spaceception, there's some stuff you can do to make sure the atmosphere encompasses the whole planet, but it might not be entirely realistic.  If you end up making your planet and want some help or suggestions when it comes time to make the atmosphere, just ask me.  I'm sure I can figure something out.

##### Share on other sites
36 minutes ago, OhioBob said:

@Spaceception, there's some stuff you can do to make sure the atmosphere encompasses the whole planet, but it might not be entirely realistic.  If you end up making your planet and want some help or suggestions when it comes time to make the atmosphere, just ask me.  I'm sure I can figure something out.

The idea itself isn't the most realistic, so I'm fine with that I'm just going over tutorials now, I'll begin once I finish the 1st draft of Infinitum ##### Share on other sites
• 4 months later...

When you create a formula for the temperature profile for a small planet I have not encountered any problems, but when I reached the gas giants, I was faced with the problem of creating a formula for the temperature profile with logs. I would be grateful if you show me how to create a formula with logs. I'm sorry for my English.

##### Share on other sites
On ‎10‎/‎19‎/‎2017 at 12:05 PM, Danilka said:

When you create a formula for the temperature profile for a small planet I have not encountered any problems, but when I reached the gas giants, I was faced with the problem of creating a formula for the temperature profile with logs. I would be grateful if you show me how to create a formula with logs. I'm sorry for my English.

I don't know exactly what you're trying to do, but I do know that many diagrams showing the pressure profile of gas giants are plotted as temperature vs. log(pressure).  You can do that using my spreadsheet if you want.  You would just have to produce the temperature profile as a function of log(P) rather than as a function of altitude.  That will allow you to use the spreadsheet to generate the model.  However, temperatureCurve doesn't take logs, so you can't use that temperature profile in your planet config.  For temperatureCurve you'll have to produce a curve as a function of altitude.  You can extract the points for temperatureCurve from the spreadsheet using the same method that I describe for producing pressureCurve.  Pressure is a logarithmic function too, but we just approximate it using a float curve.

You can also use my spreadsheets that automate most of the work for you:

Edited by OhioBob
##### Share on other sites
• 1 year later...

nvm, I posted too quickly.

Edited by Erreur_418
Wrong information posted before

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.