## How to solve the problem of negative concentration

Posted Jun 6, 2012, 6:27 AM EDT Fluid Version 3.5a, Version 5.2a 36 Replies

I do not know enough about your model, but I have noticed that if this happens in a time dependent solver case, it often comes from an initial condition of a concentration = 0 on the domain and a value of C>>0 at a boundary. Then the mesh along this boundary is often rather coarse and the diffusivity low, such that the time stepping times mesh size does not link correctly to the diffusivity and the transient gradient along the C>0 boundary is not well resolved. This then results in concentration oscillations and mostly negative values.

If so, you can avoid this by ramping up your concentration with a smoothened step function (Definition Functions) starting at or closer to "0" at t=0 and to use a denser mesh along these abrupt gradient regions

But your issue might be another case too, this is just one guess and one issue I have often observed

--

Good luck

Ivar

Indeed my reark is more applicable to time series analyses

Mesh is important and a too coarse mesh is a common issue, specially with little RAM.

Normally a laminar flow needs quite a dense mesh, but the concentration case could even need a finer mesh, it depends on the diffusivity

Using segegated solvers is one way to minimise RAM, but if you have a werey weak coupling you could consider even solving on.y the CFD part first and then separately the concetration by using the velocity field from the CFD case.

But this might not be valid for your model, it all depends, "many ways to Rome"

--

Good luck

Ivar

For stationary you have (ignore my tensorial mistakes),

- u . grad(c) = - grad . [D . grad(c)]

where c is the primary variable defined in comsol.

In comsol model builder, define a variable

c1=log(c)

Therefore,

c = exp(c1) and

grad(c) = exp(c1)*grad(c1)

So, your transformed equation is:

u . exp(c1)*grad(c1) = grad (D*exp(c1)*grad(c1))

Now go to "transport in dilute species" and click on convection and diffusion equation.

In the velocity field, u, feed

velocity in x = exp(c1)*ux

velocity in y = exp(c1)*uy

ux and uy are the velocities you get from your flow solution.

Now, go to the diffusion field, and feed:

D*exp(c1)

where D is the effective diff coeff.

Finally, if you are solving with initial conc = 0, then put a very small number say 10^-20 so that the variable c1 does not have a problem (log(0)).

This is roughly the derivation, but I have never tried this. If there is a mistake let me know.

You could switch on adaptive meshing with a reasonable initial meshing as a starting point.

Suresh

In comsol model builder, define a variable

c1=log(c)

[...]

Now, go to the diffusion field, and feed:

D*exp(c1)

where D is the effective diff coeff.

[...]

This is roughly the derivation, but I have never tried this. [...]

I've tried this once — some time ago when I had a numerically unstable transport problem with negative concentrations popping up all over the place. I derived the formulas the same way you did (and for the record, see nothing wrong in your derivation), All of this because of the promising advice given in that Comsol Knowledge Base article, which reasons that, if you take the logarithm, the concentrations simply cannot become negative anymore.

Well, this is obviously true. However, at least in my case, the logarithmic transformation made everything much worse. Sure, the concentrations were no longer negative. But they would fluctuate much more wildly than before.

In the end, this isn't surprising at all. As one can see above, transforming the equation in terms of c to something in terms of log(c) introduces those pesky exponentials in diffusion coefficients, and, usually, in source terms as well (since they typically include a dependence on concentration). So except for some pathological cases, this doesn't look like it would solve the stability problem at all.

Also, I couldn't find much literature on "logarithmic transformation" or something (but maybe I didn't search for the right keywords), but the few things I did find here and there pretty much confirmed my observation.

In comsol model builder, define a variable

c1=log(c)

[...]

Now, go to the diffusion field, and feed:

D*exp(c1)

where D is the effective diff coeff.

[...]

This is roughly the derivation, but I have never tried this. [...]

