def test_serialize_deseriailize(): 'test serialize/deserialize for webapi' wind = constant_wind(1., 0) water = Water() w = Waves(wind, water) json_ = w.serialize() # deserialize and ensure the dict's are correct w2 = Waves.deserialize(json_) assert w2.wind == Wind.deserialize(json_['wind']) assert w2.water == Water.deserialize(json_['water']) assert w == w2
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_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_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 (2, 4, 3, 1, 0)] exp_order = [weatherers[ix] for ix in (4, 2, 3, 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(2, burn) model.weatherers += exp_order[2] # 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 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, 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 test_update_from_dict(): 'wind_json only used here so take it out of conftest' wind_json = {'obj_type': 'gnome.environment.Wind', 'description': 'update_description', 'latitude': 90, 'longitude': 90, 'updated_at': '2014-03-26T14:52:45.385126', 'source_type': u'manual', 'source_id': u'unknown', 'timeseries': [('2012-11-06T20:10:00', (1.0, 0.0)), ('2012-11-06T20:11:00', (1.0, 45.0)), ('2012-11-06T20:12:00', (1.0, 90.0)), ('2012-11-06T20:13:00', (1.0, 120.0)), ('2012-11-06T20:14:00', (1.0, 180.0)), ('2012-11-06T20:15:00', (1.0, 270.0))], 'units': 'knots', 'json_': u'webapi' } wind = constant_wind(1.0, 45.0, 'meter per second') # following tests fails because unit conversion causes some # rounding errors. Look into this more carefully. # not_changed = wind.deserialize(wind.serialize('webapi')) # assert not wind.update_from_dict(not_changed) d_wind = wind.deserialize(wind_json) updated = wind.update_from_dict(d_wind) assert updated new_w = wind.serialize('webapi') # since json could be a subset of state, check equality for ones that were # updated for key in wind_json: if key != 'obj_type': assert new_w[key] == wind_json[key]
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 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_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 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=water, wind=wind) et = model.spills[0].element_type 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 test_update_from_dict(self): ''' test that the update_from_dict correctly sets efficiency to None if it is dropped from json payload if user chose compute from wind ''' self.burn.wind = constant_wind(5, 0) json_ = self.burn.serialize() assert self.burn.efficiency is not None del json_['efficiency'] dict_ = Burn.deserialize(json_) dict_['wind'] = self.burn.wind assert 'wind' in dict_ self.burn.update_from_dict(dict_) assert self.burn.efficiency is None json_['efficiency'] = .4 # make sure None for wind doesn't break it dict_['wind'] = None dict_ = Burn.deserialize(json_) self.burn.update_from_dict(dict_) assert self.burn.efficiency == json_['efficiency'] # update area/thickness st = self.burn.active_stop self.burn.thickness *= 2 assert self.burn.active_stop > st # burn duration just depents on thickness - not area st = self.burn.active_stop self.burn.area *= 2 assert self.burn.active_stop == st
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_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_update_from_dict(): 'wind_json only used here so take it out of conftest' wind_json = { 'obj_type': 'gnome.environment.Wind', 'description': 'update_description', 'latitude': 90., 'longitude': 90., 'updated_at': '2014-03-26T14:52:45.385126', 'source_type': u'manual', 'source_id': u'unknown', 'timeseries': [('2012-11-06T20:10:00', (1.0, 0.0)), ('2012-11-06T20:11:00', (1.0, 45.0)), ('2012-11-06T20:12:00', (1.0, 90.0)), ('2012-11-06T20:13:00', (1.0, 120.0)), ('2012-11-06T20:14:00', (1.0, 180.0)), ('2012-11-06T20:15:00', (1.0, 270.0))], 'units': 'knots', 'json_': u'webapi' } wind = constant_wind(1.0, 45.0, 'meter per second') updated = wind.update_from_dict(wind_json) assert wind.description == 'update_description' assert wind.source_type == 'manual'
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 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 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 test_full_run(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(), oil, temp, 1) model.environment += [Water(temp), constant_wind(1., 0)] model.weatherers += [Evaporation(model.environment[-2], model.environment[-1])] released = 0 init_rho = model.spills[0].get('substance').get_density(temp) init_vis = model.spills[0].get('substance').get_viscosity(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_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_constant_wind_bounds(): """ tests that a constan_wind returns the limit bounds """ wind = constant_wind(10, 45, 'knots') assert wind.data_start == wind.data_stop
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_serialize_deserialize(): 'test serialize/deserialize for webapi' wind = constant_wind(1., 0) av = RunningAverage(wind) json_ = av.serialize() # deserialize and ensure the dict's are correct d_av = RunningAverage.deserialize(json_) assert d_av == av
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_constant_wind_bounds(): """ tests that a constan_wind returns the limit bounds """ wind = constant_wind(10, 45, 'knots') assert wind.data_start == InfDateTime("-inf") assert wind.data_stop == InfDateTime("inf")
def test_init(): 'test sort order for Dissolution weatherer' 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 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 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)
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_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)
def test_get_emulsification_wind_with_wave_height(): wind = constant_wind(3., 0) water = Water() water.wave_height = 2.0 w = Waves(wind, water) print w.get_value(start_time) print w.get_emulsification_wind(start_time) # input wave height should hav overwhelmed assert w.get_emulsification_wind(start_time) > 3.0
def test_get_emulsification_wind_with_wave_height2(): wind = constant_wind(10., 0) water = Water() water.wave_height = 2.0 w = Waves(wind, water) print w.get_value(start_time) print w.get_emulsification_wind(start_time) # input wave height should not have overwhelmed wind speed assert w.get_emulsification_wind(start_time) == 10.0
def test_av_from_constant_wind(): ''' test constant wind gives constant running average ''' wind = constant_wind(1., 270.) av = RunningAverage(wind) # print "wind.ossm.timeseries" # print wind.ossm.timeseries[:] # print "av.ossm.timeseries" # print av.ossm.timeseries[:] assert av.ossm.timeseries[0] == wind.ossm.timeseries[0]
def test_evaporation_no_wind(): evap = Evaporation(Water(), wind=constant_wind(0., 0)) (sc, time_step) = weathering_data_arrays(evap.array_types, evap.water)[:2] model_time = (sc.spills[0].release_time + timedelta(seconds=time_step)) evap.prepare_for_model_run(sc) evap.prepare_for_model_step(sc, time_step, model_time) evap.weather_elements(sc, time_step, model_time) for spill in sc.spills: mask = sc.get_spill_mask(spill) assert np.all(sc['evap_decay_constant'][mask, :] < 0.0)
def test_serialize_deserialize(): 'test serialize/deserialize for webapi' wind = constant_wind(1., 0) av = RunningAverage(wind) json_ = av.serialize() json_['wind'] = wind.serialize() # deserialize and ensure the dict's are correct d_av = RunningAverage.deserialize(json_) assert d_av['wind'] == Wind.deserialize(json_['wind']) d_av['wind'] = wind av.update_from_dict(d_av) assert av.wind is wind
def test_evaporation_no_wind(): evap = Evaporation(Water(), wind=constant_wind(0., 0)) (sc, time_step) = weathering_data_arrays(evap.array_types, evap.water)[:2] model_time = (sc.spills[0].get('release_time') + timedelta(seconds=time_step)) evap.prepare_for_model_run(sc) evap.prepare_for_model_step(sc, time_step, model_time) evap.weather_elements(sc, time_step, model_time) for spill in sc.spills: mask = sc.get_spill_mask(spill) assert np.all(sc['evap_decay_constant'][mask, :] < 0.0)
def test_serialize_deseriailize(): 'test serialize/deserialize for webapi' wind = constant_wind(15., 0) waves = Waves(wind, Water()) e = NaturalDispersion(waves) json_ = e.serialize() json_['waves'] = waves.serialize() # deserialize and ensure the dict's are correct d_ = NaturalDispersion.deserialize(json_) assert d_['waves'] == Waves.deserialize(json_['waves']) d_['waves'] = waves e.update_from_dict(d_) assert e.waves is waves
def test_callback_add_weather(): ''' Test callback when weatherer is added ''' model = Model() water = Water() wind = constant_wind(1, 30) assert len(model.environment) == 0 model.weatherers += Evaporation(water, wind) # wind and water added to environment collection assert len(model.environment) == 2 assert wind in model.environment assert water in model.environment
def test_sort_order(): 'test sort order for Dissolution weatherer' wind = constant_wind(15., 0) waves = Waves(wind, Water()) diss = Dissolution(waves, wind) disp = NaturalDispersion(waves=waves, water=waves.water) weathering_data = WeatheringData(water=waves.water) # dissolution is dependent upon droplet distribution generated by # natural dispersion assert weatherer_sort(disp) < weatherer_sort(diss) # dissolution needs to happen before we treat our weathering data assert weatherer_sort(diss) < weatherer_sort(weathering_data)
def test_windage_index(): """ A very simple test to make sure windage is set for the correct sc if staggered release """ sc = SpillContainer() rel_time = datetime(2013, 1, 1, 0, 0) timestep = 30 for i in range(2): spill = point_line_release_spill(num_elements=5, start_position=(0., 0., 0.), release_time=rel_time + i * timedelta(hours=1), element_type=floating(windage_range=(i * .01 + .01, i * .01 + .01), windage_persist=900) ) sc.spills.add(spill) windage = {'windages': array_types.windages, 'windage_range': array_types.windage_range, 'windage_persist': array_types.windage_persist} sc.prepare_for_model_run(array_types=windage) sc.release_elements(timestep, rel_time) wm = WindMover(constant_wind(5, 0)) wm.prepare_for_model_step(sc, timestep, rel_time) wm.model_step_is_done() # need this to toggle _windage_is_set_flag def _check_index(sc): ''' internal function for doing the test after windage is set - called twice so made a function ''' # only 1st sc is released for sp in sc.spills: mask = sc.get_spill_mask(sp) if np.any(mask): assert np.all(sc['windages'][mask] == (sp.element_type.initializers["windages"] .windage_range[0])) # only 1st spill is released _check_index(sc) # 1st ASSERT sc.release_elements(timestep, rel_time + timedelta(hours=1)) wm.prepare_for_model_step(sc, timestep, rel_time) _check_index(sc) # 2nd ASSERT
def test_constant_wind(): """ tests the utility function for creating a constant wind """ wind = constant_wind(10, 45, 'knots') dt = datetime(2013, 1, 10, 12, 0) assert np.allclose(wind.get_wind_data(datetime=dt, units='knots')[0][1], (10, 45)) dt = datetime(2013, 1, 10, 12, 0) assert np.allclose(wind.get_wind_data(datetime=dt, units='knots')[0][1], (10, 45)) dt = datetime(2013, 1, 10, 12, 0) assert np.allclose(wind.get_wind_data(datetime=dt, units='knots')[0][1], (10, 45))