def test_exp_probs(): np.random.seed(2001) inst_params = {"scale": 20 * mcfpd_to_gps} time = simulation_classes.Time() gas_field = simulation_classes.GasField() test_ad = AD(time, gas_field, detection_model_name="exponential") test_fluxes = np.array([5, 10, 20, 50, 100, 120, 2000]) * mcfpd_to_gps probs = test_ad.get_exp_probabilities(test_fluxes, inst_params=inst_params) truth_probs = [ 0.221199, 0.393469, 0.632121, 0.917915, 0.993262, 0.997521, 1. ] np.testing.assert_array_almost_equal(probs, truth_probs)
def test_legacy_AD(): np.random.seed(2001) inst_params = {} # legacy has no parameters. time = simulation_classes.Time() gas_field = simulation_classes.GasField() test_ad = AD(time, gas_field) test_fluxes = np.array([5, 10, 20, 50, 100, 120, 2000]) * mcfpd_to_gps probs = test_ad.get_legacy_probabilities(test_fluxes, inst_params=inst_params) truth_probs = [ 0.99001, 0.99006, 0.990161, 0.990462, 0.990964, 0.991165, 1. ] np.testing.assert_array_almost_equal(probs, truth_probs)
def test_exp_erf_probs(): np.random.seed(2001) inst_params = { "center": 35 * mcfpd_to_gps, "width": 21 * mcfpd_to_gps, "scale": 3 * mcfpd_to_gps } time = simulation_classes.Time() gas_field = simulation_classes.GasField() test_ls = AD(time, gas_field) test_fluxes = np.array([5, 10, 20, 50, 100, 120, 2000]) * mcfpd_to_gps probs = test_ls.get_exp_erf_probabilities(test_fluxes, inst_params) truth_probs = [ 0.011896, 0.034174, 0.134452, 0.79465, 0.97044, 0.97531, 0.998501 ] np.testing.assert_array_almost_equal(probs, truth_probs)
def field_simulation(gas_field=None, atm=None, input_leaks=None, dir_out='Results', time=None, econ_set=None, tech_dict=None, detection_techs=None, display_status=True): """ field_simulation generates a single realization of scenario. The scenario is defined by the input values. gas_field a GasField object atm an Atmosphere object input_leaks a list of leaks to be generated at each timestep dir_out directory name in which to save results time a Time object econ_set a FinanceSettings object tech_dict a dict of detection technology objects detection_techs a list of detection technology identifying strings """ # -------------- Define settings -------------- # time defines parameters related to time in the model. Time units are days. if time is None: time = simulation_classes.Time() if gas_field is None: gas_field = simulation_classes.GasField() leak_list = copy.deepcopy(gas_field.initial_leaks) # Note: econ_settings are not used during the simulation, but are saved for use in post simulation data processing if econ_set is None: econ_set = simulation_classes.FinanceSettings() if atm is None: atm = simulation_classes.Atmosphere(time.n_timesteps) if detection_techs is None: detection_techs = ['fid.FID', 'null.Null', 'ir.AIR', 'ir.MIR', 'dd.DD'] new_leaks, no_repair_leakage = [], [] if tech_dict is None: tech_dict = dict() for tech in detection_techs: [tech_mod, tech_class] = tech.split(sep='.') tech_dict[tech_class] = eval('Dm.' + tech_mod.lower() + '.' + tech_class + '(time, gas_field)') elif 'Null' not in tech_dict: tech_dict['Null'] = Dm.null.Null(time, gas_field) # -------------- Run the simulation -------------- n_leaks = [] for time.time_index in range(0, time.n_timesteps): time.current_time += time.delta_t if display_status and time.current_time % int( time.end_time / 10) < time.delta_t: print("Currently evaluating time step " + str(time.time_index) + " of " + str(time.n_timesteps)) if input_leaks is None: new_leaks.append( gas_field.leak_size_maker(new_leak_count(time, gas_field), gas_field)) else: new_leaks.append(input_leaks[time.time_index]) no_repair_leakage.append(sum(leak_list.flux)) leak_list.extend(new_leaks[-1]) # Loop through each LDAR program: for tech_obj in tech_dict.values(): n_leaks.append(tech_obj.leaks.n_leaks) tech_obj.leakage.append(sum(tech_obj.leaks.flux)) tech_obj.detection(time, gas_field, atm) tech_obj.leaks.extend(new_leaks[-1]) # -------------- Save results -------------- results = simulation_classes.Results(time, gas_field, tech_dict, leak_list, no_repair_leakage, atm, econ_set, new_leaks, n_leaks) save_results(dir_out, results)
def field_simulation(gas_field=None, atm=None, input_leaks=None, dir_out="Results", time=None, econ_set=None, tech_dict=None, detection_techs=None, display_status=True, label=None, runID=None, fullResults=False): """ field_simulation generates a single realization of scenario. The scenario is defined by the input values. gas_field a GasField object atm an Atmosphere object input_leaks a list of leaks to be generated at each timestep dir_out directory name in which to save results time a Time object econ_set a FinanceSettings object tech_dict a dict of detection technology objects detection_techs a list of detection technology identifying strings label a string to include in the filenames of the realization results runID a number to include in the filenames of the realization results """ # -------------- Define settings -------------- # time defines parameters related to time in the model. Time units are days. if time is None: time = simulation_classes.Time() if gas_field is None: gas_field = simulation_classes.GasField() leak_list = copy.deepcopy(gas_field.initial_leaks) # Note: econ_settings are not used during the simulation, but are saved for use in post # simulation data processing if econ_set is None: econ_set = simulation_classes.FinanceSettings() if atm is None: atm = simulation_classes.Atmosphere(time.n_timesteps) if detection_techs is None: detection_techs = ["FID", "Null", "AIR", "MIR", "DD", "AD"] else: print("Running {}".format(detection_techs)) new_leaks, no_repair_leakage = [], [] if tech_dict is None: tech_dict = dict() for tech in detection_techs: tech_dict[tech] = DETECTION_CLASSES[tech](time, gas_field) elif "Null" not in tech_dict: print("Warning: Null tech not in dict. Adding it.") tech_dict["Null"] = Dm.null.Null(time, gas_field) # --- Establish base conditions # print("field_simulation run {}".format(runID)) # print("field_simulation: Timesteps: {}. Current time: {} Time index: {}".format( # time.n_timesteps, # time.current_time, # time.time_index)) # -------------- Run the simulation -------------- n_leaks = [] for time.time_index in range(0, time.n_timesteps): time.current_time += time.delta_t # if display_status and time.current_time % int(time.end_time / 10) < time.delta_t: # print("Currently evaluating time step " + str(time.time_index) # + " of " + str(time.n_timesteps)) if input_leaks is None: # and LEAKSTHERE_ALREADY_WHY NOT THERE: new_leaks.append(make_leaks(new_leak_count(time, gas_field), gas_field)) # print("New leaks on Day {:.1f}: {}".format(time.current_time, # len(new_leaks[-1].flux))) else: new_leaks.append(input_leaks[time.time_index]) no_repair_leakage.append(sum(leak_list.flux)) leak_list.extend(new_leaks[-1]) # Loop through each LDAR program: for tech_obj in tech_dict.values(): # the following is a terrible idea, it should be 2-D # just append the number of leaks for this tech for this step in a long list. n_leaks.append(tech_obj.leaks.n_leaks) # append leakage in this step (not necessarily total daily leakage) for this tech tech_obj.leakage.append(sum(tech_obj.leaks.flux)) # Find and remove leaks tech_obj.detection(time, gas_field, atm) # Tack on the new leaks for this time step tech_obj.leaks.extend(new_leaks[-1]) # -------------- Save results -------------- results = simulation_classes.Results(time, gas_field, tech_dict, leak_list, no_repair_leakage, atm, econ_set, new_leaks, n_leaks) save_results(dir_out, results, label=label, runID=runID, fullResults=fullResults)