Skip to content

Commit

Permalink
Adding python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Apr 30, 2024
1 parent 00ff292 commit 532d2ac
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 16 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ if( NDItk.python )
python/src/multigroup/FluxWeights.python.cpp
python/src/multigroup/Structure.python.cpp
python/src/multigroup/ReactionCrossSections.python.cpp
python/src/multigroup/AverageFissionEnergyRelease.python.cpp
)

target_link_libraries( NDItk.python PRIVATE NDItk )
Expand Down
12 changes: 6 additions & 6 deletions cmake/unit_testing_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ endfunction()

message( STATUS "Adding NDItk Python unit testing" )

add_python_test( multigroup.Metadata multigroup/Test_NDItk_multigroup_Metadata.py )
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 )
add_python_test( multigroup.ReactionCrossSections multigroup/Test_NDItk_multigroup_ReactionCrossSections.py )
add_python_test( multigroup.Metadata multigroup/Test_NDItk_multigroup_Metadata.py )
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 )
add_python_test( multigroup.ReactionCrossSections multigroup/Test_NDItk_multigroup_ReactionCrossSections.py )
add_python_test( multigroup.AverageFissionEnergyRelease multigroup/Test_NDItk_multigroup_AverageFissionEnergyRelease.py )

add_python_test( MultigroupTable Test_NDItk_MultigroupTable.py )

2 changes: 2 additions & 0 deletions python/src/multigroup.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace multigroup {
void wrapFluxWeights( python::module&, python::module& );
void wrapStructure( python::module&, python::module& );
void wrapReactionCrossSections( python::module&, python::module& );
void wrapAverageFissionEnergyRelease( python::module&, python::module& );
}

void wrapMultigroup( python::module& module, python::module& viewmodule ) {
Expand All @@ -31,4 +32,5 @@ void wrapMultigroup( python::module& module, python::module& viewmodule ) {
multigroup::wrapFluxWeights( submodule, viewmodule );
multigroup::wrapStructure( submodule, viewmodule );
multigroup::wrapReactionCrossSections( submodule, viewmodule );
multigroup::wrapAverageFissionEnergyRelease( submodule, viewmodule );
}
105 changes: 105 additions & 0 deletions python/src/multigroup/AverageFissionEnergyRelease.python.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// system includes
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

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

// namespace aliases
namespace python = pybind11;

namespace multigroup {

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

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

// wrap views created by this record

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

module,
"AverageFissionEnergyRelease",
"An average fission energy release record for multigroup neutron and photon data"
);

// wrap the record
record
.def(

python::init< double, double, double,
double, double, double >(),
python::arg( "total" ), python::arg( "prompt" ), python::arg( "neutrons" ),
python::arg( "gammas" ), python::arg( "betas" ), python::arg( "fragments" ),
"Initialise the record\n\n"
"Arguments:\n"
" self the table\n"
" total the total energy release (including delayed particles\n"
" and neutrinos)\n"
" prompt the recoverable energy release (total minus delayed\n"
" particles and neutrinos)\n"
" neutrons the energy release through prompt neutrons\n"
" gammas the energy release through prompt gammas\n"
" betas the energy release through delayed betas\n"
" fragments the kinetic energy of fission fragments"
)
.def_property_readonly(

"prompt_energy_release",
&Record::promptEnergyRelease,
"The prompt energy release (total energy release minus delayed particles\n"
"and neutrinos)"
)
.def_property_readonly(

"total_energy_release",
&Record::totalEnergyRelease,
"The total energy release (including delayed particles and neutrinos)"
)
.def_property_readonly(

"delayed_betas",
&Record::delayedBetas,
"The energy release through delayed betas"
)
.def_property_readonly(

"prompt_gammas",
&Record::promptGammas,
"The energy release through prompt gammas"
)
.def_property_readonly(

"prompt_neutrons",
&Record::promptNeutrons,
"The kinetic energy of the prompt fission neutrons"
)
.def_property_readonly(

"fission_fragments",
&Record::fissionFragments,
"The kinetic energy of the fission products"
)
.def_static(

"from_string",
[] ( const std::string& string ) -> Record
{ return read< Record >( string ); },
python::arg( "string" ),
"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"
);

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

} // multigroup namespace
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# standard imports
import unittest

