Using FENICS to solve simple problems in mechanics

FEniCS is an opensource platform for solving PDEs.

FEniCS has much more capabilities than those which will be illustrated here, and it was chosen for three main reasons:

  1. Its opensource, you can use it where you want when you want and it will not require to obtain a license, connect to the Technion network or coming to work in the compter lab at the faulty. (so no excuses)

  2. The ease of getting started working with it and solving the probelms in the HW assignment.

  3. There is an abundnace of online information, tutrials and studying mateials, meaning thta if you find it interesting and usefull for other courses, you research project etc. you can easily pick it up and gain a new powerfull tool for your toolbox.

Having said this, you are more than welcome to solve the HWs with any FE oftware of your choice.

At the end of this document you will find links to some opensource FE packages which will answer your needs for the course.

just remebmebr that I am not profficient with all FE packages outhere and will be able to provide limited support or no support at all for some of them, the packages i can assist you with are marked

Getting Started

Before starting with the examples on using FEniCS for our purpuses, we first need to steup a few things.

  1. Install WSL2 on your computer. WSL2 is a Linux kernel, by Microsoft, allowing you to run linux distributions on your computer without leaving windows.

  2. Install the Conda package manager

  3. Install FEniCS using conda

  4. Install an IDE (Jupyter notebook in this case or choose your own)

WSL2 Installation

We will follow the installation described on Microsoft’s website.

The main steps are given here for your convinience but please go to the website to avoid complications and contact me if you need assistance.

First, please make sure your windows version is meeting the requirments for installing WSL2 (step to in the link). If not and you dont want to update it, contact me and we will work something out.

Step 1: enabling WSL

open PowerShell as an administrator : (Windows Key) and type powershell, now click the powershell icon (right key of your mouse) and choose “Run as administrator”

run the following command inside the powershell:

“dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart”

you should see something like this: wsl1

Step 2: enable VM feature

still inside the powershell (as an administrator) type:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

you should see something like this: wsl2

Now restart your computer.

If you have not enabled the virtualization capabilities on your bios this will be a good time to do so or you will get an error message when trying to install a linux distribution.

Step 3: Install WSL2

Use the link on Microsoft’s website and download the WSL2 kernel.

you should see something like this: wsl3

Good. Now install it.

Step 4; installing a distro

Install a linux distribution from the Microsoft store (I use Ubuntu 20.04 but its up to you).

Step 5: Terminal

I personally like using the windows terminal but again, this is up to you

Let’s go

Launch your chosen linux distro.

Install Conda (miniconda)

We will be using python quite often throughout the course. You can use Octave/Matlab/R just as well but the materials I distribute will by “Pythonic”.

Also, it is comfortable to install FEniCS through the Conda package manager and run it using the Python API. As before, this is just the way I am accustomed to doing things and you can choose ther methods, as listed on the FEniCS website.

To install Conda on your WSL2 you have two options:

  1. Install the full Anaconda package with multiple packages pre-installed

  2. Install Miniconda (just the package manager) followed by installing just the packages you need. Below you will find instructions for doing that.

Miniconda

The link above will take you to the Miniconda documentation.

  • To install Miniconda first open a directory inside your home dir on linux:

“cd ~” will take you to your home directory.

“mkdir miniconda3” will create a directory named miniconda3

  • Now we need to get the installation files:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh

conda1

  • Now to install :

bash ~/miniconda3/miniconda.sh -u -p ~/miniconda3

and follow the instructions on the screen.

conda2

** suggestion** - when being asked if you want the installer to initialize Conda for you type yes

  • you can now remove the installation file using

rm -rf ~/miniconda3/miniconda.sh

  • Last step :)

Close the terminal and open a new one. you will notice that now you have (base) written infront of the directory location

conda3

(base) is the name of the current conda enviroment.

Install required packages (option 1)

Install FEniCS

We will follow the installation guide on the FEniCS website.

First create a new enviroment named FMcourse or any other name you want. As a general note it is helpful to use different enviroments for different projects/workflows and it prevents you from messing up the packages dependencies.

type: “conda create -n FMcourse -c conda-forge fenics”

fenics1

Congratulations, you have installed FEniCS 2019.1.

to activate the FMcourse enviroment simply type “conda activate FMcourse”

You will see that (base) has changed to (FMcourse)

Install Jupyter notebook

Simply type: “conda install -c conda-forge notebook”

Once installed start the notebook by typing: “jupyter notebook”.

It will open up in your browser and will look like this:

fenics1

Install Matplotlib

Simply type: “conda install -c conda-forge matplotlib “

Install required packages (option 2)

Download the FMcourse.yml file and type:

conda env create –file FMcourse.yml

this will create an enviroment called FMcourse and instal required packages.

To activate the enviroment type conda activate FMcourse

he examples to follow rely heavily on numerical tours of continuum mechanics using FEniCS and I strongly suggest to go over it and extend your knowledge.

A few more opensource FE frameworks which are relatively easy to get started with

  1. FEATool is a matlab GUI that can be used with various other packages (including FEniCS)

  2. FEBio

  3. Calculix

  4. FreeFEM

  5. Range

  6. Elmer

  7. GetDP

  8. SfePy

Many of the above (and other non listed codes) can be easily interfaced with:

  1. Gmsh

  2. FREECAD (includes FEA capability)

  3. ONELAB