Karana.KUtils.Sim
=================

.. py:module:: Karana.KUtils.Sim

.. autoapi-nested-parse::

   Sim class and associated classes/functions.



Attributes
----------

.. autoapisummary::

   Karana.KUtils.Sim.bc


Classes
-------

.. autoapisummary::

   Karana.KUtils.Sim.PrefabRoot
   Karana.KUtils.Sim.Sim
   Karana.KUtils.Sim.SimDS


Module Contents
---------------

.. py:data:: bc

.. py:class:: PrefabRoot(name: str, config: ConfigType, context: ContextType, params: ParamsType, parent_prefab: Prefab[Any, Any, Any] | None = None)

   Bases: :py:obj:`Karana.KUtils.Prefab.Prefab`\ [\ :py:obj:`Karana.KUtils.Prefab.Config`\ , :py:obj:`Karana.KUtils.Prefab.Context`\ , :py:obj:`Karana.KUtils.Prefab.Params`\ ]


   Prefab to act as the virtual root of the Prefab hierarchy.


.. py:class:: Sim
              Sim(mb_ds: Karana.Dynamics.SOADyn_types.SubGraphDS | None, sp_ds: Karana.Dynamics.SOADyn_types.StatePropagatorDS | None)
              Sim(mb: Karana.Dynamics.Multibody, sp: Karana.Dynamics.StatePropagator)

   Bases: :py:obj:`Karana.Core.BaseWithVars`


   Simulation class.

   This class helps one easily setup a simulation complete with a
   Multibody and StatePropagator.

   It contains helper methods to do common tasks like setup graphics or the GUI.


   .. py:attribute:: prefab_root


   .. py:method:: fromDS(sim_ds: SimDS) -> Sim
      :classmethod:


      Create a Sim instance from its associated DataStruct.

      :param sim_ds: The DataStruct to create the Sim from.
      :type sim_ds: SimDS

      :returns: A new instance of Sim created from the provided DataStruct.
      :rtype: Sim



   .. py:method:: toDS() -> SimDS

      Convert this Sim into a SimDS.

      :returns: A DataStruct representation of this simulation.
      :rtype: SimDS



   .. py:method:: fromBasicPrefabDS(basic_prefab_ds: Karana.KUtils.BasicPrefab.BasicPrefabDS[Karana.KUtils.BasicPrefab.ExtraInfoType]) -> tuple[Sim, Karana.KUtils.BasicPrefab.BasicPrefab[Karana.KUtils.BasicPrefab.ExtraInfoType]]
      :classmethod:


      Create a Sim instance from its associated DataStruct.

      :param basic_prefab_ds: The BasicPrefabDS to create a Simulation from.
      :type basic_prefab_ds: BasicPrefabDS

      :returns: A new instance of Sim created from the provided BasicPrefabDS, and the
                associated BasicPrefab.
      :rtype: tuple[Sim, BasicPrefab[ExtraInfoType]]



   .. py:method:: setupGraphics(*, port: int = 29523, axes: float = 1.0, client_type: Literal['auto', 'electron', 'notebook', 'webbrowser', 'selenium'] | None = 'auto', origin_frame: Karana.Frame.Frame | None = None, wait_for_clients: int = 0, wait_for_clients_timeout: float = 0.0, stick_parts: Literal['auto', 'always', 'never'] = 'auto') -> tuple[Callable[[], None], Karana.Scene.WebScene]

      Set up the graphics for this simulation.

      :param port: Port to bind the WebUI server to. Use 0 to request an arbitrary unused
                   port.
      :type port: int = 29523
      :param axes: Length of axes visualization on root frame.
      :type axes: float = 1.0
      :param client_type:
                          Policy for launching a client.
                              "auto": pick the best option for the environment and OS
                              "electron": always launch the electron client
                              "notebook": always open an IFrame in IPython
                              "webbrowser": always open a browser tab
                              "selenium": always open a chrome driver using selenium
                              None: don't automatically open a client
                          Defaults to "auto".
      :type client_type: Literal["auto", "electron", "notebook", "webbrowser", "selenium"] | None
      :param origin_frame: Frame to use as the world origin for the graphics scene. If None, use
                           the Multibody's virtualRoot.
      :type origin_frame: Optional[Frame] = None
      :param wait_for_clients: Number of client connections to wait for before continuing.
      :type wait_for_clients: int = 0
      :param wait_for_clients_timeout: Number of seconds to wait before raising an error if wait_for_clients
                                       is positive.
      :type wait_for_clients_timeout: float = 0.0
      :param stick_parts:
                          Policy for creating stick parts. Defaults to "auto".
                              "auto": create stick parts if the ProxyScene is empty
                              "always": unconditionally create stick parts
                              "never": unconditionally don't create stick parts
      :type stick_parts: Literal["auto", "always", "never"] = "auto"

      :returns: A tuple containing the a cleanup callable and the graphics scene.
      :rtype: tuple[Callable, WebScene]



   .. py:method:: setupGui(port=29534, client_type: Literal['auto', 'electron', 'notebook', 'webbrowser', 'selenium'] | None = 'auto', wait_for_clients: int = 0, wait_for_clients_timeout: float = 0.0, stick_parts: Literal['auto', 'always', 'never'] = 'auto', stick_parts_config: Karana.Dynamics.StickPartsConfig | None = None, name_to_label_map: dict[str, str] | None = None, time_display_period: float | None = 0.01, graphics_origin_frame: Karana.Frame.Frame | None = None) -> Karana.Dynamics.SOADyn_types.MultibodyWebUI

      Create the GUI for this multibody system.

      :param self: The Sim instance
      :type self: Sim
      :param port: Port to bind the WebUI server to. Use 0 to request an arbitrary unused
                   port. Defaults to 29534.
      :type port: int
      :param client_type:
                          Policy for launching a client.
                              "auto": pick the best option for the environment and OS
                              "electron": always launch the electron client
                              "notebook": always open an IFrame in IPython
                              "webbrowser": always open a browser tab
                              "selenium": always open a chrome driver using selenium
                              None: don't automatically open a client
                          Defaults to "auto".
      :type client_type: Literal["auto", "electron", "notebook", "webbrowser", "selenium"] | None
      :param wait_for_clients: Number of client connections to wait for before continuing.
                               Defaults to 0.
      :type wait_for_clients: int
      :param wait_for_clients_timeout: Number of seconds to wait before raising an error if wait_for_clients
                                       is positive. Defaults to 0.
      :type wait_for_clients_timeout: float
      :param graphics_origin_frame: If given, and the GUI needs to setup graphics, use this as the
                                    origin frame instead of the multibody virtual root.
      :type graphics_origin_frame: Optional[Frame]
      :param stick_parts:
                          Policy for creating stick parts. Defaults to "auto".
                              "auto": create stick parts if the ProxyScene is empty
                              "always": unconditionally create stick parts
                              "never": unconditionally don't create stick parts
      :type stick_parts: Literal["auto", "always", "never"] = "auto"
      :param stick_parts_config: Configuration parameters for the stick parts.
      :type stick_parts_config: StickPartsConfig | None
      :param name_to_label_map: Dictionary defining the labels to use for each body in the visjs graphs
                                display
      :type name_to_label_map: dict[str, str] | None
      :param time_display_period: If not None and sp is given, add a time display refreshing
                                  with this period in simulation time. Defaults to 0.01.
      :type time_display_period: float | None



