Skip to content

Commit c8fd8be

Browse files
committed
make updates for chem trf
1 parent bb30a28 commit c8fd8be

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

calphy/composition_transformation.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ class CompositionTransformation:
112112
```
113113
The output is written in LAMMPS dump format.
114114
"""
115-
def __init__(self, calc):
116-
115+
def __init__(self, calc, keep_types=False):
116+
"""
117+
keep_types: Allows to not add a new type for transformations, useful when creating a structure of given composition.
118+
"""
117119
self.input_chemical_composition = calc.composition_scaling._input_chemical_composition
118120
self.output_chemical_composition = calc.composition_scaling.output_chemical_composition
119121
self.restrictions = calc.composition_scaling.restrictions
@@ -126,7 +128,7 @@ def __init__(self, calc):
126128
self.mappings = None
127129
self.unique_mappings = None
128130
self.mappingdict = None
129-
self.prepare_mappings()
131+
self.prepare_mappings(keep_types=keep_types)
130132

131133
def dict_to_string(self, inputdict):
132134
strlst = []
@@ -160,7 +162,7 @@ def _log(val):
160162
entropy_term = kb*np.sum(ents)
161163
return entropy_term
162164

163-
def convert_to_pyscal(self):
165+
def convert_to_pyscal(self, keep_types=False):
164166
"""
165167
Convert a given system to pyscal and give a dict of type mappings
166168
"""
@@ -182,7 +184,10 @@ def convert_to_pyscal(self):
182184

183185
self.actual_species = len(self.typedict)
184186
self.new_species = len(self.output_chemical_composition) - len(self.typedict)
185-
self.maxtype = self.actual_species + 1 #+ self.new_species
187+
if keep_types:
188+
self.maxtype = self.actual_species + 1 #+ self.new_species\
189+
else:
190+
self.maxtype = self.actual_species
186191
#print(self.typedict)
187192

188193
def get_composition_transformation(self):
@@ -308,21 +313,33 @@ def get_mappings(self):
308313
self.compute_possible_mappings()
309314
self.update_mappings()
310315

311-
def prepare_pair_lists(self):
316+
def prepare_pair_lists(self, keep_types=False):
317+
"""
318+
keep_types: If True, a new atom type will not be added. Existing types will be kept.
319+
This is useful if one wants to simply create a new atomic configuration with the given composition.
320+
"""
312321
self.pair_list_old = []
313322
self.pair_list_new = []
323+
new_atomtype = []
314324
for mapping in self.unique_mappings:
315325
map_split = mapping.split("-")
316326
#conserved atom
317327
if (map_split[0]==map_split[1]):
318328
self.pair_list_old.append(self.reversetypedict[int(map_split[0])])
319329
self.pair_list_new.append(self.reversetypedict[int(map_split[0])])
330+
new_atomtype.append(int(map_split[0]))
320331
else:
321332
self.pair_list_old.append(self.reversetypedict[int(map_split[0])])
322-
self.pair_list_new.append(self.reversetypedict[int(map_split[1])])
323-
self.new_atomtype = np.array(range(len(self.unique_mappings)))+1
333+
self.pair_list_new.append(self.reversetypedict[int(map_split[1])])
334+
new_atomtype.append(int(map_split[1]))
335+
if keep_types:
336+
self.new_atomtype = new_atomtype
337+
else:
338+
self.new_atomtype = np.array(range(len(self.unique_mappings)))+1
339+
#print(new_atomtype)
324340
self.mappingdict = dict(zip(self.unique_mappings, self.new_atomtype))
325-
341+
342+
326343
def update_types(self):
327344
for x in range(len(self.atom_type)):
328345
self.atom_type[x] = self.mappingdict[self.mappings[x]]
@@ -399,17 +416,17 @@ def write_structure(self, outfilename):
399416

400417

401418

402-
def prepare_mappings(self):
419+
def prepare_mappings(self, keep_types=False):
403420
self.atom_mark = []
404421
self.atom_species = []
405422
self.mappings = []
406423
self.unique_mappings = []
407424

408425
self.get_composition_transformation()
409-
self.convert_to_pyscal()
426+
self.convert_to_pyscal(keep_types=keep_types)
410427

411428
self.mark_atoms()
412429
self.update_mark_atoms()
413430
self.get_mappings()
414-
self.prepare_pair_lists()
431+
self.prepare_pair_lists(keep_types=keep_types)
415432
self.update_types()

0 commit comments

Comments
 (0)