Simulation

High level interface to run and examine a simulation

The main Soapy Simulation module

This module contains the Sim class, which can be used to run an end-to-end simulation. Initally, a configuration file is read, the system is initialised, interaction and command matrices calculated and finally a loop run. The simulation outputs some information to the console during the simulation.

The Sim class holds all configuration information and data from the simulation.

Examples

To initialise the class:

import soapy
sim = soapy.Sim("sh_8x8_4.2m.py")

Configuration information has now been loaded, and can be accessed through the config attribute of the sim class. In fact, each sub-module of the system has a configuration object accessed through this config attribute:

print(sim.config.sim.pupilSize)
sim.config.wfss[0].pxlsPerSubap = 10

Next, the system is initialised, this entails calculating various parameters in the system sub-modules, so must be done after changing some simulation parameters:

sim.aoinit()

DM Interation and command matrices are calculated now. If sim.config.sim.simName is not None, then these matrices will be saved in data/simName (data will be saved here also in a time-stamped directory):

sim.makeIMat()

Finally, the loop is run with the command:

sim.aoloop()

Some output will be printed to the console. After the loop has finished, data specified to be saved in the config file will be saved to data/simName (if it is not set to None). Data can also be accessed from the simulation class, e.g. sim.allSlopes, sim.longStrehl

Author:Andrew Reeves
class soapy.simulation.DelayBuffer[source]

Bases: list

A delay buffer.

Each time delay() is called on the buffer, the input value is stored. If the buffer is larger than count, the oldest value is removed and returned. If the buffer is not yet full, a zero of similar shape as the last input is returned.

delay(value, count)[source]
class soapy.simulation.Sim(configFile=None)[source]

Bases: object

The soapy Simulation class.

This class holds all configuration information, data and control methods of the simulation. It contains high level methods dealing with initialising all component objects, making reconstructor control matrices, running the loop and saving data after the loop has run.

Can be sub-classed and the ‘aoloop’ method overwritten for different loops to be used

Parameters:configFile (string) – The filename of the AO configuration file
addToGuiQueue()[source]

Adds data to a Queue object provided by the soapy GUI.

The soapy GUI doesn’t need to plot every frame from the simulation. When it wants a frame, it will request if by setting waitingPlot = True. As this function is called on every iteration, data is passed to the GUI only if waitingPlot = True. This allows efficient and abstracted interaction between the GUI and the simulation

aoinit()[source]

Initialises all simulation objects.

Initialises and passes relevant data to sim objects. This does important pre-run tasks, such as creating or loading phase screens, determining WFS geometry, setting propagation modes and pre-allocating data arrays used later in the simulation.

aoloop()[source]

Main AO Loop

Runs a WFS iteration, reconstructs the phase, runs DMs and finally the science cameras. Also makes some nice output to the console and can add data to the Queue for the GUI if it has been requested. Repeats for nIters.

finishUp()[source]

Prints a message to the console giving timing data. Used on sim end.

getTimeStamp()[source]

Returns a formatted timestamp

Returns:nicely formatted timestamp of current time.
Return type:string
initSaveData()[source]

Initialise data structures used for data saving.

Initialise the data structures which will be used to store data which will be saved or analysed once the simulation has ended. If the simName = None, no data is saved, other wise a directory called simName is created, and data from simulation runs are saved in a time-stamped directory inside this.

loopFrame()[source]

Runs a single from of the entire AO system.

Moves the atmosphere, runs the WFSs, finds the corrective DM shape and finally runs the science cameras. This can be called over and over to form the “loop”

makeIMat(forceNew=False, progressCallback=None)[source]

Creates interaction and control matrices for simulation reconstruction

Makes and inverts Interaction matrices for each DM in turn to create a DM control Matrix for each DM. Each DM’s control Matrix is independent of the others, so care must be taken so DM correction modes do not “overlap”. Some reconstruction modes may require WFS frames to be taken for the creation of a control matrix. Depending on set parameters, can load previous control and interaction matrices.

Parameters:
  • forceNew (bool) – if true, will force making of new iMats and cMats, otherwise will attempt to load previously made matrices from same simName
  • progressCallback (func) – function called to report progress of interaction matrix construction
makeSaveHeader()[source]

Forms a header which can be used to give a header to FITS files saved by the simulation.

printOutput(iter, strehl=False)[source]

Prints simulation information to the console

Called on each iteration to print information about the current simulation, such as current strehl ratio, to the console. Still under development :param label: Simulation Name :type label: str :param iter: simulation frame number :type iter: int :param strehl: current strehl ration if science cameras are present to record it. :type strehl: float, optional

readParams(configFile=None)[source]

Reads configuration file parameters

Calls the radParams function in confParse to read, parse and if required set reasonable defaults to AO parameters

reset_loop()[source]

Resets parameters in the system to zero, to restart an AO run wihtout reinitialising

runDM(dmCommands, closed=True)[source]

Runs a single frame of the deformable mirrors

Calculates the total combined shape of all deformable mirrors (DMs), given an array of DM commands. DM commands correspond to shapes generated during the making of interaction matrices, the final DM shape for each DM is a combination of these. The DM commands will have already been calculated by the systems reconstructor.

Parameters:
  • dmCommands (ndarray) – an array of dm commands corresponding to dm shapes
  • closed (bool) – if True, indicates to DM that slopes are residual errors from previous frame, if False, slopes correspond to total phase error over pupil.
Returns:

the combined DM shape

Return type:

ndArray

runSciCams(dmShape=None)[source]

Runs a single frame of the science Cameras

Calculates the image recorded by all science cameras in the system for the current phase over the telescope one frame. If a dmShape is present (which it usually will be in AO!) this correction is applied to the science phase before the image is calculated.

Parameters:correction (list or ndarray, optional) – An array of the combined system DM shape to correct the science path. If not given science cameras are in open loop.
runWfs_MP(scrns=None, dmShape=None, wfsList=None, loopIter=None)[source]

Runs all WFSs using multiprocessing

Runs a single frame for each WFS in wfsList, passing the given phase screens and optional dmShape (if WFS in closed loop). If LGSs are present it will also deals with LGS propagation. Finally, the slopes from all WFSs are returned. Each WFS is allocated a separate process to complete the frame, giving a significant increase in speed, especially for computationally heavy WFSs.

Parameters:
  • scrns (list) – List of phase screens passing over telescope
  • dmShape (ndarray, optional) – 2-dimensional array of the total corrector shape
  • wfsList (list, optional) – A list of the WFSs to be run, if not set, runs all WFSs
  • loopIter (int, optional) – The loop iteration number
Returns:

The slope data return from the WFS frame (may not be actual slopes if WFS other than SH used)

Return type:

ndarray

runWfs_noMP(scrns=None, dmShape=None, wfsList=None, loopIter=None)[source]

Runs all WFSs

Runs a single frame for each WFS in wfsList, passing the given phase screens and optional dmShape (if WFS in closed loop). The WFSs are only read out if the wfs frame time co-incides with the WFS frame rate, else old slopes are provided. If iter is not given, then all WFSs are run and read out. If LGSs are present it will also deals with LGS propagation. Finally, the slopes from all WFSs are returned.

Parameters:
  • scrns (list) – List of phase screens passing over telescope
  • dmShape (ndarray, optional) – 2-dim array of the total corrector shape
  • wfsList (list, optional) – A list of the WFSs to be run
  • loopIter (int, optional) – The loop iteration number
Returns:

The slope data return from the WFS frame (may not be actual slopes if WFS other than SH used)

Return type:

ndarray

saveData()[source]

Saves all recorded data to disk

Called once simulation has ended to save the data recorded during the simulation to disk in the directories created during initialisation.

setLoggingLevel(level)[source]

sets which messages are printed from logger.

if logging level is set to 0, nothing is printed. if set to 1, only warnings are printed. if set to 2, warnings and info is printed. if set to 3 detailed debugging info is printed.

Parameters:level (int) – the desired logging level
storeData(i)[source]

Stores data from each frame in an appropriate data structure.

Called on each frame to store the simulation data into various data structures corresponding to different data sources in the system.

Parameters:i (int) – The system iteration number
soapy.simulation.make_mask(config)[source]

Generates a Soapy pupil mask

Parameters:config (SoapyConfig) – Config object describing Soapy simulation
Returns:2-d pupil mask
Return type:ndarray
soapy.simulation.multiWfs(scrns, wfsObj, dmShape, read, queue)[source]

Function to run the WFS in multiprocessing mode.

Function is called by each of the new WFS processes spawned to run each WFS. Does the same job as the sim runWfs_noMP method of running LGS, then getting slopes from each WFS.

Parameters:
  • scrns (list) – list of the phase screens over the WFS
  • wfsObj (WFS object) – the WFS object being run
  • dmShape (ndArray) – shape of system DMs for WFS phase correction
  • queue (Queue object) – a multiprocessing Queue object used to pass data back to host process.