From 26817bc15b6c8485577ae9326e73b699fc6e90e4 Mon Sep 17 00:00:00 2001
From: Timon Habenicht <t.habenicht@tu-bs.de>
Date: Wed, 21 Nov 2018 13:35:28 +0100
Subject: [PATCH] adds L2-Norm Test

---
 .../Tests/L2NormTest/L2NormTest.cpp           | 49 +++++++++++++++++++
 .../Tests/L2NormTest/L2NormTest.h             | 30 ++++++++++++
 .../L2NormLogFileInformation.cpp              |  0
 .../L2NormLogFileInformation.h                |  0
 .../LogFileInformation/package.include        |  0
 .../Tests/L2NormTest/package.include          |  0
 .../L2NormCalculator/L2NormCalculator.cpp     | 15 +++++-
 .../ConfigFileReader/ConfigFileReader.cpp     | 29 ++++++++++-
 .../ConfigFileReader/ConfigFileReader.h       |  6 ++-
 .../NumericalTests/Utilities/Test/TestImp.cpp |  5 ++
 targets/tests/NumericalTests/config.txt       |  6 +++
 11 files changed, 135 insertions(+), 5 deletions(-)
 create mode 100644 targets/tests/NumericalTests/Tests/L2NormTest/L2NormTest.cpp
 create mode 100644 targets/tests/NumericalTests/Tests/L2NormTest/L2NormTest.h
 create mode 100644 targets/tests/NumericalTests/Tests/L2NormTest/LogFileInformation/L2NormLogFileInformation.cpp
 create mode 100644 targets/tests/NumericalTests/Tests/L2NormTest/LogFileInformation/L2NormLogFileInformation.h
 create mode 100644 targets/tests/NumericalTests/Tests/L2NormTest/LogFileInformation/package.include
 create mode 100644 targets/tests/NumericalTests/Tests/L2NormTest/package.include

diff --git a/targets/tests/NumericalTests/Tests/L2NormTest/L2NormTest.cpp b/targets/tests/NumericalTests/Tests/L2NormTest/L2NormTest.cpp
new file mode 100644
index 000000000..376a20792
--- /dev/null
+++ b/targets/tests/NumericalTests/Tests/L2NormTest/L2NormTest.cpp
@@ -0,0 +1,49 @@
+#include "L2NormTest.h"
+
+#include "Utilities\Calculator\L2NormCalculator\L2NormCalculator.h"
+#include "Utilities\Results\AnalyticalResults\AnalyticalResult.h"
+#include "Utilities\Results\SimulationResults\SimulationResults.h"
+
+std::shared_ptr<L2NormTest> L2NormTest::getNewInstance(std::shared_ptr< AnalyticalResults> analyticalResult)
+{
+	return std::shared_ptr<L2NormTest>(new L2NormTest(analyticalResult));
+}
+
+void L2NormTest::update()
+{
+	TestImp::update();
+}
+
+void L2NormTest::addSimulation(std::shared_ptr<TestSimulation> sim, std::shared_ptr<SimulationInfo> simInfo)
+{
+	TestImp::addSimulation(sim, simInfo);
+}
+
+void L2NormTest::evaluate()
+{
+	analyticalResult->calc(simResults.at(0));
+
+	std::vector< double> results = calculator->calc(analyticalResult->getVx(), simResults.at(0)->getVx());
+
+	makeConsoleOutput();
+}
+
+std::string L2NormTest::getLogFileOutput()
+{
+	return std::string();
+}
+
+std::vector<bool> L2NormTest::getPassedTests()
+{
+	return std::vector<bool>();
+}
+
+void L2NormTest::makeConsoleOutput()
+{
+
+}
+
+L2NormTest::L2NormTest(std::shared_ptr< AnalyticalResults> analyticalResult) : analyticalResult(analyticalResult)
+{
+	calculator = L2NormCalculator::getNewInstance();
+}
\ No newline at end of file
diff --git a/targets/tests/NumericalTests/Tests/L2NormTest/L2NormTest.h b/targets/tests/NumericalTests/Tests/L2NormTest/L2NormTest.h
new file mode 100644
index 000000000..c4e54fee5
--- /dev/null
+++ b/targets/tests/NumericalTests/Tests/L2NormTest/L2NormTest.h
@@ -0,0 +1,30 @@
+#ifndef L2_NORM_TEST_H
+#define L2_NORM_TEST_H
+
+#include "Utilities\Test\TestImp.h"
+
+#include <memory>
+
+class L2NormCalculator;
+class AnalyticalResults;
+
+class L2NormTest : public TestImp
+{
+public:
+	static std::shared_ptr<L2NormTest> getNewInstance(std::shared_ptr< AnalyticalResults> analyticalResult);
+
+	void update();
+	void addSimulation(std::shared_ptr< TestSimulation> sim, std::shared_ptr< SimulationInfo> simInfo);
+	void evaluate();
+	std::string getLogFileOutput();
+	std::vector< bool> getPassedTests();
+	void makeConsoleOutput();
+
+private:
+	L2NormTest() {};
+	L2NormTest(std::shared_ptr< AnalyticalResults> analyticalResult);
+
+	std::shared_ptr< L2NormCalculator> calculator;
+	std::shared_ptr< AnalyticalResults> analyticalResult;
+};
+#endif 
\ No newline at end of file
diff --git a/targets/tests/NumericalTests/Tests/L2NormTest/LogFileInformation/L2NormLogFileInformation.cpp b/targets/tests/NumericalTests/Tests/L2NormTest/LogFileInformation/L2NormLogFileInformation.cpp
new file mode 100644
index 000000000..e69de29bb
diff --git a/targets/tests/NumericalTests/Tests/L2NormTest/LogFileInformation/L2NormLogFileInformation.h b/targets/tests/NumericalTests/Tests/L2NormTest/LogFileInformation/L2NormLogFileInformation.h
new file mode 100644
index 000000000..e69de29bb
diff --git a/targets/tests/NumericalTests/Tests/L2NormTest/LogFileInformation/package.include b/targets/tests/NumericalTests/Tests/L2NormTest/LogFileInformation/package.include
new file mode 100644
index 000000000..e69de29bb
diff --git a/targets/tests/NumericalTests/Tests/L2NormTest/package.include b/targets/tests/NumericalTests/Tests/L2NormTest/package.include
new file mode 100644
index 000000000..e69de29bb
diff --git a/targets/tests/NumericalTests/Utilities/Calculator/L2NormCalculator/L2NormCalculator.cpp b/targets/tests/NumericalTests/Utilities/Calculator/L2NormCalculator/L2NormCalculator.cpp
index 5bd23be58..29256bfd3 100644
--- a/targets/tests/NumericalTests/Utilities/Calculator/L2NormCalculator/L2NormCalculator.cpp
+++ b/targets/tests/NumericalTests/Utilities/Calculator/L2NormCalculator/L2NormCalculator.cpp
@@ -1,5 +1,8 @@
 #include "L2NormCalculator.h"
 
+#define _USE_MATH_DEFINES
+#include <math.h>
+
 std::shared_ptr<L2NormCalculator> L2NormCalculator::getNewInstance()
 {
 	return std::shared_ptr<L2NormCalculator>(new L2NormCalculator());
@@ -7,9 +10,17 @@ std::shared_ptr<L2NormCalculator> L2NormCalculator::getNewInstance()
 
 std::vector< double> L2NormCalculator::calc(std::vector<std::vector<double>> basicData, std::vector<std::vector<double>> divergentData)
 {
-	std::vector< double> result;
+	std::vector< double> results;
 
-	return result;
+	for (int i = 0; i < basicData.size(); i++) {
+		double sum = 0;
+		for (int j = 0; j < basicData.at(i).size(); j++) {
+			sum += (divergentData.at(i).at(j) - basicData.at(i).at(j))*(divergentData.at(i).at(j) - basicData.at(i).at(j)) / (basicData.at(i).at(j)*basicData.at(i).at(j));
+		}
+		results.push_back(sqrt(sum));
+	}
+	
+	return results;
 }
 
 L2NormCalculator::L2NormCalculator()
diff --git a/targets/tests/NumericalTests/Utilities/ConfigFileReader/ConfigFileReader.cpp b/targets/tests/NumericalTests/Utilities/ConfigFileReader/ConfigFileReader.cpp
index 9a5f43446..0a877a253 100644
--- a/targets/tests/NumericalTests/Utilities/ConfigFileReader/ConfigFileReader.cpp
+++ b/targets/tests/NumericalTests/Utilities/ConfigFileReader/ConfigFileReader.cpp
@@ -6,24 +6,25 @@
 #include "utilities/input/Input.h"
 #include "utilities/StringUtil/StringUtil.h"
 
-#include "Utilities/TestInformation/TestInformationImp.h"
 #include "Utilities/TestSimulation/TestSimulationImp.h"
 #include "Utilities\TestQueue\TestQueueImp.h"
 
 #include "Utilities\LogFileWriter\LogFileWriter.h"
 #include "Utilities\LogFileQueue\LogFileQueueImp.h"
 
-
 #include "Simulation/TaylorGreenVortex/SimulationParameter/TaylorGreenSimulationParameter.h"
 #include "Simulation/TaylorGreenVortex/LogFileInformation/TaylorGreenLogFileInformation.h"
 #include "Simulation\TaylorGreenVortex\SimulationInfo\TaylorGreenVortexSimulationInfo.h"
+#include "Simulation\TaylorGreenVortex\AnalyticalResults\TaylorGreenVortexAnalyticalResults.h"
 
 #include "Simulation/ShearWave/SimulationParameter/ShearWaveSimulationParameter.h"
 #include "Simulation/ShearWave/LogFileInformation/ShearWaveLogFileInformation.h"
 #include "Simulation\ShearWave\SimulationInfo\ShearWaveSimulationInfo.h"
+#include "Simulation\ShearWave\AnalyticalResults\ShearWaveAnalyticalResults.h"
 
 #include "Tests/PhiAndNuTest/PhiAndNuTest.h"
 #include "Tests\PhiAndNuTest\LogFileInformation\PhiAndNuLogFileInformation.h"
+#include "Tests\L2NormTest\L2NormTest.h"
 
 #include "Utilities/LogFileInformation/LogFileInformation.h"
 #include "Utilities/LogFileInformation/BasicSimulationInfo/BasicSimulationInfo.h"
@@ -80,10 +81,12 @@ void ConfigFileReader::readConfigFile(const std::string aFilePath)
 	amplitudeTGV = StringUtil::toDoubleVector(input->getValue("Amplitude_TGV"));
 	u0TGV = StringUtil::toDoubleVector(input->getValue("u0_TGV"));
 	nuAndPhiTestTGV = StringUtil::toBool(input->getValue("PhiAndNuTest_TGV"));
+	l2NormTestTGV = StringUtil::toBool(input->getValue("L2NormTest_TGV"));
 
 	v0SW = StringUtil::toDoubleVector(input->getValue("v0_SW"));
 	u0SW = StringUtil::toDoubleVector(input->getValue("u0_SW"));
 	nuAndPhiTestSW = StringUtil::toBool(input->getValue("PhiAndNuTest_SW"));
+	l2NormTestSW = StringUtil::toBool(input->getValue("L2NormTest_SW"));
 
 	numberOfTimeSteps = StringUtil::toInt(input->getValue("NumberOfTimeSteps"));
 	basisTimeStepLength = StringUtil::toInt(input->getValue("BasisTimeStepLength"));
@@ -176,11 +179,14 @@ void ConfigFileReader::makeTaylorGreenSimulations(std::string kernelName, double
 	simParaTGV.resize(0);
 	std::vector< std::shared_ptr< SimulationInfo>> simInfoTGV;
 	simInfoTGV.resize(0);
+	std::vector< std::shared_ptr< AnalyticalResults>> analyResultTGV;
+	analyResultTGV.resize(0);
 
 	for (int i = 0; i < tgv.size(); i++)
 		if (tgv.at(i)) {
 			simParaTGV.push_back(TaylorGreenSimulationParameter::getNewInstance(kernelName, u0, amplitude, viscosity, rho0, lx.at(i), lz.at(i), l0, numberOfTimeSteps, basisTimeStepLength, startStepCalculation, ySliceForCalculation, grids.at(i), maxLevel, numberOfGridLevels, writeFiles, startStepFileWriter, filePath, devices));
 			simInfoTGV.push_back(TaylorGreenVortexSimulationInfo::getNewInstance(u0, amplitude, l0, lx.at(i), viscosity, kernelName, "TaylorGreenVortex"));
+			analyResultTGV.push_back(TaylorGreenAnalyticalResults::getNewInstance(viscosity, u0, amplitude, l0, rho0));
 		}
 
 	std::vector< std::shared_ptr< TestSimulation>> testSimTGV = buildTestSimulation(simParaTGV, simInfoTGV);
@@ -192,6 +198,11 @@ void ConfigFileReader::makeTaylorGreenSimulations(std::string kernelName, double
 		std::shared_ptr< PhiAndNuInformation> phiNuLogFileInfo = PhiAndNuInformation::getNewInstance(phiAndNuTests);
 		testLogFileInfo.push_back(phiNuLogFileInfo);
 	}
+
+	if (l2NormTestTGV) {
+		std::vector< std::shared_ptr< L2NormTest>> l2NormTests = makeL2NormTests(testSimTGV, simInfoTGV, analyResultTGV);
+	}
+	
 		
 
 	for (int i = 0; i < testSimTGV.size(); i++)
@@ -255,6 +266,20 @@ std::vector< std::shared_ptr< PhiAndNuTest>> ConfigFileReader::makePhiAndNuTests
 	return phiAndNuTests;
 }
 
+std::vector<std::shared_ptr<L2NormTest>> ConfigFileReader::makeL2NormTests(std::vector<std::shared_ptr<TestSimulation>> testSim, std::vector< std::shared_ptr< SimulationInfo>> simInfo, std::vector<std::shared_ptr< AnalyticalResults>> analyticalResults)
+{
+	std::vector<std::shared_ptr<L2NormTest>> l2Tests;
+	for (int i = 0; i < testSim.size(); i++) {
+		std::shared_ptr<L2NormTest> test = L2NormTest::getNewInstance(analyticalResults.at(i));
+		test->addSimulation(testSim.at(i), simInfo.at(i));
+		testSim.at(i)->registerSimulationObserver(test);
+		l2Tests.push_back(test);
+		testQueue->addTest(test);
+	}
+
+	return l2Tests;
+}
+
 bool ConfigFileReader::shouldSimulationGroupRun(std::vector<bool> test)
 {
 	for (int i = 0; i < test.size(); i++) {
diff --git a/targets/tests/NumericalTests/Utilities/ConfigFileReader/ConfigFileReader.h b/targets/tests/NumericalTests/Utilities/ConfigFileReader/ConfigFileReader.h
index 7c67ec8e5..da03b2eb6 100644
--- a/targets/tests/NumericalTests/Utilities/ConfigFileReader/ConfigFileReader.h
+++ b/targets/tests/NumericalTests/Utilities/ConfigFileReader/ConfigFileReader.h
@@ -11,6 +11,7 @@ class SimulationParameter;
 class SimulationInfo;
 class TestCout;
 class PhiAndNuTest;
+class L2NormTest;
 class TestSimulation;
 class TestQueueImp;
 class TestQueue;
@@ -20,6 +21,7 @@ class LogFileInformation;
 class LogFileTimeInformation;
 class TestLogFileInformation;
 class SimulationLogFileInformation;
+class AnalyticalResults;
 
 class ConfigFileReader
 {
@@ -46,13 +48,15 @@ private:
 	void makeShearWaveSimulations(std::string kernelName, double viscosity, double u0, double v0);
 
 	std::vector< std::shared_ptr< PhiAndNuTest>> makePhiAndNuTests(std::vector< std::shared_ptr< TestSimulation>> testSim, std::vector< std::shared_ptr< SimulationInfo>> simInfo, double viscosity);
-	
+	std::vector< std::shared_ptr< L2NormTest>> makeL2NormTests(std::vector<std::shared_ptr< TestSimulation>> testSim, std::vector< std::shared_ptr< SimulationInfo>> simInfo, std::vector<std::shared_ptr< AnalyticalResults>> analyticalResults);
+
 	bool shouldSimulationGroupRun(std::vector<bool> test);
 
 
 	std::vector<double> u0SW, v0SW;
 	std::vector<double> amplitudeTGV, u0TGV;
 	bool nuAndPhiTestTGV, nuAndPhiTestSW;
+	bool l2NormTestTGV, l2NormTestSW;
 	std::string dataToCalcPhiAndNuTest;
 	std::vector<double> viscosity;
 	real rho0;
diff --git a/targets/tests/NumericalTests/Utilities/Test/TestImp.cpp b/targets/tests/NumericalTests/Utilities/Test/TestImp.cpp
index 0b9df1eba..cc87445c2 100644
--- a/targets/tests/NumericalTests/Utilities/Test/TestImp.cpp
+++ b/targets/tests/NumericalTests/Utilities/Test/TestImp.cpp
@@ -28,6 +28,11 @@ void TestImp::addSimulation(std::shared_ptr<TestSimulation> sim, std::shared_ptr
 	simResults.resize(simResults.size() + 1);
 }
 
+std::string TestImp::getSimulationName()
+{
+	return simulationName;
+}
+
 TestImp::TestImp()
 {
 	simulationRun.resize(0);
diff --git a/targets/tests/NumericalTests/config.txt b/targets/tests/NumericalTests/config.txt
index 53aeef3e2..33f6eb33e 100644
--- a/targets/tests/NumericalTests/config.txt
+++ b/targets/tests/NumericalTests/config.txt
@@ -42,6 +42,12 @@ DataToCalcPhiAndNuTest="Vx"
 PhiAndNuTest_TGV=true
 PhiAndNuTest_SW=true
 
+##################################################
+#			L2-Norm Test Parameter				 #
+##################################################
+L2NormTest_TGV=true
+L2NormTest_SW=true
+
 ##################################################
 #			Simulation To Perform				 #
 ##################################################
-- 
GitLab