Skip to main content

A Generic Particle+Grid Interface

Project description

GPGI

PyPI PyPI pre-commit.ci status Code style: black

A Generic Particle + Grid data Interface

This small Python library implements fundamental grid deposition algorithms to analyse (rectilinear) grid + particle datasets, with an emphasize on performance. Core algorithms are implemented as Cython extensions.

Installation

python -m pip install --upgrade pip
python -m pip install gpgi

Supported applications

A rectilinear grid is defined as 1D arrays representing cell left edges in each directions. Note that the last point of such an array is interpreted as the right edge of the rightmost cell, so for instance, a 1D grid containing 100 cells is defined by 101 edges.

Particles are defined as points that live on the grid.

Deposition is the action of going from particule description to a grid description of a field. It is useful to analyze, compare and combine simulation data that exists in a combination of the two formalisms. This process is not reversible as it degrades information.

For instance, here's a simple overlay of a particle set (red dots) against a background that represents the deposited particle count.

This example illustrates the simplest possible deposition method "Particle in Cell", in which each particle contributes only to the cell that contains it.

More refined methods are also available.

Supported deposition methods

method name abreviated name order
Nearest Grid Point NGP 0
Cloud in Cell CIC 1
Triangular Shaped Cloud TSC 2

Supported geometries

geometry name axes order
cartesian x, y, z
polar radius, z, azimuth
cylindrical radius, azimuth, z
spherical radius, colatitude, azimuth
equatorial radius, azimuth, latitude

Time complexity

An important step in perfoming deposition is to associate particle indices to cell indices. This step is called "particle indexing". In directions where the grid is uniformly stepped (if any), indexing a particle is an O(1) operation. In the more general case, indexing is performed by bisection, which is a O(log(nx))) operation (where nx represents the number of cells in the direction of interest).

Usage

The API consists in a load function, which returns a Dataset object.

Load data

import numpy as np
import gpgi

nx = ny = 64
nparticles = 600_000

prng = np.random.RandomState(0)
ds = gpgi.load(
    geometry="cartesian",
    grid={
        "cell_edges": {
            "x": np.linspace(-1, 1, nx),
            "y": np.linspace(-1, 1, ny),
        },
    },
    particles={
        "coordinates": {
            "x": 2 * (prng.normal(0.5, 0.25, nparticles) % 1 - 0.5),
            "y": 2 * (prng.normal(0.5, 0.25, nparticles) % 1 - 0.5),
        },
        "fields": {
            "mass": np.ones(nparticles),
        },
    },
)

The Dataset object holds a grid and a particles attribute, which both hold a fields attribute for accessing their data. But more importantly, the Dataset has a deposit method to translate particle fields to the grid formalism.

Deposit Particle fields on the grid

particle_mass = ds.deposit("mass", method="nearest_grid_point")  # or "ngp" for shorts

Visualize In this example we'll use matplotlib for rendering, but note that matplotlib is not a dependency to gpgi

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.set(aspect=1, xlabel="x", ylabel="y")

im = ax.pcolormesh(
    "x",
    "y",
    particle_mass.T,
    data=ds.grid.cell_edges,
    cmap="viridis",
)
fig.colorbar(im, ax=ax)

The example script given here takes about a second (top to bottom).

Supplying arbitrary metadata

new in gpgi 0.4.0

Dataset objects have a special attribute metadata which is a dictionary with string keys. This attribute is meant to hold any special metadata that may be relevant for labelling or processing (e.g. simulation time, author, ...). Metadata can be supplied at load time as

ds = gpgi.load(
    geometry="cartesian",
    grid=...,
    particles=...,
    metadata={"simulation_time": 12.5, "author": "Clément Robert"}
)

Boundary conditions

new in gpgi 0.5.0

With CIC and TSC deposition, particles contribute to cells neighbouring the one that contains them. For particles that live in the outermost layer of the domain, this means some of their contribution is lost. This behaviour corresponds to the default 'open' boundary condition, but gpgi has builtin support for more conservative boundary conditions.

Boundary conditions can selected per field, per axis and per side. Builtin recipes all perform linear combinations of ghost layers (same-side and opposite side) and active domain layers (same-side and opposite side), and replace the same-side active layer with the result.

User-selected boundary conditions take the form of an optional argument to Dataset.deposit, as dictionnary with keys being axes names, and values being 2-tuples of boundary conditions names (for left and right side respectively). For instance, here's how one would require periodic boundary conditions on all axes:

