def test_pidon_operator_on_ode_with_analytic_solution(): set_random_seed(0) r = 4. y_0 = 1. diff_eq = PopulationGrowthEquation(r) cp = ConstrainedProblem(diff_eq) t_interval = (0., .25) ic = ContinuousInitialCondition(cp, lambda _: np.array([y_0])) sampler = UniformRandomCollocationPointSampler() pidon = PIDONOperator(sampler, .001, True) training_loss_history, test_loss_history = pidon.train( cp, t_interval, training_data_args=DataArgs( y_0_functions=[ic.y_0], n_domain_points=25, n_batches=5, n_ic_repeats=5 ), model_args=ModelArgs( latent_output_size=1, trunk_hidden_layer_sizes=[50, 50, 50], ), optimization_args=OptimizationArgs( optimizer=optimizers.SGD(), epochs=100, verbose=False ), secondary_optimization_args=SecondaryOptimizationArgs( max_iterations=100, verbose=False ) ) assert len(training_loss_history) == 101 assert len(test_loss_history) == 0 assert training_loss_history[-1].weighted_total_loss.numpy() < 5e-5 ivp = InitialValueProblem( cp, t_interval, ic, lambda _ivp, t, x: np.array([y_0 * np.e ** (r * t)]) ) solution = pidon.solve(ivp) assert solution.d_t == .001 assert solution.discrete_y().shape == (250, 1) analytic_y = np.array([ivp.exact_y(t) for t in solution.t_coordinates]) assert np.mean(np.abs(analytic_y - solution.discrete_y())) < 1e-3 assert np.max(np.abs(analytic_y - solution.discrete_y())) < 2.5e-3
def test_initial_value_problem_without_exact_solution(): diff_eq = PopulationGrowthEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.array([0.])) ivp = InitialValueProblem(cp, (0., 2.), ic) assert not ivp.has_exact_solution with pytest.raises(RuntimeError): ivp.exact_y(2.)
def test_initial_value_problem(): y_0 = 100 r = .5 diff_eq = PopulationGrowthEquation(r) cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.array([y_0])) ivp = InitialValueProblem( cp, (0., 2.), ic, lambda _ivp, t, x: np.array([y_0 * np.e**(r * t)])) assert ivp.has_exact_solution assert ivp.constrained_problem == cp assert ivp.t_interval == (0., 2.) assert ivp.initial_condition == ic assert np.allclose(ivp.exact_y(0.), [y_0])
def test_solution_ode(): diff_eq = LotkaVolterraEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.array([5., 10.])) ivp = InitialValueProblem(cp, (0., 10.), ic) t_coordinates = np.array([5., 10.]) discrete_y = np.arange(4).reshape((2, 2)) solution = Solution(ivp, t_coordinates, discrete_y) assert solution.initial_value_problem == ivp assert np.array_equal(solution.t_coordinates, t_coordinates) assert np.isclose(solution.d_t, 5.) assert solution.vertex_oriented is None assert np.allclose(solution.y(), [[0., 1.], [2., 3.]]) assert np.allclose(solution.discrete_y(), [[0., 1.], [2., 3.]]) other_solutions = [ Solution(ivp, np.linspace(2.5, 10., 4), np.arange(8).reshape((4, 2))), Solution(ivp, np.linspace(1.25, 10., 8), np.arange(16).reshape((8, 2))) ] expected_differences = [ [[2., 2.], [4., 4.]], [[6., 6.], [12., 12.]], ] diff = solution.diff(other_solutions) assert np.allclose(diff.matching_time_points, [5., 10.]) assert np.allclose(diff.differences, expected_differences)
def test_pidon_operator_on_spherical_pde(): set_random_seed(0) diff_eq = DiffusionEquation(3) mesh = Mesh( [(1., 11.), (0., 2 * np.pi), (.25 * np.pi, .75 * np.pi)], [2., np.pi / 5., np.pi / 4], CoordinateSystem.SPHERICAL) bcs = [ (DirichletBoundaryCondition( lambda x, t: np.ones((len(x), 1)), is_static=True), DirichletBoundaryCondition( lambda x, t: np.full((len(x), 1), 1. / 11.), is_static=True)), (NeumannBoundaryCondition( lambda x, t: np.zeros((len(x), 1)), is_static=True), NeumannBoundaryCondition( lambda x, t: np.zeros((len(x), 1)), is_static=True)), (NeumannBoundaryCondition( lambda x, t: np.zeros((len(x), 1)), is_static=True), NeumannBoundaryCondition( lambda x, t: np.zeros((len(x), 1)), is_static=True)) ] cp = ConstrainedProblem(diff_eq, mesh, bcs) ic = ContinuousInitialCondition(cp, lambda x: 1. / x[:, :1]) t_interval = (0., .5) ivp = InitialValueProblem(cp, t_interval, ic) sampler = UniformRandomCollocationPointSampler() pidon = PIDONOperator(sampler, .001, True) training_loss_history, test_loss_history = pidon.train( cp, t_interval, training_data_args=DataArgs( y_0_functions=[ic.y_0], n_domain_points=20, n_boundary_points=10, n_batches=1 ), model_args=ModelArgs( latent_output_size=20, branch_hidden_layer_sizes=[30, 30], trunk_hidden_layer_sizes=[30, 30], ), optimization_args=OptimizationArgs( optimizer=optimizers.Adam(learning_rate=2e-5), epochs=3, verbose=False ) ) assert len(training_loss_history) == 3 for i in range(2): assert np.all( training_loss_history[i + 1].weighted_total_loss.numpy() < training_loss_history[i].weighted_total_loss.numpy()) solution = pidon.solve(ivp) assert solution.d_t == .001 assert solution.discrete_y().shape == (500, 6, 11, 3, 1)
def test_solution_generate_plots_for_2d_pde_with_scalar_and_vector_fields(): diff_eq = ShallowWaterEquation(.5) mesh = Mesh([(0., 5.), (0., 5.)], [1., 1.]) bcs = [(NeumannBoundaryCondition( vectorize_bc_function(lambda x, t: (.0, None, None)), is_static=True), NeumannBoundaryCondition( vectorize_bc_function(lambda x, t: (.0, None, None)), is_static=True))] * 2 cp = ConstrainedProblem(diff_eq, mesh, bcs) ic = GaussianInitialCondition( cp, [(np.array([2.5, 1.25]), np.array([[1., 0.], [0., 1.]]))] * 3, [1., .0, .0]) ivp = InitialValueProblem(cp, (0., 20.), ic) t_coordinates = np.array([10., 20.]) discrete_y = np.arange(216).reshape((2, 6, 6, 3)) solution = Solution(ivp, t_coordinates, discrete_y, vertex_oriented=True) plots = list(solution.generate_plots()) try: assert len(plots) == 4 assert isinstance(plots[0], QuiverPlot) assert isinstance(plots[1], StreamPlot) assert isinstance(plots[2], ContourPlot) assert isinstance(plots[3], SurfacePlot) finally: for plot in plots: plot.close()
def test_auto_regression_operator_on_ode_with_isolated_perturbations(): set_random_seed(0) diff_eq = LotkaVolterraEquation(2., 1., .8, 1.) cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.array([1., 2.])) ivp = InitialValueProblem(cp, (0., 10.), ic) oracle = ODEOperator('DOP853', .001) ref_solution = oracle.solve(ivp) ml_op = AutoRegressionOperator(2.5, True) ml_op.train(ivp, oracle, RandomForestRegressor(), 25, lambda t, y: y + np.random.normal(0., .01, size=y.shape), isolate_perturbations=True) ml_solution = ml_op.solve(ivp) assert ml_solution.vertex_oriented assert ml_solution.d_t == 2.5 assert ml_solution.discrete_y().shape == (4, 2) diff = ref_solution.diff([ml_solution]) assert np.all(diff.matching_time_points == np.linspace(2.5, 10., 4)) assert np.max(np.abs(diff.differences[0])) < .01
def test_initial_value_problem_with_invalid_time_interval(): diff_eq = PopulationGrowthEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.array([0.])) with pytest.raises(ValueError): InitialValueProblem(cp, (3., 2.), ic)
def test_fdm_operator_on_2d_pde(): diff_eq = NavierStokesEquation(5000.) mesh = Mesh([(0., 10.), (0., 10.)], [1., 1.]) bcs = [(DirichletBoundaryCondition( vectorize_bc_function(lambda x, t: (1., .1, None, None)), is_static=True), DirichletBoundaryCondition( vectorize_bc_function(lambda x, t: (0., 0., None, None)), is_static=True)), (DirichletBoundaryCondition( vectorize_bc_function(lambda x, t: (0., 0., None, None)), is_static=True), DirichletBoundaryCondition( vectorize_bc_function(lambda x, t: (0., 0., None, None)), is_static=True))] cp = ConstrainedProblem(diff_eq, mesh, bcs) ic = ContinuousInitialCondition(cp, lambda x: np.zeros((len(x), 4))) ivp = InitialValueProblem(cp, (0., 10.), ic) op = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .25) solution = op.solve(ivp) assert solution.vertex_oriented assert solution.d_t == .25 assert solution.discrete_y().shape == (40, 11, 11, 4) assert solution.discrete_y(False).shape == (40, 10, 10, 4)
def test_fdm_operator_on_pde_with_t_and_x_dependent_rhs(): class TestDiffEq(DifferentialEquation): def __init__(self): super(TestDiffEq, self).__init__(2, 1) @property def symbolic_equation_system(self) -> SymbolicEquationSystem: return SymbolicEquationSystem([ self.symbols.t / 100. * (self.symbols.x[0] + self.symbols.x[1])**2 ]) diff_eq = TestDiffEq() mesh = Mesh([(-5., 5.), (0., 3.)], [2., 1.]) bcs = [(NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 1))), NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 1))))] * 2 cp = ConstrainedProblem(diff_eq, mesh, bcs) ic = ContinuousInitialCondition(cp, lambda x: np.zeros((len(x), 1))) ivp = InitialValueProblem(cp, (0., 5.), ic) op = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .25) solution = op.solve(ivp) y = solution.discrete_y() assert solution.vertex_oriented assert solution.d_t == .25 assert y.shape == (20, 6, 4, 1)
def test_solution_with_invalid_t_coordinate_dimensions(): diff_eq = LotkaVolterraEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.array([5., 10.])) ivp = InitialValueProblem(cp, (0., 10.), ic) discrete_y = np.zeros((2, 2)) with pytest.raises(ValueError): Solution(ivp, np.array([[5., 10.]]), discrete_y)
def test_solution_with_mismatched_discrete_y_shape(): diff_eq = LotkaVolterraEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.array([5., 10.])) ivp = InitialValueProblem(cp, (0., 10.), ic) discrete_y = np.zeros((2, 3)) with pytest.raises(ValueError): Solution(ivp, np.array([5., 10.]), discrete_y)
def test_fdm_operator_on_pde_with_t_and_x_dependent_rhs(): class TestDiffEq(DifferentialEquation): def __init__(self): super(TestDiffEq, self).__init__(2, 1) @property def symbolic_equation_system(self) -> SymbolicEquationSystem: return SymbolicEquationSystem([ self.symbols.t / 100. * (self.symbols.x[0] + self.symbols.x[1]) ** 2 ]) diff_eq = TestDiffEq() mesh = Mesh([(-1., 1.), (0., 2.)], [2., 1.]) bcs = [ (NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 1))), NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 1)))) ] * 2 cp = ConstrainedProblem(diff_eq, mesh, bcs) ic = ContinuousInitialCondition(cp, lambda x: np.zeros((len(x), 1))) t_interval = (0., 1.) ivp = InitialValueProblem(cp, t_interval, ic) sampler = UniformRandomCollocationPointSampler() pidon = PIDONOperator(sampler, .05, True) training_loss_history, test_loss_history = pidon.train( cp, t_interval, training_data_args=DataArgs( y_0_functions=[ic.y_0], n_domain_points=20, n_boundary_points=10, n_batches=1 ), model_args=ModelArgs( latent_output_size=20, branch_hidden_layer_sizes=[30, 30], trunk_hidden_layer_sizes=[30, 30], ), optimization_args=OptimizationArgs( optimizer=optimizers.Adam(learning_rate=2e-5), epochs=3, verbose=False ) ) assert len(training_loss_history) == 3 for i in range(2): assert np.all( training_loss_history[i + 1].weighted_total_loss.numpy() < training_loss_history[i].weighted_total_loss.numpy()) solution = pidon.solve(ivp) assert solution.d_t == .05 assert solution.discrete_y().shape == (20, 2, 3, 1)
def test_pidon_operator_on_pde_system(): set_random_seed(0) diff_eq = NavierStokesEquation() mesh = Mesh([(-2.5, 2.5), (0., 4.)], [1., 1.]) ic_function = vectorize_ic_function(lambda x: [ 2. * x[0] - 4., 2. * x[0] ** 2 + 3. * x[1] - x[0] * x[1] ** 2, 4. * x[0] - x[1] ** 2, 2. * x[0] * x[1] - 3. ]) bcs = [ (DirichletBoundaryCondition( lambda x, t: ic_function(x), is_static=True), DirichletBoundaryCondition( lambda x, t: ic_function(x), is_static=True)) ] * 2 cp = ConstrainedProblem(diff_eq, mesh, bcs) ic = ContinuousInitialCondition(cp, ic_function) t_interval = (0., .5) ivp = InitialValueProblem(cp, t_interval, ic) sampler = UniformRandomCollocationPointSampler() pidon = PIDONOperator(sampler, .001, True) training_loss_history, test_loss_history = pidon.train( cp, t_interval, training_data_args=DataArgs( y_0_functions=[ic.y_0], n_domain_points=20, n_boundary_points=10, n_batches=1 ), model_args=ModelArgs( latent_output_size=20, branch_hidden_layer_sizes=[20, 20], trunk_hidden_layer_sizes=[20, 20], ), optimization_args=OptimizationArgs( optimizer=optimizers.Adam(learning_rate=1e-5), epochs=3, verbose=False ) ) assert len(training_loss_history) == 3 for i in range(2): assert np.all( training_loss_history[i + 1].weighted_total_loss.numpy() < training_loss_history[i].weighted_total_loss.numpy()) solution = pidon.solve(ivp) assert solution.d_t == .001 assert solution.discrete_y().shape == (500, 6, 5, 4)
def test_ode_operator_on_ode(): diff_eq = LotkaVolterraEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.array([100., 15.])) ivp = InitialValueProblem(cp, (0., 10.), ic) op = ODEOperator('DOP853', 1e-3) solution = op.solve(ivp) assert solution.vertex_oriented is None assert solution.d_t == 1e-3 assert solution.discrete_y().shape == (1e4, 2)
def test_ode_operator_on_ode_with_analytic_solution(): r = .02 y_0 = 100. diff_eq = PopulationGrowthEquation(r) cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.array([y_0])) ivp = InitialValueProblem( cp, (0., 10.), ic, lambda _ivp, t, x: np.array([y_0 * np.e**(r * t)])) op = ODEOperator('DOP853', 1e-4) solution = op.solve(ivp) assert solution.d_t == 1e-4 assert solution.discrete_y().shape == (1e5, 1) analytic_y = np.array([ivp.exact_y(t) for t in solution.t_coordinates]) assert np.allclose(analytic_y, solution.discrete_y())
def test_auto_regression_operator_with_wrong_perturbed_initial_value_shape(): diff_eq = LorenzEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.ones(3)) ivp = InitialValueProblem(cp, (0., 10.), ic) oracle = ODEOperator('DOP853', .001) ml_op = AutoRegressionOperator(2.5, True) with pytest.raises(ValueError): ml_op.train(ivp, oracle, LinearRegression(), 25, lambda t, y: np.array([1.]))
def test_auto_regression_operator_training_with_zero_iterations(): diff_eq = LorenzEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.ones(3)) ivp = InitialValueProblem(cp, (0., 10.), ic) oracle = ODEOperator('DOP853', .001) ml_op = AutoRegressionOperator(2.5, True) with pytest.raises(ValueError): ml_op.train(ivp, oracle, LinearRegression(), 0, lambda t, y: y + np.random.normal(0., .01, size=y.shape))
def test_parareal_operator_with_wrong_g_time_step_size(): diff_eq = PopulationGrowthEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.array([100.])) ivp = InitialValueProblem(cp, (0., 10.), ic) f = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .1) g = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .6) p = PararealOperator(f, g, .001) with pytest.raises(ValueError): p.solve(ivp)
def test_fdm_operator_on_ode(): diff_eq = LorenzEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.ones(3)) ivp = InitialValueProblem(cp, (0., 10.), ic) op = FDMOperator(ForwardEulerMethod(), ThreePointCentralDifferenceMethod(), .01) solution = op.solve(ivp) assert solution.vertex_oriented assert solution.d_t == .01 assert solution.discrete_y().shape == (1000, 3)
def test_ode_operator_on_pde(): diff_eq = DiffusionEquation(1, 1.5) mesh = Mesh([(0., 10.)], [.1]) bcs = [ (NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 1))), DirichletBoundaryCondition(lambda x, t: np.zeros((len(x), 1)))), ] cp = ConstrainedProblem(diff_eq, mesh, bcs) ic = GaussianInitialCondition(cp, [(np.array([5.]), np.array([[2.5]]))], [20.]) ivp = InitialValueProblem(cp, (0., 10.), ic) op = ODEOperator('RK23', 2.5e-3) with pytest.raises(ValueError): op.solve(ivp)
def test_solution_pde_with_no_vertex_orientation_defined(): diff_eq = WaveEquation(1) mesh = Mesh([(0., 2.)], [1.]) bcs = [(NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 2))), NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 2))))] cp = ConstrainedProblem(diff_eq, mesh, bcs) ic = GaussianInitialCondition(cp, [(np.array([1.]), np.array([[1.]]))] * 2) ivp = InitialValueProblem(cp, (0., 2.), ic) t_coordinates = np.array([1., 2.]) discrete_y = np.arange(12).reshape((2, 3, 2)) with pytest.raises(ValueError): Solution(ivp, t_coordinates, discrete_y)
def test_pidon_operator_in_ar_mode_on_pde(): set_random_seed(0) diff_eq = WaveEquation(1) mesh = Mesh([(0., 1.)], (.2,)) bcs = [ (NeumannBoundaryCondition( lambda x, t: np.zeros((len(x), 2)), is_static=True), NeumannBoundaryCondition( lambda x, t: np.zeros((len(x), 2)), is_static=True)), ] cp = ConstrainedProblem(diff_eq, mesh, bcs) t_interval = (0., 1.) ic = BetaInitialCondition(cp, [(3.5, 3.5), (3.5, 3.5)]) ivp = InitialValueProblem(cp, t_interval, ic) training_y_0_functions = [ BetaInitialCondition(cp, [(p, p), (p, p)]).y_0 for p in [2., 3., 4., 5.] ] sampler = UniformRandomCollocationPointSampler() pidon = PIDONOperator(sampler, .25, False, auto_regression_mode=True) assert pidon.auto_regression_mode pidon.train( cp, (0., .25), training_data_args=DataArgs( y_0_functions=training_y_0_functions, n_domain_points=50, n_boundary_points=20, n_batches=2 ), model_args=ModelArgs( latent_output_size=50, branch_hidden_layer_sizes=[50, 50], trunk_hidden_layer_sizes=[50, 50], ), optimization_args=OptimizationArgs( optimizer=optimizers.Adam(learning_rate=1e-4), epochs=2, ic_loss_weight=10., verbose=False ) ) sol = pidon.solve(ivp) assert np.allclose(sol.t_coordinates, [.25, .5, .75, 1.]) assert sol.discrete_y().shape == (4, 5, 2)
def test_parareal_operator_on_ode_system(): diff_eq = LorenzEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.ones(3)) ivp = InitialValueProblem(cp, (0., 10.), ic) f = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .01) g = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .1) p = PararealOperator(f, g, .005) f_solution = f.solve(ivp) p_solution = p.solve(ivp) assert p_solution.vertex_oriented == f_solution.vertex_oriented assert p_solution.d_t == f_solution.d_t assert np.array_equal(p_solution.t_coordinates, f_solution.t_coordinates) assert np.allclose(p_solution.discrete_y(), f_solution.discrete_y())
def test_fdm_operator_on_1d_pde(): diff_eq = BurgerEquation(1, 1000.) mesh = Mesh([(0., 10.)], [.1]) bcs = [(NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 1)), is_static=True), NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 1)), is_static=True))] cp = ConstrainedProblem(diff_eq, mesh, bcs) ic = GaussianInitialCondition(cp, [(np.array([2.5]), np.array([[1.]]))]) ivp = InitialValueProblem(cp, (0., 50.), ic) op = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .25) solution = op.solve(ivp) assert solution.vertex_oriented assert solution.d_t == .25 assert solution.discrete_y().shape == (200, 101, 1) assert solution.discrete_y(False).shape == (200, 100, 1)
def test_parareal_operator_in_serial_mode(): diff_eq = PopulationGrowthEquation() cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.array([100.])) ivp = InitialValueProblem(cp, (0., 10.), ic) f = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .1) g = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .5) p = PararealOperator(f, g, .001) f_solution = f.solve(ivp) p_solution = p.solve(ivp, parallel_enabled=False) assert p_solution.vertex_oriented == f_solution.vertex_oriented assert p_solution.d_t == f_solution.d_t assert np.array_equal(p_solution.t_coordinates, f_solution.t_coordinates) assert np.array_equal(p_solution.discrete_y(), f_solution.discrete_y())
def test_pidon_operator_in_ar_mode_on_ode(): set_random_seed(0) diff_eq = PopulationGrowthEquation() cp = ConstrainedProblem(diff_eq) t_interval = (0., 1.) ic = ContinuousInitialCondition(cp, lambda _: np.array([1.])) ivp = InitialValueProblem(cp, t_interval, ic) sampler = UniformRandomCollocationPointSampler() pidon = PIDONOperator(sampler, .25, True, auto_regression_mode=True) assert pidon.auto_regression_mode pidon.train( cp, (0., .25), training_data_args=DataArgs( y_0_functions=[ lambda _, _y_0=y_0: np.array([_y_0]) for y_0 in np.linspace(.5, 2., 10) ], n_domain_points=50, n_batches=1 ), model_args=ModelArgs( latent_output_size=1, trunk_hidden_layer_sizes=[50, 50, 50], ), optimization_args=OptimizationArgs( optimizer={ 'class_name': 'Adam', 'config': { 'learning_rate': optimizers.schedules.ExponentialDecay( 1e-2, decay_steps=25, decay_rate=.95) } }, epochs=5, verbose=False ) ) sol = pidon.solve(ivp) assert np.allclose(sol.t_coordinates, [.25, .5, .75, 1.]) assert sol.discrete_y().shape == (4, 1)
def test_solution_generate_plots_for_n_body_simulation(): diff_eq = NBodyGravitationalEquation(2, [5., 5.]) cp = ConstrainedProblem(diff_eq) ic = ContinuousInitialCondition(cp, lambda _: np.ones(diff_eq.y_dimension)) ivp = InitialValueProblem(cp, (0., 10.), ic) t_coordinates = np.array([5., 10.]) discrete_y = \ np.arange(2 * diff_eq.y_dimension).reshape((2, diff_eq.y_dimension)) solution = Solution(ivp, t_coordinates, discrete_y) plots = list(solution.generate_plots()) try: assert len(plots) == 1 assert isinstance(plots[0], NBodyPlot) finally: for plot in plots: plot.close()
def test_fdm_operator_on_spherical_pde(): diff_eq = DiffusionEquation(3) mesh = Mesh([(1., 11.), (0., 2. * np.pi), (.1 * np.pi, .9 * np.pi)], [2., np.pi / 5., np.pi / 5], CoordinateSystem.SPHERICAL) bcs = [(NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 1)), is_static=True), NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 1)), is_static=True))] * 3 cp = ConstrainedProblem(diff_eq, mesh, bcs) ic = ContinuousInitialCondition(cp, lambda x: 1. / x[:, :1]) ivp = InitialValueProblem(cp, (0., 5.), ic) op = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .1) solution = op.solve(ivp) assert solution.vertex_oriented assert solution.d_t == .1 assert solution.discrete_y().shape == (50, 6, 11, 5, 1) assert solution.discrete_y(False).shape == (50, 5, 10, 4, 1)
def test_fdm_operator_on_3d_pde(): diff_eq = CahnHilliardEquation(3) mesh = Mesh([(0., 5.), (0., 5.), (0., 10.)], [.5, 1., 2.]) bcs = [(NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 2)), is_static=True), NeumannBoundaryCondition(lambda x, t: np.zeros((len(x), 2)), is_static=True))] * 3 cp = ConstrainedProblem(diff_eq, mesh, bcs) ic = DiscreteInitialCondition( cp, .05 * np.random.uniform(-1., 1., cp.y_shape(True)), True) ivp = InitialValueProblem(cp, (0., 5.), ic) op = FDMOperator(RK4(), ThreePointCentralDifferenceMethod(), .05) solution = op.solve(ivp) assert solution.vertex_oriented assert solution.d_t == .05 assert solution.discrete_y().shape == (100, 11, 6, 6, 2) assert solution.discrete_y(False).shape == (100, 10, 5, 5, 2)