安装¶
TensorMesh 可在 Linux、macOS 和 Windows 上运行。唯一的硬性要求是 Python ≥ 3.10 和 PyTorch ≥ 2.0;其余所有依赖(NumPy、SciPy、meshio、torch-sla 等)都会由 pip 自动安装。
如果你打算使用 GPU 后端,请在安装 TensorMesh 之前 先安装启用了 CUDA 的 PyTorch 版本——请按照官方的 PyTorch 安装选择器,针对你的平台/CUDA 版本获取正确的安装命令。
通过 PyPI 安装¶
推荐通过 PyPI 来安装 TensorMesh:
pip install tensormesh-fem # CPU only
pip install "tensormesh-fem[gpu]" # + CUDA sparse solvers (CuPy + cuDSS)
如果你拥有 NVIDIA GPU 并想使用 CUDA 稀疏求解器后端,请使用第二种形式——[gpu] 这个 extra 会通过 torch-sla 同时安装 CuPy 和 cuDSS。这里需要加引号,因为 [...] 是 shell 的通配符字符;如果你只想要 CuPy 或 cuDSS 中的一个,请参阅 Sparse solvers and GPU acceleration 了解各后端的细分说明。
Either form pulls in all required dependencies, including
torch-sla, the differentiable sparse linear
algebra library that powers TensorMesh's solvers. The base
pip install tensormesh-fem installs only the CPU sparse stack
(SciPy / native PyTorch); see Sparse solvers and GPU acceleration
below for the full extras matrix ([cupy] / [cudss] / [gpu]) and
how to verify which backends are usable on your machine.
从源码安装¶
如需进行开发工作,或获取尚未发布的最新改动,请克隆仓库并以可编辑模式安装:
git clone https://github.com/camlab-ethz/TensorMesh.git
cd TensorMesh
pip install -e .
-e(可编辑)标志意味着对源码树的修改会被自动识别,无需重新安装。若要同时安装测试依赖:
pip install -e ".[test]"
稀疏求解器与 GPU 加速¶
为 TensorMesh 求解器提供动力的稀疏线性代数层位于一个独立的库 torch-sla 中,这样它便能独立演进并服务于其他项目。torch-sla 是一个 硬性的、导入时即需满足 的依赖——缺少它 tensormesh.sparse 将无法导入——并且它是 CPU 和 GPU 稀疏求解的统一入口。当前和未来的所有求解器工作都会先落地到 torch-sla;我们建议保持其为最新版本。
基础的 tensormesh-fem wheel 只会安装 CPU 栈(SciPy / 原生 PyTorch)。若要启用 GPU 后端,请安装其中一个镜像的 extra:
安装命令 |
新增的后端 |
适用场景 |
|---|---|---|
|
仅 CPU |
默认;不进行 GPU 稀疏求解。 |
|
CuPy(CUDA) |
迭代式 GPU 求解器(CG / GMRES / …)+ CuPy SuperLU。 |
|
cuDSS(CUDA) |
最快的 GPU 直接求解器(LU / Cholesky / LDLT)。 |
|
两者皆有 |
便捷 extra——安装 |
这些 extra 镜像了上游 torch-sla 的 extra([cupy] / [cudss] / [all])——安装 tensormesh-fem[gpu] 与 pip install tensormesh-fem torch-sla[all] 完全等价,只是将其写成了一步完成。
查看已安装的内容¶
安装完成后,你可以查看当前机器上有哪些后端可用——对于尚不可用的后端,还会给出一行安装提示:
>>> import torch_sla
>>> torch_sla.show_backends()
torch-sla backend status (CUDA: available)
scipy [CPU] available
eigen [CPU] not available — JIT-compiled C++ extension (requires a C++ compiler)
pytorch [CPU/CUDA] available
cupy [CUDA] not available — pip install torch-sla[cupy]
cudss [CUDA] not available — pip install torch-sla[cudss]
完整的后端/方法矩阵,以及如何在求解时选择非默认后端,请参阅 稀疏求解器。
其他 extras¶
另有几个用于可选功能的较小 extras:
Extra |
安装命令 |
|---|---|
用于示例 notebook 的 Plotly |
|
测试套件(pytest、pytest-cov) |
|
另有两个包通常也很有用,但 未 被声明为 extras——需要时请直接安装它们:
pip install gmsh # external mesh generation / .msh I/O
pip install pyvista # interactive 3D visualization