# Transition from 2D to 3D finite element code, what are the inevitable modifications to be implemented?

Imagine we have a simple 2D FEM solver (we are dealing with solid mechanics) and we would like to develop it to a 3D FEM solver (let's say for the same solid mechanics problem) in this case what are the inevitable changes/modifications to be made in the code?

I understand that some might answer "It depends on the type of the problem and it is different from case to case", but I am curious to know if there is any generic standard procedure for such transformation that should inevitably be considered regardless of the type of the problem?

I am of course talking about the places in the code where I should implement the modifications.

If you need a more specific presentation of the problem, here is the 2D code I am willing to develop to 3D. To see the code please click on this link

So so many places you have to rewrite. The whole mesh handling (accessing faces and edges from cells, neighbors from cells, ...). Shape functions. Dealing with the question of how the normal vector of a face when seen from one cell matches that on a neighboring cell. You will also likely encounter that 3d problems are always much larger and that solvers, preconditioners, data structures, parallelization schemes are all much more stressed and may need to be optimized to be efficient. Everything you have to develop also has to be debugged, and debugging is more complicated in 3d because drawing little pictures on a piece of paper is so much more cumbersome in 3d. It's going to be a lot of work to implement this, much more than just doing 2d simulation.

That's why you shouldn't do it yourself. Rely on one of the big finite element packages out there. Not only do they already have 3d meshes and finite elements implemented for you, they also have nearly everything else you may need in the future already implemented for you. Use your 2d experience as a learning opportunity, then move on to using what others have already built over decades of work.

• I think the whole accessing faces and edges from cell must be stressed. I faced that challenge in the past going from a tri-code to a tet code and everything became so much more complex. Doing it for a traditional Poisson equation was not the challenge. Adding features was. Robin BCs, calculating forces on objects, mesh handling, etc. The extension from 2D to 3D is easy only in theory... Now I use deal.II :)!
– BlaB
Jan 6, 2021 at 3:16
• @Wolfgang, based on your answer I suppose the title of the article whose link I shared in my question is actually wrong. It says "Writing a FEM solver in less than 180 lines of code." the author actually uses Eigen library in order to solve a FEM problem and Eigen is a FEM solver in itself right? so the code that the author of the article wrote is not really a solver but a FEM problem solved using Eigen library. Having said this now if I want to develop the same code from 2D to 3D I simply need to change the problem definition such as geometry and Eigen will take care of the rest right?
– Dude
Jan 6, 2021 at 12:26
• @Dude No, generally Eigen is not a finite element library. It’s a linear algebra library and has some data structures to handle matrices and vectors and some solvers to solve linear equations and eigenvalue problems. So, no Eigen would not take care of anything. Jan 6, 2021 at 19:12
• @Dude Eigen is not a FEM solver, as pointed out by AloneProgrammer. But if you wanted to go down this kind of route, a fully adaptive 2d/3d Poisson solver in deal.II requires only about 100 lines of C++ (measured in the number of semicolons). Take a look at the step-6 tutorial program. If you want to extend this program to so that it runs on 10,000 processors, you'd only have to add ~20 lines of code (step-40). Of course, behind these 20 additional lines of code are several man-years of work done by others that you would have to replicate if you wanted to build things from scratch. Jan 7, 2021 at 4:17
• Similarly, if you wanted to extend either of these two programs from linear elements to, say, cubic or quartic elements, you'd have to change a single number from 1 to 3 or 4 when building on deal.II. If you wanted to do this kind of thing starting from the 180-line code you mention, you'd spend a month of it. Jan 7, 2021 at 4:19