Skip to content

Commit f9d33cc

Browse files
committed
Adds jupyter notebooks for absorption and domains examples
1 parent 88445ca commit f9d33cc

File tree

9 files changed

+2919
-1
lines changed

9 files changed

+2919
-1
lines changed
Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import os\n",
10+
"import pathlib\n",
11+
"\n",
12+
"import numpy as np\n",
13+
"from IPython.display import Code\n",
14+
"\n",
15+
"import RATapi as RAT"
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"# Absorption (imaginary SLD) - effect below the critical edge\n",
23+
"\n",
24+
"RAT allows the use of an imaginary, as well as real part of the SLD. The effect of this is usually seen below the critical edge, and must sometimes be accounted for.\n",
25+
"\n",
26+
"The example used here is Custom Layers. It analyses a bilayer sample on a permalloy / gold substrate, measured using polarised neutrons, against D2O and H2O, leading to 4 contrasts in total. Absorption (i.e. imaginary SLD) is defined for Gold and the Permalloy, to account for non-flat data below the critical edge.\n",
27+
"\n",
28+
"For absorption with standard layers, an additional column appears in the layers block to accommodate the imagainary component of the SLD. For custom functions, we add an extra column to the output.\n",
29+
"\n",
30+
"For all calculation types, to activate this functionality it is necessary to set the 'absorption' flag when creating the project."
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 2,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"problem = RAT.Project(name=\"Absorption example\", calculation=\"non polarised\", model=\"custom layers\", geometry=\"substrate/liquid\", absorption=True)"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"We now define our parameters, noting that each SLD parameter has both a real and imaginary component:"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 3,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"problem.parameters.append(name=\"Alloy Thickness\", min=100.0, value=135.6, max=200.0, fit=True)\n",
56+
"problem.parameters.append(name=\"Alloy SLD up\", min=6.0e-6, value=9.87e-6, max=1.2e-5, fit=True)\n",
57+
"problem.parameters.append(name=\"Alloy SLD imaginary up\", min=1.0e-9, value=4.87e-8, max=1.0e-7, fit=True)\n",
58+
"problem.parameters.append(name=\"Alloy SLD down\", min=6.0e-6, value=7.05e-6, max=1.3e-5, fit=True)\n",
59+
"problem.parameters.append(name=\"Alloy SLD imaginary down\", min=1.0e-9, value=4.87e-8, max=1.0e-7, fit=True)\n",
60+
"problem.parameters.append(name=\"Alloy Roughness\", min=2.0, value=5.71, max=10.0, fit=True)\n",
61+
"problem.parameters.append(name=\"Gold Thickness\", min=100.0, value=154.7, max=200.0, fit=True)\n",
62+
"problem.parameters.append(name=\"Gold Roughness\", min=0.1, value=5.42, max=10.0, fit=True)\n",
63+
"problem.parameters.append(name=\"Gold SLD\", min=4.0e-6, value=4.49e-6, max=5.0e-6, fit=True)\n",
64+
"problem.parameters.append(name=\"Gold SLD imaginary\", min=1.0e-9, value=4.20e-8, max=1.0e-7, fit=True)\n",
65+
"\n",
66+
"problem.parameters.append(name=\"Thiol APM\", min=40.0, value=56.27, max=100.0, fit=True)\n",
67+
"problem.parameters.append(name=\"Thiol Head Hydration\", min=20.0, value=30.0, max=50.0, fit=True)\n",
68+
"problem.parameters.append(name=\"Thiol Coverage\", min=0.5, value=0.9, max=1.0, fit=True)\n",
69+
"\n",
70+
"problem.parameters.append(name=\"CW Thickness\", min=1.0, value=12.87, max=25.0, fit=True)\n",
71+
"problem.parameters.append(name=\"Bilayer APM\", min=48.0, value=65.86, max=90.0, fit=True)\n",
72+
"problem.parameters.append(name=\"Bilayer Head Hydration\", min=20.0, value=30.0, max=50.0, fit=True)\n",
73+
"problem.parameters.append(name=\"Bilayer Roughness\", min=1.0, value=3.87, max=10.0, fit=True)\n",
74+
"problem.parameters.append(name=\"Bilayer Coverage\", min=0.5, value=0.94, max=1.0, fit=True)"
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"metadata": {},
80+
"source": [
81+
"Set the bulk in and bulk out parameters:"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 4,
87+
"metadata": {},
88+
"outputs": [],
89+
"source": [
90+
"problem.bulk_in.set_fields(0, name=\"Silicon\", min=2.0e-6, value=2.073e-6, max=2.1e-6)\n",
91+
"\n",
92+
"problem.bulk_out.set_fields(0, name=\"D2O\", min=5.8e-06, value=6.21e-06, max=6.35e-06, fit=True)\n",
93+
"problem.bulk_out.append(name=\"H2O\", min=-5.6e-07, value=-3.15e-07, max=0.0, fit=True)"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"Use a different scalefactor for each dataset:"
101+
]
102+
},
103+
{
104+
"cell_type": "code",
105+
"execution_count": 5,
106+
"metadata": {},
107+
"outputs": [],
108+
"source": [
109+
"del problem.scalefactors[0]\n",
110+
"problem.scalefactors.append(name=\"Scalefactor 1\", min=0.5, value=1, max=1.5, fit=True)\n",
111+
"problem.scalefactors.append(name=\"Scalefactor 2\", min=0.5, value=1, max=1.5, fit=True)\n",
112+
"problem.scalefactors.append(name=\"Scalefactor 3\", min=0.5, value=1, max=1.5, fit=True)\n",
113+
"problem.scalefactors.append(name=\"Scalefactor 4\", min=0.5, value=1, max=1.5, fit=True)"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"metadata": {},
119+
"source": [
120+
"Set the backgrounds and resolutions:"
121+
]
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 6,
126+
"metadata": {},
127+
"outputs": [],
128+
"source": [
129+
"del problem.backgrounds[0]\n",
130+
"del problem.background_parameters[0]\n",
131+
"\n",
132+
"problem.background_parameters.append(name=\"Background parameter 1\", min=5.0e-08, value=7.88e-06, max=9.0e-05, fit=True)\n",
133+
"problem.background_parameters.append(name=\"Background parameter 2\", min=1.0e-08, value=5.46e-06, max=9.0e-05, fit=True)\n",
134+
"problem.background_parameters.append(name=\"Background parameter 3\", min=1.0e-06, value=9.01e-06, max=9.0e-05, fit=True)\n",
135+
"problem.background_parameters.append(name=\"Background parameter 4\", min=1.0e-06, value=5.61e-06, max=9.0e-05, fit=True)\n",
136+
"\n",
137+
"problem.backgrounds.append(name=\"Background 1\", type=\"constant\", value_1=\"Background parameter 1\")\n",
138+
"problem.backgrounds.append(name=\"Background 2\", type=\"constant\", value_1=\"Background parameter 2\")\n",
139+
"problem.backgrounds.append(name=\"Background 3\", type=\"constant\", value_1=\"Background parameter 3\")\n",
140+
"problem.backgrounds.append(name=\"Background 4\", type=\"constant\", value_1=\"Background parameter 4\")\n",
141+
"\n",
142+
"# Make the resolution fittable\n",
143+
"problem.resolution_parameters.set_fields(0, fit=True)\n"
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"metadata": {},
149+
"source": [
150+
"Add the datasets:"
151+
]
152+
},
153+
{
154+
"cell_type": "code",
155+
"execution_count": 7,
156+
"metadata": {},
157+
"outputs": [],
158+
"source": [
159+
"data_path = os.path.join(pathlib.Path.cwd().parents[0].resolve(), \"data\")\n",
160+
"\n",
161+
"data_1 = np.loadtxt(os.path.join(data_path, \"D2O_spin_down.dat\"))\n",
162+
"problem.data.append(name=\"D2O_dn\", data=data_1)\n",
163+
"\n",
164+
"data_2 = np.loadtxt(os.path.join(data_path, \"D2O_spin_up.dat\"))\n",
165+
"problem.data.append(name=\"D2O_up\", data=data_2)\n",
166+
"\n",
167+
"data_3 = np.loadtxt(os.path.join(data_path, \"H2O_spin_down.dat\"))\n",
168+
"problem.data.append(name=\"H2O_dn\", data=data_3)\n",
169+
"\n",
170+
"data_4 = np.loadtxt(os.path.join(data_path, \"H2O_spin_up.dat\"))\n",
171+
"problem.data.append(name=\"H2O_up\", data=data_4)"
172+
]
173+
},
174+
{
175+
"cell_type": "markdown",
176+
"metadata": {},
177+
"source": [
178+
"Add the custom file. We can see that we add an extra column for the output in our custom function."
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": 8,
184+
"metadata": {},
185+
"outputs": [
186+
{
187+
"ename": "NameError",
188+
"evalue": "name 'Code' is not defined",
189+
"output_type": "error",
190+
"traceback": [
191+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
192+
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
193+
"Cell \u001b[0;32mIn[8], line 7\u001b[0m\n\u001b[1;32m 1\u001b[0m problem\u001b[38;5;241m.\u001b[39mcustom_files\u001b[38;5;241m.\u001b[39mappend(\n\u001b[1;32m 2\u001b[0m name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDPPC absorption\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 3\u001b[0m filename\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvolume_thiol_bilayer.py\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 4\u001b[0m language\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpython\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 5\u001b[0m path\u001b[38;5;241m=\u001b[39mpathlib\u001b[38;5;241m.\u001b[39mPath\u001b[38;5;241m.\u001b[39mcwd()\u001b[38;5;241m.\u001b[39mresolve(),\n\u001b[1;32m 6\u001b[0m )\n\u001b[0;32m----> 7\u001b[0m \u001b[43mCode\u001b[49m(filename\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvolume_thiol_bilayer.py\u001b[39m\u001b[38;5;124m'\u001b[39m, language\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpython\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
194+
"\u001b[0;31mNameError\u001b[0m: name 'Code' is not defined"
195+
]
196+
}
197+
],
198+
"source": [
199+
"problem.custom_files.append(\n",
200+
" name=\"DPPC absorption\",\n",
201+
" filename=\"volume_thiol_bilayer.py\",\n",
202+
" language=\"python\",\n",
203+
" path=pathlib.Path.cwd().resolve(),\n",
204+
")\n",
205+
"Code(filename='volume_thiol_bilayer.py', language='python')"
206+
]
207+
},
208+
{
209+
"cell_type": "markdown",
210+
"metadata": {},
211+
"source": [
212+
"Finally, add the contrasts:"
213+
]
214+
},
215+
{
216+
"cell_type": "code",
217+
"execution_count": null,
218+
"metadata": {},
219+
"outputs": [],
220+
"source": [
221+
"problem.contrasts.append(\n",
222+
" name=\"D2O Down\",\n",
223+
" data=\"D2O_dn\",\n",
224+
" background=\"Background 1\",\n",
225+
" bulk_in=\"Silicon\",\n",
226+
" bulk_out=\"D2O\",\n",
227+
" scalefactor=\"Scalefactor 1\",\n",
228+
" resolution=\"Resolution 1\",\n",
229+
" resample=True,\n",
230+
" model=[\"DPPC absorption\"],\n",
231+
")\n",
232+
"\n",
233+
"problem.contrasts.append(\n",
234+
" name=\"D2O Up\",\n",
235+
" data=\"D2O_up\",\n",
236+
" background=\"Background 2\",\n",
237+
" bulk_in=\"Silicon\",\n",
238+
" bulk_out=\"D2O\",\n",
239+
" scalefactor=\"Scalefactor 2\",\n",
240+
" resolution=\"Resolution 1\",\n",
241+
" resample=True,\n",
242+
" model=[\"DPPC absorption\"],\n",
243+
")\n",
244+
"\n",
245+
"problem.contrasts.append(\n",
246+
" name=\"H2O Down\",\n",
247+
" data=\"H2O_dn\",\n",
248+
" background=\"Background 3\",\n",
249+
" bulk_in=\"Silicon\",\n",
250+
" bulk_out=\"H2O\",\n",
251+
" scalefactor=\"Scalefactor 3\",\n",
252+
" resolution=\"Resolution 1\",\n",
253+
" resample=True,\n",
254+
" model=[\"DPPC absorption\"],\n",
255+
")\n",
256+
"\n",
257+
"problem.contrasts.append(\n",
258+
" name=\"H2O Up\",\n",
259+
" data=\"H2O_up\",\n",
260+
" background=\"Background 4\",\n",
261+
" bulk_in=\"Silicon\",\n",
262+
" bulk_out=\"H2O\",\n",
263+
" scalefactor=\"Scalefactor 4\",\n",
264+
" resolution=\"Resolution 1\",\n",
265+
" resample=True,\n",
266+
" model=[\"DPPC absorption\"],\n",
267+
")"
268+
]
269+
},
270+
{
271+
"cell_type": "markdown",
272+
"metadata": {},
273+
"source": [
274+
"Now run RAT and plot the results."
275+
]
276+
},
277+
{
278+
"cell_type": "code",
279+
"execution_count": null,
280+
"metadata": {},
281+
"outputs": [],
282+
"source": [
283+
"controls = RAT.Controls(parallel=\"contrasts\", resampleParams=[0.9, 150.0])\n",
284+
"problem, results = RAT.run(problem, controls)\n",
285+
"\n",
286+
"RAT.plotting.plot_ref_sld(problem, results)"
287+
]
288+
}
289+
],
290+
"metadata": {
291+
"kernelspec": {
292+
"display_name": "Python 3 (ipykernel)",
293+
"language": "python",
294+
"name": "python3"
295+
},
296+
"language_info": {
297+
"codemirror_mode": {
298+
"name": "ipython",
299+
"version": 3
300+
},
301+
"file_extension": ".py",
302+
"mimetype": "text/x-python",
303+
"name": "python",
304+
"nbconvert_exporter": "python",
305+
"pygments_lexer": "ipython3",
306+
"version": "3.10.12"
307+
}
308+
},
309+
"nbformat": 4,
310+
"nbformat_minor": 4
311+
}

0 commit comments

Comments
 (0)