Bulk Modulus for Multiple Materials via Materials Project API

Hi, I have previously utilized the “mp.materials.summary.search” functionality to retrieve bulk modulus data for a large number of materials from the Materials Project API. However, it seems that this method is no longer available.

I would appreciate it if you could provide guidance on alternative methods or approaches that can be employed to obtain bulk modulus information for a substantial dataset of materials through the Materials Project API.

Thanks.

Does this existing thread help?

I successfully solved the problem, thank you ! !

heyy can you please share the code you used to retrieve bulk modulus for a large number of materials?

@imconfused Did you try using our API python client mp_api? See our docs. For example,

mpr.materials.summary.search(fields=["bulk_modulus", "shear_modulus"])

There’s also some existing discussion in this thread: Not able to retrieve elasticity data - #13 by Carla_Becker

Sorry for not logging into the forum for so long, this is the code I used

from mp_api.client import MPRester
import csv

class MP(object):
    def __init__(self):
        self.API_KEY = ""

    def get_data(self):
        with MPRester(self.API_KEY) as mpr:
            doc1 = mpr.materials.summary.search(
                band_gap=(3, None), is_stable=True,
                fields=["material_id", "formula_pretty", "band_gap", "bulk_modulus", "shear_modulus"]
            )
            doc2 = mpr.materials.summary.search(
                band_gap=(3, None), theoretical=False,
                fields=["material_id", "formula_pretty", "band_gap", "bulk_modulus", "shear_modulus"]
            )

            # Merge and deduplicate documents
            unique_docs = {doc.material_id: doc for doc in doc1 + doc2}.values()

            mpid = [doc.material_id for doc in unique_docs]
            formula = [doc.formula_pretty for doc in unique_docs]
            bandgap = [doc.band_gap for doc in unique_docs]

            # Check for existence of bulk_modulus and shear_modulus fields
            k_vrh = [doc.bulk_modulus.get("vrh") if doc.bulk_modulus else None for doc in unique_docs]
            g_vrh = [doc.shear_modulus.get("vrh") if doc.shear_modulus else None for doc in unique_docs]

            # Identify if the material is stable and if it's theoretical
            is_stable = [1 if doc.material_id in [d.material_id for d in doc1] else 0 for doc in unique_docs]
            exp = [1 if doc.material_id in [d.material_id for d in doc2] else 0 for doc in unique_docs]

            return mpid, formula, bandgap, k_vrh, g_vrh, is_stable, exp

    def write_to_file(self):
        mpid, formula, bandgap, k_vrh, g_vrh, is_stable, exp = self.get_data()
        headers = [
            "material_id",
            "formula_pretty",
            "band_gap",
            "k_vrh",
            "g_vrh",
            "is_stable",
            "exp",
        ]

        with open('Hv_2_3.csv', 'w', newline='', encoding='utf-8') as f:
            writer = csv.DictWriter(f, fieldnames=headers)
            writer.writeheader()
            writer.writerows(
                {
                    "material_id": material_id,
                    "formula_pretty": formula_pretty,
                    "band_gap": gap,
                    "k_vrh": bulk_modulus,
                    "g_vrh": shear_modulus,
                    "is_stable": stable,
                    "exp": exp,
                } for material_id, formula_pretty, gap, bulk_modulus, shear_modulus, stable, exp in zip(mpid, formula, bandgap, k_vrh, g_vrh, is_stable, exp)
            )

s = MP()
s.write_to_file()