2 Created on Tue Jul 12 09:28:23 2016
12 import carmaWrap
as ch
15 import matplotlib.pyplot
as plt
17 import hdf5_util
as h5u
19 from scipy.sparse
import csr_matrix
28 c = ch.carmaWrap_context(devices=np.array([6, 7], dtype=np.int32))
32 if (hasattr(config,
"simul_name")):
33 if (config.simul_name
is None):
36 simul_name = config.simul_name
39 print(
"simul name is", simul_name)
42 if (simul_name == b
""):
46 param_dict = h5u.params_dictionary(config)
47 matricesToLoad = h5u.checkMatricesDataBase(os.environ[
"SHESHA_ROOT"] +
"/data/",
52 c = ch.carmaWrap_context(devices=np.array([6, 7], dtype=np.int32))
57 wfs, tel = ao.wfs_init(config.p_wfss, config.p_atmos, config.p_tel, config.p_geom,
58 config.p_target, config.p_loop, config.p_dms)
62 atm = ao.atmos_init(c, config.p_atmos, config.p_tel, config.p_geom, config.p_loop,
63 config.p_wfss, wfs, config.p_target, rank=0, clean=clean,
68 dms = ao.dm_init(config.p_dms, config.p_wfss, wfs, config.p_geom, config.p_tel)
72 tar = ao.target_init(c, tel, config.p_target, config.p_atmos, config.p_geom,
73 config.p_tel, config.p_dms)
77 rtc = ao.rtc_init(tel, wfs, config.p_wfss, dms, config.p_dms, config.p_geom,
78 config.p_rtc, config.p_atmos, atm, config.p_tel, config.p_loop,
79 clean=clean, simul_name=simul_name, load=matricesToLoad)
82 h5u.validDataBase(os.environ[
"SHESHA_ROOT"] +
"/data/", matricesToLoad)
84 print(
"====================")
86 print(
"====================")
87 print(
"objects initialzed on GPU:")
88 print(
"--------------------------------------------------------")
95 print(
"----------------------------------------------------")
96 print(
"iter# | SE SR image | LE SR image | Fitting | LE SR phase var")
97 print(
"----------------------------------------------------")
99 error_flag =
True in [w.roket
for w
in config.p_wfss]
101 return atm, wfs, tel, dms, tar, rtc
113 Performs the main AO loop for n interations. First, initialize buffers
114 for error breakdown computations. Then, at the end of each iteration, just
115 before applying the new DM shape, calls the error_breakdown function.
117 :param n: (int) : number of iterations
120 com : (np.array((n,nactus))) : full command buffer
122 noise_com : (np.array((n,nactus))) : noise contribution for error breakdown
124 alias_wfs_com : (np.array((n,nactus))) : aliasing estimation in the WFS direction
126 tomo_com : (np.array((n,nactus))) : tomography error estimation
128 H_com : (np.array((n,nactus))) : Filtered modes contribution for error breakdown
130 trunc_com : (np.array((n,nactus))) : Truncature and sampling error of WFS
132 bp_com : (np.array((n,nactus))) : Bandwidth error estimation on target
134 mod_com : (np.array((n,nactus))) : Commanded modes expressed on the actuators
136 fit : (float) : fitting (mean variance of the residual target phase after projection)
138 SR : (float) : final strehl ratio returned by the simulation
142 nactu = rtc.get_command(0).size
143 nslopes = rtc.get_centroids(0).size
144 com = np.zeros((n, nactu), dtype=np.float32)
145 noise_com = np.zeros((n, nactu), dtype=np.float32)
146 alias_wfs_com = np.copy(noise_com)
147 wf_com = np.copy(noise_com)
148 tomo_com = np.copy(noise_com)
149 trunc_com = np.copy(noise_com)
150 H_com = np.copy(noise_com)
151 mod_com = np.copy(noise_com)
152 bp_com = np.copy(noise_com)
158 for i
in range(-10, n):
161 if (config.p_controllers[0].type == b
"geo"):
162 for t
in range(config.p_target.ntargets):
163 tar.atmos_trace(t, atm, tel)
164 rtc.docontrol_geo(0, dms, tar, 0)
165 rtc.applycontrol(0, dms)
168 for t
in range(config.p_target.ntargets):
169 tar.atmos_trace(t, atm, tel)
171 for w
in range(len(config.p_wfss)):
172 wfs.sensors_trace(w,
"all", tel, atm, dms)
173 wfs.sensors_compimg(w)
178 if (error_flag
and i > -1):
182 roket.computeBreakdown()
183 rtc.applycontrol(0, dms)
185 if ((i + 1) % 100 == 0
and i > -1):
186 strehltmp = tar.get_strehl(0)
187 print(i + 1,
"\t", strehltmp[0],
"\t", strehltmp[1],
"\t",
188 np.exp(-strehltmp[2]),
"\t", np.exp(-strehltmp[3]))
190 print(
" loop execution time:", t1 - t0,
" (", n,
"iterations), ", (t1 - t0) / n,
191 "(mean) ", n / (t1 - t0),
"Hz")
194 SR2 = np.exp(-tar.get_strehl(0, comp_strehl=
False)[3])
195 SR = tar.get_strehl(0, comp_strehl=
False)[1]
203 Performs the main AO loop for n interations. First, initialize buffers
204 for error breakdown computations. Then, at the end of each iteration, just
205 before applying the new DM shape, calls the error_breakdown function.
207 :param n: (int) : number of iterations
210 com : (np.array((n,nactus))) : full command buffer
212 noise_com : (np.array((n,nactus))) : noise contribution for error breakdown
214 alias_wfs_com : (np.array((n,nactus))) : aliasing estimation in the WFS direction
216 tomo_com : (np.array((n,nactus))) : tomography error estimation
218 H_com : (np.array((n,nactus))) : Filtered modes contribution for error breakdown
220 trunc_com : (np.array((n,nactus))) : Truncature and sampling error of WFS
222 bp_com : (np.array((n,nactus))) : Bandwidth error estimation on target
224 mod_com : (np.array((n,nactus))) : Commanded modes expressed on the actuators
226 fit : (float) : fitting (mean variance of the residual target phase after projection)
228 SR : (float) : final strehl ratio returned by the simulation
230 for i
in range(0, n):
233 if (config.p_controllers[0].type == b
"geo"):
234 for t
in range(config.p_target.ntargets):
235 tar.atmos_trace(t, atm, tel)
236 rtc.docontrol_geo(0, dms, tar, 0)
237 rtc.applycontrol(0, dms)
239 for t
in range(config.p_target.ntargets):
240 tar.atmos_trace(t, atm, tel)
241 for w
in range(len(config.p_wfss)):
242 wfs.sensors_trace(w,
"all", tel, atm, dms)
243 wfs.sensors_compimg(w)
247 rtc.applycontrol(0, dms)
257 IF = rtc.get_IFsparse(1).T
265 delta = IF.T.dot(IF).toarray() / N
268 Tp = np.ones((T.shape[0], T.shape[1] + 1))
270 deltaT = IF.T.dot(Tp) / N
272 tau = np.linalg.inv(delta).dot(deltaT)
276 tdt = tau.T.dot(delta).dot(tau)
277 subTT = tau.dot(np.linalg.inv(tdt)).dot(tau.T).dot(delta)
281 gdg = G.T.dot(delta).dot(G)
282 U, s, V = np.linalg.svd(gdg)
283 U = U[:, :U.shape[1] - 3]
285 L = np.identity(s.size) / np.sqrt(s)
290 Btt = np.zeros((n + 2, n - 1))
291 Btt[:B.shape[0], :B.shape[1]] = B
292 mini = 1. / np.sqrt(TT)
295 Btt[n:, n - 3:] = mini
298 delta = np.zeros((n + T.shape[1], n + T.shape[1]))
300 delta[:-2, :-2] = IF.T.dot(IF).toarray() / N
301 delta[-2:, -2:] = T.T.dot(T) / N
304 return Btt.astype(np.float32), P.astype(np.float32)
311 Btt_filt = np.zeros((Btt.shape[0], Btt.shape[1] - nfilt))
312 Btt_filt[:, :Btt_filt.shape[1] - 2] = Btt[:, :Btt.shape[1] - (nfilt + 2)]
313 Btt_filt[:, Btt_filt.shape[1] - 2:] = Btt[:, Btt.shape[1] - 2:]
318 Dmp = np.linalg.inv(Dm.T.dot(Dm)).dot(Dm.T)
320 cmat = Btt_filt.dot(Dmp)
322 return Dm.astype(np.float32), cmat.astype(np.float32)
332 U, s, V = np.linalg.svd(DmtDm)
334 s[s.shape[0] - nfilt - 2:s.shape[0] - 2] = 0.
335 DmtDm1 = U.dot(np.diag(s)).dot(U.T)
336 Dmp = DmtDm1.dot(Dm.T)
340 return Dm.astype(np.float32), cmat.astype(np.float32)
352 def cov_cor(P, noise, trunc, alias, H, bp, tomo):
353 cov = np.zeros((6, 6))
362 for i
in range(cov.shape[0]):
363 for j
in range(cov.shape[1]):
365 tmpi = P.dot(bufdict[str(i)])
366 tmpj = P.dot(bufdict[str(j)])
368 np.mean(tmpi * tmpj, axis=1) -
369 np.mean(tmpi, axis=1) * np.mean(tmpj, axis=1))
371 cov[i, j] = cov[j, i]
373 s = np.reshape(np.diag(cov), (cov.shape[0], 1))
375 cor = cov / np.sqrt(sst)
389 IF = rtc.get_IFsparse(1)
391 noise_com = roket.getContributor(
"noise")
392 trunc_com = roket.getContributor(
"nonlinear")
393 alias_wfs_com = roket.getContributor(
"aliasing")
394 H_com = roket.getContributor(
"filtered")
395 bp_com = roket.getContributor(
"bandwidth")
396 tomo_com = roket.getContributor(
"tomo")
397 fit = roket.getContributor(
"fitting")
399 tmp = (config.p_geom._ipupil.shape[0] -
400 (config.p_dms[0]._n2 - config.p_dms[0]._n1 + 1)) / 2
401 tmp_e0 = config.p_geom._ipupil.shape[0] - tmp
402 tmp_e1 = config.p_geom._ipupil.shape[1] - tmp
403 pup = config.p_geom._ipupil[tmp:tmp_e0, tmp:tmp_e1]
404 indx_pup = np.where(pup.flatten() > 0)[0].astype(np.int32)
405 dm_dim = config.p_dms[0]._n2 - config.p_dms[0]._n1 + 1
406 cov, cor =
cov_cor(P, noise_com, trunc_com, alias_wfs_com, H_com, bp_com, tomo_com)
407 psf = tar.get_image(0,
"le", fluxNorm=
False)
408 psfortho = roket.get_tar_imageortho()
409 covv = roket.get_covv()
410 covm = roket.get_covm()
412 fname =
"/home/fferreira/Data/" + filename
414 "noise": noise_com.T,
415 "aliasing": alias_wfs_com.T,
416 "tomography": tomo_com.T,
417 "filtered modes": H_com.T,
418 "non linearity": trunc_com.T,
419 "bandwidth": bp_com.T,
423 "IF.indices": IF.indices,
424 "IF.indptr": IF.indptr,
427 "indx_pup": indx_pup,
433 "psfortho": psfortho,
437 h5u.save_h5(fname,
"psf", config, psf)
439 for k
in list(pdict.keys()):
440 h5u.save_hdf5(fname, k, pdict[k])
450 param_file =
"/home/fferreira/compass/trunk/shesha/data/par/par4roket/correlation_study/roket_8m_1layer.py"
452 if (param_file.split(
'.')[-1] == b
"py"):
453 filename = param_file.split(
'/')[-1]
454 param_path = param_file.split(filename)[0]
455 sys.path.insert(0, param_path)
456 exec(
"import %s as config" % filename.split(
".py")[0])
459 niters = config.p_loop.niter
461 winddirs = [0, 45, 90, 135, 180]
462 windspeeds = [5., 10., 15., 20.]
464 d = float(sys.argv[1])
465 s = float(sys.argv[2])
466 g = float(sys.argv[3])
468 savename =
"roket_8m_1layer_dir%d_speed%d_g%d.h5" % (d, s, g * 10)
469 config.p_atmos.set_winddir([d])
470 config.p_atmos.set_windspeed([s])
471 config.p_controllers[0].set_gain(g)
476 rtc.load_Btt(1, Btt.dot(Btt.T))
478 rtc.set_cmat(0, cmat)
480 imat = rtc.get_imat(0)
481 RD = np.dot(R, imat).astype(np.float32)
482 gRD = (np.identity(RD.shape[0]) - config.p_controllers[0].gain * RD).astype(np.float32)
483 roket = ao.roket_init(rtc, wfs, tar, dms, tel, atm, 0, 1, Btt.shape[0], Btt.shape[1],
484 nfiltered, niters, Btt, P, gRD, RD)