Skip to content

Commit 71b228d

Browse files
committed
Adding python wrapper
1 parent 019126e commit 71b228d

8 files changed

Lines changed: 174 additions & 1 deletion

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ jobs:
4747
cmake -S . -B ${{ github.workspace }}/build \
4848
-DGCOV_PATH=`which x86_64-conda-linux-gnu-gcov` \
4949
-DEXODUSIICPP_LIBRARY_TYPE=SHARED \
50+
-DEXODUSIICPP_WITH_PYTHON=ON \
5051
-DEXODUSIICPP_BUILD_TESTS=YES \
5152
-DEXODUSIICPP_CODE_COVERAGE=YES
5253

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.16)
1+
cmake_minimum_required(VERSION 3.26)
22

33
project(exodusIIcpp
44
VERSION 3.0.0
@@ -24,6 +24,7 @@ set_property(CACHE EXODUSIICPP_LIBRARY_TYPE PROPERTY STRINGS ${LibraryTypeValues
2424
option(EXODUSIICPP_BUILD_TESTS "Build tests" NO)
2525
option(EXODUSIICPP_BUILD_TOOLS "Build tools" YES)
2626
option(EXODUSIICPP_INSTALL "Install the library" ON)
27+
option(EXODUSIICPP_WITH_PYTHON "Build python wrapper" NO)
2728
mark_as_advanced(FORCE EXODUSIICPP_INSTALL)
2829

2930
find_package(fmt 11 REQUIRED)
@@ -59,6 +60,10 @@ if (EXODUSIICPP_BUILD_TOOLS)
5960
add_subdirectory(tools)
6061
endif()
6162

63+
if (EXODUSIICPP_WITH_PYTHON)
64+
add_subdirectory(python)
65+
endif()
66+
6267
# Tests
6368

6469
if (EXODUSIICPP_BUILD_TESTS)

python/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
2+
3+
configure_file(pyproject.toml.in pyproject.toml)
4+
install(CODE "execute_process(COMMAND ${Python3_EXECUTABLE} -m pip install ${CMAKE_CURRENT_BINARY_DIR})")
5+
6+
add_subdirectory(src)
7+
8+
if (EXODUSIICPP_BUILD_TESTS)
9+
find_program(PYTEST "pytest" REQUIRED)
10+
enable_testing()
11+
add_test(
12+
NAME python-tests
13+
COMMAND ${PYTEST} ${CMAKE_CURRENT_SOURCE_DIR}
14+
)
15+
endif()

python/pyproject.toml.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[build-system]
2+
requires = ["setuptools", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "exodusIIcpp"
7+
version = "${PROJECT_VERSION}"
8+
9+
[tool.setuptools.packages.find]
10+
where = ["src"]

python/src/CMakeLists.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
project(pyexodusIIcpp LANGUAGES C CXX)
2+
3+
find_package(pybind11 2.9 REQUIRED)
4+
5+
pybind11_add_module(pyexodusIIcpp exodusIIcpp.cpp)
6+
7+
set_target_properties(pyexodusIIcpp PROPERTIES OUTPUT_NAME exodusIIcpp)
8+
9+
target_include_directories(
10+
pyexodusIIcpp
11+
PRIVATE
12+
${CMAKE_BINARY_DIR}
13+
${CMAKE_SOURCE_DIR}/include
14+
${PROJECT_BINARY_DIR}
15+
)
16+
17+
target_link_libraries(
18+
pyexodusIIcpp
19+
PUBLIC
20+
exodusIIcpp
21+
)
22+
23+
configure_file(version.h.in version.h)
24+
25+
set(PYTHON_SITE lib/python${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}/site-packages)
26+
27+
install(
28+
TARGETS pyexodusIIcpp
29+
COMPONENT python
30+
LIBRARY DESTINATION ${PYTHON_SITE}/exodusIIcpp
31+
)
32+
33+
install(
34+
FILES
35+
${CMAKE_CURRENT_SOURCE_DIR}/exodusIIcpp/__init__.py
36+
DESTINATION ${PYTHON_SITE}/exodusIIcpp
37+
COMPONENT python
38+
)

python/src/exodusIIcpp.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// SPDX-FileCopyrightText: 2025 David Andrs <andrsd@gmail.com>
2+
// SPDX-License-Identifier: MIT
3+
4+
#include <pybind11/pybind11.h>
5+
#include <pybind11/stl.h>
6+
#include "exodusIIcpp/exodusIIcpp.h"
7+
#include "version.h"
8+
9+
using namespace exodusIIcpp;
10+
11+
namespace py = pybind11;
12+
13+
PYBIND11_MODULE(exodusIIcpp, m)
14+
{
15+
m.doc() = "pybind11 plugin for exodusIIcpp";
16+
py::setattr(m, "version", py::str(EXODUSIICPP_VERSION));
17+
18+
py::class_<exodusIIcpp::File>(m, "File")
19+
.def(py::init())
20+
.def("open", &File::open)
21+
.def("create", &File::create)
22+
.def("is_opened", &File::is_opened)
23+
.def("init", static_cast<void (File::*)()>(&File::init))
24+
.def("init",
25+
static_cast<void (File::*)(const char *, int, int, int, int, int, int)>(&File::init))
26+
//
27+
.def("get_title", &File::get_title)
28+
.def("get_dim", &File::get_dim)
29+
.def("get_num_nodes", &File::get_num_nodes)
30+
.def("get_num_elements", &File::get_num_elements)
31+
.def("get_num_element_blocks", &File::get_num_element_blocks)
32+
.def("get_num_node_sets", &File::get_num_node_sets)
33+
.def("get_num_side_sets", &File::get_num_side_sets)
34+
.def("get_x_coords", &File::get_x_coords)
35+
.def("get_y_coords", &File::get_y_coords)
36+
.def("get_z_coords", &File::get_z_coords)
37+
.def("get_coord_names", &File::get_coord_names)
38+
.def("get_element_block", &File::get_element_block)
39+
.def("get_side_sets", &File::get_side_sets)
40+
.def("get_side_set_node_list", &File::get_side_set_node_list)
41+
.def("get_node_sets", &File::get_node_sets)
42+
.def("get_num_times", &File::get_num_times)
43+
.def("get_times", &File::get_times)
44+
.def("get_nodal_variable_names", &File::get_nodal_variable_names)
45+
.def("get_elemental_variable_names", &File::get_elemental_variable_names)
46+
.def("get_global_variable_names", &File::get_global_variable_names)
47+
.def("get_nodal_variable_values", &File::get_nodal_variable_values)
48+
.def("get_elemental_variable_values", &File::get_elemental_variable_values)
49+
.def("get_global_variable_values",
50+
static_cast<std::vector<double> (File::*)(int) const>(
51+
&File::get_global_variable_values))
52+
// read
53+
.def("read", &File::read)
54+
.def("read_coords", &File::read_coords)
55+
.def("read_coord_names", &File::read_coord_names)
56+
.def("read_elem_map", &File::read_elem_map)
57+
.def("read_blocks", &File::read_blocks)
58+
.def("read_block_names", &File::read_block_names)
59+
.def("read_node_sets", &File::read_node_sets)
60+
.def("read_node_set_names", &File::read_node_set_names)
61+
.def("read_side_sets", &File::read_side_sets)
62+
.def("read_side_set_names", &File::read_side_set_names)
63+
.def("read_times", &File::read_times)
64+
// write
65+
.def("write_coords",
66+
static_cast<void (File::*)(const std::vector<double> &)>(&File::write_coords))
67+
.def("write_coords",
68+
static_cast<void (File::*)(const std::vector<double> &, const std::vector<double> &)>(
69+
&File::write_coords))
70+
.def("write_coords",
71+
static_cast<void (File::*)(const std::vector<double> &,
72+
const std::vector<double> &,
73+
const std::vector<double> &)>(&File::write_coords))
74+
.def("write_coord_names", static_cast<void (File::*)()>(&File::write_coord_names))
75+
.def(
76+
"write_coord_names",
77+
static_cast<void (File::*)(const std::vector<std::string> &)>(&File::write_coord_names))
78+
.def("write_info", &File::write_info)
79+
.def("write_time", &File::write_time)
80+
.def("write_node_set_names", &File::write_node_set_names)
81+
.def("write_node_set", &File::write_node_set)
82+
.def("write_side_set_names", &File::write_side_set_names)
83+
.def("write_side_set", &File::write_side_set)
84+
.def("write_block_names", &File::write_block_names)
85+
.def("write_block", &File::write_block)
86+
.def("write_nodal_var_names", &File::write_nodal_var_names)
87+
.def("write_elem_var_names", &File::write_elem_var_names)
88+
.def("write_global_var_names", &File::write_global_var_names)
89+
.def("write_nodal_var", &File::write_nodal_var)
90+
.def("write_partial_nodal_var", &File::write_partial_nodal_var)
91+
.def("write_partial_elem_var", &File::write_partial_elem_var)
92+
.def("write_global_var", &File::write_global_var)
93+
//
94+
.def("update", &File::update)
95+
.def("close", &File::close);
96+
}

python/src/exodusIIcpp/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .exodusIIcpp import *
2+
3+
__all__ = [
4+
File
5+
]

python/src/version.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#define EXODUSIICPP_VERSION "@CMAKE_PROJECT_VERSION_MAJOR@.@CMAKE_PROJECT_VERSION_MINOR@.0"

0 commit comments

Comments
 (0)