Skip to content

Commit

Permalink
Adding create functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Aug 10, 2024
1 parent 0b7dc97 commit a435c5e
Show file tree
Hide file tree
Showing 7 changed files with 3,117 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/dryad/format/endf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "dryad/format/endf/createTabulatedEnergyDistribution.hpp"
#include "dryad/format/endf/createTabulatedEnergyDistributions.hpp"
#include "dryad/format/endf/createTabulatedAverageEnergy.hpp"
#include "dryad/format/endf/createTabulatedFormFactor.hpp"
#include "dryad/format/endf/createTabulatedScatteringFunction.hpp"
#include "dryad/format/endf/createReactionProduct.hpp"
#include "dryad/format/endf/createReactionProducts.hpp"
#include "dryad/format/endf/createReaction.hpp"
Expand Down
76 changes: 76 additions & 0 deletions src/dryad/format/endf/createTabulatedFormFactor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef NJOY_DRYAD_FORMAT_ENDF_CREATETABULATEDFORMFACTOR
#define NJOY_DRYAD_FORMAT_ENDF_CREATETABULATEDFORMFACTOR

// system includes
#include <vector>

// other includes
#include "tools/Log.hpp"
#include "dryad/format/createVector.hpp"
#include "dryad/format/endf/createBoundaries.hpp"
#include "dryad/format/endf/createInterpolants.hpp"
#include "dryad/TabulatedFormFactor.hpp"
#include "ENDFtk/section/27.hpp"
#include "ENDFtk/tree/Section.hpp"

namespace njoy {
namespace dryad {
namespace format {
namespace endf {

/**
* @brief Create a TabulatedFormFactor from a parsed ENDF section
*/
TabulatedFormFactor
createTabulatedFormFactor( const ENDFtk::section::Type< 27 >& section ) {

if ( ( section.sectionNumber() != 505 ) && ( section.sectionNumber() != 506 ) ) {

Log::error( "Tabulated form factors are only defined in MF27 MT505 and MT506, "
"got MT{} instead", section.sectionNumber() );
throw std::exception();
}

try {

Log::info( "Reading form factor data" );
auto energies = createVector( section.X() );
auto values = createVector( section.H() );
auto boundaries = createBoundaries( section.boundaries() );
auto interpolants = createInterpolants( section.interpolants() );
return TabulatedFormFactor(
std::move( energies ), std::move( values ),
std::move( boundaries ), std::move( interpolants ) );
}
catch ( ... ) {

Log::info( "Error encountered while creating a tabulated scattering function" );
throw;
}
}

/**
* @brief Create a TabulatedFormFactor from an unparsed ENDF section
*/
TabulatedFormFactor
createTabulatedFormFactor( const ENDFtk::tree::Section& tree ) {

if ( tree.fileNumber() == 27 ) {

return createTabulatedFormFactor( tree.parse< 27 >() );
}
else {

Log::error( "The MAT{} MF{} MT{} section does not define a "
"tabulated form factor", tree.materialNumber(),
tree.fileNumber(), tree.sectionNumber() );
throw std::exception();
}
}

} // endf namespace
} // format namespace
} // dryad namespace
} // njoy namespace

#endif
76 changes: 76 additions & 0 deletions src/dryad/format/endf/createTabulatedScatteringFunction.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef NJOY_DRYAD_FORMAT_ENDF_CREATETABULATEDSCATTERINGFUNCTION
#define NJOY_DRYAD_FORMAT_ENDF_CREATETABULATEDSCATTERINGFUNCTION

// system includes
#include <vector>

// other includes
#include "tools/Log.hpp"
#include "dryad/format/createVector.hpp"
#include "dryad/format/endf/createBoundaries.hpp"
#include "dryad/format/endf/createInterpolants.hpp"
#include "dryad/TabulatedScatteringFunction.hpp"
#include "ENDFtk/section/27.hpp"
#include "ENDFtk/tree/Section.hpp"

namespace njoy {
namespace dryad {
namespace format {
namespace endf {

/**
* @brief Create a TabulatedScatteringFunction from a parsed ENDF section
*/
TabulatedScatteringFunction
createTabulatedScatteringFunction( const ENDFtk::section::Type< 27 >& section ) {

if ( ( section.sectionNumber() != 502 ) && ( section.sectionNumber() != 504 ) ) {

Log::error( "Tabulated scattering functions are only defined in MF27 MT502 and MT504, "
"got MT{} instead", section.sectionNumber() );
throw std::exception();
}

try {

Log::info( "Reading scattering function data" );
auto x = createVector( section.X() );
auto values = createVector( section.H() );
auto boundaries = createBoundaries( section.boundaries() );
auto interpolants = createInterpolants( section.interpolants() );
return TabulatedScatteringFunction(
std::move( x ), std::move( values ),
std::move( boundaries ), std::move( interpolants ) );
}
catch ( ... ) {

Log::info( "Error encountered while creating a tabulated scattering function" );
throw;
}
}

/**
* @brief Create a TabulatedScatteringFunction from an unparsed ENDF section
*/
TabulatedScatteringFunction
createTabulatedScatteringFunction( const ENDFtk::tree::Section& tree ) {

if ( tree.fileNumber() == 27 ) {

return createTabulatedScatteringFunction( tree.parse< 27 >() );
}
else {

Log::error( "The MAT{} MF{} MT{} section does not define a "
"tabulated scattering function", tree.materialNumber(),
tree.fileNumber(), tree.sectionNumber() );
throw std::exception();
}
}

} // endf namespace
} // format namespace
} // dryad namespace
} // njoy namespace

