diff --git a/cmake/unit_testing.cmake b/cmake/unit_testing.cmake index 0ac626f..89be5bb 100644 --- a/cmake/unit_testing.cmake +++ b/cmake/unit_testing.cmake @@ -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 ) diff --git a/src/scion/integration/FirstMomentHistogram.hpp b/src/scion/integration/FirstMomentHistogram.hpp new file mode 100644 index 0000000..24dd98f --- /dev/null +++ b/src/scion/integration/FirstMomentHistogram.hpp @@ -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 diff --git a/src/scion/integration/FirstMomentHistogram/test/CMakeLists.txt b/src/scion/integration/FirstMomentHistogram/test/CMakeLists.txt new file mode 100644 index 0000000..ccbf6c8 --- /dev/null +++ b/src/scion/integration/FirstMomentHistogram/test/CMakeLists.txt @@ -0,0 +1 @@ +add_cpp_test( integration.FirstMomentHistogram FirstMomentHistogram.test.cpp ) diff --git a/src/scion/integration/FirstMomentHistogram/test/FirstMomentHistogram.test.cpp b/src/scion/integration/FirstMomentHistogram/test/FirstMomentHistogram.test.cpp new file mode 100644 index 0000000..c861aaa --- /dev/null +++ b/src/scion/integration/FirstMomentHistogram/test/FirstMomentHistogram.test.cpp @@ -0,0 +1,57 @@ +// include Catch2 +#include +#include +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 diff --git a/src/scion/integration/FirstMomentLinearLinear.hpp b/src/scion/integration/FirstMomentLinearLinear.hpp index 6573ee2..ef0b72c 100644 --- a/src/scion/integration/FirstMomentLinearLinear.hpp +++ b/src/scion/integration/FirstMomentLinearLinear.hpp @@ -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) */