Exact and soft boundary conditions in Physics-Informed Neural Networks
  for the Variable Coefficient Poisson equation
Boundary conditions (BCs) are a key component in every Physics-Informed
Neural Network (PINN). By defining the solution to partial differential
equations (PDEs) along domain boundaries, BCs constrain the underlying boundary
value problem (BVP) that a PINN tries to approximate. Without them, unique PDE
solutions may not exist and finding approximations with PINNs would be a
challenging, if not impossible task. This study examines how soft loss-based
and exact distance function-based BC imposition approaches differ when applied
in PINNs. The well known variable coefficient Poisson equation serves as the
target PDE for all PINN models trained in this work. Besides comparing BC
imposition approaches, the goal of this work is to also provide resources on
how to implement these PINNs in practice. To this end, Keras models with
Tensorflow backend as well as a Python notebook with code examples and
step-by-step explanations on how to build soft/exact BC PINNs are published
alongside this review.
Sebastian Barschkis
# Exact and soft boundary conditions in

###### Abstract

Boundary conditions (BCs) are a key component in every Physics-Informed Neural Network (PINN) [8]. By defining the solution to partial differential equations (PDEs) along domain boundaries, BCs constrain the underlying boundary value problem (BVP) that a PINN tries to approximate. Without them, unique PDE solutions may not exist and finding approximations with PINNs would be a challenging, if not impossible task. This study examines how soft loss-based [8] and exact distance function-based [10] BC imposition approaches differ when applied in PINNs. The well known variable coefficient Poisson equation serves as the target PDE for all PINN models trained in this work. Besides comparing BC imposition approaches, the goal of this work is to also provide resources on how to implement these PINNs in practice. To this end, Keras [5] models with Tensorflow [1] backend as well as a Python notebook with code examples and step-by-step explanations on how to build soft/exact BC PINNs are published alongside this review [2].

## 1 Introduction

Solving the Poisson equation is the most computationally expensive step in many incompressible flow simulations. MFIX's [11] fluid solver, for example, spends more than 80% of its time on solving the Poisson equation. This finding alone motivates the development of and search for new solvers that can accelerate this step. The advantages of faster Poisson solvers would be manifold: For one, PDE solutions could be found using fewer computational resources. For another, more time could be spent testing and evaluating algorithms that are currently constrained by time spent on solving the Poisson equation.

To this end, we examine PINNs, a promising class of neural PDE approximators. Their performance, training time, and accuracy varies depending on the availability of data but also hinges on how this data is processed. The way a PINN learns labelled data points directly affects training and inference times. To better understand which PINN implementations perform best, this work compares two approaches that can be used to impose boundary conditions: the soft and the exact BC imposition approach.

These BC imposition methods have been discussed in detail by Raissi et al. [8] and Sukumar and Srivastava [10]. The primary goal of this work is to show how to embed them in PINNs and provide a flexible, re-usable workflow that can approximate the solution to a PDE. The target PDE in this work is the variable coefficient Poisson equation with Dirichlet boundary conditions.
Related works

A common approach to learn BCs in PINNs is the soft loss-based approach where a BC data loss is part of the total loss. The deep learning framework that introduced PINNs [8] followed this approach by incorporating the Mean-squared-error (MSE) of points found along domain boundaries to the overall loss. Using this approach Raissi et al. [8] show that PINNs can successfully learn periodic and Dirichlet boundary conditions while approximating solutions to instances of Schrodingers, Burgers, and Navier-Stokes equations. Many other PDEs have been solved with PINNs and similar MSE losses for boundary conditions: Bischof and Kraus [3], for instance, showed solutions to Kirchhoff's plate bending problem and the Helmholtz equation.

While having exact values at domain boundaries constrains the problem space, BCs are a sufficient and not a necessary condition for PINNs to converge. If knowledge on domain boundaries is given in another form, PINNs can still find PDE solutions. Raissi et al. [9] trained PINNs that successfully approximate fluid velocities and pressure in laminar flows even in the absence of specific boundary conditions. They show that a concentration field for a passive scalar with sufficient gradients near domain boundaries can compensate the lack of explicit BCs.

Recent works have looked into hard constraints for PDEs in PINNs. Negiar et al. [7] developed a differentiable PDE-constrained layer (PDE-CL) through which hard constraints can be placed on the interior of a domain. Their approach stands in contrast to previous work with hard constraints which finds PDE solutions by enforcing hard constraints at domain boundaries Lu et al. [6], Sukumar and Srivastava [10].

