-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathareaSlab.py
More file actions
361 lines (286 loc) · 11.9 KB
/
areaSlab.py
File metadata and controls
361 lines (286 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 18 13:32:40 2018
@author: igres
"""
from refnx.analysis import Parameters, Parameter, possibly_create_parameter
from refnx.reflect import SLD, Component, Structure
import warnings
import numpy as np
class ConstrainedAmountModel(Component):
"""
Component that scales the length of a structure to ensure that adsorbed
amount of a component of interest (COI) is constrained.
If no pure_SLD is provided the system is treated as a 2-component system,
made up of the COI and the system solvent (which is supplied to the final
structure). The solvent volume fractions (and thicknesses) of the provided
slabs are used to calculate the adsorbed amount. No questions
are asked if slab SLDs differ.
If a pure_SLD is provided the system is treated as a 3-component system,
made up of the COI, the system solvent and a 'filler' substance, which is
assumed to be air (SLD 0). The solvent volume fractions are used as the
solvent fractions, and the slab SLDs and pure SLDs are used to calculate
the filler-COI volume fractions. The overall COI volume fraction is used
to calculate (and constrain) the adsorbed amount.
If both a pure_SLD and filler_SLD is provided the system is treated as a
3-component system, made up of the COI, the system solvent and a 'filler'
substance. The solvent volume fractions are used as the solvent fractions,
and the slab SLDs and pure SLDs are used to calculate the filler-COI volume
fractions. The overall COI volume fraction is used to calculate (and
constrain) the adsorbed amount.
Parameters:
pure_thick (float) - Hypothetical thickness of a pure layer of the
compound of interest.
structure (refnx.reflect.structure.Structure) - structure of slabs, the
adsorbed amount of which will be constrained by scaling its thickness.
name (str) - optional identifier for the component.
pure_sld (SLD, float, None) - SLD of the pure compound of interest.
pure_sld (SLD, float, None) - SLD of the filler (non solvent) compound.
"""
def __init__(self, pure_thick, structure, name='', pure_sld=None, filler_sld=None):
super(ConstrainedAmountModel, self).__init__()
self.name = name
self.pure_thick = possibly_create_parameter(pure_thick,
name='%s - dry thickness' % self.name)
self.structure = structure
self.pure_sld = pure_sld
if isinstance(pure_sld, SLD):
self.pure_sld = pure_sld
elif pure_sld != None:
self.pure_sld = SLD(pure_sld)
if isinstance(filler_sld, SLD):
self.filler_sld = filler_sld
elif filler_sld != None:
self.filler_sld = SLD(filler_sld)
elif pure_sld != None:
print ('warning: Filler SLD assumed to be 0')
self.filler_sld = SLD(0)
def profile(self, reverse=False, end_roughness=0):
"""
Calculates the volume fraction profile
Returns
-------
z, vfp : np.ndarray
Distance from the interface, volume fraction profile
"""
# Just create a SLD structure that varies between 0 and 1. Bit of a
# hack, but works fine.
s = Structure()
s |= SLD(0)(0)
m = SLD(1.)
for i, slab in enumerate(self.slabs()):
layer = m(slab[0], slab[3])
if self.pure_sld == None: #Two component system
layer.vfsolv.value = slab[4]
else: #3+ component system
# Get COI:Filler ratio from SLD
unsolvated_nonfill_fraction =\
(slab[1] - self.filler_sld.real.value)/ (self.pure_sld.real.value - self.filler_sld.real.value)
# Multiply COI:filler fraction by COI+Filler:Solvent Fraction
layer.vfsolv.value = 1-((1-slab[4])*unsolvated_nonfill_fraction)
s |= layer
s |= SLD(0)(0,end_roughness)
s.solvent = 0
s.reverse_structure = reverse
# now calculate the VFP.
total_thickness = np.sum(self.slabs()[:, 0])
buffer = total_thickness*0.1
zed = np.linspace(-buffer, buffer+total_thickness, 1000)
z, s = s.sld_profile(z=zed)
return z, s
@property
def parameters(self):
p = Parameters(name=self.name)
p.extend([self.pure_thick])
p.extend([component.parameters for component in self.structure])
return p
def slabs(self, structure=None):
"""
slab representation of this component. See :class:`Structure.slabs`
"""
if self.pure_sld == None: # Two component System
struct_purethick = np.sum(self.structure.slabs[:,0]*(
1-self.structure.slabs[:,4]))
else: # 3+ Component system
# Find COI:filler ratio
# (obs_SLD - filler_SLD)/(pure_SLD - filler_SLD)
unsolvated_nonfill_fraction = ((self.structure.slabs[:,1] - self.filler_sld.real.value)/
(self.pure_sld.real.value - self.filler_sld.real.value))
# Find theoretical thickness of a pure COI layer
struct_purethick = np.sum(self.structure.slabs[:,0]*
(1-self.structure.slabs[:,4])*
unsolvated_nonfill_fraction)
# Will scale thickness such that theoretical thickness will match set
# thickness.
scale = self.pure_thick/float(struct_purethick)
new_slabs = self.structure.slabs
new_slabs[:,0] = new_slabs[:,0]*scale
new_slabs[:,3] = new_slabs[:,3]*scale # Will also scale roughnesses
return new_slabs
class area_slabVF(Component):
"""
A slab component has uniform SLD over its thickness.
Parameters
----------
adsorbed_amount : refnx.analysis.Parameter or float
thickness of unsolvated slab (Angstrom)
sld : refnx.reflect.SLD, complex, or float
(complex) SLD of film (/1e-6 Angstrom**2)
rough : float
roughness on top of this slab (Angstrom)
name : str
Name of this slab
vfsolv : refnx.analysis.Parameter or float
Volume fraction of solvent [0, 1]
"""
def __init__(self, adsorbed_amount, dry_sld, rough, name='', vfsolv=0):
super().__init__()
self.adsorbed_amount = possibly_create_parameter(adsorbed_amount,
name='%s - dry thickness' % name)
if isinstance(dry_sld, SLD):
self.sld = dry_sld
else:
self.sld = SLD(dry_sld, name=name)
self.rough = possibly_create_parameter(rough,
name='%s - rough' % name)
self.vfsolv = (
possibly_create_parameter(vfsolv,
name='%s - volfrac solvent' % name))
self.name = name
p = Parameters(name=self.name)
p.extend([self.adsorbed_amount, self.sld.real, self.sld.imag,
self.rough, self.vfsolv])
self._parameters = p
@property
def parameters(self):
"""
:class:`refnx.analysis.Parameters` associated with this component
"""
self._parameters.name = self.name
return self._parameters
def slabs(self, structure=None):
"""
slab representation of this component. See :class:`Structure.slabs`
"""
return np.atleast_2d(np.array([self.thick,
self.sld.real.value,
self.sld.imag.value,
self.rough.value,
self.vfsolv.value]))
@property
def thick(self):
return self.adsorbed_amount.value / (1 - self.vfsolv.value)
def is_monotonic(self):
return True
def moment(self):
return self.thick / 2
def profile(self, reverse=False):
"""
returns the vfp for this component.
"""
m = SLD(1.)
s = Structure()
s |= SLD(0)
slab = self.slabs()[0]
thick = slab[0]
rough = slab[3]
vfsolv = slab[4]
layer = m(thick, rough)
layer.vfsolv.value = vfsolv
s |= layer
s |= SLD(0)
s.solvent = SLD(0)
if reverse:
s.reverse_structure = True
zed = np.linspace(0, thick * 1.1, thick * 1.1 + 1)
zed[0] = 0.01
z, s = s.sld_profile(z=zed)
return z, s
class area_slabT(Component):
"""
A slab component has uniform SLD over its thickness.
Parameters
----------
adsorbed_amount : refnx.analysis.Parameter or float
thickness of unsolvated slab (Angstrom)
sld : refnx.reflect.SLD, complex, or float
(complex) SLD of film (/1e-6 Angstrom**2)
rough : float
roughness on top of this slab (Angstrom)
name : str
Name of this slab
vfsolv : refnx.analysis.Parameter or float
Volume fraction of solvent [0, 1]
"""
def __init__(self, adsorbed_amount, dry_sld, thick, rough, name=''):
super().__init__()
self.adsorbed_amount = possibly_create_parameter(adsorbed_amount,
name='%s - dry thickness' % name)
if isinstance(dry_sld, SLD):
self.sld = dry_sld
else:
self.sld = SLD(dry_sld, name=name)
self.rough = possibly_create_parameter(rough,
name='%s - rough' % name)
self.thick = (
possibly_create_parameter(thick,
name='%s - thick' % name))
self.name = name
p = Parameters(name=self.name)
p.extend([self.adsorbed_amount, self.thick, self.sld.real,
self.sld.imag, self.rough])
self._parameters = p
@property
def parameters(self):
"""
:class:`refnx.analysis.Parameters` associated with this component
"""
self._parameters.name = self.name
return self._parameters
def is_monotonic(self):
return True
def __repr__(self):
# sld = repr(self.sld)
#
# s = 'Slab: {0}\n thick = {1} Å, {2}, rough = {3} Å,
# \u03D5_solv = {4}'
# t = s.format(self.name, self.thick.value, sld, self.rough.value,
# self.vfsolv.value)
return repr(self.parameters)
def slabs(self, structure=None):
"""
slab representation of this component. See :class:`Structure.slabs`
"""
vfsolv = 1 - self.adsorbed_amount.value/self.thick.value
if vfsolv < 0:
vfsolv = 0
warnings.warn('Layer thickness less than adsorbed amount. Clipping vfsolv to 0', RuntimeWarning)
return np.atleast_2d(np.array([self.thick.value,
self.sld.real.value,
self.sld.imag.value,
self.rough.value,
vfsolv]))
def moment(self):
return self.thick.value/2
def profile(self, reverse=False):
"""
returns the vfp for this component.
"""
m = SLD(1.)
s = Structure()
s |= SLD(0)
slab = self.slabs()[0]
thick = slab[0]
rough = slab[3]
vfsolv = slab[4]
layer = m(thick, rough)
layer.vfsolv.value = vfsolv
s |= layer
s |= SLD(0)
s.solvent = SLD(0)
if reverse:
s.reverse_structure = True
zed = np.linspace(0, thick*1.1, thick*1.1 + 1)
zed[0] = 0.01
z, s = s.sld_profile(z=zed)
return z, s