def test_update_std_run_sim(self): sim = make_sample_simulation(name='Run_1') self.assertIsNone(sim.output) # Contains data from the CADET run: h5file = io_data_path("Sample_Sim_from_Run_1_cadet_simulation_run.h5") with self.assertTraitChanges(sim, "perf_param_data_event", 1): update_simulation_results(sim, h5file) self.assertIsInstance(sim.output, SimulationResults) perf_params = sim.output.performance_data self.assertIsInstance(perf_params, PerformanceData) self.assertIsInstance(perf_params.pool, SolutionWithProduct) num_comp = len(sim.product.product_components) self.assertEqual( len(perf_params.pool.product_component_concentrations), # noqa num_comp) assert_unit_scalar_almost_equal(perf_params.pool_volume, UnitScalar(1.727090, units="CV"), eps=1.e-6) assert_unit_scalar_almost_equal(perf_params.step_yield, UnitScalar(95.814, units='%'), eps=1.e-3) assert_unit_scalar_almost_equal(perf_params.start_collect_time, UnitScalar(200.946, units='minute'), eps=1.e-3) assert_unit_scalar_almost_equal(perf_params.stop_collect_time, UnitScalar(227.9, units='minute'), eps=1.e-3)
def setUp(self): self.study = make_sample_study2(add_transp_bind_models=True, add_sims='Run_1') self.sim = self.study.simulations[0] self.sim_run = self.sim.copy() # Save time by loading results from an already generated file: self.result_filepath = io_data_path("std_example_xlsx_run1_cadet.h5") update_simulation_results(self.sim_run, self.result_filepath) self.sim_run.set_as_run()
def test_perf_data_name_update(self): # Since performance data pane shows sim's perf data, make sure the # change of name in sim is reflected in its performance data. sim = self.sim_from_scratch # Make it as if it is run: result_filepath = io_data_path("std_example_xlsx_run1_cadet.h5") update_simulation_results(sim, result_filepath) sim.set_as_run() sim.name = "foo" self.assertEqual(sim.output.performance_data.name, "foo")
def test_update_results(self): # Since performance data pane shows sim's perf data, make sure the # change of name in sim is reflected in its performance data. sim = self.sim_from_scratch # Make it as if it is run: result_filepath = io_data_path("std_example_xlsx_run1_cadet.h5") update_simulation_results(sim, result_filepath) sim.set_as_run() self.assertTrue(sim.has_run) self.assertTrue(isfile(sim.cadet_filepath)) self.assertIsInstance(sim.output, SimulationResults)
def make_sample_simulation(name='Run_3', result_file=""): """ Load a simulation from the default input file. """ from kromatography.model.factories.simulation import \ build_simulation_from_experiment from kromatography.io.simulation_updater import update_simulation_results exp = make_sample_experiment2(name=name) simu = build_simulation_from_experiment(exp) if result_file: update_simulation_results(simu, result_file) return simu
def setUpClass(cls): input_file = io_data_path('ChromExampleDataV2.xlsx') cls.study = load_study_from_excel(input_file, allow_gui=False) # Building and running simulation for only 1 experiment expt = cls.study.experiments[0] output_file = "{}_{}_{}.h5".format(cls.study.name, expt.name, 'cadet_simulation') output_file = string2filename(output_file) sim = build_simulation_from_experiment(expt) cls.study.simulations.append(sim) # create the CADET inputs cadet_input = build_cadet_input(sim) # write the CADET inputs write_to_h5(output_file, cadet_input, root='/input', overwrite=True) # run simulation to generate outputs run_cadet_simulator(output_file) update_simulation_results(sim, output_file)
def load_default_experiment_simulation(expt_id='Run_1'): """ Load a default experiment and already run simulation. DEPRECATED: use kromatography.model.tests.sample_data_factories.make_sample_experiment2 instead. """ # Load the experiment input_file = io_data_path('ChromExampleDataV2.xlsx') study = load_exp_study_from_excel(input_file, datasource=None, allow_gui=False) expt = study.search_experiment_by_name(expt_id) # Build and load the simulation sim = build_simulation_from_experiment(expt) build_cadet_input(sim) output_file = io_data_path('Chrom_Example_Run_1_cadet_simulation.h5') update_simulation_results(sim, output_file) return expt, sim
def get_simulation(self, sim_idx): """ Retrieve or rebuild a simulation from the group. Parameters ---------- sim_idx : int Index of the simulation in the simulations list. """ if self.simulations: return self.simulations[sim_idx] diff = self.simulation_diffs[sim_idx] sim = add_sim_to_diff(self.center_point_simulation, diff) output_filename = self._simulation_output_cache[sim_idx] sim.uuid = UUID(output_filename[:-len(FILENAME_SUFFIX)]) sim.name = self._sim_name_from_idx(sim_idx) if isfile(sim.cadet_filepath): update_simulation_results(sim) sim.set_as_run() else: sim.set_as_not_run() return sim
def setUp(self): self.study = make_sample_study2(add_transp_bind_models=True, add_sims='Run_1') self.sim = self.study.simulations[0] # Save time by loading results from an already generated file: self.result_filepath = io_data_path("std_example_xlsx_run1_cadet.h5") update_simulation_results(self.sim, output_fname=self.result_filepath) self.sim.set_as_run() # Add a simulation grid grp = SimulationGroup(center_point_simulation=self.sim, name="new group") self.study.analysis_tools.simulation_grids.append(grp) # Add a Monte Carlo simulation group mc_grp = make_sample_mc_simulation_group() self.study.analysis_tools.monte_carlo_explorations.append(mc_grp) # Skip attributes not stored in a study/task: (user) datasource, # study_datasource.dirty, sim group's cp, self.ignore = {'center_point_simulation', 'datasource', 'perf_params', 'dirty'}
def _update_simulation(self, sim, results): """ Analyze the results from the CADET executor and update simulation. """ output_file = results["output_file"] expected_output_file = sim._get_cadet_filepath() if expected_output_file != output_file: msg = "Logic is bad: attempting to update simulation {} with " \ "wrong CADET output file {}." msg = msg.format(expected_output_file, output_file) logger.critical(msg) raise RuntimeError(msg) if results["exception"]: msg = "CADET failed to run on simulation {} ({}) with error {}. " \ "Full output was\n{}" msg = msg.format(sim.name, sim.uuid, results["exception"], results["cadet_output"]) logger.error(msg) sim.run_status = SIM_FINISHED_FAIL elif results["cadet_errors"]: msg = "CADET failed to solve simulation {} ({}) with error {}." \ "Full output was\n{}" msg = msg.format(sim.name, sim.uuid, results["cadet_errors"], results["cadet_output"]) logger.warning(msg) sim.run_status = SIM_FINISHED_FAIL else: msg = "CADET ran successfully on simulation {} ({}). Full output" \ " was\n{}." msg = msg.format(sim.name, sim.uuid, results["cadet_output"]) logger.debug(msg) update_simulation_results(sim, output_file) sim.run_status = SIM_FINISHED_SUCCESS # Trigger a final update of the simulation object sim.cadet_run_finished = True
def setUpClass(cls): cls.tmp_dir = tempfile.mkdtemp() # Run the analysis input_file = io_data_path('ChromExampleDataV2.xlsx') outfile = join(cls.tmp_dir, 'cadet_data.h5') cls.exp_id = 'Run_1' binding_model = make_sample_binding_model() transport_model = make_sample_transport_model() cls.study = run_chromatography_simulation( input_file, output_file=outfile, expt_id=cls.exp_id, skip_plot=True, skip_animation=True, skip_cadet=False, binding_model=binding_model, transport_model=transport_model, allow_gui=False) sim_name = 'Sim: {}'.format(cls.exp_id) cls.output_sim = cls.study.search_simulation_by_name(sim_name) initial_experiment = cls.study.search_experiment_by_name(cls.exp_id) cls.expected_sim = build_simulation_from_experiment( initial_experiment, binding_model=binding_model, transport_model=transport_model) out_file = io_data_path('Chrom_Example_Run_1_cadet_simulation.h5') update_simulation_results(cls.expected_sim, out_file) cls.prod_comps = cls.expected_sim.product.product_component_names cls.expected_components = cls.expected_sim.output.continuous_data.keys( ) # noqa cls.found_components = cls.output_sim.output.continuous_data.keys()
def run_chromatography_simulation(input_file, output_file=None, expt_id="Run_1", skip_cadet=False, skip_plot=False, skip_animation=False, binding_model=None, transport_model=None, allow_gui=True): """ Run chromatography simulation for the inputs given in `input_file` and plot the results. Parameters ---------- input_file : str (file path) The file path for the excel file containing the experiment/simulation input data. output_file : str (file path) The file path for the CADET output h5 file. If None, then a default name is chosen based on the study and experiment ids. expt_id : str The name of the exeperiment to use for intializing the CADET simulation. If None, the first experiment in the study is chosen. skip_cadet : bool If True, the CADET simulation is not run but instead the output file is directly loaded (assuming one already exists). The main usecase here is for testing. skip_plot : bool If True, the chromatogram plot is not generated. This is useful for testing. Returns ------- study : Study The Study instance containing the experiment and the simulation data. """ study = load_study_from_excel(input_file, allow_gui=allow_gui) # lets just pick a single experiment if expt_id is None: expt = study.experiments[0] else: expt = study.search_experiment_by_name(expt_id) logger.info('Running simulation for experiment : {!r}'.format(expt_id)) if output_file is None: output_file = ("{}_{}_{}.h5".format(study.name, expt.name, 'cadet_simulation')) output_file = string2filename(output_file) # create a simulation object from an experiment # FIXME: how to identify related expt and simulations ! sim = build_simulation_from_experiment(expt, binding_model=binding_model, transport_model=transport_model) study.simulations.append(sim) # create the CADET inputs cadet_input = build_cadet_input(sim) # NOTE: This is primarily used for testing/debugging workflow when the # CADET simulation doesn't need to be run everytime. if not skip_cadet: # write the CADET inputs write_to_h5(output_file, cadet_input, root='/input', overwrite=True) logger.info("Output file {} was generated".format(output_file)) # run simulation to generate outputs run_cadet_simulator(output_file) update_simulation_results(sim, output_file) if not skip_plot: plot_chromatogram(expt, sim) if not skip_animation: column_animation(sim) return study