{ "cells": [ { "cell_type": "markdown", "id": "26bea4e7", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "# CrysFML: Test against FullProf - 1\n", "This is a comparison of CFML 0.0.3 from PyPI with FullProf.2k Version 7.30 - Mar2020-ILL JRC for macOS" ] }, { "cell_type": "code", "execution_count": null, "id": "9ecc4733", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "# Misc\n", "import sys, os, re\n", "import numpy as np\n", "from io import StringIO\n", "from pathlib import Path\n", "\n", "# CrysFML\n", "import CFML_api\n", "\n", "# Vizualization\n", "import py3Dmol\n", "from bokeh.io import show, output_notebook\n", "from bokeh.plotting import figure" ] }, { "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": "code", "execution_count": null, "id": "9bc7ab53", "metadata": {}, "outputs": [], "source": [ "def diffraction_pattern_xy(space_group, cell, atom_list, job_info):\n", " reflection_list = CFML_api.ReflectionList(cell, space_group, \n", " True, \n", " job_info)\n", " reflection_list.compute_structure_factors(space_group, \n", " atom_list, \n", " job_info)\n", " diffraction_pattern = CFML_api.DiffractionPattern(job_info,\n", " reflection_list, \n", " cell.reciprocal_cell_vol)\n", " return diffraction_pattern.x, diffraction_pattern.ycalc" ] }, { "cell_type": "markdown", "id": "acd38196", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Define sample model" ] }, { "cell_type": "markdown", "id": "45dbea02", "metadata": {}, "source": [ "### Create space group object" ] }, { "cell_type": "code", "execution_count": null, "id": "85719689", "metadata": {}, "outputs": [], "source": [ "space_group = CFML_api.SpaceGroup('P n m a')\n", "#space_group.print_description()" ] }, { "cell_type": "markdown", "id": "7318fe30", "metadata": {}, "source": [ "### Create unit cell object" ] }, { "cell_type": "code", "execution_count": null, "id": "3331332e", "metadata": {}, "outputs": [], "source": [ "lengths = np.asarray([8.485680,5.402480,6.964544], dtype='float32')\n", "angles = np.asarray([90,90,90], dtype='float32')\n", "unit_cell = CFML_api.Cell(lengths, angles)\n", "#unit_cell.print_description()" ] }, { "cell_type": "markdown", "id": "f7abc667", "metadata": {}, "source": [ "### Create list of atoms" ] }, { "cell_type": "code", "execution_count": null, "id": "990fd6f8", "metadata": {}, "outputs": [], "source": [ "cif_str = '''\n", "loop_\n", " _atom_site_label\n", " _atom_site_type_symbol\n", " _atom_site_occupancy\n", " _atom_site_fract_x\n", " _atom_site_fract_y\n", " _atom_site_fract_z\n", " _atom_site_adp_type\n", " _atom_site_U_iso_or_equiv\n", " Pb Pb 1.0 0.18797 0.25 0.16754 Uiso 0.0173\n", " S S 1.0 0.06467 0.25 0.68300 Uiso 0.0115\n", " O1 O 1.0 0.90705 0.25 0.59672 Uiso 0.0070\n", " O2 O 1.0 0.18641 0.25 0.54269 Uiso 0.0127\n", " O3 O 1.0 0.08025 0.02961 0.81208 Uiso 0.0135\n", "'''\n", "atom_list = CFML_api.AtomList(cif_str.split('\\n'))\n", "atom_list.set_mult_occ_cif(space_group)\n", "atom_list.print_description()" ] }, { "cell_type": "markdown", "id": "7e5916ec", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Load simulated data" ] }, { "cell_type": "markdown", "id": "e85b2978", "metadata": {}, "source": [ "### Load simulated data" ] }, { "cell_type": "code", "execution_count": null, "id": "9651c148", "metadata": {}, "outputs": [], "source": [ "file_path = '../datasets/fullprof_simulations/pbsox_1_data.cif'\n", "from_str = '_pd_proc_intensity_bkg_calc'\n", "to_str = '_pd_proc_number_of_points'\n", "\n", "file_content = Path(file_path).read_text()\n", "match = re.search(f'{from_str}(.*){to_str}', file_content, re.DOTALL)\n", "\n", "data_str = match.group(0)\n", "data_str = data_str.replace(from_str, '').replace(to_str, '')\n", "data_str = data_str.replace('(',' ').replace(')',' ')\n", "\n", "_, x_calc_fp, _, _, y_calc_fp, _ = np.genfromtxt(StringIO(data_str), unpack=True)" ] }, { "cell_type": "markdown", "id": "99031438", "metadata": {}, "source": [ "### Visualize simulated data" ] }, { "cell_type": "code", "execution_count": null, "id": "eb5920b9", "metadata": {}, "outputs": [], "source": [ "fig = figure(width=FIGURE_WIDTH, height=FIGURE_HEIGHT)\n", "fig.line(x_calc_fp, y_calc_fp, legend_label='Icalc (FullProf)', color='darkgray', line_width=2)\n", "show(fig)" ] }, { "cell_type": "markdown", "id": "4f607299", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Analyse data" ] }, { "cell_type": "markdown", "id": "6d7bf8f8", "metadata": {}, "source": [ "### Create job definition object" ] }, { "cell_type": "code", "execution_count": null, "id": "248d1625", "metadata": {}, "outputs": [], "source": [ "cfl_str = '''\n", "Title PbSO4\n", "Patt_1 XRAY_2THE 1.540560 1.540560 0.50 0.0 135.0\n", "'''\n", "job_info = CFML_api.JobInfo(cfl_str.split('\\n'))\n", "job_info.print_description()" ] }, { "cell_type": "markdown", "id": "8d2a501f", "metadata": {}, "source": [ "### Modify job definition object" ] }, { "cell_type": "code", "execution_count": null, "id": "a54b8c2b", "metadata": {}, "outputs": [], "source": [ "job_info.pattern_type = \"XRAY_2THE\"\n", "job_info.range_2theta = (10.0, 154.0)\n", "job_info.theta_step = 0.025\n", "#job_info.lambdas = (1.5405, 1.5444)\n", "#job_info.lambda_ratio = 0.5\n", "job_info.u_resolution = 0.031112\n", "job_info.v_resolution = -0.052102\n", "job_info.w_resolution = 0.032035\n", "job_info.x_resolution = 0.014710 # 0.014710\n", "job_info.y_resolution = 0.0\n", "job_info.print_description()" ] }, { "cell_type": "markdown", "id": "0d389b01", "metadata": {}, "source": [ "### Calculate and plot diffraction pattern" ] }, { "cell_type": "code", "execution_count": null, "id": "dfe933ff", "metadata": {}, "outputs": [], "source": [ "x_calc_cfml, y_calc_cfml = diffraction_pattern_xy(space_group, unit_cell, atom_list, job_info)" ] }, { "cell_type": "code", "execution_count": null, "id": "ff08fbf0", "metadata": {}, "outputs": [], "source": [ "fig = figure(width=FIGURE_WIDTH, height=FIGURE_HEIGHT)\n", "fig.line(x_calc_cfml, y_calc_cfml, legend_label='Icalc (CrysFML)', color='orangered', line_width=1)\n", "show(fig)" ] }, { "cell_type": "markdown", "id": "c4b6ba2e", "metadata": {}, "source": [ "### Compare observed and calculated data" ] }, { "cell_type": "code", "execution_count": null, "id": "9ceec9a4", "metadata": {}, "outputs": [], "source": [ "scale = 0.25\n", "background = 100\n", "zero_shift = -0.0" ] }, { "cell_type": "code", "execution_count": null, "id": "11fec5d2", "metadata": {}, "outputs": [], "source": [ "x_calc_cfml, y_calc_cfml = diffraction_pattern_xy(space_group, unit_cell, atom_list, job_info)\n", "x_calc_cfml = x_calc_cfml + zero_shift\n", "y_calc_cfml = y_calc_cfml * scale + background" ] }, { "cell_type": "code", "execution_count": null, "id": "57a4c4cd", "metadata": { "scrolled": true }, "outputs": [], "source": [ "fig = figure(width=FIGURE_WIDTH, height=FIGURE_HEIGHT)\n", "fig.line(x_calc_fp, y_calc_fp, legend_label='Icalc (FP)', color='darkgray', line_width=2)\n", "fig.line(x_calc_cfml, y_calc_cfml, legend_label='Icalc (CFML)', color='orangered', line_width=1)\n", "show(fig)" ] }, { "cell_type": "code", "execution_count": null, "id": "8155e22f", "metadata": { "scrolled": true }, "outputs": [], "source": [ "fig = figure(width=FIGURE_WIDTH, height=FIGURE_HEIGHT, x_range=(20,30))\n", "fig.line(x_calc_fp, y_calc_fp, legend_label='Icalc (FP)', color='darkgray', line_width=2)\n", "fig.line(x_calc_cfml, y_calc_cfml, legend_label='Icalc (CFML)', color='orangered', line_width=1)\n", "show(fig)" ] }, { "cell_type": "code", "execution_count": null, "id": "5fb9e1aa", "metadata": { "scrolled": true }, "outputs": [], "source": [ "fig = figure(width=FIGURE_WIDTH, height=FIGURE_HEIGHT, x_range=(43,46.5))\n", "fig.line(x_calc_fp, y_calc_fp, legend_label='Icalc (FP)', color='darkgray', line_width=2)\n", "fig.line(x_calc_cfml, y_calc_cfml, legend_label='Icalc (CFML)', color='orangered', line_width=1)\n", "show(fig)" ] }, { "cell_type": "code", "execution_count": null, "id": "cd79c4ee", "metadata": { "scrolled": true }, "outputs": [], "source": [ "fig = figure(width=FIGURE_WIDTH, height=FIGURE_HEIGHT, x_range=(61,65))\n", "fig.line(x_calc_fp, y_calc_fp, legend_label='Icalc (FP)', color='darkgray', line_width=2)\n", "fig.line(x_calc_cfml, y_calc_cfml, legend_label='Icalc (CFML)', color='orangered', line_width=1)\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 }