diff --git a/src/Traffic/GPU/TrafficTimestep.cu b/src/Traffic/GPU/TrafficTimestep.cu index 17e776be787de4b6ba137628d902dd3a883e97b0..19385cad24cef837c42e2f48f2173728e114d557 100644 --- a/src/Traffic/GPU/TrafficTimestep.cu +++ b/src/Traffic/GPU/TrafficTimestep.cu @@ -22,7 +22,7 @@ #include "Utilities/safe_casting.h" #include "Utilities/invalidInput_error.h" - +//kernel __global__ void trafficTimestepKernel(int* roadCurrent, int* roadNext, int* neighbors, real * pConcArray, uint* juncInCellIndices, bool* juncCarCanEnter, int* juncCarsOnJunction, uint* juncAlreadyMoved, uint* juncOldSpeeds, real* sinkCarBlockedPossibilities, uint size_road, uint size_juncInCells, uint maxVelocity, uint maxAcceleration, uint safetyDistance, bool useSlowToStart, real slowStartPossibility, real dawdlePossibility, curandState *state); @@ -31,12 +31,14 @@ __global__ void sourceTimestepKernel(int* roadCurrent, int* roadNext, int* neigh float* sourcePossibilities, real * pConcArray, uint maxVelocity, uint safetyDistance, uint size_sources, curandState *state); __global__ void junctionTimestepKernel(int* juncCarsOnJunction, uint* juncInCellIndices, int* juncOutCellIndices, uint* juncStartInIncells, uint* juncStartInOutcells, - uint* juncAlreadyMoved, int* juncCarCanNotEnterThisOutCell, bool* juncOutCellIsOpen, uint* juncOldSpeeds, bool* juncCarCanEnter, + uint* juncAlreadyMoved, int* juncCarCanNotEnterThisOutCell, bool* juncOutCellIsOpen, uint* juncOldSpeeds, bool* juncCarCanEnter, uint* juncTrafficLightSwitchTime, int* roadCurrent, int* roadNext, int* neighbors, real * pConcArray, real* sinkCarBlockedPossibilities, uint safetyDistance, - uint size_juncInCells, uint size_juncOutCells, uint size_junctions, curandState *state); + uint size_juncInCells, uint size_juncOutCells, uint size_junctions, uint numTimestep, curandState *state); __global__ void randomSetupKernel(curandState *state, uint size); + +//device functions for movement __device__ inline uint getJunctionInCellsVectorIndex(uint * juncInCellIndices, uint size_juncInCells, uint cell); __device__ inline uint getGapAfterOutCell(int* roadCurrent, int* neighbors, real* sinkCarBlockedPossibilities, int sourceIndex, uint speed, uint safetyDistance, curandState* state); @@ -46,6 +48,9 @@ __device__ inline void moveCar(uint speed, uint index, int* roadCurrent, int* ro __device__ inline void carStaysOnJunction(int* juncCarsOnJunction, uint* juncInCellIndices, uint* juncAlreadyMoved, uint*juncOldSpeeds, real* pConcArray, uint inCellVectorIndex); + + +//device functions for concentrations __device__ inline real calcConcentration(uint oldSpeed, uint newSpeed); __device__ inline void putConcIntoArray(real * pConcArray, uint oldSpeed, uint newSpeed, uint newIndex); @@ -83,6 +88,7 @@ TrafficTimestep::TrafficTimestep(std::shared_ptr<RoadNetworkData> road, real * p combineJuncInCellIndices(road->junctions); combineJuncOutCellIndices(road->junctions); combineJuncCarCanNotEnterThisOutCell(road->junctions); + combineUseTrafficLights(road->junctions); initJuncCarCanEnter(); initJuncCarsOnJunction(); @@ -119,12 +125,8 @@ TrafficTimestep::TrafficTimestep(std::shared_ptr<RoadNetworkData> road, real * p getLastCudaError("random_setup_kernel for sources execution failed"); //prepare ConcWriter - if (pConcArray == nullptr) { - checkCudaErrors(cudaMalloc((void **)&this->pConcArray, size_roads * sizeof(real))); - } - else { - this->pConcArray = pConcArray; - } + if (pConcArray == nullptr) checkCudaErrors(cudaMalloc((void **)&this->pConcArray, size_roads * sizeof(real))); + else this->pConcArray = pConcArray; } @@ -146,13 +148,15 @@ void TrafficTimestep::run(std::shared_ptr<RoadNetworkData> road) cudaDeviceSynchronize(); getLastCudaError("trafficTimestepKernel execution failed"); - callJunctionKernel(); + callJunctionTimestepKernel(); cudaDeviceSynchronize(); getLastCudaError("junctionTimestepKernel execution failed"); - callSourceKernel(); + callSourceTimestepKernel(); cudaDeviceSynchronize(); getLastCudaError("sourceTimestepKernel execution failed"); + + numTimestep++; } @@ -212,7 +216,7 @@ void TrafficTimestep::callTrafficTimestepKernel() } -void TrafficTimestep::callSourceKernel() +void TrafficTimestep::callSourceTimestepKernel() { int*pRoadCurrent = roadCurrent.data().get(); int*pRoadNext = roadNext.data().get(); @@ -236,7 +240,7 @@ void TrafficTimestep::callSourceKernel() } -void TrafficTimestep::callJunctionKernel() +void TrafficTimestep::callJunctionTimestepKernel() { int*pRoadCurrent = roadCurrent.data().get(); int*pRoadNext = roadNext.data().get(); @@ -256,6 +260,7 @@ void TrafficTimestep::callJunctionKernel() juncOutCellIsOpen.data().get(), juncOldSpeeds.data().get(), juncCarCanEnter.data().get(), + juncTrafficLightSwitchTime.data().get(), pRoadCurrent, pRoadNext, neighbors.data().get(), @@ -265,6 +270,7 @@ void TrafficTimestep::callJunctionKernel() size_juncInCells, size_juncOutCells, size_junctions, + numTimestep, statesJunctions); } @@ -361,7 +367,7 @@ __global__ void trafficTimestepKernel(int* roadCurrent, int* roadNext, int* neig ////// moveCar ///////////////////////////////////////////////////////////////////////////// moveCar(speed, index, roadCurrent, roadNext, neighbors, pConcArray, juncInCellIndices, juncCarCanEnter, juncCarsOnJunction, - juncAlreadyMoved, juncOldSpeeds, neighbor, size_juncInCells); + juncAlreadyMoved, juncOldSpeeds, neighbor, size_juncInCells); } @@ -443,9 +449,9 @@ __global__ void sourceTimestepKernel(int* roadCurrent, int* roadNext, int* neigh __global__ void junctionTimestepKernel(int* juncCarsOnJunction, uint* juncInCellIndices, int* juncOutCellIndices, uint* juncStartInIncells, uint* juncStartInOutcells, - uint* juncAlreadyMoved, int* juncCarCanNotEnterThisOutCell, bool* juncOutCellIsOpen, uint* juncOldSpeeds, bool* juncCarCanEnter, + uint* juncAlreadyMoved, int* juncCarCanNotEnterThisOutCell, bool* juncOutCellIsOpen, uint* juncOldSpeeds, bool* juncCarCanEnter, uint* juncTrafficLightSwitchTime, int* roadCurrent, int* roadNext, int* neighbors, real* pConcArray, real* sinkCarBlockedPossibilities, uint safetyDistance, - uint size_juncInCells, uint size_juncOutCells, uint size_junctions, curandState* state) { + uint size_juncInCells, uint size_juncOutCells, uint size_junctions, uint numTimestep, curandState* state) { ////////////////////////////////////////////////////////////////////////// const uint x = threadIdx.x; // Globaler x-Index const uint y = blockIdx.x; // Globaler y-Index @@ -548,7 +554,7 @@ __global__ void junctionTimestepKernel(int* juncCarsOnJunction, uint* juncInCell putConcIntoArray(pConcArray, juncOldSpeeds[inCellVectorIndex], speed, chosenCell); juncCarsOnJunction[inCellVectorIndex] = -1; juncCarCanEnter[inCellVectorIndex] = true; - return; + break; } //iterate through neighbors @@ -573,6 +579,34 @@ __global__ void junctionTimestepKernel(int* juncCarsOnJunction, uint* juncInCell } } } + + + //generate red TrafficLights + + if (juncTrafficLightSwitchTime[index] > 0) { + + uint halfNumStreets = (uint)(std::floor((float)inCellsSize * 0.5f)); + + if ((uint)(std::floor((float)numTimestep / (float)juncTrafficLightSwitchTime[index])) % 2 == 0) { + for (uint i = firstInCellIndex; i < firstInCellIndex + halfNumStreets; i++) + juncCarCanEnter[i] = false; + + if (numTimestep % juncTrafficLightSwitchTime[index] == 0) //first timestep with green light --> open the streets that were closed before + for (uint i = firstInCellIndex + halfNumStreets; i < firstInCellIndex + inCellsSize; i++) + if (juncCarsOnJunction[i] == -1) + juncCarCanEnter[i] = true; + } + else { + for (uint i = firstInCellIndex + halfNumStreets; i < firstInCellIndex + inCellsSize; i++) + juncCarCanEnter[i] = false; + + if (numTimestep % juncTrafficLightSwitchTime[index] == 0) //first timestep with green light --> open the streets that were closed before + for (uint i = firstInCellIndex; i < firstInCellIndex + halfNumStreets; i++) + if (juncCarsOnJunction[i] == -1) + juncCarCanEnter[i] = true; + } + } + } @@ -580,7 +614,7 @@ __device__ inline void carStaysOnJunction(int* juncCarsOnJunction, uint* juncInC addConcToArray(pConcArray, juncOldSpeeds[inCellVectorIndex], 0, juncInCellIndices[inCellVectorIndex]); juncCarsOnJunction[inCellVectorIndex] = 0; juncAlreadyMoved[inCellVectorIndex] = 0; - juncOldSpeeds[inCellVectorIndex] = 0; + juncOldSpeeds[inCellVectorIndex] = 0; } @@ -597,7 +631,7 @@ __device__ inline uint getGapAfterOutCell(int* roadCurrent, int* neighbors, real float randomNumber = curand_uniform(&localState); state[index] = localState; if (i <= speed && !(randomNumber < sinkCarBlockedPossibilities[(outCellIndex + 2000)*-1])) - return speed; + return speed; return i; } //junction @@ -659,13 +693,13 @@ __device__ real calcConcentration(uint oldSpeed, uint newSpeed) return 0.45f; else printf("couldn't choose driving state in calcConcentration"); - + return -1; } -__device__ void putConcIntoArray(real * pConcArray, uint oldSpeed, uint newSpeed, uint newIndex) +__device__ void putConcIntoArray(real * pConcArray, uint oldSpeed, uint newSpeed, uint newIndex) { - pConcArray[newIndex] = calcConcentration(oldSpeed,newSpeed); + pConcArray[newIndex] = calcConcentration(oldSpeed, newSpeed); } @@ -747,6 +781,12 @@ void TrafficTimestep::initJuncOldSpeeds() std::fill(juncOldSpeeds.begin(), juncOldSpeeds.end(), 0); } +void TrafficTimestep::combineUseTrafficLights(std::vector<std::shared_ptr<Junction>>& junctions) +{ + for (auto& j : junctions) + juncTrafficLightSwitchTime.push_back(j->getTrafficLightSwitchTime()); +} + void TrafficTimestep::initjuncOutCellIsOpen() { juncOutCellIsOpen.resize(juncOutCellIndices.size()); diff --git a/src/Traffic/GPU/TrafficTimestep.h b/src/Traffic/GPU/TrafficTimestep.h index f9eada20ae8afb01fcd65589afa21481c9e9cae8..9e0b8319fc00395207814362cd3c1cb52e4d9a77 100644 --- a/src/Traffic/GPU/TrafficTimestep.h +++ b/src/Traffic/GPU/TrafficTimestep.h @@ -24,6 +24,7 @@ class VF_PUBLIC TrafficTimestep { private: bool timestepIsEven = true; + uint numTimestep = 0; uint size_roads; uint maxVelocity; @@ -33,12 +34,16 @@ private: real slowStartPossibility; uint maxAcceleration; + + //sizes uint size_junctions; uint size_juncInCells; uint size_juncOutCells; uint size_sources; uint size_sinks; + + //grids dim3 gridRoad; dim3 threadsRoads; dim3 gridJunctions; @@ -46,11 +51,16 @@ private: dim3 gridSources; dim3 threadsSources; - thrust::device_vector<int> neighbors; + //road + thrust::device_vector<int> neighbors; thrust::device_vector<int> roadCurrent; thrust::device_vector<int> roadNext; + + //junctions + thrust::device_vector<uint> juncTrafficLightSwitchTime; //no TrafficLight: 0 + thrust::device_vector<uint> juncInCellIndices; thrust::device_vector<bool> juncCarCanEnter; thrust::device_vector<int> juncCarsOnJunction; @@ -63,16 +73,25 @@ private: thrust::device_vector<uint> juncStartInIncells; thrust::device_vector<uint> juncStartInOutcells; + //sinks thrust::device_vector<real> sinkCarBlockedPossibilities;; + + //sources thrust::device_vector<float> sourcePossibilities; thrust::device_vector<uint> sourceIndices; + + //concentrations + real * pConcArray; + + + //curandStates curandState *statesJunctions; curandState *statesSources; curandState *statesRoad; - real * pConcArray; + public: @@ -89,8 +108,8 @@ private: void resetOutCellIsOpen(); void callTrafficTimestepKernel(); - void callSourceKernel(); - void callJunctionKernel(); + void callSourceTimestepKernel(); + void callJunctionTimestepKernel(); //init void calculateTrafficTimestepKernelDimensions(); @@ -105,14 +124,12 @@ private: void initJuncCarsOnJunction(); void initJuncAlreadyMoved(); void initJuncOldSpeeds(); + void combineUseTrafficLights(std::vector<std::shared_ptr<Junction>>& junctions); void combineSinkBlockedPossibilities(std::vector<std::shared_ptr<Sink>>& sinks); void combineSourcePossibilities(std::vector<std::shared_ptr<Source> > &sources); - void combineSourceIndices(std::vector<std::shared_ptr<Source> > &sources); - - - + void combineSourceIndices(std::vector<std::shared_ptr<Source> > &sources); }; #endif diff --git a/src/Traffic/Junction/Junction.h b/src/Traffic/Junction/Junction.h index 4ec34c285a339d30b40fdb809fa70fefd9429e99..f64cdd496ec9a8c6f9b2bb0538835470827a7ec1 100644 --- a/src/Traffic/Junction/Junction.h +++ b/src/Traffic/Junction/Junction.h @@ -17,7 +17,7 @@ public: virtual bool acceptsCar(uint cellIndex) = 0; //determines if a car can enter the junction virtual void registerCar(uint cellIndex, uint numberOfCellsAlreadyMoved, uint speed, uint oldSpeed) = 0; //registers all cars entering the junction - virtual void calculateTimeStep(TrafficMovement &road) = 0; + virtual void calculateTimeStep(TrafficMovement &road, uint currentTimestep) = 0; virtual const std::vector<uint>& getInCellIndices()const = 0; virtual const std::vector<uint>& getOutCellIndices() const = 0; @@ -26,6 +26,7 @@ public: virtual const std::vector<uint>& getAlreadyMoved()const = 0; virtual const std::vector<uint>& getOldSpeeds()const = 0; virtual const std::vector<int>& getCarCanNotEnterThisOutCell() const = 0; + virtual uint getTrafficLightSwitchTime()const = 0; virtual void dispJunction(const uint index, const uint roadLength) const = 0; virtual uint getNumCarsOnJunction() const = 0; diff --git a/src/Traffic/Junction/JunctionData.h b/src/Traffic/Junction/JunctionData.h index e82f2a3b2e6450c68f587684c237a11d1690556e..d8c5a9acbf8a1002fbc710c827a3cdb4b9c0765e 100644 --- a/src/Traffic/Junction/JunctionData.h +++ b/src/Traffic/Junction/JunctionData.h @@ -21,6 +21,8 @@ public: std::vector<uint> alreadyMoved; std::vector<uint> oldSpeeds; + uint trafficLightSwitchTime; //no TrafficLight: 0 + std::mt19937 engine = RandomHelper::make_engine(); std::uniform_int_distribution<uint> distInt2{ 0, 1 }; std::uniform_int_distribution<uint> distInt3{ 0, 2 }; diff --git a/src/Traffic/Junction/JunctionRandom.cpp b/src/Traffic/Junction/JunctionRandom.cpp index b75eb9e710204eee04949942286114e5745ffbdb..22b61882306ac61839eed40b8c29e122aa0fcbd9 100644 --- a/src/Traffic/Junction/JunctionRandom.cpp +++ b/src/Traffic/Junction/JunctionRandom.cpp @@ -1,7 +1,8 @@ #include "JunctionRandom.h" #include <iostream> -#include <algorithm> +#include <algorithm> //used for find() +#include <math.h> //used for floor() #include "TrafficMovement.h" @@ -10,8 +11,10 @@ #include "Utilities/safe_casting.h" -JunctionRandom::JunctionRandom(const std::vector<uint> &inCellIndices, const std::vector<uint> &outCellIndices) +JunctionRandom::JunctionRandom(const std::vector<uint> &inCellIndices, const std::vector<uint> &outCellIndices, uint trafficLightSwitchTime) { + this->data.trafficLightSwitchTime = trafficLightSwitchTime; + data.inCellIndices = inCellIndices; data.outCellIndices = outCellIndices; @@ -82,7 +85,7 @@ uint JunctionRandom::getInCellsVectorIndex(uint cellIndex) } -void JunctionRandom::calculateTimeStep(TrafficMovement& road) +void JunctionRandom::calculateTimeStep(TrafficMovement& road, uint currentTimestep) { data.possibleOutCells = data.outCellIndices; @@ -94,6 +97,43 @@ void JunctionRandom::calculateTimeStep(TrafficMovement& road) ++index; } writeConcentrations(road); + + generateRedTrafficLights(currentTimestep); +} + + +void JunctionRandom::generateRedTrafficLights(uint currentTimestep) +{ + if (data.trafficLightSwitchTime > 0) { + uint halfNumStreets = static_cast<uint>(std::floor(static_cast<float>(data.inCellIndices.size()) * 0.5f)); + + if (static_cast<uint>(std::floor(static_cast<float>(currentTimestep) / static_cast<float>(data.trafficLightSwitchTime))) % 2 == 0) + turnFirstHalfRed(currentTimestep, halfNumStreets); + else + turnSecondHalfRed(currentTimestep, halfNumStreets); + } +} + +void JunctionRandom::turnFirstHalfRed(uint currentTimestep, uint halfNumStreets) +{ + for (uint i = 0; i < halfNumStreets; i++) + data.carCanEnter[i] = false; + + if (currentTimestep % data.trafficLightSwitchTime == 0) //first timestep with green light --> open the streets that were closed before + for (uint i = halfNumStreets; i < data.inCellIndices.size(); i++) + if (data.carsOnJunction[i] == -1) + data.carCanEnter[i] = true; +} + +void JunctionRandom::turnSecondHalfRed(uint currentTimestep, uint halfNumStreets) +{ + for (uint i = halfNumStreets; i < data.inCellIndices.size(); i++) + data.carCanEnter[i] = false; + + if (currentTimestep % data.trafficLightSwitchTime == 0) //first timestep with green light --> open the streets that were closed before + for (uint i = 0; i < halfNumStreets; i++) + if (data.carsOnJunction[i] == -1) + data.carCanEnter[i] = true; } @@ -235,6 +275,11 @@ const std::vector<int>& JunctionRandom::getCarCanNotEnterThisOutCell() const return data.carCanNotEnterThisOutCell; } +uint JunctionRandom::getTrafficLightSwitchTime() const +{ + return data.trafficLightSwitchTime; +} + void JunctionRandom::dispJunction(const uint index, const uint roadLength) const { if (find(data.inCellIndices.begin(), data.inCellIndices.end(), (roadLength - index - 1)) != data.inCellIndices.end()) { diff --git a/src/Traffic/Junction/JunctionRandom.h b/src/Traffic/Junction/JunctionRandom.h index d91a83ca281b7767b27cbfe6d4f462807639f0b9..c1b119ac631b4fa4d92c51694b86b7ce07c75e02 100644 --- a/src/Traffic/Junction/JunctionRandom.h +++ b/src/Traffic/Junction/JunctionRandom.h @@ -17,14 +17,14 @@ private: JunctionData data; public: - JunctionRandom(const std::vector<uint> &inCellIndices, const std::vector<uint> &outCellIndices); + JunctionRandom(const std::vector<uint> &inCellIndices, const std::vector<uint> &outCellIndices, uint trafficLightSwitchTime = 0); ~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, uint oldSpeed); //registers all cars entering the junction - virtual void calculateTimeStep(TrafficMovement &road); + virtual void calculateTimeStep(TrafficMovement &road, uint currentTimestep); virtual const std::vector<uint>& getInCellIndices() const; virtual const std::vector<uint>& getOutCellIndices() const; @@ -33,6 +33,7 @@ public: virtual const std::vector<uint>& getAlreadyMoved()const; virtual const std::vector<uint>& getOldSpeeds()const; virtual const std::vector<int>& getCarCanNotEnterThisOutCell()const; + virtual uint getTrafficLightSwitchTime()const; virtual void dispJunction(const uint index, const uint roadLength) const; virtual uint getNumCarsOnJunction() const; @@ -48,6 +49,11 @@ private: int chooseOutCell(int index); int generateRandomOutCellIndex(uint outCellsTempSize); + void generateRedTrafficLights(uint currentTimestep); + void turnFirstHalfRed(uint currentTimestep, uint halfNumStreets); + void turnSecondHalfRed(uint currentTimestep, uint halfNumStreets); + + void writeConcentrations(TrafficMovement &road); }; diff --git a/src/Traffic/Tests/TrafficFactoryForTests.cpp b/src/Traffic/Tests/TrafficFactoryForTests.cpp deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/Traffic/Tests/TrafficFactoryForTests.h b/src/Traffic/Tests/TrafficFactoryForTests.h deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/Traffic/Tests/package.include b/src/Traffic/Tests/package.include deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/src/Traffic/TrafficMovement.cpp b/src/Traffic/TrafficMovement.cpp index fd404ad0e45ca541ee7dfb72900a3f6e183bc25a..caa2cf8fdb92e1213499e67b2db1e02614c685f6 100644 --- a/src/Traffic/TrafficMovement.cpp +++ b/src/Traffic/TrafficMovement.cpp @@ -199,21 +199,21 @@ void TrafficMovement::calculateTimestep(uint step) display->putCurrentIntoResults(step); } - if (display != nullptr) { - if (useGPU) copyDevToHost(); - display->dispCurrentRoad(); - - ////dispConcFromGPU - //for (auto cell : road->conc) { - // 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); - } + //if (display != nullptr) { + // if (useGPU) copyDevToHost(); + // display->dispCurrentRoad(); + + // ////dispConcFromGPU + // //for (auto cell : road->conc) { + // // 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); + //} currentStep += 1; } @@ -237,7 +237,7 @@ void TrafficMovement::calculateSourceStep() void TrafficMovement::calculateJunctionStep() { for (auto &junction : road->junctions) { - junction->calculateTimeStep(*this); + junction->calculateTimeStep(*this, currentStep); } } diff --git a/src/Traffic/TrafficMovementFactory - Kopie.cpp b/src/Traffic/TrafficMovementFactory - Kopie.cpp index c4f378d3f51cc4972d01ba12b9d0ab0996f3f08d..07c3f8a1b488ae58c096d2d959ab17fdd552539c 100644 --- a/src/Traffic/TrafficMovementFactory - Kopie.cpp +++ b/src/Traffic/TrafficMovementFactory - Kopie.cpp @@ -20,7 +20,7 @@ void TrafficMovementFactoryTest::initTrafficMovement(real * pConcArray) { //Variables - uint roadLength = 20; + uint roadLength = 40; real vehicleDensity = 0.1f; @@ -36,7 +36,7 @@ void TrafficMovementFactoryTest::initTrafficMovement(real * pConcArray) //make RoadNetwork - std::vector<int> road(20); + std::vector<int> road(40); std::fill(road.begin(), road.end(), -1); road[9] = 5; auto roadNetwork = std::make_unique<RoadMaker>(road, maxVelocity, vehicleLength); @@ -45,18 +45,22 @@ void TrafficMovementFactoryTest::initTrafficMovement(real * pConcArray) //RoadMaker(const uint roadLength, const uint maxVelocity, uint vehicleLength);//empty road //Sources - std::unique_ptr<Source> source = std::make_unique <SourceRandom>(SourceRandom(0, 0.7f, maxVelocity)); + std::unique_ptr<Source> source = std::make_unique <SourceRandom>(SourceRandom(0, 0.9f, maxVelocity)); + std::unique_ptr<Source> source1 = std::make_unique <SourceRandom>(SourceRandom(11, 0.9f, maxVelocity)); roadNetwork->addSource(source); + roadNetwork->addSource(source1); //Sinks std::unique_ptr<Sink> s = std::make_unique <SinkRandom>(SinkRandom(roadLength-1, 0.5f)); + std::unique_ptr<Sink> s1 = std::make_unique <SinkRandom>(SinkRandom(29, 0.5f)); roadNetwork->addSink(move(s)); + roadNetwork->addSink(move(s1)); //Junctions - std::vector<uint> inCellIndices = { 9 }; - std::vector<uint> outCellIndices = { 11 }; + std::vector<uint> inCellIndices = { 9,19 }; + std::vector<uint> outCellIndices = { 21,31 }; - std::unique_ptr<Junction> j = std::make_unique<JunctionRandom>(JunctionRandom(inCellIndices, outCellIndices)); + std::unique_ptr<Junction> j = std::make_unique<JunctionRandom>(JunctionRandom(inCellIndices, outCellIndices,5)); roadNetwork->addJunction(std::move(j)); //init TrafficMovement diff --git a/src/Traffic/TrafficMovementFactory.cpp b/src/Traffic/TrafficMovementFactory.cpp index da8eb9b31b43e407bf60c32967260774e2b0b89f..eadcbccbe04df0c8bd3f2d5b0e9bdbeda68e4b2e 100644 --- a/src/Traffic/TrafficMovementFactory.cpp +++ b/src/Traffic/TrafficMovementFactory.cpp @@ -24,7 +24,7 @@ void TrafficMovementFactory::initTrafficMovement(std::string path, real * pConcA { //Variables - real vehicleDensity = 0.01f; + real vehicleDensity = 0.05f; uint vehicleLength = 7; uint maxVelocity = 14; @@ -88,7 +88,7 @@ void TrafficMovementFactory::initTrafficMovement(std::string path, real * pConcA //Junctions std::vector <std::unique_ptr<Junction> > junctions; for (uint i = 0; i < junctionReader.junctions.size(); i++) { - junctions.push_back(std::make_unique <JunctionRandom>(junctionReader.junctions[i].inCells, junctionReader.junctions[i].outCells)); + junctions.push_back(std::make_unique <JunctionRandom>(junctionReader.junctions[i].inCells, junctionReader.junctions[i].outCells, 60)); junctions[i]->setCellIndexForNoUTurn(junctionReader.junctions[i].carCanNotEnterThisOutCell); } roadNetwork->setJunctions(move(junctions)); diff --git a/src/Traffic/TrafficMovementFactoryTestRoads.cpp b/src/Traffic/TrafficMovementFactoryTestRoads.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4f378d3f51cc4972d01ba12b9d0ab0996f3f08d --- /dev/null +++ b/src/Traffic/TrafficMovementFactoryTestRoads.cpp @@ -0,0 +1,86 @@ +#include "TrafficMovementFactory - Kopie.h" + +#include <iostream> + +#include "GridGenerator/StreetPointFinder/JunctionReader.h" +#include "GridGenerator/StreetPointFinder/SourceReader.h" +#include "GridGenerator/StreetPointFinder/SinkReader.h" + +#include "RoadNetwork/RoadMaker.h" +#include "TrafficMovement.h" +#include "Source/SourceRandom.h" +#include "Junction/JunctionRandom.h" +#include "Sink/SinkRandom.h" +#include "Output/ConcentrationByPosition.h" +#include "Output/ConcBySpeedAndAcceleration.h" +#include "Utilities/safe_casting.h" + + +void TrafficMovementFactoryTest::initTrafficMovement(real * pConcArray) +{ + //Variables + + uint roadLength = 20; + + real vehicleDensity = 0.1f; + + uint vehicleLength = 2; + uint maxVelocity = 5; + uint maxAcceleration = 1; + + real dawdlePossibility = (real) 0.2; //typical value: 0.2 + real slowToStartPossibility = (real) 0.4; + + bool useGPU = true; + bool useSlowToStart = true; + + + //make RoadNetwork + std::vector<int> road(20); + std::fill(road.begin(), road.end(), -1); + road[9] = 5; + auto roadNetwork = std::make_unique<RoadMaker>(road, maxVelocity, vehicleLength); + //RoadMaker(const uint roadLength, const uint maxVelocity, uint vehicleLength, const real 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 + + //Sources + std::unique_ptr<Source> source = std::make_unique <SourceRandom>(SourceRandom(0, 0.7f, maxVelocity)); + roadNetwork->addSource(source); + + //Sinks + std::unique_ptr<Sink> s = std::make_unique <SinkRandom>(SinkRandom(roadLength-1, 0.5f)); + roadNetwork->addSink(move(s)); + + //Junctions + std::vector<uint> inCellIndices = { 9 }; + std::vector<uint> outCellIndices = { 11 }; + + std::unique_ptr<Junction> j = std::make_unique<JunctionRandom>(JunctionRandom(inCellIndices, outCellIndices)); + roadNetwork->addJunction(std::move(j)); + + //init TrafficMovement + this->simulator = std::make_shared<TrafficMovement>(std::move(roadNetwork), dawdlePossibility); + if (useSlowToStart) simulator->setSlowToStart(slowToStartPossibility); + simulator->setMaxAcceleration(maxAcceleration); + if (useGPU) simulator->setUseGPU(pConcArray); + + //init ConcentrationOutwriter + if (!useGPU) { + std::unique_ptr<ConcentrationOutwriter> writer = std::make_unique<ConcBySpeedAndAcceleration>(ConcBySpeedAndAcceleration(simulator->getRoadLength(), pConcArray)); + simulator->setConcentrationOutwriter(move(writer)); + } +} + + +void TrafficMovementFactoryTest::calculateTimestep(uint step, uint stepForVTK) +{ + simulator->calculateTimestep(step); +} + +void TrafficMovementFactoryTest::loopThroughTimesteps(uint timeSteps) +{ + simulator->setSaveResultsTrue(timeSteps); + simulator->loopTroughTimesteps(timeSteps); + //std::cout << "Number of Cars: " << simulator->getNumberOfCars() << std::endl; +} diff --git a/src/Traffic/TrafficMovementFactoryTestRoads.h b/src/Traffic/TrafficMovementFactoryTestRoads.h new file mode 100644 index 0000000000000000000000000000000000000000..c1ae8b9dd5a34d7d986d14d5a2737d20311d5d37 --- /dev/null +++ b/src/Traffic/TrafficMovementFactoryTestRoads.h @@ -0,0 +1,21 @@ +# pragma once + +#include <VirtualFluidsDefinitions.h> + +#include <vector> +#include <memory> + +#include "TrafficMovementFactory.h" +#include "Core/DataTypes.h" +#include "GridGenerator/StreetPointFinder/StreetPointFinder.h" + + +class VF_PUBLIC TrafficMovementFactoryTest : + public TrafficMovementFactory { +public: + TrafficMovementFactoryTest() {}; + ~TrafficMovementFactoryTest() {}; + virtual void initTrafficMovement(real * pConcArray = nullptr); + virtual void calculateTimestep(uint step, uint stepForVTK); + void loopThroughTimesteps(uint timeSteps); +}; \ No newline at end of file diff --git a/targets/apps/LBM/Basel/resources/Junctions.txt b/targets/apps/LBM/Basel/resources/Junctions.txt index 4b4a4ec0aa09eab8ac8f8faabbd01db3aa6f5903..8570b2dfa657925a357d5a6e1bf668cd977bbdb8 100644 --- a/targets/apps/LBM/Basel/resources/Junctions.txt +++ b/targets/apps/LBM/Basel/resources/Junctions.txt @@ -1,3 +1,3 @@ 2 -in 1 6 7 4 out 5 2 3 9 +in 1 7 6 4 out 5 3 2 9 in 0 5 -1 -1 out 8 1 -1 -1 \ No newline at end of file diff --git a/targets/apps/LBM/TrafficTest/TrafficTest.cpp b/targets/apps/LBM/TrafficTest/TrafficTest.cpp index 58b4e71dc9ff8d517030305c4d4288aa9b377003..0ca1842aacd178bac876a08e93e8c7e4ff24ad95 100644 --- a/targets/apps/LBM/TrafficTest/TrafficTest.cpp +++ b/targets/apps/LBM/TrafficTest/TrafficTest.cpp @@ -11,38 +11,39 @@ int main() { - //{ - // std::clock_t start; - // double duration; - // start = std::clock(); + //Basel + { + std::clock_t start; + double duration; + start = std::clock(); - // uint numberOfTimesteps = 50; - // //Logger - // 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); + uint numberOfTimesteps = 1000; + //Logger + 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); - // TrafficMovementFactory * factory = new TrafficMovementFactory(); - // std::string path = "C:/Users/hiwi/BaselDokumente/"; - // factory->initTrafficMovement(path); - // for (uint step = 1; step <= numberOfTimesteps; step++) { - // factory->calculateTimestep(step, step); - // } - // - // - // duration = (std::clock() - start) / (double)CLOCKS_PER_SEC; + TrafficMovementFactory * factory = new TrafficMovementFactory(); + std::string path = "C:/Users/hiwi/BaselDokumente/"; + factory->initTrafficMovement(path); - // std::cout << "Dauer: " << duration << '\n'; - // - //} + for (uint step = 1; step <= numberOfTimesteps; step++) { + factory->calculateTimestep(step, step); + } + + + duration = (std::clock() - start) / (double)CLOCKS_PER_SEC; + std::cout << "Dauer: " << duration << '\n'; + + } @@ -50,190 +51,19 @@ int main() - ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - {uint numberOfTimesteps = 20; - - TrafficMovementFactoryTest * factory = new TrafficMovementFactoryTest(); - factory->initTrafficMovement(); - factory->loopThroughTimesteps(numberOfTimesteps); - - std::cout << std::endl << std::endl; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + //Testcases + //{uint numberOfTimesteps = 20; + //TrafficMovementFactoryTest * factory = new TrafficMovementFactoryTest(); + //factory->initTrafficMovement(); + //factory->loopThroughTimesteps(numberOfTimesteps); + //std::cout << std::endl << std::endl; } - - - - - - - - //std::vector<int> oneCar = { -1,1,-1,-1,-1,-1 }; - //std::vector<int> fiveCars = { 1, -1,-1, 1, -1, -1, -1, -1, 1, -1,-1,-1,-1,0,-1,-1,1,-1,-1,-1 }; - //int roadLength = 20; - //uint vehicleLength = 2; - //uint maxVelocity = 5; - //real dawdlePossibility = 0; - - //uint numberOfTimesteps = 10; - - ////concwriter-test - //{ - // - // std::vector<int> cars = { 1,-1,0,-1,0,-1,-1,-1,-1,-1 }; //IdleTest - // //cars = { -1,-1,0,-1,-1,1,-1,0,-1,0 }; //IdleTest2 - - // std::cout << "concwriter-test" << std::endl; - - - // auto roadNetwork = std::make_unique<RoadMaker>(cars, maxVelocity, vehicleLength); - // auto simulator = std::make_shared<TrafficMovement>(move(roadNetwork), dawdlePossibility); - - - // real concArray[10]; - // real * pConcArray = &concArray[0]; - - // auto writer = std::make_unique<ConcBySpeedAndAcceleration>(ConcBySpeedAndAcceleration(simulator->getRoadLength(), pConcArray, 10, maxVelocity)); - // simulator->setSaveResultsTrue(numberOfTimesteps); - // simulator->setConcentrationOutwriter(move(writer)); - - // simulator->loopTroughTimesteps(numberOfTimesteps); - - // std::cout << std::endl << std::endl; - //} - - - ////JunctionTest - //{ - // std::cout << "junction-test" << std::endl; - - // auto roadNetwork = std::make_unique<RoadMaker>(fiveCars, maxVelocity, vehicleLength); - - // std::vector <uint> in4 = { 9 }; - // std::vector<uint> out4 = { 10 }; - // std::unique_ptr<Junction> j = std::make_unique<JunctionRandom>(in4, out4); - // roadNetwork->addJunction(j); - - // std::shared_ptr<TrafficMovement> simulator = std::make_shared<TrafficMovement>(move(roadNetwork), 0); - // auto writer = std::make_unique<ConcBySpeedAndAcceleration>(ConcBySpeedAndAcceleration(simulator->getRoadLength(), maxVelocity)); - // simulator->setSaveResultsTrue(numberOfTimesteps); - // simulator->setConcentrationOutwriter(move(writer)); - - // simulator->loopTroughTimesteps(numberOfTimesteps); - - // std::cout << "Number of Cars: " << simulator->getNumberOfCars() << std::endl; - //} - - - //{ - // dawdlePossibility = 0; - // vector<int> car = { -1,-1,-1,4,-1,-1,-1,-1,-1 }; - - // unique_ptr<RoadMaker> r = make_unique<RoadMaker>(car, 5, 2); - - // vector <uint> in = { 4 }; - // vector <uint> out = { 5 }; - // unique_ptr<Junction> j = make_unique<JunctionRandom>(JunctionRandom(in, out)); - // r->addJunction(j); - - // shared_ptr<TrafficMovement> simulator = make_shared<TrafficMovement>(move(r), dawdlePossibility); - // simulator->setSaveResultsTrue(); - // simulator->initCalculation(1); - - // simulator->calculateTimestep(1); - - // bool success = false; - // if (simulator->getSpeedAtPosition(7) == 5) success = true; - // std::cout << success << std::endl; - // simulator->dispResults(); - //} - - - - ////OneWay initial distribution - //{ - // cout << "OneWay initial distribution" << endl; - // unique_ptr<RoadNetworkData> roadNetwork = make_unique<RoadMaker>(initialDistribution, maxVelocity, vehicleLength); - // shared_ptr<TrafficMovement> simulator = make_shared<TrafficMovement>(move(roadNetwork), dawdlePossibility); - // simulator->initCalculation(numberOfTimesteps); - //simulator->loopTroughTimesteps(); - // simulator->dispResults(); - // cout << endl << endl; - //} - - - //////OneWay slowToStart - //{ - // dawdlePossibility = 0; - // cout << "OneWay slowToStart" << endl; - // vector<int> initialDist = { -1,1,5,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,0,-1,-1,-1,-1,-1 }; - - // unique_ptr<RoadNetworkData> roadNetwork = make_unique<RoadMaker>(initialDist, maxVelocity, 1); - // shared_ptr<TrafficMovement> simulator = make_shared<TrafficMovement>(move(roadNetwork), dawdlePossibility); - // simulator->setSlowToStart(0.9999f); - // simulator->initCalculation(numberOfTimesteps); - //simulator->loopTroughTimesteps(); - // simulator->dispResults(); - // cout << endl << endl; - //} - - - - ////sources and sinks - //{ - // cout << "sources and sinks" << endl; - - // vector< unique_ptr<Sink> > sinks; - // sinks.push_back(make_unique <SinkRandom>(5, 0.5f)); - - // unique_ptr<RoadMaker> roadNetwork = make_unique<RoadMaker>(oneCar, maxVelocity, vehicleLength); - - // vector< unique_ptr<Source> > sources; - // sources.push_back(make_unique <SourceRandom>(0,1.0f, roadNetwork->getMaxVelocity())); - - // roadNetwork->setSources(move(sources)); - // roadNetwork->setSinks(move(sinks)); - - // shared_ptr<TrafficMovement> roadSource = make_shared<TrafficMovement>(move(roadNetwork), 0.0f); - - // roadSource->initCalculation(numberOfTimesteps); - // roadSource->loopTroughTimesteps(); - // roadSource->dispResults(); - // cout << endl << endl; - //} - - - - ////mergingRoad - //{ - // unique_ptr<RoadMaker> roadNetwork = make_unique<RoadMaker>(25, maxVelocity, vehicleLength); - - // vector< unique_ptr<Source> > sources; - // sources.push_back(make_unique <SourceRandom>(0, 1f, roadNetwork->getMaxVelocity())); - // sources.push_back(make_unique <SourceRandom>(10, 1f, roadNetwork->getMaxVelocity())); - // roadNetwork->setSources(move(sources)); - - // unique_ptr<Sink> s = make_unique <SinkRandom>(SinkRandom(24, 0f)); - // roadNetwork->addSink(move(s)); - - // vector< unique_ptr<Junction> > junctions; - // vector<uint> in = { 9,20 }; - // vector<uint> out = { 21 }; - // junctions.push_back(make_unique <JunctionRandom>(JunctionRandom(in, out))); - - // roadNetwork->setJunctions(junctions); - - // shared_ptr<TrafficMovement> mergingRoad = make_shared<TrafficMovement>(move(roadNetwork), dawdlePossibility); - - - // mergingRoad->initCalculation(numberOfTimesteps); - // mergingRoad->loopTroughTimesteps(); - // mergingRoad->dispResults(); - // cout << endl << endl; - //} }