Skip to content

Commit

Permalink
Adding from_string
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Apr 26, 2024
1 parent e886eec commit b946a1a
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 11 deletions.
1 change: 1 addition & 0 deletions cmake/unit_testing_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ message( STATUS "Adding NDItk Python unit testing" )

add_python_test( multigroup.CrossSection multigroup/Test_NDItk_multigroup_CrossSection.py )
add_python_test( multigroup.FluxWeights multigroup/Test_NDItk_multigroup_FluxWeights.py )
add_python_test( multigroup.Structure multigroup/Test_NDItk_multigroup_Structure.py )
14 changes: 14 additions & 0 deletions python/src/multigroup/FluxWeights.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "NDItk/multigroup/FluxWeights.hpp"
#include "tools/views/views-python.hpp"
#include "definitions.hpp"
#include "read.hpp"

// namespace aliases
namespace python = pybind11;
Expand Down Expand Up @@ -50,6 +51,19 @@ void wrapFluxWeights( python::module& module, python::module& ) {
"number_groups",
&Record::numberGroups,
"The number of groups defined by this record"
)
.def_static(

"from_string",
[] ( const std::string& string, std::size_t number ) -> Record
{ return read< Record >( string, number ); },
python::arg( "string" ), python::arg( "number" ),
"Read the record from a string\n\n"
"An exception is raised if something goes wrong while reading the\n"
"component\n\n"
"Arguments:\n"
" string the string representing the component\n"
" number the number of weight values to be read"
);

// add standard record definitions
Expand Down
14 changes: 14 additions & 0 deletions python/src/multigroup/Structure.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "NDItk/multigroup/Structure.hpp"
#include "tools/views/views-python.hpp"
#include "definitions.hpp"
#include "read.hpp"

// namespace aliases
namespace python = pybind11;
Expand Down Expand Up @@ -50,6 +51,19 @@ void wrapStructure( python::module& module, python::module& ) {
"number_groups",
&Record::numberGroups,
"The number of groups defined by this record"
)
.def_static(

"from_string",
[] ( const std::string& string, std::size_t number ) -> Record
{ return read< Record >( string, number ); },
python::arg( "string" ), python::arg( "number" ),
"Read the record from a string\n\n"
"An exception is raised if something goes wrong while reading the\n"
"component\n\n"
"Arguments:\n"
" string the string representing the component\n"
" number the number of boundary values to be read"
);

// add standard record definitions
Expand Down
43 changes: 43 additions & 0 deletions python/src/read.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef NJOY_NDITK_PYTHON_READ
#define NJOY_NDITK_PYTHON_READ

// system includes
#include <string>

// other includes
#include "tools/Log.hpp"
#include "tools/disco/FreeFormatCharacter.hpp"

/**
* @brief Read a record from a string
*
* @param[in] string the string to read from
*/
template < typename Record, typename... Arguments >
Record read( const std::string& string, Arguments... arguments ) {

using namespace njoy::tools;

Record record;
auto iter = string.begin();
auto end = string.end();

std::string key = disco::FreeFormatCharacter::read< std::string >( iter, end );
if ( record.keyword() == key ) {

//! @todo take into account _0 style keys
record.read( iter, end, arguments... );

//! @todo verify the string is now empty
return record;
}
else {

Log::error( "The record keyword is not the one expected" );
Log::info( "Expected: \'{}\'", record.keyword() );
Log::info( "Found: \'{}\'", key );
throw std::exception();
}
}

#endif
5 changes: 5 additions & 0 deletions python/test/multigroup/Test_NDItk_multigroup_FluxWeights.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def verify_chunk( self, chunk ) :

verify_chunk( self, chunk )

# the data is read from a string
chunk = FluxWeights.from_string( self.chunk_string, 7 )

verify_chunk( self, chunk )

if __name__ == '__main__' :

unittest.main()
9 changes: 7 additions & 2 deletions python/test/multigroup/Test_NDItk_multigroup_Structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Test_NDItk_multigroup_Structure( unittest.TestCase ) :
"""Unit test for the Structure class."""

chunk_values = [ 0.1, 0.2, 0.25, 0.05, 0.15, 0.04, 0.06 ]
chunk_values = [ 20, 18, 16, 14, 10, 5, 1, 1e-11 ]
chunk_string = ( 'e_bounds\n'
' 20 18 16 14 10\n'
' 5 1 1e-11\n' )
Expand Down Expand Up @@ -41,7 +41,12 @@ def verify_chunk( self, chunk ) :
self.assertAlmostEqual( self.chunk_values[index], values[index] )

# the data is given explicitly
chunk = Structure( weights = [ 20., 18., 16., 14., 10., 5, 1, 1e-11 ] )
chunk = Structure( boundaries = [ 20., 18., 16., 14., 10., 5, 1, 1e-11 ] )

verify_chunk( self, chunk )

# the data is read from a string
chunk = Structure.from_string( self.chunk_string, 8 )

verify_chunk( self, chunk )

Expand Down
4 changes: 3 additions & 1 deletion src/NDItk/base/IntegerListRecord/src/read.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* @brief Read the record data
*
* @param[in] iter the current position in the input
* @param[in] iter the current position in the input
* @param[in] end the end position in the input
* @param[in] size the number of values to be read
*/
template< typename Iterator >
void read( Iterator& iter, const Iterator& end, std::size_t size ) {
Expand Down
4 changes: 3 additions & 1 deletion src/NDItk/base/RealListRecord/src/read.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* @brief Read the record data
*
* @param[in] iter the current position in the input
* @param[in] iter the current position in the input
* @param[in] end the end position in the input
* @param[in] size the number of values to be read
*/
template< typename Iterator >
void read( Iterator& iter, const Iterator& end, std::size_t size ) {
Expand Down
6 changes: 3 additions & 3 deletions src/NDItk/multigroup/FluxWeights/src/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*
* @param[in,out] iter an iterator to the current position in the input
* @param[in,out] end an iterator to the end of the input
* @param[in,out] groups the number of weight values to be read
* @param[in,out] number the number of weight values
*/
template< typename Iterator >
void read( Iterator& iter, const Iterator& end, std::size_t groups ) {
void read( Iterator& iter, const Iterator& end, std::size_t number ) {

base::RealListRecord::read( iter, end, groups );
base::RealListRecord::read( iter, end, number );
verify( this->weights() );
};
5 changes: 4 additions & 1 deletion src/NDItk/multigroup/ReactionCrossSections/src/read.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* @brief Read the record data
*
* @param[in] iter the current position in the input
* @param[in,out] iter an iterator to the current position in the input
* @param[in,out] end an iterator to the end of the input
* @param[in,out] reactions the number of reactions
* @param[in,out] groups the number of groups
*/
template< typename Iterator >
void read( Iterator& iter, const Iterator& end, std::size_t reactions, std::size_t groups ) {
Expand Down
6 changes: 3 additions & 3 deletions src/NDItk/multigroup/Structure/src/read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*
* @param[in,out] iter an iterator to the current position in the input
* @param[in,out] end an iterator to the end of the input
* @param[in,out] groups the number of groups to be read
* @param[in,out] number the number of boundary values
*/
template< typename Iterator >
void read( Iterator& iter, const Iterator& end, std::size_t groups ) {
void read( Iterator& iter, const Iterator& end, std::size_t number ) {

base::RealListRecord::read( iter, end, groups );
base::RealListRecord::read( iter, end, number );
verify( this->boundaries() );
};

0 comments on commit b946a1a

Please sign in to comment.