MoorDyn
Waves.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 "Log.hpp"
39 #include "Line.hpp"
40 #include "Point.hpp"
41 #include "Body.hpp"
42 #include "Rod.hpp"
43 #include "Waves/SpectrumKin.hpp"
44 #include <vector>
45 
46 namespace moordyn {
47 
48 namespace time {
49 class Scheme;
50 }
51 
52 class Seafloor;
53 typedef std::shared_ptr<Seafloor> SeafloorRef;
54 
56 template<class T>
57 using Vec2D = std::vector<std::vector<T>>;
58 
60 template<class T>
61 using Vec3D = std::vector<std::vector<std::vector<T>>>;
62 
64 template<class T>
65 using Vec4D = std::vector<std::vector<std::vector<std::vector<T>>>>;
66 
71 static inline Vec2D<real>
72 init2DArray(unsigned int nx, unsigned int ny)
73 {
74  return Vec2D<real>(nx, std::vector<real>(ny, 0.0));
75 }
76 
82 static inline Vec3D<real>
83 init3DArray(unsigned int nx, unsigned int ny, unsigned int nz)
84 {
85  return Vec3D<real>(nx, init2DArray(ny, nz));
86 }
87 
94 static inline Vec4D<vec3>
95 init4DArrayVec(unsigned int nx,
96  unsigned int ny,
97  unsigned int nz,
98  unsigned int nw)
99 {
100  return Vec4D<vec3>(
101  nx,
102  Vec3D<vec3>(ny, Vec2D<vec3>(nz, std::vector<vec3>(nw, vec3::Zero()))));
103 }
110 static inline Vec4D<real>
111 init4DArray(unsigned int nx, unsigned int ny, unsigned int nz, unsigned int nw)
112 {
113  return Vec4D<real>(nx, init3DArray(ny, nz, nw));
114 }
115 
119 {
120  real waterDepth;
121  SeafloorRef seafloor{};
122  real getAverageDepth() const;
123  real getDepth(const vec2& pos) const;
124 };
125 
131 {
132  public:
133  virtual ~AbstractCurrentKin() {};
143  virtual void getCurrentKin(const vec3& pos,
144  real time,
145  const SeafloorProvider& seafloor,
146  vec3* vel,
147  vec3* acc) = 0;
148 };
149 
156 {
157  public:
158  virtual ~AbstractWaveKin() {};
169  virtual void getWaveKin(const vec3& pos,
170  real time,
171  const SeafloorProvider& seafloor,
172  real* zeta,
173  vec3* vel,
174  vec3* acc,
175  real* pdyn) = 0;
176 };
177 
187 {
188  waves::SpectrumKin spectrumKin;
189 
190  public:
199  SpectrumKinWrapper(waves::SpectrumKin&& spectrum)
200  : spectrumKin(spectrum)
201  {
202  }
203 
204  ~SpectrumKinWrapper() override = default;
205 
206  void getWaveKin(const vec3& pos,
207  real time,
208  const SeafloorProvider& seafloor,
209  real* zeta,
210  vec3* vel,
211  vec3* acc,
212  real* pdyn) override
213  {
214  if (pdyn) {
215  *pdyn = 0.0;
216  }
217 
218  real avgDepth = seafloor.getAverageDepth();
219  real actualDepth = seafloor.getDepth(pos.head<2>());
220  spectrumKin.getWaveKin(
221  pos, time, avgDepth, actualDepth, zeta, vel, acc);
222  }
223 };
232 class GridXYZT
233 {
234  public:
235  GridXYZT(const std::vector<real>& px,
236  const std::vector<real>& py,
237  const std::vector<real>& pz,
238  unsigned int nt,
239  real dt)
240  : nx(static_cast<unsigned int>(px.size()))
241  , ny(static_cast<unsigned int>(py.size()))
242  , nz(static_cast<unsigned int>(pz.size()))
243  , nt(nt)
244  , dtWave(dt)
245  , px(px)
246  , py(py)
247  , pz(pz)
248  {
249  }
251  unsigned int nx;
253  unsigned int ny;
255  unsigned int nz;
257  unsigned int nt = 0;
260 
261  protected:
263  std::vector<real> px;
265  std::vector<real> py;
267  std::vector<real> pz;
268 };
269 
276 class WaveGrid
277  : public AbstractWaveKin
278  , public GridXYZT
279  , LogUser
280 {
281  public:
282  WaveGrid(moordyn::Log* log,
283  const std::vector<real>& px,
284  const std::vector<real>& py,
285  const std::vector<real>& pz,
286  unsigned int nt,
287  real dt)
288  : GridXYZT(px, py, pz, nt, dt)
289  , LogUser(log)
290  {
291  }
292  ~WaveGrid() override = default;
293 
294  void allocateKinematicArrays();
295 
296  void getWaveKin(const vec3& pos,
297  real time,
298  const SeafloorProvider& seafloor,
299  real* zeta,
300  vec3* vel,
301  vec3* acc,
302  real* pdyn) override;
303 
304  inline Vec3D<real>& Zetas() { return zetas; }
305  inline const Vec3D<real>& getZetas() const { return zetas; }
306 
307  inline Vec4D<real>& PDyn() { return pDyn; }
308  inline const Vec4D<real>& getPDyn() const { return pDyn; }
309 
310  inline Vec4D<vec3>& WaveVel() { return wave_vel; }
311  inline const Vec4D<vec3>& getWaveVel() const { return wave_vel; }
312 
313  inline Vec4D<vec3>& WaveAcc() { return wave_acc; }
314  inline const Vec4D<vec3>& getWaveAcc() const { return wave_acc; }
315 
316  inline const std::vector<real>& Px() const { return px; }
317  inline const std::vector<real>& Py() const { return py; }
318  inline const std::vector<real>& Pz() const { return pz; }
319 
320  private:
322  Vec3D<real> zetas;
324  Vec4D<real> pDyn;
326  Vec4D<vec3> wave_vel;
328  Vec4D<vec3> wave_acc;
329 };
330 
337  : public AbstractCurrentKin
338  , public GridXYZT
339  , LogUser
340 {
341  public:
343  const std::vector<real>& px,
344  const std::vector<real>& py,
345  const std::vector<real>& pz,
346  unsigned int nt,
347  real dt)
348  : GridXYZT(px, py, pz, nt, dt)
349  , LogUser(log)
350  {
351  }
352 
353  ~CurrentGrid() override = default;
354 
355  void allocateKinematicArrays();
356 
357  void getCurrentKin(const vec3& pos,
358  real time,
359  const SeafloorProvider& seafloor,
360  vec3* vel,
361  vec3* acc) override;
362 
363  inline Vec4D<vec3>& CurrentVel() { return current_vel; }
364  inline const Vec4D<vec3>& getCurrentVel() const { return current_vel; }
365 
366  inline Vec4D<vec3>& CurrentAcc() { return current_acc; }
367  inline const Vec4D<vec3>& getCurrentAcc() const { return current_acc; }
368 
369  inline const std::vector<real>& Px() const { return px; }
370  inline const std::vector<real>& Py() const { return py; }
371  inline const std::vector<real>& Pz() const { return pz; }
372 
373  private:
375  Vec4D<vec3> current_vel;
377  Vec4D<vec3> current_acc;
378 };
379 
397 class Waves : public LogUser
398 {
399  public:
401  Waves(moordyn::Log* log);
403  ~Waves();
404 
413  std::vector<vec3> getWaveKinematicsPoints();
414 
423  void setWaveKinematics(std::vector<vec> const& U,
424  std::vector<vec> const& Ud);
425 
430  void updateWaves();
431 
439  void addLine(moordyn::Line* line);
440 
448  void addRod(moordyn::Rod* rod);
449 
457  void addBody(moordyn::Body* body);
458 
466  void addPoint(moordyn::Point* point);
467 
468  using NodeKinReturnType = std::tuple<const std::vector<real>&,
469  const std::vector<vec3>&,
470  const std::vector<vec3>&,
471  const std::vector<real>&>;
478  NodeKinReturnType getWaveKinLine(size_t lineId);
479 
486  NodeKinReturnType getWaveKinRod(size_t rodId);
487 
496  NodeKinReturnType getWaveKinBody(size_t bodyId);
497 
506  NodeKinReturnType getWaveKinPoint(size_t pointId);
507 
517 
528  void getWaveKin(const vec3& pos,
529  real& zeta,
530  vec3& vel,
531  vec3& acc,
532  real& pdyn,
533  Seafloor* seafloor = nullptr);
534 
535  private:
544  template<class C>
545  struct NodeKinematics
546  {
547  std::vector<const C*> structures;
548  std::vector<std::vector<real>> zetas;
549  std::vector<std::vector<vec3>> U;
550  std::vector<std::vector<vec3>> Ud;
551  std::vector<std::vector<real>> Pdyn;
552 
553  NodeKinReturnType operator[](size_t idx)
554  {
555 
556  return { zetas[idx], U[idx], Ud[idx], Pdyn[idx] };
557  }
558  };
559 
566  struct AllNodesKin
567  {
568  NodeKinematics<moordyn::Line> lines;
569  NodeKinematics<moordyn::Body> bodies;
570  NodeKinematics<moordyn::Rod> rods;
571  NodeKinematics<moordyn::Point> points;
572  };
573 
575  AllNodesKin nodeKin;
578  AllNodesKin waveKin;
587  template<class C>
588  void genericAdd(const C* const structure,
589  unsigned int num_nodes,
590  NodeKinematics<C>& nodeKinematics)
591  {
592  nodeKinematics.structures.push_back(structure);
593  nodeKinematics.zetas.emplace_back(num_nodes, 0.0);
594  nodeKinematics.U.emplace_back(num_nodes, vec3::Zero());
595  nodeKinematics.Ud.emplace_back(num_nodes, vec3::Zero());
596  nodeKinematics.Pdyn.emplace_back(num_nodes, 0.0);
597  }
598 
608  template<typename F>
609  void kinematicsForAllNodes(AllNodesKin& nodeKinematics, F f);
610 
612  std::unique_ptr<AbstractWaveKin> waveKinematics{};
614  std::unique_ptr<AbstractCurrentKin> currentKinematics{};
615 
623  std::unique_ptr<WaveGrid> waveGrid{};
624 
626  EnvCondRef env{};
627  // Keep a reference to any potential 3d seafloor
628  SeafloorRef seafloor{};
630  real g;
632  real rho_w;
633 
635  moordyn::time::Scheme* _t_integrator;
636 
637  // ------------ from Line object... -----------
638  // new additions for handling waves in-object and precalculating them (not
639  // necessarily used right now)
640  // int WaveMod;
641  // int WaveStMod;
642  // double Hs;
643  // double Tp;
644  // double gamma;
645  // float beta; // wave heading
646  //
647  // vector< double > Ucurrent; // constant uniform current to add (three
648  // components)
649 
650  public:
653  typedef enum
654  {
661  } coordtypes;
662 
676  void setup(EnvCondRef env,
677  SeafloorRef seafloor,
678  time::Scheme* t,
679  const char* folder = "Mooring/");
680 };
681 
682 // other relevant functions being thrown into this file for now (should move to
683 // Misc?) <<<<
684 
694 std::vector<real>
695 gridAxisCoords(Waves::coordtypes coordtype, vector<string>& entries);
696 
700 typedef std::shared_ptr<Waves> WavesRef;
701 } // ::moordyn
An abstract class representing the capability of providing water current data at some point and time.
Definition: Waves.hpp:131
virtual void getCurrentKin(const vec3 &pos, real time, const SeafloorProvider &seafloor, vec3 *vel, vec3 *acc)=0
Get the velocity and acceleration at a specific position and time.
An abstract class representing having the capability of providing wave kinematics data.
Definition: Waves.hpp:156
virtual void getWaveKin(const vec3 &pos, real time, const SeafloorProvider &seafloor, real *zeta, vec3 *vel, vec3 *acc, real *pdyn)=0
Get the velocity, acceleration, wave height and dynamic pressure at a specific position and time.
A rigid body.
Definition: Body.hpp:72
Contains grid based current data.
Definition: Waves.hpp:340
void getCurrentKin(const vec3 &pos, real time, const SeafloorProvider &seafloor, vec3 *vel, vec3 *acc) override
Get the velocity and acceleration at a specific position and time.
Definition: Waves.cpp:204
A rectilinear grid with x, y, z, and t axes.
Definition: Waves.hpp:233
std::vector< real > px
grid x coordinate arrays
Definition: Waves.hpp:263
unsigned int nx
number of grid points in x direction
Definition: Waves.hpp:251
std::vector< real > py
grid y coordinate arrays
Definition: Waves.hpp:265
std::vector< real > pz
grid z coordinate arrays
Definition: Waves.hpp:267
unsigned int nz
number of grid points in z direction
Definition: Waves.hpp:255
real dtWave
time step for wave kinematics time series
Definition: Waves.hpp:259
unsigned int nt
number of time steps used in wave kinematics time series
Definition: Waves.hpp:257
unsigned int ny
number of grid points in y direction
Definition: Waves.hpp:253
A mooring line.
Definition: Line.hpp:71
A Logging utility.
Definition: Log.hpp:149
A helper for the entities to use the logger.
Definition: Log.hpp:223
LogUser(Log *log=NULL)
Constructor.
Definition: Log.hpp:231
A point for a line endpoint.
Definition: Point.hpp:69
A cylindrical rod.
Definition: Rod.hpp:65
Bathymetry description for MoorDyn.
Definition: Seafloor.hpp:52
Wrapper around waves::SpectrumKin to make it adhere to the AbstractWaveKin interface.
Definition: Waves.hpp:187
SpectrumKinWrapper(waves::SpectrumKin &&spectrum)
Construct a new Spectrum Kin Wrapper object.
Definition: Waves.hpp:199
void getWaveKin(const vec3 &pos, real time, const SeafloorProvider &seafloor, real *zeta, vec3 *vel, vec3 *acc, real *pdyn) override
Get the velocity, acceleration, wave height and dynamic pressure at a specific position and time.
Definition: Waves.hpp:206
Contains the data and functionality for the Wave grid kinematics modes.
Definition: Waves.hpp:280
void getWaveKin(const vec3 &pos, real time, const SeafloorProvider &seafloor, real *zeta, vec3 *vel, vec3 *acc, real *pdyn) override
Get the velocity, acceleration, wave height and dynamic pressure at a specific position and time.
Definition: Waves.cpp:124
Class that handles wave and current kinematics.
Definition: Waves.hpp:398
std::vector< vec3 > getWaveKinematicsPoints()
Get the positions of all the nodes with wave kinematics.
Definition: Waves.cpp:666
NodeKinReturnType getWaveKinBody(size_t bodyId)
Get the water kinematics for the body with given Id.
Definition: Waves.cpp:579
NodeKinReturnType getWaveKinRod(size_t rodId)
Get the water kinematics for the rod with given Id.
Definition: Waves.cpp:573
NodeKinReturnType getWaveKinPoint(size_t pointId)
Get the water kinematics for the point with given Id.
Definition: Waves.cpp:585
void addBody(moordyn::Body *body)
Adds a body to the list of structures we calculate water kinematics for.
Definition: Waves.cpp:539
real getWaveHeightPoint(vec2 point)
Gets the surface height at a given (x, y) point.
Definition: Waves.cpp:591
void updateWaves()
Recalculates any wave and current kinematics for all the nodes.
Definition: Waves.cpp:755
void addPoint(moordyn::Point *point)
Adds a point to the list of structures we calculate water kinematics for.
Definition: Waves.cpp:552
~Waves()
Destructor.
Definition: Waves.cpp:254
Waves(moordyn::Log *log)
Constructor.
Definition: Waves.cpp:247
void setWaveKinematics(std::vector< vec > const &U, std::vector< vec > const &Ud)
Set the wave kinematics for all the structural nodes.
Definition: Waves.cpp:681
coordtypes
Types of coordinates input on the grid file.
Definition: Waves.hpp:654
@ GRID_LATTICE
EQUISPACED POINTS.
Definition: Waves.hpp:660
@ GRID_SINGLE
Single point (0, 0, 0)
Definition: Waves.hpp:656
@ GRID_LIST
List of points.
Definition: Waves.hpp:658
void getWaveKin(const vec3 &pos, real &zeta, vec3 &vel, vec3 &acc, real &pdyn, Seafloor *seafloor=nullptr)
Get the wave kinematics at a point at the current time.
Definition: Waves.cpp:608
void setup(EnvCondRef env, SeafloorRef seafloor, time::Scheme *t, const char *folder="Mooring/")
Setup the wave kinematics.
Definition: Waves.cpp:257
void addRod(moordyn::Rod *rod)
Adds a rod to the list of structures we calculate water kinematics for.
Definition: Waves.cpp:527
NodeKinReturnType getWaveKinLine(size_t lineId)
Get the water kinematics for the line with id.
Definition: Waves.cpp:567
void addLine(moordyn::Line *line)
Adds a line to the list of structures we calculate water kinematics for.
Definition: Waves.cpp:511
Time scheme abstraction.
Definition: Time.hpp:59
MoorDyn2 C++ API namespace.
Definition: Body.cpp:27
std::shared_ptr< Waves > WavesRef
Definition: Body.hpp:53
std::vector< std::vector< std::vector< T > >> Vec3D
STL std::vector of 3 dimensions.
Definition: Waves.hpp:61
Eigen::Vector2d vec2
2-D vector of real numbers
Definition: Misc.hpp:120
double real
Real numbers wrapper. It is either double or float.
Definition: Misc.hpp:118
std::vector< std::vector< T > > Vec2D
STL std::vector of 2 dimensions.
Definition: Waves.hpp:57
std::vector< real > gridAxisCoords(Waves::coordtypes coordtype, vector< string > &entries)
Compute the coordinates from a grid definition entry line.
Definition: Waves.cpp:69
std::vector< std::vector< std::vector< std::vector< T > >> > Vec4D
STL std::vector of 4 dimensions.
Definition: Waves.hpp:65
std::shared_ptr< Seafloor > SeafloorRef
Shared pointer.
Definition: Seafloor.hpp:109
Eigen::Vector3d vec3
3-D vector of real numbers
Definition: Misc.hpp:122
Helper for moordyn::AbstractCurrentKin.
Definition: Waves.hpp:119