From 729f12b587673713bd8b15f96958ed2e904bf91f Mon Sep 17 00:00:00 2001
From: hiwis <hiwis@irmb.tu-bs.de>
Date: Fri, 12 Apr 2019 13:21:46 +0200
Subject: [PATCH] reduced writing to vtk

---
 .../StreetPointFinder/StreetPointFinder.cpp   | 106 +++++++++++++-----
 .../StreetPointFinder/StreetPointFinder.h     |  10 ++
 .../TrafficMovementFactory - Kopie.cpp        |   8 +-
 src/Traffic/TrafficMovementFactory - Kopie.h  |   2 +-
 src/Traffic/TrafficMovementFactory.cpp        |  18 ++-
 src/Traffic/TrafficMovementFactory.h          |   6 +-
 src/VirtualFluids_GPU/LBM/Simulation.cpp      |   9 +-
 src/VirtualFluids_GPU/LBM/Simulation.h        |   1 +
 targets/apps/LBM/TrafficTest/Traffic_Main.cpp |  11 +-
 9 files changed, 122 insertions(+), 49 deletions(-)

diff --git a/src/GridGenerator/StreetPointFinder/StreetPointFinder.cpp b/src/GridGenerator/StreetPointFinder/StreetPointFinder.cpp
index 05aef5d5e..2195d25e1 100644
--- a/src/GridGenerator/StreetPointFinder/StreetPointFinder.cpp
+++ b/src/GridGenerator/StreetPointFinder/StreetPointFinder.cpp
@@ -195,13 +195,38 @@ void StreetPointFinder::findIndicesLB(SPtr<Grid> grid)
 void StreetPointFinder::writeVTK(std::string filename, const std::vector<int>& cars)
 {
 	uint numberOfCells = 0;
-	uint numberOfNodes = 0;
 
-	for (auto& street : streets)
-	{
-		numberOfCells += street.numberOfCells;
-		numberOfNodes += street.numberOfCells + 1;
-	}
+	*logging::out << logging::Logger::INFO_INTERMEDIATE << "StreetPointFinder::writeVTK( " << filename << " )" << "\n";
+
+	std::ofstream file;
+
+	file.open(filename);
+
+	prepareWriteVTK(file, numberOfCells);
+
+	//////////////////////////////////////////////////////////////////////////
+
+	file << "FIELD Label " << 3 << std::endl;
+
+	//////////////////////////////////////////////////////////////////////////
+
+	writeStreetsVTK(file, numberOfCells);
+
+	writeLengthsVTK(file, numberOfCells);
+
+	writeCarsVTK(file, numberOfCells, cars);
+
+	////////////////////////////////////////////////////////////////////////////
+
+	file.close();
+
+	*logging::out << logging::Logger::INFO_INTERMEDIATE << "done!\n";
+}
+
+
+void StreetPointFinder::writeReducedVTK(std::string filename, const std::vector<int>& cars)
+{
+	uint numberOfCells = 0;
 
 	*logging::out << logging::Logger::INFO_INTERMEDIATE << "StreetPointFinder::writeVTK( " << filename << " )" << "\n";
 
@@ -209,6 +234,34 @@ void StreetPointFinder::writeVTK(std::string filename, const std::vector<int>& c
 
 	file.open(filename);
 
+	prepareWriteVTK(file, numberOfCells);
+
+	//////////////////////////////////////////////////////////////////////////
+
+	file << "FIELD Label " << 1 << std::endl;
+
+	//////////////////////////////////////////////////////////////////////////
+
+	writeCarsVTK(file, numberOfCells, cars);
+
+	////////////////////////////////////////////////////////////////////////////
+
+	file.close();
+
+	*logging::out << logging::Logger::INFO_INTERMEDIATE << "done!\n";
+}
+
+void StreetPointFinder::prepareWriteVTK(std::ofstream & file, uint & numberOfCells)
+{
+
+	uint numberOfNodes = 0;
+
+	for (auto& street : streets)
+	{
+		numberOfCells += street.numberOfCells;
+		numberOfNodes += street.numberOfCells + 1;
+	}	
+
 	file << "# vtk DataFile Version 3.0\n";
 	file << "by MeshGenerator\n";
 	file << "ASCII\n";
@@ -251,11 +304,11 @@ void StreetPointFinder::writeVTK(std::string filename, const std::vector<int>& c
 	//////////////////////////////////////////////////////////////////////////
 
 	file << "\nCELL_DATA " << numberOfCells << std::endl;
+}
 
-	file << "FIELD Label " << 3 << std::endl;
-
-	//////////////////////////////////////////////////////////////////////////
 
+void StreetPointFinder::writeStreetsVTK(std::ofstream & file, uint numberOfCells)
+{
 	file << "StreetIndex 1 " << numberOfCells << " int" << std::endl;
 
 	uint streetIndex = 0;
@@ -267,24 +320,12 @@ void StreetPointFinder::writeVTK(std::string filename, const std::vector<int>& c
 		}
 		streetIndex++;
 	}
+}
 
-	//////////////////////////////////////////////////////////////////////////
-
-	file << "length 1 " << numberOfCells << " float" << std::endl;
-
-	for (auto& street : streets)
-	{
-		for (uint i = 0; i < street.numberOfCells; i++)
-		{
-			real length = std::sqrt((street.getCoordinateX(i) - street.getCoordinateX(0)) * (street.getCoordinateX(i) - street.getCoordinateX(0))
-				+ (street.getCoordinateY(i) - street.getCoordinateY(0)) * (street.getCoordinateY(i) - street.getCoordinateY(0)));
-
-			file << length << std::endl;
-		}
-	}
 
-	//////////////////////////////////////////////////////////////////////////
 
+void StreetPointFinder::writeCarsVTK(std::ofstream& file, uint numberOfCells, const std::vector<int>& cars)
+{
 	file << "Cars 1 " << numberOfCells << " float" << std::endl;
 
 	uint index = 0;
@@ -299,12 +340,23 @@ void StreetPointFinder::writeVTK(std::string filename, const std::vector<int>& c
 			index++;
 		}
 	}
+}
 
-	//////////////////////////////////////////////////////////////////////////
 
-	file.close();
+void StreetPointFinder::writeLengthsVTK(std::ofstream & file, uint numberOfCells)
+{
+	file << "length 1 " << numberOfCells << " float" << std::endl;
 
-	*logging::out << logging::Logger::INFO_INTERMEDIATE << "done!\n";
+	for (auto& street : streets)
+	{
+		for (uint i = 0; i < street.numberOfCells; i++)
+		{
+			real length = std::sqrt((street.getCoordinateX(i) - street.getCoordinateX(0)) * (street.getCoordinateX(i) - street.getCoordinateX(0))
+				+ (street.getCoordinateY(i) - street.getCoordinateY(0)) * (street.getCoordinateY(i) - street.getCoordinateY(0)));
+
+			file << length << std::endl;
+		}
+	}
 }
 
 
diff --git a/src/GridGenerator/StreetPointFinder/StreetPointFinder.h b/src/GridGenerator/StreetPointFinder/StreetPointFinder.h
index 177a4cbd2..1837c3e7c 100644
--- a/src/GridGenerator/StreetPointFinder/StreetPointFinder.h
+++ b/src/GridGenerator/StreetPointFinder/StreetPointFinder.h
@@ -50,6 +50,16 @@ struct VF_PUBLIC StreetPointFinder
 
 	void writeVTK(std::string filename, const std::vector<int>& cars = std::vector<int>());
 
+	void writeReducedVTK(std::string filename, const std::vector<int>& cars = std::vector<int>());
+
+	void prepareWriteVTK(std::ofstream& file, uint & numberOfCells);
+
+	void writeCarsVTK(std::ofstream& file, uint numberOfCells, const std::vector<int>& cars);
+
+	void writeLengthsVTK(std::ofstream& file, uint numberOfCells);
+
+	void writeStreetsVTK(std::ofstream& file, uint numberOfCells);
+
     void writeConnectionVTK(std::string filename, SPtr<Grid> grid);
 
     void writeSimulationFile( std::string gridPath, real concentration, uint numberOfLevels, uint level );
diff --git a/src/Traffic/TrafficMovementFactory - Kopie.cpp b/src/Traffic/TrafficMovementFactory - Kopie.cpp
index 3439b4c80..09444e3da 100644
--- a/src/Traffic/TrafficMovementFactory - Kopie.cpp	
+++ b/src/Traffic/TrafficMovementFactory - Kopie.cpp	
@@ -16,7 +16,7 @@
 #include "Utilities/safe_casting.h"
 
 
-void TrafficMovementFactoryTest::initTrafficMovement(real * pConcArray)
+void TrafficMovementFactoryTest::initTrafficMovement(bool useGPU, real * pConcArray)
 {
 	//Variables
 
@@ -31,7 +31,7 @@ void TrafficMovementFactoryTest::initTrafficMovement(real * pConcArray)
 	real dawdlePossibility = (real) 0.2; //typical value: 0.2
 	real slowToStartPossibility = (real) 0.4;
 
-	bool useGPU = true;
+	this->useGPU = true;
 	bool useSlowToStart = true;
 
 
@@ -67,10 +67,10 @@ void TrafficMovementFactoryTest::initTrafficMovement(real * pConcArray)
 	this->simulator = std::make_shared<TrafficMovement>(std::move(roadNetwork), dawdlePossibility);
 	if (useSlowToStart) simulator->setSlowToStart(slowToStartPossibility);	
 	simulator->setMaxAcceleration(maxAcceleration);
-	if (useGPU) simulator->setUseGPU(pConcArray);
+	if (this->useGPU) simulator->setUseGPU(pConcArray);
 
 	//init ConcentrationOutwriter
-	if (!useGPU) {
+	if (!this->useGPU) {
 		std::unique_ptr<ConcentrationOutwriter> writer = std::make_unique<ConcBySpeedAndAcceleration>(ConcBySpeedAndAcceleration(simulator->getRoadLength(), pConcArray));
 		simulator->setConcentrationOutwriter(move(writer));
 	}
diff --git a/src/Traffic/TrafficMovementFactory - Kopie.h b/src/Traffic/TrafficMovementFactory - Kopie.h
index 12c08ef75..c3d4356d9 100644
--- a/src/Traffic/TrafficMovementFactory - Kopie.h	
+++ b/src/Traffic/TrafficMovementFactory - Kopie.h	
@@ -15,7 +15,7 @@ class VF_PUBLIC TrafficMovementFactoryTest :
 public:
 	TrafficMovementFactoryTest() {};
 	~TrafficMovementFactoryTest() {};
-	virtual void initTrafficMovement(real * pConcArray = nullptr);
+	virtual void initTrafficMovement(bool useGPU, real * pConcArray = nullptr);
 	virtual void calculateTimestep(uint step, uint stepForVTK);
 	void loopThroughTimesteps(uint timeSteps);
 };
\ No newline at end of file
diff --git a/src/Traffic/TrafficMovementFactory.cpp b/src/Traffic/TrafficMovementFactory.cpp
index ad28aa89b..69dafeb23 100644
--- a/src/Traffic/TrafficMovementFactory.cpp
+++ b/src/Traffic/TrafficMovementFactory.cpp
@@ -22,7 +22,7 @@ TrafficMovementFactory::TrafficMovementFactory()
 }
 
 
-void TrafficMovementFactory::initTrafficMovement(std::string path, real * pConcArray)
+void TrafficMovementFactory::initTrafficMovement(std::string path, bool useGPU,  real * pConcArray)
 {
 	//Variables
 
@@ -35,7 +35,7 @@ void TrafficMovementFactory::initTrafficMovement(std::string path, real * pConcA
 	real dawdlePossibility = (real) 0.2; //typical value: 0.2
 	real slowToStartPossibility = (real) 0.3;
 
-	bool useGPU = true;
+	this->useGPU = true;
 	bool useSlowToStart = true;
 	useLogger = true;
 
@@ -55,7 +55,7 @@ void TrafficMovementFactory::initTrafficMovement(std::string path, real * pConcA
 	//TrafficLogger	
 	if (useLogger) {
 		TrafficLogger::startLogger(logfile);
-		TrafficLogger::writeSimulationStart(info, useGPU);
+		TrafficLogger::writeSimulationStart(info, this->useGPU);
 	}
 
 
@@ -130,7 +130,7 @@ void TrafficMovementFactory::initTrafficMovement(std::string path, real * pConcA
 
 
 	//init ConcentrationOutwriter
-	if (!useGPU) {
+	if (!this->useGPU) {
 		std::unique_ptr<ConcentrationOutwriter> writer = std::make_unique<ConcBySpeedAndAcceleration>(ConcBySpeedAndAcceleration(simulator->getRoadLength(), pConcArray));
 		simulator->setConcentrationOutwriter(move(writer));
 	}
@@ -147,7 +147,7 @@ void TrafficMovementFactory::initTrafficMovement(std::string path, real * pConcA
 
 
 	//GPU
-	if (useGPU) simulator->setUseGPU(pConcArray);
+	if (this->useGPU) simulator->setUseGPU(pConcArray);
 }
 
 
@@ -164,6 +164,14 @@ void TrafficMovementFactory::writeTimestep(uint stepForVTK)
 	finder.writeVTK(outputPath + outputFilename + "_" + std::to_string(stepForVTK) + ".vtk", *cars);
 }
 
+
+void TrafficMovementFactory::writeReducedTimestep(uint stepForVTK)
+{
+	simulator->visualizeVehicleLengthForVTK();
+	finder.writeReducedVTK(outputPath + outputFilename + "_" + std::to_string(stepForVTK) + ".vtk", *cars);
+}
+
+
 void TrafficMovementFactory::endSimulation(uint numTimesteps, double duration)
 {
 	if (!useLogger) return;
diff --git a/src/Traffic/TrafficMovementFactory.h b/src/Traffic/TrafficMovementFactory.h
index c5fa8b87e..be6f7a072 100644
--- a/src/Traffic/TrafficMovementFactory.h
+++ b/src/Traffic/TrafficMovementFactory.h
@@ -16,9 +16,10 @@ class VF_PUBLIC TrafficMovementFactory
 public:
 	TrafficMovementFactory();
 	~TrafficMovementFactory() {};
-	virtual void initTrafficMovement(std::string path, real * pConcArray = nullptr);
+	virtual void initTrafficMovement(std::string path, bool useGPU, real * pConcArray = nullptr);
 	virtual void calculateTimestep(uint step);
 	virtual void writeTimestep(uint stepForVTK);
+	void writeReducedTimestep(uint stepForVTK);
 	virtual void endSimulation(uint numTimesteps, double duration);
 
 protected:
@@ -29,6 +30,7 @@ protected:
 	std::string outputPath;
 	std::string outputFilename;
 	const std::vector<int>* cars;
-	bool useLogger;
 
+	bool useLogger;
+	bool useGPU;
 };
\ No newline at end of file
diff --git a/src/VirtualFluids_GPU/LBM/Simulation.cpp b/src/VirtualFluids_GPU/LBM/Simulation.cpp
index 5bbfcbb75..0fd1af90e 100644
--- a/src/VirtualFluids_GPU/LBM/Simulation.cpp
+++ b/src/VirtualFluids_GPU/LBM/Simulation.cpp
@@ -179,9 +179,10 @@ void Simulation::init(SPtr<Parameter> para, SPtr<GridProvider> gridProvider, std
    //////////////////////////////////////////////////////////////////////////
    trafficFactory = new TrafficMovementFactory();
    std::string path = "C:/Users/hiwi/BaselDokumente/";
-   trafficFactory->initTrafficMovement(path, para->getParH(0)->concentration);
-   //trafficFactory->initTrafficMovement(path, para->getParD(0)->concentration);
-
+   if (useTrafficGPU) 
+	   trafficFactory->initTrafficMovement(path, para->getParD(0)->concentration);
+   else
+	   trafficFactory->initTrafficMovement(path, para->getParH(0)->concentration);
 
    //////////////////////////////////////////////////////////////////////////
    //Allocate Memory for Drag Lift Calculation
@@ -1071,7 +1072,7 @@ void Simulation::run()
 				if (t % 100 == 0)
 				{
 					trafficFactory->calculateTimestep(t / 100);
-					para->cudaCopyConcFile(0);
+					if(!useTrafficGPU)	para->cudaCopyConcFile(0);
 					if (t % 1000 == 0)
 					trafficFactory->writeTimestep(t);					
 				}
diff --git a/src/VirtualFluids_GPU/LBM/Simulation.h b/src/VirtualFluids_GPU/LBM/Simulation.h
index b36bdd30c..40b774bf5 100644
--- a/src/VirtualFluids_GPU/LBM/Simulation.h
+++ b/src/VirtualFluids_GPU/LBM/Simulation.h
@@ -91,6 +91,7 @@ protected:
 	EnstrophyAnalyzer *enstrophyWriter;
 	////////////////////////////////////////////////////////////////////////////
 	TrafficMovementFactory *trafficFactory;
+	bool useTrafficGPU;
 
  };
 #endif
diff --git a/targets/apps/LBM/TrafficTest/Traffic_Main.cpp b/targets/apps/LBM/TrafficTest/Traffic_Main.cpp
index 7b630e648..d14cd4121 100644
--- a/targets/apps/LBM/TrafficTest/Traffic_Main.cpp
+++ b/targets/apps/LBM/TrafficTest/Traffic_Main.cpp
@@ -14,7 +14,8 @@ int main()
 
 	//////Basel
 	{
-		uint numberOfTimesteps = 1000;
+		uint numberOfTimesteps = 100;
+		bool useGPU = true;
 		
 		//Stephans Logger
 		logging::Logger::addStream(&std::cout);
@@ -22,10 +23,9 @@ int main()
 		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);
+		factory->initTrafficMovement(path, useGPU);
 
 		//clock
 		std::clock_t start;
@@ -44,8 +44,7 @@ int main()
 
 		std::cout << "Dauer: " << duration << '\n';
 
-		//factory->writeTimestep(numberOfTimesteps);
-	
+		//factory->writeTimestep(numberOfTimesteps);	
 	}
 
 
@@ -58,7 +57,7 @@ int main()
 	//{uint numberOfTimesteps = 20;
 
 	//TrafficMovementFactoryTest * factory = new TrafficMovementFactoryTest();
-	//factory->initTrafficMovement();
+	//factory->initTrafficMovement(useGPU);
 	//factory->loopThroughTimesteps(numberOfTimesteps);
 
 	//std::cout << std::endl << std::endl; }
-- 
GitLab