Skip to content

Commit

Permalink
Merge branch 'feature/initial-python' into fix/review
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Apr 29, 2024
2 parents bedeffb + 350aa34 commit 3c218a5
Show file tree
Hide file tree
Showing 39 changed files with 1,427 additions and 32 deletions.
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ if( NDItk.python )

pybind11_add_module( NDItk.python
python/src/NDItk.python.cpp
python/src/MultigroupTable.python.cpp
python/src/multigroup.python.cpp
python/src/multigroup/Metadata.python.cpp
python/src/multigroup/CrossSection.python.cpp
python/src/multigroup/FluxWeights.python.cpp
python/src/multigroup/Structure.python.cpp
python/src/multigroup/ReactionCrossSections.python.cpp
)

target_link_libraries( NDItk.python PRIVATE NDItk )
Expand All @@ -104,6 +111,7 @@ if( NDItk.python )
message( STATUS "Building NDItk's python API" )

list( APPEND NDItk_PYTHONPATH ${CMAKE_CURRENT_BINARY_DIR} )
list( APPEND tools_PYTHONPATH ${CMAKE_CURRENT_BINARY_DIR}/_deps/tools-build )

if( NDItk.tests )
include( cmake/unit_testing_python.cmake )
Expand Down
11 changes: 10 additions & 1 deletion cmake/unit_testing_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function( add_python_test name source )
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/python )
set_tests_properties( ${test_name}
PROPERTIES ENVIRONMENT
PYTHONPATH=${NDItk_PYTHONPATH}:$ENV{PYTHONPATH})
PYTHONPATH=${tools_PYTHONPATH}:${NDItk_PYTHONPATH}:$ENV{PYTHONPATH})

endfunction()

Expand All @@ -21,3 +21,12 @@ 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( MultigroupTable Test_NDItk_MultigroupTable.py )

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

// local includes
#include "NDItk/MultigroupTable.hpp"
#include "definitions.hpp"
#include "read.hpp"

// namespace aliases
namespace python = pybind11;

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

// type aliases
using Table = njoy::NDItk::MultigroupTable;
using Metadata = njoy::NDItk::multigroup::Metadata;
using Structure = njoy::NDItk::multigroup::Structure;
using FluxWeights = njoy::NDItk::multigroup::FluxWeights;
using ReactionCrossSections = njoy::NDItk::multigroup::ReactionCrossSections;

// wrap views created by this table

// create the table
python::class_< Table > table(

module,
"MultigroupTable",
"A multigroup neutron and photon table"
);

// wrap the table
table
.def(

python::init< std::string, std::string, std::string, std::string,
double, double, double, double,
Structure, FluxWeights,
ReactionCrossSections >(),
python::arg( "zaid" ), python::arg( "libname" ), python::arg( "source" ),
python::arg( "process" ), python::arg( "awr" ), python::arg( "weight" ),
python::arg( "temperature" ), python::arg( "dilution" ),
python::arg( "structure" ), python::arg( "flux" ), python::arg( "xs" ),
"Initialise the table\n\n"
"Arguments:\n"
" self the table\n"
" zaid the zaid of the table\n"
" libname the library name\n"
" source the source date\n"
" process the processing date\n"
" awr the atomic weight ratio of the target (with respect\n"
" to the neutron mass)\n"
" weight the atomic weight of the target\n"
" temperature the temperature of the target\n"
" dilution the dilution (aka sigma0)\n"
" structure the primary group structure\n"
" flux the flux weights\n"
" xs the reaction cross section data"
)
.def_property_readonly(

"metadata",
&Table::metadata,
"The metadata of the table"
)
.def_property_readonly(

"structure",
&Table::structure,
"The primary group structure record"
)
.def_property_readonly(

"flux",
&Table::flux,
"The flux weight record"
)
.def_property_readonly(

"reaction_cross_sections",
&Table::reactionCrossSections,
"The reaction cross section record"
);

// add standard table definitions
addStandardTableDefinitions< Table >( table );
}
19 changes: 19 additions & 0 deletions python/src/NDItk.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace python = pybind11;

// declarations

// declarations - record and subrecord subpackages
void wrapMultigroup( python::module&, python::module& );

// declarations - NDI table types
void wrapMultigroupTable( python::module&, python::module& );

