{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Importing Field Data\n", "\n", "OSCARS supports several basic data formats for importing. We are interested in supporting more.\n", "\n", "One basic format ('OSCARS1D') is where you have the magnetic field (1, 2, or 3D) given as a function of position and your file format lists rows of (for instance) 'Z Bx By Bz'. In this case, specify X, Y, or Z, and whichever of the B's you have in the format field: iformat='Z Bx By Bz'.\n", "\n", "OSCARS format:\n", "Has 10 lines of header information, followed by lines consisting of 'Bx By Bz'. You must specify iformat='OSCARS'. The header is as follows:\n", "* Comment line\n", "* Initial X position\n", "* Step size in X\n", "* Number of points in X\n", "* Initial Y position\n", "* Step size in Y\n", "* Number of points in Y\n", "* Initial Z position\n", "* Step size in Z\n", "* Number of points in Z\n", "\n", "OSCARS also supports iformat='SPECTRA' and iformat='SRW'. See the All About Magnetic Fields tutorial for mroe information." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# matplotlib plots inline\n", "%matplotlib inline\n", "\n", "# Import the OSCARS SR module\n", "import oscars.sr\n", "\n", "# Import OSCARS plots (matplotlib)\n", "from oscars.plots_mpl import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a new OSCARS object. Default to 8 threads and always use the GPU if available\n", "osr = oscars.sr.sr(nthreads=8, gpu=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## OSCARS1D Format" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# To illustrate the basic 1D format let's create a data file, then import it.\n", "# It will be plotted before and after the import\n", "\n", "# Create an undulator field\n", "osr.clear_bfields()\n", "osr.add_bfield_undulator(bfield=[0, 1, 0], period=[0, 0, 0.05], nperiods=31)\n", "plot_bfield(osr)\n", "\n", "# Now write the field to a file\n", "osr.write_bfield(\n", " ofile='GettingStarted_OSCARS1D.dat',\n", " oformat='OSCARS1D Z By Bx Bz',\n", " zlim=[-1, 1],\n", " nz=5000\n", ") \n", "\n", "# Clear fields and import the field from the file created above\n", "osr.clear_bfields()\n", "osr.add_bfield_file(ifile='GettingStarted_OSCARS1D.dat', iformat='OSCARS1D Z By Bx Bz')\n", "plot_bfield(osr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## OSCARS Format" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# To illustrate the basic 3D format let's create a data file, then import it.\n", "# It will be plotted before and after the import\n", "\n", "# Create an undulator field\n", "osr.clear_bfields()\n", "osr.add_bfield_undulator(bfield=[1, 0, 0], period=[0, 0, 0.050], nperiods=11)\n", "plot_bfield(osr)\n", "\n", "# Now write the field to a file\n", "osr.write_bfield(\n", " ofile='GettingStarted_OSCARS.dat',\n", " oformat='OSCARS',\n", " xlim=[-1, 1],\n", " nx=2,\n", " ylim=[-1, 1],\n", " ny=2,\n", " zlim=[-1, 1],\n", " nz=5000\n", ")\n", "\n", "# Clear fields and import the field from the file created above\n", "# You can also scale position, in case your input is not in [m]\n", "osr.clear_bfields()\n", "osr.add_bfield_file(ifile='GettingStarted_OSCARS.dat', iformat='OSCARS')\n", "\n", "plot_bfield(osr)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "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.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }