Esempio n. 1
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 (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
Esempio n. 2
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
Esempio n. 3
0
def test_simple_run_rewind():
    '''
    Pretty much all this tests is that the model will run
    and the seed is set during first run, then set correctly
    after it is rewound and run again
    '''

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

    model = Model()

    model.map = gnome.map.GnomeMap()
    a_mover = SimpleMover(velocity=(1., 2., 0.))

    model.movers += a_mover
    assert len(model.movers) == 1

    spill = point_line_release_spill(num_elements=10,
                                     start_position=(0., 0., 0.),
                                     release_time=start_time)

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

    # model.add_spill(spill)

    model.start_time = spill.release.release_time

    # test iterator
    for step in model:
        print 'just ran time step: %s' % model.current_time_step
        assert step['step_num'] == model.current_time_step

    pos = np.copy(model.spills.LE('positions'))

    # rewind and run again:
    print 'rewinding'
    model.rewind()

    # test iterator is repeatable
    for step in model:
        print 'just ran time step: %s' % model.current_time_step
        assert step['step_num'] == model.current_time_step

    assert np.all(model.spills.LE('positions') == pos)
Esempio n. 4
0
def test_simple_run_rewind():
    '''
    Pretty much all this tests is that the model will run
    and the seed is set during first run, then set correctly
    after it is rewound and run again
    '''

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

    model = Model()

    model.map = gnome.map.GnomeMap()
    a_mover = SimpleMover(velocity=(1., 2., 0.))

    model.movers += a_mover
    assert len(model.movers) == 1

    spill = point_line_release_spill(num_elements=10,
                                     start_position=(0., 0., 0.),
                                     release_time=start_time)

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

    # model.add_spill(spill)

    model.start_time = spill.release.release_time

    # test iterator
    for step in model:
        print 'just ran time step: %s' % model.current_time_step
        assert step['step_num'] == model.current_time_step

    pos = np.copy(model.spills.LE('positions'))

    # rewind and run again:
    print 'rewinding'
    model.rewind()

    # test iterator is repeatable
    for step in model:
        print 'just ran time step: %s' % model.current_time_step
        assert step['step_num'] == model.current_time_step

    assert np.all(model.spills.LE('positions') == pos)
def test_serialize_deserialize(json_):
    '''
    todo: this behaves in unexpected ways when using the 'model' testfixture.
    For now, define a model in here for the testing - not sure where the
    problem lies
    '''
    s_time = datetime(2014, 1, 1, 1, 1, 1)
    model = Model(start_time=s_time)
    model.spills += point_line_release_spill(num_elements=5,
        start_position=(0, 0, 0),
        release_time=model.start_time)

    o_put = NetCDFOutput(os.path.join(base_dir, u'xtemp.nc'))
    model.outputters += o_put
    model.movers += RandomMover(diffusion_coef=100000)

    #==========================================================================
    # o_put = [model.outputters[outputter.id]
    #          for outputter in model.outputters
    #          if isinstance(outputter, NetCDFOutput)][0]
    #==========================================================================

    model.rewind()
    print "step: {0}, _start_idx: {1}".format(-1, o_put._start_idx)
    for ix in range(2):
        model.step()
        print "step: {0}, _start_idx: {1}".format(ix, o_put._start_idx)

    #for json_ in ('save', 'webapi'):
    dict_ = o_put.deserialize(o_put.serialize(json_))
    o_put2 = NetCDFOutput.new_from_dict(dict_)
    if json_ == 'save':
        assert o_put == o_put2
    else:
        # _start_idx and _middle_of_run should not match
        assert o_put._start_idx != o_put2._start_idx
        assert o_put._middle_of_run != o_put2._middle_of_run
        assert o_put != o_put2

    if os.path.exists(o_put.netcdf_filename):
        print '\n{0} exists'.format(o_put.netcdf_filename)
Esempio n. 6
0
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)
Esempio n. 7
0
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_serialize_deserialize(json_, output_filename):
    '''
    todo: this behaves in unexpected ways when using the 'model' testfixture.
    For now, define a model in here for the testing - not sure where the
    problem lies
    '''
    s_time = datetime(2014, 1, 1, 1, 1, 1)
    model = Model(start_time=s_time)
    model.spills += point_line_release_spill(num_elements=5,
                                             start_position=(0, 0, 0),
                                             release_time=model.start_time)

    o_put = NetCDFOutput(output_filename)
    model.outputters += o_put
    model.movers += RandomMover(diffusion_coef=100000)

    # ==========================================================================
    # o_put = [model.outputters[outputter.id]
    #          for outputter in model.outputters
    #          if isinstance(outputter, NetCDFOutput)][0]
    # ==========================================================================

    model.rewind()
    print "step: {0}, _start_idx: {1}".format(-1, o_put._start_idx)
    for ix in range(2):
        model.step()
        print "step: {0}, _start_idx: {1}".format(ix, o_put._start_idx)

    dict_ = o_put.deserialize(o_put.serialize(json_))
    o_put2 = NetCDFOutput.new_from_dict(dict_)
    if json_ == 'save':
        assert o_put == o_put2
    else:
        # _start_idx and _middle_of_run should not match
        assert o_put._start_idx != o_put2._start_idx
        assert o_put._middle_of_run != o_put2._middle_of_run
        assert o_put != o_put2

    if os.path.exists(o_put.netcdf_filename):
        print '\n{0} exists'.format(o_put.netcdf_filename)