# A FreeFEM++ code uses the Modified Weak Galerkin Finite Element Method to solve the Poisson Problem

**URL:** https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724
**Category:** General Discussion
**Created:** [September 27, 2023, 8:11am UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724 "2023-09-27T08:11:48Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![ahbabil1983](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/ahbabil1983/32/1860_2.png) [@ahbabil1983](https://community.freefem.org/u/ahbabil1983)
#### Post date: [September 27, 2023, 8:11am UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/1 "2023-09-27T08:11:48Z")

</div>

Hello everyone,  
I wrote a FreeFEM++ code to solve the Poisson Problem -∆u=f in Ω u=g on ∂Ω using the Modified Weak Galerkin Finite Element Method based on the paper:

[1] “A modified weak Galerkin finite element method”. X. Wang, N.S. Malluwawadu, F. Gao, T.C. McMillan.

[https://www.sciencedirect.com/science/article/pii/S0377042714002131](https://www.sciencedirect.com/science/article/pii/S0377042714002131) 3.

I modified a FreeFEM++ code for the Discontinuous Galerkin FEM to solve this problem to fit the Modified Weak Galerkin FEM, but the results are not the same as in the paper. I am wondering what’s wrong with my code.  
[MWGFEM.edp](https://community.freefem.org/uploads/short-url/8iYTlggIdgbCxh7L0thMZgc6DgF.edp) (1.4 KB)

---

<div class="post-metadata">

### Author: ![orange](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orange](https://community.freefem.org/u/orange)
#### Post date: [February 13, 2025, 8:00am UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/2 "2025-02-13T08:00:17Z")

</div>

hello!! Can your code run with a crrect results now? I have the same problem with the MWG method on this paper \>.\<

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [February 14, 2025, 3:56pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/4 "2025-02-14T15:56:00Z")

</div>

The weak gradient was not defined correctly in this code (it must not be the usual gradient). Here is a code with the weak gradient  
[mwgfem.edp](https://community.freefem.org/uploads/short-url/utaseF3AkQLMDgT03soIq2RGxtt.edp) (1.4 KB)  
However still it does not give the numerical values listed in the paper (Table 1). The results in Table 1 are doubtful because the error cannot be so small (even the L2 projection of uex gives an error to uex larger that stated!).

---

<div class="post-metadata">

### Author: ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)
#### Post date: [February 15, 2025, 8:27am UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/5 "2025-02-15T08:27:25Z")

</div>

Respected sir, I have not so much idea about weak gradient but results coming if take a penaltity parameter 1000 in the intalledges term of the formulation using P1 element. I don’t know why it is happening. But i have not found any thing about penaltity parameter in the paper.  
Here, is the Code\>  
[My Code\_wgm.edp](https://community.freefem.org/uploads/short-url/4482yvuznfCoIvNNodIdaTfM9BF.edp) (2.2 KB)

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [February 15, 2025, 11:12am UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/6 "2025-02-15T11:12:40Z")

</div>

The weak gradient is defined in the paper mentioned above.  
If we put a large penalty parameter I agree that the method becomes second order, indeed it approaches the conforming P1 framework, which is known.  
Here the paper claims that with P1 discontinuous it is better than classical, with the penalty parameter 1/h. We don’t get it.

---

<div class="post-metadata">

### Author: ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)
#### Post date: [February 15, 2025, 11:25am UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/7 "2025-02-15T11:25:26Z")

</div>

Yes Sir. I have also observed that. Thank you.

---

<div class="post-metadata">

### Author: ![orange](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orange](https://community.freefem.org/u/orange)
#### Post date: [February 21, 2025, 2:45pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/8 "2025-02-21T14:45:26Z")

</div>

Respected sir, I wrote a freefem code with the weak gradient, and the error can be small enough like the paper mentioned.  
[MWG.edp](https://community.freefem.org/uploads/short-url/30XAQaECvcoB8MwsBFtKlDWNUq5.edp) (2.4 KB)  
But When I try to change the exact solution’s boundary condition  
u=0  
to  
u = u\_{ex}, \partial\_n u = (u\_x, u\_y)\cdot n ,  
it doesn’t work. I don’t know why. TT  
[MWG\_2.edp](https://community.freefem.org/uploads/short-url/fWnEi2Dvh85IYNOskiw6c54muZq.edp) (2.6 KB)

---

<div class="post-metadata">

### Author: ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)
#### Post date: [February 22, 2025, 7:43am UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/9 "2025-02-22T07:43:52Z")

</div>

Hello Dear orange, your code is giving very good results. **But which formulation you used in the code??. Why you are defining two spaces??.**  
Do you used the formulation in the following:

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/7/711fadb1cc5077a4dc04d63696e0b8af19de340c.png)  
(equation: 3)  
**I have not understand your problem formulation:** you have not taken (dxx(sigma1)+dyy(sigma2)) term still you have used others.

> Blockquote  
> problem WGM([sigma1, sigma2, uh], [tau1, tau2, vh], solver=UMFPACK)  
> = -int2d(Th)( vh\*(dx(sigma1)+dy(sigma2)) )  
> +intalledges(Th)( (1-nTonEdge)_( mean(vh)_(jump(sigma1)_N.x + jump(sigma2)N.y) )/nTonEdge )  
> +intalledges(Th)((jump(uh)jump(vh))/lenEdge)  
> +int2d(Th)(sigma1tau1 + sigma2tau2)  
> +int2d(Th)( uh_(dx(tau1)+dy(tau2)) )  
> -intalledges(Th)( (1-nTonEdge)_( mean(uh)_(jump(tau1)_N.x + jump(tau2)N.y) )/nTonEdge )  
> //+int2d(Th)(ghvh)  
> -int2d(Th)(f_vh)// linear form  
> +on(1,2,3,4,uh=0)  
> Blockquote

Does you taken the formulation of the following paper or anything else:  
[J Comp and app math 2014.pdf](https://community.freefem.org/uploads/short-url/7OyjKAAa0FpEyGyviS8plvmtgcu.pdf) (377.6 KB)

Specifically, why you taken the following term:

> Blockquote  
> +int2d(Th)(sigma1_tau1 + sigma2_tau2)  
> +int2d(Th)( uh\*(dx(tau1)+dy(tau2)) )  
> -intalledges(Th)( (1-nTonEdge)_( mean(uh)_(jump(tau1)\*N.x + jump(tau2)_N.y) )/nTonEdge )  
> //+int2d(Th)(gh_vh)  
> Blockquote

I have not found like any term in the above paper. **Can you share your formulation pls??**

Also, I have observed :

> Blockquote  
> fespace Vh(Th,P2dc); // Discontinous P2 finite element  
> Vh uh,vh;

> Blockquote  
> fespace Wh(Th,P1dc);  
> Wh sigma1,sigma2;  
> Wh tau1, tau2;

**This two spaces Vh and Wh should be such that Wh has one less polynomial degree**. Why you taken like this ??.  
I need your clarification brother.  
In the paper still they define with same degree\>

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/0/009e0e4f176be377abd5024cd0cc6fa8343857e0.png)

**Regarding boundary condition: +on(1,2,3,4,uh=0) . You have to take it as you used uex=sin(pi\*x)_sin(pi_y) which is zero on boundary of the domain D=[0,1]^2.**

I have checked your code with P1, P2, and P3. All gives good results.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [February 24, 2025, 3:38pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/10 "2025-02-24T15:38:03Z")

</div>

Indeed my code works also if I replace `+on(1,2,3,4,uh=0)` by `+int1d(Th)(1.e10*uh*vh)`. It seems that there is a bug in the implementation of `on()` on P1dc.

---

<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: [February 24, 2025, 3:46pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/11 "2025-02-24T15:46:42Z")

</div>

It is not a bug it is due to the logical support of the degree of freedom ( interior of the triangle not on boundary for discontinuity),.

---

<div class="post-metadata">

### Author: ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)
#### Post date: [February 25, 2025, 12:44pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/12 "2025-02-25T12:44:09Z")

</div>

Respected sir, Does the **problem formulation in the following code** is fine?. I am not getting the problem formulation in codes.  
[MWG.edp](https://community.freefem.org/uploads/short-url/30XAQaECvcoB8MwsBFtKlDWNUq5.edp) (2.4 KB)

If i understand well, i have to solve the following equation:

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/7/711fadb1cc5077a4dc04d63696e0b8af19de340c.png)(eqn. 3)  
Does the code following it sir?.

Can please you explain it sir??. It will help to implement it on my works.

Thanks in advance sir.

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [February 25, 2025, 1:25pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/13 "2025-02-25T13:25:04Z")

</div>

The code is correct.  
Apply Definition 1.1 to v\in V\_h^0, taking q=\nabla\_w u gives  
(\nabla\_w v,\nabla\_w u)\_T=-(v,\nabla\cdot(\nabla\_w u))\_T+(\{v\},\nabla\_w u\cdot n)\_{\partial T}  
Replacing this in eq. (3) gives the variational formulation  
-\sum\_T\int\_Tv\nabla\cdot(\nabla\_w u) +\sum\_T\int\_{\partial T}\{v\}(\nabla\_w u)\cdot n +\sum\_e h^{-1}\int\_e [[u]][[v]]-\int\_\Omega fv=0   
for all v\in V\_h^0.  
You write then Definition 1.1 for u, and call \nabla\_wu=(\sigma\_1,\sigma\_2). Renaming the test function q as q=(\tau\_1,\tau\_2) you finally get the variational formulation of the code, with test functions v,\tau\_1,\tau\_2.  
You may prefer this version  
[mwgfem.edp](https://community.freefem.org/uploads/short-url/nWrA81gyPJOxwW1HwFsQdis0BRn.edp) (1.5 KB)  
which is exactly the above formulas ((\sigma\_1,\sigma\_2) is replaced by (Gx,Gy)).

---

<div class="post-metadata">

### Author: ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)
#### Post date: [February 25, 2025, 1:41pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/14 "2025-02-25T13:41:17Z")

</div>

Thanks for quick response sir. So, **can i apply this code ideas in any time dependent problem in modified weak Galerkin method and weak galerkin method sir??.**

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [February 25, 2025, 1:44pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/15 "2025-02-25T13:44:27Z")

</div>

A priori yes (but the proof of stability, convergence and error estimate may be missing).

---

<div class="post-metadata">

### Author: ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)
#### Post date: [February 25, 2025, 1:47pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/16 "2025-02-25T13:47:41Z")

</div>

So, why the proof of stability, convergence and error estimate may be missing sir??. Numerically, we are getting optimal results. So, theoritically should come.

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [February 25, 2025, 1:49pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/17 "2025-02-25T13:49:23Z")

</div>

It should, but as long as no proof is published, you are not sure.

---

<div class="post-metadata">

### Author: ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)
#### Post date: [February 25, 2025, 2:29pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/19 "2025-02-25T14:29:09Z")

</div>

As i am understand from your ideas sir,  
i am getting two set of equations , but i am not getting from where the term **- int2d(Th)(Gx_qx+Gy_qy)** comes. Can tell sir??. If i am understand well, we do not add after getting two set of equations otherwise the terms + intalledges(Th)( jump(uh)\*jump(vh)/h/nTonEdge ) and

- int2d(Th)(f\*vh) will comes twice. Can you tell me please sir??.

---

<div class="post-metadata">

### Author: ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)
#### Post date: [February 25, 2025, 2:52pm UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/21 "2025-02-25T14:52:12Z")

</div>

**My understanding: (q, (Gx,Gy)) giving the term - int2d(Th)(Gx_qx+Gy_qy). Am i right sir??. But this should be with “+” sign why “-” is in formulation sir??. (\tau\_1=qx, \tau\_2=qy in the code )  
Also, in this way i think we are adding many extra terms in formulation sir.**

---

<div class="post-metadata">

### Author: ![orange](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orange](https://community.freefem.org/u/orange)
#### Post date: [April 15, 2025, 3:32am UTC](https://community.freefem.org/t/a-freefem-code-uses-the-modified-weak-galerkin-finite-element-method-to-solve-the-poisson-problem/2724/22 "2025-04-15T03:32:27Z")

</div>

I’m sorry for too late to reply.  
In that artical, you can see  
(\nabla\_w u, q)\_T = -(u, \nabla\cdot q)\_T + \langle\{u\}, q\rangle\_{\partial T}.  
So, for (\nabla\_w u, \nabla\_w v)\_T, let \nabla\_w u=\sigma, and we can get:  
(\nabla\_w u, \nabla\_w v)\_T=(\sigma, \nabla\_w v)\_T= -(v, \nabla\cdot \sigma)\_T + \langle\{v\}, \sigma\rangle\_{\partial T}.  
We can also get that \sigma satisfies following equation: for any \tau  
(\sigma, \tau)\_T = (\nabla\_w u, \tau)\_T= -(u, \nabla\cdot \tau)\_T + \langle\{u\}, \tau\rangle\_{\partial T}.

Now we have following equations:  
(\nabla\_w u, \nabla\_w v)+s(u,v)=(f,v),  
(\sigma, \nabla\_w v)= -(v, \nabla\cdot \sigma) + \langle\{v\}, \sigma\rangle\_{\partial T\_h},  
(\sigma, \tau)= -(u, \nabla\cdot \tau)\_T + \langle\{u\}, \tau\rangle\_{\partial T\_h}.

Combine the equations above, we have:  
-(v, \nabla\cdot \sigma) + \langle\{v\}, \sigma\rangle\_{\partial T\_h}+s(u,v) +(\sigma, \tau) + (u, \nabla\cdot \tau)\_T - \langle\{u\}, \tau\rangle\_{\partial T\_h} -(f,v)=0.
