Skip to content

Commit 87f754d

Browse files
Refactored a utility function
1 parent c76cebe commit 87f754d

2 files changed

Lines changed: 50 additions & 32 deletions

File tree

ccinput/packages/xtb.py

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22

33
from ccinput.constants import CalcType, ATOMIC_NUMBER, LOWERCASE_ATOMIC_SYMBOLS
4-
from ccinput.utilities import get_solvent, get_method
4+
from ccinput.utilities import get_solvent, get_method, compress_indices
55
from ccinput.exceptions import InvalidParameter, ImpossibleCalculation
66

77

@@ -128,33 +128,6 @@ def handle_constraints_scan(self):
128128
if cmd.scan:
129129
self.input_file += f"{counter+1}: {cmd.start_d:.2f}, {cmd.end_d:.2f}, {cmd.num_steps}\n"
130130

131-
def compress_indices(self, arr):
132-
comp = []
133-
134-
def add_to_str(curr):
135-
if len(curr) == 0:
136-
return ""
137-
elif len(curr) == 1:
138-
return f"{curr[0]}"
139-
else:
140-
return f"{curr[0]}-{curr[-1]}"
141-
142-
_arr = sorted(set(arr))
143-
curr_atoms = []
144-
145-
for a in _arr:
146-
if len(curr_atoms) == 0:
147-
curr_atoms.append(a)
148-
else:
149-
if a == curr_atoms[-1] + 1:
150-
curr_atoms.append(a)
151-
else:
152-
comp.append(add_to_str(curr_atoms))
153-
curr_atoms = [a]
154-
155-
comp.append(add_to_str(curr_atoms))
156-
return ",".join(comp)
157-
158131
def handle_constraints_crest(self):
159132
if len(self.calc.constraints) == 0:
160133
raise InvalidParameter("No constraint in constrained optimisation mode")
@@ -171,15 +144,15 @@ def handle_constraints_crest(self):
171144
self.input_file += cmd.to_xtb()
172145
constr_atoms += cmd.ids
173146

174-
self.input_file += f"atoms: {self.compress_indices(constr_atoms)}\n"
147+
self.input_file += f"atoms: {compress_indices(constr_atoms)}\n"
175148

176149
mtd_atoms = list(range(1, num_atoms))
177150
for a in constr_atoms:
178151
if int(a) in mtd_atoms:
179152
mtd_atoms.remove(int(a))
180153

181154
self.input_file += "$metadyn\n"
182-
self.input_file += f"atoms: {self.compress_indices(mtd_atoms)}\n"
155+
self.input_file += f"atoms: {compress_indices(mtd_atoms)}\n"
183156

184157
def handle_specifications(self):
185158
accuracy = -1

ccinput/utilities.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,25 @@
22
import string
33
import numpy as np
44

5-
from ccinput.constants import *
6-
from ccinput.exceptions import *
5+
from ccinput.constants import (
6+
THEORY_LEVELS,
7+
SOFTWARE_METHODS,
8+
SOFTWARE_BASIS_SETS,
9+
SOFTWARE_SOLVENTS,
10+
SYN_TYPES,
11+
SYN_METHODS,
12+
SYN_SOFTWARE,
13+
SYN_BASIS_SETS,
14+
SYN_SOLVENTS,
15+
ATOMIC_NUMBER,
16+
ATOMIC_SYMBOL,
17+
LOWERCASE_ATOMIC_SYMBOLS,
18+
FUNCTIONALS_WITH_DISPERSION_PARAMETERS,
19+
BASIS_SET_EXCHANGE_KEY,
20+
EXCHANGE_FUNCTIONALS,
21+
CORRELATION_FUNCTIONALS,
22+
)
23+
from ccinput.exceptions import InvalidParameter, InvalidXYZ
724

825
MEMORY_FACTORS = {
926
"m": 1,
@@ -520,3 +537,31 @@ def parse_specifications(specs, add_option_fn, condense=True):
520537
add_option_fn(key, option.replace("&", " "))
521538
else:
522539
add_option_fn(spec, "")
540+
541+
542+
def compress_indices(arr):
543+
comp = []
544+
545+
def add_to_str(curr):
546+
if len(curr) == 0:
547+
return ""
548+
elif len(curr) == 1:
549+
return f"{curr[0]}"
550+
else:
551+
return f"{curr[0]}-{curr[-1]}"
552+
553+
_arr = sorted(set(arr))
554+
curr_atoms = []
555+
556+
for a in _arr:
557+
if len(curr_atoms) == 0:
558+
curr_atoms.append(a)
559+
else:
560+
if a == curr_atoms[-1] + 1:
561+
curr_atoms.append(a)
562+
else:
563+
comp.append(add_to_str(curr_atoms))
564+
curr_atoms = [a]
565+
566+
comp.append(add_to_str(curr_atoms))
567+
return ",".join(comp)

0 commit comments

Comments
 (0)