-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#ifndef NJOY_NDITK_MULTIGROUP | ||
#define NJOY_NDITK_MULTIGROUP | ||
|
||
// system includes | ||
|
||
// other includes | ||
#include "NDItk/multigroup/Metadata.hpp" | ||
#include "NDItk/multigroup/Structure.hpp" | ||
|
||
namespace njoy { | ||
namespace NDItk { | ||
|
||
/** | ||
* @brief A multigroup NDI table | ||
*/ | ||
class MultigroupTable { | ||
|
||
/* fields */ | ||
multigroup::Metadata metadata_; | ||
multigroup::Structure structure_; | ||
|
||
template < typename Record, typename Iterator, typename... Arguments > | ||
static void readRecord( Record& record, Iterator& iter, const Iterator& end, | ||
Arguments... arguments ) { | ||
|
||
if ( record.hasContent() ) { | ||
|
||
throw std::runtime_error( "Duplicate keyword" ); | ||
} | ||
record.read( iter, end, arguments... ); | ||
} | ||
|
||
public: | ||
|
||
MultigroupTable() : metadata_(), structure_() {} | ||
|
||
const multigroup::Metadata& metadata() const { return this->metadata_; } | ||
const multigroup::Structure& structure() const { return this->structure_; } | ||
|
||
/** | ||
* @brief Print the NDI table | ||
* | ||
* @param[in] iter the current position in the output | ||
*/ | ||
template< typename OutputIterator > | ||
void print( OutputIterator& iter ) const { | ||
|
||
this->metadata_.print( iter ); | ||
this->structure_.print( iter ); | ||
*iter++ = 'e'; | ||
*iter++ = 'n'; | ||
*iter++ = 'd'; | ||
*iter++ = '\n'; | ||
}; | ||
|
||
/** | ||
* @brief Read the table content | ||
* | ||
* @param[in] iter the current position in the input | ||
*/ | ||
template< typename Iterator > | ||
void read( Iterator& iter, const Iterator& end ) { | ||
|
||
std::string keyword; | ||
while ( ( keyword != "end" ) && ( iter != end ) ) { | ||
|
||
keyword = njoy::tools::disco::FreeFormatCharacter::read< std::string >( iter, end ); | ||
|
||
if ( ( keyword == "zaid" ) || ( keyword == "library_name" ) || ( keyword == "date_source" ) || | ||
( keyword == "date_processed" ) || ( keyword == "awr" ) || ( keyword == "at_wgt" ) || | ||
( keyword == "temp" ) || ( keyword == "sig_0" ) || ( keyword == "num_grps" ) || | ||
( keyword == "up_grps" ) || ( keyword == "down_grps" ) || ( keyword == "pn_order" ) ) { | ||
|
||
this->metadata_.read( keyword, iter, end ); | ||
} | ||
else if ( keyword == "e_bounds" ) { | ||
|
||
if ( this->metadata_.numberGroups().has_value() ) { | ||
|
||
readRecord( this->structure_, iter, end, this->metadata_.numberGroups().value() + 1 ); | ||
} | ||
else { | ||
|
||
throw std::runtime_error( "Required metadata is missing" ); | ||
} | ||
} | ||
else { | ||
|
||
if ( keyword != "end" ) { | ||
|
||
throw std::runtime_error( std::string( "Unknown keyword: " ) + keyword ); | ||
} | ||
} | ||
|
||
while ( std::isspace( *iter ) && iter != end ) { | ||
|
||
++iter; | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
} // NDItk namespace | ||
} // njoy namespace | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_cpp_test( MultigroupTable MultigroupTable.test.cpp ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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 "NDItk/MultigroupTable.hpp" | ||
|
||
// other includes | ||
|
||
// convenience typedefs | ||
using namespace njoy::NDItk; | ||
|
||
std::string chunk(); | ||
void verifyChunk( const MultigroupTable& ); | ||
|
||
SCENARIO( "MultigroupTable" ) { | ||
|
||
GIVEN( "valid data for a MultigroupTable instance" ) { | ||
|
||
std::string record = chunk(); | ||
|
||
WHEN( "the data is read using iterators" ) { | ||
|
||
auto iter = record.begin(); | ||
auto end = record.end(); | ||
|
||
MultigroupTable chunk; | ||
chunk.read( iter, end ); | ||
|
||
THEN( "a MultigroupTable can be constructed and members can " | ||
"be tested" ) { | ||
|
||
verifyChunk( chunk ); | ||
} // THEN | ||
|
||
THEN( "the record can be printed" ) { | ||
|
||
std::string buffer; | ||
auto output = std::back_inserter( buffer ); | ||
chunk.print( output ); | ||
|
||
CHECK( buffer == record ); | ||
} // THEN | ||
} // WHEN | ||
} // GIVEN | ||
} // SCENARIO | ||
|
||
std::string chunk() { | ||
|
||
return "zaid\n" | ||
" 92235.711nm\n" | ||
"library_name\n" | ||
" mendf71x\n" | ||
"date_source\n" | ||
" 12/22/2011\n" | ||
"date_processed\n" | ||
" 08/07/2013\n" | ||
"awr\n" | ||
" 233.0248\n" | ||
"at_wgt\n" | ||
" 235.043937521619\n" | ||
"temp\n" | ||
" 2.53e-08\n" | ||
"sig_0\n" | ||
" 10000000000\n" | ||
"num_grps\n" | ||
" 7\n" | ||
"up_grps\n" | ||
" 0\n" | ||
"down_grps\n" | ||
" 6\n" | ||
"pn_order\n" | ||
" 5\n" | ||
"e_bounds\n" | ||
" 20 18 16 14\n" | ||
" 10 5 1 1e-11\n" | ||
"end\n"; | ||
} | ||
|
||
void verifyChunk( const MultigroupTable& chunk ) { | ||
|
||
CHECK( "92235.711nm" == chunk.metadata().zaid().value() ); | ||
CHECK( "mendf71x" == chunk.metadata().libraryName().value() ); | ||
CHECK( "12/22/2011" == chunk.metadata().sourceData().value() ); | ||
CHECK( "08/07/2013" == chunk.metadata().processDate().value() ); | ||
CHECK_THAT( 233.0248, WithinRel( chunk.metadata().atomicWeightRatio().value() ) ); | ||
CHECK_THAT( 235.043937521619, WithinRel( chunk.metadata().atomicWeight().value() ) ); | ||
CHECK_THAT( 2.53e-8, WithinRel( chunk.metadata().temperature().value() ) ); | ||
CHECK_THAT( 1e+10, WithinRel( chunk.metadata().dilution().value() ) ); | ||
CHECK( 7 == chunk.metadata().numberGroups().value() ); | ||
CHECK( 6 == chunk.metadata().numberDownscatterGroups().value() ); | ||
CHECK( 0 == chunk.metadata().numberUpscatterGroups().value() ); | ||
CHECK( 5 == chunk.metadata().legendreOrder().value() ); | ||
} |