def test_lazy_sim_run_to_std_sim(self):
     # Make the run sim's filepath created
     copyfile(self.result_filepath, self.sim_run.cadet_filepath)
     lazy_sim = LazyLoadingSimulation.from_simulation(self.sim_run)
     new_std_sim = lazy_sim.to_simulation()
     assert_has_traits_almost_equal(new_std_sim,
                                    self.sim_run,
                                    ignore=('uuid', ))
 def test_paste_lazy_sim_into_simulation_list(self):
     sim_list = self.study.simulations
     existing = len(sim_list)
     sim = self.study.simulations[0].copy()
     sim.name = "NEW NAME TO AVOID COLLISION"
     sim = LazyLoadingSimulation.from_simulation(sim)
     clipboard.instance = sim
     self.right_click_on_list_and_action(self.study, "simulations", "Paste")
     self.assertEqual(len(sim_list), existing + 1)
     # Not the same object because the sim was converted to regular sim so
     # that it doesn't cause issues when pasting
     self.assertIsNot(sim_list[-1], sim)
     assert_has_traits_almost_equal(sim_list[-1], sim, check_type=False)
    def test_std_sim_run_to_lazy_sim(self):
        """ Creating a Lazy sim from a run sim, with or without CADET file.
        """
        lazy_sim = LazyLoadingSimulation.from_simulation(self.sim_run)
        # With missing cadet file, the lazy copy isn't run:
        ignore = ('uuid', 'has_run', 'run_status', 'output', 'editable')
        assert_has_traits_almost_equal(lazy_sim,
                                       self.sim_run,
                                       check_type=False,
                                       ignore=ignore)
        self.assertFalse(lazy_sim.has_run)
        self.assertTrue(lazy_sim.editable)

        # With cadet file, the lazy sim is run:
        copyfile(self.result_filepath, self.sim_run.cadet_filepath)
        try:
            lazy_sim2 = LazyLoadingSimulation.from_simulation(self.sim_run)
            assert_has_traits_almost_equal(lazy_sim2,
                                           self.sim_run,
                                           check_type=False,
                                           ignore=('uuid', ))
        finally:
            os.remove(self.sim_run.cadet_filepath)
 def test_lazy_sim_not_run_to_lazy_sim(self):
     # Make the run sim's filepath created
     copyfile(self.result_filepath, self.sim_run.cadet_filepath)
     lazy_sim = LazyLoadingSimulation.from_simulation(self.sim)
     lazy_sim2 = LazyLoadingSimulation.from_simulation(lazy_sim)
     assert_has_traits_almost_equal(lazy_sim, lazy_sim2, ignore=('uuid', ))
 def test_lazy_sim_not_run_to_std_sim(self):
     lazy_sim = LazyLoadingSimulation.from_simulation(self.sim)
     new_std_sim = lazy_sim.to_simulation()
     assert_has_traits_almost_equal(new_std_sim,
                                    self.sim,
                                    ignore=('uuid', ))
 def test_std_sim_not_run_to_lazy_sim(self):
     lazy_sim = LazyLoadingSimulation.from_simulation(self.sim)
     assert_has_traits_almost_equal(lazy_sim,
                                    self.sim,
                                    check_type=False,
                                    ignore=('uuid', ))
 def setUp(self):
     super(TestLazySimulationCopy, self).setUp()
     self.sim_from_scratch = LazyLoadingSimulation.from_simulation(
         self.sim_from_scratch)
     self.sim_from_std_exp = LazyLoadingSimulation.from_simulation(
         self.sim_from_std_exp)
 def setUpClass(cls):
     super(TestLazySimulationCopy, cls).setUpClass()
     cls.real_sim = LazyLoadingSimulation.from_simulation(cls.real_sim)
 def setUp(self):
     super(TestLazySimulationCreation, self).setUp()
     self.sim_from_scratch = LazyLoadingSimulation.from_simulation(
         self.sim_from_scratch)
