Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Equi(Kit)Script for workflows that compute, transform, join representations and estimate properties #10

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/h5o2+.extxyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
7
Lattice='100 0 0 0 100 0 0 0 100'
O 5.40450e-01 -9.74850e-01 -2.16580e-01
O 2.92484e+00 -8.39252e-01 1.52397e-01
H 1.83860e-01 -1.25804e+00 -1.07875e+00
H 1.74329e+00 -9.09278e-01 -1.00395e-01
H 3.29707e+00 -1.59324e+00 6.47253e-01
H 3.53033e+00 -6.08138e-01 -5.76078e-01
H 4.23316e-02 -1.94860e-01 9.22810e-02
Binary file added examples/multi_spectra_script-water.pickle
Binary file not shown.
227 changes: 227 additions & 0 deletions examples/multi_spectra_script.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
{
"cells": [
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't we said that we didn't want notebooks in the examples?

"cell_type": "code",
"execution_count": 1,
"id": "c86f17cd",
"metadata": {},
"outputs": [],
"source": [
"import rascaline\n",
"rascaline._c_lib._get_library()\n",
"\n",
"from copy import deepcopy\n",
"\n",
"import numpy as np\n",
"\n",
"from equisolve.numpy.scripts import MultiSpectraScript\n",
"from equisolve.numpy.models.linear_model import Ridge\n",
"from equisolve.numpy.preprocessing import StandardScaler\n",
"from equisolve.utils.convert import ase_to_tensormap\n",
"\n",
"import ase.io\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a199029f",
"metadata": {},
"outputs": [],
"source": [
"frames = ase.io.read(\"water_converted.extxyz\", \":20\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "baca7fb8",
"metadata": {},
"outputs": [],
"source": [
"HYPERS = {\n",
" \"cutoff\": 3.0,\n",
" \"max_radial\": 6,\n",
" \"max_angular\": 4,\n",
" \"atomic_gaussian_width\": 0.3,\n",
" \"center_atom_weight\": 1.0,\n",
" \"radial_basis\": {\"Gto\": {}},\n",
" \"cutoff_function\": {\"ShiftedCosine\": {\"width\": 0.5}},\n",
"}\n",
"multi_spectra_hypers = {}\n",
"#multi_spectra_hypers['Composition'] = {}\n",
"multi_spectra_hypers['SoapRadialSpectrum'] = deepcopy(HYPERS)\n",
"multi_spectra_hypers['SoapRadialSpectrum'].pop('max_angular')\n",
"multi_spectra_hypers['SoapPowerSpectrum'] = deepcopy(HYPERS)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0233bbb9",
"metadata": {},
"outputs": [],
"source": [
"y = ase_to_tensormap(frames, energy=\"TotEnergy\", forces=\"force\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "cdf7caa4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"671.9636307759672"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Because all frames have the same species, we cannot scale, so we just subtract the mean\n",
"#transformer_composition = StandardScaler(parameter_keys=[\"values\", \"positions\"], with_mean=True, with_std=False)\n",
"# THIS IS BUGGY, something wrong in the standardizer when setting these flags to False\n",
"\n",
"transformer = StandardScaler(parameter_keys=[\"values\", \"positions\"])\n",
"estimator = Ridge(parameter_keys=[\"values\", \"positions\"])\n",
"script = MultiSpectraScript(multi_spectra_hypers,\n",
" #transformer_X=transformer,\n",
" #transformer_y=transformer,\n",
" estimator=estimator)\n",
"Xi = script.compute(systems=frames, gradients=[\"positions\"])\n",
"script.fit(Xi, y, **{\"estimator_kwargs\": {\"alpha\": 1e-15}})\n",
"script.score(Xi, y)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "902e9517",
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"with open(\"multi_spectra_script-water.pickle\", \"wb\") as file:\n",
" pickle.dump(script, file)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3b640034",
"metadata": {},
"outputs": [],
"source": [
"# RESTART NOTEBOOK"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "99a94bf0",
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"with open(\"multi_spectra_script-water.pickle\", \"rb\") as file:\n",
" script = pickle.load(file)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5369b107",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-29201.186821558364\n",
"\n",
"[[-295.60671847 -4.62718863 -55.59864334]\n",
" [ 298.54620907 4.04942317 37.15507667]\n",
" [-221.68776379 -150.28232975 -242.81915489]\n",
" [ -7.67013877 1.6028996 48.61326349]\n",
" [ 176.03569875 -146.94124088 179.13481445]\n",
" [ 286.69810363 136.65180697 -172.50009028]\n",
" [-220.83316507 156.29491821 107.1942928 ]]\n"
]
}
],
"source": [
"# in ipi\n",
"\n",
"import ase.io\n",
"import numpy as np\n",
"\n",
"structure = ase.io.read(\"h5o2+.extxyz\", \"0\")\n",
"\n",
"Xi = script.compute(systems=structure, gradients=[\"positions\"])\n",
"y_pred = script.forward(Xi) # implicitely done in score function\n",
"energy = y_pred.block().values[0][0]\n",
"forces = np.array(y_pred.block().gradient(\"positions\").data.reshape(-1, 3))\n",
"\n",
"print(energy)\n",
"print()\n",
"print(forces)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "995fe798",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<equisolve.numpy.scripts.md_calculator.GenericMDCalculator at 0x7f8d31a0d6a0>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from equisolve.numpy.scripts import GenericMDCalculator\n",
"\n",
"from ipi.utils.mathtools import det_ut3x3\n",
"from ipi.utils.units import unit_to_internal, unit_to_user\n",
"\n",
"\n",
"md_calc = GenericMDCalculator(\"multi_spectra_script-water.pickle\",\n",
" is_periodic=True,\n",
" structure_template=\"h5o2+.extxyz\",\n",
" atomic_numbers=[1,8])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading