{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "## Creating a crystal from a CIF & display it in 3D\n", "In this short example you can open a CIF file directly from a filename or an URL, and display it in 3D.\n", "\n", "You can also play with the spacegroup and see how that changes the crystal structure.\n", "\n", "*Note: this requires installing `ipywidgets` and `py3Dmol`*" ] }, { "cell_type": "code", "execution_count": 1, "id": "1", "metadata": { "tags": [] }, "outputs": [], "source": [ "from pyobjcryst.crystal import *\n", "from pyobjcryst.atom import Atom\n", "from pyobjcryst.scatteringpower import ScatteringPowerAtom\n", "from math import pi\n" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "### From IUCr journals" ] }, { "cell_type": "code", "execution_count": 2, "id": "3", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C15 Cl H14 N O S\n" ] }, { "data": { "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ "
\n", "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "65060439a1a24c92b02646bd8c15f647", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Box(children=(VBox(children=(HBox(children=(VBox(children=(FloatRangeSlider(value=(0.0, 1.0), description='Xra…" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = create_crystal_from_cif(\"http://scripts.iucr.org/cgi-bin/sendcif?dt3034sup1\",\n", " oneScatteringPowerPerElement=True,\n", " connectAtoms=True)\n", "print(c.GetFormula())\n", "c.widget_3d()\n" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "### From the Crystallography Open Database" ] }, { "cell_type": "code", "execution_count": 3, "id": "5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C29 H32 N4 O5\n" ] }, { "data": { "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ "
\n", "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "734a1e47a635444a854b67cff9f172dc", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Box(children=(VBox(children=(HBox(children=(VBox(children=(FloatRangeSlider(value=(0.0, 1.0), description='Xra…" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c1 = create_crystal_from_cif(\"http://crystallography.net/cod/4506702.cif\",\n", " oneScatteringPowerPerElement=True,\n", " connectAtoms=True)\n", "print(c1.GetFormula())\n", "c1.widget_3d()\n" ] }, { "cell_type": "markdown", "id": "6", "metadata": {}, "source": [ "### Create a Crystal structure and change the spacegroup" ] }, { "cell_type": "code", "execution_count": 4, "id": "7", "metadata": {}, "outputs": [ { "data": { "application/3dmoljs_load.v0": "
\n

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n
\n", "text/html": [ "
\n", "

3Dmol.js failed to load for some reason. Please check your browser console for error messages.

\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "806ba06d0e2e444abfeef12e32bc4b87", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Box(children=(VBox(children=(HBox(children=(VBox(children=(FloatRangeSlider(value=(0.0, 1.0), description='Xra…" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = Crystal(5, 5, 5, pi/2, pi/2, pi/2, \"P1\")\n", "cu = ScatteringPowerAtom(\"Cu\", \"Cu\")\n", "c.AddScatteringPower(cu)\n", "c.AddScatterer(Atom(0,0,0, \"Cu\", cu))\n", "c.widget_3d(extra_opacity=0.1, extra_dist=2)\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "8", "metadata": {}, "outputs": [], "source": [ "# Change the spacegroup and update the display\n", "c.ChangeSpaceGroup(\"Fm-3m\")\n", "c.UpdateDisplay()\n" ] } ], "metadata": { "kernelspec": { "display_name": "objcryst", "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.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }