def simulate(): # for plots plt.close('all') fig1, ax1 = plt.subplots() #maps: all state vars at one time # locations of observations s = settings() ilocs = s['ilocs'] (x, t0) = initialize(s) t = s['t'][:] #[:40] #times=s['times'][:] #[:40] series_data = np.zeros((len(ilocs), len(t))) for i in np.arange(1, len(t)): #print('timestep %d'%i) x = timestep(x, i, s) #plot_state(fig1,x,i,s) #show spatial plot; nice but slow series_data[:, i] = x[ilocs] + np.random.normal(0, 0.1) # fmt='%1.4f' np.savetxt( 'observation.csv', series_data, delimiter=',') # this observation will be used for the twin experiment #load observations (obs_times, obs_values) = timeseries.read_series('tide_cadzand.txt') observed_data = np.zeros((len(ilocs), len(obs_times))) observed_data[0, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_vlissingen.txt') observed_data[1, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_terneuzen.txt') observed_data[2, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_hansweert.txt') observed_data[3, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_bath.txt') observed_data[4, :] = obs_values[:]
def simulate(): # for plots plt.close('all') # locations of observations s = settings() L = s['L'] dx = s['dx'] xlocs_waterlevel = np.array( [0.0 * L, 0.25 * L, 0.5 * L, 0.75 * L, 0.99 * L]) xlocs_velocity = np.array([0.0 * L, 0.25 * L, 0.5 * L, 0.75 * L]) ilocs = np.hstack((np.round( (xlocs_waterlevel) / dx) * 2, np.round( (xlocs_velocity - 0.5 * dx) / dx) * 2 + 1)).astype( int) #indices of waterlevel locations in x #print(ilocs) loc_names = [] loc_names_waterlevel = [] names = ['Cadzand', 'Vlissingen', 'Terneuzen', 'Hansweert', 'Bath'] for i in range(len(xlocs_waterlevel)): loc_names_waterlevel.append('Waterlevel at x=%f km %s' % (0.001 * xlocs_waterlevel[i], names[i])) loc_names.append('Waterlevel at x=%f km %s' % (0.001 * xlocs_waterlevel[i], names[i])) s['xlocs_waterlevel'] = xlocs_waterlevel s['xlocs_velocity'] = xlocs_velocity s['loc_names_waterlevel'] = loc_names_waterlevel s['ilocs'] = ilocs s['loc_names'] = loc_names s['names'] = names # (x, t0) = initialize(s) t = s['t'][:] #[:40] times = s['times'][:] #[:40] series_data = np.zeros((len(ilocs), len(t))) for i in np.arange(1, len(t)): #print('timestep %d'%i) x = timestep(x, i, s) # plot_state(fig1,x,i,s) #show spatial plot; nice but slow series_data[:, i] = x[ilocs] # x[ilocs] prints h, x[ilocs+1] prints u #load observations (obs_times, obs_values) = timeseries.read_series('tide_cadzand.txt') observed_data = np.zeros((len(ilocs), len(obs_times))) observed_data[0, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_vlissingen.txt') observed_data[1, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_terneuzen.txt') observed_data[2, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_hansweert.txt') observed_data[3, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_bath.txt') observed_data[4, :] = obs_values[:] plot_series(times, series_data, s, observed_data)
def settings(): s = dict() #hashmap to use s['g'] as s.g in matlab # Constants s['g'] = 9.81 # acceleration of gravity s['D'] = 20.0 # Depth s['f'] = 1 / (0.06 * days_to_seconds) # damping time scale L = 100.e3 # length of the estuary s['L'] = L n = 100 #number of cells s['n'] = n # Grid(staggered water levels at 0 (boundary) dx 2dx ... (n-1)dx # velocities at dx/2, 3dx/2, (n-1/2)dx dx = L / (n + 0.5) s['dx'] = dx x_h = np.linspace(0, L - dx, n) s['x_h'] = x_h s['x_u'] = x_h + 0.5 xlocs_waterlevel = np.array( [0.0 * L, 0.25 * L, 0.5 * L, 0.75 * L, 0.99 * L]) xlocs_velocity = np.array([0.0 * L, 0.25 * L, 0.5 * L, 0.75 * L]) ilocs = np.hstack((np.round( (xlocs_waterlevel) / dx) * 2, np.round( (xlocs_velocity - 0.5 * dx) / dx) * 2 + 1)).astype( int) #indices of waterlevel locations in x #print(ilocs) loc_names = [] names = ['Cadzand', 'Vlissingen', 'Terneuzen', 'Hansweert', 'Bath'] for i in range(len(xlocs_waterlevel)): loc_names.append('Waterlevel at x=%f km %s' % (0.001 * xlocs_waterlevel[i], names[i])) for i in range(len(xlocs_velocity)): loc_names.append('Velocity at x=%f km %s' % (0.001 * xlocs_velocity[i], names[i])) s['xlocs_waterlevel'] = xlocs_waterlevel s['xlocs_velocity'] = xlocs_velocity s['ilocs'] = ilocs s['loc_names'] = loc_names # initial condition s['h_0'] = np.zeros(n) s['u_0'] = np.zeros(n) # time t_f = 2. * days_to_seconds #end of simulation dt = 10. * minutes_to_seconds s['dt'] = dt reftime = dateutil.parser.parse("201312050000") #times in secs relative s['reftime'] = reftime t = dt * np.arange(np.round(t_f / dt)) s['t'] = t #print(len(t)) #boundary (western water level) #2) read from file # boundaries are not needed to answer the first question, thus comment the following 5 lines when running the code (bound_times, bound_values) = timeseries.read_series('tide_cadzand.txt') bound_t = np.zeros(len(bound_times)) for i in np.arange(len(bound_times)): bound_t[i] = (bound_times[i] - reftime).total_seconds() s['h_left'] = np.interp(t, bound_t, bound_values) return s
def settings(): s = dict() #hashmap to use s['g'] as s.g in matlab # Constants s['g'] = 9.81 # acceleration of gravity s['D'] = 20.0 # Depth s['f'] = 1 / (0.06 * days_to_seconds) # damping time scale L = 100.e3 # length of the estuary s['L'] = L n = 100 #number of cells s['n'] = n # Grid(staggered water levels at 0 (boundary) dx 2dx ... (n-1)dx # velocities at dx/2, 3dx/2, (n-1/2)dx dx = L / (n + 0.5) s['dx'] = dx x_h = np.linspace(0, L - dx, n) s['x_h'] = x_h s['x_u'] = x_h + 0.5 # initial condition s['h_0'] = np.zeros(n) s['u_0'] = np.zeros(n) # time t_f = 2. * days_to_seconds #end of simulation dt = 10. * minutes_to_seconds s['dt'] = dt reftime = dateutil.parser.parse("201312050000") #times in secs relative s['reftime'] = reftime t = dt * np.arange(np.round(t_f / dt)) s['t'] = t # ============================================================================= # boundary (western water level). Tide data given at Cadnaz are employed to define the left boundary. # data are read from .txt file # ============================================================================= (bound_times, bound_values) = timeseries.read_series('tide_cadzand.txt') bound_t = np.zeros(len(bound_times)) # ============================================================================= # AutoRegression - Sthocastic forcing is introduced to the left boundary # ============================================================================= ensemble_number = 50 T = 6. * hours_to_seconds alpha = np.exp(-dt / T) #time=bound_t.shape[0] time = len(t) theta_n = 0.2 theta_w = theta_n * math.sqrt(1.0 - alpha**2) n_ar = np.zeros((time, ensemble_number)) w_ar = np.zeros((time, ensemble_number)) bound_values_ensemble = np.zeros((time, ensemble_number)) n_ar, w_ar = west_boundary_AR(theta_w, theta_n, alpha, ensemble_number, t) for j in np.arange(ensemble_number): for i in np.arange(len(bound_times)): bound_t[i] = (bound_times[i] - reftime).total_seconds() temp = np.interp(t, bound_t, bound_values) bound_values_ensemble[:, j] = temp + n_ar[:, j] s['h_left'] = bound_values_ensemble #######################This part if for AR(1)#### return s
def settings(): s = dict() #hashmap to use s['g'] as s.g in matlab # Constants s['g'] = 9.81 # acceleration of gravity s['D'] = 20.0 # Depth s['f'] = 1 / (0.06 * days_to_seconds) # damping time scale L = 100.e3 # length of the estuary s['L'] = L n = 100 #number of cells s['n'] = n # Grid(staggered water levels at 0 (boundary) dx 2dx ... (n-1)dx # velocities at dx/2, 3dx/2, (n-1/2)dx dx = L / (n + 0.5) s['dx'] = dx x_h = np.linspace(0, L - dx, n) s['x_h'] = x_h s['x_u'] = x_h + 0.5 # initial condition s['h_0'] = np.zeros(n) s['u_0'] = np.zeros(n) # time t_f = 2. * days_to_seconds #end of simulation dt = 10. * minutes_to_seconds s['dt'] = dt reftime = dateutil.parser.parse("201312050000") #times in secs relative s['reftime'] = reftime t = dt * np.arange(np.round(t_f / dt)) s['t'] = t # ============================================================================= # boundary (western water level). Tide data given at Cadnaz are employed to define the left boundary. # data are read from .txt file # ============================================================================= (bound_times, bound_values) = timeseries.read_series('tide_cadzand.txt') bound_t = np.zeros(len(bound_times)) for i in np.arange(len(bound_times)): bound_t[i] = (bound_times[i] - reftime).total_seconds() s['h_left'] = np.interp(t, bound_t, bound_values) return s
def simulate(): # for plots plt.close('all') fig1, ax1 = plt.subplots() #maps: all state vars at one time # locations of observations s = settings() L = s['L'] dx = s['dx'] xlocs_waterlevel = np.array( [0.0 * L, 0.25 * L, 0.5 * L, 0.75 * L, 0.99 * L]) xlocs_velocity = np.array([0.0 * L, 0.25 * L, 0.5 * L, 0.75 * L]) ilocs = np.hstack((np.round( (xlocs_waterlevel) / dx) * 2, np.round( (xlocs_velocity - 0.5 * dx) / dx) * 2 + 1)).astype( int) #indices of waterlevel locations in x loc_names = [] loc_names_velocity = [] loc_names_waterlevel = [] names = ['Cadzand', 'Vlissingen', 'Terneuzen', 'Hansweert', 'Bath'] for i in range(len(xlocs_waterlevel)): loc_names_waterlevel.append(('Waterlevel at x=%f km %s' % (0.001 * xlocs_waterlevel[i], names[i]))) loc_names.append('Waterlevel at x=%f km %s' % (0.001 * xlocs_waterlevel[i], names[i])) for i in range(len(xlocs_velocity)): loc_names_velocity.append('Velocity at x=%f km %s' % (0.001 * xlocs_velocity[i], names[i])) loc_names.append('Velocity at x=%f km %s' % (0.001 * xlocs_velocity[i], names[i])) s['xlocs_waterlevel'] = xlocs_waterlevel s['xlocs_velocity'] = xlocs_velocity s['ilocs'] = ilocs s['loc_names'] = loc_names s['loc_names_velocity'] = loc_names_velocity s['loc_names_waterlevel'] = loc_names_waterlevel # (x, t0) = initialize(s) t = s['t'][:] #[:40] times = s['times'][:] #[:40] series_data = np.zeros((len(ilocs), len(t))) N = 50 #### ensemble size for AR(1) x_model = np.zeros((N, len(ilocs), len(t))) for j in np.arange(0, N): (x, t0) = initialize(s) for i in np.arange(1, len(t)): #print('timestep %d'%i) x = timestep(x, i, j, s) #plot_state(fig1,x,i,s) #show spatial plot; nice but slow series_data[:, i] = x[ilocs] x_model[j, :, :] = series_data # ============================================================================= # Calculate ensemble mean and confidence interval # ============================================================================= series_data = np.mean(x_model, axis=0) model_var = np.std(x_model, axis=0) confidence_temp = 1.96 * model_var / math.sqrt(N) confidence_top = series_data + confidence_temp confidence_bottom = series_data - confidence_temp #load observations (obs_times, obs_values) = timeseries.read_series('tide_cadzand.txt') observed_data = np.zeros((len(ilocs), len(obs_times))) observed_data[0, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_vlissingen.txt') observed_data[1, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_terneuzen.txt') observed_data[2, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_hansweert.txt') observed_data[3, :] = obs_values[:] (obs_times, obs_values) = timeseries.read_series('tide_bath.txt') observed_data[4, :] = obs_values[:] plot_series(times, series_data, confidence_top, confidence_bottom, s, observed_data)