Skip to content

Commit 27f8d9a

Browse files
committed
Precalculate eta with +/- IRF time + oob errors
1 parent c86fd5d commit 27f8d9a

1 file changed

Lines changed: 47 additions & 30 deletions

File tree

src/wave_types.cpp

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -240,30 +240,13 @@ IrregularWaves::IrregularWaves(const IrregularWaveParams& params)
240240
simulation_dt_(params.simulation_dt_),
241241
simulation_duration_(params.simulation_duration_),
242242
ramp_duration_(params.ramp_duration_),
243-
seed_(params.seed_) {
244-
std::cout << "Creating IrregularWaves object..." << std::endl;
243+
seed_(params.seed_) {}
244+
245+
void IrregularWaves::InitializeIRFVectors() {
245246
ex_irf_sampled_.resize(num_bodies_);
246247
ex_irf_time_sampled_.resize(num_bodies_);
247248
ex_irf_width_sampled_.resize(num_bodies_);
248-
std::cout << "eta_file_path (1)" << eta_file_path_ << std::endl;
249-
if (!eta_file_path_.empty()) {
250-
std::cout << "Reading eta.txt..." << std::endl;
251-
ReadEtaFromFile();
252-
spectrumCreated_ = false;
253-
} else if (wave_height_ != 0.0 && wave_period_ != 0.0) {
254-
CreateSpectrum();
255-
CreateFreeSurfaceElevation();
256-
spectrumCreated_ = true;
257-
}
258-
std::cout << "eta.txt read." << std::endl;
259-
260-
std::vector<Eigen::MatrixXd> ex_irf_old(num_bodies_);
261-
std::vector<Eigen::VectorXd> ex_irf_time_old(num_bodies_);
262249

263-
std::cout << "IrregularWaves instantiated..." << std::endl;
264-
}
265-
266-
void IrregularWaves::InitializeIRFVectors() {
267250
std::vector<Eigen::MatrixXd> ex_irf_old(num_bodies_);
268251
std::vector<Eigen::VectorXd> ex_irf_time_old(num_bodies_);
269252

@@ -277,6 +260,15 @@ void IrregularWaves::InitializeIRFVectors() {
277260
if (simulation_dt_ > 0.0) {
278261
ResampleIRF(simulation_dt_);
279262
}
263+
264+
if (!eta_file_path_.empty()) {
265+
ReadEtaFromFile();
266+
spectrumCreated_ = false;
267+
} else if (wave_height_ != 0.0 && wave_period_ != 0.0) {
268+
CreateSpectrum();
269+
CreateFreeSurfaceElevation();
270+
spectrumCreated_ = true;
271+
}
280272
}
281273

282274
std::vector<double> IrregularWaves::GetSpectrum() {
@@ -296,10 +288,10 @@ std::vector<double> IrregularWaves::GetEtaTimeData() {
296288
}
297289

298290
void IrregularWaves::ReadEtaFromFile() {
299-
std::cout << "ReadEtaFromFile()" << std::endl;
291+
std::cout << "Reading eta file " << eta_file_path_ << "." << std::endl;
300292
std::ifstream file(eta_file_path_);
301293
if (!file) {
302-
throw std::runtime_error("Unable to open file at: " + eta_file_path_);
294+
throw std::runtime_error("Unable to open file at: " + eta_file_path_ + ".");
303295
}
304296

305297
std::string line;
@@ -309,11 +301,12 @@ void IrregularWaves::ReadEtaFromFile() {
309301
std::stringstream ss(line);
310302
char delimiter;
311303
if (!(ss >> time >> delimiter >> eta) || delimiter != ':') {
312-
throw std::runtime_error("Could not parse line: " + line);
304+
throw std::runtime_error("Could not parse line: " + line + ".");
313305
}
314306
time_data_.push_back(time);
315307
free_surface_elevation_sampled_.push_back(eta);
316308
}
309+
std::cout << "Finished reading eta file." << std::endl;
317310
}
318311

319312
Eigen::MatrixXd IrregularWaves::GetExcitationIRF(int b) const {
@@ -478,11 +471,35 @@ void IrregularWaves::CreateFreeSurfaceElevation() {
478471
// UpdateNumTimesteps();
479472
int num_timesteps = static_cast<int>(simulation_duration_ / simulation_dt_) + 1;
480473

481-
auto time_array = Eigen::VectorXd::LinSpaced(num_timesteps, 0, simulation_duration_);
474+
double t_irf_min = 0.0;
475+
double t_irf_max = 0.0;
476+
for (auto ii = 0; ii < ex_irf_time_sampled_.size(); ii++) {
477+
if (ex_irf_time_sampled_[ii][0] < t_irf_min) {
478+
t_irf_min = ex_irf_time_sampled_[ii][0];
479+
}
480+
if (ex_irf_time_sampled_[ii][0] > t_irf_max) {
481+
t_irf_max = ex_irf_time_sampled_[ii][0];
482+
}
483+
if (ex_irf_time_sampled_[ii][ex_irf_time_sampled_[ii].size() - 1] > t_irf_max) {
484+
t_irf_max = ex_irf_time_sampled_[ii][ex_irf_time_sampled_[ii].size() - 1];
485+
}
486+
if (ex_irf_time_sampled_[ii][ex_irf_time_sampled_[ii].size() - 1] < t_irf_min) {
487+
t_irf_min = ex_irf_time_sampled_[ii][ex_irf_time_sampled_[ii].size() - 1];
488+
}
489+
}
490+
491+
auto time_array = Eigen::VectorXd::LinSpaced(num_timesteps, 0, simulation_duration_ + 2 * (t_irf_max - t_irf_min));
482492

483493
// Save time array as a std::vector
484494
free_surface_time_sampled_.resize(time_array.size());
485495
Eigen::VectorXd::Map(&free_surface_time_sampled_[0], time_array.size()) = time_array;
496+
for (int ii = 0; ii < free_surface_time_sampled_.size(); ii++) {
497+
free_surface_time_sampled_[ii] += -t_irf_max;
498+
}
499+
500+
std::cout << "Precalculating free surface elevation from " + std::to_string(free_surface_time_sampled_.front()) +
501+
" to " + std::to_string(free_surface_time_sampled_.back()) + "."
502+
<< std::endl;
486503

487504
// Calculate the free surface elevation
488505
free_surface_elevation_sampled_ =
@@ -514,9 +531,7 @@ void IrregularWaves::CreateFreeSurfaceElevation() {
514531
std::cerr << "Unable to open file for writing." << std::endl;
515532
}
516533

517-
// std::vector<std::array<double, 3>> free_surface_3d_pts = CreateFreeSurface3DPts(eta, time_index);
518-
// std::vector<std::array<size_t, 3>> free_surface_triangles = CreateFreeSurfaceTriangles(time_index.size());
519-
// WriteFreeSurfaceMeshObj(free_surface_3d_pts, free_surface_triangles, "fse_mesh.obj");
534+
std::cout << "Finished precalculating free surface elevation." << std::endl;
520535
}
521536

522537
double IrregularWaves::ExcitationConvolution(int body, int dof, double time) {
@@ -577,10 +592,12 @@ double IrregularWaves::ExcitationConvolution(int body, int dof, double time) {
577592
f_ex += irf_val_mat(dof, j) * eta_val * irf_width_array[j];
578593

579594
} else {
595+
// throw error if trying to compute convolution after the maximum precomputed free elevation time
580596
throw std::runtime_error(
581-
"Excitation convolution: trying to find free surface elevation at a time out of bounds from "
582-
"the precomputed free surface elevation (" +
583-
std::to_string(t_tau) + "not in [" + std::to_string(tmin) + ", " + std::to_string(tmax) + "]).");
597+
"Excitation convolution: trying to find free surface elevation at a time out of bounds from the "
598+
"precomputed free surface elevation (" +
599+
std::to_string(t_tau) + "not in [" + std::to_string(tmin) + ", " + std::to_string(tmax) +
600+
"]). Excitation force ignored at this time step.");
584601
}
585602
}
586603

0 commit comments

Comments
 (0)