Ejemplo n.º 10
0
def build_simulation_from_experiment(experiment, binding_model=None,
                                     transport_model=None, name="",
                                     fstep='Load', lstep='Strip',
                                     initial_buffer=None, lazy_loading=False,
                                     **traits):
    """ Build a Simulation object given an experiment and some models.

    Now only used for testing purposes.

    Parameters
    ----------
    experiment : Experiment
        Source experiment to build the simulation from.

    binding_model : BindingModel [OPTIONAL]
        Binding model to build the simulation from. If not provided, a default
        model with default value will be generated.

    transport_model : TransportModel [OPTIONAL]
        Transport model to build the simulation from. If not provided, a
        default model with default value will be generated.

    name : str [OPTIONAL]
        Name of the future simulation

    fstep : str [OPTIONAL, default='Load']
        Name of the first step to simulate. Defaults to 'Load'.

    lstep : str [OPTIONAL, default='Strip']
        Name of the last step to simulate. Defaults to 'Strip'.

    initial_buffer : Buffer [OPTIONAL]
        Buffer instance to use as initial buffer for the simulation method. If
        not provided, the buffer in the experimental step before the first
        simulated step will be used.

    lazy_loading : bool [OPTIONAL, default=False)
        Should the resulting simulation be of LazyLoadingSimulation type?
    """

    if not name:
        name = generate_sim_name(experiment.name)

    # NOTE: The number of components is : product components + cation
    # The first component is always the cation, followed by the product
    # components.
    num_components = len(experiment.product.product_component_names) + 1

    if binding_model is None:
        binding_model = create_binding_model(num_components)

    if transport_model is None:
        transport_model = create_transport_model(num_components)

    # The solver, discretization and other simulation params are set to
    # defaults (configured by CADET simulation builder)
    sim_method = build_sim_method_from_method(
        experiment.method, first_simulated_step=fstep,
        last_simulated_step=lstep, initial_buffer=initial_buffer,
    )

    simulation = Simulation(
        name=name,
        column=experiment.column.clone_traits(copy="deep"),
        transport_model=transport_model,
        binding_model=binding_model,
        source_experiment=experiment,
        method=sim_method,
        **traits
    )
    if lazy_loading:
        simulation = LazyLoadingSimulation.from_simulation(simulation)

    return simulation
Ejemplo n.º 11
0
def param_scans_to_sim_group(group_name,
                             parameter_scans,
                             center_sim,
                             lazy_loading=False,
                             max_size=None,
                             group_type=SIM_GROUP_GRID_TYPE,
                             group_size=-1,
                             auto_delete_run_sims=False):
    """ Returns simulation grid from a list of regular parameter scans.

    Parameters
    ----------
    group_name : str
        Target group name.

    parameter_scans : list(ParameterScanDescription)
        List of parameter scan descriptions.

    center_sim : Simulation
        Center point simulation from which to create all simulations in the
        future group.

    group_type : str [OPTIONAL]
        Type of the future group(s) to be created. Should be one of the values
        allowed for the :attr:`simulation_group.SimulationGroup.type`
        attribute. Set to a regular grid by default.

    lazy_loading : bool (default: False)
        Save memory by making the output group of the lazy loading kind.

    max_size : None or int [OPTIONAL]
        Max size of the group(s) created. If a non-zero value is specified, a
        list of simulation group is returned, each with a number of simulation
        diffs smaller or equal to max_size.

    group_size : int [OPTIONAL]
        Size of the resulting group, used only if of Monte-Carlo type. Size is
        computed from parameter scan descriptions for the grid type.

    auto_delete_run_sims : bool [OPTIONAL, default=False]
        Delete CADET files and in memory simulations once they run?

    Returns
    -------
    SimulationGroup or list(SimulationGroup)
        SimulationGroup created from the list of parameter scans the group
        should explore. If a max_size is provided, returns a list of
        SimulationGroup instances collectively scanning the entire parameter
        space described by parameter_scans, each with  0 < size <= max_size.
    """
    # Override the number of threads per run from preferences in case it has
    # changed. Do it on the center sim to avoid having to do it on each
    # simulation of the group:
    prefs = get_preferences()
    target_nthreads = prefs.solver_preferences.cadet_num_threads

    if lazy_loading and not isinstance(center_sim, LazyLoadingSimulation):
        center_sim = LazyLoadingSimulation.from_simulation(center_sim)
        center_sim.solver.nthreads = target_nthreads
    else:
        # if the nthreads will be overridden, make a copy to avoid modifying
        # the source simulation
        if center_sim.solver.nthreads != target_nthreads:
            center_sim = center_sim.copy()
            center_sim.solver.nthreads = target_nthreads

    if group_type == SIM_GROUP_GRID_TYPE:
        simulation_diffs = sim_diffs_from_grid_parameter_scans(parameter_scans)
    elif group_type == SIM_GROUP_MC_TYPE:
        simulation_diffs = sim_diffs_from_random_parameter_scans(
            parameter_scans, group_size)
    else:
        msg = "Unsupported group type: {}".format(group_type)
        logger.exception(msg)
        raise ValueError(msg)

    return build_group_or_groups(group_name,
                                 center_sim,
                                 simulation_diffs,
                                 max_size,
                                 group_type=group_type,
                                 auto_delete_run_sims=auto_delete_run_sims)
