AGX Dynamics  2.37.3.2
agxSDK/EnergyManager.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. 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 
83  agx::Real getPotentialChange( const agx::RigidBody* rb ) const;
84 
91  agx::Real getDissipation( const agx::RigidBody* rb ) const;
92 
98  agx::Real getPotentialChange( const agx::Constraint* constraint ) const;
99 
105  agx::Real getDissipation( const agx::Constraint* constraint ) const;
106 
112  agx::Real getPotentialChange( const agx::ElementaryConstraint* constraint ) const;
113 
119  agx::Real getDissipation( const agx::ElementaryConstraint* constraint ) const;
120 
126  agx::Real getPower( const agx::ElementaryConstraint* motor ) const;
127 
133  agx::Real getKineticEnergyChange( const agx::RigidBody* rb ) const;
134 
140  agx::Real getTranslationalEnergyChange( const agx::RigidBody* rb ) const;
141 
147  agx::Real getRotationalEnergyChange( const agx::RigidBody* rb ) const;
148 
154  static agx::Real getKineticEnergy( const agx::RigidBody* rb );
155 
161  static agx::Real getTranslationalEnergy( const agx::RigidBody* rb );
162 
168  static agx::Real getRotationalEnergy( const agx::RigidBody* rb );
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 }
AGXTERRAIN_EXPORT void clear()
Derive from this class to implement a listener for simulation step events.
Elementary constraint base class with interface and global constraint functionality.
bool Bool
Definition: Integer.h:48
Jacobian6DOFElementT< Vec3 > Jacobian6DOFElement
Definition: Jacobian.h:273
Keeps track of the energy flow in the system.
#define AGXPHYSICS_EXPORT
The base class for a constraint.
Definition: Constraint.h:88
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
agx::Real TimeStamp
Definition: TimeStamp.h:26
Type-specific Array used for fast access into the data held by a Buffer.
Definition: agxData.h:30
Base class providing referencing counted objects.
Definition: Referenced.h:119
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:40
Templated vector class.
Definition: Vector.h:52
uint32_t UInt
Definition: Integer.h:34
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Contains classes for low level data storage for AGX.
Definition: Container.h:23
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
#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
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
agx::observer_ptr< Simulation > SimulationObserver
double Real
Definition: Real.h:42
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97