-
Notifications
You must be signed in to change notification settings - Fork 29
Python Extension #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Python Extension #71
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
5da23c7
add swig
Luochenghuang 694d915
Add python wrapper for swig generated python API
Luochenghuang 3a98fae
fix bug where when query the material in tree the second time causes …
Luochenghuang 34edeb5
Add Prism and SlatedPrism
Luochenghuang 4b83858
extend the bounding of the group object from float("-inf") to float("…
Luochenghuang 01ecc24
When material_type is NULL, constitutes a None in python.
Luochenghuang 51065c6
clean up the code a little bit
Luochenghuang 84715d7
add jupyter notebook to showcase the interface
Luochenghuang 072f95d
add 1000 sphere case
Luochenghuang 8b1d2dd
Add benchmark
Luochenghuang c9c3134
Modify the build script to include relative directories for header fi…
Luochenghuang 41be3cd
Add new macros for when compiling for swig; get rid of manual include…
Luochenghuang 9076e6a
Add readme, minor edits
Luochenghuang 7919184
Change dir name to python
Luochenghuang ce3b93e
Update the bulid script to add autogen.sh
Luochenghuang 742cf41
Get rid of Guile dependency for when building python extension
Luochenghuang 896274e
update gitignore to exclude python extension generated files
Luochenghuang 9ed3791
Make bounding_box return actual coordinates; Get rid of Material obje…
Luochenghuang 2d5008d
Improve docs, make things more pythonic.
Luochenghuang b3a4743
Add vector3 arrays and enable creation of Prisms and SlantedPrisms
Luochenghuang 3a7cdce
Clean up .i file
Luochenghuang 1c4a78e
Add tests to the ctlgeom module
Luochenghuang 4b35188
Add tests for ObjectGroup
Luochenghuang db2df0a
Add readme for python extension
Luochenghuang bfe89e2
Add loop in c in material_of_numpy_points_in_tree for x10 speed up
Luochenghuang 1ad3961
update notebook
Luochenghuang 1c80504
Add python wrappers for vecterized queries of materials in group.
Luochenghuang cc12467
update readme
Luochenghuang 82c54e8
rename test.ipynb to demo.ipynb
Luochenghuang 6385568
automake-ified python build
stevengj 73b3cbe
Update README.md
Luochenghuang fc85a8a
Merge branch 'python' of https://github.com/NanoComp/libctl
Luochenghuang 2a6395e
Fix imports, delete setup.py
Luochenghuang c51e3fc
Add . into the SUBDIRS varialbes so utils/* gets built when building …
Luochenghuang 8c017f8
Make tweaks to make python installation proper.
Luochenghuang 04fac59
Automatically grab python 3
Luochenghuang 020c2a3
Keep namespace cleaner
Luochenghuang 65b919b
Make python install instruction canonical
Luochenghuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| AM_CPPFLAGS = -I$(top_srcdir)/src \ | ||
| -I$(top_srcdir)/utils \ | ||
| -I$(top_builddir) # for config.h | ||
|
|
||
| BUILT_SOURCES = libctlgeom_wrap.c libctlgeom.py | ||
| EXTRA_DIST = $(BUILT_SOURCES) libctlgeom.i numpy.i | ||
| MAINTAINERCLEANFILES = $(BUILT_SOURCES) | ||
|
|
||
| _libctlgeom_la_SOURCES = libctlgeom_wrap.c | ||
| _libctlgeom_la_LIBADD = $(top_builddir)/utils/libctlgeom.la $(PYTHON_LIBS) | ||
| _libctlgeom_la_LDFLAGS = -module -version-info @SHARED_VERSION_INFO@ | ||
| _libctlgeom_la_CPPFLAGS = $(PYTHON_INCLUDES) $(AM_CPPFLAGS) | ||
|
|
||
| pyctlgeomdir = $(pythondir)/ctlgeom | ||
| pyctlgeom_PYTHON = __init__.py ctlgeom.py libctlgeom.py | ||
| pyctlgeom_LTLIBRARIES = _libctlgeom.la | ||
|
|
||
| ################################################# | ||
| # generated code via swig and guile | ||
| ################################################# | ||
|
|
||
| if MAINTAINER_MODE | ||
|
|
||
| LIBCTL_SWIG_SRC = libctlgeom.i numpy.i | ||
|
|
||
| HPPFILES= \ | ||
| $(top_srcdir)/utils/ctlgeom.h \ | ||
| $(top_srcdir)/src/ctl.h \ | ||
| $(top_srcdir)/utils/ctlgeom-types.h | ||
|
|
||
| libctlgeom_wrap.c: $(LIBCTL_SWIG_SRC) $(HPPFILES) | ||
| $(SWIG) -Wextra $(SWIG_MEEP_FLAGS) $(AM_CPPFLAGS) -outdir $(builddir) -python -o $@ $(srcdir)/libctlgeom.i | ||
|
|
||
| libctlgeom.py: libctlgeom_wrap.c | ||
|
|
||
| endif # MAINTAINER_MODE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| # Python Extension for libctl | ||
|
|
||
| This directory contains SWIG bindings to create a Python extension for libctl. | ||
|
|
||
| ## Prerequisites | ||
| - SWIG | ||
| - Python 3.x | ||
| - A C compiler (gcc, clang, etc.) | ||
|
|
||
| ## Installation (build from source) | ||
|
|
||
| In the top source directory, run the following | ||
| ```bash | ||
| ./configure --enable-maintainer-mode --enable-shared | ||
| make | ||
| make install | ||
| ``` | ||
| Note that you'll need to have `guile` installed to build from source. | ||
|
|
||
| ## Installation (from a release) | ||
|
|
||
| If you are building from an official `.tar.gz` package on the [releases page](https://github.com/NanoComp/libctl/releases), then you can run the following commands: | ||
| ```bash | ||
| tar xvzf libctl-x.x.x.tar.gz | ||
| cd libctl-x.x.x | ||
| ./configure --enable-shared --without-guile | ||
| make | ||
| make install | ||
| ``` | ||
|
|
||
| If you *don't* want to install the python extension, you can configure with `--without-python`. | ||
|
|
||
|
|
||
| ## Usage Examples | ||
|
|
||
| ### Basic Shapes | ||
|
|
||
| ```python | ||
| from ctlgeom import Sphere, Cylinder, Block, Cone, Wedge, Ellipsoid, Prism, SlantedPrism | ||
|
|
||
| # Create a sphere | ||
| sphere = Sphere( | ||
| material="metal", | ||
| center=(0, 0, 0), | ||
| radius=1.0 | ||
| ) | ||
|
|
||
| # Create a cylinder | ||
| cylinder = Cylinder( | ||
| material=1 + 1j, | ||
| center=(0, 0, 0), | ||
| radius=1.0, | ||
| height=2.0, | ||
| axis=(0, 0, 1) | ||
| ) | ||
|
|
||
| # Create a block (rectangular prism) | ||
| block = Block( | ||
| material={"foo" : 42}, | ||
| center=(0, 0, 0), | ||
| e1=(1, 0, 0), # x-axis | ||
| e2=(0, 1, 0), # y-axis | ||
| e3=(0, 0, 1), # z-axis | ||
| size=(2.0, 3.0, 4.0) | ||
| ) | ||
|
|
||
| # Create a cone | ||
| cone = Cone( | ||
| material="glass", | ||
| center=(0, 0, 0), | ||
| radius=1.0, # base radius | ||
| height=2.0, | ||
| axis=(0, 0, 1), | ||
| radius2=0.5 # top radius (0 for perfect cone) | ||
| ) | ||
|
|
||
| # Create a wedge (partial cylinder) | ||
| wedge = Wedge( | ||
| material="plastic", | ||
| center=(0, 0, 0), | ||
| radius=1.0, | ||
| height=2.0, | ||
| axis=(0, 0, 1), | ||
| wedge_angle=1.57, # 90 degrees in radians | ||
| wedge_start=(1, 0, 0) | ||
| ) | ||
|
|
||
| # Create an ellipsoid | ||
| ellipsoid = Ellipsoid( | ||
| material="glass", | ||
| center=(0, 0, 0), | ||
| e1=(1, 0, 0), | ||
| e2=(0, 1, 0), | ||
| e3=(0, 0, 1), | ||
| size=(2.0, 3.0, 4.0) # semi-axes lengths | ||
| ) | ||
|
|
||
| # Create a prism from vertices | ||
| vertices = [ | ||
| (1.0, 0.0, 0.0), | ||
| (0.0, 1.0, 0.0), | ||
| (-1.0, 0.0, 0.0) | ||
| ] | ||
| prism = Prism( | ||
| material="metal", | ||
| vertices=vertices, | ||
| height=2.0, | ||
| axis=(0, 0, 1), | ||
| center=(0, 0, 0) # optional | ||
| ) | ||
|
|
||
| # Create a slanted prism | ||
| slanted_prism = SlantedPrism( | ||
| material="metal", | ||
| vertices=vertices, | ||
| height=2.0, | ||
| axis=(0, 0, 1), | ||
| sidewall_angle=0.523, # 30 degrees in radians | ||
| center=(0, 0, 0) # optional | ||
| ) | ||
| ``` | ||
|
|
||
| ### Working with Shapes | ||
|
|
||
| ```python | ||
| from ctlgeom import point_is_in_object, ObjectGroup | ||
|
|
||
| # Check if a point is inside an object | ||
| point = (0.5, 0.5, 0.5) | ||
| is_inside = point_is_in_object(point, sphere) | ||
|
|
||
| # Get object volume | ||
| volume = sphere.volume() | ||
|
|
||
| # Get object bounding box | ||
| low, high = sphere.bounding_box() | ||
|
|
||
| # Group multiple objects | ||
| group = ObjectGroup([sphere, cylinder, block]) | ||
|
|
||
| # Query material at a point | ||
| material = group.material_at_point(point) | ||
|
|
||
| # Query the material at a numpy array of points. (Must faster than python loops!) | ||
| material = group.material_at_numpy_points(point) | ||
|
|
||
| # Query the material at a cartesian grid | ||
| material = group.material_on_grid(xx, yy, zz) | ||
|
|
||
| # Display debug information | ||
| sphere.debug_info() | ||
| ``` | ||
|
|
||
| ### Vectorized Operations | ||
|
|
||
| ```python | ||
| import numpy as np | ||
| import matplotlib.pyplot as plt | ||
| import time | ||
|
|
||
| sphere1 = Sphere(material=1+3j, center=(0, -0.5, 0), radius=0.5) | ||
| sphere2 = Sphere(material=2+2j, center=(0, 0.5, 0), radius=1) | ||
| block = Block(material=3+1j, center=(0.5, 0.5, 0), e1=(1, 1, 0), e2=(1, -1, 0), e3=(0, 0, 1), size=(1, 1, 1)) | ||
| group = ObjectGroup([sphere2, sphere1, block]) | ||
|
|
||
| # Create a grid of points | ||
| x = np.linspace(-1, 1, 500) | ||
| y = np.linspace(-1, 1, 500) | ||
| xx, yy = np.meshgrid(x, y) | ||
| zz = np.zeros_like(xx) | ||
|
|
||
| start_time = time.time() | ||
| material_map = group.material_on_grid(xx, yy, zz) | ||
| end_time = time.time() | ||
| print(f"Time taken: {end_time - start_time} seconds") | ||
|
|
||
| plt.figure(figsize=(12, 5)) | ||
| plt.subplot(1, 2, 1) | ||
| plt.imshow(material_map.real, cmap='viridis', extent=[-5, 5, -5, 5]) | ||
| plt.colorbar(label='Material') | ||
| plt.title('Real') | ||
| plt.subplot(1, 2, 2) | ||
| plt.imshow(material_map.imag, cmap='viridis', extent=[-5, 5, -5, 5]) | ||
| plt.colorbar(label='Material') | ||
| plt.title('Imaginary') | ||
| plt.show() | ||
| ``` | ||
|
|
||
| ### Running tests | ||
| First, make sure pytest is installed. You can install it by `pip install pytest`. | ||
| ```bash | ||
| cd tests/ | ||
| pytest * -v | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import libctlgeom | ||
| from .ctlgeom import * |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs to be an http to github … I don't think readthedocs will let you link outside the docs directory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's merge this and then you can do a PR to make a proper link once it is merged and appears on github.