Skip to main content

seismokit

地震工程 Python 工具库,支持 Python 3.11 及以上。目前提供地震动弹性反应谱计算。

安装与使用

发布到 PyPI 后,在 Python 3.11 或以上的环境中安装:

python -m pip install seismokit
# uv 管理的项目也可以使用:
uv add seismokit

升级已安装的版本:python -m pip install --upgrade seismokit。Windows x64 用户可直接安装已构建的 wheel,无需 C 编译器;其他平台若没有对应 wheel,会从源码编译,需要先安装 C 编译器。

尚未发布时,可从项目根目录安装本机 wheel:

python -m pip install .\dist\seismokit-0.1.0-cp311-abi3-win_amd64.whl

下面是完整使用示例:

import numpy as np
from seismokit import spectrum

dt = 0.005
time = np.arange(0.0, 20.0, dt)
ag = np.sin(2 * np.pi * time)  # 示例加速度,单位 m/s²
T = np.r_[0.0, np.linspace(0.1, 5.0, 100)]

PSa, PSv, Sd = spectrum(ag, dt, T)  # 默认 zeta=0.05, algorithm="NJ"
PSa_nm, PSv_nm, Sd_nm = spectrum(ag, dt, T, algorithm="NM")

print("伪加速度谱 (m/s²):", PSa)
print("伪速度谱 (m/s):", PSv)
print("相对位移谱 (m):", Sd)

对于自己的数据,可用 ag = np.loadtxt("acceleration.txt") 读取单列加速度记录,并把 dt 设置为记录的实际采样间隔。如果记录单位为 g,先执行 ag = ag * 9.80665 转换为 m/s²。

计算约定

求解单位质量单自由度体系的相对运动方程:

u'' + 2 ζ ω u' + ω² u = -ag(t),    ω = 2π/T
u(0) = u'(0) = 0
Sd = max |u|,    PSv = ω Sd,    PSa = ω² Sd
  • NJ:Nigam–Jennings 法对应的分段线性输入精确状态递推。采用缩放平方法计算状态转移及荷载系数,避免长周期时相近数相减的数值消减。
  • NM:Newmark 平均加速度法,固定 β=1/4, γ=1/2,初始相对加速度为 -ag[0]。
  • ag 和 T 为一维有限实数序列,ag 至少两个采样点,dt>0、zeta>=0;支持无阻尼、临界及过阻尼。
  • 返回三个与 T 形状一致的 float64 数组,保留周期顺序和重复值,支持空周期序列。
  • T=0 采用刚性极限:PSa=PGA=max(abs(ag))、PSv=Sd=0。负周期报错。
  • 使用一致单位。dt 和 T 以 s 计、ag 以 m/s² 计时,输出依次为 m/s²、m/s、m。若记录以 g 为单位,应先乘以重力加速度(如 9.80665)再计算以获得上述 SI 单位。
  • 峰值取输入采样时刻,只计算所给记录的持续时间,不自动补零或细分时间步。若需计入记录结束后的自由振动,应自行追加零加速度段。
  • 建议 dt <= min(T[T > 0])/20,并通过减小步长检查收敛。NJ 的“精确”指线性输入假设下的采样时刻递推,采样点间的峰值仍可能遗漏;NM 短周期误差尤其需要关注。
  • 返回伪加速度谱、伪速度谱,并非绝对加速度峰值谱或相对速度峰值谱。

开发与编译

安装 uv 后,在项目根目录执行:

uv sync
uv run pytest -q
uv run python scripts/build.py

uv sync 安装开发依赖并编译可编辑安装。构建脚本生成 dist/ 下的源码包和当前平台的 wheel,例如:

dist/seismokit-0.1.0.tar.gz
dist/seismokit-0.1.0-cp311-abi3-win_amd64.whl

仅在源码目录编译 .pyd(Linux/macOS 对应 .so):

uv run python scripts/build.py --inplace

Windows 需要 Visual Studio / Build Tools 的“使用 C++ 的桌面开发”和 Windows SDK;Linux/macOS 需要对应 C 编译器。用户安装已有 wheel 时不需要编译器或 Cython。运行时只依赖 NumPy,SciPy 仅用于独立校验测试;未编译的源码提供 Python 回退实现,正式 wheel 测试会确认原生扩展可用。

Python 与二进制兼容

Cython 核心使用 PEP 3118 缓冲区读取 NumPy 数据,不依赖 NumPy C API;通过 Py_LIMITED_API=0x030B0000 和 py_limited_api=cp311 构建 CPython 稳定 ABI。

