# Diffusion coefficient between two PDEs

**URL:** <https://community.freefem.org/t/diffusion-coefficient-between-two-pdes/858>\
**Category:** General Discussion\
**Created:** [March 17, 2021, 3:27pm UTC](https://community.freefem.org/t/diffusion-coefficient-between-two-pdes/858 "2021-03-17T15:27:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nmmhuynh](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/nmmhuynh/32/461_2.png) [@nmmhuynh](https://community.freefem.org/u/nmmhuynh)\
**Post date:** [March 17, 2021, 3:27pm UTC](https://community.freefem.org/t/diffusion-coefficient-between-two-pdes/858/1 "2021-03-17T15:27:05Z")

</div>

Hi everyone!  
I am sequentially solving two PDEs, correlated by the diffusion term:  
 ![problem](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/db98ed7e74562622cf69abb2473f608a99a5f6f7.jpeg)

After solving the first PDE (thus getting u) with

> solve firstpde (u, v) = int3d (Th) ( A \* grad(u)’ \* grad(v) ) + BCs ) ;

where Th is the triangulation of the mesh Vh, I tried to define the diffusion coefficient B as

> macro grad(f) [dx(f), dy(f), dz(f)]  
> func B = int3d (Th) ( A \* grad( u ) ) )

but it gives me error on the definition of B.

Does anyone can help me?  
Thank you!

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [March 17, 2021, 4:35pm UTC](https://community.freefem.org/t/diffusion-coefficient-between-two-pdes/858/2 "2021-03-17T16:35:42Z")

</div>

Where is the depend in x in definition of B, it is in function u ?

---

<div class="post-metadata">

**Author:** ![nmmhuynh](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/nmmhuynh/32/461_2.png) [@nmmhuynh](https://community.freefem.org/u/nmmhuynh)\
**Post date:** [March 18, 2021, 8:54am UTC](https://community.freefem.org/t/diffusion-coefficient-between-two-pdes/858/3 "2021-03-18T08:54:33Z")

</div>

oh I’m sorry, I wrote it wrong.  
B is constant through the domain.

![new](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/1X/84af531feaf70abefcb62c885ebcce3a24561478.jpeg)

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [March 18, 2021, 9:11am UTC](https://community.freefem.org/t/diffusion-coefficient-between-two-pdes/858/4 "2021-03-18T09:11:13Z")

</div>

so no problem just do:  
real Bx = int2d(Th)(A_dx(u));  
real By = int2d(Th)(A_dy(u));

B is a vector because A is a scalar ,  
so the third equation have no meaning

---

<div class="post-metadata">

**Author:** ![nmmhuynh](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/nmmhuynh/32/461_2.png) [@nmmhuynh](https://community.freefem.org/u/nmmhuynh)\
**Post date:** [March 18, 2021, 10:11am UTC](https://community.freefem.org/t/diffusion-coefficient-between-two-pdes/858/5 "2021-03-18T10:11:43Z")

</div>

Ok, so B = [Bx, By], is that correct?  
How can I then solve the third equation? I would be a simple laplace problem with constant coefficient B.  
I tried to put Bx, By into a diagonal matrix

> matrix B = [[Bx, 0], [0, By] ];

and then solve the problem

> macro grad(func) [dx(func), dy(func), dz(func)] \  
> func rhs = 1;  
> solve secondPDE (w, v) = int2d(Th) ( B \* grad(w)’ \* grad(v) ) - int2d(Th) ( rhs \* v ) + BCs

but it gives me error on the last line.  
Is it related to the definition of the diffusion coefficient B as matrix? Should it be a constant?

Thank you very much for your help!

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [March 18, 2021, 10:34am UTC](https://community.freefem.org/t/diffusion-coefficient-between-two-pdes/858/6 "2021-03-18T10:34:51Z")

</div>

you have to write  
with in 2d the grad definition is

```
    macro grad(u) [dx(u),dy(u)]//
   ... int2d(Th) ( grad(w)’ *B* grad(v) ) ...
```

---

<div class="post-metadata">

**Author:** ![nmmhuynh](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/nmmhuynh/32/461_2.png) [@nmmhuynh](https://community.freefem.org/u/nmmhuynh)\
**Post date:** [March 18, 2021, 10:50am UTC](https://community.freefem.org/t/diffusion-coefficient-between-two-pdes/858/7 "2021-03-18T10:50:15Z")

</div>

Sorry, I have mixed 3D and 2D.  
However, it still doesn’t work.

> macro grad(u) [dx(u), dy(u)] //  
> matrix B = [[Bx, 0], [0, By] ];  
> func rhs = 1;  
> solve secondPDE (w, v) = int2d(Th) ( grad(w)’ \* B \* grad(v) ) …

It says

> Error in (last) line, before token \*  
> Compile error :  
> line number :176, \*

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [March 18, 2021, 12:28pm UTC](https://community.freefem.org/t/diffusion-coefficient-between-two-pdes/858/8 "2021-03-18T12:28:26Z")

</div>

the correct writing is in

[monica.edp](https://community.freefem.org/uploads/short-url/iN6OCl101D8ulDn2a7uZXa6L52M.edp) (310 Bytes)

---

<div class="post-metadata">

**Author:** ![nmmhuynh](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/nmmhuynh/32/461_2.png) [@nmmhuynh](https://community.freefem.org/u/nmmhuynh)\
**Post date:** [March 18, 2021, 1:10pm UTC](https://community.freefem.org/t/diffusion-coefficient-between-two-pdes/858/9 "2021-03-18T13:10:34Z")

</div>

Thank you very very much for your help!
