Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Commit bc9c914

Browse files
committed
Examples: reworked 2nd order acoustic example to use new stencil evaluation and API
1 parent 32da209 commit bc9c914

5 files changed

Lines changed: 33 additions & 19 deletions

File tree

devitoboundary/stencil_cache.dat

30.8 KB
Binary file not shown.

devitoboundary/stencils/evaluation.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,9 @@ def evaluate_stencils(df, point_type, n_stencils, left_variants, right_variants,
282282

283283
if point_type == 'paired_right':
284284
dst = df.dist.to_numpy()[:, np.newaxis]
285+
# dst used to have a minus (this was wrong)
285286
eta_left = np.tile(df.eta_l.to_numpy()[:, np.newaxis],
286-
(1, n_stencils)) + n_stencils - dst - eta_base - 1
287+
(1, n_stencils)) + n_stencils + dst - eta_base - 1
287288
eta_right = np.tile(df.eta_r.to_numpy()[:, np.newaxis],
288289
(1, n_stencils)) + n_stencils - eta_base - 1
289290
for left_var in range(space_order+1):
@@ -294,7 +295,6 @@ def evaluate_stencils(df, point_type, n_stencils, left_variants, right_variants,
294295
func = coeff_functions[left_var, right_var, coeff]
295296
stencils[mask, coeff] = func(eta_left[mask],
296297
eta_right[mask])
297-
298298
return stencils
299299

300300

@@ -531,11 +531,10 @@ def get_component_weights(data, axis, function, deriv, stencil_generator):
531531
w_shape = f_grid.shape + (ncoeffs,)
532532
w_dims = f_grid.dimensions + (s_dim,)
533533

534-
w = Function(name='w', dimensions=w_dims, shape=w_shape)
534+
w = Function(name='w_'+axis_dim, dimensions=w_dims, shape=w_shape)
535535

536536
w.data[:] = standard_stencil(deriv, function.space_order)
537537

538-
# FIXME: All of these except "double" have a bug
539538
# Fill the stencils
540539
get_variants(first, function.space_order, 'first',
541540
axis_dim, stencil_generator, w)
@@ -553,7 +552,7 @@ def get_component_weights(data, axis, function, deriv, stencil_generator):
553552
get_variants(paired_right, function.space_order, 'paired_right',
554553
axis_dim, stencil_generator, w)
555554

556-
w.data[:] /= f_grid.spacing[axis] # Divide everything through by spacing
555+
w.data[:] /= f_grid.spacing[axis]**deriv # Divide everything through by spacing
557556

558557
return w
559558

@@ -589,13 +588,15 @@ def get_weights(data, function, deriv, bcs, offsets=(0, 0, 0)):
589588
stencil_file=stencil_file)
590589

591590
# FIXME: This will want to cope with varying numbers of dims in the future
591+
# FIXME: Why does the name fix work?
592592
weights = [None for i in range(3)]
593593
for axis in range(3):
594594
sten_gen.all_variants(deriv, offsets[axis])
595595
axis_weights = get_component_weights(data[axis].data, axis, function,
596596
deriv, sten_gen)
597+
# Am I doing something dumb here?
598+
print(deriv, function, function.grid.dimensions[axis], axis_weights)
597599
weights[axis] = Coefficient(deriv, function,
598600
function.grid.dimensions[axis],
599601
axis_weights)
600-
601602
return Substitutions(*tuple(weights))

devitoboundary/topography.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class ImmersedBoundary:
8383
----------
8484
name : str
8585
The name of the boundary surface.
86-
suface : str
86+
surface : str
8787
The path to the geometry file
8888
functions : pandas DataFrame
8989
A dataframe of the functions to which the immersed boundary surface is

examples/seismic_topography_example.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import matplotlib.pyplot as plt
21
import numpy as np
2+
import pandas as pd
33

