From b502b0e88c88cbc40f6cbc325e3353cc06d457e6 Mon Sep 17 00:00:00 2001 From: stilnat Date: Sun, 24 Nov 2024 22:38:15 +0100 Subject: [PATCH] simplify transfer --- .../AtmosRework/AtmosCalculator.cs | 26 +++++++++++-------- .../Atmospherics/AtmosRework/AtmosChunk.cs | 6 +++++ .../AtmosRework/AtmosJobPersistentData.cs | 14 +++++++--- .../Atmospherics/AtmosRework/AtmosManager.cs | 12 ++++++--- .../Atmospherics/AtmosRework/AtmosObject.cs | 8 +++--- .../AtmosRework/ComputeVelocityJob.cs | 22 ++++++++++------ .../AtmosRework/Editor/AtmosEditor.cs | 8 +++--- .../Atmospherics/AtmosRework/GasConstants.cs | 2 +- .../AtmosRework/Machinery/UnaryVentAtmos.cs | 2 +- Builds/Game/Config/permissions.txt | 1 + 10 files changed, 66 insertions(+), 35 deletions(-) diff --git a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosCalculator.cs b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosCalculator.cs index 94400aef5..9831b7843 100644 --- a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosCalculator.cs +++ b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosCalculator.cs @@ -17,22 +17,27 @@ public static MoleTransferToNeighbours SimulateGasTransfers(AtmosObject atmos, i // moles of each gaz from each neighbour to transfer. float4x4 molesToTransfer = 0; + float4 enteringVelocity = float4.zero; // Compute the amount of moles to transfer in each direction like if there was an infinite amount of moles if (hasNeighbour[0]) { - molesToTransfer[0] = MolesToTransfer(northNeighbour, ref atmos, activeFlux, dt, atmos.VelocityNorth, northNeighbour.VelocitySouth); + enteringVelocity = hasNeighbour[1] ? southNeighbour.VelocityNorth : 0; + molesToTransfer[0] = MolesToTransfer(northNeighbour, atmos, activeFlux, dt, enteringVelocity, northNeighbour.VelocitySouth); } if (hasNeighbour[1]) { - molesToTransfer[1] = MolesToTransfer(southNeighbour, ref atmos, activeFlux, dt, atmos.VelocitySouth, southNeighbour.VelocityNorth); + enteringVelocity = hasNeighbour[0] ? northNeighbour.VelocitySouth : 0; + molesToTransfer[1] = MolesToTransfer(southNeighbour, atmos, activeFlux, dt, enteringVelocity, southNeighbour.VelocityNorth); } if (hasNeighbour[2]) { - molesToTransfer[2] = MolesToTransfer(eastNeighbour, ref atmos, activeFlux, dt, atmos.VelocityEast, eastNeighbour.VelocityWest); + enteringVelocity = hasNeighbour[3] ? westNeighbour.VelocityEast : 0; + molesToTransfer[2] = MolesToTransfer(eastNeighbour, atmos, activeFlux, dt, enteringVelocity, eastNeighbour.VelocityWest); } if (hasNeighbour[3]) { - molesToTransfer[3] = MolesToTransfer(westNeighbour, ref atmos, activeFlux, dt, atmos.VelocityWest, westNeighbour.VelocityEast); + enteringVelocity = hasNeighbour[2] ? eastNeighbour.VelocityWest : 0; + molesToTransfer[3] = MolesToTransfer(westNeighbour, atmos, activeFlux, dt, enteringVelocity, westNeighbour.VelocityEast); } @@ -64,7 +69,7 @@ public static MoleTransferToNeighbours SimulateGasTransfers(AtmosObject atmos, i } public static float4 MolesToTransfer(AtmosObject neighbour, - ref AtmosObject atmos, bool activeFlux, float dt, float atmosVelocity, float oppositeVelocity) + AtmosObject atmos, bool activeFlux, float dt, float4 enteringVelocity, float4 neighbourOppositeVelocity) { float4 molesToTransfer = 0; @@ -73,9 +78,9 @@ public static float4 MolesToTransfer(AtmosObject neighbour, return molesToTransfer; } - molesToTransfer = activeFlux ? ComputeActiveFluxMoles(ref atmos, neighbour, atmosVelocity, oppositeVelocity) : ComputeDiffusionMoles(ref atmos, neighbour); + molesToTransfer = activeFlux ? ComputeActiveFluxMoles(atmos, neighbour, enteringVelocity, neighbourOppositeVelocity) : ComputeDiffusionMoles(ref atmos, neighbour); - molesToTransfer *= GasConstants.simSpeed * dt; + molesToTransfer *= dt * GasConstants.simSpeed; // We only care about what we transfer here, not what we receive molesToTransfer = math.max(0f, molesToTransfer); @@ -83,7 +88,7 @@ public static float4 MolesToTransfer(AtmosObject neighbour, return molesToTransfer; } - private static float4 ComputeActiveFluxMoles(ref AtmosObject atmos, AtmosObject neighbour, float atmosVelocity, float oppositeVelocity) + private static float4 ComputeActiveFluxMoles(AtmosObject atmos, AtmosObject neighbour, float4 enteringVelocity, float4 neighbourOppositeVelocity) { float neighbourPressure = neighbour.Pressure; @@ -105,9 +110,8 @@ private static float4 ComputeActiveFluxMoles(ref AtmosObject atmos, AtmosObject // Use partial pressures to determine how much of each gas to move. float4 partialPressureDifference = atmos.GetAllPartialPressures() - neighbour.GetAllPartialPressures(); - // Determine the amount of moles by applying the ideal gas law and taking wind into account. - return (1 + (200f * math.max(0f, atmosVelocity - oppositeVelocity))) * partialPressureDifference * 1000f * atmos.GetVolume() / - (atmos.Temperature * GasConstants.gasConstant); + // Determine the amount of moles and taking wind into account. + return (0.9f * (enteringVelocity - neighbourOppositeVelocity)) + partialPressureDifference; } private static float4 ComputeDiffusionMoles(ref AtmosObject atmos, AtmosObject neighbour) diff --git a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosChunk.cs b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosChunk.cs index bb13beb7f..cdf8e1097 100644 --- a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosChunk.cs +++ b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosChunk.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace SS3D.Engine.AtmosphericsRework @@ -107,6 +108,11 @@ public AtmosContainer GetTileAtmosObject(Vector3 worldPosition, TileLayer layer) vector = GetXY(worldPosition); return GetTileAtmosObject(vector.x, vector.y, layer); } + + public List GetAllAtmosObjects() + { + return new(_atmosGridList.Concat(_atmosPipeLeftList)); + } public List GetAllTileAtmosObjects() { diff --git a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosJobPersistentData.cs b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosJobPersistentData.cs index 96a574ef0..b90ebe006 100644 --- a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosJobPersistentData.cs +++ b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosJobPersistentData.cs @@ -186,7 +186,7 @@ public void ClearAllGasses() { foreach (AtmosChunk atmosChunk in Map.GetAtmosChunks()) { - foreach (AtmosContainer tile in atmosChunk.GetAllTileAtmosObjects()) + foreach (AtmosContainer tile in atmosChunk.GetAllAtmosObjects()) { AtmosObject atmosObject = tile.AtmosObject; @@ -198,8 +198,16 @@ public void ClearAllGasses() ChunkKey = atmosObject.ChunkKey, Moles = atmosObject.CoreGasses, }; - - _atmosObjectsToChange.Add(tileChanges); + + switch (tile.Layer) + { + case TileLayer.Turf: + _atmosObjectsToChange.Add(tileChanges); + break; + case TileLayer.PipeLeft: + _pipeAtmosObjectsToChange.Add(tileChanges); + break; + } } } } diff --git a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosManager.cs b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosManager.cs index 0fe10ace6..37d20d84e 100644 --- a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosManager.cs +++ b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosManager.cs @@ -266,13 +266,15 @@ private void ScheduleTileObjectJobs(AtmosJobPersistentData atmosJob, float delta TransferFluxesJob transferActiveFluxesJob = new(atmosJob.MoleTransferArray, atmosJob.NativeAtmosTiles, atmosJob.NeighbourTileIndexes, atmosJob.ActiveEnvironmentIndexes, false); - ComputeVelocityJob velocityJob = new(atmosJob.NativeAtmosTiles, atmosJob.MoleTransferArray); + ComputeVelocityJob velocityJob = new(atmosJob.NativeAtmosTiles, atmosJob.MoleTransferArray, atmosJob.ActiveEnvironmentIndexes); // Pipe stuff SetActiveJob leftPipeSetActiveJob = new(atmosJob.NativeAtmosPipesLeft, atmosJob.NeighbourTileIndexes, atmosJob.ActiveLeftPipeIndexes, atmosJob.SemiActiveLeftPipeIndexes); ComputeFluxesJob leftPipeComputeFluxesJob = new(atmosJob.NativeAtmosPipesLeft, atmosJob.PipeMoleTransferArray, atmosJob.NeighbourTileIndexes, atmosJob.ActiveLeftPipeIndexes, deltaTime, true); TransferFluxesJob leftPipeTransferFluxesJob = new(atmosJob.PipeMoleTransferArray, atmosJob.NativeAtmosPipesLeft, atmosJob.NeighbourTileIndexes, atmosJob.ActiveLeftPipeIndexes, false); + + ComputeVelocityJob leftPipeVelocityJob = new(atmosJob.NativeAtmosPipesLeft, atmosJob.PipeMoleTransferArray, atmosJob.ActiveLeftPipeIndexes); if (_usesParallelComputation) { @@ -283,11 +285,12 @@ private void ScheduleTileObjectJobs(AtmosJobPersistentData atmosJob, float delta JobHandle transferDiffusionFluxHandle = transferDiffusionFluxJob.Schedule(diffusionFluxHandle); JobHandle activeFluxHandle = activeFluxesJob.Schedule(atmosJob.ActiveEnvironmentIndexes.Length, 64, transferDiffusionFluxHandle); JobHandle transferActiveFluxHandle = transferActiveFluxesJob.Schedule(activeFluxHandle); - JobHandle computeVelocityHandle = velocityJob.Schedule(atmosJob.AtmosTiles.Count, 64, transferActiveFluxHandle); + JobHandle computeVelocityHandle = velocityJob.Schedule(atmosJob.ActiveEnvironmentIndexes.Length, 64, transferActiveFluxHandle); JobHandle leftPipeSetActiveHandle = leftPipeSetActiveJob.Schedule(atmosJob.AtmosTiles.Count, 64, computeIndexesHandle); JobHandle leftPipeActiveFluxesHandle = leftPipeComputeFluxesJob.Schedule(atmosJob.ActiveLeftPipeIndexes.Length, 64, leftPipeSetActiveHandle); JobHandle leftPipeTransferFluxesHandle = leftPipeTransferFluxesJob.Schedule(leftPipeActiveFluxesHandle); + JobHandle computeLeftPipeVelocityHandle = leftPipeVelocityJob.Schedule(atmosJob.ActiveLeftPipeIndexes.Length, 64, leftPipeTransferFluxesHandle); _jobHandles.Add(computeIndexesHandle); _jobHandles.Add(setActiveHandle); @@ -296,11 +299,13 @@ private void ScheduleTileObjectJobs(AtmosJobPersistentData atmosJob, float delta _jobHandles.Add(activeFluxHandle); _jobHandles.Add(transferActiveFluxHandle); _jobHandles.Add(computeVelocityHandle); + _jobHandles.Add(leftPipeSetActiveHandle); _jobHandles.Add(leftPipeActiveFluxesHandle); _jobHandles.Add(leftPipeTransferFluxesHandle); + _jobHandles.Add(computeLeftPipeVelocityHandle); } else { @@ -310,11 +315,12 @@ private void ScheduleTileObjectJobs(AtmosJobPersistentData atmosJob, float delta transferDiffusionFluxJob.Run(); activeFluxesJob.Run(atmosJob.ActiveEnvironmentIndexes.Length); transferActiveFluxesJob.Run(); - velocityJob.Run(atmosJob.AtmosTiles.Count); + velocityJob.Run(atmosJob.ActiveEnvironmentIndexes.Length); leftPipeSetActiveJob.Run(atmosJob.AtmosTiles.Count); leftPipeComputeFluxesJob.Run(atmosJob.ActiveLeftPipeIndexes.Length); leftPipeTransferFluxesJob.Run(); + leftPipeVelocityJob.Run(atmosJob.ActiveLeftPipeIndexes.Length); } } diff --git a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosObject.cs b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosObject.cs index d49e3e31d..17565eff6 100644 --- a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosObject.cs +++ b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/AtmosObject.cs @@ -11,10 +11,10 @@ public struct AtmosObject public AtmosState State; public int2 ChunkKey; - public float VelocityNorth; - public float VelocitySouth; - public float VelocityEast; - public float VelocityWest; + public float4 VelocityNorth; + public float4 VelocitySouth; + public float4 VelocityEast; + public float4 VelocityWest; public float Volume { get; private set; } public float Temperature { get; private set; } diff --git a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/ComputeVelocityJob.cs b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/ComputeVelocityJob.cs index 46bd7f673..879e4e7ce 100644 --- a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/ComputeVelocityJob.cs +++ b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/ComputeVelocityJob.cs @@ -8,29 +8,35 @@ namespace SS3D.Engine.AtmosphericsRework { - [BurstCompile(FloatMode = FloatMode.Fast, FloatPrecision = FloatPrecision.Standard)] + //[BurstCompile(FloatMode = FloatMode.Fast, FloatPrecision = FloatPrecision.Standard)] public struct ComputeVelocityJob : IJobParallelFor { [ReadOnly] private NativeArray _moleTransfers; + [NativeDisableParallelForRestriction] private NativeArray _tileObjectBuffer; + [ReadOnly] + private NativeList _activeIndexes; + public ComputeVelocityJob(NativeArray tileObjectBuffer, - NativeArray moleTransfers) + NativeArray moleTransfers, NativeList activeIndexes) { _tileObjectBuffer = tileObjectBuffer; _moleTransfers = moleTransfers; + _activeIndexes = activeIndexes; } public void Execute(int index) { - AtmosObject atmosObject = _tileObjectBuffer[index]; - atmosObject.VelocityNorth = math.csum(_moleTransfers[index].TransferMolesNorth * GasConstants.coreGasDensity); - atmosObject.VelocitySouth = math.csum(_moleTransfers[index].TransferMolesSouth * GasConstants.coreGasDensity); - atmosObject.VelocityEast = math.csum(_moleTransfers[index].TransferMolesEast * GasConstants.coreGasDensity); - atmosObject.VelocityWest = math.csum(_moleTransfers[index].TransferMolesWest * GasConstants.coreGasDensity); - _tileObjectBuffer[index] = atmosObject; + int activeIndex = _activeIndexes[index]; + AtmosObject atmosObject = _tileObjectBuffer[activeIndex]; + atmosObject.VelocityNorth = _moleTransfers[activeIndex].TransferMolesNorth; + atmosObject.VelocitySouth = _moleTransfers[activeIndex].TransferMolesSouth; + atmosObject.VelocityEast = _moleTransfers[activeIndex].TransferMolesEast; + atmosObject.VelocityWest = _moleTransfers[activeIndex].TransferMolesWest; + _tileObjectBuffer[activeIndex] = atmosObject; } } } diff --git a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/Editor/AtmosEditor.cs b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/Editor/AtmosEditor.cs index bf4c55855..82f629357 100644 --- a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/Editor/AtmosEditor.cs +++ b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/Editor/AtmosEditor.cs @@ -351,10 +351,10 @@ private void DrawContentGizmo(AtmosObject atmosObject, Vector3 position, Color s private void DrawWindGizmo(AtmosObject atmosObject, Vector3 position) { - float velocityNorth = atmosObject.VelocityNorth / 30; - float velocitySouth = atmosObject.VelocitySouth / 30; - float velocityEast = atmosObject.VelocityEast / 30; - float velocityWest = atmosObject.VelocityWest / 30; + float velocityNorth = math.csum(atmosObject.VelocityNorth / 30); + float velocitySouth = math.csum(atmosObject.VelocitySouth / 30); + float velocityEast = math.csum(atmosObject.VelocityEast / 30); + float velocityWest = math.csum(atmosObject.VelocityWest / 30); Handles.color = Color.white; if (velocityNorth > 0f) diff --git a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/GasConstants.cs b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/GasConstants.cs index f5a8ec989..7659f44e2 100644 --- a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/GasConstants.cs +++ b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/GasConstants.cs @@ -31,7 +31,7 @@ public static class GasConstants public const bool useRealisticGasLaw = true; // Gas constants - public const float simSpeed = 1f; // Simulation speed + public const float simSpeed = 0.2f; // Simulation speed public const float gasConstant = 8.314f; // Universal gas constant // public const float drag = 0.95f; // Fluid drag, slows down flux so that gases don't infinitely slosh public const float thermalBase = 0.024f; // * volume | Rate of temperature equalization diff --git a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/Machinery/UnaryVentAtmos.cs b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/Machinery/UnaryVentAtmos.cs index 1702078cb..6adbc7dcd 100644 --- a/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/Machinery/UnaryVentAtmos.cs +++ b/Assets/Scripts/SS3D/Systems/Atmospherics/AtmosRework/Machinery/UnaryVentAtmos.cs @@ -64,7 +64,7 @@ public void Step() AtmosObject atmosToTransferTo = _operatingMode == OperatingMode.Pump ? atmosEnv : atmosPipe; AtmosObject atmosToTransferFrom = _operatingMode == OperatingMode.Suck ? atmosEnv : atmosPipe; - float4 toTransfer = AtmosCalculator.MolesToTransfer(atmosToTransferTo, ref atmosToTransferFrom, true, 0.1f, 0f, 0f); + float4 toTransfer = AtmosCalculator.MolesToTransfer(atmosToTransferTo, atmosToTransferFrom, true, 0.1f, 0f, 0f); if (math.any(toTransfer > 0f)) { diff --git a/Builds/Game/Config/permissions.txt b/Builds/Game/Config/permissions.txt index b747f028a..1b9faec81 100644 --- a/Builds/Game/Config/permissions.txt +++ b/Builds/Game/Config/permissions.txt @@ -1,3 +1,4 @@ john ServerOwner editorUser ServerOwner hostingUser Administrator +unknown ServerOwner