@@ -120,9 +120,9 @@ std::vector<double> ComputeWaveNumbers(const std::vector<double>& omegas,
120120
121121std::vector<double > FreeSurfaceElevation (const Eigen::VectorXd& freqs_hz,
122122 const Eigen::VectorXd& spectral_densities,
123+ const Eigen::VectorXd& wave_phases,
123124 const Eigen::VectorXd& time_index,
124- double water_depth,
125- int seed) {
125+ double water_depth) {
126126 double delta_f = freqs_hz (Eigen::last) / freqs_hz.size ();
127127 std::vector<double > omegas (freqs_hz.size ());
128128
@@ -149,17 +149,10 @@ std::vector<double> FreeSurfaceElevation(const Eigen::VectorXd& freqs_hz,
149149 }
150150 }
151151
152- std::mt19937 rng (seed);
153- std::uniform_real_distribution<double > dist (0.0 , 2 * M_PI);
154- std::vector<double > phases (omegas.size ());
155- for (size_t i = 0 ; i < phases.size (); ++i) {
156- phases[i] = dist (rng);
157- }
158-
159152 std::vector<double > eta (time_index.size (), 0.0 );
160153 for (size_t i = 0 ; i < spectral_densities.size (); ++i) {
161154 for (size_t j = 0 ; j < time_index.size (); ++j) {
162- eta[j] += sqrt_A[i] * std::cos (omegas_t [j][i] + phases [i]);
155+ eta[j] += sqrt_A[i] * std::cos (omegas_t [j][i] + wave_phases [i]);
163156 }
164157 }
165158
@@ -231,8 +224,7 @@ void WriteFreeSurfaceMeshObj(const std::vector<std::array<double, 3>>& points,
231224 out.close ();
232225}
233226
234- IrregularWaves::IrregularWaves (const IrregularWaveParams& params)
235- : params_(params) {}
227+ IrregularWaves::IrregularWaves (const IrregularWaveParams& params) : params_(params) {}
236228
237229void IrregularWaves::InitializeIRFVectors () {
238230 ex_irf_sampled_.resize (params_.num_bodies_ );
@@ -405,16 +397,24 @@ void IrregularWaves::CreateSpectrum() {
405397 if (params_.nfrequencies_ == 0 ) {
406398 // automatically calculate number of frequencies necessary so that timeseries does not repeat itself
407399 double df = 1.0 / params_.simulation_duration_ ;
408- nf = std::ceil ((params_.frequency_max_ - params_.frequency_min_ ) / df);
400+ nf = std::ceil ((params_.frequency_max_ - params_.frequency_min_ ) / df);
409401
410402 } else {
411403 nf = params_.nfrequencies_ ;
412404 }
413405 spectrum_frequencies_ = Eigen::VectorXd::LinSpaced (nf, params_.frequency_min_ , params_.frequency_max_ );
414406
407+ // define random phases
408+ wave_phases_ = Eigen::VectorXd (nf);
409+ std::mt19937 rng (params_.seed_ );
410+ std::uniform_real_distribution<double > dist (0.0 , 2 * M_PI);
411+ for (size_t i = 0 ; i < nf; ++i) {
412+ wave_phases_[i] = dist (rng);
413+ }
414+
415415 // Calculate the Pierson-Moskowitz Spectrum
416- spectral_densities_ =
417- JONSWAPSpectrumHz (spectrum_frequencies_,params_. wave_height_,params_. wave_period_, params_.peak_enhancement_factor_ , params_.is_normalized_ );
416+ spectral_densities_ = JONSWAPSpectrumHz (spectrum_frequencies_, params_. wave_height_ , params_. wave_period_ ,
417+ params_.peak_enhancement_factor_ , params_.is_normalized_ );
418418
419419 // Open a file stream for writing
420420 std::ofstream outputFile (" spectral_densities.txt" );
@@ -494,7 +494,8 @@ void IrregularWaves::CreateFreeSurfaceElevation() {
494494 }
495495 }
496496
497- auto time_array = Eigen::VectorXd::LinSpaced (num_timesteps, 0 , params_.simulation_duration_ + 2 * (t_irf_max - t_irf_min));
497+ auto time_array =
498+ Eigen::VectorXd::LinSpaced (num_timesteps, 0 , params_.simulation_duration_ + 2 * (t_irf_max - t_irf_min));
498499
499500 // Save time array as a std::vector
500501 free_surface_time_sampled_.resize (time_array.size ());
@@ -508,8 +509,8 @@ void IrregularWaves::CreateFreeSurfaceElevation() {
508509 << std::endl;
509510
510511 // Calculate the free surface elevation
511- free_surface_elevation_sampled_ =
512- FreeSurfaceElevation (spectrum_frequencies_, spectral_densities_, time_array, sim_data_.water_depth , params_. seed_ );
512+ free_surface_elevation_sampled_ = FreeSurfaceElevation (spectrum_frequencies_, spectral_densities_, wave_phases_,
513+ time_array, sim_data_.water_depth );
513514
514515 // Apply ramp if ramp_duration is greater than 0
515516 if (params_.ramp_duration_ > 0.0 ) {
0 commit comments