因此,同一操作系统、同一架构上的一个 cp311-abi3 wheel / .pyd 可供常规 CPython 3.11 及后续版本使用,不必为 3.12、3.13、3.14 重编译。稳定 ABI 不代表跨操作系统或 CPU 架构通用,也不适用于 PyPy 或自由线程 CPython(3.13t 等)。未来 Python 版本仍应加入兼容性测试。

对同一个 wheel 做 ABI 符号审计,并分别在 Python 3.11–3.14 的独立环境中运行完整测试:

uv run python scripts/check_wheel.py dist/seismokit-0.1.0-cp311-abi3-win_amd64.whl

本机需安装对应 Python;uv 也可按配置自动下载解释器。3.11 环境额外使用 NumPy 1.24.0 验证声明的最低 NumPy 版本。测试覆盖解析解、SciPy 状态空间独立解、独立 Newmark 实现、Python/Cython 一致性、边界输入及异常。

仓库包含 GitHub Actions:构建 Windows、Linux、macOS 的 wheel,并在 Windows 上用同一 wheel 测试四个 Python 版本。

双击发布到 PyPI

在 Windows 中双击项目根目录的 publish_pypi.bat。脚本会定位项目目录、安装 uv 开发环境依赖、重新构建、进行 ABI 审计、在隔离环境安装本次 wheel 并运行测试,通过后上传到正式 PyPI。运行结束会保留窗口,便于查看结果。

Token 按 UV_PUBLISH_TOKEN → PYPI_TOKEN → TWINE_PASSWORD 的顺序从环境变量读取。只需已有一个变量包含有效的 PyPI API token;不会把 token 写入脚本或作为命令行参数传递。Token 需拥有该项目的发布权限;第一次创建项目时,应使用允许创建项目的 token。

首次发布使用 pyproject.toml 中当前的版本 0.1.0。后续发布先修改 [project].version(例如 0.1.1),再双击脚本;uv 会自动同步锁文件。PyPI 版本文件不能被覆盖,且包名必须可用或属于自己的账号。

脚本只上传本次生成的当前平台 wheel 和源码包,产物会保存到 dist/,不会把目录中的旧版本一起上传。其他系统的 wheel 可通过 GitHub Actions 构建后另行发布。上传成功后页面为 https://pypi.org/project/seismokit/。

只演练构建和验证、不上传:

.\publish_pypi.bat --dry-run
# 或者不打开暂停窗口:
uv run python scripts/publish.py --dry-run

需要已安装 uv,以及前文所述的 C 编译工具链。若刚设置了环境变量,重新打开终端或应用,确保新进程能读取该变量。

代码位置

  • src/seismokit/_spectrum.py:公开接口、校验与两种算法的递推系数。
  • src/seismokit/_kernel_python.py:Python 递推循环及回退实现。
  • src/seismokit/_kernel.pyx:Cython 递推循环,释放 GIL。
  • setup.py / pyproject.toml:稳定 ABI 编译与打包配置。
  • scripts/build.py / scripts/check_wheel.py:构建和多版本兼容校验。
  • publish_pypi.bat / scripts/publish.py:双击构建、验证并上传到正式 PyPI。

Metadata

Release files for seismokit 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for seismokit 0.1.0
File Size Uploaded Provenance Actions
seismokit-0.1.0.tar.gz 174.6 kB Without provenance Details for seismokit-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for seismokit 0.1.0
File Interpreter ABI Platform Actions
seismokit-0.1.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for seismokit-0.1.0-cp311-abi3-win_amd64.whl

Total release size: 246.5 kB

Release files / seismokit-0.1.0.tar.gz

Download URL seismokit-0.1.0.tar.gz
Size 174.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ae7c0fef8706e0afe2869f41dda5dee2d3767a2e3a7e3f392435cb6d2f14e530
BLAKE2b-256 checksum
How to use checksums
c5fe0a86b5d3efe3f424b409e592b1de5da2d110f914afbab7811abc37b057f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / seismokit-0.1.0-cp311-abi3-win_amd64.whl

Download URL seismokit-0.1.0-cp311-abi3-win_amd64.whl
Size 72.0 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
92470e75e16aa0c92c20f081d5613cbeb2ea945260f385f9a45c75dafbf6e163
BLAKE2b-256 checksum
How to use checksums
56d3fd1c347b84d656f55bb8583c61851eca4e97382f1123f6650b5df72f08eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for seismokit 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page