Skip to content

Commit 8e5bb1d

Browse files
committed
elastic batched tests pass
1 parent 2601d5e commit 8e5bb1d

1 file changed

Lines changed: 38 additions & 11 deletions

File tree

tests/test_elastic.py

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,20 @@ def test_gradcheck_only_mu_2d():
588588
source_requires_grad=False,
589589
)
590590

591+
def test_gradcheck_batched_lamb_2d():
592+
"""Test gradcheck in a 2D model with batched lamb."""
593+
run_gradcheck_2d(propagator=elasticprop,
594+
lamb=torch.tensor([[[DEFAULT_LAMB]],[[DEFAULT_LAMB*1.2]]]))
595+
596+
def test_gradcheck_batched_mu_2d():
597+
"""Test gradcheck in a 2D model with batched mu."""
598+
run_gradcheck_2d(propagator=elasticprop,
599+
mu=torch.tensor([[[DEFAULT_MU]],[[DEFAULT_MU*1.2]]]))
600+
601+
def test_gradcheck_batched_buoyancy_2d():
602+
"""Test gradcheck in a 2D model with batched buoyancy."""
603+
run_gradcheck_2d(propagator=elasticprop,
604+
buoyancy=torch.tensor([[[DEFAULT_BUOYANCY]],[[DEFAULT_BUOYANCY*1.2]]]))
591605

592606
def _set_sources(x_s, freq, dt, nt, dtype=None, dpeak_time=0.3):
593607
"""Create sources with amplitudes that have randomly shifted start times.
@@ -834,12 +848,12 @@ def run_gradcheck(mlamb,
834848
torch.manual_seed(1)
835849
if device is None:
836850
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
837-
lamb = (torch.ones(*nx, device=device, dtype=dtype) * mlamb +
838-
torch.randn(*nx, dtype=dtype).to(device) * dlamb)
839-
mu = (torch.ones(*nx, device=device, dtype=dtype) * mmu +
840-
torch.randn(*nx, dtype=dtype).to(device) * dmu)
841-
buoyancy = (torch.ones(*nx, device=device, dtype=dtype) * mbuoyancy +
842-
torch.randn(*nx, dtype=dtype).to(device) * dbuoyancy)
851+
lamb = torch.ones(*nx, device=device, dtype=dtype) * mlamb
852+
lamb += torch.randn(*lamb.shape, dtype=dtype).to(device) * dlamb
853+
mu = torch.ones(*nx, device=device, dtype=dtype) * mmu
854+
mu += torch.randn(*mu.shape, dtype=dtype).to(device) * dmu
855+
buoyancy = torch.ones(*nx, device=device, dtype=dtype) * mbuoyancy
856+
buoyancy += torch.randn(*buoyancy.shape, dtype=dtype).to(device) * dbuoyancy
843857

844858
nx = torch.Tensor(nx).long()
845859
dx = torch.Tensor(dx)
@@ -877,21 +891,34 @@ def run_gradcheck(mlamb,
877891
if isinstance(pml_width, int):
878892
pml_width = [pml_width for _ in range(4)]
879893

880-
if mlamb != 0:
894+
if isinstance(mlamb, torch.Tensor):
895+
min_mlamb = mlamb.abs().min()
896+
else:
897+
min_mlamb = mlamb
898+
if isinstance(mmu, torch.Tensor):
899+
min_mmu = mmu.abs().min()
900+
else:
901+
min_mmu = mmu
902+
if isinstance(mbuoyancy, torch.Tensor):
903+
min_mbuoyancy = mbuoyancy.abs().min()
904+
else:
905+
min_mbuoyancy = mbuoyancy
906+
907+
if min_mlamb != 0:
881908
lamb /= mlamb
882-
if mmu != 0:
909+
if min_mmu != 0:
883910
mu /= mmu
884-
if mbuoyancy != 0:
911+
if min_mbuoyancy != 0:
885912
buoyancy /= mbuoyancy
886913

887914
lamb.requires_grad_(lamb_requires_grad)
888915
mu.requires_grad_(mu_requires_grad)
889916
buoyancy.requires_grad_(buoyancy_requires_grad)
890917

891918
def wrap(lamb, mu, buoyancy, sources_y_amplitude, sources_x_amplitude):
892-
if sources_y_amplitude is not None and mbuoyancy != 0:
919+
if sources_y_amplitude is not None and min_mbuoyancy != 0:
893920
sources_y_amplitude = sources_y_amplitude / mbuoyancy / dt
894-
if sources_x_amplitude is not None and mbuoyancy != 0:
921+
if sources_x_amplitude is not None and min_mbuoyancy != 0:
895922
sources_x_amplitude = sources_x_amplitude / mbuoyancy / dt
896923
out = propagator(lamb * mlamb,
897924
mu * mmu,

0 commit comments

Comments
 (0)