MoorDyn
Rod.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022, Matt Hall
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
35 #pragma once
36 
37 #include "Misc.hpp"
38 #include "Instance.hpp"
39 #include "Seafloor.hpp"
40 #include "Util/CFL.hpp"
41 #include "leanvtk/leanvtk.hpp"
42 #include <vector>
43 #include <utility>
44 
45 using namespace std;
46 
47 namespace moordyn {
48 
49 class Waves;
50 typedef std::shared_ptr<Waves> WavesRef;
51 class Line;
52 
62 class DECLDIR Rod final
63  : public Instance
64  , public SuperCFL
65 {
66  public:
71  Rod(moordyn::Log* log, size_t rodId);
72 
75  ~Rod();
76 
77  private:
78  // ENVIRONMENTAL STUFF
80  EnvCondRef env;
82  moordyn::WavesRef waves;
84  moordyn::SeafloorRef seafloor;
85 
87  typedef struct _attachment
88  {
90  Line* line;
92  EndPoints end_point;
93  } attachment;
94 
96  std::vector<attachment> attachedA;
97 
99  std::vector<attachment> attachedB;
100 
101  // ROD STUFF
102 
103  // parameters
105  unsigned int N;
107  moordyn::real UnstrLen;
109  vec q0;
111  moordyn::real d;
113  moordyn::real rho;
116  moordyn::real Can;
119  moordyn::real Cat;
122  moordyn::real Cdn;
125  moordyn::real Cdt;
126  // values from input files
127  moordyn::real CdEnd;
128  // values from input files
129  moordyn::real CaEnd;
130 
133  // vec6 r6;
134  XYZQuat r7;
137  vec6 v6;
139  XYZQuat vel7;
141  vec6 acc6;
142 
143  // kinematics
145  std::vector<vec> r;
147  std::vector<vec> rd;
149  vec q;
151  std::vector<moordyn::real> l;
152 
154  std::vector<mat> M;
155  // line segment volumes
156  std::vector<moordyn::real> V;
157 
159  vec FextA;
161  vec FextB;
163  vec Mext;
166  vec6 F6net;
168  mat6 M6net;
169 
170  // forces
172  std::vector<vec> W;
174  std::vector<vec> Bo;
176  std::vector<vec> Pd;
178  std::vector<vec> Dp;
180  std::vector<vec> Dq;
182  std::vector<vec> Ap;
184  std::vector<vec> Aq;
186  std::vector<vec> B;
189  std::vector<vec> Fnet;
190 
191  // wave things
193  std::vector<moordyn::real> VOF; // TODO: This doesn't need to be a vector,
194  // can be a double reused for each node
196  real h0;
197 
198  // time
200  moordyn::real t;
201 
203  vec6 r_ves;
205  vec6 rd_ves;
207  vec6 rdd_ves;
208 
209  // file stuff
211  ofstream* outfile;
213  string channels;
215  int openedoutfile;
216 
224  inline real getWaterDepth(real x, real y)
225  {
226  return seafloor ? seafloor->getDepthAt(x, y) : -env->WtrDpth;
227  }
228 
229  public:
232  typedef enum
233  {
235  COUPLED = -2,
237  CPLDPIN = -1,
239  FREE = 0,
242  PINNED = 1,
245  FIXED = 2,
246  // Some aliases
247  VESSEL = COUPLED,
248  VESPIN = CPLDPIN,
249  POINT = FREE,
250  ANCHOR = FIXED,
251  } types;
252 
257  static string TypeName(types t)
258  {
259  switch (t) {
260  case COUPLED:
261  return "COUPLED";
262  case CPLDPIN:
263  return "CPLDPIN";
264  case FREE:
265  return "FREE";
266  case PINNED:
267  return "PINNED";
268  case FIXED:
269  return "FIXED";
270  }
271  return "UNKNOWN";
272  }
273 
275  size_t rodId;
277  int number;
280 
285 
296  void setup(int number,
297  types type,
298  RodProps* props,
299  vec6 endCoords,
300  unsigned int n,
301  EnvCondRef env_in,
302  shared_ptr<ofstream> outfile,
303  string channels);
304 
311  void addLine(Line* line, EndPoints line_end_point, EndPoints rod_end_point);
312 
320  EndPoints removeLine(EndPoints end_point, Line* line);
321 
326  inline void setWaves(moordyn::WavesRef waves_in,
327  moordyn::SeafloorRef seafloor_in)
328  {
329  waves = waves_in;
330  seafloor = seafloor_in;
331  }
332 
335  inline void openoutput();
336 
347  std::pair<XYZQuat, vec6> initialize();
348 
350  {
351  const auto [pos, vel] = initialize();
352  r.row(0).head<7>() = pos.toVec7();
353  r.row(0).tail<6>() = vel;
354  }
364  inline unsigned int getN() const { return N; }
365 
372  inline vec getNodePos(unsigned int i) const
373  {
374  if (i > N) {
375  LOGERR << "Asking node " << i << " of rod " << number
376  << ", which only has " << N + 1 << " nodes" << std::endl;
377  throw moordyn::invalid_value_error("Invalid node index");
378  }
379  if (isnan(r[i].sum())) {
380  stringstream s;
381  s << "NaN detected" << endl
382  << "Rod " << number << " node positions:" << endl;
383  for (unsigned int j = 0; j <= N; j++)
384  s << j << " : " << r[j] << ";" << endl;
385  throw moordyn::nan_error(s.str().c_str());
386  }
387  return r[i];
388  }
389 
396  inline vec getNodeVel(unsigned int i) const
397  {
398  if (i > N) {
399  LOGERR << "Asking node " << i << " of rod " << number
400  << ", which only has " << N + 1 << " nodes" << std::endl;
401  throw moordyn::invalid_value_error("Invalid node index");
402  }
403  return rd[i];
404  }
405 
412  real GetRodOutput(OutChanProps outChan);
413 
417  inline std::pair<real, real> getDrag() const { return make_pair(Cdn, Cdt); }
418 
423  inline void setDrag(real cdn, real cdt)
424  {
425  Cdn = cdn;
426  Cdt = cdt;
427  }
428 
432  inline void scaleDrag(real scaler)
433  {
434  Cdn = Cdn * scaler;
435  Cdt = Cdt * scaler;
436  CdEnd = CdEnd * scaler;
437  }
438 
442  inline void setTime(real time) { t = time; }
443 
448  inline void getState(XYZQuat& pos, vec6& vel) const
449  {
450  pos = r7;
451  vel = v6;
452  }
453 
457  inline std::pair<XYZQuat, vec6> getState() const
458  {
459  return std::make_pair(r7, v6);
460  }
461 
479  void setState(const InstanceStateVarView r);
480 
491  void initiateStep(vec6 r, vec6 rd, vec6 rdd);
492 
499  inline vec6 getUnfreeVel() const { return rd_ves; }
500 
508  void updateFairlead(real time);
509 
521  void setKinematics(vec6 r, vec6 rd);
522 
528  void setDependentStates();
529 
537  void getStateDeriv(InstanceStateVarView drdt);
538 
545  const vec6 getFnet() const;
546 
552  inline const mat6 getM() const { return M6net; }
553 
561  void getNetForceAndMass(vec6& Fnet_out, mat6& M_out, vec rBody, vec6 vBody);
562 
568  inline void getNetForceAndMass(vec6& Fnet_out, mat6& M_out)
569  {
570  getNetForceAndMass(Fnet_out, M_out, r[0], vec6::Zero());
571  }
572 
576  void doRHS();
577 
578  void Output(real);
579 
583  inline const size_t stateN() const { return 1; }
584 
590  inline const size_t stateDims() const { return 13; }
591 
601  std::vector<uint64_t> Serialize(void);
602 
609  uint64_t* Deserialize(const uint64_t* data);
610 
615  void saveVTK(const char* filename);
616 
622  const leanvtk::VTPWriter* getVTK() const { return &vtk; }
623 
624  private:
626  leanvtk::VTPWriter vtk;
627 
633  inline vec getCentripetalForce(vec r, vec w) const
634  {
635  if (!N)
636  return vec::Zero();
637 
638  vec F = vec::Zero();
639  for (unsigned int i = 0; i <= N; i++) {
640  F -= M[i] * (w.cross(w.cross(this->r[i] - r)));
641  }
642  return F;
643  }
644 };
645 
646 } // ::moordyn
#define LOGERR
Log an error.
Definition: Log.hpp:69
#define DECLDIR
Prefix to export C functions on the compiled library.
Definition: MoorDynAPI.h:68
A generic instance.
Definition: Instance.hpp:55
A mooring line.
Definition: Line.hpp:71
A Logging utility.
Definition: Log.hpp:149
A cylindrical rod.
Definition: Rod.hpp:65
const size_t stateDims() const
Get the dimension of the state variable.
Definition: Rod.hpp:590
void getNetForceAndMass(vec6 &Fnet_out, mat6 &M_out)
Calculate the force and mass contributions of the point on the parent body.
Definition: Rod.hpp:568
size_t rodId
Rod ID.
Definition: Rod.hpp:275
unsigned int getN() const
Number of segments.
Definition: Rod.hpp:364
const mat6 getM() const
Get the rod mass matrix.
Definition: Rod.hpp:552
std::pair< real, real > getDrag() const
Get the drag coefficients.
Definition: Rod.hpp:417
static string TypeName(types t)
Return a string with the name of a type.
Definition: Rod.hpp:257
void setDrag(real cdn, real cdt)
Set the drag coefficients.
Definition: Rod.hpp:423
void initialize(InstanceStateVarView r)
Initialize a free instance.
Definition: Rod.hpp:349
real roll
The roll angle (useful for output)
Definition: Rod.hpp:282
const size_t stateN() const
Get the number of state variables required by this instance.
Definition: Rod.hpp:583
void getState(XYZQuat &pos, vec6 &vel) const
Get the body kinematics.
Definition: Rod.hpp:448
void scaleDrag(real scaler)
Scale the drag coefficients.
Definition: Rod.hpp:432
types type
Rod type.
Definition: Rod.hpp:279
vec getNodePos(unsigned int i) const
Get the position of a node.
Definition: Rod.hpp:372
void setTime(real time)
Set the line simulation time.
Definition: Rod.hpp:442
real pitch
The pitch angle (useful for output)
Definition: Rod.hpp:284
void setWaves(moordyn::WavesRef waves_in, moordyn::SeafloorRef seafloor_in)
Set the environmental data.
Definition: Rod.hpp:326
vec6 getUnfreeVel() const
Get the last setted velocity for an unfree rod.
Definition: Rod.hpp:499
vec getNodeVel(unsigned int i) const
Get the velocity of a node.
Definition: Rod.hpp:396
int number
Rod number.
Definition: Rod.hpp:277
types
Types of rods.
Definition: Rod.hpp:233
const leanvtk::VTPWriter * getVTK() const
Get the VTK writer.
Definition: Rod.hpp:622
std::pair< XYZQuat, vec6 > getState() const
Get the body kinematics.
Definition: Rod.hpp:457
MoorDyn2 C++ API namespace.
Definition: Body.cpp:27
vec3 vec
vec3 renaming
Definition: Misc.hpp:130
std::shared_ptr< Waves > WavesRef
Definition: Body.hpp:53
Eigen::Block< InstanceStateVar, Eigen::Dynamic > InstanceStateVarView
View of the State variables for a particular instance.
Definition: Misc.hpp:167
Eigen::Vector6d vec6
6-D vector of real numbers
Definition: Misc.hpp:126
Eigen::Matrix6d mat6
6x6 matrix of real numbers
Definition: Misc.hpp:138
EndPoints
End point qualifiers.
Definition: Misc.hpp:570
double real
Real numbers wrapper. It is either double or float.
Definition: Misc.hpp:118
std::shared_ptr< Seafloor > SeafloorRef
Shared pointer.
Definition: Seafloor.hpp:109
Definition: Misc.hpp:1292
Definition: Misc.hpp:1188
Joint of a point and a quaternion.
Definition: Misc.hpp:304