Skip to content

Commit

Permalink
Add JSON porter for network and simulation import new json model
Browse files Browse the repository at this point in the history
  • Loading branch information
micheltakken committed Nov 14, 2023
1 parent 28f9138 commit fe10070
Show file tree
Hide file tree
Showing 16 changed files with 430 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(HEADER_LIST
add_subdirectory(nodalAnalysis)
add_subdirectory(architecture)
add_subdirectory(simulation)
add_subdirectory(porting)

target_sources(${TARGET_NAME} PUBLIC ${SOURCE_LIST} ${HEADER_LIST})
target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR})
20 changes: 17 additions & 3 deletions src/architecture/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ struct Group {
* @param[in] nodeIds Ids of the nodes that constitute this group.
* @param[in] channelIds Ids of the channels that constitute this group.
*/
Group(int groupId_, std::unordered_set<int> nodeIds_, std::unordered_set<int> channelIds_) :
Group(int groupId_, std::unordered_set<int> nodeIds_, std::unordered_set<int> channelIds_, Network<T>* network_) :
groupId(groupId_), nodeIds(nodeIds_), channelIds(channelIds_) {
for (auto& nodeId : nodeIds) {
if (nodeId <= 0) {
if (network_->getNode(nodeId)->getGround()) {
grounded = true;
}
}
Expand Down Expand Up @@ -95,6 +95,14 @@ class Network {
*/
Network(std::unordered_map<int, std::shared_ptr<Node<T>>> nodes);

/**
* @brief Constructor of the Network
* @param[in] nodes Nodes of the network.
* @param[in] channels Channels of the network.
*/
Network(std::unordered_map<int, std::shared_ptr<Node<T>>> nodes,
std::unordered_map<int, std::unique_ptr<RectangularChannel<T>>> channels);

/**
* @brief Constructor of the Network from a JSON string
* @param json json string
Expand All @@ -110,7 +118,7 @@ class Network {
/**
* @brief Get a pointer to the node with the specific id.
*/
std::shared_ptr<Node<T>>& getNode(int nodeId) const;
std::shared_ptr<Node<T>>& getNode(int nodeId);

/**
* @brief Adds a new channel to the network.
Expand Down Expand Up @@ -184,6 +192,12 @@ class Network {
* @brief Sorts the nodes and channels into detached 1D domain groups
*/
void sortGroups();

/**
* @brief Set the modules of the network for a hybrid simulation.
* @param[in] modules The modules that handle the CFD simulations.
*/
void setModules(std::unordered_map<int, std::unique_ptr<lbmModule<T>>> modules);
};

} // namespace arch
14 changes: 12 additions & 2 deletions src/architecture/Network.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ namespace arch {
nodes(nodes_), channels(channels_), flowRatePumps(flowRatePumps_),
pressurePumps(pressurePumps_), modules(modules_) { }

template<typename T>
Network<T>::Network(std::unordered_map<int, std::shared_ptr<Node<T>>> nodes_,
std::unordered_map<int, std::unique_ptr<RectangularChannel<T>>> channels_) :
nodes(nodes_), channels(std::move(channels_)) { }

template<typename T>
Network<T>::Network(std::unordered_map<int, std::shared_ptr<Node<T>>> nodes_) :
nodes(nodes_) {
Expand Down Expand Up @@ -153,7 +158,7 @@ namespace arch {
}

template<typename T>
std::shared_ptr<Node<T>>& Network<T>::getNode(int nodeId) const {
std::shared_ptr<Node<T>>& Network<T>::getNode(int nodeId) {
return nodes.at(nodeId);
};

Expand Down Expand Up @@ -181,6 +186,11 @@ namespace arch {
return std::get<0>(modules.at(moduleId));
}

template<typename T>
void Network<T>::setModules(std::unordered_map<int, std::unique_ptr<lbmModule<T>>> modules_) {
this->modules = std::move(modules_);
}

template<typename T>
const std::unordered_map<int, std::shared_ptr<Node<T>>>& Network<T>::getNodes() const {
return nodes;
Expand Down Expand Up @@ -287,7 +297,7 @@ namespace arch {
}
}

Group<T>* addGroup = new Group<T>(groupId, nodeIds, channelIds);
Group<T>* addGroup = new Group<T>(groupId, nodeIds, channelIds, this);
groups.try_emplace(groupId, addGroup);

groupId++;
Expand Down
13 changes: 13 additions & 0 deletions src/architecture/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace arch {
int const id;
std::vector<T> pos;
T pressure = 0;
bool ground = false;

public:
/**
Expand All @@ -35,6 +36,18 @@ namespace arch {
*/
void setPressure(T pressure);

/**
* @brief Set the ground node role to the node.
* @param[in] ground Boolean value for ground node role.
*/
void setGround(bool ground);

/**
* @brief Get the ground node role of the node.
* @returns Boolean value for ground node role.
*/
bool getGround();

/**
* @brief Get id of the node.
* @returns id.
Expand Down
10 changes: 10 additions & 0 deletions src/architecture/Node.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ namespace arch {
return pressure;
}

template<typename T>
void Node<T>::setGround(bool ground_) {
ground = ground_;
}

template<typename T>
bool Node<T>::getGround() {
return ground;
}

} // namespace arch
4 changes: 3 additions & 1 deletion src/baseSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
#include "architecture/Network.h"
#include "architecture/Node.h"
#include "architecture/Platform.h"
#include "architecture/PressurePump.h"
#include "architecture/PressurePump.h"

#include "porting/jsonPorter.h"
4 changes: 3 additions & 1 deletion src/baseSimulator.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
#include "architecture/Module.hh"
#include "architecture/Network.hh"
#include "architecture/Node.hh"
#include "architecture/PressurePump.hh"
#include "architecture/PressurePump.hh"

#include "porting/jsonPorter.hh"
30 changes: 5 additions & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,20 @@ using T = double;

int main(int argc, char const* argv []) {

std::cout << "[Main] Create simulation object..." << std::endl;

// New simulation object
sim::Simulation<T> testSimulation = sim::Simulation<T>();

std::cout << "[Main] Load the JSON network..." << std::endl;

// Load and set the network from a JSON file
//std::string file("../examples/Hybrid/Network1.JSON");
std::string file = argv[1];
arch::Network<T>* network = new arch::Network<T>(file);

std::cout << "[Main] Add pressure and Flow rate pumps..." << std::endl;
// Add Pressure and Flow Rate Pumps
network->setPressurePump(0, T(1e3));
testSimulation.setNetwork(network);

std::cout << "[Main] Set the continuous phase fluid..." << std::endl;
// Define and set the continuous phase fluid
sim::Fluid<T>* fluid0 = new sim::Fluid<T>(0, T(1000), T(1e-3));
fluid0->setName("Water");
testSimulation.setContinuousPhase(fluid0);
std::cout << "[Main] Create simulation object..." << std::endl;

std::cout << "[Main] Set the resistance model..." << std::endl;
// Define and set the resistance model
sim::ResistanceModel2DPoiseuille<T>* resistanceModel = new sim::ResistanceModel2DPoiseuille<T>(fluid0->getViscosity());
testSimulation.setResistanceModel(resistanceModel);
// Load and set the network from a JSON file
arch::Network<T> network = porting::networkFromJSON<T>(file);

// Load and set the simulation from a JSON file
sim::Simulation<T> testSimulation = porting::simulationFromJSON<T>(file, &network);

std::cout << "[Main] Simulation..." << std::endl;
// Perform simulation and store results
testSimulation.simulate();


std::cout << "[Main] Results..." << std::endl;
// Print the results
testSimulation.printResults();
Expand Down
15 changes: 7 additions & 8 deletions src/nodalAnalysis/NodalAnalysis.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ namespace nodal {
template<typename T>
bool conductNodalAnalysis( const arch::Network<T>* network)
{
const int groundNodeValue = 0;
const int nNodes = network->getNodes().size() - 1; // -1 due to ground node
std::unordered_set<int> conductingNodeIds;
std::unordered_map<int, int> groundNodeIds;

// Sort nodes into conducting nodes and ground nodes.
// First loop, all nodes with id > 0 are conduting nodes.
// First loop, all nodes with id > 0 are conducting nodes.
int iPump = nNodes;
for (const auto& [key, group] : network->getGroups()) {
for (const auto& nodeId : group->nodeIds) {
if(nodeId > groundNodeValue && nodeId != group->groundNodeId) {
if(!network->getNodes().at(nodeId)->getGround() && nodeId != group->groundNodeId) {
conductingNodeIds.emplace(nodeId);
} else if (nodeId > groundNodeValue && nodeId == group->groundNodeId) {
} else if (!network->getNodes().at(nodeId)->getGround() && nodeId == group->groundNodeId) {
groundNodeIds.emplace(nodeId, iPump);
iPump++;
}
Expand All @@ -54,16 +53,16 @@ namespace nodal {
const T conductance = 1. / channel.second->getResistance();

// main diagonal elements of G
if (nodeAMatrixId > groundNodeValue) {
if (!network->getNodes().at(nodeAMatrixId)->getGround()) {
A(nodeAMatrixId, nodeAMatrixId) += conductance;
}

if (nodeBMatrixId > groundNodeValue) {
if (!network->getNodes().at(nodeBMatrixId)->getGround()) {
A(nodeBMatrixId, nodeBMatrixId) += conductance;
}

// minor diagonal elements of G (if no ground node was present)
if (nodeAMatrixId > groundNodeValue && nodeBMatrixId > groundNodeValue) {
if (!network->getNodes().at(nodeAMatrixId)->getGround() && !network->getNodes().at(nodeBMatrixId)->getGround()) {
A(nodeAMatrixId, nodeBMatrixId) -= conductance;
A(nodeBMatrixId, nodeAMatrixId) -= conductance;
}
Expand Down Expand Up @@ -175,7 +174,7 @@ namespace nodal {
auto& node = network->getNodes().at(nodeMatrixId);
if (contains(conductingNodeIds, nodeMatrixId)) {
node->setPressure(x(nodeMatrixId));
} else if (nodeMatrixId <= groundNodeValue) {
} else if (node->getGround()) {
node->setPressure(0.0);
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/porting/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set(SOURCE_LIST
jsonPorter.hh
)

set(HEADER_LIST
jsonPorter.h
)

target_sources(${TARGET_NAME} PUBLIC ${SOURCE_LIST} ${HEADER_LIST})
target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(${TARGET_NAME} PUBLIC lbmLib)
36 changes: 36 additions & 0 deletions src/porting/jsonPorter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <memory>
#include <fstream>
#include <unordered_map>
#include <vector>

#include "../architecture/Channel.h"
#include "../architecture/FlowRatePump.h"
#include "../architecture/lbmModule.h"
#include "../architecture/Network.h"
#include "../architecture/Node.h"
#include "../architecture/Platform.h"
#include "../architecture/PressurePump.h"

#include "../simulation/Simulation.h"

#include "nlohmann/json.hpp"

namespace porting {

/**
* @brief Constructor of the Network from a JSON string
* @param json json string
* @return Network network
*/
template<typename T>
arch::Network<T> networkFromJSON(std::string jsonFile);

template<typename T>
sim::Simulation<T> simulationFromJSON(std::string jsonFile, arch::Network<T>* network_);

template<typename T>
void resultToJSON(std::string jsonFile);

} // namespace porting
Loading

0 comments on commit fe10070

Please sign in to comment.