Adaptive Mesh Refinement in 3D

Hello,

I am using NGSolve to solve Maxwell’s equations in time-harmonic form.
I am trying to use the adaptive refinement loop (Solve→Estimate→Mark→Refine→Solve→) to refine the mesh.
I used the approach in the example in section 1.6 (Error estimation & adaptive refinement) to do that, with the necessary changes; but this doesn’t work. The step “mesh.SetRefinementFlag(el, elerr[el.nr] > 0.25*maxerr)” doesn’t work even if I change the percentage of the max error.

Note: The example in section 1.6 works for 2D problem whereas my problem in 3D, so does this approach work for 3D meshes?

I am grateful for any explanation

Here are my python script.

Attachment: simple_example_adaptive.py

Hi Elsakori,

your exact solution may produce a singularity at (0,0,0) with the term (x2+y2+z**2)**0.6.
After regularizing this term it seems to work now.

By the way: You do not need sympy for computing the right-hand side for your problem. NGSolve has its own Diff method.

Attached you’ll find the code.

Best regards,
Michael

https://ngsolve.org/media/kunena/attachments/889/simple_example_adaptive2.py

Attachment: simple_example_adaptive2.py

Hi Michael,

Thank you for your response and the changes that you made in the code. you are right, the exact solution produce a singularity at (0,0,0) with the term (x2+y2+z**2)**0.6. So, I expected a finer refinement around (0,0,0), but the refinement went in strange way. I added a grid function “e” to store the local exact error (which is the estimate used to mark the elements) for each element; but by drawing “e” we can see that some elements with big errors were not refined whereas other were refined with small error values.
My question again, does the attribute “SetRefinementFlag” work with 3D meshes?. Because I couldn’t find any other adaptive refinement example that work for 3D problems.

Note: I can’t used the Diff method of NGSolve, it gives me this error
File “”, line 26, in
AttributeError: ‘ngsolve.fem.CoefficientFunction’ object has no attribute ‘Diff’.

I am grateful for any explanation

Here are my python script after adding the grid function “e”.

Attachment: simple_example_adaptive2.py

Hi Elsakori,

yes, you are right. There seems to be a bug regarding the SetRefinementFlag in 3D.
We are working to fix it.

The Diff method for CoefficientFunctions is a quite new feature. It should work, if you download the latest version of NGSolve.

Best,
Michael

In netgen either 2d or 3d elements can be marked for refinement. This results in the difference, that for 3d you have to mark all surface elements as False for refinement. Since only 2 and 3d elements can be marked this is not necessary in 2D. So add these 2 lines to your code:

for el in mesh.Elements(BND):
  mesh.SetRefinementFlag(el, False)

Best
Christopher

The default behaviour has been changed now to not automatically refine boundary elements. In the upcoming releases you can skip the setrefinmentflag for the bnd elements.

Best
Christopher

Hello Christopher,

Thank you for your assistance.
Do I need to Install the latest release or the nightly build release to get these changes?.

Regards,

Ahmed

Hi,
currently it is only in the nightly release, but we plan to release 1909 this weak.
Best
Christopher

Hello Christopher,

The latest nightly build doesn’t work on my macOS (10.12.6). Actually any release from September 3 and older works fine, but the latest ones (September 4 and later) do not work.

Best,

Ahmed

Hello,

I was trying to do something very similar here. I would like to solve an electrostatic problem in 3D and use the adaptive refinement method to improve the result accuracy.

In practice I want to do the following.
I build a 3D geometry composed of a prism which contains come charge density surrounded by a “vacuum” domain enclosed in a far external surface (a sphere).
I than try to solve the BVP of the related Poisson problem with the adaptive refinement algorithm as in the example in section 1.6 (Error estimation & adaptive refinement).
I also added the lines of code suggested by Christopher to remove the refinement flags for surface elements.

As a result, i find a strange behavior of the error estimator. Indeed, with increasing mesh density the
error estimate tends to zero, but not with a monotonic manner (as in the tutorial example).
I have the feeling there is something wrong going on, but I can’t say if it’s a problem of my code or something related to the 3D refinement as faced by Ahmed.

I’m attaching the code.
Thanks in advance for the help.

Tommaso

Attachment: ngsolve_optimize_prism.py

Hi Tomasso,

have a look at your solution … and think about your boundary conditions

J

Hi Joachim,

My bad. It was a stupid mistake. Thanks for pointing that out.

Best,
Tommaso