.. py:class:: SimDS(/, **data: Any)

   Bases: :py:obj:`Karana.KUtils.DataStruct.DataStruct`


   DataStruct representation of a simulation.

   :param sp_ds: The StatePropagator DataStruct.
   :type sp_ds: StatePropagatorDS
   :param mb_ds: The Multibody DataStruct.
   :type mb_ds: SubGraphDS
   :param setup_graphics: The GUI or graphics setup DataStruct.
   :type setup_graphics: GuiSetupDS | GraphicsSetupDS | None
   :param collision: The CollisionScene DataStructs.
   :type collision: list[SerializeAsAny[CollisionSceneDS[CollisionSceneType]]] = [] # pyright: ignore
   :param prefabs: The Prefab DataStructs.
   :type prefabs: list[SerializeAsAny[PrefabDS[ConfigType, ContextType, ParamsType]]]  # pyright: ignore


   .. py:attribute:: sp_ds
      :type:  Karana.Dynamics.SOADyn_types.StatePropagatorDS


   .. py:attribute:: mb_ds
      :type:  Karana.Dynamics.SOADyn_types.SubGraphDS


   .. py:attribute:: setup_graphics
      :type:  Optional[Karana.Dynamics.SOADyn_types.GuiSetupDS | Karana.Dynamics.SOADyn_types.GraphicsSetupDS]
      :value: None



   .. py:attribute:: collision
      :type:  list[pydantic.SerializeAsAny[Karana.Scene.Scene_types.CollisionSceneDS[Karana.Scene.Scene_types.CollisionSceneType]]]
      :value: []



   .. py:attribute:: prefabs
      :type:  list[pydantic.SerializeAsAny[Karana.KUtils.Prefab.PrefabDS[Karana.KUtils.Prefab.ConfigType, Karana.KUtils.Prefab.ContextType, Karana.KUtils.Prefab.ParamsType]]]


   .. py:method:: toSim() -> Sim

      Create a simulation from this SimDS.

      :returns: The Sim created from this SimDS.
      :rtype: Sim



