This document provides detailed documentation for the classes and methods in the vbcsr library.
The VBCSR class represents a distributed block-sparse matrix. It inherits from scipy.sparse.linalg.LinearOperator.
shape:Tuple[int, int]- Global shape of the matrix(rows, cols).ndim:int- Number of dimensions (always 2).nnz:int- Global number of non-zero elements.dtype:np.dtype- Data type of the matrix elements (np.float64ornp.complex128).matrix_kind:str- Active backend family ("csr","bsr", or"vbcsr").T:VBCSR- Transpose of the matrix (returns a new object).real:VBCSR- Real part of the matrix (returns a new object).imag:VBCSR- Imaginary part of the matrix (returns a new object).
@classmethod
create_serial(cls, global_blocks: int, block_sizes: List[int], adjacency: List[List[int]], dtype: type = np.float64, comm: Any = None) -> 'VBCSR'Creates a matrix using serial graph construction. Rank 0 defines the entire structure, which is then distributed.
- comm: MPI communicator (or
Nonefor serial). - global_blocks: Total number of blocks.
- block_sizes: List of block sizes.
- adjacency: Adjacency list (list of lists of neighbors).
@classmethod
create_distributed(cls, owned_indices: List[int], block_sizes: List[int], adjacency: List[List[int]], dtype: type = np.float64, comm: Any = None) -> 'VBCSR'Creates a matrix using distributed graph construction. Each rank defines only its owned blocks.
- owned_indices: Global indices of blocks owned by this rank.
@classmethod
create_random(cls, global_blocks: int, block_size_min: int, block_size_max: int, density: float = 0.01, dtype: type = np.float64, seed: int = 42, comm: Any = None) -> 'VBCSR'Creates a random connected matrix for benchmarking purposes.
@classmethod
from_scipy(cls, spmat: Any, comm=None, root: int = 0) -> 'VBCSR'Creates a VBCSR matrix from a SciPy sparse matrix.
- spmat: SciPy sparse matrix. In MPI, provide it on
rootand passNoneon other ranks. - comm: Optional communicator. Distributed imports must pass it explicitly.
- root: Rank that owns the SciPy input for collective MPI imports.
add_block(self, g_row: int, g_col: int, data: np.ndarray, mode: AssemblyMode = AssemblyMode.ADD) -> NoneAdds or inserts a dense block into the matrix.
get_block(self, g_row: int, g_col: int) -> Optional[np.ndarray]Returns a dense copy of one local block in row-major layout, or None if that block is not present locally.
assemble(self) -> NoneFinalizes matrix assembly. Must be called after adding blocks.
mult(self, x: Union[DistVector, DistMultiVector, np.ndarray], y=None) -> Union[DistVector, DistMultiVector]Applies the matrix to a distributed vector, multivector, or compatible local NumPy array.
transpose(self) -> 'VBCSR'Returns a new transposed matrix.
transpose_(self) -> NoneIn-place transpose. More memory efficient.
conj(self) -> 'VBCSR'Returns a new conjugated matrix. conjugate() is the primary name; conj() is a compatibility alias.
conj_(self) -> NoneIn-place conjugate.
duplicate(self, independent_graph: bool = True) -> 'VBCSR'Returns a deep copy of the matrix values. copy() is a compatibility alias for duplicate().
- independent_graph: If
True, duplicate the graph object too. IfFalse, reuse the same immutable graph structure.
dot(self, other: Union['VBCSR', DistVector, DistMultiVector, np.ndarray]) -> Union['VBCSR', DistVector, DistMultiVector]Performs matrix multiplication.
- If
otherisVBCSR: Sparse Matrix-Matrix Multiplication (SpMM). - If
otherisDistVector/DistMultiVector: Matrix-Vector Multiplication (SpMV).
spmm(self, B: 'VBCSR', threshold: float = 0.0, transA: bool = False, transB: bool = False) -> 'VBCSR'Sparse Matrix-Matrix Multiplication with filtering options.
- threshold: Drop blocks with Frobenius norm less than threshold.
spmm_self(self, threshold: float = 0.0, transA: bool = False) -> 'VBCSR'Computes
add(self, B: 'VBCSR', alpha: float = 1.0, beta: float = 1.0) -> 'VBCSR'Computes
filter_blocks(self, threshold: float = 0.0) -> NoneDrops locally stored blocks with Frobenius norm below threshold while preserving the current backend family.
get_block_density(self) -> floatReturns the global block-density estimate nnz_blocks / n_blocks^2.
scale(self, alpha: Union[float, complex, int]) -> NoneScales the matrix in-place by alpha.
shift(self, alpha: Union[float, complex, int]) -> NoneAdds alpha to diagonal elements.
add_diagonal(self, v: Union[DistVector, np.ndarray]) -> NoneAdds vector v to diagonal elements (
extract_submatrix(self, global_indices: List[int]) -> 'VBCSR'Extracts a submatrix corresponding to given global block indices. For distributed matrices this operation is collective.
insert_submatrix(self, submat: 'VBCSR', global_indices: List[int]) -> NoneInserts a submatrix back into the matrix. For distributed matrices this operation is collective.
spmf(self, func_name: str, method: str = "lanczos", verbose: bool = False) -> 'VBCSR'Applies a supported sparse matrix function approximation and returns a matrix in the same backend family.
to_dense(self) -> np.ndarrayConverts the local matrix view to a dense NumPy array. This does not gather remote ranks.
from_dense(self, data: np.ndarray) -> NoneUpdates the local matrix view from a dense NumPy array.
to_scipy(self, format: Optional[str] = None) -> AnyConverts the local matrix view to a SciPy sparse matrix (bsr or csr). This is a local export helper, not a distributed gather.
row_ptr: np.ndarrayReturns the local logical block-row pointer array.
col_ind: np.ndarrayReturns the local logical block-column index array.
get_values(self) -> np.ndarrayReturns the packed local block values in row-major order, matching row_ptr / col_ind.
save_matrix_market(self, filename: Union[str, bytes]) -> NoneWrites a Matrix Market file in serial execution. Distributed matrices raise an error.
create_vector(self) -> DistVectorCreates a compatible DistVector.
create_multivector(self, k: int) -> DistMultiVectorCreates a compatible DistMultiVector with k columns.
__getitem__(self, key: Tuple[int, int]) -> ScalarScalar and slicing indexing are currently unsupported. Use get_block(...) for block-level inspection.
+,-: Matrix addition/subtraction.*: Scalar multiplication.@: Matrix multiplication.+=,-=,*=: In-place operators.-A: Negation.
Represents a distributed 1D vector.
shape:Tuple[int]- Global shape(size,).ndim:int- Number of dimensions (1).size:int- Global size.local_size:int- Number of locally owned elements.ghost_size:int- Number of ghost elements.full_size:int- Total local size (owned + ghost).T:DistVector- Returns self.
duplicate(self) -> 'DistVector'Returns a deep copy. copy() is a compatibility alias for duplicate().
to_numpy(self) -> np.ndarrayReturns locally owned part as NumPy array.
from_numpy(self, arr: np.ndarray) -> NoneUpdates locally owned part from NumPy array.
set_constant(self, val: Union[float, complex, int]) -> NoneSets all local elements to val.
set_random_normal(self, normalize: bool = False) -> NoneFills the locally owned entries with random normal values.
scale(self, alpha: Union[float, complex, int]) -> NoneScales vector in-place.
axpy(self, alpha: Scalar, x: 'DistVector') -> NoneComputes
axpby(self, alpha: Scalar, x: 'DistVector', beta: Scalar) -> NoneComputes
pointwise_mult(self, other: 'DistVector') -> NoneElement-wise multiplication
dot(self, other: 'DistVector') -> ScalarComputes global dot product
sync_ghosts(self) -> NoneSynchronizes ghost elements from their owners.
reduce_ghosts(self) -> NoneReduces (sums) ghost elements back to their owners.
+,-: Vector addition/subtraction (supports scalar broadcast).*: Element-wise multiplication (supports scalar).@: Dot product.+=,-=,*=: In-place operators.
Represents a distributed collection of vectors (2D, column-major).
shape:Tuple[int, int]- Global shape(rows, cols).ndim:int- Number of dimensions (2).size:int- Total elements.local_rows:int- Number of locally owned rows.ghost_rows:int- Number of ghost rows cached locally.num_vectors:int- Number of columns.
duplicate(self) -> 'DistMultiVector'Returns a deep copy. copy() is a compatibility alias for duplicate().
to_numpy(self) -> np.ndarrayReturns locally owned part as 2D NumPy array.
from_numpy(self, arr: np.ndarray) -> NoneUpdates locally owned part from 2D NumPy array.
set_constant(self, val: Union[float, complex, int]) -> NoneSets all elements to val.
set_random_normal(self, normalize: bool = False) -> NoneFills the locally owned rows with random normal values.
scale(self, alpha: Union[float, complex, int]) -> NoneScales all elements in-place.
axpy(self, alpha: Scalar, x: 'DistMultiVector') -> NoneComputes
axpby(self, alpha: Scalar, x: 'DistMultiVector', beta: Scalar) -> NoneComputes
pointwise_mult(self, other: Union['DistMultiVector', DistVector]) -> NoneElement-wise multiplication. Supports broadcasting if other is a DistVector.
bdot(self, other: 'DistMultiVector') -> listComputes batch dot products (one for each column).
sync_ghosts(self) -> NoneSynchronizes ghost elements.
reduce_ghosts(self) -> NoneReduces accumulated ghost-row contributions back to their owners.
+,-: Addition/subtraction.*: Element-wise multiplication.+=,-=,*=: In-place operators.
Low-level distributed graph object exposed by vbcsr_core. This is the canonical ownership and ghost-layout description used by matrices, vectors, and atomic/image helpers.
construct_serial(self, global_blocks: int, block_sizes: list[int], adjacency: list[list[int]]) -> NoneCollectively builds the distributed graph from root-owned serial input. Root rank is fixed at rank 0.
construct_distributed(self, owned_indices: list[int], block_sizes: list[int], adjacency: list[list[int]]) -> NoneBuilds the graph from per-rank owned rows.
owned_global_indices: global block ids owned on this rank.ghost_global_indices: ghost block ids imported onto this rank.block_sizes: local block sizes for owned and ghost blocks.owned_scalar_rows: locally owned scalar row count.local_scalar_cols: local scalar column count including ghosts.global_scalar_rows: global scalar row count.rank/size: communicator rank and size.
get_local_index(self, gid: int) -> intReturns the local block index for a global block id, or -1 if that block is not present locally.
Distributed atomic topology and metadata used by ImageContainer and atom-centered workflows.
atom_types remains the internal type-id surface. atomic_numbers / z expose true atomic numbers.
@classmethod
from_points(cls, pos, z, cell, pbc, r_max, type_norb=1, comm=None) -> 'AtomicData'Builds atomic connectivity from positions and atomic numbers.
r_maxandtype_norbmay be scalars, vectors aligned to sorted unique atomic numbers, or dictionaries keyed by atomic number / chemical symbol.- If
commis provided, rank 0 may hold the full input and other ranks may provide empty arrays.
@classmethod
from_distributed(cls, n_atom, N_atom, atom_offset, n_edge, N_edge, atom_index, atom_type, edge_index, type_norb, edge_shift, cell, pos, atomic_numbers=None, comm=None) -> 'AtomicData'Builds AtomicData from prepartitioned distributed graph data.
atom_typesstay as type ids in this path.- Pass
atomic_numbers=to preserve authoritative atomic-number metadata. - If
atomic_numbersis omitted, accessingatomic_numbers,z, orto_ase()raises instead of guessing from type ids.
@classmethod
from_ase(cls, atoms, r_max, type_norb=1, comm=None) -> 'AtomicData'
@classmethod
from_file(cls, filename, r_max, type_norb=1, comm=None, format=None) -> 'AtomicData'Convenience constructors layered on top of from_points.
positions/pos: owned atomic positions as(n_atom, 3)NumPy arrays.atom_indices/indices: original atom ids.atom_types: internal type ids used for orbital lookups.atomic_numbers/z: true atomic numbers for owned atoms.cell:(3, 3)unit-cell matrix.pbc: periodic-boundary flags.edge_index: local edge list using local/ghost atom indices.edge_shift: lattice shifts for the local edge list.graph: backingDistGraph.
norb(self) -> intReturns the global orbital count implied by atom_types and type_norb.
to_ase(self)Exports owned atoms to an ASE Atoms object. This requires explicit atomic-number metadata.
Collects real-space image blocks keyed by lattice shifts and samples them into k-space matrices.
ImageContainer(atomic_data: AtomicData, dtype=np.float64)Creates a real or complex image container over the supplied AtomicData.
add_block(self, g_row, g_col, data, R=None, mode="add") -> NoneAdds one real-space image block.
add_blocks(self, g_rows, g_cols, data_list, R_list=None, mode="add") -> NoneAdds many image blocks in one call.
assemble(self) -> NoneFinalizes distributed image assembly.
sample_k(self, k_point, convention="R", symm=False) -> VBCSRSamples the assembled image blocks at a single k-point and returns a complex VBCSR.
convention="R"uses only lattice-vector phases.convention="R+tau"includes the basis-position phase term.