# Solving simple electrostatic problem

**URL:** <https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046>\
**Category:** Modelling & Simulations\
**Tags:** pde, physics, mesh, gmsh\
**Created:** [September 12, 2021, 10:20am UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046 "2021-09-12T10:20:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![isentropic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isentropic/32/11894_2.png) [@isentropic](https://discourse.julialang.org/u/isentropic)\
**Post date:** [September 12, 2021, 10:20am UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/1 "2021-09-12T10:20:50Z")

</div>

I’m fairly new to PDE solving domain. Being overwhelmed with what julia offers [GitHub - JuliaPDE/SurveyofPDEPackages: Survey of the packages of the Julia ecosystem for solving partial differential equations](https://github.com/JuliaPDE/SurveyofPDEPackages#mfe), I could decide on the best way to solve my simple 2d electrostatic problem with Dirichlet boundary conditions. I want to find voltage (or E field) inside a circular region with almost parallel plates. Voltage obeys Laplace’s equation with the boundary conditions drawn in the following picture.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/8/28b4b48779c1faa8be7eb390ac18704e63344493.jpeg)

A have looked at Ferrite.jl and it seemed like an overkill for my problem. I have looked at @ChrisRackauckas 's awesome tutorial [Solving PDEs in Julia - Nextjournal](https://nextjournal.com/sosiris-de/pde-2018), it helped but there were so many packages listed there.

My wishes are the following:

1. Good accuracy, the plates would be barely tilted with respect to each other (maybe a fine mesh would be needed)
2. No external program meshing, I’d like julia to do it (as the geometry is fairly simple)

Best,

---

<div class="post-metadata">

**Author:** ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)\
**Post date:** [September 12, 2021, 2:39pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/2 "2021-09-12T14:39:24Z")

</div>

I would recommend Gridap [https://github.com/gridap/Gridap.jl](https://github.com/gridap/Gridap.jl) .  
Gridap uses the finite element method. You basically has to write the equations in weak form almost exactly as you would by hand. Just checkout the examples and tutorials and you will find something useful.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 12, 2021, 4:17pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/3 "2021-09-12T16:17:21Z")

</div>

> [@isentropic](#):
>
> No external program meshing, I’d like julia to do it (as the geometry is fairly simple)

You can use [Gmsh.jl](https://github.com/JuliaFEM/Gmsh.jl) to mesh the geometry within Julia (with the help of the gmsh library under the hood) for use with Gridap.jl.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 12, 2021, 4:41pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/4 "2021-09-12T16:41:40Z")

</div>

> [@stevengj](#):
>
> You can use [Gmsh.jl](https://github.com/JuliaFEM/Gmsh.jl) to mesh the geometry within Julia

Is there a user-friendly documentation for this package?  
This is not the first time we see Julia packages that refer to something else, that refer to …

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [September 12, 2021, 4:52pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/5 "2021-09-12T16:52:10Z")

</div>

Are you sure you want to have the boundary conditions like this? In fact, you have three electrodes now with a given potential. Is that what you want? I suspect you may want to have just the two electrodes, each with a given potential, and the boundaries modelled as if they were far away from the electrodes.

---

<div class="post-metadata">

**Author:** ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)\
**Post date:** [September 12, 2021, 5:21pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/6 "2021-09-12T17:21:07Z")

</div>

To learn how to use the package, follow the tutorial [https://gitlab.onelab.info/gmsh/gmsh/-/tree/gmsh\_4\_8\_4/tutorial/julia](https://gitlab.onelab.info/gmsh/gmsh/-/tree/gmsh_4_8_4/tutorial/julia). The tutorial is basically a few examples of mesh generation. The basics are pretty straight forward, you just need to get the hang of it. But for a simple mesh, just specify points and edges and surfaces (defined by edge loops).

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 12, 2021, 6:19pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/7 "2021-09-12T18:19:02Z")

</div>

@Paulo_Jabardo, thanks and sorry, because as far as documentation goes, it looks utterly unsatisfying.

Maybe one needs to [start here](https://gmsh.info/) and drill down.

---

<div class="post-metadata">

**Author:** ![Paulo\_Jabardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulo_jabardo/32/3196_2.png) [@Paulo\_Jabardo](https://discourse.julialang.org/u/Paulo_Jabardo)\
**Post date:** [September 12, 2021, 6:53pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/8 "2021-09-12T18:53:58Z")

</div>

You are correct that the documentation is not very good or clear. And linking to [http://gmsh.info](http://gmsh.info) won’t help much. One of the main issues with gmsh, IMHO, is that it looks like a graphical mesh generator but in reality it is a scripting language for describing geometry that has a GUI. It is kind of difficult to get started and the best way is to go through the tutorial scripts which are well documented in the comments.

I just checked the julia tutorial and remembered that the julia tutorial codes have almost no comments (sorry for that…) but it is line by line translation of the python code that has comments [https://gitlab.onelab.info/gmsh/gmsh/-/tree/gmsh\_4\_8\_4/tutorial/python](https://gitlab.onelab.info/gmsh/gmsh/-/tree/gmsh_4_8_4/tutorial/python)). Side by side comparison is fairly simple.

The reference manual [http://gmsh.info/doc/texinfo/gmsh.html#Tutorial](http://gmsh.info/doc/texinfo/gmsh.html#Tutorial) has all the tutorial codes in gmsh’s own language and they are well documented even though the translation to Julia has some quirks.

That is the way to get started when using gmsh.

---

<div class="post-metadata">

**Author:** ![isentropic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isentropic/32/11894_2.png) [@isentropic](https://discourse.julialang.org/u/isentropic)\
**Post date:** [September 13, 2021, 4:22am UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/9 "2021-09-13T04:22:44Z")

</div>

Yes I’m sure, real ground in a cylinder with voltage V=0. This is a fairly common setup for beampipes in accelerators

---

<div class="post-metadata">

**Author:** ![isentropic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isentropic/32/11894_2.png) [@isentropic](https://discourse.julialang.org/u/isentropic)\
**Post date:** [September 13, 2021, 4:26am UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/10 "2021-09-13T04:26:54Z")

</div>

Looking at [https://gmsh.info/](https://gmsh.info/) is feels like an overkill for my needs. The problem is 2-dimensional, ideally i would just want to see the mesh via Plots.jl or something similar. I’m not even sure if FEM is the right tool to use. Could it be advantageous to just use finite difference methods with very basic grid -like mesh?

---

<div class="post-metadata">

**Author:** ![isentropic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isentropic/32/11894_2.png) [@isentropic](https://discourse.julialang.org/u/isentropic)\
**Post date:** [September 13, 2021, 4:28am UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/11 "2021-09-13T04:28:29Z")

</div>

Julia native meshing would be perfect, as I want to change the geometry slightly and perhaps do some optimizations with regards how to make fields look like I want

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [September 13, 2021, 7:07am UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/12 "2021-09-13T07:07:51Z")

</div>

how can you plot the mesh then? in JuliaPlots/Makie?

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [September 13, 2021, 10:46am UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/13 "2021-09-13T10:46:21Z")

</div>

Of course you can solve this just as a simple PDE, but given the nature of the problem, the geometry and your accuracy requirements I wonder if it might not be better to use something like a boundary element method? This way hopefully you’d just have to discretize lines.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 13, 2021, 12:50pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/14 "2021-09-13T12:50:41Z")

</div>

One of my favorite methods is to represent the electrical potential as a radial basis function expansion and to solve for the coefficients. This is mesh free, you can just plop the basis functions down on centers either in a grid or with a layout that matches the geometry in some way.

Remember to place some centers outside the domain, this will help in getting the boundary conditions right.

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [September 13, 2021, 2:53pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/15 "2021-09-13T14:53:02Z")

</div>

@dlakelan, any citations or links you can provide on the radial basis function expansion approach?

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [September 13, 2021, 3:06pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/16 "2021-09-13T15:06:11Z")

</div>

I’d second the recommendation for BEM. Has anyone extended [BEAST](https://github.com/krcools/BEAST.jl) to potential theory, or are there other good packages?

---

<div class="post-metadata">

**Author:** ![isentropic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isentropic/32/11894_2.png) [@isentropic](https://discourse.julialang.org/u/isentropic)\
**Post date:** [September 13, 2021, 3:07pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/17 "2021-09-13T15:07:49Z")

</div>

This sound very interesting, would you mind giving some more details. Maybe some introductory texts, Julia packages, anything else?

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [September 13, 2021, 3:15pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/18 "2021-09-13T15:15:51Z")

</div>

> [@Ralph\_Smith](#):
>
> I’d second the recommendation for BEM. Has anyone extended [BEAST](https://github.com/krcools/BEAST.jl) to potential theory, or are there other good packages?

Can’t you just take the Helmholtz kernel with k=0?

> This sound very interesting, would you mind giving some more details. Maybe some introductory texts, Julia packages, anything else?

Don’t ask me, I know next to nothing about it. I’d start with the wikipedia page then try to look up sources close to your scientific field. Probably, like all Green’s function methods, it has 30 different names depending on the community. Keywords that might be related (again I’m just relaying what I’ve heard mentioned in connection to the topic): single/double layer potential, methods of moments

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 13, 2021, 3:38pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/19 "2021-09-13T15:38:47Z")

</div>

Even with BEM you still need to learn how to generate a (surface) mesh, although in 2d this is just lines. Really, there’s not much alternative to learning Gmsh or some similar package in the long run.

I agree that BEM or related surface integral equation methods (e.g. Nystrom) could be the most computationally efficient methods for this problem, but they are finicky to implement well and have a steep learning curve. I suspect it will be a lot quicker (in programmer time) just to bite the bullet and learn Gmsh and Gridap, unless BEAST already solves exactly the problem you want. The problem you are describing is not too computationally demanding.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 13, 2021, 4:41pm UTC](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046/20 "2021-09-13T16:41:29Z")

</div>

> [@tshort](#):
>
> citations or links you can provide on the radial basis function expansion approach?

At one point I had a number of citations of papers I had actually read, but my zotero instance is acting up. so I’m going to just point you to a variety of results from google, from which you can branch out. In my experience the “ill conditioning” of the matrices is less of an issue when using compact basis functions or those that decay rapidly (like gaussians). But I admit to not having extensive examples. I think of the conditioning problems as occurring when you need a lot of cancellation between different basis functions, which occurs when the basis functions are near-constant across large spatial regions. It can also be useful to include in the basis a set of functions that represent the partial derivatives of the main basis functions.

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

> **[Article4.pdf](https://sciresol.s3.us-east-2.amazonaws.com/IJST/Articles/2016/Issue-45/Article4.pdf)**
>
> 572.76 KB

> **[Solving PDEs with radial basis functions\* | Acta Numerica | Cambridge Core](https://www.cambridge.org/core/journals/acta-numerica/article/abs/solving-pdes-with-radial-basis-functions/CC8E600B719A97616843B618A19B0D6F)**
>
> Solving PDEs with radial basis functions\* - Volume 24

[http://www.it.uu.se/research/project/rbf](http://www.it.uu.se/research/project/rbf)

> **[FULLTEXT01.pdf](https://uu.diva-portal.org/smash/get/diva2:1159181/FULLTEXT01.pdf)**
>
> 825.13 KB

The essence of solving PDEs in general is to create some family of functions that are general purpose approximators, and then to either evaluate some integral/weak form of the equations, or set the equations to be exact at some set of points (colocation methods). In the RBF expansion the RBF expansions are the family of functions, and typically the colocation method is used at a set of points. In the FEM method low order polynomials on patches of space knitted together are the family of functions, and either collocation at nodes, or some kind of gaussian quadrature weak form is used to get the equations to solve.

The advantage of the RBF approach is that you don’t need a mesh, and typically you get exponential convergence with respect to the number of basis functions, also it works without a regular geometry as compared to some spectral methods which work much better for say periodic boundary conditions on simple regions of space.

I’ve actually done this in Maxima to solve some heat equations on simple geometries like the one described here, with as few as 5-20 basis functions and gotten good results.

[Next page](https://discourse.julialang.org/t/solving-simple-electrostatic-problem/68046.md?page=2)
