Skip to content

Commit

Permalink
Merge pull request #217 from Exawind/refactor-model-node-classes
Browse files Browse the repository at this point in the history
Refactor Model class to hold smart pointers as its attributes
  • Loading branch information
faisal-bhuiyan authored Aug 1, 2024
2 parents e13f4dd + 4c23972 commit ae02689
Show file tree
Hide file tree
Showing 12 changed files with 547 additions and 195 deletions.
143 changes: 107 additions & 36 deletions src/restruct_poc/model/model.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#pragma once

#include <functional>

#include "node.hpp"

#include "src/restruct_poc/beams/beam_element.hpp"
#include "src/restruct_poc/solver/constraint.hpp"
#include "src/restruct_poc/types.hpp"

Expand All @@ -15,64 +18,132 @@ static Node InvalidNode(-1, {0., 0., 0., 1., 0., 0., 0.});
/// relationships between components in a turbine. Nodes represent points in space with
/// position, displacement, velocity, and acceleration. Constraints represent relationships
/// between nodes that restrict their relative motion in some way.
struct Model {
std::vector<Node> nodes;
std::vector<Constraint> constraints;
class Model {
public:
Model() = default;

Model(
const std::vector<Node>& nodes, const std::vector<BeamElement>& beam_elements,
const std::vector<Constraint>& constraints
) {
for (const auto& n : nodes) {
this->nodes_.emplace_back(std::make_shared<Node>(n));
}
for (const auto& e : beam_elements) {
this->beam_elements_.emplace_back(std::make_shared<BeamElement>(e));
}
for (const auto& c : constraints) {
this->constraints_.emplace_back(std::make_shared<Constraint>(c));
}
}

/// Adds a node to the model and returns the node
Node AddNode(
Model(
std::vector<std::shared_ptr<Node>> nodes,
std::vector<std::shared_ptr<BeamElement>> beam_elements,
std::vector<std::shared_ptr<Constraint>> constraints
)
: nodes_(std::move(nodes)),
beam_elements_(std::move(beam_elements)),
constraints_(std::move(constraints)) {}

/// Add a node to the model and return a shared pointer to the node
std::shared_ptr<Node> AddNode(
const Array_7& position, const Array_7& displacement = Array_7{0., 0., 0., 1., 0., 0., 0.},
const Array_6& velocity = Array_6{0., 0., 0., 0., 0., 0.},
const Array_6& acceleration = Array_6{0., 0., 0., 0., 0., 0.}
) {
this->nodes.push_back(Node(nodes.size(), position, displacement, velocity, acceleration));
return this->nodes.back();
return this->nodes_.emplace_back(
std::make_shared<Node>(nodes_.size(), position, displacement, velocity, acceleration)
);
}

/// Return a node by ID - const/read-only version
const Node& GetNode(int id) const { return *this->nodes_[id]; }

/// Return a node by ID - non-const version
Node& GetNode(int id) { return *this->nodes_[id]; }

/// Returns a reference to the nodes in the model (as vector of shared pointers)
const std::vector<std::shared_ptr<Node>>& GetNodes() const { return this->nodes_; }

/// Returns the number of nodes in the model
size_t NumNodes() const { return this->nodes_.size(); }

/// Add a beam element to the model and return a shared pointer to the element
std::shared_ptr<BeamElement> AddBeamElement(
const std::vector<BeamNode>& nodes, const std::vector<BeamSection>& sections,
const BeamQuadrature& quadrature
) {
return this->beam_elements_.emplace_back(std::make_shared<BeamElement>(
std::move(nodes), std::move(sections), std::move(quadrature)
));
}

/// Adds a rigid constraint to the model and returns the constraint.
Constraint AddRigidConstraint(const Node& node1, const Node& node2) {
this->constraints.push_back(
Constraint(ConstraintType::kRigid, this->constraints.size(), node1, node2)
/// Return a beam element by ID - const/read-only version
const BeamElement& GetBeamElement(int id) const { return *this->beam_elements_[id]; }

/// Return a beam element by ID - non-const version
BeamElement& GetBeamElement(int id) { return *this->beam_elements_[id]; }

/// Returns a reference to the beam elements in the model
const std::vector<std::shared_ptr<BeamElement>>& GetBeamElements() const {
return this->beam_elements_;
}

/// Returns the number of beam elements in the model
size_t NumBeamElements() const { return this->beam_elements_.size(); }

/// Adds a rigid constraint to the model and returns the constraint
std::shared_ptr<Constraint> AddRigidConstraint(const Node& node1, const Node& node2) {
return this->constraints_.emplace_back(
std::make_shared<Constraint>(ConstraintType::kRigid, constraints_.size(), node1, node2)
);
return this->constraints.back();
}

/// Adds a prescribed boundary condition constraint to the model and returns the constraint.
Constraint AddPrescribedBC(const Node& node, const Array_3& ref_position = {0., 0., 0.}) {
this->constraints.push_back(Constraint(
ConstraintType::kPrescribedBC, this->constraints.size(), InvalidNode, node, ref_position
/// Adds a prescribed boundary condition constraint to the model and returns the constraint
std::shared_ptr<Constraint> AddPrescribedBC(
const Node& node, const Array_3& ref_position = {0., 0., 0.}
) {
return this->constraints_.emplace_back(std::make_shared<Constraint>(
ConstraintType::kPrescribedBC, constraints_.size(), InvalidNode, node, ref_position
));
return this->constraints.back();
}

/// Adds a fixed boundary condition constraint to the model and returns the constraint.
Constraint AddFixedBC(const Node& node) {
this->constraints.push_back(
Constraint(ConstraintType::kFixedBC, this->constraints.size(), InvalidNode, node)
);
return this->constraints.back();
/// Adds a fixed boundary condition constraint to the model and returns the constraint
std::shared_ptr<Constraint> AddFixedBC(const Node& node) {
return this->constraints_.emplace_back(std::make_shared<Constraint>(
ConstraintType::kFixedBC, constraints_.size(), InvalidNode, node
));
}

/// Adds a cylindrical constraint to the model and returns the constraint.
Constraint AddCylindricalConstraint(const Node& node1, const Node& node2) {
this->constraints.push_back(
Constraint(ConstraintType::kCylindrical, this->constraints.size(), node1, node2)
);
return this->constraints.back();
/// Adds a cylindrical constraint to the model and returns the constraint
std::shared_ptr<Constraint> AddCylindricalConstraint(const Node& node1, const Node& node2) {
return this->constraints_.emplace_back(std::make_shared<Constraint>(
ConstraintType::kCylindrical, constraints_.size(), node1, node2
));
}

/// Adds a rotation control constraint to the model and returns the constraint.
Constraint AddRotationControl(
/// Adds a rotation control constraint to the model and returns the constraint
std::shared_ptr<Constraint> AddRotationControl(
const Node& node1, const Node& node2, const Array_3& axis, float* control
) {
this->constraints.push_back(Constraint(
ConstraintType::kRotationControl, this->constraints.size(), node1, node2, axis, control
return this->constraints_.emplace_back(std::make_shared<Constraint>(
ConstraintType::kRotationControl, constraints_.size(), node1, node2, axis, control
));
return this->constraints.back();
}

/// Returns the number of nodes in the model
int NumNodes() { return this->nodes.size(); }
/// Returns the constraints in the model (as vector of shared pointers)
const std::vector<std::shared_ptr<Constraint>>& GetConstraints() const {
return this->constraints_;
}

/// Returns the number of constraints in the model
size_t NumConstraints() const { return this->constraints_.size(); }

private:
std::vector<std::shared_ptr<Node>> nodes_; //< Nodes in the model
std::vector<std::shared_ptr<BeamElement>> beam_elements_; //< Beam elements in the model
std::vector<std::shared_ptr<Constraint>> constraints_; //< Constraints in the model
};

} // namespace openturbine
44 changes: 39 additions & 5 deletions src/restruct_poc/model/node.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,56 @@
#pragma once

#include "src/restruct_poc/math/quaternion_operations.hpp"
#include "src/restruct_poc/types.hpp"

namespace openturbine {

struct Node {
int ID; // Node identifier
Array_7 x; // Node positions and orientations
Array_7 u; // Node displacement
Array_6 v; // Node velocity
Array_6 vd; // Node acceleration
int ID; //< Node identifier
Array_7 x; //< Node positions and orientations
Array_7 u; //< Node displacement
Array_6 v; //< Node velocity
Array_6 vd; //< Node acceleration

/// @brief Construct a node with an ID, position, displacement, velocity, and acceleration
/// vectors
Node(
int id, Array_7 position, Array_7 displacement = Array_7{0., 0., 0., 1., 0., 0., 0.},
Array_6 velocity = Array_6{0., 0., 0., 0., 0., 0.},
Array_6 acceleration = Array_6{0., 0., 0., 0., 0., 0.}
)
: ID(id), x(position), u(displacement), v(velocity), vd(acceleration) {}

/// Translate node by a displacement vector
void Translate(const Array_3& displacement) {
x[0] += displacement[0];
x[1] += displacement[1];
x[2] += displacement[2];
}

/// Rotate node by a quaternion
void Rotate(const Array_4& q) {
// Rotate position
auto x_rot = RotateVectorByQuaternion(q, {x[0], x[1], x[2]});
x[0] = x_rot[0];
x[1] = x_rot[1];
x[2] = x_rot[2];

// Rotate orientation
auto q_rot = QuaternionCompose(q, {x[3], x[4], x[5], x[6]});
x[3] = q_rot[0];
x[4] = q_rot[1];
x[5] = q_rot[2];
x[6] = q_rot[3];
}

/// Rotate node by a rotation axis and angle
void Rotate(const Array_3& axis, double angle) {
auto q = Array_4{
cos(angle / 2.), sin(angle / 2.) * axis[0], sin(angle / 2.) * axis[1],
sin(angle / 2.) * axis[2]};
Rotate(q);
}
};

} // namespace openturbine
Loading

0 comments on commit ae02689

Please sign in to comment.