MoorDyn
Body.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 
40 #pragma once
41 
42 #include "Misc.hpp"
43 #include "Instance.hpp"
44 #include "Util/CFL.hpp"
45 #include "leanvtk/leanvtk.hpp"
46 #include <vector>
47 #include <utility>
48 
49 using namespace std;
50 
51 namespace moordyn {
52 
53 class Waves;
54 typedef std::shared_ptr<Waves> WavesRef;
55 
56 class Point;
57 class Rod;
58 
69 class DECLDIR Body final
70  : public Instance
71  , public SuperCFL
72 {
73  public:
78  Body(moordyn::Log* log, size_t id);
79 
82  ~Body();
83 
84  // ENVIRONMENTAL STUFF
86  EnvCondRef env;
89 
90  // unique to Body:
92  std::vector<moordyn::Point*> attachedP;
94  std::vector<Rod*> attachedR;
95 
97  std::vector<vec> rPointRel;
99  std::vector<vec6> r6RodRel;
100 
101  // Constants set at startup from input file
102 
117 
118  // degrees of freedom (or states)
121  // vec6 r6;
126 
133 
136 
142 
146 
148  ofstream* outfile;
149 
152  typedef enum
153  {
155  COUPLED = -1,
157  FREE = 0,
159  FIXED = 1,
161  CPLDPIN = 2,
162  // Some aliases
163  VESSEL = COUPLED,
164  ANCHOR = FIXED,
165  } types;
166 
171  static string TypeName(types t)
172  {
173  switch (t) {
174  case COUPLED:
175  return "COUPLED";
176  case CPLDPIN:
177  return "CPLDPIN";
178  case FREE:
179  return "FREE";
180  case FIXED:
181  return "FIXED";
182  }
183  return "UNKNOWN";
184  }
185 
187  size_t bodyId;
189  int number;
192 
207  void setup(int number,
208  types type,
209  vec6 r6,
210  vec rCG,
211  real M,
212  real V,
213  vec I,
214  vec6 CdA,
215  vec6 Ca,
216  EnvCondRef env_in,
217  shared_ptr<ofstream> outfile);
218 
224  void addPoint(moordyn::Point* point, vec coords);
225 
232  void addRod(Rod* rod, vec6 coords);
233 
244  void initializeUnfreeBody(vec6 r = vec6::Zero(),
245  vec6 rd = vec6::Zero(),
246  vec6 rdd = vec6::Zero());
247 
254  inline vec6 getUnfreeVel() const { return rd_ves; }
255 
265  std::pair<XYZQuat, vec6> initialize();
266 
268  {
269  const auto [pos, vel] = initialize();
270  r.row(0).head<7>() = pos.toVec7();
271  r.row(0).tail<6>() = vel;
272  }
287  void initializeBody(XYZQuat r = XYZQuat::Zero(), vec6 rd = vec6::Zero());
288 
292  inline void setWaves(moordyn::WavesRef waves_in) { waves = waves_in; }
293 
299  void setDependentStates();
300 
305  inline void getState(XYZQuat& pos, vec6& vel) const
306  {
307  pos = r7;
308  vel = v6;
309  }
310 
314  inline std::pair<XYZQuat, vec6> getState() const
315  {
316  return std::make_pair(r7, v6);
317  }
318 
322  inline const vec getPosition() const { return r7.pos; }
323 
327  inline const vec getAngles() const { return Quat2Euler(r7.quat); }
328  // inline vec getAngles() const { return r6(Eigen::seqN(3, 3)); }
329 
333  inline const vec getVelocity() const { return v6(Eigen::seqN(0, 3)); }
334 
338  inline const vec getAngularVelocity() const
339  {
340  return v6(Eigen::seqN(3, 3));
341  }
342 
346  const vec6 getFnet() const;
347 
351  inline const mat6& getM() const { return M; };
352 
359  real GetBodyOutput(OutChanProps outChan);
360 
364  inline void scaleDrag(real scaler) { bodyCdA *= scaler; }
365 
374  void initiateStep(vec6 r, vec6 rd, vec6 rdd);
375 
384  void updateFairlead(real time);
385 
390  void setState(const InstanceStateVarView r);
391 
400  void getStateDeriv(InstanceStateVarView drdt);
401 
406  void doRHS();
407 
408  void Output(real time);
409 
413  inline const size_t stateN() const { return 1; }
414 
420  inline const size_t stateDims() const { return 13; }
421 
431  std::vector<uint64_t> Serialize(void);
432 
439  uint64_t* Deserialize(const uint64_t* data);
440 
445  void saveVTK(const char* filename);
446 
452  const leanvtk::VTPWriter* getVTK() const { return &vtk; }
453 private:
455  leanvtk::VTPWriter vtk;
456 
459  void defaultVTK();
460 };
461 
462 } // ::moordyn
#define DECLDIR
Prefix to export C functions on the compiled library.
Definition: MoorDynAPI.h:68
A rigid body.
Definition: Body.hpp:72
types
Types of bodies.
Definition: Body.hpp:153
const vec getVelocity() const
Get the body velocity.
Definition: Body.hpp:333
const size_t stateN() const
Get the number of state variables required by this instance.
Definition: Body.hpp:413
vec6 rd_ves
fairlead velocity for coupled bodies [x/y/z]
Definition: Body.hpp:130
EnvCondRef env
Global struct that holds environmental settings.
Definition: Body.hpp:86
size_t bodyId
Body ID.
Definition: Body.hpp:187
const leanvtk::VTPWriter * getVTK() const
Get the VTK writer.
Definition: Body.hpp:452
vec6 bodyCdA
The product of the drag coefficient and the frontal area (m^2)
Definition: Body.hpp:114
void initializeBody(XYZQuat r=XYZQuat::Zero(), vec6 rd=vec6::Zero())
Initialize the free body.
std::vector< vec6 > r6RodRel
Attachment points of each rod.
Definition: Body.hpp:99
std::vector< Rod * > attachedR
Rods attached to this body.
Definition: Body.hpp:94
real bodyV
The volume.
Definition: Body.hpp:110
void setWaves(moordyn::WavesRef waves_in)
Set the environmental data.
Definition: Body.hpp:292
const vec getAngles() const
Get the body Euler XYZ angles.
Definition: Body.hpp:327
real bodyM
The mass.
Definition: Body.hpp:108
static string TypeName(types t)
Return a string with the name of a type.
Definition: Body.hpp:171
vec6 body_r6
The reference point.
Definition: Body.hpp:104
WavesRef waves
global Waves object
Definition: Body.hpp:88
void scaleDrag(real scaler)
Scale the drag coefficients.
Definition: Body.hpp:364
vec6 F6net
total force and moment vector on body
Definition: Body.hpp:135
vec6 bodyCa
The added mass coefficients.
Definition: Body.hpp:116
vec6 a6
body 6dof acceleration[x/y/z]
Definition: Body.hpp:125
vec body_rCG
The center of gravity.
Definition: Body.hpp:106
void initialize(InstanceStateVarView r)
Initialize a free instance.
Definition: Body.hpp:267
vec6 v6
body 6dof velocity[x/y/z]
Definition: Body.hpp:123
const vec getPosition() const
Get the body position.
Definition: Body.hpp:322
const size_t stateDims() const
Get the dimension of the state variable.
Definition: Body.hpp:420
XYZQuat r7
body 6dof position [x/y/z]
Definition: Body.hpp:120
const mat6 & getM() const
Get the mass and intertia matrix.
Definition: Body.hpp:351
vec6 r_ves
fairlead position for coupled bodies [x/y/z]
Definition: Body.hpp:128
std::vector< vec > rPointRel
Attachment points of each point.
Definition: Body.hpp:97
mat6 M
total body mass + added mass matrix including all elements
Definition: Body.hpp:138
mat6 M0
Definition: Body.hpp:141
std::vector< moordyn::Point * > attachedP
Points attached to this body.
Definition: Body.hpp:92
vec bodyI
The inertia diagonal components.
Definition: Body.hpp:112
vec6 rdd_ves
fairlead acceleration for coupled bodies [x/y/z]
Definition: Body.hpp:132
std::pair< XYZQuat, vec6 > getState() const
Get the body kinematics.
Definition: Body.hpp:314
ofstream * outfile
Pointer to moordyn::MoorDyn::outfileMain.
Definition: Body.hpp:148
types type
Type of body.
Definition: Body.hpp:191
vec6 getUnfreeVel() const
Get the last setted velocity for an unfree body.
Definition: Body.hpp:254
void getState(XYZQuat &pos, vec6 &vel) const
Get the body kinematics.
Definition: Body.hpp:305
const vec getAngularVelocity() const
Get the body angular velocity.
Definition: Body.hpp:338
int number
Body number.
Definition: Body.hpp:189
mat OrMat
Definition: Body.hpp:145
A generic instance.
Definition: Instance.hpp:55
A Logging utility.
Definition: Log.hpp:149
A point for a line endpoint.
Definition: Point.hpp:69
A cylindrical rod.
Definition: Rod.hpp:65
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
double real
Real numbers wrapper. It is either double or float.
Definition: Misc.hpp:118
mat3 mat
mat3 renaming
Definition: Misc.hpp:142
Definition: Misc.hpp:1292
Joint of a point and a quaternion.
Definition: Misc.hpp:304