AGX Dynamics  2.38.0.2
WindAndWaterAlgorithms.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 #ifndef AGXMODEL_WINDANDWATERALGORITHMS_H
18 #define AGXMODEL_WINDANDWATERALGORITHMS_H
19 
21 
22 namespace agxModel {
23  template<typename T>
25  {
26  if ( objectData.getRigidBody() == nullptr )
27  return;
28 
29  auto triangleUpdate = [ & ] ( const agx::RangeJob& job )
30  {
31  agx::Vec3 force;
32  agx::Vec3 torque;
33  for ( size_t k = job.range().begin(); k < job.range().end(); ++k ) {
34  size_t i = k + objectData.getStartIndexT();
35  GlobalTriangleData& tData = trianglePool[ i ];
36  if ( tData.isClipped() )
37  for ( agx::UInt j = 0; j < tData.clipped.size(); ++j )
38  obj->calculate( tData.clipped[ j ], objectData, force, torque );
39  else
40  obj->calculate( tData, objectData, force, torque );
41  }
42  objectData.lock();
43  objectData.force() += force;
44  objectData.torque() += torque;
45  switch (obj->getForceType())
46  {
47  case WindAndWaterController::ForceType::BUOYANCY:
48  {
49  objectData.forceComponent(WindAndWaterController::ForceType::BUOYANCY) += force;
50  objectData.torqueComponent(WindAndWaterController::ForceType::BUOYANCY) += torque;
51  break;
52  }
53  case WindAndWaterController::ForceType::PRESSURE_DRAG:
54  {
55  objectData.forceComponent(WindAndWaterController::ForceType::PRESSURE_DRAG) += force;
56  objectData.torqueComponent(WindAndWaterController::ForceType::PRESSURE_DRAG) += torque;
57  break;
58  }
59  case WindAndWaterController::ForceType::VISCOUS_DRAG:
60  {
61  objectData.forceComponent(WindAndWaterController::ForceType::VISCOUS_DRAG) += force;
62  objectData.torqueComponent(WindAndWaterController::ForceType::VISCOUS_DRAG) += torque;
63  break;
64  }
65  case WindAndWaterController::ForceType::LIFT:
66  {
67  objectData.forceComponent(WindAndWaterController::ForceType::LIFT) += force;
68  objectData.torqueComponent(WindAndWaterController::ForceType::LIFT) += torque;
69  break;
70  }
72  {
75  break;
76  }
77  default:
78  LOGGER_WARNING() << "Unknown force type in WindAndWaterAlgorithms: " << obj->getForceType() << LOGGER_ENDL();
79  }
80  objectData.unLock();
81  };
82 
83  auto sizeCallback = [ & ] ()
84  {
85  return objectData.getNumTriangles();
86  };
87 
88  if ( objectData.getNumTriangles() > 500 ) {
89  agx::LambdaKernelRef computeKernel = new agx::LambdaKernel( "ComputeKernel", triangleUpdate, sizeCallback );
90  computeKernel->execute();
91  }
92  else {
93  agx::RangeJob range;
94  range.init( agx::IndexRange( 0, objectData.getNumTriangles() ) );
95  triangleUpdate( range );
96  }
97  }
98 
99  class AGXMODEL_EXPORT BuoyancyAlgorithm : public agxModel::WindAndWaterController::Algorithm
100  {
101  public:
103 
105  const GlobalTriangleData::Container& container ) override
106  {
107  triangleIterator( this, objectData, container );
108  }
109 
110  void calculate( const agxModel::TriangleData& tData,
112  agx::Vec3& force,
113  agx::Vec3& torque ) const;
114 
115  void pre( agxModel::WindAndWaterController* ) override;
116 
117  virtual WindAndWaterController::ForceType getForceType() const override { return WindAndWaterController::ForceType::BUOYANCY; }
118 
119  protected:
120  virtual ~BuoyancyAlgorithm();
121 
123 
124  // Used for scaling buoyancy forces for primitive shapes.
125  class VolumeScalingDataContainer {
126  public:
127  agx::Real getVolumeScaling( const agxCollide::Shape* shape,
129 
130  void garbageCollect();
131 
132  private:
133  struct Data
134  {
135  Data( agx::Real volumeScaling,
136  agx::UInt32 modifiedCount,
138  const agxCollide::Shape* shape )
139  : volumeScaling( volumeScaling )
140  , modifiedCount( modifiedCount )
141  , tessellation( tessellation )
142  , shape( shape )
143  {
144  }
145 
146  agx::Real volumeScaling;
147  agx::UInt32 modifiedCount;
150  };
151 
153  Container m_data;
154  std::mutex m_mutex;
155  };
156 
158 
159  mutable VolumeScalingDataContainer m_volumeScaling;
160  };
161 
163  {
164  public:
166 
167  virtual void updateInteraction(agxModel::WindAndWaterController::ObjectData& objectData,
168  const GlobalTriangleData::Container& container) override;
169 
170  void calculate(const agxModel::TriangleData& tData,
172  agx::Vec3& force,
173  agx::Vec3& torque);
174 
175  protected:
176  virtual ~CenterOfBuoyancyAlgorithm();
177 
178  agx::Real calculateTetrahedronVolume(const agx::Vec3& edge0, const agx::Vec3& edge1, const agx::Vec3& edge2) const;
179 
180  void calculatePyramidCenterOfBuoyancy(agx::Vec3* basePoints, agx::Vec3* shiftedPoints, agx::Real& vol, agx::Vec3& pos);
181 
182  void calculateLocalCenterOfBuoyancy(const TriangleData& tData, const agx::Vec3& up, agx::Real& vol, agx::Vec3& pos);
183 
186  bool m_enabled;
187  };
188 
189  class AGXMODEL_EXPORT PressureDragForceAlgorithm : public agxModel::WindAndWaterController::Algorithm
190  {
191  public:
193 
195  const GlobalTriangleData::Container& container ) override
196  {
197  triangleIterator( this, objectData, container );
198  }
199 
200  void calculate( agxModel::TriangleData& tData,
202  agx::Vec3& force,
203  agx::Vec3& torque ) const;
204 
205  virtual WindAndWaterController::ForceType getForceType() const override { return WindAndWaterController::ForceType::PRESSURE_DRAG; }
206 
207  protected:
208  virtual ~PressureDragForceAlgorithm();
209  };
210 
211  class AGXMODEL_EXPORT LiftForceAlgorithm : public agxModel::WindAndWaterController::Algorithm
212  {
213  public:
215 
217  const GlobalTriangleData::Container& container ) override
218  {
219  triangleIterator( this, objectData, container );
220  }
221 
222  void calculate( agxModel::TriangleData& tData,
224  agx::Vec3& force,
225  agx::Vec3& torque ) const;
226 
227  virtual WindAndWaterController::ForceType getForceType() const override { return WindAndWaterController::ForceType::LIFT; }
228 
229  protected:
230  virtual ~LiftForceAlgorithm();
231  };
232 
233  class AGXMODEL_EXPORT ViscousDragForceAlgorithm : public agxModel::WindAndWaterController::Algorithm
234  {
235  public:
237 
239  const GlobalTriangleData::Container& container ) override
240  {
241  triangleIterator( this, objectData, container );
242  }
243 
244  void calculate( agxModel::TriangleData& tData,
246  agx::Vec3& force,
247  agx::Vec3& torque ) const;
248 
249  virtual WindAndWaterController::ForceType getForceType() const override { return WindAndWaterController::ForceType::VISCOUS_DRAG; }
250  protected:
251  virtual ~ViscousDragForceAlgorithm();
252  };
253 
254  class AGXMODEL_EXPORT AddedMassAlgorithm : public agxModel::WindAndWaterController::Algorithm
255  {
256  public:
258 
259  virtual void updateInteraction( agxModel::WindAndWaterController::ObjectData& objectData,
260  const GlobalTriangleData::Container& container ) override;
261 
262  protected:
263  virtual ~AddedMassAlgorithm();
264  };
265 }
266 
267 #endif // AGXMODEL_WINDANDWATERALGORITHMS_H
agxModel::BuoyancyAlgorithm::updateInteraction
virtual void updateInteraction(agxModel::WindAndWaterController::ObjectData &objectData, const GlobalTriangleData::Container &container) override
Definition: WindAndWaterAlgorithms.h:104
agxModel::WindAndWaterController::ObjectData
Static data for a shape.
Definition: WindAndWaterController.h:198
LOGGER_WARNING
#define LOGGER_WARNING()
Definition: Logger.h:23
agxModel::WindAndWaterController::ObjectData::getStartIndexT
agx::UInt getStartIndexT() const
agx::UInt
uint64_t UInt
Definition: Integer.h:27
agxModel::ViscousDragForceAlgorithm
Definition: WindAndWaterAlgorithms.h:233
agx::Task::execute
void execute()
Execute the task.
agx::Real
double Real
Definition: Real.h:42
agxModel::WindAndWaterController::ObjectData::getRigidBody
const agx::RigidBody * getRigidBody() const
agxModel
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: FlowUnit.h:36
agx::Vector< GlobalTriangleData >
agxModel::WindAndWaterController::ObjectData::forceComponent
agx::Vec3 & forceComponent(ForceType type)
agx::Container::size
size_t size() const
Definition: Container.h:134
agxModel::CenterOfBuoyancyAlgorithm
Definition: WindAndWaterAlgorithms.h:162
DOXYGEN_END_INTERNAL_BLOCK
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
agx::LambdaKernel
Definition: Kernel.h:173
agxModel::ViscousDragForceAlgorithm::updateInteraction
virtual void updateInteraction(agxModel::WindAndWaterController::ObjectData &objectData, const GlobalTriangleData::Container &container) override
Definition: WindAndWaterAlgorithms.h:238
DOXYGEN_START_INTERNAL_BLOCK
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
agxModel::WindAndWaterController::ForceType
ForceType
Definition: WindAndWaterController.h:50
agxStream::ElementTypes::OTHER
@ OTHER
Definition: Types.h:38
agxModel::AddedMassAlgorithm
Definition: WindAndWaterAlgorithms.h:254
agxModel::BuoyancyAlgorithm::getForceType
virtual WindAndWaterController::ForceType getForceType() const override
Definition: WindAndWaterAlgorithms.h:117
agxModel::GlobalTriangleData
Global triangle data (mapped with agxCollide::Mesh object) holding vertex indices.
Definition: WindAndWaterUtils.h:116
agxModel::CenterOfBuoyancyAlgorithm::m_enabled
bool m_enabled
Definition: WindAndWaterAlgorithms.h:186
agx::observer_ptr< const Shape >
agxModel::LiftForceAlgorithm
Definition: WindAndWaterAlgorithms.h:211
agx::IndexRangeT< agx::UInt >
agxModel::WindAndWaterController::ObjectData::unLock
void unLock() const
Perform a mutex unlock for this ObjectData.
Definition: WindAndWaterController.h:400
agxModel::WindAndWaterController::ObjectData::torqueComponent
agx::Vec3 & torqueComponent(ForceType type)
agx::ref_ptr< LambdaKernel >
agxModel::WindAndWaterController
Controller that collects relevant data and executes algorithms (probably mostly aero- and hydrodynami...
Definition: WindAndWaterController.h:45
agxCollide::Shape
Base class for a shape.
Definition: Shape.h:53
agx::RangeJob::init
void init(const RangeCallback &callback, const IndexRange &range, agx::UInt32 costPerElement=1)
Definition: Job.h:635
agxModel::WindAndWaterController::ObjectData::force
agx::Vec3 & force()
Access force vector (in world coordinate system).
agx::RangeJob
A range job executes a kernel implementation on a subset of the data.
Definition: Job.h:283
agxModel::WindAndWaterController::ObjectData::getNumTriangles
agx::UInt getNumTriangles() const
agxModel::CenterOfBuoyancyAlgorithm::m_waterVolume
agx::Real m_waterVolume
Definition: WindAndWaterAlgorithms.h:185
agxModel::CenterOfBuoyancyAlgorithm::m_center
agx::Vec3 m_center
Definition: WindAndWaterAlgorithms.h:184
agxModel::BuoyancyAlgorithm
Definition: WindAndWaterAlgorithms.h:99
agxModel::WindAndWaterParameters::ShapeTessellation
ShapeTessellation
Tessellation levels of native, non-mesh shapes, e.g, sphere, capsule and cylinder.
Definition: WindAndWaterParameters.h:35
WindAndWaterController.h
agxModel::PressureDragForceAlgorithm::getForceType
virtual WindAndWaterController::ForceType getForceType() const override
Definition: WindAndWaterAlgorithms.h:205
agxModel::LiftForceAlgorithm::getForceType
virtual WindAndWaterController::ForceType getForceType() const override
Definition: WindAndWaterAlgorithms.h:227
agxModel::TriangleData::isClipped
agx::Bool isClipped() const
Definition: WindAndWaterUtils.h:153
AGXMODEL_EXPORT
#define AGXMODEL_EXPORT
Definition: agxModel/export.h:42
LOGGER_ENDL
#define LOGGER_ENDL()
Definition: Logger.h:27
agxModel::PressureDragForceAlgorithm
Definition: WindAndWaterAlgorithms.h:189
agxModel::triangleIterator
void triangleIterator(T *obj, WindAndWaterController::ObjectData &objectData, const GlobalTriangleData::Container &trianglePool)
Definition: WindAndWaterAlgorithms.h:24
agx::Vec3T< Real >
agxModel::WindAndWaterController::ObjectData::lock
void lock() const
Perform a mutex lock for this ObjectData.
Definition: WindAndWaterController.h:395
agxModel::ViscousDragForceAlgorithm::getForceType
virtual WindAndWaterController::ForceType getForceType() const override
Definition: WindAndWaterAlgorithms.h:249
agx::UInt32
uint32_t UInt32
Definition: Integer.h:32
agxModel::PressureDragForceAlgorithm::updateInteraction
virtual void updateInteraction(agxModel::WindAndWaterController::ObjectData &objectData, const GlobalTriangleData::Container &container) override
Definition: WindAndWaterAlgorithms.h:194
agxModel::TriangleData
Triangle data for a normal or a clipped triangle.
Definition: WindAndWaterUtils.h:60
agx::HashVector
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:40
agxModel::GlobalTriangleData::clipped
agxModel::TriangleData::Container clipped
Container with clipped triangles if this global triangle has been clipped.
Definition: WindAndWaterUtils.h:129
agxModel::LiftForceAlgorithm::updateInteraction
virtual void updateInteraction(agxModel::WindAndWaterController::ObjectData &objectData, const GlobalTriangleData::Container &container) override
Definition: WindAndWaterAlgorithms.h:216
agxModel::WindAndWaterController::ObjectData::torque
agx::Vec3 & torque()
Access torque vector (in world coordinate system).