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.0.tar.gz (359.5 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.11 Windows x86-64

gpgi-0.7.0-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.0-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.0-cp310-cp310-win_amd64.whl (203.1 kB view details)

Uploaded CPython 3.10 Windows x86-64

gpgi-0.7.0-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.0-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.0-cp39-cp39-win_amd64.whl (204.8 kB view details)

Uploaded CPython 3.9 Windows x86-64

gpgi-0.7.0-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.0-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.0-cp38-cp38-win_amd64.whl (205.1 kB view details)

Uploaded CPython 3.8 Windows x86-64

gpgi-0.7.0-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.0-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.0.tar.gz.

File metadata

  • Download URL: gpgi-0.7.0.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.0.tar.gz
Algorithm Hash digest
SHA256 993148e9fa0fd51dc93c8b23f3e7a1959552949736d8cf3cfa2ccdd37f0b7ea4
MD5 343f83f1b0e2c2f8c58b52fe1a7ac515
BLAKE2b-256 f903381d930f74a16eb5212d02356f28cbc5ea91442f904fd08ea3adfb4f7866

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gpgi-0.7.0-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.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 09d77c10eac9a71a6542cb9f1ccd7afa863a2a32cd0197336c373bbeb386ecd1
MD5 928052efee55558db8630157909e378b
BLAKE2b-256 65adeb953275a2328bd3d84d163f72493d5440ae32a5c8b47617a609c287c4e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gpgi-0.7.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5346becbb023aa6a8c52d5e85d7b70d40018809460330d1d1d7a95b1cff9c7fb
MD5 d99e099422823dfd2969b0042578792e
BLAKE2b-256 025b6426fa3dd0ead53653cc1aa16b8d0efcfe0299a3805e00304bd8387252fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gpgi-0.7.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1ad0136942d27fbb94694e5921027d548c6bb40d72b8e1ea75bc9d6bce755192
MD5 5b0fdfdb8099d7de2c1278b552c1df3e
BLAKE2b-256 cdd7289abc4a08c7e24532997957c3334c3c5cd1a84b5f043279ce8175e982cf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gpgi-0.7.0-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.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4a164929d64ea4d2a952b8212602ebee9aa0b6023ef7d9398c6861b19d4e1e56
MD5 84fa231a22943191e5f07303ad12128a
BLAKE2b-256 936c07b8a47e86e799c08253dd13b4e91d1eb268ce9031b91fb505e923d82414

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gpgi-0.7.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 13f9c68e20fcc6a577e66af48b118f5f71b9b6dc1c28c317ea3e4c1393e5ee27
MD5 60ca0bbd6e5bea9b7fddff11a0b57877
BLAKE2b-256 aa5ef86817e34f99c09781acbb65d8ec43c7c542298c41320d82b226a0258f0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gpgi-0.7.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 667edfbc555c603f180817332a7dd5098a8ca6e59de64df2f42290f627a56de5
MD5 c91439f27b1126aa53bf6916fe030c83
BLAKE2b-256 4c9e77865072621539c8844a01413a731f0b17f86fac22023250e12878518487

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gpgi-0.7.0-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.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 20acbdeef408f6b99f548bc8a8292451a6227bf70f5addc4a1d7b38b3821b4e3
MD5 0bf0057b7927dc2c21d0ba40db4aa53c
BLAKE2b-256 1b44810aea4a87dcf2a1cd446aab21af083b4059fd4d79f046ab82696665af8c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gpgi-0.7.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 244f3436e6f12d7ee02a99796a7ddfb8d8014af291c69f12d3d3bc1a22fe742c
MD5 59a7bb8f896e9efc70dac70c0b5d5196
BLAKE2b-256 422281dc112b13794e6657ba9a4c2eca4c9aba7b6dda9fb288df9fd3d3961743

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gpgi-0.7.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0afa68d929f2f80b974e67283b23342c6883205a14e6109e53151344b1abe5d4
MD5 89ee6e58dbb69597210acf1ea29a9b32
BLAKE2b-256 807d3f17868645b5630c86eccd3c2f9b0a5d4c8422daf356ef833b86e08510c4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gpgi-0.7.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 205.1 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.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 d54261e3da5457138514db9dc5dbbaf302c6f86b7092052a742ff6442c19d3c8
MD5 c9e0dd1c8bc47f2eb4b611af3e70d22b
BLAKE2b-256 c329d8afb5bb8b46199f435cfcba10be75d60cf4b6a9169241c55c2b3a24f4d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gpgi-0.7.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 189efe739061aa7ab8699e406bb4bf6a46b4e90ad548b9a6e904513a47769e82
MD5 d2db821652cedd4c7f0dfca1f5d1ab0d
BLAKE2b-256 80eac900a22c4002dfdf713a0abb33dedcdb862dc10658907a2ae54cff112af0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for gpgi-0.7.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 11e437206aa05e2efe99bdf7186a2c404128cd96b8bf978969fd8a2f6481e707
MD5 447088a6f3e657b3fd0c93fdf4078cb5
BLAKE2b-256 8d321faba505ae3f9b14211b0e24fd4b4a1b7b5c7b6cf07f95f8b355d19a3d89

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