Esempio n. 1
0
def test_serialize_deseriailize():
    'test serialize/deserialize for webapi'
    wind = constant_wind(15., 0)
    waves = Waves(wind, Water())
    e = Emulsification(waves)
    json_ = e.serialize()
    json_['waves'] = waves.serialize()

    # deserialize and ensure the dict's are correct
    d_ = Emulsification.deserialize(json_)
    assert d_['waves'] == Waves.deserialize(json_['waves'])
    d_['waves'] = waves
    e.update_from_dict(d_)
    assert e.waves is waves
Esempio n. 2
0
 def mk_objs(cls, sample_model_fcn2):
     model = sample_model_weathering2(sample_model_fcn2, test_oil, 333.0)
     model.set_make_default_refs(True)
     model.environment += [waves, wind, water]
     model.weatherers += Evaporation(wind=wind, water=water)
     model.weatherers += Emulsification(waves=waves)
     return (model.spills.items()[0], model)
Esempio n. 3
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
Esempio n. 4
0
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)
Esempio n. 5
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_full_run_emul_not_active(sample_model_fcn):
    'no water/wind/waves object and no evaporation'
    model = sample_model_weathering(sample_model_fcn, 'oil_crude')
    model.weatherers += Emulsification(on=False)
    model.outputters += WeatheringOutput()
    for step in model:
        '''
        if no weatherers, then no weathering output - need to add on/off
        switch to WeatheringOutput
        '''
        assert 'water_content' not in step['WeatheringOutput']
        assert ('time_stamp' in step['WeatheringOutput'])

        print("Completed step: {0}".format(step['step_num']))
def test_serialize_deseriailize():
    'test serialize/deserialize for webapi'
    wind = constant_wind(15., 0)
    waves = Waves(wind, Water())
    e = Emulsification(waves)
    json_ = e.serialize()
    json_['waves'] = waves.serialize()

    # deserialize and ensure the dict's are correct
    d_ = Emulsification.deserialize(json_)
    assert d_['waves'] == Waves.deserialize(json_['waves'])
    d_['waves'] = waves
    e.update_from_dict(d_)
    assert e.waves is waves
def test_full_run(sample_model_fcn, oil, temp):
    '''
    test emulsification outputs post step for a full run of model. Dump json
    for 'weathering_model.json' in dump directory
    '''
    model = sample_model_weathering2(sample_model_fcn, oil, temp)
    model.environment += [Waves(), wind, Water(temp)]
    model.weatherers += Evaporation()
    model.weatherers += Emulsification()
    model.set_make_default_refs(True)

    for step in model:
        for sc in model.spills.items():
            # need or condition to account for water_content = 0.9000000000012
            # or just a little bit over 0.9
            assert (sc.mass_balance['water_content'] <= .9
                    or np.isclose(sc.mass_balance['water_content'], 0.9))
            print("Water fraction: {0}".format(
                sc.mass_balance['water_content']))
            print "Completed step: {0}\n".format(step['step_num'])
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)[: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_evap'][:] = .31

    # sc['frac_evap'][:] = .35
    print "sc['frac_evap'][:]"
    print sc['frac_evap'][:]

    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_evap'] > 0) and np.all(sc['frac_evap'] < 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_evap'][:] = .2
    print "sc['frac_evap'][:]"
    print sc['frac_evap'][:]

    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_evap'] > 0) and np.all(sc['frac_evap'] < 1.0)
        assert np.all(sc['frac_water'] > 0) and np.all(sc['frac_water'] <= .9)
    else:
        assert np.all(sc['frac_water'] == 0)