ds.deposit(
    "mass",
    method="cic",
    boundaries={
        "x": ("periodic", "periodic"),
        "y": ("periodic", "periodic"),
    }
)

Unspecified axes will use the default 'open' boundary.

Builtin recipes

boundary conditions description conservative ?
open (default) no special treatment no
periodic add opposite ghost layer to the active domain yes
wall add same-side ghost layer to the active domain yes
antisymmetric substract same-side ghost layer from the active domain no

Define custom recipes

gpgi's boundary recipes can be customized. Let's illustrate this feature with a simple example. Say we want to fix the value of the deposited field in some outer layer. This is done by defining a new function on the user side:

def ones(
    same_side_active_layer,
    same_side_ghost_layer,
    opposite_side_active_layer,
    opposite_side_ghost_layer,
    weight_same_side_active_layer,
    weight_same_side_ghost_layer,
    weight_opposite_side_active_layer,
    weight_opposite_side_ghost_layer,
    side,
    metadata,
):
   return 1.0

where all first eight arguments are numpy.ndarray objects with the same shape (which includes ghost padding !), to which the return value must be broadcastable, side can only be either "left" or "right", and metadata is the special Dataset.metadata attribute. Not all arguments need be used in the body of the function, but this signature is required.

The method must then be registered as a boundary condition recipe as

ds.boundary_recipes.register("ones", ones)

where the associated key (here "ones") is arbitrary. The recipe can now be used exactly as builtin ones, and all of them can be mixed arbitrarily.

ds.deposit(
    "mass",
    method="cic",
    boundaries={
        "x": ("ones", "wall"),
        "y": ("periodic", "periodic"),
    }
)

Note that all first eight arguments in a boundary recipe function should represent an extensive physical quantity (as opposed to intensive). When depositing an intensive quantity u, a weight field w should be supplied (see next section), in which case, the first four arguments represent u*w and the following four represent w, so that u can still be obtained within the function as a ratio if needed.

Weight fields

new in gpgi 0.7.0

Intrisically, deposition algorithms assume that the deposited field u represents an extensive physical quantity (like mass or momentum). In order to deposit a intentive quantity (like velocity or temperature), one must provide an appropriate weight field w.

Let u' and w' be the equivalent on-grid descriptions to u and w. They are obtained as

w'(x) = Σ w(i) c(i,x)
u'(x) = (1/w'(x)) Σ u(i) w(i) c(i,x)

where x is the spatial position, i is a particle index, and c(i,x) are geometric coefficients associated with the deposition method.

Boundary recipes may be associated to the weight field with the weight_field_boundaries argument.

Call help(ds.deposit) for more detail.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gpgi-0.7.1.tar.gz (359.5 kB view details)

Uploaded Source

Built Distributions

gpgi-0.7.1-cp311-cp311-win_amd64.whl (201.2 kB view details)

Uploaded CPython 3.11 Windows x86-64

gpgi-0.7.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

