# Coupled PINN

**URL:** <https://discourse.julialang.org/t/coupled-pinn/95521>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 4, 2023, 12:58am UTC](https://discourse.julialang.org/t/coupled-pinn/95521 "2023-03-04T00:58:48Z")\
**Posts on this page:** 20\
**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:** [March 4, 2023, 12:58am UTC](https://discourse.julialang.org/t/coupled-pinn/95521/1 "2023-03-04T00:58:48Z")

</div>

Hi. I wanted to solve two different heat diffusion equations(coupled) and solve them. For this I need 2 seperate domains for equations and I want to solve them in parallel in 2 independent Neural Networks. How can I solve this problem? especially since there is an Interface condition here.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 4, 2023, 1:44am UTC](https://discourse.julialang.org/t/coupled-pinn/95521/2 "2023-03-04T01:44:23Z")

</div>

why do you want to use PINNs to solve this?

---

<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:** [March 4, 2023, 3:13am UTC](https://discourse.julialang.org/t/coupled-pinn/95521/3 "2023-03-04T03:13:35Z")

</div>

> [@Reza\_AO](#):
>
> Hi. I wanted to solve two different heat diffusion equations(coupled) and solve them. For this I need 2 seperate domains for equations and I want to solve them in parallel in 2 independent Neural Networks. How can I solve this problem? especially since there is an Interface condition here.

Just define two OptimizationProblems with NeuralPDE and then add an `additional_loss` term for the boundary handling. It is rather straightforward really from the two parts if you give it a go. What did you try?

---

<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:** [March 4, 2023, 2:42pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/4 "2023-03-04T14:42:35Z")

</div>

It’s for my Master’s degree thesis.

---

<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:** [March 4, 2023, 2:57pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/5 "2023-03-04T14:57:32Z")

</div>

I have tried it. But there are two Optimization problems which must be run in parallel. I don’t know how to run them in parallel. I have seen the examples on NeuralPDE, but none of them were solved in parallel. One thing that I was trying to do but I failed was that I made two different PDESystems as inner systems and wanted to connect them by another PDESystem like what is done by ODE in Lorenz Equations. Unfortunately, it doesn’t work the same (Expectedly) but I don’t understand why it’s made possible to define inner systems in PDESystems while they can’t be connected.  
Many Thanks

---

<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:** [March 4, 2023, 3:00pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/6 "2023-03-04T15:00:33Z")

</div>

> [@Reza\_AO](#):
>
> I have tried it. But there are two Optimization problems which must be run in parallel. I don’t know how to run them in parallel.

Define a new OptimizationProblem which calls both of the cost functions.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 4, 2023, 3:29pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/7 "2023-03-04T15:29:19Z")

</div>

> [@Reza\_AO](#):
>
> It’s for my Master’s degree thesis.

You realize that a PINN is going to be an absurdly inefficient way to solve a diffusion equation, right? Standard PDE solvers are really good at this, since people have spent decades optimizing solution methods for Poisson equations…

---

<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:** [March 4, 2023, 7:11pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/8 "2023-03-04T19:11:21Z")

</div>

Unfortunately, my supervisor insists on solving it by PINN and I can’t argue with him. There are more efficient and less time consuming methods ofcourse but it’s already late for the change of direction.

---

<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:** [March 4, 2023, 11:05pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/9 "2023-03-04T23:05:35Z")

</div>

Thanks. The problem I’m having right now is that I want them to be run in parallel in res line;

```julia
res1 = Optimization.solve(prob1 ,Adam(0.001); callback = callback, maxiters=1000)
res2 = Optimization.solve(prob2 ,Adam(0.001); callback = callback, maxiters=1000)

```

Since there are two problems which should be solved in parallel, I don’t want them to run separately. Do I need Parallel computing to run them together?  
For defining a new OptimizationProblem I don’t have any clue what to do.

---

<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:** [March 5, 2023, 5:50am UTC](https://discourse.julialang.org/t/coupled-pinn/95521/10 "2023-03-05T05:50:40Z")

</div>

> [@Reza\_AO](#):
>
> For defining a new OptimizationProblem I don’t have any clue what to do.

`newf(x,p) = prob1.f(x,p) + prob2.f(x,p)`?

---

<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:** [March 5, 2023, 6:50pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/12 "2023-03-05T18:50:56Z")

</div>

> [@Reza\_AO](#):
>
> ```julia
> @named pde_system1 = PDESystem(eqs = eq1,bcs = bc1,domain = domain1,ivs=[t,x],dvs = T1(t,x))
> 
> ```

You can’t just make up syntax 😅 . See the documentation.

> [@Reza\_AO](#):
>
> ```julia
> res = Optimization.solve(prob?? ,Adam(0.001); callback = callback, maxiters=1000)
> 
> ```

Define the third optimization problem using the first two…

---

<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:** [March 5, 2023, 8:31pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/13 "2023-03-05T20:31:00Z")

</div>

> [@ChrisRackauckas](#):
>
> You can’t just make up syntax 😅 . See the documentation

> [@ChrisRackauckas](#):
>
> You can’t just make up syntax 😅 . See the documentation

What is made up? 😅. The PDESystem Syntax is like this. I have seen it on Modelingtoolkit.jl.

> [@ChrisRackauckas](#):
>
> Define the third optimization problem using the first two…

At the same time, I know what U are saying and I don’t. How can I make a new prob using the first two when I don’t have any other relationship to define a new problem?  
Thanks for your time.

---

<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:** [March 5, 2023, 9:42pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/14 "2023-03-05T21:42:19Z")

</div>

The only solution I have on my mind is that when I define problems, I define a new loss function which calls both problems loss functions then make a new problem. Do U know if it is possible to do so with symbolic\_discretize or not?

---

<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:** [March 5, 2023, 11:23pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/15 "2023-03-05T23:23:14Z")

</div>

> [@Reza\_AO](#):
>
> Do U know if it is possible to do so with symbolic\_discretize or not?

Not right now.

> [@Reza\_AO](#):
>
> What is made up? 😅. The PDESystem Syntax is like this. I have seen it on Modelingtoolkit.jl.

No, those values are not keyword arguments. They are positional in all of the docs.

> [@Reza\_AO](#):
>
> At the same time, I know what U are saying and I don’t. How can I make a new prob using the first two when I don’t have any other relationship to define a new problem?

You know the relationship because you just built it.

> [@Reza\_AO](#):
>
> The only solution I have on my mind is that when I define problems, I define a new loss function which calls both problems loss functions then make a new problem.

That’s what I am describing.

> [@Reza\_AO](#):
>
> Do U know if it is possible to do so with symbolic\_discretize or not?

No, just do it as I showed.

---

<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:** [March 6, 2023, 10:16am UTC](https://discourse.julialang.org/t/coupled-pinn/95521/16 "2023-03-06T10:16:26Z")

</div>

Thanks. ❤

---

<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:** [March 6, 2023, 10:55am UTC](https://discourse.julialang.org/t/coupled-pinn/95521/17 "2023-03-06T10:55:45Z")

</div>

> [@ChrisRackauckas](#):
>
> newf(x,p) = prob1.f(x,p) + prob2.f(x,p)

I apologize for asking so many questions. After defining the new loss function like what U mentioned  
for defining new optimization problem surely, I need to define u0 from the init parameters produced. Since there are two different init parameters, Is it logical for me to vcat them? Or should I do something else?

```julia
f_ = OptimizationFunction(newf, Optimization.AutoZygote())
prob = Optimization.OptimizationProblem(f_, u0??)

```

Thanks

---

<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:** [March 6, 2023, 11:24am UTC](https://discourse.julialang.org/t/coupled-pinn/95521/18 "2023-03-06T11:24:33Z")

</div>

> [@Reza\_AO](#):
>
> I need to define u0 from the init parameters produced. Since there are two different init parameters, Is it logical for me to vcat them?

yes, and then you slice them in `newf`.

---

<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:** [March 6, 2023, 12:21pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/19 "2023-03-06T12:21:34Z")

</div>

> [@ChrisRackauckas](#):
>
> yes, and then you slice them in `newf`.

> [@Reza\_AO](#):
>
> ```julia
> f_ = OptimizationFunction(newf, Optimization.AutoZygote())
> prob = Optimization.OptimizationProblem(f_, u0??)
> 
> ```

What is the purpose of slicing them in newf? Can’t I just give the problem f\_ and u0? newf is my new loss function which is the sum of the prob1 and prob2 loss functions.

---

<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:** [March 6, 2023, 12:34pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/20 "2023-03-06T12:34:09Z")

</div>

yes but you want to give different coefficients (`u0` parts) to `prob1.f` and `prob2.f`? That’s the reason for concatenating `u0`?

---

<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:** [March 6, 2023, 12:50pm UTC](https://discourse.julialang.org/t/coupled-pinn/95521/21 "2023-03-06T12:50:39Z")

</div>

```julia
sym_prob1 = NeuralPDE.symbolic_discretize(pde_system1, discretization1)
sym_prob2 = NeuralPDE.symbolic_discretize(pde_system2, discretization2)
#loss definition
pde_loss_functions1 = sym_prob1.loss_functions.pde_loss_functions
bc_loss_functions1 = sym_prob1.loss_functions.bc_loss_functions
aprox_derivative_loss_functions1 = sym_prob1.loss_functions.bc_loss_functions
pde_loss_functions2 = sym_prob2.loss_functions.pde_loss_functions
bc_loss_functions2 = sym_prob2.loss_functions.bc_loss_functions
aprox_derivative_loss_functions2 = sym_prob2.loss_functions.bc_loss_functions
##
loss_functions1 = [pde_loss_functions1;bc_loss_functions1;aprox_derivative_loss_functions1]
loss_functions2 = [pde_loss_functions2;bc_loss_functions2;aprox_derivative_loss_functions2]
#Loss Functions
function loss_function1(θ,p)
    sum(map(l->l(θ) ,loss_functions1))
end
function loss_function2(θ,p)
    sum(map(l->l(θ) ,loss_functions2))
end
#newf
newf(θ,p)=loss_function1(θ,p)+loss_function2(θ,p)
#Optimization
f_ = OptimizationFunction(newf, Optimization.AutoZygote())
u0 = vcat(sym_prob1.flat_init_params,sym_prob2.flat_init_params)
prob = Optimization.OptimizationProblem(f_, u0)

```

Am I doing something wrong here in the definition of newf? since U are saying that I should produce prob1.f and prob2.f but I defined their loss functions like that.  
thanks

[Next page](https://discourse.julialang.org/t/coupled-pinn/95521.md?page=2)
