Beispiel #1
0
def test_storms():
    input_file_string = os.path.join(_THIS_DIR, 'drive_sp_params_storms.txt')
    inputs = ModelParameterDictionary(input_file_string)
    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')

    mg = RasterModelGrid(nrows, ncols, dx)

    mg.add_zeros('topographic__elevation', at='node')
    z = mg.zeros(at='node')
    mg['node']['topographic__elevation'] = z + np.random.rand(len(z)) / 1000.
    mg.add_zeros('water__unit_flux_in', at='node')

    precip = PrecipitationDistribution(input_file=input_file_string)
    fr = FlowRouter(mg)
    sp = StreamPowerEroder(mg, **inputs)

    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()
            sp.run_one_step(dt)
        mg.at_node['topographic__elevation'][
            mg.core_nodes] += uplift * interval_duration
def test_sp_discharges_new():
    input_str = os.path.join(_THIS_DIR, 'test_sp_params_discharge_new.txt')
    inputs = ModelParameterDictionary(input_str, auto_type=True)
    nrows = 5
    ncols = 5
    dx = inputs.read_float('dx')
    dt = inputs.read_float('dt')

    mg = RasterModelGrid(nrows, ncols, dx)
    mg.add_zeros('topographic__elevation', at='node')
    z = np.array([5., 5., 0., 5., 5.,
                  5., 2., 1., 2., 5.,
                  5., 3., 2., 3., 5.,
                  5., 4., 4., 4., 5.,
                  5., 5., 5., 5., 5.])
    mg['node']['topographic__elevation'] = z

    fr = FlowRouter(mg)
    sp = StreamPowerEroder(mg, **inputs)

    # perform the loop (once!)
    for i in range(1):
        fr.route_flow()
        sp.run_one_step(dt)

    z_tg = np.array([5.        ,  5.        ,  0.        ,  5.        ,
                     5.        ,  5.        ,  1.47759225,  0.43050087,
                     1.47759225,  5.        ,  5.        ,  2.32883687,
                     1.21525044,  2.32883687,  5.        ,  5.        ,
                     3.27261262,  3.07175015,  3.27261262,  5.        ,
                     5.        ,  5.        ,  5.        ,  5.        ,
                     5.        ])

    assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
Beispiel #3
0
def setup():
    from six import StringIO

    _TEST_PARAM_DICT_FILE = u"""
    # A Comment
    INT_VAL:
    1

    FLOAT_VAL: # A Comment
    2.2
    STRING_VAL:
    The Landlab

    TRUE_BOOL_VAL:
    True
    FALSE_BOOL_VAL:
    False
    """

    param_file = StringIO(_TEST_PARAM_DICT_FILE)
    param_dict = ModelParameterDictionary()
    param_dict.read_from_file(param_file)
    globals().update({
        'param_dict': param_dict,
        '_TEST_PARAM_DICT_FILE': _TEST_PARAM_DICT_FILE
    })
def main():
    
    # INITIALIZE
    
    # Name of parameter input file
    input_file_name = 'test_inputs_for_diffusion_model.txt'
    
    # Open input file and read run-control parameters
    mpd = ModelParameterDictionary(input_file_name)
    run_duration = mpd.get('RUN_DURATION', ptype=float)
    
    # Create and initialize a grid
    mg = create_and_initialize_grid(mpd)
    
    # Create and initialize a diffusion component
    dc = LinearDiffuser(mg)
    dc.initialize(mpd)
    
    # RUN
    
    # Run the diffusion component until it's time for the next output
    dc.run_until(run_duration)
    
    # FINALIZE
    
    # Display results to screen
    mg.imshow('node', 'landscape_surface__elevation')
    import pylab
    pylab.show()
    
    from landlab.io.netcdf import write_netcdf
    write_netcdf('diffusion_example.nc', mg)
Beispiel #5
0
 def initialize(self, input_stream=None):
     
     # Create a ModelParameterDictionary for the inputs
     if input_stream is None:
         inputs = None
     elif type(input_stream)==ModelParameterDictionary:
         inputs = input_stream
     else:
         inputs = ModelParameterDictionary(input_stream)
     
     # Make sure the grid includes elevation data. This means either:
     #  1. The grid has a node field called 'topographic__elevation', or
     #  2. The input file has an item called 'ELEVATION_FIELD_NAME' *and*
     #     a field by this name exists in the grid.
     try:
         self._elev = self._grid.at_node['topographic__elevation']
     except FieldError:
         try:
             topo_field_name = inputs.read_string('ELEVATION_FIELD_NAME')
         except AttributeError:
             print('Error: Because your grid does not have a node field called')
             print('"topographic__elevation", you need to pass the name of')
             print('a text input file or ModelParameterDictionary, and this')
             print('file or dictionary needs to include the name of another')
             print('field in your grid that contains your elevation data.')
             raise AttributeError
         except MissingKeyError:
             print('Error: Because your grid does not have a node field called')
             print('"topographic__elevation", your input file (or')
             print('ModelParameterDictionary) must include an entry with the')
             print('key "ELEVATION_FIELD_NAME", which gives the name of a')
             print('field in your grid that contains your elevation data.')
             raise MissingKeyError('ELEVATION_FIELD_NAME')
         try:
             self._elev = self._grid.at_node[topo_field_name]
         except AttributeError:
             print('Your grid does not seem to include a node field called',topo_field_name)
             
     # Create output variables.
     #
     # Note that we initialize depression depth to -1 (negative values make
     # no sense, so this is a clue to non-flooded nodes), and depression
     # outlet ID to BAD_INDEX_VALUE (which is a major clue!)
     self.depression_depth = self._grid.add_zeros('node', 'depression__depth') - 1.0             
     self.depression_outlet = self._grid.add_zeros('node', 'depression__outlet_node_id', dtype=int) + BAD_INDEX_VALUE               
             
     # Later on, we'll need a number that's guaranteed to be larger than the
     # highest elevation in the grid.
     self._BIG_ELEV = numpy.amax(self._elev)+1
     
     # We'll also need a handy copy of the node neighbor lists
     # TODO: presently, this grid method seems to only exist for Raster grids.
     # We need it for *all* grids!
     self._node_nbrs = self._grid.get_neighbor_list()
     if type(self._grid) is landlab.grid.raster.RasterModelGrid:
         diag_nbrs = self._grid.get_diagonal_list()
         self._node_nbrs = numpy.concatenate((self._node_nbrs, diag_nbrs), 1)
Beispiel #6
0
    def initialize(self, input_stream=None):
        """
        The BMI-style initialize method takes an optional input_stream
        parameter, which may be either a ModelParameterDictionary object or
        an input stream from which a ModelParameterDictionary can read values.
        """
        # Create a ModelParameterDictionary for the inputs
        if input_stream is None:
            inputs = None
        elif type(input_stream) == ModelParameterDictionary:
            inputs = input_stream
        else:
            inputs = ModelParameterDictionary(input_stream)

        # Make sure the grid includes elevation data. This means either:
        #  1. The grid has a node field called 'topographic__elevation', or
        #  2. The input file has an item called 'ELEVATION_FIELD_NAME' *and*
        #     a field by this name exists in the grid.
        try:
            self._elev = self._grid.at_node["topographic__elevation"]
        except FieldError:
            try:
                self.topo_field_name = inputs.read_string("ELEVATION_" + "FIELD_NAME")
            except AttributeError:
                print("Error: Because your grid does not have a node field")
                print('called "topographic__elevation", you need to pass the')
                print("name of a text input file or ModelParameterDictionary,")
                print("and this file or dictionary needs to include the name")
                print("of another field in your grid that contains your")
                print("elevation data.")
                raise AttributeError
            except MissingKeyError:
                print("Error: Because your grid does not have a node field")
                print('called "topographic__elevation", your input file (or')
                print("ModelParameterDictionary) must include an entry with")
                print('the key "ELEVATION_FIELD_NAME", which gives the name')
                print("of a field in your grid that contains your elevation")
                print("data.")
                raise MissingKeyError("ELEVATION_FIELD_NAME")
            try:
                self._elev = self._grid.at_node[self.topo_field_name]
            except AttributeError:
                print(
                    "Your grid does not seem to have a node field called",
                    self.topo_field_name,
                )
        else:
            self.topo_field_name = "topographic__elevation"
        # create the only new output field:
        self.sed_fill_depth = self._grid.add_zeros(
            "node", "sediment_fill__depth", noclobber=False
        )

        self._lf = DepressionFinderAndRouter(self._grid, routing=self._routing)
        self._fr = FlowAccumulator(self._grid, flow_director=self._routing)