The comparisons shown in this work will make use of the commonly used soft BC imposition approach introduced by Raissi et al. [8]. For exact BCs, Sukumar and Srivastava [10]'s distance-based method will be used.

## 3 Background

All PINNs from this study were trained to approximate Poisson's equation with Dirichlet boundary conditions. Special cases of Poisson's equation, such as Laplace's equation and Poisson's equation with zero-BC, were examined as well.

### Poisson's equation

We consider Poisson's equation in a 2-dimensional (2D) setting. More precisely, the PINNs from this work use the variation of Poisson's equation with variable coefficient and Dirichlet boundary conditions which can be expressed as follows

\[\nabla\cdot(a\nabla p(x,y)) =f(x,y),\qquad(x,y)\in\Omega \tag{1}\] \[p(x,y) =g(x,y),\qquad(x,y)\in\partial\Omega \tag{2}\]

where \(a\) is the variable coefficient, \(f\) the right-hand side (RHS), \(g\) the boundary condition (BC), and \(p\) the solution.

### Variations of Poisson's equation

By forcing certain variables in Poisson's equation (1) to zero we obtain PDE variations that can easily be approximated with the same PINNs. This work considers the cases where either \(f=0\) or \(g=0\). That is, Laplace's equation in the former and the zero BC Poisson equation in the latter case.

### Data generation

Training and validation data for coefficient \(a\), RHS \(f\), and BC \(g\) were generated artificially (i.e. not derived from experiments), uniformly and randomly. The generation process followed the following strategy:
1. Distribute \(n\times n\) knots uniformly and in a grid pattern on the discretized target domain (Figure 0(a)).
2. Generate a Sobol sequence with given minimum and maximum values.
3. For each knot, select a value from the Sobol sequence.
4. Use a Gaussian process to interpolate from the values found at knots and sample interpolated values at every discrete x-y domain position.
5. Repeat this process for variables \(a\), \(f\), and \(g\).

Note that for \(g\), the above process was executed in 1-dimensional (1D) setting since for BCs only a vector that wraps around the 2D domain is needed. This data generation approach ensures that the resulting value distributions in \(a\), \(f\), and \(g\) are smooth and not completely random. I.e. the resulting datasets are free of discontinuities. Since PINN models compute physics losses and expect training data to be differentiable, smooth value distributions are highly desirable.

## 4 Soft and exact BCs in PINNs

### Loss function

The total loss consists of a supervised data and a PDE loss:

\[\mathcal{L}_{Total}=\mathcal{L}_{Data}+\alpha\cdot\mathcal{L}_{PDE}\]

Both \(\mathcal{L}_{Data}\) and \(\mathcal{L}_{PDE}\) use the Mean-squared-error (MSE) metric.

\(\mathcal{L}_{Data}\) is used to compute the error for points near the domain boundary. While soft BC models rely on minimized errors at these points, exact BC models are less impacted by them as they enforce BCs at domain boundaries and, hence, errors in these areas will be very small anyways. \(\mathcal{L}_{PDE}\) on the other hand is plays a key role in both soft and exact BC models. It measures the error between \(\nabla\cdot(a\nabla p)\) and RHS \(f\). The solution in the domain interior depends on this error.

### Soft BC models

When sampling points uniformly and randomly in domain space, the number of points placed at domain boundaries is usually insufficient for PINNs to learn BCs efficiently. As a result, training becomes inefficient since the PDE loss computed at collocation points found in the domain interior cannot converge sufficiently due to the lack of learned BC knowledge. A common strategy that compensates this behavior is to selectively sample more points near domain boundaries (Figure 0(b)). These additional domain boundary points make it possible to minimize the data loss \(\mathcal{L}_{Data}\) quicker and consequently also help to find the PDE solution values that minimize \(\mathcal{L}_{PDE}\) in the domain interior.

Figure 1: Knots (4x4) in domain and interpolated instances for \(a\) and \(f\) (32x32). (0(a)): Knots (big dots) and x-y coordinates (small dots) in domain space. (0(b)): \(a\) after interpolating from knots. (0(c)): \(f\) after interpolating from knots.
### Exact BC models

The exact BC imposition approach with distance functions follows work from Sukumar and Srivastava [10]. Their approach is based on the idea that instead of learning BCs through a loss term, BCs can be hard-coded into PINNs too. By reformulating network solutions as functions of an interpolation and a filter function, the exact BC becomes part of the predicted solution. I.e. the approximation \(\hat{p}\) from a neural network with exact BCs can be expressed as

