CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

least square second order accuracy implementation Matlab

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree3Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   February 9, 2020, 10:42
Default least square second order accuracy implementation Matlab
  #1
New Member
 
Join Date: Feb 2019
Posts: 7
Rep Power: 7
amine.hanini is on a distinguished road
Hello,

I am trying to implement in 2D with matlab the least square method to calculate the gradient of my variables to reach a second order accuracy in space. Together with the minmod limiter to avoid oscillations and the appearance of new extrema. My problem here is that I have the feeling that my second order is not really second order and the results is very similar to the first order .
I appreciate any help.
amine.hanini is offline   Reply With Quote

Old   February 9, 2020, 12:07
Default
  #2
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,152
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
Well, the LSQ method is actually only first order for general unstructured grids, and not even always if it doesn't employ any weighting. But on fully structured and cartesian grids you should achieve 2nd order indeed.

The limiter has nothing to do with the gradient accuracy.
sbaffini is offline   Reply With Quote

Old   February 10, 2020, 04:41
Default
  #3
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,152
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
If you give more details on what you're doing and how are you checking your accuracy, maybe we can give more help
sbaffini is offline   Reply With Quote

Old   February 10, 2020, 07:18
Default
  #4
New Member
 
Join Date: Feb 2019
Posts: 7
Rep Power: 7
amine.hanini is on a distinguished road
@Sbaffini, Thank you for your response.

Concerning the type of LSQ method, I am using the weighted averaging procedure to compute the values on the vertices of each triangular cell, this computation needs the information of the surrounding cells with common vertex. After the cell node variables are computed, the gradient on control volume can be calculated via some arithmetic average on each cell face. I am supposed to be more accurate with this node-based reconstruction and this bigger stencil than the cell-based one, but it appears to that there is no change between 1st and 2nd order and when I plot the two results, It's exactly the same.

P.S. To limit gradient on each control volume, I use the Barth-Jespersen slope limiter.
amine.hanini is offline   Reply With Quote

Old   February 10, 2020, 07:38
Default
  #5
New Member
 
Join Date: Feb 2019
Posts: 7
Rep Power: 7
amine.hanini is on a distinguished road
untitled.png
LSQ1.jpg
LSQ2.jpg

In these attachement the method the reconstruction used in my matlab code and the results of a simple dam-break and a superposed water height level with the two orders.
amine.hanini is offline   Reply With Quote

Old   February 10, 2020, 07:40
Default
  #6
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,152
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
Quote:
Originally Posted by amine.hanini View Post
@Sbaffini, Thank you for your response.

Concerning the type of LSQ method, I am using the weighted averaging procedure to compute the values on the vertices of each triangular cell, this computation needs the information of the surrounding cells with common vertex. After the cell node variables are computed, the gradient on control volume can be calculated via some arithmetic average on each cell face. I am supposed to be more accurate with this node-based reconstruction and this bigger stencil than the cell-based one, but it appears to that there is no change between 1st and 2nd order and when I plot the two results, It's exactly the same.

P.S. To limit gradient on each control volume, I use the Barth-Jespersen slope limiter.
So, you are using a Green-Gauss gradient with face values computed as "some average" of node values, which in turn are computed from a WLSQ
interpolation? A variant of this scheme is also available in Fluent as node-based green-gauss gradient.

I have no experience with this approach but, I expect "some average" to actually have a large influence on the final result.

Moreover, on general unstructured grids, you need higher than 2nd order accuracy on face values to actually hope to get higher than 1st order accuracy on gradients. You can see this by noting that the final green-gauss formula is a sum of face values multiplied by area and divided by volume, so if your face values are like

phi_f = phi_f_exact + O(dx^2)

the final gradient will be something like (not considering other error sources):

grad_phi = grad_phi_exact + O(Area*dx^2/Vol) = grad_phi_exact + O(dx)

If your grid has some regularity, it might happen that O(dx^2) terms on opposite faces would cancel each other, but this doesn't happen in general.

Gradient tests should always be done on a set of grids:

