Skip to main content

Ansh

PyPI - Version PyPI - Python Version

Ansh (अंश), which means a part or an element, is a Python library to define numerical approximation of scalar and vector fields ideal for finite-element analysis. It is a thin wrapper around meshio, pyvista, and skfem. meshio is used for reading and writing meshes. skfem is used for finite-element field definition. pyvista is used for plotting both meshes and fields.


Table of Contents

Installation

pip install ansh       # pip
uv add ansh            # uv
pixi add --pypi ansh   # pixi

Quick Start

Meshes

Load a mesh from file and inspect its properties:

import ansh
import numpy as np

m = ansh.Mesh.from_file("tests/meshes/med/omega.med")
m.info = {"name": "shell", "about": "one of the unit test meshes"}
m.points          # float64 array of shape (n_points, 3)
m.cells           # list of CellBlock objects (type + connectivity)
m.cell_centres    # list of cell centre arrays per cell block
m.subregions      # dict mapping region names to cell index arrays
m.scale           # mesh scaling factor
m.info            # user-attached metadata dict

Subregions can be added via cell indices or selector functions:

def select_unit_cube(centres):
    return (
        ((centres[:, 0] < 1.) & (centres[:, 0] > -1.)) &
        ((centres[:, 1] < 1.) & (centres[:, 1] > -1.)) &
        ((centres[:, 2] < 1.) & (centres[:, 2] > -1.))
    )

# value has one entry per cell block. So no cells from block 0 and 2.
m.add_subregion(
    subregion_name="cube",
    value=[np.array([], dtype=np.int_), select_unit_cube, np.array([], dtype=np.int_)],
)

Convert to PyVista or scikit-fem for further use:

m.to_pyvista()        # returns pyvista.MultiBlock with one block per subregion
m.to_meshio()         # returns meshio.Mesh
m.to_skfem()          # returns skfem MeshTet1
m.plot()              # interactive 3D plot via PyVista

See the full mesh demo for more details.

Scalar fields

Define a scalar field on a mesh by specifying the element type and initial value:

f = ansh.ScalarField(mesh=m, element_type=ansh.element_types.TetP1(), value=42.)
f.element_type     # skfem element (e.g. ElementTetP1)
f.value            # 1D float64 array of DOF values, shape (n_dofs,)
f.dof_locations    # coordinates of the DOF points, shape (n_dofs, 3)

The field is callable — evaluate it at arbitrary points inside the mesh:

f([0, 0, 0])                              # array([42.])
f(np.array([[0, 2., -1.], [0, 0, 0]]))   # array([42., 42.])

Update the value with a callable (receives DOF locations, returns values):

def norm_val(dofs):
    return np.linalg.norm(dofs, axis=-1)

f.value = norm_val

Set values on a specific subregion:

f.set_subregion_value(name="boundary", value=0.)

Export for matrix assembly or visualisation:

f.to_skfem()       # returns skfem DiscreteField
f.skfem_basis      # returns skfem CellBasis
f.to_pyvista()     # returns pyvista MultiBlock with field data attached
f.plot()           # interactive 3D plot with a clip widget

See the full scalar field demo for more details.

Vector fields

Define a vector field similarly, passing a 3-component value:

f = ansh.VectorField(
    mesh=m,
    element_type=ansh.element_types.TetP1(),
    value=[42., 42., 42.],
)
f.element_type     # skfem ElementVector (wrapping ElementTetP1)
f.value            # 2D float64 array of DOF values, shape (n_dofs, 3)
f.dof_locations    # coordinates of the DOF points, shape (n_dofs, 3)

Evaluate at arbitrary points:

f([0, 0, 0])                              # array([[42., 42., 42.]])
f(np.array([[0, 2., -1.], [0, 0, 0]]))   # array([[42., 42., 42.], ...])

Set value via callable (receives DOF locations, returns array of shape (n_dofs, 3)):

f.value = lambda dofs: dofs

Set subregion values:

f.set_subregion_value(name="shell", value=[0., 0., 0.])

Plot with glyphs coloured by component or magnitude:

