### Install Dependencies for DiffCSP Source: https://github.com/jiaor17/diffcsp/blob/main/README.md List of Python packages and their versions required for the project. Ensure these are installed before proceeding. ```shell python==3.8.13 torch==1.9.0 torch-geometric==1.7.2 pytorch_lightning==1.3.8 pymatgen==2023.8.10 ``` -------------------------------- ### Train for Ab Initio Generation Task Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Command to start training for the Ab Initio Generation task. Uses a specific model configuration and requires dataset and experiment name. ```shell python diffcsp/run.py data= model=diffusion_w_type expname= ``` -------------------------------- ### DiffCSP Environment Configuration Source: https://context7.com/jiaor17/diffcsp/llms.txt Bash script and example .env file contents for setting up the DiffCSP environment. Specifies essential environment variables like PROJECT_ROOT, HYDRA_JOBS, and WABDB_DIR, crucial for training and output management. ```bash # Copy template and configure cp .env.template .env # .env file contents: PROJECT_ROOT=/absolute/path/to/DiffCSP HYDRA_JOBS=/absolute/path/to/hydra_outputs WABDB_DIR=/absolute/path/to/wandb_logs # Directory structure after training: # HYDRA_JOBS/ # singlerun/ # 2024-01-15/ # perov_csp_experiment/ # hparams.yaml # Training configuration # lattice_scaler.pt # Lattice scaling parameters ``` -------------------------------- ### DiffCSP Dataset Configuration Source: https://context7.com/jiaor17/diffcsp/llms.txt Example YAML configuration for dataset parameters in DiffCSP, specifying root path, property to predict, and data preprocessing options like Niggli reduction and graph construction method. Includes batch size settings for training and validation. ```yaml # Example: conf/data/perov_5.yaml root_path: ${oc.env:PROJECT_ROOT}/data/perov_5 prop: heat_ref # Property to predict (formation enthalpy) num_targets: 1 niggli: true # Use Niggli reduced cell primitive: false # Don't convert to primitive cell graph_method: crystalnn # Graph construction method lattice_scale_method: scale_length max_atoms: 20 eval_model_name: perovskite # Pre-trained property model for evaluation # Batch sizes for different stages datamodule: batch_size: train: 1024 val: 1024 test: 256 # Available datasets: # - perov_5: Perovskite structures (5 elements, ~19k structures) # - carbon_24: Carbon allotropes (up to 24 atoms) # - mp_20: Materials Project subset (up to 20 atoms) # - mpts_52: Materials Project time-split (up to 52 atoms) ``` -------------------------------- ### Optimize Crystal Structures for Target Properties Source: https://context7.com/jiaor17/diffcsp/llms.txt Run property optimization using a trained energy model to guide the diffusion process. Custom parameters like `--step_lr`, `--aug`, and `--num_candidates` can be adjusted. ```bash python scripts/optimization.py --model_path /path/to/energy_model --uncond_path /path/to/csp_model ``` ```bash python scripts/optimization.py \ --model_path /path/to/energy_model \ --uncond_path /path/to/csp_model \ --step_lr 1e-5 \ --aug 50 \ --test_samples 100 \ --num_candidates 10 ``` ```bash python scripts/optimization.py --model_path /path/to/energy_model --uncond_path /path/to/csp_model --aug 100 ``` -------------------------------- ### Configure Environment Variables Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Set up essential environment variables by renaming the template file and specifying absolute paths for project directories. ```shell PROJECT_ROOT: the absolute path of this repo HYDRA_JOBS: the absolute path to save hydra outputs WABDB_DIR: the absolute path to save wabdb outputs ``` -------------------------------- ### Compute Metrics for Ab Initio Generation Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Calculates metrics for the ab initio generation task. Requires model path, dataset, and ground truth file. ```shell python scripts/compute_metrics.py --root_path --tasks gen --gt_file data//test.csv ``` -------------------------------- ### Sample from Arbitrary Composition Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Generates crystal structures for a specified chemical formula. Requires model path, save path, formula, and number of evaluations. ```shell python scripts/sample.py --model_path --save_path --formula --num_evals ``` -------------------------------- ### Sample Structures for Arbitrary Compositions Source: https://context7.com/jiaor17/diffcsp/llms.txt Generate crystal structures for user-specified chemical formulas. Outputs are saved as CIF files. Adjust `--num_evals` and `--batch_size` as needed. ```bash python scripts/sample.py --model_path /path/to/trained_model --save_path ./outputs --formula Sr1Ti1O3 --num_evals 10 ``` ```bash python scripts/sample.py --model_path /path/to/trained_model --save_path ./outputs --formula La2CuO4 --num_evals 50 --batch_size 100 ``` ```bash python scripts/sample.py --model_path /path/to/trained_model --save_path ./outputs --formula Li1Fe1P1O4 --num_evals 100 ``` ```bash python scripts/sample.py --model_path /path/to/trained_model --save_path ./outputs --formula Ca1Ti1O3 --num_evals 20 --step_lr 1e-6 ``` -------------------------------- ### Evaluate Ab Initio Generation Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Script to evaluate the ab initio generation task. Requires the path to the trained model and the dataset. ```shell python scripts/generation.py --model_path --dataset ``` -------------------------------- ### Perform Property Optimization Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Initiates the property optimization process using a trained energy model and an unconditional model path. ```shell python scripts/optimization.py --model_path --uncond_path ``` -------------------------------- ### Compute Optimization Metrics Source: https://context7.com/jiaor17/diffcsp/llms.txt Calculate success rates for optimization tasks using the specified root path. Ensure the tasks parameter matches your optimization goals. ```bash python scripts/compute_metrics.py --root_path /path/to/energy_model --tasks opt ``` -------------------------------- ### Compute Metrics for Stable Structure Prediction (Multiple Samples) Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Computes metrics for CSP task with multi-evaluation enabled. Requires model path, dataset, and ground truth file. ```shell python scripts/compute_metrics.py --root_path --tasks csp --gt_file data//test.csv --multi_eval ``` -------------------------------- ### Evaluate Property Optimization Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Computes metrics for the property optimization task. Requires the root path of the energy model. ```shell python scripts/compute_metrics.py --root_path --tasks opt ``` -------------------------------- ### Train DiffCSP Models with Hydra Source: https://context7.com/jiaor17/diffcsp/llms.txt Use these commands to train different DiffCSP models. Specify the dataset, experiment name, and model architecture using Hydra configuration. Supports automatic checkpoint resumption and Weights & Biases logging. ```bash python diffcsp/run.py data=perov_5 expname=perov_csp_experiment ``` ```bash python diffcsp/run.py data=mp_20 expname=mp20_csp_experiment ``` ```bash python diffcsp/run.py data=mp_20 model=diffusion_w_type expname=mp20_gen_experiment ``` ```bash python diffcsp/run.py data=perov_5 model=energy expname=perov_energy_model data.datamodule.batch_size.test=100 ``` ```bash python diffcsp/run.py data=carbon_24 expname=carbon_csp train.pl_trainer.gpus=1 ``` ```bash python diffcsp/run.py data=perov_5 expname=test_run train.pl_trainer.fast_dev_run=True ``` -------------------------------- ### Generate Crystal Structures with Custom Sampling Source: https://context7.com/jiaor17/diffcsp/llms.txt Use this script to generate crystal structures with custom sampling parameters. Ensure the model path and dataset are correctly specified. ```bash python scripts/generation.py --model_path /path/to/gen_model --dataset perov_5 --step_lr 1e-6 --num_batches_to_samples 50 ``` -------------------------------- ### Train for CSP Task Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Command to initiate training for the Crystal Structure Prediction (CSP) task. Replace placeholders with your dataset and experiment name. ```shell python diffcsp/run.py data= expname= ``` -------------------------------- ### Generate Novel Crystal Structures Source: https://context7.com/jiaor17/diffcsp/llms.txt Generate novel crystal structures from scratch using a model trained for ab initio generation. Specify the model path, dataset, and batch size. Samples structures following the learned distribution of atom counts. ```bash python scripts/generation.py --model_path /path/to/gen_model --dataset mp_20 ``` ```bash python scripts/generation.py --model_path /path/to/gen_model --dataset carbon_24 --batch_size 256 ``` -------------------------------- ### Compute Evaluation Metrics Source: https://context7.com/jiaor17/diffcsp/llms.txt Compute metrics such as match rate and RMS distance by comparing predicted structures to ground truth. Supports CSP, generation, and optimization tasks. Multi-sample evaluation can be enabled. ```bash python scripts/compute_metrics.py --root_path /path/to/trained_model --tasks csp --gt_file data/perov_5/test.csv ``` ```bash python scripts/compute_metrics.py --root_path /path/to/trained_model --tasks csp --gt_file data/mp_20/test.csv --multi_eval ``` ```bash python scripts/compute_metrics.py --root_path /path/to/trained_model --tasks gen --gt_file data/mp_20/test.csv ``` ```bash python scripts/compute_metrics.py --root_path /path/to/trained_model --tasks opt ``` -------------------------------- ### Compute Metrics for Stable Structure Prediction (Single Sample) Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Calculates evaluation metrics for the CSP task using the specified model path, dataset, and ground truth file. ```shell python scripts/compute_metrics.py --root_path --tasks csp --gt_file data//test.csv ``` -------------------------------- ### Evaluate Stable Structure Prediction (Single Sample) Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Script to evaluate the model's performance on predicting stable structures for a single sample. Requires model path and dataset. ```shell python scripts/evaluate.py --model_path --dataset ``` -------------------------------- ### Load Pre-trained DiffCSP Model and Run Inference Source: https://context7.com/jiaor17/diffcsp/llms.txt Programmatically load a trained DiffCSP model and perform inference in Python. This snippet shows how to load the model, move it to GPU, and generate crystal structures from a batch. ```python import torch from pathlib import Path from scripts.eval_utils import load_model, get_crystals_list, lattices_to_params_shape # Load a trained model with test data model_path = Path('/path/to/trained_model') model, test_loader, cfg = load_model(model_path, load_data=True) # Move model to GPU if available if torch.cuda.is_available(): model.to('cuda') model.eval() # Run inference on a batch for batch in test_loader: if torch.cuda.is_available(): batch.cuda() # Generate crystal structures outputs, trajectory = model.sample(batch, step_lr=1e-5) # Extract results frac_coords = outputs['frac_coords'].detach().cpu() atom_types = outputs['atom_types'].detach().cpu() lattices = outputs['lattices'].detach().cpu() num_atoms = outputs['num_atoms'].detach().cpu() # Convert lattices to lengths and angles lengths, angles = lattices_to_params_shape(lattices) # Get list of crystal dictionaries crystals = get_crystals_list(frac_coords, atom_types, lengths, angles, num_atoms) # Each crystal dict contains: # - 'frac_coords': numpy array of shape (n_atoms, 3) # - 'atom_types': numpy array of atomic numbers # - 'lengths': numpy array [a, b, c] # - 'angles': numpy array [alpha, beta, gamma] for crystal in crystals: print(f"Atoms: {crystal['atom_types']}") print(f"Lattice: {crystal['lengths']}, {crystal['angles']}") break # Process first batch only for demo ``` -------------------------------- ### Evaluate Trained Diffusion Models Source: https://context7.com/jiaor17/diffcsp/llms.txt Evaluate trained models on a test set to predict crystal structures. Supports single-sample and multi-sample evaluation modes. Custom step learning rate and output labels can be specified. ```bash python scripts/evaluate.py --model_path /path/to/trained_model --dataset perov_5 ``` ```bash python scripts/evaluate.py --model_path /path/to/trained_model --dataset mp_20 --num_evals 20 ``` ```bash python scripts/evaluate.py --model_path /path/to/trained_model --dataset carbon_24 --step_lr 5e-6 ``` ```bash python scripts/evaluate.py --model_path /path/to/trained_model --dataset mpts_52 --label experiment_v1 ``` -------------------------------- ### Evaluate Stable Structure Prediction (Multiple Samples) Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Evaluates stable structure prediction for multiple samples by setting the number of evaluations. Requires model path and dataset. ```shell python scripts/evaluate.py --model_path --dataset --num_evals 20 ``` -------------------------------- ### Train Time-Dependent Energy Prediction Model Source: https://github.com/jiaor17/diffcsp/blob/main/README.md Command to train a model for predicting time-dependent energy, used as a prerequisite for property optimization. Adjust batch size as needed. ```shell python diffcsp/run.py data= model=energy expname= data.datamodule.batch_size.test=100 ``` -------------------------------- ### Convert Crystal Dictionary to Pymatgen Structure Source: https://context7.com/jiaor17/diffcsp/llms.txt Converts a DiffCSP crystal dictionary containing fractional coordinates, atom types, lattice lengths, and angles into a pymatgen Structure object. Handles potential errors during structure creation. ```python import numpy as np from pymatgen.core.structure import Structure from pymatgen.core.lattice import Lattice from pymatgen.io.cif import CifWriter def crystal_to_structure(crystal_dict): """Convert DiffCSP crystal dict to pymatgen Structure.""" frac_coords = crystal_dict['frac_coords'] atom_types = crystal_dict['atom_types'] lengths = crystal_dict['lengths'] angles = crystal_dict['angles'] try: structure = Structure( lattice=Lattice.from_parameters( *(lengths.tolist() + angles.tolist()) ), species=atom_types, coords=frac_coords, coords_are_cartesian=False ) return structure except Exception as e: print(f"Failed to create structure: {e}") return None # Example usage with generated crystal data crystal_data = { 'frac_coords': np.array([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5], [0.5, 0.5, 0.0], [0.5, 0.0, 0.5], [0.0, 0.5, 0.5]]), 'atom_types': np.array([38, 22, 8, 8, 8]), # Sr, Ti, O, O, O 'lengths': np.array([3.9, 3.9, 3.9]), 'angles': np.array([90.0, 90.0, 90.0]) } structure = crystal_to_structure(crystal_data) if structure: print(f"Formula: {structure.composition.reduced_formula}") print(f"Space group: {structure.get_space_group_info()}") print(f"Volume: {structure.volume:.2f} A^3") print(f"Density: {structure.density:.2f} g/cm^3") # Save to CIF file writer = CifWriter(structure) writer.write_file("predicted_structure.cif") ``` -------------------------------- ### BibTeX Citation for DiffCSP Source: https://github.com/jiaor17/diffcsp/blob/main/README.md BibTeX entry for citing the DiffCSP paper. Include this in your bibliography if you use the project. ```bibtex @article{jiao2023crystal, title={Crystal structure prediction by joint equivariant diffusion}, author={Jiao, Rui and Huang, Wenbing and Lin, Peijia and Han, Jiaqi and Chen, Pin and Lu, Yutong and Liu, Yang}, journal={arXiv preprint arXiv:2309.04475}, year={2023} } ``` -------------------------------- ### DiffCSP Diffusion Model Architecture Source: https://context7.com/jiaor17/diffcsp/llms.txt Defines the core diffusion model for DiffCSP, implementing joint equivariant diffusion over lattice and coordinate spaces. Features a configurable denoising neural network (CSPNet) with options for edge style and cutoff. ```python import torch from diffcsp.pl_modules.diffusion import CSPDiffusion from diffcsp.common.data_utils import lattice_params_to_matrix_torch # Model architecture parameters (from conf/model/diffusion.yaml) model_config = { 'time_dim': 256, # Dimension of time embeddings 'latent_dim': 0, # Additional latent dimension 'cost_coord': 1.0, # Loss weight for coordinates 'cost_lattice': 1.0, # Loss weight for lattice 'timesteps': 1000, # Number of diffusion timesteps 'decoder': { '_target_': 'diffcsp.pl_modules.cspnet.CSPNet', 'hidden_dim': 256, 'num_layers': 6, 'max_atoms': 100, 'act_fn': 'silu', 'dis_emb': 'sin', 'num_freqs': 128, 'edge_style': 'knn', # 'fc' for fully connected, 'knn' for k-nearest 'cutoff': 7.0, 'max_neighbors': 20, 'ln': True, # Layer normalization } } # The diffusion process: # 1. Forward process adds noise to lattice (Gaussian) and coordinates (wrapped normal) # 2. Reverse process denoises using learned score function # 3. PC-sampler (Predictor-Corrector) for high-quality generation # Sampling generates trajectory of structures from noise to prediction # outputs = { # 'frac_coords': tensor of shape (num_atoms, 3), # 'atom_types': tensor of shape (num_atoms,), # 'lattices': tensor of shape (batch_size, 3, 3), # 'num_atoms': tensor of shape (batch_size,) # } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.