Skip to main content

Single-process mass assignment (NGP/CIC/TSC/PCS) with OpenMP

Project description

mass_assignment

A Python library for 3D mass assignment and velocity moment estimation using C++ and OpenMP.

Features

  • Multiple Assignment Schemes: Supports NGP, CIC, TSC, and PCS.
  • High-Order Moments: Compute Density (0th order), Velocity (1st), Velocity Dispersion (2nd), Skewness (3rd), and Kurtosis (4th).
  • Optimized Performance: Core routines implemented in C++ with OpenMP for efficient multi-threading.

Installation

pip install mass-assignment

Usage

  1. Density Field
  • Map particle masses to a 128^3 3D mesh.
import numpy as np
import mass_assignment as ma

pos = np.random.uniform(0, 100, (1000000, 3))
mass = np.random.uniform(0.5, 1.5, 1000000)

# Compute density
# lbox: Box size, nmesh: Grid resolution
grid_density = ma.dens(pos, nmesh=128, lbox=100.0, method="TSC", mass=mass)
  1. Velocity Dispersion Field
  • Compute the 2nd-order moment tensor
import numpy as np
import mass_assignment as ma

pos = np.random.uniform(0, 100, (1000000, 3))
vel = np.random.normal(0, 10, (1000000, 3))

# Returns an array of shape (nmesh, nmesh, nmesh, 6)
# Component order: xx, xy, xz, yy, yz, zz
dispersion_tensor = ma.sigma_norm(pos, vel, nmesh=128, lbox=100.0, method="TSC")
  1. Sample a mesh field at particle positions
  • mesh_to_ptcl interpolates a scalar field defined on a regular 3D grid to arbitrary particle positions.
import numpy as np
import mass_assignment as ma

nmesh = 128
lbox = 1.0

# Example scalar field on the mesh (nmesh, nmesh, nmesh)
mesh = np.random.randn(nmesh, nmesh, nmesh).astype(np.float32)

# Particle positions (N,3) in [0, lbox)
pos = np.random.rand(200000, 3).astype(np.float32) * lbox
val = ma.mesh_to_ptcl(pos, mesh, lbox=lbox, method="TSC", nthreads=0)
print(val.shape)  # (N,)

API Parameters

  • Assignment Schemes (method) You can pass the scheme as a string or an integer.

    • NGP (1): Nearest Grid Point
    • CIC (2): Cloud-In-Cell
    • TSC (3): Triangular Shaped Cloud
    • PCS (4): Piecewise Cubic Spline
  • Mass (mass)

    • If mass is set to None (default), the library assumes uniform mass (weight = 1.0 for every particle).
  • Parallelization (nthreads)

    • 0 (Default): Automatically uses all available CPU threads via OpenMP.
    • N: Uses a specific number of threads.
  • Normalization Mode (norm_mode) This parameter defines how high-order moments are normalized in _norm functions:

    • diag_norm (Default): Normalizes each tensor component by the mass in that cell. This corresponds to the Frobenius norm calculation for each independent component.
    • tr_norm: Normalizes the tensor using the trace-based scaling.

Disclaimer: The implementations for Skewness (3rd order) and Kurtosis (4th order) are currently untested.

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

mass_assignment-1.0.2.tar.gz (11.7 kB view details)

Uploaded Source

File details

Details for the file mass_assignment-1.0.2.tar.gz.

File metadata

  • Download URL: mass_assignment-1.0.2.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for mass_assignment-1.0.2.tar.gz
Algorithm Hash digest
SHA256 7ef7e9b0b4ae9337e887a95a920159430c3becf377b03718ce1dee7c3545110b
MD5 e624cd7bc1ef47e20ee5d52e6e1bc902
BLAKE2b-256 13e93f9dc065eb6e34eb072ab7ba444c5187ddab5832386c67a1dd1db8e65cb6

See more details on using hashes here.

Supported by

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