FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
InternalCable.h
1
12
13#ifndef CInternalCable_H
14#define CInternalCable_H
15
16// Includes
17#include <marenv/marenv.h>
18#include "cable/subroutines/CableElDynStiff.h"
19
20#include "sfh/constants.h"
21#include "sfh/linalg.h"
22#include "sfh/math/array.h"
23#include "sfh/math/math.h"
24#include "sfh/timers/StopWatch.h"
25
26#include <fhsim/simobject/SimObject.h>
27#include <fhsim_environment/EnvironmentProvider.h>
28#include <fhsim_environment/ElementFlowInteraction.h>
29#include <array>
30#include <limits>
31#include <string>
32#include <vector>
33
34
35#define ATTACH_SPHERE
36
40{
41inline constexpr int kNumElements = 2;
42inline constexpr double kDensity = 1100;
43inline constexpr double kNumericalDamping = 1;
44inline constexpr double kDiameter = 0.01;
45inline constexpr double kYoungsModulus = 1e9;
46inline constexpr double kDampingRatio = 1;
47inline constexpr double kFluidRho = marenv::seawaterDensity;
48inline constexpr double kMaxStepLength = 1e-10;
49inline constexpr double kMaxTension = 1e10;
50inline constexpr double kStepSafetyFactor = 50;
51inline constexpr double kMeanTension = 0;
52inline constexpr double kAdaptationPeriod = 0;
53inline constexpr const char* kMaterialName = "Trawl/SteelWire";
54inline constexpr int kNumFaces = 5;
55inline constexpr int kBranchedNumFaces = 6;
56inline constexpr double kDrawScale = 1;
57inline constexpr double kMaxLengthChangeSpeed = 20;
58} // namespace cable_defaults
59
60
63{
64 int numElements;
65 double density;
66 double numericalDamping;
67 double diameter;
68 double dE;
69 double dampingRatio;
70 double fluidRho;
71 double maxStep;
72 double maxTension;
73 double stepSafetyFactor;
74 double dMeanTension0;
75 double eAdaptationPeriod;
76 std::string materialName;
77 int cableFaces;
78 std::string cableName;
79 double drawScale;
80
83 {
84 Reset();
85 }
87 void Reset()
88 {
89 numElements = cable_defaults::kNumElements;
91 numericalDamping = cable_defaults::kNumericalDamping;
94 dampingRatio = cable_defaults::kDampingRatio;
97 maxTension = cable_defaults::kMaxTension;
98 stepSafetyFactor = cable_defaults::kStepSafetyFactor;
99 dMeanTension0 = cable_defaults::kMeanTension;
100 eAdaptationPeriod = cable_defaults::kAdaptationPeriod;
101 materialName = cable_defaults::kMaterialName;
102 cableFaces = cable_defaults::kNumFaces;
103 cableName = "";
104 drawScale = cable_defaults::kDrawScale;
105 }
107 double CalcEA()
108 {
109 return sfh::pi * 0.25 * diameter * diameter * dE;
110 }
111};
112
115{
116 double deltaTheta;
117 double* adTheta;
118 double* adSinTheta;
119 double* adCosTheta;
120 double textureScale;
121 double* adCirclePos;
122 double* adFacePosNormal;
123 double* adTextureCoordU;
124 double* adTextureCoordV;
125 int numRenderEdges;
126 double drawDiam;
127 int* aiIndex;
128 int sizeIndexBuffer;
129
134 SCableRenderSpec(int numFaces, int numElements, double drawDiameter)
135 {
136 drawDiam = drawDiameter;
137 numRenderEdges = numFaces + 1;
138 sizeIndexBuffer = numElements * numRenderEdges * 2;
139 textureScale = 5 * (sfh::pi * drawDiam);
140 aiIndex = new int[sizeIndexBuffer];
141 deltaTheta = 2 * sfh::pi / numFaces;
142 adTheta = new double[numRenderEdges];
143 adSinTheta = new double[numRenderEdges];
144 adCosTheta = new double[numRenderEdges];
145 adCirclePos = new double[(numElements + 1) * (numRenderEdges) * 3];
146 adFacePosNormal = new double[(numElements + 1) * (numRenderEdges) * 3];
147 adTextureCoordU = new double[numRenderEdges];
148 adTextureCoordV = new double[numElements + 1];
149 adTextureCoordV[0] = 0;
150 for (unsigned short i = 0; i < numRenderEdges; i++) {
151 adTheta[i] = i * deltaTheta;
152 adSinTheta[i] = sin(adTheta[i]);
153 adCosTheta[i] = cos(adTheta[i]);
154 adTextureCoordU[i] = adTheta[i] * (sfh::pi * drawDiam) / textureScale;
155 }
156 int i = 0;
157 for (int element = 0; element < numElements; element++) {
158 for (int edge = 0; edge < numRenderEdges; edge++) {
159 aiIndex[i++] = element * numRenderEdges + edge;
160 aiIndex[i++] = (element + 1) * numRenderEdges + edge;
161 }
162 }
163 }
164
167 {
168 delete[] adTheta;
169 delete[] adSinTheta;
170 delete[] adCosTheta;
171 delete[] adCirclePos;
172 delete[] adFacePosNormal;
173 delete[] adTextureCoordU;
174 delete[] adTextureCoordV;
175 }
176
183 void Update(int element, const double* adElPosA, const double* adElPosB, const double* adDirection, double dL0)
184 {
185 // Creating a vector not parallel to the direction of the element.
186 sfh::math::FindNotParallel(adDirection, 3, adNotParallel);
187
188 // Creating two vectors normal to each other and the direction of the element.
189 sfh::linalg::Cross(adDirection, adNotParallel, adNormal1);
190 sfh::linalg::Cross(adDirection, adNormal1, adNormal2);
191 sfh::math::Normalize3(adNormal1);
192 sfh::math::Normalize3(adNormal2);
193 sfh::math::ArrayMultiply(adNormal1, drawDiam / 2.0, 3);
194 sfh::math::ArrayMultiply(adNormal2, drawDiam / 2.0, 3);
195 adTextureCoordV[element + 1] = adTextureCoordV[element] + dL0 / textureScale * 3;
196
197 for (unsigned short face = 0; face < numRenderEdges; face++) {
198 for (unsigned short ind = 0; ind < 3; ind++) {
199 if (element == 0) {
200 adFacePosNormal[element * ((numRenderEdges) * 3) + 3 * face + ind] = adSinTheta[face] * adNormal1[ind] + adCosTheta[face] * adNormal2[ind];
201 adCirclePos[element * ((numRenderEdges) * 3) + 3 * face + ind] = adElPosA[ind] + adFacePosNormal[element * ((numRenderEdges) * 3) + 3 * face + ind];
202 }
203 adFacePosNormal[(element + 1) * ((numRenderEdges) * 3) + 3 * face + ind] = adSinTheta[face] * adNormal1[ind] + adCosTheta[face] * adNormal2[ind];
204 adCirclePos[(element + 1) * ((numRenderEdges) * 3) + 3 * face + ind] = adElPosB[ind] + adFacePosNormal[(element + 1) * ((numRenderEdges) * 3) + 3 * face + ind];
205 }
206 }
207 }
208
209private:
210 // Varaiables to avoid excessive allocations
211 double adNotParallel[3];
212 double adNormal1[3];
213 double adNormal2[3];
214};
215
216// Class definition
218{
219public:
221 InternalCable(ISimObjectCreator* creator, const SCableSpec& CableSpec);
222
224 virtual ~InternalCable();
225
226 // Member functions
227 virtual void FinalSetup(const double dT, const double* const adX, ISimObjectCreator* const creator);
228
230 virtual void OdeFcn(const double* const adPosA,
231 const double* const adVelA,
232 const double* const adPosB,
233 const double* const adVelB,
234 const double* const adX,
235 double* const adXDot,
236 double dT) const;
237
240 bool HasForceOutsideJacobian() const { return m_forceOutsideJacobian; }
241
252 void EvaluateElementsAt(const double* adPosA, const double* adVelA, const double* adPosB, const double* adVelB, const double* adX, double dT) const;
253
256 double EndInertia(int end) const;
257
263 const double* posA, const double* velA,
264 const double* posB, const double* velB,
265 const double* X, double* J, int nStates, int stateOffset) const;
266
287 const double* posA, const double* velA,
288 const double* posB, const double* velB,
289 int globalPosIdxA, int globalVelIdxA,
290 int globalPosIdxB, int globalVelIdxB,
291 double endInertiaA, double endInertiaB,
292 const double* X, double* J, int nStates, int stateOffset) const;
293
295 virtual bool InitializeStates(const double* const adPosA,
296 const double* const adVelA,
297 const double* const adPosB,
298 const double* const adVelB,
299 double* const adX);
300
302 void UpdateCable(double newLength);
303
314 void AdvanceMeanTension(const double* adPosA, const double* adPosB, const double* adX, double T);
315
327 void TakeWakes(const double* adPosA, const double* adPosB, const double* adX, double T);
328
331 double* const adEndForceA,
332 double* const adEndForceB,
333 double* const pdEndInertiaA,
334 double* const pdEndInertiaB,
335 const double* const adPosA,
336 const double* const adVelA,
337 const double* const adPosB,
338 const double* const adVelB,
339 const double* const adX,
340 double dT);
341
343 double GetLength();
344
345#ifdef FH_VISUALIZATION
347 void RenderInit(Ogre::Root* ogreRoot);
348
350 void RenderUpdate(const double* const adX, const double* adPosA, const double* adPosB);
351#endif
352
353 unsigned long m_numElements;
354 unsigned long m_numNodes;
355protected:
356 // Deletes all node forces.
357 void ResetAllNodeForces() const;
358
359 // Adds the forces from each cable element to the array of node forces.
360 void AddAllElementForces(const double* const adPosA,
361 const double* const adVelA,
362 const double* const adPosB,
363 const double* const adVelB,
364 const double* const adX,
365 double dT) const;
366
367 // Calculates the derivatives of the states of the cable.
368 void CalcDerivatives(const double* const adX,
369 double* const adXDot) const;
370
371 // Adds the forces from one element to the array of node forces.
372 virtual void AddElementForce(int cableElement,
373 const double adPosA[3],
374 const double adVelA[3],
375 const double adPosB[3],
376 const double adVelB[3],
377 double adForceA[3],
378 double adForceB[3],
379 double dT) const;
380
386 virtual void AddNodeForce(const double /*pos*/[3], const double /*vel*/[3], double /*force*/[3]) const { }
387
390 void MarkForceOutsideJacobian() const { m_forceOutsideJacobian = true; }
391
397 void AddBareElementForce(int cableElement,
398 const double posA[3], const double velA[3], const double posB[3], const double velB[3],
399 const double waterVel[3], double forceA[3], double forceB[3]) const;
400
403 double NodeInertia(unsigned long node) const;
404
408 bool SegmentEndBlocks(unsigned long segment,
409 const double* posA, const double* velA,
410 const double* posB, const double* velB,
411 const double* X, double dFA_dposB[9], double dFA_dvelA[9]) const;
412
413 unsigned long* m_iStatePos;
414 unsigned long* m_iStateVel;
415
416 // Member variables
418 double m_eA;
419 double m_meanTensionTime = std::numeric_limits<double>::quiet_NaN();
420
422 double** m_aadForceA;
423 double** m_aadForceB;
424
425 environment::wake::ElementFlowInteraction m_flow;
426 environment::EnvironmentProvider* m_environment = nullptr;
427 double m_elVelWater[3];
428 double m_elMeanPos[3];
429 double m_elMeanVel[3];
430
431#ifdef FH_VISUALIZATION
432 SCableRenderSpec* m_renderSpec;
433 Ogre::Root* m_ogreRoot;
434 Ogre::SceneManager* m_sceneMgr;
435 Ogre::Entity* m_renderElement;
436 Ogre::SceneNode* m_renderNode;
437 Ogre::ManualObject* m_cableRenderObj;
438 double m_drawDiam;
439# ifdef ATTACH_SPHERE
440 Ogre::Entity* m_renderSphere[2];
441 Ogre::SceneNode* m_renderNodeSphere[2];
442# endif
443#endif
444
445private:
446 mutable bool m_forceOutsideJacobian = false;
447};
448
449
450#endif
Class containing a cable element object which is adapted to the required max step size.
Definition CableElDynStiff.h:30
Definition InternalCable.h:218
virtual ~InternalCable()
The destructor deletes dynamically allocated memory.
void ComputeStructuralJacobian(const double *posA, const double *velA, const double *posB, const double *velB, const double *X, double *J, int nStates, int stateOffset) const
double m_eA
The stiffness of the cable.
Definition InternalCable.h:418
double ** m_aadForceB
The force on the B nodes.
Definition InternalCable.h:423
void EvaluateElementsAt(const double *adPosA, const double *adVelA, const double *adPosB, const double *adVelB, const double *adX, double dT) const
virtual void AddNodeForce(const double[3], const double[3], double[3]) const
Definition InternalCable.h:386
double ** m_aadForceA
The force on the A nodes.
Definition InternalCable.h:422
bool HasForceOutsideJacobian() const
Definition InternalCable.h:240
void AddEndpointJacobianContributions(const double *posA, const double *velA, const double *posB, const double *velB, int globalPosIdxA, int globalVelIdxA, int globalPosIdxB, int globalVelIdxB, double endInertiaA, double endInertiaB, const double *X, double *J, int nStates, int stateOffset) const
void MarkForceOutsideJacobian() const
Definition InternalCable.h:390
void AddBareElementForce(int cableElement, const double posA[3], const double velA[3], const double posB[3], const double velB[3], const double waterVel[3], double forceA[3], double forceB[3]) const
unsigned long m_numNodes
The number of nodes of the cable.
Definition InternalCable.h:354
void TakeWakes(const double *adPosA, const double *adPosB, const double *adX, double T)
SCableSpec m_CableSpec
The cable specification.
Definition InternalCable.h:417
environment::wake::ElementFlowInteraction m_flow
The flow at the element midpoints, the other objects' wakes taken per step (MARE-0223); the Environme...
Definition InternalCable.h:425
double m_meanTensionTime
The time of the last AdvanceMeanTension.
Definition InternalCable.h:419
double NodeInertia(unsigned long node) const
double EndInertia(int end) const
unsigned long * m_iStatePos
An array of indices to the node position states.
Definition InternalCable.h:413
environment::EnvironmentProvider * m_environment
m_flow's Environment, from FinalSetup; null before it and without one (fhsim_fishery's tests set it b...
Definition InternalCable.h:426
CableElDynStiff ** m_cableElements
An array holding the cable elements.
Definition InternalCable.h:421
virtual void OdeFcn(const double *const adPosA, const double *const adVelA, const double *const adPosB, const double *const adVelB, const double *const adX, double *const adXDot, double dT) const
Calculates the state derivatives and the end forces.
bool SegmentEndBlocks(unsigned long segment, const double *posA, const double *velA, const double *posB, const double *velB, const double *X, double dFA_dposB[9], double dFA_dvelA[9]) const
virtual bool InitializeStates(const double *const adPosA, const double *const adVelA, const double *const adPosB, const double *const adVelB, double *const adX)
Method for setting initial conditions when the input is known, which may be implemented in the indivi...
unsigned long m_numElements
The number of elements to divide the cable into.
Definition InternalCable.h:353
double GetLength()
Calculates the length of the cable.
void UpdateCable(double newLength)
Sets the unstretched length of the cable, divided equally between its elements.
unsigned long * m_iStateVel
An array of indices to the node velocity states.
Definition InternalCable.h:414
void AddEndForcesAndInertia(double *const adEndForceA, double *const adEndForceB, double *const pdEndInertiaA, double *const pdEndInertiaB, const double *const adPosA, const double *const adVelA, const double *const adPosB, const double *const adVelB, const double *const adX, double dT)
Adds the end forces and inertia to the supplied data structures.
void AdvanceMeanTension(const double *adPosA, const double *adPosB, const double *adX, double T)
InternalCable(ISimObjectCreator *creator, const SCableSpec &CableSpec)
The constructor sets the pointer to the output object and the parser object.
Definition InternalCable.h:40
constexpr double kMaxLengthChangeSpeed
Cable/Branched: how fast the cables may change length [m/s].
Definition InternalCable.h:57
constexpr double kDrawScale
The visual scale of the cable.
Definition InternalCable.h:56
constexpr double kMaxTension
The maximum tension in the cable [N].
Definition InternalCable.h:49
constexpr int kNumFaces
The visual number of faces of Cable/LumpedMass.
Definition InternalCable.h:54
constexpr double kStepSafetyFactor
Read and checked, not used by the stiffness calculation.
Definition InternalCable.h:50
constexpr double kMeanTension
The mean tension of the cable [N].
Definition InternalCable.h:51
constexpr double kFluidRho
The density of the surrounding fluid [kg/m³].
Definition InternalCable.h:47
constexpr double kAdaptationPeriod
The time constant of the adaptation of the mean tension [s].
Definition InternalCable.h:52
constexpr double kYoungsModulus
The stiffness of the cable material [Pa].
Definition InternalCable.h:45
constexpr double kDampingRatio
The relative axial damping.
Definition InternalCable.h:46
constexpr int kBranchedNumFaces
The visual number of faces of Cable/Branched's General tag.
Definition InternalCable.h:55
constexpr double kDiameter
The diameter of the cable [m].
Definition InternalCable.h:44
constexpr double kDensity
The density of the cable material [kg/m³].
Definition InternalCable.h:42
constexpr double kNumericalDamping
Numerical damping rate [1/s]: a dashpot between the end nodes of each element, c = k m_e / 2 (owner r...
Definition InternalCable.h:43
constexpr double kMaxStepLength
The step the reduced stiffness is chosen for [s]; this small keeps the full stiffness.
Definition InternalCable.h:48
constexpr double seawaterDensity
Ocean water density [kg/m^3].
Definition marenv.h:20
Contains some specifications and methods used for rendering cables.
Definition InternalCable.h:115
SCableRenderSpec(int numFaces, int numElements, double drawDiameter)
Definition InternalCable.h:134
~SCableRenderSpec()
The destructor frees allocated memory.
Definition InternalCable.h:166
void Update(int element, const double *adElPosA, const double *adElPosB, const double *adDirection, double dL0)
Definition InternalCable.h:183
Contains some specifications used for simulating cables.
Definition InternalCable.h:63
void Reset()
Resets the specification to the defaults.
Definition InternalCable.h:87
double CalcEA()
Calculates the stiffness of the cable.
Definition InternalCable.h:107
SCableSpec()
The constructor sets some default values.
Definition InternalCable.h:82