move stuff under fdmath

This commit is contained in:
Jan Petykiewicz 2019-11-27 22:59:52 -08:00
commit a956323b94
23 changed files with 304 additions and 272 deletions

View file

@ -4,27 +4,33 @@ Basic FDTD field updates
from typing import List, Callable, Tuple, Dict
import numpy
from .. import dx_lists_t, field_t, field_updater
from ..fdmath import dx_lists_t, fdfield_t, fdfield_updater_t
from ..fdmath.functional import curl_forward, curl_back
__author__ = 'Jan Petykiewicz'
def maxwell_e(dt: float, dxes: dx_lists_t = None) -> field_updater:
curl_h_fun = curl_back(dxes[1])
def maxwell_e(dt: float, dxes: dx_lists_t = None) -> fdfield_updater_t:
if dxes is not None:
curl_h_fun = curl_back(dxes[1])
else:
curl_h_fun = curl_back()
def me_fun(e: field_t, h: field_t, epsilon: field_t):
def me_fun(e: fdfield_t, h: fdfield_t, epsilon: fdfield_t):
e += dt * curl_h_fun(h) / epsilon
return e
return me_fun
def maxwell_h(dt: float, dxes: dx_lists_t = None) -> field_updater:
curl_e_fun = curl_forward(dxes[0])
def maxwell_h(dt: float, dxes: dx_lists_t = None) -> fdfield_updater_t:
if dxes is not None:
curl_e_fun = curl_forward(dxes[0])
else:
curl_e_fun = curl_forward()
def mh_fun(e: field_t, h: field_t):
def mh_fun(e: fdfield_t, h: fdfield_t):
h -= dt * curl_e_fun(e)
return h

View file