I've tried this once — some time ago when I had a numerically unstable transport problem with negative concentrations popping up all over the place. I derived the formulas the same way you did (and for the record, see nothing wrong in your derivation), All of this because of the promising advice given in that Comsol Knowledge Base article, which reasons that, if you take the logarithm, the concentrations simply cannot become negative anymore.

Well, this is obviously true. However, at least in my case, the logarithmic transformation made everything much worse. Sure, the concentrations were no longer negative. But they would fluctuate much more wildly than before.

In the end, this isn't surprising at all. As one can see above, transforming the equation in terms of c to something in terms of log(c) introduces those pesky exponentials in diffusion coefficients, and, usually, in source terms as well (since they typically include a dependence on concentration). So except for some pathological cases, this doesn't look like it would solve the stability problem at all.

Also, I couldn't find much literature on "logarithmic transformation" or something (but maybe I didn't search for the right keywords), but the few things I did find here and there pretty much confirmed my observation.

You means that it is not a good idea to solve this problem by the method of logarithmic transformation? Except for choosing finer meshes and the method of lograrithmic transformation, it seems that there is not anything other strategy to solve this kind of problem.

Yes, I think the logarithmic transformation is unlikely to help. So use it only as a last, desperate resort. The meshing is much more important, probably the most important setting. Other settings that, in my experience are worth playing around with, are: the tuning parameters for the various stabilization techniques offered by the Transport of Diluted Species interface, the (relative/absolute) tolerances, and (manual) scaling of dependent variables — roughly in this order of importance.

I return to work. i had tried your suggestion. But it still did not pass the problem of negative concentration. It showed that 'Attempt to evaluate real logarithm of negative number.

- Function: log

'

How can I use comsol physcis well? So far I do not get the right results. It seems that they is very few parameters that can be controlled. Just mesh and solvers can be controlled............ Maybe I am wrong.....

I also attach my simulation as following:

Hi,

For stationary you have (ignore my tensorial mistakes),

- u . grad(c) = - grad . [D . grad(c)]

where c is the primary variable defined in comsol.

In comsol model builder, define a variable

c1=log(c)

Therefore,

c = exp(c1) and

grad(c) = exp(c1)*grad(c1)

So, your transformed equation is:

u . exp(c1)*grad(c1) = grad (D*exp(c1)*grad(c1))

Now go to "transport in dilute species" and click on convection and diffusion equation.

In the velocity field, u, feed

velocity in x = exp(c1)*ux

velocity in y = exp(c1)*uy

ux and uy are the velocities you get from your flow solution.

Now, go to the diffusion field, and feed:

D*exp(c1)

where D is the effective diff coeff.

Finally, if you are solving with initial conc = 0, then put a very small number say 10^-20 so that the variable c1 does not have a problem (log(0)).

This is roughly the derivation, but I have never tried this. If there is a mistake let me know.

You could switch on adaptive meshing with a reasonable initial meshing as a starting point.

Suresh

Attachments:

Here is one trick to solve negative concentrations.

I made variable "C" which is defined by "if(c<0,eps,c)" under Definitions => Variables. Instead of concentration "c" I used variable "C". Now variable "C" is never below zero. Eps is very small number defined is Comsol.

Does this help you?

Best regard

Tero

I try to use the boundary layer mesh. The appropriate velocity field could be obtained, but the concentration field was not right (some of the values much exceeded the expectable largest value, and some were much negative). Now my computer memory is enough. However, I do not get the proper results so far.........

Hi,

Here is one trick to solve negative concentrations.

I made variable "C" which is defined by "if(c<0,eps,c)" under Definitions => Variables. Instead of concentration "c" I used variable "C". Now variable "C" is never below zero. Eps is very small number defined is Comsol.

Does this help you?

Best regard

Tero

Try this: if(c<0,eps,if(c>1,1,c)).

Best regards

Tero

It is so good. You are good on using comsol physics. I should learn from experienced experts on this field like you! Thank you so much!

