1
2
3
4 """Pure numeric python implementation of Parratt's recursion for computing
5 reflectivity as a function of Q and lambda."""
6
7 import pylab as Plot
8 import numpy as N
9 import parratt
10
12 """Compute reflectometry theory from using parratt formalism"""
13 r = parratt.refl(Q,d,rho)
14 R = r*N.conj(r)
15 return R.real
16
23
25 """Compute chisq(p) given f(p),y,dy"""
27 self.y,self.dy,self.f = y,dy,f
29 return N.sum(((self.f(p)-self.y)/self.dy)**2)
30
32 """Construct a 2D landscape for the function f((x,y)) over
33 x=[lo, hi], y=[lo,hi] with the number of steps dim=(nx,ny)
34 defaulting to 50x50
35 """
36 d1 = N.linspace(x[0],x[1],dim[0])
37 d2 = N.linspace(y[0],y[1],dim[1])
38 Z = N.zeros((len(d1),len(d2)))
39 for i in range(len(d1)):
40 for j in range(len(d2)):
41 Z[i,j] = fn((d1[i],d2[j]))
42 return d1,d2,Z
43
45 """Plot a regular mesh z with grid spacing x,y"""
46 X,Y = N.meshgrid(x,y)
47
48 Plot.contourf(X,Y,Z)
49 Plot.show()
50
52 """Example model. Defines a reflectometry model object for a structure and a
53 a dataset.
54
55 model(p) returns f(x;p) corresponding to dataset measured points.
56 model.goodness(p) returns chisq for the given parameters.
57 model.x indicates the measured points
58 model.y, model.dy is the measurement and uncertainty at the points
59 model.rho,model.d are the parameters to the reflectometry model
60 """
62 """Create a new instance of the demo model"""
63 self.d = N.array([0,20,130,500,20,0],'d')
64 self.rho = N.array([0,6e-4,5e-4,9e-4,10e-4,2e-4],'d')
65 self.x = N.linspace(0.001,0.3,100)
66 self.y,self.dy = sampledata(self.x,self.d,self.rho)
67 self.goodness = Chisq(self,self.y,self.dy)
68
70 """Evaluate the model at parameter vector p, returning f(x;p)"""
71
72 d = self.d.copy()
73 rho = self.rho.copy()
74
75
76 d[2],d[3] = p
77
78
79 return theory(self.x,d,rho)
80
81 demo_model = Demo_Model()
82
83
87
88 if __name__ == "__main__":
89 demo_landscape()
90
91 __id__ = "$Id: demo_parratt.py 37 2006-02-10 22:13:51Z pkienzle $"
92