def test_fastscape_new(): input_str = os.path.join(_THIS_DIR, 'drive_sp_params.txt') inputs = ModelParameterDictionary(input_str, auto_type=True) nrows = inputs.read_int('nrows') ncols = inputs.read_int('ncols') dx = inputs.read_float('dx') dt = inputs.read_float('dt') time_to_run = inputs.read_float('run_time') uplift = inputs.read_float('uplift_rate') init_elev = inputs.read_float('init_elev') mg = RasterModelGrid(nrows, ncols, dx) mg.set_closed_boundaries_at_grid_edges(False, False, True, True) mg.add_zeros('topographic__elevation', at='node') z = mg.zeros(at='node') + init_elev numpy.random.seed(0) mg['node']['topographic__elevation'] = z + \ numpy.random.rand(len(z)) / 1000. fr = FlowAccumulator(mg, flow_director='D8') fsp = Fsc(mg, **inputs) # here's the diff from the above elapsed_time = 0. while elapsed_time < time_to_run: if elapsed_time + dt > time_to_run: dt = time_to_run - elapsed_time fr.run_one_step() fsp.run_one_step(dt) # new style mg.at_node['topographic__elevation'][mg.core_nodes] += uplift * dt elapsed_time += dt z_trg = numpy.array([5.48813504e-04, 7.15189366e-04, 6.02763376e-04, 5.44883183e-04, 4.23654799e-04, 6.45894113e-04, 1.01830760e-02, 9.58036770e-03, 6.55865452e-03, 3.83441519e-04, 7.91725038e-04, 1.00142749e-02, 8.80798884e-03, 5.78387585e-03, 7.10360582e-05, 8.71292997e-05, 9.81911417e-03, 9.52243406e-03, 7.55093226e-03, 8.70012148e-04, 9.78618342e-04, 1.00629755e-02, 8.49253798e-03, 5.33216680e-03, 1.18274426e-04, 6.39921021e-04, 9.88956320e-03, 9.47119567e-03, 6.43790696e-03, 4.14661940e-04, 2.64555612e-04, 1.00450743e-02, 8.37262908e-03, 5.21540904e-03, 1.87898004e-05, 6.17635497e-04, 9.21286940e-03, 9.34022513e-03, 7.51114450e-03, 6.81820299e-04, 3.59507901e-04, 6.19166921e-03, 7.10456176e-03, 6.62585507e-03, 6.66766715e-04, 6.70637870e-04, 2.10382561e-04, 1.28926298e-04, 3.15428351e-04, 3.63710771e-04]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_trg)
def test_fastscape(): input_str = os.path.join(_THIS_DIR, 'drive_sp_params.txt') inputs = ModelParameterDictionary(input_str) nrows = inputs.read_int('nrows') ncols = inputs.read_int('ncols') dx = inputs.read_float('dx') dt = inputs.read_float('dt') time_to_run = inputs.read_float('run_time') uplift = inputs.read_float('uplift_rate') init_elev = inputs.read_float('init_elev') mg = RasterModelGrid(nrows, ncols, dx) mg.set_closed_boundaries_at_grid_edges(False, False, True, True) mg.add_zeros('topographic__elevation', at='node') z = mg.zeros(at='node') + init_elev numpy.random.seed(0) mg['node']['topographic__elevation'] = z + \ numpy.random.rand(len(z)) / 1000. fr = FlowRouter(mg) fsp = Fsc(mg, input_str) elapsed_time = 0. while elapsed_time < time_to_run: if elapsed_time + dt > time_to_run: dt = time_to_run - elapsed_time mg = fr.route_flow(method='D8') mg = fsp.erode(mg) mg.at_node['topographic__elevation'][mg.core_nodes] += uplift * dt elapsed_time += dt z_trg = numpy.array([5.48813504e-04, 7.15189366e-04, 6.02763376e-04, 5.44883183e-04, 4.23654799e-04, 6.45894113e-04, 1.01830760e-02, 9.58036770e-03, 6.55865452e-03, 3.83441519e-04, 7.91725038e-04, 1.00142749e-02, 8.80798884e-03, 5.78387585e-03, 7.10360582e-05, 8.71292997e-05, 9.81911417e-03, 9.52243406e-03, 7.55093226e-03, 8.70012148e-04, 9.78618342e-04, 1.00629755e-02, 8.49253798e-03, 5.33216680e-03, 1.18274426e-04, 6.39921021e-04, 9.88956320e-03, 9.47119567e-03, 6.43790696e-03, 4.14661940e-04, 2.64555612e-04, 1.00450743e-02, 8.37262908e-03, 5.21540904e-03, 1.87898004e-05, 6.17635497e-04, 9.21286940e-03, 9.34022513e-03, 7.51114450e-03, 6.81820299e-04, 3.59507901e-04, 6.19166921e-03, 7.10456176e-03, 6.62585507e-03, 6.66766715e-04, 6.70637870e-04, 2.10382561e-04, 1.28926298e-04, 3.15428351e-04, 3.63710771e-04]) assert_array_almost_equal(mg.at_node['topographic__elevation'], z_trg)
# make some K values in a field to test mg.at_node["K_values"] = 0.00001 + np.random.rand(nrows * ncols) / 100000.0 # mg.at_node['water__unit_flux_in'] = dx*dx*np.ones_like(z) mg.at_node["water__unit_flux_in"] = ( dx * dx * np.ones_like(z) * 100.0 / (60.0 * 60.0 * 24.0 * 365.25) ) # remember, flux is /sec, so this is a small number! # mg.set_closed_boundaries_at_grid_edges(False, False, True, True) # mg.set_closed_boundaries_at_grid_edges(True, False, True, True) print("Running ...") # instantiate the components: fr = FlowRouter(mg) # load the Fastscape module too, to allow direct comparison fsp = FastscapeEroder(mg, "./pot_fr_params.txt") # perform the loop: elapsed_time = 0.0 # total time in simulation while elapsed_time < time_to_run: print(elapsed_time) if elapsed_time + dt > time_to_run: print("Short step!") dt = time_to_run - elapsed_time mg = fr.route_flow() # print 'Area: ', numpy.max(mg.at_node['drainage_area']) # mg = fsp.erode(mg) mg = fsp.erode(mg, K_if_used="K_values") # mg,_,_ = sp.erode(mg, dt, node_drainage_areas='drainage_area', slopes_at_nodes='topographic__steepest_slope') # add uplift mg.at_node["topographic__elevation"][mg.core_nodes] += uplift * dt
#make some K values in a field to test #mg.at_node['K_values'] = 0.1+numpy.random.rand(nrows*ncols)/10. mg.at_node['K_values'] = numpy.empty(nrows * ncols, dtype=float) #mg.at_node['K_values'].fill(0.1+numpy.random.rand()/10.) mg.at_node['K_values'].fill(0.001) print('Running ...') #instantiate the components: fr = FlowRouter(mg) sp = StreamPowerEroder(mg, input_file_string) #fsp = FastscapeEroder(mg, input_file_string) precip = PrecipitationDistribution(input_file=input_file_string) #load the Fastscape module too, to allow direct comparison fsp = FastscapeEroder(mg, input_file_string) try: #raise NameError mg = copy.deepcopy(mg_mature) except NameError: print('building a new grid...') out_interval = 50000. last_trunc = time_to_run #we use this to trigger taking an output plot #run to a steady state: #We're going to cheat by running Fastscape SP for the first part of the solution for (interval_duration, rainfall_rate) in precip.yield_storm_interstorm_duration_intensity(): if rainfall_rate != 0.: mg.at_node['water__unit_flux_in'].fill(rainfall_rate) mg = fr.route_flow()
uplift_perstep = inputs.read_float("uplift_rate") * dt rock_stress_param = inputs.read_float("rock_density") * 9.81 mg = RasterModelGrid(nrows, ncols, dx) # create the fields in the grid mg.add_zeros("topographic__elevation", at="node") z = mg.zeros(at="node") + init_elev mg["node"]["topographic__elevation"] = z + np.random.rand(len(z)) / 1000. # make some surface load stresses in a field to test mg.at_node["surface_load__stress"] = np.zeros(nrows * ncols, dtype=float) # instantiate: gf = gFlex(mg, "./coupled_SP_gflex_params.txt") fsp = FastscapeEroder(mg, "./coupled_SP_gflex_params.txt") sp = StreamPowerEroder(mg, "./coupled_SP_gflex_params.txt") fr = FlowAccumulator(mg, flow_director="D8") # perform the loop: elapsed_time = 0. # total time in simulation while elapsed_time < time_to_run: print(elapsed_time) if elapsed_time + dt > time_to_run: print("Short step!") dt = time_to_run - elapsed_time mg = fr.run_one_step() # mg = fsp.erode(mg) mg, _, _ = sp.erode( mg, dt,
mg.add_zeros('topographic__elevation', at='node') z = mg.zeros(at='node') + init_elev mg['node']['topographic__elevation'] = z + numpy.random.rand(len(z)) / 1000. #make some K values in a field to test mg.at_node['K_values'] = 0.1 + numpy.random.rand(nrows * ncols) / 10. mg.set_closed_boundaries_at_grid_edges(False, True, True, True) print('Running ...') time_on = time.time() #instantiate the components: fr = FlowRouter(mg) sp = StreamPowerEroder(mg, './drive_sp_params.txt') #load the Fastscape module too, to allow direct comparison fsp = FastscapeEroder(mg, './drive_sp_params.txt') #perform the loop: elapsed_time = 0. #total time in simulation counter = 0. while elapsed_time < time_to_run: print(elapsed_time) if elapsed_time + dt > time_to_run: print("Short step!") dt = time_to_run - elapsed_time mg = fr.route_flow(method='D8') #print 'Area: ', numpy.max(mg.at_node['drainage_area']) #mg = fsp.erode(mg) mg, _, _ = sp.erode(mg, dt, node_drainage_areas='drainage_area',
mg['node']['topographic__elevation'] = z + np.random.rand(len(z))/1000. #make some K values in a field to test mg.at_node['K_values'] = 0.00001+np.random.rand(nrows*ncols)/100000. #mg.at_node['water__unit_flux_in'] = dx*dx*np.ones_like(z) mg.at_node['water__unit_flux_in'] = dx*dx*np.ones_like(z)*100./(60.*60.*24.*365.25) #remember, flux is /sec, so this is a small number! #mg.set_closed_boundaries_at_grid_edges(False, False, True, True) #mg.set_closed_boundaries_at_grid_edges(True, False, True, True) print( 'Running ...' ) #instantiate the components: fr = FlowRouter(mg) #load the Fastscape module too, to allow direct comparison fsp = FastscapeEroder(mg, './pot_fr_params.txt') #perform the loop: elapsed_time = 0. #total time in simulation while elapsed_time < time_to_run: print(elapsed_time) if elapsed_time+dt>time_to_run: print("Short step!") dt = time_to_run - elapsed_time mg = fr.route_flow() #print 'Area: ', numpy.max(mg.at_node['drainage_area']) #mg = fsp.erode(mg) mg = fsp.erode(mg, K_if_used='K_values') #mg,_,_ = sp.erode(mg, dt, node_drainage_areas='drainage_area', slopes_at_nodes='topographic__steepest_slope') #add uplift mg.at_node['topographic__elevation'][mg.core_nodes] += uplift*dt
uplift = inputs.read_float('uplift_rate') init_elev = inputs.read_float('init_elev') mg = RasterModelGrid(nrows, ncols, dx) #create the fields in the grid mg.add_zeros('topographic__elevation', at='node') z = mg.zeros(at='node') + init_elev mg['node'][ 'topographic__elevation'] = z + numpy.random.rand(len(z))/1000. print( 'Running ...' ) #instantiate the components: fr = FlowRouter(mg) sp = StreamPowerEroder(mg, './drive_sp_params.txt') fsp = Fsc(mg, './drive_sp_params.txt') #load the Fastscape module too, to allow direct comparison fsp = Fsc(mg, './drive_sp_params.txt') vid = VideoPlotter(mg, data_centering='node', step=2.5) try: mg = copy.deepcopy(mg_mature) except NameError: #run to a steady state: #We're going to cheat by running Fastscape SP for the first part of the solution elapsed_time = 0. #total time in simulation while elapsed_time < time_to_run: print(elapsed_time) if elapsed_time+dt>time_to_run: print("Short step!")
#create the fields in the grid mg.add_zeros('topographic__elevation', at='node') z = mg.zeros(at='node') + init_elev mg['node']['topographic__elevation'] = z + numpy.random.rand(len(z)) / 1000. #make some K values in a field to test mg.at_node['K_values'] = 0.1 + numpy.random.rand(nrows * ncols) / 10. print('Running ...') #instantiate the components: fr = FlowRouter(mg) sp = StreamPowerEroder(mg, './drive_sp_params.txt') #load the Fastscape module too, to allow direct comparison fsp = FastscapeEroder(mg, './drive_sp_params.txt') #perform the loop: elapsed_time = 0. #total time in simulation while elapsed_time < time_to_run: print(elapsed_time) if elapsed_time + dt > time_to_run: print("Short step!") dt = time_to_run - elapsed_time mg = fr.route_flow() #print 'Area: ', numpy.max(mg.at_node['drainage_area']) #mg = fsp.erode(mg) mg = fsp.erode(mg, K_if_used='K_values') #mg,_,_ = sp.erode(mg, dt, node_drainage_areas='drainage_area', slopes_at_nodes='topographic__steepest_slope') #add uplift mg.at_node['topographic__elevation'][mg.core_nodes] += uplift * dt
# create the fields in the grid mg.add_zeros("topographic__elevation", at="node") z = mg.zeros(at="node") + init_elev mg["node"]["topographic__elevation"] = z + numpy.random.rand(len(z)) / 1000. # make some K values in a field to test mg.at_node["K_values"] = 1.e-6 + numpy.random.rand(nrows * ncols) * 1.e-8 mg.set_closed_boundaries_at_grid_edges(False, True, True, True) print("Running ...") # instantiate the components: fr = FlowAccumulator(mg, flow_director="D8") sp = StreamPowerEroder(mg, "./drive_sp_params.txt") fsp = Fsc(mg, "./drive_sp_params.txt") # load the Fastscape module too, to allow direct comparison fsp = Fsc(mg, "./drive_sp_params.txt") # vid = VideoPlotter(mg, data_centering='node', step=2.5) try: mg = copy.deepcopy(mg_mature) except NameError: # run to a steady state: # We're going to cheat by running Fastscape SP for the first part of the solution elapsed_time = 0. # total time in simulation while elapsed_time < time_to_run: print(elapsed_time) if elapsed_time + dt > time_to_run: print("Short step!")
def test_fastscape(): input_str = os.path.join(_THIS_DIR, "drive_sp_params.txt") inputs = ModelParameterDictionary(input_str) nrows = inputs.read_int("nrows") ncols = inputs.read_int("ncols") dx = inputs.read_float("dx") dt = inputs.read_float("dt") time_to_run = inputs.read_float("run_time") uplift = inputs.read_float("uplift_rate") init_elev = inputs.read_float("init_elev") mg = RasterModelGrid(nrows, ncols, dx) mg.set_closed_boundaries_at_grid_edges(False, False, True, True) mg.add_zeros("topographic__elevation", at="node") z = mg.zeros(at="node") + init_elev numpy.random.seed(0) mg["node"]["topographic__elevation"] = z + numpy.random.rand(len(z)) / 1000. fr = FlowAccumulator(mg, flow_director="D8") fsp = Fsc(mg, input_str, method="D8") elapsed_time = 0. while elapsed_time < time_to_run: if elapsed_time + dt > time_to_run: dt = time_to_run - elapsed_time mg = fr.run_one_step() mg = fsp.erode(mg, dt=dt) mg.at_node["topographic__elevation"][mg.core_nodes] += uplift * dt elapsed_time += dt z_trg = numpy.array( [ 5.48813504e-04, 7.15189366e-04, 6.02763376e-04, 5.44883183e-04, 4.23654799e-04, 6.45894113e-04, 1.01830760e-02, 9.58036770e-03, 6.55865452e-03, 3.83441519e-04, 7.91725038e-04, 1.00142749e-02, 8.80798884e-03, 5.78387585e-03, 7.10360582e-05, 8.71292997e-05, 9.81911417e-03, 9.52243406e-03, 7.55093226e-03, 8.70012148e-04, 9.78618342e-04, 1.00629755e-02, 8.49253798e-03, 5.33216680e-03, 1.18274426e-04, 6.39921021e-04, 9.88956320e-03, 9.47119567e-03, 6.43790696e-03, 4.14661940e-04, 2.64555612e-04, 1.00450743e-02, 8.37262908e-03, 5.21540904e-03, 1.87898004e-05, 6.17635497e-04, 9.21286940e-03, 9.34022513e-03, 7.51114450e-03, 6.81820299e-04, 3.59507901e-04, 6.19166921e-03, 7.10456176e-03, 6.62585507e-03, 6.66766715e-04, 6.70637870e-04, 2.10382561e-04, 1.28926298e-04, 3.15428351e-04, 3.63710771e-04, ] ) assert_array_almost_equal(mg.at_node["topographic__elevation"], z_trg)
def test_fastscape(): uplift = 0.001 dt = 1.0 time_to_run = 10.0 mg = RasterModelGrid((10, 5)) mg.set_closed_boundaries_at_grid_edges(False, False, True, True) mg.add_zeros("topographic__elevation", at="node") z = mg.zeros(at="node") numpy.random.seed(0) mg["node"]["topographic__elevation"] = z + numpy.random.rand( len(z)) / 1000.0 fr = FlowAccumulator(mg, flow_director="D8") fsp = Fsc(mg, K_sp=0.1, m_sp=0.5, n_sp=1.0, threshold_sp=0.0) elapsed_time = 0.0 while elapsed_time < time_to_run: if elapsed_time + dt > time_to_run: dt = time_to_run - elapsed_time fr.run_one_step() fsp.run_one_step(dt=dt) mg.at_node["topographic__elevation"][mg.core_nodes] += uplift * dt elapsed_time += dt z_trg = numpy.array([ 5.48813504e-04, 7.15189366e-04, 6.02763376e-04, 5.44883183e-04, 4.23654799e-04, 6.45894113e-04, 1.01830760e-02, 9.58036770e-03, 6.55865452e-03, 3.83441519e-04, 7.91725038e-04, 1.00142749e-02, 8.80798884e-03, 5.78387585e-03, 7.10360582e-05, 8.71292997e-05, 9.81911417e-03, 9.52243406e-03, 7.55093226e-03, 8.70012148e-04, 9.78618342e-04, 1.00629755e-02, 8.49253798e-03, 5.33216680e-03, 1.18274426e-04, 6.39921021e-04, 9.88956320e-03, 9.47119567e-03, 6.43790696e-03, 4.14661940e-04, 2.64555612e-04, 1.00450743e-02, 8.37262908e-03, 5.21540904e-03, 1.87898004e-05, 6.17635497e-04, 9.21286940e-03, 9.34022513e-03, 7.51114450e-03, 6.81820299e-04, 3.59507901e-04, 6.19166921e-03, 7.10456176e-03, 6.62585507e-03, 6.66766715e-04, 6.70637870e-04, 2.10382561e-04, 1.28926298e-04, 3.15428351e-04, 3.63710771e-04, ]) assert_array_almost_equal(mg.at_node["topographic__elevation"], z_trg)
# Set a new random outlet position mg.set_inactive_boundaries(True, True, True, True) #mg.set_closed_boundaries_at_grid_edges(True,True,True,True) random_boundary_node = random.choice(boundary_node_list) mg.status_at_node[random_boundary_node] = FIXED_VALUE_BOUNDARY # MN: Set the elevation of that random outlet boundary node to zero #mg['node'][ 'topographic__elevation'][random_boundary_node] = 0 print('Random boundary node', random_boundary_node) #instantiate the components: fr = FlowRouter(mg) sp = FastscapeEroder(mg, input_file) time_on = time() #perform the inner time loops: for i in range(nt): mg['node']['topographic__elevation'][mg.core_nodes] += uplift_per_step mg = fr.route_flow() mg = sp.erode(mg) #plot long profiles along channels pylab.figure(6) profile_IDs = prf.channel_nodes(mg, mg.at_node['topographic__steepest_slope'], mg.at_node['drainage_area'], mg.at_node['upstream_node_order'], mg.at_node['flow_receiver']) dists_upstr = prf.get_distances_upstream(mg, len(mg.at_node['topographic__steepest_slope']),
for t in range(5): # Set a new random outlet position mg.set_inactive_boundaries(True, True, True, True) #mg.set_closed_boundaries_at_grid_edges(True,True,True,True) random_boundary_node = random.choice(boundary_node_list) mg.status_at_node[random_boundary_node] = FIXED_VALUE_BOUNDARY # MN: Set the elevation of that random outlet boundary node to zero #mg['node'][ 'topographic__elevation'][random_boundary_node] = 0 print('Random boundary node', random_boundary_node) #instantiate the components: fr = FlowRouter(mg) sp = FastscapeEroder(mg, input_file) time_on = time() #perform the inner time loops: for i in range(nt): mg['node']['topographic__elevation'][mg.core_nodes] += uplift_per_step mg = fr.route_flow() mg = sp.erode(mg) #plot long profiles along channels pylab.figure(6) profile_IDs = prf.channel_nodes( mg, mg.at_node['topographic__steepest_slope'], mg.at_node['drainage_area'], mg.at_node['upstream_node_order'], mg.at_node['flow_receiver'])
def test_fastscape_new(): input_str = os.path.join(_THIS_DIR, "drive_sp_params.txt") inputs = ModelParameterDictionary(input_str, auto_type=True) nrows = inputs.read_int("nrows") ncols = inputs.read_int("ncols") dx = inputs.read_float("dx") dt = inputs.read_float("dt") time_to_run = inputs.read_float("run_time") uplift = inputs.read_float("uplift_rate") init_elev = inputs.read_float("init_elev") mg = RasterModelGrid(nrows, ncols, xy_spacing=dx) mg.set_closed_boundaries_at_grid_edges(False, False, True, True) mg.add_zeros("topographic__elevation", at="node") z = mg.zeros(at="node") + init_elev numpy.random.seed(0) mg["node"]["topographic__elevation"] = z + numpy.random.rand(len(z)) / 1000. fr = FlowAccumulator(mg, flow_director="D8") fsp = Fsc(mg, **inputs) # here's the diff from the above elapsed_time = 0. while elapsed_time < time_to_run: if elapsed_time + dt > time_to_run: dt = time_to_run - elapsed_time fr.run_one_step() fsp.run_one_step(dt) # new style mg.at_node["topographic__elevation"][mg.core_nodes] += uplift * dt elapsed_time += dt z_trg = numpy.array( [ 5.48813504e-04, 7.15189366e-04, 6.02763376e-04, 5.44883183e-04, 4.23654799e-04, 6.45894113e-04, 1.01830760e-02, 9.58036770e-03, 6.55865452e-03, 3.83441519e-04, 7.91725038e-04, 1.00142749e-02, 8.80798884e-03, 5.78387585e-03, 7.10360582e-05, 8.71292997e-05, 9.81911417e-03, 9.52243406e-03, 7.55093226e-03, 8.70012148e-04, 9.78618342e-04, 1.00629755e-02, 8.49253798e-03, 5.33216680e-03, 1.18274426e-04, 6.39921021e-04, 9.88956320e-03, 9.47119567e-03, 6.43790696e-03, 4.14661940e-04, 2.64555612e-04, 1.00450743e-02, 8.37262908e-03, 5.21540904e-03, 1.87898004e-05, 6.17635497e-04, 9.21286940e-03, 9.34022513e-03, 7.51114450e-03, 6.81820299e-04, 3.59507901e-04, 6.19166921e-03, 7.10456176e-03, 6.62585507e-03, 6.66766715e-04, 6.70637870e-04, 2.10382561e-04, 1.28926298e-04, 3.15428351e-04, 3.63710771e-04, ] ) assert_array_almost_equal(mg.at_node["topographic__elevation"], z_trg)