#endif
2 changes: 2 additions & 0 deletions src/dryad/format/endf/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ add_cpp_test( format.endf.createTabulatedAngularDistributions createTabulated
add_cpp_test( format.endf.createTabulatedEnergyDistribution createTabulatedEnergyDistribution.test.cpp )
add_cpp_test( format.endf.createTabulatedEnergyDistributions createTabulatedEnergyDistributions.test.cpp )
add_cpp_test( format.endf.createTabulatedAverageEnergy createTabulatedAverageEnergy.test.cpp )
add_cpp_test( format.endf.createTabulatedFormFactor createTabulatedFormFactor.test.cpp )
add_cpp_test( format.endf.createTabulatedScatteringFunction createTabulatedScatteringFunction.test.cpp )
add_cpp_test( format.endf.createReactionProduct createReactionProduct.test.cpp )
add_cpp_test( format.endf.createReactionProducts createReactionProducts.test.cpp )
add_cpp_test( format.endf.createReaction createReaction.test.cpp )
Expand Down
65 changes: 65 additions & 0 deletions src/dryad/format/endf/test/createTabulatedFormFactor.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 "dryad/format/endf/createTabulatedFormFactor.hpp"

// other includes
#include "ENDFtk/tree/fromFile.hpp"

// convenience typedefs
using namespace njoy::dryad;

void verifyChunk( const TabulatedFormFactor& );

SCENARIO( "createTabulatedFormFactor" ) {

GIVEN( "ENDF MF3 sections" ) {

auto tape = njoy::ENDFtk::tree::fromFile( "photoat-001_H_000.endf" );
auto section = tape.materials().front().section( 27, 505 );

WHEN( "a single unparsed MF27 section is given" ) {

THEN( "it can be converted" ) {

auto chunk = format::endf::createTabulatedFormFactor( section );

verifyChunk( chunk );
} // THEN
} // WHEN

WHEN( "a single parsed MF27 section is given" ) {

THEN( "it can be converted" ) {

auto chunk = format::endf::createTabulatedFormFactor( section.parse< 27 >() );

verifyChunk( chunk );
} // THEN
} // WHEN
} // GIVEN
} // SCENARIO

void verifyChunk( const TabulatedFormFactor& chunk ) {

CHECK( true == chunk.isLinearised() );
CHECK( 297 == chunk.numberPoints() );
CHECK( 1 == chunk.numberRegions() );
CHECK( 297 == chunk.energies().size() );
CHECK( 297 == chunk.values().size() );
CHECK( 1 == chunk.boundaries().size() );
CHECK( 1 == chunk.interpolants().size() );
CHECK( 296 == chunk.boundaries()[0] );
CHECK( InterpolationType::LinearLinear == chunk.interpolants()[0] );
CHECK_THAT( 1, WithinRel( chunk.energies()[0] ) );
CHECK_THAT( 2, WithinRel( chunk.energies()[1] ) );
CHECK_THAT( 9549925.86, WithinRel( chunk.energies()[295] ) );
CHECK_THAT( 10000000, WithinRel( chunk.energies()[296] ) );
CHECK_THAT( 0, WithinRel( chunk.values()[0] ) );
CHECK_THAT( 0, WithinRel( chunk.values()[1] ) );
CHECK_THAT( 8.9767e-15, WithinRel( chunk.values()[295] ) );
CHECK_THAT( 0, WithinRel( chunk.values()[296] ) );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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 "dryad/format/endf/createTabulatedScatteringFunction.hpp"

// other includes
#include "ENDFtk/tree/fromFile.hpp"

// convenience typedefs
using namespace njoy::dryad;

void verifyChunk( const TabulatedScatteringFunction& );

SCENARIO( "createTabulatedScatteringFunction" ) {

GIVEN( "ENDF MF3 sections" ) {

auto tape = njoy::ENDFtk::tree::fromFile( "photoat-001_H_000.endf" );
auto section = tape.materials().front().section( 27, 502 );

WHEN( "a single unparsed MF27 section is given" ) {

THEN( "it can be converted" ) {

auto chunk = format::endf::createTabulatedScatteringFunction( section );

verifyChunk( chunk );
} // THEN
} // WHEN

WHEN( "a single parsed MF27 section is given" ) {

THEN( "it can be converted" ) {

auto chunk = format::endf::createTabulatedScatteringFunction( section.parse< 27 >() );

verifyChunk( chunk );
} // THEN
} // WHEN
} // GIVEN
} // SCENARIO

void verifyChunk( const TabulatedScatteringFunction& chunk ) {

CHECK( true == chunk.isLinearised() );
CHECK( 1253 == chunk.numberPoints() );
CHECK( 1 == chunk.numberRegions() );
CHECK( 1253 == chunk.inverseLengths().size() );
CHECK( 1253 == chunk.values().size() );
CHECK( 1 == chunk.boundaries().size() );
CHECK( 1 == chunk.interpolants().size() );
CHECK( 1252 == chunk.boundaries()[0] );
CHECK( InterpolationType::LinearLinear == chunk.interpolants()[0] );
CHECK_THAT( 0, WithinRel( chunk.inverseLengths()[0] ) );
CHECK_THAT( 0.001, WithinRel( chunk.inverseLengths()[1] ) );
CHECK_THAT( 512000000, WithinRel( chunk.inverseLengths()[1251] ) );
CHECK_THAT( 1e+9, WithinRel( chunk.inverseLengths()[1252] ) );
CHECK_THAT( 1, WithinRel( chunk.values()[0] ) );
CHECK_THAT( 1, WithinRel( chunk.values()[1] ) );
CHECK_THAT( 1.1908e-37, WithinRel( chunk.values()[1251] ) );
CHECK_THAT( 8.1829e-39, WithinRel( chunk.values()[1252] ) );
}
Loading

0 comments on commit a435c5e

Please sign in to comment.