1 | try: |
---|
2 | from sans.models.prototypes.SimSphereF import SimSphereF |
---|
3 | except: |
---|
4 | print "This test uses the prototypes module." |
---|
5 | from sans.models.SphereModel import SphereModel |
---|
6 | import time, math, random |
---|
7 | |
---|
8 | def reset(seed=0): |
---|
9 | sim = SimSphereF() |
---|
10 | sim.setParam('radius', 60) |
---|
11 | sim.setParam('seed', math.fmod(time.time()*100+seed,10000)) |
---|
12 | return sim |
---|
13 | |
---|
14 | # number of times to repeat each point |
---|
15 | N_q = 50 |
---|
16 | # Relative error on the simulation |
---|
17 | sig_val = 0.10 |
---|
18 | |
---|
19 | |
---|
20 | counter = 0 |
---|
21 | |
---|
22 | sim = reset() |
---|
23 | sph = SphereModel() |
---|
24 | |
---|
25 | |
---|
26 | sph.setParam('radius', 60) |
---|
27 | sph.setParam('scale', 1) |
---|
28 | |
---|
29 | vol = 4/3*math.pi*60*60*60 |
---|
30 | |
---|
31 | f = open('sim_err_4.txt', 'w') |
---|
32 | f.write("<q> <npts> <sigma>\n") |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | |
---|
37 | for i_npts in range(15): |
---|
38 | |
---|
39 | # Find which q has an error of sig_val |
---|
40 | #print err |
---|
41 | q_sigma = 0 |
---|
42 | i_min = 0 |
---|
43 | i_max = 0 |
---|
44 | i_last_max = 0 |
---|
45 | val_min = 0 |
---|
46 | val_max = 0 |
---|
47 | last_error = 0 |
---|
48 | last_val = 0 |
---|
49 | error_vec = [] |
---|
50 | |
---|
51 | found_it = False |
---|
52 | i_vec = 0 |
---|
53 | |
---|
54 | |
---|
55 | t_0 = time.time() |
---|
56 | npts = 50000*i_npts+1000 |
---|
57 | #npts = 1000 * math.pow(2.0,(i_npts+1)) |
---|
58 | |
---|
59 | sim = reset() |
---|
60 | sim.setParam('npoints', npts) |
---|
61 | |
---|
62 | # Vectors of errors |
---|
63 | err = [] |
---|
64 | for i in range(80): |
---|
65 | q = 0.40*(i) /50 |
---|
66 | |
---|
67 | |
---|
68 | # For each Q, repeat the simulation N_q times |
---|
69 | sum_q_ana = 0 |
---|
70 | sum_q_sim = 0 |
---|
71 | sum = 0 |
---|
72 | vec = [] |
---|
73 | for i_q in range(N_q): |
---|
74 | counter += 1 |
---|
75 | sim = reset(counter) |
---|
76 | sim.setParam('npoints', npts) |
---|
77 | |
---|
78 | simval = sim.run([q, 0]) |
---|
79 | sum_q_ana += sph.run([q, 0]) |
---|
80 | sum_q_sim += simval |
---|
81 | |
---|
82 | vec.append(simval) |
---|
83 | |
---|
84 | mean = sum_q_sim/N_q |
---|
85 | for val in range(N_q): |
---|
86 | diff = vec[val] - mean |
---|
87 | sum += diff * diff |
---|
88 | sum /= (N_q-1) |
---|
89 | |
---|
90 | ana_value = sum_q_ana/N_q |
---|
91 | sim_value = sum_q_sim/N_q |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | try: |
---|
96 | err_i = [q, (sim_value-ana_value)/ana_value, ana_value, math.sqrt(sum)/ana_value] |
---|
97 | except: |
---|
98 | err_i = [q,0,0,0] |
---|
99 | |
---|
100 | err.append(err_i) |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | # update extrema |
---|
106 | if val_min > err_i[2]: |
---|
107 | val_min = err_i[2] |
---|
108 | |
---|
109 | if val_max < err_i[2]: |
---|
110 | val_max = err_i[2] |
---|
111 | |
---|
112 | # Find a minimum, where the error changes sign |
---|
113 | if last_val==val_min and err_i[2] > val_min: |
---|
114 | |
---|
115 | |
---|
116 | #print " min", err_i[0] |
---|
117 | if i_min > 0: |
---|
118 | i_plateau = i_last_max |
---|
119 | |
---|
120 | # check error for plateau |
---|
121 | #if math.fabs(err[i_plateau][1])>sig_val: |
---|
122 | if math.fabs(err[i_max][3])>sig_val: |
---|
123 | error_vec.append(err[i_plateau]) |
---|
124 | t_f = time.time() |
---|
125 | pred = sph.run([err[i_plateau][0],0]) |
---|
126 | print '[',t_f-t_0,']',math.pow((vol/npts),-.6666), 'n=',npts, 'q=',err[i_plateau][0], \ |
---|
127 | 'err=',math.fabs(err[i_plateau][1]), i_min, 'sig=',err[i_plateau][3] |
---|
128 | |
---|
129 | f.write("%10g %10g %10g\n" % (err[i_plateau][0], npts, err[i_plateau][3])) |
---|
130 | found_it = True |
---|
131 | break |
---|
132 | |
---|
133 | i_min = i_vec |
---|
134 | # Get ready for next maximum |
---|
135 | val_max = val_min |
---|
136 | |
---|
137 | # check if we are at max |
---|
138 | elif last_val==val_max and err_i[2]<val_max: |
---|
139 | #print " max", err[i_vec-1][0], err[i_vec-1][1] |
---|
140 | i_last_max = i_max |
---|
141 | i_max = i_vec-1 |
---|
142 | # get ready for next minimum |
---|
143 | val_min = val_max |
---|
144 | |
---|
145 | last_error = err_i[1] |
---|
146 | last_val = err_i[2] |
---|
147 | i_vec += 1 |
---|
148 | |
---|
149 | |
---|
150 | if found_it == False: |
---|
151 | print "Could not complete", npts |
---|
152 | #print "Npts = %g; %s" %(npts, str(len(error_vec))) |
---|
153 | |
---|
154 | |
---|
155 | f.close() |
---|
156 | |
---|