Skip to content

Commit

Permalink
🎉 Initial commit merged 1D and Hybrid Simulators
Browse files Browse the repository at this point in the history
  • Loading branch information
micheltakken committed Nov 17, 2023
1 parent 3f1076c commit d47d706
Show file tree
Hide file tree
Showing 37 changed files with 2,045 additions and 61 deletions.
2 changes: 1 addition & 1 deletion python/mmft/hybridsim/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ PYBIND11_MODULE(pyhybridsim, m) {
.def("setName", &sim::Fluid<T>::setName, "name"_a)
.def("getViscosity", &sim::Fluid<T>::getViscosity);

py::class_<sim::ResistanceModel2DPoiseuille<T>>(m, "ResistanceModel")
py::class_<sim::ResistanceModelPoiseuille<T>>(m, "ResistanceModel")
.def(py::init<T &>());

py::class_<sim::Simulation<T>>(m, "Simulation")
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_subdirectory(nodalAnalysis)
add_subdirectory(architecture)
add_subdirectory(simulation)
add_subdirectory(porting)
add_subdirectory(result)

target_sources(${TARGET_NAME} PUBLIC ${SOURCE_LIST} ${HEADER_LIST})
target_include_directories(${TARGET_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR})
4 changes: 4 additions & 0 deletions src/architecture/Channel.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file Channel.h
*/

#pragma once

#include <memory>
Expand Down
4 changes: 4 additions & 0 deletions src/architecture/Edge.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file Edge.h
*/

#pragma once

#include <string>
Expand Down
4 changes: 4 additions & 0 deletions src/architecture/FlowRatePump.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file FlowRatePump.h
*/

#pragma once

#include "Edge.h"
Expand Down
4 changes: 4 additions & 0 deletions src/architecture/Module.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file Module.h
*/

#pragma once

#include <vector>
Expand Down
4 changes: 4 additions & 0 deletions src/architecture/Network.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file Network.h
*/

#pragma once

#include <memory>
Expand Down
4 changes: 4 additions & 0 deletions src/architecture/Node.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file Node.h
*/

#pragma once

#include <vector>
Expand Down
4 changes: 4 additions & 0 deletions src/architecture/PressurePump.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file PressurePump.h
*/

#pragma once

#include "Edge.h"
Expand Down
4 changes: 4 additions & 0 deletions src/architecture/lbmModule.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file lbmModule.h
*/

#pragma once

#define M_PI 3.14159265358979323846
Expand Down
10 changes: 10 additions & 0 deletions src/baseSimulator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/**
* @file baseSimulator.h
*/

#include "simulation/CFDSim.h"
#include "simulation/Droplet.h"
#include "simulation/Fluid.h"
#include "simulation/Injection.h"
#include "simulation/ResistanceModels.h"
#include "simulation/Simulation.h"
#include "simulation/events/BoundaryEvent.h"
#include "simulation/events/Event.h"
#include "simulation/events/InjectionEvent.h"
#include "simulation/events/MergingEvent.h"

#include "nodalAnalysis/NodalAnalysis.h"

Expand Down
5 changes: 5 additions & 0 deletions src/baseSimulator.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include "simulation/CFDSim.hh"
#include "simulation/Droplet.hh"
#include "simulation/Fluid.hh"
#include "simulation/Injection.hh"
#include "simulation/ResistanceModels.hh"
#include "simulation/Simulation.hh"
#include "simulation/events/BoundaryEvent.hh"
#include "simulation/events/InjectionEvent.hh"
#include "simulation/events/MergingEvent.hh"

#include "nodalAnalysis/NodalAnalysis.hh"

Expand Down
4 changes: 4 additions & 0 deletions src/nodalAnalysis/NodalAnalysis.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file NodalAnalysis.h
*/

#pragma once

#include <Network.h>
Expand Down
6 changes: 6 additions & 0 deletions src/porting/jsonPorter.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file jsonPorter.h
*/

#pragma once

#include <memory>
Expand All @@ -14,6 +18,8 @@

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

#include "../result/Results.h"

#include "nlohmann/json.hpp"

namespace porting {
Expand Down
8 changes: 4 additions & 4 deletions src/porting/jsonPorter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,21 @@ namespace porting {
if (!jsonString["simulation"].contains("fluids") || jsonString["simulation"]["fluids"].empty()) {
throw std::invalid_argument("No fluids are defined. Please define at least 1 fluid.");
}
std::vector<std::unique_ptr<sim::Fluid<T>>> fluids;
std::unordered_map<int, std::unique_ptr<sim::Fluid<T>>> fluids;
counter = 0;
for (auto& fluid : jsonString["simulation"]["fluids"]) {
if (fluid.contains("density") && fluid.contains("viscosity") && fluid.contains("concentration") && fluid.contains("name")) {
T density = fluid["density"];
T viscosity = fluid["viscosity"];
std::string name = fluid["name"];
std::unique_ptr<sim::Fluid<T>> newFluid = std::make_unique<sim::Fluid<T>>( counter, density, viscosity, name );
fluids.push_back(std::move(newFluid));
fluids.try_emplace(counter, std::move(newFluid));
counter++;
} else {
throw std::invalid_argument("Wrongly defined fluid. Please provide following information for fluids:\nname\ndensity\nviscosity\nconcentration");
}
}
simulation.setFluids(fluids);
simulation.setFluids(std::move(fluids));

if (platform == sim::Platform::DROPLET) {
// NOT YET SUPPORTED
Expand Down Expand Up @@ -216,7 +216,7 @@ namespace porting {
}

// Import resistance model
sim::ResistanceModel2DPoiseuille<T>* resistanceModel = new sim::ResistanceModel2DPoiseuille<T>(simulation.getContinuousPhase()->getViscosity());
sim::ResistanceModelPoiseuille<T>* resistanceModel = new sim::ResistanceModelPoiseuille<T>(simulation.getContinuousPhase()->getViscosity());
simulation.setResistanceModel(resistanceModel);

simulation.setNetwork(network_);
Expand Down
11 changes: 11 additions & 0 deletions src/result/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set(SOURCE_LIST
Results.hh
)

set(HEADER_LIST
Results.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)
128 changes: 128 additions & 0 deletions src/result/Results.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* @file Results.h
*/

#pragma once

#include <unordered_map>

#include "../architecture/Network.h"
#include "../simulation/Droplet.h"
#include "../simulation/Fluid.h"
#include "../simulation/Injection.h"

namespace result {

/**
* @brief Struct to contain a state specified by time, an unordered map of pressures, an unordered map of flow rates, a vector of clogged channel ids, an unordered map of droplet positions.
*/
template<typename T>
struct State {
int id; ///< Sequential id of the state
T time; ///< Simulation time at which the following values were calculated.
std::unordered_map<int, T> pressures; ///< Keys are the nodeIds.
std::unordered_map<int, T> flowRates; ///< Keys are the edgeIds (channels and pumps).
std::unordered_map<int, sim::DropletPosition<T>> dropletPositions; ///< Only contains the position of droplets that are currently inside the network (key is the droplet id).

/**
* @brief Constructs a state, which represent a time step during a simulation.
* @param[in] id Id of the state
* @param[in] time Value of the current time step.
*/
State(int id, T time);

/**
* @brief Constructs a state, which represent a time step during a simulation.
* @param[in] id Id of the state
* @param[in] time Value of the current time step.
* @param[in] pressures The pressure values at the nodes at the current time step.
* @param[in] flowRates The flowRate values at the nodes at the current time step.
*/
State(int id, T time, std::unordered_map<int, T> pressures, std::unordered_map<int, T> flowRates);

/**
* @brief Constructs a state, which represent a time step during a simulation.
* @param[in] id Id of the state
* @param[in] time Value of the current time step.
* @param[in] pressures The pressure values at the nodes at the current time step.
* @param[in] flowRates The flowRate values at the nodes at the current time step.
*/
State(int id, T time, std::unordered_map<int, T> pressures, std::unordered_map<int, T> flowRates, std::unordered_map<int, sim::DropletPosition<T>> dropletPositions);

/**
* @brief Function to get pressure at a specific node.
* @return Pressures of this state in Pa.
*/
std::unordered_map<int, T>& getPressures() const;

/**
* @brief Function to get flow rate at a specific channel.
* @return Flowrates of this state in m^3/s.
*/
std::unordered_map<int, T>& getFlowRates() const;

/**
* @brief Function to get flow rate at a specific channel.
* @return Flowrates of this state in m^3/s.
*/
std::unordered_map<int, T>& getDropletPositions() const;
};

/**
* @brief Struct to contain the simulation result specified by a chip, an unordered map of fluids, an unordered map of droplets, an unordered map of injections, a vector of states, a continuous fluid id, the maximal adaptive time step and the id of a resistance model.
*/
template<typename T>
struct SimulationResult {
arch::Network<T>* network; /// Contains the chip, with all the channels and pumps.
std::unordered_map<int, sim::Fluid<T>>* fluids; /// Contains all fluids which were defined (i.e., also the fluids which were created when droplets merged).
std::unordered_map<int, sim::Droplet<T>>* droplets; /// Contains all droplets that occurred during the simulation not only the once that were injected (i.e., also merged and splitted droplets)/
std::unordered_map<int, sim::DropletInjection<T>>* injections; /// Contains all injections that happened during the simulation.
std::vector<std::unique_ptr<State<T>>> states; /// Contains all states ordered according to their simulation time (beginning at the start of the simulation).

int continuousPhaseId; /// Fluid id which served as the continuous phase.
T maximalAdaptiveTimeStep; /// Value for the maximal adaptive time step that was used.
int resistanceModel; /// Id of the used resistance model.

SimulationResult( arch::Network<T>* network,
std::unordered_map<int, sim::Fluid<T>>* fluids,
std::unordered_map<int, sim::Droplet<T>>* droplets,
std::unordered_map<int, sim::DropletInjection<T>>* injections);

/**
* @brief Adds a state to the simulation results.
* @param[in] state
*/
void addState(T time, std::unordered_map<int, T> pressures, std::unordered_map<int, T> flowRates);

/**
* @brief Adds a state to the simulation results.
* @param[in] state
*/
void addState(T time, std::unordered_map<int, T> pressures, std::unordered_map<int, T> flowRates, std::unordered_map<int, sim::DropletPosition<T>> dropletPositions);

/**
* @brief Get the simulated pressures at the nodes.
* @return Vector of pressure values
*/
std::unordered_map<int, T>& getFinalPressures() const;

/**
* @brief Get the simulated flowrates in the channels.
* @return Vector of flowrate values
*/
std::unordered_map<int, T>& getFinalFlowRates() const;

/**
* @brief Get the simulated flowrates in the channels.
* @return Vector of flowrate values
*/
std::unordered_map<int, T>& getFinalDropletPositions() const;

/**
* @brief Get the simulated states that were stored during simulation.
* @return Vector of states
*/
std::vector<std::unique_ptr<State<T>>>& getStates() const;
};

} // namespace results
40 changes: 40 additions & 0 deletions src/result/Results.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "Results.h"

#include <iostream>
#include <ostream>
#include <string>
#include <utility>
#include <vector>

#include <nlohmann/json.hpp>

namespace result {

template<typename T>
State<T>::State(int id, T time) : id(id), time(time) { }

template<typename T>
T State<T>::getPressure(int nodeId) const {
return pressures.at(nodeId);
}

template<typename T>
T State<T>::getPressureDrop(int node0Id, int node1Id) const {
return getPressure(node0Id) - getPressure(node1Id);
}

template<typename T>
T State<T>::getFlowRate(int channelId) const {
return flowRates.at(channelId);
}


std::unordered_map<int, double> SimulationResult::getPressures() const {
return states[0].pressures;
}

std::unordered_map<int, double> SimulationResult::getFlowRates() const {
return states[0].flowRates;
}

} // namespace droplet
4 changes: 4 additions & 0 deletions src/simulation/CFDSim.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @file CFDSim.h
*/

#pragma once

#include <olb2D.h>
Expand Down
8 changes: 7 additions & 1 deletion src/simulation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
set(SOURCE_LIST
CFDSim.hh
Droplet.hh
Fluid.hh
Injection.hh
ResistanceModels.hh
Simulation.hh
)

set(HEADER_LIST
CFDSim.h
Droplet.h
Fluid.h
Injection.h
ResistanceModels.h
Simulation.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)
target_link_libraries(${TARGET_NAME} PUBLIC lbmLib)

add_subdirectory(events)
Loading

0 comments on commit d47d706

Please sign in to comment.