Commit 233728fd authored by jannick.wolters's avatar jannick.wolters
Browse files

fix python issue

parent 21b2ce41
......@@ -134,7 +134,7 @@ execute_process(
)
add_compile_definitions( GIT_HASH="${GIT_HASH}" )
set( CORE_LIBRARIES ${Python3_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES} ${MPI_LIBRARIES} ${VTK_LIBRARIES} parmetis -lstdc++fs )
set( CORE_LIBRARIES ${Python3_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES} ${MPI_LIBRARIES} ${VTK_LIBRARIES} OpenMP::OpenMP_CXX parmetis -lstdc++fs )
target_link_libraries( ${CMAKE_PROJECT_NAME} ${CORE_LIBRARIES} )
......
......@@ -61,6 +61,22 @@ inline const void PrintVectorVector( const VectorVector vectorIn ) {
}
}
/*!
* \brief utility function for returning the last number in a string
* \param [in] string to be checked
*/
inline int GetTrailingNumber( std::string const& str ) { return std::stoi( str.substr( str.find_first_of( "0123456789" ), str.length() - 1 ) ); }
/*!
* \brief utility function for checking if a string has a certain ending
* \param [in] string to be checked
* \param [in] ending to be checked for
*/
inline bool StringEndsWith( std::string const& value, std::string const& ending ) {
if( ending.size() > value.size() ) return false;
return std::equal( ending.rbegin(), ending.rend(), value.rbegin() );
}
} // namespace TextProcessingToolbox
#endif // TEXTPROCESSINGTOOLBOX_H
#include "io.h"
#include "toolboxes/errormessages.h"
#include "toolboxes/textprocessingtoolbox.h"
void ExportVTK( const std::string fileName,
const std::vector<std::vector<std::vector<double>>>& results,
......@@ -17,7 +18,7 @@ void ExportVTK( const std::string fileName,
auto writer = vtkUnstructuredGridWriterSP::New();
std::string fileNameWithExt = fileName;
if( fileNameWithExt.substr( fileNameWithExt.find_last_of( "." ) + 1 ) != ".vtk" ) {
if( !TextProcessingToolbox::StringEndsWith( fileNameWithExt, ".vtk" ) ) {
fileNameWithExt.append( ".vtk" );
}
writer->SetFileName( fileNameWithExt.c_str() );
......@@ -114,7 +115,7 @@ Mesh* LoadSU2MeshFromFile( const Config* settings ) {
if( ifs.is_open() ) {
while( getline( ifs, line ) ) {
if( line.find( "NDIME", 0 ) != std::string::npos ) {
dim = static_cast<unsigned>( std::stoi( line.substr( line.find_first_of( "0123456789" ), line.length() - 1 ) ) );
dim = static_cast<unsigned>( TextProcessingToolbox::GetTrailingNumber( line ) );
break;
}
}
......@@ -122,7 +123,7 @@ Mesh* LoadSU2MeshFromFile( const Config* settings ) {
ifs.seekg( 0, std::ios::beg );
while( getline( ifs, line ) ) {
if( line.find( "NPOIN", 0 ) != std::string::npos ) {
unsigned numPoints = static_cast<unsigned>( std::stoi( line.substr( line.find_first_of( "0123456789" ), line.length() - 1 ) ) );
unsigned numPoints = static_cast<unsigned>( TextProcessingToolbox::GetTrailingNumber( line ) );
nodes.resize( numPoints, Vector( dim, 0.0 ) );
for( unsigned i = 0; i < numPoints; ++i ) {
getline( ifs, line );
......@@ -146,7 +147,7 @@ Mesh* LoadSU2MeshFromFile( const Config* settings ) {
ifs.seekg( 0, std::ios::beg );
while( getline( ifs, line ) ) {
if( line.find( "NMARK", 0 ) != std::string::npos ) {
unsigned numBCs = static_cast<unsigned>( std::stoi( line.substr( line.find_first_of( "0123456789" ), line.length() - 1 ) ) );
unsigned numBCs = static_cast<unsigned>( TextProcessingToolbox::GetTrailingNumber( line ) );
boundaries.resize( numBCs );
for( unsigned i = 0; i < numBCs; ++i ) {
std::string markerTag;
......@@ -165,8 +166,7 @@ Mesh* LoadSU2MeshFromFile( const Config* settings ) {
}
}
else if( line.find( "MARKER_ELEMS", 0 ) != std::string::npos ) {
unsigned numMarkerElements =
static_cast<unsigned>( std::stoi( line.substr( line.find_first_of( "0123456789" ), line.length() - 1 ) ) );
unsigned numMarkerElements = static_cast<unsigned>( TextProcessingToolbox::GetTrailingNumber( line ) );
for( unsigned j = 0; j < numMarkerElements; ++j ) {
getline( ifs, line );
std::stringstream ss;
......@@ -198,7 +198,7 @@ Mesh* LoadSU2MeshFromFile( const Config* settings ) {
std::vector<unsigned> numNodesPerCell;
while( getline( ifs, line ) ) {
if( line.find( "NELEM", 0 ) != std::string::npos ) {
unsigned numCells = static_cast<unsigned>( std::stoi( line.substr( line.find_first_of( "0123456789" ), line.length() - 1 ) ) );
unsigned numCells = static_cast<unsigned>( TextProcessingToolbox::GetTrailingNumber( line ) );
numNodesPerCell.resize( numCells, 0u );
for( unsigned i = 0; i < numCells; ++i ) {
getline( ifs, line );
......@@ -231,7 +231,7 @@ Mesh* LoadSU2MeshFromFile( const Config* settings ) {
ifs.seekg( 0, std::ios::beg );
while( getline( ifs, line ) ) {
if( line.find( "NELEM", 0 ) != std::string::npos ) {
unsigned numCells = static_cast<unsigned>( std::stoi( line.substr( line.find_first_of( "0123456789" ), line.length() - 1 ) ) );
unsigned numCells = static_cast<unsigned>( TextProcessingToolbox::GetTrailingNumber( line ) );
cells.resize( numCells, std::vector<unsigned>( numNodesPerCell[0], 0u ) );
for( unsigned i = 0; i < numCells; ++i ) {
getline( ifs, line );
......@@ -320,18 +320,16 @@ Matrix createSU2MeshFromImage( std::string imageName, std::string SU2Filename )
if( !std::filesystem::exists( imageName ) ) {
ErrorMessages::Error( "Can not open image '" + imageName + "'!", CURRENT_FUNCTION );
exit( EXIT_FAILURE );
}
std::filesystem::path outDir( std::filesystem::path( SU2Filename ).parent_path() );
if( !std::filesystem::exists( outDir ) ) {
ErrorMessages::Error( "Output directory '" + outDir.string() + "' does not exists!", CURRENT_FUNCTION );
exit( EXIT_FAILURE );
}
std::cout << getenv( "PYTHONPATH" ) << std::endl;
setenv( "PYTHONPATH", RTSN_PYTHON_PATH, 1 );
std::cout << getenv( "PYTHONPATH" ) << std::endl << std::flush;
Py_Initialize();
if( !Py_IsInitialized() ) {
setenv( "PYTHONPATH", RTSN_PYTHON_PATH, 1 );
Py_Initialize();
}
PyObject *pArgs, *pReturn, *pModule, *pFunc;
PyArrayObject* np_ret;
......@@ -340,18 +338,16 @@ Matrix createSU2MeshFromImage( std::string imageName, std::string SU2Filename )
PyObject* pName = PyUnicode_FromString( "mesh_from_image" );
pModule = PyImport_Import( pName );
Py_DECREF( pName );
Py_CLEAR( pName );
if( !pModule ) {
ErrorMessages::Error( "'mesh_from_image.py' can not be imported!", CURRENT_FUNCTION );
exit( EXIT_FAILURE );
}
pFunc = PyObject_GetAttrString( pModule, "generate" );
if( !pFunc || !PyCallable_Check( pFunc ) ) {
Py_DECREF( pModule );
Py_XDECREF( pFunc );
Py_CLEAR( pModule );
Py_CLEAR( pFunc );
ErrorMessages::Error( "'generate' is null or not callable!", CURRENT_FUNCTION );
exit( EXIT_FAILURE );
}
pArgs = PyTuple_New( 2 );
......@@ -367,10 +363,9 @@ Matrix createSU2MeshFromImage( std::string imageName, std::string SU2Filename )
Matrix gsImage( m, n, c_out );
// Finalizing
Py_DECREF( pFunc );
Py_DECREF( pModule );
Py_DECREF( np_ret );
Py_Finalize();
Py_CLEAR( pFunc );
Py_CLEAR( pModule );
Py_CLEAR( np_ret );
return gsImage;
}
#include <Python.h>
#include <mpi.h>
#include "io.h"
......@@ -7,6 +8,8 @@
int main( int argc, char** argv ) {
MPI_Init( &argc, &argv );
wchar_t* program = Py_DecodeLocale( argv[0], NULL );
Py_SetProgramName( program );
std::string filename = ParseArguments( argc, argv );
......
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
#include <Python.h>
#include <mpi.h>
int main( int argc, char** argv ) {
MPI_Init( &argc, &argv );
wchar_t* program = Py_DecodeLocale( argv[0], NULL );
Py_SetProgramName( program );
const int result = Catch::Session().run( argc, argv );
MPI_Finalize();
return result;
......
......@@ -4,6 +4,7 @@
#include "catch.hpp"
#include "cubic2dspline.h"
#include "io.h"
#include "toolboxes/textprocessingtoolbox.h"
/*
TEST_CASE( "convert image data to grayscale matrix and mesh", "[image I/O]" ) {
......@@ -96,44 +97,85 @@ TEST_CASE( "convert image data to grayscale matrix", "[image I/O]" ) {
std::string testMesh = config->GetMeshFile();
Matrix gsImage = createSU2MeshFromImage( testImage, testMesh );
SECTION( "grayscale matrix" ) {
REQUIRE( std::filesystem::exists( testMesh ) ); // mesh has been created
REQUIRE( gsImage.rows() > 0 ); // atleast some data is stored
REQUIRE( gsImage.columns() > 0 ); //
REQUIRE( blaze::min( gsImage ) >= 0 ); // lower bound
REQUIRE( blaze::max( gsImage ) <= 1.0 ); // upper bound
REQUIRE( std::filesystem::exists( testMesh ) ); // mesh has been created
REQUIRE( gsImage.rows() > 0 ); // atleast some data is stored
REQUIRE( gsImage.columns() > 0 ); //
REQUIRE( blaze::min( gsImage ) >= 0 ); // lower bound
REQUIRE( blaze::max( gsImage ) <= 1.0 ); // upper bound
// load reference matrix from csv file
std::string refMatrixFile = "../tests/input/phantom.csv";
std::ifstream data( refMatrixFile );
REQUIRE( data.is_open() );
std::string line;
std::vector<std::vector<double>> refMatrix;
while( std::getline( data, line ) ) {
std::stringstream lineStream( line );
std::string cell;
std::vector<double> row;
while( std::getline( lineStream, cell, ',' ) ) {
row.push_back( std::stod( cell ) );
// load reference matrix from csv file
std::string refMatrixFile = "../tests/input/phantom.csv";
std::ifstream data( refMatrixFile );
REQUIRE( data.is_open() );
std::string line;
std::vector<std::vector<double>> refMatrix;
while( std::getline( data, line ) ) {
std::stringstream lineStream( line );
std::string cell;
std::vector<double> row;
while( std::getline( lineStream, cell, ',' ) ) {
row.push_back( std::stod( cell ) );
}
refMatrix.push_back( row );
}
REQUIRE( refMatrix.size() == gsImage.rows() ); // equal number of rows
REQUIRE( refMatrix[0].size() == gsImage.columns() ); // equal number of columns
REQUIRE( std::all_of( begin( refMatrix ), end( refMatrix ), [refMatrix]( const std::vector<double>& x ) {
return x.size() == refMatrix[0].size();
} ) ); // consistency check if all columns of the read-in file have equal length
for( unsigned i = 0; i < gsImage.rows(); ++i ) {
for( unsigned j = 0; j < gsImage.columns(); ++j ) {
REQUIRE( refMatrix[i][j] == gsImage( i, j ) ); // all values match
}
}
refMatrix.push_back( row );
}
REQUIRE( refMatrix.size() == gsImage.rows() ); // equal number of rows
REQUIRE( refMatrix[0].size() == gsImage.columns() ); // equal number of columns
REQUIRE( std::all_of( begin( refMatrix ), end( refMatrix ), [refMatrix]( const std::vector<double>& x ) {
return x.size() == refMatrix[0].size();
} ) ); // consistency check if all columns of the read-in file have equal length
SECTION( "interpolation of grayscale matrix onto the generated mesh" ) {
Mesh* mesh = LoadSU2MeshFromFile( config );
auto cellCenters = mesh->GetCellMidPoints();
auto bounds = mesh->GetBounds();
double xMin = bounds[0].first;
double xMax = bounds[0].second;
double yMin = bounds[1].first;
double yMax = bounds[1].second;
unsigned m = gsImage.rows();
unsigned n = gsImage.columns();
Vector x( m + 1 ), y( n + 1 );
for( unsigned i = 0; i < m + 1; ++i ) {
x[i] = static_cast<double>( i ) / static_cast<double>( m ) * ( xMax - xMin );
}
for( unsigned i = 0; i < n + 1; ++i ) y[i] = static_cast<double>( i ) / static_cast<double>( n ) * ( yMax - yMin );
for( unsigned i = 0; i < gsImage.rows(); ++i ) {
for( unsigned j = 0; j < gsImage.columns(); ++j ) {
REQUIRE( refMatrix[i][j] == gsImage( i, j ) ); // all values match
Cubic2DSpline interp( x, y, gsImage );
std::vector<double> result( mesh->GetNumCells(), 0.0 );
for( unsigned i = 0; i < mesh->GetNumCells(); ++i ) {
result[i] = interp( cellCenters[i][0], cellCenters[i][1] );
}
std::vector<std::string> fieldNames{ "CT Data" };
std::vector<std::vector<double>> scalarField( 1, result );
std::vector<std::vector<std::vector<double>>> results{ scalarField };
std::string outputFile = config->GetOutputFile();
if( !TextProcessingToolbox::StringEndsWith( outputFile, ".vtk" ) ) outputFile.append( ".vtk" );
ExportVTK( outputFile, results, fieldNames, mesh );
REQUIRE( std::filesystem::exists( outputFile ) );
delete mesh;
std::remove( outputFile.c_str() );
}
std::remove( testMesh.c_str() );
}
/*
TEST_CASE( "interpolation of grayscale matrix onto the generated mesh", "[image I/O]" ) {
std::string config_file_name = "../tests/input/image_conversion.cfg";
......@@ -171,12 +213,15 @@ TEST_CASE( "interpolation of grayscale matrix onto the generated mesh", "[image
std::vector<std::string> fieldNames{ "CTData" };
std::vector<std::vector<double>> scalarField( 1, result );
std::vector<std::vector<std::vector<double>>> results{ scalarField };
ExportVTK( config->GetOutputFile(), results, fieldNames, mesh );
std::string outputFile = config->GetOutputFile();
if( !TextProcessingToolbox::StringEndsWith( outputFile, ".vtk" ) ) outputFile += ".vtk";
ExportVTK( outputFile, results, fieldNames, mesh );
REQUIRE( std::filesystem::exists( config->GetOutputFile() + ".vtk" ) );
REQUIRE( std::filesystem::exists( outputFile ) );
delete mesh;
std::remove( testMesh.c_str() );
// std::remove( config->GetOutputFile().c_str() );
}
*/
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment