!pip install pymatgen==2022.2.10
!pip install tensorflow==2.8.0
!pip install megnet==1.3.0
!pip install monty==2022.1.19
!pip install openpyxl==3.0.9
import numpy as np
from monty.json import MontyDecoder
from monty.serialization import loadfn
data = loadfn('data/bulk_moduli.json')
structures = data['structures']
# targets = np.log10(data['bulk_moduli'])
targets = (data['bulk_moduli'])
type(structures[0])
for (k,v) in enumerate(structures):
print(v.formula)
import matplotlib.pyplot as plt
%matplotlib inline
plt.hist(targets)
from megnet.data.crystal import CrystalGraph
from megnet.data.graph import GaussianDistance
from megnet.models import MEGNetModel
model = MEGNetModel(10, 2, nblocks=1, lr=1e-2,
n1=4, n2=4, n3=4, npass=1, ntarget=1,
graph_converter=CrystalGraph(bond_converter=GaussianDistance(np.linspace(0, 5, 10), 0.5)))
model.train(structures, targets, epochs=10)
# model.save_model('data/saved_model')
# model.model.save_weights('data/saved_model_weights')
model.from_file('data/saved_model')
model.model.load_weights('data/saved_model/variables/variables')
model.train(structures, targets, epochs=10)
# from pymatgen import MPRester
from pymatgen.ext.matproj import MPRester
mpr = MPRester(api_key='DzQwLTxF5Cus3PkT')
# predicted_K = 10 ** model.predict_structure(structure).ravel()
structure = mpr.get_structure_by_material_id('mp-1143') # Al2O2: 232
predicted_K = model.predict_structure(structure).ravel()
print('The predicted K for {} is {} GPa'.format(structure.formula, predicted_K[0]))
# predicted_K = 10 ** model.predict_structure(structure).ravel()
structure = mpr.get_structure_by_material_id('mp-1215')# TiO2: 184
# https://materialsproject.org/materials/mp-1215/
predicted_K = model.predict_structure(structure).ravel()
print('The predicted K for {} is {} GPa'.format(structure.formula, predicted_K[0]))
structure = mpr.get_structure_by_material_id('mp-1025377')
# https://materialsproject.org/materials/mp-1025377/ # Cd(AgI2)2:20
predicted_K = model.predict_structure(structure).ravel()
print('The predicted K for {} is {} GPa'.format(structure.formula, predicted_K[0]))
structure = mpr.get_structure_by_material_id('mp-546794')
# https://materialsproject.org/materials/mp-546794/ # SiO2: 29
predicted_K = model.predict_structure(structure).ravel()
print('The predicted K for {} is {} GPa'.format(structure.formula, predicted_K[0]))
structure = mpr.get_structure_by_material_id('mp-18732')# TiNiO3: 180
# https://materialsproject.org/materials/mp-18732/
predicted_K = model.predict_structure(structure).ravel()
print('The predicted K for {} is {} GPa'.format(structure.formula, predicted_K[0]))