Skip to content

Commit

Permalink
Merge pull request #250 from phonopy/recgrid
Browse files Browse the repository at this point in the history
Isolate recprocal space grid code
  • Loading branch information
atztogo authored Jul 14, 2024
2 parents 46482ce + d252e44 commit 1e02000
Show file tree
Hide file tree
Showing 21 changed files with 1,017 additions and 465 deletions.
65 changes: 53 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.20)

option(BUILD_PHPHCALC_LIB "Option to build phph calculation module" OFF)
option(BUILD_PHONONCALC_LIB "Option to build phonon calculation module" OFF)
option(BUILD_RECGRID_LIB "Option to build reciprocal space grid module" OFF)
option(BUILD_GRIDSYS_LIB "Option to build gridsys module" OFF)
option(PHONO3PY_WITH_Fortran "enable fortran interface" OFF)
option(PHONO3PY_USE_OMP "Option to search OpenMP library" ON)
Expand Down Expand Up @@ -125,27 +126,61 @@ if(BUILD_PHPHCALC_LIB
endif()

# ###################################################################################
# Build phphcalc module #
# Reciprocal space grid library #
# ###################################################################################
if(BUILD_RECGRID_LIB
OR BUILD_PHPHCALC_LIB
OR BUILD_NANOBIND_MODULE)
# Source code
set(SOURCES_PHPHCALC
${PROJECT_SOURCE_DIR}/c/bzgrid.c ${PROJECT_SOURCE_DIR}/c/grgrid.c
${PROJECT_SOURCE_DIR}/c/lagrid.c ${PROJECT_SOURCE_DIR}/c/snf3x3.c
${PROJECT_SOURCE_DIR}/c/recgrid.c)

if(BUILD_SHARED_LIBS)
# Shared library
add_library(recgrid_lib SHARED ${SOURCES_PHPHCALC})
else()
# Static link library
add_library(recgrid_lib STATIC ${SOURCES_PHPHCALC})
endif()

if(NOT BUILD_NANOBIND_MODULE)
if(BUILD_SHARED_LIBS)
set_property(TARGET recgrid_lib PROPERTY VERSION ${SERIAL})
set_property(TARGET recgrid_lib PROPERTY SOVERSION ${SOSERIAL})
install(TARGETS recgrid_lib LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
else()
set_property(TARGET recgrid_lib PROPERTY VERSION ${SERIAL})
set_property(TARGET recgrid_lib PROPERTY SOVERSION ${SOSERIAL})
set_property(TARGET recgrid_lib PROPERTY OUTPUT_NAME recgrid_lib)
install(TARGETS recgrid_lib ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

# Header file
install(FILES ${PROJECT_SOURCE_DIR}/c/recgrid.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()
endif()

# ###################################################################################
# Ph-ph calculation library #
# ###################################################################################
if(BUILD_PHPHCALC_LIB OR BUILD_NANOBIND_MODULE)
# Source code
set(SOURCES_PHPHCALC
${PROJECT_SOURCE_DIR}/c/bzgrid.c
${PROJECT_SOURCE_DIR}/c/collision_matrix.c
${PROJECT_SOURCE_DIR}/c/fc3.c
${PROJECT_SOURCE_DIR}/c/grgrid.c
${PROJECT_SOURCE_DIR}/c/imag_self_energy_with_g.c
${PROJECT_SOURCE_DIR}/c/interaction.c
${PROJECT_SOURCE_DIR}/c/isotope.c
${PROJECT_SOURCE_DIR}/c/lagrid.c
${PROJECT_SOURCE_DIR}/c/lapack_wrapper.c
${PROJECT_SOURCE_DIR}/c/phono3py.c
${PROJECT_SOURCE_DIR}/c/phonoc_utils.c
${PROJECT_SOURCE_DIR}/c/pp_collision.c
${PROJECT_SOURCE_DIR}/c/real_self_energy.c
${PROJECT_SOURCE_DIR}/c/real_to_reciprocal.c
${PROJECT_SOURCE_DIR}/c/reciprocal_to_normal.c
${PROJECT_SOURCE_DIR}/c/snf3x3.c
${PROJECT_SOURCE_DIR}/c/tetrahedron_method.c
${PROJECT_SOURCE_DIR}/c/triplet.c
${PROJECT_SOURCE_DIR}/c/triplet_grid.c
Expand All @@ -156,10 +191,12 @@ if(BUILD_PHPHCALC_LIB OR BUILD_NANOBIND_MODULE)
add_library(phphcalc_lib SHARED ${SOURCES_PHPHCALC})

if(OpenMP_FOUND)
target_link_libraries(phphcalc_lib PRIVATE BLAS::BLAS LAPACK::LAPACK
OpenMP::OpenMP_C)
target_link_libraries(
phphcalc_lib PRIVATE recgrid_lib BLAS::BLAS LAPACK::LAPACK
OpenMP::OpenMP_C)
else()
target_link_libraries(phphcalc_lib PRIVATE BLAS::BLAS LAPACK::LAPACK)
target_link_libraries(phphcalc_lib PRIVATE recgrid_lib BLAS::BLAS
LAPACK::LAPACK)
endif()

target_include_directories(phphcalc_lib PRIVATE ${MY_INCLUDES})
Expand Down Expand Up @@ -188,10 +225,10 @@ if(BUILD_PHPHCALC_LIB OR BUILD_NANOBIND_MODULE)
add_library(phphcalc_lib STATIC ${SOURCES_PHPHCALC})

if(OpenMP_FOUND)
target_link_libraries(phphcalc_lib BLAS::BLAS LAPACK::LAPACK
target_link_libraries(phphcalc_lib recgrid_lib BLAS::BLAS LAPACK::LAPACK
OpenMP::OpenMP_C)
else()
target_link_libraries(phphcalc_lib BLAS::BLAS LAPACK::LAPACK)
target_link_libraries(phphcalc_lib recgrid_lib BLAS::BLAS LAPACK::LAPACK)
endif()

target_include_directories(phphcalc_lib PRIVATE ${MY_INCLUDES})
Expand Down Expand Up @@ -236,7 +273,7 @@ if(BUILD_PHPHCALC_LIB OR BUILD_NANOBIND_MODULE)
endif()

# ###################################################################################
# phononcalc #
# Phonon calculation library #
# ###################################################################################
if(BUILD_PHONONCALC_LIB OR BUILD_NANOBIND_MODULE)
# Source code
Expand Down Expand Up @@ -311,7 +348,7 @@ if(BUILD_PHONONCALC_LIB OR BUILD_NANOBIND_MODULE)
endif()

# ###################################################################################
# grid #
# gridsys #
# ###################################################################################
if(BUILD_GRIDSYS_LIB)
# Source code
Expand Down Expand Up @@ -388,11 +425,15 @@ if(BUILD_NANOBIND_MODULE)
${PROJECT_SOURCE_DIR}/c/_phono3py.cpp)
nanobind_add_module(_phononcalc STABLE_ABI ${PROJECT_SOURCE_DIR}/c/phononcalc.h
${PROJECT_SOURCE_DIR}/c/_phononcalc.cpp)
nanobind_add_module(_recgrid STABLE_ABI ${PROJECT_SOURCE_DIR}/c/recgrid.h
${PROJECT_SOURCE_DIR}/c/_recgrid.cpp)

target_link_libraries(_phono3py PRIVATE phphcalc_lib)
target_link_libraries(_phononcalc PRIVATE phononcalc_lib)
target_link_libraries(_recgrid PRIVATE recgrid_lib)

target_compile_definitions(_phono3py PRIVATE THM_EPSILON=1e-10)
install(TARGETS _phono3py LIBRARY DESTINATION ${SKBUILD_PROJECT_NAME})
install(TARGETS _phononcalc LIBRARY DESTINATION ${SKBUILD_PROJECT_NAME})
install(TARGETS _recgrid LIBRARY DESTINATION ${SKBUILD_PROJECT_NAME})
endif()
8 changes: 0 additions & 8 deletions c/_phono3py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1179,14 +1179,6 @@ NB_MODULE(_phono3py, m) {
m.def("triplets_integration_weights", &py_get_triplets_integration_weights);
m.def("triplets_integration_weights_with_sigma",
&py_get_triplets_integration_weights_with_sigma);
m.def("grid_index_from_address", &py_get_grid_index_from_address);
m.def("ir_grid_map", &py_get_ir_grid_map);
m.def("gr_grid_addresses", &py_get_gr_grid_addresses);
m.def("reciprocal_rotations", &py_get_reciprocal_rotations);
m.def("transform_rotations", &py_transform_rotations);
m.def("snf3x3", &py_get_snf3x3);
m.def("bz_grid_addresses", &py_get_bz_grid_addresses);
m.def("rotate_bz_grid_index", &py_rotate_bz_grid_addresses);
m.def("diagonalize_collision_matrix", &py_diagonalize_collision_matrix);
m.def("pinv_from_eigensolution", &py_pinv_from_eigensolution);
m.def("default_colmat_solver", &py_get_default_colmat_solver);
Expand Down
179 changes: 179 additions & 0 deletions c/_recgrid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>

#include "recgrid.h"

namespace nb = nanobind;

long py_get_grid_index_from_address(nb::ndarray<> py_address,
nb::ndarray<> py_D_diag) {
long *address;
long *D_diag;
long gp;

address = (long *)py_address.data();
D_diag = (long *)py_D_diag.data();

gp = recgrid_get_grid_index_from_address(address, D_diag);

return gp;
}

long py_get_ir_grid_map(nb::ndarray<> py_grid_mapping_table,
nb::ndarray<> py_D_diag, nb::ndarray<> py_is_shift,
nb::ndarray<> py_rotations) {
long *D_diag;
long *is_shift;
long(*rot)[3][3];
long num_rot;

long *grid_mapping_table;
long num_ir;

D_diag = (long *)py_D_diag.data();
is_shift = (long *)py_is_shift.data();
rot = (long(*)[3][3])py_rotations.data();
num_rot = (long)py_rotations.shape(0);
grid_mapping_table = (long *)py_grid_mapping_table.data();

num_ir = recgrid_get_ir_grid_map(grid_mapping_table, rot, num_rot, D_diag,
is_shift);
return num_ir;
}

void py_get_gr_grid_addresses(nb::ndarray<> py_gr_grid_addresses,
nb::ndarray<> py_D_diag) {
long(*gr_grid_addresses)[3];
long *D_diag;

gr_grid_addresses = (long(*)[3])py_gr_grid_addresses.data();
D_diag = (long *)py_D_diag.data();

recgrid_get_all_grid_addresses(gr_grid_addresses, D_diag);
}

long py_get_reciprocal_rotations(nb::ndarray<> py_rec_rotations,
nb::ndarray<> py_rotations,
long is_time_reversal) {
long(*rec_rotations)[3][3];
long(*rotations)[3][3];
long num_rot, num_rec_rot;

rec_rotations = (long(*)[3][3])py_rec_rotations.data();
rotations = (long(*)[3][3])py_rotations.data();
num_rot = (long)py_rotations.shape(0);

num_rec_rot = recgrid_get_reciprocal_point_group(
rec_rotations, rotations, num_rot, is_time_reversal, 1);

return num_rec_rot;
}

bool py_transform_rotations(nb::ndarray<> py_transformed_rotations,
nb::ndarray<> py_rotations, nb::ndarray<> py_D_diag,
nb::ndarray<> py_Q) {
long(*transformed_rotations)[3][3];
long(*rotations)[3][3];
long *D_diag;
long(*Q)[3];
long num_rot, succeeded;

transformed_rotations = (long(*)[3][3])py_transformed_rotations.data();
rotations = (long(*)[3][3])py_rotations.data();
D_diag = (long *)py_D_diag.data();
Q = (long(*)[3])py_Q.data();
num_rot = (long)py_transformed_rotations.shape(0);

succeeded = recgrid_transform_rotations(transformed_rotations, rotations,
num_rot, D_diag, Q);
if (succeeded) {
return true;
} else {
return false;
}
}

bool py_get_snf3x3(nb::ndarray<> py_D_diag, nb::ndarray<> py_P,
nb::ndarray<> py_Q, nb::ndarray<> py_A) {
long *D_diag;
long(*P)[3];
long(*Q)[3];
long(*A)[3];
long succeeded;

D_diag = (long *)py_D_diag.data();
P = (long(*)[3])py_P.data();
Q = (long(*)[3])py_Q.data();
A = (long(*)[3])py_A.data();

succeeded = recgrid_get_snf3x3(D_diag, P, Q, A);
if (succeeded) {
return true;
} else {
return false;
}
}

long py_get_bz_grid_addresses(nb::ndarray<> py_bz_grid_addresses,
nb::ndarray<> py_bz_map, nb::ndarray<> py_bzg2grg,
nb::ndarray<> py_D_diag, nb::ndarray<> py_Q,
nb::ndarray<> py_PS,
nb::ndarray<> py_reciprocal_lattice, long type) {
long(*bz_grid_addresses)[3];
long *bz_map;
long *bzg2grg;
long *D_diag;
long(*Q)[3];
long *PS;
double(*reciprocal_lattice)[3];
long num_total_gp;

bz_grid_addresses = (long(*)[3])py_bz_grid_addresses.data();
bz_map = (long *)py_bz_map.data();
bzg2grg = (long *)py_bzg2grg.data();
D_diag = (long *)py_D_diag.data();
Q = (long(*)[3])py_Q.data();
PS = (long *)py_PS.data();
reciprocal_lattice = (double(*)[3])py_reciprocal_lattice.data();

num_total_gp =
recgrid_get_bz_grid_addresses(bz_grid_addresses, bz_map, bzg2grg,
D_diag, Q, PS, reciprocal_lattice, type);

return num_total_gp;
}

long py_rotate_bz_grid_addresses(long bz_grid_index, nb::ndarray<> py_rotation,
nb::ndarray<> py_bz_grid_addresses,
nb::ndarray<> py_bz_map,
nb::ndarray<> py_D_diag, nb::ndarray<> py_PS,
long type) {
long(*bz_grid_addresses)[3];
long(*rotation)[3];
long *bz_map;
long *D_diag;
long *PS;
long ret_bz_gp;

bz_grid_addresses = (long(*)[3])py_bz_grid_addresses.data();
rotation = (long(*)[3])py_rotation.data();
bz_map = (long *)py_bz_map.data();
D_diag = (long *)py_D_diag.data();
PS = (long *)py_PS.data();

ret_bz_gp = recgrid_rotate_bz_grid_index(
bz_grid_index, rotation, bz_grid_addresses, bz_map, D_diag, PS, type);

return ret_bz_gp;
}

NB_MODULE(_recgrid, m) {
m.def("grid_index_from_address", &py_get_grid_index_from_address);
m.def("ir_grid_map", &py_get_ir_grid_map);
m.def("gr_grid_addresses", &py_get_gr_grid_addresses);
m.def("reciprocal_rotations", &py_get_reciprocal_rotations);
m.def("transform_rotations", &py_transform_rotations);
m.def("snf3x3", &py_get_snf3x3);
m.def("bz_grid_addresses", &py_get_bz_grid_addresses);
m.def("rotate_bz_grid_index", &py_rotate_bz_grid_addresses);
}
Loading

0 comments on commit 1e02000

Please sign in to comment.