Skip to content

Refactor ensemble generator for flexibility#940

Open
matthewhoffman wants to merge 27 commits intoMPAS-Dev:mainfrom
matthewhoffman:landice/ensemble-gen-refactor
Open

Refactor ensemble generator for flexibility#940
matthewhoffman wants to merge 27 commits intoMPAS-Dev:mainfrom
matthewhoffman:landice/ensemble-gen-refactor

Conversation

@matthewhoffman
Copy link
Copy Markdown
Member

@matthewhoffman matthewhoffman commented Mar 8, 2026

This PR refactors the landice/ensemble_generator test case to support major enhancements:

  • support for multiple 'ensemble_templates' that contain the cfg, namelist, streams, and albany input files for different studies. This allows us to keep the configuration files for multiple studies in compass without conflicting with each other. The PR includes a template that matches the previous test case behavior (Thwaites UQ study from Jantre et al 2024) and a template for the ongoing Kangerlussuaq basal friction UQ study.
  • the ability to make any MALI namelist option a perturbed parameter without any compass code modifications. This is accompanied by an overhaul of how parameters are specified and a general refresh of the cfg files. Details of the new format are in the docs.
  • ability to sample basal friction fields from a file.

Checklist

  • User's Guide has been updated
  • Developer's Guide has been updated
  • API documentation in the Developer's Guide (api.rst) has any new or modified class, method and/or functions listed
  • Documentation has been built locally and changes look as expected
  • Document (in a comment titled Testing in this PR) any testing that was used to verify the changes

Generalize ensemble generator to support multiple model configurations used
for different studies.
* Introduced a new configuration module to handle model configurations
  for ensemble generation.
* Updated `BranchRun` and `EnsembleMember` classes to accept a
  `resource_module` parameter for dynamic configuration loading.
* Created default configuration files for branch and spinup ensembles,
  including necessary namelists and stream definitions.
* Modified the main ensemble generator configuration to streamline the
  setup process and improve clarity.
* Enhanced error handling for missing configuration sections and
  options.
* Updated the `SpinupEnsemble` class to utilize the new configuration
  methods for improved modularity and maintainability.
Variations on the word configuration are already too widespread
so this change should reduce confusion.
The primary new functionality is the ability to support any
namelist option as a parameter rather than only pre-defined
parameters.  The refactor also simplifies how parameter values
are specified and puts all parameters in a dedicated cfg section.
More details on the format are included in the updated docs.
Remove unnecessary extra section level,
Separate spinup_ensemble options from options
general to the whole test group.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the landice/ensemble_generator test group to support template-based model configurations and a more flexible parameter-perturbation workflow (including generic float namelist perturbations configured entirely from cfg files), with corresponding documentation updates.

Changes:

  • Introduce ensemble_templates/<name>/spinup and .../branch packages to manage per-study cfg/namelist/streams/(Albany) resources.
  • Replace hard-coded sampled-parameter logic with ordered definitions in [ensemble.parameters], including generic nl.* namelist float perturbations.
  • Update User’s/Developer’s Guide docs and Landice API docs to reflect the new workflow and helper functions.

Reviewed changes