# third party imports

# local imports
from NDItk.multigroup import AverageFissionEnergyRelease

class Test_NDItk_multigroup_AverageFissionEnergyRelease( unittest.TestCase ) :
"""Unit test for the AverageFissionEnergyRelease class."""

chunk_values = [ 181.238898, 202.827, 6.5, 7.281253, 169.13, 4.827645 ]
chunk_string = ( 'fiss_q\n'
' 181.238898 202.827 6.5 7.281253 169.13\n'
' 4.827645\n' )

def test_component( self ) :

def verify_chunk( self, chunk ) :

# verify content
self.assertAlmostEqual( 181.238898, chunk.prompt_energy_release )
self.assertAlmostEqual( 202.827, chunk.total_energy_release )
self.assertAlmostEqual( 6.5, chunk.delayed_betas )
self.assertAlmostEqual( 7.281253, chunk.prompt_gammas )
self.assertAlmostEqual( 169.13, chunk.fission_fragments )
self.assertAlmostEqual( 4.827645, chunk.prompt_neutrons )

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

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

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

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

# the data is given explicitly
chunk = AverageFissionEnergyRelease( total = 202.827, prompt = 181.238898, neutrons = 4.827645,
gammas = 7.281253, betas = 6.5, fragments = 169.13 )

verify_chunk( self, chunk )

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

verify_chunk( self, chunk )

if __name__ == '__main__' :

unittest.main()
3 changes: 2 additions & 1 deletion src/NDItk/multigroup/AverageFissionEnergyRelease.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace NDItk {
namespace multigroup {

/**
* @brief An average fission energy release record
* @brief An average fission energy release record for multigroup
* neutron and photon data
*/
class AverageFissionEnergyRelease : protected base::RealListRecord {

Expand Down
14 changes: 8 additions & 6 deletions src/NDItk/multigroup/AverageFissionEnergyRelease/src/ctor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ AverageFissionEnergyRelease() : RealListRecord( "fiss_q" ) {}
/**
* @brief Constructor for the primary group structure
*
* @param[in] total the total energy release (including neutrinos)
* @param[in] recoverable the recoverable energy release (total minus neutrinos)
* @param[in] neutrons the energy release through prompt neutrons
* @param[in] gammas the energy release through prompt gammas
* @param[in] betas the energy release through delayed betas
* @param[in] fragments the kinetic energy of fission fragments
* @param[in] total the total energy release (including delayed particles
* and neutrinos)
* @param[in] prompt the recoverable energy release (total minus delayed
* particles and neutrinos)
* @param[in] neutrons the energy release through prompt neutrons
* @param[in] gammas the energy release through prompt gammas
* @param[in] betas the energy release through delayed betas
* @param[in] fragments the kinetic energy of fission fragments
*/
AverageFissionEnergyRelease( double total, double recoverable, double neutrons,
double gammas, double betas, double fragments ) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ SCENARIO( "AverageFissionEnergyRelease" ) {
WHEN( "the data is given explicitly" ) {

double total = 202.827;
double recoverable = 181.238898;
double prompt = 181.238898;
double neutrons = 4.827645;
double gammas = 7.281253;
double betas = 6.5;
double fragments = 169.13;
AverageFissionEnergyRelease chunk( total, recoverable, neutrons,
AverageFissionEnergyRelease chunk( total, prompt, neutrons,
gammas, betas, fragments );

THEN( "a AverageFissionEnergyRelease can be constructed and members can "
Expand Down
2 changes: 1 addition & 1 deletion src/NDItk/multigroup/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace NDItk {
namespace multigroup {

/**
* @brief A group structure record
* @brief A group structure record for multigroup neutron and photon data
*/
class Structure : protected base::RealListRecord {

Expand Down

0 comments on commit 532d2ac

Please sign in to comment.