Skip to content

Commit

Permalink
Adding bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed May 16, 2024
1 parent 84eef21 commit fb78180
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ if( NDItk.python )
python/src/multigroup/TotalCrossSection.python.cpp
python/src/multigroup/AverageFissionEnergyRelease.python.cpp
python/src/multigroup/Velocities.python.cpp
python/src/multigroup/HeatingNumbers.python.cpp
python/src/multigroup/Kerma.python.cpp
)

target_link_libraries( NDItk.python PRIVATE NDItk )
Expand Down
2 changes: 2 additions & 0 deletions cmake/unit_testing_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ add_python_test( multigroup.ReactionCrossSections multigroup/Test_NDItk_mu
add_python_test( multigroup.TotalCrossSection multigroup/Test_NDItk_multigroup_TotalCrossSection.py )
add_python_test( multigroup.AverageFissionEnergyRelease multigroup/Test_NDItk_multigroup_AverageFissionEnergyRelease.py )
add_python_test( multigroup.Velocities multigroup/Test_NDItk_multigroup_Velocities.py )
add_python_test( multigroup.HeatingNumbers multigroup/Test_NDItk_multigroup_HeatingNumbers.py )
add_python_test( multigroup.Kerma multigroup/Test_NDItk_multigroup_Kerma.py )

add_python_test( MultigroupTable Test_NDItk_MultigroupTable.py )
4 changes: 4 additions & 0 deletions python/src/multigroup.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace multigroup {
void wrapReactionCrossSections( python::module&, python::module& );
void wrapTotalCrossSection( python::module&, python::module& );
void wrapVelocities( python::module&, python::module& );
void wrapHeatingNumbers( python::module&, python::module& );
void wrapKerma( python::module&, python::module& );
void wrapAverageFissionEnergyRelease( python::module&, python::module& );
}

Expand All @@ -36,5 +38,7 @@ void wrapMultigroup( python::module& module, python::module& viewmodule ) {
multigroup::wrapReactionCrossSections( submodule, viewmodule );
multigroup::wrapTotalCrossSection( submodule, viewmodule );
multigroup::wrapVelocities( submodule, viewmodule );
multigroup::wrapHeatingNumbers( submodule, viewmodule );
multigroup::wrapKerma( submodule, viewmodule );
multigroup::wrapAverageFissionEnergyRelease( submodule, viewmodule );
}
76 changes: 76 additions & 0 deletions python/src/multigroup/HeatingNumbers.python.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// system includes
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

// local includes
#include "NDItk/multigroup/HeatingNumbers.hpp"
#include "tools/views/views-python.hpp"
#include "definitions.hpp"
#include "read.hpp"

// namespace aliases
namespace python = pybind11;

namespace multigroup {

void wrapHeatingNumbers( python::module& module, python::module& ) {

// type aliases
using Record = njoy::NDItk::multigroup::HeatingNumbers;

// wrap views created by this record

// create the record
python::class_< Record > record(

module,
"HeatingNumbers",
"A heating numbers record for multigroup neutron and photon data"
);

// wrap the record
record
.def(

python::init< std::vector< double > >(),
python::arg( "values" ),
"Initialise the record\n\n"
"Arguments:\n"
" self the record\n"
" values the heating numbers"
)
.def(

python::init< unsigned int, std::vector< double > >(),
python::arg( "particle" ), python::arg( "values" ),
"Initialise the record\n\n"
"Arguments:\n"
" self the table\n"
" particle the secondary particle identifier\n"
" values the heating numbers"
)
.def_property_readonly(

"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 readWithSubtype< 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"
"record\n\n"
"Arguments:\n"
" string the string representing the record\n"
" number the number of heating numbers to be read"
);

// add standard record definitions
addStandardRecordDefinitions< Record, DoubleRange >( record );
}

} // multigroup namespace
76 changes: 76 additions & 0 deletions python/src/multigroup/Kerma.python.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// system includes
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

// local includes
#include "NDItk/multigroup/Kerma.hpp"
#include "tools/views/views-python.hpp"
#include "definitions.hpp"
#include "read.hpp"

// namespace aliases
namespace python = pybind11;

namespace multigroup {

void wrapKerma( python::module& module, python::module& ) {

// type aliases
using Record = njoy::NDItk::multigroup::Kerma;

// wrap views created by this record

// create the record
python::class_< Record > record(

module,
"Kerma",
"A kerma record for multigroup neutron and photon data"
);

// wrap the record
record
.def(

python::init< std::vector< double > >(),
python::arg( "values" ),
"Initialise the record\n\n"
"Arguments:\n"
" self the record\n"
" values the kerma values"
)
.def(

python::init< unsigned int, std::vector< double > >(),
python::arg( "particle" ), python::arg( "values" ),
"Initialise the record\n\n"
"Arguments:\n"
" self the table\n"
" particle the secondary particle identifier\n"
" values the kerma values"
)
.def_property_readonly(

"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 readWithSubtype< 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"
"record\n\n"
"Arguments:\n"
" string the string representing the record\n"
" number the number of kerma values to be read"
);

// add standard record definitions
addStandardRecordDefinitions< Record, DoubleRange >( record );
}

} // multigroup namespace
94 changes: 94 additions & 0 deletions python/test/multigroup/Test_NDItk_multigroup_HeatingNumbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# standard imports
import unittest

# third party imports

# local imports
from NDItk.multigroup import HeatingNumbers

class Test_NDItk_multigroup_HeatingNumbers( unittest.TestCase ) :
"""Unit test for the HeatingNumbers class."""

chunk_values = [ 10, 9, 8, 7, 6, 5, 4 ]
chunk_string = ( 'heating\n'
' 10 9 8 7 6\n'
' 5 4\n' )

chunk_outgoing_values = [ 10, 9, 8, 7, 6, 5, 4 ]
chunk_outgoing_string = ( 'heating_1001\n'
' 10 9 8 7 6\n'
' 5 4\n' )

def test_component( self ) :

def verify_chunk( self, chunk ) :

# verify content
self.assertEqual( 7, chunk.number_groups )
self.assertAlmostEqual( 10, chunk.values[0] )
self.assertAlmostEqual( 9, chunk.values[1] )
self.assertAlmostEqual( 8, chunk.values[2] )
self.assertAlmostEqual( 7, chunk.values[3] )
self.assertAlmostEqual( 6, chunk.values[4] )
self.assertAlmostEqual( 5, chunk.values[5] )
self.assertAlmostEqual( 4, chunk.values[6] )

self.assertEqual( self.chunk_string, chunk.to_string() )

# verify the record
self.assertEqual( 'heating', chunk.keyword )
self.assertEqual( False, chunk.empty )
self.assertEqual( 7, chunk.size )

values = chunk.values
for index in range( chunk.size ) :

self.assertAlmostEqual( self.chunk_values[index], values[index] )

def verify_outgoing_chunk( self, chunk ) :

# verify content
self.assertEqual( 7, chunk.number_groups )
self.assertAlmostEqual( 10, chunk.values[0] )
self.assertAlmostEqual( 9, chunk.values[1] )
self.assertAlmostEqual( 8, chunk.values[2] )
self.assertAlmostEqual( 7, chunk.values[3] )
self.assertAlmostEqual( 6, chunk.values[4] )
self.assertAlmostEqual( 5, chunk.values[5] )
self.assertAlmostEqual( 4, chunk.values[6] )

self.assertEqual( self.chunk_outgoing_string, chunk.to_string() )

# verify the record
self.assertEqual( 'heating_1001', chunk.keyword )
self.assertEqual( False, chunk.empty )
self.assertEqual( 7, chunk.size )

values = chunk.values
for index in range( chunk.size ) :

self.assertAlmostEqual( self.chunk_outgoing_values[index], values[index] )

# the data is given explicitly
chunk = HeatingNumbers( values = [ 10, 9, 8, 7, 6, 5, 4 ] )

verify_chunk( self, chunk )

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

verify_chunk( self, chunk )

# the data is given explicitly for an outgoing particle
chunk = HeatingNumbers( particle = 1001, values = [ 10, 9, 8, 7, 6, 5, 4 ] )

verify_outgoing_chunk( self, chunk )

# the data is read from a string for an outgoing particle
chunk = HeatingNumbers.from_string( self.chunk_outgoing_string, 7 )

verify_outgoing_chunk( self, chunk )

if __name__ == '__main__' :

unittest.main()
94 changes: 94 additions & 0 deletions python/test/multigroup/Test_NDItk_multigroup_Kerma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# standard imports
import unittest

# third party imports

# local imports
from NDItk.multigroup import Kerma

class Test_NDItk_multigroup_Kerma( unittest.TestCase ) :
"""Unit test for the Kerma class."""

chunk_values = [ 10, 9, 8, 7, 6, 5, 4 ]
chunk_string = ( 'kerma\n'
' 10 9 8 7 6\n'
' 5 4\n' )

chunk_outgoing_values = [ 10, 9, 8, 7, 6, 5, 4 ]
chunk_outgoing_string = ( 'kerma_1001\n'
' 10 9 8 7 6\n'
' 5 4\n' )

def test_component( self ) :

def verify_chunk( self, chunk ) :

# verify content
self.assertEqual( 7, chunk.number_groups )
self.assertAlmostEqual( 10, chunk.values[0] )
self.assertAlmostEqual( 9, chunk.values[1] )
self.assertAlmostEqual( 8, chunk.values[2] )
self.assertAlmostEqual( 7, chunk.values[3] )
self.assertAlmostEqual( 6, chunk.values[4] )
self.assertAlmostEqual( 5, chunk.values[5] )
self.assertAlmostEqual( 4, chunk.values[6] )

self.assertEqual( self.chunk_string, chunk.to_string() )

# verify the record
self.assertEqual( 'kerma', chunk.keyword )
self.assertEqual( False, chunk.empty )
self.assertEqual( 7, chunk.size )

values = chunk.values
for index in range( chunk.size ) :

self.assertAlmostEqual( self.chunk_values[index], values[index] )

def verify_outgoing_chunk( self, chunk ) :

# verify content
self.assertEqual( 7, chunk.number_groups )
self.assertAlmostEqual( 10, chunk.values[0] )
self.assertAlmostEqual( 9, chunk.values[1] )
self.assertAlmostEqual( 8, chunk.values[2] )
self.assertAlmostEqual( 7, chunk.values[3] )
self.assertAlmostEqual( 6, chunk.values[4] )
self.assertAlmostEqual( 5, chunk.values[5] )
self.assertAlmostEqual( 4, chunk.values[6] )

self.assertEqual( self.chunk_outgoing_string, chunk.to_string() )

# verify the record
self.assertEqual( 'kerma_1001', chunk.keyword )
self.assertEqual( False, chunk.empty )
self.assertEqual( 7, chunk.size )

values = chunk.values
for index in range( chunk.size ) :

self.assertAlmostEqual( self.chunk_outgoing_values[index], values[index] )

# the data is given explicitly
chunk = Kerma( values = [ 10, 9, 8, 7, 6, 5, 4 ] )

verify_chunk( self, chunk )

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

verify_chunk( self, chunk )

# the data is given explicitly for an outgoing particle
chunk = Kerma( particle = 1001, values = [ 10, 9, 8, 7, 6, 5, 4 ] )

verify_outgoing_chunk( self, chunk )

# the data is read from a string for an outgoing particle
chunk = Kerma.from_string( self.chunk_outgoing_string, 7 )

verify_outgoing_chunk( self, chunk )

if __name__ == '__main__' :

unittest.main()

0 comments on commit fb78180

Please sign in to comment.