def test_convert_tangent_euclidean_to_stereo(): solver = kmap.Solver() n_steps = 10 s3_map = map_sens.Sensitivity(solver, n_steps) n_runup = 10 u_init = solver.u_init s0 = solver.s0 u_init = solver.primal_step(u_init, s0, n_runup) u_trj = s3_map.solve_primal(solver,\ u_init, n_steps, solver.s0) u_trj = u_trj.T q = rand(2, n_steps) q[0] = 0. q[1] = 1. q_euclidean = solver.convert_tangent_stereo_to_euclidean(u_trj, q) q_euclidean = vstack(q_euclidean) q_spherical = solver.convert_tangent_euclidean_to_spherical(u_trj,\ q_euclidean) q_spherical = vstack(q_spherical) p_euclidean = solver.convert_tangent_spherical_to_euclidean( u_trj, q_spherical) p_euclidean = vstack(p_euclidean) p_stereo = solver.convert_tangent_euclidean_to_stereo(u_trj, p_euclidean) p_stereo = vstack(p_stereo) print(p_stereo) assert (norm(p_stereo - q) < 1.e-10) return
def test_step_primal(): holes = array([1./sqrt(2.),0,1./sqrt(2.0),\ -1./sqrt(2.),0,1./sqrt(2.0), \ 1./sqrt(2.),0,-1./sqrt(2.0), \ -1./sqrt(2.),0,-1./sqrt(2.0)]) holes = holes.reshape(4, 3) solver_map = kmap.Solver() u_init = solver_map.u_init n_map = solver_map.n_poincare n_steps = 500 s0 = solver_map.s0 sens_object = map_sens.Sensitivity() solve_primal = sens_object.solve_primal u = solve_primal(solver_map,\ u_init,n_steps,s0) flag = 1 epsi = 1.e-1 for i in range(1, n_steps): if (not (flag)): break for j in range(holes.shape[0]): if (norm(u[i, :3] - holes[j]) < epsi): flag = 0 if (norm(u[i, :3]) > 1.0 + epsi): flag = 0 assert (i == n_steps - 1)
def visualize_poincare_primal(): solver_map = kmap.Solver() u_init = solver_map.u_init n_steps = 500 s0 = solver_map.s0 sens_object = map_sens.Sensitivity() solve_primal = sens_object.solve_primal u_map = solve_primal(solver_map,\ u_init,n_steps,s0) stereo_real, stereo_imag = \ solver_map.stereographic_projection(u_map.T) figure() plot(stereo_real, stereo_imag, '.') savefig('st_proj_poincare_attractor.png')
def visualize_unstable_direction(): solver_map = kmap.Solver() u_init = solver_map.u_init n_map = solver_map.n_poincare n_steps = 500 s0 = solver_map.s0 sens_object = map_sens.Sensitivity() solve_primal = sens_object.solve_primal u_map = solve_primal(solver_map,\ u_init,n_steps,s0) solve_unstable_direction = sens_object.\ solve_unstable_direction v = solve_unstable_direction(solver_map,\ u_map, rand(solver_map.state_dim),n_steps,\ s0) visualize_tangent_stereographic(u_map,\ v)
def test_convert_tangent_euclidean_to_spherical(): solver = kmap.Solver() n_steps = 10 s3_map = map_sens.Sensitivity(solver, n_steps) n_runup = 10 u_init = solver.u_init s0 = solver.s0 u_init = solver.primal_step(u_init, s0, n_runup) u_trj = s3_map.solve_primal(solver,\ u_init, n_steps, solver.s0) q = rand(2, n_steps) q_euclidean = solver.convert_tangent_spherical_to_euclidean(u_trj.T, q) q_euclidean = vstack(q_euclidean) q_spherical = solver.convert_tangent_euclidean_to_spherical(u_trj.T,\ q_euclidean) assert (norm(q - q_spherical) < 1.e-10) return
def test_convert_tangent_stereo_to_euclidean(): solver = kmap.Solver() n_steps = 10 s3_map = map_sens.Sensitivity(solver, n_steps) n_runup = 10 u_init = solver.u_init s0 = solver.s0 u_init = solver.primal_step(u_init, s0, n_runup) u_trj = s3_map.solve_primal(solver,\ u_init, n_steps, solver.s0) p1 = solver.convert_tangent_stereo_to_euclidean(u_trj.T,\ vstack((ones(n_steps),zeros(n_steps)))) p2 = solver.convert_tangent_stereo_to_euclidean(u_trj.T,\ vstack((zeros(n_steps),ones(n_steps)))) p1 = vstack(p1) p2 = vstack(p2) dot_product_p1_p2 = sum(p1 * p2, axis=0) print(norm(dot_product_p1_p2)) assert (norm(dot_product_p1_p2) <= 1.e-10) return
def test_convert_tangent_spherical_to_euclidean(): solver = kmap.Solver() n_steps = 10 s3_map = map_sens.Sensitivity(solver, n_steps) n_runup = 10 u_init = solver.u_init s0 = solver.s0 u_init = solver.primal_step(u_init, s0, n_runup) u_trj = s3_map.solve_primal(solver,\ u_init, n_steps, solver.s0) q1 = solver.convert_tangent_spherical_to_euclidean(u_trj.T,\ vstack((ones(n_steps),zeros(n_steps)))) q2 = solver.convert_tangent_spherical_to_euclidean(u_trj.T,\ vstack((zeros(n_steps),ones(n_steps)))) q1 = array(q1) q2 = array(q2) dot_product_q1_q2 = sum(q1 * q2, axis=0) print(norm(dot_product_q1_q2)) assert (norm(dot_product_q1_q2) <= 1.e-10) return
u_ode = plykin.solve_primal(solver_ode,solver_ode.u_init,\ n_steps, solver_ode.s0) t1 = clock() print('{:<60s}{:>16.10f}'.format(\ "Time to solve primal: ",\ t1-t0)) t2 = clock() plykin.precompute_sources(solver_ode, u_ode) t3 = clock() print('{:<60s}{:>16.10f}'.format(\ "Time for flow precomputations: ",\ t3-t2)) poincare_plykin = map_sens.Sensitivity(solver_map, n_steps_map) #u_map = poincare_plykin.solve_primal(solver_map,\ # solver_map.u_init, n_steps_map, solver_map.s0) u_map = u_ode[::n_map] t4 = clock() poincare_plykin.precompute_sources(solver_map, u_map) t5 = clock() print('{:<60s}{:>16.10f}'.format(\ "Time for map precomputations: ",\ t5-t4)) # Comparison of pre-computations v0 = plykin.v0[::n_map] v0_map = poincare_plykin.v0
gradJ = zeros_like(u) gradJ[:, index] = 1.0 return gradJ @jit(nopython=True) def compute_dJds(gradJ, v): dJds = diag(dot(gradJ, v.T)) return dJds #def compute_sensitivity(): if __name__ == "__main__": solver = kp.Solver() n_steps = 50 s3_map = map_sens.Sensitivity(solver, n_steps) n_runup = 500 u_init = solver.u_init s0 = solver.s0 state_dim = solver.state_dim param_dim = s0.size u_init = solver.primal_step(u_init, s0, n_runup) u_trj = s3_map.solve_primal(solver,\ solver.u_init, n_steps, solver.s0) dFds = s3_map.compute_source_tangent(solver,\ u_trj, n_steps, s0)[:,0,:] v0_init = zeros_like(u_init) v_trj = solve_tangent_equation(solver, u_trj, v0_init, dFds, n_steps, s0) dJ_trj = gradient_J(u_trj, 0) dJds = compute_dJds(dJ_trj, v_trj) fig = figure(figsize=[15, 10])