"module atmos.mix; import atmos; /** Can be treated as a AmountOfSubstance[GasDatum] for…
- User
"module atmos.mix;
import atmos;
/** Can be treated as a AmountOfSubstance[GasDatum] for all intents and purposes, but automatically asserts gases. It should be noted that iteration over the whole list is no slower for this. / struct GasEntries { AmountOfSubstance[GasDatum] gases; /// Gases by datum. bool dirty = true; /// Whether or not the gas entry has been added to recently. alias gases this; /** Accessing directly by gas datum instead of accessing gases, e.g. GasEntries[getGas!“plasma”] / pure @safe AmountOfSubstance opIndex(GasDatum gas) { return gases.get(gas,0mole); } /** Same as above, but assignment. Actually does some further calculation: leads to undefined behavior of the mole total is less than 0, if the mole total EQUALS zero then it deletes that particular entry for faster reactions, if the mole total is more than previous it sets the entire GasEntries to dirty to flag it ready to maybe react./ pure @safe AmountOfSubstance opIndexAssign(AmountOfSubstance moles,GasDatum gas) in { assert(moles>=0mole,“A negative mole value was assigned to “
gas.name”!”); } do { if(moles == 0mole) { gases.remove(gas); return 0mole; } else { dirty = moles>this[gas]; return gases[gas]=moles; } } / Same as assignment, but for +=, -=, *= and /=. Note that + and - require proper units–you need to input moles! / pure @safe AmountOfSubstance opIndexOpAssign(string op,T)(T value,GasDatum gas) { import std.traits : isNumeric; static if(((op==“/” || op=="“) && isNumeric!T) || ((op==”+" || op==“-”) && is(T : AmountOfSubstance))) { auto newAmount = mixin(“this[gas]”op“value”); return opIndexAssign(newAmount,gas); } else { import std.traits : fullyQualifiedName; assert(0,Invalid operator "op" for typefullyQualifiedName!T!); } } } / Proper atmos mixture. Stores all the required stuff, can calculate the rest as needed. / struct AtmosMixture { GasEntries gases; /// See GasEntries. alias gases this; Temperature temperature = 0kelvin; /// Current temperature in kelvins. Volume volume = cellVolume; /// Volume. Default is 2500 liters. AmountOfSubstance lastShare = 0mole; /// Unused for now, until a more accurate-to-SS13 mixing operation is put in. ReactionFlag previousResult = ReactionFlag.NO_REACTION; /// For faster reaction caching. this(Volume newVolume) /// Making a new mixture with a given volume, in liters. { volume=newVolume; } this(float newVolume) /// Making a new mixture with a given volume. Float will be converted to liters. { this(newVolumeliter); } pure @nogc @safe HeatCapacity heatCapacity() /// Returns current heat capacity of mixture, in joules/kelvin. { HeatCapacity totalAmount=0heatCapacityUnit; foreach(GasDatum gas,AmountOfSubstance amount;gases) { totalAmount+=gas.heatCapacity(amount); } return totalAmount; } pure @nogc @safe AmountOfSubstance totalMoles() /// Returns the total moles currently in the mixture, in moles. { AmountOfSubstance totalAmount = 0mole; foreach(GasDatum gas,AmountOfSubstance amount;gases) { totalAmount+=amount; } return totalAmount; } pure @nogc @safe Pressure pressure() /// Returns pressure, in pascals. in { assert(volume>0liter); //no division by zero } do { return (gasConstanttotalMolestemperature)/volume; } pure @nogc @safe Energy thermalEnergy() /// Returns thermal energy, in joules. { return heatCapacitytemperature; } pure @safe void merge(AtmosMixture other) /// Merges this atmos mixture with another. { temperature = (thermalEnergy + other.thermalEnergy) / (heatCapacity + other.heatCapacity); foreach(GasDatum gas,AmountOfSubstance amount;other.gases) { this.gases[gas]+=amount; } } pure @safe AtmosMixture remove(AmountOfSubstance amount) /// Removes a certain amount of moles from mix; returns mixture with all removed substance. { import std.algorithm.comparison : min; auto removed = AtmosMixture(); const auto cachedTotal = totalMoles; amount = min(amount,cachedTotal); foreach(GasDatum gas,AmountOfSubstance amount;gases) { removed.gases[gas] = (amount / cachedTotal)amount; amount -= removed.gases[gas]; } return removed; } pure @safe AtmosMixture removeRatio(float ratio) /// Removes a percentage of substance from mix; returns mixture with all removed substance. in { assert(ratio>0); } do { import std.algorithm.comparison : min; ratio = min(ratio,1); auto removed = AtmosMixture(); const auto cachedTotal = totalMoles; foreach(GasDatum gas,AmountOfSubstance amount;gases) { removed.gases[gas] = amount * ratio; amount -= removed.gases[gas]; } return removed; } pure @safe Pressure share(AtmosMixture other, int adjacentTurfs = 4) /// A port of /tg/'s sharing code. { //none of you are free of sin import std.math : abs; const auto temperatureDelta = temperature - other.temperature; const auto absTemperatureDelta = abs(temperatureDelta.value(kelvin))kelvin; const auto oldSelfHeatCapacity = heatCapacity; const auto oldOtherHeatCapacity = other.heatCapacity; HeatCapacity heatCapacitySelfToOther; HeatCapacity heatCapacityOtherToSelf; AmountOfSubstance movedMoles = 0mole; AmountOfSubstance absMovedMoles = 0mole; foreach(GasDatum gas,AmountOfSubstance amount;gases) { auto delta = (amount - other[gas])/(adjacentTurfs+1); auto gasHeatCapacity = gas.heatCapacity(delta); if(delta>0mole) { heatCapacitySelfToOther += gasHeatCapacity; } else { heatCapacityOtherToSelf -= gasHeatCapacity; } auto absMolesThisTime=abs(delta.value(mole))mole; if(absMolesThisTime>0.1mole) { this[gas] -= delta; other[gas] += delta; movedMoles += delta; absMovedMoles += absMolesThisTime; } } if(absMovedMoles>0.1mole) { lastShare = absMovedMoles; auto newSelfHeatCapacity = oldSelfHeatCapacity + heatCapacityOtherToSelf - heatCapacitySelfToOther; auto newOtherHeatCapacity = oldOtherHeatCapacity + heatCapacitySelfToOther - heatCapacityOtherToSelf; temperature = (oldSelfHeatCapacitytemperature - heatCapacitySelfToOthertemperature + heatCapacityOtherToSelfother.temperature)/newSelfHeatCapacity; - Assistant
Данный код - часть модуля atmos.mix на языке программирования D. Он описывает структуру AtmosMixture, которая представляет собой смесь газов в атмосфере.
Функция pressure() возвращает давление смеси в паскалях. Давление рассчитывается по формуле идеального газа: P = (RT) / V, где R - газовая постоянная, T - температура в кельвинах, V - объем.
Функция share() используется для распределения газов между несколькими тайлами (местами в атмосфере) согласно алгоритму, известному как "sharing code" из /tg/. Она принимает другую смесь газов (переменная other) и количество соседних тайлов (параметр adjacentTurfs). Функция сначала вычисляет разницу в температуре между текущей смесью и другой смесью, затем использует эту разницу и другие параметры (такие как емкость теплоты, количество вещества и т. д.) для расчета теплового обмена и перемещения частиц газов между смесями. Возвращает функция давление, которое определяется как разность давлений до и после перемещения газов.
- User
как функция share рассчитывает тепловой обмен и перемещения частиц газов между смесями?
- Assistant
Функция share() рассчитывает тепловой обмен и перемещение частиц газов между двумя смесями с использованием различных переменных и параметров.