Working with numerical grids made easy.
Main Features
- Quickly define numerical grids for any rectangular or curvilinear coordinate system
- Differentiation and integration
- Interpolation
- Easy manipulation of meshed functions
- Using high precision spectral methods (FFT + Chebyshev) wherever possible
- Includes multigrid functionality
- Fully compatible with numpy
pip install --upgrade numgrids
As a quick example, here is how you define a grid on the unit disk using polar coordinates. Along the azimuthal (angular) direction, choose an equidistant spacing with periodic boundary conditions:
from numgrids import *
from numpy import pi
axis_phi = Axis(AxisType.EQUIDISTANT, 50, 0, 2*pi, periodic=True)
Along the radial axis, let's choose a non-equidistant spacing:
axis_radial = Axis(AxisType.CHEBYSHEV, 20, 0, 1)
Now combine the axes to a grid:
grid = Grid(axis_radial, axis_phi)
Sample a meshed function on this grid:
from numpy import exp, sin
R, Phi = grid.meshed_coords
f = R**2 * sin(Phi)**2
Define partial derivatives
# second argument means derivative order, third argument means axis index:
d_dr = Diff(grid, 1, 0)
d_dphi = Diff(grid, 1, 1)
df_dr = d_dr(f)
df_dphi = d_dphi(f)
Obtain the matrix representation of the differential operators:
d_dr.as_matrix()
Out: <1000x1000 sparse matrix of type '<class 'numpy.float64'>'
with 20000 stored elements in COOrdinate format>
Define integration operator
I = Integral(grid)
Calculate the area integral
(taking into account the appropriate integration measure
I(f * R)
Setting boundary values to zero
f[grid.boundary] = 0 # grid.boundary is boolean mask selecting boundary grid points
or to something more complicated:
f[grid.boundary] = exp(-R[grid.boundary])
Create an interpolation function
inter = Interpolator(grid, f)
Interpolate for a single point
point = (0.1, 0.5)
inter(point)
or for many points at once, like for a parametrized curve:
t = np.linspace(0, 1, 100)
points = zip(2*t, t**2)
inter(points)
To get an idea how numgrids can be used, have a look at the following example notebooks:
- How to define grids
- Partial derivatives in any dimension
- Polar coordinates on unit disk
- Spherical Grid and the Spherical Laplacian
- Solving the Schrödinger equation for the quantum harmonic oscillator
Clone the repository
git clone https://github.com/maroba/numgrids.git
In the project root directory, submit
python setup.py develop
to install the package in development mode.
Run the tests:
python -m unittest discover tests
- Fork the repository
- Develop
- Write tests!
- Create an issue
- Create a pull request, when done