from_equation¶
- gala.potential.potential.from_equation(expr, vars, pars, name=None, hessian=False)[source]¶
Create a potential class from an expression for the potential.
Note
This utility requires having Sympy installed.
Warning
These potentials are not pickle-able and cannot be written out to YAML files (using
save()
)- Parameters
- expr
sympy.core.expr.Expr
,str
Either a
Sympy
expression, or a string that can be converted to aSympy
expression.- varsiterable
An iterable of variable names in the expression.
- parsiterable
An iterable of parameter names in the expression.
- name
str
(optional) The name of the potential class returned.
- hessianbool (optional)
Generate a function to compute the Hessian.
- expr
- Returns
- CustomPotential
PotentialBase
A potential class that represents the input equation. To instantiate the potential, use just like a normal class with parameters.
- CustomPotential
Examples
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:>>> from gala.potential import Hamiltonian >>> H = Hamiltonian(p1) >>> orbit = H.integrate_orbit([1., 0], dt=0.01, n_steps=1000)