Add environment layers to mjModel, MJCF and the spec API - #3460
Draft
teerthsharma wants to merge 1 commit into
Draft
Add environment layers to mjModel, MJCF and the spec API#3460teerthsharma wants to merge 1 commit into
teerthsharma wants to merge 1 commit into
Conversation
teerthsharma
force-pushed
the
feat/env-layers
branch
from
August 5, 2026 09:07
8015cca to
b5ed3cd
Compare
teerthsharma
force-pushed
the
feat/env-layers
branch
from
August 5, 2026 09:47
b5ed3cd to
20ef490
Compare
mjOption carries one gravity, wind, density and viscosity that hold everywhere in the model. This adds an ordered stack of horizontal medium slabs that let those four quantities vary with height along +z. mjModel gains nlayer and the layer_height, layer_gravity, layer_density, layer_viscosity and layer_wind arrays. mjSpec gains mjsLayer together with mjs_addLayer, mjs_getLayer and mjs_deleteLayers. <option> accepts any number of <layer> children, each inheriting its unspecified attributes from the global medium, and the compiler rejects heights that are not strictly ascending. Layers are unnamed global option data rather than model elements: they have no mjtObj type, no name and no id, hence no mjsElement, and the stack is cleared at once rather than deleted element by element. The compiler holds them in a deque so that pointers returned by mjs_addLayer survive later adds. Nothing reads the new fields yet. A model with no <layer> element compiles to nlayer == 0 and is unchanged. Signed-off-by: teerthsharma <teerths57@gmail.com>
teerthsharma
force-pushed
the
feat/env-layers
branch
from
August 5, 2026 11:02
20ef490 to
bf5ab81
Compare
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.
First of two steps toward #3452, which measures what MuJoCo's five global environment constants cost and proposes replacing them with fields. This step adds the data and the authoring surface; #3461 adds the reader.
Relates to #3452. Depends on #3458 for sanitizer builds.
Summary
mjOptioncarries one gravity vector, one wind vector, one density and one viscosity, and those four values hold at every point of the model. This adds an ordered stack of horizontal medium slabs so they can vary with height along +z.mjModelgainsnlayerand the arrayslayer_height,layer_gravity,layer_density,layer_viscosity,layer_wind.mjSpecgainsmjsLayerwithmjs_addLayer,mjs_getLayerandmjs_deleteLayers.<option>accepts any number of<layer>children, each inheriting unspecified attributes from the global medium, and the compiler rejects heights that are not strictly ascending.Layers are unnamed global option data rather than model elements: no
mjtObjtype, no name, no id, hence nomjsElement, and the stack is cleared at once rather than deleted element by element. The compiler holds them in a deque so pointers returned bymjs_addLayersurvive later adds.Nothing reads the new fields yet. A model with no
<layer>element compiles tonlayer == 0and is unchanged. The consumer is a follow-up: the sampler and its use in the fluid models.There is no change to
mjOption, to any existing physics path, or to the meaning of any existing attribute.Cost at
nlayer == 0Step time, 13 models, Linux x86-64, gcc 15.2.0,
SCHED_FIFOon a pinned core, best of 28 timed batches per arm, arm order alternated.mainmeasured against a second build of itself is the control.mainmain/main(control)Four of thirteen ratios fall below 1.0, which this change cannot cause, so the residual spread is measurement noise. Each arm loaded its own
libmujocoproven three ways, including reading/proc/self/mapsat run time; the two library hashes differ.qpos+qvelchecksums after 2000 steps are bit-identical on all 13 models betweenmainand this branch, so thenlayer == 0path is the legacy path rather than an approximation of it.Memory, 11 shipped models:
mj_sizeModelgrows by exactly +8 bytes on every one, themjtSize nlayercounter, andnbufferis byte-identical. The five arrays allocate nothing when unused. On the largest model in the corpus that is 1.9e-5 %.Validation
test/xml/xml_layer_test.ccXMLWriterLocaleTest.IgnoresLocalemainmainatf9a00bd5reports 1973 on this host; 1973 + 4 new = 1977.The four new tests cover an XML round trip of three layers including attribute inheritance from the global medium, rejection of unsorted and of duplicate heights,
nlayer == 0for a model with no<layer>, and a spec-API round trip exercisingmjs_addLayerpointer stability across a later add,mjs_getLayerbounds, andmjs_deleteLayerscompiling back tonlayer == 0.src/xml/generated/mjcf.xsdis regenerated frommjcf.schema; without itdoc_testfails.Limits
Sanitizers were not run against this branch. An ASan or UBSan build of MuJoCo does not currently compile under GCC 15.2.0 or clang 21.1.8 because of two constructs in
include/mujoco/mjsan.h, which #3458 fixes; with that fix applied locally the follow-up branch's tests pass clean under ASan, UBSan and LeakSanitizer. This branch adds no runtime code path, so there is nothing new for a sanitizer to reach until the consumer lands.layer_heightis sizednlayerwith the final entry unused rather thannlayer-1, because everyMJMODEL_POINTERSrow count expands to a literal member name. The header says so and the compiler writes allnlayervalues so the writer round-trips.The benchmark host was under sustained competing load throughout, which is why every arm ran pinned at real-time priority and why the base-versus-base control is load bearing rather than decorative. Run spreads of 0.3–5.4% mean this setup resolves regressions to roughly 2% and no finer.
MJX and the Warp backend are not extended; both are unaffected at
nlayer == 0.