Repository navigation
Parametrize sim tests over integrators #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
amacati
merged 4 commits into
learnsyslab:main
from
alirezanaseri548:tests/parametrize-integrators
Oct 23, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ae7c30e
Parametrize sim tests over integrators
alirezanaseri548 4b48be4
Address review feedback on integrator test coverage
alirezanaseri548 bc7776c
Spawn drones in the air in the integrator comparison test
alirezanaseri548 ccf6645
Reword comment
amacati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """Unit tests for simulation integrators.""" | ||
|
|
||
| import jax | ||
| import jax.numpy as jnp | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from crazyflow.sim import Dynamics, Sim | ||
| from crazyflow.sim.integration import Integrator | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| @pytest.mark.parametrize("integrator", Integrator) | ||
| @pytest.mark.parametrize("dynamics", Dynamics) | ||
| def test_integrators_close_to_default(dynamics: Dynamics, integrator: Integrator, device: str): | ||
| """Every integrator stays close to the default integrator after two simulation steps.""" | ||
| sim = Sim(dynamics=dynamics, integrator=integrator, device=device) | ||
| default_sim = Sim(dynamics=dynamics, device=device) | ||
| for s in (sim, default_sim): | ||
| # Spawn the drone in the air to induce motion during the steps | ||
| states = s.data.states.replace(pos=s.data.states.pos.at[..., 2].set(2.0)) | ||
| s.data = s.data.replace(states=states) | ||
| sim.step(2) | ||
| default_sim.step(2) | ||
|
|
||
| data, structure = jax.tree.flatten_with_path(sim.data) | ||
| default_data, default_structure = jax.tree.flatten(default_sim.data) | ||
| assert structure == default_structure, "Simulation data structures must match" | ||
| for (path, value), default_value in zip(data, default_data, strict=True): | ||
| name = jax.tree_util.keystr(path) | ||
| if isinstance(value, jnp.ndarray): | ||
| if jax.dtypes.issubdtype(value.dtype, jax.dtypes.prng_key): | ||
| np.testing.assert_array_equal( | ||
| jax.random.key_data(value), jax.random.key_data(default_value), err_msg=name | ||
| ) | ||
| elif jnp.issubdtype(value.dtype, jnp.inexact): | ||
| # Rotor speeds differ by several percent between Euler and RK4. | ||
| np.testing.assert_allclose( | ||
| value, default_value, rtol=2e-2, atol=1e-4, err_msg=name, strict=True | ||
| ) | ||
| else: | ||
| np.testing.assert_array_equal(value, default_value, err_msg=name, strict=True) | ||
| else: | ||
| assert value == default_value, f"{name}: Value mismatch" | ||
| sim.close() | ||
| default_sim.close() | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing: Could you spawn the drones in the air and then do all the checks below? With the floor clipping, it's likely that we would not see a difference in the dynamics
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in bc7776c — both sims now spawn at z=2.0 before stepping, so the floor clipping can no longer mask differences between the integration schemes.