Skip to content

Monte Carlo Dispersion Improvement#1402

Merged
schaubh merged 7 commits into
developfrom
feature/mc_dispersions
Jun 25, 2026
Merged

Monte Carlo Dispersion Improvement#1402
schaubh merged 7 commits into
developfrom
feature/mc_dispersions

Conversation

@schaubh

@schaubh schaubh commented May 27, 2026

Copy link
Copy Markdown
Contributor
  • Tickets addressed: bsk-1164
  • Review: By commit
  • Merge strategy: Merge (no squash)

Description

Fixes BSK issue #1164 reported by ruthubotica by replacing the Monte Carlo executor’s direct setattr(simInstance, path, value) call with a nested path resolver. The resolver now applies dispersions and archived modifications to the actual simulation objects, including dotted attributes, integer list indices, and existing zero-argument accessor paths such as get_DynModel().scObject.hub.r_CN_NInit.

The branch also routes saved RNGSeed updates through the same application helper, ensures seeds are populated before configureFunction, and tunes scenarioMonteCarloAttRW dispersions/plot saving so the documented Monte Carlo results show subtle run-to-run variation rather than overlapping or saturated responses.

Commits are organized as controller/test fix, scenario/documentation update, and release-note/known-issue updates.

Verification

Added focused unit tests in src/utilities/MonteCarlo/_UnitTests/test_controller.py covering nested attribute updates, indexed list updates, zero-argument accessor paths, archived RNGSeed application before configuration, and generated UniformDispersion values reaching the live simulation object.

Validated with:

MPLBACKEND=Agg MPLCONFIGDIR=/private/tmp/mpl .venv/bin/pytest -q src/utilities/MonteCarlo/_UnitTests/test_controller.py
MPLBACKEND=Agg MPLCONFIGDIR=/private/tmp/mpl .venv/bin/pytest -q src/utilities/MonteCarlo/_UnitTests/test_scenarioBasicOrbitMC.py
MPLBACKEND=Agg MPLCONFIGDIR=/private/tmp/mpl .venv/bin/pytest -q 'src/tests/test_scenarioMonteCarloAttRW.py::test_MonteCarloSimulation[1]'
.venv/bin/python -m py_compile src/utilities/MonteCarlo/Controller.py src/utilities/MonteCarlo/_UnitTests/test_controller.py examples/scenarioMonteCarloAttRW.py
git diff --check origin/develop...HEAD

Documentation

Updated Monte Carlo README guidance to describe when seeds and non-seed dispersions are applied, and to document nested path support including zero-argument accessor methods.

Updated scenarioMonteCarloAttRW documentation/table to match the revised subtler dispersions and clarify nested dispersion paths. Added a release note snippet and updated known issues to mention the Monte Carlo dispersion application fix.

Reviewers should check the updated scenario dispersion table and the README’s description of configure/dispersal ordering.

Future work

No required follow-up is known.

@schaubh schaubh self-assigned this May 27, 2026
@schaubh schaubh requested a review from a team as a code owner May 27, 2026 18:36
@schaubh schaubh added this to Basilisk May 27, 2026
@schaubh schaubh added bug Something isn't working enhancement New feature or request labels May 27, 2026
@schaubh schaubh moved this to 👀 In review in Basilisk May 27, 2026
@schaubh schaubh linked an issue May 27, 2026 that may be closed by this pull request
@schaubh schaubh force-pushed the feature/mc_dispersions branch from 5f88e10 to f42a999 Compare May 27, 2026 19:36
@schaubh schaubh changed the title Feature/mc dispersions Monte Carlo Dispersion Improvement May 27, 2026
@schaubh schaubh requested a review from andrewmorell June 10, 2026 17:46

@andrewmorell andrewmorell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few issues here that I think need to be fixed and a few things that are optional. Feels like a good thing we're addressing but needs some more refinement.

Also a heads up that there are a number of next-door issues in dispersions.py that will surface with controller.py fixed. UniformVectorAngleDispersion(), NormalVectorAngleDispersion(), NormalVectorAngleDispersion(), NormalThrusterUnitDirectionVectorDispersion(), and maybe others use getattr() which can't resolve the nested paths that the controller now correctly writes.

Comment thread examples/scenarioMonteCarloAttRW.py Outdated
Comment thread examples/scenarioMonteCarloAttRW.py Outdated
Comment thread src/utilities/MonteCarlo/Controller.py Outdated
Comment thread examples/scenarioMonteCarloAttRW.py
Comment thread examples/scenarioMonteCarloAttRW.py Outdated
Comment thread src/utilities/MonteCarlo/_UnitTests/test_controller.py
Comment thread src/utilities/MonteCarlo/README.md Outdated
Comment thread src/utilities/MonteCarlo/_UnitTests/test_controller.py
Comment thread src/utilities/MonteCarlo/Controller.py Outdated
@schaubh schaubh force-pushed the feature/mc_dispersions branch from f42a999 to 19b72e8 Compare June 18, 2026 03:18
@schaubh schaubh requested a review from andrewmorell June 18, 2026 03:19
@schaubh schaubh force-pushed the feature/mc_dispersions branch from 19b72e8 to 9f22c0b Compare June 18, 2026 20:13

