Skip to content

Commit ef48e40

Browse files
feat(core): Create a base class for the instances which provides a unique identifier
1 parent fa23498 commit ef48e40

File tree

11 files changed

+137
-15
lines changed

11 files changed

+137
-15
lines changed

source/Body.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ using namespace std;
3939
namespace moordyn {
4040

4141
Body::Body(moordyn::Log* log, size_t id)
42-
: io::IO(log)
42+
: Instance(log)
4343
, bodyId(id)
4444
{
4545
#ifdef USE_VTK

source/Body.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#pragma once
4141

4242
#include "Misc.hpp"
43-
#include "IO.hpp"
43+
#include "Instance.hpp"
4444
#include "Util/CFL.hpp"
4545
#include <vector>
4646
#include <utility>
@@ -69,11 +69,8 @@ class Rod;
6969
* In the configuration file the options are:
7070
*
7171
* Name/ID, X0, Y0, Z0, Xcg, Ycg, Zcg, M, V, IX, IY, IZ, CdA-x,y,z Ca-x,y,z
72-
*
73-
* moordyn::Body extends the io::IO class, allowing it to perform input/output
74-
* in a consistent manner.
7572
*/
76-
class DECLDIR Body final : public io::IO, public SuperCFL
73+
class DECLDIR Body final : public Instance, public SuperCFL
7774
{
7875
public:
7976
/** @brief Costructor

source/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(MOORDYN_SRCS
22
Body.cpp
33
Point.cpp
4+
Instance.cpp
45
IO.cpp
56
Line.cpp
67
Log.cpp
@@ -23,6 +24,7 @@ set(MOORDYN_SRCS
2324
set(MOORDYN_HEADERS
2425
Body.hpp
2526
Point.hpp
27+
Instance.hpp
2628
IO.hpp
2729
Line.hpp
2830
Log.hpp

source/Instance.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2019 Matt Hall <[email protected]>
3+
*
4+
* This file is part of MoorDyn. MoorDyn is free software: you can redistribute
5+
* it and/or modify it under the terms of the GNU General Public License as
6+
* published by the Free Software Foundation, either version 3 of the License,
7+
* or (at your option) any later version.
8+
*
9+
* MoorDyn is distributed in the hope that it will be useful, but WITHOUT ANY
10+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11+
12+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with MoorDyn. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include "Instance.hpp"
19+
#include <atomic>
20+
21+
namespace moordyn {
22+
23+
std::atomic<size_t> __instances_counter(0);
24+
25+
Instance::Instance(moordyn::Log* log)
26+
: io::IO(log)
27+
{
28+
_id = __instances_counter++;
29+
}
30+
31+
} // ::moordyn

source/Instance.hpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
31+
/** @file Instance.hpp
32+
* Base class for the moordyn::Body, moordyn::Line, moordyn::Point and
33+
* moordyn::Rod objects.
34+
*/
35+
36+
#pragma once
37+
38+
#include "IO.hpp"
39+
#include "Log.hpp"
40+
#include <utility>
41+
42+
namespace moordyn {
43+
44+
class Waves;
45+
typedef std::shared_ptr<Waves> WavesRef;
46+
47+
class Point;
48+
class Rod;
49+
50+
/** @class Instance Instance.hpp
51+
* @brief A generic instance
52+
*/
53+
class DECLDIR Instance : public io::IO
54+
{
55+
public:
56+
/** @brief Costructor
57+
* @param log Logging handler defining where/how results should be logged.
58+
*/
59+
Instance(moordyn::Log* log);
60+
61+
/** @brief Destructor
62+
*/
63+
virtual ~Instance() = default;
64+
65+
/** @brief Get the unique identifier of this instance
66+
*
67+
* The unique identifier is a growing index which identifies every single
68+
* created instance. Even if an instance is removed, the index will remain
69+
* taken.
70+
* @return The unique identifier
71+
*/
72+
inline const size_t Id() const { return _id; }
73+
74+
/** @brief Get the number of state variables required by this instance
75+
*
76+
* This can be seen as the number of rows on the state variables holder.
77+
* @return The number of state variables
78+
*/
79+
virtual inline const size_t state_n() const { return 1; }
80+
81+
/** @brief Get the dimension of the state variable.
82+
*
83+
* This can be seen as the number of columns on the state variables holder.
84+
* @return The dimension of the state variable
85+
*/
86+
virtual inline const size_t state_dims() const { return 6; }
87+
private:
88+
/// Unique identifier of this instance
89+
size_t _id;
90+
};
91+
92+
} // ::moordyn

source/Line.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace moordyn {
5151
using namespace waves;
5252

5353
Line::Line(moordyn::Log* log, size_t lineId)
54-
: io::IO(log)
54+
: Instance(log)
5555
, lineId(lineId)
5656
, isPb(false)
5757
{

source/Line.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#pragma once
3636

3737
#include "Misc.hpp"
38-
#include "IO.hpp"
38+
#include "Instance.hpp"
3939
#include "Seafloor.hpp"
4040
#include "Util/CFL.hpp"
4141
#include <utility>
@@ -70,7 +70,7 @@ typedef Eigen::Ref<Eigen::Matrix<list, Eigen::Dynamic, 1>> StateVarRef;
7070
* The integration time step (moordyn::MoorDyn.dtM0) should be smaller than
7171
* this natural period to avoid numerical instabilities
7272
*/
73-
class DECLDIR Line final : public io::IO, public NatFreqCFL
73+
class DECLDIR Line final : public Instance, public NatFreqCFL
7474
{
7575
public:
7676
/** @brief Constructor

source/Point.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
namespace moordyn {
4747

4848
Point::Point(moordyn::Log* log, size_t id)
49-
: io::IO(log)
49+
: Instance(log)
5050
, seafloor(nullptr)
5151
, pointId(id)
5252
{

source/Point.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#pragma once
3636

3737
#include "Misc.hpp"
38-
#include "IO.hpp"
38+
#include "Instance.hpp"
3939
#include "Seafloor.hpp"
4040
#include "Util/CFL.hpp"
4141
#include <utility>
@@ -67,7 +67,7 @@ typedef std::shared_ptr<Waves> WavesRef;
6767
* weight or float via the point's mass and volume parameters
6868
* - Coupled: The point position and velocity is externally imposed
6969
*/
70-
class DECLDIR Point final : public io::IO, public SuperCFL
70+
class DECLDIR Point final : public Instance, public SuperCFL
7171
{
7272
public:
7373
/** @brief Constructor

source/Rod.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace moordyn {
5454
// ... --- seg n-2 --- [node n-1] --- seg n-1 --- [point (node N)]
5555

5656
Rod::Rod(moordyn::Log* log, size_t rodId)
57-
: io::IO(log)
57+
: Instance(log)
5858
, seafloor(nullptr)
5959
, rodId(rodId)
6060
{

0 commit comments

Comments
 (0)