@@ -185,16 +185,17 @@ def species_by_znucl(structure: Structure) -> list[Species]:
185
185
return types
186
186
187
187
188
- def structure_to_abivars (structure , enforce_znucl = None , enforce_typat = None , ** kwargs ):
188
+ def structure_to_abivars (
189
+ structure : Structure , enforce_znucl : list | None = None , enforce_typat : list | None = None , ** kwargs
190
+ ):
189
191
"""
190
192
Receives a structure and returns a dictionary with ABINIT variables.
191
193
192
194
Args:
193
- enforce_znucl: List of ntypat entries with the value of Z for each type of atom.
194
- Used to change the default ordering.
195
- enforce_typat: List with natom entries with the type index.
196
- Fortran conventions: start to count from 1.
197
- Used to change the default ordering.
195
+ enforce_znucl (list): ntypat entries with the value of Z for each type of atom.
196
+ Used to change the default ordering. Defaults to None.
197
+ enforce_typat (list): natom entries with the type index.
198
+ Fortran conventions: start to count from 1. Used to change the default ordering.
198
199
"""
199
200
if not structure .is_ordered :
200
201
raise ValueError (
@@ -205,7 +206,6 @@ def structure_to_abivars(structure, enforce_znucl=None, enforce_typat=None, **kw
205
206
)
206
207
207
208
n_atoms = len (structure )
208
- n_types_atom = structure .ntypesp
209
209
enforce_order = False
210
210
211
211
if enforce_znucl is not None or enforce_typat is not None :
@@ -219,19 +219,21 @@ def structure_to_abivars(structure, enforce_znucl=None, enforce_typat=None, **kw
219
219
f"enforce_typat contains { len (enforce_typat )} entries while it should be { len (structure )= } "
220
220
)
221
221
222
- if len (enforce_znucl ) != n_types_atom :
223
- raise ValueError (f"enforce_znucl contains { len (enforce_znucl )} entries while it should be { n_types_atom = } " )
222
+ if len (enforce_znucl ) != structure .n_elems :
223
+ raise ValueError (
224
+ f"enforce_znucl contains { len (enforce_znucl )} entries while it should be { structure .n_elems = } "
225
+ )
224
226
225
- if not enforce_order :
227
+ if enforce_order :
228
+ znucl_type = enforce_znucl
229
+ typat = enforce_typat or [] # or [] added for mypy
230
+ else :
226
231
types_of_specie = species_by_znucl (structure )
227
232
228
233
znucl_type = [specie .number for specie in types_of_specie ]
229
234
typat = np .zeros (n_atoms , int )
230
235
for atm_idx , site in enumerate (structure ):
231
236
typat [atm_idx ] = types_of_specie .index (site .specie ) + 1
232
- else :
233
- znucl_type = enforce_znucl
234
- typat = enforce_typat
235
237
236
238
r_prim = ArrayWithUnit (structure .lattice .matrix , "ang" ).to ("bohr" )
237
239
ang_deg = structure .lattice .angles
@@ -245,19 +247,19 @@ def structure_to_abivars(structure, enforce_znucl=None, enforce_typat=None, **kw
245
247
# Info on atoms.
246
248
dct = {
247
249
"natom" : n_atoms ,
248
- "ntypat" : n_types_atom ,
250
+ "ntypat" : structure . n_elems ,
249
251
"typat" : typat ,
250
252
"znucl" : znucl_type ,
251
253
"xred" : x_red ,
252
254
"properties" : structure .properties ,
253
255
}
254
256
255
257
# Add info on the lattice.
256
- # Should we use (rprim, acell) or (angdeg , acell) to specify the lattice?
258
+ # Should we use (rprim, acell) or (ang_deg , acell) to specify the lattice?
257
259
geo_mode = kwargs .pop ("geomode" , "rprim" )
258
260
if geo_mode == "automatic" :
259
261
geo_mode = "rprim"
260
- if structure .lattice .is_hexagonal : # or structure.lattice.is_rhombohedral
262
+ if structure .lattice .is_hexagonal () : # or structure.lattice.is_rhombohedral
261
263
geo_mode = "angdeg"
262
264
ang_deg = structure .lattice .angles
263
265
# Here one could polish a bit the numerical values if they are not exact.
@@ -281,15 +283,15 @@ def structure_to_abivars(structure, enforce_znucl=None, enforce_typat=None, **kw
281
283
return dct
282
284
283
285
284
- def contract (s ):
286
+ def contract (string ):
285
287
"""
286
288
assert contract("1 1 1 2 2 3") == "3*1 2*2 1*3"
287
289
assert contract("1 1 3 2 3") == "2*1 1*3 1*2 1*3"
288
290
"""
289
- if not s :
290
- return s
291
+ if not string :
292
+ return string
291
293
292
- tokens = s .split ()
294
+ tokens = string .split ()
293
295
old = tokens [0 ]
294
296
count = [[1 , old ]]
295
297
0 commit comments