I have the site_map = {
‘A’: [‘Sc’, ‘Ti’, ‘V’, ‘Cr’, ‘Mn’, ‘Fe’, ‘Co’, ‘Ni’, ‘Cu’,'],
‘B’: [‘Al’, ‘Si’, ‘P’, ‘As’, ‘In’, ‘Sn’, ‘Sb’, ‘Ta’],
‘X’: [‘Te’, ‘Se’]
. I want to make substitution for one of the Cr in Cr2Ge2Te6 using the elements in site A, in such a way that there will be unique binary combinations of elements in B, while X remain Te or Se. I expected that I would get consistent number of elements in each generated chemical formula to be 5 but I was getting four in some cases. I need help on how to further on this. I do not know if I am wrong with the code.

Please post a concise code snippet illustrating what you’re trying to achieve. Make sure to not include your API key in the post. Thanks!

Define the elements for A, B, and X sites

site_specs = [‘A’, ‘A’, ‘B’, ‘B’, ‘X’]

site_map = {
‘A’: [‘Sc’, ‘Ti’, ‘V’, ‘Cr’, ‘Mn’, ‘Fe’, ‘Co’, ‘Ni’, ‘Cu’, ‘Zn’, ‘Y’, ‘Zr’, ‘Nb’, ‘Mo’, ‘Tc’, ‘Ru’,
‘Rh’, ‘Pd’, ‘Ag’, ‘Cd’, ‘Er’, ‘Hf’, ‘Ta’, ‘W’, ‘Re’, ‘Os’, ‘Ir’, ‘Pt’, ‘Au’, ‘Hg’],
‘B’: [‘Al’, ‘Si’, ‘P’, ‘Ga’, ‘Ge’, ‘As’, ‘In’, ‘Sn’, ‘Sb’, ‘Ta’],
‘X’: [‘O’, ‘S’, ‘Se’, ‘Te’]

Define the actual lists of elements for A, B, and X sites

A_elements = site_map[‘A’]
B_elements = [‘Al’, ‘Si’, ‘Tl’, ‘P’, ‘Ge’, ‘In’, ‘As’, ‘Sn’, ‘Sb’, ‘Pb’, ‘Bi’] # Only include the elements you want for the B site
X_elements = site_map[‘X’]

Create a list to store the formulas

formulas = []

Loop through A elements

for A1 in A_elements:
for A2 in A_elements:
# Ensure A1 and A2 are not the same element
if A1 != A2:
# Loop through B combinations
for B1 in B_elements:
for B2 in B_elements:
# Loop through X elements
for X_element in X_elements:
# Create a dictionary for the composition with substitutions using pymatgen
composition_dict = {‘Cr’: 1, A2: 1, X_element: 6}

                    # Check if B1 is the same as B2, and if so, set stoichiometry for that element
                    if B1 == B2:
                        composition_dict[B1] = 2
                        composition_dict[B1] = 1
                        composition_dict[B2] = 1

                    # Create a Composition object from the dictionary
                    composition = Composition(composition_dict)
                    # Get the formula and append it to the list
                    formula = composition.formula

Create a DataFrame to store the formulas

df = pd.DataFrame({‘formula’: formulas})

Remove spaces from the Formula column using .str.replace

df[‘formula’] = df[‘formula’].str.replace(’ ', ‘’)

Remove duplicate formulas

df = df.drop_duplicates(subset=‘formula’)

Define a list of values to be replaced and their replacements

replace_dict = {
‘Cr1Si2Te6’: ‘Cr2Si2Te6’,
‘Cr1Si1Te6P1’: ‘Cr2Si1Te6P1’,
‘Cr1Si1Ge1Te6’: ‘Cr2Si1Ge1Te6’,
‘Cr1Si1Te6As1’: ‘Cr2Si1Te6As1’,
‘Cr1Si1Sn1Te6’: ‘Cr2Si1Sn1Te6’,
‘Cr1Si1Sb1Te6’: ‘Cr2Si1Sb1Te6’,
‘Cr1Si1Te6Pb1’: ‘Cr2Si1Te6Pb1’,
‘Cr1Si1Bi1Te6’: ‘Cr2Si1Bi1Te6’,
‘Tl1Cr1Sb1Te6’: ‘Tl1Cr2Sb1Te6’,
‘Tl1Cr1Bi1Te6’: ‘Tl1Cr2Bi1Te6’,
‘Cr1Ge1Te6P1’: ‘Cr2Ge1Te6P1’,
‘Cr1Ge2Te6’: ‘Cr2Ge2Te6’,
‘Cr1Ge1Te6As1’: ‘Cr2Ge1Te6As1’,
‘Cr1Sn1Ge1Te6’: ‘Cr2Sn1Ge1Te6’,
‘Cr1Ge1Sb1Te6’: ‘Cr2Ge1Sb1Te6’,
‘Cr1Ge1Te6Pb1’: ‘Cr2Ge1Te6Pb1’,
‘Cr1Ge1Bi1Te6’: ‘Cr2Ge1Bi1Te6’,
‘Cr1In1Bi1Te6’: ‘Cr2In1Bi1Te6’,
‘Cr1Te6Pb2’: ‘Cr2Te6Pb2’,
‘Cr1Sn2Te6’: ‘Cr2Sn2Te6’,
‘Cr1Sn1Te6Pb1’: ‘Cr2Sn1Te6Pb1’
. When I printed out the formulas, it was good for other compounds except the case where I was expecting Cr2 combining with B sites and X sites. I found out that the code override Cr whenever there should be combination of CrCr i.e. Cr1Cr1 which I referred to as Cr2. So, I do not want to manually iput the Cr2, I was expecting the code to loop through and generate it along.

Basically, what I wanted to do is that I have a chemical formula Cr2Ge2Te6 in structural form A2B2X6 and I want to substitute either of the two Cr’s with elements from site A, while I have every possible binary combination of unique elements at site B, and X site can be S, Te, Se. so, for B sites, if there exist B1==B2, I give the count as 2. Therefore, since Cr is also present at site A, which means I should be able to have CrCr combination at site A to be counted as Cr2, but I was getting Cr1Ge2Te6, Cr1Ge1Sb1Te6 etc, instead of Cr2Ge2Te6, Cr2Ge1Sb1Te6.

Thanks! This does not seem to include any questions or code directly related to Materials Project. I’ve moved your question into the pymatgen forum.