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 test_Water_update_from_dict(): ''' test that the update_from_dict correctly sets fetch and wave_height to None if it is dropped from json payload so user chose compute from wind option. ''' w = Water() json_ = w.serialize() w.fetch = 0.0 w.wave_height = 1.0 json_with_values = w.serialize() w.update_from_dict(json_) assert w.fetch is None assert w.wave_height is None w.update_from_dict(json_with_values) assert w.fetch == 0.0 assert w.wave_height == 1.0
def test_unit_errors(attr, unit): ''' - currently salinity only has psu in there since there is no conversion from psu to ppt, though ppt is a valid unit. This needs to be fixed - similarly, sediment only has mg/l as units. We need to decide if we want more units here ''' w = Water() w.wave_height = 1 w.fetch = 10000 with pytest.raises(InvalidUnitError): w.get(attr, unit) with pytest.raises(InvalidUnitError): w.set(attr, 5, unit)
def test_Water_set(attr, unit): w = Water() w.set(attr, 1.0, unit) assert getattr(w, attr) == 1.0 assert w.units[attr] == unit
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_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 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_exceptions(attr, unit): w = Water() with pytest.raises(InvalidUnitError): w.get(attr, unit) with pytest.raises(InvalidUnitError): w.set(attr, 5, unit)
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_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 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_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_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_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_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 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_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_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 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 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_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_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 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_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_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 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_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 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 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_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_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_serialize_deseriailize(): 'test serialize/deserialize for webapi' e = Evaporation() wind = constant_wind(1., 0) water = Water() json_ = e.serialize() json_['wind'] = wind.serialize() json_['water'] = water.serialize() # deserialize and ensure the dict's are correct d_ = Evaporation.deserialize(json_) assert d_['wind'] == Wind.deserialize(json_['wind']) assert d_['water'] == Water.deserialize(json_['water']) d_['wind'] = wind d_['water'] = water e.update_from_dict(d_) assert e.wind is wind assert e.water is water
def test_serialize_deseriailize(): "test serialize/deserialize for webapi" wind = constant_wind(1.0, 0) water = Water() w = Waves(wind, water) json_ = w.serialize() json_["wind"] = wind.serialize() json_["water"] = water.serialize() # deserialize and ensure the dict's are correct d_ = Waves.deserialize(json_) print "d_" print d_ assert d_["wind"] == Wind.deserialize(json_["wind"]) assert d_["water"] == Water.deserialize(json_["water"]) d_["wind"] = wind d_["water"] = water w.update_from_dict(d_) assert w.wind is wind assert w.water is water