CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

How to solve nonlinear equation.

Register Blogs Community New Posts Updated Threads Search

Like Tree9Likes
  • 5 Post By Tobi
  • 1 Post By Tobi
  • 1 Post By babakflame
  • 2 Post By blais.bruno

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 27, 2016, 18:39
Default How to solve nonlinear equation.
  #1
New Member
 
Charles
Join Date: Jun 2016
Posts: 2
Rep Power: 0
BoredKoala is on a distinguished road
Hi Foamers,

I'm trying to solve a modified Poisson's equation that has an exponential term. The equation looks like this:

fvm::laplacian(V) = exp(V) + exp(-V) + constant.

From what I understand, the exp terms are actually explicit terms and are not being treated implicitly. Is there anyway to treat the exponential terms implicitly?

Thanks!
BoredKoala is offline   Reply With Quote

Old   June 28, 2016, 08:35
Default
  #2
Senior Member
 
Hesam
Join Date: Feb 2015
Posts: 139
Rep Power: 11
rapierrz is on a distinguished road
Quote:
Originally Posted by BoredKoala View Post
Hi Foamers,

I'm trying to solve a modified Poisson's equation that has an exponential term. The equation looks like this:

fvm::laplacian(V) = exp(V) + exp(-V) + constant.

From what I understand, the exp terms are actually explicit terms and are not being treated implicitly. Is there anyway to treat the exponential terms implicitly?

Thanks!
Hi Charles,

I think you can use polynomial estimation of this functions and extract V from it for implicit solution.
rapierrz is offline   Reply With Quote

Old   June 28, 2016, 19:30
Default
  #3
New Member
 
Charles
Join Date: Jun 2016
Posts: 2
Rep Power: 0
BoredKoala is on a distinguished road
Is there any way to avoid a polynomial approximation?

I'm expecting the exponential to vary from 10^3 to 10^7, so I don't expect polynomial approximations to be accurate.

On a side note, I'm not sure how to code in an implicit squared term. As far as I know, only linear terms can be implicit. Do you know of a way to code in an implicit, nonlinear term?
BoredKoala is offline   Reply With Quote

Old   July 3, 2016, 18:52
Default
  #4
Member
 
Bruno Blais
Join Date: Sep 2013
Location: Canada
Posts: 64
Rep Power: 13
blais.bruno is on a distinguished road
Hello,

You will need to use sub-cycling and solve the equation iteratively until it converges. What you could do is start with an approximation or use a relaxation method.

i.e :
- Start with an estimate of V
- Calculate V(i+1)
- Relax V(i+1)
Recalculate V

etc.


Quote:
Originally Posted by BoredKoala View Post
Hi Foamers,

I'm trying to solve a modified Poisson's equation that has an exponential term. The equation looks like this:

fvm::laplacian(V) = exp(V) + exp(-V) + constant.

From what I understand, the exp terms are actually explicit terms and are not being treated implicitly. Is there anyway to treat the exponential terms implicitly?

Thanks!
blais.bruno is offline   Reply With Quote

Old   September 24, 2016, 18:00
Default
  #5
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
Hey Bruno

Can u describe a little bit your cycle:

HTML Code:
- Start with an estimate of V- Calculate V(i+1)- Relax V(i+1)Recalculate V
I am solving the same equation considering both exp(V) + exp(-V) with V as the dependent variable. However, I am interested in combined forms such as a unified sinh function.

I have used the following snippet for solving it:

Code:
solve
        (
              fvm::laplacian(phi)  == beta * sinh(phi) 
        );
My solver for large values of scalar beta crashes, I tried to relax it as follows:

Code:
fvScalarMatrix phiEqn
(               fvm::laplacian(phi) -  beta * sinh(phi)             );          
 phiEqn.relax();          
phiEqn.solve();
I added the following relaxation factor as well to fvSolutions:

Code:
relaxationFactors {    
                                      equations    
                                      {          phi       0.3;
                                           phiFinal   1;      } }
But didn't work. Can u explain your relaxing method in a coded way?

Regards

Last edited by babakflame; September 27, 2016 at 13:14.
babakflame is offline   Reply With Quote

Old   September 25, 2016, 16:22
Default
  #6
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Bad Wörishofen
Posts: 2,711
Blog Entries: 6
Rep Power: 52
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi Bobi,

I am not sure but I think that Bruno means exactly what you describe with - lets say - 500 outer loops. You go out of the loops if the residual is reached (your solution is converged for that time step). After that, go on in time and to the same procedure.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   September 25, 2016, 16:37
Default
  #7
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
Hey Tobi,

Thanks for your reply. I am thinking of using previous iterations for the right hand side of the equation :

Code:
solve         
( fvm::laplacian(phi)  == beta * sinh(phi) );
and relaxing the resulted equation. Here:
http://www.cfd-online.com/Forums/ope...tml#post619187

I used
Code:
fieldName.oldTime()
member function with no success, I am gonna use these member functions Now:

Code:
  fieldName.storePrevIter();
  fieldName.prevIter()
Regards
babakflame is offline   Reply With Quote

Old   September 25, 2016, 17:01
Default
  #8
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Bad Wörishofen
Posts: 2,711
Blog Entries: 6
Rep Power: 52
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi Bobi,

if you first solve your equation and relax your quantity, than you do not relax the equation, you relax the field. It is different. I can not tell you in a accurate way (because I never jumped into that area and only have some feeling) but for example, we do not relax the pressure equation. We relax the field. I think there is somehow a physical meaning behind, that I can not tell you.

By the way. The equation:

Code:
fvm::laplacian(V) = sinh(V)
should already be explicit and implicit because the source term on the RHS is treated explicit. The equation therefore is:

\nabla^2 V^{n+1} = \mathrm{sinh}(V^{n})

If you want to solve this equation explicit you have do to it like:

Code:
fvc::laplacian(V) = sinh(V)
Out of the box I would say that fvc is not inherit by laplacian and therefore you have to do it like that:

Code:
fvc::div(fvc::grad(V)) = sinh(V)
If you remove your storePrevIter() line and compile and run the code without error, then you do not need that line. If I am correct, this function call is needed if you solve explicit or in a second order time scheme. So if the solver is not returning an error message about that function, you do not need it and hence, you dont use old values.

You can have a look into my Gray-Scott-Foam ... Here I solved explicit and implicit and if I remember correct, I had to use the function there.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   September 25, 2016, 23:11
Default
  #9
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
Dear Tobi

Many thanks for your reply.

I think you are completely correct. The following snippet:

Code:
solve
        (
              fvm::laplacian(phi)  == beta * sinh(phi) 
        );
actually solves the above equation in this manner:

\nabla ^2 \varphi^{n+1} =\beta \; \sinh(\varphi^{n}) in which n is the iteration number.

Since, I replaced the right hand side of above equation with
Code:
phi.prevIter()
and provided comparable results in small values of beta and crashed in larger values.
So I assume that when using explicit terms in iterative methods, its always previous iteration values (It should be, since Implicit matrix of coeffiecients should assume them as known values).

I looked into this thread written by u regarding Gray-Scott Foam:
http://www.cfd-online.com/Forums/ope...ott-model.html

However, It seems that the links in your post didn't lead me to your solver. It says its under construction.

BTW, the fully explicit method:

Code:
solve
        (
              fvc::laplacian(phi)  == beta * sinh(phi) 
        );
crashed.

I have searched OpenFoam forum and found out that the only way that exponential term can be written implicitly is:

Code:
solve
        (
              fvm::laplacian(phi)  == fvm::Sp(beta * sinh(phi) / phi, phi) 
        );
The above formulation did not work for me. And the under-relaxation did not work too.

I am still interested to make the right hand side more implicit.

Pickard's method (http://www.cfd-online.com/Wiki/Sourc..._linearization) or adding a term to both sides :http://www.cfd-online.com/Forums/openfoam-programming-development/176413-source-term-modification-poisson-equation-does-not-affect-results-2.html#post619187
are possible thoughts.


Finally, please check the provided link to your valuable Gray-Scott model.

Regards


Last edited by babakflame; September 26, 2016 at 01:03.
babakflame is offline   Reply With Quote

Old   September 26, 2016, 03:01
Default
  #10
Super Moderator
 
Tobi's Avatar
 
Tobias Holzmann
Join Date: Oct 2010
Location: Bad Wörishofen
Posts: 2,711
Blog Entries: 6
Rep Power: 52
Tobi has a spectacular aura aboutTobi has a spectacular aura aboutTobi has a spectacular aura about
Send a message via ICQ to Tobi Send a message via Skype™ to Tobi
Hi ,

my Homepage is not available at the moment. I am doing some stuff. Today it will be available again. I think in a few hours.
To the stuff of non-linear terms... I think your solution is not correct. Non-linear terms like:
V^2 can be treated fully explicit (the easiest way) like:

Code:
== V * V   //- Explicit | old time
or we linearize them like V^{n+1} V^{n} which would be:

Code:
== fvm::Sp(V, V)
Why we use Sp is based on the sign of the value of the quantity V because if it is a negative sign, we will still treat it as explicit part. Why? The reason for that is that we try to improve the diagonal dominance of the matrix. If the quantity is negative, we would decrease the diagonal dominance, which lead to a bad matrix. Hence, only positive values are put to the matrix whereas, negative once go to the RHS (explicit).
Kummi likes this.
__________________
Keep foaming,
Tobias Holzmann
Tobi is offline   Reply With Quote

Old   September 26, 2016, 09:05
Default
  #11
Member
 
Bruno Blais
Join Date: Sep 2013
Location: Canada
Posts: 64
Rep Power: 13
blais.bruno is on a distinguished road
The formulation you mentioned, that is :

Code:
solve
        (
              fvm::laplacian(phi)  == fvm::Sp(beta * sinh(phi) / phi, phi) 
        );
Is actually only partially implicit and would most likely be quite unstable. I would not use that.

What you need to do is relax your equation a lot more than by 0.3. Lets say you are solving :

Code:
solve
        (
              fvm::laplacian(phi)  == beta*sinh(phi)
        );
Then you relax and you loop (you need to have an outer loop that loops untils the change in the solved phi is smaller than the tolerance you wish to attain). You will need a very low relaxation factor if beta is large. Something like 1e-2, 1e-3 or even less. Your equation is highly non-linear and OpenFOAM does not really have non-linear solvers for fields.
Another thing you could try to do is to first solve the equation with the Taylor expansion of the source partially implicitely in order to get a good estimate and then solve the full equation with the right member explicit. Another way could be to solve transient version of this equation, which is an indirect way or relaxing the way you solve the equation.


Quote:
Originally Posted by babakflame View Post
Dear Tobi

Many thanks for your reply.

I think you are completely correct. The following snippet:

Code:
solve
        (
              fvm::laplacian(phi)  == beta * sinh(phi) 
        );
actually solves the above equation in this manner:

\nabla ^2 \varphi^{n+1} =\beta \; \sinh(\varphi^{n}) in which n is the iteration number.

Since, I replaced the right hand side of above equation with
Code:
phi.prevIter()
and provided comparable results in small values of beta and crashed in larger values.
So I assume that when using explicit terms in iterative methods, its always previous iteration values (It should be, since Implicit matrix of coeffiecients should assume them as known values).

I looked into this thread written by u regarding Gray-Scott Foam:
http://www.cfd-online.com/Forums/ope...ott-model.html

However, It seems that the links in your post didn't lead me to your solver. It says its under construction.

BTW, the fully explicit method:

Code:
solve
        (
              fvc::laplacian(phi)  == beta * sinh(phi) 
        );
crashed.

I have searched OpenFoam forum and found out that the only way that exponential term can be written implicitly is:

Code:
solve
        (
              fvm::laplacian(phi)  == fvm::Sp(beta * sinh(phi) / phi, phi) 
        );
The above formulation did not work for me. And the under-relaxation did not work too.

I am still interested to make the right hand side more implicit.

Pickard's method (http://www.cfd-online.com/Wiki/Sourc..._linearization) or adding a term to both sides :http://www.cfd-online.com/Forums/openfoam-programming-development/176413-source-term-modification-poisson-equation-does-not-affect-results-2.html#post619187
are possible thoughts.


Finally, please check the provided link to your valuable Gray-Scott model.

Regards

blais.bruno is offline   Reply With Quote

Old   September 26, 2016, 22:54
Default
  #12
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
Hey,
@ Tobi
Many thanks for the discussion Tobi.
HTML Code:
 I think your solution is not correct.
I do not recommend the
Code:
solve         
            (               
            fvm::laplacian(phi)  == fvm::Sp(beta * sinh(phi) / phi, phi)          
            );

by myself too, since it is highly unstable. What I did up to now as a modification for the primary equation in previous posts is using this modifications to my fvSolution file:
Code:
phi
    {
    solver smoothSolver;
    smoother GaussSeidel;
    preconditioner DIC;
    tolerance 1e-11;
    relTol 0.2;
    }
With above settings for my Matrix solver and with the following snippet:
Code:
solve
        (
              fvm::laplacian(phi)  == beta * sinh(phi) 
//          fvm::laplacian(phi) - beta * (phi + (1/6)*pow(phi,3) + (1/120)*pow(phi,5) + (1/362880)*pow(phi,9) + (1/39916800)*pow(phi,11))
        );
I am capable of raising the value of \beta up to 14 now. I am still interested to increase the value of \beta


BTW, I am gonna take a look at the your Gray-Scott model soon

@ Bruno

Thanks bruno for your comments. As I mentioned above, I figured it out that the
Code:
solve         
           (               
           fvm::laplacian(phi)  == fvm::Sp(beta * sinh(phi) / phi, phi)          
           );
is highly unstable. I am gonna try your algorithm soon with feedbacks.

All in all, Thanks for discussions.

Regards
Tobi likes this.
babakflame is offline   Reply With Quote

Old   October 1, 2016, 17:31
Default
  #13
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
Hey Tobi,

I looked at your solver, It seems that you had a non-linear term in your equation, however, I couldn't find the source code. When I click on the link, it directs me to the following link:
https://bitbucket.org/shor-ty/flameletmodel

Which I assume is the solver both of us were using before.


@ Bruno

Still can not get your idea for looping. If you can make it coded, could be judged better, Otherwise just an idea
HTML Code:
You will need a very low relaxation factor if beta is large. Something like 1e-2, 1e-3 or even less.
The relaxation factor u propose is somehow illogical, since increases the time of simulation 100 times.

HTML Code:
Another  thing you could try to do is to first solve the equation with the  Taylor expansion of the source partially implicitely in order to get a  good estimate and then solve the full equation with the right member  explicit. Another way could be to solve transient version of this  equation, which is an indirect way or relaxing the way you solve the  equation.
None of your above ideas works, since it was the first thing came to my mind a long time ago.


However, on the contrary to Bruno post, OpenFoam does have a solver capable of handling stiff non-linear equations:


Code:
solver smoothSolver;
    smoother GaussSeidel;
    preconditioner DIC;
    tolerance 1e-11;
    relTol 0.2;
Employing above settings easily helps to deal with highly non-linear equations, as mine. Enjoy it Foamers and keep Foaming.
babakflame is offline   Reply With Quote