1) Uniform cartesian
2) Non-uniform cartesian
3) Prismatic (split the uniform cartesian cells along the diagonal of one plane only)
4) Unstructured (both regular and not, if possible)

and on a set of functions:

1) linear in 1D
2) linear in multi dimensions
3) mixed linear-higher order (e.g., x*y^2)
4) polynomial in multi-dimensions (e.g., x^2+y^2 + (x*y)^2)
5) full non linear ones (sin, cos, exp, etc.)

But in no case you should include the limiter in this exercise.
sbaffini is offline   Reply With Quote

Old   February 10, 2020, 07:47
Default
  #7
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,152
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
Quote:
Originally Posted by amine.hanini View Post
Attachment 74724
Attachment 74725
Attachment 74726

In these attachement the method the reconstruction used in my matlab code and the results of a simple dam-break and a superposed water height level with the two orders.
I just posted few seconds after you posted your last message. Few things:

1) Yes, your gradient method is indeed the node-based green-gauss one. Also note that it is mentioned as more robust and not more accurate.

2) You seem to be comparing the two methods on the final full solution (of a dam break problem!!!), which explains why you keep mentioning the limiter. While this might make sense, it has not if you limit the discussion to the gradient. It is highly probable that in your case the gradient has no such a great difference.

3) Are you using a specific code of someone else or something coded by you? In the last case, I suggest to test the gradient alone as mentioned in my last post (if this is what you actually want to test).
sbaffini is offline   Reply With Quote

Old   February 10, 2020, 08:15
Default
  #8
New Member
 
Join Date: Feb 2019
Posts: 7
Rep Power: 7
amine.hanini is on a distinguished road
Quote:
Originally Posted by sbaffini View Post

3) Are you using a specific code of someone else or something coded by you? In the last case, I suggest to test the gradient alone as mentioned in my last post (if this is what you actually want to test).
I am using my own matlab code. It's a 2D unstructured triangular code.

Yes, It will be very interesting if I could test this reconstruction on a simple test cases, but I can't follow your steps in your last post; in fact uniform cartesian, non-uniform one and prismatic can not be reached by my code, 1D either.

I can test my reconstruction method using no regular triangular grid.

[QUOTE]

1) linear in 1D
2) linear in multi dimensions
3) mixed linear-higher order (e.g., x*y^2)
4) polynomial in multi-dimensions (e.g., x^2+y^2 + (x*y)^2)
5) full non linear ones (sin, cos, exp, etc.)

[QUOTE]

1) not possible with my code

If I understand well, I have to change my system of equation by a simple linear one, first, like transport equation ?
amine.hanini is offline   Reply With Quote

Old   February 10, 2020, 08:31
Default
  #9
Senior Member
 
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 6,764
Rep Power: 71
FMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura about
From a general theoretical point of view, a linear reconstruction of the function can never produce a second order accurate gradient. The only case that happens is for a uniform grid where you have some terms in the local truncation error disappearing.
Thus, the question is what do you want more by doing a LSR?
amine.hanini likes this.
FMDenaro is offline   Reply With Quote

Old   February 10, 2020, 08:43
Default
  #10
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,152
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
I assumed you were using a 3D code, but you can do almost all the things I mentioned in 2D as well.

You certainly have an initialization part in your code, and this certainly precedes the one where the gradient is computed for the first time. So, the idea to test the gradient alone involves:

1) Initializing the variables (those whose gradient will be computed) with some known function, taking care of correctly applying the boundary conditions to them (using the same known function in a dirichlet bc or neaumann bc... better to test both implementations actually).

2) Once their gradient is computed you can compute the error with respect to the known one (because you know the function used to initialize the fields, so you can manually take their analytical derivative in the two directions x and y), take some norm of it (I suggest both L2 and L_inf) and print it

3) Just stop the code because you don't need anything else from it

You should repeat the above 3 steps, carefully annotating the resulting error norms, for different classes of functions and different grids, and for each combination of the former using different refinement levels for the given type of grid.

For what concerns the possible functions to use as initialization, you could use:

1) k*x (linear in 1D), you choose k
2) k*x*y, m*x + n*y (linear in multidimensions), you choose k, m and n
.
.
.

etc. with the remaining ones I already mentioned in my previous post. That is, you can certainly initialize your fields with whatever you want.

For what concerns the types of grid, some of the cases I mentioned have indeed no sense in 2D. So you could just do:

1) Uniform cartesian
2) Non uniform cartesian
3) Regular triangular (obtained by splitting the quads of a cartesian along the diagonal)
4) Fully triangular with no special properties

If you can't do cartesian grids, well, just do the kind of grids you can actually use
amine.hanini likes this.
sbaffini is offline   Reply With Quote

Old   February 10, 2020, 10:01
Default
  #11
Member
 
Raphael
Join Date: Nov 2012
Posts: 68
Rep Power: 13
arkie87 is on a distinguished road
Quote:
Originally Posted by sbaffini View Post
The limiter has nothing to do with the gradient accuracy.
I thought that when the limiter is engaged, accuracy approaches first order for all limiters? Isn't that how limiters work?
arkie87 is offline   Reply With Quote

Old   February 10, 2020, 10:02
Default
  #12
New Member
 
Join Date: Feb 2019
Posts: 7
Rep Power: 7
amine.hanini is on a distinguished road
Quote:
Originally Posted by FMDenaro View Post
From a general theoretical point of view, a linear reconstruction of the function can never produce a second order accurate gradient. The only case that happens is for a uniform grid where you have some terms in the local truncation error disappearing.
Thus, the question is what do you want more by doing a LSR?
@FMDenaro, thank you for your reponse.

If I can't obtain second order, I must be a little more than 1st order, say 1.2 or 1.3, for instance, you don't think so ?

Can you guide me to any reference on that topic from a general theoritical point of view ?

And is there any other method to compute the gradient that allow me to be arround 2nd order with my configuration ?

Many thanks
amine.hanini is offline   Reply With Quote

Old   February 10, 2020, 10:13
Default
  #13
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,152
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
Quote:
Originally Posted by arkie87 View Post
I thought that when the limiter is engaged, accuracy approaches first order for all limiters? Isn't that how limiters work?
(Gradient) Limiters are a tool used, specifically, to avoid that variables reconstructed on faces trough gradients get out of bounds. So, you have a gradient and, after the limiter is applied to it, you also have the limited gradient.

You are right that, when the limiter is on, the limited gradient is of reduced accuracy (so that reconstructed values end up being first order) but it has no sense to test the accuracy of the limited gradient. Gradient accuracy is tested with unlimited gradients.
sbaffini is offline   Reply With Quote

Old   February 10, 2020, 10:19
Default
  #14
Member
 
Raphael
Join Date: Nov 2012
Posts: 68
Rep Power: 13
arkie87 is on a distinguished road
Quote:
Originally Posted by sbaffini View Post
(Gradient) Limiters are a tool used, specifically, to avoid that variables reconstructed on faces trough gradients get out of bounds. So, you have a gradient and, after the limiter is applied to it, you also have the limited gradient.

You are right that, when the limiter is on, the limited gradient is of reduced accuracy (so that reconstructed values end up being first order) but it has no sense to test the accuracy of the limited gradient. Gradient accuracy is tested with unlimited gradients.
Makes sense.

Is it possible though that OP is experiencing first-order solution with 2nd order technique because his gradients are limited? I suppose it would have to be limited in every cell for that to occur...
amine.hanini likes this.
arkie87 is offline   Reply With Quote

Old   February 10, 2020, 10:23
Default
  #15
New Member
 
Join Date: Feb 2019
Posts: 7
Rep Power: 7
amine.hanini is on a distinguished road
Quote:
Originally Posted by arkie87 View Post
Makes sense.

Is it possible though that OP is experiencing first-order solution with 2nd order technique because his gradients are limited? I suppose it would have to be limited in every cell for that to occur...
Yes I think in every cell and at every time step I am limited and I go back to first order. Without limiting I have lot of overshootings
amine.hanini is offline   Reply With Quote

Old   February 10, 2020, 10:24
Default
  #16
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,152
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
It depends from a lot of factors, that's why I suggested him testing the gradient separately if that is what he actually wants to do.

