.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_tosca_from_modes.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_auto_examples_plot_tosca_from_modes.py: TOSCA spectrum from precomputed phonon modes ============================================== Run :class:`~aiida_pythonjob_ins.workflows.ToscaFromModesWorkChain` on ethanol phonon modes to simulate the inelastic-neutron-scattering spectrum the TOSCA spectrometer would record, plot it grouped two different ways, and visualise the AiiDA provenance graph. Ethanol is a hydrogenous molecular crystal, which is what TOSCA is mostly used for: its almost-isotropic incoherent approximation is dominated by hydrogen, and published spectra for such samples are plentiful to compare against. .. GENERATED FROM PYTHON SOURCE LINES 15-18 Set up AiiDA ------------ A shared helper loads a temporary in-memory profile and a localhost Python code. .. GENERATED FROM PYTHON SOURCE LINES 18-25 .. code-block:: Python from _aiida_setup import example_data, get_python_code, show_provenance from aiida import orm from aiida.engine import run_get_node code = get_python_code() .. GENERATED FROM PYTHON SOURCE LINES 26-31 Load the phonon modes --------------------- The modes are a Euphonic ``QpointPhononModes`` JSON dump, which ``QpointPhononModesData`` reads directly -- no conversion step is needed, because the node stores that JSON byte-for-byte. .. GENERATED FROM PYTHON SOURCE LINES 31-43 .. code-block:: Python from aiida.plugins import DataFactory, WorkflowFactory # Load via the plugin factories (direct imports like # `from aiida_pythonjob_ins.data import QpointPhononModesData` also work) QpointPhononModesData = DataFactory("pythonjob_ins.qpoint_phonon_modes") ToscaFromModesWorkChain = WorkflowFactory("pythonjob_ins.tosca_from_modes") modes = QpointPhononModesData.from_json_file( example_data("ethanol_qpoint_phonon_modes.json") ) .. GENERATED FROM PYTHON SOURCE LINES 44-53 Run the workflow, grouped by quantum order ------------------------------------------ The workflow computes the full, ungrouped line set as a PythonJob (one line per atom, quantum order and detector bank), then groups and broadens it. Both detector banks -- backward at 135 degrees and forward at 45 degrees -- are evaluated by default. Caching is enabled explicitly rather than relying on the ambient configuration, so the second run below can demonstrably reuse the expensive step. .. GENERATED FROM PYTHON SOURCE LINES 53-75 .. code-block:: Python from aiida.manage.configuration import get_config # The process type aiida-pythonjob registers PythonJob under. PYTHONJOB_PROCESS_TYPE = "aiida.calculations:pythonjob.pythonjob" # `aiida.manage.caching.enable_caching` is the usual way to do this in a script, # but it is a context manager, and everything it covers would have to be indented # into a single block -- which would collapse the separately explained steps below # into one. Setting the option achieves the same thing without that constraint. get_config().set_option("caching.enabled_for", [PYTHONJOB_PROCESS_TYPE]) by_order_results, by_order_node = run_get_node( ToscaFromModesWorkChain, modes=modes, temperature=orm.Float(10.0), # kelvin energy_spacing=orm.Float(10.0), # 1/cm group_by=orm.List(list=["quantum_order"]), code=code, ) print(f"WorkChain finished OK: {by_order_node.is_finished_ok}") .. rst-class:: sphx-glr-script-out .. code-block:: none WorkChain finished OK: True .. GENERATED FROM PYTHON SOURCE LINES 76-82 Run again, grouped by element ----------------------------- Only the grouping keys differ, so the expensive intensity calculation is taken from the cache and only the cheap grouping and broadening steps run again. This is the point of committing the ungrouped line set to the graph as its own output. .. GENERATED FROM PYTHON SOURCE LINES 82-93 .. code-block:: Python by_element_results, by_element_node = run_get_node( ToscaFromModesWorkChain, modes=modes, temperature=orm.Float(10.0), energy_spacing=orm.Float(10.0), group_by=orm.List(list=["atom_symbol"]), code=code, ) print(f"WorkChain finished OK: {by_element_node.is_finished_ok}") .. rst-class:: sphx-glr-script-out .. code-block:: none WorkChain finished OK: True .. GENERATED FROM PYTHON SOURCE LINES 94-97 Confirm the expensive step was reused rather than repeated. Asserting this means a silent loss of cacheability fails the documentation build instead of passing unnoticed. .. GENERATED FROM PYTHON SOURCE LINES 97-107 .. code-block:: Python intensity_jobs = [ process for process in by_element_node.called_descendants if isinstance(process, orm.CalcJobNode) ] reused = all(job.base.caching.is_created_from_cache for job in intensity_jobs) print(f"Intensity calculation taken from the cache: {reused}") assert reused, "expected the second run to reuse the cached intensity calculation" .. rst-class:: sphx-glr-script-out .. code-block:: none Intensity calculation taken from the cache: True .. GENERATED FROM PYTHON SOURCE LINES 108-113 Plot the spectrum, grouped by quantum order ------------------------------------------- The output is a native AiiDA ``XyData``: one x array of energies, one y array per group, each named with a ready-made legend label derived from the line's metadata. .. GENERATED FROM PYTHON SOURCE LINES 113-136 .. code-block:: Python import matplotlib.pyplot as plt def plot_spectrum(spectrum, title): """Plot every y array of an XyData spectrum, using its names as labels.""" _, energy, energy_unit = spectrum.get_x() lines = spectrum.get_y() intensity_unit = lines[0][2] # shared by every line of the collection fig, ax = plt.subplots() for label, intensity, _ in lines: ax.plot(energy, intensity, label=label) ax.set_xlabel(f"Energy transfer ({energy_unit})") ax.set_ylabel(f"Intensity ({intensity_unit})") ax.set_title(title) ax.legend() fig.tight_layout() return fig plot_spectrum(by_order_results["spectrum"], "Ethanol TOSCA spectrum (by quantum order)") .. image-sg:: /auto_examples/images/sphx_glr_plot_tosca_from_modes_001.png :alt: Ethanol TOSCA spectrum (by quantum order) :srcset: /auto_examples/images/sphx_glr_plot_tosca_from_modes_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 137-141 Plot the same calculation, grouped by element --------------------------------------------- Hydrogen dominates, as expected for an incoherent-approximation spectrum of a hydrogenous sample. .. GENERATED FROM PYTHON SOURCE LINES 141-144 .. code-block:: Python plot_spectrum(by_element_results["spectrum"], "Ethanol TOSCA spectrum (by element)") .. image-sg:: /auto_examples/images/sphx_glr_plot_tosca_from_modes_002.png :alt: Ethanol TOSCA spectrum (by element) :srcset: /auto_examples/images/sphx_glr_plot_tosca_from_modes_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. GENERATED FROM PYTHON SOURCE LINES 145-149 Provenance ---------- The graph shows the single intensity PythonJob feeding the ``components`` output, and the grouping and broadening calcfunctions branching off it. .. GENERATED FROM PYTHON SOURCE LINES 149-152 .. code-block:: Python show_provenance(by_element_node, title="TOSCA-from-modes workflow provenance") .. image-sg:: /auto_examples/images/sphx_glr_plot_tosca_from_modes_003.png :alt: TOSCA-from-modes workflow provenance :srcset: /auto_examples/images/sphx_glr_plot_tosca_from_modes_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none
.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.677 seconds) .. _sphx_glr_download_auto_examples_plot_tosca_from_modes.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_tosca_from_modes.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_tosca_from_modes.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_tosca_from_modes.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_