Marine systems simulation
FishCage.h
1#pragma once
2
3#include "eigen_matrix_defs.h"
4#include "sfh/constants.h"
5#include "SimObject.h"
6#include <memory>
7#include "Eigen/StdVector"
8#include "CEnvironment.h"
9#include <CPrintDuringExec.h>
10
11#ifdef FH_VISUALIZATION
12#include "sfh/ogre/C3DLine.h"
13#endif
14
15namespace Fish{
16
17class FishCage {
18public:
19 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
20
21 ~FishCage();
22
23 void Init(ISimObjectCreator* creator);
24 void OdeFcn(double T, const double* X, double* XDot);
25 void PreOdeFcn(const double T, const double* X, IStateUpdater* updater);
26
27 double Radius(){return m_cage_radius;}
28 double Depth(){return m_Depth;}
29
30 void SetEnvironment(CEnvironment* environment);
31
32 struct CageEdge{
33 vec3 P;
34 vec3 N;
35 double depth;
36 };
37
38 CageEdge GetClosestEdge(const vec3& P);
39
40 struct NetPanel{
41 vec3 P; // Net panel position
42 vec3 N; // Net panel normal vector. Points "out" of cage
43 double Distance; // Distance to the obstacle
44
45 vec3 G;
46 vec3 VN;
47 vec3 VV;
48 };
49
50 struct ix_pair { uint16_t i; uint16_t j; ix_pair(uint16_t a, uint16_t b) { i = a; j = b; } };
51
52 NetPanel GetClosestNetPanel(const vec3& P, double& velR, const vec3& V);
53
54 //============================================================ Herman_dev
55 struct Triangle {
56 vec3 vertex1; // First vertex (N,E,D) of triangle
57 vec3 vertex2; // 2nd --''--
58 vec3 vertex3; // 3rd --''--
59 };
60 bool GetRayIntersection(const vec3& rayOrigin, const vec3& rayDirection, double& outDistance);
61 bool RayIntersectsTriangle(const Triangle* inTriangle, const vec3& rayOrigin, const vec3& rayVector, double& outDistance);
62 //============================================================ Herman_dev
63
64 double m_dragForceNet[3]; // Total hydrodynamic drag force on the net
65 double m_dragForceSkirt[3]; // Total hydrodynamic drag force on the skirt
66 double m_dragForceDepth[3]; // Total hydrodynamic drag force on the net above a given depth
67 int m_DepthI;
68
69 bool m_ExtTopConn; // Boolean, true if external connection is considered
70 double* m_TopOutForce; // Top connection force on the net
71 int m_TopConnectNum; // Top connection
72 int m_BotConnectNum; // Bottom connection
73 double m_RingOutPos[3]; // Ring out position
74 double m_BotOutPos[3]; // Bottom out position
75 double m_NetOutPos[3]; // Net out position (single)
76 double m_BotOutVel[3]; // Bottom out velocity
77
78 int m_NumNetOut; // Number of net out position
79 double* m_MatNetOut; // Net out position (multiple)
80
81 int m_ExtTopConnNum; // External top connection number
82 bool m_ExtTopBody; // External top body
83 vec3 m_RelTopPos; // Top position relative to body
84 double m_ExtTopBodyForce[6]; // External top body force
85
86 bool m_PortPosToForce; // Port to input position and output force
87 int m_PortPosNum; // Port number
88 int* m_PortPosIndex; // Port index
89 double m_PortConLength; // Port connection length
90 double* m_PortOutForce; // Port out force
91
92 bool m_PortForceToPos; // Port to input force and output position
93 int m_PortForceNum; // Port number
94 int* m_PortForceIndex; // Port index
95 double* m_PortOutPos; // Port out position
96 double* m_PortOutVel; // Port out velocity
97
98 double m_SkirtPos[3]; // Position of the sea-lice skirt
99
100 bool m_CalVolume; // Port to output cage volume
101 double m_Volume[3]; // Cage volume
102 double m_CageTopCentre[3]; // Cage top centre
103
104 double m_FlowReduction; // Flow reduction factor
105
106 double m_SkirtCD[2]; // Skirt drag coefficient, [0]: drag coefficient; [1]: flow reduction factor
107 double m_InputCD[3]; // Input drag coefficient, [0]: drag coefficient; [1]: flow reduction factor (global); [2]: flow reduction factor
108
109 bool m_PortCD; // Port to output drag coefficient
110 double* m_PortOutCD; // Port out drag coefficient
111 int m_PortCDIndex[2]; // Port index
112 double m_PortCDDepth; // Port depth
113 double* m_PortNodeCD; // Node drag coefficient
114
115 bool m_RigidCage;
116
117 bool m_InBottomForce;
118 ISignalPort* m_AddBottomForce;
119
120 bool m_InRingForce;
121 ISignalPort* m_AddRingForce;
122
123 int m_InCdNum;
124 double* m_InCd;
125 int* m_InCdIndex;
126 ISignalPort** m_AddCd;
127
128 bool m_ExtBotRing;
129 ISignalPort** m_InRingPos;
130 ISignalPort** m_InRingVel;
131 double* m_OutRingForce;
132
133 bool m_ExtChain;
134 ISignalPort** m_InChainPos;
135 ISignalPort** m_InChainVel;
136 double* m_OutChainForce;
137 std::vector<double> m_ChainPos;
138 vec3 m_ChainCen;
139 double m_ChainLen;
140 double m_ChainLim;
141 int m_ExtChainNum;
142 int m_ExtChainDepIdx;
143 int m_RingStatePos;
144 int m_RingStateVel;
145 double m_RingStateM;
146 double m_RingStateA;
147 double m_RingStateD;
148
149 bool m_BotRingInState;
150 ISignalPort* m_BotRingInPos;
151 ISignalPort* m_BotRingInVel;
152
153 // Net track ===============================
154 bool m_NetTrack; // Boolean, true if net panels are tracked
155 double m_NetTrackDT;
156 int m_NetTrackNum;
157 double m_NetTrackDis;
158 ISignalPort* m_NetTrackPos;
159 int m_NetPanelNum;
160 int m_NetPanelNumH;
161 int m_NetPanelNumV;
162 int* m_NetPanelMat;
163 double* m_NetPanelArea;
164 double m_NetPanelAreaTrack;
165 double m_NetPanelOutDT;
166 int m_NetPanelOutNum;
167 std::string m_NetPanelOutFile;
168
169 double m_CageNodeOutDT;
170 int m_CageNodeOutNum;
171 std::string m_CageNodeOutFile;
172 double m_CageNodeOutTSta;
173 double m_CageNodeOutTStp;
174
175 double m_NetMinDis;
176 double m_NetTrackAng;
177
178 bool m_NetIdentify;
179 ISignalPort* m_NetIdentifyNum;
180
181 bool m_NetTrackHeadingToPosition;
182 ISignalPort* m_NetTrackHeading;
183
184 int m_NetTrack_States;
185 double m_NetTrackPosOut[12];
186 double m_NetTrackPosIn[6];
187 double m_NetTrackMaxDiff;
188 double m_NetTrackCenDiff[4];
189 double m_NetTrackInitialTime[3];
190
191 double m_CageHeading;
192 bool m_Quat;
193 Quat m_CageQL;
194 mat3 m_CageRL;
195
196 int NodeNumber() {return m_num_nodes;}
197 vec3 NodePosition(int index) {return m_P[index];};
198 // Net track ===============================
199
200 //============================================================================ PC_dev start
201 bool m_isSmallTank; // Set to true if a small tank is simulated
202 bool m_useDummyTankVel;// Set to true if a dummy tank velocity should be used.
203 double m_URadialMax; // Max value for radially linearly varying water velocity in tank.
204 double m_UzConst; // Constant vertical water velocity in tank
205 void GetDummyTankVel(const double* pos, double* outVal);// Function that returns dummy tank velocity
206 //void UsingDummyTankVel(bool outVal); // Function that returns true if dummy tank velocity is used
207 //============================================================================ PC_dev end
208
209#ifdef FH_VISUALIZATION
210 virtual void RenderInit(Ogre::Root *const ogreRoot, ISimObjectCreator *const creator);
211 virtual void RenderUpdate(const double T, const double *const X);
212 C3DLine* m_net_line;
213 C3DLine* m_top_line;
214 C3DLine* m_ring_line;
215 //C3DLine* m_node_normal_line;
216 C3DLine* m_node_unit_hydro_force;
217 Ogre::SceneNode* m_ring_node;
218 std::vector<ix_pair> m_net_pair;
219 C3DLine* m_top_line_ext;
220
221 C3DLine* m_chain_line;
222
223 bool m_net_Render;
224 C3DLine* m_net_triangle;
225
226 bool m_skirt_Render;
227 C3DLine* m_skirt_triangle;
228 int m_skirt_DI;
229 int m_skirt_TN;
230
231 // Net track ===============================
232 C3DLine* m_track_triangle;
233 double m_track_color[4];
234 // Net track ===============================
235#endif
236
237
238private:
239 void distance_constraint(vec3& PA, vec3& PB, vec3& VA, vec3& VB, vec3& NA, vec3& NB, vec3& F, mat3& M, double L, double L_dot, double igamma);
240 void distance_constraint_t(vec3& PA, vec3& PB, vec3& VA, vec3& VB, vec3& NA, vec3& NB, vec3& F, mat3& M, double L, double L_dot, double igamma);
241 void ring_constraint(vec3& P, vec3 V, double angle, vec3& F, vec3& T, mat3& NN, mat3& NK, mat3& KK, double igamma, double ST, const double* SX, int ic);
242 void ring_constraint_ext(vec3& P, vec3 V, double angle, vec3& F, vec3& T, mat3& NN, mat3& NK, mat3& KK, double igamma, double ST, const double* SX, int ic);
243 void chain_constraint(vec3& P, vec3 V, double angle, vec3& F, vec3& T, mat3& NN, mat3& NK, mat3& KK, double igamma, double ST, const double* SX, int ic, vec3& FR, mat3& NNR, int dIdx, mat3& NNC, vec3& FC);
244
245 uint16_t ConvertToZindex(const vec3& P);
246 vec3 ConvertFromZindex(uint16_t Z_index);
247
248 std::vector<int> m_EdgeLookupArray;
249
250 Eigen::Matrix<double,15,1> m_net_bottom_function_approximation_coefficients;
251
252 double m_cage_radius;
253 int m_num_nodes_circumference;
254 int m_num_nodes_vertical;
255 int m_num_nodes;
256
257 double m_net_diameter; // Net twine diameter
258 double m_bar_length;
259 double m_net_solidity;
260 double m_ring_connection_bar_length;
261 double m_net_size;
262
263 double m_node_mass;
264 double m_node_added_mass;
265 double m_node_projected_area;
266 double m_node_submerged_weight;
267
268 vec3 TopRingConnectionPoint(int ix, double T, const double* X);
269
270 ISignalPort* m_WinchLength;
271 ISignalPort* m_WinchSpeed;
272
273 std::vector<mat3,Eigen::aligned_allocator<mat3>> m_connection_matrices;
274 std::vector<mat3,Eigen::aligned_allocator<mat3>> m_node_matrices;
275 std::vector<mat36,Eigen::aligned_allocator<mat36>> m_ring_matrices;
276
277 std::vector<ix_pair> m_connections; // structural connections
278
279 std::vector<vec3,Eigen::aligned_allocator<vec3>> m_N; // node normal vector
280 std::vector<vec3,Eigen::aligned_allocator<vec3>> m_P; // node position
281 std::vector<vec3,Eigen::aligned_allocator<vec3>> m_V; // node velocity
282
283 std::vector<vec3, Eigen::aligned_allocator<vec3>> m_unitHydF; // node unit hydrodynamic force
284
285 // ============================================================================ herman_dev
286 double m_delta_t;
287 //bool m_tau_given;
288 // ============================================================================ herman_dev
289 double m_tau;
290 double m_gamma;
291 SparseMat m_system_matrix;
292 Eigen::SimplicialLDLT<SparseMat,Eigen::Upper> m_solver;
293
294 int m_position_states;
295 int m_velocity_states;
296
297 CEnvironment* m_environment;
298
299 struct BottomRing {
300 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
301
302 int states_ix;
303 double major_radius;
304 double minor_radius;
305 double mass;
306
307 double tau;
308 double delta_t;
309
310 void Init();
311
312 void UpdateStates(const double* X);
313 void HydroDynamics(vec6& Forces, mat6& Inertia, double T, const double* X);
314 void Kinematics(const vec6& Accelerations, double* XDot);
315
316 double buoyancy;
317 vec3 translational_inertia;
318 vec3 angular_inertia;
319
320 vec3 P;
321 Quat Q;
322 vec3 V;
323 vec3 W;
324
325 CEnvironment* m_environment;
326
327 void UpdateStatesInput(const double* X, const double* InPos, const double* InVel);
328 void KinematicsInput(const vec6& Accelerations, double* XDot);
329 } m_ring;
330
331 int m_Triangles; // Number of triangular elements
332 std::vector<int> m_TI; // Index of the triangle nodes in Node matrix
333 //std::vector<vec3, Eigen::aligned_allocator<vec3>> m_TP; // Triangle position
334 //std::vector<vec3, Eigen::aligned_allocator<vec3>> m_TN; // Triangle normal vector
335 //std::vector<double> m_TA; // Triangle area
336
337 std::vector<int> m_TopI;
338
339 double m_Depth; // Cage depth
340 std::vector<int> m_BotI;
341 int m_BotNode; // Bottom node
342 double m_BotForce; // Bottom force
343
344 double m_Density; // Net density
345 std::vector<double> m_A; // Node area
346 int m_BNum; // Number of connections
347 std::vector<double> m_BL; // Bar length
348
349 int m_ENum; // Number of edges
350
351 bool m_BotEdge; // Boolean, true if bottom edges are checked
352 std::vector<int> m_BotELookupArray;
353 int m_BotENum; // Number of bottom edges
354
355 vec3 m_Current; // Current velocities
356 double m_NetE; // Net elastic modulus
357
358 double m_TopConnL; // Top connection length
359 ISignalPort* m_WinchTime; // Input port, when to winch
360
361 ISignalPort** m_TopInPos; // Input port, top positions
362 ISignalPort** m_TopInVel; // Input port, top velocities
363
364 ISignalPort* m_TopInBodyPos; // Input port
365 ISignalPort* m_TopInBodyQuat; // Input port
366
367 int m_BotRI;
368 int m_EBNum;
369 int m_BBNum;
370
371 int m_NetOutI[4];
372 double m_NetOutR[2];
373
374 int* m_MatNetOutI;
375 double* m_MatNetOutR;
376
377 std::vector<double> m_RingPos;
378 int m_RingOutI[2];
379 double m_RingOutR[2];
380
381 double m_SkirtDepth;
382 int m_SkirtI;
383
384 double m_BotRingForce;
385
386 bool m_ModelBottom;
387
388 int m_TopINum;
389 std::vector<vec3, Eigen::aligned_allocator<vec3>> m_TopPPos;
390 std::vector<vec3, Eigen::aligned_allocator<vec3>> m_TopTPos;
391
392 double m_VelRedFactor;
393
394 ISignalPort** m_PortInPos;
395 ISignalPort** m_PortInVel;
396 ISignalPort** m_PortInForce;
397
398 bool PointInsideTriangle(vec3& P, vec3& A, vec3& B, vec3& C);
399 int Factorial(int n);
400
401 ISignalPort* m_BotConForce; // Input port, bottom connection force
402
403 double m_OutputDT;
404 int m_OutputNum;
405 //============================================================================ herman_dev
406 //double m_prev_time = 0;
407 CPrintDuringExec* m_logger;
408 //============================================================================ herman_dev
409
410
411};
412};
Definition: RingStructureGravityHydro.h:74
Definition: CEnvironment.h:10
Definition: FishCage.h:17
Definition: FishCage.h:32
double depth
edge normal vector. Points "out" of cage
Definition: FishCage.h:35
vec3 N
edge node position
Definition: FishCage.h:34
Definition: FishCage.h:40
Definition: FishCage.h:55
Definition: FishCage.h:50