{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# File conversion\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Here we create the necessary files for this example.\nGo to next section for the file conversion.*\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ase.build import fcc111\nfrom ase.io import write\n\natoms = fcc111('Au', size=(4, 4, 6), vacuum=6.0)\natoms.info.clear()\nwrite('slab.extxyz', atoms, format='extxyz')\n\nimages_rattled = []\nfor i in range(10):\n atoms.rattle(stdev=0.1, seed=i)\n images_rattled.append(atoms.copy())\nwrite('md_trajectory.traj', images_rattled)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## With python interpreter\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Note

\n\nTo begin, the formats available for reading\nand writing are listed here:\n[Input/Output module](https://docs.ase-lib.org/ase/io/io.html).\n\nYou can also access this page by looking for the ``IO module``.\n\n
\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a very simple example of a file conversion.\nIt takes a extended xyz (``.extxyz``) file as entry,\nand output a ``.cif`` file.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ase.io import read, write\n\nimage = read('slab.extxyz', format='extxyz', index=0)\nwrite('slab.cif', image, format='cif')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Note

\n\nFormat can be automatically found with extension\nbut it is preferable to specify it explicitly.\n\n
\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The index argument allow to load a specific configuration\nor range of configuration.\n\nYou can pass an ``integer`` (0 for\nfirst configuration, -1 for the last) or a ``string``.\nThe string follows array indexing, meaning \":\" for all,\n\"::2\" for all and every 2, etc...\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "imagepath = 'md_trajectory.traj'\n\n# load all configurations\nimages = read(imagepath, format='traj', index=':')\n\n# load every 2nd configurations\nimages = read(imagepath, format='traj', index='::2')\n\n# load last configuration\nimage = read(imagepath, format='traj', index='-1')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load a trajectory from a MD tutorial.\nLoad from the third element to the last.\nWrite it in ``extended xyz`` every 2 frame.\nThis format can hold several configurations.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "trajectory = read(\n imagepath,\n format='traj',\n index='2:',\n)\nwrite('md_trajectory.extxyz', trajectory[::2], format='extxyz')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## With command line input (cli)\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also convert the file using the cli:\n\n```bash\nase convert md_trajectory.traj \\\nmd_trajectory_cli.xyz \\\n--input-format traj --output-format extxyz --image-number ::2\n```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## With graphical user interface (gui)\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also convert the file using the gui.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open the GUI\n\n```bash\nase gui md_trajectory.traj\n```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "in ``File -> save`` you can save your image/trajectory.\n\nYou can provide the name as: ``md_last.cif@-1``\nto save only the last image.\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.14.5" } }, "nbformat": 4, "nbformat_minor": 0 }