# \`NeuralPDE.jl\`: How to impose Dirichlet boundary conditions as hard constraints?

**URL:** https://discourse.julialang.org/t/neuralpde-jl-how-to-impose-dirichlet-boundary-conditions-as-hard-constraints/132007
**Category:** Modelling & Simulations
**Tags:** pde, machine-learning, sciml
**Created:** [September 1, 2025, 9:43am UTC](https://discourse.julialang.org/t/neuralpde-jl-how-to-impose-dirichlet-boundary-conditions-as-hard-constraints/132007 "2025-09-01T09:43:51Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Sigmund](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Sigmund](https://discourse.julialang.org/u/Sigmund)
#### Post date: [September 1, 2025, 9:43am UTC](https://discourse.julialang.org/t/neuralpde-jl-how-to-impose-dirichlet-boundary-conditions-as-hard-constraints/132007/1 "2025-09-01T09:43:51Z")

</div>

Im solving a coupled PDE-problem using `NeuralPDE.jl`, the boundary conditions are now set by the penalization method.

Instead I want to apply these as hard boundary constraints, or as an output transformation, as it is done in [DeepXDE](https://deepxde.readthedocs.io/en/latest/demos/pinn_forward/diffusion.1d.exactBC.html) in Python.

The only option I’ve found for Julia, is an approach used in [this blog post](https://www.stochasticlifestyle.com/machine-learning-with-hard-constraints-neural-differential-algebraic-equations-daes-as-a-general-formalism/), however this example is for DAEs, and not PDEs.

Can hard boundary constraints be set when using `NeuralPDE.jl`?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 6, 2025, 10:24pm UTC](https://discourse.julialang.org/t/neuralpde-jl-how-to-impose-dirichlet-boundary-conditions-as-hard-constraints/132007/2 "2025-09-06T22:24:28Z")

</div>

You can just do thisby putting a last layer. For example, if you want Dirchlet s.t. `u(0)=u(1)=0`, you just put a layer defined as `f(x) = x*(x-1)*NN(x)` and the property is guaranteed by design.
