Installing Fenics on Windows

Hello,

For my internship, I need to do some modelization using finite element. For this I tried to install Fenics but nothing is working for the moment.

I first tried to install Fenics using the recommendation. I download Ubuntu and then use the command to download Fenics and it worked. But I couldn’t use it. I couldn’t import from dolfin or fenics. The only way to use it do ‘fenicsproject run’ but it creates some kind of session where I couldn’t interact with the rest of my computer.

Since it didn’t work I tried to follow this tutorial: Install FeniCS on Windows - Section 1 - Linux in Windows 10(Linux subsystem)-Part 1 - YouTube.
So, I uninstall Anaconda that I had on windows and try to install it on Ubuntu. Then installing Fenics on Jupyter Notebook. Installing Anaconda was ok, but when I tried to install Fenics there is an error.
Malformed version string ‘~’.

I saw that the first problem was because I already had Anaconda when I installed it on Ubuntu and that I should uninstall everything and try it again. But now it’s not working anymore. It can’t communicate with the Ubuntu archive anymore.

I tried the second option but I have an error when I try to open Jupyter Notebook. It did open, I did an installation, but it took 4 hours and there was a really long message, like 10 pages, to say that he did something but not what I wanted and it’s still not working.

I also tried to use docker toolbox, but I can’t even open docker toolbox.

If you can help me i really appreciate it.
kev

Hello @kmj,

Could you post a bit more detail on your first attempt ? I understand you began with trying to use Docker - the fenicsproject run command starts a Docker container.

To me that seems like your best option still. Install a Ubuntu subsystem (I guess you used the Windows store), install Docker from the Windows Subsystem Linux (WSL) command line, rerun curl -s https://get.fenicsproject.org | bash then fenicsproject run in your project folder.

That should leave you inside a Docker container, with your project folder mounted and shared. It’s normal for you to have trouble interacting with the rest of your computer while inside a container, but running project python sciprts with python script.py should work fine inside the container.

For further information on fenics installation, I recommend this tutorial.

If you are not familiar with bash (Ubuntu default command line) or Docker (allows you to get fenics running in an efficient manner on your computer without worrying too much about dependencies) may I further recommend this tutorial and that one, respectively. In my experience, taking the time now for a little bit of reading on the tools you want to use will save you much effort down the road.

Best of luck.

The error message you reported says it was just a temporary problem with internet access.

Ubuntu already provides python packages, so best not to install Anaconda inside an Ubuntu installation. Otherwise there can be problems with mismatched library versions.

Running from jupyter notebook introduces extra complications. Use ipython3 from the command line instead.

Hello,

For my first attempt I been to cmd and just used the command wsl --install. It installed Ubuntu and I could do the 4 commands
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:fenics-packages/fenics
sudo apt-get update
sudo apt-get install fenics

Once it was installed i could only open the docker container and I didn’t understood how to get my files to run them. I will read the tutorials and try again

Yeah it look like it’s working rn. So I install it again but it can’t find dolfin.

I uninstall ubuntu and install it again and it’s finally working.

Thanks everyone

Ok for future reference, I don’t think the commands you provided make use of docker.

docker is an application to create and use containers, which you can think of as lean virtual machines. apt-get is a package manager. Both have their pros and cons :

  • A package manager might be less adept at handling dependencies
  • A docker container image is harder to change than your own machine’s package list
  • A docker container is less efficient in terms of memory space, insofar as it might provide dependencies that your system already
  • Conversely, a container is safer in terms of stability and reproducibility, since a docker image has its own libraries collaborators will execute your code on the same container

It’s great if your problem is resolved, but a reader on this thread might get apt-get and docker confused.

Bonne chance,

That’s a strange comment to make, hawkspar. Handling package dependencies is precisely what the package manager is adept at.

I didn’t mean that a package manager would be unable to install dependencies, I meant a package manager’s job is not to keep them clean and to avoid conflicts in the long run. To my understanding, it’s possible to break a conda or fenics installation using apt-get, it will require more effort in docker.

It seemed a worthy enough difference to point it out. Of course, both of them will install the software and its dependencies in the end.

