Skip to content

Commit d0c078e

Browse files
committed
more more
1 parent b1c6432 commit d0c078e

21 files changed

+527
-682
lines changed

cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/runner/FullFluidSimulation.scala

Lines changed: 17 additions & 357 deletions
Large diffs are not rendered by default.

cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/solver/FieldUtils.scala

Lines changed: 0 additions & 168 deletions
This file was deleted.

cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/solver/AdvectionProgram.scala renamed to cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/solver/programs/AdvectionProgram.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package io.computenode.cyfra.fluids.solver
1+
package io.computenode.cyfra.fluids.solver.programs
22

33
import io.computenode.cyfra.core.GProgram
44
import io.computenode.cyfra.core.GProgram.StaticDispatch
55
import io.computenode.cyfra.dsl.{*, given}
6-
import GridUtils.*
6+
import io.computenode.cyfra.fluids.solver.*
7+
import io.computenode.cyfra.fluids.solver.utils.GridUtils.*
78

89
object AdvectionProgram:
910

@@ -43,14 +44,11 @@ object AdvectionProgram:
4344
val (x, y, z) = idxTo3D(idx, n)
4445
val pos = vec3(x.asFloat, y.asFloat, z.asFloat)
4546

46-
// Read velocity from previous buffer (pure operation)
4747
val vel = GIO.read(state.velocityPrevious, idx)
4848

49-
// Backtrace to previous position (use xyz components only)
5049
val vel3 = vec3(vel.x, vel.y, vel.z)
5150
val prevPos = pos - vel3 * params.dt
5251

53-
// Interpolate values at previous position
5452
val interpolatedVel = trilinearInterpolateVec4(
5553
state.velocityPrevious,
5654
prevPos,
@@ -75,7 +73,6 @@ object AdvectionProgram:
7573
n
7674
)
7775

78-
// Write advected values to current buffers
7976
for
8077
_ <- GIO.write(state.velocityCurrent, idx, interpolatedVel)
8178
_ <- GIO.write(state.densityCurrent, idx, interpolatedDensity)

cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/solver/BoundaryProgram.scala renamed to cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/solver/programs/BoundaryProgram.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package io.computenode.cyfra.fluids.solver
1+
package io.computenode.cyfra.fluids.solver.programs
22

33
import io.computenode.cyfra.core.GProgram
44
import io.computenode.cyfra.core.GProgram.StaticDispatch
55
import io.computenode.cyfra.dsl.{*, given}
6-
import io.computenode.cyfra.fluids.solver.GridUtils.idxTo3D
6+
import io.computenode.cyfra.fluids.solver.*
7+
import io.computenode.cyfra.fluids.solver.utils.{GridUtils, ObstacleUtils}
8+
import GridUtils.idxTo3D
79

810
/** Applies no-slip boundary conditions at domain walls and obstacles */
911
object BoundaryProgram:
@@ -35,19 +37,14 @@ object BoundaryProgram:
3537
val totalCells = n * n * n
3638

3739
GIO.when(idx < totalCells):
38-
// Convert 1D index to 3D coordinates
3940
val (x, y, z) = idxTo3D(idx, n)
4041

41-
// Check if on domain boundary
4242
val onDomainBoundary = (x === 0) || (x === n - 1) ||
4343
(y === 0) || (y === n - 1) ||
4444
(z === 0) || (z === n - 1)
4545

46-
// Check if inside obstacle
4746
val isSolid = ObstacleUtils.isSolid(state.obstacles, idx, totalCells)
4847

49-
// Apply no-slip conditions at walls or obstacles
5048
GIO.when(onDomainBoundary || isSolid):
51-
// No-slip: velocity = 0 at walls/obstacles
5249
val boundaryVel = vec4(0.0f, 0.0f, 0.0f, 0.0f)
5350
GIO.write(state.velocity, idx, boundaryVel)

cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/solver/DiffusionProgram.scala renamed to cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/solver/programs/DiffusionProgram.scala

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package io.computenode.cyfra.fluids.solver
1+
package io.computenode.cyfra.fluids.solver.programs
22

