{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy.integrate import ode\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define bloch and B_eff functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def bloch_ode(t,M,T1,T2):\n", " B = B_eff(t)\n", " return np.array([M[1]*B[2] - M[2]*B[1] - M[0]/T2,\n", " M[2]*B[0] - M[0]*B[2] - M[1]/T2,\n", " M[0]*B[1] - M[1]*B[0] - (M[2]-1)/T1])\n", "\n", "def B_eff(t):\n", " if t < 0.25:\n", " return np.array([0, 0, 0])\n", " elif t < 1.25:\n", " return np.array([1.8*np.sinc(t-0.75), 0, 0])\n", " elif t < 1.50:\n", " return np.array([0, 0, 0])\n", " elif t < 3.00:\n", " return np.array([0, 0, 2*np.pi])\n", " else:\n", " return np.array([0, 0, 0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Integrate ODE" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t = np.array([0])\n", "M = np.array([[0, 0, 1]])\n", "dt= 0.005\n", "r = ode(bloch_ode)\n", "r.set_integrator('dopri5')\n", "r.set_initial_value(M[0],t[0])\n", "r.set_f_params(1500, 50)\n", "while r.successful() and r.t < 5:\n", " t = np.append(t,r.t+dt)\n", " M = np.append(M, np.array([r.integrate(t[-1])]),axis=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot Results" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "_, ax = plt.subplots(figsize=(12,12))\n", "ax.plot(t,M[:,0], label='Mx')\n", "ax.plot(t,M[:,1], label='My')\n", "ax.plot(t,M[:,2], label='Mz')\n", "ax.legend()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }