Skip to content

Accept strings for integrator, optimizer, and cell_filter - #620

Closed
akwarii wants to merge 1 commit into
TorchSim:mainfrom
akwarii:enum-as-str
Closed

akwarii wants to merge 1 commit into
TorchSim:mainfrom
akwarii:enum-as-str

Conversation

@akwarii

@akwarii akwarii commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Summary

ts.integrate, ts.optimize and ts.get_cell_filter (and therefore the cell_filter argument of the fire/gradient_descent/lbfgs/bfgs optimizers) now accept plain strings such as "nve", "fire" or "frechet". Strings are converted with Integrator(x) / Optimizer(x) / CellFilter(x) before the existing isinstance check. Enum members and (init_fn, step_fn) tuples behave exactly as before.

An unknown string is not converted and falls through to the existing error branch, so the error message is the same as for any other invalid input.

Motivation

Jobflow jobs and atomate2 makers are stored as JSON documents. Enum members and callables round-trip poorly, whereas strings are trivially serialized, stored and queried. With this change a maker can simply hold, e.g., optimizer="fire", cell_filter="frechet", integrator="nvt_langevin". This leads to:

  • No torch-sim imports needed to configure a workflow.
  • The settings recorded in the JSON document are directly reusable as inputs to re-run a calculation, with no enum<->string translation layer in each downstream package.
  • Less glue code. Currently we have to map user-facing strings to torch-sim enums per maker.

Moreover, the change is minimal and does not require additional maintenance since the new conditional branch will not fail before reaching the already existing ones (1 line below the new code block).

Checklist

Before a pull request can be merged, the following items must be checked:

  • Doc strings have been added in the Google docstring format.
  • Run ruff on your code.
  • Tests have been added for any new functionality or bug fixes.

@akwarii akwarii changed the title cast str to enum member Accept strings for integrator, optimizer, and cell_filter Oct 21, 2026

@lil-lon lil-lon left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for raising this!

From the torch-sim side, I'd prefer not to make these arguments accept str, since it weakens type checking: typos would only be caught at runtime.

Since Integrator, Optimizer, and CellFilter are StrEnums, converting from a string is already a one-liner.
I also noticed that atomate2 already does this conversion for optimizer in TorchSimOptimizeMaker.make (src/atomate2/torchsim/core.py).

Could you consider adding the same conversion in atomate2 for integrator and cell_filter instead of patching torch-sim?
To be clear, I understand this PR would save downstream packages a conversion step, but I think keeping the torch-sim API typed is worth that small cost.

@akwarii

akwarii commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback. I have no problem moving this to atomate2 if you prefer keeping stricter type hints

@akwarii

akwarii commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

The fix was moved to materialsproject/atomate2#1619 so I'm closing this PR.

@akwarii akwarii closed this Oct 23, 2026
@akwarii
akwarii deleted the enum-as-str branch October 23, 2026 13:13
@lil-lon

lil-lon commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

Thanks for your understanding! If the atomate2 side sees it differently, please feel free to ping me again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants