{"id":15898700,"url":"https://github.com/danporter/dans_diffraction","last_synced_at":"2025-04-05T11:08:00.089Z","repository":{"id":57417870,"uuid":"121402271","full_name":"DanPorter/Dans_Diffraction","owner":"DanPorter","description":"Reads crystallographic cif files and simulates diffraction","archived":false,"fork":false,"pushed_at":"2025-02-10T07:28:10.000Z","size":12196,"stargazers_count":59,"open_issues_count":0,"forks_count":13,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-05T11:07:50.640Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DanPorter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-13T15:54:24.000Z","updated_at":"2025-03-19T18:21:32.000Z","dependencies_parsed_at":"2023-09-24T05:23:15.129Z","dependency_job_id":"b659c77d-88ce-49f3-aef7-4a311eb653d0","html_url":"https://github.com/DanPorter/Dans_Diffraction","commit_stats":{"total_commits":111,"total_committers":4,"mean_commits":27.75,"dds":0.1351351351351351,"last_synced_commit":"6a336ffbd781b75c46204db370cbbd02b04f78be"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanPorter%2FDans_Diffraction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanPorter%2FDans_Diffraction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanPorter%2FDans_Diffraction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanPorter%2FDans_Diffraction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DanPorter","download_url":"https://codeload.github.com/DanPorter/Dans_Diffraction/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247325693,"owners_count":20920714,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-06T10:07:41.525Z","updated_at":"2025-04-05T11:08:00.072Z","avatar_url":"https://github.com/DanPorter.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dans_Diffraction\nReads crystallographic cif files, calculates crystal properties and simulates diffraction.\n\n**Version 3.3**\n\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.8106031.svg)](https://doi.org/10.5281/zenodo.8106031)\n[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/DanPorter/Dans_Diffraction/master?labpath=Dans_Diffraction.ipynb) \n[![](https://img.shields.io/github/forks/DanPorter/Dans_Diffraction?label=GitHub%20Repo\u0026style=social)](https://github.com/DanPorter/Dans_Diffraction)\n\n\nBy Dan Porter, Diamond Light Source\n2024\n\n#### TL;DR:\n```text\n$ ipython -i -m Dans_Diffraction\nOR\n$ ipython -m Dans_Diffraction gui\n```\n\n```python\n\"\"\"Python Sctipt\"\"\"\nimport Dans_Diffraction as dif\nxtl = dif.Crystal('some_file.cif')\nprint(xtl) # print Crystal structure parameters\n\n# Print reflection list:\nprint(xtl.Scatter.print_all_reflections(energy_kev=5)) \n\n# Plot Powder pattern:\nxtl.Plot.simulate_powder(energy_kev=8)\nplt.show()\n\n# Start graphical user interface:\nxtl.start_gui()\n```\n\nFull code documentation available [here](https://danporter.github.io/Dans_Diffraction/).\n\nTry it out on [mybinder!](https://mybinder.org/v2/gh/DanPorter/Dans_Diffraction/master?labpath=Dans_Diffraction.ipynb)\n\nFor comments, queries or bugs - email [dan.porter@diamond.ac.uk](mailto:dan.porter@diamond.ac.uk)\n\n**Citation:** If you use this code (great!), please cite the published DOI: [10.5281/zenodo.8106031](https://doi.org/10.5281/zenodo.8106031)\n\n# Installation\n**Requirements:** \nPython 3+ with packages: *Numpy*, *Matplotlib*, *Tkinter*.\nBuiltIn packages used: *sys*, *os*, *re*, *glob*, *warnings*, *json*, *itertools*\n\nInstall stable version from PyPI:\n```text\n$ python -m pip install Dans-Diffraction\n```\n\nOr, install the latest version direct from GitHub:\n```text\n$ python -m pip install git+https://github.com/DanPorter/Dans_Diffraction.git\n```\n\nOr, Download the latest version from GitHub (with examples!):\n```text\n$ git clone https://github.com/DanPorter/Dans_Diffraction.git\n$ cd Dans_Diffraction\n$ python -m pip install .\n```\n\n\n\n# Operation\nFrom version 3.2, installing Dans_Diffraction will include a run script for the gui:\n```text\n$ dansdiffraction\n```\nAlternatively, Dans_Diffraction is best run within an interactive python environment:\n```text\n$ ipython -i -m Dans_Diffraction\n```\n\nDans_Diffraction can also be run in scripts as an import, example scripts are provided in the [Examples](https://github.com/DanPorter/Dans_Diffraction/blob/master/Examples) folder.\n### Read CIF file\n```python\nimport Dans_Diffraction as dif\nxtl = dif.Crystal('some_file.cif')\nxtl.info() # print Crystal structure parameters\nhelp(xtl)  # all functions (nearly!) are documented\n```\n\n### Alter atomic positions\n```python\nxtl.Cell.latt(2.85, 2.85, 10.8, 90, 90, 120) #  set lattice parameters\nxtl.Atoms.info() # Print Symmetric positions\nxtl.Structure.info() # Print All positions in P1 symmetry (same structure and functions as xtl.Atoms)\n# Symmetric positions\nxtl.Atoms.changeatom(idx=0, u=0, v=0, w=0, type='Co', label='Co1')\nxtl.Atoms.addatom(idx=0, u=0, v=0, w=0, type='Co', label='Co1')\n# After adding or changing an atom in the Atoms class, re-generate the full structure using symmetry arguments:\nxtl.generate_lattice()\n# Full atomic structure in P1 symmetry\nxtl.Structure.changeatom(idx=0, u=0, v=0, w=0, type='Co', label='Co1')\nxtl.Structure.addatom(idx=0, u=0, v=0, w=0, type='Co', label='Co1')\n# Plot crystal Structure\nxtl.Plot.plot_crystal() # 3D plot\nxtl.Plot.plot_layers() # 2D plot for layered materials\n```\n![3D Plot](https://github.com/DanPorter/Dans_Diffraction/blob/master/Screenshots/3Dstructrue_Ca3CoMnO6.png?raw=true)\n\n\n### Alter crystal symmetry\n```python\nxtl.Symmetry.info() # print symmetry arguments\nxtl.Symmetry.addsym('x,y,z+1/2') # adds single symmetry operation\nxtl.Symmetry.changesym(0, 'x,y,z+1/4')\nxtl.Symmetry.load_spacegroup(194) # replaces current symmetry operations\n# After adding or changing symmetry operations, regengerate the symmetry matrices\nxtl.Symmetry.generate_matrices()\n```\n\n### Save structure as CIF\nLattice parameters, crystal structure and symmetry operations will be saved to the CIF.\nIf magnetic moments are defined, magnetic symmetry operations and moments will also be saved\nand format changed to \"*.mcif\".\n```python\nxtl.write_cif('edited file.cif')\n```\n\n### Calculate Structure Factors\nX-ray or neutron structure factors/ intensities are calculated based on the full unit cell structure, including atomic \nform-factors (x-rays) or coherent scattering lengths (neutrons).\n```python\n# Choose scattering options (see help(xtl.Scatter.setup_scatter))\nxtl.Scatter.setup_scatter(scattering_type='x-ray', energy_keV=8.0)\n# Allowed radiation types:\n#    'xray','neutron','xray magnetic','neutron magnetic','xray resonant'\nxtl.Scatter.print_all_reflections() # Returns formated string of all allowed reflections\ninten = xtl.Scatter.intensity([h,k,l]) # Returns intensity\ntwotheta, iten, reflections = xtl.Scatter.powder(units='twotheta')\n# Plot Experimental Intensities\nxtl.Plot.simulate_powder() # Powder pattern\nxtl.Plot.simulate_hk0() # Reciprocal space plane\n```\n![Powder Pattern](https://github.com/DanPorter/Dans_Diffraction/blob/master/Screenshots/powder_diamond.png?raw=true)\n![HK0 Simulation](https://github.com/DanPorter/Dans_Diffraction/blob/master/Screenshots/supercell_diffraction.png?raw=true)\n\n\n### Magnetic Structrues\n*Magnetic structures and scattering are currently in development and shouldn't be treated as accurate!*\n\nSimple magnetic structures can be loaded from magnetic cif (*.mcif) files. Magnetic moments are stored for each atomic \nposition as a vector. The crystal object has a seperate set of magnetic symmetry operations. Symmetry operations from the \ntables of magnetic spacegroups can also be loaded. Only simple magnetic structures are allowed. There must be the same\nnumber of magnetic symmetry operations as crystal symmetry operations and atomic positions can only have single moments\nassigned.\n```python\nxtl = dif.Crystal('some_file.mcif')\nxtl.Atoms.mxmymz() # return magnetic moment vectors on each ion\nxtl.Symmetry.symmetry_operations_magnetic # magnetic symmetry operations (list of strings)\nxtl.Symmetry.print_magnetic_spacegroups() # return str of available magnetic spacegroups, given crystal's spacegroup\nxtl.Symmetry.load_magnetic_spacegroup(mag_spg_number) # loads mag. operations given mag. spacegroup number\n```\nMagnetic scattering is also available for neutrons and x-rays (both resonant and non-resonant), using the appropriate magnetic form-factors.\n```python\nImag = xtl.Scatter.magnetic_neutron(HKL=[0,0,3])\nIres = xtl.Scatter.xray_resonant_magnetic(HKL=[0,0,3], energy_kev=2.838, azim_zero=[1, 0, 0], psi=0, polarisation='s-p', F0=0, F1=1, F2=0)\n```\n\n### Superstructures\nSuperstructures can be built using the Superstructure class, requring only a matrix to define the new phase:\n```python\nsu = xtl.generate_superstructure([[2,0,0],[0,2,0],[0,0,1]])\n```\n\nSuperstucture classes behave like Crystal classes, but have an additional 'Parent' property that references the original \ncrystal structure and additional behaviours partiular to superstructures. Superstructures loose their parent crystal and\nmagnetic symmetry, always being defined in P1 symmetry. So su.Atoms == su.Structure.\n\n```python\nprint(su.parent.info())  # Parent structure\nsu.P # superstructure matrix \nsu.superhkl2parent([h, k, l])  # index superstructure hkl with parent cell\nsu.parenthkl2super([h, k, l])  # index parent hkl with supercell\n```\n\n### Multi-phase\nScattering from different crystal structures can be compared using the MultiCrystal class:\n```python\nxtls = xtl1 + xtl2\nxtls.simulate_powder()\n```\n\n\n### Properties\nThe Crystal class contains a lot of atomic properties that can be exposed in the Properties class:\n```python\nxtl.Properties.info()\n```\n\nCalculated properties include:\n - Molecular weight\n - Density\n - Diamagnetic suscpetibility \n - x-ray absorption coefficient, attenuation length, transmission and refractive index\n - Molecular charge balance\n - Molecular mass fraction\n - Atomic orbitals\n - Magnetic exchange paths (in progress...)\n\nProperties are calulated using the atomic structure along with atomic data stored in the folder [Dans_Diffraction/data](data).\n\n\n### Multiple Scattering\nSimulations of multiple scattering at different azimuths for a particular energy can be simulated. Based on [code by Dr Gareth Nisbet](https://journals.iucr.org/a/issues/2015/01/00/td5022/).\n [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.12866.svg)](https://doi.org/10.5281/zenodo.12866).\n\n```python\nazimuth, intensity = xtl.Scatter.ms_azimuth([h,k,l], energy_kev=8)\n```\n\n![Multiple Scattering](https://github.com/DanPorter/Dans_Diffraction/blob/master/Screenshots/ms_azimuth_silicon.png?raw=true)\n\n\n### Graphical Front End\n![All GUI elements](https://github.com/DanPorter/Dans_Diffraction/blob/master/Screenshots/GUI_all.png?raw=true)\n\nStart a new GUI, then select a cif file:\n```text\n$ ipython -i -m Dans_Diffraction gui\n```\nOr start the GUI from within the interactive console:\n```python\ndif.start_gui()\n```\nUsing an already generated crystal:\n```python\nxtl.start_gui()\n```\n\n### Diffractometer Simulator\n![Diffractometer](https://github.com/DanPorter/Dans_Diffraction/blob/master/Screenshots/diffractometer.png?raw=true)\n\nNew in version 3.0.0. Simulate a generic detector situated around the crystal sample, with the ability to \ncontrol detector location shape and size and lattice orientation.\n\n\n### FDMNES functionality\nFDMNES is a powerful tool for simulating resonant x-ray diffraction, created by [Y. Joly and O. Bunau.](https://fdmnes.neel.cnrs.fr/)\n\nThe Dans_Diffraction FDMNES class allows for the automatic creation of input files and simple analysis of results.\nThe following command should be used to activate these features (only needs to be issued once). \n```python\ndif.activate_fdmnes()\n```\nOnce activated, the FDMNES classes become available.\n```python\nfdm = dif.Fdmnes(xtl) # Create input files and run FDMNES\nfdma = dif.FdmnesAnalysis(output_path, output_name) # Load output files and plot results\n```\nSee class documentation for more information.\n\n\nOnce activated, FDMNES GUI elements become available from the main window, emulating functionality of the classes.\n\n![FDMNES Run](https://github.com/DanPorter/Dans_Diffraction/blob/master/Screenshots/GUI_08.png?raw=true)\n![FDMNES Analyse](https://github.com/DanPorter/Dans_Diffraction/blob/master/Screenshots/GUI_09.png?raw=true)\n\n\n# Acknoledgements\n| Date        | Thanks to...                                                                            |\n|-------------|-----------------------------------------------------------------------------------------|\n| 2018        | Thanks to Hepesu for help with Python3 support and ideas about breaking up calculations |\n| Dec 2019    | Thanks to Gareth Nisbet for allowing me to inlude his multiple scattering siumulation   |\n| April 2020  | Thanks to ChunHai Wang for helpful suggestions in readcif!                              |\n| May 2020    | Thanks to AndreEbel for helpful suggestions on citations                                |\n| Dec 2020    | Thanks to Chris Drozdowski for suggestions about reflection families                    |\n| Jan 2021    | Thanks to aslarsen for suggestions about outputting the structure factor                |\n| April 2021  | Thanks to Trygve Ræder for suggestions about x-ray scattering factors                   |\n| Feb 2022    | Thanks to Mirko for pointing out the error in two-theta values in Scatter.powder        |\n| March 2022  | Thanks to yevgenyr for suggesting new peak profiles in Scatter.powder                   |\n| Jan 2023    | Thanks to Anuradha Vibhakar for pointing out the error in f0 + if'-if''                 |\n| Jan 2023    | Thanks to Andreas Rosnes for testing the installation in jupyterlab                     |\n| May 2023    | Thanks to Carmelo Prestipino for adding electron scattering factors                     |\n| June 2023   | Thanks to Sergio I. Rincon for pointing out the rounding error in Scatter.powder        |\n| July 2023   | Thanks to asteppke for suggested update to Arrow3D for matplotlib V\u003e3.4                 |\n| July 2023   | Thanks to Yves Joly for helpful suggestions on FDMNES wrapper                           |\n| Jan 2024    | Thanks to Carmelo Prestipino for adding search_distance and plot_distance               | \n| April 2024  | Thanks to Innbig for pointing out an issue with liquid crystal simulations              |\n| May 2024    | Thanks to paul-cares pointing out a silly spelling error in the title!                  |\n\nCopyright\n-----------------------------------------------------------------------------\n   Copyright 2024 Diamond Light Source Ltd.\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n\nFiles in this package covered by this licence:\n* classes_crystal.py\n* classes_scattering.py\n* classes_plotting.py\n* classes_properties.py\n* classes_multicrystal.py\n* classes_orientation.py\n* classes_orbitals.py\n* functions_general.py\n* functions_plotting.py\n* functions_scattering.py\n* functions_crystallography.py\n* tkgui/*.py\n\nOther files are either covered by their own licence or not licenced for other use.\n\n| Dr Daniel G Porter | [dan.porter@diamond.ac.uk](mailto:dan.porter@diamond.ac.uk) |\n| ---- | ---- |\n| [www.diamond.ac.uk](www.diamond.ac.uk) | Diamond Light Source, Chilton, Didcot, Oxon, OX11 0DE, U.K. |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanporter%2Fdans_diffraction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanporter%2Fdans_diffraction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanporter%2Fdans_diffraction/lists"}