Skip to content

Commit

Permalink
Adding the first moment of a histogram panel
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Oct 2, 2024
1 parent f7f29d6 commit 7e71f1d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmake/unit_testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ add_subdirectory( src/scion/integration/LinearLinear/test )
add_subdirectory( src/scion/integration/LinearLogarithmic/test )
add_subdirectory( src/scion/integration/LogarithmicLinear/test )
add_subdirectory( src/scion/integration/LogarithmicLogarithmic/test )
add_subdirectory( src/scion/integration/FirstMomentHistogram/test )
add_subdirectory( src/scion/integration/FirstMomentLinearLinear/test )
add_subdirectory( src/scion/integration/GaussLegendre/2/test )
add_subdirectory( src/scion/integration/GaussLegendre/4/test )
Expand Down
54 changes: 54 additions & 0 deletions src/scion/integration/FirstMomentHistogram.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef NJOY_SCION_INTEGRATION_FIRSTMOMENTHISTOGRAM
#define NJOY_SCION_INTEGRATION_FIRSTMOMENTHISTOGRAM

// system includes

// other includes
#include "scion/integration/IntegratorBase.hpp"

namespace njoy {
namespace scion {
namespace integration {

/**
* @class
* @brief First raw moment of a histogram panel (y is constant in x)
*
* The first raw moment or mean is defined as the integral of x * f(x)
*/
class FirstMomentHistogram : public IntegratorBase< FirstMomentHistogram > {

/* friend declarations */
friend class IntegratorBase< FirstMomentHistogram >;

/* interface implementation functions */

/**
* @brief Perform first raw moment integration of a histogram panel (y is constant in x)
*
* @param[in] xLeft the left value on the x interval
* @param[in] xRight the right value on the x interval
* @param[in] yLeft the left value on the y interval
* @param[in] yRight the right value on the y interval
*/
template < typename X, typename Y,
typename I = decltype( std::declval< X >() * std::declval< X >() * std::declval< Y >() ) >
I integrate( const X& xLeft, const X& xRight,
const Y& yLeft, const Y& ) const noexcept {

return 0.5 * yLeft * ( xRight - xLeft ) * ( xRight + xLeft );
}

public:

using IntegratorBase::operator();
};

// integration function
static constexpr FirstMomentHistogram firstMomentHistogram;

} // integration namespace
} // scion namespace
} // njoy namespace

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_cpp_test( integration.FirstMomentHistogram FirstMomentHistogram.test.cpp )
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// include Catch2
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
using Catch::Matchers::WithinRel;

// what we are testing
#include "scion/integration/FirstMomentHistogram.hpp"

// other includes

// convenience typedefs
using namespace njoy::scion;

SCENARIO( "FirstMomentHistogram" ) {

GIVEN( "FirstMomentHistogram integration object" ) {

WHEN( "integrating an interval" ) {

integration::FirstMomentHistogram integrator{};

THEN( "the integration is performed correctly" ) {

double xLeft = 1.0;
double xRight = 2.0;
double yLeft = 1.0;
double yRight = 4.0;

// both y values are the same
CHECK_THAT( 1.5, WithinRel( integrator( xLeft, xRight, yLeft, yLeft ) ) );

// the y values are different
CHECK_THAT( 1.5, WithinRel( integrator( xLeft, xRight, yLeft, yRight ) ) );
} // THEN
} // WHEN
} // GIVEN

GIVEN( "linlin integration function" ) {

WHEN( "integrating an interval" ) {

THEN( "the integration is performed correctly" ) {

double xLeft = 1.0;
double xRight = 2.0;
double yLeft = 1.0;
double yRight = 4.0;

// both y values are the same
CHECK_THAT( 1.5, WithinRel( integration::firstMomentHistogram( xLeft, xRight, yLeft, yLeft ) ) );

// the y values are different
CHECK_THAT( 1.5, WithinRel( integration::firstMomentHistogram( xLeft, xRight, yLeft, yRight ) ) );
} // THEN
} // WHEN
} // GIVEN
} // SCENARIO
2 changes: 1 addition & 1 deletion src/scion/integration/FirstMomentLinearLinear.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace integration {

/**
* @class
* @brief First raw moment of a Linear-linear panel (y is linear in x)
* @brief First raw moment of a linear-linear panel (y is linear in x)
*
* The first raw moment or mean is defined as the integral of x * f(x)
*/
Expand Down

0 comments on commit 7e71f1d

Please sign in to comment.