Add jet, surface and fixed-patch tests and runnable examples
This commit is contained in:
@@ -6,9 +6,9 @@ import unittest
|
||||
|
||||
import numpy as np
|
||||
import pycuda.driver as cuda
|
||||
import pycuda.autoinit
|
||||
|
||||
from CelerisLab.simulation import Simulation
|
||||
from ._cases import make_sim
|
||||
|
||||
|
||||
class TestUnifiedObs(unittest.TestCase):
|
||||
@@ -16,7 +16,7 @@ class TestUnifiedObs(unittest.TestCase):
|
||||
|
||||
def test_set_body_then_run_read_body(self):
|
||||
"""set_body (host-only), run, read_body returns finite force."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim = make_sim()
|
||||
nx = sim.lbm_cfg.nx
|
||||
ny = sim.lbm_cfg.ny
|
||||
sim.add_body("circle", center=(nx // 4, ny // 2), radius=8)
|
||||
@@ -35,7 +35,7 @@ class TestUnifiedObs(unittest.TestCase):
|
||||
|
||||
def test_skip_transfer(self):
|
||||
"""run(upload_act=False, sync_obs=False) should not crash."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim = make_sim()
|
||||
nx = sim.lbm_cfg.nx
|
||||
ny = sim.lbm_cfg.ny
|
||||
sim.add_body("circle", center=(nx // 4, ny // 2), radius=8)
|
||||
@@ -47,12 +47,13 @@ class TestUnifiedObs(unittest.TestCase):
|
||||
|
||||
def test_external_stream(self):
|
||||
"""Providing an external CUDA stream should not crash."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim = make_sim()
|
||||
nx = sim.lbm_cfg.nx
|
||||
ny = sim.lbm_cfg.ny
|
||||
sim.add_body("circle", center=(nx // 4, ny // 2), radius=8)
|
||||
sim.initialize()
|
||||
s = cuda.Stream()
|
||||
with sim.ctx.activate():
|
||||
s = cuda.Stream()
|
||||
sim.run(50, stream=s)
|
||||
force = sim.read_force(0)
|
||||
self.assertTrue(np.all(np.isfinite(force)),
|
||||
@@ -62,8 +63,8 @@ class TestUnifiedObs(unittest.TestCase):
|
||||
def test_read_body_before_run_returns_zeros(self):
|
||||
"""read_body before any run() should return zero force (buffer is
|
||||
initialized to zero during sync_to_gpu)."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim.add_body("circle", center=(128, 128), radius=8)
|
||||
sim = make_sim()
|
||||
sim.add_body("circle", center=(sim.lbm_cfg.nx // 4, sim.lbm_cfg.ny // 2), radius=8)
|
||||
sim.initialize()
|
||||
force = sim.read_force(0)
|
||||
np.testing.assert_array_equal(force, np.zeros(2, dtype=np.float32))
|
||||
@@ -71,7 +72,7 @@ class TestUnifiedObs(unittest.TestCase):
|
||||
|
||||
def test_drl_pattern(self):
|
||||
"""DRL-style loop: run → read → set → run → read."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim = make_sim()
|
||||
nx = sim.lbm_cfg.nx
|
||||
ny = sim.lbm_cfg.ny
|
||||
sim.add_body("circle", center=(nx // 4, ny // 2), radius=8)
|
||||
@@ -95,7 +96,7 @@ class TestObsZeroingAndNormalize(unittest.TestCase):
|
||||
def test_zero_obs_true_resets_force(self):
|
||||
"""run(zero_obs=True) resets the step counter; force per-step
|
||||
should be the same order of magnitude across blocks."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim = make_sim()
|
||||
nx = sim.lbm_cfg.nx
|
||||
ny = sim.lbm_cfg.ny
|
||||
sim.add_body("circle", center=(nx // 4, ny // 2), radius=8)
|
||||
@@ -115,7 +116,7 @@ class TestObsZeroingAndNormalize(unittest.TestCase):
|
||||
|
||||
def test_zero_obs_false_accumulates_force(self):
|
||||
"""run(zero_obs=False) twice → step counter accumulates."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim = make_sim()
|
||||
nx = sim.lbm_cfg.nx
|
||||
ny = sim.lbm_cfg.ny
|
||||
sim.add_body("circle", center=(nx // 4, ny // 2), radius=8)
|
||||
@@ -136,7 +137,7 @@ class TestObsZeroingAndNormalize(unittest.TestCase):
|
||||
|
||||
def test_zero_obs_true_resets_sensor(self):
|
||||
"""Sensor values should not spill across run() calls with zero_obs."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim = make_sim()
|
||||
nx = sim.lbm_cfg.nx
|
||||
ny = sim.lbm_cfg.ny
|
||||
sim.add_body("sensor", center=(nx // 2, ny // 2), radius=8)
|
||||
@@ -157,7 +158,7 @@ class TestObsZeroingAndNormalize(unittest.TestCase):
|
||||
|
||||
def test_normalize_divides_by_steps(self):
|
||||
"""read_force(normalize=True) should give per-step force."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim = make_sim()
|
||||
nx = sim.lbm_cfg.nx
|
||||
ny = sim.lbm_cfg.ny
|
||||
sim.add_body("circle", center=(nx // 4, ny // 2), radius=8)
|
||||
@@ -175,7 +176,7 @@ class TestObsZeroingAndNormalize(unittest.TestCase):
|
||||
def test_read_sensor_normalize_false(self):
|
||||
"""read_sensor(normalize=False) returns area-averaged but not
|
||||
time-averaged value."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim = make_sim()
|
||||
nx = sim.lbm_cfg.nx
|
||||
ny = sim.lbm_cfg.ny
|
||||
sim.add_body("sensor", center=(nx // 2, ny // 2), radius=8)
|
||||
@@ -193,7 +194,7 @@ class TestObsZeroingAndNormalize(unittest.TestCase):
|
||||
|
||||
def test_read_body_normalize(self):
|
||||
"""read_body(normalize=True) divides all fields by step count."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim = make_sim()
|
||||
nx = sim.lbm_cfg.nx
|
||||
ny = sim.lbm_cfg.ny
|
||||
sim.add_body("circle", center=(nx // 4, ny // 2), radius=8)
|
||||
@@ -212,8 +213,8 @@ class TestObsZeroingAndNormalize(unittest.TestCase):
|
||||
|
||||
def test_normalize_returns_zero_before_run(self):
|
||||
"""read(..., normalize=True) before any run() returns zeros."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim.add_body("circle", center=(128, 128), radius=8)
|
||||
sim = make_sim()
|
||||
sim.add_body("circle", center=(sim.lbm_cfg.nx // 4, sim.lbm_cfg.ny // 2), radius=8)
|
||||
sim.initialize()
|
||||
|
||||
force = sim.read_force(0, normalize=True)
|
||||
@@ -227,7 +228,7 @@ class TestObsZeroingAndNormalize(unittest.TestCase):
|
||||
def test_sensor_area_always_normalized(self):
|
||||
"""Sensor always does area-normalisation internally.
|
||||
normalize=False should NOT equal GPU raw (should be smaller by cell_count)."""
|
||||
sim = Simulation(device_id=0)
|
||||
sim = make_sim()
|
||||
nx = sim.lbm_cfg.nx
|
||||
ny = sim.lbm_cfg.ny
|
||||
sim.add_body("sensor", center=(nx // 2, ny // 2), radius=8)
|
||||
|
||||
Reference in New Issue
Block a user