Skip to content

Commit 5c666b5

Browse files
committed
Refactor Wave classes.
- add elevation argument to virtual WaveBase functions for extracting wave velocity and acceleration (for calls where elevation was already querried). - add non-virtual WaveBase functions for extracting wave velocity and acceleration which conditionally call GetElevation. - add NoWaveParams and RegularWaveParams structs and corresponding constructors (for consistency with IrregularWaves). - move utility function `get_lower_index` to irregular_wave.cpp (so that external projects need not include the HydroChrono configuration header which is included through helper.h).
1 parent 1b81e26 commit 5c666b5

8 files changed

Lines changed: 108 additions & 102 deletions

File tree

include/hydroc/helper.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,20 @@
33

44
#pragma once
55

6-
#include <Eigen/Dense> // Need for the container function
76
#include <fstream>
87
#include <iostream>
9-
#include <hydroc/logging.h>
108
#include <string>
119
#include <filesystem> // C++17
1210

11+
#include <Eigen/Dense> // Need for the container function
12+
13+
#include <hydroc/logging.h>
14+
15+
//// RADU - why not use constants from Chrono?
1316
#ifndef M_PI
1417
#define M_PI 3.14159265358979323846
1518
#endif
1619

17-
/**@brief Returns last index of vector element below value.
18-
*
19-
* @param value Input value
20-
* @param ticks Array of ticks from which to find lower-bound index (assuming ascending order)
21-
*
22-
*/
23-
size_t get_lower_index(double value, const std::vector<double>& ticks);
24-
2520
/**@brief Base namespace for HydroChrono library
2621
*
2722
*/

include/hydroc/waves/irregular_wave.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class IrregularWaves : public WaveBase {
5151
void AddH5Data(std::vector<HydroData::IrregularWaveInfo>& irreg_h5_data, HydroData::SimulationParameters& sim_data);
5252

5353
double GetElevation(const Eigen::Vector3d& position, double time) override;
54-
Eigen::Vector3d GetVelocity(const Eigen::Vector3d& position, double time) override;
55-
Eigen::Vector3d GetAcceleration(const Eigen::Vector3d& position, double time) override;
54+
Eigen::Vector3d GetVelocity(const Eigen::Vector3d& position, double time, double elevation) override;
55+
Eigen::Vector3d GetAcceleration(const Eigen::Vector3d& position, double time, double elevation) override;
5656

5757
private:
5858
IrregularWaveParams params_;

include/hydroc/waves/regular_wave.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,34 @@
1414
// Forward declaration for internal utilities (not exposed in public API)
1515
// Wave utilities are internal implementation details
1616

17+
struct RegularWaveParams {
18+
unsigned int num_bodies_;
19+
double regular_wave_amplitude_;
20+
double regular_wave_omega_;
21+
double regular_wave_phase_ = 0.0;
22+
bool wave_stretching_ = true;
23+
};
24+
1725
class RegularWave : public WaveBase {
1826
public:
1927
RegularWave();
2028
explicit RegularWave(unsigned int num_b);
29+
explicit RegularWave(const RegularWaveParams& params);
2130

2231
void Initialize() override;
2332
Eigen::VectorXd GetForceAtTime(double t) override;
2433
WaveMode GetWaveMode() override { return mode_; }
2534

35+
//// RADU - eliminate these and use a RegularWaveParams struct (consistent with IrregularWaves)
2636
double regular_wave_amplitude_;
2737
double regular_wave_omega_;
2838
double regular_wave_phase_ = 0.0;
29-
bool wave_stretching_ = true;
3039

3140
void AddH5Data(std::vector<HydroData::RegularWaveInfo>& reg_h5_data, HydroData::SimulationParameters& sim_data);
3241

3342
double GetElevation(const Eigen::Vector3d& position, double time) override;
34-
Eigen::Vector3d GetVelocity(const Eigen::Vector3d& position, double time) override;
35-
Eigen::Vector3d GetAcceleration(const Eigen::Vector3d& position, double time) override;
43+
Eigen::Vector3d GetVelocity(const Eigen::Vector3d& position, double time, double elevation) override;
44+
Eigen::Vector3d GetAcceleration(const Eigen::Vector3d& position, double time, double elevation) override;
3645

3746
private:
3847
unsigned int num_bodies_;

include/hydroc/waves/wave_base.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
#include <Eigen/Dense>
1010

11+
//// RADU - why is this not in some namespace?
12+
//// - should these classes be DLL-exported (e.g., to allow AddWave(NoWave))?
13+
//// if not, why is this a public headers?
14+
1115
enum class WaveMode {
1216
noWaveCIC = 0,
1317
regular = 1,
@@ -18,29 +22,38 @@ class WaveBase {
1822
public:
1923
virtual ~WaveBase() = default;
2024

21-
virtual void Initialize() = 0;
22-
virtual Eigen::VectorXd GetForceAtTime(double t) = 0;
23-
virtual WaveMode GetWaveMode() = 0;
24-
virtual double GetElevation(const Eigen::Vector3d& position, double time) = 0;
25-
virtual Eigen::Vector3d GetVelocity(const Eigen::Vector3d& position, double time) = 0;
26-
virtual Eigen::Vector3d GetAcceleration(const Eigen::Vector3d& position, double time) = 0;
25+
virtual void Initialize() = 0;
26+
virtual Eigen::VectorXd GetForceAtTime(double t) = 0;
27+
virtual WaveMode GetWaveMode() = 0;
28+
virtual double GetElevation(const Eigen::Vector3d& position, double time) = 0;
29+
virtual Eigen::Vector3d GetVelocity(const Eigen::Vector3d& position, double time, double elevation) = 0;
30+
virtual Eigen::Vector3d GetAcceleration(const Eigen::Vector3d& position, double time, double elevation) = 0;
31+
32+
Eigen::Vector3d GetVelocity(const Eigen::Vector3d& position, double time);
33+
Eigen::Vector3d GetAcceleration(const Eigen::Vector3d& position, double time);
2734

28-
double mwl_ = 0.0;
29-
double g_ = 9.81;
30-
double water_depth_ = 0.0;
35+
double mwl_ = 0.0;
36+
double g_ = 9.81;
37+
double water_depth_ = 0.0;
38+
bool wave_stretching_ = true;
39+
};
40+
41+
struct NoWaveParams {
42+
unsigned int num_bodies_;
3143
};
3244

3345
class NoWave : public WaveBase {
3446
public:
3547
NoWave();
3648
explicit NoWave(unsigned int num_b);
49+
explicit NoWave(const NoWaveParams& params);
3750

3851
void Initialize() override {}
3952
Eigen::VectorXd GetForceAtTime(double t) override;
4053
WaveMode GetWaveMode() override { return mode_; }
4154
double GetElevation(const Eigen::Vector3d&, double) override { return 0.0; }
42-
Eigen::Vector3d GetVelocity(const Eigen::Vector3d&, double) override { return Eigen::Vector3d(0.0, 0.0, 0.0); }
43-
Eigen::Vector3d GetAcceleration(const Eigen::Vector3d&, double) override { return Eigen::Vector3d(0.0, 0.0, 0.0); }
55+
Eigen::Vector3d GetVelocity(const Eigen::Vector3d&, double, double) override { return Eigen::Vector3d(0.0, 0.0, 0.0); }
56+
Eigen::Vector3d GetAcceleration(const Eigen::Vector3d&, double, double) override { return Eigen::Vector3d(0.0, 0.0, 0.0); }
4457

4558
private:
4659
unsigned int num_bodies_;

src/hydro/utils/helper.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,6 @@ bool hydroc::GetCLIArguments(int argc,
7777
return true;
7878
}
7979

80-
size_t get_lower_index(double value, const std::vector<double>& ticks) {
81-
auto it = std::upper_bound(ticks.begin(), ticks.end(), value);
82-
// get nearest-below index
83-
size_t idx = it - ticks.begin() - 1;
84-
// remove one if equal to value
85-
if (ticks[idx] == value) {
86-
idx -= 1;
87-
}
88-
if (idx <= 0 || idx >= ticks.size() - 1) {
89-
throw std::runtime_error("Could not find index for value " + std::to_string(value) + " in array with bounds (" +
90-
std::to_string(ticks.front()) + ", " + std::to_string(ticks.back()) + ").");
91-
}
92-
// return index
93-
return idx;
94-
}
95-
9680
using std::filesystem::path;
9781

9882
static path DATADIR{};

src/hydro/waves/irregular_wave.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
#include <sstream>
1919
#include <unsupported/Eigen/Splines>
2020

21-
#include <hydroc/helper.h>
2221
#include <hydroc/logging.h>
2322

24-
IrregularWaves::IrregularWaves(const IrregularWaveParams& params) : params_(params) {}
23+
IrregularWaves::IrregularWaves(const IrregularWaveParams& params) : params_(params) {
24+
wave_stretching_ = params.wave_stretching_;
25+
}
2526

2627
void IrregularWaves::InitializeIRFVectors() {
2728
ex_irf_sampled_.resize(params_.num_bodies_);
@@ -151,38 +152,18 @@ void IrregularWaves::AddH5Data(std::vector<HydroData::IrregularWaveInfo>& irreg_
151152
InitializeIRFVectors();
152153
}
153154

154-
Eigen::Vector3d IrregularWaves::GetVelocity(const Eigen::Vector3d& position, double time) {
155-
auto position_stretched = position;
156-
if (params_.wave_stretching_) {
157-
position_stretched = GetWheelerStretchedPosition(position, GetElevation(position, time), water_depth_, mwl_);
158-
}
159-
160-
return GetWaterVelocityIrregular(position_stretched,
161-
time,
162-
spectrum_frequencies_,
163-
spectral_densities_,
164-
spectral_widths_,
165-
wave_phases_,
166-
wavenumbers_,
167-
water_depth_,
168-
mwl_);
155+
Eigen::Vector3d IrregularWaves::GetVelocity(const Eigen::Vector3d& position, double time, double elevation) {
156+
auto position_stretched =
157+
params_.wave_stretching_ ? GetWheelerStretchedPosition(position, elevation, water_depth_, mwl_) : position;
158+
return GetWaterVelocityIrregular(position_stretched, time, spectrum_frequencies_, spectral_densities_,
159+
spectral_widths_, wave_phases_, wavenumbers_, water_depth_, mwl_);
169160
}
170161

171-
Eigen::Vector3d IrregularWaves::GetAcceleration(const Eigen::Vector3d& position, double time) {
172-
auto position_stretched = position;
173-
if (params_.wave_stretching_) {
174-
position_stretched = GetWheelerStretchedPosition(position, GetElevation(position, time), water_depth_, mwl_);
175-
}
176-
177-
return GetWaterAccelerationIrregular(position_stretched,
178-
time,
179-
spectrum_frequencies_,
180-
spectral_densities_,
181-
spectral_widths_,
182-
wave_phases_,
183-
wavenumbers_,
184-
water_depth_,
185-
mwl_);
162+
Eigen::Vector3d IrregularWaves::GetAcceleration(const Eigen::Vector3d& position, double time, double elevation) {
163+
auto position_stretched =
164+
params_.wave_stretching_ ? GetWheelerStretchedPosition(position, elevation, water_depth_, mwl_) : position;
165+
return GetWaterAccelerationIrregular(position_stretched, time, spectrum_frequencies_, spectral_densities_,
166+
spectral_widths_, wave_phases_, wavenumbers_, water_depth_, mwl_);
186167
}
187168

188169
double IrregularWaves::GetElevation(const Eigen::Vector3d& position, double time) {
@@ -356,6 +337,25 @@ void IrregularWaves::CalculateWidthIRF() {
356337
}
357338
}
358339

340+
// Return last index of vector element below value.
341+
// - value: Input value
342+
// - ticks: Array of ticks from which to find lower-bound index (assuming ascending order)
343+
static size_t get_lower_index(double value, const std::vector<double>& ticks) {
344+
auto it = std::upper_bound(ticks.begin(), ticks.end(), value);
345+
// get nearest-below index
346+
size_t idx = it - ticks.begin() - 1;
347+
// remove one if equal to value
348+
if (ticks[idx] == value) {
349+
idx -= 1;
350+
}
351+
if (idx <= 0 || idx >= ticks.size() - 1) {
352+
throw std::runtime_error("Could not find index for value " + std::to_string(value) + " in array with bounds (" +
353+
std::to_string(ticks.front()) + ", " + std::to_string(ticks.back()) + ").");
354+
}
355+
// return index
356+
return idx;
357+
}
358+
359359
double IrregularWaves::ExcitationConvolution(int body, int dof, double time) {
360360
double f_ex = 0.0;
361361
auto& irf_time_array = ex_irf_time_sampled_[body];

src/hydro/waves/regular_wave.cpp

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ RegularWave::RegularWave(unsigned int num_b) {
1616
num_bodies_ = num_b;
1717
}
1818

19+
RegularWave::RegularWave(const RegularWaveParams& params) {
20+
num_bodies_ = params.num_bodies_;
21+
regular_wave_amplitude_ = params.regular_wave_amplitude_;
22+
regular_wave_omega_ = params.regular_wave_omega_;
23+
regular_wave_phase_ = params.regular_wave_phase_;
24+
wave_stretching_ = params.wave_stretching_;
25+
}
26+
1927
void RegularWave::Initialize() {
2028
wavenumber_ = ComputeWaveNumber(regular_wave_omega_, water_depth_, g_);
2129
}
@@ -41,36 +49,20 @@ void RegularWave::AddH5Data(std::vector<HydroData::RegularWaveInfo>& reg_h5_data
4149
}
4250
}
4351

44-
Eigen::Vector3d RegularWave::GetVelocity(const Eigen::Vector3d& position, double time) {
45-
auto position_stretched = position;
46-
if (wave_stretching_) {
47-
position_stretched = GetWheelerStretchedPosition(position, GetElevation(position, time), water_depth_, mwl_);
48-
}
49-
return GetWaterVelocity(position_stretched,
50-
time,
51-
regular_wave_omega_,
52-
regular_wave_amplitude_,
53-
regular_wave_phase_,
54-
wavenumber_,
55-
water_depth_,
56-
mwl_);
52+
Eigen::Vector3d RegularWave::GetVelocity(const Eigen::Vector3d& position, double time, double elevation) {
53+
auto position_stretched =
54+
wave_stretching_ ? GetWheelerStretchedPosition(position, elevation, water_depth_, mwl_) : position;
55+
return GetWaterVelocity(position_stretched, time, regular_wave_omega_, regular_wave_amplitude_, regular_wave_phase_,
56+
wavenumber_, water_depth_, mwl_);
5757
}
5858

59-
Eigen::Vector3d RegularWave::GetAcceleration(const Eigen::Vector3d& position, double time) {
60-
auto position_stretched = position;
61-
if (wave_stretching_) {
62-
position_stretched = GetWheelerStretchedPosition(position, GetElevation(position, time), water_depth_, mwl_);
63-
}
64-
return GetWaterAcceleration(position_stretched,
65-
time,
66-
regular_wave_omega_,
67-
regular_wave_amplitude_,
68-
regular_wave_phase_,
69-
wavenumber_,
70-
water_depth_,
71-
mwl_);
59+
Eigen::Vector3d RegularWave::GetAcceleration(const Eigen::Vector3d& position, double time, double elevation) {
60+
auto position_stretched =
61+
wave_stretching_ ? GetWheelerStretchedPosition(position, elevation, water_depth_, mwl_) : position;
62+
return GetWaterAcceleration(position_stretched, time, regular_wave_omega_, regular_wave_amplitude_,
63+
regular_wave_phase_, wavenumber_, water_depth_, mwl_);
7264
}
73-
65+
7466
double RegularWave::GetElevation(const Eigen::Vector3d& position, double time) {
7567
return GetEta(position, time, regular_wave_omega_, regular_wave_amplitude_, regular_wave_phase_, wavenumber_);
7668
}

src/hydro/waves/wave_base.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55

66
#include <hydroc/waves/wave_base.h>
77

8+
Eigen::Vector3d WaveBase::GetVelocity(const Eigen::Vector3d& position, double time) {
9+
// Elevation needed only if stretching=true
10+
double elevation = wave_stretching_ ? GetElevation(position, time) : 0.0;
11+
return GetVelocity(position, time, elevation);
12+
}
13+
14+
Eigen::Vector3d WaveBase::GetAcceleration(const Eigen::Vector3d& position, double time) {
15+
// Elevation needed only if stretching=true
16+
double elevation = wave_stretching_ ? GetElevation(position, time) : 0.0;
17+
return GetAcceleration(position, time, elevation);
18+
}
19+
820
NoWave::NoWave() : num_bodies_(1) {}
921

1022
NoWave::NoWave(unsigned int num_b) : num_bodies_(num_b) {}
1123

24+
NoWave::NoWave(const NoWaveParams& params) : num_bodies_(params.num_bodies_) {}
25+
1226
Eigen::VectorXd NoWave::GetForceAtTime(double) {
1327
unsigned int dof = num_bodies_ * 6;
1428
Eigen::VectorXd f(dof);
@@ -17,4 +31,3 @@ Eigen::VectorXd NoWave::GetForceAtTime(double) {
1731
}
1832
return f;
1933
}
20-

0 commit comments

Comments
 (0)