def get_cur_be(pre_u, cur_u, dPde, cur_time): 'compute b[n], e[n] = A * e[n-1] + be[n]' assert isinstance(dPde, DPdeAutomaton) def get_V1_l1(V, l, xlist): '\int (u_n(x) p_i(x))dx = alpha * V1 + beta * l1' assert isinstance(V, csc_matrix) assert isinstance(l, csc_matrix) assert V.shape == l.shape n = V.shape[0] assert n == len(xlist) - 2 V1 = lil_matrix((n, 1), dtype=float) l1 = lil_matrix((n, 1), dtype=float) for i in xrange(0, n): hi = xlist[i + 1] - xlist[i] hi_plus_1 = xlist[i + 2] - xlist[i + 1] if i == 0: V1[i, 0] = V[i, 0] * ( hi / 3 + hi_plus_1 / 3) + V[i + 1, 0] * hi_plus_1 / 6 l1[i, 0] = l[i, 0] * ( hi / 3 + hi_plus_1 / 3) + l[i + 1, 0] * hi_plus_1 / 6 elif i == n - 1: V1[i, 0] = V[i, 0] * (hi / 3 + hi_plus_1 / 3) + V[i - 1, 0] * hi / 6 l1[i, 0] = l[i, 0] * (hi / 3 + hi_plus_1 / 3) + l[i - 1, 0] * hi / 6 elif 0 < i < n - 1: V1[i, 0] = V[i, 0] * (hi / 3 + hi_plus_1 / 3) + V[ i - 1, 0] * hi / 6 + V[i + 1, 0] * hi_plus_1 / 6 l1[i, 0] = l[i, 0] * (hi / 3 + hi_plus_1 / 3) + l[ i - 1, 0] * hi / 6 + l[i + 1, 0] * hi_plus_1 / 6 return V1, l1 cur_be = DReachSet() pre_V1, pre_l1 = get_V1_l1(pre_u.Vn, pre_u.ln, dPde.xlist) cur_V1, cur_l1 = get_V1_l1(cur_u.Vn, cur_u.ln, dPde.xlist) cur_b_vec = Fem1D.load_assembler(dPde.xlist, dPde.f_xdom, dPde.time_step, cur_time) cur_be.Vn = dPde.inv_b_matrix * (pre_V1 - cur_V1) cur_be.ln = dPde.inv_b_matrix * (cur_b_vec + pre_l1 - cur_l1) cur_be.alpha_range = dPde.alpha_range cur_be.beta_range = dPde.beta_range return cur_be
def get_dreachset(dPde, toTimeStep): 'compute approximate discrete reachable set of u and e and the bloated u + e' assert isinstance(dPde, DPdeAutomaton) assert isinstance(toTimeStep, int) and toTimeStep >= 0 u_dreachset_list = [] err_dreachset_list = [] bloated_dreachset_list = [] for cur_time in xrange(0, toTimeStep + 1): err_dreachset = DReachSet() u_dreachset = DReachSet() if cur_time == 0: u_Vn = dPde.init_vector u_ln = csc_matrix((dPde.init_vector.shape[0], 1), dtype=float) err_Vn = csc_matrix((dPde.init_vector.shape[0], 1), dtype=float) err_ln = csc_matrix((dPde.init_vector.shape[0], 1), dtype=float) u_dreachset.set_reach_set(dPde.alpha_range, dPde.beta_range, u_Vn, u_ln) err_dreachset.set_reach_set(dPde.alpha_range, dPde.beta_range, err_Vn, err_ln) else: cur_b_vec = Fem1D.load_assembler(dPde.xlist, dPde.f_xdom, dPde.time_step, cur_time) u_dreachset = ReachSetAssembler.get_cur_u_dreachset(dPde.matrix_a, \ u_dreachset_list[cur_time - 1], cur_b_vec) cur_be = ReachSetAssembler.get_cur_be( u_dreachset_list[cur_time - 1], u_dreachset, dPde, cur_time) err_dreachset = ReachSetAssembler.get_cur_err_dreachset( dPde.matrix_a, err_dreachset_list[cur_time - 1], cur_be) u_dreachset_list.append(u_dreachset) err_dreachset_list.append(err_dreachset) bloated_dreachset = DReachSet() bloated_dreachset.set_reach_set(dPde.alpha_range, dPde.beta_range, u_dreachset.Vn + err_dreachset.Vn, u_dreachset.ln + err_dreachset.ln) bloated_dreachset_list.append(bloated_dreachset) return u_dreachset_list, err_dreachset_list, bloated_dreachset_list
def computationtime_vs_numtimesteps(): 'measure reachability analysis computation time for time range [0, 10s] with different number of time steps' ################################################## # generate dPde automaton L = 10.0 # length of rod num_mesh_points = 20 # number of mesh points mesh_grid = np.arange(0, num_mesh_points + 1, step=1) mesh_points = np.multiply(mesh_grid, L / num_mesh_points) print "\nmesh_points = {}".format(mesh_points) steps = [0.2, 0.1, 0.05, 0.01, 0.005, 0.001] # time step of FEM x_dom = [2.0, 4.0] # domain of input function alpha_range = (0.8, 1.1) beta_range = (0.9, 1.1) computation_time = [] number_time_steps = [] print "\nmeasuring reachability analysis time for time range [0, 10s] using different number of time steps" for j in xrange(0, len(steps)): start = time.time() step = steps[j] toTimeStep = int(10.0 / step) # number of time steps number_time_steps.append(toTimeStep) time_grid = np.arange(0, toTimeStep + 1, step=1) time_list = np.multiply(time_grid, step) print "\ntime_list = {}".format(time_list) dPde = Fem1D().get_dPde_automaton(mesh_points.tolist(), x_dom, step) dPde.set_perturbation(alpha_range, beta_range) ############################################################ RSA = ReachSetAssembler() RSA.get_interpolationset(dPde, toTimeStep) # compute discrete reachable set end = time.time() computation_time.append(end - start) print "\ncomputation time for number of time steps = {} is {}".format( toTimeStep, computation_time[j]) store_data(number_time_steps, computation_time, ['number of time steps', 'computation time'], 'computationtime_vs_numtimestpes.dat')
def computationtime_vs_nummeshpoint(): 'measure reachability analysis computation time for time range [0, 10s] with different number of mesh points' ################################################## # generate dPde automaton L = 10.0 # length of rod num_mesh_points = [10, 20, 40, 50, 80, 100] # number of mesh points step = 0.1 x_dom = [2.0, 4.0] # domain of input function alpha_range = (0.8, 1.1) beta_range = (0.9, 1.1) computation_time = [] for j in xrange(0, len(num_mesh_points)): start = time.time() num_mesh_point = num_mesh_points[j] mesh_grid = np.arange(0, num_mesh_point + 1, step=1) mesh_points = np.multiply(mesh_grid, L / num_mesh_point) print "\nmesh_points = {}".format(mesh_points) toTimeStep = 1 # number of time steps time_grid = np.arange(0, toTimeStep + 1, step=1) time_list = np.multiply(time_grid, step) xlist = mesh_points[1:mesh_points.shape[0] - 1] dPde = Fem1D().get_dPde_automaton(mesh_points.tolist(), x_dom, step) dPde.set_perturbation(alpha_range, beta_range) ############################################################ # compute error dicrete reachable set RSA = ReachSetAssembler() RSA.get_interpolationset(dPde, toTimeStep) end = time.time() computation_time.append(end - start) print "\ncomputation time for number of mesh points = {} is {}".format( num_mesh_point, computation_time[j]) store_data(num_mesh_points, computation_time, ['number of mesh points', 'computation time'], 'computationtime_vs_nummeshpoints.dat')
''' This is for ploting dreach set Dung Tran: Dec/2017 ''' import matplotlib.pyplot as plt from engine.fem import Fem1D from engine.verifier import ReachSetAssembler from engine.plot import Plot import numpy as np if __name__ == '__main__': ################################################## # generate dPde automaton FEM = Fem1D() L = 10.0 # length of rod num_mesh_points = 100 # number of mesh points mesh_grid = np.arange(0, num_mesh_points + 1, step=1) mesh_points = np.multiply(mesh_grid, L / num_mesh_points) print "\nmesh_points = {}".format(mesh_points) step = 0.01 # time step of FEM toTimeStep = 1000 # number of time steps time_grid = np.arange(0, toTimeStep + 1, step=1) time_list = np.multiply(time_grid, step) print "\ntime_list = {}".format(time_list) xlist = mesh_points[1:mesh_points.shape[0] - 1] x_dom = [2.0, 4.0] # domain of input function dPde = Fem1D().get_dPde_automaton(mesh_points.tolist(), x_dom, step)
''' This is test file Dung Tran: Nov/2017 ''' import matplotlib.pyplot as plt from scipy.sparse import lil_matrix from engine.fem import Fem1D from engine.verifier import Verifier, ReachSetAssembler from engine.plot import Plot if __name__ == '__main__': ################################################## # test Fem1D class FEM = Fem1D() mesh_points = [0.0, 0.5, 1.0, 1.5, 2.0] # generate mesh points step = 0.1 # time step of FEM x_dom = [0.5, 1.0] # domain of input function mass_mat, stiff_mat, load_vec, init_vector, dPde = Fem1D( ).get_dPde_automaton(mesh_points, x_dom, step) print "\nmass matrix = \n{}".format(mass_mat.todense()) print "\nstiff matrix = \n{}".format(stiff_mat.todense()) print "\nload vector = \n{}".format(load_vec.todense()) print "\ninit vector = \n{}".format(init_vector.todense()) print "\ndPde matrix_a = {}".format(dPde.matrix_a.todense()) alpha_range = (0.99, 1.01) beta_range = (0.99, 1.01) dPde.set_perturbation(alpha_range, beta_range)