|
| 1 | +Poisson equation in 1D |
| 2 | +====================== |
| 3 | + |
| 4 | +Problem setup |
| 5 | +------------- |
| 6 | + |
| 7 | +We will learn the solution operator |
| 8 | + |
| 9 | +.. math:: G: f \mapsto u |
| 10 | + |
| 11 | +for the one-dimensional Poisson problem |
| 12 | + |
| 13 | +.. math:: u''(x) = f(x), \qquad x \in [0, 1], |
| 14 | + |
| 15 | +with zero Dirichlet boundary conditions :math:`u(0) = u(1) = 0`. |
| 16 | + |
| 17 | +The source term :math:`f` is supposed to be an arbitrary continuous function. |
| 18 | + |
| 19 | + |
| 20 | +Implementation |
| 21 | +-------------- |
| 22 | + |
| 23 | +The solution operator can be learned by training a physics-informed DeepONet. |
| 24 | + |
| 25 | +First, we define the PDE with boundary conditions and the domain: |
| 26 | + |
| 27 | +.. code-block:: python |
| 28 | +
|
| 29 | + def equation(x, y, f): |
| 30 | + dy_xx = dde.grad.hessian(y, x) |
| 31 | + return -dy_xx - f |
| 32 | +
|
| 33 | + geom = dde.geometry.Interval(0, 1) |
| 34 | +
|
| 35 | + def u_boundary(_): |
| 36 | + return 0 |
| 37 | +
|
| 38 | + def boundary(_, on_boundary): |
| 39 | + return on_boundary |
| 40 | +
|
| 41 | + bc = dde.icbc.DirichletBC(geom, u_boundary, boundary) |
| 42 | +
|
| 43 | + pde = dde.data.PDE(geom, equation, bc, num_domain=100, num_boundary=2) |
| 44 | +
|
| 45 | +
|
| 46 | +Next, we specify the function space for :math:`f` and the corresponding evaluation points. |
| 47 | +For this example, we use the ``dde.data.PowerSeries`` to get the function space |
| 48 | +of polynomials of degree three. |
| 49 | +Together with the PDE, the function space is used to define a |
| 50 | +PDEOperator ``dde.data.PDEOperatorCartesianProd`` that incorporates the PDE into |
| 51 | +the loss function. |
| 52 | + |
| 53 | +.. code-block:: python |
| 54 | +
|
| 55 | + degree = 3 |
| 56 | + space = dde.data.PowerSeries(N=degree + 1) |
| 57 | +
|
| 58 | + num_eval_points = 10 |
| 59 | + evaluation_points = geom.uniform_points(num_eval_points, boundary=True) |
| 60 | +
|
| 61 | + pde_op = dde.data.PDEOperatorCartesianProd( |
| 62 | + pde, |
| 63 | + space, |
| 64 | + evaluation_points, |
| 65 | + num_function=100, |
| 66 | + ) |
| 67 | +
|
| 68 | +
|
| 69 | +The DeepONet can be defined using ``dde.nn.DeepONetCartesianProd``. |
| 70 | +The branch net is chosen as a fully connected neural network of size ``[m, 32, p]`` where ``p=32`` |
| 71 | +and the trunk net is a fully connected neural network of size ``[dim_x, 32, p]``. |
| 72 | + |
| 73 | +.. code-block:: python |
| 74 | +
|
| 75 | + dim_x = 1 |
| 76 | + p = 32 |
| 77 | + net = dde.nn.DeepONetCartesianProd( |
| 78 | + [num_eval_points, 32, p], |
| 79 | + [dim_x, 32, p], |
| 80 | + activation="tanh", |
| 81 | + kernel_initializer="Glorot normal", |
| 82 | + ) |
| 83 | +
|
| 84 | +
|
| 85 | +We define the ``Model`` and train it with L-BFGS: |
| 86 | + |
| 87 | +.. code-block:: python |
| 88 | +
|
| 89 | + model = dde.Model(pde_op, net) |
| 90 | + dde.optimizers.set_LBFGS_options(maxiter=1000) |
| 91 | + model.compile("L-BFGS") |
| 92 | + model.train() |
| 93 | +
|
| 94 | +Finally, the trained model can be used to predict the solution of the Poisson |
| 95 | +equation. We sample the solution for three random representations of :math:`f`. |
| 96 | + |
| 97 | +.. code-block:: python |
| 98 | +
|
| 99 | + n = 3 |
| 100 | + features = space.random(n) |
| 101 | + fx = space.eval_batch(features, evaluation_points) |
| 102 | +
|
| 103 | + x = geom.uniform_points(100, boundary=True) |
| 104 | + y = model.predict((fx, x)) |
| 105 | +
|
| 106 | +
|
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +Complete code |
| 111 | +------------- |
| 112 | + |
| 113 | +.. literalinclude:: ../../../examples/operator/pideeponet_1d_poisson.py |
| 114 | + :language: python |
0 commit comments