/**
* @brief NDItk python bindings
*
Expand All @@ -17,5 +23,18 @@ namespace python = pybind11;
*/
PYBIND11_MODULE( NDItk, module ) {

python::module::import( "tools" );

// create the views submodule
python::module viewmodule = module.def_submodule(

"sequence",
"sequence - NDI sequences (internal use only)"
);

// record and subrecord subpackages
wrapMultigroup( module, viewmodule );

// wrap ACE table types
wrapMultigroupTable( module, viewmodule );
}
149 changes: 149 additions & 0 deletions python/src/definitions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#ifndef NJOY_NDITK_PYTHON_DEFINITIONS
#define NJOY_NDITK_PYTHON_DEFINITIONS

// system includes
#include <complex>

// other includes
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "tools/views/views-python.hpp"
#include "NDItk/fromFile.hpp"

namespace python = pybind11;

/**
* @brief Add standard subrecord definitions
*
* This adds the following standard properties:
* - values, size, empty
*
* This adds the following standard functions:
* - to_string()
*
* @param[in] record the record to which the definitions have to be added
*/
template < typename Record, typename Range, typename PythonClass >
void addStandardSubrecordDefinitions( PythonClass& subrecord ) {

subrecord
.def_property_readonly(

"values",
[] ( const Record& self ) -> Range
{ return self.values(); },
"The data values of the subrecord"
)
.def_property_readonly(

"size",
[] ( const Record& self ) { return self.size(); },
"The size of the subrecord"
)
.def_property_readonly(

"empty",
[] ( const Record& self ) { return self.empty(); },
"Flag indicating whether or not the subrecord is empty"
)
.def(

"to_string",
[] ( const Record& self ) -> std::string {

std::string buffer;
auto output = std::back_inserter( buffer );
self.print( output );
return buffer;
},
"Return the string representation of the subrecord\n\n"
"Arguments:\n"
" self the subrecord"
);
}

/**
* @brief Add standard record definitions
*
* This adds the following standard properties:
* - keyword, values, size, empty
*
* This adds the following standard functions:
* - to_string()
*
* @param[in] record the record to which the definitions have to be added
*/
template < typename Record, typename Range, typename PythonClass >
void addStandardRecordDefinitions( PythonClass& record ) {

record
.def_property_readonly(

"keyword",
[] ( const Record& self ) { return self.keyword(); },
"The record keyword"
)
.def_property_readonly(

"values",
[] ( const Record& self ) -> Range
{ return self.values(); },
"The data values of the record"
)
.def_property_readonly(

"size",
[] ( const Record& self ) { return self.size(); },
"The size of the record"
)
.def_property_readonly(

"empty",
[] ( const Record& self ) { return self.empty(); },
"Flag indicating whether or not the record is empty"
)
.def(

"to_string",
[] ( const Record& self ) -> std::string {

std::string buffer;
auto output = std::back_inserter( buffer );
self.print( output );
return buffer;
},
"Return the string representation of the record\n\n"
"Arguments:\n"
" self the record"
);
}

/**
* @brief Add standard table definitions
*
* This adds the following standard functions:
* - from_file()
*
* @param[in] table the table to which the definitions have to be added
*/
template < typename Table, typename PythonClass >
void addStandardTableDefinitions( PythonClass& table ) {

table
.def_static(

"from_file",
[] ( const std::string& filename ) -> Table {

return njoy::NDItk::fromFile< Table >( filename );
},
"Read an ACE table from a file\n\n"
"An exception is raised if something goes wrong while reading the\n"
"table\n\n"
"Arguments:\n"
" filename the file name and path"
);
}

#endif

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

// local includes

// namespace aliases
namespace python = pybind11;

namespace multigroup {

// declarations - NDI records and subrecords
void wrapMetadata( python::module&, python::module& );
void wrapCrossSection( python::module&, python::module& );
void wrapFluxWeights( python::module&, python::module& );
void wrapStructure( python::module&, python::module& );
void wrapReactionCrossSections( python::module&, python::module& );
}

void wrapMultigroup( python::module& module, python::module& viewmodule ) {

// create the submodule
python::module submodule = module.def_submodule(

"multigroup",
"Multigroup neutron and photon NDI records and subrecords"
);

multigroup::wrapMetadata( submodule, viewmodule );
multigroup::wrapCrossSection( submodule, viewmodule );
multigroup::wrapFluxWeights( submodule, viewmodule );
multigroup::wrapStructure( submodule, viewmodule );
multigroup::wrapReactionCrossSections( submodule, viewmodule );
}
Loading

0 comments on commit 3c218a5

Please sign in to comment.