moonbit-granular

    Discrete Element Method (DEM) library for granular media in MoonBit focusing on particle mechanics, contact models, and micro-macro statistics.

    dem
    granular
    physics
    discrete-element-method
    simulation
    hertz-contact
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    2 months ago
    Downloads
    31

    No dependencies

    #moonbit-granular

    MoonBit License CI

    moonbit-granular 是一个用 MoonBit 原生实现的三维颗粒介质离散元(Discrete Element Method, DEM)库。它面向土壤、砂石、矿粉、粮食和药品颗粒等散体材料,提供接触力学、边界条件、时间积分、邻域搜索、微宏观统计和可复现实验诊断。

    #适用场景

    • 颗粒堆积、沉积和自由落料的快速原型
    • 直剪、三轴、漏斗下料、粮仓和旋转鼓等实验场景
    • 接触力、力链、织构、应力张量和孔隙率的后处理
    • 用确定性随机数、边界测试和 native 基准构建回归实验

    #特性

    • Hertz–Mindlin、Hooke 粘弹性、JKR 黏附和 Parallel Bond 接触模型
    • Velocity–Verlet 积分、Rayleigh 稳定步长和转动四元数
    • 三维均匀空间哈希网格与 Verlet 邻域表
    • 平面墙、圆柱墙、周期边界、Lees–Edwards 剪切和三角网格边界
    • PSD 级配、Multi-Sphere Clump 异形颗粒和多种应用场景
    • Love–Weber 应力、Fabric 织构、力链、配位数、堆积密度和孔隙比
    • 能量/动量快照、几何质量报告、有限时间序列和确定性分析摘要

    #项目结构

    src/ ├── math/ 向量、矩阵、四元数、随机数与应力数学工具 ├── particle/ 球形颗粒、材料属性、PSD 与多球团簇 ├── contact/ Hertz–Mindlin、Hooke、JKR、Parallel Bond ├── boundary/ 平面/圆柱/周期/网格边界与伺服控制 ├── spatial/ 空间哈希网格与 Verlet 邻域表 ├── integrator/ Velocity–Verlet 与自适应子步 ├── simulation/ 仿真世界、接触推进和轨迹导出 ├── stats/ 微宏观应力、织构、力链、配位数和堆积统计 ├── analysis/ 能量、质量、时间序列和摘要报告 ├── app/ 直剪、漏斗、三轴、粮仓和旋转鼓案例 ├── bench/ 确定性算法基准库 └── main/ 可运行的 CLI 演示 cmd/benchmark/ native 基准入口 benchmarks/ 本机基准证据 scripts/ 本地复现脚本

    #快速开始

    需要 MoonBit stable 工具链。仓库当前本地验证使用 moon 0.1.20260819,CI 使用官方 stable 安装脚本获取最新稳定版本。

    moon version --all moon update moon check --target all moon test --target all moon run --target native src/main

    运行 native 算法基准并生成本地报告:

    ./scripts/benchmark.ps1 -Runs 5 ./scripts/benchmark.ps1 -Runs 5 -OutputPath benchmarks/latest-native.md

    固定 workload 的参考结果见 benchmarks/2026-08-24-native.md。墙钟时间会受操作系统、编译缓存和机器负载影响;checksum 与操作数用于确认计算仍然执行。

    #API 示例

    import {
    "ssqzerz/moonbit-granular/src/analysis"
    "ssqzerz/moonbit-granular/src/math"
    "ssqzerz/moonbit-granular/src/particle"
    "ssqzerz/moonbit-granular/src/simulation"
    }

    let world = @simulation.SimulationWorld::new(
    0.02,
    512,
    0.005,
    0.0001,
    @math.Vec3::new(0.0, -9.81, 0.0),
    )
    let particle = @particle.Particle3D::new(
    1,
    @math.Vec3::new(0.0, 0.01, 0.0),
    0.005,
    2650.0,
    0,
    )
    world.add_particle(particle)
    for _step = 0; _step < 100; _step = _step + 1 {
    world.step()
    }

    let report = @analysis.AnalysisReport::from(
    world.particles(),
    @math.Vec3::new(-0.1, -0.1, -0.1),
    @math.Vec3::new(0.1, 0.1, 0.1),
    )
    println(report.to_string())

    #验证与质量保证

    本地验证入口:

    moon fmt moon check --deny-warn moon test --deny-warn moon test --target all moon info

    测试覆盖数学、接触、边界、空间邻域、积分、仿真、统计、分析和基准包,并包含空输入、退化域、负坐标、周期边界临界点、无接触世界及固定容量时间序列等边界场景。

    GitHub Actions 在 Ubuntu、macOS 和 Windows 上执行 stable 工具链安装、依赖更新、全目标检查与测试、格式检查和公开接口漂移检查。

    #兼容性与发布

    • 主要实现语言:MoonBit
    • 推荐目标:wasm、wasm-gc、js、native
    • 不依赖第三方运行时或 FFI
    • 模块元数据与发布信息位于 moon.mod

    发布到 Mooncakes 时,请先在目标环境执行完整验证,再按 MoonBit 工具链文档完成登录、版本标记和发布流程。

    #许可证

    本项目以 Apache License 2.0 发布。