On a separate note, the method he is using is not 2nd order, no matter what.
sbaffini is offline   Reply With Quote

Old   February 10, 2020, 10:28
Default
  #17
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,152
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
But how do you know that your two gradient methods actually work and are doing what they are expected to do and you don't have bugs in them? Just out of curiosity
sbaffini is offline   Reply With Quote

Old   February 10, 2020, 10:49
Default
  #18
Member
 
Raphael
Join Date: Nov 2012
Posts: 68
Rep Power: 13
arkie87 is on a distinguished road
Quote:
Originally Posted by sbaffini View Post
But how do you know that your two gradient methods actually work and are doing what they are expected to do and you don't have bugs in them? Just out of curiosity
Along the same lines, it might make sense to build a test case (such as the classic case of flow at 45° angle to the square Cartesian grid with bottom at 0K inlet and left at 100K and with thermal conductivity (diffusion) set to 0), and check if it converges as expected. If you select the test case properly, you can ensure you dont have flux limiter engaged for all the points in the domain, so you can eliminate that effect as well.

I actually did something similar (since i wanted to see effect on false/numerical/artificial diffusion), and discovered a bug in my code when i reversed the flow direction. So, I would recommend trying all combinations.

Perhaps sbaffini can suggest a better test case?
arkie87 is offline   Reply With Quote

Old   February 10, 2020, 10:57
Default
  #19
Senior Member
 
sbaffini's Avatar
 
Paolo Lampitella
Join Date: Mar 2009
Location: Italy
Posts: 2,152
Blog Entries: 29
Rep Power: 39
sbaffini will become famous soon enoughsbaffini will become famous soon enough
Send a message via Skype™ to sbaffini
Quote:
Originally Posted by arkie87 View Post
Along the same lines, it might make sense to build a test case (such as the classic case of flow at 45° angle to the square Cartesian grid with bottom at 0K inlet and left at 100K and with thermal conductivity (diffusion) set to 0), and check if it converges as expected. If you select the test case properly, you can ensure you dont have flux limiter engaged for all the points in the domain, so you can eliminate that effect as well.

I actually did something similar (since i wanted to see effect on false/numerical/artificial diffusion), and discovered a bug in my code when i reversed the flow direction. So, I would recommend trying all combinations.

Perhaps sbaffini can suggest a better test case?
I actually did it in posts n. 6 and 10.


What you suggest is good as well, but comes much later in code verification, it basically is a whole code test.
sbaffini is offline   Reply With Quote

Old   February 10, 2020, 11:06
Default
  #20
Senior Member
 
Filippo Maria Denaro
Join Date: Jul 2010
Posts: 6,764
Rep Power: 71
FMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura aboutFMDenaro has a spectacular aura about
Just to make clear the issues:


- The flux limiting concept is developed in the framework of monotonic-preserving property, it has nothing to do with the accuracy order. It is applied to prevent numerical oscillations around sharp or infinite gradients while the accuracy order is determined on smooth solutions


- the linear reconstruction, that is a polynomial of degree 1, has a second order accuracy for the approximation of the function but when you make the derivative of the polynomial you get a constant function in the interval between the two values. That is a first order accuracy for the derivative. You cannot improve the accuracy by doing a LSR or some type of averaging.


- Obviously, a quadratic reconstruction (that is a polynomial of degree 2) allows for a second order accurate gradient.


- The accuracy of the gradient enters by means of the diffusive flux therefore you need to test a case in which you have an exact solution from a diffusion problem.
FMDenaro is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementation of higher order FD scheme near boundries pawank Main CFD Forum 16 September 23, 2016 08:52
Estimation of order of accuracy lalupp Main CFD Forum 1 October 13, 2015 02:16
upwind finite volume order of accuracy less than one ?? dgfem Main CFD Forum 7 June 1, 2015 04:02
Implementation of 2nd order upwind scheme jaason OpenFOAM Running, Solving & CFD 4 February 6, 2015 17:40
Unstructured grid order of accuracy Biga Main CFD Forum 12 October 13, 2005 19:56


All times are GMT -4. The time now is 20:52.