Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def model(sample_model, output_dir):
    model = sample_model['model']
    rel_start_pos = sample_model['release_start_pos']
    rel_end_pos = sample_model['release_end_pos']

    model.cache_enabled = True
    model.uncertain = True

    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)

    release = SpatialRelease(start_position=line_pos,
                             release_time=model.start_time)

    model.spills += Spill(release)
    model.outputters += TrajectoryGeoJsonOutput(output_dir=output_dir)
    model.rewind()

    return model
Ejemplo n.º 3
0
def test_simple_run_with_image_output(tmpdir):
    '''
    Pretty much all this tests is that the model will run and output images
    '''
    images_dir = tmpdir.mkdir('Test_images').strpath

    if os.path.isdir(images_dir):
        shutil.rmtree(images_dir)
    os.mkdir(images_dir)

    start_time = datetime(2012, 9, 15, 12, 0)

    # the land-water map
    gnome_map = gnome.map.MapFromBNA(testdata['MapFromBNA']['testmap'],
                                     refloat_halflife=6)  # hours
    renderer = gnome.outputters.Renderer(testdata['MapFromBNA']['testmap'],
                                         images_dir, size=(400, 300))
    geo_json = TrajectoryGeoJsonOutput(output_dir=images_dir)

    model = Model(time_step=timedelta(minutes=15),
                  start_time=start_time, duration=timedelta(hours=1),
                  map=gnome_map,
                  uncertain=False, cache_enabled=False)

    model.outputters += renderer
    model.outputters += geo_json

    a_mover = SimpleMover(velocity=(1., -1., 0.))
    model.movers += a_mover
    assert len(model.movers) == 1

    N = 10  # a line of ten points
    start_points = np.zeros((N, 3), dtype=np.float64)
    start_points[:, 0] = np.linspace(-127.1, -126.5, N)
    start_points[:, 1] = np.linspace(47.93, 48.1, N)
    # print start_points

    spill = Spill(SpatialRelease(start_position=start_points,
                                 release_time=start_time))

    model.spills += spill
    assert len(model.spills) == 1

    model.start_time = spill.release.release_time

    # image_info = model.next_image()
    num_steps_output = 0
    while True:
        try:
            model.step()
            num_steps_output += 1
        except StopIteration:
            print 'Done with the model run'
            break

    # There is the zeroth step, too.
    calculated_steps = (model.duration.total_seconds() / model.time_step) + 1
    assert num_steps_output == calculated_steps
Ejemplo n.º 4
0
def test_init():
    'simple initialization passes'
    g = TrajectoryGeoJsonOutput()
    assert g.output_dir is None
    assert g.round_to == 4
    assert g.round_data
Ejemplo n.º 5
0
                                   0), datetime(2014, 1, 1, 4, 0)),
            units='kg'),
    Burn(area=100,
         thickness=1,
         active_range=(datetime(2014, 1, 1, 0, 0), datetime(2014, 1, 1, 4, 0)),
         efficiency=.9),
    ChemicalDispersion(fraction_sprayed=.2,
                       active_range=(datetime(2014, 1, 1, 0,
                                              0), datetime(2014, 1, 1, 4, 0)),
                       efficiency=.3),
    # todo: ask Caitlin how to fix
    # movers.RiseVelocityMover(),
    # todo: This is incomplete - no _schema for
    #       SpatialRelease, GeoJson
    # spill.SpatialRelease(datetime.now(), ((0, 0, 0), (1, 2, 0))),
    TrajectoryGeoJsonOutput(),
)


@pytest.mark.parametrize("obj", g_objects)
def test_serial_deserial(saveloc_, obj):
    'test save/load functionality'
    json_ = obj.serialize()
    obj2 = obj.__class__.deserialize(json_)

    assert obj == obj2


@pytest.mark.parametrize("obj", g_objects)
def test_save_load(saveloc_, obj):
    'test save/load functionality'
Ejemplo n.º 6
0
def make_model(uncertain=False, geojson_output=False):
    print 'initializing the model'

    start_time = datetime(2012, 9, 15, 12, 0)
    mapfile = testdata["lis"]["map"]

    gnome_map = MapFromBNA(mapfile, refloat_halflife=6)  # hours

    # # the image output renderer
    # global renderer

    # one hour timestep
    model = Model(start_time=start_time,
                  duration=timedelta(hours=48),
                  time_step=3600,
                  map=gnome_map,
                  uncertain=uncertain,
                  cache_enabled=False)

    print 'adding a spill'
    spill = point_line_release_spill(num_elements=1000,
                                     start_position=(-72.419992, 41.202120,
                                                     0.0),
                                     release_time=start_time,
                                     amount=1000,
                                     substance=test_oil,
                                     units='kg')
    spill.amount_uncertainty_scale = 1.0
    model.spills += spill

    print 'adding a RandomMover:'
    model.movers += RandomMover(diffusion_coef=500000, uncertain_factor=2)

    print 'adding a wind mover:'
    series = np.zeros((5, ), dtype=datetime_value_2d)
    series[0] = (start_time, (20, 45))
    series[1] = (start_time + timedelta(hours=18), (20, 90))
    series[2] = (start_time + timedelta(hours=30), (20, 135))
    series[3] = (start_time + timedelta(hours=42), (20, 180))
    series[4] = (start_time + timedelta(hours=54), (20, 225))

    wind = Wind(timeseries=series, units='m/s', speed_uncertainty_scale=0.05)
    model.movers += WindMover(wind)

    print 'adding a cats mover:'
    c_mover = CatsMover(testdata["lis"]["cats_curr"],
                        tide=Tide(testdata["lis"]["cats_tide"]))
    model.movers += c_mover

    model.environment += c_mover.tide

    print 'adding Weatherers'
    rel_time = model.spills[0].get('release_time')
    skim_start = rel_time + timedelta(hours=4)
    amount = spill.amount
    units = spill.units

    # define skimmer/burn cleanup options
    skimmer = Skimmer(0.3 * amount,
                      units=units,
                      efficiency=0.3,
                      active_start=skim_start,
                      active_stop=skim_start + timedelta(hours=4))
    # 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=.9)
    c_disp = ChemicalDispersion(0.1,
                                efficiency=0.5,
                                active_start=skim_start,
                                active_stop=skim_start + timedelta(hours=1))

    water_env = Water(311.15)
    model.environment += water_env
    model.weatherers += [Evaporation(water_env, wind), c_disp, burn, skimmer]

    print 'adding outputters'
    model.outputters += WeatheringOutput()

    if geojson_output:
        model.outputters += TrajectoryGeoJsonOutput()

    return model