44
from devito import Grid, TimeFunction, Eq, solve, Operator, ConditionalDimension
55
from devitoboundary import ImmersedBoundary
@@ -42,12 +42,14 @@
4242
# Surface configuration
4343
infile = 'topography/crater_lake.ply'
4444
# Zero even derivatives on the boundary
45-
bc_0 = Eq(generic_function(x_b), 0) # u(x_b) = 0
46-
bc_2 = Eq(generic_function(x_b, 2), 0) # d^2u/dx^2(x_b) = 0
47-
bcs = {u: [bc_0, bc_2]}
45+
bcs_u = [Eq(generic_function(x_b, 2*i), 0)
46+
for i in range(1+u.space_order//2)]
47+
functions = pd.DataFrame({'function': [u],
48+
'bcs': [bcs_u]},
49+
columns=['function', 'bcs'])
4850

4951
# Create the immersed boundary surface
50-
surface = ImmersedBoundary(infile, u, bcs)
52+
surface = ImmersedBoundary('topography', infile, functions)
5153

5254
# Configure the source
5355
time_range = TimeAxis(start=t0, stop=tn, step=dt)
@@ -60,11 +62,14 @@
6062
src.coordinates.data[0, -1] = -500 # 500m below sea level
6163

6264
# Configure derivative needed
63-
deriv = ('u.d2',)
65+
derivs = pd.DataFrame({'function': [u],
66+
'derivative': [2]},
67+
columns=['function', 'derivative'])
68+
coeffs = surface.subs(derivs)
6469

6570
# We can now write the PDE
6671
pde = VP*u.dt2 - u.laplace
67-
eq = Eq(pde, 0, coefficients=surface.subs(deriv))
72+
eq = Eq(pde, 0, coefficients=coeffs['substitution'].values[0])
6873

6974
# And set up the update
7075
stencil = solve(eq.evaluate, u.forward)
@@ -78,9 +83,9 @@
7883
# And run
7984
op.apply(dt=dt)
8085

81-
# outfile = 'data/seismic_topography_wavefield.npy'
82-
# np.save(outfile, usave.data)
83-
86+
outfile = 'data/seismic_topography_wavefield.npy'
87+
np.save(outfile, usave.data)
88+
"""
8489
plot_extent = [0, grid.extent[0],
8590
origin[2], grid.extent[2] + origin[2]]
8691
for i in range(usave.data.shape[0] - 1):
@@ -94,3 +99,4 @@
9499
plt.savefig("images/image-%s" % str(i))
95100
# plt.show()
96101
plt.close()
102+
"""

tests/test_evaluation.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,20 @@ def test_type_splitting(self, axis):
6565
assert(np.all(paired_left.index.get_level_values(xyz[axis]).to_numpy() == 3))
6666
assert(np.all(paired_right.index.get_level_values(xyz[axis]).to_numpy() == 5))
6767

68+
69+
class TestStencils:
70+
"""
71+
A class containing tests to check stencil evaluation.
72+
"""
73+
# FIXME: Need to check evaluate_stencils
74+
# FIXME: Need to check fill_weights
75+
# FIXME: Wants to check several grid spacings
6876
@pytest.mark.parametrize('axis', [0, 1, 2])
6977
@pytest.mark.parametrize('deriv', [1, 2])
7078
def test_stencil_evaluation(self, axis, deriv):
7179
"""
7280
A test to check that stencils are evaluated to their correct values.
7381
"""
74-
# FIXME: In wrong class
7582
def evaluate_variant(stencil_generator, left_var, right_var,
7683
left_eta, right_eta):
7784
"""Evaluate the specified stencil"""
@@ -132,7 +139,7 @@ def check_row(data, index, stencil_generator):
132139

133140
sten_gen = StencilGen(function.space_order, bcs,
134141
stencil_file=stencil_file)
135-
sten_gen.all_variants(deriv, 0)
142+
sten_gen.all_variants(deriv, 0.)
136143

137144
w = get_component_weights(distances, axis, function, deriv, sten_gen)
138145

0 commit comments

Comments
 (0)