Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
18569155bc | ||
|
|
5db21aba7b | ||
|
|
2456912850 | ||
|
|
700a7e3d10 | ||
|
|
f297ffca3a | ||
|
|
3f0cae70a2 | ||
|
|
e9ef7f9d4d | ||
|
|
9c1c8f75c2 | ||
|
|
20281e5959 | ||
|
|
030799dfc8 | ||
|
|
b62be1ada6 | ||
|
|
c611080182 | ||
|
|
4d0790565c | ||
|
|
950d86c54e | ||
|
|
e5ac6d35ce | ||
|
|
85b117ec7c | ||
|
|
5a4c501524 | ||
|
|
effb5a7a14 | ||
|
|
54e367970f | ||
|
|
7cd3dcfaa0 | ||
|
|
a2e747b9ba | ||
|
|
09385f069f | ||
|
|
6c79498005 | ||
|
|
e8e3fc647c | ||
|
|
59a02b0172 | ||
|
|
a5c71bc5b0 | ||
|
|
cb4141d0f0 | ||
|
|
bb3def1cf5 | ||
|
|
72ebd5bc7f | ||
|
|
68e9bcde92 | ||
|
|
3da332be77 | ||
|
|
56553909f1 | ||
|
|
cd6c53adbb | ||
|
|
4908f5679c | ||
|
|
6f94346253 | ||
|
|
7f2d4cb6e1 | ||
|
|
b3a7f12472 | ||
|
|
3a5a8213a1 | ||
|
|
4e6fd59409 | ||
|
|
12b3128f53 | ||
|
|
8cd7b43c35 | ||
|
|
793fbe3b71 | ||
|
|
db0f0567a0 | ||
|
|
fea61fb180 | ||
|
|
58174ba168 | ||
|
|
ce02d263c7 | ||
|
|
22d709e604 | ||
|
|
b126c2293b | ||
|
|
f38a3be917 | ||
|
|
cbac4c7e27 | ||
|
|
38678253fa | ||
|
|
b88a8b7093 | ||
|
|
e4233ff794 | ||
|
|
7b7d38fabd | ||
|
|
6f07e4c09c | ||
|
|
e887e3a8e5 | ||
|
|
d5fb100182 | ||
|
|
937e557e5a | ||
|
|
18dbc29357 | ||
|
|
fcc068bce9 | ||
|
|
7c3107dec0 | ||
|
|
992bc71b6b | ||
|
|
a226b7dcac | ||
|
|
ac1037e1af | ||
|
|
a079e51f9d | ||
|
|
b74d15e44d | ||
|
|
7d269c58f9 | ||
|
|
3ee622f62a | ||
|
|
4bb2134b47 | ||
|
|
655a60c138 | ||
|
|
3ee542453e | ||
|
|
78b8fc8d6c | ||
|
|
6136770c1c | ||
|
|
b655af305a | ||
|
|
7f0af28e9c | ||
|
|
7714afbced | ||
|
|
eae709b3c0 | ||
|
|
80da2d968b | ||
|
|
c6bc4d2ba0 | ||
|
|
b905f1bc89 | ||
|
|
4ad793ca5e | ||
|
|
c160712343 | ||
|
|
19c0538dd8 | ||
|
|
1f183e04ea | ||
|
|
4fa6292749 | ||
|
|
927accce45 | ||
|
|
4a8d34d619 | ||
|
|
9a8469cf4b | ||
|
|
cbd60a7985 | ||
|
|
47e92eebb1 | ||
|
|
bf61ae8406 | ||
|
|
4d6d69ee6d | ||
|
|
e79ba866a1 | ||
|
|
f8b92fcd89 | ||
|
|
9b705f5334 | ||
|
|
98e96921e6 | ||
|
|
d17cf414f7 | ||
|
|
e4d1f6c7da | ||
|
|
b18195ff89 | ||
|
|
61f0d42ec4 | ||
|
|
1c18a463e7 | ||
|
|
af9b4bb222 | ||
|
|
ed25de6521 | ||
|
|
ead489592c | ||
|
|
7a5363e07b | ||
|
|
9a30c7d9ed | ||
|
|
0b7dda95f7 | ||
|
|
02b408c499 | ||
|
|
3d38753cad | ||
|
|
1af87f1f98 | ||
|
|
e9801e4528 | ||
|
|
75b8c7e26e | ||
|
|
33d69fc53d | ||
|
|
44de124d9d | ||
|
|
5cee94f947 | ||
|
|
06c4a8d73d | ||
|
|
f9a0282acc | ||
|
|
439dfc678f | ||
|
|
68016f69f7 | ||
|
|
92d49debec | ||
|
|
1d71a9f8af | ||
|
|
87710a83ab | ||
|
|
eecdcb5b96 | ||
|
|
464417d84d | ||
|
|
c107ea8105 | ||
|
|
390ddcd6cc | ||
|
|
c9163e98ad | ||
|
|
fee978c7e4 | ||
|
|
248e74ca9e | ||
|
|
575ae10dbf | ||
|
|
58d3b351ff | ||
|
|
5306450413 | ||
|
|
d29334a7a7 | ||
|
|
361ac2e7b3 | ||
|
|
febd40fadc | ||
|
|
4dadaa50aa | ||
|
|
e468133f9a | ||
|
|
c82c13019a | ||
|
|
d703cdde0b | ||
|
|
c077131285 | ||
|
|
4ac31d3988 | ||
|
|
600b2ff633 | ||
|
|
d6b336538a | ||
|
|
efe90ec50e | ||
|
|
8dd7446569 | ||
|
|
4cfb6dd3de | ||
|
|
d3ac6395bb | ||
|
|
62fc00755a | ||
|
|
d781d6e427 | ||
|
|
6efad8feeb | ||
|
|
8092c39892 | ||
|
|
4610876384 | ||
|
|
2be6aff2f9 | ||
|
|
991dd354cb |
No files matched your search
@@ -1,13 +1,14 @@
|
||||
FieldCraft is open-source CAE pre-processing in Rust: it turns geometry into analysis-ready
|
||||
finite element models, writes Abaqus, Nastran and LS-DYNA decks, and solves linear static and
|
||||
modal problems with its own solver. AI agents can drive it over MCP (`fieldcraft-cli mcp`).
|
||||
[README](https://github.com/jyviko/fieldcraft#readme) ·
|
||||
[roadmap](https://github.com/jyviko/fieldcraft/blob/main/ROADMAP.md) ·
|
||||
[issues](https://github.com/jyviko/fieldcraft/issues)
|
||||
[README](https://github.com/tangency-labs/fieldcraft#readme) ·
|
||||
[roadmap](https://github.com/tangency-labs/fieldcraft/blob/main/ROADMAP.md) ·
|
||||
[issues](https://github.com/tangency-labs/fieldcraft/issues)
|
||||
|
||||
**Which file?**
|
||||
- **macOS 11+** (Apple silicon and Intel): open `fieldcraft-{version}-macos-universal.dmg` and
|
||||
drag `FieldCraft.app` onto Applications. The command-line tool is in
|
||||
drag `FieldCraft.app` onto Applications. The command-line tool is included at
|
||||
`/Applications/FieldCraft.app/Contents/MacOS/fieldcraft-cli` and also available separately in
|
||||
`fieldcraft-cli-{version}-macos-universal.zip`.
|
||||
- **Linux** (x86_64, glibc 2.35+: Ubuntu 22.04, Debian 12, Fedora 36 or newer):
|
||||
`fieldcraft-{version}-linux-x86_64.AppImage` (`chmod +x`, then run it), or unpack
|
||||
|
||||
@@ -129,10 +129,17 @@ jobs:
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
# Shared keys: release.yml's verify jobs restore these caches (they save only on main).
|
||||
shared-key: ci
|
||||
save-if: ${{ github.ref == 'refs/heads/develop' }}
|
||||
- name: Software GPU (lavapipe), so headless snapshots run the GPU pipeline
|
||||
run: sudo apt-get update -q && sudo apt-get install -y -q --no-install-recommends mesa-vulkan-drivers libvulkan1
|
||||
- name: cargo xtask ci (the affected crates, or everything)
|
||||
env:
|
||||
BASE: ${{ needs.plan.outputs.base }}
|
||||
# The drawing tests run on lavapipe and fail, instead of skipping, without it.
|
||||
FIELDCRAFT_GPU: software
|
||||
FIELDCRAFT_REQUIRE_GPU: "1"
|
||||
run: |
|
||||
if [ -n "$BASE" ]; then cargo xtask ci --affected "$BASE"; else cargo xtask ci; fi
|
||||
|
||||
@@ -149,6 +156,7 @@ jobs:
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
shared-key: heavy
|
||||
save-if: ${{ github.ref == 'refs/heads/develop' }}
|
||||
- run: cargo xtask test --heavy
|
||||
|
||||
@@ -167,7 +175,7 @@ jobs:
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
key: oracle
|
||||
shared-key: oracle
|
||||
save-if: ${{ github.ref == 'refs/heads/develop' }}
|
||||
- run: cargo xtask oracle
|
||||
- name: docs/oracle.md is current
|
||||
@@ -185,12 +193,29 @@ jobs:
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
key: oracle-macos
|
||||
shared-key: oracle-macos
|
||||
save-if: ${{ github.ref == 'refs/heads/develop' }}
|
||||
- run: cargo xtask oracle
|
||||
- name: docs/oracle.md is current
|
||||
run: git diff --exit-code docs/oracle.md
|
||||
|
||||
windows:
|
||||
# The desktop app's tests on Windows, nightly and on demand: the Windows-only code
|
||||
# (apps/fieldcraft/src/native_crash.rs, the exception filter) builds and a child process that
|
||||
# faults on purpose leaves its native crash report (#299). Releases build Windows too.
|
||||
name: Windows app tests
|
||||
if: github.event.schedule == '23 3 * * *' || github.event_name == 'workflow_dispatch'
|
||||
runs-on: windows-latest
|
||||
timeout-minutes: 60
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
shared-key: windows
|
||||
save-if: ${{ github.ref == 'refs/heads/develop' }}
|
||||
- run: cargo test --locked --profile ci -p fieldcraft
|
||||
|
||||
advisories:
|
||||
name: Dependency licences, advisories and sources
|
||||
if: github.event.schedule != '41 4 * * 0' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository || github.event.pull_request.user.login == 'dependabot[bot]')
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# The web app (apps/fieldcraft-web, docs/web.md) on GitHub Pages: https://jyviko.github.io/fieldcraft/
|
||||
# The web app (apps/fieldcraft-web, docs/web.md) on GitHub Pages: https://tangency-labs.github.io/fieldcraft/
|
||||
# After SolidCraft's pages.yml (MIT OR Apache-2.0; see ATTRIBUTION.md).
|
||||
#
|
||||
# A push to `main` (a release; main holds released code only) builds the app with Trunk (release,
|
||||
|
||||
@@ -17,9 +17,12 @@
|
||||
# macOS: signed with a Developer ID and notarized when the APPLE_* secrets exist, ad-hoc
|
||||
# otherwise (the release notes say which). Linux and Windows builds are not signed.
|
||||
#
|
||||
# Before anything is packaged, the verify jobs run everything CI can: the full `cargo xtask ci`,
|
||||
# `cargo xtask test --heavy` and `cargo xtask oracle` on Linux, and the oracle on macOS. Develop's
|
||||
# CI tests only what each change affects (ci.yml), so this is where a release proves all of it.
|
||||
# The verify jobs run everything CI can: the full `cargo xtask ci`, `cargo xtask test --heavy` and
|
||||
# `cargo xtask oracle` on Linux, and the oracle on macOS. Develop's CI tests only what each change
|
||||
# affects (ci.yml), so this is where a release proves all of it. The packaging jobs build in
|
||||
# parallel with them and only upload workflow artifacts; the `release` job, the only one that
|
||||
# signs SHA256SUMS.txt or touches a GitHub Release, needs every verify and packaging job, so a
|
||||
# failing verify means no draft and no signature.
|
||||
#
|
||||
# Dry run: `gh workflow run release.yml --ref <branch>` verifies and builds everything and uploads the
|
||||
# `release-assets` artifact without creating a release. `-f draft=true` on `main` retries
|
||||
@@ -45,8 +48,10 @@ concurrency:
|
||||
cancel-in-progress: false
|
||||
|
||||
env:
|
||||
# The same CARGO_* environment as ci.yml: rust-cache hashes it into the cache key, so a
|
||||
# difference would stop the verify jobs restoring develop's caches. (rust-cache already sets
|
||||
# CARGO_INCREMENTAL=0 for every job.)
|
||||
CARGO_TERM_COLOR: always
|
||||
CARGO_INCREMENTAL: 0
|
||||
|
||||
jobs:
|
||||
version:
|
||||
@@ -75,8 +80,9 @@ jobs:
|
||||
{ echo "version=$VERSION"; echo "prerelease=$pre"; echo "draft=$REQUEST_DRAFT"; } >>"$GITHUB_OUTPUT"
|
||||
echo "### FieldCraft v$VERSION (\`${GITHUB_SHA::9}\`)" >>"$GITHUB_STEP_SUMMARY"
|
||||
|
||||
# The full gate, the heavy tests and the oracle on Linux and macOS: every packaging job needs
|
||||
# them, so a release (or a dry run) never builds from a commit that fails any of them.
|
||||
# The full gate, the heavy tests and the oracle on Linux and macOS: the `release` job needs them,
|
||||
# so a release never drafts or signs a commit that fails any of them. Each restores the cache of
|
||||
# its ci.yml counterpart on develop (same shared-key), so it starts warm.
|
||||
verify-ci:
|
||||
name: Verify (cargo xtask ci)
|
||||
runs-on: ubuntu-24.04
|
||||
@@ -89,6 +95,7 @@ jobs:
|
||||
targets: wasm32-unknown-unknown
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
shared-key: ci
|
||||
# Release runs are rare: only release-branch runs keep a cache, so dry runs don't evict CI's.
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
- run: cargo xtask ci
|
||||
@@ -102,6 +109,7 @@ jobs:
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
shared-key: heavy
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
- run: cargo xtask test --heavy
|
||||
|
||||
@@ -112,9 +120,9 @@ jobs:
|
||||
matrix:
|
||||
include:
|
||||
- os: ubuntu-24.04
|
||||
cache: release-oracle-linux
|
||||
cache: oracle
|
||||
- os: macos-14
|
||||
cache: release-oracle-macos
|
||||
cache: oracle-macos
|
||||
runs-on: ${{ matrix.os }}
|
||||
timeout-minutes: 60
|
||||
steps:
|
||||
@@ -122,7 +130,7 @@ jobs:
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
|
||||
with:
|
||||
key: ${{ matrix.cache }}
|
||||
shared-key: ${{ matrix.cache }}
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
- run: cargo xtask oracle
|
||||
- name: docs/oracle.md is current
|
||||
@@ -130,7 +138,7 @@ jobs:
|
||||
|
||||
macos:
|
||||
name: macOS universal
|
||||
needs: [version, verify-ci, verify-heavy, verify-oracle]
|
||||
needs: version
|
||||
# Signing, notarization and analytics secrets live in the `release` environment, which only
|
||||
# `main` may use; a dry run on another branch asks for no environment and builds unsigned.
|
||||
environment: ${{ github.ref == 'refs/heads/main' && 'release' || '' }}
|
||||
@@ -179,7 +187,7 @@ jobs:
|
||||
|
||||
linux:
|
||||
name: Linux x86_64
|
||||
needs: [version, verify-ci, verify-heavy, verify-oracle]
|
||||
needs: version
|
||||
# The oldest GitHub-hosted image, so the binaries need only glibc >= 2.35.
|
||||
# Signing, notarization and analytics secrets live in the `release` environment, which only
|
||||
# `main` may use; a dry run on another branch asks for no environment and builds unsigned.
|
||||
@@ -268,7 +276,7 @@ jobs:
|
||||
|
||||
windows:
|
||||
name: Windows x64
|
||||
needs: [version, verify-ci, verify-heavy, verify-oracle]
|
||||
needs: version
|
||||
# Signing, notarization and analytics secrets live in the `release` environment, which only
|
||||
# `main` may use; a dry run on another branch asks for no environment and builds unsigned.
|
||||
environment: ${{ github.ref == 'refs/heads/main' && 'release' || '' }}
|
||||
@@ -302,7 +310,8 @@ jobs:
|
||||
|
||||
release:
|
||||
name: Draft release (or dry-run artifacts)
|
||||
needs: [version, macos, linux, windows]
|
||||
# The only job that publishes anything: it waits for every verify and packaging job.
|
||||
needs: [version, verify-ci, verify-heavy, verify-oracle, macos, linux, windows]
|
||||
# Signing, notarization and analytics secrets live in the `release` environment, which only
|
||||
# `main` may use; a dry run on another branch asks for no environment and builds unsigned.
|
||||
environment: ${{ github.ref == 'refs/heads/main' && 'release' || '' }}
|
||||
|
||||
@@ -24,7 +24,9 @@ FieldCraft everywhere: crates `fieldcraft-*`, binaries `fieldcraft` and `fieldcr
|
||||
## Never crash
|
||||
People trust FieldCraft with their models; a crash loses their work. **This outranks feature work.**
|
||||
- **No panics in non-test code:** no `unwrap()`, `expect()`, `panic!`, `unreachable!`, `todo!`,
|
||||
`unimplemented!`; no `unsafe` (`unsafe_code = "forbid"`).
|
||||
`unimplemented!`; no `unsafe` (`unsafe_code = "forbid"`). The one exception, in the app crate
|
||||
(which denies it instead), has no safe API: the Windows-only `apps/fieldcraft/src/native_crash.rs`
|
||||
(the exception filter that records crashes inside native code such as graphics drivers).
|
||||
- **Errors are `Result<T, E>`** through the crate's error type and `?`. An unfinished feature
|
||||
returns an error ("not supported yet: …"); it never panics.
|
||||
- **Input-derived numbers are hostile** (command parameters, model files, solver decks,
|
||||
@@ -137,7 +139,7 @@ People trust FieldCraft with their models; a crash loses their work. **This outr
|
||||
heavy tests, nightly, before every release and on demand (`gh workflow run CI --ref <branch>`). Hostile-input tests run commands on sessions with small
|
||||
work caps (`Session::limits`) so refusals are instant; never lower the production caps for a
|
||||
test.
|
||||
- **Publishing**: `origin` is github.com/jyviko/fieldcraft. Changes land through pull requests
|
||||
- **Publishing**: `origin` is github.com/tangency-labs/fieldcraft. Changes land through pull requests
|
||||
as the workflow above describes; the coordinator reads every merged diff and sends findings
|
||||
back as fix-forward work. Never force-push `develop` or `main`; `main` holds released code and moves only by
|
||||
fast-forward at a release (docs/releasing.md). Never commit `plan/`, `.agent-files/`,
|
||||
|
||||
@@ -24,6 +24,7 @@ is committed and listed here with its exports.
|
||||
| docs/media/examples/bracket-hex.png | FieldCraft contributors | Original: the FieldCraft window captured (`ui.capture`) with the solved bracket-hex sample (docs/examples.md) | MIT OR Apache-2.0 |
|
||||
| docs/media/examples/crank-arm.png | FieldCraft contributors | Original: the FieldCraft window captured (`ui.capture`) with the solved crank-arm sample (docs/examples.md) | MIT OR Apache-2.0 |
|
||||
| docs/media/examples/crank-mirror.png | FieldCraft contributors | Original: the FieldCraft window captured (`ui.capture`) with the solved crank-mirror sample (docs/examples.md) | MIT OR Apache-2.0 |
|
||||
| docs/meshing/skill/analytic-cylinder.png, docs/meshing/skill/analytic-pipe.png, docs/meshing/skill/analytic-plate-hole.png, docs/meshing/skill/analytic-sphere.png, docs/meshing/skill/analytic-torus.png, docs/meshing/skill/bent-tube.png, docs/meshing/skill/bolted-assembly.png, docs/meshing/skill/cantilever-i-beam.png, docs/meshing/skill/crank-arm.png, docs/meshing/skill/cup.png, docs/meshing/skill/filleted-bracket-blocks.png, docs/meshing/skill/filleted-bracket.png, docs/meshing/skill/i-beam.png, docs/meshing/skill/loft-transition.png, docs/meshing/skill/perforated-plate.png, docs/meshing/skill/pi5-base.png, docs/meshing/skill/pi5-lid.png, docs/meshing/skill/pinhole-block.png, docs/meshing/skill/plain-cup.png, docs/meshing/skill/rounded-box.png, docs/meshing/skill/shallow-groove.png, docs/meshing/skill/sharp-wedge.png, docs/meshing/skill/sphere-on-plate.png, docs/meshing/skill/spline-vase.png, docs/meshing/skill/stepped-block.png, docs/meshing/skill/thin-ribs.png, docs/meshing/skill/thin-wall-enclosure.png, docs/meshing/skill/upright-bracket.png | FieldCraft contributors | Original: section pictures of the corpus parts and samples meshed by the meshing skill, drawn by `fieldcraft-cli snapshot` (`cargo xtask skill-results`, docs/meshing/skill-results.md) | MIT OR Apache-2.0 |
|
||||
| docs/media/examples/flow-past.png | FieldCraft contributors | Original: the FieldCraft window captured (`ui.capture`) with the flow-past sample (its bracket; the viewport does not draw flow results yet) (docs/examples.md) | MIT OR Apache-2.0 |
|
||||
| docs/media/examples/cantilever.png | FieldCraft contributors | Original: the FieldCraft window captured (`ui.capture`) with the solved cantilever sample (docs/examples.md) | MIT OR Apache-2.0 |
|
||||
| docs/media/examples/impact.png | FieldCraft contributors | Original: the FieldCraft window captured (`ui.capture`) with the solved impact sample (docs/examples.md) | MIT OR Apache-2.0 |
|
||||
@@ -56,6 +57,7 @@ is committed and listed here with its exports.
|
||||
| assets/icons/file.save_as.svg, assets/icons/file.save_as.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/file.save_as.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/file.save.svg, assets/icons/file.save.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/file.save.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.bodies.svg, assets/icons/geometry.bodies.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.bodies.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.check.svg, assets/icons/geometry.check.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.check.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.datum.svg, assets/icons/geometry.datum.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.datum.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.datum.axis.svg, assets/icons/geometry.datum.axis.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.datum.axis.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.datum.point.svg, assets/icons/geometry.datum.point.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.datum.point.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
@@ -63,6 +65,11 @@ is committed and listed here with its exports.
|
||||
| assets/icons/geometry.midsurface.svg, assets/icons/geometry.midsurface.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.midsurface.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.open.svg, assets/icons/geometry.open.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.open.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.partition.svg, assets/icons/geometry.partition.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.partition.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.faces.merge.svg, assets/icons/geometry.faces.merge.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.faces.merge.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.face.absorb.svg, assets/icons/geometry.face.absorb.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.face.absorb.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.face.split.svg, assets/icons/geometry.face.split.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.face.split.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.defeature.svg, assets/icons/geometry.defeature.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.defeature.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.stitch.svg, assets/icons/geometry.stitch.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.stitch.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/geometry.tessellate.svg, assets/icons/geometry.tessellate.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/geometry.tessellate.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/interaction.bolt.svg, assets/icons/interaction.bolt.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/interaction.bolt.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/interaction.connector.svg, assets/icons/interaction.connector.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/interaction.connector.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
@@ -92,16 +99,21 @@ is committed and listed here with its exports.
|
||||
| assets/icons/mesh.order.quadratic.svg, assets/icons/mesh.order.quadratic.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.order.quadratic.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.quad.faces.svg, assets/icons/mesh.quad.faces.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.quad.faces.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.quality.svg, assets/icons/mesh.quality.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.quality.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.quality.mark.svg, assets/icons/mesh.quality.mark.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.quality.mark.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/results.export.vtu.svg, assets/icons/results.export.vtu.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/results.export.vtu.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.export.stl.svg, assets/icons/mesh.export.stl.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.export.stl.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.region.remesh.svg, assets/icons/mesh.region.remesh.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.region.remesh.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.size.adapt.svg, assets/icons/mesh.size.adapt.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.size.adapt.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.size.global.svg, assets/icons/mesh.size.global.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.size.global.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.size.set.svg, assets/icons/mesh.size.set.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.size.set.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.seed.edge.svg, assets/icons/mesh.seed.edge.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.seed.edge.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.smooth.svg, assets/icons/mesh.smooth.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.smooth.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.surface.check.svg, assets/icons/mesh.surface.check.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.surface.check.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.tag.svg, assets/icons/mesh.tag.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.tag.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.tet.generate.svg, assets/icons/mesh.tet.generate.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.tet.generate.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.hex.extrude.svg, assets/icons/mesh.hex.extrude.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.hex.extrude.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.plan.svg, assets/icons/mesh.plan.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.plan.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.planar.svg, assets/icons/mesh.planar.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.planar.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.transition.svg, assets/icons/mesh.transition.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.transition.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.untangle.svg, assets/icons/mesh.untangle.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.untangle.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/mesh.verify.svg, assets/icons/mesh.verify.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/mesh.verify.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
| assets/icons/model.activity.svg, assets/icons/model.activity.vectorcraft | FieldCraft contributors | Original toolbar icon drawn with VectorCraft from `assets/icons/src/model.activity.json` (`cargo xtask icons`) | MIT OR Apache-2.0 |
|
||||
@@ -197,12 +209,12 @@ is committed and listed here with its exports.
|
||||
| clippy.toml, rustfmt.toml | SolidCraft | MIT OR Apache-2.0 |
|
||||
| crates/geometry/src/tagged_mesh.rs (package and part reading, transforms, units, caps) | SolidCraft `crates/io/src/threemf.rs` | MIT OR Apache-2.0 |
|
||||
| crates/render/src/camera.rs, crates/render/src/raster.rs | SolidCraft `crates/render/src/camera.rs`, `raster.rs` | MIT OR Apache-2.0 |
|
||||
| crates/ui-egui/src/gpu.rs, crates/ui-egui/src/theme.rs (tokens and their values), crates/ui-egui/src/control.rs, crates/ui-egui/src/lib.rs (input injection, screenshots), crates/ui-egui/src/toolbar.rs (workspace switcher, tabs, panels with drop-downs, fitting), crates/ui-egui/src/appbar.rs (application bar; the mark is drawn from assets/app-icon/icon-small.svg's coordinates), crates/ui-egui/src/titlebar.rs (caption buttons, drag and resize zones; SolidCraft took it from VectorCraft), crates/ui-egui/src/frame.rs (windows, heading bands), crates/ui-egui/src/browser.rs (row drawing), crates/ui-egui/src/palette.rs (command line and search), crates/ui-egui/src/timeline.rs (timeline strip, transport), crates/ui-egui/src/viewport.rs (navigation bar, orientation gizmo), crates/ui-egui/src/files.rs (background opening, file pickers as host services) | SolidCraft `crates/ui-egui/src/` (titlebar.rs also VectorCraft `crates/ui-egui/src/titlebar.rs`) | MIT OR Apache-2.0 |
|
||||
| crates/gpu/src/view.rs (viewport pipelines and shaders), crates/ui-egui/src/gpu.rs, crates/ui-egui/src/theme.rs (tokens and their values), crates/ui-egui/src/control.rs, crates/ui-egui/src/lib.rs (input injection, screenshots), crates/ui-egui/src/toolbar.rs (workspace switcher, tabs, panels with drop-downs, fitting), crates/ui-egui/src/appbar.rs (application bar; the mark is drawn from assets/app-icon/icon-small.svg's coordinates), crates/ui-egui/src/titlebar.rs (caption buttons, drag and resize zones; SolidCraft took it from VectorCraft), crates/ui-egui/src/frame.rs (windows, heading bands), crates/ui-egui/src/browser.rs (row drawing), crates/ui-egui/src/palette.rs (command line and search), crates/ui-egui/src/timeline.rs (timeline strip, transport), crates/ui-egui/src/viewport.rs (navigation bar, orientation gizmo), crates/ui-egui/src/files.rs (background opening, file pickers as host services) | SolidCraft `crates/ui-egui/src/` (titlebar.rs also VectorCraft `crates/ui-egui/src/titlebar.rs`) | MIT OR Apache-2.0 |
|
||||
| crates/ui-egui/src/navigation.rs (navigation presets and bindings), crates/ui-egui/src/viewport.rs (orbit about the point under the cursor, or the drawn centre from empty space; Ctrl + left drag orbits) | SolidCraft `crates/ui-egui/src/prefs.rs` (`NavPreset`, `NavBinding`, `NAV_PRESETS`, `orbit`) | MIT OR Apache-2.0 |
|
||||
| crates/ui-egui/src/fields.rs (form rows: a label column of fixed width, then the field) | SolidCraft `crates/ui-egui/src/dialogs.rs` | MIT OR Apache-2.0 |
|
||||
| crates/io/src/fs.rs (atomic writes) | SolidCraft `crates/io/src/safe_write.rs` | MIT OR Apache-2.0 |
|
||||
| crates/io/src/recovery.rs (recovery folder, entries, locks) | SolidCraft `crates/engine/src/recovery.rs` | MIT OR Apache-2.0 |
|
||||
| crates/engine/src/crash.rs, crates/engine/src/crash_tests.rs (crash reports: panic hook, locked command journal, unclean-exit detection, report folders and limits, replay), apps/fieldcraft-cli/src/crashes.rs, docs/crash-reports.md (structure) | SolidCraft `crates/engine/src/crash.rs`, `crash_tests.rs`, `apps/solidcraft-cli/src/crashes.rs`, `docs/crash-reports.md` | MIT OR Apache-2.0 |
|
||||
| crates/engine/src/crash.rs, crates/engine/src/crash_tests.rs (crash reports: panic hook, locked command journal, unclean-exit detection, startup steps, native crash reports, report folders and limits, replay), apps/fieldcraft/src/native_crash.rs (Windows exception filter and minidump), apps/fieldcraft-cli/src/crashes.rs, docs/crash-reports.md (structure) | SolidCraft `crates/engine/src/crash.rs`, `crash_tests.rs`, `apps/solidcraft/src/native_crash.rs`, `apps/solidcraft-cli/src/crashes.rs`, `docs/crash-reports.md` | MIT OR Apache-2.0 |
|
||||
| apps/fieldcraft/src/control_server.rs | SolidCraft `apps/solidcraft/src/control_server.rs` | MIT OR Apache-2.0 |
|
||||
| crates/mcp/src/ (JSON-RPC framing, MCP lifecycle, tool schema checks, the control-channel backend, base64) | SolidCraft `crates/mcp/src/` | MIT OR Apache-2.0 |
|
||||
| crates/io/src/roots.rs (the rules of read and write roots) | VectorCraft's automation roots (docs/mcp.md there) | MIT OR Apache-2.0 |
|
||||
|
||||
@@ -15,7 +15,7 @@ needed most, and how to claim work. The rules every change follows, by a person
|
||||
You need stable Rust 1.95 or newer.
|
||||
|
||||
```sh
|
||||
git clone https://github.com/jyviko/fieldcraft && cd fieldcraft
|
||||
git clone https://github.com/tangency-labs/fieldcraft && cd fieldcraft
|
||||
cargo run --release -p fieldcraft -- --sample cantilever # the desktop app on a sample model
|
||||
cargo run -p fieldcraft-cli -- commands # every command and its parameters
|
||||
cargo test -p fieldcraft-solver # the tests of one crate
|
||||
@@ -28,32 +28,32 @@ cargo xtask oracle --quick # the verification ca
|
||||
|
||||
## Where help is wanted most
|
||||
|
||||
See the [`help wanted`](https://github.com/jyviko/fieldcraft/labels/help%20wanted) and
|
||||
[`good first issue`](https://github.com/jyviko/fieldcraft/labels/good%20first%20issue) labels, and
|
||||
See the [`help wanted`](https://github.com/tangency-labs/fieldcraft/labels/help%20wanted) and
|
||||
[`good first issue`](https://github.com/tangency-labs/fieldcraft/labels/good%20first%20issue) labels, and
|
||||
the `area:` labels for your field. In order of how much they would help users:
|
||||
|
||||
1. **Reproducible meshes** ([#115](https://github.com/jyviko/fieldcraft/issues/115)). The same part
|
||||
1. **Reproducible meshes** ([#115](https://github.com/tangency-labs/fieldcraft/issues/115)). The same part
|
||||
must give the same tet mesh on every platform and every run, as the solvers already do.
|
||||
2. **All-hex meshing of thin and curved parts** ([#85](https://github.com/jyviko/fieldcraft/issues/85)).
|
||||
2. **All-hex meshing of thin and curved parts** ([#85](https://github.com/tangency-labs/fieldcraft/issues/85)).
|
||||
The octree mesher over-refines thin walls; concave transition templates and an up-front depth
|
||||
are next. Hex meshing is the centre of gravity of the project.
|
||||
3. **Getting results in and out**: ParaView export ([#90](https://github.com/jyviko/fieldcraft/issues/90)),
|
||||
Gmsh import ([#91](https://github.com/jyviko/fieldcraft/issues/91)), Abaqus deck reading
|
||||
([#98](https://github.com/jyviko/fieldcraft/issues/98)), CSV and STL
|
||||
([#92](https://github.com/jyviko/fieldcraft/issues/92), [#93](https://github.com/jyviko/fieldcraft/issues/93)).
|
||||
4. **Verification benchmarks** ([#97](https://github.com/jyviko/fieldcraft/issues/97),
|
||||
[#106](https://github.com/jyviko/fieldcraft/issues/106)). An engineer with a published benchmark
|
||||
3. **Getting results in and out**: ParaView export ([#90](https://github.com/tangency-labs/fieldcraft/issues/90)),
|
||||
Gmsh import ([#91](https://github.com/tangency-labs/fieldcraft/issues/91)), Abaqus deck reading
|
||||
([#98](https://github.com/tangency-labs/fieldcraft/issues/98)), CSV and STL
|
||||
([#92](https://github.com/tangency-labs/fieldcraft/issues/92), [#93](https://github.com/tangency-labs/fieldcraft/issues/93)).
|
||||
4. **Verification benchmarks** ([#97](https://github.com/tangency-labs/fieldcraft/issues/97),
|
||||
[#106](https://github.com/tangency-labs/fieldcraft/issues/106)). An engineer with a published benchmark
|
||||
can add it as an oracle case without touching solver code.
|
||||
5. **Nonlinear elements and materials**: incompatible modes at finite strain
|
||||
([#82](https://github.com/jyviko/fieldcraft/issues/82)), kinematic hardening
|
||||
([#81](https://github.com/jyviko/fieldcraft/issues/81)), and the material library
|
||||
([#99](https://github.com/jyviko/fieldcraft/issues/99)).
|
||||
6. **The app**: typed command forms ([#112](https://github.com/jyviko/fieldcraft/issues/112)), XY and
|
||||
path plots ([#94](https://github.com/jyviko/fieldcraft/issues/94), [#95](https://github.com/jyviko/fieldcraft/issues/95)).
|
||||
([#82](https://github.com/tangency-labs/fieldcraft/issues/82)), kinematic hardening
|
||||
([#81](https://github.com/tangency-labs/fieldcraft/issues/81)), and the material library
|
||||
([#99](https://github.com/tangency-labs/fieldcraft/issues/99)).
|
||||
6. **The app**: typed command forms ([#112](https://github.com/tangency-labs/fieldcraft/issues/112)), XY and
|
||||
path plots ([#94](https://github.com/tangency-labs/fieldcraft/issues/94), [#95](https://github.com/tangency-labs/fieldcraft/issues/95)).
|
||||
|
||||
Bigger areas on the [roadmap](ROADMAP.md) — contact, dynamics, composites, coupled fields, wave
|
||||
propagation, turbulence — are open to anyone who wants to own one: start a thread in
|
||||
[Discussions](https://github.com/jyviko/fieldcraft/discussions).
|
||||
[Discussions](https://github.com/tangency-labs/fieldcraft/discussions).
|
||||
|
||||
## Before you start: claim an issue
|
||||
|
||||
|
||||
@@ -425,12 +425,6 @@ version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.22.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.23.1"
|
||||
@@ -1279,12 +1273,14 @@ dependencies = [
|
||||
"fieldcraft-io",
|
||||
"fieldcraft-ui-egui",
|
||||
"fieldcraft-update",
|
||||
"getrandom 0.3.4",
|
||||
"getrandom 0.4.3",
|
||||
"log",
|
||||
"rfd",
|
||||
"rustls",
|
||||
"serde_json",
|
||||
"signal-hook",
|
||||
"ureq",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1292,9 +1288,12 @@ name = "fieldcraft-cli"
|
||||
version = "0.6.0"
|
||||
dependencies = [
|
||||
"fieldcraft-engine",
|
||||
"fieldcraft-gpu",
|
||||
"fieldcraft-io",
|
||||
"fieldcraft-mcp",
|
||||
"fieldcraft-render",
|
||||
"fieldcraft-update",
|
||||
"png",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
@@ -1356,6 +1355,7 @@ version = "0.6.0"
|
||||
dependencies = [
|
||||
"fieldcraft-model",
|
||||
"quick-xml 0.42.0",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"solidcraft-doc",
|
||||
"solidcraft-engine",
|
||||
@@ -1366,6 +1366,18 @@ dependencies = [
|
||||
"zip 9.0.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fieldcraft-gpu"
|
||||
version = "0.6.0"
|
||||
dependencies = [
|
||||
"fieldcraft-render",
|
||||
"log",
|
||||
"pollster 1.0.1",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"wgpu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fieldcraft-io"
|
||||
version = "0.6.0"
|
||||
@@ -1394,6 +1406,7 @@ name = "fieldcraft-mcp"
|
||||
version = "0.6.0"
|
||||
dependencies = [
|
||||
"fieldcraft-engine",
|
||||
"fieldcraft-gpu",
|
||||
"fieldcraft-io",
|
||||
"fieldcraft-render",
|
||||
"fieldcraft-testkit",
|
||||
@@ -1405,6 +1418,7 @@ name = "fieldcraft-mesh"
|
||||
version = "0.6.0"
|
||||
dependencies = [
|
||||
"fieldcraft-model",
|
||||
"libm",
|
||||
"num-bigint",
|
||||
"num-rational",
|
||||
"num-traits",
|
||||
@@ -1457,6 +1471,7 @@ dependencies = [
|
||||
"fieldcraft-engine",
|
||||
"fieldcraft-geometry",
|
||||
"fieldcraft-mesh",
|
||||
"fieldcraft-parallel",
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1472,6 +1487,7 @@ dependencies = [
|
||||
"fieldcraft-engine",
|
||||
"fieldcraft-explicit",
|
||||
"fieldcraft-geometry",
|
||||
"fieldcraft-gpu",
|
||||
"fieldcraft-model",
|
||||
"fieldcraft-render",
|
||||
"fieldcraft-update",
|
||||
@@ -1503,7 +1519,7 @@ dependencies = [
|
||||
name = "fieldcraft-web"
|
||||
version = "0.6.0"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"base64",
|
||||
"eframe",
|
||||
"egui",
|
||||
"fieldcraft-engine",
|
||||
@@ -2241,9 +2257,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "minisign"
|
||||
version = "0.9.1"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aefd422a7a8b5efced01cd7cdf3771e62ebecbc90e3d27695002d3af6af94586"
|
||||
checksum = "3062dbbf4d8f57899e2252804f7512a4fd89c8211c58fa6c786d38794a0c809d"
|
||||
dependencies = [
|
||||
"ct-codecs",
|
||||
"getrandom 0.4.3",
|
||||
@@ -3660,6 +3676,16 @@ version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b29ffa27d3d59a049bb328258de100f8012848d1b08d444e08bbc02ac468435e"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"signal-hook-registry",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.8"
|
||||
@@ -4414,7 +4440,7 @@ version = "3.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a7ac20be9b7726e0bbdbf974c059676d9acb1cd414961f570a4e8231cacd7fc"
|
||||
dependencies = [
|
||||
"base64 0.23.1",
|
||||
"base64",
|
||||
"log",
|
||||
"percent-encoding",
|
||||
"rustls",
|
||||
@@ -4430,7 +4456,7 @@ version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f86fd172ccca569e458f61b6bdd6220965a9ef36e672a6852953b51a0e1583be"
|
||||
dependencies = [
|
||||
"base64 0.23.1",
|
||||
"base64",
|
||||
"http",
|
||||
"httparse",
|
||||
"log",
|
||||
|
||||
@@ -8,7 +8,7 @@ edition = "2024"
|
||||
license = "MIT OR Apache-2.0"
|
||||
rust-version = "1.95"
|
||||
authors = ["FieldCraft contributors"]
|
||||
repository = "https://github.com/jyviko/fieldcraft"
|
||||
repository = "https://github.com/tangency-labs/fieldcraft"
|
||||
|
||||
[workspace.dependencies]
|
||||
# internal
|
||||
@@ -28,6 +28,7 @@ fieldcraft-render = { path = "crates/render" }
|
||||
fieldcraft-ui-egui = { path = "crates/ui-egui" }
|
||||
fieldcraft-mcp = { path = "crates/mcp" }
|
||||
fieldcraft-update = { path = "crates/update" }
|
||||
fieldcraft-gpu = { path = "crates/gpu" }
|
||||
|
||||
# foundation
|
||||
thiserror = "2"
|
||||
@@ -51,6 +52,9 @@ resvg = { version = "0.48", default-features = false }
|
||||
egui = "0.36"
|
||||
eframe = { version = "0.36", default-features = false }
|
||||
egui-wgpu = "0.36"
|
||||
# the wgpu egui-wgpu uses, for the offscreen viewport (crates/gpu); backends come per target there
|
||||
wgpu = { version = "30", default-features = false, features = ["std", "wgsl"] }
|
||||
pollster = "1.0"
|
||||
# Shared Craft-family egui components (icon buttons, tabs, widget palette); the same rev as
|
||||
# VectorCraft. MIT OR Apache-2.0.
|
||||
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "5c874d51eeb3de70e735c8ce87a59a7dedd47675" }
|
||||
|
||||
@@ -10,8 +10,8 @@ see the results — with a nonlinear implicit solver (large deformation, plastic
|
||||
in place. Hex meshing is the centre of gravity. Next: buckling and arc-length, contact, then
|
||||
the wider material and multiphysics library. See [ROADMAP.md](ROADMAP.md).
|
||||
**Want to help?** Pick an issue labelled
|
||||
[good first issue](https://github.com/jyviko/fieldcraft/labels/good%20first%20issue) or
|
||||
[help wanted](https://github.com/jyviko/fieldcraft/labels/help%20wanted) and read
|
||||
[good first issue](https://github.com/tangency-labs/fieldcraft/labels/good%20first%20issue) or
|
||||
[help wanted](https://github.com/tangency-labs/fieldcraft/labels/help%20wanted) and read
|
||||
[CONTRIBUTING.md](CONTRIBUTING.md).
|
||||
|
||||

|
||||
@@ -28,15 +28,15 @@ theory with shear 0.177 mm).
|
||||
|
||||
## Download
|
||||
|
||||
[](https://github.com/jyviko/fieldcraft/releases/latest)
|
||||
[](https://github.com/jyviko/fieldcraft/releases/latest)
|
||||
[](https://github.com/jyviko/fieldcraft/releases/latest)
|
||||
[](https://jyviko.github.io/fieldcraft/)
|
||||
[](https://github.com/tangency-labs/fieldcraft/releases/latest)
|
||||
[](https://github.com/tangency-labs/fieldcraft/releases/latest)
|
||||
[](https://github.com/tangency-labs/fieldcraft/releases/latest)
|
||||
[](https://tangency-labs.github.io/fieldcraft/)
|
||||
|
||||
- **macOS:** open the `.dmg` and drag FieldCraft into Applications (Apple silicon and Intel).
|
||||
- **Windows:** unzip and run `fieldcraft.exe`.
|
||||
- **Linux:** the `.AppImage` (allow it to run as a program), or the `.tar.gz`.
|
||||
- **Browser:** [try it at jyviko.github.io/fieldcraft](https://jyviko.github.io/fieldcraft/), no install.
|
||||
- **Browser:** [try it at tangency-labs.github.io/fieldcraft](https://tangency-labs.github.io/fieldcraft/), no install.
|
||||
- **Command line:** `fieldcraft-cli` ships in every package; AI agents connect to it over MCP
|
||||
([docs/mcp.md](docs/mcp.md)).
|
||||
|
||||
@@ -118,6 +118,7 @@ cargo xtask ci # fmt, assets, layers, clippy, t
|
||||
cargo xtask test --heavy # the heavy (ignored) tests and the doc tests
|
||||
cargo run -p fieldcraft-cli -- commands # every command and its parameters
|
||||
cargo run -p fieldcraft-cli -- run script.json # run a command script
|
||||
cargo run -p fieldcraft-cli -- snapshot script.json --out shot.png # draw it to a PNG, no window
|
||||
cargo run -p fieldcraft-cli -- mcp # MCP server on stdio for AI agents (docs/mcp.md)
|
||||
```
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ pressure) is a degree of freedom of the existing DOF framework, not a separate s
|
||||
|---|---|---|
|
||||
| M0 Foundation | Workspace, quality gates, FE model, command engine with rollback and panic guard, CLI, geometry boundary on SolidCraft | done |
|
||||
| M1 Geometry in and surface preparation | Open `.solidcraft` designs with persistent face names, assemblies placed (done); face-tagged 3MF (done); test corpus, `cargo xtask corpus` (done); exact predicates (done); quality metrics and certified validity gate (done); weld, closed/manifold/orientation and self-intersection checks, feature edges (`fieldcraft-mesh::surface`, `mesh.surface.check`, done) | in progress |
|
||||
| M2 Tet meshing and size field | Size field (global, curvature and proximity; edge seeds done: `mesh.seed.edge` on persistent design edge names (`<face>|<face>`) with a count or size, geometric bias and an exact, min or free constraint, shown as ticks and under Mesh › Seeds, honoured by the hex sweep, extrude, revolve, mapped, blocks and cells meshers through interval assignment (conflicting exact seeds named) and by the tet mesher, which divides the edge at the seed's nodes and grades its size field from them; per-face seeds to do; the `fieldcraft_mesh::size::SizeField` trait and edge-division helpers are in); Bowyer–Watson Delaunay (exact, symbolic perturbation, verified; `fieldcraft-mesh::delaunay`, done); robust CDT boundary recovery (Diazzi et al. 2023: implicit LNC Steiner points on segments, gift-wrapping facet recovery, inside/outside labelling, B-rep face tags on boundary tet faces, flip repair of rounding (§5.1) checked by the validity gate; `fieldcraft-mesh::cdt` and `mesh.tet.generate`, done: all 20 valid corpus parts); quality stage (done: `fieldcraft-mesh::tet` — curvature-graded size field, edge-collapse coarsening, constrained Delaunay refinement with boundary points on the B-rep faces and curves, ODT smoothing, Klingner–Shewchuk improvement, tet4 and tet10 with mid-side nodes on the B-rep, quality report; one validity criterion shared by the gate and both solvers; the filleted-bracket fillet stress converges, tests/oracle); mesh verify per set | in progress |
|
||||
| M2 Tet meshing and size field | Size field (global, curvature and proximity; edge seeds done: `mesh.seed.edge` on persistent design edge names (`<face>|<face>`) with a count or size, geometric bias and an exact, min or free constraint, shown as ticks and under Mesh › Seeds, honoured by the hex sweep, extrude, revolve, mapped, blocks and cells meshers through interval assignment (conflicting exact seeds named) and by the tet mesher, which divides the edge at the seed's nodes and grades its size field from them; per-face seeds to do; the `fieldcraft_mesh::size::SizeField` trait and edge-division helpers are in; size controls done (#435): `mesh.size.set` on faces, bodies, a box or a sphere with a growth rate (`mesh.size.growth`), saved with the model, listed by `mesh.size.list`, drawn in the viewport, honoured by the tet mesher's graded field, the structured hex meshers' and the block projection's divisions and the octree's depth); Bowyer–Watson Delaunay (exact, symbolic perturbation, verified; `fieldcraft-mesh::delaunay`, done); robust CDT boundary recovery (Diazzi et al. 2023: implicit LNC Steiner points on segments, gift-wrapping facet recovery, inside/outside labelling, B-rep face tags on boundary tet faces, flip repair of rounding (§5.1) checked by the validity gate; `fieldcraft-mesh::cdt` and `mesh.tet.generate`, done: all 20 valid corpus parts); quality stage (done: `fieldcraft-mesh::tet` — curvature-graded size field, edge-collapse coarsening, constrained Delaunay refinement with boundary points on the B-rep faces and curves, ODT smoothing, Klingner–Shewchuk improvement, tet4 and tet10 with mid-side nodes on the B-rep, quality report; one validity criterion shared by the gate and both solvers; the filleted-bracket fillet stress converges, tests/oracle); mesh verify per set | in progress |
|
||||
| M3 First deck | Sets and surfaces bound to SolidCraft face, edge, vertex and body names, resolved after meshing; materials (isotropic and orthotropic elastic, density) with a unit-system label and consistency check; sections (solid, shell, beam profiles) assigned by set; element formulation per region mapped to each solver; BCs (fixed, displacement, symmetry); loads (force and moment on reference points, pressure, gravity); reference points with kinematic and distributing coupling; steps (linear static, frequency) with explicit per-step load and BC tables and output presets; `model.check` naming every missing or bad item; `model.*` and `mesh.tag` commands (model definition: done); writers for Abaqus input (done: `deck.write`), Nastran SOL 101/103 (done: `deck.write`) and LS-DYNA implicit (done); the `.fieldcraft` file (text definition, binary mesh, geometry references reopened on load, journal; atomic saves, autosave and recovery; undo/redo on the journal: done) | in progress |
|
||||
| M4 Own solver and first results | Element library per docs/elements.md (1D and 2D first); Linear static and modal solver (tet4/10, wedge6, hex8, hex20; sparse direct, Lanczos), verified against analytic solutions (solver: done — `fieldcraft-fem` kernels, `fieldcraft-solver` with nested dissection, sparse Cholesky and block Lanczos, `solve.static`/`solve.frequency`/`solve.probe`, patch, cantilever, Lamé, Kirsch and beam-frequency tests; reference points with kinematic and distributing couplings and moments by master–slave constraint elimination, verified by rigid motion, clamping, Saint-Venant tension, bending and tube torsion; 1D elements I: rod2/rod3 trusses, point masses, rotary inertia, springs and dashpots in both solvers and all three decks, verified by the method of joints, the three-bar truss, spring–mass, two-mass chain and torsional pendulum frequencies, and damped decay; 2D elements: plane stress, plane strain and axisymmetric tri3, tri6, quad4 (incompatible modes QM6), quad8 with two degrees of freedom per node, centrifugal loads, `mesh.planar` 2D meshes of polygons and planar faces, verified by patch tests, Kirsch, Lamé (plane strain and axisymmetric), rotating discs and cylinders, and a plane stress cantilever; deck mapping to Abaqus CPS/CPE/CAX, Nastran membranes, LS-DYNA ELFORM 12/13); shells and beams (done: quad4 MITC4 and tri3 MITC3 flat shells with a Hughes–Brezzi drilling rotation, two-node Timoshenko beams with profile properties and orientations, six degrees of freedom at shell and beam nodes shared with solids and trusses, section forces and surface stresses, verified by shell patch tests, Navier's plate, cantilever and portal-frame beams, and the Scordelis–Lo and pinched-cylinder benchmarks); results display (done: `results.*` commands, deformed shape to scale, banded contours of displacement, von Mises, principal and component stresses with a legend, mode shapes with their frequencies and animation, probing a node by clicking it, reaction force and moment sums per set, stale results marked after an edit; explicit steps: field frames by slider or animation with contours per frame, energy histories plotted) | started |
|
||||
| M5 Structured hex | 2D quad meshing of faces (`fieldcraft-mesh::quad`: transfinite mapped meshes, free all-quad meshes of polygons with holes on prescribed boundary nodes, exact planar quad validity, shared `SizeField` and even edge divisions; done for planar faces); mapped hex of six-sided regions (trivariate TFI, curved faces) and sweep meshing (multi-level extrusions with imprinted cross-sections, revolutions, one-to-one sweeps through linking faces; interval assignment, hex8/hex20 on the geometry, B-rep face tags, `mesh.hex.generate`/`sweep`/`mapped`: done, 16 of 21 corpus parts all-hex); conforming interfaces between cells (done: `mesh.hex.blocks` meshes a part split with SolidCraft's Split Body into mapped blocks as one conforming mesh — composite faces for seams and tangent faces, interval assignment over all cells, shared faces meshed once, grading off holes and fillets, free-internal-face check; the filleted bracket in 30 cells, hex20 within 0.1 % of a fine tet10 tip deflection); partitions inside FieldCraft (done: datum planes and axes, `geometry.partition` cuts the open design by a datum plane, kept in the model and replayed when the design opens, the two faces of each cut paired by name); cells meshed each with its own method into one mesh (done: `mesh.cells` — hex where a method fits, else tet; matching interfaces merged node for node, others, such as hex next to tet, tied by dual mortar, verified by a tied hex–tet bar in tension to 1e-6); meshing half a symmetric part and mirroring it (done: `mesh.mirror` reflects hex8/hex20/tet4/tet10/wedge6 across a datum plane, right-handed, nodes on the plane merged, tags carried to the image half; a mirrored half bar equals the whole bar's mesh and bending deflection to 1e-12; samples `crank-arm` and `crank-mirror`: one datum, the half extruded in automatic blocks and mirrored, scaled Jacobian ≥ 0.37; or solving the half: `model.boundary_condition.symmetry` on a datum plane, a half bar with half the load bends as the whole to 1e-11); three-sided sweep regions mapped as triangle primitives (done: midpoint subdivision, dominance constraints in interval assignment); automatic blocks of extruded cross-sections (done: `hex::decompose` — holes separated, O-grids, cuts from concave corners and fillets chosen by block quality, kept only when no worse than the free mesh; nothing added to the model); pyramid transitions; automatic decomposition of 3D parts into sweepable and mappable regions | started |
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
FieldCraft is young; only `main` and the latest release get fixes.
|
||||
|
||||
Please **don't open a public issue** for a security problem. Report it privately through the
|
||||
repository's [Security tab](https://github.com/jyviko/fieldcraft/security/advisories/new) with
|
||||
repository's [Security tab](https://github.com/tangency-labs/fieldcraft/security/advisories/new) with
|
||||
what you found, how to reproduce it (a model, deck or script), the version and platform, and the
|
||||
impact you expect.
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "fieldcraft-cli"
|
||||
description = "FieldCraft from the command line: list commands, run scripts, serve MCP"
|
||||
description = "FieldCraft from the command line: list commands, run scripts, draw snapshots, serve MCP"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
@@ -10,10 +10,13 @@ workspace = true
|
||||
|
||||
[dependencies]
|
||||
fieldcraft-engine = { workspace = true }
|
||||
fieldcraft-gpu = { workspace = true }
|
||||
fieldcraft-render = { workspace = true }
|
||||
fieldcraft-io = { workspace = true }
|
||||
fieldcraft-mcp = { workspace = true }
|
||||
fieldcraft-update = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
png = { workspace = true }
|
||||
fieldcraft-update = { workspace = true, features = ["mock"] }
|
||||
@@ -1,5 +1,6 @@
|
||||
//! fieldcraft-cli commands
|
||||
//! fieldcraft-cli run <script.json>
|
||||
//! fieldcraft-cli snapshot <script.json> --out shot.png [--view V | --camera JSON] [--size WxH] [--section x|y|z[=V]] [--field F]
|
||||
//! fieldcraft-cli features
|
||||
//! fieldcraft-cli mcp [--connect ADDR | --headless] [--automation-read-root DIR] [--automation-write-root DIR]
|
||||
//! fieldcraft-cli crashes [list [--json] | show <id> | replay <id> | dir | clear [<id>]]
|
||||
@@ -9,6 +10,7 @@
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
mod crashes;
|
||||
mod snapshot;
|
||||
mod update;
|
||||
|
||||
use std::path::Path;
|
||||
@@ -23,6 +25,19 @@ usage:
|
||||
fieldcraft-cli commands --json the same as JSON, each command's parameters as JSON Schema
|
||||
fieldcraft-cli run <script.json> run a script (a JSON array of {\"command\", \"params\"})
|
||||
and print the model summary
|
||||
fieldcraft-cli snapshot <script.json> --out FILE.png [--view V | --camera JSON]
|
||||
[--size WxH] [--section x|y|z[=VALUE]] [--field FIELD]
|
||||
run a script and draw the model to a PNG with no
|
||||
window: the viewport's GPU pipeline on a hardware or
|
||||
software GPU, else the CPU rasterizer (FIELDCRAFT_GPU=
|
||||
auto|hardware|software|cpu); V: iso (default), x, y, z,
|
||||
front, back, left, right, top, bottom; size 1280x800;
|
||||
--section hides the elements beyond a plane (at the
|
||||
mesh's middle, or VALUE); --field contours a nodal
|
||||
field of the solved step (results.contour);
|
||||
--camera '{\"yaw\":30,\"pitch\":20,\"target\":...,
|
||||
\"fit\":true,\"fov\":35}' aims as MCP screenshot's camera
|
||||
(default target: the model's centre of mass)
|
||||
fieldcraft-cli features print docs/features.md (every capability, ready or
|
||||
coming soon) from the capability catalogue
|
||||
fieldcraft-cli mcp [options] serve the Model Context Protocol on stdin/stdout
|
||||
@@ -67,6 +82,7 @@ fn main() -> ExitCode {
|
||||
Some("mcp") => mcp(args.get(1..).unwrap_or(&[])),
|
||||
Some("crashes") => crashes::run(args.get(1..).unwrap_or(&[])),
|
||||
Some("update") => update::run(args.get(1..).unwrap_or(&[])),
|
||||
Some("snapshot") => snapshot::run(args.get(1..).unwrap_or(&[])),
|
||||
Some("run") => args.get(1).ok_or_else(|| format!("run needs a script\n\n{USAGE}")).and_then(|p| run(p)),
|
||||
Some("-V" | "--version") => {
|
||||
println!("fieldcraft-cli {}", env!("CARGO_PKG_VERSION"));
|
||||
@@ -135,7 +151,14 @@ fn mcp(args: &[String]) -> Result<(), String> {
|
||||
server.serve(std::io::stdin().lock(), std::io::stdout().lock()).map_err(|e| format!("mcp: {e}"))
|
||||
}
|
||||
|
||||
fn run(path: &str) -> Result<(), String> {
|
||||
/// The JSON report of a picture: what drew it and what it shows (the MCP `screenshot` reply's
|
||||
/// fields).
|
||||
pub fn picture_report(r: &fieldcraft_gpu::Renderer, shown: &fieldcraft_engine::view::Shown) -> serde_json::Value {
|
||||
fieldcraft_mcp::picture_report(r, shown)
|
||||
}
|
||||
|
||||
/// The session after running the script at `path`.
|
||||
fn session_of(path: &str) -> Result<Session, String> {
|
||||
let size = std::fs::metadata(path).map_err(|e| format!("{path}: {e}"))?.len();
|
||||
if size > MAX_SCRIPT_BYTES {
|
||||
return Err(format!("{path}: larger than {} MB", MAX_SCRIPT_BYTES / (1024 * 1024)));
|
||||
@@ -144,6 +167,11 @@ fn run(path: &str) -> Result<(), String> {
|
||||
let script: serde_json::Value = serde_json::from_str(&text).map_err(|e| format!("{path}: {e}"))?;
|
||||
let mut session = Session::default();
|
||||
session.run_script(&script).map_err(|e| e.to_string())?;
|
||||
Ok(session)
|
||||
}
|
||||
|
||||
fn run(path: &str) -> Result<(), String> {
|
||||
let mut session = session_of(path)?;
|
||||
let info = session.execute("model.info", &json!({})).map_err(|e| e.to_string())?;
|
||||
println!("{}", serde_json::to_string_pretty(&info).map_err(|e| e.to_string())?);
|
||||
Ok(())
|
||||
|
||||
@@ -0,0 +1,261 @@
|
||||
//! `fieldcraft-cli snapshot`: a script in, a PNG out, with no window and no display. The picture
|
||||
//! is the viewport's: the engine's scene ([`fieldcraft_engine::view::frame`]) drawn with the
|
||||
//! window's GPU pipelines ([`fieldcraft_gpu::capture`]) on a hardware GPU or a software one
|
||||
//! (lavapipe, llvmpipe, WARP), and by the CPU rasterizer as the last tier (`FIELDCRAFT_GPU`,
|
||||
//! [`fieldcraft_gpu::select`]).
|
||||
|
||||
use fieldcraft_engine::view::{self, Aim, Palette, Section, Target};
|
||||
use fieldcraft_gpu::Renderer;
|
||||
use fieldcraft_gpu::capture::{self, Capturer};
|
||||
use fieldcraft_gpu::select::Policy;
|
||||
use fieldcraft_render::{Orientation, StandardView};
|
||||
|
||||
/// The picture size when `--size` is not given.
|
||||
const DEFAULT_SIZE: (u32, u32) = (1280, 800);
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Options {
|
||||
pub script: String,
|
||||
pub out: String,
|
||||
pub view: StandardView,
|
||||
/// A camera aimed as the MCP `screenshot`'s (`--camera JSON`; not with `--view`).
|
||||
pub camera: Option<Aim>,
|
||||
pub size: (u32, u32),
|
||||
/// A section through the mesh (`--section x|y|z[=VALUE]`).
|
||||
pub section: Option<Section>,
|
||||
/// A nodal field contoured on the last solved step (`--field von_mises`).
|
||||
pub field: Option<String>,
|
||||
}
|
||||
|
||||
/// A view name: a standard view, or the axis looked along (`x`: from the right, `y`: from the
|
||||
/// front, `z`: from the top).
|
||||
fn view_of(s: &str) -> Option<StandardView> {
|
||||
match s.to_ascii_lowercase().as_str() {
|
||||
"x" => Some(StandardView::Right),
|
||||
"y" => Some(StandardView::Front),
|
||||
"z" => Some(StandardView::Top),
|
||||
other => StandardView::parse(other),
|
||||
}
|
||||
}
|
||||
|
||||
/// `WxH`, each side 1..=the renderer's limit.
|
||||
fn size_of(s: &str) -> Option<(u32, u32)> {
|
||||
let (w, h) = s.split_once(['x', 'X'])?;
|
||||
let (w, h) = (w.trim().parse::<u32>().ok()?, h.trim().parse::<u32>().ok()?);
|
||||
let max = fieldcraft_gpu::capture::MAX_SIDE;
|
||||
((1..=max).contains(&w) && (1..=max).contains(&h) && u64::from(w) * u64::from(h) <= fieldcraft_gpu::capture::MAX_PIXELS).then_some((w, h))
|
||||
}
|
||||
|
||||
/// The options: the script, then each flag once, as `--flag value` or `--flag=value`.
|
||||
pub fn options(args: &[String]) -> Result<Options, String> {
|
||||
let (mut script, mut out, mut view, mut size, mut section, mut field, mut camera) = (None, None, None, None, None, None, None);
|
||||
let mut it = args.iter();
|
||||
while let Some(arg) = it.next() {
|
||||
if !arg.starts_with("--") {
|
||||
if script.replace(arg.clone()).is_some() {
|
||||
return Err(format!("snapshot takes one script, not `{arg}` too"));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let (flag, inline) = match arg.split_once('=') {
|
||||
Some((f, v)) => (f, Some(v.to_string())),
|
||||
None => (arg.as_str(), None),
|
||||
};
|
||||
let value = inline.or_else(|| it.next().cloned()).filter(|v| !v.is_empty()).ok_or_else(|| format!("{flag} needs a value"))?;
|
||||
let twice = match flag {
|
||||
"--out" => out.replace(value).is_some(),
|
||||
"--view" => view
|
||||
.replace(
|
||||
view_of(&value).ok_or_else(|| format!("--view: unknown view `{value}` (iso, x, y, z, front, back, left, right, top, bottom)"))?,
|
||||
)
|
||||
.is_some(),
|
||||
"--size" => size
|
||||
.replace(size_of(&value).ok_or_else(|| {
|
||||
format!(
|
||||
"--size: `{value}` is not WxH with each side 1..={} and at most {} pixels",
|
||||
fieldcraft_gpu::capture::MAX_SIDE,
|
||||
fieldcraft_gpu::capture::MAX_PIXELS
|
||||
)
|
||||
})?)
|
||||
.is_some(),
|
||||
"--section" => section
|
||||
.replace(Section::parse(&value).ok_or_else(|| format!("--section: `{value}` is not x, y, z or AXIS=VALUE (x=12.5)"))?)
|
||||
.is_some(),
|
||||
"--field" => field.replace(value).is_some(),
|
||||
"--camera" => {
|
||||
let v: serde_json::Value = serde_json::from_str(&value).map_err(|e| format!("--camera: not JSON: {e}"))?;
|
||||
camera.replace(view::camera(&v).map_err(|e| format!("--camera: {e}"))?).is_some()
|
||||
}
|
||||
other => return Err(format!("snapshot: unknown option `{other}`")),
|
||||
};
|
||||
if twice {
|
||||
return Err(format!("{flag} is given twice"));
|
||||
}
|
||||
}
|
||||
if view.is_some() && camera.is_some() {
|
||||
return Err("give --view or --camera, not both".into());
|
||||
}
|
||||
Ok(Options {
|
||||
script: script.ok_or("snapshot needs a script")?,
|
||||
out: out.ok_or("snapshot needs --out FILE.png")?,
|
||||
view: view.unwrap_or(StandardView::Iso),
|
||||
camera,
|
||||
size: size.unwrap_or(DEFAULT_SIZE),
|
||||
section,
|
||||
field,
|
||||
})
|
||||
}
|
||||
|
||||
/// The PNG of the session `o` describes, drawn as `policy` says ([`capture::png`]).
|
||||
pub fn picture(o: &Options, policy: Policy) -> Result<(Vec<u8>, Renderer, view::Shown), String> {
|
||||
let mut session = crate::session_of(&o.script)?;
|
||||
if let Some(field) = &o.field {
|
||||
session.execute("results.contour", &serde_json::json!({"field": field})).map_err(|e| format!("--field: {e}"))?;
|
||||
}
|
||||
let (w, h) = o.size;
|
||||
let f = match &o.camera {
|
||||
Some(aim) => view::frame_at(&session, aim, w as usize, h as usize, &Palette::LIGHT, o.section).map_err(|e| e.to_string())?,
|
||||
None => view::frame(&session, &Orientation::of(o.view), w as usize, h as usize, &Palette::LIGHT, o.section),
|
||||
};
|
||||
let capturer = Capturer::open(policy);
|
||||
let (png, renderer) = capture::png(&capturer, policy, &f.scene, &[&f.symbols], &f.cam, w, h).map_err(|e| e.to_string())?;
|
||||
Ok((png, renderer, f.shown))
|
||||
}
|
||||
|
||||
pub fn run(args: &[String]) -> Result<(), String> {
|
||||
let o = options(args)?;
|
||||
let policy = Policy::from_env().map_err(|e| e.to_string())?;
|
||||
let (png, renderer, shown) = picture(&o, policy)?;
|
||||
std::fs::write(&o.out, &png).map_err(|e| format!("{}: {e}", o.out))?;
|
||||
let mut report = crate::picture_report(&renderer, &shown);
|
||||
if let Some(r) = report.as_object_mut() {
|
||||
r.insert("out".into(), o.out.clone().into());
|
||||
r.insert("width".into(), o.size.0.into());
|
||||
r.insert("height".into(), o.size.1.into());
|
||||
match &o.camera {
|
||||
Some(aim) => r.insert("camera".into(), Aim { target: Some(aim.target.clone().unwrap_or(Target::MODEL)), ..aim.clone() }.json()),
|
||||
None => r.insert("view".into(), o.view.label().into()),
|
||||
};
|
||||
}
|
||||
println!("{report}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn opts(args: &[&str]) -> Result<Options, String> {
|
||||
options(&args.iter().map(|s| s.to_string()).collect::<Vec<_>>())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn options_are_checked() {
|
||||
let o = opts(&["s.json", "--out", "a.png"]).unwrap();
|
||||
assert_eq!(
|
||||
o,
|
||||
Options {
|
||||
script: "s.json".into(),
|
||||
out: "a.png".into(),
|
||||
view: StandardView::Iso,
|
||||
camera: None,
|
||||
size: DEFAULT_SIZE,
|
||||
section: None,
|
||||
field: None
|
||||
}
|
||||
);
|
||||
let o = opts(&["s", "--out", "o", "--section", "y=2.5", "--field", "displacement"]).unwrap();
|
||||
assert_eq!((o.section, o.field.as_deref()), (Some(Section { axis: 1, at: Some(2.5) }), Some("displacement")));
|
||||
assert_eq!(opts(&["s", "--out", "o", "--section=Z"]).unwrap().section, Some(Section { axis: 2, at: None }));
|
||||
let o = opts(&["--view=z", "s.json", "--size", "640x480", "--out=b.png"]).unwrap();
|
||||
assert_eq!((o.view, o.size), (StandardView::Top, (640, 480)));
|
||||
assert_eq!(opts(&["s", "--out", "o", "--view", "X"]).unwrap().view, StandardView::Right);
|
||||
assert_eq!(opts(&["s", "--out", "o", "--view", "front"]).unwrap().view, StandardView::Front);
|
||||
let o = opts(&["s", "--out", "o", "--camera", r#"{"yaw": 30, "pitch": 10, "target": [1, 2, 3], "fit": true}"#]).unwrap();
|
||||
assert_eq!(o.camera.and_then(|a| a.target), Some(Target::Point(fieldcraft_render::Vec3::new(1.0, 2.0, 3.0))));
|
||||
for bad in [
|
||||
vec![],
|
||||
vec!["s.json"],
|
||||
vec!["--out", "a.png"],
|
||||
vec!["s", "t", "--out", "o"],
|
||||
vec!["s", "--out"],
|
||||
vec!["s", "--out", "o", "--out", "p"],
|
||||
vec!["s", "--out", "o", "--view", "diagonal"],
|
||||
vec!["s", "--out", "o", "--size", "0x10"],
|
||||
vec!["s", "--out", "o", "--size", "10"],
|
||||
vec!["s", "--out", "o", "--size", "99999x10"],
|
||||
vec!["s", "--out", "o", "--size", "8192x8192"],
|
||||
vec!["s", "--out", "o", "--size", "-5x10"],
|
||||
vec!["s", "--out", "o", "--nope", "1"],
|
||||
vec!["s", "--out", "o", "--section", "w"],
|
||||
vec!["s", "--out", "o", "--section", "x=inf"],
|
||||
vec!["s", "--out", "o", "--section", "x=", "--field", "f"],
|
||||
vec!["s", "--out", "o", "--camera", "{yaw: 1}"],
|
||||
vec!["s", "--out", "o", "--camera", r#"{"yaw": 0}"#],
|
||||
vec!["s", "--out", "o", "--camera", r#"{"yaw": 0, "pitch": 0}"#, "--view", "x"],
|
||||
] {
|
||||
assert!(opts(&bad).is_err(), "{bad:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// PNG width, height and pixels (RGBA).
|
||||
fn decode(png: &[u8]) -> (u32, u32, Vec<u8>) {
|
||||
let mut r = png::Decoder::new(std::io::Cursor::new(png)).read_info().unwrap();
|
||||
let mut buf = vec![0; r.output_buffer_size().unwrap()];
|
||||
let info = r.next_frame(&mut buf).unwrap();
|
||||
buf.truncate(info.buffer_size());
|
||||
(info.width, info.height, buf)
|
||||
}
|
||||
|
||||
fn script(dir: &std::path::Path, name: &str, solve: bool) -> String {
|
||||
let mut steps = fieldcraft_engine::sample::cantilever();
|
||||
if solve && let Some(a) = steps.as_array_mut() {
|
||||
a.push(serde_json::json!({"command": "solve.static", "params": {}}));
|
||||
}
|
||||
let path = dir.join(name);
|
||||
std::fs::write(&path, steps.to_string()).unwrap();
|
||||
path.to_string_lossy().into()
|
||||
}
|
||||
|
||||
fn opts_for(script: String) -> Options {
|
||||
Options { script, out: String::new(), view: StandardView::Iso, camera: None, size: (320, 200), section: None, field: None }
|
||||
}
|
||||
|
||||
/// The cantilever drawn headless: a PNG of the size asked for, with the model in it (pixels
|
||||
/// of the hex colour) and its element edges (dark pixels). `auto` draws on the GPU when the
|
||||
/// machine has one and on the CPU otherwise (it must be the GPU under `FIELDCRAFT_REQUIRE_GPU=1`); `cpu` always
|
||||
/// on the CPU, and both pictures show the same model. A section and a field change it.
|
||||
#[test]
|
||||
fn snapshots_the_model_and_its_mesh_headless() {
|
||||
let dir = std::env::temp_dir().join(format!("fieldcraft-snapshot-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let o = opts_for(script(&dir, "cantilever.json", false));
|
||||
for policy in [Policy::Auto, Policy::Cpu] {
|
||||
let (png, drawn, _) = picture(&o, policy).unwrap();
|
||||
let (w, h, px) = decode(&png);
|
||||
assert_eq!((w, h), (320, 200), "{drawn:?}");
|
||||
let hex = Palette::LIGHT.hex;
|
||||
let near = |p: &[u8], c: fieldcraft_render::Rgb, d: u8| p[0].abs_diff(c.0) <= d && p[1].abs_diff(c.1) <= d && p[2].abs_diff(c.2) <= d;
|
||||
// Shaded faces vary in brightness around the hex colour; the edges are near black.
|
||||
let faces = px.chunks(4).filter(|p| near(p, hex, 60) && p[2] > p[0]).count();
|
||||
let edges = px.chunks(4).filter(|p| p[0] < 90 && p[1] < 90 && p[2] < 90).count();
|
||||
assert!(faces > 500, "{drawn:?}: {faces} face pixels");
|
||||
assert!(edges > 100, "{drawn:?}: {edges} edge pixels");
|
||||
if policy == Policy::Cpu {
|
||||
assert_eq!(drawn, Renderer::Cpu { why: None });
|
||||
} else if fieldcraft_gpu::select::gpu_required() {
|
||||
assert!(matches!(drawn, Renderer::Gpu { .. }), "FIELDCRAFT_REQUIRE_GPU=1 but drawn by {drawn:?}");
|
||||
}
|
||||
}
|
||||
// A section along the beam draws half of it.
|
||||
let whole = picture(&o, Policy::Cpu).unwrap().0;
|
||||
let cut = picture(&Options { section: Some(Section { axis: 0, at: None }), ..o.clone() }, Policy::Cpu).unwrap().0;
|
||||
assert_ne!(whole, cut);
|
||||
// A field needs a solved step; on a solved one it is the contour reported.
|
||||
assert!(picture(&Options { field: Some("von_mises".into()), ..o.clone() }, Policy::Cpu).is_err());
|
||||
let solved = Options { field: Some("displacement".into()), ..opts_for(script(&dir, "solved.json", true)) };
|
||||
let (_, _, shown) = picture(&solved, Policy::Auto).unwrap();
|
||||
assert_eq!(shown.contour.map(|c| c.0).as_deref(), Some("displacement"));
|
||||
std::fs::remove_dir_all(&dir).ok();
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,10 @@ fn cli(host: &str, args: &[&str]) -> std::process::Output {
|
||||
#[test]
|
||||
fn update_check_reports_the_latest_release() {
|
||||
let server = Server::start().unwrap();
|
||||
server.release("latest", &json!({"tag_name": "v99.0.0", "html_url": "https://github.com/jyviko/fieldcraft/releases/tag/v99.0.0", "assets": []}));
|
||||
server.release(
|
||||
"latest",
|
||||
&json!({"tag_name": "v99.0.0", "html_url": "https://github.com/tangency-labs/fieldcraft/releases/tag/v99.0.0", "assets": []}),
|
||||
);
|
||||
let out = cli(&server.url(), &["update", "--check"]);
|
||||
assert!(out.status.success(), "{out:?}");
|
||||
let report: Value = serde_json::from_slice(&out.stdout).unwrap();
|
||||
|
||||
@@ -31,4 +31,4 @@ wasm-bindgen-futures = "0.4"
|
||||
js-sys = "0.3"
|
||||
web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlElement", "HtmlCanvasElement", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "Url", "Location", "DomTokenList", "CssStyleDeclaration", "Event", "EventTarget", "BeforeUnloadEvent", "Storage", "Crypto", "Headers", "Request", "RequestInit", "RequestCache", "Response"] }
|
||||
# Autosaves kept in the browser's local storage, which holds text.
|
||||
base64 = "0.22"
|
||||
base64 = "0.23"
|
||||
@@ -9,15 +9,15 @@
|
||||
<meta property="og:site_name" content="FieldCraft">
|
||||
<meta property="og:title" content="FieldCraft">
|
||||
<meta property="og:description" content="Open-source CAE built in Rust: structures, heat, fluids, and MCP for AI agents.">
|
||||
<meta property="og:url" content="https://jyviko.github.io/fieldcraft/">
|
||||
<meta property="og:image" content="https://jyviko.github.io/fieldcraft/og.png">
|
||||
<meta property="og:url" content="https://tangency-labs.github.io/fieldcraft/">
|
||||
<meta property="og:image" content="https://tangency-labs.github.io/fieldcraft/og.png">
|
||||
<meta property="og:image:width" content="1280">
|
||||
<meta property="og:image:height" content="640">
|
||||
<meta property="og:image:alt" content="FieldCraft: Open-source CAE built in Rust">
|
||||
<meta name="twitter:card" content="summary_large_image">
|
||||
<meta name="twitter:title" content="FieldCraft">
|
||||
<meta name="twitter:description" content="Open-source CAE built in Rust: structures, heat, fluids, and MCP for AI agents.">
|
||||
<meta name="twitter:image" content="https://jyviko.github.io/fieldcraft/og.png">
|
||||
<meta name="twitter:image" content="https://tangency-labs.github.io/fieldcraft/og.png">
|
||||
<link data-trunk rel="rust" data-bin="fieldcraft-web" data-wasm-opt="z" data-weak-refs />
|
||||
<link data-trunk rel="icon" type="image/svg+xml" href="../../assets/app-icon/icon-small.svg" />
|
||||
<link data-trunk rel="copy-file" href="../../assets/app-icon/icon.iconset/icon_128x128@2x.png" data-target-path="icons" />
|
||||
|
||||
@@ -6,8 +6,18 @@ edition.workspace = true
|
||||
license.workspace = true
|
||||
publish = false
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
# The workspace lints, except unsafe_code: denied, not forbidden, so that src/native_crash.rs
|
||||
# (the Windows exception filter, which has no safe API) can allow it. Keep the rest in sync with
|
||||
# [workspace.lints].
|
||||
[lints.rust]
|
||||
unsafe_code = "deny"
|
||||
|
||||
[lints.clippy]
|
||||
dbg_macro = "warn"
|
||||
todo = "warn"
|
||||
too_many_arguments = "allow"
|
||||
type_complexity = "allow"
|
||||
neg_cmp_op_on_partial_ord = "allow"
|
||||
|
||||
[dependencies]
|
||||
fieldcraft-engine = { workspace = true }
|
||||
@@ -23,7 +33,27 @@ log = { workspace = true }
|
||||
# operating system's certificates (no bundled root list), and a random install id.
|
||||
ureq = { version = "3.4", default-features = false, features = ["rustls-no-provider", "platform-verifier"] }
|
||||
rustls = { version = "0.23", default-features = false, features = ["ring", "std"] }
|
||||
getrandom = "0.3"
|
||||
getrandom = "0.4"
|
||||
|
||||
# A termination signal (SIGTERM, SIGINT) closes the window the normal way (terminate.rs).
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
signal-hook = "0.4"
|
||||
|
||||
# The exception filter and minidump of a native crash (src/native_crash.rs).
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows-sys = { version = "0.61", features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_Security",
|
||||
"Win32_Storage_FileSystem",
|
||||
"Win32_System_Diagnostics_Debug",
|
||||
# WriteFile's OVERLAPPED.
|
||||
"Win32_System_IO",
|
||||
"Win32_System_Kernel",
|
||||
"Win32_System_LibraryLoader",
|
||||
# MiniDumpWriteDump.
|
||||
"Win32_System_Memory",
|
||||
"Win32_System_Threading",
|
||||
] }
|
||||
|
||||
[build-dependencies]
|
||||
# The Windows executable's icon resource (build.rs).
|
||||
@@ -71,7 +101,7 @@ assets = [
|
||||
[package.metadata.generate-rpm]
|
||||
name = "fieldcraft"
|
||||
license = "MIT OR Apache-2.0"
|
||||
url = "https://github.com/jyviko/fieldcraft"
|
||||
url = "https://github.com/tangency-labs/fieldcraft"
|
||||
assets = [
|
||||
{ source = "target/release/fieldcraft", dest = "/usr/bin/fieldcraft", mode = "755" },
|
||||
{ source = "target/release/fieldcraft-cli", dest = "/usr/bin/fieldcraft-cli", mode = "755" },
|
||||
|
||||
@@ -9,9 +9,12 @@
|
||||
//! `fieldcraft_ui_egui::control` and docs/control-protocol.md.
|
||||
#![cfg_attr(all(target_os = "windows", not(debug_assertions)), windows_subsystem = "windows")]
|
||||
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
||||
#![forbid(unsafe_code)]
|
||||
// Unsafe code is denied (Cargo.toml): only the Windows exception filter allows it (native_crash.rs).
|
||||
|
||||
mod control_server;
|
||||
#[cfg(windows)]
|
||||
mod native_crash;
|
||||
mod terminate;
|
||||
mod update;
|
||||
mod usage;
|
||||
|
||||
@@ -49,8 +52,8 @@ struct App {
|
||||
autosaver: Option<Autosaver>,
|
||||
/// App time of the last autosave check.
|
||||
autosaved_at: f64,
|
||||
/// The first frame was drawn (the crash journal says the app is up).
|
||||
running: bool,
|
||||
/// Frames drawn so far, up to 2 (the second means the first was presented: the app is up).
|
||||
frames: u8,
|
||||
/// Sends the anonymous usage events, when this run may (docs/privacy.md).
|
||||
usage: Option<usage::Sender>,
|
||||
}
|
||||
@@ -67,9 +70,13 @@ impl App {
|
||||
|
||||
impl eframe::App for App {
|
||||
fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
if !self.running {
|
||||
self.running = true;
|
||||
crash::activity("running");
|
||||
if self.frames < 2 {
|
||||
self.frames += 1;
|
||||
// A second frame right away, which means the first was presented: the app is up.
|
||||
ctx.request_repaint();
|
||||
if self.frames == 2 {
|
||||
crash::phase(crash::RUNNING);
|
||||
}
|
||||
}
|
||||
self.app.logic(ctx);
|
||||
if self.app.now - self.autosaved_at >= AUTOSAVE_SECONDS {
|
||||
@@ -162,10 +169,18 @@ fn start_reporting() -> Vec<crash::Summary> {
|
||||
b.version_line(),
|
||||
crash::default_dir().map(|d| d.display().to_string()).unwrap_or_default()
|
||||
);
|
||||
crash::activity("startup");
|
||||
crashed
|
||||
}
|
||||
|
||||
/// egui-wgpu is about to start a step of getting the graphics device and the window's surface
|
||||
/// up: the crash journal keeps it, and the adapter once it is picked (#299).
|
||||
fn startup_step(step: &'static str, detail: &str) {
|
||||
if step == eframe::egui_wgpu::STEP_ADAPTER_CHOSEN {
|
||||
crash::set_environment(&format!("Renderer: {detail}"));
|
||||
}
|
||||
crash::phase(step);
|
||||
}
|
||||
|
||||
/// Command-line options.
|
||||
#[derive(Debug, Default, PartialEq)]
|
||||
struct Args {
|
||||
@@ -223,6 +238,12 @@ fn main() -> eframe::Result {
|
||||
fieldcraft_io::recovery::use_automation_dir();
|
||||
}
|
||||
let crashed = start_reporting();
|
||||
// A crash in native code (a graphics driver) says where it happened: the exception and the
|
||||
// module on Windows, and on every system the startup step the app was in.
|
||||
#[cfg(windows)]
|
||||
native_crash::install();
|
||||
eframe::egui_wgpu::set_startup_hook(startup_step);
|
||||
crash::phase("window");
|
||||
let options = eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default()
|
||||
.with_title("FieldCraft")
|
||||
@@ -245,6 +266,7 @@ fn main() -> eframe::Result {
|
||||
"FieldCraft",
|
||||
options,
|
||||
Box::new(move |cc| {
|
||||
crash::phase("app setup");
|
||||
let mut session = Session::default();
|
||||
// The window's model: its commands go into the crash journal.
|
||||
session.crash_journal = crash::enabled();
|
||||
@@ -265,7 +287,21 @@ fn main() -> eframe::Result {
|
||||
if let Some(rs) = &cc.wgpu_render_state {
|
||||
let info = rs.adapter.get_info();
|
||||
log::info!("graphics {:?} {} ({}), MSAA {}x (requested {MSAA}x)", info.backend, info.name, info.driver, rs.msaa_samples);
|
||||
crash::set_environment(&format!("Renderer: {:?} {} ({}), MSAA {}x", info.backend, info.name, info.driver, rs.msaa_samples));
|
||||
crash::phase("viewport pipelines (shader compile)");
|
||||
app.set_wgpu(rs, DEPTH_BITS, rs.msaa_samples, &cc.egui_ctx);
|
||||
// The graphics driver is loaded now; it may have put its own exception filter
|
||||
// in front of ours.
|
||||
#[cfg(windows)]
|
||||
native_crash::reinstall();
|
||||
}
|
||||
// `kill` or Ctrl-C closes the window as Close does: autosaved, no crash (#248).
|
||||
let ctx = cc.egui_ctx.clone();
|
||||
if let Err(e) = terminate::watch(move || {
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
ctx.request_repaint();
|
||||
}) {
|
||||
log::warn!("termination signals: {e}");
|
||||
}
|
||||
if let Some(port) = args.control {
|
||||
app = app.with_control(control_server::start(port, cc.egui_ctx.clone()));
|
||||
@@ -285,7 +321,8 @@ fn main() -> eframe::Result {
|
||||
Autosaver::new(&dir).map_err(|e| log::warn!("autosave unavailable: {e}")).ok()
|
||||
});
|
||||
app.announce_crashes(&crashed);
|
||||
Ok(Box::new(App { app, autosaver, autosaved_at: 0.0, running: false, usage }))
|
||||
crash::phase("first frame");
|
||||
Ok(Box::new(App { app, autosaver, autosaved_at: 0.0, frames: 0, usage }))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,290 @@
|
||||
//! Windows: what a crash in native code leaves behind (docs/crash-reports.md, #299).
|
||||
//!
|
||||
//! A crash inside a graphics driver or another DLL (an access violation, a stack overflow) is no
|
||||
//! Rust panic: the panic hook never runs and the next start only found "an unclean exit". An
|
||||
//! unhandled-exception filter now writes, before the process goes, a small JSON file with the
|
||||
//! exception code, the faulting module and the offset into it, and a minidump, next to the run's
|
||||
//! crash journal (`fieldcraft_engine::crash::native_paths`). The next start puts both into the
|
||||
//! unclean exit's report, with the startup step the journal says the app was in. Everything that
|
||||
//! can be tested without Windows (reading the file, the report, the startup steps) lives in
|
||||
//! `fieldcraft_engine::crash` and is tested there.
|
||||
//!
|
||||
//! Installing an exception filter and asking the system which module holds an address have no
|
||||
//! safe API, hence the `unsafe_code` allowance on this module, the only one in FieldCraft (the
|
||||
//! app crate denies unsafe code instead of forbidding it for this). The filter runs on a thread
|
||||
//! that may have a broken heap or almost no stack left: it doesn't allocate, formats into a buffer
|
||||
//! on the stack, and skips the minidump after a stack overflow.
|
||||
//!
|
||||
//! Adapted from SolidCraft's `apps/solidcraft/src/native_crash.rs` (MIT OR Apache-2.0), see
|
||||
//! ATTRIBUTION.md.
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
use std::fmt::Write as _;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::path::Path;
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
|
||||
use windows_sys::Win32::Foundation::{CloseHandle, GENERIC_WRITE, HANDLE, HMODULE, INVALID_HANDLE_VALUE};
|
||||
use windows_sys::Win32::Storage::FileSystem::{CREATE_ALWAYS, CreateFileW, FILE_ATTRIBUTE_NORMAL, WriteFile};
|
||||
use windows_sys::Win32::System::Diagnostics::Debug::{
|
||||
EXCEPTION_CONTINUE_SEARCH, EXCEPTION_POINTERS, MINIDUMP_EXCEPTION_INFORMATION, MiniDumpNormal, MiniDumpWithThreadInfo,
|
||||
MiniDumpWithUnloadedModules, MiniDumpWriteDump, SetUnhandledExceptionFilter,
|
||||
};
|
||||
use windows_sys::Win32::System::LibraryLoader::{
|
||||
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, GetModuleFileNameW, GetModuleHandleExW,
|
||||
};
|
||||
use windows_sys::Win32::System::Threading::{GetCurrentProcess, GetCurrentProcessId, GetCurrentThreadId};
|
||||
|
||||
/// Where the filter writes (NUL-terminated UTF-16), set once by [`install`].
|
||||
struct Paths {
|
||||
info: Vec<u16>,
|
||||
dump: Vec<u16>,
|
||||
}
|
||||
|
||||
static PATHS: OnceLock<Paths> = OnceLock::new();
|
||||
/// The filter that was installed before ours (called after ours), as an address.
|
||||
static PREVIOUS: AtomicUsize = AtomicUsize::new(0);
|
||||
/// The filter has started writing (a second exception while it does is left alone).
|
||||
static BUSY: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
const STATUS_ACCESS_VIOLATION: u32 = 0xC000_0005;
|
||||
const STATUS_IN_PAGE_ERROR: u32 = 0xC000_0006;
|
||||
const STATUS_STACK_OVERFLOW: u32 = 0xC000_00FD;
|
||||
|
||||
/// Catch native crashes from now on (after `fieldcraft_engine::crash::install`; nothing without it).
|
||||
pub fn install() {
|
||||
let Some((info, dump)) = fieldcraft_engine::crash::native_paths() else { return };
|
||||
let wide = |p: &Path| p.as_os_str().encode_wide().chain([0]).collect::<Vec<u16>>();
|
||||
if PATHS.set(Paths { info: wide(&info), dump: wide(&dump) }).is_ok() {
|
||||
set_filter();
|
||||
}
|
||||
}
|
||||
|
||||
/// Put the filter back on top: a DLL loaded since (a graphics driver) may have set its own,
|
||||
/// which is then called after ours.
|
||||
pub fn reinstall() {
|
||||
if PATHS.get().is_some() {
|
||||
set_filter();
|
||||
}
|
||||
}
|
||||
|
||||
fn set_filter() {
|
||||
// SAFETY: `filter` has the signature the system calls it with and stays valid for the
|
||||
// process's life.
|
||||
let previous = unsafe { SetUnhandledExceptionFilter(Some(filter)) };
|
||||
if let Some(p) = previous
|
||||
&& p as usize != filter as *const () as usize
|
||||
{
|
||||
PREVIOUS.store(p as usize, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
/// The unhandled-exception filter: record, then let the previous filter (or the system) go on.
|
||||
unsafe extern "system" fn filter(pointers: *const EXCEPTION_POINTERS) -> i32 {
|
||||
if !BUSY.swap(true, Ordering::SeqCst) {
|
||||
// SAFETY: the system passes valid exception pointers for the duration of the call.
|
||||
unsafe { record(pointers) };
|
||||
}
|
||||
let previous = PREVIOUS.load(Ordering::SeqCst);
|
||||
if previous != 0 && previous != filter as *const () as usize {
|
||||
// SAFETY: the address came from `SetUnhandledExceptionFilter` as a filter of this type.
|
||||
let previous: unsafe extern "system" fn(*const EXCEPTION_POINTERS) -> i32 = unsafe { std::mem::transmute(previous) };
|
||||
// SAFETY: as for this filter.
|
||||
return unsafe { previous(pointers) };
|
||||
}
|
||||
EXCEPTION_CONTINUE_SEARCH
|
||||
}
|
||||
|
||||
/// A fixed buffer to format into without allocating (what doesn't fit is cut off).
|
||||
struct Buf {
|
||||
bytes: [u8; 2048],
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl std::fmt::Write for Buf {
|
||||
fn write_str(&mut self, s: &str) -> std::fmt::Result {
|
||||
let room = self.bytes.len().saturating_sub(self.len);
|
||||
let n = s.len().min(room);
|
||||
if let (Some(to), Some(from)) = (self.bytes.get_mut(self.len..self.len + n), s.as_bytes().get(..n)) {
|
||||
to.copy_from_slice(from);
|
||||
self.len += n;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// The usual names of the exception codes a crash shows.
|
||||
fn code_name(code: u32) -> &'static str {
|
||||
match code {
|
||||
STATUS_ACCESS_VIOLATION => "EXCEPTION_ACCESS_VIOLATION",
|
||||
STATUS_IN_PAGE_ERROR => "EXCEPTION_IN_PAGE_ERROR",
|
||||
STATUS_STACK_OVERFLOW => "EXCEPTION_STACK_OVERFLOW",
|
||||
0xC000_001D => "EXCEPTION_ILLEGAL_INSTRUCTION",
|
||||
0xC000_0094 => "EXCEPTION_INT_DIVIDE_BY_ZERO",
|
||||
0xC000_0096 => "EXCEPTION_PRIV_INSTRUCTION",
|
||||
0xC000_008C => "EXCEPTION_ARRAY_BOUNDS_EXCEEDED",
|
||||
0xC000_0374 => "STATUS_HEAP_CORRUPTION",
|
||||
0xC000_0409 => "STATUS_STACK_BUFFER_OVERRUN",
|
||||
0xC000_0602 => "STATUS_FAIL_FAST_EXCEPTION",
|
||||
0x8000_0003 => "EXCEPTION_BREAKPOINT",
|
||||
0xE06D_7363 => "C++ exception",
|
||||
_ => "exception",
|
||||
}
|
||||
}
|
||||
|
||||
/// Write what `pointers` says into the info file, then the minidump.
|
||||
unsafe fn record(pointers: *const EXCEPTION_POINTERS) {
|
||||
let Some(paths) = PATHS.get() else { return };
|
||||
if pointers.is_null() {
|
||||
return;
|
||||
}
|
||||
// SAFETY: checked non-null; the system keeps it valid during the filter.
|
||||
let rec = unsafe { (*pointers).ExceptionRecord };
|
||||
if rec.is_null() {
|
||||
return;
|
||||
}
|
||||
// SAFETY: as above.
|
||||
let rec = unsafe { &*rec };
|
||||
let code = rec.ExceptionCode as u32;
|
||||
let address = rec.ExceptionAddress as usize;
|
||||
let mut module: HMODULE = std::ptr::null_mut();
|
||||
let mut name = [0u16; 512];
|
||||
// SAFETY: FROM_ADDRESS takes the address in place of a name; UNCHANGED_REFCOUNT means no
|
||||
// handle to release; `name` is as long as we say.
|
||||
let len = unsafe {
|
||||
if GetModuleHandleExW(
|
||||
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
|
||||
address as *const u16,
|
||||
&mut module,
|
||||
) != 0
|
||||
{
|
||||
GetModuleFileNameW(module, name.as_mut_ptr(), name.len() as u32) as usize
|
||||
} else {
|
||||
0
|
||||
}
|
||||
};
|
||||
// SAFETY: no preconditions.
|
||||
let thread = unsafe { GetCurrentThreadId() };
|
||||
let mut b = Buf { bytes: [0; 2048], len: 0 };
|
||||
let _ = write!(b, "{{\"code\":\"0x{code:08X}\",\"name\":\"{}\",\"address\":\"0x{address:016X}\",\"thread\":{thread}", code_name(code));
|
||||
if len > 0 {
|
||||
let _ = write!(b, ",\"offset\":\"0x{:X}\",\"module\":\"", address.wrapping_sub(module as usize));
|
||||
for c in char::decode_utf16(name.iter().take(len).copied()) {
|
||||
let _ = match c.unwrap_or(char::REPLACEMENT_CHARACTER) {
|
||||
'"' => b.write_str("\\\""),
|
||||
'\\' => b.write_str("\\\\"),
|
||||
c if (c as u32) < 0x20 => b.write_str("?"),
|
||||
c => b.write_char(c),
|
||||
};
|
||||
}
|
||||
let _ = b.write_str("\"");
|
||||
}
|
||||
if matches!(code, STATUS_ACCESS_VIOLATION | STATUS_IN_PAGE_ERROR) && rec.NumberParameters >= 2 {
|
||||
let what = match rec.ExceptionInformation[0] {
|
||||
0 => "read of",
|
||||
1 => "write to",
|
||||
8 => "execution at",
|
||||
_ => "access to",
|
||||
};
|
||||
let _ = write!(b, ",\"detail\":\"{what} 0x{:016X}\"", rec.ExceptionInformation[1]);
|
||||
}
|
||||
let _ = b.write_str("}");
|
||||
let bytes = b.bytes.get(..b.len).unwrap_or_default();
|
||||
// SAFETY: NUL-terminated path; the handle is closed below.
|
||||
unsafe {
|
||||
let file = create(&paths.info);
|
||||
if !file.is_null() {
|
||||
let mut written = 0u32;
|
||||
WriteFile(file, bytes.as_ptr(), bytes.len() as u32, &mut written, std::ptr::null_mut());
|
||||
CloseHandle(file);
|
||||
}
|
||||
}
|
||||
// After a stack overflow there is no stack left for writing a minidump.
|
||||
if code == STATUS_STACK_OVERFLOW {
|
||||
return;
|
||||
}
|
||||
// SAFETY: as above; the exception information points at this thread's exception.
|
||||
unsafe {
|
||||
let file = create(&paths.dump);
|
||||
if !file.is_null() {
|
||||
let exception = MINIDUMP_EXCEPTION_INFORMATION { ThreadId: thread, ExceptionPointers: pointers as *mut _, ClientPointers: 0 };
|
||||
MiniDumpWriteDump(
|
||||
GetCurrentProcess(),
|
||||
GetCurrentProcessId(),
|
||||
file,
|
||||
MiniDumpNormal | MiniDumpWithThreadInfo | MiniDumpWithUnloadedModules,
|
||||
&exception,
|
||||
std::ptr::null(),
|
||||
std::ptr::null(),
|
||||
);
|
||||
CloseHandle(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A new file at `path` (NUL-terminated) for writing, or null.
|
||||
unsafe fn create(path: &[u16]) -> HANDLE {
|
||||
// SAFETY: the caller passes a NUL-terminated path.
|
||||
let h = unsafe { CreateFileW(path.as_ptr(), GENERIC_WRITE, 0, std::ptr::null(), CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, std::ptr::null_mut()) };
|
||||
if h == INVALID_HANDLE_VALUE { std::ptr::null_mut() } else { h }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// The child half: install crash reporting and the filter, then fault in native code.
|
||||
#[test]
|
||||
fn child() {
|
||||
let Some(dir) = std::env::var_os("FIELDCRAFT_NATIVE_CRASH_CHILD") else { return };
|
||||
let build = fieldcraft_engine::crash::Build { version: "9.9.9", commit: "abc1234" };
|
||||
let fresh = fieldcraft_engine::crash::install(PathBuf::from(dir), build);
|
||||
println!("FRESH {}", serde_json::to_string(&fresh).unwrap_or_default());
|
||||
if std::env::var_os("FIELDCRAFT_NATIVE_CRASH_FAULT").is_none() {
|
||||
fieldcraft_engine::crash::end_session();
|
||||
return;
|
||||
}
|
||||
super::install();
|
||||
fieldcraft_engine::crash::phase("device creation");
|
||||
// SAFETY: none; this is the crash under test (a read of address 16, as a null struct
|
||||
// pointer's field would be).
|
||||
let v = unsafe { std::ptr::read_volatile(16 as *const u64) };
|
||||
println!("unreachable {v}");
|
||||
}
|
||||
|
||||
fn run(dir: &std::path::Path, fault: bool) -> std::process::Output {
|
||||
let mut c = std::process::Command::new(std::env::current_exe().unwrap());
|
||||
c.args(["--exact", "native_crash::tests::child", "--test-threads=1", "--nocapture"]).env("FIELDCRAFT_NATIVE_CRASH_CHILD", dir);
|
||||
if fault {
|
||||
c.env("FIELDCRAFT_NATIVE_CRASH_FAULT", "1");
|
||||
}
|
||||
c.output().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_access_violation_is_reported_with_its_module_step_and_minidump() {
|
||||
let dir = std::env::temp_dir().join(format!("fieldcraft-native-crash-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let out = run(&dir, true);
|
||||
assert_eq!(out.status.code().map(|c| c as u32), Some(super::STATUS_ACCESS_VIOLATION), "{out:?}");
|
||||
let sessions: Vec<PathBuf> = std::fs::read_dir(dir.join("sessions")).unwrap().flatten().map(|e| e.path()).collect();
|
||||
let info = sessions.iter().find(|p| p.to_string_lossy().ends_with(".native.json")).expect("the filter wrote what it caught");
|
||||
let v: serde_json::Value = serde_json::from_slice(&std::fs::read(info).unwrap()).unwrap();
|
||||
assert_eq!(v["code"], "0xC0000005", "{v}");
|
||||
assert_eq!(v["detail"], "read of 0x0000000000000010", "{v}");
|
||||
assert!(v["module"].as_str().unwrap().to_lowercase().contains("fieldcraft"), "the fault is in the test binary: {v}");
|
||||
let dump = sessions.iter().find(|p| p.extension().is_some_and(|e| e == "dmp")).expect("a minidump");
|
||||
assert_eq!(std::fs::read(dump).unwrap().get(..4), Some(&b"MDMP"[..]));
|
||||
// The next start reports it.
|
||||
let out = run(&dir, false);
|
||||
assert!(out.status.success(), "{out:?}");
|
||||
let reports = fieldcraft_engine::crash::list(&dir);
|
||||
assert_eq!(reports.len(), 1, "{reports:?}");
|
||||
let r = fieldcraft_engine::crash::read(&dir, &reports[0].id).unwrap();
|
||||
assert!(r["message"].as_str().unwrap().contains("EXCEPTION_ACCESS_VIOLATION (0xC0000005) in fieldcraft"), "{r}");
|
||||
assert!(r["message"].as_str().unwrap().contains("during startup (device creation"), "{r}");
|
||||
assert!(r.get("minidump_path").is_some(), "{r}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
//! A request to terminate (SIGTERM, SIGINT on Unix) closes the window the normal way.
|
||||
//!
|
||||
//! Without this, `kill PID`, Ctrl-C in the terminal, a system shutdown or a test harness's timeout
|
||||
//! ended the process on the spot: no `on_exit`, so no last autosave and no clean end of the crash
|
||||
//! journal, and the next start filed an `unclean_exit` crash report for what was no crash (#248:
|
||||
//! "ended without a clean exit between commands (last: control ui.capture)" was an automation run
|
||||
//! stopped with SIGTERM after its capture). Now the first signal asks the window to close, which
|
||||
//! autosaves, ends the journal and exits as a click on Close does; the journal notes the signal,
|
||||
//! so a run that still dies while closing says why. A second signal, for a window that does not
|
||||
//! close, ends the process at once with the signal's default action.
|
||||
//!
|
||||
//! SIGHUP is left alone: `nohup fieldcraft &` ignores it on purpose so the app outlives its
|
||||
//! terminal, and a handler would override that (a safe API cannot ask whether a signal was
|
||||
//! ignored at launch).
|
||||
//!
|
||||
//! Windows has no such signals: a window there is closed through its messages, which eframe
|
||||
//! already turns into a normal close.
|
||||
|
||||
use std::io;
|
||||
|
||||
/// The signals that ask the app to terminate.
|
||||
#[cfg(unix)]
|
||||
const SIGNALS: [i32; 2] = [signal_hook::consts::SIGTERM, signal_hook::consts::SIGINT];
|
||||
|
||||
/// The signal's usual name, for the log and the crash journal.
|
||||
#[cfg(unix)]
|
||||
fn name(signal: i32) -> &'static str {
|
||||
match signal {
|
||||
signal_hook::consts::SIGTERM => "SIGTERM",
|
||||
signal_hook::consts::SIGINT => "SIGINT",
|
||||
_ => "signal",
|
||||
}
|
||||
}
|
||||
|
||||
/// Call `close` (on a thread of its own) on the first termination signal; a second one ends the
|
||||
/// process with the signal's default action.
|
||||
#[cfg(unix)]
|
||||
pub fn watch(close: impl Fn() + Send + 'static) -> io::Result<()> {
|
||||
let mut signals = signal_hook::iterator::Signals::new(SIGNALS)?;
|
||||
std::thread::Builder::new().name("terminate".into()).spawn(move || {
|
||||
let mut asked = false;
|
||||
for signal in signals.forever() {
|
||||
if asked {
|
||||
log::warn!("{}: the window did not close; ending now", name(signal));
|
||||
if let Err(e) = signal_hook::low_level::emulate_default_handler(signal) {
|
||||
log::error!("{}: {e}", name(signal));
|
||||
std::process::exit(128 + signal);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
asked = true;
|
||||
log::info!("{}: closing", name(signal));
|
||||
fieldcraft_engine::crash::activity(&format!("terminating ({})", name(signal)));
|
||||
close();
|
||||
}
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Nothing to watch without Unix signals.
|
||||
#[cfg(not(unix))]
|
||||
pub fn watch(_close: impl Fn() + Send + 'static) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
mod tests {
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::mpsc;
|
||||
|
||||
use fieldcraft_engine::crash;
|
||||
|
||||
/// The child half: start crash reporting as the app does, then get SIGTERM. With the watcher
|
||||
/// the "window" (here the main thread) closes the normal way; without it the process dies.
|
||||
#[test]
|
||||
fn child() {
|
||||
let Some(dir) = std::env::var_os("FIELDCRAFT_TERMINATE_CHILD") else { return };
|
||||
let build = crash::Build { version: "9.9.9", commit: "abc1234" };
|
||||
let fresh = crash::install(PathBuf::from(dir), build);
|
||||
if std::env::var_os("FIELDCRAFT_TERMINATE_NEXT").is_some() {
|
||||
// The next start: what it reports of the run before.
|
||||
println!("FRESH {}", fresh.iter().map(|s| s.message.as_str()).collect::<Vec<_>>().join(" | "));
|
||||
crash::end_session();
|
||||
return;
|
||||
}
|
||||
crash::activity("control ui.capture");
|
||||
if std::env::var_os("FIELDCRAFT_TERMINATE_WATCH").is_some() {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
super::watch(move || {
|
||||
let _ = tx.send(());
|
||||
})
|
||||
.unwrap();
|
||||
signal_hook::low_level::raise(signal_hook::consts::SIGTERM).unwrap();
|
||||
// What `on_exit` does once the window has closed.
|
||||
rx.recv().unwrap();
|
||||
crash::end_session();
|
||||
println!("CLOSED");
|
||||
return;
|
||||
}
|
||||
signal_hook::low_level::raise(signal_hook::consts::SIGTERM).unwrap();
|
||||
// Not reached: SIGTERM's default action ends the process.
|
||||
std::thread::sleep(std::time::Duration::from_secs(10));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sigterm_closes_cleanly_and_leaves_no_crash_report() {
|
||||
let root = std::env::temp_dir().join(format!("fieldcraft-terminate-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
let run = |dir: &Path, mode: &str| {
|
||||
let mut c = std::process::Command::new(std::env::current_exe().unwrap());
|
||||
c.args(["--exact", "terminate::tests::child", "--test-threads=1", "--nocapture"]).env("FIELDCRAFT_TERMINATE_CHILD", dir);
|
||||
match mode {
|
||||
"watch" => c.env("FIELDCRAFT_TERMINATE_WATCH", "1"),
|
||||
"next" => c.env("FIELDCRAFT_TERMINATE_NEXT", "1"),
|
||||
_ => &mut c,
|
||||
};
|
||||
c.output().unwrap()
|
||||
};
|
||||
// What the next start in `dir` reports of the run before.
|
||||
let reported_at_next_start = |dir: &Path| {
|
||||
let out = run(dir, "next");
|
||||
assert!(out.status.success(), "{out:?}");
|
||||
let stdout = String::from_utf8_lossy(&out.stdout).to_string();
|
||||
stdout.lines().find_map(|l| l.split_once("FRESH ").map(|(_, r)| r.trim().to_string())).unwrap_or_else(|| panic!("{stdout}"))
|
||||
};
|
||||
|
||||
// Without the watcher (the bug): killed, and the journal it leaves reads as a crash.
|
||||
let unwatched = root.join("unwatched");
|
||||
let out = run(&unwatched, "kill");
|
||||
use std::os::unix::process::ExitStatusExt;
|
||||
assert_eq!(out.status.signal(), Some(signal_hook::consts::SIGTERM), "{out:?}");
|
||||
let reported = reported_at_next_start(&unwatched);
|
||||
assert!(reported.contains("ended without a clean exit") && reported.contains("control ui.capture"), "{reported}");
|
||||
|
||||
// With it: the close runs, the journal ends, and nothing is reported as a crash.
|
||||
let watched = root.join("watched");
|
||||
let out = run(&watched, "watch");
|
||||
assert!(out.status.success(), "{out:?}");
|
||||
assert!(String::from_utf8_lossy(&out.stdout).contains("CLOSED"), "{out:?}");
|
||||
assert_eq!(reported_at_next_start(&watched), "");
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>geometry.check</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M9 3 L16 6.5 L16 13 L9 16.5 L2 13 L2 6.5 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M2 6.5 L9 10 L16 6.5 M9 10 L9 16.5" fill="none" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M2 6.5 L2 9.5" fill="none" stroke="#f0b44a" stroke-width="2.25" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M13.5 17 C13.5 15.067 15.067 13.5 17 13.5 C18.933 13.5 20.5 15.067 20.5 17 C20.5 18.933 18.933 20.5 17 20.5 C15.067 20.5 13.5 18.933 13.5 17 Z" fill="none" stroke="#4f9ce8" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M19.5 19.5 L22 22" fill="none" stroke="#4f9ce8" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.0 KiB |
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>geometry.defeature</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M3 8 L12 4 L21 8 L12 12 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M3 8 L3 16 L12 20 L12 12 M12 20 L21 16 L21 8" fill="none" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M9 8 C9 8.828 10.343 9.5 12 9.5 C13.657 9.5 15 8.828 15 8 C15 7.172 13.657 6.5 12 6.5 C10.343 6.5 9 7.172 9 8 Z" fill="#f0b44a" stroke="#f0b44a" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M7.5 11 L16.5 5" fill="none" stroke="#4f9ce8" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 886 B |
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>geometry.face.absorb</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M3 8 L12 4 L21 8 L12 12 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M3 8 L3 16 L12 20 L12 12 M12 20 L21 16 L21 8" fill="none" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M14.5 5.1 L17 6.2 L8 10.3 L5.5 9.2 Z" fill="#f0b44a" stroke="#f0b44a" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M3 8 L12 4 L21 8 L12 12 Z" fill="none" stroke="#4f9ce8" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 823 B |
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>geometry.face.split</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M3 8 L12 4 L21 8 L12 12 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M3 8 L3 16 L12 20 L12 12 M12 20 L21 16 L21 8" fill="none" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M7.5 10 L16.5 6" fill="none" stroke="#f0b44a" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M5 4.5 L9 2.5 M15 9.5 L19 7.5" fill="none" stroke="#4f9ce8" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="1.5 1.5"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 845 B |
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>geometry.faces.merge</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M3 8 L12 4 L21 8 L12 12 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M3 8 L3 16 L12 20 L12 12 M12 20 L21 16 L21 8" fill="none" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M7.5 6 L16.5 10" fill="none" stroke="#f0b44a" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="1.5 2"/>
|
||||
<path d="M3 8 L12 4 L21 8 L12 12 Z" fill="none" stroke="#4f9ce8" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 844 B |
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>geometry.stitch</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M3 6 L10.5 6 L10.5 18 L3 18 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M13.5 6 L21 6 L21 18 L13.5 18 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M9 8.5 L15 8.5" fill="none" stroke="#f0b44a" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M9 12 L15 12" fill="none" stroke="#f0b44a" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M9 15.5 L15 15.5" fill="none" stroke="#f0b44a" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 923 B |
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>mesh.export.stl</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M3 9 L9 5 L15 9 L9 13 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M3 9 L3 17 L9 21 L9 13 M9 21 L15 17 L15 9" fill="none" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M3 9 L9 21 M9 13 L15 17" fill="none" stroke="#dde2ea" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M15 15 L21 15 M18.19 16.53 L21 15 L18.19 13.47" fill="none" stroke="#4f9ce8" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 817 B |
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>mesh.hex.extrude</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M6 20 L18 20 L18 11.6 C14 9.467 10 9.467 6 11.6 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M6 17.2 L18 17.2 M6 14.4 C10 12.8 14 12.8 18 14.4 M10 20 L10 9.9 M14 20 L14 9.9" fill="none" stroke="#4f9ce8" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M3 9.2 C9 5.333 15 5.333 21 9.2" fill="none" stroke="#4f9ce8" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M12 6 L12 3 M10.5 4.5 L12 3 L13.5 4.5" fill="none" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 879 B |
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>mesh.plan</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M12 3 L20 7 L20 16 L12 20 L4 16 L4 7 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M4 7 L12 11 L20 7 M12 11 L12 20" fill="none" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M4 11.5 L12 15.5 L20 11.5 M8 9 L8 18 M16 9 L16 18" fill="none" stroke="#4f9ce8" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M12 3 L12 5.4 M20 7 L15.2 7 M12 11 L12 8.6 M4 7 L8.8 7" fill="none" stroke="#4f9ce8" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M8.8 7 C8.8 7.884 10.233 8.6 12 8.6 C13.767 8.6 15.2 7.884 15.2 7 C15.2 6.116 13.767 5.4 12 5.4 C10.233 5.4 8.8 6.116 8.8 7 Z" fill="none" stroke="#f0b44a" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.0 KiB |
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>mesh.quality.mark</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M3 3 L11 3 L11 11 L3 11 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M13 3 L21 3 L21 11 L13 11 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M3 13 L11 13 L11 21 L3 21 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M13 14 L21 13 L20 21 L14 19 Z" fill="#f0b44a" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 822 B |
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>mesh.size.set</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M3 3 L21 3 L21 21 L3 21 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M12 3 L12 21 M3 12 L21 12" fill="none" stroke="#dde2ea" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M12 12 L21 12 L21 21 L12 21 Z" fill="none" stroke="#4f9ce8" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M16.5 12 L16.5 21 M12 16.5 L21 16.5" fill="none" stroke="#4f9ce8" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 777 B |
@@ -1,10 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>mesh.transition</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M3 4 L21 4 L21 20 L3 20 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M7.5 4 L7.5 20 M3 9.33 L12 9.33 M3 14.67 L12 14.67" fill="none" stroke="#dde2ea" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M12 4 L21 9.33 L12 14.67 L21 20 M12 14.67 L21 14.67 M12 4 L21 4" fill="none" stroke="#dde2ea" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M12 4 L12 20" fill="none" stroke="#4f9ce8" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 816 B |
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
|
||||
<title>results.export.vtu</title>
|
||||
<g id="Layer_1" data-name="Layer 1">
|
||||
<path d="M4 3 L12 3 L16 7 L16 21 L4 21 Z" fill="#2d4e70" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M12 3 L12 7 L16 7" fill="none" stroke="#dde2ea" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M7 17 L7 14 M10 17 L10 11 M13 17 L13 13" fill="none" stroke="#4f9ce8" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M15 15 L21 15 M18.19 16.53 L21 15 L18.19 13.47" fill="none" stroke="#4f9ce8" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 837 B |
@@ -0,0 +1 @@
|
||||
{"shapes": [{"d": "M9 3 L16 6.5 L16 13 L9 16.5 L2 13 L2 6.5 Z", "fill": "soft", "stroke": "line"}, {"d": "M2 6.5 L9 10 L16 6.5 M9 10 L9 16.5", "stroke": "line"}, {"line": [2, 6.5, 2, 9.5], "stroke": "warm", "width": 2.25}, {"circle": [17, 17, 3.5], "stroke": "accent", "width": 1.75}, {"line": [19.5, 19.5, 22, 22], "stroke": "accent", "width": 2}]}
|
||||
@@ -0,0 +1 @@
|
||||
{"shapes": [{"d": "M3 8 L12 4 L21 8 L12 12 Z", "fill": "soft", "stroke": "line"}, {"d": "M3 8 L3 16 L12 20 L12 12 M12 20 L21 16 L21 8", "stroke": "line"}, {"d": "M9 8 A3 1.5 0 1 0 15 8 A3 1.5 0 1 0 9 8 Z", "fill": "warm", "stroke": "warm", "width": 1}, {"d": "M7.5 11 L16.5 5", "stroke": "accent", "width": 1.75}]}
|
||||
@@ -0,0 +1 @@
|
||||
{"shapes": [{"d": "M3 8 L12 4 L21 8 L12 12 Z", "fill": "soft", "stroke": "line"}, {"d": "M3 8 L3 16 L12 20 L12 12 M12 20 L21 16 L21 8", "stroke": "line"}, {"d": "M14.5 5.1 L17 6.2 L8 10.3 L5.5 9.2 Z", "fill": "warm", "stroke": "warm", "width": 1}, {"d": "M3 8 L12 4 L21 8 L12 12 Z", "stroke": "accent", "width": 1.75}]}
|
||||
@@ -0,0 +1 @@
|
||||
{"shapes": [{"d": "M3 8 L12 4 L21 8 L12 12 Z", "fill": "soft", "stroke": "line"}, {"d": "M3 8 L3 16 L12 20 L12 12 M12 20 L21 16 L21 8", "stroke": "line"}, {"line": [7.5, 10, 16.5, 6], "stroke": "warm", "width": 2}, {"d": "M5 4.5 L9 2.5 M15 9.5 L19 7.5", "stroke": "accent", "width": 1.5, "dash": [1.5, 1.5]}]}
|
||||
@@ -0,0 +1 @@
|
||||
{"shapes": [{"d": "M3 8 L12 4 L21 8 L12 12 Z", "fill": "soft", "stroke": "line"}, {"d": "M3 8 L3 16 L12 20 L12 12 M12 20 L21 16 L21 8", "stroke": "line"}, {"line": [7.5, 6, 16.5, 10], "stroke": "warm", "width": 1.25, "dash": [1.5, 2]}, {"d": "M3 8 L12 4 L21 8 L12 12 Z", "stroke": "accent", "width": 1.75}]}
|
||||
@@ -0,0 +1 @@
|
||||
{"shapes": [{"d": "M3 6 L10.5 6 L10.5 18 L3 18 Z", "fill": "soft", "stroke": "line"}, {"d": "M13.5 6 L21 6 L21 18 L13.5 18 Z", "fill": "soft", "stroke": "line"}, {"line": [9, 8.5, 15, 8.5], "stroke": "warm", "width": 1.75}, {"line": [9, 12, 15, 12], "stroke": "warm", "width": 1.75}, {"line": [9, 15.5, 15, 15.5], "stroke": "warm", "width": 1.75}]}
|
||||
@@ -0,0 +1 @@
|
||||
{"shapes": [{"d": "M3 9 L9 5 L15 9 L9 13 Z", "fill": "soft", "stroke": "line"}, {"d": "M3 9 L3 17 L9 21 L9 13 M9 21 L15 17 L15 9", "stroke": "line"}, {"d": "M3 9 L9 21 M9 13 L15 17", "stroke": "line", "width": 1}, {"d": "M15 15 L21 15 M18.19 16.53 L21 15 L18.19 13.47", "stroke": "accent", "width": 1.5}]}
|
||||
@@ -0,0 +1 @@
|
||||
{"shapes": [{"d": "M6 20 L18 20 L18 11.6 Q12 8.4 6 11.6 Z", "fill": "soft", "stroke": "line"}, {"d": "M6 17.2 L18 17.2 M6 14.4 Q12 12 18 14.4 M10 20 L10 9.9 M14 20 L14 9.9", "stroke": "accent", "width": 1}, {"d": "M3 9.2 Q12 3.4 21 9.2", "stroke": "accent", "width": 2}, {"d": "M12 6 L12 3 M10.5 4.5 L12 3 L13.5 4.5", "stroke": "line"}]}
|
||||
@@ -0,0 +1 @@
|
||||
{"shapes": [{"d": "M12 3 L20 7 L20 16 L12 20 L4 16 L4 7 Z", "fill": "soft", "stroke": "line"}, {"d": "M4 7 L12 11 L20 7 M12 11 L12 20", "stroke": "line"}, {"d": "M4 11.5 L12 15.5 L20 11.5 M8 9 L8 18 M16 9 L16 18", "stroke": "accent", "width": 1}, {"d": "M12 3 L12 5.4 M20 7 L15.2 7 M12 11 L12 8.6 M4 7 L8.8 7", "stroke": "accent", "width": 1}, {"d": "M8.8 7 A3.2 1.6 0 1 0 15.2 7 A3.2 1.6 0 1 0 8.8 7 Z", "stroke": "warm", "width": 1.5}]}
|
||||
@@ -0,0 +1 @@
|
||||
{"shapes": [{"rect": [3, 3, 8, 8], "fill": "soft", "stroke": "line"}, {"rect": [13, 3, 8, 8], "fill": "soft", "stroke": "line"}, {"rect": [3, 13, 8, 8], "fill": "soft", "stroke": "line"}, {"d": "M13 14 L21 13 L20 21 L14 19 Z", "fill": "warm", "stroke": "line"}]}
|
||||
@@ -0,0 +1 @@
|
||||
{"shapes": [{"rect": [3, 3, 18, 18], "fill": "soft", "stroke": "line"}, {"d": "M12 3 L12 21 M3 12 L21 12", "stroke": "line", "width": 1}, {"rect": [12, 12, 9, 9], "fill": null, "stroke": "accent", "width": 1.5}, {"d": "M16.5 12 L16.5 21 M12 16.5 L21 16.5", "stroke": "accent", "width": 1}]}
|
||||
@@ -1 +0,0 @@
|
||||
{"shapes": [{"rect": [3, 4, 18, 16], "fill": "soft", "stroke": "line"}, {"d": "M7.5 4 L7.5 20 M3 9.33 L12 9.33 M3 14.67 L12 14.67", "stroke": "line", "width": 1}, {"d": "M12 4 L21 9.33 L12 14.67 L21 20 M12 14.67 L21 14.67 M12 4 L21 4", "stroke": "line", "width": 1}, {"d": "M12 4 L12 20", "stroke": "accent", "width": 1.75}]}
|
||||
@@ -0,0 +1 @@
|
||||
{"shapes": [{"d": "M4 3 L12 3 L16 7 L16 21 L4 21 Z", "fill": "soft", "stroke": "line"}, {"d": "M12 3 L12 7 L16 7", "stroke": "line"}, {"d": "M7 17 L7 14 M10 17 L10 11 M13 17 L13 13", "stroke": "accent", "width": 2}, {"d": "M15 15 L21 15 M18.19 16.53 L21 15 L18.19 13.47", "stroke": "accent", "width": 1.5}]}
|
||||
@@ -85,6 +85,7 @@
|
||||
//! | tet4 | C3D4 (H) | | | |
|
||||
//! | tet10 | C3D10 (H) | | | |
|
||||
//! | wedge6 | C3D6 (H) | | | |
|
||||
//! | pyramid5 | C3D5 (H) | | | |
|
||||
//! | hex8 | C3D8, C3D8R, C3D8I (H) | | | |
|
||||
//! | hex20 | C3D20, C3D20R (H) | | | |
|
||||
//! | tri3, quad4 | | S3 / S3R, S4 / S4R | | |
|
||||
@@ -148,7 +149,7 @@ use std::fmt::Write as _;
|
||||
|
||||
use fieldcraft_model::{
|
||||
BeamProfile, BoundaryCondition, ContactSurfaces, CouplingKind, Elastic, Element, ElementKind, Hardening, Hyperelastic, InitialPenetration,
|
||||
Integration, Load, Model, OutputVariable, Plastic, Procedure, Resolved, Section, SectionKind, Step, Symmetry, Target,
|
||||
Integration, Load, Model, OutputVariable, Plastic, Procedure, Resolved, Section, SectionKind, Step, Target,
|
||||
};
|
||||
|
||||
use crate::{Error, Result, unsupported};
|
||||
@@ -193,6 +194,7 @@ pub fn write_resolved(model: &Model, r: &Resolved) -> Result<String> {
|
||||
d.couplings(model, r);
|
||||
d.contacts(model, r)?;
|
||||
d.ties(model)?;
|
||||
d.equations(model, r)?;
|
||||
d.steps(model, r)?;
|
||||
Ok(d.out)
|
||||
}
|
||||
@@ -230,11 +232,12 @@ pub fn element_type(kind: ElementKind, section: &Section) -> std::result::Result
|
||||
(S::Solid {}, _, true, true) => return Err("incompatible modes with reduced integration".into()),
|
||||
(S::Solid {}, _, _, true) => return Err(format!("incompatible modes on {} elements (hex8 only)", kind.name())),
|
||||
(S::Solid {}, K::Tet4, ..) => "C3D4",
|
||||
(S::Solid {}, K::Tet10 | K::Wedge6, true, _) => {
|
||||
(S::Solid {}, K::Tet10 | K::Wedge6 | K::Pyramid5, true, _) => {
|
||||
return Err(format!("reduced integration of {} elements (Abaqus has no reduced-integration variant)", kind.name()));
|
||||
}
|
||||
(S::Solid {}, K::Tet10, ..) => "C3D10",
|
||||
(S::Solid {}, K::Wedge6, ..) => "C3D6",
|
||||
(S::Solid {}, K::Pyramid5, ..) => "C3D5",
|
||||
(S::Solid {}, K::Hex8, r, _) => ["C3D8", "C3D8R"][usize::from(r)],
|
||||
(S::Solid {}, K::Hex20, r, _) => ["C3D20", "C3D20R"][usize::from(r)],
|
||||
(S::Shell { .. }, K::Tri3, r, _) => ["S3", "S3R"][usize::from(r)],
|
||||
@@ -370,9 +373,36 @@ fn check_expressible(model: &Model) -> Result<()> {
|
||||
bad.insert(format!("coupling `{name}`: an Abaqus distributing coupling always couples translations 1-3"));
|
||||
}
|
||||
}
|
||||
for name in inclined_symmetries(model) {
|
||||
if !model.steps().iter().all(|s| s.boundary_conditions.contains(name)) {
|
||||
bad.insert(format!(
|
||||
"boundary condition `{name}`: a symmetry plane not normal to X, Y or Z active in some steps only (it is written as `*EQUATION`, which is model data and holds in every step)"
|
||||
));
|
||||
}
|
||||
}
|
||||
unsupported(FORMAT, bad)
|
||||
}
|
||||
|
||||
/// The symmetry conditions on inclined planes some step uses: linear equations rather than held
|
||||
/// degrees of freedom.
|
||||
fn inclined_symmetries(model: &Model) -> BTreeSet<&String> {
|
||||
model
|
||||
.boundary_conditions()
|
||||
.iter()
|
||||
.filter(|(n, bc)| {
|
||||
matches!(bc, BoundaryCondition::Symmetry { plane, .. } if plane.axis().is_none()) && !model.boundary_condition_users(n).is_empty()
|
||||
})
|
||||
.map(|(n, _)| n)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The boundary conditions of `names` that `*BOUNDARY` writes (inclined symmetry planes are
|
||||
/// `*EQUATION`s).
|
||||
fn held_conditions(model: &Model, names: &BTreeSet<String>) -> BTreeSet<String> {
|
||||
let inclined = inclined_symmetries(model);
|
||||
names.iter().filter(|n| !inclined.contains(n)).cloned().collect()
|
||||
}
|
||||
|
||||
/// The property of a group of discrete elements.
|
||||
enum DiscreteProperty {
|
||||
Mass(f64),
|
||||
@@ -1076,10 +1106,12 @@ impl Deck {
|
||||
match bc {
|
||||
BoundaryCondition::Fixed { .. } => self.line(&format!("{target}, ENCASTRE")),
|
||||
BoundaryCondition::Symmetry { plane, .. } => {
|
||||
let s = match plane {
|
||||
Symmetry::X => "XSYMM",
|
||||
Symmetry::Y => "YSYMM",
|
||||
Symmetry::Z => "ZSYMM",
|
||||
let s = match plane.axis() {
|
||||
Some(0) => "XSYMM",
|
||||
Some(1) => "YSYMM",
|
||||
Some(2) => "ZSYMM",
|
||||
// An inclined plane is an `*EQUATION` (model data), never here.
|
||||
_ => continue,
|
||||
};
|
||||
self.line(&format!("{target}, {s}"));
|
||||
}
|
||||
@@ -1097,8 +1129,74 @@ impl Deck {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The inclined symmetry planes: one `*EQUATION` per node and held direction (n · u = 0, and
|
||||
/// the rotations about the plane's axes at shell and beam nodes), whose first term Abaqus
|
||||
/// eliminates; it is chosen among the degrees of freedom no boundary condition, coupling
|
||||
/// or tie constrains ([`crate::symmetry_equations`]).
|
||||
fn equations(&mut self, model: &Model, r: &Resolved) -> Result<()> {
|
||||
let inclined = inclined_symmetries(model);
|
||||
if inclined.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let steps = model.steps();
|
||||
// Per node and degree of freedom: the value it is held at in each step that holds it.
|
||||
let mut held: BTreeMap<(u32, u8), Vec<f64>> = BTreeMap::new();
|
||||
for step in steps {
|
||||
for name in held_conditions(model, &step.boundary_conditions) {
|
||||
let Some(bc) = model.boundary_condition(&name) else { continue };
|
||||
for &node in r.target_nodes(bc.target()) {
|
||||
for dof in bc.dofs().list() {
|
||||
let v = if matches!(step.procedure, Procedure::Frequency { .. } | Procedure::Buckle { .. }) {
|
||||
0.0
|
||||
} else {
|
||||
bc.value(dof).unwrap_or(0.0)
|
||||
};
|
||||
held.entry((node, dof)).or_default().push(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut dependent: BTreeSet<(u32, u8)> = BTreeSet::new();
|
||||
for c in model.couplings().values().filter(|c| c.kind == CouplingKind::Kinematic) {
|
||||
for &node in r.sets.get(&c.set).map(|s| s.nodes.as_slice()).unwrap_or_default() {
|
||||
dependent.extend(c.dofs.list().into_iter().map(|d| (node, d)));
|
||||
}
|
||||
}
|
||||
for t in model.ties().values() {
|
||||
for &node in r.sets.get(&t.secondary).map(|s| s.nodes.as_slice()).unwrap_or_default() {
|
||||
dependent.extend((1..=6).map(|d| (node, d)));
|
||||
}
|
||||
}
|
||||
let state = |node: u32, dof: u8| {
|
||||
if dependent.contains(&(node, dof)) {
|
||||
return crate::Held::Sometimes;
|
||||
}
|
||||
match held.get(&(node, dof)) {
|
||||
None => crate::Held::Free,
|
||||
Some(v) if v.len() == steps.len() && v.iter().all(|x| *x == v[0]) => crate::Held::Always(v[0]),
|
||||
Some(_) => crate::Held::Sometimes,
|
||||
}
|
||||
};
|
||||
let mut problems = BTreeSet::new();
|
||||
let equations = crate::symmetry_equations(model, r, inclined, &model.rotating_nodes(), state, &mut problems);
|
||||
unsupported(FORMAT, problems)?;
|
||||
self.comment("Symmetry planes not normal to X, Y or Z: per node, no motion along the normal (and no rotation about the plane's axes)");
|
||||
for e in &equations {
|
||||
self.line("*EQUATION");
|
||||
self.line(&e.terms.len().to_string());
|
||||
let mut fields = Vec::new();
|
||||
for (dof, c) in &e.terms {
|
||||
fields.push(format!("{}, {dof}", e.node));
|
||||
fields.push(number(*c)?);
|
||||
}
|
||||
self.line(&fields.join(", "));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Boundary conditions of a step against the state it starts from.
|
||||
fn step_boundaries(&mut self, model: &Model, r: &Resolved, base: &BTreeSet<String>, active: &BTreeSet<String>) -> Result<()> {
|
||||
let (base, active) = (&held_conditions(model, base), &held_conditions(model, active));
|
||||
if active == base {
|
||||
self.comment("boundary conditions: as in the base state");
|
||||
Ok(())
|
||||
@@ -1188,7 +1286,8 @@ impl Deck {
|
||||
BTreeSet::new()
|
||||
} else {
|
||||
let in_every_step = |n: &String| steps.iter().all(|s| s.boundary_conditions.contains(n));
|
||||
model.boundary_conditions().iter().filter(|(n, bc)| holds_at_zero(bc) && in_every_step(n)).map(|(n, _)| n.clone()).collect()
|
||||
let all = model.boundary_conditions().iter().filter(|(n, bc)| holds_at_zero(bc) && in_every_step(n)).map(|(n, _)| n.clone()).collect();
|
||||
held_conditions(model, &all)
|
||||
};
|
||||
if !initial.is_empty() {
|
||||
self.comment("Initial state: boundary conditions active in every step");
|
||||
|
||||
@@ -16,7 +16,7 @@ pub mod nastran;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use fieldcraft_model::nodal::edge_pressure;
|
||||
use fieldcraft_model::{Diagnostic, Load, Model, Resolved, ResolvedDirection};
|
||||
use fieldcraft_model::{BoundaryCondition, Diagnostic, Load, Model, Resolved, ResolvedDirection};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
|
||||
pub enum Error {
|
||||
@@ -176,6 +176,120 @@ pub fn edge_pressure_loads<'a>(model: &Model, r: &Resolved, active: impl IntoIte
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// How a degree of freedom is constrained apart from the inclined symmetry planes, for choosing
|
||||
/// the dependent one of a plane's equation (formats eliminate it, so it may carry no other
|
||||
/// constraint).
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub(crate) enum Held {
|
||||
Free,
|
||||
/// Held at this value wherever the equation applies: it cannot depend on the others, and an
|
||||
/// equation of held ones alone is redundant when the values satisfy it.
|
||||
Always(f64),
|
||||
/// Held in some steps only, or made dependent by a coupling or tie: it cannot be dependent,
|
||||
/// nor can the equation be dropped.
|
||||
Sometimes,
|
||||
}
|
||||
|
||||
/// A linear equation Σ cⱼ u(node, dofⱼ) = 0 of an inclined symmetry plane, dependent degree of
|
||||
/// freedom first.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub(crate) struct Equation {
|
||||
pub node: u32,
|
||||
pub terms: Vec<(u8, f64)>,
|
||||
}
|
||||
|
||||
/// A coefficient below this fraction of its row's largest is round-off of the elimination.
|
||||
const NEGLIGIBLE: f64 = 1e-12;
|
||||
|
||||
/// The equations of the inclined symmetry conditions `names` (others are skipped), node by node
|
||||
/// in label order: n · u = 0 and, at a node in `rotating`, t₁ · θ = 0 and t₂ · θ = 0 for the
|
||||
/// plane's in-plane axes ([`fieldcraft_model::Symmetry`]). The rows of one node (several planes
|
||||
/// meet at an edge) are reduced against each other, so each has a dependent degree of freedom
|
||||
/// of its own: the largest coefficient among those `held` says are free. A row of held degrees
|
||||
/// of freedom only is left out when their values satisfy it; otherwise it is a problem, as is a
|
||||
/// row with none free that cannot be left out.
|
||||
pub(crate) fn symmetry_equations<'a>(
|
||||
model: &Model,
|
||||
r: &Resolved,
|
||||
names: impl IntoIterator<Item = &'a String>,
|
||||
rotating: &BTreeSet<u32>,
|
||||
held: impl Fn(u32, u8) -> Held,
|
||||
problems: &mut BTreeSet<String>,
|
||||
) -> Vec<Equation> {
|
||||
let mut rows: BTreeMap<u32, Vec<(&str, [f64; 6])>> = BTreeMap::new();
|
||||
let mut seen = BTreeSet::new();
|
||||
for name in names {
|
||||
if !seen.insert(name.as_str()) {
|
||||
continue;
|
||||
}
|
||||
let Some(BoundaryCondition::Symmetry { target, plane }) = model.boundary_condition(name) else { continue };
|
||||
let (Some(n), Some([t1, t2])) = (plane.inclined(), plane.in_plane()) else { continue };
|
||||
for &node in r.target_nodes(target) {
|
||||
let at = rows.entry(node).or_default();
|
||||
at.push((name, [n[0], n[1], n[2], 0.0, 0.0, 0.0]));
|
||||
if rotating.contains(&node) || target.reference_point().is_some() {
|
||||
at.push((name, [0.0, 0.0, 0.0, t1[0], t1[1], t1[2]]));
|
||||
at.push((name, [0.0, 0.0, 0.0, t2[0], t2[1], t2[2]]));
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
for (node, rows) in rows {
|
||||
// The equations of this node so far: (dependent index, coefficients).
|
||||
let mut done: Vec<(usize, [f64; 6])> = Vec::new();
|
||||
for (name, mut c) in rows {
|
||||
let scale = c.iter().fold(0.0f64, |m, v| m.max(v.abs()));
|
||||
for (d, e) in &done {
|
||||
let f = c[*d] / e[*d];
|
||||
for k in 0..6 {
|
||||
c[k] -= f * e[k];
|
||||
}
|
||||
c[*d] = 0.0;
|
||||
}
|
||||
for v in &mut c {
|
||||
if v.abs() <= NEGLIGIBLE * scale {
|
||||
*v = 0.0;
|
||||
}
|
||||
}
|
||||
if c.iter().all(|v| *v == 0.0) {
|
||||
continue;
|
||||
}
|
||||
let dof = |k: usize| k as u8 + 1;
|
||||
let pivot = (0..6).filter(|&k| c[k] != 0.0 && held(node, dof(k)) == Held::Free).fold(None, |best: Option<usize>, k| match best {
|
||||
Some(b) if c[b].abs() >= c[k].abs() => Some(b),
|
||||
_ => Some(k),
|
||||
});
|
||||
let Some(pivot) = pivot else {
|
||||
let mut residual = 0.0;
|
||||
let mut size = 0.0;
|
||||
let mut always = true;
|
||||
for k in (0..6).filter(|&k| c[k] != 0.0) {
|
||||
match held(node, dof(k)) {
|
||||
Held::Always(v) => {
|
||||
residual += c[k] * v;
|
||||
size += (c[k] * v).abs();
|
||||
}
|
||||
_ => always = false,
|
||||
}
|
||||
}
|
||||
if !always {
|
||||
problems.insert(format!(
|
||||
"boundary condition `{name}`: at node {node} every degree of freedom the inclined symmetry plane relates is held in some steps only or follows a coupling or tie, so none can be eliminated by its equation"
|
||||
));
|
||||
} else if residual.abs() > 1e-9 * size {
|
||||
problems.insert(format!("boundary condition `{name}`: node {node} is held off the inclined symmetry plane"));
|
||||
}
|
||||
continue;
|
||||
};
|
||||
let mut terms = vec![(dof(pivot), c[pivot])];
|
||||
terms.extend((0..6).filter(|&k| k != pivot && c[k] != 0.0).map(|k| (dof(k), c[k])));
|
||||
out.push(Equation { node, terms });
|
||||
done.push((pivot, c));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The global degree of freedom (1–6) a spring or dashpot direction acts along, when it is one
|
||||
/// (a vector or line along an axis counts: the sign does not matter to a spring).
|
||||
pub(crate) fn axis_dof(d: ResolvedDirection) -> Option<u8> {
|
||||
|
||||
@@ -36,7 +36,7 @@ impl Keyword {
|
||||
pub(crate) fn of(kind: ElementKind) -> Keyword {
|
||||
match kind {
|
||||
ElementKind::Hex20 => Keyword::SolidH20,
|
||||
ElementKind::Tet4 | ElementKind::Tet10 | ElementKind::Wedge6 | ElementKind::Hex8 => Keyword::Solid,
|
||||
ElementKind::Tet4 | ElementKind::Tet10 | ElementKind::Wedge6 | ElementKind::Pyramid5 | ElementKind::Hex8 => Keyword::Solid,
|
||||
ElementKind::Tri3 | ElementKind::Tri6 | ElementKind::Quad4 | ElementKind::Quad8 => Keyword::Shell,
|
||||
ElementKind::Rod2 | ElementKind::Rod3 | ElementKind::Beam2 => Keyword::Beam,
|
||||
}
|
||||
@@ -64,6 +64,8 @@ pub(crate) fn order(kind: ElementKind) -> &'static [usize] {
|
||||
ElementKind::Tet4 => &[0, 1, 2, 3, 3, 3, 3, 3],
|
||||
ElementKind::Tet10 => &[0, 1, 2, 3, 4, 5, 6, 8, 9, 7],
|
||||
ElementKind::Wedge6 => &[1, 0, 3, 4, 2, 2, 5, 5],
|
||||
// The degenerate hexahedron: the base, then the apex four times.
|
||||
ElementKind::Pyramid5 => &[0, 1, 2, 3, 4, 4, 4, 4],
|
||||
ElementKind::Hex8 => &[0, 1, 2, 3, 4, 5, 6, 7],
|
||||
ElementKind::Hex20 => &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
|
||||
}
|
||||
@@ -95,7 +97,7 @@ pub(crate) struct Elform {
|
||||
///
|
||||
/// Solids: hex8 2 (eight-point), reduced 1 (one-point) with hourglass control, incompatible
|
||||
/// modes −18 (eight-point enhanced strain with incompatible modes); wedges 2 (as degenerate
|
||||
/// hexahedra), reduced 15 (two-point pentahedron); tet4 10 (one point, exact for a linear
|
||||
/// hexahedra), reduced 15 (two-point pentahedron); pyramids 2 (as degenerate hexahedra); tet4 10 (one point, exact for a linear
|
||||
/// tetrahedron), hybrid 13 (nodal pressure); tet10 16; hex20 23. Shells: quad4 16 (fully
|
||||
/// integrated), reduced 2 (one-point) with hourglass control; tri3 4 (C0 triangle); quad8 23;
|
||||
/// tri6 24. Rods: 3 (truss). 2D (in the XY plane): plane stress 12, plane strain 13, both one-point
|
||||
@@ -122,6 +124,8 @@ pub(crate) fn elform(kind: ElementKind, section: &SectionKind, f: Formulation, s
|
||||
ElementKind::Hex8 => plain(2),
|
||||
ElementKind::Wedge6 if reduced => plain(15),
|
||||
ElementKind::Wedge6 => plain(2),
|
||||
ElementKind::Pyramid5 if reduced => no("there is no reduced-integration form of"),
|
||||
ElementKind::Pyramid5 => plain(2),
|
||||
ElementKind::Tet4 => plain(10),
|
||||
_ if reduced && kind.is_quadratic() => no("there is no reduced-integration form of"),
|
||||
ElementKind::Tet10 => plain(16),
|
||||
|
||||
@@ -40,7 +40,8 @@
|
||||
//! LS-DYNA's defaults; a step without nonlinear geometry and hyperelastic materials in nonlinear
|
||||
//! steps are refused); boundary conditions
|
||||
//! as `*BOUNDARY_SPC_*_ID` (named) and `*BOUNDARY_PRESCRIBED_MOTION_*_ID` for non-zero
|
||||
//! displacements; forces, moments and tractions as `*LOAD_NODE_SET`/`_POINT` from the
|
||||
//! displacements, a symmetry plane not normal to X, Y or Z as `*BOUNDARY_SPC_*_ID` in a
|
||||
//! `*DEFINE_COORDINATE_SYSTEM` whose x axis is the plane's normal; forces, moments and tractions as `*LOAD_NODE_SET`/`_POINT` from the
|
||||
//! resolved nodal values; pressure as `*LOAD_SEGMENT_SET`; gravity as `*LOAD_BODY_X/Y/Z`
|
||||
//! (with `*LOAD_BODY_PARTS` when it acts on a set); output as `*DATABASE_BINARY_D3PLOT`,
|
||||
//! `*DATABASE_SPCFORC` and `*DATABASE_EXTENT_BINARY`.
|
||||
@@ -1048,6 +1049,24 @@ fn boundary_condition(
|
||||
(r.reference_point_nodes.get(p).copied().ok_or_else(|| broken(format!("boundary condition `{name}`: reference point `{p}`")))?, "NODE")
|
||||
}
|
||||
};
|
||||
// A symmetry plane not normal to an axis: held in a coordinate system whose x axis is its
|
||||
// normal and whose xy plane holds its first in-plane axis (translation x, rotations y and z).
|
||||
if let BoundaryCondition::Symmetry { plane, .. } = bc
|
||||
&& let (Some(n), Some([t1, _])) = (plane.inclined(), plane.in_plane())
|
||||
{
|
||||
*constraint_id = constraint_id.saturating_add(1);
|
||||
let cid = *constraint_id;
|
||||
d.line("*DEFINE_COORDINATE_SYSTEM");
|
||||
d.header(&["cid", "xo", "yo", "zo", "xl", "yl", "zl", "cidl"]);
|
||||
d.card([Field::from(cid), Field::from(0.0), Field::from(0.0), Field::from(0.0), Field::from(n[0]), Field::from(n[1]), Field::from(n[2])]);
|
||||
d.header(&["xp", "yp", "zp"]);
|
||||
d.card([Field::from(t1[0]), Field::from(t1[1]), Field::from(t1[2])]);
|
||||
d.line(&format!("*BOUNDARY_SPC_{suffix}_ID"));
|
||||
id_card(d, constraint_id, name);
|
||||
d.header(&["nsid", "cid", "dofx", "dofy", "dofz", "dofrx", "dofry", "dofrz"]);
|
||||
d.card([Field::from(entity), Field::from(cid)].into_iter().chain([1, 0, 0, 0, 1, 1].map(|v: i32| Field::from(v))));
|
||||
return Ok(());
|
||||
}
|
||||
let dofs = bc.dofs();
|
||||
// Held degrees of freedom (in a frequency step every constrained one is held).
|
||||
let held: Vec<u8> = dofs.list().into_iter().filter(|dof| !is_static || bc.value(*dof).is_none_or(|v| v == 0.0)).collect();
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
//! data (the entries). What the model holds becomes:
|
||||
//!
|
||||
//! - nodes and reference points: `GRID`;
|
||||
//! - solids: `CTETRA` (tet4, tet10), `CPENTA` (wedge6), `CHEXA` (hex8, hex20) with `PSOLID`;
|
||||
//! - solids: `CTETRA` (tet4, tet10), `CPENTA` (wedge6), `CPYRAM` (pyramid5), `CHEXA` (hex8, hex20) with `PSOLID`;
|
||||
//! shells: `CTRIA3`, `CQUAD4`, `CTRIA6`, `CQUAD8` with `PSHELL`; beams: `CBEAM` with `PBEAML`;
|
||||
//! rods: `CROD` with `PROD`. Nastran numbers a 20-node hexahedron's mid-side nodes bottom
|
||||
//! edges, vertical edges, top edges, where the model has bottom, top, vertical
|
||||
@@ -22,6 +22,9 @@
|
||||
//! the basic X axis through a `CORD2R`;
|
||||
//! - boundary conditions: per step, one constraint set of `SPC1` (held degrees of freedom) and
|
||||
//! `SPC` (prescribed values); in a modes step every constrained degree of freedom is held;
|
||||
//! symmetry about a plane not normal to X, Y or Z: one `MPC` set per step, an equation per node
|
||||
//! and held direction (n · u = 0, and t · θ = 0 about the plane's two axes at shell and beam
|
||||
//! grids), its dependent component one no `SPC` or rigid element constrains;
|
||||
//! - forces, moments and tractions: `FORCE` and `MOMENT` at the nodal values the model resolves,
|
||||
//! so every writer and the solver share one distribution; pressure: `PLOAD4` per element face;
|
||||
//! gravity: `GRAV`; a step with several loads combines them with `LOAD`;
|
||||
@@ -110,6 +113,7 @@ pub fn element_card(kind: ElementKind) -> &'static str {
|
||||
ElementKind::Quad8 => "CQUAD8",
|
||||
ElementKind::Tet4 | ElementKind::Tet10 => "CTETRA",
|
||||
ElementKind::Wedge6 => "CPENTA",
|
||||
ElementKind::Pyramid5 => "CPYRAM",
|
||||
ElementKind::Hex8 | ElementKind::Hex20 => "CHEXA",
|
||||
}
|
||||
}
|
||||
@@ -241,6 +245,8 @@ type Constraints = BTreeMap<u32, [Option<f64>; 6]>;
|
||||
struct Subcase<'m> {
|
||||
step: &'m Step,
|
||||
spc: Option<u32>,
|
||||
/// The equations of its inclined symmetry planes.
|
||||
mpc: Option<u32>,
|
||||
load: Option<u32>,
|
||||
method: Option<u32>,
|
||||
}
|
||||
@@ -269,6 +275,8 @@ struct Deck<'m> {
|
||||
dependent: BTreeMap<u32, (Dofs, &'m str)>,
|
||||
/// Constraint sets by id.
|
||||
spc_sets: Vec<(u32, Constraints)>,
|
||||
/// Multipoint constraint sets (inclined symmetry planes) by id.
|
||||
mpc_sets: Vec<(u32, Vec<crate::Equation>)>,
|
||||
/// Load name → load set id.
|
||||
load_sids: BTreeMap<&'m str, u32>,
|
||||
/// Combinations: (id, load set ids).
|
||||
@@ -310,7 +318,7 @@ impl<'m> Deck<'m> {
|
||||
model,
|
||||
r,
|
||||
sol,
|
||||
subcases: steps.into_iter().map(|step| Subcase { step, spc: None, load: None, method: None }).collect(),
|
||||
subcases: steps.into_iter().map(|step| Subcase { step, spc: None, mpc: None, load: None, method: None }).collect(),
|
||||
problems,
|
||||
next_sid: 1,
|
||||
materials: BTreeMap::new(),
|
||||
@@ -321,6 +329,7 @@ impl<'m> Deck<'m> {
|
||||
discrete: Vec::new(),
|
||||
dependent: BTreeMap::new(),
|
||||
spc_sets: Vec::new(),
|
||||
mpc_sets: Vec::new(),
|
||||
load_sids: BTreeMap::new(),
|
||||
combinations: Vec::new(),
|
||||
}
|
||||
@@ -587,6 +596,7 @@ impl<'m> Deck<'m> {
|
||||
/// One constraint set per distinct step constraint, checked against coupled nodes.
|
||||
fn plan_constraints(&mut self) -> Result<()> {
|
||||
let (model, r) = (self.model, self.r);
|
||||
let rotating = model.rotating_nodes();
|
||||
// Conflicts, one problem per pair of definitions: the nodes they meet on.
|
||||
let mut clashes: BTreeMap<(&str, &str, &str), BTreeSet<u32>> = BTreeMap::new();
|
||||
let mut on_dependent: BTreeMap<(&str, &str), BTreeSet<u32>> = BTreeMap::new();
|
||||
@@ -598,6 +608,10 @@ impl<'m> Deck<'m> {
|
||||
for name in &step.boundary_conditions {
|
||||
let Some(bc) = model.boundary_condition(name) else { continue };
|
||||
let dofs = bc.dofs();
|
||||
// Inclined symmetry planes hold no global degree of freedom: they are equations.
|
||||
if dofs.is_empty() {
|
||||
continue;
|
||||
}
|
||||
// In a modes step every constrained degree of freedom is held.
|
||||
let values: Vec<(u8, f64)> = dofs.list().into_iter().filter_map(|d| Some((d, if modes { 0.0 } else { bc.value(d)? }))).collect();
|
||||
for &node in r.target_nodes(bc.target()) {
|
||||
@@ -622,6 +636,19 @@ impl<'m> Deck<'m> {
|
||||
}
|
||||
}
|
||||
}
|
||||
let dependent = &self.dependent;
|
||||
let held = |node: u32, dof: u8| match (
|
||||
dependent.get(&node),
|
||||
set.get(&node).and_then(|v| v.get(usize::from(dof).checked_sub(1)?).copied().flatten()),
|
||||
) {
|
||||
(Some((d, _)), _) if d.contains(dof) => crate::Held::Sometimes,
|
||||
(_, Some(v)) => crate::Held::Always(v),
|
||||
_ => crate::Held::Free,
|
||||
};
|
||||
let equations = crate::symmetry_equations(model, r, &step.boundary_conditions, &rotating, held, &mut self.problems);
|
||||
if !equations.is_empty() {
|
||||
sub.mpc = Some(intern(&mut self.mpc_sets, &mut self.next_sid, equations)?);
|
||||
}
|
||||
if !set.is_empty() {
|
||||
sub.spc = Some(intern(&mut self.spc_sets, &mut self.next_sid, set)?);
|
||||
}
|
||||
@@ -707,7 +734,7 @@ impl<'m> Deck<'m> {
|
||||
let _ = writeln!(out, "SUBCASE {}", i + 1);
|
||||
let title: String = format!(" SUBTITLE = {}", sub.step.name).chars().take(72).collect();
|
||||
let _ = writeln!(out, "{title}");
|
||||
for (key, sid) in [("SPC", sub.spc), ("LOAD", sub.load), ("METHOD", sub.method)] {
|
||||
for (key, sid) in [("SPC", sub.spc), ("MPC", sub.mpc), ("LOAD", sub.load), ("METHOD", sub.method)] {
|
||||
if let Some(sid) = sid {
|
||||
let _ = writeln!(out, " {key} = {sid}");
|
||||
}
|
||||
@@ -715,6 +742,7 @@ impl<'m> Deck<'m> {
|
||||
for v in &sub.step.output {
|
||||
let request = match v {
|
||||
OutputVariable::Displacement => "DISPLACEMENT",
|
||||
OutputVariable::ReactionForce if sub.mpc.is_some() => "SPCFORCES = ALL\n MPCFORCES",
|
||||
OutputVariable::ReactionForce => "SPCFORCES",
|
||||
OutputVariable::Stress => "STRESS",
|
||||
OutputVariable::Strain => "STRAIN",
|
||||
@@ -892,6 +920,23 @@ impl<'m> Deck<'m> {
|
||||
Card::new("SPC").int(*sid).int(node).int(component(Dofs::from_bits(mask))).real(value).write(out)?;
|
||||
}
|
||||
}
|
||||
for (sid, equations) in &self.mpc_sets {
|
||||
for sub in self.subcases.iter().filter(|s| s.mpc == Some(*sid)) {
|
||||
comment(out, &format!("step {}: inclined symmetry planes, MPC {sid} (the first grid component is dependent)", sub.step.name));
|
||||
}
|
||||
for e in equations {
|
||||
// SID, then (G, C, A) triples: two on the first line (its last field blank), two
|
||||
// per continuation after a blank first field.
|
||||
let mut card = Card::new("MPC").int(*sid);
|
||||
for (i, (dof, c)) in e.terms.iter().enumerate() {
|
||||
if i % 2 == 0 && i > 0 {
|
||||
card = card.blank().blank();
|
||||
}
|
||||
card = card.int(e.node).int(i64::from(*dof)).real(*c);
|
||||
}
|
||||
card.write(out)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -461,10 +461,11 @@ fn written_connectivity_has_a_positive_jacobian_in_abaqus_conventions() {
|
||||
#[test]
|
||||
fn model_faces_are_abaqus_faces() {
|
||||
// Each face's node positions (1-based, Abaqus element library, our transcription).
|
||||
let table: [(ElementKind, &[&[usize]]); 3] = [
|
||||
let table: [(ElementKind, &[&[usize]]); 4] = [
|
||||
(ElementKind::Hex8, &[&[1, 2, 3, 4], &[5, 8, 7, 6], &[1, 5, 6, 2], &[2, 6, 7, 3], &[3, 7, 8, 4], &[4, 8, 5, 1]]),
|
||||
(ElementKind::Tet4, &[&[1, 2, 3], &[1, 4, 2], &[2, 4, 3], &[3, 4, 1]]),
|
||||
(ElementKind::Wedge6, &[&[1, 2, 3], &[4, 6, 5], &[1, 4, 5, 2], &[2, 5, 6, 3], &[3, 6, 4, 1]]),
|
||||
(ElementKind::Pyramid5, &[&[1, 2, 3, 4], &[1, 5, 2], &[2, 5, 3], &[3, 5, 4], &[4, 5, 1]]),
|
||||
];
|
||||
for (kind, faces) in table {
|
||||
assert_eq!(kind.faces().len(), faces.len());
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
//! Symmetry about a plane not normal to X, Y or Z in the three decks (#242): Abaqus `*EQUATION`s,
|
||||
//! Nastran `MPC` sets and an LS-DYNA `*BOUNDARY_SPC` in a local coordinate system, on a small
|
||||
//! shell plate of our own whose diagonal x = y is the plane.
|
||||
|
||||
use fieldcraft_decks::{Error, abaqus, lsdyna, nastran};
|
||||
use fieldcraft_model::{
|
||||
BoundaryCondition, Elastic, ElementFace, ElementKind, Formulation, Load, Material, Model, Procedure, Section, SectionKind, Set, Step, Symmetry,
|
||||
Target,
|
||||
};
|
||||
|
||||
/// A 2 × 2 quad4 shell plate in the XY plane (nodes 1–9, x and y in 0, 1, 2), clamped on x = 0,
|
||||
/// the inclined plane (normal (1, −1, 0)) on the diagonal nodes 1, 5 and 9, under pressure;
|
||||
/// steps `Load` (static) and `Modes`, `in_modes` saying whether the plane is active in both.
|
||||
fn plate(in_modes: bool) -> Model {
|
||||
let mut m = Model::default();
|
||||
for j in 0..3u32 {
|
||||
for i in 0..3u32 {
|
||||
m.set_node(1 + i + 3 * j, [f64::from(i), f64::from(j), 0.0]).unwrap();
|
||||
}
|
||||
}
|
||||
for (e, n) in [(1, [1, 2, 5, 4]), (2, [2, 3, 6, 5]), (3, [4, 5, 8, 7]), (4, [5, 6, 9, 8])] {
|
||||
m.set_element(e, ElementKind::Quad4, n.to_vec()).unwrap();
|
||||
}
|
||||
m.add_material(
|
||||
"Steel",
|
||||
Material { elastic: Some(Elastic::Isotropic { young: 210e3, poisson: 0.3 }), density: Some(7.85e-9), ..Material::default() },
|
||||
)
|
||||
.unwrap();
|
||||
m.add_set("all", Set { elements: vec![1, 2, 3, 4], ..Set::default() }).unwrap();
|
||||
let kind = SectionKind::Shell { thickness: 0.1, offset: 0.0, points: 5 };
|
||||
m.add_section("plate", Section { set: "all".into(), material: "Steel".into(), kind, formulation: Formulation::default() }).unwrap();
|
||||
m.add_set("edge", Set { nodes: vec![1, 4, 7], ..Set::default() }).unwrap();
|
||||
m.add_set("diagonal", Set { nodes: vec![1, 5, 9], ..Set::default() }).unwrap();
|
||||
let faces = (1..=4).map(|element| ElementFace { element, face: 1 }).collect();
|
||||
m.add_set("top", Set { faces, ..Set::default() }).unwrap();
|
||||
m.add_boundary_condition("clamp", BoundaryCondition::Fixed { target: Target::Set("edge".into()) }).unwrap();
|
||||
let plane = Symmetry::from_normal([1.0, -1.0, 0.0]).unwrap();
|
||||
m.add_boundary_condition("sym", BoundaryCondition::Symmetry { target: Target::Set("diagonal".into()), plane }).unwrap();
|
||||
m.add_load("q", Load::Pressure { set: "top".into(), magnitude: 0.01 }).unwrap();
|
||||
let mut load = Step::new("Load", Procedure::Static {});
|
||||
load.boundary_conditions = ["clamp", "sym"].map(String::from).into();
|
||||
load.loads = ["q".to_string()].into();
|
||||
m.add_step(load, None).unwrap();
|
||||
let mut modes = Step::new("Modes", Procedure::Frequency { modes: 3, min_hz: None, max_hz: None });
|
||||
modes.boundary_conditions = if in_modes { ["clamp", "sym"].map(String::from).into() } else { ["clamp".to_string()].into() };
|
||||
m.add_step(modes, None).unwrap();
|
||||
m
|
||||
}
|
||||
|
||||
/// A Nastran real: an exponent may come without `E`.
|
||||
fn real(s: &str) -> f64 {
|
||||
let split = s.char_indices().skip(1).find(|(i, c)| (*c == '+' || *c == '-') && !s[..*i].ends_with(['E', 'e']));
|
||||
match split {
|
||||
Some((i, _)) => format!("{}E{}", &s[..i], &s[i..]).parse().unwrap(),
|
||||
None => s.parse().unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
fn dot(a: [f64; 3], b: [f64; 3]) -> f64 {
|
||||
(0..3).map(|k| a[k] * b[k]).sum()
|
||||
}
|
||||
|
||||
/// Every equation is one of the plane's at a free node: its terms on one node, its coefficients
|
||||
/// along the normal (translations) or along an in-plane axis (rotations), unit length; nodes 5
|
||||
/// and 9 get three each (n · u, t₁ · θ, t₂ · θ); node 1 is clamped, so its equations are left out;
|
||||
/// no dependent degree of freedom repeats.
|
||||
fn check(equations: &[(u32, Vec<(u8, f64)>)]) {
|
||||
let n = [1.0 / 2f64.sqrt(), -1.0 / 2f64.sqrt(), 0.0];
|
||||
let mut dependents = std::collections::BTreeSet::new();
|
||||
let mut per_node = std::collections::BTreeMap::<u32, usize>::new();
|
||||
for (node, terms) in equations {
|
||||
assert!(dependents.insert((*node, terms[0].0)), "{node} {terms:?}");
|
||||
*per_node.entry(*node).or_default() += 1;
|
||||
let mut v = [0.0; 3];
|
||||
let rotation = terms[0].0 > 3;
|
||||
for (dof, c) in terms {
|
||||
assert_eq!(*dof > 3, rotation, "{terms:?}");
|
||||
v[usize::from((dof - 1) % 3)] = *c;
|
||||
}
|
||||
assert!((dot(v, v) - 1.0).abs() < 1e-12, "{terms:?}");
|
||||
if rotation {
|
||||
assert!(dot(v, n).abs() < 1e-12, "{terms:?}");
|
||||
} else {
|
||||
assert!((dot(v, n).abs() - 1.0).abs() < 1e-12, "{terms:?}");
|
||||
}
|
||||
}
|
||||
assert_eq!(per_node, [(5, 3), (9, 3)].into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn abaqus_writes_equations_in_the_model_data() {
|
||||
let deck = abaqus::write(&plate(true)).unwrap();
|
||||
let lines: Vec<&str> = deck.lines().collect();
|
||||
let mut equations = Vec::new();
|
||||
for (i, l) in lines.iter().enumerate() {
|
||||
if *l == "*EQUATION" {
|
||||
let count: usize = lines[i + 1].trim().parse().unwrap();
|
||||
let f: Vec<&str> = lines[i + 2].split(',').map(str::trim).collect();
|
||||
assert_eq!(f.len(), 3 * count, "{}", lines[i + 2]);
|
||||
let node: u32 = f[0].parse().unwrap();
|
||||
let terms = f.chunks(3).map(|t| (t[1].parse().unwrap(), t[2].parse().unwrap())).collect();
|
||||
assert!(f.chunks(3).all(|t| t[0] == f[0]));
|
||||
equations.push((node, terms));
|
||||
}
|
||||
}
|
||||
check(&equations);
|
||||
// Model data, before the first step; the plane is no `*BOUNDARY` line.
|
||||
let first_step = deck.find("*STEP").unwrap();
|
||||
assert!(deck.rfind("*EQUATION").unwrap() < first_step);
|
||||
assert!(!deck.contains("SYMM"), "{deck}");
|
||||
|
||||
let e = abaqus::write(&plate(false)).unwrap_err();
|
||||
assert!(matches!(&e, Error::Unsupported { what, .. } if what.contains("boundary condition `sym`") && what.contains("some steps only")), "{e}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nastran_writes_one_mpc_set_per_step() {
|
||||
// A static deck and a modes deck (one solution per run).
|
||||
for (steps, sol) in [("Load", 101), ("Modes", 103)] {
|
||||
let m = fieldcraft_decks::with_steps(&plate(true), &[steps.to_string()]).unwrap();
|
||||
let deck = nastran::write(&m).unwrap();
|
||||
assert!(deck.contains(&format!("SOL {sol}")));
|
||||
let sid = deck.lines().find_map(|l| l.trim().strip_prefix("MPC = ")).unwrap().to_string();
|
||||
// MPC entries: SID, then (grid, component, coefficient) triples; the continuation after
|
||||
// two triples starts with a blank field. Large-field lines are 16 columns per field.
|
||||
let mut equations = Vec::new();
|
||||
let mut fields: Vec<String> = Vec::new();
|
||||
let flush = |fields: &mut Vec<String>, equations: &mut Vec<(u32, Vec<(u8, f64)>)>| {
|
||||
if fields.is_empty() {
|
||||
return;
|
||||
}
|
||||
assert_eq!(fields[0], sid);
|
||||
let data: Vec<&String> = fields[1..].iter().enumerate().filter(|(i, _)| !matches!(i % 8, 6 | 7)).map(|(_, f)| f).collect();
|
||||
let triples: Vec<(u32, u8, f64)> = data
|
||||
.chunks(3)
|
||||
.filter(|t| t.len() == 3 && !t[0].is_empty())
|
||||
.map(|t| (t[0].parse().unwrap(), t[1].parse().unwrap(), real(t[2])))
|
||||
.collect();
|
||||
assert!(triples.iter().all(|t| t.0 == triples[0].0));
|
||||
equations.push((triples[0].0, triples.iter().map(|t| (t.1, t.2)).collect()));
|
||||
fields.clear();
|
||||
};
|
||||
for line in deck.lines() {
|
||||
let (head, rest) = line.split_at(8.min(line.len()));
|
||||
let row: Vec<String> = rest.as_bytes().chunks(16).map(|c| String::from_utf8_lossy(c).trim().to_string()).collect();
|
||||
let mut row = row;
|
||||
row.resize(4, String::new());
|
||||
match head.trim() {
|
||||
"MPC*" => {
|
||||
flush(&mut fields, &mut equations);
|
||||
fields.extend(row);
|
||||
}
|
||||
"*" if !fields.is_empty() => fields.extend(row),
|
||||
_ => flush(&mut fields, &mut equations),
|
||||
}
|
||||
}
|
||||
flush(&mut fields, &mut equations);
|
||||
check(&equations);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lsdyna_holds_the_plane_in_a_local_system() {
|
||||
let deck = lsdyna::write(&plate(true)).unwrap().text;
|
||||
let block = |keyword: &str| -> Vec<Vec<f64>> {
|
||||
let at = deck.find(keyword).unwrap();
|
||||
deck[at..]
|
||||
.lines()
|
||||
.skip(1)
|
||||
.filter(|l| !l.starts_with('$'))
|
||||
.take_while(|l| !l.starts_with('*'))
|
||||
.map(|l| l.as_bytes().chunks(20).filter_map(|c| String::from_utf8_lossy(c).trim().parse().ok()).collect())
|
||||
.collect()
|
||||
};
|
||||
let system = block("*DEFINE_COORDINATE_SYSTEM");
|
||||
let (cid, x, p) = (system[0][0], [system[0][4], system[0][5], system[0][6]], [system[1][0], system[1][1], system[1][2]]);
|
||||
assert!((x[0] + x[1]).abs() < 1e-12 && x[2] == 0.0 && (dot(x, x) - 1.0).abs() < 1e-12, "{x:?}");
|
||||
assert!(dot(x, p).abs() < 1e-12, "{p:?}");
|
||||
// The SPC after the system: its id card, then nsid, cid and the held local directions.
|
||||
let at = deck.find("*DEFINE_COORDINATE_SYSTEM").unwrap();
|
||||
let spc = deck[at..].find("*BOUNDARY_SPC_SET_ID").unwrap() + at;
|
||||
let card: Vec<f64> = deck[spc..]
|
||||
.lines()
|
||||
.skip(2)
|
||||
.find(|l| !l.starts_with('$'))
|
||||
.unwrap()
|
||||
.as_bytes()
|
||||
.chunks(20)
|
||||
.map(|c| String::from_utf8_lossy(c).trim().parse().unwrap())
|
||||
.collect();
|
||||
assert_eq!(card[1], cid);
|
||||
assert_eq!(&card[2..], [1.0, 0.0, 0.0, 0.0, 1.0, 1.0]);
|
||||
}
|
||||
@@ -17,7 +17,7 @@ use serde_json::{Value, json};
|
||||
use crate::crash::without_home;
|
||||
|
||||
/// The issue form.
|
||||
pub const ISSUE_URL: &str = "https://github.com/jyviko/fieldcraft/issues/new";
|
||||
pub const ISSUE_URL: &str = "https://github.com/tangency-labs/fieldcraft/issues/new";
|
||||
/// The longest link a draft opens: GitHub refuses new-issue links much past 8 KB.
|
||||
pub const MAX_URL: usize = 7000;
|
||||
/// The session file a draft attaches.
|
||||
@@ -329,7 +329,7 @@ mod tests {
|
||||
..Default::default()
|
||||
};
|
||||
let d = draft(&input, "v1.2.3, testos x");
|
||||
assert!(d.url.starts_with("https://github.com/jyviko/fieldcraft/issues/new?template=bug.yml&title="), "{}", d.url);
|
||||
assert!(d.url.starts_with("https://github.com/tangency-labs/fieldcraft/issues/new?template=bug.yml&title="), "{}", d.url);
|
||||
assert!(d.attachments.is_empty() && d.clipboard.is_none(), "nothing attached without Share my session");
|
||||
let all = format!("{} {:?} {}", d.title, d.fields, decode(&d.url));
|
||||
assert!(!all.contains(&home()), "no home folder: {all}");
|
||||
|
||||
@@ -244,9 +244,16 @@ pub static CATALOGUE: &[Capability] = &[
|
||||
Geometry,
|
||||
"Inspect",
|
||||
"List the open design's bodies, volumes and named faces, edges and vertices",
|
||||
&["geometry.centre"],
|
||||
&["geometry.centre", "geometry.features"],
|
||||
),
|
||||
ready("geometry.tessellate", "Tessellate", Geometry, "Inspect", "Tessellate a body into a face-tagged boundary surface"),
|
||||
ready(
|
||||
"geometry.check",
|
||||
"Check Geometry",
|
||||
Geometry,
|
||||
"Inspect",
|
||||
"Short edges, small and sliver faces and near faces against the element size, located",
|
||||
),
|
||||
ready(
|
||||
"mesh.surface.check",
|
||||
"Check Surface",
|
||||
@@ -287,6 +294,45 @@ pub static CATALOGUE: &[Capability] = &[
|
||||
"Cut bodies into cells by a datum plane; the two faces of each cut are paired by name",
|
||||
&["geometry.partition.delete", "geometry.partition.suppress"],
|
||||
),
|
||||
covering(
|
||||
"geometry.faces.merge",
|
||||
"Merge Faces",
|
||||
Geometry,
|
||||
"Prepare",
|
||||
"Mesh adjacent faces as one (virtual topology): a fillet in pieces, a face split by an imprint; the B-rep is unchanged",
|
||||
&["geometry.faces.merge.delete"],
|
||||
),
|
||||
ready(
|
||||
"geometry.face.absorb",
|
||||
"Absorb Faces",
|
||||
Geometry,
|
||||
"Prepare",
|
||||
"Absorb tiny and sliver faces into a neighbour for meshing, one picked or every one Check Geometry found",
|
||||
),
|
||||
covering(
|
||||
"geometry.face.split",
|
||||
"Split Face",
|
||||
Geometry,
|
||||
"Prepare",
|
||||
"Split a face along a datum plane for meshing only: two faces on its surface, the cut an edge between them",
|
||||
&["geometry.face.split.delete"],
|
||||
),
|
||||
covering(
|
||||
"geometry.defeature",
|
||||
"Defeature",
|
||||
Geometry,
|
||||
"Prepare",
|
||||
"Take small holes, fillets, chamfers and bosses out for meshing, from a proposal list with each one on or off",
|
||||
&["geometry.defeature.propose", "geometry.defeature.delete"],
|
||||
),
|
||||
covering(
|
||||
"geometry.stitch",
|
||||
"Stitch",
|
||||
Geometry,
|
||||
"Prepare",
|
||||
"Close an imported surface's gaps under a tolerance: open points welded, T-junctions split",
|
||||
&["geometry.stitch.delete"],
|
||||
),
|
||||
soon("geometry.midsurface", "Midsurface", Geometry, "Prepare", "M8", "Extract midsurfaces of thin parts for shell meshes"),
|
||||
// ---- Mesh ----
|
||||
soon("mesh.size.global", "Global Size", Mesh, "Size", "M2", "One element size for the whole part"),
|
||||
@@ -298,6 +344,14 @@ pub static CATALOGUE: &[Capability] = &[
|
||||
"Element counts or sizes per design edge, with bias and an exact, minimum or free constraint; the meshers honour them",
|
||||
&["mesh.seed.clear", "mesh.seed.list"],
|
||||
),
|
||||
ready_more(
|
||||
"mesh.size.set",
|
||||
"Local Size",
|
||||
Mesh,
|
||||
"Size",
|
||||
"Element size on faces or bodies, in a box or in a sphere, growing away at a set rate; every mesher honours it",
|
||||
&["mesh.size.growth", "mesh.size.clear", "mesh.size.list"],
|
||||
),
|
||||
soon("mesh.size.adapt", "Curvature", Mesh, "Size", "M2", "Refine by curvature and proximity"),
|
||||
ready(
|
||||
"mesh.tet.generate",
|
||||
@@ -329,10 +383,32 @@ pub static CATALOGUE: &[Capability] = &[
|
||||
"Partitioned Mesh",
|
||||
Mesh,
|
||||
"Structured Hex",
|
||||
"One mesh of a partitioned part, a method per cell (hex, tet, or all hex as conforming blocks); cells joined across partitions",
|
||||
"One mesh of a partitioned part, a method per cell (hex, tet, or all hex as conforming blocks); cells joined across partitions, tet cells to hex cells through conforming pyramids",
|
||||
&["mesh.hex.blocks"],
|
||||
),
|
||||
ready("mesh.block", "Mesh Block", Mesh, "Structured Hex", "A mapped block of hex, wedge or quad elements between corner points"),
|
||||
ready(
|
||||
"mesh.hex.extrude",
|
||||
"Extrude to Surface",
|
||||
Mesh,
|
||||
"Structured Hex",
|
||||
"Hex layers from a meshed or planar section up to a curved target surface, following the side faces",
|
||||
),
|
||||
ready_more(
|
||||
"mesh.plan",
|
||||
"Mesh Plan",
|
||||
Mesh,
|
||||
"Structured Hex",
|
||||
"A hex-meshing plan from the features that built a body (sweeps, blocks, O-grids, layers, imprints, copies); saved, editable, applied with the structured meshers",
|
||||
&["mesh.plan.apply"],
|
||||
),
|
||||
ready_more(
|
||||
"mesh.block",
|
||||
"Mesh Block",
|
||||
Mesh,
|
||||
"Structured Hex",
|
||||
"A mapped block of hex, wedge or quad elements between corner points; projection blocking (a block topology projected onto the part)",
|
||||
&["mesh.hex.project"],
|
||||
),
|
||||
soon(
|
||||
"mesh.hex.decompose",
|
||||
"Decompose",
|
||||
@@ -341,7 +417,6 @@ pub static CATALOGUE: &[Capability] = &[
|
||||
"M5",
|
||||
"Find sweepable and mappable regions of 3D parts (extruded cross-sections are already cut into blocks by Hex Mesh)",
|
||||
),
|
||||
soon("mesh.transition", "Transition", Mesh, "Structured Hex", "M5", "Conforming tet–hex interfaces with pyramids"),
|
||||
ready(
|
||||
"mesh.hex.octree",
|
||||
"Octree Hex",
|
||||
@@ -376,7 +451,15 @@ pub static CATALOGUE: &[Capability] = &[
|
||||
soon("mesh.region.remesh", "Remesh Region", Mesh, "Edit", "M6", "Remesh a region in place; split and collapse elements"),
|
||||
soon("mesh.import.orphan", "Orphan Mesh", Mesh, "Edit", "M6", "Import a mesh without geometry"),
|
||||
ready("mesh.quality", "Quality", Mesh, "Verify", "Quality metrics and the certified validity check, failing elements listed"),
|
||||
ready(
|
||||
"mesh.quality.mark",
|
||||
"Mark Bad Elements",
|
||||
Mesh,
|
||||
"Verify",
|
||||
"Mark the elements below quality limits as a set, located and drawn in the marker colour",
|
||||
),
|
||||
soon("mesh.verify", "Verify Set", Mesh, "Verify", "M2", "Quality and validity per set"),
|
||||
ready("mesh.export.stl", "Export STL", Mesh, "Verify", "Write the mesh's outer surface (or a set's faces) as binary STL"),
|
||||
ready_more("mesh.tag", "Tag Geometry", Mesh, "Verify", "Tag elements, faces, edges and nodes with the geometry they mesh", &["mesh.tag.clear"]),
|
||||
// ---- Properties ----
|
||||
ready_more(
|
||||
@@ -590,11 +673,12 @@ pub static CATALOGUE: &[Capability] = &[
|
||||
),
|
||||
ready_more("results.reactions", "Reactions", Results, "Query", "Reaction force and moment sums per set", &["results.export.csv"]),
|
||||
soon("results.free_body", "Free Body", Results, "Query", "M9", "Free-body forces across a cut"),
|
||||
soon("results.xy", "XY Plot", Results, "Plots", "M9", "Plot a quantity against time or another"),
|
||||
ready("results.xy", "XY Plot", Results, "Plots", "Plot a quantity against the load factor, time or mode"),
|
||||
soon("results.path", "Path Plot", Results, "Plots", "M9", "Plot a quantity along a path"),
|
||||
soon("results.cut", "Cut", Results, "Plots", "M9", "Section cuts through the model"),
|
||||
soon("results.display_group", "Display Group", Results, "Plots", "M9", "Show and hide parts of the model"),
|
||||
soon("results.streamlines", "Streamlines", Results, "Plots", "M12", "Streamlines and flow fields"),
|
||||
ready("results.export.vtu", "Export VTU", Results, "Files", "Write a solved step as VTK unstructured grids for ParaView"),
|
||||
soon("results.read", "Read Results", Results, "Files", "M9", "Read results in public formats"),
|
||||
soon("results.convergence", "Convergence", Results, "Files", "M9", "Mesh convergence studies"),
|
||||
// ---- Tools ----
|
||||
|
||||
@@ -28,21 +28,20 @@ use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use fieldcraft_mesh::cdt::CdtOptions;
|
||||
use fieldcraft_mesh::hex::{Cell, CellsMesh, HexMesh, HexMethod, HexOptions, hex_mesh_swept_cells};
|
||||
use fieldcraft_mesh::size::UniformSize;
|
||||
use fieldcraft_mesh::surface::BoundarySurface;
|
||||
use fieldcraft_model::geometry::Entity;
|
||||
use fieldcraft_model::{ElementFace, GeometryRef, Model, Set, Tie};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::hex::{Input, add_cells, add_hex, hex_body};
|
||||
use super::mesh::{BodyTets, add_tets, mesh_body, quality_options};
|
||||
use super::mesh::{add_tets, mesh_body, quality_options};
|
||||
use crate::requirement::Requirement;
|
||||
use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
|
||||
pub const CELLS: CommandSpec = CommandSpec {
|
||||
id: "mesh.cells",
|
||||
label: "Partitioned Mesh",
|
||||
params: r#"{"methods": {"Body1/a": "tet", "Body1/b": "swept"} (per cell; default `default`), "default": "auto", "bodies": ["Body1/a", "Body1/b"] (default: every body of the open design), "size": 2.0 (element size, mm; default 1/25 of the bounding-box diagonal of all of them), "order": 1 | 2 (hex8 and tet4, or hex20 and tet10), "tolerance": 0.05 (chordal tolerance, mm)} — replaces the cells' meshes (as mesh.tet.generate; other bodies keep theirs) with one mesh of the cells, each with its method: "auto" (structured hex where a method fits, else tet), "hex" (any structured hex method), "swept", "extrude", "sweep", "revolve", "mapped" or "tet"; or "default": "blocks" (with no `methods`): every cell a mapped block, division counts assigned over all cells together, exactly mesh.hex.blocks with the same parameters. Cells on either side of a partition (faces `partition:<name>:a` and `:b`) are joined: where their element faces match node for node the nodes are merged (conforming); otherwise, as between a hex and a tet cell, they are tied: each cell's tied cut faces make one tie, `tie_<cell>` (`/` as `_`; sets `<tie>_main`, the facing cells' cut faces, and `<tie>_secondary`, the cell's own), hex cells main to tet cells and otherwise by name, so a node is a secondary node of one such tie only; the ties an earlier run made on these cells are replaced. The cells of one design body that may sweep (auto, hex, swept, extrude, revolve) are swept together first: one cross-section and one set of layers for all of them, so they share their nodes across every cut, T-junctions included, with no edge seeds (`swept_together` names the body; `not_swept_together` says why not, and those cells are meshed one by one). Reports each cell's method (and why auto fell back to tet), its `quality` (volume, smallest and mean scaled Jacobian, largest edge ratio), and each interface: `merged` with the nodes merged or shared, or `tied`"#,
|
||||
params: r#"{"methods": {"Body1/a": "tet", "Body1/b": "swept"} (per cell; default `default`), "default": "auto", "bodies": ["Body1/a", "Body1/b"] (default: every body of the open design), "size": 2.0 (element size, mm; default 1/25 of the bounding-box diagonal of all of them), "order": 1 | 2 (hex8 and tet4, or hex20 and tet10), "tolerance": 0.05 (chordal tolerance, mm), "transition": "pyramid" | "tie" (how a tet cell meets hex cells; default pyramid)} — replaces the cells' meshes (as mesh.tet.generate; other bodies keep theirs) with one mesh of the cells, each with its method: "auto" (structured hex where a method fits, else tet), "hex" (any structured hex method), "swept", "extrude", "sweep", "revolve", "mapped" or "tet"; or "default": "blocks" (with no `methods`): every cell a mapped block, division counts assigned over all cells together, exactly mesh.hex.blocks with the same parameters. Cells on either side of a partition (faces `partition:<name>:a` and `:b`) are joined: where their element faces match node for node the nodes are merged (conforming); between a tet cell and the hex cells across its partitions, with "transition": "pyramid" (linear meshes), the tet cell is meshed after them with a pyramid (pyramid5) on every hex element face of the cut, its tetrahedra sharing the pyramids' sides, so the mesh is conforming there (`joined`: `pyramids`, with their count); where that cannot be built (a cut the hex faces do not cover exactly, quadratic elements, a pyramid that would reach through a thin cell) the cell says why in `no_pyramids` and is tied; otherwise, as between a hex and a tet cell with "transition": "tie", they are tied: each cell's tied cut faces make one tie, `tie_<cell>` (`/` as `_`; sets `<tie>_main`, the facing cells' cut faces, and `<tie>_secondary`, the cell's own), hex cells main to tet cells and otherwise by name, so a node is a secondary node of one such tie only; the ties an earlier run made on these cells are replaced. The cells of one design body that may sweep (auto, hex, swept, extrude, revolve) are swept together first: one cross-section and one set of layers for all of them, so they share their nodes across every cut, T-junctions included, with no edge seeds (`swept_together` names the body; `not_swept_together` says why not, and those cells are meshed one by one). Reports each cell's method (and why auto fell back to tet), its `quality` (volume, smallest and mean scaled Jacobian, largest edge ratio), and each interface: `merged` with the nodes merged or shared, or `tied` (with `why` when both cells are of one design body: the tet cell and why auto fell back, or why the body's cells were not swept together)"#,
|
||||
form: Some(super::forms::CELLS),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Design],
|
||||
@@ -80,10 +79,22 @@ impl Method {
|
||||
}
|
||||
}
|
||||
|
||||
/// A meshed cell.
|
||||
mod transition;
|
||||
|
||||
/// A meshed cell; tet cells are meshed after the hex cells are in the model, so they can meet
|
||||
/// them through pyramids.
|
||||
enum Meshed<'a> {
|
||||
Hex(Input<'a>, HexMesh),
|
||||
Tet(BodyTets),
|
||||
Hex(Box<(Input<'a>, HexMesh)>),
|
||||
Tet,
|
||||
}
|
||||
|
||||
/// How a tet cell joins the hex cells across its partitions.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Transition {
|
||||
/// Pyramids on the hex faces, conforming (falls back to a tie with the reason).
|
||||
Pyramid,
|
||||
/// A tie (dual mortar), each mesh as its method made it.
|
||||
Tie,
|
||||
}
|
||||
|
||||
fn cells(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
@@ -105,6 +116,11 @@ fn cells(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
_ => return Err(EngineError::invalid("order", "must be 1 or 2")),
|
||||
},
|
||||
};
|
||||
let transition = match p.get("transition").map(|v| v.as_str()) {
|
||||
None | Some(Some("pyramid")) => Transition::Pyramid,
|
||||
Some(Some("tie")) => Transition::Tie,
|
||||
Some(_) => return Err(EngineError::invalid("transition", "must be \"pyramid\" or \"tie\"")),
|
||||
};
|
||||
let default = p.get("default").map(Method::parse).transpose()?.unwrap_or(Method::Auto);
|
||||
let methods: BTreeMap<String, Method> = match p.get("methods") {
|
||||
None => BTreeMap::new(),
|
||||
@@ -156,7 +172,10 @@ fn cells(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Some(o) = hex_params.as_object_mut() {
|
||||
o.remove("order");
|
||||
}
|
||||
// The cells are meshed at one size, so the size controls apply to all of them alike.
|
||||
let sizing = super::sizes::sizing(s)?.joint(&names.iter().map(String::as_str).collect::<Vec<_>>());
|
||||
let (tet_options, quality) = quality_options(&tet_params, s.limits.max_tets as u64)?;
|
||||
let tet_options = sizing.tet_options("", &tet_params, &tet_options);
|
||||
let cdt = CdtOptions { max_steiner: s.limits.max_steiner, max_tets: tet_options.max_tets };
|
||||
|
||||
let seeds = super::seeds::edge_seeds(s)?;
|
||||
@@ -187,7 +206,7 @@ fn cells(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.filter_map(|&i| shapes.get(i))
|
||||
.map(|shape| Ok(Input { seeds: seeds.clone(), ..super::hex::shape_input(shape, Some(tolerance))? }))
|
||||
.collect::<Result<_>>()?;
|
||||
match swept_together(p, &inputs, m, quadratic, size, tolerance, &seeds)? {
|
||||
match swept_together(p, &inputs, m, quadratic, size, tolerance, &seeds, &sizing)? {
|
||||
Ok(cm) => {
|
||||
for &i in &members {
|
||||
if let Some(slot) = joint.get_mut(i) {
|
||||
@@ -212,23 +231,22 @@ fn cells(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let input = || Ok::<_, EngineError>(Input { seeds: seeds.clone(), ..super::hex::shape_input(shape, Some(tolerance))? });
|
||||
let hex = |m: HexMethod| -> Result<(Input<'static>, HexMesh)> {
|
||||
let b = input()?;
|
||||
let mesh = hex_body(&hex_params, &b, m, quadratic, size)?;
|
||||
let mesh = hex_body(&hex_params, &b, m, quadratic, size, &sizing)?;
|
||||
Ok((b, mesh))
|
||||
};
|
||||
let tet = |s: &Session| mesh_body(s, &tet_params, input()?, &cdt, &tet_options, quality, true);
|
||||
let (cell, mut info) = match method {
|
||||
Method::Hex(m) => {
|
||||
let (b, mesh) = hex(m)?;
|
||||
let info = json!({"method": mesh.report.method.name()});
|
||||
(Meshed::Hex(b, mesh), info)
|
||||
(Meshed::Hex(Box::new((b, mesh))), info)
|
||||
}
|
||||
Method::Tet => (Meshed::Tet(tet(s)?), json!({"method": "tet"})),
|
||||
Method::Tet => (Meshed::Tet, json!({"method": "tet"})),
|
||||
Method::Auto => match hex(HexMethod::Auto) {
|
||||
Ok((b, mesh)) => {
|
||||
let info = json!({"method": mesh.report.method.name()});
|
||||
(Meshed::Hex(b, mesh), info)
|
||||
(Meshed::Hex(Box::new((b, mesh))), info)
|
||||
}
|
||||
Err(e) => (Meshed::Tet(tet(s)?), json!({"method": "tet", "why": e.to_string()})),
|
||||
Err(e) => (Meshed::Tet, json!({"method": "tet", "why": e.to_string()})),
|
||||
},
|
||||
};
|
||||
if let Some(o) = info.as_object_mut() {
|
||||
@@ -259,19 +277,50 @@ fn cells(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
interfaces.extend(swept_interfaces(&design, inputs, cm));
|
||||
}
|
||||
let mut k = 0;
|
||||
for (i, _) in shapes.iter().enumerate() {
|
||||
if joint.get(i).copied().flatten().is_some() {
|
||||
continue;
|
||||
}
|
||||
let Some((cell, mut info)) = meshed.get_mut(k).map(|(c, v)| (c, std::mem::take(v))) else { break };
|
||||
k += 1;
|
||||
let (kind, elements, nodes) = match &*cell {
|
||||
Meshed::Hex(b, m) => {
|
||||
// The cells meshed alone, in the order of `shapes`: the hex ones first, then the tet ones,
|
||||
// which may meet the hex cells through pyramids.
|
||||
let alone: Vec<usize> = (0..shapes.len()).filter(|&i| joint.get(i).copied().flatten().is_none()).collect();
|
||||
let mut hex_cells: Vec<String> = swept.iter().flat_map(|(_, _, inputs, _)| inputs.iter().map(|b| b.name.clone())).collect();
|
||||
let mut tet_cells = Vec::new();
|
||||
for (&i, (cell, info)) in alone.iter().zip(meshed.iter_mut()) {
|
||||
let mut info = std::mem::take(info);
|
||||
match &*cell {
|
||||
Meshed::Hex(hex) => {
|
||||
let (b, m) = &**hex;
|
||||
add_hex(model, b, m)?;
|
||||
(m.kind, m.element_count(), m.nodes.len())
|
||||
hex_cells.push(b.name.clone());
|
||||
if let Some(o) = info.as_object_mut() {
|
||||
o.insert("kind".into(), json!(m.kind.name()));
|
||||
o.insert("elements".into(), json!(m.element_count()));
|
||||
o.insert("nodes".into(), json!(m.nodes.len()));
|
||||
}
|
||||
report.push((i, info));
|
||||
}
|
||||
Meshed::Tet(t) => {
|
||||
Meshed::Tet => tet_cells.push((i, info)),
|
||||
}
|
||||
}
|
||||
for (i, mut info) in tet_cells {
|
||||
let Some(shape) = shapes.get(i) else { continue };
|
||||
let input = Input { seeds: seeds.clone(), ..super::hex::shape_input(shape, Some(tolerance))? };
|
||||
let pyramids = if transition == Transition::Tie {
|
||||
Err("`transition` is \"tie\"".to_string())
|
||||
} else if quadratic {
|
||||
Err("pyramid transitions are linear (pyramid5); order 2 is tied".to_string())
|
||||
} else {
|
||||
transition::mesh_with_pyramids(&design, model, &tet_params, &input, &hex_cells, &cdt, &tet_options)?
|
||||
};
|
||||
let (kind, elements, nodes) = match pyramids {
|
||||
Ok(c) => {
|
||||
interfaces.extend(c.interfaces.iter().cloned());
|
||||
let kind = c.tets.mesh.kind;
|
||||
let (elements, nodes) = transition::add(model, c)?;
|
||||
(kind, elements, nodes)
|
||||
}
|
||||
Err(why) => {
|
||||
if let Some(o) = info.as_object_mut() {
|
||||
o.insert("no_pyramids".into(), json!(why));
|
||||
}
|
||||
let t = mesh_body(Some(&design), &tet_params, input, &cdt, &tet_options, quality, true)?;
|
||||
add_tets(model, &t.name, &t.face_names, &t.mesh)?;
|
||||
(t.mesh.kind, t.mesh.elements.len(), t.mesh.points.len())
|
||||
}
|
||||
@@ -300,6 +349,7 @@ fn cells(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.filter_map(|r| r.get("body").and_then(Value::as_str).map(String::from))
|
||||
.collect();
|
||||
interfaces.extend(join(model, &names, &tet, COINCIDENT * diagonal)?);
|
||||
explain_ties(&design, &report, &mut interfaces);
|
||||
let (nodes, elements) = (model.nodes().len(), model.elements().len());
|
||||
Ok(cleared.report(s, json!({"cells": report, "interfaces": interfaces, "nodes": nodes, "elements": elements, "size": size})))
|
||||
}
|
||||
@@ -315,8 +365,10 @@ fn swept_together(
|
||||
size: Option<f64>,
|
||||
tolerance: f64,
|
||||
seeds: &[fieldcraft_mesh::seeds::EdgeSeed],
|
||||
sizing: &super::sizes::Sizing,
|
||||
) -> Result<std::result::Result<CellsMesh, String>> {
|
||||
let h = size.ok_or_else(|| EngineError::Params("no element size".into()))?;
|
||||
let field = sizing.field("", h)?;
|
||||
let mut surfaces = Vec::with_capacity(inputs.len());
|
||||
for b in inputs {
|
||||
let options = super::mesh::surface_options(p, &b.mesh)?;
|
||||
@@ -334,7 +386,7 @@ fn swept_together(
|
||||
seeds: seeds.to_vec(),
|
||||
..HexOptions::default()
|
||||
};
|
||||
match hex_mesh_swept_cells(&cells, &UniformSize(h), &options) {
|
||||
match hex_mesh_swept_cells(&cells, &field, &options) {
|
||||
Ok(cm) => Ok(Ok(cm)),
|
||||
Err(fieldcraft_mesh::Error::Model(fieldcraft_model::Error::Cancelled)) => Err(EngineError::Model(fieldcraft_model::Error::Cancelled)),
|
||||
Err(e @ fieldcraft_mesh::Error::Limit(_)) => Err(EngineError::Params(e.to_string())),
|
||||
@@ -542,6 +594,52 @@ fn join(model: &mut Model, cells: &[String], tet: &BTreeSet<String>, tol: f64) -
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Give every tied interface between cells of one design body its reason (`why`), from the
|
||||
/// cells' reports (#396): a cell meshed with tet (and why auto fell back), the reason the body's
|
||||
/// cells did not sweep together, or a cell whose method does not sweep with the others; so an
|
||||
/// agent knows which cut or method to change for a conforming mesh.
|
||||
fn explain_ties(design: &fieldcraft_geometry::Design, report: &[Value], interfaces: &mut [Value]) {
|
||||
let info = |cell: &str| report.iter().find(|r| r.get("body").and_then(Value::as_str) == Some(cell));
|
||||
let text = |r: Option<&Value>, key: &str| r.and_then(|r| r.get(key)).and_then(Value::as_str).map(String::from);
|
||||
for interface in interfaces.iter_mut() {
|
||||
if interface.get("joined").and_then(Value::as_str) != Some("tied") {
|
||||
continue;
|
||||
}
|
||||
let cells: Vec<String> = interface
|
||||
.get("cells")
|
||||
.and_then(Value::as_array)
|
||||
.map(|c| c.iter().filter_map(Value::as_str).map(String::from).collect())
|
||||
.unwrap_or_default();
|
||||
let [a, b] = cells.as_slice() else { continue };
|
||||
let Some(source) = design.source_of(a).filter(|s| design.source_of(b) == Some(*s)) else { continue };
|
||||
let mut why: Vec<String> = Vec::new();
|
||||
for cell in [a, b] {
|
||||
let r = info(cell);
|
||||
if text(r, "kind").is_some_and(|k| k.starts_with("tet")) {
|
||||
why.push(match text(r, "why") {
|
||||
Some(w) => format!("`{cell}` is meshed with tet: {w}"),
|
||||
None => format!("`{cell}` is meshed with tet"),
|
||||
});
|
||||
}
|
||||
}
|
||||
if why.is_empty() {
|
||||
let not_together = [a, b].into_iter().find_map(|c| text(info(c), "not_swept_together"));
|
||||
let alone = [a, b].into_iter().find(|c| text(info(c), "swept_together").is_none() && text(info(c), "not_swept_together").is_none());
|
||||
if let Some(w) = not_together {
|
||||
why.push(format!("the cells of `{source}` do not sweep together ({w}), so each was meshed alone with its own divisions"));
|
||||
} else if let Some(c) = alone {
|
||||
let method = text(info(c), "method").unwrap_or_default();
|
||||
why.push(format!("`{c}` is meshed alone (method `{method}`), not swept with the other cells of `{source}`"));
|
||||
} else {
|
||||
why.push(format!("the element faces of `{a}` and `{b}` do not match node for node"));
|
||||
}
|
||||
}
|
||||
if let Some(o) = interface.as_object_mut() {
|
||||
o.insert("why".into(), json!(why.join("; ")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The name of the tie that holds `cell`'s tied cut faces.
|
||||
fn tie_name(cell: &str) -> String {
|
||||
let name = format!("tie_{cell}");
|
||||
@@ -613,11 +711,12 @@ mod tests {
|
||||
fn hex_and_tet_cells_are_tied_and_matching_hex_cells_merged() {
|
||||
let (_dir, mut s) = cut_plate();
|
||||
// A tet cell next to a hex cell: tied, the hex cell main.
|
||||
let r = s.execute("mesh.cells", &json!({"methods": {"Body1/a": "tet", "Body1/b": "mapped"}, "size": 2.5})).unwrap();
|
||||
let r = s.execute("mesh.cells", &json!({"methods": {"Body1/a": "tet", "Body1/b": "mapped"}, "size": 2.5, "transition": "tie"})).unwrap();
|
||||
assert_eq!(r["cells"][0]["kind"], "tet4", "{r}");
|
||||
assert_eq!(r["cells"][1]["kind"], "hex8", "{r}");
|
||||
let i = &r["interfaces"][0];
|
||||
assert_eq!(i["joined"], "tied", "{r}");
|
||||
assert!(i["why"].as_str().is_some_and(|w| w.contains("`Body1/a` is meshed with tet")), "{r}");
|
||||
assert_eq!(s.model.ties().len(), 1);
|
||||
assert_eq!((i["main"].as_str(), i["secondary"].as_str(), i["tie"].as_str()), (Some("Body1/b"), Some("Body1/a"), Some("tie_Body1_a")), "{r}");
|
||||
for set in ["tie_Body1_a_main", "tie_Body1_a_secondary"] {
|
||||
@@ -631,10 +730,37 @@ mod tests {
|
||||
let separate = r["cells"].as_array().unwrap().iter().map(|c| c["nodes"].as_u64().unwrap()).sum::<u64>();
|
||||
assert_eq!(r["nodes"].as_u64().unwrap() + i["nodes"].as_u64().unwrap(), separate, "{r}");
|
||||
// Rerunning replaces the tie it made rather than adding another.
|
||||
s.execute("mesh.cells", &json!({"methods": {"Body1/a": "tet"}, "size": 2.5})).unwrap();
|
||||
s.execute("mesh.cells", &json!({"methods": {"Body1/a": "tet"}, "size": 2.5, "transition": "tie"})).unwrap();
|
||||
assert_eq!(s.model.ties().len(), 1);
|
||||
}
|
||||
|
||||
/// A tie between cells of one design body says why they were not conforming (#396): the
|
||||
/// reason the body's cells did not sweep together, or the cell meshed alone by its method.
|
||||
#[test]
|
||||
fn ties_inside_one_body_say_why() {
|
||||
let (_dir, s) = cut_plate();
|
||||
let design = s.design.clone().unwrap();
|
||||
let tied = || vec![json!({"cells": ["Body1/a", "Body1/b"], "joined": "tied"}), json!({"cells": ["Body1/a", "Body1/b"], "joined": "merged"})];
|
||||
let mut interfaces = tied();
|
||||
let report = [
|
||||
json!({"body": "Body1/a", "kind": "hex8", "method": "extrude", "not_swept_together": "extrude: the levels differ"}),
|
||||
json!({"body": "Body1/b", "kind": "hex8", "method": "extrude", "not_swept_together": "extrude: the levels differ"}),
|
||||
];
|
||||
explain_ties(&design, &report, &mut interfaces);
|
||||
assert_eq!(
|
||||
interfaces[0]["why"],
|
||||
"the cells of `Body1` do not sweep together (extrude: the levels differ), so each was meshed alone with its own divisions"
|
||||
);
|
||||
assert!(interfaces[1].get("why").is_none());
|
||||
let mut interfaces = tied();
|
||||
let report = [
|
||||
json!({"body": "Body1/a", "kind": "hex8", "method": "mapped"}),
|
||||
json!({"body": "Body1/b", "kind": "hex8", "method": "extrude", "swept_together": "Body1"}),
|
||||
];
|
||||
explain_ties(&design, &report, &mut interfaces);
|
||||
assert_eq!(interfaces[0]["why"], "`Body1/a` is meshed alone (method `mapped`), not swept with the other cells of `Body1`");
|
||||
}
|
||||
|
||||
/// The half crank arm (cut on its symmetry plane only) has no cell that maps or sweeps as
|
||||
/// the design gives it; the extrusion decomposes its cross-section into blocks itself, alike
|
||||
/// through mesh.hex.generate (auto) and mesh.cells, with no seeds.
|
||||
@@ -687,11 +813,10 @@ mod tests {
|
||||
}
|
||||
|
||||
/// A 40 × 10 × 10 bar in uniaxial tension, cut in the middle: one half hex, the other tet,
|
||||
/// tied. With ν = 0, symmetry conditions on three faces and the end load spread by consistent
|
||||
/// weights, the stress is uniform, which both elements and the dual mortar tie carry exactly:
|
||||
/// the end moves F L / (E A).
|
||||
#[test]
|
||||
fn a_tied_hex_tet_bar_in_tension_stretches_as_the_formula_says() {
|
||||
/// joined by `transition` (`joined` in the reply). With ν = 0, symmetry conditions on three
|
||||
/// faces and the end load spread by consistent weights, the stress is uniform, which the
|
||||
/// elements, the pyramids and the dual mortar tie all carry exactly: the end moves F L / (E A).
|
||||
fn hex_tet_bar_in_tension(transition: &str, joined: &str) -> Session {
|
||||
let commands = json!({"commands": [
|
||||
{"command": "solid.box", "params": {"length": 40, "width": 10, "height": 10, "body_name": "Bar"}},
|
||||
]});
|
||||
@@ -711,8 +836,8 @@ mod tests {
|
||||
point[axis] = mid;
|
||||
s.execute("geometry.datum", &json!({"name": "Mid", "by": {"type": "point_normal", "point": point, "normal": normal}})).unwrap();
|
||||
s.execute("geometry.partition", &json!({"name": "Cut", "datum": "Mid"})).unwrap();
|
||||
let r = s.execute("mesh.cells", &json!({"methods": {"Bar/a": "mapped", "Bar/b": "tet"}, "size": 2.5})).unwrap();
|
||||
assert_eq!(r["interfaces"][0]["joined"], "tied", "{r}");
|
||||
let r = s.execute("mesh.cells", &json!({"methods": {"Bar/a": "mapped", "Bar/b": "tet"}, "size": 2.5, "transition": transition})).unwrap();
|
||||
assert_eq!(r["interfaces"][0]["joined"], joined, "{r}");
|
||||
|
||||
let m = &s.model;
|
||||
let on = |k: usize, v: f64| -> Vec<u32> { m.nodes().iter().filter(|(_, n)| (n.xyz[k] - v).abs() < 1e-9).map(|(l, _)| *l).collect() };
|
||||
@@ -754,6 +879,27 @@ mod tests {
|
||||
let u = n["displacement"][axis].as_f64().unwrap();
|
||||
assert!((u - expected).abs() < 1e-6 * expected, "{u} vs {expected}: {n}");
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tied_hex_tet_bar_in_tension_stretches_as_the_formula_says() {
|
||||
let s = hex_tet_bar_in_tension("tie", "tied");
|
||||
assert_eq!(s.model.ties().len(), 1);
|
||||
}
|
||||
|
||||
/// Through pyramids the bar is one conforming mesh: no tie, no free faces inside, every
|
||||
/// element valid.
|
||||
#[test]
|
||||
fn a_hex_tet_bar_joined_by_pyramids_is_conforming_and_stretches_as_the_formula_says() {
|
||||
let mut s = hex_tet_bar_in_tension("pyramid", "pyramids");
|
||||
assert!(s.model.ties().is_empty());
|
||||
let kinds: BTreeSet<&str> = s.model.elements().values().map(|e| e.kind.name()).collect();
|
||||
assert_eq!(kinds.into_iter().collect::<Vec<_>>(), ["hex8", "pyramid5", "tet4"]);
|
||||
let d = s.execute("mesh.check.duplicates", &json!({})).unwrap();
|
||||
assert_eq!(d["counts"], json!({"coincident": 0, "cracks": 0, "duplicate_elements": 0, "free_edges": 0}), "{d}");
|
||||
let q = s.execute("mesh.quality", &json!({})).unwrap();
|
||||
assert_eq!(q["invalid"]["count"], 0, "{q}");
|
||||
}
|
||||
|
||||
/// A box cut by planes into cells, `methods` per cell, a heat source in every cell and
|
||||
@@ -811,6 +957,7 @@ mod tests {
|
||||
let tied: Vec<&Value> = interfaces.iter().filter(|i| i["joined"] == "tied").collect();
|
||||
assert_eq!(tied.len(), 3, "{r}");
|
||||
assert!(tied.iter().all(|i| i["secondary"] == "Box/b/a/a" && i["tie"] == "tie_Box_b_a_a"), "{r}");
|
||||
assert!(tied.iter().all(|i| i["why"].as_str().is_some_and(|w| w.starts_with("`Box/b/a/a` is meshed with tet"))), "{r}");
|
||||
let merged: Vec<&Value> = interfaces.iter().filter(|i| i["joined"] == "merged").collect();
|
||||
assert_eq!(merged.len(), 4, "{r}");
|
||||
// Box/a/a and Box/a/b meet across y = 10, the third cut.
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
//! Pyramid transitions between a tet cell and the hex cells across its partitions (#439).
|
||||
//!
|
||||
//! The tet cell is meshed after the hex cells: its cut faces that face hex cells are replaced by
|
||||
//! a pyramid on every hex element face there ([`fieldcraft_mesh::transition`]), the tetrahedra
|
||||
//! fill the rest and keep the pyramids' sides, and the pyramids join the hex nodes to the tet
|
||||
//! nodes. The mesh is conforming across the cut, so no tie is needed there.
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use fieldcraft_mesh::cdt::{CdtOptions, tetrahedralize};
|
||||
use fieldcraft_mesh::surface::BoundarySurface;
|
||||
use fieldcraft_mesh::tet::{QualityMesh, QualityOptions, quality_mesh};
|
||||
use fieldcraft_mesh::transition::{Transition, apex_points, pyramid_surface};
|
||||
use fieldcraft_model::{ElementKind, GeometryRef, Model};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::super::hex::Input;
|
||||
use super::super::mesh::{BodyTets, add_tets, analytic_surfaces};
|
||||
use crate::{EngineError, Result};
|
||||
|
||||
/// A tet cell meshed against the pyramids on its hex neighbours' faces.
|
||||
pub(super) struct Pyramids {
|
||||
pub(super) tets: BodyTets,
|
||||
transition: Transition,
|
||||
/// Each pyramid's base: the hex nodes of its face, oriented into the tet cell.
|
||||
bases: Vec<[u32; 4]>,
|
||||
/// The interfaces it makes (reply entries).
|
||||
pub(super) interfaces: Vec<Value>,
|
||||
}
|
||||
|
||||
/// The base name of a cut face (`partition:P:a` of `partition:P:a#1`): its partition and side.
|
||||
fn cut_of(name: &str) -> Option<(&str, usize)> {
|
||||
let base = name.split_once('#').map_or(name, |(b, _)| b);
|
||||
let (partition, side) = base.strip_prefix("partition:")?.rsplit_once(':')?;
|
||||
Some((partition, usize::from(side == "b")))
|
||||
}
|
||||
|
||||
/// Mesh tet cell `input` with pyramids on the faces of the hex cells `hex` (already in `model`)
|
||||
/// across its cut faces. `Ok(Err(why))` when there is no such face or the transition cannot be
|
||||
/// built (the caller meshes the cell plainly and ties it); limits and a cancel are errors.
|
||||
pub(super) fn mesh_with_pyramids(
|
||||
design: &fieldcraft_geometry::Design,
|
||||
model: &Model,
|
||||
p: &Value,
|
||||
input: &Input,
|
||||
hex: &[String],
|
||||
cdt: &CdtOptions,
|
||||
options: &QualityOptions,
|
||||
) -> Result<std::result::Result<Pyramids, String>> {
|
||||
// The hex element faces across each of the cell's cut faces.
|
||||
let sides = super::sides(model, hex);
|
||||
let mut cut: Vec<u32> = Vec::new();
|
||||
let mut quads: Vec<[u32; 4]> = Vec::new();
|
||||
let mut interfaces = Vec::new();
|
||||
let mut seen: BTreeSet<(String, String)> = BTreeSet::new();
|
||||
for (i, name) in input.face_names.iter().enumerate() {
|
||||
let Some((partition, side)) = cut_of(name) else { continue };
|
||||
let Some(across) = sides.get(partition).and_then(|s| s.get(1 - side)) else { continue };
|
||||
if across.is_empty() {
|
||||
continue;
|
||||
}
|
||||
cut.push(i as u32);
|
||||
for other in across {
|
||||
if !seen.insert((partition.to_string(), other.body.clone())) {
|
||||
continue;
|
||||
}
|
||||
for f in &other.faces {
|
||||
let nodes = model.face_nodes(*f).unwrap_or_default();
|
||||
let Ok(q) = <[u32; 4]>::try_from(nodes.as_slice()) else {
|
||||
return Ok(Err(format!("`{}` has quadratic or non-quadrilateral faces on the cut; pyramid transitions are linear", other.body)));
|
||||
};
|
||||
quads.push(q);
|
||||
}
|
||||
interfaces.push((partition.to_string(), other.body.clone(), other.faces.len()));
|
||||
}
|
||||
}
|
||||
if quads.is_empty() {
|
||||
return Ok(Err("no hex cell faces it across a partition".into()));
|
||||
}
|
||||
// The hex faces must cover the cut faces exactly (a cut shared with a tet cell, or a hex
|
||||
// cell larger than this one, is tied instead).
|
||||
let xyz = |n: u32| model.node(n).map(|x| x.xyz).unwrap_or([f64::NAN; 3]);
|
||||
let positions: Vec<[[f64; 3]; 4]> = quads.iter().map(|q| q.map(xyz)).collect();
|
||||
let quad_area: f64 = positions.iter().map(|q| 0.5 * norm(cross(sub(q[2], q[0]), sub(q[3], q[1])))).sum();
|
||||
let m = &input.mesh;
|
||||
let cut_area: f64 = m
|
||||
.triangles
|
||||
.iter()
|
||||
.zip(&m.tri_face)
|
||||
.filter(|(_, f)| cut.contains(f))
|
||||
.map(|(t, _)| {
|
||||
let c = t.map(|v| m.positions.get(v as usize).copied().unwrap_or([f64::NAN; 3]));
|
||||
0.5 * norm(cross(sub(c[1], c[0]), sub(c[2], c[0])))
|
||||
})
|
||||
.sum();
|
||||
if !((quad_area - cut_area).abs() <= 1e-3 * cut_area) {
|
||||
return Ok(Err(format!("the hex cells' faces ({quad_area:.6} mm²) do not cover its cut faces ({cut_area:.6} mm²)")));
|
||||
}
|
||||
let tag = u32::try_from(input.face_names.len()).map_err(|_| EngineError::Params("too many faces".into()))?;
|
||||
let welding = super::super::mesh::surface_options(p, m)?;
|
||||
let built = || -> fieldcraft_mesh::Result<(QualityMesh, Transition, usize, usize, f64)> {
|
||||
let surface = BoundarySurface::new(&m.positions, &m.triangles, &m.tri_face, &welding)?;
|
||||
let t = pyramid_surface(&surface, &cut, &positions, tag)?;
|
||||
// Already welded: nothing more merges.
|
||||
let exact = fieldcraft_mesh::surface::SurfaceOptions { weld_tolerance: 0.0, weld_distance: 0.0, ..welding };
|
||||
let region = BoundarySurface::new(&t.positions, &t.triangles, &t.tri_face, &exact)?;
|
||||
let tets = tetrahedralize(®ion, cdt)?;
|
||||
let mut analytic = design.body(&input.name).map(analytic_surfaces).unwrap_or_default();
|
||||
analytic.resize(tag as usize, None);
|
||||
analytic.push(None);
|
||||
let o = QualityOptions { seeds: input.seeds.clone(), frozen_faces: vec![tag], ..options.clone() };
|
||||
let mesh = quality_mesh(®ion, &tets, analytic, &o)?;
|
||||
Ok((mesh, t, tets.steiner.len(), tets.stats.cavities, region.volume()))
|
||||
};
|
||||
let (mesh, transition, steiner, cavities, volume) = match built() {
|
||||
Ok(x) => x,
|
||||
Err(fieldcraft_mesh::Error::Model(fieldcraft_model::Error::Cancelled)) => return Err(EngineError::Model(fieldcraft_model::Error::Cancelled)),
|
||||
Err(e @ fieldcraft_mesh::Error::Limit(_)) => return Err(EngineError::Params(e.to_string())),
|
||||
Err(e) => return Ok(Err(e.to_string())),
|
||||
};
|
||||
if let Err(e) = apex_points(&mesh, &transition) {
|
||||
return Ok(Err(e.to_string()));
|
||||
}
|
||||
let tets = BodyTets::new(input.name.clone(), input.face_names.clone(), mesh, steiner, cavities, volume);
|
||||
let interfaces = interfaces
|
||||
.into_iter()
|
||||
.map(|(partition, other, faces)| json!({"partition": partition, "cells": [other, input.name], "joined": "pyramids", "pyramids": faces}))
|
||||
.collect();
|
||||
Ok(Ok(Pyramids { tets, transition, bases: quads, interfaces }))
|
||||
}
|
||||
|
||||
/// Add the cell's tetrahedra and pyramids to `model`: the tetrahedra's nodes on the hex faces
|
||||
/// are the hex nodes, the pyramids join them to the apexes.
|
||||
pub(super) fn add(model: &mut Model, mut c: Pyramids) -> Result<(usize, usize)> {
|
||||
let tag = c.tets.face_names.len() as u32;
|
||||
let apexes = apex_points(&c.tets.mesh, &c.transition).map_err(|e| EngineError::Params(e.to_string()))?;
|
||||
// The pyramid sides are inside the part: no geometry face.
|
||||
c.tets.mesh.boundary.retain(|b| b.face != tag);
|
||||
let (first_node, _) = add_tets(model, &c.tets.name, &c.tets.face_names, &c.tets.mesh)?;
|
||||
// The tet mesh keeps the hex nodes' coordinates exactly: merge its copies into them.
|
||||
let hex: BTreeMap<[u64; 3], u32> = c.bases.iter().flatten().filter_map(|&n| model.node(n).map(|x| (x.xyz.map(f64::to_bits), n))).collect();
|
||||
let mut merge = BTreeMap::new();
|
||||
for (i, x) in c.tets.mesh.points.iter().enumerate() {
|
||||
if let Some(&h) = hex.get(&x.map(f64::to_bits)) {
|
||||
merge.insert(first_node + i as u32, h);
|
||||
}
|
||||
}
|
||||
let shared = merge.len();
|
||||
model.merge_nodes(&merge)?;
|
||||
let (_, first) = crate::cmd::fresh_labels(model, 0, c.bases.len())?;
|
||||
let body = model.geometry_tag(&GeometryRef::body(c.tets.name.as_str())?)?;
|
||||
for (k, (base, apex)) in c.bases.iter().zip(apexes).enumerate() {
|
||||
let label = first + k as u32;
|
||||
let mut nodes = base.to_vec();
|
||||
nodes.push(first_node + apex);
|
||||
model.set_element(label, ElementKind::Pyramid5, nodes)?;
|
||||
model.tag_element(label, body)?;
|
||||
}
|
||||
Ok((c.tets.mesh.elements.len() + c.bases.len(), c.tets.mesh.points.len() - shared))
|
||||
}
|
||||
|
||||
fn sub(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
|
||||
[a[0] - b[0], a[1] - b[1], a[2] - b[2]]
|
||||
}
|
||||
|
||||
fn cross(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
|
||||
[a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]
|
||||
}
|
||||
|
||||
fn norm(a: [f64; 3]) -> f64 {
|
||||
(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]).sqrt()
|
||||
}
|
||||
@@ -12,7 +12,7 @@
|
||||
use crate::schema::ProblemKind;
|
||||
use std::sync::Arc;
|
||||
|
||||
use fieldcraft_geometry::{Cut, Design};
|
||||
use fieldcraft_geometry::{Cut, Design, Merge};
|
||||
use fieldcraft_io::{Item, ItemKind, MAX_HIDDEN};
|
||||
use fieldcraft_model::{Datum, Model, Partition};
|
||||
use serde::Deserialize;
|
||||
@@ -94,7 +94,7 @@ pub const DATUM_DELETE: CommandSpec = CommandSpec {
|
||||
pub const PARTITION: CommandSpec = CommandSpec {
|
||||
id: "geometry.partition",
|
||||
label: "Partition",
|
||||
params: r#"{"name": "Half", "datum": "Mid", "bodies"?: ["Body1"] (default: every body the plane divides)} — cuts bodies of the open design into cells by a datum plane, in memory (the design file is not changed); the model keeps the partition and applies it again whenever the design is opened, after the partitions before it. A cut body `B` becomes the cells `B/a` (on the plane's negative side) and `B/b` (several pieces on a side: `B/a1`, `B/a2`, …). The two faces of the cut are paired by name: `partition:<name>:a` on the cells on the negative side, `partition:<name>:b` on the positive side (`…#0`, `…#1` where a cell has several); every other face keeps the persistent name of the face it is a piece of (`<face>#k` for several pieces on one cell), so sets by face name still select it. Reports the cells made and the cut faces named, and `warnings` when the plane lies on a face of a body it cuts or a cell's boundary does not tessellate into one a mesher takes (a cut through existing edges or vertices, such as a hole's axis), naming the plane, the cell and where; a body it cuts loses its mesh (`replaced`, as a mesher reports it; remesh the cells); a plane that divides no body, a listed body it does not divide, or a body the kernel fails to cut (its message) is an error"#,
|
||||
params: r#"{"name": "Half", "datum": "Mid", "bodies"?: ["Body1"] (default: every body the plane divides; a body earlier partitions cut stands for its cells)} — cuts bodies of the open design into cells by a datum plane, in memory (the design file is not changed); the model keeps the partition and applies it again whenever the design is opened, after the partitions before it. A cut body `B` becomes the cells `B/a` (on the plane's negative side) and `B/b` (several pieces on a side: `B/a1`, `B/a2`, …). The two faces of the cut are paired by name: `partition:<name>:a` on the cells on the negative side, `partition:<name>:b` on the positive side (`…#0`, `…#1` where a cell has several); every other face keeps the persistent name of the face it is a piece of (`<face>#k` for several pieces on one cell), so sets by face name still select it. Reports the cells made and the cut faces named, and `warnings` when the plane lies on a face of a body it cuts or a cell's boundary does not tessellate into one a mesher takes (a cut through existing edges or vertices, such as a hole's axis), naming the plane, the cell and where; a body it cuts loses its mesh (`replaced`, as a mesher reports it; remesh the cells); a plane that divides no body, a listed body it does not divide, or a body the kernel fails to cut (its message) is an error"#,
|
||||
form: Some(super::forms::PARTITION),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Design, Requirement::DatumPlane],
|
||||
@@ -470,15 +470,35 @@ fn cuts(model: &Model) -> Vec<Cut> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `design` cut by `model`'s partitions (the design as opened when the model has none).
|
||||
/// `design` without the features `model` defeatures, cut by its partitions, then with its face
|
||||
/// merges (after those of the features defeatured virtually) and its face splits (those along a
|
||||
/// suppressed datum left out; the design as opened when the model has none).
|
||||
pub(crate) fn partitioned(design: &Design, model: &Model) -> Result<Design> {
|
||||
Ok(design.partitioned(&cuts(model))?)
|
||||
let defeatures: Vec<fieldcraft_geometry::defeature::Defeature> =
|
||||
model.defeatures().iter().map(|d| fieldcraft_geometry::defeature::Defeature { name: d.name.clone(), features: d.features.clone() }).collect();
|
||||
let (design, mut merges) = design.defeatured(&defeatures)?;
|
||||
merges.extend(model.face_merges().iter().map(|m| Merge {
|
||||
name: m.name.clone(),
|
||||
body: m.body.clone(),
|
||||
faces: m.faces.clone(),
|
||||
into: m.into.clone(),
|
||||
}));
|
||||
let splits: Vec<fieldcraft_geometry::face_split::FaceSplit> = model
|
||||
.face_splits()
|
||||
.iter()
|
||||
.filter(|s| !model.datum_suppressed(&s.datum))
|
||||
.filter_map(|s| {
|
||||
let (point, normal) = model.datum(&s.datum)?.plane()?;
|
||||
Some(fieldcraft_geometry::face_split::FaceSplit { name: s.name.clone(), body: s.body.clone(), face: s.face.clone(), point, normal })
|
||||
})
|
||||
.collect();
|
||||
Ok(design.partitioned(&cuts(model))?.merged(&merges)?.split(&splits)?)
|
||||
}
|
||||
|
||||
/// Cut the session's design by its model's partitions again. The bodies that are gone (cells
|
||||
/// Cut the session's design by its model's partitions again (and merge its faces again). The bodies that are gone (cells
|
||||
/// whose partition went, a body a new partition cut) take their meshes with them (#385): what
|
||||
/// went is in the returned [`super::replace::Cleared`], which the command reports.
|
||||
fn recut(s: &mut Session) -> Result<super::replace::Cleared> {
|
||||
pub(super) fn recut(s: &mut Session) -> Result<super::replace::Cleared> {
|
||||
let Some(d) = &s.design else { return Ok(super::replace::Cleared::default()) };
|
||||
let cut = partitioned(d, &s.model)?;
|
||||
let gone: Vec<String> = d.bodies.iter().filter(|b| cut.body(&b.name).is_none()).map(|b| b.name.clone()).collect();
|
||||
@@ -508,7 +528,7 @@ fn partition(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (faces, bodies): (usize, Vec<String>) = (report.map_or(0, |r| r.faces), design.bodies.iter().map(|b| b.name.clone()).collect());
|
||||
let mut warnings: Vec<String> = report.map(|r| r.warnings.clone()).unwrap_or_default();
|
||||
for cell in &cells {
|
||||
if let Some(why) = design.body(cell).and_then(|shape| unmeshable(shape).err()) {
|
||||
if let Some(why) = design.body(cell).and_then(|shape| unmeshable(shape, None).err()) {
|
||||
warnings.push(format!(
|
||||
"partition `{name}`: cell `{cell}` has a boundary no mesher takes ({why}): the plane passes within the kernel's tolerance of a face, edge or vertex it cut (along a face, through the axis of a hole); move the plane off it (by a fraction of the element size)"
|
||||
));
|
||||
@@ -534,10 +554,10 @@ fn partition(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(cleared.report_if_any(s, reply))
|
||||
}
|
||||
|
||||
/// Whether a cell's boundary, tessellated as the meshers take it by default, bounds a volume mesh:
|
||||
/// Whether a cell's boundary, tessellated as the meshers take it (by default, or at `tolerance`), bounds a volume mesh:
|
||||
/// the defects (open edges, edges on three triangles) when it does not.
|
||||
fn unmeshable(shape: &fieldcraft_geometry::Shape) -> Result<()> {
|
||||
let input = super::hex::shape_input(shape, None)?;
|
||||
pub(super) fn unmeshable(shape: &fieldcraft_geometry::Shape, tolerance: Option<f64>) -> Result<()> {
|
||||
let input = super::hex::shape_input(shape, tolerance)?;
|
||||
let options = fieldcraft_mesh::surface::SurfaceOptions { weld_distance: input.mesh.weld_distance, ..Default::default() };
|
||||
fieldcraft_mesh::surface::BoundarySurface::new(&input.mesh.positions, &input.mesh.triangles, &input.mesh.tri_face, &options)
|
||||
.map(|_| ())
|
||||
@@ -945,7 +965,8 @@ pub(crate) mod tests {
|
||||
|
||||
/// #384: cuts stay on their plane (the kernel's own split put them 1.8 µm off it on the air
|
||||
/// of the enclosure, so the cut faces were not even recognized), and a plane along an
|
||||
/// existing face or through a hole's axis is a warning naming the plane, the body and where.
|
||||
/// existing face or through a hole's axis is a warning naming the plane, the body and where,
|
||||
/// or refused where the kernel could only cut it off the plane.
|
||||
#[test]
|
||||
fn cuts_stay_on_the_plane_and_risky_ones_are_named() {
|
||||
let (_dir, mut s) = pi5();
|
||||
@@ -972,11 +993,11 @@ pub(crate) mod tests {
|
||||
assert!(!cut.is_empty() && cut.iter().all(|x| (x - 16.5).abs() < 1e-9), "{cell}: {cut:?}");
|
||||
}
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
// Along the side of the heat sink's pedestal: named.
|
||||
let r = plane(&mut s, "Side", 34.0);
|
||||
let warnings = r["warnings"].to_string();
|
||||
assert!(warnings.contains("partition `PSide`: the plane lies on face `F45:-x` of `Air`"), "{r}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
// Along the side of the heat sink's pedestal: the kernel only cuts with its box nudged
|
||||
// off the plane, so the cut is refused, saying why (#427).
|
||||
s.execute("geometry.datum", &json!({"name": "Side", "by": {"type": "offset", "plane": "yz", "offset": 34.0}})).unwrap();
|
||||
let e = s.execute("geometry.partition", &json!({"name": "PSide", "datum": "Side", "bodies": ["Air"]})).unwrap_err().to_string();
|
||||
assert!(e.contains("could not cut `Air`") && e.contains("only made off the plane"), "{e}");
|
||||
// Through a standoff's hole: the cell whose boundary breaks at the hole's edge is named.
|
||||
let r = plane(&mut s, "Axis", 74.5);
|
||||
let warnings = r["warnings"].to_string();
|
||||
|
||||
@@ -0,0 +1,352 @@
|
||||
//! Defeaturing for meshing: `geometry.defeature` takes holes, fillets, chamfers and small bosses
|
||||
//! out of the open design by suppressing their SolidCraft features (virtually, as a face merge,
|
||||
//! when a feature cannot be suppressed on its own), and `geometry.defeature.propose` lists the
|
||||
//! candidates of a body against size thresholds and the last geometry.check.
|
||||
//!
|
||||
//! A defeature is part of the model (`fieldcraft_model::Defeature`, by feature id); the geometry
|
||||
//! layer applies it to the design as opened (`fieldcraft_geometry::defeature`), before the
|
||||
//! partitions, whenever the design is opened or cut again ([`super::datum::partitioned`]), so the
|
||||
//! meshers, the viewport and the checks all see the part without them.
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use fieldcraft_geometry::defeature::{Candidate, candidate};
|
||||
use fieldcraft_model::Defeature;
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::requirement::Requirement;
|
||||
use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
|
||||
pub const DEFEATURE: CommandSpec = CommandSpec {
|
||||
id: "geometry.defeature",
|
||||
label: "Defeature",
|
||||
params: r#"{"name": "Small", "features": [3, "F7"] (feature ids, as geometry.features and geometry.defeature.propose list them; or the text "3, F7")} — takes features of the open design out for meshing: the design is rebuilt without them, as SolidCraft builds it with them suppressed (a hole filled, a fillet or chamfer back to its sharp edge, a boss gone), and the other faces keep their persistent names. A feature that cannot be suppressed alone (a later feature needs it, or a body would go) is defeatured virtually instead: its faces are merged into the neighbour they share the longest edges with, for meshing only (virtual topology; the B-rep keeps it). A feature that builds a body or a sketch, or one the design lacks, is an error. Kept in the model by feature id and applied again whenever the design is opened (before the partitions); the same name again replaces its features. A suppressed feature affects every body it shaped. The bodies lose their meshes (`replaced`; remesh them); sets naming their faces are `stale_sets`. Replies `suppressed` (feature ids), `virtual` ([{feature, reason}]) and `bodies`. One undo step; geometry.defeature.delete turns them back on"#,
|
||||
form: Some(super::forms::DEFEATURE),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Design],
|
||||
run: defeature,
|
||||
};
|
||||
|
||||
pub const DEFEATURE_DELETE: CommandSpec = CommandSpec {
|
||||
id: "geometry.defeature.delete",
|
||||
label: "Delete Defeature",
|
||||
params: r#"{"name": "Small"} — turns a defeature's features back on: the design is rebuilt with them; the bodies they shaped lose their meshes (`replaced`)"#,
|
||||
form: Some(super::forms::DEFEATURE_DELETE),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Defeature],
|
||||
run: delete,
|
||||
};
|
||||
|
||||
pub const DEFEATURE_PROPOSE: CommandSpec = CommandSpec {
|
||||
id: "geometry.defeature.propose",
|
||||
label: "Propose Defeaturing",
|
||||
params: r#"{"body": "Base", "below"?: {"hole_diameter"?: 6, "fillet_radius"?: 2, "chamfer"?: 1, "boss_height"?: 2} (mm), "from_check"?: false, "name"?: "Defeature"} — lists the features of a body (as opened) that defeaturing may take out (read-only): holes (a hole, or a cut of a circle or cylinder) by diameter, fillets by radius, chamfers by distance, bosses (a join of an extrude, cylinder or box) by height. A candidate is `proposed` when it is smaller than its `below` threshold, or with `from_check` when one of its faces is a finding of the last geometry.check (still marked). Replies `candidates` ([{id, feature, name, type, category, size, faces, proposed, suppressed, by, method}]: `suppressed` when a defeature takes it out, `by` that defeature, `method` suppressed or virtual), `proposal` (the proposed ids) and `name`, the defeature the panel's on/off switches edit (geometry.defeature with that name)"#,
|
||||
form: Some(super::forms::DEFEATURE_PROPOSE),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Design],
|
||||
run: propose,
|
||||
};
|
||||
|
||||
/// A feature id: `3` or `"F3"`.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum FeatureId {
|
||||
Id(u64),
|
||||
Name(String),
|
||||
}
|
||||
|
||||
/// The feature ids of `features`: a list, or the form's text (`3, F7`).
|
||||
fn ids(p: &Value) -> Result<Vec<u64>> {
|
||||
let given: Vec<FeatureId> = match p.get("features") {
|
||||
Some(Value::String(t)) if t.len() <= 64 * 1024 => {
|
||||
t.split([',', ' ', ';']).filter(|w| !w.is_empty()).map(|w| FeatureId::Name(w.to_string())).collect()
|
||||
}
|
||||
_ => super::req(p, "features")?,
|
||||
};
|
||||
if given.is_empty() || given.len() > fieldcraft_model::virtual_faces::MAX_DEFEATURE_FEATURES {
|
||||
return Err(EngineError::invalid("features", format!("1 to {} feature ids", fieldcraft_model::virtual_faces::MAX_DEFEATURE_FEATURES)));
|
||||
}
|
||||
let mut out = Vec::with_capacity(given.len());
|
||||
for f in given {
|
||||
let id = match f {
|
||||
FeatureId::Id(id) => Some(id),
|
||||
FeatureId::Name(n) => n.strip_prefix('F').unwrap_or(&n).parse().ok(),
|
||||
};
|
||||
match id {
|
||||
Some(id) if !out.contains(&id) => out.push(id),
|
||||
Some(_) => {}
|
||||
None => return Err(EngineError::invalid("features", "feature ids: 3 or \"F3\"")),
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The bodies of the open design (as opened) that features `ids` shaped.
|
||||
fn shaped(s: &Session, ids: &BTreeSet<u64>) -> Vec<String> {
|
||||
let Some(design) = s.design.as_deref() else { return Vec::new() };
|
||||
let graph = design.undefeatured().features(None).unwrap_or_default();
|
||||
let sources: Vec<String> = graph.into_iter().filter(|b| b.features.iter().any(|f| ids.contains(&f.id))).map(|b| b.body).collect();
|
||||
// A body the partitions cut: each of its cells.
|
||||
design.bodies.iter().map(|b| b.name.clone()).filter(|n| design.source_of(n).is_some_and(|src| sources.iter().any(|x| x == src))).collect()
|
||||
}
|
||||
|
||||
/// What the design's defeatures did with the features of `name`, as the reply lists them.
|
||||
fn outcome(s: &Session, name: &str) -> (Vec<u64>, Vec<Value>) {
|
||||
let report = s.design.as_deref().map(|d| d.defeat_report().to_vec()).unwrap_or_default();
|
||||
let mine = report.into_iter().filter(|r| r.defeature == name);
|
||||
let (mut suppressed, mut virt) = (Vec::new(), Vec::new());
|
||||
for r in mine {
|
||||
if r.method == "suppressed" {
|
||||
suppressed.push(r.feature);
|
||||
} else {
|
||||
virt.push(json!({"feature": r.feature, "reason": r.reason}));
|
||||
}
|
||||
}
|
||||
(suppressed, virt)
|
||||
}
|
||||
|
||||
fn defeature(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = super::name(p)?.to_string();
|
||||
let features = ids(p)?;
|
||||
let before: BTreeSet<u64> = s.model.defeatures().iter().filter(|d| d.name == name).flat_map(|d| d.features.clone()).collect();
|
||||
s.model.set_defeature(Defeature { name: name.clone(), features: features.clone() })?;
|
||||
let mut touched = before;
|
||||
touched.extend(&features);
|
||||
let bodies = shaped(s, &touched);
|
||||
let merges: Vec<String> = features.iter().map(|id| format!("merge {name}-F{id}")).collect();
|
||||
let mut errors = vec![format!("defeature {name}")];
|
||||
errors.extend(merges);
|
||||
let reply = super::virtual_faces::reapply(s, &errors, &bodies, json!({"name": name}))?;
|
||||
let (suppressed, virt) = outcome(s, &name);
|
||||
let bodies = shaped(s, &touched);
|
||||
let mut reply = reply;
|
||||
if let Some(o) = reply.as_object_mut() {
|
||||
o.insert("suppressed".into(), json!(suppressed));
|
||||
o.insert("virtual".into(), json!(virt));
|
||||
o.insert("bodies".into(), json!(bodies));
|
||||
}
|
||||
Ok(reply)
|
||||
}
|
||||
|
||||
fn delete(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = super::name(p)?;
|
||||
let removed = s.model.remove_defeature(name)?;
|
||||
let bodies = shaped(s, &removed.features.iter().copied().collect());
|
||||
let mut cleared = super::datum::recut(s)?;
|
||||
cleared.extend(super::replace::clear(&mut s.model, bodies.iter().map(String::as_str)));
|
||||
Ok(cleared.report_if_any(s, json!({"deleted": name, "features": removed.features, "bodies": bodies})))
|
||||
}
|
||||
|
||||
/// Size thresholds of a proposal (mm).
|
||||
#[derive(Deserialize, Default)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Below {
|
||||
hole_diameter: Option<f64>,
|
||||
fillet_radius: Option<f64>,
|
||||
chamfer: Option<f64>,
|
||||
boss_height: Option<f64>,
|
||||
}
|
||||
|
||||
impl Below {
|
||||
fn of(&self, category: &str) -> Option<f64> {
|
||||
match category {
|
||||
"hole" => self.hole_diameter,
|
||||
"fillet" => self.fillet_radius,
|
||||
"chamfer" => self.chamfer,
|
||||
"boss" => self.boss_height,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn propose(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let body: String = super::req(p, "body")?;
|
||||
let below: Below = super::opt(p, "below")?.unwrap_or_default();
|
||||
for (k, v) in [
|
||||
("hole_diameter", below.hole_diameter),
|
||||
("fillet_radius", below.fillet_radius),
|
||||
("chamfer", below.chamfer),
|
||||
("boss_height", below.boss_height),
|
||||
] {
|
||||
if v.is_some_and(|v| !(0.0..=fieldcraft_model::MAX_COORD).contains(&v)) {
|
||||
return Err(EngineError::invalid("below", format!("`{k}` is a length of 0 or more")));
|
||||
}
|
||||
}
|
||||
let from_check: bool = super::opt(p, "from_check")?.unwrap_or(false);
|
||||
let name = match p.get("name") {
|
||||
None | Some(Value::Null) => "Defeature".to_string(),
|
||||
Some(_) => super::name(p)?.to_string(),
|
||||
};
|
||||
let design = s.design.as_deref().ok_or_else(|| EngineError::Params("open a design first".into()))?;
|
||||
let source = design
|
||||
.source_of(&body)
|
||||
.ok_or_else(|| EngineError::Params(format!("the open design has no body `{}`", body.chars().take(200).collect::<String>())))?;
|
||||
let graph = design.undefeatured().features(Some(source))?;
|
||||
// The faces the last check marked on the body (merged faces by their B-rep faces).
|
||||
let mut flagged: BTreeSet<String> = BTreeSet::new();
|
||||
if from_check {
|
||||
let marks = crate::view::geometry_marks(s)
|
||||
.ok_or_else(|| EngineError::Params("`from_check`: run geometry.check (with marks) on the open design first".into()))?;
|
||||
for (b, i) in marks.marks.iter().flat_map(|m| m.faces.iter()) {
|
||||
if design.source_of(b) != Some(source) {
|
||||
continue;
|
||||
}
|
||||
if let Some(f) = design.body(b).and_then(|shape| shape.faces().get(*i as usize)) {
|
||||
flagged.insert(f.name.split('#').next().unwrap_or(&f.name).to_string());
|
||||
flagged.extend(f.members.iter().cloned());
|
||||
}
|
||||
}
|
||||
}
|
||||
// Which defeature takes each feature out, and how.
|
||||
let by: BTreeMap<u64, (String, &'static str)> = design.defeat_report().iter().map(|r| (r.feature, (r.defeature.clone(), r.method))).collect();
|
||||
let mut candidates = Vec::new();
|
||||
let mut proposal = Vec::new();
|
||||
for c in graph.iter().flat_map(|b| b.features.iter()).filter_map(candidate) {
|
||||
let Candidate { id, name: fname, kind, category, size, faces } = c;
|
||||
let small = below.of(category).is_some_and(|t| size < t);
|
||||
let hit = faces.iter().any(|f| flagged.contains(f));
|
||||
let proposed = small || hit;
|
||||
if proposed {
|
||||
proposal.push(id);
|
||||
}
|
||||
let taken = by.get(&id);
|
||||
candidates.push(json!({
|
||||
"id": id,
|
||||
"feature": format!("F{id}"),
|
||||
"name": fname,
|
||||
"type": kind,
|
||||
"category": category,
|
||||
"size": size,
|
||||
"faces": faces,
|
||||
"proposed": proposed,
|
||||
"suppressed": taken.is_some(),
|
||||
"by": taken.map(|t| t.0.clone()),
|
||||
"method": taken.map(|t| t.1),
|
||||
}));
|
||||
}
|
||||
let off = candidates.iter().filter(|c| c["suppressed"] == true).count();
|
||||
let summary = format!(
|
||||
"{} on `{source}`: {} proposed, {} defeatured",
|
||||
match candidates.len() {
|
||||
1 => "1 candidate".to_string(),
|
||||
n => format!("{n} candidates"),
|
||||
},
|
||||
proposal.len(),
|
||||
off
|
||||
);
|
||||
Ok(json!({"body": source, "name": name, "candidates": candidates, "proposal": proposal, "summary": summary}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
fn faces(s: &Session) -> usize {
|
||||
s.design.as_ref().unwrap().bodies[0].faces().len()
|
||||
}
|
||||
|
||||
fn hole(s: &mut Session) -> u64 {
|
||||
let body = s.design.as_ref().unwrap().bodies[0].name.clone();
|
||||
let r = s.execute("geometry.defeature.propose", &json!({"body": body, "below": {"hole_diameter": 12}})).unwrap();
|
||||
assert_eq!(r["proposal"].as_array().map(Vec::len), Some(1), "{r}");
|
||||
r["proposal"][0].as_u64().unwrap()
|
||||
}
|
||||
|
||||
/// The sample plate: its hole is proposed below 12 mm, suppressing it gives a block meshed
|
||||
/// without it (tet and hex), undo and redo take it out and put it back, it is saved and
|
||||
/// replayed on open, and deleting the defeature brings the hole back.
|
||||
#[test]
|
||||
fn the_plate_hole_is_suppressed_meshed_undone_saved_and_restored() {
|
||||
let (mut s, dir) = super::super::datum::tests::plate();
|
||||
let body = s.design.as_ref().unwrap().bodies[0].name.clone();
|
||||
assert_eq!(faces(&s), 8);
|
||||
let none = s.execute("geometry.defeature.propose", &json!({"body": body, "below": {"hole_diameter": 5}})).unwrap();
|
||||
assert_eq!((none["candidates"].as_array().map(Vec::len), none["proposal"].as_array().map(Vec::len)), (Some(1), Some(0)), "{none}");
|
||||
let id = hole(&mut s);
|
||||
s.execute("mesh.tet.generate", &json!({"body": body, "size": 4})).unwrap();
|
||||
let with_hole = s.model.elements().len();
|
||||
let r = s.execute("geometry.defeature", &json!({"name": "Holes", "features": [id]})).unwrap();
|
||||
assert_eq!(r["suppressed"], json!([id]), "{r}");
|
||||
assert_eq!(r["replaced"][0]["body"].as_str(), Some(body.as_str()), "{r}");
|
||||
assert_eq!(faces(&s), 6);
|
||||
// The proposal shows it off, by the defeature.
|
||||
let p = s.execute("geometry.defeature.propose", &json!({"body": body})).unwrap();
|
||||
assert_eq!((p["candidates"][0]["suppressed"].as_bool(), p["candidates"][0]["by"].as_str()), (Some(true), Some("Holes")), "{p}");
|
||||
// Meshed without it: a tet mesh fills the whole block, and the block sweeps into hexes.
|
||||
s.execute("mesh.tet.generate", &json!({"body": body, "size": 4})).unwrap();
|
||||
let volume = |s: &Session| s.design.as_ref().unwrap().bodies[0].measure().unwrap().volume;
|
||||
assert!((volume(&s) - 40.0 * 30.0 * 10.0).abs() < 1e-6, "{}", volume(&s));
|
||||
assert_ne!(s.model.elements().len(), with_hole);
|
||||
s.execute("mesh.hex.sweep", &json!({"body": body, "size": 5})).unwrap();
|
||||
assert!(s.model.elements().values().all(|e| e.nodes.len() == 8), "a block sweeps into hexes only");
|
||||
// Undo the meshes and the defeature: the hole is back; redo takes it out again.
|
||||
while !s.model.defeatures().is_empty() {
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
}
|
||||
assert_eq!(faces(&s), 8);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(faces(&s), 6);
|
||||
// Saved, and replayed on open of the model and on a re-open of the design.
|
||||
let file = dir.join("defeatured.fieldcraft");
|
||||
s.execute("file.save", &json!({"path": file.to_str().unwrap()})).unwrap();
|
||||
let mut t = Session::default();
|
||||
t.execute("file.open", &json!({"path": file.to_str().unwrap()})).unwrap();
|
||||
assert_eq!((t.model.defeatures().len(), faces(&t)), (1, 6));
|
||||
let design = s.sources.design.as_ref().unwrap().path().to_string();
|
||||
s.execute("geometry.open", &json!({"path": design})).unwrap();
|
||||
assert_eq!(faces(&s), 6);
|
||||
// Switched back on.
|
||||
let r = s.execute("geometry.defeature.delete", &json!({"name": "Holes"})).unwrap();
|
||||
assert_eq!(r["features"], json!([id]), "{r}");
|
||||
assert_eq!(faces(&s), 8);
|
||||
std::fs::remove_dir_all(dir).ok();
|
||||
}
|
||||
|
||||
/// Unknown features, body-building ones and bad ids are refused and leave the model as it was.
|
||||
#[test]
|
||||
fn bad_features_are_refused() {
|
||||
let (mut s, dir) = super::super::datum::tests::plate();
|
||||
for features in [json!([999]), json!(["Fx"]), json!([]), json!("F1, x"), json!(7)] {
|
||||
assert!(s.execute("geometry.defeature", &json!({"name": "Bad", "features": features})).is_err(), "{features}");
|
||||
}
|
||||
let body = s.design.as_ref().unwrap().bodies[0].name.clone();
|
||||
let base = s.execute("geometry.features", &json!({"body": body})).unwrap()["bodies"][0]["features"][0]["id"].as_u64().unwrap();
|
||||
let e = s.execute("geometry.defeature", &json!({"name": "Bad", "features": [base]})).unwrap_err();
|
||||
assert!(e.to_string().contains("builds a body"), "{e}");
|
||||
assert!(s.model.defeatures().is_empty() && faces(&s) == 8);
|
||||
for below in [json!({"hole_diameter": -1}), json!({"hole_diameter": "x"}), json!({"nope": 1})] {
|
||||
assert!(s.execute("geometry.defeature.propose", &json!({"body": body, "below": below})).is_err(), "{below}");
|
||||
}
|
||||
assert!(s.execute("geometry.defeature.propose", &json!({"body": "Nope"})).is_err());
|
||||
std::fs::remove_dir_all(dir).ok();
|
||||
}
|
||||
|
||||
/// The bracket's 6 mm fillet is proposed below 8 mm, defeatured back to a sharp corner and
|
||||
/// meshed; its holes stay.
|
||||
#[test]
|
||||
fn a_bracket_is_meshed_with_its_small_fillet_defeatured() {
|
||||
let (_dir, mut s) = super::super::datum::tests::scripted(json!([
|
||||
{"command": "sketch.create", "params": {"plane": "XY", "name": "Profile"}},
|
||||
{"command": "sketch.line", "params": {"points": [[0, 0], [60, 0], [60, 8], [8, 8], [8, 50], [0, 50]], "closed": true}},
|
||||
{"command": "sketch.finish", "params": {}},
|
||||
{"command": "solid.extrude", "params": {"distance": 40, "body_name": "Bracket"}},
|
||||
{"command": "solid.fillet", "params": {"edges": [[8, 8, 20]], "radius": 6}},
|
||||
{"command": "solid.hole", "params": {"position": [40, 8, 20], "diameter": 8}},
|
||||
{"command": "solid.hole", "params": {"position": [8, 32, 20], "diameter": 8}}
|
||||
]));
|
||||
let p = s.execute("geometry.defeature.propose", &json!({"body": "Bracket", "below": {"fillet_radius": 8}})).unwrap();
|
||||
let cats: Vec<&str> = p["candidates"].as_array().unwrap().iter().filter_map(|c| c["category"].as_str()).collect();
|
||||
assert_eq!(cats, ["fillet", "hole", "hole"], "{p}");
|
||||
let fillet = p["proposal"].as_array().unwrap().clone();
|
||||
assert_eq!(fillet.len(), 1);
|
||||
let before = faces(&s);
|
||||
let r = s.execute("geometry.defeature", &json!({"name": "Fillets", "features": fillet})).unwrap();
|
||||
assert_eq!(r["suppressed"], json!(fillet), "{r}");
|
||||
assert_eq!(faces(&s), before - 1);
|
||||
let r = s.execute("mesh.tet.generate", &json!({"body": "Bracket", "size": 4})).unwrap();
|
||||
assert!(r["elements"].as_u64().unwrap_or(0) > 0, "{r}");
|
||||
}
|
||||
}
|
||||
@@ -252,7 +252,7 @@ definition_commands!(
|
||||
BoundaryCondition,
|
||||
"boundary_condition",
|
||||
"Boundary Condition",
|
||||
r#"{"name": "clamp", "type": "fixed", "target": {"set": "root"} | {"reference_point": "RP"}} | {"name", "type": "displacement", "target", "values": [u1, u2, u3, ur1, ur2, ur3] (null leaves a degree of freedom free)} | {"name", "type": "symmetry", "target", "plane": "x|y|z" (the plane's normal)} — active in the steps that list it (model.step.activate)"#,
|
||||
r#"{"name": "clamp", "type": "fixed", "target": {"set": "root"} | {"reference_point": "RP"}} | {"name", "type": "displacement", "target", "values": [u1, u2, u3, ur1, ur2, ur3] (null leaves a degree of freedom free)} | {"name", "type": "symmetry", "target", "plane": "x|y|z" | {"normal": [nx, ny, nz]} (the plane's normal; an inclined plane holds n · u = 0 and the rotations about its in-plane axes)} — active in the steps that list it (model.step.activate)"#,
|
||||
super::forms::BOUNDARY_CONDITION,
|
||||
super::forms::BOUNDARY_CONDITION_DELETE,
|
||||
add_boundary_condition,
|
||||
|
||||
@@ -0,0 +1,515 @@
|
||||
//! `mesh.hex.extrude`: hexahedra from a quad section up to a target surface
|
||||
//! ([`fieldcraft_mesh::hex::extrude_to_surface`]).
|
||||
//!
|
||||
//! The section is, in this order: the quadrilaterals of a set (`source_set`: shell elements or
|
||||
//! element faces); the mesh already on the source faces (element faces tagged with them, or the
|
||||
//! boundary faces of a neighbouring body's mesh lying on them), whose nodes the extrusion shares
|
||||
//! so it conforms with that mesh; or a fresh quad mesh of a planar source face. Target and side
|
||||
//! faces are projected onto through the trimmed B-rep projection that projection blocking uses
|
||||
//! ([`fieldcraft_mesh::tet::geometry::Tessellation::project_trimmed`]): exact on analytic faces,
|
||||
//! the tessellation's accuracy on free-form ones.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use fieldcraft_geometry::{FaceSurface, Shape};
|
||||
use fieldcraft_mesh::hex::{ExtrudeLayers, ExtrudePath, ExtrudeRole, ExtrudeSpec, MAX_BIAS, MAX_LAYERS, extrude_to_surface};
|
||||
use fieldcraft_mesh::planar::{face_boundary, mesh_region};
|
||||
use fieldcraft_mesh::project::Geometry;
|
||||
use fieldcraft_mesh::surface::{BoundarySurface, SurfaceOptions};
|
||||
use fieldcraft_mesh::tet::geometry::Tessellation;
|
||||
use fieldcraft_model::{ElementKind, Entity, GeometryRef, Model};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::requirement::Requirement;
|
||||
use crate::schema::ProblemKind;
|
||||
use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
|
||||
pub const HEX_EXTRUDE: CommandSpec = CommandSpec {
|
||||
id: "mesh.hex.extrude",
|
||||
label: "Extrude to Surface",
|
||||
params: r#"{"source": [{"body": "Post", "face": "F1"}] (the section: faces already meshed, by this body or a neighbour, or one planar face meshed here) or "source_set": "Section" (a set of quad shell elements or element faces, such as a 2D mesh), "to_surface": [{"body": "Wall", "face": "F7"}] (the target faces the last layer lies on), "sides": [{"body": "Post", "face": "F3"}, …] (optional: the faces the section's sides follow), "direction": [0, 0, 1] or "normal": true (each node along the section's normal toward the target; the default), "layers": 8 or "size": 2.0 (layer thickness, mm; also the size of a section meshed here; default 1/25 of the paths' extent), "bias": 1.0 (last layer's thickness over the first's, 0.001 to 1000), "body": "Post" (the body the elements fill; default: the first source face's body), "tolerance": 0.05 (chordal tolerance of the tessellation, mm; default 1e-3 of each body's diagonal)} — replaces `body`'s mesh with hex8 layers stacked on the section, each node along its own straight path, the last layer on the target surface; with `sides`, every layer's boundary nodes lie on the side faces (which need not be straight) and the interior follows them. A section taken from an existing mesh keeps its nodes, so the extrusion conforms with the neighbour across it. Boundary element faces are tagged with `body`'s source, target and side faces. Reports the layers, counts, quality, the target gap and where the section came from; a path that misses the target, or elements that would invert, is an error that says where"#,
|
||||
form: Some(super::forms::HEX_EXTRUDE),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Design],
|
||||
run: hex_extrude,
|
||||
};
|
||||
|
||||
/// Most faces in `source`, `to_surface` or `sides`.
|
||||
const MAX_FACES: usize = 1000;
|
||||
/// Default layer size and chordal tolerance, relative to the extent.
|
||||
const DEFAULT_RELATIVE_SIZE: f64 = 1.0 / 25.0;
|
||||
const DEFAULT_RELATIVE_TOLERANCE: f64 = 1e-3;
|
||||
const MIN_RELATIVE_TOLERANCE: f64 = 1e-5;
|
||||
/// Most quadrilaterals of a section meshed here.
|
||||
const MAX_SECTION_QUADS: usize = 1_000_000;
|
||||
|
||||
/// The bodies the extrusion's faces lie on, and every face it names, with one projection.
|
||||
struct Faces {
|
||||
bodies: Vec<Tessellation>,
|
||||
/// Per body: its name, and its faces' names.
|
||||
names: Vec<(String, Vec<String>)>,
|
||||
/// Face id → (body, B-rep face).
|
||||
faces: Vec<(usize, u32)>,
|
||||
/// The largest chordal tolerance used.
|
||||
tolerance: f64,
|
||||
}
|
||||
|
||||
impl Geometry for Faces {
|
||||
fn project(&self, face: u32, p: [f64; 3]) -> Option<[f64; 3]> {
|
||||
let &(b, f) = self.faces.get(face as usize)?;
|
||||
self.bodies.get(b)?.project_trimmed(f, p)
|
||||
}
|
||||
fn normal(&self, face: u32, p: [f64; 3]) -> Option<[f64; 3]> {
|
||||
let &(b, f) = self.faces.get(face as usize)?;
|
||||
self.bodies.get(b)?.face_normal(f, p)
|
||||
}
|
||||
}
|
||||
|
||||
impl Faces {
|
||||
/// The ids of the faces `refs` names (a name selects all its pieces), loading their bodies.
|
||||
fn add(&mut self, s: &Session, refs: &[GeometryRef], tolerance: Option<f64>, key: &str) -> Result<Vec<u32>> {
|
||||
let design = s.design.as_deref().ok_or_else(|| EngineError::Params("open a design first".into()))?;
|
||||
let mut out = Vec::new();
|
||||
for r in refs {
|
||||
if !matches!(r.entity, Entity::Face(_)) {
|
||||
return Err(EngineError::invalid(key, format!("{r} is not a face")));
|
||||
}
|
||||
let shape = design.body(&r.body).ok_or_else(|| EngineError::invalid(key, format!("no body `{}`", r.body)))?;
|
||||
let b = match self.names.iter().position(|(n, _)| *n == shape.name) {
|
||||
Some(b) => b,
|
||||
None => self.load(shape, tolerance)?,
|
||||
};
|
||||
let names = self.names.get(b).map(|(_, f)| f.clone()).unwrap_or_default();
|
||||
let before = out.len();
|
||||
for (i, name) in names.iter().enumerate() {
|
||||
if r.selects(&GeometryRef::face(shape.name.as_str(), name.as_str())?) {
|
||||
let id = self.id(b, i as u32);
|
||||
if !out.contains(&id) {
|
||||
out.push(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
if out.len() == before {
|
||||
return Err(EngineError::invalid(key, format!("body `{}` has no face `{}`", r.body, r.entity.name().unwrap_or_default())));
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn load(&mut self, shape: &Shape, tolerance: Option<f64>) -> Result<usize> {
|
||||
let diag = shape.size()?;
|
||||
let tol = tolerance.unwrap_or(DEFAULT_RELATIVE_TOLERANCE * diag);
|
||||
if tol < MIN_RELATIVE_TOLERANCE * diag {
|
||||
return Err(EngineError::Params(format!("`tolerance` must be at least {} mm for body `{}`", MIN_RELATIVE_TOLERANCE * diag, shape.name)));
|
||||
}
|
||||
let mesh = shape.tessellate(tol)?;
|
||||
let surface = BoundarySurface::new(
|
||||
&mesh.positions,
|
||||
&mesh.triangles,
|
||||
&mesh.tri_face,
|
||||
&SurfaceOptions { weld_distance: mesh.weld_distance, ..SurfaceOptions::default() },
|
||||
)
|
||||
.map_err(|e| EngineError::Params(format!("body `{}`: {e}", shape.name)))?;
|
||||
self.bodies.push(Tessellation::new(&surface, super::mesh::analytic_surfaces(shape)));
|
||||
self.names.push((shape.name.clone(), shape.faces().iter().map(|f| f.name.clone()).collect()));
|
||||
self.tolerance = self.tolerance.max(tol);
|
||||
Ok(self.bodies.len() - 1)
|
||||
}
|
||||
|
||||
fn id(&mut self, b: usize, f: u32) -> u32 {
|
||||
match self.faces.iter().position(|x| *x == (b, f)) {
|
||||
Some(i) => i as u32,
|
||||
None => {
|
||||
self.faces.push((b, f));
|
||||
(self.faces.len() - 1) as u32
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The body and face name of face `id`.
|
||||
fn name(&self, id: u32) -> Option<(&str, &str)> {
|
||||
let &(b, f) = self.faces.get(id as usize)?;
|
||||
let (body, faces) = self.names.get(b)?;
|
||||
Some((body.as_str(), faces.get(f as usize)?.as_str()))
|
||||
}
|
||||
}
|
||||
|
||||
/// The section: node coordinates, the model label of each node it shares (none for a fresh
|
||||
/// section), and its quadrilaterals.
|
||||
struct Section {
|
||||
nodes: Vec<[f64; 3]>,
|
||||
labels: Vec<Option<u32>>,
|
||||
quads: Vec<[u32; 4]>,
|
||||
from: &'static str,
|
||||
}
|
||||
|
||||
impl Section {
|
||||
/// A section of model nodes: each label once, in order of first use.
|
||||
fn of_labels(model: &Model, quads: &[[u32; 4]], from: &'static str) -> Result<Section> {
|
||||
let mut index: BTreeMap<u32, u32> = BTreeMap::new();
|
||||
let mut s = Section { nodes: Vec::new(), labels: Vec::new(), quads: Vec::with_capacity(quads.len()), from };
|
||||
for q in quads {
|
||||
let mut local = [0u32; 4];
|
||||
for (slot, &l) in local.iter_mut().zip(q) {
|
||||
*slot = match index.get(&l) {
|
||||
Some(&i) => i,
|
||||
None => {
|
||||
let xyz = model.nodes().get(&l).map(|n| n.xyz).ok_or_else(|| EngineError::Params(format!("node {l} is not in the model")))?;
|
||||
let i = s.nodes.len() as u32;
|
||||
s.nodes.push(xyz);
|
||||
s.labels.push(Some(l));
|
||||
index.insert(l, i);
|
||||
i
|
||||
}
|
||||
};
|
||||
}
|
||||
s.quads.push(local);
|
||||
}
|
||||
Ok(s)
|
||||
}
|
||||
}
|
||||
|
||||
/// The four corners of element `e`'s face `face` (1-based), when it is a quadrilateral.
|
||||
fn quad_face(model: &Model, e: u32, face: u8) -> Option<[u32; 4]> {
|
||||
let el = model.elements().get(&e)?;
|
||||
let local = el.kind.faces().get(usize::from(face).checked_sub(1)?)?;
|
||||
if local.len() < 4 || (local.len() != 4 && local.len() != 8) {
|
||||
return None;
|
||||
}
|
||||
let corner = |k: usize| local.get(k).and_then(|&i| el.nodes.get(i)).copied();
|
||||
Some([corner(0)?, corner(1)?, corner(2)?, corner(3)?])
|
||||
}
|
||||
|
||||
/// The corners of a quad shell element.
|
||||
fn quad_shell(model: &Model, e: u32) -> Option<[u32; 4]> {
|
||||
let el = model.elements().get(&e)?;
|
||||
if !matches!(el.kind, ElementKind::Quad4 | ElementKind::Quad8) {
|
||||
return None;
|
||||
}
|
||||
Some([*el.nodes.first()?, *el.nodes.get(1)?, *el.nodes.get(2)?, *el.nodes.get(3)?])
|
||||
}
|
||||
|
||||
/// The elements of `body`'s mesh.
|
||||
fn body_elements(model: &Model, body: &str) -> std::collections::BTreeSet<u32> {
|
||||
model.body_elements(body).into_iter().collect()
|
||||
}
|
||||
|
||||
/// The quadrilaterals tagged with the faces `refs` names (shell elements filling them, element
|
||||
/// faces on them), leaving out `exclude`'s elements.
|
||||
fn tagged_quads(model: &Model, refs: &[GeometryRef], exclude: &std::collections::BTreeSet<u32>) -> Result<Vec<[u32; 4]>> {
|
||||
let c = model.classification();
|
||||
let mut tags: Vec<u32> = refs.iter().flat_map(|r| c.tags_selected_by(r)).collect();
|
||||
tags.sort_unstable();
|
||||
tags.dedup();
|
||||
let mut out = Vec::new();
|
||||
for (&e, t) in c.elements() {
|
||||
if exclude.contains(&e) {
|
||||
continue;
|
||||
}
|
||||
if t.region.is_some_and(|r| tags.binary_search(&r).is_ok()) {
|
||||
out.push(quad_shell(model, e).ok_or_else(|| EngineError::Params(format!("element {e} on the source face is not a quadrilateral")))?);
|
||||
}
|
||||
for (&f, tag) in &t.faces {
|
||||
if tags.binary_search(tag).is_ok() {
|
||||
out.push(
|
||||
quad_face(model, e, f)
|
||||
.ok_or_else(|| EngineError::Params(format!("element {e}'s face {f} on the source face is not a quadrilateral")))?,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The boundary faces of other bodies' meshes (tagged faces) that lie on the source faces.
|
||||
fn neighbour_quads(model: &Model, geometry: &Faces, source: &[u32], exclude: &std::collections::BTreeSet<u32>) -> Vec<[u32; 4]> {
|
||||
let tol = 10.0 * geometry.tolerance.max(1e-9);
|
||||
let on = |p: [f64; 3]| {
|
||||
source.iter().any(|&f| geometry.project(f, p).is_some_and(|q| (0..3).map(|k| (q[k] - p[k]).powi(2)).sum::<f64>().sqrt() <= tol))
|
||||
};
|
||||
let xyz = |l: u32| model.nodes().get(&l).map(|n| n.xyz);
|
||||
let mut out = Vec::new();
|
||||
for (&e, t) in model.classification().elements() {
|
||||
if exclude.contains(&e) {
|
||||
continue;
|
||||
}
|
||||
for &f in t.faces.keys() {
|
||||
let Some(q) = quad_face(model, e, f) else { continue };
|
||||
let Some(pts) = q.iter().map(|&l| xyz(l)).collect::<Option<Vec<_>>>() else { continue };
|
||||
let centre = pts.iter().fold([0.0; 3], |a, p| [a[0] + p[0] / 4.0, a[1] + p[1] / 4.0, a[2] + p[2] / 4.0]);
|
||||
if pts.iter().all(|p| on(*p)) && on(centre) {
|
||||
out.push(q);
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// A quad mesh of one planar face of `shape` of about `size`.
|
||||
fn mesh_planar_face(shape: &Shape, face: u32, size: f64, tolerance: f64) -> Result<Section> {
|
||||
let surfaces = shape.face_surfaces()?;
|
||||
let Some(Some(FaceSurface::Plane { normal, point })) = surfaces.get(face as usize).copied() else {
|
||||
return Err(EngineError::Params(format!(
|
||||
"the source face of body `{}` is not meshed and not planar: mesh it first (a 2D mesh, or a neighbouring body's mesh)",
|
||||
shape.name
|
||||
)));
|
||||
};
|
||||
let n = unit(normal).ok_or_else(|| EngineError::Params("the source face has no normal".into()))?;
|
||||
let t = if n[0].abs() < 0.9 { [1.0, 0.0, 0.0] } else { [0.0, 1.0, 0.0] };
|
||||
let u = unit(cross(n, t)).ok_or_else(|| EngineError::Params("the source face has no normal".into()))?;
|
||||
let v = cross(n, u);
|
||||
let dot = |a: [f64; 3], b: [f64; 3]| a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
||||
let mesh = shape.tessellate(tolerance)?;
|
||||
// The tessellation in the face's frame, the face at z = 0.
|
||||
let local: Vec<[f64; 3]> = mesh
|
||||
.positions
|
||||
.iter()
|
||||
.map(|p| {
|
||||
let d = [p[0] - point[0], p[1] - point[1], p[2] - point[2]];
|
||||
[dot(d, u), dot(d, v), dot(d, n)]
|
||||
})
|
||||
.collect();
|
||||
let boundary = face_boundary(&local, &mesh.triangles, &mesh.tri_face, face, mesh.weld_distance.max(tolerance))?;
|
||||
let opts = fieldcraft_mesh::quad::QuadOptions { max_elements: MAX_SECTION_QUADS, ..Default::default() };
|
||||
let m = mesh_region(&boundary.loops, ElementKind::Quad4, size, &opts)?;
|
||||
let at = |xy: [f64; 2]| [0, 1, 2].map(|k| point[k] + xy[0] * u[k] + xy[1] * v[k]);
|
||||
let nodes: Vec<[f64; 3]> = m.nodes.iter().map(|xy| at(*xy)).collect();
|
||||
let quads = m
|
||||
.elements
|
||||
.iter()
|
||||
.map(|e| match e.as_slice() {
|
||||
[a, b, c, d, ..] => Ok([*a, *b, *c, *d]),
|
||||
_ => Err(EngineError::Params("the section mesher made an element that is not a quadrilateral".into())),
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
Ok(Section { labels: vec![None; nodes.len()], nodes, quads, from: "meshed" })
|
||||
}
|
||||
|
||||
fn unit(a: [f64; 3]) -> Option<[f64; 3]> {
|
||||
let l = (a[0] * a[0] + a[1] * a[1] + a[2] * a[2]).sqrt();
|
||||
(l > 0.0 && l.is_finite()).then(|| a.map(|x| x / l))
|
||||
}
|
||||
|
||||
fn cross(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
|
||||
[a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]
|
||||
}
|
||||
|
||||
/// A list of face references.
|
||||
fn face_refs(p: &Value, key: &str, required: bool) -> Result<Vec<GeometryRef>> {
|
||||
let refs: Vec<GeometryRef> = match p.get(key) {
|
||||
None if !required => return Ok(Vec::new()),
|
||||
None => return Err(EngineError::param(key, ProblemKind::Required)),
|
||||
Some(v) => serde_json::from_value(v.clone()).map_err(|e| EngineError::invalid(key, format!("a list of {{\"body\", \"face\"}}: {e}")))?,
|
||||
};
|
||||
if refs.len() > MAX_FACES || (required && refs.is_empty()) {
|
||||
return Err(EngineError::invalid(key, format!("1 to {MAX_FACES} faces")));
|
||||
}
|
||||
Ok(refs)
|
||||
}
|
||||
|
||||
/// The body the elements fill: `body`, or the first source face's.
|
||||
pub(super) fn extrude_body(_: &Session, p: &Value) -> Result<Vec<String>> {
|
||||
if let Some(b) = p.get("body") {
|
||||
let name = b.as_str().ok_or_else(|| EngineError::param("body", ProblemKind::NotAName))?;
|
||||
GeometryRef::body(name)?;
|
||||
return Ok(vec![name.to_string()]);
|
||||
}
|
||||
let source = face_refs(p, "source", false)?;
|
||||
let first = source.first().ok_or_else(|| EngineError::Params("give `body` (the body the elements fill) with `source_set`".into()))?;
|
||||
Ok(vec![first.body.clone()])
|
||||
}
|
||||
|
||||
fn hex_extrude(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let tolerance = super::hex::number(p, "tolerance")?;
|
||||
let size = super::hex::number(p, "size")?;
|
||||
let layers = match p.get("layers") {
|
||||
None => None,
|
||||
Some(v) => Some(
|
||||
v.as_u64()
|
||||
.filter(|n| (1..=MAX_LAYERS as u64).contains(n))
|
||||
.ok_or_else(|| EngineError::invalid("layers", format!("a whole number from 1 to {MAX_LAYERS}")))? as usize,
|
||||
),
|
||||
};
|
||||
let bias = match p.get("bias") {
|
||||
None => 1.0,
|
||||
Some(v) => v
|
||||
.as_f64()
|
||||
.filter(|b| (1.0 / MAX_BIAS..=MAX_BIAS).contains(b))
|
||||
.ok_or_else(|| EngineError::invalid("bias", format!("a number from {} to {MAX_BIAS}", 1.0 / MAX_BIAS)))?,
|
||||
};
|
||||
let path = match (p.get("direction"), p.get("normal")) {
|
||||
(Some(d), None) => {
|
||||
let d: [f64; 3] = serde_json::from_value(d.clone()).map_err(|_| EngineError::param("direction", ProblemKind::NotAVector { len: 3 }))?;
|
||||
if !d.iter().all(|x| x.is_finite()) || unit(d).is_none() {
|
||||
return Err(EngineError::invalid("direction", "a vector with a length"));
|
||||
}
|
||||
ExtrudePath::Direction(d)
|
||||
}
|
||||
(None, None) => ExtrudePath::Normal,
|
||||
(None, Some(n)) => match n.as_bool() {
|
||||
Some(true) => ExtrudePath::Normal,
|
||||
_ => return Err(EngineError::invalid("normal", "true, or give `direction`")),
|
||||
},
|
||||
(Some(_), Some(_)) => return Err(EngineError::Params("give `direction` or `normal`, not both".into())),
|
||||
};
|
||||
let source = face_refs(p, "source", false)?;
|
||||
let source_set = match p.get("source_set") {
|
||||
None => None,
|
||||
Some(v) => Some(v.as_str().ok_or_else(|| EngineError::param("source_set", ProblemKind::NotAName))?.to_string()),
|
||||
};
|
||||
if source.is_empty() == source_set.is_none() {
|
||||
return Err(EngineError::Params("give the section as `source` (faces) or `source_set` (a set), one of them".into()));
|
||||
}
|
||||
let targets = face_refs(p, "to_surface", true)?;
|
||||
let sides = face_refs(p, "sides", false)?;
|
||||
let body = extrude_body(s, p)?.into_iter().next().unwrap_or_default();
|
||||
let body_ref = GeometryRef::body(body.as_str())?;
|
||||
let mut faces = Faces { bodies: Vec::new(), names: Vec::new(), faces: Vec::new(), tolerance: 0.0 };
|
||||
let source_ids = faces.add(s, &source, tolerance, "source")?;
|
||||
let target_ids = faces.add(s, &targets, tolerance, "to_surface")?;
|
||||
let side_ids = faces.add(s, &sides, tolerance, "sides")?;
|
||||
if let Some(f) = target_ids.iter().find(|f| source_ids.contains(f) || side_ids.contains(f)) {
|
||||
let (b, n) = faces.name(*f).unwrap_or_default();
|
||||
return Err(EngineError::Params(format!("face `{n}` of body `{b}` is both a target and a source or side face")));
|
||||
}
|
||||
|
||||
// The section: a set, the mesh already on the source faces, or a fresh mesh of one face.
|
||||
let own = body_elements(&s.model, &body);
|
||||
let section = match &source_set {
|
||||
Some(name) => {
|
||||
let r = s.model.resolve_set(name)?;
|
||||
let mut quads = Vec::new();
|
||||
for &e in &r.elements {
|
||||
if let Some(q) = quad_shell(&s.model, e) {
|
||||
quads.push(q);
|
||||
}
|
||||
}
|
||||
for f in &r.faces {
|
||||
quads.push(
|
||||
quad_face(&s.model, f.element, f.face).ok_or_else(|| {
|
||||
EngineError::Params(format!("set `{name}`: element {}'s face {} is not a quadrilateral", f.element, f.face))
|
||||
})?,
|
||||
);
|
||||
}
|
||||
if quads.is_empty() {
|
||||
return Err(EngineError::Params(format!("set `{name}` has no quadrilaterals (quad shell elements or hex element faces)")));
|
||||
}
|
||||
Section::of_labels(&s.model, &quads, "set")?
|
||||
}
|
||||
None => {
|
||||
let tagged = tagged_quads(&s.model, &source, &own)?;
|
||||
if !tagged.is_empty() {
|
||||
Section::of_labels(&s.model, &tagged, "mesh")?
|
||||
} else {
|
||||
let near = neighbour_quads(&s.model, &faces, &source_ids, &own);
|
||||
if !near.is_empty() {
|
||||
Section::of_labels(&s.model, &near, "neighbour")?
|
||||
} else {
|
||||
let [id] = source_ids.as_slice() else {
|
||||
return Err(EngineError::Params("the source faces are not meshed: give one face to mesh here, or mesh them first".into()));
|
||||
};
|
||||
let (b, f) = faces.faces.get(*id as usize).copied().ok_or_else(|| EngineError::Params("no source face".into()))?;
|
||||
let name = faces.names.get(b).map(|n| n.0.clone()).unwrap_or_default();
|
||||
let shape = s.design.as_deref().and_then(|d| d.body(&name)).ok_or_else(|| EngineError::Params(format!("no body `{name}`")))?;
|
||||
let extent = shape.size()?;
|
||||
let h = size.unwrap_or(DEFAULT_RELATIVE_SIZE * extent);
|
||||
mesh_planar_face(shape, f, h, tolerance.unwrap_or(DEFAULT_RELATIVE_TOLERANCE * extent))?
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let layers = match (layers, size) {
|
||||
(Some(n), _) => ExtrudeLayers::Count(n),
|
||||
(None, Some(h)) => ExtrudeLayers::Size(h),
|
||||
(None, None) => {
|
||||
let extent = s.design.as_deref().and_then(|d| d.body(&body)).map(Shape::size).transpose()?.unwrap_or(1.0);
|
||||
ExtrudeLayers::Size(DEFAULT_RELATIVE_SIZE * extent)
|
||||
}
|
||||
};
|
||||
let spec = ExtrudeSpec {
|
||||
nodes: §ion.nodes,
|
||||
quads: §ion.quads,
|
||||
path,
|
||||
targets: &target_ids,
|
||||
sides: &side_ids,
|
||||
layers,
|
||||
bias,
|
||||
tolerance: faces.tolerance.max(1e-9),
|
||||
max_elements: fieldcraft_model::MAX_ELEMENTS / 8,
|
||||
};
|
||||
let m = extrude_to_surface(&spec, &faces).map_err(|e| EngineError::of_body(&body, e))?;
|
||||
|
||||
// Replace the body's mesh; the section's shared nodes stay.
|
||||
let mut model = s.model.clone();
|
||||
let cleared = super::replace::clear(&mut model, [body.as_str()]);
|
||||
if let Some(l) = section.labels.iter().flatten().find(|l| !model.nodes().contains_key(l)) {
|
||||
return Err(EngineError::Params(format!("the section's node {l} belongs to body `{body}`'s mesh, which the extrusion replaces")));
|
||||
}
|
||||
let n_src = section.nodes.len();
|
||||
let fresh = m.nodes.len() - section.labels.iter().flatten().count();
|
||||
let (first_node, first_element) = super::fresh_labels(&model, fresh, m.hexes.len())?;
|
||||
let mut labels = Vec::with_capacity(m.nodes.len());
|
||||
let mut next = first_node;
|
||||
for (i, xyz) in m.nodes.iter().enumerate() {
|
||||
let shared = if i < n_src { section.labels.get(i).copied().flatten() } else { None };
|
||||
labels.push(match shared {
|
||||
Some(l) => l,
|
||||
None => {
|
||||
model.set_node(next, *xyz)?;
|
||||
next += 1;
|
||||
next - 1
|
||||
}
|
||||
});
|
||||
}
|
||||
let body_tag = model.geometry_tag(&body_ref)?;
|
||||
for (e, h) in m.hexes.iter().enumerate() {
|
||||
let nodes: Vec<u32> = h.iter().map(|&i| labels.get(i as usize).copied().unwrap_or(0)).collect();
|
||||
let l = first_element + e as u32;
|
||||
model.set_element(l, ElementKind::Hex8, nodes)?;
|
||||
model.tag_element(l, body_tag)?;
|
||||
}
|
||||
// Boundary faces on the body's own B-rep faces get their tags.
|
||||
let source_own: Vec<u32> = source_ids.iter().copied().filter(|&f| faces.name(f).is_some_and(|(b, _)| b == body)).collect();
|
||||
let mut tags: BTreeMap<u32, u32> = BTreeMap::new();
|
||||
for b in &m.boundary {
|
||||
let face = match b.role {
|
||||
ExtrudeRole::Source => source_own.first().copied(),
|
||||
ExtrudeRole::Target(f) | ExtrudeRole::Side(Some(f)) => Some(f),
|
||||
ExtrudeRole::Side(None) => None,
|
||||
};
|
||||
let Some((owner, name)) = face.and_then(|f| faces.name(f)) else { continue };
|
||||
if owner != body {
|
||||
continue;
|
||||
}
|
||||
let key = face.unwrap_or_default();
|
||||
let tag = match tags.get(&key) {
|
||||
Some(&t) => t,
|
||||
None => {
|
||||
let t = model.geometry_tag(&GeometryRef::face(body.as_str(), name)?)?;
|
||||
tags.insert(key, t);
|
||||
t
|
||||
}
|
||||
};
|
||||
model.tag_element_face(first_element + b.element, b.face, tag)?;
|
||||
}
|
||||
let (nodes, elements) = (model.nodes().len(), model.elements().len());
|
||||
s.model = model;
|
||||
let reply = json!({
|
||||
"body": body,
|
||||
"section": {"from": section.from, "quadrilaterals": section.quads.len(), "nodes": n_src, "shared_nodes": section.labels.iter().flatten().count()},
|
||||
"layers": m.layers,
|
||||
"kind": "hex8",
|
||||
"elements": m.hexes.len(),
|
||||
"new_nodes": fresh,
|
||||
"quality": {"min_scaled_jacobian": m.min_scaled_jacobian, "mean_scaled_jacobian": m.mean_scaled_jacobian},
|
||||
"volume": m.gate.volume,
|
||||
"max_target_gap": m.max_target_gap,
|
||||
"nodes": nodes,
|
||||
"model_elements": elements,
|
||||
});
|
||||
Ok(cleared.report(s, reply))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,185 @@
|
||||
use super::*;
|
||||
use crate::tests::{TempDir, design_file};
|
||||
|
||||
fn open(commands: Value) -> (TempDir, Session) {
|
||||
let bytes = fieldcraft_geometry::script_design(&json!({ "commands": commands }).to_string()).unwrap();
|
||||
let (dir, path) = design_file(&bytes);
|
||||
let mut s = Session::default();
|
||||
s.execute("geometry.open", &json!({"path": path})).unwrap();
|
||||
(dir, s)
|
||||
}
|
||||
|
||||
/// A square post whose top is cut by a cylinder (axis X, radius 20, at z = 30), a square post
|
||||
/// whose top is a sphere cap (centre z = −5, radius 25), and a block under the first post.
|
||||
fn parts() -> (TempDir, Session) {
|
||||
open(json!([
|
||||
{"command": "solid.box", "params": {"corner": [-4, -4, 0], "length": 8, "width": 8, "height": 30, "body_name": "Post"}},
|
||||
{"command": "solid.cylinder", "params": {"base": [-20, 0, 30], "axis": [1, 0, 0], "radius": 20, "height": 40, "body_name": "Bar", "operation": "new"}},
|
||||
{"command": "solid.combine", "params": {"target": "Post", "tools": ["Bar"], "operation": "cut"}},
|
||||
{"command": "solid.box", "params": {"corner": [-6, -6, 0], "length": 12, "width": 12, "height": 30, "body_name": "Dome", "operation": "new"}},
|
||||
{"command": "solid.sphere", "params": {"center": [0, 0, -5], "radius": 25, "body_name": "Ball", "operation": "new"}},
|
||||
{"command": "solid.combine", "params": {"target": "Dome", "tools": ["Ball"], "operation": "intersect"}},
|
||||
{"command": "solid.box", "params": {"corner": [-4, -4, -10], "length": 8, "width": 8, "height": 10, "body_name": "Base", "operation": "new"}},
|
||||
]))
|
||||
}
|
||||
|
||||
/// `{"body", "face"}` of each face of `body` whose surface `pick` accepts.
|
||||
fn faces(s: &Session, body: &str, pick: impl Fn(&FaceSurface) -> bool) -> Vec<Value> {
|
||||
let b = s.design.as_deref().unwrap().body(body).unwrap();
|
||||
b.faces()
|
||||
.iter()
|
||||
.zip(b.face_surfaces().unwrap())
|
||||
.filter(|(_, f)| f.as_ref().is_some_and(&pick))
|
||||
.map(|(f, _)| json!({"body": body, "face": f.name}))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn plane(n: [f64; 3]) -> impl Fn(&FaceSurface) -> bool {
|
||||
move |f| matches!(f, FaceSurface::Plane { normal, .. } if (0..3).all(|k| (normal[k] - n[k]).abs() < 1e-9))
|
||||
}
|
||||
|
||||
fn sides(s: &Session, body: &str) -> Vec<Value> {
|
||||
faces(s, body, |f| matches!(f, FaceSurface::Plane { normal, .. } if normal[2].abs() < 1e-9))
|
||||
}
|
||||
|
||||
/// The coordinates of the nodes on face `face` (through a set by geometry).
|
||||
fn nodes_on(s: &mut Session, name: &str, face: &Value) -> Vec<[f64; 3]> {
|
||||
s.execute("model.set.create", &json!({"name": name, "geometry": [face]})).unwrap();
|
||||
let r = s.model.resolve_set(name).unwrap();
|
||||
r.nodes.iter().map(|n| s.model.nodes()[n].xyz).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_square_post_extrudes_up_to_a_cylinder() {
|
||||
let (_d, mut s) = parts();
|
||||
let bottom = faces(&s, "Post", plane([0.0, 0.0, -1.0]));
|
||||
let top = faces(&s, "Post", |f| matches!(f, FaceSurface::Cylinder { .. }));
|
||||
let r = s
|
||||
.execute("mesh.hex.extrude", &json!({"source": bottom, "to_surface": top, "sides": sides(&s, "Post"), "direction": [0, 0, 1], "size": 1.0}))
|
||||
.unwrap();
|
||||
assert_eq!((r["body"].as_str(), r["section"]["from"].as_str()), (Some("Post"), Some("meshed")), "{r}");
|
||||
assert_eq!(r["layers"], 10, "{r}");
|
||||
let min_sj = r["quality"]["min_scaled_jacobian"].as_f64().unwrap();
|
||||
assert!(min_sj >= 0.5, "{r}");
|
||||
assert!(r["max_target_gap"].as_f64().unwrap() < 1e-6, "{r}");
|
||||
// ∫∫ (30 − √(400 − y²)) dx dy over the 8 × 8 square.
|
||||
let exact = 8.0 * (30.0 * 8.0 - (4.0 * 384.0f64.sqrt() + 400.0 * 0.2f64.asin()));
|
||||
let volume = r["volume"].as_f64().unwrap();
|
||||
assert!((volume - exact).abs() < 1e-3 * exact, "{volume} vs {exact}");
|
||||
// Every node of the last layer is on the cylinder: the target faces are tagged.
|
||||
let on = nodes_on(&mut s, "Top", &top[0]);
|
||||
assert_eq!(on.len() as u64, r["section"]["nodes"].as_u64().unwrap(), "one node per section node");
|
||||
for p in on {
|
||||
let radius = (p[1].powi(2) + (p[2] - 30.0).powi(2)).sqrt();
|
||||
assert!((radius - 20.0).abs() < 1e-6, "{p:?}: {radius}");
|
||||
}
|
||||
let q = s.execute("mesh.quality", &json!({})).unwrap();
|
||||
assert_eq!(q["invalid"]["count"], 0, "{q}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_square_post_extrudes_up_to_a_sphere_cap_along_its_normal() {
|
||||
let (_d, mut s) = parts();
|
||||
let bottom = faces(&s, "Dome", plane([0.0, 0.0, -1.0]));
|
||||
let cap = faces(&s, "Dome", |f| matches!(f, FaceSurface::Sphere { .. }));
|
||||
let r = s
|
||||
.execute("mesh.hex.extrude", &json!({"source": bottom, "to_surface": cap, "sides": sides(&s, "Dome"), "layers": 8, "bias": 0.5, "size": 1.5}))
|
||||
.unwrap();
|
||||
assert_eq!(r["layers"], 8, "{r}");
|
||||
assert!(r["quality"]["min_scaled_jacobian"].as_f64().unwrap() >= 0.5, "{r}");
|
||||
let on = nodes_on(&mut s, "Cap", &cap[0]);
|
||||
assert!(on.len() > 50, "{}", on.len());
|
||||
for p in on {
|
||||
let radius = (p[0].powi(2) + p[1].powi(2) + (p[2] + 5.0).powi(2)).sqrt();
|
||||
assert!((radius - 25.0).abs() < 1e-6, "{p:?}: {radius}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_extrusion_conforms_with_the_neighbour_it_starts_from() {
|
||||
let (_d, mut s) = parts();
|
||||
s.execute("mesh.hex.generate", &json!({"body": "Base", "size": 2.0})).unwrap();
|
||||
let base_nodes = s.model.nodes().len();
|
||||
let bottom = faces(&s, "Post", plane([0.0, 0.0, -1.0]));
|
||||
let top = faces(&s, "Post", |f| matches!(f, FaceSurface::Cylinder { .. }));
|
||||
let r = s
|
||||
.execute("mesh.hex.extrude", &json!({"source": bottom, "to_surface": top, "sides": sides(&s, "Post"), "direction": [0, 0, 1], "size": 2.0}))
|
||||
.unwrap();
|
||||
assert_eq!(r["section"]["from"], "neighbour", "{r}");
|
||||
assert_eq!(r["section"]["shared_nodes"], 25, "the base's 5 × 5 top nodes: {r}");
|
||||
assert_eq!(s.model.nodes().len(), base_nodes + r["new_nodes"].as_u64().unwrap() as usize);
|
||||
let d = s.execute("mesh.check.duplicates", &json!({})).unwrap();
|
||||
assert_eq!((d["counts"]["coincident"].as_u64(), d["counts"]["cracks"].as_u64()), (Some(0), Some(0)), "{d}");
|
||||
// The base keeps its mesh; extruding again replaces only the post's.
|
||||
let base = s.model.body_elements("Base").len();
|
||||
s.execute("mesh.hex.extrude", &json!({"source": bottom, "to_surface": top, "direction": [0, 0, 1], "layers": 3})).unwrap();
|
||||
assert_eq!(s.model.body_elements("Base").len(), base);
|
||||
assert_eq!(s.model.body_elements("Post").len(), 16 * 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_2d_mesh_extrudes_from_its_set() {
|
||||
let (_d, mut s) = parts();
|
||||
s.execute("mesh.planar", &json!({"polygon": [[-3, -3], [3, -3], [3, 3], [-3, 3]], "size": 1.0, "name": "Plane"})).unwrap();
|
||||
s.execute("model.set.create", &json!({"name": "Section", "geometry": [{"body": "Plane", "face": "area"}]})).unwrap();
|
||||
let top = faces(&s, "Post", |f| matches!(f, FaceSurface::Cylinder { .. }));
|
||||
let r = s.execute("mesh.hex.extrude", &json!({"source_set": "Section", "to_surface": top, "body": "Column", "layers": 5})).unwrap();
|
||||
assert_eq!(r["section"]["from"], "set", "{r}");
|
||||
let quads = r["section"]["quadrilaterals"].as_u64().unwrap() as usize;
|
||||
assert_eq!(r["section"]["shared_nodes"], r["section"]["nodes"], "{r}");
|
||||
assert_eq!(s.model.body_elements("Column").len(), quads * 5);
|
||||
assert!(r["quality"]["min_scaled_jacobian"].as_f64().unwrap() >= 0.5, "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_params_are_errors_and_change_nothing() {
|
||||
let (_d, mut s) = parts();
|
||||
let bottom = faces(&s, "Post", plane([0.0, 0.0, -1.0]));
|
||||
let top = faces(&s, "Post", |f| matches!(f, FaceSurface::Cylinder { .. }));
|
||||
let ok = json!({"source": bottom, "to_surface": top, "direction": [0, 0, 1], "layers": 2});
|
||||
let with = |k: &str, v: Value| {
|
||||
let mut p = ok.clone();
|
||||
p[k] = v;
|
||||
p
|
||||
};
|
||||
let without = |k: &str| {
|
||||
let mut p = ok.clone();
|
||||
p.as_object_mut().unwrap().remove(k);
|
||||
p
|
||||
};
|
||||
let cases = [
|
||||
without("to_surface"),
|
||||
without("source"),
|
||||
with("to_surface", json!([])),
|
||||
with("to_surface", json!("F1")),
|
||||
with("to_surface", json!([{"body": "Nope", "face": "F1"}])),
|
||||
with("to_surface", json!([{"body": "Post", "face": "nope"}])),
|
||||
with("to_surface", json!([{"body": "Post"}])),
|
||||
with("to_surface", bottom.clone().into()),
|
||||
with("source_set", json!("Nope")),
|
||||
with("direction", json!([0, 0, 0])),
|
||||
with("direction", json!([0, 0, -1])),
|
||||
with("direction", json!([1, 0])),
|
||||
with("direction", json!([f64::MAX, 0, 0])),
|
||||
with("normal", json!(false)),
|
||||
with("layers", json!(0)),
|
||||
with("layers", json!(1e9)),
|
||||
with("layers", json!(-3)),
|
||||
with("bias", json!(0)),
|
||||
with("bias", json!(1e9)),
|
||||
with("bias", json!("x")),
|
||||
with("size", json!(-1)),
|
||||
with("tolerance", json!(1e-30)),
|
||||
with("body", json!(7)),
|
||||
with("sides", json!([{"body": "Dome", "face": "F4:-z"}])),
|
||||
with("source", json!([{"body": "Post", "face": "F1:-x"}, {"body": "Post", "face": "F1:+x"}])),
|
||||
// A curved face that is not meshed cannot be meshed here.
|
||||
with("source", top.clone().into()),
|
||||
];
|
||||
let before = s.model.clone();
|
||||
for (i, p) in cases.iter().enumerate() {
|
||||
assert!(s.execute("mesh.hex.extrude", p).is_err(), "case {i}: {p}");
|
||||
assert_eq!(s.model, before, "case {i} changed the model");
|
||||
}
|
||||
assert!(s.execute("mesh.hex.extrude", &ok).is_ok());
|
||||
}
|
||||
@@ -231,7 +231,12 @@ impl Session {
|
||||
}
|
||||
});
|
||||
if let Some(d) = &design {
|
||||
warnings.extend(d.errors.iter().filter(|e| e.feature.starts_with("partition ")).map(|e| format!("{}: {}", e.feature, e.message)));
|
||||
warnings.extend(
|
||||
d.errors
|
||||
.iter()
|
||||
.filter(|e| e.feature.starts_with("partition ") || e.feature.starts_with("merge "))
|
||||
.map(|e| format!("{}: {}", e.feature, e.message)),
|
||||
);
|
||||
}
|
||||
sources.design = Some(source);
|
||||
}
|
||||
@@ -241,7 +246,7 @@ impl Session {
|
||||
surfaces.extend(opened.into_iter().flatten().map(Arc::new));
|
||||
sources.meshes.push(source);
|
||||
}
|
||||
let session = Session {
|
||||
let mut session = Session {
|
||||
model: file.model,
|
||||
design,
|
||||
surfaces,
|
||||
@@ -252,6 +257,8 @@ impl Session {
|
||||
view: file.view,
|
||||
..Session::default()
|
||||
};
|
||||
let (_, failed) = super::stitch::restitch(&mut session);
|
||||
warnings.extend(failed.into_iter().map(|(n, why)| format!("stitch `{n}`: {why}")));
|
||||
(session, warnings)
|
||||
}
|
||||
|
||||
|
||||
@@ -58,8 +58,14 @@ pub const SET: &[Field] = &[
|
||||
labels("elements", "Elements", LabelOf::Element)
|
||||
.help("Mesh element labels; Use selection takes the picked elements.")
|
||||
.hint("labels: 1, 2, 10-20"),
|
||||
json("faces", "Element faces").help("Element faces, as a JSON list of element label and local face number.").hint("[{\"element\", \"face\"}]"),
|
||||
json("edges", "Element edges").help("Element edges, as a JSON list of element label and local edge number.").hint("[{\"element\", \"edge\"}]"),
|
||||
sides("faces", "Element faces", Side::Face)
|
||||
.help(
|
||||
"Element faces as element:face (local face number), ranges of elements sharing one: 1-20:3, 25:1. Use selection takes the picked faces.",
|
||||
)
|
||||
.hint("element:face, e.g. 1-20:3, 25:1"),
|
||||
sides("edges", "Element edges", Side::Edge)
|
||||
.help("Element edges as element:edge (local edge number), ranges of elements sharing one: 1-20:3, 25:1.")
|
||||
.hint("element:edge, e.g. 1-20:3"),
|
||||
];
|
||||
|
||||
const ELASTIC: Field = variants(
|
||||
@@ -186,18 +192,19 @@ const HYPERELASTIC: Field = variants(
|
||||
.help("A large-strain rubber model (nonlinear steps), built on the isotropic constants.")
|
||||
.hint("large strain in nonlinear steps, from the isotropic constants");
|
||||
|
||||
/// The material axes 1, 2, 3 (the global X, Y, Z until material orientations land).
|
||||
const AXES_123: &[&str] = &["1", "2", "3"];
|
||||
|
||||
const THERMAL: Field = group(
|
||||
"thermal",
|
||||
"Thermal",
|
||||
&[
|
||||
json("conductivity", "Conductivity")
|
||||
.help("Thermal conductivity, one value or one per axis. Steel ≈ 50 W/(m·K), i.e. 50 in mm-t-s-N.")
|
||||
.hint("k or [k1, k2, k3]"),
|
||||
per_axis("conductivity", "Conductivity", D::Conductivity, Some(0.0), AXES_123)
|
||||
.help("Thermal conductivity, one value or one per material axis. Steel ≈ 50 W/(m·K), i.e. 50 in mm-t-s-N."),
|
||||
positive("specific_heat", "Specific heat", D::SpecificHeat)
|
||||
.help("Heat that raises a unit mass by one degree. Steel ≈ 460 J/(kg·K), 4.6e8 in mm-t-s."),
|
||||
json("expansion", "Expansion")
|
||||
.help("Thermal expansion coefficient, one value or one per axis, in 1/K. Steel ≈ 1.2e-5, aluminium ≈ 2.3e-5.")
|
||||
.hint("α or [α1, α2, α3], 1/K"),
|
||||
per_axis("expansion", "Expansion", D::Expansion, None, AXES_123)
|
||||
.help("Thermal expansion coefficient, one value or one per material axis. Steel ≈ 1.2e-5, aluminium ≈ 2.3e-5."),
|
||||
num("reference_temperature", "Reference temperature", D::Temperature).help("The temperature at which there is no thermal strain.").def("0"),
|
||||
],
|
||||
)
|
||||
@@ -371,7 +378,9 @@ pub const COUPLING: &[Field] = &[
|
||||
pub const MASS: &[Field] = &[
|
||||
NAME,
|
||||
TARGET,
|
||||
positive("mass", "Mass", D::Mass).help("A lumped mass at each node of the target.").hint("per node; [mx, my, mz] in JSON"),
|
||||
per_axis("mass", "Mass", D::Mass, Some(0.0), XYZ)
|
||||
.help("A lumped mass at each node of the target: one value, or one per direction X, Y, Z.")
|
||||
.hint("per node"),
|
||||
vector("inertia", "Inertia", D::Inertia, &["I11", "I22", "I33", "I12", "I13", "I23"])
|
||||
.help("Rotary inertia about the reference point: three moments and three products.")
|
||||
.hint("about the node; needs a reference point"),
|
||||
@@ -581,6 +590,24 @@ const FACE_CONDITION: &[Variant] = &[
|
||||
variant("periodic", "Periodic", &[]),
|
||||
];
|
||||
|
||||
const BOX_FACE: Field =
|
||||
choice("", "Box face", &[("x_min", "X min"), ("x_max", "X max"), ("y_min", "Y min"), ("y_max", "Y max"), ("z_min", "Z min"), ("z_max", "Z max")])
|
||||
.help("A face of the domain's box, at the smallest or largest X, Y or Z.")
|
||||
.req();
|
||||
const OBSTACLE_FACE_FIELDS: &[Field] = &[
|
||||
reference("body", "Body", C::Body).help("An obstacle body of the design.").req(),
|
||||
text("face", "Face").help("A persistent face name of the body (e.g. F3); it also selects the face's split pieces.").req(),
|
||||
];
|
||||
const OBSTACLE_FACE: Field = group("face", "Face", OBSTACLE_FACE_FIELDS).help("The obstacle face.").req();
|
||||
const SURFACE_CONDITION: &[Variant] = &[
|
||||
variant("wall", "Wall at rest", &[]),
|
||||
variant(
|
||||
"moving_wall",
|
||||
"Moving wall",
|
||||
&[xyz("velocity", "Velocity", D::Velocity).help("The wall's tangential velocity; the body itself does not move.").req()],
|
||||
),
|
||||
];
|
||||
|
||||
const fn face(key: &'static str, label: &'static str) -> Field {
|
||||
variants(key, label, FACE_CONDITION).hint("wall")
|
||||
}
|
||||
@@ -607,9 +634,16 @@ pub const FLUID_DOMAIN: &[Field] = &[
|
||||
)
|
||||
.help("The condition on each face of the box."),
|
||||
references("obstacles", "Obstacles", C::Body).help("Bodies of the design the flow goes around."),
|
||||
json("surfaces", "Obstacle faces")
|
||||
.help("Conditions on single obstacle faces, as JSON; walls by default.")
|
||||
.hint("[{\"face\", \"condition\"}]; walls by default"),
|
||||
rows(
|
||||
"surfaces",
|
||||
"Obstacle faces",
|
||||
&[
|
||||
OBSTACLE_FACE,
|
||||
variants("condition", "Condition", SURFACE_CONDITION).help("The flow condition on that face.").req().def(r#"{"type": "wall"}"#),
|
||||
],
|
||||
)
|
||||
.help("Conditions on single obstacle faces; every other obstacle face is a wall at rest.")
|
||||
.hint("walls by default"),
|
||||
xyz("initial_velocity", "Initial velocity", D::Velocity).help("The fluid's velocity at the start."),
|
||||
xyz("acceleration", "Acceleration", D::Acceleration).help("A body acceleration on the fluid, such as gravity."),
|
||||
positive("tolerance", "Obstacle tolerance", D::Length)
|
||||
@@ -619,11 +653,10 @@ pub const FLUID_DOMAIN: &[Field] = &[
|
||||
|
||||
pub const FLUID_BOUNDARY: &[Field] = &[
|
||||
reference("domain", "Domain", C::FluidDomain).help("The fluid domain to change.").req(),
|
||||
json("at", "Boundary")
|
||||
.help("A box face (x_min … z_max) or an obstacle face, as JSON.")
|
||||
name_or("at", "Boundary", &BOX_FACE, "Obstacle face", OBSTACLE_FACE_FIELDS)
|
||||
.help("A face of the domain's box, or a named face of an obstacle body.")
|
||||
.req()
|
||||
.def("\"x_min\"")
|
||||
.hint("\"x_min\" … \"z_max\", or {\"body\", \"face\"}"),
|
||||
.def("\"x_min\""),
|
||||
variants("condition", "Condition", FACE_CONDITION).help("The flow condition on that boundary.").req().def(r#"{"type": "velocity"}"#),
|
||||
];
|
||||
|
||||
@@ -661,9 +694,9 @@ const PROCEDURE: Field = variants(
|
||||
flag("line_search", "Line search")
|
||||
.help("Scale each Newton correction to reduce the residual; helps with hard convergence.")
|
||||
.def("false"),
|
||||
json("load_path", "Load path")
|
||||
.help("How the load factor varies through the step, as JSON pairs of step fraction and factor; a linear ramp when empty.")
|
||||
.hint("[[fraction, factor], …]; a ramp when empty"),
|
||||
table("load_path", "Load path", D::None, &["fraction", "factor"], fieldcraft_model::MAX_LOAD_PATH_POINTS)
|
||||
.help("How the load factor varies through the step: rows of step fraction (0 to 1) and load factor; a linear ramp when empty.")
|
||||
.hint("a ramp when empty"),
|
||||
group(
|
||||
"imperfection",
|
||||
"Imperfection",
|
||||
@@ -684,9 +717,18 @@ const PROCEDURE: Field = variants(
|
||||
.help("Cylindrical: displacements only. Spherical: displacements and load factor.")
|
||||
.def("\"cylindrical\""),
|
||||
positive("max_load_factor", "Max load factor", D::None).help("Stop when the load factor reaches this.").def("1"),
|
||||
json("displacement_limit", "Displacement limit")
|
||||
.help("Stop when a node's displacement reaches a value, as JSON.")
|
||||
.hint("{\"node\": 1, \"direction\": 3, \"displacement\": -10}"),
|
||||
group(
|
||||
"displacement_limit",
|
||||
"Displacement limit",
|
||||
&[
|
||||
int("node", "Node", 1, u32::MAX as i64).help("The label of the node watched.").req(),
|
||||
int("direction", "Direction", 1, 3).help("1, 2, 3: its translation along X, Y, Z.").req().def("3"),
|
||||
num("displacement", "Displacement", D::Length)
|
||||
.help("The step stops when that translation reaches this size (its sign is ignored).")
|
||||
.req(),
|
||||
],
|
||||
)
|
||||
.help("Stop when a node's displacement reaches a value; at the max load factor when empty."),
|
||||
],
|
||||
)
|
||||
.help("Follow the load-displacement path through limit points (snap-through), with the load factor as an unknown."),
|
||||
@@ -1127,21 +1169,69 @@ pub const CELLS: &[Field] = &[
|
||||
HEX_SIZE,
|
||||
HEX_ORDER,
|
||||
TOLERANCE,
|
||||
choice("transition", "Hex–tet joint", &[("pyramid", "Pyramids (conforming)"), ("tie", "Tie")])
|
||||
.help("How a tet cell meets the hex cells across its partitions: a layer of pyramids on the hex faces, one conforming mesh (linear elements), or a tie between the two meshes.")
|
||||
.def("\"pyramid\""),
|
||||
];
|
||||
|
||||
pub const HEX_PROJECT: &[Field] = &[
|
||||
reference("body", "Body", C::Body).help("The body to mesh with a projected block topology.").req(),
|
||||
HEX_SIZE,
|
||||
choice(
|
||||
"template",
|
||||
"Template",
|
||||
&[("grid", "Block grid (carve)"), ("sphere", "Sphere (7 blocks)"), ("butterfly", "Butterfly (5-block disc, extruded)"), ("blocks", "Blocks (given vertices)")],
|
||||
)
|
||||
.help("The block topology projected onto the body: a grid of blocks you may carve, the seven-block sphere, the five-block butterfly for round sections, or blocks you list.")
|
||||
.def("\"grid\""),
|
||||
xyz("center", "Centre", POINT).help("Sphere or butterfly centre.").hint("from the body's bounding box"),
|
||||
positive("radius", "Radius", D::Millimetre).help("Sphere or butterfly radius.").hint("from the body"),
|
||||
xyz("axis", "Axis", D::None).help("Butterfly axis direction.").hint("0, 0, 1"),
|
||||
json("levels", "Levels")
|
||||
.help("Sphere and butterfly levels; for a butterfly, distances along the axis from the centre, as JSON.")
|
||||
.hint("[z0, z1, ...]; from the bounding box"),
|
||||
range("inset", "Inset", D::None, 0.05, 0.95).help("Inner square or cube corner at this fraction of the radius.").def("0.55"),
|
||||
json("grid", "Grid").help("The block grid: planes per axis or a cell count, and blocks to carve away, as JSON.").hint(r#"{"cells": [ni, nj, nk], "carve": [{"i": [a, b], "j": [c, d], "k": [e, f]}]}"#),
|
||||
json("blocks", "Blocks").help("Template Blocks: vertices and hex8 blocks, as JSON.").hint(r#"{"vertices": [[x, y, z], ...], "blocks": [[v1..v8], ...]}"#),
|
||||
json("operations", "Operations").help("O-grids, extrusions, connections, removals and merges applied in order, as JSON.").hint(r#"[{"op": "o_grid", "block": 0, "open": ["+z"]}]"#),
|
||||
json("faces", "Face targets").help("Block faces sent to chosen B-rep faces; other outer faces go to the nearest surfaces.").hint(r#"[{"block": 0, "face": "+x", "to": ["F1:+x"]}]"#),
|
||||
json("divisions", "Divisions").help("Element counts per block direction.").hint(r#"[{"block": 0, "direction": 1, "count": 8}]"#),
|
||||
json("fixed", "Fixed faces")
|
||||
.help("Faces of the body another body's mesh already covers: its element faces there are kept as they are and this mesh meets them node to node.")
|
||||
.hint(r#"["F3:partition:P:b"]"#),
|
||||
choice("smoothing", "Smoothing", &[("elliptic", "Elliptic"), ("elliptic-controlled", "Elliptic, controlled (Thomas–Middlecoff)"), ("laplace", "Laplace"), ("none", "None")])
|
||||
.help("How interior nodes are smoothed after projection, with the boundary held on the geometry.")
|
||||
.def("\"elliptic\""),
|
||||
int("iterations", "Iterations", 1, 100_000).help("Smoothing iterations.").def("100"),
|
||||
range("min_quality", "Minimum quality", D::None, 0.0, 1.0).help("Smallest scaled Jacobian accepted.").def("0.2"),
|
||||
range("max_arc_angle", "Max arc angle", D::Degree, 0.0, 90.0)
|
||||
.help("The most a curved face may turn across one element; the divisions grow to keep it. 0 for no limit.")
|
||||
.def("30"),
|
||||
range("merge", "Merge distance", D::Millimetre, 0.0, 1.0e9)
|
||||
.help("Join this mesh's nodes with other bodies' meshes within this distance; 0 never.")
|
||||
.hint("1e-6 of the diagonal"),
|
||||
flag("preview", "Preview only").help("Return the block topology without meshing."),
|
||||
TOLERANCE,
|
||||
];
|
||||
|
||||
pub const CLEAR_MESH: &[Field] = &[references("bodies", "Bodies", C::Body)
|
||||
.help("The bodies whose meshes go. When none is ticked: the meshes of bodies the open design no longer has because its partitions changed (model.check lists them).")
|
||||
.hint("the bodies partitions removed")];
|
||||
|
||||
const MIRROR_DATUM: Field = reference("", "Datum plane", C::DatumPlane).help("A datum plane of the model.");
|
||||
const MIRROR_POINT_NORMAL: &[Field] = &[
|
||||
xyz("point", "Point", D::Length).help("A point on the plane.").req(),
|
||||
xyz("normal", "Normal", D::None).help("The plane's normal; its length does not matter.").req(),
|
||||
];
|
||||
|
||||
pub const MIRROR: &[Field] = &[
|
||||
json("plane", "Plane")
|
||||
.help("The mirror plane: a datum plane's name, or a point and normal as JSON.")
|
||||
.req()
|
||||
.hint(r#""Datum" or {"point": [x, y, z], "normal": [nx, ny, nz]}"#),
|
||||
name_or("plane", "Plane", &MIRROR_DATUM, "Point and normal", MIRROR_POINT_NORMAL)
|
||||
.help("The mirror plane: a datum plane, or a point on it and its normal.")
|
||||
.req(),
|
||||
references("bodies", "Bodies", C::Body).help("The meshed bodies to mirror; every meshed body when empty.").hint("every meshed body"),
|
||||
json("as", "Images")
|
||||
.help("Names for the mirrored bodies, as JSON.")
|
||||
.hint(r#"{"body": "image body"}; default: the design's body at the reflected place"#),
|
||||
reference_text("as", "Images", C::Body)
|
||||
.help("The body each mirrored body becomes; when empty, the design's body at the reflected place, else <body>/mirror.")
|
||||
.hint("default"),
|
||||
];
|
||||
|
||||
pub const SYMMETRY: &[Field] = &[
|
||||
@@ -1156,7 +1246,9 @@ pub const HEX_OCTREE: &[Field] = &[
|
||||
SURFACE,
|
||||
HEX_SIZE,
|
||||
TOLERANCE,
|
||||
int("max_depth", "Refinement levels", 0, 5).help("How many times the octree may refine near the surface.").hint("3"),
|
||||
int("max_depth", "Refinement levels", 0, 5)
|
||||
.help("The most times the octree may refine near thin walls, narrow faces and curves; the levels the part needs are found before meshing.")
|
||||
.hint("4"),
|
||||
int("max_elements", "Max elements", 1, 10_000_000).help("Cap on the elements created."),
|
||||
int("attempts", "Attempts", 1, 8).help("How many times to retry with adjusted settings.").hint("4"),
|
||||
int("max_sweeps", "Optimiser sweeps", 0, 10_000).help("The most smoothing sweeps.").hint("300"),
|
||||
@@ -1165,6 +1257,37 @@ pub const HEX_OCTREE: &[Field] = &[
|
||||
FEATURE_ANGLE,
|
||||
];
|
||||
|
||||
/// A face by body and persistent name, a row of a face list.
|
||||
const FACE_ROW: &[Field] = &[
|
||||
reference("body", "Body", C::Body).help("A body of the open design.").req(),
|
||||
text("face", "Face").help("A persistent face name of the body (e.g. F3).").req().hint("persistent face name"),
|
||||
];
|
||||
const fn face_rows(key: &'static str, label: &'static str) -> Field {
|
||||
rows(key, label, FACE_ROW)
|
||||
}
|
||||
|
||||
pub const HEX_EXTRUDE: &[Field] = &[
|
||||
face_rows("source", "Source")
|
||||
.help("The section: faces already meshed (by this body or a neighbour, whose nodes are shared), or one planar face meshed here. Pick it, then Use as source.")
|
||||
.hint("pick the source face"),
|
||||
reference("source_set", "Source set", C::Set).help("Instead of Source: a set of quadrilaterals (a 2D mesh, or hex element faces)."),
|
||||
face_rows("to_surface", "Target surface")
|
||||
.help("The faces the last layer lies on. Pick them, then Use as target surface.")
|
||||
.req()
|
||||
.hint("pick the target surface"),
|
||||
face_rows("sides", "Side faces").help("The faces the section's sides follow (they need not be straight); straight paths when empty.").hint("straight sides"),
|
||||
xyz("direction", "Direction", D::None).help("The direction every node moves along; along the section's normal toward the target when empty.").hint("the section's normal"),
|
||||
flag("normal", "Along the normal").help("Each node along the section's normal toward the target (the default when no direction is given)."),
|
||||
int("layers", "Layers", 1, 10_000).help("The number of element layers; from Size when empty.").hint("from the size"),
|
||||
positive("size", "Size", D::Length).help("Layer thickness, and the element size of a section meshed here.").hint("1/25 of the body"),
|
||||
range("bias", "Bias", D::None, 0.001, 1000.0).help("The last layer's thickness over the first's: below 1 grades toward the target, above 1 toward the source.").def("1"),
|
||||
reference("body", "Body", C::Body).help("The body the elements fill; its mesh is replaced.").hint("the source face's body"),
|
||||
TOLERANCE,
|
||||
];
|
||||
|
||||
/// A block's edge directions, from corner 1.
|
||||
const BLOCK_DIRECTIONS: &[&str] = &["1-2", "1-4", "1-5"];
|
||||
|
||||
pub const BLOCK: &[Field] = &[
|
||||
text("body", "Body").help("Name of the new block body.").req().def("\"Block\""),
|
||||
choice(
|
||||
@@ -1186,8 +1309,12 @@ pub const BLOCK: &[Field] = &[
|
||||
.req()
|
||||
.def("\"hex8\""),
|
||||
points("corners", "Corners", D::Length, 8).help("The eight corner points (four for 2D, at z = 0).").req(),
|
||||
json("divisions", "Divisions").help("Elements along each edge direction, as JSON.").req().hint("[n12, n14, n15] (2D: [n12, n14])"),
|
||||
json("bias", "Bias").help("Element size ratio along each direction, as JSON; 1 is uniform.").hint("[1, 1, 1]"),
|
||||
tuple("divisions", "Divisions", BLOCK_DIRECTIONS, 2, true)
|
||||
.help("Elements along each edge direction: corner 1 to 2, 1 to 4 and 1 to 5; a 2D block takes the first two.")
|
||||
.req(),
|
||||
tuple("bias", "Bias", BLOCK_DIRECTIONS, 2, false)
|
||||
.help("Last over first element size along each direction; 1 is uniform. A 2D block takes the first two.")
|
||||
.hint("uniform"),
|
||||
json("edges", "Curved and named edges").help("Edge curves and names, as JSON."),
|
||||
json("faces", "Face names").help("Names of the block's faces, as JSON.").hint("{\"1\": \"bottom\"}"),
|
||||
json("vertices", "Corner names").help("Names of the block's corners, as JSON."),
|
||||
@@ -1214,6 +1341,25 @@ pub const QUALITY: &[Field] = &[
|
||||
int("bins", "Histogram bins", 1, 1000).help("Number of bars in the quality histogram.").def("10"),
|
||||
];
|
||||
|
||||
pub const QUALITY_MARK: &[Field] = &[
|
||||
labels("elements", "Elements", LabelOf::Element).help("The elements to check; all when empty.").hint("all"),
|
||||
group(
|
||||
"limits",
|
||||
"Limits",
|
||||
&[
|
||||
num("scaled_jacobian", "Scaled Jacobian at least", D::None)
|
||||
.help("Elements with a lower scaled Jacobian are marked (1 is a perfect element).")
|
||||
.hint("0.2"),
|
||||
num("min_dihedral", "Min dihedral at least", D::Degree).help("Tetrahedra with a smaller dihedral angle are marked.").hint("10"),
|
||||
num("max_dihedral", "Max dihedral at most", D::Degree).help("Tetrahedra with a larger dihedral angle are marked.").hint("165"),
|
||||
num("aspect_ratio", "Aspect ratio at most", D::None).help("Elements with a larger aspect ratio are marked.").hint("10"),
|
||||
num("min_angle", "Min angle at least", D::Degree).help("Shells with a smaller corner angle are marked.").hint("15"),
|
||||
num("max_angle", "Max angle at most", D::Degree).help("Shells with a larger corner angle are marked.").hint("165"),
|
||||
],
|
||||
)
|
||||
.help("Elements worse than a limit are marked; a limit left empty keeps its default."),
|
||||
];
|
||||
|
||||
pub const SURFACE_CHECK: &[Field] = &[
|
||||
reference("body", "Body", C::Body).help("The body whose surface to check.").req().hint("or an imported surface"),
|
||||
SURFACE,
|
||||
@@ -1243,6 +1389,7 @@ pub const ELEMENT_ADD: &[Field] = &[
|
||||
("tet4", "tet4"),
|
||||
("tet10", "tet10"),
|
||||
("wedge6", "wedge6"),
|
||||
("pyramid5", "pyramid5"),
|
||||
("hex8", "hex8"),
|
||||
("hex20", "hex20"),
|
||||
],
|
||||
@@ -1270,9 +1417,18 @@ pub const NODE_RELAX: &[Field] = &[
|
||||
int("around_element", "Around element", 1, 99_999_999)
|
||||
.help("Instead of a centre: a sphere round this element (radius default twice its longest edge).")
|
||||
.hint("none"),
|
||||
face_rows("faces", "Faces")
|
||||
.help("Instead of a sphere: every node on these CAD faces and the elements touching them, nodes sliding on the faces. Shift-click faces in the view, then Use selection.")
|
||||
.hint("Shift-click faces"),
|
||||
labels("nodes", "Nodes", LabelOf::Node).help("Instead of a sphere: these nodes.").hint("none"),
|
||||
labels("elements", "Elements", LabelOf::Element).help("Instead of a sphere: the nodes of these elements.").hint("none"),
|
||||
references("bodies", "Bodies", C::Body).help("Only the nodes of these bodies.").hint("every body"),
|
||||
int("iterations", "Iterations", 1, 100).help("Sweeps over the nodes.").def("5"),
|
||||
choice("mode", "Mode", &[("guarded", "Guarded (local)"), ("strong", "Strong (optimise the region)")])
|
||||
.help("Guarded: each move keeps the worst element around the node from getting worse. Strong: move the nodes of the bad elements and morph the mesh round them, nodes on CAD edges held.")
|
||||
.def("\"guarded\""),
|
||||
range("below", "Below", D::None, 0.0, 1.0).help("Strong: the elements below this scaled Jacobian are moved.").hint("0.7"),
|
||||
positive("reach", "Reach", D::Length).help("Strong: nodes this close to a moved node follow it, blended to zero at the reach.").hint("three element lengths"),
|
||||
int("iterations", "Iterations", 1, 200).help("Guarded: sweeps over the nodes (1 to 100). Strong: morph rounds.").hint("5 (strong: 20)"),
|
||||
range("strength", "Strength", D::None, 0.0, 1.0).help("How far each node moves toward its relaxed position, 0 to 1.").def("1"),
|
||||
choice("falloff", "Falloff", &[("smooth", "Smooth"), ("linear", "Linear"), ("none", "None")])
|
||||
.help("How the strength falls from the centre to the radius, so the edit blends into the mesh.")
|
||||
@@ -1324,6 +1480,37 @@ pub const SEED_EDGE: &[Field] = &[
|
||||
|
||||
pub const SEED_CLEAR: &[Field] = &[json("edges", "Edges").help("The edges to clear, as JSON; every seed when empty.").hint("every seed")];
|
||||
|
||||
pub const SIZE_SET: &[Field] = &[
|
||||
NAME,
|
||||
positive("size", "Element size", D::Length).help("The element size in the region.").req(),
|
||||
face_rows("faces", "Faces").help("Faces to refine. Pick them, then Use as faces.").hint("or bodies, a box or a sphere"),
|
||||
references("bodies", "Bodies", C::Body).help("Whole bodies meshed at this size.").hint("or faces, a box or a sphere"),
|
||||
group(
|
||||
"box",
|
||||
"Box",
|
||||
&[
|
||||
xyz("min", "Box min", D::Length).help("The box corner with the smallest coordinates.").req(),
|
||||
xyz("max", "Box max", D::Length).help("The box corner with the largest coordinates.").req(),
|
||||
],
|
||||
)
|
||||
.help("An axis-aligned box to refine."),
|
||||
group(
|
||||
"sphere",
|
||||
"Sphere",
|
||||
&[
|
||||
xyz("center", "Centre", D::Length).help("The sphere's centre; click the model to place it.").req(),
|
||||
positive("radius", "Radius", D::Length).help("The sphere's radius.").req(),
|
||||
],
|
||||
)
|
||||
.help("A sphere to refine."),
|
||||
];
|
||||
|
||||
pub const SIZE_GROWTH: &[Field] =
|
||||
&[range("rate", "Growth rate", D::None, 0.05, 2.0).help("The largest change of element size per unit length away from a local size.").def("0.3")];
|
||||
|
||||
pub const SIZE_CLEAR: &[Field] =
|
||||
&[references("names", "Local sizes", C::SizeControl).help("The size controls to remove; every one when empty.").hint("every one")];
|
||||
|
||||
pub const DELAUNAY_POINTS: &[Field] = &[points("points", "Points", D::Length, 20_000).help("The points to triangulate.").req()];
|
||||
|
||||
pub const TAG: &[Field] = &[
|
||||
@@ -1340,8 +1527,8 @@ pub const TAG: &[Field] = &[
|
||||
.help("The geometry the mesh entities are tagged with.")
|
||||
.req(),
|
||||
labels("elements", "Elements", LabelOf::Element).help("Element labels."),
|
||||
json("faces", "Element faces").help("Element faces, as JSON."),
|
||||
json("edges", "Element edges").help("Element edges, as JSON."),
|
||||
sides("faces", "Element faces", Side::Face).help("Element faces as element:face: 1-20:3, 25:1.").hint("element:face"),
|
||||
sides("edges", "Element edges", Side::Edge).help("Element edges as element:edge: 1-20:3, 25:1.").hint("element:edge"),
|
||||
labels("nodes", "Nodes", LabelOf::Node).help("Node labels."),
|
||||
];
|
||||
|
||||
@@ -1565,6 +1752,87 @@ pub const PARTITION: &[Field] = &[
|
||||
references("bodies", "Bodies", C::Body).help("The bodies to divide; every body the plane cuts when empty.").hint("every body the plane divides"),
|
||||
];
|
||||
pub const PARTITION_DELETE: &[Field] = existing!("Partition", C::Partition);
|
||||
pub const FACES_MERGE: &[Field] = &[
|
||||
NAME,
|
||||
face_rows("faces", "Faces")
|
||||
.help("Two or more adjacent faces of one body, meshed as one face. Shift-click faces in the view, then Use selection.")
|
||||
.req()
|
||||
.hint("Shift-click faces"),
|
||||
];
|
||||
pub const FACE_ABSORB: &[Field] = &[
|
||||
text("name", "Name").help("The absorption's name (with From check: the prefix of each one's, Name-1, Name-2, …).").hint("Absorb"),
|
||||
group("face", "Face", FACE_ROW).help("The tiny or sliver face to absorb; leave empty with From check."),
|
||||
text("into", "Into").help("The neighbour face that absorbs it, by persistent name.").hint("the neighbour sharing the longest edges"),
|
||||
flag("from_check", "From check").help("Absorb every small and sliver face the last Check Geometry found, each into its best neighbour."),
|
||||
];
|
||||
pub const STITCH: &[Field] = &[
|
||||
NAME,
|
||||
text("surface", "Surface").help("The imported surface to stitch, by name.").req(),
|
||||
positive("tolerance", "Tolerance", D::Length)
|
||||
.help("Gaps narrower than this close: open points this close become one, and a point this close to another open edge splits it.")
|
||||
.req(),
|
||||
];
|
||||
pub const STITCH_DELETE: &[Field] = existing!("Stitch", C::Stitch);
|
||||
pub const FACE_SPLIT: &[Field] = &[
|
||||
NAME,
|
||||
group("face", "Face", FACE_ROW).help("The face to split.").req(),
|
||||
reference("datum", "Datum plane", C::DatumPlane).help("The plane the face is split along.").req(),
|
||||
];
|
||||
pub const FACE_SPLIT_DELETE: &[Field] = existing!("Face split", C::FaceSplit);
|
||||
pub const FACES_MERGE_DELETE: &[Field] = existing!("Face merge", C::FaceMerge);
|
||||
pub const DEFEATURE: &[Field] = &[
|
||||
NAME,
|
||||
text("features", "Features")
|
||||
.help("The feature ids to take out for meshing (3 or F3), as Propose Defeaturing and the feature graph list them; separate them with commas.")
|
||||
.req(),
|
||||
];
|
||||
pub const DEFEATURE_DELETE: &[Field] = existing!("Defeature", C::Defeature);
|
||||
pub const DEFEATURE_PROPOSE: &[Field] = &[
|
||||
reference("body", "Body", C::Body).help("The body whose holes, fillets, chamfers and bosses to list.").req(),
|
||||
group(
|
||||
"below",
|
||||
"Propose below",
|
||||
&[
|
||||
positive("hole_diameter", "Hole diameter", D::Millimetre).help("Propose holes narrower than this."),
|
||||
positive("fillet_radius", "Fillet radius", D::Millimetre).help("Propose fillets with a smaller radius."),
|
||||
positive("chamfer", "Chamfer", D::Millimetre).help("Propose chamfers smaller than this."),
|
||||
positive("boss_height", "Boss height", D::Millimetre).help("Propose bosses lower than this."),
|
||||
],
|
||||
)
|
||||
.help("Size thresholds: a feature below its threshold is proposed."),
|
||||
flag("from_check", "From check").help("Also propose the features with a face the last Check Geometry found."),
|
||||
text("name", "Defeature").help("The defeature the list's on/off switches edit.").hint("Defeature"),
|
||||
];
|
||||
pub const MESH_PLAN: &[Field] = &[
|
||||
reference("body", "Body", C::Body).help("The body of the design to plan (as opened, not a partition cell).").req(),
|
||||
text("name", "Name").help("The name the plan is saved under.").hint("Plan-<body>"),
|
||||
positive("size", "Element size", D::Millimetre).help("The largest element size; thin walls make it smaller.").hint("1/25 of the diagonal"),
|
||||
int("layers", "Layers through walls", 1, 10).help("Elements through the thinnest wall the features give (a shell, a plate).").hint("2"),
|
||||
int("max_elements", "Most elements", 1, 5_000_000).help("The size grows past the walls' when more elements would be needed.").hint("300000"),
|
||||
];
|
||||
pub const MESH_PLAN_APPLY: &[Field] = &[
|
||||
text("plan", "Plan").help("The name of a saved mesh plan (mesh.plan).").req(),
|
||||
flag("symmetry", "Use symmetry")
|
||||
.help("Mesh one cell of a body that is its own mirror image across planes through its middle, and mirror it.")
|
||||
.def("true"),
|
||||
flag("corners", "Copy corners")
|
||||
.help("With symmetry: when the body's two ends along another axis are alike (an enclosure's corners), mesh one end and mirror it onto the other, the middle meshed to fit both.")
|
||||
.def("true"),
|
||||
text("name", "Save as").help("The name a plan given as an object is saved under.").hint("Plan-<body>"),
|
||||
];
|
||||
pub const FEATURES: &[Field] = &[reference("body", "Body", C::Body).help("Only this body's features.").hint("every body")];
|
||||
pub const GEOMETRY_CHECK: &[Field] = &[
|
||||
reference("body", "Body", C::Body).help("The body to check; every body when empty.").hint("every body"),
|
||||
positive("size", "Element size", D::Millimetre)
|
||||
.help("The element size you intend to mesh with; features are small against it.")
|
||||
.hint("1/20 of the diagonal"),
|
||||
range("short_edge", "Short edge", D::None, 0.0, 10.0).help("Edges shorter than this fraction of the size are reported.").def("0.3"),
|
||||
range("small_face", "Small face", D::None, 0.0, 10.0).help("Faces with area below this fraction of size² are reported.").def("0.1"),
|
||||
range("sliver", "Sliver width", D::None, 0.0, 10.0).help("Faces narrower than this fraction of the size are reported.").def("0.3"),
|
||||
range("gap", "Gap", D::None, 0.0, 10.0).help("Parallel faces closer than this fraction of the size, but not touching, are reported.").def("0.3"),
|
||||
range("parallel_angle", "Parallel within", D::Degree, 0.0, 60.0).help("Faces this close to parallel count for the gap.").def("10"),
|
||||
flag("mark", "Mark findings").help("Mark what is found in the viewport until the design changes.").def("true"),
|
||||
];
|
||||
pub const TESSELLATE: &[Field] = &[reference("body", "Body", C::Body).help("The body to tessellate.").req(), TOLERANCE];
|
||||
pub const IMPORT_MESH: &[Field] = &[path("path", "3MF file", false).help("A 3MF surface mesh with named faces.").req()];
|
||||
pub const FILE_OPEN: &[Field] = &[path("path", "Model", false).help("The FieldCraft model file to open.").req()];
|
||||
@@ -1634,6 +1902,12 @@ pub const DECK: &[Field] = &[
|
||||
references("steps", "Steps", C::Step).help("The steps to write; all when empty.").hint("all"),
|
||||
];
|
||||
|
||||
pub const EXPORT_STL: &[Field] = &[
|
||||
path("path", "File", true).help("Where to write the STL file.").req(),
|
||||
reference("set", "Set", C::Set).help("Write only this set's element faces; the whole outer surface when empty.").hint("the outer surface"),
|
||||
choice("format", "Format", &[("binary", "Binary"), ("ascii", "ASCII")]).help("Binary is compact; ASCII is readable text.").def("\"binary\""),
|
||||
];
|
||||
|
||||
// ---- Solving ----
|
||||
|
||||
const STEP: Field = reference("step", "Step", C::Step).help("The step to solve; the only one when empty.").hint("the only one");
|
||||
@@ -1762,6 +2036,29 @@ pub const EXPORT_CSV: &[Field] = &[
|
||||
int("mode", "Mode", 1, 1_000_000).help("Probe: the mode to read, for frequency and buckling steps."),
|
||||
int("frame", "Frame", 0, 1_000_000_000).help("Probe: the frame to read, for explicit and transient steps."),
|
||||
];
|
||||
pub const RESULTS_XY: &[Field] = &[
|
||||
SOLVED,
|
||||
json("y", "Quantity")
|
||||
.help("What to plot: a nodal field at a node, an explicit energy or history, a history node's quantity, or \"frequency\".")
|
||||
.hint(r#"{"field": "displacement_z", "node": 21}"#)
|
||||
.req(),
|
||||
choice("x", "Against", &[("auto", "The step's own axis"), ("load_factor", "Load factor"), ("time", "Time"), ("mode", "Mode")])
|
||||
.help("The x axis: the load factor (nonlinear static), time (explicit, heat) or mode number (frequency).")
|
||||
.def("\"auto\""),
|
||||
int("every", "Keep one in", 1, 1_000_000_000).help("Keep one point in this many; by default enough to fit 100000 points."),
|
||||
path("path", "CSV file", true).help("Also write the series as a two-column CSV."),
|
||||
];
|
||||
pub const EXPORT_VTU: &[Field] = &[
|
||||
path("path", "File", true).help("A .vtu file for one state, or a .pvd collection for every frame or mode.").req(),
|
||||
SOLVED,
|
||||
int("mode", "Mode", 1, 1_000_000).help("Frequency steps: the mode to write; every mode in a .pvd when empty.").hint("all (.pvd) or 1"),
|
||||
choice("frames", "Frames", &[("all", "Every frame"), ("last", "The last frame")])
|
||||
.help("A .pvd series: every frame or only the last.")
|
||||
.def("\"all\""),
|
||||
choice("encoding", "Encoding", &[("ascii", "Text"), ("binary", "Binary (base64)")])
|
||||
.help("Text is readable; binary is smaller and exact.")
|
||||
.def("\"ascii\""),
|
||||
];
|
||||
pub const REACTIONS: &[Field] = &[
|
||||
SOLVED,
|
||||
references("sets", "Sets", C::Set)
|
||||
@@ -1933,6 +2230,10 @@ mod tests {
|
||||
}
|
||||
match f.kind {
|
||||
Kind::Group(g) | Kind::Rows { fields: g, .. } | Kind::Either(g) => walk(id, g, missing),
|
||||
Kind::NameOr { name, fields, .. } => {
|
||||
walk(id, std::slice::from_ref(name), missing);
|
||||
walk(id, fields, missing);
|
||||
}
|
||||
Kind::Variants { variants, .. } => variants.iter().for_each(|v| walk(id, v.fields, missing)),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,16 @@ pub const BODIES: CommandSpec = CommandSpec {
|
||||
run: bodies,
|
||||
};
|
||||
|
||||
pub const FEATURES: CommandSpec = CommandSpec {
|
||||
id: "geometry.features",
|
||||
label: "Feature Graph",
|
||||
params: r#"{"body"?: "Base" (default: every body)} — the features of the SolidCraft timeline that built each body of the open design, in order (read-only): each feature's `id` (`F<id>` in persistent names), `name`, `type` (extrude, box, cylinder, hole, shell, fillet, combine, pattern, mirror, split, …), `operation` (new_body, join, cut, intersect), `values` (its numeric inputs evaluated: mm, radians, counts), `placement` (a primitive's corner, base or centre), `direction` (an extrude's along the sketch normal, a cylinder's axis, a hole's drilling direction), `two_sided`, `sketch` (plane origin, axes and normal, and the profiles used: lines, arcs and circles in sketch coordinates, holes, curve ids), `transforms` (pattern and mirror copies, 4×4 row-major), `targets` and `tools`, the stored `definition`, the persistent `faces` and `edges` of the body it made, and `error` when it failed to rebuild. A partition cell is described by the body it was cut from"#,
|
||||
form: Some(super::forms::FEATURES),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Design],
|
||||
run: features,
|
||||
};
|
||||
|
||||
pub const CENTRE: CommandSpec = CommandSpec {
|
||||
id: "geometry.centre",
|
||||
label: "Centre of Selection",
|
||||
@@ -43,6 +53,16 @@ pub const CENTRE: CommandSpec = CommandSpec {
|
||||
run: centre,
|
||||
};
|
||||
|
||||
pub const CHECK: CommandSpec = CommandSpec {
|
||||
id: "geometry.check",
|
||||
label: "Check Geometry",
|
||||
params: r#"{"body"?: "Body1" (default: every body of the open design), "size"?: mm (the target element size; default a twentieth of the smallest checked body's bounding-box diagonal), "short_edge": 0.3, "small_face": 0.1, "sliver": 0.3, "gap": 0.3 (fractions of the size, 0..=10), "parallel_angle": 10 (degrees, 0..=60)} — before meshing, finds what is small against the size (read-only): `short_edge` edges shorter than short_edge × size, `small_face` faces with area below small_face × size², `sliver_face` faces narrower (2 × area / perimeter) than sliver × size, and `near_faces` pairs of faces within parallel_angle of parallel that are closer than gap × size without touching (a gap between bodies or cells, an overlap, or a wall thinner than the gap; faces within twice the tessellation tolerance touch). Replies `size`, `counts` per kind, `summary`, and `findings` (by kind, worst first, at most 1000; `truncated` says when more were found), each with `kind`, `body`, `edge`, `faces` (persistent names; near faces: the second on `other_body`), `value` (mm, mm²), `limit` and `at` (a point on it). `mark` (default true): the viewport and pictures mark the findings in the marker colour (their faces, short edges as lines, and a dot at each `at`) until the design changes or a check with `mark: false` or nothing found"#,
|
||||
form: Some(super::forms::GEOMETRY_CHECK),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Design],
|
||||
run: check,
|
||||
};
|
||||
|
||||
pub const TESSELLATE: CommandSpec = CommandSpec {
|
||||
id: "geometry.tessellate",
|
||||
label: "Tessellate Body",
|
||||
@@ -170,6 +190,16 @@ fn bodies(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
Ok(Value::Array(out))
|
||||
}
|
||||
|
||||
fn features(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let body = match p.get("body") {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(v) => Some(v.as_str().ok_or_else(|| EngineError::param("body", ProblemKind::NotAName))?),
|
||||
};
|
||||
let design = s.design.as_deref().ok_or_else(|| EngineError::Params("open a design first".into()))?;
|
||||
let graph = design.features(body)?;
|
||||
Ok(json!({"bodies": graph}))
|
||||
}
|
||||
|
||||
/// Most geometry items and nodes `geometry.centre` takes.
|
||||
const MAX_CENTRE_ITEMS: usize = 10_000;
|
||||
|
||||
@@ -363,6 +393,105 @@ pub(crate) fn body_mesh<'a>(s: &'a Session, p: &Value) -> Result<(&'a Shape, Sur
|
||||
Ok((shape, shape.tessellate(tolerance)?))
|
||||
}
|
||||
|
||||
/// Most findings `geometry.check` lists.
|
||||
const MAX_FINDINGS: usize = 1000;
|
||||
|
||||
fn check(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
use fieldcraft_geometry::check::{self, CheckOptions, Kind};
|
||||
let shapes: Vec<&Shape> = match p.get("body") {
|
||||
Some(_) => vec![body(s, p)?],
|
||||
None => s.design.as_deref().map(|d| d.bodies.iter().collect()).unwrap_or_default(),
|
||||
};
|
||||
if shapes.is_empty() {
|
||||
return Err(EngineError::Params("the design has no bodies to check".into()));
|
||||
}
|
||||
let size = match p.get("size") {
|
||||
Some(v) => v.as_f64().filter(|x| *x > 0.0 && x.is_finite()).ok_or_else(|| EngineError::param("size", ProblemKind::NotPositive))?,
|
||||
None => {
|
||||
let mut smallest = f64::INFINITY;
|
||||
for b in &shapes {
|
||||
smallest = smallest.min(b.size()?);
|
||||
}
|
||||
smallest / 20.0
|
||||
}
|
||||
};
|
||||
let fraction = |key: &str, default: f64| super::mesh::number_in(p, key, default, 0.0..=10.0);
|
||||
let defaults = CheckOptions::new(size);
|
||||
let options = CheckOptions {
|
||||
size,
|
||||
short_edge: fraction("short_edge", defaults.short_edge)?,
|
||||
small_face: fraction("small_face", defaults.small_face)?,
|
||||
sliver: fraction("sliver", defaults.sliver)?,
|
||||
gap: fraction("gap", defaults.gap)?,
|
||||
parallel_angle: super::mesh::number_in(p, "parallel_angle", defaults.parallel_angle.to_degrees(), 0.0..=60.0)?.to_radians(),
|
||||
};
|
||||
let mark = match p.get("mark") {
|
||||
None => true,
|
||||
Some(v) => v.as_bool().ok_or_else(|| EngineError::Params("`mark` must be true or false".into()))?,
|
||||
};
|
||||
let findings = check::check(&shapes, &options)?;
|
||||
s.geometry_marks = s.design.as_ref().filter(|_| mark && !findings.is_empty()).map(|design| {
|
||||
let marks = findings
|
||||
.iter()
|
||||
.map(|f| {
|
||||
// A near pair's second face lies on the other body.
|
||||
let on = |k: usize| if k == 1 { f.other_body.clone().unwrap_or_else(|| f.body.clone()) } else { f.body.clone() };
|
||||
// A short edge is drawn itself: its faces can be large.
|
||||
let faces = if f.kind == Kind::ShortEdge {
|
||||
Vec::new()
|
||||
} else if f.kind == Kind::NearFaces {
|
||||
f.face_indices.iter().enumerate().map(|(k, &i)| (on(k), i)).collect()
|
||||
} else {
|
||||
f.face_indices.iter().map(|&i| (f.body.clone(), i)).collect()
|
||||
};
|
||||
let small =
|
||||
matches!(f.kind, Kind::SmallFace | Kind::SliverFace).then(|| f.faces.first().map(|n| (f.body.clone(), n.clone()))).flatten();
|
||||
crate::view::GeometryMark { faces, path: f.path.clone(), at: f.at, small }
|
||||
})
|
||||
.collect();
|
||||
Arc::new(crate::view::GeometryMarks { design: Arc::downgrade(design), marks })
|
||||
});
|
||||
let counts: serde_json::Map<String, Value> =
|
||||
Kind::ALL.iter().map(|k| (k.name().to_string(), json!(findings.iter().filter(|f| f.kind == *k).count()))).collect();
|
||||
// The size to three decimals, without trailing zeros.
|
||||
let shown = format!("{size:.3}").trim_end_matches('0').trim_end_matches('.').to_string();
|
||||
let summary = if findings.is_empty() {
|
||||
format!("nothing small against size {shown}")
|
||||
} else {
|
||||
let parts: Vec<String> = Kind::ALL
|
||||
.iter()
|
||||
.filter_map(|k| {
|
||||
let n = findings.iter().filter(|f| f.kind == *k).count();
|
||||
(n > 0).then(|| format!("{n} {}", k.name().replace('_', " ")))
|
||||
})
|
||||
.collect();
|
||||
format!("{} against size {shown}", parts.join(", "))
|
||||
};
|
||||
let listed: Vec<Value> = findings
|
||||
.iter()
|
||||
.take(MAX_FINDINGS)
|
||||
.map(|f| {
|
||||
json!({
|
||||
"kind": f.kind.name(),
|
||||
"body": f.body,
|
||||
"edge": f.edge,
|
||||
"faces": f.faces,
|
||||
"other_body": f.other_body,
|
||||
"value": f.value,
|
||||
"limit": f.limit,
|
||||
"at": f.at,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
Ok(json!({
|
||||
"size": size,
|
||||
"counts": counts,
|
||||
"summary": summary,
|
||||
"findings": listed,
|
||||
"truncated": findings.len() > MAX_FINDINGS,
|
||||
}))
|
||||
}
|
||||
|
||||
fn tessellate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let (shape, mesh) = body_mesh(s, p)?;
|
||||
let mut per_face = vec![0usize; shape.faces().len()];
|
||||
@@ -391,16 +520,157 @@ fn import_mesh(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.collect();
|
||||
s.surfaces.extend(surfaces.into_iter().map(Arc::new));
|
||||
s.sources.meshes.push(source);
|
||||
Ok(json!({"path": path, "surfaces": added}))
|
||||
// The model's stitches of surfaces by these names apply to them too.
|
||||
let (_, failed) = super::stitch::restitch(s);
|
||||
let mut reply = json!({"path": path, "surfaces": added});
|
||||
let warnings: Vec<String> =
|
||||
failed.into_iter().filter(|(_, why)| !why.starts_with("no surface")).map(|(n, why)| format!("stitch `{n}`: {why}")).collect();
|
||||
if !warnings.is_empty()
|
||||
&& let Some(o) = reply.as_object_mut()
|
||||
{
|
||||
o.insert("warnings".into(), json!(warnings));
|
||||
}
|
||||
Ok(reply)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
pub(crate) mod tests {
|
||||
use std::io::{Cursor, Write};
|
||||
|
||||
use super::*;
|
||||
|
||||
fn package(model: &str) -> Vec<u8> {
|
||||
/// A session with the design the SolidCraft `commands` build open.
|
||||
pub(crate) fn design(commands: Value) -> (crate::tests::TempDir, Session) {
|
||||
let bytes = fieldcraft_geometry::script_design(&json!({"commands": commands}).to_string()).unwrap();
|
||||
let (dir, path) = crate::tests::design_file(&bytes);
|
||||
let mut s = Session::default();
|
||||
s.execute("geometry.open", &json!({"path": path})).unwrap();
|
||||
(dir, s)
|
||||
}
|
||||
|
||||
fn kinds(r: &Value) -> Vec<&str> {
|
||||
r["findings"].as_array().unwrap().iter().map(|f| f["kind"].as_str().unwrap()).collect()
|
||||
}
|
||||
|
||||
/// A plain block is clean at a size it can be meshed with; a 0.4 mm step on its top is a
|
||||
/// sliver riser with two short edges, located on the step; the check changes nothing.
|
||||
#[test]
|
||||
fn check_finds_a_small_step() {
|
||||
let (_dir, mut s) = design(json!([{"command": "solid.box", "params": {"length": 40, "width": 20, "height": 10, "body_name": "Base"}}]));
|
||||
let clean = s.execute("geometry.check", &json!({"size": 5})).unwrap();
|
||||
assert!(kinds(&clean).is_empty(), "{clean}");
|
||||
assert_eq!(clean["summary"], "nothing small against size 5");
|
||||
let (_dir, mut s) = design(json!([
|
||||
{"command": "solid.box", "params": {"length": 40, "width": 20, "height": 10, "body_name": "Base"}},
|
||||
{"command": "solid.box", "params": {"length": 20, "width": 20, "height": 10.4, "operation": "join", "targets": ["Base"]}},
|
||||
]));
|
||||
let journal = s.journal.len();
|
||||
let r = s.execute("geometry.check", &json!({"body": "Base", "size": 5})).unwrap();
|
||||
assert_eq!(s.journal.len(), journal, "read-only");
|
||||
assert_eq!(r["counts"]["short_edge"], 2, "{r}");
|
||||
assert_eq!(r["counts"]["sliver_face"], 1, "{r}");
|
||||
assert_eq!(r["counts"]["small_face"], 0, "{r}");
|
||||
assert_eq!(r["counts"]["near_faces"], 0, "{r}");
|
||||
let names: Vec<String> = s.execute("geometry.bodies", &json!({})).unwrap()[0]["faces"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|f| f["name"].as_str().unwrap().to_string())
|
||||
.collect();
|
||||
for f in r["findings"].as_array().unwrap() {
|
||||
let at: Vec<f64> = f["at"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap()).collect();
|
||||
assert!((at[0] - 20.0).abs() < 1e-6 && at[2] >= 10.0 - 1e-6 && at[2] <= 10.4 + 1e-6, "on the riser: {f}");
|
||||
assert!(f["faces"].as_array().unwrap().iter().all(|n| names.contains(&n.as_str().unwrap().to_string())), "{f}");
|
||||
assert!(f["value"].as_f64().unwrap() < f["limit"].as_f64().unwrap());
|
||||
}
|
||||
let short = &r["findings"][0];
|
||||
assert!((short["value"].as_f64().unwrap() - 0.4).abs() < 1e-6, "{short}");
|
||||
assert!(short["edge"].is_string());
|
||||
// The size is what makes the step small: at 1 mm it is a feature like any other.
|
||||
let fine = s.execute("geometry.check", &json!({"size": 1})).unwrap();
|
||||
assert!(kinds(&fine).is_empty(), "{fine}");
|
||||
}
|
||||
|
||||
/// The findings are drawn in the picture: the sliver riser's faces in the marker colour, the
|
||||
/// short edges as lines and a dot at each finding over everything; a clean check, `mark:
|
||||
/// false` or another design takes them away.
|
||||
#[test]
|
||||
fn check_marks_its_findings_in_the_picture() {
|
||||
let (_dir, mut s) = design(json!([
|
||||
{"command": "solid.box", "params": {"length": 40, "width": 20, "height": 10, "body_name": "Base"}},
|
||||
{"command": "solid.box", "params": {"length": 20, "width": 20, "height": 10.4, "operation": "join", "targets": ["Base"]}},
|
||||
]));
|
||||
let p = crate::view::Palette::LIGHT;
|
||||
let marked = |s: &Session| {
|
||||
let (scene, _, _) = crate::view::scene(s, &p);
|
||||
let faces = scene.tris.iter().filter(|t| t.color == p.marked).count();
|
||||
let lines: Vec<_> = scene.on_top.iter().filter(|l| l.color == p.marked).collect();
|
||||
let dots = lines.iter().filter(|l| l.a == l.b).count();
|
||||
(faces, lines.len() - dots, dots)
|
||||
};
|
||||
assert_eq!(marked(&s), (0, 0, 0), "nothing checked yet");
|
||||
let r = s.execute("geometry.check", &json!({"size": 5})).unwrap();
|
||||
let (faces, edges, dots) = marked(&s);
|
||||
assert!(faces > 0, "the riser's faces are marked");
|
||||
assert!(edges >= 2, "the short edges are drawn: {edges}");
|
||||
assert_eq!(dots, r["findings"].as_array().unwrap().len(), "a dot at each finding");
|
||||
// Every marked triangle lies on the riser, x = 20, between z = 10 and 10.4.
|
||||
let (scene, _, _) = crate::view::scene(&s, &p);
|
||||
for t in scene.tris.iter().filter(|t| t.color == p.marked) {
|
||||
assert!(t.p.iter().all(|v| (v.x - 20.0).abs() < 1e-6 && v.z > 10.0 - 1e-6 && v.z < 10.4 + 1e-6), "{t:?}");
|
||||
}
|
||||
s.execute("geometry.check", &json!({"size": 5, "mark": false})).unwrap();
|
||||
assert_eq!(marked(&s), (0, 0, 0), "mark: false clears");
|
||||
s.execute("geometry.check", &json!({"size": 5})).unwrap();
|
||||
s.execute("geometry.check", &json!({"size": 1})).unwrap();
|
||||
assert_eq!(marked(&s), (0, 0, 0), "a clean check clears");
|
||||
s.execute("geometry.check", &json!({"size": 5})).unwrap();
|
||||
assert!(s.execute("geometry.check", &json!({"size": 5, "mark": "yes"})).is_err());
|
||||
assert!(marked(&s).2 > 0, "a failed check keeps the marks");
|
||||
s.design = s.design.as_deref().map(|d| Arc::new(d.clone()));
|
||||
assert_eq!(marked(&s), (0, 0, 0), "stale once another design is open");
|
||||
}
|
||||
|
||||
/// Two bodies 0.5 mm apart: their facing faces are near faces; once they touch they are not.
|
||||
#[test]
|
||||
fn check_finds_a_gap_between_bodies() {
|
||||
let (_dir, mut s) = design(json!([
|
||||
{"command": "solid.box", "params": {"length": 10, "width": 10, "height": 10, "body_name": "A"}},
|
||||
{"command": "solid.box", "params": {"length": 10, "width": 10, "height": 10, "corner": [0, 0, 10.5], "body_name": "B"}},
|
||||
]));
|
||||
let r = s.execute("geometry.check", &json!({"size": 2.5})).unwrap();
|
||||
assert_eq!(kinds(&r), ["near_faces"], "{r}");
|
||||
let f = &r["findings"][0];
|
||||
assert!((f["value"].as_f64().unwrap() - 0.5).abs() < 1e-6, "{f}");
|
||||
let bodies = [f["body"].as_str().unwrap(), f["other_body"].as_str().unwrap()];
|
||||
assert!(bodies.contains(&"A") && bodies.contains(&"B"), "{f}");
|
||||
assert!(s.execute("geometry.check", &json!({"size": 2.5, "gap": 0.1})).unwrap()["findings"].as_array().unwrap().is_empty());
|
||||
// Without a size the default is a twentieth of the smaller diagonal.
|
||||
let d = s.execute("geometry.check", &json!({})).unwrap();
|
||||
assert!((d["size"].as_f64().unwrap() - 300f64.sqrt() / 20.0).abs() < 1e-9, "{d}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_rejects_hostile_parameters() {
|
||||
let mut s = Session::default();
|
||||
assert!(s.execute("geometry.check", &json!({})).is_err(), "needs a design");
|
||||
let (_dir, mut s) = design(json!([{"command": "solid.box", "params": {"length": 10, "width": 10, "height": 10, "body_name": "A"}}]));
|
||||
for p in [
|
||||
json!({"size": 0}),
|
||||
json!({"size": -1}),
|
||||
json!({"size": "big"}),
|
||||
json!({"size": 1e308, "gap": 1e308}),
|
||||
json!({"short_edge": -1}),
|
||||
json!({"sliver": 1e9}),
|
||||
json!({"parallel_angle": 90}),
|
||||
json!({"body": "Nope"}),
|
||||
json!({"body": 3}),
|
||||
] {
|
||||
assert!(s.execute("geometry.check", &p).is_err(), "{p}");
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn package(model: &str) -> Vec<u8> {
|
||||
let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
|
||||
zip.start_file("3D/3dmodel.model", zip::write::SimpleFileOptions::default()).unwrap();
|
||||
zip.write_all(model.as_bytes()).unwrap();
|
||||
|
||||
@@ -5,18 +5,18 @@ use std::borrow::Cow;
|
||||
|
||||
use fieldcraft_geometry::{FaceSurface, Shape, SurfaceMesh};
|
||||
use fieldcraft_mesh::hex::{BoundaryFace, Cell, HexMesh, HexMethod, HexOptions, MAX_CELLS, Surface, hex_mesh, hex_mesh_cells};
|
||||
use fieldcraft_mesh::size::UniformSize;
|
||||
use fieldcraft_mesh::surface::BoundarySurface;
|
||||
use fieldcraft_model::{GeometryRef, Model};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::sizes::Sizing;
|
||||
use crate::requirement::Requirement;
|
||||
use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
|
||||
pub const HEX_GENERATE: CommandSpec = CommandSpec {
|
||||
id: "mesh.hex.generate",
|
||||
label: "Hex Mesh",
|
||||
params: r#"{"body": "Body1" (default: every body of the open design) or "surface": 0 (an imported surface), "size": 2.0 (element size, mm; default 1/25 of the bounding-box diagonal), "order": 1 (hex8) or 2 (hex20, mid-edge nodes on the geometry), "method": "auto" (default) | "swept" (any sweep) | "extrude" | "sweep" | "revolve" | "mapped", "tolerance": 0.05 (chordal tolerance of the tessellation, mm; default 1e-3 of the diagonal)} — replaces the meshes of the bodies it meshes (as mesh.tet.generate; other bodies keep theirs) with structured hexahedra: multi-level sweeps along a direction or around an axis (their cross-section cut into mappable blocks automatically: O-grids round holes, triangle primitives, four-sided blocks; nothing is added to the model), one-to-one sweeps through linking faces, or mapped blocks. Boundary element faces are tagged with their B-rep faces (named as in geometry.bodies; by index when a face has no name; `weld`, `weld_distance` and `feature_angle` as in mesh.tet.generate), so sets by geometry resolve. Edge seeds (mesh.seed.edge) set the counts and spacing of their edges: exact seeds are held, and seeds that cannot all hold (opposite sides of a mapped face, the layers between two levels) are an error naming them. Reports per body the method, counts and quality; a body no structured method fits is an error that says why"#,
|
||||
params: r#"{"body": "Body1" (default: every body of the open design) or "surface": 0 (an imported surface), "size": 2.0 (element size, mm; default 1/25 of the bounding-box diagonal), "order": 1 (hex8) or 2 (hex20, mid-edge nodes on the geometry), "method": "auto" (default) | "swept" (any sweep) | "extrude" | "sweep" | "revolve" | "mapped", "tolerance": 0.05 (chordal tolerance of the tessellation, mm; default 1e-3 of the diagonal)} — replaces the meshes of the bodies it meshes (as mesh.tet.generate; other bodies keep theirs) with structured hexahedra: multi-level sweeps along a direction or around an axis (their cross-section cut into mappable blocks automatically: O-grids round holes, triangle primitives, four-sided blocks; nothing is added to the model), one-to-one sweeps through linking faces, or mapped blocks. Boundary element faces are tagged with their B-rep faces (named as in geometry.bodies; by index when a face has no name; `weld`, `weld_distance` and `feature_angle` as in mesh.tet.generate), so sets by geometry resolve. Edge seeds (mesh.seed.edge) set the counts and spacing of their edges: exact seeds are held, and seeds that cannot all hold (opposite sides of a mapped face, the layers between two levels) are an error naming them. Size controls (mesh.size.set) set the division counts and spacing of the other edges through the graded size field (a structured mesh carries a count across its block, so a control refines the whole layer or row it lies in). Reports per body the method, counts and quality; a body no structured method fits is an error that says why"#,
|
||||
form: Some(super::forms::HEX_GENERATE),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Geometry],
|
||||
@@ -42,6 +42,9 @@ pub(super) struct Input<'a> {
|
||||
/// The design's edge seeds ([`super::seeds::edge_seeds`]); the mesher applies those on its
|
||||
/// edges.
|
||||
pub(super) seeds: Vec<fieldcraft_mesh::seeds::EdgeSeed>,
|
||||
/// Per triangle of `mesh`, the B-rep face it lies on: its piece of a merged face (empty for
|
||||
/// a surface that is not a body's).
|
||||
pub(super) pieces: Vec<u32>,
|
||||
}
|
||||
|
||||
fn analytic(s: FaceSurface) -> Surface {
|
||||
@@ -73,7 +76,8 @@ pub(super) fn shape_input(shape: &Shape, tolerance: Option<f64>) -> Result<Input
|
||||
}
|
||||
let surfaces = shape.face_surfaces()?.into_iter().map(|f| f.map(analytic)).collect();
|
||||
let face_names = shape.faces().iter().map(|f| f.name.clone()).collect();
|
||||
Ok(Input { name: shape.name.clone(), mesh: Cow::Owned(shape.tessellate(tol)?), surfaces, face_names, diagonal: diag, seeds: Vec::new() })
|
||||
let (mesh, pieces) = shape.tessellate_pieces(tol)?;
|
||||
Ok(Input { name: shape.name.clone(), mesh: Cow::Owned(mesh), surfaces, face_names, diagonal: diag, seeds: Vec::new(), pieces })
|
||||
}
|
||||
|
||||
/// The imported surface `surface` names, with the body name its mesh is tagged with.
|
||||
@@ -109,7 +113,15 @@ pub(super) fn inputs<'a>(s: &'a Session, p: &Value) -> Result<Vec<Input<'a>>> {
|
||||
}
|
||||
// Faces named by index, as mesh.tet.generate names them.
|
||||
let face_names = (0..t.faces).map(|f| f.to_string()).collect();
|
||||
Ok(vec![Input { name, mesh: Cow::Borrowed(&t.mesh), surfaces: Vec::new(), face_names, diagonal: diagonal(lo, hi), seeds: Vec::new() }])
|
||||
Ok(vec![Input {
|
||||
name,
|
||||
mesh: Cow::Borrowed(&t.mesh),
|
||||
surfaces: Vec::new(),
|
||||
face_names,
|
||||
diagonal: diagonal(lo, hi),
|
||||
seeds: Vec::new(),
|
||||
pieces: Vec::new(),
|
||||
}])
|
||||
}
|
||||
(None, None) => {
|
||||
let design = s.design.as_deref().ok_or_else(|| EngineError::Params("open a design first, or give `surface`".into()))?;
|
||||
@@ -146,7 +158,7 @@ pub const HEX_MAPPED: CommandSpec = CommandSpec {
|
||||
pub const HEX_BLOCKS: CommandSpec = CommandSpec {
|
||||
id: "mesh.hex.blocks",
|
||||
label: "Block Hex Mesh",
|
||||
params: r#"{"bodies": ["Cell1", "Cell2"] (default: every body of the open design), "size": 2.0 (element size, mm; default 1/25 of the bounding-box diagonal of all of them), "order": 1 (hex8) or 2 (hex20, mid-edge nodes on the geometry), "tolerance": 0.05 (chordal tolerance of the tessellation, mm; default 1e-3 of that diagonal)} — replaces the cells' meshes (as mesh.tet.generate; other bodies keep theirs) with one conforming all-hex mesh of a part partitioned into cells (bodies that meet face to face, such as the pieces of solid.split_body in SolidCraft): every cell a mapped block (six four-sided faces; faces that meet smoothly, such as the two halves of a hole, count as one), division counts assigned over all cells together, each shared face meshed once and its nodes merged, radial lines graded off holes and fillets. Elements are tagged with their cell's body and boundary faces with its B-rep faces (`weld`, `weld_distance` and `feature_angle` as in mesh.tet.generate). Reports the cells, the shared faces, counts and quality; a cell that is not a block, or cells that touch without sharing whole faces, is an error that says which"#,
|
||||
params: r#"{"bodies": ["Cell1", "Cell2"] (default: every body of the open design), "size": 2.0 (element size, mm; default 1/25 of the bounding-box diagonal of all of them), "order": 1 (hex8) or 2 (hex20, mid-edge nodes on the geometry), "tolerance": 0.05 (chordal tolerance of the tessellation, mm; default 1e-3 of that diagonal)} — replaces the cells' meshes (as mesh.tet.generate; other bodies keep theirs) with one conforming all-hex mesh of a part partitioned into cells (bodies that meet face to face, such as the pieces of solid.split_body in SolidCraft): every cell a mapped block (six four-sided faces; faces that meet smoothly, such as the two halves of a hole, count as one), division counts assigned over all cells together, each shared face meshed once and its nodes merged, radial lines graded off holes and fillets, size controls (mesh.size.set; one on a whole cell refines its surface) setting the counts and spacing through the graded size field. Elements are tagged with their cell's body and boundary faces with its B-rep faces (`weld`, `weld_distance` and `feature_angle` as in mesh.tet.generate). Reports the cells, the shared faces, counts and quality; a cell that is not a block, or cells that touch without sharing whole faces, is an error that says which"#,
|
||||
form: Some(super::forms::HEX_BLOCKS),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Design],
|
||||
@@ -220,7 +232,9 @@ pub(super) fn hex_blocks(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
seeds: super::seeds::edge_seeds(s)?,
|
||||
..HexOptions::default()
|
||||
};
|
||||
let cm = hex_mesh_cells(&cells, &UniformSize(h), &options).map_err(|e| match e {
|
||||
let names: Vec<&str> = inputs.iter().map(|b| b.name.as_str()).collect();
|
||||
let field = super::sizes::sizing(s)?.joint(&names).field("", h)?;
|
||||
let cm = hex_mesh_cells(&cells, &field, &options).map_err(|e| match e {
|
||||
fieldcraft_mesh::Error::Model(fieldcraft_model::Error::Cancelled) => EngineError::Model(fieldcraft_model::Error::Cancelled),
|
||||
e => EngineError::Params(e.to_string()),
|
||||
})?;
|
||||
@@ -285,9 +299,10 @@ fn generate(s: &mut Session, p: &Value, default: HexMethod, allowed: &[HexMethod
|
||||
})?,
|
||||
};
|
||||
let bodies = inputs(s, p)?;
|
||||
let sizing = super::sizes::sizing(s)?;
|
||||
let mut meshes: Vec<(Input, HexMesh)> = Vec::with_capacity(bodies.len());
|
||||
for b in bodies {
|
||||
let mesh = hex_body(p, &b, method, quadratic, size)?;
|
||||
let mesh = hex_body(p, &b, method, quadratic, size, &sizing)?;
|
||||
meshes.push((b, mesh));
|
||||
}
|
||||
// Replace the bodies' meshes (replace.rs); other bodies and the definitions stay.
|
||||
@@ -315,13 +330,13 @@ fn generate(s: &mut Session, p: &Value, default: HexMethod, allowed: &[HexMethod
|
||||
Ok(cleared.report(s, json!({"bodies": report, "nodes": nodes, "elements": elements})))
|
||||
}
|
||||
|
||||
/// Mesh one body with `method` (hex20 when `quadratic`) at `size` (default: from its extent);
|
||||
/// the model is not touched. Errors name the body.
|
||||
pub(super) fn hex_body(p: &Value, b: &Input, method: HexMethod, quadratic: bool, size: Option<f64>) -> Result<HexMesh> {
|
||||
/// Mesh one body with `method` (hex20 when `quadratic`) at `size` (default: from its extent) and
|
||||
/// the size controls of `sizing`; the model is not touched. Errors name the body.
|
||||
pub(super) fn hex_body(p: &Value, b: &Input, method: HexMethod, quadratic: bool, size: Option<f64>, sizing: &Sizing) -> Result<HexMesh> {
|
||||
if !(b.diagonal > 0.0 && b.diagonal.is_finite()) {
|
||||
return Err(EngineError::Params(format!("body `{}` has no extent", b.name)));
|
||||
}
|
||||
let h = size.unwrap_or(DEFAULT_RELATIVE_SIZE * b.diagonal);
|
||||
let h = sizing.body_size(&b.name, size.unwrap_or(DEFAULT_RELATIVE_SIZE * b.diagonal));
|
||||
if h < MIN_RELATIVE_SIZE * b.diagonal {
|
||||
return Err(EngineError::Params(format!("`size` must be at least {} mm for body `{}`", MIN_RELATIVE_SIZE * b.diagonal, b.name)));
|
||||
}
|
||||
@@ -337,7 +352,8 @@ pub(super) fn hex_body(p: &Value, b: &Input, method: HexMethod, quadratic: bool,
|
||||
seeds: b.seeds.clone(),
|
||||
..HexOptions::default()
|
||||
};
|
||||
hex_mesh(&surface, &b.surfaces, &UniformSize(h), &options).map_err(|e| EngineError::of_body(&b.name, e))
|
||||
let field = sizing.field(&b.name, h)?;
|
||||
hex_mesh(&surface, &b.surfaces, &field, &options).map_err(|e| EngineError::of_body(&b.name, e))
|
||||
}
|
||||
|
||||
/// Add a mesh of several cells (`inputs[c]` is cell `c`) after the model's largest labels, its
|
||||
|
||||
@@ -7,6 +7,7 @@ use fieldcraft_mesh::cdt::{CdtOptions, tetrahedralize};
|
||||
use fieldcraft_mesh::delaunay::Triangulation;
|
||||
use fieldcraft_mesh::quality::{self, QualityReport};
|
||||
use fieldcraft_mesh::surface::{BoundarySurface, Defect, FeatureKind, SurfaceOptions};
|
||||
use fieldcraft_mesh::tet::canonical::CanonicalSoup;
|
||||
use fieldcraft_mesh::tet::geometry::AnalyticSurface;
|
||||
use fieldcraft_mesh::tet::improve::ImproveOptions;
|
||||
use fieldcraft_mesh::tet::{Order, QualityMesh, QualityOptions, TetQuality, quality_mesh, raw_mesh};
|
||||
@@ -40,7 +41,7 @@ pub const NODE_REMOVE: CommandSpec = CommandSpec {
|
||||
pub const ELEMENT_ADD: CommandSpec = CommandSpec {
|
||||
id: "mesh.element.add",
|
||||
label: "Add Element",
|
||||
params: r#"{"label": 1, "kind": "rod2|beam2|tri3|tri6|quad4|quad8|tet4|tet10|wedge6|hex8|hex20", "nodes": [labels]} — adds or replaces"#,
|
||||
params: r#"{"label": 1, "kind": "rod2|beam2|tri3|tri6|quad4|quad8|tet4|tet10|wedge6|pyramid5|hex8|hex20", "nodes": [labels]} — adds or replaces"#,
|
||||
form: Some(super::forms::ELEMENT_ADD),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Nodes],
|
||||
@@ -67,6 +68,26 @@ pub const QUALITY: CommandSpec = CommandSpec {
|
||||
run: quality_report,
|
||||
};
|
||||
|
||||
pub const QUALITY_MARK: CommandSpec = CommandSpec {
|
||||
id: "mesh.quality.mark",
|
||||
label: "Mark Bad Elements",
|
||||
params: r#"{"limits"?: {"scaled_jacobian": 0.2, "min_dihedral": 10, "max_dihedral": 165, "aspect_ratio": 10, "min_angle": 15, "max_angle": 165} (the defaults; given limits are merged over them, null drops one; any metric of mesh.quality may be limited: a floor for those where higher is better, a ceiling for max_dihedral, aspect_ratio, edge_ratio, max_angle and skew; angles in degrees), "elements"?: [labels] (default all)} — after meshing, marks the elements that fail a limit (and every solid not proven valid) as the element set `quality_marked`, replaced on every run and removed when none fail; the viewport and `fieldcraft-cli snapshot` draw its elements in the marker colour. Replies `summary`, `measured`, `marked`, `invalid`, the `limits` used and `elements` (worst first: not valid, then lowest scaled Jacobian; at most 1000, `truncated` says when more): each `label`, `kind`, `body`, `at` (its centroid), `valid` and `failed` ({metric, value, limit} per limit it fails). One undo step"#,
|
||||
form: Some(super::forms::QUALITY_MARK),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Mesh],
|
||||
run: quality_mark,
|
||||
};
|
||||
|
||||
pub const EXPORT_STL: CommandSpec = CommandSpec {
|
||||
id: "mesh.export.stl",
|
||||
label: "Export STL",
|
||||
params: r#"{"path": "skin.stl", "set": "top", "format": "binary|ascii"} — writes the mesh's outer surface as STL: every solid element face no other solid shares, and the top side (face 1) of every shell; `set` (optional) writes only that set's element faces instead; `format` defaults to binary. Quadrilaterals are split in two, normals point out of the elements, and coordinates are rounded to single precision (STL stores f32). Replies {path, triangles, bytes}"#,
|
||||
form: Some(super::forms::EXPORT_STL),
|
||||
kind: CommandKind::Session,
|
||||
requires: &[Requirement::Mesh],
|
||||
run: export_stl,
|
||||
};
|
||||
|
||||
pub const DELAUNAY_POINTS: CommandSpec = CommandSpec {
|
||||
id: "mesh.delaunay.points",
|
||||
label: "Delaunay of Points",
|
||||
@@ -107,7 +128,7 @@ pub const SURFACE_CHECK: CommandSpec = CommandSpec {
|
||||
pub const TET_GENERATE: CommandSpec = CommandSpec {
|
||||
id: "mesh.tet.generate",
|
||||
label: "Generate Tet Mesh",
|
||||
params: r#"{"body": "Body1" (default: every body of the open design) or "surface": 0 (an imported surface), "tolerance": 0.05 (chordal tolerance of the tessellation, mm; default 1e-3 of the body's bounding-box diagonal), the boundary options of mesh.surface.check ("weld", "weld_distance", "feature_angle"), optional "size": mm (target edge length; default a twentieth of the bounding-box diagonal), "order": "linear" | "quadratic" (tet4 or tet10; quadratic mid-edge nodes lie on the B-rep faces), "quality": true (false: the raw constrained Delaunay tetrahedralization), "quality_target": 40 (degrees: tetrahedra whose smallest dihedral-angle sine is at least sin of this are left alone by improvement, 5..=60), "curvature_angle": 22.5 (degrees per element along curved boundary, 0 for none, at most 90), "grading": 0.3 (largest size change per unit length, 0.05..=2), "max_passes": 20 (improvement passes, 0..=200), "max_points": 1000000 (1..=20000000), "max_steiner": 50000 (0..=10000000), "max_tets": 5000000 (1..=50000000) — the constrained Delaunay tetrahedralization of the welded boundary (every boundary triangle a union of tet faces), refined to the size field with its boundary vertices on the B-rep faces, smoothed and improved (Delaunay refinement, ODT smoothing, Klingner–Shewchuk improvement), with a quality report. Replaces the mesh of each body it meshes (its elements and the nodes only they use; other bodies' meshes stay; the reply's `replaced`, `stale_sets` and `results_cleared` say what went), adds the elements and their nodes (labels after the largest existing ones), tags the elements with the body and each boundary element face with its geometry face, and fails, changing nothing, if a surface is invalid or a mesh does not pass the validity gate. Edge seeds (mesh.seed.edge) divide their edges at the seeded nodes, and the size field grades from them. Reports the totals and, under `bodies`, each body's counts, size, quality report and `seeds` (per seeded edge, the mesh edges along it)"#,
|
||||
params: r#"{"body": "Body1" (default: every body of the open design) or "surface": 0 (an imported surface), "tolerance": 0.05 (chordal tolerance of the tessellation, mm; default 1e-3 of the body's bounding-box diagonal), the boundary options of mesh.surface.check ("weld", "weld_distance", "feature_angle"), optional "size": mm (target edge length; default a twentieth of the bounding-box diagonal), "order": "linear" | "quadratic" (tet4 or tet10; quadratic mid-edge nodes lie on the B-rep faces), "quality": true (false: the raw constrained Delaunay tetrahedralization), "quality_target": 40 (degrees: tetrahedra whose smallest dihedral-angle sine is at least sin of this are left alone by improvement, 5..=60), "curvature_angle": 22.5 (degrees per element along curved boundary, 0 for none, at most 90), "grading": 0.3 (largest size change per unit length, 0.05..=2; default the model's growth rate, mesh.size.growth), "max_passes": 20 (improvement passes, 0..=200), "max_points": 1000000 (1..=20000000), "max_steiner": 50000 (0..=10000000), "max_tets": 5000000 (1..=50000000) — the constrained Delaunay tetrahedralization of the welded boundary (every boundary triangle a union of tet faces), refined to the size field with its boundary vertices on the B-rep faces, smoothed and improved (Delaunay refinement, ODT smoothing, Klingner–Shewchuk improvement), with a quality report. Replaces the mesh of each body it meshes (its elements and the nodes only they use; other bodies' meshes stay; the reply's `replaced`, `stale_sets` and `results_cleared` say what went), adds the elements and their nodes (labels after the largest existing ones), tags the elements with the body and each boundary element face with its geometry face, and fails, changing nothing, if a surface is invalid or a mesh does not pass the validity gate. Edge seeds (mesh.seed.edge) divide their edges at the seeded nodes, and the size field grades from them; size controls (mesh.size.set) refine it, graded at the model's growth rate (mesh.size.growth) unless `grading` is given. Reports the totals and, under `bodies`, each body's counts, size, quality report and `seeds` (per seeded edge, the mesh edges along it)"#,
|
||||
form: Some(super::forms::TET),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Geometry],
|
||||
@@ -146,7 +167,7 @@ pub(super) fn surface_options(p: &Value, mesh: &SurfaceMesh) -> Result<SurfaceOp
|
||||
}
|
||||
|
||||
/// An optional number in `range`.
|
||||
fn number_in(p: &Value, key: &str, default: f64, range: std::ops::RangeInclusive<f64>) -> Result<f64> {
|
||||
pub(super) fn number_in(p: &Value, key: &str, default: f64, range: std::ops::RangeInclusive<f64>) -> Result<f64> {
|
||||
let v = optional_number(p, key, default)?;
|
||||
if range.contains(&v) { Ok(v) } else { Err(EngineError::param(key, ProblemKind::NotInRange { min: *range.start(), max: *range.end() })) }
|
||||
}
|
||||
@@ -196,21 +217,30 @@ pub(super) fn quality_options(p: &Value, max_tets: u64) -> Result<(QualityOption
|
||||
/// The exact surfaces of a body's faces, for the quality stage's projections (none when the
|
||||
/// kernel cannot give them: the stage then keeps to the tessellation).
|
||||
pub fn analytic_surfaces(shape: &fieldcraft_geometry::Shape) -> Vec<Option<AnalyticSurface>> {
|
||||
shape.face_surfaces().unwrap_or_default().into_iter().map(|f| f.map(analytic_surface)).collect()
|
||||
}
|
||||
|
||||
fn analytic_surface(f: fieldcraft_geometry::FaceSurface) -> AnalyticSurface {
|
||||
use fieldcraft_geometry::FaceSurface as F;
|
||||
shape
|
||||
.face_surfaces()
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|f| {
|
||||
f.map(|f| match f {
|
||||
F::Plane { normal, point } => AnalyticSurface::Plane { normal, point },
|
||||
F::Cylinder { origin, axis, radius } => AnalyticSurface::Cylinder { origin, axis, radius },
|
||||
F::Cone { apex, axis, half_angle } => AnalyticSurface::Cone { apex, axis, half_angle },
|
||||
F::Sphere { center, radius } => AnalyticSurface::Sphere { center, radius },
|
||||
F::Torus { center, axis, major, minor } => AnalyticSurface::Torus { center, axis, major, minor },
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
match f {
|
||||
F::Plane { normal, point } => AnalyticSurface::Plane { normal, point },
|
||||
F::Cylinder { origin, axis, radius } => AnalyticSurface::Cylinder { origin, axis, radius },
|
||||
F::Cone { apex, axis, half_angle } => AnalyticSurface::Cone { apex, axis, half_angle },
|
||||
F::Sphere { center, radius } => AnalyticSurface::Sphere { center, radius },
|
||||
F::Torus { center, axis, major, minor } => AnalyticSurface::Torus { center, axis, major, minor },
|
||||
}
|
||||
}
|
||||
|
||||
/// The B-rep pieces of `shape`'s merged faces (virtual topology) for a projection target built
|
||||
/// on its tessellation whose triangles lie on the B-rep faces `tri_piece`
|
||||
/// ([`fieldcraft_geometry::Shape::tessellate_pieces`]): a merged face on no one surface then
|
||||
/// projects onto each piece's own; none when no face is merged.
|
||||
pub(crate) fn pieces(shape: &fieldcraft_geometry::Shape, tri_piece: &[u32]) -> fieldcraft_mesh::tet::geometry::Pieces {
|
||||
if !shape.has_merged_faces() {
|
||||
return fieldcraft_mesh::tet::geometry::Pieces::default();
|
||||
}
|
||||
let analytic = shape.brep_face_surfaces().unwrap_or_default().into_iter().map(|f| f.map(analytic_surface)).collect();
|
||||
fieldcraft_mesh::tet::geometry::Pieces { tri_piece: tri_piece.to_vec(), analytic }
|
||||
}
|
||||
|
||||
pub(super) fn quality_json(q: &TetQuality) -> Value {
|
||||
@@ -235,10 +265,16 @@ pub(super) struct BodyTets {
|
||||
surface_volume: f64,
|
||||
}
|
||||
|
||||
impl BodyTets {
|
||||
pub(super) fn new(name: String, face_names: Vec<String>, mesh: QualityMesh, steiner: usize, cavities: usize, surface_volume: f64) -> Self {
|
||||
BodyTets { name, face_names, mesh, steiner, cavities, surface_volume }
|
||||
}
|
||||
}
|
||||
|
||||
/// Mesh one body or imported surface; the model is not touched. With `several` bodies an error
|
||||
/// names the body.
|
||||
pub(super) fn mesh_body(
|
||||
s: &Session,
|
||||
design: Option<&fieldcraft_geometry::Design>,
|
||||
p: &Value,
|
||||
input: super::hex::Input,
|
||||
cdt: &CdtOptions,
|
||||
@@ -249,19 +285,23 @@ pub(super) fn mesh_body(
|
||||
let mesh = || -> Result<BodyTets> {
|
||||
// The exact faces of a design body; an imported surface keeps to its triangles.
|
||||
let analytic = match p.get("surface") {
|
||||
None => s.design.as_deref().and_then(|d| d.body(&input.name)).map(analytic_surfaces).unwrap_or_default(),
|
||||
None => design.and_then(|d| d.body(&input.name)).map(analytic_surfaces).unwrap_or_default(),
|
||||
Some(_) => Vec::new(),
|
||||
};
|
||||
fieldcraft_model::progress::stage(&format!("{}: boundary", input.name), None, "");
|
||||
let surface = BoundarySurface::new(&input.mesh.positions, &input.mesh.triangles, &input.mesh.tri_face, &surface_options(p, &input.mesh)?)?;
|
||||
// The mesh depends on the geometry only, not on the order the kernel lists it in.
|
||||
let soup = CanonicalSoup::new(&input.mesh.positions, &input.mesh.triangles, &input.mesh.tri_face)?;
|
||||
let surface = BoundarySurface::new(&soup.positions, &soup.triangles, &soup.tri_face, &surface_options(p, &input.mesh)?)?;
|
||||
fieldcraft_model::progress::stage(&format!("{}: Delaunay", input.name), None, "");
|
||||
let tets = tetrahedralize(&surface, cdt)?;
|
||||
let mesh = if quality {
|
||||
let options = QualityOptions { seeds: input.seeds.clone(), ..options.clone() };
|
||||
quality_mesh(&surface, &tets, analytic, &options)?
|
||||
let mut mesh = if quality {
|
||||
let frozen_faces = options.frozen_faces.iter().filter_map(|&f| soup.canonical_tag(f)).collect();
|
||||
let options = QualityOptions { seeds: input.seeds.clone(), frozen_faces, ..options.clone() };
|
||||
quality_mesh(&surface, &tets, soup.analytic(&analytic), &options)?
|
||||
} else {
|
||||
raw_mesh(&surface, &tets)?
|
||||
};
|
||||
soup.restore_tags(&mut mesh);
|
||||
Ok(BodyTets {
|
||||
name: input.name.clone(),
|
||||
face_names: input.face_names.clone(),
|
||||
@@ -280,10 +320,12 @@ fn tet_generate(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
CdtOptions { max_steiner: optional_count(p, "max_steiner", s.limits.max_steiner as u64, 0..=TET_MAX_STEINER)?, max_tets: options.max_tets };
|
||||
// Every body is meshed before the model changes; with several, an error names its body.
|
||||
let inputs = super::hex::inputs(s, p)?;
|
||||
let sizing = super::sizes::sizing(s)?;
|
||||
let several = inputs.len() > 1;
|
||||
let mut meshed = Vec::with_capacity(inputs.len());
|
||||
for input in inputs {
|
||||
meshed.push(mesh_body(s, p, input, &cdt_options, &options, quality, several)?);
|
||||
let options = sizing.tet_options(&input.name, p, &options);
|
||||
meshed.push(mesh_body(s.design.as_deref(), p, input, &cdt_options, &options, quality, several)?);
|
||||
}
|
||||
|
||||
// Replace the bodies' meshes (replace.rs); other bodies and the definitions stay.
|
||||
@@ -553,6 +595,53 @@ fn number(v: f64) -> Value {
|
||||
if v.is_finite() { json!(v) } else { json!(if v > 0.0 { "inf" } else { "-inf" }) }
|
||||
}
|
||||
|
||||
fn export_stl(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let set: Option<String> = super::opt(p, "set")?;
|
||||
let format: Option<String> = super::opt(p, "format")?;
|
||||
let ascii = match format.as_deref() {
|
||||
None | Some("binary") => false,
|
||||
Some("ascii") => true,
|
||||
Some(other) => {
|
||||
let shown: String = other.chars().take(32).collect();
|
||||
return Err(EngineError::Params(format!("unknown STL format `{shown}`; known: binary, ascii")));
|
||||
}
|
||||
};
|
||||
let path = super::file_param(s, p, "path", fieldcraft_io::roots::Access::Write)?;
|
||||
let m = &s.model;
|
||||
if m.elements().is_empty() {
|
||||
return Err(EngineError::Params("the mesh has no elements".into()));
|
||||
}
|
||||
let faces = match &set {
|
||||
Some(name) => {
|
||||
let shown: String = name.chars().take(64).collect();
|
||||
if !m.sets().contains_key(name) {
|
||||
return Err(EngineError::Params(format!("no set `{shown}`")));
|
||||
}
|
||||
let faces = m.resolve_set(name)?.faces;
|
||||
if faces.is_empty() {
|
||||
return Err(EngineError::Params(format!("set `{shown}` has no element faces")));
|
||||
}
|
||||
faces
|
||||
}
|
||||
None => {
|
||||
let faces = m.skin_faces();
|
||||
if faces.is_empty() {
|
||||
return Err(EngineError::Params("the mesh has only line elements: no surface to write".into()));
|
||||
}
|
||||
faces
|
||||
}
|
||||
};
|
||||
let bytes = if ascii {
|
||||
let name = std::path::Path::new(&path).file_stem().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
fieldcraft_io::stl::write_ascii(m, &faces, &name)?.into_bytes()
|
||||
} else {
|
||||
fieldcraft_io::stl::write_binary(m, &faces)?
|
||||
};
|
||||
let triangles = fieldcraft_io::stl::triangles(m, &faces)?.len();
|
||||
fieldcraft_io::fs::write_atomic(std::path::Path::new(&path), &bytes, false)?;
|
||||
Ok(json!({"path": path, "triangles": triangles, "bytes": bytes.len()}))
|
||||
}
|
||||
|
||||
fn quality_report(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let bins = match p.get("bins") {
|
||||
None => 10,
|
||||
@@ -593,6 +682,103 @@ fn quality_report(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}))
|
||||
}
|
||||
|
||||
/// The element set `mesh.quality.mark` keeps the marked elements in.
|
||||
pub const MARKED_SET: &str = "quality_marked";
|
||||
|
||||
/// The default limits of `mesh.quality.mark`.
|
||||
const MARK_LIMITS: [(quality::Metric, f64); 6] = [
|
||||
(quality::Metric::ScaledJacobian, 0.2),
|
||||
(quality::Metric::MinDihedral, 10.0),
|
||||
(quality::Metric::MaxDihedral, 165.0),
|
||||
(quality::Metric::AspectRatio, 10.0),
|
||||
(quality::Metric::MinAngle, 15.0),
|
||||
(quality::Metric::MaxAngle, 165.0),
|
||||
];
|
||||
|
||||
/// Most marked elements `mesh.quality.mark` lists.
|
||||
const MAX_MARKED_LISTED: usize = 1000;
|
||||
|
||||
fn quality_mark(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let mut limits: std::collections::BTreeMap<quality::Metric, f64> = MARK_LIMITS.into_iter().collect();
|
||||
match p.get("limits") {
|
||||
None | Some(Value::Null) => {}
|
||||
Some(Value::Object(given)) => {
|
||||
for (name, v) in given {
|
||||
let metric = quality::Metric::parse(name).ok_or_else(|| {
|
||||
let known: Vec<&str> = quality::Metric::ALL.iter().map(|m| m.name()).collect();
|
||||
EngineError::Params(format!("no quality metric `{}` (one of {})", name.chars().take(40).collect::<String>(), known.join(", ")))
|
||||
})?;
|
||||
match v {
|
||||
Value::Null => {
|
||||
limits.remove(&metric);
|
||||
}
|
||||
v => {
|
||||
let limit = v
|
||||
.as_f64()
|
||||
.filter(|x| x.is_finite())
|
||||
.ok_or_else(|| EngineError::Params(format!("the limit of `{name}` must be a number")))?;
|
||||
limits.insert(metric, limit);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(_) => return Err(EngineError::Params("`limits` must be an object of metric names and numbers".into())),
|
||||
}
|
||||
let selection = match p.get("elements") {
|
||||
None => None,
|
||||
Some(_) => Some(labels(p, "elements", MAX_ELEMENTS)?),
|
||||
};
|
||||
let limits: Vec<(quality::Metric, f64)> = limits.into_iter().collect();
|
||||
let (measured, marked) = quality::mark_elements(&s.model, selection.as_deref(), &limits)?;
|
||||
let set = fieldcraft_model::Set { elements: marked.iter().map(|m| m.label).collect(), ..Default::default() };
|
||||
if set.elements.is_empty() {
|
||||
if s.model.set(MARKED_SET).is_some() {
|
||||
s.model.remove_set(MARKED_SET)?;
|
||||
}
|
||||
} else if s.model.set(MARKED_SET).is_some() {
|
||||
s.model.replace_set(MARKED_SET, set)?;
|
||||
} else {
|
||||
s.model.add_set(MARKED_SET, set)?;
|
||||
}
|
||||
let invalid = marked.iter().filter(|m| m.is_invalid()).count();
|
||||
let c = s.model.classification();
|
||||
let body = |label: u32| c.elements().get(&label).and_then(|t| t.region).and_then(|r| c.entity(r)).map(|g| g.body.clone());
|
||||
let elements: Vec<Value> = marked
|
||||
.iter()
|
||||
.take(MAX_MARKED_LISTED)
|
||||
.map(|m| {
|
||||
json!({
|
||||
"label": m.label,
|
||||
"kind": m.kind.name(),
|
||||
"body": body(m.label),
|
||||
"at": m.centroid,
|
||||
"valid": m.validity.map(|v| v == quality::Validity::Valid),
|
||||
"failed": m.failed.iter().map(|(metric, v, limit)| json!({"metric": metric.name(), "value": number(*v), "limit": limit})).collect::<Vec<_>>(),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let mut summary = format!("{} of {measured} elements marked", marked.len());
|
||||
if invalid > 0 {
|
||||
summary.push_str(&format!(", {invalid} not valid"));
|
||||
}
|
||||
if let Some(worst) = marked.first().and_then(|m| m.scaled_jacobian) {
|
||||
summary.push_str(&format!("; worst scaled Jacobian {worst:.3}"));
|
||||
}
|
||||
if !marked.is_empty() {
|
||||
summary.push_str(&format!(" (set {MARKED_SET})"));
|
||||
}
|
||||
Ok(json!({
|
||||
"summary": summary,
|
||||
"set": (!marked.is_empty()).then_some(MARKED_SET),
|
||||
"measured": measured,
|
||||
"marked": marked.len(),
|
||||
"invalid": invalid,
|
||||
"limits": limits.iter().map(|(m, v)| (m.name().to_string(), json!(v))).collect::<serde_json::Map<_, _>>(),
|
||||
"elements": elements,
|
||||
"truncated": marked.len() > MAX_MARKED_LISTED,
|
||||
}))
|
||||
}
|
||||
|
||||
/// A quality report in a line, for the status bar and the CLI: the elements measured, how many
|
||||
/// are invalid or undetermined, and the scaled Jacobian's minimum and mean.
|
||||
fn quality_summary(r: &QualityReport) -> String {
|
||||
@@ -671,6 +857,63 @@ fn surface_check(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn export_stl_writes_the_skin_or_a_set() {
|
||||
let dir = std::env::temp_dir().join(format!("fieldcraft-stl-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("skin.stl");
|
||||
let path_str = path.to_string_lossy().into_owned();
|
||||
let mut s = Session::default();
|
||||
assert!(s.execute("mesh.export.stl", &json!({"path": path_str})).is_err(), "no mesh");
|
||||
s.execute("file.sample", &json!({"name": "cantilever"})).unwrap();
|
||||
let journal = s.journal.len();
|
||||
let out = s.execute("mesh.export.stl", &json!({"path": path_str})).unwrap();
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
assert!(!bytes.starts_with(b"solid"));
|
||||
let skin = fieldcraft_io::stl::triangles(&s.model, &s.model.skin_faces()).unwrap().len();
|
||||
assert!(skin >= s.model.skin_faces().len() && skin > 0);
|
||||
assert_eq!(out["triangles"], skin);
|
||||
assert_eq!(bytes.len(), 84 + 50 * skin);
|
||||
assert_eq!(out["bytes"], bytes.len());
|
||||
assert_eq!(s.journal.len(), journal, "exporting does not change the model");
|
||||
|
||||
// A set with faces writes only those; one without faces, or an unknown one, is refused.
|
||||
let faces = s.model.skin_faces();
|
||||
let two: Vec<serde_json::Value> = faces.iter().take(2).map(|f| json!({"element": f.element, "face": f.face})).collect();
|
||||
let first = *s.model.elements().keys().next().unwrap();
|
||||
s.execute("model.set.create", &json!({"name": "stl_faces", "faces": two})).unwrap();
|
||||
s.execute("model.set.create", &json!({"name": "stl_elements", "elements": [first]})).unwrap();
|
||||
let out = s.execute("mesh.export.stl", &json!({"path": path_str, "set": "stl_faces"})).unwrap();
|
||||
assert_eq!(out["triangles"], fieldcraft_io::stl::triangles(&s.model, &faces[..2]).unwrap().len());
|
||||
let other = dir.join("other.stl").to_string_lossy().into_owned();
|
||||
let e = s.execute("mesh.export.stl", &json!({"path": other, "set": "nope"})).unwrap_err().to_string();
|
||||
assert!(e.contains("no set `nope`"), "{e}");
|
||||
let e = s.execute("mesh.export.stl", &json!({"path": other, "set": "stl_elements"})).unwrap_err().to_string();
|
||||
assert!(e.contains("has no element faces"), "{e}");
|
||||
let e = s.execute("mesh.export.stl", &json!({"path": other, "format": "obj"})).unwrap_err().to_string();
|
||||
assert!(e.contains("known: binary, ascii"), "{e}");
|
||||
assert!(!dir.join("other.stl").exists());
|
||||
|
||||
let out = s.execute("mesh.export.stl", &json!({"path": path_str, "format": "ascii"})).unwrap();
|
||||
let text = std::fs::read_to_string(&path).unwrap();
|
||||
assert!(text.starts_with("solid skin\n"));
|
||||
assert_eq!(text.matches("facet normal").count(), skin);
|
||||
assert_eq!(out["triangles"], skin);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_stl_refuses_a_mesh_of_lines() {
|
||||
let mut s = Session::default();
|
||||
s.execute("mesh.node.add", &json!({"label": 1, "xyz": [0, 0, 0]})).unwrap();
|
||||
s.execute("mesh.node.add", &json!({"label": 2, "xyz": [1, 0, 0]})).unwrap();
|
||||
s.execute("mesh.element.add", &json!({"label": 1, "kind": "rod2", "nodes": [1, 2]})).unwrap();
|
||||
let path = std::env::temp_dir().join(format!("fieldcraft-stl-lines-{}.stl", std::process::id()));
|
||||
let e = s.execute("mesh.export.stl", &json!({"path": path.to_string_lossy()})).unwrap_err().to_string();
|
||||
assert!(e.contains("only line elements"), "{e}");
|
||||
assert!(!path.exists());
|
||||
}
|
||||
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
@@ -711,6 +954,73 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn mark_session() -> Session {
|
||||
let mut s = Session::default();
|
||||
let corners = json!([[0, 0, 0], [2, 0, 0], [2, 1, 0], [0, 1, 0], [0, 0, 1], [2, 0, 1], [2, 1, 1], [0, 1, 1]]);
|
||||
s.execute("mesh.block", &json!({"body": "Bar", "kind": "hex8", "corners": corners, "divisions": [2, 1, 1]})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
/// Two good hexes mark nothing; a corner pulled in marks its element, located, as the set
|
||||
/// the viewport draws; looser limits empty it; undo brings it back.
|
||||
#[test]
|
||||
fn marks_bad_elements_as_a_set() {
|
||||
let mut s = mark_session();
|
||||
let r = s.execute("mesh.quality.mark", &json!({})).unwrap();
|
||||
assert_eq!((r["measured"].as_u64(), r["marked"].as_u64()), (Some(2), Some(0)), "{r}");
|
||||
assert!(s.model.set(super::MARKED_SET).is_none() && r["set"].is_null());
|
||||
assert_eq!(r["limits"]["scaled_jacobian"], 0.2);
|
||||
// Pull the far top corner of the second hex nearly onto its middle.
|
||||
let (&corner, _) = s.model.nodes().iter().find(|(_, n)| n.xyz == [2.0, 1.0, 1.0]).unwrap();
|
||||
let (&bad, _) = s.model.elements().iter().find(|(_, e)| e.nodes.contains(&corner)).unwrap();
|
||||
s.execute("mesh.node.add", &json!({"label": corner, "xyz": [1.7, 0.7, 0.7]})).unwrap();
|
||||
let r = s.execute("mesh.quality.mark", &json!({})).unwrap();
|
||||
assert_eq!((r["marked"].as_u64(), r["invalid"].as_u64()), (Some(1), Some(0)), "{r}");
|
||||
assert_eq!(r["set"], super::MARKED_SET);
|
||||
let e = &r["elements"][0];
|
||||
assert_eq!((e["label"].as_u64(), e["kind"].as_str(), e["body"].as_str()), (Some(bad as u64), Some("hex8"), Some("Bar")), "{e}");
|
||||
assert!(e["failed"].as_array().unwrap().iter().any(|f| f["metric"] == "scaled_jacobian"), "{e}");
|
||||
let at: Vec<f64> = e["at"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap()).collect();
|
||||
assert!(at[0] > 1.0 && at[0] < 2.0, "{at:?}");
|
||||
assert!(r["summary"].as_str().unwrap().starts_with("1 of 2 elements marked"), "{r}");
|
||||
assert_eq!(s.model.set(super::MARKED_SET).unwrap().elements, [bad]);
|
||||
assert_eq!(crate::view::marked(&s).into_iter().collect::<Vec<_>>(), [bad]);
|
||||
// The picture draws its faces in the marker colour, and only its.
|
||||
let p = crate::view::Palette::LIGHT;
|
||||
let (scene, _, _) = crate::view::scene(&s, &p);
|
||||
let red = scene.tris.iter().filter(|t| t.color == p.marked).count();
|
||||
assert_eq!(red, 5 * 2, "the bad hex's outer faces (all but the shared one), as triangles");
|
||||
// Without limits the (valid) element passes and the set goes.
|
||||
let all_off = json!({"limits": {"scaled_jacobian": null, "min_dihedral": null, "max_dihedral": null, "aspect_ratio": null, "min_angle": null, "max_angle": null}});
|
||||
let r = s.execute("mesh.quality.mark", &all_off).unwrap();
|
||||
assert_eq!(r["marked"], 0, "{r}");
|
||||
assert!(s.model.set(super::MARKED_SET).is_none());
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.model.set(super::MARKED_SET).unwrap().elements, [bad], "undo restores the marks");
|
||||
// Only the elements asked about.
|
||||
let other = s.model.elements().keys().copied().find(|l| *l != bad).unwrap();
|
||||
assert_eq!(s.execute("mesh.quality.mark", &json!({"elements": [other]})).unwrap()["marked"], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mark_rejects_hostile_parameters() {
|
||||
let mut s = Session::default();
|
||||
assert!(s.execute("mesh.quality.mark", &json!({})).is_err(), "needs a mesh");
|
||||
let mut s = mark_session();
|
||||
for p in [
|
||||
json!({"limits": {"nope": 1}}),
|
||||
json!({"limits": {"scaled_jacobian": "low"}}),
|
||||
json!({"limits": {"scaled_jacobian": true}}),
|
||||
json!({"limits": [0.2]}),
|
||||
json!({"limits": {"x".repeat(10000): 1}}),
|
||||
json!({"elements": [0]}),
|
||||
json!({"elements": [99]}),
|
||||
json!({"elements": "all"}),
|
||||
] {
|
||||
assert!(s.execute("mesh.quality.mark", &p).is_err(), "{p}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reports_quality_and_inverted_elements() {
|
||||
let mut s = Session::default();
|
||||
@@ -787,7 +1097,7 @@ mod tests {
|
||||
SurfaceMesh { positions: p.clone(), triangles: t.clone(), tri_face: vec![0, 1, 1, 1], weld_distance: 0.0, ..SurfaceMesh::default() };
|
||||
let open = SurfaceMesh { positions: p, triangles: t[..3].to_vec(), tri_face: vec![0, 1, 1], weld_distance: 0.0, ..SurfaceMesh::default() };
|
||||
for mesh in [tet, open] {
|
||||
s.surfaces.push(Arc::new(TaggedSurface { name: "Tet".into(), mesh, faces: 2 }));
|
||||
s.surfaces.push(Arc::new(TaggedSurface { name: "Tet".into(), mesh, faces: 2, original: None }));
|
||||
}
|
||||
let r = s.execute("mesh.surface.check", &json!({"surface": 0, "feature_angle": 180})).unwrap();
|
||||
assert_eq!((r["valid"].clone(), r["feature_edges"].clone()), (json!(true), json!({"face_boundary": 3, "sharp": 0})));
|
||||
@@ -1020,7 +1330,7 @@ mod tests {
|
||||
let open = SurfaceMesh { positions: p, triangles: t[..3].to_vec(), tri_face: vec![0, 1, 1], weld_distance: 0.0, ..SurfaceMesh::default() };
|
||||
let mut s = Session::default();
|
||||
for mesh in [tet, open] {
|
||||
s.surfaces.push(Arc::new(TaggedSurface { name: "Tet".into(), mesh, faces: 2 }));
|
||||
s.surfaces.push(Arc::new(TaggedSurface { name: "Tet".into(), mesh, faces: 2, original: None }));
|
||||
}
|
||||
let r = s.execute("mesh.tet.generate", &json!({"surface": 0, "quality": false})).unwrap();
|
||||
assert_eq!((r["elements"].clone(), r["nodes"].clone(), r["boundary_faces"].clone()), (json!(1), json!(4), json!(4)));
|
||||
|
||||
@@ -9,8 +9,10 @@
|
||||
use crate::schema::ProblemKind;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use fieldcraft_geometry::Shape;
|
||||
use fieldcraft_geometry::{Design, Shape};
|
||||
use fieldcraft_mesh::mirror::{mirror, reflected_edges, reflected_faces};
|
||||
use fieldcraft_mesh::surface::{BoundarySurface, SurfaceOptions};
|
||||
use fieldcraft_mesh::tet::geometry::Tessellation;
|
||||
use fieldcraft_model::geometry::Entity;
|
||||
use fieldcraft_model::{ElementFace, GeometryRef, Model};
|
||||
use serde_json::{Value, json};
|
||||
@@ -21,7 +23,7 @@ use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
pub const MIRROR: CommandSpec = CommandSpec {
|
||||
id: "mesh.mirror",
|
||||
label: "Mirror Mesh",
|
||||
params: r#"{"plane": "Sym" (a datum plane) or {"point": [x, y, z], "normal": [nx, ny, nz]}, "bodies": ["Part/a"] (default: every body the mesh is tagged with), "as": {"Part/a": "Part/b"} (optional: the body each image is; default the design's body at the reflected place, else "<body>/mirror")} — replaces each image body's mesh (as mesh.tet.generate) with the reflection of the bodies' elements (tet4, tet10, wedge6, hex8, hex20) across the plane: each copy right-handed (its nodes in reflected order), the copies of the nodes on the plane merged with them so the mesh is conforming there, geometry tags carried to the image (its body, and on it the faces the reflected faces lie on, so sets by face name select both halves). Reports the elements and nodes added, the nodes shared on the plane and each image's body"#,
|
||||
params: r#"{"plane": "Sym" (a datum plane) or {"point": [x, y, z], "normal": [nx, ny, nz]}, "bodies": ["Part/a"] (default: every body the mesh is tagged with), "as": {"Part/a": "Part/b"} (optional: the body each image is; default the design's body at the reflected place, else "<body>/mirror")} — replaces each image body's mesh (as mesh.tet.generate) with the reflection of the bodies' elements (tet4, tet10, wedge6, pyramid5, hex8, hex20) across the plane: each copy right-handed (its nodes in reflected order), the copies of the nodes on the plane merged with them so the mesh is conforming there, geometry tags carried to the image (its body, and on it the faces the reflected faces lie on, so sets by face name select both halves). Reports the elements and nodes added, the nodes shared on the plane and each image's body"#,
|
||||
form: Some(super::forms::MIRROR),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Mesh],
|
||||
@@ -52,6 +54,11 @@ fn plane(s: &Session, p: &Value) -> Result<([f64; 3], [f64; 3])> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The distance between two points.
|
||||
fn dist(a: [f64; 3], b: [f64; 3]) -> f64 {
|
||||
(0..3).map(|k| (a[k] - b[k]).powi(2)).sum::<f64>().sqrt()
|
||||
}
|
||||
|
||||
fn reflect(x: [f64; 3], point: [f64; 3], n: [f64; 3]) -> [f64; 3] {
|
||||
let d: f64 = (0..3).map(|i| (x[i] - point[i]) * n[i]).sum();
|
||||
[x[0] - 2.0 * d * n[0], x[1] - 2.0 * d * n[1], x[2] - 2.0 * d * n[2]]
|
||||
@@ -165,7 +172,7 @@ fn mirror_mesh(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let cleared = super::replace::clear(model, image.values().map(|(name, _)| name.as_str()));
|
||||
let before = model.clone();
|
||||
let r = mirror(model, ®ion, point, n, ON_PLANE * diagonal).map_err(|e| EngineError::Params(e.to_string()))?;
|
||||
tag_image(model, &before, &r, &image, point, n, diagonal)?;
|
||||
tag_image(model, &before, &r, &image, design.as_deref(), point, n, diagonal)?;
|
||||
let added_nodes = model.nodes().len() - before.nodes().len();
|
||||
let images: Vec<Value> = image.iter().map(|(b, (t, shape))| json!({"body": b, "image": t, "in_design": shape.is_some()})).collect();
|
||||
let reply = json!({
|
||||
@@ -185,50 +192,66 @@ fn tag_image(
|
||||
before: &Model,
|
||||
r: &fieldcraft_mesh::mirror::Mirrored,
|
||||
image: &BTreeMap<String, (String, Option<&Shape>)>,
|
||||
design: Option<&Design>,
|
||||
point: [f64; 3],
|
||||
n: [f64; 3],
|
||||
diagonal: f64,
|
||||
) -> Result<()> {
|
||||
let c = before.classification();
|
||||
let entity = |t: u32| c.entity(t).cloned();
|
||||
// A source face's image: by the image body's face under its reflected centroid, one lookup per
|
||||
// face name (the first element face of it stands for all).
|
||||
let mut face_points: BTreeMap<(String, String), [f64; 3]> = BTreeMap::new();
|
||||
for (&e, tags) in c.elements() {
|
||||
if !r.elements.contains_key(&e) {
|
||||
continue;
|
||||
}
|
||||
for (&f, &t) in &tags.faces {
|
||||
let Some(GeometryRef { body, entity: Entity::Face(name) }) = entity(t) else { continue };
|
||||
if face_points.contains_key(&(body.clone(), name.clone())) {
|
||||
continue;
|
||||
// Each image body's faces, to find the one each reflected boundary face lies on. A source
|
||||
// face's image is the set of image faces its own reflection covers (sampled at its
|
||||
// triangles' centres: one face for an exact mirror, pieces where the image is split
|
||||
// differently); each element face goes to the one of them nearest its reflected centre. So
|
||||
// an element face the mesher gave a face narrower than the elements (its centre on a
|
||||
// neighbour) keeps that face's image.
|
||||
let tolerance = (1e-3 * diagonal).max(1e-4);
|
||||
let mut targets: BTreeMap<&str, (Tessellation, Vec<String>)> = BTreeMap::new();
|
||||
for (target, shape) in image.values() {
|
||||
let Some(shape) = shape else { continue };
|
||||
let mesh = shape.tessellate(tolerance)?;
|
||||
let options = SurfaceOptions { weld_distance: mesh.weld_distance, ..SurfaceOptions::default() };
|
||||
let surface = BoundarySurface::new(&mesh.positions, &mesh.triangles, &mesh.tri_face, &options)
|
||||
.map_err(|e| EngineError::Params(format!("body `{target}`: {e}")))?;
|
||||
let names = shape.faces().iter().map(|f| f.name.clone()).collect();
|
||||
targets.insert(target.as_str(), (Tessellation::new(&surface, Vec::new()), names));
|
||||
}
|
||||
// The image faces each source face's reflection covers, by (source body, face name).
|
||||
let mut covers: BTreeMap<(String, String), Vec<u32>> = BTreeMap::new();
|
||||
for (body, (target, _)) in image {
|
||||
let (Some(shape), Some((tess, _))) = (design.and_then(|d| d.body(body)), targets.get(target.as_str())) else { continue };
|
||||
let mesh = shape.tessellate(tolerance)?;
|
||||
let at = |v: u32| mesh.positions.get(v as usize).copied().unwrap_or_default();
|
||||
for (t, &f) in mesh.triangles.iter().zip(&mesh.tri_face) {
|
||||
let Some(name) = shape.faces().get(f as usize).map(|x| x.name.clone()) else { continue };
|
||||
let centre = [0, 1, 2].map(|k| (at(t[0])[k] + at(t[1])[k] + at(t[2])[k]) / 3.0);
|
||||
let Some(hit) = tess.nearest_face(reflect(centre, point, n)) else { continue };
|
||||
let list = covers.entry((body.clone(), name)).or_default();
|
||||
if !list.contains(&hit) {
|
||||
list.push(hit);
|
||||
}
|
||||
let Some(nodes) = before.face_nodes(ElementFace { element: e, face: f }) else { continue };
|
||||
}
|
||||
}
|
||||
let image_face = |e: u32, f: u8, body: &str, name: &str| -> Option<(String, String)> {
|
||||
let (target, _) = image.get(body)?;
|
||||
let mapped = targets.get(target.as_str()).and_then(|(tess, names)| {
|
||||
let nodes = before.face_nodes(ElementFace { element: e, face: f })?;
|
||||
let pts: Vec<[f64; 3]> = nodes.iter().filter_map(|n| before.node(*n).map(|x| x.xyz)).collect();
|
||||
let k = pts.len().max(1) as f64;
|
||||
let centre = [0, 1, 2].map(|i| pts.iter().map(|p| p[i]).sum::<f64>() / k);
|
||||
face_points.insert((body, name), reflect(centre, point, n));
|
||||
}
|
||||
}
|
||||
let mut face_names: BTreeMap<(String, String), (String, String)> = BTreeMap::new();
|
||||
let mut by_body: BTreeMap<String, Vec<(String, [f64; 3])>> = BTreeMap::new();
|
||||
for ((body, name), p) in &face_points {
|
||||
by_body.entry(body.clone()).or_default().push((name.clone(), *p));
|
||||
}
|
||||
for (body, faces) in by_body {
|
||||
let Some((target, shape)) = image.get(&body) else { continue };
|
||||
let found = match shape {
|
||||
Some(shape) => {
|
||||
let pts: Vec<[f64; 3]> = faces.iter().map(|(_, p)| *p).collect();
|
||||
shape.nearest_faces(&pts, (1e-3 * diagonal).max(1e-4))?
|
||||
}
|
||||
None => vec![None; faces.len()],
|
||||
};
|
||||
for ((name, _), f) in faces.iter().zip(found) {
|
||||
let mapped = f.and_then(|f| shape.and_then(|s| s.faces().get(f as usize))).map_or_else(|| name.clone(), |x| x.name.clone());
|
||||
face_names.insert((body.clone(), name.clone()), (target.clone(), mapped));
|
||||
}
|
||||
}
|
||||
let p = reflect([0, 1, 2].map(|i| pts.iter().map(|p| p[i]).sum::<f64>() / k), point, n);
|
||||
let face = match covers.get(&(body.to_string(), name.to_string())).map(Vec::as_slice) {
|
||||
Some([only]) => Some(*only),
|
||||
Some(several @ [_, _, ..]) => several
|
||||
.iter()
|
||||
.filter_map(|&g| tess.project_trimmed(g, p).map(|q| (dist(p, q), g)))
|
||||
.min_by(|a, b| a.0.total_cmp(&b.0))
|
||||
.map(|(_, g)| g),
|
||||
_ => tess.nearest_face(p),
|
||||
};
|
||||
face.and_then(|i| names.get(i as usize)).cloned()
|
||||
});
|
||||
Some((target.clone(), mapped.unwrap_or_else(|| name.to_string())))
|
||||
};
|
||||
let mut tags: BTreeMap<GeometryRef, u32> = BTreeMap::new();
|
||||
let mut tag = |model: &mut Model, r: GeometryRef| -> Result<u32> {
|
||||
if let Some(&t) = tags.get(&r) {
|
||||
@@ -251,7 +274,7 @@ fn tag_image(
|
||||
}
|
||||
for (&f, &ft) in &t.faces {
|
||||
let Some(GeometryRef { body, entity: Entity::Face(name) }) = entity(ft) else { continue };
|
||||
let Some((tb, tn)) = face_names.get(&(body, name)) else { continue };
|
||||
let Some((tb, tn)) = image_face(e, f, &body, &name) else { continue };
|
||||
let g = faces.get(usize::from(f).saturating_sub(1)).copied().unwrap_or(f);
|
||||
let tg = tag(model, GeometryRef::face(tb.as_str(), tn.as_str())?)?;
|
||||
model.tag_element_face(copy, g, tg)?;
|
||||
|
||||
@@ -5,9 +5,11 @@ mod cells;
|
||||
mod contact;
|
||||
pub(crate) mod datum;
|
||||
pub(crate) mod deck;
|
||||
mod defeature;
|
||||
pub(crate) mod define;
|
||||
pub mod diagnostics;
|
||||
mod edit;
|
||||
mod extrude;
|
||||
pub(crate) mod file;
|
||||
pub(crate) mod forms;
|
||||
mod geometry;
|
||||
@@ -18,20 +20,27 @@ pub use datum::edit_params as datum_edit_params;
|
||||
pub use datum::preview as datum_preview;
|
||||
pub use mesh::analytic_surfaces;
|
||||
pub use nodes::merge_preview as node_merge_preview;
|
||||
pub use plan::untagged_faces;
|
||||
pub use relax::{Preview as RelaxPreview, preview as relax_preview, sphere as relax_sphere};
|
||||
mod mirror;
|
||||
mod model;
|
||||
pub(crate) mod nodes;
|
||||
pub(crate) mod octree;
|
||||
pub(crate) mod plan;
|
||||
mod project;
|
||||
pub(crate) mod relax;
|
||||
pub mod replace;
|
||||
mod results;
|
||||
pub(crate) mod seeds;
|
||||
pub(crate) mod sizes;
|
||||
pub(crate) mod solve;
|
||||
pub(crate) mod stitch;
|
||||
mod symmetry;
|
||||
mod tags;
|
||||
pub mod update;
|
||||
pub(crate) mod view;
|
||||
mod virtual_faces;
|
||||
pub mod xy;
|
||||
|
||||
use crate::schema::ProblemKind;
|
||||
use fieldcraft_model::MAX_LABEL;
|
||||
@@ -46,7 +55,9 @@ pub static ALL: &[CommandSpec] = &[
|
||||
model::INFO,
|
||||
geometry::OPEN,
|
||||
geometry::BODIES,
|
||||
geometry::FEATURES,
|
||||
geometry::CENTRE,
|
||||
geometry::CHECK,
|
||||
geometry::TESSELLATE,
|
||||
geometry::IMPORT_MESH,
|
||||
datum::DATUM_PLANE,
|
||||
@@ -64,12 +75,24 @@ pub static ALL: &[CommandSpec] = &[
|
||||
datum::DATUM_SUPPRESS,
|
||||
datum::PARTITION_SUPPRESS,
|
||||
datum::PARTITION_DELETE,
|
||||
virtual_faces::FACES_MERGE,
|
||||
virtual_faces::FACES_MERGE_DELETE,
|
||||
virtual_faces::FACE_ABSORB,
|
||||
virtual_faces::FACE_SPLIT,
|
||||
virtual_faces::FACE_SPLIT_DELETE,
|
||||
defeature::DEFEATURE,
|
||||
defeature::DEFEATURE_DELETE,
|
||||
defeature::DEFEATURE_PROPOSE,
|
||||
stitch::STITCH,
|
||||
stitch::STITCH_DELETE,
|
||||
mesh::NODE_ADD,
|
||||
mesh::NODE_REMOVE,
|
||||
mesh::ELEMENT_ADD,
|
||||
mesh::ELEMENT_REMOVE,
|
||||
replace::CLEAR,
|
||||
mesh::QUALITY,
|
||||
mesh::QUALITY_MARK,
|
||||
mesh::EXPORT_STL,
|
||||
mesh::DELAUNAY_POINTS,
|
||||
mesh::SURFACE_CHECK,
|
||||
mesh::PLANAR,
|
||||
@@ -80,10 +103,18 @@ pub static ALL: &[CommandSpec] = &[
|
||||
hex::HEX_SWEEP,
|
||||
hex::HEX_MAPPED,
|
||||
hex::HEX_BLOCKS,
|
||||
project::HEX_PROJECT,
|
||||
extrude::HEX_EXTRUDE,
|
||||
cells::CELLS,
|
||||
plan::PLAN,
|
||||
plan::APPLY,
|
||||
seeds::SEED_EDGE,
|
||||
seeds::SEED_CLEAR,
|
||||
seeds::SEED_LIST,
|
||||
sizes::SIZE_SET,
|
||||
sizes::SIZE_GROWTH,
|
||||
sizes::SIZE_CLEAR,
|
||||
sizes::SIZE_LIST,
|
||||
mirror::MIRROR,
|
||||
nodes::MERGE,
|
||||
nodes::DUPLICATES,
|
||||
@@ -173,7 +204,9 @@ pub static ALL: &[CommandSpec] = &[
|
||||
results::FRAME,
|
||||
results::REACTIONS,
|
||||
results::EXPORT_CSV,
|
||||
results::EXPORT_VTU,
|
||||
results::CLEAR,
|
||||
xy::XY,
|
||||
diagnostics::CRASH_REPORTS,
|
||||
diagnostics::CRASH_REPORT,
|
||||
diagnostics::CRASH_REPORTS_DELETE,
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
//! imported surfaces.
|
||||
|
||||
use fieldcraft_mesh::octree::{OctreeHexMesh, OctreeOptions, octree_hex_mesh};
|
||||
use fieldcraft_mesh::size::UniformSize;
|
||||
use fieldcraft_mesh::surface::BoundarySurface;
|
||||
use fieldcraft_model::{ElementKind, GeometryRef, MAX_ELEMENTS};
|
||||
use serde_json::{Value, json};
|
||||
@@ -15,7 +14,7 @@ use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
pub const HEX_OCTREE: CommandSpec = CommandSpec {
|
||||
id: "mesh.hex.octree",
|
||||
label: "Octree Hex Mesh",
|
||||
params: r#"{"body": "Body1" (default: every body of the open design) or "surface": 0 (an imported surface), "size": 2.0 (element size in the interior, mm; default 1/25 of the bounding-box diagonal), "tolerance": 0.05 (chordal tolerance of the tessellation, mm; default 1e-3 of the diagonal), "max_depth": 3 (refinement levels below the size, each a third, 0..=5), "max_elements": 2000000 (1..=10000000; default the session limit), "attempts": 4 (builds, refining where one failed, 1..=8), "max_sweeps": 300 (optimiser sweeps, 0..=10000), and the boundary options of mesh.tet.generate ("weld", "weld_distance", "feature_angle")} — replaces the meshes of the bodies it meshes (as mesh.tet.generate; other bodies keep theirs) with an unstructured all-hex8 mesh of any part: an adaptive 3-refinement grid with conforming transition templates, a core inside the part, two boundary layers that capture every B-rep face boundary and sharp edge, and untangling. Every element is proven valid; boundary element faces are tagged with their B-rep faces (named as in geometry.bodies; by index when a face has no name), so sets by geometry resolve. Reports per body the elements, quality, volume and grid statistics"#,
|
||||
params: r#"{"body": "Body1" (default: every body of the open design) or "surface": 0 (an imported surface), "size": 2.0 (element size in the interior, mm; default 1/25 of the bounding-box diagonal), "tolerance": 0.05 (chordal tolerance of the tessellation, mm; default 1e-3 of the diagonal), "max_depth": 4 (the most refinement levels below the size, each a third, 0..=5; the levels the part's thinnest wall, narrowest face and smallest local size (mesh.size.set) need are found before meshing, and a part that needs more levels or more than max_elements cells fails at once, naming them), "max_elements": 2000000 (1..=10000000; default the session limit), "attempts": 4 (builds, refining where one failed, 1..=8), "max_sweeps": 300 (optimiser sweeps, 0..=10000), and the boundary options of mesh.tet.generate ("weld", "weld_distance", "feature_angle")} — replaces the meshes of the bodies it meshes (as mesh.tet.generate; other bodies keep theirs) with an unstructured all-hex8 mesh of any part: an adaptive 3-refinement grid (root cells at `size`, refined where the size controls ask for smaller elements) with conforming transition templates, a core inside the part, two boundary layers that capture every B-rep face boundary and sharp edge, and untangling. Every element is proven valid; boundary element faces are tagged with their B-rep faces (named as in geometry.bodies; by index when a face has no name), so sets by geometry resolve. Reports per body the elements, quality, volume and grid statistics (root cell size, levels, the thinnest wall and narrowest face, cells per level after refinement and after closure)"#,
|
||||
form: Some(super::forms::HEX_OCTREE),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Geometry],
|
||||
@@ -46,13 +45,14 @@ fn hex_octree(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
..defaults
|
||||
};
|
||||
let bodies = inputs(s, p)?;
|
||||
let sizing = super::sizes::sizing(s)?;
|
||||
let mut meshes: Vec<(Input, OctreeHexMesh)> = Vec::with_capacity(bodies.len());
|
||||
let mut total = 0usize;
|
||||
for b in bodies {
|
||||
if !(b.diagonal > 0.0 && b.diagonal.is_finite()) {
|
||||
return Err(EngineError::Params(format!("body `{}` has no extent", b.name)));
|
||||
}
|
||||
let h = size.unwrap_or(DEFAULT_RELATIVE_SIZE * b.diagonal);
|
||||
let h = sizing.body_size(&b.name, size.unwrap_or(DEFAULT_RELATIVE_SIZE * b.diagonal));
|
||||
if h < MIN_RELATIVE_SIZE * b.diagonal {
|
||||
return Err(EngineError::Params(format!("`size` must be at least {} mm for body `{}`", MIN_RELATIVE_SIZE * b.diagonal, b.name)));
|
||||
}
|
||||
@@ -60,7 +60,8 @@ fn hex_octree(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let surface = BoundarySurface::new(&b.mesh.positions, &b.mesh.triangles, &b.mesh.tri_face, &surface_options)
|
||||
.map_err(|e| EngineError::Params(format!("body `{}`: {e}", b.name)))?;
|
||||
let left = OctreeOptions { max_elements: options.max_elements.saturating_sub(total).max(1), ..options.clone() };
|
||||
let mesh = octree_hex_mesh(&surface, &UniformSize(h), &left).map_err(|e| EngineError::of_body(&b.name, e))?;
|
||||
let field = sizing.field(&b.name, h)?;
|
||||
let mesh = octree_hex_mesh(&surface, &field, &left).map_err(|e| EngineError::of_body(&b.name, e))?;
|
||||
total += mesh.hexes.len();
|
||||
meshes.push((b, mesh));
|
||||
}
|
||||
@@ -90,7 +91,7 @@ fn hex_octree(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
"quality": {"min_scaled_jacobian": r.min_scaled_jacobian, "mean_scaled_jacobian": r.mean_scaled_jacobian},
|
||||
"volume": r.volume,
|
||||
"surface_volume": r.surface_volume,
|
||||
"grid": {"cells": r.grid_cells, "core_elements": r.core_elements, "layer_elements": r.layer_elements, "frame": r.frame},
|
||||
"grid": {"cells": r.grid_cells, "root_size": r.root_size, "levels": r.levels, "thinnest_wall": finite(r.thinnest_wall), "narrowest_patch": finite(r.narrowest_patch), "refined_per_level": r.refined_cells, "closed_per_level": r.closed_cells, "core_elements": r.core_elements, "layer_elements": r.layer_elements, "frame": r.frame},
|
||||
"features": {"corners": r.corners, "curves": r.curves, "patches": r.patches},
|
||||
"attempts": r.attempts,
|
||||
"retries": r.retries,
|
||||
@@ -102,6 +103,11 @@ fn hex_octree(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(cleared.report(s, json!({"bodies": report, "nodes": nodes, "elements": elements})))
|
||||
}
|
||||
|
||||
/// A measure that may be infinite (no wall, no bounded face) as JSON: `null` when it is.
|
||||
fn finite(x: f64) -> Value {
|
||||
if x.is_finite() { json!(x) } else { Value::Null }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -0,0 +1,832 @@
|
||||
//! The plan's block topology for the projection blocking mesher (#402 step 3, #399): the
|
||||
//! translation of a mesh plan into one block topology over the whole body, so the rings round
|
||||
//! its circles and the blend layer along its fillets, chamfers and shell walls come out of one
|
||||
//! conforming mesh (no partitions, no ties, one division count per sheet of blocks).
|
||||
//!
|
||||
//! The body is read in a frame whose third axis is the plan's sweep direction (a principal axis):
|
||||
//! - **Grid.** The planes of the body's planar faces square to each axis (its sides, a shell's
|
||||
//! inner walls and floor, a boss's top, a blind hole's bottom) and the frames round its
|
||||
//! circles make a grid of blocks over the body.
|
||||
//! - **Rings.** The frame cell round each circle (concentric circles together) is O-gridded
|
||||
//! through every level, square inside square: the frame, then a square with its corners at
|
||||
//! 45° on each circle, then a core. Corners on a circle sit on it before projection, so the
|
||||
//! ring's radial block edges meet the cylinder square on. A hole's frame is centred on it: a
|
||||
//! side of the body near the hole is the frame there and the far side is as far; a fillet
|
||||
//! round a corner of the outline that holds a hole (a screw hole in a rounded corner) is taken
|
||||
//! in by the hole's ring, its frame reaching the two sides and its outer corner projected onto
|
||||
//! the fillet's middle, so the corner needs no blend layer. A boss the body's walls run into
|
||||
//! (planes of the body crossing its circle outside it) is framed by the square round its
|
||||
//! whole circle, subdivided by those planes, every square inside it divided alike so the
|
||||
//! ring's blocks meet the walls node to node; on the walls' side the frame stops at their
|
||||
//! inner plane and its corner projects onto the inner fillet's middle (a boss just touching
|
||||
//! the fillet included).
|
||||
//! - **Blend layer.** A side of the body a fillet or chamfer runs along, or that a shell's wall
|
||||
//! stands on, gets a layer of blocks between the grid (on the body shrunk by the blend's
|
||||
//! depth or the wall's thickness) and the side, one block per boundary face of the grid, the
|
||||
//! layer's vertices pushed out square to every side they lie on: diagonally at an edge and a
|
||||
//! corner. Projected, a corner vertex lands on the fillet (or chamfer) at its middle, so the
|
||||
//! two blocks meeting there share the blend's 180° as two right angles; on a shell's inner
|
||||
//! corner, as three 120°-ish ones. The layer's depth is the blend's measured depth (its
|
||||
//! stored radius or distance) or the wall's thickness; its divisions follow the element size.
|
||||
//! - **Carving.** Every block whose middle lies outside the body (a cavity, the space round a
|
||||
//! boss, a hole's core) is removed, by the generalized winding number of the body's
|
||||
//! tessellation (solid angles, Van Oosterom and Strackee 1983).
|
||||
//!
|
||||
//! The projection mesher then projects the outer block faces onto the body's faces (each the
|
||||
//! smooth group nearest it) and smooths the grid. Parallel block edges share one division count
|
||||
//! across the whole topology, so neighbouring blocks always match: no shear bands, no ties.
|
||||
//!
|
||||
//! A body this layout does not fit (a face at an angle to the axes that is not a chamfer, a
|
||||
//! curved face that is neither a circle's cylinder nor a blend, two circles in one frame) is
|
||||
//! reported with the reason; the plan then keeps its frame cuts and swept cells.
|
||||
|
||||
use fieldcraft_geometry::{FaceSurface, Shape, SurfaceMesh};
|
||||
use fieldcraft_mesh::project::Topology;
|
||||
use fieldcraft_model::{PlanRing, RingKind};
|
||||
|
||||
type V3 = [f64; 3];
|
||||
|
||||
/// Most blocks a plan's layout makes before carving.
|
||||
const MAX_LAYOUT_BLOCKS: usize = 20_000;
|
||||
/// Most triangles the winding number classification sums over per point.
|
||||
const MAX_CLASSIFY_TRIANGLES: usize = 400_000;
|
||||
|
||||
/// A body's block layout.
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct Layout {
|
||||
pub(crate) topology: Topology,
|
||||
/// Circles ringed (concentric ones each).
|
||||
pub(crate) rings: usize,
|
||||
/// The sides with a blend layer: (side, depth, what).
|
||||
pub(crate) layers: Vec<(String, f64, &'static str)>,
|
||||
/// Grid planes per axis of the frame (across, across, along the sweep).
|
||||
pub(crate) planes: [usize; 3],
|
||||
/// The frame round each circle group: its axis point (the sweep's coordinate 0) and the
|
||||
/// smallest half-side of the frame.
|
||||
pub(crate) frames: Vec<(V3, f64)>,
|
||||
/// The ring-and-pillow layout ([`super::pillow`]): the pillow's least thickness along the walls.
|
||||
pub(crate) pillow: Option<f64>,
|
||||
}
|
||||
|
||||
impl Layout {
|
||||
/// The half-side of the frame round the circle about the axis through `center` along the
|
||||
/// sweep `direction`.
|
||||
pub(crate) fn frame_of(&self, center: V3, direction: V3) -> Option<f64> {
|
||||
self.frames.iter().find_map(|(p, half)| {
|
||||
let off = sub(center, *p);
|
||||
let along = dot(off, direction);
|
||||
(dot(off, off) - along * along < 1e-12 * dot(center, center).max(1.0)).then_some(*half)
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn summary(&self) -> String {
|
||||
let layers: Vec<String> = self.layers.iter().map(|(s, d, w)| format!("{s} {d:.4} mm ({w})")).collect();
|
||||
format!(
|
||||
"projection blocking: {} blocks over a {}×{}×{} grid, {} circle(s) ringed{}{}",
|
||||
self.topology.blocks.len(),
|
||||
self.planes[0].saturating_sub(1),
|
||||
self.planes[1].saturating_sub(1),
|
||||
self.planes[2].saturating_sub(1),
|
||||
self.rings,
|
||||
if layers.is_empty() { String::new() } else { format!(", a blend layer on {}", layers.join(", ")) },
|
||||
self.pillow.map_or_else(String::new, |t| format!(", a pillow layer from the carved grid to the walls ({t:.4} mm and more)"))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// What the layout reads.
|
||||
pub(crate) struct Input<'a> {
|
||||
pub(crate) shape: &'a Shape,
|
||||
pub(crate) surfaces: &'a [Option<FaceSurface>],
|
||||
pub(crate) mesh: &'a SurfaceMesh,
|
||||
pub(crate) direction: V3,
|
||||
pub(crate) rings: &'a [PlanRing],
|
||||
/// The faces fillets and chamfers made (indices on `shape`).
|
||||
pub(crate) blends: Vec<u32>,
|
||||
/// A shell's wall thickness.
|
||||
pub(crate) shell: Option<f64>,
|
||||
pub(crate) size: f64,
|
||||
}
|
||||
|
||||
/// The local frame: local axis `a` is world axis `PERM[k][a]`, `k` the sweep's world axis (a
|
||||
/// cyclic permutation, so blocks keep their handedness).
|
||||
pub(super) const PERM: [[usize; 3]; 3] = [[1, 2, 0], [2, 0, 1], [0, 1, 2]];
|
||||
|
||||
const SIDE_NAMES: [[&str; 2]; 3] = [["-x", "+x"], ["-y", "+y"], ["-z", "+z"]];
|
||||
|
||||
fn dot(a: V3, b: V3) -> f64 {
|
||||
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
|
||||
}
|
||||
|
||||
fn sub(a: V3, b: V3) -> V3 {
|
||||
[a[0] - b[0], a[1] - b[1], a[2] - b[2]]
|
||||
}
|
||||
|
||||
fn norm(a: V3) -> f64 {
|
||||
dot(a, a).sqrt()
|
||||
}
|
||||
|
||||
fn cross(a: V3, b: V3) -> V3 {
|
||||
[a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]
|
||||
}
|
||||
|
||||
/// Twice the solid angle triangle `a b c` subtends at the origin, signed (Van Oosterom and
|
||||
/// Strackee 1983: tan(Ω/2) = a·(b×c) / (|a||b||c| + (a·b)|c| + (a·c)|b| + (b·c)|a|)).
|
||||
fn solid_angle(a: V3, b: V3, c: V3) -> f64 {
|
||||
let (la, lb, lc) = (norm(a), norm(b), norm(c));
|
||||
let num = dot(a, cross(b, c));
|
||||
let den = la * lb * lc + dot(a, b) * lc + dot(a, c) * lb + dot(b, c) * la;
|
||||
2.0 * num.atan2(den)
|
||||
}
|
||||
|
||||
/// The generalized winding number of the closed surface `mesh` at `p`: 1 inside, 0 outside.
|
||||
pub(super) fn winding(mesh: &SurfaceMesh, p: V3) -> f64 {
|
||||
let at = |i: u32| mesh.positions.get(i as usize).map(|q| sub(*q, p));
|
||||
let mut sum = 0.0;
|
||||
for t in mesh.triangles.iter().take(MAX_CLASSIFY_TRIANGLES) {
|
||||
if let (Some(a), Some(b), Some(c)) = (at(t[0]), at(t[1]), at(t[2])) {
|
||||
sum += solid_angle(a, b, c);
|
||||
}
|
||||
}
|
||||
sum / (4.0 * std::f64::consts::PI)
|
||||
}
|
||||
|
||||
/// A circle's axis point and radius in the local frame, with its kind.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct Circle {
|
||||
c: [f64; 2],
|
||||
r: f64,
|
||||
kind: RingKind,
|
||||
}
|
||||
|
||||
/// Concentric circles framed together: radii descending.
|
||||
#[derive(Debug, Clone)]
|
||||
struct Group {
|
||||
c: [f64; 2],
|
||||
circles: Vec<Circle>,
|
||||
/// The frame's extent per local axis (0, 1): [low, high].
|
||||
frame: [[f64; 2]; 2],
|
||||
/// The frame's half-side per local axis when a corner blend it takes in sets it: the
|
||||
/// distance from the centre to the body's side the blend runs along.
|
||||
forced: [Option<f64>; 2],
|
||||
/// A boss the body's walls run into: planes of the body cross its circle (outside it, where
|
||||
/// the walls join), so its frame is the square round the whole circle, subdivided by those
|
||||
/// planes, and the ring's blocks follow the subdivision.
|
||||
patch: bool,
|
||||
/// The extent per local axis of the concave blends a patch takes in (the inner fillet where
|
||||
/// the walls meet, in pieces where the boss touches it): the frame's side on the blends'
|
||||
/// side of the centre goes to their middle.
|
||||
blends: Option<[[f64; 2]; 2]>,
|
||||
}
|
||||
|
||||
/// Plan the block topology of the body.
|
||||
// Axes index several per-axis arrays at once (planes, frames, layers).
|
||||
#[allow(clippy::needless_range_loop)]
|
||||
pub(crate) fn layout(inp: &Input) -> Result<Layout, String> {
|
||||
let d = inp.direction;
|
||||
let k = (0..3).find(|&k| (d[k].abs() - 1.0).abs() < 1e-9).ok_or("the sweep direction is not along an axis")?;
|
||||
let perm = PERM[k];
|
||||
let local = |p: V3| -> V3 { [p[perm[0]], p[perm[1]], p[perm[2]]] };
|
||||
let world = |l: V3| -> V3 {
|
||||
let mut w = [0.0; 3];
|
||||
for a in 0..3 {
|
||||
w[perm[a]] = l[a];
|
||||
}
|
||||
w
|
||||
};
|
||||
if inp.mesh.triangles.is_empty() || inp.mesh.triangles.len() > MAX_CLASSIFY_TRIANGLES {
|
||||
return Err(format!("the body's tessellation has {} triangles", inp.mesh.triangles.len()));
|
||||
}
|
||||
let (mut lo, mut hi) = ([f64::INFINITY; 3], [f64::NEG_INFINITY; 3]);
|
||||
let mut face_points: Vec<Vec<V3>> = vec![Vec::new(); inp.surfaces.len()];
|
||||
for (t, &f) in inp.mesh.triangles.iter().zip(&inp.mesh.tri_face) {
|
||||
for &i in t {
|
||||
let Some(p) = inp.mesh.positions.get(i as usize).map(|p| local(*p)) else { continue };
|
||||
for a in 0..3 {
|
||||
lo[a] = lo[a].min(p[a]);
|
||||
hi[a] = hi[a].max(p[a]);
|
||||
}
|
||||
if let Some(list) = face_points.get_mut(f as usize) {
|
||||
list.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
let diag = norm(sub(hi, lo));
|
||||
if !(diag.is_finite() && diag > 0.0) {
|
||||
return Err("the body has no extent".into());
|
||||
}
|
||||
let eps = 1e-6 * diag;
|
||||
let name = |f: usize| inp.shape.faces().get(f).map_or_else(|| format!("#{f}"), |x| x.name.clone());
|
||||
|
||||
// Circles: the plan's rings along the sweep, in the local frame.
|
||||
let mut circles: Vec<Circle> = Vec::new();
|
||||
for r in inp.rings {
|
||||
if (dot(r.axis, d).abs() - 1.0).abs() > 1e-6 {
|
||||
return Err(format!("the ring of F{} is not square to the sweep", r.feature));
|
||||
}
|
||||
let c = local(r.center);
|
||||
circles.push(Circle { c: [c[0], c[1]], r: r.radius, kind: r.kind });
|
||||
}
|
||||
let on_circle = |origin: V3, radius: f64| {
|
||||
let o = local(origin);
|
||||
circles.iter().any(|c| (c.r - radius).abs() < 1e-6 * radius.max(1.0) && (c.c[0] - o[0]).hypot(c.c[1] - o[1]) < 1e-6 * radius.max(1.0))
|
||||
};
|
||||
|
||||
// The faces: planes square to an axis (with their outward sign), the circles' cylinders, and
|
||||
// the blends; anything else is not this layout's.
|
||||
let mut planes_on: [Vec<(f64, f64)>; 3] = [Vec::new(), Vec::new(), Vec::new()];
|
||||
// The planar faces square to an in-plane axis: (axis, offset, face).
|
||||
let mut side_faces: Vec<(usize, f64, usize)> = Vec::new();
|
||||
let mut blend_faces: Vec<usize> = Vec::new();
|
||||
for (f, s) in inp.surfaces.iter().enumerate() {
|
||||
let blend = inp.blends.contains(&(f as u32));
|
||||
match s {
|
||||
Some(FaceSurface::Plane { normal, point }) => {
|
||||
let n = local(*normal);
|
||||
match (0..3).find(|&a| (n[a].abs() - 1.0).abs() < 1e-6) {
|
||||
Some(a) => {
|
||||
planes_on[a].push((local(*point)[a], n[a].signum()));
|
||||
if a < 2 {
|
||||
side_faces.push((a, local(*point)[a], f));
|
||||
}
|
||||
}
|
||||
None if blend => blend_faces.push(f),
|
||||
None => return Err(format!("face `{}` is a plane at an angle to the axes", name(f))),
|
||||
}
|
||||
}
|
||||
// A ring's cylinder, even where a fillet's history names it (a boss over a filleted
|
||||
// corner).
|
||||
Some(FaceSurface::Cylinder { origin, axis, radius }) if (dot(*axis, d).abs() - 1.0).abs() < 1e-6 && on_circle(*origin, *radius) => {}
|
||||
Some(FaceSurface::Cylinder { .. }) if !blend => {
|
||||
return Err(format!("face `{}` is a cylinder that is not one of the plan's rings", name(f)));
|
||||
}
|
||||
_ if blend => blend_faces.push(f),
|
||||
_ => return Err(format!("face `{}` is curved and neither a ring's cylinder nor a fillet's", name(f))),
|
||||
}
|
||||
}
|
||||
|
||||
// Circles, concentric ones grouped (radii descending).
|
||||
let mut groups: Vec<Group> = Vec::new();
|
||||
for c in &circles {
|
||||
match groups.iter_mut().find(|g| (g.c[0] - c.c[0]).hypot(g.c[1] - c.c[1]) < 1e-6 * c.r.max(1.0)) {
|
||||
Some(g) => {
|
||||
if !g.circles.iter().any(|x| (x.r - c.r).abs() < 1e-6 * c.r.max(1.0)) {
|
||||
g.circles.push(*c);
|
||||
}
|
||||
}
|
||||
None => groups.push(Group { c: c.c, circles: vec![*c], frame: [[0.0; 2]; 2], forced: [None; 2], patch: false, blends: None }),
|
||||
}
|
||||
}
|
||||
for g in &mut groups {
|
||||
g.circles.sort_by(|a, b| b.r.total_cmp(&a.r));
|
||||
let Some(outer) = g.circles.first().copied() else { continue };
|
||||
let r0 = outer.r;
|
||||
let crossing: Vec<usize> = side_faces.iter().filter(|(a, v, _)| (v - g.c[*a]).abs() < r0 - eps).map(|(_, _, f)| *f).collect();
|
||||
if outer.kind != RingKind::Butterfly || crossing.is_empty() {
|
||||
continue;
|
||||
}
|
||||
// The walls join the boss outside its circle: no face of theirs inside it.
|
||||
for f in crossing {
|
||||
let pts = face_points.get(f).map_or(&[][..], Vec::as_slice);
|
||||
if pts.iter().any(|p| (p[0] - g.c[0]).hypot(p[1] - g.c[1]) < r0 * (1.0 - 1e-3)) {
|
||||
return Err(format!("face `{}` runs through the circle of radius {r0:.4}", name(f)));
|
||||
}
|
||||
}
|
||||
g.patch = true;
|
||||
}
|
||||
|
||||
// The blend layer: per side, the depth of the blends along it, or the shell's wall. A blend
|
||||
// round a corner of the outline that a hole's ring can frame (a screw hole in a rounded
|
||||
// corner) is taken in by the ring instead.
|
||||
let mut ring = [[0.0f64; 2]; 3];
|
||||
let mut what = [[""; 2]; 3];
|
||||
for &f in &blend_faces {
|
||||
let pts = face_points.get(f).map_or(&[][..], Vec::as_slice);
|
||||
if pts.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if let Some((g, half)) = corner_ring(&groups, pts, lo, hi, eps)
|
||||
&& let Some(g) = groups.get_mut(g)
|
||||
{
|
||||
g.forced = half.map(Some);
|
||||
continue;
|
||||
}
|
||||
if let Some((g, extent)) = patch_blend(&groups, pts)
|
||||
&& let Some(g) = groups.get_mut(g)
|
||||
{
|
||||
let union = g.blends.map_or(extent, |b| std::array::from_fn(|a| [b[a][0].min(extent[a][0]), b[a][1].max(extent[a][1])]));
|
||||
g.blends = Some(union);
|
||||
continue;
|
||||
}
|
||||
let mut touched = false;
|
||||
for a in 0..3 {
|
||||
for s in 0..2 {
|
||||
let depth = pts.iter().map(|p| if s == 0 { p[a] - lo[a] } else { hi[a] - p[a] }).fold(0.0f64, f64::max);
|
||||
if depth < 0.45 * (hi[a] - lo[a]) {
|
||||
touched = true;
|
||||
if depth > ring[a][s] {
|
||||
ring[a][s] = depth;
|
||||
what[a][s] = "blend";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !touched {
|
||||
return Err(format!("the blend face `{}` does not run along a side of the body", name(f)));
|
||||
}
|
||||
}
|
||||
if let Some(t) = inp.shell.filter(|t| t.is_finite() && *t > 0.0) {
|
||||
// A wall: the inner face of the shell, `t` inside the side and facing away from it.
|
||||
for a in 0..3 {
|
||||
for s in 0..2 {
|
||||
let at = if s == 0 { lo[a] + t } else { hi[a] - t };
|
||||
let inward = if s == 0 { 1.0 } else { -1.0 };
|
||||
if planes_on[a].iter().any(|(v, sign)| (v - at).abs() < 1e-4 * t.max(1.0) && *sign == inward) {
|
||||
ring[a][s] = t;
|
||||
what[a][s] = "shell wall";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let ilo: V3 = std::array::from_fn(|a| lo[a] + ring[a][0]);
|
||||
let ihi: V3 = std::array::from_fn(|a| hi[a] - ring[a][1]);
|
||||
if (0..3).any(|a| !(ihi[a] - ilo[a] > eps)) {
|
||||
return Err("the blend layers leave no room inside the body".into());
|
||||
}
|
||||
|
||||
// The fixed planes per axis: faces square to it (a side with a layer moves in by its depth).
|
||||
let mut fixed: [Vec<f64>; 3] = [Vec::new(), Vec::new(), Vec::new()];
|
||||
for a in 0..3 {
|
||||
for &(v, _) in &planes_on[a] {
|
||||
let v = if ring[a][0] > 0.0 && (v - lo[a]).abs() < eps {
|
||||
ilo[a]
|
||||
} else if ring[a][1] > 0.0 && (v - hi[a]).abs() < eps {
|
||||
ihi[a]
|
||||
} else {
|
||||
v
|
||||
};
|
||||
if v < ilo[a] - eps || v > ihi[a] + eps {
|
||||
return Err(format!("a face square to {} lies inside the blend layer", SIDE_NAMES[perm[a]][0].trim_start_matches('-')));
|
||||
}
|
||||
fixed[a].push(v);
|
||||
}
|
||||
if ring[a][0] > 0.0 {
|
||||
fixed[a].push(ilo[a]);
|
||||
}
|
||||
if ring[a][1] > 0.0 {
|
||||
fixed[a].push(ihi[a]);
|
||||
}
|
||||
fixed[a].sort_by(f64::total_cmp);
|
||||
fixed[a].dedup_by(|x, y| (*x - *y).abs() < eps);
|
||||
}
|
||||
|
||||
let snap = (0.3 * inp.size).min(0.25 * circles.iter().map(|c| c.r).fold(f64::INFINITY, f64::min));
|
||||
for g in &mut groups {
|
||||
let outer = g.circles.first().copied().ok_or("an empty circle group")?;
|
||||
let r0 = outer.r;
|
||||
for a in 0..2 {
|
||||
let c = g.c[a];
|
||||
if !g.patch && fixed[a].iter().any(|v| (v - c).abs() < r0 - eps) {
|
||||
return Err(format!("a face of the body runs through the circle of radius {r0:.4}"));
|
||||
}
|
||||
}
|
||||
let half = match outer.kind {
|
||||
// The square round the whole circle (its corners projected onto it at 45°), or
|
||||
// where a blend it takes in sets a side, at the walls' inner plane.
|
||||
RingKind::Butterfly if g.patch => std::array::from_fn(|a| {
|
||||
let mut h = [r0; 2];
|
||||
// The blends' near edge: the walls' inner plane, whose corner projects onto
|
||||
// the blend's middle.
|
||||
if let Some(b) = g.blends {
|
||||
if 0.5 * (b[a][0] + b[a][1]) > g.c[a] {
|
||||
h[1] = b[a][0] - g.c[a];
|
||||
} else {
|
||||
h[0] = g.c[a] - b[a][1];
|
||||
}
|
||||
}
|
||||
h
|
||||
}),
|
||||
RingKind::Butterfly => [[r0 / 2f64.sqrt(); 2]; 2],
|
||||
RingKind::OGrid => o_grid_frame(g, &fixed, eps)?,
|
||||
};
|
||||
for a in 0..2 {
|
||||
let c = g.c[a];
|
||||
for s in 0..2 {
|
||||
let h = half[a][s];
|
||||
if outer.kind == RingKind::Butterfly && !g.patch && fixed[a].iter().any(|v| (v - c).abs() < h - eps) {
|
||||
return Err(format!("a face of the body runs through the square inside the circle of radius {r0:.4}"));
|
||||
}
|
||||
g.frame[a][s] = if s == 0 { c - h } else { c + h };
|
||||
}
|
||||
}
|
||||
}
|
||||
// Frames that overlap along an axis share their planes there (a plane may not cut another
|
||||
// frame); two frames that overlap along both are one cell, which the layout does not take.
|
||||
for _ in 0..=groups.len() * groups.len() {
|
||||
let mut changed = false;
|
||||
for a in 0..2 {
|
||||
for i in 0..groups.len() {
|
||||
for j in 0..groups.len() {
|
||||
if i == j {
|
||||
continue;
|
||||
}
|
||||
let (fi, fj) = (groups[i].frame[a], groups[j].frame[a]);
|
||||
let overlap = fi[0] < fj[1] - eps && fj[0] < fi[1] - eps;
|
||||
let same = (fi[0] - fj[0]).abs() < eps && (fi[1] - fj[1]).abs() < eps;
|
||||
if overlap && !same {
|
||||
let u = [fi[0].min(fj[0]), fi[1].max(fj[1])];
|
||||
groups[i].frame[a] = u;
|
||||
groups[j].frame[a] = u;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !changed {
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (i, g) in groups.iter().enumerate() {
|
||||
for h in groups.iter().skip(i + 1) {
|
||||
if (0..2).all(|a| g.frame[a][0] < h.frame[a][1] - eps && h.frame[a][0] < g.frame[a][1] - eps) {
|
||||
return Err(format!(
|
||||
"the circles at ({:.4}, {:.4}) and ({:.4}, {:.4}) are too close for a frame each",
|
||||
g.c[0], g.c[1], h.c[0], h.c[1]
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
// The planes: the fixed ones and the frames, a frame within `snap` of another plane moved
|
||||
// onto it (a fixed plane wins).
|
||||
let mut planes: [Vec<f64>; 3] = fixed.clone();
|
||||
for a in 0..2 {
|
||||
for g in &mut groups {
|
||||
for s in 0..2 {
|
||||
let v = g.frame[a][s];
|
||||
let near = fixed[a].iter().copied().filter(|p| (p - v).abs() < snap).min_by(|p, q| (p - v).abs().total_cmp(&(q - v).abs()));
|
||||
let v = match near {
|
||||
Some(p) => p,
|
||||
None => planes[a].iter().copied().find(|p| (p - v).abs() < snap).unwrap_or(v),
|
||||
};
|
||||
g.frame[a][s] = v;
|
||||
planes[a].push(v);
|
||||
}
|
||||
}
|
||||
planes[a].sort_by(f64::total_cmp);
|
||||
planes[a].dedup_by(|x, y| (*x - *y).abs() < eps);
|
||||
}
|
||||
for g in &groups {
|
||||
for a in 0..2 {
|
||||
// A patch's frame may stop short of its circle where the walls join (the ring's
|
||||
// faces there are projected out onto it), but holds the circles inside.
|
||||
let r0 = g.circles.first().map_or(0.0, |c| c.r);
|
||||
let hold = if g.patch { 1.2 * g.circles.get(1).map_or(0.0, |c| c.r) } else { 0.7 * r0 };
|
||||
if !(g.frame[a][0] < g.c[a] - hold && g.frame[a][1] > g.c[a] + hold) {
|
||||
return Err(format!("the frame round the circle of radius {r0:.4} does not hold it"));
|
||||
}
|
||||
if !g.patch && planes[a].iter().any(|p| *p > g.frame[a][0] + eps && *p < g.frame[a][1] - eps) {
|
||||
return Err(format!("a plane of the body runs through the frame round the circle of radius {r0:.4}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if planes.iter().any(|p| p.len() < 2) {
|
||||
return Err("the body has no planar sides or circles to lay a grid between".into());
|
||||
}
|
||||
let n = planes.each_ref().map(|p| p.len() - 1);
|
||||
if n.iter().product::<usize>() * (1 + 4 * circles.len()) > MAX_LAYOUT_BLOCKS {
|
||||
return Err(format!("a grid of {n:?} cells is too large for one layout"));
|
||||
}
|
||||
|
||||
// The grid, in the local frame; each block's point that says whether it is material.
|
||||
let mut t = Topology::grid([&planes[0], &planes[1], &planes[2]]).map_err(|e| e.to_string())?;
|
||||
let mid = |p: &[f64], i: usize| 0.5 * (p[i] + p[i + 1]);
|
||||
let mut probe: Vec<V3> = t
|
||||
.blocks
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(b, _)| [mid(&planes[0], b % n[0]), mid(&planes[1], (b / n[0]) % n[1]), mid(&planes[2], b / (n[0] * n[1]))])
|
||||
.collect();
|
||||
let index = |p: &[f64], v: f64| p.iter().position(|x| (x - v).abs() < eps);
|
||||
// The grid cells the rings replace.
|
||||
let mut replaced: Vec<usize> = Vec::new();
|
||||
let mut rings = 0;
|
||||
for g in &groups {
|
||||
let (Some(i0), Some(i1), Some(j0), Some(j1)) =
|
||||
(index(&planes[0], g.frame[0][0]), index(&planes[0], g.frame[0][1]), index(&planes[1], g.frame[1][0]), index(&planes[1], g.frame[1][1]))
|
||||
else {
|
||||
return Err("a frame lost its planes".into());
|
||||
};
|
||||
let r0 = g.circles.first().map_or(0.0, |c| c.r);
|
||||
// The squares inside the frame: one per circle (corners at 45° on it), then the core.
|
||||
// Each: half-sides [-u, +u, -v, +v] and the radius it stands for per side (a patch's
|
||||
// frame stands for its circle, which the frame's corners are projected onto).
|
||||
let mut squares: Vec<([f64; 4], [f64; 4])> = Vec::new();
|
||||
let fr = [g.c[0] - g.frame[0][0], g.frame[0][1] - g.c[0], g.c[1] - g.frame[1][0], g.frame[1][1] - g.c[1]];
|
||||
squares.push((fr, if g.patch { [r0; 4] } else { fr }));
|
||||
for c in &g.circles {
|
||||
let h = c.r / 2f64.sqrt();
|
||||
if squares.last().is_some_and(|(s, _)| s.iter().all(|x| (x - h).abs() < eps)) || (g.patch && c.r == r0) {
|
||||
continue;
|
||||
}
|
||||
squares.push(([h; 4], [c.r; 4]));
|
||||
}
|
||||
rings += g.circles.len();
|
||||
let inner = g.circles.last().map_or(0.0, |c| c.r);
|
||||
squares.push(([inner / (2.0 * 2f64.sqrt()); 4], [0.0; 4]));
|
||||
if squares.windows(2).any(|w| (0..4).any(|i| !(w[1].0[i] < w[0].0[i] - eps))) {
|
||||
return Err(format!("the frame round the circle of radius {r0:.4} is too tight for its ring"));
|
||||
}
|
||||
// The frame's subdivision by the grid's planes, as fractions along each side: every
|
||||
// square inside is divided alike, so the ring's blocks meet node to node.
|
||||
let frac = |a: usize, lo: usize, hi: usize| -> Vec<f64> {
|
||||
let (p0, p1) = (planes[a][lo], planes[a][hi]);
|
||||
planes[a][lo..=hi].iter().map(|p| (p - p0) / (p1 - p0)).collect()
|
||||
};
|
||||
let (fu, fv) = (frac(0, i0, i1), frac(1, j0, j1));
|
||||
// Point (u, v) (fractions along the axes) of square `k`.
|
||||
let at = |k: usize, u: f64, v: f64| -> [f64; 2] {
|
||||
let h = squares.get(k).map_or([0.0; 4], |s| s.0);
|
||||
[g.c[0] - h[0] + u * (h[0] + h[1]), g.c[1] - h[2] + v * (h[2] + h[3])]
|
||||
};
|
||||
// The frame's boundary, counter-clockwise, as (u, v) fractions with the side each
|
||||
// segment that starts there lies on (0: -u, 1: +u, 2: -v, 3: +v).
|
||||
let mut around: Vec<([f64; 2], usize)> = Vec::new();
|
||||
around.extend(fu[..fu.len() - 1].iter().map(|&u| ([u, 0.0], 2)));
|
||||
around.extend(fv[..fv.len() - 1].iter().map(|&v| ([1.0, v], 1)));
|
||||
around.extend(fu[1..].iter().rev().map(|&u| ([u, 1.0], 3)));
|
||||
around.extend(fv[1..].iter().rev().map(|&v| ([0.0, v], 0)));
|
||||
for w in 0..n[2] {
|
||||
let (z0, z1) = (planes[2][w], planes[2][w + 1]);
|
||||
let zmid = 0.5 * (z0 + z1);
|
||||
for i in i0..i1 {
|
||||
for j in j0..j1 {
|
||||
replaced.push((w * n[1] + j) * n[0] + i);
|
||||
}
|
||||
}
|
||||
let add = |q: [[f64; 2]; 4], pr: V3, t: &mut Topology, probe: &mut Vec<V3>| {
|
||||
let base = t.vertices.len() as u32;
|
||||
for z in [z0, z1] {
|
||||
for p in q {
|
||||
t.vertices.push([p[0], p[1], z]);
|
||||
}
|
||||
}
|
||||
let mut b = [base, base + 1, base + 2, base + 3, base + 4, base + 5, base + 6, base + 7];
|
||||
let pos = b.map(|v| t.vertices.get(v as usize).copied().unwrap_or_default());
|
||||
if corner_det(&pos) < 0.0 {
|
||||
b = [b[0], b[3], b[2], b[1], b[4], b[7], b[6], b[5]];
|
||||
}
|
||||
t.blocks.push(b);
|
||||
probe.push(pr);
|
||||
};
|
||||
for (k, pair) in squares.windows(2).enumerate() {
|
||||
let (outer_rho, inner_rho) = (pair[0].1, pair[1].1);
|
||||
for (s, &(p, side)) in around.iter().enumerate() {
|
||||
let Some(&(q, _)) = around.get((s + 1) % around.len()) else { continue };
|
||||
let quad = [at(k, p[0], p[1]), at(k, q[0], q[1]), at(k + 1, q[0], q[1]), at(k + 1, p[0], p[1])];
|
||||
// The probe: between the two radii the squares stand for, toward the
|
||||
// segment's middle.
|
||||
let m = [0.5 * (quad[0][0] + quad[1][0]) - g.c[0], 0.5 * (quad[0][1] + quad[1][1]) - g.c[1]];
|
||||
let len = m[0].hypot(m[1]).max(eps);
|
||||
let rho = 0.5 * (outer_rho[side] + inner_rho[side]);
|
||||
add(quad, [g.c[0] + m[0] / len * rho, g.c[1] + m[1] / len * rho, zmid], &mut t, &mut probe);
|
||||
}
|
||||
}
|
||||
// The core, divided as the frame.
|
||||
let last = squares.len() - 1;
|
||||
for u in fu.windows(2) {
|
||||
for v in fv.windows(2) {
|
||||
let quad = [at(last, u[0], v[0]), at(last, u[1], v[0]), at(last, u[1], v[1]), at(last, u[0], v[1])];
|
||||
let cx = 0.25 * quad.iter().map(|p| p[0]).sum::<f64>();
|
||||
let cy = 0.25 * quad.iter().map(|p| p[1]).sum::<f64>();
|
||||
add(quad, [cx, cy, zmid], &mut t, &mut probe);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if probe.len() != t.blocks.len() {
|
||||
return Err("the layout lost track of its blocks".into());
|
||||
}
|
||||
replaced.sort_unstable();
|
||||
replaced.dedup();
|
||||
t.remove(&replaced).map_err(|e| e.to_string())?;
|
||||
let mut k = 0;
|
||||
probe.retain(|_| {
|
||||
k += 1;
|
||||
replaced.binary_search(&(k - 1)).is_err()
|
||||
});
|
||||
t.merge(eps);
|
||||
|
||||
// The blend layer: a block on every boundary face of the grid on a side with a layer.
|
||||
let mut uses: std::collections::BTreeMap<[u32; 4], usize> = std::collections::BTreeMap::new();
|
||||
for block in &t.blocks {
|
||||
for f in hex_faces(block) {
|
||||
let mut key = f;
|
||||
key.sort_unstable();
|
||||
*uses.entry(key).or_insert(0) += 1;
|
||||
}
|
||||
}
|
||||
let mut moved: std::collections::BTreeMap<u32, u32> = std::collections::BTreeMap::new();
|
||||
let original = t.blocks.len();
|
||||
for b in 0..original {
|
||||
let Some(block) = t.blocks.get(b).copied() else { continue };
|
||||
for f in hex_faces(&block) {
|
||||
let mut key = f;
|
||||
key.sort_unstable();
|
||||
if uses.get(&key) != Some(&1) {
|
||||
continue;
|
||||
}
|
||||
let pts = f.map(|v| t.vertices.get(v as usize).copied().unwrap_or_default());
|
||||
let side = (0..3).flat_map(|a| [(a, 0), (a, 1)]).find(|&(a, s)| {
|
||||
let plane = if s == 0 { ilo[a] } else { ihi[a] };
|
||||
ring[a][s] > 0.0 && pts.iter().all(|p| (p[a] - plane).abs() < eps)
|
||||
});
|
||||
let Some((a, s)) = side else { continue };
|
||||
let mut outer = [0u32; 4];
|
||||
for (o, &v) in outer.iter_mut().zip(&f) {
|
||||
*o = match moved.get(&v) {
|
||||
Some(&w) => w,
|
||||
None => {
|
||||
let p = t.vertices.get(v as usize).copied().unwrap_or_default();
|
||||
let mut q = p;
|
||||
for aa in 0..3 {
|
||||
if ring[aa][0] > 0.0 && (p[aa] - ilo[aa]).abs() < eps {
|
||||
q[aa] -= ring[aa][0];
|
||||
}
|
||||
if ring[aa][1] > 0.0 && (p[aa] - ihi[aa]).abs() < eps {
|
||||
q[aa] += ring[aa][1];
|
||||
}
|
||||
}
|
||||
let w = t.vertices.len() as u32;
|
||||
t.vertices.push(q);
|
||||
moved.insert(v, w);
|
||||
w
|
||||
}
|
||||
};
|
||||
}
|
||||
let mut nb = [f[0], f[1], f[2], f[3], outer[0], outer[1], outer[2], outer[3]];
|
||||
let at = |v: u32| t.vertices.get(v as usize).copied().unwrap_or_default();
|
||||
if corner_det(&nb.map(at)) < 0.0 {
|
||||
nb = [f[0], f[3], f[2], f[1], outer[0], outer[3], outer[2], outer[1]];
|
||||
}
|
||||
let centre = pts.iter().fold([0.0; 3], |acc, p| [acc[0] + p[0] / 4.0, acc[1] + p[1] / 4.0, acc[2] + p[2] / 4.0]);
|
||||
let mut pr = centre;
|
||||
pr[a] += if s == 0 { -0.5 * ring[a][s] } else { 0.5 * ring[a][s] };
|
||||
t.blocks.push(nb);
|
||||
probe.push(pr);
|
||||
}
|
||||
}
|
||||
if t.blocks.len() > MAX_LAYOUT_BLOCKS {
|
||||
return Err(format!("{} blocks: too many for one layout", t.blocks.len()));
|
||||
}
|
||||
|
||||
// Carve: blocks whose probe lies outside the body.
|
||||
let gone: Vec<usize> = probe.iter().enumerate().filter(|(_, p)| winding(inp.mesh, world(**p)) < 0.5).map(|(b, _)| b).collect();
|
||||
if gone.len() == t.blocks.len() {
|
||||
return Err("every block of the layout lies outside the body".into());
|
||||
}
|
||||
if !gone.is_empty() {
|
||||
t.remove(&gone).map_err(|e| e.to_string())?;
|
||||
}
|
||||
for v in &mut t.vertices {
|
||||
*v = world(*v);
|
||||
}
|
||||
t.check().map_err(|e| format!("the layout's blocks do not fit: {e}"))?;
|
||||
let layers = (0..3)
|
||||
.flat_map(|a| [(a, 0), (a, 1)])
|
||||
.filter(|&(a, s)| ring[a][s] > 0.0)
|
||||
.map(|(a, s)| (SIDE_NAMES[perm[a]][s].to_string(), ring[a][s], what[a][s]))
|
||||
.collect();
|
||||
let frames = groups
|
||||
.iter()
|
||||
.map(|g| {
|
||||
let half = (0..2).flat_map(|a| [g.c[a] - g.frame[a][0], g.frame[a][1] - g.c[a]]).fold(f64::INFINITY, f64::min);
|
||||
(world([g.c[0], g.c[1], 0.0]), half)
|
||||
})
|
||||
.collect();
|
||||
Ok(Layout { topology: t, rings, layers, planes: planes.each_ref().map(Vec::len), frames, pillow: None })
|
||||
}
|
||||
|
||||
/// The ring that takes in the blend with points `pts` (local frame): a blend round a corner of
|
||||
/// the outline, touching the body's side along both in-plane axes, that lies inside the frame of
|
||||
/// an O-grid ring reaching from the hole to those sides, the hole centred in it. Returns the
|
||||
/// group and the frame's half-side per axis (the hole's distance to the side). A frame wider
|
||||
/// than 6 radii (or narrower than 1.2) does not take a blend in: the hole is not the corner's.
|
||||
fn corner_ring(groups: &[Group], pts: &[V3], lo: V3, hi: V3, eps: f64) -> Option<(usize, [f64; 2])> {
|
||||
let (mut bl, mut bh) = ([f64::INFINITY; 2], [f64::NEG_INFINITY; 2]);
|
||||
for p in pts {
|
||||
for a in 0..2 {
|
||||
bl[a] = bl[a].min(p[a]);
|
||||
bh[a] = bh[a].max(p[a]);
|
||||
}
|
||||
}
|
||||
let tol = 1e3 * eps;
|
||||
groups.iter().enumerate().find_map(|(i, g)| {
|
||||
let outer = g.circles.first()?;
|
||||
if outer.kind != RingKind::OGrid {
|
||||
return None;
|
||||
}
|
||||
let mut half = [0.0; 2];
|
||||
for a in 0..2 {
|
||||
let h = if (bl[a] - lo[a]).abs() < tol {
|
||||
g.c[a] - lo[a]
|
||||
} else if (bh[a] - hi[a]).abs() < tol {
|
||||
hi[a] - g.c[a]
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
if !(1.2 * outer.r..=6.0 * outer.r).contains(&h) || bl[a] < g.c[a] - h - tol || bh[a] > g.c[a] + h + tol {
|
||||
return None;
|
||||
}
|
||||
half[a] = h;
|
||||
}
|
||||
Some((i, half))
|
||||
})
|
||||
}
|
||||
|
||||
/// The patch ring that takes in the concave blend with points `pts` (local frame): a blend
|
||||
/// inside the square round a boss's whole circle and not inside the circle (the inner fillet
|
||||
/// where the walls meet the boss, the boss touching it at most). Returns the group and, per
|
||||
/// axis, the blend's extent.
|
||||
fn patch_blend(groups: &[Group], pts: &[V3]) -> Option<(usize, [[f64; 2]; 2])> {
|
||||
let (mut bl, mut bh) = ([f64::INFINITY; 2], [f64::NEG_INFINITY; 2]);
|
||||
for p in pts {
|
||||
for a in 0..2 {
|
||||
bl[a] = bl[a].min(p[a]);
|
||||
bh[a] = bh[a].max(p[a]);
|
||||
}
|
||||
}
|
||||
groups.iter().enumerate().find_map(|(i, g)| {
|
||||
let r0 = g.circles.first()?.r;
|
||||
let tol = 1e-3 * r0;
|
||||
if !g.patch || pts.iter().any(|p| (p[0] - g.c[0]).hypot(p[1] - g.c[1]) < 0.9 * r0) {
|
||||
return None;
|
||||
}
|
||||
if (0..2).any(|a| bl[a] < g.c[a] - r0 - tol || bh[a] > g.c[a] + r0 + tol) {
|
||||
return None;
|
||||
}
|
||||
Some((i, [[bl[0], bh[0]], [bl[1], bh[1]]]))
|
||||
})
|
||||
}
|
||||
|
||||
/// The half-sides of an O-grid ring's frame, per local axis and side: the hole centred in it.
|
||||
/// A side of the body within 3 radii (or a corner blend the ring takes in) is the frame on that
|
||||
/// side, and the frame's other side is as far from the centre unless the body's side is nearer
|
||||
/// there too; an axis with no side near takes the half-side of the other axis's (a square
|
||||
/// frame) when that is 1.5 to 3 radii, else 2 radii.
|
||||
fn o_grid_frame(g: &Group, fixed: &[Vec<f64>; 3], eps: f64) -> Result<[[f64; 2]; 2], String> {
|
||||
let r0 = g.circles.first().map_or(0.0, |c| c.r);
|
||||
let mut half: [Option<[f64; 2]>; 2] = [None; 2];
|
||||
for a in 0..2 {
|
||||
let c = g.c[a];
|
||||
let mut dist = [0.0; 2];
|
||||
for (s, d) in dist.iter_mut().enumerate() {
|
||||
*d = fixed[a].iter().map(|v| if s == 0 { c - v } else { v - c }).filter(|x| *x > 0.0).fold(f64::INFINITY, f64::min);
|
||||
if !d.is_finite() {
|
||||
return Err(format!("the hole of radius {r0:.4} has no side of the body round it"));
|
||||
}
|
||||
if *d < 1.05 * r0 {
|
||||
return Err(format!("the hole of radius {r0:.4} is {d:.4} mm from a face: too close for a ring"));
|
||||
}
|
||||
}
|
||||
let near = dist[0].min(dist[1]);
|
||||
half[a] = match g.forced[a] {
|
||||
Some(h) => Some([h; 2]),
|
||||
// The side itself frames the ring when a strip narrower than the radius would be
|
||||
// left between them; the other side keeps the frame centred unless it is near too.
|
||||
None if near <= 3.0 * r0 + eps => Some(dist.map(|d| if d <= 3.0 * r0 + eps { d } else { near })),
|
||||
None => None,
|
||||
};
|
||||
}
|
||||
let square = |b: usize| half[b].map(|h| h[0].min(h[1])).filter(|h| (1.5 * r0 - eps..=3.0 * r0 + eps).contains(h)).unwrap_or(2.0 * r0);
|
||||
Ok([half[0].unwrap_or([square(1); 2]), half[1].unwrap_or([square(0); 2])])
|
||||
}
|
||||
|
||||
/// The six faces of a hex8 block, as corner cycles.
|
||||
fn hex_faces(b: &[u32; 8]) -> [[u32; 4]; 6] {
|
||||
[
|
||||
[b[0], b[1], b[2], b[3]],
|
||||
[b[4], b[5], b[6], b[7]],
|
||||
[b[0], b[1], b[5], b[4]],
|
||||
[b[1], b[2], b[6], b[5]],
|
||||
[b[2], b[3], b[7], b[6]],
|
||||
[b[3], b[0], b[4], b[7]],
|
||||
]
|
||||
}
|
||||
|
||||
/// The Jacobian determinant at corner 1 of a hex8 block (positive: right-handed).
|
||||
pub(super) fn corner_det(c: &[V3; 8]) -> f64 {
|
||||
dot(sub(c[1], c[0]), cross(sub(c[3], c[0]), sub(c[4], c[0])))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn winding_of_a_cube_is_one_inside_and_zero_outside() {
|
||||
let shape = fieldcraft_geometry::block([0.0; 3], [2.0, 2.0, 2.0]).unwrap();
|
||||
let mesh = shape.tessellate(0.01).unwrap();
|
||||
assert!((winding(&mesh, [1.0, 1.0, 1.0]) - 1.0).abs() < 1e-6);
|
||||
assert!(winding(&mesh, [3.0, 1.0, 1.0]).abs() < 1e-6);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
//! Decomposition proposals (#439): for a body with features the plan cannot map, the partition
|
||||
//! cuts that box those features into a cell of their own, so the rest of the body is cells the
|
||||
//! sweep meshes and only the boxed cell is left to tetrahedra (joined to the hex cells by
|
||||
//! pyramids, `mesh.cells`).
|
||||
//!
|
||||
//! The region is the bounding box of the unmapped features' faces (those not already prismatic
|
||||
//! along the sweep), in the frame of the sweep direction and two directions square to it. Each
|
||||
//! side of the box becomes a cut one element size outside it, where that plane lies inside the
|
||||
//! body, clear of its planar faces and of its curved faces (a cut through a curved face does not
|
||||
//! close in the partitions' tessellation); a side with no such plane is left open and the tet
|
||||
//! cell reaches the body's boundary there. Decomposing a part into sweepable volumes and a
|
||||
//! remainder for a free mesher is the practice of T. D. Blacker, *The Cooper Tool*, Proc. 5th
|
||||
//! International Meshing Roundtable, 1996 (docs/meshing/literature.md, B11). The proposal is a
|
||||
//! reply, not a change: the agent applies the cuts (`commands`) and meshes the cells.
|
||||
|
||||
use fieldcraft_geometry::{Design, FaceSurface};
|
||||
use fieldcraft_model::MeshPlan;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::{AXES, Clearance, V3, cross, dot, faces_prismatic, parallel, plan_shape, unit};
|
||||
use crate::Result;
|
||||
|
||||
/// The decomposition proposal for `plan`, or `None` when every feature is mapped (or the plan
|
||||
/// has no sweep direction to make cells along).
|
||||
pub(super) fn proposal(design: &Design, plan: &MeshPlan) -> Result<Option<Value>> {
|
||||
let Some(d) = plan.direction else { return Ok(None) };
|
||||
if plan.unmapped.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let Some(shape) = plan_shape(design, &plan.body) else { return Ok(None) };
|
||||
let source = design.source_of(&plan.body).unwrap_or(&plan.body);
|
||||
let features: Vec<_> = design.features(Some(source))?.into_iter().next().map(|b| b.features).unwrap_or_default();
|
||||
let surfaces = shape.face_surfaces()?;
|
||||
// The unmapped features whose faces the sweep cannot mesh.
|
||||
let lumps: Vec<_> = features
|
||||
.iter()
|
||||
.filter(|f| plan.unmapped.iter().any(|u| u.id == f.id))
|
||||
.filter(|f| !f.faces.is_empty() && !faces_prismatic(shape, &surfaces, &f.faces, d))
|
||||
.collect();
|
||||
if lumps.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let faces: Vec<u32> = lumps.iter().flat_map(|f| &f.faces).filter_map(|n| shape.face_index(n)).collect();
|
||||
let m = shape.measure()?;
|
||||
let diagonal = (0..3).map(|k| (m.bbox_max[k] - m.bbox_min[k]).powi(2)).sum::<f64>().sqrt();
|
||||
let mesh = shape.tessellate(plan.tolerance.unwrap_or(1e-3 * diagonal))?;
|
||||
// The frame: the sweep direction and two directions square to it (axes when it is one).
|
||||
let side = AXES.iter().copied().filter(|a| !parallel(*a, d)).min_by(|a, b| dot(*a, d).abs().total_cmp(&dot(*b, d).abs())).unwrap_or(AXES[0]);
|
||||
let e1 = unit(cross(d, side)).unwrap_or(side);
|
||||
let e2 = unit(cross(d, e1)).unwrap_or(side);
|
||||
let frame = [d, e1, e2].map(|a| AXES.iter().copied().find(|x| parallel(*x, a)).unwrap_or(a));
|
||||
let mut lo = [f64::INFINITY; 3];
|
||||
let mut hi = [f64::NEG_INFINITY; 3];
|
||||
let mut curved: Vec<Vec<V3>> = vec![Vec::new(); surfaces.len()];
|
||||
for (t, &f) in mesh.triangles.iter().zip(&mesh.tri_face) {
|
||||
let pts: Vec<V3> = t.iter().filter_map(|&i| mesh.positions.get(i as usize).copied()).collect();
|
||||
if faces.contains(&f) {
|
||||
for p in &pts {
|
||||
for (k, a) in frame.iter().enumerate() {
|
||||
lo[k] = lo[k].min(dot(*p, *a));
|
||||
hi[k] = hi[k].max(dot(*p, *a));
|
||||
}
|
||||
}
|
||||
}
|
||||
if !matches!(surfaces.get(f as usize), Some(Some(FaceSurface::Plane { .. })))
|
||||
&& let Some(c) = curved.get_mut(f as usize)
|
||||
{
|
||||
c.extend(pts);
|
||||
}
|
||||
}
|
||||
if lo.iter().chain(&hi).any(|v| !v.is_finite()) {
|
||||
return Ok(None);
|
||||
}
|
||||
curved.retain(|p| !p.is_empty());
|
||||
let planes = surfaces
|
||||
.iter()
|
||||
.zip(shape.faces())
|
||||
.filter_map(|(s, f)| match s {
|
||||
Some(FaceSurface::Plane { normal, point }) => Some((*normal, dot(*normal, *point), f.name.as_str())),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let corners = (0..8)
|
||||
.map(|i| {
|
||||
[
|
||||
if i & 1 == 0 { m.bbox_min[0] } else { m.bbox_max[0] },
|
||||
if i & 2 == 0 { m.bbox_min[1] } else { m.bbox_max[1] },
|
||||
if i & 4 == 0 { m.bbox_min[2] } else { m.bbox_max[2] },
|
||||
]
|
||||
})
|
||||
.collect();
|
||||
let body = Clearance { corners, planes, curved, gap: 0.5 * plan.size };
|
||||
let names: Vec<&str> = lumps.iter().map(|f| f.name.as_str()).collect();
|
||||
let mut cuts = Vec::new();
|
||||
let mut open = Vec::new();
|
||||
let mut region = [[0.0; 3]; 2];
|
||||
for (k, n) in frame.iter().enumerate() {
|
||||
let (blo, bhi) = body.extent(*n);
|
||||
region[0][k] = blo;
|
||||
region[1][k] = bhi;
|
||||
for (end, t) in [(0, lo[k] - plan.size), (1, hi[k] + plan.size)] {
|
||||
let which = if end == 0 { "below" } else { "above" };
|
||||
let why = if !body.inside(*n, t) {
|
||||
Some("the body ends there".to_string())
|
||||
} else if let Some((_, face)) = body.faces_near(*n, t).next() {
|
||||
Some(format!("it would lie on face `{face}`"))
|
||||
} else if body.crosses_curved(*n, t) {
|
||||
Some("it would cross a curved face".into())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
match why {
|
||||
Some(why) => open.push(json!({"normal": n, "side": which, "reason": why})),
|
||||
None => {
|
||||
region[end][k] = t;
|
||||
let point = n.map(|c| c * t);
|
||||
cuts.push(json!({"point": point, "normal": n, "reason": format!("boxes {} ({which} along {n:?})", names.join(", "))}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// The commands that make the cuts and mesh the cells (the boxed cell falls back to tet).
|
||||
let mut commands = Vec::new();
|
||||
for (i, c) in cuts.iter().enumerate() {
|
||||
let name = format!("{}-split{}", plan.body, i + 1);
|
||||
commands.push(json!({"command": "geometry.datum", "params": {"name": name, "by": {"type": "point_normal", "point": c["point"], "normal": c["normal"]}}}));
|
||||
commands.push(json!({"command": "geometry.partition", "params": {"name": name, "datum": name}}));
|
||||
}
|
||||
commands.push(json!({"command": "mesh.cells", "params": {"default": "auto", "size": plan.size, "transition": "pyramid"}}));
|
||||
let box_volume: f64 = (0..3).map(|k| region[1][k] - region[0][k]).product();
|
||||
let share = if m.volume.abs() > 0.0 { (box_volume / m.volume.abs()).min(1.0) } else { 1.0 };
|
||||
Ok(Some(json!({
|
||||
"features": names,
|
||||
"cuts": cuts,
|
||||
"open_sides": open,
|
||||
"tet_region": {"frame": frame, "min": region[0], "max": region[1]},
|
||||
"tet_share_at_most": share,
|
||||
"commands": commands,
|
||||
"note": "cut the body by these planes; the cell inside `tet_region` is meshed with tet, every other cell is a box or a slice of the sweep's cross-section the hex meshers take, and mesh.cells joins the tet cell to them through pyramids",
|
||||
})))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// A 80 × 40 × 12 plate with a ball (radius 8) joined on its top: the sweep takes the plate,
|
||||
/// not the dome.
|
||||
fn plate_with_dome() -> (crate::tests::TempDir, Session) {
|
||||
let commands = json!({"commands": [
|
||||
{"command": "solid.box", "params": {"length": 80, "width": 40, "height": 12, "body_name": "Plate"}},
|
||||
{"command": "solid.sphere", "params": {"center": [60, 20, 12], "radius": 8, "body_name": "Ball", "operation": "new"}},
|
||||
{"command": "solid.combine", "params": {"target": "Plate", "tools": ["Ball"], "operation": "join"}},
|
||||
]});
|
||||
let bytes = fieldcraft_geometry::script_design(&commands.to_string()).unwrap();
|
||||
let (dir, path) = crate::tests::design_file(&bytes);
|
||||
let mut s = Session::default();
|
||||
s.execute("geometry.open", &json!({"path": path})).unwrap();
|
||||
(dir, s)
|
||||
}
|
||||
|
||||
/// Fix the end x = 0, pull the end x = 80 with `force` (N), solve, and return the mean
|
||||
/// displacement of the loaded end along `k`.
|
||||
fn pull(s: &mut Session, force: [f64; 3], k: usize) -> f64 {
|
||||
let m = &s.model;
|
||||
let at = |x: f64| -> Vec<u32> { m.nodes().iter().filter(|(_, n)| (n.xyz[0] - x).abs() < 1e-6).map(|(l, _)| *l).collect() };
|
||||
let (root, tip) = (at(0.0), at(80.0));
|
||||
// The loaded end's element faces, so the force is shared by consistent weights.
|
||||
let end: Vec<Value> = m
|
||||
.elements()
|
||||
.iter()
|
||||
.flat_map(|(&e, el)| (1..=el.kind.faces().len() as u8).map(move |f| (e, f)))
|
||||
.filter(|&(e, f)| {
|
||||
m.face_nodes(fieldcraft_model::ElementFace { element: e, face: f })
|
||||
.is_some_and(|ns| ns.iter().all(|n| (m.node(*n).unwrap().xyz[0] - 80.0).abs() < 1e-6))
|
||||
})
|
||||
.map(|(e, f)| json!({"element": e, "face": f}))
|
||||
.collect();
|
||||
let script = json!([
|
||||
{"command": "model.set.create", "params": {"name": "all", "elements": m.elements().keys().collect::<Vec<_>>()}},
|
||||
{"command": "model.set.create", "params": {"name": "root", "nodes": root}},
|
||||
{"command": "model.set.create", "params": {"name": "tip", "faces": end}},
|
||||
{"command": "model.material.create", "params": {"name": "Steel", "elastic": {"type": "isotropic", "young": 210000, "poisson": 0.3}}},
|
||||
{"command": "model.section.create", "params": {"name": "S", "set": "all", "material": "Steel", "kind": {"type": "solid"}}},
|
||||
{"command": "model.boundary_condition.create", "params": {"name": "Clamp", "type": "fixed", "target": {"set": "root"}}},
|
||||
{"command": "model.load.create", "params": {"name": "Pull", "type": "force", "target": {"set": "tip"}, "vector": force}},
|
||||
{"command": "model.step.create", "params": {"name": "Load", "procedure": {"type": "static"}, "boundary_conditions": ["Clamp"], "loads": ["Pull"]}},
|
||||
]);
|
||||
s.run_script(&script).unwrap();
|
||||
s.execute("solve.static", &json!({})).unwrap();
|
||||
let probe = s.execute("solve.probe", &json!({"step": "Load", "nodes": tip})).unwrap();
|
||||
let w: Vec<f64> = probe["nodes"].as_array().unwrap().iter().map(|n| n["displacement"][k].as_f64().unwrap()).collect();
|
||||
w.iter().sum::<f64>() / w.len() as f64
|
||||
}
|
||||
|
||||
/// #439's acceptance: the plan proposes cuts that box the dome; with them the plate is hex
|
||||
/// cells and the dome's cell tet, joined by pyramids into one valid, conforming mesh whose
|
||||
/// static answer matches the all-tet mesh's within 2 %.
|
||||
#[test]
|
||||
fn a_dome_on_a_plate_is_boxed_into_a_tet_cell_and_the_rest_is_hex() {
|
||||
let (_dir, mut s) = plate_with_dome();
|
||||
let plan = s.execute("mesh.plan", &json!({"body": "Plate", "size": 4})).unwrap();
|
||||
let d = &plan["decomposition"];
|
||||
assert!(d["features"].as_array().is_some_and(|f| !f.is_empty()), "{plan}");
|
||||
let cuts = d["cuts"].as_array().unwrap();
|
||||
assert!(cuts.len() >= 4, "{d}");
|
||||
let commands: Vec<Value> = d["commands"].as_array().unwrap().clone();
|
||||
let (cut_commands, mesh) = commands.split_at(commands.len() - 1);
|
||||
s.run_script(&json!(cut_commands)).unwrap();
|
||||
// The cell inside the region is tet; the others take the default (auto: hex).
|
||||
let (lo, hi) = (&d["tet_region"]["min"], &d["tet_region"]["max"]);
|
||||
let bodies = s.execute("geometry.bodies", &json!({})).unwrap();
|
||||
// The region is given along its frame's axes (here coordinate axes).
|
||||
let axis = |k: usize| (0..3).find(|&i| d["tet_region"]["frame"][k][i].as_f64().unwrap().abs() > 0.5).unwrap();
|
||||
let inside = |b: &Value| {
|
||||
(0..3).all(|k| {
|
||||
let i = axis(k);
|
||||
b["bbox"][0][i].as_f64().unwrap() >= lo[k].as_f64().unwrap() - 1e-6
|
||||
&& b["bbox"][1][i].as_f64().unwrap() <= hi[k].as_f64().unwrap() + 1e-6
|
||||
})
|
||||
};
|
||||
let tet: Vec<String> = bodies.as_array().unwrap().iter().filter(|b| inside(b)).map(|b| b["name"].as_str().unwrap().to_string()).collect();
|
||||
assert_eq!(tet.len(), 1, "{bodies}");
|
||||
let mut params = mesh[0]["params"].clone();
|
||||
params["methods"] = json!({tet[0].clone(): "tet"});
|
||||
let r = s.execute("mesh.cells", ¶ms).unwrap();
|
||||
let kinds: BTreeSet<&str> = s.model.elements().values().map(|e| e.kind.name()).collect();
|
||||
assert_eq!(kinds.into_iter().collect::<Vec<_>>(), ["hex8", "pyramid5", "tet4"], "{r}");
|
||||
assert!(r["interfaces"].as_array().unwrap().iter().all(|i| i["joined"] != "tied"), "{r}");
|
||||
let dup = s.execute("mesh.check.duplicates", &json!({})).unwrap();
|
||||
assert_eq!(dup["counts"], json!({"coincident": 0, "cracks": 0, "duplicate_elements": 0, "free_edges": 0}), "{dup}");
|
||||
let q = s.execute("mesh.quality", &json!({})).unwrap();
|
||||
assert_eq!(q["invalid"]["count"], 0, "{q}");
|
||||
// Tension along the plate, which the dome stiffens locally. (In bending, linear hexahedra
|
||||
// and linear tetrahedra lock differently at three elements through the thickness, so
|
||||
// the two meshes would differ by their discretisation, not by the transition.)
|
||||
let hex = s.model.elements().values().filter(|e| e.kind.name() == "hex8").count();
|
||||
let mixed = pull(&mut s, [8000.0, 0.0, 0.0], 0);
|
||||
let (_dir2, mut t) = plate_with_dome();
|
||||
t.execute("mesh.tet.generate", &json!({"body": "Plate", "size": 4})).unwrap();
|
||||
let all_tet = pull(&mut t, [8000.0, 0.0, 0.0], 0);
|
||||
eprintln!("mixed {mixed:.6e} ({hex} hex of {} elements), all tet {all_tet:.6e}", s.model.elements().len());
|
||||
assert!(((mixed - all_tet) / all_tet).abs() < 0.02, "{mixed} vs {all_tet}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,697 @@
|
||||
//! `mesh.hex.project`: projection blocking — a block topology (a carved grid, a butterfly or
|
||||
//! seven-block sphere template, O-grids, extruded faces) projected onto a body's B-rep and
|
||||
//! smoothed ([`fieldcraft_mesh::project`]).
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use fieldcraft_mesh::hex::BoundaryFace;
|
||||
use fieldcraft_mesh::join::{check_conforming, weld};
|
||||
use fieldcraft_mesh::project::{FixedPatch, ProjectOptions, Smoothing, Topology, project_mesh};
|
||||
use fieldcraft_mesh::surface::BoundarySurface;
|
||||
use fieldcraft_mesh::tet::geometry::Tessellation;
|
||||
use fieldcraft_model::{ElementKind, GeometryRef};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::requirement::Requirement;
|
||||
use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
|
||||
pub const HEX_PROJECT: CommandSpec = CommandSpec {
|
||||
id: "mesh.hex.project",
|
||||
label: "Projection Blocking",
|
||||
params: r#"{"body": "Body1", "size": 2.0 (element size, mm; default 1/25 of the body's diagonal), "template": "grid" (default) | "sphere" | "butterfly" | "blocks", "center": [x, y, z], "radius": r, "axis": [0, 0, 1], "levels": [z0, z1, …] (sphere and butterfly: default from the body's bounding box; butterfly levels are distances along the axis from the centre), "inset": 0.55 (inner square or cube corner at this fraction of the radius), "grid": {"planes": {"x": [...], "y": [...], "z": [...]} or "cells": [ni, nj, nk] (even planes over the bounding box, default [1, 1, 1]), "carve": [{"i": [a, b], "j": [c, d], "k": [e, f]}] (block index ranges, inclusive, removed)}, "blocks": {"vertices": [[x, y, z], ...], "blocks": [[v1..v8], ...] (0-based vertices, hex8 order)}, "operations": [{"op": "o_grid", "block": b, "open": [faces], "inset": 0.5 or [i1, i2, i3], "extrude": {"offset": [dx, dy, dz], "layers": 1, "faces": "all" (the open face's butterfly: a round branch) | "inner" (its inner face: a square stub)}} | {"op": "extrude", "faces": [[block, face], ...], "offset": [dx, dy, dz], "layers": 1} | {"op": "connect", "from": [block, face], "to": [block, face], "layers": 1} (blocks bridging two faces: closes a handle) | {"op": "remove", "blocks": [...]} (carve; later blocks renumber) | {"op": "merge", "distance": d} | {"op": "split", "block": b, "direction": 1|2|3 or "x"|"y"|"z", "at": [fractions]} (cuts the block and the whole sheet of blocks the cut runs through, so the topology stays conforming; fractions from the block's side-0 face, or from the low end along an axis; the block keeps its first slab, the others are appended)] (applied in order; blocks are 0-based, new blocks appended, the O-grid's inner block keeping the old index; a face is its hex8 number 1-6 or the direction it faces, "+x" … "-z"), "preview": true (return the topology without meshing), "faces": [{"block": b, "face": 1-6, "to": ["face names"]}] (targets set by hand; other outer block faces go to the smooth group of B-rep faces nearest them), "divisions": [{"block": b, "direction": 1|2|3, "count": n}], "smoothing": "elliptic" (default) | "elliptic-controlled" (Thomas–Middlecoff) | "laplace" | "none", "iterations": 100, "min_quality": 0.2 (smallest scaled Jacobian accepted), "max_arc_angle": 30 (degrees: the most a curved face may turn across one element; the divisions grow to keep it, 0 for no limit, at most 90), "fixed": ["face names"] (faces of the body another body's mesh already covers: its element faces there are taken as given, their nodes held and the block divisions there taken from them, so the two meshes meet node to node), "merge": distance (join nodes of other bodies' meshes within it; default 1e-6 of the diagonal, 0 never), "tolerance": chordal tolerance of the tessellation} — replaces the body's mesh with an all-hex mesh of the block topology: outer block faces projected onto their B-rep faces, edges between faces with different targets onto the curve where those meet (tangent-plane iteration), vertices on three onto their point, nodes placed by transfinite interpolation and smoothed with the boundary held on the geometry. Elements are tagged with the body and boundary faces with their B-rep faces. Reports blocks, divisions, targets and quality before and after smoothing; a corner that flattens on a curved face is an error that says to use an O-grid or template there"#,
|
||||
form: Some(super::forms::HEX_PROJECT),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Geometry],
|
||||
run: hex_project,
|
||||
};
|
||||
|
||||
/// Most operations, face targets or division settings one command takes.
|
||||
const MAX_LIST: usize = 10_000;
|
||||
/// Default element size relative to the body's diagonal.
|
||||
const DEFAULT_RELATIVE_SIZE: f64 = 1.0 / 25.0;
|
||||
const MIN_RELATIVE_SIZE: f64 = 1e-4;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct GridParams {
|
||||
planes: Option<BTreeMap<String, Vec<f64>>>,
|
||||
cells: Option<[usize; 3]>,
|
||||
#[serde(default)]
|
||||
carve: Vec<BTreeMap<String, [usize; 2]>>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct BlocksParams {
|
||||
vertices: Vec<[f64; 3]>,
|
||||
blocks: Vec<[u32; 8]>,
|
||||
}
|
||||
|
||||
/// A block face: its hex8 number, or the direction it faces ("+x", "-z", …).
|
||||
#[derive(Deserialize, Clone)]
|
||||
#[serde(untagged)]
|
||||
enum FaceSel {
|
||||
Number(u8),
|
||||
Towards(String),
|
||||
}
|
||||
|
||||
/// An inset: one fraction, or one per block direction.
|
||||
#[derive(Deserialize, Clone, Copy)]
|
||||
#[serde(untagged)]
|
||||
enum Inset {
|
||||
All(f64),
|
||||
Each([f64; 3]),
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct OGridExtrude {
|
||||
offset: [f64; 3],
|
||||
layers: Option<usize>,
|
||||
/// "all" (default: the whole open face, a butterfly section) or "inner" (the inner block's
|
||||
/// face alone, a four-sided section inside the face).
|
||||
faces: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields, tag = "op", rename_all = "snake_case")]
|
||||
enum Operation {
|
||||
OGrid {
|
||||
block: usize,
|
||||
#[serde(default)]
|
||||
open: Vec<FaceSel>,
|
||||
inset: Option<Inset>,
|
||||
extrude: Option<OGridExtrude>,
|
||||
},
|
||||
Extrude {
|
||||
faces: Vec<(usize, FaceSel)>,
|
||||
offset: [f64; 3],
|
||||
layers: Option<usize>,
|
||||
},
|
||||
Connect {
|
||||
from: (usize, FaceSel),
|
||||
to: (usize, FaceSel),
|
||||
layers: Option<usize>,
|
||||
},
|
||||
Remove {
|
||||
blocks: Vec<usize>,
|
||||
},
|
||||
Merge {
|
||||
distance: f64,
|
||||
},
|
||||
Split {
|
||||
block: usize,
|
||||
direction: DirSel,
|
||||
at: Vec<f64>,
|
||||
},
|
||||
}
|
||||
|
||||
/// A block direction: 1, 2 or 3 (along 1→2, 1→4, 1→5), or the axis it runs nearest ("x", "y",
|
||||
/// "z"; fractions then count from its low end).
|
||||
#[derive(Deserialize, Clone)]
|
||||
#[serde(untagged)]
|
||||
enum DirSel {
|
||||
Number(usize),
|
||||
Axis(String),
|
||||
}
|
||||
|
||||
fn face_of(t: &Topology, block: usize, f: &FaceSel) -> Result<u8> {
|
||||
match f {
|
||||
FaceSel::Number(n) if (1..=6).contains(n) => Ok(*n),
|
||||
FaceSel::Number(n) => Err(EngineError::Params(format!("face {n} is not a hex8 face (1 to 6)"))),
|
||||
FaceSel::Towards(d) => {
|
||||
let dir = match d.as_str() {
|
||||
"+x" => [1.0, 0.0, 0.0],
|
||||
"-x" => [-1.0, 0.0, 0.0],
|
||||
"+y" => [0.0, 1.0, 0.0],
|
||||
"-y" => [0.0, -1.0, 0.0],
|
||||
"+z" => [0.0, 0.0, 1.0],
|
||||
"-z" => [0.0, 0.0, -1.0],
|
||||
other => return Err(EngineError::Params(format!("face `{other}`: give a hex8 face number or +x, -x, +y, -y, +z, -z"))),
|
||||
};
|
||||
Ok(t.face_towards(block, dir)?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply the topology operations in order.
|
||||
fn operate(t: &mut Topology, ops: &[Operation]) -> Result<()> {
|
||||
for op in ops {
|
||||
match op {
|
||||
Operation::OGrid { block, open, inset, extrude } => {
|
||||
bounded(open, "operations.open")?;
|
||||
let open: Vec<u8> = open.iter().map(|f| face_of(t, *block, f)).collect::<Result<_>>()?;
|
||||
let inset = match inset.unwrap_or(Inset::All(0.5)) {
|
||||
Inset::All(x) => [x; 3],
|
||||
Inset::Each(v) => v,
|
||||
};
|
||||
let on = t.o_grid(*block, &open, inset)?;
|
||||
if let Some(e) = extrude {
|
||||
let first = on.first().ok_or_else(|| EngineError::Params("an O-grid extrusion needs an open face".into()))?;
|
||||
let faces: Vec<(usize, u8)> = match e.faces.as_deref().unwrap_or("all") {
|
||||
"all" => first.clone(),
|
||||
"inner" => first.iter().take(1).copied().collect(),
|
||||
other => return Err(EngineError::Params(format!("O-grid extrusion faces `{other}`: all or inner"))),
|
||||
};
|
||||
t.extrude(&faces, e.offset, e.layers.unwrap_or(1))?;
|
||||
}
|
||||
}
|
||||
Operation::Extrude { faces, offset, layers } => {
|
||||
bounded(faces, "operations.faces")?;
|
||||
let faces: Vec<(usize, u8)> = faces.iter().map(|(b, f)| Ok((*b, face_of(t, *b, f)?))).collect::<Result<_>>()?;
|
||||
t.extrude(&faces, *offset, layers.unwrap_or(1))?;
|
||||
}
|
||||
Operation::Connect { from, to, layers } => {
|
||||
let a = (from.0, face_of(t, from.0, &from.1)?);
|
||||
let b = (to.0, face_of(t, to.0, &to.1)?);
|
||||
t.connect(a, b, layers.unwrap_or(1))?;
|
||||
}
|
||||
Operation::Remove { blocks } => {
|
||||
bounded(blocks, "operations.blocks")?;
|
||||
t.remove(blocks)?;
|
||||
}
|
||||
Operation::Merge { distance } => {
|
||||
if !(distance.is_finite() && *distance >= 0.0) {
|
||||
return Err(EngineError::invalid("operations.distance", "a distance of 0 or more"));
|
||||
}
|
||||
t.merge(*distance);
|
||||
}
|
||||
Operation::Split { block, direction, at } => {
|
||||
bounded(at, "operations.at")?;
|
||||
let (d, against) = match direction {
|
||||
DirSel::Number(n @ 1..=3) => (n - 1, false),
|
||||
DirSel::Number(n) => return Err(EngineError::Params(format!("split direction {n}: 1, 2, 3 or x, y, z"))),
|
||||
DirSel::Axis(a) => {
|
||||
let axis = match a.as_str() {
|
||||
"x" => [1.0, 0.0, 0.0],
|
||||
"y" => [0.0, 1.0, 0.0],
|
||||
"z" => [0.0, 0.0, 1.0],
|
||||
other => return Err(EngineError::Params(format!("split direction `{other}`: 1, 2, 3 or x, y, z"))),
|
||||
};
|
||||
t.direction_along(*block, axis)?
|
||||
}
|
||||
};
|
||||
// Fractions count from the block's side-0 face; along an axis, from its low end.
|
||||
let at: Vec<f64> = if against { at.iter().rev().map(|x| 1.0 - x).collect() } else { at.clone() };
|
||||
t.split(*block, d, &at)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct FaceTarget {
|
||||
block: usize,
|
||||
face: FaceSel,
|
||||
to: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Division {
|
||||
block: usize,
|
||||
direction: usize,
|
||||
count: usize,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct ProjectParams {
|
||||
body: String,
|
||||
size: Option<f64>,
|
||||
tolerance: Option<f64>,
|
||||
template: Option<String>,
|
||||
center: Option<[f64; 3]>,
|
||||
radius: Option<f64>,
|
||||
axis: Option<[f64; 3]>,
|
||||
levels: Option<Vec<f64>>,
|
||||
inset: Option<f64>,
|
||||
grid: Option<GridParams>,
|
||||
blocks: Option<BlocksParams>,
|
||||
#[serde(default)]
|
||||
operations: Vec<Operation>,
|
||||
#[serde(default)]
|
||||
faces: Vec<FaceTarget>,
|
||||
#[serde(default)]
|
||||
divisions: Vec<Division>,
|
||||
smoothing: Option<String>,
|
||||
iterations: Option<usize>,
|
||||
min_quality: Option<f64>,
|
||||
max_arc_angle: Option<f64>,
|
||||
merge: Option<f64>,
|
||||
#[serde(default)]
|
||||
fixed: Vec<String>,
|
||||
// Read from the raw parameters by the surface preparation (mesh.tet.generate's options).
|
||||
#[allow(dead_code)]
|
||||
weld: Option<f64>,
|
||||
#[allow(dead_code)]
|
||||
weld_distance: Option<f64>,
|
||||
#[allow(dead_code)]
|
||||
feature_angle: Option<f64>,
|
||||
#[serde(default)]
|
||||
preview: bool,
|
||||
}
|
||||
|
||||
/// The body, its tessellation as a boundary surface and projection target, and its size.
|
||||
pub(crate) struct Body {
|
||||
input: super::hex::Input<'static>,
|
||||
surface: BoundarySurface,
|
||||
target: Tessellation,
|
||||
lo: [f64; 3],
|
||||
hi: [f64; 3],
|
||||
}
|
||||
|
||||
impl Body {
|
||||
/// The body's face names, by face index.
|
||||
pub(crate) fn face_names(&self) -> &[String] {
|
||||
&self.input.face_names
|
||||
}
|
||||
|
||||
/// The body's bounding-box diagonal.
|
||||
pub(crate) fn diagonal(&self) -> f64 {
|
||||
self.input.diagonal
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn open_body(s: &Session, name: &str, tolerance: Option<f64>, raw: &Value) -> Result<Body> {
|
||||
let shape =
|
||||
s.design.as_deref().and_then(|d| d.body(name)).ok_or_else(|| EngineError::Params(format!("no body `{name}` (open a design first)")))?;
|
||||
if tolerance.is_some_and(|t| !(t > 0.0 && t.is_finite())) {
|
||||
return Err(EngineError::Params("`tolerance` must be a positive number".into()));
|
||||
}
|
||||
let input = super::hex::shape_input(shape, tolerance)?;
|
||||
let surface =
|
||||
BoundarySurface::new(&input.mesh.positions, &input.mesh.triangles, &input.mesh.tri_face, &super::mesh::surface_options(raw, &input.mesh)?)
|
||||
.map_err(|e| EngineError::Params(format!("body `{name}`: {e}")))?;
|
||||
let target = Tessellation::new(&surface, super::mesh::analytic_surfaces(shape)).with_pieces(&surface, super::mesh::pieces(shape, &input.pieces));
|
||||
let m = shape.measure()?;
|
||||
Ok(Body { input, surface, target, lo: m.bbox_min, hi: m.bbox_max })
|
||||
}
|
||||
|
||||
fn face_index(b: &Body, body: &str, name: &str) -> Result<u32> {
|
||||
b.input
|
||||
.face_names
|
||||
.iter()
|
||||
.position(|f| f == name)
|
||||
.map(|i| i as u32)
|
||||
.ok_or_else(|| EngineError::Params(format!("body `{body}` has no face `{name}`")))
|
||||
}
|
||||
|
||||
fn element_size(size: Option<f64>, diag: f64) -> Result<f64> {
|
||||
let h = size.unwrap_or(DEFAULT_RELATIVE_SIZE * diag);
|
||||
if !(h.is_finite() && h >= MIN_RELATIVE_SIZE * diag) {
|
||||
return Err(EngineError::Params(format!("`size` must be at least {} mm", MIN_RELATIVE_SIZE * diag)));
|
||||
}
|
||||
Ok(h)
|
||||
}
|
||||
|
||||
pub(crate) fn merge_distance(merge: Option<f64>, diag: f64) -> Result<f64> {
|
||||
let m = merge.unwrap_or(1e-6 * diag);
|
||||
if !(m.is_finite() && m >= 0.0 && m <= 1e-2 * diag) {
|
||||
return Err(EngineError::Params(format!("`merge` must be from 0 to 1/100 of the body's diagonal ({diag})")));
|
||||
}
|
||||
Ok(m)
|
||||
}
|
||||
|
||||
/// Replace `body`'s mesh with `nodes` and `hexes`, sharing nodes of other bodies within
|
||||
/// `merge`; tag the elements with the body and `boundary` with its B-rep faces.
|
||||
fn add_mesh(
|
||||
s: &mut Session,
|
||||
body: &super::hex::Input,
|
||||
nodes: &[[f64; 3]],
|
||||
hexes: &[[u32; 8]],
|
||||
boundary: &[BoundaryFace],
|
||||
merge: f64,
|
||||
) -> Result<(Value, u32, usize)> {
|
||||
let mut model = s.model.clone();
|
||||
let cleared = super::replace::clear(&mut model, [body.name.as_str()]);
|
||||
let (first_node, first_element) = super::fresh_labels(&model, nodes.len(), hexes.len())?;
|
||||
let welded = if merge > 0.0 { weld(&model, nodes, merge)? } else { vec![None; nodes.len()] };
|
||||
let mut labels = Vec::with_capacity(nodes.len());
|
||||
let mut added = 0u32;
|
||||
for (xyz, existing) in nodes.iter().zip(&welded) {
|
||||
let label = match existing {
|
||||
Some(l) => *l,
|
||||
None => {
|
||||
let l = first_node + added;
|
||||
model.set_node(l, *xyz)?;
|
||||
added += 1;
|
||||
l
|
||||
}
|
||||
};
|
||||
labels.push(label);
|
||||
}
|
||||
let joined: BTreeSet<u32> = welded.iter().flatten().copied().collect();
|
||||
let label = |i: u32| labels.get(i as usize).copied().ok_or_else(|| EngineError::Params(format!("node {i} has no label")));
|
||||
let elements: Vec<(ElementKind, Vec<u32>)> =
|
||||
hexes.iter().map(|h| Ok((ElementKind::Hex8, h.iter().map(|&i| label(i)).collect::<Result<Vec<u32>>>()?))).collect::<Result<_>>()?;
|
||||
check_conforming(&model, &elements, &joined, merge)?;
|
||||
let body_tag = model.geometry_tag(&GeometryRef::body(body.name.as_str())?)?;
|
||||
for (l, (kind, nodes)) in (first_element..).zip(elements) {
|
||||
model.set_element(l, kind, nodes)?;
|
||||
model.tag_element(l, body_tag)?;
|
||||
}
|
||||
super::hex::tag_faces(&mut model, std::slice::from_ref(body), |_| 0, boundary, first_element)?;
|
||||
s.model = model;
|
||||
let reply = json!({"first_node": first_node, "first_element": first_element, "nodes": added, "joined": joined.len()});
|
||||
let reply = cleared.report(s, reply);
|
||||
Ok((reply, first_element, joined.len()))
|
||||
}
|
||||
|
||||
fn extend(reply: &mut Value, more: Value) {
|
||||
if let (Some(o), Some(m)) = (reply.as_object_mut(), more.as_object()) {
|
||||
for (k, v) in m {
|
||||
o.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn round3(x: f64) -> Value {
|
||||
if x.is_finite() { json!((x * 1000.0).round() / 1000.0) } else { Value::Null }
|
||||
}
|
||||
|
||||
fn bounded<T>(v: &[T], what: &str) -> Result<()> {
|
||||
if v.len() > MAX_LIST { Err(EngineError::Params(format!("`{what}` lists more than {MAX_LIST} entries"))) } else { Ok(()) }
|
||||
}
|
||||
|
||||
/// The blocks for a reply: corners (positions before projection) and divisions.
|
||||
fn listing(t: &Topology, divisions: Option<&[[usize; 3]]>) -> Vec<Value> {
|
||||
t.blocks
|
||||
.iter()
|
||||
.enumerate()
|
||||
.take(500)
|
||||
.map(|(i, v)| {
|
||||
let corners: Vec<Value> =
|
||||
v.iter().map(|&k| t.vertices.get(k as usize).map_or(Value::Null, |p| json!(p.map(|x| (x * 1e4).round() / 1e4)))).collect();
|
||||
let mut b = json!({"block": i, "corners": corners});
|
||||
if let Some(d) = divisions.and_then(|d| d.get(i)) {
|
||||
b["divisions"] = json!(d);
|
||||
}
|
||||
b
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The base topology from the template parameters.
|
||||
fn topology(p: &ProjectParams, lo: [f64; 3], hi: [f64; 3]) -> Result<Topology> {
|
||||
let centre = [0, 1, 2].map(|d| 0.5 * (lo[d] + hi[d]));
|
||||
let inset = p.inset.unwrap_or(0.55);
|
||||
let t = match p.template.as_deref().unwrap_or(if p.blocks.is_some() { "blocks" } else { "grid" }) {
|
||||
"sphere" => {
|
||||
let r = p.radius.unwrap_or_else(|| (0..3).map(|d| 0.5 * (hi[d] - lo[d])).fold(0.0, f64::max));
|
||||
Topology::sphere(p.center.unwrap_or(centre), r, inset)?
|
||||
}
|
||||
"butterfly" => {
|
||||
let axis = p.axis.unwrap_or([0.0, 0.0, 1.0]);
|
||||
let c = p.center.unwrap_or(centre);
|
||||
let len = (axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]).sqrt();
|
||||
if !(len > 0.0 && len.is_finite()) {
|
||||
return Err(EngineError::invalid("axis", "must be a nonzero direction"));
|
||||
}
|
||||
let a = axis.map(|x| x / len);
|
||||
// The bounding box's extent along the axis and across it.
|
||||
let corners: Vec<[f64; 3]> = (0..8).map(|k| [0, 1, 2].map(|d| if k >> d & 1 == 1 { hi[d] } else { lo[d] })).collect();
|
||||
let along: Vec<f64> = corners.iter().map(|q| (0..3).map(|d| (q[d] - c[d]) * a[d]).sum()).collect();
|
||||
let across = corners
|
||||
.iter()
|
||||
.zip(&along)
|
||||
.map(|(q, s)| (0..3).map(|d| (q[d] - c[d] - s * a[d]).powi(2)).sum::<f64>().sqrt() / 2f64.sqrt())
|
||||
.fold(0.0, f64::max);
|
||||
let r = p.radius.unwrap_or(across);
|
||||
let levels = p.levels.clone().unwrap_or_else(|| {
|
||||
vec![along.iter().copied().fold(f64::INFINITY, f64::min), along.iter().copied().fold(f64::NEG_INFINITY, f64::max)]
|
||||
});
|
||||
if levels.len() > 1001 {
|
||||
return Err(EngineError::Params("`levels` lists more than 1001 levels".into()));
|
||||
}
|
||||
Topology::butterfly(c, a, r, &levels, inset)?
|
||||
}
|
||||
"blocks" => {
|
||||
let b = p.blocks.as_ref().ok_or_else(|| EngineError::Params("template `blocks` needs `blocks`: {vertices, blocks}".into()))?;
|
||||
bounded(&b.blocks, "blocks.blocks")?;
|
||||
if b.vertices.len() > 8 * MAX_LIST {
|
||||
return Err(EngineError::Params("`blocks.vertices` lists too many vertices".into()));
|
||||
}
|
||||
Topology { vertices: b.vertices.clone(), blocks: b.blocks.clone() }
|
||||
}
|
||||
"grid" => {
|
||||
let g = p.grid.as_ref();
|
||||
let planes: [Vec<f64>; 3] = match g.and_then(|g| g.planes.as_ref()) {
|
||||
Some(pl) => {
|
||||
if let Some(k) = pl.keys().find(|k| !matches!(k.as_str(), "x" | "y" | "z")) {
|
||||
return Err(EngineError::Params(format!("grid planes are keyed x, y and z, not `{k}`")));
|
||||
}
|
||||
[("x", 0), ("y", 1), ("z", 2)].map(|(k, d)| pl.get(k).cloned().unwrap_or_else(|| vec![lo[d], hi[d]]))
|
||||
}
|
||||
None => {
|
||||
let cells = g.and_then(|g| g.cells).unwrap_or([1, 1, 1]);
|
||||
if cells.iter().any(|&c| !(1..=1000).contains(&c)) {
|
||||
return Err(EngineError::invalid("grid.cells", "1 to 1000 per direction"));
|
||||
}
|
||||
[0, 1, 2].map(|d| (0..=cells[d]).map(|q| lo[d] + (hi[d] - lo[d]) * q as f64 / cells[d] as f64).collect())
|
||||
}
|
||||
};
|
||||
if planes.iter().any(|p| p.len() > 1001) {
|
||||
return Err(EngineError::Params("at most 1000 grid cells per direction".into()));
|
||||
}
|
||||
let mut t = Topology::grid([&planes[0], &planes[1], &planes[2]])?;
|
||||
if let Some(carve) = g.map(|g| &g.carve).filter(|c| !c.is_empty()) {
|
||||
bounded(carve, "grid.carve")?;
|
||||
let n = planes.each_ref().map(|p| p.len() - 1);
|
||||
let mut ranges = Vec::with_capacity(carve.len());
|
||||
for c in carve {
|
||||
if let Some(k) = c.keys().find(|k| !matches!(k.as_str(), "i" | "j" | "k")) {
|
||||
return Err(EngineError::Params(format!("carve ranges are keyed i, j and k, not `{k}`")));
|
||||
}
|
||||
ranges.push([("i", 0), ("j", 1), ("k", 2)].map(|(k, d)| c.get(k).copied().unwrap_or([0, n[d].saturating_sub(1)])));
|
||||
}
|
||||
t.carve(n, &ranges)?;
|
||||
}
|
||||
t
|
||||
}
|
||||
other => return Err(EngineError::Params(format!("unknown template `{other}` (grid, sphere, butterfly or blocks)"))),
|
||||
};
|
||||
Ok(t)
|
||||
}
|
||||
|
||||
fn hex_project(s: &mut Session, raw: &Value) -> Result<Value> {
|
||||
let p: ProjectParams = serde_json::from_value(raw.clone()).map_err(|e| EngineError::Params(format!("mesh.hex.project: {e}")))?;
|
||||
bounded(&p.operations, "operations")?;
|
||||
bounded(&p.faces, "faces")?;
|
||||
bounded(&p.divisions, "divisions")?;
|
||||
bounded(&p.fixed, "fixed")?;
|
||||
let b = open_body(s, &p.body, p.tolerance, raw)?;
|
||||
let diag = b.input.diagonal;
|
||||
let sizing = super::sizes::sizing(s)?;
|
||||
let size = element_size(sizing.bodies.get(&p.body).copied().or(p.size), diag)?;
|
||||
let field = sizing.local_field(size)?;
|
||||
let mut t = topology(&p, b.lo, b.hi)?;
|
||||
operate(&mut t, &p.operations)?;
|
||||
if p.preview {
|
||||
return Ok(json!({"body": p.body, "blocks": t.blocks.len(), "topology": listing(&t, None)}));
|
||||
}
|
||||
let mut faces = Vec::with_capacity(p.faces.len());
|
||||
for f in &p.faces {
|
||||
bounded(&f.to, "faces.to")?;
|
||||
faces.push((f.block, face_of(&t, f.block, &f.face)?, f.to.iter().map(|n| face_index(&b, &p.body, n)).collect::<Result<Vec<u32>>>()?));
|
||||
}
|
||||
let mut divisions = Vec::with_capacity(p.divisions.len());
|
||||
for d in &p.divisions {
|
||||
if !(1..=3).contains(&d.direction) {
|
||||
return Err(EngineError::invalid("divisions.direction", "1, 2 or 3 (along 1→2, 1→4, 1→5)"));
|
||||
}
|
||||
divisions.push((d.block, d.direction - 1, d.count));
|
||||
}
|
||||
let smoothing = match p.smoothing.as_deref().unwrap_or("elliptic") {
|
||||
"elliptic" => Smoothing::Elliptic,
|
||||
"elliptic-controlled" => Smoothing::EllipticControlled,
|
||||
"laplace" => Smoothing::Laplace,
|
||||
"none" => Smoothing::None,
|
||||
other => return Err(EngineError::Params(format!("unknown smoothing `{other}` (elliptic, elliptic-controlled, laplace or none)"))),
|
||||
};
|
||||
let iterations = p.iterations.unwrap_or(100);
|
||||
if iterations > 10_000 {
|
||||
return Err(EngineError::invalid("iterations", "at most 10000"));
|
||||
}
|
||||
let min_quality = p.min_quality.unwrap_or(fieldcraft_mesh::project::DEFAULT_MIN_QUALITY);
|
||||
if !(-1.0..=1.0).contains(&min_quality) {
|
||||
return Err(EngineError::invalid("min_quality", "from -1 to 1"));
|
||||
}
|
||||
let max_arc_angle = p.max_arc_angle.unwrap_or(fieldcraft_mesh::project::DEFAULT_MAX_ARC_ANGLE.to_degrees());
|
||||
if !(0.0..=90.0).contains(&max_arc_angle) {
|
||||
return Err(EngineError::invalid("max_arc_angle", "from 0 to 90 degrees"));
|
||||
}
|
||||
let fixed = if p.fixed.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
let patch = fixed_patch(s, &b, &p.body, &p.fixed, size)?;
|
||||
if patch.quads.is_empty() {
|
||||
return Err(EngineError::Params(format!(
|
||||
"`fixed`: no other body's mesh has element faces on faces {:?} of `{}`; mesh the neighbour first",
|
||||
p.fixed, p.body
|
||||
)));
|
||||
}
|
||||
vec![patch]
|
||||
};
|
||||
let opts = ProjectOptions {
|
||||
size,
|
||||
max_arc_angle: max_arc_angle.to_radians(),
|
||||
divisions,
|
||||
faces,
|
||||
smoothing,
|
||||
iterations,
|
||||
max_elements: fieldcraft_model::MAX_ELEMENTS / 8,
|
||||
min_quality,
|
||||
fixed,
|
||||
planes: Vec::new(),
|
||||
field,
|
||||
};
|
||||
let merge = merge_distance(p.merge, diag)?;
|
||||
project_body(s, &p.body, b, &t, &opts, merge)
|
||||
}
|
||||
|
||||
/// The projection mesh of topology `t` over body `b`, not added to the model.
|
||||
pub(crate) fn project_only(b: &Body, name: &str, t: &Topology, opts: &ProjectOptions) -> Result<fieldcraft_mesh::project::ProjectedMesh> {
|
||||
project_mesh(t, &b.surface, &b.target, opts).map_err(|e| EngineError::of_body(name, e))
|
||||
}
|
||||
|
||||
/// Replace body `name`'s mesh (opened as `b`) with `nodes` and `hexes` made for it elsewhere
|
||||
/// (a part of another body's mesh): its outer faces (those no other of the hexes shares) tagged
|
||||
/// with the face `tags` names for them (element, face number) when `b` has it, else with the
|
||||
/// B-rep face of `b` nearest their middle. Nodes within `merge` of other meshes' join them.
|
||||
pub(crate) fn add_hexes(
|
||||
s: &mut Session,
|
||||
b: &Body,
|
||||
nodes: &[[f64; 3]],
|
||||
hexes: &[[u32; 8]],
|
||||
tags: &BTreeMap<(u32, u8), String>,
|
||||
merge: f64,
|
||||
) -> Result<Value> {
|
||||
use fieldcraft_mesh::project::Geometry;
|
||||
let faces = ElementKind::Hex8.faces();
|
||||
let mut count: BTreeMap<[u32; 4], usize> = BTreeMap::new();
|
||||
let quad = |h: &[u32; 8], f: &[usize]| -> [u32; 4] { [0, 1, 2, 3].map(|i| f.get(i).and_then(|&l| h.get(l)).copied().unwrap_or(0)) };
|
||||
for h in hexes {
|
||||
for f in faces {
|
||||
let mut k = quad(h, f);
|
||||
k.sort_unstable();
|
||||
*count.entry(k).or_insert(0) += 1;
|
||||
}
|
||||
}
|
||||
let face_count = b.input.face_names.len() as u32;
|
||||
let mut boundary = Vec::new();
|
||||
for (e, h) in hexes.iter().enumerate() {
|
||||
for (fi, f) in faces.iter().enumerate() {
|
||||
let q = quad(h, f);
|
||||
let mut k = q;
|
||||
k.sort_unstable();
|
||||
if count.get(&k) != Some(&1) {
|
||||
continue;
|
||||
}
|
||||
let mid = q.iter().fold([0.0; 3], |a, &v| {
|
||||
let p = nodes.get(v as usize).copied().unwrap_or_default();
|
||||
[a[0] + 0.25 * p[0], a[1] + 0.25 * p[1], a[2] + 0.25 * p[2]]
|
||||
});
|
||||
if let Some(f) = tags.get(&(e as u32, fi as u8 + 1)).and_then(|n| b.input.face_names.iter().position(|x| x == n)) {
|
||||
boundary.push(BoundaryFace { element: e as u32, face: fi as u8 + 1, brep_face: f as u32 });
|
||||
continue;
|
||||
}
|
||||
let nearest = (0..face_count)
|
||||
.filter_map(|f| b.target.project(f, mid).map(|p| ((0..3).map(|i| (p[i] - mid[i]).powi(2)).sum::<f64>(), f)))
|
||||
.min_by(|x, y| x.0.total_cmp(&y.0));
|
||||
if let Some((_, brep_face)) = nearest {
|
||||
boundary.push(BoundaryFace { element: e as u32, face: fi as u8 + 1, brep_face });
|
||||
}
|
||||
}
|
||||
}
|
||||
let (reply, _, _) = add_mesh(s, &b.input, nodes, hexes, &boundary, merge)?;
|
||||
Ok(reply)
|
||||
}
|
||||
|
||||
/// Fraction of the element size within which a node of another mesh lies on a face.
|
||||
const ON_FACE: f64 = 0.05;
|
||||
|
||||
/// The quads other bodies' meshes have on faces `names` of body `body` (opened as `b`), as one
|
||||
/// fixed patch: the element faces of other bodies, tagged as on their boundary, whose nodes all
|
||||
/// lie on one of those faces (within [`ON_FACE`] of the element size `size`). Empty when no other
|
||||
/// mesh is there.
|
||||
pub(crate) fn fixed_patch(s: &Session, b: &Body, body: &str, names: &[String], size: f64) -> Result<FixedPatch> {
|
||||
use fieldcraft_mesh::project::Geometry;
|
||||
let faces: Vec<u32> = names.iter().map(|n| face_index(b, body, n)).collect::<Result<_>>()?;
|
||||
let tol = (ON_FACE * size).max(1e-9 * b.diagonal());
|
||||
let c = s.model.classification();
|
||||
let mut on: BTreeMap<u32, Option<usize>> = BTreeMap::new();
|
||||
let mut index: BTreeMap<u32, u32> = BTreeMap::new();
|
||||
let mut seen: BTreeSet<[u32; 4]> = BTreeSet::new();
|
||||
let mut patch = FixedPatch { faces: faces.clone(), ..FixedPatch::default() };
|
||||
for (&element, tags) in c.elements() {
|
||||
let theirs = tags.region.and_then(|r| c.entity(r)).is_some_and(|g| g.body != body);
|
||||
if !theirs {
|
||||
continue;
|
||||
}
|
||||
for &face in tags.faces.keys() {
|
||||
let Some(labels) = s.model.face_nodes(fieldcraft_model::ElementFace { element, face }) else { continue };
|
||||
let Ok(quad) = <[u32; 4]>::try_from(labels) else { continue };
|
||||
let mut lies = true;
|
||||
for l in quad {
|
||||
let hit = *on.entry(l).or_insert_with(|| {
|
||||
let x = s.model.node(l)?.xyz;
|
||||
faces
|
||||
.iter()
|
||||
.position(|&f| b.target.project(f, x).is_some_and(|q| (0..3).map(|k| (q[k] - x[k]).powi(2)).sum::<f64>().sqrt() <= tol))
|
||||
});
|
||||
lies &= hit.is_some();
|
||||
}
|
||||
let mut key = quad;
|
||||
key.sort_unstable();
|
||||
if !lies || !seen.insert(key) {
|
||||
continue;
|
||||
}
|
||||
let q = quad.map(|l| {
|
||||
*index.entry(l).or_insert_with(|| {
|
||||
patch.nodes.push(s.model.node(l).map_or([0.0; 3], |n| n.xyz));
|
||||
patch.nodes.len() as u32 - 1
|
||||
})
|
||||
});
|
||||
patch.quads.push(q);
|
||||
}
|
||||
}
|
||||
Ok(patch)
|
||||
}
|
||||
|
||||
/// Mesh body `name` (opened as `b`) with the block topology `t` projected onto it, replace its
|
||||
/// mesh, and reply what `mesh.hex.project` replies. The plan's projection mesher (plan.rs) takes
|
||||
/// the same path.
|
||||
pub(crate) fn project_body(s: &mut Session, name: &str, b: Body, t: &Topology, opts: &ProjectOptions, merge: f64) -> Result<Value> {
|
||||
let m = project_mesh(t, &b.surface, &b.target, opts).map_err(|e| EngineError::of_body(name, e))?;
|
||||
let r = &m.report;
|
||||
let names = b.input.face_names.clone();
|
||||
let face_name = |f: &u32| names.get(*f as usize).cloned().unwrap_or_else(|| f.to_string());
|
||||
let blocks = listing(t, Some(&r.divisions));
|
||||
let targets: Vec<Value> = r.targets.iter().map(|set| json!(set.iter().map(face_name).collect::<Vec<_>>())).collect();
|
||||
let mut reply = json!({
|
||||
"body": name,
|
||||
"kind": "hex8",
|
||||
"blocks": r.blocks,
|
||||
"elements": r.elements,
|
||||
"initial_min_scaled_jacobian": round3(r.initial_min_scaled_jacobian),
|
||||
"min_scaled_jacobian": round3(r.min_scaled_jacobian),
|
||||
"mean_scaled_jacobian": round3(r.mean_scaled_jacobian),
|
||||
"smoothing": {"iterations": r.smoothing.iterations, "refused": r.smoothing.refused},
|
||||
"fixed_nodes": r.fixed_nodes,
|
||||
"max_gap": r.max_gap,
|
||||
"volume": r.volume,
|
||||
"body_volume": r.surface_volume,
|
||||
"targets": targets,
|
||||
"topology": blocks,
|
||||
});
|
||||
let (more, _, _) = add_mesh(s, &b.input, &m.nodes, &m.hexes, &m.boundary, merge)?;
|
||||
extend(&mut reply, more);
|
||||
Ok(reply)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,300 @@
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// A session with the design the SolidCraft commands build.
|
||||
fn session(commands: Value) -> (crate::tests::TempDir, Session) {
|
||||
let bytes = fieldcraft_geometry::script_design(&json!({ "commands": commands }).to_string()).unwrap();
|
||||
let (dir, path) = crate::tests::design_file(&bytes);
|
||||
let mut s = Session::default();
|
||||
s.execute("geometry.open", &json!({"path": path})).unwrap();
|
||||
(dir, s)
|
||||
}
|
||||
|
||||
fn sj(r: &Value) -> f64 {
|
||||
r["min_scaled_jacobian"].as_f64().unwrap()
|
||||
}
|
||||
|
||||
fn faces(s: &mut Session, body: &str) -> Vec<String> {
|
||||
let b = s.execute("geometry.bodies", &json!({})).unwrap();
|
||||
let body = b.as_array().unwrap().iter().find(|x| x["name"] == body).unwrap().clone();
|
||||
body["faces"].as_array().unwrap().iter().map(|f| f["name"].as_str().unwrap().to_string()).collect()
|
||||
}
|
||||
|
||||
/// The 7-block sphere at two refinements: all hex, min scaled Jacobian at least 0.5, the volume
|
||||
/// converging, and a set on the sphere's face resolving to the projected boundary.
|
||||
#[test]
|
||||
fn sphere_seven_blocks_at_two_refinements() {
|
||||
let (_d, mut s) = session(json!([{"command": "solid.sphere", "params": {"radius": 10, "body_name": "Ball"}}]));
|
||||
let exact = 4.0 / 3.0 * std::f64::consts::PI * 1000.0;
|
||||
let mut errors = Vec::new();
|
||||
for size in [3.0, 1.5] {
|
||||
let r = s.execute("mesh.hex.project", &json!({"body": "Ball", "template": "sphere", "size": size})).unwrap();
|
||||
assert_eq!(r["blocks"], 7, "{r}");
|
||||
assert!(sj(&r) >= 0.5, "{r}");
|
||||
errors.push((r["volume"].as_f64().unwrap() - exact).abs() / exact);
|
||||
assert_eq!(s.model.elements().len() as u64, r["elements"].as_u64().unwrap());
|
||||
}
|
||||
assert!(errors[1] < errors[0] && errors[1] < 0.03, "{errors:?}");
|
||||
let names = faces(&mut s, "Ball");
|
||||
let geometry: Vec<Value> = names.iter().map(|f| json!({"body": "Ball", "face": f})).collect();
|
||||
s.execute("model.set.create", &json!({"name": "skin", "geometry": geometry})).unwrap();
|
||||
let skin = s.model.resolve_set("skin").unwrap();
|
||||
assert!(!skin.faces.is_empty());
|
||||
let q = s.execute("mesh.quality", &json!({})).unwrap();
|
||||
assert_eq!(q["valid"].as_u64(), Some(s.model.elements().len() as u64), "{q}");
|
||||
}
|
||||
|
||||
/// A disc: the five-block butterfly, its rims on both circles.
|
||||
#[test]
|
||||
fn disc_butterfly() {
|
||||
let (_d, mut s) = session(json!([{"command": "solid.cylinder", "params": {"radius": 10, "height": 2, "body_name": "Disc"}}]));
|
||||
let r = s.execute("mesh.hex.project", &json!({"body": "Disc", "template": "butterfly", "size": 1})).unwrap();
|
||||
assert_eq!(r["blocks"], 5, "{r}");
|
||||
assert!(sj(&r) >= 0.5, "{r}");
|
||||
let v = r["volume"].as_f64().unwrap();
|
||||
assert!((v - std::f64::consts::PI * 200.0).abs() < 0.01 * 200.0 * std::f64::consts::PI, "{r}");
|
||||
assert_eq!(r["targets"].as_array().unwrap().len(), 3, "{r}");
|
||||
}
|
||||
|
||||
/// A cylinder with a side branch: the main tube a butterfly along X, the top block of its middle
|
||||
/// layer O-gridded open upward and its open face extruded into the branch; the junction edges go
|
||||
/// onto the curve where the two cylinders meet.
|
||||
#[test]
|
||||
fn cylinder_with_a_side_branch() {
|
||||
let (_d, mut s) = session(json!([
|
||||
{"command": "solid.cylinder", "params": {"base": [-30, 0, 0], "axis": "X", "radius": 10, "height": 60, "body_name": "Tee"}},
|
||||
{"command": "solid.cylinder", "params": {"base": [0, 0, 0], "axis": "Z", "radius": 7, "height": 25, "operation": "join", "targets": ["Tee"]}}
|
||||
]));
|
||||
let r = s
|
||||
.execute(
|
||||
"mesh.hex.project",
|
||||
&json!({"body": "Tee", "template": "butterfly", "center": [0, 0, 0], "axis": [1, 0, 0], "radius": 10, "levels": [-30, -7, 7, 30], "size": 1.5,
|
||||
"operations": [{"op": "o_grid", "block": 8, "open": ["+z"], "inset": 0.5, "extrude": {"offset": [0, 0, 18], "layers": 2}}]}),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(sj(&r) >= 0.3, "{r}");
|
||||
let exact = std::f64::consts::PI * (100.0 * 60.0) + std::f64::consts::PI * 49.0 * 15.0;
|
||||
assert!((r["volume"].as_f64().unwrap() - exact).abs() < 0.03 * exact, "{r}");
|
||||
assert!(r["max_gap"].as_f64().unwrap() < 1e-6, "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_parameters_change_nothing() {
|
||||
let (_d, mut s) = session(json!([{"command": "solid.sphere", "params": {"radius": 10, "body_name": "Ball"}}]));
|
||||
let model = s.model.clone();
|
||||
for bad in [
|
||||
json!({"body": "Nope", "template": "sphere"}),
|
||||
json!({"body": "Ball", "template": "teapot"}),
|
||||
json!({"body": "Ball", "template": "sphere", "size": -1}),
|
||||
json!({"body": "Ball", "template": "sphere", "size": 1e-9}),
|
||||
json!({"body": "Ball", "template": "sphere", "radius": f64::MAX}),
|
||||
json!({"body": "Ball", "template": "sphere", "inset": 2}),
|
||||
json!({"body": "Ball", "template": "sphere", "smoothing": "magic"}),
|
||||
json!({"body": "Ball", "template": "sphere", "iterations": 1_000_000}),
|
||||
json!({"body": "Ball", "template": "sphere", "faces": [{"block": 0, "face": 1, "to": ["nope"]}]}),
|
||||
json!({"body": "Ball", "template": "sphere", "divisions": [{"block": 99, "direction": 1, "count": 2}]}),
|
||||
json!({"body": "Ball", "template": "sphere", "divisions": [{"block": 0, "direction": 4, "count": 2}]}),
|
||||
json!({"body": "Ball", "template": "sphere", "operations": [{"op": "remove", "blocks": [0, 1, 2, 3, 4, 5, 6]}]}),
|
||||
json!({"body": "Ball", "template": "sphere", "operations": [{"op": "o_grid", "block": 77}]}),
|
||||
json!({"body": "Ball", "template": "sphere", "operations": [{"op": "extrude", "faces": [[0, "+w"]], "offset": [0, 0, 1]}]}),
|
||||
json!({"body": "Ball", "template": "sphere", "operations": [{"op": "merge", "distance": -1}]}),
|
||||
json!({"body": "Ball", "template": "sphere", "operations": [{"op": "split", "block": 0, "direction": "w", "at": [0.5]}]}),
|
||||
json!({"body": "Ball", "template": "sphere", "operations": [{"op": "split", "block": 0, "direction": 4, "at": [0.5]}]}),
|
||||
json!({"body": "Ball", "template": "sphere", "operations": [{"op": "split", "block": 0, "direction": "x", "at": [1.5]}]}),
|
||||
json!({"body": "Ball", "template": "sphere", "operations": [{"op": "split", "block": 99, "direction": 1, "at": [0.5]}]}),
|
||||
json!({"body": "Ball", "template": "blocks"}),
|
||||
json!({"body": "Ball", "template": "blocks", "blocks": {"vertices": [[0, 0, 0]], "blocks": [[0, 0, 0, 0, 0, 0, 0, 0]]}}),
|
||||
json!({"body": "Ball", "grid": {"cells": [0, 1, 1]}}),
|
||||
json!({"body": "Ball", "grid": {"cells": [2, 2, 2], "carve": [{"q": [0, 1]}]}}),
|
||||
json!({"body": "Ball", "template": "sphere", "unknown": 1}),
|
||||
] {
|
||||
assert!(s.execute("mesh.hex.project", &bad).is_err(), "{bad}");
|
||||
assert_eq!(s.model, model, "{bad}");
|
||||
}
|
||||
// A preview lists the topology without meshing.
|
||||
let r = s.execute("mesh.hex.project", &json!({"body": "Ball", "template": "sphere", "preview": true})).unwrap();
|
||||
assert_eq!(r["topology"].as_array().unwrap().len(), 7);
|
||||
assert_eq!(s.model, model);
|
||||
}
|
||||
|
||||
fn cup_session() -> (crate::tests::TempDir, Session) {
|
||||
session(json!([
|
||||
{"command": "solid.cylinder", "params": {"radius": 20, "height": 30, "body_name": "Cup"}},
|
||||
{"command": "solid.cylinder", "params": {"base": [0, 0, 4], "radius": 16, "height": 27, "operation": "cut", "targets": ["Cup"]}},
|
||||
{"command": "solid.box", "params": {"length": 11, "width": 6, "height": 16, "corner": [19, -3, 8], "operation": "new", "body_name": "Handle"}},
|
||||
{"command": "solid.box", "params": {"length": 8, "width": 8, "height": 8, "corner": [18, -4, 12], "operation": "cut", "targets": ["Handle"]}},
|
||||
{"command": "solid.combine", "params": {"target": "Cup", "tools": ["Handle"], "operation": "join"}}
|
||||
]))
|
||||
}
|
||||
|
||||
fn cup_params(size: f64, inset: Value) -> Value {
|
||||
json!({"body": "Cup", "template": "butterfly", "center": [0, 0, 0], "axis": [0, 0, 1], "radius": 20, "levels": [0, 4, 6, 14, 18, 26, 30], "size": size,
|
||||
"operations": [
|
||||
{"op": "o_grid", "block": 0, "open": [1, 2], "inset": 0.6},
|
||||
{"op": "split", "block": 14, "direction": "y", "at": [0.36, 0.64]},
|
||||
{"op": "o_grid", "block": 54, "open": ["+x"], "inset": inset},
|
||||
{"op": "o_grid", "block": 62, "open": ["+x"], "inset": inset},
|
||||
{"op": "extrude", "faces": [[54, "+x"]], "offset": [12, 0, 0]},
|
||||
{"op": "extrude", "faces": [[84, "+x"]], "offset": [4, 0, 0]},
|
||||
{"op": "extrude", "faces": [[62, "+x"]], "offset": [12, 0, 0]},
|
||||
{"op": "extrude", "faces": [[86, "+x"]], "offset": [4, 0, 0]},
|
||||
{"op": "connect", "from": [85, "+z"], "to": [87, "-z"], "layers": 2},
|
||||
{"op": "remove", "blocks": [1, 2, 3, 4, 5, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45]}
|
||||
]})
|
||||
}
|
||||
|
||||
/// A cup with a handle: a double O-grid section (the floor's butterfly inside the wall's), the
|
||||
/// cavity carved, the handle a square bar extruded from O-grids in the wall and closed by a
|
||||
/// connection. The wall's +x petals are split in three around the handle first, so each arm's
|
||||
/// footprint sits in a block face with even margins round it (#399).
|
||||
fn cup_with_a_handle_at(size: f64) {
|
||||
let (_d, mut s) = cup_session();
|
||||
let r = s.execute("mesh.hex.project", &cup_params(size, json!(0.5))).unwrap();
|
||||
assert_thin_layers_hold(&mut s, &r);
|
||||
let (v, body) = (r["volume"].as_f64().unwrap(), r["body_volume"].as_f64().unwrap());
|
||||
assert!((v - body).abs() < 0.01 * body, "{r}");
|
||||
assert!(r["max_gap"].as_f64().unwrap() < 1e-6, "{r}");
|
||||
let q = s.execute("mesh.quality", &json!({})).unwrap();
|
||||
assert_eq!(q["valid"].as_u64(), Some(s.model.elements().len() as u64), "{q}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cup_with_a_handle() {
|
||||
cup_with_a_handle_at(2.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "heavy: cargo xtask test --heavy (CI heavy job)"]
|
||||
fn cup_with_a_handle_refined() {
|
||||
cup_with_a_handle_at(1.0);
|
||||
cup_with_a_handle_at(0.75);
|
||||
}
|
||||
|
||||
/// Every element valid, the scaled Jacobian at least 0.5 and no edge ratio above 10 (#418).
|
||||
fn assert_thin_layers_hold(s: &mut Session, r: &Value) {
|
||||
assert!(sj(r) >= 0.5, "{r}");
|
||||
let q = s.execute("mesh.quality", &json!({})).unwrap();
|
||||
assert_eq!(q["valid"].as_u64(), Some(s.model.elements().len() as u64), "{q}");
|
||||
assert!(q["metrics"]["scaled_jacobian"]["min"].as_f64().unwrap() >= 0.5, "{q}");
|
||||
assert!(q["metrics"]["edge_ratio"]["max"].as_f64().unwrap() <= 10.0, "{q}");
|
||||
}
|
||||
|
||||
/// Without the split, the ring block under the upper arm is squeezed between the petal's edge
|
||||
/// and the window-top plane: the mesher refuses the mesh and names the worst element's block.
|
||||
#[test]
|
||||
fn squeezed_junction_elements_are_reported_not_meshed() {
|
||||
let (_d, mut s) = cup_session();
|
||||
let mut p = cup_params(2.0, json!([0.5, 0.3, 0.3]));
|
||||
p["operations"] = json!([
|
||||
{"op": "o_grid", "block": 0, "open": [1, 2], "inset": 0.6},
|
||||
{"op": "o_grid", "block": 14, "open": ["+x"], "inset": [0.5, 0.3, 0.3]},
|
||||
{"op": "o_grid", "block": 22, "open": ["+x"], "inset": [0.5, 0.3, 0.3]},
|
||||
{"op": "extrude", "faces": [[14, "+x"]], "offset": [12, 0, 0]},
|
||||
{"op": "extrude", "faces": [[44, "+x"]], "offset": [4, 0, 0]},
|
||||
{"op": "extrude", "faces": [[22, "+x"]], "offset": [12, 0, 0]},
|
||||
{"op": "extrude", "faces": [[46, "+x"]], "offset": [4, 0, 0]},
|
||||
{"op": "connect", "from": [45, "+z"], "to": [47, "-z"], "layers": 2},
|
||||
{"op": "remove", "blocks": [1, 2, 3, 4, 5]}
|
||||
]);
|
||||
// Smoothing lifts its worst elements just over the default floor (0.21): the squeeze is
|
||||
// reported at any floor an analyst would accept.
|
||||
p["min_quality"] = json!(0.3);
|
||||
let model = s.model.clone();
|
||||
let e = s.execute("mesh.hex.project", &p).unwrap_err().to_string();
|
||||
// Smoothing leaves the worst element in the squeezed ring block (34) or in block 36.
|
||||
assert!(e.contains("smallest scaled Jacobian") && (e.contains("worst in block 34") || e.contains("worst in block 36")), "{e}");
|
||||
assert_eq!(s.model, model);
|
||||
}
|
||||
|
||||
/// The cup without its handle: the floor's O-grid inside the wall's butterfly, cavity carved.
|
||||
/// The wall (4 mm) shares its radial divisions with the floor's longer radial edges; counted
|
||||
/// from the longest, the wall was cut into slivers that sheared at the cavity's rim, and
|
||||
/// smoothing collapsed their edges (edge ratio 172 at size 1, #418).
|
||||
fn cup_floor_double_o_grid_at(size: f64) {
|
||||
let (_d, mut s) = session(json!([
|
||||
{"command": "solid.cylinder", "params": {"radius": 20, "height": 30, "body_name": "Cup"}},
|
||||
{"command": "solid.cylinder", "params": {"base": [0, 0, 4], "radius": 16, "height": 27, "operation": "cut", "targets": ["Cup"]}}
|
||||
]));
|
||||
let mut p = cup_params(size, json!(0.5));
|
||||
p["operations"] = json!([{"op": "o_grid", "block": 0, "open": [1, 2], "inset": 0.6}, {"op": "remove", "blocks": [1, 2, 3, 4, 5]}]);
|
||||
let r = s.execute("mesh.hex.project", &p).unwrap();
|
||||
assert_thin_layers_hold(&mut s, &r);
|
||||
let (v, body) = (r["volume"].as_f64().unwrap(), r["body_volume"].as_f64().unwrap());
|
||||
assert!((v - body).abs() < 0.02 * body, "{r}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cup_floor_double_o_grid() {
|
||||
cup_floor_double_o_grid_at(2.0);
|
||||
cup_floor_double_o_grid_at(1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "heavy: cargo xtask test --heavy (CI heavy job)"]
|
||||
fn cup_floor_double_o_grid_refined() {
|
||||
cup_floor_double_o_grid_at(0.75);
|
||||
}
|
||||
|
||||
/// Thin layers like a sealed enclosure's: a 1.6 mm floor slab and 1 mm walls round a pocket.
|
||||
/// The grid is regular already; smoothing must not even out its graded layers (it took the
|
||||
/// scaled Jacobian from 1 to 0.5 at size 2.5, #418).
|
||||
#[test]
|
||||
fn thin_enclosure_layers() {
|
||||
let (_d, mut s) = session(json!([
|
||||
{"command": "solid.box", "params": {"length": 40, "width": 30, "height": 12, "body_name": "Case"}},
|
||||
{"command": "solid.box", "params": {"length": 38, "width": 28, "height": 20, "corner": [1, 1, 1.6], "operation": "cut", "targets": ["Case"]}}
|
||||
]));
|
||||
for size in [1.0, 2.5] {
|
||||
let r = s
|
||||
.execute(
|
||||
"mesh.hex.project",
|
||||
&json!({"body": "Case", "size": size, "grid": {"planes": {"x": [0, 1, 39, 40], "y": [0, 1, 29, 30], "z": [0, 1.6, 12]}, "carve": [{"i": [1, 1], "j": [1, 1], "k": [1, 1]}]}}),
|
||||
)
|
||||
.unwrap();
|
||||
assert_thin_layers_hold(&mut s, &r);
|
||||
assert!(sj(&r) > 0.99, "{r}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Two discs stacked as two bodies: the lower meshed fine, the upper meshed against it with its
|
||||
/// bottom face fixed (#490) takes the lower's top quads as given — the same divisions though its
|
||||
/// own size is coarser, its nodes there the lower's (joined, not copied), all hex and valid.
|
||||
#[test]
|
||||
fn a_fixed_face_takes_the_neighbours_mesh_as_given() {
|
||||
let (_d, mut s) = session(json!([
|
||||
{"command": "solid.cylinder", "params": {"radius": 10, "height": 2, "body_name": "Low"}},
|
||||
{"command": "solid.cylinder", "params": {"base": [0, 0, 2], "radius": 10, "height": 2, "body_name": "High"}}
|
||||
]));
|
||||
s.execute("mesh.hex.project", &json!({"body": "Low", "template": "butterfly", "size": 0.75})).unwrap();
|
||||
let nodes_before = s.model.nodes().len();
|
||||
let top_quads = |s: &Session, body: &str, z: f64| {
|
||||
let c = s.model.classification();
|
||||
s.model
|
||||
.body_elements(body)
|
||||
.into_iter()
|
||||
.flat_map(|e| c.elements().get(&e).map(|t| t.faces.keys().map(|&f| (e, f)).collect::<Vec<_>>()).unwrap_or_default())
|
||||
.filter(|&(element, face)| {
|
||||
s.model
|
||||
.face_nodes(fieldcraft_model::ElementFace { element, face })
|
||||
.is_some_and(|ns| ns.iter().all(|n| (s.model.node(*n).unwrap().xyz[2] - z).abs() < 1e-9))
|
||||
})
|
||||
.count()
|
||||
};
|
||||
let shared = top_quads(&s, "Low", 2.0);
|
||||
let r = s.execute("mesh.hex.project", &json!({"body": "High", "template": "butterfly", "size": 1.5, "fixed": ["F2:bottom"]})).unwrap();
|
||||
assert!(sj(&r) >= 0.5, "{r}");
|
||||
// Every node of the lower's top joined: the upper adds only its own.
|
||||
let on_face = r["fixed_nodes"].as_u64().unwrap() as usize;
|
||||
assert!(on_face > 0 && r["joined"].as_u64().unwrap() as usize == on_face, "{r}");
|
||||
assert_eq!(s.model.nodes().len(), nodes_before + r["nodes"].as_u64().unwrap() as usize);
|
||||
assert_eq!(top_quads(&s, "High", 4.0), shared, "{r}");
|
||||
let q = s.execute("mesh.quality", &json!({})).unwrap();
|
||||
assert_eq!(q["valid"].as_u64(), Some(s.model.elements().len() as u64), "{q}");
|
||||
// A face no other mesh covers, or not the body's, is an error that changes nothing.
|
||||
let before = s.model.clone();
|
||||
for bad in [json!(["F2:top"]), json!(["nope"])] {
|
||||
assert!(s.execute("mesh.hex.project", &json!({"body": "High", "template": "butterfly", "size": 1.5, "fixed": bad})).is_err(), "{bad}");
|
||||
}
|
||||
assert!(s.model == before);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
//! Node relaxation (#398): `mesh.node.relax` moves the nodes inside a sphere of influence (or
|
||||
//! listed nodes, elements or a set) to fix the worst elements there, nodes on the geometry
|
||||
//! the nodes on listed CAD faces, or listed nodes or elements) to fix the worst elements there, nodes on the geometry
|
||||
//! staying on it ([`fieldcraft_mesh::relax`]), and keeps what it did as a relaxation field of the
|
||||
//! model; `mesh.node.relax.apply` does a field again on the current mesh (after a remesh), and a
|
||||
//! field marked `reapply` is applied by itself after every remesh of its bodies.
|
||||
@@ -17,7 +17,7 @@ use std::collections::{BTreeMap, BTreeSet};
|
||||
use fieldcraft_mesh::relax::{self, Constraints, Falloff, Freedom, RegionQuality, Relaxed};
|
||||
use fieldcraft_mesh::surface::{BoundarySurface, SurfaceOptions};
|
||||
use fieldcraft_mesh::tet::geometry::{AnalyticSurface, Tessellation};
|
||||
use fieldcraft_model::geometry::Entity;
|
||||
use fieldcraft_model::geometry::{Entity, GeometryRef};
|
||||
use fieldcraft_model::{Model, Relaxation};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -28,7 +28,7 @@ use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
pub const RELAX: CommandSpec = CommandSpec {
|
||||
id: "mesh.node.relax",
|
||||
label: "Relax Nodes",
|
||||
params: r#"{"center": [x, y, z], "radius": mm (the sphere of influence) | "around_element": label (a sphere round that element; radius default twice its longest edge) | "nodes": [labels] | "elements": [labels] | "set": "Name", "bodies"?: ["Body1"] (only their nodes), "iterations"?: 5 (1..=100), "strength"?: 1 (0..1], "falloff"?: "smooth" (default: weight 1 at the centre to 0 at the radius) | "linear" | "none", "field"?: "Name" (the relaxation field to keep; default Relax1, Relax2, …), "reapply"?: false (apply the field again after each remesh of these bodies)} — moves the nodes to improve the worst elements around them (smart Laplacian, then optimization of the worst element; a move is kept only when the worst element around the node does not get worse, and never inverts one). Nodes on a B-rep face slide in its UV parameters and stay on it, nodes on a B-rep edge slide along it, nodes on vertices stay; nodes on partition cut faces move freely (shared by both cells); mid-side nodes follow, on the geometry. Reports the nodes moved, the largest displacement, the region's smallest scaled Jacobian, largest edge ratio and worst elements before and after, and the field kept. One undo step"#,
|
||||
params: r#"{"center": [x, y, z], "radius": mm (the sphere of influence) | "around_element": label (a sphere round that element; radius default twice its longest edge) | "faces": [{"body": "Base", "face": "F3"}] (persistent B-rep face names, as model.set.create takes them: every node on those faces and the nodes of the elements touching them; pick faces in the view) | "nodes": [labels] | "elements": [labels] (their nodes), "bodies"?: ["Body1"] (only their nodes; a partitioned body's cells count as it), "iterations"?: 5 (1..=100), "strength"?: 1 (0..1], "falloff"?: "smooth" (default: weight 1 at the centre to 0 at the radius) | "linear" | "none", "field"?: "Name" (the relaxation field to keep; default Relax1, Relax2, …), "reapply"?: false (apply the field again after each remesh of these bodies), "mode"?: "guarded" (default) | "strong", "below"?: 0.7 (strong: the elements below this scaled Jacobian are the bad ones), "reach"?: mm (strong: nodes this close to a bad node follow it; default three times the bad elements' mean edge length)} — moves the nodes to improve the worst elements around them (smart Laplacian, then optimization of the worst element; a move is kept only when the worst element around the node does not get worse, and never inverts one). `"mode": "strong"` morphs the region instead (`bodies` alone is the whole body, nothing listed the whole mesh; `iterations` are rounds, default 20, 1..=200): each round moves the corner nodes of the elements below `below` to better places (their own optimisation, the rest held) and carries that displacement to the nodes within `reach` by harmonic (graph Laplacian) interpolation, zero beyond; nodes on B-rep edges and vertices are held, nodes on faces put back on them, nodes outside the reach unchanged. A round is kept (at full or a halved scale) only when nothing inverts, no element gets longer than the longest was, and the worst element it touches improves; the result never has a lower smallest scaled Jacobian than the start. Its reply adds the mean scaled Jacobian and, per round, the bad elements and nodes, their clusters (bad nodes within the reach of each other, each morphed and checked on its own) and how many were kept, the region's nodes, the reach and the min and mean (`rounds`). Nodes on a B-rep face slide in its UV parameters and stay on it, nodes on a B-rep edge slide along it, nodes on vertices stay; nodes on partition cut faces move freely (shared by both cells); mid-side nodes follow, on the geometry. Reports the nodes moved, the largest displacement, the region's smallest scaled Jacobian, largest edge ratio and worst elements before and after, the field kept, and the listed faces no mesh is tagged with (`faces_without_mesh`; an error when none is). One undo step"#,
|
||||
form: Some(super::forms::NODE_RELAX),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Mesh],
|
||||
@@ -52,6 +52,7 @@ const REMESH: &[&str] = &[
|
||||
"mesh.hex.sweep",
|
||||
"mesh.hex.mapped",
|
||||
"mesh.hex.blocks",
|
||||
"mesh.plan.apply",
|
||||
"mesh.hex.octree",
|
||||
"mesh.cells",
|
||||
"mesh.block",
|
||||
@@ -193,7 +194,7 @@ fn design_geometry(s: &Session, nodes: &BTreeSet<u32>) -> Result<DesignGeometry>
|
||||
None => None,
|
||||
Some(shape) => {
|
||||
let tol = (shape.size()? * 1e-3).max(1e-6);
|
||||
let mesh = shape.tessellate(tol)?;
|
||||
let (mesh, pieces) = shape.tessellate_pieces(tol)?;
|
||||
let surface = BoundarySurface::new(
|
||||
&mesh.positions,
|
||||
&mesh.triangles,
|
||||
@@ -201,7 +202,7 @@ fn design_geometry(s: &Session, nodes: &BTreeSet<u32>) -> Result<DesignGeometry>
|
||||
&SurfaceOptions { weld_distance: mesh.weld_distance, ..SurfaceOptions::default() },
|
||||
)?;
|
||||
let analytic = super::mesh::analytic_surfaces(shape);
|
||||
g.bodies.push(Tessellation::new(&surface, analytic.clone()));
|
||||
g.bodies.push(Tessellation::new(&surface, analytic.clone()).with_pieces(&surface, super::mesh::pieces(shape, &pieces)));
|
||||
Some((g.bodies.len() - 1, analytic))
|
||||
}
|
||||
};
|
||||
@@ -280,8 +281,11 @@ fn design_geometry(s: &Session, nodes: &BTreeSet<u32>) -> Result<DesignGeometry>
|
||||
fn body_nodes(model: &Model, p: &Value) -> Result<Option<(Vec<String>, BTreeSet<u32>)>> {
|
||||
let Some(bodies) = super::opt::<Vec<String>>(p, "bodies")? else { return Ok(None) };
|
||||
let mut nodes = BTreeSet::new();
|
||||
// A body cut into cells by partitions is meshed as its cells (`Base/a`, `Base/b/a`, …).
|
||||
let meshed: BTreeSet<String> = model.classification().entities().iter().map(|r| r.body.clone()).collect();
|
||||
for b in &bodies {
|
||||
let elements = model.body_elements(b);
|
||||
let cell = format!("{b}/");
|
||||
let elements: Vec<u32> = meshed.iter().filter(|m| *m == b || m.starts_with(&cell)).flat_map(|m| model.body_elements(m)).collect();
|
||||
if elements.is_empty() {
|
||||
return Err(EngineError::Params(format!("body `{}` has no mesh", b.chars().take(80).collect::<String>())));
|
||||
}
|
||||
@@ -302,6 +306,78 @@ fn point(p: &Value, key: &str) -> Result<Option<[f64; 3]>> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The most faces `faces` may list.
|
||||
const MAX_FACES: usize = 10_000;
|
||||
|
||||
/// The `faces` parameter: a list of `{body, face}` B-rep face references.
|
||||
fn face_refs(p: &Value) -> Result<Vec<GeometryRef>> {
|
||||
let refs: Vec<GeometryRef> = super::req(p, "faces")?;
|
||||
if refs.is_empty() || refs.len() > MAX_FACES {
|
||||
return Err(EngineError::invalid("faces", format!("1 to {MAX_FACES} faces")));
|
||||
}
|
||||
if let Some(r) = refs.iter().find(|r| !matches!(r.entity, Entity::Face(_))) {
|
||||
return Err(EngineError::invalid("faces", format!("`{}`: give {{\"body\", \"face\"}}", r.body.chars().take(80).collect::<String>())));
|
||||
}
|
||||
Ok(refs)
|
||||
}
|
||||
|
||||
/// The tags of the faces `refs` select, and the faces with no tagged mesh.
|
||||
fn face_tags<'a>(model: &Model, refs: &'a [GeometryRef]) -> (BTreeSet<u32>, Vec<&'a GeometryRef>) {
|
||||
let c = model.classification();
|
||||
let mut tags = BTreeSet::new();
|
||||
let mut missing = Vec::new();
|
||||
for r in refs {
|
||||
let found = c.tags_selected_by(r);
|
||||
if found.is_empty() {
|
||||
missing.push(r);
|
||||
}
|
||||
tags.extend(found);
|
||||
}
|
||||
(tags, missing)
|
||||
}
|
||||
|
||||
/// The listed faces with no tagged mesh, as `{body, face}` (the reply names them).
|
||||
fn unmeshed_faces(model: &Model, p: &Value) -> Vec<Value> {
|
||||
match p.get("faces").map(|_| face_refs(p)) {
|
||||
Some(Ok(refs)) => face_tags(model, &refs).1.into_iter().map(|r| json!(r)).collect(),
|
||||
_ => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The nodes on the B-rep faces `refs` (from the mesh's geometry tags: element faces on them and
|
||||
/// shell elements meshing them) and the nodes of every element touching those. Faces with no
|
||||
/// tagged mesh are left out (the reply names them); none with one is an error.
|
||||
fn face_nodes(model: &Model, refs: &[GeometryRef]) -> Result<BTreeSet<u32>> {
|
||||
let c = model.classification();
|
||||
let (tags, missing) = face_tags(model, refs);
|
||||
if tags.is_empty() {
|
||||
let names: Vec<String> =
|
||||
missing.iter().take(5).map(|r| format!("{}/{}", r.body, r.entity.name().unwrap_or_default()).chars().take(120).collect()).collect();
|
||||
return Err(EngineError::Params(format!("no mesh is tagged with the faces ({}): mesh their bodies, or mesh.tag", names.join(", "))));
|
||||
}
|
||||
let mut on = BTreeSet::new();
|
||||
for (&label, t) in c.elements() {
|
||||
let Some(e) = model.element(label) else { continue };
|
||||
if e.kind.dimension() == 2 && t.region.is_some_and(|r| tags.contains(&r)) {
|
||||
on.extend(e.nodes.iter().copied());
|
||||
}
|
||||
for (&face, tag) in &t.faces {
|
||||
if !tags.contains(tag) {
|
||||
continue;
|
||||
}
|
||||
let Some(local) = e.kind.faces().get(usize::from(face).wrapping_sub(1)) else { continue };
|
||||
on.extend(local.iter().filter_map(|&i| e.nodes.get(i).copied()));
|
||||
}
|
||||
}
|
||||
let mut out = on.clone();
|
||||
for e in model.elements().values() {
|
||||
if e.nodes.iter().any(|n| on.contains(n)) {
|
||||
out.extend(e.nodes.iter().copied());
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The sphere around element `label`: its centroid, and twice its longest corner edge.
|
||||
fn around_element(model: &Model, label: u32) -> Result<([f64; 3], f64)> {
|
||||
let e = model.element(label).ok_or_else(|| EngineError::Params(format!("no element {label}")))?;
|
||||
@@ -343,11 +419,11 @@ pub(crate) fn weights(model: &Model, p: &Value) -> Result<(BTreeMap<u32, f64>, O
|
||||
.map(Some)?,
|
||||
None => around.map(|a| a.1),
|
||||
};
|
||||
let listed = ["nodes", "elements", "set"].iter().filter(|k| p.get(**k).is_some()).count();
|
||||
let listed = ["faces", "nodes", "elements"].iter().filter(|k| p.get(**k).is_some()).count();
|
||||
if let (Some(c), Some(r)) = (center, radius) {
|
||||
if listed > 0 {
|
||||
return Err(EngineError::Params(
|
||||
"give a sphere (`center` and `radius`, or `around_element`) or `nodes`, `elements` or `set`, not both".into(),
|
||||
"give a sphere (`center` and `radius`, or `around_element`) or `faces`, `nodes` or `elements`, not both".into(),
|
||||
));
|
||||
}
|
||||
return Ok((relax::sphere_weights(model, c, r, strength, falloff, only.as_ref())?, Some((c, r))));
|
||||
@@ -363,11 +439,8 @@ pub(crate) fn weights(model: &Model, p: &Value) -> Result<(BTreeMap<u32, f64>, O
|
||||
if p.get("elements").is_some() {
|
||||
elements.extend(super::labels(p, "elements", fieldcraft_model::MAX_ELEMENTS)?);
|
||||
}
|
||||
if let Some(name) = super::opt::<String>(p, "set")? {
|
||||
let set = model.resolve_set(&name)?;
|
||||
nodes.extend(set.nodes.iter().copied());
|
||||
elements.extend(set.elements.iter().copied());
|
||||
elements.extend(set.faces.iter().map(|f| f.element));
|
||||
if p.get("faces").is_some() {
|
||||
nodes.extend(face_nodes(model, &face_refs(p)?)?);
|
||||
}
|
||||
for e in elements {
|
||||
let el = model.element(e).ok_or_else(|| EngineError::Params(format!("no element {e}")))?;
|
||||
@@ -375,7 +448,7 @@ pub(crate) fn weights(model: &Model, p: &Value) -> Result<(BTreeMap<u32, f64>, O
|
||||
}
|
||||
if listed == 0 {
|
||||
return Err(EngineError::Params(
|
||||
"say which nodes to relax: give one of `center` and `radius`, `around_element`, `nodes`, `elements` or `set`".into(),
|
||||
"say which nodes to relax: give one of `center` and `radius`, `around_element`, `faces`, `nodes` or `elements`".into(),
|
||||
));
|
||||
}
|
||||
if let Some(only) = &only {
|
||||
@@ -413,6 +486,10 @@ pub struct Preview {
|
||||
}
|
||||
|
||||
pub fn preview(s: &Session, params: &Value) -> Option<Preview> {
|
||||
// The strong mode morphs a whole region: too heavy for a live preview.
|
||||
if params.get("mode").and_then(Value::as_str) == Some("strong") {
|
||||
return None;
|
||||
}
|
||||
let (w, sphere) = weights(&s.model, params).ok()?;
|
||||
let g = design_geometry(s, &touched(&s.model, &w)).ok()?;
|
||||
let relaxed = relax::relax(&s.model, &w, iterations(params).ok()?, &g).ok()?;
|
||||
@@ -427,7 +504,114 @@ pub fn sphere(s: &Session, params: &Value) -> Option<([f64; 3], f64)> {
|
||||
Some((c, r))
|
||||
}
|
||||
|
||||
/// Default rounds of the strong mode.
|
||||
const STRONG_ROUNDS: usize = 20;
|
||||
|
||||
/// `mesh.node.relax` with `"mode": "strong"`: morph the region round its bad elements
|
||||
/// ([`relax::morph`]).
|
||||
fn relax_strong(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let only = body_nodes(&s.model, p)?;
|
||||
let below = super::optional_number(p, "below", relax::MorphOptions::default().below)?;
|
||||
if !(below > 0.0 && below <= 1.0) {
|
||||
return Err(EngineError::param("below", ProblemKind::NotInRange { min: 0.0, max: 1.0 }));
|
||||
}
|
||||
let reach = match p.get("reach") {
|
||||
None => None,
|
||||
Some(_) => Some(super::optional_number(p, "reach", 0.0)?)
|
||||
.filter(|r| *r > 0.0 && r.is_finite())
|
||||
.ok_or_else(|| EngineError::param("reach", ProblemKind::NotPositive))
|
||||
.map(Some)?,
|
||||
};
|
||||
let rounds = match p.get("iterations") {
|
||||
None => STRONG_ROUNDS,
|
||||
Some(v) => v
|
||||
.as_u64()
|
||||
.and_then(|i| usize::try_from(i).ok())
|
||||
.filter(|i| (1..=relax::MAX_ROUNDS).contains(i))
|
||||
.ok_or_else(|| EngineError::param("iterations", ProblemKind::NotInRange { min: 1.0, max: relax::MAX_ROUNDS as f64 }))?,
|
||||
};
|
||||
let listed = ["center", "radius", "around_element", "faces", "nodes", "elements"].iter().any(|k| p.get(*k).is_some());
|
||||
let nodes: BTreeSet<u32> = if listed {
|
||||
weights(&s.model, p)?.0.into_keys().collect()
|
||||
} else if let Some((_, n)) = &only {
|
||||
n.clone()
|
||||
} else {
|
||||
s.model.nodes().keys().copied().collect()
|
||||
};
|
||||
let nodes: BTreeSet<u32> = relax::uniform_weights(&s.model, &nodes, 1.0)?.into_keys().collect();
|
||||
if nodes.is_empty() {
|
||||
return Err(EngineError::Params("no mesh nodes in the region to relax".into()));
|
||||
}
|
||||
let g = design_geometry(s, &touched(&s.model, &nodes.iter().map(|&n| (n, 1.0)).collect()))?;
|
||||
let r = relax::morph(&s.model, &nodes, relax::MorphOptions { below, reach, rounds }, &g)?;
|
||||
let field_name = match super::opt::<String>(p, "field")? {
|
||||
Some(n) => n,
|
||||
None => (1..).map(|i| format!("Relax{i}")).find(|n| s.model.relaxation(n).is_none()).unwrap_or_else(|| "Relax".into()),
|
||||
};
|
||||
let bodies = only.map(|(b, _)| b).unwrap_or_default();
|
||||
let reapply = super::opt::<bool>(p, "reapply")?.unwrap_or(false);
|
||||
if !r.positions.is_empty() {
|
||||
let cells = relax::field_cells(&s.model, &Relaxed { positions: r.positions.clone(), ..Relaxed::default() });
|
||||
s.model.set_relaxation(&field_name, Relaxation { bodies, cells, reapply })?;
|
||||
}
|
||||
for (n, x) in &r.positions {
|
||||
s.model.set_node(*n, *x)?;
|
||||
}
|
||||
let summary = format!(
|
||||
"strong: moved {} node{} (largest {:.3e}) in {} round{}; scaled Jacobian min {:.3} → {:.3}, mean {:.3} → {:.3}{}",
|
||||
r.moved,
|
||||
if r.moved == 1 { "" } else { "s" },
|
||||
r.max_displacement,
|
||||
r.rounds.len(),
|
||||
if r.rounds.len() == 1 { "" } else { "s" },
|
||||
r.before.min_quality,
|
||||
r.after.min_quality,
|
||||
r.mean_before,
|
||||
r.mean_after,
|
||||
if r.positions.is_empty() {
|
||||
format!("; nothing below {below} could be improved, nothing moved")
|
||||
} else {
|
||||
format!("; kept as field {field_name}")
|
||||
}
|
||||
);
|
||||
let rounds: Vec<Value> = r
|
||||
.rounds
|
||||
.iter()
|
||||
.map(|k| {
|
||||
json!({"bad_elements": k.bad_elements, "bad_nodes": k.bad_nodes, "region_nodes": k.region_nodes, "reach": k.reach,
|
||||
"clusters": k.clusters, "kept": k.kept, "min": k.min_quality, "mean": k.mean_quality})
|
||||
})
|
||||
.collect();
|
||||
let mut before = quality_json(&r.before);
|
||||
let mut after = quality_json(&r.after);
|
||||
if let (Some(b), Some(a)) = (before.as_object_mut(), after.as_object_mut()) {
|
||||
b.insert("mean_scaled_jacobian".into(), json!(r.mean_before));
|
||||
a.insert("mean_scaled_jacobian".into(), json!(r.mean_after));
|
||||
}
|
||||
Ok(json!({
|
||||
"mode": "strong",
|
||||
"moved": r.moved,
|
||||
"nodes_changed": r.positions.len(),
|
||||
"max_displacement": r.max_displacement,
|
||||
"iterations": r.rounds.len(),
|
||||
"rounds": rounds,
|
||||
"before": before,
|
||||
"after": after,
|
||||
"field": (!r.positions.is_empty()).then_some(field_name),
|
||||
"faces_without_mesh": unmeshed_faces(&s.model, p),
|
||||
"summary": summary,
|
||||
}))
|
||||
}
|
||||
|
||||
fn relax_nodes(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
match super::opt::<String>(p, "mode")?.as_deref() {
|
||||
None | Some("guarded") => {}
|
||||
Some("strong") => return relax_strong(s, p),
|
||||
Some(_) => return Err(EngineError::param("mode", ProblemKind::NotAChoice)),
|
||||
}
|
||||
if p.get("below").is_some() || p.get("reach").is_some() {
|
||||
return Err(EngineError::Params("`below` and `reach` go with \"mode\": \"strong\"".into()));
|
||||
}
|
||||
let (w, sphere) = weights(&s.model, p)?;
|
||||
if w.is_empty() {
|
||||
return Err(EngineError::Params("no mesh nodes in the region to relax".into()));
|
||||
@@ -469,6 +653,7 @@ fn relax_nodes(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
"before": quality_json(&r.before),
|
||||
"after": quality_json(&r.after),
|
||||
"field": (!r.positions.is_empty()).then_some(field_name),
|
||||
"faces_without_mesh": unmeshed_faces(&s.model, p),
|
||||
"summary": summary,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -192,3 +192,202 @@ fn hostile_parameters_never_panic() {
|
||||
assert!(s.execute("mesh.node.relax.apply", &json!({"field": "nope"})).is_err());
|
||||
assert!(s.execute("mesh.node.relax.apply", &json!({})).is_err());
|
||||
}
|
||||
|
||||
/// A 20 mm box with an R2 fillet along its edge at x = z = 20 (axis x = z = 18, along y), tet
|
||||
/// meshed; its fillet faces' persistent names.
|
||||
fn filleted_box() -> (crate::tests::TempDir, Session, Vec<String>) {
|
||||
let script = json!({"commands": [
|
||||
{"command": "solid.box", "params": {"length": 20, "width": 20, "height": 20, "body_name": "B"}},
|
||||
{"command": "solid.fillet", "params": {"edges": [[20, 10, 20]], "radius": 2}},
|
||||
]});
|
||||
let bytes = fieldcraft_geometry::script_design(&script.to_string()).unwrap();
|
||||
let (dir, path) = crate::tests::design_file(&bytes);
|
||||
let mut s = Session::default();
|
||||
s.execute("geometry.open", &json!({"path": path})).unwrap();
|
||||
let features = s.execute("geometry.features", &json!({"body": "B"})).unwrap();
|
||||
let fillet = features["bodies"][0]["features"].as_array().unwrap().iter().find(|f| f["type"] == "fillet").unwrap().clone();
|
||||
let faces: Vec<String> = fillet["faces"].as_array().unwrap().iter().map(|f| f.as_str().unwrap().to_string()).collect();
|
||||
assert!(!faces.is_empty(), "{fillet}");
|
||||
s.execute("mesh.tet.generate", &json!({"size": 1.5})).unwrap();
|
||||
(dir, s, faces)
|
||||
}
|
||||
|
||||
fn fillet_radius(p: [f64; 3]) -> f64 {
|
||||
((p[0] - 18.0).powi(2) + (p[2] - 18.0).powi(2)).sqrt()
|
||||
}
|
||||
|
||||
/// #471: relaxing by CAD face. Relaxing the fillet faces of the tet mesh raises the worst
|
||||
/// element there, inverts none, keeps the fillet's nodes on its cylinder
|
||||
/// and moves nothing away from the fillet's elements. Faces that are not meshed are refused.
|
||||
#[test]
|
||||
fn relaxing_fillet_faces_improves_them() {
|
||||
let (_d, mut s, faces) = filleted_box();
|
||||
let perturbed = s.model.clone();
|
||||
let refs: Vec<Value> = faces.iter().map(|f| json!({"body": "B", "face": f})).collect();
|
||||
let r = s.execute("mesh.node.relax", &json!({"faces": refs, "iterations": 10})).unwrap();
|
||||
let (before, after) = (r["before"]["min_scaled_jacobian"].as_f64().unwrap(), r["after"]["min_scaled_jacobian"].as_f64().unwrap());
|
||||
assert!(after > before, "{r}");
|
||||
assert_eq!(r["after"]["invalid"], 0, "{r}");
|
||||
assert_eq!(invalid(&mut s), 0);
|
||||
for (n, p) in s.model.nodes() {
|
||||
let old = perturbed.node(*n).unwrap().xyz;
|
||||
if old[0] > 18.0 && old[2] > 18.0 && (fillet_radius(old) - 2.0).abs() < 1e-6 {
|
||||
assert!((fillet_radius(p.xyz) - 2.0).abs() < 1e-6, "node {n} left the fillet");
|
||||
}
|
||||
if old != p.xyz {
|
||||
assert!(fillet_radius(old) < 6.0, "node {n} far from the fillet moved");
|
||||
}
|
||||
}
|
||||
// Faces: required objects, known and meshed; not with a sphere.
|
||||
for bad in [
|
||||
json!({"faces": []}),
|
||||
json!({"faces": "F1"}),
|
||||
json!({"faces": [{"body": "B", "face": "Nope"}]}),
|
||||
json!({"faces": [{"body": "B", "face": "Nope"}, {"body": "B", "face": "Nope2"}]}),
|
||||
json!({"faces": [{"body": "Nope", "face": faces[0]}]}),
|
||||
json!({"faces": [{"body": "B"}]}),
|
||||
json!({"faces": refs, "center": [0, 0, 0], "radius": 1}),
|
||||
] {
|
||||
assert!(s.execute("mesh.node.relax", &bad).is_err(), "{bad}");
|
||||
}
|
||||
// A listed face with no mesh is named, the others relaxed.
|
||||
let mut some = refs.clone();
|
||||
some.push(json!({"body": "B", "face": "Nope"}));
|
||||
let r = s.execute("mesh.node.relax", &json!({"faces": some})).unwrap();
|
||||
assert_eq!(r["faces_without_mesh"], json!([{"body": "B", "face": "Nope"}]), "{r}");
|
||||
// Elements by label are accepted too.
|
||||
let e = *s.model.elements().keys().next().unwrap();
|
||||
s.execute("mesh.node.relax", &json!({"elements": [e]})).unwrap();
|
||||
assert!(s.execute("mesh.node.relax", &json!({"set": "All"})).is_err());
|
||||
}
|
||||
|
||||
/// Whether a node of the 20 mm box lies on two of its planes (an edge or a vertex of the box,
|
||||
/// or a tangent line of its fillet).
|
||||
fn on_box_edge(p: [f64; 3]) -> bool {
|
||||
p.iter().filter(|c| c.abs() < 1e-6 || (**c - 20.0).abs() < 1e-6).count() >= 2
|
||||
}
|
||||
|
||||
/// Strong mode on the whole filleted box: the smallest scaled Jacobian does not drop, the mean rises,
|
||||
/// nothing inverts, the fillet's nodes stay on its cylinder, the plane faces' nodes on their
|
||||
/// planes, and the nodes on the box's edges and the fillet's tangent lines do not move. The
|
||||
/// reply carries each round's region and quality.
|
||||
#[test]
|
||||
fn strong_relaxation_of_a_filleted_box_keeps_faces_and_edges() {
|
||||
let (_d, mut s, _) = filleted_box();
|
||||
let before = s.model.clone();
|
||||
let r = s.execute("mesh.node.relax", &json!({"mode": "strong", "bodies": ["B"], "below": 0.3})).unwrap();
|
||||
let (b, a) = (&r["before"], &r["after"]);
|
||||
// The worst tets may not improve: their corners can all be held on the box's edges.
|
||||
assert!(a["min_scaled_jacobian"].as_f64().unwrap() >= b["min_scaled_jacobian"].as_f64().unwrap(), "{r}");
|
||||
assert!(a["mean_scaled_jacobian"].as_f64().unwrap() > b["mean_scaled_jacobian"].as_f64().unwrap(), "{r}");
|
||||
assert_eq!(a["invalid"], 0, "{r}");
|
||||
assert_eq!(invalid(&mut s), 0);
|
||||
let rounds = r["rounds"].as_array().unwrap();
|
||||
assert!(!rounds.is_empty() && rounds.iter().all(|k| k["region_nodes"].as_u64() >= k["bad_nodes"].as_u64() && k["min"].is_number()), "{r}");
|
||||
for (n, p) in s.model.nodes() {
|
||||
let (old, new) = (before.node(*n).unwrap().xyz, p.xyz);
|
||||
if on_box_edge(old) {
|
||||
assert_eq!(old, new, "node {n} on an edge moved");
|
||||
}
|
||||
if old[0] > 18.0 && old[2] > 18.0 && (fillet_radius(old) - 2.0).abs() < 1e-6 {
|
||||
assert!((fillet_radius(new) - 2.0).abs() < 1e-6, "node {n} left the fillet");
|
||||
}
|
||||
for k in 0..3 {
|
||||
for plane in [0.0, 20.0] {
|
||||
if (old[k] - plane).abs() < 1e-9 {
|
||||
assert!((new[k] - plane).abs() < 1e-6, "node {n} left the plane {k} = {plane}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Strong mode on a distorted hex plate: over the guarded mode's sphere it lifts the smallest
|
||||
/// scaled Jacobian past the threshold, close to the guarded mode (which also slides the edge nodes) (with nodes on edges held, where the guarded mode slides
|
||||
/// them); by quality threshold (the elements below 0.5, morphed 4 mm round) it untangles
|
||||
/// every element. Nothing inverts, nodes on the hole stay on its cylinder and the caps' on their
|
||||
/// planes, nodes on the hole's edges do not move.
|
||||
#[test]
|
||||
fn strong_relaxation_untangles_a_distorted_plate() {
|
||||
let mut s = plate();
|
||||
s.execute("mesh.hex.generate", &json!({"size": 2.0})).unwrap();
|
||||
perturb(&mut s, ON_HOLE, 6.0, 0.35);
|
||||
let perturbed = s.model.clone();
|
||||
let sphere = json!({"center": ON_HOLE, "radius": 7.0, "iterations": 20});
|
||||
let guarded = s.execute("mesh.node.relax", &sphere).unwrap();
|
||||
let min = |r: &Value, at: &str| r[at]["min_scaled_jacobian"].as_f64().unwrap();
|
||||
for strong in [json!({"mode": "strong", "center": ON_HOLE, "radius": 7.0}), json!({"mode": "strong", "below": 0.5, "reach": 4.0})] {
|
||||
s.model = perturbed.clone();
|
||||
let r = s.execute("mesh.node.relax", &strong).unwrap();
|
||||
assert!(min(&r, "after") > strong["below"].as_f64().unwrap_or(0.7) && min(&r, "before") < 0.0, "{strong}: {r}");
|
||||
// Close to the guarded mode, which also slides the hole's edge nodes.
|
||||
assert!(strong.get("below").is_some() || min(&r, "after") > min(&guarded, "after") - 0.05, "strong {r}\nguarded {guarded}");
|
||||
assert_eq!(r["after"]["invalid"], 0, "{r}");
|
||||
assert_eq!(invalid(&mut s), 0);
|
||||
check_on_geometry(&perturbed, &s.model);
|
||||
for (n, p) in s.model.nodes() {
|
||||
let old = perturbed.node(*n).unwrap().xyz;
|
||||
if on_faces(old).len() >= 2 {
|
||||
assert_eq!(old, p.xyz, "node {n} on the hole's edge moved");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strong_mode_parameters_are_checked() {
|
||||
let mut s = plate();
|
||||
s.execute("mesh.hex.generate", &json!({"size": 4.0})).unwrap();
|
||||
for p in [
|
||||
json!({"mode": "wild", "nodes": [1]}),
|
||||
json!({"mode": "strong", "below": 0}),
|
||||
json!({"mode": "strong", "below": 1.5}),
|
||||
json!({"mode": "strong", "below": 0.5, "reach": 0}),
|
||||
json!({"mode": "strong", "below": 0.5, "reach": -1}),
|
||||
json!({"mode": "strong", "bodies": ["nope"]}),
|
||||
json!({"mode": "strong", "nodes": [1], "iterations": 0}),
|
||||
json!({"mode": "strong", "nodes": [1], "iterations": 201}),
|
||||
json!({"below": 0.5}),
|
||||
json!({"nodes": [1], "reach": 1}),
|
||||
json!({"nodes": [1], "mode": "strong", "layers": 1}),
|
||||
] {
|
||||
assert!(s.execute("mesh.node.relax", &p).is_err(), "{p}");
|
||||
}
|
||||
// Nothing below the threshold: nothing moves.
|
||||
let r = s.execute("mesh.node.relax", &json!({"mode": "strong", "below": 0.01})).unwrap();
|
||||
assert_eq!(r["moved"], 0, "{r}");
|
||||
}
|
||||
|
||||
/// The mesh and the strong relaxation are the same bits on every platform and thread count:
|
||||
/// the plate's hex mesh differed between macOS and Linux (the system maths library's `atan2`,
|
||||
/// `sin` … are not correctly rounded), so the distorted-plate test passed on one and failed on
|
||||
/// the other (#438). The fingerprint is of the nodes after mesh.hex.generate, after the
|
||||
/// perturbation and after the strong relaxation; CI checks it on Linux, it was taken on macOS.
|
||||
#[test]
|
||||
fn mesh_and_strong_relaxation_are_the_same_bits_everywhere() {
|
||||
let fingerprint = |s: &Session, h: &mut u64| {
|
||||
// FNV-1a over every node's label and coordinate bits.
|
||||
for (n, p) in s.model.nodes() {
|
||||
for word in [u64::from(*n), p.xyz[0].to_bits(), p.xyz[1].to_bits(), p.xyz[2].to_bits()] {
|
||||
for b in word.to_le_bytes() {
|
||||
*h = (*h ^ u64::from(b)).wrapping_mul(0x0000_0100_0000_01b3);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
let run = || {
|
||||
let mut h = 0xcbf2_9ce4_8422_2325;
|
||||
let mut s = plate();
|
||||
s.execute("mesh.hex.generate", &json!({"size": 2.0})).unwrap();
|
||||
fingerprint(&s, &mut h);
|
||||
perturb(&mut s, ON_HOLE, 6.0, 0.35);
|
||||
fingerprint(&s, &mut h);
|
||||
s.execute("mesh.node.relax", &json!({"mode": "strong", "below": 0.5, "reach": 4.0})).unwrap();
|
||||
fingerprint(&s, &mut h);
|
||||
h
|
||||
};
|
||||
let one = fieldcraft_parallel::with_threads(1, run);
|
||||
let many = fieldcraft_parallel::with_threads(4, run);
|
||||
assert_eq!(one, many, "1 and 4 threads differ");
|
||||
assert_eq!(one, 0x7445_e04c_0d65_c1d0, "fingerprint {one:#018x}");
|
||||
}
|
||||
@@ -46,6 +46,17 @@ pub(super) fn clear<'a>(model: &mut Model, bodies: impl IntoIterator<Item = &'a
|
||||
}
|
||||
|
||||
impl Cleared {
|
||||
/// Add what `other` removed (a body in both counts once, as `other` reports it).
|
||||
pub(super) fn extend(&mut self, other: Cleared) {
|
||||
self.bodies.retain(|(b, _, _)| !other.bodies.iter().any(|(o, _, _)| o == b));
|
||||
self.bodies.extend(other.bodies);
|
||||
self.removed.elements.extend(other.removed.elements);
|
||||
self.removed.nodes.extend(other.removed.nodes);
|
||||
self.stale_sets.extend(other.stale_sets);
|
||||
self.stale_sets.sort();
|
||||
self.stale_sets.dedup();
|
||||
}
|
||||
|
||||
/// [`Cleared::report`] when anything was removed; `reply` as it is otherwise.
|
||||
pub(super) fn report_if_any(self, s: &Session, reply: Value) -> Value {
|
||||
if self.bodies.is_empty() { reply } else { self.report(s, reply) }
|
||||
@@ -128,8 +139,11 @@ pub static MESHERS: &[(&str, Targets)] = &[
|
||||
("mesh.hex.mapped", body_or_all),
|
||||
("mesh.hex.octree", body_or_all),
|
||||
("mesh.hex.blocks", listed_or_all),
|
||||
("mesh.hex.project", body_or_all),
|
||||
("mesh.cells", listed_or_all),
|
||||
("mesh.plan.apply", super::plan::plan_bodies),
|
||||
("mesh.planar", super::mesh::planar_body),
|
||||
("mesh.hex.extrude", super::extrude::extrude_body),
|
||||
("mesh.mirror", super::mirror::image_names),
|
||||
];
|
||||
|
||||
@@ -171,7 +185,8 @@ fn made_kind(command: &str, params: &Value) -> Option<ElementKind> {
|
||||
"mesh.tet.generate" => {
|
||||
Some(if params.get("order").and_then(Value::as_str) == Some("quadratic") { ElementKind::Tet10 } else { ElementKind::Tet4 })
|
||||
}
|
||||
"mesh.hex.generate" | "mesh.hex.sweep" | "mesh.hex.mapped" | "mesh.hex.blocks" => {
|
||||
"mesh.hex.project" => Some(ElementKind::Hex8),
|
||||
"mesh.hex.generate" | "mesh.hex.sweep" | "mesh.hex.mapped" | "mesh.hex.blocks" | "mesh.plan.apply" | "mesh.hex.extrude" => {
|
||||
Some(if params.get("order").and_then(Value::as_i64) == Some(2) { ElementKind::Hex20 } else { ElementKind::Hex8 })
|
||||
}
|
||||
_ => None,
|
||||
@@ -302,6 +317,16 @@ mod tests {
|
||||
let tet_other = json!({"command": "mesh.tet.generate", "params": {"body": "Box.2", "size": 5}});
|
||||
let hex_box = json!({"command": "mesh.hex.generate", "params": {"body": "Box.1", "size": 2.5}});
|
||||
let square = json!([[0, 0], [10, 0], [10, 10], [0, 10]]);
|
||||
// Box.1's bottom and top faces, for the extrusion between them.
|
||||
let (_probe_dir, probe) = two_boxes();
|
||||
let face = |nz: f64| {
|
||||
let b = probe.design.as_deref().and_then(|d| d.body("Box.1")).unwrap();
|
||||
let surfaces = b.face_surfaces().unwrap();
|
||||
let i =
|
||||
surfaces.iter().position(|f| matches!(f, Some(fieldcraft_geometry::FaceSurface::Plane { normal, .. }) if normal[2] == nz)).unwrap();
|
||||
json!({"body": "Box.1", "face": b.faces()[i].name})
|
||||
};
|
||||
let extrude = json!({"source": [face(-1.0)], "to_surface": [face(1.0)], "direction": [0, 0, 1], "layers": 4, "size": 2.5});
|
||||
let cases: Vec<(&str, Vec<Value>, Value, &str, &str)> = vec![
|
||||
("mesh.tet.generate", vec![tet_other.clone()], json!({"body": "Box.1", "size": 4}), "Box.1", "Box.2"),
|
||||
("mesh.hex.generate", vec![tet_other.clone()], json!({"body": "Box.1", "size": 2.5}), "Box.1", "Box.2"),
|
||||
@@ -309,7 +334,16 @@ mod tests {
|
||||
("mesh.hex.mapped", vec![tet_other.clone()], json!({"body": "Box.1", "size": 2.5}), "Box.1", "Box.2"),
|
||||
("mesh.hex.octree", vec![tet_other.clone()], json!({"body": "Box.1", "size": 2.5}), "Box.1", "Box.2"),
|
||||
("mesh.hex.blocks", vec![tet_other.clone()], json!({"bodies": ["Box.1"], "size": 2.5}), "Box.1", "Box.2"),
|
||||
("mesh.hex.project", vec![tet_other.clone()], json!({"body": "Box.1", "size": 2.5, "merge": 0}), "Box.1", "Box.2"),
|
||||
("mesh.cells", vec![tet_other.clone()], json!({"bodies": ["Box.1"], "size": 2.5, "default": "hex"}), "Box.1", "Box.2"),
|
||||
(
|
||||
"mesh.plan.apply",
|
||||
vec![tet_other.clone(), json!({"command": "mesh.plan", "params": {"body": "Box.1", "size": 2.5}})],
|
||||
json!({"plan": "Plan-Box_1"}),
|
||||
"Box.1",
|
||||
"Box.2",
|
||||
),
|
||||
("mesh.hex.extrude", vec![tet_other.clone()], extrude, "Box.1", "Box.2"),
|
||||
("mesh.planar", vec![tet_other.clone()], json!({"polygon": square, "size": 2.5, "name": "Box.1"}), "Box.1", "Box.2"),
|
||||
// The image's tets (meshed before) are replaced by the mirrored hexahedra.
|
||||
(
|
||||
|
||||
@@ -70,6 +70,16 @@ pub const REACTIONS: CommandSpec = CommandSpec {
|
||||
run: reactions,
|
||||
};
|
||||
|
||||
pub const EXPORT_VTU: CommandSpec = CommandSpec {
|
||||
id: "results.export.vtu",
|
||||
label: "Export VTU",
|
||||
params: r#"{"step": "Load", "path": "out.vtu" | "out.pvd", "mode": 1, "frames": "all|last", "encoding": "ascii|binary"} — writes a solved step as VTK XML unstructured grids for ParaView: the mesh (points in node label order, cells in element label order, arrays `node_label` and `element_label`), the step's nodal fields as point data and per-element fields as cell data. Static: displacement, rotation (with shells or beams), reaction, stress (6 components in VTK order XX, YY, ZZ, XY, YZ, XZ), von_mises, temperature (coupled steps); nonlinear static: displacement per frame, the final fields on the last; frequency: mode_shape, with mode, frequency and eigenvalue as field data; explicit: displacement and velocity per frame, cell stress, plastic_strain, damage, eroded; heat: temperature per frame, heat_flux on the last. Point array `has_result` is 0 at nodes the step has no values for (written as 0, as are values that are not finite). A `.vtu` path writes one state (the last frame, or `mode`, default 1); a `.pvd` path writes every frame or mode (`frames: "last"` only the last) as `out_0000.vtu`, … beside it and the collection listing each with its time (load factor, mode number). `encoding` ascii (default) or binary (inline base64). Fluid steps are not supported yet. Replies {files, bytes, points, cells}"#,
|
||||
form: Some(super::forms::EXPORT_VTU),
|
||||
kind: CommandKind::Session,
|
||||
requires: &[Requirement::Result],
|
||||
run: export_vtu,
|
||||
};
|
||||
|
||||
pub const EXPORT_CSV: CommandSpec = CommandSpec {
|
||||
id: "results.export.csv",
|
||||
label: "Export Results (CSV)",
|
||||
@@ -102,7 +112,7 @@ fn solved_list(s: &Session) -> String {
|
||||
|
||||
/// The steps a command works on.
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum Want {
|
||||
pub(super) enum Want {
|
||||
Any,
|
||||
Frequency,
|
||||
/// Steps with frames: explicit dynamics and nonlinear static (one per converged increment).
|
||||
@@ -128,7 +138,7 @@ impl Want {
|
||||
|
||||
/// The step a command works on: `step`, else the step shown, else the first solved one
|
||||
/// (current results first) of the kind it wants.
|
||||
fn step_for(s: &Session, p: &Value, want: Want) -> Result<String> {
|
||||
pub(super) fn step_for(s: &Session, p: &Value, want: Want) -> Result<String> {
|
||||
if let Some(name) = opt::<String>(p, "step")? {
|
||||
if s.latest_result(&name).is_none() {
|
||||
return Err(EngineError::Params(format!("step `{name}` has no result; {}", solved_list(s))));
|
||||
@@ -379,6 +389,251 @@ fn reactions(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({"step": step, "stale": !current, "about": about, "sets": per_set, "total": total, "applied": r.applied_total}))
|
||||
}
|
||||
|
||||
/// Most files one `results.export.vtu` writes.
|
||||
pub const MAX_VTU_FILES: usize = 10_000;
|
||||
/// Most bytes one `results.export.vtu` writes, over all its files.
|
||||
pub const MAX_VTU_BYTES: u64 = 8 << 30;
|
||||
|
||||
/// One state of a result written as a `.vtu`: the end of a linear step, a mode (from 1) or a
|
||||
/// frame (from 0).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum State {
|
||||
Final,
|
||||
Mode(usize),
|
||||
Frame(usize),
|
||||
}
|
||||
|
||||
fn export_vtu(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
use fieldcraft_io::vtk::{self, Encoding};
|
||||
let step = step_for(s, p, Want::Any)?;
|
||||
let mode: Option<usize> = opt(p, "mode")?;
|
||||
let last_only = match opt::<String>(p, "frames")?.as_deref() {
|
||||
None | Some("all") => false,
|
||||
Some("last") => true,
|
||||
Some(_) => return Err(EngineError::invalid("frames", "must be \"all\" or \"last\"")),
|
||||
};
|
||||
let encoding = match opt::<String>(p, "encoding")?.as_deref() {
|
||||
None | Some("ascii") => Encoding::Ascii,
|
||||
Some("binary") => Encoding::Base64,
|
||||
Some(_) => return Err(EngineError::invalid("encoding", "must be \"ascii\" or \"binary\"")),
|
||||
};
|
||||
let path = super::file_param(s, p, "path", fieldcraft_io::roots::Access::Write)?;
|
||||
let target = std::path::Path::new(&path);
|
||||
let series = match target.extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).as_deref() {
|
||||
Some("vtu") => false,
|
||||
Some("pvd") => true,
|
||||
_ => return Err(EngineError::Params("`path` must end in .vtu (one file) or .pvd (a series of .vtu files)".into())),
|
||||
};
|
||||
let r = match s.latest_result(&step) {
|
||||
Some((_, false)) => return Err(EngineError::Params(format!("the model changed since step `{step}` was solved; solve it again"))),
|
||||
Some((r, true)) => std::sync::Arc::clone(r),
|
||||
None => return Err(EngineError::Params(format!("step `{step}` has no result; solve it first"))),
|
||||
};
|
||||
let frames = |n: usize, what: &str| -> Result<Vec<State>> {
|
||||
let last = n.checked_sub(1).ok_or_else(|| EngineError::Params(format!("step `{step}` has no {what}")))?;
|
||||
Ok(if series && !last_only { (0..n).map(State::Frame).collect() } else { vec![State::Frame(last)] })
|
||||
};
|
||||
if mode.is_some() && !matches!(r.kind(), Kind::Frequency(_)) {
|
||||
return Err(EngineError::invalid("mode", "is for frequency and buckling steps"));
|
||||
}
|
||||
let states: Vec<State> = match r.kind() {
|
||||
Kind::Fluid(_) => return Err(EngineError::Params("fluid results are per cell; not supported yet".into())),
|
||||
Kind::Static(st) => match &st.nonlinear {
|
||||
Some(h) if !h.frames.is_empty() => frames(h.frames.len(), "frames")?,
|
||||
_ => vec![State::Final],
|
||||
},
|
||||
Kind::Frequency(f) => match mode {
|
||||
Some(m) if m == 0 || m > f.modes.len() => {
|
||||
return Err(EngineError::invalid("mode", format!("step `{step}` has modes 1 to {}", f.modes.len())));
|
||||
}
|
||||
Some(m) => vec![State::Mode(m)],
|
||||
None if series && !last_only => (1..=f.modes.len()).map(State::Mode).collect(),
|
||||
None if f.modes.is_empty() => return Err(EngineError::Params(format!("step `{step}` has no modes"))),
|
||||
None => vec![State::Mode(1)],
|
||||
},
|
||||
Kind::Explicit(e) => frames(e.frames.len(), "field frames")?,
|
||||
Kind::Heat(h) => frames(h.frames.len(), "temperatures")?,
|
||||
};
|
||||
if states.len() > MAX_VTU_FILES {
|
||||
return Err(EngineError::Params(format!("step `{step}` has {} frames; at most {MAX_VTU_FILES} files are written", states.len())));
|
||||
}
|
||||
|
||||
let model = &s.model;
|
||||
let (nodes, elements) = vtk::points_and_cells(model);
|
||||
let build = |state: State| -> Result<(f64, String)> {
|
||||
let (time, fields, data) = vtu_fields(&r, state, &nodes, &elements);
|
||||
let data: Vec<(&str, f64)> = data.iter().map(|(n, v)| (*n, *v)).collect();
|
||||
Ok((time, vtk::write_vtu(model, &fields, &data, encoding)?))
|
||||
};
|
||||
// The last state has the most fields: its size times the count bounds the output before
|
||||
// anything is written.
|
||||
let Some(&last) = states.last() else { return Err(EngineError::Params(format!("step `{step}` has nothing to write"))) };
|
||||
let sample = build(last)?;
|
||||
let estimate = (sample.1.len() as u64).saturating_mul(states.len() as u64);
|
||||
if estimate > MAX_VTU_BYTES {
|
||||
return Err(EngineError::Params(format!(
|
||||
"the export would write about {estimate} bytes; at most {MAX_VTU_BYTES} (8 GiB) are written: choose `frames: \"last\"` or a single .vtu"
|
||||
)));
|
||||
}
|
||||
|
||||
let mut written: Vec<std::path::PathBuf> = Vec::new();
|
||||
let mut bytes = 0usize;
|
||||
let result = (|| -> Result<Vec<String>> {
|
||||
if !series {
|
||||
fieldcraft_io::fs::write_atomic(target, sample.1.as_bytes(), false)?;
|
||||
written.push(target.to_path_buf());
|
||||
bytes += sample.1.len();
|
||||
return Ok(vec![path.clone()]);
|
||||
}
|
||||
let stem = target.file_stem().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let dir = target.parent().unwrap_or(std::path::Path::new(""));
|
||||
let mut entries: Vec<(f64, String)> = Vec::with_capacity(states.len());
|
||||
let mut files = Vec::with_capacity(states.len() + 1);
|
||||
for (k, &state) in states.iter().enumerate() {
|
||||
let name = format!("{stem}_{k:04}.vtu");
|
||||
let file = dir.join(&name);
|
||||
let built = if state == last { None } else { Some(build(state)?) };
|
||||
let (time, text) = built.as_ref().map_or((sample.0, sample.1.as_str()), |(t, x)| (*t, x.as_str()));
|
||||
fieldcraft_io::fs::write_atomic(&file, text.as_bytes(), false)?;
|
||||
written.push(file.clone());
|
||||
bytes += text.len();
|
||||
entries.push((time, name));
|
||||
files.push(file.to_string_lossy().into_owned());
|
||||
}
|
||||
let list: Vec<(f64, &str)> = entries.iter().map(|(t, n)| (*t, n.as_str())).collect();
|
||||
let collection = vtk::pvd(&list);
|
||||
fieldcraft_io::fs::write_atomic(target, collection.as_bytes(), false)?;
|
||||
bytes += collection.len();
|
||||
files.push(path.clone());
|
||||
Ok(files)
|
||||
})();
|
||||
match result {
|
||||
Ok(files) => Ok(json!({"files": files, "bytes": bytes, "points": nodes.len(), "cells": elements.len()})),
|
||||
Err(e) => {
|
||||
// Leave nothing half written behind.
|
||||
for f in &written {
|
||||
let _ = std::fs::remove_file(f);
|
||||
}
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A stress (xx, yy, zz, xy, xz, yz) in VTK's symmetric tensor order (XX, YY, ZZ, XY, YZ, XZ).
|
||||
fn vtk_tensor(t: &[f64; 6]) -> [f64; 6] {
|
||||
[t[0], t[1], t[2], t[3], t[5], t[4]]
|
||||
}
|
||||
|
||||
/// Values given per label (`labels` sorted, as a result's nodes and elements are) spread over
|
||||
/// `order` (the points' or cells' labels): `N` components each, 0 where a label has none.
|
||||
fn spread<const N: usize>(order: &[u32], labels: &[u32], values: &[[f64; N]]) -> Vec<f64> {
|
||||
let mut out = vec![0.0; order.len() * N];
|
||||
for (label, v) in labels.iter().zip(values) {
|
||||
if let Ok(i) = order.binary_search(label)
|
||||
&& let Some(slot) = out.get_mut(i * N..(i + 1) * N)
|
||||
{
|
||||
slot.copy_from_slice(v);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn scalars(values: &[f64]) -> Vec<[f64; 1]> {
|
||||
values.iter().map(|v| [*v]).collect()
|
||||
}
|
||||
|
||||
/// The fields of one state of a result, its time (load factor, mode number) and its field data.
|
||||
fn vtu_fields(
|
||||
r: &crate::StepResult,
|
||||
state: State,
|
||||
nodes: &[u32],
|
||||
elements: &[u32],
|
||||
) -> (f64, Vec<fieldcraft_io::vtk::Field>, Vec<(&'static str, f64)>) {
|
||||
use fieldcraft_io::vtk::{Field, Location};
|
||||
let mut fields = Vec::new();
|
||||
let mut data = Vec::new();
|
||||
let mut add = |name: &str, location: Location, components: usize, values: Vec<f64>, integer: bool| {
|
||||
fields.push(Field { name: name.into(), location, components, values, integer });
|
||||
};
|
||||
let on = |labels: &[u32]| -> Vec<f64> { nodes.iter().map(|n| if labels.binary_search(n).is_ok() { 1.0 } else { 0.0 }).collect() };
|
||||
add("has_result", Location::Point, 1, on(r.nodes()), true);
|
||||
let point = Location::Point;
|
||||
let cell = Location::Cell;
|
||||
let time = match (r.kind(), state) {
|
||||
(Kind::Static(st), _) => {
|
||||
let frame = match (state, &st.nonlinear) {
|
||||
(State::Frame(k), Some(h)) => h.frames.get(k).map(|f| (f.load_factor, f.displacement.as_slice(), k + 1 == h.frames.len())),
|
||||
_ => None,
|
||||
};
|
||||
let (time, displacement, last) = frame.unwrap_or((0.0, st.displacement.as_slice(), true));
|
||||
add("displacement", point, 3, spread(nodes, &st.nodes, displacement), false);
|
||||
if last {
|
||||
if !st.shells.is_empty() || !st.beams.is_empty() {
|
||||
add("rotation", point, 3, spread(nodes, &st.nodes, &st.rotation), false);
|
||||
}
|
||||
add("reaction", point, 3, spread(nodes, &st.nodes, &st.reaction), false);
|
||||
if !st.stress.is_empty() {
|
||||
let stress: Vec<[f64; 6]> = st.stress.iter().map(vtk_tensor).collect();
|
||||
add("stress", point, 6, spread(nodes, &st.nodes, &stress), false);
|
||||
add("von_mises", point, 1, spread(nodes, &st.nodes, &scalars(&st.von_mises)), false);
|
||||
}
|
||||
if let Some(t) = &st.temperature {
|
||||
add("temperature", point, 1, spread(nodes, &st.nodes, &scalars(t)), false);
|
||||
}
|
||||
}
|
||||
if st.nonlinear.is_some() {
|
||||
data.push(("load_factor", time));
|
||||
}
|
||||
time
|
||||
}
|
||||
(Kind::Frequency(f), State::Mode(m)) => {
|
||||
if let Some(mode) = m.checked_sub(1).and_then(|k| f.modes.get(k)) {
|
||||
add("mode_shape", point, 3, spread(nodes, &f.nodes, &mode.shape), false);
|
||||
data.extend([("mode", m as f64), ("frequency", mode.frequency), ("eigenvalue", mode.eigenvalue)]);
|
||||
}
|
||||
m as f64
|
||||
}
|
||||
(Kind::Explicit(e), State::Frame(k)) => {
|
||||
let Some(f) = e.frames.get(k) else { return (0.0, fields, data) };
|
||||
if !f.displacement.is_empty() {
|
||||
add("displacement", point, 3, spread(nodes, &e.nodes, &f.displacement), false);
|
||||
}
|
||||
if !f.velocity.is_empty() {
|
||||
add("velocity", point, 3, spread(nodes, &e.nodes, &f.velocity), false);
|
||||
}
|
||||
if !f.stress.is_empty() {
|
||||
let stress: Vec<[f64; 6]> = f.stress.iter().map(vtk_tensor).collect();
|
||||
add("stress", cell, 6, spread(elements, &e.elements, &stress), false);
|
||||
}
|
||||
if !f.plastic_strain.is_empty() {
|
||||
add("plastic_strain", cell, 1, spread(elements, &e.elements, &scalars(&f.plastic_strain)), false);
|
||||
}
|
||||
if !f.damage.is_empty() {
|
||||
add("damage", cell, 1, spread(elements, &e.elements, &scalars(&f.damage)), false);
|
||||
}
|
||||
if !f.failed.is_empty() {
|
||||
let eroded: Vec<[f64; 1]> = f.failed.iter().map(|&b| [if b { 1.0 } else { 0.0 }]).collect();
|
||||
add("eroded", cell, 1, spread(elements, &e.elements, &eroded), true);
|
||||
}
|
||||
data.push(("time", f.time));
|
||||
f.time
|
||||
}
|
||||
(Kind::Heat(h), State::Frame(k)) => {
|
||||
let time = h.times.get(k).copied().unwrap_or(0.0);
|
||||
if let Some(t) = h.frames.get(k) {
|
||||
add("temperature", point, 1, spread(nodes, &h.nodes, &scalars(t)), false);
|
||||
}
|
||||
if k + 1 == h.frames.len() && !h.flux.is_empty() {
|
||||
add("heat_flux", point, 3, spread(nodes, &h.nodes, &h.flux), false);
|
||||
}
|
||||
data.push(("time", time));
|
||||
time
|
||||
}
|
||||
_ => 0.0,
|
||||
};
|
||||
(time, fields, data)
|
||||
}
|
||||
|
||||
fn export_csv(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let what: String = super::req(p, "what")?;
|
||||
let path = super::file_param(s, p, "path", fieldcraft_io::roots::Access::Write)?;
|
||||
@@ -405,13 +660,13 @@ fn export_csv(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
|
||||
/// A table of cells (a number, text, or empty) under a header, written as CSV.
|
||||
struct Table {
|
||||
header: Vec<String>,
|
||||
rows: Vec<Vec<Cell>>,
|
||||
pub(super) struct Table {
|
||||
pub(super) header: Vec<String>,
|
||||
pub(super) rows: Vec<Vec<Cell>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
enum Cell {
|
||||
pub(super) enum Cell {
|
||||
Number(f64),
|
||||
Text(String),
|
||||
Empty,
|
||||
@@ -420,7 +675,7 @@ enum Cell {
|
||||
impl Table {
|
||||
/// RFC 4180: comma-separated, CRLF line ends, a field with a comma, quote or line break in
|
||||
/// quotes (its quotes doubled); numbers in Rust's shortest round-trip form.
|
||||
fn csv(&self) -> String {
|
||||
pub(super) fn csv(&self) -> String {
|
||||
let field = |s: &str| if s.contains([',', '"', '\r', '\n']) { format!("\"{}\"", s.replace('"', "\"\"")) } else { s.to_string() };
|
||||
let mut out = String::new();
|
||||
let line = |cells: Vec<String>, out: &mut String| {
|
||||
@@ -549,6 +804,164 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::cmd::solve::tests::cantilever;
|
||||
|
||||
/// The numbers of the first `DataArray` called `name` in a written `.vtu`.
|
||||
fn vtu_array(xml: &str, name: &str) -> Vec<f64> {
|
||||
let at = xml.find(&format!("Name=\"{name}\"")).unwrap_or_else(|| panic!("no array {name}"));
|
||||
let start = at + xml[at..].find('>').unwrap() + 1;
|
||||
let end = start + xml[start..].find("</DataArray>").unwrap();
|
||||
xml[start..end].split_whitespace().map(|t| t.parse().unwrap()).collect()
|
||||
}
|
||||
|
||||
fn scratch(name: &str) -> std::path::PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("fieldcraft-vtu-{name}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
dir
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_vtu_writes_a_static_step_and_its_modes() {
|
||||
let dir = scratch("static");
|
||||
let out = dir.join("out.vtu").to_string_lossy().into_owned();
|
||||
let mut s = Session::default();
|
||||
s.execute("file.sample", &json!({"name": "cantilever"})).unwrap();
|
||||
let e = s.execute("results.export.vtu", &json!({"step": "Load", "path": out})).unwrap_err().to_string();
|
||||
assert!(e.contains("Needs a solved step"), "{e}");
|
||||
s.execute("solve.static", &json!({"step": "Load"})).unwrap();
|
||||
let reply = s.execute("results.export.vtu", &json!({"step": "Load", "path": out})).unwrap();
|
||||
let xml = std::fs::read_to_string(&out).unwrap();
|
||||
assert_eq!(reply["points"], s.model.nodes().len());
|
||||
assert_eq!(reply["cells"], s.model.elements().len());
|
||||
assert_eq!(reply["bytes"], xml.len());
|
||||
assert!(xml.contains(&format!("NumberOfPoints=\"{}\"", s.model.nodes().len())));
|
||||
|
||||
// The tip node's displacement is the probe's, at the node's point.
|
||||
let labels = vtu_array(&xml, "node_label");
|
||||
let u = vtu_array(&xml, "displacement");
|
||||
let r = s.result("Load").unwrap().clone();
|
||||
let Kind::Static(st) = r.kind() else { panic!() };
|
||||
let (k, tip) = st.displacement.iter().enumerate().max_by(|a, b| a.1[2].abs().total_cmp(&b.1[2].abs())).unwrap();
|
||||
let tip_label = st.nodes[k];
|
||||
let probe = s.execute("solve.probe", &json!({"step": "Load", "nodes": [tip_label]})).unwrap();
|
||||
let i = labels.iter().position(|&l| l == f64::from(tip_label)).unwrap();
|
||||
assert_eq!(&u[3 * i..3 * i + 3], tip.as_slice());
|
||||
assert_eq!(
|
||||
probe["nodes"][0]["displacement"].as_array().map(|a| a.iter().filter_map(Value::as_f64).collect::<Vec<_>>()),
|
||||
Some(tip.to_vec()),
|
||||
"{probe}"
|
||||
);
|
||||
// Stress components in VTK order: XY at 3, YZ at 4, XZ at 5.
|
||||
let stress = vtu_array(&xml, "stress");
|
||||
let t = st.stress[k];
|
||||
assert_eq!(&stress[6 * i..6 * i + 6], [t[0], t[1], t[2], t[3], t[5], t[4]].as_slice());
|
||||
assert_eq!(vtu_array(&xml, "has_result").iter().filter(|&&h| h == 1.0).count(), st.nodes.len());
|
||||
// Deterministic.
|
||||
s.execute("results.export.vtu", &json!({"step": "Load", "path": out})).unwrap();
|
||||
assert_eq!(std::fs::read_to_string(&out).unwrap(), xml);
|
||||
|
||||
// Modes: one file per mode with its frequency, and a collection.
|
||||
s.execute("solve.frequency", &json!({"step": "Modes"})).unwrap();
|
||||
let pvd = dir.join("modes.pvd").to_string_lossy().into_owned();
|
||||
let reply = s.execute("results.export.vtu", &json!({"step": "Modes", "path": pvd})).unwrap();
|
||||
let files = reply["files"].as_array().unwrap();
|
||||
assert_eq!(files.len(), 7, "six modes and the collection: {reply}");
|
||||
let mode2 = std::fs::read_to_string(dir.join("modes_0001.vtu")).unwrap();
|
||||
assert_eq!(vtu_array(&mode2, "mode"), [2.0]);
|
||||
let collection = std::fs::read_to_string(&pvd).unwrap();
|
||||
assert!(collection.contains("timestep=\"2.0\" part=\"0\" file=\"modes_0001.vtu\""), "{collection}");
|
||||
let one = dir.join("mode3.vtu").to_string_lossy().into_owned();
|
||||
s.execute("results.export.vtu", &json!({"step": "Modes", "path": one, "mode": 3, "encoding": "binary"})).unwrap();
|
||||
assert!(std::fs::read_to_string(&one).unwrap().contains("format=\"binary\""));
|
||||
|
||||
// Refusals write nothing.
|
||||
let none = dir.join("none.vtu").to_string_lossy().into_owned();
|
||||
for (p, why) in [
|
||||
(json!({"step": "Modes", "path": none, "mode": 0}), "modes 1 to 6"),
|
||||
(json!({"step": "Modes", "path": none, "mode": 1_000_000_000}), "modes 1 to 6"),
|
||||
(json!({"step": "Load", "path": none, "mode": 1}), "frequency"),
|
||||
(json!({"step": "Load", "path": dir.join("x.vtk").to_string_lossy()}), ".vtu"),
|
||||
(json!({"step": "Load", "path": none, "frames": 3}), "frames"),
|
||||
(json!({"step": "Load", "path": none, "encoding": "hex"}), "encoding"),
|
||||
(json!({"step": "Nope", "path": none}), "has no result"),
|
||||
] {
|
||||
let e = s.execute("results.export.vtu", &p).unwrap_err().to_string();
|
||||
assert!(e.contains(why), "{p}: {e}");
|
||||
}
|
||||
s.execute("mesh.node.add", &json!({"label": 9999, "xyz": [50, 50, 50]})).unwrap();
|
||||
let e = s.execute("results.export.vtu", &json!({"step": "Load", "path": none})).unwrap_err().to_string();
|
||||
assert!(e.contains("the model changed since step `Load` was solved"), "{e}");
|
||||
assert!(!dir.join("none.vtu").exists() && !dir.join("x.vtk").exists());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_vtu_writes_nonlinear_frames_and_heat() {
|
||||
let dir = scratch("nl-heat");
|
||||
let mut s = Session::default();
|
||||
cantilever(&mut s);
|
||||
s.run_script(&json!([
|
||||
{"command": "model.section.edit", "params": {"name": "bar", "set": "all", "material": "Mat", "kind": {"type": "solid"}, "formulation": {}}},
|
||||
{"command": "model.step.create", "params": {"name": "Large", "procedure": {"type": "nonlinear_static", "initial_increment": 0.25},
|
||||
"boundary_conditions": ["clamp"], "loads": ["push"]}},
|
||||
{"command": "model.material.edit", "params": {"name": "Mat", "elastic": {"type": "isotropic", "young": 1000, "poisson": 0}, "density": 1, "thermal": {"conductivity": 2}}},
|
||||
{"command": "model.boundary_condition.create", "params": {"name": "Heater", "type": "temperature", "target": {"set": "root"}, "value": 100}},
|
||||
{"command": "model.set.create", "params": {"name": "skin", "faces": (1..=6).map(|f| json!({"element": 10, "face": f})).collect::<Vec<_>>()}},
|
||||
{"command": "model.load.create", "params": {"name": "Air", "type": "convection", "set": "skin", "film": 0.5, "sink": 20}},
|
||||
{"command": "model.step.create", "params": {"name": "Therm", "procedure": {"type": "heat_transfer"}, "boundary_conditions": ["Heater"], "loads": ["Air"]}},
|
||||
]))
|
||||
.unwrap();
|
||||
s.execute("solve.nonlinear", &json!({"step": "Large"})).unwrap();
|
||||
s.execute("solve.heat", &json!({"step": "Therm"})).unwrap();
|
||||
|
||||
let pvd = dir.join("large.pvd").to_string_lossy().into_owned();
|
||||
let reply = s.execute("results.export.vtu", &json!({"step": "Large", "path": pvd})).unwrap();
|
||||
let Kind::Static(st) = s.result("Large").unwrap().kind() else { panic!() };
|
||||
let h = st.nonlinear.as_ref().unwrap();
|
||||
assert_eq!(reply["files"].as_array().unwrap().len(), h.frames.len() + 1);
|
||||
let first = std::fs::read_to_string(dir.join("large_0000.vtu")).unwrap();
|
||||
let last = std::fs::read_to_string(dir.join(format!("large_{:04}.vtu", h.frames.len() - 1))).unwrap();
|
||||
assert!(first.contains("Name=\"displacement\"") && !first.contains("Name=\"stress\""), "final stresses on the last frame only");
|
||||
assert!(last.contains("Name=\"stress\"") && last.contains("Name=\"von_mises\""));
|
||||
assert_eq!(vtu_array(&last, "load_factor"), [h.frames.last().unwrap().load_factor]);
|
||||
|
||||
let heat = dir.join("heat.vtu").to_string_lossy().into_owned();
|
||||
s.execute("results.export.vtu", &json!({"step": "Therm", "path": heat})).unwrap();
|
||||
let xml = std::fs::read_to_string(&heat).unwrap();
|
||||
let t = vtu_array(&xml, "temperature");
|
||||
assert!(t.contains(&100.0), "the heater's temperature");
|
||||
assert_eq!(vtu_array(&xml, "heat_flux").len(), 3 * s.model.nodes().len());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_vtu_writes_explicit_frames_as_a_series() {
|
||||
let dir = scratch("impact");
|
||||
let mut s = Session::default();
|
||||
s.execute("file.sample", &json!({"name": "impact"})).unwrap();
|
||||
s.execute("solve.explicit", &json!({})).unwrap();
|
||||
let pvd = dir.join("impact.pvd").to_string_lossy().into_owned();
|
||||
let reply = s.execute("results.export.vtu", &json!({"path": pvd})).unwrap();
|
||||
let Kind::Explicit(e) = s.result("Impact").unwrap().kind() else { panic!() };
|
||||
let frames = e.frames.len();
|
||||
assert!(frames > 2);
|
||||
assert_eq!(reply["files"].as_array().unwrap().len(), frames + 1);
|
||||
let collection = std::fs::read_to_string(&pvd).unwrap();
|
||||
let times: Vec<f64> =
|
||||
collection.match_indices("timestep=\"").map(|(i, m)| collection[i + m.len()..].split('"').next().unwrap().parse().unwrap()).collect();
|
||||
assert_eq!(times.len(), frames);
|
||||
assert!(times.windows(2).all(|w| w[0] < w[1]), "{times:?}");
|
||||
let last = std::fs::read_to_string(dir.join(format!("impact_{:04}.vtu", frames - 1))).unwrap();
|
||||
let cell = &last[last.find("<CellData>").unwrap()..last.find("</CellData>").unwrap()];
|
||||
assert!(cell.contains("Name=\"stress\" NumberOfComponents=\"6\""), "{cell}");
|
||||
// A .vtu takes the last frame; `frames: "last"` makes a one-entry series.
|
||||
let one = dir.join("last.vtu").to_string_lossy().into_owned();
|
||||
s.execute("results.export.vtu", &json!({"path": one})).unwrap();
|
||||
assert_eq!(std::fs::read_to_string(&one).unwrap(), last);
|
||||
let lastp = dir.join("last.pvd").to_string_lossy().into_owned();
|
||||
assert_eq!(s.execute("results.export.vtu", &json!({"path": lastp, "frames": "last"})).unwrap()["files"].as_array().unwrap().len(), 2);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
fn solved() -> Session {
|
||||
let mut s = Session::default();
|
||||
cantilever(&mut s);
|
||||
|
||||
@@ -0,0 +1,461 @@
|
||||
//! Size controls: `mesh.size.set` asks for elements of a size on faces or bodies, in a box or in
|
||||
//! a sphere; `mesh.size.growth` sets how fast the size grows away from them; `mesh.size.clear`
|
||||
//! removes controls and `mesh.size.list` lists them with what each covers in the open design.
|
||||
//! Controls live in the model (`fieldcraft_model::sizes`), are saved with it, and every mesher
|
||||
//! reads them through [`Sizing`]: the tetrahedral mesher bakes them into its graded size field,
|
||||
//! the structured hex meshers and the octree take the exact graded field
|
||||
//! ([`fieldcraft_mesh::size::RegionSize`]), and the block projection (`mesh.hex.project`) divides
|
||||
//! its block edges by it.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use fieldcraft_geometry::{Design, Shape};
|
||||
use fieldcraft_mesh::size::{LocalSize, Region, RegionSize};
|
||||
use fieldcraft_mesh::tet::QualityOptions;
|
||||
use fieldcraft_model::{Entity, GeometryRef, SizeControl, SizeRegion};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::opt;
|
||||
use crate::requirement::Requirement;
|
||||
use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
|
||||
/// Most controls one `mesh.size.clear` names.
|
||||
const MAX_NAMES: usize = 10_000;
|
||||
/// The chordal tolerance of the tessellation a face control is resolved on, relative to the
|
||||
/// body's diagonal (as the meshers' default).
|
||||
const FACE_TOLERANCE: f64 = 1e-3;
|
||||
|
||||
pub const SIZE_SET: CommandSpec = CommandSpec {
|
||||
id: "mesh.size.set",
|
||||
label: "Local Size",
|
||||
params: r#"{"name": "Hole", "size": 0.5, and one of "faces": [{"body": "Body1", "face": "F3"}, …] (persistent face names, geometry.bodies; a name without its `#k` number takes every piece), "bodies": ["Body1", …], "box": {"min": [x, y, z], "max": [x, y, z]}, "sphere": {"center": [x, y, z], "radius": r}} — adds the size control `name`, or replaces it: elements of about `size` on those faces, in those bodies, in the box or in the sphere. Every mesher honours it: the size at a point is the smallest of the mesher's size and, over the controls, a control's size plus the growth rate (mesh.size.growth) times the distance to its region, so elements grow away from it gradually. A control finer than the mesher's size refines; a coarser one changes nothing, except on whole bodies, where it is the body's size in place of the mesher's `size` (meshers that mesh several bodies together, mesh.hex.blocks and mesh.cells, take it as a control on the body's surface). Saved with the model; mesh.size.list shows it"#,
|
||||
form: Some(super::forms::SIZE_SET),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[],
|
||||
run: size_set,
|
||||
};
|
||||
|
||||
pub const SIZE_GROWTH: CommandSpec = CommandSpec {
|
||||
id: "mesh.size.growth",
|
||||
label: "Size Growth",
|
||||
params: r#"{"rate": 0.3 (0.05..=2; omitted or null: back to the default, 0.3)} — the size field's growth rate: the largest change of element size per unit length away from a size control or an edge seed. mesh.tet.generate's `grading` overrides it for one run"#,
|
||||
form: Some(super::forms::SIZE_GROWTH),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[],
|
||||
run: size_growth,
|
||||
};
|
||||
|
||||
pub const SIZE_CLEAR: CommandSpec = CommandSpec {
|
||||
id: "mesh.size.clear",
|
||||
label: "Clear Local Sizes",
|
||||
params: r#"{"names"?: ["Hole", …]} — removes these size controls, or every one"#,
|
||||
form: Some(super::forms::SIZE_CLEAR),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::SizeControl],
|
||||
run: size_clear,
|
||||
};
|
||||
|
||||
pub const SIZE_LIST: CommandSpec = CommandSpec {
|
||||
id: "mesh.size.list",
|
||||
label: "Local Sizes",
|
||||
params: "{} — the size controls by name, each with its region and size and, for faces and bodies, what it covers in the open design (`stale`: names the design no longer has), and the growth rate",
|
||||
form: Some(&[]),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[],
|
||||
run: size_list,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct BoxParams {
|
||||
min: [f64; 3],
|
||||
max: [f64; 3],
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct SphereParams {
|
||||
center: [f64; 3],
|
||||
radius: f64,
|
||||
}
|
||||
|
||||
fn size_set(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name: String = opt(p, "name")?.ok_or_else(|| EngineError::Params("`name` is required".into()))?;
|
||||
let size: f64 = opt(p, "size")?.ok_or_else(|| EngineError::Params("`size` is required: the element size".into()))?;
|
||||
let faces: Option<Vec<GeometryRef>> = opt(p, "faces")?;
|
||||
let bodies: Option<Vec<String>> = opt(p, "bodies")?;
|
||||
let boxed: Option<BoxParams> = opt(p, "box")?;
|
||||
let sphere: Option<SphereParams> = opt(p, "sphere")?;
|
||||
let given = [faces.is_some(), bodies.is_some(), boxed.is_some(), sphere.is_some()].iter().filter(|g| **g).count();
|
||||
if given != 1 {
|
||||
return Err(EngineError::Params("give one of `faces`, `bodies`, `box` or `sphere`".into()));
|
||||
}
|
||||
let region = if let Some(faces) = faces {
|
||||
if let Some(r) = faces.iter().find(|r| !matches!(r.entity, Entity::Face(_))) {
|
||||
return Err(EngineError::Params(format!("`faces` lists the {r}; name faces as {{body, face}}")));
|
||||
}
|
||||
SizeRegion::Geometry(faces)
|
||||
} else if let Some(bodies) = bodies {
|
||||
SizeRegion::Geometry(bodies.into_iter().map(|b| GeometryRef::new(b, Entity::Body)).collect::<fieldcraft_model::Result<_>>()?)
|
||||
} else if let Some(b) = boxed {
|
||||
SizeRegion::Box { min: b.min, max: b.max }
|
||||
} else if let Some(sp) = sphere {
|
||||
SizeRegion::Sphere { center: sp.center, radius: sp.radius }
|
||||
} else {
|
||||
return Err(EngineError::Params("give one of `faces`, `bodies`, `box` or `sphere`".into()));
|
||||
};
|
||||
let control = SizeControl { region, size };
|
||||
// Faces and bodies must name the open design's, when there is one.
|
||||
if let (Some(design), SizeRegion::Geometry(refs)) = (s.design.as_deref(), &control.region)
|
||||
&& let Some(r) = refs.iter().find(|r| covered(design, r).is_ok_and(|c| c.is_empty()))
|
||||
{
|
||||
return Err(EngineError::Params(format!("the design has no {r} (geometry.bodies lists the faces)")));
|
||||
}
|
||||
s.model.set_size_control(&name, control.clone())?;
|
||||
Ok(report(s, &name, &control))
|
||||
}
|
||||
|
||||
fn size_growth(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let rate: Option<f64> = opt(p, "rate")?;
|
||||
s.model.set_size_growth(rate)?;
|
||||
Ok(json!({"growth": s.model.size_growth(), "default": !s.model.size_growth_set()}))
|
||||
}
|
||||
|
||||
fn size_clear(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let names: Option<Vec<String>> = opt(p, "names")?;
|
||||
let cleared = match names {
|
||||
None => {
|
||||
let all: Vec<String> = s.model.size_controls().keys().cloned().collect();
|
||||
if all.is_empty() {
|
||||
return Err(EngineError::Params("the model has no size controls".into()));
|
||||
}
|
||||
for n in &all {
|
||||
s.model.remove_size_control(n)?;
|
||||
}
|
||||
all.len()
|
||||
}
|
||||
Some(names) => {
|
||||
if names.is_empty() || names.len() > MAX_NAMES {
|
||||
return Err(EngineError::Params(format!("`names` must list 1 to {MAX_NAMES} size controls")));
|
||||
}
|
||||
for n in &names {
|
||||
s.model.remove_size_control(n)?;
|
||||
}
|
||||
names.len()
|
||||
}
|
||||
};
|
||||
Ok(json!({"cleared": cleared, "left": s.model.size_controls().len()}))
|
||||
}
|
||||
|
||||
fn size_list(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
let controls: serde_json::Map<String, Value> = s.model.size_controls().iter().map(|(n, c)| (n.clone(), report(s, n, c))).collect();
|
||||
Ok(json!({"controls": controls, "growth": s.model.size_growth()}))
|
||||
}
|
||||
|
||||
/// A control's report: its definition and, for faces and bodies, the names each covers in the
|
||||
/// open design.
|
||||
fn report(s: &Session, name: &str, c: &SizeControl) -> Value {
|
||||
let mut v = json!({"name": name, "size": c.size, "region": c.region});
|
||||
if let (SizeRegion::Geometry(refs), Some(design)) = (&c.region, s.design.as_deref()) {
|
||||
let on: Vec<Value> = refs.iter().map(|r| json!({"target": r.to_string(), "covers": covered(design, r).unwrap_or_default()})).collect();
|
||||
v["stale"] = json!(on.iter().any(|o| o["covers"].as_array().is_none_or(Vec::is_empty)));
|
||||
v["on"] = json!(on);
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// The faces a face reference selects (its `#k` pieces too), or the body a body reference names.
|
||||
fn covered(design: &Design, r: &GeometryRef) -> Result<Vec<String>> {
|
||||
let Some(body) = design.body(&r.body) else { return Ok(Vec::new()) };
|
||||
Ok(match r.entity {
|
||||
Entity::Body => vec![body.name.clone()],
|
||||
_ => body.faces().iter().filter(|f| selects(r, &body.name, &f.name)).map(|f| f.name.clone()).collect(),
|
||||
})
|
||||
}
|
||||
|
||||
fn selects(r: &GeometryRef, body: &str, face: &str) -> bool {
|
||||
GeometryRef::new(body.to_string(), Entity::Face(face.to_string())).is_ok_and(|g| r.selects(&g))
|
||||
}
|
||||
|
||||
/// The model's size controls resolved on the open design, as the meshers take them.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub(crate) struct Sizing {
|
||||
/// Local sizes on faces, in boxes and in spheres.
|
||||
pub(crate) locals: Vec<LocalSize>,
|
||||
/// Per body, the size its body controls set.
|
||||
pub(crate) bodies: BTreeMap<String, f64>,
|
||||
/// Per body, its boundary surface's triangles (for the meshers that take a body control as
|
||||
/// a local size on its surface).
|
||||
surfaces: BTreeMap<String, Vec<[[f64; 3]; 3]>>,
|
||||
pub(crate) growth: f64,
|
||||
}
|
||||
|
||||
/// The triangles of `shape`'s faces that `select` picks.
|
||||
fn triangles(shape: &Shape, select: impl Fn(&str) -> bool) -> Result<Vec<[[f64; 3]; 3]>> {
|
||||
let tolerance = FACE_TOLERANCE * shape.size()?;
|
||||
let mesh = shape.tessellate(tolerance)?;
|
||||
let names = shape.faces();
|
||||
let point = |i: u32| mesh.positions.get(i as usize).copied();
|
||||
Ok(mesh
|
||||
.triangles
|
||||
.iter()
|
||||
.zip(&mesh.tri_face)
|
||||
.filter(|(_, f)| names.get(**f as usize).is_some_and(|n| select(&n.name)))
|
||||
.filter_map(|(t, _)| Some([point(t[0])?, point(t[1])?, point(t[2])?]))
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Resolve the model's size controls on the open design. Face controls on faces the design does
|
||||
/// not have, and body controls on bodies it does not have, are left out (mesh.size.list shows
|
||||
/// them as stale).
|
||||
pub(crate) fn sizing(s: &Session) -> Result<Sizing> {
|
||||
let mut out = Sizing { growth: s.model.size_growth(), ..Sizing::default() };
|
||||
for c in s.model.size_controls().values() {
|
||||
let region = match &c.region {
|
||||
SizeRegion::Box { min, max } => Region::Box { lo: *min, hi: *max },
|
||||
SizeRegion::Sphere { center, radius } => Region::Sphere { center: *center, radius: *radius },
|
||||
SizeRegion::Geometry(refs) => {
|
||||
let Some(design) = s.design.as_deref() else { continue };
|
||||
let mut tris = Vec::new();
|
||||
for r in refs {
|
||||
let Some(shape) = design.body(&r.body) else { continue };
|
||||
if r.entity == Entity::Body {
|
||||
let size = out.bodies.entry(shape.name.clone()).or_insert(c.size);
|
||||
*size = size.min(c.size);
|
||||
if !out.surfaces.contains_key(&shape.name) {
|
||||
out.surfaces.insert(shape.name.clone(), triangles(shape, |_| true)?);
|
||||
}
|
||||
} else {
|
||||
tris.extend(triangles(shape, |f| selects(r, &shape.name, f))?);
|
||||
}
|
||||
}
|
||||
if tris.is_empty() {
|
||||
continue;
|
||||
}
|
||||
Region::Triangles(tris)
|
||||
}
|
||||
};
|
||||
out.locals.push(LocalSize { region, size: c.size });
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
impl Sizing {
|
||||
/// The size of `body` meshed alone: its body control's, else `size`.
|
||||
pub(crate) fn body_size(&self, body: &str, size: f64) -> f64 {
|
||||
self.bodies.get(body).copied().unwrap_or(size)
|
||||
}
|
||||
|
||||
/// The sizing of several bodies meshed together at one size, so the curves and faces they
|
||||
/// share are divided alike: the face, box and sphere controls, and each body control as a
|
||||
/// local size on that body's surface.
|
||||
pub(crate) fn joint(&self, bodies: &[&str]) -> Sizing {
|
||||
let mut locals = self.locals.clone();
|
||||
for b in bodies {
|
||||
if let (Some(&size), Some(tris)) = (self.bodies.get(*b), self.surfaces.get(*b)) {
|
||||
locals.push(LocalSize { region: Region::Triangles(tris.clone()), size });
|
||||
}
|
||||
}
|
||||
Sizing { locals, growth: self.growth, ..Sizing::default() }
|
||||
}
|
||||
|
||||
/// The graded field of `body` meshed at `size` (its body control's size in place of it).
|
||||
pub(crate) fn field(&self, body: &str, size: f64) -> Result<RegionSize> {
|
||||
Ok(RegionSize::new(self.body_size(body, size), self.growth, self.locals.clone())?)
|
||||
}
|
||||
|
||||
/// The graded field of the face, box and sphere controls about a size already chosen (body
|
||||
/// controls taken into it, or a mesh plan's size), for a mesher that takes a uniform size
|
||||
/// and an optional field; `None` when no control refines it.
|
||||
pub(crate) fn local_field(&self, size: f64) -> Result<Option<RegionSize>> {
|
||||
Ok(Some(RegionSize::new(size, self.growth, self.locals.clone())?).filter(|f| !f.is_uniform()))
|
||||
}
|
||||
|
||||
/// The tetrahedral mesher's options for `body`: its body control's size in place of the
|
||||
/// global one, the local sizes, and the growth rate unless the command gives `grading`.
|
||||
pub(crate) fn tet_options(&self, body: &str, p: &Value, o: &QualityOptions) -> QualityOptions {
|
||||
QualityOptions {
|
||||
size: self.bodies.get(body).copied().or(o.size),
|
||||
locals: self.locals.clone(),
|
||||
grading: if p.get("grading").is_some() { o.grading } else { self.growth },
|
||||
..o.clone()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn plate() -> (Session, std::path::PathBuf) {
|
||||
super::super::datum::tests::plate()
|
||||
}
|
||||
|
||||
/// The name of the plate's face that lies in the plane x = `x`.
|
||||
fn face_at_x(s: &Session, x: f64) -> String {
|
||||
let body = s.design.as_ref().unwrap().body("Body1").unwrap();
|
||||
let mesh = body.tessellate(0.1).unwrap();
|
||||
let faces = body.faces();
|
||||
(0..faces.len())
|
||||
.find(|&f| {
|
||||
let mut pts = mesh.triangles.iter().zip(&mesh.tri_face).filter(|(_, g)| **g as usize == f).flat_map(|(t, _)| t.iter());
|
||||
pts.clone().next().is_some() && pts.all(|&v| (mesh.positions[v as usize][0] - x).abs() < 1e-6)
|
||||
})
|
||||
.map(|f| faces[f].name.clone())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// Every element edge of the model's tet4 or hex8 elements, once: its two end points.
|
||||
fn edges(s: &Session) -> Vec<([f64; 3], [f64; 3])> {
|
||||
const TET: &[[usize; 2]] = &[[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]];
|
||||
const HEX: &[[usize; 2]] = &[[0, 1], [1, 2], [2, 3], [3, 0], [4, 5], [5, 6], [6, 7], [7, 4], [0, 4], [1, 5], [2, 6], [3, 7]];
|
||||
let mut seen = std::collections::BTreeSet::new();
|
||||
let mut out = Vec::new();
|
||||
for e in s.model.elements().values() {
|
||||
let pairs = if e.nodes.len() == 4 { TET } else { HEX };
|
||||
for [a, b] in pairs {
|
||||
let (a, b) = (e.nodes[*a], e.nodes[*b]);
|
||||
if seen.insert((a.min(b), a.max(b))) {
|
||||
out.push((s.model.nodes()[&a].xyz, s.model.nodes()[&b].xyz));
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn length((a, b): &([f64; 3], [f64; 3])) -> f64 {
|
||||
((a[0] - b[0]).powi(2) + (a[1] - b[1]).powi(2) + (a[2] - b[2]).powi(2)).sqrt()
|
||||
}
|
||||
|
||||
fn mean(v: &[f64]) -> f64 {
|
||||
assert!(!v.is_empty());
|
||||
v.iter().sum::<f64>() / v.len() as f64
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn controls_are_set_listed_cleared_saved_and_undone() {
|
||||
let (mut s, dir) = plate();
|
||||
let end = face_at_x(&s, 40.0);
|
||||
let r = s.execute("mesh.size.set", &json!({"name": "End", "size": 1.5, "faces": [{"body": "Body1", "face": end}]})).unwrap();
|
||||
assert_eq!(r["on"][0]["covers"], json!([end]), "{r}");
|
||||
assert_eq!(r["stale"], false);
|
||||
s.execute("mesh.size.set", &json!({"name": "Zone", "size": 2, "box": {"min": [0, 0, 0], "max": [5, 5, 10]}})).unwrap();
|
||||
s.execute("mesh.size.set", &json!({"name": "Zone", "size": 2, "sphere": {"center": [0, 0, 0], "radius": 3}})).unwrap();
|
||||
s.execute("mesh.size.set", &json!({"name": "Whole", "size": 4, "bodies": ["Body1"]})).unwrap();
|
||||
s.execute("mesh.size.growth", &json!({"rate": 0.5})).unwrap();
|
||||
let list = s.execute("mesh.size.list", &json!({})).unwrap();
|
||||
assert_eq!(list["controls"].as_object().unwrap().len(), 3, "{list}");
|
||||
assert_eq!(list["controls"]["Zone"]["region"]["sphere"]["radius"], 3.0);
|
||||
assert_eq!(list["growth"], 0.5);
|
||||
|
||||
let file = dir.join("sized.fieldcraft");
|
||||
s.execute("file.save", &json!({"path": file.to_str().unwrap()})).unwrap();
|
||||
let mut back = Session::default();
|
||||
back.execute("file.open", &json!({"path": file.to_str().unwrap()})).unwrap();
|
||||
assert_eq!(back.model.size_controls(), s.model.size_controls());
|
||||
assert_eq!(back.model.size_growth(), 0.5);
|
||||
|
||||
s.execute("mesh.size.clear", &json!({"names": ["Zone"]})).unwrap();
|
||||
assert_eq!(s.model.size_controls().len(), 2);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.model.size_controls().len(), 3);
|
||||
assert!(s.execute("mesh.size.clear", &json!({"names": ["Nope"]})).is_err());
|
||||
assert_eq!(s.execute("mesh.size.clear", &json!({})).unwrap()["cleared"], 3);
|
||||
assert!(s.execute("mesh.size.clear", &json!({})).is_err(), "nothing to clear");
|
||||
s.execute("mesh.size.growth", &json!({})).unwrap();
|
||||
assert_eq!(s.model.size_growth(), fieldcraft_model::sizes::DEFAULT_GROWTH);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_params_never_panic() {
|
||||
let (mut s, _) = plate();
|
||||
for p in [
|
||||
json!({}),
|
||||
json!({"name": "A"}),
|
||||
json!({"name": "A", "size": 1}),
|
||||
json!({"name": "A", "size": 0, "bodies": ["Body1"]}),
|
||||
json!({"name": "A", "size": -1, "bodies": ["Body1"]}),
|
||||
json!({"name": "A", "size": 1e300, "bodies": ["Body1"]}),
|
||||
json!({"name": "A", "size": "big", "bodies": ["Body1"]}),
|
||||
json!({"name": "A", "size": 1, "bodies": ["Nope"]}),
|
||||
json!({"name": "A", "size": 1, "bodies": []}),
|
||||
json!({"name": "A", "size": 1, "faces": [{"body": "Body1", "face": "Nope"}]}),
|
||||
json!({"name": "A", "size": 1, "faces": [{"body": "Body1", "edge": "E"}]}),
|
||||
json!({"name": "A", "size": 1, "bodies": ["Body1"], "box": {"min": [0, 0, 0], "max": [1, 1, 1]}}),
|
||||
json!({"name": "A", "size": 1, "box": {"min": [1, 0, 0], "max": [0, 1, 1]}}),
|
||||
json!({"name": "A", "size": 1, "box": {"min": [0, 0], "max": [1, 1, 1]}}),
|
||||
json!({"name": "A", "size": 1, "sphere": {"center": [0, 0, 0], "radius": 0}}),
|
||||
json!({"name": "A", "size": 1, "sphere": {"center": [0, 0, 0], "radius": 1, "x": 1}}),
|
||||
json!({"name": "1 bad", "size": 1, "bodies": ["Body1"]}),
|
||||
] {
|
||||
assert!(s.execute("mesh.size.set", &p).is_err(), "{p}");
|
||||
}
|
||||
for p in [json!({"rate": 0}), json!({"rate": 5}), json!({"rate": "x"})] {
|
||||
assert!(s.execute("mesh.size.growth", &p).is_err(), "{p}");
|
||||
}
|
||||
assert!(s.execute("mesh.size.clear", &json!({"names": []})).is_err());
|
||||
assert!(s.model.size_controls().is_empty());
|
||||
}
|
||||
|
||||
/// A size box halves the tetrahedra inside it; a face control gives its face the size it
|
||||
/// asks for, and the elements grow away from it no faster than the growth rate allows.
|
||||
#[test]
|
||||
fn tet_meshes_follow_size_controls() {
|
||||
let (mut s, _) = plate();
|
||||
s.execute("mesh.tet.generate", &json!({"body": "Body1", "size": 6, "tolerance": 0.2})).unwrap();
|
||||
let inside = |e: &&([f64; 3], [f64; 3])| [e.0, e.1].iter().all(|p| p[0] >= 30.0 && p[1] >= 0.0 && p[1] <= 10.0);
|
||||
let before = mean(&edges(&s).iter().filter(inside).map(length).collect::<Vec<_>>());
|
||||
s.execute("mesh.size.set", &json!({"name": "Zone", "size": 3, "box": {"min": [30, -1, -1], "max": [41, 10, 11]}})).unwrap();
|
||||
s.execute("mesh.tet.generate", &json!({"body": "Body1", "size": 6, "tolerance": 0.2})).unwrap();
|
||||
let after = mean(&edges(&s).iter().filter(inside).map(length).collect::<Vec<_>>());
|
||||
assert!((after / before - 0.5).abs() < 0.15, "box: {before} -> {after}");
|
||||
|
||||
s.execute("mesh.size.clear", &json!({})).unwrap();
|
||||
let end = face_at_x(&s, 40.0);
|
||||
s.execute("mesh.size.set", &json!({"name": "End", "size": 1.5, "faces": [{"body": "Body1", "face": end}]})).unwrap();
|
||||
s.execute("mesh.size.growth", &json!({"rate": 0.5})).unwrap();
|
||||
s.execute("mesh.tet.generate", &json!({"body": "Body1", "size": 6, "tolerance": 0.2})).unwrap();
|
||||
let all = edges(&s);
|
||||
let on_face: Vec<f64> = all.iter().filter(|e| (e.0[0] - 40.0).abs() < 1e-6 && (e.1[0] - 40.0).abs() < 1e-6).map(length).collect();
|
||||
let m = mean(&on_face);
|
||||
assert!((1.2..1.95).contains(&m), "on the face: {m}");
|
||||
// Slabs away from the face: the mean edge stays near 1.5 + 0.5 d (and below 6).
|
||||
for d in [3.0, 6.0, 9.0] {
|
||||
let slab: Vec<f64> = all.iter().filter(|e| (40.0 - 0.5 * (e.0[0] + e.1[0]) - d).abs() < 1.0).map(length).collect();
|
||||
let want = (1.5f64 + 0.5 * d).min(6.0);
|
||||
let m = mean(&slab);
|
||||
assert!(m < want * 1.3 && m > want * 0.5, "{d} mm from the face: mean edge {m}, field {want}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A face control sets the hex divisions of the face's edges: the swept plate's end face gets
|
||||
/// elements of the size asked for.
|
||||
#[test]
|
||||
fn hex_meshes_follow_size_controls() {
|
||||
let (mut s, _) = plate();
|
||||
let end = face_at_x(&s, 40.0);
|
||||
s.execute("mesh.hex.generate", &json!({"body": "Body1", "size": 5})).unwrap();
|
||||
let on_end = |s: &Session| -> Vec<f64> {
|
||||
edges(s).iter().filter(|e| (e.0[0] - 40.0).abs() < 1e-6 && (e.1[0] - 40.0).abs() < 1e-6).map(length).collect()
|
||||
};
|
||||
let coarse = on_end(&s).into_iter().fold(0.0, f64::max);
|
||||
assert!(coarse > 3.0, "{coarse}");
|
||||
s.execute("mesh.size.set", &json!({"name": "End", "size": 1.25, "faces": [{"body": "Body1", "face": end}]})).unwrap();
|
||||
s.execute("mesh.hex.generate", &json!({"body": "Body1", "size": 5})).unwrap();
|
||||
let fine = on_end(&s);
|
||||
assert!(fine.iter().all(|&l| l < 1.25 * 1.3), "{fine:?}");
|
||||
assert!(mean(&fine) > 1.25 * 0.7, "{fine:?}");
|
||||
}
|
||||
|
||||
/// The octree's cells on a face with a size control reach the level its size needs.
|
||||
#[test]
|
||||
fn octree_meshes_follow_size_controls() {
|
||||
let (mut s, _) = plate();
|
||||
let end = face_at_x(&s, 40.0);
|
||||
s.execute("mesh.size.set", &json!({"name": "End", "size": 2, "faces": [{"body": "Body1", "face": end}]})).unwrap();
|
||||
s.execute("mesh.hex.octree", &json!({"body": "Body1", "size": 6})).unwrap();
|
||||
let on_end: Vec<f64> = edges(&s).iter().filter(|e| (e.0[0] - 40.0).abs() < 1e-3 && (e.1[0] - 40.0).abs() < 1e-3).map(length).collect();
|
||||
assert!(!on_end.is_empty() && mean(&on_end) < 2.0 * 1.3, "{on_end:?}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
//! Stitching imported surfaces (`geometry.stitch`): their gaps under a tolerance closed for
|
||||
//! meshing ([`fieldcraft_geometry::stitch`]).
|
||||
//!
|
||||
//! A stitch is part of the model (`fieldcraft_model::virtual_faces::Stitch`), naming the surface
|
||||
//! by name. The surfaces keep what was read ([`TaggedSurface::original`]); [`restitch`] stitches
|
||||
//! each again from it by the model's stitches, in order, whenever a surface is imported or the
|
||||
//! model is opened, and after every stitch or delete, so the meshers always take the stitched
|
||||
//! surface. Undo restores the surfaces with the model.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use fieldcraft_geometry::tagged_mesh::TaggedSurface;
|
||||
use fieldcraft_model::Stitch;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::requirement::Requirement;
|
||||
use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
|
||||
pub const STITCH: CommandSpec = CommandSpec {
|
||||
id: "geometry.stitch",
|
||||
label: "Stitch Surface",
|
||||
params: r#"{"name": "Gaps", "surface": "Mesh1" (an imported surface, by name), "tolerance": 0.05 (mm)} — closes the gaps of an imported surface under the tolerance, for meshing: points on open edges closer than it become one point (triangles that collapse go), and a point on an open edge within it of the inside of another open edge splits that edge's triangle (a T-junction). Edges that are not open are never touched, so a closed surface is unchanged; triangles keep their face tags. Kept in the model and applied again, in order, whenever the surface is imported or the model opened (the file is not changed). Replies the open edges before and after (`open_before`, `open_after`: 0 means closed), the points `merged`, the triangles `split` and `removed`; the surface loses its mesh (`replaced`). A surface the session lacks is an error. One undo step"#,
|
||||
form: Some(super::forms::STITCH),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Geometry],
|
||||
run: stitch,
|
||||
};
|
||||
|
||||
pub const STITCH_DELETE: CommandSpec = CommandSpec {
|
||||
id: "geometry.stitch.delete",
|
||||
label: "Delete Stitch",
|
||||
params: r#"{"name": "Gaps"} — removes a stitch: the surface is stitched again by the ones left (as read when none); it loses its mesh (`replaced`)"#,
|
||||
form: Some(super::forms::STITCH_DELETE),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Stitch],
|
||||
run: delete,
|
||||
};
|
||||
|
||||
/// The name a surface goes by (and its mesh is tagged with): its own, or `Surface<index>`.
|
||||
pub(crate) fn surface_name(index: usize, t: &TaggedSurface) -> String {
|
||||
if t.name.trim().is_empty() { format!("Surface{index}") } else { t.name.clone() }
|
||||
}
|
||||
|
||||
/// Stitch the session's surfaces again from what was read, by the model's stitches. Replies each
|
||||
/// stitch's report by name, and the stitches whose surface is not there or that failed (with why).
|
||||
pub(crate) fn restitch(s: &mut Session) -> (Vec<(String, Value)>, Vec<(String, String)>) {
|
||||
let mut reports = Vec::new();
|
||||
let mut failed = Vec::new();
|
||||
let stitches = s.model.stitches().to_vec();
|
||||
for st in stitches.iter().filter(|st| !s.surfaces.iter().enumerate().any(|(i, t)| surface_name(i, t) == st.surface)) {
|
||||
failed.push((st.name.clone(), format!("no surface `{}`", st.surface)));
|
||||
}
|
||||
for (i, slot) in s.surfaces.iter_mut().enumerate() {
|
||||
let name = surface_name(i, slot);
|
||||
let mine: Vec<&Stitch> = stitches.iter().filter(|st| st.surface == name).collect();
|
||||
if mine.is_empty() && slot.original.is_none() {
|
||||
continue;
|
||||
}
|
||||
let raw = slot.original.clone().unwrap_or_else(|| Arc::new(slot.mesh.clone()));
|
||||
let mut mesh = raw.as_ref().clone();
|
||||
for st in mine {
|
||||
match fieldcraft_geometry::stitch::stitch(&mesh, st.tolerance) {
|
||||
Ok((m, r)) => {
|
||||
mesh = m;
|
||||
let report =
|
||||
json!({"open_before": r.open_before, "open_after": r.open_after, "merged": r.merged, "split": r.split, "removed": r.removed});
|
||||
reports.push((st.name.clone(), report));
|
||||
}
|
||||
Err(e) => failed.push((st.name.clone(), e.to_string())),
|
||||
}
|
||||
}
|
||||
let original = (!s.model.stitches().iter().all(|st| st.surface != name)).then_some(raw);
|
||||
*slot = Arc::new(TaggedSurface { name: slot.name.clone(), mesh, faces: slot.faces, original });
|
||||
}
|
||||
(reports, failed)
|
||||
}
|
||||
|
||||
fn stitch(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = super::name(p)?.to_string();
|
||||
let surface: String = super::req(p, "surface")?;
|
||||
let tolerance: f64 = super::req(p, "tolerance")?;
|
||||
s.model.add_stitch(Stitch { name: name.clone(), surface: surface.clone(), tolerance })?;
|
||||
let (reports, failed) = restitch(s);
|
||||
if let Some((_, why)) = failed.iter().find(|(n, _)| *n == name) {
|
||||
return Err(EngineError::Params(format!("stitch `{name}`: {why}")));
|
||||
}
|
||||
let mut reply = reports.into_iter().find(|(n, _)| *n == name).map(|(_, r)| r).unwrap_or_else(|| json!({}));
|
||||
if let Some(o) = reply.as_object_mut() {
|
||||
o.insert("name".into(), json!(name));
|
||||
o.insert("surface".into(), json!(surface));
|
||||
}
|
||||
let cleared = super::replace::clear(&mut s.model, [surface.as_str()]);
|
||||
Ok(cleared.report_if_any(s, reply))
|
||||
}
|
||||
|
||||
fn delete(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = super::name(p)?;
|
||||
let removed = s.model.remove_stitch(name)?;
|
||||
restitch(s);
|
||||
let cleared = super::replace::clear(&mut s.model, [removed.surface.as_str()]);
|
||||
Ok(cleared.report_if_any(s, json!({"deleted": name, "surface": removed.surface})))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// A unit cube in a 3MF whose top face is raised 0.01 mm: open along the top's rim.
|
||||
fn gapped_cube(dir: &std::path::Path) -> String {
|
||||
let m = fieldcraft_geometry::stitch::test_cube(0.01, false);
|
||||
let vertices: String = m.positions.iter().map(|p| format!(r#"<vertex x="{}" y="{}" z="{}"/>"#, p[0], p[1], p[2])).collect();
|
||||
let triangles: String = m
|
||||
.triangles
|
||||
.iter()
|
||||
.zip(&m.tri_face)
|
||||
.map(|(t, f)| format!(r#"<triangle v1="{}" v2="{}" v3="{}" ft:face="{f}"/>"#, t[0], t[1], t[2]))
|
||||
.collect();
|
||||
let model = format!(
|
||||
r#"<model unit="millimeter" xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02" xmlns:ft="{}"><resources><object id="1" name="Cube" ft:faces="6"><mesh><vertices>{vertices}</vertices><triangles>{triangles}</triangles></mesh></object></resources><build><item objectid="1"/></build></model>"#,
|
||||
fieldcraft_geometry::tagged_mesh::FACE_TAGS_NS
|
||||
);
|
||||
let path = dir.join("cube.3mf");
|
||||
std::fs::write(&path, super::super::geometry::tests::package(&model)).unwrap();
|
||||
path.to_str().unwrap().to_string()
|
||||
}
|
||||
|
||||
/// The gapped cube does not mesh; stitched under a tolerance over the gap it does; the stitch
|
||||
/// is saved and applied again when the model is opened; undo and delete bring the gap back.
|
||||
#[test]
|
||||
fn a_stitched_surface_meshes_and_the_stitch_is_replayed() {
|
||||
let dir = std::env::temp_dir().join(format!("fieldcraft-stitch-{}-{:?}", std::process::id(), std::thread::current().id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let mut s = Session::default();
|
||||
s.execute("geometry.import.mesh", &json!({"path": gapped_cube(&dir)})).unwrap();
|
||||
let tet = json!({"surface": 0, "size": 0.25});
|
||||
assert!(s.execute("mesh.tet.generate", &tet).is_err(), "open: no volume to fill");
|
||||
assert!(
|
||||
s.execute("geometry.stitch", &json!({"name": "Small", "surface": "Cube", "tolerance": 0.001})).unwrap()["open_after"].as_u64() > Some(0)
|
||||
);
|
||||
let r = s.execute("geometry.stitch", &json!({"name": "Gaps", "surface": "Cube", "tolerance": 0.05})).unwrap();
|
||||
assert_eq!((r["open_after"].as_u64(), r["merged"].as_u64()), (Some(0), Some(4)), "{r}");
|
||||
s.execute("mesh.tet.generate", &tet).unwrap();
|
||||
assert!(!s.model.elements().is_empty());
|
||||
let file = dir.join("stitched.fieldcraft");
|
||||
s.execute("file.save", &json!({"path": file.to_str().unwrap()})).unwrap();
|
||||
let mut t = Session::default();
|
||||
t.execute("file.open", &json!({"path": file.to_str().unwrap()})).unwrap();
|
||||
assert_eq!(t.surfaces[0].mesh, s.surfaces[0].mesh);
|
||||
assert!(t.surfaces[0].original.is_some());
|
||||
// Undo the mesh and the stitch: the gap is back.
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(s.execute("mesh.tet.generate", &tet).is_err());
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
s.execute("mesh.tet.generate", &tet).unwrap();
|
||||
let r = s.execute("geometry.stitch.delete", &json!({"name": "Gaps"})).unwrap();
|
||||
assert!(r["replaced"].is_array(), "{r}");
|
||||
assert!(s.execute("mesh.tet.generate", &tet).is_err());
|
||||
std::fs::remove_dir_all(dir).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_stitches_change_nothing() {
|
||||
let dir = std::env::temp_dir().join(format!("fieldcraft-stitch-bad-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let mut s = Session::default();
|
||||
s.execute("geometry.import.mesh", &json!({"path": gapped_cube(&dir)})).unwrap();
|
||||
let journal = s.journal.len();
|
||||
for p in [
|
||||
json!({}),
|
||||
json!({"name": "A", "surface": "Cube"}),
|
||||
json!({"name": "A", "surface": "Nope", "tolerance": 0.1}),
|
||||
json!({"name": "A", "surface": "Cube", "tolerance": 0}),
|
||||
json!({"name": "A", "surface": "Cube", "tolerance": "x"}),
|
||||
json!({"name": "1A", "surface": "Cube", "tolerance": 0.1}),
|
||||
] {
|
||||
assert!(s.execute("geometry.stitch", &p).is_err(), "{p}");
|
||||
}
|
||||
assert!(s.execute("geometry.stitch.delete", &json!({"name": "A"})).is_err());
|
||||
assert_eq!(s.journal.len(), journal);
|
||||
assert!(s.model.stitches().is_empty() && s.surfaces[0].original.is_none());
|
||||
std::fs::remove_dir_all(dir).ok();
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,8 @@
|
||||
//! symmetric part can be solved as a half.
|
||||
//!
|
||||
//! The condition holds the displacement normal to the plane, and the rotations about the two
|
||||
//! axes in it, at zero (`type: "symmetry"`). Its set is given, or made of the faces of the open
|
||||
//! axes in it, at zero (`type: "symmetry"`): on a plane normal to X, Y or Z those are global
|
||||
//! degrees of freedom; on any other plane they are linear constraints along its normal. Its set is given, or made of the faces of the open
|
||||
//! design's bodies that lie on the plane (the cut faces of a partition by that datum, `partition:
|
||||
//! <name>:a|b`), so it follows the geometry through a remesh.
|
||||
|
||||
@@ -16,7 +17,7 @@ use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
pub const SYMMETRY: CommandSpec = CommandSpec {
|
||||
id: "model.boundary_condition.symmetry",
|
||||
label: "Symmetry Plane",
|
||||
params: r#"{"name": "Sym", "datum": "Mid" (a datum plane normal to X, Y or Z), "set"?: "cut" (default: a new set of the same name holding every face of the open design's bodies on the plane, e.g. a partition's cut faces), "bodies"?: ["Part/a"] (only their faces)} — a symmetry boundary condition on the plane: zero displacement normal to it and zero rotation about the axes in it, so half a symmetric part (loaded symmetrically, with half the load) is solved as a half. Add it to the steps (model.step.activate). Planes not normal to X, Y or Z are not supported yet"#,
|
||||
params: r#"{"name": "Sym", "datum": "Mid" (a datum plane, any orientation), "set"?: "cut" (default: a new set of the same name holding every face of the open design's bodies on the plane, e.g. a partition's cut faces), "bodies"?: ["Part/a"] (only their faces)} — a symmetry boundary condition on the plane: zero displacement normal to it and zero rotation about the axes in it, so half a symmetric part (loaded symmetrically, with half the load) is solved as a half. Add it to the steps (model.step.activate). Reports the plane as "x", "y", "z" or its unit normal"#,
|
||||
form: Some(super::forms::SYMMETRY),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::DatumPlane],
|
||||
@@ -30,10 +31,10 @@ fn symmetry(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = super::name(p)?.to_string();
|
||||
let datum: String = super::req(p, "datum")?;
|
||||
let (point, normal) = super::datum::datum_plane_of(&s.model, &datum)?;
|
||||
let axis = (0..3).find(|&k| (normal[k].abs() - 1.0).abs() < 1e-9).ok_or_else(|| {
|
||||
EngineError::Params(format!("not supported yet: symmetry about a plane not normal to X, Y or Z (datum `{datum}` has normal {normal:?})"))
|
||||
})?;
|
||||
let plane = [Symmetry::X, Symmetry::Y, Symmetry::Z][axis];
|
||||
let unusable = || EngineError::Params(format!("datum `{datum}` has no usable normal ({normal:?})"));
|
||||
let plane = Symmetry::from_normal(normal).ok_or_else(unusable)?;
|
||||
let n = plane.unit_normal().ok_or_else(unusable)?;
|
||||
let along = |x: [f64; 3]| (0..3).map(|k| x[k] * n[k]).sum::<f64>();
|
||||
let set = match p.get("set") {
|
||||
Some(_) => super::req::<String>(p, "set")?,
|
||||
None => {
|
||||
@@ -48,8 +49,9 @@ fn symmetry(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let tol = ON_PLANE * b.size()?.max(1.0);
|
||||
for (f, surface) in b.faces().iter().zip(b.face_surfaces()?) {
|
||||
if let Some(FaceSurface::Plane { normal: m, point: q }) = surface {
|
||||
let parallel = (m[axis].abs() - 1.0).abs() < 1e-9;
|
||||
if parallel && (q[axis] - point[axis]).abs() < tol {
|
||||
let length = m.iter().map(|c| c * c).sum::<f64>().sqrt();
|
||||
let parallel = (along(m).abs() - length).abs() < 1e-9 * length;
|
||||
if parallel && (along(q) - along(point)).abs() < tol {
|
||||
faces.push(GeometryRef::face(b.name.as_str(), f.name.as_str())?);
|
||||
}
|
||||
}
|
||||
@@ -64,8 +66,11 @@ fn symmetry(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
};
|
||||
s.model.add_boundary_condition(&name, BoundaryCondition::Symmetry { target: Target::Set(set.clone()), plane })?;
|
||||
let faces = s.model.set(&set).map_or(0, |x| x.geometry.len());
|
||||
let axis_name = ["x", "y", "z"].get(axis).copied().unwrap_or("x");
|
||||
Ok(json!({"name": name, "set": set, "plane": axis_name, "faces": faces}))
|
||||
let reported = match plane.axis() {
|
||||
Some(k) => json!(["x", "y", "z"].get(k).copied().unwrap_or("x")),
|
||||
None => json!(n),
|
||||
};
|
||||
Ok(json!({"name": name, "set": set, "plane": reported, "faces": faces}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -137,17 +142,117 @@ mod tests {
|
||||
assert!((2.0 * halved.1 - full.1).abs() < 1e-9 * full.1.abs(), "reaction {} vs {}", halved.1, full.1);
|
||||
}
|
||||
|
||||
/// A 20 × 20 × 2 plate (0..20 in x and y), cut on its diagonal plane x = y (datum `Diag`,
|
||||
/// normal (1, −1, 0)) into `Plate/a` and `Plate/b`, the half below the diagonal (y < x) meshed
|
||||
/// with quadratic tets.
|
||||
fn plate() -> (crate::tests::TempDir, Session, String) {
|
||||
let commands = json!({"commands": [
|
||||
{"command": "solid.box", "params": {"length": 20, "width": 20, "height": 2, "body_name": "Plate"}},
|
||||
]});
|
||||
let bytes = fieldcraft_geometry::script_design(&commands.to_string()).unwrap();
|
||||
let (dir, path) = crate::tests::design_file(&bytes);
|
||||
let mut s = Session::default();
|
||||
s.execute("geometry.open", &json!({"path": path})).unwrap();
|
||||
s.execute("geometry.datum", &json!({"name": "Diag", "by": {"type": "point_normal", "point": [0, 0, 0], "normal": [1, -1, 0]}})).unwrap();
|
||||
s.execute("geometry.partition", &json!({"name": "Cut", "datum": "Diag"})).unwrap();
|
||||
let design = s.design.as_ref().unwrap();
|
||||
let below = design
|
||||
.bodies
|
||||
.iter()
|
||||
.find(|b| {
|
||||
let c = b.measure().unwrap().centroid;
|
||||
c[0] > c[1]
|
||||
})
|
||||
.unwrap()
|
||||
.name
|
||||
.clone();
|
||||
s.execute("mesh.tet.generate", &json!({"body": below, "size": 2, "order": "quadratic"})).unwrap();
|
||||
(dir, s, below)
|
||||
}
|
||||
|
||||
/// Clamp the faces at y = 0 (and x = 0 when `whole`), press the top faces, solve; the
|
||||
/// displacement of every node of the half below the diagonal, by label.
|
||||
fn press(s: &mut Session, below: &str, whole: bool, extra: &[&str]) -> std::collections::BTreeMap<u64, [f64; 3]> {
|
||||
let bodies: Vec<String> = s.design.as_ref().unwrap().bodies.iter().map(|b| b.name.clone()).collect();
|
||||
let mut clamp = vec![json!({"body": below, "face": face(s, below, [10.0, 0.0, 1.0])})];
|
||||
let mut top = vec![json!({"body": below, "face": face(s, below, [15.0, 5.0, 2.0])})];
|
||||
if whole {
|
||||
let above = bodies.iter().find(|b| b.as_str() != below).unwrap();
|
||||
clamp.push(json!({"body": above, "face": face(s, above, [0.0, 10.0, 1.0])}));
|
||||
top.push(json!({"body": above, "face": face(s, above, [5.0, 15.0, 2.0])}));
|
||||
}
|
||||
let all: Vec<Value> = bodies.iter().filter(|b| whole || b.as_str() == below).map(|b| json!({"body": b})).collect();
|
||||
let mut bcs = vec!["Clamp".to_string()];
|
||||
bcs.extend(extra.iter().map(|x| x.to_string()));
|
||||
let script = json!([
|
||||
{"command": "model.set.create", "params": {"name": "all", "geometry": all}},
|
||||
{"command": "model.set.create", "params": {"name": "clamped", "geometry": clamp}},
|
||||
{"command": "model.set.create", "params": {"name": "top", "geometry": top}},
|
||||
{"command": "model.set.create", "params": {"name": "half", "geometry": [{"body": below}]}},
|
||||
{"command": "model.material.create", "params": {"name": "Steel", "elastic": {"type": "isotropic", "young": 210000, "poisson": 0.3}}},
|
||||
{"command": "model.section.create", "params": {"name": "Plate", "set": "all", "material": "Steel", "kind": {"type": "solid"}}},
|
||||
{"command": "model.boundary_condition.create", "params": {"name": "Clamp", "type": "fixed", "target": {"set": "clamped"}}},
|
||||
{"command": "model.load.create", "params": {"name": "Press", "type": "pressure", "set": "top", "magnitude": 0.1}},
|
||||
{"command": "model.step.create", "params": {"name": "Load", "procedure": {"type": "static"}, "boundary_conditions": bcs, "loads": ["Press"]}},
|
||||
]);
|
||||
s.run_script(&script).unwrap();
|
||||
s.execute("solve.static", &json!({})).unwrap();
|
||||
let nodes = s.model.resolve_set("half").unwrap().nodes;
|
||||
let probe = s.execute("solve.probe", &json!({"step": "Load", "nodes": nodes})).unwrap();
|
||||
probe["nodes"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|n| (n["node"].as_u64().unwrap(), [0, 1, 2].map(|k| n["displacement"][k].as_f64().unwrap())))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The half of the plate below its diagonal, with the inclined symmetry plane (found on the
|
||||
/// cut face) and its half of the pressure, deflects as the whole plate, meshed as that half
|
||||
/// mirrored across the diagonal (conforming there, so the whole is the same mesh twice).
|
||||
#[test]
|
||||
fn inclined_planes_and_hostile_params_change_nothing() {
|
||||
fn a_half_cut_on_an_inclined_plane_matches_the_whole() {
|
||||
let (_d1, mut whole, below) = plate();
|
||||
let r = whole.execute("mesh.mirror", &json!({"plane": "Diag", "bodies": [below]})).unwrap();
|
||||
let on_plane = r["on_plane"].as_u64().unwrap();
|
||||
let q = whole.execute("mesh.quality", &json!({})).unwrap();
|
||||
assert_eq!(q["valid"].as_u64().unwrap() as usize, whole.model.elements().len(), "{q}");
|
||||
assert!((q["volume"].as_f64().unwrap() - 800.0).abs() < 1e-9 * 800.0, "{q}");
|
||||
let full = press(&mut whole, &below, true, &[]);
|
||||
|
||||
let (_d2, mut half, below) = plate();
|
||||
let nodes = half.model.nodes().len() as u64;
|
||||
assert_eq!(whole.model.nodes().len() as u64, 2 * nodes - on_plane, "the mirror shares the plane's nodes");
|
||||
let r = half.execute("model.boundary_condition.symmetry", &json!({"name": "Sym", "datum": "Diag", "bodies": [below]})).unwrap();
|
||||
let h = 1.0 / 2f64.sqrt();
|
||||
assert_eq!(r["faces"], json!(1), "{r}");
|
||||
let n: Vec<f64> = r["plane"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap()).collect();
|
||||
assert!((n[0] - h).abs() < 1e-15 && (n[1] + h).abs() < 1e-15 && n[2] == 0.0, "{r}");
|
||||
assert_eq!(half.model.resolve_set("Sym").unwrap().nodes.len() as u64, on_plane);
|
||||
let halved = press(&mut half, &below, false, &["Sym"]);
|
||||
|
||||
assert_eq!(full.len(), halved.len());
|
||||
let size = full.values().fold(0.0f64, |m, u| m.max(u.iter().map(|c| c * c).sum::<f64>().sqrt()));
|
||||
let worst = halved.iter().map(|(l, u)| (0..3).map(|k| (u[k] - full[l][k]).powi(2)).sum::<f64>().sqrt()).fold(0.0f64, f64::max);
|
||||
eprintln!("half vs whole plate on the inclined plane: largest deflection {size:.6e}, difference {:.1e}", worst / size);
|
||||
assert!(size > 1e-4 && worst < 1e-9 * size, "{worst} of {size}");
|
||||
// On the plane the nodes move along it: n · u = 0.
|
||||
for l in half.model.resolve_set("Sym").unwrap().nodes {
|
||||
let u = halved[&u64::from(l)];
|
||||
assert!((u[0] - u[1]).abs() * h < 1e-12 * size, "node {l}: {u:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_params_change_nothing() {
|
||||
let (_d, mut s) = bar(true);
|
||||
s.execute("geometry.datum", &json!({"name": "Tilt", "by": {"type": "point_normal", "point": [0, 0, 0], "normal": [1, 1, 0]}})).unwrap();
|
||||
s.execute("geometry.datum", &json!({"name": "Out", "by": {"type": "offset", "plane": "xy", "offset": 100}})).unwrap();
|
||||
s.execute("geometry.datum", &json!({"name": "Tilt", "by": {"type": "point_normal", "point": [100, 0, 0], "normal": [1, 1, 0]}})).unwrap();
|
||||
s.execute("geometry.datum.axis", &json!({"name": "Ax", "by": {"type": "principal", "axis": "x"}})).unwrap();
|
||||
let model = s.model.clone();
|
||||
let e = s.execute("model.boundary_condition.symmetry", &json!({"name": "S", "datum": "Tilt"})).unwrap_err();
|
||||
assert!(e.to_string().contains("not supported yet"), "{e}");
|
||||
for p in [
|
||||
json!({"name": "S", "datum": "Out"}),
|
||||
json!({"name": "S", "datum": "Tilt"}),
|
||||
json!({"name": "S", "datum": "Ax"}),
|
||||
json!({"name": "S", "datum": "Nope"}),
|
||||
json!({"name": "S", "datum": "Mid", "set": "nope"}),
|
||||
|
||||
@@ -0,0 +1,623 @@
|
||||
//! Virtual topology: faces merged for meshing only (`geometry.faces.merge`), and tiny or sliver
|
||||
//! faces absorbed into a neighbour (`geometry.face.absorb`, a merge that keeps the neighbour's
|
||||
//! name).
|
||||
//!
|
||||
//! A face merge is part of the model (`fieldcraft_model::virtual_faces`); the geometry layer
|
||||
//! applies it to the open design's bodies (`fieldcraft_geometry::virtual_faces`), after the
|
||||
//! partitions, whenever the design is opened or cut again ([`super::datum::partitioned`]), so the
|
||||
//! meshers, the viewport and the checks all see the merged face. The B-rep is not changed.
|
||||
|
||||
use fieldcraft_geometry::virtual_faces::merged_face_name;
|
||||
use fieldcraft_model::FaceMerge;
|
||||
use fieldcraft_model::geometry::{Entity, GeometryRef};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::requirement::Requirement;
|
||||
use crate::{CommandKind, CommandSpec, EngineError, Result, Session};
|
||||
|
||||
pub const FACES_MERGE: CommandSpec = CommandSpec {
|
||||
id: "geometry.faces.merge",
|
||||
label: "Merge Faces",
|
||||
params: r#"{"name": "Fillet", "faces": [{"body": "Base", "face": "F3:blend#0"}, {"body": "Base", "face": "F3:blend#1"}]} — makes two or more adjacent faces of one body one face for meshing only (virtual topology): the meshers, the checks and the viewport take them as one face `merge:<name>`, without the edges between them (an edge they leave in pieces along a neighbour is one edge), on the surface they share (none when they lie on different surfaces: the projection mesher and node relaxation keep to each B-rep piece's own surface, the other meshers to the tessellation). The design's B-rep is not changed. A face name also selects the pieces a partition cut it into, and may be a face an earlier merge made. The model keeps the merge and applies it again, by persistent face names, whenever the design is opened (after the partitions: on a body they cut, in each cell where its faces, or their pieces, touch); one that no longer applies (a face gone, faces that no longer touch) is listed with the design's errors. The body loses its mesh (`replaced`; remesh it); sets naming the merged faces no longer resolve (`stale_sets`: name `merge:<name>` instead). Faces on several bodies, faces that do not touch along an edge, or a face the body lacks are an error. One undo step"#,
|
||||
form: Some(super::forms::FACES_MERGE),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Design],
|
||||
run: merge,
|
||||
};
|
||||
|
||||
pub const FACES_MERGE_DELETE: CommandSpec = CommandSpec {
|
||||
id: "geometry.faces.merge.delete",
|
||||
label: "Delete Face Merge",
|
||||
params: r#"{"name": "Fillet"} — removes a face merge: its faces are separate faces again (a later merge that names `merge:<name>` no longer applies and is listed with the design's errors); the body loses its mesh (`replaced`)"#,
|
||||
form: Some(super::forms::FACES_MERGE_DELETE),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::FaceMerge],
|
||||
run: delete,
|
||||
};
|
||||
|
||||
pub const FACE_ABSORB: CommandSpec = CommandSpec {
|
||||
id: "geometry.face.absorb",
|
||||
label: "Absorb Faces",
|
||||
params: r#"{"name": "Sliver", "face": {"body": "Base", "face": "F4:side#2"}, "into"?: "F1:+z" (default: the neighbour it shares the longest edges with)} | {"name"?: "Absorb", "from_check": true} — absorbs a tiny or sliver face into a neighbour for meshing only (virtual topology, as geometry.faces.merge): the meshers, the checks and the viewport take the two as one face that keeps the neighbour's name, so sets on the neighbour still select it and the tiny face no longer drives the mesh. The B-rep is not changed. `from_check`: every small and sliver face the last geometry.check found (and marked) on the open design, each into its best neighbour, as `<name>-1`, `<name>-2`, … in the check's order (a face already absorbed by an earlier one is `skipped`). Kept in the model and applied again by persistent names whenever the design is opened (delete one with geometry.faces.merge.delete); the bodies lose their meshes (`replaced`); sets naming an absorbed face are `stale_sets`. Replies `absorbed` ([{name, body, face, into}]) and `skipped`. One undo step"#,
|
||||
form: Some(super::forms::FACE_ABSORB),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Design],
|
||||
run: absorb,
|
||||
};
|
||||
|
||||
pub const FACE_SPLIT: CommandSpec = CommandSpec {
|
||||
id: "geometry.face.split",
|
||||
label: "Split Face",
|
||||
params: r#"{"name": "Band", "face": {"body": "Base", "face": "F1:+z"}, "datum": "Mid" (a datum plane)} — splits a face along the curve where a datum plane crosses it, for meshing only (virtual topology; partitions cut whole bodies): the meshers, the checks and the viewport take it as two faces on its surface, `split:<name>:a` on the plane's negative side and `split:<name>:b` on the other, with the cut as an edge between them (a row of nodes along it; seed it like any edge), and the face's edges cut where the plane crosses them. The B-rep is not changed. Applied after the face merges, by persistent names, whenever the design is opened (a merge cannot name a split face); moving the datum moves the cut, and the datum cannot be deleted while a split uses it. The body loses its mesh (`replaced`); sets naming the face no longer resolve (`stale_sets`). A plane that does not cross the face is an error. Delete with geometry.face.split.delete. One undo step"#,
|
||||
form: Some(super::forms::FACE_SPLIT),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::Design, Requirement::DatumPlane],
|
||||
run: split,
|
||||
};
|
||||
|
||||
pub const FACE_SPLIT_DELETE: CommandSpec = CommandSpec {
|
||||
id: "geometry.face.split.delete",
|
||||
label: "Delete Face Split",
|
||||
params: r#"{"name": "Band"} — removes a face split: the face is one face again; the body loses its mesh (`replaced`)"#,
|
||||
form: Some(super::forms::FACE_SPLIT_DELETE),
|
||||
kind: CommandKind::Model,
|
||||
requires: &[Requirement::FaceSplit],
|
||||
run: split_delete,
|
||||
};
|
||||
|
||||
/// Most faces one merge lists.
|
||||
const MAX_FACES: usize = fieldcraft_model::virtual_faces::MAX_FACE_MERGE_FACES;
|
||||
|
||||
/// The body and the face names of `faces` (`[{body, face}]`, all on one body).
|
||||
fn faces_of(p: &Value) -> Result<(String, Vec<String>)> {
|
||||
let refs: Vec<GeometryRef> = super::req(p, "faces")?;
|
||||
if !(2..=MAX_FACES).contains(&refs.len()) {
|
||||
return Err(EngineError::invalid("faces", format!("2 to {MAX_FACES} faces of one body")));
|
||||
}
|
||||
let body = refs.first().map(|r| r.body.clone()).unwrap_or_default();
|
||||
let mut names = Vec::with_capacity(refs.len());
|
||||
for r in &refs {
|
||||
match &r.entity {
|
||||
Entity::Face(f) if r.body == body => names.push(f.clone()),
|
||||
Entity::Face(_) => return Err(EngineError::invalid("faces", "faces of one body: a merge makes one face of one body")),
|
||||
_ => {
|
||||
return Err(EngineError::invalid("faces", format!("`{}`: give {{\"body\", \"face\"}}", r.body.chars().take(80).collect::<String>())));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok((body, names))
|
||||
}
|
||||
|
||||
/// The open design's sets that name a face of `body` it no longer has.
|
||||
fn stale_sets(s: &Session, body: &str) -> Vec<String> {
|
||||
let Some(shape) = s.design.as_deref().and_then(|d| d.body(body)) else { return Vec::new() };
|
||||
let gone = |r: &GeometryRef| r.body == body && matches!(&r.entity, Entity::Face(f) if shape.faces_named(f).is_empty());
|
||||
s.model.sets().iter().filter(|(_, set)| set.geometry.iter().any(gone)).map(|(n, _)| n.clone()).collect()
|
||||
}
|
||||
|
||||
/// Apply the model's partitions, merges and splits to the design again, failing with the error of
|
||||
/// one of `features` (`merge <name>`, `split <name>`) when it has one; the meshes of `bodies` go
|
||||
/// (their faces changed).
|
||||
pub(super) fn reapply(s: &mut Session, features: &[String], bodies: &[String], reply: Value) -> Result<Value> {
|
||||
let mut cleared = super::datum::recut(s)?;
|
||||
let design = s.design.as_deref().ok_or_else(|| EngineError::Params("open a design first".into()))?;
|
||||
let errors: Vec<String> =
|
||||
design.errors.iter().filter(|e| features.contains(&e.feature)).map(|e| format!("{}: {}", e.feature, e.message)).collect();
|
||||
if !errors.is_empty() {
|
||||
return Err(EngineError::Params(errors.join("; ")));
|
||||
}
|
||||
cleared.extend(super::replace::clear(&mut s.model, bodies.iter().map(String::as_str)));
|
||||
let stale: Vec<String> = bodies.iter().flat_map(|b| stale_sets(s, b)).collect();
|
||||
let mut reply = cleared.report_if_any(s, reply);
|
||||
if !stale.is_empty()
|
||||
&& let Some(o) = reply.as_object_mut()
|
||||
{
|
||||
let mut all: Vec<String> = o.get("stale_sets").and_then(|v| serde_json::from_value(v.clone()).ok()).unwrap_or_default();
|
||||
all.extend(stale);
|
||||
all.sort();
|
||||
all.dedup();
|
||||
o.insert("stale_sets".into(), json!(all));
|
||||
}
|
||||
Ok(reply)
|
||||
}
|
||||
|
||||
fn merge(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = super::name(p)?.to_string();
|
||||
let (body, faces) = faces_of(p)?;
|
||||
if s.design.as_deref().and_then(|d| d.body(&body)).is_none() {
|
||||
return Err(EngineError::Params(format!("the open design has no body `{}`", body.chars().take(200).collect::<String>())));
|
||||
}
|
||||
s.model.add_face_merge(FaceMerge { name: name.clone(), body: body.clone(), faces: faces.clone(), into: None })?;
|
||||
let face = merged_face_name(&name);
|
||||
let reply = json!({"name": name, "body": body, "face": face});
|
||||
let mut reply = reapply(s, &[format!("merge {name}")], std::slice::from_ref(&body), reply)?;
|
||||
let shape = s.design.as_deref().and_then(|d| d.body(&body));
|
||||
let brep = shape.and_then(|b| b.face_index(&face)).and_then(|f| shape?.merged_faces().into_iter().find(|(g, _)| *g == f)).map_or(0, |(_, n)| n);
|
||||
if let Some(o) = reply.as_object_mut() {
|
||||
o.insert("brep_faces".into(), json!(brep));
|
||||
}
|
||||
Ok(reply)
|
||||
}
|
||||
|
||||
/// The faces to absorb, by body and name: the one named, or the small and sliver faces of the
|
||||
/// last geometry.check.
|
||||
fn to_absorb(s: &Session, p: &Value) -> Result<Vec<(String, String)>> {
|
||||
let from_check: bool = super::opt(p, "from_check")?.unwrap_or(false);
|
||||
match (p.get("face"), from_check) {
|
||||
(Some(_), false) => {
|
||||
let r: GeometryRef = super::req(p, "face")?;
|
||||
match r.entity {
|
||||
Entity::Face(f) => Ok(vec![(r.body, f)]),
|
||||
_ => Err(EngineError::invalid("face", "give {\"body\", \"face\"}")),
|
||||
}
|
||||
}
|
||||
(None, true) => {
|
||||
let marks = crate::view::geometry_marks(s)
|
||||
.ok_or_else(|| EngineError::Params("`from_check`: run geometry.check (with marks) on the open design first".into()))?;
|
||||
let mut faces: Vec<(String, String)> = Vec::new();
|
||||
for f in marks.marks.iter().filter_map(|m| m.small.clone()) {
|
||||
if !faces.contains(&f) {
|
||||
faces.push(f);
|
||||
}
|
||||
}
|
||||
if faces.is_empty() {
|
||||
return Err(EngineError::Params("`from_check`: the last geometry.check found no small or sliver face".into()));
|
||||
}
|
||||
Ok(faces)
|
||||
}
|
||||
_ => Err(EngineError::Params("give either `face` or `from_check: true`".into())),
|
||||
}
|
||||
}
|
||||
|
||||
fn absorb(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let faces = to_absorb(s, p)?;
|
||||
let one = faces.len() == 1 && p.get("face").is_some();
|
||||
let name = match p.get("name") {
|
||||
None if !one => "Absorb".to_string(),
|
||||
_ => super::name(p)?.to_string(),
|
||||
};
|
||||
let into: Option<String> = super::opt(p, "into")?;
|
||||
if into.is_some() && !one {
|
||||
return Err(EngineError::invalid("into", "goes with `face`"));
|
||||
}
|
||||
let design = s.design.as_deref().ok_or_else(|| EngineError::Params("open a design first".into()))?;
|
||||
// The bodies as each absorption leaves them, to find the next one's neighbour.
|
||||
let mut shapes: Vec<fieldcraft_geometry::Shape> = Vec::new();
|
||||
let (mut absorbed, mut skipped, mut merges) = (Vec::new(), Vec::new(), Vec::new());
|
||||
for (k, (body, face)) in faces.into_iter().enumerate() {
|
||||
if !shapes.iter().any(|b| b.name == body) {
|
||||
let shape = design
|
||||
.body(&body)
|
||||
.ok_or_else(|| EngineError::Params(format!("the open design has no body `{}`", body.chars().take(200).collect::<String>())))?;
|
||||
shapes.push(shape.clone());
|
||||
}
|
||||
let Some(shape) = shapes.iter_mut().find(|b| b.name == body) else { continue };
|
||||
let target = match &into {
|
||||
Some(i) => Ok(i.clone()),
|
||||
None => shape.absorbing_neighbour(&face),
|
||||
};
|
||||
let merged = target.map_err(EngineError::from).and_then(|t| Ok((shape.merge_faces(&t, &[face.clone(), t.clone()])?, t)));
|
||||
let (next, target) = match merged {
|
||||
Ok(m) => m,
|
||||
Err(e) if !one => {
|
||||
skipped.push(json!({"body": body, "face": face, "reason": e.to_string()}));
|
||||
continue;
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
*shape = next;
|
||||
let merge = if one { name.clone() } else { format!("{name}-{}", k + 1) };
|
||||
s.model.add_face_merge(FaceMerge {
|
||||
name: merge.clone(),
|
||||
body: body.clone(),
|
||||
faces: vec![face.clone(), target.clone()],
|
||||
into: Some(target.clone()),
|
||||
})?;
|
||||
absorbed.push(json!({"name": merge, "body": body, "face": face, "into": target}));
|
||||
merges.push(merge);
|
||||
}
|
||||
if merges.is_empty() {
|
||||
return Err(EngineError::Params(format!("no face could be absorbed: {}", json!(skipped))));
|
||||
}
|
||||
let bodies: Vec<String> = shapes.into_iter().map(|b| b.name).collect();
|
||||
let features: Vec<String> = merges.iter().map(|m| format!("merge {m}")).collect();
|
||||
reapply(s, &features, &bodies, json!({"absorbed": absorbed, "skipped": skipped}))
|
||||
}
|
||||
|
||||
fn split(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = super::name(p)?.to_string();
|
||||
let r: GeometryRef = super::req(p, "face")?;
|
||||
let Entity::Face(face) = r.entity else { return Err(EngineError::invalid("face", "give {\"body\", \"face\"}")) };
|
||||
let datum: String = super::req(p, "datum")?;
|
||||
super::datum::datum_plane_of(&s.model, &datum)?;
|
||||
s.model.add_face_split(fieldcraft_model::FaceSplit { name: name.clone(), body: r.body.clone(), face: face.clone(), datum })?;
|
||||
let feature = format!("split:{name}");
|
||||
let faces = [format!("{feature}:a"), format!("{feature}:b")];
|
||||
let reply = json!({"name": name, "body": r.body, "faces": faces});
|
||||
reapply(s, &[format!("split {name}")], std::slice::from_ref(&r.body), reply)
|
||||
}
|
||||
|
||||
fn split_delete(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = super::name(p)?;
|
||||
let removed = s.model.remove_face_split(name)?;
|
||||
let mut cleared = super::datum::recut(s)?;
|
||||
cleared.extend(super::replace::clear(&mut s.model, [removed.body.as_str()]));
|
||||
Ok(cleared.report_if_any(s, json!({"deleted": name, "body": removed.body})))
|
||||
}
|
||||
|
||||
fn delete(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let name = super::name(p)?;
|
||||
let removed = s.model.remove_face_merge(name)?;
|
||||
let mut cleared = super::datum::recut(s)?;
|
||||
cleared.extend(super::replace::clear(&mut s.model, [removed.body.as_str()]));
|
||||
Ok(cleared.report_if_any(s, json!({"deleted": name, "body": removed.body})))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use fieldcraft_geometry::FaceSurface;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
/// The plate's hole halves (two half-cylinders), by name.
|
||||
fn halves(s: &Session) -> Vec<String> {
|
||||
let b = &s.design.as_ref().unwrap().bodies[0];
|
||||
b.faces()
|
||||
.iter()
|
||||
.zip(b.face_surfaces().unwrap())
|
||||
.filter(|(_, x)| matches!(x, Some(FaceSurface::Cylinder { .. })))
|
||||
.map(|(f, _)| f.name.clone())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn merge_hole(s: &mut Session) -> serde_json::Value {
|
||||
let body = s.design.as_ref().unwrap().bodies[0].name.clone();
|
||||
let faces: Vec<_> = halves(s).into_iter().map(|f| json!({"body": body, "face": f})).collect();
|
||||
s.execute("geometry.faces.merge", &json!({"name": "Hole", "faces": faces})).unwrap()
|
||||
}
|
||||
|
||||
fn faces(s: &Session) -> Vec<String> {
|
||||
s.design.as_ref().unwrap().bodies[0].faces().iter().map(|f| f.name.clone()).collect()
|
||||
}
|
||||
|
||||
/// A merge makes the hole one face of the design; undo and redo take it out and put it back;
|
||||
/// it is saved with the model and applied again when the model and its design are opened.
|
||||
#[test]
|
||||
fn a_merge_is_one_face_undone_saved_and_replayed() {
|
||||
let (mut s, dir) = super::super::datum::tests::plate();
|
||||
let before = faces(&s);
|
||||
assert_eq!(before.len(), 8);
|
||||
let r = merge_hole(&mut s);
|
||||
assert_eq!((r["face"].as_str(), r["brep_faces"].as_u64()), (Some("merge:Hole"), Some(2)), "{r}");
|
||||
assert_eq!(faces(&s).len(), 7);
|
||||
assert!(faces(&s).contains(&"merge:Hole".to_string()));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(faces(&s), before);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!(faces(&s).len(), 7);
|
||||
// Saved, and replayed on open.
|
||||
let file = dir.join("merged.fieldcraft");
|
||||
s.execute("file.save", &json!({"path": file.to_str().unwrap()})).unwrap();
|
||||
let mut t = Session::default();
|
||||
t.execute("file.open", &json!({"path": file.to_str().unwrap()})).unwrap();
|
||||
assert_eq!(t.model.face_merges().len(), 1);
|
||||
assert_eq!(faces(&t), faces(&s));
|
||||
// Opening the design again (after an edit of it) replays the merge.
|
||||
let design = s.sources.design.as_ref().unwrap().path().to_string();
|
||||
s.execute("geometry.open", &json!({"path": design})).unwrap();
|
||||
assert_eq!(faces(&s).len(), 7);
|
||||
// Deleting it brings the halves back.
|
||||
s.execute("geometry.faces.merge.delete", &json!({"name": "Hole"})).unwrap();
|
||||
assert_eq!(faces(&s), before);
|
||||
std::fs::remove_dir_all(dir).ok();
|
||||
}
|
||||
|
||||
/// The mesh nodes on the seams between the plate's hole halves (the B-rep edges between them).
|
||||
fn nodes_on_seams(s: &Session, seams: &[Vec<[f64; 3]>]) -> usize {
|
||||
let on = |p: [f64; 3]| {
|
||||
seams.iter().any(|line| {
|
||||
line.windows(2).any(|w| {
|
||||
let (a, b) = (w[0], w[1]);
|
||||
let ab = [b[0] - a[0], b[1] - a[1], b[2] - a[2]];
|
||||
let ap = [p[0] - a[0], p[1] - a[1], p[2] - a[2]];
|
||||
let l2 = ab.iter().map(|x| x * x).sum::<f64>();
|
||||
let t = (ab.iter().zip(&ap).map(|(x, y)| x * y).sum::<f64>() / l2).clamp(0.0, 1.0);
|
||||
(0..3).map(|k| (ap[k] - t * ab[k]).powi(2)).sum::<f64>().sqrt() < 1e-6
|
||||
})
|
||||
})
|
||||
};
|
||||
s.model.nodes().values().filter(|n| on(n.xyz)).count()
|
||||
}
|
||||
|
||||
/// The sample plate made square (30 × 30 × 10, the Ø10 hole in the middle): no layout of the
|
||||
/// cross-section has a reason to cut along the seams.
|
||||
fn square_plate() -> (Session, std::path::PathBuf) {
|
||||
let dir = std::env::temp_dir().join(format!("fieldcraft-merge-{}-{:?}", std::process::id(), std::thread::current().id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("square.solidcraft");
|
||||
let plate = fieldcraft_geometry::sample::Plate { width: 30.0, ..Default::default() };
|
||||
std::fs::write(&path, fieldcraft_geometry::sample::plate_design(&plate).unwrap()).unwrap();
|
||||
let mut s = Session::default();
|
||||
s.execute("geometry.open", &json!({"path": path.to_str().unwrap()})).unwrap();
|
||||
(s, dir)
|
||||
}
|
||||
|
||||
/// A mesher takes the merged face as one: its boundary element faces are tagged with it, the
|
||||
/// halves are gone from the tags, and the seams between the halves (no longer edges) carry
|
||||
/// `expected(session, seams)` nodes.
|
||||
fn mesher_meshes_a_merged_face_as_one(command: &str, params: serde_json::Value, expected: impl Fn(&Session, &[Vec<[f64; 3]>]) -> usize) {
|
||||
let (mut s, dir) = square_plate();
|
||||
let hole = halves(&s);
|
||||
let b = &s.design.as_ref().unwrap().bodies[0];
|
||||
let seams: Vec<Vec<[f64; 3]>> = b
|
||||
.edges()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|e| e.faces.len() == 2 && e.faces[0] != e.faces[1] && e.faces.iter().all(|&f| hole.contains(&b.faces()[f as usize].name)))
|
||||
.map(|e| e.points)
|
||||
.collect();
|
||||
assert_eq!(seams.len(), 2);
|
||||
s.execute(command, ¶ms).unwrap();
|
||||
let before = nodes_on_seams(&s, &seams);
|
||||
assert!(before >= 6, "{command}: a node row on each seam before the merge: {before}");
|
||||
let r = merge_hole(&mut s);
|
||||
assert!(r["replaced"].as_array().is_some_and(|a| !a.is_empty()), "the mesh goes: {r}");
|
||||
assert!(s.model.elements().is_empty());
|
||||
s.execute(command, ¶ms).unwrap();
|
||||
// The face, edge and vertex names the elements and nodes are tagged with.
|
||||
let c = s.model.classification();
|
||||
let used = c.elements().values().flat_map(|t| t.faces.values().chain(t.edges.values())).chain(c.nodes().values());
|
||||
let tagged: BTreeSet<String> = used.filter_map(|&t| c.entity(t)?.entity.name().map(str::to_string)).collect();
|
||||
assert!(tagged.contains("merge:Hole"), "{tagged:?}");
|
||||
assert!(!tagged.iter().any(|n| hole.iter().any(|h| n.split(['|', '~']).any(|p| p == h))), "{tagged:?}");
|
||||
let after = nodes_on_seams(&s, &seams);
|
||||
let expected = expected(&s, &seams);
|
||||
assert_eq!(after, expected, "{command}: nodes on the seams after the merge (before {before})");
|
||||
std::fs::remove_dir_all(dir).ok();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_tet_mesher_meshes_a_merged_face_as_one() {
|
||||
// Before: the surface mesh keeps the seams as feature lines; after: none.
|
||||
mesher_meshes_a_merged_face_as_one("mesh.tet.generate", json!({"size": 4}), |_, _| 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_hex_mesher_meshes_a_merged_face_as_one() {
|
||||
// Before, the swept O-grid round the hole had a node column on each seam, and after the
|
||||
// merge one on the seam where the closed rim's chain started (#512). The rim's start is
|
||||
// no curve end now: the seams are lines like any other round the hole. The square plate's
|
||||
// layout (cuts from its corners) still puts nodes in the middle of the arcs between the
|
||||
// cuts, which on this plate are where the seams lie and also a quarter turn round: the
|
||||
// seams carry as many nodes as those turned lines, no more and no fewer.
|
||||
let turned = |s: &Session, seams: &[Vec<[f64; 3]>]| {
|
||||
let quarter: Vec<Vec<[f64; 3]>> = seams.iter().map(|l| l.iter().map(|p| [30.0 - p[1], p[0], p[2]]).collect()).collect();
|
||||
nodes_on_seams(s, &quarter)
|
||||
};
|
||||
mesher_meshes_a_merged_face_as_one("mesh.hex.generate", json!({"size": 2.5}), turned);
|
||||
}
|
||||
|
||||
/// The hex-meshing plan sees the merged hole as one face: the hole's feature and its ring
|
||||
/// project onto `merge:Hole` (still a cylinder), not the halves.
|
||||
#[test]
|
||||
fn the_plan_sees_a_merged_face_as_one() {
|
||||
let (mut s, dir) = super::super::datum::tests::plate();
|
||||
let body = s.design.as_ref().unwrap().bodies[0].name.clone();
|
||||
let hole = halves(&s);
|
||||
let before = s.execute("mesh.plan", &json!({"body": body})).unwrap();
|
||||
let ring = |r: &serde_json::Value| r["rings"][0]["faces"].clone();
|
||||
let mut halves_sorted = hole.clone();
|
||||
halves_sorted.sort();
|
||||
let mut got: Vec<String> = serde_json::from_value(ring(&before)).unwrap();
|
||||
got.sort();
|
||||
assert_eq!(got, halves_sorted, "{before}");
|
||||
merge_hole(&mut s);
|
||||
let after = s.execute("mesh.plan", &json!({"body": body})).unwrap();
|
||||
assert_eq!(ring(&after), json!(["merge:Hole"]), "{after}");
|
||||
let features = after["features"].as_array().unwrap();
|
||||
assert!(features.iter().any(|f| f["faces"] == json!(["merge:Hole"])), "{after}");
|
||||
assert!(!after.to_string().contains(&hole[0]), "{after}");
|
||||
std::fs::remove_dir_all(dir).ok();
|
||||
}
|
||||
|
||||
/// The viewport draws a merged face in one colour: the hole's triangles, and only those.
|
||||
#[test]
|
||||
fn a_merged_face_is_drawn_in_one_colour() {
|
||||
let (mut s, dir) = super::super::datum::tests::plate();
|
||||
let p = crate::view::Palette::LIGHT;
|
||||
let merged = |s: &Session| crate::view::scene(s, &p).0.tris.into_iter().filter(|t| t.color == p.merged).collect::<Vec<_>>();
|
||||
assert!(merged(&s).is_empty());
|
||||
merge_hole(&mut s);
|
||||
let tris = merged(&s);
|
||||
assert!(!tris.is_empty());
|
||||
// The hole: Ø10 about (15, 15).
|
||||
for t in &tris {
|
||||
assert!(t.p.iter().all(|v| ((v.x - 15.0).hypot(v.y - 15.0) - 5.0).abs() < 1e-3), "{t:?}");
|
||||
}
|
||||
std::fs::remove_dir_all(dir).ok();
|
||||
}
|
||||
|
||||
/// Sets naming a merged face go stale (and are named); bad merges change nothing.
|
||||
#[test]
|
||||
fn bad_merges_change_nothing() {
|
||||
let (mut s, dir) = super::super::datum::tests::plate();
|
||||
let body = s.design.as_ref().unwrap().bodies[0].name.clone();
|
||||
let names = faces(&s);
|
||||
let hole = halves(&s);
|
||||
s.execute("model.set.create", &json!({"name": "Bore", "geometry": [{"body": body, "face": hole[0]}]})).unwrap();
|
||||
let journal = s.journal.len();
|
||||
let f = |n: &str| json!({"body": body, "face": n});
|
||||
for p in [
|
||||
json!({}),
|
||||
json!({"name": "M"}),
|
||||
json!({"name": "M", "faces": [f(&hole[0])]}),
|
||||
json!({"name": "M", "faces": [f(&hole[0]), {"body": "Other", "face": hole[1]}]}),
|
||||
json!({"name": "M", "faces": [{"body": "Other", "face": "a"}, {"body": "Other", "face": "b"}]}),
|
||||
json!({"name": "M", "faces": [f(&hole[0]), f("nope")]}),
|
||||
json!({"name": "M", "faces": [f(&hole[0]), {"body": body}]}),
|
||||
json!({"name": "M", "faces": [f(&hole[0]), f(&hole[0])]}),
|
||||
json!({"name": "1M", "faces": [f(&hole[0]), f(&hole[1])]}),
|
||||
json!({"name": "M", "faces": "x"}),
|
||||
] {
|
||||
assert!(s.execute("geometry.faces.merge", &p).is_err(), "{p}");
|
||||
}
|
||||
// Top and bottom do not touch.
|
||||
let b = &s.design.as_ref().unwrap().bodies[0];
|
||||
let caps: Vec<String> = b
|
||||
.faces()
|
||||
.iter()
|
||||
.zip(b.face_surfaces().unwrap())
|
||||
.filter(|(_, x)| matches!(x, Some(FaceSurface::Plane { normal, .. }) if normal[2].abs() > 0.5))
|
||||
.map(|(f, _)| f.name.clone())
|
||||
.collect();
|
||||
let e = s.execute("geometry.faces.merge", &json!({"name": "Caps", "faces": [f(&caps[0]), f(&caps[1])]})).unwrap_err();
|
||||
assert!(e.to_string().contains("does not touch"), "{e}");
|
||||
assert!(s.execute("geometry.faces.merge.delete", &json!({"name": "Caps"})).is_err());
|
||||
assert_eq!((s.journal.len(), faces(&s)), (journal, names));
|
||||
assert!(s.model.face_merges().is_empty());
|
||||
let r = merge_hole(&mut s);
|
||||
assert_eq!(r["stale_sets"], json!(["Bore"]), "{r}");
|
||||
std::fs::remove_dir_all(dir).ok();
|
||||
}
|
||||
|
||||
/// A block with a 0.4 mm step on its top: the riser is a sliver at size 5.
|
||||
fn stepped() -> (crate::tests::TempDir, Session) {
|
||||
super::super::geometry::tests::design(json!([
|
||||
{"command": "solid.box", "params": {"length": 40, "width": 20, "height": 10, "body_name": "Base"}},
|
||||
{"command": "solid.box", "params": {"length": 20, "width": 20, "height": 10.4, "operation": "join", "targets": ["Base"]}},
|
||||
]))
|
||||
}
|
||||
|
||||
fn small_faces(s: &mut Session) -> usize {
|
||||
let r = s.execute("geometry.check", &json!({"size": 5})).unwrap();
|
||||
r["findings"].as_array().unwrap().iter().filter(|f| matches!(f["kind"].as_str(), Some("small_face" | "sliver_face"))).count()
|
||||
}
|
||||
|
||||
/// `from_check` absorbs every sliver the check found into its best neighbour, which keeps its
|
||||
/// name; the check then finds none, the tet mesher tags no element face with the sliver, and
|
||||
/// undo brings it back.
|
||||
#[test]
|
||||
fn absorbing_the_check_findings_clears_them() {
|
||||
let (_dir, mut s) = stepped();
|
||||
let before = small_faces(&mut s);
|
||||
assert!(before > 0);
|
||||
let start = faces(&s);
|
||||
assert!(s.execute("geometry.face.absorb", &json!({"from_check": true, "into": "x"})).is_err());
|
||||
let r = s.execute("geometry.face.absorb", &json!({"from_check": true})).unwrap();
|
||||
let absorbed = r["absorbed"].as_array().unwrap();
|
||||
assert_eq!(absorbed.len(), before, "{r}");
|
||||
assert_eq!(absorbed[0]["name"], "Absorb-1");
|
||||
let (gone, into) = (absorbed[0]["face"].as_str().unwrap().to_string(), absorbed[0]["into"].as_str().unwrap().to_string());
|
||||
assert!(!faces(&s).contains(&gone) && faces(&s).contains(&into));
|
||||
assert_eq!(faces(&s).len(), start.len() - before);
|
||||
assert_eq!(small_faces(&mut s), 0);
|
||||
s.execute("mesh.tet.generate", &json!({"size": 2})).unwrap();
|
||||
let c = s.model.classification();
|
||||
let used: BTreeSet<String> =
|
||||
c.elements().values().flat_map(|t| t.faces.values()).filter_map(|&t| c.entity(t)?.entity.name().map(str::to_string)).collect();
|
||||
assert!(used.contains(&into) && !used.contains(&gone), "{used:?}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(faces(&s), start);
|
||||
}
|
||||
|
||||
/// One face into a named neighbour, or its best one; bad requests change nothing.
|
||||
#[test]
|
||||
fn absorbing_one_face() {
|
||||
let (_dir, mut s) = stepped();
|
||||
let r = s.execute("geometry.check", &json!({"size": 5})).unwrap();
|
||||
let sliver =
|
||||
r["findings"].as_array().unwrap().iter().find(|f| f["kind"] == "sliver_face" || f["kind"] == "small_face").unwrap()["faces"][0].clone();
|
||||
let face = json!({"body": "Base", "face": sliver});
|
||||
let journal = s.journal.len();
|
||||
for p in [
|
||||
json!({"name": "A"}),
|
||||
json!({"name": "A", "face": face, "from_check": true}),
|
||||
json!({"face": face}),
|
||||
json!({"name": "A", "face": {"body": "Base", "face": "nope"}}),
|
||||
json!({"name": "A", "face": face, "into": "nope"}),
|
||||
json!({"name": "A", "face": {"body": "Nobody", "face": "x"}}),
|
||||
json!({"name": "A", "face": face, "from_check": "yes"}),
|
||||
] {
|
||||
assert!(s.execute("geometry.face.absorb", &p).is_err(), "{p}");
|
||||
}
|
||||
assert_eq!(s.journal.len(), journal);
|
||||
let r = s.execute("geometry.face.absorb", &json!({"name": "Riser", "face": face})).unwrap();
|
||||
assert_eq!(r["absorbed"][0]["name"], "Riser", "{r}");
|
||||
assert!(s.model.face_merges()[0].into.is_some());
|
||||
}
|
||||
|
||||
/// The plate's top face (z = 10), by name.
|
||||
fn top(s: &Session) -> String {
|
||||
let b = &s.design.as_ref().unwrap().bodies[0];
|
||||
b.faces()
|
||||
.iter()
|
||||
.zip(b.face_surfaces().unwrap())
|
||||
.find(|(_, x)| matches!(x, Some(FaceSurface::Plane { normal, point }) if normal[2] > 0.5 && point[2] > 9.0))
|
||||
.unwrap()
|
||||
.0
|
||||
.name
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// Mesh nodes on the top along x = `x`.
|
||||
fn nodes_along(s: &Session, x: f64) -> usize {
|
||||
s.model.nodes().values().filter(|n| (n.xyz[0] - x).abs() < 1e-6 && (n.xyz[2] - 10.0).abs() < 1e-6).count()
|
||||
}
|
||||
|
||||
/// Splitting the top along a datum plane gives two faces the tet mesher tags, with a row of
|
||||
/// nodes along the cut; the split follows the datum, holds it, is saved and replayed, and
|
||||
/// undoes.
|
||||
#[test]
|
||||
fn a_split_face_meshes_with_a_node_row_along_the_cut() {
|
||||
let (mut s, dir) = super::super::datum::tests::plate();
|
||||
let body = s.design.as_ref().unwrap().bodies[0].name.clone();
|
||||
let face = json!({"body": body, "face": top(&s)});
|
||||
s.execute("geometry.datum", &json!({"name": "Cut", "by": {"type": "offset", "plane": "yz", "offset": 30.3}})).unwrap();
|
||||
let mesh = json!({"size": 3});
|
||||
s.execute("mesh.tet.generate", &mesh).unwrap();
|
||||
let before = nodes_along(&s, 30.3);
|
||||
let r = s.execute("geometry.face.split", &json!({"name": "Band", "face": face, "datum": "Cut"})).unwrap();
|
||||
assert_eq!(r["faces"], json!(["split:Band:a", "split:Band:b"]), "{r}");
|
||||
assert!(r["replaced"].is_array(), "{r}");
|
||||
assert_eq!(faces(&s).len(), 9);
|
||||
s.execute("mesh.tet.generate", &mesh).unwrap();
|
||||
let after = nodes_along(&s, 30.3);
|
||||
assert!(after >= 10 && after > before, "a node row along the 30 mm cut: {before} -> {after}");
|
||||
let c = s.model.classification();
|
||||
let used: BTreeSet<String> =
|
||||
c.elements().values().flat_map(|t| t.faces.values()).filter_map(|&t| c.entity(t)?.entity.name().map(str::to_string)).collect();
|
||||
assert!(used.contains("split:Band:a") && used.contains("split:Band:b"), "{used:?}");
|
||||
// The datum holds; moving it moves the cut.
|
||||
assert!(s.execute("geometry.datum.delete", &json!({"name": "Cut"})).is_err());
|
||||
s.execute("geometry.datum.edit", &json!({"name": "Cut", "by": {"type": "offset", "plane": "yz", "offset": 35.5}})).unwrap();
|
||||
s.execute("mesh.tet.generate", &mesh).unwrap();
|
||||
assert!(nodes_along(&s, 35.5) >= 10);
|
||||
// Saved and replayed.
|
||||
let file = dir.join("split.fieldcraft");
|
||||
s.execute("file.save", &json!({"path": file.to_str().unwrap()})).unwrap();
|
||||
let mut t = Session::default();
|
||||
t.execute("file.open", &json!({"path": file.to_str().unwrap()})).unwrap();
|
||||
assert_eq!(faces(&t), faces(&s));
|
||||
// Bad splits change nothing; delete brings the face back.
|
||||
let journal = s.journal.len();
|
||||
for p in [
|
||||
json!({"name": "X", "face": face, "datum": "nope"}),
|
||||
json!({"name": "X", "face": {"body": body, "face": "nope"}, "datum": "Cut"}),
|
||||
json!({"name": "X", "face": {"body": body}, "datum": "Cut"}),
|
||||
json!({"name": "Band", "face": face, "datum": "Cut"}),
|
||||
json!({"name": "X", "face": {"body": body, "face": "split:Band:b"}, "datum": "Cut"}),
|
||||
] {
|
||||
assert!(s.execute("geometry.face.split", &p).is_err(), "{p}");
|
||||
}
|
||||
assert_eq!(s.journal.len(), journal);
|
||||
s.execute("geometry.face.split.delete", &json!({"name": "Band"})).unwrap();
|
||||
assert_eq!(faces(&s).len(), 8);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(faces(&s).len(), 9);
|
||||
std::fs::remove_dir_all(dir).ok();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,490 @@
|
||||
//! `results.xy`: one quantity of a solved step against its natural axis — a nodal field per
|
||||
//! frame against the load factor (nonlinear static) or time (explicit, transient heat), a mode
|
||||
//! shape's value or the frequencies against the mode number, and an explicit step's histories
|
||||
//! (energies, momentum, constraint force, history nodes) against time. Read-only; with `path`
|
||||
//! the series is also written as a two-column CSV.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::opt;
|
||||
use super::results::{Cell, Table, Want, step_for};
|
||||
use crate::requirement::Requirement;
|
||||
use crate::results::{Field, Kind, Pick, Quantity, field_values, with_unit};
|
||||
use crate::schema::ProblemKind;
|
||||
use crate::{CommandKind, CommandSpec, EngineError, Result, Session, StepResult};
|
||||
|
||||
pub const XY: CommandSpec = CommandSpec {
|
||||
id: "results.xy",
|
||||
label: "XY Plot",
|
||||
params: r#"{"step": "Load", "y": {"field": "displacement_z", "node": 21}, "x": "auto", "every": 1, "path": "tip.csv"} — a quantity of a solved step against its natural axis: `x` "load_factor" (nonlinear static), "time" (explicit, transient heat), "mode" (frequency and buckling) or "auto" (the step's own, the default). `y` is {"field": <a contour field id>, "node": <label>} (a nodal field per frame or mode: on nonlinear static steps the displacements, whose frames start at load factor 0; on heat steps the temperature), {"energy": "kinetic|internal|hourglass|viscous|external|constraint|contact|friction|plastic|eroded|added_kinetic|balance"}, {"history": "constraint_force_x|_y|_z|momentum_x|_y|_z|increment"}, {"history_node": 21, "quantity": "displacement|displacement_x|…|velocity_z"} (explicit histories), "frequency" (frequency steps) or "load_factor" (buckling steps). Reactions per increment are not stored by the nonlinear solver, so a reaction cannot be plotted against the load factor; an explicit step's `constraint_force` history is its reaction against time. At most 100000 points: `every` keeps one in so many (by default enough to fit). A stale result is refused. With `path` the series is written as CSV (a header line, then x,y rows). Replies {step, x: {label, values}, y: {label, values}, points, every, frames?} (`frames`: the field frame of each point, for results.frame)"#,
|
||||
form: Some(super::forms::RESULTS_XY),
|
||||
kind: CommandKind::Session,
|
||||
requires: &[Requirement::Result],
|
||||
run: xy,
|
||||
};
|
||||
|
||||
/// Most points a series holds.
|
||||
pub const MAX_POINTS: usize = 100_000;
|
||||
|
||||
/// What a series is plotted against.
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
enum Axis {
|
||||
LoadFactor,
|
||||
Time,
|
||||
Mode,
|
||||
}
|
||||
|
||||
impl Axis {
|
||||
fn id(self) -> &'static str {
|
||||
match self {
|
||||
Axis::LoadFactor => "load_factor",
|
||||
Axis::Time => "time",
|
||||
Axis::Mode => "mode",
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(id: &str) -> Option<Axis> {
|
||||
[Axis::LoadFactor, Axis::Time, Axis::Mode].into_iter().find(|a| a.id() == id)
|
||||
}
|
||||
|
||||
fn label(self, s: &Session) -> String {
|
||||
match self {
|
||||
Axis::LoadFactor => "Load factor".into(),
|
||||
Axis::Time => with_unit("Time", Quantity::Time.unit(s.model.units()).as_deref()),
|
||||
Axis::Mode => "Mode".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The energies of an explicit step (`y: {"energy": …}`).
|
||||
pub const ENERGIES: [&str; 12] =
|
||||
["kinetic", "internal", "hourglass", "viscous", "external", "constraint", "contact", "friction", "plastic", "eroded", "added_kinetic", "balance"];
|
||||
/// The other histories of an explicit step (`y: {"history": …}`).
|
||||
pub const HISTORIES: [&str; 7] =
|
||||
["constraint_force_x", "constraint_force_y", "constraint_force_z", "momentum_x", "momentum_y", "momentum_z", "increment"];
|
||||
/// What a history node keeps (`y: {"history_node": n, "quantity": …}`).
|
||||
pub const NODE_QUANTITIES: [&str; 8] =
|
||||
["displacement", "displacement_x", "displacement_y", "displacement_z", "velocity", "velocity_x", "velocity_y", "velocity_z"];
|
||||
|
||||
/// A series: its labels, its x values and a function giving the point at an index.
|
||||
struct Series<'a> {
|
||||
x_label: String,
|
||||
y_label: String,
|
||||
len: usize,
|
||||
point: Box<dyn Fn(usize) -> Result<(f64, f64)> + 'a>,
|
||||
/// Whether point `i` is field frame `i` (results.frame can show it).
|
||||
frames: bool,
|
||||
}
|
||||
|
||||
fn y_error(why: impl Into<String>) -> EngineError {
|
||||
EngineError::invalid("y", why)
|
||||
}
|
||||
|
||||
fn magnitude(v: &[f64; 3]) -> f64 {
|
||||
(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt()
|
||||
}
|
||||
|
||||
/// A vector's component or magnitude by suffix: `""` the magnitude, `"_x"`… a component.
|
||||
fn component(v: &[f64; 3], suffix: &str) -> Option<f64> {
|
||||
match suffix {
|
||||
"" => Some(magnitude(v)),
|
||||
"_x" => Some(v[0]),
|
||||
"_y" => Some(v[1]),
|
||||
"_z" => Some(v[2]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A node label from `y.node` or `y.history_node`.
|
||||
fn node_label(y: &Value, key: &str) -> Result<u32> {
|
||||
let v = y.get(key).ok_or_else(|| y_error(format!("needs `{key}`: a node label")))?;
|
||||
v.as_u64()
|
||||
.and_then(|n| u32::try_from(n).ok())
|
||||
.filter(|n| *n > 0)
|
||||
.ok_or_else(|| y_error(format!("`{key}` must be a node label from 1 to {}", u32::MAX)))
|
||||
}
|
||||
|
||||
/// The fields a step plots per frame or mode at a node.
|
||||
pub fn plottable(r: &StepResult) -> Vec<Field> {
|
||||
let shapes = [Field::Displacement, Field::DisplacementX, Field::DisplacementY, Field::DisplacementZ];
|
||||
match r.kind() {
|
||||
// Nonlinear frames hold displacements only; the other fields are the final state's.
|
||||
Kind::Static(_) | Kind::Frequency(_) => shapes.to_vec(),
|
||||
Kind::Explicit(_) => Field::available(r).collect(),
|
||||
Kind::Heat(_) => vec![Field::Temperature],
|
||||
Kind::Fluid(_) => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn ids(list: &[&str]) -> String {
|
||||
list.join(", ")
|
||||
}
|
||||
|
||||
/// The series `y` asks for on step `step`'s result `r`, against `axis`.
|
||||
fn series<'a>(s: &'a Session, step: &str, r: &'a StepResult, axis: Axis, y: &Value) -> Result<Series<'a>> {
|
||||
let x_label = axis.label(s);
|
||||
let units = s.model.units();
|
||||
let not_explicit = || y_error(format!("step `{step}` is not an explicit dynamic step; it has no histories"));
|
||||
if let Some(name) = y.as_str() {
|
||||
let Kind::Frequency(f) = r.kind() else {
|
||||
return Err(y_error(format!("`{name}` needs a frequency or buckling step; `{step}` is not one")));
|
||||
};
|
||||
let buckling = f.buckling.is_some();
|
||||
let (y_label, field): (String, fn(&fieldcraft_solver::Mode) -> f64) = match (name, buckling) {
|
||||
("frequency", false) => (with_unit("Frequency", Some("cycles per time unit")), |m| m.frequency),
|
||||
("load_factor", true) => ("Buckling load factor".into(), |m| m.eigenvalue),
|
||||
("frequency", true) => return Err(y_error(format!("step `{step}` is a buckling step: its modes are load factors (y: \"load_factor\")"))),
|
||||
("load_factor", false) => return Err(y_error(format!("step `{step}` is a frequency step: y: \"frequency\""))),
|
||||
_ => {
|
||||
return Err(y_error(format!(
|
||||
"`{name}` is not a series; use \"frequency\", \"load_factor\" or an object ({{\"field\", \"node\"}}, …)"
|
||||
)));
|
||||
}
|
||||
};
|
||||
return Ok(Series {
|
||||
x_label,
|
||||
y_label,
|
||||
len: f.modes.len(),
|
||||
point: Box::new(move |i| Ok((i as f64 + 1.0, f.modes.get(i).map_or(f64::NAN, field)))),
|
||||
frames: false,
|
||||
});
|
||||
}
|
||||
if !y.is_object() {
|
||||
return Err(y_error(
|
||||
"must be an object ({\"field\", \"node\"}, {\"energy\"}, {\"history\"} or {\"history_node\", \"quantity\"}) or \"frequency\"",
|
||||
));
|
||||
}
|
||||
if let Some(id) = y.get("field") {
|
||||
let available = plottable(r);
|
||||
let list = || available.iter().map(|f| f.id()).collect::<Vec<_>>().join(", ");
|
||||
let field = id.as_str().and_then(Field::parse).filter(|f| available.contains(f)).ok_or_else(|| {
|
||||
if matches!(r.kind(), Kind::Fluid(_)) {
|
||||
y_error(format!("step `{step}` is a fluid step: its fields are per cell (solve.probe)"))
|
||||
} else {
|
||||
y_error(format!("`field` must be one step `{step}` has per {}: {}", if axis == Axis::Mode { "mode" } else { "frame" }, list()))
|
||||
}
|
||||
})?;
|
||||
let label = node_label(y, "node")?;
|
||||
let at = r.nodes().iter().position(|n| *n == label).ok_or_else(|| y_error(format!("node {label} has no result in step `{step}`")))?;
|
||||
let model = &s.model;
|
||||
let value = move |pick: Pick| -> Result<f64> { Ok(field_values(model, r, field, pick)?.get(at).copied().unwrap_or(f64::NAN)) };
|
||||
let y_label = format!("{} at node {label}", field.title(r, units));
|
||||
let (len, frames, x): (usize, bool, Box<dyn Fn(usize) -> f64 + 'a>) = match r.kind() {
|
||||
Kind::Static(st) => {
|
||||
let h = st.nonlinear.as_ref().ok_or_else(|| {
|
||||
y_error(format!("step `{step}` is a linear static step: one state, nothing to plot against (a nonlinear static step keeps a frame per increment)"))
|
||||
})?;
|
||||
(h.frames.len(), true, Box::new(move |i| h.frames.get(i).map_or(f64::NAN, |f| f.load_factor)))
|
||||
}
|
||||
Kind::Explicit(e) => (e.frames.len(), true, Box::new(move |i| e.frames.get(i).map_or(f64::NAN, |f| f.time))),
|
||||
Kind::Heat(h) => (h.frames.len(), true, Box::new(move |i| h.times.get(i).copied().unwrap_or(f64::NAN))),
|
||||
Kind::Frequency(f) => (f.modes.len(), false, Box::new(|i| i as f64 + 1.0)),
|
||||
Kind::Fluid(_) => return Err(y_error(format!("step `{step}` is a fluid step: its fields are per cell (solve.probe)"))),
|
||||
};
|
||||
let point = move |i: usize| -> Result<(f64, f64)> {
|
||||
let pick = if frames { Pick { mode: 1, frame: Some(i) } } else { Pick { mode: i + 1, frame: None } };
|
||||
Ok((x(i), value(pick)?))
|
||||
};
|
||||
return Ok(Series { x_label, y_label, len, point: Box::new(point), frames });
|
||||
}
|
||||
if y.get("reaction").is_some() {
|
||||
return Err(y_error(
|
||||
"reactions are not stored per increment, so they cannot be plotted against the load factor (results.reactions gives the final sums; an explicit step's {\"history\": \"constraint_force_z\"} is its reaction against time)",
|
||||
));
|
||||
}
|
||||
let Kind::Explicit(e) = r.kind() else { return Err(not_explicit()) };
|
||||
let h = &e.history;
|
||||
let time = move |i: usize| h.time.get(i).copied().unwrap_or(f64::NAN);
|
||||
if let Some(name) = y.get("energy") {
|
||||
let name = name.as_str().filter(|n| ENERGIES.contains(n)).ok_or_else(|| y_error(format!("`energy` must be one of {}", ids(&ENERGIES))))?;
|
||||
let pick: fn(&fieldcraft_explicit::Energies) -> f64 = match name {
|
||||
"kinetic" => |e| e.kinetic,
|
||||
"internal" => |e| e.internal,
|
||||
"hourglass" => |e| e.hourglass,
|
||||
"viscous" => |e| e.viscous,
|
||||
"external" => |e| e.external,
|
||||
"constraint" => |e| e.constraint,
|
||||
"contact" => |e| e.contact,
|
||||
"friction" => |e| e.friction,
|
||||
"plastic" => |e| e.plastic,
|
||||
"eroded" => |e| e.eroded,
|
||||
"added_kinetic" => |e| e.added_kinetic,
|
||||
_ => |e| e.balance,
|
||||
};
|
||||
let label = format!("{} energy", name.replace('_', " "));
|
||||
let label = label.get(..1).map(str::to_uppercase).unwrap_or_default() + label.get(1..).unwrap_or_default();
|
||||
return Ok(Series {
|
||||
x_label,
|
||||
y_label: with_unit(&label, Quantity::Energy.unit(units).as_deref()),
|
||||
len: h.time.len(),
|
||||
point: Box::new(move |i| Ok((time(i), h.energies.get(i).map_or(f64::NAN, pick)))),
|
||||
frames: false,
|
||||
});
|
||||
}
|
||||
if let Some(name) = y.get("history") {
|
||||
let name = name.as_str().filter(|n| HISTORIES.contains(n)).ok_or_else(|| y_error(format!("`history` must be one of {}", ids(&HISTORIES))))?;
|
||||
let axis_of = |suffix: &str| match suffix {
|
||||
"x" => 0,
|
||||
"y" => 1,
|
||||
_ => 2,
|
||||
};
|
||||
let (y_label, value): (String, Box<dyn Fn(usize) -> f64 + 'a>) = if let Some(c) = name.strip_prefix("constraint_force_") {
|
||||
let k = axis_of(c);
|
||||
(
|
||||
with_unit(&format!("Constraint force {}", c.to_uppercase()), Quantity::Force.unit(units).as_deref()),
|
||||
Box::new(move |i| h.constraint_force.get(i).map_or(f64::NAN, |v| v[k])),
|
||||
)
|
||||
} else if let Some(c) = name.strip_prefix("momentum_") {
|
||||
let k = axis_of(c);
|
||||
(format!("Momentum {}", c.to_uppercase()), Box::new(move |i| h.momentum.get(i).map_or(f64::NAN, |v| v[k])))
|
||||
} else {
|
||||
(with_unit("Time increment", Quantity::Time.unit(units).as_deref()), Box::new(move |i| h.increment.get(i).copied().unwrap_or(f64::NAN)))
|
||||
};
|
||||
return Ok(Series { x_label, y_label, len: h.time.len(), point: Box::new(move |i| Ok((time(i), value(i)))), frames: false });
|
||||
}
|
||||
if y.get("history_node").is_some() {
|
||||
let label = node_label(y, "history_node")?;
|
||||
let k = h.nodes.iter().position(|n| *n == label).ok_or_else(|| {
|
||||
let listed: Vec<String> = h.nodes.iter().take(super::LISTED).map(u32::to_string).collect();
|
||||
y_error(format!(
|
||||
"node {label} has no history in step `{step}`; {}",
|
||||
if listed.is_empty() { "the step keeps no node histories".to_string() } else { format!("history nodes: {}", listed.join(", ")) }
|
||||
))
|
||||
})?;
|
||||
let quantity = y
|
||||
.get("quantity")
|
||||
.and_then(Value::as_str)
|
||||
.filter(|q| NODE_QUANTITIES.contains(q))
|
||||
.ok_or_else(|| y_error(format!("`quantity` must be one of {}", ids(&NODE_QUANTITIES))))?;
|
||||
let (values, suffix, title, q) = match quantity.strip_prefix("displacement") {
|
||||
Some(suffix) => (&h.displacement, suffix, "Displacement", Quantity::Length),
|
||||
None => (&h.velocity, quantity.trim_start_matches("velocity"), "Velocity", Quantity::Velocity),
|
||||
};
|
||||
let name =
|
||||
if suffix.is_empty() { format!("{title} magnitude") } else { format!("{title} {}", suffix.trim_start_matches('_').to_uppercase()) };
|
||||
let suffix = suffix.to_string();
|
||||
return Ok(Series {
|
||||
x_label,
|
||||
y_label: format!("{} at node {label}", with_unit(&name, q.unit(units).as_deref())),
|
||||
len: h.time.len(),
|
||||
point: Box::new(move |i| Ok((time(i), values.get(i).and_then(|row| row.get(k)).and_then(|v| component(v, &suffix)).unwrap_or(f64::NAN)))),
|
||||
frames: false,
|
||||
});
|
||||
}
|
||||
Err(y_error("must have one of `field` (with `node`), `energy`, `history` or `history_node` (with `quantity`)"))
|
||||
}
|
||||
|
||||
/// The step's own axis.
|
||||
fn natural(step: &str, r: &StepResult) -> Result<Axis> {
|
||||
match r.kind() {
|
||||
Kind::Static(st) if st.nonlinear.is_some() => Ok(Axis::LoadFactor),
|
||||
Kind::Static(_) => Err(EngineError::Params(format!(
|
||||
"step `{step}` is a linear static step: one state, nothing to plot against (a nonlinear static step keeps a frame per increment)"
|
||||
))),
|
||||
Kind::Explicit(_) | Kind::Heat(_) => Ok(Axis::Time),
|
||||
Kind::Frequency(_) => Ok(Axis::Mode),
|
||||
Kind::Fluid(_) => Err(EngineError::Params(format!("step `{step}` is a fluid step: it has no series to plot yet"))),
|
||||
}
|
||||
}
|
||||
|
||||
fn xy(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let step = step_for(s, p, Want::Any)?;
|
||||
let (r, current) = s.latest_result(&step).ok_or_else(|| EngineError::Params(format!("step `{step}` has no result")))?;
|
||||
let axis = match opt::<String>(p, "x")?.as_deref() {
|
||||
None | Some("auto") => None,
|
||||
Some(id) => Some(Axis::parse(id).ok_or_else(|| EngineError::invalid("x", "must be \"auto\", \"load_factor\", \"time\" or \"mode\""))?),
|
||||
};
|
||||
let y = p.get("y").filter(|v| !v.is_null()).ok_or_else(|| EngineError::param("y", ProblemKind::Required))?;
|
||||
if !current {
|
||||
return Err(EngineError::Params(format!("the result of step `{step}` is stale (the model changed since it was solved); solve it again")));
|
||||
}
|
||||
let own = natural(&step, r)?;
|
||||
if let Some(a) = axis.filter(|a| *a != own) {
|
||||
return Err(EngineError::invalid("x", format!("step `{step}` is plotted against {}, not {}", own.id(), a.id())));
|
||||
}
|
||||
let every = match opt::<u64>(p, "every")? {
|
||||
Some(0) => return Err(EngineError::param("every", ProblemKind::Below(1.0))),
|
||||
Some(k) => Some(usize::try_from(k).unwrap_or(usize::MAX)),
|
||||
None => None,
|
||||
};
|
||||
let path = match p.get("path") {
|
||||
Some(_) => Some(super::file_param(s, p, "path", fieldcraft_io::roots::Access::Write)?),
|
||||
None => None,
|
||||
};
|
||||
let series = series(s, &step, r, own, y)?;
|
||||
let every = every.unwrap_or_else(|| series.len.div_ceil(MAX_POINTS).max(1));
|
||||
if series.len.div_ceil(every) > MAX_POINTS {
|
||||
return Err(EngineError::invalid("every", format!("{} points in steps of {every} are more than {MAX_POINTS}; raise `every`", series.len)));
|
||||
}
|
||||
let picked: Vec<usize> = (0..series.len).step_by(every).collect();
|
||||
let (mut xs, mut ys) = (Vec::with_capacity(picked.len()), Vec::with_capacity(picked.len()));
|
||||
for &i in &picked {
|
||||
let (x, y) = (series.point)(i)?;
|
||||
xs.push(x);
|
||||
ys.push(y);
|
||||
}
|
||||
let mut out = json!({
|
||||
"step": step,
|
||||
"x": {"axis": own.id(), "label": series.x_label, "values": xs},
|
||||
"y": {"label": series.y_label, "values": ys},
|
||||
"points": picked.len(),
|
||||
"every": every,
|
||||
});
|
||||
if series.frames {
|
||||
out["frames"] = json!(picked);
|
||||
}
|
||||
if let Some(path) = path {
|
||||
let cell = |v: f64| if v.is_finite() { Cell::Number(v) } else { Cell::Empty };
|
||||
let table = Table { header: vec![series.x_label, series.y_label], rows: xs.iter().zip(&ys).map(|(x, y)| vec![cell(*x), cell(*y)]).collect() };
|
||||
fieldcraft_io::fs::write_atomic(std::path::Path::new(&path), table.csv().as_bytes(), false)?;
|
||||
out["path"] = json!(path);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::cmd::solve::tests::cantilever;
|
||||
|
||||
fn values(r: &Value, axis: &str) -> Vec<f64> {
|
||||
r[axis]["values"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap_or(f64::NAN)).collect()
|
||||
}
|
||||
|
||||
fn impact() -> Session {
|
||||
let mut s = Session::default();
|
||||
s.run_script(&crate::sample::impact()).unwrap();
|
||||
s.execute("solve.explicit", &json!({})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
/// Energies and constraint forces are solve.history's numbers, sample for sample.
|
||||
#[test]
|
||||
fn explicit_series_are_the_histories() {
|
||||
let mut s = impact();
|
||||
let history = s.execute("solve.history", &json!({"step": "Impact"})).unwrap();
|
||||
let r = s.execute("results.xy", &json!({"step": "Impact", "y": {"energy": "kinetic"}})).unwrap();
|
||||
let time: Vec<f64> = history["time"].as_array().unwrap().iter().map(|v| v.as_f64().unwrap()).collect();
|
||||
let kinetic: Vec<f64> = history["energies"].as_array().unwrap().iter().map(|e| e["kinetic"].as_f64().unwrap()).collect();
|
||||
assert_eq!(values(&r, "x"), time);
|
||||
assert_eq!(values(&r, "y"), kinetic);
|
||||
assert_eq!(r["x"]["axis"], "time");
|
||||
assert!(r.get("frames").is_none(), "history samples are not field frames");
|
||||
let r = s.execute("results.xy", &json!({"step": "Impact", "x": "time", "y": {"history": "constraint_force_x"}})).unwrap();
|
||||
let fx: Vec<f64> = history["constraint_force"].as_array().unwrap().iter().map(|f| f[0].as_f64().unwrap()).collect();
|
||||
assert_eq!(values(&r, "y"), fx);
|
||||
// A nodal field per field frame, with the frames for results.frame.
|
||||
let r = s.execute("results.xy", &json!({"step": "Impact", "y": {"field": "velocity", "node": 1}})).unwrap();
|
||||
assert_eq!(r["points"], 11);
|
||||
assert_eq!(r["frames"].as_array().unwrap().len(), 11);
|
||||
assert!((values(&r, "y")[0] - 10_000.0).abs() < 1e-6, "every node starts at 10 m/s: {r}");
|
||||
// `every` strides.
|
||||
let r = s.execute("results.xy", &json!({"step": "Impact", "y": {"energy": "balance"}, "every": 2})).unwrap();
|
||||
assert_eq!(r["points"].as_u64().unwrap() as usize, time.len().div_ceil(2));
|
||||
}
|
||||
|
||||
/// The frequency series is the solve's frequencies against modes 1 to n.
|
||||
#[test]
|
||||
fn frequencies_against_the_mode_number() {
|
||||
let mut s = Session::default();
|
||||
cantilever(&mut s);
|
||||
let summary = s.execute("solve.frequency", &json!({})).unwrap();
|
||||
let r = s.execute("results.xy", &json!({"step": "Modes", "y": "frequency"})).unwrap();
|
||||
let expected: Vec<f64> = summary["modes"].as_array().unwrap().iter().map(|m| m["frequency"].as_f64().unwrap()).collect();
|
||||
assert_eq!(values(&r, "y"), expected);
|
||||
assert_eq!(values(&r, "x"), (1..=expected.len()).map(|m| m as f64).collect::<Vec<_>>());
|
||||
assert!(s.execute("results.xy", &json!({"step": "Modes", "y": "load_factor"})).is_err());
|
||||
assert!(s.execute("results.xy", &json!({"step": "Modes", "x": "time", "y": "frequency"})).is_err());
|
||||
}
|
||||
|
||||
/// A small tip load on the nonlinear cantilever stays linear: the tip deflection grows in
|
||||
/// proportion to the load factor, from 0, to the linear static answer (P L³ / 3 E I plus
|
||||
/// shear), and its last point is solve.probe's at the last frame. The CSV holds the series.
|
||||
#[test]
|
||||
fn tip_deflection_against_the_load_factor() {
|
||||
let mut s = Session::default();
|
||||
cantilever(&mut s);
|
||||
s.run_script(&json!([
|
||||
{"command": "model.section.edit", "params": {"name": "bar", "set": "all", "material": "Mat", "kind": {"type": "solid"}, "formulation": {}}},
|
||||
{"command": "model.load.edit", "params": {"name": "push", "type": "force", "target": {"set": "tip"}, "vector": [0, 0, -0.001]}},
|
||||
{"command": "model.step.create", "params": {"name": "Large", "procedure": {"type": "nonlinear_static", "initial_increment": 0.25},
|
||||
"boundary_conditions": ["clamp"], "loads": ["push"]}},
|
||||
]))
|
||||
.unwrap();
|
||||
s.execute("solve.static", &json!({"step": "Bend"})).unwrap();
|
||||
let linear = -s.execute("solve.probe", &json!({"step": "Bend", "nodes": [11]})).unwrap()["nodes"][0]["displacement"][2].as_f64().unwrap();
|
||||
s.execute("solve.nonlinear", &json!({"step": "Large"})).unwrap();
|
||||
let path = std::env::temp_dir().join(format!("fieldcraft-xy-{}.csv", std::process::id())).to_string_lossy().into_owned();
|
||||
let r = s.execute("results.xy", &json!({"step": "Large", "y": {"field": "displacement_z", "node": 11}, "path": path})).unwrap();
|
||||
let (x, y) = (values(&r, "x"), values(&r, "y"));
|
||||
assert_eq!(r["x"]["axis"], "load_factor");
|
||||
assert_eq!((x[0], y[0]), (0.0, 0.0), "the undeformed state first");
|
||||
assert!((x.last().unwrap() - 1.0).abs() < 1e-12, "{r}");
|
||||
for (l, d) in x.iter().zip(&y).skip(1) {
|
||||
assert!((-d / l - linear).abs() / linear < 1e-3, "λ {l}: {d} against {linear} λ");
|
||||
}
|
||||
let frames = r["frames"].as_array().unwrap().len();
|
||||
let probe = s.execute("solve.probe", &json!({"step": "Large", "nodes": [11], "frame": frames - 1})).unwrap();
|
||||
assert_eq!(*y.last().unwrap(), probe["nodes"][0]["displacement"][2].as_f64().unwrap());
|
||||
let csv = std::fs::read_to_string(&path).unwrap();
|
||||
let lines: Vec<&str> = csv.lines().collect();
|
||||
assert_eq!(lines[0], "Load factor,Displacement Z at node 11");
|
||||
assert_eq!(lines.len(), x.len() + 1);
|
||||
assert_eq!(lines.last().unwrap().split(',').nth(1).unwrap().parse::<f64>().unwrap(), *y.last().unwrap());
|
||||
let _ = std::fs::remove_file(&path);
|
||||
// Stresses are the final state's only; a linear step has one state; reactions are not stored.
|
||||
assert!(s.execute("results.xy", &json!({"step": "Large", "y": {"field": "von_mises", "node": 11}})).is_err());
|
||||
assert!(s.execute("results.xy", &json!({"step": "Bend", "y": {"field": "displacement_z", "node": 11}})).is_err());
|
||||
let e = s.execute("results.xy", &json!({"step": "Large", "y": {"reaction": "root"}})).unwrap_err().to_string();
|
||||
assert!(e.contains("not stored per increment"), "{e}");
|
||||
// A stale result is refused.
|
||||
s.execute("mesh.node.add", &json!({"label": 99_999, "xyz": [0, 0, 0]})).unwrap();
|
||||
let e = s.execute("results.xy", &json!({"step": "Large", "y": {"field": "displacement_z", "node": 11}})).unwrap_err().to_string();
|
||||
assert!(e.contains("stale"), "{e}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xy_hostile_params_never_panic() {
|
||||
let mut modes = Session::default();
|
||||
cantilever(&mut modes);
|
||||
modes.execute("solve.frequency", &json!({"step": "Modes"})).unwrap();
|
||||
let hostile = [
|
||||
json!(null),
|
||||
json!([]),
|
||||
json!({}),
|
||||
json!({"y": null}),
|
||||
json!({"y": 7}),
|
||||
json!({"y": []}),
|
||||
json!({"y": "nope"}),
|
||||
json!({"step": "Nope", "y": "frequency"}),
|
||||
json!({"step": "Impact", "y": {"field": "nope", "node": 1}}),
|
||||
json!({"step": "Impact", "y": {"field": "velocity", "node": 0}}),
|
||||
json!({"step": "Impact", "y": {"field": "velocity", "node": 4_294_967_296_u64}}),
|
||||
json!({"step": "Impact", "y": {"field": "velocity", "node": -1}}),
|
||||
json!({"step": "Impact", "y": {"field": "velocity", "node": f64::NAN}}),
|
||||
json!({"step": "Impact", "y": {"field": "velocity"}}),
|
||||
json!({"step": "Impact", "y": {"energy": 3}}),
|
||||
json!({"step": "Impact", "y": {"history": "nope"}}),
|
||||
json!({"step": "Impact", "y": {"history_node": 1e300, "quantity": "velocity"}}),
|
||||
json!({"step": "Impact", "y": {"history_node": 1, "quantity": "nope"}}),
|
||||
json!({"step": "Impact", "x": "load_factor", "y": {"energy": "kinetic"}}),
|
||||
json!({"step": "Impact", "x": 7, "y": {"energy": "kinetic"}}),
|
||||
json!({"step": "Impact", "y": {"energy": "kinetic"}, "every": 0}),
|
||||
json!({"step": "Impact", "y": {"energy": "kinetic"}, "every": 1e300}),
|
||||
json!({"step": "Impact", "y": {"energy": "kinetic"}, "path": ""}),
|
||||
json!({"step": "Impact", "y": {"energy": "kinetic"}, "path": "/no/such/dir/x.csv"}),
|
||||
json!({"step": "Modes", "y": {"energy": "kinetic"}}),
|
||||
json!({"step": "Modes", "y": {"field": "von_mises", "node": 1}}),
|
||||
];
|
||||
for s in [impact(), modes] {
|
||||
for p in &hostile {
|
||||
let mut t = s.clone();
|
||||
assert!(t.execute("results.xy", p).is_err(), "{p}");
|
||||
assert_eq!(t.model, s.model);
|
||||
}
|
||||
}
|
||||
let mut empty = Session::default();
|
||||
let e = empty.execute("results.xy", &json!({"y": "frequency"})).unwrap_err().to_string();
|
||||
assert!(e.contains("solve"), "{e}");
|
||||
}
|
||||
}
|
||||