dockonsurf / modules / screening.py @ b4b2f307
Historique | Voir | Annoter | Télécharger (21,84 ko)
1 | e07c09eb | Carles | import logging |
---|---|---|---|
2 | f3d1e601 | Carles Marti | import numpy as np |
3 | f3d1e601 | Carles Marti | import ase |
4 | e07c09eb | Carles | |
5 | e07c09eb | Carles | logger = logging.getLogger('DockOnSurf')
|
6 | e07c09eb | Carles | |
7 | e07c09eb | Carles | |
8 | bfe93f0d | Carles Marti | def assign_prop(atoms: ase.Atoms, prop_name: str, prop_val): # TODO Needed? |
9 | bfe93f0d | Carles Marti | atoms.info[prop_name] = prop_val |
10 | bfe93f0d | Carles Marti | |
11 | bfe93f0d | Carles Marti | |
12 | bfe93f0d | Carles Marti | def select_confs(orig_conf_list: list, magns: list, num_sel: int, code: str): |
13 | bfe93f0d | Carles Marti | """Takes a list ase.Atoms and selects the most different magnitude-wise.
|
14 | bfe93f0d | Carles Marti |
|
15 | bfe93f0d | Carles Marti | Given a list of ase.Atoms objects and a list of magnitudes, it selects a
|
16 | bfe93f0d | Carles Marti | number of the most different conformers according to every magnitude
|
17 | bfe93f0d | Carles Marti | specified.
|
18 | bfe93f0d | Carles Marti | @param orig_conf_list: list of ase.Atoms objects to select among.
|
19 | bfe93f0d | Carles Marti | @param magns: list of str with the names of the magnitudes to use for the
|
20 | bfe93f0d | Carles Marti | conformer selection.
|
21 | bfe93f0d | Carles Marti | Supported magnitudes: 'energy', 'moi'.
|
22 | bfe93f0d | Carles Marti | @param num_sel: number of conformers to select for every of the magnitudes.
|
23 | bfe93f0d | Carles Marti | @param code: The code that generated the magnitude information.
|
24 | bfe93f0d | Carles Marti | Supported codes: See formats.py
|
25 | bfe93f0d | Carles Marti | @return: list of the selected ase.Atoms objects.
|
26 | bfe93f0d | Carles Marti | """
|
27 | bfe93f0d | Carles Marti | from copy import deepcopy |
28 | bfe93f0d | Carles Marti | from modules.formats import read_energies |
29 | bfe93f0d | Carles Marti | |
30 | bfe93f0d | Carles Marti | conf_list = deepcopy(orig_conf_list) |
31 | bfe93f0d | Carles Marti | selected_ids = [] |
32 | bfe93f0d | Carles Marti | if num_sel >= len(conf_list): |
33 | bfe93f0d | Carles Marti | logger.warning('Number of conformers per magnitude is equal or larger '
|
34 | bfe93f0d | Carles Marti | 'than the total number of conformers. Using all '
|
35 | 695dcff8 | Carles Marti | f'available conformers: {len(conf_list)}.')
|
36 | bfe93f0d | Carles Marti | return conf_list
|
37 | bfe93f0d | Carles Marti | |
38 | bfe93f0d | Carles Marti | # Read properties
|
39 | bfe93f0d | Carles Marti | if 'energy' in magns: |
40 | bfe93f0d | Carles Marti | conf_enrgs = read_energies(code, 'isolated')
|
41 | bfe93f0d | Carles Marti | if 'moi' in magns: |
42 | bfe93f0d | Carles Marti | mois = np.array([conf.get_moments_of_inertia() for conf in conf_list]) |
43 | bfe93f0d | Carles Marti | |
44 | bfe93f0d | Carles Marti | # Assign values
|
45 | bfe93f0d | Carles Marti | for i, conf in enumerate(conf_list): |
46 | bfe93f0d | Carles Marti | assign_prop(conf, 'idx', i)
|
47 | bfe93f0d | Carles Marti | if 'energy' in magns: |
48 | bfe93f0d | Carles Marti | assign_prop(conf, 'energy', conf_enrgs[i])
|
49 | bfe93f0d | Carles Marti | if 'moi' in magns: |
50 | bfe93f0d | Carles Marti | assign_prop(conf, 'moi', mois[i, 2]) |
51 | bfe93f0d | Carles Marti | |
52 | bfe93f0d | Carles Marti | # pick ids
|
53 | bfe93f0d | Carles Marti | for magn in magns: |
54 | bfe93f0d | Carles Marti | sorted_list = sorted(conf_list, key=lambda conf: abs(conf.info[magn])) |
55 | bfe93f0d | Carles Marti | if sorted_list[-1].info['idx'] not in selected_ids: |
56 | bfe93f0d | Carles Marti | selected_ids.append(sorted_list[-1].info['idx']) |
57 | bfe93f0d | Carles Marti | if num_sel > 1: |
58 | bfe93f0d | Carles Marti | for i in range(0, len(sorted_list) - 1, |
59 | bfe93f0d | Carles Marti | len(conf_list) // (num_sel - 1)): |
60 | bfe93f0d | Carles Marti | if sorted_list[i].info['idx'] not in selected_ids: |
61 | bfe93f0d | Carles Marti | selected_ids.append(sorted_list[i].info['idx'])
|
62 | bfe93f0d | Carles Marti | |
63 | 695dcff8 | Carles Marti | logger.info(f'Selected {len(selected_ids)} conformers for adsorption.')
|
64 | bfe93f0d | Carles Marti | return [conf_list[idx] for idx in selected_ids] |
65 | bfe93f0d | Carles Marti | |
66 | bfe93f0d | Carles Marti | |
67 | b4aef3d7 | Carles Marti | def get_vect_angle(v1, v2, degrees=False): |
68 | b4aef3d7 | Carles Marti | """Computes the angle between two vectors.
|
69 | b4aef3d7 | Carles Marti |
|
70 | b4aef3d7 | Carles Marti | @param v1: The first vector.
|
71 | b4aef3d7 | Carles Marti | @param v2: The second vector.
|
72 | b4aef3d7 | Carles Marti | @param degrees: Whether the result should be in radians (True) or in
|
73 | b4aef3d7 | Carles Marti | degrees (False).
|
74 | b4aef3d7 | Carles Marti | @return: The angle in radians if degrees = False, or in degrees if
|
75 | b4aef3d7 | Carles Marti | degrees =True
|
76 | b4aef3d7 | Carles Marti | """
|
77 | b4aef3d7 | Carles Marti | v1_u = v1 / np.linalg.norm(v1) |
78 | b4aef3d7 | Carles Marti | v2_u = v2 / np.linalg.norm(v2) |
79 | b4aef3d7 | Carles Marti | angle = np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0)) |
80 | b4aef3d7 | Carles Marti | return angle if not degrees else angle * 180 / np.pi |
81 | b4aef3d7 | Carles Marti | |
82 | b4aef3d7 | Carles Marti | |
83 | 12a12bdd | Carles Marti | def vect_avg(vects): |
84 | a5cc42ff | Carles Marti | """Computes the element-wise mean of a set of vectors.
|
85 | 12a12bdd | Carles Marti |
|
86 | dadc6016 | Carles Marti | @param vects: list of lists-like: containing the vectors (num_vectors,
|
87 | dadc6016 | Carles Marti | length_vector).
|
88 | a5cc42ff | Carles Marti | @return: vector average computed doing the element-wise mean.
|
89 | 12a12bdd | Carles Marti | """
|
90 | 12a12bdd | Carles Marti | from utilities import try_command |
91 | 12a12bdd | Carles Marti | err = "vect_avg parameter vects must be a list-like, able to be converted" \
|
92 | 12a12bdd | Carles Marti | " np.array"
|
93 | dadc6016 | Carles Marti | array = try_command(np.array, [(ValueError, err)], vects)
|
94 | dadc6016 | Carles Marti | if len(array.shape) == 1: |
95 | dadc6016 | Carles Marti | return array
|
96 | 12a12bdd | Carles Marti | else:
|
97 | dadc6016 | Carles Marti | num_vects = array.shape[1]
|
98 | dadc6016 | Carles Marti | return np.array([np.average(array[:, i]) for i in range(num_vects)]) |
99 | 12a12bdd | Carles Marti | |
100 | 12a12bdd | Carles Marti | |
101 | 8fdff302 | Carles Marti | def get_atom_coords(atoms: ase.Atoms, ctrs_list=None): |
102 | 8fdff302 | Carles Marti | """Gets the coordinates of the specified indices from a ase.Atoms object.
|
103 | a5cc42ff | Carles Marti |
|
104 | a5cc42ff | Carles Marti | Given an ase.Atoms object and a list of atom indices specified in ctrs_list
|
105 | a5cc42ff | Carles Marti | it gets the coordinates of the specified atoms. If the element in the
|
106 | a5cc42ff | Carles Marti | ctrs_list is not an index but yet a list of indices, it computes the
|
107 | a5cc42ff | Carles Marti | element-wise mean of the coordinates of the atoms specified in the inner
|
108 | a5cc42ff | Carles Marti | list.
|
109 | a5cc42ff | Carles Marti | @param atoms: ase.Atoms object for which to obtain the coordinates of.
|
110 | a5cc42ff | Carles Marti | @param ctrs_list: list of (indices/list of indices) of the atoms for which
|
111 | a5cc42ff | Carles Marti | the coordinates should be extracted.
|
112 | a5cc42ff | Carles Marti | @return: np.ndarray of atomic coordinates.
|
113 | a5cc42ff | Carles Marti | """
|
114 | 12a12bdd | Carles Marti | coords = [] |
115 | 8fdff302 | Carles Marti | err = "'ctrs_list' argument must be an integer, a list of integers or a " \
|
116 | 8fdff302 | Carles Marti | "list of lists of integers. Every integer must be in the range " \
|
117 | 8fdff302 | Carles Marti | "[0, num_atoms)"
|
118 | 8fdff302 | Carles Marti | if ctrs_list is None: |
119 | 8fdff302 | Carles Marti | ctrs_list = range(len(atoms)) |
120 | 8fdff302 | Carles Marti | elif isinstance(ctrs_list, int): |
121 | 8fdff302 | Carles Marti | if ctrs_list not in range(len(atoms)): |
122 | 8fdff302 | Carles Marti | logger.error(err) |
123 | 8fdff302 | Carles Marti | raise ValueError(err) |
124 | 8fdff302 | Carles Marti | return atoms[ctrs_list].position
|
125 | 8fdff302 | Carles Marti | for elem in ctrs_list: |
126 | 12a12bdd | Carles Marti | if isinstance(elem, list): |
127 | 8fdff302 | Carles Marti | coords.append(vect_avg([atoms[c].position for c in elem])) |
128 | 12a12bdd | Carles Marti | elif isinstance(elem, int): |
129 | 12a12bdd | Carles Marti | coords.append(atoms[elem].position) |
130 | 12a12bdd | Carles Marti | else:
|
131 | 8fdff302 | Carles Marti | |
132 | 12a12bdd | Carles Marti | logger.error(err) |
133 | 12a12bdd | Carles Marti | raise ValueError |
134 | 12a12bdd | Carles Marti | return np.array(coords)
|
135 | f3d1e601 | Carles Marti | |
136 | f3d1e601 | Carles Marti | |
137 | dadc6016 | Carles Marti | def add_adsorbate(slab, adsorbate, site_coord, ctr_coord, height, offset=None, |
138 | 1d22a086 | Carles Marti | norm_vect=(0, 0, 1)): |
139 | 1d22a086 | Carles Marti | """Add an adsorbate to a surface.
|
140 | 1d22a086 | Carles Marti |
|
141 | 1d22a086 | Carles Marti | This function extends the functionality of ase.build.add_adsorbate
|
142 | 1d22a086 | Carles Marti | (https://wiki.fysik.dtu.dk/ase/ase/build/surface.html#ase.build.add_adsorbate)
|
143 | 1d22a086 | Carles Marti | by enabling to change the z coordinate and the axis perpendicular to the
|
144 | 1d22a086 | Carles Marti | surface.
|
145 | 1d22a086 | Carles Marti | @param slab: ase.Atoms object containing the coordinates of the surface
|
146 | 1d22a086 | Carles Marti | @param adsorbate: ase.Atoms object containing the coordinates of the
|
147 | 1d22a086 | Carles Marti | adsorbate.
|
148 | dadc6016 | Carles Marti | @param site_coord: The coordinates of the adsorption site on the surface.
|
149 | dadc6016 | Carles Marti | @param ctr_coord: The coordinates of the adsorption center in the molecule.
|
150 | dadc6016 | Carles Marti | @param height: The height above the surface where to adsorb.
|
151 | 1d22a086 | Carles Marti | @param offset: Offsets the adsorbate by a number of unit cells. Mostly
|
152 | 1d22a086 | Carles Marti | useful when adding more than one adsorbate.
|
153 | 1d22a086 | Carles Marti | @param norm_vect: The vector perpendicular to the surface.
|
154 | 1d22a086 | Carles Marti | """
|
155 | 36d92f4f | Carles Marti | from copy import deepcopy |
156 | 1d22a086 | Carles Marti | info = slab.info.get('adsorbate_info', {})
|
157 | dadc6016 | Carles Marti | pos = np.array([0.0, 0.0, 0.0]) # part of absolute coordinates |
158 | 1d22a086 | Carles Marti | spos = np.array([0.0, 0.0, 0.0]) # part relative to unit cell |
159 | dadc6016 | Carles Marti | norm_vect_u = np.array(norm_vect) / np.linalg.norm(norm_vect) |
160 | 1d22a086 | Carles Marti | if offset is not None: |
161 | 1d22a086 | Carles Marti | spos += np.asarray(offset, float)
|
162 | dadc6016 | Carles Marti | if isinstance(site_coord, str): |
163 | 1d22a086 | Carles Marti | # A site-name:
|
164 | 1d22a086 | Carles Marti | if 'sites' not in info: |
165 | 1d22a086 | Carles Marti | raise TypeError('If the atoms are not made by an ase.build ' |
166 | 1d22a086 | Carles Marti | 'function, position cannot be a name.')
|
167 | dadc6016 | Carles Marti | if site_coord not in info['sites']: |
168 | dadc6016 | Carles Marti | raise TypeError('Adsorption site %s not supported.' % site_coord) |
169 | dadc6016 | Carles Marti | spos += info['sites'][site_coord]
|
170 | 1d22a086 | Carles Marti | else:
|
171 | dadc6016 | Carles Marti | pos += site_coord |
172 | 1d22a086 | Carles Marti | if 'cell' in info: |
173 | 1d22a086 | Carles Marti | cell = info['cell']
|
174 | 1d22a086 | Carles Marti | else:
|
175 | 1d22a086 | Carles Marti | cell = slab.get_cell() |
176 | 1d22a086 | Carles Marti | pos += np.dot(spos, cell) |
177 | 1d22a086 | Carles Marti | # Convert the adsorbate to an Atoms object
|
178 | 1d22a086 | Carles Marti | if isinstance(adsorbate, ase.Atoms): |
179 | 36d92f4f | Carles Marti | ads = deepcopy(adsorbate) |
180 | 1d22a086 | Carles Marti | elif isinstance(adsorbate, ase.Atom): |
181 | 1d22a086 | Carles Marti | ads = ase.Atoms([adsorbate]) |
182 | 1d22a086 | Carles Marti | else:
|
183 | 1d22a086 | Carles Marti | # Assume it is a string representing a single Atom
|
184 | 1d22a086 | Carles Marti | ads = ase.Atoms([ase.Atom(adsorbate)]) |
185 | dadc6016 | Carles Marti | pos += height * norm_vect_u |
186 | 1d22a086 | Carles Marti | # Move adsorbate into position
|
187 | dadc6016 | Carles Marti | ads.translate(pos - ctr_coord) |
188 | 1d22a086 | Carles Marti | # Attach the adsorbate
|
189 | 1d22a086 | Carles Marti | slab.extend(ads) |
190 | 1d22a086 | Carles Marti | |
191 | 1d22a086 | Carles Marti | |
192 | a4b57124 | Carles Marti | def check_collision(slab_molec, slab_num_atoms, min_height, vect, nn_slab=0, |
193 | a4b57124 | Carles Marti | nn_molec=0, coll_coeff=0.9): |
194 | 5f3f4b69 | Carles Marti | """Checks whether a slab and a molecule collide or not.
|
195 | 5f3f4b69 | Carles Marti |
|
196 | 5f3f4b69 | Carles Marti | @param slab_molec: The system of adsorbate-slab for which to detect if there
|
197 | 5f3f4b69 | Carles Marti | are collisions.
|
198 | 5f3f4b69 | Carles Marti | @param nn_slab: Number of neigbors in the surface.
|
199 | 5f3f4b69 | Carles Marti | @param nn_molec: Number of neighbors in the molecule.
|
200 | 5f3f4b69 | Carles Marti | @param coll_coeff: The coefficient that multiplies the covalent radius of
|
201 | 5f3f4b69 | Carles Marti | atoms resulting in a distance that two atoms being closer to that is
|
202 | 5f3f4b69 | Carles Marti | considered as atomic collision.
|
203 | 5f3f4b69 | Carles Marti | @param slab_num_atoms: Number of atoms of the bare slab.
|
204 | 5f3f4b69 | Carles Marti | @param min_height: The minimum height atoms can have to not be considered as
|
205 | 5f3f4b69 | Carles Marti | colliding.
|
206 | 5f3f4b69 | Carles Marti | @param vect: The vector perpendicular to the slab.
|
207 | 5f3f4b69 | Carles Marti | @return: bool, whether the surface and the molecule collide.
|
208 | 5f3f4b69 | Carles Marti | """
|
209 | 5f3f4b69 | Carles Marti | from ase.neighborlist import natural_cutoffs, neighbor_list |
210 | 5fb01677 | Carles Marti | if min_height is not False: |
211 | a4b57124 | Carles Marti | cart_axes = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0], |
212 | a4b57124 | Carles Marti | [-1.0, 0.0, 0.0], [0.0, -1.0, 0.0], [0.0, 0.0, -1.0]] |
213 | a4b57124 | Carles Marti | if vect.tolist() in cart_axes: |
214 | 5fb01677 | Carles Marti | for atom in slab_molec[slab_num_atoms:]: |
215 | a4b57124 | Carles Marti | for i, coord in enumerate(vect): |
216 | a4b57124 | Carles Marti | if coord == 0: |
217 | a4b57124 | Carles Marti | continue
|
218 | a4b57124 | Carles Marti | if atom.position[i] * coord < min_height:
|
219 | a4b57124 | Carles Marti | return True |
220 | a4b57124 | Carles Marti | return False |
221 | 5f3f4b69 | Carles Marti | else:
|
222 | 5fb01677 | Carles Marti | slab_molec_cutoffs = natural_cutoffs(slab_molec, mult=coll_coeff) |
223 | b4b2f307 | Carles Marti | slab_molec_nghbs = len(
|
224 | b4b2f307 | Carles Marti | neighbor_list("i", slab_molec, slab_molec_cutoffs))
|
225 | 5fb01677 | Carles Marti | if slab_molec_nghbs > nn_slab + nn_molec:
|
226 | 5fb01677 | Carles Marti | return True |
227 | 5fb01677 | Carles Marti | else:
|
228 | 5fb01677 | Carles Marti | return False |
229 | 5f3f4b69 | Carles Marti | |
230 | 5f3f4b69 | Carles Marti | |
231 | b4b2f307 | Carles Marti | def dissociate_h(slab_molec_orig, h_idx, num_atoms_slab, neigh_cutoff=1): |
232 | b4b2f307 | Carles Marti | # TODO rethink
|
233 | b4b2f307 | Carles Marti | """Moves a H atom from a molecule to the surface if the distance is short
|
234 | b4b2f307 | Carles Marti | enough.
|
235 | b4b2f307 | Carles Marti |
|
236 | b4b2f307 | Carles Marti | @param slab_molec_orig: The ase.Atoms object of the system adsorbate-slab.
|
237 | b4b2f307 | Carles Marti | @param h_idx: The index of the hydrogen atom to carry out adsorption of.
|
238 | b4b2f307 | Carles Marti | @param num_atoms_slab: The number of atoms of the slab without adsorbate.
|
239 | b4b2f307 | Carles Marti | @param neigh_cutoff: half the maximum distance between the surface and the
|
240 | b4b2f307 | Carles Marti | H for it to carry out dissociation.
|
241 | b4b2f307 | Carles Marti | @return: An ase.Atoms object of the system adsorbate-surface with H
|
242 | b4b2f307 | Carles Marti | """
|
243 | b4b2f307 | Carles Marti | from copy import deepcopy |
244 | b4b2f307 | Carles Marti | from ase.neighborlist import NeighborList |
245 | b4b2f307 | Carles Marti | slab_molec = deepcopy(slab_molec_orig) |
246 | b4b2f307 | Carles Marti | cutoffs = len(slab_molec) * [neigh_cutoff]
|
247 | b4b2f307 | Carles Marti | nl = NeighborList(cutoffs, self_interaction=False, bothways=True) |
248 | b4b2f307 | Carles Marti | nl.update(slab_molec) |
249 | b4b2f307 | Carles Marti | surf_h_vect = np.array([np.infty] * 3)
|
250 | b4b2f307 | Carles Marti | for neigh_idx in nl.get_neighbors(h_idx)[0]: |
251 | b4b2f307 | Carles Marti | if neigh_idx < num_atoms_slab:
|
252 | b4b2f307 | Carles Marti | dist = np.linalg.norm(slab_molec[neigh_idx].position - |
253 | b4b2f307 | Carles Marti | slab_molec[h_idx].position) |
254 | b4b2f307 | Carles Marti | if dist < np.linalg.norm(surf_h_vect):
|
255 | b4b2f307 | Carles Marti | surf_h_vect = slab_molec[neigh_idx].position \ |
256 | b4b2f307 | Carles Marti | - slab_molec[h_idx].position |
257 | b4b2f307 | Carles Marti | if np.linalg.norm(surf_h_vect) != np.infty:
|
258 | b4b2f307 | Carles Marti | trans_vect = surf_h_vect - surf_h_vect / np.linalg.norm(surf_h_vect) |
259 | b4b2f307 | Carles Marti | slab_molec[h_idx].position = slab_molec[h_idx].position + trans_vect |
260 | b4b2f307 | Carles Marti | return slab_molec
|
261 | b4b2f307 | Carles Marti | |
262 | b4b2f307 | Carles Marti | |
263 | b4b2f307 | Carles Marti | def dissociation(slab_molec, disso_atoms, num_atoms_slab): |
264 | b4b2f307 | Carles Marti | # TODO rethink
|
265 | b4b2f307 | Carles Marti | # TODO multiple dissociation
|
266 | b4b2f307 | Carles Marti | """Decides which H atoms to dissociate according to a list of atoms.
|
267 | b4b2f307 | Carles Marti |
|
268 | b4b2f307 | Carles Marti | Given a list of chemical symbols or atom indices it checks for every atom
|
269 | b4b2f307 | Carles Marti | or any of its neighbor if it's a H and calls dissociate_h to try to carry
|
270 | b4b2f307 | Carles Marti | out dissociation of that H. For atom indices, it checks both whether
|
271 | b4b2f307 | Carles Marti | the atom index or its neighbors are H, for chemical symbols, it only checks
|
272 | b4b2f307 | Carles Marti | if there is a neighbor H.
|
273 | b4b2f307 | Carles Marti | @param slab_molec: The ase.Atoms object of the system adsorbate-slab.
|
274 | b4b2f307 | Carles Marti | @param disso_atoms: The indices or chemical symbols of the atoms
|
275 | b4b2f307 | Carles Marti | @param num_atoms_slab:
|
276 | b4b2f307 | Carles Marti | @return:
|
277 | b4b2f307 | Carles Marti | """
|
278 | b4b2f307 | Carles Marti | from ase.neighborlist import natural_cutoffs, NeighborList |
279 | b4b2f307 | Carles Marti | molec = slab_molec[num_atoms_slab:] |
280 | b4b2f307 | Carles Marti | cutoffs = natural_cutoffs(molec) |
281 | b4b2f307 | Carles Marti | nl = NeighborList(cutoffs, self_interaction=False, bothways=True) |
282 | b4b2f307 | Carles Marti | nl.update(molec) |
283 | b4b2f307 | Carles Marti | disso_structs = [] |
284 | b4b2f307 | Carles Marti | for el in disso_atoms: |
285 | b4b2f307 | Carles Marti | if isinstance(el, int): |
286 | b4b2f307 | Carles Marti | if molec[el].symbol == 'H': |
287 | b4b2f307 | Carles Marti | disso_struct = dissociate_h(slab_molec, el + num_atoms_slab, |
288 | b4b2f307 | Carles Marti | num_atoms_slab) |
289 | b4b2f307 | Carles Marti | if disso_struct is not None: |
290 | b4b2f307 | Carles Marti | disso_structs.append(disso_struct) |
291 | b4b2f307 | Carles Marti | else:
|
292 | b4b2f307 | Carles Marti | for neigh_idx in nl.get_neighbors(el)[0]: |
293 | b4b2f307 | Carles Marti | if molec[neigh_idx].symbol == 'H': |
294 | b4b2f307 | Carles Marti | disso_struct = dissociate_h(slab_molec, neigh_idx + |
295 | b4b2f307 | Carles Marti | num_atoms_slab, |
296 | b4b2f307 | Carles Marti | num_atoms_slab) |
297 | b4b2f307 | Carles Marti | if disso_struct is not None: |
298 | b4b2f307 | Carles Marti | disso_structs.append(disso_struct) |
299 | b4b2f307 | Carles Marti | else:
|
300 | b4b2f307 | Carles Marti | for atom in molec: |
301 | b4b2f307 | Carles Marti | if atom.symbol.lower() == el.lower():
|
302 | b4b2f307 | Carles Marti | for neigh_idx in nl.get_neighbors(atom.index)[0]: |
303 | b4b2f307 | Carles Marti | if molec[neigh_idx].symbol == 'H': |
304 | b4b2f307 | Carles Marti | disso_struct = dissociate_h(slab_molec, neigh_idx \ |
305 | b4b2f307 | Carles Marti | + num_atoms_slab, |
306 | b4b2f307 | Carles Marti | num_atoms_slab) |
307 | b4b2f307 | Carles Marti | if disso_struct is not None: |
308 | b4b2f307 | Carles Marti | disso_structs.append(disso_struct) |
309 | b4b2f307 | Carles Marti | return disso_structs
|
310 | b4b2f307 | Carles Marti | |
311 | b4b2f307 | Carles Marti | |
312 | a260d04d | Carles Marti | def correct_coll(molec, slab, ctr_coord, site_coord, num_pts, |
313 | 5fb01677 | Carles Marti | min_coll_height, norm_vect, slab_nghbs, molec_nghbs, |
314 | 5fb01677 | Carles Marti | coll_coeff): |
315 | a260d04d | Carles Marti | # TODO Rethink this function
|
316 | a260d04d | Carles Marti | """Tries to adsorb a molecule on a slab trying to avoid collisions by doing
|
317 | a260d04d | Carles Marti | small rotations.
|
318 | a260d04d | Carles Marti |
|
319 | a260d04d | Carles Marti | @param molec: ase.Atoms object of the molecule to adsorb
|
320 | a260d04d | Carles Marti | @param slab: ase.Atoms object of the surface on which to adsorb the
|
321 | a260d04d | Carles Marti | molecule
|
322 | a260d04d | Carles Marti | @param ctr_coord: The coordinates of the molecule to use as adsorption
|
323 | a260d04d | Carles Marti | center.
|
324 | a260d04d | Carles Marti | @param site_coord: The coordinates of the surface on which to adsorb the
|
325 | a260d04d | Carles Marti | molecule
|
326 | a260d04d | Carles Marti | @param num_pts: Number on which to sample Euler angles.
|
327 | 5fb01677 | Carles Marti | @param min_coll_height: The lowermost height for which to detect a collision
|
328 | a260d04d | Carles Marti | @param norm_vect: The vector perpendicular to the surface.
|
329 | a260d04d | Carles Marti | @param slab_nghbs: Number of neigbors in the surface.
|
330 | a260d04d | Carles Marti | @param molec_nghbs: Number of neighbors in the molecule.
|
331 | a260d04d | Carles Marti | @param coll_coeff: The coefficient that multiplies the covalent radius of
|
332 | a260d04d | Carles Marti | atoms resulting in a distance that two atoms being closer to that is
|
333 | a260d04d | Carles Marti | considered as atomic collision.
|
334 | a260d04d | Carles Marti | @return collision: bool, whether the structure generated has collisions
|
335 | a260d04d | Carles Marti | between slab and adsorbate.
|
336 | a260d04d | Carles Marti | """
|
337 | a260d04d | Carles Marti | from copy import deepcopy |
338 | a260d04d | Carles Marti | slab_num_atoms = len(slab)
|
339 | a260d04d | Carles Marti | collision = True
|
340 | a260d04d | Carles Marti | max_corr = 6 # Should be an even number |
341 | a260d04d | Carles Marti | d_angle = 180 / ((max_corr / 2.0) * num_pts) |
342 | a260d04d | Carles Marti | num_corr = 0
|
343 | a260d04d | Carles Marti | while collision and num_corr <= max_corr: |
344 | a260d04d | Carles Marti | k = num_corr * (-1) ** num_corr
|
345 | a260d04d | Carles Marti | slab_molec = deepcopy(slab) |
346 | a260d04d | Carles Marti | molec.euler_rotate(k * d_angle, k * d_angle / 2, k * d_angle,
|
347 | a260d04d | Carles Marti | center=ctr_coord) |
348 | a260d04d | Carles Marti | add_adsorbate(slab_molec, molec, site_coord, ctr_coord, 2.5,
|
349 | a260d04d | Carles Marti | norm_vect=norm_vect) |
350 | 5fb01677 | Carles Marti | collision = check_collision(slab_molec, slab_num_atoms, min_coll_height, |
351 | a260d04d | Carles Marti | norm_vect, slab_nghbs, molec_nghbs, |
352 | a260d04d | Carles Marti | coll_coeff) |
353 | a260d04d | Carles Marti | num_corr += 1
|
354 | a260d04d | Carles Marti | return slab_molec, collision
|
355 | a260d04d | Carles Marti | |
356 | 1d22a086 | Carles Marti | |
357 | 3ab0865c | Carles Marti | def ads_euler(orig_molec, slab, ctr_coord, site_coord, num_pts, |
358 | b4b2f307 | Carles Marti | min_coll_height, coll_coeff, norm_vect, slab_nghbs, molec_nghbs, |
359 | b4b2f307 | Carles Marti | disso_atoms): |
360 | 3ab0865c | Carles Marti | """Generates adsorbate-surface structures by sampling over Euler angles.
|
361 | 3ab0865c | Carles Marti |
|
362 | 3ab0865c | Carles Marti | This function generates a number of adsorbate-surface structures at
|
363 | 3ab0865c | Carles Marti | different orientations of the adsorbate sampled at multiple Euler (zxz)
|
364 | 3ab0865c | Carles Marti | angles.
|
365 | 3ab0865c | Carles Marti | @param orig_molec: ase.Atoms object of the molecule to adsorb
|
366 | 3ab0865c | Carles Marti | @param slab: ase.Atoms object of the surface on which to adsorb the molecule
|
367 | 3ab0865c | Carles Marti | @param ctr_coord: The coordinates of the molecule to use as adsorption
|
368 | 3ab0865c | Carles Marti | center.
|
369 | 3ab0865c | Carles Marti | @param site_coord: The coordinates of the surface on which to adsorb the
|
370 | 3ab0865c | Carles Marti | molecule
|
371 | 3ab0865c | Carles Marti | @param num_pts: Number on which to sample Euler angles.
|
372 | 5fb01677 | Carles Marti | @param min_coll_height: The lowermost height for which to detect a collision
|
373 | 3ab0865c | Carles Marti | @param coll_coeff: The coefficient that multiplies the covalent radius of
|
374 | 3ab0865c | Carles Marti | atoms resulting in a distance that two atoms being closer to that is
|
375 | 3ab0865c | Carles Marti | considered as atomic collision.
|
376 | 3ab0865c | Carles Marti | @param norm_vect: The vector perpendicular to the surface.
|
377 | 3ab0865c | Carles Marti | @param slab_nghbs: Number of neigbors in the surface.
|
378 | 3ab0865c | Carles Marti | @param molec_nghbs: Number of neighbors in the molecule.
|
379 | b4b2f307 | Carles Marti | @param disso_atoms: List of atom types or atom numbers to try to dissociate.
|
380 | 3ab0865c | Carles Marti | @return: list of ase.Atoms object conatining all the orientations of a given
|
381 | 3ab0865c | Carles Marti | conformer
|
382 | 3ab0865c | Carles Marti | """
|
383 | 3ab0865c | Carles Marti | from copy import deepcopy |
384 | 3ab0865c | Carles Marti | slab_ads_list = [] |
385 | 3ab0865c | Carles Marti | # rotation around z
|
386 | 3ab0865c | Carles Marti | for alpha in np.arange(0, 360, 360 / num_pts): |
387 | 3ab0865c | Carles Marti | # rotation around x'
|
388 | 3ab0865c | Carles Marti | for beta in np.arange(0, 180, 180 / num_pts): |
389 | 3ab0865c | Carles Marti | # rotation around z'
|
390 | 3ab0865c | Carles Marti | for gamma in np.arange(0, 360, 360 / num_pts): |
391 | 3ab0865c | Carles Marti | molec = deepcopy(orig_molec) |
392 | 3ab0865c | Carles Marti | molec.euler_rotate(alpha, beta, gamma, center=ctr_coord) |
393 | 3ab0865c | Carles Marti | slab_molec, collision = correct_coll(molec, slab, |
394 | 5fb01677 | Carles Marti | ctr_coord, site_coord, |
395 | 5fb01677 | Carles Marti | num_pts, min_coll_height, |
396 | 5fb01677 | Carles Marti | norm_vect, |
397 | 5fb01677 | Carles Marti | slab_nghbs, molec_nghbs, |
398 | 5fb01677 | Carles Marti | coll_coeff) |
399 | 3ab0865c | Carles Marti | if not collision: |
400 | 3ab0865c | Carles Marti | slab_ads_list.append(slab_molec) |
401 | b4b2f307 | Carles Marti | slab_ads_list.extend(dissociation(slab_molec, disso_atoms, |
402 | b4b2f307 | Carles Marti | len(slab)))
|
403 | 3ab0865c | Carles Marti | |
404 | 3ab0865c | Carles Marti | return slab_ads_list
|
405 | f3d1e601 | Carles Marti | |
406 | f3d1e601 | Carles Marti | |
407 | f3d1e601 | Carles Marti | def ads_chemcat(site, ctr, pts_angle): |
408 | a5cc42ff | Carles Marti | return "TO IMPLEMENT" |
409 | f3d1e601 | Carles Marti | |
410 | f3d1e601 | Carles Marti | |
411 | bb55f47c | Carles Marti | def adsorb_confs(conf_list, surf, molec_ctrs, sites, algo, num_pts, neigh_ctrs, |
412 | b4b2f307 | Carles Marti | norm_vect, min_coll_height, coll_coeff, disso_atoms): |
413 | a5cc42ff | Carles Marti | """Generates a number of adsorbate-surface structure coordinates.
|
414 | a5cc42ff | Carles Marti |
|
415 | a5cc42ff | Carles Marti | Given a list of conformers, a surface, a list of atom indices (or list of
|
416 | a5cc42ff | Carles Marti | list of indices) of both the surface and the adsorbate, it generates a
|
417 | a5cc42ff | Carles Marti | number of adsorbate-surface structures for every possible combination of
|
418 | a5cc42ff | Carles Marti | them at different orientations.
|
419 | a5cc42ff | Carles Marti | @param conf_list: list of ase.Atoms of the different conformers
|
420 | a5cc42ff | Carles Marti | @param surf: the ase.Atoms object of the surface
|
421 | bb55f47c | Carles Marti | @param molec_ctrs: the list atom indices of the adsorbate.
|
422 | a5cc42ff | Carles Marti | @param sites: the list of atom indices of the surface.
|
423 | a5cc42ff | Carles Marti | @param algo: the algorithm to use for the generation of adsorbates.
|
424 | a5cc42ff | Carles Marti | @param num_pts: the number of points per angle orientation to sample
|
425 | a5cc42ff | Carles Marti | @param neigh_ctrs: the indices of the neighboring atoms to the adsorption
|
426 | bb55f47c | Carles Marti | atoms.
|
427 | 1d22a086 | Carles Marti | @param norm_vect: The vector perpendicular to the surface.
|
428 | 5fb01677 | Carles Marti | @param min_coll_height: The lowermost height for which to detect a collision
|
429 | bb55f47c | Carles Marti | @param coll_coeff: The coefficient that multiplies the covalent radius of
|
430 | bb55f47c | Carles Marti | atoms resulting in a distance that two atoms being closer to that is
|
431 | bb55f47c | Carles Marti | considered as atomic collision.
|
432 | b4b2f307 | Carles Marti | @param disso_atoms: List of atom types or atom numbers to try to dissociate.
|
433 | a5cc42ff | Carles Marti | @return: list of ase.Atoms for the adsorbate-surface structures
|
434 | a5cc42ff | Carles Marti | """
|
435 | bb55f47c | Carles Marti | from ase.neighborlist import natural_cutoffs, neighbor_list |
436 | f3d1e601 | Carles Marti | surf_ads_list = [] |
437 | f3d1e601 | Carles Marti | sites_coords = get_atom_coords(surf, sites) |
438 | 3ccf0131 | Carles Marti | if min_coll_height is False: |
439 | 5fb01677 | Carles Marti | surf_cutoffs = natural_cutoffs(surf, mult=coll_coeff) |
440 | 5fb01677 | Carles Marti | surf_nghbs = len(neighbor_list("i", surf, surf_cutoffs)) |
441 | 5fb01677 | Carles Marti | else:
|
442 | 5fb01677 | Carles Marti | surf_nghbs = 0
|
443 | f3d1e601 | Carles Marti | for conf in conf_list: |
444 | bb55f47c | Carles Marti | molec_ctr_coords = get_atom_coords(conf, molec_ctrs) |
445 | f3d1e601 | Carles Marti | molec_neigh_coords = get_atom_coords(conf, neigh_ctrs) |
446 | 3ccf0131 | Carles Marti | if min_coll_height is False: |
447 | 5fb01677 | Carles Marti | conf_cutoffs = natural_cutoffs(conf, mult=coll_coeff) |
448 | 5fb01677 | Carles Marti | molec_nghbs = len(neighbor_list("i", conf, conf_cutoffs)) |
449 | 5fb01677 | Carles Marti | else:
|
450 | 5fb01677 | Carles Marti | molec_nghbs = 0
|
451 | f3d1e601 | Carles Marti | for site in sites_coords: |
452 | bb55f47c | Carles Marti | for ctr in molec_ctr_coords: |
453 | f3d1e601 | Carles Marti | if algo == 'euler': |
454 | bb55f47c | Carles Marti | surf_ads_list.extend(ads_euler(conf, surf, ctr, site, |
455 | 5fb01677 | Carles Marti | num_pts, min_coll_height, |
456 | bb55f47c | Carles Marti | coll_coeff, norm_vect, |
457 | b4b2f307 | Carles Marti | surf_nghbs, molec_nghbs, |
458 | b4b2f307 | Carles Marti | disso_atoms)) |
459 | f3d1e601 | Carles Marti | elif algo == 'chemcat': |
460 | bb55f47c | Carles Marti | surf_ads_list.extend(ads_chemcat(site, ctr, num_pts)) |
461 | f3d1e601 | Carles Marti | return surf_ads_list
|
462 | f3d1e601 | Carles Marti | |
463 | f3d1e601 | Carles Marti | |
464 | 4614bb6a | Carles | def run_screening(inp_vars): |
465 | e07c09eb | Carles | """Carry out the screening of adsorbate coordinates on a surface
|
466 | e07c09eb | Carles |
|
467 | e07c09eb | Carles | @param inp_vars: Calculation parameters from input file.
|
468 | e07c09eb | Carles | """
|
469 | e07c09eb | Carles | import os |
470 | bfe93f0d | Carles Marti | from modules.formats import read_coords, adapt_format |
471 | f3d1e601 | Carles Marti | from modules.calculation import run_calc |
472 | e07c09eb | Carles | |
473 | e07c09eb | Carles | if not os.path.isdir("isolated"): |
474 | e07c09eb | Carles | err = "'isolated' directory not found. It is needed in order to carry "
|
475 | e07c09eb | Carles | "out the screening of structures to be adsorbed"
|
476 | e07c09eb | Carles | logger.error(err) |
477 | e07c09eb | Carles | raise ValueError(err) |
478 | e07c09eb | Carles | |
479 | bfe93f0d | Carles Marti | conf_list = read_coords(inp_vars['code'], 'isolated', 'ase') |
480 | 695dcff8 | Carles Marti | logger.info(f"Found {len(conf_list)} structures of isolated conformers.")
|
481 | bfe93f0d | Carles Marti | selected_confs = select_confs(conf_list, inp_vars['select_magns'],
|
482 | bfe93f0d | Carles Marti | inp_vars['confs_per_magn'],
|
483 | bfe93f0d | Carles Marti | inp_vars['code'])
|
484 | f3d1e601 | Carles Marti | surf = adapt_format('ase', inp_vars['surf_file']) |
485 | bfe93f0d | Carles Marti | surf_ads_list = adsorb_confs(selected_confs, surf, |
486 | bfe93f0d | Carles Marti | inp_vars['molec_ads_ctrs'], inp_vars['sites'], |
487 | bfe93f0d | Carles Marti | inp_vars['ads_algo'],
|
488 | f3d1e601 | Carles Marti | inp_vars['sample_points_per_angle'],
|
489 | 1d22a086 | Carles Marti | inp_vars['molec_neigh_ctrs'],
|
490 | dadc6016 | Carles Marti | inp_vars['surf_norm_vect'],
|
491 | 5fb01677 | Carles Marti | inp_vars['min_coll_height'],
|
492 | b4b2f307 | Carles Marti | inp_vars['collision_threshold'],
|
493 | b4b2f307 | Carles Marti | inp_vars['disso_atoms'])
|
494 | bfe93f0d | Carles Marti | logger.info(f'Generated {len(surf_ads_list)} adsorbate-surface atomic '
|
495 | 695dcff8 | Carles Marti | f'configurations, to carry out a calculation of.')
|
496 | f3d1e601 | Carles Marti | run_calc('screening', inp_vars, surf_ads_list) |