Controller

class Controller.Controller[source]

Bases: object

The MonteCarloController class is used to run a monte carlo simulation. It is used to execute multiple runs of a simulation with varying initial parameters. Data from each run is retained in order to analyze differences in the simulation runs and the parameters used.

addDispersion(disp)[source]

Add a dispersion to the simulation.

Parameters:

disp – Dispersion The dispersion to add to the simulation.

addRetentionPolicy(policy)[source]

Add a retention policy to the simulation.

Parameters:

disp – RetentionPolicy The retention policy to add to the simulation. This defines variables to be logged and saved

executeCallbacks(rng=None, retentionPolicies=[])[source]

Execute retention policy callbacks after running a monteCarlo sim.

Parameters:
  • rng – A list of simulations to execute callbacks on

  • retentionPolicies – the retention policies to execute

executeSimulations()[source]

Execute simulations in parallel

Returns:

failed: int[] A list of the indices of all failed simulation runs.

generateICSims(caseList)[source]

Generator function to clone a baseSimulation for IC run

Parameters:
  • baseSimulation – SimulationParams A base simulation to clone.

  • numSims – int[] The desired runs to generate.

Returns:

generator<SimulationParams>

A generator that yields that number of cloned simulations

generateSims(simNumList)[source]

Generator function to clone a baseSimulation

Parameters:
  • baseSimulation – SimulationParams A base simulation to clone.

  • numSims – int[] The desired runs to generate.

Returns:

generator<SimulationParams>

A generator that yields that number of cloned simulations

getParameters(caseNumber)[source]

Get the parameters used for a particular run of the montecarlo

Parameters:

caseNumber (int) – The number of the run to get the parameters used for.

Returns:

A dictionary of the parameters of the simulation For example: {“keyForSim”: parameterValue, ‘TaskList[0].TaskModels[0].RNGSeed’: 1674764759}

getRetainedData(case)[source]

Get the data that was retained for a run, or list of runs.

Parameters:

cases – int The desired case to get data from.

Returns:

The retained data for that run is returned.

getRetainedDatas(cases)[source]

Get the data that was retained for a list of runs.

Parameters:

cases – int[] The desired cases to get data from.

Returns:

A generator is returned, which will yield, in-order, the retained data for each of these cases

static load(runDirectory)[source]

Load a previously completed MonteCarlo simulation :param The path to the MonteCarlo.data file that contains the archived MonteCarlo run:

reRunCases(caseList)[source]

Rerun some cases from a MonteCarlo run. Does not run in parallel

Parameters:

caseList – int[] The list of runs to repeat, a list of numbers.

Returns:

int[]

The list of failed runs.

Return type:

failures

runInitialConditions(caseList)[source]

Run initial conditions given in a file

Parameters:

caseList – int[] The list of runs to repeat, a list of numbers.

Returns:

int[]

The list of failed runs.

Return type:

failures

setArchiveDir(dirName)[source]

Set-up archives for this MonteCarlo run

Parameters:

dirName – string The name of the directory to archive runs in. None, if no archive desired.

setConfigureFunction(newModule)[source]

Set an execution function that executes a simulation instance.

Parameters:

executionFunction – (sim: SimulationBaseClass) => None A function with one parameter, a simulation instance. The function will be called after the creationFunction and configurationFunction in each simulation run. It must execute the simulation. Its return value is not used.

setDispMagnitudeFile(magnitudes)[source]

Save .txt with the magnitude of each dispersion in % or sigma away from mean

Parameters:

magnitudes – bool Whether to save extra files for analysis.

setExecutionCount(newCount)[source]

Set the number of runs for the MonteCarlo simulation

Parameters:

newCount – int The number of runs to use for the simulation

setExecutionFunction(newModule)[source]

Set an execution function that executes a simulation instance.

Parameters:

executionFunction – (sim: SimulationBaseClass) => None A function with one parameter, a simulation instance. The function will be called after the creationFunction and configurationFunction in each simulation run. It must execute the simulation. Its return value is not used.

setICDir(dirName)[source]

Set-up archives containing IC data

Parameters:

dirName – string The name of the directory to archive runs in. None, if no archive desired.

setICRunFlag(bool)[source]

Set the number of threads to use for the monte carlo simulation

Parameters:

threads – int Number of threads to execute the montecarlo run on.

setShouldDisperseSeeds(seedDisp)[source]

Disperse the RNG seeds of each run in the MonteCarlo

Parameters:

seedDisp – bool Whether to disperse the RNG seeds in each run of the simulation

setShowProgressBar(value)[source]

To enable or disable progress bar to show simulation progress :param value: boolean value, decide to show/hide progress bar

setSimulationFunction(newObject)[source]

Set the function that creates the simulation instance.

Parameters:

creationFunction – () => SimulationBaseClass A function with no parameters, that returns a simulation instance.

setThreadCount(threads)[source]

Set the number of threads to use for the monte carlo simulation

Parameters:

threads – int Number of threads to execute the montecarlo run on.

setVarCast(varCast)[source]

Set the variable type to downcast the data to

Parameters:

varCast – ‘float’, ‘integer’, ‘signed’, ‘unsigned’ (see pandas.to_numeric documentation)

Returns:

setVerbose(verbose)[source]

Use verbose output for this MonteCarlo run

Parameters:

verbose – bool Whether to print verbose information during this MonteCarlo sim.

class Controller.SimulationExecutor[source]

Bases: object

This class is used to execute a simulation in a worker thread. To use, create an instance of this class, and then call the instance with the simulation parameters to run them in:

executor = SimulationExecutor()
simParams = SimulationParameters()
successFlag = executor(simParams)

This class can be used to execute a simulation on a different thread, by using this class as the processes target.

static disperseSeeds(simInstance)[source]

Disperses the RNG seeds of all the tasks in the sim, and returns a statement that contains the seeds. Example return dictionary:

{
   '.TaskList[0].TaskModels[1]': 1934586,
   '.TaskList[0].TaskModels[2]': 3450093,
   '.TaskList[1].TaskModels[0]': 2221934,
   '.TaskList[2].TaskModels[0]': 1123244
}
Parameters:

simInstance (SimulationBaseClass) – A basilisk simulation to set random seeds on

Returns:

A dictionary with the random seeds that should be applied to the sim

static populateSeeds(simInstance, modifications)[source]

only populate the RNG seeds of all the tasks in the sim

Parameters:
  • simInstance – SimulationBaseClass A basilisk simulation to set random seeds on

  • modifications – A dictionary containing RNGSeeds to be populate for the sim, among other sim modifications.

class Controller.SimulationParameters(creationFunction, executionFunction, configureFunction, retentionPolicies, dispersions, shouldDisperseSeeds, shouldArchiveParameters, filename, icfilename, index=None, verbose=False, modifications={}, showProgressBar=False)[source]

Bases: object

This class represents the run parameters for a simulation, with information including

  • a function that creates the simulation

  • a function that executes the simulation

  • the dispersions to use on that simulation

  • parameters describing the data to be retained for a simulation

  • whether randomized seeds should be applied to the simulation

  • whether data should be archived