Esempio n. 10
0
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2015, 5, 14, 0, 0)

    # 1 day of data in file
    # 1/2 hr in seconds
    model = Model(start_time=start_time,
                  duration=timedelta(days=1.75),
                  time_step=60 * 60,
                  uncertain=True)

    #     mapfile = get_datafile(os.path.join(base_dir, './ak_arctic.bna'))
    #
    #     print 'adding the map'
    #     model.map = MapFromBNA(mapfile, refloat_halflife=1)  # seconds
    #
    #     # draw_ontop can be 'uncertain' or 'forecast'
    #     # 'forecast' LEs are in black, and 'uncertain' are in red
    #     # default is 'forecast' LEs draw on top
    #     renderer = Renderer(mapfile, images_dir, size=(800, 600),
    #                         output_timestep=timedelta(hours=2),
    #                         draw_ontop='forecast')
    #
    #     print 'adding outputters'
    #     model.outputters += renderer

    model.outputters += WeatheringOutput()

    netcdf_file = os.path.join(base_dir, 'script_weatherers.nc')
    scripting.remove_netcdf(netcdf_file)
    model.outputters += NetCDFOutput(netcdf_file,
                                     which_data='all',
                                     output_timestep=timedelta(hours=1))

    print 'adding a spill'
    # for now subsurface spill stays on initial layer
    # - will need diffusion and rise velocity
    # - wind doesn't act
    # - start_position = (-76.126872, 37.680952, 5.0),
    end_time = start_time + timedelta(hours=24)
    spill = point_line_release_spill(
        num_elements=100,
        start_position=(-164.791878561, 69.6252597267, 0.0),
        release_time=start_time,
        end_release_time=end_time,
        amount=1000,
        substance='ALASKA NORTH SLOPE (MIDDLE PIPELINE)',
        units='bbl')

    # set bullwinkle to .303 to cause mass goes to zero bug at 24 hours (when continuous release ends)
    spill.element_type._substance._bullwinkle = .303
    model.spills += spill

    print 'adding a RandomMover:'
    #model.movers += RandomMover(diffusion_coef=50000)

    print 'adding a wind mover:'

    series = np.zeros((2, ), dtype=datetime_value_2d)
    series[0] = (start_time, (20, 0))
    series[1] = (start_time + timedelta(hours=23), (20, 0))

    wind2 = Wind(timeseries=series, units='knot')

    w_mover = WindMover(wind)
    model.movers += w_mover

    print 'adding weatherers and cleanup options:'

    # define skimmer/burn cleanup options
    skim1_start = start_time + timedelta(hours=15.58333)
    skim2_start = start_time + timedelta(hours=16)
    units = spill.units
    skimmer1 = Skimmer(80,
                       units=units,
                       efficiency=0.36,
                       active_start=skim1_start,
                       active_stop=skim1_start + timedelta(hours=8))
    skimmer2 = Skimmer(120,
                       units=units,
                       efficiency=0.2,
                       active_start=skim2_start,
                       active_stop=skim2_start + timedelta(hours=12))

    burn_start = start_time + timedelta(hours=36)
    burn = Burn(1000., .1, active_start=burn_start, efficiency=.2)

    chem_start = start_time + timedelta(hours=24)
    c_disp = ChemicalDispersion(0.5,
                                efficiency=0.4,
                                active_start=chem_start,
                                active_stop=chem_start + timedelta(hours=8))

    model.environment += [Water(280.928), wind, waves]

    model.weatherers += Evaporation(water, wind)
    model.weatherers += Emulsification(waves)
    model.weatherers += NaturalDispersion(waves, water)
    model.weatherers += skimmer1
    model.weatherers += skimmer2
    model.weatherers += burn
    model.weatherers += c_disp

    return model
