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_dissolution(oil, temp, num_elems, on): ''' Fuel Oil #6 does not exist... ''' et = floating(substance=oil) diss = Dissolution(waves) (sc, time_step) = weathering_data_arrays(diss.array_types, water, element_type=et, num_elements=num_elems)[:2] print 'num spills:', len(sc.spills) print 'spill[0] amount:', sc.spills[0].amount model_time = (sc.spills[0].get('release_time') + timedelta(seconds=time_step)) diss.on = on diss.prepare_for_model_run(sc) diss.initialize_data(sc, sc.num_released) diss.prepare_for_model_step(sc, time_step, model_time) diss.weather_elements(sc, time_step, model_time) if on: print sc._data_arrays assert all(np.isclose(sc._data_arrays['partition_coeff'], 511.445)) assert sc.mass_balance['dissolution'] > 0 print "sc.mass_balance['dissolution']" print sc.mass_balance['dissolution'] else: assert 'dissolution' not in sc.mass_balance
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, element_type=floating(substance=subs))[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_dissolution_k_ow(oil, temp, num_elems, k_ow, on): ''' Here we are testing that the molar averaged oil/water partition coefficient (K_ow) is getting calculated with reasonable values ''' et = floating(substance=oil) diss = Dissolution(waves) (sc, time_step) = weathering_data_arrays(diss.array_types, water, element_type=et, num_elements=num_elems)[:2] print 'num spills:', len(sc.spills) print 'spill[0] amount:', sc.spills[0].amount # we don't want to query the oil database, but get the sample oil assert sc.spills[0].element_type.substance.record.id is None model_time = (sc.spills[0].release_time + timedelta(seconds=time_step)) diss.on = on diss.prepare_for_model_run(sc) diss.initialize_data(sc, sc.num_released) diss.prepare_for_model_step(sc, time_step, model_time) diss.weather_elements(sc, time_step, model_time) assert all(np.isclose(sc._data_arrays['partition_coeff'], k_ow))
def test_dispersion(oil, temp, num_elems, on): ''' Fuel Oil #6 does not exist... ''' disp = NaturalDispersion(waves, water) (sc, time_step) = weathering_data_arrays(disp.array_types, water)[:2] model_time = (sc.spills[0].release_time + timedelta(seconds=time_step)) disp.on = on disp.prepare_for_model_run(sc) disp.prepare_for_model_step(sc, time_step, model_time) disp.weather_elements(sc, time_step, model_time) if on: # print "sc.mass_balance['natural_dispersion']" # print sc.mass_balance['natural_dispersion'] # print "sc.mass_balance['sedimentation']" # print sc.mass_balance['sedimentation'] assert sc.mass_balance['natural_dispersion'] > 0 assert sc.mass_balance['sedimentation'] > 0 else: assert 'natural_dispersion' not in sc.mass_balance assert 'sedimentation' not in sc.mass_balance
def test_dissolution_mass_balance(oil, temp, num_elems, expected_mb, on): ''' Fuel Oil #6 does not exist... ''' et = floating(substance=oil) diss = Dissolution(waves) (sc, time_step) = weathering_data_arrays(diss.array_types, water, element_type=et, num_elements=num_elems)[:2] print 'num spills:', len(sc.spills) print 'spill[0] amount:', sc.spills[0].amount model_time = (sc.spills[0].get('release_time') + timedelta(seconds=time_step)) diss.on = on diss.prepare_for_model_run(sc) diss.initialize_data(sc, sc.num_released) diss.prepare_for_model_step(sc, time_step, model_time) diss.weather_elements(sc, time_step, model_time) if on: assert np.isclose(sc.mass_balance['dissolution'], expected_mb) else: assert 'dissolution' not in sc.mass_balance
def test_dispersion(oil, temp, num_elems, on): ''' Fuel Oil #6 does not exist... ''' et = floating(substance=oil) disp = NaturalDispersion(waves, water) (sc, time_step) = weathering_data_arrays(disp.array_types, water, element_type=et)[:2] model_time = (sc.spills[0].release_time + timedelta(seconds=time_step)) disp.on = on disp.prepare_for_model_run(sc) disp.prepare_for_model_step(sc, time_step, model_time) disp.weather_elements(sc, time_step, model_time) if on: # print "sc.mass_balance['natural_dispersion']" # print sc.mass_balance['natural_dispersion'] # print "sc.mass_balance['sedimentation']" # print sc.mass_balance['sedimentation'] assert sc.mass_balance['natural_dispersion'] > 0 assert sc.mass_balance['sedimentation'] > 0 else: assert 'natural_dispersion' not in sc.mass_balance assert 'sedimentation' not in sc.mass_balance
def test_dispersion_not_active(oil, temp, num_elems): ''' Fuel Oil #6 does not exist... ''' disp = NaturalDispersion(waves, water) (sc, time_step) = \ weathering_data_arrays(disp.array_types, water)[:2] sc.amount = 10000 model_time = (sc.spills[0].release_time + timedelta(seconds=time_step)) disp.prepare_for_model_run(sc) assert np.all(sc.mass_balance['natural_dispersion'] == 0) assert np.all(sc.mass_balance['sedimentation'] == 0) new_model_time = (sc.spills[0].release_time + timedelta(seconds=3600)) disp.active_range = (new_model_time, InfDateTime('inf')) disp.prepare_for_model_step(sc, time_step, model_time) assert np.all(sc.mass_balance['natural_dispersion'] == 0) assert np.all(sc.mass_balance['sedimentation'] == 0) disp.weather_elements(sc, time_step, model_time) assert np.all(sc.mass_balance['natural_dispersion'] == 0) assert np.all(sc.mass_balance['sedimentation'] == 0)
def test_dispersion_not_active(oil, temp, num_elems): ''' Fuel Oil #6 does not exist... ''' disp = NaturalDispersion(waves, water) (sc, time_step) = \ weathering_data_arrays(disp.array_types, water, element_type=floating(substance=oil))[:2] sc.amount = 10000 model_time = (sc.spills[0].release_time + timedelta(seconds=time_step)) disp.prepare_for_model_run(sc) assert np.all(sc.mass_balance['natural_dispersion'] == 0) assert np.all(sc.mass_balance['sedimentation'] == 0) new_model_time = (sc.spills[0].release_time + timedelta(seconds=3600)) disp.active_range = (new_model_time, InfDateTime('inf')) disp.prepare_for_model_step(sc, time_step, model_time) assert np.all(sc.mass_balance['natural_dispersion'] == 0) assert np.all(sc.mass_balance['sedimentation'] == 0) disp.weather_elements(sc, time_step, model_time) assert np.all(sc.mass_balance['natural_dispersion'] == 0) assert np.all(sc.mass_balance['sedimentation'] == 0)
def test_emulsification(oil, temp, num_elems, on): ''' Fuel Oil #6 does not emulsify fixme: this fails for ALASKA NORTH SLOPE - what is it supposed to test? ''' print oil, temp, num_elems, on emul = Emulsification(waves) emul.on = on (sc, time_step) = \ weathering_data_arrays(emul.array_types, water, element_type=floating(substance=oil))[:2] model_time = (sc.spills[0].release_time + timedelta(seconds=time_step)) emul.prepare_for_model_run(sc) # also want a test for a user set value for bulltime or bullwinkle if oil == s_oils[0]: sc['frac_lost'][:] = .31 # sc['frac_lost'][:] = .35 print "sc['frac_lost'][:]" print sc['frac_lost'][:] emul.prepare_for_model_step(sc, time_step, model_time) emul.weather_elements(sc, time_step, model_time) print "sc['frac_water'][:]" print sc['frac_water'][:] if on: assert np.all(sc['frac_lost'] > 0) and np.all(sc['frac_lost'] < 1.0) assert np.all(sc['frac_water'] > 0) and np.all(sc['frac_water'] <= .9) else: assert np.all(sc['frac_water'] == 0) sc['frac_lost'][:] = .2 print "sc['frac_lost'][:]" print sc['frac_lost'][:] emul.prepare_for_model_step(sc, time_step, model_time) emul.weather_elements(sc, time_step, model_time) print "sc['frac_water'][:]" print sc['frac_water'][:] if on: assert np.all(sc['frac_lost'] > 0) and np.all(sc['frac_lost'] < 1.0) assert np.all(sc['frac_water'] > 0) and np.all(sc['frac_water'] <= .9) else: assert np.all(sc['frac_water'] == 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_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_prepare_for_model_run(): 'test sort order for Dissolution weatherer' diss = Dissolution(waves, wind) (sc, _time_step) = weathering_data_arrays(diss.array_types, water)[:2] assert 'partition_coeff' in sc.data_arrays assert 'dissolution' not in sc.mass_balance diss.prepare_for_model_run(sc) assert 'dissolution' in sc.mass_balance
def test_prepare_for_model_run(): 'test sort order for Dissolution weatherer' et = floating(substance='ABU SAFAH') diss = Dissolution(waves) (sc, time_step) = weathering_data_arrays(diss.array_types, water, element_type=et)[:2] assert 'partition_coeff' in sc.data_arrays assert 'dissolution' not in sc.mass_balance diss.prepare_for_model_run(sc) assert 'dissolution' in sc.mass_balance
def test_prepare_for_model_run(): 'test sort order for Dissolution weatherer' et = floating(substance='oil_bahia') diss = Dissolution(waves) # we don't want to query the oil database, but get the sample oil assert et.substance.record.id is None (sc, _time_step) = weathering_data_arrays(diss.array_types, water, element_type=et)[:2] assert 'partition_coeff' in sc.data_arrays assert 'dissolution' not in sc.mass_balance diss.prepare_for_model_run(sc) assert 'dissolution' in sc.mass_balance
def test_dissolution_droplet_size(oil, temp, num_elems, drop_size, on): ''' Here we are testing that the molar averaged oil/water partition coefficient (K_ow) is getting calculated with reasonable values ''' et = floating(substance=oil) disp = NaturalDispersion(waves, water) diss = Dissolution(waves) (sc, time_step) = weathering_data_arrays(diss.array_types, water, element_type=et, num_elements=num_elems)[:2] print 'num_spills:', len(sc.spills) print 'spill[0] amount:', sc.spills[0].amount, sc.spills[0].units model_time = (sc.spills[0] .release_time + timedelta(seconds=time_step)) print 'model_time = ', model_time print 'time_step = ', time_step # we don't want to query the oil database, but get the sample oil assert sc.spills[0].element_type.substance.record.id is None disp.on = on diss.on = on disp.prepare_for_model_run(sc) diss.prepare_for_model_run(sc) disp.initialize_data(sc, sc.num_released) diss.initialize_data(sc, sc.num_released) for i in range(3): disp.prepare_for_model_step(sc, time_step, model_time) diss.prepare_for_model_step(sc, time_step, model_time) disp.weather_elements(sc, time_step, model_time) diss.weather_elements(sc, time_step, model_time) print 'droplet_avg_size:', sc._data_arrays['droplet_avg_size'] assert np.allclose(sc._data_arrays['droplet_avg_size'], drop_size[i])
def test_dissolution_droplet_size(oil, temp, num_elems, drop_size, on): ''' Here we are testing that the molar averaged oil/water partition coefficient (K_ow) is getting calculated with reasonable values ''' et = floating(substance=oil) disp = NaturalDispersion(waves, water) diss = Dissolution(waves, wind) (sc, time_step) = weathering_data_arrays(diss.array_types, water, element_type=et, num_elements=num_elems)[:2] print 'num_spills:', len(sc.spills) print 'spill[0] amount:', sc.spills[0].amount, sc.spills[0].units model_time = (sc.spills[0] .release_time + timedelta(seconds=time_step)) print 'model_time = ', model_time print 'time_step = ', time_step # we don't want to query the oil database, but get the sample oil assert sc.spills[0].element_type.substance.record.id is None disp.on = on diss.on = on disp.prepare_for_model_run(sc) diss.prepare_for_model_run(sc) disp.initialize_data(sc, sc.num_released) diss.initialize_data(sc, sc.num_released) for i in range(3): disp.prepare_for_model_step(sc, time_step, model_time) diss.prepare_for_model_step(sc, time_step, model_time) disp.weather_elements(sc, time_step, model_time) diss.weather_elements(sc, time_step, model_time) print 'droplet_avg_size:', sc._data_arrays['droplet_avg_size'] assert np.allclose(sc._data_arrays['droplet_avg_size'], drop_size[i])
def test_evaporation(oil, temp, num_elems, on): ''' still working on tests .. ''' et = floating(substance=oil) time_step = 15. * 60 evap = Evaporation(Water(), wind=constant_wind(1., 0)) evap.on = on sc = weathering_data_arrays(evap.array_types, evap.water, time_step, et)[0] 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) init_mass = sc['mass_components'].copy() evap.weather_elements(sc, time_step, model_time) if on: assert np.all(sc['frac_lost'] > 0) and np.all(sc['frac_lost'] < 1.0) # all elements experience the same evaporation assert np.all(sc['frac_lost'][0] == sc['frac_lost']) for spill in sc.spills: mask = sc.get_spill_mask(spill) if on: assert np.all(sc['evap_decay_constant'][mask, :] < 0.0) else: assert np.all(sc['evap_decay_constant'][mask, :] == 0.0) print '\nevap_decay_const', sc['evap_decay_constant'] print 'frac_lost', sc['frac_lost'] if on: assert sc.mass_balance['evaporated'] > 0.0 print 'total evaporated', sc.mass_balance['evaporated'] else: assert 'evaporated' not in sc.mass_balance assert np.all(sc['mass_components'] == init_mass)
def test_evaporation(oil, temp, num_elems, on): ''' still working on tests .. ''' et = floating(substance=oil) time_step = 15. * 60 evap = Evaporation(Water(), wind=constant_wind(1., 0)) evap.on = on sc = weathering_data_arrays(evap.array_types, evap.water, time_step, et)[0] 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) init_mass = sc['mass_components'].copy() evap.weather_elements(sc, time_step, model_time) if on: assert np.all(sc['frac_lost'] > 0) and np.all(sc['frac_lost'] < 1.0) # all elements experience the same evaporation assert np.all(sc['frac_lost'][0] == sc['frac_lost']) for spill in sc.spills: mask = sc.get_spill_mask(spill) if on: assert np.all(sc['evap_decay_constant'][mask, :] < 0.0) else: assert np.all(sc['evap_decay_constant'][mask, :] == 0.0) print '\nevap_decay_const', sc['evap_decay_constant'] print 'frac_lost', sc['frac_lost'] if on: assert sc.mass_balance['evaporated'] > 0.0 print 'total evaporated', sc.mass_balance['evaporated'] else: assert 'evaporated' not in sc.mass_balance assert np.all(sc['mass_components'] == init_mass)
def test_dissolution_not_active(oil, temp, num_elems): ''' Fuel Oil #6 does not exist... ''' diss = Dissolution(waves) et = floating(substance=oil) (sc, time_step) = weathering_data_arrays(diss.array_types, water, element_type=et)[:2] sc.amount = 10000 model_time = (sc.spills[0].get('release_time') + timedelta(seconds=time_step)) diss.prepare_for_model_run(sc) new_model_time = (sc.spills[0].get('release_time') + timedelta(seconds=3600)) diss.active_start = new_model_time diss.prepare_for_model_step(sc, time_step, model_time) diss.weather_elements(sc, time_step, model_time) assert np.all(sc.mass_balance['dissolution'] == 0)
def test_dissolution_k_ow(oil, temp, num_elems, k_ow, on): ''' Here we are testing that the molar averaged oil/water partition coefficient (K_ow) is getting calculated with reasonable values Note: for now droplets are calculated in natural dispersion so natural dispersion is required for the dissolution algorithm ''' diss = Dissolution(waves, wind) disp = NaturalDispersion(waves, water) (sc, time_step) = weathering_data_arrays(diss.array_types, water, substance=oil, num_elements=num_elems)[:2] print 'num spills:', len(sc.spills) print 'spill[0] amount:', sc.spills[0].amount # we don't want to query the oil database, but get the sample oil #assert sc.spills[0].substance.record.id is None model_time = (sc.spills[0].release_time + timedelta(seconds=time_step)) disp.on = on diss.on = on disp.prepare_for_model_run(sc) diss.prepare_for_model_run(sc) disp.initialize_data(sc, sc.num_released) diss.initialize_data(sc, sc.num_released) disp.prepare_for_model_step(sc, time_step, model_time) diss.prepare_for_model_step(sc, time_step, model_time) disp.weather_elements(sc, time_step, model_time) diss.weather_elements(sc, time_step, model_time) assert all(np.isclose(sc._data_arrays['partition_coeff'], k_ow))
def test_dispersion_not_active(oil, temp, num_elems): ''' Fuel Oil #6 does not exist... ''' disp = NaturalDispersion(waves, water) (sc, time_step) = \ weathering_data_arrays(disp.array_types, water, element_type=floating(substance=oil))[:2] sc.amount = 10000 model_time = (sc.spills[0].get('release_time') + timedelta(seconds=time_step)) disp.prepare_for_model_run(sc) new_model_time = (sc.spills[0].get('release_time') + timedelta(seconds=3600)) disp.active_start = new_model_time disp.prepare_for_model_step(sc, time_step, model_time) disp.weather_elements(sc, time_step, model_time) assert np.all(sc.mass_balance['natural_dispersion'] == 0) assert np.all(sc.mass_balance['sedimentation'] == 0)
def test_dissolution_mass_balance(oil, temp, wind_speed, num_elems, expected_mb, on): ''' Test a single dissolution step. - for this, we need a dispersion weatherer to give us a droplet size distribution. Fuel Oil #6 does not exist... ''' waves = build_waves_obj(wind_speed, 'knots', 270, temp) wind = waves.wind water = waves.water disp = NaturalDispersion(waves, water) diss = Dissolution(waves, wind) all_array_types = diss.array_types.union(disp.array_types) (sc, time_step) = weathering_data_arrays(all_array_types, water, num_elements=num_elems, units='kg', amount_per_element=1.0 )[:2] print 'time_step: {}'.format(time_step) print 'num spills:', len(sc.spills) print 'spill[0] amount: {} {}'.format(sc.spills[0].amount, sc.spills[0].units) print 'temperature = ', temp print 'wind = ', print '\n'.join(['\t{} {}'.format(ts[1][0], waves.wind.units) for ts in waves.wind.timeseries]) print # we don't want to query the oil database, but get the sample oil #assert sc.spills[0].substance.record.id is None initial_amount = sc.spills[0].amount model_time = (sc.spills[0].release_time + timedelta(seconds=time_step)) disp.on = on disp.prepare_for_model_run(sc) disp.initialize_data(sc, sc.num_released) diss.on = on diss.prepare_for_model_run(sc) diss.initialize_data(sc, sc.num_released) disp.prepare_for_model_step(sc, time_step, model_time) diss.prepare_for_model_step(sc, time_step, model_time) disp.weather_elements(sc, time_step, model_time) diss.weather_elements(sc, time_step, model_time) if on: print ('fraction dissolved: {}' .format(sc.mass_balance['dissolution'] / initial_amount) ) print ('fraction dissolved: {:.2%}' .format(sc.mass_balance['dissolution'] / initial_amount) ) print sc.mass_balance['dissolution'], expected_mb assert np.isclose(sc.mass_balance['dissolution'], expected_mb, rtol=1e-4) else: assert 'dissolution' not in sc.mass_balance
def test_dissolution_mass_balance(oil, temp, wind_speed, num_elems, expected_mb, on): ''' Test a single dissolution step. - for this, we need a dispersion weatherer to give us a droplet size distribution. Fuel Oil #6 does not exist... ''' et = floating(substance=oil) waves = build_waves_obj(wind_speed, 'knots', 270, temp) water = waves.water disp = NaturalDispersion(waves, water) diss = Dissolution(waves) all_array_types = diss.array_types.union(disp.array_types) (sc, time_step) = weathering_data_arrays(all_array_types, water, element_type=et, num_elements=num_elems, units='kg', amount_per_element=1.0 )[:2] print 'time_step: {}'.format(time_step) print 'num spills:', len(sc.spills) print 'spill[0] amount: {} {}'.format(sc.spills[0].amount, sc.spills[0].units) print 'temperature = ', temp print 'wind = ', print '\n'.join(['\t{} {}'.format(ts[1][0], waves.wind.units) for ts in waves.wind.timeseries]) print # we don't want to query the oil database, but get the sample oil assert sc.spills[0].element_type.substance.record.id is None initial_amount = sc.spills[0].amount model_time = (sc.spills[0].release_time + timedelta(seconds=time_step)) disp.on = on disp.prepare_for_model_run(sc) disp.initialize_data(sc, sc.num_released) diss.on = on diss.prepare_for_model_run(sc) diss.initialize_data(sc, sc.num_released) disp.prepare_for_model_step(sc, time_step, model_time) diss.prepare_for_model_step(sc, time_step, model_time) disp.weather_elements(sc, time_step, model_time) diss.weather_elements(sc, time_step, model_time) if on: print ('fraction dissolved: {}' .format(sc.mass_balance['dissolution'] / initial_amount) ) print ('fraction dissolved: {:.2%}' .format(sc.mass_balance['dissolution'] / initial_amount) ) print sc.mass_balance['dissolution'], expected_mb assert np.isclose(sc.mass_balance['dissolution'], expected_mb) else: assert 'dissolution' not in sc.mass_balance