Skip to content

Commit

Permalink
Compile and Run case after Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
micheltakken committed Aug 26, 2024
1 parent 9e88c90 commit 7d180d3
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 152 deletions.
69 changes: 15 additions & 54 deletions src/architecture/Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ enum class ChannelShape {
/**
* @brief A struct that defines a straight channel segment
*/
template<typename T, int DIM>
struct Line_segment { };

template<typename T>
struct Line_segment<T, 2> {
std::vector<T> start;
std::vector<T> end;
struct Line_segment {
std::array<T,3> start;
std::array<T,3> end;

Line_segment(std::vector<T> start, std::vector<T> end);
Line_segment(std::array<T,3> start, std::array<T,3> end);

/**
* @brief Returns the length of this line segment.
Expand All @@ -55,53 +52,17 @@ struct Line_segment<T, 2> {
T getLength();
};

template<typename T>
struct Line_segment<T, 3> {
std::vector<T> start;
std::vector<T> end;

Line_segment(std::vector<T> start, std::vector<T> end);

/**
* @brief Returns the length of this line segment.
* @returns Length of line segment in m.
*/
T getLength();
};

/**
* @brief A struct that defines an arc channel segment
*/
template<typename T, int DIM>
struct Arc { };

template<typename T>
struct Arc<T, 2> {
bool right;
std::vector<T> start;
std::vector<T> end;
std::vector<T> center;

Arc(bool right, std::vector<T> start, std::vector<T> end, std::vector<T> center);

/**
* @brief Returns the length of this arc.
* @returns Length of arc in m.
*/
T getLength();
};

/**
* @brief A struct that defines an arc channel segment
*/
template<typename T>
struct Arc<T, 3> {
struct Arc {
bool right;
std::vector<T> start;
std::vector<T> end;
std::vector<T> center;
std::array<T,3> start;
std::array<T,3> end;
std::array<T,3> center;

Arc(bool right, std::vector<T> start, std::vector<T> end, std::vector<T> center);
Arc(bool right, std::array<T,3> start, std::array<T,3> end, std::array<T,3> center);

/**
* @brief Returns the length of this arc.
Expand All @@ -121,8 +82,8 @@ class Channel : public Edge<T>{
ChannelShape shape = ChannelShape::NONE; ///< The cross-section shape of this channel is rectangular.
ChannelType type = ChannelType::NORMAL; ///< What kind of channel it is.

std::vector<std::unique_ptr<Line_segment<T,2>>> line_segments; ///< Straight line segments in the channel.
std::vector<std::unique_ptr<Arc<T,2>>> arcs; ///< Arcs in the channel.
std::vector<std::shared_ptr<Line_segment<T>>> line_segments; ///< Straight line segments in the channel.
std::vector<std::shared_ptr<Arc<T>>> arcs; ///< Arcs in the channel.

public:
/**
Expand All @@ -134,8 +95,8 @@ class Channel : public Edge<T>{
* @param[in] arcs The arcs of this channel.
*/
Channel(int id, std::shared_ptr<Node<T>> nodeA, std::shared_ptr<Node<T>> nodeB,
std::vector<Line_segment<T,2>*> line_segments,
std::vector<Arc<T,2>*> arcs);
std::vector<Line_segment<T>*> line_segments,
std::vector<Arc<T>*> arcs);

/**
* @brief Constructor of a channel connecting two-nodes.
Expand Down Expand Up @@ -258,8 +219,8 @@ class RectangularChannel : public Channel<T> {
* @param[in] height The height of the channel cross-section.
*/
RectangularChannel(int id, std::shared_ptr<Node<T>> nodeA, std::shared_ptr<Node<T>> nodeB,
std::vector<Line_segment<T,2>*> line_segments,
std::vector<Arc<T,2>*> arcs, T width, T height);
std::vector<Line_segment<T>*> line_segments,
std::vector<Arc<T>*> arcs, T width, T height);

/**
* @brief Set width of rectangular channel.
Expand Down
60 changes: 12 additions & 48 deletions src/architecture/Channel.hh
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,12 @@ namespace arch {
//=====================================================================================

template<typename T>
Line_segment<T,2>::Line_segment(std::vector<T> start_, std::vector<T> end_) :
Line_segment<T>::Line_segment(std::array<T,3> start_, std::array<T,3> end_) :
start(start_), end(end_) {
}

template<typename T>
T Line_segment<T,2>::getLength() {
T dx = start[0] - end[0];
T dy = start[1] - end[1];
T length = sqrt(dx*dx + dy*dy);

return length;
}

template<typename T>
Line_segment<T,3>::Line_segment(std::vector<T> start_, std::vector<T> end_) :
start(start_), end(end_) {
}

template<typename T>
T Line_segment<T,3>::getLength() {
T Line_segment<T>::getLength() {
T dx = start[0] - end[0];
T dy = start[1] - end[1];
T dz = start[2] - end[2];
Expand All @@ -42,33 +28,12 @@ namespace arch {
//=====================================================================================

template<typename T>
Arc<T,2>::Arc(bool right_, std::vector<T> start_, std::vector<T> end_, std::vector<T> center_) :
Arc<T>::Arc(bool right_, std::array<T,3> start_, std::array<T,3> end_, std::array<T,3> center_) :
right(right_), start(start_), end(end_), center(center_) {
}


template<typename T>
T Arc<T,2>::getLength() {
T dx1 = start[0] - center[0];
T dy1 = start[1] - center[1];
T dx2 = start[0] - end[0];
T dy2 = start[1] - end[1];

T radius = sqrt(dx1*dx1 + dy1*dy1);
T chord = sqrt(dx2*dx2 + dy2*dy2);
T theta = 2*asin(chord/(2*radius));
T length = theta*radius;

return length;
}

template<typename T>
Arc<T,3>::Arc(bool right_, std::vector<T> start_, std::vector<T> end_, std::vector<T> center_) :
right(right_), start(start_), end(end_), center(center_) {
}

template<typename T>
T Arc<T,3>::getLength() {
T Arc<T>::getLength() {
T dx1 = start[0] - center[0];
T dy1 = start[1] - center[1];
T dz1 = start[2] - center[2];
Expand All @@ -82,8 +47,7 @@ namespace arch {
T length = theta*radius;

return length;
}

}

//=====================================================================================
//================================ Channel ===========================================
Expand All @@ -92,24 +56,24 @@ namespace arch {
template<typename T>
Channel<T>::Channel(int id_, std::shared_ptr<Node<T>> nodeA_, std::shared_ptr<Node<T>> nodeB_) :
Edge<T>(id_, nodeA_->getId(), nodeB_->getId()) {
std::unique_ptr<Line_segment<T,2>> line = std::make_unique<Line_segment<T,2>> (nodeA_->getPosition(), nodeB_->getPosition());
std::shared_ptr<Line_segment<T>> line = std::make_shared<Line_segment<T>> (nodeA_->getPosition(), nodeB_->getPosition());
this->length = line->getLength();
line_segments.push_back(std::move(line));
}

template<typename T>
Channel<T>::Channel(int id_, std::shared_ptr<Node<T>> nodeA_, std::shared_ptr<Node<T>> nodeB_,
std::vector<Line_segment<T,2>*> line_segments_,
std::vector<Arc<T,2>*> arcs_) :
std::vector<Line_segment<T>*> line_segments_,
std::vector<Arc<T>*> arcs_) :
Edge<T>(id_, nodeA_->getId(), nodeB_->getId()) {
for (auto& line : line_segments_) {
this->length += line->getLength();
std::unique_ptr<Line_segment<T,2>> uLine(line);
std::unique_ptr<Line_segment<T>> uLine(line);
line_segments.push_back(std::move(uLine));
}
for (auto& arc : arcs_) {
this->length += arc->getLength();
std::unique_ptr<Arc<T,2>> uArc(arc);
std::unique_ptr<Arc<T>> uArc(arc);
arcs.push_back(std::move(uArc));
}
}
Expand Down Expand Up @@ -191,8 +155,8 @@ namespace arch {

template<typename T>
RectangularChannel<T>::RectangularChannel(int id_, std::shared_ptr<Node<T>> nodeA_, std::shared_ptr<Node<T>> nodeB_,
std::vector<Line_segment<T,2>*> line_segments_,
std::vector<Arc<T,2>*> arcs_, T width_, T height_) :
std::vector<Line_segment<T>*> line_segments_,
std::vector<Arc<T>*> arcs_, T width_, T height_) :
Channel<T>(id_, nodeA_, nodeB_, line_segments_, arcs_), width(width_), height(height_) {
this->area = width*height;
}
Expand Down
8 changes: 4 additions & 4 deletions src/architecture/Network.hh
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ Network<T>::Network(std::string jsonFile) {
for (auto& channel : jsonString["Network"]["Channels"]) {
RectangularChannel<T>* addChannel = nullptr;
if (channel.contains("pieces")) {
std::vector<Line_segment<T,2>*> line_segments;
std::vector<Arc<T,2>*> arcs;
std::vector<Line_segment<T>*> line_segments;
std::vector<Arc<T>*> arcs;
for (auto& piece : channel["pieces"]) {
std::vector<T> start(2);
std::vector<T> end(2);
Expand All @@ -85,7 +85,7 @@ Network<T>::Network(std::string jsonFile) {
end[0] = piece["line_segment"]["end"][0];
end[1] = piece["line_segment"]["end"][1];
}
Line_segment<T,2>* addLineSeg = new Line_segment<T,2> (start, end);
Line_segment<T>* addLineSeg = new Line_segment<T> (start, end);
line_segments.emplace_back(std::move(addLineSeg));
} else if (piece.contains("arc")) {
if (piece["arc"]["start"] == 0) {
Expand All @@ -106,7 +106,7 @@ Network<T>::Network(std::string jsonFile) {
}
center[0] = piece["arc"]["center"][0];
center[1] = piece["arc"]["center"][1];
Arc<T,2>* addArc = new Arc<T,2> (piece["arc"]["right"], start, end, center);
Arc<T>* addArc = new Arc<T> (piece["arc"]["right"], start, end, center);
arcs.emplace_back(std::move(addArc));
}
}
Expand Down
18 changes: 16 additions & 2 deletions src/architecture/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ template<typename T>
class Node {
private:
int const id;
std::vector<T> pos;
std::array<T,3> pos;
T pressure = 0;
bool ground = false;
bool sink = false;
Expand All @@ -29,12 +29,26 @@ class Node {
*/
Node(int id, T x, T y, bool ground=false);

/**
* @brief Constructor of the node.
* @param[in] id Id of the node.
* @param[in] x Absolute x position of the node.
* @param[in] y Absolute y position of the node.
*/
Node(int id, T x, T y, T z, bool ground=false);

/**
* @brief Get position of the node.
* @param[in] pos Vector of the absolute position of the node.
*/
void setPosition(std::vector<T> pos);

/**
* @brief Get position of the node.
* @param[in] pos Vector of the absolute position of the node.
*/
void setPosition(std::array<T,3> pos);

/**
* @brief Set pressure level at the node.
* @param[in] pressure Pressure level at the node in Pa.
Expand Down Expand Up @@ -75,7 +89,7 @@ class Node {
* @brief Get position of the node.
* @returns Absolute position of the node
*/
std::vector<T> getPosition() const;
std::array<T,3> getPosition() const;

/**
* @brief Get pressure level at node.
Expand Down
35 changes: 29 additions & 6 deletions src/architecture/Node.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,37 @@ namespace arch {

template<typename T>
Node<T>::Node(int id_, T x_, T y_, bool ground_) :
id(id_), ground(ground_) {
pos.push_back(x_);
pos.push_back(y_);
}
id(id_), ground(ground_)
{
pos[0] = x_;
pos[1] = y_;
pos[2] = T(0.0);
}

template<typename T>
Node<T>::Node(int id_, T x_, T y_, T z_, bool ground_) :
id(id_), ground(ground_)
{
pos[0] = x_;
pos[1] = y_;
pos[2] = z_;
}

template<typename T>
void Node<T>::setPosition(std::vector<T> pos_) {
this->pos = pos_;
if (pos_.size() == 2) {
std::copy(pos_.begin(), pos_.begin() + 1, pos.begin());
pos[2] = T(0.0);
} else if (pos_.size() == 3) {
std::copy(pos_.begin(), pos_.begin() + 2, pos.begin());
} else {
throw std::domain_error("Node position should have two or three elements.");
}
}

template<typename T>
void Node<T>::setPosition(std::array<T,3> pos_) {
this->pos = pos_;
}

template<typename T>
Expand All @@ -25,7 +48,7 @@ int Node<T>::getId() const {
}

template<typename T>
std::vector<T> Node<T>::getPosition() const {
std::array<T,3> Node<T>::getPosition() const {
return pos;
}

Expand Down
2 changes: 0 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
//namespace py = pybind11;
#include <iostream>

#define DIMENSION 2

#include <baseSimulator.h>
#include <baseSimulator.hh>

Expand Down
6 changes: 0 additions & 6 deletions src/porting/jsonReaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ namespace sim {
// Forward declared dependencies
enum class Platform;

template<typename T, int DIM>
class lbmSimulator;

template<typename T, int DIM>
class essLbmSimulator;

template<typename T>
class lbmSimulator;

Expand Down
16 changes: 0 additions & 16 deletions src/porting/jsonReaders.hh
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,6 @@ void readModules(json jsonString, arch::Network<T>& network) {
}
}

template<typename T>
void readModules(json jsonString, arch::Network<T>& network) {
for (auto& module : jsonString["network"]["modules"]) {
if (!module.contains("position") || !module.contains("size") || !module.contains("nodes")) {
throw std::invalid_argument("Module is ill-defined. Please define:\nposition\nsize\nnodes");
}
std::vector<T> position = { module["position"][0], module["position"][1] };
std::vector<T> size = { module["size"][0], module["size"][1] };
std::unordered_map<int, std::shared_ptr<arch::Node<T>>> Nodes;
for (auto& nodeId : module["nodes"]) {
Nodes.try_emplace(nodeId, network.getNode(nodeId));
}
network.addModule(position, size, std::move(Nodes));
}
}

template<typename T>
sim::Platform readPlatform(json jsonString, sim::Simulation<T>& simulation) {
sim::Platform platform = sim::Platform::Continuous;
Expand Down
Loading

0 comments on commit 7d180d3

Please sign in to comment.