Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions adapters/chrono/include/seastack/adapters/chrono/viz_extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,18 @@ bool IsWaterSurfaceBody(const ::chrono::ChBody& body);
/// entry is a short description for the user; empty = everything is drawable.
std::vector<std::string> FindUndrawableItems(const ::chrono::ChSystem& system);

/// Remove the visual shapes of every link in `system`. Returns the number of
/// links that had shapes.
///
/// For runs in which nothing draws the Chrono system (headless runs). Chrono
/// rebuilds the geometry of every point-to-point shape on a link at each time
/// step, whether or not it is drawn; a SPRING coil is rebuilt point by point.
/// Removing the shapes stops that work and does not change the simulation.
///
/// Call it after every VizExtractor that should describe these links has been
/// constructed: an extractor reads a link's SEGMENT or SPRING shape once, to
/// describe the scene, and afterwards captures only the link's end points and
/// force. Body shapes are kept; Chrono does not rebuild them.
int RemoveLinkVisualShapes(::chrono::ChSystem& system);

} // namespace seastack::chrono
11 changes: 11 additions & 0 deletions adapters/chrono/src/viz_extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ std::vector<std::string> FindUndrawableItems(const ::chrono::ChSystem& system) {
return items;
}

int RemoveLinkVisualShapes(::chrono::ChSystem& system) {
int links_changed = 0;
for (const auto& link : system.GetLinks()) {
if (link && HasVisualShapes(*link)) {
link->GetVisualModel()->Clear();
++links_changed;
}
}
return links_changed;
}

VizExtractor::VizExtractor(::chrono::ChSystem* system, viz::MooringVizProvider mooring)
: system_(system), mooring_(std::move(mooring)) {
if (system_) {
Expand Down
20 changes: 19 additions & 1 deletion apps/seastack/single_run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,15 @@ SingleRunResult RunSingleCase(const SingleRunConfig& config) {
double active_wall_s = 0.0;

if (nogui) {
// Nothing draws the Chrono system in a headless run, and the replay
// scene above already holds the description of every spring and
// cable. Without this, Chrono rebuilds each spring coil every step.
const int links_without_shapes = seastack::chrono::RemoveLinkVisualShapes(*system);
if (links_without_shapes > 0) {
seastack::infra::debug::LogDebug("Headless run: removed the visual shapes of " +
std::to_string(links_without_shapes) + " link(s)");
}

const double end_time_bound = timing.HeadlessEndTime();
if (!timing.end_time_explicit) {
seastack::infra::cli::LogInfo("simulation.end_time is not set in " + config.simulation_file +
Expand Down Expand Up @@ -1864,14 +1873,23 @@ SingleRunResult RunSingleCase(const SingleRunConfig& config) {

if (hydro_forces) {
auto hp = hydro_forces->GetProfileStats();
double hydro_total = hp.hydrostatics_seconds + hp.radiation_seconds + hp.excitation_seconds;
// Everything in the loop that is not a hydrodynamic force component: the Chrono
// step itself, PTO and controller callbacks, and progress output.
double hydro_total = hp.hydrostatics_seconds + hp.radiation_seconds + hp.excitation_seconds +
hp.mooring_seconds + hp.other_seconds;
double chrono_solver = std::max(0.0, prof_loop_seconds - hydro_total);

auto loop_pct = [&](double s){ return seastack::infra::FormatNumber(100.0 * (s / std::max(1e-12, prof_loop_seconds)), 1) + "%"; };
prof_lines.push_back(seastack::infra::cli::CreateAlignedLine(" 🔧", "Chrono Solver", seastack::infra::FormatNumber(chrono_solver, 4) + " s (" + loop_pct(chrono_solver) + ")"));
prof_lines.push_back(seastack::infra::cli::CreateAlignedLine(" ⚓", "Hydrostatics", seastack::infra::FormatNumber(hp.hydrostatics_seconds, 4) + " s (" + loop_pct(hp.hydrostatics_seconds) + ") [" + std::to_string(hp.hydrostatics_calls) + " calls]"));
prof_lines.push_back(seastack::infra::cli::CreateAlignedLine(" 💧", "Radiation Damping", seastack::infra::FormatNumber(hp.radiation_seconds, 4) + " s (" + loop_pct(hp.radiation_seconds) + ") [" + std::to_string(hp.radiation_calls) + " calls]"));
prof_lines.push_back(seastack::infra::cli::CreateAlignedLine(" 🌊", "Wave Forces", seastack::infra::FormatNumber(hp.excitation_seconds, 4) + " s (" + loop_pct(hp.excitation_seconds) + ") [" + std::to_string(hp.excitation_calls) + " calls]"));
if (hp.mooring_calls > 0) {
prof_lines.push_back(seastack::infra::cli::CreateAlignedLine(" 🔗", "Mooring", seastack::infra::FormatNumber(hp.mooring_seconds, 4) + " s (" + loop_pct(hp.mooring_seconds) + ") [" + std::to_string(hp.mooring_calls) + " calls]"));
}
if (hp.other_calls > 0) {
prof_lines.push_back(seastack::infra::cli::CreateAlignedLine(" ➕", "Damping + External", seastack::infra::FormatNumber(hp.other_seconds, 4) + " s (" + loop_pct(hp.other_seconds) + ") [" + std::to_string(hp.other_calls) + " calls]"));
}
}

if (exporter) {
Expand Down
7 changes: 7 additions & 0 deletions demos/5sa/demo_5sa_bimodal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <iomanip>
#include <vector>

#include <seastack/adapters/chrono/viz_extractor.h>

#include "five_sa_model_setup.h"

using namespace chrono;
Expand Down Expand Up @@ -109,6 +111,11 @@ int main(int argc, char* argv[]) {
});
#endif

// Without a viewer nothing draws the ram springs; stop Chrono rebuilding their coils every step.
if (!visualizationOn) {
seastack::chrono::RemoveLinkVisualShapes(system);
}

std::vector<double> time_vector;
std::vector<double> body3_heave;
std::vector<double> body3_sway;
Expand Down
7 changes: 7 additions & 0 deletions demos/5sa/demo_5sa_spreading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <iomanip>
#include <vector>

#include <seastack/adapters/chrono/viz_extractor.h>

#include "five_sa_model_setup.h"

using namespace chrono;
Expand Down Expand Up @@ -97,6 +99,11 @@ int main(int argc, char* argv[]) {
});
#endif

// Without a viewer nothing draws the ram springs; stop Chrono rebuilding their coils every step.
if (!visualizationOn) {
seastack::chrono::RemoveLinkVisualShapes(system);
}

std::vector<double> time_vector;
std::vector<double> body3_heave;

Expand Down
10 changes: 5 additions & 5 deletions docs/usage/EXAMPLE_WORKFLOWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ Limits:
incident wave is not disturbed by the body. Slamming, breaking and green
water are not modelled.
- Cost scales with mesh vertices times wave components. For the 8064-face
IEA sphere mesh: about 0.4 ms per time step in a regular wave
(`test_benchmark_sphere_reg_nlfk`), and about 1.6 ms in an irregular sea
IEA sphere mesh: about 0.1 ms per time step in a regular wave
(`test_benchmark_sphere_reg_nlfk`), and about 1.5 ms in an irregular sea
with 512 components (`test_benchmark_sphere_irreg_nlfk`; 16-core desktop,
12 ms on one thread). The wave evaluation runs in parallel with OpenMP; set
`OMP_NUM_THREADS` to limit it. Use the coarsest mesh that resolves the
waterline.
11 ms on one thread). With more than 8 wave components the wave evaluation
runs in parallel with OpenMP; set `OMP_NUM_THREADS` to limit it. Use the
coarsest mesh that resolves the waterline.

## Regular waves: constant-coefficient radiation

Expand Down
1 change: 1 addition & 0 deletions libs/hydro/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_library(seastack_hydro STATIC
src/waves/wave_utilities.cpp
src/radiation/radiation_rirf_processing.cpp
src/radiation/radiation_rirf_convolution.cpp
src/radiation/velocity_history.cpp
src/radiation/radiation_ss_model.cpp
src/radiation/radiation_ss_fitter.cpp
src/radiation/bem_frequency_match.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@
*
* This component owns the interpolated excitation transfer function data,
* separating excitation force responsibility from the wave kinematic model.
*
* Evaluation. With Re_i and Im_i the columns of the transfer function data
* and theta_i = phi_i - omega_i t, the force in a body's heading frame is
*
* F(t) = sum_i A_i [ Re_i cos(theta_i) - Im_i sin(theta_i) ] (1)
*
* Components with the same frequency are added together once, when the
* component is built. Using
*
* cos(theta_i) = cos(phi_i) cos(omega_i t) + sin(phi_i) sin(omega_i t)
* sin(theta_i) = sin(phi_i) cos(omega_i t) - cos(phi_i) sin(omega_i t)
*
* (1) becomes a sum over the distinct frequencies omega_m:
*
* F(t) = sum_m [ P_m cos(omega_m t) + Q_m sin(omega_m t) ] (2)
* P_m = sum_{i: omega_i = omega_m} A_i [ Re_i cos(phi_i) - Im_i sin(phi_i) ]
* Q_m = sum_{i: omega_i = omega_m} A_i [ Re_i sin(phi_i) + Im_i cos(phi_i) ]
*
* (2) is (1) with its terms in another order. A sea with directional
* spreading has n_omega x n_theta components but only n_omega frequencies,
* so (2) needs n_theta times fewer cosines and sines per time step.
*
* (1) is evaluated term by term during and after a live change of wave
* period or direction, when each component has its own time-varying weight.
*********************************************************************/

#ifndef SEASTACK_HYDRO_FORCE_COMPONENTS_EXCITATION_COMPONENT_H
Expand Down Expand Up @@ -88,6 +112,21 @@ class ExcitationComponent : public IHydroForceComponent {
TransferFunction transfer_;
Eigen::VectorXd weights_; ///< Layer weight of each component [-] (work buffer)

// Sum over distinct frequencies, (2) of the file comment. Per body:
// frequency_cos_[b] holds P_m and frequency_sin_[b] holds Q_m as columns
// (6 x M) [N or N m].
bool has_frequency_sums_ = false;
Eigen::ArrayXd distinct_omegas_; ///< omega_m [rad/s]
std::vector<Eigen::MatrixXd> frequency_cos_;
std::vector<Eigen::MatrixXd> frequency_sin_;
Eigen::ArrayXd omega_t_; ///< omega_m t [rad] (work buffer)
Eigen::VectorXd cos_omega_t_, sin_omega_t_; ///< work buffers

/// Group the components by frequency and compute P_m and Q_m.
void BuildFrequencySums();
/// Add the excitation at `time` from (2).
void ComputeFromFrequencySums(const SystemState& state, double time, BodyForces& inout_forces);

/// Append TF data for components the wave gained since the last call.
void AddNewWaveComponents();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,63 @@
* interpolated between BEM headings (see SelectKernelForHeading).
* Seas with several directions are rejected: use the frequency-domain
* ExcitationComponent instead.
*
* Evaluation. With trapezoid weights w_j on the lag grid tau_j and the
* start-up ramp r, the force in one degree of freedom is
*
* F(t) = sum_j K(tau_j) w_j r(t - tau_j) eta(t - tau_j) (1)
*
* For a wave made of components, eta(t) = sum_i A_i cos(phi_i - omega_i t).
* Putting this into (1) and summing over the lags first gives, wherever the
* ramp is 1,
*
* F(t) = sum_i A_i [ C_i cos(theta_i) - S_i sin(theta_i) ], (2)
* theta_i = phi_i - omega_i t,
* C_i = sum_j K(tau_j) w_j cos(omega_i tau_j),
* S_i = sum_j K(tau_j) w_j sin(omega_i tau_j).
*
* C_i and S_i do not depend on time and are computed once. (2) is the same
* sum as (1) in a different order, not another excitation model, and costs
* a few operations per component instead of one per component and lag.
*
* Start-up. Until t = T_r + (largest lag) some lags look back into the ramp.
* At time t the lags fall into three groups (the lag grid increases):
*
* t - tau_j >= T_r r = 1 fully ramped: the first lags
* 0 < t - tau_j < T_r 0 < r < 1 ramping
* t - tau_j <= 0 r = 0 no contribution: the last lags
*
* The fully ramped lags are a sum like (2) over the first lags only. For the
* ramping lags, r(t - tau) is a sum of a few products of a function of t and
* a function of tau,
*
* linear ramp: r = (t / T_r) 1 - (1 / T_r) tau
* cosine ramp: r = 0.5 - 0.5 cos(pi t / T_r) cos(pi tau / T_r)
* - 0.5 sin(pi t / T_r) sin(pi tau / T_r)
*
* i.e. r(t - tau) = sum_m h_m(t) g_m(tau), so their contribution is
*
* sum_m h_m(t) sum_i A_i [ C_i,m cos(theta_i) - S_i,m sin(theta_i) ],
* C_i,m = sum_{j ramping} K(tau_j) w_j g_m(tau_j) cos(omega_i tau_j) (S_i,m with sin).
*
* Both are sums over a range of lags. They are taken from partial sums
*
* sum_{j < p} K(tau_j) w_j g_m(tau_j) cos(omega_i tau_j) (and sin)
*
* stored once at every B-th lag p (B = 16, or larger for large data). A sum
* over a range is the difference of two stored partial sums; the fewer than
* 2 B lags between the ends of a range and the nearest stored p are summed
* directly, as in (1). The result is (1) in another order. Each step then
* costs a few operations per component, plus those few lags, in place of one
* operation per component and lag.
*
* A live change of wave height multiplies eta(t - tau_j) by the envelope
* e(t - tau_j). Where e has one value over all the lags, the result above is
* multiplied by it. While the change is inside the kernel window, each lag
* has its own factor and (1) is evaluated directly.
*
* (1) is also evaluated directly for waves without components (eta table)
* and for live period or direction changes.
*********************************************************************/

#ifndef SEASTACK_HYDRO_FORCE_COMPONENTS_EXCITATION_IRF_COMPONENT_H
Expand Down Expand Up @@ -104,6 +161,51 @@ class ExcitationIrfComponent : public IHydroForceComponent {
// Reusable buffer for eta_at_tau to avoid per-timestep allocation
Eigen::VectorXd eta_at_tau_buffer_;

// Closed form (2) of the file comment, for component waves. Per body:
// closed_form_cos_[b](dof, i) = C_i and closed_form_sin_[b](dof, i) = S_i
// of degree of freedom `dof` [N/m], 6 x N_comp.
bool has_closed_form_ = false;
std::vector<Eigen::MatrixXd> closed_form_cos_;
std::vector<Eigen::MatrixXd> closed_form_sin_;
// Per step: A_i cos(theta_i), A_i sin(theta_i), the force of each body in
// its heading frame, and the ramped elevation r(t - tau_j) eta(t - tau_j)
// at the lags that are summed directly.
Eigen::ArrayXd phase_buffer_; // theta_i
Eigen::VectorXd amplitude_cos_buffer_, amplitude_sin_buffer_;
std::vector<Eigen::Matrix<double, 6, 1>> heading_force_buffer_;
Eigen::VectorXd ramped_elevation_buffer_;

// Start-up (see the file comment): partial sums over the first lags,
// stored at every ramp_block_-th lag. Checkpoint c stands at lag
// p_c = min(c ramp_block_, number of lags), c = 0 .. ramp_last_checkpoint_.
// For body b, ramp function m and checkpoint c, the N_comp x 6 matrices
// RampCosTable(b, m, c)(i, dof) = sum_{j < p_c} K(dof, tau_j) w_j g_m(tau_j) cos(omega_i tau_j)
// RampSinTable(b, m, c)(i, dof) = the same with sin(omega_i tau_j).
bool has_ramp_tables_ = false;
Eigen::Index ramp_block_ = 0;
Eigen::Index ramp_last_checkpoint_ = 0;
int ramp_terms_ = 0; ///< Number of functions g_m: 2 (linear ramp) or 3 (cosine ramp)
std::vector<Eigen::MatrixXd> ramp_cos_tables_;
std::vector<Eigen::MatrixXd> ramp_sin_tables_;

/// Compute C_i and S_i. Requires the fast-path tables and an increasing lag grid.
void BuildClosedForm();
/// Compute the partial sums for the start-up. Requires the closed form.
void BuildRampTables();
/// Position of the table of (body, ramp function, checkpoint) in ramp_cos_tables_ and ramp_sin_tables_.
std::size_t RampTableIndex(int body, int term, Eigen::Index checkpoint) const;
/// Add the excitation at `time` from the closed form, multiplied by the
/// wave-height factor e [-] that holds at every lag. Returns false, adding
/// nothing, when it cannot be used at this time (a ramp shape without
/// partial sums, during the start-up).
bool ComputeClosedForm(const SystemState& state, double time, double wave_height_factor,
BodyForces& inout_forces);
/// Start-up: fill heading_force_buffer_ for lags [0, first_zero), of which
/// those from first_ramping on are still ramping.
void ComputeRampWindow(double time, Eigen::Index first_ramping, Eigen::Index first_zero);
/// Add lags [begin, end) to heading_force_buffer_ by the direct sum (1).
void AddLagsDirectly(double time, Eigen::Index begin, Eigen::Index end);

void TryBuildFastPath();
void ComputeFastPath(double time, Eigen::VectorXd& eta_at_tau) const;
void ComputeSlowPath(double time, Eigen::VectorXd& eta_at_tau) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class HydroData;
* Computes radiation damping forces via convolution of RIRF kernels
* with body velocity history. Supports optional kernel processing
* (smoothing and/or tapering) via RadiationKernelProcessing.
*
* The kernel is taken from the HydroData in dimensional units, processed if
* requested, and handed to RadiationRirfConvolution once, on the first call
* to Compute(). Each call then records the body velocities in each body's
* heading frame (heading_frame.h), evaluates the convolution and subtracts
* the result, rotated back to the world frame, from the body forces.
*/
class RadiationComponent : public IHydroForceComponent {
public:
Expand Down Expand Up @@ -63,15 +69,15 @@ class RadiationComponent : public IHydroForceComponent {
std::unique_ptr<RadiationRirfConvolution> convolution_;
Eigen::VectorXd rirf_time_vector_;
Eigen::VectorXd rirf_width_vector_;

bool rirf_processed_ready_ = false;
std::vector<Eigen::Tensor<double, 3>> rirf_processed_;


bool kernel_ready_ = false;

// Reusable buffer for body velocities to avoid per-timestep allocation
std::vector<std::vector<double>> body_velocities_buffer_;

void EnsureProcessedRIRF();
double GetRIRFval(int row, int col, int st);

/// Build the kernel (processed if requested) and give it to the
/// convolution. Done once, on the first Compute().
void EnsureKernel();
};

} // namespace seastack::hydro
Expand Down
5 changes: 5 additions & 0 deletions libs/hydro/include/seastack/hydro/geometry/triangle_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ struct TriangleMesh {

Eigen::AlignedBox3d bounding_box() const;

/// Vertex positions in the world frame, one per column: R v + t for each
/// vertex v, with R and t the linear part and translation of
/// body_to_world. `world` is resized if needed.
void TransformVertices(const Eigen::Affine3d& body_to_world, Eigen::Matrix3Xd& world) const;

/**
* @brief Mesh topology diagnostics.
*/
Expand Down
7 changes: 6 additions & 1 deletion libs/hydro/include/seastack/hydro/hydro_forces.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ struct HydroForcesProfileStats {
int radiation_calls = 0;
int excitation_calls = 0;
int mooring_calls = 0;

/// Linear / quadratic damping and external force components.
double other_seconds = 0.0;
int other_calls = 0;

/// Reset all counters to zero
void Reset() {
hydrostatics_seconds = radiation_seconds = excitation_seconds = mooring_seconds = 0.0;
hydrostatics_calls = radiation_calls = excitation_calls = mooring_calls = 0;
other_seconds = 0.0;
other_calls = 0;
}
};

Expand Down
Loading
Loading