{ "cells": [ { "cell_type": "markdown", "id": "8a365a14", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "# Switch calculation engine\n", "This notebook shows how we can switch calculation engine (external crystallographic library) and calculate diffraction profile using constant wavelength experiment type." ] }, { "cell_type": "code", "execution_count": null, "id": "9ecc4733", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "# EasyScience, technique-independent\n", "from easyCore import np\n", "from easyCore.Fitting.Fitting import Fitter\n", "\n", "# EasyScience, diffraction\n", "from easyDiffractionLib import Site, Phase, Phases\n", "from easyDiffractionLib.Jobs import Powder1DCW, InterfaceFactory\n", "\n", "# Vizualization\n", "import py3Dmol\n", "from bokeh.io import show, output_notebook\n", "from bokeh.plotting import figure\n", "\n", "# Misc\n", "import xarray as xr" ] }, { "cell_type": "code", "execution_count": null, "id": "d76d50fc", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "output_notebook()\n", "FIGURE_WIDTH = 700\n", "FIGURE_HEIGHT = 300" ] }, { "cell_type": "markdown", "id": "283767f9", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Sample" ] }, { "cell_type": "markdown", "id": "19a39b25", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Create an atom using `Site` interface" ] }, { "cell_type": "code", "execution_count": null, "id": "41020330", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "atom = Site.from_pars(label=\"Cl\",\n", " specie=\"Cl\",\n", " fract_x=0.0,\n", " fract_y=0.0,\n", " fract_z=0.0)" ] }, { "cell_type": "markdown", "id": "910aed87", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Create a phase, set space group, add atom" ] }, { "cell_type": "code", "execution_count": null, "id": "1e2f3193", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "phase = Phase(name=\"salt\")\n", "\n", "phase.spacegroup.space_group_HM_name = \"F m -3 m\"\n", "\n", "phase.add_atom(atom)" ] }, { "cell_type": "markdown", "id": "d4532d8b", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Add another atom (using `Phase` interface)" ] }, { "cell_type": "code", "execution_count": null, "id": "4231d16a", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "phase.add_atom('Na', 'Na', 0.5, 0.5, 0.5)" ] }, { "cell_type": "markdown", "id": "81ace320", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Visualise the structure" ] }, { "cell_type": "code", "execution_count": null, "id": "e68ddbea", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "viewer = py3Dmol.view()\n", "viewer.addModel(phase.to_cif_str(),'cif',{'doAssembly':True,'duplicateAssemblyAtoms':True,'normalizeAssembly':True})\n", "viewer.setStyle({'sphere':{'colorscheme':'Jmol','scale':.2},'stick':{'colorscheme':'Jmol', 'radius': 0.1}})\n", "viewer.addUnitCell()\n", "viewer.replicateUnitCell(2,2,2)\n", "viewer.zoomTo()" ] }, { "cell_type": "markdown", "id": "ebe0e0ee", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Create Phases object" ] }, { "cell_type": "code", "execution_count": null, "id": "29071aa2", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "phases = Phases()\n", "phases.append(phase)" ] }, { "cell_type": "markdown", "id": "07db27b5", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Experiment" ] }, { "cell_type": "markdown", "id": "dd0fce50", "metadata": {}, "source": [ "### Create `Dataset`" ] }, { "cell_type": "code", "execution_count": null, "id": "df64aa80", "metadata": {}, "outputs": [], "source": [ "data = xr.Dataset()\n", "x_data = np.linspace(20, 170, 500)" ] }, { "cell_type": "markdown", "id": "2d961469", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Create `Job`" ] }, { "cell_type": "code", "execution_count": null, "id": "b4ffa97b", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "job = Powder1DCW('NaCl', data, phases=phases)" ] }, { "cell_type": "code", "execution_count": null, "id": "784fb8e5", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "print(f\"Current calculator engine: {job.interface.current_interface_name}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "210bc79d", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "print(f\"Available calculator engines: {job.interface.available_interfaces}\")\n", "print(f\"Available calculators for CW: {job.interface.interface_compatability('Npowder1DCWunp')}\")" ] }, { "cell_type": "markdown", "id": "e93c42b9", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Modify instrumental parameters" ] }, { "cell_type": "code", "execution_count": null, "id": "83e3845f", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "job.parameters.resolution_u = 0.1447\n", "job.parameters.resolution_v = -0.4252\n", "job.parameters.resolution_w = 0.3864\n", "job.parameters.resolution_x = 0.0\n", "job.parameters.resolution_y = 0.0" ] }, { "cell_type": "markdown", "id": "370f0baa", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Modify pattern parameters" ] }, { "cell_type": "code", "execution_count": null, "id": "70fa1571", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "job.pattern.zero_shift = 0.0\n", "job.pattern.scale = 100.0" ] }, { "cell_type": "markdown", "id": "eb234f76", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Simulation" ] }, { "cell_type": "markdown", "id": "00e24eab", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Calculate the profile using the default calculation engine" ] }, { "cell_type": "code", "execution_count": null, "id": "91df1925", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "_ = job.create_simulation(x_data)\n", "y_data_cryspy = np.array(data['sim_NaCl'])" ] }, { "cell_type": "code", "execution_count": null, "id": "05ab7ef9", "metadata": {}, "outputs": [], "source": [ "print(\"some Ycalc from CrysPy:\\n\", y_data_cryspy[315:330])" ] }, { "cell_type": "code", "execution_count": null, "id": "f556cc94", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "fig = figure(width=FIGURE_WIDTH, height=FIGURE_HEIGHT)\n", "fig.line(x_data, y_data_cryspy, legend_label='CW Simulation', color='orangered', line_width=2)\n", "show(fig)" ] }, { "cell_type": "markdown", "id": "c9a336a8", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Switch calculator" ] }, { "cell_type": "code", "execution_count": null, "id": "eeeb243b", "metadata": {}, "outputs": [], "source": [ "# The following code generates AttributeError\n", "#calculator.switch('CrysFML')\n", "#_ = job.create_simulation(x_data)" ] }, { "cell_type": "code", "execution_count": null, "id": "20c3ce77", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "# Temporary workaround\n", "job.interface = InterfaceFactory(interface_name='CrysFML')" ] }, { "cell_type": "code", "execution_count": null, "id": "d6012853", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "print(f\"Current calculator engine: {job.interface.current_interface_name}\")" ] }, { "cell_type": "markdown", "id": "271bf903", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Simulation" ] }, { "cell_type": "markdown", "id": "0c0b9ac4", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Calculate the profile using the calculator engine just selected" ] }, { "cell_type": "code", "execution_count": null, "id": "e03a750e", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "_ = job.create_simulation(x_data)\n", "y_data_crysfml = np.array(data['sim_NaCl'])" ] }, { "cell_type": "code", "execution_count": null, "id": "70544360", "metadata": {}, "outputs": [], "source": [ "print(\"some Ycalc from CrysFML:\\n\", y_data_crysfml[315:330])" ] }, { "cell_type": "code", "execution_count": null, "id": "071a29ea", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "fig = figure(width=FIGURE_WIDTH, height=FIGURE_HEIGHT)\n", "fig.line(x_data, y_data_crysfml, legend_label='CW Simulation', color='olivedrab', line_width=2)\n", "show(fig)" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 5 }