Hi,

Try this: if(c<0,eps,if(c>1,1,c)).

Best regards

Tero

I think about this issue again. I use the method that you mentioned, all positive values of concentration could be obtained. However, maybe it just related to the range of the values that you want to get. For example, by this method we can use C to instead of c, but if you want to show the values of C after finishing the simulation, it is the same as the previous ones without instead of c by C.

Now I doubt this method.........We can continue to discuss this issue. Thank you!

Hi,

Try this: if(c<0,eps,if(c>1,1,c)).

Best regards

Tero

I not sure did I understand you correctly...? This is just modeling trick how to get over problems during solving. Like Ivar said other things (mesh etc) are still main solution to this problems (if you want to get accurate results). When other things are ok, then difference between values C and c should be minimal.

Best regards

Tero

Hi,

I not sure did I understand you correctly...? This is just modeling trick how to get over problems during solving. Like Ivar said other things (mesh etc) are still main solution to this problems (if you want to get accurate results). When other things are ok, then difference between values C and c should be minimal.

Best regards

Tero

often in time series models you have steep gradients at t=0 i.e. concentration at a boundary, nothing inside the domain.

Then I have noticed that using a "boundary mesh" on the c=1 (or T=cnst) boundary can help, as you do not need always the very fine mesh in the full domain (but this is very model dependent) this works fine for me with most of my diffusion driven models, such as HT and Chemistry models

--

Good luck

Ivar

Thank you so much.

My model is stationary model with laminar flow and transport of diluted specie models. To my model I used the boundary layer mesh for the laminar flow, and got physical solution (I had tested the dependence of simulation result on the mesh number, and found it did not change after putting enough meshes and it seemed right from physical meaning). However, when I tried to use the boundary layer mesh for the transport, it got much large negative concentration (for example, some points reached -0.07 mol/l, 7%of the expectable largest value; certainly, I also tested the effect of mesh number, these results were also not feasible). Therefore, I used boundary layer meshes for laminar, and used meshes without boundary layer mesh for transport. These two parts were separated to solve, and the transport part was based on the velocity field obtained from the laminar flow solution. With the later method, I got the concentration field. However, the concentration in the two inlet parts were not so good from physical opinion (larger than 1, and smaller than 0, -3%-3% and most of them were between -2%-2% of the expectable largest value; certainly, they were not far from the expectable values, and the main geometry part (main channnel) could get proper results).

Hi

often in time series models you have steep gradients at t=0 i.e. concentration at a boundary, nothing inside the domain.

Then I have noticed that using a "boundary mesh" on the c=1 (or T=cnst) boundary can help, as you do not need always the very fine mesh in the full domain (but this is very model dependent) this works fine for me with most of my diffusion driven models, such as HT and Chemistry models

--

Good luck

Ivar

indeed my remark was more for time dependent cases with high gradients due to initialcondition steps.

The next on the list, in my view, is the numerical stabilisation parameters, but I'm not use to tweak these for such models so I have no real idea where to start. but its worth to read carefully the "stabilization" chapter in the COMSOl pdf documents

--

Good luck

Ivar

Hi,

Try this: if(c<0,eps,if(c>1,1,c)).

Best regards

Tero

Thank you so much, Tero. I appreciate your helps.

Dear Tero,

First i would like to say thank you because ,i am doing my MS project "modeling of electrosorption" using comsol version 4.3 at Hindustan Unilever R&D,Bangalore and i got the same problem of getting negative values or larger values which are not expected.I searched for resolving this problem in various literatures and available comsol materials but i did not find anywhere and finally i found randomly here(discussion forum) and i tried it and its working well.But i have a few doubts about defining this condition.

(1)How much small number is this eps? or is it constant small number?

(2)what is the condition if c lies between 0 and 1 i.e (0=<c=<1) (here =< represents less than or equall)?

could you please give me the combined condition for this(for all three conditions).

Thanking you,

