1+ #------------------------------------------------------
2+ # This is an example of Function and SplineCH
3+ # They are containers for (x,y) (or (x,y,err) ) points
4+ # Function provides a linear interpolation, and SplineCH
5+ # uses 3-rd order polynomials. SplineCH can be used for
6+ # derivatives calculations.
7+ #-------------------------------------------------------
8+
9+ import sys
10+ import math
11+
12+ from orbit .core .orbit_utils import Function
13+ from orbit .core .orbit_utils import SplineCH
14+
15+ f = Function ()
16+
17+ def FF (x ):
18+ return math .sin (x )
19+
20+ def FFP (x ):
21+ return math .cos (x )
22+
23+ #------------------------------------------
24+ # Let's create PyORBIT Function = sin(x)
25+ #------------------------------------------
26+ n = 50
27+ step = 2 * math .pi / n
28+ for i in range (n ):
29+ x = step * i + 0.1 * ((1.0 * i )/ n )** 2 ;
30+ y = FF (x )
31+ f .add (x ,y )
32+
33+ """
34+ #---- Test of creating of x,y,err lists
35+ (x_arr,y_arr,err_arr) = f.getXYErrLists()
36+ print ("debug x_arr =",x_arr)
37+ print ("debug y_arr =",y_arr)
38+ print ("debug err_arr=",err_arr)
39+ sys.exit(0)
40+ """
41+
42+ """
43+ #----- Test of initialization form PyLists
44+ x_arr = [0.,1.,2.,3.]
45+ y_arr = [1.,2.,3.,4.]
46+ err_arr = [0.1,0.2,0.3,0.4]
47+ f_test = Function()
48+ #---------------------------------------
49+ f_test.initFromLists(x_arr,y_arr)
50+ f_test.dump()
51+ #-----------------------------------------
52+ f_test.initFromLists(x_arr,y_arr,err_arr)
53+ f_test.dump()
54+ sys.exit(0)
55+ """
56+
57+ #--------------------------------
58+ # This will print out Function
59+ #--------------------------------
60+ #f.dump()
61+ #f.dump("funcion_x_y_err.dat")
62+
63+ #--------------------------------------
64+ # This will create spline from Function
65+ #--------------------------------------
66+ spline = SplineCH ()
67+ spline .compile (f )
68+
69+ #--------------------------------
70+ # This will print out Spline
71+ #--------------------------------
72+ #spline.dump()
73+ #spline.dump("spline_x_y_err.dat")
74+
75+ print ("====================================" )
76+ n = 30
77+ step = 0.9 * (f .getMaxX () - f .getMinX ())/ (n - 1 )
78+ y_dev_max = 0.
79+ yp_dev_max = 0.
80+ for j in range (n ):
81+ x = f .getMinX () + j * step
82+ y = FF (x )
83+ yp = FFP (x )
84+ ys = spline .getY (x )
85+ yps = spline .getYP (x )
86+ ys = spline .getY (x )
87+ yps = spline .getYP (x )
88+ dy = abs (ys - y )
89+ dyp = abs (yps - yp )
90+ if (y_dev_max < dy ): y_dev_max = dy
91+ if (yp_dev_max < dyp ): yp_dev_max = dyp
92+ #------------------------
93+ st = " x= %+8.6f " % x
94+ st += " y = %+8.6f ys = %+8.6f " % (y ,ys )
95+ st += " err = %+8.6f " % dy
96+ st += " yp = %+8.6f yps = %+8.6f " % (yp ,yps )
97+ st += " err = %+8.6f " % dyp
98+ print (st )
99+
100+ print ("====================================" )
101+
102+ print (" max deviation y =" ,y_dev_max )
103+ print (" max deviation yp =" ,yp_dev_max )
104+
105+ print ("Stop." )
106+
107+ #sys.exit(0)
108+
109+ #----------------------------------
110+ # Test removePoint and updatePoint
111+ #----------------------------------
112+ print ("====================================" )
113+ f = Function ()
114+ f .add (1. ,1. )
115+ f .add (2. ,2. )
116+ f .add (3. ,3. )
117+
118+ f_test = Function ()
119+
120+ iteration = 0
121+
122+ while (1 < 2 ):
123+ #f.dump()
124+ #print ("====================================")
125+ f .removePoint (0 )
126+ #f.dump()
127+ #print ("====================================")
128+ f .add (1. ,1. )
129+ #f.dump()
130+ #print ("====================================")
131+ f .removePoint (1 )
132+ #f.dump()
133+ #print ("====================================")
134+ f .add (2. ,2. )
135+ #f.dump()
136+ #print ("====================================")
137+ f .removePoint (2 )
138+ #f.dump()
139+ #print ("====================================")
140+ f .add (3. ,3. )
141+ #f.dump()
142+ #print ("====================================")
143+ f .updatePoint (0 ,1. )
144+ f .updatePoint (1 ,2. )
145+ f .updatePoint (2 ,2. )
146+ #print ("====================================")
147+ (x_arr ,y_arr ,err_arr ) = f .getXYErrLists ()
148+ #print ("====================================")
149+ f_test .initFromLists (x_arr ,y_arr )
150+ #-----------------------------------------
151+ f_test .initFromLists (x_arr ,y_arr ,err_arr )
152+ #print ("====================================")
153+ if (iteration % 100000 == 0 ):
154+ print ("iter=" ,iteration )
155+ iteration += 1
156+
157+ print ("====================================" )
158+ print ("====================================" )
159+ print ("====================================" )
160+
161+ f .updatePoint (0 ,5. )
162+ f .dump ()
163+ print ("====================================" )
164+ f .updatePoint (2 ,7. ,3. )
165+ f .dump ()
166+ print ("====================================" )
0 commit comments