Skip to content

Commit

Permalink
Testing for correctness of derived variables using add and magnitude
Browse files Browse the repository at this point in the history
Co-authored-by: lizdulac <[email protected]>
  • Loading branch information
anagainaru and lizdulac committed Nov 2, 2023
1 parent c934202 commit 5e6ec33
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 2 deletions.
3 changes: 1 addition & 2 deletions source/adios2/toolkit/derived/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ namespace adios2
{
namespace detail
{
adios2::derived::ExpressionTree
ASTNode_to_ExpressionTree(adios2::detail::ASTNode *node)
adios2::derived::ExpressionTree ASTNode_to_ExpressionTree(adios2::detail::ASTNode *node)
{
adios2::derived::ExpressionTree exprTree_node(node->operation);
for (adios2::detail::ASTNode *e : node->sub_expr)
Expand Down
3 changes: 3 additions & 0 deletions testing/adios2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
6 changes: 6 additions & 0 deletions testing/adios2/derived/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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. "")
183 changes: 183 additions & 0 deletions testing/adios2/derived/TestBPDerivedCorrectness.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#include <cstdint>
#include <cstring>

#include <cmath>
#include <iostream>
#include <numeric>
#include <random>
#include <stdexcept>
#include <vector>

#include <adios2.h>
#include <gtest/gtest.h>

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<float> distribution(0.0, 10.0);

std::vector<float> simArray1(Nx * Ny * Nz);
std::vector<float> simArray2(Nx * Ny * Nz);
std::vector<float> 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<std::string> varname = {"sim1/Ux", "sim1/Uy", "sim1/Uz"};
std::string derivedname = "derived/addU";

std::cout << "Define Variable " << varname[0] << std::endl;
auto Ux = bpOut.DefineVariable<float>(varname[0], {Nx, Ny, Nz}, {0, 0, 0}, {Nx, Ny, Nz});
std::cout << "Define Variable " << varname[1] << std::endl;
auto Uy = bpOut.DefineVariable<float>(varname[1], {Nx, Ny, Nz}, {0, 0, 0}, {Nx, Ny, Nz});
std::cout << "Define Variable " << varname[2] << std::endl;
auto Uz = bpOut.DefineVariable<float>(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<float> readUx;
std::vector<float> readUy;
std::vector<float> readUz;
std::vector<float> 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<float> distribution(0.0, 10.0);

std::vector<float> simArray1(Nx * Ny * Nz);
std::vector<float> simArray2(Nx * Ny * Nz);
std::vector<float> 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<std::string> varname = {"sim2/Ux", "sim2/Uy", "sim2/Uz"};
std::string derivedname = "derived/magU";

auto Ux = bpOut.DefineVariable<float>(varname[0], {Nx, Ny, Nz}, {0, 0, 0}, {Nx, Ny, Nz});
auto Uy = bpOut.DefineVariable<float>(varname[1], {Nx, Ny, Nz}, {0, 0, 0}, {Nx, Ny, Nz});
auto Uz = bpOut.DefineVariable<float>(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<float> readUx;
std::vector<float> readUy;
std::vector<float> readUz;
std::vector<float> readMag;

float calcM;
float epsilon = 0.01;
for (int i = 0; i < steps; i++)
{
bpFileReader.BeginStep();
auto varx = bpIn.InquireVariable<float>(varname[0]);
auto vary = bpIn.InquireVariable<float>(varname[1]);
auto varz = bpIn.InquireVariable<float>(varname[2]);
auto varmag = bpIn.InquireVariable<float>(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;
}

0 comments on commit 5e6ec33

Please sign in to comment.