materialsvirtuallab / matgl

Graph deep learning library for materials
BSD 3-Clause "New" or "Revised" License
286 stars 65 forks source link

[Bug]: Issue with structures relaxed by the CHGNet #414

Closed zdcao121 closed 1 month ago

zdcao121 commented 1 month ago

Email (Optional)

zdcao@iphy.ac.cn

Version

v1.1.3

Which OS(es) are you using?

What happened?

If the structure is relaxed by the CHGNet, the final_magmom will be added to the site properties like 'final_magmom': array([-0.1934694], dtype=float32). It will cause problems when reloading it from string of the dictionary format.

Code snippet

# assume `structs` is the structures relaxed by CHGNet
output['struct'] = [s.as_dict() for s in struct]

# reload the struct
reload_structs = [Structure.from_dict(literal_eval(s)) for s in output['sturct']]

Log output

ValueError: malformed node or string: {'@module': 'pymatgen.core.structure', '@class': 'Structure', 'charge': 0, 'lattice': {'matrix': [[5.63485911306041, -0.22753159059944444, 0.0476352802211047], [-0.24706601371743264, 5.7201431539883725, -0.045668370314641285], [0.06275913697403292, -0.11238518801363732, 7.851371047116878]], 'pbc': (True, True, True), 'a': 5.639652202812015, 'b': 5.7256584701937525, 'c': 7.85242615114996, 'alpha': 91.29604180095923, 'beta': 89.02544845372715, 'gamma': 94.78905438944795, 'volume': 252.5815541487389}, 'properties': {}, 'sites': [{'species': [{'element': 'La', 'occu': 1}], 'abc': [0.5111679799159602, 0.4924952790960808, 0.762582126262501], 'properties': {'final_magmom': array([0.01850872], dtype=float32)}, 'label': 'La', 'xyz': [2.8065397006692634, 2.6151336999234056, 5.989173400365608]}, {'species': [{'element': 'La', 'occu': 1}], 'abc': [0.48472123552021157, 0.5089051059374287, 0.227156200527532], 'properties': {'final_magmom': array([0.01698504], dtype=float32)}, 'label': 'La', 'xyz': [2.6198588424839655, 2.775191671737718, 1.7833365810451036]}, {'species': [{'element': 'La', 'occu': 1}], 'abc': [1.0143303789037035, 0.9903911214691051, 0.76655325580044], 'properties': {'final_magmom': array([0.02590303], dtype=float32)}, 'label': 'La', 'xyz': [5.519025013595714, 5.348237556961473, 6.021582402009026]}, {'species': [{'element': 'La', 'occu': 1}], 'abc': [-0.005171434126463238, -0.003726316180796917, 0.22522286169617814], 'properties': {'final_magmom': array([0.02108149], dtype=float32)}, 'label': 'La', 'xyz': [-0.014084864203578604, -0.04545011101540157, 1.768232087543677]}, {'species': [{'element': 'Ti', 'occu': 1}], 'abc': [-0.0010304163640166356, 0.4944574403229479, -0.0033578678897668683], 'properties': {'final_magmom': array([0.30656403], dtype=float32)}, 'label': 'Ti', 'xyz': [-0.12818061666337638, 2.8289791690903257, -0.048994016391501416]}, {'species': [{'element': 'Ti', 'occu': 1}], 'abc': [0.5058197146133805, -0.00234752275142351, -0.002214095005376556], 'properties': {'final_magmom': array([0.31067306], dtype=float32)}, 'label': 'Ti', 'xyz': [2.850663866851412, -0.12826929893446734, 0.006818389964522064]}, {'species': [{'element': 'Ti', 'occu': 1}], 'abc': [0.5011143857325214, 0.006697693938605176, 0.5004389203254911], 'properties': {'final_magmom': array([0.35148638], dtype=float32)}, 'label': 'Ti', 'xyz': [2.853461305335902, -0.13194950727768434, 3.9526965013141435]}, {'species': [{'element': 'Ti', 'occu': 1}], 'abc': [-0.0037640187590753348, 0.5035617873869248, 0.5002496950021401], 'properties': {'final_magmom': array([0.34851813], dtype=float32)}, 'label': 'Ti', 'xyz': [-0.11422747974656133, 2.8250812878801845, 3.904469825397817]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.26101524350310523, 0.7365770702141864, 0.9424155502534851], 'properties': {'final_magmom': array([-0.02897738], dtype=float32)}, 'label': 'O', 'xyz': [1.3479461495724872, 4.048023523243315, 7.378049425471369]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.7438602658759278, 0.255325122131338, 0.07264609080551317], 'properties': {'final_magmom': array([-0.02758966], dtype=float32)}, 'label': 'O', 'xyz': [4.133024843851096, 1.2830801953487092, 0.59414512401885]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.04711364966377713, 0.5695295689382355, 0.25902921210631297], 'properties': {'final_magmom': array([-0.00025372], dtype=float32)}, 'label': 'O', 'xyz': [0.14102382766854846, 3.2179597744054598, 2.009969240933566]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.9624670230590084, 0.41989036136051316, 0.7408003953659988], 'properties': {'final_magmom': array([0.00036235], dtype=float32)}, 'label': 'O', 'xyz': [5.366117431607655, 2.0995863315921306, 5.842970453702167]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.21685709503420825, 0.21119467193754107, -0.001180549322295277], 'properties': {'final_magmom': array([0.02833025], dtype=float32)}, 'label': 'O', 'xyz': [1.1697060622147242, 1.158854593333983, -0.008583798765396155]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.78191116506291, 0.7758669145143305, 1.0019831163128596], 'properties': {'final_magmom': array([0.02627419], dtype=float32)}, 'label': 'O', 'xyz': [4.2771525039564695, 4.14755226745769, 7.868755209007593]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.5657325673228738, 0.056423023661917336, 0.7438673476294612], 'properties': {'final_magmom': array([0.00108285], dtype=float32)}, 'label': 'O', 'xyz': [3.2205675737567114, 0.11042606990961694, 5.864750637909343]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.45124345323653736, 0.94127658217669, 0.25876442656042453], 'properties': {'final_magmom': array([0.00204121], dtype=float32)}, 'label': 'O', 'xyz': [2.3263756638053, 5.252483368154838, 2.010164067539835]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.7478161103066214, 0.7358451703050422, -0.023728428494675748], 'properties': {'final_magmom': array([0.02958317], dtype=float32)}, 'label': 'O', 'xyz': [4.030546915420087, 4.041654648159482, -0.1842831162400988]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.2609090713725044, 0.2560771907545315, 1.006569577981355], 'properties': {'final_magmom': array([0.03634532], dtype=float32)}, 'label': 'O', 'xyz': [1.470089325798231, 1.2923096223056556, 7.903685090222012]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.6745637657667664, 0.32626400035781056, 0.46330898827382044], 'properties': {'final_magmom': array([0.01250856], dtype=float32)}, 'label': 'O', 'xyz': [3.7495399091396817, 1.6607231536983653, 3.654843865222714]}, {'species': [{'element': 'O', 'occu': 1}], 'abc': [0.27932273551392944, 0.7252957311654512, 0.5588931553778906], 'properties': {'final_magmom': array([0.01502733], dtype=float32)}, 'label': 'O', 'xyz': [1.4298239887225197, 4.022429352594295, 4.368260081305604]}]}

Code of Conduct

kenko911 commented 1 month ago

Hi @zdcao121, I am unable to reproduce your error. I used the following code to test it:

import matgl from matgl.ext.ase import Relaxer from pymatgen.core import Structure, Lattice

pot = matgl.load_model("pretrained_models/CHGNet-MPtrj-2023.12.1-PES-2.7M/") relaxer = Relaxer(pot) struct = Structure(Lattice.cubic(4.0), ["Mo", "S"], [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]]) out = relaxer.relax(struct) o_dict = out['final_structure'].as_dict() print(Structure.from_dict(o_dict))

I don't understand why you are using "literal_eval(s)" but not just "s" in your code. I hope it helps

zdcao121 commented 1 month ago

Thanks for your reply! @kenko911 Because I saved the Structure.as_dict() in a csv file, I need to convert it from a string to a dictionary format when reloading it. The following code can reproduce the error.

pot = matgl.load_model("pretrained_models/CHGNet-MPtrj-2023.12.1-PES-2.7M/")
relaxer = Relaxer(pot)
struct = Structure(Lattice.cubic(4.0), ["Mo", "S"], [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]])
out = relaxer.relax(struct)
o_dict = str(out['final_structure'].as_dict())  
print(Structure.from_dict(literal_eval(o_dict)))
kenko911 commented 1 month ago

Hi @zdcao121, I would recommend using json instead of csv since we have monty to handle numpy arrays in json. Please read the documentation carefully (https://pymatgen.org/usage.html) and figure it out by yourself. It should be very easy to follow.

zdcao121 commented 1 month ago

Thanks for the suggestion. I will check out the documentation.