From da0e7fe6a30f296acdd8c4a833ef91b8bd96a94c Mon Sep 17 00:00:00 2001 From: hiwis <hiwis@irmb.tu-bs.de> Date: Fri, 22 Feb 2019 13:33:26 +0100 Subject: [PATCH] adds the Traffic code to the project --- CMakeLists.txt | 37 +- CMakeMacros/Environment/MachineFiles/GAMLING | 10 + src/Traffic/ConcentrationByPosition.cpp | 34 ++ src/Traffic/ConcentrationByPosition.h | 21 ++ src/Traffic/ConcentrationOutwriter.h | 14 + src/Traffic/Junction.h | 26 ++ src/Traffic/JunctionData.h | 13 + src/Traffic/JunctionSimple.cpp | 180 ++++++++++ src/Traffic/JunctionSimple.h | 62 ++++ src/Traffic/OneWayRoad.cpp | 326 ++++++++++++++++++ src/Traffic/OneWayRoad.h | 92 +++++ src/Traffic/OneWayRoadSSJ.cpp | 273 +++++++++++++++ src/Traffic/OneWayRoadSSJ.h | 40 +++ src/Traffic/RandomHelper.cpp | 10 + src/Traffic/RandomHelper.h | 12 + src/Traffic/RoadMaker.cpp | 281 +++++++++++++++ src/Traffic/RoadMaker.h | 48 +++ src/Traffic/RoadNetworkData.h | 34 ++ src/Traffic/Sink.cpp | 0 src/Traffic/Sink.h | 13 + src/Traffic/SinkData.h | 11 + src/Traffic/SinkSimple.cpp | 41 +++ src/Traffic/SinkSimple.h | 31 ++ src/Traffic/Source.cpp | 1 + src/Traffic/Source.h | 13 + src/Traffic/SourceData.h | 12 + src/Traffic/SourceTerm.cpp | 58 ++++ src/Traffic/SourceTerm.h | 36 ++ src/Traffic/VectorHelper.cpp | 71 ++++ src/Traffic/VectorHelper.h | 23 ++ src/Traffic/invalidInput_error.cpp | 11 + src/Traffic/invalidInput_error.h | 16 + src/Traffic/package.include | 0 src/VirtualFluids_GPU/LBM/Simulation.cpp | 20 +- targets/apps/LBM/Basel/main.cpp | 20 +- .../LBM/Basel/resources/ExampleStreets.txt | 11 + .../LBM/TrafficTest/3rdPartyLinking.cmake | 13 + targets/apps/LBM/TrafficTest/CMakeLists.txt | 15 + .../apps/LBM/TrafficTest/CMakePackage.cmake | 9 + targets/apps/LBM/TrafficTest/TrafficTest.cpp | 228 ++++++++++++ targets/apps/LBM/TrafficTest/package.include | 0 .../TrafficTest/resources/ExampleStreets.txt | 11 + .../LBM/TrafficTest/resources/FlatGround.stl | 86 +++++ targets/libs/Traffic/3rdPartyLinking.cmake | 10 + targets/libs/Traffic/CMakeLists.txt | 16 + targets/libs/Traffic/CMakePackage.cmake | 8 + 46 files changed, 2266 insertions(+), 31 deletions(-) create mode 100644 CMakeMacros/Environment/MachineFiles/GAMLING create mode 100644 src/Traffic/ConcentrationByPosition.cpp create mode 100644 src/Traffic/ConcentrationByPosition.h create mode 100644 src/Traffic/ConcentrationOutwriter.h create mode 100644 src/Traffic/Junction.h create mode 100644 src/Traffic/JunctionData.h create mode 100644 src/Traffic/JunctionSimple.cpp create mode 100644 src/Traffic/JunctionSimple.h create mode 100644 src/Traffic/OneWayRoad.cpp create mode 100644 src/Traffic/OneWayRoad.h create mode 100644 src/Traffic/OneWayRoadSSJ.cpp create mode 100644 src/Traffic/OneWayRoadSSJ.h create mode 100644 src/Traffic/RandomHelper.cpp create mode 100644 src/Traffic/RandomHelper.h create mode 100644 src/Traffic/RoadMaker.cpp create mode 100644 src/Traffic/RoadMaker.h create mode 100644 src/Traffic/RoadNetworkData.h create mode 100644 src/Traffic/Sink.cpp create mode 100644 src/Traffic/Sink.h create mode 100644 src/Traffic/SinkData.h create mode 100644 src/Traffic/SinkSimple.cpp create mode 100644 src/Traffic/SinkSimple.h create mode 100644 src/Traffic/Source.cpp create mode 100644 src/Traffic/Source.h create mode 100644 src/Traffic/SourceData.h create mode 100644 src/Traffic/SourceTerm.cpp create mode 100644 src/Traffic/SourceTerm.h create mode 100644 src/Traffic/VectorHelper.cpp create mode 100644 src/Traffic/VectorHelper.h create mode 100644 src/Traffic/invalidInput_error.cpp create mode 100644 src/Traffic/invalidInput_error.h create mode 100644 src/Traffic/package.include create mode 100644 targets/apps/LBM/Basel/resources/ExampleStreets.txt create mode 100644 targets/apps/LBM/TrafficTest/3rdPartyLinking.cmake create mode 100644 targets/apps/LBM/TrafficTest/CMakeLists.txt create mode 100644 targets/apps/LBM/TrafficTest/CMakePackage.cmake create mode 100644 targets/apps/LBM/TrafficTest/TrafficTest.cpp create mode 100644 targets/apps/LBM/TrafficTest/package.include create mode 100644 targets/apps/LBM/TrafficTest/resources/ExampleStreets.txt create mode 100644 targets/apps/LBM/TrafficTest/resources/FlatGround.stl create mode 100644 targets/libs/Traffic/3rdPartyLinking.cmake create mode 100644 targets/libs/Traffic/CMakeLists.txt create mode 100644 targets/libs/Traffic/CMakePackage.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index b948dfa50..6c910b146 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,7 +78,7 @@ ENDIF(MSVC) ############################################################# option(BUILD_SHARED_LIBS "Build shared libraries" ON ) option(HULC.BUILD_VF_GPU "Build VirtualFluids GPU" ON ) -option(HULC.BUILD_VF_GKS "Build VirtualFluids GKS" OFF ) +option(HULC.BUILD_VF_GKS "Build VirtualFluids GKS" OFF) option(HULC.BUILD_JSONCPP "Builds json cpp " ON ) option(HULC.BUILD_NUMERIC_TESTS "Build numeric tests" OFF) @@ -100,6 +100,10 @@ ELSE(MSVC) SET( CMAKE_CXX_FLAGS "-include ${CMAKE_BINARY_DIR}/VirtualFluidsDefinitions.h ${CMAKE_CXX_FLAGS}" ) ENDIF(MSVC) +set(CMAKE_CUDA_FLAGS " -arch=sm_60" CACHE STRING "" FORCE) + +set(CMAKE_CUDA_FLAGS_DEBUG " -G" CACHE STRING "" FORCE) + ########################################################################################################################## ### Subdirectories ### @@ -113,24 +117,17 @@ add_subdirectory(targets/libs/VirtualFluidsBasics) add_subdirectory(targets/libs/Core) add_subdirectory(targets/libs/GridGenerator) +add_subdirectory(3rdParty/metis/metis-5.1.0) ############################################################# ### Virtual Fluids GPU ### ############################################################# IF (HULC.BUILD_VF_GPU) - add_subdirectory(3rdParty/metis/metis-5.1.0) - add_subdirectory(targets/libs/VirtualFluids_GPU) add_subdirectory(targets/apps/LBM/lbmTest) - add_subdirectory(targets/apps/LBM/metisTest) - - add_subdirectory(targets/apps/LBM/TGV_3D) - - add_subdirectory(targets/apps/LBM/streetTest) - add_subdirectory(targets/apps/LBM/Basel) ELSE() MESSAGE( STATUS "exclude Virtual Fluids GPU." ) @@ -143,10 +140,20 @@ ENDIF() IF (HULC.BUILD_VF_GKS) add_subdirectory(targets/libs/GksMeshAdapter) add_subdirectory(targets/libs/GksVtkAdapter) - + add_subdirectory(targets/libs/GksGpu) - add_subdirectory(targets/apps/GKS/gksTest) + #add_subdirectory(targets/apps/GKS/gksTest) + #add_subdirectory(targets/apps/GKS/ChannelFlow) + #add_subdirectory(targets/apps/GKS/ShearWave) + + add_subdirectory(targets/apps/GKS/TaylorGreen3D) + add_subdirectory(targets/apps/GKS/DrivenCavity3D) + add_subdirectory(targets/apps/GKS/ThermalCavity) + + add_subdirectory(targets/apps/GKS/ThermalCavityMultiGPU) + + add_subdirectory(targets/apps/GKS/SalinasVazquez) ELSE() MESSAGE( STATUS "exclude Virtual Fluids GKS." ) ENDIF() @@ -169,3 +176,11 @@ if(HULC.BUILD_NUMERIC_TESTS) add_subdirectory(3rdParty/fftw/fftw-3.3.7) add_subdirectory(targets/tests/TestingHULC) endif() + +############################################################# +### Annas Traffis Sim ### +############################################################# + +add_subdirectory(targets/libs/Traffic) + +add_subdirectory(targets/apps/LBM/TrafficTest) \ No newline at end of file diff --git a/CMakeMacros/Environment/MachineFiles/GAMLING b/CMakeMacros/Environment/MachineFiles/GAMLING new file mode 100644 index 000000000..b3a52ae0f --- /dev/null +++ b/CMakeMacros/Environment/MachineFiles/GAMLING @@ -0,0 +1,10 @@ +#Don't change: +SET(METIS_ROOT ${CMAKE_SOURCE_DIR}/3rdParty/metis/metis-5.1.0 CACHE PATH "METIS ROOT") +SET(GMOCK_ROOT ${CMAKE_SOURCE_DIR}/3rdParty/googletest CACHE PATH "GMOCK ROOT") +SET(JSONCPP_ROOT ${CMAKE_SOURCE_DIR}/3rdParty/jsoncpp CACHE PATH "JSONCPP ROOT") +SET(FFTW_ROOT ${CMAKE_SOURCE_DIR}/3rdParty/fftw/fftw-3.3.7 CACHE PATH "JSONCPP ROOT") + + +#SET TO CORRECT PATH: +SET(BOOST_ROOT "C:\\lib\\boost_1_65_1" CACHE PATH "BOOST_ROOT") +SET(BOOST_LIBRARYDIR "C:\\lib\\boost_1_65_1\\stageMSVC64\\lib" CACHE PATH "BOOST_LIBRARYDIR") diff --git a/src/Traffic/ConcentrationByPosition.cpp b/src/Traffic/ConcentrationByPosition.cpp new file mode 100644 index 000000000..ea4d136b1 --- /dev/null +++ b/src/Traffic/ConcentrationByPosition.cpp @@ -0,0 +1,34 @@ +#include "ConcentrationByPosition.h" + + + +ConcentrationByPosition::ConcentrationByPosition(unsigned int roadlength) +{ + concentration.resize(roadlength); +} + + +ConcentrationByPosition::~ConcentrationByPosition() +{ +} + +void ConcentrationByPosition::writeToArray(const std::vector<int>& currentCarDistribution) +{ + for (unsigned int i = 0; i < currentCarDistribution.size(); i++) { + if (currentCarDistribution[i] >= 0) + concentration[i] = 1.0; + else + concentration[i] = 0.0; + } + + //dispConcentration(); +} + +void ConcentrationByPosition::dispConcentration() +{ + for (auto cell : concentration) { + std::cout << std::setw(4) << cell; + } + std::cout << std::endl; +} + diff --git a/src/Traffic/ConcentrationByPosition.h b/src/Traffic/ConcentrationByPosition.h new file mode 100644 index 000000000..01b80aa73 --- /dev/null +++ b/src/Traffic/ConcentrationByPosition.h @@ -0,0 +1,21 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include "ConcentrationOutwriter.h" + +class VF_PUBLIC ConcentrationByPosition: + public ConcentrationOutwriter +{ +public: + ConcentrationByPosition(unsigned int roadlength); + ~ConcentrationByPosition(); + + virtual void writeToArray(const std::vector<int> & currentCarDistribution); + +private: + void dispConcentration(); + +private: + std::vector<double> concentration; +}; + diff --git a/src/Traffic/ConcentrationOutwriter.h b/src/Traffic/ConcentrationOutwriter.h new file mode 100644 index 000000000..b531d6e32 --- /dev/null +++ b/src/Traffic/ConcentrationOutwriter.h @@ -0,0 +1,14 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <vector> +#include <iostream> + +#include <iomanip> //formatting output streams + +class VF_PUBLIC ConcentrationOutwriter +{ +public: + virtual void writeToArray(const std::vector<int> & currentCarDistribution) = 0; +}; + diff --git a/src/Traffic/Junction.h b/src/Traffic/Junction.h new file mode 100644 index 000000000..a41175e6f --- /dev/null +++ b/src/Traffic/Junction.h @@ -0,0 +1,26 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <vector> + +#include "JunctionData.h" + +class OneWayRoadSSJ; + +class VF_PUBLIC Junction +{ +public: + virtual ~Junction() {}; + + virtual void checkOutCellIndices(unsigned int roadLength) = 0; + + virtual bool acceptsCar(unsigned int cellIndex) = 0; //determines if a car can enter the junction + virtual void registerCar(unsigned int cellIndex, unsigned int numberOfCellsAlreadyMoved, unsigned int speed) = 0; //registers all cars entering the junction + virtual void calculateTimeStep(OneWayRoadSSJ& road) = 0; + virtual void updateJunction() = 0; + + virtual const std::vector<unsigned int>& getInCellIndices() const = 0; + + virtual void dispJunction(const unsigned int index, unsigned int roadLength) const = 0; + virtual const unsigned int getNumCarsOnJunction() const = 0; +}; \ No newline at end of file diff --git a/src/Traffic/JunctionData.h b/src/Traffic/JunctionData.h new file mode 100644 index 000000000..e7853947f --- /dev/null +++ b/src/Traffic/JunctionData.h @@ -0,0 +1,13 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <vector> +#include <memory> + +struct VF_PUBLIC JunctionData +{ +public: + std::vector<unsigned int> inCellIndices; + std::vector<unsigned int> outCellIndices; +}; + diff --git a/src/Traffic/JunctionSimple.cpp b/src/Traffic/JunctionSimple.cpp new file mode 100644 index 000000000..eb8b4c3a7 --- /dev/null +++ b/src/Traffic/JunctionSimple.cpp @@ -0,0 +1,180 @@ +#include "JunctionSimple.h" + + + +JunctionSimple::JunctionSimple(const vector<unsigned int> &inCellIndices, const vector<unsigned int> &outCellIndices) : + engine{ random_device{}() } +{ + data.inCellIndices = inCellIndices; + data.outCellIndices = outCellIndices; + + unsigned int inRoads = inCellIndices.size(); + + carCanEnter.resize(inRoads); + fill(carCanEnter.begin(), carCanEnter.end(), true); + + carsOnJunction.resize(inRoads); + fill(carsOnJunction.begin(), carsOnJunction.end(), -1); + + alreadyMoved.resize(inRoads); + fill(alreadyMoved.begin(), alreadyMoved.end(), 0); +} + + +JunctionSimple::~JunctionSimple() +{ +} + + +bool JunctionSimple::acceptsCar(unsigned int cellIndex) +{ + return carCanEnter[getInCellsVectorIndex(cellIndex)]; +} + + +void JunctionSimple::registerCar(unsigned int cellIndex, unsigned int numberOfCellsAlreadyMoved, unsigned int speed) +{ + unsigned int index = getInCellsVectorIndex(cellIndex); + carsOnJunction[index] = speed; + carCanEnter[index] = false; + alreadyMoved[index] = numberOfCellsAlreadyMoved; +} + + +unsigned int JunctionSimple::getInCellsVectorIndex(unsigned int cellIndex) +{ + try { + auto it = find(data.inCellIndices.begin(), data.inCellIndices.end(), cellIndex); + + if (it != data.inCellIndices.end()) + { + return distance(data.inCellIndices.begin(), it); + } + + throw runtime_error("The passed cell is not an incoming cell to this junction."); + } + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + } +} + + +void JunctionSimple::updateJunction() +{ + freeOutCells = data.outCellIndices; +} + + +void JunctionSimple::calculateTimeStep(OneWayRoadSSJ& road) +{ + index = 0; + for (int carSpeed : carsOnJunction) { + + if (carSpeed >= 0) { //check if there is a car on the junction + + if (carSpeed == 0) + cerr << "speed on junction was 0" << endl; + else + applyRules(carSpeed, index, road); + alreadyMoved[index] = 0; + + } + ++ index; + } +} + + +void JunctionSimple::applyRules(int & carSpeed,const int & index, OneWayRoadSSJ& road) +{ + remainingDistance = static_cast<unsigned int>(carSpeed) - alreadyMoved[index]; + + if (remainingDistance > 0 && freeOutCells.size() > 0) { + outCell = chooseOutCell(); + breakCar(outCell, carSpeed, remainingDistance, road); + + if (remainingDistance > 0) + moveCar(carSpeed, index, road); + else + carsOnJunction[index] = 1; + } + else + carsOnJunction[index] = 1; +} + + +void JunctionSimple::breakCar(unsigned int outCellIndex, int &speed, unsigned int &remainingDistance, OneWayRoadSSJ& road) +{ + gap = road.getGapAfterOutCell(outCellIndex, remainingDistance); + if (gap < remainingDistance) { + speed = speed - remainingDistance + gap; + remainingDistance = gap; + } + +} + + +void JunctionSimple::moveCar(int & carSpeed, const int & index, OneWayRoadSSJ& road) +{ + road.moveJunctionCar(outCell, remainingDistance, carSpeed); + carsOnJunction[index] = -1; + carCanEnter[index] = true; +} + + +unsigned int JunctionSimple::chooseOutCell() +{ + uniform_int_distribution<unsigned int> distFloat(0, freeOutCells.size() - 1); + random = distFloat(engine); + + outCell = freeOutCells[random]; + freeOutCells.erase(freeOutCells.begin() + random); + return outCell; +} + + + + + +const vector<unsigned int>& JunctionSimple::getInCellIndices() const +{ + return data.inCellIndices; +} + +void JunctionSimple::dispJunction(const unsigned int index, unsigned int roadLength) const +{ + if (find(data.inCellIndices.begin(), data.inCellIndices.end(), (roadLength - index - 1)) != data.inCellIndices.end()) { + cout << setw(4) << "in"; + } + else if (find(data.outCellIndices.begin(), data.outCellIndices.end(), (roadLength - index - 1)) != data.outCellIndices.end()) { + cout << setw(4) << "out"; + } + else { + cout << setw(4) << " "; + } +} + +const unsigned int JunctionSimple::getNumCarsOnJunction() const +{ + unsigned int num = 0; + for (auto car : carsOnJunction) { + if (car >= 0) + ++num; + } + return num; +} + + +void JunctionSimple::checkOutCellIndices(unsigned int roadLength) +{ + try { + for (unsigned int cell : data.outCellIndices) + if (cell >= roadLength) throw invalidInput_error("The indices of incoming cells to a junction are greater than the roadLength."); + } + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + } +} \ No newline at end of file diff --git a/src/Traffic/JunctionSimple.h b/src/Traffic/JunctionSimple.h new file mode 100644 index 000000000..242724463 --- /dev/null +++ b/src/Traffic/JunctionSimple.h @@ -0,0 +1,62 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <vector> +#include <iostream> +#include <random> + +#include "invalidInput_error.h" +#include "VectorHelper.h" +#include "Junction.h" +#include "OneWayRoadSSJ.h" + +using namespace std; + +class VF_PUBLIC JunctionSimple : + public Junction +{ + +private: + JunctionData data; + + vector<unsigned int> freeOutCells; + + vector<bool> carCanEnter; + vector<int> carsOnJunction; + vector<unsigned int> alreadyMoved; + + mt19937 engine; + +public: + JunctionSimple(const vector<unsigned int> &inCellIndices, const vector<unsigned int> &outCellIndices); + ~JunctionSimple(); + + virtual bool acceptsCar(unsigned int cellIndex); //determines if a car can enter the junction + virtual void registerCar(unsigned int cellIndex, unsigned int numberOfCellsAlreadyMoved, unsigned int speed); //registers all cars entering the junction + virtual void calculateTimeStep(OneWayRoadSSJ& road); + virtual void updateJunction(); + + virtual const vector<unsigned int>& getInCellIndices() const; + + virtual void dispJunction(const unsigned int index, unsigned int roadLength) const; + virtual const unsigned int getNumCarsOnJunction() const; + + virtual void checkOutCellIndices(unsigned int roadLength); + +private: + unsigned int getInCellsVectorIndex(unsigned int cellIndex); + + void applyRules(int &carSpeed,const int &index, OneWayRoadSSJ& road); + void breakCar(unsigned int outCellIndex, int &speed, unsigned int &remainingDistance, OneWayRoadSSJ& road); + void moveCar(int & carSpeed, const int & index, OneWayRoadSSJ& road); + unsigned int chooseOutCell(); + +private: + //variables for temporaray calculations + unsigned int remainingDistance; + unsigned int outCell; + unsigned int random; + unsigned int gap; + int index; +}; + diff --git a/src/Traffic/OneWayRoad.cpp b/src/Traffic/OneWayRoad.cpp new file mode 100644 index 000000000..cc9333c5e --- /dev/null +++ b/src/Traffic/OneWayRoad.cpp @@ -0,0 +1,326 @@ +#include "OneWayRoad.h" +#include <stdexcept> + +OneWayRoad::OneWayRoad() +{ +} + + +OneWayRoad::OneWayRoad(shared_ptr<RoadNetworkData> road, const float dawdlePossibility) +{ + this->road = road; + + pcurrent = &this->road->current; + pnext = &(this->road->next); + + initDawdle(dawdlePossibility); + initResults(); +} + + +OneWayRoad::~OneWayRoad() +{ + pcurrent = NULL; + pnext = NULL; + pdummy = NULL; +} + + +void OneWayRoad::initResults() +{ + results.resize(road->roadLength, vector<int>(1)); + VectorHelper::fillVector(results, -10); +} + + +void OneWayRoad::initDawdle(const float dawdlePossibility) +{ + try { + if (dawdlePossibility >= 0 && dawdlePossibility < 1) { + this->dawdlePossibility = dawdlePossibility; + } + else { + throw invalidInput_error("The dawdlePossibility should be between 0 and 1."); + } + } + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + } +} + + +void OneWayRoad::setSlowToStart(const float slowStartPossibility) +{ + try { + if (slowStartPossibility >= 0 && slowStartPossibility < 1) { + + this->slowStartPossibility = slowStartPossibility; + useSlowToStart=true; + + } + else { + throw invalidInput_error("The slowStartPossibility should be between 0 and 1."); + } + } + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + } +} + + +void OneWayRoad::setConcentrationOutwriter(unique_ptr<ConcentrationOutwriter> writer) +{ + this->concWriter = move(writer); +} + + +const unsigned int OneWayRoad::getNumberOfCars() const +{ + return findCarIndicesInCurrent().size(); +} + +const int OneWayRoad::getSpeedAtPosition(unsigned int pos) const +{ + return (*pcurrent)[pos]; +} + +unsigned int OneWayRoad::getRoadLength() const +{ + return road->roadLength; +} + +unsigned int OneWayRoad::getMaxVelocity() const +{ + return road->maxVelocity; +} + +void OneWayRoad::calculateResults(int timeSteps) +{ + this->timeSteps = timeSteps; + + initResultsForCalculation(); + + putCurrentIntoResults(0); + + //iterate through timesteps + for (int step = 1; step < timeSteps + 1; step++) { + currentStep += 1; + calculateTimestep(step); + } +} + +void OneWayRoad::initResultsForCalculation() +{ + for (unsigned int i = 0; i < road->roadLength; i++) { + results[i].resize(timeSteps + 1); + } + VectorHelper::fillVector(results, -1); +} + +void OneWayRoad::calculateTimestep(unsigned int step) +{ + dispCurrentRoad(); + writeConcentration(); + + VectorHelper::fillVector(*pnext, -1); + vector<unsigned int> cars = findCarIndicesInCurrent(); + + for (auto &car : cars) { + applyRules(car); + } + + switchCurrentNext(); + putCurrentIntoResults(step); +} + +void OneWayRoad::switchCurrentNext() +{ + pdummy = pcurrent; + pcurrent = pnext; + pnext = pdummy; +} + +void OneWayRoad::applyRules(unsigned int carIndex) +{ + unsigned int speed = (*pcurrent)[carIndex]; + accelerateCar(speed); + breakCar(carIndex, speed); + dawdleCar(carIndex, speed); + moveCar(carIndex, speed); +} + +void OneWayRoad::accelerateCar(unsigned int & speed) +{ + if (speed < road->maxVelocity) { + speed += 1; + } +} + +void OneWayRoad::breakCar(unsigned int carIndex, unsigned int &speed) +{ + int neighbor = road->neighbors[carIndex]; + gap = getGapAfterCar(carIndex, speed, neighbor); + if (speed > gap) { + speed = gap; + } +} + +void OneWayRoad::dawdleCar(unsigned int carIndex, unsigned int & speed) +{ + randomNumber = distFloat(engine); + + //Barlovic / SlowToStart + if ( (*pcurrent)[carIndex] == 0 && useSlowToStart == true) { + if (randomNumber < slowStartPossibility) { + speed=0; + } + return; + } + + //Standard NaSch + if (randomNumber < dawdlePossibility) { + if ((speed) > 0) { + speed -= 1; + } + else { + speed = 0; + } + } +} + +void OneWayRoad::moveCar(unsigned int carIndex, unsigned int speed) +{ + int neighbor = road->neighbors[carIndex]; + + if (speed == 0) { + (*pnext)[carIndex] = 0; + } + else { + for (unsigned int i = 2; i <= speed; i++) { + neighbor = road->neighbors[neighbor]; + } + (*pnext)[neighbor] = speed; + } +} + + +unsigned int OneWayRoad::getGapAfterCar(unsigned int carIndex, unsigned int speed, int neighbor) +{ + for (unsigned int i = 0; i < (speed + road->safetyDistance); i++) { + if ((*pcurrent)[neighbor] > -1) { + return adjustGapConsideringSafetyDistance(i); + } + neighbor = road->neighbors[neighbor]; + } + return speed; +} + + +unsigned int OneWayRoad::adjustGapConsideringSafetyDistance(unsigned int speed) +{ + if (speed <= road->safetyDistance) + return 0; + else + return speed - road->safetyDistance; +} + + +vector<unsigned int> OneWayRoad::findCarIndicesInCurrent() const +{ + vector<unsigned int> cars; + for (unsigned int i = 0; i < road->roadLength; i++) { + if ((*pcurrent)[i] > -1) { + cars.push_back(i); + } + } + return cars; +} + +void OneWayRoad::putCurrentIntoResults(unsigned int step) +{ + for (unsigned int i = 0; i < road->roadLength; i++) { + results[i][step] = (*pcurrent)[i]; + } +} + +void OneWayRoad::writeConcentration() +{ + if (concWriter != nullptr) { + concWriter->writeToArray(*pcurrent); + } +} + + +void OneWayRoad::writeResultsToFile() const +{ + try { + + + fstream outFile("results.txt", fstream::out | fstream::trunc); + if (outFile.is_open()) + { + for (unsigned int i = 0; i < results.size(); i++) { + for (unsigned int j = 0; j < results[i].size() - 1; j++) + outFile << results[i][j] << " "; + + outFile << results[i][results[i].size() - 1]; + outFile << endl; + } + cout << "Finished writing data to file" << endl; + } + + + else + throw runtime_error("Couldn't open file"); + + } + catch (const exception& e) { + cerr << e.what() << endl; + } + catch (...) { + cerr << "unknown exception while writing to file" << endl; + } +} + + + +void OneWayRoad::dispCurrentRoad() const +{ + cout << "current: ( step: " << currentStep << " )" << endl; + VectorHelper::dispVectorColour(*pcurrent); +} + +void OneWayRoad::dispResults() +{ + cout << "results:" << endl; + + visualizeSafetyDistance(); + reverse(results.begin(), results.end()); + VectorHelper::dispVectorColour(results); +} + +void OneWayRoad::visualizeSafetyDistance() +{ + if (road->safetyDistance != 0) { + for (unsigned int step = 0; step <= timeSteps; step++) { + for (unsigned int i = 0; i < road->roadLength; i++) { + if (results[i][step] > -1) { + int neighbor = road->neighbors[i]; + for (unsigned int j = 1; j <= road->safetyDistance; j++) { + if (results[neighbor][step] > -1) { + cerr << "safetyDistance was violated: timestep: " << step << "\t carIndex: " << i << endl; + break; + } + else + results[neighbor][step] = -5; + neighbor = road->neighbors[neighbor]; + } + } + } + } + } +} diff --git a/src/Traffic/OneWayRoad.h b/src/Traffic/OneWayRoad.h new file mode 100644 index 000000000..fc2944518 --- /dev/null +++ b/src/Traffic/OneWayRoad.h @@ -0,0 +1,92 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <iostream> +#include <fstream> +#include <vector> +#include <random> +#include <memory> +#include <iomanip> //formatting output streams +#include <windows.h> //for colourful console output + +#include "invalidInput_error.h" +#include "VectorHelper.h" +#include "RandomHelper.h" +#include "RoadNetworkData.h" +#include "ConcentrationOutwriter.h" + +using namespace std; + +class VF_PUBLIC OneWayRoad //Periodic BC +{ +protected: + shared_ptr<RoadNetworkData> road; + + float dawdlePossibility; + unsigned int timeSteps; + unsigned int currentStep = 0; //only needed for dispCurrent() + + bool useSlowToStart = false; + float slowStartPossibility; + + mt19937 engine = RandomHelper::make_engine(); + uniform_real_distribution<float> distFloat{ 0.0, 1.0 }; + + vector<int> *pcurrent; + vector<int> *pnext; + vector<int> *pdummy; + + vector<vector<int> > results; //saves the results of the calculation; x-axis = timesteps, y axis = positions + + unique_ptr<ConcentrationOutwriter> concWriter = nullptr; + +public: + OneWayRoad(shared_ptr<RoadNetworkData> road, const float dawdlePossibility); + OneWayRoad(); + OneWayRoad(const OneWayRoad&) = delete; + virtual ~OneWayRoad(); + + void setSlowToStart(const float slowStartPossibility); + void setConcentrationOutwriter(unique_ptr<ConcentrationOutwriter> writer); + + virtual void calculateResults(int timeSteps); + + virtual const unsigned int getNumberOfCars() const; //only use for testing + const int getSpeedAtPosition(unsigned int pos) const; //only use for testing + unsigned int getRoadLength() const; + unsigned int getMaxVelocity() const; + + void dispCurrentRoad() const; + virtual void dispResults(); + void writeResultsToFile() const; + + +protected: + void initDawdle(const float dawdlePossibility); + void initResults(); + + void initResultsForCalculation(); + + virtual void calculateTimestep(unsigned int step); + void putCurrentIntoResults(unsigned int step); + void writeConcentration(); + void switchCurrentNext(); + + vector<unsigned int> findCarIndicesInCurrent() const; + virtual unsigned int getGapAfterCar(unsigned int carIndex, unsigned int speed, int neighbor); + unsigned int adjustGapConsideringSafetyDistance(unsigned int speed); + + void applyRules(unsigned int carIndex); + void accelerateCar(unsigned int &speed); + void breakCar(unsigned int carIndex, unsigned int &speed); + void dawdleCar(unsigned int carIndex, unsigned int &speed); + virtual void moveCar(unsigned int carIndex, unsigned int speed); + + virtual void visualizeSafetyDistance(); + +protected: + //temporary varables for calculation + unsigned int gap; + double randomNumber; +}; + diff --git a/src/Traffic/OneWayRoadSSJ.cpp b/src/Traffic/OneWayRoadSSJ.cpp new file mode 100644 index 000000000..164ce1da1 --- /dev/null +++ b/src/Traffic/OneWayRoadSSJ.cpp @@ -0,0 +1,273 @@ +#include "OneWayRoadSSJ.h" + +OneWayRoadSSJ::OneWayRoadSSJ(shared_ptr<RoadNetworkData> road, const float dawdlePossibility) +{ + this->road = road; + + pcurrent = &this->road->current; + pnext = &(this->road->next); + + initDawdle(dawdlePossibility); + initResults(); +} + +OneWayRoadSSJ::OneWayRoadSSJ() +{ +} + +OneWayRoadSSJ::~OneWayRoadSSJ() +{ +} + + +void OneWayRoadSSJ::calculateTimestep(unsigned int step) +{ +// dispCurrentRoad(); + writeConcentration(); + + VectorHelper::fillVector(*pnext, -1); + vector<unsigned int> cars = findCarIndicesInCurrent(); + + for (auto &car : cars) + applyRules(car); + + for (auto &junction : road->junctions) { + junction->updateJunction(); + junction->calculateTimeStep(*this); + } + + calculateSourceStep(); + + switchCurrentNext(); + + putCurrentIntoResults(step); +} + + +void OneWayRoadSSJ::calculateSourceStep() +{ + unsigned int sourceIndex; + unsigned int maxSpeed; + for (auto &source : road->sources) { + sourceIndex = source->getIndex(); + maxSpeed = getGapAfterOutCell(sourceIndex, road->maxVelocity); + if (maxSpeed > 0) + (*pnext)[sourceIndex] = source->getSourceCar(); + } +} + + +void OneWayRoadSSJ::moveCar(unsigned int carIndex, unsigned int speed) +{ + if (speed == 0) { + (*pnext)[carIndex] = 0; + return; + } + + int neighbor = road->neighbors[carIndex]; + unsigned int currentCell = carIndex; + + unsigned int numberOfCellsMoved = iterateNeighborsInMove(currentCell, speed, neighbor); + + if (neighbor <= -1000 && neighbor > -2000) { + getJunctionFromNeighbor(neighbor)->registerCar(currentCell, numberOfCellsMoved, speed); + return; + } + + if (neighbor >= 0) + (*pnext)[neighbor] = speed; +} + +unsigned int OneWayRoadSSJ::iterateNeighborsInMove(unsigned int ¤tCell, unsigned int speed, int &neighbor) +{ + unsigned int numberOfCellsMoved = 1; + + for (unsigned int i = 2; i <= speed; i++) { + if (neighbor >= 0) { + currentCell = neighbor; + numberOfCellsMoved += 1; + neighbor = road->neighbors[neighbor]; + } + else + break; + } + + return numberOfCellsMoved; +} + +unsigned int OneWayRoadSSJ::getGapAfterCar(unsigned int carIndex, unsigned int speed, int neighbor) +{ + unsigned int currentCell = carIndex; + + for (unsigned int i = 0; i < (speed + road->safetyDistance); i++) { + + if (neighbor <= -2000) + return getGapToSink(neighbor, i, speed); + if (neighbor <= -1000) + return getGapToJunction(neighbor, i, speed, currentCell); + + //car in Cell + if ((*pcurrent)[neighbor] > -1) + return adjustGapConsideringSafetyDistance(i); + + //empty cell -> get next neighbor, update currentCell + currentCell = neighbor; + neighbor = road->neighbors[neighbor]; + } + return speed; +} + + + +unsigned int OneWayRoadSSJ::getGapToJunction(int neighbor, unsigned int i, unsigned int speed, unsigned int currentCell) +{ + if (getJunctionFromNeighbor(neighbor)->acceptsCar(currentCell) && i <= speed) + return speed; + return i; +} + +unsigned int OneWayRoadSSJ::getGapToSink(int neighbor, unsigned int i, unsigned int speed) +{ + if (getSinkFromNeighbor(neighbor)->carCanEnter()) + return speed; + return adjustGapConsideringSafetyDistance(i); +} + + +unsigned int OneWayRoadSSJ::getGapAfterOutCell(unsigned int outCellIndex, unsigned int speed) +{ + if ((*pcurrent)[outCellIndex] > -1) + return 0; + gap=getGapAfterCar(outCellIndex, speed, outCellIndex); + return gap; +} + + +void OneWayRoadSSJ::moveJunctionCar(unsigned int outCellIndex, unsigned int remainingDistance, unsigned int speed) +{ + if (remainingDistance == 1) { + (*pnext)[outCellIndex] = speed; + return; + } + + int neighbor = outCellIndex; + unsigned int currentCell = outCellIndex; + + unsigned int numberOfCellsMoved = iterateNeighborsInMove(currentCell, remainingDistance, neighbor); + + if (neighbor <= -1000 && neighbor > -2000) { + (*pnext)[currentCell] = speed; // car can't enter more than one junction in one timestep + return; + } + + if (neighbor >= 0) + (*pnext)[neighbor] = speed; +} + + +shared_ptr<Junction>& OneWayRoadSSJ::getJunctionFromNeighbor(int neighbor) +{ + //calculate index in junctions vector for neighbor (-1000 to -1999) + return road->junctions[((neighbor + 1000)*-1)]; +} + +shared_ptr<Sink>& OneWayRoadSSJ::getSinkFromNeighbor(int neighbor) +{ + //calculate index in junctions vector for neighbor (-2000 to -2999) + int index = ((neighbor + 2000)*-1); + return road->sinks[index]; +} + + +const unsigned int OneWayRoadSSJ::getNumberOfCars() const +{ + unsigned int num = 0; + for (auto& junc : road->junctions) { + num += junc->getNumCarsOnJunction(); + } + + return OneWayRoad::getNumberOfCars() + num; +} + + +void OneWayRoadSSJ::dispResults() +{ + visualizeSafetyDistance(); + reverse(results.begin(), results.end()); + + for (unsigned int i = 0; i < results.size(); i++) { + + dispJunctionsAtCell(i); + dispSinksAtCell(i); + dispSourcesAtCell(i); + + for (unsigned int j = 0; j < results[i].size(); j++) { + VectorHelper::makeVectorOutputColourful(results[i][j]); + cout << setw(4) << results[i][j]; + } + + cout << endl; + } + cout << endl; + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // set output default white 7; +} + +void OneWayRoadSSJ::visualizeSafetyDistance() { + if (road->safetyDistance != 0) { + int neighbor; + for (unsigned int step = 0; step <= timeSteps; step++) { + for (unsigned int i = 0; i < road->roadLength; i++) { + if (results[i][step] > -1) { + neighbor = road->neighbors[i]; + for (unsigned int j = 1; j <= road->safetyDistance; j++) { + //junction or sink + if (neighbor <= -1000) { + break; + } + if (results[neighbor][step] > -1) { + cerr << "safetyDistance was violated: timestep: " << step << "\t carIndex: " << i << endl; + break; + } + else + results[neighbor][step] = -5; + neighbor = road->neighbors[neighbor]; + } + } + } + } + } + +} + +void OneWayRoadSSJ::dispJunctionsAtCell(unsigned int index) const +{ + for (auto& junc : road->junctions) { + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // set output default white 7; + junc->dispJunction(index, road->roadLength); + } +} + +void OneWayRoadSSJ::dispSinksAtCell(unsigned int index) const +{ + for (auto& sink : road->sinks) { + if (sink->getIndex() == road->roadLength - index - 1) { + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); //set output bright green 10, bright red 12 + cout << setw(4) << 1 - (sink->getPossibilityBeingBlocked()); + return; + } + cout << setw(4) << " "; + } +} + + +void OneWayRoadSSJ::dispSourcesAtCell(unsigned int index) const +{ + for (auto& source : road->sources) { + if (source->getIndex() == road->roadLength - index - 1) { + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); //set output bright green 10, bright red 12 + cout << setw(4) << source->getPossibility(); + return; + } + cout << setw(4) << " "; + } +} \ No newline at end of file diff --git a/src/Traffic/OneWayRoadSSJ.h b/src/Traffic/OneWayRoadSSJ.h new file mode 100644 index 000000000..af85ddbc3 --- /dev/null +++ b/src/Traffic/OneWayRoadSSJ.h @@ -0,0 +1,40 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include "OneWayRoad.h" + +class VF_PUBLIC OneWayRoadSSJ : + public OneWayRoad +{ + +public: + OneWayRoadSSJ(shared_ptr<RoadNetworkData> road, const float dawdlePossibility); + OneWayRoadSSJ(); + virtual ~OneWayRoadSSJ(); + + unsigned int getGapAfterOutCell(unsigned int outCellIndex, unsigned int speed); + void moveJunctionCar(unsigned int outCellIndex, unsigned int remainingDistance, unsigned int speed); + + virtual void dispResults(); + + virtual const unsigned int getNumberOfCars() const; //only use for testing + +private: + + virtual void calculateTimestep(unsigned int step); + void calculateSourceStep(); + + virtual unsigned int getGapAfterCar(unsigned int carIndex, unsigned int speed, int neighbor); + unsigned int getGapToSink(int neighbor, unsigned int i, unsigned int speed); + unsigned int getGapToJunction(int neighbor, unsigned int i, unsigned int speed, unsigned int currentCell); + + virtual void moveCar(unsigned int carIndex, unsigned int speed); + unsigned int iterateNeighborsInMove(unsigned int ¤tCell, unsigned int speed, int &neighbor); + shared_ptr<Junction>& getJunctionFromNeighbor(int neighbor); + shared_ptr<Sink>& getSinkFromNeighbor(int neighbor); + + virtual void visualizeSafetyDistance(); + void dispJunctionsAtCell(unsigned int index) const; + void dispSinksAtCell(unsigned int index) const; + void dispSourcesAtCell(unsigned int index) const; +}; diff --git a/src/Traffic/RandomHelper.cpp b/src/Traffic/RandomHelper.cpp new file mode 100644 index 000000000..52b3f15a2 --- /dev/null +++ b/src/Traffic/RandomHelper.cpp @@ -0,0 +1,10 @@ +#include "RandomHelper.h" + + + +mt19937 RandomHelper::make_engine() +{ + std::random_device r; + std::seed_seq seed{r()}; + return std::mt19937(seed); +} \ No newline at end of file diff --git a/src/Traffic/RandomHelper.h b/src/Traffic/RandomHelper.h new file mode 100644 index 000000000..417f80af1 --- /dev/null +++ b/src/Traffic/RandomHelper.h @@ -0,0 +1,12 @@ +#pragma once +#include <random> +#include <VirtualFluidsDefinitions.h> + +using namespace std; + +class VF_PUBLIC RandomHelper +{ +public: + static std::mt19937 make_engine(); +}; + diff --git a/src/Traffic/RoadMaker.cpp b/src/Traffic/RoadMaker.cpp new file mode 100644 index 000000000..939b0ca98 --- /dev/null +++ b/src/Traffic/RoadMaker.cpp @@ -0,0 +1,281 @@ +#include "RoadMaker.h" + + +//random vehicle Distribution +RoadMaker::RoadMaker(const unsigned int roadLength, const unsigned int maxVelocity, unsigned int vehicleLength, const float vehicleDensity) +{ + uniform_int_distribution<unsigned int> distInt2{ 0, maxVelocity }; + distInt = distInt2; + + this->roadLength = roadLength; + this->maxVelocity = maxVelocity; + initVehicleLength(vehicleLength); + + //init vectors + initCurrentAsEmpty(); + initNext(); + initNeighbors(); + + initVehicleDensity(vehicleDensity); +} + + +//given vehicle distribution +RoadMaker::RoadMaker(const std::vector<int> vehicleDistribution, const unsigned int maxVelocity, unsigned int vehicleLength) +{ + this->roadLength = vehicleDistribution.size(); + + this->maxVelocity = maxVelocity; + initVehicleLength(vehicleLength); + + //init vectors + current = vehicleDistribution; + initNext(); + initNeighbors(); +} + + +//empty road +RoadMaker::RoadMaker(const unsigned int roadLength, const unsigned int maxVelocity, unsigned int vehicleLength) +{ + this->roadLength = roadLength; + this->maxVelocity = maxVelocity; + initVehicleLength(vehicleLength); + + initCurrentAsEmpty(); + initNext(); + initNeighbors(); +} + + +RoadMaker::~RoadMaker() +{ +} + + +void RoadMaker::initNext() +{ + next.resize(roadLength); + VectorHelper::fillVector(next, -1); +} + + +void RoadMaker::initNeighbors() +{ + neighbors.resize(roadLength); + for (unsigned int i = 0; i < roadLength - 1; i++) { + neighbors[i] = i + 1; + } + neighbors[roadLength - 1] = 0; +} + + +void RoadMaker::initCurrentAsEmpty() +{ + current.resize(roadLength); + VectorHelper::fillVector(current, -1); +} + + +void RoadMaker::initVehicleDensity(const float vehicleDensity) +{ + try { + if (vehicleDensity > 0 && vehicleDensity < 1) { + initRandomCars(vehicleDensity); + } + else { + throw invalidInput_error("The vehicleDensity should be between 0 and 1"); + } + } + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + } +} + + +void RoadMaker::initRandomCars(const float vehicleDensity) +{ + //this method doesn't fill the first cells, so that the safetyDistance isn't violated in a periodic road + for (unsigned int i = safetyDistance; i < roadLength; i++) { + double randomNumber = distFloat(engine); + if (randomNumber <= vehicleDensity) { + current[i] = randomSpeed(); + i += safetyDistance; + } + } +} + + +int RoadMaker::randomSpeed() +{ + return distInt(engine); +} + + +void RoadMaker::initVehicleLength(const unsigned int vehicleLength) +{ + try { + if (vehicleLength == 0) throw invalidInput_error("The vehicleLength has to be greater than 0"); + this->vehicleLength = vehicleLength; + this->safetyDistance = vehicleLength - 1; + } + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + } +} + + +void RoadMaker::setJunctions(std::vector<std::shared_ptr<Junction> > & junctions) +{ + for (auto& junc : junctions) + addJunction(junc); +} + + +void RoadMaker::addJunction(std::shared_ptr<Junction>& junction) +{ + try { + + junction->checkOutCellIndices(roadLength); + setJunctionAsNeighbor(junction); + this->junctions.push_back(junction); + + if (junctions.size() > 999) throw runtime_error("too many junctions"); + + } + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + } +} + + +void RoadMaker::setJunctionAsNeighbor(std::shared_ptr<Junction> & junction) +{ + //set the junction as neighbor of the incoming cells + + int junctionIndex = -1000 - junctions.size(); //value range: -1000 to -1999 + vector<unsigned int> inCells = junction->getInCellIndices(); + + try { + + for (auto cell : inCells) { + if (cell >= roadLength) throw invalidInput_error("The index of an incoming cell to a junction ist greater than the roadLength."); + if (neighbors[cell] < 0) + cout << "The neighboring cell of cell " << cell << " was already definded as sink or junction, no new junction added." << endl; + else + neighbors[cell] = junctionIndex; + } + + } + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + } +} + + +void RoadMaker::setSinks(std::vector<std::shared_ptr<Sink> > & sinks) +{ + for (auto& sink : sinks) + addSink(sink); +} + + +void RoadMaker::addSink(std::shared_ptr<Sink>& sink) +{ + + try { + + setSinkAsNeighbor(sink); + this->sinks.push_back(sink); + if (sinks.size() > 999) throw runtime_error("too many sinks"); + + + } + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + } + +} + + +void RoadMaker::setSinkAsNeighbor(std::shared_ptr<Sink> & sink) +{ + //set the sink as neighbor of the incoming cell + + int sinkIndex = -2000 - sinks.size(); //value range: -2000 to -2999 + unsigned int sinkCell = sink->getIndex(); + + if (sinkCell >= roadLength) throw invalidInput_error("The index of a sink ist greater than the roadLength."); + + if (neighbors[sinkCell] < 0) { + cout << "The neighboring cell of cell " << sinkCell << " was already definded as sink or junction, no new sink added." << endl; + } + else + { + neighbors[sinkCell] = sinkIndex; + } +} + + + +void RoadMaker::setSources(std::vector< std::shared_ptr<Source> > & sources) +{ + for (auto& source : sources) + addSource(source); +} + + +void RoadMaker::addSource(std::shared_ptr<Source>& source) +{ + try { + if (source->getIndex() >= roadLength) throw invalidInput_error("Source index is greater than roadlength"); + this->sources.push_back(source); + } + + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + } +} + + +void RoadMaker::setNeighbor(unsigned int index, unsigned int neighbor) +{ + this->neighbors[index] = neighbor; +} + + +unsigned int RoadMaker::getMaxVelocity() +{ + return maxVelocity; +} + +void RoadMaker::checkCurrentForSafetyDistance() +{ + if (safetyDistance > 0) { + unsigned int neighbor; + for (unsigned int i = 0; i < roadLength; i++) { + if (current[i] > -1) { + neighbor = neighbors[i]; + for (unsigned int j = 1; j <= safetyDistance; j++) { + if ((current)[neighbor] > -1) { + std::cerr << "timestep 0: safetyDistance was violated: carIndex: " << i << std::endl; + break; + } + neighbor = neighbors[neighbor]; + } + } + } + } +} diff --git a/src/Traffic/RoadMaker.h b/src/Traffic/RoadMaker.h new file mode 100644 index 000000000..9f6072b65 --- /dev/null +++ b/src/Traffic/RoadMaker.h @@ -0,0 +1,48 @@ +#pragma once +#include <random> +#include "RoadNetworkData.h" +#include "RandomHelper.h" +#include <VirtualFluidsDefinitions.h> + +struct VF_PUBLIC RoadMaker : + public RoadNetworkData +{ +public: + RoadMaker(const unsigned int roadLength, const unsigned int maxVelocity, unsigned int vehicleLength, const float vehicleDensity); //random vehicle Distribution + RoadMaker(const std::vector<int> vehicleDistribution, const unsigned int maxVelocity, unsigned int vehicleLength); //given vehicle distribution + RoadMaker(const unsigned int roadLength, const unsigned int maxVelocity, unsigned int vehicleLength);//empty road + + ~RoadMaker(); + + void setJunctions( std::vector<std::shared_ptr<Junction> > & junctions); //max 999 junctions + void addJunction(std::shared_ptr<Junction> & junction); + void setSinks(std::vector< std::shared_ptr<Sink> > & sinks); //max 999 sinks + void addSink(std::shared_ptr<Sink> & sink); + void setSources(std::vector< std::shared_ptr<Source> > & sources); + void addSource(std::shared_ptr<Source> & source); + + void setNeighbor(unsigned int index, unsigned int neighbor); // don't use it for setting sinks or junctions! + + void checkCurrentForSafetyDistance(); + + unsigned int getMaxVelocity(); + +private: + mt19937 engine = RandomHelper::make_engine(); + uniform_real_distribution<float> distFloat{ 0.0, 1.0 }; + uniform_int_distribution<unsigned int> distInt{ 0, maxVelocity }; + +private: + void initNext(); + void initNeighbors(); + void initCurrentAsEmpty(); + void initVehicleDensity(const float vehicleDensity); + void initRandomCars(const float vehicleDensity); + void initVehicleLength(const unsigned int vehicleLength); + int randomSpeed(); + + void setJunctionAsNeighbor(std::shared_ptr<Junction> & junction); + void setSinkAsNeighbor(std::shared_ptr<Sink> & sink); + +}; + diff --git a/src/Traffic/RoadNetworkData.h b/src/Traffic/RoadNetworkData.h new file mode 100644 index 000000000..9e36785b7 --- /dev/null +++ b/src/Traffic/RoadNetworkData.h @@ -0,0 +1,34 @@ +#pragma once +#include <memory> +#include <vector> + +#include "VectorHelper.h" +#include "invalidInput_error.h" + +#include <VirtualFluidsDefinitions.h> + +#include "Source.h" +#include "Sink.h" +#include "Junction.h" + + +struct VF_PUBLIC RoadNetworkData +{ +public: + //friend class OneWayRoad; + //friend class OneWayRoadSSJ; + + unsigned int roadLength; + unsigned int maxVelocity; + unsigned int vehicleLength; + unsigned int safetyDistance; + + std::vector<int> current; + std::vector<int> next; //for temporary calculations + std::vector<int> neighbors; + + std::vector<std::shared_ptr<Sink> > sinks; + std::vector<std::shared_ptr<Junction> > junctions; + std::vector<std::shared_ptr<Source> > sources; +}; + diff --git a/src/Traffic/Sink.cpp b/src/Traffic/Sink.cpp new file mode 100644 index 000000000..e69de29bb diff --git a/src/Traffic/Sink.h b/src/Traffic/Sink.h new file mode 100644 index 000000000..2c4f01705 --- /dev/null +++ b/src/Traffic/Sink.h @@ -0,0 +1,13 @@ +#pragma once + +#include "SinkData.h" +#include <VirtualFluidsDefinitions.h> + +class VF_PUBLIC Sink +{ +public: + virtual float getPossibilityBeingBlocked() const = 0; + virtual bool carCanEnter() = 0; + virtual unsigned int getIndex() const = 0; +}; + diff --git a/src/Traffic/SinkData.h b/src/Traffic/SinkData.h new file mode 100644 index 000000000..ef66ebae8 --- /dev/null +++ b/src/Traffic/SinkData.h @@ -0,0 +1,11 @@ +#pragma once + +#include <memory> +#include <vector> +#include <VirtualFluidsDefinitions.h> + + +struct VF_PUBLIC SinkData { + unsigned int sinkIndex; + float sinkBlockedPossibility; +}; \ No newline at end of file diff --git a/src/Traffic/SinkSimple.cpp b/src/Traffic/SinkSimple.cpp new file mode 100644 index 000000000..31c8a8646 --- /dev/null +++ b/src/Traffic/SinkSimple.cpp @@ -0,0 +1,41 @@ +#include "SinkSimple.h" + + + +SinkSimple::SinkSimple(unsigned int sinkIndex, float sinkBlockedPossibility) +{ + data.sinkIndex = sinkIndex; + + try { + if (sinkBlockedPossibility >= 0 && sinkBlockedPossibility <= 1) { + data.sinkBlockedPossibility = sinkBlockedPossibility; + } + else { + throw invalidInput_error("possibility of the sink being blocked should be between 0 and 1"); + } + } + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + }; +} + + +bool SinkSimple::carCanEnter() +{ + return !(distFloat(engine) < data.sinkBlockedPossibility); +} + + +float SinkSimple::getPossibilityBeingBlocked() const +{ + return data.sinkBlockedPossibility; +} + + +unsigned int SinkSimple::getIndex() const +{ + return data.sinkIndex; +} + diff --git a/src/Traffic/SinkSimple.h b/src/Traffic/SinkSimple.h new file mode 100644 index 000000000..409c316b7 --- /dev/null +++ b/src/Traffic/SinkSimple.h @@ -0,0 +1,31 @@ +#pragma once + +#include <VirtualFluidsDefinitions.h> + +#include <iostream> +#include <random> + +#include "RandomHelper.h" +#include "Sink.h" +#include "invalidInput_error.h" + +using namespace std; + +class VF_PUBLIC SinkSimple: + public Sink +{ +private: + SinkData data; + + mt19937 engine = RandomHelper::make_engine(); + uniform_real_distribution<float> distFloat{ 0.0, 1.0 }; + +public: + SinkSimple(unsigned int sinkIndex, float sinkBlockedPossibility); + ~SinkSimple() {}; + + float getPossibilityBeingBlocked() const; + bool carCanEnter(); + unsigned int getIndex() const; +}; + diff --git a/src/Traffic/Source.cpp b/src/Traffic/Source.cpp new file mode 100644 index 000000000..1cc1a2393 --- /dev/null +++ b/src/Traffic/Source.cpp @@ -0,0 +1 @@ +#include "Source.h" diff --git a/src/Traffic/Source.h b/src/Traffic/Source.h new file mode 100644 index 000000000..5390c1a91 --- /dev/null +++ b/src/Traffic/Source.h @@ -0,0 +1,13 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include "SourceData.h" + +class VF_PUBLIC Source +{ +public: + virtual unsigned int getIndex() const = 0; + virtual float getPossibility() const = 0; + virtual unsigned int getSourceCar() = 0; +}; + diff --git a/src/Traffic/SourceData.h b/src/Traffic/SourceData.h new file mode 100644 index 000000000..7cc20455d --- /dev/null +++ b/src/Traffic/SourceData.h @@ -0,0 +1,12 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <memory> +#include <vector> + + +struct VF_PUBLIC SourceData { + unsigned int sourceIndex; + float sourcePossibility; + unsigned int maxVelocity; +}; \ No newline at end of file diff --git a/src/Traffic/SourceTerm.cpp b/src/Traffic/SourceTerm.cpp new file mode 100644 index 000000000..8c8a9fe00 --- /dev/null +++ b/src/Traffic/SourceTerm.cpp @@ -0,0 +1,58 @@ +#pragma once + +#include "SourceTerm.h" + + +SourceTerm::SourceTerm(const unsigned int sourceIndex, const float sourcePossibility, unsigned int maxVelocity) +{ + data.sourceIndex = sourceIndex; + data.maxVelocity = maxVelocity; + + try { + if (sourcePossibility >= 0 && sourcePossibility <= 1) { + data.sourcePossibility = sourcePossibility; + uniform_int_distribution<unsigned int> distInt2{ 0, maxVelocity }; + distInt = distInt2; + } + else { + throw invalidInput_error("possibility of a car leaving the sink should be between 0 and 1"); + } + } + catch (const exception& e) { + cerr << e.what() << endl; + cin.get(); + exit(EXIT_FAILURE); + }; +} + + + +SourceTerm::~SourceTerm() +{ +} + +unsigned int SourceTerm::getIndex() const +{ + return data.sourceIndex; +} + +float SourceTerm::getPossibility() const +{ + return data.sourcePossibility; +} + + +unsigned int SourceTerm::getSourceCar() +{ + randomNumber = distFloat(engine); + if (randomNumber < data.sourcePossibility) { + return randomSpeed(); + } + return -1; +} + + +unsigned int SourceTerm::randomSpeed() +{ + return distInt(engine); +} diff --git a/src/Traffic/SourceTerm.h b/src/Traffic/SourceTerm.h new file mode 100644 index 000000000..2916c6e0b --- /dev/null +++ b/src/Traffic/SourceTerm.h @@ -0,0 +1,36 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <iostream> +#include <random> +#include "Source.h" +#include "OneWayRoadSSJ.h" + +using namespace std; + +class VF_PUBLIC SourceTerm: + public Source +{ +private: + SourceData data; + + mt19937 engine = RandomHelper::make_engine(); + uniform_real_distribution<float> distFloat{ 0.0, 1.0 }; + uniform_int_distribution<unsigned int> distInt{ 0, 1 }; + +public: + SourceTerm(const unsigned int sourceIndex, const float sourcePossibility, unsigned int maxVelocity); + ~SourceTerm(); + + virtual unsigned int getIndex() const; + virtual float getPossibility() const; + virtual unsigned int getSourceCar(); + +private: + unsigned int randomSpeed(); + +private: + //variables for temporaray calculations + float randomNumber; +}; + diff --git a/src/Traffic/VectorHelper.cpp b/src/Traffic/VectorHelper.cpp new file mode 100644 index 000000000..444950acd --- /dev/null +++ b/src/Traffic/VectorHelper.cpp @@ -0,0 +1,71 @@ +#include "VectorHelper.h" + +void VectorHelper::fillVector(std::vector<int> &vector, int insertNumber) { + fill(vector.begin(), vector.end(), insertNumber); +} + +void VectorHelper::fillVector(std::vector<std::vector<int> > &vector, int insertNumber) { + for (unsigned int i = 0; i < vector.size(); i++) { + fill(vector[i].begin(), vector[i].end(), insertNumber); + } +} + +void VectorHelper::dispVector(const std::vector<int> &vector) +{ + for (int number : vector) { + std::cout << std::setw(4) << number; + } + std::cout << std::endl; +} + +void VectorHelper::dispVector(const std::vector<std::vector<int> > &vector) +{ + for (unsigned int i = 0; i < vector.size(); i++) { + for (unsigned int j = 0; j < vector[i].size(); j++) { + std::cout << std::setw(4) << vector[i][j]; + } + std::cout << std::endl; + } + std::cout << std::endl; +} + +void VectorHelper::dispVectorColour(const std::vector<int> &vector) +{ + for (int number : vector) { + makeVectorOutputColourful(number); + std::cout << std::setw(4) << number; + } + std::cout << std::endl; + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // set output default white 7; +} + +void VectorHelper::dispVectorColour(const std::vector<std::vector<int>>& vector) +{ + for (unsigned int i = 0; i < vector.size(); i++) { + for (unsigned int j = 0; j < vector[i].size(); j++) { + makeVectorOutputColourful(vector[i][j]); + std::cout << std::setw(4) << vector[i][j]; + } + std::cout << std::endl; + } + std::cout << std::endl; + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // set output default white 7; +} + +void VectorHelper::makeVectorOutputColourful(int outputNumber) +{ + switch (outputNumber) { + case -1: + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8); //set output dark grey 8, dark blue 1, black 0; + break; + case 0: + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); //set output bright green 10, bright red 12; + break; + case -5: + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0); //set output dark grey 8, dark blue 1, black 0; + break; + default: + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); //set output bright green 10, bright red 12; + } + +} \ No newline at end of file diff --git a/src/Traffic/VectorHelper.h b/src/Traffic/VectorHelper.h new file mode 100644 index 000000000..37cfc6960 --- /dev/null +++ b/src/Traffic/VectorHelper.h @@ -0,0 +1,23 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <vector> +#include <iostream> + +#include <windows.h> //for colourful console output +#include <iomanip> //formatting output streams + +class VF_PUBLIC VectorHelper +{ +public: + + static void fillVector(std::vector<int>& vector, int insertNumber); + static void fillVector(std::vector<std::vector<int> > &vector, int insertNumber); + + static void dispVector(const std::vector<int> &vector); + static void dispVector(const std::vector<std::vector<int> >& vector); + static void dispVectorColour(const std::vector<int> &vector); + static void dispVectorColour(const std::vector<std::vector<int> >& vector); + static void makeVectorOutputColourful(const int outputNumber); +}; + diff --git a/src/Traffic/invalidInput_error.cpp b/src/Traffic/invalidInput_error.cpp new file mode 100644 index 000000000..12aae8bc5 --- /dev/null +++ b/src/Traffic/invalidInput_error.cpp @@ -0,0 +1,11 @@ +#include "invalidInput_error.h" + +invalidInput_error::invalidInput_error(char const * const message) throw() : runtime_error(message) +{ +} + +char const * invalidInput_error::what() const throw() +{ + return runtime_error::what(); +} + diff --git a/src/Traffic/invalidInput_error.h b/src/Traffic/invalidInput_error.h new file mode 100644 index 000000000..74a599eae --- /dev/null +++ b/src/Traffic/invalidInput_error.h @@ -0,0 +1,16 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +// using standard exceptions +#include <iostream> +#include <stdexcept> +using namespace std; + +class VF_PUBLIC invalidInput_error : + public runtime_error +{ +public: + invalidInput_error(char const* const message) throw(); + virtual char const* what() const throw(); +}; + diff --git a/src/Traffic/package.include b/src/Traffic/package.include new file mode 100644 index 000000000..e69de29bb diff --git a/src/VirtualFluids_GPU/LBM/Simulation.cpp b/src/VirtualFluids_GPU/LBM/Simulation.cpp index 1415afacd..322a15734 100644 --- a/src/VirtualFluids_GPU/LBM/Simulation.cpp +++ b/src/VirtualFluids_GPU/LBM/Simulation.cpp @@ -87,7 +87,7 @@ void Simulation::init(SPtr<Parameter> para, SPtr<GridProvider> gridProvider, std gridProvider->setBoundingBox(); para->initParameter(); - para->setRe(para->getVelocity() * (real)1.0 / para->getViscosity()); + para->setRe(para->getVelocity() * (real)1.2 / para->getViscosity()); para->setPhi((real) 0.0); para->setlimitOfNodesForVTK(30000000); //max 30 Million nodes per VTK file if (para->getDoRestart()) @@ -163,10 +163,10 @@ void Simulation::init(SPtr<Parameter> para, SPtr<GridProvider> gridProvider, std //////////////////////////////////////////////////////////////////////////// - uint tAnalyse = 100; - kinEnergyWriter = new KineticEnergyAnalyzer(para, tAnalyse); + //uint tAnalyse = 100; + //kinEnergyWriter = new KineticEnergyAnalyzer(para, tAnalyse); //////////////////////////////////////////////////////////////////////////// - enstrophyWriter = new EnstrophyAnalyzer(para, tAnalyse); + //enstrophyWriter = new EnstrophyAnalyzer(para, tAnalyse); //////////////////////////////////////////////////////////////////////////// @@ -1600,10 +1600,10 @@ void Simulation::run() } //////////////////////////////////////////////////////////////////////////////// - kinEnergyWriter->run(t); + //kinEnergyWriter->run(t); //////////////////////////////////////////////////////////////////////////////// //printf("Simulation - Before enstrophyWriter run\n"); - enstrophyWriter->run(t); + //enstrophyWriter->run(t); //printf("Simulation - after enstrophyWriter run\n"); //////////////////////////////////////////////////////////////////////////////// @@ -2568,11 +2568,11 @@ void Simulation::run() } ////////////////////////////////////////////////////////////////////////// - VeloASCIIWriter::writeVelocitiesAsTXT(para.get(), lev, t); + //VeloASCIIWriter::writeVelocitiesAsTXT(para.get(), lev, t); ////////////////////////////////////////////////////////////////////////// - std::string fname = para->getFName() + StringUtil::toString<int>(t) + "_t_"; - kinEnergyWriter->writeToFile(fname); - enstrophyWriter->writeToFile(fname); + //std::string fname = para->getFName() + StringUtil::toString<int>(t) + "_t_"; + //kinEnergyWriter->writeToFile(fname); + //enstrophyWriter->writeToFile(fname); ////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/targets/apps/LBM/Basel/main.cpp b/targets/apps/LBM/Basel/main.cpp index bd3278cf5..3d46637aa 100644 --- a/targets/apps/LBM/Basel/main.cpp +++ b/targets/apps/LBM/Basel/main.cpp @@ -297,7 +297,7 @@ void multipleLevel(const std::string& configPath) real dx = 1.2; real vx = 0.05; - TriangularMesh* BaselSTL = TriangularMesh::make("M:/Basel2019/stl/BaselUrbanProfile_066_deg_bridge_All_CLOSED.stl"); + TriangularMesh* BaselSTL = TriangularMesh::make("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/stl/BaselUrbanProfile_066_deg_bridge_All_CLOSED.stl"); gridBuilder->addCoarseGrid(-256.0, -256.0, - 8.0, 256.0, 256.0, 160.0, dx); @@ -317,23 +317,23 @@ void multipleLevel(const std::string& configPath) ////////////////////////////////////////////////////////////////////////// - gridBuilder->writeGridsToVtk("M:/Basel2019/grids/BaselUni/Basel_Grid"); + gridBuilder->writeGridsToVtk("C:/Users/hiwi/Desktop/Basel_Ergebnisse/grids/BaselUni/Basel_Grid"); - SimulationFileWriter::write("M:/Basel2019/grids/BaselUni/", gridBuilder, FILEFORMAT::BINARY); + SimulationFileWriter::write("C:/Users/hiwi/Desktop/Basel_Ergebnisse/grids/BaselUni/", gridBuilder, FILEFORMAT::BINARY); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// StreetPointFinder finder; - finder.readStreets("C:/Users/schoen/Desktop/git/MS2/git/targets/apps/LBM/streetTest/resources/ExampleStreets.txt"); + finder.readStreets("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/targets/apps/LBM/streetTest/resources/ExampleStreets.txt"); - finder.writeVTK("F:/Work/Computations/NagelSchreckenberg/ExampleStreets.vtk"); + finder.writeVTK("C:/Users/hiwi/Desktop/Basel_Ergebnisse/ExampleStreets.vtk"); finder.findIndicesLB(gridBuilder->getGrid(0)); - finder.writeConnectionVTK("M:/Basel2019/grids/BaselUni/Basel_Grid/ExampleStreetsConnection.vtk", gridBuilder->getGrid(0)); + finder.writeConnectionVTK("C:/Users/hiwi/Desktop/Basel_Ergebnisse/grids/BaselUni/Basel_Grid/ExampleStreetsConnection.vtk", gridBuilder->getGrid(0)); - finder.writeSimulationFile("M:/Basel2019/grids/BaselUni/", 1.0, gridBuilder->getNumberOfLevels(), 0); + finder.writeSimulationFile("C:/Users/hiwi/Desktop/Basel_Ergebnisse/grids/BaselUni/", 1.0, gridBuilder->getNumberOfLevels(), 0); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -359,7 +359,7 @@ void multipleLevel(const std::string& configPath) std::ifstream stream; stream.open(configPath.c_str(), std::ios::in); - if (stream.fail()) + //if (stream.fail()) throw std::runtime_error("can not open config file!"); UPtr<input::Input> input = input::Input::makeInput(stream, "config"); @@ -401,7 +401,7 @@ int main( int argc, char* argv[]) { try { - multipleLevel("C:/Users/schoen/Desktop/bin/ReleaseBasel/configBasel.txt"); + multipleLevel("C:/Users/hiwi/Desktop/MS/configBasel.txt"); //multipleLevel("F:/Work/Computations/gridGenerator/inp/configTest.txt"); } catch (const std::exception& e) @@ -424,7 +424,7 @@ int main( int argc, char* argv[]) //std::cout << "unknown exeption" << std::endl; } - std::cout << "\nConfiguration file must be set!: lbmgm <config file>" << std::endl << std::flush; + //std::cout << "\nConfiguration file must be set!: lbmgm <config file>" << std::endl << std::flush; //MPI_Abort(MPI_COMM_WORLD, -1); } } diff --git a/targets/apps/LBM/Basel/resources/ExampleStreets.txt b/targets/apps/LBM/Basel/resources/ExampleStreets.txt new file mode 100644 index 000000000..9e2df6c6d --- /dev/null +++ b/targets/apps/LBM/Basel/resources/ExampleStreets.txt @@ -0,0 +1,11 @@ +10 + 256 5 87 5 1 + 87 5 -85 5 1 + -85 5 -80 256 1 + -85 5 -256 5 1 + -185 -256 -85 5 1 + -92 0 87 0 1 + -85 256 -92 0 1 + -256 0 -92 0 1 + 87 0 256 0 1 + -92 0 -190 -256 1 \ No newline at end of file diff --git a/targets/apps/LBM/TrafficTest/3rdPartyLinking.cmake b/targets/apps/LBM/TrafficTest/3rdPartyLinking.cmake new file mode 100644 index 000000000..7fb2e8a6d --- /dev/null +++ b/targets/apps/LBM/TrafficTest/3rdPartyLinking.cmake @@ -0,0 +1,13 @@ +#include (${CMAKE_SOURCE_DIR}/${cmakeMacroPath}/MPI/Link.cmake) +#linkMPI(${targetName}) +#include (${CMAKE_SOURCE_DIR}/${cmakeMacroPath}/Cuda/Link.cmake) +#linkCuda(${targetName}) +#include (${CMAKE_SOURCE_DIR}/${cmakeMacroPath}/Boost/Link.cmake) +#linkBoost(${targetName} "serialization") +#include (${CMAKE_SOURCE_DIR}/${cmakeMacroPath}/Metis/Link.cmake) +#linkMetis(${targetName}) + +#if(HULC.BUILD_JSONCPP) +# include (${CMAKE_SOURCE_DIR}/${cmakeMacroPath}/JsonCpp/Link.cmake) +# linkJsonCpp(${targetName}) +#endif() \ No newline at end of file diff --git a/targets/apps/LBM/TrafficTest/CMakeLists.txt b/targets/apps/LBM/TrafficTest/CMakeLists.txt new file mode 100644 index 000000000..73504dec2 --- /dev/null +++ b/targets/apps/LBM/TrafficTest/CMakeLists.txt @@ -0,0 +1,15 @@ +setTargetNameToFolderName(${CMAKE_CURRENT_LIST_DIR}) + +set(linkDirectories "") +set(libsToLink Traffic) +set(includeDirectories "${CMAKE_SOURCE_DIR}/src" + "${CMAKE_SOURCE_DIR}/src/Traffic") + +#glob files and save in MY_SRCS +include(CMakePackage.cmake) + +buildExe(${targetName} "${MY_SRCS}" "${linkDirectories}" "${libsToLink}" "${includeDirectories}") +groupTarget(${targetName} ${lbmAppFolder}) + +# Specify the linking to 3rdParty libs +include(3rdPartyLinking.cmake) \ No newline at end of file diff --git a/targets/apps/LBM/TrafficTest/CMakePackage.cmake b/targets/apps/LBM/TrafficTest/CMakePackage.cmake new file mode 100644 index 000000000..5d39e3804 --- /dev/null +++ b/targets/apps/LBM/TrafficTest/CMakePackage.cmake @@ -0,0 +1,9 @@ +#FILE ENDINGS +resetFileEndingsToCollect() +addCAndCPPFileTypes() +addFileEndingToCollect("*.cu") +addFileEndingToCollect("*.cuh") + +#GLOB SOURCE FILES IN MY_SRCS +unset(MY_SRCS) +includeRecursiveAllFilesFrom(${targetName} ${CMAKE_CURRENT_LIST_DIR}) \ No newline at end of file diff --git a/targets/apps/LBM/TrafficTest/TrafficTest.cpp b/targets/apps/LBM/TrafficTest/TrafficTest.cpp new file mode 100644 index 000000000..154f4b070 --- /dev/null +++ b/targets/apps/LBM/TrafficTest/TrafficTest.cpp @@ -0,0 +1,228 @@ +#include <iostream> +#include <vector> +#include <memory> + +#include "Traffic/RoadMaker.h" +#include "Traffic/OneWayRoad.h" +#include "Traffic/OneWayRoadSSJ.h" +#include "Traffic/SourceTerm.h" +#include "Traffic/Junction.h" +#include "Traffic/JunctionSimple.h" +#include "Traffic/Sink.h" +#include "Traffic/SinkSimple.h" +#include "Traffic/ConcentrationByPosition.h" + + +using namespace std; + +//static void junctionTest(unsigned int maxVelocity, unsigned int vehicleLength) { +// { +// float dawdlePossibility = 0.5; +// vector<int> fiveCars = { 1, -1,-1, 1, -1, -1, -1, 2, -1, -1,-1,0,-1,-1,-1,-1,1,-1,-1,-1 }; +// +// OneWayRoadSSJ* junctionTest = new OneWayRoadSSJ(fiveCars, maxVelocity, dawdlePossibility, vehicleLength); +// +// vector <unsigned int> in4 = { 9 }; +// vector<unsigned int> out4 = { 10 }; +// vector<Junction*> junctions4 = { new JunctionSimple(in4, out4, junctionTest) }; +// junctionTest->setJunctions(junctions4); +// +// junctionTest->calculateResults(10); +// junctionTest->dispResults(); +// +// cout << "Number of Cars" << junctionTest->getNumberOfCars() << endl; +// } +//} +// +//static void junctionTestCrossRoads(unsigned int maxVelocity, unsigned int vehicleLength) { +// { +// float dawdlePossibility = 0.5; +// vector<int> fiveCars = { 1, -1,-1, 1, -1, -1, -1, 2, -1, -1,-1,0,-1,-1,-1,-1,1,-1,-1,-1 }; +// OneWayRoadSSJ* junctionTestCrossRoads = new OneWayRoadSSJ(fiveCars, maxVelocity, dawdlePossibility, vehicleLength); +// +// vector <unsigned int> in5 = { 6,9 }; +// vector<unsigned int> out5 = { 10, 14 }; +// vector<Junction*> junctions5 = {new JunctionSimple(in5, out5, junctionTestCrossRoads) }; +// junctionTestCrossRoads->setJunctions(junctions5); +// junctionTestCrossRoads->setNeighbor(13, 7); +// +// junctionTestCrossRoads->calculateResults(10); +// junctionTestCrossRoads->dispResults(); +// +// cout << "Number of Cars" << junctionTestCrossRoads->getNumberOfCars() << endl; +// } +//} + + +int main() +{ + int roadLength = 20; + const unsigned int maxVelocity = 5; + float vehicleDensity = 0.5; + float dawdlePossibility = 0.5;//typical value: 0.2 + int numberOfTimesteps = 25; + unsigned int vehicleLength = 3; + vector<int> initialDistribution = { 1,-1,-1,1,-1,-1,1,-1,-1,1,-1,-1,1,-1,-1,1,-1,-1,-1,-1 }; + vector<int> oneCar = { 1,-1,-1,-1,-1,-1 }; + vector<int> fiveCars = { 1, -1,-1, 1, -1, -1, -1, 2, -1, -1,-1,0,-1,-1,-1,-1,1,-1,-1,-1 }; + + //OneWay random + { + cout << "OneWay random" << endl; + + + auto r = make_shared<RoadMaker>(roadLength, maxVelocity, vehicleLength, vehicleDensity); + auto road = make_shared<OneWayRoad>(r ,dawdlePossibility); + + //auto writer = make_unique<ConcentrationByPosition>(ConcentrationByPosition(road->getRoadLength())); + //road->setConcentrationOutwriter(move(writer)); + + road->calculateResults(numberOfTimesteps); + road->dispResults(); + + cout << endl << endl; + + } + + + ////OneWay initial distribution + //{ + // cout << "OneWay initial distribution" << endl; + // unique_ptr<RoadNetworkData> r = make_unique<RoadMaker>(initialDistribution, maxVelocity, vehicleLength); + // shared_ptr<OneWayRoad> road = make_shared<OneWayRoad>(move(r), dawdlePossibility); + // road->calculateResults(numberOfTimesteps); + // road->dispResults(); + // cout << endl << endl; + //} + + + //////OneWay slowToStart + //{ + // dawdlePossibility = 0; + // cout << "OneWay slowToStart" << endl; + // vector<int> initialDist = { -1,1,5,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,0,-1,-1,-1,-1,-1 }; + + // unique_ptr<RoadNetworkData> r = make_unique<RoadMaker>(initialDist, maxVelocity, 1); + // shared_ptr<OneWayRoad> road = make_shared<OneWayRoad>(move(r), dawdlePossibility); + // road->setSlowToStart(static_cast<float>(0.9999)); + // road->calculateResults(numberOfTimesteps); + // road->dispResults(); + // cout << endl << endl; + //} + + + + ////sources and sinks + //{ + //cout << "sources and sinks" << endl; + + //vector< unique_ptr<Sink> > sinks; + //sinks.push_back( make_unique <SinkSimple>(5, static_cast<float>(0.5))); + + //unique_ptr<RoadMaker> r = make_unique<RoadMaker>(oneCar, maxVelocity, vehicleLength); + + //vector< unique_ptr<Source> > sources; + //sources.push_back( make_unique <SourceTerm>(0, static_cast<float>(1.0), r->getMaxVelocity()) ); + + //r->setSources(sources); + //r->setSinks(sinks); + + //shared_ptr<OneWayRoadSSJ> roadSource = make_shared<OneWayRoadSSJ>(move(r), static_cast<float>(0.0)); + + //roadSource->calculateResults(numberOfTimesteps); + //roadSource->dispResults(); + //cout << endl << endl; + //} + + + + ////mergingRoad + //{ + // unique_ptr<RoadMaker> r = make_unique<RoadMaker>(25, maxVelocity, vehicleLength); + // + // vector< unique_ptr<Source> > sources; + // sources.push_back(make_unique <SourceTerm>(0, static_cast<float>(0.5), r->getMaxVelocity())); + // sources.push_back(make_unique <SourceTerm>(10, static_cast<float>(0.5), r->getMaxVelocity())); + // r->setSources(sources); + + // unique_ptr<Sink> s = make_unique <SinkSimple>(SinkSimple(24, static_cast<float>(0.0))); + // r->addSink(s); + + // vector< unique_ptr<Junction> > junctions; + // vector<unsigned int> in = { 9,20 }; + // vector<unsigned int> out = { 21 }; + // junctions.push_back(make_unique <JunctionSimple>(JunctionSimple(in, out))); + + // r->setJunctions(junctions); + + // shared_ptr<OneWayRoadSSJ> mergingRoad = make_shared<OneWayRoadSSJ>(move(r), dawdlePossibility); + + + // mergingRoad->calculateResults(numberOfTimesteps); + // mergingRoad->dispResults(); + // cout << endl << endl; + //} + + + ////splittingRoad + //{ + // OneWayRoadSSJ* splittingRoad = new OneWayRoadSSJ(25, maxVelocity, dawdlePossibility, vehicleLength); + + // vector<Source*> sources2 = { new SourceTerm(0, 1, splittingRoad) }; + + // splittingRoad->setSources(sources2); + + // vector<Sink*> sinks2; + // sinks2.push_back(new SinkSimple(24, 0)); + // sinks2.push_back(new SinkSimple(19, 0)); + // splittingRoad->setSinks(sinks2); + + // vector <unsigned int> in = { 9 }; + // vector<unsigned int> out = { 11,20 }; + // vector<Junction*> junctions2 = { new JunctionSimple(in, out, splittingRoad) }; + // splittingRoad->setJunctions(junctions2); + + // splittingRoad->calculateResults(numberOfTimesteps); + // splittingRoad->dispResults(); + // cout << endl << endl; + //} + + + + + ////crossRoads + //{ + + // OneWayRoadSSJ* crossRoads = new OneWayRoadSSJ(31, maxVelocity, dawdlePossibility, vehicleLength); + + + // vector<Source*> sources; + // sources.push_back(new SourceTerm(0, 1, crossRoads)); + // sources.push_back(new SourceTerm(11, 1, crossRoads)); + + // crossRoads->setSources(sources); + + // vector<Sink*> sinks3; + // sinks3.push_back(new SinkSimple(30, 0)); + // sinks3.push_back(new SinkSimple(25, 0)); + // crossRoads->setSinks(sinks3); + + // vector<unsigned int> in3 = { 9,19 }; + // vector<unsigned int> out3 = { 21,26 }; + // vector<Junction*> junctions3 = { new JunctionSimple(in3, out3, crossRoads) }; + // crossRoads->setJunctions(junctions3); + + // crossRoads->calculateResults(numberOfTimesteps); + // crossRoads->dispResults(); + // cout << endl << endl; + //} + + + + //junctionTest(maxVelocity, vehicleLength); + + //junctionTestCrossRoads(maxVelocity, vehicleLength); + + std::cin.get(); +} + diff --git a/targets/apps/LBM/TrafficTest/package.include b/targets/apps/LBM/TrafficTest/package.include new file mode 100644 index 000000000..e69de29bb diff --git a/targets/apps/LBM/TrafficTest/resources/ExampleStreets.txt b/targets/apps/LBM/TrafficTest/resources/ExampleStreets.txt new file mode 100644 index 000000000..9e2df6c6d --- /dev/null +++ b/targets/apps/LBM/TrafficTest/resources/ExampleStreets.txt @@ -0,0 +1,11 @@ +10 + 256 5 87 5 1 + 87 5 -85 5 1 + -85 5 -80 256 1 + -85 5 -256 5 1 + -185 -256 -85 5 1 + -92 0 87 0 1 + -85 256 -92 0 1 + -256 0 -92 0 1 + 87 0 256 0 1 + -92 0 -190 -256 1 \ No newline at end of file diff --git a/targets/apps/LBM/TrafficTest/resources/FlatGround.stl b/targets/apps/LBM/TrafficTest/resources/FlatGround.stl new file mode 100644 index 000000000..8b18606cc --- /dev/null +++ b/targets/apps/LBM/TrafficTest/resources/FlatGround.stl @@ -0,0 +1,86 @@ +solid FlatGround +facet normal 0.0 0.0 -1.0 + outer loop + vertex 300.0 299.9999999999999 -10.000000000000002 + vertex -300.0 -299.99999999999994 -10.000000000000002 + vertex -300.0 299.9999999999999 -10.000000000000002 + endloop +endfacet +facet normal 0.0 0.0 -1.0 + outer loop + vertex -300.0 -299.99999999999994 -10.000000000000002 + vertex 300.0 299.9999999999999 -10.000000000000002 + vertex 300.0 -299.99999999999994 -10.000000000000002 + endloop +endfacet +facet normal -0.0 -0.0 1.0 + outer loop + vertex 300.0 -299.99999999999994 0.0 + vertex -300.0 299.9999999999999 0.0 + vertex -300.0 -299.99999999999994 0.0 + endloop +endfacet +facet normal -0.0 -0.0 1.0 + outer loop + vertex -300.0 299.9999999999999 0.0 + vertex 300.0 -299.99999999999994 0.0 + vertex 300.0 299.9999999999999 0.0 + endloop +endfacet +facet normal 0.0 -1.0 0.0 + outer loop + vertex 300.0 -299.99999999999994 0.0 + vertex -300.0 -299.99999999999994 -10.000000000000002 + vertex 300.0 -299.99999999999994 -10.000000000000002 + endloop +endfacet +facet normal 0.0 -1.0 0.0 + outer loop + vertex -300.0 -299.99999999999994 -10.000000000000002 + vertex 300.0 -299.99999999999994 0.0 + vertex -300.0 -299.99999999999994 0.0 + endloop +endfacet +facet normal -1.0 0.0 0.0 + outer loop + vertex -300.0 299.9999999999999 0.0 + vertex -300.0 -299.99999999999994 -10.000000000000002 + vertex -300.0 -299.99999999999994 0.0 + endloop +endfacet +facet normal -1.0 0.0 0.0 + outer loop + vertex -300.0 -299.99999999999994 -10.000000000000002 + vertex -300.0 299.9999999999999 0.0 + vertex -300.0 299.9999999999999 -10.000000000000002 + endloop +endfacet +facet normal -0.0 1.0 -0.0 + outer loop + vertex -300.0 299.9999999999999 0.0 + vertex 300.0 299.9999999999999 -10.000000000000002 + vertex -300.0 299.9999999999999 -10.000000000000002 + endloop +endfacet +facet normal -0.0 1.0 -0.0 + outer loop + vertex 300.0 299.9999999999999 -10.000000000000002 + vertex -300.0 299.9999999999999 0.0 + vertex 300.0 299.9999999999999 0.0 + endloop +endfacet +facet normal 1.0 -0.0 -0.0 + outer loop + vertex 300.0 299.9999999999999 -10.000000000000002 + vertex 300.0 -299.99999999999994 0.0 + vertex 300.0 -299.99999999999994 -10.000000000000002 + endloop +endfacet +facet normal 1.0 -0.0 -0.0 + outer loop + vertex 300.0 -299.99999999999994 0.0 + vertex 300.0 299.9999999999999 -10.000000000000002 + vertex 300.0 299.9999999999999 0.0 + endloop +endfacet +endsolid FlatGround diff --git a/targets/libs/Traffic/3rdPartyLinking.cmake b/targets/libs/Traffic/3rdPartyLinking.cmake new file mode 100644 index 000000000..0c6183ae8 --- /dev/null +++ b/targets/libs/Traffic/3rdPartyLinking.cmake @@ -0,0 +1,10 @@ +#include (${CMAKE_SOURCE_DIR}/${cmakeMacroPath}/Cuda/Link.cmake) +#linkCuda(${targetName}) +#include (${CMAKE_SOURCE_DIR}/${cmakeMacroPath}/MPI/Link.cmake) +#linkMPI(${targetName}) +#include (${CMAKE_SOURCE_DIR}/${cmakeMacroPath}/Boost/Link.cmake) +#linkBoost(${targetName} "serialization") +#include (${CMAKE_SOURCE_DIR}/${cmakeMacroPath}/Metis/Link.cmake) +#linkMetis(${targetName}) +#include (${CMAKE_SOURCE_DIR}/${cmakeMacroPath}/OpenMP/Link.cmake) +#linkOpenMP(${targetName}) diff --git a/targets/libs/Traffic/CMakeLists.txt b/targets/libs/Traffic/CMakeLists.txt new file mode 100644 index 000000000..e4ef8287f --- /dev/null +++ b/targets/libs/Traffic/CMakeLists.txt @@ -0,0 +1,16 @@ +setTargetNameToFolderName(${CMAKE_CURRENT_LIST_DIR}) + +set(linkDirectories "") +set(libsToLink "") + +set(includeDirectories ${CMAKE_SOURCE_DIR}/src/${targetName} + ${CMAKE_SOURCE_DIR}/src ) + +#glob files and save in MY_SRCS +include(CMakePackage.cmake) + +buildLib(${targetName} "${MY_SRCS}" "${linkDirectories}" "${libsToLink}" "${includeDirectories}") +groupTarget(${targetName} ${libraryFolder}) + +# Specify the linking to 3rdParty libs +include(3rdPartyLinking.cmake) diff --git a/targets/libs/Traffic/CMakePackage.cmake b/targets/libs/Traffic/CMakePackage.cmake new file mode 100644 index 000000000..7ec316fe0 --- /dev/null +++ b/targets/libs/Traffic/CMakePackage.cmake @@ -0,0 +1,8 @@ +#FILE ENDINGS +resetFileEndingsToCollect() +addCAndCPPFileTypes() + +#GLOB SOURCE FILES IN MY_SRCS +unset(MY_SRCS) +includeRecursiveAllFilesFrom(${targetName} ${CMAKE_CURRENT_LIST_DIR}) +includeRecursiveProductionFilesFrom(${targetName} ${CMAKE_SOURCE_DIR}/src/${targetName}) \ No newline at end of file -- GitLab