Setting up FEniCS for Windows in minutes: My suggested workflow

In this post I’ll show you how to set everything to start modelling and solving PDEs numerically in minutes. There are hundreds of possibilities based on methods and software. I chose to use the FEniCS project mainly because it was the tool used within my research group. However, it did not pass very long when I started approciating its transparency and flexibility, besides… common, it’s free!! FEniCS, stands for Finite Elements for Computing in Science (the -ni- in between placed just to make it sound nicer, as far as I know). It is an “open-source computing platform for the solution of PDEs” programmed in C++ and Python, which provides a nice combination between performance and ease-of-use. 

To be sure, there are two projects, one called FEniCS and another called FEniCSx. The former is the stable version which has been developed, tested and debugged for years by several researchers around the globe. The latter is a new version in development, featuring some new functionalities, such as support for new type of cells and elements and improvement in the memory parallelisation, among other things. To be fair, I have no experience with FEniCSx yet, so everything I’ll say here will apply only to the legacy version, FEniCS. Maybe in the short future I will take the time to try FEniCSx and let you know.

For more information about FEniCS, you can check its website here.

Now, with that being said, let’s start!

1. Install Docker

In order to use FEniCS, there are several alternatives, however, for Windows users the recommended one is to install FEniCS within a docker container. If you – like me when I started- have no idea of what docker is, this is a software that enables deployment and execution of software in the form of “containers”. In such container, a piece of software is packed with all required dependencies, including its operating system, which makes it possible to use it in different platforms without a large overhear. For more information you can always have a look in google about docker and containerization. To download docker for desktop you can click here

2. Install Visual Studio Code

Strictly speaking you do not need to have Visual Studio Code (VSC), in fact,you don’t even need an IDE (Integrated Development Enviroment) – you could write your codes in any text editor and run them through the command window-. However, I greatly recommend VSC. In principle, VSC is just a fancy text editor, which makes it very light, however you can add a bunch of extra functionalities by installing “Extensions”. This can be easilly downloaded here.

3. Install FEniCS

In order to install FEniCS, we need to download the corresponding docker image, and use this image to build our fenics container. In order to do so, you need first to choose an easy-to-remember working directory in you system. Here you will save all the files developed for your model, and will work as a communication gate between your (windows) system and the (linux) container’s system. Then make sure that you are located in that chose directory in the VSC terminal, you can do so by navigating in the terminal using the commands cd and ls. Once there, you just need to run the following command in the terminal of VSC (you could also use the command window or powershell, actually):

docker run -ti -p 127.0.0.1:8000:8000 ${pwd}:/home/fenics/shared -w /home/fenics/shared quay.io/fenicsproject/stable:current 

The installation will take sometime, especially to download the docker image, but if everything went well, the following text should be printed in your terminal. 

# FEniCS stable version image 

Welcome to FEniCS/stable!

This image provides a full-featured and optimized build of the stable release of FEniCS. 

To help you get started this image contains a number of demomprograms. Explore the demos by entering the 'demo' directory, for example:    

cd ~/demo/python/documented/poisson       python3 demo_poisson.py

fenics@281c191c265a:~/shared$ 

For more options of installation, you can also have a look at the FEniCS website.

4. Install Remote Container addon

From this moment on, you should be able to use FEniCS. There are different possible workflows for the development such as writing your code in .py files and executing them in interactive sessions of the container, or executing a jupyter notebook version of the container. However, in my opinion, the most convenient way of using FEniCS is by launching the container directly in Visual Studio Code. In order to do that, we need to download two extensions in VSC: Docker and Remote Container. To do so, go to the vertical bar in the left in VSC and click on the bottom for Extensions, you can then search for the two extensions mentioned.

Once installed and enabled, you should see two new bottoms appear on you left vertical bar. Go to the one corresponding to Remote – Containers and you should see the corresponding FEniCS container. Right click on it and choose “Attach to container”. This should open a new window, in this new window, click on the “Explorer” bottom, the first on the vertical left bar and open a new folder in the direction 

/home/fenics/shared/

You may need to install also the extension for Python, which allows VSC to identify python code, and activates all the IDE functionalities for python files. I also suggest to install in addition the extension for Jupyter Notebooks, which allows you to run interactives codes of Python that you can execute as you code. Probably not the best option for performance, but very helpful when exploring new things. 

After this, you should be able to write you own code directly in the container and use the functionalities of VSC such as debugging and testing for your code. 

5. Test FEniCS

Now, you are ready to go! You can test you FEniCS installation with the “Hello World” of PDEs: The Poisson equation, which can be found in the FEniCS tutorial, either in its web version or its printed version. I really recommend you to spend a few hours (maybe days) to implement and understand all the problems covered in the FEniCS tutorial. But for this quick test, you can copy the code corresponding to the Poisson problem demo and paste it in a new file in you own container. 

If everything went well, at this moment you should be able to run the code directly using the “Run” buttom from VSC (a triangle located near the upper right corner) and after a few seconds you should get something like this, as output in your terminal, or either from the terminal by running:

    python3 poisson.py

Finally, what we have been waiting for all this time: a nice and fancy colorful plot 😀

Good luck in your programming journey, and see you along the road 😉


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *