Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added InterfaceBuilder to improve ergonomics of creating our CFD interface #352

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
3 changes: 2 additions & 1 deletion src/interfaces/cfd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(openturbine_library
target_sources(openturbine_library
PRIVATE
interface.cpp
)
Expand All @@ -7,6 +7,7 @@ install(FILES
floating_platform.hpp
floating_platform_input.hpp
interface.hpp
interface_builder.hpp
interface_input.hpp
mooring_line.hpp
mooring_line_input.hpp
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/cfd/floating_platform_input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct FloatingPlatformInput {
std::array<double, 6> acceleration{0., 0., 0., 0., 0., 0.};

/// Platform point mass matrix
std::array<std::array<double, 6>, 6> mass_matrix;
std::array<std::array<double, 6>, 6> mass_matrix{};

/// Mooring line array
std::vector<MooringLineInput> mooring_lines;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use brace initializer here as well?

std::vector<MooringLineInput> mooring_lines{};

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that. Clang-Tidy had some reason for not liking it there.

Expand Down
187 changes: 187 additions & 0 deletions src/interfaces/cfd/interface_builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#pragma once

#include "floating_platform_input.hpp"
#include "interface.hpp"
#include "interface_input.hpp"
#include "mooring_line_input.hpp"

namespace openturbine::cfd {
struct InterfaceBuilder {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we call it BuildInterface to be consistent with how we named our other factory methods (such as CreateBeams)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to be consistent with the Builder Pattern, where things are named this way by convention. I'm open to changing it if there are strong opinions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong opinion here, wanted to make sure we are being consistent within the codebase.

struct FloatingPlatformBuilder {
FloatingPlatformBuilder& SetPosition(const std::array<double, 7>& p) {
i_builder->interface_input.turbine.floating_platform.position = p;
return *this;
}

FloatingPlatformBuilder& SetVelocity(const std::array<double, 6>& v) {
i_builder->interface_input.turbine.floating_platform.velocity = v;
return *this;
}

FloatingPlatformBuilder& SetAcceleration(const std::array<double, 6>& a) {
i_builder->interface_input.turbine.floating_platform.acceleration = a;
return *this;
}

FloatingPlatformBuilder& SetMassMatrix(
const std::array<std::array<double, 6>, 6>& mass_matrix
) {
i_builder->interface_input.turbine.floating_platform.mass_matrix = mass_matrix;
return *this;
}

[[nodiscard]] InterfaceBuilder& EndFloatingPlatform() const {
i_builder->interface_input.turbine.floating_platform.enable = true;
return *i_builder;
}

InterfaceBuilder* i_builder;
};

struct MooringLineBuilder {
MooringLineBuilder& SetStiffness(double stiffness) {
i_builder->interface_input.turbine.floating_platform.mooring_lines.back().stiffness =
stiffness;
return *this;
}

MooringLineBuilder& SetUndeformedLength(double length) {
i_builder->interface_input.turbine.floating_platform.mooring_lines.back()
.undeformed_length = length;
return *this;
}

MooringLineBuilder& SetFairleadPosition(const std::array<double, 3>& p) {
i_builder->interface_input.turbine.floating_platform.mooring_lines.back()
.fairlead_position = p;
return *this;
}

MooringLineBuilder& SetAnchorPosition(const std::array<double, 3>& p) {
i_builder->interface_input.turbine.floating_platform.mooring_lines.back()
.anchor_position = p;
return *this;
}

MooringLineBuilder& SetAnchorVelocity(const std::array<double, 3>& v) {
i_builder->interface_input.turbine.floating_platform.mooring_lines.back()
.anchor_velocity = v;
return *this;
}

MooringLineBuilder& SetAnchorAcceleration(const std::array<double, 3>& a) {
i_builder->interface_input.turbine.floating_platform.mooring_lines.back()
.anchor_acceleration = a;
return *this;
}

[[nodiscard]] InterfaceBuilder& EndMooringLine() const { return *i_builder; }

InterfaceBuilder* i_builder;
};

InterfaceBuilder() : interface_input{}, fp_builder{this}, ml_builder{this} {}

InterfaceBuilder& SetGravity(const std::array<double, 3>& gravity) {
interface_input.gravity = gravity;
return *this;
}

InterfaceBuilder& SetMaximumNonlinearIterations(size_t max_iter) {
interface_input.max_iter = max_iter;
return *this;
}

InterfaceBuilder& SetTimeStep(double time_step) {
interface_input.time_step = time_step;
return *this;
}

FloatingPlatformBuilder& StartFloatingPlatform() { return fp_builder; }

MooringLineBuilder& AddMooringLine() {
interface_input.turbine.floating_platform.mooring_lines.push_back({});
return ml_builder;
}

InterfaceBuilder& SetDampingFactor(double rho_inf) {
interface_input.rho_inf = rho_inf;
return *this;
}

InterfaceBuilder& EnableFloatingPlatform(bool enable) {
interface_input.turbine.floating_platform.enable = enable;
return *this;
}

InterfaceBuilder& SetFloatingPlatformPosition(const std::array<double, 7>& p) {
interface_input.turbine.floating_platform.position = p;
return *this;
}

InterfaceBuilder& SetFloatingPlatformVelocity(const std::array<double, 6>& v) {
interface_input.turbine.floating_platform.velocity = v;
return *this;
}

InterfaceBuilder& SetFloatingPlatformAcceleration(const std::array<double, 6>& a) {
interface_input.turbine.floating_platform.acceleration = a;
return *this;
}

InterfaceBuilder& SetFloatingPlatformMassMatrix(
const std::array<std::array<double, 6>, 6>& mass_matrix
) {
interface_input.turbine.floating_platform.mass_matrix = mass_matrix;
return *this;
}

InterfaceBuilder& SetNumberOfMooringLines(size_t number) {
interface_input.turbine.floating_platform.mooring_lines.resize(number);
return *this;
}

InterfaceBuilder& SetMooringLineStiffness(size_t line, double stiffness) {
interface_input.turbine.floating_platform.mooring_lines[line].stiffness = stiffness;
return *this;
}

InterfaceBuilder& SetMooringLineUndeformedLength(size_t line, double length) {
interface_input.turbine.floating_platform.mooring_lines[line].undeformed_length = length;
return *this;
}

InterfaceBuilder& SetMooringLineFairleadPosition(size_t line, const std::array<double, 3>& p) {
interface_input.turbine.floating_platform.mooring_lines[line].fairlead_position = p;
return *this;
}

InterfaceBuilder& SetMooringLineAnchorPosition(size_t line, const std::array<double, 3>& p) {
interface_input.turbine.floating_platform.mooring_lines[line].anchor_position = p;
return *this;
}

InterfaceBuilder& SetMooringLineAnchorVelocity(size_t line, const std::array<double, 3>& v) {
interface_input.turbine.floating_platform.mooring_lines[line].anchor_velocity = v;
return *this;
}

InterfaceBuilder& SetMooringLineAnchorAcceleration(size_t line, const std::array<double, 3>& a) {
interface_input.turbine.floating_platform.mooring_lines[line].anchor_acceleration = a;
return *this;
}

[[nodiscard]] Interface Build() const { return Interface(interface_input); }

friend FloatingPlatformBuilder;
friend MooringLineBuilder;

protected:
InterfaceInput interface_input;

private:
FloatingPlatformBuilder fp_builder;
MooringLineBuilder ml_builder;
};

} // namespace openturbine::cfd
78 changes: 23 additions & 55 deletions tests/documentation_tests/floating_platform/floating_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <Kokkos_Core.hpp>
#include <interfaces/cfd/interface.hpp>
#include <interfaces/cfd/interface_builder.hpp>

int main() {
Kokkos::initialize();
Expand Down Expand Up @@ -37,61 +38,28 @@ int main() {
constexpr auto mooring_line_initial_length{55.432}; // m

// Create cfd interface
auto interface = openturbine::cfd::Interface(openturbine::cfd::InterfaceInput{
gravity,
time_step, // time step
rho_inf, // rho infinity (numerical damping)
max_iter, // max convergence iterations
openturbine::cfd::TurbineInput{
openturbine::cfd::FloatingPlatformInput{
true, // enable
{
platform_cm_position[0],
platform_cm_position[1],
platform_cm_position[2],
1.,
0.,
0.,
0.,
}, // position
{0., 0., 0., 0., 0., 0.}, // velocity
{0., 0., 0., 0., 0., 0.}, // acceleration
platform_mass_matrix,
{
{
mooring_line_stiffness,
mooring_line_initial_length,
{-40.87, 0.0, -14.}, // Fairlead node coordinates
{0., 0., 0.}, // Fairlead node velocity
{0., 0., 0.}, // Fairlead node acceleration
{-105.47, 0.0, -58.4}, // Anchor node coordinates
{0., 0., 0.}, // Anchor node velocity
{0., 0., 0.}, // Anchor node acceleration
},
{
mooring_line_stiffness,
mooring_line_initial_length,
{20.43, -35.39, -14.}, // Fairlead node coordinates
{0., 0., 0.}, // Fairlead node velocity
{0., 0., 0.}, // Fairlead node acceleration
{52.73, -91.34, -58.4}, // Anchor node coordinates
{0., 0., 0.}, // Anchor node velocity
{0., 0., 0.}, // Anchor node acceleration
},
{
mooring_line_stiffness,
mooring_line_initial_length,
{20.43, 35.39, -14.}, // Fairlead node coordinates
{0., 0., 0.}, // Fairlead node velocity
{0., 0., 0.}, // Fairlead node acceleration
{52.73, 91.34, -58.4}, // Anchor node coordinates
{0., 0., 0.}, // Anchor node velocity
{0., 0., 0.}, // Anchor node acceleration
},
},
},
},
});
auto interface = InterfaceBuilder{}
.SetGravity(gravity)
.SetTimeStep(time_step)
.SetDampingFactor(rho_inf)
.SetMaximumNonlinearIterations(max_iter)
.EnableFloatingPlatform(true)
.SetFloatingPlatformPosition({0., 0., -7.53, 1., 0., 0., 0.})
.SetFloatingPlatformMassMatrix(platform_mass_matrix)
.SetNumberOfMooringLines(3)
.SetMooringLineStiffness(0, mooring_line_stiffness)
.SetMooringLineUndeformedLength(0, mooring_line_initial_length)
.SetMooringLineFairleadPosition(0, {-40.87, 0.0, -14.})
.SetMooringLineAnchorPosition(0, {-105.47, 0.0, -58.4})
.SetMooringLineStiffness(1, mooring_line_stiffness)
.SetMooringLineUndeformedLength(1, mooring_line_initial_length)
.SetMooringLineFairleadPosition(1, {20.43, -35.39, -14.})
.SetMooringLineAnchorPosition(1, {52.73, -91.34, -58.4})
.SetMooringLineStiffness(2, mooring_line_stiffness)
.SetMooringLineUndeformedLength(2, mooring_line_initial_length)
.SetMooringLineFairleadPosition(2, {20.43, 35.39, -14.})
.SetMooringLineAnchorPosition(2, {52.73, 91.34, -58.4})
.Build();

// Calculate buoyancy force as percentage of gravitational force plus spring forces times
const auto initial_spring_force = 1907514.4912628897;
Expand Down
Loading