Open hongyi-zhao opened 2 weeks ago
To update the MAGMOMs via user_incar_settings
, you have to specify it as a dict (doesn't make the most sense I know). The dict should be {species: magmom}, like {"Zn2+": 0.5}
The alternative is to add the magmoms to the site properties of a structure, and atomate2 will automatically read those in as MAGMOM:
structure.add_site_property("magmom", incar_settings.pop("magmom") )
In general, it should be used this way:
def apply_magmoms_from_incar(structure, incar_settings):
"""
Apply magnetic moments from INCAR settings to structure, handling multiple species.
Args:
structure: pymatgen Structure object
incar_settings: dict containing INCAR settings including MAGMOM
"""
if "MAGMOM" not in incar_settings:
print("No MAGMOM settings found in INCAR")
return structure
magmoms = incar_settings.pop("MAGMOM")
# Check if number of MAGMOMs matches number of sites
if len(magmoms) == len(structure):
# Direct 1:1 mapping case
structure.add_site_property("magmom", magmoms)
else:
# Need to expand magmoms based on unique species
# Get unique species and their counts
species_counts = structure.composition.as_dict()
# Create expanded magmom list matching structure sites
site_magmoms = []
magmom_idx = 0
# Iterate through sites and assign magmoms
for site in structure:
site_magmoms.append(magmoms[magmom_idx])
# Check if we need to move to next magmom value
next_species_sites = structure.indices_from_symbol(site.species_string)
if len(site_magmoms) == max(next_species_sites) + 1:
magmom_idx += 1
structure.add_site_property("magmom", site_magmoms)
return structure
# 使用示例:
structure = apply_magmoms_from_incar(structure, incar_settings)
# 验证结果
print("Site properties:", structure.site_properties)
print("\nDetailed site information:")
for i, site in enumerate(structure):
print(f"Site {i}: {site.species_string} at {site.frac_coords}, "
f"magmom = {site.properties.get('magmom')}")
The output:
No MAGMOM settings found in INCAR
Site properties: {'magmom': [-0.002, -0.002, -0.002, -0.002]}
Detailed site information:
Site 0: Pt at [0. 0. 0.], magmom = -0.002
Site 1: Pt at [0. 0.5 0.5], magmom = -0.002
Site 2: Pt at [0.5 0. 0.5], magmom = -0.002
Site 3: Pt at [0.5 0.5 0. ], magmom = -0.002
The updated result of structure.site_properties
:
In [24]: structure.site_properties
Out[24]: {'magmom': [-0.002, -0.002, -0.002, -0.002]}
The testing code below is adapted from my example here:
But the above code will trigger the following error:
Is there a proper way to handle
MAGMOM
settings when copying from MP calculations without requiring further user adjustment, say, the one done by the code snippetConvert MAGMOM list to dictionary format
shown above?Regards, Zhao