It’s true that mixing apt and conda will break things. Mixing apt and pip regularly breaks things.

apt on its own is safe though. It’s designed to maintain package dependencies. It’s its job to avoid conflicts between the packages that it manages.

Hello,

I have another question. For the moment I have Fenics on Ubuntu and Spider, paraview on windows. So i need to code on spider to execute it on ubuntu for having the results on paraview. Is there a better configuration. Something where i could use everything (coding and viewing the results) on one software ? I would like to have something more simple.

Best regards,

The better configuration is not to use Windows. Install Ubuntu directly on the metal.

spyder and paraview are available on Ubuntu, so you’ll be certainly able to install them inside your linux subsystem. Within linux they run as X11 applications, so getting X11 to run from your virtual Ubuntu machine inside Windows will be the challenge. Some configuration will be needed to communicate the X11 display between the linux subsystem and the windows host system. I’m not sure what needs to be done exactly to make that work, or whether it’s difficult or easy. There’s likely to be some discussion about it somewhere. It might need installation of an X server on Windows, which would handle the display of the applications.

Arguably simpler to just use paraview on Windows as you’re doing now, if you’re not able to avoid Windows altogether.

I use Paraview too, but it’s not strictly speaking necessary to have this visualisation application to use FENICs. Depending on your use case, you may find in the tutorials some python script that does a good enough job for your needs.

But at a higher level I agree with @dparsons, WSL is good for installing stuff, but it’s going to be harder to get fancy applications running.

For the record, would probably have the same problems inside a docker container.

The main goal of my internship is the simulation of a yarn in a knitted fabric to see the residual strength in the yarn and how it can create some defect in the knitted fabric.

For the moment I am trying to understand Fenics with this 2 tutorials but it’s not that easy
https://comet-fenics.readthedocs.io/en/latest/demo/beam_buckling/beam_buckling.html
https://comet-fenics.readthedocs.io/en/latest/demo/beams_3D/beams_3D.html

For the moment i have trouble finding the meaning of the functions and I am not always sure what I can / should extract to see on paraview.

Anyway thanks for the information

Might I repeat my pointer to this book ? It’s long but it covers a lot of things, such as the difference between trial and test functions. You don’t need to read everything, but it could save you time down the road to pick up the basics.

In FENICs, any Function can be exported to Paraview pretty simply with dolfin.File("savefile.pvd") << functiontosave

I’m afraid I can’t help you more without a more specific question.

I am reading the book you recommended and it helped a lot to understand.
I am supposed to do this 2 tutorials and play with the values to understand how to use Fenics. For the moment I only tried on BeamBuckling.
Since I don’t fully understand the functions I wanted to see them. Like the finite element U and T. From what I understood it take the dimension of cell from the mesh with mesh.ufl_cell() so it should be 1D Lagrangian finite element. And the number should give the degree of the element. But I couldn’t check, since I didn’t found how to print this element.
I am also not sure how many dimension have V and how spliting the testfunction worked.
Lastly I couldn’t extract the graph we have in the presentation. I got the values but not the graph. Maybe it is because I put run_line_magic in comment but if I leave it I got some errors

I do not know which two tutorials you are referring to. Are these in the book ? Online ? Some other place ?

My understanding of dolfin.VectorElement is that it takes as argument the base function type, the mesh cell type, degree, and dimension. If left unspecified, the dimension of the VectorElement is inferred from the mesh dimension.

Splitting a MixedElement V=T\times U simply yields back finite elements T and U.

Again, I can’t help you without a more specific question or code, and I fear we’ve spun a tad off topic…

I’m talking about this code : Eulerian buckling of a beam — Numerical tours of continuum mechanics using FEniCS master documentation
I know it’s not really a tutorial but i teacher told me to do it to learn about Fenics but there is no explanation what’s so ever.
Here is the code:

from dolfin import *
from fenics import *
import numpy as np
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'notebook')

# dimension de la poutre
L = 10.
thick = Constant(0.03)
width = Constant(0.01)
E = Constant(70e3)
nu = Constant(0.) #coefficient de poisson

