.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/compute-lambda-soap.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_compute-lambda-soap.py: .. _compute-lambda-soap: Computing λ-SOAP features ========================== .. start-body .. GENERATED FROM PYTHON SOURCE LINES 9-18 .. code-block:: Python import chemfiles import numpy as np from metatensor import Labels from featomic import LodeSphericalExpansion, SphericalExpansion from featomic.clebsch_gordan import EquivariantPowerSpectrum .. GENERATED FROM PYTHON SOURCE LINES 19-23 Let's see how to compute the :math:`\lambda`-SOAP descriptor using featomic. First we can read the input systems using chemfiles. You can download the dataset for this example from our :download:`website <../../static/dataset.xyz>`. .. GENERATED FROM PYTHON SOURCE LINES 24-28 .. code-block:: Python with chemfiles.Trajectory("dataset.xyz") as trajectory: systems = [s for s in trajectory] .. GENERATED FROM PYTHON SOURCE LINES 29-34 Featomic also handles systems read by `ASE `_: ``systems = ase.io.read("dataset.xyz", ":")``. Next, define the hyperparameters for the spherical expansion: .. GENERATED FROM PYTHON SOURCE LINES 35-52 .. code-block:: Python HYPERPARAMETERS = { "cutoff": { "radius": 5.0, "smoothing": {"type": "ShiftedCosine", "width": 0.5}, }, "density": { "type": "Gaussian", "width": 0.3, }, "basis": { "type": "TensorProduct", "max_angular": 2, "radial": {"type": "Gto", "max_radial": 2}, }, } .. GENERATED FROM PYTHON SOURCE LINES 53-57 Create the spherical expansion calculator. The :class:`~featomic.SphericalExpansion` class uses the hyperparameters above. Then, wrap it with :class:`~featomic.clebsch_gordan.EquivariantPowerSpectrum` to compute the Clebsch-Gordan contraction for :math:`\lambda`-SOAP. .. GENERATED FROM PYTHON SOURCE LINES 57-61 .. code-block:: Python spex_calculator = SphericalExpansion(**HYPERPARAMETERS) calculator = EquivariantPowerSpectrum(spex_calculator) .. GENERATED FROM PYTHON SOURCE LINES 62-63 Run the actual calculation .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: Python power_spectrum = calculator.compute(systems, neighbors_to_properties=True) .. GENERATED FROM PYTHON SOURCE LINES 67-68 The result is a :class:`~metatensor.TensorMap` whose keys encode symmetry: .. GENERATED FROM PYTHON SOURCE LINES 68-71 .. code-block:: Python power_spectrum.keys .. rst-class:: sphx-glr-script-out .. code-block:: none Labels( o3_lambda o3_sigma center_type 0 1 1 1 1 1 2 1 1 1 -1 1 2 -1 1 3 1 1 3 -1 1 4 1 1 0 1 6 1 1 6 2 1 6 1 -1 6 2 -1 6 3 1 6 3 -1 6 4 1 6 0 1 7 1 1 7 2 1 7 1 -1 7 2 -1 7 3 1 7 3 -1 7 4 1 7 0 1 8 1 1 8 2 1 8 1 -1 8 2 -1 8 3 1 8 3 -1 8 4 1 8 ) .. GENERATED FROM PYTHON SOURCE LINES 72-79 Often, you only need specific :math:`\lambda` values. For example, if the `target property is the polarizability tensor `_, (a rank-2 symmetric Cartesian tensor), you can restrict the output to :math:`\lambda=0` and :math:`\lambda=2` (with :math:`\sigma=1` to discard `pseudotensors `_) using the ``selected_keys`` parameter: .. GENERATED FROM PYTHON SOURCE LINES 79-87 .. code-block:: Python power_spectrum_0_2 = calculator.compute( systems, neighbors_to_properties=True, selected_keys=Labels(["o3_lambda", "o3_sigma"], np.array([[0, 1], [2, 1]])), ) power_spectrum_0_2.keys .. rst-class:: sphx-glr-script-out .. code-block:: none Labels( o3_lambda o3_sigma center_type 0 1 1 2 1 1 0 1 6 2 1 6 0 1 7 2 1 7 0 1 8 2 1 8 ) .. GENERATED FROM PYTHON SOURCE LINES 88-91 You can also compute a :math:`\lambda`-SOAP-like descriptor using two different expansions. For instance, combine a standarad spherical expansion with a long-range :class:`~featomic.LodeSphericalExpansion`: .. GENERATED FROM PYTHON SOURCE LINES 91-109 .. code-block:: Python LODE_HYPERPARAMETERS = { "density": { "type": "SmearedPowerLaw", "smearing": 0.3, "exponent": 1, }, "basis": { "type": "TensorProduct", "max_angular": 2, "radial": {"type": "Gto", "max_radial": 3, "radius": 1.0}, }, } lode_calculator = LodeSphericalExpansion(**LODE_HYPERPARAMETERS) calculator = EquivariantPowerSpectrum(spex_calculator, lode_calculator) power_spectrum = calculator.compute(systems, neighbors_to_properties=True) power_spectrum.keys .. rst-class:: sphx-glr-script-out .. code-block:: none Labels( o3_lambda o3_sigma center_type 0 1 1 1 1 1 2 1 1 1 -1 1 2 -1 1 3 1 1 3 -1 1 4 1 1 0 1 6 1 1 6 2 1 6 1 -1 6 2 -1 6 3 1 6 3 -1 6 4 1 6 0 1 7 1 1 7 2 1 7 1 -1 7 2 -1 7 3 1 7 3 -1 7 4 1 7 0 1 8 1 1 8 2 1 8 1 -1 8 2 -1 8 3 1 8 3 -1 8 4 1 8 ) .. GENERATED FROM PYTHON SOURCE LINES 110-111 .. end-body .. _sphx_glr_download_examples_compute-lambda-soap.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: compute-lambda-soap.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: compute-lambda-soap.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: compute-lambda-soap.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_