Skip to content

Commit 439e321

Browse files
authored
Merge branch 'python_version' into read_cpp_files
2 parents 46684a2 + 4be816e commit 439e321

7 files changed

Lines changed: 175 additions & 4 deletions

File tree

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,16 @@
3131
*.out
3232
*.app
3333

34+
3435
# Compiled Python
3536
*.pyc
37+
38+
# Build path
39+
build/
40+
dist/
41+
*.egg-info/
42+
43+
# Python
44+
__pycache__/
45+
*.py[cod]
46+

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "pybind11"]
2+
path = pybind11
3+
url = https://github.com/pybind/pybind11

CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
2+
project(parametric_plasma_source)
3+
4+
set(CMAKE_VERBOSE_MAKEFILE OFF)
5+
6+
set(SRC_DIR parametric_plasma_source)
7+
8+
if(NOT CMAKE_BUILD_TYPE)
9+
set(CMAKE_BUILD_TYPE Release)
10+
endif()
11+
12+
set(CMAKE_CXX_FLAGS "-Wall")
13+
set(CMAKE_CXX_FLAGS_DEBUG "-g")
14+
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
15+
16+
# Use output paths from OpenMC install - change if needed
17+
set(OPENMC_DIR /opt/openmc)
18+
set(OPENMC_INC_DIR ${OPENMC_DIR}/include)
19+
set(OPENMC_LIB_DIR ${OPENMC_DIR}/lib)
20+
21+
# Build source_sampling
22+
list(APPEND source_sampling_SOURCES
23+
${SRC_DIR}/source_sampling.cpp
24+
${SRC_DIR}/plasma_source.cpp
25+
)
26+
27+
add_library(source_sampling SHARED ${source_sampling_SOURCES})
28+
29+
find_library(OPENMC_LIB openmc HINTS ${OPENMC_LIB_DIR})
30+
31+
set_target_properties(source_sampling PROPERTIES PREFIX "")
32+
set_target_properties(source_sampling PROPERTIES POSITION_INDEPENDENT_CODE ON)
33+
target_include_directories(source_sampling PUBLIC ${OPENMC_INC_DIR})
34+
target_include_directories(source_sampling PUBLIC ${OPENMC_DIR}/vendor/pugixml)
35+
target_link_libraries(source_sampling ${OPENMC_LIB} gfortran)
36+
37+
# Build plasma_source Python bindings
38+
list(APPEND plasma_source_pybind_SOURCES
39+
${SRC_DIR}/plasma_source.cpp
40+
${SRC_DIR}/plasma_source_pybind.cpp
41+
)
42+
43+
add_subdirectory(pybind11)
44+
45+
pybind11_add_module(plasma_source ${plasma_source_pybind_SOURCES})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .plasma import Plasma
2+
from .plasma_source import PlasmaSource
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <pybind11/pybind11.h>
2+
#include "plasma_source.hpp"
3+
4+
namespace py = pybind11;
5+
namespace ps = plasma_source;
6+
7+
PYBIND11_MODULE(plasma_source, m) {
8+
m.doc() = "A parametric plasma source";
9+
10+
py::class_<ps::PlasmaSource>(m, "PlasmaSource")
11+
.def(py::init<const double &, const double &, const double &, const double &,
12+
const double &, const double &, const double &, const double &,
13+
const double &, const double &, const double &, const double &,
14+
const double &, const double &, const std::string &, const int &,
15+
const int &, const double &, const double &>(),
16+
py::arg("ion_density_pedistal")=1.09e20,
17+
py::arg("ion_density_seperatrix")=3e19,
18+
py::arg("ion_density_origin")=1.09e20,
19+
py::arg("ion_temperature_pedistal")=6.09,
20+
py::arg("ion_temperature_seperatrix")=0.1,
21+
py::arg("ion_temperature_origin")=45.9,
22+
py::arg("pedistal_radius")=0.8,
23+
py::arg("ion_density_peaking_factor")=1.0,
24+
py::arg("ion_temperature_peaking_factor")=8.06,
25+
py::arg("minor_radius")=1.5,
26+
py::arg("major_radius")=4.5,
27+
py::arg("elongation")=2.0,
28+
py::arg("triangularity")=0.55,
29+
py::arg("shafranov_shift")=0.0,
30+
py::arg("plasma_type")="plasma",
31+
py::arg("plasma_id")=1,
32+
py::arg("number_of_bins")=100,
33+
py::arg("min_toroidal_angle") = 0.0,
34+
py::arg("max_toridal_angle") = 360.0)
35+
.def("ion_density",
36+
&ps::PlasmaSource::ion_density,
37+
"Calculate the ion density at a specific minor radius",
38+
py::arg("minor_radius"))
39+
.def("ion_temperature",
40+
&ps::PlasmaSource::ion_temperature,
41+
"calculate the ion temperature at a specific minor radius",
42+
py::arg("minor_radius"))
43+
.def("dt_xs",
44+
&ps::PlasmaSource::dt_xs,
45+
"determine the value of the dt xs cross sections at a specific ion temperature",
46+
py::arg("ion_temperature"));
47+
}

pybind11

Submodule pybind11 added at d54d6d8

setup.py

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,69 @@
1-
import setuptools
1+
import os
2+
import subprocess
3+
import sys
4+
5+
from setuptools import setup, find_packages, Extension
6+
from setuptools.command.build_ext import build_ext
7+
8+
9+
class CMakeExtention(Extension):
10+
def __init__(self, name, sourcedir=""):
11+
Extension.__init__(self, name, sources=[])
12+
self.sourcedir = os.path.abspath(sourcedir)
13+
14+
15+
class CMakeBuild(build_ext):
16+
def run(self):
17+
try:
18+
subprocess.check_output(["cmake", "--version"])
19+
except OSError:
20+
raise RuntimeError("CMake must be installed to build the "
21+
"following extentions: "
22+
", ".join(e.name for e in self.extensions))
23+
24+
for ext in self.extensions:
25+
self.build_extension(ext)
26+
27+
def build_extension(self, ext):
28+
extdir = os.path.abspath(
29+
os.path.dirname(self.get_ext_fullpath(ext.name))
30+
)
31+
if not extdir.endswith(os.path.sep):
32+
extdir += os.path.sep
33+
34+
cmake_args = ["-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir,
35+
"-DPYTHON_EXECUTABLE=" + sys.executable]
36+
37+
cfg = "Debug" if self.debug else "Release"
38+
build_args = ["--config", cfg]
39+
40+
cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg]
41+
build_args += ["--", "-j2"]
42+
build_args += ["plasma_source"]
43+
44+
env = os.environ.copy()
45+
env["CXXFLAGS"] = "{} -DVERSION_INFO=\\\"{}\\\"".format(
46+
env.get("CXXFLAGS", ""),
47+
self.distribution.get_version()
48+
)
49+
50+
if not os.path.exists(self.build_temp):
51+
os.makedirs(self.build_temp)
52+
subprocess.check_call(
53+
["cmake", ext.sourcedir] + cmake_args,
54+
cwd=self.build_temp,
55+
env=env
56+
)
57+
subprocess.check_call(
58+
["cmake", "--build", "."] + build_args,
59+
cwd=self.build_temp
60+
)
61+
262

363
with open("README.md", "r") as fh:
464
long_description = fh.read()
565

6-
setuptools.setup(
66+
setup(
767
name="parametric_plasma_source",
868
version="0.0.6",
969
author="Jonathan Shimwell",
@@ -12,10 +72,12 @@
1272
long_description=long_description,
1373
long_description_content_type="text/markdown",
1474
url="https://github.com/shimwell/parametric_plasma_source",
15-
packages=setuptools.find_packages(),
75+
packages=find_packages(),
76+
ext_modules=[CMakeExtention("parametric_plasma_source/plasma_source")],
77+
cmdclass=dict(build_ext=CMakeBuild),
1678
classifiers=[
1779
"Programming Language :: Python :: 3",
1880
"License :: OSI Approved :: MIT License",
1981
"Operating System :: OS Independent",
2082
],
21-
)
83+
)

0 commit comments

Comments
 (0)