Skip to content

Commit 4254f96

Browse files
committed
Encapsulate RegularWave members; add SetPeriod/SetOmega API
Replace public data members on RegularWave with validated setter/getter methods (SetAmplitude, SetOmega, SetPeriod, SetPhase) so users can specify wave frequency as either angular frequency (rad/s) or period (s). Omega remains the single stored quantity; period is computed on demand. RegularWaveParams also accepts either field, throwing if both are set. All demos, tests, and internal call sites updated.
1 parent 3737e63 commit 4254f96

11 files changed

Lines changed: 121 additions & 39 deletions

File tree

demos/oswec/demo_oswec_reg_waves.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ int main(int argc, char* argv[]) {
199199
bodies.push_back(base_body);
200200

201201
auto my_hydro_inputs = std::make_shared<RegularWave>();
202-
my_hydro_inputs->regular_wave_amplitude_ = 0.01;
203-
my_hydro_inputs->regular_wave_omega_ = (2 * CH_PI) / (periods[reg_wave_num - 1]);
202+
my_hydro_inputs->SetAmplitude(0.01);
203+
my_hydro_inputs->SetPeriod(periods[reg_wave_num - 1]);
204204

205205
//// attach hydrodynamic forces to body
206206
/*std::vector<std::shared_ptr<ChBody>> bodies;
@@ -260,8 +260,8 @@ int main(int argc, char* argv[]) {
260260
outputFile.precision(10);
261261
outputFile.width(12);
262262
outputFile << "Wave #: \t" << reg_wave_num << "\n";
263-
outputFile << "Wave amplitude (m): \t" << my_hydro_inputs->regular_wave_amplitude_ << "\n";
264-
outputFile << "Wave omega (rad/s): \t" << my_hydro_inputs->regular_wave_omega_ << "\n";
263+
outputFile << "Wave amplitude (m): \t" << my_hydro_inputs->GetAmplitude() << "\n";
264+
outputFile << "Wave omega (rad/s): \t" << my_hydro_inputs->GetOmega() << "\n";
265265
outputFile << std::left << std::setw(10) << "Time (s)" << std::right << std::setw(12) << "Pitch (rads)"
266266
<< std::endl;
267267
for (size_t i = 0; i < time_vector.size(); ++i)

demos/rm3/demo_rm3_mooring.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int main(int argc, char* argv[]) {
5050
system.SetTimestepperType(ChTimestepper::Type::HHT);
5151
system.SetSolverType(ChSolver::Type::SPARSE_LU);
5252
ChRealtimeStepTimer realtime_timer;
53-
double simulationDuration = 40.0;
53+
double simulationDuration = 300.0;
5454

5555
std::shared_ptr<hydroc::gui::UI> pui = hydroc::gui::CreateUI(visualizationOn);
5656
hydroc::gui::UI& ui = *pui.get();
@@ -107,9 +107,9 @@ int main(int argc, char* argv[]) {
107107
bodies.push_back(float_body1);
108108
bodies.push_back(plate_body2);
109109

110-
auto my_hydro_inputs = std::make_shared<RegularWave>();
111-
my_hydro_inputs->regular_wave_amplitude_ = 1.0;
112-
my_hydro_inputs->regular_wave_omega_ = 2.10;
110+
auto my_hydro_inputs = std::make_shared<RegularWave>();
111+
my_hydro_inputs->SetAmplitude(4.0);
112+
my_hydro_inputs->SetPeriod(12.0);
113113

114114
TestHydro hydro_forces(bodies, h5fname);
115115

demos/rm3/demo_rm3_reg_waves.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ int main(int argc, char* argv[]) {
133133
bodies.push_back(plate_body2);
134134

135135
// define wave parameters
136-
auto my_hydro_inputs = std::make_shared<RegularWave>();
137-
my_hydro_inputs->regular_wave_amplitude_ = 1.0;
138-
my_hydro_inputs->regular_wave_omega_ = 2.10;
136+
auto my_hydro_inputs = std::make_shared<RegularWave>();
137+
my_hydro_inputs->SetAmplitude(1.0);
138+
my_hydro_inputs->SetOmega(2.10);
139139

140140
TestHydro hydro_forces(bodies, h5fname);
141141
hydro_forces.AddWaves(my_hydro_inputs);

demos/sphere/demo_sphere_reg_waves.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ int main(int argc, char* argv[]) {
113113
spring_1->SetDampingCoefficient(damping_coef);
114114
system.AddLink(spring_1);
115115

116-
auto my_hydro_inputs = std::make_shared<RegularWave>();
117-
my_hydro_inputs->regular_wave_amplitude_ = task10_wave_amps[reg_wave_num - 1]; // 0.095;
118-
my_hydro_inputs->regular_wave_omega_ = task10_wave_omegas[reg_wave_num - 1]; // 1.427996661;
116+
auto my_hydro_inputs = std::make_shared<RegularWave>();
117+
my_hydro_inputs->SetAmplitude(task10_wave_amps[reg_wave_num - 1]);
118+
my_hydro_inputs->SetOmega(task10_wave_omegas[reg_wave_num - 1]);
119119

120120
std::vector<std::shared_ptr<ChBody>> bodies;
121121
bodies.push_back(sphereBody);
@@ -162,8 +162,8 @@ int main(int argc, char* argv[]) {
162162
outputFile.precision(10);
163163
outputFile.width(12);
164164
outputFile << "Wave #: \t" << reg_wave_num << "\n";
165-
outputFile << "Wave amplitude (m): \t" << my_hydro_inputs->regular_wave_amplitude_ << "\n";
166-
outputFile << "Wave omega (rad/s): \t" << my_hydro_inputs->regular_wave_omega_ << "\n";
165+
outputFile << "Wave amplitude (m): \t" << my_hydro_inputs->GetAmplitude() << "\n";
166+
outputFile << "Wave omega (rad/s): \t" << my_hydro_inputs->GetOmega() << "\n";
167167
outputFile << std::left << std::setw(10) << "Time (s)" << std::right << std::setw(12)
168168
<< "Heave (m)"
169169
//<< std::right << std::setw(18) << "Heave Vel (m/s)"

include/hydroc/waves/regular_wave.h

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

17+
/// Parameters for constructing a RegularWave.
18+
///
19+
/// Specify the wave frequency as either omega (rad/s) or period (s), but not
20+
/// both. If both are non-zero, the constructor throws std::invalid_argument.
1721
struct RegularWaveParams {
1822
double regular_wave_amplitude = 0.0;
1923
double regular_wave_omega = 0.0;
24+
double regular_wave_period = 0.0;
2025
double regular_wave_phase = 0.0;
2126
bool wave_stretching = true;
2227
};
@@ -30,10 +35,21 @@ class RegularWave : public WaveBase {
3035
Eigen::VectorXd GetForceAtTime(double t) const override;
3136
WaveMode GetWaveMode() const override { return mode_; }
3237

33-
// TODO: Eliminate public members; use RegularWaveParams struct (consistent with IrregularWaves).
34-
double regular_wave_amplitude_;
35-
double regular_wave_omega_;
36-
double regular_wave_phase_ = 0.0;
38+
/// @name Mutators
39+
/// @{
40+
void SetAmplitude(double amplitude);
41+
void SetOmega(double omega);
42+
void SetPeriod(double period);
43+
void SetPhase(double phase);
44+
/// @}
45+
46+
/// @name Accessors
47+
/// @{
48+
double GetAmplitude() const { return regular_wave_amplitude_; }
49+
double GetOmega() const { return regular_wave_omega_; }
50+
double GetPeriod() const;
51+
double GetPhase() const { return regular_wave_phase_; }
52+
/// @}
3753

3854
void AddH5Data(std::vector<HydroData::RegularWaveInfo>& reg_h5_data, const HydroData::SimulationParameters& sim_data);
3955

@@ -47,6 +63,11 @@ class RegularWave : public WaveBase {
4763

4864
private:
4965
static constexpr WaveMode mode_ = WaveMode::regular;
66+
67+
double regular_wave_amplitude_ = 0.0;
68+
double regular_wave_omega_ = 0.0;
69+
double regular_wave_phase_ = 0.0;
70+
5071
std::vector<HydroData::RegularWaveInfo> wave_info_;
5172
Eigen::VectorXd excitation_force_mag_;
5273
Eigen::VectorXd excitation_force_phase_;

src/gui/guihelperVSG.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ void GUIImplVSG::UpdateRadiationSourceBody(double t) {
268268
// Wave period from RegularWave.
269269
if (wave_model_->GetWaveMode() == WaveMode::regular) {
270270
auto* reg_wave = dynamic_cast<RegularWave*>(wave_model_.get());
271-
if (reg_wave && reg_wave->regular_wave_omega_ > 0.0) {
272-
rad_params.wave_period = (2.0 * M_PI) / reg_wave->regular_wave_omega_;
271+
if (reg_wave && reg_wave->GetOmega() > 0.0) {
272+
rad_params.wave_period = reg_wave->GetPeriod();
273273
}
274274
}
275275
// Peak period from IrregularWaves (JONSWAP).

src/hydro/config/setup_from_yaml.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ std::shared_ptr<WaveBase> CreateWaveFromSettings(const WaveSettings& wave_settin
5151

5252
if (type == "regular") {
5353
auto regular_wave = std::make_shared<RegularWave>();
54-
55-
regular_wave->regular_wave_amplitude_ = wave_settings.height / 2.0;
56-
regular_wave->regular_wave_omega_ = 2.0 * M_PI / wave_settings.period;
57-
regular_wave->regular_wave_phase_ = wave_settings.phase;
54+
regular_wave->SetAmplitude(wave_settings.height / 2.0);
55+
regular_wave->SetPeriod(wave_settings.period);
56+
regular_wave->SetPhase(wave_settings.phase);
5857

5958
hydroc::debug::LogDebug(std::string("Attached wave model: RegularWave, H=") + std::to_string(wave_settings.height) +
6059
"m, T=" + std::to_string(wave_settings.period) + "s");

src/hydro/waves/regular_wave.cpp

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,79 @@
77
#include "wave_utilities.h"
88

99
#include <cmath>
10+
#include <stdexcept>
11+
12+
namespace {
13+
constexpr double kTwoPi = 2.0 * 3.14159265358979323846;
14+
} // namespace
1015

1116
RegularWave::RegularWave()
1217
: wavenumber_(0.0) {
1318
}
1419

1520
RegularWave::RegularWave(const RegularWaveParams& params)
1621
: wavenumber_(0.0) {
22+
const bool has_omega = params.regular_wave_omega > 0.0;
23+
const bool has_period = params.regular_wave_period > 0.0;
24+
25+
if (has_omega && has_period) {
26+
throw std::invalid_argument(
27+
"RegularWaveParams: specify either regular_wave_omega or "
28+
"regular_wave_period, not both.");
29+
}
30+
1731
regular_wave_amplitude_ = params.regular_wave_amplitude;
18-
regular_wave_omega_ = params.regular_wave_omega;
1932
regular_wave_phase_ = params.regular_wave_phase;
2033
wave_stretching_ = params.wave_stretching;
34+
35+
if (has_period) {
36+
regular_wave_omega_ = kTwoPi / params.regular_wave_period;
37+
} else {
38+
regular_wave_omega_ = params.regular_wave_omega;
39+
}
40+
}
41+
42+
// ---------------------------------------------------------------------------
43+
// Setters
44+
// ---------------------------------------------------------------------------
45+
46+
void RegularWave::SetAmplitude(double amplitude) {
47+
if (amplitude < 0.0) {
48+
throw std::invalid_argument(
49+
"RegularWave::SetAmplitude: amplitude must be non-negative.");
50+
}
51+
regular_wave_amplitude_ = amplitude;
52+
}
53+
54+
void RegularWave::SetOmega(double omega) {
55+
if (omega <= 0.0) {
56+
throw std::invalid_argument(
57+
"RegularWave::SetOmega: omega must be positive.");
58+
}
59+
regular_wave_omega_ = omega;
60+
}
61+
62+
void RegularWave::SetPeriod(double period) {
63+
if (period <= 0.0) {
64+
throw std::invalid_argument(
65+
"RegularWave::SetPeriod: period must be positive.");
66+
}
67+
regular_wave_omega_ = kTwoPi / period;
68+
}
69+
70+
void RegularWave::SetPhase(double phase) {
71+
regular_wave_phase_ = phase;
72+
}
73+
74+
// ---------------------------------------------------------------------------
75+
// Getters
76+
// ---------------------------------------------------------------------------
77+
78+
double RegularWave::GetPeriod() const {
79+
if (regular_wave_omega_ <= 0.0) {
80+
return 0.0;
81+
}
82+
return kTwoPi / regular_wave_omega_;
2183
}
2284

2385
void RegularWave::Initialize() {

tests/regression/oswec/test_oswec_reg_waves.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ int main(int argc, char* argv[]) {
105105
bodies.push_back(base_body);
106106

107107
auto my_hydro_inputs = std::make_shared<RegularWave>();
108-
my_hydro_inputs->regular_wave_amplitude_ = 0.01;
109-
my_hydro_inputs->regular_wave_omega_ = (2 * CH_PI) / (periods[reg_wave_num - 1]);
108+
my_hydro_inputs->SetAmplitude(0.01);
109+
my_hydro_inputs->SetPeriod(periods[reg_wave_num - 1]);
110110

111111
HydroForces hydro_forces(bodies, h5fname);
112112
hydro_forces.AddWaves(my_hydro_inputs);
@@ -137,8 +137,8 @@ int main(int argc, char* argv[]) {
137137
outputFile.precision(10);
138138
outputFile.width(12);
139139
outputFile << "Wave #: \t" << reg_wave_num << "\n";
140-
outputFile << "Wave amplitude (m): \t" << my_hydro_inputs->regular_wave_amplitude_ << "\n";
141-
outputFile << "Wave omega (rad/s): \t" << my_hydro_inputs->regular_wave_omega_ << "\n";
140+
outputFile << "Wave amplitude (m): \t" << my_hydro_inputs->GetAmplitude() << "\n";
141+
outputFile << "Wave omega (rad/s): \t" << my_hydro_inputs->GetOmega() << "\n";
142142
outputFile << std::left << std::setw(10) << "Time (s)" << std::right << std::setw(12) << "Pitch (rads)"
143143
<< std::endl;
144144
for (size_t i = 0; i < time_vector.size(); ++i)

tests/regression/rm3/test_rm3_reg_waves.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ int main(int argc, char* argv[]) {
109109
bodies.push_back(plate_body2);
110110

111111
// define wave parameters
112-
auto my_hydro_inputs = std::make_shared<RegularWave>();
113-
my_hydro_inputs->regular_wave_amplitude_ = 1.0;
114-
my_hydro_inputs->regular_wave_omega_ = 2.10;
112+
auto my_hydro_inputs = std::make_shared<RegularWave>();
113+
my_hydro_inputs->SetAmplitude(1.0);
114+
my_hydro_inputs->SetOmega(2.10);
115115

116116
HydroForces hydro_forces(bodies, h5fname);
117117
hydro_forces.AddWaves(my_hydro_inputs);

0 commit comments

Comments
 (0)