Skip to content

Commit

Permalink
Updating ...
Browse files Browse the repository at this point in the history
  • Loading branch information
whaeck committed Nov 3, 2021
2 parents 4da8ba2 + a25a0da commit 726564b
Show file tree
Hide file tree
Showing 13 changed files with 553 additions and 94 deletions.
56 changes: 29 additions & 27 deletions autogen/json2class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2171,24 +2171,21 @@ void filePythonClass(const InfoSpecs &specs, const PerClass &per)
{ "quad" , { "long double" , "quads" } }
};

std::string dataType = "unknownType";
std::string dataName = "data";
std::vector< std::pair< std::string, std::string > > dataTypesNames;
if (per.isData) {
// try to find per.dataType in the map
auto it = map.find(per.dataType);
if (it == map.end()) {
// look in metadata for valueType; if found,
// try to find its default value in the map
for (auto &m : per.metadata) {
if (m.name == "valueType") {
it = map.find(m.defaultValue);
break;
}
}
}
if (it != map.end()) {
dataType = it->second.first;
dataName = it->second.second;
// this is a node with a fixed data type
dataTypesNames.emplace_back( it->second.first, it->second.second );
}
else {
// this is a node with a runtime data type: select types to expose
std::array< std::string, 3 > types = { "int", "double", "string" };
for ( const auto& type : types ) {
it = map.find( type );
dataTypesNames.emplace_back( it->second.first, it->second.second );
}
}
}

Expand Down Expand Up @@ -2239,11 +2236,11 @@ void filePythonClass(const InfoSpecs &specs, const PerClass &per)
out();
out(1,"// wrap the component");
out(1,"component");
out(2,".def(");

// python::init<...>
// python::init<...> for attributes and children
out(2,".def(");
out(3,"python::init<");
int count = 0, total = per.nfields() + per.isData;
int count = 0, total = per.nfields();
for (auto &m : per.metadata)
out(4,"const @ &@",
m.isDefaulted ? "std::optional<" + m.type + ">" : m.typeFull,
Expand All @@ -2252,11 +2249,7 @@ void filePythonClass(const InfoSpecs &specs, const PerClass &per)
out(4,"const @ &@", c.typeFull, ++count < total ? "," : "");
for (auto &v : per.variants)
out(4,"const @ &@", v.typeFull, ++count < total ? "," : "");
if (per.isData)
out(4,"const std::vector<@> &", dataType);
out(3,">(),");

// python::arg...
for (auto &m : per.metadata)
out(3,"python::arg(\"@\")@,",
namePython(m.name),
Expand All @@ -2266,11 +2259,20 @@ void filePythonClass(const InfoSpecs &specs, const PerClass &per)
c.isOptional ? " = std::nullopt" : "");
for (auto &v : per.variants)
out(3,"python::arg(\"@\"),", namePython(v.name));
if (per.isData)
out(3,"python::arg(\"values\"),");
out(3,"Component::documentation(\"constructor\").data()");
out(2,")");

// python::init<...> for each data type and name pair
for ( const auto& dataTypeName : dataTypesNames ) {
out(2,".def(");
out(3,"python::init<");
out(4,"const std::vector<@> &", dataTypeName.first);
out(3,">(),");
out(3,"python::arg(\"@\"),", dataTypeName.second);
out(3,"Component::documentation(\"constructor\").data()");
out(2,")");
}

// .def_property_readonly...
for (auto &m : per.metadata) {
const auto pyname = namePython(m.name);
Expand Down Expand Up @@ -2312,11 +2314,11 @@ void filePythonClass(const InfoSpecs &specs, const PerClass &per)
out(2,")");
}

if (per.isData) {
for ( const auto& dataTypeName : dataTypesNames ) {
out(2,".def_property_readonly(");
out(3,"\"@\",", dataName);
out(3,"[] (const Component &self) { return self.@(); },", dataName);
out(3,"Component::documentation(\"@\").data()", dataName);
out(3,"\"@\",", dataTypeName.second);
out(3,"[] (const Component &self) { return self.@(); },", dataTypeName.second);
out(3,"Component::documentation(\"@\").data()", dataTypeName.second);
out(2,")");
}

Expand Down
1 change: 1 addition & 0 deletions cmake/unit_testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ add_subdirectory( src/GNDStk/v1.9/containers/Link/test )
add_subdirectory( src/GNDStk/v1.9/containers/Grid/test )
add_subdirectory( src/GNDStk/v1.9/containers/Axes/test )
add_subdirectory( src/GNDStk/v1.9/containers/XYs1d/test )
add_subdirectory( src/GNDStk/v1.9/containers/Regions1d/test )
35 changes: 32 additions & 3 deletions python/src/v1.9/containers/Values.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,32 @@ void wrapValues(python::module &module)
python::init<
const std::optional<int> &,
const std::optional<int> &,
const std::optional<std::string> &,
const std::vector<double> &
const std::optional<std::string> &
>(),
python::arg("length") = std::nullopt,
python::arg("start") = std::nullopt,
python::arg("value_type") = std::nullopt,
python::arg("values"),
Component::documentation("constructor").data()
)
.def(
python::init<
const std::vector<int> &
>(),
python::arg("ints"),
Component::documentation("constructor").data()
)
.def(
python::init<
const std::vector<double> &
>(),
python::arg("doubles"),
Component::documentation("constructor").data()
)
.def(
python::init<
const std::vector<std::string> &
>(),
python::arg("strings"),
Component::documentation("constructor").data()
)
.def_property_readonly(
Expand All @@ -62,11 +81,21 @@ void wrapValues(python::module &module)
[](const Component &self) { return self.valueType().value(); },
Component::documentation("value_type").data()
)
.def_property_readonly(
"ints",
[] (const Component &self) { return self.ints(); },
Component::documentation("ints").data()
)
.def_property_readonly(
"doubles",
[] (const Component &self) { return self.doubles(); },
Component::documentation("doubles").data()
)
.def_property_readonly(
"strings",
[] (const Component &self) { return self.strings(); },
Component::documentation("strings").data()
)
;

// add standard component definitions
Expand Down
17 changes: 17 additions & 0 deletions python/src/v1.9/transport/ReactionSuite.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ void wrapReactionSuite(python::module &module)
python::overload_cast<>(&Component::reactions),
Component::documentation("reactions").data()
)
.def_static(

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

using namespace njoy::GNDStk::core;
Tree tree( filename );

return Component( tree( child::reactionSuite ) );
},
python::arg( "filename" ),
"Read a reaction suite from an XML or json file\n\n"
"An exception is raised if something goes wrong while reading the\n"
"component\n\n"
"Arguments:\n"
" filename the name of the file"
)
;

