Created
March 17, 2023 01:04
-
-
Save andyfaff/6abc5ca58796f4e74cad213028b76e96 to your computer and use it in GitHub Desktop.
Find an adequate resolution smearing oversample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 162, | |
"id": "f5160716", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib inline\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"\n", | |
"import refnx\n", | |
"from refnx import reflect as refnx_reflect\n", | |
"import timeit\n", | |
"\n", | |
"import numpy as np\n", | |
"# a typical Q range set of points measured by TOF-NR\n", | |
"q = np.geomspace(0.01, 0.2, 101)\n", | |
"dq = q * 0.05\n", | |
"\n", | |
"w = np.array([[0, 2.07, 0, 0],\n", | |
" [100, 3.47, 0, 3],\n", | |
" [300, -0.5, 0.00001, 3],\n", | |
" [100, 3.47, 0, 3],\n", | |
" [300, -0.5, 0.00001, 3],\n", | |
" [0, 6.36, 0, 3]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 163, | |
"id": "6b490c2a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"s = refnx_reflect.Structure()\n", | |
"for i, slab in enumerate(w):\n", | |
" m = refnx_reflect.SLD(complex(slab[1], slab[2]))\n", | |
" s |= m(slab[0], slab[-1])\n", | |
" \n", | |
"model = refnx_reflect.ReflectModel(s, bkg=0)\n", | |
"model.dq.value = 5.0\n", | |
"model.quad_order = 17" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 191, | |
"id": "f5180d62", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def diff(a, b):\n", | |
" return np.abs((a - b) / b)\n", | |
"\n", | |
"def find_adequate_resolution(model, x, x_err=None, threads=-1, rtol=0.05):\n", | |
" _dq_type = model.dq_type\n", | |
" _quad_order = model.quad_order\n", | |
" _threads = model.threads\n", | |
" _dq = model.dq.value\n", | |
" \n", | |
" diffs = {}\n", | |
"\n", | |
" try:\n", | |
" model.dq_type='pointwise'\n", | |
" model.quad_order = 201\n", | |
" model.threads = threads\n", | |
" best_r = model.model(x, x_err=x_err)\n", | |
"\n", | |
" model.dq_type='constant'\n", | |
" if x_err is not None:\n", | |
" dq = 100 * np.mean(x_err/x)\n", | |
"\n", | |
" print(f\"Quad order: % difference, time (s)\") \n", | |
" # try no smear\n", | |
" model.dq.value = 0\n", | |
" none_r = model.model(x)\n", | |
" _t = timeit.Timer(\"model.model(x)\", globals=locals()).repeat(number=20, repeat=5)\n", | |
" print(f\"No smear : {100 * np.max(diff(none_r, best_r)): <25}, {np.mean(_t): ^8}\")\n", | |
" \n", | |
" model.dq.value = dq\n", | |
" continuous_r = model.model(x)\n", | |
" _t = timeit.Timer(\"model.model(x)\", globals=locals()).repeat(number=20, repeat=5)\n", | |
" print(f\"Continuous: {100 * np.max(diff(continuous_r, best_r)): <25}, {np.mean(_t): ^8}\")\n", | |
" \n", | |
" if x_err is None:\n", | |
" x_err = _dq * x\n", | |
"\n", | |
" for qo in range(1, 101, 2):\n", | |
" model.dq_type = 'pointwise'\n", | |
" model.quad_order = qo\n", | |
" r = model.model(x, x_err=x_err)\n", | |
" \n", | |
" diffs[qo] = np.max(diff(r, best_r))\n", | |
" _t = timeit.Timer(\"model.model(x, x_err=x_err)\", globals=locals()).repeat(number=20, repeat=5)\n", | |
" print(f\"Pointwise: {qo:<4}, {100 * np.max(diff(r, best_r)): <25}, {np.mean(_t): ^8}\")\n", | |
" finally:\n", | |
" model.threads = _threads\n", | |
" model.quad_order = _quad_order\n", | |
" model.dq_type = _dq_type\n", | |
" model.dq.value = _dq\n", | |
" \n", | |
" # find the lowest quad_order for which you satisfy the tolerance\n", | |
" for k, v in diffs.items():\n", | |
" if v < rtol:\n", | |
" return k" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 192, | |
"id": "12a1ee98", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Quad order: % difference, time (s)\n", | |
"No smear : 87.62278377619023 , 0.004772618412971497\n", | |
"Continuous: 5.934856302567495 , 0.037357107549905774\n", | |
"Pointwise: 1 , 368.0047544673799 , 0.005014491081237793\n", | |
"Pointwise: 3 , 108.3561706334832 , 0.013524832949042321\n", | |
"Pointwise: 5 , 56.24173757561278 , 0.014011584967374802\n", | |
"Pointwise: 7 , 20.13888797908223 , 0.022984862327575684\n", | |
"Pointwise: 9 , 3.305520733643539 , 0.024128807708621025\n", | |
"Pointwise: 11 , 2.692858632142202 , 0.030209342390298842\n", | |
"Pointwise: 13 , 2.1542188568601848 , 0.03211847990751267\n", | |
"Pointwise: 15 , 1.7013668796641648 , 0.04141804426908493\n", | |
"Pointwise: 17 , 1.3215564087095482 , 0.057372669130563735\n", | |
"Pointwise: 19 , 1.0007546661118902 , 0.06933653503656387\n", | |
"Pointwise: 21 , 0.7272279537180877 , 0.06541916765272618\n", | |
"Pointwise: 23 , 0.49175157088758087 , 0.05971390157938004\n", | |
"Pointwise: 25 , 0.5740754302168882 , 0.06798989921808243\n", | |
"Pointwise: 27 , 0.5043096855993633 , 0.07712197974324227\n", | |
"Pointwise: 29 , 0.16488300114770837 , 0.07353278212249278\n", | |
"Pointwise: 31 , 0.26843666000897853 , 0.09225127771496773\n", | |
"Pointwise: 33 , 0.31672110420689853 , 0.10340539067983627\n", | |
"Pointwise: 35 , 0.42988837051212886 , 0.09722417257726193\n", | |
"Pointwise: 37 , 0.5322144873609873 , 0.09131810069084167\n", | |
"Pointwise: 39 , 0.6251732739350431 , 0.09332728050649167\n", | |
"Pointwise: 41 , 0.7099853980190032 , 0.0993193406611681\n", | |
"Pointwise: 43 , 0.7876697565759285 , 0.10350952334702015\n", | |
"Pointwise: 45 , 0.3486123458505503 , 0.11353656686842442\n", | |
"Pointwise: 47 , 0.13969724128497601 , 0.11122776791453362\n", | |
"Pointwise: 49 , 0.11491612538413187 , 0.12902318574488164\n", | |
"Pointwise: 51 , 0.20117499303727035 , 0.12322373352944851\n", | |
"Pointwise: 53 , 0.22168121937118654 , 0.1260330155491829\n", | |
"Pointwise: 55 , 0.2326132256207918 , 0.12990813031792642\n", | |
"Pointwise: 57 , 0.2279332575871147 , 0.13689285926520825\n", | |
"Pointwise: 59 , 0.21253185430997523 , 0.13912916146218776\n", | |
"Pointwise: 61 , 0.18962808782448626 , 0.14705765545368193\n", | |
"Pointwise: 63 , 0.16143584768752675 , 0.1510664764791727\n", | |
"Pointwise: 65 , 0.12952571864046541 , 0.1517138347029686\n", | |
"Pointwise: 67 , 0.0950374185819811 , 0.171964718028903\n", | |
"Pointwise: 69 , 0.08605490972095102 , 0.17203781493008136\n", | |
"Pointwise: 71 , 0.08334787898237196 , 0.17165488861501216\n", | |
"Pointwise: 73 , 0.09046672947289981 , 0.1763641882687807\n", | |
"Pointwise: 75 , 0.054737720564031145 , 0.17652337066829205\n", | |
"Pointwise: 77 , 0.10826068765608746 , 0.18465997762978076\n", | |
"Pointwise: 79 , 0.1310309994924026 , 0.18573067225515844\n", | |
"Pointwise: 81 , 0.1687129194358679 , 0.19075977467000485\n", | |
"Pointwise: 83 , 0.20591336251847048 , 0.19403040036559105\n", | |
"Pointwise: 85 , 0.24254331672158322 , 0.19687076173722745\n", | |
"Pointwise: 87 , 0.2785372475494332 , 0.20455852672457694\n", | |
"Pointwise: 89 , 0.28357202004080595 , 0.2146530222147703\n", | |
"Pointwise: 91 , 0.06811190446391893 , 0.21141146421432494\n", | |
"Pointwise: 93 , 0.052295489300434525 , 0.21456486769020558\n", | |
"Pointwise: 95 , 0.049454094965603504 , 0.2298251323401928\n", | |
"Pointwise: 97 , 0.07032272194403144 , 0.22960584349930285\n", | |
"Pointwise: 99 , 0.08055479802646841 , 0.23020706400275232\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"9" | |
] | |
}, | |
"execution_count": 192, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"find_adequate_resolution(model, q, x_err=dq, threads=1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "f229edb1", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.10.9" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment