Skip to content

Commit

Permalink
centroid and vector rotations
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogila committed Jan 31, 2025
1 parent fd80c97 commit b82814d
Show file tree
Hide file tree
Showing 37 changed files with 343 additions and 297 deletions.
2 changes: 1 addition & 1 deletion aton/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"""

__version__ = 'v0.0.11'
__version__ = 'v0.0.12'

41 changes: 27 additions & 14 deletions aton/qrotor/rotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def structure_qe(
angle:float,
repeat:bool=False,
precision:int=3,
use_centroid:bool=True,
show_axis:bool=False,
) -> list:
"""Rotates atoms from a Quantum ESPRESSO input file.
Expand All @@ -40,11 +41,16 @@ def structure_qe(
These input positions can be approximate, and are used to identify the target atoms.
The decimal precision in the search for these positions is controlled by `precision`.
It rotates the atoms by the geometrical center of the first 3 atoms by a specific `angle`.
It rotates the atoms by a specific `angle` in degrees.
Additionally, if `repeat = True` it repeats the same rotation over the whole circunference.
Finally, it writes the rotated structure(s) to a new structural file(s).
Returns a list with the output filename(s).
By default, the rotation axis is defined by the perpendicular vector
passing through the geometrical center of the first three points.
To override this and instead use the vector between the first two atoms
as the rotation axis, set `use_centroid = False`.
To debug, `show_axis = True` adds two additional helium atoms as the rotation vector.
"""
print('Rotating Quantum ESPRESSO input structure with QRotor...')
Expand Down Expand Up @@ -76,7 +82,7 @@ def structure_qe(
for angle in angles:
output_name = name + f'_{angle}' + ext
output = os.path.join(path, output_name)
rotated_positions_cartesian = rotate_coords(full_positions, angle, show_axis)
rotated_positions_cartesian = rotate_coords(full_positions, angle, use_centroid, show_axis)
rotated_positions = []
for coord in rotated_positions_cartesian:
pos = interface.qe.from_cartesian(filepath, coord)
Expand All @@ -90,36 +96,43 @@ def structure_qe(
def rotate_coords(
positions:list,
angle:float,
use_centroid:bool=True,
show_axis:bool=False,
) -> list:
"""Rotates geometrical coordinates.
Takes a list of atomic `positions` in cartesian coordinates, as
`[[x1,y1,z1], [x2,y2,z2], [x3,y3,z3]], [etc]`.
Then rotates said coordinates by a given `angle` (degrees),
taking the perpendicular axis that passes through the
geometrical center of the first three points as the axis of rotation.
Any additional coordinates are rotated with the same rotation matrix.
`[[x1,y1,z1], [x2,y2,z2], [x3,y3,z3], [etc]`.
Then rotates said coordinates by a given `angle` in degrees.
Returns a list with the updated positions.
By default, the rotation axis is defined by the perpendicular vector
passing through the geometrical center of the first three points.
To override this and use the vector between the first two atoms
as the rotation axis, set `use_centroid = False`.
If `show_axis = True` it returns two additional coordinates at the end of the list,
with the centroid and the rotation vector.
with the centroid and the rotation vector. Only works with `use_centroid = True`.
"""
if len(positions) < 3:
raise ValueError("At least three coordinates are required to define the rotation axis.")
raise ValueError("At least three atoms must be rotated.")
if not isinstance(positions[0], list):
raise ValueError(f"Atomic positions must have the form: [[x1,y1,z1], [x2,y2,z2], [x3,y3,z3], etc]. Yours were:\n{positions}")
positions = np.array(positions)
#print(f'POSITIONS: {positions}') # DEBUG
# Define the geometrical center of the first three points
center = np.mean(positions[:3], axis=0)
# Define the geometrical center
center_atoms = positions[:2]
if use_centroid:
center_atoms = positions[:3]
center = np.mean(center_atoms, axis=0)
# Ensure the axis passes through the geometrical center
centered_positions = positions - center
# Define the perpendicular axis (normal to the plane formed by the first three points)
v1 = centered_positions[0] - centered_positions[1]
v2 = centered_positions[0] - centered_positions[2]
# Cross product
axis = np.cross(v2, v1)
axis = v1 # Axis defined by the first two points
if use_centroid: # Axis defined by the cross product of the first three points
axis = np.cross(v2, v1)
axis_length = np.linalg.norm(axis)
axis = axis / axis_length
# Create the rotation object using scipy
Expand All @@ -128,7 +141,7 @@ def rotate_coords(
rotated_centered_positions = rotation.apply(centered_positions)
rotated_positions = (rotated_centered_positions + center).tolist()
#print(f'ROTATED_POSITIONS: {rotated_positions}') # DEBUG
if show_axis:
if show_axis and use_centroid:
rotated_positions.append(center.tolist())
rotated_positions.append((center + axis).tolist())
return rotated_positions
Expand Down
2 changes: 1 addition & 1 deletion docs/aton.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ <h2>Submodules</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
4 changes: 2 additions & 2 deletions docs/aton/_version.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down Expand Up @@ -116,7 +116,7 @@ <h1 class="modulename">
</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a>
</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a><span class="sd">&quot;&quot;&quot;</span>
</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a>
</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="n">__version__</span> <span class="o">=</span> <span class="s1">&#39;v0.0.11&#39;</span>
</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="n">__version__</span> <span class="o">=</span> <span class="s1">&#39;v0.0.12&#39;</span>
</span></pre></div>


Expand Down
2 changes: 1 addition & 1 deletion docs/aton/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ <h2>Submodules</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/interface/castep.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/interface/phonopy.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/interface/qe.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/interface/slurm.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/phys.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ <h2>Submodules</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/phys/atoms.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/phys/functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/phys/units.html
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/qrotor.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ <h2>Submodules</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/qrotor/constants.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/qrotor/plot.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/qrotor/potential.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.11 documentation</footer>
<footer>ATON v0.0.12 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
Loading

0 comments on commit b82814d

Please sign in to comment.