perf: avoid np.isclose in preprocess_strains_with_limits#361
Open
magnusfjeldolsen wants to merge 2 commits into
Open
perf: avoid np.isclose in preprocess_strains_with_limits#361magnusfjeldolsen wants to merge 2 commits into
magnusfjeldolsen wants to merge 2 commits into
Conversation
preprocess_strains_with_limits runs at the top of every constitutive law's get_stress/get_tangent, so it is called for every strain evaluation in the section integrators. It called np.isclose twice and allocated two zeros_like temporaries on every call, even though it only compares each strain against the constant ultimate-strain limit. Replace this with precomputed scalar tolerances and plain abs(...) <= tol comparisons that exactly reproduce np.isclose(atol=1e-6) with the default rtol=1e-5, so results are bit-identical. The fiber integrator benefits most, since every fiber goes through get_stress: ~1.8x faster calculations across randomly generated RC sections (median over 40 sections). Marin gains little, as it does far fewer strain evaluations.
Assert that the precomputed-threshold implementation reproduces the documented np.isclose(atol=1e-6) tolerance bit-for-bit, and that strains within the band snap to the exact limit while strains outside it are returned unchanged. Parametrized over all nine constitutive laws.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #360
ConstitutiveLaw.preprocess_strains_with_limitssnaps strains that fall within asmall tolerance of an ultimate-strain limit onto that exact limit. It runs at the
top of every constitutive law's
get_stress/get_tangent, so it is called forevery strain evaluation in the section integrators — for the fiber integrator,
once per fiber on each iteration of the equilibrium solver.
It previously called
np.isclosetwice and allocated twonp.zeros_like(eps)arrays on every call, even though it only compares each strain against the
constant limit. This PR precomputes the absolute tolerance as a scalar and uses a
plain
abs(...) <= tolcomparison.Behaviour is unchanged
preprocess_strains_with_limitsreads the limits once per call viaget_ultimate_strain(), so within a call the closeness threshold is a singlescalar.
np.isclose(a, b)tests|a - b| <= atol + rtol * |b|; withbthelimit,
atol=1e-6, and the defaultrtol=1e-5, the threshold is exactly1e-6 + 1e-5 * |limit|. Computing that scalar once per call reproduces the sameboolean masks bit-for-bit — only the per-element
np.isclosemachinery and thezeros_likeallocations are removed.Performance
Fiber integrator: ~1.8× faster calculations (median over 40 randomly generated
rectangular and circular RC sections; calculation time, with the build path
unchanged as a control). Marin gains little (~1.0–1.1×), since it routes far
fewer strains through this method.
Tests
Added tests parametrized over all nine constitutive laws, asserting results are
bit-identical to the documented
np.isclose(atol=1e-6)reference and thatstrains snap correctly. Full suite passes;
ruff formatandruff check(0.12.10) clean.