A.Naveen

Eps is coming from Comsol and it is about 2.2e-16. I have used it instead of zero.

If c is between 0 and 1 it is keeping original value of c. For example if:

c is -1 => return value is 2.2e-16 (eps),

c is 2 => return value is 1,

c is 0.5 => return valus is 0.5,

etc...

I hope this will answer to your question.

Best regards

Tero

Hi,

Here is one trick to solve negative concentrations.

I made variable "C" which is defined by "if(c<0,eps,c)" under Definitions => Variables. Instead of concentration "c" I used variable "C". Now variable "C" is never below zero. Eps is very small number defined is Comsol.

Does this help you?

Best regard

Tero

Hi Tero

I try this for species transport in porous media but my finally concentrations are negative. my problem is unsteady.

Best regards

Attachments:

Diffucult to say without seeing your model... have you check your model mesh? I have never tested this method with unsteady case.

Best regards

Tero

Thanks for the analysis. Is there any way to solve with logarithmic concentration(or any alternative approach) for time dependent drift-diffusion equation. Physically why we can't use your approach time dependent eqn.?

Thanks again,

Sanjoy

I met the exactly same problem as you described.

I am working on 3 coupled PDEs, two from the Joule heating, one PDE form diffusion-drift equation.

The domain is a rectangle.

The boundary condition for the Diffusion-Drift is Zero flux on all edges.

The initial condition for the diffusion-drift is 1E27 [m^-3] in the left half domain and 0 for the right half.

The solver is time dependent with constant damping factor Newton's method and a MUMPS linear solver.

The issue:

Negative concentration keeps to present.

The approaches I have tried:

(1) C=if(c<0,eps,c). No problems caused by negative ion concentration. However, there is a time step within which the maximum iterations of (constant damping) newton's method has been reached. (Resolved by C=if(c<0,1E25,c))

(2) Ultra dense mesh+Boundary mesh layer. Problem "NaN or Inf found" in the linear MUMPS solver. And the speed is so slow that I believe segregated solver has to be implemented.

(3) As the diffusion drift equation is implemented in the PDE interface, there is no stabilization option, although the Joule heating equations are of available stabilization options.

(4) Put a background ion concentration for the right half domain. However, when time increases, there will be negative ion concentration in some regions of the left-half domain.

Can any one help? Many many thanks in advance.

Please see the up message. Thank you for your help.

Hi,

Here is one trick to solve negative concentrations.

I made variable "C" which is defined by "if(c<0,eps,c)" under Definitions => Variables. Instead of concentration "c" I used variable "C". Now variable "C" is never below zero. Eps is very small number defined is Comsol.

Does this help you?

Best regard

Tero

Hi,

What do you mean by - [Instead of concentration "c" I used variable "C"]?

Did you replace "c" with "C"? But c is the main variable.

Can you please elaborate?

I'm sorry, my doubt might be trivial since I'm new to COMSOL.

Thank You,

Shiva

(1) C=if(c<0,eps,c). No problems caused by negative ion concentration. However, there is a time step within which the maximum iterations of (constant damping) newton's method has been reached. (Resolved by C=if(c<0,1E25,c))

Hi,

I believe that I'm trying to solve the same set of PDEs that you are trying to solve as well. Can you please let me know how did you use 'C'?

It is clear that 'c' is the variable that we declare for the number of species in the drift-diffusion PDE. But after you define 'C' as 'if(c=0,eps,c)' or 'if(c<0,1E25,c)', how do we use it? I mean, are we supposed to replace 'c' with 'C' anywhere?

I have simply defined C=if(c=0,eps,c) as a local variable for all the domains where the species exist but still can find negative values of 'c' as and when the the solver solves.

And also which solver would you recommend? I tried both MUMPS and PARADISO. Unfortunately, each solver gives different results.

I'd really be grateful if you can help.

Sincerely,

Shiva

Thanks,

Blake

