Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Nov 25, 2020
1 parent edc41c5 commit c6fb9c6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 17 deletions.
20 changes: 19 additions & 1 deletion src/sage/manifolds/differentiable/poisson_tensor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from sage.manifolds.differentiable.tensorfield_paral import TensorFieldParal
from sage.manifolds.differentiable.diff_form import DiffForm
from sage.manifolds.differentiable.vectorfield import VectorField
from sage.manifolds.differentiable.scalarfield import DiffScalarField
Expand Down Expand Up @@ -89,6 +90,23 @@ def poisson_bracket(self, f: DiffScalarField, g: DiffScalarField) -> DiffScalarF
[X_f, X_g] = X_{{f,g}}
"""
poisson_bracket = self.contract(0, self.hamiltonian_vector_field(f)).contract(0, self.hamiltonian_vector_field(g))
poisson_bracket = self.contract(0, f.exterior_derivative()).contract(0, g.exterior_derivative())
poisson_bracket.set_name(f"poisson({f._name}, {g._name})", '\\{' + f'{f._latex_name}, {g._latex_name}' + '\\}')
return poisson_bracket


class PoissonTensorFieldParal(PoissonTensorField, TensorFieldParal):

def __init__(self, manifold: Union[DifferentiableManifold, VectorFieldModule], name: Optional[str] = None, latex_name: Optional[str] = None):
try:
vector_field_module = manifold.vector_field_module()
except AttributeError:
vector_field_module = manifold

if name is None:
name = "varpi"
if latex_name is None:
latex_name = "\\varpi"

TensorFieldParal.__init__(self, vector_field_module, (2, 0), name=name, latex_name=latex_name, antisym=(0, 1))

4 changes: 2 additions & 2 deletions src/sage/manifolds/differentiable/symplectic_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def poisson(self, expansion_symbol=None, order=1) -> PoissonTensorField:
# Initialize the Poisson tensor
poisson_name = f'poisson_{self._name}'
poisson_latex_name = f'{self._latex_name}^{{-1}}'
self._poisson = PoissonTensorField(self._vmodule, poisson_name, poisson_latex_name)
self._poisson = self._vmodule.poisson_tensor(poisson_name, poisson_latex_name)

# Update the Poisson tensor
# TODO: Should this be done instead when a new restriction is added?
Expand Down Expand Up @@ -805,7 +805,7 @@ def restrict(self, subdomain: DifferentiableManifold, dest_map: Optional[DiffMap


@classmethod
def wrap(cls, form:DiffFormParal, name: Optional[str] = None, latex_name: Optional[str] = None) -> 'SymplecticFormParal':
def wrap(cls, form: DiffFormParal, name: Optional[str] = None, latex_name: Optional[str] = None) -> 'SymplecticFormParal':
r"""
Define the metric from a field of symmetric bilinear forms.
Expand Down
33 changes: 19 additions & 14 deletions src/sage/manifolds/differentiable/symplectic_form_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sage.manifolds.differentiable.symplectic_form import SymplecticForm
from sage.manifolds.differentiable.examples.symplectic_vector_space import SymplecticVectorSpace
from sage.symbolic.function_factory import function
from sage.manifolds.scalarfield import ScalarField


class TestGenericSymplecticForm:
Expand Down Expand Up @@ -49,8 +50,8 @@ def M(self, request: FixtureRequest):
elif request.param == "S2":
# TODO: Return sphere here instead
# Problem: we use cartesian_coordinates below
# return Sphere(2)
return SymplecticVectorSpace(4, 'R4', symplectic_name='omega')
return Sphere(2)
#return SymplecticVectorSpace(4, 'R4', symplectic_name='omega')

@pytest.fixture()
def omega(self, M):
Expand All @@ -60,23 +61,27 @@ def omega(self, M):
return SymplecticForm.wrap(M.metric().volume_form(), 'omega')

def test_flat_of_hamiltonian_vector_field(self, M: DifferentiableManifold, omega: SymplecticForm):
q, p = M.cartesian_coordinates()[:]
H = M.scalar_field(function('H')(q, p), name='H')
H = generic_scalar_field(M, 'H')
assert omega.flat(omega.hamiltonian_vector_field(H)) == - H.differential()

