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)
def test_diffusion_no_deposit(): # Make an array with three core nodes, in one column. # Because there is zero slope between two of the nodes, there # would be deposition. However, with the deposit flag as 'False', # the elevation of the node with zero downslope gradient will not change. # Use closed boundaries all around because this is a simpler scenario. mg = RasterModelGrid((5, 3), (10, 10)) z = mg.zeros(at="node") z[4] = 3. z[7] = 3. z[10] = 4. mg["node"]["topographic__elevation"] = z # The gradient at node 7 should be zero, so the elevation here would # go up if deposition was allowed. Maker sure it doesn't change with # deposit set to 'False' z_7_before = z[7] mg.set_closed_boundaries_at_grid_edges(True, True, True, True) # instantiate: dfn = LinearDiffuser(mg, linear_diffusivity=1., method="simple", deposit=False) dfn.run_one_step(100) z_7_after = z[7] assert_equal(z_7_before, z_7_after)
def test_diffusion_no_deposit(): # Make an array with three core nodes, in one column. # Because there is zero slope between two of the nodes, there # would be deposition. However, with the deposit flag as 'False', # the elevation of the node with zero downslope gradient will not change. # Use closed boundaries all around because this is a simpler scenario. mg = RasterModelGrid((5, 3), (10, 10)) z = mg.zeros(at='node') z[4]=3. z[7]=3. z[10]=4. mg['node']['topographic__elevation'] = z # The gradient at node 7 should be zero, so the elevation here would # go up if deposition was allowed. Maker sure it doesn't change with # deposit set to 'False' z_7_before = z[7] mg.set_closed_boundaries_at_grid_edges(True, True, True, True) # instantiate: dfn = LinearDiffuser(mg, linear_diffusivity=1., method='simple', deposit=False) dfn.run_one_step(100) z_7_after = z[7] assert_equal(z_7_before, z_7_after)
def __init__(self, input_file=None, params=None): """Initialize the LinearDiffusionModel.""" # Call ErosionModel's init super(LinearDiffusionModel, self).__init__(input_file=input_file, params=params) # Instantiate a LinearDiffuser component self.diffuser = LinearDiffuser(self.grid, **self.params)
class LinearDiffusionModel(_ErosionModel): """ A LinearDiffusionModel is a single-component model that uses a finite- volume solution to the 2D linear diffusion equation to compute erosion. """ def __init__(self, input_file=None, params=None): """Initialize the LinearDiffusionModel.""" # Call ErosionModel's init super(LinearDiffusionModel, self).__init__(input_file=input_file, params=params) # Instantiate a LinearDiffuser component self.diffuser = LinearDiffuser(self.grid, **self.params) def run_one_step(self, dt): """ Advance model for one time-step of duration dt. """ self.diffuser.run_one_step(dt)
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)
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)
def test_diffusion(): inputs = ModelParameterDictionary(os.path.join(_THIS_DIR, 'diffusion_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, 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.diffuse(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.17712569e-04, 1.02071232e-03, 1.10556005e-03, 1.14946096e-03, 1.20022436e-03, 1.12938983e-03, 1.12734463e-03, 1.00699946e-03, 8.70012148e-04, 9.78618342e-04, 1.12639399e-03, 1.41449092e-03, 2.66410106e-03, 2.80054364e-03, 2.82446032e-03, 2.68730913e-03, 2.44468915e-03, 2.03932919e-03, 4.14661940e-04, 2.64555612e-04, 2.14829293e-03, 2.77874345e-03, 3.21568734e-03, 3.45767578e-03, 4.46407381e-03, 4.25087300e-03, 3.82133955e-03, 3.24905386e-03, 6.81820299e-04, 3.59507901e-04, 3.36368061e-03, 4.19756389e-03, 4.81101469e-03, 5.12991298e-03, 5.14551834e-03, 4.82212362e-03, 5.21733341e-03, 4.36590155e-03, 3.63710771e-04, 5.70196770e-04, 4.64503306e-03, 5.67212626e-03, 6.43364397e-03, 6.85196170e-03, 6.84719792e-03, 6.44199511e-03, 5.63718526e-03, 4.53705235e-03, 2.44425592e-04, 1.58969584e-04, 5.85390586e-03, 7.15167925e-03, 8.09808225e-03, 8.58987393e-03, 8.60040722e-03, 8.08353544e-03, 7.11357773e-03, 5.74363061e-03, 9.60984079e-05, 9.76459465e-04, 6.28409752e-03, 7.69302638e-03, 9.77162122e-03, 1.03665461e-02, 1.03596559e-02, 9.77581391e-03, 8.63184402e-03, 7.06221620e-03, 1.18727719e-04, 3.17983179e-04, 7.42141185e-03, 9.16089943e-03, 1.04492602e-02, 1.11235269e-02, 1.21428645e-02, 1.14619409e-02, 1.01991149e-02, 8.52209581e-03, 9.29296198e-04, 3.18568952e-04, 8.66507665e-03, 1.06513243e-02, 1.20945739e-02, 1.28805314e-02, 1.28817803e-02, 1.21355585e-02, 1.16763419e-02, 9.65364442e-03, 4.69547619e-06, 6.77816537e-04, 9.99969455e-03, 1.21210512e-02, 1.37222525e-02, 1.45639747e-02, 1.45899214e-02, 1.37503063e-02, 1.21879324e-02, 1.00970967e-02, 9.52749012e-04, 4.47125379e-04, 1.11861039e-02, 1.35273547e-02, 1.52388381e-02, 1.61710069e-02, 1.61700373e-02, 1.52797248e-02, 1.35608698e-02, 1.12630871e-02, 6.92531590e-04, 7.25254280e-04, 1.14112447e-02, 1.38209264e-02, 1.66345746e-02, 1.75854796e-02, 1.76037603e-02, 1.66317633e-02, 1.48117588e-02, 1.22941311e-02, 2.90077607e-04, 6.18015429e-04, 1.24651035e-02, 1.49479969e-02, 1.67762718e-02, 1.77682020e-02, 1.87567995e-02, 1.77826641e-02, 1.59106401e-02, 1.33841504e-02, 4.31418435e-04, 8.96546596e-04, 1.34298871e-02, 1.58121125e-02, 1.76176213e-02, 1.85627061e-02, 1.85694232e-02, 1.75911811e-02, 1.67878955e-02, 1.43993331e-02, 9.98847007e-04, 1.49448305e-04, 1.40269701e-02, 1.63483655e-02, 1.80226762e-02, 1.89156586e-02, 1.88916976e-02, 1.79880403e-02, 1.62672916e-02, 1.39254090e-02, 6.91669955e-05, 6.97428773e-04, 1.46967049e-02, 1.65718319e-02, 1.79957330e-02, 1.87279193e-02, 1.87059832e-02, 1.79052307e-02, 1.64399258e-02, 1.44435378e-02, 1.71629677e-04, 5.21036606e-04, 1.40296771e-02, 1.54293506e-02, 1.75066749e-02, 1.80476077e-02, 1.79866098e-02, 1.73732550e-02, 1.62714602e-02, 1.47877073e-02, 3.18389295e-05, 1.64694156e-04, 1.41367998e-02, 1.49517470e-02, 1.57129817e-02, 1.59700260e-02, 1.68585204e-02, 1.64421520e-02, 1.58441873e-02, 1.50473253e-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)