Skip to content

Instantly share code, notes, and snippets.

@YotaXP
Last active July 7, 2016 16:57
Show Gist options
  • Save YotaXP/e341a7a6a9ebf13be97c6e9a869bf10f to your computer and use it in GitHub Desktop.
Save YotaXP/e341a7a6a9ebf13be97c6e9a869bf10f to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Collections.Generic;
public static class GasConstants
{
const float OneAtmosphere = 101.325; // kPa
const float IdealGasEquation = 8.31; // kPa*L/(K*mol)
const float TemperatureCosmicMicrowaveBackround = 2.7; // K
}
public sealed class GasType
{
public string Name { get; private set; }
public string ShortName { get; private set; }
public float SpecificHeat { get; private set; }
public int GasIndex { get; private set; }
private static int nextIndex = 0;
private static GasType[] byIndex = new GasType[Types];
public static GasType ByIndex(int index)
{
return byIndex[index];
}
private GasType(string name, string shortName, float specificHeat)
{
Name = name;
ShortName = shortName;
SpecificHeat = specificHeat;
GasIndex = nextIndex++;
byIndex[GasIndex] = this;
}
public static readonly GasType Oxygen = new GasType("oxygen", "O2", 20);
public static readonly GasType Nitrogen = new GasType("nitrogen", "N", 20);
public static readonly GasType Plasma = new GasType("plasma", "Pl", 300);
public static readonly GasType NitrousOxide = new GasType("nitrous oxide", "N2O", 40);
public static readonly GasType CarbonDioxide = new GasType("carbon dioxide", "CO2", 30);
public static readonly int Types = 5; // Make sure this is always accurate.
}
public class GasMixture
{
private float[] moles = new float[GasType.Types];
private float temperature = GasConstants.TemperatureCosmicMicrowaveBackround;
// Moles
public float this[GasType type] {
get {
return moles[type.GasIndex];
}
set
{
moles[type.GasIndex] = Math.Max(0, value);
}
}
// Returns in K
public float Temperature {
get { return temperature; }
set { temperature = Math.Max(value, GasConstants.TemperatureCosmicMicrowaveBackround); }
}
// Returns in kPa, volume in L
public float Pressure(float volume)
{
return moles.Sum() * GasConstants.IdealGasEquation * temperature / volume;
}
public float HeatCapacity()
{
return moles.Select((moles, index) => moles < float.Epsilon ? 0 : moles * GasType.ByIndex(index).SpecificHeat).Sum();
}
//public void AddGas(GasMixture otherMixture) { ... }
public void MoveGas(GasMixture otherMixture, float percentage) { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment