From 8e79b6f2d01d5e706c9694058849f7fc86a56e7f Mon Sep 17 00:00:00 2001 From: Anna Wellmann <a.wellmann@tu-braunschweig.de> Date: Wed, 13 Sep 2023 08:38:04 +0000 Subject: [PATCH] Add come test to coordinate transformation and rename --- src/gpu/VirtualFluids_GPU/CMakeLists.txt | 3 + src/gpu/VirtualFluids_GPU/GPU/CalcMac27.cu | 2 +- .../interpolateRotatingToStatic.cu | 16 +-- .../interpolateStaticToRotating.cu | 2 +- .../CoordinateTransformation.h | 54 +++++----- .../CoordinateTransformationTest.h | 101 ++++++++++++++++++ src/gpu/VirtualFluids_GPU/LBM/Simulation.cpp | 1 - 7 files changed, 141 insertions(+), 38 deletions(-) create mode 100644 src/gpu/VirtualFluids_GPU/LBM/GPUHelperFunctions/CoordinateTransformationTest.h diff --git a/src/gpu/VirtualFluids_GPU/CMakeLists.txt b/src/gpu/VirtualFluids_GPU/CMakeLists.txt index 4cd0d1df5..fdefde370 100644 --- a/src/gpu/VirtualFluids_GPU/CMakeLists.txt +++ b/src/gpu/VirtualFluids_GPU/CMakeLists.txt @@ -28,4 +28,7 @@ if(BUILD_VF_UNIT_TESTS) set_source_files_properties(Kernel/Utilities/DistributionHelperTests.cpp PROPERTIES LANGUAGE CUDA) set_source_files_properties(Parameter/ParameterTest.cpp PROPERTIES LANGUAGE CUDA) set_source_files_properties(PreCollisionInteractor/ActuatorFarmInlinesTest.cpp PROPERTIES LANGUAGE CUDA) + set_source_files_properties(GPU/GridScaling/interpolateStaticToRotatingInlinesTest.cpp PROPERTIES LANGUAGE CUDA) + set_source_files_properties(GPU/GridScaling/interpolateRotatingToStaticInlinesTest.cpp PROPERTIES LANGUAGE CUDA) + set_source_files_properties(LBM/GPUHelperFunctions/CoordinateTransformationTest.h PROPERTIES LANGUAGE CUDA) endif() diff --git a/src/gpu/VirtualFluids_GPU/GPU/CalcMac27.cu b/src/gpu/VirtualFluids_GPU/GPU/CalcMac27.cu index 63de33118..ba6d3ef7d 100644 --- a/src/gpu/VirtualFluids_GPU/GPU/CalcMac27.cu +++ b/src/gpu/VirtualFluids_GPU/GPU/CalcMac27.cu @@ -355,7 +355,7 @@ __global__ void LBCalcMacCompSP27RotatingToStatic( real velocityRotatingZ = vf::lbm::getCompressibleVelocityX3(distribution.f, rhoD[nodeIndex]); pressD[nodeIndex] = vf::lbm::getPressure(distribution.f, rhoD[nodeIndex], vxD[nodeIndex], vyD[nodeIndex], vzD[nodeIndex]); - rotateVelocityFromGlobalToRotating(velocityRotatingX, velocityRotatingY, velocityRotatingZ, angleX, angleY, angleZ); + rotateDataFromGlobalToRotating(velocityRotatingX, velocityRotatingY, velocityRotatingZ, angleX, angleY, angleZ); vxD[nodeIndex] = velocityRotatingX; vyD[nodeIndex] = velocityRotatingY; diff --git a/src/gpu/VirtualFluids_GPU/GPU/GridScaling/interpolateRotatingToStatic.cu b/src/gpu/VirtualFluids_GPU/GPU/GridScaling/interpolateRotatingToStatic.cu index 5aab3a2dc..236d8c8ab 100644 --- a/src/gpu/VirtualFluids_GPU/GPU/GridScaling/interpolateRotatingToStatic.cu +++ b/src/gpu/VirtualFluids_GPU/GPU/GridScaling/interpolateRotatingToStatic.cu @@ -367,7 +367,7 @@ __global__ void interpolateRotatingToStatic( //////////////////////////////////////////////////////////////////////////////// //! - rotate the velocities //! - rotateVelocityFromGlobalToRotating(vvx, vvy, vvz, angleX, angleY, angleZ); + rotateDataFromGlobalToRotating(vvx, vvy, vvz, angleX, angleY, angleZ); //////////////////////////////////////////////////////////////////////////////// // calculate the squares of the velocities @@ -382,23 +382,23 @@ __global__ void interpolateRotatingToStatic( // linear combinations for second order moments // The second order moments have to be rotated. First they are computed in a rotating frame of reference + real useNEQ = c1o1; // zero; //one; //.... one = on ..... zero = off real mxxPyyPzz = m000; real mxxMyy = -c2o3 * ((coefficients.a100 - coefficients.b010)) / omegaRotating * (c1o1 + m000); real mxxMzz = -c2o3 * ((coefficients.a100 - coefficients.c001)) / omegaRotating * (c1o1 + m000); - m011 = -c1o3 * ((coefficients.b001 + coefficients.c010)) / omegaRotating * (c1o1 + m000); - m101 = -c1o3 * ((coefficients.a001 + coefficients.c100)) / omegaRotating * (c1o1 + m000); - m110 = -c1o3 * ((coefficients.a010 + coefficients.b100)) / omegaRotating * (c1o1 + m000); + m011 = -c1o3 * ((coefficients.b001 + coefficients.c010)) / omegaRotating * (c1o1 + m000) * useNEQ; + m101 = -c1o3 * ((coefficients.a001 + coefficients.c100)) / omegaRotating * (c1o1 + m000) * useNEQ; + m110 = -c1o3 * ((coefficients.a010 + coefficients.b100)) / omegaRotating * (c1o1 + m000) * useNEQ; // rotate some second order moments rotateSecondOrderMomentsGlobalToRotating(m011, m101, m110, mxxMyy, mxxMzz, angleX, angleY, angleZ); // calculate remaining second order moments from previously rotated moments - real useNEQ = c1o1; // zero; //one; //.... one = on ..... zero = off - m200 = c1o3 * (mxxMyy + mxxMzz + mxxPyyPzz) * useNEQ; - m020 = c1o3 * (-c2o1 * mxxMyy + mxxMzz + mxxPyyPzz) * useNEQ; - m002 = c1o3 * (mxxMyy - c2o1 * mxxMzz + mxxPyyPzz) * useNEQ; + m200 = c1o3 * ( mxxMyy + mxxMzz + mxxPyyPzz) * useNEQ; + m020 = c1o3 * (-c2o1 * mxxMyy + mxxMzz + mxxPyyPzz) * useNEQ; + m002 = c1o3 * ( mxxMyy - c2o1 * mxxMzz + mxxPyyPzz) * useNEQ; // linear combinations for third order moments real mxxyPyzz, mxxyMyzz, mxxzPyyz, mxxzMyyz, mxyyPxzz, mxyyMxzz; diff --git a/src/gpu/VirtualFluids_GPU/GPU/GridScaling/interpolateStaticToRotating.cu b/src/gpu/VirtualFluids_GPU/GPU/GridScaling/interpolateStaticToRotating.cu index 81672b66e..7f8af0357 100644 --- a/src/gpu/VirtualFluids_GPU/GPU/GridScaling/interpolateStaticToRotating.cu +++ b/src/gpu/VirtualFluids_GPU/GPU/GridScaling/interpolateStaticToRotating.cu @@ -238,7 +238,7 @@ __global__ void interpolateStaticToRotating( //////////////////////////////////////////////////////////////////////////////// //! - rotate the velocities //! - rotateVelocityFromRotatingToGlobal(vvx, vvy, vvz, angleX, angleY, angleZ); + rotateDataFromRotatingToGlobal(vvx, vvy, vvz, angleX, angleY, angleZ); //////////////////////////////////////////////////////////////////////////////// // calculate the squares of the velocities diff --git a/src/gpu/VirtualFluids_GPU/LBM/GPUHelperFunctions/CoordinateTransformation.h b/src/gpu/VirtualFluids_GPU/LBM/GPUHelperFunctions/CoordinateTransformation.h index fea772f62..b6785fb28 100644 --- a/src/gpu/VirtualFluids_GPU/LBM/GPUHelperFunctions/CoordinateTransformation.h +++ b/src/gpu/VirtualFluids_GPU/LBM/GPUHelperFunctions/CoordinateTransformation.h @@ -7,27 +7,27 @@ using namespace vf::basics::constant; -__inline__ __device__ void rotateVelocityFromRotatingToGlobal(real &velocityX, real &velocityY, real &velocityZ, real angleX, - real angleY, real angleZ) +__inline__ __host__ __device__ void rotateDataFromRotatingToGlobal(real &datumX, real &datumY, real &datumZ, real angleX, real angleY, + real angleZ) { - real velocityXTemp = velocityX; - real velocityYTemp = velocityY; - real velocityZTemp = velocityZ; + real datumXTemp = datumX; + real datumYTemp = datumY; + real datumZTemp = datumZ; if (angleX != c0o1) { - velocityYTemp = velocityY * cos(angleX) - velocityZ * sin(angleX); - velocityZTemp = velocityY * sin(angleX) + velocityZ * cos(angleX); + datumYTemp = datumY * cos(angleX) - datumZ * sin(angleX); + datumZTemp = datumY * sin(angleX) + datumZ * cos(angleX); } else if (angleY != c0o1) { // rotate in y - velocityXTemp = velocityX * cos(angleY) + velocityZ * sin(angleY); - velocityZTemp = -velocityX * sin(angleY) + velocityZ * cos(angleY); + datumXTemp = datumX * cos(angleY) + datumZ * sin(angleY); + datumZTemp = -datumX * sin(angleY) + datumZ * cos(angleY); } else if (angleZ != c0o1) { // rotate in z - velocityXTemp = velocityX * cos(angleZ) - velocityY * sin(angleZ); - velocityYTemp = velocityX * sin(angleZ) + velocityY * cos(angleZ); + datumXTemp = datumX * cos(angleZ) - datumY * sin(angleZ); + datumYTemp = datumX * sin(angleZ) + datumY * cos(angleZ); } - velocityX = velocityXTemp; - velocityY = velocityYTemp; - velocityZ = velocityZTemp; + datumX = datumXTemp; + datumY = datumYTemp; + datumZ = datumZTemp; } __inline__ __device__ void transformRotatingToGlobal(real &globalX, real &globalY, real &globalZ, real localX, real localY, @@ -60,27 +60,27 @@ __inline__ __device__ void transformRotatingToGlobal(real &globalX, real &global } -__inline__ __device__ void rotateVelocityFromGlobalToRotating(real &velocityX, real &velocityY, real &velocityZ, real angleX, +__inline__ __device__ void rotateDataFromGlobalToRotating(real &datumX, real &datumY, real &datumZ, real angleX, real angleY, real angleZ) { - real velocityXTemp = velocityX; - real velocityYTemp = velocityY; - real velocityZTemp = velocityZ; + real datumXTemp = datumX; + real datumYTemp = datumY; + real datumZTemp = datumZ; if (angleX != c0o1) { - velocityYTemp = velocityY * cos(angleX) + velocityZ * sin(angleX); - velocityZTemp = -velocityY * sin(angleX) + velocityZ * cos(angleX); + datumYTemp = datumY * cos(angleX) + datumZ * sin(angleX); + datumZTemp = -datumY * sin(angleX) + datumZ * cos(angleX); } else if (angleY != c0o1) { // rotate in y - velocityXTemp = velocityX * cos(angleY) - velocityZ * sin(angleY); - velocityZTemp = velocityX * sin(angleY) + velocityZ * cos(angleY); + datumXTemp = datumX * cos(angleY) - datumZ * sin(angleY); + datumZTemp = datumX * sin(angleY) + datumZ * cos(angleY); } else if (angleZ != c0o1) { // rotate in z - velocityXTemp = velocityX * cos(angleZ) + velocityY * sin(angleZ); - velocityYTemp = -velocityX * sin(angleZ) + velocityY * cos(angleZ); + datumXTemp = datumX * cos(angleZ) + datumY * sin(angleZ); + datumYTemp = -datumX * sin(angleZ) + datumY * cos(angleZ); } - velocityX = velocityXTemp; - velocityY = velocityYTemp; - velocityZ = velocityZTemp; + datumX = datumXTemp; + datumY = datumYTemp; + datumZ = datumZTemp; } __inline__ __device__ void transformGlobalToRotating(real &rotatingX, real &rotatingY, real &rotatingZ, real globalX, diff --git a/src/gpu/VirtualFluids_GPU/LBM/GPUHelperFunctions/CoordinateTransformationTest.h b/src/gpu/VirtualFluids_GPU/LBM/GPUHelperFunctions/CoordinateTransformationTest.h new file mode 100644 index 000000000..af2b06bf0 --- /dev/null +++ b/src/gpu/VirtualFluids_GPU/LBM/GPUHelperFunctions/CoordinateTransformationTest.h @@ -0,0 +1,101 @@ + +#include "CoordinateTransformation.h" +#include "tests/testUtilities.h" + +bool isEqualWithAccuracy(real number, real expected, real accuracy) +{ + if (number > (expected - accuracy) && number < (expected + accuracy)) return true; + return false; +} + +MATCHER_P2(RealNearForContainer, containerExpected, accuracy, "") +{ + if (arg.size() != containerExpected.size()) { + std::cout << "The checked container does not have the same size as the expected container.\n" << std::endl; + return false; + } + + for (int i = 0; i < arg.size(); i++) { + if (!isEqualWithAccuracy(arg[i], containerExpected[i], accuracy)) { + std::cout << "First mismatching element at index " << i << ": The actual element " << std::to_string(arg[i]) + << " is not near the expected element " << std::to_string(containerExpected[i]) << ".\n"; + return false; + } + } + return true; +} + +MATCHER_P2(NearForContainer, containerExpected, matcher, "") +{ + if (arg.size() != containerExpected.size()) { + std::cout << "The checked container does not have the same size as the expected container.\n" << std::endl; + return false; + } + + for (int i = 0; i < arg.size(); i++) { + testing::ExplainMatchResult(matcher, arg[i], result_listener); + } + return true; +} + + +TEST(isEqualWithAccuracy, test) +{ + const real accuracy = 1.0; + const real expected = 0.0; + + EXPECT_TRUE(isEqualWithAccuracy( 0.0, expected, accuracy)); + EXPECT_TRUE(isEqualWithAccuracy( 0.999999, expected, accuracy)); + EXPECT_TRUE(isEqualWithAccuracy( -0.999999, expected, accuracy)); + EXPECT_FALSE(isEqualWithAccuracy( 1.000001, expected, accuracy)); + EXPECT_FALSE(isEqualWithAccuracy( -1.000001, expected, accuracy)); +} + +class CoordinateTransformationTest : public testing::Test +{ +protected: + real datumX = 1.; + real datumY = 2.; + real datumZ = 3.; + std::array<real, 3> data = { 1., 2., 3. }; + std::array<real, 3> dataBeforeTransformation = data; +}; + +TEST_F(CoordinateTransformationTest, rotatingToGlobal_zeroRotationAndTranslation) +{ + const std::array<real, 3> angles = { 0.0, 0.0, 0.0 }; + rotateDataFromRotatingToGlobal(data[0], data[1], data[2], angles[0], angles[1], angles[2]); + EXPECT_THAT(data, testing::Eq(dataBeforeTransformation)); +} + + +TEST_F(CoordinateTransformationTest, rotatingToGlobal_twoPiRotation) +{ + const std::array<real, 3> angles = { c2Pi, 0.0, 0.0 }; + rotateDataFromRotatingToGlobal(data[0], data[1], data[2], angles[0], angles[1], angles[2]); + EXPECT_THAT(data, RealNearForContainer(dataBeforeTransformation, 1e-6)); +} + +TEST_F(CoordinateTransformationTest, rotatingToGlobal_piRotation) +{ + const std::array<real, 3> angles = { cPi, 0.0, 0.0 }; + rotateDataFromRotatingToGlobal(data[0], data[1], data[2], angles[0], angles[1], angles[2]); + const std::array<real, 3> expected = { datumX, -datumY, -datumZ }; + EXPECT_THAT(data, RealNearForContainer(expected, 1e-6)); +} + +TEST_F(CoordinateTransformationTest, rotatingToGlobal_piHalfRotation) +{ + const std::array<real, 3> angles = { real(0.5 * cPi), 0.0, 0.0 }; + rotateDataFromRotatingToGlobal(data[0], data[1], data[2], angles[0], angles[1], angles[2]); + const std::array<real, 3> expected = { datumX, -datumZ, datumY }; + EXPECT_THAT(data, RealNearForContainer(expected, 1e-6)); +} + +TEST_F(CoordinateTransformationTest, rotatingToGlobal_minusPiHalfRotation) +{ + const std::array<real, 3> angles = { real(-0.5 * cPi), 0.0, 0.0 }; + rotateDataFromRotatingToGlobal(data[0], data[1], data[2], angles[0], angles[1], angles[2]); + const std::array<real, 3> expected = { datumX, datumZ, -datumY }; + EXPECT_THAT(data, RealNearForContainer(expected, 1e-6)); +} diff --git a/src/gpu/VirtualFluids_GPU/LBM/Simulation.cpp b/src/gpu/VirtualFluids_GPU/LBM/Simulation.cpp index 65999b0ae..6a9fe44f7 100644 --- a/src/gpu/VirtualFluids_GPU/LBM/Simulation.cpp +++ b/src/gpu/VirtualFluids_GPU/LBM/Simulation.cpp @@ -990,7 +990,6 @@ void Simulation::readAndWriteFiles(uint timestep) } //////////////////////////////////////////////////////////////////////// // rotating grid - UpdateGlobalCoordinates(para->getParD(1).get(), para->getRotatingGridParameter()->parameterRotDevice.get()); cudaMemoryManager->cudaCopyCoordDeviceToHost(1); -- GitLab