@ -5,12 +5,12 @@ Boundary conditions
from typing import List, Callable, Tuple, Dict
import numpy
from .. import dx_lists_t, field_t, field_updater
from ..fdmath import dx_lists_t, fdfield_t, fdfield_updater_t
def conducting_boundary(direction: int,
polarity: int
) -> Tuple[field_updater, field_updater]:
) -> Tuple[fdfield_updater_t, fdfield_updater_t]:
dirs = [0, 1, 2]
if direction not in dirs:
raise Exception('Invalid direction: {}'.format(direction))
@ -23,13 +23,13 @@ def conducting_boundary(direction: int,
boundary_slice[direction] = 0
shifted1_slice[direction] = 1
def en(e: field_t):
def en(e: fdfield_t):
e[direction][boundary_slice] = 0
e[u][boundary_slice] = e[u][shifted1_slice]
e[v][boundary_slice] = e[v][shifted1_slice]
return e
def hn(h: field_t):
def hn(h: fdfield_t):
h[direction][boundary_slice] = h[direction][shifted1_slice]
h[u][boundary_slice] = 0
h[v][boundary_slice] = 0
@ -45,14 +45,14 @@ def conducting_boundary(direction: int,
shifted1_slice[direction] = -2
shifted2_slice[direction] = -3
def ep(e: field_t):
def ep(e: fdfield_t):
e[direction][boundary_slice] = -e[direction][shifted2_slice]
e[direction][shifted1_slice] = 0
e[u][boundary_slice] = e[u][shifted1_slice]
e[v][boundary_slice] = e[v][shifted1_slice]
return e
def hp(h: field_t):
def hp(h: fdfield_t):
h[direction][boundary_slice] = h[direction][shifted1_slice]
h[u][boundary_slice] = -h[u][shifted2_slice]
h[u][shifted1_slice] = 0

View file

@ -2,12 +2,13 @@
from typing import List, Callable, Tuple, Dict
import numpy
from .. import dx_lists_t, field_t, field_updater, fdmath
from ..fdmath import dx_lists_t, fdfield_t, fdfield_updater_t
from ..fdmath.functional import deriv_back, deriv_forward
def poynting(e: field_t,
h: field_t,
def poynting(e: fdfield_t,
h: fdfield_t,
dxes: dx_lists_t = None,
) -> field_t:
) -> fdfield_t:
if dxes is None:
dxes = tuple(tuple(numpy.ones(1) for _ in range(3)) for _ in range(2))
@ -25,51 +26,51 @@ def poynting(e: field_t,
return s
def poynting_divergence(s: field_t = None,
def poynting_divergence(s: fdfield_t = None,
*,
e: field_t = None,
h: field_t = None,
e: fdfield_t = None,
h: fdfield_t = None,
dxes: dx_lists_t = None,
) -> field_t:
) -> fdfield_t:
if s is None:
s = poynting(e, h, dxes=dxes)
Dx, Dy, Dz = fdmath.functional.deriv_back()
Dx, Dy, Dz = deriv_back()
ds = Dx(s[0]) + Dy(s[1]) + Dz(s[2])
return ds
def energy_hstep(e0: field_t,
h1: field_t,
e2: field_t,
epsilon: field_t = None,
mu: field_t = None,
def energy_hstep(e0: fdfield_t,
h1: fdfield_t,
e2: fdfield_t,
epsilon: fdfield_t = None,
mu: fdfield_t = None,
dxes: dx_lists_t = None,
) -> field_t:
) -> fdfield_t:
u = dxmul(e0 * e2, h1 * h1, epsilon, mu, dxes)
return u
def energy_estep(h0: field_t,
e1: field_t,
h2: field_t,
epsilon: field_t = None,
mu: field_t = None,
def energy_estep(h0: fdfield_t,
e1: fdfield_t,
h2: fdfield_t,
epsilon: fdfield_t = None,
mu: fdfield_t = None,
dxes: dx_lists_t = None,
) -> field_t:
) -> fdfield_t:
u = dxmul(e1 * e1, h0 * h2, epsilon, mu, dxes)
return u
def delta_energy_h2e(dt: float,
e0: field_t,
h1: field_t,
e2: field_t,
h3: field_t,
epsilon: field_t = None,
mu: field_t = None,
e0: fdfield_t,
h1: fdfield_t,
e2: fdfield_t,
h3: fdfield_t,
epsilon: fdfield_t = None,
mu: fdfield_t = None,
dxes: dx_lists_t = None,
) -> field_t:
) -> fdfield_t:
"""
This is just from (e2 * e2 + h3 * h1) - (h1 * h1 + e0 * e2)
"""
@ -80,14 +81,14 @@ def delta_energy_h2e(dt: float,
def delta_energy_e2h(dt: float,
h0: field_t,
e1: field_t,
h2: field_t,
e3: field_t,
epsilon: field_t = None,
mu: field_t = None,
h0: fdfield_t,
e1: fdfield_t,
h2: fdfield_t,
e3: fdfield_t,
epsilon: fdfield_t = None,
mu: fdfield_t = None,
dxes: dx_lists_t = None,
) -> field_t:
) -> fdfield_t:
"""
This is just from (h2 * h2 + e3 * e1) - (e1 * e1 + h0 * h2)
"""
@ -97,7 +98,7 @@ def delta_energy_e2h(dt: float,
return du
def delta_energy_j(j0: field_t, e1: field_t, dxes: dx_lists_t = None) -> field_t:
def delta_energy_j(j0: fdfield_t, e1: fdfield_t, dxes: dx_lists_t = None) -> fdfield_t:
if dxes is None:
dxes = tuple(tuple(numpy.ones(1) for _ in range(3)) for _ in range(2))
@ -108,12 +109,12 @@ def delta_energy_j(j0: field_t, e1: field_t, dxes: dx_lists_t = None) -> field_t
return du
def dxmul(ee: field_t,
hh: field_t,
epsilon: field_t = None,
mu: field_t = None,
def dxmul(ee: fdfield_t,
hh: fdfield_t,
epsilon: fdfield_t = None,
mu: fdfield_t = None,
dxes: dx_lists_t = None
) -> field_t:
) -> fdfield_t:
if epsilon is None:
epsilon = 1
if mu is None:

View file

@ -7,7 +7,7 @@ PML implementations
from typing import List, Callable, Tuple, Dict
import numpy
from .. import dx_lists_t, field_t, field_updater
from ..fdmath import dx_lists_t, fdfield_t, fdfield_updater_t
__author__ = 'Jan Petykiewicz'
@ -16,7 +16,7 @@ __author__ = 'Jan Petykiewicz'
def cpml(direction: int,
polarity: int,
dt: float,
epsilon: field_t,
epsilon: fdfield_t,
thickness: int = 8,
ln_R_per_layer: float = -1.6,
epsilon_eff: float = 1,
@ -25,7 +25,7 @@ def cpml(direction: int,
ma: float = 1,
cfs_alpha: float = 0,
dtype: numpy.dtype = numpy.float32,
) -> Tuple[Callable, Callable, Dict[str, field_t]]:
) -> Tuple[Callable, Callable, Dict[str, fdfield_t]]:
if direction not in range(3):
raise Exception('Invalid direction: {}'.format(direction))
@ -58,6 +58,7 @@ def cpml(direction: int,
expand_slice = [None] * 3
expand_slice[direction] = slice(None)
expand_slice = tuple(expand_slice)
def par(x):
scaling = (x / thickness) ** m
@ -79,6 +80,7 @@ def cpml(direction: int,
region[direction] = slice(-thickness, None)
else:
raise Exception('Bad polarity!')
region = tuple(region)
se = 1 if direction == 1 else -1
@ -97,7 +99,7 @@ def cpml(direction: int,
# Note that this is kinda slow -- would be faster to reuse dHv*p2h for the original
# H update, but then you have multiple arrays and a monolithic (field + pml) update operation
def pml_e(e: field_t, h: field_t, epsilon: field_t) -> Tuple[field_t, field_t]:
def pml_e(e: fdfield_t, h: fdfield_t, epsilon: fdfield_t) -> Tuple[fdfield_t, fdfield_t]:
dHv = h[v][region] - numpy.roll(h[v], 1, axis=direction)[region]
dHu = h[u][region] - numpy.roll(h[u], 1, axis=direction)[region]
psi_e[0] *= p0e
@ -108,7 +110,7 @@ def cpml(direction: int,
e[v][region] -= se * dt / epsilon[v][region] * (psi_e[1] + (p2e - 1) * dHu)
return e, h
def pml_h(e: field_t, h: field_t) -> Tuple[field_t, field_t]:
def pml_h(e: fdfield_t, h: fdfield_t) -> Tuple[fdfield_t, fdfield_t]:
dEv = (numpy.roll(e[v], -1, axis=direction)[region] - e[v][region])
dEu = (numpy.roll(e[u], -1, axis=direction)[region] - e[u][region])
psi_h[0] *= p0h