Hi,

Here is one trick to solve negative concentrations.

I made variable "C" which is defined by "if(c<0,eps,c)" under Definitions => Variables. Instead of concentration "c" I used variable "C". Now variable "C" is never below zero. Eps is very small number defined is Comsol.

Does this help you?

Best regard

Tero

With regards to the issue of negative concentration in general, please see:

www.comsol.com/support/knowledgebase/952/

Based upon some of the previous questions about stabilization in this thread, we would also recommend reading:

www.comsol.com/blogs/understanding-stabilization-methods/

Some of the questions here also seem to touch upon solving a form of the drift-diffusion equations in log form. It is reasonable to say that this would be fairly involved to implement, but it is also worth pointing out that the Semiconductor Module (www.comsol.com/semiconductor-module) does already include such a log formulation for electron and hole transport.

Best Regards,

I know it is in response to your very old post. But I also have the same negative concentration. And I am not able to incorporate the trick you suggested in this post. How do you have 2 variables for concentration c and C?

I haven't use Comsol for ages but I try to remember how this was done.

Yes, I have two variables where "c" is Comsol own parameter and "C" is new variable for calcuation purposes. "C" is used in reactions to prevent negative concentration and it is never below 0.

Did this answer to your quoestion?

Best regards

Tero

## Reply

Please read the discussion forum rules before posting.

## Discussion Forum Rules and Guidelines

The goal of COMSOL Access is to provide a forum for you to communicate effectively with COMSOL as well as your colleagues within the multiphysics simulation community. This involves providing you with access to technical support and downloads of the latest {:comsol} software releases, as well as the ability to share your comments and work with other users of the {:comsol} software through forums such as the blog, discussion forum, and Application Exchange. In order to make this an efficient and pleasant experience for you and other members of COMSOL Access, we ask that you follow a few rules and guidelines.

When registering for COMSOL Access, you agree to provide your complete and truthful information for all fields requested on your COMSOL Access account registration page. You also agree to maintain the accuracy of all information associated with you on your COMSOL Access account. You agree to maintain your COMSOL Access account for use solely by you, not to share your username and password with anyone else, and to take appropriate precautions to restrict access to your username and password from others. Furthermore, you agree not to submit any information relating to your employer through your COMSOL Access account without your employer’s authorization. Should you use a COMSOL Access account associated with an employer, you agree to immediately discontinue using that account upon termination of that employment.

The moderators of the forums will remove any generally objectionable material as quickly as possible. You acknowledge that all posts made to these forums express the views and opinions of the author and not the administrators, moderators, or webmaster (except for posts by these people). Hence, they will not be held liable.

You agree not to post or link to any material that is abusive, obscene, vulgar, slanderous, hateful, threatening, sexually oriented, or that infringes upon or violates any third-party rights or any other material that may violate any applicable laws. You agree that you will not otherwise use your COMSOL Access account to violate or to assist anyone in violating any law. Engaging in any activity in violation of these COMSOL Access rules and guidelines may lead to you being immediately and permanently banned from COMSOL Access. The IP address of all posts is recorded to aid in enforcing these conditions. You agree that the webmaster, administrator, and moderators of the forums have the right to remove, move, or close any topic at any time as they see fit. As a user of COMSOL Access, you agree to any information you have entered into any of the forums being stored in a database.

You agree that you will not use your COMSOL Access account in violation of any applicable export control laws. You represent and warrant that you are not subject to any comprehensive sanction or embargo by the U.S. or any other country, nor are you identified on any list maintained by the U.S. government that identifies persons for which the U.S. maintains restrictions. Further, you represent and warrant that you are not subject to any restriction on the receipt of technology or products under the export control laws of the U.S. or any other country.

### Basic Rules

