def __init__(self, time_basis='elapsed time', duration=100, time_step='1 days'): Aegis.__init__(self) self.time_basis = time_basis self.c = Clock() self.ts = pd.Series(0, index=pd.date_range(self.c.start_date, periods=365, freq='D'))
def __init__(self, name='TimeHistory', display_unit='in', clock=Clock()): Aegis.__init__(self) self.name = name self.unit = display_unit range = pd.date_range(clock.start_date, clock.range.size, clock.time_step.days) """range : pandas DatetimeIndex Represents the date range for the time series Must be used to provide the index list for the values""" self.series = pd.Series(np.zeros(len(range)), index=range) self.series.name = self.name self.series.index.name = 'date' self.th_list = []
def setUp(self): """Set up a new object to be tested""" self.rain_obs = [ 1.41, 1.68, 2.51, 3.63, 4.45, 4.15, 3.95, 2.84, 4.00, 3.12, 1.58, 1.66 ] self.min_temp_obs = [ 19.73, 24.25, 31.42, 44.59, 54.4, 63.32, 67.51, 65.65, 57.49, 46.77, 34.2, 24.88 ] self.max_temp_obs = [ 28.99, 33.75, 41.5, 55.37, 65.06, 73.66, 78.14, 76.57, 68.98, 58.03, 44.17, 33.45 ] self.avg_temp_obs = [ 38.24, 43.24, 51.57, 66.15, 75.71, 83.99, 88.77, 87.48, 80.46, 69.28, 54.13, 42.02 ] self.c = Clock() self.w = Wgen() self.precision = 1
def __init__(self): Aegis.__init__(self) self.clock = Clock() self.simulator = Simulator() self.listSet = SetLabel() dir_path = '..\data_external' file_name = 'data.xlsx' fileman = FileManager(dir_path) fileman.add_file(file_name) xls_file = fileman.get_file(file_name)
class Simulator(Aegis): """Class for creating a new simulation. Attributes ---------- time_basis : str Select between static, elapsed time, or calendar time basis for the simulation """ def __init__(self, time_basis='elapsed time', duration=100, time_step='1 days'): Aegis.__init__(self) self.time_basis = time_basis self.c = Clock() self.ts = pd.Series(0, index=pd.date_range(self.c.start_date, periods=365, freq='D')) def run(self): while self.c.running: self.c.update(self.c.current_date) precip = self.r.rain * 25.4 et = np.random.uniform() self.w.update(precip, et, self.w.sink_node) self.ts[self.c.current_date] = self.w.outflow self.c.advance() print("Simulation Complete!") def plot_ts(self): df = self.ts.to_frame() df.plot() plt.show()
def testAdvanceClock(self): """Make sure that the attributes are correct after advancing the clock for a while. """ clock = Clock('1/1/2019', '1/10/2019') realizations = 10 for r in range(0, realizations): clock.reset() while clock.running: clock.advance() self.assertEqual(clock.current_date, clock.end_date)
class TestWGEN(unittest.TestCase): def setUp(self): """Set up a new object to be tested""" self.rain_obs = [ 1.41, 1.68, 2.51, 3.63, 4.45, 4.15, 3.95, 2.84, 4.00, 3.12, 1.58, 1.66 ] self.min_temp_obs = [ 19.73, 24.25, 31.42, 44.59, 54.4, 63.32, 67.51, 65.65, 57.49, 46.77, 34.2, 24.88 ] self.max_temp_obs = [ 28.99, 33.75, 41.5, 55.37, 65.06, 73.66, 78.14, 76.57, 68.98, 58.03, 44.17, 33.45 ] self.avg_temp_obs = [ 38.24, 43.24, 51.57, 66.15, 75.71, 83.99, 88.77, 87.48, 80.46, 69.28, 54.13, 42.02 ] self.c = Clock() self.w = Wgen() self.precision = 1 def tearDown(self): """Destroy the object after running tests""" del self.w def test1DayRain(self): """Check rain total using known chance of rain""" realizations = 100 rain_total = 0.0 for r in range(0, realizations): self.w.update() rain_total += self.w.rain rain = rain_total / realizations self.assertAlmostEqual(rain, 0.0488, self.precision) def testMonthlyRain(self): """Check cumulative rain avg""" rain_array = [0.0] * 12 realizations = 200 self.w.min_rain = 0.0 for r in range(1, realizations): self.c.reset() while self.c.running: month = self.c.current_date.month - 1 self.w.update(self.c.current_date) rain_array[month] += self.w.rain self.c.advance() for i in range(0, 12): rain_array[i] /= realizations np.testing.assert_allclose(self.rain_obs, rain_array, rtol=0.15, atol=0.1, err_msg='Not close enough!', verbose=True) def testDetermTemp(self): """Using deterministic runs, ensure the correct temperature for the first day of each month.""" # Below are daily average temps from deterministic GoldSim model # Each value taken from the 1st day of each month starting with Jan goldsim_tavg = [ 19.19, 19.04, 23.47, 33.7, 47.97, 63.43, 73.54, 74.41, 65.17, 50.55, 35.4, 24.74 ] self.w.temp_determ = True self.w.rain_deterministic = True self.w.markov_deterministic = True monthly_temps = [] while self.c.running: month = self.c.current_date.month - 1 self.w.update(self.c.current_date) #monthly_temps[month] += self.w.tavg / self.c.current_date.daysinmonth if self.c.current_date.day == 1 and self.c.current_date.year == 2019: monthly_temps.append(self.w.tavg) self.c.advance() np.testing.assert_almost_equal(goldsim_tavg, monthly_temps, decimal=2, err_msg='test', verbose=True) def testAvgTemp(self): """Make sure the average temperature is correct for each month""" observed_tavg = [ 28.99, 33.75, 41.5, 55.37, 65.06, 73.66, 78.14, 76.57, 68.98, 58.03, 44.17, 33.45 ] realizations = 100 monthly_temps = [0.0] * 12 for r in range(1, realizations): self.c.reset() while self.c.running: month = self.c.current_date.month - 1 self.w.update(self.c.current_date) monthly_temps[ month] += self.w.tavg / self.c.current_date.daysinmonth self.c.advance() for i in range(0, 12): monthly_temps[i] /= realizations np.testing.assert_allclose(observed_tavg, monthly_temps, rtol=0.1, atol=0.05, err_msg='Not close enough!', verbose=True)
from global_attributes.clock import Clock """This is a test model. Used to test my library to see if I can build a model using its features and modules. This model replicates the work done in the IWRM SLC V9.111.gsm model.""" fm = FileManager('..\\data_external') input_file = fm.add_file('data.xlsx') xls_file = pd.ExcelFile(input_file) monthly_data = pd.read_excel(xls_file, 'Monthly') xls_file.close() c = Clock() evap_table = Vector('Evaporation', 'in', monthly_data['Evap']) precip_table = Vector('Precipitation', 'in', monthly_data['Precip']) e_ts = pd.Series(name='evaporation') p_ts = pd.Series(name='precip') th1 = TimeHistory('Climate Results', 'in', c) while c.running: """Set up the run properties""" month = c.current_date.month_name() dayofyear = c.current_date.dayofyear """Define input variables"""
def setUp(self): """Set up a new object to be tested""" self.start_date = pd.Timestamp('4/28/1975') self.end_date = pd.Timestamp('10/15/1988') self.duration = self.end_date - self.start_date self.c = Clock(self.start_date, self.end_date)
class TestClockCase(unittest.TestCase): def setUp(self): """Set up a new object to be tested""" self.start_date = pd.Timestamp('4/28/1975') self.end_date = pd.Timestamp('10/15/1988') self.duration = self.end_date - self.start_date self.c = Clock(self.start_date, self.end_date) def tearDown(self): """Destroy the object after running tests""" del self.c def testEndTime(self): """End Time should be 100 days from start""" self.assertEqual(self.c.end_date, self.end_date) def testClockReset(self): days = 17 for d in range(0, days): self.c.advance() self.c.reset() self.assertEqual(self.c.current_date, self.c.start_date) def testAdvanceClock(self): """Make sure that the attributes are correct after advancing the clock for a while. """ clock = Clock('1/1/2019', '1/10/2019') realizations = 10 for r in range(0, realizations): clock.reset() while clock.running: clock.advance() self.assertEqual(clock.current_date, clock.end_date) def testRemainingTime(self): """Check to see if remaining time is correct after one clock advancement""" num_days = np.random.randint(1, 50) for t in range(num_days): self.c.advance() current_date = pd.Timestamp(self.start_date + pd.Timedelta(days=num_days)) remaining_time = pd.Timedelta(self.duration - pd.Timedelta(days=num_days)) self.assertEqual(self.c.remaining_time, remaining_time) self.assertEqual(self.c.current_date, current_date) def testChangeDuration(self): """Make sure the end time is correct after changing the duration""" self.c.set_duration('10 days') end_date = self.start_date + pd.Timedelta(days=10) self.assertEqual(self.c.remaining_time, pd.Timedelta('10 days')) self.assertEqual(self.c.end_date, end_date) def testChangeStart(self): """Make sure the end date is correct after changing the start date""" start_date = '4/15/2010' self.c.set_start_date(start_date) num_days = np.random.randint(1, 50) for t in range(num_days): self.c.advance() current_date = pd.Timestamp( pd.Timestamp(start_date) + pd.Timedelta(days=num_days)) self.assertEqual(current_date, self.c.current_date)