@andrewmorell andrewmorell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a few more things on this pass. In particular the double index nested paths edge case and next door bugs in dispersions.py feel in need of applying now, everything else is minor.

Also please give a few bread crumbs as to where/how feedback is implemented. It took a while to hunt down changes corresponding to one word replies.

Comment thread src/utilities/MonteCarlo/README.md
Comment thread src/utilities/MonteCarlo/Dispersions.py
Comment thread src/utilities/MonteCarlo/Dispersions.py Outdated
Comment thread src/utilities/MonteCarlo/Dispersions.py Outdated
Comment thread src/utilities/MonteCarlo/_UnitTests/test_controller.py
schaubh added 6 commits June 24, 2026 14:49
Replace direct setattr() on full Monte Carlo path strings with a shared path resolver that traverses dotted attributes, integer indices, and zero-argument accessor methods without eval or exec. This lets archived parameters, generated dispersions, and RNG seed modifications update the actual simulation objects instead of creating literal top-level attributes.

Reuse the same resolver for dispersion generators that need to read nominal nested values, and write indexed mutations back to their owning attributes so copy-on-read SWIG/Eigen containers such as voltage2TorqueGain[0] are not silently discarded.

Add focused unit coverage for nested attributes, accessor paths, SWIG indexed containers, RNG seeds before configureFunction, and generated UniformDispersion values reaching the live simulation object.
Tune scenarioMonteCarloAttRW dispersions so the example shows subtle run-to-run variation without driving the response into saturation. Keep the nominal RW speed values readable in RPM, but convert the dispersion bounds to rad/s before applying them to the RW*.Omega fields.

Fix the scenario figure-saving path so saved documentation figures accumulate all Monte Carlo traces, add a retained-trajectory comparison to catch identical-run regressions, and repair the RST grid table with explicit inertia units.
Add the release-note snippet for the Monte Carlo dispersion fix and record the resolved known issue for the current release. Keep this metadata separate from the framework and scenario commits so release-note changes are easy to review.
Extend the Monte Carlo nested-attribute setter to handle trailing
integer index chains, such as Eigen Matrix3d paths of the form
scObject.hub.IHubPntBc_B[0][1]. The setter now updates the deepest
indexed value, writes each indexed container back up the path, and
finally writes the full container back to the owning SWIG attribute.

Add a unit test using a real Basilisk spacecraft hub Matrix3d to verify
that double-indexed modifications propagate through the SWIG layer.
Also update the Monte Carlo README to document repeated bracket indexing
for matrix-like attributes.
Store the NormalVectorDispersion mean and stdDeviation constructor
arguments on the dispersion instance so generated values use the
configured normal distribution parameters. Also update generate() to call
the shared normal-vector helper consistently, since the helper reads the
stored statistics from self.

Add a deterministic regression test with zero standard deviation to
verify that NormalVectorDispersion honors a configured mean value.
Use the absolute phi and theta bounds computed from the nominal vector
directly when sampling UniformVectorAngleDispersion values. The previous
logic added the nominal phi/theta twice, shifting the sampling interval
to be centered around roughly twice the nominal angle.

Add a regression test with a non-axis nominal vector that records the
np.random.uniform() intervals and verifies they are centered on the
nominal vector angle plus the configured off-nominal bounds.
@schaubh schaubh force-pushed the feature/mc_dispersions branch from 9f22c0b to 1aae0ad Compare June 24, 2026 21:20
@schaubh schaubh requested a review from andrewmorell June 24, 2026 21:20
Update NormalVectorAngleDispersion to draw scalar phi/theta samples
instead of one-element NumPy arrays. The previous size=1 calls caused
magnitude generation to fail when round() received an ndarray.

Add a regression test that records the np.random.normal() calls and
verifies the dispersion uses scalar sample arguments while still
producing a normalized output vector and valid sigma magnitudes.

@andrewmorell andrewmorell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good to go now!

@schaubh schaubh merged commit 9fbb408 into develop Jun 25, 2026
7 checks passed
@schaubh schaubh deleted the feature/mc_dispersions branch June 25, 2026 02:01
@github-project-automation github-project-automation Bot moved this from 👀 In review to ✅ Done in Basilisk Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

Monte Carlo dispersions are not being applied

2 participants