**Flaming:**Do not post any messages that harass, insult, belittle, threaten, or flame another member or guest. Debates are fine, but argue with the point, not the person.**Trolling:**Do not post with the purpose of starting a dispute. Note that a person disagreeing with your opinion is not considered trolling; keep it civil, even if you are sure that the other person is wrong. Anything seen as trolling will result in you being banned.**Spamming:**Posts without content or containing nonsense waste space and everyone's time. Spam will be removed from the forum.**Offensive Posts, Links or Images:**Do not use profanity, racial, ethnic, religious, or other slurs or any other offensive material.**Advertising:**Posting of advertisements for products or services, links to auctions, affiliate links, links to promote websites, and so forth is not allowed.**Disclosure of Personal Information:**Do not disclose any other member's email, real name, address, phone number, IP address, or other personal information. This includes posting contents of emails and private messages without the sender's consent. Do not bring personal disputes into the forums. Bans and warnings issued to other users are considered personal information.**Misleading Titles:**The subject line of the post should be as informative as possible about the content of the post. This also reduces the amount of duplicate posts.**Thread Hijacking:**Taking a thread off-topic to pursue one's own agenda is not permitted.**Piracy:**Do not upload, post, email, transmit, or otherwise make available any content that infringes upon any patent, trademark, trade secret, copyright, or other proprietary rights ("Rights") of any party. This includes COMSOL model files created by a forged or illegitimate license.**Honesty:**Users must provide truthful information in creating their COMSOL Access account.

These rules are subject to change. The moderators reserve the right to remove, edit, or move posts at their discretion. The COMSOL Access administrators will reserve the right to permanently remove a user account without notice if any of the rules are not followed. Particular services accessible with your COMSOL Access account may be subject to additional rules. You agree to comply with all rules applicable to each service you access through your COMSOL Access account.

### Posting Guidelines

When posting, understand that you are trying to communicate with other people. Although many COMSOL Access members are not fluent in English, the official language of this forum is English.

Here are some important guidelines of language:

- Write in English.
- If you are familiar with LaTeX, please use this to write mathematical equations.
- Always do a quick check for spelling/grammar mistakes.
- Format your post in a legible manner. Use the Preview button often.
- Be concise and articulate as much as possible.
- Use the Enter key to create paragraphs.
- Capitalize correctly. It is difficult to read posts that are written entirely in uppercase or lowercase.
- Use correct punctuation. Avoid run-on sentences.
- Try to avoid using “text speak”, “net speak”, or slang. The purpose of language is to be understood.
- Never invent acronyms and use as few acronyms as possible. For example, write "COMSOL Multiphysics" and not "CMP".
- Review your post before publishing it. Try to catch typos.
- Please check to see if a topic has already been posted. Do not post multiple threads on the same topic.

### Disclaimer

By submitting content to the forums, you hereby grant COMSOL a nonexclusive, royalty-free, perpetual, worldwide, and unrestricted license to reproduce, publicly display, publicly distribute, and prepare derivative works of the content. COMSOL hereby grants you a license to copy and/or use content from the forums solely for your own internal purposes. COMSOL provides the forum service for the benefit of our users to share content with the community. All content is provided "as is" without warranty of any kind, express or implied, including without limitation, warranties of merchantability, noninfringement, design, operation, and fitness for a particular purpose, and the entire risk as to the quality and performance of the programs is with you.

Neither COMSOL, the authors, nor the copyright owners of submitted materials warrant that the programs will be error-free, uninterrupted, virus-free, secure, and suitable for your needs, produce specific results, or that errors or failures will be corrected. Comments on supplied content should be sent to the author or copyright owner through the tools provided in the forums.

Please log in to post a reply.

Note that while COMSOL employees may participate in the discussion forum, COMSOL^{®} software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.

## Suggested Content

- KNOWLEDGE BASE Selecting hardware for clusters
- FORUM Negative Decibel Levels in Results
- KNOWLEDGE BASE Error: "Out of memory"
- FORUM negative concentration at surface
- BLOG Density-Driven Fluid Flow in Porous Media