def test_poisson_bracket_as_contraction_symplectic_form(self, M: DifferentiableManifold, omega: SymplecticForm):
q, p = M.cartesian_coordinates()[:]
f = M.scalar_field(function('f')(q,p), name='f')
g = M.scalar_field(function('g')(q,p), name='g')
f = generic_scalar_field(M, 'f')
g = generic_scalar_field(M, 'g')
assert omega.poisson_bracket(f, g) == omega.contract(0, omega.hamiltonian_vector_field(f)).contract(0, omega.hamiltonian_vector_field(g))

def test_poisson_bracket_as_contraction_poisson_tensor(self, M: DifferentiableManifold, omega: SymplecticForm):
q, p = M.cartesian_coordinates()[:]
f = M.scalar_field(function('f')(q,p), name='f')
g = M.scalar_field(function('g')(q,p), name='g')
f = generic_scalar_field(M, 'f')
g = generic_scalar_field(M, 'g')
assert omega.poisson_bracket(f, g) == omega.poisson().contract(0, f.exterior_derivative()).contract(0, g.exterior_derivative())


def generic_scalar_field(M: DifferentiableManifold, name: str) -> ScalarField:
chart_functions = {
chart: function(name)(*chart[:]) for chart in M.atlas()
}
return M.scalar_field(chart_functions, name=name)


class TestR2VectorSpace:

@pytest.fixture
Expand All @@ -91,13 +96,13 @@ def test_display(self, omega: SymplecticForm):
assert str(omega.display()) == r'omega = -dq/\dp'

def test_poisson(self, omega: SymplecticForm):
assert str(omega.poisson().display()) == r'ee = '
# TODO: Shouldn't this be written with wedge product?
assert str(omega.poisson().display()) == r'poisson_omega = -e_q*e_p + e_p*e_q'

def test_hamiltonian_vector_field(self, M: SymplecticVectorSpace, omega: SymplecticForm):
q, p = M.cartesian_coordinates()[:]
H = M.scalar_field(function('H')(q, p), name='H')
H = generic_scalar_field(M, 'H')
XH = omega.hamiltonian_vector_field(H)
assert str(XH.display()) == r'XH = '
assert str(XH.display()) == r'XH = d(H)/dp e_q - d(H)/dq e_p'

def test_flat(self, M: SymplecticVectorSpace, omega: SymplecticForm):
X = M.vector_field(1, 2, name='X')
Expand Down
28 changes: 28 additions & 0 deletions src/sage/manifolds/differentiable/vectorfield_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,20 @@ def metric(self, name, signature=None, latex_name=None):
return PseudoRiemannianMetric(self, name, signature=signature[0]-signature[1],
latex_name=latex_name)

def symplectic_form(self, name: Optional[str] = None, latex_name: Optional[str] = None):
r"""
Construct a symplectic form on the current vector field module.
"""
from sage.manifolds.differentiable.symplectic_form import SymplecticForm
return SymplecticForm(self, name, latex_name)

def poisson_tensor(self, name: Optional[str] = None, latex_name: Optional[str] = None):
r"""
Construct a Poisson tensor on the current vector field module.
"""
from sage.manifolds.differentiable.poisson_tensor import PoissonTensorField
return PoissonTensorField(self, name, latex_name)


#******************************************************************************

Expand Down Expand Up @@ -2282,3 +2296,17 @@ def metric(self, name, signature=None, latex_name=None):
return PseudoRiemannianMetricParal(self, name,
signature=signature[0]-signature[1],
latex_name=latex_name)

def symplectic_form(self, name: Optional[str] = None, latex_name: Optional[str] = None):
r"""
Construct a symplectic form on the current vector field module.
"""
from sage.manifolds.differentiable.symplectic_form import SymplecticFormParal
return SymplecticFormParal(self, name, latex_name)

def poisson_tensor(self, name: Optional[str] = None, latex_name: Optional[str] = None):
r"""
Construct a Poisson tensor on the current vector field module.
"""
from sage.manifolds.differentiable.poisson_tensor import PoissonTensorFieldParal
return PoissonTensorFieldParal(self, name, latex_name)

0 comments on commit c6fb9c6

Please sign in to comment.