Skip to content
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

Minor simplification of trim_cell #347

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 30 additions & 21 deletions src/cell.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ static void set_positions_and_tensors(Cell *trimmed_cell,
const int *mapping_table,
const int *overlap_table);
static VecDBL *translate_atoms_in_trimmed_lattice(const Cell *cell,
const double prim_lat[3][3]);
const int tmat_p_i[3][3]);
static int *get_overlap_table(const VecDBL *position, const int cell_size,
const int *cell_types, const Cell *trimmed_cell,
const double symprec);

// @brief Allocate Cell. NULL is returned if failed
// @param size number of atoms
// @param tensor_rank rank of site tensors for magnetic symmetry. Set -1 if not
// used.
// @param tensor_rank rank of site tensors for magnetic symmetry. Set -1 if
// not used.
Comment on lines +64 to +65
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My line length setting is considered different from the original code. Do we have a formatter to make it same?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you using clang-format by default? I'm not sure on VSCode, but on CLion there is an option to use clang-format instead of the built-in

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your info. @LecrisUT. I use VSCode, and my current setting is like below,

    "C_Cpp.clang_format_style": "{BasedOnStyle: Google, IndentWidth: 4}",

I found some information, https://code.visualstudio.com/docs/cpp/cpp-ide#_code-formatting . I should read this in detail.

Cell *cel_alloc_cell(const int size, const SiteTensorType tensor_rank) {
Cell *cell;

Expand Down Expand Up @@ -131,7 +131,8 @@ void cel_free_cell(Cell *cell) {
cell->types = NULL;
}
if ((cell->tensor_rank != NOSPIN) && (cell->tensors != NULL)) {
// When cell->tensor_rank==NOSPIN, cell->tensors is already NULL.
// When cell->tensor_rank==NOSPIN, cell->tensors is already
// NULL.
free(cell->tensors);
cell->tensors = NULL;
}
Expand Down Expand Up @@ -325,12 +326,14 @@ int cel_layer_any_overlap_with_same_type(const Cell *cell,
return 0;
}

/// @param[out] mapping_table array (cell->size, ), maps atom-`i` in cell to
/// non-overlapped atom-`mapping_table[i]`
/// @param[in] trimmed_lattice
/// @param[in] cell
/// @param[in] symprec
/// @return trimmed_cell
/// @param[out] mapping_table array (`cell->size`, ), maps atom-`i` in `cell` to
/// atom-`j` in smaller cell to be returned.
/// @param[in] trimmed_lattice basis vectors of smaller cell which are
/// commensure with basis vectors of larger cell.
/// @param[in] cell larger cell from which smaller cell having basis vectors of
/// `trimmed_lattice` is created.
/// @param[in] symprec tolerance parameter.
/// @return smaller cell having basis vectors of `trimmed_lattice`.
Cell *cel_trim_cell(int *mapping_table, const double trimmed_lattice[3][3],
const Cell *cell, const double symprec) {
return trim_cell(mapping_table, trimmed_lattice, cell, symprec);
Expand Down Expand Up @@ -378,8 +381,8 @@ static Cell *trim_cell(int *mapping_table, const double trimmed_lattice[3][3],
goto err;
}

if ((position = translate_atoms_in_trimmed_lattice(
cell, trimmed_lattice)) == NULL) {
if ((position = translate_atoms_in_trimmed_lattice(cell, tmp_mat_int)) ==
NULL) {
warning_print("spglib: translate_atoms_in_trimmed_lattice failed.\n");
warning_print(" (line %d, %s).\n", __LINE__, __FILE__);
cel_free_cell(trimmed_cell);
Expand Down Expand Up @@ -502,10 +505,9 @@ static void set_positions_and_tensors(Cell *trimmed_cell,
}

/* Return NULL if failed */
static VecDBL *translate_atoms_in_trimmed_lattice(
const Cell *cell, const double trimmed_lattice[3][3]) {
static VecDBL *translate_atoms_in_trimmed_lattice(const Cell *cell,
const int tmat_p_i[3][3]) {
int i, j;
double tmp_matrix[3][3], axis_inv[3][3];
VecDBL *position;

position = NULL;
Expand All @@ -514,13 +516,10 @@ static VecDBL *translate_atoms_in_trimmed_lattice(
return NULL;
}

mat_inverse_matrix_d3(tmp_matrix, trimmed_lattice, 0);
mat_multiply_matrix_d3(axis_inv, tmp_matrix, cell->lattice);

/* Send atoms into the trimmed cell */
for (i = 0; i < cell->size; i++) {
mat_multiply_matrix_vector_d3(position->vec[i], axis_inv,
cell->position[i]);
mat_multiply_matrix_vector_id3(position->vec[i], tmat_p_i,
cell->position[i]);
for (j = 0; j < 3; j++) {
if (j != cell->aperiodic_axis) {
position->vec[i][j] = mat_Dmod1(position->vec[i][j]);
Expand All @@ -531,7 +530,17 @@ static VecDBL *translate_atoms_in_trimmed_lattice(
return position;
}

/* Return NULL if failed */
/// @brief Find translationally equivalent atoms in larger cell with respect to
/// lattice of smaller cell.
/// @param position array of positions of atoms in larger cells that are reduced
/// in smaller cells.
/// @param cell_size number of atoms in larger cell.
/// @param cell_types yypes of atoms in larger cell.
/// @param trimmed_cell basis vectors of smaller cell.
/// @param symprec tolerance parameter.
/// @return array (cell_size, ) If `position[i]` and `position[j]` with `i` <=
/// `j` are equivalent, `overlap_table[i]` = `overlap_table[j]` = `i`. Return
/// NULL if failed.
static int *get_overlap_table(const VecDBL *position, const int cell_size,
const int *cell_types, const Cell *trimmed_cell,
const double symprec) {
Expand Down