初次提交
This commit is contained in:
95
sci_mcp/material_mcp/pymatgen_cal/pymatgen_cal_tools.py
Normal file
95
sci_mcp/material_mcp/pymatgen_cal/pymatgen_cal_tools.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import os
|
||||
import asyncio
|
||||
from pymatgen.core import Structure
|
||||
from ...core.config import material_config
|
||||
from ...core.llm_tools import llm_tool
|
||||
from ..support.utils import read_structure_from_file_name_or_content_string
|
||||
|
||||
@llm_tool(name="calculate_density_Pymatgen", description="Calculate the density of a crystal structure from a file or content string using Pymatgen")
|
||||
async def calculate_density_Pymatgen(structure_source: str) -> str:
|
||||
"""
|
||||
Calculates the density of a structure from a file or content string.
|
||||
|
||||
Args:
|
||||
structure_source (str): The name of the structure file (e.g., POSCAR, CIF) or the content string.
|
||||
|
||||
Returns:
|
||||
str: A Markdown formatted string with the density or an error message if the calculation fails.
|
||||
"""
|
||||
# 使用asyncio.to_thread异步执行可能阻塞的操作
|
||||
|
||||
try:
|
||||
# 使用read_structure_from_file_name_or_content_string函数读取结构
|
||||
structure_content, content_format = read_structure_from_file_name_or_content_string(structure_source)
|
||||
# # 使用pymatgen读取结构
|
||||
structure = Structure.from_str(structure_content,fmt=content_format)
|
||||
density = structure.density
|
||||
|
||||
# 删除临时文件
|
||||
|
||||
return (f"## Density Calculation\n\n"
|
||||
f"- **Structure**: `{structure.composition.reduced_formula}`\n"
|
||||
f"- **Density**: `{density:.2f} g/cm³`\n")
|
||||
except Exception as e:
|
||||
return f"Error: error occurred while calculating density: {str(e)}\n"
|
||||
|
||||
|
||||
@llm_tool(name="get_element_composition_Pymatgen", description="Analyze and retrieve the elemental composition of a crystal structure from a file or content string using Pymatgen")
|
||||
async def get_element_composition_Pymatgen(structure_source: str) -> str:
|
||||
"""
|
||||
Returns the elemental composition of a structure from a file or content string.
|
||||
|
||||
Args:
|
||||
structure_source (str): The name of the structure file (e.g., POSCAR, CIF) or the content string.
|
||||
|
||||
Returns:
|
||||
str: A Markdown formatted string with the elemental composition or an error message if the operation fails.
|
||||
"""
|
||||
# 使用asyncio.to_thread异步执行可能阻塞的操作
|
||||
|
||||
try:
|
||||
# 使用read_structure_from_file_name_or_content_string函数读取结构
|
||||
structure_content, content_format = read_structure_from_file_name_or_content_string(structure_source)
|
||||
|
||||
# 使用pymatgen读取结构
|
||||
structure = Structure.from_str(structure_content, fmt=content_format)
|
||||
composition = structure.composition
|
||||
|
||||
return (f"## Element Composition\n\n"
|
||||
f"- **Structure**: `{structure.composition.reduced_formula}`\n"
|
||||
f"- **Composition**: `{composition}`\n")
|
||||
except Exception as e:
|
||||
return f"Error: error occurred while getting element composition: {str(e)}\n"
|
||||
|
||||
|
||||
|
||||
@llm_tool(name="calculate_symmetry_Pymatgen", description="Determine the space group and symmetry operations of a crystal structure from a file or content string using Pymatgen")
|
||||
async def calculate_symmetry_Pymatgen(structure_source: str) -> str:
|
||||
"""
|
||||
Calculates the symmetry of a structure from a file or content string.
|
||||
|
||||
Args:
|
||||
structure_source (str): The name of the structure file (e.g., POSCAR, CIF) or the content string.
|
||||
|
||||
Returns:
|
||||
str: A Markdown formatted string with the symmetry information or an error message if the operation fails.
|
||||
"""
|
||||
# 使用asyncio.to_thread异步执行可能阻塞的操作
|
||||
|
||||
try:
|
||||
# 使用read_structure_from_file_name_or_content_string函数读取结构
|
||||
structure_content, content_format = read_structure_from_file_name_or_content_string(structure_source)
|
||||
|
||||
|
||||
|
||||
# 使用pymatgen读取结构
|
||||
structure = Structure.from_str(structure_content, fmt=content_format)
|
||||
symmetry = structure.get_space_group_info()
|
||||
|
||||
return (f"## Symmetry Information\n\n"
|
||||
f"- **Structure**: `{structure.composition.reduced_formula}`\n"
|
||||
f"- **Space Group**: `{symmetry[0]}`\n"
|
||||
f"- **Number**: `{symmetry[1]}`\n")
|
||||
except Exception as e:
|
||||
return f"Error: error occurred while calculating symmetry: {str(e)}\n"
|
||||
|
||||
Reference in New Issue
Block a user