Spaces:
Running
Running
File size: 5,392 Bytes
79edee4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
from itertools import product
from pathlib import Path
import numpy as np
import pandas as pd
from dask.distributed import Client
from dask_jobqueue import SLURMCluster
from mlip_arena.models import MLIPEnum
from mlip_arena.tasks import ELASTICITY, OPT, PHONON
from mlip_arena.tasks.optimize import run as OPT
from mlip_arena.tasks.utils import get_calculator
from numpy import linalg as LA
from prefect import flow, task
from prefect_dask import DaskTaskRunner
from tqdm.auto import tqdm
from ase.db import connect
select_models = [
"ALIGNN",
"CHGNet",
"M3GNet",
"MACE-MP(M)",
"MACE-MPA",
"MatterSim",
"ORBv2",
"SevenNet",
]
def elastic_tensor_to_voigt(C):
"""
Convert a rank-4 (3x3x3x3) elastic tensor into a rank-2 (6x6) tensor using Voigt notation.
Parameters:
C (numpy.ndarray): A 3x3x3x3 elastic tensor.
Returns:
numpy.ndarray: A 6x6 elastic tensor in Voigt notation.
"""
# voigt_map = {
# (0, 0): 0, (1, 1): 1, (2, 2): 2, # Normal components
# (1, 2): 3, (2, 1): 3, # Shear components
# (0, 2): 4, (2, 0): 4,
# (0, 1): 5, (1, 0): 5
# }
voigt_map = {
(0, 0): 0,
(1, 1): 1,
(2, 2): -1, # Normal components
(1, 2): -1,
(2, 1): -1, # Shear components
(0, 2): -1,
(2, 0): -1,
(0, 1): 2,
(1, 0): 2,
}
C_voigt = np.zeros((3, 3))
for i in range(3):
for j in range(3):
for k in range(3):
for l in range(3):
alpha = voigt_map[(i, j)]
beta = voigt_map[(k, l)]
if alpha == -1 or beta == -1:
continue
factor = 1
# if alpha in [3, 4, 5]:
if alpha == 2:
factor = factor * (2**0.5)
if beta == 2:
factor = factor * (2**0.5)
C_voigt[alpha, beta] = C[i, j, k, l] * factor
return C_voigt
# -
@task
def run_one(model, row):
if Path(f"{model.name}.pkl").exists():
df = pd.read_pickle(f"{model.name}.pkl")
# if row.key_value_pairs.get('uid', None) in df['uid'].unique():
# pass
else:
df = pd.DataFrame(columns=["model", "uid", "eigenvalues", "frequencies"])
atoms = row.toatoms()
# print(data := row.key_value_pairs)
calc = get_calculator(model)
result_opt = OPT(
atoms,
calc,
optimizer="FIRE",
criterion=dict(fmax=0.05, steps=500),
symmetry=True,
)
atoms = result_opt["atoms"]
result_elastic = ELASTICITY(
atoms,
calc,
optimizer="FIRE",
criterion=dict(fmax=0.05, steps=500),
pre_relax=False,
)
elastic_tensor = elastic_tensor_to_voigt(result_elastic["elastic_tensor"])
eigenvalues, eigenvectors = LA.eig(elastic_tensor)
outdir = Path(f"{model.name}") / row.key_value_pairs.get(
"uid", atoms.get_chemical_formula()
)
outdir.mkdir(parents=True, exist_ok=True)
np.savez(outdir / "elastic.npz", tensor=elastic_tensor, eigenvalues=eigenvalues)
result_phonon = PHONON(
atoms,
calc,
supercell_matrix=(2, 2, 1),
outdir=outdir,
)
frequencies = result_phonon["phonon"].get_frequencies(q=(0, 0, 0))
new_row = pd.DataFrame(
[
{
"model": model.name,
"uid": row.key_value_pairs.get("uid", None),
"eigenvalues": eigenvalues,
"frequencies": frequencies,
}
]
)
df = pd.concat([df, new_row], ignore_index=True)
df.drop_duplicates(subset=["model", "uid"], keep="last", inplace=True)
df.to_pickle(f"{model.name}.pkl")
@flow
def run_all():
import random
random.seed(0)
futures = []
with connect("c2db.db") as db:
random_indices = random.sample(range(1, len(db) + 1), 1000)
for row, model in tqdm(
product(db.select(filter=lambda r: r["id"] in random_indices), MLIPEnum)
):
if model.name not in select_models:
continue
future = run_one.submit(model, row)
futures.append(future)
return [f.result(raise_on_failure=False) for f in futures]
# +
if __name__ == "__main__":
nodes_per_alloc = 1
gpus_per_alloc = 1
ntasks = 1
cluster_kwargs = dict(
cores=1,
memory="64 GB",
processes=1,
shebang="#!/bin/bash",
account="matgen",
walltime="00:30:00",
# job_cpu=128,
job_mem="0",
job_script_prologue=[
"source ~/.bashrc",
"module load python",
"source activate /pscratch/sd/c/cyrusyc/.conda/dev",
],
job_directives_skip=["-n", "--cpus-per-task", "-J"],
job_extra_directives=[
"-J c2db",
"-q regular",
f"-N {nodes_per_alloc}",
"-C gpu",
f"-G {gpus_per_alloc}",
],
)
cluster = SLURMCluster(**cluster_kwargs)
print(cluster.job_script())
cluster.adapt(minimum_jobs=25, maximum_jobs=50)
client = Client(cluster)
# -
run_all.with_options(
task_runner=DaskTaskRunner(address=client.scheduler.address), log_prints=True
)()
|