Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Restructured Python and created pydriver extension module
Browse files Browse the repository at this point in the history
The pydriver extension module will (eventually) allow us to run the swiftest fortran driver from a module instead of via a subprocess call to the CLI program.
  • Loading branch information
daminton committed Aug 7, 2023
1 parent b7fddf4 commit 10b098c
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 68 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ SET(LIB ${CMAKE_SOURCE_DIR}/lib)
SET(BIN ${CMAKE_SOURCE_DIR}/bin)
SET(MOD ${CMAKE_SOURCE_DIR}/include)
#SET(TEST ${CMAKE_SOURCE_DIR}/test)
SET(PY ${CMAKE_SOURCE_DIR}/python/swiftest)
SET(PY ${CMAKE_SOURCE_DIR}/src/python)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIB})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIB})
Expand All @@ -93,8 +93,9 @@ FUNCTION(REPLACE_VERSION IN_FILE LANGUAGE)
STRING(FIND "${LINE}" "version=" LINE_HAS_VER)
IF (LINE_HAS_VER GREATER_EQUAL 0) # This is the version line
FILE(APPEND ${IN_FILE} " version='${CMAKE_PROJECT_VERSION}',\n")
ELSE ()
FILE(APPEND ${IN_FILE} "${LINE}\n") # No match. Put this line back like we found it
ELSE () # No match. Put this line back like we found it
STRING(REPLACE ";" "\n" LINE "${LINE}") # Fixes an issue when parsing the list that defines the pydriver extension module
FILE(APPEND ${IN_FILE} "${LINE}\n")
ENDIF ()
ENDIF ()
ENDFOREACH ()
Expand Down
21 changes: 19 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ RUN cd netcdf-fortran-4.6.1 && \
make install

FROM intel/oneapi-hpckit:2023.1.0-devel-ubuntu20.04 as build_driver
SHELL ["/bin/bash", "--login", "-c"]
COPY --from=build_deps /usr/local/. /usr/local/
ENV INSTALL_DIR="/usr/local"
ENV CC="${ONEAPI_ROOT}/compiler/latest/linux/bin/icx"
Expand Down Expand Up @@ -106,20 +107,36 @@ ENV FC="${ONEAPI_ROOT}/mpi/latest/bin/mpiifort"
ENV FFLAGS="-fPIC -standard-semantics"
ENV LDFLAGS="-L/usr/local/lib"
ENV LIBS="-lhdf5_hl -lhdf5 -lz"
ENV PATH /root/miniconda3/bin:$PATH

COPY ./cmake/ /swiftest/cmake/
COPY ./src/ /swiftest/src/
COPY ./CMakeLists.txt /swiftest/
COPY ./python/ /swiftest/python/
COPY ./version.txt /swiftest/
RUN cd swiftest && \
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-py311_23.5.2-0-Linux-x86_64.sh && \
/usr/bin/bash Miniconda3-py311_23.5.2-0-Linux-x86_64.sh -b && \
/root/miniconda3/bin/conda init bash && \
source /root/.bashrc && conda update --all -y && \
conda install conda-libmamba-solver -y && \
conda config --set solver libmamba && \
conda install -c conda-forge numpy -y&& \
conda install -c anaconda cython -y && \
cd swiftest && \
cmake -S . -B build -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
-DMACHINE_CODE_VALUE=${MACHINE_CODE_VALUE} \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DUSE_COARRAY=OFF \
-DBUILD_SHARED_LIBS=OFF \
${EXTRA_CMAKE_OPTIONS} && \
cmake --build build && \
cmake --install build
cmake --install build && \
mv bin/CMakeFiles/swiftest.dir/__/python/swiftest/f2py/driver.f90.o python/swiftest/f2py/ && \
mv bin/CMakeFiles/swiftest.dir/__/python/swiftest/f2py/pydriver.f90.o python/swiftest/f2py/

RUN cd swiftest/python/swiftest/f2py && \
python setup.py build_ext --inplace


# This build target creates a container that executes just the driver program
FROM ubuntu:20.04 as driver
Expand Down
24 changes: 0 additions & 24 deletions python/swiftest/f2py/pydriver.f90

This file was deleted.

16 changes: 0 additions & 16 deletions python/swiftest/requirements.txt

This file was deleted.

17 changes: 0 additions & 17 deletions python/swiftest/setup.py

This file was deleted.

