def test_get_simulation_engine(self): model = MakeModel.make_test_model( '2 species, 1 reaction, with rates given by reactant population') simulation = Simulation(model) self.assertEqual(simulation.get_simulation_engine(), None) with CaptureOutput(relay=False): simulation.run(time_max=5) self.assertTrue( isinstance(simulation.get_simulation_engine(), SimulationEngine))
def test_provide_event_counts(self): model = MakeModel.make_test_model( '2 species, 1 reaction, with rates given by reactant population') simulation = Simulation(model) self.assertTrue('execute run() to obtain event counts' in simulation.provide_event_counts()) with CaptureOutput(relay=False): simulation.run(time_max=100) self.assertTrue('Event type' in simulation.provide_event_counts())
def _default(self): args = self.app.pargs simulation = Simulation(args.model_file) simulation.run(time_max=args.time_max, results_dir=args.results_dir, checkpoint_period=args.checkpoint_period, dfba_time_step=args.dfba_time_step, profile=args.profile, verbose=args.verbose)
def simulate(self, end_time, checkpoint_period, peturbation=None): if perturbation: model = copy.deepcopy(self.MODEL) peturbation(model) else: model = self.MODEL sim = Simulation(self.MODEL) sim.run(time_max=end_time, results_dir=self._results_path, checkpoint_period=checkpoint_period) return RunResults(self._results_path)
def simulate(self, model, time_max=100.): # simulate env = EnvironmentVarGuard() env.set( 'CONFIG__DOT__wc_lang__DOT__validation__DOT__validate_element_charge_balance', '0') with env: simulation = Simulation(model) _, results_dirname = simulation.run(time_max=time_max, ode_time_step=1., checkpoint_period=1., results_dir=self.tempdir) # get results results = RunResults(results_dirname) agg_states = results.get('aggregate_states') comp_mass = agg_states[(self.comp.id, 'mass')] comp_vol = results.get('functions')[self.volume.id] pops = results.get('populations') time = pops.index pop_constant = pops[self.spec_constant.id] pop_dynamic = pops[self.spec_dynamic.id] return (time, pop_constant, pop_dynamic, comp_mass, comp_vol)
def run_model(model, results_dir, checkpoint_period=5, end_time=100): """ Simulates model """ if not os.path.exists(results_dir): os.makedirs(results_dir) simulation = Simulation(model) results = simulation.run(end_time, results_dir, checkpoint_period) return results
def simulate(self, end_time, checkpoint_period=None, n_sims=1): results = [] simulation = Simulation(self.model) for i_sim in range(n_sims): temp_dir = tempfile.mkdtemp(dir=self.results_dir) results_dir = simulation.run( time_max=end_time, results_dir=temp_dir, checkpoint_period=checkpoint_period).results_dir run_results = RunResults(results_dir) results.append(run_results) return results
def test_simulate_deterministic_simulation_algorithm_submodel(self): model = MakeModel.make_test_model('1 species, 1 reaction') self.transform_model_for_dsa_simulation(model) simulation = Simulation(model) num_events, _ = simulation.run(time_max=100) self.assertGreater(num_events, 0)
def test_check_simul_results(self): init_volume = 1E-16 init_density = 1000 molecular_weight = 100. default_species_copy_number = 10_000 init_accounted_mass = molecular_weight * default_species_copy_number / Avogadro init_accounted_density = init_accounted_mass / init_volume expected_initial_values_compt_1 = dict( init_volume=init_volume, init_accounted_mass=init_accounted_mass, init_mass=init_volume * init_density, init_density=init_density, init_accounted_density=init_accounted_density, accounted_fraction=init_accounted_density / init_density) expected_initial_values = {'compt_1': expected_initial_values_compt_1} model = MakeModel.make_test_model( '1 species, 1 reaction', init_vols=[expected_initial_values_compt_1['init_volume']], init_vol_stds=[0], density=init_density, molecular_weight=molecular_weight, default_species_copy_number=default_species_copy_number, default_species_std=0, submodel_framework='WC:deterministic_simulation_algorithm') multialgorithm_simulation = MultialgorithmSimulation( model, self.wc_sim_config) _, dynamic_model = multialgorithm_simulation.build_simulation() check_simul_results(self, dynamic_model, None, expected_initial_values=expected_initial_values) # test dynamics simulation = Simulation(model) _, results_dir = simulation.run(time_max=2, **self.args) nan = float('NaN') check_simul_results(self, dynamic_model, results_dir, expected_initial_values=expected_initial_values, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 9999., 9998.]}) check_simul_results(self, dynamic_model, results_dir, expected_initial_values=expected_initial_values, expected_species_trajectories=\ {'spec_type_0[compt_1]':[nan, nan, nan]}) with self.assertRaises(AssertionError): check_simul_results(self, dynamic_model, results_dir, expected_initial_values=expected_initial_values, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 10000., 9998.]}) with self.assertRaises(AssertionError): check_simul_results(self, dynamic_model, results_dir, expected_initial_values=expected_initial_values, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 10000.]}) check_simul_results(self, dynamic_model, results_dir, expected_initial_values=expected_initial_values, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 9999., 9998.]}, rel_tol=1E-5) check_simul_results(self, dynamic_model, results_dir, expected_property_trajectories={ 'compt_1': { 'mass': [1.000e-13, 9.999e-14, 9.998e-14] } }) check_simul_results(self, dynamic_model, results_dir, expected_property_trajectories={ 'compt_1': { 'mass': [nan, nan, nan] } }) with self.assertRaises(AssertionError): check_simul_results(self, dynamic_model, results_dir, expected_property_trajectories={ 'compt_1': { 'mass': [1.000e-13, 1.000e-13, 9.999e-14] } }, rel_tol=0) plots_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', 'tests', 'results')) os.makedirs(plots_dir, exist_ok=True) plot_expected_vs_simulated(dynamic_model, 'ordinary_differential_equations', results_dir, trajectory_times=[0, 1, 2], plots_dir=plots_dir, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 10000., 9998.]}, expected_property_trajectories=\ {'compt_1': {'mass':[1.000e-13, 1.000e-13, 9.999e-14]}}) plot_expected_vs_simulated(dynamic_model, 'ordinary_differential_equations', results_dir, trajectory_times=[0, 1, 2], plots_dir=plots_dir, expected_property_trajectories=\ {'compt_1': {'mass':[1.000e-13, 1.000e-13, 9.999e-14]}}) plot_expected_vs_simulated(dynamic_model, 'ordinary_differential_equations', results_dir, trajectory_times=[0, 1, 2], plots_dir=plots_dir, expected_species_trajectories=\ {'spec_type_0[compt_1]':[10000., 10000., 9998.]}) plot_expected_vs_simulated(dynamic_model, 'ordinary_differential_equations', results_dir, trajectory_times=[0, 1, 2], plots_dir=plots_dir, expected_species_trajectories=\ {'spec_type_0[compt_1]':[nan, nan, nan]}, expected_property_trajectories=\ {'compt_1': {'mass':[nan, nan, nan]}})