\[\hat{p}(x,y)=G(x,y)+\phi(x,y)\cdot\hat{p}(x,y)\]

where \(G(x,y)\) is the interpolating and \(\phi(x,y)\) the filter function.

#### 4.3.1 Interpolation function

\(G(x,y)\) is the function that interpolates BCs. That is, at any position \((x,y)\) in the domain \(G\) returns an approximated solution value that is only based on the values found at domain boundaries. By

Figure 3: Exact BC enforcement in a fully-connected architecture: The BC is “injected” into the result tensor.

Figure 2: Points sampled in domain space: (2a) 200 collocation points, sampled randomly and uniformly. (2b) Collocation points with 20 additional boundary points per domain side. (Note: number of points shown only for illustration purposes, not used in actual models).
using an interpolation method such as Inverse-Distance-Weighting (IDW), it is possible to broadcast the BC to the domain interior with varying weight. IDW achieves this by measuring the distance from collocation points to each boundary point and then computing the weighted solution value based on the distance to the BC points.

\[G(\mathbf{x}) =\sum_{i}^{N_{bc}}\frac{w_{i}z_{i}}{\sum_{i}^{N_{bc}}w_{i}}\] \[w_{i} =|\mathbf{x}-\mathbf{x_{i}}|\]

Note that special treatment is required in situations where a collocation point coincides with a BC point, i.e. the case where \(w_{i}=0\). The resulting zero-division can be avoid in practice by adding \(\epsilon>0\) to \(w_{i}\).

#### 4.3.2 Filter function

Filter function \(\phi(x,y)\) forms the second part in exact BC models. It scales the network and reduces output contributions in areas close to the BC. \(\phi(x,y)\) should be smooth and chosen with domain knowledge in mind. Depending on the shape of the domain, the domain sides where exact BCs should be enforced, and the overall filtering strength, \(\phi(x,y)\) can take different shapes. For square domains and exact BCs along all domain sides, a good choice is

\[\phi(x,y)=x\cdot(1-x)\cdot y\cdot(1-y)\]

where \(x\) and \(y\) are locations in a domain with width and height in range \([0,1]\).

## 5 Experiments

The PINN models with soft and exact BCs were trained on instances of the Poisson equation with non-zero BC, the Laplace equation, and the Poisson equation with zero-BC. To ensure comparability, all datasets had the same dimension (128x128) and were used to train one soft BC and one exact BC model. I.e. every side-by-side comparision of soft and exact BC models is the result from training with the same instances of \(a\), \(f\), and \(g\).

All models were trained using fully-connected architectures with 4 layers and 128 neurons per layer. \(GELU\) activations were used in all layers except for the last layer which used a linear activation. The optimizer was a Adam optimizer with \(\beta_{1}=0.9\), \(\beta_{2}=0.999\) and initial learning rate of \(0.0005\). Learning rate was dropped on plateau with factor \(0.1\) after a patience of \(10\) epochs and \(\delta_{min}=0.01\).

In both soft and exact BC models, the loss was computed using \(10.000\) (collocation) points, each of which contributed to the PDE or data loss. The latter was only computed for points in vicinity to domain boundaries. In soft BC models, an additional \(1.000\) boundary points were sampled near domain boundaries. Exact BC models did not make use of additional boundary points. Instead, to compute \(G\), they used \(20\) boundary condition points per domain side.

### Parameter selection

\(GELU\) was chosen over \(tanh\) as the activation function since models tended to yield slightly lowers errors in the former case. The choice, however, is highly dependent on training data. Poisson datasets were the random generator produced narrow value distributions (i.e. datasets were values at knots (see section 3.3) were close to each other) benefited more from \(tanh\) activations.

The choice of \(\alpha=0.1\) was based on the assumption that the number of collocation and boundary points has a ratio of 10:1. Exact BC models don't have to learn the BC through a loss term and could use a larger \(\alpha\). However, to ensure fairness during training, \(\alpha=0.1\) was used in both soft and exact BC models.
### Poisson's equation with non-zero BC

The plots in Figure 4 show the results for solution \(p\) to an instance of Poisson's equation with non-zero BC. Grids for \(a\) and \(g\) resulted from knots with random values in range \([-1,1]\). \(f\) was generated with knots ranging from \([-10,10]\).

### Laplace's equation

The models approximating Laplace's equation, shown in Figure 5, are based on the same training data used in the models for Poisson's equation with non-zero BC (Section 5.2). Only RHS \(f=0\) differs.