gpgi-0.7.1-cp311-cp311-macosx_10_9_x86_64.whl (239.0 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

gpgi-0.7.1-cp310-cp310-win_amd64.whl (203.1 kB view details)

Uploaded CPython 3.10 Windows x86-64

gpgi-0.7.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

gpgi-0.7.1-cp310-cp310-macosx_10_9_x86_64.whl (242.9 kB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

gpgi-0.7.1-cp39-cp39-win_amd64.whl (204.8 kB view details)

Uploaded CPython 3.9 Windows x86-64

gpgi-0.7.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

gpgi-0.7.1-cp39-cp39-macosx_10_9_x86_64.whl (241.4 kB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

gpgi-0.7.1-cp38-cp38-win_amd64.whl (205.2 kB view details)

Uploaded CPython 3.8 Windows x86-64

gpgi-0.7.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

gpgi-0.7.1-cp38-cp38-macosx_10_9_x86_64.whl (238.4 kB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

File details

Details for the file gpgi-0.7.1.tar.gz.

File metadata

  • Download URL: gpgi-0.7.1.tar.gz
  • Upload date:
  • Size: 359.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for gpgi-0.7.1.tar.gz
Algorithm Hash digest
SHA256 ab531773f4afc11bf4300154befa58b1cb3c4b3980619a917cb1788e83dca879
MD5 e4e4491b068c28fa707831198bf98b21
BLAKE2b-256 8d4ca492cdd539cd19deae899f7dabcf45c574a6e849a8e78956ee4612e2674a

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: gpgi-0.7.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 201.2 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for gpgi-0.7.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 840f09e9619029a54a260ee8c81b1b9a2a0cea246f99c937281d83c3b06c48a4
MD5 1e11380c4515a2637e3a122a638fc001
BLAKE2b-256 3a8659c81fd321ccab7d37ed2209bdfae7910d49dc06fc813493f2d3e48f0ba1

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gpgi-0.7.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e64c4d031a512a4fd33aba81a5da13d718ece58f9d675f94ba5af39ac18a24b6
MD5 0a57489e3c3c0841307d5fdb3e6b626b
BLAKE2b-256 67250eb8fca4afd73ecd483b628fd0416c4e50a0a78819ad82e5e5d898884a74

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for gpgi-0.7.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3e414ce3920c63e21e94f0653cf908b8812cd0c512b4eb1d932471666d2b5475
MD5 db23842919325f9247278d7e96dfe32d
BLAKE2b-256 ba0c96e0d0c42616c4a5454de087636fa42c1528e9ce96b3364adabc231a4ea4

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: gpgi-0.7.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 203.1 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for gpgi-0.7.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 30f7b5d94aa5604db5ca90e9413353c18278301e05100a7ba717abac150dd6c0
MD5 b2d12abaeb0de00df124fde2254a903c
BLAKE2b-256 b0bf070ddcff1547b95a9b6de101f334980f51ab70477065469e22dd8145563a

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gpgi-0.7.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f0cdf5200ed9ffed9d7f7d6c5a09114093754c04a0cc1a88369d29e3cc6c0e81
MD5 c0c271d8eb0a7a855ba2c1f41e95b00a
BLAKE2b-256 1e55b7bfeba90032e6b21d2bac4472daa04ea4834d84fb69b0bab81ca8263b9d

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for gpgi-0.7.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 57798b2659c2efb64cb8a8699aee6f346378bd8fbf7d77cefa3f83e708e5413c
MD5 5e4c8eee9268b5cee22d57800acf27fc
BLAKE2b-256 ff5ff2075c840e80859418bed14dba4f80b6014c9eaa1d2bd40f0d56614d7f94

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: gpgi-0.7.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 204.8 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for gpgi-0.7.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 4f4016365dfdccf14cca8916a4e31ce0804ce09406a83c70cfe7f68f2441a57e
MD5 47afd9fce08e459a8d2b479a199e9029
BLAKE2b-256 0680d2f4e67b4511017c790db9eacdb9d7c053fb5f8d95a722586a88b806b7be

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gpgi-0.7.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 69ddf0f836265723d9542ed8aad82a1f35f641a9816465f637cd7c2b8467fdeb
MD5 ea687766884381476e42fce6a8e52263
BLAKE2b-256 11e9c21ad5efd370bfd8e2a81981090ab412abf20aaad78d6e68499740847da4

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for gpgi-0.7.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c61002769cb43d0ec29a49fa6a12c29e353f8997aebcd52cda7b8920c5cfafe0
MD5 c08086d437c77919ffafec6b61e18f66
BLAKE2b-256 0cda34b23b6555e37cf866124c54d35b22acf7d73722f0acf9d8f2aa16733edc

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: gpgi-0.7.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 205.2 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for gpgi-0.7.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 43a7fe23c0c5ee2ef6e986b9940550b50ba5cff0637283182417b95b475190f5
MD5 f8c1919ac4e7339b93639a6dc34f0976
BLAKE2b-256 68354370bc4458d072e3404e249c0cae32ea671123033652971f25390e73486c

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for gpgi-0.7.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b99c6b7011e4c66227d42ad7bba4754da7df5cd2d09171bd7da8e8ee66e85041
MD5 4cf4f8898c2dac2d675699960babb7a7
BLAKE2b-256 278eaed56c2abc0d122d5b5231b593982e57264391b986195b92647390f27898

See more details on using hashes here.

File details

Details for the file gpgi-0.7.1-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for gpgi-0.7.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1e409118bed24a82e6f83ff51ef9c5a580606f04e917dfd61e4b1aae66e96140
MD5 a417d9ac050d9f61330604446feaebfc
BLAKE2b-256 e40a5a9a5c9b96bfccd69c9c004b3b754f51a563d789b6acf78006250731cf0d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page