def test_inverted_start_stop_fails(self):
     first_simulated_step = "Strip"
     last_simulated_step = "Load"
     with self.assertRaises(ValueError):
         build_sim_method_from_method(self.source_method,
                                      first_simulated_step,
                                      last_simulated_step)
 def test_build_sim_method_from_short_method_fail(self):
     self.source_method.method_steps = self.source_method.method_steps[2:]
     # This has to fail because no step before the first simulated step and
     # no initial buffer specified.
     with self.assertRaises(ValueError):
         build_sim_method_from_method(
             self.source_method, self.first_simulated_step,
             self.last_simulated_step
         )
    def to_simulation(self):
        """ Returns a new Simulation object from current builder.
        """
        from kromatography.model.factories.method import \
            build_sim_method_from_method

        sim_method = build_sim_method_from_method(
            self.method, self.first_simulated_step_name,
            self.last_simulated_step_name, initial_buffer=self.initial_buffer,
        )

        simulation = Simulation(
            name=self.simulation_name,
            column=self.column,
            method=sim_method,
            first_simulated_step=self.first_simulated_step_name,
            last_simulated_step=self.last_simulated_step_name,
            transport_model=self.transport_model,
            binding_model=self.binding_model,
            solver=Solver(),
            discretization=Discretization(),
            sensitivity=Sensitivity(),
        )

        # Product is a property of a simulation, which isn't set until we set
        # the method.
        if not is_has_traits_almost_equal(simulation.product, self.product):
            msg = ("The simulation's product (read in the method) doesn't "
                   "match the selected product. Please review the product and"
                   " method selected.")
            logger.info(msg)
            warning(None, msg, "Review simulation data")

        return simulation
    def test_build_sim_method_custom_initial_buffer(self):
        pre_equil_step = self.source_method.method_steps[1]
        pre_equil_buffer = pre_equil_step.solutions[0]

        method = build_sim_method_from_method(
            self.source_method, self.first_simulated_step,
            self.last_simulated_step, initial_buffer=pre_equil_buffer
        )

        self.assertValidMethod(method)
 def test_build_sim_method_from_short_method(self):
     equil_step = self.source_method.method_steps[1]
     equil_buffer = equil_step.solutions[0]
     # strip out pre-equil and equil steps and make sure we can still build
     # the sim.
     with self.remove_n_steps_source_method():
         method = build_sim_method_from_method(
             self.source_method, self.first_simulated_step,
             self.last_simulated_step, initial_buffer=equil_buffer
         )
         self.assertValidMethod(method, init_buffer_auto_set=False)
Esempio n. 6
0
 def rebuild_method(self, sim):
     """ Return a fully formed method from source experiment.
     """
     from kromatography.model.factories.method import \
         build_sim_method_from_method
     # Rebuild the method from source_experiment if possible"
     if sim.source_experiment is not None:
         source_method = sim.source_experiment.method
         fstep = sim.method.method_steps[0].name
         lstep = sim.method.method_steps[-1].name
         sim_method = build_sim_method_from_method(source_method, fstep,
                                                   lstep)
         return sim_method
 def test_build_1step_method(self):
     """ Test that a simulation method can be built from a method containing
     just the method steps simulated.
     """
     equil_step = self.source_method.method_steps[1]
     equil_buffer = equil_step.solutions[0]
     first_simulated_step = "Load"
     last_simulated_step = first_simulated_step
     method = build_sim_method_from_method(
         self.source_method, first_simulated_step, last_simulated_step,
         initial_buffer=equil_buffer
     )
     self.assertValidMethod(method, num_steps=1, fstep=first_simulated_step,
                            lstep=last_simulated_step, test_pooling=False)
    def to_simulations(self):
        """ Returns a list of simulations built from each experiment selected.
        """
        fstep, lstep = (self.first_simulated_step_name,
                        self.last_simulated_step_name)
        new_simulations = []
        for exp_name, sim_name in zip(self.experiment_selected,
                                      self.simulation_names):
            experiment = self.target_study.search_experiment_by_name(exp_name)
            try:
                sim_method = build_sim_method_from_method(
                    experiment.method,
                    fstep,
                    lstep,
                    initial_buffer=self.initial_buffer,
                )
            except StepLookupError as e:
                msg = "Failed to find the start/stop method steps for " \
                      "simulation {}. Its experiment doesn't seem to contain" \
                      " {} or {}. Please review these step names or treat " \
                      "this simulation separately from the others. Aborting..."
                msg = msg.format(sim_name, fstep, lstep)
                details = " Details: error was {}".format(e)
                logger.error(msg + details)
                warning(None, msg)
                return []

            simulation = Simulation(
                name=sim_name,
                column=experiment.column.clone_traits(copy="deep"),
                method=sim_method,
                first_simulated_step=self.first_simulated_step_name,
                last_simulated_step=self.last_simulated_step_name,
                transport_model=self.transport_model,
                binding_model=self.binding_model,
                source_experiment=experiment,
                solver=Solver(),
                discretization=Discretization(),
                sensitivity=Sensitivity(),
            )
            new_simulations.append(simulation)

        return new_simulations
    def test_build_sim_method(self):
        method = build_sim_method_from_method(self.source_method,
                                              self.first_simulated_step,
                                              self.last_simulated_step)

        self.assertValidMethod(method)
Esempio 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