### Poisson's equation with zero BC

Similarly to the experiments with non-zero BCs, the zero-BC model for Poisson was trained with the same \(a\) and \(f\). Note how since \(g=0\), the solutions \(p\) are on a different scale.

\begin{table}
\begin{tabular}{c c c c c c} \hline \hline Dataset \# & BC Type & MAE & RMSE & MAPE & Avg. time / epoch (sec) \\ \hline \multirow{2}{*}{0} & Soft & 4.97e-2 & 7.01e-2 & 114.55\% & 1.78 \\  & Exact & 4.29e-2 & 6.46e-2 & 116.48\% & 1.86 \\ \hline \hline \end{tabular}
\end{table}
Table 1: Error metrics for models solving Poisson’s equation with non-zero BC (plots in Figure 4)

Figure 4: Results from models solving Poisson’s equation with non-zero BC.

Figure 5: Results from models solving Laplace’s equation with non-zero BC.

\begin{table}
\begin{tabular}{c c c c c c} \hline \hline Dataset \# & BC Type & MAE & RMSE & MAPE & Avg. time / epoch (sec) \\ \hline \multirow{2}{*}{0} & Soft & 2.53e-2 & 3.19e-2 & 150.71\% & 1.79 \\  & Exact & 2.19e-2 & 2.99e-2 & 140.02\% & 1.84 \\ \hline \hline \end{tabular}
\end{table}
Table 2: Error metrics for models solving Laplace’s equation with non-zero BC (plots in Figure 5)
## 6 Conclusions

The experiments with both Poisson's and Laplace's equation have shown that exact BC models generally yield smaller errors than soft BC models. Since exact BC models don't have to learn boundary points, training can focus on collocation points and minimizing the PDE loss. I.e. all effort goes into learning areas where no labelled data is present and where hidden physics should be recovered.

However, exact BC models are not always the better choice. The following points should be considered when using exact BCs:

* When increasing the number of BC points in exact BC models, both training and inference times will be prolonged due to the interpolation overhead (every collocation point has to evaluate every BC point).
* The number of BC points in exact BC models should be chosen carefully and with the domain size in mind. A small domain will require fewer BC points than a larger domain.
* Filter function \(\phi\) should be chosen carefully as well. Especially when the domain is non-square or when boundary conditions are not enforced everywhere. An inappropriate choice of \(\phi\) can significantly increase training times and lead to models that converge to sub-optimal solutions.

## 7 Additional resources

This work includes a notebook [2] with annotations and code examples that show how to implement soft and exact BCs in PINN models. All models in this notebook are based on Keras[5] and Tensorflow [1] as the backend. The code is open-source and readers are free to use it in their own PINN projects.

## 8 Future work

Multiple PINN instances from this study could be combined into a Mixture-of-Experts (MoE) PINN [4]. I.e. a network composed of multiple child PINNs and one gating network that combines their output (e.g. through the use of a softmax function). By using such an architecture, solutions would no longer be constrained to a single BC and the Poisson equation could be solved for arbitrary BCs. Future studies should evaluate the accuracy and performance of such an MoE Poisson PINN. Additionally, a comparison with existing Poisson neural operators that can solve for arbitrary BCs should be carried out.

Figure 6: Results from models solving Poisson’s equation with non-zero BC.

\begin{table}
\begin{tabular}{c c c c c c} \hline \hline Dataset \# & BC Type & MAE & RMSE & MAPE & Time / epoch (sec) \\ \hline \multirow{2}{*}{0} & Soft & 6.08e-2 & 8.39e-2 & 152.46\% & 1.77 \\  & Exact & 5.79e-2 & 8.27e-2 & 119.99\% & 1.86 \\ \hline \hline \end{tabular}
\end{table}
Table 3: Error metrics for models solving Poisson’s equation with non-zero BC (plots in Figure 6)
While the above PINN ensembles would consist of PINNs trained on different instances of the same equation (e.g. mutiple instances for Poisson non-zero BC), PINN ensembles could also consist of PINNs trained on different equations. The Laplace and Poisson zero-BC PINNs from this work, for example, could be combined into an MoE PINN that solves the Poisson non-zero BC equation. The gating function could leverage the superposition principle

\[p_{\text{Laplace}}+p_{\text{Poisson zero BC}}=p_{\text{Poisson non-zero BC}}\]

In general, more research should evaluate the benefits of splitting up PDEs into simpler ones, and approximating solutions with PINN ensembles.