Old   October 3, 2016, 09:18
Default
  #14
Member
 
Bruno Blais
Join Date: Sep 2013
Location: Canada
Posts: 64
Rep Power: 13
blais.bruno is on a distinguished road
I think a little bit more manners would be appreciated in your answer.

First,
Code:
solver smoothSolver;
    smoother GaussSeidel;
    preconditioner DIC;
    tolerance 1e-11;
    relTol 0.2;
Is a linear equation and not a non-linear equation solver. This is an iterative algorithm to solve a linear system of equations and does not constitute a non-linear solver.

If you wish to obtain a solution to a non-linear system, you need to use an iterative process such as Newton's method (2nd order convergence rate, more sensitive to initial conditions ) or using Picard's method (slower convergence). For Newton method you can use an explicit version of the jacobian (that is, you know the equation of the jacobian and you write it down) or a numerical version of it (which is akin to a secant method)

HTML Code:
The relaxation factor u  propose is somehow illogical, since increases the time of simulation 100  times.
This is not illogical. Relaxation of an equation is a slow process to solve a non-linear equation when compared to Newton's method or Picard. If you equation is highly non linear, then yes it may require a very small relaxation factor.

As for the code to the loop, it is identical to using relaxation, with a while loop around the relaxation to check for convergence.

Regards,
BB

Quote:
Originally Posted by babakflame View Post
Hey Tobi,

I looked at your solver, It seems that you had a non-linear term in your equation, however, I couldn't find the source code. When I click on the link, it directs me to the following link:
https://bitbucket.org/shor-ty/flameletmodel

Which I assume is the solver both of us were using before.


@ Bruno

Still can not get your idea for looping. If you can make it coded, could be judged better, Otherwise just an idea
HTML Code:
You will need a very low relaxation factor if beta is large. Something like 1e-2, 1e-3 or even less.
The relaxation factor u propose is somehow illogical, since increases the time of simulation 100 times.




HTML Code:
Another  thing you could try to do is to first solve the equation with the  Taylor expansion of the source partially implicitely in order to get a  good estimate and then solve the full equation with the right member  explicit. Another way could be to solve transient version of this  equation, which is an indirect way or relaxing the way you solve the  equation.
None of your above ideas works, since it was the first thing came to my mind a long time ago.


However, on the contrary to Bruno post, OpenFoam does have a solver capable of handling stiff non-linear equations:


Code:
solver smoothSolver;
    smoother GaussSeidel;
    preconditioner DIC;
    tolerance 1e-11;
    relTol 0.2;
Employing above settings easily helps to deal with highly non-linear equations, as mine. Enjoy it Foamers and keep Foaming.
Tobi and arashn18 like this.
blais.bruno is offline   Reply With Quote

Old   October 3, 2016, 13:19
Default
  #15
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
@ Bruno
I believe that general CFD ideas might be more helpful in the following forum:
http://www.cfd-online.com/Forums/main/

You can easily share your ideas there.

In this forum, when I want to give some hints to people, I prefer to do it with samples, since people might have problems in the coding way not necessarily general CFD ideas.



HTML Code:
Still can not get your idea for looping. If you can make it coded, could be judged better
I asked for explaining your idea in a coded way, instead you gave me your new ideas.

Anyway, I don't want to continue this, good luck with your ideas.
babakflame is offline   Reply With Quote

Reply


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
Solve a non-differential equation agustinvo OpenFOAM Programming & Development 3 January 18, 2016 10:22
Calculation of the Governing Equations Mihail CFX 7 September 7, 2014 07:27
solve only transport equation and not Navier-Stokes cosine CFX 2 November 19, 2011 03:58
stability and nonlinear equation. bnedse Main CFD Forum 3 March 2, 2004 13:23
Qeustion: to solve nonlinear ODE Peter Main CFD Forum 1 April 1, 2003 06:47


All times are GMT -4. The time now is 01:46.