Ejemplo n.º 12
0
def build_random_simulation_group(center_sim,
                                  param_names,
                                  group_size=200,
                                  dist_types="uniform",
                                  dist_desc=None,
                                  lazy_loading=False,
                                  max_block_size=None,
                                  group_name=DEFAULT_MC_GROUP,
                                  adtl_params=None):
    """ Build a simulation group around a center point scanning a list of
    parameters randomly. Useful for scripting tools.

    Parameters
    ----------
    center_sim : Simulation
        Simulation around which to build the grid.

    param_names : List
        List of parameters to scan in the grid.

    group_size : int [OPTIONAL, default=200]
        Number of simulations to generate to explore the space.

    dist_types : str or list of str [OPTIONAL, default="uniform"]
        Type(s) of sampling distributions for each parameter scanned. Supported
        values are "uniform", "normal".

    dist_desc : str or list of str
        Parameters of the sampling distributions for each parameter scanned, in
        the order of the constructor of the chosen distribution. That is the
        low (inclusive) and high (EXclusive) if "uniform" is chosen, and mean
        and standard deviation if "normal" is chosen.

    lazy_loading : bool
        Whether to force the center simulation to be a lazy loading simulation,
        to conserve RAM.

    max_block_size : int
        Maximum group size for the resulting group. If the needed group is
        larger, a list of groups will be created and returned.

    group_name : str
        Name of the resulting group.

    adtl_params : dict
        Map between parameters and adjustment parameters that must be changed
        at the same time.

    Returns
    -------
    SimulationGroup or list(SimulationGroup)
        SimulationGroup created from the list of parameter scans the group
        should explore. If a max_size is provided, returns a list of
        SimulationGroup instances collectively scanning the entire parameter
        space described by parameter_scans, each with  0 < size <= max_size.
    """
    if not param_names or not group_size:
        return

    if lazy_loading and not isinstance(center_sim, LazyLoadingSimulation):
        center_sim = LazyLoadingSimulation.from_simulation(center_sim)

    if isinstance(dist_types, str):
        dist_types = [dist_types] * len(param_names)

    assert len(dist_types) == len(dist_desc)

    param_scans = []
    for param, dist_type, desc in zip(param_names, dist_types, dist_desc):
        param_scan = RandomParameterScanDescription(
            name=param,
            distribution=dist_type,
            dist_param1=desc[0],
            dist_param2=desc[1],
            target_simulation=center_sim)
        param_scans.append(param_scan)

    sim_diff_list = sim_diffs_from_random_parameter_scans(
        param_scans, group_size, adtl_params=adtl_params)
    return build_group_or_groups(group_name,
                                 center_sim,
                                 sim_diff_list,
                                 max_block_size,
                                 group_type=SIM_GROUP_MC_TYPE)
 def setUpClass(cls):
     super(TestCreateRunLazySimulationGroup, cls).setUpClass()
     cls.sim = LazyLoadingSimulation.from_simulation(cls.sim)
     cls.sim_class = LazyLoadingSimulation
Ejemplo n.º 14
0
 def setUpClass(cls):
     super(TestLazySimulationRun, cls).setUpClass()
     # Convert the simulation to a LazyLoading one
     cls.real_sim = LazyLoadingSimulation.from_simulation(cls.real_sim)