Skip to content

Commit

Permalink
simplify transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
stilnat committed Nov 24, 2024
1 parent 78a137d commit b502b0e
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down Expand Up @@ -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;

Expand All @@ -73,17 +78,17 @@ 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);

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;

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace SS3D.Engine.AtmosphericsRework
Expand Down Expand Up @@ -107,6 +108,11 @@ public AtmosContainer GetTileAtmosObject(Vector3 worldPosition, TileLayer layer)
vector = GetXY(worldPosition);
return GetTileAtmosObject(vector.x, vector.y, layer);
}

public List<AtmosContainer> GetAllAtmosObjects()
{
return new(_atmosGridList.Concat(_atmosPipeLeftList));
}

public List<AtmosContainer> GetAllTileAtmosObjects()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
Expand All @@ -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
{
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MoleTransferToNeighbours> _moleTransfers;

[NativeDisableParallelForRestriction]
private NativeArray<AtmosObject> _tileObjectBuffer;

[ReadOnly]
private NativeList<int> _activeIndexes;

public ComputeVelocityJob(NativeArray<AtmosObject> tileObjectBuffer,
NativeArray<MoleTransferToNeighbours> moleTransfers)
NativeArray<MoleTransferToNeighbours> moleTransfers, NativeList<int> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
1 change: 1 addition & 0 deletions Builds/Game/Config/permissions.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
john ServerOwner
editorUser ServerOwner
hostingUser Administrator
unknown ServerOwner

0 comments on commit b502b0e

Please sign in to comment.