def test_ice_image_mid_run(): """ Test image outputter with a model NOTE: could it be tested with just a mover, and not a full model? -- that gets tricky with the cache and timesteps... """ start_time = datetime(2015, 5, 14, 0) model = Model(time_step=3600 * 24, start_time=start_time, duration=timedelta(days=3)) # one day model.cache_enabled = False model.uncertain = False c_ice_mover = IceMover(curr_file, topology_file) model.movers += c_ice_mover # run the model a couple steps step = model.step() step = model.step() # now add the outputter model.outputters += IceImageOutput(c_ice_mover, viewport=((-175.0, 65.0), (-145.0, 75.05))) # and run some more: step = model.step() step = model.step() # and check the output ice_output = step["IceImageOutput"] for key in ("time_stamp", "thickness_image", "concentration_image", "bounding_box", "projection"): assert key in ice_output print "thickness img size:", len(ice_output["thickness_image"]) print "concentration img size:", len(ice_output["concentration_image"])
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
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
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_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_start_time(): model = Model() st = datetime.now() model.start_time = st assert model.start_time == st assert model.current_time_step == -1 model.step() st = datetime(2012, 8, 12, 13) model.start_time = st assert model.current_time_step == -1 assert model.start_time == st
def test_start_time(): model = Model() st = datetime.now() model.start_time = st assert model.start_time == st assert model.current_time_step == -1 model.step() st = datetime(2012, 8, 12, 13) model.start_time = st assert model.current_time_step == -1 assert model.start_time == st
def test_model_time_and_current_time_in_sc(): model = Model() model.start_time = datetime.now() assert model.current_time_step == -1 assert model.model_time == model.start_time for step in range(4): model.step() assert model.current_time_step == step assert (model.model_time == model.start_time + timedelta(seconds=step * model.time_step)) for sc in model.spills.items(): assert model.model_time == sc.current_time_stamp
def test_model_time_and_current_time_in_sc(): model = Model() model.start_time = datetime.now() assert model.current_time_step == -1 assert model.model_time == model.start_time for step in range(4): model.step() assert model.current_time_step == step assert (model.model_time == model.start_time + timedelta(seconds=step * model.time_step)) for sc in model.spills.items(): assert model.model_time == sc.current_time_stamp
def test_simple_run_with_image_output_uncertainty(): ''' Pretty much all this tests is that the model will run and output images ''' images_dir = os.path.join(basedir, 'Test_images2') 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 gmap = gnome.map.MapFromBNA(testmap, refloat_halflife=6) # hours renderer = gnome.outputters.Renderer(testmap, images_dir, size=(400, 300)) model = Model(start_time=start_time, time_step=timedelta(minutes=15), duration=timedelta(hours=1), map=gmap, uncertain=True, cache_enabled=False, ) model.outputters += renderer a_mover = SimpleMover(velocity=(1., -1., 0.)) model.movers += a_mover 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 release = SpatialRelease(start_position=start_points, release_time=start_time) model.spills += Spill(release) # model.add_spill(spill) model.start_time = release.release_time # image_info = model.next_image() model.uncertain = True num_steps_output = 0 while True: try: image_info = model.step() num_steps_output += 1 print image_info 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
def test_callback_add_mover_midrun(): 'Test callback after add mover called midway through the run' model = Model() model.start_time = datetime(2012, 1, 1, 0, 0) model.duration = timedelta(hours=10) model.time_step = timedelta(hours=1) # start_loc = (1.0, 2.0, 0.0) # random non-zero starting points # model = setup_simple_model() for i in range(2): model.step() assert model.current_time_step > -1 # now add another mover and make sure model rewinds model.movers += SimpleMover(velocity=(2., -2., 0.)) assert model.current_time_step == -1
def test_callback_add_mover_midrun(): 'Test callback after add mover called midway through the run' model = Model() model.start_time = datetime(2012, 1, 1, 0, 0) model.duration = timedelta(hours=10) model.time_step = timedelta(hours=1) # start_loc = (1.0, 2.0, 0.0) # random non-zero starting points # model = setup_simple_model() for i in range(2): model.step() assert model.current_time_step > -1 # now add another mover and make sure model rewinds model.movers += SimpleMover(velocity=(2., -2., 0.)) assert model.current_time_step == -1
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)
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)
def test_ice_image_mid_run(): ''' Test image outputter with a model NOTE: could it be tested with just a mover, and not a full model? -- that gets tricky with the cache and timesteps... ''' start_time = datetime(2015, 5, 14, 0) model = Model( time_step=3600 * 24, # one day start_time=start_time, duration=timedelta(days=3), ) model.cache_enabled = False model.uncertain = False c_ice_mover = IceMover(curr_file, topology_file) model.movers += c_ice_mover # run the model a couple steps step = model.step() step = model.step() # now add the outputter model.outputters += IceImageOutput(c_ice_mover, viewport=((-175.0, 65.0), (-145.0, 75.05))) # and run some more: step = model.step() step = model.step() # and check the output ice_output = step['IceImageOutput'] for key in ('time_stamp', 'thickness_image', 'concentration_image', 'bounding_box', 'projection'): assert key in ice_output print 'thickness img size:', len(ice_output['thickness_image']) print 'concentration img size:', len(ice_output['concentration_image'])
def test_ice_image_mid_run(): ''' Test image outputter with a model NOTE: could it be tested with just a mover, and not a full model? -- that gets tricky with the cache and timesteps... ''' start_time = datetime(2015, 5, 14, 0) model = Model(time_step=3600*24, # one day start_time=start_time, duration=timedelta(days=3),) model.cache_enabled = False model.uncertain = False c_ice_mover = IceMover(curr_file, topology_file) model.movers += c_ice_mover ## run the model a couple steps step = model.step() step = model.step() ## now add the outputter iio = IceImageOutput(c_ice_mover) model.outputters += iio ## and run some more: step = model.step() step = model.step() ## and check the output ice_output = step['IceImageOutput'] # print ice_output['time_stamp'] # print ice_output['concentration_image'][:50] # could be really big! # print ice_output['bounding_box'] # print ice_output['projection'] for key in ('time_stamp', 'thickness_image', 'concentration_image', 'bounding_box', 'projection'): assert key in ice_output
def test_make_default_refs(): ''' ensure make_default_refs is a thread-safe operation once object is instantiated, object.make_default_refs is an attribute of instance ''' model = Model() model1 = Model() wind = Wind(timeseries=[(t, (0, 1))], units='m/s') water = Water() waves = Waves(name='waves') waves1 = Waves(name='waves1', make_default_refs=False) model.environment += [wind, water, waves] model1.environment += waves1 # waves should get auto hooked up/waves1 should not model.step() assert waves.wind is wind assert waves.water is water with pytest.raises(ReferencedObjectNotSet): model1.step()
def test_release_at_right_time(): ''' Tests that the elements get released when they should There are issues in that we want the elements to show up in the output for a given time step if they were supposed to be released then. Particularly for the first time step of the model. ''' # default to now, rounded to the nearest hour seconds_in_minute = 60 minutes_in_hour = 60 seconds_in_hour = seconds_in_minute * minutes_in_hour start_time = datetime(2013, 1, 1, 0) time_step = 2 * seconds_in_hour model = Model(time_step=time_step, start_time=start_time, duration=timedelta(hours=12)) # add a spill that starts right when the run begins model.spills += point_line_release_spill(num_elements=12, start_position=(0, 0, 0), release_time=datetime(2013, 1, 1, 0), end_release_time=datetime(2013, 1, 1, 6) ) # before the run - no elements present since data_arrays get defined after # 1st step (prepare_for_model_run): assert model.spills.items()[0].num_released == 0 model.step() assert model.spills.items()[0].num_released == 4 model.step() assert model.spills.items()[0].num_released == 8 model.step() assert model.spills.items()[0].num_released == 12 model.step() assert model.spills.items()[0].num_released == 12
def test_release_at_right_time(): ''' Tests that the elements get released when they should There are issues in that we want the elements to show up in the output for a given time step if they were supposed to be released then. Particularly for the first time step of the model. ''' # default to now, rounded to the nearest hour seconds_in_minute = 60 minutes_in_hour = 60 seconds_in_hour = seconds_in_minute * minutes_in_hour start_time = datetime(2013, 1, 1, 0) time_step = 2 * seconds_in_hour model = Model(time_step=time_step, start_time=start_time, duration=timedelta(hours=12)) # add a spill that starts right when the run begins model.spills += point_line_release_spill(num_elements=12, start_position=(0, 0, 0), release_time=datetime(2013, 1, 1, 0), end_release_time=datetime(2013, 1, 1, 6) ) # before the run - no elements present since data_arrays get defined after # 1st step (prepare_for_model_run): assert model.spills.items()[0].num_released == 0 model.step() assert model.spills.items()[0].num_released == 4 model.step() assert model.spills.items()[0].num_released == 8 model.step() assert model.spills.items()[0].num_released == 12 model.step() assert model.spills.items()[0].num_released == 12
def test_make_default_refs(): ''' ensure make_default_refs is a thread-safe operation once object is instantiated, object.make_default_refs is an attribute of instance ''' model = Model() model1 = Model() wind = Wind(timeseries=[(t, (0, 1))], units='m/s') water = Water() waves = Waves(name='waves') waves1 = Waves(name='waves1', make_default_refs=False) model.environment += [wind, water, waves] model1.environment += waves1 # waves should get auto hooked up/waves1 should not model.step() assert waves.wind is wind assert waves.water is water with pytest.raises(ReferencedObjectNotSet): model1.step()
def test_simple_run_with_image_output(): """ pretty much all this tests is that the model will run and output images """ # create a place for test images (cleaning out any old ones) images_dir = os.path.join(basedir, 'Test_images') 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(testmap, refloat_halflife=6) # hours renderer = gnome.renderer.Renderer(testmap, images_dir, size=(400, 300)) 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 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 = SpatialRelease(start_positions=start_points, release_time=start_time) model.spills += spill # model.add_spill(spill) assert len(model.spills) == 1 model.start_time = spill.release_time # image_info = model.next_image() num_steps_output = 0 while True: print 'calling step' try: image_info = model.step() num_steps_output += 1 print image_info except StopIteration: print 'Done with the model run' break # there is the zeroth step, too. assert num_steps_output == model.duration.total_seconds() \ / model.time_step + 1
def test_full_model_run(): """ This will have a couple movers, and make sure that the on_tideflat status code stops all movers. only winds and currents for now """ start_time = "2018-09-10T12:00" model = Model( start_time=start_time, time_step=sc.minutes(10), ) # start_time=round_time(datetime.now(), 3600), # duration=timedelta(days=1), # weathering_substeps=1, # map=None, # uncertain=False, # cache_enabled=False, # mode=None, # location=[], # environment=[], # outputters=[], # movers=[], # weatherers=[], # spills=[], # uncertain_spills=[], # **kwargs): # ) model.movers += constant_wind_mover(speed=10, direction=225, units='m/s') model.movers += RandomMover() # defaults are fine model.spills += point_line_release_spill( num_elements=10, start_position=(0.0, 0.0, 0.0), release_time=start_time, ) # fixme: should maybe add currents -- though if the currents are smart, # they should be zero on the tideflats # run one step: print model.step() # step zero -- should be released, but not yet moved positions = model.get_spill_property('positions').copy() assert np.all(positions == 0.0) prev_positions = positions print model.step() # step one -- all elements should have moved in horizontal positions = model.get_spill_property('positions').copy() assert np.all(positions[:, 0:1] != prev_positions[:, 0:1]) assert np.all(positions[:, 2] == prev_positions[:, 2]) prev_positions = positions # Now set the flags for half of the elements status_codes = model.get_spill_property('status_codes') new_status_codes = np.array([oil_status.on_tideflat] * 5 + [oil_status.in_water] * 5) status_codes[:] = new_status_codes # make sure it took assert np.all(model.get_spill_property('status_codes') == new_status_codes) # step the model again model.step() positions = model.get_spill_property('positions').copy() delta = positions - prev_positions # first five should not have moved assert np.all(delta[:5, 0:1] == 0.0) assert np.all(delta[5:, 0:1] != 0.0) prev_positions = positions # reset status codes to in_water: model.get_spill_property('status_codes')[:] = oil_status.in_water # step the model again print model.step() positions = model.get_spill_property('positions').copy() delta = positions - prev_positions # they all should have moved again assert np.all(delta[:, 0:1] != 0.0)