Solutions¶
What pm.solve returns: the values of every unknown on the grid, with
the plan that produced them and the diagnostics of the solve.
To get |
Write |
|---|---|
the values of an unknown on the grid |
|
the points of the grid |
|
the recorded times of a problem in time |
|
the eigenvalues of an eigenproblem |
|
values at any points |
|
the integral over the domain |
|
the plan and the diagnostics |
|
what was found about each body |
|
a picture |
|
a file |
|
In a problem in time the values of an unknown have the recorded times along their first axis, and in an eigenproblem the eigenfunctions, in the order of the eigenvalues.
- class pdemagic.Solution(plan, diagnostics, fields, times=None, problem=None, native=None, eigenvalues=None)¶
The result of a solve.
Index the solution with an unknown to get its values, as in
solution[phi]. The values are a numpy array with one axis per coordinate of the domain, on the grid of the plan. The array of a complex unknown holds complex numbers. On aRegionthe grid covers the rectangle that holds the region, and a point outside the region holdsnan.For a problem that depends on time, the first axis of the array is the time:
solution[u][i]holds the unknown atsolution.times[i].For an eigenproblem the first axis counts the eigenfunctions:
solution[psi][i]is the eigenfunction ofsolution.eigenvalues[i], from the lowest eigenvalue. Each has the integral of its square over the domain equal to one, and its largest value positive.Call the solution with coordinates to get its values at any points, as in
solution(0.3, 0.4): see__call__().- Parameters:
- diagnostics¶
Measurements that tell how far the result can be trusted.
- Type:
dict
- times¶
The times at which the solution is recorded, or
Nonefor a problem that does not depend on time.- Type:
numpy.ndarray or None
- problem¶
The problem that was solved. A solution that was loaded from a file has a
SavedProblem.- Type:
Problem, SavedProblem or None
- native¶
What the method holds of the solution beyond its values on the grid, as the core returns it.
__call__()reads it.- Type:
numpy.ndarray or None
- eigenvalues¶
The eigenvalues of an eigenproblem, from the lowest, or
Nonefor every other problem.- Type:
numpy.ndarray or None
- property unknowns: tuple[str, ...]¶
The names of the unknowns that were solved for.
- __call__(*coordinates, unknown=None)¶
Return the values of the solution at any points of its domain.
The values come from the expansion that the method solved with, not from an interpolation of the grid by its neighbours. They are as accurate as the solution itself:
On a
Region, from the polynomials of the elements, also on the boundary of the region, where the grid has no points.Along a periodic coordinate, from the trigonometric sum through the points of the grid.
Along a coordinate between walls and along a radius, from the polynomial through the points of the grid, also on the walls themselves.
This works on a
Region, on a domain that is periodic in every direction, and on aBox, aDiskand aCylinderwith boundary conditions. It does not work yet with a direction that is free, in aBall, and in a cylinder with vacuum outside.- Parameters:
*coordinates (Any) – The coordinates of the points, one number or one array for each coordinate of the domain, in its order. Arrays are broadcast against each other.
unknown (Basic | str | None) – The unknown to evaluate. It can be left out if the problem has one.
- Returns:
The values, in the shape that the coordinates have together. For a problem that depends on time the first axis is the time, as in
solution[u]. A point outside the domain holdsnan.- Return type:
numpy.ndarray
- Raises:
UnsupportedProblemError – On a domain where values between grid points are not given yet.
InvalidProblemError – If the number of coordinates is not that of the domain, or if the problem has several unknowns and none is named.
Examples
The membrane on the unit disk, at its centre and on its rim:
>>> import sympy as sp >>> import pdemagic as pm >>> x, y = sp.symbols("x y", real=True) >>> u = sp.Function("u")(x, y) >>> disk = pm.Region(x, y, pm.curves.Circle((0, 0), 1)) >>> membrane = pm.Problem( ... sp.Eq(-pm.laplacian(u), 1), domain=disk, boundary=sp.Eq(u, 0) ... ) >>> solution = pm.solve(membrane) >>> round(float(solution(0, 0)), 9) 0.25 >>> abs(float(solution(1, 0))) < 1e-12 True
- integral(unknown=None)¶
Return the integral of an unknown over the domain.
The integral is taken by the quadrature that the method solved with, which is as accurate as the solution: over the points of the grid with their weights, in the coordinates of the domain, so that a disk is integrated over its area and a ball over its volume, and on a
Regionover the elements, up to the boundary.Where a direction is free the integral is over the box of the grid, not over all of space.
- Parameters:
unknown (Basic | str | None) – The unknown to integrate. It can be left out if the problem has one.
- Returns:
The integral. For a problem that depends on time, an array with one integral for each recorded time.
- Return type:
float, complex or numpy.ndarray
- Raises:
InvalidProblemError – If the problem has several unknowns and none is named.
Examples
The membrane on the unit disk,
(1 - r**2) / 4, has the integralpi / 8:>>> import math >>> import sympy as sp >>> import pdemagic as pm >>> x, y = sp.symbols("x y", real=True) >>> u = sp.Function("u")(x, y) >>> disk = pm.Region(x, y, pm.curves.Circle((0, 0), 1)) >>> membrane = pm.Problem( ... sp.Eq(-pm.laplacian(u), 1), domain=disk, boundary=sp.Eq(u, 0) ... ) >>> abs(pm.solve(membrane).integral() - math.pi / 8) < 1e-12 True
- save(path)¶
Write the solution to a file, to be read again with
pdemagic.load().The file holds the values of the unknowns, the recorded times, the plan, the diagnostics, and the problem as the solver read it. It is a numpy archive, which
numpy.loadopens as well: the unknownuis its entry"field/u".The values of data fields that the problem was given are not part of the file, and neither are the sympy expressions of the equations.
- Parameters:
path (str | Path) – Where to write. The file gets the ending
.npzif it has none.- Returns:
The file that was written.
- Return type:
pathlib.Path
- Raises:
ValueError – If the solution does not know its problem.
- to_xarray()¶
Return the solution as an
xarray.Dataset.Each unknown is a variable of the dataset, over the coordinates of the domain by their names, and over the time for a problem that depends on it. The points of the grid and the recorded times are the labels. The plan and the diagnostics are attributes, as JSON.
This needs xarray, which is an optional dependency:
pip install pdemagic[xarray].- Returns:
The values of the solution with their labels.
- Return type:
xarray.Dataset
- Raises:
ImportError – If xarray is not installed.
- to_vtk(path)¶
Write the solution as VTK files, which ParaView and VisIt open.
A stationary solution is one file. A solution in time is one file for each recorded time, numbered, and one file with the ending
.pvdthat lists them with their times: open that one.A solution on Cartesian coordinates is written on its grid, with the ending
.vtr. In a disk, a ball or a cylinder the points are written where they lie in space, with the ending.vts, and the circle around the axis is closed. On aRegionthe points of the grid outside the region holdnan. A complex unknownuis written asu_realandu_imag.- Parameters:
path (str | Path) – Where to write, without an ending: the endings are added.
- Returns:
The file to open: the one with the ending
.pvdfor a solution in time, and the only one otherwise.- Return type:
pathlib.Path
- Raises:
UnsupportedProblemError – For a solution in more than three dimensions.
- property axes: tuple[ndarray, ...]¶
The coordinates of the grid points along each axis.
- property bodies: dict[str, BodyResult]¶
What was found out about each body of the problem, by name.
The charge a conductor carries, and the potential it sits at, are
solution.bodies[name].fluxandsolution.bodies[name].potential.
- mesh()¶
Return the coordinates of every grid point.
Returns one array per coordinate, each of the shape of the solution, so that
solution[phi][i, j, k]is the value at(X[i, j, k], Y[i, j, k], Z[i, j, k]).- Return type:
tuple[ndarray, …]
- plot(unknown=None, *, time=None, state=None, cut=None, part=None, ax=None, theme=None)¶
Draw the solution with matplotlib.
What is drawn follows from the problem:
in one dimension a line, and for a problem in time one line for each of a few times;
in two dimensions the field in colour, and for a problem in time at its last recorded time. On a disk or a ring it is drawn on that, and on a
Regionup to its boundary;in three dimensions a section through the field. In a ball that is the plane through the axis at the angle zero, with both its halves, and in a cylinder a disk across the axis.
A field with both signs is drawn in two hues around zero, any other in one hue. Of a complex unknown the magnitude is drawn, or the part that is asked for.
- Parameters:
unknown (Basic | str | None) – The unknown to draw. Not needed while there is only one.
time (float | Sequence[float] | None) – For a problem in time: the time to draw. The recorded time nearest to it is taken. In one dimension it can be several times.
state (int | Sequence[int] | None) – For an eigenproblem: the number of the eigenfunction to draw, counted from zero for the lowest eigenvalue. In one dimension it can be several, and by default the lowest five are drawn. In two and three dimensions the lowest is drawn by default.
cut (Mapping[Any, float] | None) – In three dimensions: the coordinate that is held fixed and its value, as in
{z: 0.5}. By default the last coordinate, at the middle of the box.part (str | None) – What to draw of the unknown:
"abs"for its magnitude,"real"or"imag". By default the magnitude of a complex unknown, and a real one as it is.ax (Any) – The matplotlib axes to draw into. By default a new figure.
theme (str | None) –
"light"or"dark".
- Returns:
The axes that were drawn into, for further changes.
- Return type:
matplotlib.axes.Axes
- Raises:
ImportError – If matplotlib is not installed:
pip install pdemagic[plot].ValueError – If the part or the theme is not one of those listed.
- animate(unknown=None, *, cut=None, part=None, interval=80, theme=None)¶
Animate a solution that depends on time, with matplotlib.
Of an eigenproblem the animation leafs through the eigenfunctions, from the lowest eigenvalue.
- Parameters:
unknown (Basic | str | None) – The unknown to draw. Not needed while there is only one.
cut (Mapping[Any, float] | None) – In three dimensions: the coordinate that is held fixed and its value. By default the last coordinate, at the middle of the box.
part (str | None) – What to draw of the unknown:
"abs"for its magnitude,"real"or"imag". By default the magnitude of a complex unknown, and a real one as it is.interval (int) – The time between two frames in milliseconds. There is one frame per recorded time.
theme (str | None) –
"light"or"dark".
- Returns:
The animation. Save it with its
savemethod, or show it in a notebook withto_jshtml.- Return type:
matplotlib.animation.FuncAnimation
See also: Solutions, Plotting and Accuracy and diagnostics.