Skip to content

Commit

Permalink
Adding python tests
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Jun 5, 2024
1 parent d27cb45 commit e86c286
Show file tree
Hide file tree
Showing 10 changed files with 622 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ if( NDItk.python )
python/src/multigroup/AverageFissionEnergyRelease.python.cpp
python/src/multigroup/FissionNeutronMultiplicity.python.cpp
python/src/multigroup/FissionNeutronProduction.python.cpp
python/src/multigroup/FissionNeutronSpectrumVector.python.cpp
python/src/multigroup/FissionNeutronSpectrumMatrix.python.cpp
python/src/multigroup/Velocities.python.cpp
python/src/multigroup/HeatingNumbers.python.cpp
python/src/multigroup/Kerma.python.cpp
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 @@ -34,6 +34,8 @@ add_python_test( multigroup.TotalCrossSection multigroup/Test_NDItk_
add_python_test( multigroup.AverageFissionEnergyRelease multigroup/Test_NDItk_multigroup_AverageFissionEnergyRelease.py )
add_python_test( multigroup.FissionNeutronMultiplicity multigroup/Test_NDItk_multigroup_FissionNeutronMultiplicity.py )
add_python_test( multigroup.FissionNeutronProduction multigroup/Test_NDItk_multigroup_FissionNeutronProduction.py )
add_python_test( multigroup.FissionNeutronSpectrumMatrix multigroup/Test_NDItk_multigroup_FissionNeutronSpectrumMatrix.py )
add_python_test( multigroup.FissionNeutronSpectrumVector multigroup/Test_NDItk_multigroup_FissionNeutronSpectrumVector.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 )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "f53a715b",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"# a recursive function to find all files with a given extension in a directory\n",
"def getFilesInFolder( directory, extension ) :\n",
"\n",
" files = os.listdir( directory )\n",
" everything = list()\n",
"\n",
" for entry in files :\n",
"\n",
" file = os.path.join( directory, entry )\n",
"\n",
" if os.path.isdir( file ) :\n",
"\n",
" everything += getFilesInFolder( file, extension )\n",
"\n",
" else :\n",
"\n",
" everything.append( file )\n",
"\n",
" return [ file for file in everything if file.endswith( extension ) ]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "74836354",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"FAILED /Users/wim/Projects/libraries/ndi/mt80_30/t20/50118.851nm\n",
"-------------------------\n",
" 1 files\n",
" 1 failed\n",
"elapsed time = 0.000673 s\n",
"-------------------------\n"
]
}
],
"source": [
"import NDItk\n",
"import time\n",
"\n",
"path = '/Users/wim/Projects/libraries/ndi/mt80_30'\n",
"files = getFilesInFolder( path, '1001.851nm' )\n",
"failed = []\n",
"\n",
"start = time.perf_counter()\n",
"for file in files :\n",
"\n",
" try :\n",
"\n",
" table = NDItk.MultigroupTable.from_file( file )\n",
"\n",
" except :\n",
"\n",
" print( 'FAILED', file )\n",
" failed.append( file )\n",
"\n",
"end = time.perf_counter()\n",
"\n",
"print( '-------------------------' )\n",
"print( '{:4} files'.format( len( files ) ) )\n",
"print( '{:4} failed'.format( len( failed ) ) )\n",
"print( 'elapsed time = {:.3} s'.format( end - start ) )\n",
"print( '-------------------------' )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d108125",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
121 changes: 121 additions & 0 deletions python/examples/NDItk-getting-started-example-1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "f53a715b",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"# a recursive function to find all files with a given extension in a directory\n",
"def getFilesInFolder( directory, extension ) :\n",
"\n",
" files = os.listdir( directory )\n",
" everything = list()\n",
"\n",
" for entry in files :\n",
"\n",
" file = os.path.join( directory, entry )\n",
"\n",
" if os.path.isdir( file ) :\n",
"\n",
" everything += getFilesInFolder( file, extension )\n",
"\n",
" else :\n",
"\n",
" everything.append( file )\n",
"\n",
" return [ file for file in everything if file.endswith( extension ) ]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "74836354",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-------------------------\n",
"13455 files\n",
" 0 failed\n",
"elapsed time = 5.79e+02 s\n",
"-------------------------\n"
]
}
],
"source": [
"import NDItk\n",
"import time\n",
"\n",
"path = '/Users/wim/Projects/libraries/ndi/mt80'\n",
"files = getFilesInFolder( path, 'nm' )\n",
"failed = []\n",
"\n",
"start = time.perf_counter()\n",
"for file in files :\n",
"\n",
" try :\n",
"\n",
" table = NDItk.MultigroupTable.from_file( file )\n",
"\n",
" except :\n",
"\n",
" print( 'FAILED', file )\n",
" failed.append( file )\n",
"\n",
"end = time.perf_counter()\n",
"\n",
"print( '-------------------------' )\n",
"print( '{:4} files'.format( len( files ) ) )\n",
"print( '{:4} failed'.format( len( failed ) ) )\n",
"print( 'elapsed time = {:.3} s'.format( end - start ) )\n",
"print( '-------------------------' )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d108125",
"metadata": {},
"outputs": [],
"source": [
"import NDItk\n",
"table = NDItk.MultigroupTable.from_file( '/Users/wim/Projects/libraries/ndi/mt80_30/t22/38086.853nm' )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3ed6d2e8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
4 changes: 4 additions & 0 deletions python/src/multigroup.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace multigroup {
void wrapAverageFissionEnergyRelease( python::module&, python::module& );
void wrapFissionNeutronMultiplicity( python::module&, python::module& );
void wrapFissionNeutronProduction( python::module&, python::module& );
void wrapFissionNeutronSpectrumMatrix( python::module&, python::module& );
void wrapFissionNeutronSpectrumVector( python::module&, python::module& );
void wrapOutgoingParticleTypes( python::module&, python::module& );
void wrapOutgoingParticleTransportData( python::module&, python::module& );
void wrapLegendreMoment( python::module&, python::module& );
Expand Down Expand Up @@ -51,6 +53,8 @@ void wrapMultigroup( python::module& module, python::module& viewmodule ) {
multigroup::wrapAverageFissionEnergyRelease( submodule, viewmodule );
multigroup::wrapFissionNeutronMultiplicity( submodule, viewmodule );
multigroup::wrapFissionNeutronProduction( submodule, viewmodule );
multigroup::wrapFissionNeutronSpectrumMatrix( submodule, viewmodule );
multigroup::wrapFissionNeutronSpectrumVector( submodule, viewmodule );
multigroup::wrapOutgoingParticleTypes( submodule, viewmodule );
multigroup::wrapOutgoingParticleTransportData( submodule, viewmodule );
multigroup::wrapLegendreMoment( submodule, viewmodule );
Expand Down
80 changes: 80 additions & 0 deletions python/src/multigroup/FissionNeutronSpectrumMatrix.python.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// system includes
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

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

// namespace aliases
namespace python = pybind11;

namespace multigroup {

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

// type aliases
using Record = njoy::NDItk::multigroup::FissionNeutronSpectrumMatrix;
using FissionType = njoy::NDItk::multigroup::FissionType;

// wrap views created by this record

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

module,
"FissionNeutronSpectrumMatrix",
"A fission neutron spectrum matrix record for multigroup neutron and photon data"
);

// wrap the record
record
.def(

python::init< FissionType, std::vector< double > >(),
python::arg( "type" ), python::arg( "values" ),
"Initialise the record\n\n"
"Arguments:\n"
" self the record\n"
" values the fission neutron spectrum matrix values"
)
.def_property_readonly(

"type",
&Record::type,
"The fission type defined by this record"
)
.def_property_readonly(

"number_groups",
&Record::numberGroups,
"The number of groups defined by this record"
)
.def_property_readonly(

"matrix",
[] ( const Record& self ) -> DoubleRange2D
{ return self.matrix(); },
"The matrix defined by this record"
)
.def_static(

"from_string",
[] ( const std::string& string, unsigned int number ) -> Record
{ return readWithFissionSubtype< 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 groups"
);

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

} // multigroup namespace
Loading

0 comments on commit e86c286

Please sign in to comment.