FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
NetElement3NForces.h
1#pragma once
2
25#include <fhsim_environment/hydrodynamics/PanelClampLog.h>
26#include <fhsim_environment/hydrodynamics/PanelLoadLaw.h>
27
28#include <sfh/linalg/matrix_ops.h>
29#include <sfh/math/array.h>
30#include <sfh/math/math.h>
31#include <sfh/sim/kinematics.h>
32#include <sfh/util/diagnostic.h>
33
34#include <cmath>
35#include <iostream>
36#include <type_traits>
37
38namespace net_element_forces
39{
40
42const double kMinMeshBarLength = 1.0e-10;
43
46const hydrodynamics::FlowReference kFlowReference = hydrodynamics::FlowReference::FreeStream;
47
48
59{
60 double nodesDistAB_mesh[2];
61 double nodesDistAC_mesh[2];
62 double nodesDistBC_mesh[2];
63 double meshDet;
64 double barL0;
65 double eATwine;
66 double barD;
67 double knotDiameter;
68 double numBarsU;
69 double numBarsV;
70 double numKnots;
71 double NodeWeight;
72 double dampingCoeff;
73};
74
75
81template<class T>
90
91
102template<class T>
103void CalcLocalUVComponents(const PanelParams& params, const T distAB_panel[3],
104 const T distAC_panel[3], T meshBarComp[6], T meshBarLength[2])
105{
106 using std::sqrt;
107
108 meshBarComp[0] = (params.nodesDistAC_mesh[1] * distAB_panel[0] - params.nodesDistAB_mesh[1] * distAC_panel[0]) / params.meshDet;
109 meshBarComp[1] = (params.nodesDistAC_mesh[1] * distAB_panel[1] - params.nodesDistAB_mesh[1] * distAC_panel[1]) / params.meshDet;
110 meshBarComp[2] = T(0.0);
111 meshBarComp[3] = (params.nodesDistAB_mesh[0] * distAC_panel[0] - params.nodesDistAC_mesh[0] * distAB_panel[0]) / params.meshDet;
112 meshBarComp[4] = (params.nodesDistAB_mesh[0] * distAC_panel[1] - params.nodesDistAC_mesh[0] * distAB_panel[1]) / params.meshDet;
113 meshBarComp[5] = T(0.0);
114
115 meshBarLength[0] = sfh::math::Max(
116 sqrt(meshBarComp[0] * meshBarComp[0] + meshBarComp[1] * meshBarComp[1]),
117 T(kMinMeshBarLength));
118 meshBarLength[1] = sfh::math::Max(
119 sqrt(meshBarComp[3] * meshBarComp[3] + meshBarComp[4] * meshBarComp[4]),
120 T(kMinMeshBarLength));
121}
122
123
133template<class T>
134PanelFrame<T> ComputePanelFrame(const PanelParams& params, const T* const posA_ned,
135 const T* const posB_ned, const T* const posC_ned)
136{
137 using std::acos;
138 using std::sqrt;
139
140 PanelFrame<T> frame;
141 sfh::sim::RMatrixFromThreePoints(posA_ned, posB_ned, posC_ned, frame.R_ned2panel);
142
143 // The panel-frame separations. They are held as 3-vectors although the third component
144 // is always zero, so that the standard rotation and array routines apply.
145 T distAB_ned[3], distAC_ned[3];
146 T distAB_panel[3], distAC_panel[3];
147 sfh::math::ArraySubtract(posB_ned, posA_ned, distAB_ned, 3ul);
148 sfh::math::ArraySubtract(posC_ned, posA_ned, distAC_ned, 3ul);
149 sfh::sim::RotateToLocal(distAB_ned, frame.R_ned2panel, distAB_panel);
150 sfh::sim::RotateToLocal(distAC_ned, frame.R_ned2panel, distAC_panel);
151
152 CalcLocalUVComponents(params, distAB_panel, distAC_panel, frame.meshBarComp,
153 frame.meshBarLength);
154
155 // The mesh half opening angle, from the scalar product of the u- and v-bar vectors
156 // (Priour, 2001, p. 232). The positive mesh directions must be defined so that the half
157 // angle is the one between bars that would naturally tend to close.
158 T normBarCompU = T(0);
159 T normBarCompV = T(0);
160 for (int i = 0; i < 3; i++) {
161 normBarCompU += frame.meshBarComp[i] * frame.meshBarComp[i];
162 normBarCompV += frame.meshBarComp[i + 3] * frame.meshBarComp[i + 3];
163 }
164 normBarCompU = sqrt(normBarCompU);
165 normBarCompV = sqrt(normBarCompV);
166
167 frame.meshOpeningAngle = 0.5 * acos(sfh::linalg::Dot(frame.meshBarComp, frame.meshBarComp + 3, 2ul) / (normBarCompU * normBarCompV));
168 if (!(frame.meshOpeningAngle == frame.meshOpeningAngle)) frame.meshOpeningAngle = T(0);
169
170 // In the panel frame AB lies along x and the element in the xy-plane.
171 frame.area = 0.5 * sfh::math::Abs(distAB_panel[0] * distAC_panel[1] - distAB_panel[1] * distAC_panel[0]);
172 return frame;
173}
174
175
181template<class T>
182void CalcTensionForces(const PanelParams& params, const T meshBarComp[6],
183 const T meshBarLength[2], T nodeATension_panel[3], T nodeBTension_panel[3],
184 T nodeCTension_panel[3])
185{
186 // The original multiplied by the `bool` of this comparison. That promotes to 1.0 or 0.0
187 // for a built-in type but discards the derivative of a differentiable one, so the branch
188 // is written out. The retained branch is the original expression unchanged, and
189 // multiplying it by an exact 1.0 was exact, so the `double` value is unaffected.
190 T twineTension[2];
191 twineTension[0] = (meshBarLength[0] > params.barL0)
192 ? params.eATwine * (meshBarLength[0] - params.barL0) / params.barL0
193 : T(0.0);
194 twineTension[1] = (meshBarLength[1] > params.barL0)
195 ? params.eATwine * (meshBarLength[1] - params.barL0) / params.barL0
196 : T(0.0);
197 twineTension[0] = sfh::math::Max(twineTension[0], T(0.0));
198 twineTension[1] = sfh::math::Max(twineTension[1], T(0.0));
199
200 nodeATension_panel[0] = params.nodesDistBC_mesh[1] * twineTension[0] * meshBarComp[0] / (2 * meshBarLength[0]) - params.nodesDistBC_mesh[0] * twineTension[1] * meshBarComp[3] / (2 * meshBarLength[1]);
201 nodeATension_panel[1] = params.nodesDistBC_mesh[1] * twineTension[0] * meshBarComp[1] / (2 * meshBarLength[0]) - params.nodesDistBC_mesh[0] * twineTension[1] * meshBarComp[4] / (2 * meshBarLength[1]);
202 nodeATension_panel[2] = T(0.0);
203
204 nodeBTension_panel[0] = -params.nodesDistAC_mesh[1] * twineTension[0] * meshBarComp[0] / (2 * meshBarLength[0]) + params.nodesDistAC_mesh[0] * twineTension[1] * meshBarComp[3] / (2 * meshBarLength[1]);
205 nodeBTension_panel[1] = -params.nodesDistAC_mesh[1] * twineTension[0] * meshBarComp[1] / (2 * meshBarLength[0]) + params.nodesDistAC_mesh[0] * twineTension[1] * meshBarComp[4] / (2 * meshBarLength[1]);
206 nodeBTension_panel[2] = T(0.0);
207
208 nodeCTension_panel[0] = params.nodesDistAB_mesh[1] * twineTension[0] * meshBarComp[0] / (2 * meshBarLength[0]) - params.nodesDistAB_mesh[0] * twineTension[1] * meshBarComp[3] / (2 * meshBarLength[1]);
209 nodeCTension_panel[1] = params.nodesDistAB_mesh[1] * twineTension[0] * meshBarComp[1] / (2 * meshBarLength[0]) - params.nodesDistAB_mesh[0] * twineTension[1] * meshBarComp[4] / (2 * meshBarLength[1]);
210 nodeCTension_panel[2] = T(0.0);
211}
212
213
230template<class T>
231hydrodynamics::PanelLoad<T> EvaluatePanelLoad(const PanelParams& params, const PanelFrame<T>& frame,
232 const hydrodynamics::PanelLoadLaw& law, const hydrodynamics::Fluid& fluid, const T& solidity,
233 const T elementVel_ned[3], const T* const waterVel_ned)
234{
235 hydrodynamics::PanelGeometry<T> geometry;
236 geometry.area = frame.area;
237
238 hydrodynamics::Netting<T> netting;
239 netting.solidity = solidity;
240 netting.twineThickness = params.barD;
241 netting.hasBarDirections = params.meshDet != 0;
242 netting.barLength = params.barL0;
243 netting.knotDiameter = params.knotDiameter;
244 netting.numBarsU = params.numBarsU;
245 netting.numBarsV = params.numBarsV;
246 netting.numKnots = params.numKnots;
247
248 hydrodynamics::FlowSample<T> flow;
249 flow.reference = kFlowReference;
250
251 for (int i = 0; i < 3; i++) {
252 geometry.normal[i] = frame.R_ned2panel[i][2];
253 flow.relativeVelocity[i] = waterVel_ned[i] - elementVel_ned[i];
254
255 // The bars lie in the panel's xy-plane; rotate them to the global frame and normalise.
256 const T barU = frame.R_ned2panel[i][0] * frame.meshBarComp[0] + frame.R_ned2panel[i][1] * frame.meshBarComp[1];
257 const T barV = frame.R_ned2panel[i][0] * frame.meshBarComp[3] + frame.R_ned2panel[i][1] * frame.meshBarComp[4];
258 netting.barU[i] = barU / frame.meshBarLength[0];
259 netting.barV[i] = barV / frame.meshBarLength[1];
260 }
261
262 return hydrodynamics::Evaluate(law, geometry, netting, fluid, flow);
263}
264
265
267template<class T>
268void CalcDampingForces(const PanelParams& params, const T elementVel_ned[3],
269 const T* const velA_ned, const T* const velB_ned, const T* const velC_ned,
270 T dampingForcesNodeA_ned[3], T dampingForcesNodeB_ned[3],
271 T dampingForcesNodeC_ned[3], double addedLinearDrag)
272{
273 for (unsigned short i = 0; i < 3; i++) {
274 dampingForcesNodeA_ned[i] = params.dampingCoeff * (elementVel_ned[i] - velA_ned[i]);
275 dampingForcesNodeB_ned[i] = params.dampingCoeff * (elementVel_ned[i] - velB_ned[i]);
276 dampingForcesNodeC_ned[i] = params.dampingCoeff * (elementVel_ned[i] - velC_ned[i]);
277 }
278
279 if (addedLinearDrag > 0) {
280 for (unsigned short i = 0; i < 3; i++) {
281 dampingForcesNodeA_ned[i] -= addedLinearDrag / 3 * velA_ned[i];
282 dampingForcesNodeB_ned[i] -= addedLinearDrag / 3 * velB_ned[i];
283 dampingForcesNodeC_ned[i] -= addedLinearDrag / 3 * velC_ned[i];
284 }
285 }
286}
287
288
309template<class T>
310void AddNodeForces(const PanelParams& params, const PanelFrame<T>& frame,
311 const hydrodynamics::PanelLoadLaw& law, const hydrodynamics::Fluid& fluid, const T& solidity,
312 const T* const velA_ned, const T* const velB_ned, const T* const velC_ned,
313 const T* const waterVel_ned, T* const nodeAforce_ned, T* const nodeBforce_ned,
314 T* const nodeCforce_ned, double addedLinearDrag = 0.0,
315 const hydrodynamics::PanelClampLog* clampLog = nullptr)
316{
317 T nodeATension_panel[3] = {T(0), T(0), T(0)};
318 T nodeBTension_panel[3] = {T(0), T(0), T(0)};
319 T nodeCTension_panel[3] = {T(0), T(0), T(0)};
320 CalcTensionForces(params, frame.meshBarComp, frame.meshBarLength, nodeATension_panel,
321 nodeBTension_panel, nodeCTension_panel);
322
323 T elementVel_ned[3];
324 for (int i = 0; i < 3; i++) {
325 elementVel_ned[i] = (velA_ned[i] + velB_ned[i] + velC_ned[i]) / 3.0;
326 }
327
328 const hydrodynamics::PanelLoad<T> load =
329 EvaluatePanelLoad(params, frame, law, fluid, solidity, elementVel_ned, waterVel_ned);
330 if (clampLog)
331 clampLog->Note(load.flags);
332
333 T dampingForcesNodeA_ned[3] = {T(0), T(0), T(0)};
334 T dampingForcesNodeB_ned[3] = {T(0), T(0), T(0)};
335 T dampingForcesNodeC_ned[3] = {T(0), T(0), T(0)};
336 CalcDampingForces(params, elementVel_ned, velA_ned, velB_ned, velC_ned,
337 dampingForcesNodeA_ned, dampingForcesNodeB_ned, dampingForcesNodeC_ned,
338 addedLinearDrag);
339
340 // Local z-components of the tension are zero, the element being plane.
341 sfh::sim::RotateFromLocalAndAdd(nodeATension_panel, frame.R_ned2panel, nodeAforce_ned);
342 sfh::sim::RotateFromLocalAndAdd(nodeBTension_panel, frame.R_ned2panel, nodeBforce_ned);
343 sfh::sim::RotateFromLocalAndAdd(nodeCTension_panel, frame.R_ned2panel, nodeCforce_ned);
344
345 // The hydrodynamic force is shared equally between the three nodes.
346 for (int i = 0; i < 3; i++) {
347 const T hydroForcePerNode = load.force[i] / 3.0;
348 nodeAforce_ned[i] += hydroForcePerNode;
349 nodeBforce_ned[i] += hydroForcePerNode;
350 nodeCforce_ned[i] += hydroForcePerNode;
351 }
352
353 // The weight in water of the netting, distributed equally between the three nodes.
354 nodeAforce_ned[2] += params.NodeWeight;
355 nodeBforce_ned[2] += params.NodeWeight;
356 nodeCforce_ned[2] += params.NodeWeight;
357
358 for (unsigned short i = 0; i < 3; i++) {
359 nodeAforce_ned[i] += dampingForcesNodeA_ned[i];
360 nodeBforce_ned[i] += dampingForcesNodeB_ned[i];
361 nodeCforce_ned[i] += dampingForcesNodeC_ned[i];
362 }
363
364 // The diagnostic below reads raw `double` arrays, so it applies only to the `double`
365 // instantiation.
366 if constexpr (std::is_same_v<T, double>) {
367 unsigned long bad;
368 if ((bad = sfh::util::FindBadValues(nodeAforce_ned, 3)) != (unsigned long)-1)
369 std::cout << "Bad element in nodeAforce_ned: " << bad << std::endl;
370 if ((bad = sfh::util::FindBadValues(nodeBforce_ned, 3)) != (unsigned long)-1)
371 std::cout << "Bad element in nodeBforce_ned: " << bad << std::endl;
372 if ((bad = sfh::util::FindBadValues(nodeCforce_ned, 3)) != (unsigned long)-1)
373 std::cout << "Bad element in nodeCforce_ned: " << bad << std::endl;
374 }
375}
376
377} // namespace net_element_forces
Definition NetElement3NForces.h:83
T meshBarComp[6]
The u-bar in [0..2] and the v-bar in [3..5], panel frame.
Definition NetElement3NForces.h:85
T meshOpeningAngle
The mesh half opening angle, rad; 0 when undefined.
Definition NetElement3NForces.h:87
T R_ned2panel[3][3]
Columns: the panel x, y and z axes in the global frame.
Definition NetElement3NForces.h:84
T area
The element's area, m^2.
Definition NetElement3NForces.h:88
T meshBarLength[2]
The length of a u-bar and of a v-bar, bounded away from zero.
Definition NetElement3NForces.h:86
Definition NetElement3NForces.h:59
double meshDet
Mesh determinant, Priour (2005, Eq. 7); 0 for collinear corners.
Definition NetElement3NForces.h:63
double nodesDistBC_mesh[2]
Distance from node B to C in mesh (u,v) coordinates.
Definition NetElement3NForces.h:62
double knotDiameter
Diameter of the knots.
Definition NetElement3NForces.h:67
double eATwine
E-modulus times twine cross-sectional area.
Definition NetElement3NForces.h:65
double numKnots
Number of knots in the element, its number of meshes.
Definition NetElement3NForces.h:70
double nodesDistAB_mesh[2]
Distance from node A to B in mesh (u,v) coordinates.
Definition NetElement3NForces.h:60
double barD
Diameter of the twines.
Definition NetElement3NForces.h:66
double dampingCoeff
Coefficient of the rotational and structural damping.
Definition NetElement3NForces.h:72
double NodeWeight
Weight in water carried by one node.
Definition NetElement3NForces.h:71
double barL0
Unstretched length of a mesh bar.
Definition NetElement3NForces.h:64
double numBarsV
Number of v-bars in the element, half its bar count.
Definition NetElement3NForces.h:69
double numBarsU
Number of u-bars in the element, half its bar count.
Definition NetElement3NForces.h:68
double nodesDistAC_mesh[2]
Distance from node A to C in mesh (u,v) coordinates.
Definition NetElement3NForces.h:61