Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/tests/uniform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pyfmm
import numpy as np

xarr = np.arange(0, 100, 0.08)
yarr = np.arange(0, 50, 0.05)
zarr = np.array([0.0]) # 二维情况


# 慢度场
slw = np.ones((len(xarr), len(yarr), len(zarr)), dtype='f')
print(slw.shape)

srcloc = [10, 20, 0.0]

# FMM解
FMMTT = pyfmm.travel_time_source(
srcloc,
xarr, yarr, zarr, slw)

# FSM解
FSMTT = pyfmm.travel_time_source(
srcloc,
xarr, yarr, zarr, slw, useFSM=True, FSMparallel=True)

# 真实解
xx, yy, zz = srcloc
real_TT = np.sqrt(((xarr-xx)**2)[:,None,None] + ((yarr-yy)**2)[None,:,None] + ((zarr-zz)**2)[None,None,:])

# 误差
FMM_error = np.mean(np.abs(FMMTT - real_TT))
FSM_error = np.mean(np.abs(FSMTT - real_TT))
print("FMM_error = ", FMM_error)
print("FSM_error = ", FSM_error)

tol = 0.1

if FMM_error > tol:
raise ValueError(f"FMM_error({FMM_error}) > tol({tol})")
if FSM_error > tol:
raise ValueError(f"FMM_error({FSM_error}) > tol({tol})")
329 changes: 329 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,329 @@
name: Build C programs and libraries, publish to PyPI and create a release

on:
push:
tags:
- "v*.*.*"

jobs:
build: # 编译C库
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: windows-2022
arch: x86_64 # windows x86_6
- os: ubuntu-22.04
arch: x86_64 # Ubuntu x86_64
- os: macos-13
arch: x86_64 # macOS Intel
- os: macos-14
arch: arm64 # macOS Apple Silicon

fail-fast: true

defaults:
run:
shell: bash

steps:
- name: Set MSYS2 (Windows) # windows平台使用MSYS2工具编译程序
if: contains(matrix.os, 'windows')
uses: msys2/setup-msys2@v2
with:
msystem: UCRT64
install: >-
git
mingw-w64-ucrt-x86_64-gcc
make

- name: Configured git attributes for line endings (Windows) # 防止接下来在windows平台checkout代码时,文本文件的换行符发生变化,导致MSYS2工作出错
if: contains(matrix.os, 'windows')
run: git config --global core.autocrlf input

- name: Checkout code # 下载库代码
uses: actions/checkout@v3
with:
fetch-depth: 1

# --------------------- 检查文件中的版本名是否和tag名相符 --------------------
- name: Check version
run: |
TAG_NAME=${{ github.ref_name }}
PYVERSION_FILE="pyfmm/_version.py"

# 提取 Python 版本号
PYTHON_VERSION=$(sed -n 's/^__version__ = "\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)"/\1/p' ${PYVERSION_FILE})
if [ -z "$PYTHON_VERSION" ]; then
echo "Error: Failed to extract version from ${PYVERSION_FILE}"
exit 1
fi

# 检查 Python 版本与 Tag 是否匹配
if [ "$PYTHON_VERSION" != "${TAG_NAME#v}" ]; then
echo "Error: Version mismatch between tag (${TAG_NAME}) and ${PYVERSION_FILE} (${PYTHON_VERSION})"
exit 1
fi

echo "Version check passed:"
echo " Tag: ${TAG_NAME}"
echo " Python version: ${PYTHON_VERSION}"

# --------------------------- 安装依赖 ------------------------------------------
# - name: Install dependencies (Ubuntu)
# if: contains(matrix.os, 'ubuntu')
# run: |
# sudo apt update
# sudo apt install -y build-essential

- name: Install dependencies (macOS)
# 匹配包含 "macos" 的操作系统
if: contains(matrix.os, 'macos')
run: |
brew install libomp

# ----------------- 编译C库和C程序 ----------------------------------
- name: Build the project (macOS)
if: contains(matrix.os, 'macos')
working-directory: ./pyfmm/C_extension
run: |
make ARCH="-arch ${{ matrix.arch }}" \
CC=gcc-14

