Skip to content

Commit

Permalink
🚨 Add tests for hybrid simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
micheltakken committed Nov 7, 2023
1 parent 28f9138 commit 737fc2b
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 33 deletions.
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ FetchContent_Declare(
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG 3.3.9
)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.11.0
)
FetchContent_Declare(
json
GIT_REPOSITORY https://github.com/nlohmann/json.git
Expand All @@ -32,7 +37,7 @@ FetchContent_Declare(
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
)
FetchContent_MakeAvailable(eigen json lbm)
FetchContent_MakeAvailable(eigen googletest json lbm)

add_library(lbmLib)

Expand Down Expand Up @@ -103,3 +108,14 @@ option(BINDINGS "Configure for building Python bindings")
if(BINDINGS)
add_subdirectory(python/mmft/hybridsim)
endif()

# create tests
enable_testing()
include(GoogleTest)
set(TARGET_NAME simulatorTest)
add_executable(${TARGET_NAME})
target_link_libraries(${TARGET_NAME} PUBLIC gtest gtest_main)
target_link_libraries(${TARGET_NAME} PUBLIC gtest lbmLib)
target_link_libraries(${TARGET_NAME} PUBLIC gtest hybridLib)
add_subdirectory(tests)
gtest_discover_tests(${TARGET_NAME})
9 changes: 9 additions & 0 deletions src/architecture/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ class Network {
std::unordered_map<int, std::unique_ptr<FlowRatePump<T>>> flowRatePump,
std::unordered_map<int, std::unique_ptr<PressurePump<T>>> pressurePump,
std::unordered_map<int, std::unique_ptr<lbmModule<T>>> modules);

public:
/**
* @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);

public:
/**
Expand Down
27 changes: 19 additions & 8 deletions src/architecture/Network.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ namespace arch {
std::unordered_map<int, std::unique_ptr<lbmModule<T>>> modules_) :
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(channels_) { }

template<typename T>
Network<T>::Network(std::unordered_map<int, std::shared_ptr<Node<T>>> nodes_) :
Expand Down Expand Up @@ -46,16 +51,20 @@ namespace arch {
std::ifstream f(jsonFile);
json jsonString = json::parse(f);

std::cout << "Loading Nodes..." << std::endl;
#ifdef VERBOSE
std::cout << "Loading Nodes..." << std::endl;
#endif

for (auto& node : jsonString["Network"]["Nodes"]) {
Node<T>* addNode = new Node<T>(node["iD"], T(node["x"]), T(node["y"]));
nodes.try_emplace(node["iD"], addNode);
}

std::cout << "Loaded Nodes... OK" << std::endl;
#ifdef VERBOSE
std::cout << "Loaded Nodes... OK" << std::endl;

std::cout << "Loading Channels..." << std::endl;
std::cout << "Loading Channels..." << std::endl;
#endif

for (auto& channel : jsonString["Network"]["Channels"]) {
RectangularChannel<T>* addChannel = nullptr;
Expand Down Expand Up @@ -118,10 +127,11 @@ namespace arch {
channels.try_emplace(channel["iD"], addChannel);
}

std::cout << "Loaded Channels... OK" << std::endl;

std::cout << "Loading Modules..." << std::endl;
#ifdef VERBOSE
std::cout << "Loaded Channels... OK" << std::endl;

std::cout << "Loading Modules..." << std::endl;
#endif
for (auto& module : jsonString["Network"]["Modules"]) {
std::unordered_map<int, std::shared_ptr<Node<T>>> Nodes;
std::unordered_map<int, Opening<T>> Openings;
Expand All @@ -141,8 +151,9 @@ namespace arch {
modules.try_emplace(module["iD"], addModule);
}
this->sortGroups();

std::cout << "Loaded Modules... OK" << std::endl;
#ifdef VERBOSE
std::cout << "Loaded Modules... OK" << std::endl;
#endif
}

template<typename T>
Expand Down
60 changes: 45 additions & 15 deletions src/architecture/lbmModule.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ namespace arch{
template<typename T>
void lbmModule<T>::prepareGeometry () {

bool print = false;

olb::STLreader<T> stlReader(stlFile, converter->getConversionFactorLength());
std::cout << "[lbmModule] reading STL file " << name << "... OK" << std::endl;
#ifdef VERBOSE
print = true;
std::cout << "[lbmModule] reading STL file " << name << "... OK" << std::endl;
#endif
olb::IndicatorF2DfromIndicatorF3D<T> stl2Dindicator(stlReader);
std::cout << "[lbmModule] create 2D indicator " << name << "... OK" << std::endl;
#ifdef VERBOSE
std::cout << "[lbmModule] create 2D indicator " << name << "... OK" << std::endl;
#endif

olb::Vector<T,2> origin(-0.5*converter->getConversionFactorLength(), -0.5*converter->getConversionFactorLength());
olb::Vector<T,2> extend(this->size[0] + converter->getConversionFactorLength(), this->size[1] + converter->getConversionFactorLength());
Expand All @@ -37,12 +44,16 @@ namespace arch{
*cuboidGeometry,
*loadBalancer);

std::cout << "[lbmModule] generate geometry " << name << "... OK" << std::endl;
#ifdef VERBOSE
std::cout << "[lbmModule] generate geometry " << name << "... OK" << std::endl;
#endif

this->geometry->rename(0, 2);
this->geometry->rename(2, 1, stl2Dindicator);

std::cout << "[lbmModule] generate 2D geometry from STL " << name << "... OK" << std::endl;
#ifdef VERBOSE
std::cout << "[lbmModule] generate 2D geometry from STL " << name << "... OK" << std::endl;
#endif

for (auto& [key, Opening] : moduleOpenings ) {
// The unit vector pointing to the extend (opposite origin) of the opening
Expand Down Expand Up @@ -71,10 +82,12 @@ namespace arch{
this->geometry->rename(2, key+3, 1, opening);
}

this->geometry->clean();
this->geometry->checkForErrors();
this->geometry->clean(print);
this->geometry->checkForErrors(print);

std::cout << "[lbmModule] prepare geometry " << name << "... OK" << std::endl;
#ifdef VERBOSE
std::cout << "[lbmModule] prepare geometry " << name << "... OK" << std::endl;
#endif
}

template<typename T>
Expand Down Expand Up @@ -132,7 +145,9 @@ namespace arch{
lattice->template setParameter<olb::descriptors::OMEGA>(omega);
lattice->initialize();

std::cout << "[lbmModule] prepare lattice " << name << "... OK" << std::endl;
#ifdef VERBOSE
std::cout << "[lbmModule] prepare lattice " << name << "... OK" << std::endl;
#endif
}

template<typename T>
Expand Down Expand Up @@ -174,13 +189,17 @@ namespace arch{
T newPressure = output[0]/output[1];
pressures.at(key) = newPressure;
if (iT % 1000 == 0) {
meanPressures.at(key)->print();
#ifdef VERBOSE
meanPressures.at(key)->print();
#endif
}
} else {
fluxes.at(key)->operator()(output,input);
flowRates.at(key) = output[0];
if (iT % 1000 == 0) {
fluxes.at(key)->print();
#ifdef VERBOSE
fluxes.at(key)->print();
#endif
}
}
}
Expand Down Expand Up @@ -211,7 +230,9 @@ namespace arch{
density
);

this->converter->print();
#ifdef VERBOSE
this->converter->print();
#endif

// Initialize pressure, flowRate and resistance value-containers
for (auto& [key, node] : this->boundaryNodes) {
Expand All @@ -222,12 +243,19 @@ namespace arch{
// Initialize a convergence tracker for pressure
this->converge = std::make_unique<olb::util::ValueTracer<T>> (stepIter, epsilon);

std::cout << "[lbmModule] lbmInit " << name << "... OK" << std::endl;
#ifdef VERBOSE
std::cout << "[lbmModule] lbmInit " << name << "... OK" << std::endl;
#endif
}

template<typename T>
void lbmModule<T>::writeVTK (int iT) {

bool print = false;
#ifdef VERBOSE
print = true;
#endif

olb::SuperVTMwriter2D<T> vtmWriter( name );
// Writes geometry to file system
if (iT == 0) {
Expand All @@ -247,13 +275,15 @@ namespace arch{

// write vtk to file system
vtmWriter.write(iT);
converge->takeValue(getLattice().getStatistics().getAverageEnergy(), true);
converge->takeValue(getLattice().getStatistics().getAverageEnergy(), print);
}
if (iT %1000 == 0) {
std::cout << "[writeVTK] " << name << " currently at timestep " << iT << std::endl;
#ifdef VERBOSE
std::cout << "[writeVTK] " << name << " currently at timestep " << iT << std::endl;
#endif
}

converge->takeValue(getLattice().getStatistics().getAverageEnergy(), true);
converge->takeValue(getLattice().getStatistics().getAverageEnergy(), print);

if (iT%100 == 0) {
if (converge->hasConverged()) {
Expand Down
27 changes: 18 additions & 9 deletions src/simulation/Simulation.hh
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ namespace sim {

}

if (pressureConverged && allConverged) {
std::cout << "[Simulation] All pressures have converged." << std::endl;
}

printResults();
#ifdef VERBOSE
if (pressureConverged && allConverged) {
std::cout << "[Simulation] All pressures have converged." << std::endl;
}
printResults();
#endif
}
}

Expand Down Expand Up @@ -110,7 +111,9 @@ namespace sim {
this->resistanceModel = new ResistanceModel2DPoiseuille(continuousPhase->getViscosity());

// compute and set channel lengths
std::cout << "[Simulation] Compute and set channel lengths..." << std::endl;
#ifdef VERBOSE
std::cout << "[Simulation] Compute and set channel lengths..." << std::endl;
#endif
for (auto& [key, channel] : network->getChannels()) {
auto& nodeA = network->getNodes().at(channel->getNodeA());
auto& nodeB = network->getNodes().at(channel->getNodeB());
Expand All @@ -120,7 +123,9 @@ namespace sim {
}

// compute channel resistances
std::cout << "[Simulation] Compute and set channel resistances..." << std::endl;
#ifdef VERBOSE
std::cout << "[Simulation] Compute and set channel resistances..." << std::endl;
#endif
for (auto& [key, channel] : network->getChannels()) {
T resistance = resistanceModel->getChannelResistance(channel.get());
channel->setResistance(resistance);
Expand Down Expand Up @@ -153,11 +158,15 @@ namespace sim {
}

// compute nodal analysis
std::cout << "[Simulation] Conduct initial nodal analysis..." << std::endl;
#ifdef VERBOSE
std::cout << "[Simulation] Conduct initial nodal analysis..." << std::endl;
#endif
nodal::conductNodalAnalysis(this->network);

// Prepare CFD geometry and lattice
std::cout << "[Simulation] Prepare CFD geometry and lattice..." << std::endl;
#ifdef VERBOSE
std::cout << "[Simulation] Prepare CFD geometry and lattice..." << std::endl;
#endif

for (auto& [key, module] : network->getModules()) {
module->prepareGeometry();
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(hybrid)
5 changes: 5 additions & 0 deletions tests/hybrid/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(SOURCE_LIST
Hybrid.test.cpp
)

target_sources(${TARGET_NAME} PRIVATE ${SOURCE_LIST})
Loading

0 comments on commit 737fc2b

Please sign in to comment.