Skip to content

Commit

Permalink
Merge pull request #191 from firedrakeproject/test_assert_messages
Browse files Browse the repository at this point in the history
Add messages to the assertions in the test suite
  • Loading branch information
colinjcotter authored Jun 4, 2024
2 parents 9c9707b + ef542be commit 1c9192d
Show file tree
Hide file tree
Showing 13 changed files with 421 additions and 407 deletions.
7 changes: 4 additions & 3 deletions asQ/allatonce/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,16 @@ def vec_assign(x, y):
return self.update_time_halos(blocking=blocking)

@profiler()
def zero(self, subset=None):
def zero(self, subset=None, zero_ics=True):
"""
Set all values to zero.
:arg subset: pyop2.types.set.Subset indicating the nodes to zero.
If None then the whole function is zeroed.
"""
funcs = [self._fbuf, self.uprev, self.unext]
if hasattr(self, 'initial_condition'):
funcs = [self[i] for i in range(self.nlocal_timesteps)]
funcs.extend([self.uprev, self.unext])
if hasattr(self, 'initial_condition') and zero_ics:
funcs.append(self.initial_condition)
for f in funcs:
f.zero(subset=subset)
Expand Down
34 changes: 20 additions & 14 deletions tests/allatonce/test_allatonceform.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_heat_form(bc_opt, alpha):
"""
Test that assembling the AllAtOnceForm is the same as assembling the
slice-local part of an all-at-once form for the whole timeseries.
Diffusion coefficient is time-dependent.
"""

# build the all-at-once function
Expand Down Expand Up @@ -54,7 +55,7 @@ def test_heat_form(bc_opt, alpha):

def form_function(u, v, t):
c = fd.Constant(0.1)
nu = fd.Constant(1) + c*fd.inner(u, u)
nu = fd.Constant(1) + t*c*fd.inner(u, u)
return fd.inner(nu*fd.grad(u), fd.grad(v))*fd.dx

def form_mass(u, v):
Expand Down Expand Up @@ -127,7 +128,7 @@ def form_mass(u, v):
userial = Ffull.subfunctions[windx]
uparallel = aaoform.F[step].subfunctions[0]
err = fd.errornorm(userial, uparallel)
assert (err < 1e-12)
assert (err < 1e-12), "Each component of AllAtOnceForm residual should match component of monolithic residual calculated locally"


@pytest.mark.parametrize("bc_opt", bc_opts)
Expand Down Expand Up @@ -243,12 +244,15 @@ def form_mass(u, p, v, q):
userial = Ffull.subfunctions[windx]
uparallel = aaoform.F[step].subfunctions[cpt]
err = fd.errornorm(userial, uparallel)
assert (err < 1e-12)
assert (err < 1e-12), "Each component of AllAtOnceForm residual should match component of monolithic residual calculated locally"


@pytest.mark.parallel(nprocs=4)
def test_time_update():
# Given that the initial time step is at t=1. Test if we have correct time update from the first window to the next one.
"""
Given that the initial time step is at t=1, test if we have
correct time update from the first window to the next one.
"""

nslices = fd.COMM_WORLD.size//2
slice_length = 2
Expand Down Expand Up @@ -280,7 +284,7 @@ def form_mass(u, v):
form_mass, form_function,
alpha=alpha)

# The time series we get from the allatonce form
# Collect times for whole timeseries on every rank.
times = asQ.SharedArray(time_partition, comm=ensemble.ensemble_comm)

for i in range(aaofunc.nlocal_timesteps):
Expand All @@ -289,27 +293,29 @@ def form_mass(u, v):

assert (float(aaoform.t0) == 0)
for i in range(aaofunc.ntimesteps):
assert (times.dglobal[i] == ((i + 1)*dt))
# Test time seried of the second window with the optional argument t=0.
assert (times.dglobal[i] == ((i + 1)*dt)), "AllAtOnceForm.time should be multiples of dt at creation"

# Test time series of the second window with the optional argument t=t1.
np.random.seed(10)
t_1 = np.random.random()
aaoform.time_update(t=t_1)
t1 = np.random.random()
aaoform.time_update(t=t1)

for i in range(aaofunc.nlocal_timesteps):
times.dlocal[i] = aaoform.time[i]
times.synchronise()

assert (float(aaoform.t0) == t_1)
assert (float(aaoform.t0) == t1)
for i in range(aaofunc.ntimesteps):
assert (times.dglobal[i] == (t_1 + (i + 1)*dt))
assert (times.dglobal[i] == (t1 + (i + 1)*dt)), "aaoform.time_update(t1) should offset new times _to_ t1"

# Test the time series of the third window with the optional argument t=None.
# Test the time series of the third window without the optional argument t.
aaoform.time_update()

for i in range(aaofunc.nlocal_timesteps):
times.dlocal[i] = aaoform.time[i]
times.synchronise()

assert (float(aaoform.t0) == t_1 + 4*dt)
nt = aaoform.ntimesteps
assert (float(aaoform.t0) == t1 + nt*dt)
for i in range(aaofunc.ntimesteps):
assert (times.dglobal[i] == (t_1 + 4*dt + (i + 1)*dt))
assert (times.dglobal[i] == (t1 + nt*dt + (i + 1)*dt)), "aaoform.time_update() should offset new times _by_ window duration"
Loading

0 comments on commit 1c9192d

Please sign in to comment.