#Grandeur physique
EI = E*width*thick**3/12 # ** = exposant
GS = E/2/(1+nu)*thick*width # G module de cisaillement
kappa = Constant(5./6.)

# maillage
N = 100
mesh = IntervalMesh(N, 0, L) 

U = FiniteElement("CG", mesh.ufl_cell(), 2)
T = FiniteElement("CG", mesh.ufl_cell(), 1)
V = FunctionSpace(mesh, U*T)

u_ = TestFunction(V)
du = TrialFunction(V)
(w_, theta_) = split(u_)
(dw, dtheta) = split(du)

k_form = EI*inner(grad(theta_), grad(dtheta))*dx + kappa*GS*dot(grad(w_)[0]-theta_, grad(dw)[0]-dtheta)*dx
l_form = Constant(1.)*u_[0]*dx

def both_ends(x, on_boundary):
    return on_boundary
def left_end(x, on_boundary):
    return near(x[0], 0) and on_boundary

bc = [DirichletBC(V.sub(0), Constant(0.), both_ends),
      DirichletBC(V.sub(1), Constant(0.), left_end)]

K = PETScMatrix()
assemble_system(k_form, l_form, bc, A_tensor=K)

N0 = Constant(1e-3)
kg_form = N0*dot(grad(w_), grad(dw))*dx
KG = PETScMatrix()
assemble(kg_form, tensor=KG)
for bci in bc:
    bci.zero(KG)

eigensolver = SLEPcEigenSolver(K, KG)
eigensolver.parameters['problem_type'] = 'gen_hermitian'
eigensolver.parameters['spectral_transform'] = 'shift-and-invert'
eigensolver.parameters['spectral_shift'] = 1e-3
eigensolver.parameters['tolerance'] = 1e-12

N_eig = 3   # number of eigenvalues
print("Computing {} first eigenvalues...".format(N_eig))
eigensolver.solve(N_eig)

# Exact solution computation
from scipy.optimize import root
from math import tan
falpha = lambda x: tan(x)-x
alpha = lambda n: root(falpha, 0.99*(2*n+1)*pi/2.)['x'][0]

plt.figure()
# Extraction
print("Critical buckling loads:")
for i in range(N_eig):
    # Extract eigenpair
    r, c, rx, cx = eigensolver.get_eigenpair(i)
    
    critical_load_an = alpha(i+1)**2*float(EI/N0)/L**2
    print("Exact: {0:>10.5f}  FE: {1:>10.5f}  Rel. gap {2:1.2f}%%".format(
           critical_load_an, r, 100*(r/critical_load_an-1)))
    
    # Initialize function and assign eigenvector (renormalize for plotting)
    eigenmode = Function(V,name="Eigenvector "+str(i))
    eigenmode.vector()[:] = rx/np.max(np.abs(rx.get_local()))

    plot(eigenmode.sub(0), label="Buckling mode "+str(i+1))

plt.ylim((-1.2, 1.2))
plt.legend()
plt.show()

I think you understand U and T right. They are finite element basis for scalar quantities, so you could consider them as vector elements of dimension 1. Note this is independent from geometrical dimension. You could have a n dimension vector on a d dimension mesh. It makes no sense to plot these finite element basis as such, because they really represent the space your functions will be operating upon.

Their first argument is the basis type, which is in this case Continuous Galerkin, so a Lagrangian polynomial is expected. The second one is about the cell type, here segments, and the third one is about the degree of your basis functions, which is a key factor for numerical stability and interpolation quality.

V is a MixedElement of the two previous spaces. Since the other two are scalars, it is of dimension 2 with V.sub(0)=U and V.sub(1)=T. It is of course possible to defined vector valued basis in fenics but here the main draw is to have different degrees for interpolation.

Regarding your figure problem, if you’re still running in WSL you could have issues with the window manager. I suggest plt.savefig('somewhere.png') and looking at the obtained file instead of going for an interactive display inside WSL.

Thanks a lot.
I still have few questions, how does the split function work ?
And if I want to use this plt.savefig, do I need the run_line_magic ? Because when I have it I have the UnknowBackend problem.