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 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 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 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): # 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 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)
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)
class TestModelParameterDictionary(unittest.TestCase): def setUp(self): from StringIO import StringIO self.param_file = StringIO(_TEST_PARAM_DICT_FILE) self.param_dict = ModelParameterDictionary() self.param_dict.read_from_file(self.param_file) def test_read_file(self): all_keys = set([ 'FLOAT_VAL', 'INT_VAL', 'STRING_VAL', 'TRUE_BOOL_VAL', 'FALSE_BOOL_VAL', ]) param_list = set(self.param_dict.params()) self.assertEqual(param_list, all_keys) def test_read_file_name(self): (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()) self.assertEqual(param_list, all_keys) def test_read_file_like_twice(self): from StringIO 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) def test_read_int(self): self.assertEqual(self.param_dict.read_int('INT_VAL'), 1) with self.assertRaises(ParameterValueError): self.param_dict.read_int('FLOAT_VAL') with self.assertRaises(MissingKeyError): self.param_dict.read_int('MISSING_INT') self.assertEqual(self.param_dict.read_int('MISSING_INT', 2), 2) def test_get_int(self): self.assertEqual(self.param_dict.get('INT_VAL', ptype=int), 1) with self.assertRaises(ParameterValueError): self.param_dict.get('FLOAT_VAL', ptype=int) with self.assertRaises(MissingKeyError): self.param_dict.get('MISSING_INT', ptype=int) def test_set_default(self): self.param_dict.setdefault('MISSING_INT', 2) self.assertEqual(self.param_dict.read_int('MISSING_INT'), 2) def test_read_float(self): self.assertEqual(self.param_dict.read_float('FLOAT_VAL'), 2.2) self.assertEqual(self.param_dict.read_float('INT_VAL'), 1) with self.assertRaises(ParameterValueError): self.param_dict.read_float('STRING_VAL') with self.assertRaises(MissingKeyError): self.param_dict.read_float('MISSING_FLOAT') def test_read_string(self): self.assertEqual(self.param_dict.read_string('STRING_VAL'), 'The Landlab') self.assertEqual(self.param_dict.read_string('INT_VAL'), '1') self.assertEqual(self.param_dict.read_string('FLOAT_VAL'), '2.2') with self.assertRaises(MissingKeyError): self.param_dict.read_string('MISSING_STRING') def test_read_bool(self): self.assertEqual(self.param_dict.read_bool('TRUE_BOOL_VAL'), True) self.assertEqual(self.param_dict.read_bool('FALSE_BOOL_VAL'), False) with self.assertRaises(MissingKeyError): self.param_dict.read_bool('MISSING_BOOLEAN') with self.assertRaises(ParameterValueError): self.param_dict.read_bool('STRING_VAL') def test_dict_keys(self): all_keys = set([ 'FLOAT_VAL', 'INT_VAL', 'STRING_VAL', 'TRUE_BOOL_VAL', 'FALSE_BOOL_VAL', ]) self.assertEqual(set(self.param_dict), all_keys) for key in all_keys: self.assertTrue(key in self.param_dict) def test_dict_index(self): self.assertEqual(self.param_dict['INT_VAL'], '1')
class TestModelParameterDictionary(unittest.TestCase): def setUp(self): from StringIO import StringIO self.param_file = StringIO(_TEST_PARAM_DICT_FILE) self.param_dict = ModelParameterDictionary() self.param_dict.read_from_file(self.param_file) def test_read_file(self): all_keys = set([ 'FLOAT_VAL', 'INT_VAL', 'STRING_VAL', 'TRUE_BOOL_VAL', 'FALSE_BOOL_VAL', ]) param_list = set(self.param_dict.params()) self.assertEqual(param_list, all_keys) def test_read_file_name(self): (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()) self.assertEqual(param_list, all_keys) def test_read_file_like_twice(self): from StringIO 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) def test_read_int(self): self.assertEqual(self.param_dict.read_int('INT_VAL'), 1) with self.assertRaises(ParameterValueError): self.param_dict.read_int('FLOAT_VAL') with self.assertRaises(MissingKeyError): self.param_dict.read_int('MISSING_INT') self.assertEqual(self.param_dict.read_int('MISSING_INT', 2), 2) def test_get_int(self): self.assertEqual(self.param_dict.get('INT_VAL', ptype=int), 1) with self.assertRaises(ParameterValueError): self.param_dict.get('FLOAT_VAL', ptype=int) with self.assertRaises(MissingKeyError): self.param_dict.get('MISSING_INT', ptype=int) def test_set_default(self): self.param_dict.setdefault('MISSING_INT', 2) self.assertEqual(self.param_dict.read_int('MISSING_INT'), 2) def test_read_float(self): self.assertEqual(self.param_dict.read_float('FLOAT_VAL'), 2.2) self.assertEqual(self.param_dict.read_float('INT_VAL'), 1) with self.assertRaises(ParameterValueError): self.param_dict.read_float('STRING_VAL') with self.assertRaises(MissingKeyError): self.param_dict.read_float('MISSING_FLOAT') def test_read_string(self): self.assertEqual(self.param_dict.read_string('STRING_VAL'), 'The Landlab') self.assertEqual(self.param_dict.read_string('INT_VAL'), '1') self.assertEqual(self.param_dict.read_string('FLOAT_VAL'), '2.2') with self.assertRaises(MissingKeyError): self.param_dict.read_string('MISSING_STRING') def test_read_bool(self): self.assertEqual(self.param_dict.read_bool('TRUE_BOOL_VAL'), True) self.assertEqual(self.param_dict.read_bool('FALSE_BOOL_VAL'), False) with self.assertRaises(MissingKeyError): self.param_dict.read_bool('MISSING_BOOLEAN') with self.assertRaises(ParameterValueError): self.param_dict.read_bool('STRING_VAL') def test_dict_keys(self): all_keys = set([ 'FLOAT_VAL', 'INT_VAL', 'STRING_VAL', 'TRUE_BOOL_VAL', 'FALSE_BOOL_VAL', ]) self.assertEqual(set(self.param_dict), all_keys) for key in all_keys: self.assertTrue(key in self.param_dict) def test_dict_index(self): self.assertEqual(self.param_dict['INT_VAL'], '1')