### Launch Web GUI Server Source: https://github.com/keenon/nimblephysics/wiki/2.-Building-and-running-code Commands to install dependencies and start the development server for the web GUI. ```bash # cd javascript # npm install # npm run dev ``` -------------------------------- ### Complete Optimization Loop Source: https://github.com/keenon/nimblephysics/blob/master/www/docs/backprop-through-physics.md A full example demonstrating world setup, GUI initialization, simulation stepping, and the optimization loop. ```python import torch import nimblephysics as nimble # Set up the world world = nimble.simulation.World() world.setGravity([0, -9.81, 0]) world.setTimeStep(0.01) # Set up the box box = nimble.dynamics.Skeleton() boxJoint, boxBody = box.createTranslationalJoint2DAndBodyNodePair() boxShape = boxBody.createShapeNode(nimble.dynamics.BoxShape([.1, .1, .1])) boxVisual = boxShape.createVisualAspect() boxVisual.setColor([0.5, 0.5, 0.5]) world.addSkeleton(box) # Set up initial conditions for optimization initial_position: torch.Tensor = torch.tensor([3.0, 0.0]) initial_velocity: torch.Tensor = torch.zeros((world.getNumDofs()), requires_grad=True) # Set up the GUI gui: nimble.NimbleGUI = nimble.NimbleGUI(world) gui.serve(8080) gui.nativeAPI().createSphere("goal", radius=0.1, pos=[0, 0, 0], color=[0, 255, 0]) while True: state: torch.Tensor = torch.cat((initial_position, initial_velocity), 0) states = [state] num_timesteps = 100 for i in range(num_timesteps): state = nimble.timestep(world, state, torch.zeros((world.getNumDofs()))) states.append(state) # This call will overwrite any previous set of states we were looping from # a previous iteration of gradient descent. gui.loopStates(states) # Our loss is just the distance to the origin at the final step final_position = state[:world.getNumDofs()] # Position is the first half of the state vector loss = final_position.norm() print('loss: '+str(loss)) loss.backward() # Manually update weights using gradient descent. Wrap in torch.no_grad() # because weights have requires_grad=True, but we don't need to track this # in autograd. with torch.no_grad(): learning_rate = 0.01 initial_velocity -= learning_rate * initial_velocity.grad initial_velocity.grad = None ``` -------------------------------- ### Complete mass tuning example Source: https://github.com/keenon/nimblephysics/blob/master/www/docs/system-identification.md A full implementation demonstrating the setup of a simulation world, registration of mass for optimization, and the training loop using gradient descent. ```default import torch import numpy as np import nimblephysics as nimble # Set up the world world = nimble.simulation.World() world.setGravity([0, -9.81, 0]) world.setTimeStep(0.01) # Set up initial conditions for optimization initial_position: torch.Tensor = torch.tensor([3.0, 0.0]) initial_velocity: torch.Tensor = torch.tensor([-3.0011, 4.8577]) mass: torch.Tensor = torch.tensor([1.0], requires_grad=True) # True mass is 2.0 goal: torch.Tensor = torch.Tensor([[2.4739, 2.4768]]) # We apply nonzero force so that mass can be determined from the trajectory. action: torch.Tensor = torch.tensor([10.0, 10.0]) # Set up the box box = nimble.dynamics.Skeleton() boxJoint, boxBody = box.createTranslationalJoint2DAndBodyNodePair() world.addSkeleton(box) bound = np.zeros((1,)) # This is not used, so we just pass in zeros world.getWrtMass().registerNode( boxBody, nimble.neural.WrtMassBodyNodeEntryType.MASS, bound, bound) while True: state: torch.Tensor = torch.cat((initial_position, initial_velocity), 0) states = [state] num_timesteps = 100 for i in range(num_timesteps): state = nimble.timestep(world, state, action, mass) states.append(state) # Our loss is just the distance to the origin at the final step final_position = state[:world.getNumDofs()] # Position is the first half of the state vector loss = (goal - final_position).norm() print('loss: '+str(loss)) loss.backward() # Manually update weights using gradient descent. Wrap in torch.no_grad() # because weights have requires_grad=True, but we don't need to track this # in autograd. with torch.no_grad(): learning_rate = 0.01 mass -= learning_rate * mass.grad mass.grad = None ``` -------------------------------- ### Install Project Files Source: https://github.com/keenon/nimblephysics/blob/master/dart/math/CMakeLists.txt Configures the installation of header files. It installs the main headers and the generated math header to 'include/dart/math', and detail headers to 'include/dart/math/detail'. Files are installed as part of the 'headers' component. ```cmake install( FILES ${hdrs} ${CMAKE_CURRENT_BINARY_DIR}/math.hpp DESTINATION include/dart/math COMPONENT headers ) install( FILES ${detail_hdrs} DESTINATION include/dart/math/detail COMPONENT headers ) ``` -------------------------------- ### Install Project Files Source: https://github.com/keenon/nimblephysics/blob/master/dart/constraint/CMakeLists.txt Defines installation rules for header files and their corresponding detail implementations. ```cmake # Install install( FILES ${hdrs} ${CMAKE_CURRENT_BINARY_DIR}/constraint.hpp DESTINATION include/dart/constraint COMPONENT headers ) install( FILES ${detail_hdrs} DESTINATION include/dart/constraint/detail COMPONENT headers ) ``` -------------------------------- ### Inspect Python Package Metadata Source: https://github.com/keenon/nimblephysics/wiki/2.-Building-and-running-code Example output from the pip show command to identify the installation location. ```text Name: nimblephysics Version: 0.6.15.1 Summary: A differentiable fully featured physics engine Home-page: UNKNOWN Author: Keenon Werling Author-email: keenonwerling@gmail.com License: MIT Location: /home//.local/lib/python3.8/site-packages Requires: torch, numpy Required-by: ``` -------------------------------- ### Install Project Files Source: https://github.com/keenon/nimblephysics/blob/master/dart/server/CMakeLists.txt Defines the installation rules for header files to the specified destination. ```cmake # Install install( FILES ${hdrs} ${CMAKE_CURRENT_BINARY_DIR}/server.hpp DESTINATION include/dart/server COMPONENT headers ) ``` -------------------------------- ### Install Nimble Physics Source: https://context7.com/keenon/nimblephysics/llms.txt Use pip to install the library for immediate access to the engine. ```bash pip3 install nimblephysics ``` -------------------------------- ### Install Project Headers Source: https://github.com/keenon/nimblephysics/blob/master/dart/simulation/CMakeLists.txt Install the project's header files to the appropriate destination directories. This ensures that users can include the library's headers when using the installed package. ```cmake install( FILES ${hdrs} ${CMAKE_CURRENT_BINARY_DIR}/simulation.hpp DESTINATION include/dart/simulation COMPONENT headers ) install( FILES ${detail_hdrs} DESTINATION include/dart/simulation/detail COMPONENT headers ) ``` -------------------------------- ### Install Project Files with CMake Source: https://github.com/keenon/nimblephysics/blob/master/dart/collision/dart/CMakeLists.txt The `install` command is used to copy header files and the generated `dart.hpp` to the destination directory. This ensures that the project's headers are available when the library is installed. ```cmake install( FILES ${hdrs} ${CMAKE_CURRENT_BINARY_DIR}/dart.hpp DESTINATION include/dart/collision/dart COMPONENT headers ) ``` -------------------------------- ### Define Installation Rules Source: https://github.com/keenon/nimblephysics/blob/master/dart/common/CMakeLists.txt Specifies the destination paths for headers and generated files during the installation process. ```cmake install( FILES ${hdrs} ${CMAKE_CURRENT_BINARY_DIR}/common.hpp DESTINATION include/dart/common COMPONENT headers ) install( FILES ${detail_hdrs} DESTINATION include/dart/common/detail COMPONENT headers ) ``` -------------------------------- ### Install Sphinx and Read The Docs Theme Source: https://github.com/keenon/nimblephysics/wiki/5.-Adding-pages-to-the-website Use pip to install the required documentation tools. Ensure you are using pip3 for Python 3. ```bash pip3 install sphinx sphinx_rtd_theme ``` -------------------------------- ### Install Python Wheel Source: https://github.com/keenon/nimblephysics/wiki/2.-Building-and-running-code Command to install a custom-built wheel while ensuring it overrides existing installations. ```bash --force-reinstall ``` -------------------------------- ### Initialize Simulation and GUI Source: https://github.com/keenon/nimblephysics/blob/master/python/new_examples/test.ipynb Sets up a physics world with gravity, loads URDF skeletons, and starts a NimbleGUI server for rendering. ```python import nimblephysics as nimble import torch import numpy as np from IPython.core.display import display, HTML world = nimble.simulation.World() world.setGravity([0, -9.81, 0]) # Set up the 2D cartpole arm: nimble.dynamics.Skeleton = world.loadSkeleton("../../data/urdf/KR5/KR5 sixx R650.urdf") ground: nimble.dynamics.Skeleton = world.loadSkeleton("../../data/sdf/atlas/ground.urdf") floorBody: nimble.dynamics.BodyNode = ground.getBodyNode(0) floorBody.getShapeNode(0).getVisualAspect().setCastShadows(False) ticker = nimble.realtime.Ticker(world.getTimeStep()) goal_x = 0.0 goal_y = 0.8 goal_z = -1.0 goal: torch.Tensor = torch.tensor([goal_x, goal_y, goal_z]) gui = nimble.NimbleGUI(world) gui.serve(8081) gui.nativeAPI().renderWorld(world, "world") gui.nativeAPI().createSphere("goal_pos", 0.1, np.array( [goal_x, goal_y, goal_z]), np.array([0.0, 1.0, 0.0]), True, False) display(HTML('