f.plot(colour_with="z", factor=3e-3, cmap="RdBu")
f.plot(colour_with="magnitude", factor=3e-3, cmap="viridis")

See the full vector field demo for more details.

An advanced example combining meshes with scalar and vector fields is available in the demagnetisation truncation notebook.

I/O

Mesh — read any format supported by meshio (.med, .vol, .vtk, .msh, etc.) and write to Abaqus .inp or Ansh HDF5 .h5:

m.to_file("mesh.inp")
m.to_file("mesh.h5")
ansh.Mesh.from_file("mesh.inp")
ansh.Mesh.from_file("mesh.h5")

Scalar/vector field — read/write HDF5 (.h5) or ParaView multiblock (.vtm):

f.to_file("field.h5")
f.to_file("field.vtm")
ansh.ScalarField.from_file("field.h5")
ansh.VectorField.from_file("field.h5")

Developers

Set up a development environment with Pixi:

git clone https://codeberg.org/Sankhya/Ansh.git
cd Ansh
pixi install
pixi shell -e dev   # activates the environment

Run tests with pytest:

pixi run pytest

Lint the source with Ruff:

pixi run ruff check src/ansh/

Build the package with Hatchling:

pixi run hatch build -t wheel

Publish to PyPI:

pixi run hatch publish

Explore the Jupyter notebooks for ad-hoc testing and demos:

pixi run jupyter lab docs/

Key conventions:

  • Source lives under src/ansh/ (src layout).
  • Public API is exported from src/ansh/__init__.py — symbols: Mesh, CellBlock, ScalarField, VectorField, element_types.
  • Runtime type validation is enforced by beartype decorators wherever it makes sense.
  • Docstrings follow Google style (Args/Returns/Raises/Notes).
  • HDF5 I/O uses the .h5 extension.
  • The remote is on Codeberg.

License

ansh is distributed under the terms of the MIT license.

Download files

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

Source Distribution

ansh-0.2.1.tar.gz (61.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ansh-0.2.1-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

Details for the file ansh-0.2.1.tar.gz.

File metadata

  • Download URL: ansh-0.2.1.tar.gz
  • Upload date:
  • Size: 61.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.17.1 {"ci":null,"cpu":"x86_64","distro":{"libc":{"lib":"glibc","version":"2.44"},"name":"Arch Linux"},"implementation":{"name":"CPython","version":"3.12.13"},"installer":{"name":"hatch","version":"1.17.1"},"openssl_version":"OpenSSL 3.6.3 9 Jun 2026","python":"3.12.13","system":{"name":"Linux","release":"7.1.5-zen1-2-zen"}} HTTPX2/2.10.0

File hashes

Hashes for ansh-0.2.1.tar.gz
Algorithm Hash digest
SHA256 392545d479e7ac244caa740c3f43301d48520ef780a97d84c169b75fbf68330d
MD5 97d9ddf57434fce80e857368d75f4cd3
BLAKE2b-256 12c34d21c674156093dfcb4fcf99f101e3b484c3ef24b0f36c1cf37f0c6f42e9

See more details on using hashes here.

File details

Details for the file ansh-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: ansh-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 27.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.17.1 {"ci":null,"cpu":"x86_64","distro":{"libc":{"lib":"glibc","version":"2.44"},"name":"Arch Linux"},"implementation":{"name":"CPython","version":"3.12.13"},"installer":{"name":"hatch","version":"1.17.1"},"openssl_version":"OpenSSL 3.6.3 9 Jun 2026","python":"3.12.13","system":{"name":"Linux","release":"7.1.5-zen1-2-zen"}} HTTPX2/2.10.0

File hashes

Hashes for ansh-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c166a2ffc36fdd5198687b643e5d2e5c21aa331bb7387f81884754447b6a9a61
MD5 802f0524feb9a20860c8441056948458
BLAKE2b-256 76928b453912f82efc925f00b8fa2e92341561838c9764e374526ecabfaeb36f

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 files

0.2.0

2 files

0.1.2

1 file

0.1.1

1 file

0.1.0

1 file

Supported by

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