lattice
Simple Lattice/Graph Library
Main functions
- Access to information of lattice structure (sites, bonds, etc)
- Construct lattice from unitcell, span vector of supercell, and boundary conditions
- Construct lattice by adding sites and bonds one by one
- Reading and writing ALPS Lattice XML file
Prerequisites
For Rust
- Rust toolchain (
rustc,cargo)
For C++
- C++-17 compiler
- CMake (>= 3.14)
- Eigen3
- Rust toolchain (
rustc,cargo) for auto-buildingrust/lattice-ffi
Note: C++ build may invoke cargo automatically to build rust/lattice-ffi when the shared library is missing.
Rust workspace
The repository is being extended with a Rust core under rust/ as the shared implementation base for future Python and Julia bindings.
C++ XML compatibility bridge (default)
Rust-backed XML implementation is now the default C++ XML backend.
When building C++ targets, CMake automatically builds rust/lattice-ffi with cargo if the required shared library is missing.
Rust targets are managed in the workspace under rust/:
lattice-core: core model + XML parser/writerlattice-ffi: C ABI layer for C++ compatibility
Build, test, and sample run
Rust
Build and run Rust tests:
cargo build
cargo test
Run Rust samples:
cargo run -p lattice-core --example construct1
cargo run -p lattice-core --example construct2
cargo run -p lattice-core --example construct3
cargo run -p lattice-core --example construct4
cargo run -p lattice-core --example construct_xml
cargo run -p lattice-core --example ising
C++ (default)
Configure and build:
Run C++ tests:
Enable tests at configure time, then run ctest:
cmake -S . -B build -DLATTICE_BUILD_TESTS=ON
cmake --build build
ctest --test-dir build --output-on-failure
Run C++ samples:
./build/example/construct1
./build/example/construct2
./build/example/construct3
./build/example/construct4
./build/example/construct_xml
./build/example/ising
Workaround for MacOSX26.sdk
Use CMake presets to pin the SDK to MacOSX15.4.sdk:
cmake --preset macos-sdk154
cmake --build --preset macos-sdk154
ctest --preset macos-sdk154
Using Installed Package
Install into a prefix:
CMake find_package(lattice)
Set CMAKE_PREFIX_PATH to the install prefix and use find_package:
cmake_minimum_required(VERSION 3.14)
project(lattice_consumer CXX)
find_package(lattice REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE lattice::lattice)
Configure example:
pkg-config
Set PKG_CONFIG_PATH and query compile/link flags:
Compile example:
Classes/types
-
lattice::basis
Helper class that contains the shape, i.e. the set of basis vectors, of the unit cell.
-
lattice::unitcell
Helper class that contains the structure, i.e. sites and bonds, of the unit cell.
-
lattice::graph
This class contains the structure of the whole lattice structure. It provides various information of sites (vertices) and bonds (edges) via the following member functions:
member functions description std::size_t dimension() const; dimension of the lattice std::size_t num_sites() const; total number of sites std::size_t site_type(std::size_t s) const; type of site s const coordinate_t& coordinate(std::size_t s) const; coordinate of site s std::size_t num_neighbors(std::size_t s) const; number of neighboring sites of site s std::size_t neighbor(std::size_t s, std::size_t k) const; k-th neighbor site of site s std::size_t neighbor_bond(std::size_t s, std::size_t k) const; bond connecting site s and its k-th neighbor site std::size_t num_bonds() const; total number of bonds int bond_type(std::size_t b) const; type of bond s std::size_t source(std::size_t b) const; start point (site) of bond b std::size_t target(std::size_t b) const; end point (site) of bond b
How to construct lattices
Rust
-
periodic chain lattice of 16 sites
-
simplest interface
use Graph; let graph = simple; -
most generic interface
use ; let basis = new; let mut unitcell = new; unitcell.add_site; unitcell.add_bond; let extent = from_element; let boundary = vec!; let graph = from_basis_unitcell_extent;
-
-
periodic square lattice of 4 x 4 sites
-
simplest interface
use Graph; let graph = simple; -
most generic interface
use ; let basis = new; let mut unitcell = new; unitcell.add_site; unitcell.add_bond; unitcell.add_bond; let extent = from_vec; let boundary = vec!; let graph = from_basis_unitcell_extent; -
reading basis and unitcell from XML file
use ; let file = "cxx/example/lattices.xml"; let basis = read_basis_from_file?; let cell = read_unitcell_from_file?; let extent = from_vec; let boundary = vec!; let graph = from_basis_unitcell_extent;
-
-
fully connected lattice of 10 sites
use Graph; let graph = fully_connected;
C++
-
periodic chain lattice of 16 sites
-
simplest interface
lattice::graph lat = ; -
most generic interface
lattice::basis_t ; bs << 1; // 1x1 matrix lattice::basis ; lattice::unitcell ; unitcell.; unitcell.; lattice::span_t ; span << 16; // 1x1 matrix std::vector<lattice::boundary_t> ; lattice::graph ;
-
-
periodic square lattice of 4 x 4 sites
-
simplest interface
lattice::graph lat = ; -
most generic interface
lattice::basis_t ; bs << 1, 0, 0, 1; // 2x2 matrix lattice::basis ; lattice::unitcell ; unitcell.; unitcell.; unitcell.; lattice::span_t ; span << 4, 0, 0, 4; // 2x2 matrix std::vector<lattice::boundary_t> ; lattice::graph ; -
reading basis and unitcell from XML file
std::string file = "lattices.xml"; lattice::basis bs; ; lattice::unitcell cell; ; lattice::graph ; ``` -
-
fully connected lattice of 10 sites
lattice::graph lat = ;