33
import io.computenode.cyfra.core.GProgram
44
import io.computenode.cyfra.core.GProgram.StaticDispatch
55
import io.computenode.cyfra.dsl.{*, given}
6-
import GridUtils.*
6+
import io.computenode.cyfra.fluids.solver.*
7+
import io.computenode.cyfra.fluids.solver.utils.GridUtils.*
78

89
/** Implements diffusion via Jacobi iteration.
910
* Solves: (I - ν·Δt·∇²)v_new = v_old
@@ -43,17 +44,13 @@ object DiffusionProgram:
4344
val totalCells = n * n * n
4445

4546
GIO.when(idx < totalCells):
46-
// Convert 1D index to 3D coordinates
4747
val (x, y, z) = idxTo3D(idx, n)
4848

49-
// Jacobi iteration coefficients
5049
val alpha = 1.0f / (params.viscosity * params.dt)
5150
val beta = 1.0f / (6.0f + alpha)
5251

53-
// Read center value from previous buffer (pure operation)
5452
val center = GIO.read(state.velocityPrevious, idx)
5553

56-
// Sample six neighbors from previous buffer (pure operations)
5754
val xm = readVec4Safe(state.velocityPrevious, x - 1, y, z, n)
5855
val xp = readVec4Safe(state.velocityPrevious, x + 1, y, z, n)
5956
val ym = readVec4Safe(state.velocityPrevious, x, y - 1, z, n)
@@ -63,8 +60,6 @@ object DiffusionProgram:
6360

6461
val neighborSum = xm + xp + ym + yp + zm + zp
6562

66-
// Jacobi update: x = (b + Σneighbors) / (diagonal coeff + 6)
6763
val newVel = (center * alpha + neighborSum) * beta
6864

69-
// Write result to current buffer
7065
GIO.write(state.velocityCurrent, idx, newVel)

cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/solver/DissipativeBoundaryProgram.scala renamed to cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/solver/programs/DissipativeBoundaryProgram.scala

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package io.computenode.cyfra.fluids.solver
1+
package io.computenode.cyfra.fluids.solver.programs
22

33
import io.computenode.cyfra.core.GProgram
44
import io.computenode.cyfra.core.GProgram.StaticDispatch
55
import io.computenode.cyfra.dsl.{*, given}
6-
import io.computenode.cyfra.fluids.solver.GridUtils.idxTo3D
6+
import io.computenode.cyfra.fluids.solver.*
7+
import io.computenode.cyfra.fluids.solver.utils.{GridUtils, ObstacleUtils}
8+
import GridUtils.idxTo3D
79

810
/** Free-slip boundary conditions with density/temperature dissipation.
911
*
@@ -46,7 +48,6 @@ object DissipativeBoundaryProgram:
4648
(z === 0) || (z === n - 1)
4749
val isSolid = ObstacleUtils.isSolid(state.obstacles, idx, totalCells)
4850

49-
// Check if adjacent to an obstacle (for obstacle surface boundary conditions)
5051
val solidXP = ObstacleUtils.isSolidAt(state.obstacles, x + 1, y, z, n)
5152
val solidXM = ObstacleUtils.isSolidAt(state.obstacles, x - 1, y, z, n)
5253
val solidYP = ObstacleUtils.isSolidAt(state.obstacles, x, y + 1, z, n)
@@ -55,59 +56,48 @@ object DissipativeBoundaryProgram:
5556
val solidZM = ObstacleUtils.isSolidAt(state.obstacles, x, y, z - 1, n)
5657
val adjacentToObstacle = (solidXP || solidXM || solidYP || solidYM || solidZP || solidZM) && !isSolid
5758

58-
// Compose both GIO operations
5959
for
60-
// 1. Handle obstacles - zero out everything inside
6160
_ <- GIO.when(isSolid):
6261
for
6362
_ <- GIO.write(state.velocity, idx, vec4(0.0f, 0.0f, 0.0f, 0.0f))
6463
_ <- GIO.write(state.density, idx, 0.0f)
6564
_ <- GIO.write(state.temperature, idx, 0.0f)
6665
yield GStruct.Empty()
6766

68-
// 2. Free-slip at obstacle surfaces (fluid cells adjacent to obstacles)
6967
_ <- GIO.when(adjacentToObstacle && !onDomainBoundary):
7068
val vel = state.velocity.read(idx)
7169
val vel3 = vec3(vel.x, vel.y, vel.z)
7270

73-
// Compute normal pointing away from obstacle (sum of normals for all adjacent solid faces)
7471
val nx = when(solidXP)(1.0f).otherwise(0.0f) + when(solidXM)(-1.0f).otherwise(0.0f)
7572
val ny = when(solidYP)(1.0f).otherwise(0.0f) + when(solidYM)(-1.0f).otherwise(0.0f)
7673
val nz = when(solidZP)(1.0f).otherwise(0.0f) + when(solidZM)(-1.0f).otherwise(0.0f)
7774
val obstacleNormal = vec3(nx, ny, nz)
7875

79-
// Normalize
8076
val normalLength = sqrt((obstacleNormal dot obstacleNormal) + 1e-8f)
8177
val normalNorm = obstacleNormal * (1.0f / normalLength)
8278

83-
// Free-slip: remove normal component
8479
val normalComponent = vel3 dot normalNorm
8580
val tangentialVel = vel3 - (normalNorm * normalComponent)
8681
val newVel = vec4(tangentialVel.x, tangentialVel.y, tangentialVel.z, 0.0f)
8782

8883
GIO.write(state.velocity, idx, newVel)
8984

90-
// 3. Free-slip with dissipation at domain boundaries
9185
_ <- GIO.when(onDomainBoundary && !isSolid && !adjacentToObstacle):
9286
val vel = state.velocity.read(idx)
9387
val vel3 = vec3(vel.x, vel.y, vel.z)
9488

95-
// Domain boundary normals
9689
val nx = when(x === 0)(1.0f).elseWhen(x === n - 1)(-1.0f).otherwise(0.0f)
9790
val ny = when(y === 0)(1.0f).elseWhen(y === n - 1)(-1.0f).otherwise(0.0f)
9891
val nz = when(z === 0)(1.0f).elseWhen(z === n - 1)(-1.0f).otherwise(0.0f)
9992
val normal = vec3(nx, ny, nz)
10093

101-
// Normalize for corners (where multiple components are non-zero)
10294
val normalLength = sqrt((normal dot normal) + 1e-8f)
10395
val normalNorm = normal * (1.0f / normalLength)
10496

105-
// Free-slip: remove normal component
10697
val normalComponent = vel3 dot normalNorm
10798
val tangentialVel = vel3 - (normalNorm * normalComponent)
10899
val newVel = vec4(tangentialVel.x, tangentialVel.y, tangentialVel.z, 0.0f)
109100

110-
// Dissipate density and temperature at domain boundaries
111101
val currentDensity = state.density.read(idx)
112102
val currentTemp = state.temperature.read(idx)
113103
val dissipationFactor = 0.95f

cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/solver/ForcesProgram.scala renamed to cyfra-fluids/src/main/scala/io/computenode/cyfra/fluids/solver/programs/ForcesProgram.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package io.computenode.cyfra.fluids.solver
1+
package io.computenode.cyfra.fluids.solver.programs
22

33
import io.computenode.cyfra.core.GProgram
44
import io.computenode.cyfra.core.GProgram.StaticDispatch
55
import io.computenode.cyfra.dsl.{*, given}
6+
import io.computenode.cyfra.fluids.solver.*
67

78
object ForcesProgram:
89

@@ -32,11 +33,9 @@ object ForcesProgram:
3233
val totalCells = params.gridSize * params.gridSize * params.gridSize
3334

3435
GIO.when(idx < totalCells):
35-
// Read values
3636
val oldVel = GIO.read(state.velocity, idx)
3737
val temp = GIO.read(state.temperature, idx)
3838

39-
// Compute buoyancy force + wind (Vec4 with w=0)
4039
val forces = vec4(
4140
params.windX,
4241
params.buoyancy * (temp - params.ambient) + params.windY,
@@ -45,5 +44,4 @@ object ForcesProgram:
4544
)
4645
val newVel = oldVel + forces * params.dt
4746

48-
// Write result
4947
GIO.write(state.velocity, idx, newVel)

0 commit comments

Comments
 (0)