Removed opm_memcmp_double() - use cmp from common.
This commit is contained in:
parent
9b1efd7810
commit
5172652e73
@ -33,6 +33,7 @@ list (APPEND MAIN_SOURCE_FILES
|
||||
opm/core/grid/GridManager.cpp
|
||||
opm/core/grid/GridUtilities.cpp
|
||||
opm/core/grid/grid.c
|
||||
opm/core/grid/grid_equal.cpp
|
||||
opm/core/grid/cart_grid.c
|
||||
opm/core/grid/cornerpoint_grid.c
|
||||
opm/core/grid/cpgpreprocess/facetopology.c
|
||||
@ -132,7 +133,6 @@ list (APPEND MAIN_SOURCE_FILES
|
||||
opm/core/utility/VelocityInterpolation.cpp
|
||||
opm/core/utility/WachspressCoord.cpp
|
||||
opm/core/utility/miscUtilities.cpp
|
||||
opm/core/utility/opm_memcmp_double.c
|
||||
opm/core/utility/miscUtilitiesBlackoil.cpp
|
||||
opm/core/utility/NullStream.cpp
|
||||
opm/core/utility/parameters/Parameter.cpp
|
||||
@ -430,7 +430,6 @@ list (APPEND PUBLIC_HEADER_FILES
|
||||
opm/core/utility/Event_impl.hpp
|
||||
opm/core/utility/Factory.hpp
|
||||
opm/core/utility/MonotCubicInterpolator.hpp
|
||||
opm/core/utility/opm_memcmp_double.h
|
||||
opm/core/utility/NonuniformTableLinear.hpp
|
||||
opm/core/utility/NullStream.hpp
|
||||
opm/core/utility/RegionMapping.hpp
|
||||
|
@ -19,7 +19,6 @@
|
||||
|
||||
#include "config.h"
|
||||
#include <opm/core/grid.h>
|
||||
#include <opm/core/utility/opm_memcmp_double.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
@ -544,66 +543,3 @@ read_grid(const char *fname)
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool
|
||||
grid_equal(const struct UnstructuredGrid * grid1 , const struct UnstructuredGrid * grid2) {
|
||||
if ((grid1->dimensions == grid2->dimensions) &&
|
||||
(grid1->number_of_cells == grid2->number_of_cells) &&
|
||||
(grid1->number_of_faces == grid2->number_of_faces) &&
|
||||
(grid1->number_of_nodes == grid2->number_of_nodes)) {
|
||||
|
||||
// Exact integer comparisons
|
||||
{
|
||||
if (memcmp(grid1->face_nodepos , grid2->face_nodepos , (grid1->number_of_faces + 1) * sizeof * grid1->face_nodepos) != 0)
|
||||
return false;
|
||||
|
||||
if (memcmp(grid1->face_nodes , grid2->face_nodes , grid1->face_nodepos[grid1->number_of_faces] * sizeof * grid1->face_nodes) != 0)
|
||||
return false;
|
||||
|
||||
if (memcmp(grid1->face_cells , grid2->face_cells , 2 * grid1->number_of_faces * sizeof * grid1->face_cells) != 0)
|
||||
return false;
|
||||
|
||||
if (memcmp(grid1->cell_faces , grid2->cell_faces , grid1->cell_facepos[grid1->number_of_cells] * sizeof * grid1->cell_faces) != 0)
|
||||
return false;
|
||||
|
||||
if (memcmp(grid1->cell_facepos , grid2->cell_facepos , (grid1->number_of_cells + 1) * sizeof * grid1->cell_facepos) != 0)
|
||||
return false;
|
||||
|
||||
if (grid1->global_cell && grid2->global_cell) {
|
||||
if (memcmp(grid1->global_cell , grid2->global_cell , grid1->number_of_cells * sizeof * grid1->global_cell) != 0)
|
||||
return false;
|
||||
} else if (grid1->global_cell != grid2->global_cell)
|
||||
return false;
|
||||
|
||||
if (grid1->cell_facetag && grid2->cell_facetag) {
|
||||
if (memcmp(grid1->cell_facetag , grid2->cell_facetag , grid1->cell_facepos[grid1->number_of_cells] * sizeof * grid1->cell_facetag) != 0)
|
||||
return false;
|
||||
} else if (grid1->cell_facetag != grid2->cell_facetag)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Floating point comparisons.
|
||||
{
|
||||
if (opm_memcmp_double( grid1->node_coordinates , grid2->node_coordinates , grid1->dimensions * grid1->number_of_nodes) != 0)
|
||||
return false;
|
||||
|
||||
if (opm_memcmp_double( grid1->face_centroids , grid2->face_centroids , grid1->dimensions * grid1->number_of_faces) != 0)
|
||||
return false;
|
||||
|
||||
if (opm_memcmp_double( grid1->face_areas , grid2->face_areas , grid1->number_of_faces) != 0)
|
||||
return false;
|
||||
|
||||
if (opm_memcmp_double( grid1->face_normals , grid2->face_normals , grid1->dimensions * grid1->number_of_faces) != 0)
|
||||
return false;
|
||||
|
||||
if (opm_memcmp_double( grid1->cell_centroids , grid2->cell_centroids , grid1->dimensions * grid1->number_of_cells) != 0)
|
||||
return false;
|
||||
|
||||
if (opm_memcmp_double( grid1->cell_volumes , grid2->cell_volumes , grid1->number_of_cells) != 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
76
opm/core/grid/grid_equal.cpp
Normal file
76
opm/core/grid/grid_equal.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include <string.h> // C string.h to get memcmp()
|
||||
|
||||
#include <opm/common/util/numeric/cmp.hpp>
|
||||
|
||||
#include <opm/core/grid.h>
|
||||
|
||||
/*
|
||||
The grid_equal() function is separated out into a separate file to
|
||||
ensure that it is compiled with a C++ compiler, so that the
|
||||
namespace features used in the opm/common/util/numeric/cmp.cpp
|
||||
implementation compiles.
|
||||
*/
|
||||
|
||||
|
||||
bool
|
||||
grid_equal(const struct UnstructuredGrid * grid1 , const struct UnstructuredGrid * grid2) {
|
||||
if ((grid1->dimensions == grid2->dimensions) &&
|
||||
(grid1->number_of_cells == grid2->number_of_cells) &&
|
||||
(grid1->number_of_faces == grid2->number_of_faces) &&
|
||||
(grid1->number_of_nodes == grid2->number_of_nodes)) {
|
||||
|
||||
// Exact integer comparisons
|
||||
{
|
||||
if (memcmp(grid1->face_nodepos , grid2->face_nodepos , (grid1->number_of_faces + 1) * sizeof * grid1->face_nodepos) != 0)
|
||||
return false;
|
||||
|
||||
if (memcmp(grid1->face_nodes , grid2->face_nodes , grid1->face_nodepos[grid1->number_of_faces] * sizeof * grid1->face_nodes) != 0)
|
||||
return false;
|
||||
|
||||
if (memcmp(grid1->face_cells , grid2->face_cells , 2 * grid1->number_of_faces * sizeof * grid1->face_cells) != 0)
|
||||
return false;
|
||||
|
||||
if (memcmp(grid1->cell_faces , grid2->cell_faces , grid1->cell_facepos[grid1->number_of_cells] * sizeof * grid1->cell_faces) != 0)
|
||||
return false;
|
||||
|
||||
if (memcmp(grid1->cell_facepos , grid2->cell_facepos , (grid1->number_of_cells + 1) * sizeof * grid1->cell_facepos) != 0)
|
||||
return false;
|
||||
|
||||
if (grid1->global_cell && grid2->global_cell) {
|
||||
if (memcmp(grid1->global_cell , grid2->global_cell , grid1->number_of_cells * sizeof * grid1->global_cell) != 0)
|
||||
return false;
|
||||
} else if (grid1->global_cell != grid2->global_cell)
|
||||
return false;
|
||||
|
||||
if (grid1->cell_facetag && grid2->cell_facetag) {
|
||||
if (memcmp(grid1->cell_facetag , grid2->cell_facetag , grid1->cell_facepos[grid1->number_of_cells] * sizeof * grid1->cell_facetag) != 0)
|
||||
return false;
|
||||
} else if (grid1->cell_facetag != grid2->cell_facetag)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Floating point comparisons.
|
||||
{
|
||||
if (!Opm::cmp::double_ptr_equal( grid1->node_coordinates , grid2->node_coordinates , grid1->dimensions * grid1->number_of_nodes))
|
||||
return false;
|
||||
|
||||
if (!Opm::cmp::double_ptr_equal( grid1->face_centroids , grid2->face_centroids , grid1->dimensions * grid1->number_of_faces))
|
||||
return false;
|
||||
|
||||
if (!Opm::cmp::double_ptr_equal( grid1->face_areas , grid2->face_areas , grid1->number_of_faces))
|
||||
return false;
|
||||
|
||||
if (!Opm::cmp::double_ptr_equal( grid1->face_normals , grid2->face_normals , grid1->dimensions * grid1->number_of_faces))
|
||||
return false;
|
||||
|
||||
if (!Opm::cmp::double_ptr_equal( grid1->cell_centroids , grid2->cell_centroids , grid1->dimensions * grid1->number_of_cells))
|
||||
return false;
|
||||
|
||||
if (!Opm::cmp::double_ptr_equal( grid1->cell_volumes , grid2->cell_volumes , grid1->number_of_cells))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
#include "BlackoilState.hpp"
|
||||
#include <opm/common/util/numeric/cmp.hpp>
|
||||
#include <opm/core/props/BlackoilPropertiesInterface.hpp>
|
||||
|
||||
|
||||
using namespace Opm;
|
||||
|
||||
void
|
||||
@ -38,14 +40,19 @@ BlackoilState::equals(const SimulatorState& other,
|
||||
const BlackoilState* that = dynamic_cast <const BlackoilState*> (&other);
|
||||
bool equal = that != 0;
|
||||
equal = equal && SimulatorState::equals (other, epsilon);
|
||||
equal = equal && SimulatorState::vectorApproxEqual(this->surfacevol(),
|
||||
that->surfacevol(),
|
||||
epsilon);
|
||||
equal = equal && SimulatorState::vectorApproxEqual(this->gasoilratio(),
|
||||
that->gasoilratio(),
|
||||
epsilon);
|
||||
equal = equal && SimulatorState::vectorApproxEqual(this->rv(),
|
||||
that->rv(),
|
||||
epsilon);
|
||||
equal = equal && cmp::double_vector_equal(this->surfacevol(),
|
||||
that->surfacevol(),
|
||||
cmp::default_abs_epsilon,
|
||||
epsilon);
|
||||
|
||||
equal = equal && cmp::double_vector_equal(this->gasoilratio(),
|
||||
that->gasoilratio(),
|
||||
cmp::default_abs_epsilon,
|
||||
epsilon);
|
||||
|
||||
equal = equal && cmp::double_vector_equal(this->rv(),
|
||||
that->rv(),
|
||||
cmp::default_abs_epsilon,
|
||||
epsilon);
|
||||
return equal;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <opm/common/ErrorMacros.hpp>
|
||||
#include <opm/common/util/numeric/cmp.hpp>
|
||||
#include <opm/core/simulator/SimulatorState.hpp>
|
||||
|
||||
#include <cmath>
|
||||
@ -12,33 +13,15 @@ SimulatorState::equals (const SimulatorState& other,
|
||||
bool equal = (num_phases_ == other.num_phases_);
|
||||
|
||||
// if we use &=, then all the tests will be run regardless
|
||||
equal = equal && vectorApproxEqual( pressure() , other.pressure() , epsilon);
|
||||
equal = equal && vectorApproxEqual( temperature() , other.temperature() , epsilon);
|
||||
equal = equal && vectorApproxEqual( facepressure() , other.facepressure() , epsilon);
|
||||
equal = equal && vectorApproxEqual( faceflux() , other.faceflux() , epsilon);
|
||||
equal = equal && vectorApproxEqual( saturation() , other.saturation() , epsilon);
|
||||
equal = equal && cmp::double_vector_equal( pressure() , other.pressure() , cmp::default_abs_epsilon , epsilon);
|
||||
equal = equal && cmp::double_vector_equal( temperature() , other.temperature() , cmp::default_abs_epsilon , epsilon);
|
||||
equal = equal && cmp::double_vector_equal( facepressure() , other.facepressure() , cmp::default_abs_epsilon , epsilon);
|
||||
equal = equal && cmp::double_vector_equal( faceflux() , other.faceflux() , cmp::default_abs_epsilon , epsilon);
|
||||
equal = equal && cmp::double_vector_equal( saturation() , other.saturation() , cmp::default_abs_epsilon , epsilon);
|
||||
|
||||
return equal;
|
||||
}
|
||||
|
||||
bool
|
||||
SimulatorState::vectorApproxEqual(const std::vector<double>& v1,
|
||||
const std::vector<double>& v2,
|
||||
double epsilon) {
|
||||
if (v1.size() != v2.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < v1.size(); i++) {
|
||||
const double diff = std::abs(v1[i] - v2[i]);
|
||||
const double scale = std::abs(v1[i]) + std::abs(v2[i]);
|
||||
if (diff > epsilon * scale) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
|
@ -1,30 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
Ahhh - the joys of comparing floating point numbers ....
|
||||
|
||||
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
|
||||
*/
|
||||
|
||||
int opm_memcmp_double(const double * p1 , const double *p2 , size_t num_elements) {
|
||||
if (memcmp(p1 , p2 , num_elements * sizeof * p1) == 0)
|
||||
return 0;
|
||||
else {
|
||||
const double abs_epsilon = 1e-8;
|
||||
const double rel_epsilon = 1e-5;
|
||||
size_t index;
|
||||
for (index = 0; index < num_elements; index++) {
|
||||
double diff = fabs(p1[index] - p2[index]);
|
||||
if (diff > abs_epsilon) {
|
||||
double sum = fabs(p1[index]) + fabs(p2[index]);
|
||||
|
||||
if (diff > sum * rel_epsilon)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
/*
|
||||
Will compare all the elements in the two double pointers p1 and
|
||||
p2. If all elements are 'sufficiently' the function will return 0,
|
||||
otherwise it will return 1.
|
||||
*/
|
||||
|
||||
#ifndef OPM_MEMCMP_DOUBLE_H
|
||||
#define OPM_MEMCMP_DOUBLE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
int opm_memcmp_double(const double * p1 , const double *p2 , size_t num_elements);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -19,7 +19,6 @@
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <opm/core/grid.h>
|
||||
#include <opm/core/utility/opm_memcmp_double.h>
|
||||
#include <opm/core/grid/cornerpoint_grid.h> /* compute_geometry */
|
||||
#include <opm/core/grid/GridManager.hpp> /* compute_geometry */
|
||||
#include <opm/core/grid/cpgpreprocess/preprocess.h>
|
||||
@ -169,64 +168,3 @@ BOOST_AUTO_TEST_CASE(TOPS_Fully_Specified) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(compare_double) {
|
||||
const double abs_epsilon = 1e-8;
|
||||
const double rel_epsilon = 1e-5;
|
||||
|
||||
double v1,v2;
|
||||
/* Should be equal: */
|
||||
{
|
||||
v1 = 0.0;
|
||||
v2 = 0.0;
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 0 );
|
||||
|
||||
v1 = 1e-12;
|
||||
v2 = v1 + 0.5*abs_epsilon;
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 0 );
|
||||
|
||||
v1 = 7.0;
|
||||
v2 = 7.0;
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 0 );
|
||||
|
||||
v1 = -7.0;
|
||||
v2 = -7.0;
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 0 );
|
||||
|
||||
v1 = 0;
|
||||
v2 = 0.5 * abs_epsilon;
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 0 );
|
||||
|
||||
|
||||
v1 = 1e7;
|
||||
v2 = 1e7 + 2*abs_epsilon;
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 0 );
|
||||
|
||||
v1 = 1e7;
|
||||
v2 = 1e7*(1 + 2*rel_epsilon);
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 0 );
|
||||
}
|
||||
|
||||
/* Should be different: */
|
||||
{
|
||||
v1 = 0;
|
||||
v2 = 1.5 * abs_epsilon;
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 1 );
|
||||
|
||||
v1 = 1e-8;
|
||||
v2 = v1 + 1.5*abs_epsilon;
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 1 );
|
||||
|
||||
v1 = 1;
|
||||
v2 = v1*(1 + 2*rel_epsilon + abs_epsilon);
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 1 );
|
||||
|
||||
v1 = 10;
|
||||
v2 = v1*(1 + 2*rel_epsilon + abs_epsilon);
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 1 );
|
||||
|
||||
v1 = 1e7;
|
||||
v2 = 1e7*(1 + 2*rel_epsilon + abs_epsilon);
|
||||
BOOST_CHECK_EQUAL( opm_memcmp_double( &v1 , &v2 , 1 ) , 1 );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user