Skip to content

Conversation

@curtischong
Copy link
Collaborator

@curtischong curtischong commented Nov 6, 2025

Summary

Before this change, we'd get this error

TypeError: torch_sim.runners.static.<locals>.StaticState() got multiple values for keyword argument 'energy'

This is because we were creating a static_state but passing in fields that already existed (because we optimized the state before calculating the static property). Since we used **vars() to initialize the static state, we'd pass properties like energies twice into the constructor for StaticState.

To fix this PR I also had to make StaticState's attributes be dependent on only SimState's attributes.
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.


_atom_attributes = state._atom_attributes | {"forces"} # noqa: SLF001
_system_attributes = state._system_attributes | { # noqa: SLF001
_atom_attributes = SimState._atom_attributes | {"forces"} # noqa: SLF001
Copy link
Collaborator Author

@curtischong curtischong Nov 6, 2025

Choose a reason for hiding this comment

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

I don't like changing the atom attributes from state to SimState. But I'm okay with it for now since torchsim currently only supports calculating energies, forces, and stresses.

When we support magnetic moments (and arbitrary properties in general) we will need to rethink how to create this

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The reason why I needed to set these atom attributes to be derived from SimState is because if I left it as state, then inside the trajectory reporter, it would think that StaticState would have extra attributes (that we did not initialize when we created it) the extra attribute in particular is velocity.

So by explicitly defining the _atom_attributes to be from SimState, we only say that this StaticState has a fixed limited set of attributes

Copy link
Collaborator

Choose a reason for hiding this comment

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

I plan to add mag moms soon so I'd prefer we pick a solution here that will be generalizable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I need to think through that some more as well. On another note, I'd prefer to do that in a different PR so this one can be constrained to fixing just this bug

@curtischong curtischong force-pushed the fix-static-state-after-relax branch 2 times, most recently from 6eca84b to 50d24c7 Compare November 6, 2025 02:17
… a test to document the fix

you can now calculate static properties after optimizing

better docs

fix prek
@curtischong curtischong force-pushed the fix-static-state-after-relax branch from 35a5619 to 5cb88ea Compare November 6, 2025 02:19
@curtischong curtischong marked this pull request as ready for review November 6, 2025 02:19
@curtischong curtischong added the bug Something isn't working label Nov 6, 2025
Copy link
Collaborator

@thomasloux thomasloux left a comment

Choose a reason for hiding this comment

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

agreed with the change, it's what we do in all other subclass of SimState.

@curtischong curtischong merged commit f87a704 into TorchSim:main Nov 6, 2025
139 checks passed
@curtischong curtischong deleted the fix-static-state-after-relax branch November 6, 2025 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants