Skip to content

Commit

Permalink
Renamed method for Jacobian to stress spectral representation
Browse files Browse the repository at this point in the history
  • Loading branch information
david-zwicker committed Mar 2, 2024
1 parent 9c15a0d commit ca5c148
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
10 changes: 5 additions & 5 deletions pde/pdes/pde.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
r"³": r"**3",
}

# Define how common operators map to fourier space
# Define how common operators map to Fourier space
_OPERATOR_FOURIER_MAPPING = {
"laplace": "-wave_vector**2 * argument",
"gradient": "I * wave_vector * argument",
"divergence": "I * wave_vector * argument",
"gradient_squared": "wave_vector**2 * argument**2",
# "gradient_squared": "wave_vector**2 * argument**2", # or "0"? <- CHECK
}


Expand Down Expand Up @@ -617,15 +617,15 @@ def _make_pde_rhs_numba( # type: ignore
else:
raise TypeError(f"Unsupported field {state.__class__.__name__}")

def _jacobian_expression(
def _jacobian_spectral(
self,
state_hom: numbers.Number | list | dict[str, float] | None = None,
*,
t: float = 0,
wave_vector: str | sympy.Symbol = "q",
check_steady_state: bool = True,
) -> sympy.Matrix:
"""calculate the sympy expression for the fourier-transformed Jacobian
"""calculate the Jacobian in spectral representation
Note:
This method currently only supports scalar fields, so that inner and outer
Expand Down Expand Up @@ -741,7 +741,7 @@ def _dispersion_relation(

if qs is None:
qs = np.linspace(0, 1)

Check warning on line 743 in pde/pdes/pde.py

View check run for this annotation

Codecov / codecov/patch

pde/pdes/pde.py#L743

Added line #L743 was not covered by tests
jac = self._jacobian_expression(state_hom, t=t, wave_vector="wave_vector")
jac = self._jacobian_spectral(state_hom, t=t, wave_vector="wave_vector")
evs_list = []
for q in qs:
jacN = sympy.matrix2numpy(jac.subs("wave_vector", q), dtype=complex)
Expand Down
34 changes: 17 additions & 17 deletions tests/pdes/test_pde_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,48 +393,48 @@ def test_pde_heaviside(backend):
np.testing.assert_allclose(res.data, np.array([-1.0, 2]))


def test_jacobian():
"""test jacobian method"""
def test_jacobian_spectral():
"""test jacobian_spectral method"""
eq = PDE({"c": "laplace(c**3 - c - laplace(c))"})
expected = "q**2*(-3*c**2 - q**2 + 1)"
expr = eq._jacobian_expression()[0]
expr = eq._jacobian_spectral()[0]
assert expr.expand() == sympy.parse_expr(expected).expand()
expr = eq._jacobian_expression(state_hom=1)
expr = eq._jacobian_spectral(state_hom=1)
np.testing.assert_equal(sympy.matrix2numpy(expr.subs("q", 1)), np.array([[-3]]))

eq = PDE({"c": "divergence(c * gradient(c**3 - c - laplace(c)))"})
expected = "2*c*q**2*(-2*c**2 - q**2 + 1)"
expr = eq._jacobian_expression()[0, 0]
expr = eq._jacobian_spectral()[0, 0]
assert expr.expand() == sympy.parse_expr(expected).expand()
expr = eq._jacobian_expression(state_hom=1)
expr = eq._jacobian_spectral(state_hom=1)
np.testing.assert_equal(sympy.matrix2numpy(expr.subs("q", 1)), np.array([[-4]]))

eq = PDE({"a": "laplace(a) + (b - 1)**2", "b": "laplace(a + b**2)"})
jac = eq._jacobian_expression(state_hom=[0, 1])
jac = eq._jacobian_spectral(state_hom=[0, 1])
assert jac[0, 0].expand() == sympy.parse_expr("-q**2").expand()
assert jac[0, 1].expand() == sympy.parse_expr("0").expand()
assert jac[1, 0].expand() == sympy.parse_expr("-q**2").expand()
assert jac[1, 1].expand() == sympy.parse_expr("-2*q**2").expand()


def test_jacobian_bad_input():
"""test jacobian method with bad input"""
def test_jacobian_spectral_bad_input():
"""test jacobian_spectral method with bad input"""
with pytest.raises(ValueError):
PDE({"a": "a"})._jacobian_expression(wave_vector="t")
PDE({"a": "a"})._jacobian_spectral(wave_vector="t")
with pytest.raises(ValueError):
PDE({"a": "a"})._jacobian_expression(wave_vector="a")
PDE({"a": "a"})._jacobian_spectral(wave_vector="a")
with pytest.raises(TypeError):
PDE({"a": "a"})._jacobian_expression(state_hom="1")
PDE({"a": "a"})._jacobian_spectral(state_hom="1")
with pytest.raises(TypeError):
PDE({"a": "a"})._jacobian_expression(state_hom=[np.arange(3)])
PDE({"a": "a"})._jacobian_spectral(state_hom=[np.arange(3)])
with pytest.raises(ValueError):
PDE({"a": "a"})._jacobian_expression(state_hom=np.arange(2))
PDE({"a": "a"})._jacobian_spectral(state_hom=np.arange(2))
with pytest.raises(RuntimeError):
PDE({"a": "a"})._jacobian_expression(state_hom=0.1)
PDE({"a": "a"})._jacobian_spectral(state_hom=0.1)
with pytest.raises(TypeError):
PDE({"a": "inner(a, a)"})._jacobian_expression(state_hom=0)
PDE({"a": "inner(a, a)"})._jacobian_spectral(state_hom=0)
with pytest.raises(TypeError):
PDE({"a": "b"}, consts={"b": 1})._jacobian_expression(state_hom=0)
PDE({"a": "b"}, consts={"b": 1})._jacobian_spectral(state_hom=0)


def test_dispersion_relationn():
Expand Down

0 comments on commit ca5c148

Please sign in to comment.