Skip to content

Commit cdac02f

Browse files
fix(core): Give a common Instance::initialize() method
1 parent be0a344 commit cdac02f

File tree

6 files changed

+64
-21
lines changed

6 files changed

+64
-21
lines changed

source/Body.hpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,27 @@ class DECLDIR Body final : public Instance, public SuperCFL
255255
*/
256256
inline vec6 getUnfreeVel() const { return rd_ves; }
257257

258-
/** @brief Initialize the FREE point state
258+
/** @brief Initialize a free instance
259+
* @param r The output state variable
259260
* @return The 6-dof position (first) and the 6-dof velocity (second)
260-
* @throw moordyn::invalid_value_error If the body is not of type
261-
* moordyn::Body::FREE
261+
* @throw moordyn::invalid_value_error If the instance does not have free
262+
* states. e.g. a coupled body controlled from outside
262263
* @throws moordyn::output_file_error If an outfile has been provided, but
263264
* it cannot be written
265+
* @{
264266
*/
265267
std::pair<XYZQuat, vec6> initialize();
266268

269+
inline void initialize(InstanceStateVarView r)
270+
{
271+
const auto [pos, vel] = initialize();
272+
r.row(0).head<7>() = pos.toVec7();
273+
r.row(0).tail<6>() = vel;
274+
}
275+
/**
276+
* @}
277+
*/
278+
267279
/** @brief Initialize the free body
268280
*
269281
* Those are the bodies with type moordyn::Body::FREE

source/Instance.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ class DECLDIR Instance : public io::IO
7575
*/
7676
inline const size_t id() const { return _id; }
7777

78+
/** @brief Initialize a free instance
79+
* @param r The output state variable
80+
* @throw moordyn::invalid_value_error If the instance does not have free
81+
* states. e.g. a coupled body controlled from outside
82+
*/
83+
virtual void initialize(InstanceStateVarView r) = 0;
84+
7885
/** @brief Set the state
7986
* @param r The state variable
8087
*/

source/Line.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,28 @@ class DECLDIR Line final : public Instance, public NatFreqCFL
343343
}
344344

345345
/** @brief Compute the stationary Initial Condition (IC)
346+
* @param r The output state variable
346347
* @return The states, i.e. the positions of the internal nodes
347348
* (first) and the velocities of the internal nodes (second)
348349
* @throws moordyn::output_file_error If an outfile has been provided, but
349350
* it cannot be written
350351
* @throws invalid_value_error If there is no enough water depth
352+
* @{
351353
*/
352354
std::pair<std::vector<vec>, std::vector<vec>> initialize();
353355

356+
inline void initialize(InstanceStateVarView r)
357+
{
358+
const auto [pos, vel] = initialize();
359+
for (unsigned int i = 0; i < N - 1; i++) {
360+
r.row(i).head<3>() = pos[i];
361+
r.row(i).segment<3>(3) = vel[i];
362+
}
363+
}
364+
/**
365+
* @}
366+
*/
367+
354368
/** @brief Number of segments
355369
*
356370
* The number of nodes can be computed as moordyn::Line::getN() + 1

source/Point.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,22 @@ class DECLDIR Point final : public Instance, public SuperCFL
243243
inline std::vector<attachment> getLines() const { return attached; }
244244

245245
/** @brief Initialize the FREE point state
246+
* @param r The output state variable
246247
* @return The position (first) and the velocity (second)
247248
* @throws moordyn::invalid_value_error If it is not a FREE point
248249
*/
249250
std::pair<vec, vec> initialize();
250251

252+
inline void initialize(InstanceStateVarView r)
253+
{
254+
const auto [pos, vel] = initialize();
255+
r.row(0).head<3>() = pos;
256+
r.row(0).tail<3>() = vel;
257+
}
258+
/**
259+
* @}
260+
*/
261+
251262
/** @brief Get the point position
252263
* @return The position [x,y,z]
253264
*/

source/Rod.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ class DECLDIR Rod final : public Instance, public SuperCFL
336336
inline void openoutput();
337337

338338
/** @brief Initialize the rod state
339+
* @param r The output state variable
339340
* @return The position and orientation angles (first) and the linear and
340341
* angular velocity (second)
341342
* @note moordyn::Rod::r6 and moordyn::Rod::v6 must already be set
@@ -346,6 +347,16 @@ class DECLDIR Rod final : public Instance, public SuperCFL
346347
*/
347348
std::pair<XYZQuat, vec6> initialize();
348349

350+
inline void initialize(InstanceStateVarView r)
351+
{
352+
const auto [pos, vel] = initialize();
353+
r.row(0).head<7>() = pos.toVec7();
354+
r.row(0).tail<6>() = vel;
355+
}
356+
/**
357+
* @}
358+
*/
359+
349360
/** @brief Number of segments
350361
*
351362
* The number of nodes can be computed as moordyn::Rod::getN() + 1

source/Time.hpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -532,41 +532,29 @@ class SchemeBase : public Scheme
532532
if ((bodies[i]->type != Body::FREE) &&
533533
(bodies[i]->type != Body::CPLDPIN))
534534
continue;
535-
auto [pos, vel] = bodies[i]->initialize();
536-
auto r = AS_STATE(_r[0])->get(bodies[i]);
537-
r.row(0).head(7) = pos.toVec7();
538-
r.row(0).tail(6) = vel;
535+
bodies[i]->initialize(AS_STATE(_r[0])->get(bodies[i]));
536+
for (unsigned int j = 0; j < NDERIV; j++)
537+
AS_STATE(_rd[j])->get(bodies[i]).setZero();
539538
}
540539

541540
for (unsigned int i = 0; i < rods.size(); i++) {
542541
if ((rods[i]->type != Rod::FREE) && (rods[i]->type != Rod::PINNED))
543542
continue;
544-
auto [pos, vel] = rods[i]->initialize();
545-
auto r = AS_STATE(_r[0])->get(rods[i]);
546-
r.row(0).head(7) = pos.toVec7();
547-
r.row(0).tail(6) = vel;
543+
rods[i]->initialize(AS_STATE(_r[0])->get(rods[i]));
548544
for (unsigned int j = 0; j < NDERIV; j++)
549545
AS_STATE(_rd[j])->get(rods[i]).setZero();
550546
}
551547

552548
for (unsigned int i = 0; i < points.size(); i++) {
553549
if (points[i]->type != Point::FREE)
554550
continue;
555-
auto [pos, vel] = points[i]->initialize();
556-
auto r = AS_STATE(_r[0])->get(points[i]);
557-
r.row(0).head(3) = pos;
558-
r.row(0).tail(3) = vel;
551+
points[i]->initialize(AS_STATE(_r[0])->get(points[i]));
559552
for (unsigned int j = 0; j < NDERIV; j++)
560553
AS_STATE(_rd[j])->get(points[i]).setZero();
561554
}
562555

563556
for (unsigned int i = 0; i < lines.size(); i++) {
564-
auto [pos, vel] = lines[i]->initialize();
565-
auto r = AS_STATE(_r[0])->get(lines[i]);
566-
for (unsigned int j = 0; j < pos.size(); j++) {
567-
r.row(j).head(3) = pos[j];
568-
r.row(j).segment(3, 3) = vel[j];
569-
}
557+
lines[i]->initialize(AS_STATE(_r[0])->get(lines[i]));
570558
for (unsigned int j = 0; j < NDERIV; j++)
571559
AS_STATE(_rd[j])->get(lines[i]).setZero();
572560
}

0 commit comments

Comments
 (0)