本期将介绍著名的有限元计算框架DOLFINx(FEniCSx), 并带领读者配置Python接口的DOLFINx运行环境。
默认您已满足以下要求
- 掌握python的基本语法
- 掌握conda的基本使用
- 拥有一台可以科学上网的计算机
- 了解linux, docker*
What's DOLFINx(FEniCSx)
FEniCS 是一个开源的数值计算框架,专注于通过有限元方法自动化求解偏微分方程(PDEs),广泛应用于工程、物理和数学建模领域。它提供直观的Python/C++接口,允许用户以接近数学符号的形式定义方程,并自动生成高效计算代码,显著简化复杂问题的仿真流程,支持从学术研究到工业应用的跨尺度求解需求。这里引用项目的一段原文:
DOLFINx is the next generation problem solving interface of the FEniCS Project. It is developed on GitHub.
帮助文档: 官方tutorial.
尽管互联网常常见到FEniCSx的配置教程,但准确来说,我们实际在配置新一代计算框架的DOLFINx. 后续我们将其统一称呼为DOLFINx.
Installation
V0.90官方文档:Installation — DOLFINx 0.9.0 documentation
安装方式分为Binaries(预编译源代码)和Source(手动编译源代码)两种安装过程,本文介绍不需要编译的conda/docker安装(更简单)。两种安装方式都在windows平台下进行,linux平台的安装同理。
Conda
难度:简单
常用命令:anaconda添加环境、镜像源、删除镜像源、退出环境、删除环境_
创建虚拟环境,并按你所使用的平台,安装对应的库,等待顺利安装即可~
conda create -n fenicsx-env conda activate fenicsx-env conda install -c conda-forge fenics-dolfinx mpich pyvista # Linux and macOS conda install -c conda-forge fenics-dolfinx pyvista pyamg # Windows
如果下载速度缓慢,可更换conda源
conda config --add channels https://mirrors.aliyun.com/pypi/simple/
Docker*
难度:简单
Docker安装教程可以参考:
⚠️注意⚠️
25年3月实测docker镜像被墙了,拼尽全力无法拉取镜像,请放弃这个安装方法...
使用Docker配置FEniCS可简化依赖管理,确保环境隔离与一致性,跨平台支持,快速部署,避免版本冲突,提升协作和复现效率。docker desktop客户端中搜索镜像(Images) dolfinx/lab:stable(支持notebook)或dolfinx/dolfinx:stable(原生环境) 点击下载。

在cmd中运行容器(Container), ps: 在docker桌面端直接点击创建可能会自动退出,导致无法使用容器
下面两个解决方案任选其一即可~
docker run -ti dolfinx/dolfinx:stable # 原生容器 docker run --init -ti -p 8888:8888 dolfinx/lab:stable # 带notebook的容器
输入第一条命令,会生成一个配置好dolfinx的环境.
我们在vscode中连接到对应容器即可,使用方法同ssh.
输入第二条命令,容器为我们提供了一个Jupyter Notebook,浏览器输入下面的地址即可访问
http://localhost:8888

注意不要关闭命令行窗口,这实际上是DOLFINx的后端,而浏览器的Jupyter Notebook是前端。
第二次进入时,我们可以在客户端,点击启动容器,即可访问。这次需要我们手动设置token,注意docker终端会给出token,复制到网页端即可实现密码登录。
(base) PS C:\Users\19408> docker start -i fbcdc65d45b77322ba1987e7d2358f3ef6c6a4eee8b822c767ebe3d8c13d6c21 ...... [I 2025-01-21 11:49:53.917 ServerApp] Jupyter Server 2.14.2 is running at: [I 2025-01-21 11:49:53.917 ServerApp] http://127.0.0.1:8888/lab?token=38749781ec5f13e06483aca656c9a9f31ed932b4d007df30 ......
常见问题解决
docker的命令行可以正确打印dolfinx的版本号,但使用vscode远程则无法打印.
原因是python解释器选择错误, 在container命令行查看python, 将其添加到vscode的python解释器即可~
# which python /dolfinx-env/bin/python
代码可以运行,但提示错误.
error: XDG_RUNTIME_DIR is invalid or not set in the environment. MESA: error: ZINK: failed to choose pdev glx: failed to create drisw screen
这是因为docker的环境比较简陋,没有图形化界面,所以缺了一些图形化相关的库,可以忽略它~
linux 平台
难度:简单
运行下面的命令,即可完成安装。
同理,也可以安装到WSL上,甚至不需要下载虚拟机,更加简单...(强烈推荐)
sudo add-apt-repository ppa:fenics-packages/fenics sudo apt update sudo apt install fenicsx
命令将 fenicsx 环境安装到本地的 python 环境,如果你无法接受本地环境被污染,请选择 conda 安装。
linux和conda可能会出现:"未安装PESTc"的报错,原因未知.
Test
若正常打印版本号,则配置成功(若版本为0.4.x则可能出现问题)。
import dolfinx print(f"DOLFINx version: {dolfinx.__version__} based on GIT commit: {dolfinx.git_commit_hash} of https://github.com/FEniCS/dolfinx/")
输出结果
yls@iZbp1fadzyhteqoygpjg37Z:~$ python3 test.py DOLFINx version: 0.9.0 based on GIT commit: ubuntu of https://github.com/FEniCS/dolfinx/
文章的最后附FEniCS参考书一本,点击链接下载即可~
注: 本书编写于2011年,使用旧框架的dolfin而非最新的dolfinx, 书中代码需修改后才能使用.
直接看demo真的是比较头大,一定要看看tutorial呀,不看真的要走很多弯路~
Python API Reference
dolfinx.common | General tools for timing and configuration. |
dolfinx.fem | Tools for assembling and manipulating finite element forms. |
dolfinx.fem.petsc | Assembly functions into PETSc objects for variational forms. |
dolfinx.geometry | Methods for geometric searches and operations. |
dolfinx.graph | Graph representations and operations on graphs. |
dolfinx.io | Tools for file input/output (IO). |
dolfinx.io.gmshio | Tools to extract data from Gmsh models. |
dolfinx.jit | Just-in-time (JIT) compilation using FFCx |
dolfinx.la | Linear algebra functionality |
dolfinx.mesh | Creation, refining and marking of meshes. |
dolfinx.nls.petsc | Methods for solving nonlinear equations using PETSc solvers. |
dolfinx.pkgconfig | Tool for querying pkg-config files. |
dolfinx.plot | Support functions for plotting |
Comments | NOTHING