Skip to content

Commit cafba0e

Browse files
authored
Adds examples (#36)
* Adds validation and changes "ClassList.set_fields" functionality * Adds domains examples * Adds non polarised examples * Adds absorption example * Adds example for custom functions in different languages * Updates cells in "make_input.py" * Adds "function_name" field to customFile model * Adds wrappers for custom functions * Fixes bug when adding data in "inputs.py" * Adds run and plot lines to examples * Adds python custom functions * Fixes data resolution bug * Applies updates to submodule * Adds tests for custom function wrappers * Adds "check_indices" routine to "make_inputs.py" * Tidies up examples * Fixes bug in data model. * Moves example data into shared directoy and resolves paths in examples * Allows custom file model to accept pathlib.Path objects * Addresses review comments * Reintroduces "model_post_init" for "CustomFile" model * Removes warning from custom file validator * Adds additional "__init__.py" for test data
1 parent 558ad61 commit cafba0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2577
-67
lines changed

RAT/classlist.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,9 @@ def extend(self, other: Sequence[object]) -> None:
218218
def set_fields(self, index: int, **kwargs) -> None:
219219
"""Assign the values of an existing object's attributes using keyword arguments."""
220220
self._validate_name_field(kwargs)
221-
for key, value in kwargs.items():
222-
setattr(self.data[index], key, value)
221+
class_handle = self.data[index].__class__
222+
new_fields = {**self.data[index].__dict__, **kwargs}
223+
self.data[index] = class_handle(**new_fields)
223224

224225
def get_names(self) -> list[str]:
225226
"""Return a list of the values of the name_field attribute of each class object in the list.

RAT/examples/__init__.py

Whitespace-only changes.

RAT/examples/absorption/__init__.py

Whitespace-only changes.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""Custom layers model including absorption"""
2+
3+
import numpy as np
4+
import os
5+
import pathlib
6+
7+
import RAT
8+
import RAT.utils.plotting
9+
10+
problem = RAT.Project(name="Absorption example", calculation="non polarised", model="custom layers",
11+
geometry="substrate/liquid", absorption=True)
12+
13+
# Add the required parameters (substrate roughness is already there by default)
14+
problem.parameters.append(name="Alloy Thickness", min=100.0, value=135.6, max=200.0, fit=True)
15+
problem.parameters.append(name="Alloy SLD up", min=6.0e-6, value=9.87e-6, max=1.2e-5, fit=True)
16+
problem.parameters.append(name="Alloy SLD imaginary up", min=1.0e-9, value=4.87e-8, max=1.0e-7, fit=True)
17+
problem.parameters.append(name="Alloy SLD down", min=6.0e-6, value=7.05e-6, max=1.3e-5, fit=True)
18+
problem.parameters.append(name="Alloy SLD imaginary down", min=1.0e-9, value=4.87e-8, max=1.0e-7, fit=True)
19+
problem.parameters.append(name="Alloy Roughness", min=2.0, value=5.71, max=10.0, fit=True)
20+
problem.parameters.append(name="Gold Thickness", min=100.0, value=154.7, max=200.0, fit=True)
21+
problem.parameters.append(name="Gold Roughness", min=0.1, value=5.42, max=10.0, fit=True)
22+
problem.parameters.append(name="Gold SLD", min=4.0e-6, value=4.49e-6, max=5.0e-6, fit=True)
23+
problem.parameters.append(name="Gold SLD imaginary", min=1.0e-9, value=4.20e-8, max=1.0e-7, fit=True)
24+
25+
problem.parameters.append(name="Thiol APM", min=40.0, value=56.27, max=100.0, fit=True)
26+
problem.parameters.append(name="Thiol Head Hydration", min=20.0, value=30.0, max=50.0, fit=True)
27+
problem.parameters.append(name="Thiol Coverage", min=0.5, value=0.9, max=1.0, fit=True)
28+
29+
problem.parameters.append(name="CW Thickness", min=1.0, value=12.87, max=25.0, fit=True)
30+
problem.parameters.append(name="Bilayer APM", min=48.0, value=65.86, max=90.0, fit=True)
31+
problem.parameters.append(name="Bilayer Head Hydration", min=20.0, value=30.0, max=50.0, fit=True)
32+
problem.parameters.append(name="Bilayer Roughness", min=1.0, value=3.87, max=10.0, fit=True)
33+
problem.parameters.append(name="Bilayer Coverage", min=0.5, value=0.94, max=1.0, fit=True)
34+
35+
# Change the existing Bulk In parameter to be Silicon
36+
problem.bulk_in.set_fields(0, name="Silicon", min=2.0e-6, value=2.073e-6, max=2.1e-6)
37+
38+
# We need 2 bulk outs - D2O and H2O
39+
problem.bulk_out.set_fields(0, name="D2O", min=5.8e-06, value=6.21e-06, max=6.35e-06, fit=True)
40+
problem.bulk_out.append(name="H2O", min=-5.6e-07, value=-3.15e-07, max=0.0, fit=True)
41+
42+
# Use a different scalefactor for each dataset
43+
del problem.scalefactors[0]
44+
problem.scalefactors.append(name="Scalefactor 1", min=0.5, value=1, max=1.5, fit=True)
45+
problem.scalefactors.append(name="Scalefactor 2", min=0.5, value=1, max=1.5, fit=True)
46+
problem.scalefactors.append(name="Scalefactor 3", min=0.5, value=1, max=1.5, fit=True)
47+
problem.scalefactors.append(name="Scalefactor 4", min=0.5, value=1, max=1.5, fit=True)
48+
49+
# Similarly, use an individual background for each dataset
50+
del problem.backgrounds[0]
51+
del problem.background_parameters[0]
52+
53+
problem.background_parameters.append(name="Background parameter 1", min=5.0e-08, value=7.88e-06, max=9.0e-05, fit=True)
54+
problem.background_parameters.append(name="Background parameter 2", min=1.0e-08, value=5.46e-06, max=9.0e-05, fit=True)
55+
problem.background_parameters.append(name="Background parameter 3", min=1.0e-06, value=9.01e-06, max=9.0e-05, fit=True)
56+
problem.background_parameters.append(name="Background parameter 4", min=1.0e-06, value=5.61e-06, max=9.0e-05, fit=True)
57+
58+
problem.backgrounds.append(name="Background 1", type="constant", value_1="Background parameter 1")
59+
problem.backgrounds.append(name="Background 2", type="constant", value_1="Background parameter 2")
60+
problem.backgrounds.append(name="Background 3", type="constant", value_1="Background parameter 3")
61+
problem.backgrounds.append(name="Background 4", type="constant", value_1="Background parameter 4")
62+
63+
# Make the resolution fittable
64+
problem.resolution_parameters.set_fields(0, fit=True)
65+
66+
# Now add the data we need
67+
data_path = os.path.join(pathlib.Path(__file__).parents[1].resolve(), "data")
68+
69+
data_1 = np.loadtxt(os.path.join(data_path, "D2O_spin_down.dat"))
70+
problem.data.append(name="D2O_dn", data=data_1)
71+
72+
data_2 = np.loadtxt(os.path.join(data_path, "D2O_spin_up.dat"))
73+
problem.data.append(name="D2O_up", data=data_2)
74+
75+
data_3 = np.loadtxt(os.path.join(data_path, "H2O_spin_down.dat"))
76+
problem.data.append(name="H2O_dn", data=data_3)
77+
78+
data_4 = np.loadtxt(os.path.join(data_path, "H2O_spin_up.dat"))
79+
problem.data.append(name="H2O_up", data=data_4)
80+
81+
# Add the custom file
82+
problem.custom_files.append(name="DPPC absorption", filename="volume_thiol_bilayer.py", language="python",
83+
path=pathlib.Path(__file__).parent.resolve())
84+
85+
# Finally add the contrasts
86+
problem.contrasts.append(name="D2O Down", data="D2O_dn", background="Background 1", bulk_in="Silicon",
87+
bulk_out="D2O", scalefactor="Scalefactor 1", resolution="Resolution 1", resample=True,
88+
model=["DPPC absorption"])
89+
90+
problem.contrasts.append(name="D2O Up", data="D2O_up", background="Background 2", bulk_in="Silicon",
91+
bulk_out="D2O", scalefactor="Scalefactor 2", resolution="Resolution 1", resample=True,
92+
model=["DPPC absorption"])
93+
94+
problem.contrasts.append(name="H2O Down", data="H2O_dn", background="Background 3", bulk_in="Silicon",
95+
bulk_out="H2O", scalefactor="Scalefactor 3", resolution="Resolution 1", resample=True,
96+
model=["DPPC absorption"])
97+
98+
problem.contrasts.append(name="H2O Up", data="H2O_up", background="Background 4", bulk_in="Silicon",
99+
bulk_out="H2O", scalefactor="Scalefactor 4", resolution="Resolution 1", resample=True,
100+
model=["DPPC absorption"])
101+
102+
# Now make a controls block
103+
controls = RAT.set_controls(parallel="contrasts", resampleParams=[0.9, 150.0])
104+
105+
# Run the code and plot the results
106+
problem, results = RAT.run(problem, controls)
107+
RAT.utils.plotting.plot_ref_sld(problem, results, True)
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
def volume_thiol_bilayer(params, bulk_in, bulk_out, contrast):
2+
"""
3+
volumeThiolBilayer RAT Custom Layer Model File.
4+
5+
This file accepts 3 vectors containing the values for params, bulk in and bulk out.
6+
The final parameter is an index of the contrast being calculated
7+
8+
The function should output a matrix of layer values, in the form...
9+
10+
Output = [thick 1, SLD 1, Rough 1, Percent Hydration 1, Hydrate how 1
11+
....
12+
thick n, SLD n, Rough n, Percent Hydration n, Hydration how n]
13+
14+
The "hydrate how" parameter decides if the layer is hydrated with Bulk out or Bulk in phases.
15+
Set to 1 for Bulk out, zero for Bulk in.
16+
Alternatively, leave out hydration and just return...
17+
18+
Output = [thick 1, SLD 1, Rough 1,
19+
....
20+
thick n, SLD n, Rough n]
21+
22+
The second output parameter should be the substrate roughness.
23+
"""
24+
subRough = params[0]
25+
alloyThick = params[1]
26+
alloySLDUp = params[2]
27+
alloyISLDUp = params[3]
28+
alloySLDDown = params[4]
29+
alloyISLDDown = params[5]
30+
alloyRough = params[6]
31+
goldThick = params[7]
32+
goldRough = params[8]
33+
goldSLD = params[9]
34+
goldISLD = params[10]
35+
thiolAPM = params[11]
36+
thiolHeadHydr = params[12]
37+
thiolCoverage = params[13]
38+
cwThick = params[14]
39+
bilayerAPM = params[15]
40+
bilHeadHydr = params[16]
41+
bilayerRough = params[17]
42+
bilayerCoverage = params[18]
43+
44+
# Make the metal layers
45+
gold = [goldThick, goldSLD, goldISLD, goldRough]
46+
alloyUp = [alloyThick, alloySLDUp, alloyISLDUp, alloyRough]
47+
alloyDown = [alloyThick, alloySLDDown, alloyISLDDown, alloyRough]
48+
49+
# Neutron b's..
50+
# define all the neutron b's.
51+
bc = 0.6646e-4 # Carbon
52+
bo = 0.5843e-4 # Oxygen
53+
bh = -0.3739e-4 # Hydrogen
54+
bp = 0.513e-4 # Phosphorus
55+
bn = 0.936e-4 # Nitrogen
56+
bd = 0.6671e-4 # Deuterium
57+
58+
# Work out the total scattering length in each fragment
59+
# Define scattering lengths
60+
# Hydrogenated version
61+
COO = (2*bo) + (bc)
62+
GLYC = (3*bc) + (5*bh)
63+
CH3 = (1*bc) + (3*bh)
64+
PO4 = (1*bp) + (4*bo)
65+
CH2 = (1*bc) + (2*bh)
66+
CH = (1*bc) + (1*bh)
67+
CHOL = (5*bc) + (12*bh) + (1*bn)
68+
H2O = (2*bh) + (1*bo)
69+
D2O = (2*bd) + (1*bo)
70+
71+
# And also volumes
72+
vCH3 = 52.7 # CH3 volume in the paper appears to be for 2 * CH3's
73+
vCH2 = 28.1
74+
vCOO = 39.0
75+
vGLYC = 68.8
76+
vPO4 = 53.7
77+
vCHOL = 120.4
78+
vWAT = 30.4
79+
vCHCH = 42.14
80+
81+
vHead = vCHOL + vPO4 + vGLYC + 2*vCOO
82+
vTail = (28*vCH2) + (1*vCHCH) + (2*vCH3) # Tail volume
83+
84+
# Calculate sum_b's for other fragments
85+
sumbHead = CHOL + PO4 + GLYC + 2*COO
86+
sumbTail = (28*CH2) + (2*CH) + 2*CH3
87+
88+
# Calculate SLDs and Thickness
89+
sldHead = sumbHead/vHead
90+
thickHead = vHead/thiolAPM
91+
92+
sldTail = sumbTail/vTail
93+
thickTail = vTail/thiolAPM
94+
95+
# Correct head SLD based on hydration
96+
thiolHeadHydr = thiolHeadHydr/100
97+
sldHead = (sldHead * (1 - thiolHeadHydr) + (thiolHeadHydr * bulk_out[contrast]))
98+
99+
# Now correct both the SLDs for the coverage parameter
100+
sldTail = (thiolCoverage*sldTail) + ((1-thiolCoverage) * bulk_out[contrast])
101+
sldHead = (thiolCoverage*sldHead) + ((1-thiolCoverage) * bulk_out[contrast])
102+
103+
SAMTAILS = [thickTail, sldTail, 0, goldRough]
104+
SAMHEAD = [thickHead, sldHead, 0, goldRough]
105+
106+
# Now do the same for the bilayer
107+
vHead = vCHOL + vPO4 + vGLYC + 2*vCOO
108+
vTail = (28*vCH2) # Tail volume
109+
vMe = (2*vCH3)
110+
111+
sumbHead = CHOL + PO4 + GLYC + 2*COO
112+
sumbTail = (28*CH2)
113+
sumbMe = 2*CH3
114+
115+
sldHead = sumbHead/vHead
116+
thickHead = vHead/bilayerAPM
117+
bilHeadHydr = bilHeadHydr / 100
118+
sldHead = (sldHead * (1 - bilHeadHydr) + (bilHeadHydr * bulk_out[contrast]))
119+
120+
sldTail = sumbTail/vTail
121+
thickTail = vTail/bilayerAPM
122+
123+
sldMe = sumbMe/vMe
124+
thickMe = vMe/bilayerAPM
125+
126+
sldTail = (bilayerCoverage * sldTail) + ((1-bilayerCoverage) * bulk_out[contrast])
127+
sldHead = (bilayerCoverage * sldHead) + ((1-bilayerCoverage) * bulk_out[contrast])
128+
sldMe = (bilayerCoverage * sldMe) + ((1-bilayerCoverage) * bulk_out[contrast])
129+
130+
BILTAILS = [thickTail, sldTail, 0, bilayerRough]
131+
BILHEAD = [thickHead, sldHead, 0, bilayerRough]
132+
BILME = [thickMe, sldMe, 0, bilayerRough]
133+
134+
BILAYER = [BILHEAD, BILTAILS, BILME, BILME, BILTAILS, BILHEAD]
135+
136+
CW = [cwThick, bulk_out[contrast], 0, bilayerRough]
137+
138+
if contrast == 1 or contrast == 3:
139+
output = [alloyUp, gold, SAMTAILS, SAMHEAD, CW, *BILAYER]
140+
else:
141+
output = [alloyDown, gold, SAMTAILS, SAMHEAD, CW, *BILAYER]
142+
143+
return output, subRough
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
0.01218 1.015359223955 0.0231718818417596
2+
0.012545 0.919431439253596 0.0193509380578665
3+
0.012922 0.924921688167335 0.0183337262959345
4+
0.013309 0.892889622419078 0.01658695139614
5+
0.013709 0.88564788305433 0.0151834012597258
6+
0.01412 0.954057058169692 0.0152655865808885
7+
0.014544 0.801205833810502 0.0128764862406952
8+
0.01498 0.57206372730641 0.00977028529084846
9+
0.015429 0.507292263127758 0.00840850146518913
10+
0.015892 0.441240863619522 0.0072616120448651
11+
0.016369 0.411263430900333 0.00703930748762168
12+
0.01686 0.398699855165213 0.00667722051938428
13+
0.017366 0.369396072619489 0.00590757519620061
14+
0.017887 0.333584829398094 0.00529590083869447
15+
0.018423 0.300131361783826 0.00480817811310586
16+
0.018976 0.254862070126983 0.00436222169827209
17+
0.019545 0.218629795547172 0.0038320590117552
18+
0.020132 0.183303580450672 0.00337229276836539
19+
0.020736 0.158391997035939 0.00306386203644447
20+
0.021358 0.128249520024251 0.00266512849877059
21+
0.021998 0.0956717976354879 0.0021819192293442
22+
0.022658 0.0688201017211762 0.00176311091650106
23+
0.023338 0.0481760921553437 0.00145639799252248
24+
0.024038 0.0328252888275119 0.00118309811714777
25+
0.024759 0.0183323789955876 0.000868570851156994
26+
0.025502 0.0106467041665263 0.000644514803462562
27+
0.026267 0.00788878035636094 0.000551382666981037
28+
0.027055 0.0069025565024083 0.0005165549530129
29+
0.027867 0.00807841288019132 0.000666341069082825
30+
0.028703 0.0104166526322881 0.000419481963016605
31+
0.029564 0.0134376368351915 0.000465997507494358
32+
0.030451 0.0158250530499512 0.000492135134224797
33+
0.031365 0.018520664219071 0.000528646973626596
34+
0.032305 0.0188403112263801 0.000530903701707703
35+
0.033275 0.0189720098352925 0.000529859543938832
36+
0.034273 0.0167957829499141 0.000677826804540402
37+
0.035301 0.0143561588467109 0.000448314190440904
38+
0.03636 0.0120825895112668 0.000385361581730607
39+
0.037451 0.00911010812085284 0.000338273434605409
40+
0.038574 0.0064407693084981 0.000272228771598909
41+
0.039732 0.00380747078042373 0.000191909461416686
42+
0.040924 0.00219977095894102 0.000139024554548823
43+
0.042151 0.00106749974738118 9.30715079659133e-05
44+
0.043416 0.000526289198019469 6.53777493347705e-05
45+
0.044718 0.000538684361211223 6.36026811276904e-05
46+
0.04606 0.000666071609013439 6.89514635050019e-05
47+
0.047442 0.00096513860352319 8.1892283337263e-05
48+
0.048865 0.00123628279834282 9.06632085957762e-05
49+
0.050331 0.000932500252618815 7.56441779783758e-05
50+
0.051841 0.000704570716426959 6.33837448213143e-05
51+
0.053396 0.000422547071305871 4.87823773114622e-05
52+
0.054998 0.000225234935497996 3.52790595843578e-05
53+
0.056648 0.000224295193506012 3.47502441981879e-05
54+
0.058347 0.000348546599750749 4.2399541917882e-05
55+
0.060098 0.000753444036511839 6.21105459934656e-05
56+
0.061901 0.00108427363670046 0.000132874128465088
57+
0.063758 0.00147590016504429 6.80656135268955e-05
58+
0.06567 0.00157522988312169 6.65397958839974e-05
59+
0.06764 0.00123685540099027 5.63878877698811e-05
60+
0.06967 0.000809188588366061 4.4090403853279e-05
61+
0.07176 0.000394860049176463 3.01087945030146e-05
62+
0.073913 0.000158546936575836 1.87827141365489e-05
63+
0.07613 0.000205156792077874 2.11738354272626e-05
64+
0.078414 0.000432213951295092 3.08747347502442e-05
65+
0.080766 0.000654956381151268 3.77210414631682e-05
66+
0.083189 0.000798309138064603 4.33157061537943e-05
67+
0.085685 0.000842635319478595 5.73848900266092e-05
68+
0.088255 0.000695947994206608 4.00080838020816e-05
69+
0.090903 0.000486577520293712 3.1324396241032e-05
70+
0.09363 0.000518575903533295 3.11105123109569e-05
71+
0.096439 0.000572434234901815 3.26076998214827e-05
72+
0.099332 0.000634309003334568 3.34605409410893e-05
73+
0.10231 0.000482232476674863 2.81521775741857e-05
74+
0.10538 0.000365623631648085 2.39677995217084e-05
75+
0.10854 0.000216433695981677 1.81376940954562e-05
76+
0.1118 0.00016573141567584 1.53181312944188e-05
77+
0.11515 0.000215844252079895 1.6793425174307e-05
78+
0.11861 0.000301364141601267 1.9534170905049e-05
79+
0.12217 0.000246353868436121 1.75755330256998e-05
80+
0.12583 0.000162551786857085 1.40661524470343e-05
81+
0.12961 7.50985213378692e-05 9.36878978746337e-06
82+
0.13349 6.5539425376402e-05 8.69884468995251e-06
83+
0.1375 4.34100171780794e-05 7.01707703189734e-06
84+
0.14162 4.008555357203e-05 6.73885951025632e-06
85+
0.14587 3.09498467445855e-05 5.80450671966048e-06
86+
0.15025 9.04307992859308e-06 3.1003401933376e-06
87+
0.15476 1.20906733133484e-05 3.56933544410388e-06
88+
0.1594 1.50092626898851e-05 4.01428138367746e-06
89+
0.16418 2.38057866549901e-05 5.21304186735828e-06
90+
0.16911 2.45653272255718e-05 5.35080332783186e-06
91+
0.17418 1.01465189127286e-05 3.58853447404763e-06
92+
0.17941 9.15355855704133e-06 3.55249418976725e-06
93+
0.18479 4.82434571726902e-06 2.64124760012126e-06
94+
0.19033 1.52628919801947e-06 1.52659234059753e-06
95+
0.19604 3.69800262723568e-06 2.61612044865102e-06
96+
0.20192 1.54286099228671e-06 1.90198389976085e-06
97+
0.20798 1.2093704739129e-05 5.23729327360302e-06
98+
0.21422 2.37360638620364e-06 2.37431371888578e-06
99+
0.22065 5.71086934554886e-06 4.04257469096298e-06
100+
0.22727 3.01060999023207e-06 3.01168783050962e-06

0 commit comments

Comments
 (0)