gala.potential.potential.from_equation(expr, vars, pars, name=None, hessian=False)[source]

Create a potential class from an expression for the potential.


This utility requires having Sympy installed.


These potentials are not pickle-able and cannot be written out to YAML files (using save())

expr : sympy.core.expr.Expr, str

Either a Sympy expression, or a string that can be converted to a Sympy expression.

vars : iterable

An iterable of variable names in the expression.

pars : iterable

An iterable of parameter names in the expression.

name : str (optional)

The name of the potential class returned.

hessian : bool (optional)

Generate a function to compute the Hessian.

CustomPotential : PotentialBase

A potential class that represents the input equation. To instantiate the potential, use just like a normal class with parameters.


Here we’ll create a potential class for the harmonic oscillator potential, \(\Phi(x) = \frac{1}{2}\,k\,x^2\):

>>> Potential = from_equation("1/2*k*x**2", vars="x", pars="k",
...                           name='HarmonicOscillator')
>>> p1 = Potential(k=1.)
>>> p1
<HarmonicOscillatorPotential: k=1.00 (dimensionless)>

The potential class (and object) is a fully-fledged subclass of PotentialBase and therefore has many useful methods. For example, to integrate an orbit:

>>> orbit = p1.integrate_orbit([1.,0], dt=0.01, n_steps=1000)