diff --git a/src/Traffic/Junction/JunctionRandom.cpp b/src/Traffic/Junction/JunctionRandom.cpp index fd81128047ad0a2e50a3ebabd0a6e9d057fd60d7..b3bfdd4e0ead2a78223eafb9d425404abc104ef2 100644 --- a/src/Traffic/Junction/JunctionRandom.cpp +++ b/src/Traffic/Junction/JunctionRandom.cpp @@ -92,9 +92,8 @@ void JunctionRandom::updateJunction() void JunctionRandom::calculateTimeStep(TrafficMovement& road) { - index = 0; + uint index = 0; for (int carSpeed : data.carsOnJunction) { - if (carSpeed >= 0) { //check if there is a car on the junction if (carSpeed == 0) carSpeed += 1; @@ -103,27 +102,21 @@ void JunctionRandom::calculateTimeStep(TrafficMovement& road) } ++index; } - writeConcentrations(road); } void JunctionRandom::applyRules(int & carSpeed, const int & index, TrafficMovement& road) { - remainingDistance = static_cast<uint>(carSpeed) - data.alreadyMoved[index]; - + int remainingDistance = static_cast<uint>(carSpeed) - data.alreadyMoved[index]; if (remainingDistance > 0) { - outCell = chooseOutCell(index); + int outCell = chooseOutCell(index); if (outCell >= 0) { breakCar(outCell, carSpeed, remainingDistance, index, road); - if (remainingDistance > 0) { - moveCar(outCell, carSpeed, index, road); - } - else { - data.carsOnJunction[index] = 0; + moveCar(outCell, carSpeed, index, remainingDistance, road); + return; } - return; } } data.carsOnJunction[index] = 0; //cars, which can't cross the junctionin one timestep, because they already moved to many cells, loose their speed. @@ -131,9 +124,9 @@ void JunctionRandom::applyRules(int & carSpeed, const int & index, TrafficMoveme } -void JunctionRandom::breakCar(uint outCellIndex, int &speed, uint &remainingDistance, const int & index, TrafficMovement& road) +void JunctionRandom::breakCar(uint outCellIndex, int &speed, int &remainingDistance, const int & index, TrafficMovement& road) { - gap = road.getGapAfterOutCell(outCellIndex, remainingDistance); + int gap = road.getGapAfterOutCell(outCellIndex, remainingDistance); if (gap < remainingDistance) { speed = speed - remainingDistance + gap; remainingDistance = gap; @@ -141,7 +134,7 @@ void JunctionRandom::breakCar(uint outCellIndex, int &speed, uint &remainingDist } -void JunctionRandom::moveCar(uint outCell, int & carSpeed, const int & index, TrafficMovement& road) +void JunctionRandom::moveCar(uint outCell, int & carSpeed, const int & index, int remainingDistance, TrafficMovement& road) { road.moveJunctionCar(outCell, remainingDistance, carSpeed, data.oldSpeeds[index]); data.carsOnJunction[index] = -1; @@ -153,7 +146,7 @@ int JunctionRandom::chooseOutCell(const int & index) { std::vector<uint> outCellsTemp; - if (data.carCanNotEnterThisOutCell.size() > 0 && data.carCanNotEnterThisOutCell[index]) { + if (data.carCanNotEnterThisOutCell.size() > 0 && data.carCanNotEnterThisOutCell[index] >= 0) { for (uint cell : data.possibleOutCells) { if (cell != data.carCanNotEnterThisOutCell[index]) outCellsTemp.push_back(cell); @@ -162,10 +155,23 @@ int JunctionRandom::chooseOutCell(const int & index) else outCellsTemp = data.possibleOutCells; + int random = generateRandomOutCellIndex(castSizeT_Uint(outCellsTemp.size())); + if (random < 0) return -1; + + int outCell = outCellsTemp[random]; + data.possibleOutCells.erase(std::remove(data.possibleOutCells.begin(), data.possibleOutCells.end(), outCell), data.possibleOutCells.end()); + return outCell; +} - switch (outCellsTemp.size()) { + +int JunctionRandom::generateRandomOutCellIndex(uint outCellsTempSize) +{ + int random = 1000; + + switch (outCellsTempSize) { case 0: - return -1; + random = -1; + break; case 1: random = 0; break; @@ -180,11 +186,10 @@ int JunctionRandom::chooseOutCell(const int & index) break; default: std::cerr << "default in JunctionRandom::chooseOutCell()" << std::endl; + break; } - outCell = outCellsTemp[random]; - data.possibleOutCells.erase(std::remove(data.possibleOutCells.begin(), data.possibleOutCells.end(), outCell), data.possibleOutCells.end()); - return outCell; + return random; } void JunctionRandom::writeConcentrations(TrafficMovement& road) @@ -199,7 +204,6 @@ void JunctionRandom::writeConcentrations(TrafficMovement& road) } } - const std::vector<uint>& JunctionRandom::getInCellIndices() const { return data.inCellIndices; diff --git a/src/Traffic/Junction/JunctionRandom.h b/src/Traffic/Junction/JunctionRandom.h index 574e6098e6a42608fc3f76d9cf4c783951ca27cc..5f9142194c994be068510f1b02865118e62f0e30 100644 --- a/src/Traffic/Junction/JunctionRandom.h +++ b/src/Traffic/Junction/JunctionRandom.h @@ -38,18 +38,12 @@ private: uint getInCellsVectorIndex(uint cellIndex); void applyRules(int &carSpeed,const int &index, TrafficMovement& road); - void breakCar(uint outCellIndex, int &speed, uint &remainingDistance, const int & index, TrafficMovement& road); - void moveCar(uint outCell, int & carSpeed, const int & index, TrafficMovement& road); + void breakCar(uint outCellIndex, int &speed, int &remainingDistance, const int & index, TrafficMovement& road); + void moveCar(uint outCell, int & carSpeed, const int & index, int remainingDistance, TrafficMovement& road); int chooseOutCell(const int & index); + int generateRandomOutCellIndex(uint outCellsTempSize); void writeConcentrations(TrafficMovement& road); -private: - //variables for temporaray calculations - uint remainingDistance; - int outCell; - uint random; - uint gap; - int index; }; diff --git a/src/Traffic/TrafficMovement.cpp b/src/Traffic/TrafficMovement.cpp index cd461e835db9feb921e1d6f781a6df2dc80f505a..270b571918e9fc1bbd64ea6a4c9004f17c98c5c1 100644 --- a/src/Traffic/TrafficMovement.cpp +++ b/src/Traffic/TrafficMovement.cpp @@ -142,7 +142,7 @@ void TrafficMovement::calculateTimestep(uint step) } } - calculateJunctionStep(); + calculateJunctionStep(); calculateSourceStep(); @@ -195,7 +195,7 @@ void TrafficMovement::applyRules(uint carIndex) void TrafficMovement::accelerateCar(uint & speed) { - if (speed < road->maxVelocity) { + if (speed <= road->maxVelocity - maxAcceleration) { speed += maxAcceleration; } } @@ -222,8 +222,8 @@ void TrafficMovement::dawdleCar(uint carIndex, uint & speed) } //Standard NaSch - if (randomNumber < dawdlePossibility) { - if (speed >= maxAcceleration) + if (randomNumber < dawdlePossibility) { + if (speed >= maxAcceleration) speed -= maxAcceleration; else speed = 0; @@ -447,16 +447,18 @@ void TrafficMovement::visualizeVehicleLengthForVTK() if (road->safetyDistance != 0) { for (uint i = 0; i < road->roadLength; i++) { if ((*pcurrent)[i] > -1) { + checkSpeed((*pcurrent)[i]); 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 + else (road->currentWithLongVehicles)[neighbor] = (*pcurrent)[i]; neighbor = road->neighbors[neighbor]; } @@ -466,3 +468,9 @@ void TrafficMovement::visualizeVehicleLengthForVTK() } +void TrafficMovement::checkSpeed(uint speed) +{ + if (speed > road->maxVelocity){ + std::cerr << "Speed is greater than allowed maxSpeed" << std::endl; + } +} \ No newline at end of file diff --git a/src/Traffic/TrafficMovement.h b/src/Traffic/TrafficMovement.h index 0d14e41c90858b828021e08ab8bf0031bfe842be..96821e99ca1ffa60e3cfcdd7e11965ca5d2f4fdb 100644 --- a/src/Traffic/TrafficMovement.h +++ b/src/Traffic/TrafficMovement.h @@ -45,6 +45,8 @@ public: void visualizeVehicleLengthForVTK(); const std::vector<int> & getVehiclesForVTK(); + //for debugging + void checkSpeed(uint speed); private: //init @@ -80,7 +82,6 @@ private: //pollution void writeConcentration(uint index, uint oldSpeed); - private: std::unique_ptr<RoadNetworkData> road; std::unique_ptr<ConcentrationOutwriter> concWriter = nullptr; diff --git a/src/Traffic/TrafficMovementFactory.cpp b/src/Traffic/TrafficMovementFactory.cpp index dbdeb163765b2497e600a44fb0c1a0c0f8f2ef4d..044e7ec2bc9f98f1bdd2dc49eb7158faf6b1b635 100644 --- a/src/Traffic/TrafficMovementFactory.cpp +++ b/src/Traffic/TrafficMovementFactory.cpp @@ -34,22 +34,22 @@ void TrafficMovementFactory::initTrafficMovement(real * pconcArrayStart) //StreetPointFinder M:\Basel2019 C:\Users\schoen\Desktop\git\MS2 - finder.readStreets("C:/Users/schoen/Desktop/git/MS2/git/targets/apps/LBM/streetTest/resources/ExampleStreets.txt"); - finder.writeVTK("M:/Basel2019/results/ExampleStreets.vtk"); - //finder.readStreets("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/git/targets/apps/LBM/streetTest/resources/ExampleStreets.txt"); - //finder.writeVTK("C:/Users/hiwi/BaselDokumente/Basel_Ergebnisse/ExampleStreets.vtk"); + //finder.readStreets("C:/Users/schoen/Desktop/git/MS2/git/targets/apps/LBM/streetTest/resources/ExampleStreets.txt"); + //finder.writeVTK("M:/Basel2019/results/ExampleStreets.vtk"); + finder.readStreets("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/git/targets/apps/LBM/streetTest/resources/ExampleStreets.txt"); + finder.writeVTK("C:/Users/hiwi/BaselDokumente/Basel_Ergebnisse/ExampleStreets.vtk"); JunctionReader junctionReader; - junctionReader.readJunctions("C:/Users/schoen/Desktop/git/MS2/git/targets/apps/LBM/Basel/resources/Junctions.txt", finder); - //junctionReader.readJunctions("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/git/targets/apps/LBM/Basel/resources/Junctions.txt", finder); + //junctionReader.readJunctions("C:/Users/schoen/Desktop/git/MS2/git/targets/apps/LBM/Basel/resources/Junctions.txt", finder); + junctionReader.readJunctions("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/git/targets/apps/LBM/Basel/resources/Junctions.txt", finder); SinkReader sinkReader; - sinkReader.readSinks("C:/Users/schoen/Desktop/git/MS2/git/targets/apps/LBM/Basel/resources/Sinks.txt", finder); - //sinkReader.readSinks("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/git/targets/apps/LBM/Basel/resources/Sinks.txt", finder); + //sinkReader.readSinks("C:/Users/schoen/Desktop/git/MS2/git/targets/apps/LBM/Basel/resources/Sinks.txt", finder); + sinkReader.readSinks("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/git/targets/apps/LBM/Basel/resources/Sinks.txt", finder); SourceReader sourceReader; - sourceReader.readSources("C:/Users/schoen/Desktop/git/MS2/git/targets/apps/LBM/Basel/resources/Sources.txt", finder); - //sourceReader.readSources("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/git/targets/apps/LBM/Basel/resources/Sources.txt", finder); + //sourceReader.readSources("C:/Users/schoen/Desktop/git/MS2/git/targets/apps/LBM/Basel/resources/Sources.txt", finder); + sourceReader.readSources("C:/Users/hiwi/BaselDokumente/VirtualFluidsGPU/git/targets/apps/LBM/Basel/resources/Sources.txt", finder); //calculate RoadLength uint roadLength = 0; @@ -97,8 +97,8 @@ void TrafficMovementFactory::initTrafficMovement(real * pconcArrayStart) simulator->setConcentrationOutwriter(move(writer)); //prepare writing to vtk - this->outputPath = "M:/Basel2019/results/"; - //this->outputPath = "C:/Users/hiwi/BaselDokumente/Basel_Ergebnisse/"; + //this->outputPath = "M:/Basel2019/results/"; + this->outputPath = "C:/Users/hiwi/BaselDokumente/Basel_Ergebnisse/"; this->outputFilename = "ExampleStreets"; this->cars = &(simulator->getVehiclesForVTK()); diff --git a/src/VirtualFluids_GPU/LBM/Simulation.cpp b/src/VirtualFluids_GPU/LBM/Simulation.cpp index 35ffe8fed3122c299d0131bb10726eceef2e4962..9ded85b941bb844eac6beef00de56020c0798986 100644 --- a/src/VirtualFluids_GPU/LBM/Simulation.cpp +++ b/src/VirtualFluids_GPU/LBM/Simulation.cpp @@ -177,8 +177,8 @@ void Simulation::init(SPtr<Parameter> para, SPtr<GridProvider> gridProvider, std ////////////////////////////////////////////////////////////////////////// //Init Traffic by Anna ////////////////////////////////////////////////////////////////////////// - factory = new TrafficMovementFactory(); - factory->initTrafficMovement(para->getParH(0)->concentration); + trafficFactory = new TrafficMovementFactory(); + trafficFactory->initTrafficMovement(para->getParH(0)->concentration); ////////////////////////////////////////////////////////////////////////// @@ -1068,7 +1068,7 @@ void Simulation::run() //Calculate Traffic by Anna if (t % 100 == 0) { - factory->calculateTimestep(t / 100, t); + trafficFactory->calculateTimestep(t / 100, t); para->cudaCopyConcFile(0); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/VirtualFluids_GPU/LBM/Simulation.h b/src/VirtualFluids_GPU/LBM/Simulation.h index 73e350a5b17e27e8b2f7609cb6bfcb824b47f5b7..b36bdd30cf6fade441fbc217d620787e3115e0e9 100644 --- a/src/VirtualFluids_GPU/LBM/Simulation.h +++ b/src/VirtualFluids_GPU/LBM/Simulation.h @@ -90,7 +90,7 @@ protected: //////////////////////////////////////////////////////////////////////////// EnstrophyAnalyzer *enstrophyWriter; //////////////////////////////////////////////////////////////////////////// - TrafficMovementFactory *factory; + TrafficMovementFactory *trafficFactory; }; #endif diff --git a/targets/apps/LBM/Basel/main.cpp b/targets/apps/LBM/Basel/main.cpp index db7877e21f86069c1a9804192da786a7483eaf77..35a77858861e84e5b3cf47776e8a3268810bcca2 100644 --- a/targets/apps/LBM/Basel/main.cpp +++ b/targets/apps/LBM/Basel/main.cpp @@ -199,8 +199,9 @@ int main( int argc, char* argv[]) try { //multipleLevel("E:/temp/Basel2019/config/configBasel.txt"); //Tesla03 - multipleLevel("C:/Users/schoen/Desktop/bin/ReleaseBasel/configBasel.txt"); //Baumbart + //multipleLevel("C:/Users/schoen/Desktop/bin/ReleaseBasel/configBasel.txt"); //Baumbart //multipleLevel("F:/Work/Computations/gridGenerator/inp/configTest.txt"); + multipleLevel("C:/Users/hiwi/Desktop/configBasel.txt"); //Gamling } catch (const std::exception& e) {