Skip to content

Commit 601d99d

Browse files
committed
api: fix norm with complex numbers
1 parent 6a563a2 commit 601d99d

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

devito/builtins/arithmetic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ def norm(f, order=2):
3232
s = dv.types.Symbol(name='sum', dtype=n.dtype)
3333

3434
op = dv.Operator([dv.Eq(s, 0.0)] + eqns +
35-
[dv.Inc(s, dv.Abs(Pow(p, order))), dv.Eq(n[0], s)],
35+
[dv.Inc(s, Pow(dv.Abs(p), order)), dv.Eq(n[0], s)],
3636
name='norm%d' % order)
3737
op.apply(**kwargs)
3838

3939
v = np.power(n.data[0], 1/order)
4040

41-
return f.dtype(v)
41+
return np.real(f.dtype(v))
4242

4343

4444
@dv.switchconfig(log_level='ERROR')

devito/builtins/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
# NOTE: np.float128 isn't really a thing, see for example
2424
# https://github.com/numpy/numpy/issues/10288
2525
# https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#1070
26-
np.float64: np.float64
26+
np.float64: np.float64,
27+
# ComplexX accumulates on Complex2X
28+
np.complex64: np.complex128,
29+
np.complex128: np.complex128,
2730
}
2831

2932

devito/types/basic.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,6 @@ class AbstractFunction(sympy.Function, Basic, Pickable, Evaluable):
859859
is_AbstractFunction = True
860860

861861
# SymPy default assumptions
862-
is_real = True
863862
is_imaginary = False
864863
is_commutative = True
865864

@@ -1316,6 +1315,10 @@ def is_const(self):
13161315
def is_transient(self):
13171316
return self._is_transient
13181317

1318+
@property
1319+
def is_real(self):
1320+
return not np.iscomplex(self.dtype(0))
1321+
13191322
@property
13201323
def is_persistent(self):
13211324
"""

examples/seismic/tutorials/17_fourier_mode.ipynb

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
"outputs": [],
2121
"source": [
2222
"from devito import *\n",
23+
"\n",
2324
"from examples.seismic import demo_model, AcquisitionGeometry, plot_velocity\n",
2425
"from examples.seismic.acoustic import AcousticWaveSolver\n",
25-
"import numpy as np\n",
26+
"\n",
2627
"import matplotlib.pyplot as plt"
2728
]
2829
},
@@ -42,6 +43,7 @@
4243
}
4344
],
4445
"source": [
46+
"#NBVAL_IGNORE_OUTPUT\n",
4547
"model = demo_model('layers-isotropic', vp=3.0, origin=(0., 0.), shape=(101, 101), spacing=(10., 10.), nbl=40, nlayers=4)"
4648
]
4749
},
@@ -62,6 +64,7 @@
6264
}
6365
],
6466
"source": [
67+
"#NBVAL_IGNORE_OUTPUT\n",
6568
"plot_velocity(model)"
6669
]
6770
},
@@ -114,13 +117,6 @@
114117
"execution_count": 5,
115118
"metadata": {},
116119
"outputs": [
117-
{
118-
"name": "stdout",
119-
"output_type": "stream",
120-
"text": [
121-
"damp(x, y)*Derivative(u(t, x, y), t) - Derivative(u(t, x, y), (x, 2)) - Derivative(u(t, x, y), (y, 2)) + Derivative(u(t, x, y), (t, 2))/vp(x, y)**2\n"
122-
]
123-
},
124120
{
125121
"data": {
126122
"text/latex": [
@@ -147,8 +143,6 @@
147143
"# We can now write the PDE\n",
148144
"pde = model.m * u.dt2 - u.laplace + model.damp * u.dt\n",
149145
"\n",
150-
"# The PDE representation is as on paper\n",
151-
"print(pde)\n",
152146
"\n",
153147
"# Stencil update\n",
154148
"stencil = Eq(u.forward, solve(pde, u.forward))\n",
@@ -209,11 +203,11 @@
209203
"data": {
210204
"text/plain": [
211205
"PerformanceSummary([(PerfKey(name='section0', rank=None),\n",
212-
" PerfEntry(time=0.016828, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),\n",
206+
" PerfEntry(time=0.01551399999999999, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),\n",
213207
" (PerfKey(name='section1', rank=None),\n",
214-
" PerfEntry(time=0.002213999999999991, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),\n",
208+
" PerfEntry(time=0.0023819999999999913, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),\n",
215209
" (PerfKey(name='section2', rank=None),\n",
216-
" PerfEntry(time=0.0021949999999999943, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])"
210+
" PerfEntry(time=0.002333999999999994, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])"
217211
]
218212
},
219213
"execution_count": 9,
@@ -222,6 +216,7 @@
222216
}
223217
],
224218
"source": [
219+
"#NBVAL_IGNORE_OUTPUT\n",
225220
"op(dt=model.critical_dt)"
226221
]
227222
},
@@ -242,6 +237,7 @@
242237
}
243238
],
244239
"source": [
240+
"#NBVAL_IGNORE_OUTPUT\n",
245241
"plt.figure(figsize=(12, 6))\n",
246242
"plt.subplot(1, 2, 1)\n",
247243
"plt.imshow(np.real(freq_mode.data.T), cmap='seismic', vmin=-1e2, vmax=1e2)\n",
@@ -251,6 +247,16 @@
251247
"plt.colorbar()\n",
252248
"plt.show()"
253249
]
250+
},
251+
{
252+
"cell_type": "code",
253+
"execution_count": 11,
254+
"metadata": {},
255+
"outputs": [],
256+
"source": [
257+
"assert np.isclose(norm(freq_mode), 13873.049, atol=0, rtol=1e-4)\n",
258+
"assert np.isclose(norm(u), 323.74207, atol=0, rtol=1e-4)"
259+
]
254260
}
255261
],
256262
"metadata": {

0 commit comments

Comments
 (0)