Copilot reviewed 12 out of 21 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
docs/users_guide/landice/test_groups/ensemble_generator.rst Documents new parameter types, template workflow, and updated cfg structure
docs/developers_guide/landice/test_groups/ensemble_generator.rst Describes new template package layout and updated configuration behavior
docs/developers_guide/landice/api.rst Adds new ensemble_template.* functions to the API docs list
compass/landice/tests/ensemble_generator/spinup_ensemble/init.py Loads template cfg and parses ordered [ensemble.parameters] including nl.* handling
compass/landice/tests/ensemble_generator/ensemble_template.py New helpers for selecting and loading template packages/files
compass/landice/tests/ensemble_generator/ensemble_member.py Applies generic namelist option overrides; loads resources from selected template
compass/landice/tests/ensemble_generator/branch_ensemble/init.py Loads branch template cfg and passes template resources into branch runs
compass/landice/tests/ensemble_generator/branch_ensemble/branch_run.py Uses template-provided namelist/streams; makes albany_input.yaml optional
compass/landice/tests/ensemble_generator/plot_ensemble.py Updates cfg section usage to match new split between ensemble_generator and spinup_ensemble
compass/landice/tests/ensemble_generator/ensemble_templates/default/** Adds default template cfg/namelist/streams/(Albany) resources for spinup and branch
Comments suppressed due to low confidence (2)

compass/landice/tests/ensemble_generator/ensemble_templates/default/spinup/ensemble_generator.cfg:5

  • The comment above ensemble_template says this is the subdirectory where branch_ensemble options are located, but this file is the spinup template (.../spinup/ensemble_generator.cfg). This is misleading—update the comment to refer to spinup/ensemble_generator options (or to templates in general).
    compass/landice/tests/ensemble_generator/ensemble_templates/default/spinup/ensemble_generator.cfg:108
  • The template uses backslashes for line continuation in nl.von_mises_threshold.option_name. With the current parser (_split_entries()), the backslashes will be treated as literal entries and can produce an invalid namelist option name. Prefer ConfigParser-style multiline values (indented continuation lines without \) or keep the list on one line.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

This function was flagged as too complex, so Copilot helped me break it
into smaller functions.
Update ensemble generator parsing and docs based on outdated but still
relevant Copilot review feedback from PR MPAS-Dev#940.

- Sanitize multiline option parsing in spinup_ensemble._split_entries
  to remove continuation backslashes before tokenization.
- Use importlib.resources.as_file() when handling optional
  albany_input.yaml in ensemble_member setup.
- Clarify in developer docs that albany_input.yaml is copied only
  when present for Albany-based configurations.
- Fix users-guide cfg block indentation and multiline .option_name
  examples to match ConfigParser behavior.
@alexolinhager
Copy link
Copy Markdown

alexolinhager commented Mar 16, 2026

@matthewhoffman, I tested this PR using both the uniform and log-uniform sampling methods and it worked smoothly. I think it might be cleaner at this point to merge this PR first, then merge my SGH template PR separately.

One thing I did change in my SGH PR is making the basal melt param and the TF forcing files optional here: https://github.com/alexolinhager/compass/blob/3cfc6fc8536d9792324b964ff5bbbdd3901a1c43/compass/landice/tests/ensemble_generator/ensemble_member.py#L235-L257 to make it more generalizable to SGH configurations. I'm not sure if is better in the SGH PR or in this one.

As of now, I've just used this PR four times to create separate ensembles, each varying one SGH parameter while holding others constant. I'm not sure how much additional work it would be to include an option to do all of this within one ensemble but it could be useful. Unless I missed something, the script only automatically vary all parameters concurrently, but it could be nice to have the option to vary each parameter individually as well.

GitHub Copilot and others added 4 commits March 17, 2026 03:28
* Rename ensemble templates to use underscores instead of periods to
  avoid parsing issues
* Remove unneeded add_template_file function now that we are not loading
  cfgs from a template
* other minor cleanup
@matthewhoffman
Copy link
Copy Markdown
Member Author

Testing

In addition to the testing @alexolinhager described above, I have set up and run an ensemble with the Kanger basal friction template on perlmutter here: /pscratch/sd/h/hoffman2/KANGER_FRICTION_ENSEMBLES/20260317_postVarSamples
Other than confirming that ensembles can be set up and run, I can't think of other testing to do.

@matthewhoffman matthewhoffman force-pushed the landice/ensemble-gen-refactor branch from 72e96e5 to 9e966ec Compare March 29, 2026 17:58
The yaml parser caused two problems:
1. albany_input.yaml often contains keys without values which causes the
   parser to introduce values of 'null' which cause an error in Albany
2. the parser writes the file out in arbitrary order which is
   inconvenient

This commit replaces the yaml parser with the python string search and
replace, which eliminates both issues.
Previous value had been a temporary test value
This change saves the run number in the modified file's name, and it
allows periods in filenames prior to the final .nc
@matthewhoffman matthewhoffman requested a review from trhille March 29, 2026 19:56
@matthewhoffman
Copy link
Copy Markdown
Member Author

Testing (cont.)

I set up a Kanger friction ensemble with:

compass setup -n 35 -w /pscratch/sd/h/hoffman2/KANGER_FRICTION_ENSEMBLES/20260329_postVarSamples -f /global/cfs/cdirs/fanssie/users/hoffman2/compass/kanger-friction-refactored/compass/landice/tests/ensemble_generator/ensemble_templates/kanger_frictionuq_2026/spinup/ensemble_generator.cfg

I confirmed ensemble members run and seem to be doing what they're supposed to. The output is here:

/pscratch/sd/h/hoffman2/KANGER_FRICTION_ENSEMBLES/20260329_postVarSamples/landice/ensemble_generator/spinup_ensemble/run001

Also disable friction exponent for now, because it will ideally require
recalculating the velocity solution for each friction sample.

Also switch to depth-integrated solver
@matthewhoffman matthewhoffman force-pushed the landice/ensemble-gen-refactor branch from 44d2d26 to 75043ac Compare March 30, 2026 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants