Skip to content
Snippets Groups Projects
Commit e83c7bc5 authored by Chris Cantwell's avatar Chris Cantwell
Browse files

Merge branch 'compare_fields' into 'master'

Add a `Field::compare` method

See merge request !9
parents 142c6bd4 7e8f4042
No related branches found
Tags v4.3.4
Loading
...@@ -99,6 +99,39 @@ public: ...@@ -99,6 +99,39 @@ public:
return *this; return *this;
} }
/**
* @brief Compare this field to another field, with absolute
* tolerance tol.
* @return bool
*/
bool compare(Field<TType, TState> &rhs, double tol)
{
const std::vector<BlockAttributes>& rhs_blocks = rhs.GetBlocks();
TType *store = GetStorage().GetCPUPtr();
TType *rhs_store = rhs.GetStorage().GetCPUPtr();
if (rhs_blocks.size() != block_attributes.size()) return false;
size_t i {0};
for (size_t bl = 0; bl < block_attributes.size(); ++bl){
size_t num_elements = block_attributes[bl].num_elements;
size_t num_pts = block_attributes[bl].num_pts;
// Check that each block have the same structure
if (num_elements != rhs_blocks[bl].num_elements) return false;
if (num_pts != rhs_blocks[bl].num_pts) return false;
// Compare elements in the blocks
for (size_t el = 0 ; el < num_elements; ++el) {
for (size_t coeff = 0; coeff < num_pts; ++coeff) {
i = coeff + el * num_pts + bl * num_pts * num_elements;
if (std::abs(store[i] - rhs_store[i]) > tol) return false;
}
}
}
return true;
}
/** /**
* @brief Static templated creation method. * @brief Static templated creation method.
* *
......
...@@ -32,6 +32,7 @@ class InitFields ...@@ -32,6 +32,7 @@ class InitFields
public: public:
Field<double, stateIn> *fixt_in; Field<double, stateIn> *fixt_in;
Field<double, stateOut> *fixt_out; Field<double, stateOut> *fixt_out;
Field<double, stateOut> *fixt_expected;
MultiRegions::ExpListSharedPtr fixt_explist{nullptr}; MultiRegions::ExpListSharedPtr fixt_explist{nullptr};
~InitFields() ~InitFields()
{ {
...@@ -67,8 +68,10 @@ public: ...@@ -67,8 +68,10 @@ public:
// Create two Field objects with a MemoryRegionCPU backend by default // Create two Field objects with a MemoryRegionCPU backend by default
auto f_in = Field<double, stateIn>::create(blocks_in); auto f_in = Field<double, stateIn>::create(blocks_in);
auto f_out = Field<double, stateOut>::create(blocks_out); auto f_out = Field<double, stateOut>::create(blocks_out);
auto f_expected = Field<double, stateOut>::create(blocks_out);
fixt_in = new Field<double, stateIn>(std::move(f_in)); fixt_in = new Field<double, stateIn>(std::move(f_in));
fixt_out = new Field<double, stateOut>(std::move(f_out)); fixt_out = new Field<double, stateOut>(std::move(f_out));
fixt_expected = new Field<double, stateOut>(std::move(f_expected));
} }
protected: protected:
......
...@@ -63,8 +63,28 @@ BOOST_FIXTURE_TEST_CASE(bwdtrans, Line) ...@@ -63,8 +63,28 @@ BOOST_FIXTURE_TEST_CASE(bwdtrans, Line)
} }
} }
BwdTrans<>::create(fixt_explist, "StdMat")->apply(*fixt_in, *fixt_out); // TODO: Initialise expected solution
x = fixt_expected->GetStorage().GetCPUPtr();
double *y = fixt_out->GetStorage().GetCPUPtr(); // For each element, initialise first coefficient to zero and rest
BOOST_TEST(y[0] == 1.0); // to 1.
for (auto const &block : fixt_expected->GetBlocks())
{
for (size_t el = 0; el < block.num_elements; ++el)
{
for (size_t coeff = 0; coeff < block.num_pts; ++coeff)
{
if (coeff == 0) {
*(x++) = 1.0;
}
else {
*(x++) = 0.0;
}
}
}
}
BwdTrans<>::create(fixt_explist, "StdMat")->apply(*fixt_in, *fixt_out);
double TOL {0.01};
BOOST_TEST( fixt_out->compare(*fixt_expected, TOL));
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment