| 1 |
""" |
| 2 |
python script to illustrate an application of the simple model language. |
| 3 |
|
| 4 |
usage: python3 sample2data.py <stack> [<angle> <time>] [<angle2> <time2>] ... |
| 5 |
with <stack> = sample description according to simple model language |
| 6 |
<angle> = sample orientation 'mu' on Amor |
| 7 |
<time> = counting time |
| 8 |
! The intensity and time parameters are wild guesses and will be |
| 9 |
! adjusted to real measurements in the future. |
| 10 |
|
| 11 |
If no angles are given, a set of default angle - time pairs is used. |
| 12 |
|
| 13 |
Output are two graphs: |
| 14 |
- the q-dependent flux on the sample for a given angle |
| 15 |
- the calculated reflectivity with errorbars |
| 16 |
This might help to plan the experiment (angles and counting times). |
| 17 |
|
| 18 |
Not yet implemented is a variable resolution or a reduced divergence. |
| 19 |
|
| 20 |
Jochen Stahn, 2022-03-30 |
| 21 |
""" |
| 22 |
|
| 23 |
import sys |
| 24 |
|
| 25 |
import numpy as np |
| 26 |
import yaml |
| 27 |
|
| 28 |
from matplotlib import pyplot |
| 29 |
from refnx.reflect import SLD, ReflectModel, Structure |
| 30 |
|
| 31 |
from orsopy.fileio import model_language |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
def Imap(mu): |
| 37 |
theta_t = np.array(np.arange(-0.7, 0.701, 0.01), ndmin=2) |
| 38 |
I_t = np.exp(-(theta_t ** 2) / 1.6) |
| 39 |
lamda_l = np.array(np.arange(3.5, 12.01, 0.01), ndmin=2) |
| 40 |
I_l = np.exp(-((lamda_l - 4) ** 2) / 30) |
| 41 |
q_lt = np.matmul(1 / lamda_l.T, 4.0 * np.pi * np.sin(np.deg2rad(theta_t + mu))) |
| 42 |
I_lt = np.matmul(I_l.T, I_t * np.sin(np.deg2rad(theta_t + mu))) |
| 43 |
return q_lt, I_lt |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
def qBins(resolution=0.02, qMin=0.005, qMax=0.42): |
| 48 |
n = np.log(qMax / qMin) / np.log(1.0 + resolution) |
| 49 |
return qMin * (1.0 + resolution) ** np.arange(n) |
| 50 |
|
| 51 |
|
| 52 |
def main(txt=None): |
| 53 |
|
| 54 |
if txt is None: |
| 55 |
txt = sys.argv[1] |
| 56 |
if txt.endswith(".yml"): |
| 57 |
dtxt = yaml.safe_load(open(txt, "r").read()) |
| 58 |
if "data_source" in dtxt: |
| 59 |
dtxt = dtxt["data_source"] |
| 60 |
if "sample" in dtxt: |
| 61 |
dtxt = dtxt["sample"]["model"] |
| 62 |
sample = model_language.SampleModel(**dtxt) |
| 63 |
else: |
| 64 |
sample = model_language.SampleModel(stack=txt) |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
layers = sample.resolve_to_layers() |
| 70 |
structure = Structure() |
| 71 |
for li in reversed(layers): |
| 72 |
m = SLD(li.material.get_sld() * 1e6) |
| 73 |
if li.thickness.unit == "nm": |
| 74 |
structure |= m(li.thickness.magnitude * 10.0, li.roughness.magnitude * 10.0) |
| 75 |
else: |
| 76 |
structure |= m(li.thickness.magnitude, li.roughness.magnitude) |
| 77 |
model = ReflectModel(structure, bkg=0.0) |
| 78 |
|
| 79 |
bins_q = qBins() |
| 80 |
q = (bins_q[:-1] + bins_q[1:]) / 2.0 |
| 81 |
R_q = model(q) |
| 82 |
|
| 83 |
|
| 84 |
if len(sys.argv) > 3: |
| 85 |
mus = np.array(list(zip(sys.argv[2::2], sys.argv[3::2])), dtype=float) |
| 86 |
else: |
| 87 |
mus = np.array([[0.8, 300], [2.3, 6000], [6.0, 18000]], dtype=float) |
| 88 |
|
| 89 |
pyplot.figure(figsize=(12, 5)) |
| 90 |
|
| 91 |
for mu, tme in mus: |
| 92 |
|
| 93 |
q_lt, I_lt = Imap(mu) |
| 94 |
q_q = q_lt.flatten() |
| 95 |
I_q = I_lt.flatten() |
| 96 |
p_q, bins = np.histogram(q_q, bins=bins_q, weights=I_q) |
| 97 |
|
| 98 |
fltr = p_q > 0 |
| 99 |
pyplot.subplot(121) |
| 100 |
pyplot.plot(q[fltr], p_q[fltr], linewidth=2.0, label=f"$\\mu$={mu}") |
| 101 |
|
| 102 |
|
| 103 |
sigma_q = np.where(p_q > 1e-4 * R_q, np.sqrt(R_q / (p_q + 1e-40)), 0) / np.sqrt(tme) |
| 104 |
|
| 105 |
pyplot.subplot(122) |
| 106 |
pyplot.errorbar( |
| 107 |
q[fltr], R_q[fltr], yerr=sigma_q[fltr], lw=0.4, marker="o", ms=2.5, label=f"{tme}s @ $\\mu$={mu}" |
| 108 |
) |
| 109 |
|
| 110 |
pyplot.subplot(121) |
| 111 |
pyplot.title("incoming intensity vs. q") |
| 112 |
pyplot.xlabel("$q \\,/\\, \\mathrm{Å}^{-1}$") |
| 113 |
pyplot.ylabel("intensity") |
| 114 |
pyplot.legend() |
| 115 |
|
| 116 |
pyplot.subplot(122) |
| 117 |
pyplot.title("$q_z$ ranges and statistics") |
| 118 |
pyplot.xlabel("$q \\,/\\,\\mathrm{Å}^{-1}$") |
| 119 |
pyplot.ylabel("$\\log_{10}R(q_z)$") |
| 120 |
pyplot.loglog(q, R_q, c="k", lw=1) |
| 121 |
pyplot.text(0.01, 1.2, txt) |
| 122 |
pyplot.legend() |
| 123 |
|
| 124 |
pyplot.show() |
| 125 |
|
| 126 |
|
| 127 |
if __name__ == "__main__": |
| 128 |
main() |