From e8673bd4064f3f94090dbdb8ec15eace77e82be1 Mon Sep 17 00:00:00 2001 From: anagainaru Date: Thu, 2 Nov 2023 18:29:26 -0400 Subject: [PATCH] Testing for correctness of derived variables using add and magnitude Co-authored-by: lizdulac --- testing/adios2/CMakeLists.txt | 3 + testing/adios2/derived/CMakeLists.txt | 6 + .../derived/TestBPDerivedCorrectness.cpp | 183 ++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 testing/adios2/derived/CMakeLists.txt create mode 100644 testing/adios2/derived/TestBPDerivedCorrectness.cpp diff --git a/testing/adios2/CMakeLists.txt b/testing/adios2/CMakeLists.txt index bddb8485f4..47235dd577 100644 --- a/testing/adios2/CMakeLists.txt +++ b/testing/adios2/CMakeLists.txt @@ -13,3 +13,6 @@ add_subdirectory(performance) add_subdirectory(helper) add_subdirectory(hierarchy) add_subdirectory(backward_compatibility) +if (ADIOS2_HAVE_Derived) +add_subdirectory(derived) +endif() diff --git a/testing/adios2/derived/CMakeLists.txt b/testing/adios2/derived/CMakeLists.txt new file mode 100644 index 0000000000..2df2938853 --- /dev/null +++ b/testing/adios2/derived/CMakeLists.txt @@ -0,0 +1,6 @@ +#------------------------------------------------------------------------------# +#Distributed under the OSI - approved Apache License, Version 2.0. See +#accompanying file Copyright.txt for details. +#------------------------------------------------------------------------------# + +gtest_add_tests_helper(DerivedCorrectness MPI_ALLOW BP Derived. "") diff --git a/testing/adios2/derived/TestBPDerivedCorrectness.cpp b/testing/adios2/derived/TestBPDerivedCorrectness.cpp new file mode 100644 index 0000000000..002015b843 --- /dev/null +++ b/testing/adios2/derived/TestBPDerivedCorrectness.cpp @@ -0,0 +1,183 @@ +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +TEST(DerivedCorrectness, AddCorrectnessTest) +{ + const size_t Nx = 10, Ny = 3, Nz = 6; + const size_t steps = 2; + /** Application variable */ + std::default_random_engine generator; + std::uniform_real_distribution distribution(0.0, 10.0); + + std::vector simArray1(Nx * Ny * Nz); + std::vector simArray2(Nx * Ny * Nz); + std::vector simArray3(Nx * Ny * Nz); + for (size_t i = 0; i < Nx * Ny * Nz; ++i) + { + simArray1[i] = distribution(generator); + simArray2[i] = distribution(generator); + simArray3[i] = distribution(generator); + } + + adios2::ADIOS adios; + + adios2::IO bpOut = adios.DeclareIO("BPWriteAddExpression"); + + std::vector varname = {"sim1/Ux", "sim1/Uy", "sim1/Uz"}; + std::string derivedname = "derived/addU"; + + std::cout << "Define Variable " << varname[0] << std::endl; + auto Ux = bpOut.DefineVariable(varname[0], {Nx, Ny, Nz}, {0, 0, 0}, {Nx, Ny, Nz}); + std::cout << "Define Variable " << varname[1] << std::endl; + auto Uy = bpOut.DefineVariable(varname[1], {Nx, Ny, Nz}, {0, 0, 0}, {Nx, Ny, Nz}); + std::cout << "Define Variable " << varname[2] << std::endl; + auto Uz = bpOut.DefineVariable(varname[2], {Nx, Ny, Nz}, {0, 0, 0}, {Nx, Ny, Nz}); + std::cout << "Define Derived Variable " << derivedname << std::endl; + // clang-format off + auto addU = bpOut.DefineDerivedVariable(derivedname, + "x:" + varname[0] + " \n" + "y:" + varname[1] + " \n" + "z:" + varname[2] + " \n" + "x+y+z", + adios2::DerivedVarType::StoreData); + // clang-format on + std::string filename = "expAdd.bp"; + adios2::Engine bpFileWriter = bpOut.Open(filename, adios2::Mode::Write); + + for (int i = 0; i < steps; i++) + { + bpFileWriter.BeginStep(); + bpFileWriter.Put(Ux, simArray1.data()); + bpFileWriter.Put(Uy, simArray2.data()); + bpFileWriter.Put(Uz, simArray3.data()); + bpFileWriter.EndStep(); + } + bpFileWriter.Close(); + + adios2::IO bpIn = adios.DeclareIO("BPReadExpression"); + adios2::Engine bpFileReader = bpIn.Open(filename, adios2::Mode::Read); + + std::vector readUx; + std::vector readUy; + std::vector readUz; + std::vector readAdd; + + float calcA; + float epsilon = 0.01; + for (int i = 0; i < steps; i++) + { + bpFileReader.BeginStep(); + bpFileReader.Get(varname[0], readUx); + bpFileReader.Get(varname[1], readUy); + bpFileReader.Get(varname[2], readUz); + bpFileReader.Get(derivedname, readAdd); + bpFileReader.EndStep(); + + for (size_t ind = 0; ind < Nx * Ny * Nz; ++ind) + { + calcA = readUx[ind] + readUy[ind] + readUz[ind]; + EXPECT_TRUE(fabs(calcA - readAdd[ind]) < epsilon); + } + } + bpFileReader.Close(); +} + +TEST(DerivedCorrectness, MagCorrectnessTest) +{ + const size_t Nx = 2, Ny = 3, Nz = 10; + const size_t steps = 2; + // Application variable + std::default_random_engine generator; + std::uniform_real_distribution distribution(0.0, 10.0); + + std::vector simArray1(Nx * Ny * Nz); + std::vector simArray2(Nx * Ny * Nz); + std::vector simArray3(Nx * Ny * Nz); + for (size_t i = 0; i < Nx * Ny * Nz; ++i) + { + simArray1[i] = distribution(generator); + simArray2[i] = distribution(generator); + simArray3[i] = distribution(generator); + } + + adios2::ADIOS adios; + adios2::IO bpOut = adios.DeclareIO("BPWriteExpression"); + std::vector varname = {"sim2/Ux", "sim2/Uy", "sim2/Uz"}; + std::string derivedname = "derived/magU"; + + auto Ux = bpOut.DefineVariable(varname[0], {Nx, Ny, Nz}, {0, 0, 0}, {Nx, Ny, Nz}); + auto Uy = bpOut.DefineVariable(varname[1], {Nx, Ny, Nz}, {0, 0, 0}, {Nx, Ny, Nz}); + auto Uz = bpOut.DefineVariable(varname[2], {Nx, Ny, Nz}, {0, 0, 0}, {Nx, Ny, Nz}); + // clang-format off + auto magU = bpOut.DefineDerivedVariable(derivedname, + "x:" + varname[0] + " \n" + "y:" + varname[1] + " \n" + "z:" + varname[2] + " \n" + "magnitude(x,y,z)", + adios2::DerivedVarType::StoreData); + // clang-format on + std::string filename = "expMagnitude.bp"; + adios2::Engine bpFileWriter = bpOut.Open(filename, adios2::Mode::Write); + + for (int i = 0; i < steps; i++) + { + bpFileWriter.BeginStep(); + bpFileWriter.Put(Ux, simArray1.data()); + bpFileWriter.Put(Uy, simArray2.data()); + bpFileWriter.Put(Uz, simArray3.data()); + bpFileWriter.EndStep(); + } + bpFileWriter.Close(); + + adios2::IO bpIn = adios.DeclareIO("BPReadMagExpression"); + adios2::Engine bpFileReader = bpIn.Open(filename, adios2::Mode::Read); + + std::vector readUx; + std::vector readUy; + std::vector readUz; + std::vector readMag; + + float calcM; + float epsilon = 0.01; + for (int i = 0; i < steps; i++) + { + bpFileReader.BeginStep(); + auto varx = bpIn.InquireVariable(varname[0]); + auto vary = bpIn.InquireVariable(varname[1]); + auto varz = bpIn.InquireVariable(varname[2]); + auto varmag = bpIn.InquireVariable(derivedname); + + bpFileReader.Get(varx, readUx); + bpFileReader.Get(vary, readUy); + bpFileReader.Get(varz, readUz); + bpFileReader.Get(varmag, readMag); + bpFileReader.EndStep(); + + for (size_t ind = 0; ind < Nx * Ny * Nz; ++ind) + { + calcM = sqrt(pow(readUx[ind], 2) + pow(readUy[ind], 2) + pow(readUz[ind], 2)); + EXPECT_TRUE(fabs(calcM - readMag[ind]) < epsilon); + } + } + bpFileReader.Close(); +} + +int main(int argc, char **argv) +{ + int result; + ::testing::InitGoogleTest(&argc, argv); + + result = RUN_ALL_TESTS(); + + return result; +}