// add standard component definitions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def verify_chunk( self, chunk ) :
# the data is given explicitly
chunk = Grid( index = 2, label = "row_energy_bounds",
unit = "eV", style = GridStyle.boundaries,
link_values = Values( 2, 0, "Float64", [ 1e-5, 2e7 ] ) )
link_values = Values( [ 1e-5, 2e7 ] ) )

verify_chunk( self, chunk )

Expand Down
85 changes: 75 additions & 10 deletions python/test/v1_9/containers/Test_GNDStk_v1_9_containers_Values.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
class Test_GNDStk_v1_9_containers_Values( unittest.TestCase ) :
"""Unit test for the Section class."""

chunk = ( '<values length="4" start="0" valueType="Float64">2500 8.9172 2550 8.9155</values>\n' )
chunk_doubles = ( '<values length="4" start="0" valueType="Float64">2500 8.9172 2550 8.9155</values>\n' )
chunk_ints = ( '<values length="4" start="0" valueType="Integer32">2500 9 2550 9</values>\n' )
chunk_strings = ( '<values length="4" start="0" valueType="UTF8Text">2500 8.9172 2550 8.9155</values>\n' )
wrong = ( '<wrongName length="4" start="0" valueType="Float64">2500 8.9172 2550 8.9155</wrongName>\n' )

def test_component( self ) :

def verify_chunk( self, chunk ) :
def verify_chunk_doubles( self, chunk ) :

# verify content
self.assertEqual( 4, chunk.length )
Expand All @@ -29,23 +31,86 @@ def verify_chunk( self, chunk ) :
self.assertAlmostEqual( 8.9155, chunk.doubles[3] )

# verify string
self.assertEqual( self.chunk, chunk.to_xml_string() )
self.assertEqual( self.chunk_doubles, chunk.to_xml_string() )

# the data is given explicitly (fully specified)
chunk = Values( length = 4, start = 0, value_type = "Float64",
values = [ 2500., 8.9172, 2550., 8.9155 ] )
def verify_chunk_ints( self, chunk ) :

verify_chunk( self, chunk )
# verify content
self.assertEqual( 4, chunk.length )
self.assertEqual( 0, chunk.start )
self.assertEqual( "Integer32", chunk.value_type )

self.assertEqual( 4, len( chunk.ints ) )

self.assertEqual( 2500, chunk.ints[0] )
self.assertEqual( 9, chunk.ints[1] )
self.assertEqual( 2550, chunk.ints[2] )
self.assertEqual( 9, chunk.ints[3] )

# verify string
self.assertEqual( self.chunk_ints, chunk.to_xml_string() )

def verify_chunk_strings( self, chunk ) :

# verify content
self.assertEqual( 4, chunk.length )
self.assertEqual( 0, chunk.start )
self.assertEqual( "UTF8Text", chunk.value_type )

self.assertEqual( 4, len( chunk.strings ) )

self.assertAlmostEqual( "2500", chunk.strings[0] )
self.assertAlmostEqual( "8.9172", chunk.strings[1] )
self.assertAlmostEqual( "2550", chunk.strings[2] )
self.assertAlmostEqual( "8.9155", chunk.strings[3] )

# verify string
self.assertEqual( self.chunk_strings, chunk.to_xml_string() )

# the data is given explicitly (doubles)
chunk = Values( doubles = [ 2500., 8.9172, 2550., 8.9155 ] )

verify_chunk_doubles( self, chunk )

# the data is read from a string
chunk = Values.from_string( self.chunk_doubles )

verify_chunk_doubles( self, chunk )

# the data is copied
copy = Values( chunk )

verify_chunk_doubles( self, copy )

# the data is given explicitly (ints)
chunk = Values( ints = [ 2500, 9, 2550, 9 ] )

verify_chunk_ints( self, chunk )

# the data is read from a string
chunk = Values.from_string( self.chunk_ints )

verify_chunk_ints( self, chunk )

# the data is copied
copy = Values( chunk )

verify_chunk_ints( self, copy )

# the data is given explicitly (strings)
chunk = Values( strings = [ "2500", "8.9172", "2550", "8.9155" ] )

verify_chunk_strings( self, chunk )

# the data is read from a string
chunk = Values.from_string( self.chunk )
chunk = Values.from_string( self.chunk_strings )

verify_chunk( self, chunk )
verify_chunk_strings( self, chunk )

# the data is copied
copy = Values( chunk )

verify_chunk( self, copy )
verify_chunk_strings( self, copy )

def test_failures( self ) :

Expand Down
12 changes: 6 additions & 6 deletions src/GNDStk/precision/test/precision.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ SCENARIO("Precision code in BodyText::get(), "

WHEN("We rework a vector<float> into a vector<string>") {
const std::vector<float> fvec = {{
std::sqrt(float(2)),
std::sqrt(float(3)),
std::sqrt(float(5))
static_cast<float>(std::sqrt(double(2))),
static_cast<float>(std::sqrt(double(3))),
static_cast<float>(std::sqrt(double(5)))
}};

using precision::key::child::reals;
Expand Down Expand Up @@ -446,9 +446,9 @@ SCENARIO("Precision code in BodyText::toNode(), "

WHEN("We print floats>") {
const std::vector<float> fvec = {{
std::sqrt(float(2)),
std::sqrt(float(3)),
std::sqrt(float(5))
static_cast<float>(std::sqrt(double(2))),
static_cast<float>(std::sqrt(double(3))),
static_cast<float>(std::sqrt(double(5)))
}};

THEN("data::floats precision has the intended effect") {
Expand Down
5 changes: 1 addition & 4 deletions src/GNDStk/v1.9/containers/Grid/test/Grid.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ SCENARIO( "Grid" ) {
enums::GridStyle style = enums::GridStyle::boundaries;
std::vector< double > values = { 1e-5, 2e7 };

Grid chunk( index, std::nullopt, label, style, unit,
Values( 2, 0, "Float64", values ) );
Grid chunk( index, std::nullopt, label, style, unit, Values( values ) );

THEN( "the component can be constructed and members can be tested" ) {

Expand Down Expand Up @@ -209,8 +208,6 @@ void verifyChunk( const Grid& component ) {
CHECK( 0 == values.start() );
CHECK( "Float64" == values.valueType().value() );

CHECK( "1e-05 2e+07" == values.string() );

CHECK( 2 == values.size() );
CHECK( 2 == values.doubles().size() );

Expand Down
18 changes: 18 additions & 0 deletions src/GNDStk/v1.9/containers/Regions1d/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

add_executable( GNDStk.v1.9.containers.Regions1d.test Regions1d.test.cpp )
target_compile_options( GNDStk.v1.9.containers.Regions1d.test PRIVATE ${${PREFIX}_common_flags}
$<$<BOOL:${strict}>:${${PREFIX}_strict_flags}>$<$<CONFIG:DEBUG>:
${${PREFIX}_DEBUG_flags}
$<$<BOOL:${coverage}>:${${PREFIX}_coverage_flags}>>
$<$<CONFIG:RELEASE>:
${${PREFIX}_RELEASE_flags}
$<$<BOOL:${link_time_optimization}>:${${PREFIX}_link_time_optimization_flags}>
$<$<BOOL:${nonportable_optimization}>:${${PREFIX}_nonportable_optimization_flags}>>

${CXX_appended_flags} ${GNDStk_appended_flags} )
target_link_libraries( GNDStk.v1.9.containers.Regions1d.test PUBLIC GNDStk )
file( GLOB resources "resources/*" )
foreach( resource ${resources})
file( COPY "${resource}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}" )
endforeach()
add_test( NAME GNDStk.v1.9.containers.Regions1d COMMAND GNDStk.v1.9.containers.Regions1d.test )
Loading

0 comments on commit 726564b

Please sign in to comment.