diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c910b146b79a00ccb9213a2cf3ced8a3244977b..7ab9548599698000d3727a5549b530fa07975646 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -178,7 +178,7 @@ if(HULC.BUILD_NUMERIC_TESTS) endif() ############################################################# -### Annas Traffis Sim ### +### Annas Traffic Sim ### ############################################################# add_subdirectory(targets/libs/Traffic) diff --git a/src/GridGenerator/StreetPointFinder/JunctionReader.cpp b/src/GridGenerator/StreetPointFinder/JunctionReader.cpp index 3e813247e78f76055b5660dc086643427f027cdf..a6dd1eed6b8a7f75efc0098d327e20390742131e 100644 --- a/src/GridGenerator/StreetPointFinder/JunctionReader.cpp +++ b/src/GridGenerator/StreetPointFinder/JunctionReader.cpp @@ -2,6 +2,7 @@ #include <fstream> #include <iostream> +#include <string> JunctionInReader::JunctionInReader(std::vector<uint> inCells, std::vector<uint> outCells, std::vector<int> carCanNotEnterThisOutCell) : @@ -20,26 +21,28 @@ void JunctionReader::readJunctions(std::string filename, StreetPointFinder stree uint numberOfJunctions; file >> numberOfJunctions; + std::string inOutDummy; int streetIndex; - char startOrEnd; for (uint i = 0; i < numberOfJunctions; i++) { std::vector<uint> inCells, outCells; std::vector<int> carCanNotEnterThisOutCell; //inCells + file >> inOutDummy; for (uint i = 0; i < 4; i++) { - file >> streetIndex >> startOrEnd; + file >> streetIndex; if (streetIndex >= 0) - inCells.push_back(getCellIndex(streetIndex, startOrEnd)); + inCells.push_back(getCellIndex(streetIndex, 'e')); } //outCells + file >> inOutDummy; for (uint i = 0; i < 4; i++) { - file >> streetIndex >> startOrEnd; + file >> streetIndex; if (streetIndex >= 0) { - outCells.push_back(getCellIndex(streetIndex, startOrEnd)); - carCanNotEnterThisOutCell.push_back(getCellIndex(streetIndex, startOrEnd)); + outCells.push_back(getCellIndex(streetIndex, 's')); + carCanNotEnterThisOutCell.push_back(getCellIndex(streetIndex, 's')); }else if(streetIndex == -2) //allow u-turn carCanNotEnterThisOutCell.push_back(-2); } diff --git a/src/GridGenerator/StreetPointFinder/SinkReader.cpp b/src/GridGenerator/StreetPointFinder/SinkReader.cpp index dfe1967509a62ebb848af7e1e001be04009b9ac1..1a275c759d086486c3f97a16611b0ec80aabcfd2 100644 --- a/src/GridGenerator/StreetPointFinder/SinkReader.cpp +++ b/src/GridGenerator/StreetPointFinder/SinkReader.cpp @@ -21,17 +21,16 @@ void SinkReader::readSinks(std::string filename, StreetPointFinder streetPointFi file >> numberOfSinks; uint streetIndex; - char startOrEnd; float sinkBlockedPossibility; for (uint i = 0; i < numberOfSinks; i++) { - file >> streetIndex >> startOrEnd >> sinkBlockedPossibility; - sinks.push_back(SinkInReader(getCellIndex(streetIndex, startOrEnd), sinkBlockedPossibility)); + file >> streetIndex >> sinkBlockedPossibility; + sinks.push_back(SinkInReader(getCellIndexEnd(streetIndex), sinkBlockedPossibility)); } } -unsigned int SinkReader::getCellIndex(unsigned int streetIndex, char startOrEnd) +unsigned int SinkReader::getCellIndexEnd(unsigned int streetIndex) { uint i = 0; unsigned int cellIndex = 0; @@ -39,8 +38,6 @@ unsigned int SinkReader::getCellIndex(unsigned int streetIndex, char startOrEnd) cellIndex += streetPointFinder.streets[i].numberOfCells; ++i; } - if (startOrEnd == 's') { - return cellIndex; - } + return cellIndex + streetPointFinder.streets[streetIndex].numberOfCells - 1; } diff --git a/src/GridGenerator/StreetPointFinder/SinkReader.h b/src/GridGenerator/StreetPointFinder/SinkReader.h index e4db88dc51683bdf0b20706374b2795840b7a7c3..bc49c159f9b68f0a50288bd09f74dfa70061cac7 100644 --- a/src/GridGenerator/StreetPointFinder/SinkReader.h +++ b/src/GridGenerator/StreetPointFinder/SinkReader.h @@ -24,7 +24,7 @@ struct VF_PUBLIC SinkReader void readSinks(std::string filename, StreetPointFinder streetPointFinder); private: - unsigned int getCellIndex(unsigned int streetIndex, char startOrEnd); + unsigned int getCellIndexEnd(unsigned int streetIndex); }; diff --git a/src/GridGenerator/StreetPointFinder/SourceReader.cpp b/src/GridGenerator/StreetPointFinder/SourceReader.cpp index 25c67f36e40f35e840de78f174fd81174c51f22f..85bcdc5a90111d09d119be154e844581f1d3eebb 100644 --- a/src/GridGenerator/StreetPointFinder/SourceReader.cpp +++ b/src/GridGenerator/StreetPointFinder/SourceReader.cpp @@ -22,18 +22,17 @@ void SourceReader::readSources(std::string filename, StreetPointFinder streetPoi file >> numberOfSources; uint streetIndex; - char startOrEnd; float sourcePossibility; for (uint i = 0; i < numberOfSources; i++) { - file >> streetIndex >> startOrEnd >> sourcePossibility; - sources.push_back(SourceInReader(getCellIndex(streetIndex, startOrEnd), sourcePossibility)); + file >> streetIndex >> sourcePossibility; + sources.push_back(SourceInReader(getCellIndexStart(streetIndex), sourcePossibility)); } } -unsigned int SourceReader::getCellIndex(unsigned int streetIndex, char startOrEnd) +unsigned int SourceReader::getCellIndexStart(unsigned int streetIndex) { uint i = 0; unsigned int cellIndex = 0; @@ -41,8 +40,7 @@ unsigned int SourceReader::getCellIndex(unsigned int streetIndex, char startOrEn cellIndex += streetPointFinder.streets[i].numberOfCells; ++i; } - if (startOrEnd == 's') { - return cellIndex; - } - return cellIndex + streetPointFinder.streets[streetIndex].numberOfCells - 1; + return cellIndex; } + + diff --git a/src/GridGenerator/StreetPointFinder/SourceReader.h b/src/GridGenerator/StreetPointFinder/SourceReader.h index c4816e636c66f10e6772f6d411ea35b54400e381..9f9225760436c4303c0d825dd60217edab37c3f9 100644 --- a/src/GridGenerator/StreetPointFinder/SourceReader.h +++ b/src/GridGenerator/StreetPointFinder/SourceReader.h @@ -24,7 +24,7 @@ struct VF_PUBLIC SourceReader void readSources(std::string filename, StreetPointFinder streetPointFinder); private: - unsigned int getCellIndex(unsigned int streetIndex, char startOrEnd); + unsigned int getCellIndexStart(unsigned int streetIndex); }; diff --git a/src/Traffic/Junction/Junction.h b/src/Traffic/Junction/Junction.h index 4772f3b5c5c465bdbd1edeb9b0c3e4b966bb669e..39d55fd9fe185b77373dfd1e5ecb5b4cc76e4d33 100644 --- a/src/Traffic/Junction/Junction.h +++ b/src/Traffic/Junction/Junction.h @@ -5,24 +5,22 @@ #include "JunctionData.h" -class OneWayRoadSSJ; +class TrafficMovement; class VF_PUBLIC Junction { public: - virtual ~Junction() {}; - - virtual void checkOutCellIndices(unsigned int roadLength) = 0; + virtual void checkOutCellIndices(uint roadLength) = 0; virtual void setCellIndexForNoUTurn(std::vector<int> carCanNotEnterThisOutCell) = 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 bool acceptsCar(uint cellIndex) = 0; //determines if a car can enter the junction + virtual void registerCar(uint cellIndex, uint numberOfCellsAlreadyMoved, uint speed) = 0; //registers all cars entering the junction + virtual void calculateTimeStep(TrafficMovement& road) = 0; virtual void updateJunction() = 0; - virtual const std::vector<unsigned int>& getInCellIndices() const = 0; + virtual const std::vector<uint>& getInCellIndices() const = 0; - virtual void dispJunction(const unsigned int index, unsigned int roadLength) const = 0; - virtual const unsigned int getNumCarsOnJunction() const = 0; + virtual void dispJunction(const uint index, uint roadLength) const = 0; + virtual const uint getNumCarsOnJunction() const = 0; }; \ No newline at end of file diff --git a/src/Traffic/Junction/JunctionData.h b/src/Traffic/Junction/JunctionData.h index b4f92abacff22b0b9a555e52aa56d7d9a484c5e7..58c31186796b90dfc3a20751353ff4779a0bcaf0 100644 --- a/src/Traffic/Junction/JunctionData.h +++ b/src/Traffic/Junction/JunctionData.h @@ -8,19 +8,19 @@ struct VF_PUBLIC JunctionData { public: - std::vector<unsigned int> inCellIndices; - std::vector<unsigned int> outCellIndices; + std::vector<uint> inCellIndices; + std::vector<uint> outCellIndices; std::vector<int> carCanNotEnterThisOutCell; //no such inCell: -2 - std::vector<unsigned int> freeOutCells; + std::vector<uint> possibleOutCells; std::vector<bool> carCanEnter; std::vector<int> carsOnJunction; - std::vector<unsigned int> alreadyMoved; + std::vector<uint> alreadyMoved; std::mt19937 engine = RandomHelper::make_engine(); - uniform_int_distribution<unsigned int> distInt2{ 0, 1 }; - uniform_int_distribution<unsigned int> distInt3{ 0, 2 }; - uniform_int_distribution<unsigned int> distInt4{ 0, 3 }; + std::uniform_int_distribution<uint> distInt2{ 0, 1 }; + std::uniform_int_distribution<uint> distInt3{ 0, 2 }; + std::uniform_int_distribution<uint> distInt4{ 0, 3 }; }; diff --git a/src/Traffic/Junction/JunctionSimple.cpp b/src/Traffic/Junction/JunctionRandom.cpp similarity index 52% rename from src/Traffic/Junction/JunctionSimple.cpp rename to src/Traffic/Junction/JunctionRandom.cpp index 6b1e789c64966b73bbc1e5cd639d2bf545426191..b840f0f4460d4512554271a18d0c0a3beb871fd4 100644 --- a/src/Traffic/Junction/JunctionSimple.cpp +++ b/src/Traffic/Junction/JunctionRandom.cpp @@ -1,26 +1,26 @@ -#include "JunctionSimple.h" +#include "JunctionRandom.h" -JunctionSimple::JunctionSimple(const vector<unsigned int> &inCellIndices, const vector<unsigned int> &outCellIndices) +JunctionRandom::JunctionRandom(const std::vector<uint> &inCellIndices, const std::vector<uint> &outCellIndices) { data.inCellIndices = inCellIndices; data.outCellIndices = outCellIndices; - unsigned int inRoads = inCellIndices.size(); + uint inRoads = inCellIndices.size(); data.carCanEnter.resize(inRoads); - fill(data.carCanEnter.begin(), data.carCanEnter.end(), true); + std::fill(data.carCanEnter.begin(), data.carCanEnter.end(), true); data.carsOnJunction.resize(inRoads); - fill(data.carsOnJunction.begin(), data.carsOnJunction.end(), -1); + std::fill(data.carsOnJunction.begin(), data.carsOnJunction.end(), -1); data.alreadyMoved.resize(inRoads); - fill(data.alreadyMoved.begin(), data.alreadyMoved.end(), 0); + std::fill(data.alreadyMoved.begin(), data.alreadyMoved.end(), 0); } -void JunctionSimple::setCellIndexForNoUTurn(std::vector<int> carCanNotEnterThisOutCell) +void JunctionRandom::setCellIndexForNoUTurn(std::vector<int> carCanNotEnterThisOutCell) { try { @@ -28,30 +28,30 @@ void JunctionSimple::setCellIndexForNoUTurn(std::vector<int> carCanNotEnterThisO data.carCanNotEnterThisOutCell = carCanNotEnterThisOutCell; } - catch (const exception& e) { - cerr << e.what() << endl; - cin.get(); + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); exit(EXIT_FAILURE); } } -bool JunctionSimple::acceptsCar(unsigned int cellIndex) +bool JunctionRandom::acceptsCar(uint cellIndex) { return data.carCanEnter[getInCellsVectorIndex(cellIndex)]; } -void JunctionSimple::registerCar(unsigned int cellIndex, unsigned int numberOfCellsAlreadyMoved, unsigned int speed) +void JunctionRandom::registerCar(uint cellIndex, uint numberOfCellsAlreadyMoved, uint speed) { - unsigned int index = getInCellsVectorIndex(cellIndex); + uint index = getInCellsVectorIndex(cellIndex); data.carsOnJunction[index] = speed; data.carCanEnter[index] = false; data.alreadyMoved[index] = numberOfCellsAlreadyMoved; } -unsigned int JunctionSimple::getInCellsVectorIndex(unsigned int cellIndex) +uint JunctionRandom::getInCellsVectorIndex(uint cellIndex) { try { auto it = find(data.inCellIndices.begin(), data.inCellIndices.end(), cellIndex); @@ -61,23 +61,23 @@ unsigned int JunctionSimple::getInCellsVectorIndex(unsigned int cellIndex) return distance(data.inCellIndices.begin(), it); } - throw runtime_error("The passed cell is not an incoming cell to this junction."); + throw std::runtime_error("The passed cell is not an incoming cell to this junction."); } - catch (const exception& e) { - cerr << e.what() << endl; - cin.get(); + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); exit(EXIT_FAILURE); } } -void JunctionSimple::updateJunction() +void JunctionRandom::updateJunction() { - data.freeOutCells = data.outCellIndices; + data.possibleOutCells = data.outCellIndices; } -void JunctionSimple::calculateTimeStep(OneWayRoadSSJ& road) +void JunctionRandom::calculateTimeStep(TrafficMovement& road) { index = 0; for (int carSpeed : data.carsOnJunction) { @@ -85,7 +85,7 @@ void JunctionSimple::calculateTimeStep(OneWayRoadSSJ& road) if (carSpeed >= 0) { //check if there is a car on the junction if (carSpeed == 0) - cerr << "speed on junction was 0" << endl; + std::cerr << "speed on junction was 0" << std::endl; else applyRules(carSpeed, index, road); data.alreadyMoved[index] = 0; @@ -96,9 +96,9 @@ void JunctionSimple::calculateTimeStep(OneWayRoadSSJ& road) } -void JunctionSimple::applyRules(int & carSpeed, const int & index, OneWayRoadSSJ& road) +void JunctionRandom::applyRules(int & carSpeed, const int & index, TrafficMovement& road) { - remainingDistance = static_cast<unsigned int>(carSpeed) - data.alreadyMoved[index]; + remainingDistance = static_cast<uint>(carSpeed) - data.alreadyMoved[index]; if (remainingDistance > 0) { outCell = chooseOutCell(index); @@ -116,7 +116,7 @@ void JunctionSimple::applyRules(int & carSpeed, const int & index, OneWayRoadSSJ } -void JunctionSimple::breakCar(unsigned int outCellIndex, int &speed, unsigned int &remainingDistance, OneWayRoadSSJ& road) +void JunctionRandom::breakCar(uint outCellIndex, int &speed, uint &remainingDistance, TrafficMovement& road) { gap = road.getGapAfterOutCell(outCellIndex, remainingDistance); if (gap < remainingDistance) { @@ -126,7 +126,7 @@ void JunctionSimple::breakCar(unsigned int outCellIndex, int &speed, unsigned in } -void JunctionSimple::moveCar(unsigned int outCell, int & carSpeed, const int & index, OneWayRoadSSJ& road) +void JunctionRandom::moveCar(uint outCell, int & carSpeed, const int & index, TrafficMovement& road) { road.moveJunctionCar(outCell, remainingDistance, carSpeed); data.carsOnJunction[index] = -1; @@ -134,21 +134,21 @@ void JunctionSimple::moveCar(unsigned int outCell, int & carSpeed, const int & i } -int JunctionSimple::chooseOutCell(const int & index) +int JunctionRandom::chooseOutCell(const int & index) { - vector<unsigned int> freeOutCellsTemp; + std::vector<uint> outCellsTemp; if (data.carCanNotEnterThisOutCell.size() > 0 && data.carCanNotEnterThisOutCell[index]) { - for (unsigned int cell : data.freeOutCells) { + for (uint cell : data.possibleOutCells) { if (cell != data.carCanNotEnterThisOutCell[index]) - freeOutCellsTemp.push_back(cell); + outCellsTemp.push_back(cell); } } else - freeOutCellsTemp = data.freeOutCells; + outCellsTemp = data.possibleOutCells; - switch (freeOutCellsTemp.size()) { + switch (outCellsTemp.size()) { case 0: return -1; case 1: @@ -165,33 +165,33 @@ int JunctionSimple::chooseOutCell(const int & index) break; } - outCell = freeOutCellsTemp[random]; - data.freeOutCells.erase(std::remove(data.freeOutCells.begin(), data.freeOutCells.end(), outCell), data.freeOutCells.end()); + outCell = outCellsTemp[random]; + data.possibleOutCells.erase(std::remove(data.possibleOutCells.begin(), data.possibleOutCells.end(), outCell), data.possibleOutCells.end()); return outCell; } -const vector<unsigned int>& JunctionSimple::getInCellIndices() const +const std::vector<uint>& JunctionRandom::getInCellIndices() const { return data.inCellIndices; } -void JunctionSimple::dispJunction(const unsigned int index, unsigned int roadLength) const +void JunctionRandom::dispJunction(const uint index, uint roadLength) const { if (find(data.inCellIndices.begin(), data.inCellIndices.end(), (roadLength - index - 1)) != data.inCellIndices.end()) { - cout << setw(4) << "in"; + std::cout << std::setw(4) << "in"; } else if (find(data.outCellIndices.begin(), data.outCellIndices.end(), (roadLength - index - 1)) != data.outCellIndices.end()) { - cout << setw(4) << "out"; + std::cout << std::setw(4) << "out"; } else { - cout << setw(4) << " "; + std::cout << std::setw(4) << " "; } } -const unsigned int JunctionSimple::getNumCarsOnJunction() const +const uint JunctionRandom::getNumCarsOnJunction() const { - unsigned int num = 0; + uint num = 0; for (auto car : data.carsOnJunction) { if (car >= 0) ++num; @@ -200,15 +200,15 @@ const unsigned int JunctionSimple::getNumCarsOnJunction() const } -void JunctionSimple::checkOutCellIndices(unsigned int roadLength) +void JunctionRandom::checkOutCellIndices(uint roadLength) { try { - for (unsigned int cell : data.outCellIndices) + for (uint 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(); + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); exit(EXIT_FAILURE); } } \ No newline at end of file diff --git a/src/Traffic/Junction/JunctionRandom.h b/src/Traffic/Junction/JunctionRandom.h new file mode 100644 index 0000000000000000000000000000000000000000..67808d7bd55864393c62e2ae218d37ce16dda17c --- /dev/null +++ b/src/Traffic/Junction/JunctionRandom.h @@ -0,0 +1,57 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <vector> +#include <iostream> +#include <random> +#include <algorithm> + +#include "Utilities/invalidInput_error.h" +#include "Utilities/VectorHelper.h" +#include "Junction.h" +#include "TrafficMovement.h" + + +class VF_PUBLIC JunctionRandom : + public Junction +{ + +private: + JunctionData data; + +public: + JunctionRandom(const std::vector<uint> &inCellIndices, const std::vector<uint> &outCellIndices); + ~JunctionRandom() {}; + + virtual void setCellIndexForNoUTurn(std::vector<int> carCanNotEnterThisOutCell); + + virtual bool acceptsCar(uint cellIndex); //determines if a car can enter the junction + virtual void registerCar(uint cellIndex, uint numberOfCellsAlreadyMoved, uint speed); //registers all cars entering the junction + virtual void calculateTimeStep(TrafficMovement& road); + virtual void updateJunction(); + + virtual const std::vector<uint>& getInCellIndices() const; + + virtual void dispJunction(const uint index, uint roadLength) const; + virtual const uint getNumCarsOnJunction() const; + + virtual void checkOutCellIndices(uint roadLength); + +private: + uint getInCellsVectorIndex(uint cellIndex); + + void applyRules(int &carSpeed,const int &index, TrafficMovement& road); + void breakCar(uint outCellIndex, int &speed, uint &remainingDistance, TrafficMovement& road); + void moveCar(uint outCell, int & carSpeed, const int & index, TrafficMovement& road); + int chooseOutCell(const int & index); + + +private: + //variables for temporaray calculations + uint remainingDistance; + int outCell; + uint random; + uint gap; + int index; +}; + diff --git a/src/Traffic/Junction/JunctionSimple.h b/src/Traffic/Junction/JunctionSimple.h deleted file mode 100644 index cf455de6fe83df0c25b39828ee6595b35c66b078..0000000000000000000000000000000000000000 --- a/src/Traffic/Junction/JunctionSimple.h +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once -#include <VirtualFluidsDefinitions.h> - -#include <vector> -#include <iostream> -#include <random> -#include <algorithm> - -#include "Utilities/invalidInput_error.h" -#include "Utilities/VectorHelper.h" -#include "Junction.h" -#include "OneWayRoadSSJ.h" - -using namespace std; - -class VF_PUBLIC JunctionSimple : - public Junction -{ - -private: - JunctionData data; - -public: - JunctionSimple(const vector<unsigned int> &inCellIndices, const vector<unsigned int> &outCellIndices); - ~JunctionSimple() {}; - - virtual void setCellIndexForNoUTurn(vector<int> carCanNotEnterThisOutCell); - - 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(unsigned int outCell, int & carSpeed, const int & index, OneWayRoadSSJ& road); - int chooseOutCell(const int & index); - - -private: - //variables for temporaray calculations - unsigned int remainingDistance; - int outCell; - unsigned int random; - unsigned int gap; - int index; -}; - diff --git a/src/Traffic/OneWayRoad.cpp b/src/Traffic/OneWayRoad.cpp deleted file mode 100644 index 8a7b6d4639370a276f40d047b1f0fb2c92c29d07..0000000000000000000000000000000000000000 --- a/src/Traffic/OneWayRoad.cpp +++ /dev/null @@ -1,402 +0,0 @@ -#include "OneWayRoad.h" -#include <stdexcept> - -OneWayRoad::OneWayRoad() -{ -} - - -OneWayRoad::OneWayRoad(unique_ptr<RoadNetworkData> road, const float dawdlePossibility) -{ - this->road = move(road); - - pcurrent = &this->road->current; - pnext = &(this->road->next); - - checkCurrentForSafetyDistance(); - - initDawdle(dawdlePossibility); -} - - -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); -} - - -void OneWayRoad::setSaveResults(bool saveResults) -{ - if (saveResults) { - this->saveResults = true; - initResults(); - } -} - - -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::initCalculation(int timeSteps) -{ - this->timeSteps = timeSteps; - - initResultsForCalculation(); - - putCurrentIntoResults(0); -} - -void OneWayRoad::initResultsForCalculation() -{ - if (!saveResults) return; - - for (unsigned int i = 0; i < road->roadLength; i++) { - results[i].resize(timeSteps + 1); - } - VectorHelper::fillVector(results, -1); -} - - -void OneWayRoad::loopTroughTimesteps() -{ - for (unsigned int step = 1; step < timeSteps + 1; step++) { - calculateTimestep(step); - } -} - - -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); - currentStep += 1; -} - -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) -{ - if (!saveResults) return; - - 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; - cin.get(); - exit(EXIT_FAILURE); - } - catch (...) { - cerr << "unknown exception while writing to file" << endl; - cin.get(); - exit(EXIT_FAILURE); - } -} - - - -void OneWayRoad::dispCurrentRoad() const -{ - cout << "current: ( step: " << currentStep << " )" << endl; - VectorHelper::dispVectorColour(*pcurrent); -} - -void OneWayRoad::dispResults() -{ - if (!saveResults) { - std::cout << "No results were saved" << std::endl; - return; - } - - cout << "results:" << endl; - - visualizeSafetyDistanceForConsole(); - reverse(results.begin(), results.end()); - VectorHelper::dispVectorColour(results); -} - - -void OneWayRoad::checkCurrentForSafetyDistance() -{ - if (road->safetyDistance > 0) { - unsigned int neighbor; - for (unsigned int i = 0; i < road->roadLength; i++) { - if ((*pcurrent)[i] > -1) { - neighbor = road->neighbors[i]; - for (unsigned int j = 1; j <= road->safetyDistance; j++) { - if ((*pcurrent)[neighbor] > -1) { - std::cerr << "timestep 0: safetyDistance was violated: carIndex: " << i << std::endl; - break; - } - neighbor = road->neighbors[neighbor]; - } - } - } - } -} - - -void OneWayRoad::visualizeSafetyDistanceForConsole() -{ - 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]; - } - } - } - } - } -} - - -const std::vector<int>& OneWayRoad::getVehiclesForVTK() -{ - return road->currentWithLongVehicles; -} - -void OneWayRoad::visualizeVehicleLengthForVTK() -{ - road->currentWithLongVehicles = *pcurrent; - - if (road->safetyDistance != 0) { - for (unsigned int i = 0; i < road->roadLength; i++) { - if ((*pcurrent)[i] > -1) { - int neighbor = road->neighbors[i]; - for (unsigned int j = 1; j <= road->safetyDistance; j++) { - if ((*pcurrent)[neighbor] > -1) { - cerr << "safetyDistance was violated: timestep: " << currentStep << "\t carIndex: " << i << endl; - break; - } - else - (road->currentWithLongVehicles)[neighbor] = (*pcurrent)[i]; - neighbor = road->neighbors[neighbor]; - } - } - } - } -} \ No newline at end of file diff --git a/src/Traffic/OneWayRoad.h b/src/Traffic/OneWayRoad.h deleted file mode 100644 index ff4e4baf1d17385403b015787f4b7671969b4776..0000000000000000000000000000000000000000 --- a/src/Traffic/OneWayRoad.h +++ /dev/null @@ -1,99 +0,0 @@ -#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 "Utilities/invalidInput_error.h" -#include "Utilities/VectorHelper.h" -#include "Utilities/RandomHelper.h" -#include "RoadNetwork/RoadNetworkData.h" -#include "Output/ConcentrationOutwriter.h" - -using namespace std; - -class VF_PUBLIC OneWayRoad //Periodic BC -{ -protected: - unique_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; - - bool saveResults = false; - vector<vector<int> > results; //saves the results of the calculation; x-axis = timesteps, y axis = positions - - unique_ptr<ConcentrationOutwriter> concWriter = nullptr; - -public: - OneWayRoad(unique_ptr<RoadNetworkData> road, const float dawdlePossibility); - OneWayRoad(); - OneWayRoad(const OneWayRoad&) = delete; - virtual ~OneWayRoad(); - - void setSlowToStart(const float slowStartPossibility); - void setConcentrationOutwriter(unique_ptr<ConcentrationOutwriter> writer); - void setSaveResults(bool saveResults); - - virtual void initCalculation(int timeSteps); - virtual void calculateTimestep(unsigned int step); - void loopTroughTimesteps(); - - 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; - - virtual void visualizeVehicleLengthForVTK(); - virtual const std::vector<int>& getVehiclesForVTK(); - -protected: - void initDawdle(const float dawdlePossibility); - void initResults(); - - void initResultsForCalculation(); - - - 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); - - void checkCurrentForSafetyDistance(); - virtual void visualizeSafetyDistanceForConsole(); - -protected: - //temporary varables for calculation - unsigned int gap; - double randomNumber; -}; - diff --git a/src/Traffic/OneWayRoadSSJ.cpp b/src/Traffic/OneWayRoadSSJ.cpp deleted file mode 100644 index 5fa6c52f32c27b118e99c0ea65cf8b37234d685e..0000000000000000000000000000000000000000 --- a/src/Traffic/OneWayRoadSSJ.cpp +++ /dev/null @@ -1,306 +0,0 @@ -#include "OneWayRoadSSJ.h" - -OneWayRoadSSJ::OneWayRoadSSJ(unique_ptr<RoadNetworkData> road, const float dawdlePossibility) -{ - this->road = move(road); - - pcurrent = &this->road->current; - pnext = &(this->road->next); - - checkCurrentForSafetyDistance(); - - initDawdle(dawdlePossibility); -} - -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); - currentStep += 1; -} - - -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() -{ - if (!saveResults) { - std::cout << "No results were saved" << std::endl; - return; - } - - visualizeSafetyDistanceForConsole(); - 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::visualizeSafetyDistanceForConsole() { - 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::visualizeVehicleLengthForVTK() -{ - road->currentWithLongVehicles = *pcurrent; - - if (road->safetyDistance != 0) { - for (unsigned int i = 0; i < road->roadLength; i++) { - if ((*pcurrent)[i] > -1) { - int neighbor = road->neighbors[i]; - for (unsigned int j = 1; j <= road->safetyDistance; j++) { - if (neighbor <= -1000) - break; - if ((*pcurrent)[neighbor] > -1) { - cerr << "safetyDistance was violated: timestep: " << currentStep << "\t carIndex: " << i << endl; - break; - } - else - (road->currentWithLongVehicles)[neighbor] = (*pcurrent)[i]; - 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 deleted file mode 100644 index 36d6c83c76389940056a32982994f3ae54c3c1f9..0000000000000000000000000000000000000000 --- a/src/Traffic/OneWayRoadSSJ.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once -#include <VirtualFluidsDefinitions.h> - -#include "OneWayRoad.h" - -class VF_PUBLIC OneWayRoadSSJ : - public OneWayRoad -{ - -public: - OneWayRoadSSJ(unique_ptr<RoadNetworkData> road, const float dawdlePossibility); - OneWayRoadSSJ(); - virtual ~OneWayRoadSSJ(); - - virtual void calculateTimestep(unsigned int step); - - 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 - virtual void visualizeVehicleLengthForVTK(); - -private: - - 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 visualizeSafetyDistanceForConsole(); - void dispJunctionsAtCell(unsigned int index) const; - void dispSinksAtCell(unsigned int index) const; - void dispSourcesAtCell(unsigned int index) const; -}; diff --git a/src/Traffic/Output/CarDisplay.cpp b/src/Traffic/Output/CarDisplay.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a22d2ae0aa82203bb8e91b63c8398bd809760c3 --- /dev/null +++ b/src/Traffic/Output/CarDisplay.cpp @@ -0,0 +1,159 @@ +#include "CarDisplay.h" + +CarDisplay::CarDisplay(std::vector<int> **pcurrent, const uint safetyDistance): + safetyDistance{ safetyDistance } +{ + this->ppcurrent = pcurrent; + roadLength = (*pcurrent)->size(); +} + + +void CarDisplay::initResults(uint timeSteps) +{ + this->timeSteps = timeSteps; + + results.resize(roadLength, std::vector<int>(1)); + + for (uint i = 0; i < roadLength; i++) { + results[i].resize(timeSteps + 1); + } + + VectorHelper::fillVector(results, -1); + putCurrentIntoResults(0); +} + + +void CarDisplay::putCurrentIntoResults(uint step) +{ + writingStep = step; + for (uint i = 0; i < roadLength; i++) + results[i][writingStep] = (**ppcurrent)[i]; +} + + +void CarDisplay::writeResultsToFile() const +{ + try { + + + std::fstream outFile("results.txt", std::fstream::out | std::fstream::trunc); + if (outFile.is_open()) + { + for (uint i = 0; i < results.size(); i++) { + for (uint j = 0; j < results[i].size() - 1; j++) + outFile << results[i][j] << " "; + + outFile << results[i][results[i].size() - 1]; + outFile << std::endl; + } + std::cout << "Finished writing data to file" << std::endl; + } + + + else + throw std::runtime_error("Couldn't open file"); + + } + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); + exit(EXIT_FAILURE); + } + catch (...) { + std::cerr << "unknown exception while writing to file" << std::endl; + std::cin.get(); + exit(EXIT_FAILURE); + } +} + + +void CarDisplay::dispCurrentRoad() const +{ + std::cout << "current: ( step: " << writingStep << " )" << std::endl; + VectorHelper::dispVectorColour(**ppcurrent); +} + + +void CarDisplay::dispResults(const std::vector<int> * neighbors, const std::vector<std::shared_ptr<Sink> > & sinks, const std::vector<std::shared_ptr<Junction> > & junctions, const std::vector<std::shared_ptr<Source> > & sources) +{ + visualizeSafetyDistanceForConsole(neighbors); + reverse(results.begin(), results.end()); + + for (uint i = 0; i < results.size(); i++) { + + dispJunctionsAtCell(i, junctions); + dispSinksAtCell(i, sinks); + dispSourcesAtCell(i, sources); + + for (uint j = 0; j < results[i].size(); j++) { + VectorHelper::makeVectorOutputColourful(results[i][j]); + std::cout << std::setw(4) << results[i][j]; + } + + std::cout << std::endl; + } + std::cout << std::endl; + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // set output default white 7; +} + + +void CarDisplay::dispJunctionsAtCell(uint index, const std::vector<std::shared_ptr<Junction> > & junctions) const +{ + for (auto& junc : junctions) { + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // set output default white 7; + junc->dispJunction(index, roadLength); + } +} + + +void CarDisplay::dispSinksAtCell(uint index, const std::vector<std::shared_ptr<Sink> > & sinks) const +{ + for (auto& sink : sinks) { + if (sink->getIndex() == roadLength - index - 1) { + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); //set output bright green 10, bright red 12 + std::cout << std::setw(4) << 1 - (sink->getPossibilityBeingBlocked()); + return; + } + std::cout << std::setw(4) << " "; + } +} + + +void CarDisplay::dispSourcesAtCell(uint index, const std::vector<std::shared_ptr<Source> > & sources) const +{ + for (auto& source : sources) { + if (source->getIndex() == roadLength - index - 1) { + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); //set output bright green 10, bright red 12 + std::cout << std::setw(4) << source->getPossibility(); + return; + } + std::cout << std::setw(4) << " "; + } +} + + +void CarDisplay::visualizeSafetyDistanceForConsole(const std::vector<int>* neighbors) +{ + if (safetyDistance != 0) { + int neighbor; + for (uint step = 0; step <= timeSteps; step++) { + for (uint i = 0; i < roadLength; i++) { + if (results[i][step] > -1) { + neighbor = (*neighbors)[i]; + for (uint j = 1; j <= safetyDistance; j++) { + //junction or sink + if (neighbor <= -1000) + break; + if (results[neighbor][step] > -1) { + std::cerr << "safetyDistance was violated: timestep: " << step << "\t carIndex: " << i << std::endl; + break; + } + else + results[neighbor][step] = -5; + neighbor = (*neighbors)[neighbor]; + } + } + } + } + } +} diff --git a/src/Traffic/Output/CarDisplay.h b/src/Traffic/Output/CarDisplay.h new file mode 100644 index 0000000000000000000000000000000000000000..34e84fb7d1a8cdc503eb4d701d9359123d76851c --- /dev/null +++ b/src/Traffic/Output/CarDisplay.h @@ -0,0 +1,47 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <fstream> +#include <iostream> +#include <vector> +#include <memory> +#include <iomanip> //formatting output streams +#include <windows.h> //for colourful console output +#include <stdexcept> + +#include "Sink/Sink.h" +#include "Source/Source.h" +#include "Junction/Junction.h" + +#include "Utilities/VectorHelper.h" + +class VF_PUBLIC CarDisplay { +public: + CarDisplay(std::vector<int> **pcurrent, const uint safetyDistance); + ~CarDisplay() {}; + + void initResults(uint timeSteps); + + void dispCurrentRoad() const; + void dispResults(const std::vector<int> * neighbors, const std::vector<std::shared_ptr<Sink> > & sinks, const std::vector<std::shared_ptr<Junction> > & junctions, const std::vector<std::shared_ptr<Source> > & sources); + void writeResultsToFile() const; + + void putCurrentIntoResults(uint step); + +private: + void visualizeSafetyDistanceForConsole(const std::vector<int> * neighbors); + + void dispJunctionsAtCell(uint index, const std::vector<std::shared_ptr<Junction> > & junctions) const; + void dispSinksAtCell(uint index, const std::vector<std::shared_ptr<Sink> > & sinks) const; + void dispSourcesAtCell(uint index, const std::vector<std::shared_ptr<Source> > & sources) const; + +private: + std::vector<std::vector<int> > results; //saves the results of the calculation; x-axis = timesteps, y axis = positions + + std::vector<int> **ppcurrent; + uint roadLength; + const uint safetyDistance; + + uint timeSteps; + uint writingStep; +}; \ No newline at end of file diff --git a/src/Traffic/Output/ConcBySpeedAndAcceleration.cpp b/src/Traffic/Output/ConcBySpeedAndAcceleration.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91367a473a72a81a95cc45d529cab0d93daa39d5 --- /dev/null +++ b/src/Traffic/Output/ConcBySpeedAndAcceleration.cpp @@ -0,0 +1,14 @@ +#include "ConcBySpeedAndAcceleration.h" + +ConcBySpeedAndAcceleration::ConcBySpeedAndAcceleration(uint roadlength, uint maxSpeed, uint maxAcceleration) +{ + concentration.resize(roadlength); + this->maxAcceleration = static_cast<float>(maxAcceleration); + this->maxSpeed = static_cast<float>(maxSpeed); +} + + +void ConcBySpeedAndAcceleration::calculateConcForSingleCar(uint index, uint speed, uint acceleration) +{ + concentration[index] = static_cast<float>(speed) / maxSpeed; +} \ No newline at end of file diff --git a/src/Traffic/Output/ConcBySpeedAndAcceleration.h b/src/Traffic/Output/ConcBySpeedAndAcceleration.h new file mode 100644 index 0000000000000000000000000000000000000000..6ad2d4f66c71fc30e0c67e22c56ccd208f0318fb --- /dev/null +++ b/src/Traffic/Output/ConcBySpeedAndAcceleration.h @@ -0,0 +1,19 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include "ConcentrationOutwriter.h" + +class VF_PUBLIC ConcBySpeedAndAcceleration : + public ConcentrationOutwriter +{ +public: + ConcBySpeedAndAcceleration(uint roadlength, uint maxSpeed, uint maxAcceleration); + ~ConcBySpeedAndAcceleration() {}; + + virtual void calculateConcForSingleCar(uint index, uint speed, uint acceleration); + +private: + float maxSpeed = 0; + float maxAcceleration; +}; + diff --git a/src/Traffic/Output/ConcentrationByPosition.cpp b/src/Traffic/Output/ConcentrationByPosition.cpp index ea4d136b1c16fa13194260589e414750cfd25eb4..0ddfca0508914bb9be40c4aa9c275b97a33ce530 100644 --- a/src/Traffic/Output/ConcentrationByPosition.cpp +++ b/src/Traffic/Output/ConcentrationByPosition.cpp @@ -2,33 +2,29 @@ -ConcentrationByPosition::ConcentrationByPosition(unsigned int roadlength) +ConcentrationByPosition::ConcentrationByPosition(uint roadlength, uint maxSpeed, uint maxAcceleration) { concentration.resize(roadlength); } -ConcentrationByPosition::~ConcentrationByPosition() -{ -} +//void ConcentrationByPosition::calculateConcFromCarDistribution(const std::vector<int>& currentCarDistribution) +//{ +// for (uint i = 0; i < currentCarDistribution.size(); i++) { +// if (currentCarDistribution[i] >= 0) +// concentration[i] = 1.0; +// else +// concentration[i] = 0.0; +// } +// +// //dispConcentration(); +//} -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() +void ConcentrationByPosition::calculateConcForSingleCar(uint index, uint speed, uint acceleration) { - for (auto cell : concentration) { - std::cout << std::setw(4) << cell; - } - std::cout << std::endl; + concentration[index] = 1.0; } + + diff --git a/src/Traffic/Output/ConcentrationByPosition.h b/src/Traffic/Output/ConcentrationByPosition.h index 01b80aa73da6f555ed74723f19956130d70ffb0e..6f808b829bacecf70e550c29fbab5011e43c4af3 100644 --- a/src/Traffic/Output/ConcentrationByPosition.h +++ b/src/Traffic/Output/ConcentrationByPosition.h @@ -7,15 +7,12 @@ class VF_PUBLIC ConcentrationByPosition: public ConcentrationOutwriter { public: - ConcentrationByPosition(unsigned int roadlength); - ~ConcentrationByPosition(); + ConcentrationByPosition(uint roadlength, uint maxSpeed = 0, uint maxAcceleration = 0); + ~ConcentrationByPosition() {}; - virtual void writeToArray(const std::vector<int> & currentCarDistribution); + virtual void calculateConcForSingleCar(uint index, uint speed = 0, uint acceleration = 0); + virtual void setMaxSpeedAndAcceleration(uint maxSpeed, uint maxAcceleration) {}; -private: - void dispConcentration(); -private: - std::vector<double> concentration; }; diff --git a/src/Traffic/Output/ConcentrationOutwriter.cpp b/src/Traffic/Output/ConcentrationOutwriter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..738cc21f7f94c0ec0efae8a89f0e7f00803ddae3 --- /dev/null +++ b/src/Traffic/Output/ConcentrationOutwriter.cpp @@ -0,0 +1,26 @@ +#include "ConcentrationOutwriter.h" + +void ConcentrationOutwriter::dispConcentration() +{ + for (auto cell : concentration) { + if(cell>0) + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); + else + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8); + std::cout << std::setw(4) << cell; + } + std::cout << std::endl; + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); +} + + +void ConcentrationOutwriter::resetConcVector() +{ + std::fill(concentration.begin(), concentration.end(), 0); +} + + +const std::vector<float> & ConcentrationOutwriter::getConcentrations() +{ + return concentration; +} \ No newline at end of file diff --git a/src/Traffic/Output/ConcentrationOutwriter.h b/src/Traffic/Output/ConcentrationOutwriter.h index b531d6e32c1acb5ed0410e78ca4201388481ea78..45d0da640b031e3d5af1c16e4cd9775fd52ad66d 100644 --- a/src/Traffic/Output/ConcentrationOutwriter.h +++ b/src/Traffic/Output/ConcentrationOutwriter.h @@ -1,14 +1,23 @@ #pragma once -#include <VirtualFluidsDefinitions.h> #include <vector> #include <iostream> - #include <iomanip> //formatting output streams +#include <windows.h> //for colourful console output + +#include <VirtualFluidsDefinitions.h> +#include "Core/DataTypes.h" class VF_PUBLIC ConcentrationOutwriter { public: - virtual void writeToArray(const std::vector<int> & currentCarDistribution) = 0; + virtual void resetConcVector(); + virtual void calculateConcForSingleCar(uint index, uint speed = 0, uint acceleration = 0) = 0; + + void dispConcentration(); + const std::vector<float> & getConcentrations(); + +protected: + std::vector<float> concentration; }; diff --git a/src/Traffic/RoadNetwork/RoadMaker.cpp b/src/Traffic/RoadNetwork/RoadMaker.cpp index 3c07976ba015df8cc8f7013a7661c7fa0545d503..1aeabf67754136113f8ada3d0cd2715ac4a32874 100644 --- a/src/Traffic/RoadNetwork/RoadMaker.cpp +++ b/src/Traffic/RoadNetwork/RoadMaker.cpp @@ -2,9 +2,9 @@ //random vehicle Distribution -RoadMaker::RoadMaker(const unsigned int roadLength, const unsigned int maxVelocity, unsigned int vehicleLength, const float vehicleDensity) +RoadMaker::RoadMaker(const uint roadLength, const uint maxVelocity, uint vehicleLength, const float vehicleDensity) { - uniform_int_distribution<unsigned int> distInt2{ 0, maxVelocity }; + std::uniform_int_distribution<uint> distInt2{ 0, maxVelocity }; distInt = distInt2; this->roadLength = roadLength; @@ -22,7 +22,7 @@ RoadMaker::RoadMaker(const unsigned int roadLength, const unsigned int maxVeloci //given vehicle distribution -RoadMaker::RoadMaker(const std::vector<int> vehicleDistribution, const unsigned int maxVelocity, unsigned int vehicleLength) +RoadMaker::RoadMaker(const std::vector<int> vehicleDistribution, const uint maxVelocity, uint vehicleLength) { this->roadLength = vehicleDistribution.size(); @@ -38,7 +38,7 @@ RoadMaker::RoadMaker(const std::vector<int> vehicleDistribution, const unsigned //empty road -RoadMaker::RoadMaker(const unsigned int roadLength, const unsigned int maxVelocity, unsigned int vehicleLength) +RoadMaker::RoadMaker(const uint roadLength, const uint maxVelocity, uint vehicleLength) { this->roadLength = roadLength; this->maxVelocity = maxVelocity; @@ -66,7 +66,7 @@ void RoadMaker::initNext() void RoadMaker::initNeighbors() { neighbors.resize(roadLength); - for (unsigned int i = 0; i < roadLength - 1; i++) { + for (uint i = 0; i < roadLength - 1; i++) { neighbors[i] = i + 1; } neighbors[roadLength - 1] = 0; @@ -79,6 +79,7 @@ void RoadMaker::initCurrentAsEmpty() VectorHelper::fillVector(current, -1); } + void RoadMaker::initCurrentWithLongVehicles() { currentWithLongVehicles.resize(roadLength); @@ -95,9 +96,9 @@ void RoadMaker::initVehicleDensity(const float vehicleDensity) throw invalidInput_error("The vehicleDensity should be between 0 and 1"); } } - catch (const exception& e) { - cerr << e.what() << endl; - cin.get(); + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); exit(EXIT_FAILURE); } } @@ -106,9 +107,9 @@ void RoadMaker::initVehicleDensity(const float vehicleDensity) 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++) { + for (uint i = safetyDistance; i < roadLength; i++) { double randomNumber = distFloat(engine); - if (randomNumber <= vehicleDensity) { + if (randomNumber <= (vehicleDensity/vehicleLength)) { current[i] = randomSpeed(); i += safetyDistance; } @@ -122,16 +123,16 @@ int RoadMaker::randomSpeed() } -void RoadMaker::initVehicleLength(const unsigned int vehicleLength) +void RoadMaker::initVehicleLength(const uint 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(); + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); exit(EXIT_FAILURE); } } @@ -152,12 +153,12 @@ void RoadMaker::addJunction(std::unique_ptr<Junction>& junction) setJunctionAsNeighbor(junction); this->junctions.push_back(move(junction)); - if (junctions.size() > 999) throw runtime_error("too many junctions"); + if (junctions.size() > 999) throw std::runtime_error("too many junctions"); } - catch (const exception& e) { - cerr << e.what() << endl; - cin.get(); + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); exit(EXIT_FAILURE); } } @@ -168,22 +169,22 @@ void RoadMaker::setJunctionAsNeighbor(std::unique_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(); + std::vector<uint> 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; + std::cout << "The neighboring cell of cell " << cell << " was already definded as sink or junction, no new junction added." << std::endl; else neighbors[cell] = junctionIndex; } } - catch (const exception& e) { - cerr << e.what() << endl; - cin.get(); + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); exit(EXIT_FAILURE); } } @@ -203,13 +204,13 @@ void RoadMaker::addSink(std::unique_ptr<Sink>& sink) setSinkAsNeighbor(sink); this->sinks.push_back(move(sink)); - if (sinks.size() > 999) throw runtime_error("too many sinks"); + if (sinks.size() > 999) throw std::runtime_error("too many sinks"); } - catch (const exception& e) { - cerr << e.what() << endl; - cin.get(); + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); exit(EXIT_FAILURE); } @@ -221,12 +222,12 @@ void RoadMaker::setSinkAsNeighbor(std::unique_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(); + uint 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; + std::cout << "The neighboring cell of cell " << sinkCell << " was already definded as sink or junction, no new sink added." << std::endl; } else { @@ -250,21 +251,21 @@ void RoadMaker::addSource(std::unique_ptr<Source>& source) this->sources.push_back(move(source)); } - catch (const exception& e) { - cerr << e.what() << endl; - cin.get(); + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); exit(EXIT_FAILURE); } } -void RoadMaker::setNeighbor(unsigned int index, unsigned int neighbor) +void RoadMaker::setNeighbor(uint index, uint neighbor) { this->neighbors[index] = neighbor; } -unsigned int RoadMaker::getMaxVelocity() +uint RoadMaker::getMaxVelocity() { return maxVelocity; } diff --git a/src/Traffic/RoadNetwork/RoadMaker.h b/src/Traffic/RoadNetwork/RoadMaker.h index da6a9b722a54e43f20963504a1b12549cf3c2c15..a53aa34f34a36f65f626453c6634917228c1cc6a 100644 --- a/src/Traffic/RoadNetwork/RoadMaker.h +++ b/src/Traffic/RoadNetwork/RoadMaker.h @@ -1,4 +1,4 @@ -#pragma once + #pragma once #include <random> #include "RoadNetworkData.h" #include "Utilities/RandomHelper.h" @@ -8,9 +8,9 @@ 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(const uint roadLength, const uint maxVelocity, uint vehicleLength, const float vehicleDensity); //random vehicle Distribution + RoadMaker(const std::vector<int> vehicleDistribution, const uint maxVelocity, uint vehicleLength); //given vehicle distribution + RoadMaker(const uint roadLength, const uint maxVelocity, uint vehicleLength);//empty road ~RoadMaker(); @@ -21,14 +21,14 @@ public: void setSources(std::vector< std::unique_ptr<Source> > & sources); void addSource(std::unique_ptr<Source> & source); - void setNeighbor(unsigned int index, unsigned int neighbor); // don't use it for setting sinks or junctions! + void setNeighbor(uint index, uint neighbor); // don't use it for setting sinks or junctions! - unsigned int getMaxVelocity(); + uint getMaxVelocity(); private: - mt19937 engine = RandomHelper::make_engine(); - uniform_real_distribution<float> distFloat{ 0.0, 1.0 }; - uniform_int_distribution<unsigned int> distInt{ 0, maxVelocity }; + std::mt19937 engine = RandomHelper::make_engine(); + std::uniform_real_distribution<float> distFloat{ 0.0, 1.0 }; + std::uniform_int_distribution<uint> distInt{ 0, maxVelocity }; private: void initNext(); @@ -37,7 +37,7 @@ private: void initCurrentWithLongVehicles(); void initVehicleDensity(const float vehicleDensity); void initRandomCars(const float vehicleDensity); - void initVehicleLength(const unsigned int vehicleLength); + void initVehicleLength(const uint vehicleLength); int randomSpeed(); void setJunctionAsNeighbor(std::unique_ptr<Junction> & junction); diff --git a/src/Traffic/RoadNetwork/RoadNetworkData.h b/src/Traffic/RoadNetwork/RoadNetworkData.h index 64aa228eaac5ea11794ee7581189443706981dd9..f9777bb3cbd41e57abfb1bc80c2ca0a02a59a9d1 100644 --- a/src/Traffic/RoadNetwork/RoadNetworkData.h +++ b/src/Traffic/RoadNetwork/RoadNetworkData.h @@ -15,20 +15,19 @@ struct VF_PUBLIC RoadNetworkData { public: - //friend class OneWayRoad; - //friend class OneWayRoadSSJ; + //friend class TrafficMovement; - unsigned int roadLength; - unsigned int maxVelocity; - unsigned int vehicleLength; - unsigned int safetyDistance; + uint roadLength; + uint maxVelocity; + uint vehicleLength; + uint safetyDistance; std::vector<int> current; std::vector<int> currentWithLongVehicles; - std::vector<int> next; //for temporary calculations + std::vector<int> next; //for temporary calculations std::vector<int> neighbors; - std::vector<std::shared_ptr<Sink> > sinks; + 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/Sink.h b/src/Traffic/Sink/Sink.h index 2c4f01705d87bf72f9a76699bc960e4299db120d..0fca62684a7e6eb8a5cdace992fc566719626d0f 100644 --- a/src/Traffic/Sink/Sink.h +++ b/src/Traffic/Sink/Sink.h @@ -8,6 +8,6 @@ class VF_PUBLIC Sink public: virtual float getPossibilityBeingBlocked() const = 0; virtual bool carCanEnter() = 0; - virtual unsigned int getIndex() const = 0; + virtual uint getIndex() const = 0; }; diff --git a/src/Traffic/Sink/SinkData.h b/src/Traffic/Sink/SinkData.h index ef66ebae8008a1b1bdd6bbc8b924aafe21f0f839..9fbab5651198d78d76619a27478af7ac04d208a6 100644 --- a/src/Traffic/Sink/SinkData.h +++ b/src/Traffic/Sink/SinkData.h @@ -3,9 +3,9 @@ #include <memory> #include <vector> #include <VirtualFluidsDefinitions.h> - +#include "Core/DataTypes.h" struct VF_PUBLIC SinkData { - unsigned int sinkIndex; + uint sinkIndex; float sinkBlockedPossibility; }; \ No newline at end of file diff --git a/src/Traffic/Sink/SinkSimple.cpp b/src/Traffic/Sink/SinkRandom.cpp similarity index 60% rename from src/Traffic/Sink/SinkSimple.cpp rename to src/Traffic/Sink/SinkRandom.cpp index 31c8a8646937e5ee1ec8af201e53751203b462f8..b16cebfeaf9e74a40feb98ca44025d2a32e2c67c 100644 --- a/src/Traffic/Sink/SinkSimple.cpp +++ b/src/Traffic/Sink/SinkRandom.cpp @@ -1,8 +1,8 @@ -#include "SinkSimple.h" +#include "SinkRandom.h" -SinkSimple::SinkSimple(unsigned int sinkIndex, float sinkBlockedPossibility) +SinkRandom::SinkRandom(uint sinkIndex, float sinkBlockedPossibility) { data.sinkIndex = sinkIndex; @@ -14,27 +14,27 @@ SinkSimple::SinkSimple(unsigned int sinkIndex, float sinkBlockedPossibility) 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(); + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); exit(EXIT_FAILURE); }; } -bool SinkSimple::carCanEnter() +bool SinkRandom::carCanEnter() { return !(distFloat(engine) < data.sinkBlockedPossibility); } -float SinkSimple::getPossibilityBeingBlocked() const +float SinkRandom::getPossibilityBeingBlocked() const { return data.sinkBlockedPossibility; } -unsigned int SinkSimple::getIndex() const +uint SinkRandom::getIndex() const { return data.sinkIndex; } diff --git a/src/Traffic/Sink/SinkSimple.h b/src/Traffic/Sink/SinkRandom.h similarity index 53% rename from src/Traffic/Sink/SinkSimple.h rename to src/Traffic/Sink/SinkRandom.h index 583587d911bb46bc73234e070b92ce9cef17bedb..d6775fd706059ba3ba3c233c81b07123cd8e1e4f 100644 --- a/src/Traffic/Sink/SinkSimple.h +++ b/src/Traffic/Sink/SinkRandom.h @@ -9,23 +9,21 @@ #include "Sink.h" #include "Utilities/invalidInput_error.h" -using namespace std; - -class VF_PUBLIC SinkSimple: +class VF_PUBLIC SinkRandom: public Sink { private: SinkData data; - mt19937 engine = RandomHelper::make_engine(); - uniform_real_distribution<float> distFloat{ 0.0, 1.0 }; + std::mt19937 engine = RandomHelper::make_engine(); + std::uniform_real_distribution<float> distFloat{ 0.0, 1.0 }; public: - SinkSimple(unsigned int sinkIndex, float sinkBlockedPossibility); - ~SinkSimple() {}; + SinkRandom(uint sinkIndex, float sinkBlockedPossibility); + ~SinkRandom() {}; float getPossibilityBeingBlocked() const; bool carCanEnter(); - unsigned int getIndex() const; + uint getIndex() const; }; diff --git a/src/Traffic/Source/Source.h b/src/Traffic/Source/Source.h index 5390c1a9189f860ce5ba52b7c3208129432b58aa..f4b3b3670ef27df9aef1f619a132d738b3806a3c 100644 --- a/src/Traffic/Source/Source.h +++ b/src/Traffic/Source/Source.h @@ -6,8 +6,8 @@ class VF_PUBLIC Source { public: - virtual unsigned int getIndex() const = 0; + virtual uint getIndex() const = 0; virtual float getPossibility() const = 0; - virtual unsigned int getSourceCar() = 0; + virtual uint getSourceCar() = 0; }; diff --git a/src/Traffic/Source/SourceData.h b/src/Traffic/Source/SourceData.h index 7cc20455d1d84ad1617138d12915e72508f03187..9c98db76e9765b3b6ed5e18097d1e2fdbd464d6f 100644 --- a/src/Traffic/Source/SourceData.h +++ b/src/Traffic/Source/SourceData.h @@ -1,12 +1,13 @@ #pragma once #include <VirtualFluidsDefinitions.h> +#include "Core/DataTypes.h" #include <memory> #include <vector> struct VF_PUBLIC SourceData { - unsigned int sourceIndex; + uint sourceIndex; float sourcePossibility; - unsigned int maxVelocity; + uint maxVelocity; }; \ No newline at end of file diff --git a/src/Traffic/Source/SourceTerm.cpp b/src/Traffic/Source/SourceRandom.cpp similarity index 56% rename from src/Traffic/Source/SourceTerm.cpp rename to src/Traffic/Source/SourceRandom.cpp index 8c8a9fe00662e5996a7b234e832a2fb83c04fd97..b5f68e337ae166d7dc133092cd305f3b9ef93d01 100644 --- a/src/Traffic/Source/SourceTerm.cpp +++ b/src/Traffic/Source/SourceRandom.cpp @@ -1,9 +1,9 @@ #pragma once -#include "SourceTerm.h" +#include "SourceRandom.h" -SourceTerm::SourceTerm(const unsigned int sourceIndex, const float sourcePossibility, unsigned int maxVelocity) +SourceRandom::SourceRandom(const uint sourceIndex, const float sourcePossibility, uint maxVelocity) { data.sourceIndex = sourceIndex; data.maxVelocity = maxVelocity; @@ -11,38 +11,38 @@ SourceTerm::SourceTerm(const unsigned int sourceIndex, const float sourcePossibi try { if (sourcePossibility >= 0 && sourcePossibility <= 1) { data.sourcePossibility = sourcePossibility; - uniform_int_distribution<unsigned int> distInt2{ 0, maxVelocity }; + std::uniform_int_distribution<uint> 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(); + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); exit(EXIT_FAILURE); }; } -SourceTerm::~SourceTerm() +SourceRandom::~SourceRandom() { } -unsigned int SourceTerm::getIndex() const +uint SourceRandom::getIndex() const { return data.sourceIndex; } -float SourceTerm::getPossibility() const +float SourceRandom::getPossibility() const { return data.sourcePossibility; } -unsigned int SourceTerm::getSourceCar() +uint SourceRandom::getSourceCar() { randomNumber = distFloat(engine); if (randomNumber < data.sourcePossibility) { @@ -52,7 +52,7 @@ unsigned int SourceTerm::getSourceCar() } -unsigned int SourceTerm::randomSpeed() +uint SourceRandom::randomSpeed() { return distInt(engine); } diff --git a/src/Traffic/Source/SourceRandom.h b/src/Traffic/Source/SourceRandom.h new file mode 100644 index 0000000000000000000000000000000000000000..4f747084c7f2559c41306aeb59c4c9b6d2d28a62 --- /dev/null +++ b/src/Traffic/Source/SourceRandom.h @@ -0,0 +1,36 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <iostream> +#include <random> +#include "Source.h" +#include "Utilities\RandomHelper.h" +#include "Utilities\invalidInput_error.h" + + +class VF_PUBLIC SourceRandom: + public Source +{ +private: + SourceData data; + + std::mt19937 engine = RandomHelper::make_engine(); + std::uniform_real_distribution<float> distFloat{ 0.0, 1.0 }; + std::uniform_int_distribution<uint> distInt{ 0, 1 }; + +public: + SourceRandom(const uint sourceIndex, const float sourcePossibility, uint maxVelocity); + ~SourceRandom(); + + virtual uint getIndex() const; + virtual float getPossibility() const; + virtual uint getSourceCar(); + +private: + uint randomSpeed(); + +private: + //variables for temporaray calculations + float randomNumber; +}; + diff --git a/src/Traffic/Source/SourceTerm.h b/src/Traffic/Source/SourceTerm.h deleted file mode 100644 index 2916c6e0b0e9bd48839d80cb8f5d46489ab3396d..0000000000000000000000000000000000000000 --- a/src/Traffic/Source/SourceTerm.h +++ /dev/null @@ -1,36 +0,0 @@ -#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/TrafficMovement.cpp b/src/Traffic/TrafficMovement.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3087a0171cd92ba70577d3706a8b747cb0ee7776 --- /dev/null +++ b/src/Traffic/TrafficMovement.cpp @@ -0,0 +1,451 @@ +#include "TrafficMovement.h" + + +TrafficMovement::TrafficMovement(std::unique_ptr<RoadNetworkData> road, const float dawdlePossibility) +{ + this->road = std::move(road); + + pcurrent = &this->road->current; + pnext = &(this->road->next); + + checkCurrentForSafetyDistance(); + + initDawdle(dawdlePossibility); +} + + +TrafficMovement::~TrafficMovement() +{ + pcurrent = NULL; + pnext = NULL; + pdummy = NULL; +} + + +void TrafficMovement::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 std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); + exit(EXIT_FAILURE); + } +} + + + +void TrafficMovement::initCalculation(uint timeSteps) +{ + this->timeSteps = timeSteps; + + if (display != nullptr) display->initResults(timeSteps); +} + + + +void TrafficMovement::setSlowToStart(const float slowStartPossibility) +{ + try { + if (slowStartPossibility >= 0 && slowStartPossibility < 1) { + if (slowStartPossibility > 0) { + this->slowStartPossibility = slowStartPossibility; + useSlowToStart = true; + } + } + else { + throw invalidInput_error("The slowStartPossibility should be between 0 and 1."); + } + } + catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + std::cin.get(); + exit(EXIT_FAILURE); + } +} + + +void TrafficMovement::setConcentrationOutwriter(std::unique_ptr<ConcentrationOutwriter> writer) +{ + this->concWriter = std::move(writer); +} + + +void TrafficMovement::setSaveResultsTrue() +{ + this->display = std::make_unique<CarDisplay>(&pcurrent, road->safetyDistance); +} + + +const uint TrafficMovement::getNumberOfCars() const +{ + uint num = 0; + for (auto& junc : road->junctions) { + num += junc->getNumCarsOnJunction(); + } + + return findCarIndicesInCurrent().size() + num; +} + + +const int TrafficMovement::getSpeedAtPosition(uint pos) const +{ + return (*pcurrent)[pos]; +} + + +const uint TrafficMovement::getRoadLength() const +{ + return road->roadLength; +} + + +const uint TrafficMovement::getMaxVelocity() const +{ + return road->maxVelocity; +} + + +void TrafficMovement::loopTroughTimesteps() +{ + for (uint step = 1; step < timeSteps + 1; step++) { + calculateTimestep(step); + } + if (display != nullptr) display->dispResults(&road->neighbors, road->sinks, road->junctions, road->sources); +} + + +void TrafficMovement::calculateTimestep(uint step) +{ + //if(display != nullptr) display->dispCurrentRoad(); + //if(concWriter != nullptr) concWriter->dispConcentration(); + + VectorHelper::fillVector(*pnext, -1); + if (concWriter != nullptr) concWriter->resetConcVector(); + + std::vector<uint> cars = findCarIndicesInCurrent(); + + for (auto &car : cars) { + applyRules(car); + } + + for (auto &junction : road->junctions) { + junction->updateJunction(); + junction->calculateTimeStep(*this); + } + + calculateSourceStep(); + + switchCurrentNext(); + + if (display != nullptr) display->putCurrentIntoResults(step); + + currentStep += 1; +} + + +void TrafficMovement::calculateSourceStep() +{ + uint sourceIndex; + uint maxSpeed; + for (auto &source : road->sources) { + sourceIndex = source->getIndex(); + maxSpeed = getGapAfterOutCell(sourceIndex, road->maxVelocity); + if (maxSpeed > 0) + (*pnext)[sourceIndex] = source->getSourceCar(); + } +} + + +void TrafficMovement::switchCurrentNext() +{ + pdummy = pcurrent; + pcurrent = pnext; + pnext = pdummy; +} + +void TrafficMovement::applyRules(uint carIndex) +{ + uint speed = (*pcurrent)[carIndex]; + accelerateCar(speed); + breakCar(carIndex, speed); + dawdleCar(carIndex, speed); + moveCar(carIndex, speed); +} + +void TrafficMovement::accelerateCar(uint & speed) +{ + if (speed < road->maxVelocity) { + speed += 1; + } +} + +void TrafficMovement::breakCar(uint carIndex, uint &speed) +{ + int neighbor = road->neighbors[carIndex]; + gap = getGapAfterCar(carIndex, speed, neighbor); + if (speed > gap) { + speed = gap; + } +} + +void TrafficMovement::dawdleCar(uint carIndex, uint & 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 TrafficMovement::moveCar(uint carIndex, uint speed) +{ + if (speed == 0) { + (*pnext)[carIndex] = 0; + writeConcentration(carIndex); + return; + } + + int neighbor = road->neighbors[carIndex]; + uint currentCell = carIndex; + + uint numberOfCellsMoved = iterateNeighborsInMove(currentCell, speed, neighbor); + + if (neighbor <= -1000 && neighbor > -2000) { + getJunctionFromNeighbor(neighbor)->registerCar(currentCell, numberOfCellsMoved, speed); + return; + } + + if (neighbor >= 0) { + (*pnext)[neighbor] = speed; + writeConcentration(neighbor); + } +} + + +void TrafficMovement::moveJunctionCar(uint outCellIndex, uint remainingDistance, uint speed) +{ + if (remainingDistance == 1) { + (*pnext)[outCellIndex] = speed; + writeConcentration(outCellIndex); + return; + } + + int neighbor = outCellIndex; + uint currentCell = outCellIndex; + + uint numberOfCellsMoved = iterateNeighborsInMove(currentCell, remainingDistance, neighbor); + + if (neighbor <= -1000 && neighbor > -2000) { + (*pnext)[currentCell] = speed; // car can't enter more than one junction in one timestep + writeConcentration(currentCell); + return; + } + + if (neighbor >= 0) { + (*pnext)[neighbor] = speed; + writeConcentration(neighbor); + } + +} + + +uint TrafficMovement::iterateNeighborsInMove(uint & currentCell, uint speed, int & neighbor) +{ + uint numberOfCellsMoved = 1; + + for (uint i = 2; i <= speed; i++) { + if (neighbor >= 0) { + currentCell = neighbor; + numberOfCellsMoved += 1; + neighbor = road->neighbors[neighbor]; + } + else + break; + } + return numberOfCellsMoved; +} + + +std::shared_ptr<Junction>& TrafficMovement::getJunctionFromNeighbor(int neighbor) +{ + //calculate index in junctions vector for neighbor (-1000 to -1999) + return road->junctions[((neighbor + 1000)*-1)]; +} + + +std::shared_ptr<Sink>& TrafficMovement::getSinkFromNeighbor(int neighbor) +{ + //calculate index in junctions vector for neighbor (-2000 to -2999) + int index = ((neighbor + 2000)*-1); + return road->sinks[index]; +} + + +uint TrafficMovement::getGapAfterCar(uint carIndex, uint speed, int neighbor) +{ + uint currentCell = carIndex; + + for (uint 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 adjustGapToSafetyDist(i); + + //empty cell -> get next neighbor, update currentCell + currentCell = neighbor; + neighbor = road->neighbors[neighbor]; + } + return speed; +} + + +uint TrafficMovement::getGapToSink(int neighbor, uint i, uint speed) +{ + if (getSinkFromNeighbor(neighbor)->carCanEnter()) + return speed; + return adjustGapToSafetyDist(i); +} + + +uint TrafficMovement::getGapToJunction(int neighbor, uint i, uint speed, uint currentCell) +{ + if (getJunctionFromNeighbor(neighbor)->acceptsCar(currentCell) && i <= speed) + return speed; + return i; +} + + +uint TrafficMovement::getGapAfterOutCell(uint outCellIndex, uint speed) +{ + if ((*pcurrent)[outCellIndex] > -1) + return 0; + gap = getGapAfterCar(outCellIndex, speed, outCellIndex); + return gap; +} + +uint TrafficMovement::adjustGapToSafetyDist(uint speed) +{ + if (speed <= road->safetyDistance) + return 0; + else + return speed - road->safetyDistance; +} + + +std::vector<uint> TrafficMovement::findCarIndicesInCurrent() const +{ + std::vector<uint> cars; + for (uint i = 0; i < road->roadLength; i++) { + if ((*pcurrent)[i] > -1) { + cars.push_back(i); + } + } + return cars; +} + + + +void TrafficMovement::writeConcentration(uint index) +{ + if (concWriter != nullptr) { + concWriter->calculateConcForSingleCar(index, (*pnext)[index]); + } +} + + +void TrafficMovement::dispResults() { + if (display == nullptr) + std::cout << "No results were saved." << std::endl; + else + display->dispResults(&road->neighbors, road->sinks, road->junctions, road->sources); +} + + +void TrafficMovement::checkCurrentForSafetyDistance() +{ + if (road->safetyDistance > 0) { + uint neighbor; + for (uint i = 0; i < road->roadLength; i++) { + if ((*pcurrent)[i] > -1) { + neighbor = road->neighbors[i]; + + for (uint j = 1; j <= road->safetyDistance; j++) { + if (neighbor <= -1000) + break; + if ((*pcurrent)[neighbor] > -1) { + std::cerr << "timestep 0: safetyDistance was violated: carIndex: " << i << std::endl; + break; + } + neighbor = road->neighbors[neighbor]; + } + } + } + } +} + + +const std::vector<int>& TrafficMovement::getVehiclesForVTK() +{ + return road->currentWithLongVehicles; +} + + +void TrafficMovement::visualizeVehicleLengthForVTK() +{ + road->currentWithLongVehicles = *pcurrent; + + if (road->safetyDistance != 0) { + for (uint i = 0; i < road->roadLength; i++) { + if ((*pcurrent)[i] > -1) { + int neighbor = road->neighbors[i]; + + for (uint j = 1; j <= road->safetyDistance; j++) { + if (neighbor <= -1000) + break; + if ((*pcurrent)[neighbor] > -1) { + std::cerr << "safetyDistance was violated: timestep: " << currentStep << "\t carIndex: " << i << std::endl; + break; + } + else + (road->currentWithLongVehicles)[neighbor] = (*pcurrent)[i]; + neighbor = road->neighbors[neighbor]; + } + } + } + } +} + + +const std::vector<float>& TrafficMovement::getConcentrations() +{ + if (concWriter != nullptr) return concWriter->getConcentrations(); + std::cerr << "No concentrations were calculated." << std::endl; + return {}; +} + diff --git a/src/Traffic/TrafficMovement.h b/src/Traffic/TrafficMovement.h new file mode 100644 index 0000000000000000000000000000000000000000..8365fbcaf6f9467195714e499ac91286cf4a1ce7 --- /dev/null +++ b/src/Traffic/TrafficMovement.h @@ -0,0 +1,114 @@ +#pragma once +#include <VirtualFluidsDefinitions.h> + +#include <iostream> + +#include <vector> +#include <random> +#include <memory> +#include <stdexcept> + +#include "Utilities/invalidInput_error.h" +#include "Utilities/VectorHelper.h" +#include "Utilities/RandomHelper.h" +#include "RoadNetwork/RoadNetworkData.h" +#include "Output/ConcentrationOutwriter.h" +#include "Output/CarDisplay.h" + + +class VF_PUBLIC TrafficMovement //Periodic BC +{ +public: + TrafficMovement(std::unique_ptr<RoadNetworkData> road, const float dawdlePossibility); + //TrafficMovement() {}; + TrafficMovement(const TrafficMovement&) = delete; + ~TrafficMovement(); + + //setUp + void setSlowToStart(const float slowStartPossibility); + void setConcentrationOutwriter(std::unique_ptr<ConcentrationOutwriter> writer); + void setSaveResultsTrue(); + void initCalculation(uint timeSteps); + + //timpestep + void loopTroughTimesteps(); + void calculateTimestep(uint step); + + //get + const uint getRoadLength() const; + const uint getMaxVelocity() const; + const uint getNumberOfCars() const; //only use for testing + const int getSpeedAtPosition(uint pos) const; //only use for testing + + //methods used by junctions and sinks + uint getGapAfterOutCell(uint outCellIndex, uint speed); + void moveJunctionCar(uint outCellIndex, uint remainingDistance, uint speed); + + //disp + void dispResults(); + + //vtk + void visualizeVehicleLengthForVTK(); + const std::vector<int>& getVehiclesForVTK(); + + //pollution + const std::vector<float>& getConcentrations(); + + +private: + //init + void initDawdle(const float dawdlePossibility); + void checkCurrentForSafetyDistance(); + + //calculate timestep + std::vector<uint> findCarIndicesInCurrent() const; + void calculateSourceStep(); + void switchCurrentNext(); + + //gap + uint getGapAfterCar(uint carIndex, uint speed, int neighbor); + uint getGapToSink(int neighbor, uint i, uint speed); + uint getGapToJunction(int neighbor, uint i, uint speed, uint currentCell); + uint adjustGapToSafetyDist(uint speed); + + //getVectorIndex + std::shared_ptr<Junction>& getJunctionFromNeighbor(int neighbor); + std::shared_ptr<Sink>& getSinkFromNeighbor(int neighbor); + + //apply rules + void applyRules(uint carIndex); + void accelerateCar(uint &speed); + void breakCar(uint carIndex, uint &speed); + void dawdleCar(uint carIndex, uint &speed); + void moveCar(uint carIndex, uint speed); + uint iterateNeighborsInMove(uint ¤tCell, uint speed, int &neighbor); + + void writeConcentration(uint index); + + +private: + std::unique_ptr<RoadNetworkData> road; + std::unique_ptr<ConcentrationOutwriter> concWriter = nullptr; + std::unique_ptr<CarDisplay> display = nullptr; + + std::vector<int> *pcurrent; + std::vector<int> *pnext; + std::vector<int> *pdummy; + + float dawdlePossibility; + + bool useSlowToStart = false; + float slowStartPossibility; + + uint timeSteps; + uint currentStep; + + std::mt19937 engine = RandomHelper::make_engine(); + std::uniform_real_distribution<float> distFloat{ 0.0, 1.0 }; + +private: + //temporary varables for calculation + uint gap; + double randomNumber; +}; + diff --git a/src/Traffic/Utilities/RandomHelper.cpp b/src/Traffic/Utilities/RandomHelper.cpp index 52b3f15a20cbbd76367ecde143405ef1d34a974f..176334f63b68a118c17a651c172aee7f541772b1 100644 --- a/src/Traffic/Utilities/RandomHelper.cpp +++ b/src/Traffic/Utilities/RandomHelper.cpp @@ -2,7 +2,7 @@ -mt19937 RandomHelper::make_engine() +std::mt19937 RandomHelper::make_engine() { std::random_device r; std::seed_seq seed{r()}; diff --git a/src/Traffic/Utilities/RandomHelper.h b/src/Traffic/Utilities/RandomHelper.h index 417f80af1c40c8ec88ff950b8916eddde141ffab..c4fc9c1ed98c94b05ababe0594af27156c1835db 100644 --- a/src/Traffic/Utilities/RandomHelper.h +++ b/src/Traffic/Utilities/RandomHelper.h @@ -2,8 +2,6 @@ #include <random> #include <VirtualFluidsDefinitions.h> -using namespace std; - class VF_PUBLIC RandomHelper { public: diff --git a/src/Traffic/Utilities/VectorHelper.cpp b/src/Traffic/Utilities/VectorHelper.cpp index 444950acd2389eec71cdd2faab75e298a3ea74c7..ac5627748f4be25d93047f409b4aa6c7ecf7e0bd 100644 --- a/src/Traffic/Utilities/VectorHelper.cpp +++ b/src/Traffic/Utilities/VectorHelper.cpp @@ -5,7 +5,7 @@ void VectorHelper::fillVector(std::vector<int> &vector, int insertNumber) { } void VectorHelper::fillVector(std::vector<std::vector<int> > &vector, int insertNumber) { - for (unsigned int i = 0; i < vector.size(); i++) { + for (uint i = 0; i < vector.size(); i++) { fill(vector[i].begin(), vector[i].end(), insertNumber); } } @@ -20,8 +20,8 @@ void VectorHelper::dispVector(const std::vector<int> &vector) 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++) { + for (uint i = 0; i < vector.size(); i++) { + for (uint j = 0; j < vector[i].size(); j++) { std::cout << std::setw(4) << vector[i][j]; } std::cout << std::endl; @@ -41,8 +41,8 @@ void VectorHelper::dispVectorColour(const std::vector<int> &vector) 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++) { + for (uint i = 0; i < vector.size(); i++) { + for (uint j = 0; j < vector[i].size(); j++) { makeVectorOutputColourful(vector[i][j]); std::cout << std::setw(4) << vector[i][j]; } diff --git a/src/Traffic/Utilities/VectorHelper.h b/src/Traffic/Utilities/VectorHelper.h index 37cfc69600039f60131d58b88b710f8183e17f67..bda0acf9edcaba1193b6ba22dc465ffdafe8848c 100644 --- a/src/Traffic/Utilities/VectorHelper.h +++ b/src/Traffic/Utilities/VectorHelper.h @@ -1,5 +1,6 @@ #pragma once #include <VirtualFluidsDefinitions.h> +#include "Core/DataTypes.h" #include <vector> #include <iostream> diff --git a/src/Traffic/Utilities/invalidInput_error.h b/src/Traffic/Utilities/invalidInput_error.h index 74a599eaebe1b2d9efd42c8e20c262582007a056..96633c621428ee7ded55b5bf9bd4eb006816a9bb 100644 --- a/src/Traffic/Utilities/invalidInput_error.h +++ b/src/Traffic/Utilities/invalidInput_error.h @@ -4,10 +4,9 @@ // using standard exceptions #include <iostream> #include <stdexcept> -using namespace std; class VF_PUBLIC invalidInput_error : - public runtime_error + public std::runtime_error { public: invalidInput_error(char const* const message) throw(); diff --git a/targets/apps/LBM/Basel/main.cpp b/targets/apps/LBM/Basel/main.cpp index 3d46637aa78a7d912b67636ba05dc04e4a759675..ab7c534ea22cf75095a2a6b234fcafe21a0b2029 100644 --- a/targets/apps/LBM/Basel/main.cpp +++ b/targets/apps/LBM/Basel/main.cpp @@ -317,9 +317,9 @@ void multipleLevel(const std::string& configPath) ////////////////////////////////////////////////////////////////////////// - gridBuilder->writeGridsToVtk("C:/Users/hiwi/Desktop/Basel_Ergebnisse/grids/BaselUni/Basel_Grid"); + gridBuilder->writeGridsToVtk("C:/Users/hiwi/BaselDokumente/Basel_Ergebnisse/grids/BaselUni/Basel_Grid"); - SimulationFileWriter::write("C:/Users/hiwi/Desktop/Basel_Ergebnisse/grids/BaselUni/", gridBuilder, FILEFORMAT::BINARY); + SimulationFileWriter::write("C:/Users/hiwi/BaselDokumente/Basel_Ergebnisse/grids/BaselUni/", gridBuilder, FILEFORMAT::BINARY); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -327,13 +327,13 @@ void multipleLevel(const std::string& configPath) finder.readStreets("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/targets/apps/LBM/streetTest/resources/ExampleStreets.txt"); - finder.writeVTK("C:/Users/hiwi/Desktop/Basel_Ergebnisse/ExampleStreets.vtk"); + finder.writeVTK("C:/Users/hiwi/BaselDokumente/Basel_Ergebnisse/ExampleStreets.vtk"); finder.findIndicesLB(gridBuilder->getGrid(0)); - finder.writeConnectionVTK("C:/Users/hiwi/Desktop/Basel_Ergebnisse/grids/BaselUni/Basel_Grid/ExampleStreetsConnection.vtk", gridBuilder->getGrid(0)); + finder.writeConnectionVTK("C:/Users/hiwi/BaselDokumente//Basel_Ergebnisse/grids/BaselUni/Basel_Grid/ExampleStreetsConnection.vtk", gridBuilder->getGrid(0)); - finder.writeSimulationFile("C:/Users/hiwi/Desktop/Basel_Ergebnisse/grids/BaselUni/", 1.0, gridBuilder->getNumberOfLevels(), 0); + finder.writeSimulationFile("C:/Users/hiwi/BaselDokumente/Basel_Ergebnisse/grids/BaselUni/", 1.0, gridBuilder->getNumberOfLevels(), 0); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -401,7 +401,7 @@ int main( int argc, char* argv[]) { try { - multipleLevel("C:/Users/hiwi/Desktop/MS/configBasel.txt"); + multipleLevel("C:/Users/hiwi/Desktop/configBasel.txt"); //multipleLevel("F:/Work/Computations/gridGenerator/inp/configTest.txt"); } catch (const std::exception& e) diff --git a/targets/apps/LBM/Basel/resources/Junctions.txt b/targets/apps/LBM/Basel/resources/Junctions.txt index 624178339b54652b30b8f194afde2f16868daa80..4b4a4ec0aa09eab8ac8f8faabbd01db3aa6f5903 100644 --- a/targets/apps/LBM/Basel/resources/Junctions.txt +++ b/targets/apps/LBM/Basel/resources/Junctions.txt @@ -1,5 +1,3 @@ 2 -1 e 6 e 7 e 4 e -5 s 2 s 3 s 9 s -0 e 5 e -1 a -1 a -8 s 1 s -1 a -1 a \ No newline at end of file +in 1 6 7 4 out 5 2 3 9 +in 0 5 -1 -1 out 8 1 -1 -1 \ No newline at end of file diff --git a/targets/apps/LBM/Basel/resources/Sinks.txt b/targets/apps/LBM/Basel/resources/Sinks.txt index 86f4f3be8dd799810025266be9853af1efffac28..e3c5a3a526264a73498061c997b09734b8615e1f 100644 --- a/targets/apps/LBM/Basel/resources/Sinks.txt +++ b/targets/apps/LBM/Basel/resources/Sinks.txt @@ -1,5 +1,5 @@ 4 -2 e 0 -3 e 0 -9 e 0 -8 e 0 +2 0.2 +3 0.2 +9 0.2 +8 0.2 diff --git a/targets/apps/LBM/Basel/resources/Sources.txt b/targets/apps/LBM/Basel/resources/Sources.txt index 7ad46f19d67e0369dd4429e132650da8b1b28d52..4af10e940263a0fb86c946e1005ded89d7ddb2fc 100644 --- a/targets/apps/LBM/Basel/resources/Sources.txt +++ b/targets/apps/LBM/Basel/resources/Sources.txt @@ -1,5 +1,5 @@ 4 -6 s 0.1 -7 s 0.1 -4 s 0.1 -0 s 0.1 \ No newline at end of file +6 0.4 +7 0.4 +4 0.4 +0 0.4 \ No newline at end of file diff --git a/targets/apps/LBM/TrafficTest/TrafficTest.cpp b/targets/apps/LBM/TrafficTest/TrafficTest.cpp index 7f04d647036258da091284ac06024d58cc2bcd64..d683c5cd6c6bf57edc52001d15cc9c6b3e1d8a66 100644 --- a/targets/apps/LBM/TrafficTest/TrafficTest.cpp +++ b/targets/apps/LBM/TrafficTest/TrafficTest.cpp @@ -10,46 +10,44 @@ #include "GridGenerator/StreetPointFinder/SinkReader.h" #include "Traffic/RoadNetwork/RoadMaker.h" -#include "Traffic/OneWayRoad.h" -#include "Traffic/OneWayRoadSSJ.h" -#include "Traffic/Source/SourceTerm.h" +#include "Traffic/TrafficMovement.h" +#include "Traffic/Source/SourceRandom.h" #include "Traffic/Junction/Junction.h" -#include "Traffic/Junction/JunctionSimple.h" +#include "Traffic/Junction/JunctionRandom.h" #include "Traffic/Sink/Sink.h" -#include "Traffic/Sink/SinkSimple.h" +#include "Traffic/Sink/SinkRandom.h" #include "Traffic/Output/ConcentrationByPosition.h" - -//using namespace std; +#include "Traffic/Output/ConcBySpeedAndAcceleration.h" int main() { - //Logger + //Variables - logging::Logger::addStream(&std::cout); - logging::Logger::setDebugLevel(logging::Logger::Level::INFO_LOW); - logging::Logger::timeStamp(logging::Logger::ENABLE); - logging::Logger::enablePrintedRankNumbers(logging::Logger::ENABLE); + int numberOfTimesteps = 1000; + float vehicleDensity = 0.5; + const uint maxVelocity = 14; + float dawdlePossibility = (float) 0.2; //typical value: 0.2 + float slowToStartPossibility = (float) 0.4; + uint vehicleLength = 7; - //Variables + //Logger - int numberOfTimesteps = 10; - float vehicleDensity = 0.005; + logging::Logger::addStream(&std::cout); + logging::Logger::setDebugLevel(logging::Logger::Level::INFO_LOW); + logging::Logger::timeStamp(logging::Logger::ENABLE); + logging::Logger::enablePrintedRankNumbers(logging::Logger::ENABLE); - const unsigned int maxVelocity = 5; - float dawdlePossibility = 0.5;//typical value: 0.2 - unsigned int vehicleLength = 7; - //StreetPointFinder StreetPointFinder finder; finder.readStreets("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/git/targets/apps/LBM/streetTest/resources/ExampleStreets.txt"); - finder.writeVTK("C:/Users/hiwi/Desktop/Basel_Ergebnisse/ExampleStreets.vtk"); + finder.writeVTK("C:/Users/hiwi/BaselDokumente/Basel_Ergebnisse/ExampleStreets.vtk"); JunctionReader junctionReader; junctionReader.readJunctions("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/git/targets/apps/LBM/Basel/resources/Junctions.txt", finder); @@ -61,87 +59,117 @@ int main() sourceReader.readSources("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/git/targets/apps/LBM/Basel/resources/Sources.txt", finder); - ////RandomRoad - - { - unsigned int roadLength = 0; - for (unsigned int i = 0; i < finder.streets.size(); i++) { + { + //calculate RoadLength + uint roadLength = 0; + uint numberOfStreets = finder.streets.size(); + for (uint i = 0; i < numberOfStreets; i++) { roadLength += finder.streets[i].numberOfCells; } + //make RoadNetwork auto roadNetwork = std::make_unique<RoadMaker>(roadLength, maxVelocity, vehicleLength, vehicleDensity); - vector< unique_ptr<Source> > sources; + + //Sources + std::vector< std::unique_ptr<Source> > sources; for (uint i = 0; i < sourceReader.sources.size(); i++) - sources.push_back(make_unique <SourceTerm>(sourceReader.sources[i].sourceIndex, sourceReader.sources[i].sourcePossibility, roadNetwork->getMaxVelocity())); + sources.push_back(std::make_unique <SourceRandom>(sourceReader.sources[i].sourceIndex, sourceReader.sources[i].sourcePossibility, roadNetwork->getMaxVelocity())); roadNetwork->setSources(move(sources)); - vector< unique_ptr<Sink> > sinks; + //Sinks + std::vector< std::unique_ptr<Sink> > sinks; for (uint i = 0; i < sinkReader.sinks.size(); i++) - sinks.push_back(make_unique <SinkSimple>(sinkReader.sinks[i].sinkIndex, sinkReader.sinks[i].sinkBlockedPossibility)); + sinks.push_back(std::make_unique <SinkRandom>(sinkReader.sinks[i].sinkIndex, sinkReader.sinks[i].sinkBlockedPossibility)); roadNetwork->setSinks(move(sinks)); - vector <unique_ptr<Junction> > junctions; + //Junctions + std::vector <std::unique_ptr<Junction> > junctions; for (uint i = 0; i < junctionReader.junctions.size(); i++) { - junctions.push_back(make_unique <JunctionSimple>(junctionReader.junctions[i].inCells, junctionReader.junctions[i].outCells)); + junctions.push_back(std::make_unique <JunctionRandom>(junctionReader.junctions[i].inCells, junctionReader.junctions[i].outCells)); junctions[i]->setCellIndexForNoUTurn(junctionReader.junctions[i].carCanNotEnterThisOutCell); } roadNetwork->setJunctions(move(junctions)); - auto simulator = std::make_shared<OneWayRoadSSJ>(move(roadNetwork), dawdlePossibility); + //init TrafficMovement + auto simulator = std::make_shared<TrafficMovement>(move(roadNetwork), dawdlePossibility); + //simulator->setSaveResultsTrue(); + simulator->setSlowToStart(slowToStartPossibility); simulator->initCalculation(numberOfTimesteps); - unique_ptr<ConcentrationOutwriter> writer = make_unique<ConcentrationByPosition>(ConcentrationByPosition(simulator->getRoadLength())); - simulator->setConcentrationOutwriter(move(writer)); + //init ConcentrationOutwriter + //std::unique_ptr<ConcentrationOutwriter> writer = std::make_unique<ConcentrationByPosition>(ConcentrationByPosition(simulator->getRoadLength())); + //simulator->setConcentrationOutwriter(move(writer)); - std::string outputPath("C:/Users/hiwi/Desktop/Basel_Ergebnisse/"); + //prepare writing to vtk + std::string outputPath("C:/Users/hiwi/BaselDokumente/Basel_Ergebnisse/"); std::string outputFilename("ExampleStreets"); - auto& cars = simulator->getVehiclesForVTK(); + + //loop through timesteps for (int step = 1; step < numberOfTimesteps + 1; step++) { simulator->calculateTimestep(step); simulator->visualizeVehicleLengthForVTK(); finder.writeVTK(outputPath + outputFilename + "_" + std::to_string(step) + ".vtk", cars); } + + std::vector<float> sim = simulator->getConcentrations(); + simulator->dispResults(); + std::cout << std::endl << std::endl; } + ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //std::vector<int> initialDistribution = { 1,-1,-1,1,-1,-1,1,-1,-1,1,-1,-1,1,-1,-1,1,-1,-1,-1,-1 }; + //std::vector<int> oneCar = { -1,1,-1,-1,-1,-1 }; + //std::vector<int> fiveCars = { 1, -1,-1, 1, -1, -1, -1, 2, -1, -1,-1,0,-1,-1,-1,-1,1,-1,-1,-1 }; + //int roadLength = 20; + //vehicleLength = 3; + ////OneWay random concwriter-test + //{ + // std::cout << "OneWay random" << std::endl; - ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - std::vector<int> initialDistribution = { 1,-1,-1,1,-1,-1,1,-1,-1,1,-1,-1,1,-1,-1,1,-1,-1,-1,-1 }; - std::vector<int> oneCar = { 1,-1,-1,-1,-1,-1 }; - std::vector<int> fiveCars = { 1, -1,-1, 1, -1, -1, -1, 2, -1, -1,-1,0,-1,-1,-1,-1,1,-1,-1,-1 }; - int roadLength = 20; - vehicleLength = 3; + // auto roadNetwork = std::make_unique<RoadMak er>(oneCar, maxVelocity, vehicleLength); + // auto simulator = std::make_shared<TrafficMovement>(move(roadNetwork) ,dawdlePossibility); + // auto writer = std::make_unique<ConcBySpeedAndAcceleration>(ConcBySpeedAndAcceleration(simulator->getRoadLength(), 5, 0)); + // simulator->setSaveResultsTrue(); + // simulator->setConcentrationOutwriter(move(writer)); + + // simulator->initCalculation(1); + // simulator->loopTroughTimesteps(); + + // std::cout << std::endl << std::endl; + + //} ////JunctionTest //{ // auto roadNetwork = std::make_unique<RoadMaker>(fiveCars, maxVelocity, vehicleLength); - // vector <unsigned int> in4 = { 9 }; - // vector<unsigned int> out4 = { 10 }; - // std::unique_ptr<Junction> j = std::make_unique<JunctionSimple>(in4, out4); + // vector <uint> in4 = { 9 }; + // vector<uint> out4 = { 10 }; + // std::unique_ptr<Junction> j = std::make_unique<JunctionRandom>(in4, out4); // j->setCellIndexForNoUTurn({ 10 }); // roadNetwork->addJunction(j); - // std::shared_ptr<OneWayRoadSSJ> simulator = std::make_shared<OneWayRoadSSJ>(move(roadNetwork), dawdlePossibility); - // simulator->setSaveResults(true); + // std::shared_ptr<TrafficMovement> simulator = std::make_shared<TrafficMovement>(move(roadNetwork), dawdlePossibility); + // simulator->setSaveResultsTrue(); // simulator->initCalculation(numberOfTimesteps); // for (int step = 1; step < numberOfTimesteps + 1; step++) { @@ -158,13 +186,13 @@ int main() // unique_ptr<RoadMaker> r = make_unique<RoadMaker>(car, 5, 2); - // vector <unsigned int> in = { 4 }; - // vector <unsigned int> out = { 5 }; - // unique_ptr<Junction> j = make_unique<JunctionSimple>(JunctionSimple(in, out)); + // vector <uint> in = { 4 }; + // vector <uint> out = { 5 }; + // unique_ptr<Junction> j = make_unique<JunctionRandom>(JunctionRandom(in, out)); // r->addJunction(j); - // shared_ptr<OneWayRoadSSJ> simulator = make_shared<OneWayRoadSSJ>(move(r), dawdlePossibility); - // simulator->setSaveResults(true); + // shared_ptr<TrafficMovement> simulator = make_shared<TrafficMovement>(move(r), dawdlePossibility); + // simulator->setSaveResultsTrue(); // simulator->initCalculation(1); // simulator->calculateTimestep(1); @@ -176,31 +204,12 @@ int main() //} - ////OneWay random - //{ - // std::cout << "OneWay random" << std::endl; - - - // auto roadNetwork = std::make_unique<RoadMaker>(roadLength, maxVelocity, vehicleLength, vehicleDensity); - // auto simulator = std::make_shared<OneWayRoad>(move(roadNetwork) ,dawdlePossibility); - - // //auto writer = make_unique<ConcentrationByPosition>(ConcentrationByPosition(simulator->getRoadLength())); - // //simulator->setConcentrationOutwriter(writer); - - // simulator->initCalculation(numberOfTimesteps); - //simulator->loopTroughTimesteps(); - // simulator->dispResults(); - - // std::cout << std::endl << std::endl; - - //} - ////OneWay initial distribution //{ // cout << "OneWay initial distribution" << endl; // unique_ptr<RoadNetworkData> roadNetwork = make_unique<RoadMaker>(initialDistribution, maxVelocity, vehicleLength); - // shared_ptr<OneWayRoad> simulator = make_shared<OneWayRoad>(move(roadNetwork), dawdlePossibility); + // shared_ptr<TrafficMovement> simulator = make_shared<TrafficMovement>(move(roadNetwork), dawdlePossibility); // simulator->initCalculation(numberOfTimesteps); //simulator->loopTroughTimesteps(); // simulator->dispResults(); @@ -215,7 +224,7 @@ int main() // 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> roadNetwork = make_unique<RoadMaker>(initialDist, maxVelocity, 1); - // shared_ptr<OneWayRoad> simulator = make_shared<OneWayRoad>(move(roadNetwork), dawdlePossibility); + // shared_ptr<TrafficMovement> simulator = make_shared<TrafficMovement>(move(roadNetwork), dawdlePossibility); // simulator->setSlowToStart(static_cast<float>(0.9999)); // simulator->initCalculation(numberOfTimesteps); //simulator->loopTroughTimesteps(); @@ -230,17 +239,17 @@ int main() // cout << "sources and sinks" << endl; // vector< unique_ptr<Sink> > sinks; - // sinks.push_back(make_unique <SinkSimple>(5, static_cast<float>(0.5))); + // sinks.push_back(make_unique <SinkRandom>(5, static_cast<float>(0.5))); // unique_ptr<RoadMaker> roadNetwork = make_unique<RoadMaker>(oneCar, maxVelocity, vehicleLength); // vector< unique_ptr<Source> > sources; - // sources.push_back(make_unique <SourceTerm>(0, static_cast<float>(1.0), roadNetwork->getMaxVelocity())); + // sources.push_back(make_unique <SourceRandom>(0, static_cast<float>(1.0), roadNetwork->getMaxVelocity())); // roadNetwork->setSources(move(sources)); // roadNetwork->setSinks(move(sinks)); - // shared_ptr<OneWayRoadSSJ> roadSource = make_shared<OneWayRoadSSJ>(move(roadNetwork), static_cast<float>(0.0)); + // shared_ptr<TrafficMovement> roadSource = make_shared<TrafficMovement>(move(roadNetwork), static_cast<float>(0.0)); // roadSource->initCalculation(numberOfTimesteps); // roadSource->loopTroughTimesteps(); @@ -255,21 +264,21 @@ int main() // unique_ptr<RoadMaker> roadNetwork = make_unique<RoadMaker>(25, maxVelocity, vehicleLength); // vector< unique_ptr<Source> > sources; - // sources.push_back(make_unique <SourceTerm>(0, static_cast<float>(1), roadNetwork->getMaxVelocity())); - // sources.push_back(make_unique <SourceTerm>(10, static_cast<float>(1), roadNetwork->getMaxVelocity())); + // sources.push_back(make_unique <SourceRandom>(0, static_cast<float>(1), roadNetwork->getMaxVelocity())); + // sources.push_back(make_unique <SourceRandom>(10, static_cast<float>(1), roadNetwork->getMaxVelocity())); // roadNetwork->setSources(move(sources)); - // unique_ptr<Sink> s = make_unique <SinkSimple>(SinkSimple(24, static_cast<float>(0.0))); + // unique_ptr<Sink> s = make_unique <SinkRandom>(SinkRandom(24, static_cast<float>(0.0))); // roadNetwork->addSink(move(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))); + // vector<uint> in = { 9,20 }; + // vector<uint> out = { 21 }; + // junctions.push_back(make_unique <JunctionRandom>(JunctionRandom(in, out))); // roadNetwork->setJunctions(junctions); - // shared_ptr<OneWayRoadSSJ> mergingRoad = make_shared<OneWayRoadSSJ>(move(roadNetwork), dawdlePossibility); + // shared_ptr<TrafficMovement> mergingRoad = make_shared<TrafficMovement>(move(roadNetwork), dawdlePossibility); // mergingRoad->initCalculation(numberOfTimesteps); @@ -277,64 +286,5 @@ int main() // 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->initCalculation(numberOfTimesteps); - //splittingRoad->loopTroughTimesteps(); - // 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->initCalculation(numberOfTimesteps); - //crossRoads->loopTroughTimesteps(); - // crossRoads->dispResults(); - // cout << endl << endl; - //} - - - }