Esempio n. 11
0
def test_emulsification(oil, temp, num_elems, on):
    '''
    Fuel Oil #6 does not emulsify
    '''
    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].get('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)
Esempio n. 12
0
    def make_model():
        print ('initializing the model')

        #print (start_date)

        start_time = startday

        model = Model(start_time=start_time, duration=timedelta(days=30), 
                      #weathering_substeps = 6,
                      time_step=24 * 3600,
                      uncertain=False)

        mapfile = get_datafile(os.path.join(base_dir,'gulf.bna'))

        #mapfile='gulf.bna'

        print ('adding the map')
        model.map = MapFromBNA(mapfile, refloat_halflife=6)  # hours

        #
        # Add the outputters -- render to images, and save out as netCDF
        #

        

        

        print ("adding shapefile output")

         # with open("Result {}".format(spill_num), "w") as fp:
         # fp.write("text")
        
        dir_name = os.path.join (base_dir, season, str(position), "Spillnum {}".format(spill_num))
        
        if not os.path.exists(dir_name):
            os.makedirs(dir_name)
        
        #os.makedirs(dir_name, exist_ok =True)

        for i in range(1, 31, 1):
            model.outputters += ShapeOutput(os.path.join(dir_name, 'gnome_result {id}'.format(id=i)),
                                        zip_output=False,
                                        output_timestep=timedelta(days=i)) 
        images_dir = os.path.join(dir_name, 'image')
        # print 'adding renderer'
        # model.outputters += Renderer(mapfile,
        #                         images_dir,
        #                         image_size=(800, 600))
                                 
        # print ('adding renderer')
        # dir_image = os.path.join(dir_name)
        # model.outputters += Renderer(mapfile,
        #                            dir_image,
        #                             size=(800, 600))
                              

        #
        # Set up the movers:
        #

        print ('adding a RandomMover:')
        model.movers += RandomMover(diffusion_coef=10000)

        print ('adding a simple wind mover:')
    

        
        wind_file = get_datafile(os.path.join(base_dir, 'ECMWF.nc'))
        model.movers += GridWindMover(wind_file)

                                                  

        print ('adding a current mover:')

        # # this is NEMO currents

        curr_file = get_datafile(os.path.join(base_dir, Currents))
        model.movers += GridCurrentMover(curr_file,
                                         num_method='Euler');


        
        # # Add some spills (sources of elements)
        

        print ('adding one spill')

        
       
        spill = point_line_release_spill(num_elements=1000,
                                                 amount=  3200000000 * spilldur , units='grams',
                                                 start_position = position,
                                                 release_time = start_time,
                                                 substance = (sub))
        model.spills += spill

        ####### open excel file 
        print ('adding Excel file')
      
        workbook = xlsxwriter.Workbook(os.path.join(dir_name, 'Result {}_{}.xlsx'.format(spill_num, position)))
        worksheet = workbook.add_worksheet () 
        a = ((spilldur*3200)**(-0.3))*0.000069
        worksheet.write ('A1', a)
        workbook.close()

        
        print ('adding weatherers and cleanup options:')

        model.environment += [water,wind,waves]
        model.weatherers += Evaporation()
        model.weatherers += Emulsification()
        model.weatherers += NaturalDispersion()
        print ('model full run:')
        model.full_run()
        return model
Esempio n. 13
0
    def make_model(images_dir=os.path.join(base_dir, 'images')):
        print ('initializing the model')

        #print (start_date)

        start_time = start_date

        model = Model(start_time=start_time, duration=timedelta(days=30), 
                      #weathering_substeps = 6,
                      time_step=24 * 3600,
                      uncertain=True)

        mapfile = get_datafile(os.path.join(base_dir,'gulf.bna'))

        #mapfile='gulf.bna'

        print ('adding the map')
        model.map = MapFromBNA(mapfile, refloat_halflife=6)  # hours

        #
        # Add the outputters -- render to images, and save out as netCDF
        #

        print ('adding renderer')
        #model.outputters += Renderer(mapfile,
                                    # images_dir,
                                     #size=(800, 600),
                                     #draw_back_to_fore=True)

        #print ("adding netcdf output")
     #   netcdf_output_file = os.path.join(base_dir,'gulf_output.nc')
      #scripting.remove_netcdf(netcdf_output_file)
    #    model.outputters += NetCDFOutput(netcdf_output_file, which_data='all',
     #                                    output_timestep=timedelta(hours=24))


        print ("adding shapefile output")

        

        # with open("Result {}".format(spill_num), "w") as fp:
            # fp.write("text")
        
        dir_name = "Result {}".format(spill_num)
        if not os.path.exists(dir_name):
            os.mkdir(dir_name)

        for b in range(1, 31, 1):
            model.outputters += ShapeOutput(os.path.join(base_dir, dir_name, 'gnome_result{id}'.format(id=b)),
                                        zip_output=False,
                                        output_timestep=timedelta(days=b))    

        #
        # Set up the movers:
        #

        print ('adding a RandomMover:')
        model.movers += RandomMover(diffusion_coef=10000)

        print ('adding a simple wind mover:')
    #    model.movers += constant_wind_mover(5, 315, units='m/s')

        wind_file = get_datafile(os.path.join(base_dir, 'nc3ECMWF2016.nc'))
        model.movers += GridWindMover(wind_file)
                                         

        print ('adding a current mover:')

        # # this is NEMO currents

        curr_file = get_datafile(os.path.join(base_dir, 'current2016nc3.nc'))
        model.movers += GridCurrentMover(curr_file,
                                         num_method='Euler');


            # #
        # # Add some spills (sources of elements)
        # #

        print ('adding one spill')

        spill = point_line_release_spill(num_elements=1000,
                                                 amount=4000 * spilldur, units='m^3',
                                                 start_position = position,
                                                 release_time = start_time,
                                                 substance =( u'BAHRGANSAR, OIL & GAS'))
        model.spills += spill


        
        print ('adding weatherers and cleanup options:')

        model.environment += [water, wind, waves]

        model.weatherers += Evaporation(water, wind)
        model.weatherers += Emulsification(waves)
        model.weatherers += NaturalDispersion(waves, water)
        model.full_run()
        return model