Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
{
"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
}