Commit e634dfad authored by dedner's avatar dedner
Browse files

start on python bindings

export the first vtk writer (unstructured vtk) still very crude

improve testvtk.py script

[bugfix] added missing 'unkown' to 'DataTypes' to_string

added lists for enum to allow for iteration which help with the export topython
parent b2687dc3
......@@ -28,5 +28,11 @@ add_subdirectory(doc)
add_subdirectory(lib)
add_subdirectory(cmake/modules)
# if Python bindings are enabled, include necessary sub directories.
if( DUNE_ENABLE_PYTHONBINDINGS )
add_subdirectory(python)
dune_python_install_package(PATH python)
endif()
# finalize the dune project, e.g. generating config.h etc.
finalize_dune_project(GENERATE_CONFIG_H_CMAKE)
add_subdirectory(vtk)
# if Python bindings are enabled, include necessary sub directories.
if( DUNE_ENABLE_PYTHONBINDINGS )
add_subdirectory("python")
endif()
add_subdirectory(vtk)
add_subdirectory(test)
import math
from dune.grid import structuredGrid, gridFunction
from dune.vtk import vtkUnstructuredGridWriter
grid = structuredGrid([0,0],[1,1],[10,10])
def test1(writer):
@gridFunction(grid)
def f(x):
return x[0]*x[1]
writer.addPointData(f)
del f
def test2(writer):
@gridFunction(grid)
def g(x):
return [math.sin(2*math.pi*x[0]*x[1]), x[0]*x[1]]
writer.addPointData(g)
del g
writer1 = vtkUnstructuredGridWriter(grid)
test1(writer1)
writer1.write("test1")
writer2 = vtkUnstructuredGridWriter(grid)
test2(writer2)
writer2.write("test2")
# fails since 'name' is missing on grid function
writer = vtkUnstructuredGridWriter(grid)
test1(writer)
test2(writer)
writer.write("test")
set(HEADERS
writer.hh
)
install(FILES ${HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dune/python/vtk)
// -*- tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_PYTHON_VTK_WRITER_HH
#define DUNE_PYTHON_VTK_WRITER_HH
#include <dune/vtk/vtkwriter.hh>
#include <dune/python/pybind11/pybind11.h>
namespace Dune
{
namespace Vtk
{
// registerVTKWriter
// -----------------
template< class Writer, class... options >
inline static void registerVtkWriter ( pybind11::handle scope,
pybind11::class_< Writer, options... > cls )
{
using GridView = typename Writer::GridView;
using VirtualizedGF = Dune::Vtk::Function<GridView>;
cls.def( pybind11::init( [] ( GridView &grid )
{ return new Writer( grid ); } ),
pybind11::keep_alive< 1, 2 >() );
cls.def( "write",
[] ( Writer &writer, const std::string &name) {
writer.write( name );
},
pybind11::arg("name") );
cls.def( "write",
[] ( Writer &writer, const std::string &name, int number ) {
std::stringstream s; s << name << std::setw(5) << std::setfill('0') << number;
writer.write( s.str());
},
pybind11::arg("name"),
pybind11::arg("number") );
cls.def( "addPointData",
[] ( Writer &writer, VirtualizedGF &f) {
writer.addPointData(f);
},
pybind11::arg("f") );
}
} // namespace Python
} // namespace Dune
#endif // #ifndef DUNE_PYTHON_GRID_VTK_HH
......@@ -78,6 +78,7 @@ Vtk::RangeTypes rangeTypeOf (int ncomps)
std::string to_string (Vtk::DataTypes type)
{
switch (type) {
case Vtk::DataTypes::UNKNOWN: return "unknown";
case Vtk::DataTypes::INT8: return "Int8";
case Vtk::DataTypes::UINT8: return "UInt8";
case Vtk::DataTypes::INT16: return "Int16";
......
......@@ -25,6 +25,7 @@ namespace Dune
APPENDED = BINARY | COMPRESSED
};
std::string to_string (Vtk::FormatTypes);
inline auto formatTypesList = {FormatTypes::ASCII, FormatTypes::BINARY, FormatTypes::COMPRESSED, FormatTypes::APPENDED};
/// Map the dune-grid OutputType to FormatTypes
Vtk::FormatTypes formatTypeOf(Dune::VTK::OutputType);
......@@ -39,6 +40,7 @@ namespace Dune
TENSOR //< Use exactly 9 components
};
std::string to_string (Vtk::RangeTypes);
inline auto rangeTypesList = { RangeTypes::UNSPECIFIED, RangeTypes::AUTO, RangeTypes::SCALAR, RangeTypes::VECTOR, RangeTypes::TENSOR };
// Map a dune-grid FieldInfo::Type to ValueTypes
Vtk::RangeTypes rangeTypeOf (Dune::VTK::FieldInfo::Type);
......@@ -57,6 +59,14 @@ namespace Dune
FLOAT64 = 64
};
std::string to_string (Vtk::DataTypes);
inline auto dataTypesLists = {
DataTypes::UNKNOWN,
DataTypes::INT8, DataTypes::UINT8,
DataTypes::INT16, DataTypes::UINT16,
DataTypes::INT32, DataTypes::UINT32,
DataTypes::INT64, DataTypes::UINT64,
DataTypes::FLOAT32, DataTypes::FLOAT64
};
// Map a dune-grid Precision type to DataTypes
Vtk::DataTypes dataTypeOf (Dune::VTK::Precision);
......
......@@ -25,6 +25,8 @@ namespace Dune
{
template <class> friend class TimeseriesWriter;
template <class> friend class PvdWriter;
public:
using GridView = GV;
public:
using GridView = GV;
......
[build-system]
requires = ['setuptools', 'wheel', 'scikit-build', 'cmake', 'ninja', 'requests', 'dune-common<=2.8.0.dev', 'dune-geometry<=2.8.0.dev', 'dune-grid<=2.8.0.dev', 'dune-localfunctions<=2.8.0.dev']
build-backend = 'setuptools.build_meta'
add_subdirectory(dune)
configure_file(setup.py.in setup.py)
add_subdirectory(vtk)
add_python_targets(dune
__init__
)
__import__('pkg_resources').declare_namespace(__name__)
add_python_targets(vtk
__init__
)
import hashlib
from dune.generator.generator import SimpleGenerator
generator = SimpleGenerator("VtkWriter", "Dune::Vtk")
def load(includes, typeName, *args):
includes += ["dune/python/vtk/writer.hh"]
moduleName = "vtkwriter_" + hashlib.md5(typeName.encode('utf-8')).hexdigest()
module = generator.load(includes, typeName, moduleName, *args)
return module
def vtkUnstructuredGridWriter(grid):
"""create a VtkWriter for higher order lagrange elements
Args:
grid: grid view to use for the vtk file
Returns:
vtkWriter: the constructed writer
"""
typeName = "Dune::VtkUnstructuredGridWriter<" + grid._typeName + ">"
includes = grid._includes + ["dune/vtk/writers/vtkunstructuredgridwriter.hh"]
return load(includes, typeName).VtkWriter(grid)
from setuptools import setup, find_packages
setup(name="${ProjectName}",
namespace_packages=['dune'],
description="${ProjectDescription}",
version="${ProjectVersionString}",
author="${ProjectAuthor}",
author_email="${ProjectMaintainerEmail}",
packages = find_packages(),
zip_safe = 0,
package_data = {'': ['*.so']},
install_requires = "${ProjectPythonRequires}".split(' ')
)
try:
from dune.packagemetadata import metaData
except ImportError:
from packagemetadata import metaData
from skbuild import setup
setup(**metaData()[1])
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