# How to solve 2D PDE for heat conduction with x and y having different length (A rectangle)

**URL:** <https://discourse.julialang.org/t/how-to-solve-2d-pde-for-heat-conduction-with-x-and-y-having-different-length-a-rectangle/103907>\
**Category:** Machine Learning\
**Tags:** question\
**Created:** [September 15, 2023, 5:13pm UTC](https://discourse.julialang.org/t/how-to-solve-2d-pde-for-heat-conduction-with-x-and-y-having-different-length-a-rectangle/103907 "2023-09-15T17:13:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShadmanSakief](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shadmansakief/32/51586_2.png) [@ShadmanSakief](https://discourse.julialang.org/u/ShadmanSakief)\
**Post date:** [September 15, 2023, 5:13pm UTC](https://discourse.julialang.org/t/how-to-solve-2d-pde-for-heat-conduction-with-x-and-y-having-different-length-a-rectangle/103907/1 "2023-09-15T17:13:23Z")

</div>

using NeuralPDE, Lux, Optimization, OptimizationOptimJL  
import ModelingToolkit: Interval

@parameters x y  
@variables u(..)  
Dxx = Differential(x)^2  
Dyy = Differential(y)^2  
Dx = Differential(x)  
Dy = Differential(y)

# 2D PDE

eq = Dxx(u(x, y)) + Dyy(u(x, y)) ~ -(1000/238)

# Boundary conditions

bcs = [Dx(u(0, y)) ~ 0.0, Dx(u(1, y)) ~ 0.0,  
u(x, 0) ~ 350, Dy(u(x, 1)) ~ (50/238)\*(298-u(x, 1)) ]

# Space and time domains

domains = [x ∈ Interval(0.0, 1.0),  
y ∈ Interval(0.0, 1.0)]

# Neural network

dim = 2 # number of dimensions  
chain = Lux.Chain(Dense(dim, 16, Lux.σ), Dense(16, 16, Lux.σ), Dense(16, 1))

# Discretization

dx = 0.05  
discretization = PhysicsInformedNN(chain, GridTraining(dx))

@named pde\_system = PDESystem(eq, bcs, domains, [x, y], [u(x, y)])  
prob = discretize(pde\_system, discretization)

#Optimizer  
opt = OptimizationOptimJL.BFGS()

#Callback function  
callback = function (p, l)  
println(“Current loss is: $l”)  
return false  
end

res = Optimization.solve(prob, opt, callback = callback, maxiters = 1000)  
phi = discretization.phi

using Plots

xs, ys = [infimum(d.domain):(dx / 10):supremum(d.domain) for d in domains]

u\_predict = reshape([first(phi([x, y], res.u)) for x in xs for y in ys],  
(length(xs), length(ys)))  
p1 = plot(xs, ys, u\_predict, linetype = :contourf, title = “predict”);  
plot(p1)

I wrote this code for 2D heat conduction in square shape. It runs properly and gives contour plot. But the problem is: when I want to makde it rectangular shape having x=2, y=1. the code results blank plot. There shows no contour. Just shows colour range with values. How can I do this

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 15, 2023, 5:17pm UTC](https://discourse.julialang.org/t/how-to-solve-2d-pde-for-heat-conduction-with-x-and-y-having-different-length-a-rectangle/103907/2 "2023-09-15T17:17:05Z")

</div>

Welcome! Your post is a little hard to read — could you take some time to edit and format it such that it leads with the core question and the code itself is formatted? You can use triple-backticks to encapsulate a section of code like so:

````julia
```
# code block goes here
```

````

Thanks! Doing that will help those who may be able to answer your question read and understand it more clearly.
