u_exact = sp.lambdify((t, x), u_exact, 'numpy') # All data t_domain = np.linspace(0, T_end, n_samples) x_domain = np.linspace(-0.5, 0.5, n_samples) T, X = np.meshgrid(t_domain, x_domain, indexing='ij') u_data = u_exact(T, X) # To build the model we sample the data (in interior indices) to get # the derivative right. For the rhs we keep it simple u_grid = GridFunction([t_domain, x_domain], u_data) x_grid = Coordinate([t_domain, x_domain], 1) lhs_stream = compile_stream((sp.Derivative(u, t), ), {u: u_grid}) foos = polynomials([u], 3) derivatives = Dx(u, 1, [3])[1:] # Don't include 0;th # Combine these guys columns = [(1./sp.cos(x))*sp.Derivative(u, x)] + \ list(foos) + \ list(derivatives) + \ [reduce(operator.mul, cs) for cs in itertools.product(foos, derivatives)] rhs_stream = compile_stream(columns, {u: u_grid}) # A subset of this data is now used for training; where we can eval deriv # safely train_indices = map( tuple, np.random.choice(np.arange(5, n_samples - 5), (n_train, 2)))
u_exact = sp.lambdify((t, x), u_exact_expr, 'numpy') # All data t_domain = np.linspace(0, T_end, n_samples) x_domain = np.linspace(0, X_end, n_samples) T, X = np.meshgrid(t_domain, x_domain, indexing='ij') u_data = u_exact(T, X) # To build the model we sample the data (in interior indices) to get # the derivative right. For the rhs we keep it simple grid = [t_domain, x_domain] u_grid = GridFunction(grid, u_data) equation = sp.Derivative(u, t) - u - sp.Derivative(u, x) - sp.Derivative(u, x, x) lhs_stream = compile_stream((equation, ), {u: u_grid}) foos = (-t*x**2, -2*t*x, -2*t, x**2)# + (sp.sin(x), sp.cos(x), sp.sin(t), sp.cos(t)) #polynomials([x, t], 3) # Combine these guys columns = list(foos) rhs_stream = compile_stream(columns, {u:u_grid}) # A subset of this data is now used for training; where we can eval deriv # safely train_indices = map(tuple, np.random.choice(np.arange(5, n_samples-5), (n_train, 2))) true_rhs_stream = sp.lambdify((t, x), equation.subs(u, u_exact_expr).doit()) tx = SpaceTimeCoordinate(grid)
# # All data t_domain = np.linspace(0, T_end, n_samples) u_data = np.array(map(u_exact, t_domain)) # Let's make one grid function for evvery component of that vector u_grids = [] for col in range(n_eqs): u_grids.append(GridFunction([t_domain], u_data[:, col])) # And a symbols for each us = sp.symbols(' '.join(['u%d' % i for i in range(n_eqs)])) symbol_foo_map = dict(zip(us, u_grids)) # We are going to build a block structured system d u0/dt @ all_pts, next component lhs_streams = [ compile_stream((sp.Derivative(usi, t), ), symbol_foo_map) for usi in us ] # NOTE: this is reused for each block columns = polynomials(us, 2) print columns rhs_stream = compile_stream(columns, symbol_foo_map) # A subset of this data is now used for training; where we can eval deriv # safely train_indices = np.random.choice(np.arange(5, n_samples - 5), n_train) # Now build it lhs = np.hstack([[lhs_stream((point, )) for point in train_indices] for lhs_stream in lhs_streams])
# Not the streams expression = (1, f, g, f**2 + f * g + g**2, sp.sin(f + g), sp.exp(-(f**2 + g**2)), sp.Derivative(f, x), sp.Derivative(f**2, y), sp.Derivative(f + g, x), 2 * f * sp.Derivative(g, y, x), 2 * g * sp.Derivative(f, y, x), sp.Derivative(2 * f * g, y, x, y), sp.Derivative(f**2, x, x)) from streams import expand_derivative gg = expand_derivative(expression[0]) print subs = {f: grid_fs[0], g: grid_fs[1]} # Sympy version expr_stream = compile_stream(expression, subs) f, g = expressions expression = (1, f, g, f**2 + f * g + g**2, sp.sin(f + g), sp.exp(-(f**2 + g**2)), f.diff(x, 1), (f**2).diff(y), (f + g).diff(x), 2 * f * (g.diff(y, x)), 2 * g * (f.diff(y, x)), (2 * f * g).diff(x, y, y), (f**2).diff(x, x)) sympy_stream = sp.lambdify((t, x, y, z), expression) # Check the values at i_point = (8, 7, 6, 7) i_to_x = SpaceTimeCoordinate(grid) x = i_to_x(i_point)