public void Step() { if (mixerActive) { ratioOnetoTwo = InputOneAmount / 100f; PipeObject input1 = atmosNeighbours[0]; PipeObject input2 = atmosNeighbours[3]; PipeObject output = atmosNeighbours[1]; if (!input1 || !input2 || !output) { return; } float[] inputGasses1 = input1.GetAtmosContainer().GetGasses(); float[] inputGasses2 = input2.GetAtmosContainer().GetGasses(); float[] outputGasses = output.GetAtmosContainer().GetGasses(); if (input1.GetTotalMoles() <= 1f || input2.GetTotalMoles() <= 1f) { return; } if (output.GetPressure() <= _targetPressure) { float totalMoles = output.GetTotalMoles(); // Calculate necessary moles to transfer using PV=nRT float pressureDifference = _targetPressure - output.GetPressure(); float transferMoles = pressureDifference * 1000 * output.volume / (output.GetAtmosContainer().GetTemperature() * Gas.gasConstant); // We can not transfer more moles than the machinery allows transferMoles = Mathf.Min(Gas.maxMoleTransfer, transferMoles); float transfer_moles1 = ratioOnetoTwo * transferMoles; float transfer_moles2 = (1f - ratioOnetoTwo) * transferMoles; // We can't transfer more moles than there are float inputMoles1 = input1.GetTotalMoles(); float inputMoles2 = input2.GetTotalMoles(); // If one of the inputs didn't contain enough gas, scale the other down if (transfer_moles1 > input1.GetTotalMoles()) { transfer_moles2 = input1.GetTotalMoles() * (1 / ratioOnetoTwo) * (1 - ratioOnetoTwo); transfer_moles1 = input1.GetTotalMoles(); } if (transfer_moles2 > input2.GetTotalMoles()) { transfer_moles1 = input2.GetTotalMoles() * (1 / (1 - ratioOnetoTwo)) * ratioOnetoTwo; transfer_moles2 = input2.GetTotalMoles(); } if (transfer_moles1 > inputMoles1 || transfer_moles2 > inputMoles2) { Debug.LogError("More gas to be transfered than possible"); } for (int i = 0; i < Gas.numOfGases; i++) { // Input 1 and input 2 float molePerGas1 = (inputGasses1[i] / inputMoles1) * transfer_moles1; float molePerGas2 = (inputGasses2[i] / inputMoles2) * transfer_moles2; if (inputGasses1[i] >= molePerGas1 && inputGasses1[i] > 0.1f) { input1.GetAtmosContainer().RemoveGas(i, molePerGas1); output.GetAtmosContainer().AddGas(i, molePerGas1); } if (inputGasses2[i] >= molePerGas2 && inputGasses2[i] > 0.1f) { input2.GetAtmosContainer().RemoveGas(i, molePerGas2); output.GetAtmosContainer().AddGas(i, molePerGas2); } } input1.SetStateActive(); input2.SetStateActive(); output.SetStateActive(); } } }
public void Step() { // If both sides of the pump are connected if (pumpActive && atmosNeighbours[0] && atmosNeighbours[1]) { PipeObject input = atmosNeighbours[0]; PipeObject output = atmosNeighbours[1]; if (input.GetTotalMoles() == 0) { return; } float[] inputGasses = input.GetAtmosContainer().GetGasses(); float[] outputGasses = output.GetAtmosContainer().GetGasses(); // And the output pressure is acceptable if (pumpType == PumpType.Pressure) { if (output.GetPressure() <= TargetPressure - 0.1f) { float totalMoles = input.GetTotalMoles(); // Calculate necessary moles to transfer using PV=nRT float pressureDifference = TargetPressure - output.GetPressure(); float transferMoles = pressureDifference * 1000 * output.volume / (output.GetAtmosContainer().GetTemperature() * Gas.gasConstant); // Reach our target pressure in N steps transferMoles = transferMoles / stepsToEqualize; // We can't transfer more moles than there are if (transferMoles > totalMoles) { transferMoles = totalMoles; } for (int i = 0; i < Gas.numOfGases; i++) { // Divide the moles according to their percentage float molePerGas = (inputGasses[i] / totalMoles) * transferMoles; if (inputGasses[i] > 0f) { inputGasses[i] -= molePerGas; outputGasses[i] += molePerGas; } } input.SetStateActive(); output.SetStateActive(); } } // TODO: different pump speeds between volume/pressure pumps else if (pumpType == PumpType.Volume) { // At 200 L/s float inputVolume = input.volume * 1000 / CurrentVolumeSetting; float transferMoles = input.GetPressure() * 1000 * inputVolume / (input.GetAtmosContainer().GetTemperature() * Gas.gasConstant); float totalMoles = input.GetTotalMoles(); for (int i = 0; i < Gas.numOfGases; i++) { // We can't transfer more moles than there are if (transferMoles > totalMoles) { transferMoles = totalMoles; } // Divide the moles according to their percentage float molePerGas = (inputGasses[i] / totalMoles) * transferMoles; if (inputGasses[i] >= molePerGas) { inputGasses[i] -= molePerGas; outputGasses[i] += molePerGas; input.SetStateActive(); output.SetStateActive(); } } } } }