This week Dunyu Liu a computational scientist at the Institute of Geophysics at the University of Texas at Austin, shares his tips, tricks, and best practices for painless code development.
The scientific world is becoming more and more computational, with numerical methods and models being critical to many disciplines such as geodynamics, earthquake dynamics and climate modelling to name a few. My experience with Finite Element Methods using FORTRAN spans to my undergraduate years which I applied to simulate earthquakes and study rupture dynamics. Earlier this year however, I took up the role of a computational geoscientist at Institute for Geophysics (UTIG) at University of Texas Austin, which allowed me to expand my research efforts into the fields of geodynamics and climate modelling. Through my involvement in different projects at UTIG I have developed models in different coding languages, used different software, and spent considerable effort in getting codes and files organised. Along the way, I benefitted from some nice tools and ideas, as well as some scientific computing platforms. These can greatly reduce the barriers for newcomers to use and develop numerical models based on Finite Element Method. Read on for some handy tips and tricks
If you or your research group develop exciting software/numerical models, with several contributors adding new features to it, you may end up with codes of many different versions that are so hard to manage and track after a few months/years of development. Errors may pop up occasionally, and although you might eventually figure the root of the issue, this often means several days’ or weeks’ worth of work. A simpler, cleaner approach is to use version control and one way of doing that is to use a GitHub repository. This is a powerful tool that can help you version control and organise the codes while facilitating collaborations.
GitHub is free to use and once you register a GitHub account, it is very easy to create a repository on it. As shown below, all you need to do is to name your repository, add a description, choose to make it either public or private, and finally add a README file and a license (a really good and recommended habit). Once this is set up you can start uploading your codes/files to the repository and start your version control! You can either upload your code/files directly to the web version or choose to install GitHub Desktop on your local machine and manage everything in your local cloned repository, which method I am using.
Let’s use my GitHub repository named Geodynamics_FEniCS as an example. This hosts the codes I develop for specific geodynamic models. I recently implemented some new features in the code 3_stokes_benchmark.ipynb which is a Python Jupyter notebook and I update the new file in my local GitHub repository as you can see in Figure 2 my GitHub Desktop interface reflects the change. After I describe the changes in the left bottom boxes, I hit the “Commit to main” button to “take a photo” of my current repository. If in my next round of code development, I mess up my whole code and couldn’t find the cause, I can always go back to the last “photo” (or commit in GitHub’s language) to restart the development. Then I can use “pull” or “push” button to sync my local and remote repositories
Note that developing a good habit of tracking changes frequently with written documents and making small changes in each development will be a huge asset as you develop your code. This will avoid situations (which have definitely happened to me in the past) where you develop new exciting features without tracking the changes only to discover that the new modifications didn’t work out! In such a situation our only option would be to start scratch from the “photo” taken a few weeks ago. Taking baby steps and making weekly documentation have really save me from my old nightmares of wasting a few weeks’ time when things don’t work out.
What we are essentially doing in this approach is to manage our digital “assets”: codes, notes, work logs, manuscripts, etc. I find using that GitHub with other tools such as Google Docs/OneNote and developing a good habit to document frequently helps greatly in the long run. Here is an example of a GitHub repository that I developed with Dr. Geeta Persad from UT Austin (https://github.com/GeePersClim/UT_CESM2_Guide) for porting the Community Earth System Model (CESM) which is widely used in climate modelling on High Performance Computers at the Texas Advanced Computing Center (TACC). The README file hosts links to quite a few quick start guides that are developed in Google Docs.
- FEniCS is a platform that makes developing and using Finite Element Method easier than ever before. Finite Element Methods (FEMs) are powerful numerical methods that can simulate various physical processes governed by partial differential equations. My expertise is in developing parallel FEMs and codes to simulate earthquake dynamics. Back in my undergraduate days, learning the theory of FEM, how to code in FORTRAN, parallelise the code, and learn how to debug was a steep learning curve for me. Luckily now, there are platforms such as FEniCS which greatly reduce the barriers for newcomers to develop and use FEMs. This doesn’t mean that you do not need to learn the mathematics and theory behind the FEM, but the learning curve has been greatly reduced.
FEniCS’ strengths are fourfold:
- FEniCS automatically handles a significant amount of coding for the user that involves assembly of stiffness matrix and force vectors, etc. For that part of coding, it would be error free.
- The only coding task left for the user is in the widely popular and easy to learn language Python. And the syntax is pretty much similar to the mathematical formulations, like the weak forms of the FEM.
- It is open-source, versatile and applicable to many physical problems. Users can easily choose different function spaces, customise different solvers which are then leveraged on powerful scientific computing packages such as PETSc
- It has great community support, great documentation, and many nice tutorials. This is a very big plus.
Figure 3 shows the simplicity of the FEniCS code to solve the Poisson equation. Amazingly, you can implement a FEM for Poisson equation in less than two dozens of lines!
Over the past year, I have developed several codes with FEniCS to simulate elastic deformation from 2D cracks or 3D faults, viscoelastic deformation, and viscous deformation governed by Stokes equation. If you have plans to use FEMs in your research but don’t know where to start, I would highly recommend you to give FEniCS a try.
Here are some great resources and tutorials that I found particularly useful:
- The FEniCS book by Drs. Anders Logg, Kent-Andre Mardal, Garth N. Wells, https://fenicsproject.org/book/
- DOLFIN demo: https://fenicsproject.org/olddocs/dolfin/1.3.0/python/demo/
- Numerical tours of continuum mechanics using FEniCS by Dr. Jeremy Bleyer https://comet-fenics.readthedocs.io/en/latest/index.html
- FEniCS Community archived Q&A https://fenicsproject.org/qa/
- And simply Google it!
A final word:
A lot of great tools and numerical models are already developed and open-source. Before you start developing some exciting new tools or models, search online first. It is very likely that you will find open-source codes that do what you need along with nice tutorials to support you along the way!
Acknowledgement: A word of thanks goes to Simone Puel, a PhD candidate at UT Austin, who has supported my code development in FEniCS.