Basic queries from the fenics book, chapter 1, Set #1

I am using fenics version 2019.2.0.dev0. Yes, I am able to finally install it, after lots of efforts! Some of my queries might be trivial to you, but it would be good, if I can resolve as I go along the way to read and practice the codes in the book.

I am using Spyder IDE and Python 3.8. My apology beforehand, if the queries are too basic.

1.) How to resolve the indentation problem in this code?
This works:

def u0_boundary(x, on_boundary):
    tol = 1E-15
    return  abs(x[0]) < tol or \
            abs(x[1])< tol or \
            abs(x[0] - 1) < tol or \
            abs(x[1] - 1) < tol 

This does not work:

def u0_boundary(x, on_boundary):
    return  
            for n in range(2):
                tol = 1E-15
                abs(x[n]) < tol or \
                abs(x[n] - 1) < tol   

2.) What would be the Matlab equivalent of clc, clear in a script

I searched online, but the few tips provided are roundabouts.

3.) It seems that the

interactive()

is deprecated in dolfin version 2019.2.0.dev0. What would be the equivalent command? What I read, it seems to be a good function for preliminary observation.

4.) Do I need to set the solver parameters, like linear algebra backend, linear solver etc. before I call the solve()? I am asking it, as I do not see the difference, if I call before and after the solve()

5.) It seems that the following function has no effect whether the parameter is True or False. What is the usefulness of it? It does not print anything.

info(parameters, True)

or

info(parameters, False)

6.) How to provide a colorbar in a plot function? In matlab, we can use plot(matrix), colorbar

7.) The following two linear algebra backends do not work
MTL4, PETSc

8.) How to print the solver parameters?

solve(a == L, u, bc, solver_parameters = {“linear_solver”: “cg”, “preconditioner”: “ilu”})
print(solver_parameters[‘linear_solver’])

The print gives me error

NameError: name ‘solver_parameters’ is not defined

9.) It shows nothing command prompt. I used the canonical Poisson equation on a unit square problem in book or tutorial.

set_log_level(LogLevel.PROGRESS)
set_log_level(LogLevel.DEBUG)

For this I used the program d3_p2D.py of the fenics book and can be found in this URL

https://fenicsproject.org/pub/book/chapters/01/stationary/poisson/

10.) The variable explorer in Spyder looks weird, in comparison to what I have experienced in Matlab. All the variables in a fenics program are not listed there. I have seen some additional variables. What does the variable explorer mean in Spyder?

  1. I guess this is more of a python question than FEniCS.

This looks fine to me. At least evaluating the function itself, pe se. Check the lines before and after.

That is as much as there is to Spyder. I haven’t used it in a very long time. The answers here are your best friend.

  1. If you want to interactively visualize the solution as opposed to using Paraview, I would suggest using vtkplotter (now vedo I believe). See here and a list of examples here. A minimal example would be
from vedo.dolfin import plot as vtplot # if you are using `from dolfin import *`
vtplot(u) # u is your `dolfin` Function
  1. For simple problems, when using vanilla solve interface would give you the solution, I believe you need not. For more complicated and niche problems, you may have to fine tune your solver.

  2. You can get the keys of parameters by simply doing:

print(parameters.keys()) #this gives you the keys
print(parameters["key"]) # "key" is in the list `parameters.keys()`
  1. See this It is pretty much how you would do it in matplotlib which is the backend for plot

  2. Could you explain by what do you mean they do not work?

  3. You are passing a dictionary to solver_parameters. If you assign it to a variable first and then pass that, you can see what you want

solverParams = {“linear_solver”: “cg”, “preconditioner”: “ilu”}
solve(a == L, u, bc, solver_parameters = solverParams)

  1. You are setting the verbosity of dolfin’s log and not printing anything. You need to print the log level
print(get_log_level())

to print the current log_level

  1. It should print all the global variable names. As I have not used dolfin with Spyder I do not completely know what variable names are you seeing in addition to what you globally declare in your code. If you paste a screenshot, that would perhaps help in deciphering the issue.
3 Likes

Thanks for addressing all my queries. The boundary x and y values does not return properly in the function u0_boundary():

from __future__ import print_function
from dolfin import *
import matplotlib.pyplot as plt 

# Create mesh and define function space
mesh = UnitSquareMesh(6, 4)
V = FunctionSpace(mesh, "Lagrange", 1)

# Define boundary conditions
u0 = Expression("1 + x[0]*x[0] + 2*x[1]*x[1]", degree = 2)

def u0_boundary(x, on_boundary):
    for n in range(2):
        tol = 1E-15
        abs(x[n]) < tol or \
        abs(x[n] - 1) < tol    
    return x

bc = DirichletBC(V, u0, u0_boundary)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-6.0)
a = inner(nabla_grad(u), nabla_grad(v))*dx
L = f*v*dx

# Compute solution
#u = Function(V)
#solve(a == L, u, bc)

u = Function(V)
solve(a == L, u, bc, solver_parameters = {"linear_solver": "cg", "preconditioner": "ilu"})

#File('poisson/parameters.xml') << parameters


#info(parameters, verbose = True)

plot(u)
#plt.show()
plot(mesh)
#plt.show()

# Dump solution to file in VTK format
#file = File("poisson/solution.pvd")
#file << u

Here you are lacking some basic understanding of what you would like to achieve. As what you would like to return is a boolean (True/False) that is true whenever either of the conditions above are satisfied.
Thus the return type of your problem is wrong, and you do not do anything with the true/false statement that you have written above as

To fix this, I suggest the following:

def u0_boundary(x, on_boundary):
    truth = False
    for n in range(len(x)):
        tol = 1E-15
        truth = truth or (abs(x[n]) < tol or \
                          abs(x[n] - 1) < tol)
    return truth
1 Like

Ah I thought you had indentation issues.

Just as a side note you could also use dolfin’s near function to identify boundary (in your specific example on_boundary should be sufficient to return the boundary as it’s a unit square mesh)

def u0_boundary(x, on_boundary):
    return (near(x[0], 0.) or near(x[1], 1.) or near(x[1], 0.) or near(x[0], 1.)) and on_boundary