Some mixed boundary conditions in FEniCS

When you first start to use a piece of software, it is good to have some really stupid examples to refer to, which expert users would probably laugh at. For the noob, like me, it is sometimes difficult to figure out how to do even the most basic things. Take the following example:

You have some kind of partial differential equation involving a function F(z,t), in one time variable 0\leq t \leq T and one space variable z \in [0,L]. The given boundary and initial conditions are F(0,t) = g(t) and F(z,0) = 0. How to formulate these conditions in FEniCS?

Note that in the FEniCS implementation we are actually only considering a single variable, since the time differential will be incorporated by applying a finite difference-type calculation over specified time intervals.

First, we need to define the relevant boundaries. For the first one, F(0,t) = g(t), In FEniCS, we define a function that tests whether a point is on the boundary by

def GammaD(x, on_boundary):
    return near(x[0],0)

The use of “near” is because we are using floating point numbers, and can’t test for exact equality. Now that we can test for this boundary, we can specify the boundary condition:

g = Expression('...')
bc = DirichletBC(V, g, GammaD)

where V was specified earlier in the code, as usual. In my case, I wanted F(0,t) = \sin \omega t, so I set g(x[0],t) = (1-x[0])*\sin \omega t. What about the initial condition F(z,0) = 0? That is also taken care of by the expression for g. But does the restricted boundary condition take this initial condition into account? Since we later specify

u_n = interpolate(u_D, V)

and initially set t=0 in u_D, it is indeed specified. If we want to add Neumann boundary conditions, we don’t need to do anything more – they are included by default.

Credit where it is due: the post at https://sites.math.rutgers.edu/~falk/math575/Boundary-conditions.html was very helpful, as was http://vefur.simula.no/~hpl/homepage/fenics-tutorial/release-1.0/webm/timedep.html.

As I need to add more and trickier conditions, I’ll update this post.

Not an ad, and I’m not getting paid to put it here. Just a thing that works, and that did wonders for my mother, when all the drugs couldn’t:

Installing FEniCS to run with Jupyter Notebook

So, I have some PDEs that I need to solve numerically, and came across the FEniCS package. Problem is, the thing just did not want to work on my Mac. I tried several ways, including https://github.com/FEniCS/dolfinx#conda. I even tried to do the same on an Ubuntu installation, with no luck. Although I could import dolfinx successfully in the command line once the environment had been activated, I had no joy importing it in Jupyter Notebook or VSCode.

During the installation, I saw that there were several inconsistencies in my Anaconda installation, so I tried to remove them with

conda install anaconda

This took ages (more than 40 hours), so prepare yourself for a long haul. After this, I installed Notebook and then tried

conda create -n fenicsx-env
conda activate fenicsx-env 
conda install -c conda-forge fenics-dolfinx mpich pyvista

Still no luck when trying

python3 -c "import dolfinx"

(In a previous installation this was actually successful, but I still couldn’t import it from Notebook.)

In https://stackoverflow.com/questions/73108638/install-fenics-to-use-it-within-jupyter-lab, I saw that someone had some success by launching the environment in Anaconda Navigator, which I now attempted, and which gave me some errors on start up. To fix this, I tried

conda update anaconda-navigator
conda update navigator-updateranaconda-navigator --reset

which I found at https://stackoverflow.com/questions/46329455/anaconda-an-unexpected-error-ocurred-on-navigator-start-up. Note that this can also take a bloody long time. It also does not seem to work… In the meantime, I was getting desperate and started throwing commands around. I tried (from https://fenicsproject.org/qa/13194/how-to-use-fenics-in-jupyter-by-anaconda/):

conda create -n fenicsproject -c conda-forge python=3.11 jupyter fenics

(this will depend on your version of Python, of course). Now, I activated the environment and tried the import:

conda activate fenicsprojectpython3 -c "import fenics"

This ran without any errors, which at least gave me hope. Next, I opened Notebook and tried

from fenics import *

Success! For the first time, I did not get a “No module…” type error. Now I have to see if everything works as advertised. By the way, what I would have tried next would have been to do the install with Docker (which is actually recommended in the FEniCS manual). I should also mention that I did a pip3 install, which did not work. I will still see if this method is successful on Ubuntu as well, and let you know.

Spora Ransomware

Unfortunately, someone close to me got hit by Spora Ransomware a few days ago. This is quite a nasty piece of work, and if they’re telling the truth about their RSA encryption, not easily decrypted. We even considered paying the ransom at some point, but couldn’t even get on the website. Every time we tried to connect we got a “Server not found” message. Perhaps they’ve already taken the money and ran? There is a happy ending to this story though, which we’ll get to in a little bit.

First though, let’s discuss how to avoid this kind of thing happening in the future:

  1. Do not use Windows. For anything. I only have a Windows partition for gaming, but with more and more games available on Linux, this is starting to look like a flimsy excuse. I hardly get to play anything these days, anyway…
  2. Do not open suspicious email attachments. This one can be slightly tricky, because the email may look like someone you know has sent it. However, if you do not recognise the file extension, and you think it might be a legitimate communication, ask them to re-send it in a more acceptable format. (They will probably reply that they never sent that email.) And if the attachment asks you to install something in order to view it (e.g. a new font), never, ever, ever, ever comply!
  3. Have everything backed up. This one will save you after nearly any kind of attack. Preferably have some kind of cloud storage and automatic back up set. I do like to have many backups, so I occasionally back up all my documents and photos on an external hard drive as well. Things that are easily available online don’t really need this treatment, unless you have a really slow connection. This will mean that even if your entire system is fried, you can just install a new open-source OS and continue working. If you don’t want to pay for storage (you should, it’s worth it), you can get some free space at various providers. The most that I’ve seen is Mega, which gives you a cool 50GB for free.

Of course, in this case the above advice had not been followed in time. We were looking at the possibility of lost work, family photos that were archived nowhere else and a serious amount of financial documentation. After some trial and error, we managed to get nearly everything back by following the following steps, and without paying the ransom!

  1. Clean the computer. There are quite a few ways to do this. I used Malwarebytes and HitmanPro (look it up; I’m not going to link everything in this blog).
  2. Remove the startup file. Even after removing the malware, Firefox still opened on startup with the ransom note. Turns out there is a file which the software didn’t remove. Use msconfig to get to the start up applications and untick the box.
  3. Recover your files. You probably won’t be able to decrypt the files, but the ransomware fortunately did not encrypt all of your files, just the ones it could see. There are still many older versions of your files that hang around in your hard drive, that don’t show up in Windows Explorer. I found a rather nifty little program called Shadow Explorer to find and restore the files (Windows does have an option that let’s you do this for each file, but it is sucky). By using Shadow Explorer, we were able to recover all important documents, and only lost a page or two of work from the most recent document.

One other thing I did before starting to play with recovery was to make a full backup of the hard drive, after the bugs had been removed. That way, I knew that if I screwed things up even more and I could get hold of the authors, I still had the option of paying the ransom. But you don’t really want to do this, since you don’t want to encourage this kind of behaviour, and you also have no guarantee that they’ll give you the key!