Differential Equations in iTensor

Solve ODEs and PDEs with both symbolic and numerical methods for physics applications

Overview

Differential equations are essential in physics, describing how systems evolve in space and time. iTensor implements both symbolic and numerical solvers specifically optimized for tensor-based equations.

Unlike general-purpose ODE/PDE solvers, iTensor preserves tensor structure throughout the solution process, maintaining covariance and tracking index positions automatically.

The differential equation module connects directly to the symbolic tensor engine, allowing you to derive equations symbolically and then solve them numerically without manual conversion between representations.

ODE Solvers

iTensor implements specialized ODE solvers that maintain tensor properties:

Implemented Methods

  • RK4 - Classic 4th-order Runge-Kutta with fixed step size
  • RK45 - Adaptive step-size control using error estimation
  • ImplicitEuler - For stiff equations with stability guarantees
  • Verlet - Symplectic integrator preserving energy in Hamiltonian systems

Example: Harmonic Oscillator

from itensor.ode import ODESystem # Define the oscillator equation d²x/dt² + k/m*x = 0 system = ODESystem() system.add_equation("d²x/dt² + k/m*x = 0") system.set_parameters(k=1.0, m=1.0) system.set_initial_conditions(x=1.0, dx_dt=0.0) # Solve from t=0 to t=10 with 100 steps solution = system.solve(t_span=[0, 10], method='RK4', steps=100) # Plot the solution solution.plot(components=['x', 'dx_dt'])

Performance Note

For systems with many equations or stiff problems, use the compiled C++ backend by setting use_compiled=Truein the solve method. This can accelerate computation by 10-50x.

Example: Coupled Predator-Prey System

from itensor.ode import ODESystem import numpy as np # Define Lotka-Volterra equations system = ODESystem() system.add_equation("dx/dt = α*x - β*x*y") # Prey system.add_equation("dy/dt = δ*x*y - γ*y") # Predator system.set_parameters(α=1.0, β=0.2, δ=0.1, γ=0.8) system.set_initial_conditions(x=10.0, y=2.0) # Solve with adaptive step size solution = system.solve(t_span=[0, 50], method='RK45', rtol=1e-6) # Plot phase portrait solution.plot_phase(['x', 'y'])

PDE Solvers

iTensor's PDE solvers handle multi-dimensional problems across several types of equations:

Elliptic PDEs

Poisson and Laplace equations for equilibrium problems:

∇²u = f(x,y,z)

Application: Electrostatic potentials, gravitational fields

Parabolic PDEs

Diffusion and heat equations:

∂u/∂t = α∇²u

Application: Heat transfer, diffusion processes

Hyperbolic PDEs

Wave equations describing propagation:

∂²u/∂t² = c²∇²u

Application: Electromagnetic waves, sound propagation

Numerical Methods

  • Finite Difference - Implemented for regular grids with customizable stencils
  • Spectral Methods - For high-accuracy solutions with periodic boundaries
  • Finite Element - For complex geometries (requires mesh definition)

Example: 2D Heat Equation

from itensor.pde import PDESystem, UniformGrid2D import numpy as np # Create a uniform 2D grid grid = UniformGrid2D(nx=50, ny=50, xmin=0, xmax=1, ymin=0, ymax=1) # Define the heat equation PDE system = PDESystem(grid) system.add_equation("∂u/∂t = α*(∂²u/∂x² + ∂²u/∂y²)") system.set_parameters(α=0.01) # Diffusion coefficient # Initial condition: Gaussian peak x, y = grid.get_coordinates() X, Y = np.meshgrid(x, y) u0 = np.exp(-100*((X-0.5)**2 + (Y-0.5)**2)) system.set_initial_condition(u=u0) # Set boundary conditions (fixed at zero) system.set_boundary_conditions('dirichlet', u=0.0) # Solve for 100 time steps solution = system.solve(t_span=[0, 1.0], nt=100, method='crank_nicolson') # Animate the solution solution.animate_field('u')

Tensor Differential Equations

One of iTensor's unique features is solving differential equations where the unknown quantities are tensors, not just scalar fields.

Einstein Field Equations

Solve for the metric tensor components given a stress-energy tensor:

Gμν = 8πG Tμν
from itensor.relativity import EinsteinSolver # Set up a spherically symmetric matter distribution solver = EinsteinSolver() solver.set_stress_energy_tensor(model='perfect_fluid', density=lambda r: 1.0 if r < 2.0 else 0.0, pressure=lambda r: 0.0) # Solve for the metric (uses numerical relaxation) solution = solver.solve(grid_size=100, r_max=10.0) # Examine metric components g_tt = solution.get_metric_component('g_tt') solver.plot_metric_components(['g_tt', 'g_rr'])

Maxwell's Equations in Tensor Form

Solve for the electromagnetic field tensor Fμν:

μFμν = Jν
from itensor.em import MaxwellSolver, PointCharge # Define a system with a moving charge solver = MaxwellSolver(method='FDTD') solver.add_source(PointCharge(position=[0, 0, 0], charge=1.0, velocity=[0.5, 0, 0])) # Solve in the computational domain solution = solver.solve(domain_size=[10, 10, 10], time_steps=100) # Visualize the electromagnetic field solution.plot_field_lines(time_index=50)

Working with Results

Solution objects from iTensor's differential equation solvers provide analysis methods:

  • Data Extraction:
    t_values, u_values = solution.get_data('u')
  • Visualization:
    solution.plot_field('u', time_index=50) # Specific time slice
  • Animation:
    animation = solution.animate(component='u', fps=30)
  • Data Export:
    solution.save('simulation_results.h5') # HDF5 format
  • Derived Quantities:
    energy = solution.compute_total_energy()

Common Applications

Particle Trajectories in Curved Spacetime

Solve the geodesic equation to simulate how particles and light move near black holes.

Accretion Disk Simulation

Model fluid dynamics around compact objects using relativistic hydrodynamics equations.