Modelling#
At a high-level, OPynSim’s modelling API is designed around three classes with distinct roles:
opynsim.ModelSpecification: A high-level specification of the model. This is what Python code manipulates before callingopynsim.compile()to yield…opynsim.Model: A compiled read-only physics model, which can create/read/manipulate…opynsim.ModelState: A single state of anopynsim.Model. This can be manipulated with Python code to do something useful with the model.
API Reference#
- opynsim.import_osim_file(osim_file_path: str | os.PathLike) opynsim._core.ModelSpecification#
Returns a
ModelSpecificationimported from an OpenSim (.osim) file on the caller’s filesystem.- Raises:
RuntimeError – If the file cannot be found, read, or is invalid.
- opynsim.compile_specification(model_specification: opynsim._core.ModelSpecification) opynsim._core.Model#
Compiles a
ModelSpecificationinto aModel.The compilation process:
Validates the
ModelSpecification’s components (properties, subcomponents, and sockets), throwing an exception if the specification is invalid in some way.Assembles a physics system from the validated specification, throwing an exception if the physics system cannot be assembled (e.g. if the contains impossible-to-satisfy joints, or invalid muscle definitions).
- Raises:
RuntimeError – If the compilation process failed in some way. It is assumed that the provided
ModelSpecificationis valid.
- class opynsim.Model#
Bases:
objectA validated, optimized, compiled, and ready-to-simulate model of a physics system.
A
Modelcan only be created from aModelSpecificationvia thecompile_specification()function. Therefore, editing aModelrequires editing its associatedModelSpecificationand recompiling it to create a newModel.- initial_state(self) opynsim._core.ModelState#
Returns a
ModelStatethat represents the initial (default) state of thisModel.The initial state of a
Modelis dictated by its associatedModelSpecification. For example, if a translational coordinate in the specification had adefault_valueof1.0then that would be written into theModelStatereturned by this function.This function does not guarantee the returned
ModelState’sModelStateStage. Therefore, it’s recommended that callers perform any state modifications they need followed by callingrealize()with aModelStateStagethat’s suitable for their desired needs (e.g. user interfaces may needModelStateStage.REPORT).
- realize(self, model_state: opynsim._core.ModelState, model_state_stage: opynsim._core.ModelStateStage) None#
Realizes
model_stateto the desiredmodel_state_stage, which modifiesmodel_statein-place.“Realization” of the state involves taking a new set of values from the
ModelStateand performing computations that those new values enable. Realization is performed in-order oneModelStateStageat time. For example,ModelStateStage.POSITIONis realized beforeModelStateStage.VELOCITY,thenModelStateStage.DYNAMICS, and so on.Notes
State realization is a concept that OPynSim inherited from Simbody, which has a much more comprehensive explanation of the realization process in its Simbody Theory Manual. You should read that manual if you want to know more.
- set_coordinate_value(self, model_state: std::basic_string_view<char, std: :char_traits<char> >, coordinate_path: opynsim._core.ModelState, value: float) None#
Finds
coordinate_pathin the model and sets the corresponding state variable inmodel_statetovalue.Changing the value of a coordinate changes
model_state’sModelStateStagetoModelStateStage.POSITION, which means you may need to useModel.realize()to re-realize the state to a later stage if you subsequently intend on doing something else with the model (e.g. for rendering).
- class opynsim.ModelSpecification(*args, **kwargs)#
Bases:
objectA high-level specification for an
opynsim.Model.A
ModelSpecificationis what Python code can manipulate, scale, and customize before passing it toopynsim.compile_specification(), which returns a readonlyopynsim.Model.Notes
OPynSim’s API design separates the specification of a model (
ModelSpecification) from its validated, assembled, and optimized simulation representation (Model) to ensure that the compilation process (compile_specification()) can freeze and optimize internal datastructures at a single point in the process. This is in contrast to OpenSim, which handles both concerns with a singleOpenSim::Modelclass, which results in edge-cases, such as incorrectly being able to edit a model after a physics system has already been assembled from it.
- class opynsim.ModelState#
Bases:
objectRepresents a single state of a
Model.A
ModelStatebundles together the state variables, cache variables, and other information necessary to describe a single state of aModel.Model’s can read/manipulateModelStates in order toopynsim.Model.realize()the state to a later stage (e.g. as part of forward integration) or read outputs values. However,ModelStates may also be created, read, and manipulated by downstream Python code and other utilities in OPynSim.
- class opynsim.ModelStateStage(*values)#
Bases:
EnumRepresents a stage of state realization (computation).
When calling methods like
Model.realize(), aModelStateis realized in-order through eachModelStateStage, starting at the lowest stage and ending at the highest stage.Each time a
ModelStateadvances through aModelStateStage, more information is available in the state. For example, after aModelStateis realized toModelStateStage.POSITION, positional quantities such as the positions of bodies and offset frames are known, and any positional output on the associatedModelcan then extract that information from theModelState.- ACCELERATION = 4#
- DYNAMICS = 3#
- POSITION = 1#
- REPORT = 5#
- TIME = 0#
- VELOCITY = 2#