# Heat-diffusion equation boundary condition

**URL:** <https://discourse.julialang.org/t/heat-diffusion-equation-boundary-condition/100592>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 20, 2023, 7:10am UTC](https://discourse.julialang.org/t/heat-diffusion-equation-boundary-condition/100592 "2023-06-20T07:10:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reza\_AO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reza_ao/32/51137_2.png) [@Reza\_AO](https://discourse.julialang.org/u/Reza_AO)\
**Post date:** [June 20, 2023, 7:10am UTC](https://discourse.julialang.org/t/heat-diffusion-equation-boundary-condition/100592/1 "2023-06-20T07:10:55Z")

</div>

Hi  
I want to solve a 1 dimensional heat diffusion equation. At the right side of my domain I got the convective boundary condition;

> #Tamb, Kl,h are constants  
> `h * (T(t, Le) - Tamb) - kl * Dx(T(t, 0)) ~ 0`

When I solve my equation, the residual of this term almost remains constant (or reduced a little bit). Is there a problem with my implementation or definition?

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [June 20, 2023, 7:30am UTC](https://discourse.julialang.org/t/heat-diffusion-equation-boundary-condition/100592/2 "2023-06-20T07:30:31Z")

</div>

Can you provide a bit more information please?

- Are you solving the equation numerically or do you want to check an analytic solution? For the 1D heat equation there is almost certainly an analytic (at least asymptotic) expression for the true solution which you can use to compare.
- What packages are you using? Ideally, what code are you running exactly? Without knowing that, it is impossible to check if your implementation/definition has a problem.

---

<div class="post-metadata">

**Author:** ![Reza\_AO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reza_ao/32/51137_2.png) [@Reza\_AO](https://discourse.julialang.org/u/Reza_AO)\
**Post date:** [June 20, 2023, 11:04am UTC](https://discourse.julialang.org/t/heat-diffusion-equation-boundary-condition/100592/3 "2023-06-20T11:04:58Z")

</div>

I’m using NeuralPDE and Lux.  
my equation is:

```julia
eqs = [ρ*Cp*Dt(T(t,x)) ~ kl * Dxx(T(t, x)) - H*W*Dt(c(t,x)),
-Dt(c(t,x)) ~ A* c(t,x) * exp(-E/(Rc*T(t,x)))
    ]

```

my boundary condition is :

```julia
bcs = [ T(0,x) ~ T0,
        c(0,x) ~ c0,
        Dx(T(t,0)) ~ 0,
        h * (T(t, Le) - Tamb) - kl * Dx(T(t, 0)) ~ 0

```

I got 2 networks consisting of 6\*20 hidden layers, 2 inputs(t,x) and 1 output for each network (T and c).  
my training algorithm is QuadratureTraining.  
everything else is as the same as the NeuralPDE examples.

---

<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:** [June 20, 2023, 12:44pm UTC](https://discourse.julialang.org/t/heat-diffusion-equation-boundary-condition/100592/4 "2023-06-20T12:44:21Z")

</div>

This is still very terse. What are you solving, what is your code, and what’s going wrong?

---

<div class="post-metadata">

**Author:** ![Reza\_AO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reza_ao/32/51137_2.png) [@Reza\_AO](https://discourse.julialang.org/u/Reza_AO)\
**Post date:** [June 20, 2023, 3:14pm UTC](https://discourse.julialang.org/t/heat-diffusion-equation-boundary-condition/100592/5 "2023-06-20T15:14:45Z")

</div>

I want to couple solve an ODE and a PDE in a system in which the heat-diffusion equation is the PDE and the ODE is for concentration (c). My inputs are t, x and my outputs are T and c. My problem is that the loss for my convective boundary loss doesn’t decrease and if I give a learning rate higher than 1e-4, I get NaNs. I don’t know what my problem is. Thanks. Below is My full code for this problem.

```julia
using NeuralPDE, Lux, ModelingToolkit, Optimization, OptimizationOptimJL
import ModelingToolkit: Interval, infimum, supremum

@parameters t, x
@variables T(..) , c(..)
Dt = Differential(t)
#Dyy = Differential(y)^2
Dx = Differential(x)
#Dy = Differential(y)
Dxx = Differential(x)^2
########################
Le = 0.013 # m
He = 0.065 # m
kl = 0.7 # Thermal conductivity in radial direction (W/mk)
T0 = 273.15 + 25 # Initial Temperature (C)
ρ = 2231.2 # Cell density (kg/m^3)
Cp = 1100 # Heat capacity (J/kgK)
h = 12 # Heat transfer coefficient (W/m^2K)
Rc = 8.314 # Gas constant (J/molK)
E = 2.7e5 # Activation energy value of electrolyte [J/mol]
A = 5.14e25 # Reaction factor of electrolyte [1/s]
m = 1 # Reaction order of electrolyte
c0 = 1 # Initial dimensionless concentration
t_max = 133 * 60 # solution time
H = 6.2e5
W = 335
Tamb = 420
####################################################
eqs = [ρ*Cp*Dt(T(t,x)) ~ kl * Dxx(T(t, x)) - H*W*Dt(c(t,x)),
-Dt(c(t,x)) ~ A* c(t,x) * exp(-E/(Rc*T(t,x)))
    ]

bcs = [ T(0,x) ~ T0,
        c(0,x) ~ c0,
        Dx(T(t,0)) ~ 0,
        h * (T(t, Le) - Tamb) - kl * Dx(T(t, 0)) ~ 0
    ]
# Space and time domains
domains = [t ∈ Interval(0, t_max),
    x ∈ Interval(0, Le),]

# Neural network
input_ = length(domains)
n = 20
chain = [Lux.Chain(Dense(input_, n, Lux.relu), Dense(n, n, Lux.relu), Dense(n, n, Lux.relu), Dense(n, n, Lux.relu), Dense(n, n, Lux.relu), Dense(n, n, Lux.relu), Dense(n, n, Lux.relu), Dense(n, 1)) for _ in 1:2]
@named pdesystem = PDESystem(eqs, bcs, domains, [t,x], [T(t,x),c(t,x)])
strategy = NeuralPDE.QuadratureTraining()
discretization = PhysicsInformedNN(chain, strategy)
sym_prob = NeuralPDE.symbolic_discretize(pdesystem, discretization)

pde_loss_functions = sym_prob.loss_functions.pde_loss_functions
bc_loss_functions = sym_prob.loss_functions.bc_loss_functions

callback = function (p, l)
    println("loss: ", l)
    println("pde_losses: ", map(l_ -> l_(p), pde_loss_functions))
    println("bcs_losses: ", map(l_ -> l_(p), bc_loss_functions))
    return false
end

loss_functions = [pde_loss_functions; bc_loss_functions]

function loss_function(θ, p)
    sum(map(l -> l(θ), loss_functions))
end

f_ = OptimizationFunction(loss_function, Optimization.AutoZygote())
prob = Optimization.OptimizationProblem(f_, sym_prob.flat_init_params)
using Optimization
using OptimizationOptimisers
res = Optimization.solve(prob, Adam(0.0001); callback = callback, maxiters = 100000)

```

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [June 20, 2023, 6:19pm UTC](https://discourse.julialang.org/t/heat-diffusion-equation-boundary-condition/100592/6 "2023-06-20T18:19:43Z")

</div>

Is there a typo in the bcs?

```julia
Dx(T(t,0)) ~ 0
h * (T(t, Le) - Tamb) - kl * Dx(T(t, 0)) ~ 0

```

The second one contains both `T(t, Le)` and `T(t, 0)`, which seems weird, considering that you don’t seem to have/want a periodic domain.

Also, since you impose a no-flux b.c. at the left boundary, the second b.c. effectively reads

```julia
h * (T(t, Le) - Tamb) ~ 0

```

Or just `T(t, Le) ~ Tamb`. Is this what you want?

* * *

PS: I’m was also a bit surprised that these boundary conditions specify the whole solution, but I guess `c(t,x)` is specified through the equations + b.c. for `T(t,x)`. At least solving it with finite differences seems to work

```julia
discretizationMOL = MOLFiniteDifference([x => 0.0005], t)
symMOL = MethodOfLines.discretize(pdesystem, discretizationMOL)
sol = solve(symMOL, Tsit5())

```

---

<div class="post-metadata">

**Author:** ![Reza\_AO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reza_ao/32/51137_2.png) [@Reza\_AO](https://discourse.julialang.org/u/Reza_AO)\
**Post date:** [June 21, 2023, 8:33am UTC](https://discourse.julialang.org/t/heat-diffusion-equation-boundary-condition/100592/7 "2023-06-21T08:33:21Z")

</div>

> [@Sevi](#):
>
> Is there a typo in the bcs?

Thanks you are right about this. I am going to check it.

---

<div class="post-metadata">

**Author:** ![Reza\_AO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reza_ao/32/51137_2.png) [@Reza\_AO](https://discourse.julialang.org/u/Reza_AO)\
**Post date:** [June 29, 2023, 8:48am UTC](https://discourse.julialang.org/t/heat-diffusion-equation-boundary-condition/100592/8 "2023-06-29T08:48:39Z")

</div>

> h \* (T(t, Le) - Tamb) - kl \* Dx(T(t, Le)) ~ 0

My boundary is the conduction-convection balance on the right boundary. I can’t minimize it’s loss.  
I don’t understand where the problem is.  
I am assuming that this kind of boundary condition may take so many times to minimize it’s loss. but my problem isn’t that complicated.