17 changes: 11 additions & 6 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ SET(STRICT_MATH_FILES
${SRC}/whm/whm_gr.f90
${SRC}/whm/whm_kick.f90
${SRC}/whm/whm_step.f90
${PY}/pydriver/src/driver.f90
${PY}/pydriver/src/pydriver.f90
)

SET(FAST_MATH_FILES
Expand Down Expand Up @@ -81,7 +83,6 @@ SET(FAST_MATH_FILES
${SRC}/symba/symba_util.f90
${SRC}/walltime/walltime_implementations.f90
${SRC}/whm/whm_util.f90

)

SET(COARRAY_FILES
Expand Down Expand Up @@ -131,7 +132,6 @@ IF(USE_COARRAY)
ENDIF(USE_COARRAY)



#Set strict vs fast math flags
STRING(TOUPPER "${CMAKE_BUILD_TYPE}" BT)
IF(BT STREQUAL "RELEASE" OR BT STREQUAL "PROFILE")
Expand Down Expand Up @@ -176,9 +176,14 @@ ELSE()
SET(CMAKE_INSTALL_PREFIX /usr/local)
ENDIF(WIN32)
INSTALL(TARGETS ${SWIFTEST_DRIVER} ${SWIFTEST_LIBRARY}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
INCLUDES DESTINATION include
)
INSTALL(FILES ${MOD}/swiftest.mod DESTINATION include)

INSTALL(TARGETS ${SWIFTEST_LIBRARY}
LIBRARY DESTINATION "${PY}/pydriver/lib"
ARCHIVE DESTINATION "${PY}/pydriver/lib"
INCLUDES DESTINATION "${PY}pydriver/include"
)
26 changes: 26 additions & 0 deletions src/python/pydriver/src/driver.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module pydriver_module
use swiftest, only : swiftest_driver
implicit none

contains
subroutine driver(integrator, param_file_name, display_style)

!! author: David A. Minton
!!
!! Driver program for the Swiftest integrators. Unlike the earlier Swift and Swifter drivers, in Swiftest all integrators
!! are run from this single program.
!!
!! Adapted from Swifter by David E. Kaufmann's Swifter driver programs swifter_[bs,helio,ra15,rmvs,symba,tu4,whm].f90
!! Adapted from Hal Levison and Martin Duncan's Swift driver programs
implicit none

! Arguments
character(len=:), intent(in), allocatable :: integrator !! Symbolic code of the requested integrator
character(len=:), intent(in), allocatable :: param_file_name !! Name of the input parameters file
character(len=:), intent(in), allocatable :: display_style !! Style of the output display {"STANDARD", "COMPACT", "PROGRESS"}). Default is "STANDARD")

call swiftest_driver(integrator, param_file_name, display_style)

return
end subroutine driver
end module pydriver_module
9 changes: 9 additions & 0 deletions src/python/pydriver/src/pydriver.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module pydriver_interface
use iso_c_binding, only : c_char
use pydriver_module, only : driver
implicit none
contains
subroutine c_driver(integrator, param_file_name, display_style) bind(c)
character(kind=c_char) :: integrator(:), param_file_name(:), display_style(:)
end subroutine c_driver
end module pydriver_interface
35 changes: 35 additions & 0 deletions src/python/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Copyright 2022 - David Minton, Carlisle Wishard, Jennifer Pouplin, Jake Elliott, & Dana Singh
This file is part of Swiftest.
Swiftest is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Swiftest is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Swiftest.
If not, see: https://www.gnu.org/licenses.
"""

from setuptools import setup, find_packages, Extension
from Cython.Build import cythonize
import os

# Build the pydriver extension that allows us to run the Fortran driver as a Python module.
root_dir = 'pydriver'
include_dir = os.path.join(root_dir,'include')
lib_dir = os.path.join(root_dir,'lib')
pydriver_extension = [Extension('swiftest.pydriver',
[os.path.join(root_dir,'pydriver.pyx')],
extra_compile_args=['-fPIC', '-O3'],
library_dirs=[lib_dir],
libraries=['swiftest','netcdff','netcdf','hdf5_hl','hdf5','m','z'],
include_dirs=[include_dir],
)]

setup(name='swiftest',
version='2023.08.00',
author='David A. Minton',
author_email='daminton@purdue.edu',
url='https://github.itap.purdue.edu/MintonGroup/swiftest',
ext_modules = cythonize(pydriver_extension),
packages=find_packages())
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 10b098c

Please sign in to comment.