Skip to content

Commit 56873be

Browse files
committed
examples: Update boundary conditions notebook to use new SubDomain API
1 parent e2af0fb commit 56873be

1 file changed

Lines changed: 49 additions & 47 deletions

File tree

examples/userapi/04_boundary_conditions.ipynb

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"cell_type": "markdown",
4242
"metadata": {},
4343
"source": [
44-
"We will need to use subdomains to accomodate the modified equations in the PML regions. As `Grid` objects cannot have subdomains added retroactively, we must define our subdomains beforehand."
44+
"We will need to use subdomains to accommodate the modified equations in the PML regions."
4545
]
4646
},
4747
{
@@ -52,75 +52,71 @@
5252
"source": [
5353
"from devito import SubDomain\n",
5454
"\n",
55-
"s_o = 6 # Space order\n",
55+
"so = 6 # Space order\n",
5656
"\n",
5757
"class MainDomain(SubDomain): # Main section with no damping\n",
5858
" name = 'main'\n",
59-
" def __init__(self, PMLS, S_O):\n",
60-
" super().__init__()\n",
61-
" self.PMLS = PMLS\n",
62-
" self.S_O = S_O\n",
59+
" def __init__(self, pmls, so, grid=None):\n",
60+
" # NOTE: These attributes are used in `define`, and thus must be\n",
61+
" # set up before `super().__init__` is called.\n",
62+
" self.pmls = pmls\n",
63+
" self.so = so\n",
64+
" super().__init__(grid=grid)\n",
6365
"\n",
6466
" def define(self, dimensions):\n",
6567
" x, y = dimensions\n",
66-
" return {x: ('middle', self.PMLS, self.PMLS),\n",
67-
" y: ('middle', self.S_O//2, self.PMLS)}\n",
68+
" return {x: ('middle', self.pmls, self.pmls),\n",
69+
" y: ('middle', self.so//2, self.pmls)}\n",
6870
"\n",
6971
"\n",
7072
"class Left(SubDomain): # Left PML region\n",
7173
" name = 'left'\n",
72-
" def __init__(self, PMLS):\n",
73-
" super().__init__()\n",
74-
" self.PMLS = PMLS\n",
74+
" def __init__(self, pmls, grid=None):\n",
75+
" self.pmls = pmls\n",
76+
" super().__init__(grid=grid)\n",
7577
"\n",
7678
" def define(self, dimensions):\n",
7779
" x, y = dimensions\n",
78-
" return {x: ('left', self.PMLS), y: y}\n",
80+
" return {x: ('left', self.pmls), y: y}\n",
7981
"\n",
8082
"\n",
8183
"class Right(SubDomain): # Right PML region\n",
8284
" name = 'right'\n",
83-
" def __init__(self, PMLS):\n",
84-
" super().__init__()\n",
85-
" self.PMLS = PMLS\n",
85+
" def __init__(self, pmls, grid=None):\n",
86+
" self.pmls = pmls\n",
87+
" super().__init__(grid=grid)\n",
8688
"\n",
8789
" def define(self, dimensions):\n",
8890
" x, y = dimensions\n",
89-
" return {x: ('right', self.PMLS), y: y}\n",
91+
" return {x: ('right', self.pmls), y: y}\n",
9092
"\n",
9193
"class Base(SubDomain): # Base PML region\n",
9294
" name = 'base'\n",
93-
" def __init__(self, PMLS):\n",
94-
" super().__init__()\n",
95-
" self.PMLS = PMLS\n",
95+
" def __init__(self, pmls, grid=None):\n",
96+
" self.pmls = pmls\n",
97+
" super().__init__(grid=grid)\n",
9698
"\n",
9799
" def define(self, dimensions):\n",
98100
" x, y = dimensions\n",
99-
" return {x: ('middle', self.PMLS, self.PMLS), y: ('right', self.PMLS)}\n",
101+
" return {x: ('middle', self.pmls, self.pmls), y: ('right', self.pmls)}\n",
100102
"\n",
101103
"class FreeSurface(SubDomain): # Free surface region\n",
102104
" name = 'freesurface'\n",
103-
" def __init__(self, PMLS, S_O):\n",
104-
" super().__init__()\n",
105-
" self.PMLS = PMLS\n",
106-
" self.S_O = S_O\n",
105+
" def __init__(self, pmls, so, grid=None):\n",
106+
" self.pmls = pmls\n",
107+
" self.so = so\n",
108+
" super().__init__(grid=grid)\n",
107109
"\n",
108110
" def define(self, dimensions):\n",
109111
" x, y = dimensions\n",
110-
" return {x: ('middle', self.PMLS, self.PMLS), y: ('left', self.S_O//2)}\n",
111-
"\n",
112-
"main_domain = MainDomain(nbpml, s_o)\n",
113-
"left = Left(nbpml)\n",
114-
"right = Right(nbpml)\n",
115-
"base = Base(nbpml)\n",
116-
"freesurface = FreeSurface(nbpml, s_o)"
112+
" return {x: ('middle', self.pmls, self.pmls), y: ('left', self.so//2)}"
117113
]
118114
},
119115
{
120116
"cell_type": "markdown",
121117
"metadata": {},
122118
"source": [
123-
"And create the grid, attaching our subdomains:"
119+
"We create the grid and set up our subdomains:"
124120
]
125121
},
126122
{
@@ -131,8 +127,14 @@
131127
"source": [
132128
"from devito import Grid\n",
133129
"\n",
134-
"grid = Grid(shape=shape, extent=extent,\n",
135-
" subdomains=(main_domain, left, right, base, freesurface))\n",
130+
"grid = Grid(shape=shape, extent=extent)\n",
131+
"\n",
132+
"main = MainDomain(nbpml, so, grid=grid)\n",
133+
"left = Left(nbpml, grid=grid)\n",
134+
"right = Right(nbpml, grid=grid)\n",
135+
"base = Base(nbpml, grid=grid)\n",
136+
"freesurface = FreeSurface(nbpml, so, grid=grid)\n",
137+
"\n",
136138
"x, y = grid.dimensions"
137139
]
138140
},
@@ -170,9 +172,9 @@
170172
"from devito import TimeFunction, VectorTimeFunction, NODE\n",
171173
"\n",
172174
"p = TimeFunction(name='p', grid=grid, time_order=1,\n",
173-
" space_order=s_o, staggered=NODE)\n",
175+
" space_order=so, staggered=NODE)\n",
174176
"v = VectorTimeFunction(name='v', grid=grid, time_order=1,\n",
175-
" space_order=s_o)"
177+
" space_order=so)"
176178
]
177179
},
178180
{
@@ -323,10 +325,10 @@
323325
"from devito import Eq, grad, div\n",
324326
"\n",
325327
"eq_v = Eq(v.forward, v - dt*grad(p)/density,\n",
326-
" subdomain=grid.subdomains['main'])\n",
328+
" subdomain=main)\n",
327329
"\n",
328330
"eq_p = Eq(p.forward, p - dt*velocity**2*density*div(v.forward),\n",
329-
" subdomain=grid.subdomains['main'])"
331+
" subdomain=main)"
330332
]
331333
},
332334
{
@@ -361,35 +363,35 @@
361363
"# Left side\n",
362364
"eq_v_damp_left = Eq(v.forward,\n",
363365
" (1-d_l)*v - dt*grad(p)/density,\n",
364-
" subdomain=grid.subdomains['left'])\n",
366+
" subdomain=left)\n",
365367
"\n",
366368
"eq_p_damp_left = Eq(p.forward,\n",
367369
" (1-gamma*velocity**2*dt-d_l*dt)*p\n",
368370
" - d_l*gamma*velocity**2*p_i\n",
369371
" - dt*velocity**2*density*div(v.forward),\n",
370-
" subdomain=grid.subdomains['left'])\n",
372+
" subdomain=left)\n",
371373
"\n",
372374
"# Right side\n",
373375
"eq_v_damp_right = Eq(v.forward,\n",
374376
" (1-d_r)*v - dt*grad(p)/density,\n",
375-
" subdomain=grid.subdomains['right'])\n",
377+
" subdomain=right)\n",
376378
"\n",
377379
"eq_p_damp_right = Eq(p.forward,\n",
378380
" (1-gamma*velocity**2*dt-d_r*dt)*p\n",
379381
" - d_r*gamma*velocity**2*p_i\n",
380382
" - dt*velocity**2*density*div(v.forward),\n",
381-
" subdomain=grid.subdomains['right'])\n",
383+
" subdomain=right)\n",
382384
"\n",
383385
"# Base edge\n",
384386
"eq_v_damp_base = Eq(v.forward,\n",
385387
" (1-d_b)*v - dt*grad(p)/density,\n",
386-
" subdomain=grid.subdomains['base'])\n",
388+
" subdomain=base)\n",
387389
"\n",
388390
"eq_p_damp_base = Eq(p.forward,\n",
389391
" (1-gamma*velocity**2*dt-d_b*dt)*p\n",
390392
" - d_b*gamma*velocity**2*p_i\n",
391393
" - dt*velocity**2*density*div(v.forward),\n",
392-
" subdomain=grid.subdomains['base'])"
394+
" subdomain=base)"
393395
]
394396
},
395397
{
@@ -456,8 +458,8 @@
456458
" mapper.update({f: f.subs({yind: INT(abs(yind))})})\n",
457459
" return Eq(lhs, rhs.subs(mapper), subdomain=subdomain)\n",
458460
" \n",
459-
"fs_p = free_surface_top(eq_p, grid.subdomains['freesurface'], 'pressure')\n",
460-
"fs_v = free_surface_top(eq_v, grid.subdomains['freesurface'], 'velocity')"
461+
"fs_p = free_surface_top(eq_p, freesurface, 'pressure')\n",
462+
"fs_v = free_surface_top(eq_v, freesurface, 'velocity')"
461463
]
462464
},
463465
{
@@ -616,7 +618,7 @@
616618
"name": "python",
617619
"nbconvert_exporter": "python",
618620
"pygments_lexer": "ipython3",
619-
"version": "3.9.16"
621+
"version": "3.11.5"
620622
}
621623
},
622624
"nbformat": 4,

0 commit comments

Comments
 (0)