Debian Codegraph @

examples/resolve_and_plot_model.py

raw symbols SCIP
1 """
2 Python script to showcase the simple model language by resolving a stack string
3 and plotting SLD and reflectivity for neutrons.
4
5 Script requires matplotlib and refnx to run:
6 pip install matplotlib refnx
7
8 If ran without installing orsopy, remember to put folder in PYTHONPATH.
9 Be aware of other orsopy version that might be installed by pip.
10 To be sure run: pip uninstall orsopy
11 """
12
13 import sys
14
15 import yaml
16
17 from matplotlib import pyplot
18 from numpy import linspace
19 from refnx.reflect import SLD, ReflectModel, Structure
20
21 from orsopy.fileio import model_language
22
23 q = linspace(0.001, 0.2, 200)
24
25
26 def main(txt=None):
27 if txt is None:
28 txt = sys.argv[1]
29 if txt.endswith(".yml"):
30 dtxt = yaml.safe_load(open(txt, "r").read())
31 if "data_source" in dtxt:
32 dtxt = dtxt["data_source"]
33 if "sample" in dtxt:
34 dtxt = dtxt["sample"]["model"]
35 sample = model_language.SampleModel(**dtxt)
36 txt += f"\n{sample.stack}"
37 else:
38 sample = model_language.SampleModel(stack=txt)
39 # initial model before resolving any names
40 print(repr(sample), "\n")
41 print("\n".join([repr(ss) for ss in sample.resolve_stack()]), "\n")
42
43 layers = sample.resolve_to_layers()
44 print("\n".join([repr(li) for li in layers]))
45
46 structure = Structure()
47 for lj in layers:
48 m = SLD(lj.material.get_sld() * 1e6)
49 structure |= m(lj.thickness.as_unit("angstrom"), lj.roughness.as_unit("angstrom"))
50 model = ReflectModel(structure, bkg=0.0)
51 structurex = Structure()
52 for lj in layers:
53 m = SLD(lj.material.get_sld(xray_energy="Cu") * 1e6)
54 structurex |= m(lj.thickness.as_unit("angstrom"), lj.roughness.as_unit("angstrom"))
55 modelx = ReflectModel(structurex, bkg=0.0)
56
57 pyplot.figure(figsize=(12, 5))
58 pyplot.subplot(121)
59 pyplot.semilogy(q, model(q), label="neutron")
60 pyplot.semilogy(q, modelx(q), label="x-ray (Cu)")
61 pyplot.legend()
62 pyplot.title(txt)
63 pyplot.xlabel("q [Å$^{-1}$]")
64 pyplot.ylabel("Neutron-reflectivity")
65 pyplot.subplot(122)
66 pyplot.plot(*structure.sld_profile(), label="neutron")
67 pyplot.plot(*structurex.sld_profile(), label="x-ray (Cu)")
68 pyplot.legend()
69 pyplot.title(txt)
70 pyplot.ylabel("SLD / $10^{-6} \\AA^{-2}$")
71 pyplot.xlabel("distance / $\\AA$")
72 pyplot.show()
73
74
75 if __name__ == "__main__":
76 main()