Skip to content

Gradients

Define the gradient flows that the systems of several fields share.

A state moves down the gradient of a potential, as a particle in a force field, a field that orders, or a crowd that avoids crowding.

GradientInteraction(label, *, target, states='x', potential=None, sources=(), variational=False, conserved=False, inertia=False)

Bases: Interaction

An interaction that moves states down the gradient of a potential.

Every state x that the interaction moves follows the gradient flow x' = -dU/dx, where U is the potential. A vector state gets the gradient of every component. With inertia the force moves the second derivative instead, Newton's law RateLaw(x.diff(t, 2), -dU/dx / m), divided by the mass m of the member when it has one.

On a field, the potential is the density f of a functional F, the integral of f over space, and may read the field, its derivatives, and its Laplacian. With variational the field moves by the functional derivative of F, as in the Allen-Cahn equation. With conserved the rate is the Laplacian of that derivative, as in the Cahn-Hilliard equation, so the total of the field stays constant. The potential function takes a namespace with the roles, the sources, the time t, and the parameters a subclass declares.

Unlike a RateLaw, which takes the rate itself, it takes a potential and derives the rates from it. Choose it when the dynamics descend an energy or a cost, so that one scalar function gives every rate.

It appears in many fields. Particles move in a potential well, phases separate in an alloy, a neural network descends its loss, and cells sort by their adhesion energy.

Parameters:

Name Type Description Default
label str

The name of the interaction, unique among the components of its system, such as 'descent'.

required
target Role

The path of the element or group whose states move.

required
states str | tuple[str, ...]

The name of the state that moves, or the names of several states.

'x'
potential Callable[[SimpleNamespace], object] | None

A function of the namespace that returns the potential, or the density of the functional of a field.

None
sources tuple[Role, ...]

The paths of the elements or groups the potential reads but does not move.

()
variational bool

Whether the potential is the density of a functional of a field, moved by its functional derivative.

False
conserved bool

Whether the rate is the Laplacian of the derivative of the potential, which conserves the total of a field.

False
inertia bool

Whether the force moves the second derivative of every state, divided by the mass m of the member when it has one.

False

Raises:

Type Description
ValidationError

A state is not a name, or the potential is not callable.

Examples:

>>> from skcomplex.core.components.elements import Element
>>> from skcomplex.core.systems import System
>>> from skcomplex.systems.general.gradients import GradientInteraction
>>> well = GradientInteraction('well', target='walker', states='x', potential=lambda s: s.target.x**2 / 2)
>>> system = System(elements=[Element('walker', values={'x': 1.0})], interactions=[well])
>>> trajectory = system.prepare().simulate(until=1.0, dt=0.01)
>>> round(float(trajectory.get_values('walker.x')[-1]), 4)
0.3679
Source code in src/skcomplex/systems/general/gradients/_gradient.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def __init__(
    self,
    label: str,
    *,
    target: Role,
    states: str | tuple[str, ...] = 'x',
    potential: Callable[[SimpleNamespace], object] | None = None,
    sources: tuple[Role, ...] = (),
    variational: bool = False,
    conserved: bool = False,
    inertia: bool = False,
) -> None:
    super().__init__(label)
    self.target = target
    self.states = states
    self.potential = potential
    self.sources = sources
    self.variational = variational
    self.conserved = conserved
    self.inertia = inertia