# Copyright (C) 2010, Jesper Friis# (see accompanying license files for details)."""Utility tools for atoms/geometry manipulations. - convenient creation of slabs and interfaces ofdifferent orientations. - detection of duplicate atoms / atoms within cutoff radius"""import itertoolsimport numpy as npfrom ase.cell import Cellfrom ase.geometry import complete_cellfrom ase.geometry.minkowski_reduction import minkowski_reducefrom ase.utils import pbc2pbcdef translate_pretty(fractional, pbc): """Translates atoms such that fractional positions are minimized.""" for i in range(3): if not pbc[i]: continue indices = np.argsort(fractional[:, i]) sp = fractional[indices, i] widths = (np.roll(sp, 1) - sp) % 1.0 fractional[:, i] -= sp[np.argmin(widths)] fractional[:, i] %= 1.0 return fractional[docs]def wrap_positions(positions, cell, pbc=True, center=(0.5, 0.5, 0.5), pretty_translation=False, eps=1e-7): """Wrap positions to unit cell. Returns positions changed by a multiple of the unit cell vectors to fit inside the space spanned by these vectors. See also the :meth:`ase.Atoms.wrap` method. Parameters: positions: float ndarray of shape (n, 3) Positions of the atoms cell: float ndarray of shape (3, 3) Unit cell vectors. pbc: one or 3 bool For each axis in the unit cell decides whether the positions will be moved along this axis. center: three float The positons in fractional coordinates that the new positions will be nearest possible to. pretty_translation: bool Translates atoms such that fractional coordinates are minimized. eps: float Small number to prevent slightly negative coordinates from being wrapped. Example: >>> from ase.geometry import wrap_positions >>> wrap_positions([[-0.1, 1.01, -0.5]], ... [[1, 0, 0], [0, 1, 0], [0, 0, 4]], ... pbc=[1, 1, 0]) array([[ 0.9 , 0.01, -0.5 ]]) """ if not hasattr(center, '__len__'): center = (center,) * 3 pbc = pbc2pbc(pbc) shift = np.asarray(center) - 0.5 - eps # Don't change coordinates when pbc is False shift[np.logical_not(pbc)] = 0.0 assert np.asarray(cell)[np.asarray(pbc)].any(axis=1).all(), (cell, pbc) cell = complete_cell(cell) fractional = np.linalg.solve(cell.T, np.asarray(positions).T).T - shift if pretty_translation: fractional = translate_pretty(fractional, pbc) shift = np.asarray(center) - 0.5 shift[np.logical_not(pbc)] = 0.0 fractional += shift else: for i, periodic in enumerate(pbc): if periodic: fractional[:, i] %= 1.0 fractional[:, i] += shift[i] return np.dot(fractional, cell)
[docs]def get_layers(atoms, miller, tolerance=0.001): """Returns two arrays describing which layer each atom belongs to and the distance between the layers and origo. Parameters: miller: 3 integers The Miller indices of the planes. Actually, any direction in reciprocal space works, so if a and b are two float vectors spanning an atomic plane, you can get all layers parallel to this with miller=np.cross(a,b). tolerance: float The maximum distance in Angstrom along the plane normal for counting two atoms as belonging to the same plane. Returns: tags: array of integres Array of layer indices for each atom. levels: array of floats Array of distances in Angstrom from each layer to origo. Example: >>> import numpy as np >>> from ase.spacegroup import crystal >>> from ase.geometry.geometry import get_layers >>> atoms = crystal('Al', [(0,0,0)], spacegroup=225, cellpar=4.05) >>> np.round(atoms.positions, decimals=5) # doctest: +NORMALIZE_WHITESPACE array([[ 0. , 0. , 0. ], [ 0. , 2.025, 2.025], [ 2.025, 0. , 2.025], [ 2.025, 2.025, 0. ]]) >>> get_layers(atoms, (0,0,1)) # doctest: +ELLIPSIS (array([0, 1, 1, 0]...), array([ 0. , 2.025])) """ miller = np.asarray(miller) metric = np.dot(atoms.cell, atoms.cell.T) c = np.linalg.solve(metric.T, miller.T).T miller_norm = np.sqrt(np.dot(c, miller)) d = np.dot(atoms.get_scaled_positions(), miller) / miller_norm keys = np.argsort(d) ikeys = np.argsort(keys) mask = np.concatenate(([True], np.diff(d[keys]) > tolerance)) tags = np.cumsum(mask)[ikeys] if tags.min() == 1: tags -= 1 levels = d[keys][mask] return tags, levels
def naive_find_mic(v, cell): """Finds the minimum-image representation of vector(s) v. Safe to use for (pbc.all() and (norm(v_mic) < 0.5 * min(cell.lengths()))). Can otherwise fail for non-orthorhombic cells. Described in: W. Smith, "The Minimum Image Convention in Non-Cubic MD Cells", 1989, http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.57.1696.""" f = Cell(cell).scaled_positions(v) f -= np.floor(f + 0.5) vmin = f @ cell vlen = np.linalg.norm(vmin, axis=1) return vmin, vlendef general_find_mic(v, cell, pbc=True): """Finds the minimum-image representation of vector(s) v. Using the Minkowski reduction the algorithm is relatively slow but safe for any cell. """ cell = complete_cell(cell) rcell, _ = minkowski_reduce(cell, pbc=pbc) positions = wrap_positions(v, rcell, pbc=pbc, eps=0) # In a Minkowski-reduced cell we only need to test nearest neighbors, # or "Voronoi-relevant" vectors. These are a subset of combinations of # [-1, 0, 1] of the reduced cell vectors. # Define ranges [-1, 0, 1] for periodic directions and [0] for aperiodic # directions. ranges = [np.arange(-1 * p, p + 1) for p in pbc] # Get Voronoi-relevant vectors. # Pre-pend (0, 0, 0) to resolve issue #772 hkls = np.array([(0, 0, 0)] + list(itertools.product(*ranges))) vrvecs = hkls @ rcell # Map positions into neighbouring cells. x = positions + vrvecs[:, None] # Find minimum images lengths = np.linalg.norm(x, axis=2) indices = np.argmin(lengths, axis=0) vmin = x[indices, np.arange(len(positions)), :] vlen = lengths[indices, np.arange(len(positions))] return vmin, vlen[docs]def find_mic(v, cell, pbc=True): """Finds the minimum-image representation of vector(s) v using either one of two find mic algorithms depending on the given cell, v and pbc.""" cell = Cell(cell) pbc = cell.any(1) & pbc2pbc(pbc) dim = np.sum(pbc) v = np.asarray(v) single = v.ndim == 1 v = np.atleast_2d(v) if dim > 0: naive_find_mic_is_safe = False if dim == 3: vmin, vlen = naive_find_mic(v, cell) # naive find mic is safe only for the following condition if (vlen < 0.5 * min(cell.lengths())).all(): naive_find_mic_is_safe = True # hence skip Minkowski reduction if not naive_find_mic_is_safe: vmin, vlen = general_find_mic(v, cell, pbc=pbc) else: vmin = v.copy() vlen = np.linalg.norm(vmin, axis=1) if single: return vmin[0], vlen[0] else: return vmin, vlen
See AlsoGeometry tools — ASE documentationThe Cell object — ASE documentationQuadratic forms, reduction of - Encyclopedia of Mathematics[docs]def conditional_find_mic(vectors, cell, pbc): """Return vectors and their lengths considering cell and pbc. The minimum image convention is applied if cell and pbc are set. This can be used like a simple version of get_distances. """ vectors = np.array(vectors) if (cell is None) != (pbc is None): raise ValueError("cell or pbc must be both set or both be None") if cell is not None: mics = [find_mic(v, cell, pbc) for v in vectors] vectors, vector_lengths = zip(*mics) else: vector_lengths = np.sqrt(np.add.reduce(vectors**2, axis=-1)) return vectors, vector_lengths
[docs]def get_angles(v0, v1, cell=None, pbc=None): """Get angles formed by two lists of vectors. Calculate angle in degrees between vectors v0 and v1 Set a cell and pbc to enable minimum image convention, otherwise angles are taken as-is. """ (v0, v1), (nv0, nv1) = conditional_find_mic([v0, v1], cell, pbc) if (nv0 <= 0).any() or (nv1 <= 0).any(): raise ZeroDivisionError('Undefined angle') v0n = v0 / nv0[:, np.newaxis] v1n = v1 / nv1[:, np.newaxis] # We just normalized the vectors, but in some cases we can get # bad things like 1+2e-16. These we clip away: angles = np.arccos(np.einsum('ij,ij->i', v0n, v1n).clip(-1.0, 1.0)) return np.degrees(angles)
[docs]def get_angles_derivatives(v0, v1, cell=None, pbc=None): """Get derivatives of angles formed by two lists of vectors (v0, v1) w.r.t. Cartesian coordinates in degrees. Set a cell and pbc to enable minimum image convention, otherwise derivatives of angles are taken as-is. There is a singularity in the derivatives for sin(angle) -> 0 for which a ZeroDivisionError is raised. Derivative output format: [[dx_a0, dy_a0, dz_a0], [...], [..., dz_a2]. """ (v0, v1), (nv0, nv1) = conditional_find_mic([v0, v1], cell, pbc) angles = np.radians(get_angles(v0, v1, cell=cell, pbc=pbc)) sin_angles = np.sin(angles) cos_angles = np.cos(angles) if (sin_angles == 0.).any(): # identify singularities raise ZeroDivisionError('Singularity for derivative of a planar angle') product = nv0 * nv1 deriv_d0 = (-(v1 / product[:, np.newaxis] # derivatives by atom 0 - np.einsum('ij,i->ij', v0, cos_angles / nv0**2)) / sin_angles[:, np.newaxis]) deriv_d2 = (-(v0 / product[:, np.newaxis] # derivatives by atom 2 - np.einsum('ij,i->ij', v1, cos_angles / nv1**2)) / sin_angles[:, np.newaxis]) deriv_d1 = -(deriv_d0 + deriv_d2) # derivatives by atom 1 derivs = np.stack((deriv_d0, deriv_d1, deriv_d2), axis=1) return np.degrees(derivs)
[docs]def get_dihedrals(v0, v1, v2, cell=None, pbc=None): """Get dihedral angles formed by three lists of vectors. Calculate dihedral angle (in degrees) between the vectors a0->a1, a1->a2 and a2->a3, written as v0, v1 and v2. Set a cell and pbc to enable minimum image convention, otherwise angles are taken as-is. """ (v0, v1, v2), (_, nv1, _) = conditional_find_mic([v0, v1, v2], cell, pbc) v1n = v1 / nv1[:, np.newaxis] # v, w: projection of v0, v2 onto plane perpendicular to v1 v = -v0 - np.einsum('ij,ij,ik->ik', -v0, v1n, v1n) w = v2 - np.einsum('ij,ij,ik->ik', v2, v1n, v1n) # formula returns 0 for undefined dihedrals; prefer ZeroDivisionError undefined_v = np.all(v == 0.0, axis=1) undefined_w = np.all(w == 0.0, axis=1) if np.any([undefined_v, undefined_w]): raise ZeroDivisionError('Undefined dihedral for planar inner angle') x = np.einsum('ij,ij->i', v, w) y = np.einsum('ij,ij->i', np.cross(v1n, v, axis=1), w) dihedrals = np.arctan2(y, x) # dihedral angle in [-pi, pi] dihedrals[dihedrals < 0.] += 2 * np.pi # dihedral angle in [0, 2*pi] return np.degrees(dihedrals)
[docs]def get_dihedrals_derivatives(v0, v1, v2, cell=None, pbc=None): """Get derivatives of dihedrals formed by three lists of vectors (v0, v1, v2) w.r.t Cartesian coordinates in degrees. Set a cell and pbc to enable minimum image convention, otherwise dihedrals are taken as-is. Derivative output format: [[dx_a0, dy_a0, dz_a0], ..., [..., dz_a3]]. """ (v0, v1, v2), (nv0, nv1, nv2) = conditional_find_mic([v0, v1, v2], cell, pbc) v0n = v0 / nv0[:, np.newaxis] v1n = v1 / nv1[:, np.newaxis] v2n = v2 / nv2[:, np.newaxis] normal_v01 = np.cross(v0n, v1n, axis=1) normal_v12 = np.cross(v1n, v2n, axis=1) cos_psi01 = np.einsum('ij,ij->i', v0n, v1n) # == np.sum(v0 * v1, axis=1) sin_psi01 = np.sin(np.arccos(cos_psi01)) cos_psi12 = np.einsum('ij,ij->i', v1n, v2n) sin_psi12 = np.sin(np.arccos(cos_psi12)) if (sin_psi01 == 0.).any() or (sin_psi12 == 0.).any(): msg = ('Undefined derivative for undefined dihedral with planar inner ' 'angle') raise ZeroDivisionError(msg) deriv_d0 = -normal_v01 / (nv0 * sin_psi01**2)[:, np.newaxis] # by atom 0 deriv_d3 = normal_v12 / (nv2 * sin_psi12**2)[:, np.newaxis] # by atom 3 deriv_d1 = (((nv1 + nv0 * cos_psi01) / nv1)[:, np.newaxis] * -deriv_d0 + (cos_psi12 * nv2 / nv1)[:, np.newaxis] * deriv_d3) # by a1 deriv_d2 = (-((nv1 + nv2 * cos_psi12) / nv1)[:, np.newaxis] * deriv_d3 - (cos_psi01 * nv0 / nv1)[:, np.newaxis] * -deriv_d0) # by a2 derivs = np.stack((deriv_d0, deriv_d1, deriv_d2, deriv_d3), axis=1) return np.degrees(derivs)
[docs]def get_distances(p1, p2=None, cell=None, pbc=None): """Return distance matrix of every position in p1 with every position in p2 If p2 is not set, it is assumed that distances between all positions in p1 are desired. p2 will be set to p1 in this case. Use set cell and pbc to use the minimum image convention. """ p1 = np.atleast_2d(p1) if p2 is None: np1 = len(p1) ind1, ind2 = np.triu_indices(np1, k=1) D = p1[ind2] - p1[ind1] else: p2 = np.atleast_2d(p2) D = (p2[np.newaxis, :, :] - p1[:, np.newaxis, :]).reshape((-1, 3)) (D, ), (D_len, ) = conditional_find_mic([D], cell=cell, pbc=pbc) if p2 is None: Dout = np.zeros((np1, np1, 3)) Dout[(ind1, ind2)] = D Dout -= np.transpose(Dout, axes=(1, 0, 2)) Dout_len = np.zeros((np1, np1)) Dout_len[(ind1, ind2)] = D_len Dout_len += Dout_len.T return Dout, Dout_len # Expand back to matrix indexing D.shape = (-1, len(p2), 3) D_len.shape = (-1, len(p2)) return D, D_len
[docs]def get_distances_derivatives(v0, cell=None, pbc=None): """Get derivatives of distances for all vectors in v0 w.r.t. Cartesian coordinates in Angstrom. Set cell and pbc to use the minimum image convention. There is a singularity for distances -> 0 for which a ZeroDivisionError is raised. Derivative output format: [[dx_a0, dy_a0, dz_a0], [dx_a1, dy_a1, dz_a1]]. """ (v0, ), (dists, ) = conditional_find_mic([v0], cell, pbc) if (dists <= 0.).any(): # identify singularities raise ZeroDivisionError('Singularity for derivative of a zero distance') derivs_d0 = np.einsum('i,ij->ij', -1. / dists, v0) # derivatives by atom 0 derivs_d1 = -derivs_d0 # derivatives by atom 1 derivs = np.stack((derivs_d0, derivs_d1), axis=1) return derivs
[docs]def get_duplicate_atoms(atoms, cutoff=0.1, delete=False): """Get list of duplicate atoms and delete them if requested. Identify all atoms which lie within the cutoff radius of each other. Delete one set of them if delete == True. """ from scipy.spatial.distance import pdist dists = pdist(atoms.get_positions(), 'sqeuclidean') dup = np.nonzero(dists < cutoff**2) rem = np.array(_row_col_from_pdist(len(atoms), dup[0])) if delete: if rem.size != 0: del atoms[rem[:, 0]] else: return rem
def _row_col_from_pdist(dim, i): """Calculate the i,j index in the square matrix for an index in a condensed (triangular) matrix. """ i = np.array(i) b = 1 - 2 * dim x = (np.floor((-b - np.sqrt(b**2 - 8 * i)) / 2)).astype(int) y = (i + x * (b + x + 2) / 2 + 1).astype(int) if i.shape: return list(zip(x, y)) else: return [(x, y)][docs]def permute_axes(atoms, permutation): """Permute axes of unit cell and atom positions. Considers only cell and atomic positions. Other vector quantities such as momenta are not modified.""" assert (np.sort(permutation) == np.arange(3)).all() permuted = atoms.copy() scaled = permuted.get_scaled_positions() permuted.set_cell(permuted.cell.permute_axes(permutation), scale_atoms=False) permuted.set_scaled_positions(scaled[:, permutation]) permuted.set_pbc(permuted.pbc[permutation]) return permuted
ase.geometry.geometry — ASE documentation (2024)
Top Articles
In the Heights (2021) | Rotten Tomatoes
Antimatter Dimensions Nameless Reality
In The Heights: What to know about the Lin-Manuel Miranda film
Latest Posts
BlueStacks Multi-Instance: Play multiple games or same game from multiple accounts
The Best Tropical Smoothie Recipe - Baker by Nature
Recommended Articles
- J-BRAID® x8 GRAND BRAIDED LINE
- Little Bill Thinks Big Not Logging In
- How Many پوست شیر Films Are There
- Stream It Or Skip It: 'Glitter and Greed: The Lisa Frank Story' on Prime Video, a scathing look at the toxic company responsible for all those unicorns and rainbows
- The Barbie For Girls Horseback Riding 2
- The Girl Downstairs Vrv
- Cho Jae-Wan Oscar 2016
- Test du jeu Tales of Symphonia sur NGC
- Proof Of Two Minecraft Skin
- Characters Of Genius Quiz Lol
- This Upcoming George RR Martin Fantasy Film Is a Secret Western 10 Years in the Making
- [OFFICIAL] LocaChange - Change GPS Location on iOS/Android in 1 Click
- Is Choujin Locke Still Ongoing
- Friseur für Bad Salzuflen - Neslihan Hair & Beauty: Perücken, Haare schneiden -
- Is Lino Brocka Single
- Bats: Human Harvest (2007) Movie Download
- Arunkumar Dhanasekaran Boyfriend 2017
- Twim Film Review
- How To Join Friends On Zombies Ate My Neighbors And Ghoul Patrol
- Tf1 Dc
- Code Geass: Lelouch Of The Rebellion Picture Drama - The Night Of The Beginning Opening English Lyrics
- En Proie (2024) Where To Stream
- Stu Cowan: Canadiens need to be a tougher home team at Bell Centre
- Louis Malle (prog) - Louis Malle (prog)
- Azamara Quest 174 nt cruise dep San Francisco 5 Jan 2027 from $0pp
- Rapport de Stage - PDFCOFFEE.COM
- The 100 Best Songs of 2024
- Black Book | MovieWeb
- Peter Kay Live Comedy Genius (2024) Film
- WEDGE · fuzz rock band Berlin ((( ABOUT )))
- Filmografie von Clint Eastwood
- Hiiro No Kakera: The Tamayori Princess Saga Family Guy
- Every Grand Theft Auto soundtrack ranked from worst to best
- If Love Be Blind Ep3
- Review Senyuu.: Alba/Ross Arc - The Hero Of Port City
- Can You Mod Moto-Crash +
- Main Character Names
- 好先生 Ringtone
- Soundtrack From Inumber Number
- Nisshin Seifun Group Cms Episode 98
- When Is Up And Away (2018) Set
- Tenshi No Shippo Chu! Full Movie English Dub Download
- How To Take Care Of A Hamster – [Vet Explains Pets]
- Yuuyame Season 1 Episode 9
- Yuki Kaji Ri Boku
- Essential Nail Care Tools - Tammy Taylor Global Franchising
- Λουκία Ρικάκη Flashing
- ACNP 63rd Annual Meeting: Poster Abstracts P305-P608
- Objections to Sam Harris’s Critic of Religion
- Market Stall Business Name Generator
- Watch The Future Diary English Sub/Dub online Free on HiAnime.to
- MF Ghost 2nd Season Episode 9 Discussion
- Namiuchigiwa No Muromi-San Manga 112
- Grimmway Farms Recalls Organic Whole and Select Organic Baby Carrots That May Be in Consumers’ Homes Due to Potential E. coli Contamination
- The Best Make-Up Brushes For Every Purpose, According To A Pro MUA
- I Failed to Abandon the Villain
- Shikkakumon No Saikyou Kenja: Sekai Saikyou No Kenja Ga Sarani Tsuyokunaru Tame Ni Tensei Shimashita Characyers
- Laut Öko-Test: Das sind die besten Augen-Make-up-Entferner
- Selene H. Vigil Eyes Color
- Jennings Rice Net Worth 2019
- Renae Moneymaker Gi
- Turandot: a guide to Puccini's bloodthirsty final opera, its famous 'Nessun Dorma' aria, and its best recordings
- ✨ Generic ReliOn Premier Blood Glucose Test Strips | Blood Glucose Test Strips | Test Strips for Diabetes | Blood Sugar Test Strips | Diabetic Test Strips | Reli On Premier Test Strips 100 Count, Black — 🛍️ The Retail Market
- Legend Of The Strongest Man Kurosawa Sad Scene
- The Fugitive | Rotten Tomatoes
- Tier List | Solo Leveling: Arise | Prydwen Institute
- Is Rasheed Overrated
- 10 Hairstyles for Women Over 50 who Wear Glasses | Rush Hair & Beauty
- Shinobu Amaama To Inazuma
- Atashin'chi 3D Movie: Jounetsu No Chou Chounouryoku Haha Dai Bousou Height
- Stripping The Flesh: Tooda Oto Short Stories 1080P
- Who Is Laura Lindsay Mother
- Actors Of Love Undercover (2010)
- Sms Racing Update
- What Is The Order Of The After Death (1990)
- Fashioning Little Miss Lonesome [R18] Review
- Zeno.org-Shop. Autoren
- Here’s Why You Should Add An After Sun Lotion To Your Skincare Routine
- When Does The Next Wonder Cat Kyuu-Chan Come Out
- When Does The Next Wonder Cat Kyuu-Chan Come Out
- When Does The Next Wonder Cat Kyuu-Chan Come Out
- When Does The Next Wonder Cat Kyuu-Chan Come Out
- When Does The Next Wonder Cat Kyuu-Chan Come Out
- When Does The Next Wonder Cat Kyuu-Chan Come Out
- When Does The Next Wonder Cat Kyuu-Chan Come Out
- When Does The Next Wonder Cat Kyuu-Chan Come Out
- When Does The Next Wonder Cat Kyuu-Chan Come Out
- When Does The Next Wonder Cat Kyuu-Chan Come Out
- When Does The Next Wonder Cat Kyuu-Chan Come Out
- Remove personally identifiable info or doxxing content from Google Search
- Croatia's Dora 2025: One of these 24 acts will follow Baby Lasagna to Eurovision
- SHOCKWAVE Power Impact Driver Bits and Bit Kits | Milwaukee Tool
- Who Wrote Derelict Rescue Novel
- Christine Fuller Favorite Book
- Des stars du rap français vont faire vibrer Lausanne
- What Is The 69 Minutes Of 86 Days (2017) About
- Story Of War: Futatsu No Kurumi Ep 1 Crunchyroll
- I Tried an Ultrasonic Skin Spatula to Unclog My Pores
- Nier: Automata - Limited Edition Steelbook Newbie Guide
- How to make background transparent on Canva? - Mad Penguin
Article information
Author: Greg O'Connell
Last Updated:
Views: 5748
Rating: 4.1 / 5 (62 voted)
Reviews: 93% of readers found this page helpful
Author information
Name: Greg O'Connell
Birthday: 1992-01-10
Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519
Phone: +2614651609714
Job: Education Developer
Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding
Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.