Beispiel #7
0
def test_tl_fluvial():
    input_file = os.path.join(_THIS_DIR, 'stream_power_params_ideal.txt')
    inputs = ModelParameterDictionary(input_file)
    nrows = inputs.read_int('nrows')
    ncols = inputs.read_int('ncols')
    dx = inputs.read_float('dx')
    leftmost_elev = inputs.read_float('leftmost_elevation')
    initial_slope = inputs.read_float('initial_slope')
    uplift_rate = inputs.read_float('uplift_rate')

    runtime = inputs.read_float('total_time')
    dt = inputs.read_float('dt')

    nt = int(runtime // dt)
    uplift_per_step = uplift_rate * dt

    mg = RasterModelGrid(nrows, ncols, dx)
    mg.add_zeros('node', 'topographic__elevation')
    z = np.loadtxt(os.path.join(_THIS_DIR, 'tl_init.txt'))
    mg['node']['topographic__elevation'] = z

    mg.set_closed_boundaries_at_grid_edges(True, False, True, False)
    mg.set_fixed_value_boundaries_at_grid_edges(
        False, True, False, True, value_of='topographic__elevation')

    fr = FlowRouter(mg)
    tl = TransportLimitedEroder(mg, input_file)

    for i in range(nt):
        mg.at_node['topographic__elevation'][mg.core_nodes] += uplift_per_step
        mg = fr.route_flow()
        mg, _ = tl.erode(mg, dt, stability_condition='loose')

    z_tg = np.loadtxt(os.path.join(_THIS_DIR, 'tlz_tg.txt'))
    assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
    def __init__(self, input_file=None, mean_storm=None, mean_interstorm=None, mean_storm_depth=None, total_t=None, delta_t=None):
        """ This reads in information from the input_file (either default or user
            assigned, and creates an instantaneous storm event drawn from the Poisson distribution
        """
        
        # First we create an instance of the Model Parameter Dictionary
        MPD = ModelParameterDictionary()
        
        # If no input_file is given,the default file is used
        if input_file is None:
            input_file = _DEFAULT_INPUT_FILE
            
        # This reads in the file information    
        MPD.read_from_file(input_file)       
        
        # And now we set our different parameters 
        # using the model parameter dictionary...
        if mean_storm == None:
            self.mean_storm = MPD.read_float( 'MEAN_STORM')
        else:
            self.mean_storm = mean_storm
        
        if mean_interstorm == None:
            self.mean_interstorm = MPD.read_float( 'MEAN_INTERSTORM')
        else:
            self.mean_interstorm =mean_interstorm
            
        if mean_storm_depth== None:
            self.mean_storm_depth = MPD.read_float( 'MEAN_DEPTH')
        else:
            self.mean_storm_depth =mean_storm_depth
            
        if total_t== None:
            self.run_time = MPD.read_float( 'RUN_TIME')
        else:
            self.run_time =total_t
            
        if delta_t== None:
            self.delta_t = MPD.read_int( 'DELTA_T')
        else:
            self.detla_t =delta_t
            
        # Mean_intensity is not set by the MPD, but can be drawn from 
        # the mean storm depth and mean storm duration.
        self.mean_intensity = self.mean_storm_depth / self.mean_storm

        # If a time series is created later, this blank list will be used.
        self.storm_time_series =[]

        # Given the mean values assigned above using either the model
        # parameter dictionary or the init function, we can call the
        # different methods to assign values from the Poisson distribution.
        
        self.storm_duration = self.get_precipitation_event_duration()
        self.interstorm_duration = self.get_interstorm_event_duration()
        self.storm_depth = self.get_storm_depth()
        self.intensity = self.get_storm_intensity()
    def initialize( self ):

        MPD = ModelParameterDictionary()
        MPD.read_from_file(_DEFAULT_INPUT_FILE)
        
        self._vegcover = MPD.read_float( 'VEG_COV' )
        self._WUE = MPD.read_float( 'WUE' )
        self._LAI_max = MPD.read_float( 'LAI_MAX' )
        self._cb = MPD.read_float( 'CB' )
        self._cd = MPD.read_float( 'CD' )
        self._ksg = MPD.read_float( 'KSG' )
        self._kdd = MPD.read_float( 'KDD' )
        self._Blive_ini = MPD.read_float( 'BLIVE_INI' )
        self._Bdead_ini = MPD.read_float( 'BDEAD_INI' )
Beispiel #10
0
def test_read_file_name(pdict_setup):
    (prm_fd, prm_file_name) = tempfile.mkstemp()

    prm_file = os.fdopen(prm_fd, "w")
    prm_file.write(pdict_setup.param_dict_file)
    prm_file.close()

    param_dict = ModelParameterDictionary()
    param_dict.read_from_file(prm_file_name)

    os.remove(prm_file_name)

    all_keys = set(
        ["FLOAT_VAL", "INT_VAL", "STRING_VAL", "TRUE_BOOL_VAL", "FALSE_BOOL_VAL"]
    )
    param_list = set(param_dict.params())

    assert param_list == all_keys
Beispiel #11
0
def test_read_file_name():
    (prm_fd, prm_file_name) = tempfile.mkstemp()

    prm_file = os.fdopen(prm_fd, 'w')
    prm_file.write(_TEST_PARAM_DICT_FILE)
    prm_file.close()

    param_dict = ModelParameterDictionary()
    param_dict.read_from_file(prm_file_name)

    os.remove(prm_file_name)

    all_keys = set([
        'FLOAT_VAL', 'INT_VAL', 'STRING_VAL', 'TRUE_BOOL_VAL',
        'FALSE_BOOL_VAL',
    ])
    param_list = set(param_dict.params())

    assert_equal(param_list, all_keys)
    def initialize(self, grid, input_stream):

        # Create a ModelParameterDictionary for the inputs
        if type(input_stream)==ModelParameterDictionary:
            inputs = input_stream
        else:
            inputs = ModelParameterDictionary(input_stream)

        # Read input/configuration parameters
        self.m = inputs.get('M_EXPONENT', ptype=float)
        self.n = inputs.get('N_EXPONENT', ptype=float)
        self.K = inputs.get('K_COEFFICIENT', ptype=float)
        self.rainfall_myr = inputs.get('RAINFALL_RATE_M_PER_YEAR', ptype=float)
        self.rain_duration_yr = inputs.get('RAIN_DURATION_YEARS', ptype=float)
        self.frac = 0.9 #for time step calculations

        #print "Rainfall duration ", self.rain_duration

        # Set up state variables
        self.I = grid.zeros(centering='node')    # incision rate (M/Y)
 def initialize(self, grid, input_stream, intensity=None, stormduration=None):
     
     # Create a ModelParameterDictionary for the inputs
     if type(input_stream)==ModelParameterDictionary:
         inputs = input_stream
     else:
         inputs = ModelParameterDictionary(input_stream)
     
     # Read input/configuration parameters
     self.m_n = inputs.get('MANNINGS_N', ptype=float)
     
     #NG do a check to make sure Manning's n is an appropriate values
     if intensity == None:
         self.rainfall_mmhr = inputs.get('RAINFALL_RATE', ptype=float)
     else:
         self.rainfall_mmhr = intensity
     
     if stormduration == None:
         self.rain_duration = inputs.get('RAIN_DURATION', ptype=float)
     else:
         self.rain_duration = stormduration
       
     self.h_init = 0.001        # initial thin layer of water (m)
     self.g = 9.8               # gravitational acceleration (m/s2)
     self.alpha = 0.2           # time-step factor (ND; from Bates et al., 2010)
     self.m_n_sq = self.m_n*self.m_n # manning's n squared
     self.rho = 1000 # densith of water, kg/m^3
           
     
     
     # Derived parameters
     self.rainfall_rate = (self.rainfall_mmhr/1000.)/3600.  # rainfall in m/s
     self.ten_thirds = 10./3.   # pre-calculate 10/3 for speed
     
     # Set up state variables
     
     self.hstart = grid.zeros(centering='node') + self.h_init 
     self.h = grid.zeros(centering='node') + self.h_init     # water depth (m)
     #NG why is water depth at each node and not at cells, which are only active
     self.q = grid.zeros(centering='active_link') # unit discharge (m2/s)
     self.dhdt = grid.zeros(centering='active_cell') # rate of water-depth change
def test_sp_widths():
    input_str = os.path.join(_THIS_DIR, 'test_sp_params_widths.txt')
    inputs = ModelParameterDictionary(input_str, auto_type=True)
    nrows = 5
    ncols = 5
    dx = inputs.read_float('dx')
    dt = inputs.read_float('dt')

    mg = RasterModelGrid(nrows, ncols, dx)
    widths = np.ones(mg.number_of_nodes, dtype=float)
    mg.add_zeros('topographic__elevation', at='node')
    z = np.array([5., 5., 0., 5., 5.,
                  5., 2., 1., 2., 5.,
                  5., 3., 2., 3., 5.,
                  5., 4., 4., 4., 5.,
                  5., 5., 5., 5., 5.])
    mg['node']['topographic__elevation'] = z

    fr = FlowRouter(mg)
    sp = StreamPowerEroder(mg, use_W=widths, **inputs)

    # perform the loop (once!)
    for i in range(1):
        fr.route_flow()
        sqrt_A = mg.at_node['drainage_area']**0.5
        widths[mg.core_nodes] = sqrt_A[mg.core_nodes]/sqrt_A[
            mg.core_nodes].mean()
        # so widths has mean=1.
        # note the issue with drainage_area not defined at perimeter => nans
        # if not careful...
        sp.run_one_step(dt)

    z_tg = np.array([ 5.        ,  5.        ,  0.        ,  5.        ,
                      5.        ,  5.        ,  1.37222369,  0.36876358,
                      1.37222369,  5.        ,  5.        ,  2.17408606,
                      1.07986038,  2.17408606,  5.        ,  5.        ,
                      3.08340277,  2.85288049,  3.08340277,  5.        ,
                      5.        ,  5.        ,  5.        ,  5.        ,
                      5.        ])

    assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
 def __init__(self, grid, input_stream):
     self.grid = grid
     inputs = ModelParameterDictionary(input_stream)
     
     #User sets:
     self.K = inputs.read_float('K_sp')
     self.m = inputs.read_float('m_sp')
     try:
         self.n = inputs.read_float('n_sp')
     except:
         self.n = 1.
     try:
         self.dt = inputs.read_float('dt')
     except: #if dt isn't supplied, it must be set by another module, so look in the grid
         print 'Set dynamic timestep from the grid. You must call gear_timestep() to set dt each iteration.'
     else:
         try:
             self.r_i = inputs.read_float('rainfall_intensity')
         except:
             self.r_i = 1.
     try:
         self.value_field = inputs.read_str('value_field')
     except:
         self.value_field = 'planet_surface__elevation'
         
     #make storage variables
     self.A_to_the_m = grid.create_node_array_zeros()
     self.alpha = grid.empty(centering='node')
     
     if self.n != 1.:
         raise ValueError('The Braun Willett stream power algorithm requires n==1. at the moment, sorry...')
 def __init__(self, grid, input_stream):
     self.grid = grid
     inputs = ModelParameterDictionary(input_stream)
     
     #User sets:
     try:
         self.K = inputs.read_float('K_sp')
     except ParameterValueError:
         self.use_K = True
     else:
         self.use_K = False
         
     self.m = inputs.read_float('m_sp')
     try:
         self.n = inputs.read_float('n_sp')
     except:
         self.n = 1.
     try:
         self.dt = inputs.read_float('dt')
     except: #if dt isn't supplied, it must be set by another module, so look in the grid
         print('Set dynamic timestep from the grid. You must call gear_timestep() to set dt each iteration.')
     try:
         self.r_i = inputs.read_float('rainfall_intensity')
     except:
         self.r_i = 1.
     try:
         self.value_field = inputs.read_str('value_field')
     except:
         self.value_field = 'topographic__elevation'
         
     #make storage variables
     self.A_to_the_m = grid.create_node_array_zeros()
     self.alpha = grid.empty(centering='node')
     self.alpha_by_flow_link_lengthtothenless1 = numpy.empty_like(self.alpha)
     
     self.grid.node_diagonal_links() #calculates the number of diagonal links
     
     if self.n != 1.:
         #raise ValueError('The Braun Willett stream power algorithm requires n==1. at the moment, sorry...')
         self.nonlinear_flag = True
         if self.n<1.:
             print("***WARNING: With n<1 performance of the Fastscape algorithm is slow!***")
     else:
         self.nonlinear_flag = False
     
     def func_for_newton(x, last_step_elev, receiver_elev, alpha_by_flow_link_lengthtothenless1, n):
         y = x - last_step_elev + alpha_by_flow_link_lengthtothenless1*(x-receiver_elev)**n
         return y
     
     def func_for_newton_diff(x, last_step_elev, receiver_elev, alpha_by_flow_link_lengthtothenless1, n):
         y = 1. + n*alpha_by_flow_link_lengthtothenless1*(x-receiver_elev)**(n-1.)
         return y
     
     self.func_for_newton = func_for_newton
     self.func_for_newton_diff = func_for_newton_diff
    def __init__(self, grid, input_stream):
        self.grid = grid
        inputs = ModelParameterDictionary(input_stream)

        # User sets:
        self.K = inputs.read_float("K_sp")
        self.m = inputs.read_float("m_sp")
        try:
            self.n = inputs.read_float("n_sp")
        except:
            self.n = 1.0
        try:
            self.dt = inputs.read_float("dt")
        except:  # if dt isn't supplied, it must be set by another module, so look in the grid
            print "Set dynamic timestep from the grid. You must call gear_timestep() to set dt each iteration."
        else:
            try:
                self.r_i = inputs.read_float("dt")
            except:
                self.r_i = 1.0
        try:
            self.value_field = inputs.read_str("value_field")
        except:
            self.value_field = "planet_surface__elevation"

        # make storage variables
        self.A_to_the_m = grid.create_node_array_zeros()
        self.alpha = grid.empty(centering="node")

        if self.n != 1.0:
            raise ValueError("The Braun Willett stream power algorithm requires n==1. at the moment, sorry...")
Beispiel #18
0
    def initialize(self, input_stream):
        
        # Create a ModelParameterDictionary for the inputs
        if type(input_stream)==ModelParameterDictionary:
            inputs = input_stream
        else:
            inputs = ModelParameterDictionary(input_stream)
        
        # Read input/configuration parameters
        self.kd = inputs.read_float('DIFMOD_KD')
        try:
            self.uplift_rate = inputs.read_float('DIFMOD_UPLIFT_RATE')
        except:
            self.uplift_rate = inputs.read_float('uplift_rate')
        try:
            self.values_to_diffuse = inputs.read_str('values_to_diffuse')
        except:
            self.values_to_diffuse = 'planet_surface__elevation'
        try:
            self.timestep_in = inputs.read_float('dt')  
        except:
            print 'No fixed timestep supplied, it must be set dynamically somewhere else. Be sure to call input_timestep(timestep_in) as part of your run loop.'

        
        # Create grid if one doesn't already exist
        if self.grid==None:
            self.grid = create_and_initialize_grid(input_stream)
            
        # Set internal time step
        # ..todo:
        #   implement mechanism to compute time-steps dynamically if grid is 
        #   adaptive/changing
        dx = self.grid.min_active_link_length()  # smallest active link length
        self.dt = _ALPHA*dx*dx/self.kd  # CFL condition
        try:
            self.tstep_ratio = self.timestep_in/self.dt
        except:
            pass
        
        # Get a list of interior cells
        self.interior_cells = self.grid.get_core_cell_node_ids()
        
        # Here we're experimenting with different approaches: with 
        # 'make_all_data', we create and manage all the data we need and embed
        # it all in the grid. With 'explicit', we require the caller/user to 
        # provide data.
        if _VERSION=='make_all_data':
            #print 'creating internal data'
            self.z = self.grid.add_zeros('node', 'landscape_surface__elevation')
            self.g = self.grid.add_zeros('active_link', 'landscape_surface__gradient')  # surface gradients
            self.qs = self.grid.add_zeros('active_link','unit_sediment_flux')  # unit sediment flux
            self.dqds = self.grid.add_zeros('node', 'sediment_flux_divergence')  # sed flux derivative
        elif _VERSION=='explicit':
            pass
        else:
            # Create data arrays for variables that won't (?) be shared with other
            # components
            self.g = self.grid.create_active_link_array_zeros()  # surface gradients
            self.qs = self.grid.create_active_link_array_zeros()  # unit sediment flux
            self.dqds = self.grid.create_node_array_zeros()  # sed flux derivative
Beispiel #19
0
def test_sed_dep():
    input_file = os.path.join(_THIS_DIR, "sed_dep_params.txt")
    inputs = ModelParameterDictionary(input_file, auto_type=True)
    nrows = inputs.read_int("nrows")
    ncols = inputs.read_int("ncols")
    dx = inputs.read_float("dx")
    uplift_rate = inputs.read_float("uplift_rate")

    runtime = inputs.read_float("total_time")
    dt = inputs.read_float("dt")

    nt = int(runtime // dt)
    uplift_per_step = uplift_rate * dt

    mg = RasterModelGrid((nrows, ncols), xy_spacing=(dx, dx))

    mg.add_zeros("topographic__elevation", at="node")
    z = np.loadtxt(os.path.join(_THIS_DIR, "seddepinit.txt"))
    mg["node"]["topographic__elevation"] = z

    mg.set_closed_boundaries_at_grid_edges(True, False, True, False)

    fr = FlowAccumulator(mg, flow_director="D8")
    sde = SedDepEroder(mg, **inputs)

    for i in range(nt):
        mg.at_node["topographic__elevation"][mg.core_nodes] += uplift_per_step
        mg = fr.run_one_step()
        mg, _ = sde.erode(dt)

    z_tg = np.loadtxt(os.path.join(_THIS_DIR, "seddepz_tg.txt"))

    assert_array_almost_equal(
        mg.at_node["topographic__elevation"][mg.core_nodes], z_tg[mg.core_nodes]
    )
    def __init__(self, grid, input_stream):
        #grid here is a true model field. i.e., we should be able to do grid.at_node['topographic__elevation']
        #input_stream is a text file, entered in format './my_file.txt'
        self.grid = grid

        inputs = ModelParameterDictionary(input_stream)
        self.n = inputs.read_float('n')              # roughness coefficient (Manning's n)
        self.g = inputs.read_float('g')               # gravitational acceleration (m/s2)
        self.alpha = inputs.read_float('alpha')       # time-step factor (ND; from Bates et al., 2010)
        self.tau_crit = inputs.read_float('tau_crit') # critical shear stress, pascals
        self.mpm = inputs.read_float('mpm')          # sed trans coefficient
        self.erode_start_time = inputs.read_float('erode_start_time') # allows an offset between when flow starts and when we start to allow erosion


        #test the necessary fields are all already present:
        try:
            self.z = grid.at_node['topographic__elevation']
        except:
            warnings.warn('elevations not found in grid!')
        try:
            self.h = grid.at_node['planet_surface__water_depth']
        except:
            warnings.warn('initial water depths not found in grid!')

        #build the internally necessary params:
        self.rhog = 9810.          # water unit weight, kg/m2s2 (N/m3)
        self.q = grid.zeros(at='active_link')       # unit discharge (m2/s)
        self.dhdt = grid.zeros(at='node')           # rate of water-depth change
        self.tau = grid.zeros(at='active_link')     # shear stress (Pa)
        self.qs = grid.zeros(at='active_link')      # sediment flux (m2/s)
        self.dqsds = grid.zeros(at='node')
        self.dzdt = self.dhdt
        self.dzaccum = grid.zeros(at='node')
        self.zm = grid.zeros(at='node')
        self.zm[:] = self.z[:]
def test_sp_discharges():
    input_str = os.path.join(_THIS_DIR, 'test_sp_params_discharge.txt')
    inputs = ModelParameterDictionary(input_str)
    nrows = 5
    ncols = 5
    dx = inputs.read_float('dx')
    dt = inputs.read_float('dt')

    mg = RasterModelGrid(nrows, ncols, dx)
    mg.add_zeros('topographic__elevation', at='node')
    z = np.array([5., 5., 0., 5., 5.,
                  5., 2., 1., 2., 5.,
                  5., 3., 2., 3., 5.,
                  5., 4., 4., 4., 5.,
                  5., 5., 5., 5., 5.])
    mg['node']['topographic__elevation'] = z

    fr = FlowRouter(mg)
    sp = StreamPowerEroder(mg, input_str)

    # perform the loop (once!)
    for i in range(1):
        fr.route_flow(method='D8')
        my_Q = mg.at_node['water__volume_flux'] * 1.
        sp.erode(mg, dt, node_drainage_areas='drainage_area',
                 slopes_at_nodes='topographic__steepest_slope',
                 Q_if_used=my_Q)

    z_tg = np.array([5.00000000e+00,   5.00000000e+00,   0.00000000e+00,
                     5.00000000e+00,   5.00000000e+00,   5.00000000e+00,
                     1.29289322e+00,   1.00000000e-06,   1.29289322e+00,
                     5.00000000e+00,   5.00000000e+00,   2.29289322e+00,
                     1.00000000e+00,   2.29289322e+00,   5.00000000e+00,
                     5.00000000e+00,   3.29289322e+00,   3.00000000e+00,
                     3.29289322e+00,   5.00000000e+00,   5.00000000e+00,
                     5.00000000e+00,   5.00000000e+00,   5.00000000e+00,
                     5.00000000e+00])

    assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
Beispiel #22
0
def test_read_file_like_twice():
    from six import StringIO
    param_file = StringIO(_TEST_PARAM_DICT_FILE)
    param_dict_1 = ModelParameterDictionary()
    param_dict_2 = ModelParameterDictionary()

    param_dict_1.read_from_file(param_file)
    param_dict_2.read_from_file(param_file)
Beispiel #23
0
def test_storms():
    input_file_string = os.path.join(_THIS_DIR, 'drive_sp_params_storms.txt')
    inputs = ModelParameterDictionary(input_file_string, 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')

    mean_duration  = inputs.read_float('mean_storm')
    mean_interstorm  = inputs.read_float('mean_interstorm')
    mean_depth = inputs.read_float('mean_depth')

    storm_run_time  = inputs.read_float('storm_run_time')
    delta_t  = inputs.read_float('delta_t')
    mg = RasterModelGrid(nrows, ncols, dx)

    mg.add_zeros('topographic__elevation', at='node')
    z = mg.zeros(at='node')
    mg['node']['topographic__elevation'] = z + np.random.rand(len(z)) / 1000.
    mg.add_zeros('water__unit_flux_in', at='node')

    precip = PrecipitationDistribution(mean_storm_duration = mean_duration,
                                mean_interstorm_duration = mean_interstorm,
                                mean_storm_depth = mean_depth,
                                total_t = storm_run_time, delta_t = delta_t)
    fr = FlowAccumulator(mg, flow_director='D8')
    sp = StreamPowerEroder(mg, **inputs)

    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)
            fr.run_one_step()
            sp.run_one_step(dt)
        mg.at_node['topographic__elevation'][
            mg.core_nodes] += uplift * interval_duration
Beispiel #24
0
def test_storms():
    input_file_string = os.path.join(_THIS_DIR, "drive_sp_params_storms.txt")
    inputs = ModelParameterDictionary(input_file_string, auto_type=True)
    nrows = inputs.read_int("nrows")
    ncols = inputs.read_int("ncols")
    dx = inputs.read_float("dx")
    dt = inputs.read_float("dt")
    uplift = inputs.read_float("uplift_rate")

    mean_duration = inputs.read_float("mean_storm")
    mean_interstorm = inputs.read_float("mean_interstorm")
    mean_depth = inputs.read_float("mean_depth")

    storm_run_time = inputs.read_float("storm_run_time")
    delta_t = inputs.read_float("delta_t")
    mg = RasterModelGrid(nrows, ncols, xy_spacing=dx)

    mg.add_zeros("topographic__elevation", at="node")
    z = mg.zeros(at="node")
    mg["node"]["topographic__elevation"] = z + np.random.rand(len(z)) / 1000.
    mg.add_zeros("water__unit_flux_in", at="node")

    precip = PrecipitationDistribution(
        mean_storm_duration=mean_duration,
        mean_interstorm_duration=mean_interstorm,
        mean_storm_depth=mean_depth,
        total_t=storm_run_time,
        delta_t=delta_t,
    )
    fr = FlowAccumulator(mg, flow_director="D8")
    sp = StreamPowerEroder(mg, **inputs)

    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)
            fr.run_one_step()
            sp.run_one_step(dt)
        mg.at_node["topographic__elevation"][mg.core_nodes] += (
            uplift * interval_duration
        )
Beispiel #25
0
 def initialize(self, input_stream=None):
 
     # If no input file/stream specified, use default (or command line?)
     # (or default values?)
     if input_stream==None:
         input_stream = str(raw_input('Enter name of input file: '))
     
     # Open input file
     inputs = ModelParameterDictionary(input_stream)
 
     # Create grid
     self.grid = create_and_initialize_grid(inputs)
     
     # Create a diffusion component
     self.diffusion_component = diffusion.DiffusionComponent(self.grid)
     self.diffusion_component.initialize(input_stream)
     
     # Read parameters
     self.run_duration = inputs.get('RUN_DURATION', ptype=float)
     self.opt_netcdf_output = inputs.get('OPT_FILE_OUTPUT', ptype='bool')
     self.opt_display_output = inputs.get('OPT_DISPLAY_OUTPUT', ptype='bool')
     
     self.setup_output_timing(inputs)
Beispiel #26
0
    def initialize( self ):
        
        MPD = ModelParameterDictionary()
        MPD.read_from_file( _DEFAULT_INPUT_FILE )

        # Reading Input Parameters
        self._N = MPD.read_float( 'CLOUDINESS' )        
        self._latitude = MPD.read_float( 'LATITUDE' )
        self._A = MPD.read_float( 'ALBEDO' )
Beispiel #27
0
def test_read_file_like_twice(pdict_setup):
    from six import StringIO

    param_file = StringIO(pdict_setup.param_dict_file)
    param_dict_1 = ModelParameterDictionary()
    param_dict_2 = ModelParameterDictionary()

    param_dict_1.read_from_file(param_file)
    param_dict_2.read_from_file(param_file)
def test_sp_discharges_old():
    input_str = os.path.join(_THIS_DIR, 'test_sp_params_discharge.txt')
    inputs = ModelParameterDictionary(input_str)
    nrows = 5
    ncols = 5
    dx = inputs.read_float('dx')
    dt = inputs.read_float('dt')

    mg = RasterModelGrid(nrows, ncols, dx)
    mg.add_zeros('topographic__elevation', at='node')
    z = np.array([5., 5., 0., 5., 5.,
                  5., 2., 1., 2., 5.,
                  5., 3., 2., 3., 5.,
                  5., 4., 4., 4., 5.,
                  5., 5., 5., 5., 5.])
    mg['node']['topographic__elevation'] = z

    fr = FlowAccumulator(mg, flow_director='D8')
    my_Q = mg.at_node['surface_water__discharge']
    sp = StreamPowerEroder(mg, input_str, use_Q=my_Q)

    # perform the loop (once!)
    for i in range(1):
        fr.run_one_step()
        my_Q[:] = mg.at_node['surface_water__discharge'] * 1.
        sp.run_one_step(dt)

    z_tg = np.array([5.        ,  5.        ,  0.        ,  5.        ,
                     5.        ,  5.        ,  1.47759225,  0.43050087,
                     1.47759225,  5.        ,  5.        ,  2.32883687,
                     1.21525044,  2.32883687,  5.        ,  5.        ,
                     3.27261262,  3.07175015,  3.27261262,  5.        ,
                     5.        ,  5.        ,  5.        ,  5.        ,
                     5.        ])

    assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
def test_sp_discharges_new():
    input_str = os.path.join(_THIS_DIR, 'test_sp_params_discharge_new.txt')
    inputs = ModelParameterDictionary(input_str)
    nrows = 5
    ncols = 5
    dx = inputs.read_float('dx')
    dt = inputs.read_float('dt')

    mg = RasterModelGrid(nrows, ncols, dx)
    mg.add_zeros('topographic__elevation', at='node')
    z = np.array([5., 5., 0., 5., 5.,
                  5., 2., 1., 2., 5.,
                  5., 3., 2., 3., 5.,
                  5., 4., 4., 4., 5.,
                  5., 5., 5., 5., 5.])
    mg['node']['topographic__elevation'] = z

    fr = FlowRouter(mg)
    sp = StreamPowerEroder(mg, **inputs)

    # perform the loop (once!)
    for i in range(1):
        fr.route_flow()
        sp.run_one_step(dt)

    z_tg = np.array([5.00000000e+00,   5.00000000e+00,   0.00000000e+00,
                     5.00000000e+00,   5.00000000e+00,   5.00000000e+00,
                     1.29289322e+00,   1.00000000e-06,   1.29289322e+00,
                     5.00000000e+00,   5.00000000e+00,   2.29289322e+00,
                     1.00000000e+00,   2.29289322e+00,   5.00000000e+00,
                     5.00000000e+00,   3.29289322e+00,   3.00000000e+00,
                     3.29289322e+00,   5.00000000e+00,   5.00000000e+00,
                     5.00000000e+00,   5.00000000e+00,   5.00000000e+00,
                     5.00000000e+00])

    assert_array_almost_equal(mg.at_node['topographic__elevation'], z_tg)
 def initialize(self, grid=None, input_file=None, intensity=None, stormduration=None):
     
     self.grid = grid 
     if self.grid==None:
         self.grid = create_and_initialize_grid(input_file)
         
     ##self.current_time = current_time <- this isn't being used yet. 
     
     # Create a ModelParameterDictionary for the inputs
     
     MPD = ModelParameterDictionary()
     ## I am not sure we want to use an instance of MPD as an input_file. I'm confused about this. SO for 
     ## now I changed this to reflect other component types
     
     if input_file is None:
         input_file = _DEFAULT_INPUT_FILE
         
     MPD.read_from_file(input_file)
     
     ## This was the old way... where an instance of MPD is an input_file
     #if type(input_file)==MPD: 
     #    inputs = input_file
     #else:
     #    inputs = MPD(input_file)
     
     # Read input/configuration parameters
     self.m_n = MPD.read_float('MANNINGS_N')
     
     if intensity == None:
         self.rainfall_mmhr = MPD.read_float( 'RAINFALL_RATE')
     else:
         self.rainfall_mmhr = intensity
     
     if stormduration == None:
         self.rain_duration = MPD.read_float( 'RAIN_DURATION' )
     else:
         self.rain_duration = stormduration
       
     #self.h_init = 0.001        # initial thin layer of water (m)
     #self.g = 9.8               # gravitational acceleration (m/s2)
     #self.alpha = 0.2           # time-step factor (ND; from Bates et al., 2010)
     #self.m_n_sq = self.m_n*self.m_n # manning's n squared
     #self.rho = 1000 # density of water, kg/m^3     
     
     # Derived parameters ## THIS IS IMPORTANT - UNITS!!!! Double check precip component...
     self.rainfall_rate = (self.rainfall_mmhr/1000.)/3600.  # rainfall in m/s
     
     
     # Set up state variables
     
     self.hstart = grid.zeros(centering='node') + self.h_init 
     self.h = grid.zeros(centering='node') + self.h_init     # water depth (m)
     #NG why is water depth at each node and not at cells, which are only active
     self.q = grid.zeros(centering='active_link') # unit discharge (m2/s)
     self.dhdt = grid.zeros(centering='active_cell') # rate of water-depth change
Beispiel #31
0
from time import time

import numpy as np
import pylab
from pylab import colorbar, figure, loglog, plot, show
from six.moves import range

from landlab import ModelParameterDictionary, RasterModelGrid
from landlab.components import FlowAccumulator, SedDepEroder
from landlab.plot import channel_profile as prf, imshow
from landlab.plot.imshow import imshow_node_grid
from landlab.plot.video_out import VideoPlotter

# get the needed properties to build the grid:
input_file = "./sed_dep_params_reliable.txt"  # './sed_dep_NMGparams2.txt'
inputs = ModelParameterDictionary(input_file)
nrows = inputs.read_int("nrows")
ncols = inputs.read_int("ncols")
dx = inputs.read_float("dx")
leftmost_elev = inputs.read_float("leftmost_elevation")
initial_slope = inputs.read_float("initial_slope")
uplift_rate = inputs.read_float("uplift_rate")

runtime = inputs.read_float("total_time")
dt = inputs.read_float("dt")

nt = int(runtime // dt)
uplift_per_step = uplift_rate * dt
print("uplift per step: ", uplift_per_step)

# instantiate the grid object
from landlab.components.flow_routing import FlowRouter
from landlab.components.stream_power import FastscapeEroder
from landlab import ModelParameterDictionary
from landlab.plot import channel_profile as prf

from landlab import RasterModelGrid, FIXED_VALUE_BOUNDARY
import numpy as np
import pylab

from time import time

import random

#get the needed properties to build the grid:
input_file = './drive_sp_params.txt'
inputs = ModelParameterDictionary(input_file)
nrows = inputs.read_int('nrows')
ncols = inputs.read_int('ncols')
dx = inputs.read_float('dx')
uplift_rate = inputs.read_float('uplift_rate')
runtime = inputs.read_float('run_time')
dt = inputs.read_float('dt')
nt = int(runtime // dt)
uplift_per_step = uplift_rate * dt

#instantiate the grid object
mg = RasterModelGrid(nrows, ncols, dx)

boundary_node_list = mg.boundary_nodes

#set up its boundary conditions (bottom, right, top, left is inactive)
    def initialize(self, grid, params_file):
        '''
        params_file is the name of the text file containing the parameters
        needed for this stream power component.

        ***Parameters for input file***
        OBLIGATORY:
            * Qc -> String. Controls how to set the carrying capacity.
                Either 'MPM', or a string giving the name of the model field
                where capacity values are stored on nodes.
                At the moment, only 'MPM' is permitted as a way to set the
                capacity automatically, but expansion would be trivial.
                If 'from_array', the module will attempt to set the capacity
                Note capacities must be specified as volume flux.
            *

            ...Then, assuming you set Qc=='MPM':
            * b_sp, c_sp -> Floats. These are the powers on discharge and
                drainage area in the equations used to control channel width and
                basin hydrology, respectively:
                        W = k_w * Q**b_sp
                        Q = k_Q * A**c_sp
                These parameters are used to constrain flow depth, and may be
                omitted if use_W or use_Q are set.
            *k_Q, k_w, mannings_n -> floats. These are the prefactors on the
                basin hydrology and channel width-discharge relations, and n
                from the Manning's equation, respectively. These are
                needed to allow calculation of shear stresses and hence carrying
                capacities from the local slope and drainage area alone.
                The equation for depth used to derive shear stress and hence
                carrying capacity contains a prefactor:
                    mannings_n*(k_Q**(1-b)/K_w)**0.6
                (so shear = fluid_density*g*depth_equation_prefactor*A**(0.6*c*(1-b)*S**0.7 !)
                Don't know what to set these values to? k_w=0.002, k_Q=1.e-9,
                mannings_n=0.03 give vaguely plausible numbers (e.g., for a
                drainage area ~400km2, like Boulder Creek at Boulder,
                => depth~2.5m, width~35m, shear stress ~O(1000Pa)).
            *Dchar -> float.  The characteristic grain diameter in meters
                (==D50 in most cases) used to calculate Shields numbers
                in the channel. If you want to define Dchar values at each node,
                don't set, and use the Dchar_if_used argument in erode()
                instead.

        OPTIONS:
            *rock_density -> in kg/m3 (defaults to 2700)
            *sediment_density -> in kg/m3 (defaults to 2700)
            *fluid_density -> in most cases water density, in kg/m3 (defaults to 1000)
            *g -> acceleration due to gravity, in m/s**2 (defaults to 9.81)

            *threshold_shields -> +ve float; the threshold taustar_crit.
                Defaults to 0.047, or if 'slope_sensitive_threshold' is set True,
                becomes a weak function of local slope following Lamb et al
                (2008):
                    threshold_shields=0.15*S**0.25
            *slope_sensitive_threshold -> bool, defaults to 'False'.
                If true, threshold_shields is set according to the Lamb
                equation. An exception will be raised if threshold_shields is
                also set.
            *Parker_epsilon -> float, defaults to 0.4. This is Parker's (1978)
                epsilon, which is used in the relation
                    tau - tauc = tau * (epsilon/(epsilon+1))
                The 0.4 default is appropriate for coarse (gravelly) channels.
                The value approaches infinity as the river banks become more
                cohesive.
            *dt -> +ve float. If set, this is the fixed timestep for this
                component. Can be overridden easily as a parameter in erode().
                If not set (default), this parameter MUST be set in erode().
            *use_W -> Bool; if True, component will look for node-centered data
                describing channel width in grid.at_node['channel_width'], and
                use it to implement incision ~ stream power per unit width.
                Defaults to False.
            *use_Q -> Bool. Overrides the basin hydrology relation, using an
                local water discharge value assumed already calculated and
                stored in grid.at_node['discharge'].
            *C_MPM -> float. Defaults to 1. Allows tuning of the MPM prefactor,
                which is calculated as
                    Qc = 8.*C_MPM*(taustar - taustarcrit)**1.5
                In almost all cases, tuning depth_equation_prefactor' is
                preferred to tuning this parameter.
            *return_capacity -> bool (default False). NOT YET IMPLEMENTED.
                If True, this component
                will save the calculated capacity in the field
                'fluvial_sediment_transport_capacity'. (Requires some additional
                math, so is suppressed for speed by default).

        '''
        self.grid = grid
        # needs to be filled with values in execution
        self.link_S_with_trailing_blank = np.zeros(grid.number_of_links + 1)
        self.count_active_links = np.zeros_like(
            self.link_S_with_trailing_blank, dtype=int)
        self.count_active_links[:-1] = 1
        inputs = ModelParameterDictionary(params_file)
        try:
            self.g = inputs.read_float('g')
        except MissingKeyError:
            self.g = 9.81
        try:
            self.rock_density = inputs.read_float('rock_density')
        except MissingKeyError:
            self.rock_density = 2700.
        try:
            self.sed_density = inputs.read_float('sediment_density')
        except MissingKeyError:
            self.sed_density = 2700.
        try:
            self.fluid_density = inputs.read_float('fluid_density')
        except MissingKeyError:
            self.fluid_density = 1000.
        self.rho_g = self.fluid_density * self.g

        try:
            self.Qc = inputs.read_string('Qc')
        except MissingKeyError:
            raise MissingKeyError("Qc must be 'MPM' or a grid field name!")
        else:
            if self.Qc == 'MPM':
                self.calc_cap_flag = True
            else:
                self.calc_cap_flag = False

        try:
            self.lamb_flag = inputs.read_bool('slope_sensitive_threshold')
        except:
            self.lamb_flag = False
        try:
            self.shields_crit = inputs.read_float('threshold_shields')
            # flag for sed_flux_dep_incision to see if the threshold was
            # manually set.
            self.set_threshold = True
            # print("Found a threshold to use: ", self.shields_crit)
            assert self.lamb_flag == False
        except MissingKeyError:
            if not self.lamb_flag:
                self.shields_crit = 0.047
            self.set_threshold = False
        try:
            self.tstep = inputs.read_float('dt')
        except MissingKeyError:
            pass
        try:
            self.use_W = inputs.read_bool('use_W')
        except MissingKeyError:
            self.use_W = False
        try:
            self.use_Q = inputs.read_bool('use_Q')
        except MissingKeyError:
            self.use_Q = False
        try:
            self.return_capacity = inputs.read_bool('return_capacity')
        except MissingKeyError:
            self.return_capacity = False

        try:
            self._b = inputs.read_float('b_sp')
        except MissingKeyError:
            if self.use_W:
                self._b = 0.
            else:
                if self.calc_cap_flag:
                    raise NameError('b was not set')
        try:
            self._c = inputs.read_float('c_sp')
        except MissingKeyError:
            if self.use_Q:
                self._c = 1.
            else:
                if self.calc_cap_flag:
                    raise NameError('c was not set')
        try:
            self.Dchar_in = inputs.read_float('Dchar')
        except MissingKeyError:
            pass

        # assume Manning's equation to set the power on A for shear stress:
        self.shear_area_power = 0.6 * self._c * (1. - self._b)

        self.k_Q = inputs.read_float('k_Q')
        self.k_w = inputs.read_float('k_w')
        mannings_n = inputs.read_float('mannings_n')
        if mannings_n < 0. or mannings_n > 0.2:
            warnings.warn("Manning's n outside it's typical range")
        self.depth_prefactor = self.rho_g * mannings_n * \
            (self.k_Q**(1. - self._b) / self.k_w)**0.6
        # Note the depth_prefactor we store already holds rho*g
        try:
            epsilon = inputs.read_float('Parker_epsilon')
        except MissingKeyError:
            epsilon = 0.4

        try:
            self.C_MPM = inputs.read_float('C_MPM')
        except MissingKeyError:
            self.C_MPM = 1.
        try:
            self.shields_prefactor = 1. / \
                ((self.sed_density - self.fluid_density) * self.g * self.Dchar_in)
            self.MPM_prefactor = 8. * self.C_MPM * \
                np.sqrt(self.relative_weight * self.Dchar_in *
                        self.Dchar_in * self.Dchar_in)
            self.MPM_prefactor_alt = 4. * \
                self.g**(-2. / 3.) / self.excess_SG / \
                self.fluid_density / self.sed_density
        except AttributeError:
            # have to set these manually as needed
            self.shields_prefactor_noD = 1. / \
                ((self.sed_density - self.fluid_density) * self.g)
        self.diffusivity_prefactor = 8. * np.sqrt(
            8. * self.g) / (self.sed_density / self.fluid_density - 1.) * (
                epsilon / (epsilon + 1.)
            )**1.5 * mannings_n**(5. / 6.) * self.k_w**-0.9 * self.k_Q**(
                0.9 *
                (1. - self._b))  # ...this is multiplied by A**c(1-0.1*(1-b))
        # we consciously skip out a factor of S**0.05-->1. in the diffusion prefactor, to avoid delinearizing the diffusion. Only a possible problem at tiny S (20% error @S==0.01; 37% error @S==10**-4)
        # we could include this as a static adjustment in the actual looping code (i.e., just multiply by S**0.05, and don't work with it as part of the problem)
        # in reality, Manning's n changes downstream too, so... whatever
        self.diffusivity_power_on_A = 0.9 * self._c * \
            (1. - self._b)  # i.e., q/D**(1/6)

        self.cell_areas = np.empty(grid.number_of_nodes)
        self.cell_areas.fill(np.mean(grid.area_of_cell))
        self.cell_areas[grid.node_at_cell] = grid.area_of_cell
        self.dx2 = grid.dx**2
        self.dy2 = grid.dy**2
        self.bad_neighbor_mask = np.equal(
            grid.get_active_neighbors_at_node(bad_index=-1), -1)
# Atmospheric Sciences. This is an example script showing how to build a simple rectangular
# mountain with 2 flanks and channel networks.

from landlab.components.flow_routing.route_flow_dn import FlowRouter
from landlab.components.stream_power.stream_power import StreamPowerEroder
from landlab.components.diffusion.diffusion import LinearDiffuser
from landlab import RasterModelGrid, CLOSED_BOUNDARY, FIXED_VALUE_BOUNDARY
from landlab import ModelParameterDictionary
from landlab.io.netcdf import write_netcdf
import numpy as np

#get needed properties to build grid:
input_file = 'create_topo.txt'

#initialize an object that will supply the parameters:
inputs = ModelParameterDictionary(input_file)

#load grid properties
nrows = inputs.read_int('nrows')
ncols = inputs.read_int('ncols')
dx = inputs.read_float('dx')
dt = inputs.read_float('dt')
total_t = inputs.read_float('total_time')
uplift_rate = inputs.read_float('uplift_rate')

nt = int(total_t // dt)  #this is how many loops we'll need
uplift_per_step = uplift_rate * dt

#build the grid
mg = RasterModelGrid((nrows, ncols), dx)
z = mg.add_zeros('node', 'topographic__elevation')
Beispiel #35
0
    def _initialize(self, input_stream=None):
        """Initialize the component from an input file.

        The BMI-style initialize method takes an optional input_stream
        parameter, which may be either a ModelParameterDictionary object or
        an input stream from which a ModelParameterDictionary can read values.

        Parameters
        ----------
        input_stream : str, file_like, or ModelParameterDictionary, optional
            ModelParameterDictionary that holds the input parameters.
        """
        # Create a ModelParameterDictionary for the inputs
        if input_stream is None:
            inputs = None
        elif type(input_stream) == ModelParameterDictionary:
            inputs = input_stream
        else:
            inputs = ModelParameterDictionary(input_stream)

        # Make sure the grid includes elevation data. This means either:
        #  1. The grid has a node field called 'topographic__elevation', or
        #  2. The input file has an item called 'ELEVATION_FIELD_NAME' *and*
        #     a field by this name exists in the grid.
        try:
            self._elev = self._grid.at_node['topographic__elevation']
        except FieldError:
            try:
                topo_field_name = inputs.read_string('ELEVATION_FIELD_NAME')
            except AttributeError:
                print('Error: Because your grid does not have a node field')
                print('called "topographic__elevation", you need to pass the')
                print('name of a text input file or ModelParameterDictionary,')
                print('and this file or dictionary needs to include the name')
                print('of another field in your grid that contains your')
                print('elevation data.')
                raise AttributeError
            except MissingKeyError:
                print('Error: Because your grid does not have a node field')
                print('called "topographic__elevation", your input file (or')
                print('ModelParameterDictionary) must include an entry with')
                print('the key "ELEVATION_FIELD_NAME", which gives the name')
                print('of a field in your grid that contains your elevation')
                print('data.')
                raise MissingKeyError('ELEVATION_FIELD_NAME')
            try:
                self._elev = self._grid.at_node[topo_field_name]
            except AttributeError:
                print('Your grid does not seem to have a node field called',
                      topo_field_name)

        # Create output variables.
        #
        # Note that we initialize depression
        # outlet ID to LOCAL_BAD_INDEX_VALUE (which is a major clue!)
        self.depression_depth = self._grid.add_zeros('node',
                                                     'depression__depth',
                                                     noclobber=False)
        self.depression_outlet_map = self._grid.add_zeros(
            'node', 'depression__outlet_node', dtype=int, noclobber=False)
        self.depression_outlet_map += LOCAL_BAD_INDEX_VALUE

        # Later on, we'll need a number that's guaranteed to be larger than the
        # highest elevation in the grid.
        self._BIG_ELEV = np.amax(self._elev) + 1

        # We'll also need a handy copy of the node neighbor lists
        # TODO: presently, this grid method seems to only exist for Raster
        # grids. We need it for *all* grids!
        self._node_nbrs = self._grid.active_neighbors_at_node()
        dx = self._grid.dx
        dy = self._grid.dy
        if self._D8:
            diag_nbrs = self._grid._diagonal_neighbors_at_node.copy()
            # remove the inactive nodes:
            diag_nbrs[self._grid.status_at_node[diag_nbrs] ==
                      CLOSED_BOUNDARY] = -1
            self._node_nbrs = np.concatenate((self._node_nbrs, diag_nbrs), 1)
            self._link_lengths = np.empty(8, dtype=float)
            self._link_lengths[0] = dx
            self._link_lengths[2] = dx
            self._link_lengths[1] = dy
            self._link_lengths[3] = dy
            self._link_lengths[4:].fill(np.sqrt(dx * dx + dy * dy))
        elif ((type(self._grid) is landlab.grid.raster.RasterModelGrid)
              and (self._routing is 'D4')):
            self._link_lengths = np.empty(4, dtype=float)
            self._link_lengths[0] = dx
            self._link_lengths[2] = dx
            self._link_lengths[1] = dy
            self._link_lengths[3] = dy
        else:
            self._link_lengths = self._grid._length_of_link_with_diagonals
        self._lake_outlets = []  # a list of each unique lake outlet
        # ^note this is nlakes-long

        self.is_pit = self._grid.add_ones('node',
                                          'is_pit',
                                          dtype=bool,
                                          noclobber=False)
        self.flood_status = self._grid.add_zeros('node',
                                                 'flood_status_code',
                                                 dtype=int,
                                                 noclobber=False)
        self._lake_map = np.empty(self._grid.number_of_nodes, dtype=int)
        self._lake_map.fill(LOCAL_BAD_INDEX_VALUE)
Beispiel #36
0
                                FastscapeEroder,
                                PrecipitationDistribution)
from landlab.plot import channel_profile as prf
from landlab.plot import imshow as llplot
from landlab.plot.imshow import imshow_node_grid
from pylab import figure, plot, xlabel, ylabel, title, show

import numpy
from landlab import RasterModelGrid
from landlab import ModelParameterDictionary
import pylab
import time
import copy

input_file_string = './drive_sp_params_storms.txt'
inputs = ModelParameterDictionary(input_file_string)
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')
#nt needs defining
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.
Beispiel #37
0
def test_sp_widths():
    input_str = os.path.join(_THIS_DIR, "test_sp_params_widths.txt")
    inputs = ModelParameterDictionary(input_str, auto_type=True)
    nrows = 5
    ncols = 5
    dx = inputs.read_float("dx")
    dt = inputs.read_float("dt")

    mg = RasterModelGrid(nrows, ncols, dx)
    widths = np.ones(mg.number_of_nodes, dtype=float)
    mg.add_zeros("topographic__elevation", at="node")
    z = np.array([
        5.,
        5.,
        0.,
        5.,
        5.,
        5.,
        2.,
        1.,
        2.,
        5.,
        5.,
        3.,
        2.,
        3.,
        5.,
        5.,
        4.,
        4.,
        4.,
        5.,
        5.,
        5.,
        5.,
        5.,
        5.,
    ])
    mg["node"]["topographic__elevation"] = z

    fr = FlowAccumulator(mg, flow_director="D8")
    sp = StreamPowerEroder(mg, use_W=widths, **inputs)

    # perform the loop (once!)
    for i in range(1):
        fr.run_one_step()
        sqrt_A = mg.at_node["drainage_area"]**0.5
        widths[mg.core_nodes] = sqrt_A[mg.core_nodes] / sqrt_A[
            mg.core_nodes].mean()
        # so widths has mean=1.
        # note the issue with drainage_area not defined at perimeter => nans
        # if not careful...
        sp.run_one_step(dt)

    z_tg = np.array([
        5.,
        5.,
        0.,
        5.,
        5.,
        5.,
        1.37222369,
        0.36876358,
        1.37222369,
        5.,
        5.,
        2.17408606,
        1.07986038,
        2.17408606,
        5.,
        5.,
        3.08340277,
        2.85288049,
        3.08340277,
        5.,
        5.,
        5.,
        5.,
        5.,
        5.,
    ])

    assert_array_almost_equal(mg.at_node["topographic__elevation"], z_tg)
Beispiel #38
0
# mg.set_fixed_value_boundaries_at_grid_edges(True, True, True, True)
##make some K values in a field to test
# mg.at_node['K_values'] = 0.1+np.random.rand(nrows*ncols)/10.
##elapsed_time = 0. #total time in simulation
##while elapsed_time < time_to_run:
##    #add uplift
##    mg.at_node['topographic__elevation'][mg.core_nodes] += uplift*dt
##    print elapsed_time
##    if elapsed_time+dt>time_to_run:
##        print "Short step!"
##        dt = time_to_run - elapsed_time
##    mg = fr.route_flow(grid=mg)
##    mg,_,_ = sp.erode(mg)
##    elapsed_time += dt

inputs = ModelParameterDictionary("./pot_fr_params.txt")
nrows = 200  # inputs.read_int('nrows')
ncols = 200  # inputs.read_int('ncols')
dx = inputs.read_float("dx")
dt = inputs.read_float("dt")
time_to_run = inputs.read_float("run_time")
# nt needs defining
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 + np.random.rand(len(z)) / 1000.
Beispiel #39
0
import numpy
from landlab import RasterModelGrid
from landlab import ModelParameterDictionary
from landlab.components.nonlinear_diffusion.Perron_nl_diffuse import PerronNLDiffuse
import pylab
import time


inputs = ModelParameterDictionary('./drive_perron_params.txt')
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')
#nt needs defining
uplift = inputs.read_float('uplift_rate')
init_elev = inputs.read_float('init_elev')

mg = RasterModelGrid(nrows, ncols, dx)
#mg.set_inactive_boundaries(False, False, False, False)
#mg.set_inactive_boundaries(True,True,True,True)
mg.set_looped_boundaries(True, True)

#create the fields in the grid
mg.create_node_array_zeros('planet_surface__elevation')
z = mg.create_node_array_zeros() + init_elev
mg['node'][ 'planet_surface__elevation'] = z + numpy.random.rand(len(z))/1000.

##Now add a step to diffuse out:
#mg.at_node['planet_surface__elevation'][mg.active_nodes[:(mg.active_nodes.shape[0]//2.)]] += 0.05 #half block uplift
from landlab.components.craters.dig_craters import impactor
from landlab import ModelParameterDictionary

from landlab import RasterModelGrid
import numpy as np
import time
import pylab

#get the needed properties to build the grid:
input_file = './craters_params_degrade.txt'
inputs = ModelParameterDictionary(input_file)
nrows = inputs.read_int('nrows')
ncols = inputs.read_int('ncols')
dx = inputs.read_float('dx')
leftmost_elev = inputs.read_float('leftmost_elevation')
initial_slope = inputs.read_float('initial_slope')
nt = inputs.read_int('number_of_craters_per_loop')
loops = inputs.read_int('number_of_loops')

mg = RasterModelGrid(nrows, ncols, dx)
mg.set_looped_boundaries(True, True)

#create the fields in the grid
mg.create_node_array_zeros('topographic_elevation')
mg['node'][ 'topographic_elevation'] = np.load('init_topo.npy')

# Display a message
print( 'Running ...' )
start_time = time.time()

#instantiate the component:
Beispiel #41
0
@author: danhobley
"""
from __future__ import print_function

from six.moves import range

from landlab import RasterModelGrid, ModelParameterDictionary
from landlab.plot.imshow import imshow_node_grid
import numpy as np
from pylab import imshow, show, contour, figure, clabel, quiver, plot, close
from landlab.components.potentiality_flowrouting import PotentialityFlowRouter
from landlab.components.flow_routing import FlowRouter
from landlab.components.stream_power import FastscapeEroder
# from landlab.grid.mappers import map_link_end_node_max_value_to_link

inputs = ModelParameterDictionary('./pot_fr_params.txt')
nrows = 50  #inputs.read_int('nrows')
ncols = 50  #inputs.read_int('ncols')
dx = inputs.read_float('dx')
init_elev = inputs.read_float('init_elev')

mg = RasterModelGrid(nrows, ncols, dx)

# attempt to implement diffusion with flow routing...

#modify the fields in the grid
z = mg.zeros(at='node') + init_elev
z_slope = (49000. - mg.node_y) / mg.node_y.max() / 20.
mg.at_node[
    'topographic__elevation'] = z + z_slope  #+ np.random.rand(len(z))/1000.
mg.add_zeros('water__unit_flux_in', at='node')
A simple driver implementing Braun-Willett flow routing and then a
(non-fastscape) stream power component.
DEJH, 09/15/14
"""
from __future__ import print_function

import numpy as np
import pylab
from six.moves import range

from landlab import ModelParameterDictionary, RasterModelGrid
from landlab.components import FlowAccumulator, StreamPowerEroder
from landlab.components.stream_power import FastscapeEroder as Fsc
from landlab.plot.imshow import imshow_node_grid

inputs = ModelParameterDictionary("./drive_sp_params_discharge.txt")
nrows = 5
ncols = 5
dx = inputs.read_float("dx")
dt = inputs.read_float("dt")
time_to_run = inputs.read_float("run_time")
# nt needs defining
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 = np.array(
    [
Beispiel #43
0
"""
A driver for our version of AW's gFlex component.

Created on Fri Feb 20 11:17:52 2015

@author: danhobley
"""

from landlab.components.gFlex.flexure import gFlex
import numpy as np
import pylab
from landlab import RasterModelGrid
from landlab import ModelParameterDictionary
from landlab.plot.imshow import imshow_node_grid

inputs = ModelParameterDictionary('./AW_gflex_params.txt')
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')
init_elev = inputs.read_float('init_elev')

mg = RasterModelGrid(nrows, ncols, dx)

#create the fields in the grid
mg.create_node_array_zeros('topographic__elevation')
z = mg.create_node_array_zeros() + init_elev
mg['node']['topographic__elevation'] = z + np.random.rand(len(z)) / 1000.

#make some surface load stresses in a field to test
Beispiel #44
0
show_figs_in_run = True  #disable to run straight through
DL_or_TL = 'DL'

if DL_or_TL == 'TL':
    init_interval = 20
else:
    init_interval = 100

#get the needed properties to build the grid:
input_file = './sed_dep_NMGparams2.txt'
#####remember to change the fixed y-axis dimension in the plots!!
y_max = 3000
make_output_plots = True
out_interval = 15  #was 15
inputs = ModelParameterDictionary(input_file)
nrows = inputs.read_int('nrows')
ncols = inputs.read_int('ncols')
dx = inputs.read_float('dx')
leftmost_elev = inputs.read_float('leftmost_elevation')
initial_slope = inputs.read_float('initial_slope')
uplift_rate = inputs.read_float('uplift_rate')

runtime = inputs.read_float('total_time')
dt = inputs.read_float('dt')

#check we have a plaubible grid
mg = RasterModelGrid(nrows, ncols, dx)
assert mg.number_of_nodes == nrows * ncols
assert mg.node_spacing == dx
from six.moves import range

from landlab.components.flow_routing import FlowRouter
from landlab.components.stream_power import StreamPowerEroder
from landlab.components.stream_power import FastscapeEroder as Fsc

import numpy
import numpy as np
from landlab import RasterModelGrid
from landlab import ModelParameterDictionary
from landlab.plot.imshow import imshow_node_grid
import pylab
import time

inputs = ModelParameterDictionary('./drive_sp_params_discharge.txt')
nrows = 5
ncols = 5
dx = inputs.read_float('dx')
dt = inputs.read_float('dt')
time_to_run = inputs.read_float('run_time')
# nt needs defining
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 = np.array([
    5., 5., 0., 5., 5., 5., 2., 1., 2., 5., 5., 3., 2., 3., 5., 5., 4., 4., 4.,
    def __init__(self, input_stream):
        """
        Reads in parameters and initializes the model.

        Examples
        --------
        >>> from six import StringIO
        >>> p = StringIO('''
        ... model_grid_row__count: number of rows in grid
        ... 4
        ... model_grid_column__count: number of columns in grid
        ... 4
        ... plot_interval: interval for plotting to display, s
        ... 2.0
        ... model__run_time: duration of model run, s
        ... 1.0
        ... model__report_interval: time interval for reporting progress, real-time seconds
        ... 1.0e6
        ... surface_bleaching_time_scale: time scale for OSL bleaching, s
        ... 2.42
        ... light_attenuation_length: length scale for light attenuation, cells (1 cell = 1 mm)
        ... 2.0
        ... ''')
        >>> tsbm = TurbulentSuspensionAndBleachingModel(p)
        >>> tsbm.node_state
        array([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
        >>> tsbm.grid.at_node['osl']
        array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,
                0.,  0.,  0.])
        >>> tsbm.n_xn
        array([0, 1, 1, 0, 0, 1, 1, 0])
        >>> tsbm.fluid_surface_height
        3.5
        """
        # Get a source for input parameters.
        params = ModelParameterDictionary(input_stream)

        # Read user-defined parameters
        nr = params.read_int("model_grid_row__count"
                             )  # number of rows (CSDMS Standard Name [CSN])
        nc = params.read_int(
            "model_grid_column__count")  # number of cols (CSN)
        self.plot_interval = params.read_float(
            "plot_interval")  # interval for plotting output, s
        self.run_duration = params.read_float(
            "model__run_time")  # duration of run, sec (CSN)
        self.report_interval = params.read_float(
            "model__report_interval")  # report interval, in real-time seconds
        self.bleach_T0 = params.read_float(
            "surface_bleaching_time_scale"
        )  # time scale for bleaching at fluid surface, s
        self.zstar = params.read_float(
            "light_attenuation_length"
        )  # length scale for light attenuation in fluid, CELLS

        # Derived parameters
        self.fluid_surface_height = nr - 0.5

        # Calculate when we next want to report progress.
        self.next_report = time.time() + self.report_interval

        # Create grid
        mg = RasterModelGrid(nr, nc, 1.0)

        # Make the boundaries be walls
        mg.set_closed_boundaries_at_grid_edges(True, True, True, True)

        # Set up the states and pair transitions.
        ns_dict = {0: "fluid", 1: "particle"}
        xn_list = self.setup_transition_list()

        # Create the node-state array and attach it to the grid
        node_state_grid = mg.add_zeros("node", "node_state_map", dtype=int)

        # For visual display purposes, set all boundary nodes to fluid
        node_state_grid[mg.closed_boundary_nodes] = 0

        # Initialize the node-state array: here, the initial condition is a pile of
        # resting grains at the bottom of a container.
        bottom_rows = where(mg.node_y < 0.4 * nr)[0]
        node_state_grid[bottom_rows] = 1

        # Create a data array for bleaching.
        # Here, osl=optically stimulated luminescence, normalized to the original
        # signal (hence, initially all unity). Over time this signal may get
        # bleached out due to exposure to light.
        self.osl = mg.add_zeros("node", "osl")
        self.osl[bottom_rows] = 1.0
        self.osl_display = mg.add_zeros("node", "osl_display")
        self.osl_display[bottom_rows] = 1.0

        # We'll need an array to track the last time any given node was
        # updated, so we can figure out the duration of light exposure between
        # update events
        self.last_update_time = mg.add_zeros("node", "last_update_time")

        # Call the  base class (RasterCTS) init method
        super(TurbulentSuspensionAndBleachingModel,
              self).__init__(mg,
                             ns_dict,
                             xn_list,
                             node_state_grid,
                             prop_data=self.osl)

        # Set up plotting (if plotting desired)
        if self.plot_interval <= self.run_duration:
            self.initialize_plotting()
Beispiel #47
0
from __future__ import print_function

from six.moves import range

from landlab.components.craters import impactor
from landlab import ModelParameterDictionary

from landlab import RasterModelGrid
import numpy as np
import time
import pylab

#get the needed properties to build the grid:
input_file = './craters_params.txt'
inputs = ModelParameterDictionary(input_file)
nt = inputs.read_int('number_of_craters_per_loop')
loops = inputs.read_int('number_of_loops')

#the grid should already exist in the environment
#instantiate the component; need to do this again as old version is set to force size:
craters_component = impactor(mg, input_file)

# Display a message
print('Running ...')
start_time = time.time()

#perform the loops:
x = np.empty(nt)
y = np.empty(nt)
r = np.empty(nt)
slope = np.empty(nt)
Beispiel #48
0
from landlab.components.craters.dig_craters import impactor
from landlab import ModelParameterDictionary

from landlab import RasterModelGrid
import numpy as np
import time
import pylab

#get the needed properties to build the grid:
input_file = './craters_params_init.txt'
inputs = ModelParameterDictionary(input_file)
nrows = inputs.read_int('nrows')
ncols = inputs.read_int('ncols')
dx = inputs.read_float('dx')
leftmost_elev = inputs.read_float('leftmost_elevation')
initial_slope = inputs.read_float('initial_slope')
nt = 1  #inputs.read_int('number_of_craters_per_loop')
loops = 1  #inputs.read_int('number_of_loops')

mg = RasterModelGrid(nrows, ncols, dx)
mg.set_looped_boundaries(True, True)

#create the fields in the grid
mg.create_node_array_zeros('planet_surface__elevation')
z = mg.create_node_array_zeros() + leftmost_elev
z += initial_slope * np.amax(mg.node_y) - initial_slope * mg.node_y
mg['node']['planet_surface__elevation'] = z  #+ np.random.rand(len(z))/10000.

# Display a message
print('Running ...')
start_time = time.time()
Beispiel #49
0
def test_sp_discharges_new():
    input_str = os.path.join(_THIS_DIR, "test_sp_params_discharge_new.txt")
    inputs = ModelParameterDictionary(input_str, auto_type=True)
    nrows = 5
    ncols = 5
    dx = inputs.read_float("dx")
    dt = inputs.read_float("dt")

    mg = RasterModelGrid(nrows, ncols, xy_spacing=dx)
    mg.add_zeros("topographic__elevation", at="node")
    z = np.array(
        [
            5.,
            5.,
            0.,
            5.,
            5.,
            5.,
            2.,
            1.,
            2.,
            5.,
            5.,
            3.,
            2.,
            3.,
            5.,
            5.,
            4.,
            4.,
            4.,
            5.,
            5.,
            5.,
            5.,
            5.,
            5.,
        ]
    )
    mg["node"]["topographic__elevation"] = z

    fr = FlowAccumulator(mg, flow_director="D8")
    sp = StreamPowerEroder(mg, **inputs)

    # perform the loop (once!)
    for i in range(1):
        fr.run_one_step()
        sp.run_one_step(dt)

    z_tg = np.array(
        [
            5.,
            5.,
            0.,
            5.,
            5.,
            5.,
            1.47759225,
            0.43050087,
            1.47759225,
            5.,
            5.,
            2.32883687,
            1.21525044,
            2.32883687,
            5.,
            5.,
            3.27261262,
            3.07175015,
            3.27261262,
            5.,
            5.,
            5.,
            5.,
            5.,
            5.,
        ]
    )

    assert_array_almost_equal(mg.at_node["topographic__elevation"], z_tg)
Beispiel #50
0
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, 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, 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_sp_discharges_old():
    input_str = os.path.join(_THIS_DIR, "test_sp_params_discharge.txt")
    inputs = ModelParameterDictionary(input_str)
    nrows = 5
    ncols = 5
    dx = inputs.read_float("dx")
    dt = inputs.read_float("dt")

    mg = RasterModelGrid((nrows, ncols), xy_spacing=dx)
    mg.add_zeros("topographic__elevation", at="node")
    z = np.array([
        5.0,
        5.0,
        0.0,
        5.0,
        5.0,
        5.0,
        2.0,
        1.0,
        2.0,
        5.0,
        5.0,
        3.0,
        2.0,
        3.0,
        5.0,
        5.0,
        4.0,
        4.0,
        4.0,
        5.0,
        5.0,
        5.0,
        5.0,
        5.0,
        5.0,
    ])
    mg["node"]["topographic__elevation"] = z

    fr = FlowAccumulator(mg, flow_director="D8")
    my_Q = mg.at_node["surface_water__discharge"]
    sp = StreamPowerEroder(mg, input_str, use_Q=my_Q)

    # perform the loop (once!)
    for i in range(1):
        fr.run_one_step()
        my_Q[:] = mg.at_node["surface_water__discharge"] * 1.0
        sp.run_one_step(dt)

    z_tg = np.array([
        5.0,
        5.0,
        0.0,
        5.0,
        5.0,
        5.0,
        1.47759225,
        0.43050087,
        1.47759225,
        5.0,
        5.0,
        2.32883687,
        1.21525044,
        2.32883687,
        5.0,
        5.0,
        3.27261262,
        3.07175015,
        3.27261262,
        5.0,
        5.0,
        5.0,
        5.0,
        5.0,
        5.0,
    ])

    assert_array_almost_equal(mg.at_node["topographic__elevation"], z_tg)
Beispiel #52
0
from landlab import RasterModelGrid
from landlab import ModelParameterDictionary
from landlab.utils import structured_grid as sgrid
from landlab.components.sed_trp_shallow_flow.transport_sed_in_shallow_flow import SurfaceFlowTransport

import time
import pylab
import numpy as np

#get the needed properties to build the grid:
inputs = ModelParameterDictionary('./stof_params_basin.txt')
nrows = inputs.read_int('nrows')
ncols = inputs.read_int('ncols')
dx = inputs.read_float('dx')
h_init = inputs.read_float('h_init')
z_boundary = inputs.read_float('z_boundary')  #now the outlet height
drop_ht = inputs.read_float('drop_ht')  #Now the inlet gradient
initial_slope = inputs.read_float('initial_slope')
time_to_run = inputs.read_int('run_time')

inlet_nodes = [0, 1, 2, ncols, 2 * ncols]

mg = RasterModelGrid(nrows, ncols, dx)
mg.set_inactive_boundaries(False, True, False, True)
#mg.node_status[inlet_nodes] = 1
#mg.node_status[-5] = 1 #Fixed lip outlet
#print sgrid.node_tolink_index(mg.shape)[1]
#mg.reset_list_of_active_links()

#node_distances_to_inlet = mg.get_distances_of_nodes_to_point((mg.node_x[0], mg.node_y[0]))
#z0 = initial_slope*(np.amax(node_distances_to_inlet) - node_distances_to_inlet)
Beispiel #53
0
def test_diffusion():
    infile = os.path.join(_THIS_DIR, 'diffusion_params.txt')
    inputs = ModelParameterDictionary(infile, 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')
    init_elev = inputs.read_float('init_elev')

    mg = RasterModelGrid((nrows, ncols), (dx, dx))
    uplift_rate = mg.node_y[mg.core_cells] / 100000.

    # create the fields in the grid
    mg.add_zeros('topographic__elevation', at='node')
    z = mg.zeros(at='node') + init_elev
    np.random.seed(0)
    mg['node']['topographic__elevation'] = z + np.random.rand(len(z)) / 1000.

    mg.set_fixed_value_boundaries_at_grid_edges(True, True, True, True)

    # instantiate:
    dfn = LinearDiffuser(mg, **inputs)

    # perform the loop:
    elapsed_time = 0.  # total time in simulation
    while elapsed_time < time_to_run:
        if elapsed_time + dt > time_to_run:
            dt = time_to_run - elapsed_time
        dfn.run_one_step(dt)
        mg.at_node['topographic__elevation'][mg.core_nodes] += uplift_rate * dt
        elapsed_time += dt

    z_target = np.array([
        5.48813504e-04, 7.15189366e-04, 6.02763376e-04, 5.44883183e-04,
        4.23654799e-04, 6.45894113e-04, 4.37587211e-04, 8.91773001e-04,
        9.63662761e-04, 3.83441519e-04, 7.91725038e-04, 9.18166135e-04,
        1.02015039e-03, 1.10666198e-03, 1.14866514e-03, 1.20224288e-03,
        1.12953135e-03, 1.12966219e-03, 1.00745155e-03, 8.70012148e-04,
        9.78618342e-04, 1.12628772e-03, 1.41663596e-03, 2.66338249e-03,
        2.80420703e-03, 2.82445061e-03, 2.69263914e-03, 2.44620140e-03,
        2.04237613e-03, 4.14661940e-04, 2.64555612e-04, 2.15073330e-03,
        2.77965579e-03, 3.22134736e-03, 3.45859244e-03, 4.47224671e-03,
        4.25371135e-03, 3.82941648e-03, 3.25127747e-03, 6.81820299e-04,
        3.59507901e-04, 3.36577718e-03, 4.20490812e-03, 4.81467159e-03,
        5.14099588e-03, 5.15029835e-03, 4.83533539e-03, 5.22312276e-03,
        4.37284689e-03, 3.63710771e-04, 5.70196770e-04, 4.65122535e-03,
        5.67854747e-03, 6.44757828e-03, 6.85985389e-03, 6.86464781e-03,
        6.45159799e-03, 5.65255723e-03, 4.54258827e-03, 2.44425592e-04,
        1.58969584e-04, 5.85971567e-03, 7.16648352e-03, 8.10954246e-03,
        8.61082386e-03, 8.61350727e-03, 8.10597021e-03, 7.12594182e-03,
        5.75483957e-03, 9.60984079e-05, 9.76459465e-04, 6.29476234e-03,
        7.70594852e-03, 9.79504842e-03, 1.03829367e-02, 1.03869062e-02,
        9.79374998e-03, 8.65447904e-03, 7.07179252e-03, 1.18727719e-04,
        3.17983179e-04, 7.43078552e-03, 9.18353155e-03, 1.04682910e-02,
        1.11542648e-02, 1.21643980e-02, 1.14930584e-02, 1.02184219e-02,
        8.53727126e-03, 9.29296198e-04, 3.18568952e-04, 8.68034110e-03,
        1.06702554e-02, 1.21275181e-02, 1.29049224e-02, 1.29184938e-02,
        1.21616788e-02, 1.17059081e-02, 9.66728348e-03, 4.69547619e-06,
        6.77816537e-04, 1.00128306e-02, 1.21521279e-02, 1.37494046e-02,
        1.46053573e-02, 1.46205669e-02, 1.37908840e-02, 1.22146332e-02,
        1.01165765e-02, 9.52749012e-04, 4.47125379e-04, 1.12069867e-02,
        1.35547122e-02, 1.52840440e-02, 1.62069802e-02, 1.62196380e-02,
        1.53169489e-02, 1.35997836e-02, 1.12818577e-02, 6.92531590e-04,
        7.25254280e-04, 1.14310516e-02, 1.38647655e-02, 1.66771925e-02,
        1.76447108e-02, 1.76515649e-02, 1.66885162e-02, 1.48507549e-02,
        1.23206170e-02, 2.90077607e-04, 6.18015429e-04, 1.24952067e-02,
        1.49924260e-02, 1.68435913e-02, 1.78291009e-02, 1.88311310e-02,
        1.78422046e-02, 1.59665841e-02, 1.34122052e-02, 4.31418435e-04,
        8.96546596e-04, 1.34612553e-02, 1.58763600e-02, 1.76887976e-02,
        1.86526609e-02, 1.86492669e-02, 1.76752679e-02, 1.68480793e-02,
        1.44368883e-02, 9.98847007e-04, 1.49448305e-04, 1.40672989e-02,
        1.64140227e-02, 1.81162514e-02, 1.90091351e-02, 1.89959971e-02,
        1.80757625e-02, 1.63425116e-02, 1.39643530e-02, 6.91669955e-05,
        6.97428773e-04, 1.47340964e-02, 1.66453353e-02, 1.80835612e-02,
        1.88335770e-02, 1.88048458e-02, 1.80022362e-02, 1.65110248e-02,
        1.44854151e-02, 1.71629677e-04, 5.21036606e-04, 1.40633664e-02,
        1.54867652e-02, 1.75865008e-02, 1.81309867e-02, 1.80760242e-02,
        1.74501109e-02, 1.63343931e-02, 1.48208186e-02, 3.18389295e-05,
        1.64694156e-04, 1.41550038e-02, 1.49870334e-02, 1.57563641e-02,
        1.60213295e-02, 1.69074625e-02, 1.64888825e-02, 1.58787450e-02,
        1.50671910e-02, 3.11944995e-04, 3.98221062e-04, 2.09843749e-04,
        1.86193006e-04, 9.44372390e-04, 7.39550795e-04, 4.90458809e-04,
        2.27414628e-04, 2.54356482e-04, 5.80291603e-05, 4.34416626e-04
    ])

    assert_array_almost_equal(mg.at_node['topographic__elevation'], z_target)
Beispiel #54
0
import numpy
import pylab
from pylab import figure, plot, show, title, xlabel, ylabel

from landlab import ModelParameterDictionary, RasterModelGrid
from landlab.components import (
    FastscapeEroder,
    FlowAccumulator,
    PrecipitationDistribution,
    StreamPowerEroder,
)
from landlab.plot import channel_profile as prf, imshow as llplot
from landlab.plot.imshow import imshow_node_grid

input_file_string = "./drive_sp_params_storms.txt"
inputs = ModelParameterDictionary(input_file_string)
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")
# nt needs defining
uplift = inputs.read_float("uplift_rate")
init_elev = inputs.read_float("init_elev")

mg = RasterModelGrid(nrows, ncols, xy_spacing=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.
    def __init__(self,
                 input_file=None,
                 mean_storm=None,
                 mean_interstorm=None,
                 mean_storm_depth=None,
                 total_t=None,
                 delta_t=None):
        """ This reads in information from the input_file (either default or user
            assigned, and creates an instantaneous storm event drawn from the Poisson distribution
        """

        # First we create an instance of the Model Parameter Dictionary
        MPD = ModelParameterDictionary()

        # If no input_file is given,the default file is used
        if input_file is None:
            input_file = _DEFAULT_INPUT_FILE

        # This reads in the file information
        MPD.read_from_file(input_file)

        # And now we set our different parameters
        # using the model parameter dictionary...
        if mean_storm == None:
            self.mean_storm = MPD.read_float('MEAN_STORM')
        else:
            self.mean_storm = mean_storm

        if mean_interstorm == None:
            self.mean_interstorm = MPD.read_float('MEAN_INTERSTORM')
        else:
            self.mean_interstorm = mean_interstorm

        if mean_storm_depth == None:
            self.mean_storm_depth = MPD.read_float('MEAN_DEPTH')
        else:
            self.mean_storm_depth = mean_storm_depth

        if total_t == None:
            self.run_time = MPD.read_float('RUN_TIME')
        else:
            self.run_time = total_t

        if delta_t == None:
            if input_file != _DEFAULT_INPUT_FILE:
                try:
                    self.delta_t = MPD.read_float('DELTA_T')
                except MissingKeyError:
                    self.delta_t = None
            else:
                self.delta_t = None
        else:
            self.delta_t = delta_t

        # Mean_intensity is not set by the MPD, but can be drawn from
        # the mean storm depth and mean storm duration.
        self.mean_intensity = self.mean_storm_depth / self.mean_storm

        # If a time series is created later, this blank list will be used.
        self.storm_time_series = []

        # Given the mean values assigned above using either the model
        # parameter dictionary or the init function, we can call the
        # different methods to assign values from the Poisson distribution.

        self.storm_duration = self.get_precipitation_event_duration()
        self.interstorm_duration = self.get_interstorm_event_duration()
        self.storm_depth = self.get_storm_depth()
        self.intensity = self.get_storm_intensity()
        self._elapsed_time = 0.
Beispiel #56
0
@author: danhobley
"""
from __future__ import print_function

import numpy as np
import pylab

from landlab.components.gFlex.flexure import gFlex
from landlab.components.flow_routing import FlowAccumulator
from landlab.components.stream_power import StreamPowerEroder, FastscapeEroder
from landlab import RasterModelGrid
from landlab import ModelParameterDictionary
from landlab.plot.imshow import imshow_node_grid

inputs = ModelParameterDictionary('./coupled_SP_gflex_params.txt')
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')
init_elev = inputs.read_float('init_elev')
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.
Beispiel #57
0
#mg.set_fixed_value_boundaries_at_grid_edges(True, True, True, True)
##make some K values in a field to test
#mg.at_node['K_values'] = 0.1+np.random.rand(nrows*ncols)/10.
##elapsed_time = 0. #total time in simulation
##while elapsed_time < time_to_run:
##    #add uplift
##    mg.at_node['topographic__elevation'][mg.core_nodes] += uplift*dt
##    print elapsed_time
##    if elapsed_time+dt>time_to_run:
##        print "Short step!"
##        dt = time_to_run - elapsed_time
##    mg = fr.route_flow(grid=mg)
##    mg,_,_ = sp.erode(mg)
##    elapsed_time += dt

inputs = ModelParameterDictionary('./pot_fr_params.txt')
nrows = 200  #inputs.read_int('nrows')
ncols = 200  #inputs.read_int('ncols')
dx = inputs.read_float('dx')
dt = inputs.read_float('dt')
time_to_run = inputs.read_float('run_time')
#nt needs defining
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 + np.random.rand(len(z)) / 1000.
    def initialize(self, grid, params_file):
        '''
        params_file is the name of the text file containing the parameters 
        needed for this stream power component.
        
        ***Parameters for input file***
        OBLIGATORY:
            * Qc -> String. Controls how to set the carrying capacity.
                Either 'MPM', or a string giving the name of the model field
                where capacity values are stored on nodes.
                At the moment, only 'MPM' is permitted as a way to set the 
                capacity automatically, but expansion would be trivial.
                If 'from_array', the module will attempt to set the capacity
                Note capacities must be specified as volume flux.
            * 
            
            ...Then, assuming you set Qc=='MPM':
            * b_sp, c_sp -> Floats. These are the powers on discharge and 
                drainage area in the equations used to control channel width and 
                basin hydrology, respectively:
                        W = k_w * Q**b_sp
                        Q = k_Q * A**c_sp
                These parameters are used to constrain flow depth, and may be
                omitted if use_W or use_Q are set.
            *k_Q, k_w, mannings_n -> floats. These are the prefactors on the 
                basin hydrology and channel width-discharge relations, and n
                from the Manning's equation, respectively. These are 
                needed to allow calculation of shear stresses and hence carrying
                capacities from the local slope and drainage area alone.
                Don't know what to set these values to? k_w=2.5, k_Q=2.5e-7,
                mannings_n=0.05 give vaguely plausible numbers with b=0.5, 
                c = 1.(e.g., for a drainage area ~350km2, like Boulder Creek 
                at Boulder, => depth~1.3m, width~23m, shear stress ~O(200Pa) 
                for an "annual-ish" flood). [If you want to continue playing
                with calibration, the ?50yr return time 2013 floods produced
                depths ~2.3m with Q~200m3/s]
            *Dchar -> float.  The characteristic grain diameter in meters
                (==D50 in most cases) used to calculate Shields numbers
                in the channel. If you want to define Dchar values at each node,
                don't set, and use the Dchar_if_used argument in erode() 
                instead.
            
        OPTIONS:
            *rock_density -> in kg/m3 (defaults to 2700)
            *sediment_density -> in kg/m3 (defaults to 2700)
            *fluid_density -> in most cases water density, in kg/m3 (defaults to 1000)
            *g -> acceleration due to gravity, in m/s**2 (defaults to 9.81)
            
            *threshold_shields -> +ve float; the threshold taustar_crit. 
                Defaults to 0.047, or if 'slope_sensitive_threshold' is set True,
                becomes a weak function of local slope following Lamb et al 
                (2008):
                    threshold_shields=0.15*S**0.25
            *slope_sensitive_threshold -> bool, defaults to 'False'.
                If true, threshold_shields is set according to the Lamb
                equation, 
                An exception will be raised if threshold_shields is 
                also set.
            *dt -> +ve float. If set, this is the fixed timestep for this
                component. Can be overridden easily as a parameter in erode(). 
                If not set (default), this parameter MUST be set in erode().
            *use_W -> Bool; if True, component will look for node-centered data
                describing channel width in grid.at_node['channel_width'], and 
                use it to implement incision ~ stream power per unit width. 
                Defaults to False. NOT YET IMPLEMENTED
            *use_Q -> Bool. Overrides the basin hydrology relation, using an
                local water discharge value assumed already calculated and
                stored in grid.at_node['discharge']. NOT YET IMPLEMENTED
            *C_MPM -> float. Defaults to 1. Allows tuning of the MPM prefactor,
                which is calculated as
                    Qc = 8.*C_MPM*(taustar - taustarcrit)**1.5
                In almost all cases, tuning depth_equation_prefactor' is 
                preferred to tuning this parameter.
            *return_stream_properties -> bool (default False).
                If True, this component will save the calculations for 
                'channel_width', 'channel_depth', and 'channel_discharge' in 
                those grid fields. (Requires some 
                additional math, so is suppressed for speed by default).
            
        '''
        #this is the fraction we allow any given slope in the grid to evolve by in one go (suppresses numerical instabilities)
        self.fraction_gradient_change = 0.25
        self.grid = grid
        self.link_S_with_trailing_blank = np.zeros(grid.number_of_links+1) #needs to be filled with values in execution
        self.count_active_links = np.zeros_like(self.link_S_with_trailing_blank, dtype=int)
        self.count_active_links[:-1] = 1
        inputs = ModelParameterDictionary(params_file)
        try:
            self.g = inputs.read_float('g')
        except MissingKeyError:
            self.g = 9.81
        try:
            self.rock_density = inputs.read_float('rock_density')
        except MissingKeyError:
            self.rock_density = 2700.
        try:
            self.sed_density = inputs.read_float('sediment_density')
        except MissingKeyError:
            self.sed_density = 2700.
        try:
            self.fluid_density = inputs.read_float('fluid_density')
        except MissingKeyError:
            self.fluid_density = 1000.
        self.rho_g = self.fluid_density * self.g
        
        try:
            self.Qc = inputs.read_string('Qc')
        except MissingKeyError:
            raise MissingKeyError("Qc must be 'MPM' or a grid field name!")
        else:
            if self.Qc=='MPM':
                self.calc_cap_flag = True
            else:
                self.calc_cap_flag = False
        try:
            self.return_ch_props = inputs.read_bool('return_stream_properties')
        except MissingKeyError:
            self.return_ch_props = False
                
        try:
            self.lamb_flag = inputs.read_bool('slope_sensitive_threshold')
        except:
            self.lamb_flag = False
        try:
            self.shields_crit = inputs.read_float('threshold_shields')
            self.set_threshold = True #flag for sed_flux_dep_incision to see if the threshold was manually set.
            print "Found a threshold to use: ", self.shields_crit
            assert self.lamb_flag == False
        except MissingKeyError:
            if not self.lamb_flag:
                self.shields_crit = 0.047
            self.set_threshold = False
        try:
            self.tstep = inputs.read_float('dt')
        except MissingKeyError:
            pass
        try:
            self.use_W = inputs.read_bool('use_W')
        except MissingKeyError:
            self.use_W = False
        try:
            self.use_Q = inputs.read_bool('use_Q')
        except MissingKeyError:
            self.use_Q = False
        try:
            self.return_capacity = inputs.read_bool('return_capacity')
        except MissingKeyError:
            self.return_capacity = False
            
        try:
            self._b = inputs.read_float('b_sp')
        except MissingKeyError:
            if self.use_W:
                self._b = 0.
            else:
                if self.calc_cap_flag:
                    raise NameError('b was not set')
        try:
            self._c = inputs.read_float('c_sp')
        except MissingKeyError:
            if self.use_Q:
                self._c = 1.
            else:
                if self.calc_cap_flag:
                    raise NameError('c was not set')
        try:
            self.Dchar_in = inputs.read_float('Dchar')
        except MissingKeyError:
            pass
            
        #assume Manning's equation to set the power on A for shear stress:
        self.shear_area_power = 0.6*self._c*(1.-self._b)
        
        self.k_Q = inputs.read_float('k_Q')
        self.k_w = inputs.read_float('k_w')
        mannings_n = inputs.read_float('mannings_n')
        self.mannings_n = mannings_n
        if mannings_n<0. or mannings_n>0.2:
            print "***STOP. LOOK. THINK. You appear to have set Manning's n outside its typical range. Did you mean it? Proceeding...***"
            sleep(2)

        try:
            self.C_MPM = inputs.read_float('C_MPM')
        except MissingKeyError:
            self.C_MPM = 1.
        self.diffusivity_power_on_A = 0.9*self._c*(1.-self._b) #i.e., q/D**(1/6)
        
        #new for v3:
        #set thresh in shear stress if poss at this stage:
        try: #fails if no Dchar provided, or shields crit is being set dynamically from slope
            self.thresh = self.shields_crit*(self.sed_density-self.fluid_density)*self.g*self.Dchar_in
        except AttributeError:
            try:
                self.shields_prefactor_to_shear = (self.sed_density-self.fluid_density)*self.g*self.Dchar_in
            except AttributeError: #no Dchar
                self.shields_prefactor_to_shear_noDchar = (self.sed_density-self.fluid_density)*self.g
        twothirds = 2./3.
        self.Qs_prefactor = 4.*self.C_MPM**twothirds*self.fluid_density**twothirds/(self.sed_density-self.fluid_density)**twothirds*self.g**(twothirds/2.)*mannings_n**0.6*self.k_w**(1./15.)*self.k_Q**(0.6+self._b/15.)/self.sed_density**twothirds
        self.Qs_thresh_prefactor = 4.*(self.C_MPM*self.k_w*self.k_Q**self._b/self.fluid_density**0.5/(self.sed_density-self.fluid_density)/self.g/self.sed_density)**twothirds
        #both these are divided by sed density to give a vol flux
        self.Qs_power_onA = self._c*(0.6+self._b/15.)
        self.Qs_power_onAthresh = twothirds*self._b*self._c

        if RasterModelGrid in inspect.getmro(grid.__class__):
            self.cell_areas = grid.node_spacing_horizontal*grid.node_spacing_vertical
        else:
            self.cell_areas = np.empty(grid.number_of_nodes)
            self.cell_areas.fill(np.mean(grid.cell_areas))
            self.cell_areas[grid.cell_node] = grid.cell_areas
        self.bad_neighbor_mask = np.equal(grid.get_neighbor_list(bad_index=-1),-1)
        
        self.routing_code = """
Beispiel #59
0
    def initialize(self, grid, params):
        """
        Optional input parameters are:
        * "flow_equation" - options are "default" (default), "Manning", or "Chezy".
          If Equation is Manning or Chezy, you must also specify:
           - "Mannings_n" (if "Manning") : float
           - "Chezys_C" (if "Chezy") : float
        """
        assert RasterModelGrid in inspect.getmro(grid.__class__)
        assert grid.number_of_node_rows >= 3
        assert grid.number_of_node_columns >= 3

        self._grid = grid

        if type(params) == str:
            input_dict = ModelParameterDictionary(params)
        else:
            assert type(params) == dict
            input_dict = params

        #ingest the inputs
        try:
            self.equation = input_dict['flow_equation']
        except KeyError:
            self.equation = 'default'
        assert self.equation in ('default', 'Chezy', 'Manning')
        if self.equation == 'Chezy':
            self.chezy_C = float(input_dict["Chezys_C"])
        if self.equation == 'Manning':
            self.manning_n = float(input_dict["Mannings_n"])

        ncols = grid.number_of_node_columns
        nrows = grid.number_of_node_rows
        #create the blank node maps; assign the topo to an internally held 2D map with dummy edges:
        self.elev_raster = np.empty((nrows+2, ncols+2), dtype=float)
        self._aPP = np.zeros_like(self.elev_raster)
        self._aWW = np.zeros_like(self.elev_raster)
        self._aWP = np.zeros_like(self.elev_raster)
        self._aEE = np.zeros_like(self.elev_raster)
        self._aEP = np.zeros_like(self.elev_raster)
        self._aNN = np.zeros_like(self.elev_raster)
        self._aNP = np.zeros_like(self.elev_raster)
        self._aSS = np.zeros_like(self.elev_raster)
        self._aSP = np.zeros_like(self.elev_raster)
        self._uE = np.zeros_like(self.elev_raster)
        self._uW = np.zeros_like(self.elev_raster)
        self._uN = np.zeros_like(self.elev_raster)
        self._uS = np.zeros_like(self.elev_raster)
        self._uNE = np.zeros_like(self.elev_raster)
        self._uNW = np.zeros_like(self.elev_raster)
        self._uSE = np.zeros_like(self.elev_raster)
        self._uSW = np.zeros_like(self.elev_raster)
        self._K = np.zeros_like(self.elev_raster)

        #extras for diagonal routing:
        self._aNWNW = np.zeros_like(self.elev_raster)
        self._aNWP = np.zeros_like(self.elev_raster)
        self._aNENE = np.zeros_like(self.elev_raster)
        self._aNEP = np.zeros_like(self.elev_raster)
        self._aSESE = np.zeros_like(self.elev_raster)
        self._aSEP = np.zeros_like(self.elev_raster)
        self._aSWSW = np.zeros_like(self.elev_raster)
        self._aSWP = np.zeros_like(self.elev_raster)
        self._totalfluxout = np.empty((nrows, ncols), dtype=float)
        self._meanflux = np.zeros_like(self._totalfluxout)
        self._xdirfluxout = np.zeros_like(self._totalfluxout)
        self._ydirfluxout = np.zeros_like(self._totalfluxout)
        self._xdirfluxin = np.zeros_like(self._totalfluxout)
        self._ydirfluxin = np.zeros_like(self._totalfluxout)

        #setup slices for use in IDing the neighbors
        self._Es = (slice(1,-1),slice(2,ncols+2))
        self._NEs = (slice(2,nrows+2),slice(2,ncols+2))
        self._Ns = (slice(2,nrows+2),slice(1,-1))
        self._NWs = (slice(2,nrows+2),slice(0,-2))
        self._Ws = (slice(1,-1),slice(0,-2))
        self._SWs = (slice(0,-2),slice(0,-2))
        self._Ss = (slice(0,-2),slice(1,-1))
        self._SEs = (slice(0,-2),slice(2,ncols+2))
        self._core = (slice(1,-1),slice(1,-1))
        self._corecore = (slice(2,-2),slice(2,-2)) #the actual, LL-sense core (interior) nodes of the grid

        for out_field in self._output_var_names:
            if self._var_mapping[out_field]=='node':
                try:
                    self._grid.at_node[out_field]
                except FieldError:
                    self._grid.at_node[out_field] = np.empty(self._grid.number_of_nodes, dtype=float)
            elif self._var_mapping[out_field]=='link':
                try:
                    self._grid.at_link[out_field]
                except FieldError:
                    self._grid.at_link[out_field] = np.empty(self._grid.number_of_links, dtype=float)

        #make and store a 2d reference for node BCs
        self._BCs = 4*np.ones_like(self.elev_raster)
        self._BCs[self._core].flat = self._grid.status_at_node
        BCR = self._BCs #for conciseness below
        #these are conditions for boundary->boundary contacts AND anything->closed contacts w/i the grid, both of which forbid flow
        self.boundaryboundaryN = np.logical_or(np.logical_and(BCR[self._core]>0, BCR[self._Ns]>0), np.logical_or(BCR[self._Ns]==4, BCR[self._core]==4))
        self.boundaryboundaryS = np.logical_or(np.logical_and(BCR[self._core]>0, BCR[self._Ss]>0), np.logical_or(BCR[self._Ss]==4, BCR[self._core]==4))
        self.boundaryboundaryE = np.logical_or(np.logical_and(BCR[self._core]>0, BCR[self._Es]>0), np.logical_or(BCR[self._Es]==4, BCR[self._core]==4))
        self.boundaryboundaryW = np.logical_or(np.logical_and(BCR[self._core]>0, BCR[self._Ws]>0), np.logical_or(BCR[self._Ws]==4, BCR[self._core]==4))
        self.boundaryboundaryNE = np.logical_or(np.logical_and(BCR[self._core]>0, BCR[self._NEs]>0), np.logical_or(BCR[self._NEs]==4, BCR[self._core]==4))
        self.boundaryboundaryNW = np.logical_or(np.logical_and(BCR[self._core]>0, BCR[self._NWs]>0), np.logical_or(BCR[self._NWs]==4, BCR[self._core]==4))
        self.boundaryboundarySE = np.logical_or(np.logical_and(BCR[self._core]>0, BCR[self._SEs]>0), np.logical_or(BCR[self._SEs]==4, BCR[self._core]==4))
        self.boundaryboundarySW = np.logical_or(np.logical_and(BCR[self._core]>0, BCR[self._SWs]>0), np.logical_or(BCR[self._SWs]==4, BCR[self._core]==4))
        self.notboundaries = BCR[self._core] == 0

        self.equiv_circ_diam = 2.*np.sqrt(grid.dx*grid.dy/np.pi)
from __future__ import print_function

import random
from time import time

import numpy as np
import pylab
from six.moves import range

from landlab import FIXED_VALUE_BOUNDARY, ModelParameterDictionary, RasterModelGrid
from landlab.components import FastscapeEroder, FlowAccumulator
from landlab.plot import channel_profile as prf

# get the needed properties to build the grid:
input_file = "./drive_sp_params.txt"
inputs = ModelParameterDictionary(input_file)
nrows = inputs.read_int("nrows")
ncols = inputs.read_int("ncols")
dx = inputs.read_float("dx")
uplift_rate = inputs.read_float("uplift_rate")
runtime = inputs.read_float("run_time")
dt = inputs.read_float("dt")
nt = int(runtime // dt)
uplift_per_step = uplift_rate * dt

# instantiate the grid object
mg = RasterModelGrid(nrows, ncols, dx)

boundary_node_list = mg.boundary_nodes

# set up its boundary conditions (bottom, right, top, left is inactive)