AGX Dynamics  2.35.0.1
agxSDK/EnergyManager.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/agxPhysics_export.h>
20 #include <agx/RigidBody.h>
21 
26 #include <agx/Jacobian.h>
27 #include <agx/Constraint.h>
29 
31 
32 // Forward declarations
33 namespace agxSDK
34 {
35  class Simulation;
37 }
38 
39 namespace agxSDK
40 {
42 
47  {
48 
49  public:
51 
52  public:
53 
58  void add( const agx::RigidBody* rigidBody );
59 
64  void add( const agx::Constraint* constraint );
65 
70  void remove( const agx::RigidBody* rigidBody );
71 
76  void remove( const agx::Constraint* constraint );
77 
84 
92 
98  agx::Real getPotentialChange( const agx::Constraint* constraint ) const;
99 
105  agx::Real getDissipation( const agx::Constraint* constraint ) const;
106 
113 
120 
127 
134 
141 
148 
155 
162 
169 
174  void calculateWorkFromExternalForces( const agx::Physics::RigidBodyData& rigidBody );
178  void calculateWorkFromBinaryConstraints( const agx::Physics::BinaryConstraintData& binaryConstraint,
179  const agx::Physics::ConstraintRowData& binaryConstraintRow,
180  const agxData::Array< agx::Jacobian6DOFElement >& binaryConstraintJacobian,
181  const agx::Physics::RigidBodyData& rigidBody );
185  void calculateWorkFromManyBodyConstraints( const agx::Physics::ManyBodyConstraintData& manyBodyConstraint,
186  const agx::Physics::ConstraintRowData& manyBodyConstraintRow,
187  const agxData::Array< agx::Jacobian6DOFElement >& manyBodyConstraintJacobian,
188  const agx::Physics::RigidBodyData& rigidBody );
189 
193  void updateData();
194 
198  void resetEnergy();
199 
200 
201  protected:
202  // Default constructor
203  EnergyManager() = default;
204 
208  virtual ~EnergyManager();
209 
210  private:
211  class RigidBodyData
212  {
213  public:
214  RigidBodyData( const agx::RigidBody* rb );
215 
216  agx::Vec3 velocity() const;
217  agx::Vec3 angularVelocity() const;
218 
219  agx::Bool update();
220 
221  private:
222  agx::Vec3 m_velocity;
223  agx::Vec3 m_angularVelocity;
225  };
226 
227  struct EnergyChange
228  {
229  EnergyChange( agx::Real initValue = 0 )
230  : potential( initValue ), dissipation( initValue )
231  {
232  }
233 
234  agx::Real potential;
235  agx::Real dissipation;
236 
237  void reset()
238  {
239  potential = 0;
240  dissipation = 0;
241  };
242  };
243 
248  using RigidBodyConstRefVector = agx::Vector< agx::RigidBodyConstRef >;
249  using ConstraintConstRefVector = agx::Vector< agx::ConstraintConstRef >;
250 
251  AGX_DECLARE_POINTER_TYPES( UpdateListener );
252 
253  class UpdateListener : public agxSDK::StepEventListener
254  {
255  public:
256  UpdateListener( EnergyManager* energyManager );
257  virtual void pre( const agx::TimeStamp& t ) override;
258 
259  protected:
260  virtual ~UpdateListener();
261 
262  private:
263  EnergyManagerObserver m_energyManager;
264  };
265 
266  private:
267  friend class Simulation;
268 
272  EnergyManager( agxSDK::Simulation* simulation );
273 
277  void setSimulation( agxSDK::Simulation* simulation );
278 
282  void clear();
283 
284  template<class T>
285  void calculateEnergyForElementaryConstraint( agx::Real& potential, agx::Real& dissipation,
286  ConstraintEnergyData* data, agx::UInt numRows, agx::Bool isActive,
287  agx::UInt& rowIndex, const T& rbIndices,
288  const agx::Physics::ConstraintRowData& binaryConstraintRow,
289  agx::UInt& gIndex,
290  const agxData::Array< agx::Jacobian6DOFElement >& binaryConstraintJacobian,
291  const agx::Physics::RigidBodyData& rigidBody );
292 
293  void calculateWorkOnBody( agx::Real& potential, agx::Real& dissipation, const EnergyManager::RigidBodyData& rbData,
294  const agx::Vec3& v, const agx::Vec3& w, agx::Bool isHolonomic, agx::Real lambdaDot,
295  agx::Real tau, const agx::Jacobian6DOFElement& G, agx::Real lambda );
296 
298 
299  private:
300  RigidBodyDataContainer m_rbData;
301  EnergyChangeBodyContainer m_energyChangeBodies;
302  EnergyChangeConstraintContainer m_energyChangeConstraints;
303  EnergyChangeScContainer m_energyChangeSc;
304 
305  RigidBodyConstRefVector m_rigidBodies;
306  ConstraintConstRefVector m_constraints;
307 
308  agxSDK::SimulationObserver m_simulation;
309  UpdateListenerRef m_updateListener;
310  };
311 }
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXSTREAM_DECLARE_SERIALIZABLE(T)
Use this in a Serializable class to add the required methods Important: Use full namespace in the dec...
Definition: Serializable.h:207
#define AGXPHYSICS_EXPORT
Type-specific Array used for fast access into the data held by a Buffer.
Definition: Array.h:107
Keeps track of the energy flow in the system.
agx::Real getDissipation(const agx::Constraint *constraint) const
Get the dissipation for a constraint during the last timestep.
void add(const agx::Constraint *constraint)
Enables energy calculations for a constraint.
agx::Real getPotentialChange(const agx::RigidBody *rb) const
Get the change in gravity potential for a rigid body during the last timestep.
agx::Real getRotationalEnergyChange(const agx::RigidBody *rb) const
Get the change in kinetic energy from the rotational motion for a rigid body during the last time ste...
static agx::Real getRotationalEnergy(const agx::RigidBody *rb)
Get the rotational part of the kinetic energy of a rigid body.
agx::Real getTranslationalEnergyChange(const agx::RigidBody *rb) const
Get the change in kinetic energy from the translational motion for a rigid body during the last time ...
agx::Real getPotentialChange(const agx::Constraint *constraint) const
Get the change in potential for a constraint during the last timestep.
void remove(const agx::Constraint *constraint)
Removes the constraint from the EnergyManager.
agx::Real getKineticEnergyChange(const agx::RigidBody *rb) const
Get the change in kinetic energy for a rigid body during the last time step.
agx::Real getPotentialChange(const agx::ElementaryConstraint *constraint) const
Get the change in potential for a secondary constraint during the last timestep.
void remove(const agx::RigidBody *rigidBody)
Removes the rigid body from the EnergyManager.
static agx::Real getKineticEnergy(const agx::RigidBody *rb)
Get the kinetic energy for a rigid body.
agx::Real getDissipation(const agx::RigidBody *rb) const
Get the dissipation from linear and angular velocity damping and external forces.
void add(const agx::RigidBody *rigidBody)
Enables energy calculations for a rigid body.
agx::Real getDissipation(const agx::ElementaryConstraint *constraint) const
Get the dissipation for a secondary constraint during the last timestep.
agx::Real getPower(const agx::ElementaryConstraint *motor) const
Get the power from a constraint motor.
static agx::Real getTranslationalEnergy(const agx::RigidBody *rb)
Get the translational part of the kinetic energy of a rigid body.
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:127
Derive from this class to implement a listener for simulation step events.
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:45
The base class for a constraint.
Definition: Constraint.h:89
Elementary constraint base class with interface and global constraint functionality.
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:41
Base class providing referencing counted objects.
Definition: Referenced.h:114
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
Templated vector class.
Definition: agx/Vector.h:53
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
Contains classes for low level data storage for AGX.
Definition: Container.h:23
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
agx::observer_ptr< Simulation > SimulationObserver
agx::observer_ptr< EnergyManager > EnergyManagerObserver
AGXTERRAIN_EXPORT void clear()
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Jacobian6DOFElementT< Vec3 > Jacobian6DOFElement
Definition: agx/Jacobian.h:273
bool Bool
Definition: Integer.h:48
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42
agx::Real TimeStamp
Definition: TimeStamp.h:26