Skip to content

Instantly share code, notes, and snippets.

@crystalfp
Created June 6, 2024 07:58
Show Gist options
  • Save crystalfp/d258b1b168dd35b7f7a6b3823c12d1b0 to your computer and use it in GitHub Desktop.
Save crystalfp/d258b1b168dd35b7f7a6b3823c12d1b0 to your computer and use it in GitHub Desktop.
Material for issue 494
cmake_minimum_required(VERSION 3.20...3.29)
project(example_spglib LANGUAGES C)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
find_package(Spglib REQUIRED)
add_executable(example example.c)
target_link_libraries(example PRIVATE Spglib::symspg)
# Windows is weird like that :/
# https://stackoverflow.com/a/73550650
if (CMAKE_IMPORT_LIBRARY_SUFFIX)
add_custom_command(TARGET example POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:example> $<TARGET_FILE_DIR:example>
COMMAND_EXPAND_LISTS
)
endif ()
#include <stdio.h>
#include "spglib.h"
int main(int argc, char* argv[]) {
double lattice[3][3] = {
{7.255212, 0.000000, 0.000000},
{3.627606, 6.283219, 0.000000},
{0.000000, 0.000000, 5.712635},
};
double positions[8][3] = {
{0.171633, 0.171632, 0.250000},
{0.171633, 0.656735, 0.250000},
{0.656734, 0.171632, 0.250000},
{0.343266, 0.828368, 0.750000},
{0.828367, 0.343265, 0.750000},
{0.828367, 0.828368, 0.750000},
{0.666699, 0.666601, 0.250000},
{0.333301, 0.333399, 0.750000},
};
int types[8] = {11, 11, 11, 11, 11, 11, 12, 12};
int num_atom = 8;
double symprec = 1e-2;
int num_primitive_atom = spg_standardize_cell(lattice, positions, types,
num_atom, 1, 0, symprec);
printf("From C-API\n1.0\n");
for(int i=0; i < 3; ++i)
{
printf("%lf %lf %lf\n", lattice[i][0], lattice[i][1], lattice[i][2]);
}
printf(" Na Mg\n 6 2\nDirect\n");
for(int i=0; i < num_primitive_atom; ++i)
{
printf("%lf %lf %lf\n", positions[i][0], positions[i][1], positions[i][2]);
}
}
import spglib
if __name__ == '__main__':
lattice = [[7.255212, 0.000000, 0.000000],
[3.627606, 6.283219, 0.000000],
[0.000000, 0.000000, 5.712635]]
positions = [[0.171633, 0.171632, 0.250000],
[0.171633, 0.656735, 0.250000],
[0.656734, 0.171632, 0.250000],
[0.343266, 0.828368, 0.750000],
[0.828367, 0.343265, 0.750000],
[0.828367, 0.828368, 0.750000],
[0.666699, 0.666601, 0.250000],
[0.333301, 0.333399, 0.750000]]
numbers = [11, 11, 11, 11, 11, 11, 12, 12]
cell = (lattice, positions, numbers)
new_cell, new_frac_positions, new_numbers = spglib.standardize_cell(cell, to_primitive=True, symprec=1e-2)
print("Lattice:")
print(new_cell)
print("\nPositions:")
print(new_frac_positions)
print("\nNumbers:")
print(new_numbers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment