Metadata-Version: 2.4
Name: pdemagic
Version: 0.1.0
Classifier: Development Status :: 3 - Alpha
Classifier: License :: Other/Proprietary License
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Rust
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Dist: numpy>=1.26
Requires-Dist: sympy>=1.12
Requires-Dist: matplotlib>=3.8 ; extra == 'plot'
Provides-Extra: plot
License-File: LICENSE
License-File: THIRD-PARTY-LICENSES.txt
Summary: Solve partial differential equations from a symbolic problem statement.
License-Expression: LicenseRef-Proprietary
Requires-Python: >=3.11
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Documentation, https://maroba.github.io/pdemagic/

# pdemagic

**State a partial differential equation as you would write it down.
pdemagic chooses the method, the grid and every setting, solves it, and
tells you what it did.**

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://maroba.github.io/pdemagic/_static/readme/antennas-dark.png">
    <img src="https://maroba.github.io/pdemagic/_static/readme/antennas-light.png" width="32%" alt="Waves from two sources that interfere: the Helmholtz equation in free space.">
  </picture>
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://maroba.github.io/pdemagic/_static/readme/light-dark.png">
    <img src="https://maroba.github.io/pdemagic/_static/readme/light-light.png" width="32%" alt="Two pulses of light that spread as rings: Maxwell's equations in vacuum.">
  </picture>
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://maroba.github.io/pdemagic/_static/readme/pattern-dark.png">
    <img src="https://maroba.github.io/pdemagic/_static/readme/pattern-light.png" width="32%" alt="A pattern that grows from a seed: two substances that diffuse and react.">
  </picture>
</p>

<p align="center">
  <em>Waves from two antennas · a flash of light · a pattern from a reaction.<br>
  Each is a few lines of sympy and one call to <code>pm.solve</code>.</em>
</p>

## An example

Two sources send out waves of one frequency. This is the Helmholtz equation
in free space, and the first picture above:

```python
import sympy as sp
import pdemagic as pm

x, y = sp.symbols("x y", real=True)
w = sp.Function("w")(x, y)


def antenna(centre):
    return sp.exp(-8 * ((x - centre) ** 2 + y**2))


problem = pm.Problem(
    sp.Eq(pm.laplacian(w) + 36 * w, -(antenna(-1.5) + antenna(1.5))),
    domain=pm.FreeSpace(x, y),
)
solution = pm.solve(problem)
solution.plot(part="real")
```

There is no box to choose here, no boundary condition to invent, and no
grid to set up. pdemagic did all of that, and says how:

```python
print(solution.plan)
```

```text
method:
  kind: fourier
grid:
  lower: [-5.454052280232192, -3.952980960979054]
  upper: [5.454052280232192, 3.952980960979054]
  shape: [192, 139]
  periodic: [False, False]
notes:
  - The equation is a Helmholtz equation with the wavelength 1.0472, in 2-dimensional free space.
  - Box: chosen so that the source falls to 1e-12 of the maximum at the faces.
  - The box is 2 wavelengths wider than the source on every free side, and the grid has at least 6 points per wavelength, so that the waves around the source can be seen. The solution is the outgoing one, and is complex.
  - Grid: the coarsest candidate on which the spectrum of the source falls below 1e-10 at high frequencies.
```

The plan is data. Change any entry and solve again:

```python
plan = pm.plan(problem)
plan.grid.shape = (384, 278)
finer = pm.solve(problem, plan)
```

## Why

- **Symbolic in, numbers out.** A problem is a set of sympy equations on a
  domain. The solution is a set of numpy arrays, with the grid they live on.
- **Free space is free space.** An unbounded domain is solved as exactly
  that, with no artificial boundary: the potential that vanishes at
  infinity, the wave that leaves and does not come back.
- **Accurate by default.** Most methods are spectral, and linear equations
  in time are propagated exactly. Ten digits on a modest grid are common,
  and every solution reports how far it can be trusted.
- **Nothing is hidden.** Every choice the planner makes is an entry of the
  plan, with a note that says why.
- **A fast core.** The numerics are written in Rust. Python is the first
  frontend, and the core is built to be called from other languages.

## What it solves

| | |
|---|---|
| **Poisson's equation** | free space in any dimension, periodic directions, disks, balls, cylinders, dielectrics, conductors |
| **Screened Poisson and Helmholtz** | free space, with the right condition at infinity for each, also in a medium that bends the waves |
| **Coefficients that vary in space** | a medium, a potential: any linear stationary equation on periodic boxes, and in free space where the coefficients settle |
| **Nonlinear stationary equations** | Poisson–Boltzmann, ground states: Newton's method, from a starting value you can give |
| **Boundary value problems** | boxes in any dimension, disks, rings, balls, spherical shells and cylinders, with values, derivatives or a mix on each face, stationary and in time |
| **Shapes of your own** | a region of the plane bounded by lines and arcs, with holes: any elliptic equation of second order, also in a medium, with drift, or with a nonlinear term, with a condition on each piece of the boundary, and heat, waves and Schrödinger in time |
| **Heat, waves, Schrödinger** | free space, periodic boxes, and between walls: boxes, round domains and shapes of your own. Exact in time |
| **Nonlinear equations in time** | Burgers, Korteweg–de Vries, nonlinear Schrödinger, sine-Gordon, on periodic boxes. Equations of first order in time also between the walls of boxes, round domains and shapes of your own |
| **Systems** | Maxwell's equations in vacuum, reaction–diffusion, a Schrödinger equation with its own potential |

Unknowns may be real or complex. Sources, coefficients, initial states and
boundary values are formulas or arrays, and on a shape of your own
formulas. The [documentation](https://maroba.github.io/pdemagic/) has a
gallery, a guide, and a page on
[what is covered and what is not](https://maroba.github.io/pdemagic/capabilities.html).

## Install

```bash
pip install pdemagic
```

or, with matplotlib for `solution.plot()` and `solution.animate()`:

```bash
pip install "pdemagic[plot]"
```

There are packages for Linux, macOS and Windows, for Python 3.11 and
later. Nothing has to be compiled.

## Status

pdemagic is young: this is version 0.1. What it solves, it solves
carefully: every method is verified against exact solutions, with the order
of convergence asserted, and test coverage is above 95 % in both languages.
The interface may still change from one 0.x version to the next, and the
[changes](https://maroba.github.io/pdemagic/changes.html) say where.

The largest gap is what can be solved on a shape of your own: equations
for one unknown in the plane so far, and nothing in space. The
[roadmap](https://maroba.github.io/pdemagic/dev/roadmap.html) says what
comes next.

## Documentation

- [Getting started](https://maroba.github.io/pdemagic/getting-started.html):
  a first solve, step by step.
- [Gallery](https://maroba.github.io/pdemagic/gallery.html): problems
  with their pictures.
- [Guide](https://maroba.github.io/pdemagic/guide/domains.html): domains,
  boundaries, shapes, data, bodies, equations, time, systems, plans,
  plotting.
- [Methods](https://maroba.github.io/pdemagic/methods/index.html): how each
  problem is solved, and how accurately.

## License

pdemagic is proprietary software. You may install it and use it free of
charge, for any purpose. You may not pass it on, change it, or take it
apart. The source code of its core is not published. The terms are in the file
`LICENSE` that comes with the package, and hold for the versions 0.1.x:
later versions may come under other terms.