make cleanbuild
otool -L lib/*

- name: Build the project (Ubuntu)
if: contains(matrix.os, 'ubuntu')
working-directory: ./pyfmm/C_extension
run: |
make
make cleanbuild
ldd lib/*

- name: Build the project (Windows)
if: contains(matrix.os, 'windows')
shell: msys2 {0}
working-directory: ./pyfmm/C_extension
run: |
make
make cleanbuild
ldd lib/*


# ------------------------ 定义接下来打包程序命名时的系统名后缀 ---------------
- name: Define the package OS suffix
run: |
# 符合pypi命名规范,否则上传失败
if [[ "${{ matrix.os }}" == *"ubuntu"* ]]; then
SUFFIX_PLAT_NAME="manylinux2014_x86_64"
elif [[ "${{ matrix.os }}" == *"macos"* && "${{ matrix.arch }}" == *"x86_64"* ]]; then
SUFFIX_PLAT_NAME="macosx_10_9_x86_64"
elif [[ "${{ matrix.os }}" == *"macos"* && "${{ matrix.arch }}" == *"arm64"* ]]; then
SUFFIX_PLAT_NAME="macosx_11_0_arm64"
elif [[ "${{ matrix.os }}" == *"windows"* ]]; then
SUFFIX_PLAT_NAME="win_amd64"
else
echo " Unsupported OS: ${{ matrix.os }} (${{ matrix.arch }})"
exit 1
fi

echo "SUFFIX_PLAT_NAME=$SUFFIX_PLAT_NAME" >> $GITHUB_ENV

# --------------------------- 打包整个程序 ---------------------
- name: Package the binary
run: |
PACK_NAME=pyfmm_kit-${{ github.ref_name }}-${{ env.SUFFIX_PLAT_NAME }}
echo "PACK_NAME=$PACK_NAME" >> $GITHUB_ENV
FILE_CONTENT=$(ls -A)
mkdir -p $PACK_NAME
cp -r ${FILE_CONTENT} $PACK_NAME/
tar -czvf $PACK_NAME.tar.gz $PACK_NAME
rm -rf $PACK_NAME

# -------------------- upload artifacts -----------------------
- name: Upload artifact (*.tar.gz)
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.os }}-${{ matrix.arch }}_tar
path: ${{ env.PACK_NAME }}.tar.gz


# =======================================================================================
test_project: # 在全新系统上测试程序,不安装其它依赖,看能否运行
runs-on: ${{ matrix.os }}
needs: build
strategy:
matrix:
include:
- os: windows-2022
arch: x86_64 # windows x86_6
- os: ubuntu-22.04
arch: x86_64 # Ubuntu x86_64
- os: macos-13
arch: x86_64 # macOS Intel
- os: macos-14
arch: arm64 # macOS Apple Silicon

fail-fast: true

defaults:
run:
shell: bash

steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: ${{ matrix.os }}-${{ matrix.arch }}_tar
path: artifacts

- name: Display structure of downloaded files, and Uncompress
run: |
ls -R artifacts
echo "------------------- tar output -----------------------------"
tar -xzvf artifacts/*.tar.gz
echo "------------------------------------------------------------"

# 获得压缩包解压出来的文件夹名
PACK_NAME=$(ls | grep pyfmm_kit)
echo "PACK_NAME=$PACK_NAME" >> $GITHUB_ENV

# 从解压出的文件夹命名来推断${{ env.SUFFIX_PLAT_NAME }}
SUFFIX_PLAT_NAME=$(echo $PACK_NAME | sed 's/.*-\(.*\)/\1/')
echo "SUFFIX_PLAT_NAME=$SUFFIX_PLAT_NAME" >> $GITHUB_ENV

echo $PACK_NAME
echo $SUFFIX_PLAT_NAME

# --------------------------- 安装依赖 ------------------------------------------
# 实际使用时可能需要安装libomp
# - name: Install libomp (Ubuntu)
# if: contains(matrix.os, 'ubuntu')
# run: |
# sudo apt install -y libomp-dev

# - name: Set alias (MacOS)
# if: contains(matrix.os, 'macos')
# run: |
# brew install coreutils
# echo "alias timeout=gtimeout" >> ~/.bashrc

# --------------------搭建python环境,开始测试,并制作wheel文件 ------------------------------
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Install dependencies
working-directory: ${{ env.PACK_NAME }}
run: |
python -m pip install --upgrade pip
pip install --upgrade setuptools wheel build
pip install -v .

- name: Clean up build and egg-info directories
working-directory: ${{ env.PACK_NAME }}
run: |
# 清理临时文件
rm -rf build/
rm -rf pyfmm_kit.egg-info/

- name: Test
working-directory: ${{ env.PACK_NAME }}/.github/tests
run: |
python uniform.py

# --------------------------- 制作wheels ---------------------
- name: Build the Python Wheel
working-directory: ${{ env.PACK_NAME }}
run: |
python setup.py bdist_wheel --plat-name=${{ env.SUFFIX_PLAT_NAME }} # 只制作wheel,这里不打包源码

- name: Upload artifact (*.whl)
uses: actions/upload-artifact@v4
with:
name: ${{ env.PACK_NAME }}_whl
path: ${{ env.PACK_NAME }}/dist/*.whl


# ===============================================================================
publish_pypi: # 上传pypi
runs-on: ubuntu-latest
needs: test_project
permissions:
id-token: write
contents: read
defaults:
run:
shell: bash


steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
pattern: "*_whl"
path: artifacts

- name: Display structure of downloaded files
run: ls -R artifacts

- name: Move wheel files to dist/
run: |
mkdir -p dist
mv artifacts/*/*.whl dist/

- name: Pypi Publish
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/


# ===============================================================================
release: # 创建Release
runs-on: ubuntu-latest
needs: build # test_project
permissions:
contents: write

defaults:
run:
shell: bash

steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
pattern: "*_tar"

- name: Display structure of downloaded files
run: ls -R artifacts

# 对artifacts中的每个tar.gz文件,根据其内部的文件夹名重新命名压缩包
- name: Rename tar.gz files
run: |
mkdir -p release
for file in artifacts/*/*.tar.gz; do
TEMP_FILE=tmp
# 这里不能把tar合并到下方的管道中,在github actions中会报错
tar -tzf $file > $TEMP_FILE
PACK_NAME=$(head -n 1 $TEMP_FILE | cut -f 1 -d '/')
mv $file release/${PACK_NAME}.tar.gz
rm $TEMP_FILE
done

ls -R release

- name: Create Release
id: create_release
uses: softprops/action-gh-release@v1
with:
draft: true
files: "release/*.tar.gz"





Loading