COMPASS  5.4.4
End-to-end AO simulation tool using GPU acceleration
atmosCompass.py
1 
37 from shesha.init.atmos_init import atmos_init
38 from shesha.constants import CONST
39 import numpy as np
40 
41 class AtmosCompass(object):
42  """ Atmosphere handler for compass simulation
43 
44  Attributes:
45  is_enable : (bool) : Flag to enable/disable atmophere
46 
47  _atmos : (sutraWrap.Atmos) : Sutra atmos instance
48 
49  _context : (carmaContext) : CarmaContext instance
50 
51  _config : (config module) : Parameters configuration structure module
52  """
53  def __init__(self, context, config):
54  """ Initialize an AtmosCompass component for atmosphere related supervision
55 
56  Args:
57  context : (carmaContext) : CarmaContext instance
58 
59  config : (config module) : Parameters configuration structure module
60  """
61  self.is_enableis_enable = True # Flag to enable/disable atmophere
62  self._context_context = context
63  self._config_config = config # Parameters configuration coming from supervisor init
64  print("->atmosphere init")
65  self._atmos_atmos = atmos_init(self._context_context, self._config_config.p_atmos, self._config_config.p_tel,
66  self._config_config.p_geom, self._config_config.p_loop.ittime, p_wfss=self._config_config.p_wfss,
67  p_targets=self._config_config.p_targets)
68 
69 
70  def enable_atmos(self, enable : bool) -> None:
71  """ Set or unset whether atmos is enabled when running loop
72 
73  Args:
74  enable : (bool) : True to enable, False to disable
75  """
76  self.is_enableis_enable = enable
77 
78  def set_r0(self, r0 : float, *, reset_seed : int=-1) -> None:
79  """ Change the current r0 for all layers
80 
81  Args:
82  r0 : (float) : r0 @ 0.5 µm
83 
84  Kwargs:
85  reset_seed : (int): if -1 (default), keep same seed and same screen
86  if 0 random seed is applied and refresh screens
87  if (value) set the given seed and refresh screens
88  """
89  self._atmos_atmos.set_r0(r0)
90  if reset_seed != -1:
91  if reset_seed == 0:
92  ilayer = np.random.randint(1e4)
93  else:
94  ilayer = reset_seed
95  for k in range(self._atmos_atmos.nscreens):
96  self._atmos_atmos.set_seed(k, self._config_config.p_atmos.seeds[ilayer])
97  self._atmos_atmos.refresh_screen(k)
98  ilayer += 1
99  self._config_config.p_atmos.set_r0(r0)
100 
101  def set_wind(self, screen_index : int, *, windspeed : float = None, winddir : float = None) -> None:
102  """ Set new wind information for the given screen
103 
104  Args:
105  screen_index : (int) : Atmos screen to change
106 
107  Kwargs:
108  windspeed : (float) [m/s] : new wind speed of the screen. If None, the wind speed is unchanged
109 
110  winddir : (float) [deg]: new wind direction of the screen. If None, the wind direction is unchanged
111  """
112  if windspeed is not None:
113  self._config_config.p_atmos.windspeed[screen_index] = windspeed
114  if winddir is not None:
115  self._config_config.p_atmos.winddir[screen_index] = winddir
116 
117  lin_delta = self._config_config.p_geom.pupdiam / self._config_config.p_tel.diam * self._config_config.p_atmos.windspeed[screen_index] * \
118  np.cos(CONST.DEG2RAD * self._config_config.p_geom.zenithangle) * self._config_config.p_loop.ittime
119  oldx = self._config_config.p_atmos._deltax[screen_index]
120  oldy = self._config_config.p_atmos._deltay[screen_index]
121  self._config_config.p_atmos._deltax[screen_index] = lin_delta * np.sin(CONST.DEG2RAD * self._config_config.p_atmos.winddir[screen_index] + np.pi)
122  self._config_config.p_atmos._deltay[screen_index] = lin_delta * np.cos(CONST.DEG2RAD * self._config_config.p_atmos.winddir[screen_index] + np.pi)
123  self._atmos_atmos.d_screens[screen_index].set_deltax(self._config_config.p_atmos._deltax[screen_index])
124  self._atmos_atmos.d_screens[screen_index].set_deltay(self._config_config.p_atmos._deltay[screen_index])
125  if(oldx * self._config_config.p_atmos._deltax[screen_index] < 0): #Sign has changed, must change the stencil
126  stencilx = np.array(self._atmos_atmos.d_screens[screen_index].d_istencilx)
127  n = self._config_config.p_atmos.dim_screens[screen_index]
128  stencilx = (n * n - 1) - stencilx
129  self._atmos_atmos.d_screens[screen_index].set_istencilx(stencilx)
130  if(oldy * self._config_config.p_atmos._deltay[screen_index] < 0): #Sign has changed, must change the stencil
131  stencily = np.array(self._atmos_atmos.d_screens[screen_index].d_istencily)
132  n = self._config_config.p_atmos.dim_screens[screen_index]
133  stencily = (n * n - 1) - stencily
134  self._atmos_atmos.d_screens[screen_index].set_istencily(stencily)
135 
136  def reset_turbu(self) -> None:
137  """ Reset the turbulence layers to their original state
138  """
139  ilayer = 0
140  for k in range(self._atmos_atmos.nscreens):
141  self._atmos_atmos.set_seed(k, self._config_config.p_atmos.seeds[ilayer])
142  self._atmos_atmos.refresh_screen(k)
143  ilayer += 1
144 
145  def get_atmos_layer(self, indx: int) -> np.ndarray:
146  """ Return the selected atmos screen
147 
148  Args:
149  indx : (int) : Index of the turbulent layer to return
150 
151  Returns:
152  layer : (np.ndarray) : turbulent layer phase screen
153  """
154  return np.array(self._atmos_atmos.d_screens[indx].d_screen)
155 
156  def move_atmos(self) -> None:
157  """ Move the turbulent layers according to wind speed and direction for a single iteration
158  """
159  self._atmos_atmos.move_atmos()
Atmosphere handler for compass simulation.
Definition: atmosCompass.py:45
None reset_turbu(self)
Reset the turbulence layers to their original state.
None move_atmos(self)
Move the turbulent layers according to wind speed and direction for a single iteration.
None set_r0(self, float r0, *int reset_seed=-1)
Change the current r0 for all layers.
Definition: atmosCompass.py:99
None enable_atmos(self, bool enable)
Set or unset whether atmos is enabled when running loop.
Definition: atmosCompass.py:86
None set_wind(self, int screen_index, *float windspeed=None, float winddir=None)
Set new wind information for the given screen.
np.ndarray get_atmos_layer(self, int indx)
Return the selected atmos screen.
is_enable
(bool) : Flag to enable/disable atmophere
Definition: atmosCompass.py:72
Numerical constants for shesha and config enumerations for safe-typing.
Definition: constants.py:1
Initialization of a Atmos object.
Definition: atmos_init.py:1