def setup_test(self, end_time_delay, num_les, ts=900.): stime = datetime(2015, 1, 1, 12, 0) etime = stime + end_time_delay st_pos = (0, 0, 0) oil = test_oil m1 = Model(start_time=stime, time_step=ts) m1.environment += [constant_wind(0, 0), Water()] m1.weatherers += Evaporation() m1.spills += point_line_release_spill(num_les[0], st_pos, stime, end_release_time=etime, substance=oil, amount=36000, units='kg') m1.outputters += WeatheringOutput() m2 = Model(start_time=stime, time_step=ts) m2.environment += [constant_wind(0, 0), Water()] m2.weatherers += Evaporation() m2.spills += point_line_release_spill(num_les[1], st_pos, stime, end_release_time=etime, substance=oil, amount=36000, units='kg') m2.outputters += WeatheringOutput() return (m1, m2)
def test_Water_init(): w = Water() assert w.temperature == 300.0 assert w.salinity == 35.0 w = Water(temperature=273, salinity=0) assert w.temperature == 273.0 assert w.salinity == 0.0
def test_full_run_no_evap(sample_model_fcn2, oil, temp, expected_balance): ''' test dissolution outputs post step for a full run of model. Dump json for 'weathering_model.json' in dump directory ''' low_wind = constant_wind(1., 270, 'knots') low_waves = Waves(low_wind, Water(temp)) model = sample_model_weathering2(sample_model_fcn2, oil, temp) model.environment += [Water(temp), low_wind, low_waves] # model.weatherers += Evaporation(Water(temp), low_wind) model.weatherers += NaturalDispersion(low_waves, Water(temp)) model.weatherers += Dissolution(low_waves, low_wind) print ('Model start time: {}, Duration: {}, Time step: {}' .format(model.start_time, model.duration, model.time_step)) for sc in model.spills.items(): print '\nSpill dict keys: ', sc.__dict__.keys() print '\nSpill data arrays: ', sc._data_arrays print 'num spills:', len(sc.spills) print ('spill[0] amount: {} {} ({})' .format(sc.spills[0].amount, sc.spills[0].units, sc.spills[0].substance.name) ) original_amount = sc.spills[0].amount # set make_default_refs to True for objects contained in model after adding # objects to the model model.set_make_default_refs(True) model.setup_model_run() dissolved = [] for step_num, step in enumerate(model): for sc in model.spills.items(): if step['step_num'] > 0: assert (sc.mass_balance['dissolution'] > 0) assert (sc.mass_balance['natural_dispersion'] > 0) assert (sc.mass_balance['sedimentation'] > 0) dissolved.append(sc.mass_balance['dissolution']) print ('\n#Step: {}'.format(step_num)) print ("Dissolved: {0}". format(sc.mass_balance['dissolution'])) print ("Mass: {0}". format(sc._data_arrays['mass'])) print ("Mass Components: {0}". format(sc._data_arrays['mass_components'])) print ('Fraction dissolved after full run: {}' .format(dissolved[-1] / original_amount)) assert dissolved[0] == 0.0 assert np.isclose(dissolved[-1], expected_balance)
def test_weatherer_sort(): ''' Sample model with weatherers - only tests sorting of weathereres. The Model will likely not run ''' model = Model() skimmer = Skimmer(100, 'kg', efficiency=0.3, active_start=datetime(2014, 1, 1, 0, 0), active_stop=datetime(2014, 1, 1, 0, 3)) burn = Burn(100, 1, active_start=datetime(2014, 1, 1, 0, 0)) c_disp = ChemicalDispersion(.3, active_start=datetime(2014, 1, 1, 0, 0), active_stop=datetime(2014, 1, 1, 0, 3), efficiency=0.2) weatherers = [Emulsification(), Evaporation(Water(), constant_wind(1, 0)), burn, c_disp, skimmer] exp_order = [weatherers[ix] for ix in (3, 4, 2, 1, 0)] model.environment += [Water(), constant_wind(5, 0), Waves()] model.weatherers += weatherers # WeatheringData and FayGravityViscous automatically get added to # weatherers. Only do assertion on weatherers contained in list above assert model.weatherers.values()[:len(exp_order)] != exp_order model.setup_model_run() assert model.weatherers.values()[:len(exp_order)] == exp_order # check second time around order is kept model.rewind() assert model.weatherers.values()[:len(exp_order)] == exp_order # Burn, ChemicalDispersion are at same sorting level so appending # another Burn to the end of the list will sort it to be just after # ChemicalDispersion so index 2 burn = Burn(50, 1, active_start=datetime(2014, 1, 1, 0, 0)) exp_order.insert(3, burn) model.weatherers += exp_order[3] # add this and check sorting still works assert model.weatherers.values()[:len(exp_order)] != exp_order model.setup_model_run() assert model.weatherers.values()[:len(exp_order)] == exp_order
def test_setup_model_run(model): 'turn of movers/weatherers and ensure data_arrays change' model.environment += Water() model.rewind() model.step() exp_keys = { 'windages', 'windage_range', 'mass_components', 'windage_persist' } # no exp_keys in model data_arrays assert not exp_keys.intersection(model.spills.LE_data) cwm = gnome.movers.constant_wind_mover(1., 0.) model.weatherers += [HalfLifeWeatherer(), Evaporation()] model.movers += cwm model.rewind() model.step() assert exp_keys.issubset(model.spills.LE_data) cwm.on = False for w in xrange(2): model.weatherers[w].on = False model.rewind() model.step() assert not exp_keys.intersection(model.spills.LE_data)
def test_validate_model_env_obj(self, obj_make_default_refs): ''' test that Model is invalid if make_default_refs is True and referenced objects are not in model's environment collection ''' # object is complete but model must contain (model, waves) = self.make_model_incomplete_waves() waves.water = Water() waves.wind = constant_wind(5, 0) assert len(model.environment) == 1 waves.make_default_refs = obj_make_default_refs (msgs, isvalid) = model.validate() print msgs if obj_make_default_refs: assert not isvalid assert len(msgs) > 0 assert ('warning: Model: water not found in environment collection' in msgs) assert ('warning: Model: wind not found in environment collection' in msgs) else: assert isvalid assert len(msgs) == 0
def allWeatherers(timeStep, start_time, duration, weatheringSteps, map, uncertain, data_path, curr_path, wind_path, map_path, reFloatHalfLife, windFile, currFile, tidalFile, num_elements, depths, lat, lon, output_path, wind_scale, save_nc, timestep_outputs, weatherers, td): print 'initializing the model:' model = Model(time_step=timeStep, start_time=start_time, duration=duration) print 'adding the map:' map_folder = os.path.join(data_path, map_path) if not(os.path.exists(map_folder)): print('The map folder is incorrectly set:', map_folder) mapfile = get_datafile( os.path.join(map_folder,map) ) model.map = MapFromBNA(mapfile, refloat_halflife=reFloatHalfLife) print 'adding a renderer' model.outputters += Renderer(mapfile, output_path, size=(800, 600), output_timestep=timedelta(hours=1)) if save_nc: nc_outputter = NetCDFOutput(netcdf_file, which_data='most', output_timestep=timedelta(hours=1)) model.outputters += nc_outputter print 'adding a wind mover:' wind_file = get_datafile(os.path.join(data_path, wind_path, windFile)) wind = GridWindMover(wind_file) wind.wind_scale = wind_scale model.movers += wind print 'adding a current mover: ' curr_file = get_datafile(os.path.join(data_path, curr_path, currFile)) model.movers += GridCurrentMover(curr_file, num_method='RK4') if td: random_mover = RandomMover(diffusion_coef=10000) model.movers += random_mover print 'adding spill' model.spills += point_line_release_spill(num_elements=num_elements, start_position=(lon, lat, 0), release_time=start_time, end_release_time=start_time + duration) print 'adding weatherers' water = Water(280.92) wind = constant_wind(20.0, 117, 'knots') waves = Waves(wind, water) model.weatherers += Evaporation(water, wind) model.weatherers += Emulsification(waves) model.weatherers += NaturalDispersion(waves, water) return model
def test_full_run(sample_model_fcn, oil, temp): ''' test evaporation outputs for a full run of model. This contains a mover so at some point several elements end up on_land. This test also checks the evap_decay_constant for elements that are not in water is 0 so mass is unchanged. ''' model = sample_model_weathering(sample_model_fcn, oil, temp, 10) model.environment += [Water(temp), constant_wind(1., 0)] model.weatherers += [ Evaporation(model.environment[-2], model.environment[-1]) ] released = 0 init_rho = model.spills[0].substance.density_at_temp(temp) init_vis = model.spills[0].substance.kvis_at_temp(temp) for step in model: for sc in model.spills.items(): assert_helper(sc, sc.num_released - released) released = sc.num_released if sc.num_released > 0: assert np.all(sc['density'] >= init_rho) assert np.all(sc['viscosity'] >= init_vis) mask = sc['status_codes'] == oil_status.in_water assert sc.mass_balance['floating'] == np.sum(sc['mass'][mask]) print("Amount released: {0}".format( sc.mass_balance['amount_released'])) print "Mass floating: {0}".format(sc.mass_balance['floating']) print "Mass evap: {0}".format(sc.mass_balance['evaporated']) print "LEs in water: {0}".format(sum(mask)) print "Mass on land: {0}".format(np.sum(sc['mass'][~mask])) print "Completed step: {0}\n".format(step['step_num'])
def test_full_run(sample_model_fcn2, oil, temp, dispersed): ''' test dispersion outputs post step for a full run of model. Dump json for 'weathering_model.json' in dump directory ''' model = sample_model_weathering2(sample_model_fcn2, oil, temp) model.environment += [Water(temp), wind, waves] model.weatherers += Evaporation() model.weatherers += Emulsification(waves) model.weatherers += NaturalDispersion() # set make_default_refs to True for objects contained in model after adding # objects to the model model.set_make_default_refs(True) for step in model: for sc in model.spills.items(): if step['step_num'] > 0: # print ("Dispersed: {0}". # format(sc.mass_balance['natural_dispersion'])) # print ("Sedimentation: {0}". # format(sc.mass_balance['sedimentation'])) # print "Completed step: {0}\n".format(step['step_num']) assert (sc.mass_balance['natural_dispersion'] > 0) assert (sc.mass_balance['sedimentation'] > 0) sc = model.spills.items()[0] print (sc.mass_balance['natural_dispersion'], dispersed) assert np.isclose(sc.mass_balance['natural_dispersion'], dispersed, atol=0.001)
def test_weathering_data_attr(): ''' mass_balance is initialized/written if we have weatherers ''' ts = 900 s1_rel = datetime.now().replace(microsecond=0) s2_rel = s1_rel + timedelta(seconds=ts) s = [point_line_release_spill(10, (0, 0, 0), s1_rel), point_line_release_spill(10, (0, 0, 0), s2_rel)] model = Model(time_step=ts, start_time=s1_rel) model.spills += s model.step() for sc in model.spills.items(): assert len(sc.mass_balance) == 2 for key in ('beached', 'off_maps'): assert key in sc.mass_balance model.environment += [Water(), constant_wind(0., 0)] model.weatherers += [Evaporation(model.environment[0], model.environment[1])] # use different element_type and initializers for both spills s[0].amount = 10.0 s[0].units = 'kg' model.rewind() model.step() for sc in model.spills.items(): # since no substance is defined, all the LEs are marked as # nonweathering assert sc.mass_balance['non_weathering'] == sc['mass'].sum() assert sc.mass_balance['non_weathering'] == s[0].amount s[1].amount = 5.0 s[1].units = 'kg' model.rewind() exp_rel = 0.0 for ix in range(2): model.step() exp_rel += s[ix].amount for sc in model.spills.items(): assert sc.mass_balance['non_weathering'] == sc['mass'].sum() assert sc.mass_balance['non_weathering'] == exp_rel model.rewind() assert sc.mass_balance == {} # weathering data is now empty for all steps del model.weatherers[0] for ix in xrange(2): model.step() for sc in model.spills.items(): assert len(sc.mass_balance) == 2 assert (len(set(sc.mass_balance.keys()) - {'beached', 'off_maps'}) == 0)
def test_sort_order(): 'test sort order for Dissolution weatherer' wind = constant_wind(15., 0) waves = Waves(wind, Water()) diss = Dissolution(waves) assert weatherer_sort(diss) == 8
def model(sample_model, output_dir): model = sample_model_weathering(sample_model, test_oil) rel_start_pos = sample_model['release_start_pos'] rel_end_pos = sample_model['release_end_pos'] model.cache_enabled = True model.uncertain = True water, wind = Water(), constant_wind(1., 0) model.environment += [water, wind] model.weatherers += Evaporation(water, wind) et = floating(substance=model.spills[0].substance.name) N = 10 # a line of ten points line_pos = np.zeros((N, 3), dtype=np.float64) line_pos[:, 0] = np.linspace(rel_start_pos[0], rel_end_pos[0], N) line_pos[:, 1] = np.linspace(rel_start_pos[1], rel_end_pos[1], N) # print start_points model.spills += point_line_release_spill(1, start_position=rel_start_pos, release_time=model.start_time, end_position=rel_end_pos, element_type=et, amount=100, units='tons') model.outputters += TrajectoryGeoJsonOutput(output_dir=output_dir) model.rewind() return model
def weathering_data_arrays(n_arrays, water=None, time_step=15.*60, element_type=None, langmuir=False): ''' function to initialize data_arrays set by WeatheringData. Weatherer tests can use this function to release elements and initialize data without defining a model ''' if water is None: water = Water() rqd_weatherers = [WeatheringData(water), FayGravityViscous(water)] arrays = set() arrays.update(n_arrays) for wd in rqd_weatherers: arrays.update(wd.array_types) if element_type is None: element_type = floating(substance=test_oil) sc = sample_sc_release(num_elements=2, element_type=element_type, arr_types=arrays, time_step=time_step) for wd in rqd_weatherers: wd.prepare_for_model_run(sc) wd.initialize_data(sc, sc.num_released) return (sc, time_step, rqd_weatherers)
def test_get_emulsification_wind(): wind = constant_wind(3., 0) water = Water() w = Waves(wind, water) print w.get_emulsification_wind(start_time) assert w.get_emulsification_wind(start_time) == 3.0
def test_sort_order(): 'test sort order for Biodegradation weatherer' wind = constant_wind(15., 0) waves = Waves(wind, Water()) bio_deg = Biodegradation(waves) assert weatherer_sort(bio_deg) == 11
def test_Water_get(attr, unit, val, si_val): w = Water() setattr(w, attr, val) w.units[attr] = unit assert w.get(attr) == si_val assert w.get(attr, unit) == val
def build_waves_obj(wind_speed, wind_units, direction_deg, temperature): # also test with lower wind no dispersion wind = constant_wind(wind_speed, direction_deg, wind_units) water = Water(temperature=temperature) waves = Waves(wind, water) return waves
def model(sample_model_fcn, output_filename): """ Use fixture model_surface_release_spill and add a few things to it for the test """ model = sample_model_fcn['model'] model.cache_enabled = True model.spills += \ point_line_release_spill(num_elements=5, start_position=sample_model_fcn['release_start_pos'], release_time=model.start_time, end_release_time=model.start_time + model.duration, substance=test_oil, amount=1000, units='kg') water = Water() model.movers += RandomMover(diffusion_coef=100000) model.movers += constant_wind_mover(1.0, 0.0) model.weatherers += Evaporation(water=water, wind=model.movers[-1].wind) model.outputters += NetCDFOutput(output_filename) model.rewind() return model
def mk_test_objs(cls, water=None): ''' create SpillContainer and test WeatheringData object test objects so we can run Skimmer, Burn like a model without using a full on Model NOTE: Use this function to define class level objects. Other methods in this class expect sc, and weatherers to be class level objects ''' # spreading does not need to be initialized correctly for these tests, # but since we are mocking the model, let's do it correctly if water is None: water = Water() # keep this order weatherers = [WeatheringData(water), FayGravityViscous(water)] weatherers.sort(key=weatherer_sort) sc = SpillContainer() print "******************" print "Adding a spill to spill container" sc.spills += point_line_release_spill(10, (0, 0, 0), rel_time, substance=test_oil, amount=amount, units='kg', water=water) return (sc, weatherers)
def sample_model_weathering(sample_model_fcn, oil, temp=311.16, num_les=10): model = sample_model_fcn['model'] rel_pos = sample_model_fcn['release_start_pos'] # update model the same way for multiple tests model.uncertain = False # fixme: with uncertainty, copying spill fails! model.duration = timedelta(hours=4) et = gnome.spill.elements.floating(substance=oil) start_time = model.start_time + timedelta(hours=1) end_time = start_time + timedelta(seconds=model.time_step*3) spill = gnome.spill.point_line_release_spill(num_les, rel_pos, start_time, end_release_time=end_time, element_type=et, amount=100, units='kg') model.spills += spill # define environment objects that weatherers require model.environment += [constant_wind(1, 0), Water(), Waves()] return model
def sample_sc_intrinsic(self, num_elements, rel_time, add_at=None): ''' initialize Sample SC and WeatheringData object objects are constructed and prepare_for_model_run() is invoked on all ''' wd = WeatheringData(Water()) end_time = rel_time + timedelta(hours=1) spills = [ point_line_release_spill(num_elements, (0, 0, 0), rel_time, end_release_time=end_time, amount=100, units='kg', substance=test_oil) ] sc = SpillContainer() sc.spills += spills at = wd.array_types if add_at is not None: at.update(add_at) sc.prepare_for_model_run(at) # test initialization as well wd.prepare_for_model_run(sc) for val in sc.mass_balance.values(): assert val == 0.0 # test initialization as well return (sc, wd)
def test_load_location_file(self, saveloc_, model): ''' create a model load save file from script_boston which contains a spill. Then merge the created model into the model loaded from save file ''' m = Model() m.environment += [Water(), constant_wind(1., 0.)] m.weatherers += Evaporation(m.environment[0], m.environment[-1]) m.spills += point_line_release_spill(10, (0, 0, 0), datetime(2014, 1, 1, 12, 0)) # create save model sample_save_file = os.path.join(saveloc_, 'SampleSaveModel.zip') model.save(saveloc_, name='SampleSaveModel.zip') if os.path.exists(sample_save_file): model = load(sample_save_file) model.merge(m) for oc in m._oc_list: for item in getattr(m, oc): model_oc = getattr(model, oc) assert item is model_oc[item.id] for spill in m.spills: assert spill is model.spills[spill.id] # merge the other way and ensure model != m m.merge(model) assert model != m
def test_one_weather(self): ''' calls one weathering step and checks that we decayed at the expected rate. Needs more tests with varying half_lives ''' time_step = 15. * 60 hl = tuple([time_step] * subs.num_components) weatherer = HalfLifeWeatherer(half_lives=hl) sc = weathering_data_arrays(weatherer.array_types, Water(), time_step)[0] print '\nsc["mass"]:\n', sc['mass'] orig_mc = np.copy(sc['mass_components']) model_time = rel_time weatherer.prepare_for_model_run(sc) weatherer.prepare_for_model_step(sc, time_step, model_time) weatherer.weather_elements(sc, time_step, model_time) weatherer.model_step_is_done() print '\nsc["mass"]:\n', sc['mass'] assert np.allclose(0.5 * orig_mc.sum(1), sc['mass']) assert np.allclose(0.5 * orig_mc, sc['mass_components'])
def test_exceptions(attr, unit): w = Water() with pytest.raises(InvalidUnitError): w.get(attr, unit) with pytest.raises(InvalidUnitError): w.set(attr, 5, unit)
def test_init(): wind = constant_wind(15., 0) waves = Waves(wind, Water()) bio_deg = Biodegradation(waves) print(bio_deg.array_types) assert all([(at in bio_deg.array_types) for at in ('mass', 'droplet_avg_size')])
def test_not_implemented_in_water(): sample_time = 60 * 60 * 24 * 365 * 30 # seconds w = Water() with raises(AttributeError): w.data_start = sample_time with raises(AttributeError): w.data_stop = sample_time
def test_init(): 'test initialization' wind = constant_wind(15., 0) waves = Waves(wind, Water()) diss = Dissolution(waves) print diss.array_types assert all([(at in diss.array_types) for at in ('mass', 'viscosity', 'density')])
def test_properties_in_si(attr, unit, val, exp_si): ''' set properties in non SI units and check default get() returns it in SI ''' kw = {attr: val, 'units': {attr: unit}} w = Water(**kw) assert getattr(w, attr) == val assert w.units[attr] == unit assert w.get(attr) == exp_si
def model(sample_model): model = sample_model['model'] model.make_default_refs = True rel_start_pos = sample_model['release_start_pos'] rel_end_pos = sample_model['release_end_pos'] model.cache_enabled = True model.uncertain = False model.environment += Water(311.15) print 'adding a Weatherer' model.environment += constant_wind(1.0, 0.0) N = 10 # a line of ten points line_pos = np.zeros((N, 3), dtype=np.float64) line_pos[:, 0] = np.linspace(rel_start_pos[0], rel_end_pos[0], N) line_pos[:, 1] = np.linspace(rel_start_pos[1], rel_end_pos[1], N) # print start_points model.duration = timedelta(hours=6) end_time = model.start_time + timedelta(hours=1) spill = point_line_release_spill(1000, start_position=rel_start_pos, release_time=model.start_time, end_release_time=end_time, end_position=rel_end_pos, substance=test_oil, amount=1000, units='kg') model.spills += spill # figure out mid-run save for weathering_data attribute, then add this in rel_time = model.spills[0].get('release_time') skim_start = rel_time + timedelta(hours=1) amount = model.spills[0].amount units = model.spills[0].units skimmer = Skimmer(.3 * amount, units=units, efficiency=0.3, active_start=skim_start, active_stop=skim_start + timedelta(hours=1)) # thickness = 1m so area is just 20% of volume volume = spill.get_mass() / spill.get('substance').get_density() burn = Burn(0.2 * volume, 1.0, active_start=skim_start, efficiency=0.9) c_disp = ChemicalDispersion(.1, efficiency=0.5, active_start=skim_start, active_stop=skim_start + timedelta(hours=1)) model.weatherers += [Evaporation(), c_disp, burn, skimmer] model.outputters += WeatheringOutput() model.rewind() return model
def test_contains_object(sample_model_fcn): ''' Test that we can find all contained object types with a model. ''' model = sample_model_weathering(sample_model_fcn, test_oil) gnome_map = model.map = gnome.map.GnomeMap() # make it all water rel_time = model.spills[0].get('release_time') model.start_time = rel_time - timedelta(hours=1) model.duration = timedelta(days=1) water, wind = Water(), constant_wind(1., 0) model.environment += [water, wind] et = floating(substance=model.spills[0].get('substance').name) sp = point_line_release_spill(500, (0, 0, 0), rel_time + timedelta(hours=1), element_type=et, amount=100, units='tons') rel = sp.release initializers = et.initializers model.spills += sp movers = [m for m in model.movers] evaporation = Evaporation() skim_start = sp.get('release_time') + timedelta(hours=1) skimmer = Skimmer(.5 * sp.amount, units=sp.units, efficiency=0.3, active_start=skim_start, active_stop=skim_start + timedelta(hours=1)) burn = burn_obj(sp) disp_start = skim_start + timedelta(hours=1) dispersion = ChemicalDispersion(0.1, active_start=disp_start, active_stop=disp_start + timedelta(hours=1)) model.weatherers += [evaporation, dispersion, burn, skimmer] renderer = Renderer(images_dir='junk', size=(400, 300)) model.outputters += renderer for o in (gnome_map, sp, rel, et, water, wind, evaporation, dispersion, burn, skimmer, renderer): assert model.contains_object(o.id) for o in initializers: assert model.contains_object(o.id) for o in movers: assert model.contains_object(o.id)